blob: e8f5c4b88922c962d321d8f38935f9808dd8b5f1 [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
Greg Daniel02611d92017-07-25 10:05:01 -040041#ifdef SK_VULKAN
42#include "vk/GrVkGpu.h"
43#endif
Greg Danielb76a72a2017-07-13 15:07:54 -040044
Robert Phillipse78b7252017-04-06 07:59:41 -040045#define ASSERT_OWNED_PROXY(P) \
46SkASSERT(!(P) || !((P)->priv().peekTexture()) || (P)->priv().peekTexture()->getContext() == this)
Robert Phillips7ee385e2017-03-30 08:02:11 -040047#define ASSERT_OWNED_PROXY_PRIV(P) \
48SkASSERT(!(P) || !((P)->priv().peekTexture()) || (P)->priv().peekTexture()->getContext() == fContext)
49
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000050#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
joshualitt1de610a2016-01-06 08:26:09 -080051#define ASSERT_SINGLE_OWNER \
52 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fSingleOwner);)
robertphillips4fd74ae2016-08-03 14:26:53 -070053#define ASSERT_SINGLE_OWNER_PRIV \
54 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fContext->fSingleOwner);)
robertphillips7761d612016-05-16 09:14:53 -070055#define RETURN_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return; }
Robert Phillips7ee385e2017-03-30 08:02:11 -040056#define RETURN_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return; }
robertphillips7761d612016-05-16 09:14:53 -070057#define RETURN_FALSE_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return false; }
Robert Phillipse78b7252017-04-06 07:59:41 -040058#define RETURN_FALSE_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return false; }
robertphillips7761d612016-05-16 09:14:53 -070059#define RETURN_NULL_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return nullptr; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000060
robertphillipsea461502015-05-26 11:38:03 -070061////////////////////////////////////////////////////////////////////////////////
62
bsalomon682c2692015-05-22 14:01:46 -070063GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext) {
64 GrContextOptions defaultOptions;
65 return Create(backend, backendContext, defaultOptions);
66}
bsalomonf28cff72015-05-22 12:25:41 -070067
bsalomon682c2692015-05-22 14:01:46 -070068GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
69 const GrContextOptions& options) {
Brian Salomon91a3e522017-06-23 10:58:19 -040070 sk_sp<GrContext> context(new GrContext);
bsalomon682c2692015-05-22 14:01:46 -070071
Brian Salomon91a3e522017-06-23 10:58:19 -040072 if (!context->init(backend, backendContext, options)) {
halcanary96fcdcc2015-08-27 07:41:13 -070073 return nullptr;
bsalomon@google.com27847de2011-02-22 20:59:41 +000074 }
Brian Salomon91a3e522017-06-23 10:58:19 -040075 return context.release();
bsalomon@google.com27847de2011-02-22 20:59:41 +000076}
77
Brian Salomon384fab42017-12-07 12:33:05 -050078sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface) {
Greg Daniel02611d92017-07-25 10:05:01 -040079 GrContextOptions defaultOptions;
Brian Salomon384fab42017-12-07 12:33:05 -050080 return MakeGL(std::move(interface), defaultOptions);
Greg Daniel02611d92017-07-25 10:05:01 -040081}
82
Brian Salomon384fab42017-12-07 12:33:05 -050083sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface,
Greg Daniel02611d92017-07-25 10:05:01 -040084 const GrContextOptions& options) {
85 sk_sp<GrContext> context(new GrContext);
Brian Salomon384fab42017-12-07 12:33:05 -050086 context->fGpu = GrGLGpu::Make(std::move(interface), options, context.get());
Greg Daniel02611d92017-07-25 10:05:01 -040087 if (!context->fGpu) {
88 return nullptr;
89 }
90 context->fBackend = kOpenGL_GrBackend;
91 if (!context->init(options)) {
92 return nullptr;
93 }
94 return context;
95}
96
97sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions) {
98 GrContextOptions defaultOptions;
99 return MakeMock(mockOptions, defaultOptions);
100}
101
102sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions,
103 const GrContextOptions& options) {
104 sk_sp<GrContext> context(new GrContext);
Brian Salomon384fab42017-12-07 12:33:05 -0500105 context->fGpu = GrMockGpu::Make(mockOptions, options, context.get());
Greg Daniel02611d92017-07-25 10:05:01 -0400106 if (!context->fGpu) {
107 return nullptr;
108 }
109 context->fBackend = kMock_GrBackend;
110 if (!context->init(options)) {
111 return nullptr;
112 }
113 return context;
114}
115
116#ifdef SK_VULKAN
Brian Salomon384fab42017-12-07 12:33:05 -0500117sk_sp<GrContext> GrContext::MakeVulkan(sk_sp<const GrVkBackendContext> backendContext) {
Greg Daniel02611d92017-07-25 10:05:01 -0400118 GrContextOptions defaultOptions;
Brian Salomon384fab42017-12-07 12:33:05 -0500119 return MakeVulkan(std::move(backendContext), defaultOptions);
Greg Daniel02611d92017-07-25 10:05:01 -0400120}
121
Brian Salomon384fab42017-12-07 12:33:05 -0500122sk_sp<GrContext> GrContext::MakeVulkan(sk_sp<const GrVkBackendContext> backendContext,
Greg Daniel02611d92017-07-25 10:05:01 -0400123 const GrContextOptions& options) {
124 sk_sp<GrContext> context(new GrContext);
Brian Salomon384fab42017-12-07 12:33:05 -0500125 context->fGpu = GrVkGpu::Make(std::move(backendContext), options, context.get());
Greg Daniel02611d92017-07-25 10:05:01 -0400126 if (!context->fGpu) {
127 return nullptr;
128 }
129 context->fBackend = kVulkan_GrBackend;
130 if (!context->init(options)) {
131 return nullptr;
132 }
133 return context;
134}
135#endif
136
Greg Danielb76a72a2017-07-13 15:07:54 -0400137#ifdef SK_METAL
Greg Daniel02611d92017-07-25 10:05:01 -0400138sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue) {
139 GrContextOptions defaultOptions;
140 return MakeMetal(device, queue, defaultOptions);
141}
142
Greg Danielb76a72a2017-07-13 15:07:54 -0400143sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) {
144 sk_sp<GrContext> context(new GrContext);
Brian Salomon384fab42017-12-07 12:33:05 -0500145 context->fGpu = GrMtlTrampoline::MakeGpu(context.get(), options, device, queue);
Greg Danielb76a72a2017-07-13 15:07:54 -0400146 if (!context->fGpu) {
147 return nullptr;
148 }
149 context->fBackend = kMetal_GrBackend;
150 if (!context->init(options)) {
151 return nullptr;
152 }
153 return context;
154}
155#endif
156
joshualitt0acd0d32015-05-07 08:23:19 -0700157static int32_t gNextID = 1;
158static int32_t next_id() {
159 int32_t id;
160 do {
161 id = sk_atomic_inc(&gNextID);
162 } while (id == SK_InvalidGenID);
163 return id;
164}
165
bsalomon682c2692015-05-22 14:01:46 -0700166GrContext::GrContext() : fUniqueID(next_id()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700167 fCaps = nullptr;
168 fResourceCache = nullptr;
169 fResourceProvider = nullptr;
Brian Salomonf856fd12016-12-16 14:24:34 -0500170 fAtlasGlyphCache = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000171}
172
bsalomon682c2692015-05-22 14:01:46 -0700173bool GrContext::init(GrBackend backend, GrBackendContext backendContext,
174 const GrContextOptions& options) {
joshualitt1de610a2016-01-06 08:26:09 -0800175 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700176 SkASSERT(!fGpu);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000177
Greg Danielfc978fd2017-04-13 09:54:12 -0400178 fBackend = backend;
179
Brian Salomon384fab42017-12-07 12:33:05 -0500180 fGpu = GrGpu::Make(backend, backendContext, options, this);
robertphillipsea461502015-05-26 11:38:03 -0700181 if (!fGpu) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000182 return false;
183 }
Greg Danielb76a72a2017-07-13 15:07:54 -0400184 return this->init(options);
185}
joshualitt1de610a2016-01-06 08:26:09 -0800186
Greg Danielb76a72a2017-07-13 15:07:54 -0400187bool GrContext::init(const GrContextOptions& options) {
188 ASSERT_SINGLE_OWNER
bsalomon76228632015-05-29 08:02:10 -0700189 fCaps = SkRef(fGpu->caps());
Brian Osman13dddce2017-05-09 13:19:50 -0400190 fResourceCache = new GrResourceCache(fCaps, fUniqueID);
Brian Salomon384fab42017-12-07 12:33:05 -0500191 fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, &fSingleOwner);
commit-bot@chromium.org1836d332013-07-16 22:55:03 +0000192
Brian Osman46da1cc2017-02-14 14:15:48 -0500193 fDisableGpuYUVConversion = options.fDisableGpuYUVConversion;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000194 fDidTestPMConversions = false;
195
bsalomon6b2552f2016-09-15 13:50:26 -0700196 GrPathRendererChain::Options prcOptions;
bsalomon39ef7fb2016-09-21 11:16:05 -0700197 prcOptions.fAllowPathMaskCaching = options.fAllowPathMaskCaching;
Brian Osman195c05b2017-08-30 15:14:04 -0400198#if GR_TEST_UTILS
csmartdalton008b9d82017-02-22 12:00:42 -0700199 prcOptions.fGpuPathRenderers = options.fGpuPathRenderers;
Brian Osman195c05b2017-08-30 15:14:04 -0400200#endif
Brian Osmanb350ae22017-08-29 16:15:39 -0400201 if (options.fDisableDistanceFieldPaths) {
Brian Osman195c05b2017-08-30 15:14:04 -0400202 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
Brian Osmanb350ae22017-08-29 16:15:39 -0400203 }
Brian Salomonaf597482017-11-07 16:23:34 -0500204
205 GrAtlasTextContext::Options atlasTextContextOptions;
206 atlasTextContextOptions.fMaxDistanceFieldFontSize = options.fGlyphsAsPathsFontSize;
207 atlasTextContextOptions.fMinDistanceFieldFontSize = options.fMinDistanceFieldFontSize;
208
209 fDrawingManager.reset(
210 new GrDrawingManager(this, prcOptions, atlasTextContextOptions, &fSingleOwner));
joshualitt7c3a2f82015-03-31 13:32:05 -0700211
Brian Salomon9f545bc2017-11-06 10:36:57 -0500212 GrDrawOpAtlas::AllowMultitexturing allowMultitexturing;
Jim Van Verthc02cb8a2017-11-20 22:11:30 +0000213 switch (options.fAllowMultipleGlyphCacheTextures) {
214 case GrContextOptions::Enable::kDefault:
215#ifdef SK_BUILD_FOR_IOS
216 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
217#else
218 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
219#endif
220 break;
221 case GrContextOptions::Enable::kNo:
222 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
223 break;
224 case GrContextOptions::Enable::kYes:
225 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
226 break;
Brian Salomon9f545bc2017-11-06 10:36:57 -0500227 }
228 fAtlasGlyphCache = new GrAtlasGlyphCache(this, options.fGlyphCacheTextureMaximumBytes,
229 allowMultitexturing);
Jim Van Verth106b5c42017-09-26 12:45:29 -0400230 this->contextPriv().addOnFlushCallbackObject(fAtlasGlyphCache);
joshualittb7133be2015-04-08 09:08:31 -0700231
halcanary385fe4d2015-08-26 13:07:48 -0700232 fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB, this));
Brian Salomon91a3e522017-06-23 10:58:19 -0400233
Brian Osman51279982017-08-23 10:12:00 -0400234 if (options.fExecutor) {
235 fTaskGroup = skstd::make_unique<SkTaskGroup>(*options.fExecutor);
236 }
237
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400238 fPersistentCache = options.fPersistentCache;
239
Brian Salomon91a3e522017-06-23 10:58:19 -0400240 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000241}
242
bsalomon@google.com27847de2011-02-22 20:59:41 +0000243GrContext::~GrContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800244 ASSERT_SINGLE_OWNER
245
robertphillipsea461502015-05-26 11:38:03 -0700246 if (!fGpu) {
bsalomon76228632015-05-29 08:02:10 -0700247 SkASSERT(!fCaps);
bsalomon@google.com733c0622013-04-24 17:59:32 +0000248 return;
249 }
250
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000251 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000252
robertphillips77a2e522015-10-17 07:43:27 -0700253 fDrawingManager->cleanup();
robertphillips2334fb62015-06-17 05:43:33 -0700254
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000255 for (int i = 0; i < fCleanUpData.count(); ++i) {
256 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
257 }
258
halcanary385fe4d2015-08-26 13:07:48 -0700259 delete fResourceProvider;
260 delete fResourceCache;
Brian Salomonf856fd12016-12-16 14:24:34 -0500261 delete fAtlasGlyphCache;
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000262
bsalomon76228632015-05-29 08:02:10 -0700263 fCaps->unref();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000264}
265
bungeman6bd52842016-10-27 09:30:08 -0700266sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
bsalomon41b952c2016-03-11 06:46:33 -0800267 if (!fThreadSafeProxy) {
bungeman6bd52842016-10-27 09:30:08 -0700268 fThreadSafeProxy.reset(new GrContextThreadSafeProxy(sk_ref_sp(fCaps), this->uniqueID()));
bsalomon41b952c2016-03-11 06:46:33 -0800269 }
bungeman6bd52842016-10-27 09:30:08 -0700270 return fThreadSafeProxy;
bsalomon41b952c2016-03-11 06:46:33 -0800271}
272
bsalomon2354f842014-07-28 13:48:36 -0700273void GrContext::abandonContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800274 ASSERT_SINGLE_OWNER
275
bsalomond309e7a2015-04-30 14:18:54 -0700276 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800277
278 // Need to abandon the drawing manager first so all the render targets
279 // will be released/forgotten before they too are abandoned.
280 fDrawingManager->abandon();
281
bsalomon@google.com205d4602011-04-25 12:43:45 +0000282 // abandon first to so destructors
283 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800284 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700285
bsalomon6e2aad42016-04-01 11:54:31 -0700286 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
287
Brian Salomonf856fd12016-12-16 14:24:34 -0500288 fAtlasGlyphCache->freeAll();
bsalomon6e2aad42016-04-01 11:54:31 -0700289 fTextBlobCache->freeAll();
290}
291
292void GrContext::releaseResourcesAndAbandonContext() {
293 ASSERT_SINGLE_OWNER
294
295 fResourceProvider->abandon();
296
297 // Need to abandon the drawing manager first so all the render targets
298 // will be released/forgotten before they too are abandoned.
299 fDrawingManager->abandon();
300
301 // Release all resources in the backend 3D API.
302 fResourceCache->releaseAll();
303
304 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000305
Brian Salomonf856fd12016-12-16 14:24:34 -0500306 fAtlasGlyphCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700307 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000308}
309
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000310void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800311 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000312 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000313}
314
315void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800316 ASSERT_SINGLE_OWNER
317
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000318 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000319
Brian Salomonf856fd12016-12-16 14:24:34 -0500320 fAtlasGlyphCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700321
322 fDrawingManager->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700323
324 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000325}
326
Brian Salomon5e150852017-03-22 14:53:13 -0400327void GrContext::purgeResourcesNotUsedInMs(std::chrono::milliseconds ms) {
328 ASSERT_SINGLE_OWNER
329 fResourceCache->purgeResourcesNotUsedSince(GrStdSteadyClock::now() - ms);
330}
331
Derek Sollenberger5480a182017-05-25 16:43:59 -0400332void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
333 ASSERT_SINGLE_OWNER
334 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
335}
336
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000337void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800338 ASSERT_SINGLE_OWNER
339
bsalomon71cb0c22014-11-14 12:10:14 -0800340 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800341 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800342 }
343 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800344 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800345 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000346}
347
Derek Sollenbergeree479142017-05-24 11:41:33 -0400348size_t GrContext::getResourceCachePurgeableBytes() const {
349 ASSERT_SINGLE_OWNER
350 return fResourceCache->getPurgeableBytes();
351}
352
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000353////////////////////////////////////////////////////////////////////////////////
354
joshualitt0db6dfa2015-04-10 07:01:30 -0700355void GrContext::TextBlobCacheOverBudgetCB(void* data) {
356 SkASSERT(data);
Brian Osman11052242016-10-27 14:47:55 -0400357 // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on
358 // GrRenderTargetContext to perform a necessary flush. The solution is to move drawText calls
359 // to below the GrContext level, but this is not trivial because they call drawPath on
360 // SkGpuDevice.
joshualitt0db6dfa2015-04-10 07:01:30 -0700361 GrContext* context = reinterpret_cast<GrContext*>(data);
362 context->flush();
363}
364
bsalomon@google.com27847de2011-02-22 20:59:41 +0000365////////////////////////////////////////////////////////////////////////////////
366
bsalomonb77a9072016-09-07 10:02:04 -0700367void GrContext::flush() {
joshualitt1de610a2016-01-06 08:26:09 -0800368 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700369 RETURN_IF_ABANDONED
Robert Phillips7ee385e2017-03-30 08:02:11 -0400370
371 fDrawingManager->flush(nullptr);
372}
373
Greg Daniel51316782017-08-02 15:10:09 +0000374GrSemaphoresSubmitted GrContext::flushAndSignalSemaphores(int numSemaphores,
375 GrBackendSemaphore signalSemaphores[]) {
376 ASSERT_SINGLE_OWNER
377 if (fDrawingManager->wasAbandoned()) { return GrSemaphoresSubmitted::kNo; }
378
379 return fDrawingManager->flush(nullptr, numSemaphores, signalSemaphores);
380}
381
Robert Phillips7ee385e2017-03-30 08:02:11 -0400382void GrContextPriv::flush(GrSurfaceProxy* proxy) {
383 ASSERT_SINGLE_OWNER_PRIV
384 RETURN_IF_ABANDONED_PRIV
385 ASSERT_OWNED_PROXY_PRIV(proxy);
386
387 fContext->fDrawingManager->flush(proxy);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000388}
389
bsalomon81beccc2014-10-13 12:32:55 -0700390bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
391 const void* inPixels, size_t outRowBytes, void* outPixels) {
Matt Sarettc7b29082017-02-09 16:22:39 -0500392 SkColorType colorType;
393 if (!GrPixelConfigToColorType(srcConfig, &colorType) ||
394 4 != SkColorTypeBytesPerPixel(colorType))
395 {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000396 return false;
397 }
bsalomon81beccc2014-10-13 12:32:55 -0700398
Matt Sarettc7b29082017-02-09 16:22:39 -0500399 for (int y = 0; y < height; y++) {
400 SkOpts::RGBA_to_rgbA((uint32_t*) outPixels, inPixels, width);
401 outPixels = SkTAddOffset<void>(outPixels, outRowBytes);
402 inPixels = SkTAddOffset<const void>(inPixels, inRowBytes);
403 }
bsalomon81beccc2014-10-13 12:32:55 -0700404
Matt Sarettc7b29082017-02-09 16:22:39 -0500405 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000406}
407
Brian Osmand2ca59a2017-04-13 14:03:57 -0400408static bool valid_premul_config(GrPixelConfig config) {
Brian Osmance425512017-03-22 14:37:50 -0400409 return GrPixelConfigIs8888Unorm(config) || kRGBA_half_GrPixelConfig == config;
410}
411
Brian Osmand2ca59a2017-04-13 14:03:57 -0400412static bool valid_pixel_conversion(GrPixelConfig srcConfig, GrPixelConfig dstConfig,
413 bool premulConversion) {
414 // We don't allow conversion between integer configs and float/fixed configs.
415 if (GrPixelConfigIsSint(srcConfig) != GrPixelConfigIsSint(dstConfig)) {
416 return false;
417 }
418
419 // We only allow premul <-> unpremul conversions for some formats
420 if (premulConversion && (!valid_premul_config(srcConfig) || !valid_premul_config(dstConfig))) {
421 return false;
422 }
423
424 return true;
425}
426
Brian Osman409e74f2017-04-17 11:48:28 -0400427static bool pm_upm_must_round_trip(GrPixelConfig config, SkColorSpace* colorSpace) {
428 return !colorSpace &&
429 (kRGBA_8888_GrPixelConfig == config || kBGRA_8888_GrPixelConfig == config);
430}
431
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400432bool GrContextPriv::writeSurfacePixels(GrSurfaceContext* dst,
Robert Phillipse78b7252017-04-06 07:59:41 -0400433 int left, int top, int width, int height,
434 GrPixelConfig srcConfig, SkColorSpace* srcColorSpace,
435 const void* buffer, size_t rowBytes,
436 uint32_t pixelOpsFlags) {
Brian Osmanb62ea222016-12-22 11:12:16 -0500437 // TODO: Color space conversion
438
Robert Phillipse78b7252017-04-06 07:59:41 -0400439 ASSERT_SINGLE_OWNER_PRIV
440 RETURN_FALSE_IF_ABANDONED_PRIV
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400441 SkASSERT(dst);
442 ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy());
Brian Salomondcbb9d92017-07-19 10:53:20 -0400443 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "writeSurfacePixels", fContext);
bsalomon6c6f6582015-09-10 08:12:46 -0700444
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400445 if (!dst->asSurfaceProxy()->instantiate(fContext->resourceProvider())) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400446 return false;
447 }
448
Robert Phillips16d8ec62017-07-27 16:16:25 -0400449 GrSurfaceProxy* dstProxy = dst->asSurfaceProxy();
450 GrSurface* dstSurface = dstProxy->priv().peekSurface();
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400451
Brian Osmand2ca59a2017-04-13 14:03:57 -0400452 // 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 -0400453 const bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
Robert Phillips16d8ec62017-07-27 16:16:25 -0400454 if (!valid_pixel_conversion(srcConfig, dstProxy->config(), premul)) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400455 return false;
456 }
457
Brian Osman409e74f2017-04-17 11:48:28 -0400458 // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data,
459 // without any color spaces attached, and the caller wants us to premul.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400460 bool useConfigConversionEffect =
Brian Salomonf3569f02017-10-24 12:52:33 -0400461 premul && pm_upm_must_round_trip(srcConfig, srcColorSpace) &&
462 pm_upm_must_round_trip(dstProxy->config(), dst->colorSpaceInfo().colorSpace());
Brian Osman409e74f2017-04-17 11:48:28 -0400463
464 // Are we going to try to premul as part of a draw? For the non-legacy case, we always allow
465 // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly.
466 bool premulOnGpu = premul &&
467 (!useConfigConversionEffect || fContext->validPMUPMConversionExists());
bsalomon81beccc2014-10-13 12:32:55 -0700468
bsalomone8d21e82015-07-16 08:23:13 -0700469 // 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 -0700470 // necessary and because GrGpu::getWritePixelsInfo requires it.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400471 if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(),
bsalomone8d21e82015-07-16 08:23:13 -0700472 GrBytesPerPixel(srcConfig), &left, &top, &width,
473 &height, &buffer, &rowBytes)) {
474 return false;
475 }
476
Brian Osman409e74f2017-04-17 11:48:28 -0400477 GrGpu::DrawPreference drawPreference = premulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference
478 : GrGpu::kNoDraw_DrawPreference;
bsalomonf0674512015-07-28 13:26:15 -0700479 GrGpu::WritePixelTempDrawInfo tempDrawInfo;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400480 if (!fContext->fGpu->getWritePixelsInfo(dstSurface, dstProxy->origin(), width, height,
Robert Phillips16d8ec62017-07-27 16:16:25 -0400481 srcConfig, &drawPreference, &tempDrawInfo)) {
bsalomonf0674512015-07-28 13:26:15 -0700482 return false;
483 }
484
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400485 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && dstSurface->surfacePriv().hasPendingIO()) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400486 this->flush(nullptr); // MDB TODO: tighten this
bsalomonf0674512015-07-28 13:26:15 -0700487 }
488
Robert Phillips2f493142017-03-02 18:18:38 -0500489 sk_sp<GrTextureProxy> tempProxy;
bsalomonf0674512015-07-28 13:26:15 -0700490 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400491 tempProxy = GrSurfaceProxy::MakeDeferred(fContext->resourceProvider(),
Robert Phillips2f493142017-03-02 18:18:38 -0500492 tempDrawInfo.fTempSurfaceDesc,
493 SkBackingFit::kApprox,
494 SkBudgeted::kYes);
495 if (!tempProxy && GrGpu::kRequireDraw_DrawPreference == drawPreference) {
bsalomonf0674512015-07-28 13:26:15 -0700496 return false;
497 }
498 }
499
500 // temp buffer for doing sw premul conversion, if needed.
501 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
Brian Osman409e74f2017-04-17 11:48:28 -0400502 // We need to do sw premul if we were unable to create a RT for drawing, or if we can't do the
503 // premul on the GPU
504 if (premul && (!tempProxy || !premulOnGpu)) {
505 size_t tmpRowBytes = 4 * width;
506 tmpPixels.reset(width * height);
507 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
508 tmpPixels.get())) {
509 return false;
bsalomonf0674512015-07-28 13:26:15 -0700510 }
Brian Osman409e74f2017-04-17 11:48:28 -0400511 rowBytes = tmpRowBytes;
512 buffer = tmpPixels.get();
bsalomonf0674512015-07-28 13:26:15 -0700513 }
Brian Osman409e74f2017-04-17 11:48:28 -0400514
515 if (tempProxy) {
Brian Osman2240be92017-10-18 13:15:13 -0400516 auto fp = GrSimpleTextureEffect::Make(tempProxy, SkMatrix::I());
Brian Osman409e74f2017-04-17 11:48:28 -0400517 if (premulOnGpu) {
518 fp = fContext->createUPMToPMEffect(std::move(fp), useConfigConversionEffect);
bsalomon81beccc2014-10-13 12:32:55 -0700519 }
Brian Osman409e74f2017-04-17 11:48:28 -0400520 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400521 if (!fp) {
522 return false;
523 }
Brian Osman409e74f2017-04-17 11:48:28 -0400524
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400525 if (!tempProxy->instantiate(fContext->resourceProvider())) {
Brian Osman409e74f2017-04-17 11:48:28 -0400526 return false;
527 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400528 GrTexture* texture = tempProxy->priv().peekTexture();
Robert Phillips7bbbf622017-10-17 07:36:59 -0400529
530 if (tempProxy->priv().hasPendingIO()) {
531 this->flush(tempProxy.get());
532 }
533
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400534 if (!fContext->fGpu->writePixels(texture, tempProxy->origin(), 0, 0, width, height,
535 tempDrawInfo.fWriteConfig, buffer, rowBytes)) {
Brian Osman409e74f2017-04-17 11:48:28 -0400536 return false;
537 }
Robert Phillips7bbbf622017-10-17 07:36:59 -0400538 tempProxy = nullptr;
539
Brian Osman409e74f2017-04-17 11:48:28 -0400540 SkMatrix matrix;
541 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400542 GrRenderTargetContext* renderTargetContext = dst->asRenderTargetContext();
Brian Osman409e74f2017-04-17 11:48:28 -0400543 if (!renderTargetContext) {
544 return false;
545 }
546 GrPaint paint;
547 paint.addColorFragmentProcessor(std::move(fp));
548 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonf3569f02017-10-24 12:52:33 -0400549 paint.setAllowSRGBInputs(dst->colorSpaceInfo().isGammaCorrect() ||
550 GrPixelConfigIsSRGB(dst->colorSpaceInfo().config()));
Brian Osman409e74f2017-04-17 11:48:28 -0400551 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
552 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, matrix, rect,
553 nullptr);
554
555 if (kFlushWrites_PixelOp & pixelOpsFlags) {
556 this->flushSurfaceWrites(renderTargetContext->asRenderTargetProxy());
557 }
558 } else {
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400559 return fContext->fGpu->writePixels(dstSurface, dstProxy->origin(), left, top, width,
560 height, srcConfig, buffer, rowBytes);
bsalomon81beccc2014-10-13 12:32:55 -0700561 }
bsalomon81beccc2014-10-13 12:32:55 -0700562 return true;
563}
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000564
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400565bool GrContextPriv::readSurfacePixels(GrSurfaceContext* src,
Robert Phillipse78b7252017-04-06 07:59:41 -0400566 int left, int top, int width, int height,
567 GrPixelConfig dstConfig, SkColorSpace* dstColorSpace,
568 void* buffer, size_t rowBytes, uint32_t flags) {
Brian Osmanb62ea222016-12-22 11:12:16 -0500569 // TODO: Color space conversion
570
Robert Phillipse78b7252017-04-06 07:59:41 -0400571 ASSERT_SINGLE_OWNER_PRIV
572 RETURN_FALSE_IF_ABANDONED_PRIV
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400573 SkASSERT(src);
574 ASSERT_OWNED_PROXY_PRIV(src->asSurfaceProxy());
Brian Salomondcbb9d92017-07-19 10:53:20 -0400575 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "readSurfacePixels", fContext);
bsalomon32ab2602015-09-09 18:57:49 -0700576
Robert Phillipse78b7252017-04-06 07:59:41 -0400577 // MDB TODO: delay this instantiation until later in the method
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400578 if (!src->asSurfaceProxy()->instantiate(fContext->resourceProvider())) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400579 return false;
580 }
581
Robert Phillips16d8ec62017-07-27 16:16:25 -0400582 GrSurfaceProxy* srcProxy = src->asSurfaceProxy();
583 GrSurface* srcSurface = srcProxy->priv().peekSurface();
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400584
Brian Osmand2ca59a2017-04-13 14:03:57 -0400585 // The src is premul but the dst is unpremul -> unpremul the src after or as part of the read
586 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
Robert Phillips16d8ec62017-07-27 16:16:25 -0400587 if (!valid_pixel_conversion(srcProxy->config(), dstConfig, unpremul)) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400588 return false;
589 }
590
Brian Osman409e74f2017-04-17 11:48:28 -0400591 // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data,
592 // without any color spaces attached, and the caller wants us to unpremul.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400593 bool useConfigConversionEffect =
Brian Salomonf3569f02017-10-24 12:52:33 -0400594 unpremul &&
595 pm_upm_must_round_trip(srcProxy->config(), src->colorSpaceInfo().colorSpace()) &&
596 pm_upm_must_round_trip(dstConfig, dstColorSpace);
Brian Osman409e74f2017-04-17 11:48:28 -0400597
598 // Are we going to try to unpremul as part of a draw? For the non-legacy case, we always allow
599 // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly.
600 bool unpremulOnGpu = unpremul &&
601 (!useConfigConversionEffect || fContext->validPMUPMConversionExists());
bsalomon6c6f6582015-09-10 08:12:46 -0700602
bsalomone8d21e82015-07-16 08:23:13 -0700603 // Adjust the params so that if we wind up using an intermediate surface we've already done
604 // all the trimming and the temporary can be the min size required.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400605 if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(),
bsalomone8d21e82015-07-16 08:23:13 -0700606 GrBytesPerPixel(dstConfig), &left,
607 &top, &width, &height, &buffer, &rowBytes)) {
608 return false;
609 }
610
Brian Osman409e74f2017-04-17 11:48:28 -0400611 GrGpu::DrawPreference drawPreference = unpremulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference
612 : GrGpu::kNoDraw_DrawPreference;
bsalomon39826022015-07-23 08:07:21 -0700613 GrGpu::ReadPixelTempDrawInfo tempDrawInfo;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400614 if (!fContext->fGpu->getReadPixelsInfo(srcSurface, srcProxy->origin(), width, height, rowBytes,
Robert Phillips16d8ec62017-07-27 16:16:25 -0400615 dstConfig, &drawPreference, &tempDrawInfo)) {
bsalomon39826022015-07-23 08:07:21 -0700616 return false;
617 }
bsalomon191bcc02014-11-14 11:31:13 -0800618
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400619 if (!(kDontFlush_PixelOpsFlag & flags) && srcSurface->surfacePriv().hasPendingWrite()) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400620 this->flush(nullptr); // MDB TODO: tighten this
621 }
622
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400623 sk_sp<GrSurfaceProxy> proxyToRead = src->asSurfaceProxyRef();
bsalomon39826022015-07-23 08:07:21 -0700624 bool didTempDraw = false;
625 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomonb117ff12016-07-19 07:24:40 -0700626 if (SkBackingFit::kExact == tempDrawInfo.fTempSurfaceFit) {
bsalomon39826022015-07-23 08:07:21 -0700627 // We only respect this when the entire src is being read. Otherwise we can trigger too
628 // many odd ball texture sizes and trash the cache.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400629 if (width != srcSurface->width() || height != srcSurface->height()) {
bsalomonb117ff12016-07-19 07:24:40 -0700630 tempDrawInfo.fTempSurfaceFit= SkBackingFit::kApprox;
bsalomon39826022015-07-23 08:07:21 -0700631 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000632 }
brianosmandfe4f2e2016-07-21 13:28:36 -0700633 // TODO: Need to decide the semantics of this function for color spaces. Do we support
634 // conversion to a passed-in color space? For now, specifying nullptr means that this
635 // path will do no conversion, so it will match the behavior of the non-draw path.
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400636 sk_sp<GrRenderTargetContext> tempRTC = fContext->makeDeferredRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400637 tempDrawInfo.fTempSurfaceFit,
bsalomonb117ff12016-07-19 07:24:40 -0700638 tempDrawInfo.fTempSurfaceDesc.fWidth,
639 tempDrawInfo.fTempSurfaceDesc.fHeight,
640 tempDrawInfo.fTempSurfaceDesc.fConfig,
brianosmandfe4f2e2016-07-21 13:28:36 -0700641 nullptr,
bsalomonb117ff12016-07-19 07:24:40 -0700642 tempDrawInfo.fTempSurfaceDesc.fSampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -0400643 GrMipMapped::kNo,
bsalomonb117ff12016-07-19 07:24:40 -0700644 tempDrawInfo.fTempSurfaceDesc.fOrigin);
Brian Osman693a5402016-10-27 15:13:22 -0400645 if (tempRTC) {
Robert Phillips67c18d62017-01-20 12:44:06 -0500646 SkMatrix textureMatrix = SkMatrix::MakeTrans(SkIntToScalar(left), SkIntToScalar(top));
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400647 sk_sp<GrTextureProxy> proxy = src->asTextureProxyRef();
Brian Osman2240be92017-10-18 13:15:13 -0400648 auto fp = GrSimpleTextureEffect::Make(std::move(proxy), textureMatrix);
Brian Osman409e74f2017-04-17 11:48:28 -0400649 if (unpremulOnGpu) {
650 fp = fContext->createPMToUPMEffect(std::move(fp), useConfigConversionEffect);
651 // We no longer need to do this on CPU after the read back.
652 unpremul = false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000653 }
Brian Osman409e74f2017-04-17 11:48:28 -0400654 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400655 if (!fp) {
656 return false;
657 }
Brian Osman60cd57e2017-04-06 10:19:06 -0400658
Brian Osman409e74f2017-04-17 11:48:28 -0400659 GrPaint paint;
660 paint.addColorFragmentProcessor(std::move(fp));
661 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
662 paint.setAllowSRGBInputs(true);
663 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
664 tempRTC->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect,
665 nullptr);
666 proxyToRead = tempRTC->asTextureProxyRef();
667 left = 0;
668 top = 0;
669 didTempDraw = true;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000670 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000671 }
joshualitt5c55fef2014-10-31 14:04:35 -0700672
Robert Phillipse78b7252017-04-06 07:59:41 -0400673 if (!proxyToRead) {
674 return false;
675 }
676
bsalomon39826022015-07-23 08:07:21 -0700677 if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000678 return false;
679 }
bsalomon39826022015-07-23 08:07:21 -0700680 GrPixelConfig configToRead = dstConfig;
681 if (didTempDraw) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400682 this->flushSurfaceWrites(proxyToRead.get());
bsalomon6c9cd552016-01-22 07:17:34 -0800683 configToRead = tempDrawInfo.fReadConfig;
bsalomon39826022015-07-23 08:07:21 -0700684 }
Robert Phillips09dfc472017-09-13 15:25:47 -0400685
686 if (!proxyToRead->instantiate(fContext->resourceProvider())) {
687 return false;
688 }
689
690 GrSurface* surfaceToRead = proxyToRead->priv().peekSurface();
691
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400692 if (!fContext->fGpu->readPixels(surfaceToRead, proxyToRead->origin(),
Robert Phillips16d8ec62017-07-27 16:16:25 -0400693 left, top, width, height, configToRead, buffer, rowBytes)) {
bsalomon39826022015-07-23 08:07:21 -0700694 return false;
695 }
696
697 // Perform umpremul conversion if we weren't able to perform it as a draw.
698 if (unpremul) {
Matt Sarettc7b29082017-02-09 16:22:39 -0500699 SkColorType colorType;
700 if (!GrPixelConfigToColorType(dstConfig, &colorType) ||
701 4 != SkColorTypeBytesPerPixel(colorType))
702 {
reed@google.com7111d462014-03-25 16:20:24 +0000703 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000704 }
reed@google.com7111d462014-03-25 16:20:24 +0000705
Matt Sarettc7b29082017-02-09 16:22:39 -0500706 for (int y = 0; y < height; y++) {
707 SkUnpremultiplyRow<false>((uint32_t*) buffer, (const uint32_t*) buffer, width);
708 buffer = SkTAddOffset<void>(buffer, rowBytes);
709 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000710 }
711 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000712}
713
Robert Phillips7ee385e2017-03-30 08:02:11 -0400714void GrContextPriv::prepareSurfaceForExternalIO(GrSurfaceProxy* proxy) {
715 ASSERT_SINGLE_OWNER_PRIV
716 RETURN_IF_ABANDONED_PRIV
717 SkASSERT(proxy);
718 ASSERT_OWNED_PROXY_PRIV(proxy);
Greg Daniel51316782017-08-02 15:10:09 +0000719 fContext->fDrawingManager->prepareSurfaceForExternalIO(proxy, 0, nullptr);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000720}
721
Robert Phillips7ee385e2017-03-30 08:02:11 -0400722void GrContextPriv::flushSurfaceWrites(GrSurfaceProxy* proxy) {
723 ASSERT_SINGLE_OWNER_PRIV
724 RETURN_IF_ABANDONED_PRIV
725 SkASSERT(proxy);
726 ASSERT_OWNED_PROXY_PRIV(proxy);
727 if (proxy->priv().hasPendingWrite()) {
728 this->flush(proxy);
bsalomonf80bfed2014-10-07 05:56:02 -0700729 }
730}
731
Robert Phillips7ee385e2017-03-30 08:02:11 -0400732void GrContextPriv::flushSurfaceIO(GrSurfaceProxy* proxy) {
733 ASSERT_SINGLE_OWNER_PRIV
734 RETURN_IF_ABANDONED_PRIV
735 SkASSERT(proxy);
736 ASSERT_OWNED_PROXY_PRIV(proxy);
737 if (proxy->priv().hasPendingIO()) {
738 this->flush(proxy);
ajuma95243eb2016-08-24 08:19:02 -0700739 }
740}
741
bsalomon@google.com27847de2011-02-22 20:59:41 +0000742////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000743int GrContext::getRecommendedSampleCount(GrPixelConfig config,
744 SkScalar dpi) const {
joshualitt1de610a2016-01-06 08:26:09 -0800745 ASSERT_SINGLE_OWNER
746
bsalomon76228632015-05-29 08:02:10 -0700747 if (!this->caps()->isConfigRenderable(config, true)) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000748 return 0;
749 }
750 int chosenSampleCount = 0;
jvanverthe9c0fc62015-04-29 11:18:05 -0700751 if (fGpu->caps()->shaderCaps()->pathRenderingSupport()) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000752 if (dpi >= 250.0f) {
753 chosenSampleCount = 4;
754 } else {
755 chosenSampleCount = 16;
756 }
757 }
Greg Daniel81e7bf82017-07-19 14:47:42 -0400758 int supportedSampleCount = fGpu->caps()->getSampleCount(chosenSampleCount, config);
759 return chosenSampleCount <= supportedSampleCount ? supportedSampleCount : 0;
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000760}
761
Robert Phillips2c862492017-01-18 10:08:39 -0500762sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy> proxy,
763 sk_sp<SkColorSpace> colorSpace) {
Brian Osman45580d32016-11-23 09:37:01 -0500764 ASSERT_SINGLE_OWNER_PRIV
765
Brian Osman45580d32016-11-23 09:37:01 -0500766 if (proxy->asRenderTargetProxy()) {
Robert Phillips2c862492017-01-18 10:08:39 -0500767 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
768 std::move(colorSpace), nullptr);
Brian Osman45580d32016-11-23 09:37:01 -0500769 } else {
770 SkASSERT(proxy->asTextureProxy());
Robert Phillips2c862492017-01-18 10:08:39 -0500771 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Brian Osman45580d32016-11-23 09:37:01 -0500772 }
773}
774
Robert Phillipse2f7d182016-12-15 09:23:05 -0500775sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(const GrSurfaceDesc& dstDesc,
Greg Daniel65c7f662017-10-30 13:39:09 -0400776 GrMipMapped mipMapped,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500777 SkBackingFit fit,
778 SkBudgeted isDstBudgeted) {
779
Greg Daniel65c7f662017-10-30 13:39:09 -0400780 sk_sp<GrTextureProxy> proxy;
781 if (GrMipMapped::kNo == mipMapped) {
782 proxy = GrSurfaceProxy::MakeDeferred(fContext->resourceProvider(), dstDesc, fit,
783 isDstBudgeted);
784 } else {
785 SkASSERT(SkBackingFit::kExact == fit);
786 proxy = GrSurfaceProxy::MakeDeferredMipMap(fContext->resourceProvider(), dstDesc,
787 isDstBudgeted);
788 }
Robert Phillips77b3f322017-01-31 18:24:12 -0500789 if (!proxy) {
790 return nullptr;
791 }
Robert Phillipse2f7d182016-12-15 09:23:05 -0500792
Robert Phillips2c862492017-01-18 10:08:39 -0500793 return this->makeWrappedSurfaceContext(std::move(proxy), nullptr);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500794}
795
Brian Salomond17f6582017-07-19 18:28:58 -0400796sk_sp<GrTextureContext> GrContextPriv::makeBackendTextureContext(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000797 GrSurfaceOrigin origin,
Brian Osmanc1e37052017-03-09 14:19:20 -0500798 sk_sp<SkColorSpace> colorSpace) {
Robert Phillips26caf892017-01-27 10:58:31 -0500799 ASSERT_SINGLE_OWNER_PRIV
800
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400801 sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTexture(tex));
Robert Phillips26caf892017-01-27 10:58:31 -0500802 if (!surface) {
803 return nullptr;
804 }
805
Robert Phillips066f0202017-07-25 10:16:35 -0400806 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface), origin));
Robert Phillips77b3f322017-01-31 18:24:12 -0500807 if (!proxy) {
808 return nullptr;
809 }
Robert Phillips26caf892017-01-27 10:58:31 -0500810
Brian Salomond17f6582017-07-19 18:28:58 -0400811 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Robert Phillips26caf892017-01-27 10:58:31 -0500812}
813
Brian Osman11052242016-10-27 14:47:55 -0400814sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000815 const GrBackendTexture& tex,
816 GrSurfaceOrigin origin,
817 int sampleCnt,
Brian Osman11052242016-10-27 14:47:55 -0400818 sk_sp<SkColorSpace> colorSpace,
Brian Osmanc1e37052017-03-09 14:19:20 -0500819 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700820 ASSERT_SINGLE_OWNER_PRIV
robertphillips4fd74ae2016-08-03 14:26:53 -0700821
Brian Salomond17f6582017-07-19 18:28:58 -0400822 sk_sp<GrSurface> surface(
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400823 fContext->resourceProvider()->wrapRenderableBackendTexture(tex, sampleCnt));
robertphillips4fd74ae2016-08-03 14:26:53 -0700824 if (!surface) {
825 return nullptr;
826 }
827
Robert Phillips066f0202017-07-25 10:16:35 -0400828 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface), origin));
Robert Phillips77b3f322017-01-31 18:24:12 -0500829 if (!proxy) {
830 return nullptr;
831 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400832
Robert Phillips37430132016-11-09 06:50:43 -0500833 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Brian Osman11052242016-10-27 14:47:55 -0400834 std::move(colorSpace), props);
robertphillips4fd74ae2016-08-03 14:26:53 -0700835}
836
Brian Osman11052242016-10-27 14:47:55 -0400837sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext(
Greg Danielbcf612b2017-05-01 13:50:58 +0000838 const GrBackendRenderTarget& backendRT,
839 GrSurfaceOrigin origin,
robertphillips4fd74ae2016-08-03 14:26:53 -0700840 sk_sp<SkColorSpace> colorSpace,
841 const SkSurfaceProps* surfaceProps) {
842 ASSERT_SINGLE_OWNER_PRIV
843
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400844 sk_sp<GrRenderTarget> rt(fContext->resourceProvider()->wrapBackendRenderTarget(backendRT));
robertphillips4fd74ae2016-08-03 14:26:53 -0700845 if (!rt) {
846 return nullptr;
847 }
848
Robert Phillips066f0202017-07-25 10:16:35 -0400849 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(rt), origin));
Robert Phillips77b3f322017-01-31 18:24:12 -0500850 if (!proxy) {
851 return nullptr;
852 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400853
Robert Phillips37430132016-11-09 06:50:43 -0500854 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400855 std::move(colorSpace),
Brian Osman11052242016-10-27 14:47:55 -0400856 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -0700857}
858
Brian Osman11052242016-10-27 14:47:55 -0400859sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000860 const GrBackendTexture& tex,
861 GrSurfaceOrigin origin,
862 int sampleCnt,
robertphillips4fd74ae2016-08-03 14:26:53 -0700863 sk_sp<SkColorSpace> colorSpace,
864 const SkSurfaceProps* surfaceProps) {
865 ASSERT_SINGLE_OWNER_PRIV
robertphillips4fd74ae2016-08-03 14:26:53 -0700866
Greg Daniel7ef28f32017-04-20 16:41:55 +0000867 sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTextureAsRenderTarget(
868 tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000869 sampleCnt));
robertphillips4fd74ae2016-08-03 14:26:53 -0700870 if (!surface) {
871 return nullptr;
872 }
873
Robert Phillips066f0202017-07-25 10:16:35 -0400874 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface), origin));
Robert Phillips77b3f322017-01-31 18:24:12 -0500875 if (!proxy) {
876 return nullptr;
877 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400878
Robert Phillips37430132016-11-09 06:50:43 -0500879 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400880 std::move(colorSpace),
881 surfaceProps);
robertphillips77a2e522015-10-17 07:43:27 -0700882}
883
Chris Daltonfe199b72017-05-05 11:26:15 -0400884void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
885 fContext->fDrawingManager->addOnFlushCallbackObject(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400886}
887
888
robertphillips48fde9c2016-09-06 05:20:20 -0700889static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) {
Brian Osman78f20e02017-01-12 10:28:01 -0500890 switch (config) {
891 case kAlpha_8_GrPixelConfig:
892 case kRGB_565_GrPixelConfig:
893 case kRGBA_4444_GrPixelConfig:
894 case kBGRA_8888_GrPixelConfig:
895 return kRGBA_8888_GrPixelConfig;
896 case kSBGRA_8888_GrPixelConfig:
897 return kSRGBA_8888_GrPixelConfig;
898 case kAlpha_half_GrPixelConfig:
899 return kRGBA_half_GrPixelConfig;
900 default:
901 return kUnknown_GrPixelConfig;
902 }
robertphillips48fde9c2016-09-06 05:20:20 -0700903}
904
robertphillipsd728f0c2016-11-21 11:05:03 -0800905sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContextWithFallback(
906 SkBackingFit fit,
907 int width, int height,
908 GrPixelConfig config,
909 sk_sp<SkColorSpace> colorSpace,
910 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -0400911 GrMipMapped mipMapped,
robertphillipsd728f0c2016-11-21 11:05:03 -0800912 GrSurfaceOrigin origin,
913 const SkSurfaceProps* surfaceProps,
914 SkBudgeted budgeted) {
915 if (!this->caps()->isConfigRenderable(config, sampleCnt > 0)) {
916 config = GrPixelConfigFallback(config);
917 }
918
919 return this->makeDeferredRenderTargetContext(fit, width, height, config, std::move(colorSpace),
Greg Daniel45d63032017-10-30 13:41:26 -0400920 sampleCnt, mipMapped, origin, surfaceProps,
Greg Daniele1da1d92017-10-06 15:59:27 -0400921 budgeted);
robertphillipsd728f0c2016-11-21 11:05:03 -0800922}
923
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400924sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContext(
925 SkBackingFit fit,
926 int width, int height,
927 GrPixelConfig config,
928 sk_sp<SkColorSpace> colorSpace,
929 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -0400930 GrMipMapped mipMapped,
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400931 GrSurfaceOrigin origin,
932 const SkSurfaceProps* surfaceProps,
933 SkBudgeted budgeted) {
Brian Salomon79e4d1b2017-07-13 11:17:11 -0400934 if (this->abandoned()) {
935 return nullptr;
936 }
937
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400938 GrSurfaceDesc desc;
939 desc.fFlags = kRenderTarget_GrSurfaceFlag;
940 desc.fOrigin = origin;
941 desc.fWidth = width;
942 desc.fHeight = height;
943 desc.fConfig = config;
944 desc.fSampleCnt = sampleCnt;
945
Greg Daniele1da1d92017-10-06 15:59:27 -0400946 sk_sp<GrTextureProxy> rtp;
Greg Daniel45d63032017-10-30 13:41:26 -0400947 if (GrMipMapped::kNo == mipMapped) {
Greg Daniele1da1d92017-10-06 15:59:27 -0400948 rtp = GrSurfaceProxy::MakeDeferred(this->resourceProvider(), desc, fit, budgeted);
949 } else {
950 rtp = GrSurfaceProxy::MakeDeferredMipMap(this->resourceProvider(), desc, budgeted);
951 }
Robert Phillips08c5ec72017-01-30 12:26:47 -0500952 if (!rtp) {
953 return nullptr;
954 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400955
Robert Phillips1119dc32017-04-11 12:54:57 -0400956 sk_sp<GrRenderTargetContext> renderTargetContext(
957 fDrawingManager->makeRenderTargetContext(std::move(rtp),
958 std::move(colorSpace),
959 surfaceProps));
Robert Phillips1119dc32017-04-11 12:54:57 -0400960 if (!renderTargetContext) {
961 return nullptr;
962 }
963
964 renderTargetContext->discard();
965
966 return renderTargetContext;
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400967}
968
joshualitt1de610a2016-01-06 08:26:09 -0800969bool GrContext::abandoned() const {
970 ASSERT_SINGLE_OWNER
robertphillips7761d612016-05-16 09:14:53 -0700971 return fDrawingManager->wasAbandoned();
robertphillips77a2e522015-10-17 07:43:27 -0700972}
973
Brian Salomonaff329b2017-08-11 09:40:37 -0400974std::unique_ptr<GrFragmentProcessor> GrContext::createPMToUPMEffect(
975 std::unique_ptr<GrFragmentProcessor> fp, bool useConfigConversionEffect) {
Robert Phillips757914d2017-01-25 15:48:30 -0500976 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -0400977 // We have specialized effects that guarantee round-trip conversion for some formats
978 if (useConfigConversionEffect) {
979 // We should have already called this->validPMUPMConversionExists() in this case
980 SkASSERT(fDidTestPMConversions);
981 // ...and it should have succeeded
982 SkASSERT(this->validPMUPMConversionExists());
983
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500984 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul);
Brian Osman2d2da4f2017-04-12 17:07:22 -0400985 } else {
986 // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and
987 // explicitly round the results. Just do the obvious, naive thing in the shader.
988 return GrFragmentProcessor::UnpremulOutput(std::move(fp));
Robert Phillips757914d2017-01-25 15:48:30 -0500989 }
990}
991
Brian Salomonaff329b2017-08-11 09:40:37 -0400992std::unique_ptr<GrFragmentProcessor> GrContext::createUPMToPMEffect(
993 std::unique_ptr<GrFragmentProcessor> fp, bool useConfigConversionEffect) {
joshualitt1de610a2016-01-06 08:26:09 -0800994 ASSERT_SINGLE_OWNER
Brian Osman2d2da4f2017-04-12 17:07:22 -0400995 // We have specialized effects that guarantee round-trip conversion for these formats
Brian Osman409e74f2017-04-17 11:48:28 -0400996 if (useConfigConversionEffect) {
997 // We should have already called this->validPMUPMConversionExists() in this case
998 SkASSERT(fDidTestPMConversions);
999 // ...and it should have succeeded
1000 SkASSERT(this->validPMUPMConversionExists());
1001
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001002 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
Brian Osman2d2da4f2017-04-12 17:07:22 -04001003 } else {
1004 // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and
1005 // explicitly round the results. Just do the obvious, naive thing in the shader.
1006 return GrFragmentProcessor::PremulOutput(std::move(fp));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001007 }
1008}
1009
Brian Osman409e74f2017-04-17 11:48:28 -04001010bool GrContext::validPMUPMConversionExists() {
joshualitt1de610a2016-01-06 08:26:09 -08001011 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -04001012 if (!fDidTestPMConversions) {
Brian Osman28804f32017-04-20 10:24:36 -04001013 fPMUPMConversionsRoundTrip = GrConfigConversionEffect::TestForPreservingPMConversions(this);
Brian Osman409e74f2017-04-17 11:48:28 -04001014 fDidTestPMConversions = true;
1015 }
1016
bsalomon636e8022015-07-29 06:08:46 -07001017 // The PM<->UPM tests fail or succeed together so we only need to check one.
Brian Osman28804f32017-04-20 10:24:36 -04001018 return fPMUPMConversionsRoundTrip;
bsalomon636e8022015-07-29 06:08:46 -07001019}
1020
bsalomon37f9a262015-02-02 13:00:10 -08001021//////////////////////////////////////////////////////////////////////////////
1022
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001023void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -08001024 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001025 if (maxResources) {
1026 *maxResources = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -08001027 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001028 if (maxResourceBytes) {
1029 *maxResourceBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -08001030 }
1031}
1032
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001033void GrContext::setResourceCacheLimits(int maxResources, size_t maxResourceBytes) {
joshualitt1de610a2016-01-06 08:26:09 -08001034 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001035 fResourceCache->setLimits(maxResources, maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -08001036}
1037
ericrk0a5fa482015-09-15 14:16:10 -07001038//////////////////////////////////////////////////////////////////////////////
1039
1040void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -08001041 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -07001042 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
1043}
Brian Osman71a18892017-08-10 10:23:25 -04001044
1045//////////////////////////////////////////////////////////////////////////////
1046
1047SkString GrContext::dump() const {
1048 SkDynamicMemoryWStream stream;
1049 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
1050 writer.beginObject();
1051
1052 static const char* kBackendStr[] = {
1053 "Metal",
1054 "OpenGL",
1055 "Vulkan",
1056 "Mock",
1057 };
1058 GR_STATIC_ASSERT(0 == kMetal_GrBackend);
1059 GR_STATIC_ASSERT(1 == kOpenGL_GrBackend);
1060 GR_STATIC_ASSERT(2 == kVulkan_GrBackend);
1061 GR_STATIC_ASSERT(3 == kMock_GrBackend);
1062 writer.appendString("backend", kBackendStr[fBackend]);
1063
1064 writer.appendName("caps");
1065 fCaps->dumpJSON(&writer);
1066
1067 writer.appendName("gpu");
1068 fGpu->dumpJSON(&writer);
1069
1070 // Flush JSON to the memory stream
1071 writer.endObject();
1072 writer.flush();
1073
1074 // Null terminate the JSON data in the memory stream
1075 stream.write8(0);
1076
1077 // Allocate a string big enough to hold all the data, then copy out of the stream
1078 SkString result(stream.bytesWritten());
1079 stream.copyToAndReset(result.writable_str());
1080 return result;
1081}