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 | |
| 8 | #include "GrMockGpu.h" |
| 9 | #include "GrMockBuffer.h" |
| 10 | #include "GrMockCaps.h" |
| 11 | #include "GrMockGpuCommandBuffer.h" |
| 12 | #include "GrMockStencilAttachment.h" |
| 13 | #include "GrMockTexture.h" |
| 14 | |
Brian Salomon | 8fe2427 | 2017-07-07 12:56:11 -0400 | [diff] [blame] | 15 | int GrMockGpu::NextInternalTextureID() { |
| 16 | static int gID = 0; |
| 17 | return sk_atomic_inc(&gID) + 1; |
| 18 | } |
| 19 | |
| 20 | int GrMockGpu::NextExternalTextureID() { |
| 21 | // We use negative ints for the "testing only external textures" so they can easily be |
| 22 | // identified when debugging. |
| 23 | static int gID = 0; |
| 24 | return sk_atomic_dec(&gID) - 1; |
| 25 | } |
| 26 | |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 27 | int GrMockGpu::NextInternalRenderTargetID() { |
| 28 | // We start off with large numbers to differentiate from texture IDs, even though their |
| 29 | // technically in a different space. |
| 30 | static int gID = SK_MaxS32; |
| 31 | return sk_atomic_dec(&gID); |
| 32 | } |
| 33 | |
| 34 | int GrMockGpu::NextExternalRenderTargetID() { |
| 35 | // We use large negative ints for the "testing only external render targets" so they can easily |
| 36 | // be identified when debugging. |
| 37 | static int gID = SK_MinS32; |
| 38 | return sk_atomic_inc(&gID); |
| 39 | } |
| 40 | |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 41 | sk_sp<GrGpu> GrMockGpu::Make(const GrMockOptions* mockOptions, |
| 42 | const GrContextOptions& contextOptions, GrContext* context) { |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 43 | static const GrMockOptions kDefaultOptions = GrMockOptions(); |
| 44 | if (!mockOptions) { |
| 45 | mockOptions = &kDefaultOptions; |
| 46 | } |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame] | 47 | return sk_sp<GrGpu>(new GrMockGpu(context, *mockOptions, contextOptions)); |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 51 | GrGpuRTCommandBuffer* GrMockGpu::createCommandBuffer( |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 52 | GrRenderTarget* rt, GrSurfaceOrigin origin, |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 53 | const GrGpuRTCommandBuffer::LoadAndStoreInfo&, |
| 54 | const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo&) { |
| 55 | return new GrMockGpuRTCommandBuffer(this, rt, origin); |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 56 | } |
| 57 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 58 | GrGpuTextureCommandBuffer* GrMockGpu::createCommandBuffer(GrTexture* texture, |
| 59 | GrSurfaceOrigin origin) { |
| 60 | return new GrMockGpuTextureCommandBuffer(texture, origin); |
| 61 | } |
| 62 | |
| 63 | |
| 64 | void GrMockGpu::submitCommandBuffer(const GrMockGpuRTCommandBuffer* cmdBuffer) { |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 65 | for (int i = 0; i < cmdBuffer->numDraws(); ++i) { |
| 66 | fStats.incNumDraws(); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | GrMockGpu::GrMockGpu(GrContext* context, const GrMockOptions& options, |
| 71 | const GrContextOptions& contextOptions) |
| 72 | : INHERITED(context) { |
| 73 | fCaps.reset(new GrMockCaps(contextOptions, options)); |
| 74 | } |
| 75 | |
| 76 | sk_sp<GrTexture> GrMockGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted, |
Brian Salomon | 58389b9 | 2018-03-07 13:01:25 -0500 | [diff] [blame] | 77 | const GrMipLevel texels[], int mipLevelCount) { |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 78 | GrMipMapsStatus mipMapsStatus = mipLevelCount > 1 ? GrMipMapsStatus::kValid |
| 79 | : GrMipMapsStatus::kNotAllocated; |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 80 | GrMockTextureInfo texInfo; |
| 81 | texInfo.fConfig = desc.fConfig; |
| 82 | texInfo.fID = NextInternalTextureID(); |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 83 | if (desc.fFlags & kRenderTarget_GrSurfaceFlag) { |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 84 | GrMockRenderTargetInfo rtInfo; |
| 85 | rtInfo.fConfig = desc.fConfig; |
| 86 | rtInfo.fID = NextInternalRenderTargetID(); |
| 87 | return sk_sp<GrTexture>(new GrMockTextureRenderTarget(this, budgeted, desc, mipMapsStatus, |
| 88 | texInfo, rtInfo)); |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 89 | } |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 90 | return sk_sp<GrTexture>(new GrMockTexture(this, budgeted, desc, mipMapsStatus, texInfo)); |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 91 | } |
| 92 | |
Greg Daniel | 4684f82 | 2018-03-08 15:27:36 -0500 | [diff] [blame] | 93 | sk_sp<GrTexture> GrMockGpu::onWrapBackendTexture(const GrBackendTexture& tex, |
| 94 | GrWrapOwnership ownership) { |
| 95 | GrSurfaceDesc desc; |
| 96 | desc.fWidth = tex.width(); |
| 97 | desc.fHeight = tex.height(); |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 98 | |
| 99 | GrMockTextureInfo info; |
| 100 | SkAssertResult(tex.getMockTextureInfo(&info)); |
Greg Daniel | 4684f82 | 2018-03-08 15:27:36 -0500 | [diff] [blame] | 101 | desc.fConfig = info.fConfig; |
| 102 | |
| 103 | GrMipMapsStatus mipMapsStatus = tex.hasMipMaps() ? GrMipMapsStatus::kValid |
| 104 | : GrMipMapsStatus::kNotAllocated; |
| 105 | |
| 106 | return sk_sp<GrTexture>(new GrMockTexture(this, GrMockTexture::kWrapped, desc, mipMapsStatus, |
| 107 | info)); |
| 108 | } |
| 109 | |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 110 | sk_sp<GrTexture> GrMockGpu::onWrapRenderableBackendTexture(const GrBackendTexture& tex, |
| 111 | int sampleCnt, |
| 112 | GrWrapOwnership ownership) { |
| 113 | GrSurfaceDesc desc; |
| 114 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 115 | desc.fWidth = tex.width(); |
| 116 | desc.fHeight = tex.height(); |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 117 | |
| 118 | GrMockTextureInfo texInfo; |
| 119 | SkAssertResult(tex.getMockTextureInfo(&texInfo)); |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 120 | desc.fConfig = texInfo.fConfig; |
| 121 | |
| 122 | GrMipMapsStatus mipMapsStatus = |
| 123 | tex.hasMipMaps() ? GrMipMapsStatus::kValid : GrMipMapsStatus::kNotAllocated; |
| 124 | |
| 125 | GrMockRenderTargetInfo rtInfo; |
| 126 | rtInfo.fConfig = texInfo.fConfig; |
| 127 | // The client gave us the texture ID but we supply the render target ID. |
| 128 | rtInfo.fID = NextInternalRenderTargetID(); |
| 129 | |
| 130 | return sk_sp<GrTexture>( |
| 131 | new GrMockTextureRenderTarget(this, desc, mipMapsStatus, texInfo, rtInfo)); |
| 132 | } |
| 133 | |
| 134 | sk_sp<GrRenderTarget> GrMockGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& rt) { |
| 135 | GrSurfaceDesc desc; |
| 136 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 137 | desc.fWidth = rt.width(); |
| 138 | desc.fHeight = rt.height(); |
Greg Daniel | 323fbcf | 2018-04-10 13:46:30 -0400 | [diff] [blame^] | 139 | |
| 140 | GrMockRenderTargetInfo info; |
| 141 | SkAssertResult(rt.getMockRenderTargetInfo(&info)); |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 142 | desc.fConfig = info.fConfig; |
| 143 | |
| 144 | return sk_sp<GrRenderTarget>( |
| 145 | new GrMockRenderTarget(this, GrMockRenderTarget::kWrapped, desc, info)); |
| 146 | } |
| 147 | |
| 148 | sk_sp<GrRenderTarget> GrMockGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex, |
| 149 | int sampleCnt) { |
| 150 | GrSurfaceDesc desc; |
| 151 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 152 | desc.fWidth = tex.width(); |
| 153 | desc.fHeight = tex.height(); |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 154 | |
| 155 | GrMockTextureInfo texInfo; |
| 156 | SkAssertResult(tex.getMockTextureInfo(&texInfo)); |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 157 | desc.fConfig = texInfo.fConfig; |
| 158 | desc.fSampleCnt = sampleCnt; |
| 159 | |
| 160 | GrMockRenderTargetInfo rtInfo; |
| 161 | rtInfo.fConfig = texInfo.fConfig; |
| 162 | // The client gave us the texture ID but we supply the render target ID. |
| 163 | rtInfo.fID = NextInternalRenderTargetID(); |
| 164 | |
| 165 | return sk_sp<GrRenderTarget>( |
| 166 | new GrMockRenderTarget(this, GrMockRenderTarget::kWrapped, desc, rtInfo)); |
| 167 | } |
| 168 | |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 169 | GrBuffer* GrMockGpu::onCreateBuffer(size_t sizeInBytes, GrBufferType type, |
| 170 | GrAccessPattern accessPattern, const void*) { |
| 171 | return new GrMockBuffer(this, sizeInBytes, type, accessPattern); |
| 172 | } |
| 173 | |
| 174 | GrStencilAttachment* GrMockGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt, |
| 175 | int width, |
| 176 | int height) { |
| 177 | static constexpr int kBits = 8; |
| 178 | fStats.incStencilAttachmentCreates(); |
| 179 | return new GrMockStencilAttachment(this, width, height, kBits, rt->numColorSamples()); |
| 180 | } |
Brian Salomon | 8fe2427 | 2017-07-07 12:56:11 -0400 | [diff] [blame] | 181 | |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 182 | #if GR_TEST_UTILS |
Brian Salomon | 52e943a | 2018-03-13 09:32:39 -0400 | [diff] [blame] | 183 | GrBackendTexture GrMockGpu::createTestingOnlyBackendTexture(const void* pixels, int w, int h, |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 184 | GrPixelConfig config, bool isRT, |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 185 | GrMipMapped mipMapped) { |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 186 | GrMockTextureInfo info; |
Robert Phillips | fc711a2 | 2018-02-13 17:03:00 -0500 | [diff] [blame] | 187 | info.fConfig = config; |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 188 | info.fID = NextExternalTextureID(); |
| 189 | fOutstandingTestingOnlyTextureIDs.add(info.fID); |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 190 | return GrBackendTexture(w, h, mipMapped, info); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | bool GrMockGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const { |
| 194 | SkASSERT(kMock_GrBackend == tex.backend()); |
| 195 | |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 196 | GrMockTextureInfo info; |
| 197 | if (!tex.getMockTextureInfo(&info)) { |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 198 | return false; |
| 199 | } |
| 200 | |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 201 | return fOutstandingTestingOnlyTextureIDs.contains(info.fID); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 202 | } |
| 203 | |
Brian Salomon | 26102cb | 2018-03-09 09:33:19 -0500 | [diff] [blame] | 204 | void GrMockGpu::deleteTestingOnlyBackendTexture(const GrBackendTexture& tex) { |
| 205 | SkASSERT(kMock_GrBackend == tex.backend()); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 206 | |
Greg Daniel | 52e16d9 | 2018-04-10 09:34:07 -0400 | [diff] [blame] | 207 | GrMockTextureInfo info; |
| 208 | if (tex.getMockTextureInfo(&info)) { |
| 209 | fOutstandingTestingOnlyTextureIDs.remove(info.fID); |
Robert Phillips | d21b2a5 | 2017-12-12 13:01:25 -0500 | [diff] [blame] | 210 | } |
| 211 | } |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 212 | |
Brian Salomon | 0c51eea | 2018-03-09 17:02:09 -0500 | [diff] [blame] | 213 | GrBackendRenderTarget GrMockGpu::createTestingOnlyBackendRenderTarget(int w, int h, |
| 214 | GrColorType colorType, |
| 215 | GrSRGBEncoded srgbEncoded) { |
| 216 | auto config = GrColorTypeToPixelConfig(colorType, srgbEncoded); |
| 217 | if (kUnknown_GrPixelConfig == config) { |
| 218 | return {}; |
| 219 | } |
| 220 | GrMockRenderTargetInfo info = {config, NextExternalRenderTargetID()}; |
| 221 | static constexpr int kSampleCnt = 1; |
| 222 | static constexpr int kStencilBits = 8; |
| 223 | return {w, h, kSampleCnt, kStencilBits, info}; |
Brian Salomon | f865b05 | 2018-03-09 09:01:53 -0500 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | void GrMockGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget&) {} |
| 227 | #endif |