blob: ae4eeed2802d60345f546737e2834af428740f1c [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/mock/GrMockBuffer.h"
9#include "src/gpu/mock/GrMockCaps.h"
10#include "src/gpu/mock/GrMockGpu.h"
11#include "src/gpu/mock/GrMockGpuCommandBuffer.h"
12#include "src/gpu/mock/GrMockStencilAttachment.h"
13#include "src/gpu/mock/GrMockTexture.h"
Mike Kleinde2244c2018-12-04 11:16:08 -050014#include <atomic>
Brian Salomoncfe910d2017-07-06 16:40:18 -040015
Brian Salomon8fe24272017-07-07 12:56:11 -040016int GrMockGpu::NextInternalTextureID() {
Mike Kleinde2244c2018-12-04 11:16:08 -050017 static std::atomic<int> nextID{1};
Chris Dalton351e80c2019-01-06 22:51:00 -070018 int id;
19 do {
20 id = nextID.fetch_add(1);
21 } while (0 == id); // Reserve 0 for an invalid ID.
22 return id;
Brian Salomon8fe24272017-07-07 12:56:11 -040023}
24
25int GrMockGpu::NextExternalTextureID() {
26 // We use negative ints for the "testing only external textures" so they can easily be
27 // identified when debugging.
Mike Kleinde2244c2018-12-04 11:16:08 -050028 static std::atomic<int> nextID{-1};
29 return nextID--;
Brian Salomon8fe24272017-07-07 12:56:11 -040030}
31
Brian Salomon0c51eea2018-03-09 17:02:09 -050032int GrMockGpu::NextInternalRenderTargetID() {
Mike Kleinde2244c2018-12-04 11:16:08 -050033 // We start off with large numbers to differentiate from texture IDs, even though they're
Brian Salomon0c51eea2018-03-09 17:02:09 -050034 // technically in a different space.
Mike Kleinde2244c2018-12-04 11:16:08 -050035 static std::atomic<int> nextID{SK_MaxS32};
36 return nextID--;
Brian Salomon0c51eea2018-03-09 17:02:09 -050037}
38
39int GrMockGpu::NextExternalRenderTargetID() {
40 // We use large negative ints for the "testing only external render targets" so they can easily
41 // be identified when debugging.
Mike Kleinde2244c2018-12-04 11:16:08 -050042 static std::atomic<int> nextID{SK_MinS32};
43 return nextID++;
Brian Salomon0c51eea2018-03-09 17:02:09 -050044}
45
Brian Salomon384fab42017-12-07 12:33:05 -050046sk_sp<GrGpu> GrMockGpu::Make(const GrMockOptions* mockOptions,
47 const GrContextOptions& contextOptions, GrContext* context) {
Greg Daniel02611d92017-07-25 10:05:01 -040048 static const GrMockOptions kDefaultOptions = GrMockOptions();
49 if (!mockOptions) {
50 mockOptions = &kDefaultOptions;
51 }
Brian Salomon384fab42017-12-07 12:33:05 -050052 return sk_sp<GrGpu>(new GrMockGpu(context, *mockOptions, contextOptions));
Greg Daniel02611d92017-07-25 10:05:01 -040053}
54
Robert Phillips5b5d84c2018-08-09 15:12:18 -040055GrGpuRTCommandBuffer* GrMockGpu::getCommandBuffer(
Ethan Nicholas56d19a52018-10-15 11:26:20 -040056 GrRenderTarget* rt, GrSurfaceOrigin origin, const SkRect& bounds,
Robert Phillips5b5d84c2018-08-09 15:12:18 -040057 const GrGpuRTCommandBuffer::LoadAndStoreInfo&,
58 const GrGpuRTCommandBuffer::StencilLoadAndStoreInfo&) {
Greg Daniel500d58b2017-08-24 15:59:33 -040059 return new GrMockGpuRTCommandBuffer(this, rt, origin);
Brian Salomoncfe910d2017-07-06 16:40:18 -040060}
61
Robert Phillips5b5d84c2018-08-09 15:12:18 -040062GrGpuTextureCommandBuffer* GrMockGpu::getCommandBuffer(GrTexture* texture, GrSurfaceOrigin origin) {
Greg Daniel500d58b2017-08-24 15:59:33 -040063 return new GrMockGpuTextureCommandBuffer(texture, origin);
64}
65
Robert Phillips5b5d84c2018-08-09 15:12:18 -040066void GrMockGpu::submit(GrGpuCommandBuffer* buffer) {
67 if (buffer->asRTCommandBuffer()) {
68 this->submitCommandBuffer(
69 static_cast<GrMockGpuRTCommandBuffer*>(buffer->asRTCommandBuffer()));
70 }
71
72 delete buffer;
73}
Greg Daniel500d58b2017-08-24 15:59:33 -040074
75void GrMockGpu::submitCommandBuffer(const GrMockGpuRTCommandBuffer* cmdBuffer) {
Brian Salomoncfe910d2017-07-06 16:40:18 -040076 for (int i = 0; i < cmdBuffer->numDraws(); ++i) {
77 fStats.incNumDraws();
78 }
79}
80
81GrMockGpu::GrMockGpu(GrContext* context, const GrMockOptions& options,
82 const GrContextOptions& contextOptions)
Chris Dalton91ab1552018-04-18 13:24:25 -060083 : INHERITED(context)
84 , fMockOptions(options) {
Brian Salomoncfe910d2017-07-06 16:40:18 -040085 fCaps.reset(new GrMockCaps(contextOptions, options));
86}
87
88sk_sp<GrTexture> GrMockGpu::onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
Brian Salomon58389b92018-03-07 13:01:25 -050089 const GrMipLevel texels[], int mipLevelCount) {
Chris Dalton91ab1552018-04-18 13:24:25 -060090 if (fMockOptions.fFailTextureAllocations) {
91 return nullptr;
92 }
93
Robert Phillipsa5e78be2019-07-09 12:34:38 -040094 GrSRGBEncoded srgbEncoding;
95 GrColorType ct = GrPixelConfigToColorTypeAndEncoding(desc.fConfig, &srgbEncoding);
96 if (GrColorType::kUnknown == ct) {
97 return nullptr;
98 }
99
Greg Daniel0fc4d2d2017-10-12 11:23:36 -0400100 GrMipMapsStatus mipMapsStatus = mipLevelCount > 1 ? GrMipMapsStatus::kValid
101 : GrMipMapsStatus::kNotAllocated;
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400102 GrMockTextureInfo texInfo(ct, srgbEncoding, NextInternalTextureID());
Brian Salomoncfe910d2017-07-06 16:40:18 -0400103 if (desc.fFlags & kRenderTarget_GrSurfaceFlag) {
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400104 GrMockRenderTargetInfo rtInfo(ct, srgbEncoding, NextInternalRenderTargetID());
Brian Salomon0c51eea2018-03-09 17:02:09 -0500105 return sk_sp<GrTexture>(new GrMockTextureRenderTarget(this, budgeted, desc, mipMapsStatus,
106 texInfo, rtInfo));
Brian Salomoncfe910d2017-07-06 16:40:18 -0400107 }
Brian Salomon0c51eea2018-03-09 17:02:09 -0500108 return sk_sp<GrTexture>(new GrMockTexture(this, budgeted, desc, mipMapsStatus, texInfo));
Brian Salomoncfe910d2017-07-06 16:40:18 -0400109}
110
Brian Salomonbb8dde82019-06-27 10:52:13 -0400111sk_sp<GrTexture> GrMockGpu::onCreateCompressedTexture(int width, int height,
112 SkImage::CompressionType compressionType,
113 SkBudgeted budgeted, const void* data) {
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400114 return nullptr;
Brian Salomonbb8dde82019-06-27 10:52:13 -0400115}
116
Greg Daniel4684f822018-03-08 15:27:36 -0500117sk_sp<GrTexture> GrMockGpu::onWrapBackendTexture(const GrBackendTexture& tex,
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500118 GrWrapOwnership ownership,
119 GrWrapCacheable wrapType, GrIOType ioType) {
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400120 GrMockTextureInfo info;
121 SkAssertResult(tex.getMockTextureInfo(&info));
122
Greg Daniel4684f822018-03-08 15:27:36 -0500123 GrSurfaceDesc desc;
124 desc.fWidth = tex.width();
125 desc.fHeight = tex.height();
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400126 desc.fConfig = info.pixelConfig();
Greg Daniel4684f822018-03-08 15:27:36 -0500127
128 GrMipMapsStatus mipMapsStatus = tex.hasMipMaps() ? GrMipMapsStatus::kValid
129 : GrMipMapsStatus::kNotAllocated;
130
Brian Salomonfa2ebea2019-01-24 15:58:58 -0500131 return sk_sp<GrTexture>(new GrMockTexture(this, desc, mipMapsStatus, info, wrapType, ioType));
Greg Daniel4684f822018-03-08 15:27:36 -0500132}
133
Brian Salomon0c51eea2018-03-09 17:02:09 -0500134sk_sp<GrTexture> GrMockGpu::onWrapRenderableBackendTexture(const GrBackendTexture& tex,
135 int sampleCnt,
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500136 GrWrapOwnership ownership,
137 GrWrapCacheable cacheable) {
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400138 GrMockTextureInfo texInfo;
139 SkAssertResult(tex.getMockTextureInfo(&texInfo));
140
Brian Salomon0c51eea2018-03-09 17:02:09 -0500141 GrSurfaceDesc desc;
142 desc.fFlags = kRenderTarget_GrSurfaceFlag;
143 desc.fWidth = tex.width();
144 desc.fHeight = tex.height();
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400145 desc.fConfig = texInfo.pixelConfig();
Brian Salomon0c51eea2018-03-09 17:02:09 -0500146
147 GrMipMapsStatus mipMapsStatus =
148 tex.hasMipMaps() ? GrMipMapsStatus::kValid : GrMipMapsStatus::kNotAllocated;
149
Brian Salomon0c51eea2018-03-09 17:02:09 -0500150 // The client gave us the texture ID but we supply the render target ID.
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400151 GrMockRenderTargetInfo rtInfo(texInfo.fColorType, texInfo.fSRGBEncoded,
152 NextInternalRenderTargetID());
Brian Salomon0c51eea2018-03-09 17:02:09 -0500153
154 return sk_sp<GrTexture>(
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500155 new GrMockTextureRenderTarget(this, desc, mipMapsStatus, texInfo, rtInfo, cacheable));
Brian Salomon0c51eea2018-03-09 17:02:09 -0500156}
157
158sk_sp<GrRenderTarget> GrMockGpu::onWrapBackendRenderTarget(const GrBackendRenderTarget& rt) {
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400159 GrMockRenderTargetInfo info;
160 SkAssertResult(rt.getMockRenderTargetInfo(&info));
161
Brian Salomon0c51eea2018-03-09 17:02:09 -0500162 GrSurfaceDesc desc;
163 desc.fFlags = kRenderTarget_GrSurfaceFlag;
164 desc.fWidth = rt.width();
165 desc.fHeight = rt.height();
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400166 desc.fConfig = info.pixelConfig();
Brian Salomon0c51eea2018-03-09 17:02:09 -0500167
168 return sk_sp<GrRenderTarget>(
169 new GrMockRenderTarget(this, GrMockRenderTarget::kWrapped, desc, info));
170}
171
172sk_sp<GrRenderTarget> GrMockGpu::onWrapBackendTextureAsRenderTarget(const GrBackendTexture& tex,
173 int sampleCnt) {
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400174 GrMockTextureInfo texInfo;
175 SkAssertResult(tex.getMockTextureInfo(&texInfo));
176
Brian Salomon0c51eea2018-03-09 17:02:09 -0500177 GrSurfaceDesc desc;
178 desc.fFlags = kRenderTarget_GrSurfaceFlag;
179 desc.fWidth = tex.width();
180 desc.fHeight = tex.height();
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400181 desc.fConfig = texInfo.pixelConfig();
Brian Salomon0c51eea2018-03-09 17:02:09 -0500182 desc.fSampleCnt = sampleCnt;
183
Brian Salomon0c51eea2018-03-09 17:02:09 -0500184 // The client gave us the texture ID but we supply the render target ID.
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400185 GrMockRenderTargetInfo rtInfo(texInfo.fColorType, texInfo.fSRGBEncoded,
186 NextInternalRenderTargetID());
Brian Salomon0c51eea2018-03-09 17:02:09 -0500187
188 return sk_sp<GrRenderTarget>(
189 new GrMockRenderTarget(this, GrMockRenderTarget::kWrapped, desc, rtInfo));
190}
191
Brian Salomondbf70722019-02-07 11:31:24 -0500192sk_sp<GrGpuBuffer> GrMockGpu::onCreateBuffer(size_t sizeInBytes, GrGpuBufferType type,
193 GrAccessPattern accessPattern, const void*) {
194 return sk_sp<GrGpuBuffer>(new GrMockBuffer(this, sizeInBytes, type, accessPattern));
Brian Salomoncfe910d2017-07-06 16:40:18 -0400195}
196
Chris Daltoneffee202019-07-01 22:28:03 -0600197GrStencilAttachment* GrMockGpu::createStencilAttachmentForRenderTarget(
198 const GrRenderTarget* rt, int width, int height, int numStencilSamples) {
199 SkASSERT(numStencilSamples == rt->numSamples());
Brian Salomoncfe910d2017-07-06 16:40:18 -0400200 static constexpr int kBits = 8;
201 fStats.incStencilAttachmentCreates();
Chris Dalton6ce447a2019-06-23 18:07:38 -0600202 return new GrMockStencilAttachment(this, width, height, kBits, rt->numSamples());
Brian Salomoncfe910d2017-07-06 16:40:18 -0400203}
Brian Salomon8fe24272017-07-07 12:56:11 -0400204
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400205GrBackendTexture GrMockGpu::createBackendTexture(int w, int h,
206 const GrBackendFormat& format,
207 GrMipMapped mipMapped,
208 GrRenderable /* renderable */,
209 const void* /* pixels */,
Robert Phillips459b2952019-05-23 09:38:27 -0400210 size_t /* rowBytes */,
Emircan Uysaler23ca4e72019-06-24 10:53:09 -0400211 const SkColor4f* /* color */,
212 GrProtected /* isProtected */) {
Robert Phillips646f6372018-09-25 09:31:10 -0400213
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400214 if (!format.getMockColorType() || !format.getMockSRGBEncoded()) {
215 return GrBackendTexture(); // invalid;
216 }
217
218 GrPixelConfig config = GrColorTypeToPixelConfig(*format.getMockColorType(),
219 *format.getMockSRGBEncoded());
220
221 if (!this->caps()->isConfigTexturable(config)) {
Robert Phillips9dbcdcc2019-05-13 10:40:06 -0400222 return GrBackendTexture(); // invalid
223 }
224
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400225 GrMockTextureInfo info(*format.getMockColorType(), *format.getMockSRGBEncoded(),
226 NextExternalTextureID());
Robert Phillips646f6372018-09-25 09:31:10 -0400227
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500228 fOutstandingTestingOnlyTextureIDs.add(info.fID);
Brian Salomon0c51eea2018-03-09 17:02:09 -0500229 return GrBackendTexture(w, h, mipMapped, info);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500230}
231
Robert Phillipsf0313ee2019-05-21 13:51:11 -0400232void GrMockGpu::deleteBackendTexture(const GrBackendTexture& tex) {
Robert Phillipsf0ced622019-05-16 09:06:25 -0400233 SkASSERT(GrBackendApi::kMock == tex.backend());
234
235 GrMockTextureInfo info;
236 if (tex.getMockTextureInfo(&info)) {
237 fOutstandingTestingOnlyTextureIDs.remove(info.fID);
238 }
239}
240
241#if GR_TEST_UTILS
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500242bool GrMockGpu::isTestingOnlyBackendTexture(const GrBackendTexture& tex) const {
Greg Danielbdf12ad2018-10-12 09:31:11 -0400243 SkASSERT(GrBackendApi::kMock == tex.backend());
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500244
Greg Daniel52e16d92018-04-10 09:34:07 -0400245 GrMockTextureInfo info;
246 if (!tex.getMockTextureInfo(&info)) {
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500247 return false;
248 }
249
Greg Daniel52e16d92018-04-10 09:34:07 -0400250 return fOutstandingTestingOnlyTextureIDs.contains(info.fID);
Robert Phillipsd21b2a52017-12-12 13:01:25 -0500251}
252
Brian Salomon0c51eea2018-03-09 17:02:09 -0500253GrBackendRenderTarget GrMockGpu::createTestingOnlyBackendRenderTarget(int w, int h,
Brian Osman2d010b62018-08-09 10:55:09 -0400254 GrColorType colorType) {
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400255 GrMockRenderTargetInfo info(colorType, GrSRGBEncoded::kNo, NextExternalRenderTargetID());
Brian Salomon0c51eea2018-03-09 17:02:09 -0500256 static constexpr int kSampleCnt = 1;
257 static constexpr int kStencilBits = 8;
Robert Phillipsa5e78be2019-07-09 12:34:38 -0400258 return GrBackendRenderTarget(w, h, kSampleCnt, kStencilBits, info);
Brian Salomonf865b052018-03-09 09:01:53 -0500259}
260
261void GrMockGpu::deleteTestingOnlyBackendRenderTarget(const GrBackendRenderTarget&) {}
262#endif