blob: 397e1a1ca95746e9814b56e4dae2a53a0caaa500 [file] [log] [blame]
Brian Salomoncfe910d2017-07-06 16:40:18 -04001/*
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 Salomon8fe24272017-07-07 12:56:11 -040015int GrMockGpu::NextInternalTextureID() {
16 static int gID = 0;
17 return sk_atomic_inc(&gID) + 1;
18}
19
20int 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 Salomoncfe910d2017-07-06 16:40:18 -040027GrGpu* GrMockGpu::Create(GrBackendContext backendContext, const GrContextOptions& contextOptions,
28 GrContext* context) {
Greg Daniel02611d92017-07-25 10:05:01 -040029 return Create(reinterpret_cast<const GrMockOptions*>(backendContext), contextOptions, context);
Brian Salomoncfe910d2017-07-06 16:40:18 -040030}
31
Greg Daniel02611d92017-07-25 10:05:01 -040032GrGpu* 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 Phillips95214472017-08-08 18:00:03 -040042GrGpuCommandBuffer* GrMockGpu::createCommandBuffer(
Robert Phillips19e51dc2017-08-09 09:30:51 -040043 GrRenderTarget* rt, GrSurfaceOrigin origin,
Robert Phillips95214472017-08-08 18:00:03 -040044 const GrGpuCommandBuffer::LoadAndStoreInfo&,
45 const GrGpuCommandBuffer::StencilLoadAndStoreInfo&) {
Robert Phillips19e51dc2017-08-09 09:30:51 -040046 return new GrMockGpuCommandBuffer(this, rt, origin);
Brian Salomoncfe910d2017-07-06 16:40:18 -040047}
48
49void GrMockGpu::submitCommandBuffer(const GrMockGpuCommandBuffer* cmdBuffer) {
50 for (int i = 0; i < cmdBuffer->numDraws(); ++i) {
51 fStats.incNumDraws();
52 }
53}
54
55GrMockGpu::GrMockGpu(GrContext* context, const GrMockOptions& options,
56 const GrContextOptions& contextOptions)
57 : INHERITED(context) {
58 fCaps.reset(new GrMockCaps(contextOptions, options));
59}
60
61sk_sp<GrTexture> GrMockGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
Robert Phillips590533f2017-07-11 14:22:35 -040062 const GrMipLevel texels[], int mipLevelCount) {
63 bool hasMipLevels = mipLevelCount > 1;
Brian Salomon8fe24272017-07-07 12:56:11 -040064 GrMockTextureInfo info;
65 info.fID = NextInternalTextureID();
Brian Salomoncfe910d2017-07-06 16:40:18 -040066 if (desc.fFlags & kRenderTarget_GrSurfaceFlag) {
Brian Salomon8fe24272017-07-07 12:56:11 -040067 return sk_sp<GrTexture>(
68 new GrMockTextureRenderTarget(this, budgeted, desc, hasMipLevels, info));
Brian Salomoncfe910d2017-07-06 16:40:18 -040069 }
Brian Salomon8fe24272017-07-07 12:56:11 -040070 return sk_sp<GrTexture>(new GrMockTexture(this, budgeted, desc, hasMipLevels, info));
Brian Salomoncfe910d2017-07-06 16:40:18 -040071}
72
73GrBuffer* GrMockGpu::onCreateBuffer(size_t sizeInBytes, GrBufferType type,
74 GrAccessPattern accessPattern, const void*) {
75 return new GrMockBuffer(this, sizeInBytes, type, accessPattern);
76}
77
78GrStencilAttachment* 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 Salomon8fe24272017-07-07 12:56:11 -040085
86GrBackendObject 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
94bool GrMockGpu::isTestingOnlyBackendTexture(GrBackendObject object) const {
95 return fOutstandingTestingOnlyTextureIDs.contains(
96 reinterpret_cast<const GrMockTextureInfo*>(object)->fID);
97}
98
99void GrMockGpu::deleteTestingOnlyBackendTexture(GrBackendObject object, bool abandonTexture) {
100 auto info = reinterpret_cast<const GrMockTextureInfo*>(object);
101 fOutstandingTestingOnlyTextureIDs.remove(info->fID);
102 delete info;
103}