blob: c61a6c189ee5dbc197884c6bdee0219b69ff39a6 [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
bsalomon@google.com1fadb202011-12-12 16:10:08 +00008#include "GrContext.h"
Brian Salomon5f33a8c2018-02-26 14:32:39 -05009#include "GrBackendSemaphore.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"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050015#include "GrProxyProvider.h"
Brian Osman11052242016-10-27 14:47:55 -040016#include "GrRenderTargetContext.h"
Brian Salomonc65aec92017-03-09 09:03:58 -050017#include "GrRenderTargetProxy.h"
bsalomon0ea80f42015-02-11 10:49:59 -080018#include "GrResourceCache.h"
bsalomond309e7a2015-04-30 14:18:54 -070019#include "GrResourceProvider.h"
Greg Danield85f97d2017-03-07 13:37:21 -050020#include "GrSemaphore.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000021#include "GrSoftwarePathRenderer.h"
Brian Osman45580d32016-11-23 09:37:01 -050022#include "GrSurfaceContext.h"
bsalomonafbf2d62014-09-30 12:18:44 -070023#include "GrSurfacePriv.h"
Robert Phillips757914d2017-01-25 15:48:30 -050024#include "GrSurfaceProxyPriv.h"
Robert Phillips646e4292017-06-13 12:44:56 -040025#include "GrTexture.h"
Brian Osman45580d32016-11-23 09:37:01 -050026#include "GrTextureContext.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040027#include "GrTracing.h"
Matt Sarett485c4992017-02-14 14:18:27 -050028#include "SkConvertPixels.h"
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -050029#include "SkDeferredDisplayList.h"
Brian Osman3b655982017-03-07 16:58:08 -050030#include "SkGr.h"
Mike Reed7fcfb622018-02-09 13:26:46 -050031#include "SkImageInfoPriv.h"
Brian Osman71a18892017-08-10 10:23:25 -040032#include "SkJSONWriter.h"
Brian Osman51279982017-08-23 10:12:00 -040033#include "SkMakeUnique.h"
34#include "SkTaskGroup.h"
Matt Sarettc7b29082017-02-09 16:22:39 -050035#include "SkUnPreMultiplyPriv.h"
joshualitt5478d422014-11-14 16:00:38 -080036#include "effects/GrConfigConversionEffect.h"
Greg Daniel02611d92017-07-25 10:05:01 -040037#include "gl/GrGLGpu.h"
38#include "mock/GrMockGpu.h"
Brian Salomon5f33a8c2018-02-26 14:32:39 -050039#include "text/GrTextBlobCache.h"
Greg Danielb76a72a2017-07-13 15:07:54 -040040#ifdef SK_METAL
41#include "mtl/GrMtlTrampoline.h"
42#endif
Greg Daniel02611d92017-07-25 10:05:01 -040043#ifdef SK_VULKAN
44#include "vk/GrVkGpu.h"
45#endif
Greg Danielb76a72a2017-07-13 15:07:54 -040046
Robert Phillipse78b7252017-04-06 07:59:41 -040047#define ASSERT_OWNED_PROXY(P) \
48SkASSERT(!(P) || !((P)->priv().peekTexture()) || (P)->priv().peekTexture()->getContext() == this)
Robert Phillips7ee385e2017-03-30 08:02:11 -040049#define ASSERT_OWNED_PROXY_PRIV(P) \
50SkASSERT(!(P) || !((P)->priv().peekTexture()) || (P)->priv().peekTexture()->getContext() == fContext)
51
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000052#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
joshualitt1de610a2016-01-06 08:26:09 -080053#define ASSERT_SINGLE_OWNER \
54 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fSingleOwner);)
robertphillips4fd74ae2016-08-03 14:26:53 -070055#define ASSERT_SINGLE_OWNER_PRIV \
56 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fContext->fSingleOwner);)
robertphillips7761d612016-05-16 09:14:53 -070057#define RETURN_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return; }
Robert Phillips7ee385e2017-03-30 08:02:11 -040058#define RETURN_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return; }
robertphillips7761d612016-05-16 09:14:53 -070059#define RETURN_FALSE_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return false; }
Robert Phillipse78b7252017-04-06 07:59:41 -040060#define RETURN_FALSE_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return false; }
robertphillips7761d612016-05-16 09:14:53 -070061#define RETURN_NULL_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return nullptr; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000062
robertphillipsea461502015-05-26 11:38:03 -070063////////////////////////////////////////////////////////////////////////////////
64
Robert Phillips88260b52018-01-19 12:56:09 -050065class SK_API GrDirectContext : public GrContext {
66public:
Robert Phillipse5b7cee2018-02-27 18:00:22 -050067 GrDirectContext(GrBackend backend)
68 : INHERITED(backend)
69 , fFullAtlasManager(nullptr) {
70 }
71
72 ~GrDirectContext() override {
73 this->flush();
74
75 delete fFullAtlasManager;
76 }
77
78 void abandonContext() override {
79 INHERITED::abandonContext();
80 fFullAtlasManager->freeAll();
81 }
82
83 void releaseResourcesAndAbandonContext() override {
84 INHERITED::releaseResourcesAndAbandonContext();
85 fFullAtlasManager->freeAll();
86 }
87
88 void freeGpuResources() override {
89 this->flush();
90 fFullAtlasManager->freeAll();
91
92 INHERITED::freeGpuResources();
93 }
Robert Phillips88260b52018-01-19 12:56:09 -050094
95protected:
Robert Phillipse5b7cee2018-02-27 18:00:22 -050096 bool init(const GrContextOptions& options) override {
97 if (!INHERITED::init(options)) {
98 return false;
99 }
100
101 GrDrawOpAtlas::AllowMultitexturing allowMultitexturing;
102 if (GrContextOptions::Enable::kNo == options.fAllowMultipleGlyphCacheTextures ||
103 // multitexturing supported only if range can represent the index + texcoords fully
104 !(fCaps->shaderCaps()->floatIs32Bits() || fCaps->shaderCaps()->integerSupport())) {
105 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
106 } else {
107 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
108 }
109
110 GrGlyphCache* glyphCache = this->contextPriv().getGlyphCache();
111 GrProxyProvider* proxyProvider = this->contextPriv().proxyProvider();
112
113 fFullAtlasManager = new GrAtlasManager(proxyProvider, glyphCache,
114 options.fGlyphCacheTextureMaximumBytes,
115 allowMultitexturing);
116 this->contextPriv().addOnFlushCallbackObject(fFullAtlasManager);
117
118 glyphCache->setGlyphSizeLimit(fFullAtlasManager->getGlyphSizeLimit());
119 return true;
120 }
121
122 GrRestrictedAtlasManager* onGetRestrictedAtlasManager() override { return fFullAtlasManager; }
123 GrAtlasManager* onGetFullAtlasManager() override { return fFullAtlasManager; }
Robert Phillips88260b52018-01-19 12:56:09 -0500124
125private:
Robert Phillipse5b7cee2018-02-27 18:00:22 -0500126 GrAtlasManager* fFullAtlasManager;
127
Robert Phillips88260b52018-01-19 12:56:09 -0500128 typedef GrContext INHERITED;
129};
130
Robert Phillipse5b7cee2018-02-27 18:00:22 -0500131/**
132 * The DDL Context is the one in effect during DDL Recording. It isn't backed by a GrGPU and
133 * cannot allocate any GPU resources.
134 */
Robert Phillips88260b52018-01-19 12:56:09 -0500135class SK_API GrDDLContext : public GrContext {
136public:
Robert Phillipse5b7cee2018-02-27 18:00:22 -0500137 GrDDLContext(GrContextThreadSafeProxy* proxy)
138 : INHERITED(proxy)
139 , fRestrictedAtlasManager(nullptr) {
140 }
141
142 ~GrDDLContext() override {
143 // The GrDDLContext doesn't actually own the fRestrictedAtlasManager so don't delete it
144 }
145
146 void abandonContext() override {
147 SkASSERT(0); // abandoning in a DDL Recorder doesn't make a whole lot of sense
148 INHERITED::abandonContext();
149 }
150
151 void releaseResourcesAndAbandonContext() override {
152 SkASSERT(0); // abandoning in a DDL Recorder doesn't make a whole lot of sense
153 INHERITED::releaseResourcesAndAbandonContext();
154 }
155
156 void freeGpuResources() override {
157 SkASSERT(0); // freeing resources in a DDL Recorder doesn't make a whole lot of sense
158 INHERITED::freeGpuResources();
159 }
Robert Phillips88260b52018-01-19 12:56:09 -0500160
161protected:
Robert Phillipse5b7cee2018-02-27 18:00:22 -0500162 bool init(const GrContextOptions& options) override {
163 if (!INHERITED::init(options)) {
164 return false;
165 }
166
167 // DDL TODO: in DDL-mode grab a GrRestrictedAtlasManager from the thread-proxy and
168 // do not add an onFlushCB
169 return true;
170 }
171
172 GrRestrictedAtlasManager* onGetRestrictedAtlasManager() override {
173 return fRestrictedAtlasManager;
174 }
175
176 GrAtlasManager* onGetFullAtlasManager() override {
177 SkASSERT(0); // the DDL Recorders should never invoke this
178 return nullptr;
179 }
Robert Phillips88260b52018-01-19 12:56:09 -0500180
181private:
Robert Phillipse5b7cee2018-02-27 18:00:22 -0500182 GrRestrictedAtlasManager* fRestrictedAtlasManager;
183
Robert Phillips88260b52018-01-19 12:56:09 -0500184 typedef GrContext INHERITED;
185};
186
bsalomon682c2692015-05-22 14:01:46 -0700187GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext) {
188 GrContextOptions defaultOptions;
189 return Create(backend, backendContext, defaultOptions);
190}
bsalomonf28cff72015-05-22 12:25:41 -0700191
bsalomon682c2692015-05-22 14:01:46 -0700192GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
193 const GrContextOptions& options) {
bsalomon682c2692015-05-22 14:01:46 -0700194
Robert Phillips88260b52018-01-19 12:56:09 -0500195 sk_sp<GrContext> context(new GrDirectContext(backend));
Robert Phillipse42edcc2017-12-13 11:50:22 -0500196
197 context->fGpu = GrGpu::Make(backend, backendContext, options, context.get());
198 if (!context->fGpu) {
halcanary96fcdcc2015-08-27 07:41:13 -0700199 return nullptr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000200 }
Robert Phillipse42edcc2017-12-13 11:50:22 -0500201
202 if (!context->init(options)) {
203 return nullptr;
204 }
205
Brian Salomon91a3e522017-06-23 10:58:19 -0400206 return context.release();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000207}
208
Brian Salomon384fab42017-12-07 12:33:05 -0500209sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface) {
Greg Daniel02611d92017-07-25 10:05:01 -0400210 GrContextOptions defaultOptions;
Brian Salomon384fab42017-12-07 12:33:05 -0500211 return MakeGL(std::move(interface), defaultOptions);
Greg Daniel02611d92017-07-25 10:05:01 -0400212}
213
Brian Salomon384fab42017-12-07 12:33:05 -0500214sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface,
Greg Daniel02611d92017-07-25 10:05:01 -0400215 const GrContextOptions& options) {
Robert Phillips88260b52018-01-19 12:56:09 -0500216 sk_sp<GrContext> context(new GrDirectContext(kOpenGL_GrBackend));
Robert Phillipse42edcc2017-12-13 11:50:22 -0500217
Brian Salomon384fab42017-12-07 12:33:05 -0500218 context->fGpu = GrGLGpu::Make(std::move(interface), options, context.get());
Greg Daniel02611d92017-07-25 10:05:01 -0400219 if (!context->fGpu) {
220 return nullptr;
221 }
Greg Daniel02611d92017-07-25 10:05:01 -0400222 if (!context->init(options)) {
223 return nullptr;
224 }
225 return context;
226}
227
Brian Salomon990014d2017-12-07 16:23:39 -0500228sk_sp<GrContext> GrContext::MakeGL(const GrGLInterface* interface) {
229 return MakeGL(sk_ref_sp(interface));
230}
231
232sk_sp<GrContext> GrContext::MakeGL(const GrGLInterface* interface,
233 const GrContextOptions& options) {
234 return MakeGL(sk_ref_sp(interface), options);
235}
236
Greg Daniel02611d92017-07-25 10:05:01 -0400237sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions) {
238 GrContextOptions defaultOptions;
239 return MakeMock(mockOptions, defaultOptions);
240}
241
242sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions,
243 const GrContextOptions& options) {
Robert Phillips88260b52018-01-19 12:56:09 -0500244 sk_sp<GrContext> context(new GrDirectContext(kMock_GrBackend));
Robert Phillipse42edcc2017-12-13 11:50:22 -0500245
Brian Salomon384fab42017-12-07 12:33:05 -0500246 context->fGpu = GrMockGpu::Make(mockOptions, options, context.get());
Greg Daniel02611d92017-07-25 10:05:01 -0400247 if (!context->fGpu) {
248 return nullptr;
249 }
Greg Daniel02611d92017-07-25 10:05:01 -0400250 if (!context->init(options)) {
251 return nullptr;
252 }
253 return context;
254}
255
256#ifdef SK_VULKAN
Brian Salomon384fab42017-12-07 12:33:05 -0500257sk_sp<GrContext> GrContext::MakeVulkan(sk_sp<const GrVkBackendContext> backendContext) {
Greg Daniel02611d92017-07-25 10:05:01 -0400258 GrContextOptions defaultOptions;
Brian Salomon384fab42017-12-07 12:33:05 -0500259 return MakeVulkan(std::move(backendContext), defaultOptions);
Greg Daniel02611d92017-07-25 10:05:01 -0400260}
261
Brian Salomon384fab42017-12-07 12:33:05 -0500262sk_sp<GrContext> GrContext::MakeVulkan(sk_sp<const GrVkBackendContext> backendContext,
Greg Daniel02611d92017-07-25 10:05:01 -0400263 const GrContextOptions& options) {
Robert Phillips88260b52018-01-19 12:56:09 -0500264 sk_sp<GrContext> context(new GrDirectContext(kVulkan_GrBackend));
Robert Phillipse42edcc2017-12-13 11:50:22 -0500265
Brian Salomon384fab42017-12-07 12:33:05 -0500266 context->fGpu = GrVkGpu::Make(std::move(backendContext), options, context.get());
Greg Daniel02611d92017-07-25 10:05:01 -0400267 if (!context->fGpu) {
268 return nullptr;
269 }
Greg Daniel02611d92017-07-25 10:05:01 -0400270 if (!context->init(options)) {
271 return nullptr;
272 }
273 return context;
274}
275#endif
276
Greg Danielb76a72a2017-07-13 15:07:54 -0400277#ifdef SK_METAL
Greg Daniel02611d92017-07-25 10:05:01 -0400278sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue) {
279 GrContextOptions defaultOptions;
280 return MakeMetal(device, queue, defaultOptions);
281}
282
Greg Danielb76a72a2017-07-13 15:07:54 -0400283sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) {
Robert Phillipse5b7cee2018-02-27 18:00:22 -0500284 sk_sp<GrContext> context(new GrDirectContext(kMetal_GrBackend));
Robert Phillipse42edcc2017-12-13 11:50:22 -0500285
Brian Salomon384fab42017-12-07 12:33:05 -0500286 context->fGpu = GrMtlTrampoline::MakeGpu(context.get(), options, device, queue);
Greg Danielb76a72a2017-07-13 15:07:54 -0400287 if (!context->fGpu) {
288 return nullptr;
289 }
Greg Danielb76a72a2017-07-13 15:07:54 -0400290 if (!context->init(options)) {
291 return nullptr;
292 }
293 return context;
294}
295#endif
296
joshualitt0acd0d32015-05-07 08:23:19 -0700297static int32_t gNextID = 1;
298static int32_t next_id() {
299 int32_t id;
300 do {
301 id = sk_atomic_inc(&gNextID);
302 } while (id == SK_InvalidGenID);
303 return id;
304}
305
Robert Phillipse42edcc2017-12-13 11:50:22 -0500306sk_sp<GrContext> GrContextPriv::MakeDDL(GrContextThreadSafeProxy* proxy) {
Robert Phillips88260b52018-01-19 12:56:09 -0500307 sk_sp<GrContext> context(new GrDDLContext(proxy));
Robert Phillipse42edcc2017-12-13 11:50:22 -0500308
Robert Phillips88260b52018-01-19 12:56:09 -0500309 // Note: we aren't creating a Gpu here. This causes the resource provider & cache to
310 // also not be created
Robert Phillipse42edcc2017-12-13 11:50:22 -0500311 if (!context->init(proxy->fOptions)) {
312 return nullptr;
313 }
314 return context;
315}
316
317GrContext::GrContext(GrBackend backend)
318 : fUniqueID(next_id())
319 , fBackend(backend) {
halcanary96fcdcc2015-08-27 07:41:13 -0700320 fResourceCache = nullptr;
321 fResourceProvider = nullptr;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500322 fProxyProvider = nullptr;
Robert Phillipsacf17902018-02-27 16:43:18 -0500323 fGlyphCache = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000324}
325
Robert Phillipse42edcc2017-12-13 11:50:22 -0500326GrContext::GrContext(GrContextThreadSafeProxy* proxy)
Robert Phillips88260b52018-01-19 12:56:09 -0500327 : fCaps(proxy->fCaps)
328 , fUniqueID(proxy->fContextUniqueID)
Robert Phillipse42edcc2017-12-13 11:50:22 -0500329 , fBackend(proxy->fBackend) {
330 fResourceCache = nullptr;
331 fResourceProvider = nullptr;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500332 fProxyProvider = nullptr;
Robert Phillipsacf17902018-02-27 16:43:18 -0500333 fGlyphCache = nullptr;
Greg Danielb76a72a2017-07-13 15:07:54 -0400334}
joshualitt1de610a2016-01-06 08:26:09 -0800335
Greg Danielb76a72a2017-07-13 15:07:54 -0400336bool GrContext::init(const GrContextOptions& options) {
337 ASSERT_SINGLE_OWNER
Robert Phillips88260b52018-01-19 12:56:09 -0500338
339 if (fGpu) {
340 fCaps = fGpu->refCaps();
341 fResourceCache = new GrResourceCache(fCaps.get(), fUniqueID);
Robert Phillips4150eea2018-02-07 17:08:21 -0500342 fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, &fSingleOwner,
343 options.fExplicitlyAllocateGPUResources);
Robert Phillips88260b52018-01-19 12:56:09 -0500344 }
345
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500346 fProxyProvider = new GrProxyProvider(fResourceProvider, fResourceCache, fCaps, &fSingleOwner);
Robert Phillips88260b52018-01-19 12:56:09 -0500347
348 if (fResourceCache) {
349 fResourceCache->setProxyProvider(fProxyProvider);
350 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500351
Robert Phillipse42edcc2017-12-13 11:50:22 -0500352 // DDL TODO: we need to think through how the task group & persistent cache
353 // get passed on to/shared between all the DDLRecorders created with this context.
354 fThreadSafeProxy.reset(new GrContextThreadSafeProxy(fCaps, this->uniqueID(), fBackend,
355 options));
commit-bot@chromium.org1836d332013-07-16 22:55:03 +0000356
Brian Osman46da1cc2017-02-14 14:15:48 -0500357 fDisableGpuYUVConversion = options.fDisableGpuYUVConversion;
Brian Osman8a83ca42018-02-12 14:32:17 -0500358 fSharpenMipmappedTextures = options.fSharpenMipmappedTextures;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000359 fDidTestPMConversions = false;
360
bsalomon6b2552f2016-09-15 13:50:26 -0700361 GrPathRendererChain::Options prcOptions;
bsalomon39ef7fb2016-09-21 11:16:05 -0700362 prcOptions.fAllowPathMaskCaching = options.fAllowPathMaskCaching;
Brian Osman195c05b2017-08-30 15:14:04 -0400363#if GR_TEST_UTILS
csmartdalton008b9d82017-02-22 12:00:42 -0700364 prcOptions.fGpuPathRenderers = options.fGpuPathRenderers;
Brian Osman195c05b2017-08-30 15:14:04 -0400365#endif
Brian Osmanb350ae22017-08-29 16:15:39 -0400366 if (options.fDisableDistanceFieldPaths) {
Brian Osman195c05b2017-08-30 15:14:04 -0400367 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
Brian Osmanb350ae22017-08-29 16:15:39 -0400368 }
Brian Salomonaf597482017-11-07 16:23:34 -0500369
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500370 if (!fResourceCache) {
371 // DDL TODO: remove this crippling of the path renderer chain
372 // Disable the small path renderer bc of the proxies in the atlas. They need to be
373 // unified when the opLists are added back to the destination drawing manager.
374 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
375 }
376
Brian Salomonaf597482017-11-07 16:23:34 -0500377 GrAtlasTextContext::Options atlasTextContextOptions;
378 atlasTextContextOptions.fMaxDistanceFieldFontSize = options.fGlyphsAsPathsFontSize;
379 atlasTextContextOptions.fMinDistanceFieldFontSize = options.fMinDistanceFieldFontSize;
Brian Salomonb5086962017-12-13 10:59:33 -0500380 atlasTextContextOptions.fDistanceFieldVerticesAlwaysHaveW = false;
381#if SK_SUPPORT_ATLAS_TEXT
382 if (GrContextOptions::Enable::kYes == options.fDistanceFieldGlyphVerticesAlwaysHaveW) {
383 atlasTextContextOptions.fDistanceFieldVerticesAlwaysHaveW = true;
384 }
385#endif
Brian Salomonaf597482017-11-07 16:23:34 -0500386
Robert Phillips4150eea2018-02-07 17:08:21 -0500387 fDrawingManager.reset(new GrDrawingManager(this, prcOptions, atlasTextContextOptions,
388 &fSingleOwner, options.fSortRenderTargets));
joshualitt7c3a2f82015-03-31 13:32:05 -0700389
Robert Phillipsacf17902018-02-27 16:43:18 -0500390 fGlyphCache = new GrGlyphCache;
391
Robert Phillips303cd582018-02-14 18:54:01 -0500392 fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB,
393 this, this->uniqueID(), SkToBool(fGpu)));
Brian Salomon91a3e522017-06-23 10:58:19 -0400394
Brian Osman51279982017-08-23 10:12:00 -0400395 if (options.fExecutor) {
396 fTaskGroup = skstd::make_unique<SkTaskGroup>(*options.fExecutor);
397 }
398
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400399 fPersistentCache = options.fPersistentCache;
400
Brian Salomon91a3e522017-06-23 10:58:19 -0400401 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000402}
403
bsalomon@google.com27847de2011-02-22 20:59:41 +0000404GrContext::~GrContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800405 ASSERT_SINGLE_OWNER
406
Robert Phillips2e6feed2018-01-22 15:27:20 -0500407 if (fDrawingManager) {
408 fDrawingManager->cleanup();
409 }
robertphillips2334fb62015-06-17 05:43:33 -0700410
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000411 for (int i = 0; i < fCleanUpData.count(); ++i) {
412 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
413 }
414
halcanary385fe4d2015-08-26 13:07:48 -0700415 delete fResourceProvider;
416 delete fResourceCache;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500417 delete fProxyProvider;
Robert Phillipsacf17902018-02-27 16:43:18 -0500418 delete fGlyphCache;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000419}
420
bungeman6bd52842016-10-27 09:30:08 -0700421sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
bungeman6bd52842016-10-27 09:30:08 -0700422 return fThreadSafeProxy;
bsalomon41b952c2016-03-11 06:46:33 -0800423}
424
Robert Phillipsfc711a22018-02-13 17:03:00 -0500425SkSurfaceCharacterization GrContextThreadSafeProxy::createCharacterization(
426 size_t cacheMaxResourceBytes,
427 const SkImageInfo& ii, const GrBackendFormat& backendFormat,
428 int sampleCnt, GrSurfaceOrigin origin,
429 const SkSurfaceProps& surfaceProps,
430 bool isMipMapped) {
431 if (!backendFormat.isValid()) {
432 return SkSurfaceCharacterization(); // return an invalid characterization
433 }
434
435 // We're assuming GrFSAAType::kMixedSamples will never be specified via this code path
436 GrFSAAType FSAAType = sampleCnt > 1 ? GrFSAAType::kUnifiedMSAA : GrFSAAType::kNone;
437
438 if (!fCaps->mipMapSupport()) {
439 isMipMapped = false;
440 }
441
442 GrPixelConfig config = kUnknown_GrPixelConfig;
443 if (!fCaps->getConfigFromBackendFormat(backendFormat, ii.colorType(), &config)) {
444 return SkSurfaceCharacterization(); // return an invalid characterization
445 }
446
447 // This surface characterization factory assumes that the resulting characterization is
448 // textureable.
449 if (!fCaps->isConfigTexturable(config)) {
450 return SkSurfaceCharacterization(); // return an invalid characterization
451 }
452
453 return SkSurfaceCharacterization(sk_ref_sp<GrContextThreadSafeProxy>(this),
454 cacheMaxResourceBytes,
455 origin, ii.width(), ii.height(), config, FSAAType, sampleCnt,
456 SkSurfaceCharacterization::Textureable(true),
457 SkSurfaceCharacterization::MipMapped(isMipMapped),
458 ii.refColorSpace(), surfaceProps);
459}
460
bsalomon2354f842014-07-28 13:48:36 -0700461void GrContext::abandonContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800462 ASSERT_SINGLE_OWNER
463
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500464 fProxyProvider->abandon();
bsalomond309e7a2015-04-30 14:18:54 -0700465 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800466
467 // Need to abandon the drawing manager first so all the render targets
468 // will be released/forgotten before they too are abandoned.
469 fDrawingManager->abandon();
470
bsalomon@google.com205d4602011-04-25 12:43:45 +0000471 // abandon first to so destructors
472 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800473 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700474
bsalomon6e2aad42016-04-01 11:54:31 -0700475 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
476
Robert Phillipsacf17902018-02-27 16:43:18 -0500477 fGlyphCache->freeAll();
bsalomon6e2aad42016-04-01 11:54:31 -0700478 fTextBlobCache->freeAll();
479}
480
481void GrContext::releaseResourcesAndAbandonContext() {
482 ASSERT_SINGLE_OWNER
483
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500484 fProxyProvider->abandon();
bsalomon6e2aad42016-04-01 11:54:31 -0700485 fResourceProvider->abandon();
486
487 // Need to abandon the drawing manager first so all the render targets
488 // will be released/forgotten before they too are abandoned.
489 fDrawingManager->abandon();
490
491 // Release all resources in the backend 3D API.
492 fResourceCache->releaseAll();
493
494 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000495
Robert Phillipsacf17902018-02-27 16:43:18 -0500496 fGlyphCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700497 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000498}
499
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000500void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800501 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000502 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000503}
504
505void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800506 ASSERT_SINGLE_OWNER
507
Robert Phillipsacf17902018-02-27 16:43:18 -0500508 fGlyphCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700509
510 fDrawingManager->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700511
512 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000513}
514
Jim Van Verth76d917c2017-12-13 09:26:37 -0500515void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) {
Brian Salomon5e150852017-03-22 14:53:13 -0400516 ASSERT_SINGLE_OWNER
Jim Van Verth76d917c2017-12-13 09:26:37 -0500517 fResourceCache->purgeAsNeeded();
518 fResourceCache->purgeResourcesNotUsedSince(GrStdSteadyClock::now() - msNotUsed);
519
520 fTextBlobCache->purgeStaleBlobs();
Brian Salomon5e150852017-03-22 14:53:13 -0400521}
522
Derek Sollenberger5480a182017-05-25 16:43:59 -0400523void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
524 ASSERT_SINGLE_OWNER
525 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
526}
527
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000528void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800529 ASSERT_SINGLE_OWNER
530
bsalomon71cb0c22014-11-14 12:10:14 -0800531 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800532 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800533 }
534 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800535 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800536 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000537}
538
Derek Sollenbergeree479142017-05-24 11:41:33 -0400539size_t GrContext::getResourceCachePurgeableBytes() const {
540 ASSERT_SINGLE_OWNER
541 return fResourceCache->getPurgeableBytes();
542}
543
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000544////////////////////////////////////////////////////////////////////////////////
545
Brian Salomonbdecacf2018-02-02 20:32:49 -0500546bool GrContext::colorTypeSupportedAsImage(SkColorType colorType) const {
547 GrPixelConfig config = SkImageInfo2GrPixelConfig(colorType, nullptr, *this->caps());
548 return this->caps()->isConfigTexturable(config);
549}
550
551int GrContext::maxSurfaceSampleCountForColorType(SkColorType colorType) const {
552 GrPixelConfig config = SkImageInfo2GrPixelConfig(colorType, nullptr, *this->caps());
553 return this->caps()->maxRenderTargetSampleCount(config);
554}
555
556////////////////////////////////////////////////////////////////////////////////
557
joshualitt0db6dfa2015-04-10 07:01:30 -0700558void GrContext::TextBlobCacheOverBudgetCB(void* data) {
559 SkASSERT(data);
Brian Osman11052242016-10-27 14:47:55 -0400560 // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on
561 // GrRenderTargetContext to perform a necessary flush. The solution is to move drawText calls
562 // to below the GrContext level, but this is not trivial because they call drawPath on
563 // SkGpuDevice.
joshualitt0db6dfa2015-04-10 07:01:30 -0700564 GrContext* context = reinterpret_cast<GrContext*>(data);
565 context->flush();
566}
567
bsalomon@google.com27847de2011-02-22 20:59:41 +0000568////////////////////////////////////////////////////////////////////////////////
569
bsalomonb77a9072016-09-07 10:02:04 -0700570void GrContext::flush() {
joshualitt1de610a2016-01-06 08:26:09 -0800571 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700572 RETURN_IF_ABANDONED
Robert Phillips7ee385e2017-03-30 08:02:11 -0400573
574 fDrawingManager->flush(nullptr);
575}
576
Greg Daniel51316782017-08-02 15:10:09 +0000577GrSemaphoresSubmitted GrContext::flushAndSignalSemaphores(int numSemaphores,
578 GrBackendSemaphore signalSemaphores[]) {
579 ASSERT_SINGLE_OWNER
580 if (fDrawingManager->wasAbandoned()) { return GrSemaphoresSubmitted::kNo; }
581
582 return fDrawingManager->flush(nullptr, numSemaphores, signalSemaphores);
583}
584
Robert Phillips7ee385e2017-03-30 08:02:11 -0400585void GrContextPriv::flush(GrSurfaceProxy* proxy) {
586 ASSERT_SINGLE_OWNER_PRIV
587 RETURN_IF_ABANDONED_PRIV
588 ASSERT_OWNED_PROXY_PRIV(proxy);
589
590 fContext->fDrawingManager->flush(proxy);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000591}
592
Brian Salomonc320b152018-02-20 14:05:36 -0500593bool sw_convert_to_premul(GrColorType srcColorType, int width, int height, size_t inRowBytes,
bsalomon81beccc2014-10-13 12:32:55 -0700594 const void* inPixels, size_t outRowBytes, void* outPixels) {
Brian Salomonc320b152018-02-20 14:05:36 -0500595 SkColorType colorType = GrColorTypeToSkColorType(srcColorType);
596 if (kUnknown_SkColorType == colorType || 4 != SkColorTypeBytesPerPixel(colorType)) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000597 return false;
598 }
bsalomon81beccc2014-10-13 12:32:55 -0700599
Matt Sarettc7b29082017-02-09 16:22:39 -0500600 for (int y = 0; y < height; y++) {
601 SkOpts::RGBA_to_rgbA((uint32_t*) outPixels, inPixels, width);
602 outPixels = SkTAddOffset<void>(outPixels, outRowBytes);
603 inPixels = SkTAddOffset<const void>(inPixels, inRowBytes);
604 }
bsalomon81beccc2014-10-13 12:32:55 -0700605
Matt Sarettc7b29082017-02-09 16:22:39 -0500606 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000607}
608
Brian Salomonc320b152018-02-20 14:05:36 -0500609// TODO: This will be removed when GrSurfaceContexts are aware of their color types.
610// (skbug.com/6718)
Brian Osmand2ca59a2017-04-13 14:03:57 -0400611static bool valid_premul_config(GrPixelConfig config) {
Brian Salomonc320b152018-02-20 14:05:36 -0500612 switch (config) {
613 case kUnknown_GrPixelConfig: return false;
614 case kAlpha_8_GrPixelConfig: return false;
615 case kGray_8_GrPixelConfig: return false;
616 case kRGB_565_GrPixelConfig: return false;
617 case kRGBA_4444_GrPixelConfig: return true;
618 case kRGBA_8888_GrPixelConfig: return true;
619 case kBGRA_8888_GrPixelConfig: return true;
620 case kSRGBA_8888_GrPixelConfig: return true;
621 case kSBGRA_8888_GrPixelConfig: return true;
622 case kRGBA_float_GrPixelConfig: return true;
623 case kRG_float_GrPixelConfig: return false;
624 case kAlpha_half_GrPixelConfig: return false;
625 case kRGBA_half_GrPixelConfig: return true;
626 case kAlpha_8_as_Alpha_GrPixelConfig: return false;
627 case kAlpha_8_as_Red_GrPixelConfig: return false;
628 case kAlpha_half_as_Red_GrPixelConfig: return false;
629 case kGray_8_as_Lum_GrPixelConfig: return false;
630 case kGray_8_as_Red_GrPixelConfig: return false;
631 }
632 SK_ABORT("Invalid GrPixelConfig");
633 return false;
Brian Osmance425512017-03-22 14:37:50 -0400634}
635
Brian Salomonc320b152018-02-20 14:05:36 -0500636static bool valid_premul_color_type(GrColorType ct) {
637 switch (ct) {
638 case GrColorType::kUnknown: return false;
639 case GrColorType::kAlpha_8: return false;
640 case GrColorType::kRGB_565: return false;
641 case GrColorType::kABGR_4444: return true;
642 case GrColorType::kRGBA_8888: return true;
643 case GrColorType::kBGRA_8888: return true;
644 case GrColorType::kGray_8: return false;
645 case GrColorType::kAlpha_F16: return false;
646 case GrColorType::kRGBA_F16: return true;
647 case GrColorType::kRG_F32: return false;
648 case GrColorType::kRGBA_F32: return true;
649 }
650 SK_ABORT("Invalid GrColorType");
651 return false;
652}
653
654static bool valid_pixel_conversion(GrColorType cpuColorType, GrPixelConfig gpuConfig,
Brian Osmand2ca59a2017-04-13 14:03:57 -0400655 bool premulConversion) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400656 // We only allow premul <-> unpremul conversions for some formats
Brian Salomonc320b152018-02-20 14:05:36 -0500657 if (premulConversion &&
658 (!valid_premul_color_type(cpuColorType) || !valid_premul_config(gpuConfig))) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400659 return false;
660 }
661
662 return true;
663}
664
Brian Salomonc320b152018-02-20 14:05:36 -0500665static bool pm_upm_must_round_trip(GrColorType cpuColorType, const SkColorSpace* cpuColorSpace) {
666 return !cpuColorSpace &&
667 (GrColorType::kRGBA_8888 == cpuColorType || GrColorType::kBGRA_8888 == cpuColorType);
Brian Osman409e74f2017-04-17 11:48:28 -0400668}
669
Brian Salomonc320b152018-02-20 14:05:36 -0500670// TODO: This will be removed when GrSurfaceContexts are aware of their color types.
671// (skbug.com/6718)
672static bool pm_upm_must_round_trip(GrPixelConfig surfaceConfig,
673 const SkColorSpace* surfaceColorSpace) {
674 return !surfaceColorSpace &&
675 (kRGBA_8888_GrPixelConfig == surfaceConfig || kBGRA_8888_GrPixelConfig == surfaceConfig);
676}
677
678static GrSRGBConversion determine_write_pixels_srgb_conversion(GrColorType srcColorType,
679 const SkColorSpace* srcColorSpace,
680 GrSRGBEncoded dstSRGBEncoded,
681 const SkColorSpace* dstColorSpace,
682 const GrCaps& caps) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500683 // No support for sRGB-encoded alpha.
Brian Salomonc320b152018-02-20 14:05:36 -0500684 if (GrColorTypeIsAlphaOnly(srcColorType)) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500685 return GrSRGBConversion::kNone;
686 }
Brian Salomonc320b152018-02-20 14:05:36 -0500687 // No conversions without GPU support for sRGB. (Legacy mode)
688 if (!caps.srgbSupport()) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500689 return GrSRGBConversion::kNone;
690 }
Brian Salomonc320b152018-02-20 14:05:36 -0500691 // If the GrSurfaceContext has no color space then it is in legacy mode.
692 if (!dstColorSpace) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500693 return GrSRGBConversion::kNone;
694 }
695
696 bool srcColorSpaceIsSRGB = srcColorSpace && srcColorSpace->gammaCloseToSRGB();
697 bool dstColorSpaceIsSRGB = dstColorSpace->gammaCloseToSRGB();
698
699 // For now we are assuming that if color space of the dst does not have sRGB gamma then the
700 // texture format is not sRGB encoded and vice versa. Note that we already checked for "legacy"
Brian Salomonc320b152018-02-20 14:05:36 -0500701 // mode being forced on by caps above. This may change in the future. We will then have to
702 // perform shader based conversions.
Brian Salomon9b009bb2018-02-14 13:53:55 -0500703 SkASSERT(dstColorSpaceIsSRGB == (GrSRGBEncoded::kYes == dstSRGBEncoded));
704
Brian Salomon9b009bb2018-02-14 13:53:55 -0500705 if (srcColorSpaceIsSRGB == dstColorSpaceIsSRGB) {
706 return GrSRGBConversion::kNone;
707 }
708 return srcColorSpaceIsSRGB ? GrSRGBConversion::kSRGBToLinear : GrSRGBConversion::kLinearToSRGB;
709}
710
Brian Salomonc320b152018-02-20 14:05:36 -0500711static GrSRGBConversion determine_read_pixels_srgb_conversion(GrSRGBEncoded srcSRGBEncoded,
712 const SkColorSpace* srcColorSpace,
713 GrColorType dstColorType,
714 const SkColorSpace* dstColorSpace,
715 const GrCaps& caps) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500716 // This is symmetrical with the write version.
Brian Salomonc320b152018-02-20 14:05:36 -0500717 switch (determine_write_pixels_srgb_conversion(dstColorType, dstColorSpace, srcSRGBEncoded,
718 srcColorSpace, caps)) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500719 case GrSRGBConversion::kNone: return GrSRGBConversion::kNone;
720 case GrSRGBConversion::kLinearToSRGB: return GrSRGBConversion::kSRGBToLinear;
721 case GrSRGBConversion::kSRGBToLinear: return GrSRGBConversion::kLinearToSRGB;
722 }
723 return GrSRGBConversion::kNone;
724}
725
Brian Salomonc320b152018-02-20 14:05:36 -0500726bool GrContextPriv::writeSurfacePixels(GrSurfaceContext* dst, int left, int top, int width,
727 int height, GrColorType srcColorType,
728 SkColorSpace* srcColorSpace, const void* buffer,
729 size_t rowBytes, uint32_t pixelOpsFlags) {
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500730#ifndef SK_LEGACY_GPU_PIXEL_OPS
731 return this->writeSurfacePixels2(dst, left, top, width, height, srcColorType, srcColorSpace,
732 buffer, rowBytes, pixelOpsFlags);
733#endif
734
Brian Osmanb62ea222016-12-22 11:12:16 -0500735 // TODO: Color space conversion
736
Robert Phillipse78b7252017-04-06 07:59:41 -0400737 ASSERT_SINGLE_OWNER_PRIV
738 RETURN_FALSE_IF_ABANDONED_PRIV
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400739 SkASSERT(dst);
740 ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy());
Brian Salomondcbb9d92017-07-19 10:53:20 -0400741 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "writeSurfacePixels", fContext);
bsalomon6c6f6582015-09-10 08:12:46 -0700742
Robert Phillips6be756b2018-01-16 15:07:54 -0500743 if (!dst->asSurfaceProxy()->instantiate(this->resourceProvider())) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400744 return false;
745 }
746
Robert Phillips16d8ec62017-07-27 16:16:25 -0400747 GrSurfaceProxy* dstProxy = dst->asSurfaceProxy();
748 GrSurface* dstSurface = dstProxy->priv().peekSurface();
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400749
Brian Osmand2ca59a2017-04-13 14:03:57 -0400750 // 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 -0400751 const bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
Brian Salomonc320b152018-02-20 14:05:36 -0500752
753 if (!valid_pixel_conversion(srcColorType, dstProxy->config(), premul)) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400754 return false;
755 }
756
Brian Osman409e74f2017-04-17 11:48:28 -0400757 // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data,
758 // without any color spaces attached, and the caller wants us to premul.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400759 bool useConfigConversionEffect =
Brian Salomonc320b152018-02-20 14:05:36 -0500760 premul && pm_upm_must_round_trip(srcColorType, srcColorSpace) &&
Brian Salomonf3569f02017-10-24 12:52:33 -0400761 pm_upm_must_round_trip(dstProxy->config(), dst->colorSpaceInfo().colorSpace());
Brian Osman409e74f2017-04-17 11:48:28 -0400762
763 // Are we going to try to premul as part of a draw? For the non-legacy case, we always allow
764 // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly.
765 bool premulOnGpu = premul &&
766 (!useConfigConversionEffect || fContext->validPMUPMConversionExists());
bsalomon81beccc2014-10-13 12:32:55 -0700767
bsalomone8d21e82015-07-16 08:23:13 -0700768 // 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 -0700769 // necessary and because GrGpu::getWritePixelsInfo requires it.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400770 if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(),
Brian Salomonc320b152018-02-20 14:05:36 -0500771 GrColorTypeBytesPerPixel(srcColorType), &left, &top,
772 &width, &height, &buffer, &rowBytes)) {
bsalomone8d21e82015-07-16 08:23:13 -0700773 return false;
774 }
775
Brian Osman409e74f2017-04-17 11:48:28 -0400776 GrGpu::DrawPreference drawPreference = premulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference
777 : GrGpu::kNoDraw_DrawPreference;
bsalomonf0674512015-07-28 13:26:15 -0700778 GrGpu::WritePixelTempDrawInfo tempDrawInfo;
Brian Salomon9b009bb2018-02-14 13:53:55 -0500779 GrSRGBConversion srgbConversion = determine_write_pixels_srgb_conversion(
Brian Salomonc320b152018-02-20 14:05:36 -0500780 srcColorType, srcColorSpace, GrPixelConfigIsSRGBEncoded(dstProxy->config()),
781 dst->colorSpaceInfo().colorSpace(), *fContext->caps());
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400782 if (!fContext->fGpu->getWritePixelsInfo(dstSurface, dstProxy->origin(), width, height,
Brian Salomonc320b152018-02-20 14:05:36 -0500783 srcColorType, srgbConversion, &drawPreference,
Brian Salomon9b009bb2018-02-14 13:53:55 -0500784 &tempDrawInfo)) {
bsalomonf0674512015-07-28 13:26:15 -0700785 return false;
786 }
787
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400788 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && dstSurface->surfacePriv().hasPendingIO()) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400789 this->flush(nullptr); // MDB TODO: tighten this
bsalomonf0674512015-07-28 13:26:15 -0700790 }
791
Robert Phillips2f493142017-03-02 18:18:38 -0500792 sk_sp<GrTextureProxy> tempProxy;
bsalomonf0674512015-07-28 13:26:15 -0700793 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500794 tempProxy = this->proxyProvider()->createProxy(tempDrawInfo.fTempSurfaceDesc,
795 SkBackingFit::kApprox,
796 SkBudgeted::kYes);
Robert Phillips2f493142017-03-02 18:18:38 -0500797 if (!tempProxy && GrGpu::kRequireDraw_DrawPreference == drawPreference) {
bsalomonf0674512015-07-28 13:26:15 -0700798 return false;
799 }
800 }
801
802 // temp buffer for doing sw premul conversion, if needed.
803 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
Brian Osman409e74f2017-04-17 11:48:28 -0400804 // We need to do sw premul if we were unable to create a RT for drawing, or if we can't do the
805 // premul on the GPU
806 if (premul && (!tempProxy || !premulOnGpu)) {
807 size_t tmpRowBytes = 4 * width;
808 tmpPixels.reset(width * height);
Brian Salomonc320b152018-02-20 14:05:36 -0500809 if (!sw_convert_to_premul(srcColorType, width, height, rowBytes, buffer, tmpRowBytes,
Brian Osman409e74f2017-04-17 11:48:28 -0400810 tmpPixels.get())) {
811 return false;
bsalomonf0674512015-07-28 13:26:15 -0700812 }
Brian Osman409e74f2017-04-17 11:48:28 -0400813 rowBytes = tmpRowBytes;
814 buffer = tmpPixels.get();
bsalomonf0674512015-07-28 13:26:15 -0700815 }
Brian Osman409e74f2017-04-17 11:48:28 -0400816
817 if (tempProxy) {
Brian Osman2240be92017-10-18 13:15:13 -0400818 auto fp = GrSimpleTextureEffect::Make(tempProxy, SkMatrix::I());
Brian Osman409e74f2017-04-17 11:48:28 -0400819 if (premulOnGpu) {
820 fp = fContext->createUPMToPMEffect(std::move(fp), useConfigConversionEffect);
bsalomon81beccc2014-10-13 12:32:55 -0700821 }
Brian Osman409e74f2017-04-17 11:48:28 -0400822 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400823 if (!fp) {
824 return false;
825 }
Brian Osman409e74f2017-04-17 11:48:28 -0400826
Robert Phillips6be756b2018-01-16 15:07:54 -0500827 if (!tempProxy->instantiate(this->resourceProvider())) {
Brian Osman409e74f2017-04-17 11:48:28 -0400828 return false;
829 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400830 GrTexture* texture = tempProxy->priv().peekTexture();
Robert Phillips7bbbf622017-10-17 07:36:59 -0400831
832 if (tempProxy->priv().hasPendingIO()) {
833 this->flush(tempProxy.get());
834 }
835
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400836 if (!fContext->fGpu->writePixels(texture, tempProxy->origin(), 0, 0, width, height,
Brian Salomonc320b152018-02-20 14:05:36 -0500837 tempDrawInfo.fWriteColorType, buffer, rowBytes)) {
Brian Osman409e74f2017-04-17 11:48:28 -0400838 return false;
839 }
Robert Phillips7bbbf622017-10-17 07:36:59 -0400840 tempProxy = nullptr;
841
Brian Osman409e74f2017-04-17 11:48:28 -0400842 SkMatrix matrix;
843 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400844 GrRenderTargetContext* renderTargetContext = dst->asRenderTargetContext();
Brian Osman409e74f2017-04-17 11:48:28 -0400845 if (!renderTargetContext) {
846 return false;
847 }
848 GrPaint paint;
849 paint.addColorFragmentProcessor(std::move(fp));
850 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonf3569f02017-10-24 12:52:33 -0400851 paint.setAllowSRGBInputs(dst->colorSpaceInfo().isGammaCorrect() ||
852 GrPixelConfigIsSRGB(dst->colorSpaceInfo().config()));
Brian Osman409e74f2017-04-17 11:48:28 -0400853 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
854 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, matrix, rect,
855 nullptr);
856
857 if (kFlushWrites_PixelOp & pixelOpsFlags) {
858 this->flushSurfaceWrites(renderTargetContext->asRenderTargetProxy());
859 }
860 } else {
Brian Salomonc320b152018-02-20 14:05:36 -0500861 return fContext->fGpu->writePixels(dstSurface, dstProxy->origin(), left, top, width, height,
862 srcColorType, buffer, rowBytes);
bsalomon81beccc2014-10-13 12:32:55 -0700863 }
bsalomon81beccc2014-10-13 12:32:55 -0700864 return true;
865}
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000866
Brian Salomonc320b152018-02-20 14:05:36 -0500867bool GrContextPriv::readSurfacePixels(GrSurfaceContext* src, int left, int top, int width,
868 int height, GrColorType dstColorType,
869 SkColorSpace* dstColorSpace, void* buffer, size_t rowBytes,
870 uint32_t flags) {
Brian Osmanb62ea222016-12-22 11:12:16 -0500871 // TODO: Color space conversion
872
Robert Phillipse78b7252017-04-06 07:59:41 -0400873 ASSERT_SINGLE_OWNER_PRIV
874 RETURN_FALSE_IF_ABANDONED_PRIV
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400875 SkASSERT(src);
876 ASSERT_OWNED_PROXY_PRIV(src->asSurfaceProxy());
Brian Salomondcbb9d92017-07-19 10:53:20 -0400877 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "readSurfacePixels", fContext);
bsalomon32ab2602015-09-09 18:57:49 -0700878
Robert Phillipse78b7252017-04-06 07:59:41 -0400879 // MDB TODO: delay this instantiation until later in the method
Robert Phillips6be756b2018-01-16 15:07:54 -0500880 if (!src->asSurfaceProxy()->instantiate(this->resourceProvider())) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400881 return false;
882 }
883
Robert Phillips16d8ec62017-07-27 16:16:25 -0400884 GrSurfaceProxy* srcProxy = src->asSurfaceProxy();
885 GrSurface* srcSurface = srcProxy->priv().peekSurface();
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400886
Brian Osmand2ca59a2017-04-13 14:03:57 -0400887 // The src is premul but the dst is unpremul -> unpremul the src after or as part of the read
888 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
Brian Salomonc320b152018-02-20 14:05:36 -0500889
890 if (!valid_pixel_conversion(dstColorType, srcProxy->config(), unpremul)) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400891 return false;
892 }
893
Brian Osman409e74f2017-04-17 11:48:28 -0400894 // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data,
895 // without any color spaces attached, and the caller wants us to unpremul.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400896 bool useConfigConversionEffect =
Brian Salomonf3569f02017-10-24 12:52:33 -0400897 unpremul &&
898 pm_upm_must_round_trip(srcProxy->config(), src->colorSpaceInfo().colorSpace()) &&
Brian Salomonc320b152018-02-20 14:05:36 -0500899 pm_upm_must_round_trip(dstColorType, dstColorSpace);
Brian Osman409e74f2017-04-17 11:48:28 -0400900
901 // Are we going to try to unpremul as part of a draw? For the non-legacy case, we always allow
902 // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly.
903 bool unpremulOnGpu = unpremul &&
904 (!useConfigConversionEffect || fContext->validPMUPMConversionExists());
bsalomon6c6f6582015-09-10 08:12:46 -0700905
bsalomone8d21e82015-07-16 08:23:13 -0700906 // Adjust the params so that if we wind up using an intermediate surface we've already done
907 // all the trimming and the temporary can be the min size required.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400908 if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(),
Brian Salomonc320b152018-02-20 14:05:36 -0500909 GrColorTypeBytesPerPixel(dstColorType), &left, &top,
910 &width, &height, &buffer, &rowBytes)) {
bsalomone8d21e82015-07-16 08:23:13 -0700911 return false;
912 }
913
Brian Osman409e74f2017-04-17 11:48:28 -0400914 GrGpu::DrawPreference drawPreference = unpremulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference
915 : GrGpu::kNoDraw_DrawPreference;
bsalomon39826022015-07-23 08:07:21 -0700916 GrGpu::ReadPixelTempDrawInfo tempDrawInfo;
Brian Salomon9b009bb2018-02-14 13:53:55 -0500917 GrSRGBConversion srgbConversion = determine_read_pixels_srgb_conversion(
918 GrPixelConfigIsSRGBEncoded(srcProxy->config()), src->colorSpaceInfo().colorSpace(),
Brian Salomonc320b152018-02-20 14:05:36 -0500919 dstColorType, dstColorSpace, *fContext->caps());
920
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400921 if (!fContext->fGpu->getReadPixelsInfo(srcSurface, srcProxy->origin(), width, height, rowBytes,
Brian Salomonc320b152018-02-20 14:05:36 -0500922 dstColorType, srgbConversion, &drawPreference,
Brian Salomon9b009bb2018-02-14 13:53:55 -0500923 &tempDrawInfo)) {
bsalomon39826022015-07-23 08:07:21 -0700924 return false;
925 }
bsalomon191bcc02014-11-14 11:31:13 -0800926
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400927 if (!(kDontFlush_PixelOpsFlag & flags) && srcSurface->surfacePriv().hasPendingWrite()) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400928 this->flush(nullptr); // MDB TODO: tighten this
929 }
930
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400931 sk_sp<GrSurfaceProxy> proxyToRead = src->asSurfaceProxyRef();
bsalomon39826022015-07-23 08:07:21 -0700932 bool didTempDraw = false;
933 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomonb117ff12016-07-19 07:24:40 -0700934 if (SkBackingFit::kExact == tempDrawInfo.fTempSurfaceFit) {
bsalomon39826022015-07-23 08:07:21 -0700935 // We only respect this when the entire src is being read. Otherwise we can trigger too
936 // many odd ball texture sizes and trash the cache.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400937 if (width != srcSurface->width() || height != srcSurface->height()) {
bsalomonb117ff12016-07-19 07:24:40 -0700938 tempDrawInfo.fTempSurfaceFit= SkBackingFit::kApprox;
bsalomon39826022015-07-23 08:07:21 -0700939 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000940 }
brianosmandfe4f2e2016-07-21 13:28:36 -0700941 // TODO: Need to decide the semantics of this function for color spaces. Do we support
942 // conversion to a passed-in color space? For now, specifying nullptr means that this
Brian Salomon366093f2018-02-13 09:25:22 -0500943 // path will do no conversion, so it will match the behavior of the non-draw path. For
944 // now we simply infer an sRGB color space if the config is sRGB in order to avoid an
945 // illegal combination.
946 sk_sp<SkColorSpace> colorSpace;
947 if (GrPixelConfigIsSRGB(tempDrawInfo.fTempSurfaceDesc.fConfig)) {
948 colorSpace = SkColorSpace::MakeSRGB();
949 }
950 sk_sp<GrRenderTargetContext> tempRTC =
951 fContext->makeDeferredRenderTargetContext(tempDrawInfo.fTempSurfaceFit,
952 tempDrawInfo.fTempSurfaceDesc.fWidth,
953 tempDrawInfo.fTempSurfaceDesc.fHeight,
954 tempDrawInfo.fTempSurfaceDesc.fConfig,
955 std::move(colorSpace),
956 tempDrawInfo.fTempSurfaceDesc.fSampleCnt,
957 GrMipMapped::kNo,
958 tempDrawInfo.fTempSurfaceDesc.fOrigin);
Brian Osman693a5402016-10-27 15:13:22 -0400959 if (tempRTC) {
Greg Danielf44cb482018-02-27 14:26:32 -0500960 // Adding discard to appease vulkan validation warning about loading uninitialized data
961 // on draw
962 tempRTC->discard();
Robert Phillips67c18d62017-01-20 12:44:06 -0500963 SkMatrix textureMatrix = SkMatrix::MakeTrans(SkIntToScalar(left), SkIntToScalar(top));
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400964 sk_sp<GrTextureProxy> proxy = src->asTextureProxyRef();
Brian Osman2240be92017-10-18 13:15:13 -0400965 auto fp = GrSimpleTextureEffect::Make(std::move(proxy), textureMatrix);
Brian Osman409e74f2017-04-17 11:48:28 -0400966 if (unpremulOnGpu) {
967 fp = fContext->createPMToUPMEffect(std::move(fp), useConfigConversionEffect);
968 // We no longer need to do this on CPU after the read back.
969 unpremul = false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000970 }
Brian Osman409e74f2017-04-17 11:48:28 -0400971 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400972 if (!fp) {
973 return false;
974 }
Brian Osman60cd57e2017-04-06 10:19:06 -0400975
Brian Osman409e74f2017-04-17 11:48:28 -0400976 GrPaint paint;
977 paint.addColorFragmentProcessor(std::move(fp));
978 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
979 paint.setAllowSRGBInputs(true);
980 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
981 tempRTC->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect,
982 nullptr);
983 proxyToRead = tempRTC->asTextureProxyRef();
984 left = 0;
985 top = 0;
986 didTempDraw = true;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000987 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000988 }
joshualitt5c55fef2014-10-31 14:04:35 -0700989
Robert Phillipse78b7252017-04-06 07:59:41 -0400990 if (!proxyToRead) {
991 return false;
992 }
993
bsalomon39826022015-07-23 08:07:21 -0700994 if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000995 return false;
996 }
Brian Salomonc320b152018-02-20 14:05:36 -0500997 GrColorType colorTypeToRead = dstColorType;
bsalomon39826022015-07-23 08:07:21 -0700998 if (didTempDraw) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400999 this->flushSurfaceWrites(proxyToRead.get());
Brian Salomonc320b152018-02-20 14:05:36 -05001000 colorTypeToRead = tempDrawInfo.fReadColorType;
bsalomon39826022015-07-23 08:07:21 -07001001 }
Robert Phillips09dfc472017-09-13 15:25:47 -04001002
Robert Phillips6be756b2018-01-16 15:07:54 -05001003 if (!proxyToRead->instantiate(this->resourceProvider())) {
Robert Phillips09dfc472017-09-13 15:25:47 -04001004 return false;
1005 }
1006
1007 GrSurface* surfaceToRead = proxyToRead->priv().peekSurface();
1008
Brian Salomonc320b152018-02-20 14:05:36 -05001009 if (!fContext->fGpu->readPixels(surfaceToRead, proxyToRead->origin(), left, top, width, height,
1010 colorTypeToRead, buffer, rowBytes)) {
bsalomon39826022015-07-23 08:07:21 -07001011 return false;
1012 }
1013
1014 // Perform umpremul conversion if we weren't able to perform it as a draw.
1015 if (unpremul) {
Brian Salomonc320b152018-02-20 14:05:36 -05001016 SkColorType colorType = GrColorTypeToSkColorType(dstColorType);
1017 if (kUnknown_SkColorType == colorType || 4 != SkColorTypeBytesPerPixel(colorType)) {
reed@google.com7111d462014-03-25 16:20:24 +00001018 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001019 }
reed@google.com7111d462014-03-25 16:20:24 +00001020
Matt Sarettc7b29082017-02-09 16:22:39 -05001021 for (int y = 0; y < height; y++) {
1022 SkUnpremultiplyRow<false>((uint32_t*) buffer, (const uint32_t*) buffer, width);
1023 buffer = SkTAddOffset<void>(buffer, rowBytes);
1024 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001025 }
1026 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001027}
1028
Brian Salomon5f33a8c2018-02-26 14:32:39 -05001029bool GrContextPriv::writeSurfacePixels2(GrSurfaceContext* dst, int left, int top, int width,
1030 int height, GrColorType srcColorType,
1031 SkColorSpace* srcColorSpace, const void* buffer,
1032 size_t rowBytes, uint32_t pixelOpsFlags) {
1033 ASSERT_SINGLE_OWNER_PRIV
1034 RETURN_FALSE_IF_ABANDONED_PRIV
1035 SkASSERT(dst);
1036 SkASSERT(buffer);
1037 ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy());
1038 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "writeSurfacePixels2", fContext);
1039
1040 if (GrColorType::kUnknown == srcColorType) {
1041 return false;
1042 }
1043
1044 if (!dst->asSurfaceProxy()->instantiate(this->resourceProvider())) {
1045 return false;
1046 }
1047
1048 GrSurfaceProxy* dstProxy = dst->asSurfaceProxy();
1049 GrSurface* dstSurface = dstProxy->priv().peekSurface();
1050
1051 if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(),
1052 GrColorTypeBytesPerPixel(srcColorType), &left, &top,
1053 &width, &height, &buffer, &rowBytes)) {
1054 return false;
1055 }
1056
Brian Salomon3d86a192018-02-27 16:46:11 -05001057 if (!fContext->caps()->surfaceSupportsWritePixels(dstSurface)) {
Brian Salomon5f33a8c2018-02-26 14:32:39 -05001058 GrSurfaceDesc desc;
1059 desc.fConfig = dstProxy->config();
1060 desc.fWidth = width;
1061 desc.fHeight = height;
1062 desc.fSampleCnt = 1;
1063 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
1064 auto tempProxy =
1065 this->proxyProvider()->createProxy(desc, SkBackingFit::kApprox, SkBudgeted::kYes);
1066 if (!tempProxy) {
1067 return false;
1068 }
1069 auto tempCtx = this->drawingManager()->makeTextureContext(
1070 tempProxy, dst->colorSpaceInfo().refColorSpace());
1071 if (!tempCtx) {
1072 return false;
1073 }
1074 if (!this->writeSurfacePixels2(tempCtx.get(), 0, 0, width, height, srcColorType,
1075 srcColorSpace, buffer, rowBytes, pixelOpsFlags)) {
1076 return false;
1077 }
Brian Salomon3d86a192018-02-27 16:46:11 -05001078 return dst->copy(tempProxy.get(), SkIRect::MakeWH(width, height), {left, top});
Brian Salomon5f33a8c2018-02-26 14:32:39 -05001079 }
1080
1081 // TODO: Make GrSurfaceContext know its alpha type and pass src buffer's alpha type.
1082 bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
1083 bool convert = premul;
1084
1085 if (!valid_pixel_conversion(srcColorType, dstProxy->config(), premul)) {
1086 return false;
1087 }
1088
1089 GrColorType allowedColorType =
1090 fContext->caps()->supportedWritePixelsColorType(dstProxy->config(), srcColorType);
1091 convert = convert || (srcColorType != allowedColorType);
1092
1093 if (!dst->colorSpaceInfo().colorSpace()) {
1094 // "Legacy" mode - no color space conversions.
1095 srcColorSpace = nullptr;
1096 }
1097 convert = convert || !SkColorSpace::Equals(srcColorSpace, dst->colorSpaceInfo().colorSpace());
1098
1099 std::unique_ptr<char[]> tempBuffer;
1100 if (convert) {
1101 auto srcSkColorType = GrColorTypeToSkColorType(srcColorType);
1102 auto dstSkColorType = GrColorTypeToSkColorType(allowedColorType);
1103 if (kUnknown_SkColorType == srcSkColorType || kUnknown_SkColorType == dstSkColorType) {
1104 return false;
1105 }
1106 auto srcAlphaType = premul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType;
1107 SkPixmap src(SkImageInfo::Make(width, height, srcSkColorType, srcAlphaType,
1108 sk_ref_sp(srcColorSpace)),
1109 buffer, rowBytes);
1110 auto tempSrcII = SkImageInfo::Make(width, height, dstSkColorType, kPremul_SkAlphaType,
1111 dst->colorSpaceInfo().refColorSpace());
1112 auto size = tempSrcII.computeMinByteSize();
1113 if (!size) {
1114 return false;
1115 }
1116 tempBuffer.reset(new char[size]);
1117 SkPixmap tempSrc(tempSrcII, tempBuffer.get(), tempSrcII.minRowBytes());
1118 if (!src.readPixels(tempSrc)) {
1119 return false;
1120 }
1121 srcColorType = allowedColorType;
1122 buffer = tempSrc.addr();
1123 rowBytes = tempSrc.rowBytes();
1124 if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
1125 std::unique_ptr<char[]> row(new char[rowBytes]);
1126 for (int y = 0; y < height / 2; ++y) {
1127 memcpy(row.get(), tempSrc.addr(0, y), rowBytes);
1128 memcpy(tempSrc.writable_addr(0, y), tempSrc.addr(0, height - 1 - y), rowBytes);
1129 memcpy(tempSrc.writable_addr(0, height - 1 - y), row.get(), rowBytes);
1130 }
1131 top = dstProxy->height() - top - height;
1132 }
1133 } else if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
1134 size_t trimRowBytes = GrColorTypeBytesPerPixel(srcColorType) * width;
1135 tempBuffer.reset(new char[trimRowBytes * height]);
1136 char* dst = reinterpret_cast<char*>(tempBuffer.get()) + trimRowBytes * (height - 1);
1137 const char* src = reinterpret_cast<const char*>(buffer);
1138 for (int i = 0; i < height; ++i, src += rowBytes, dst -= trimRowBytes) {
1139 memcpy(dst, src, trimRowBytes);
1140 }
1141 buffer = tempBuffer.get();
1142 rowBytes = trimRowBytes;
1143 top = dstProxy->height() - top - height;
1144 }
1145
1146 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && dstSurface->surfacePriv().hasPendingIO()) {
1147 this->flush(nullptr); // MDB TODO: tighten this
1148 }
1149
1150 return this->getGpu()->writePixels(dstSurface, left, top, width, height, srcColorType, buffer,
1151 rowBytes);
1152}
1153
Robert Phillips7ee385e2017-03-30 08:02:11 -04001154void GrContextPriv::prepareSurfaceForExternalIO(GrSurfaceProxy* proxy) {
1155 ASSERT_SINGLE_OWNER_PRIV
1156 RETURN_IF_ABANDONED_PRIV
1157 SkASSERT(proxy);
1158 ASSERT_OWNED_PROXY_PRIV(proxy);
Greg Daniel51316782017-08-02 15:10:09 +00001159 fContext->fDrawingManager->prepareSurfaceForExternalIO(proxy, 0, nullptr);
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001160}
1161
Robert Phillips7ee385e2017-03-30 08:02:11 -04001162void GrContextPriv::flushSurfaceWrites(GrSurfaceProxy* proxy) {
1163 ASSERT_SINGLE_OWNER_PRIV
1164 RETURN_IF_ABANDONED_PRIV
1165 SkASSERT(proxy);
1166 ASSERT_OWNED_PROXY_PRIV(proxy);
1167 if (proxy->priv().hasPendingWrite()) {
1168 this->flush(proxy);
bsalomonf80bfed2014-10-07 05:56:02 -07001169 }
1170}
1171
Robert Phillips7ee385e2017-03-30 08:02:11 -04001172void GrContextPriv::flushSurfaceIO(GrSurfaceProxy* proxy) {
1173 ASSERT_SINGLE_OWNER_PRIV
1174 RETURN_IF_ABANDONED_PRIV
1175 SkASSERT(proxy);
1176 ASSERT_OWNED_PROXY_PRIV(proxy);
1177 if (proxy->priv().hasPendingIO()) {
1178 this->flush(proxy);
ajuma95243eb2016-08-24 08:19:02 -07001179 }
1180}
1181
bsalomon@google.com27847de2011-02-22 20:59:41 +00001182////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +00001183
Robert Phillips2c862492017-01-18 10:08:39 -05001184sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy> proxy,
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -05001185 sk_sp<SkColorSpace> colorSpace,
1186 const SkSurfaceProps* props) {
Brian Osman45580d32016-11-23 09:37:01 -05001187 ASSERT_SINGLE_OWNER_PRIV
1188
Brian Salomon366093f2018-02-13 09:25:22 -05001189 // sRGB pixel configs may only be used with near-sRGB gamma color spaces.
1190 if (GrPixelConfigIsSRGB(proxy->config())) {
1191 if (!colorSpace || !colorSpace->gammaCloseToSRGB()) {
1192 return nullptr;
1193 }
1194 }
Brian Osman45580d32016-11-23 09:37:01 -05001195 if (proxy->asRenderTargetProxy()) {
Robert Phillips2c862492017-01-18 10:08:39 -05001196 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -05001197 std::move(colorSpace), props);
Brian Osman45580d32016-11-23 09:37:01 -05001198 } else {
1199 SkASSERT(proxy->asTextureProxy());
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -05001200 SkASSERT(!props);
Robert Phillips2c862492017-01-18 10:08:39 -05001201 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Brian Osman45580d32016-11-23 09:37:01 -05001202 }
1203}
1204
Robert Phillipse2f7d182016-12-15 09:23:05 -05001205sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(const GrSurfaceDesc& dstDesc,
Greg Daniel65c7f662017-10-30 13:39:09 -04001206 GrMipMapped mipMapped,
Robert Phillipse2f7d182016-12-15 09:23:05 -05001207 SkBackingFit fit,
Brian Salomon366093f2018-02-13 09:25:22 -05001208 SkBudgeted isDstBudgeted,
1209 sk_sp<SkColorSpace> colorSpace,
1210 const SkSurfaceProps* props) {
Greg Daniel65c7f662017-10-30 13:39:09 -04001211 sk_sp<GrTextureProxy> proxy;
1212 if (GrMipMapped::kNo == mipMapped) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001213 proxy = this->proxyProvider()->createProxy(dstDesc, fit, isDstBudgeted);
Greg Daniel65c7f662017-10-30 13:39:09 -04001214 } else {
1215 SkASSERT(SkBackingFit::kExact == fit);
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001216 proxy = this->proxyProvider()->createMipMapProxy(dstDesc, isDstBudgeted);
Greg Daniel65c7f662017-10-30 13:39:09 -04001217 }
Robert Phillips77b3f322017-01-31 18:24:12 -05001218 if (!proxy) {
1219 return nullptr;
1220 }
Robert Phillipse2f7d182016-12-15 09:23:05 -05001221
Brian Salomon366093f2018-02-13 09:25:22 -05001222 return this->makeWrappedSurfaceContext(std::move(proxy), std::move(colorSpace), props);
Robert Phillipse2f7d182016-12-15 09:23:05 -05001223}
1224
Brian Salomond17f6582017-07-19 18:28:58 -04001225sk_sp<GrTextureContext> GrContextPriv::makeBackendTextureContext(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +00001226 GrSurfaceOrigin origin,
Brian Osmanc1e37052017-03-09 14:19:20 -05001227 sk_sp<SkColorSpace> colorSpace) {
Robert Phillips26caf892017-01-27 10:58:31 -05001228 ASSERT_SINGLE_OWNER_PRIV
1229
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001230 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->createWrappedTextureProxy(tex, origin);
Robert Phillips77b3f322017-01-31 18:24:12 -05001231 if (!proxy) {
1232 return nullptr;
1233 }
Robert Phillips26caf892017-01-27 10:58:31 -05001234
Brian Salomond17f6582017-07-19 18:28:58 -04001235 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Robert Phillips26caf892017-01-27 10:58:31 -05001236}
1237
Brian Osman11052242016-10-27 14:47:55 -04001238sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +00001239 const GrBackendTexture& tex,
1240 GrSurfaceOrigin origin,
1241 int sampleCnt,
Brian Osman11052242016-10-27 14:47:55 -04001242 sk_sp<SkColorSpace> colorSpace,
Brian Osmanc1e37052017-03-09 14:19:20 -05001243 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -07001244 ASSERT_SINGLE_OWNER_PRIV
Brian Salomonbdecacf2018-02-02 20:32:49 -05001245 SkASSERT(sampleCnt > 0);
robertphillips4fd74ae2016-08-03 14:26:53 -07001246
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001247 sk_sp<GrTextureProxy> proxy(this->proxyProvider()->createWrappedTextureProxy(tex, origin,
1248 sampleCnt));
Robert Phillips77b3f322017-01-31 18:24:12 -05001249 if (!proxy) {
1250 return nullptr;
1251 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001252
Robert Phillips37430132016-11-09 06:50:43 -05001253 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Brian Osman11052242016-10-27 14:47:55 -04001254 std::move(colorSpace), props);
robertphillips4fd74ae2016-08-03 14:26:53 -07001255}
1256
Brian Osman11052242016-10-27 14:47:55 -04001257sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext(
Greg Danielbcf612b2017-05-01 13:50:58 +00001258 const GrBackendRenderTarget& backendRT,
1259 GrSurfaceOrigin origin,
robertphillips4fd74ae2016-08-03 14:26:53 -07001260 sk_sp<SkColorSpace> colorSpace,
1261 const SkSurfaceProps* surfaceProps) {
1262 ASSERT_SINGLE_OWNER_PRIV
1263
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001264 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->createWrappedRenderTargetProxy(backendRT,
1265 origin);
Robert Phillips77b3f322017-01-31 18:24:12 -05001266 if (!proxy) {
1267 return nullptr;
1268 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001269
Robert Phillips37430132016-11-09 06:50:43 -05001270 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001271 std::move(colorSpace),
Brian Osman11052242016-10-27 14:47:55 -04001272 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -07001273}
1274
Brian Osman11052242016-10-27 14:47:55 -04001275sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +00001276 const GrBackendTexture& tex,
1277 GrSurfaceOrigin origin,
1278 int sampleCnt,
robertphillips4fd74ae2016-08-03 14:26:53 -07001279 sk_sp<SkColorSpace> colorSpace,
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001280 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -07001281 ASSERT_SINGLE_OWNER_PRIV
Brian Salomonbdecacf2018-02-02 20:32:49 -05001282 SkASSERT(sampleCnt > 0);
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001283 sk_sp<GrSurfaceProxy> proxy(this->proxyProvider()->createWrappedRenderTargetProxy(tex, origin,
1284 sampleCnt));
Robert Phillips77b3f322017-01-31 18:24:12 -05001285 if (!proxy) {
1286 return nullptr;
1287 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001288
Robert Phillips37430132016-11-09 06:50:43 -05001289 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001290 std::move(colorSpace),
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001291 props);
robertphillips77a2e522015-10-17 07:43:27 -07001292}
1293
Chris Daltonfe199b72017-05-05 11:26:15 -04001294void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
1295 fContext->fDrawingManager->addOnFlushCallbackObject(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -04001296}
1297
Robert Phillips62000362018-02-01 09:10:04 -05001298void GrContextPriv::moveOpListsToDDL(SkDeferredDisplayList* ddl) {
1299 fContext->fDrawingManager->moveOpListsToDDL(ddl);
1300}
1301
1302void GrContextPriv::copyOpListsFromDDL(const SkDeferredDisplayList* ddl,
1303 GrRenderTargetProxy* newDest) {
1304 fContext->fDrawingManager->copyOpListsFromDDL(ddl, newDest);
1305}
1306
robertphillips48fde9c2016-09-06 05:20:20 -07001307static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) {
Brian Osman78f20e02017-01-12 10:28:01 -05001308 switch (config) {
1309 case kAlpha_8_GrPixelConfig:
1310 case kRGB_565_GrPixelConfig:
1311 case kRGBA_4444_GrPixelConfig:
1312 case kBGRA_8888_GrPixelConfig:
1313 return kRGBA_8888_GrPixelConfig;
1314 case kSBGRA_8888_GrPixelConfig:
1315 return kSRGBA_8888_GrPixelConfig;
1316 case kAlpha_half_GrPixelConfig:
1317 return kRGBA_half_GrPixelConfig;
1318 default:
1319 return kUnknown_GrPixelConfig;
1320 }
robertphillips48fde9c2016-09-06 05:20:20 -07001321}
1322
robertphillipsd728f0c2016-11-21 11:05:03 -08001323sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContextWithFallback(
1324 SkBackingFit fit,
1325 int width, int height,
1326 GrPixelConfig config,
1327 sk_sp<SkColorSpace> colorSpace,
1328 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -04001329 GrMipMapped mipMapped,
robertphillipsd728f0c2016-11-21 11:05:03 -08001330 GrSurfaceOrigin origin,
1331 const SkSurfaceProps* surfaceProps,
1332 SkBudgeted budgeted) {
Brian Salomonbdecacf2018-02-02 20:32:49 -05001333 SkASSERT(sampleCnt > 0);
1334 if (0 == this->caps()->getRenderTargetSampleCount(sampleCnt, config)) {
robertphillipsd728f0c2016-11-21 11:05:03 -08001335 config = GrPixelConfigFallback(config);
1336 }
1337
1338 return this->makeDeferredRenderTargetContext(fit, width, height, config, std::move(colorSpace),
Greg Daniel45d63032017-10-30 13:41:26 -04001339 sampleCnt, mipMapped, origin, surfaceProps,
Greg Daniele1da1d92017-10-06 15:59:27 -04001340 budgeted);
robertphillipsd728f0c2016-11-21 11:05:03 -08001341}
1342
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001343sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContext(
1344 SkBackingFit fit,
1345 int width, int height,
1346 GrPixelConfig config,
1347 sk_sp<SkColorSpace> colorSpace,
1348 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -04001349 GrMipMapped mipMapped,
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001350 GrSurfaceOrigin origin,
1351 const SkSurfaceProps* surfaceProps,
1352 SkBudgeted budgeted) {
Brian Salomonbdecacf2018-02-02 20:32:49 -05001353 SkASSERT(sampleCnt > 0);
Brian Salomon79e4d1b2017-07-13 11:17:11 -04001354 if (this->abandoned()) {
1355 return nullptr;
1356 }
1357
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001358 GrSurfaceDesc desc;
1359 desc.fFlags = kRenderTarget_GrSurfaceFlag;
1360 desc.fOrigin = origin;
1361 desc.fWidth = width;
1362 desc.fHeight = height;
1363 desc.fConfig = config;
1364 desc.fSampleCnt = sampleCnt;
1365
Greg Daniele1da1d92017-10-06 15:59:27 -04001366 sk_sp<GrTextureProxy> rtp;
Greg Daniel45d63032017-10-30 13:41:26 -04001367 if (GrMipMapped::kNo == mipMapped) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001368 rtp = fProxyProvider->createProxy(desc, fit, budgeted);
Greg Daniele1da1d92017-10-06 15:59:27 -04001369 } else {
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001370 rtp = fProxyProvider->createMipMapProxy(desc, budgeted);
Greg Daniele1da1d92017-10-06 15:59:27 -04001371 }
Robert Phillips08c5ec72017-01-30 12:26:47 -05001372 if (!rtp) {
1373 return nullptr;
1374 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001375
Robert Phillips1119dc32017-04-11 12:54:57 -04001376 sk_sp<GrRenderTargetContext> renderTargetContext(
1377 fDrawingManager->makeRenderTargetContext(std::move(rtp),
1378 std::move(colorSpace),
1379 surfaceProps));
Robert Phillips1119dc32017-04-11 12:54:57 -04001380 if (!renderTargetContext) {
1381 return nullptr;
1382 }
1383
1384 renderTargetContext->discard();
1385
1386 return renderTargetContext;
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001387}
1388
joshualitt1de610a2016-01-06 08:26:09 -08001389bool GrContext::abandoned() const {
1390 ASSERT_SINGLE_OWNER
robertphillips7761d612016-05-16 09:14:53 -07001391 return fDrawingManager->wasAbandoned();
robertphillips77a2e522015-10-17 07:43:27 -07001392}
1393
Brian Salomonaff329b2017-08-11 09:40:37 -04001394std::unique_ptr<GrFragmentProcessor> GrContext::createPMToUPMEffect(
1395 std::unique_ptr<GrFragmentProcessor> fp, bool useConfigConversionEffect) {
Robert Phillips757914d2017-01-25 15:48:30 -05001396 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -04001397 // We have specialized effects that guarantee round-trip conversion for some formats
1398 if (useConfigConversionEffect) {
1399 // We should have already called this->validPMUPMConversionExists() in this case
1400 SkASSERT(fDidTestPMConversions);
1401 // ...and it should have succeeded
1402 SkASSERT(this->validPMUPMConversionExists());
1403
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001404 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul);
Brian Osman2d2da4f2017-04-12 17:07:22 -04001405 } else {
1406 // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and
1407 // explicitly round the results. Just do the obvious, naive thing in the shader.
1408 return GrFragmentProcessor::UnpremulOutput(std::move(fp));
Robert Phillips757914d2017-01-25 15:48:30 -05001409 }
1410}
1411
Brian Salomonaff329b2017-08-11 09:40:37 -04001412std::unique_ptr<GrFragmentProcessor> GrContext::createUPMToPMEffect(
1413 std::unique_ptr<GrFragmentProcessor> fp, bool useConfigConversionEffect) {
joshualitt1de610a2016-01-06 08:26:09 -08001414 ASSERT_SINGLE_OWNER
Brian Osman2d2da4f2017-04-12 17:07:22 -04001415 // We have specialized effects that guarantee round-trip conversion for these formats
Brian Osman409e74f2017-04-17 11:48:28 -04001416 if (useConfigConversionEffect) {
1417 // We should have already called this->validPMUPMConversionExists() in this case
1418 SkASSERT(fDidTestPMConversions);
1419 // ...and it should have succeeded
1420 SkASSERT(this->validPMUPMConversionExists());
1421
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001422 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
Brian Osman2d2da4f2017-04-12 17:07:22 -04001423 } else {
1424 // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and
1425 // explicitly round the results. Just do the obvious, naive thing in the shader.
1426 return GrFragmentProcessor::PremulOutput(std::move(fp));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001427 }
1428}
1429
Brian Osman409e74f2017-04-17 11:48:28 -04001430bool GrContext::validPMUPMConversionExists() {
joshualitt1de610a2016-01-06 08:26:09 -08001431 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -04001432 if (!fDidTestPMConversions) {
Brian Osman28804f32017-04-20 10:24:36 -04001433 fPMUPMConversionsRoundTrip = GrConfigConversionEffect::TestForPreservingPMConversions(this);
Brian Osman409e74f2017-04-17 11:48:28 -04001434 fDidTestPMConversions = true;
1435 }
1436
bsalomon636e8022015-07-29 06:08:46 -07001437 // The PM<->UPM tests fail or succeed together so we only need to check one.
Brian Osman28804f32017-04-20 10:24:36 -04001438 return fPMUPMConversionsRoundTrip;
bsalomon636e8022015-07-29 06:08:46 -07001439}
1440
bsalomon37f9a262015-02-02 13:00:10 -08001441//////////////////////////////////////////////////////////////////////////////
1442
Robert Phillipsfc711a22018-02-13 17:03:00 -05001443// DDL TODO: remove 'maxResources'
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001444void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -08001445 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001446 if (maxResources) {
1447 *maxResources = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -08001448 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001449 if (maxResourceBytes) {
1450 *maxResourceBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -08001451 }
1452}
1453
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001454void GrContext::setResourceCacheLimits(int maxResources, size_t maxResourceBytes) {
joshualitt1de610a2016-01-06 08:26:09 -08001455 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001456 fResourceCache->setLimits(maxResources, maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -08001457}
1458
ericrk0a5fa482015-09-15 14:16:10 -07001459//////////////////////////////////////////////////////////////////////////////
1460
1461void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -08001462 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -07001463 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
1464}
Brian Osman71a18892017-08-10 10:23:25 -04001465
1466//////////////////////////////////////////////////////////////////////////////
1467
1468SkString GrContext::dump() const {
1469 SkDynamicMemoryWStream stream;
1470 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
1471 writer.beginObject();
1472
1473 static const char* kBackendStr[] = {
1474 "Metal",
1475 "OpenGL",
1476 "Vulkan",
1477 "Mock",
1478 };
1479 GR_STATIC_ASSERT(0 == kMetal_GrBackend);
1480 GR_STATIC_ASSERT(1 == kOpenGL_GrBackend);
1481 GR_STATIC_ASSERT(2 == kVulkan_GrBackend);
1482 GR_STATIC_ASSERT(3 == kMock_GrBackend);
1483 writer.appendString("backend", kBackendStr[fBackend]);
1484
1485 writer.appendName("caps");
1486 fCaps->dumpJSON(&writer);
1487
1488 writer.appendName("gpu");
1489 fGpu->dumpJSON(&writer);
1490
1491 // Flush JSON to the memory stream
1492 writer.endObject();
1493 writer.flush();
1494
1495 // Null terminate the JSON data in the memory stream
1496 stream.write8(0);
1497
1498 // Allocate a string big enough to hold all the data, then copy out of the stream
1499 SkString result(stream.bytesWritten());
1500 stream.copyToAndReset(result.writable_str());
1501 return result;
1502}