blob: 388c9411b19fee12f3650beabdceed999c72c3d7 [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
Brian Salomon990014d2017-12-07 16:23:39 -050097sk_sp<GrContext> GrContext::MakeGL(const GrGLInterface* interface) {
98 return MakeGL(sk_ref_sp(interface));
99}
100
101sk_sp<GrContext> GrContext::MakeGL(const GrGLInterface* interface,
102 const GrContextOptions& options) {
103 return MakeGL(sk_ref_sp(interface), options);
104}
105
Greg Daniel02611d92017-07-25 10:05:01 -0400106sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions) {
107 GrContextOptions defaultOptions;
108 return MakeMock(mockOptions, defaultOptions);
109}
110
111sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions,
112 const GrContextOptions& options) {
113 sk_sp<GrContext> context(new GrContext);
Brian Salomon384fab42017-12-07 12:33:05 -0500114 context->fGpu = GrMockGpu::Make(mockOptions, options, context.get());
Greg Daniel02611d92017-07-25 10:05:01 -0400115 if (!context->fGpu) {
116 return nullptr;
117 }
118 context->fBackend = kMock_GrBackend;
119 if (!context->init(options)) {
120 return nullptr;
121 }
122 return context;
123}
124
125#ifdef SK_VULKAN
Brian Salomon384fab42017-12-07 12:33:05 -0500126sk_sp<GrContext> GrContext::MakeVulkan(sk_sp<const GrVkBackendContext> backendContext) {
Greg Daniel02611d92017-07-25 10:05:01 -0400127 GrContextOptions defaultOptions;
Brian Salomon384fab42017-12-07 12:33:05 -0500128 return MakeVulkan(std::move(backendContext), defaultOptions);
Greg Daniel02611d92017-07-25 10:05:01 -0400129}
130
Brian Salomon384fab42017-12-07 12:33:05 -0500131sk_sp<GrContext> GrContext::MakeVulkan(sk_sp<const GrVkBackendContext> backendContext,
Greg Daniel02611d92017-07-25 10:05:01 -0400132 const GrContextOptions& options) {
133 sk_sp<GrContext> context(new GrContext);
Brian Salomon384fab42017-12-07 12:33:05 -0500134 context->fGpu = GrVkGpu::Make(std::move(backendContext), options, context.get());
Greg Daniel02611d92017-07-25 10:05:01 -0400135 if (!context->fGpu) {
136 return nullptr;
137 }
138 context->fBackend = kVulkan_GrBackend;
139 if (!context->init(options)) {
140 return nullptr;
141 }
142 return context;
143}
144#endif
145
Greg Danielb76a72a2017-07-13 15:07:54 -0400146#ifdef SK_METAL
Greg Daniel02611d92017-07-25 10:05:01 -0400147sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue) {
148 GrContextOptions defaultOptions;
149 return MakeMetal(device, queue, defaultOptions);
150}
151
Greg Danielb76a72a2017-07-13 15:07:54 -0400152sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) {
153 sk_sp<GrContext> context(new GrContext);
Brian Salomon384fab42017-12-07 12:33:05 -0500154 context->fGpu = GrMtlTrampoline::MakeGpu(context.get(), options, device, queue);
Greg Danielb76a72a2017-07-13 15:07:54 -0400155 if (!context->fGpu) {
156 return nullptr;
157 }
158 context->fBackend = kMetal_GrBackend;
159 if (!context->init(options)) {
160 return nullptr;
161 }
162 return context;
163}
164#endif
165
joshualitt0acd0d32015-05-07 08:23:19 -0700166static int32_t gNextID = 1;
167static int32_t next_id() {
168 int32_t id;
169 do {
170 id = sk_atomic_inc(&gNextID);
171 } while (id == SK_InvalidGenID);
172 return id;
173}
174
bsalomon682c2692015-05-22 14:01:46 -0700175GrContext::GrContext() : fUniqueID(next_id()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700176 fResourceCache = nullptr;
177 fResourceProvider = nullptr;
Brian Salomonf856fd12016-12-16 14:24:34 -0500178 fAtlasGlyphCache = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000179}
180
bsalomon682c2692015-05-22 14:01:46 -0700181bool GrContext::init(GrBackend backend, GrBackendContext backendContext,
182 const GrContextOptions& options) {
joshualitt1de610a2016-01-06 08:26:09 -0800183 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700184 SkASSERT(!fGpu);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000185
Greg Danielfc978fd2017-04-13 09:54:12 -0400186 fBackend = backend;
187
Brian Salomon384fab42017-12-07 12:33:05 -0500188 fGpu = GrGpu::Make(backend, backendContext, options, this);
robertphillipsea461502015-05-26 11:38:03 -0700189 if (!fGpu) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000190 return false;
191 }
Greg Danielb76a72a2017-07-13 15:07:54 -0400192 return this->init(options);
193}
joshualitt1de610a2016-01-06 08:26:09 -0800194
Greg Danielb76a72a2017-07-13 15:07:54 -0400195bool GrContext::init(const GrContextOptions& options) {
196 ASSERT_SINGLE_OWNER
Brian Salomoneace8cd2017-12-08 13:11:11 -0500197 fCaps = sk_ref_sp(fGpu->caps());
198 fResourceCache = new GrResourceCache(fCaps.get(), fUniqueID);
Brian Salomon384fab42017-12-07 12:33:05 -0500199 fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, &fSingleOwner);
commit-bot@chromium.org1836d332013-07-16 22:55:03 +0000200
Brian Osman46da1cc2017-02-14 14:15:48 -0500201 fDisableGpuYUVConversion = options.fDisableGpuYUVConversion;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000202 fDidTestPMConversions = false;
203
bsalomon6b2552f2016-09-15 13:50:26 -0700204 GrPathRendererChain::Options prcOptions;
bsalomon39ef7fb2016-09-21 11:16:05 -0700205 prcOptions.fAllowPathMaskCaching = options.fAllowPathMaskCaching;
Brian Osman195c05b2017-08-30 15:14:04 -0400206#if GR_TEST_UTILS
csmartdalton008b9d82017-02-22 12:00:42 -0700207 prcOptions.fGpuPathRenderers = options.fGpuPathRenderers;
Brian Osman195c05b2017-08-30 15:14:04 -0400208#endif
Brian Osmanb350ae22017-08-29 16:15:39 -0400209 if (options.fDisableDistanceFieldPaths) {
Brian Osman195c05b2017-08-30 15:14:04 -0400210 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
Brian Osmanb350ae22017-08-29 16:15:39 -0400211 }
Brian Salomonaf597482017-11-07 16:23:34 -0500212
213 GrAtlasTextContext::Options atlasTextContextOptions;
214 atlasTextContextOptions.fMaxDistanceFieldFontSize = options.fGlyphsAsPathsFontSize;
215 atlasTextContextOptions.fMinDistanceFieldFontSize = options.fMinDistanceFieldFontSize;
216
217 fDrawingManager.reset(
218 new GrDrawingManager(this, prcOptions, atlasTextContextOptions, &fSingleOwner));
joshualitt7c3a2f82015-03-31 13:32:05 -0700219
Brian Salomon9f545bc2017-11-06 10:36:57 -0500220 GrDrawOpAtlas::AllowMultitexturing allowMultitexturing;
Jim Van Verthc02cb8a2017-11-20 22:11:30 +0000221 switch (options.fAllowMultipleGlyphCacheTextures) {
222 case GrContextOptions::Enable::kDefault:
223#ifdef SK_BUILD_FOR_IOS
224 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
225#else
226 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
227#endif
228 break;
229 case GrContextOptions::Enable::kNo:
230 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
231 break;
232 case GrContextOptions::Enable::kYes:
233 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
234 break;
Brian Salomon9f545bc2017-11-06 10:36:57 -0500235 }
236 fAtlasGlyphCache = new GrAtlasGlyphCache(this, options.fGlyphCacheTextureMaximumBytes,
237 allowMultitexturing);
Jim Van Verth106b5c42017-09-26 12:45:29 -0400238 this->contextPriv().addOnFlushCallbackObject(fAtlasGlyphCache);
joshualittb7133be2015-04-08 09:08:31 -0700239
halcanary385fe4d2015-08-26 13:07:48 -0700240 fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB, this));
Brian Salomon91a3e522017-06-23 10:58:19 -0400241
Brian Osman51279982017-08-23 10:12:00 -0400242 if (options.fExecutor) {
243 fTaskGroup = skstd::make_unique<SkTaskGroup>(*options.fExecutor);
244 }
245
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400246 fPersistentCache = options.fPersistentCache;
247
Brian Salomon91a3e522017-06-23 10:58:19 -0400248 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000249}
250
bsalomon@google.com27847de2011-02-22 20:59:41 +0000251GrContext::~GrContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800252 ASSERT_SINGLE_OWNER
253
robertphillipsea461502015-05-26 11:38:03 -0700254 if (!fGpu) {
bsalomon76228632015-05-29 08:02:10 -0700255 SkASSERT(!fCaps);
bsalomon@google.com733c0622013-04-24 17:59:32 +0000256 return;
257 }
258
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000259 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000260
robertphillips77a2e522015-10-17 07:43:27 -0700261 fDrawingManager->cleanup();
robertphillips2334fb62015-06-17 05:43:33 -0700262
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000263 for (int i = 0; i < fCleanUpData.count(); ++i) {
264 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
265 }
266
halcanary385fe4d2015-08-26 13:07:48 -0700267 delete fResourceProvider;
268 delete fResourceCache;
Brian Salomonf856fd12016-12-16 14:24:34 -0500269 delete fAtlasGlyphCache;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000270}
271
bungeman6bd52842016-10-27 09:30:08 -0700272sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
bsalomon41b952c2016-03-11 06:46:33 -0800273 if (!fThreadSafeProxy) {
Brian Salomoneace8cd2017-12-08 13:11:11 -0500274 fThreadSafeProxy.reset(new GrContextThreadSafeProxy(fCaps, this->uniqueID()));
bsalomon41b952c2016-03-11 06:46:33 -0800275 }
bungeman6bd52842016-10-27 09:30:08 -0700276 return fThreadSafeProxy;
bsalomon41b952c2016-03-11 06:46:33 -0800277}
278
bsalomon2354f842014-07-28 13:48:36 -0700279void GrContext::abandonContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800280 ASSERT_SINGLE_OWNER
281
bsalomond309e7a2015-04-30 14:18:54 -0700282 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800283
284 // Need to abandon the drawing manager first so all the render targets
285 // will be released/forgotten before they too are abandoned.
286 fDrawingManager->abandon();
287
bsalomon@google.com205d4602011-04-25 12:43:45 +0000288 // abandon first to so destructors
289 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800290 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700291
bsalomon6e2aad42016-04-01 11:54:31 -0700292 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
293
Brian Salomonf856fd12016-12-16 14:24:34 -0500294 fAtlasGlyphCache->freeAll();
bsalomon6e2aad42016-04-01 11:54:31 -0700295 fTextBlobCache->freeAll();
296}
297
298void GrContext::releaseResourcesAndAbandonContext() {
299 ASSERT_SINGLE_OWNER
300
301 fResourceProvider->abandon();
302
303 // Need to abandon the drawing manager first so all the render targets
304 // will be released/forgotten before they too are abandoned.
305 fDrawingManager->abandon();
306
307 // Release all resources in the backend 3D API.
308 fResourceCache->releaseAll();
309
310 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000311
Brian Salomonf856fd12016-12-16 14:24:34 -0500312 fAtlasGlyphCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700313 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000314}
315
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000316void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800317 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000318 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000319}
320
321void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800322 ASSERT_SINGLE_OWNER
323
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000324 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000325
Brian Salomonf856fd12016-12-16 14:24:34 -0500326 fAtlasGlyphCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700327
328 fDrawingManager->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700329
330 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000331}
332
Brian Salomon5e150852017-03-22 14:53:13 -0400333void GrContext::purgeResourcesNotUsedInMs(std::chrono::milliseconds ms) {
334 ASSERT_SINGLE_OWNER
335 fResourceCache->purgeResourcesNotUsedSince(GrStdSteadyClock::now() - ms);
336}
337
Derek Sollenberger5480a182017-05-25 16:43:59 -0400338void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
339 ASSERT_SINGLE_OWNER
340 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
341}
342
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000343void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800344 ASSERT_SINGLE_OWNER
345
bsalomon71cb0c22014-11-14 12:10:14 -0800346 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800347 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800348 }
349 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800350 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800351 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000352}
353
Derek Sollenbergeree479142017-05-24 11:41:33 -0400354size_t GrContext::getResourceCachePurgeableBytes() const {
355 ASSERT_SINGLE_OWNER
356 return fResourceCache->getPurgeableBytes();
357}
358
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000359////////////////////////////////////////////////////////////////////////////////
360
joshualitt0db6dfa2015-04-10 07:01:30 -0700361void GrContext::TextBlobCacheOverBudgetCB(void* data) {
362 SkASSERT(data);
Brian Osman11052242016-10-27 14:47:55 -0400363 // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on
364 // GrRenderTargetContext to perform a necessary flush. The solution is to move drawText calls
365 // to below the GrContext level, but this is not trivial because they call drawPath on
366 // SkGpuDevice.
joshualitt0db6dfa2015-04-10 07:01:30 -0700367 GrContext* context = reinterpret_cast<GrContext*>(data);
368 context->flush();
369}
370
bsalomon@google.com27847de2011-02-22 20:59:41 +0000371////////////////////////////////////////////////////////////////////////////////
372
bsalomonb77a9072016-09-07 10:02:04 -0700373void GrContext::flush() {
joshualitt1de610a2016-01-06 08:26:09 -0800374 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700375 RETURN_IF_ABANDONED
Robert Phillips7ee385e2017-03-30 08:02:11 -0400376
377 fDrawingManager->flush(nullptr);
378}
379
Greg Daniel51316782017-08-02 15:10:09 +0000380GrSemaphoresSubmitted GrContext::flushAndSignalSemaphores(int numSemaphores,
381 GrBackendSemaphore signalSemaphores[]) {
382 ASSERT_SINGLE_OWNER
383 if (fDrawingManager->wasAbandoned()) { return GrSemaphoresSubmitted::kNo; }
384
385 return fDrawingManager->flush(nullptr, numSemaphores, signalSemaphores);
386}
387
Robert Phillips7ee385e2017-03-30 08:02:11 -0400388void GrContextPriv::flush(GrSurfaceProxy* proxy) {
389 ASSERT_SINGLE_OWNER_PRIV
390 RETURN_IF_ABANDONED_PRIV
391 ASSERT_OWNED_PROXY_PRIV(proxy);
392
393 fContext->fDrawingManager->flush(proxy);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000394}
395
bsalomon81beccc2014-10-13 12:32:55 -0700396bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
397 const void* inPixels, size_t outRowBytes, void* outPixels) {
Matt Sarettc7b29082017-02-09 16:22:39 -0500398 SkColorType colorType;
399 if (!GrPixelConfigToColorType(srcConfig, &colorType) ||
400 4 != SkColorTypeBytesPerPixel(colorType))
401 {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000402 return false;
403 }
bsalomon81beccc2014-10-13 12:32:55 -0700404
Matt Sarettc7b29082017-02-09 16:22:39 -0500405 for (int y = 0; y < height; y++) {
406 SkOpts::RGBA_to_rgbA((uint32_t*) outPixels, inPixels, width);
407 outPixels = SkTAddOffset<void>(outPixels, outRowBytes);
408 inPixels = SkTAddOffset<const void>(inPixels, inRowBytes);
409 }
bsalomon81beccc2014-10-13 12:32:55 -0700410
Matt Sarettc7b29082017-02-09 16:22:39 -0500411 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000412}
413
Brian Osmand2ca59a2017-04-13 14:03:57 -0400414static bool valid_premul_config(GrPixelConfig config) {
Brian Osmance425512017-03-22 14:37:50 -0400415 return GrPixelConfigIs8888Unorm(config) || kRGBA_half_GrPixelConfig == config;
416}
417
Brian Osmand2ca59a2017-04-13 14:03:57 -0400418static bool valid_pixel_conversion(GrPixelConfig srcConfig, GrPixelConfig dstConfig,
419 bool premulConversion) {
420 // We don't allow conversion between integer configs and float/fixed configs.
421 if (GrPixelConfigIsSint(srcConfig) != GrPixelConfigIsSint(dstConfig)) {
422 return false;
423 }
424
425 // We only allow premul <-> unpremul conversions for some formats
426 if (premulConversion && (!valid_premul_config(srcConfig) || !valid_premul_config(dstConfig))) {
427 return false;
428 }
429
430 return true;
431}
432
Brian Osman409e74f2017-04-17 11:48:28 -0400433static bool pm_upm_must_round_trip(GrPixelConfig config, SkColorSpace* colorSpace) {
434 return !colorSpace &&
435 (kRGBA_8888_GrPixelConfig == config || kBGRA_8888_GrPixelConfig == config);
436}
437
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400438bool GrContextPriv::writeSurfacePixels(GrSurfaceContext* dst,
Robert Phillipse78b7252017-04-06 07:59:41 -0400439 int left, int top, int width, int height,
440 GrPixelConfig srcConfig, SkColorSpace* srcColorSpace,
441 const void* buffer, size_t rowBytes,
442 uint32_t pixelOpsFlags) {
Brian Osmanb62ea222016-12-22 11:12:16 -0500443 // TODO: Color space conversion
444
Robert Phillipse78b7252017-04-06 07:59:41 -0400445 ASSERT_SINGLE_OWNER_PRIV
446 RETURN_FALSE_IF_ABANDONED_PRIV
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400447 SkASSERT(dst);
448 ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy());
Brian Salomondcbb9d92017-07-19 10:53:20 -0400449 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "writeSurfacePixels", fContext);
bsalomon6c6f6582015-09-10 08:12:46 -0700450
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400451 if (!dst->asSurfaceProxy()->instantiate(fContext->resourceProvider())) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400452 return false;
453 }
454
Robert Phillips16d8ec62017-07-27 16:16:25 -0400455 GrSurfaceProxy* dstProxy = dst->asSurfaceProxy();
456 GrSurface* dstSurface = dstProxy->priv().peekSurface();
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400457
Brian Osmand2ca59a2017-04-13 14:03:57 -0400458 // 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 -0400459 const bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
Robert Phillips16d8ec62017-07-27 16:16:25 -0400460 if (!valid_pixel_conversion(srcConfig, dstProxy->config(), premul)) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400461 return false;
462 }
463
Brian Osman409e74f2017-04-17 11:48:28 -0400464 // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data,
465 // without any color spaces attached, and the caller wants us to premul.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400466 bool useConfigConversionEffect =
Brian Salomonf3569f02017-10-24 12:52:33 -0400467 premul && pm_upm_must_round_trip(srcConfig, srcColorSpace) &&
468 pm_upm_must_round_trip(dstProxy->config(), dst->colorSpaceInfo().colorSpace());
Brian Osman409e74f2017-04-17 11:48:28 -0400469
470 // Are we going to try to premul as part of a draw? For the non-legacy case, we always allow
471 // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly.
472 bool premulOnGpu = premul &&
473 (!useConfigConversionEffect || fContext->validPMUPMConversionExists());
bsalomon81beccc2014-10-13 12:32:55 -0700474
bsalomone8d21e82015-07-16 08:23:13 -0700475 // 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 -0700476 // necessary and because GrGpu::getWritePixelsInfo requires it.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400477 if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(),
bsalomone8d21e82015-07-16 08:23:13 -0700478 GrBytesPerPixel(srcConfig), &left, &top, &width,
479 &height, &buffer, &rowBytes)) {
480 return false;
481 }
482
Brian Osman409e74f2017-04-17 11:48:28 -0400483 GrGpu::DrawPreference drawPreference = premulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference
484 : GrGpu::kNoDraw_DrawPreference;
bsalomonf0674512015-07-28 13:26:15 -0700485 GrGpu::WritePixelTempDrawInfo tempDrawInfo;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400486 if (!fContext->fGpu->getWritePixelsInfo(dstSurface, dstProxy->origin(), width, height,
Robert Phillips16d8ec62017-07-27 16:16:25 -0400487 srcConfig, &drawPreference, &tempDrawInfo)) {
bsalomonf0674512015-07-28 13:26:15 -0700488 return false;
489 }
490
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400491 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && dstSurface->surfacePriv().hasPendingIO()) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400492 this->flush(nullptr); // MDB TODO: tighten this
bsalomonf0674512015-07-28 13:26:15 -0700493 }
494
Robert Phillips2f493142017-03-02 18:18:38 -0500495 sk_sp<GrTextureProxy> tempProxy;
bsalomonf0674512015-07-28 13:26:15 -0700496 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400497 tempProxy = GrSurfaceProxy::MakeDeferred(fContext->resourceProvider(),
Robert Phillips2f493142017-03-02 18:18:38 -0500498 tempDrawInfo.fTempSurfaceDesc,
499 SkBackingFit::kApprox,
500 SkBudgeted::kYes);
501 if (!tempProxy && GrGpu::kRequireDraw_DrawPreference == drawPreference) {
bsalomonf0674512015-07-28 13:26:15 -0700502 return false;
503 }
504 }
505
506 // temp buffer for doing sw premul conversion, if needed.
507 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
Brian Osman409e74f2017-04-17 11:48:28 -0400508 // We need to do sw premul if we were unable to create a RT for drawing, or if we can't do the
509 // premul on the GPU
510 if (premul && (!tempProxy || !premulOnGpu)) {
511 size_t tmpRowBytes = 4 * width;
512 tmpPixels.reset(width * height);
513 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
514 tmpPixels.get())) {
515 return false;
bsalomonf0674512015-07-28 13:26:15 -0700516 }
Brian Osman409e74f2017-04-17 11:48:28 -0400517 rowBytes = tmpRowBytes;
518 buffer = tmpPixels.get();
bsalomonf0674512015-07-28 13:26:15 -0700519 }
Brian Osman409e74f2017-04-17 11:48:28 -0400520
521 if (tempProxy) {
Brian Osman2240be92017-10-18 13:15:13 -0400522 auto fp = GrSimpleTextureEffect::Make(tempProxy, SkMatrix::I());
Brian Osman409e74f2017-04-17 11:48:28 -0400523 if (premulOnGpu) {
524 fp = fContext->createUPMToPMEffect(std::move(fp), useConfigConversionEffect);
bsalomon81beccc2014-10-13 12:32:55 -0700525 }
Brian Osman409e74f2017-04-17 11:48:28 -0400526 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400527 if (!fp) {
528 return false;
529 }
Brian Osman409e74f2017-04-17 11:48:28 -0400530
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400531 if (!tempProxy->instantiate(fContext->resourceProvider())) {
Brian Osman409e74f2017-04-17 11:48:28 -0400532 return false;
533 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400534 GrTexture* texture = tempProxy->priv().peekTexture();
Robert Phillips7bbbf622017-10-17 07:36:59 -0400535
536 if (tempProxy->priv().hasPendingIO()) {
537 this->flush(tempProxy.get());
538 }
539
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400540 if (!fContext->fGpu->writePixels(texture, tempProxy->origin(), 0, 0, width, height,
541 tempDrawInfo.fWriteConfig, buffer, rowBytes)) {
Brian Osman409e74f2017-04-17 11:48:28 -0400542 return false;
543 }
Robert Phillips7bbbf622017-10-17 07:36:59 -0400544 tempProxy = nullptr;
545
Brian Osman409e74f2017-04-17 11:48:28 -0400546 SkMatrix matrix;
547 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400548 GrRenderTargetContext* renderTargetContext = dst->asRenderTargetContext();
Brian Osman409e74f2017-04-17 11:48:28 -0400549 if (!renderTargetContext) {
550 return false;
551 }
552 GrPaint paint;
553 paint.addColorFragmentProcessor(std::move(fp));
554 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonf3569f02017-10-24 12:52:33 -0400555 paint.setAllowSRGBInputs(dst->colorSpaceInfo().isGammaCorrect() ||
556 GrPixelConfigIsSRGB(dst->colorSpaceInfo().config()));
Brian Osman409e74f2017-04-17 11:48:28 -0400557 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
558 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, matrix, rect,
559 nullptr);
560
561 if (kFlushWrites_PixelOp & pixelOpsFlags) {
562 this->flushSurfaceWrites(renderTargetContext->asRenderTargetProxy());
563 }
564 } else {
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400565 return fContext->fGpu->writePixels(dstSurface, dstProxy->origin(), left, top, width,
566 height, srcConfig, buffer, rowBytes);
bsalomon81beccc2014-10-13 12:32:55 -0700567 }
bsalomon81beccc2014-10-13 12:32:55 -0700568 return true;
569}
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000570
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400571bool GrContextPriv::readSurfacePixels(GrSurfaceContext* src,
Robert Phillipse78b7252017-04-06 07:59:41 -0400572 int left, int top, int width, int height,
573 GrPixelConfig dstConfig, SkColorSpace* dstColorSpace,
574 void* buffer, size_t rowBytes, uint32_t flags) {
Brian Osmanb62ea222016-12-22 11:12:16 -0500575 // TODO: Color space conversion
576
Robert Phillipse78b7252017-04-06 07:59:41 -0400577 ASSERT_SINGLE_OWNER_PRIV
578 RETURN_FALSE_IF_ABANDONED_PRIV
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400579 SkASSERT(src);
580 ASSERT_OWNED_PROXY_PRIV(src->asSurfaceProxy());
Brian Salomondcbb9d92017-07-19 10:53:20 -0400581 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "readSurfacePixels", fContext);
bsalomon32ab2602015-09-09 18:57:49 -0700582
Robert Phillipse78b7252017-04-06 07:59:41 -0400583 // MDB TODO: delay this instantiation until later in the method
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400584 if (!src->asSurfaceProxy()->instantiate(fContext->resourceProvider())) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400585 return false;
586 }
587
Robert Phillips16d8ec62017-07-27 16:16:25 -0400588 GrSurfaceProxy* srcProxy = src->asSurfaceProxy();
589 GrSurface* srcSurface = srcProxy->priv().peekSurface();
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400590
Brian Osmand2ca59a2017-04-13 14:03:57 -0400591 // The src is premul but the dst is unpremul -> unpremul the src after or as part of the read
592 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
Robert Phillips16d8ec62017-07-27 16:16:25 -0400593 if (!valid_pixel_conversion(srcProxy->config(), dstConfig, unpremul)) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400594 return false;
595 }
596
Brian Osman409e74f2017-04-17 11:48:28 -0400597 // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data,
598 // without any color spaces attached, and the caller wants us to unpremul.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400599 bool useConfigConversionEffect =
Brian Salomonf3569f02017-10-24 12:52:33 -0400600 unpremul &&
601 pm_upm_must_round_trip(srcProxy->config(), src->colorSpaceInfo().colorSpace()) &&
602 pm_upm_must_round_trip(dstConfig, dstColorSpace);
Brian Osman409e74f2017-04-17 11:48:28 -0400603
604 // Are we going to try to unpremul as part of a draw? For the non-legacy case, we always allow
605 // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly.
606 bool unpremulOnGpu = unpremul &&
607 (!useConfigConversionEffect || fContext->validPMUPMConversionExists());
bsalomon6c6f6582015-09-10 08:12:46 -0700608
bsalomone8d21e82015-07-16 08:23:13 -0700609 // Adjust the params so that if we wind up using an intermediate surface we've already done
610 // all the trimming and the temporary can be the min size required.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400611 if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(),
bsalomone8d21e82015-07-16 08:23:13 -0700612 GrBytesPerPixel(dstConfig), &left,
613 &top, &width, &height, &buffer, &rowBytes)) {
614 return false;
615 }
616
Brian Osman409e74f2017-04-17 11:48:28 -0400617 GrGpu::DrawPreference drawPreference = unpremulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference
618 : GrGpu::kNoDraw_DrawPreference;
bsalomon39826022015-07-23 08:07:21 -0700619 GrGpu::ReadPixelTempDrawInfo tempDrawInfo;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400620 if (!fContext->fGpu->getReadPixelsInfo(srcSurface, srcProxy->origin(), width, height, rowBytes,
Robert Phillips16d8ec62017-07-27 16:16:25 -0400621 dstConfig, &drawPreference, &tempDrawInfo)) {
bsalomon39826022015-07-23 08:07:21 -0700622 return false;
623 }
bsalomon191bcc02014-11-14 11:31:13 -0800624
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400625 if (!(kDontFlush_PixelOpsFlag & flags) && srcSurface->surfacePriv().hasPendingWrite()) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400626 this->flush(nullptr); // MDB TODO: tighten this
627 }
628
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400629 sk_sp<GrSurfaceProxy> proxyToRead = src->asSurfaceProxyRef();
bsalomon39826022015-07-23 08:07:21 -0700630 bool didTempDraw = false;
631 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomonb117ff12016-07-19 07:24:40 -0700632 if (SkBackingFit::kExact == tempDrawInfo.fTempSurfaceFit) {
bsalomon39826022015-07-23 08:07:21 -0700633 // We only respect this when the entire src is being read. Otherwise we can trigger too
634 // many odd ball texture sizes and trash the cache.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400635 if (width != srcSurface->width() || height != srcSurface->height()) {
bsalomonb117ff12016-07-19 07:24:40 -0700636 tempDrawInfo.fTempSurfaceFit= SkBackingFit::kApprox;
bsalomon39826022015-07-23 08:07:21 -0700637 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000638 }
brianosmandfe4f2e2016-07-21 13:28:36 -0700639 // TODO: Need to decide the semantics of this function for color spaces. Do we support
640 // conversion to a passed-in color space? For now, specifying nullptr means that this
641 // path will do no conversion, so it will match the behavior of the non-draw path.
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400642 sk_sp<GrRenderTargetContext> tempRTC = fContext->makeDeferredRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400643 tempDrawInfo.fTempSurfaceFit,
bsalomonb117ff12016-07-19 07:24:40 -0700644 tempDrawInfo.fTempSurfaceDesc.fWidth,
645 tempDrawInfo.fTempSurfaceDesc.fHeight,
646 tempDrawInfo.fTempSurfaceDesc.fConfig,
brianosmandfe4f2e2016-07-21 13:28:36 -0700647 nullptr,
bsalomonb117ff12016-07-19 07:24:40 -0700648 tempDrawInfo.fTempSurfaceDesc.fSampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -0400649 GrMipMapped::kNo,
bsalomonb117ff12016-07-19 07:24:40 -0700650 tempDrawInfo.fTempSurfaceDesc.fOrigin);
Brian Osman693a5402016-10-27 15:13:22 -0400651 if (tempRTC) {
Robert Phillips67c18d62017-01-20 12:44:06 -0500652 SkMatrix textureMatrix = SkMatrix::MakeTrans(SkIntToScalar(left), SkIntToScalar(top));
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400653 sk_sp<GrTextureProxy> proxy = src->asTextureProxyRef();
Brian Osman2240be92017-10-18 13:15:13 -0400654 auto fp = GrSimpleTextureEffect::Make(std::move(proxy), textureMatrix);
Brian Osman409e74f2017-04-17 11:48:28 -0400655 if (unpremulOnGpu) {
656 fp = fContext->createPMToUPMEffect(std::move(fp), useConfigConversionEffect);
657 // We no longer need to do this on CPU after the read back.
658 unpremul = false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000659 }
Brian Osman409e74f2017-04-17 11:48:28 -0400660 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400661 if (!fp) {
662 return false;
663 }
Brian Osman60cd57e2017-04-06 10:19:06 -0400664
Brian Osman409e74f2017-04-17 11:48:28 -0400665 GrPaint paint;
666 paint.addColorFragmentProcessor(std::move(fp));
667 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
668 paint.setAllowSRGBInputs(true);
669 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
670 tempRTC->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect,
671 nullptr);
672 proxyToRead = tempRTC->asTextureProxyRef();
673 left = 0;
674 top = 0;
675 didTempDraw = true;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000676 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000677 }
joshualitt5c55fef2014-10-31 14:04:35 -0700678
Robert Phillipse78b7252017-04-06 07:59:41 -0400679 if (!proxyToRead) {
680 return false;
681 }
682
bsalomon39826022015-07-23 08:07:21 -0700683 if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000684 return false;
685 }
bsalomon39826022015-07-23 08:07:21 -0700686 GrPixelConfig configToRead = dstConfig;
687 if (didTempDraw) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400688 this->flushSurfaceWrites(proxyToRead.get());
bsalomon6c9cd552016-01-22 07:17:34 -0800689 configToRead = tempDrawInfo.fReadConfig;
bsalomon39826022015-07-23 08:07:21 -0700690 }
Robert Phillips09dfc472017-09-13 15:25:47 -0400691
692 if (!proxyToRead->instantiate(fContext->resourceProvider())) {
693 return false;
694 }
695
696 GrSurface* surfaceToRead = proxyToRead->priv().peekSurface();
697
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400698 if (!fContext->fGpu->readPixels(surfaceToRead, proxyToRead->origin(),
Robert Phillips16d8ec62017-07-27 16:16:25 -0400699 left, top, width, height, configToRead, buffer, rowBytes)) {
bsalomon39826022015-07-23 08:07:21 -0700700 return false;
701 }
702
703 // Perform umpremul conversion if we weren't able to perform it as a draw.
704 if (unpremul) {
Matt Sarettc7b29082017-02-09 16:22:39 -0500705 SkColorType colorType;
706 if (!GrPixelConfigToColorType(dstConfig, &colorType) ||
707 4 != SkColorTypeBytesPerPixel(colorType))
708 {
reed@google.com7111d462014-03-25 16:20:24 +0000709 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000710 }
reed@google.com7111d462014-03-25 16:20:24 +0000711
Matt Sarettc7b29082017-02-09 16:22:39 -0500712 for (int y = 0; y < height; y++) {
713 SkUnpremultiplyRow<false>((uint32_t*) buffer, (const uint32_t*) buffer, width);
714 buffer = SkTAddOffset<void>(buffer, rowBytes);
715 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000716 }
717 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000718}
719
Robert Phillips7ee385e2017-03-30 08:02:11 -0400720void GrContextPriv::prepareSurfaceForExternalIO(GrSurfaceProxy* proxy) {
721 ASSERT_SINGLE_OWNER_PRIV
722 RETURN_IF_ABANDONED_PRIV
723 SkASSERT(proxy);
724 ASSERT_OWNED_PROXY_PRIV(proxy);
Greg Daniel51316782017-08-02 15:10:09 +0000725 fContext->fDrawingManager->prepareSurfaceForExternalIO(proxy, 0, nullptr);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000726}
727
Robert Phillips7ee385e2017-03-30 08:02:11 -0400728void GrContextPriv::flushSurfaceWrites(GrSurfaceProxy* proxy) {
729 ASSERT_SINGLE_OWNER_PRIV
730 RETURN_IF_ABANDONED_PRIV
731 SkASSERT(proxy);
732 ASSERT_OWNED_PROXY_PRIV(proxy);
733 if (proxy->priv().hasPendingWrite()) {
734 this->flush(proxy);
bsalomonf80bfed2014-10-07 05:56:02 -0700735 }
736}
737
Robert Phillips7ee385e2017-03-30 08:02:11 -0400738void GrContextPriv::flushSurfaceIO(GrSurfaceProxy* proxy) {
739 ASSERT_SINGLE_OWNER_PRIV
740 RETURN_IF_ABANDONED_PRIV
741 SkASSERT(proxy);
742 ASSERT_OWNED_PROXY_PRIV(proxy);
743 if (proxy->priv().hasPendingIO()) {
744 this->flush(proxy);
ajuma95243eb2016-08-24 08:19:02 -0700745 }
746}
747
bsalomon@google.com27847de2011-02-22 20:59:41 +0000748////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000749int GrContext::getRecommendedSampleCount(GrPixelConfig config,
750 SkScalar dpi) const {
joshualitt1de610a2016-01-06 08:26:09 -0800751 ASSERT_SINGLE_OWNER
752
bsalomon76228632015-05-29 08:02:10 -0700753 if (!this->caps()->isConfigRenderable(config, true)) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000754 return 0;
755 }
756 int chosenSampleCount = 0;
jvanverthe9c0fc62015-04-29 11:18:05 -0700757 if (fGpu->caps()->shaderCaps()->pathRenderingSupport()) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000758 if (dpi >= 250.0f) {
759 chosenSampleCount = 4;
760 } else {
761 chosenSampleCount = 16;
762 }
763 }
Greg Daniel81e7bf82017-07-19 14:47:42 -0400764 int supportedSampleCount = fGpu->caps()->getSampleCount(chosenSampleCount, config);
765 return chosenSampleCount <= supportedSampleCount ? supportedSampleCount : 0;
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000766}
767
Robert Phillips2c862492017-01-18 10:08:39 -0500768sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy> proxy,
769 sk_sp<SkColorSpace> colorSpace) {
Brian Osman45580d32016-11-23 09:37:01 -0500770 ASSERT_SINGLE_OWNER_PRIV
771
Brian Osman45580d32016-11-23 09:37:01 -0500772 if (proxy->asRenderTargetProxy()) {
Robert Phillips2c862492017-01-18 10:08:39 -0500773 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
774 std::move(colorSpace), nullptr);
Brian Osman45580d32016-11-23 09:37:01 -0500775 } else {
776 SkASSERT(proxy->asTextureProxy());
Robert Phillips2c862492017-01-18 10:08:39 -0500777 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Brian Osman45580d32016-11-23 09:37:01 -0500778 }
779}
780
Robert Phillipse2f7d182016-12-15 09:23:05 -0500781sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(const GrSurfaceDesc& dstDesc,
Greg Daniel65c7f662017-10-30 13:39:09 -0400782 GrMipMapped mipMapped,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500783 SkBackingFit fit,
784 SkBudgeted isDstBudgeted) {
785
Greg Daniel65c7f662017-10-30 13:39:09 -0400786 sk_sp<GrTextureProxy> proxy;
787 if (GrMipMapped::kNo == mipMapped) {
788 proxy = GrSurfaceProxy::MakeDeferred(fContext->resourceProvider(), dstDesc, fit,
789 isDstBudgeted);
790 } else {
791 SkASSERT(SkBackingFit::kExact == fit);
792 proxy = GrSurfaceProxy::MakeDeferredMipMap(fContext->resourceProvider(), dstDesc,
793 isDstBudgeted);
794 }
Robert Phillips77b3f322017-01-31 18:24:12 -0500795 if (!proxy) {
796 return nullptr;
797 }
Robert Phillipse2f7d182016-12-15 09:23:05 -0500798
Robert Phillips2c862492017-01-18 10:08:39 -0500799 return this->makeWrappedSurfaceContext(std::move(proxy), nullptr);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500800}
801
Brian Salomond17f6582017-07-19 18:28:58 -0400802sk_sp<GrTextureContext> GrContextPriv::makeBackendTextureContext(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000803 GrSurfaceOrigin origin,
Brian Osmanc1e37052017-03-09 14:19:20 -0500804 sk_sp<SkColorSpace> colorSpace) {
Robert Phillips26caf892017-01-27 10:58:31 -0500805 ASSERT_SINGLE_OWNER_PRIV
806
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400807 sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTexture(tex));
Robert Phillips26caf892017-01-27 10:58:31 -0500808 if (!surface) {
809 return nullptr;
810 }
811
Robert Phillips066f0202017-07-25 10:16:35 -0400812 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface), origin));
Robert Phillips77b3f322017-01-31 18:24:12 -0500813 if (!proxy) {
814 return nullptr;
815 }
Robert Phillips26caf892017-01-27 10:58:31 -0500816
Brian Salomond17f6582017-07-19 18:28:58 -0400817 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Robert Phillips26caf892017-01-27 10:58:31 -0500818}
819
Brian Osman11052242016-10-27 14:47:55 -0400820sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000821 const GrBackendTexture& tex,
822 GrSurfaceOrigin origin,
823 int sampleCnt,
Brian Osman11052242016-10-27 14:47:55 -0400824 sk_sp<SkColorSpace> colorSpace,
Brian Osmanc1e37052017-03-09 14:19:20 -0500825 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700826 ASSERT_SINGLE_OWNER_PRIV
robertphillips4fd74ae2016-08-03 14:26:53 -0700827
Brian Salomond17f6582017-07-19 18:28:58 -0400828 sk_sp<GrSurface> surface(
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400829 fContext->resourceProvider()->wrapRenderableBackendTexture(tex, sampleCnt));
robertphillips4fd74ae2016-08-03 14:26:53 -0700830 if (!surface) {
831 return nullptr;
832 }
833
Robert Phillips066f0202017-07-25 10:16:35 -0400834 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface), origin));
Robert Phillips77b3f322017-01-31 18:24:12 -0500835 if (!proxy) {
836 return nullptr;
837 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400838
Robert Phillips37430132016-11-09 06:50:43 -0500839 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Brian Osman11052242016-10-27 14:47:55 -0400840 std::move(colorSpace), props);
robertphillips4fd74ae2016-08-03 14:26:53 -0700841}
842
Brian Osman11052242016-10-27 14:47:55 -0400843sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext(
Greg Danielbcf612b2017-05-01 13:50:58 +0000844 const GrBackendRenderTarget& backendRT,
845 GrSurfaceOrigin origin,
robertphillips4fd74ae2016-08-03 14:26:53 -0700846 sk_sp<SkColorSpace> colorSpace,
847 const SkSurfaceProps* surfaceProps) {
848 ASSERT_SINGLE_OWNER_PRIV
849
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400850 sk_sp<GrRenderTarget> rt(fContext->resourceProvider()->wrapBackendRenderTarget(backendRT));
robertphillips4fd74ae2016-08-03 14:26:53 -0700851 if (!rt) {
852 return nullptr;
853 }
854
Robert Phillips066f0202017-07-25 10:16:35 -0400855 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(rt), 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),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400861 std::move(colorSpace),
Brian Osman11052242016-10-27 14:47:55 -0400862 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -0700863}
864
Brian Osman11052242016-10-27 14:47:55 -0400865sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000866 const GrBackendTexture& tex,
867 GrSurfaceOrigin origin,
868 int sampleCnt,
robertphillips4fd74ae2016-08-03 14:26:53 -0700869 sk_sp<SkColorSpace> colorSpace,
870 const SkSurfaceProps* surfaceProps) {
871 ASSERT_SINGLE_OWNER_PRIV
robertphillips4fd74ae2016-08-03 14:26:53 -0700872
Greg Daniel7ef28f32017-04-20 16:41:55 +0000873 sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTextureAsRenderTarget(
874 tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000875 sampleCnt));
robertphillips4fd74ae2016-08-03 14:26:53 -0700876 if (!surface) {
877 return nullptr;
878 }
879
Robert Phillips066f0202017-07-25 10:16:35 -0400880 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface), origin));
Robert Phillips77b3f322017-01-31 18:24:12 -0500881 if (!proxy) {
882 return nullptr;
883 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400884
Robert Phillips37430132016-11-09 06:50:43 -0500885 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400886 std::move(colorSpace),
887 surfaceProps);
robertphillips77a2e522015-10-17 07:43:27 -0700888}
889
Chris Daltonfe199b72017-05-05 11:26:15 -0400890void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
891 fContext->fDrawingManager->addOnFlushCallbackObject(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400892}
893
894
robertphillips48fde9c2016-09-06 05:20:20 -0700895static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) {
Brian Osman78f20e02017-01-12 10:28:01 -0500896 switch (config) {
897 case kAlpha_8_GrPixelConfig:
898 case kRGB_565_GrPixelConfig:
899 case kRGBA_4444_GrPixelConfig:
900 case kBGRA_8888_GrPixelConfig:
901 return kRGBA_8888_GrPixelConfig;
902 case kSBGRA_8888_GrPixelConfig:
903 return kSRGBA_8888_GrPixelConfig;
904 case kAlpha_half_GrPixelConfig:
905 return kRGBA_half_GrPixelConfig;
906 default:
907 return kUnknown_GrPixelConfig;
908 }
robertphillips48fde9c2016-09-06 05:20:20 -0700909}
910
robertphillipsd728f0c2016-11-21 11:05:03 -0800911sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContextWithFallback(
912 SkBackingFit fit,
913 int width, int height,
914 GrPixelConfig config,
915 sk_sp<SkColorSpace> colorSpace,
916 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -0400917 GrMipMapped mipMapped,
robertphillipsd728f0c2016-11-21 11:05:03 -0800918 GrSurfaceOrigin origin,
919 const SkSurfaceProps* surfaceProps,
920 SkBudgeted budgeted) {
921 if (!this->caps()->isConfigRenderable(config, sampleCnt > 0)) {
922 config = GrPixelConfigFallback(config);
923 }
924
925 return this->makeDeferredRenderTargetContext(fit, width, height, config, std::move(colorSpace),
Greg Daniel45d63032017-10-30 13:41:26 -0400926 sampleCnt, mipMapped, origin, surfaceProps,
Greg Daniele1da1d92017-10-06 15:59:27 -0400927 budgeted);
robertphillipsd728f0c2016-11-21 11:05:03 -0800928}
929
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400930sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContext(
931 SkBackingFit fit,
932 int width, int height,
933 GrPixelConfig config,
934 sk_sp<SkColorSpace> colorSpace,
935 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -0400936 GrMipMapped mipMapped,
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400937 GrSurfaceOrigin origin,
938 const SkSurfaceProps* surfaceProps,
939 SkBudgeted budgeted) {
Brian Salomon79e4d1b2017-07-13 11:17:11 -0400940 if (this->abandoned()) {
941 return nullptr;
942 }
943
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400944 GrSurfaceDesc desc;
945 desc.fFlags = kRenderTarget_GrSurfaceFlag;
946 desc.fOrigin = origin;
947 desc.fWidth = width;
948 desc.fHeight = height;
949 desc.fConfig = config;
950 desc.fSampleCnt = sampleCnt;
951
Greg Daniele1da1d92017-10-06 15:59:27 -0400952 sk_sp<GrTextureProxy> rtp;
Greg Daniel45d63032017-10-30 13:41:26 -0400953 if (GrMipMapped::kNo == mipMapped) {
Greg Daniele1da1d92017-10-06 15:59:27 -0400954 rtp = GrSurfaceProxy::MakeDeferred(this->resourceProvider(), desc, fit, budgeted);
955 } else {
956 rtp = GrSurfaceProxy::MakeDeferredMipMap(this->resourceProvider(), desc, budgeted);
957 }
Robert Phillips08c5ec72017-01-30 12:26:47 -0500958 if (!rtp) {
959 return nullptr;
960 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400961
Robert Phillips1119dc32017-04-11 12:54:57 -0400962 sk_sp<GrRenderTargetContext> renderTargetContext(
963 fDrawingManager->makeRenderTargetContext(std::move(rtp),
964 std::move(colorSpace),
965 surfaceProps));
Robert Phillips1119dc32017-04-11 12:54:57 -0400966 if (!renderTargetContext) {
967 return nullptr;
968 }
969
970 renderTargetContext->discard();
971
972 return renderTargetContext;
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400973}
974
joshualitt1de610a2016-01-06 08:26:09 -0800975bool GrContext::abandoned() const {
976 ASSERT_SINGLE_OWNER
robertphillips7761d612016-05-16 09:14:53 -0700977 return fDrawingManager->wasAbandoned();
robertphillips77a2e522015-10-17 07:43:27 -0700978}
979
Brian Salomonaff329b2017-08-11 09:40:37 -0400980std::unique_ptr<GrFragmentProcessor> GrContext::createPMToUPMEffect(
981 std::unique_ptr<GrFragmentProcessor> fp, bool useConfigConversionEffect) {
Robert Phillips757914d2017-01-25 15:48:30 -0500982 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -0400983 // We have specialized effects that guarantee round-trip conversion for some formats
984 if (useConfigConversionEffect) {
985 // We should have already called this->validPMUPMConversionExists() in this case
986 SkASSERT(fDidTestPMConversions);
987 // ...and it should have succeeded
988 SkASSERT(this->validPMUPMConversionExists());
989
Ethan Nicholasaae47c82017-11-10 15:34:03 -0500990 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul);
Brian Osman2d2da4f2017-04-12 17:07:22 -0400991 } else {
992 // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and
993 // explicitly round the results. Just do the obvious, naive thing in the shader.
994 return GrFragmentProcessor::UnpremulOutput(std::move(fp));
Robert Phillips757914d2017-01-25 15:48:30 -0500995 }
996}
997
Brian Salomonaff329b2017-08-11 09:40:37 -0400998std::unique_ptr<GrFragmentProcessor> GrContext::createUPMToPMEffect(
999 std::unique_ptr<GrFragmentProcessor> fp, bool useConfigConversionEffect) {
joshualitt1de610a2016-01-06 08:26:09 -08001000 ASSERT_SINGLE_OWNER
Brian Osman2d2da4f2017-04-12 17:07:22 -04001001 // We have specialized effects that guarantee round-trip conversion for these formats
Brian Osman409e74f2017-04-17 11:48:28 -04001002 if (useConfigConversionEffect) {
1003 // We should have already called this->validPMUPMConversionExists() in this case
1004 SkASSERT(fDidTestPMConversions);
1005 // ...and it should have succeeded
1006 SkASSERT(this->validPMUPMConversionExists());
1007
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001008 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
Brian Osman2d2da4f2017-04-12 17:07:22 -04001009 } else {
1010 // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and
1011 // explicitly round the results. Just do the obvious, naive thing in the shader.
1012 return GrFragmentProcessor::PremulOutput(std::move(fp));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001013 }
1014}
1015
Brian Osman409e74f2017-04-17 11:48:28 -04001016bool GrContext::validPMUPMConversionExists() {
joshualitt1de610a2016-01-06 08:26:09 -08001017 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -04001018 if (!fDidTestPMConversions) {
Brian Osman28804f32017-04-20 10:24:36 -04001019 fPMUPMConversionsRoundTrip = GrConfigConversionEffect::TestForPreservingPMConversions(this);
Brian Osman409e74f2017-04-17 11:48:28 -04001020 fDidTestPMConversions = true;
1021 }
1022
bsalomon636e8022015-07-29 06:08:46 -07001023 // The PM<->UPM tests fail or succeed together so we only need to check one.
Brian Osman28804f32017-04-20 10:24:36 -04001024 return fPMUPMConversionsRoundTrip;
bsalomon636e8022015-07-29 06:08:46 -07001025}
1026
bsalomon37f9a262015-02-02 13:00:10 -08001027//////////////////////////////////////////////////////////////////////////////
1028
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001029void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -08001030 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001031 if (maxResources) {
1032 *maxResources = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -08001033 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001034 if (maxResourceBytes) {
1035 *maxResourceBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -08001036 }
1037}
1038
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001039void GrContext::setResourceCacheLimits(int maxResources, size_t maxResourceBytes) {
joshualitt1de610a2016-01-06 08:26:09 -08001040 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001041 fResourceCache->setLimits(maxResources, maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -08001042}
1043
ericrk0a5fa482015-09-15 14:16:10 -07001044//////////////////////////////////////////////////////////////////////////////
1045
1046void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -08001047 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -07001048 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
1049}
Brian Osman71a18892017-08-10 10:23:25 -04001050
1051//////////////////////////////////////////////////////////////////////////////
1052
1053SkString GrContext::dump() const {
1054 SkDynamicMemoryWStream stream;
1055 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
1056 writer.beginObject();
1057
1058 static const char* kBackendStr[] = {
1059 "Metal",
1060 "OpenGL",
1061 "Vulkan",
1062 "Mock",
1063 };
1064 GR_STATIC_ASSERT(0 == kMetal_GrBackend);
1065 GR_STATIC_ASSERT(1 == kOpenGL_GrBackend);
1066 GR_STATIC_ASSERT(2 == kVulkan_GrBackend);
1067 GR_STATIC_ASSERT(3 == kMock_GrBackend);
1068 writer.appendString("backend", kBackendStr[fBackend]);
1069
1070 writer.appendName("caps");
1071 fCaps->dumpJSON(&writer);
1072
1073 writer.appendName("gpu");
1074 fGpu->dumpJSON(&writer);
1075
1076 // Flush JSON to the memory stream
1077 writer.endObject();
1078 writer.flush();
1079
1080 // Null terminate the JSON data in the memory stream
1081 stream.write8(0);
1082
1083 // Allocate a string big enough to hold all the data, then copy out of the stream
1084 SkString result(stream.bytesWritten());
1085 stream.copyToAndReset(result.writable_str());
1086 return result;
1087}