blob: 5969f2f1d28d7c29d122ef534935634f2b726650 [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 Salomon384fab42017-12-07 12:33:05 -050027sk_sp<GrGpu> GrMockGpu::Make(GrBackendContext backendContext,
28 const GrContextOptions& contextOptions, GrContext* context) {
29 return Make(reinterpret_cast<const GrMockOptions*>(backendContext), contextOptions, context);
Brian Salomoncfe910d2017-07-06 16:40:18 -040030}
31
Brian Salomon384fab42017-12-07 12:33:05 -050032sk_sp<GrGpu> GrMockGpu::Make(const GrMockOptions* mockOptions,
33 const GrContextOptions& contextOptions, GrContext* context) {
Greg Daniel02611d92017-07-25 10:05:01 -040034 static const GrMockOptions kDefaultOptions = GrMockOptions();
35 if (!mockOptions) {
36 mockOptions = &kDefaultOptions;
37 }
Brian Salomon384fab42017-12-07 12:33:05 -050038 return sk_sp<GrGpu>(new GrMockGpu(context, *mockOptions, contextOptions));
Greg Daniel02611d92017-07-25 10:05:01 -040039}
40
41
Greg Daniel500d58b2017-08-24 15:59:33 -040042GrGpuRTCommandBuffer* GrMockGpu::createCommandBuffer(
Robert Phillips19e51dc2017-08-09 09:30:51 -040043 GrRenderTarget* rt, GrSurfaceOrigin origin,
Greg Daniel500d58b2017-08-24 15:59:33 -040044 const GrGpuRTCommandBuffer::LoadAndStoreInfo&,
45 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo&) {
46 return new GrMockGpuRTCommandBuffer(this, rt, origin);
Brian Salomoncfe910d2017-07-06 16:40:18 -040047}
48
Greg Daniel500d58b2017-08-24 15:59:33 -040049GrGpuTextureCommandBuffer* GrMockGpu::createCommandBuffer(GrTexture* texture,
50 GrSurfaceOrigin origin) {
51 return new GrMockGpuTextureCommandBuffer(texture, origin);
52}
53
54
55void GrMockGpu::submitCommandBuffer(const GrMockGpuRTCommandBuffer* cmdBuffer) {
Brian Salomoncfe910d2017-07-06 16:40:18 -040056 for (int i = 0; i < cmdBuffer->numDraws(); ++i) {
57 fStats.incNumDraws();
58 }
59}
60
61GrMockGpu::GrMockGpu(GrContext* context, const GrMockOptions& options,
62 const GrContextOptions& contextOptions)
63 : INHERITED(context) {
64 fCaps.reset(new GrMockCaps(contextOptions, options));
65}
66
67sk_sp<GrTexture> GrMockGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
Robert Phillips590533f2017-07-11 14:22:35 -040068 const GrMipLevel texels[], int mipLevelCount) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040069 GrMipMapsStatus mipMapsStatus = mipLevelCount > 1 ? GrMipMapsStatus::kValid
70 : GrMipMapsStatus::kNotAllocated;
Brian Salomon8fe24272017-07-07 12:56:11 -040071 GrMockTextureInfo info;
72 info.fID = NextInternalTextureID();
Brian Salomoncfe910d2017-07-06 16:40:18 -040073 if (desc.fFlags & kRenderTarget_GrSurfaceFlag) {
Brian Salomon8fe24272017-07-07 12:56:11 -040074 return sk_sp<GrTexture>(
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040075 new GrMockTextureRenderTarget(this, budgeted, desc, mipMapsStatus, info));
Brian Salomoncfe910d2017-07-06 16:40:18 -040076 }
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040077 return sk_sp<GrTexture>(new GrMockTexture(this, budgeted, desc, mipMapsStatus, info));
Brian Salomoncfe910d2017-07-06 16:40:18 -040078}
79
80GrBuffer* GrMockGpu::onCreateBuffer(size_t sizeInBytes, GrBufferType type,
81 GrAccessPattern accessPattern, const void*) {
82 return new GrMockBuffer(this, sizeInBytes, type, accessPattern);
83}
84
85GrStencilAttachment* 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 Salomon8fe24272017-07-07 12:56:11 -040092
93GrBackendObject GrMockGpu::createTestingOnlyBackendTexture(void* pixels, int w, int h,
Greg Daniel177e6952017-10-12 12:27:11 -040094 GrPixelConfig config, bool isRT,
95 GrMipMapped) {
Brian Salomon8fe24272017-07-07 12:56:11 -040096 auto info = new GrMockTextureInfo;
97 info->fID = NextExternalTextureID();
98 fOutstandingTestingOnlyTextureIDs.add(info->fID);
99 return reinterpret_cast<GrBackendObject>(info);
100}
101
102bool GrMockGpu::isTestingOnlyBackendTexture(GrBackendObject object) const {
103 return fOutstandingTestingOnlyTextureIDs.contains(
104 reinterpret_cast<const GrMockTextureInfo*>(object)->fID);
105}
106
107void GrMockGpu::deleteTestingOnlyBackendTexture(GrBackendObject object, bool abandonTexture) {
108 auto info = reinterpret_cast<const GrMockTextureInfo*>(object);
109 fOutstandingTestingOnlyTextureIDs.remove(info->fID);
110 delete info;
111}