Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/mock/GrMockBuffer.h" |
| 9 | #include "src/gpu/mock/GrMockCaps.h" |
| 10 | #include "src/gpu/mock/GrMockGpu.h" |
| 11 | #include "src/gpu/mock/GrMockGpuCommandBuffer.h" |
| 12 | #include "src/gpu/mock/GrMockStencilAttachment.h" |
| 13 | #include "src/gpu/mock/GrMockTexture.h" |
Mike Klein | de2244c | 2018-12-04 11:16:08 -0500 | [diff] [blame] | 14 | #include <atomic> |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 15 | |
Brian Salomon | 8fe2427 | 2017-07-07 12:56:11 -0400 | [diff] [blame] | 16 | int GrMockGpu::NextInternalTextureID() { |
Mike Klein | de2244c | 2018-12-04 11:16:08 -0500 | [diff] [blame] | 17 | static std::atomic<int> nextID{1}; |
Chris Dalton | 351e80c | 2019-01-06 22:51:00 -0700 | [diff] [blame] | 18 | int id; |
| 19 | do { |
| 20 | id = nextID.fetch_add(1); |
| 21 | } while (0 == id); // Reserve 0 for an invalid ID. |
| 22 | return id; |
Brian Salomon | 8fe2427 | 2017-07-07 12:56:11 -0400 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | int GrMockGpu::NextExternalTextureID() { |
| 26 | // We use negative ints for the "testing only external textures" so they can easily be |
| 27 | // identified when debugging. |
Mike Klein | de2244c | 2018-12-04 11:16:08 -0500 | [diff] [blame] | 28 | static std::atomic<int> nextID{-1}; |
| 29 | return nextID--; |
Brian Salomon | 8fe2427 | 2017-07-07 12:56:11 -0400 | [diff] [blame] | 30 | } |
| 31 | |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 32 | int GrMockGpu::NextInternalRenderTargetID() { |
Mike Klein | de2244c | 2018-12-04 11:16:08 -0500 | [diff] [blame] | 33 | // We start off with large numbers to differentiate from texture IDs, even though they're |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 34 | // technically in a different space. |
Mike Klein | de2244c | 2018-12-04 11:16:08 -0500 | [diff] [blame] | 35 | static std::atomic<int> nextID{SK_MaxS32}; |
| 36 | return nextID--; |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | int GrMockGpu::NextExternalRenderTargetID() { |
| 40 | // We use large negative ints for the "testing only external render targets" so they can easily |
| 41 | // be identified when debugging. |
Mike Klein | de2244c | 2018-12-04 11:16:08 -0500 | [diff] [blame] | 42 | static std::atomic<int> nextID{SK_MinS32}; |
| 43 | return nextID++; |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 44 | } |
| 45 | |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 46 | sk_sp<GrGpu> GrMockGpu::Make(const GrMockOptions* mockOptions, |
| 47 | const GrContextOptions& contextOptions, GrContext* context) { |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 48 | static const GrMockOptions kDefaultOptions = GrMockOptions(); |
| 49 | if (!mockOptions) { |
| 50 | mockOptions = &kDefaultOptions; |
| 51 | } |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 52 | return sk_sp<GrGpu>(new GrMockGpu(context, *mockOptions, contextOptions)); |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 53 | } |
| 54 | |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 55 | GrGpuRTCommandBuffer* GrMockGpu::getCommandBuffer( |
Ethan Nicholas | 56d19a5 | 2018-10-15 11:26:20 -0400 | [diff] [blame] | 56 | GrRenderTarget* rt, GrSurfaceOrigin origin, const SkRect& bounds, |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 57 | const GrGpuRTCommandBuffer::LoadAndStoreInfo&, |
| 58 | const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo&) { |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 59 | return new GrMockGpuRTCommandBuffer(this, rt, origin); |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 60 | } |
| 61 | |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 62 | GrGpuTextureCommandBuffer* GrMockGpu::getCommandBuffer(GrTexture* texture, GrSurfaceOrigin origin) { |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 63 | return new GrMockGpuTextureCommandBuffer(texture, origin); |
| 64 | } |
| 65 | |
Robert Phillips | 5b5d84c | 2018-08-09 15:12:18 -0400 | [diff] [blame] | 66 | void GrMockGpu::submit(GrGpuCommandBuffer* buffer) { |
| 67 | if (buffer->asRTCommandBuffer()) { |
| 68 | this->submitCommandBuffer( |
| 69 | static_cast<GrMockGpuRTCommandBuffer*>(buffer->asRTCommandBuffer())); |
| 70 | } |
| 71 | |
| 72 | delete buffer; |
| 73 | } |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 74 | |
| 75 | void GrMockGpu::submitCommandBuffer(const GrMockGpuRTCommandBuffer* cmdBuffer) { |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 76 | for (int i = 0; i < cmdBuffer->numDraws(); ++i) { |
| 77 | fStats.incNumDraws(); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | GrMockGpu::GrMockGpu(GrContext* context, const GrMockOptions& options, |
| 82 | const GrContextOptions& contextOptions) |
Chris Dalton | 91ab155 | 2018-04-18 13:24:25 -0600 | [diff] [blame] | 83 | : INHERITED(context) |
| 84 | , fMockOptions(options) { |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 85 | fCaps.reset(new GrMockCaps(contextOptions, options)); |
| 86 | } |
| 87 | |
| 88 | sk_sp<GrTexture> GrMockGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted, |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 89 | const GrMipLevel texels[], int mipLevelCount) { |
Chris Dalton | 91ab155 | 2018-04-18 13:24:25 -0600 | [diff] [blame] | 90 | if (fMockOptions.fFailTextureAllocations) { |
| 91 | return nullptr; |
| 92 | } |
| 93 | |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 94 | GrMipMapsStatus mipMapsStatus = mipLevelCount > 1 ? GrMipMapsStatus::kValid |
| 95 | : GrMipMapsStatus::kNotAllocated; |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 96 | GrMockTextureInfo texInfo; |
| 97 | texInfo.fConfig = desc.fConfig; |
| 98 | texInfo.fID = NextInternalTextureID(); |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 99 | if (desc.fFlags & kRenderTarget_GrSurfaceFlag) { |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 100 | GrMockRenderTargetInfo rtInfo; |
| 101 | rtInfo.fConfig = desc.fConfig; |
| 102 | rtInfo.fID = NextInternalRenderTargetID(); |
| 103 | return sk_sp<GrTexture>(new GrMockTextureRenderTarget(this, budgeted, desc, mipMapsStatus, |
| 104 | texInfo, rtInfo)); |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 105 | } |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 106 | return sk_sp<GrTexture>(new GrMockTexture(this, budgeted, desc, mipMapsStatus, texInfo)); |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 107 | } |
| 108 | |
Greg Daniel | 4684f82 | 2018-03-08 15:27:36 -0500 | [diff] [blame] | 109 | sk_sp<GrTexture> GrMockGpu::onWrapBackendTexture(const GrBackendTexture& tex, |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 110 | GrWrapOwnership ownership, |
| 111 | GrWrapCacheable wrapType, GrIOType ioType) { |
Greg Daniel | 4684f82 | 2018-03-08 15:27:36 -0500 | [diff] [blame] | 112 | GrSurfaceDesc desc; |
| 113 | desc.fWidth = tex.width(); |
| 114 | desc.fHeight = tex.height(); |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 115 | |
| 116 | GrMockTextureInfo info; |
| 117 | SkAssertResult(tex.getMockTextureInfo(&info)); |
Greg Daniel | 4684f82 | 2018-03-08 15:27:36 -0500 | [diff] [blame] | 118 | desc.fConfig = info.fConfig; |
| 119 | |
| 120 | GrMipMapsStatus mipMapsStatus = tex.hasMipMaps() ? GrMipMapsStatus::kValid |
| 121 | : GrMipMapsStatus::kNotAllocated; |
| 122 | |
Brian Salomon | fa2ebea | 2019-01-24 15:58:58 -0500 | [diff] [blame] | 123 | return sk_sp<GrTexture>(new GrMockTexture(this, desc, mipMapsStatus, info, wrapType, ioType)); |
Greg Daniel | 4684f82 | 2018-03-08 15:27:36 -0500 | [diff] [blame] | 124 | } |
| 125 | |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 126 | sk_sp<GrTexture> GrMockGpu::onWrapRenderableBackendTexture(const GrBackendTexture& tex, |
| 127 | int sampleCnt, |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 128 | GrWrapOwnership ownership, |
| 129 | GrWrapCacheable cacheable) { |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 130 | GrSurfaceDesc desc; |
| 131 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 132 | desc.fWidth = tex.width(); |
| 133 | desc.fHeight = tex.height(); |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 134 | |
| 135 | GrMockTextureInfo texInfo; |
| 136 | SkAssertResult(tex.getMockTextureInfo(&texInfo)); |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 137 | desc.fConfig = texInfo.fConfig; |
| 138 | |
| 139 | GrMipMapsStatus mipMapsStatus = |
| 140 | tex.hasMipMaps() ? GrMipMapsStatus::kValid : GrMipMapsStatus::kNotAllocated; |
| 141 | |
| 142 | GrMockRenderTargetInfo rtInfo; |
| 143 | rtInfo.fConfig = texInfo.fConfig; |
| 144 | // The client gave us the texture ID but we supply the render target ID. |
| 145 | rtInfo.fID = NextInternalRenderTargetID(); |
| 146 | |
| 147 | return sk_sp<GrTexture>( |
Brian Salomon | aa6ca0a | 2019-01-24 16:03:07 -0500 | [diff] [blame] | 148 | new GrMockTextureRenderTarget(this, desc, mipMapsStatus, texInfo, rtInfo, cacheable)); |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | sk_sp<GrRenderTarget> GrMockGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& rt) { |
| 152 | GrSurfaceDesc desc; |
| 153 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 154 | desc.fWidth = rt.width(); |
| 155 | desc.fHeight = rt.height(); |
Greg Daniel | 323fbcf | 2018-04-10 13:46:30 -0400 | [diff] [blame] | 156 | |
| 157 | GrMockRenderTargetInfo info; |
| 158 | SkAssertResult(rt.getMockRenderTargetInfo(&info)); |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 159 | desc.fConfig = info.fConfig; |
| 160 | |
| 161 | return sk_sp<GrRenderTarget>( |
| 162 | new GrMockRenderTarget(this, GrMockRenderTarget::kWrapped, desc, info)); |
| 163 | } |
| 164 | |
| 165 | sk_sp<GrRenderTarget> GrMockGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex, |
| 166 | int sampleCnt) { |
| 167 | GrSurfaceDesc desc; |
| 168 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 169 | desc.fWidth = tex.width(); |
| 170 | desc.fHeight = tex.height(); |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 171 | |
| 172 | GrMockTextureInfo texInfo; |
| 173 | SkAssertResult(tex.getMockTextureInfo(&texInfo)); |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 174 | desc.fConfig = texInfo.fConfig; |
| 175 | desc.fSampleCnt = sampleCnt; |
| 176 | |
| 177 | GrMockRenderTargetInfo rtInfo; |
| 178 | rtInfo.fConfig = texInfo.fConfig; |
| 179 | // The client gave us the texture ID but we supply the render target ID. |
| 180 | rtInfo.fID = NextInternalRenderTargetID(); |
| 181 | |
| 182 | return sk_sp<GrRenderTarget>( |
| 183 | new GrMockRenderTarget(this, GrMockRenderTarget::kWrapped, desc, rtInfo)); |
| 184 | } |
| 185 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 186 | sk_sp<GrGpuBuffer> GrMockGpu::onCreateBuffer(size_t sizeInBytes, GrGpuBufferType type, |
| 187 | GrAccessPattern accessPattern, const void*) { |
| 188 | return sk_sp<GrGpuBuffer>(new GrMockBuffer(this, sizeInBytes, type, accessPattern)); |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | GrStencilAttachment* GrMockGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt, |
| 192 | int width, |
| 193 | int height) { |
| 194 | static constexpr int kBits = 8; |
| 195 | fStats.incStencilAttachmentCreates(); |
| 196 | return new GrMockStencilAttachment(this, width, height, kBits, rt->numColorSamples()); |
| 197 | } |
Brian Salomon | 8fe2427 | 2017-07-07 12:56:11 -0400 | [diff] [blame] | 198 | |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 199 | GrBackendTexture GrMockGpu::createTestingOnlyBackendTexture(int w, int h, |
| 200 | const GrBackendFormat& format, |
Robert Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 201 | GrMipMapped mipMapped, |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 202 | GrRenderable renderable, |
| 203 | const void* pixels, |
Robert Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 204 | size_t rowBytes) { |
| 205 | |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 206 | const GrPixelConfig* pixelConfig = format.getMockFormat(); |
| 207 | if (!pixelConfig) { |
| 208 | return GrBackendTexture(); // invalid |
| 209 | } |
| 210 | |
| 211 | if (!this->caps()->isConfigTexturable(*pixelConfig)) { |
Robert Phillips | 646f637 | 2018-09-25 09:31:10 -0400 | [diff] [blame] | 212 | return GrBackendTexture(); // invalid |
| 213 | } |
| 214 | |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 215 | GrMockTextureInfo info; |
Robert Phillips | 9dbcdcc | 2019-05-13 10:40:06 -0400 | [diff] [blame] | 216 | info.fConfig = *pixelConfig; |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 217 | info.fID = NextExternalTextureID(); |
| 218 | fOutstandingTestingOnlyTextureIDs.add(info.fID); |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 219 | return GrBackendTexture(w, h, mipMapped, info); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 220 | } |
| 221 | |
Robert Phillips | f0ced62 | 2019-05-16 09:06:25 -0400 | [diff] [blame^] | 222 | void GrMockGpu::deleteTestingOnlyBackendTexture(const GrBackendTexture& tex) { |
| 223 | SkASSERT(GrBackendApi::kMock == tex.backend()); |
| 224 | |
| 225 | GrMockTextureInfo info; |
| 226 | if (tex.getMockTextureInfo(&info)) { |
| 227 | fOutstandingTestingOnlyTextureIDs.remove(info.fID); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | #if GR_TEST_UTILS |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 232 | bool GrMockGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const { |
Greg Daniel | bdf12ad | 2018-10-12 09:31:11 -0400 | [diff] [blame] | 233 | SkASSERT(GrBackendApi::kMock == tex.backend()); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 234 | |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 235 | GrMockTextureInfo info; |
| 236 | if (!tex.getMockTextureInfo(&info)) { |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 237 | return false; |
| 238 | } |
| 239 | |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 240 | return fOutstandingTestingOnlyTextureIDs.contains(info.fID); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 241 | } |
| 242 | |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 243 | GrBackendRenderTarget GrMockGpu::createTestingOnlyBackendRenderTarget(int w, int h, |
Brian Osman | 2d010b6 | 2018-08-09 10:55:09 -0400 | [diff] [blame] | 244 | GrColorType colorType) { |
| 245 | auto config = GrColorTypeToPixelConfig(colorType, GrSRGBEncoded::kNo); |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 246 | if (kUnknown_GrPixelConfig == config) { |
| 247 | return {}; |
| 248 | } |
| 249 | GrMockRenderTargetInfo info = {config, NextExternalRenderTargetID()}; |
| 250 | static constexpr int kSampleCnt = 1; |
| 251 | static constexpr int kStencilBits = 8; |
| 252 | return {w, h, kSampleCnt, kStencilBits, info}; |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | void GrMockGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget&) {} |
| 256 | #endif |