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