blob: c410874fd9113246330f39cf9c73a1ac1fe94d3e [file] [log] [blame]
bsalomon@google.com27847de2011-02-22 20:59:41 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 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.
bsalomon@google.com27847de2011-02-22 20:59:41 +00006 */
7
Greg Daniel51316782017-08-02 15:10:09 +00008#include "GrBackendSemaphore.h"
bsalomon@google.com1fadb202011-12-12 16:10:08 +00009#include "GrContext.h"
Brian Salomonc65aec92017-03-09 09:03:58 -050010#include "GrClip.h"
bsalomon682c2692015-05-22 14:01:46 -070011#include "GrContextOptions.h"
Brian Salomonc65aec92017-03-09 09:03:58 -050012#include "GrContextPriv.h"
robertphillips77a2e522015-10-17 07:43:27 -070013#include "GrDrawingManager.h"
Robert Phillips646e4292017-06-13 12:44:56 -040014#include "GrGpu.h"
Brian Osman11052242016-10-27 14:47:55 -040015#include "GrRenderTargetContext.h"
Brian Salomonc65aec92017-03-09 09:03:58 -050016#include "GrRenderTargetProxy.h"
bsalomon0ea80f42015-02-11 10:49:59 -080017#include "GrResourceCache.h"
bsalomond309e7a2015-04-30 14:18:54 -070018#include "GrResourceProvider.h"
Greg Danield85f97d2017-03-07 13:37:21 -050019#include "GrSemaphore.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000020#include "GrSoftwarePathRenderer.h"
Brian Osman45580d32016-11-23 09:37:01 -050021#include "GrSurfaceContext.h"
bsalomonafbf2d62014-09-30 12:18:44 -070022#include "GrSurfacePriv.h"
Robert Phillips757914d2017-01-25 15:48:30 -050023#include "GrSurfaceProxyPriv.h"
Robert Phillips646e4292017-06-13 12:44:56 -040024#include "GrTexture.h"
Brian Osman45580d32016-11-23 09:37:01 -050025#include "GrTextureContext.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040026#include "GrTracing.h"
Matt Sarett485c4992017-02-14 14:18:27 -050027#include "SkConvertPixels.h"
Brian Osman3b655982017-03-07 16:58:08 -050028#include "SkGr.h"
Brian Osman71a18892017-08-10 10:23:25 -040029#include "SkJSONWriter.h"
Brian Osman51279982017-08-23 10:12:00 -040030#include "SkMakeUnique.h"
31#include "SkTaskGroup.h"
Matt Sarettc7b29082017-02-09 16:22:39 -050032#include "SkUnPreMultiplyPriv.h"
joshualitt5478d422014-11-14 16:00:38 -080033#include "effects/GrConfigConversionEffect.h"
joshualitte8042922015-12-11 06:11:21 -080034#include "text/GrTextBlobCache.h"
joshualitt5478d422014-11-14 16:00:38 -080035
Greg Daniel02611d92017-07-25 10:05:01 -040036#include "gl/GrGLGpu.h"
37#include "mock/GrMockGpu.h"
Greg Danielb76a72a2017-07-13 15:07:54 -040038#ifdef SK_METAL
39#include "mtl/GrMtlTrampoline.h"
40#endif
Robert Phillipse42edcc2017-12-13 11:50:22 -050041#include "ddl/GrDDLGpu.h"
Greg Daniel02611d92017-07-25 10:05:01 -040042#ifdef SK_VULKAN
43#include "vk/GrVkGpu.h"
44#endif
Greg Danielb76a72a2017-07-13 15:07:54 -040045
Robert Phillipse78b7252017-04-06 07:59:41 -040046#define ASSERT_OWNED_PROXY(P) \
47SkASSERT(!(P) || !((P)->priv().peekTexture()) || (P)->priv().peekTexture()->getContext() == this)
Robert Phillips7ee385e2017-03-30 08:02:11 -040048#define ASSERT_OWNED_PROXY_PRIV(P) \
49SkASSERT(!(P) || !((P)->priv().peekTexture()) || (P)->priv().peekTexture()->getContext() == fContext)
50
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000051#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
joshualitt1de610a2016-01-06 08:26:09 -080052#define ASSERT_SINGLE_OWNER \
53 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fSingleOwner);)
robertphillips4fd74ae2016-08-03 14:26:53 -070054#define ASSERT_SINGLE_OWNER_PRIV \
55 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fContext->fSingleOwner);)
robertphillips7761d612016-05-16 09:14:53 -070056#define RETURN_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return; }
Robert Phillips7ee385e2017-03-30 08:02:11 -040057#define RETURN_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return; }
robertphillips7761d612016-05-16 09:14:53 -070058#define RETURN_FALSE_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return false; }
Robert Phillipse78b7252017-04-06 07:59:41 -040059#define RETURN_FALSE_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return false; }
robertphillips7761d612016-05-16 09:14:53 -070060#define RETURN_NULL_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return nullptr; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000061
robertphillipsea461502015-05-26 11:38:03 -070062////////////////////////////////////////////////////////////////////////////////
63
bsalomon682c2692015-05-22 14:01:46 -070064GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext) {
65 GrContextOptions defaultOptions;
66 return Create(backend, backendContext, defaultOptions);
67}
bsalomonf28cff72015-05-22 12:25:41 -070068
bsalomon682c2692015-05-22 14:01:46 -070069GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
70 const GrContextOptions& options) {
bsalomon682c2692015-05-22 14:01:46 -070071
Robert Phillipse42edcc2017-12-13 11:50:22 -050072 sk_sp<GrContext> context(new GrContext(backend));
73
74 context->fGpu = GrGpu::Make(backend, backendContext, options, context.get());
75 if (!context->fGpu) {
halcanary96fcdcc2015-08-27 07:41:13 -070076 return nullptr;
bsalomon@google.com27847de2011-02-22 20:59:41 +000077 }
Robert Phillipse42edcc2017-12-13 11:50:22 -050078
79 if (!context->init(options)) {
80 return nullptr;
81 }
82
Brian Salomon91a3e522017-06-23 10:58:19 -040083 return context.release();
bsalomon@google.com27847de2011-02-22 20:59:41 +000084}
85
Brian Salomon384fab42017-12-07 12:33:05 -050086sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface) {
Greg Daniel02611d92017-07-25 10:05:01 -040087 GrContextOptions defaultOptions;
Brian Salomon384fab42017-12-07 12:33:05 -050088 return MakeGL(std::move(interface), defaultOptions);
Greg Daniel02611d92017-07-25 10:05:01 -040089}
90
Brian Salomon384fab42017-12-07 12:33:05 -050091sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface,
Greg Daniel02611d92017-07-25 10:05:01 -040092 const GrContextOptions& options) {
Robert Phillipse42edcc2017-12-13 11:50:22 -050093 sk_sp<GrContext> context(new GrContext(kOpenGL_GrBackend));
94
Brian Salomon384fab42017-12-07 12:33:05 -050095 context->fGpu = GrGLGpu::Make(std::move(interface), options, context.get());
Greg Daniel02611d92017-07-25 10:05:01 -040096 if (!context->fGpu) {
97 return nullptr;
98 }
Greg Daniel02611d92017-07-25 10:05:01 -040099 if (!context->init(options)) {
100 return nullptr;
101 }
102 return context;
103}
104
Brian Salomon990014d2017-12-07 16:23:39 -0500105sk_sp<GrContext> GrContext::MakeGL(const GrGLInterface* interface) {
106 return MakeGL(sk_ref_sp(interface));
107}
108
109sk_sp<GrContext> GrContext::MakeGL(const GrGLInterface* interface,
110 const GrContextOptions& options) {
111 return MakeGL(sk_ref_sp(interface), options);
112}
113
Greg Daniel02611d92017-07-25 10:05:01 -0400114sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions) {
115 GrContextOptions defaultOptions;
116 return MakeMock(mockOptions, defaultOptions);
117}
118
119sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions,
120 const GrContextOptions& options) {
Robert Phillipse42edcc2017-12-13 11:50:22 -0500121 sk_sp<GrContext> context(new GrContext(kMock_GrBackend));
122
Brian Salomon384fab42017-12-07 12:33:05 -0500123 context->fGpu = GrMockGpu::Make(mockOptions, options, context.get());
Greg Daniel02611d92017-07-25 10:05:01 -0400124 if (!context->fGpu) {
125 return nullptr;
126 }
Greg Daniel02611d92017-07-25 10:05:01 -0400127 if (!context->init(options)) {
128 return nullptr;
129 }
130 return context;
131}
132
133#ifdef SK_VULKAN
Brian Salomon384fab42017-12-07 12:33:05 -0500134sk_sp<GrContext> GrContext::MakeVulkan(sk_sp<const GrVkBackendContext> backendContext) {
Greg Daniel02611d92017-07-25 10:05:01 -0400135 GrContextOptions defaultOptions;
Brian Salomon384fab42017-12-07 12:33:05 -0500136 return MakeVulkan(std::move(backendContext), defaultOptions);
Greg Daniel02611d92017-07-25 10:05:01 -0400137}
138
Brian Salomon384fab42017-12-07 12:33:05 -0500139sk_sp<GrContext> GrContext::MakeVulkan(sk_sp<const GrVkBackendContext> backendContext,
Greg Daniel02611d92017-07-25 10:05:01 -0400140 const GrContextOptions& options) {
Robert Phillipse42edcc2017-12-13 11:50:22 -0500141 sk_sp<GrContext> context(new GrContext(kVulkan_GrBackend));
142
Brian Salomon384fab42017-12-07 12:33:05 -0500143 context->fGpu = GrVkGpu::Make(std::move(backendContext), options, context.get());
Greg Daniel02611d92017-07-25 10:05:01 -0400144 if (!context->fGpu) {
145 return nullptr;
146 }
Greg Daniel02611d92017-07-25 10:05:01 -0400147 if (!context->init(options)) {
148 return nullptr;
149 }
150 return context;
151}
152#endif
153
Greg Danielb76a72a2017-07-13 15:07:54 -0400154#ifdef SK_METAL
Greg Daniel02611d92017-07-25 10:05:01 -0400155sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue) {
156 GrContextOptions defaultOptions;
157 return MakeMetal(device, queue, defaultOptions);
158}
159
Greg Danielb76a72a2017-07-13 15:07:54 -0400160sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) {
Robert Phillipse42edcc2017-12-13 11:50:22 -0500161 sk_sp<GrContext> context(new GrContext(kMetal_GrBackend));
162
Brian Salomon384fab42017-12-07 12:33:05 -0500163 context->fGpu = GrMtlTrampoline::MakeGpu(context.get(), options, device, queue);
Greg Danielb76a72a2017-07-13 15:07:54 -0400164 if (!context->fGpu) {
165 return nullptr;
166 }
Greg Danielb76a72a2017-07-13 15:07:54 -0400167 if (!context->init(options)) {
168 return nullptr;
169 }
170 return context;
171}
172#endif
173
joshualitt0acd0d32015-05-07 08:23:19 -0700174static int32_t gNextID = 1;
175static int32_t next_id() {
176 int32_t id;
177 do {
178 id = sk_atomic_inc(&gNextID);
179 } while (id == SK_InvalidGenID);
180 return id;
181}
182
Robert Phillipse42edcc2017-12-13 11:50:22 -0500183sk_sp<GrContext> GrContextPriv::MakeDDL(GrContextThreadSafeProxy* proxy) {
184 sk_sp<GrContext> context(new GrContext(proxy));
185
186 context->fGpu = GrDDLGpu::Make(context.get(), proxy->fCaps);
187 if (!context->fGpu) {
188 return nullptr;
189 }
190 if (!context->init(proxy->fOptions)) {
191 return nullptr;
192 }
193 return context;
194}
195
196GrContext::GrContext(GrBackend backend)
197 : fUniqueID(next_id())
198 , fBackend(backend) {
halcanary96fcdcc2015-08-27 07:41:13 -0700199 fResourceCache = nullptr;
200 fResourceProvider = nullptr;
Brian Salomonf856fd12016-12-16 14:24:34 -0500201 fAtlasGlyphCache = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000202}
203
Robert Phillipse42edcc2017-12-13 11:50:22 -0500204GrContext::GrContext(GrContextThreadSafeProxy* proxy)
205 : fUniqueID(proxy->fContextUniqueID)
206 , fBackend(proxy->fBackend) {
207 fResourceCache = nullptr;
208 fResourceProvider = nullptr;
209 fAtlasGlyphCache = nullptr;
Greg Danielb76a72a2017-07-13 15:07:54 -0400210}
joshualitt1de610a2016-01-06 08:26:09 -0800211
Greg Danielb76a72a2017-07-13 15:07:54 -0400212bool GrContext::init(const GrContextOptions& options) {
213 ASSERT_SINGLE_OWNER
Robert Phillipse42edcc2017-12-13 11:50:22 -0500214 fCaps = fGpu->refCaps();
Brian Salomoneace8cd2017-12-08 13:11:11 -0500215 fResourceCache = new GrResourceCache(fCaps.get(), fUniqueID);
Brian Salomon384fab42017-12-07 12:33:05 -0500216 fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, &fSingleOwner);
Robert Phillipse42edcc2017-12-13 11:50:22 -0500217 // DDL TODO: we need to think through how the task group & persistent cache
218 // get passed on to/shared between all the DDLRecorders created with this context.
219 fThreadSafeProxy.reset(new GrContextThreadSafeProxy(fCaps, this->uniqueID(), fBackend,
220 options));
commit-bot@chromium.org1836d332013-07-16 22:55:03 +0000221
Brian Osman46da1cc2017-02-14 14:15:48 -0500222 fDisableGpuYUVConversion = options.fDisableGpuYUVConversion;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000223 fDidTestPMConversions = false;
224
bsalomon6b2552f2016-09-15 13:50:26 -0700225 GrPathRendererChain::Options prcOptions;
bsalomon39ef7fb2016-09-21 11:16:05 -0700226 prcOptions.fAllowPathMaskCaching = options.fAllowPathMaskCaching;
Brian Osman195c05b2017-08-30 15:14:04 -0400227#if GR_TEST_UTILS
csmartdalton008b9d82017-02-22 12:00:42 -0700228 prcOptions.fGpuPathRenderers = options.fGpuPathRenderers;
Brian Osman195c05b2017-08-30 15:14:04 -0400229#endif
Brian Osmanb350ae22017-08-29 16:15:39 -0400230 if (options.fDisableDistanceFieldPaths) {
Brian Osman195c05b2017-08-30 15:14:04 -0400231 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
Brian Osmanb350ae22017-08-29 16:15:39 -0400232 }
Brian Salomonaf597482017-11-07 16:23:34 -0500233
234 GrAtlasTextContext::Options atlasTextContextOptions;
235 atlasTextContextOptions.fMaxDistanceFieldFontSize = options.fGlyphsAsPathsFontSize;
236 atlasTextContextOptions.fMinDistanceFieldFontSize = options.fMinDistanceFieldFontSize;
237
238 fDrawingManager.reset(
239 new GrDrawingManager(this, prcOptions, atlasTextContextOptions, &fSingleOwner));
joshualitt7c3a2f82015-03-31 13:32:05 -0700240
Brian Salomon9f545bc2017-11-06 10:36:57 -0500241 GrDrawOpAtlas::AllowMultitexturing allowMultitexturing;
Jim Van Verthc02cb8a2017-11-20 22:11:30 +0000242 switch (options.fAllowMultipleGlyphCacheTextures) {
243 case GrContextOptions::Enable::kDefault:
244#ifdef SK_BUILD_FOR_IOS
245 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
246#else
247 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
248#endif
249 break;
250 case GrContextOptions::Enable::kNo:
251 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
252 break;
253 case GrContextOptions::Enable::kYes:
254 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
255 break;
Brian Salomon9f545bc2017-11-06 10:36:57 -0500256 }
257 fAtlasGlyphCache = new GrAtlasGlyphCache(this, options.fGlyphCacheTextureMaximumBytes,
258 allowMultitexturing);
Jim Van Verth106b5c42017-09-26 12:45:29 -0400259 this->contextPriv().addOnFlushCallbackObject(fAtlasGlyphCache);
joshualittb7133be2015-04-08 09:08:31 -0700260
Jim Van Verth474d6872017-12-14 13:00:05 -0500261 fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB, this, this->uniqueID()));
Brian Salomon91a3e522017-06-23 10:58:19 -0400262
Brian Osman51279982017-08-23 10:12:00 -0400263 if (options.fExecutor) {
264 fTaskGroup = skstd::make_unique<SkTaskGroup>(*options.fExecutor);
265 }
266
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400267 fPersistentCache = options.fPersistentCache;
268
Brian Salomon91a3e522017-06-23 10:58:19 -0400269 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000270}
271
bsalomon@google.com27847de2011-02-22 20:59:41 +0000272GrContext::~GrContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800273 ASSERT_SINGLE_OWNER
274
robertphillipsea461502015-05-26 11:38:03 -0700275 if (!fGpu) {
bsalomon76228632015-05-29 08:02:10 -0700276 SkASSERT(!fCaps);
bsalomon@google.com733c0622013-04-24 17:59:32 +0000277 return;
278 }
279
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000280 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000281
robertphillips77a2e522015-10-17 07:43:27 -0700282 fDrawingManager->cleanup();
robertphillips2334fb62015-06-17 05:43:33 -0700283
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000284 for (int i = 0; i < fCleanUpData.count(); ++i) {
285 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
286 }
287
halcanary385fe4d2015-08-26 13:07:48 -0700288 delete fResourceProvider;
289 delete fResourceCache;
Brian Salomonf856fd12016-12-16 14:24:34 -0500290 delete fAtlasGlyphCache;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000291}
292
bungeman6bd52842016-10-27 09:30:08 -0700293sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
bungeman6bd52842016-10-27 09:30:08 -0700294 return fThreadSafeProxy;
bsalomon41b952c2016-03-11 06:46:33 -0800295}
296
bsalomon2354f842014-07-28 13:48:36 -0700297void GrContext::abandonContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800298 ASSERT_SINGLE_OWNER
299
bsalomond309e7a2015-04-30 14:18:54 -0700300 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800301
302 // Need to abandon the drawing manager first so all the render targets
303 // will be released/forgotten before they too are abandoned.
304 fDrawingManager->abandon();
305
bsalomon@google.com205d4602011-04-25 12:43:45 +0000306 // abandon first to so destructors
307 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800308 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700309
bsalomon6e2aad42016-04-01 11:54:31 -0700310 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
311
Brian Salomonf856fd12016-12-16 14:24:34 -0500312 fAtlasGlyphCache->freeAll();
bsalomon6e2aad42016-04-01 11:54:31 -0700313 fTextBlobCache->freeAll();
314}
315
316void GrContext::releaseResourcesAndAbandonContext() {
317 ASSERT_SINGLE_OWNER
318
319 fResourceProvider->abandon();
320
321 // Need to abandon the drawing manager first so all the render targets
322 // will be released/forgotten before they too are abandoned.
323 fDrawingManager->abandon();
324
325 // Release all resources in the backend 3D API.
326 fResourceCache->releaseAll();
327
328 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000329
Brian Salomonf856fd12016-12-16 14:24:34 -0500330 fAtlasGlyphCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700331 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000332}
333
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000334void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800335 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000336 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000337}
338
339void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800340 ASSERT_SINGLE_OWNER
341
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000342 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000343
Brian Salomonf856fd12016-12-16 14:24:34 -0500344 fAtlasGlyphCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700345
346 fDrawingManager->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700347
348 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000349}
350
Jim Van Verth76d917c2017-12-13 09:26:37 -0500351void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) {
Brian Salomon5e150852017-03-22 14:53:13 -0400352 ASSERT_SINGLE_OWNER
Jim Van Verth76d917c2017-12-13 09:26:37 -0500353 fResourceCache->purgeAsNeeded();
354 fResourceCache->purgeResourcesNotUsedSince(GrStdSteadyClock::now() - msNotUsed);
355
356 fTextBlobCache->purgeStaleBlobs();
Brian Salomon5e150852017-03-22 14:53:13 -0400357}
358
Derek Sollenberger5480a182017-05-25 16:43:59 -0400359void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
360 ASSERT_SINGLE_OWNER
361 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
362}
363
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000364void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800365 ASSERT_SINGLE_OWNER
366
bsalomon71cb0c22014-11-14 12:10:14 -0800367 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800368 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800369 }
370 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800371 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800372 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000373}
374
Derek Sollenbergeree479142017-05-24 11:41:33 -0400375size_t GrContext::getResourceCachePurgeableBytes() const {
376 ASSERT_SINGLE_OWNER
377 return fResourceCache->getPurgeableBytes();
378}
379
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000380////////////////////////////////////////////////////////////////////////////////
381
joshualitt0db6dfa2015-04-10 07:01:30 -0700382void GrContext::TextBlobCacheOverBudgetCB(void* data) {
383 SkASSERT(data);
Brian Osman11052242016-10-27 14:47:55 -0400384 // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on
385 // GrRenderTargetContext to perform a necessary flush. The solution is to move drawText calls
386 // to below the GrContext level, but this is not trivial because they call drawPath on
387 // SkGpuDevice.
joshualitt0db6dfa2015-04-10 07:01:30 -0700388 GrContext* context = reinterpret_cast<GrContext*>(data);
389 context->flush();
390}
391
bsalomon@google.com27847de2011-02-22 20:59:41 +0000392////////////////////////////////////////////////////////////////////////////////
393
bsalomonb77a9072016-09-07 10:02:04 -0700394void GrContext::flush() {
joshualitt1de610a2016-01-06 08:26:09 -0800395 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700396 RETURN_IF_ABANDONED
Robert Phillips7ee385e2017-03-30 08:02:11 -0400397
398 fDrawingManager->flush(nullptr);
399}
400
Greg Daniel51316782017-08-02 15:10:09 +0000401GrSemaphoresSubmitted GrContext::flushAndSignalSemaphores(int numSemaphores,
402 GrBackendSemaphore signalSemaphores[]) {
403 ASSERT_SINGLE_OWNER
404 if (fDrawingManager->wasAbandoned()) { return GrSemaphoresSubmitted::kNo; }
405
406 return fDrawingManager->flush(nullptr, numSemaphores, signalSemaphores);
407}
408
Robert Phillips7ee385e2017-03-30 08:02:11 -0400409void GrContextPriv::flush(GrSurfaceProxy* proxy) {
410 ASSERT_SINGLE_OWNER_PRIV
411 RETURN_IF_ABANDONED_PRIV
412 ASSERT_OWNED_PROXY_PRIV(proxy);
413
414 fContext->fDrawingManager->flush(proxy);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000415}
416
bsalomon81beccc2014-10-13 12:32:55 -0700417bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
418 const void* inPixels, size_t outRowBytes, void* outPixels) {
Matt Sarettc7b29082017-02-09 16:22:39 -0500419 SkColorType colorType;
420 if (!GrPixelConfigToColorType(srcConfig, &colorType) ||
421 4 != SkColorTypeBytesPerPixel(colorType))
422 {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000423 return false;
424 }
bsalomon81beccc2014-10-13 12:32:55 -0700425
Matt Sarettc7b29082017-02-09 16:22:39 -0500426 for (int y = 0; y < height; y++) {
427 SkOpts::RGBA_to_rgbA((uint32_t*) outPixels, inPixels, width);
428 outPixels = SkTAddOffset<void>(outPixels, outRowBytes);
429 inPixels = SkTAddOffset<const void>(inPixels, inRowBytes);
430 }
bsalomon81beccc2014-10-13 12:32:55 -0700431
Matt Sarettc7b29082017-02-09 16:22:39 -0500432 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000433}
434
Brian Osmand2ca59a2017-04-13 14:03:57 -0400435static bool valid_premul_config(GrPixelConfig config) {
Brian Osmance425512017-03-22 14:37:50 -0400436 return GrPixelConfigIs8888Unorm(config) || kRGBA_half_GrPixelConfig == config;
437}
438
Brian Osmand2ca59a2017-04-13 14:03:57 -0400439static bool valid_pixel_conversion(GrPixelConfig srcConfig, GrPixelConfig dstConfig,
440 bool premulConversion) {
441 // We don't allow conversion between integer configs and float/fixed configs.
442 if (GrPixelConfigIsSint(srcConfig) != GrPixelConfigIsSint(dstConfig)) {
443 return false;
444 }
445
446 // We only allow premul <-> unpremul conversions for some formats
447 if (premulConversion && (!valid_premul_config(srcConfig) || !valid_premul_config(dstConfig))) {
448 return false;
449 }
450
451 return true;
452}
453
Brian Osman409e74f2017-04-17 11:48:28 -0400454static bool pm_upm_must_round_trip(GrPixelConfig config, SkColorSpace* colorSpace) {
455 return !colorSpace &&
456 (kRGBA_8888_GrPixelConfig == config || kBGRA_8888_GrPixelConfig == config);
457}
458
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400459bool GrContextPriv::writeSurfacePixels(GrSurfaceContext* dst,
Robert Phillipse78b7252017-04-06 07:59:41 -0400460 int left, int top, int width, int height,
461 GrPixelConfig srcConfig, SkColorSpace* srcColorSpace,
462 const void* buffer, size_t rowBytes,
463 uint32_t pixelOpsFlags) {
Brian Osmanb62ea222016-12-22 11:12:16 -0500464 // TODO: Color space conversion
465
Robert Phillipse78b7252017-04-06 07:59:41 -0400466 ASSERT_SINGLE_OWNER_PRIV
467 RETURN_FALSE_IF_ABANDONED_PRIV
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400468 SkASSERT(dst);
469 ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy());
Brian Salomondcbb9d92017-07-19 10:53:20 -0400470 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "writeSurfacePixels", fContext);
bsalomon6c6f6582015-09-10 08:12:46 -0700471
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400472 if (!dst->asSurfaceProxy()->instantiate(fContext->resourceProvider())) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400473 return false;
474 }
475
Robert Phillips16d8ec62017-07-27 16:16:25 -0400476 GrSurfaceProxy* dstProxy = dst->asSurfaceProxy();
477 GrSurface* dstSurface = dstProxy->priv().peekSurface();
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400478
Brian Osmand2ca59a2017-04-13 14:03:57 -0400479 // The src is unpremul but the dst is premul -> premul the src before or as part of the write
Brian Osman409e74f2017-04-17 11:48:28 -0400480 const bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
Robert Phillips16d8ec62017-07-27 16:16:25 -0400481 if (!valid_pixel_conversion(srcConfig, dstProxy->config(), premul)) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400482 return false;
483 }
484
Brian Osman409e74f2017-04-17 11:48:28 -0400485 // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data,
486 // without any color spaces attached, and the caller wants us to premul.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400487 bool useConfigConversionEffect =
Brian Salomonf3569f02017-10-24 12:52:33 -0400488 premul && pm_upm_must_round_trip(srcConfig, srcColorSpace) &&
489 pm_upm_must_round_trip(dstProxy->config(), dst->colorSpaceInfo().colorSpace());
Brian Osman409e74f2017-04-17 11:48:28 -0400490
491 // Are we going to try to premul as part of a draw? For the non-legacy case, we always allow
492 // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly.
493 bool premulOnGpu = premul &&
494 (!useConfigConversionEffect || fContext->validPMUPMConversionExists());
bsalomon81beccc2014-10-13 12:32:55 -0700495
bsalomone8d21e82015-07-16 08:23:13 -0700496 // Trim the params here so that if we wind up making a temporary surface it can be as small as
bsalomonf0674512015-07-28 13:26:15 -0700497 // necessary and because GrGpu::getWritePixelsInfo requires it.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400498 if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(),
bsalomone8d21e82015-07-16 08:23:13 -0700499 GrBytesPerPixel(srcConfig), &left, &top, &width,
500 &height, &buffer, &rowBytes)) {
501 return false;
502 }
503
Brian Osman409e74f2017-04-17 11:48:28 -0400504 GrGpu::DrawPreference drawPreference = premulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference
505 : GrGpu::kNoDraw_DrawPreference;
bsalomonf0674512015-07-28 13:26:15 -0700506 GrGpu::WritePixelTempDrawInfo tempDrawInfo;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400507 if (!fContext->fGpu->getWritePixelsInfo(dstSurface, dstProxy->origin(), width, height,
Robert Phillips16d8ec62017-07-27 16:16:25 -0400508 srcConfig, &drawPreference, &tempDrawInfo)) {
bsalomonf0674512015-07-28 13:26:15 -0700509 return false;
510 }
511
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400512 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && dstSurface->surfacePriv().hasPendingIO()) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400513 this->flush(nullptr); // MDB TODO: tighten this
bsalomonf0674512015-07-28 13:26:15 -0700514 }
515
Robert Phillips2f493142017-03-02 18:18:38 -0500516 sk_sp<GrTextureProxy> tempProxy;
bsalomonf0674512015-07-28 13:26:15 -0700517 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400518 tempProxy = GrSurfaceProxy::MakeDeferred(fContext->resourceProvider(),
Robert Phillips2f493142017-03-02 18:18:38 -0500519 tempDrawInfo.fTempSurfaceDesc,
520 SkBackingFit::kApprox,
521 SkBudgeted::kYes);
522 if (!tempProxy && GrGpu::kRequireDraw_DrawPreference == drawPreference) {
bsalomonf0674512015-07-28 13:26:15 -0700523 return false;
524 }
525 }
526
527 // temp buffer for doing sw premul conversion, if needed.
528 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
Brian Osman409e74f2017-04-17 11:48:28 -0400529 // We need to do sw premul if we were unable to create a RT for drawing, or if we can't do the
530 // premul on the GPU
531 if (premul && (!tempProxy || !premulOnGpu)) {
532 size_t tmpRowBytes = 4 * width;
533 tmpPixels.reset(width * height);
534 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
535 tmpPixels.get())) {
536 return false;
bsalomonf0674512015-07-28 13:26:15 -0700537 }
Brian Osman409e74f2017-04-17 11:48:28 -0400538 rowBytes = tmpRowBytes;
539 buffer = tmpPixels.get();
bsalomonf0674512015-07-28 13:26:15 -0700540 }
Brian Osman409e74f2017-04-17 11:48:28 -0400541
542 if (tempProxy) {
Brian Osman2240be92017-10-18 13:15:13 -0400543 auto fp = GrSimpleTextureEffect::Make(tempProxy, SkMatrix::I());
Brian Osman409e74f2017-04-17 11:48:28 -0400544 if (premulOnGpu) {
545 fp = fContext->createUPMToPMEffect(std::move(fp), useConfigConversionEffect);
bsalomon81beccc2014-10-13 12:32:55 -0700546 }
Brian Osman409e74f2017-04-17 11:48:28 -0400547 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400548 if (!fp) {
549 return false;
550 }
Brian Osman409e74f2017-04-17 11:48:28 -0400551
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400552 if (!tempProxy->instantiate(fContext->resourceProvider())) {
Brian Osman409e74f2017-04-17 11:48:28 -0400553 return false;
554 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400555 GrTexture* texture = tempProxy->priv().peekTexture();
Robert Phillips7bbbf622017-10-17 07:36:59 -0400556
557 if (tempProxy->priv().hasPendingIO()) {
558 this->flush(tempProxy.get());
559 }
560
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400561 if (!fContext->fGpu->writePixels(texture, tempProxy->origin(), 0, 0, width, height,
562 tempDrawInfo.fWriteConfig, buffer, rowBytes)) {
Brian Osman409e74f2017-04-17 11:48:28 -0400563 return false;
564 }
Robert Phillips7bbbf622017-10-17 07:36:59 -0400565 tempProxy = nullptr;
566
Brian Osman409e74f2017-04-17 11:48:28 -0400567 SkMatrix matrix;
568 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400569 GrRenderTargetContext* renderTargetContext = dst->asRenderTargetContext();
Brian Osman409e74f2017-04-17 11:48:28 -0400570 if (!renderTargetContext) {
571 return false;
572 }
573 GrPaint paint;
574 paint.addColorFragmentProcessor(std::move(fp));
575 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonf3569f02017-10-24 12:52:33 -0400576 paint.setAllowSRGBInputs(dst->colorSpaceInfo().isGammaCorrect() ||
577 GrPixelConfigIsSRGB(dst->colorSpaceInfo().config()));
Brian Osman409e74f2017-04-17 11:48:28 -0400578 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
579 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, matrix, rect,
580 nullptr);
581
582 if (kFlushWrites_PixelOp & pixelOpsFlags) {
583 this->flushSurfaceWrites(renderTargetContext->asRenderTargetProxy());
584 }
585 } else {
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400586 return fContext->fGpu->writePixels(dstSurface, dstProxy->origin(), left, top, width,
587 height, srcConfig, buffer, rowBytes);
bsalomon81beccc2014-10-13 12:32:55 -0700588 }
bsalomon81beccc2014-10-13 12:32:55 -0700589 return true;
590}
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000591
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400592bool GrContextPriv::readSurfacePixels(GrSurfaceContext* src,
Robert Phillipse78b7252017-04-06 07:59:41 -0400593 int left, int top, int width, int height,
594 GrPixelConfig dstConfig, SkColorSpace* dstColorSpace,
595 void* buffer, size_t rowBytes, uint32_t flags) {
Brian Osmanb62ea222016-12-22 11:12:16 -0500596 // TODO: Color space conversion
597
Robert Phillipse78b7252017-04-06 07:59:41 -0400598 ASSERT_SINGLE_OWNER_PRIV
599 RETURN_FALSE_IF_ABANDONED_PRIV
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400600 SkASSERT(src);
601 ASSERT_OWNED_PROXY_PRIV(src->asSurfaceProxy());
Brian Salomondcbb9d92017-07-19 10:53:20 -0400602 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "readSurfacePixels", fContext);
bsalomon32ab2602015-09-09 18:57:49 -0700603
Robert Phillipse78b7252017-04-06 07:59:41 -0400604 // MDB TODO: delay this instantiation until later in the method
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400605 if (!src->asSurfaceProxy()->instantiate(fContext->resourceProvider())) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400606 return false;
607 }
608
Robert Phillips16d8ec62017-07-27 16:16:25 -0400609 GrSurfaceProxy* srcProxy = src->asSurfaceProxy();
610 GrSurface* srcSurface = srcProxy->priv().peekSurface();
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400611
Brian Osmand2ca59a2017-04-13 14:03:57 -0400612 // The src is premul but the dst is unpremul -> unpremul the src after or as part of the read
613 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
Robert Phillips16d8ec62017-07-27 16:16:25 -0400614 if (!valid_pixel_conversion(srcProxy->config(), dstConfig, unpremul)) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400615 return false;
616 }
617
Brian Osman409e74f2017-04-17 11:48:28 -0400618 // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data,
619 // without any color spaces attached, and the caller wants us to unpremul.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400620 bool useConfigConversionEffect =
Brian Salomonf3569f02017-10-24 12:52:33 -0400621 unpremul &&
622 pm_upm_must_round_trip(srcProxy->config(), src->colorSpaceInfo().colorSpace()) &&
623 pm_upm_must_round_trip(dstConfig, dstColorSpace);
Brian Osman409e74f2017-04-17 11:48:28 -0400624
625 // Are we going to try to unpremul as part of a draw? For the non-legacy case, we always allow
626 // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly.
627 bool unpremulOnGpu = unpremul &&
628 (!useConfigConversionEffect || fContext->validPMUPMConversionExists());
bsalomon6c6f6582015-09-10 08:12:46 -0700629
bsalomone8d21e82015-07-16 08:23:13 -0700630 // Adjust the params so that if we wind up using an intermediate surface we've already done
631 // all the trimming and the temporary can be the min size required.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400632 if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(),
bsalomone8d21e82015-07-16 08:23:13 -0700633 GrBytesPerPixel(dstConfig), &left,
634 &top, &width, &height, &buffer, &rowBytes)) {
635 return false;
636 }
637
Brian Osman409e74f2017-04-17 11:48:28 -0400638 GrGpu::DrawPreference drawPreference = unpremulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference
639 : GrGpu::kNoDraw_DrawPreference;
bsalomon39826022015-07-23 08:07:21 -0700640 GrGpu::ReadPixelTempDrawInfo tempDrawInfo;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400641 if (!fContext->fGpu->getReadPixelsInfo(srcSurface, srcProxy->origin(), width, height, rowBytes,
Robert Phillips16d8ec62017-07-27 16:16:25 -0400642 dstConfig, &drawPreference, &tempDrawInfo)) {
bsalomon39826022015-07-23 08:07:21 -0700643 return false;
644 }
bsalomon191bcc02014-11-14 11:31:13 -0800645
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400646 if (!(kDontFlush_PixelOpsFlag & flags) && srcSurface->surfacePriv().hasPendingWrite()) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400647 this->flush(nullptr); // MDB TODO: tighten this
648 }
649
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400650 sk_sp<GrSurfaceProxy> proxyToRead = src->asSurfaceProxyRef();
bsalomon39826022015-07-23 08:07:21 -0700651 bool didTempDraw = false;
652 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomonb117ff12016-07-19 07:24:40 -0700653 if (SkBackingFit::kExact == tempDrawInfo.fTempSurfaceFit) {
bsalomon39826022015-07-23 08:07:21 -0700654 // We only respect this when the entire src is being read. Otherwise we can trigger too
655 // many odd ball texture sizes and trash the cache.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400656 if (width != srcSurface->width() || height != srcSurface->height()) {
bsalomonb117ff12016-07-19 07:24:40 -0700657 tempDrawInfo.fTempSurfaceFit= SkBackingFit::kApprox;
bsalomon39826022015-07-23 08:07:21 -0700658 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000659 }
brianosmandfe4f2e2016-07-21 13:28:36 -0700660 // TODO: Need to decide the semantics of this function for color spaces. Do we support
661 // conversion to a passed-in color space? For now, specifying nullptr means that this
662 // path will do no conversion, so it will match the behavior of the non-draw path.
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400663 sk_sp<GrRenderTargetContext> tempRTC = fContext->makeDeferredRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400664 tempDrawInfo.fTempSurfaceFit,
bsalomonb117ff12016-07-19 07:24:40 -0700665 tempDrawInfo.fTempSurfaceDesc.fWidth,
666 tempDrawInfo.fTempSurfaceDesc.fHeight,
667 tempDrawInfo.fTempSurfaceDesc.fConfig,
brianosmandfe4f2e2016-07-21 13:28:36 -0700668 nullptr,
bsalomonb117ff12016-07-19 07:24:40 -0700669 tempDrawInfo.fTempSurfaceDesc.fSampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -0400670 GrMipMapped::kNo,
bsalomonb117ff12016-07-19 07:24:40 -0700671 tempDrawInfo.fTempSurfaceDesc.fOrigin);
Brian Osman693a5402016-10-27 15:13:22 -0400672 if (tempRTC) {
Robert Phillips67c18d62017-01-20 12:44:06 -0500673 SkMatrix textureMatrix = SkMatrix::MakeTrans(SkIntToScalar(left), SkIntToScalar(top));
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400674 sk_sp<GrTextureProxy> proxy = src->asTextureProxyRef();
Brian Osman2240be92017-10-18 13:15:13 -0400675 auto fp = GrSimpleTextureEffect::Make(std::move(proxy), textureMatrix);
Brian Osman409e74f2017-04-17 11:48:28 -0400676 if (unpremulOnGpu) {
677 fp = fContext->createPMToUPMEffect(std::move(fp), useConfigConversionEffect);
678 // We no longer need to do this on CPU after the read back.
679 unpremul = false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000680 }
Brian Osman409e74f2017-04-17 11:48:28 -0400681 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400682 if (!fp) {
683 return false;
684 }
Brian Osman60cd57e2017-04-06 10:19:06 -0400685
Brian Osman409e74f2017-04-17 11:48:28 -0400686 GrPaint paint;
687 paint.addColorFragmentProcessor(std::move(fp));
688 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
689 paint.setAllowSRGBInputs(true);
690 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
691 tempRTC->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect,
692 nullptr);
693 proxyToRead = tempRTC->asTextureProxyRef();
694 left = 0;
695 top = 0;
696 didTempDraw = true;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000697 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000698 }
joshualitt5c55fef2014-10-31 14:04:35 -0700699
Robert Phillipse78b7252017-04-06 07:59:41 -0400700 if (!proxyToRead) {
701 return false;
702 }
703
bsalomon39826022015-07-23 08:07:21 -0700704 if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000705 return false;
706 }
bsalomon39826022015-07-23 08:07:21 -0700707 GrPixelConfig configToRead = dstConfig;
708 if (didTempDraw) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400709 this->flushSurfaceWrites(proxyToRead.get());
bsalomon6c9cd552016-01-22 07:17:34 -0800710 configToRead = tempDrawInfo.fReadConfig;
bsalomon39826022015-07-23 08:07:21 -0700711 }
Robert Phillips09dfc472017-09-13 15:25:47 -0400712
713 if (!proxyToRead->instantiate(fContext->resourceProvider())) {
714 return false;
715 }
716
717 GrSurface* surfaceToRead = proxyToRead->priv().peekSurface();
718
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400719 if (!fContext->fGpu->readPixels(surfaceToRead, proxyToRead->origin(),
Robert Phillips16d8ec62017-07-27 16:16:25 -0400720 left, top, width, height, configToRead, buffer, rowBytes)) {
bsalomon39826022015-07-23 08:07:21 -0700721 return false;
722 }
723
724 // Perform umpremul conversion if we weren't able to perform it as a draw.
725 if (unpremul) {
Matt Sarettc7b29082017-02-09 16:22:39 -0500726 SkColorType colorType;
727 if (!GrPixelConfigToColorType(dstConfig, &colorType) ||
728 4 != SkColorTypeBytesPerPixel(colorType))
729 {
reed@google.com7111d462014-03-25 16:20:24 +0000730 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000731 }
reed@google.com7111d462014-03-25 16:20:24 +0000732
Matt Sarettc7b29082017-02-09 16:22:39 -0500733 for (int y = 0; y < height; y++) {
734 SkUnpremultiplyRow<false>((uint32_t*) buffer, (const uint32_t*) buffer, width);
735 buffer = SkTAddOffset<void>(buffer, rowBytes);
736 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000737 }
738 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000739}
740
Robert Phillips7ee385e2017-03-30 08:02:11 -0400741void GrContextPriv::prepareSurfaceForExternalIO(GrSurfaceProxy* proxy) {
742 ASSERT_SINGLE_OWNER_PRIV
743 RETURN_IF_ABANDONED_PRIV
744 SkASSERT(proxy);
745 ASSERT_OWNED_PROXY_PRIV(proxy);
Greg Daniel51316782017-08-02 15:10:09 +0000746 fContext->fDrawingManager->prepareSurfaceForExternalIO(proxy, 0, nullptr);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000747}
748
Robert Phillips7ee385e2017-03-30 08:02:11 -0400749void GrContextPriv::flushSurfaceWrites(GrSurfaceProxy* proxy) {
750 ASSERT_SINGLE_OWNER_PRIV
751 RETURN_IF_ABANDONED_PRIV
752 SkASSERT(proxy);
753 ASSERT_OWNED_PROXY_PRIV(proxy);
754 if (proxy->priv().hasPendingWrite()) {
755 this->flush(proxy);
bsalomonf80bfed2014-10-07 05:56:02 -0700756 }
757}
758
Robert Phillips7ee385e2017-03-30 08:02:11 -0400759void GrContextPriv::flushSurfaceIO(GrSurfaceProxy* proxy) {
760 ASSERT_SINGLE_OWNER_PRIV
761 RETURN_IF_ABANDONED_PRIV
762 SkASSERT(proxy);
763 ASSERT_OWNED_PROXY_PRIV(proxy);
764 if (proxy->priv().hasPendingIO()) {
765 this->flush(proxy);
ajuma95243eb2016-08-24 08:19:02 -0700766 }
767}
768
bsalomon@google.com27847de2011-02-22 20:59:41 +0000769////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000770int GrContext::getRecommendedSampleCount(GrPixelConfig config,
771 SkScalar dpi) const {
joshualitt1de610a2016-01-06 08:26:09 -0800772 ASSERT_SINGLE_OWNER
773
bsalomon76228632015-05-29 08:02:10 -0700774 if (!this->caps()->isConfigRenderable(config, true)) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000775 return 0;
776 }
777 int chosenSampleCount = 0;
jvanverthe9c0fc62015-04-29 11:18:05 -0700778 if (fGpu->caps()->shaderCaps()->pathRenderingSupport()) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000779 if (dpi >= 250.0f) {
780 chosenSampleCount = 4;
781 } else {
782 chosenSampleCount = 16;
783 }
784 }
Greg Daniel81e7bf82017-07-19 14:47:42 -0400785 int supportedSampleCount = fGpu->caps()->getSampleCount(chosenSampleCount, config);
786 return chosenSampleCount <= supportedSampleCount ? supportedSampleCount : 0;
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000787}
788
Robert Phillips2c862492017-01-18 10:08:39 -0500789sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy> proxy,
790 sk_sp<SkColorSpace> colorSpace) {
Brian Osman45580d32016-11-23 09:37:01 -0500791 ASSERT_SINGLE_OWNER_PRIV
792
Brian Osman45580d32016-11-23 09:37:01 -0500793 if (proxy->asRenderTargetProxy()) {
Robert Phillips2c862492017-01-18 10:08:39 -0500794 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
795 std::move(colorSpace), nullptr);
Brian Osman45580d32016-11-23 09:37:01 -0500796 } else {
797 SkASSERT(proxy->asTextureProxy());
Robert Phillips2c862492017-01-18 10:08:39 -0500798 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Brian Osman45580d32016-11-23 09:37:01 -0500799 }
800}
801
Robert Phillipse2f7d182016-12-15 09:23:05 -0500802sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(const GrSurfaceDesc& dstDesc,
Greg Daniel65c7f662017-10-30 13:39:09 -0400803 GrMipMapped mipMapped,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500804 SkBackingFit fit,
805 SkBudgeted isDstBudgeted) {
806
Greg Daniel65c7f662017-10-30 13:39:09 -0400807 sk_sp<GrTextureProxy> proxy;
808 if (GrMipMapped::kNo == mipMapped) {
809 proxy = GrSurfaceProxy::MakeDeferred(fContext->resourceProvider(), dstDesc, fit,
810 isDstBudgeted);
811 } else {
812 SkASSERT(SkBackingFit::kExact == fit);
813 proxy = GrSurfaceProxy::MakeDeferredMipMap(fContext->resourceProvider(), dstDesc,
814 isDstBudgeted);
815 }
Robert Phillips77b3f322017-01-31 18:24:12 -0500816 if (!proxy) {
817 return nullptr;
818 }
Robert Phillipse2f7d182016-12-15 09:23:05 -0500819
Robert Phillips2c862492017-01-18 10:08:39 -0500820 return this->makeWrappedSurfaceContext(std::move(proxy), nullptr);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500821}
822
Brian Salomond17f6582017-07-19 18:28:58 -0400823sk_sp<GrTextureContext> GrContextPriv::makeBackendTextureContext(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000824 GrSurfaceOrigin origin,
Brian Osmanc1e37052017-03-09 14:19:20 -0500825 sk_sp<SkColorSpace> colorSpace) {
Robert Phillips26caf892017-01-27 10:58:31 -0500826 ASSERT_SINGLE_OWNER_PRIV
827
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400828 sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTexture(tex));
Robert Phillips26caf892017-01-27 10:58:31 -0500829 if (!surface) {
830 return nullptr;
831 }
832
Robert Phillips066f0202017-07-25 10:16:35 -0400833 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface), origin));
Robert Phillips77b3f322017-01-31 18:24:12 -0500834 if (!proxy) {
835 return nullptr;
836 }
Robert Phillips26caf892017-01-27 10:58:31 -0500837
Brian Salomond17f6582017-07-19 18:28:58 -0400838 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Robert Phillips26caf892017-01-27 10:58:31 -0500839}
840
Brian Osman11052242016-10-27 14:47:55 -0400841sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000842 const GrBackendTexture& tex,
843 GrSurfaceOrigin origin,
844 int sampleCnt,
Brian Osman11052242016-10-27 14:47:55 -0400845 sk_sp<SkColorSpace> colorSpace,
Brian Osmanc1e37052017-03-09 14:19:20 -0500846 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700847 ASSERT_SINGLE_OWNER_PRIV
robertphillips4fd74ae2016-08-03 14:26:53 -0700848
Brian Salomond17f6582017-07-19 18:28:58 -0400849 sk_sp<GrSurface> surface(
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400850 fContext->resourceProvider()->wrapRenderableBackendTexture(tex, sampleCnt));
robertphillips4fd74ae2016-08-03 14:26:53 -0700851 if (!surface) {
852 return nullptr;
853 }
854
Robert Phillips066f0202017-07-25 10:16:35 -0400855 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface), origin));
Robert Phillips77b3f322017-01-31 18:24:12 -0500856 if (!proxy) {
857 return nullptr;
858 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400859
Robert Phillips37430132016-11-09 06:50:43 -0500860 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Brian Osman11052242016-10-27 14:47:55 -0400861 std::move(colorSpace), props);
robertphillips4fd74ae2016-08-03 14:26:53 -0700862}
863
Brian Osman11052242016-10-27 14:47:55 -0400864sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext(
Greg Danielbcf612b2017-05-01 13:50:58 +0000865 const GrBackendRenderTarget& backendRT,
866 GrSurfaceOrigin origin,
robertphillips4fd74ae2016-08-03 14:26:53 -0700867 sk_sp<SkColorSpace> colorSpace,
868 const SkSurfaceProps* surfaceProps) {
869 ASSERT_SINGLE_OWNER_PRIV
870
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400871 sk_sp<GrRenderTarget> rt(fContext->resourceProvider()->wrapBackendRenderTarget(backendRT));
robertphillips4fd74ae2016-08-03 14:26:53 -0700872 if (!rt) {
873 return nullptr;
874 }
875
Robert Phillips066f0202017-07-25 10:16:35 -0400876 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(rt), origin));
Robert Phillips77b3f322017-01-31 18:24:12 -0500877 if (!proxy) {
878 return nullptr;
879 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400880
Robert Phillips37430132016-11-09 06:50:43 -0500881 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400882 std::move(colorSpace),
Brian Osman11052242016-10-27 14:47:55 -0400883 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -0700884}
885
Brian Osman11052242016-10-27 14:47:55 -0400886sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000887 const GrBackendTexture& tex,
888 GrSurfaceOrigin origin,
889 int sampleCnt,
robertphillips4fd74ae2016-08-03 14:26:53 -0700890 sk_sp<SkColorSpace> colorSpace,
891 const SkSurfaceProps* surfaceProps) {
892 ASSERT_SINGLE_OWNER_PRIV
robertphillips4fd74ae2016-08-03 14:26:53 -0700893
Greg Daniel7ef28f32017-04-20 16:41:55 +0000894 sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTextureAsRenderTarget(
895 tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000896 sampleCnt));
robertphillips4fd74ae2016-08-03 14:26:53 -0700897 if (!surface) {
898 return nullptr;
899 }
900
Robert Phillips066f0202017-07-25 10:16:35 -0400901 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface), origin));
Robert Phillips77b3f322017-01-31 18:24:12 -0500902 if (!proxy) {
903 return nullptr;
904 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400905
Robert Phillips37430132016-11-09 06:50:43 -0500906 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400907 std::move(colorSpace),
908 surfaceProps);
robertphillips77a2e522015-10-17 07:43:27 -0700909}
910
Chris Daltonfe199b72017-05-05 11:26:15 -0400911void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
912 fContext->fDrawingManager->addOnFlushCallbackObject(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400913}
914
915
robertphillips48fde9c2016-09-06 05:20:20 -0700916static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) {
Brian Osman78f20e02017-01-12 10:28:01 -0500917 switch (config) {
918 case kAlpha_8_GrPixelConfig:
919 case kRGB_565_GrPixelConfig:
920 case kRGBA_4444_GrPixelConfig:
921 case kBGRA_8888_GrPixelConfig:
922 return kRGBA_8888_GrPixelConfig;
923 case kSBGRA_8888_GrPixelConfig:
924 return kSRGBA_8888_GrPixelConfig;
925 case kAlpha_half_GrPixelConfig:
926 return kRGBA_half_GrPixelConfig;
927 default:
928 return kUnknown_GrPixelConfig;
929 }
robertphillips48fde9c2016-09-06 05:20:20 -0700930}
931
robertphillipsd728f0c2016-11-21 11:05:03 -0800932sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContextWithFallback(
933 SkBackingFit fit,
934 int width, int height,
935 GrPixelConfig config,
936 sk_sp<SkColorSpace> colorSpace,
937 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -0400938 GrMipMapped mipMapped,
robertphillipsd728f0c2016-11-21 11:05:03 -0800939 GrSurfaceOrigin origin,
940 const SkSurfaceProps* surfaceProps,
941 SkBudgeted budgeted) {
942 if (!this->caps()->isConfigRenderable(config, sampleCnt > 0)) {
943 config = GrPixelConfigFallback(config);
944 }
945
946 return this->makeDeferredRenderTargetContext(fit, width, height, config, std::move(colorSpace),
Greg Daniel45d63032017-10-30 13:41:26 -0400947 sampleCnt, mipMapped, origin, surfaceProps,
Greg Daniele1da1d92017-10-06 15:59:27 -0400948 budgeted);
robertphillipsd728f0c2016-11-21 11:05:03 -0800949}
950
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400951sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContext(
952 SkBackingFit fit,
953 int width, int height,
954 GrPixelConfig config,
955 sk_sp<SkColorSpace> colorSpace,
956 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -0400957 GrMipMapped mipMapped,
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400958 GrSurfaceOrigin origin,
959 const SkSurfaceProps* surfaceProps,
960 SkBudgeted budgeted) {
Brian Salomon79e4d1b2017-07-13 11:17:11 -0400961 if (this->abandoned()) {
962 return nullptr;
963 }
964
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400965 GrSurfaceDesc desc;
966 desc.fFlags = kRenderTarget_GrSurfaceFlag;
967 desc.fOrigin = origin;
968 desc.fWidth = width;
969 desc.fHeight = height;
970 desc.fConfig = config;
971 desc.fSampleCnt = sampleCnt;
972
Greg Daniele1da1d92017-10-06 15:59:27 -0400973 sk_sp<GrTextureProxy> rtp;
Greg Daniel45d63032017-10-30 13:41:26 -0400974 if (GrMipMapped::kNo == mipMapped) {
Greg Daniele1da1d92017-10-06 15:59:27 -0400975 rtp = GrSurfaceProxy::MakeDeferred(this->resourceProvider(), desc, fit, budgeted);
976 } else {
977 rtp = GrSurfaceProxy::MakeDeferredMipMap(this->resourceProvider(), desc, budgeted);
978 }
Robert Phillips08c5ec72017-01-30 12:26:47 -0500979 if (!rtp) {
980 return nullptr;
981 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400982
Robert Phillips1119dc32017-04-11 12:54:57 -0400983 sk_sp<GrRenderTargetContext> renderTargetContext(
984 fDrawingManager->makeRenderTargetContext(std::move(rtp),
985 std::move(colorSpace),
986 surfaceProps));
Robert Phillips1119dc32017-04-11 12:54:57 -0400987 if (!renderTargetContext) {
988 return nullptr;
989 }
990
991 renderTargetContext->discard();
992
993 return renderTargetContext;
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400994}
995
joshualitt1de610a2016-01-06 08:26:09 -0800996bool GrContext::abandoned() const {
997 ASSERT_SINGLE_OWNER
robertphillips7761d612016-05-16 09:14:53 -0700998 return fDrawingManager->wasAbandoned();
robertphillips77a2e522015-10-17 07:43:27 -0700999}
1000
Brian Salomonaff329b2017-08-11 09:40:37 -04001001std::unique_ptr<GrFragmentProcessor> GrContext::createPMToUPMEffect(
1002 std::unique_ptr<GrFragmentProcessor> fp, bool useConfigConversionEffect) {
Robert Phillips757914d2017-01-25 15:48:30 -05001003 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -04001004 // We have specialized effects that guarantee round-trip conversion for some formats
1005 if (useConfigConversionEffect) {
1006 // We should have already called this->validPMUPMConversionExists() in this case
1007 SkASSERT(fDidTestPMConversions);
1008 // ...and it should have succeeded
1009 SkASSERT(this->validPMUPMConversionExists());
1010
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001011 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul);
Brian Osman2d2da4f2017-04-12 17:07:22 -04001012 } else {
1013 // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and
1014 // explicitly round the results. Just do the obvious, naive thing in the shader.
1015 return GrFragmentProcessor::UnpremulOutput(std::move(fp));
Robert Phillips757914d2017-01-25 15:48:30 -05001016 }
1017}
1018
Brian Salomonaff329b2017-08-11 09:40:37 -04001019std::unique_ptr<GrFragmentProcessor> GrContext::createUPMToPMEffect(
1020 std::unique_ptr<GrFragmentProcessor> fp, bool useConfigConversionEffect) {
joshualitt1de610a2016-01-06 08:26:09 -08001021 ASSERT_SINGLE_OWNER
Brian Osman2d2da4f2017-04-12 17:07:22 -04001022 // We have specialized effects that guarantee round-trip conversion for these formats
Brian Osman409e74f2017-04-17 11:48:28 -04001023 if (useConfigConversionEffect) {
1024 // We should have already called this->validPMUPMConversionExists() in this case
1025 SkASSERT(fDidTestPMConversions);
1026 // ...and it should have succeeded
1027 SkASSERT(this->validPMUPMConversionExists());
1028
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001029 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
Brian Osman2d2da4f2017-04-12 17:07:22 -04001030 } else {
1031 // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and
1032 // explicitly round the results. Just do the obvious, naive thing in the shader.
1033 return GrFragmentProcessor::PremulOutput(std::move(fp));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001034 }
1035}
1036
Brian Osman409e74f2017-04-17 11:48:28 -04001037bool GrContext::validPMUPMConversionExists() {
joshualitt1de610a2016-01-06 08:26:09 -08001038 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -04001039 if (!fDidTestPMConversions) {
Brian Osman28804f32017-04-20 10:24:36 -04001040 fPMUPMConversionsRoundTrip = GrConfigConversionEffect::TestForPreservingPMConversions(this);
Brian Osman409e74f2017-04-17 11:48:28 -04001041 fDidTestPMConversions = true;
1042 }
1043
bsalomon636e8022015-07-29 06:08:46 -07001044 // The PM<->UPM tests fail or succeed together so we only need to check one.
Brian Osman28804f32017-04-20 10:24:36 -04001045 return fPMUPMConversionsRoundTrip;
bsalomon636e8022015-07-29 06:08:46 -07001046}
1047
bsalomon37f9a262015-02-02 13:00:10 -08001048//////////////////////////////////////////////////////////////////////////////
1049
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001050void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -08001051 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001052 if (maxResources) {
1053 *maxResources = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -08001054 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001055 if (maxResourceBytes) {
1056 *maxResourceBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -08001057 }
1058}
1059
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001060void GrContext::setResourceCacheLimits(int maxResources, size_t maxResourceBytes) {
joshualitt1de610a2016-01-06 08:26:09 -08001061 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001062 fResourceCache->setLimits(maxResources, maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -08001063}
1064
ericrk0a5fa482015-09-15 14:16:10 -07001065//////////////////////////////////////////////////////////////////////////////
1066
1067void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -08001068 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -07001069 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
1070}
Brian Osman71a18892017-08-10 10:23:25 -04001071
1072//////////////////////////////////////////////////////////////////////////////
1073
1074SkString GrContext::dump() const {
1075 SkDynamicMemoryWStream stream;
1076 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
1077 writer.beginObject();
1078
1079 static const char* kBackendStr[] = {
1080 "Metal",
1081 "OpenGL",
1082 "Vulkan",
1083 "Mock",
1084 };
1085 GR_STATIC_ASSERT(0 == kMetal_GrBackend);
1086 GR_STATIC_ASSERT(1 == kOpenGL_GrBackend);
1087 GR_STATIC_ASSERT(2 == kVulkan_GrBackend);
1088 GR_STATIC_ASSERT(3 == kMock_GrBackend);
1089 writer.appendString("backend", kBackendStr[fBackend]);
1090
1091 writer.appendName("caps");
1092 fCaps->dumpJSON(&writer);
1093
1094 writer.appendName("gpu");
1095 fGpu->dumpJSON(&writer);
1096
1097 // Flush JSON to the memory stream
1098 writer.endObject();
1099 writer.flush();
1100
1101 // Null terminate the JSON data in the memory stream
1102 stream.write8(0);
1103
1104 // Allocate a string big enough to hold all the data, then copy out of the stream
1105 SkString result(stream.bytesWritten());
1106 stream.copyToAndReset(result.writable_str());
1107 return result;
1108}