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
NextInternalTextureID()15 int GrMockGpu::NextInternalTextureID() {
16 static int gID = 0;
17 return sk_atomic_inc(&gID) + 1;
18 }
19
NextExternalTextureID()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
Make(GrBackendContext backendContext,const GrContextOptions & contextOptions,GrContext * context)27 sk_sp<GrGpu> GrMockGpu::Make(GrBackendContext backendContext,
28 const GrContextOptions& contextOptions, GrContext* context) {
29 return Make(reinterpret_cast<const GrMockOptions*>(backendContext), contextOptions, context);
30 }
31
Make(const GrMockOptions * mockOptions,const GrContextOptions & contextOptions,GrContext * context)32 sk_sp<GrGpu> GrMockGpu::Make(const GrMockOptions* mockOptions,
33 const GrContextOptions& contextOptions, GrContext* context) {
34 static const GrMockOptions kDefaultOptions = GrMockOptions();
35 if (!mockOptions) {
36 mockOptions = &kDefaultOptions;
37 }
38 return sk_sp<GrGpu>(new GrMockGpu(context, *mockOptions, contextOptions));
39 }
40
41
createCommandBuffer(GrRenderTarget * rt,GrSurfaceOrigin origin,const GrGpuRTCommandBuffer::LoadAndStoreInfo &,const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo &)42 GrGpuRTCommandBuffer* GrMockGpu::createCommandBuffer(
43 GrRenderTarget* rt, GrSurfaceOrigin origin,
44 const GrGpuRTCommandBuffer::LoadAndStoreInfo&,
45 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo&) {
46 return new GrMockGpuRTCommandBuffer(this, rt, origin);
47 }
48
createCommandBuffer(GrTexture * texture,GrSurfaceOrigin origin)49 GrGpuTextureCommandBuffer* GrMockGpu::createCommandBuffer(GrTexture* texture,
50 GrSurfaceOrigin origin) {
51 return new GrMockGpuTextureCommandBuffer(texture, origin);
52 }
53
54
submitCommandBuffer(const GrMockGpuRTCommandBuffer * cmdBuffer)55 void GrMockGpu::submitCommandBuffer(const GrMockGpuRTCommandBuffer* cmdBuffer) {
56 for (int i = 0; i < cmdBuffer->numDraws(); ++i) {
57 fStats.incNumDraws();
58 }
59 }
60
GrMockGpu(GrContext * context,const GrMockOptions & options,const GrContextOptions & contextOptions)61 GrMockGpu::GrMockGpu(GrContext* context, const GrMockOptions& options,
62 const GrContextOptions& contextOptions)
63 : INHERITED(context) {
64 fCaps.reset(new GrMockCaps(contextOptions, options));
65 }
66
onCreateTexture(const GrSurfaceDesc & desc,SkBudgeted budgeted,const GrMipLevel texels[],int mipLevelCount)67 sk_sp<GrTexture> GrMockGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
68 const GrMipLevel texels[], int mipLevelCount) {
69 GrMipMapsStatus mipMapsStatus = mipLevelCount > 1 ? GrMipMapsStatus::kValid
70 : GrMipMapsStatus::kNotAllocated;
71 GrMockTextureInfo info;
72 info.fID = NextInternalTextureID();
73 if (desc.fFlags & kRenderTarget_GrSurfaceFlag) {
74 return sk_sp<GrTexture>(
75 new GrMockTextureRenderTarget(this, budgeted, desc, mipMapsStatus, info));
76 }
77 return sk_sp<GrTexture>(new GrMockTexture(this, budgeted, desc, mipMapsStatus, info));
78 }
79
onCreateBuffer(size_t sizeInBytes,GrBufferType type,GrAccessPattern accessPattern,const void *)80 GrBuffer* GrMockGpu::onCreateBuffer(size_t sizeInBytes, GrBufferType type,
81 GrAccessPattern accessPattern, const void*) {
82 return new GrMockBuffer(this, sizeInBytes, type, accessPattern);
83 }
84
createStencilAttachmentForRenderTarget(const GrRenderTarget * rt,int width,int height)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 }
92
createTestingOnlyBackendTexture(void * pixels,int w,int h,GrPixelConfig config,bool isRT,GrMipMapped)93 GrBackendTexture GrMockGpu::createTestingOnlyBackendTexture(void* pixels, int w, int h,
94 GrPixelConfig config, bool isRT,
95 GrMipMapped) {
96 GrMockTextureInfo info;
97 info.fID = NextExternalTextureID();
98 fOutstandingTestingOnlyTextureIDs.add(info.fID);
99 return GrBackendTexture(w, h, config, info);
100 }
101
isTestingOnlyBackendTexture(const GrBackendTexture & tex) const102 bool GrMockGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
103 SkASSERT(kMock_GrBackend == tex.backend());
104
105 const GrMockTextureInfo* info = tex.getMockTextureInfo();
106 if (!info) {
107 return false;
108 }
109
110 return fOutstandingTestingOnlyTextureIDs.contains(info->fID);
111 }
112
deleteTestingOnlyBackendTexture(GrBackendTexture * tex,bool abandonTexture)113 void GrMockGpu::deleteTestingOnlyBackendTexture(GrBackendTexture* tex, bool abandonTexture) {
114 SkASSERT(kMock_GrBackend == tex->backend());
115
116 const GrMockTextureInfo* info = tex->getMockTextureInfo();
117 if (info) {
118 fOutstandingTestingOnlyTextureIDs.remove(info->fID);
119 }
120 }
121