blob: 0d4cf6296be2cc4690c2abd801fb1629a7dc5c34 [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 Salomon0c51eea2018-03-09 17:02:09 -050027int GrMockGpu::NextInternalRenderTargetID() {
28 // We start off with large numbers to differentiate from texture IDs, even though their
29 // technically in a different space.
30 static int gID = SK_MaxS32;
31 return sk_atomic_dec(&gID);
32}
33
34int GrMockGpu::NextExternalRenderTargetID() {
35 // We use large negative ints for the "testing only external render targets" so they can easily
36 // be identified when debugging.
37 static int gID = SK_MinS32;
38 return sk_atomic_inc(&gID);
39}
40
Brian Salomon384fab42017-12-07 12:33:05 -050041sk_sp<GrGpu> GrMockGpu::Make(const GrMockOptions* mockOptions,
42 const GrContextOptions& contextOptions, GrContext* context) {
Greg Daniel02611d92017-07-25 10:05:01 -040043 static const GrMockOptions kDefaultOptions = GrMockOptions();
44 if (!mockOptions) {
45 mockOptions = &kDefaultOptions;
46 }
Brian Salomon384fab42017-12-07 12:33:05 -050047 return sk_sp<GrGpu>(new GrMockGpu(context, *mockOptions, contextOptions));
Greg Daniel02611d92017-07-25 10:05:01 -040048}
49
50
Greg Daniel500d58b2017-08-24 15:59:33 -040051GrGpuRTCommandBuffer* GrMockGpu::createCommandBuffer(
Robert Phillips19e51dc2017-08-09 09:30:51 -040052 GrRenderTarget* rt, GrSurfaceOrigin origin,
Greg Daniel500d58b2017-08-24 15:59:33 -040053 const GrGpuRTCommandBuffer::LoadAndStoreInfo&,
54 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo&) {
55 return new GrMockGpuRTCommandBuffer(this, rt, origin);
Brian Salomoncfe910d2017-07-06 16:40:18 -040056}
57
Greg Daniel500d58b2017-08-24 15:59:33 -040058GrGpuTextureCommandBuffer* GrMockGpu::createCommandBuffer(GrTexture* texture,
59 GrSurfaceOrigin origin) {
60 return new GrMockGpuTextureCommandBuffer(texture, origin);
61}
62
63
64void GrMockGpu::submitCommandBuffer(const GrMockGpuRTCommandBuffer* cmdBuffer) {
Brian Salomoncfe910d2017-07-06 16:40:18 -040065 for (int i = 0; i < cmdBuffer->numDraws(); ++i) {
66 fStats.incNumDraws();
67 }
68}
69
70GrMockGpu::GrMockGpu(GrContext* context, const GrMockOptions& options,
71 const GrContextOptions& contextOptions)
72 : INHERITED(context) {
73 fCaps.reset(new GrMockCaps(contextOptions, options));
74}
75
76sk_sp<GrTexture> GrMockGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
Brian Salomon58389b92018-03-07 13:01:25 -050077 const GrMipLevel texels[], int mipLevelCount) {
Greg Daniel0fc4d2d2017-10-12 11:23:36 -040078 GrMipMapsStatus mipMapsStatus = mipLevelCount > 1 ? GrMipMapsStatus::kValid
79 : GrMipMapsStatus::kNotAllocated;
Brian Salomon0c51eea2018-03-09 17:02:09 -050080 GrMockTextureInfo texInfo;
81 texInfo.fConfig = desc.fConfig;
82 texInfo.fID = NextInternalTextureID();
Brian Salomoncfe910d2017-07-06 16:40:18 -040083 if (desc.fFlags & kRenderTarget_GrSurfaceFlag) {
Brian Salomon0c51eea2018-03-09 17:02:09 -050084 GrMockRenderTargetInfo rtInfo;
85 rtInfo.fConfig = desc.fConfig;
86 rtInfo.fID = NextInternalRenderTargetID();
87 return sk_sp<GrTexture>(new GrMockTextureRenderTarget(this, budgeted, desc, mipMapsStatus,
88 texInfo, rtInfo));
Brian Salomoncfe910d2017-07-06 16:40:18 -040089 }
Brian Salomon0c51eea2018-03-09 17:02:09 -050090 return sk_sp<GrTexture>(new GrMockTexture(this, budgeted, desc, mipMapsStatus, texInfo));
Brian Salomoncfe910d2017-07-06 16:40:18 -040091}
92
Greg Daniel4684f822018-03-08 15:27:36 -050093sk_sp<GrTexture> GrMockGpu::onWrapBackendTexture(const GrBackendTexture& tex,
94 GrWrapOwnership ownership) {
95 GrSurfaceDesc desc;
96 desc.fWidth = tex.width();
97 desc.fHeight = tex.height();
Greg Daniel52e16d92018-04-10 09:34:07 -040098
99 GrMockTextureInfo info;
100 SkAssertResult(tex.getMockTextureInfo(&info));
Greg Daniel4684f822018-03-08 15:27:36 -0500101 desc.fConfig = info.fConfig;
102
103 GrMipMapsStatus mipMapsStatus = tex.hasMipMaps() ? GrMipMapsStatus::kValid
104 : GrMipMapsStatus::kNotAllocated;
105
106 return sk_sp<GrTexture>(new GrMockTexture(this, GrMockTexture::kWrapped, desc, mipMapsStatus,
107 info));
108}
109
Brian Salomon0c51eea2018-03-09 17:02:09 -0500110sk_sp<GrTexture> GrMockGpu::onWrapRenderableBackendTexture(const GrBackendTexture& tex,
111 int sampleCnt,
112 GrWrapOwnership ownership) {
113 GrSurfaceDesc desc;
114 desc.fFlags = kRenderTarget_GrSurfaceFlag;
115 desc.fWidth = tex.width();
116 desc.fHeight = tex.height();
Greg Daniel52e16d92018-04-10 09:34:07 -0400117
118 GrMockTextureInfo texInfo;
119 SkAssertResult(tex.getMockTextureInfo(&texInfo));
Brian Salomon0c51eea2018-03-09 17:02:09 -0500120 desc.fConfig = texInfo.fConfig;
121
122 GrMipMapsStatus mipMapsStatus =
123 tex.hasMipMaps() ? GrMipMapsStatus::kValid : GrMipMapsStatus::kNotAllocated;
124
125 GrMockRenderTargetInfo rtInfo;
126 rtInfo.fConfig = texInfo.fConfig;
127 // The client gave us the texture ID but we supply the render target ID.
128 rtInfo.fID = NextInternalRenderTargetID();
129
130 return sk_sp<GrTexture>(
131 new GrMockTextureRenderTarget(this, desc, mipMapsStatus, texInfo, rtInfo));
132}
133
134sk_sp<GrRenderTarget> GrMockGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& rt) {
135 GrSurfaceDesc desc;
136 desc.fFlags = kRenderTarget_GrSurfaceFlag;
137 desc.fWidth = rt.width();
138 desc.fHeight = rt.height();
Greg Daniel323fbcf2018-04-10 13:46:30 -0400139
140 GrMockRenderTargetInfo info;
141 SkAssertResult(rt.getMockRenderTargetInfo(&info));
Brian Salomon0c51eea2018-03-09 17:02:09 -0500142 desc.fConfig = info.fConfig;
143
144 return sk_sp<GrRenderTarget>(
145 new GrMockRenderTarget(this, GrMockRenderTarget::kWrapped, desc, info));
146}
147
148sk_sp<GrRenderTarget> GrMockGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
149 int sampleCnt) {
150 GrSurfaceDesc desc;
151 desc.fFlags = kRenderTarget_GrSurfaceFlag;
152 desc.fWidth = tex.width();
153 desc.fHeight = tex.height();
Greg Daniel52e16d92018-04-10 09:34:07 -0400154
155 GrMockTextureInfo texInfo;
156 SkAssertResult(tex.getMockTextureInfo(&texInfo));
Brian Salomon0c51eea2018-03-09 17:02:09 -0500157 desc.fConfig = texInfo.fConfig;
158 desc.fSampleCnt = sampleCnt;
159
160 GrMockRenderTargetInfo rtInfo;
161 rtInfo.fConfig = texInfo.fConfig;
162 // The client gave us the texture ID but we supply the render target ID.
163 rtInfo.fID = NextInternalRenderTargetID();
164
165 return sk_sp<GrRenderTarget>(
166 new GrMockRenderTarget(this, GrMockRenderTarget::kWrapped, desc, rtInfo));
167}
168
Brian Salomoncfe910d2017-07-06 16:40:18 -0400169GrBuffer* GrMockGpu::onCreateBuffer(size_t sizeInBytes, GrBufferType type,
170 GrAccessPattern accessPattern, const void*) {
171 return new GrMockBuffer(this, sizeInBytes, type, accessPattern);
172}
173
174GrStencilAttachment* GrMockGpu::createStencilAttachmentForRenderTarget(const GrRenderTarget* rt,
175 int width,
176 int height) {
177 static constexpr int kBits = 8;
178 fStats.incStencilAttachmentCreates();
179 return new GrMockStencilAttachment(this, width, height, kBits, rt->numColorSamples());
180}
Brian Salomon8fe24272017-07-07 12:56:11 -0400181
Brian Salomonf865b052018-03-09 09:01:53 -0500182#if GR_TEST_UTILS
Brian Salomon52e943a2018-03-13 09:32:39 -0400183GrBackendTexture GrMockGpu::createTestingOnlyBackendTexture(const void* pixels, int w, int h,
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500184 GrPixelConfig config, bool isRT,
Brian Salomon0c51eea2018-03-09 17:02:09 -0500185 GrMipMapped mipMapped) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500186 GrMockTextureInfo info;
Robert Phillipsfc711a22018-02-13 17:03:00 -0500187 info.fConfig = config;
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500188 info.fID = NextExternalTextureID();
189 fOutstandingTestingOnlyTextureIDs.add(info.fID);
Brian Salomon0c51eea2018-03-09 17:02:09 -0500190 return GrBackendTexture(w, h, mipMapped, info);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500191}
192
193bool GrMockGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
194 SkASSERT(kMock_GrBackend == tex.backend());
195
Greg Daniel52e16d92018-04-10 09:34:07 -0400196 GrMockTextureInfo info;
197 if (!tex.getMockTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500198 return false;
199 }
200
Greg Daniel52e16d92018-04-10 09:34:07 -0400201 return fOutstandingTestingOnlyTextureIDs.contains(info.fID);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500202}
203
Brian Salomon26102cb2018-03-09 09:33:19 -0500204void GrMockGpu::deleteTestingOnlyBackendTexture(const GrBackendTexture& tex) {
205 SkASSERT(kMock_GrBackend == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500206
Greg Daniel52e16d92018-04-10 09:34:07 -0400207 GrMockTextureInfo info;
208 if (tex.getMockTextureInfo(&info)) {
209 fOutstandingTestingOnlyTextureIDs.remove(info.fID);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500210 }
211}
Brian Salomonf865b052018-03-09 09:01:53 -0500212
Brian Salomon0c51eea2018-03-09 17:02:09 -0500213GrBackendRenderTarget GrMockGpu::createTestingOnlyBackendRenderTarget(int w, int h,
214 GrColorType colorType,
215 GrSRGBEncoded srgbEncoded) {
216 auto config = GrColorTypeToPixelConfig(colorType, srgbEncoded);
217 if (kUnknown_GrPixelConfig == config) {
218 return {};
219 }
220 GrMockRenderTargetInfo info = {config, NextExternalRenderTargetID()};
221 static constexpr int kSampleCnt = 1;
222 static constexpr int kStencilBits = 8;
223 return {w, h, kSampleCnt, kStencilBits, info};
Brian Salomonf865b052018-03-09 09:01:53 -0500224}
225
226void GrMockGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget&) {}
227#endif