blob: cb2f975422a8166d6c0b31589460c76b8eda4a74 [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"
Robert Phillipsfbcef6e2017-06-15 12:07:18 -040015#include "GrGpu.h"
bsalomon3582d3e2015-02-13 14:20:05 -080016#include "GrGpuResourceCacheAccess.h"
robertphillips5fa7f302016-07-21 09:21:04 -070017#include "GrPipelineBuilder.h"
Brian Salomon17726632017-05-12 14:09:46 -040018#include "GrRenderTargetContext.h"
Brian Salomon2ee084e2016-12-16 18:59:19 -050019#include "GrRenderTargetContextPriv.h"
csmartdaltonf9635992016-08-10 11:09:07 -070020#include "GrRenderTargetProxy.h"
bsalomon0ea80f42015-02-11 10:49:59 -080021#include "GrResourceCache.h"
Greg Daniel6be35232017-03-01 17:01:09 -050022#include "GrSemaphore.h"
Brian Salomon17726632017-05-12 14:09:46 -040023#include "GrSurfaceContextPriv.h"
Robert Phillips646e4292017-06-13 12:44:56 -040024#include "GrTexture.h"
Brian Osman3b655982017-03-07 16:58:08 -050025#include "SkGr.h"
Robert Phillips22f4a1f2016-12-20 08:57:26 -050026#include "SkImage_Gpu.h"
halcanary4dbbd042016-06-07 17:21:10 -070027#include "SkMathPriv.h"
mtkleinb9eb4ac2015-02-02 18:26:03 -080028#include "SkString.h"
Brian Salomon17726632017-05-12 14:09:46 -040029#include "ops/GrMeshDrawOp.h"
Brian Salomonf856fd12016-12-16 14:24:34 -050030#include "text/GrAtlasGlyphCache.h"
joshualitte8042922015-12-11 06:11:21 -080031#include "text/GrTextBlobCache.h"
32
joshualitt7f9c9eb2015-08-21 11:08:00 -070033namespace GrTest {
34void SetupAlwaysEvictAtlas(GrContext* context) {
35 // These sizes were selected because they allow each atlas to hold a single plot and will thus
36 // stress the atlas
Brian Salomon2ee084e2016-12-16 18:59:19 -050037 int dim = GrDrawOpAtlas::kGlyphMaxDim;
38 GrDrawOpAtlasConfig configs[3];
joshualitt7f9c9eb2015-08-21 11:08:00 -070039 configs[kA8_GrMaskFormat].fWidth = dim;
40 configs[kA8_GrMaskFormat].fHeight = dim;
jvanverth7023a002016-02-22 11:25:32 -080041 configs[kA8_GrMaskFormat].fLog2Width = SkNextLog2(dim);
42 configs[kA8_GrMaskFormat].fLog2Height = SkNextLog2(dim);
joshualitt7f9c9eb2015-08-21 11:08:00 -070043 configs[kA8_GrMaskFormat].fPlotWidth = dim;
44 configs[kA8_GrMaskFormat].fPlotHeight = dim;
45
46 configs[kA565_GrMaskFormat].fWidth = dim;
47 configs[kA565_GrMaskFormat].fHeight = dim;
jvanverth7023a002016-02-22 11:25:32 -080048 configs[kA565_GrMaskFormat].fLog2Width = SkNextLog2(dim);
49 configs[kA565_GrMaskFormat].fLog2Height = SkNextLog2(dim);
joshualitt7f9c9eb2015-08-21 11:08:00 -070050 configs[kA565_GrMaskFormat].fPlotWidth = dim;
51 configs[kA565_GrMaskFormat].fPlotHeight = dim;
52
53 configs[kARGB_GrMaskFormat].fWidth = dim;
54 configs[kARGB_GrMaskFormat].fHeight = dim;
jvanverth7023a002016-02-22 11:25:32 -080055 configs[kARGB_GrMaskFormat].fLog2Width = SkNextLog2(dim);
56 configs[kARGB_GrMaskFormat].fLog2Height = SkNextLog2(dim);
joshualitt7f9c9eb2015-08-21 11:08:00 -070057 configs[kARGB_GrMaskFormat].fPlotWidth = dim;
58 configs[kARGB_GrMaskFormat].fPlotHeight = dim;
59
60 context->setTextContextAtlasSizes_ForTesting(configs);
61}
Greg Daniel7ef28f32017-04-20 16:41:55 +000062
63GrBackendTexture CreateBackendTexture(GrBackend backend, int width, int height,
64 GrPixelConfig config, GrBackendObject handle) {
Mike Reedd20b5c42017-06-14 06:03:10 -040065#ifdef SK_VULKAN
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +000066 if (kVulkan_GrBackend == backend) {
Greg Daniel7ef28f32017-04-20 16:41:55 +000067 GrVkImageInfo* vkInfo = (GrVkImageInfo*)(handle);
Greg Daniel207282e2017-04-26 13:29:21 -040068 return GrBackendTexture(width, height, *vkInfo);
Greg Daniel7ef28f32017-04-20 16:41:55 +000069 }
Robert Phillipsfcd5fdd2017-06-14 01:43:29 +000070#endif
71 SkASSERT(kOpenGL_GrBackend == backend);
72 GrGLTextureInfo* glInfo = (GrGLTextureInfo*)(handle);
73 return GrBackendTexture(width, height, config, *glInfo);
Greg Daniel7ef28f32017-04-20 16:41:55 +000074}
joshualitt7f9c9eb2015-08-21 11:08:00 -070075};
76
Robert Phillipseaa86252016-11-08 13:49:39 +000077bool GrSurfaceProxy::isWrapped_ForTesting() const {
78 return SkToBool(fTarget);
79}
80
81bool GrRenderTargetContext::isWrapped_ForTesting() const {
82 return fRenderTargetProxy->isWrapped_ForTesting();
83}
84
joshualitt17d833b2015-08-03 10:17:44 -070085void GrContext::setTextBlobCacheLimit_ForTesting(size_t bytes) {
86 fTextBlobCache->setBudget(bytes);
87}
88
Brian Salomon2ee084e2016-12-16 18:59:19 -050089void GrContext::setTextContextAtlasSizes_ForTesting(const GrDrawOpAtlasConfig* configs) {
Brian Salomonf856fd12016-12-16 14:24:34 -050090 fAtlasGlyphCache->setAtlasSizes_ForTesting(configs);
joshualittda04e0e2015-08-19 08:16:43 -070091}
92
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000093///////////////////////////////////////////////////////////////////////////////
94
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +000095void GrContext::purgeAllUnlockedResources() {
bsalomon0ea80f42015-02-11 10:49:59 -080096 fResourceCache->purgeAllUnlocked();
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +000097}
bsalomon33435572014-11-05 14:47:41 -080098
joshualitte45c81c2015-12-02 09:05:37 -080099void GrContext::resetGpuStats() const {
100#if GR_GPU_STATS
101 fGpu->stats()->reset();
102#endif
103}
104
mtkleinb9eb4ac2015-02-02 18:26:03 -0800105void GrContext::dumpCacheStats(SkString* out) const {
106#if GR_CACHE_STATS
bsalomon0ea80f42015-02-11 10:49:59 -0800107 fResourceCache->dumpStats(out);
mtkleinb9eb4ac2015-02-02 18:26:03 -0800108#endif
109}
110
joshualittdc5685a2015-12-02 14:08:25 -0800111void GrContext::dumpCacheStatsKeyValuePairs(SkTArray<SkString>* keys,
112 SkTArray<double>* values) const {
113#if GR_CACHE_STATS
114 fResourceCache->dumpStatsKeyValuePairs(keys, values);
115#endif
116}
117
mtkleinb9eb4ac2015-02-02 18:26:03 -0800118void GrContext::printCacheStats() const {
119 SkString out;
120 this->dumpCacheStats(&out);
kkinnunen297aaf92015-02-19 06:32:12 -0800121 SkDebugf("%s", out.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800122}
123
124void GrContext::dumpGpuStats(SkString* out) const {
125#if GR_GPU_STATS
126 return fGpu->stats()->dump(out);
127#endif
128}
129
joshualitte45c81c2015-12-02 09:05:37 -0800130void GrContext::dumpGpuStatsKeyValuePairs(SkTArray<SkString>* keys,
131 SkTArray<double>* values) const {
132#if GR_GPU_STATS
133 return fGpu->stats()->dumpKeyValuePairs(keys, values);
134#endif
135}
136
mtkleinb9eb4ac2015-02-02 18:26:03 -0800137void GrContext::printGpuStats() const {
138 SkString out;
139 this->dumpGpuStats(&out);
kkinnunen297aaf92015-02-19 06:32:12 -0800140 SkDebugf("%s", out.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -0800141}
142
Robert Phillipsc37e6142017-01-20 10:03:25 -0500143sk_sp<SkImage> GrContext::getFontAtlasImage_ForTesting(GrMaskFormat format) {
Brian Salomonf856fd12016-12-16 14:24:34 -0500144 GrAtlasGlyphCache* cache = this->getAtlasGlyphCache();
jvanverth629162d2015-11-08 08:07:24 -0800145
Robert Phillips32f28182017-02-28 16:20:03 -0500146 sk_sp<GrTextureProxy> proxy = cache->getProxy(format);
147 if (!proxy) {
148 return nullptr;
149 }
150
Robert Phillipsb726d582017-03-09 16:36:32 -0500151 SkASSERT(proxy->priv().isExact());
152 sk_sp<SkImage> image(new SkImage_Gpu(this, kNeedNewImageUniqueID, kPremul_SkAlphaType,
153 std::move(proxy), nullptr, SkBudgeted::kNo));
Robert Phillips22f4a1f2016-12-20 08:57:26 -0500154 return image;
jvanverth0671b962015-12-08 18:53:44 -0800155}
jvanverth629162d2015-11-08 08:07:24 -0800156
mtkleinb9eb4ac2015-02-02 18:26:03 -0800157#if GR_GPU_STATS
158void GrGpu::Stats::dump(SkString* out) {
159 out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);
160 out->appendf("Shader Compilations: %d\n", fShaderCompilations);
bsalomonb12ea412015-02-02 21:19:50 -0800161 out->appendf("Textures Created: %d\n", fTextureCreates);
162 out->appendf("Texture Uploads: %d\n", fTextureUploads);
jvanverth17aa0472016-01-05 10:41:27 -0800163 out->appendf("Transfers to Texture: %d\n", fTransfersToTexture);
egdaniel8dc7c3a2015-04-16 11:22:42 -0700164 out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates);
joshualitt87a5c9f2015-09-08 13:42:05 -0700165 out->appendf("Number of draws: %d\n", fNumDraws);
mtkleinb9eb4ac2015-02-02 18:26:03 -0800166}
joshualitte45c81c2015-12-02 09:05:37 -0800167
168void GrGpu::Stats::dumpKeyValuePairs(SkTArray<SkString>* keys, SkTArray<double>* values) {
169 keys->push_back(SkString("render_target_binds")); values->push_back(fRenderTargetBinds);
170 keys->push_back(SkString("shader_compilations")); values->push_back(fShaderCompilations);
joshualitte45c81c2015-12-02 09:05:37 -0800171 keys->push_back(SkString("texture_uploads")); values->push_back(fTextureUploads);
joshualitte45c81c2015-12-02 09:05:37 -0800172 keys->push_back(SkString("number_of_draws")); values->push_back(fNumDraws);
bsalomon1d417a82016-03-23 11:50:26 -0700173 keys->push_back(SkString("number_of_failed_draws")); values->push_back(fNumFailedDraws);
joshualitte45c81c2015-12-02 09:05:37 -0800174}
175
mtkleinb9eb4ac2015-02-02 18:26:03 -0800176#endif
177
178#if GR_CACHE_STATS
robertphillips60029a52015-11-09 13:51:06 -0800179void GrResourceCache::getStats(Stats* stats) const {
180 stats->reset();
181
182 stats->fTotal = this->getResourceCount();
183 stats->fNumNonPurgeable = fNonpurgeableResources.count();
184 stats->fNumPurgeable = fPurgeableQueue.count();
185
186 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
187 stats->update(fNonpurgeableResources[i]);
188 }
189 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
190 stats->update(fPurgeableQueue.at(i));
191 }
192}
193
bsalomon0ea80f42015-02-11 10:49:59 -0800194void GrResourceCache::dumpStats(SkString* out) const {
mtkleinb9eb4ac2015-02-02 18:26:03 -0800195 this->validate();
196
bsalomonf320e042015-02-17 15:09:34 -0800197 Stats stats;
198
robertphillips60029a52015-11-09 13:51:06 -0800199 this->getStats(&stats);
mtkleinb9eb4ac2015-02-02 18:26:03 -0800200
201 float countUtilization = (100.f * fBudgetedCount) / fMaxCount;
202 float byteUtilization = (100.f * fBudgetedBytes) / fMaxBytes;
203
204 out->appendf("Budget: %d items %d bytes\n", fMaxCount, (int)fMaxBytes);
205 out->appendf("\t\tEntry Count: current %d"
kkinnunen2e6055b2016-04-22 01:48:29 -0700206 " (%d budgeted, %d wrapped, %d locked, %d scratch %.2g%% full), high %d\n",
207 stats.fTotal, fBudgetedCount, stats.fWrapped, stats.fNumNonPurgeable,
208 stats.fScratch, countUtilization, fHighWaterCount);
mtkleinb9eb4ac2015-02-02 18:26:03 -0800209 out->appendf("\t\tEntry Bytes: current %d (budgeted %d, %.2g%% full, %d unbudgeted) high %d\n",
bsalomonf320e042015-02-17 15:09:34 -0800210 SkToInt(fBytes), SkToInt(fBudgetedBytes), byteUtilization,
211 SkToInt(stats.fUnbudgetedSize), SkToInt(fHighWaterBytes));
mtkleinb9eb4ac2015-02-02 18:26:03 -0800212}
213
joshualittdc5685a2015-12-02 14:08:25 -0800214void GrResourceCache::dumpStatsKeyValuePairs(SkTArray<SkString>* keys,
215 SkTArray<double>* values) const {
216 this->validate();
217
218 Stats stats;
219 this->getStats(&stats);
220
joshualittdc5685a2015-12-02 14:08:25 -0800221 keys->push_back(SkString("gpu_cache_purgable_entries")); values->push_back(stats.fNumPurgeable);
joshualittdc5685a2015-12-02 14:08:25 -0800222}
223
mtkleinb9eb4ac2015-02-02 18:26:03 -0800224#endif
225
bsalomonddf30e62015-02-19 11:38:44 -0800226///////////////////////////////////////////////////////////////////////////////
227
228void GrResourceCache::changeTimestamp(uint32_t newTimestamp) { fTimestamp = newTimestamp; }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800229
Brian Salomon1090da62017-01-06 12:04:19 -0500230#ifdef SK_DEBUG
231int GrResourceCache::countUniqueKeysWithTag(const char* tag) const {
232 int count = 0;
233 UniqueHash::ConstIter iter(&fUniqueHash);
234 while (!iter.done()) {
235 if (0 == strcmp(tag, (*iter).getUniqueKey().tag())) {
236 ++count;
237 }
238 ++iter;
239 }
240 return count;
241}
242#endif
243
bsalomon33435572014-11-05 14:47:41 -0800244///////////////////////////////////////////////////////////////////////////////
joshualittf5883a62016-01-13 07:47:38 -0800245
246#define ASSERT_SINGLE_OWNER \
Robert Phillipsa90aa2b2017-04-10 08:19:26 -0400247 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fRenderTargetContext->singleOwner());)
joshualittf5883a62016-01-13 07:47:38 -0800248
Brian Salomond3ccb0a2017-04-03 10:38:00 -0400249uint32_t GrRenderTargetContextPriv::testingOnly_addLegacyMeshDrawOp(
250 GrPaint&& paint,
251 GrAAType aaType,
252 std::unique_ptr<GrLegacyMeshDrawOp> op,
253 const GrUserStencilSettings* uss,
254 bool snapToCenters) {
joshualittf5883a62016-01-13 07:47:38 -0800255 ASSERT_SINGLE_OWNER
Robert Phillipsc0138922017-03-08 11:50:55 -0500256 if (fRenderTargetContext->drawingManager()->wasAbandoned()) {
257 return SK_InvalidUniqueID;
258 }
Brian Salomonac70f842017-05-08 10:43:33 -0400259 SkDEBUGCODE(fRenderTargetContext->validate());
260 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
261 "GrRenderTargetContext::testingOnly_addLegacyMeshDrawOp");
joshualittf5883a62016-01-13 07:47:38 -0800262
Brian Salomon82f44312017-01-11 13:42:54 -0500263 GrPipelineBuilder pipelineBuilder(std::move(paint), aaType);
robertphillips28a838e2016-06-23 14:07:00 -0700264 if (uss) {
265 pipelineBuilder.setUserStencil(uss);
cdalton846c0512016-05-13 10:25:00 -0700266 }
Brian Salomon189098e72017-01-19 09:55:19 -0500267 pipelineBuilder.setSnapVerticesToPixelCenters(snapToCenters);
robertphillips28a838e2016-06-23 14:07:00 -0700268
Brian Salomone14bd802017-04-04 15:13:25 -0400269 return fRenderTargetContext->addLegacyMeshDrawOp(std::move(pipelineBuilder), GrNoClip(),
270 std::move(op));
joshualittf5883a62016-01-13 07:47:38 -0800271}
272
Brian Salomonac70f842017-05-08 10:43:33 -0400273uint32_t GrRenderTargetContextPriv::testingOnly_addDrawOp(std::unique_ptr<GrDrawOp> op) {
274 ASSERT_SINGLE_OWNER
275 if (fRenderTargetContext->drawingManager()->wasAbandoned()) {
276 return SK_InvalidUniqueID;
277 }
278 SkDEBUGCODE(fRenderTargetContext->validate());
279 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->fAuditTrail,
280 "GrRenderTargetContext::testingOnly_addDrawOp");
281 return fRenderTargetContext->addDrawOp(GrNoClip(), std::move(op));
282}
283
joshualittf5883a62016-01-13 07:47:38 -0800284#undef ASSERT_SINGLE_OWNER
joshualittf5883a62016-01-13 07:47:38 -0800285
286///////////////////////////////////////////////////////////////////////////////
csmartdaltonf9635992016-08-10 11:09:07 -0700287
Robert Phillipsc4f0a822017-06-13 08:11:36 -0400288GrRenderTargetFlags GrRenderTargetProxy::testingOnly_getFlags() const {
Brian Salomondac5f6b2017-02-28 16:11:04 -0500289 return fRenderTargetFlags;
csmartdaltonf9635992016-08-10 11:09:07 -0700290}
291
292///////////////////////////////////////////////////////////////////////////////
bsalomon33435572014-11-05 14:47:41 -0800293// Code for the mock context. It's built on a mock GrGpu class that does nothing.
294////
295
bsalomon33435572014-11-05 14:47:41 -0800296#include "GrGpu.h"
297
egdaniel8dd688b2015-01-22 10:16:09 -0800298class GrPipeline;
joshualittd53a8272014-11-10 16:03:14 -0800299
bsalomon41e4384e2016-01-08 09:12:44 -0800300class MockCaps : public GrCaps {
301public:
302 explicit MockCaps(const GrContextOptions& options) : INHERITED(options) {}
Robert Phillipsb7b7e5f2017-05-22 13:23:19 -0400303 bool isConfigTexturable(GrPixelConfig) const override { return false; }
bsalomon41e4384e2016-01-08 09:12:44 -0800304 bool isConfigRenderable(GrPixelConfig config, bool withMSAA) const override { return false; }
Brian Salomonf9f45122016-11-29 11:59:17 -0500305 bool canConfigBeImageStorage(GrPixelConfig) const override { return false; }
Robert Phillipsbf25d432017-04-07 10:08:53 -0400306 bool initDescForDstCopy(const GrRenderTargetProxy* src, GrSurfaceDesc* desc,
307 bool* rectsMustMatch, bool* disallowSubrect) const override {
Brian Salomon467921e2017-03-06 16:17:12 -0500308 return false;
309 }
Brian Salomonf9f45122016-11-29 11:59:17 -0500310
bsalomon41e4384e2016-01-08 09:12:44 -0800311private:
312 typedef GrCaps INHERITED;
313};
314
bsalomon33435572014-11-05 14:47:41 -0800315class MockGpu : public GrGpu {
316public:
bsalomon682c2692015-05-22 14:01:46 -0700317 MockGpu(GrContext* context, const GrContextOptions& options) : INHERITED(context) {
bsalomon41e4384e2016-01-08 09:12:44 -0800318 fCaps.reset(new MockCaps(options));
bsalomon682c2692015-05-22 14:01:46 -0700319 }
mtklein36352bf2015-03-25 18:17:31 -0700320 ~MockGpu() override {}
bsalomon33435572014-11-05 14:47:41 -0800321
bsalomonf0674512015-07-28 13:26:15 -0700322 bool onGetReadPixelsInfo(GrSurface* srcSurface, int readWidth, int readHeight, size_t rowBytes,
323 GrPixelConfig readConfig, DrawPreference*,
324 ReadPixelTempDrawInfo*) override { return false; }
325
cblumeed828002016-02-16 13:00:01 -0800326 bool onGetWritePixelsInfo(GrSurface* dstSurface, int width, int height,
bsalomonf0674512015-07-28 13:26:15 -0700327 GrPixelConfig srcConfig, DrawPreference*,
328 WritePixelTempDrawInfo*) override { return false; }
bsalomon39826022015-07-23 08:07:21 -0700329
joshualitt1cbdcde2015-08-21 11:53:29 -0700330 bool onCopySurface(GrSurface* dst,
331 GrSurface* src,
332 const SkIRect& srcRect,
Mike Kleinfc6c37b2016-09-27 09:34:10 -0400333 const SkIPoint& dstPoint) override { return false; }
bsalomonf90a02b2014-11-26 12:28:00 -0800334
csmartdaltonc25c5d72016-11-01 07:03:59 -0700335 void onQueryMultisampleSpecs(GrRenderTarget* rt, const GrStencilSettings&,
336 int* effectiveSampleCnt, SamplePattern*) override {
Brian Salomon154ce912017-05-17 10:40:02 -0400337 *effectiveSampleCnt = rt->numStencilSamples();
cdalton28f45b92016-03-07 13:58:26 -0800338 }
339
Brian Salomonc293a292016-11-30 13:38:32 -0500340 GrGpuCommandBuffer* createCommandBuffer(const GrGpuCommandBuffer::LoadAndStoreInfo&,
egdaniel9cb63402016-06-23 08:37:05 -0700341 const GrGpuCommandBuffer::LoadAndStoreInfo&) override {
egdaniel066df7c2016-06-08 14:02:27 -0700342 return nullptr;
343 }
344
Greg Daniel6be35232017-03-01 17:01:09 -0500345 GrFence SK_WARN_UNUSED_RESULT insertFence() override { return 0; }
346 bool waitFence(GrFence, uint64_t) override { return true; }
jvanverth84741b32016-09-30 08:39:02 -0700347 void deleteFence(GrFence) const override {}
348
Greg Daniel6be35232017-03-01 17:01:09 -0500349 sk_sp<GrSemaphore> SK_WARN_UNUSED_RESULT makeSemaphore() override { return nullptr; }
Brian Osmandc87c952017-04-28 13:57:38 -0400350 void insertSemaphore(sk_sp<GrSemaphore> semaphore, bool flush) override {}
Greg Daniel6be35232017-03-01 17:01:09 -0500351 void waitSemaphore(sk_sp<GrSemaphore> semaphore) override {}
Brian Osman13dddce2017-05-09 13:19:50 -0400352 sk_sp<GrSemaphore> prepareTextureForCrossContextUsage(GrTexture*) override { return nullptr; }
Greg Daniel6be35232017-03-01 17:01:09 -0500353
bsalomon33435572014-11-05 14:47:41 -0800354private:
mtklein36352bf2015-03-25 18:17:31 -0700355 void onResetContext(uint32_t resetBits) override {}
bsalomonf90a02b2014-11-26 12:28:00 -0800356
bsalomoncb02b382015-08-12 11:14:50 -0700357 void xferBarrier(GrRenderTarget*, GrXferBarrierType) override {}
358
Robert Phillips67d52cf2017-06-05 13:38:13 -0400359 sk_sp<GrTexture> onCreateTexture(const GrSurfaceDesc& desc, SkBudgeted budgeted,
360 const SkTArray<GrMipLevel>& texels) override {
halcanary96fcdcc2015-08-27 07:41:13 -0700361 return nullptr;
bsalomon33435572014-11-05 14:47:41 -0800362 }
363
Greg Daniel7ef28f32017-04-20 16:41:55 +0000364 sk_sp<GrTexture> onWrapBackendTexture(const GrBackendTexture&,
365 GrSurfaceOrigin,
366 GrBackendTextureFlags,
367 int sampleCnt,
368 GrWrapOwnership) override {
halcanary96fcdcc2015-08-27 07:41:13 -0700369 return nullptr;
bsalomon33435572014-11-05 14:47:41 -0800370 }
371
Greg Danielbcf612b2017-05-01 13:50:58 +0000372 sk_sp<GrRenderTarget> onWrapBackendRenderTarget(const GrBackendRenderTarget&,
373 GrSurfaceOrigin) override {
bungeman6bd52842016-10-27 09:30:08 -0700374 return nullptr;
375 }
376
Greg Daniel7ef28f32017-04-20 16:41:55 +0000377 sk_sp<GrRenderTarget> onWrapBackendTextureAsRenderTarget(const GrBackendTexture&,
378 GrSurfaceOrigin,
379 int sampleCnt) override {
ericrkf7b8b8a2016-02-24 14:49:51 -0800380 return nullptr;
381 }
382
cdalton1bf3e712016-04-19 10:00:02 -0700383 GrBuffer* onCreateBuffer(size_t, GrBufferType, GrAccessPattern, const void*) override {
384 return nullptr;
385 }
jvanverth73063dc2015-12-03 09:15:47 -0800386
csmartdaltone0d36292016-07-29 08:14:20 -0700387 gr_instanced::InstancedRendering* onCreateInstancedRendering() override { return nullptr; }
388
bsalomon6cb3cbe2015-07-30 07:34:27 -0700389 bool onReadPixels(GrSurface* surface,
bsalomonf90a02b2014-11-26 12:28:00 -0800390 int left, int top, int width, int height,
391 GrPixelConfig,
392 void* buffer,
mtklein36352bf2015-03-25 18:17:31 -0700393 size_t rowBytes) override {
bsalomonf90a02b2014-11-26 12:28:00 -0800394 return false;
bsalomon33435572014-11-05 14:47:41 -0800395 }
396
bsalomon6cb3cbe2015-07-30 07:34:27 -0700397 bool onWritePixels(GrSurface* surface,
398 int left, int top, int width, int height,
cblume55f2d2d2016-02-26 13:20:48 -0800399 GrPixelConfig config, const SkTArray<GrMipLevel>& texels) override {
bsalomon33435572014-11-05 14:47:41 -0800400 return false;
401 }
402
jvanverthc3d706f2016-04-20 10:33:27 -0700403 bool onTransferPixels(GrSurface* surface,
jvanverth17aa0472016-01-05 10:41:27 -0800404 int left, int top, int width, int height,
cdalton397536c2016-03-25 12:15:03 -0700405 GrPixelConfig config, GrBuffer* transferBuffer,
jvanverth17aa0472016-01-05 10:41:27 -0800406 size_t offset, size_t rowBytes) override {
407 return false;
408 }
409
mtklein36352bf2015-03-25 18:17:31 -0700410 void onResolveRenderTarget(GrRenderTarget* target) override { return; }
bsalomonf90a02b2014-11-26 12:28:00 -0800411
egdanielec00d942015-09-14 12:56:10 -0700412 GrStencilAttachment* createStencilAttachmentForRenderTarget(const GrRenderTarget*,
413 int width,
414 int height) override {
415 return nullptr;
bsalomon33435572014-11-05 14:47:41 -0800416 }
417
mtklein36352bf2015-03-25 18:17:31 -0700418 void clearStencil(GrRenderTarget* target) override {}
bsalomon33435572014-11-05 14:47:41 -0800419
jvanverth88957922015-07-14 11:02:52 -0700420 GrBackendObject createTestingOnlyBackendTexture(void* pixels, int w, int h,
egdaniel0a3a7f72016-06-24 09:22:31 -0700421 GrPixelConfig config, bool isRT) override {
cblume61214052016-01-26 09:10:48 -0800422 return 0;
jvanverth88957922015-07-14 11:02:52 -0700423 }
bsalomon67d76202015-11-11 12:40:42 -0800424 bool isTestingOnlyBackendTexture(GrBackendObject ) const override { return false; }
bsalomone63ffef2016-02-05 07:17:34 -0800425 void deleteTestingOnlyBackendTexture(GrBackendObject, bool abandonTexture) override {}
jvanverth672bb7f2015-07-13 07:19:57 -0700426
bsalomon33435572014-11-05 14:47:41 -0800427 typedef GrGpu INHERITED;
428};
429
430GrContext* GrContext::CreateMockContext() {
halcanary385fe4d2015-08-26 13:07:48 -0700431 GrContext* context = new GrContext;
bsalomon33435572014-11-05 14:47:41 -0800432
433 context->initMockContext();
434 return context;
435}
436
437void GrContext::initMockContext() {
bsalomon682c2692015-05-22 14:01:46 -0700438 GrContextOptions options;
cdalton397536c2016-03-25 12:15:03 -0700439 options.fBufferMapThreshold = 0;
halcanary96fcdcc2015-08-27 07:41:13 -0700440 SkASSERT(nullptr == fGpu);
halcanary385fe4d2015-08-26 13:07:48 -0700441 fGpu = new MockGpu(this, options);
bsalomon33435572014-11-05 14:47:41 -0800442 SkASSERT(fGpu);
bsalomon69cfe952015-11-30 13:27:47 -0800443 this->initCommon(options);
bsalomon33435572014-11-05 14:47:41 -0800444
445 // We delete these because we want to test the cache starting with zero resources. Also, none of
446 // these objects are required for any of tests that use this context. TODO: make stop allocating
447 // resources in the buffer pools.
robertphillips77a2e522015-10-17 07:43:27 -0700448 fDrawingManager->abandon();
bsalomon33435572014-11-05 14:47:41 -0800449}
Chris Daltonfe199b72017-05-05 11:26:15 -0400450
Brian Salomon17726632017-05-12 14:09:46 -0400451//////////////////////////////////////////////////////////////////////////////
452
Chris Daltonfe199b72017-05-05 11:26:15 -0400453void GrContextPriv::testingOnly_flushAndRemoveOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
454 fContext->flush();
455 fContext->fDrawingManager->testingOnly_removeOnFlushCallbackObject(cb);
456}
457
458void GrDrawingManager::testingOnly_removeOnFlushCallbackObject(GrOnFlushCallbackObject* cb) {
459 int n = std::find(fOnFlushCBObjects.begin(), fOnFlushCBObjects.end(), cb) -
460 fOnFlushCBObjects.begin();
461 SkASSERT(n < fOnFlushCBObjects.count());
462 fOnFlushCBObjects.removeShuffle(n);
463}
Brian Salomon17726632017-05-12 14:09:46 -0400464
465//////////////////////////////////////////////////////////////////////////////
466
467#define DRAW_OP_TEST_EXTERN(Op) \
468 extern std::unique_ptr<GrDrawOp> Op##__Test(GrPaint&&, SkRandom*, GrContext*, GrFSAAType);
469
470#define LEGACY_MESH_DRAW_OP_TEST_EXTERN(Op) \
471 extern std::unique_ptr<GrLegacyMeshDrawOp> Op##__Test(SkRandom*, GrContext*);
472
473#define DRAW_OP_TEST_ENTRY(Op) Op##__Test
474
475LEGACY_MESH_DRAW_OP_TEST_EXTERN(AAConvexPathOp);
Brian Salomon17726632017-05-12 14:09:46 -0400476LEGACY_MESH_DRAW_OP_TEST_EXTERN(AAFlatteningConvexPathOp)
477LEGACY_MESH_DRAW_OP_TEST_EXTERN(AAHairlineOp);
Brian Salomon17726632017-05-12 14:09:46 -0400478LEGACY_MESH_DRAW_OP_TEST_EXTERN(AnalyticRectOp);
Brian Salomon17726632017-05-12 14:09:46 -0400479LEGACY_MESH_DRAW_OP_TEST_EXTERN(DashOp);
480LEGACY_MESH_DRAW_OP_TEST_EXTERN(DefaultPathOp);
Brian Salomon17726632017-05-12 14:09:46 -0400481LEGACY_MESH_DRAW_OP_TEST_EXTERN(GrDrawAtlasOp);
Brian Salomon17726632017-05-12 14:09:46 -0400482LEGACY_MESH_DRAW_OP_TEST_EXTERN(SmallPathOp);
483LEGACY_MESH_DRAW_OP_TEST_EXTERN(TesselatingPathOp);
484LEGACY_MESH_DRAW_OP_TEST_EXTERN(TextBlobOp);
485LEGACY_MESH_DRAW_OP_TEST_EXTERN(VerticesOp);
486
Brian Salomonbaaf4392017-06-15 09:59:23 -0400487DRAW_OP_TEST_EXTERN(AAFillRectOp)
488DRAW_OP_TEST_EXTERN(AAStrokeRectOp);
Brian Salomon05441c42017-05-15 16:45:49 -0400489DRAW_OP_TEST_EXTERN(CircleOp)
490DRAW_OP_TEST_EXTERN(DIEllipseOp);
491DRAW_OP_TEST_EXTERN(EllipseOp);
Brian Salomon17726632017-05-12 14:09:46 -0400492DRAW_OP_TEST_EXTERN(NonAAFillRectOp)
Brian Salomonbaaf4392017-06-15 09:59:23 -0400493DRAW_OP_TEST_EXTERN(NonAAStrokeRectOp);
Brian Salomon05441c42017-05-15 16:45:49 -0400494DRAW_OP_TEST_EXTERN(RRectOp);
Brian Salomon17726632017-05-12 14:09:46 -0400495
496void GrDrawRandomOp(SkRandom* random, GrRenderTargetContext* renderTargetContext, GrPaint&& paint) {
497 GrContext* context = renderTargetContext->surfPriv().getContext();
498 using MakeTestLegacyMeshDrawOpFn = std::unique_ptr<GrLegacyMeshDrawOp>(SkRandom*, GrContext*);
499 static constexpr MakeTestLegacyMeshDrawOpFn* gLegacyFactories[] = {
500 DRAW_OP_TEST_ENTRY(AAConvexPathOp),
Brian Salomon17726632017-05-12 14:09:46 -0400501 DRAW_OP_TEST_ENTRY(AAFlatteningConvexPathOp),
502 DRAW_OP_TEST_ENTRY(AAHairlineOp),
Brian Salomon17726632017-05-12 14:09:46 -0400503 DRAW_OP_TEST_ENTRY(AnalyticRectOp),
Brian Salomon17726632017-05-12 14:09:46 -0400504 DRAW_OP_TEST_ENTRY(DashOp),
505 DRAW_OP_TEST_ENTRY(DefaultPathOp),
Brian Salomon17726632017-05-12 14:09:46 -0400506 DRAW_OP_TEST_ENTRY(GrDrawAtlasOp),
Brian Salomon17726632017-05-12 14:09:46 -0400507 DRAW_OP_TEST_ENTRY(SmallPathOp),
508 DRAW_OP_TEST_ENTRY(TesselatingPathOp),
509 DRAW_OP_TEST_ENTRY(TextBlobOp),
510 DRAW_OP_TEST_ENTRY(VerticesOp)
511 };
512
513 using MakeDrawOpFn = std::unique_ptr<GrDrawOp>(GrPaint&&, SkRandom*, GrContext*, GrFSAAType);
514 static constexpr MakeDrawOpFn* gFactories[] = {
Brian Salomonbaaf4392017-06-15 09:59:23 -0400515 DRAW_OP_TEST_ENTRY(AAFillRectOp),
516 DRAW_OP_TEST_ENTRY(AAStrokeRectOp),
Brian Salomon05441c42017-05-15 16:45:49 -0400517 DRAW_OP_TEST_ENTRY(CircleOp),
518 DRAW_OP_TEST_ENTRY(DIEllipseOp),
519 DRAW_OP_TEST_ENTRY(EllipseOp),
Brian Salomon17726632017-05-12 14:09:46 -0400520 DRAW_OP_TEST_ENTRY(NonAAFillRectOp),
Brian Salomonbaaf4392017-06-15 09:59:23 -0400521 DRAW_OP_TEST_ENTRY(NonAAStrokeRectOp),
Brian Salomon05441c42017-05-15 16:45:49 -0400522 DRAW_OP_TEST_ENTRY(RRectOp),
Brian Salomon17726632017-05-12 14:09:46 -0400523 };
524
525 static constexpr size_t kTotal = SK_ARRAY_COUNT(gLegacyFactories) + SK_ARRAY_COUNT(gFactories);
526
527 uint32_t index = random->nextULessThan(static_cast<uint32_t>(kTotal));
528 if (index < SK_ARRAY_COUNT(gLegacyFactories)) {
529 const GrUserStencilSettings* uss = GrGetRandomStencil(random, context);
530 // We don't use kHW because we will hit an assertion if the render target is not
531 // multisampled
532 static constexpr GrAAType kAATypes[] = {GrAAType::kNone, GrAAType::kCoverage};
533 GrAAType aaType = kAATypes[random->nextULessThan(SK_ARRAY_COUNT(kAATypes))];
534 bool snapToCenters = random->nextBool();
535 auto op = gLegacyFactories[index](random, context);
536 SkASSERT(op);
537 renderTargetContext->priv().testingOnly_addLegacyMeshDrawOp(
538 std::move(paint), aaType, std::move(op), uss, snapToCenters);
539 } else {
540 auto op = gFactories[index - SK_ARRAY_COUNT(gLegacyFactories)](
541 std::move(paint), random, context, renderTargetContext->fsaaType());
542 SkASSERT(op);
543 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
544 }
545}