blob: 8a187f69323f07be9cd2d6f2a723a270c80754c0 [file] [log] [blame]
commit-bot@chromium.org78a10782013-08-21 19:27:48 +00001/*
2 * Copyright 2013 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 "GrTest.h"
Brian Salomon17726632017-05-12 14:09:46 -04009#include <algorithm>
Greg Daniel7ef28f32017-04-20 16:41:55 +000010#include "GrBackendSurface.h"
bsalomon682c2692015-05-22 14:01:46 -070011#include "GrContextOptions.h"
Chris Daltonfe199b72017-05-05 11:26:15 -040012#include "GrContextPriv.h"
Brian Salomon2ee084e2016-12-16 18:59:19 -050013#include "GrDrawOpAtlas.h"
robertphillips77a2e522015-10-17 07:43:27 -070014#include "GrDrawingManager.h"
bsalomon3582d3e2015-02-13 14:20:05 -080015#include "GrGpuResourceCacheAccess.h"
robertphillips5fa7f302016-07-21 09:21:04 -070016#include "GrPipelineBuilder.h"
Brian Salomon17726632017-05-12 14:09:46 -040017#include "GrRenderTargetContext.h"
Brian Salomon2ee084e2016-12-16 18:59:19 -050018#include "GrRenderTargetContextPriv.h"
csmartdaltonf9635992016-08-10 11:09:07 -070019#include "GrRenderTargetProxy.h"
bsalomon0ea80f42015-02-11 10:49:59 -080020#include "GrResourceCache.h"
Greg Daniel6be35232017-03-01 17:01:09 -050021#include "GrSemaphore.h"
Brian Salomon17726632017-05-12 14:09:46 -040022#include "GrSurfaceContextPriv.h"
Brian Osman3b655982017-03-07 16:58:08 -050023#include "SkGr.h"
Robert Phillips22f4a1f2016-12-20 08:57:26 -050024#include "SkImage_Gpu.h"
halcanary4dbbd042016-06-07 17:21:10 -070025#include "SkMathPriv.h"
mtkleinb9eb4ac2015-02-02 18:26:03 -080026#include "SkString.h"
Brian Salomon17726632017-05-12 14:09:46 -040027#include "ops/GrMeshDrawOp.h"
Brian Salomonf856fd12016-12-16 14:24:34 -050028#include "text/GrAtlasGlyphCache.h"
joshualitte8042922015-12-11 06:11:21 -080029#include "text/GrTextBlobCache.h"
30
joshualitt7f9c9eb2015-08-21 11:08:00 -070031namespace GrTest {
32void SetupAlwaysEvictAtlas(GrContext* context) {
33 // These sizes were selected because they allow each atlas to hold a single plot and will thus
34 // stress the atlas
Brian Salomon2ee084e2016-12-16 18:59:19 -050035 int dim = GrDrawOpAtlas::kGlyphMaxDim;
36 GrDrawOpAtlasConfig configs[3];
joshualitt7f9c9eb2015-08-21 11:08:00 -070037 configs[kA8_GrMaskFormat].fWidth = dim;
38 configs[kA8_GrMaskFormat].fHeight = dim;
jvanverth7023a002016-02-22 11:25:32 -080039 configs[kA8_GrMaskFormat].fLog2Width = SkNextLog2(dim);
40 configs[kA8_GrMaskFormat].fLog2Height = SkNextLog2(dim);
joshualitt7f9c9eb2015-08-21 11:08:00 -070041 configs[kA8_GrMaskFormat].fPlotWidth = dim;
42 configs[kA8_GrMaskFormat].fPlotHeight = dim;
43
44 configs[kA565_GrMaskFormat].fWidth = dim;
45 configs[kA565_GrMaskFormat].fHeight = dim;
jvanverth7023a002016-02-22 11:25:32 -080046 configs[kA565_GrMaskFormat].fLog2Width = SkNextLog2(dim);
47 configs[kA565_GrMaskFormat].fLog2Height = SkNextLog2(dim);
joshualitt7f9c9eb2015-08-21 11:08:00 -070048 configs[kA565_GrMaskFormat].fPlotWidth = dim;
49 configs[kA565_GrMaskFormat].fPlotHeight = dim;
50
51 configs[kARGB_GrMaskFormat].fWidth = dim;
52 configs[kARGB_GrMaskFormat].fHeight = dim;
jvanverth7023a002016-02-22 11:25:32 -080053 configs[kARGB_GrMaskFormat].fLog2Width = SkNextLog2(dim);
54 configs[kARGB_GrMaskFormat].fLog2Height = SkNextLog2(dim);
joshualitt7f9c9eb2015-08-21 11:08:00 -070055 configs[kARGB_GrMaskFormat].fPlotWidth = dim;
56 configs[kARGB_GrMaskFormat].fPlotHeight = dim;
57
58 context->setTextContextAtlasSizes_ForTesting(configs);
59}
Greg Daniel7ef28f32017-04-20 16:41:55 +000060
61GrBackendTexture CreateBackendTexture(GrBackend backend, int width, int height,
62 GrPixelConfig config, GrBackendObject handle) {
63 if (kOpenGL_GrBackend == backend) {
64 GrGLTextureInfo* glInfo = (GrGLTextureInfo*)(handle);
Greg Daniel207282e2017-04-26 13:29:21 -040065 return GrBackendTexture(width, height, config, *glInfo);
Greg Daniel7ef28f32017-04-20 16:41:55 +000066 } else {
67 SkASSERT(kVulkan_GrBackend == backend);
68 GrVkImageInfo* vkInfo = (GrVkImageInfo*)(handle);
Greg Daniel207282e2017-04-26 13:29:21 -040069 return GrBackendTexture(width, height, *vkInfo);
Greg Daniel7ef28f32017-04-20 16:41:55 +000070 }
71}
joshualitt7f9c9eb2015-08-21 11:08:00 -070072};
73
Robert Phillipseaa86252016-11-08 13:49:39 +000074bool GrSurfaceProxy::isWrapped_ForTesting() const {
75 return SkToBool(fTarget);
76}
77
78bool GrRenderTargetContext::isWrapped_ForTesting() const {
79 return fRenderTargetProxy->isWrapped_ForTesting();
80}
81
joshualitt17d833b2015-08-03 10:17:44 -070082void GrContext::setTextBlobCacheLimit_ForTesting(size_t bytes) {
83 fTextBlobCache->setBudget(bytes);
84}
85
Brian Salomon2ee084e2016-12-16 18:59:19 -050086void GrContext::setTextContextAtlasSizes_ForTesting(const GrDrawOpAtlasConfig* configs) {
Brian Salomonf856fd12016-12-16 14:24:34 -050087 fAtlasGlyphCache->setAtlasSizes_ForTesting(configs);
joshualittda04e0e2015-08-19 08:16:43 -070088}
89
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000090///////////////////////////////////////////////////////////////////////////////
91
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +000092void GrContext::purgeAllUnlockedResources() {
bsalomon0ea80f42015-02-11 10:49:59 -080093 fResourceCache->purgeAllUnlocked();
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +000094}
bsalomon33435572014-11-05 14:47:41 -080095
joshualitte45c81c2015-12-02 09:05:37 -080096void GrContext::resetGpuStats() const {
97#if GR_GPU_STATS
98 fGpu->stats()->reset();
99#endif
100}
101
mtkleinb9eb4ac2015-02-02 18:26:03 -0800102void GrContext::dumpCacheStats(SkString* out) const {
103#if GR_CACHE_STATS
bsalomon0ea80f42015-02-11 10:49:59 -0800104 fResourceCache->dumpStats(out);
mtkleinb9eb4ac2015-02-02 18:26:03 -0800105#endif
106}
107
joshualittdc5685a2015-12-02 14:08:25 -0800108void GrContext::dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys,
109 SkTArray<double>* values) const {
110#if GR_CACHE_STATS
111 fResourceCache->dumpStatsKeyValuePairs(keys, values);
112#endif
113}
114
mtkleinb9eb4ac2015-02-02 18:26:03 -0800115void GrContext::printCacheStats() const {
116 SkString out;
117 this->dumpCacheStats(&out);
kkinnunen297aaf92015-02-19 06:32:12 -0800118 SkDebugf("%s", out.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800119}
120
121void GrContext::dumpGpuStats(SkString* out) const {
122#if GR_GPU_STATS
123 return fGpu->stats()->dump(out);
124#endif
125}
126
joshualitte45c81c2015-12-02 09:05:37 -0800127void GrContext::dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys,
128 SkTArray<double>* values) const {
129#if GR_GPU_STATS
130 return fGpu->stats()->dumpKeyValuePairs(keys, values);
131#endif
132}
133
mtkleinb9eb4ac2015-02-02 18:26:03 -0800134void GrContext::printGpuStats() const {
135 SkString out;
136 this->dumpGpuStats(&out);
kkinnunen297aaf92015-02-19 06:32:12 -0800137 SkDebugf("%s", out.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800138}
139
Robert Phillipsc37e6142017-01-20 10:03:25 -0500140sk_sp<SkImage> GrContext::getFontAtlasImage_ForTesting(GrMaskFormat format) {
Brian Salomonf856fd12016-12-16 14:24:34 -0500141 GrAtlasGlyphCache* cache = this->getAtlasGlyphCache();
jvanverth629162d2015-11-08 08:07:24 -0800142
Robert Phillips32f28182017-02-28 16:20:03 -0500143 sk_sp<GrTextureProxy> proxy = cache->getProxy(format);
144 if (!proxy) {
145 return nullptr;
146 }
147
Robert Phillipsb726d582017-03-09 16:36:32 -0500148 SkASSERT(proxy->priv().isExact());
149 sk_sp<SkImage> image(new SkImage_Gpu(this, kNeedNewImageUniqueID, kPremul_SkAlphaType,
150 std::move(proxy), nullptr, SkBudgeted::kNo));
Robert Phillips22f4a1f2016-12-20 08:57:26 -0500151 return image;
jvanverth0671b962015-12-08 18:53:44 -0800152}
jvanverth629162d2015-11-08 08:07:24 -0800153
mtkleinb9eb4ac2015-02-02 18:26:03 -0800154#if GR_GPU_STATS
155void GrGpu::Stats::dump(SkString* out) {
156 out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);
157 out->appendf("Shader Compilations: %d\n", fShaderCompilations);
bsalomonb12ea412015-02-02 21:19:50 -0800158 out->appendf("Textures Created: %d\n", fTextureCreates);
159 out->appendf("Texture Uploads: %d\n", fTextureUploads);
jvanverth17aa0472016-01-05 10:41:27 -0800160 out->appendf("Transfers to Texture: %d\n", fTransfersToTexture);
egdaniel8dc7c3a2015-04-16 11:22:42 -0700161 out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates);
joshualitt87a5c9f2015-09-08 13:42:05 -0700162 out->appendf("Number of draws: %d\n", fNumDraws);
mtkleinb9eb4ac2015-02-02 18:26:03 -0800163}
joshualitte45c81c2015-12-02 09:05:37 -0800164
165void GrGpu::Stats::dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) {
166 keys->push_back(SkString("render_target_binds")); values->push_back(fRenderTargetBinds);
167 keys->push_back(SkString("shader_compilations")); values->push_back(fShaderCompilations);
joshualitte45c81c2015-12-02 09:05:37 -0800168 keys->push_back(SkString("texture_uploads")); values->push_back(fTextureUploads);
joshualitte45c81c2015-12-02 09:05:37 -0800169 keys->push_back(SkString("number_of_draws")); values->push_back(fNumDraws);
bsalomon1d417a82016-03-23 11:50:26 -0700170 keys->push_back(SkString("number_of_failed_draws")); values->push_back(fNumFailedDraws);
joshualitte45c81c2015-12-02 09:05:37 -0800171}
172
mtkleinb9eb4ac2015-02-02 18:26:03 -0800173#endif
174
175#if GR_CACHE_STATS
robertphillips60029a52015-11-09 13:51:06 -0800176void GrResourceCache::getStats(Stats* stats) const {
177 stats->reset();
178
179 stats->fTotal = this->getResourceCount();
180 stats->fNumNonPurgeable = fNonpurgeableResources.count();
181 stats->fNumPurgeable = fPurgeableQueue.count();
182
183 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
184 stats->update(fNonpurgeableResources[i]);
185 }
186 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
187 stats->update(fPurgeableQueue.at(i));
188 }
189}
190
bsalomon0ea80f42015-02-11 10:49:59 -0800191void GrResourceCache::dumpStats(SkString* out) const {
mtkleinb9eb4ac2015-02-02 18:26:03 -0800192 this->validate();
193
bsalomonf320e042015-02-17 15:09:34 -0800194 Stats stats;
195
robertphillips60029a52015-11-09 13:51:06 -0800196 this->getStats(&stats);
mtkleinb9eb4ac2015-02-02 18:26:03 -0800197
198 float countUtilization = (100.f * fBudgetedCount) / fMaxCount;
199 float byteUtilization = (100.f * fBudgetedBytes) / fMaxBytes;
200
201 out->appendf("Budget: %d items %d bytes\n", fMaxCount, (int)fMaxBytes);
202 out->appendf("\t\tEntry Count: current %d"
kkinnunen2e6055b2016-04-22 01:48:29 -0700203 " (%d budgeted, %d wrapped, %d locked, %d scratch %.2g%% full), high %d\n",
204 stats.fTotal, fBudgetedCount, stats.fWrapped, stats.fNumNonPurgeable,
205 stats.fScratch, countUtilization, fHighWaterCount);
mtkleinb9eb4ac2015-02-02 18:26:03 -0800206 out->appendf("\t\tEntry Bytes: current %d (budgeted %d, %.2g%% full, %d unbudgeted) high %d\n",
bsalomonf320e042015-02-17 15:09:34 -0800207 SkToInt(fBytes), SkToInt(fBudgetedBytes), byteUtilization,
208 SkToInt(stats.fUnbudgetedSize), SkToInt(fHighWaterBytes));
mtkleinb9eb4ac2015-02-02 18:26:03 -0800209}
210
joshualittdc5685a2015-12-02 14:08:25 -0800211void GrResourceCache::dumpStatsKeyValuePairs(SkTArray<SkString>* keys,
212 SkTArray<double>* values) const {
213 this->validate();
214
215 Stats stats;
216 this->getStats(&stats);
217
joshualittdc5685a2015-12-02 14:08:25 -0800218 keys->push_back(SkString("gpu_cache_purgable_entries")); values->push_back(stats.fNumPurgeable);
joshualittdc5685a2015-12-02 14:08:25 -0800219}
220
mtkleinb9eb4ac2015-02-02 18:26:03 -0800221#endif
222
bsalomonddf30e62015-02-19 11:38:44 -0800223///////////////////////////////////////////////////////////////////////////////
224
225void GrResourceCache::changeTimestamp(uint32_t newTimestamp) { fTimestamp = newTimestamp; }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800226
Brian Salomon1090da62017-01-06 12:04:19 -0500227#ifdef SK_DEBUG
228int GrResourceCache::countUniqueKeysWithTag(const char* tag) const {
229 int count = 0;
230 UniqueHash::ConstIter iter(&fUniqueHash);
231 while (!iter.done()) {
232 if (0 == strcmp(tag, (*iter).getUniqueKey().tag())) {
233 ++count;
234 }
235 ++iter;
236 }
237 return count;
238}
239#endif
240
bsalomon33435572014-11-05 14:47:41 -0800241///////////////////////////////////////////////////////////////////////////////
joshualittf5883a62016-01-13 07:47:38 -0800242
243#define ASSERT_SINGLE_OWNER \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400244 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fRenderTargetContext->singleOwner());)
joshualittf5883a62016-01-13 07:47:38 -0800245
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400246uint32_t GrRenderTargetContextPriv::testingOnly_addLegacyMeshDrawOp(
247 GrPaint&& paint,
248 GrAAType aaType,
249 std::unique_ptr<GrLegacyMeshDrawOp> op,
250 const GrUserStencilSettings* uss,
251 bool snapToCenters) {
joshualittf5883a62016-01-13 07:47:38 -0800252 ASSERT_SINGLE_OWNER
Robert Phillipsc0138922017-03-08 11:50:55 -0500253 if (fRenderTargetContext->drawingManager()->wasAbandoned()) {
254 return SK_InvalidUniqueID;
255 }
Brian Salomonac70f842017-05-08 10:43:33 -0400256 SkDEBUGCODE(fRenderTargetContext->validate());
257 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
258 "GrRenderTargetContext::testingOnly_addLegacyMeshDrawOp");
joshualittf5883a62016-01-13 07:47:38 -0800259
Brian Salomon82f44312017-01-11 13:42:54 -0500260 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
robertphillips28a838e2016-06-23 14:07:00 -0700261 if (uss) {
262 pipelineBuilder.setUserStencil(uss);
cdalton846c0512016-05-13 10:25:00 -0700263 }
Brian Salomon189098e72017-01-19 09:55:19 -0500264 pipelineBuilder.setSnapVerticesToPixelCenters(snapToCenters);
robertphillips28a838e2016-06-23 14:07:00 -0700265
Brian Salomone14bd802017-04-04 15:13:25 -0400266 return fRenderTargetContext->addLegacyMeshDrawOp(std::move(pipelineBuilder), GrNoClip(),
267 std::move(op));
joshualittf5883a62016-01-13 07:47:38 -0800268}
269
Brian Salomonac70f842017-05-08 10:43:33 -0400270uint32_t GrRenderTargetContextPriv::testingOnly_addDrawOp(std::unique_ptr<GrDrawOp> op) {
271 ASSERT_SINGLE_OWNER
272 if (fRenderTargetContext->drawingManager()->wasAbandoned()) {
273 return SK_InvalidUniqueID;
274 }
275 SkDEBUGCODE(fRenderTargetContext->validate());
276 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
277 "GrRenderTargetContext::testingOnly_addDrawOp");
278 return fRenderTargetContext->addDrawOp(GrNoClip(), std::move(op));
279}
280
joshualittf5883a62016-01-13 07:47:38 -0800281#undef ASSERT_SINGLE_OWNER
joshualittf5883a62016-01-13 07:47:38 -0800282
283///////////////////////////////////////////////////////////////////////////////
csmartdaltonf9635992016-08-10 11:09:07 -0700284
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400285GrRenderTarget::Flags GrRenderTargetProxy::testingOnly_getFlags() const {
Brian Salomondac5f6b2017-02-28 16:11:04 -0500286 return fRenderTargetFlags;
csmartdaltonf9635992016-08-10 11:09:07 -0700287}
288
289///////////////////////////////////////////////////////////////////////////////
bsalomon33435572014-11-05 14:47:41 -0800290// Code for the mock context. It's built on a mock GrGpu class that does nothing.
291////
292
bsalomon33435572014-11-05 14:47:41 -0800293#include "GrGpu.h"
294
egdaniel8dd688b2015-01-22 10:16:09 -0800295class GrPipeline;
joshualittd53a8272014-11-10 16:03:14 -0800296
bsalomon41e4384e2016-01-08 09:12:44 -0800297class MockCaps : public GrCaps {
298public:
299 explicit MockCaps(const GrContextOptions& options) : INHERITED(options) {}
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400300 bool isConfigTexturable(GrPixelConfig) const override { return false; }
bsalomon41e4384e2016-01-08 09:12:44 -0800301 bool isConfigRenderable(GrPixelConfig config, bool withMSAA) const override { return false; }
Brian Salomonf9f45122016-11-29 11:59:17 -0500302 bool canConfigBeImageStorage(GrPixelConfig) const override { return false; }
Robert Phillipsbf25d432017-04-07 10:08:53 -0400303 bool initDescForDstCopy(const GrRenderTargetProxy* src, GrSurfaceDesc* desc,
304 bool* rectsMustMatch, bool* disallowSubrect) const override {
Brian Salomon467921e2017-03-06 16:17:12 -0500305 return false;
306 }
Brian Salomonf9f45122016-11-29 11:59:17 -0500307
bsalomon41e4384e2016-01-08 09:12:44 -0800308private:
309 typedef GrCaps INHERITED;
310};
311
bsalomon33435572014-11-05 14:47:41 -0800312class MockGpu : public GrGpu {
313public:
bsalomon682c2692015-05-22 14:01:46 -0700314 MockGpu(GrContext* context, const GrContextOptions& options) : INHERITED(context) {
bsalomon41e4384e2016-01-08 09:12:44 -0800315 fCaps.reset(new MockCaps(options));
bsalomon682c2692015-05-22 14:01:46 -0700316 }
mtklein36352bf2015-03-25 18:17:31 -0700317 ~MockGpu() override {}
bsalomon33435572014-11-05 14:47:41 -0800318
bsalomonf0674512015-07-28 13:26:15 -0700319 bool onGetReadPixelsInfo(GrSurface* srcSurface, int readWidth, int readHeight, size_t rowBytes,
320 GrPixelConfig readConfig, DrawPreference*,
321 ReadPixelTempDrawInfo*) override { return false; }
322
cblumeed828002016-02-16 13:00:01 -0800323 bool onGetWritePixelsInfo(GrSurface* dstSurface, int width, int height,
bsalomonf0674512015-07-28 13:26:15 -0700324 GrPixelConfig srcConfig, DrawPreference*,
325 WritePixelTempDrawInfo*) override { return false; }
bsalomon39826022015-07-23 08:07:21 -0700326
joshualitt1cbdcde2015-08-21 11:53:29 -0700327 bool onCopySurface(GrSurface* dst,
328 GrSurface* src,
329 const SkIRect& srcRect,
Mike Kleinfc6c37b2016-09-27 09:34:10 -0400330 const SkIPoint& dstPoint) override { return false; }
bsalomonf90a02b2014-11-26 12:28:00 -0800331
csmartdaltonc25c5d72016-11-01 07:03:59 -0700332 void onQueryMultisampleSpecs(GrRenderTarget* rt, const GrStencilSettings&,
333 int* effectiveSampleCnt, SamplePattern*) override {
Brian Salomon154ce912017-05-17 10:40:02 -0400334 *effectiveSampleCnt = rt->numStencilSamples();
cdalton28f45b92016-03-07 13:58:26 -0800335 }
336
Brian Salomonc293a292016-11-30 13:38:32 -0500337 GrGpuCommandBuffer* createCommandBuffer(const GrGpuCommandBuffer::LoadAndStoreInfo&,
egdaniel9cb63402016-06-23 08:37:05 -0700338 const GrGpuCommandBuffer::LoadAndStoreInfo&) override {
egdaniel066df7c2016-06-08 14:02:27 -0700339 return nullptr;
340 }
341
Mike Kleinfc6c37b2016-09-27 09:34:10 -0400342 void drawDebugWireRect(GrRenderTarget*, const SkIRect&, GrColor) override {}
bsalomon6dea83f2015-12-03 12:58:06 -0800343
Greg Daniel6be35232017-03-01 17:01:09 -0500344 GrFence SK_WARN_UNUSED_RESULT insertFence() override { return 0; }
345 bool waitFence(GrFence, uint64_t) override { return true; }
jvanverth84741b32016-09-30 08:39:02 -0700346 void deleteFence(GrFence) const override {}
347
Greg Daniel6be35232017-03-01 17:01:09 -0500348 sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT makeSemaphore() override { return nullptr; }
Brian Osmandc87c952017-04-28 13:57:38 -0400349 void insertSemaphore(sk_sp<GrSemaphore> semaphore, bool flush) override {}
Greg Daniel6be35232017-03-01 17:01:09 -0500350 void waitSemaphore(sk_sp<GrSemaphore> semaphore) override {}
Brian Osman13dddce2017-05-09 13:19:50 -0400351 sk_sp<GrSemaphore> prepareTextureForCrossContextUsage(GrTexture*) override { return nullptr; }
Greg Daniel6be35232017-03-01 17:01:09 -0500352
bsalomon33435572014-11-05 14:47:41 -0800353private:
mtklein36352bf2015-03-25 18:17:31 -0700354 void onResetContext(uint32_t resetBits) override {}
bsalomonf90a02b2014-11-26 12:28:00 -0800355
bsalomoncb02b382015-08-12 11:14:50 -0700356 void xferBarrier(GrRenderTarget*, GrXferBarrierType) override {}
357
kkinnunen2e6055b2016-04-22 01:48:29 -0700358 GrTexture* onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
cblume55f2d2d2016-02-26 13:20:48 -0800359 const SkTArray<GrMipLevel>& texels) override {
halcanary96fcdcc2015-08-27 07:41:13 -0700360 return nullptr;
bsalomon33435572014-11-05 14:47:41 -0800361 }
362
kkinnunen2e6055b2016-04-22 01:48:29 -0700363 GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
cblume55f2d2d2016-02-26 13:20:48 -0800364 const SkTArray<GrMipLevel>& texels) override {
halcanary96fcdcc2015-08-27 07:41:13 -0700365 return nullptr;
bsalomon33435572014-11-05 14:47:41 -0800366 }
367
Greg Daniel7ef28f32017-04-20 16:41:55 +0000368 sk_sp<GrTexture> onWrapBackendTexture(const GrBackendTexture&,
369 GrSurfaceOrigin,
370 GrBackendTextureFlags,
371 int sampleCnt,
372 GrWrapOwnership) override {
halcanary96fcdcc2015-08-27 07:41:13 -0700373 return nullptr;
bsalomon33435572014-11-05 14:47:41 -0800374 }
375
Greg Danielbcf612b2017-05-01 13:50:58 +0000376 sk_sp<GrRenderTarget> onWrapBackendRenderTarget(const GrBackendRenderTarget&,
377 GrSurfaceOrigin) override {
bungeman6bd52842016-10-27 09:30:08 -0700378 return nullptr;
379 }
380
Greg Daniel7ef28f32017-04-20 16:41:55 +0000381 sk_sp<GrRenderTarget> onWrapBackendTextureAsRenderTarget(const GrBackendTexture&,
382 GrSurfaceOrigin,
383 int sampleCnt) override {
ericrkf7b8b8a2016-02-24 14:49:51 -0800384 return nullptr;
385 }
386
cdalton1bf3e712016-04-19 10:00:02 -0700387 GrBuffer* onCreateBuffer(size_t, GrBufferType, GrAccessPattern, const void*) override {
388 return nullptr;
389 }
jvanverth73063dc2015-12-03 09:15:47 -0800390
csmartdaltone0d36292016-07-29 08:14:20 -0700391 gr_instanced::InstancedRendering* onCreateInstancedRendering() override { return nullptr; }
392
bsalomon6cb3cbe2015-07-30 07:34:27 -0700393 bool onReadPixels(GrSurface* surface,
bsalomonf90a02b2014-11-26 12:28:00 -0800394 int left, int top, int width, int height,
395 GrPixelConfig,
396 void* buffer,
mtklein36352bf2015-03-25 18:17:31 -0700397 size_t rowBytes) override {
bsalomonf90a02b2014-11-26 12:28:00 -0800398 return false;
bsalomon33435572014-11-05 14:47:41 -0800399 }
400
bsalomon6cb3cbe2015-07-30 07:34:27 -0700401 bool onWritePixels(GrSurface* surface,
402 int left, int top, int width, int height,
cblume55f2d2d2016-02-26 13:20:48 -0800403 GrPixelConfig config, const SkTArray<GrMipLevel>& texels) override {
bsalomon33435572014-11-05 14:47:41 -0800404 return false;
405 }
406
jvanverthc3d706f2016-04-20 10:33:27 -0700407 bool onTransferPixels(GrSurface* surface,
jvanverth17aa0472016-01-05 10:41:27 -0800408 int left, int top, int width, int height,
cdalton397536c2016-03-25 12:15:03 -0700409 GrPixelConfig config, GrBuffer* transferBuffer,
jvanverth17aa0472016-01-05 10:41:27 -0800410 size_t offset, size_t rowBytes) override {
411 return false;
412 }
413
mtklein36352bf2015-03-25 18:17:31 -0700414 void onResolveRenderTarget(GrRenderTarget* target) override { return; }
bsalomonf90a02b2014-11-26 12:28:00 -0800415
egdanielec00d942015-09-14 12:56:10 -0700416 GrStencilAttachment* createStencilAttachmentForRenderTarget(const GrRenderTarget*,
417 int width,
418 int height) override {
419 return nullptr;
bsalomon33435572014-11-05 14:47:41 -0800420 }
421
mtklein36352bf2015-03-25 18:17:31 -0700422 void clearStencil(GrRenderTarget* target) override {}
bsalomon33435572014-11-05 14:47:41 -0800423
jvanverth88957922015-07-14 11:02:52 -0700424 GrBackendObject createTestingOnlyBackendTexture(void* pixels, int w, int h,
egdaniel0a3a7f72016-06-24 09:22:31 -0700425 GrPixelConfig config, bool isRT) override {
cblume61214052016-01-26 09:10:48 -0800426 return 0;
jvanverth88957922015-07-14 11:02:52 -0700427 }
bsalomon67d76202015-11-11 12:40:42 -0800428 bool isTestingOnlyBackendTexture(GrBackendObject ) const override { return false; }
bsalomone63ffef2016-02-05 07:17:34 -0800429 void deleteTestingOnlyBackendTexture(GrBackendObject, bool abandonTexture) override {}
jvanverth672bb7f2015-07-13 07:19:57 -0700430
bsalomon33435572014-11-05 14:47:41 -0800431 typedef GrGpu INHERITED;
432};
433
434GrContext* GrContext::CreateMockContext() {
halcanary385fe4d2015-08-26 13:07:48 -0700435 GrContext* context = new GrContext;
bsalomon33435572014-11-05 14:47:41 -0800436
437 context->initMockContext();
438 return context;
439}
440
441void GrContext::initMockContext() {
bsalomon682c2692015-05-22 14:01:46 -0700442 GrContextOptions options;
cdalton397536c2016-03-25 12:15:03 -0700443 options.fBufferMapThreshold = 0;
halcanary96fcdcc2015-08-27 07:41:13 -0700444 SkASSERT(nullptr == fGpu);
halcanary385fe4d2015-08-26 13:07:48 -0700445 fGpu = new MockGpu(this, options);
bsalomon33435572014-11-05 14:47:41 -0800446 SkASSERT(fGpu);
bsalomon69cfe952015-11-30 13:27:47 -0800447 this->initCommon(options);
bsalomon33435572014-11-05 14:47:41 -0800448
449 // We delete these because we want to test the cache starting with zero resources. Also, none of
450 // these objects are required for any of tests that use this context. TODO: make stop allocating
451 // resources in the buffer pools.
robertphillips77a2e522015-10-17 07:43:27 -0700452 fDrawingManager->abandon();
bsalomon33435572014-11-05 14:47:41 -0800453}
Chris Daltonfe199b72017-05-05 11:26:15 -0400454
Brian Salomon17726632017-05-12 14:09:46 -0400455//////////////////////////////////////////////////////////////////////////////
456
Chris Daltonfe199b72017-05-05 11:26:15 -0400457void GrContextPriv::testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
458 fContext->flush();
459 fContext->fDrawingManager->testingOnly_removeOnFlushCallbackObject(cb);
460}
461
462void GrDrawingManager::testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
463 int n = std::find(fOnFlushCBObjects.begin(), fOnFlushCBObjects.end(), cb) -
464 fOnFlushCBObjects.begin();
465 SkASSERT(n < fOnFlushCBObjects.count());
466 fOnFlushCBObjects.removeShuffle(n);
467}
Brian Salomon17726632017-05-12 14:09:46 -0400468
469//////////////////////////////////////////////////////////////////////////////
470
471#define DRAW_OP_TEST_EXTERN(Op) \
472 extern std::unique_ptr<GrDrawOp> Op##__Test(GrPaint&&, SkRandom*, GrContext*, GrFSAAType);
473
474#define LEGACY_MESH_DRAW_OP_TEST_EXTERN(Op) \
475 extern std::unique_ptr<GrLegacyMeshDrawOp> Op##__Test(SkRandom*, GrContext*);
476
477#define DRAW_OP_TEST_ENTRY(Op) Op##__Test
478
479LEGACY_MESH_DRAW_OP_TEST_EXTERN(AAConvexPathOp);
480LEGACY_MESH_DRAW_OP_TEST_EXTERN(AAFillRectOp);
481LEGACY_MESH_DRAW_OP_TEST_EXTERN(AAFillRectOpLocalMatrix);
482LEGACY_MESH_DRAW_OP_TEST_EXTERN(AAFlatteningConvexPathOp)
483LEGACY_MESH_DRAW_OP_TEST_EXTERN(AAHairlineOp);
484LEGACY_MESH_DRAW_OP_TEST_EXTERN(AAStrokeRectOp);
485LEGACY_MESH_DRAW_OP_TEST_EXTERN(AnalyticRectOp);
Brian Salomon17726632017-05-12 14:09:46 -0400486LEGACY_MESH_DRAW_OP_TEST_EXTERN(DashOp);
487LEGACY_MESH_DRAW_OP_TEST_EXTERN(DefaultPathOp);
Brian Salomon17726632017-05-12 14:09:46 -0400488LEGACY_MESH_DRAW_OP_TEST_EXTERN(GrDrawAtlasOp);
489LEGACY_MESH_DRAW_OP_TEST_EXTERN(NonAAStrokeRectOp);
Brian Salomon17726632017-05-12 14:09:46 -0400490LEGACY_MESH_DRAW_OP_TEST_EXTERN(SmallPathOp);
491LEGACY_MESH_DRAW_OP_TEST_EXTERN(TesselatingPathOp);
492LEGACY_MESH_DRAW_OP_TEST_EXTERN(TextBlobOp);
493LEGACY_MESH_DRAW_OP_TEST_EXTERN(VerticesOp);
494
Brian Salomon05441c42017-05-15 16:45:49 -0400495DRAW_OP_TEST_EXTERN(CircleOp)
496DRAW_OP_TEST_EXTERN(DIEllipseOp);
497DRAW_OP_TEST_EXTERN(EllipseOp);
Brian Salomon17726632017-05-12 14:09:46 -0400498DRAW_OP_TEST_EXTERN(NonAAFillRectOp)
Brian Salomon05441c42017-05-15 16:45:49 -0400499DRAW_OP_TEST_EXTERN(RRectOp);
Brian Salomon17726632017-05-12 14:09:46 -0400500
501void GrDrawRandomOp(SkRandom* random, GrRenderTargetContext* renderTargetContext, GrPaint&& paint) {
502 GrContext* context = renderTargetContext->surfPriv().getContext();
503 using MakeTestLegacyMeshDrawOpFn = std::unique_ptr<GrLegacyMeshDrawOp>(SkRandom*, GrContext*);
504 static constexpr MakeTestLegacyMeshDrawOpFn* gLegacyFactories[] = {
505 DRAW_OP_TEST_ENTRY(AAConvexPathOp),
506 DRAW_OP_TEST_ENTRY(AAFillRectOp),
507 DRAW_OP_TEST_ENTRY(AAFillRectOpLocalMatrix),
508 DRAW_OP_TEST_ENTRY(AAFlatteningConvexPathOp),
509 DRAW_OP_TEST_ENTRY(AAHairlineOp),
510 DRAW_OP_TEST_ENTRY(AAStrokeRectOp),
511 DRAW_OP_TEST_ENTRY(AnalyticRectOp),
Brian Salomon17726632017-05-12 14:09:46 -0400512 DRAW_OP_TEST_ENTRY(DashOp),
513 DRAW_OP_TEST_ENTRY(DefaultPathOp),
Brian Salomon17726632017-05-12 14:09:46 -0400514 DRAW_OP_TEST_ENTRY(GrDrawAtlasOp),
515 DRAW_OP_TEST_ENTRY(NonAAStrokeRectOp),
Brian Salomon17726632017-05-12 14:09:46 -0400516 DRAW_OP_TEST_ENTRY(SmallPathOp),
517 DRAW_OP_TEST_ENTRY(TesselatingPathOp),
518 DRAW_OP_TEST_ENTRY(TextBlobOp),
519 DRAW_OP_TEST_ENTRY(VerticesOp)
520 };
521
522 using MakeDrawOpFn = std::unique_ptr<GrDrawOp>(GrPaint&&, SkRandom*, GrContext*, GrFSAAType);
523 static constexpr MakeDrawOpFn* gFactories[] = {
Brian Salomon05441c42017-05-15 16:45:49 -0400524 DRAW_OP_TEST_ENTRY(CircleOp),
525 DRAW_OP_TEST_ENTRY(DIEllipseOp),
526 DRAW_OP_TEST_ENTRY(EllipseOp),
Brian Salomon17726632017-05-12 14:09:46 -0400527 DRAW_OP_TEST_ENTRY(NonAAFillRectOp),
Brian Salomon05441c42017-05-15 16:45:49 -0400528 DRAW_OP_TEST_ENTRY(RRectOp),
Brian Salomon17726632017-05-12 14:09:46 -0400529 };
530
531 static constexpr size_t kTotal = SK_ARRAY_COUNT(gLegacyFactories) + SK_ARRAY_COUNT(gFactories);
532
533 uint32_t index = random->nextULessThan(static_cast<uint32_t>(kTotal));
534 if (index < SK_ARRAY_COUNT(gLegacyFactories)) {
535 const GrUserStencilSettings* uss = GrGetRandomStencil(random, context);
536 // We don't use kHW because we will hit an assertion if the render target is not
537 // multisampled
538 static constexpr GrAAType kAATypes[] = {GrAAType::kNone, GrAAType::kCoverage};
539 GrAAType aaType = kAATypes[random->nextULessThan(SK_ARRAY_COUNT(kAATypes))];
540 bool snapToCenters = random->nextBool();
541 auto op = gLegacyFactories[index](random, context);
542 SkASSERT(op);
543 renderTargetContext->priv().testingOnly_addLegacyMeshDrawOp(
544 std::move(paint), aaType, std::move(op), uss, snapToCenters);
545 } else {
546 auto op = gFactories[index - SK_ARRAY_COUNT(gLegacyFactories)](
547 std::move(paint), random, context, renderTargetContext->fsaaType());
548 SkASSERT(op);
549 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
550 }
551}