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 | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame^] | 27 | sk_sp<GrGpu> GrMockGpu::Make(GrBackendContext backendContext, |
| 28 | const GrContextOptions& contextOptions, GrContext* context) { |
| 29 | return Make(reinterpret_cast<const GrMockOptions*>(backendContext), contextOptions, context); |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 30 | } |
| 31 | |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame^] | 32 | sk_sp<GrGpu> GrMockGpu::Make(const GrMockOptions* mockOptions, |
| 33 | const GrContextOptions& contextOptions, GrContext* context) { |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 34 | static const GrMockOptions kDefaultOptions = GrMockOptions(); |
| 35 | if (!mockOptions) { |
| 36 | mockOptions = &kDefaultOptions; |
| 37 | } |
Brian Salomon | 384fab4 | 2017-12-07 12:33:05 -0500 | [diff] [blame^] | 38 | return sk_sp<GrGpu>(new GrMockGpu(context, *mockOptions, contextOptions)); |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 42 | GrGpuRTCommandBuffer* GrMockGpu::createCommandBuffer( |
Robert Phillips | 19e51dc | 2017-08-09 09:30:51 -0400 | [diff] [blame] | 43 | GrRenderTarget* rt, GrSurfaceOrigin origin, |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 44 | const GrGpuRTCommandBuffer::LoadAndStoreInfo&, |
| 45 | const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo&) { |
| 46 | return new GrMockGpuRTCommandBuffer(this, rt, origin); |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 47 | } |
| 48 | |
Greg Daniel | 500d58b | 2017-08-24 15:59:33 -0400 | [diff] [blame] | 49 | GrGpuTextureCommandBuffer* GrMockGpu::createCommandBuffer(GrTexture* texture, |
| 50 | GrSurfaceOrigin origin) { |
| 51 | return new GrMockGpuTextureCommandBuffer(texture, origin); |
| 52 | } |
| 53 | |
| 54 | |
| 55 | void GrMockGpu::submitCommandBuffer(const GrMockGpuRTCommandBuffer* cmdBuffer) { |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 56 | for (int i = 0; i < cmdBuffer->numDraws(); ++i) { |
| 57 | fStats.incNumDraws(); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | GrMockGpu::GrMockGpu(GrContext* context, const GrMockOptions& options, |
| 62 | const GrContextOptions& contextOptions) |
| 63 | : INHERITED(context) { |
| 64 | fCaps.reset(new GrMockCaps(contextOptions, options)); |
| 65 | } |
| 66 | |
| 67 | sk_sp<GrTexture> GrMockGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted, |
Robert Phillips | 590533f | 2017-07-11 14:22:35 -0400 | [diff] [blame] | 68 | const GrMipLevel texels[], int mipLevelCount) { |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 69 | GrMipMapsStatus mipMapsStatus = mipLevelCount > 1 ? GrMipMapsStatus::kValid |
| 70 | : GrMipMapsStatus::kNotAllocated; |
Brian Salomon | 8fe2427 | 2017-07-07 12:56:11 -0400 | [diff] [blame] | 71 | GrMockTextureInfo info; |
| 72 | info.fID = NextInternalTextureID(); |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 73 | if (desc.fFlags & kRenderTarget_GrSurfaceFlag) { |
Brian Salomon | 8fe2427 | 2017-07-07 12:56:11 -0400 | [diff] [blame] | 74 | return sk_sp<GrTexture>( |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 75 | new GrMockTextureRenderTarget(this, budgeted, desc, mipMapsStatus, info)); |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 76 | } |
Greg Daniel | 0fc4d2d | 2017-10-12 11:23:36 -0400 | [diff] [blame] | 77 | return sk_sp<GrTexture>(new GrMockTexture(this, budgeted, desc, mipMapsStatus, info)); |
Brian Salomon | cfe910d | 2017-07-06 16:40:18 -0400 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | GrBuffer* GrMockGpu::onCreateBuffer(size_t sizeInBytes, GrBufferType type, |
| 81 | GrAccessPattern accessPattern, const void*) { |
| 82 | return new GrMockBuffer(this, sizeInBytes, type, accessPattern); |
| 83 | } |
| 84 | |
| 85 | GrStencilAttachment* GrMockGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt, |
| 86 | int width, |
| 87 | int height) { |
| 88 | static constexpr int kBits = 8; |
| 89 | fStats.incStencilAttachmentCreates(); |
| 90 | return new GrMockStencilAttachment(this, width, height, kBits, rt->numColorSamples()); |
| 91 | } |
Brian Salomon | 8fe2427 | 2017-07-07 12:56:11 -0400 | [diff] [blame] | 92 | |
| 93 | GrBackendObject GrMockGpu::createTestingOnlyBackendTexture(void* pixels, int w, int h, |
Greg Daniel | 177e695 | 2017-10-12 12:27:11 -0400 | [diff] [blame] | 94 | GrPixelConfig config, bool isRT, |
| 95 | GrMipMapped) { |
Brian Salomon | 8fe2427 | 2017-07-07 12:56:11 -0400 | [diff] [blame] | 96 | auto info = new GrMockTextureInfo; |
| 97 | info->fID = NextExternalTextureID(); |
| 98 | fOutstandingTestingOnlyTextureIDs.add(info->fID); |
| 99 | return reinterpret_cast<GrBackendObject>(info); |
| 100 | } |
| 101 | |
| 102 | bool GrMockGpu::isTestingOnlyBackendTexture(GrBackendObject object) const { |
| 103 | return fOutstandingTestingOnlyTextureIDs.contains( |
| 104 | reinterpret_cast<const GrMockTextureInfo*>(object)->fID); |
| 105 | } |
| 106 | |
| 107 | void GrMockGpu::deleteTestingOnlyBackendTexture(GrBackendObject object, bool abandonTexture) { |
| 108 | auto info = reinterpret_cast<const GrMockTextureInfo*>(object); |
| 109 | fOutstandingTestingOnlyTextureIDs.remove(info->fID); |
| 110 | delete info; |
| 111 | } |