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