blob: 931357f11396871819a67e737f282ae62fa3472e [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 Phillips05518182018-02-28 14:19:04 +000067 GrDirectContext(GrBackend backend) : INHERITED(backend) { }
Robert Phillips88260b52018-01-19 12:56:09 -050068
69protected:
70
71private:
72 typedef GrContext INHERITED;
73};
74
75class SK_API GrDDLContext : public GrContext {
76public:
Robert Phillips05518182018-02-28 14:19:04 +000077 GrDDLContext(GrContextThreadSafeProxy* proxy) : INHERITED(proxy) {}
Robert Phillips88260b52018-01-19 12:56:09 -050078
79protected:
80
81private:
82 typedef GrContext INHERITED;
83};
84
bsalomon682c2692015-05-22 14:01:46 -070085GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext) {
86 GrContextOptions defaultOptions;
87 return Create(backend, backendContext, defaultOptions);
88}
bsalomonf28cff72015-05-22 12:25:41 -070089
bsalomon682c2692015-05-22 14:01:46 -070090GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
91 const GrContextOptions& options) {
bsalomon682c2692015-05-22 14:01:46 -070092
Robert Phillips88260b52018-01-19 12:56:09 -050093 sk_sp<GrContext> context(new GrDirectContext(backend));
Robert Phillipse42edcc2017-12-13 11:50:22 -050094
95 context->fGpu = GrGpu::Make(backend, backendContext, options, context.get());
96 if (!context->fGpu) {
halcanary96fcdcc2015-08-27 07:41:13 -070097 return nullptr;
bsalomon@google.com27847de2011-02-22 20:59:41 +000098 }
Robert Phillipse42edcc2017-12-13 11:50:22 -050099
100 if (!context->init(options)) {
101 return nullptr;
102 }
103
Brian Salomon91a3e522017-06-23 10:58:19 -0400104 return context.release();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000105}
106
Brian Salomon384fab42017-12-07 12:33:05 -0500107sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface) {
Greg Daniel02611d92017-07-25 10:05:01 -0400108 GrContextOptions defaultOptions;
Brian Salomon384fab42017-12-07 12:33:05 -0500109 return MakeGL(std::move(interface), defaultOptions);
Greg Daniel02611d92017-07-25 10:05:01 -0400110}
111
Brian Salomon384fab42017-12-07 12:33:05 -0500112sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface,
Greg Daniel02611d92017-07-25 10:05:01 -0400113 const GrContextOptions& options) {
Robert Phillips88260b52018-01-19 12:56:09 -0500114 sk_sp<GrContext> context(new GrDirectContext(kOpenGL_GrBackend));
Robert Phillipse42edcc2017-12-13 11:50:22 -0500115
Brian Salomon384fab42017-12-07 12:33:05 -0500116 context->fGpu = GrGLGpu::Make(std::move(interface), options, context.get());
Greg Daniel02611d92017-07-25 10:05:01 -0400117 if (!context->fGpu) {
118 return nullptr;
119 }
Greg Daniel02611d92017-07-25 10:05:01 -0400120 if (!context->init(options)) {
121 return nullptr;
122 }
123 return context;
124}
125
Brian Salomon990014d2017-12-07 16:23:39 -0500126sk_sp<GrContext> GrContext::MakeGL(const GrGLInterface* interface) {
127 return MakeGL(sk_ref_sp(interface));
128}
129
130sk_sp<GrContext> GrContext::MakeGL(const GrGLInterface* interface,
131 const GrContextOptions& options) {
132 return MakeGL(sk_ref_sp(interface), options);
133}
134
Greg Daniel02611d92017-07-25 10:05:01 -0400135sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions) {
136 GrContextOptions defaultOptions;
137 return MakeMock(mockOptions, defaultOptions);
138}
139
140sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions,
141 const GrContextOptions& options) {
Robert Phillips88260b52018-01-19 12:56:09 -0500142 sk_sp<GrContext> context(new GrDirectContext(kMock_GrBackend));
Robert Phillipse42edcc2017-12-13 11:50:22 -0500143
Brian Salomon384fab42017-12-07 12:33:05 -0500144 context->fGpu = GrMockGpu::Make(mockOptions, options, context.get());
Greg Daniel02611d92017-07-25 10:05:01 -0400145 if (!context->fGpu) {
146 return nullptr;
147 }
Greg Daniel02611d92017-07-25 10:05:01 -0400148 if (!context->init(options)) {
149 return nullptr;
150 }
151 return context;
152}
153
154#ifdef SK_VULKAN
Brian Salomon384fab42017-12-07 12:33:05 -0500155sk_sp<GrContext> GrContext::MakeVulkan(sk_sp<const GrVkBackendContext> backendContext) {
Greg Daniel02611d92017-07-25 10:05:01 -0400156 GrContextOptions defaultOptions;
Brian Salomon384fab42017-12-07 12:33:05 -0500157 return MakeVulkan(std::move(backendContext), defaultOptions);
Greg Daniel02611d92017-07-25 10:05:01 -0400158}
159
Brian Salomon384fab42017-12-07 12:33:05 -0500160sk_sp<GrContext> GrContext::MakeVulkan(sk_sp<const GrVkBackendContext> backendContext,
Greg Daniel02611d92017-07-25 10:05:01 -0400161 const GrContextOptions& options) {
Robert Phillips88260b52018-01-19 12:56:09 -0500162 sk_sp<GrContext> context(new GrDirectContext(kVulkan_GrBackend));
Robert Phillipse42edcc2017-12-13 11:50:22 -0500163
Brian Salomon384fab42017-12-07 12:33:05 -0500164 context->fGpu = GrVkGpu::Make(std::move(backendContext), options, context.get());
Greg Daniel02611d92017-07-25 10:05:01 -0400165 if (!context->fGpu) {
166 return nullptr;
167 }
Greg Daniel02611d92017-07-25 10:05:01 -0400168 if (!context->init(options)) {
169 return nullptr;
170 }
171 return context;
172}
173#endif
174
Greg Danielb76a72a2017-07-13 15:07:54 -0400175#ifdef SK_METAL
Greg Daniel02611d92017-07-25 10:05:01 -0400176sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue) {
177 GrContextOptions defaultOptions;
178 return MakeMetal(device, queue, defaultOptions);
179}
180
Greg Danielb76a72a2017-07-13 15:07:54 -0400181sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) {
Robert Phillips05518182018-02-28 14:19:04 +0000182 sk_sp<GrContext> context(new GrContext(kMetal_GrBackend));
Robert Phillipse42edcc2017-12-13 11:50:22 -0500183
Brian Salomon384fab42017-12-07 12:33:05 -0500184 context->fGpu = GrMtlTrampoline::MakeGpu(context.get(), options, device, queue);
Greg Danielb76a72a2017-07-13 15:07:54 -0400185 if (!context->fGpu) {
186 return nullptr;
187 }
Greg Danielb76a72a2017-07-13 15:07:54 -0400188 if (!context->init(options)) {
189 return nullptr;
190 }
191 return context;
192}
193#endif
194
joshualitt0acd0d32015-05-07 08:23:19 -0700195static int32_t gNextID = 1;
196static int32_t next_id() {
197 int32_t id;
198 do {
199 id = sk_atomic_inc(&gNextID);
200 } while (id == SK_InvalidGenID);
201 return id;
202}
203
Robert Phillipse42edcc2017-12-13 11:50:22 -0500204sk_sp<GrContext> GrContextPriv::MakeDDL(GrContextThreadSafeProxy* proxy) {
Robert Phillips88260b52018-01-19 12:56:09 -0500205 sk_sp<GrContext> context(new GrDDLContext(proxy));
Robert Phillipse42edcc2017-12-13 11:50:22 -0500206
Robert Phillips88260b52018-01-19 12:56:09 -0500207 // Note: we aren't creating a Gpu here. This causes the resource provider & cache to
208 // also not be created
Robert Phillipse42edcc2017-12-13 11:50:22 -0500209 if (!context->init(proxy->fOptions)) {
210 return nullptr;
211 }
212 return context;
213}
214
215GrContext::GrContext(GrBackend backend)
216 : fUniqueID(next_id())
217 , fBackend(backend) {
halcanary96fcdcc2015-08-27 07:41:13 -0700218 fResourceCache = nullptr;
219 fResourceProvider = nullptr;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500220 fProxyProvider = nullptr;
Robert Phillips5c56af12018-02-28 16:37:34 +0000221 fAtlasGlyphCache = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000222}
223
Robert Phillipse42edcc2017-12-13 11:50:22 -0500224GrContext::GrContext(GrContextThreadSafeProxy* proxy)
Robert Phillips88260b52018-01-19 12:56:09 -0500225 : fCaps(proxy->fCaps)
226 , fUniqueID(proxy->fContextUniqueID)
Robert Phillipse42edcc2017-12-13 11:50:22 -0500227 , fBackend(proxy->fBackend) {
228 fResourceCache = nullptr;
229 fResourceProvider = nullptr;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500230 fProxyProvider = nullptr;
Robert Phillips5c56af12018-02-28 16:37:34 +0000231 fAtlasGlyphCache = nullptr;
Greg Danielb76a72a2017-07-13 15:07:54 -0400232}
joshualitt1de610a2016-01-06 08:26:09 -0800233
Greg Danielb76a72a2017-07-13 15:07:54 -0400234bool GrContext::init(const GrContextOptions& options) {
235 ASSERT_SINGLE_OWNER
Robert Phillips88260b52018-01-19 12:56:09 -0500236
237 if (fGpu) {
238 fCaps = fGpu->refCaps();
239 fResourceCache = new GrResourceCache(fCaps.get(), fUniqueID);
Robert Phillips4150eea2018-02-07 17:08:21 -0500240 fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, &fSingleOwner,
241 options.fExplicitlyAllocateGPUResources);
Robert Phillips88260b52018-01-19 12:56:09 -0500242 }
243
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500244 fProxyProvider = new GrProxyProvider(fResourceProvider, fResourceCache, fCaps, &fSingleOwner);
Robert Phillips88260b52018-01-19 12:56:09 -0500245
246 if (fResourceCache) {
247 fResourceCache->setProxyProvider(fProxyProvider);
248 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500249
Robert Phillipse42edcc2017-12-13 11:50:22 -0500250 // DDL TODO: we need to think through how the task group & persistent cache
251 // get passed on to/shared between all the DDLRecorders created with this context.
252 fThreadSafeProxy.reset(new GrContextThreadSafeProxy(fCaps, this->uniqueID(), fBackend,
253 options));
commit-bot@chromium.org1836d332013-07-16 22:55:03 +0000254
Brian Osman46da1cc2017-02-14 14:15:48 -0500255 fDisableGpuYUVConversion = options.fDisableGpuYUVConversion;
Brian Osman8a83ca42018-02-12 14:32:17 -0500256 fSharpenMipmappedTextures = options.fSharpenMipmappedTextures;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000257 fDidTestPMConversions = false;
258
bsalomon6b2552f2016-09-15 13:50:26 -0700259 GrPathRendererChain::Options prcOptions;
bsalomon39ef7fb2016-09-21 11:16:05 -0700260 prcOptions.fAllowPathMaskCaching = options.fAllowPathMaskCaching;
Brian Osman195c05b2017-08-30 15:14:04 -0400261#if GR_TEST_UTILS
csmartdalton008b9d82017-02-22 12:00:42 -0700262 prcOptions.fGpuPathRenderers = options.fGpuPathRenderers;
Brian Osman195c05b2017-08-30 15:14:04 -0400263#endif
Brian Osmanb350ae22017-08-29 16:15:39 -0400264 if (options.fDisableDistanceFieldPaths) {
Brian Osman195c05b2017-08-30 15:14:04 -0400265 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
Brian Osmanb350ae22017-08-29 16:15:39 -0400266 }
Brian Salomonaf597482017-11-07 16:23:34 -0500267
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500268 if (!fResourceCache) {
269 // DDL TODO: remove this crippling of the path renderer chain
270 // Disable the small path renderer bc of the proxies in the atlas. They need to be
271 // unified when the opLists are added back to the destination drawing manager.
272 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
273 }
274
Brian Salomonaf597482017-11-07 16:23:34 -0500275 GrAtlasTextContext::Options atlasTextContextOptions;
276 atlasTextContextOptions.fMaxDistanceFieldFontSize = options.fGlyphsAsPathsFontSize;
277 atlasTextContextOptions.fMinDistanceFieldFontSize = options.fMinDistanceFieldFontSize;
Brian Salomonb5086962017-12-13 10:59:33 -0500278 atlasTextContextOptions.fDistanceFieldVerticesAlwaysHaveW = false;
279#if SK_SUPPORT_ATLAS_TEXT
280 if (GrContextOptions::Enable::kYes == options.fDistanceFieldGlyphVerticesAlwaysHaveW) {
281 atlasTextContextOptions.fDistanceFieldVerticesAlwaysHaveW = true;
282 }
283#endif
Brian Salomonaf597482017-11-07 16:23:34 -0500284
Robert Phillips4150eea2018-02-07 17:08:21 -0500285 fDrawingManager.reset(new GrDrawingManager(this, prcOptions, atlasTextContextOptions,
286 &fSingleOwner, options.fSortRenderTargets));
joshualitt7c3a2f82015-03-31 13:32:05 -0700287
Robert Phillips05518182018-02-28 14:19:04 +0000288 GrDrawOpAtlas::AllowMultitexturing allowMultitexturing;
289 if (GrContextOptions::Enable::kNo == options.fAllowMultipleGlyphCacheTextures ||
290 // multitexturing supported only if range can represent the index + texcoords fully
291 !(fCaps->shaderCaps()->floatIs32Bits() || fCaps->shaderCaps()->integerSupport())) {
292 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
293 } else {
294 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
295 }
Robert Phillips4bc70112018-03-01 10:24:02 -0500296 fAtlasGlyphCache = new GrAtlasGlyphCache(fProxyProvider, options.fGlyphCacheTextureMaximumBytes,
Robert Phillips5c56af12018-02-28 16:37:34 +0000297 allowMultitexturing);
298 this->contextPriv().addOnFlushCallbackObject(fAtlasGlyphCache);
Robert Phillips05518182018-02-28 14:19:04 +0000299
Robert Phillips303cd582018-02-14 18:54:01 -0500300 fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB,
301 this, this->uniqueID(), SkToBool(fGpu)));
Brian Salomon91a3e522017-06-23 10:58:19 -0400302
Brian Osman51279982017-08-23 10:12:00 -0400303 if (options.fExecutor) {
304 fTaskGroup = skstd::make_unique<SkTaskGroup>(*options.fExecutor);
305 }
306
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400307 fPersistentCache = options.fPersistentCache;
308
Brian Salomon91a3e522017-06-23 10:58:19 -0400309 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000310}
311
bsalomon@google.com27847de2011-02-22 20:59:41 +0000312GrContext::~GrContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800313 ASSERT_SINGLE_OWNER
314
Robert Phillips05518182018-02-28 14:19:04 +0000315 if (fGpu) {
316 this->flush();
317 }
318
Robert Phillips2e6feed2018-01-22 15:27:20 -0500319 if (fDrawingManager) {
320 fDrawingManager->cleanup();
321 }
robertphillips2334fb62015-06-17 05:43:33 -0700322
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000323 for (int i = 0; i < fCleanUpData.count(); ++i) {
324 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
325 }
326
halcanary385fe4d2015-08-26 13:07:48 -0700327 delete fResourceProvider;
328 delete fResourceCache;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500329 delete fProxyProvider;
Robert Phillips5c56af12018-02-28 16:37:34 +0000330 delete fAtlasGlyphCache;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000331}
332
bungeman6bd52842016-10-27 09:30:08 -0700333sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
bungeman6bd52842016-10-27 09:30:08 -0700334 return fThreadSafeProxy;
bsalomon41b952c2016-03-11 06:46:33 -0800335}
336
Robert Phillipsfc711a22018-02-13 17:03:00 -0500337SkSurfaceCharacterization GrContextThreadSafeProxy::createCharacterization(
338 size_t cacheMaxResourceBytes,
339 const SkImageInfo& ii, const GrBackendFormat& backendFormat,
340 int sampleCnt, GrSurfaceOrigin origin,
341 const SkSurfaceProps& surfaceProps,
342 bool isMipMapped) {
343 if (!backendFormat.isValid()) {
344 return SkSurfaceCharacterization(); // return an invalid characterization
345 }
346
347 // We're assuming GrFSAAType::kMixedSamples will never be specified via this code path
348 GrFSAAType FSAAType = sampleCnt > 1 ? GrFSAAType::kUnifiedMSAA : GrFSAAType::kNone;
349
350 if (!fCaps->mipMapSupport()) {
351 isMipMapped = false;
352 }
353
354 GrPixelConfig config = kUnknown_GrPixelConfig;
355 if (!fCaps->getConfigFromBackendFormat(backendFormat, ii.colorType(), &config)) {
356 return SkSurfaceCharacterization(); // return an invalid characterization
357 }
358
359 // This surface characterization factory assumes that the resulting characterization is
360 // textureable.
361 if (!fCaps->isConfigTexturable(config)) {
362 return SkSurfaceCharacterization(); // return an invalid characterization
363 }
364
365 return SkSurfaceCharacterization(sk_ref_sp<GrContextThreadSafeProxy>(this),
366 cacheMaxResourceBytes,
367 origin, ii.width(), ii.height(), config, FSAAType, sampleCnt,
368 SkSurfaceCharacterization::Textureable(true),
369 SkSurfaceCharacterization::MipMapped(isMipMapped),
370 ii.refColorSpace(), surfaceProps);
371}
372
bsalomon2354f842014-07-28 13:48:36 -0700373void GrContext::abandonContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800374 ASSERT_SINGLE_OWNER
375
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500376 fProxyProvider->abandon();
bsalomond309e7a2015-04-30 14:18:54 -0700377 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800378
379 // Need to abandon the drawing manager first so all the render targets
380 // will be released/forgotten before they too are abandoned.
381 fDrawingManager->abandon();
382
bsalomon@google.com205d4602011-04-25 12:43:45 +0000383 // abandon first to so destructors
384 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800385 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700386
bsalomon6e2aad42016-04-01 11:54:31 -0700387 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
388
Robert Phillips5c56af12018-02-28 16:37:34 +0000389 fAtlasGlyphCache->freeAll();
bsalomon6e2aad42016-04-01 11:54:31 -0700390 fTextBlobCache->freeAll();
391}
392
393void GrContext::releaseResourcesAndAbandonContext() {
394 ASSERT_SINGLE_OWNER
395
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500396 fProxyProvider->abandon();
bsalomon6e2aad42016-04-01 11:54:31 -0700397 fResourceProvider->abandon();
398
399 // Need to abandon the drawing manager first so all the render targets
400 // will be released/forgotten before they too are abandoned.
401 fDrawingManager->abandon();
402
403 // Release all resources in the backend 3D API.
404 fResourceCache->releaseAll();
405
406 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000407
Robert Phillips5c56af12018-02-28 16:37:34 +0000408 fAtlasGlyphCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700409 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000410}
411
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000412void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800413 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000414 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000415}
416
417void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800418 ASSERT_SINGLE_OWNER
419
Robert Phillips05518182018-02-28 14:19:04 +0000420 this->flush();
421
Robert Phillips5c56af12018-02-28 16:37:34 +0000422 fAtlasGlyphCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700423
424 fDrawingManager->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700425
426 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000427}
428
Jim Van Verth76d917c2017-12-13 09:26:37 -0500429void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) {
Brian Salomon5e150852017-03-22 14:53:13 -0400430 ASSERT_SINGLE_OWNER
Jim Van Verth76d917c2017-12-13 09:26:37 -0500431 fResourceCache->purgeAsNeeded();
432 fResourceCache->purgeResourcesNotUsedSince(GrStdSteadyClock::now() - msNotUsed);
433
434 fTextBlobCache->purgeStaleBlobs();
Brian Salomon5e150852017-03-22 14:53:13 -0400435}
436
Derek Sollenberger5480a182017-05-25 16:43:59 -0400437void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
438 ASSERT_SINGLE_OWNER
439 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
440}
441
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000442void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800443 ASSERT_SINGLE_OWNER
444
bsalomon71cb0c22014-11-14 12:10:14 -0800445 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800446 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800447 }
448 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800449 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800450 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000451}
452
Derek Sollenbergeree479142017-05-24 11:41:33 -0400453size_t GrContext::getResourceCachePurgeableBytes() const {
454 ASSERT_SINGLE_OWNER
455 return fResourceCache->getPurgeableBytes();
456}
457
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000458////////////////////////////////////////////////////////////////////////////////
459
Brian Salomonbdecacf2018-02-02 20:32:49 -0500460bool GrContext::colorTypeSupportedAsImage(SkColorType colorType) const {
461 GrPixelConfig config = SkImageInfo2GrPixelConfig(colorType, nullptr, *this->caps());
462 return this->caps()->isConfigTexturable(config);
463}
464
465int GrContext::maxSurfaceSampleCountForColorType(SkColorType colorType) const {
466 GrPixelConfig config = SkImageInfo2GrPixelConfig(colorType, nullptr, *this->caps());
467 return this->caps()->maxRenderTargetSampleCount(config);
468}
469
470////////////////////////////////////////////////////////////////////////////////
471
joshualitt0db6dfa2015-04-10 07:01:30 -0700472void GrContext::TextBlobCacheOverBudgetCB(void* data) {
473 SkASSERT(data);
Brian Osman11052242016-10-27 14:47:55 -0400474 // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on
475 // GrRenderTargetContext to perform a necessary flush. The solution is to move drawText calls
476 // to below the GrContext level, but this is not trivial because they call drawPath on
477 // SkGpuDevice.
joshualitt0db6dfa2015-04-10 07:01:30 -0700478 GrContext* context = reinterpret_cast<GrContext*>(data);
479 context->flush();
480}
481
bsalomon@google.com27847de2011-02-22 20:59:41 +0000482////////////////////////////////////////////////////////////////////////////////
483
bsalomonb77a9072016-09-07 10:02:04 -0700484void GrContext::flush() {
joshualitt1de610a2016-01-06 08:26:09 -0800485 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700486 RETURN_IF_ABANDONED
Robert Phillips7ee385e2017-03-30 08:02:11 -0400487
488 fDrawingManager->flush(nullptr);
489}
490
Greg Daniel51316782017-08-02 15:10:09 +0000491GrSemaphoresSubmitted GrContext::flushAndSignalSemaphores(int numSemaphores,
492 GrBackendSemaphore signalSemaphores[]) {
493 ASSERT_SINGLE_OWNER
494 if (fDrawingManager->wasAbandoned()) { return GrSemaphoresSubmitted::kNo; }
495
496 return fDrawingManager->flush(nullptr, numSemaphores, signalSemaphores);
497}
498
Robert Phillips7ee385e2017-03-30 08:02:11 -0400499void GrContextPriv::flush(GrSurfaceProxy* proxy) {
500 ASSERT_SINGLE_OWNER_PRIV
501 RETURN_IF_ABANDONED_PRIV
502 ASSERT_OWNED_PROXY_PRIV(proxy);
503
504 fContext->fDrawingManager->flush(proxy);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000505}
506
Brian Salomonc320b152018-02-20 14:05:36 -0500507bool sw_convert_to_premul(GrColorType srcColorType, int width, int height, size_t inRowBytes,
bsalomon81beccc2014-10-13 12:32:55 -0700508 const void* inPixels, size_t outRowBytes, void* outPixels) {
Brian Salomonc320b152018-02-20 14:05:36 -0500509 SkColorType colorType = GrColorTypeToSkColorType(srcColorType);
510 if (kUnknown_SkColorType == colorType || 4 != SkColorTypeBytesPerPixel(colorType)) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000511 return false;
512 }
bsalomon81beccc2014-10-13 12:32:55 -0700513
Matt Sarettc7b29082017-02-09 16:22:39 -0500514 for (int y = 0; y < height; y++) {
515 SkOpts::RGBA_to_rgbA((uint32_t*) outPixels, inPixels, width);
516 outPixels = SkTAddOffset<void>(outPixels, outRowBytes);
517 inPixels = SkTAddOffset<const void>(inPixels, inRowBytes);
518 }
bsalomon81beccc2014-10-13 12:32:55 -0700519
Matt Sarettc7b29082017-02-09 16:22:39 -0500520 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000521}
522
Brian Salomonc320b152018-02-20 14:05:36 -0500523// TODO: This will be removed when GrSurfaceContexts are aware of their color types.
524// (skbug.com/6718)
Brian Osmand2ca59a2017-04-13 14:03:57 -0400525static bool valid_premul_config(GrPixelConfig config) {
Brian Salomonc320b152018-02-20 14:05:36 -0500526 switch (config) {
527 case kUnknown_GrPixelConfig: return false;
528 case kAlpha_8_GrPixelConfig: return false;
529 case kGray_8_GrPixelConfig: return false;
530 case kRGB_565_GrPixelConfig: return false;
531 case kRGBA_4444_GrPixelConfig: return true;
532 case kRGBA_8888_GrPixelConfig: return true;
533 case kBGRA_8888_GrPixelConfig: return true;
534 case kSRGBA_8888_GrPixelConfig: return true;
535 case kSBGRA_8888_GrPixelConfig: return true;
Brian Osman44b61202018-03-01 10:49:26 -0500536 case kRGBA_1010102_GrPixelConfig: return true;
Brian Salomonc320b152018-02-20 14:05:36 -0500537 case kRGBA_float_GrPixelConfig: return true;
538 case kRG_float_GrPixelConfig: return false;
539 case kAlpha_half_GrPixelConfig: return false;
540 case kRGBA_half_GrPixelConfig: return true;
541 case kAlpha_8_as_Alpha_GrPixelConfig: return false;
542 case kAlpha_8_as_Red_GrPixelConfig: return false;
543 case kAlpha_half_as_Red_GrPixelConfig: return false;
544 case kGray_8_as_Lum_GrPixelConfig: return false;
545 case kGray_8_as_Red_GrPixelConfig: return false;
546 }
547 SK_ABORT("Invalid GrPixelConfig");
548 return false;
Brian Osmance425512017-03-22 14:37:50 -0400549}
550
Brian Salomonc320b152018-02-20 14:05:36 -0500551static bool valid_premul_color_type(GrColorType ct) {
552 switch (ct) {
Brian Osman44b61202018-03-01 10:49:26 -0500553 case GrColorType::kUnknown: return false;
554 case GrColorType::kAlpha_8: return false;
555 case GrColorType::kRGB_565: return false;
556 case GrColorType::kABGR_4444: return true;
557 case GrColorType::kRGBA_8888: return true;
558 case GrColorType::kBGRA_8888: return true;
559 case GrColorType::kRGBA_1010102: return true;
560 case GrColorType::kGray_8: return false;
561 case GrColorType::kAlpha_F16: return false;
562 case GrColorType::kRGBA_F16: return true;
563 case GrColorType::kRG_F32: return false;
564 case GrColorType::kRGBA_F32: return true;
Brian Salomonc320b152018-02-20 14:05:36 -0500565 }
566 SK_ABORT("Invalid GrColorType");
567 return false;
568}
569
570static bool valid_pixel_conversion(GrColorType cpuColorType, GrPixelConfig gpuConfig,
Brian Osmand2ca59a2017-04-13 14:03:57 -0400571 bool premulConversion) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400572 // We only allow premul <-> unpremul conversions for some formats
Brian Salomonc320b152018-02-20 14:05:36 -0500573 if (premulConversion &&
574 (!valid_premul_color_type(cpuColorType) || !valid_premul_config(gpuConfig))) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400575 return false;
576 }
577
578 return true;
579}
580
Brian Salomonc320b152018-02-20 14:05:36 -0500581static bool pm_upm_must_round_trip(GrColorType cpuColorType, const SkColorSpace* cpuColorSpace) {
582 return !cpuColorSpace &&
583 (GrColorType::kRGBA_8888 == cpuColorType || GrColorType::kBGRA_8888 == cpuColorType);
Brian Osman409e74f2017-04-17 11:48:28 -0400584}
585
Brian Salomonc320b152018-02-20 14:05:36 -0500586// TODO: This will be removed when GrSurfaceContexts are aware of their color types.
587// (skbug.com/6718)
588static bool pm_upm_must_round_trip(GrPixelConfig surfaceConfig,
589 const SkColorSpace* surfaceColorSpace) {
590 return !surfaceColorSpace &&
591 (kRGBA_8888_GrPixelConfig == surfaceConfig || kBGRA_8888_GrPixelConfig == surfaceConfig);
592}
593
594static GrSRGBConversion determine_write_pixels_srgb_conversion(GrColorType srcColorType,
595 const SkColorSpace* srcColorSpace,
596 GrSRGBEncoded dstSRGBEncoded,
597 const SkColorSpace* dstColorSpace,
598 const GrCaps& caps) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500599 // No support for sRGB-encoded alpha.
Brian Salomonc320b152018-02-20 14:05:36 -0500600 if (GrColorTypeIsAlphaOnly(srcColorType)) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500601 return GrSRGBConversion::kNone;
602 }
Brian Salomonc320b152018-02-20 14:05:36 -0500603 // No conversions without GPU support for sRGB. (Legacy mode)
604 if (!caps.srgbSupport()) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500605 return GrSRGBConversion::kNone;
606 }
Brian Salomonc320b152018-02-20 14:05:36 -0500607 // If the GrSurfaceContext has no color space then it is in legacy mode.
608 if (!dstColorSpace) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500609 return GrSRGBConversion::kNone;
610 }
611
612 bool srcColorSpaceIsSRGB = srcColorSpace && srcColorSpace->gammaCloseToSRGB();
613 bool dstColorSpaceIsSRGB = dstColorSpace->gammaCloseToSRGB();
614
615 // For now we are assuming that if color space of the dst does not have sRGB gamma then the
616 // texture format is not sRGB encoded and vice versa. Note that we already checked for "legacy"
Brian Salomonc320b152018-02-20 14:05:36 -0500617 // mode being forced on by caps above. This may change in the future. We will then have to
618 // perform shader based conversions.
Brian Salomon9b009bb2018-02-14 13:53:55 -0500619 SkASSERT(dstColorSpaceIsSRGB == (GrSRGBEncoded::kYes == dstSRGBEncoded));
620
Brian Salomon9b009bb2018-02-14 13:53:55 -0500621 if (srcColorSpaceIsSRGB == dstColorSpaceIsSRGB) {
622 return GrSRGBConversion::kNone;
623 }
624 return srcColorSpaceIsSRGB ? GrSRGBConversion::kSRGBToLinear : GrSRGBConversion::kLinearToSRGB;
625}
626
Brian Salomonc320b152018-02-20 14:05:36 -0500627static GrSRGBConversion determine_read_pixels_srgb_conversion(GrSRGBEncoded srcSRGBEncoded,
628 const SkColorSpace* srcColorSpace,
629 GrColorType dstColorType,
630 const SkColorSpace* dstColorSpace,
631 const GrCaps& caps) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500632 // This is symmetrical with the write version.
Brian Salomonc320b152018-02-20 14:05:36 -0500633 switch (determine_write_pixels_srgb_conversion(dstColorType, dstColorSpace, srcSRGBEncoded,
634 srcColorSpace, caps)) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500635 case GrSRGBConversion::kNone: return GrSRGBConversion::kNone;
636 case GrSRGBConversion::kLinearToSRGB: return GrSRGBConversion::kSRGBToLinear;
637 case GrSRGBConversion::kSRGBToLinear: return GrSRGBConversion::kLinearToSRGB;
638 }
639 return GrSRGBConversion::kNone;
640}
641
Brian Salomonc320b152018-02-20 14:05:36 -0500642bool GrContextPriv::writeSurfacePixels(GrSurfaceContext* dst, int left, int top, int width,
643 int height, GrColorType srcColorType,
644 SkColorSpace* srcColorSpace, const void* buffer,
645 size_t rowBytes, uint32_t pixelOpsFlags) {
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500646#ifndef SK_LEGACY_GPU_PIXEL_OPS
647 return this->writeSurfacePixels2(dst, left, top, width, height, srcColorType, srcColorSpace,
648 buffer, rowBytes, pixelOpsFlags);
649#endif
650
Brian Osmanb62ea222016-12-22 11:12:16 -0500651 // TODO: Color space conversion
652
Robert Phillipse78b7252017-04-06 07:59:41 -0400653 ASSERT_SINGLE_OWNER_PRIV
654 RETURN_FALSE_IF_ABANDONED_PRIV
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400655 SkASSERT(dst);
656 ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy());
Brian Salomondcbb9d92017-07-19 10:53:20 -0400657 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "writeSurfacePixels", fContext);
bsalomon6c6f6582015-09-10 08:12:46 -0700658
Robert Phillips6be756b2018-01-16 15:07:54 -0500659 if (!dst->asSurfaceProxy()->instantiate(this->resourceProvider())) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400660 return false;
661 }
662
Robert Phillips16d8ec62017-07-27 16:16:25 -0400663 GrSurfaceProxy* dstProxy = dst->asSurfaceProxy();
664 GrSurface* dstSurface = dstProxy->priv().peekSurface();
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400665
Brian Osmand2ca59a2017-04-13 14:03:57 -0400666 // 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 -0400667 const bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
Brian Salomonc320b152018-02-20 14:05:36 -0500668
669 if (!valid_pixel_conversion(srcColorType, dstProxy->config(), premul)) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400670 return false;
671 }
672
Brian Osman409e74f2017-04-17 11:48:28 -0400673 // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data,
674 // without any color spaces attached, and the caller wants us to premul.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400675 bool useConfigConversionEffect =
Brian Salomonc320b152018-02-20 14:05:36 -0500676 premul && pm_upm_must_round_trip(srcColorType, srcColorSpace) &&
Brian Salomonf3569f02017-10-24 12:52:33 -0400677 pm_upm_must_round_trip(dstProxy->config(), dst->colorSpaceInfo().colorSpace());
Brian Osman409e74f2017-04-17 11:48:28 -0400678
679 // Are we going to try to premul as part of a draw? For the non-legacy case, we always allow
680 // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly.
681 bool premulOnGpu = premul &&
682 (!useConfigConversionEffect || fContext->validPMUPMConversionExists());
bsalomon81beccc2014-10-13 12:32:55 -0700683
bsalomone8d21e82015-07-16 08:23:13 -0700684 // 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 -0700685 // necessary and because GrGpu::getWritePixelsInfo requires it.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400686 if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(),
Brian Salomonc320b152018-02-20 14:05:36 -0500687 GrColorTypeBytesPerPixel(srcColorType), &left, &top,
688 &width, &height, &buffer, &rowBytes)) {
bsalomone8d21e82015-07-16 08:23:13 -0700689 return false;
690 }
691
Brian Osman409e74f2017-04-17 11:48:28 -0400692 GrGpu::DrawPreference drawPreference = premulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference
693 : GrGpu::kNoDraw_DrawPreference;
bsalomonf0674512015-07-28 13:26:15 -0700694 GrGpu::WritePixelTempDrawInfo tempDrawInfo;
Brian Salomon9b009bb2018-02-14 13:53:55 -0500695 GrSRGBConversion srgbConversion = determine_write_pixels_srgb_conversion(
Brian Salomonc320b152018-02-20 14:05:36 -0500696 srcColorType, srcColorSpace, GrPixelConfigIsSRGBEncoded(dstProxy->config()),
697 dst->colorSpaceInfo().colorSpace(), *fContext->caps());
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400698 if (!fContext->fGpu->getWritePixelsInfo(dstSurface, dstProxy->origin(), width, height,
Brian Salomonc320b152018-02-20 14:05:36 -0500699 srcColorType, srgbConversion, &drawPreference,
Brian Salomon9b009bb2018-02-14 13:53:55 -0500700 &tempDrawInfo)) {
bsalomonf0674512015-07-28 13:26:15 -0700701 return false;
702 }
703
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400704 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && dstSurface->surfacePriv().hasPendingIO()) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400705 this->flush(nullptr); // MDB TODO: tighten this
bsalomonf0674512015-07-28 13:26:15 -0700706 }
707
Robert Phillips2f493142017-03-02 18:18:38 -0500708 sk_sp<GrTextureProxy> tempProxy;
bsalomonf0674512015-07-28 13:26:15 -0700709 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500710 tempProxy = this->proxyProvider()->createProxy(tempDrawInfo.fTempSurfaceDesc,
711 SkBackingFit::kApprox,
712 SkBudgeted::kYes);
Robert Phillips2f493142017-03-02 18:18:38 -0500713 if (!tempProxy && GrGpu::kRequireDraw_DrawPreference == drawPreference) {
bsalomonf0674512015-07-28 13:26:15 -0700714 return false;
715 }
716 }
717
718 // temp buffer for doing sw premul conversion, if needed.
719 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
Brian Osman409e74f2017-04-17 11:48:28 -0400720 // We need to do sw premul if we were unable to create a RT for drawing, or if we can't do the
721 // premul on the GPU
722 if (premul && (!tempProxy || !premulOnGpu)) {
723 size_t tmpRowBytes = 4 * width;
724 tmpPixels.reset(width * height);
Brian Salomonc320b152018-02-20 14:05:36 -0500725 if (!sw_convert_to_premul(srcColorType, width, height, rowBytes, buffer, tmpRowBytes,
Brian Osman409e74f2017-04-17 11:48:28 -0400726 tmpPixels.get())) {
727 return false;
bsalomonf0674512015-07-28 13:26:15 -0700728 }
Brian Osman409e74f2017-04-17 11:48:28 -0400729 rowBytes = tmpRowBytes;
730 buffer = tmpPixels.get();
bsalomonf0674512015-07-28 13:26:15 -0700731 }
Brian Osman409e74f2017-04-17 11:48:28 -0400732
733 if (tempProxy) {
Brian Osman2240be92017-10-18 13:15:13 -0400734 auto fp = GrSimpleTextureEffect::Make(tempProxy, SkMatrix::I());
Brian Osman409e74f2017-04-17 11:48:28 -0400735 if (premulOnGpu) {
736 fp = fContext->createUPMToPMEffect(std::move(fp), useConfigConversionEffect);
bsalomon81beccc2014-10-13 12:32:55 -0700737 }
Brian Osman409e74f2017-04-17 11:48:28 -0400738 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400739 if (!fp) {
740 return false;
741 }
Brian Osman409e74f2017-04-17 11:48:28 -0400742
Robert Phillips6be756b2018-01-16 15:07:54 -0500743 if (!tempProxy->instantiate(this->resourceProvider())) {
Brian Osman409e74f2017-04-17 11:48:28 -0400744 return false;
745 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400746 GrTexture* texture = tempProxy->priv().peekTexture();
Robert Phillips7bbbf622017-10-17 07:36:59 -0400747
748 if (tempProxy->priv().hasPendingIO()) {
749 this->flush(tempProxy.get());
750 }
751
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400752 if (!fContext->fGpu->writePixels(texture, tempProxy->origin(), 0, 0, width, height,
Brian Salomonc320b152018-02-20 14:05:36 -0500753 tempDrawInfo.fWriteColorType, buffer, rowBytes)) {
Brian Osman409e74f2017-04-17 11:48:28 -0400754 return false;
755 }
Robert Phillips7bbbf622017-10-17 07:36:59 -0400756 tempProxy = nullptr;
757
Brian Osman409e74f2017-04-17 11:48:28 -0400758 SkMatrix matrix;
759 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400760 GrRenderTargetContext* renderTargetContext = dst->asRenderTargetContext();
Brian Osman409e74f2017-04-17 11:48:28 -0400761 if (!renderTargetContext) {
762 return false;
763 }
764 GrPaint paint;
765 paint.addColorFragmentProcessor(std::move(fp));
766 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonf3569f02017-10-24 12:52:33 -0400767 paint.setAllowSRGBInputs(dst->colorSpaceInfo().isGammaCorrect() ||
768 GrPixelConfigIsSRGB(dst->colorSpaceInfo().config()));
Brian Osman409e74f2017-04-17 11:48:28 -0400769 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
770 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, matrix, rect,
771 nullptr);
772
773 if (kFlushWrites_PixelOp & pixelOpsFlags) {
774 this->flushSurfaceWrites(renderTargetContext->asRenderTargetProxy());
775 }
776 } else {
Brian Salomonc320b152018-02-20 14:05:36 -0500777 return fContext->fGpu->writePixels(dstSurface, dstProxy->origin(), left, top, width, height,
778 srcColorType, buffer, rowBytes);
bsalomon81beccc2014-10-13 12:32:55 -0700779 }
bsalomon81beccc2014-10-13 12:32:55 -0700780 return true;
781}
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000782
Brian Salomonc320b152018-02-20 14:05:36 -0500783bool GrContextPriv::readSurfacePixels(GrSurfaceContext* src, int left, int top, int width,
784 int height, GrColorType dstColorType,
785 SkColorSpace* dstColorSpace, void* buffer, size_t rowBytes,
786 uint32_t flags) {
Brian Osmanb62ea222016-12-22 11:12:16 -0500787 // TODO: Color space conversion
788
Robert Phillipse78b7252017-04-06 07:59:41 -0400789 ASSERT_SINGLE_OWNER_PRIV
790 RETURN_FALSE_IF_ABANDONED_PRIV
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400791 SkASSERT(src);
792 ASSERT_OWNED_PROXY_PRIV(src->asSurfaceProxy());
Brian Salomondcbb9d92017-07-19 10:53:20 -0400793 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "readSurfacePixels", fContext);
bsalomon32ab2602015-09-09 18:57:49 -0700794
Robert Phillipse78b7252017-04-06 07:59:41 -0400795 // MDB TODO: delay this instantiation until later in the method
Robert Phillips6be756b2018-01-16 15:07:54 -0500796 if (!src->asSurfaceProxy()->instantiate(this->resourceProvider())) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400797 return false;
798 }
799
Robert Phillips16d8ec62017-07-27 16:16:25 -0400800 GrSurfaceProxy* srcProxy = src->asSurfaceProxy();
801 GrSurface* srcSurface = srcProxy->priv().peekSurface();
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400802
Brian Osmand2ca59a2017-04-13 14:03:57 -0400803 // The src is premul but the dst is unpremul -> unpremul the src after or as part of the read
804 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
Brian Salomonc320b152018-02-20 14:05:36 -0500805
806 if (!valid_pixel_conversion(dstColorType, srcProxy->config(), unpremul)) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400807 return false;
808 }
809
Brian Osman409e74f2017-04-17 11:48:28 -0400810 // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data,
811 // without any color spaces attached, and the caller wants us to unpremul.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400812 bool useConfigConversionEffect =
Brian Salomonf3569f02017-10-24 12:52:33 -0400813 unpremul &&
814 pm_upm_must_round_trip(srcProxy->config(), src->colorSpaceInfo().colorSpace()) &&
Brian Salomonc320b152018-02-20 14:05:36 -0500815 pm_upm_must_round_trip(dstColorType, dstColorSpace);
Brian Osman409e74f2017-04-17 11:48:28 -0400816
817 // Are we going to try to unpremul as part of a draw? For the non-legacy case, we always allow
818 // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly.
819 bool unpremulOnGpu = unpremul &&
820 (!useConfigConversionEffect || fContext->validPMUPMConversionExists());
bsalomon6c6f6582015-09-10 08:12:46 -0700821
bsalomone8d21e82015-07-16 08:23:13 -0700822 // Adjust the params so that if we wind up using an intermediate surface we've already done
823 // all the trimming and the temporary can be the min size required.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400824 if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(),
Brian Salomonc320b152018-02-20 14:05:36 -0500825 GrColorTypeBytesPerPixel(dstColorType), &left, &top,
826 &width, &height, &buffer, &rowBytes)) {
bsalomone8d21e82015-07-16 08:23:13 -0700827 return false;
828 }
829
Brian Osman409e74f2017-04-17 11:48:28 -0400830 GrGpu::DrawPreference drawPreference = unpremulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference
831 : GrGpu::kNoDraw_DrawPreference;
bsalomon39826022015-07-23 08:07:21 -0700832 GrGpu::ReadPixelTempDrawInfo tempDrawInfo;
Brian Salomon9b009bb2018-02-14 13:53:55 -0500833 GrSRGBConversion srgbConversion = determine_read_pixels_srgb_conversion(
834 GrPixelConfigIsSRGBEncoded(srcProxy->config()), src->colorSpaceInfo().colorSpace(),
Brian Salomonc320b152018-02-20 14:05:36 -0500835 dstColorType, dstColorSpace, *fContext->caps());
836
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400837 if (!fContext->fGpu->getReadPixelsInfo(srcSurface, srcProxy->origin(), width, height, rowBytes,
Brian Salomonc320b152018-02-20 14:05:36 -0500838 dstColorType, srgbConversion, &drawPreference,
Brian Salomon9b009bb2018-02-14 13:53:55 -0500839 &tempDrawInfo)) {
bsalomon39826022015-07-23 08:07:21 -0700840 return false;
841 }
bsalomon191bcc02014-11-14 11:31:13 -0800842
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400843 if (!(kDontFlush_PixelOpsFlag & flags) && srcSurface->surfacePriv().hasPendingWrite()) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400844 this->flush(nullptr); // MDB TODO: tighten this
845 }
846
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400847 sk_sp<GrSurfaceProxy> proxyToRead = src->asSurfaceProxyRef();
bsalomon39826022015-07-23 08:07:21 -0700848 bool didTempDraw = false;
849 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomonb117ff12016-07-19 07:24:40 -0700850 if (SkBackingFit::kExact == tempDrawInfo.fTempSurfaceFit) {
bsalomon39826022015-07-23 08:07:21 -0700851 // We only respect this when the entire src is being read. Otherwise we can trigger too
852 // many odd ball texture sizes and trash the cache.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400853 if (width != srcSurface->width() || height != srcSurface->height()) {
bsalomonb117ff12016-07-19 07:24:40 -0700854 tempDrawInfo.fTempSurfaceFit= SkBackingFit::kApprox;
bsalomon39826022015-07-23 08:07:21 -0700855 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000856 }
brianosmandfe4f2e2016-07-21 13:28:36 -0700857 // TODO: Need to decide the semantics of this function for color spaces. Do we support
858 // conversion to a passed-in color space? For now, specifying nullptr means that this
Brian Salomon366093f2018-02-13 09:25:22 -0500859 // path will do no conversion, so it will match the behavior of the non-draw path. For
860 // now we simply infer an sRGB color space if the config is sRGB in order to avoid an
861 // illegal combination.
862 sk_sp<SkColorSpace> colorSpace;
863 if (GrPixelConfigIsSRGB(tempDrawInfo.fTempSurfaceDesc.fConfig)) {
864 colorSpace = SkColorSpace::MakeSRGB();
865 }
866 sk_sp<GrRenderTargetContext> tempRTC =
867 fContext->makeDeferredRenderTargetContext(tempDrawInfo.fTempSurfaceFit,
868 tempDrawInfo.fTempSurfaceDesc.fWidth,
869 tempDrawInfo.fTempSurfaceDesc.fHeight,
870 tempDrawInfo.fTempSurfaceDesc.fConfig,
871 std::move(colorSpace),
872 tempDrawInfo.fTempSurfaceDesc.fSampleCnt,
873 GrMipMapped::kNo,
874 tempDrawInfo.fTempSurfaceDesc.fOrigin);
Brian Osman693a5402016-10-27 15:13:22 -0400875 if (tempRTC) {
Greg Danielf44cb482018-02-27 14:26:32 -0500876 // Adding discard to appease vulkan validation warning about loading uninitialized data
877 // on draw
878 tempRTC->discard();
Robert Phillips67c18d62017-01-20 12:44:06 -0500879 SkMatrix textureMatrix = SkMatrix::MakeTrans(SkIntToScalar(left), SkIntToScalar(top));
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400880 sk_sp<GrTextureProxy> proxy = src->asTextureProxyRef();
Brian Osman2240be92017-10-18 13:15:13 -0400881 auto fp = GrSimpleTextureEffect::Make(std::move(proxy), textureMatrix);
Brian Osman409e74f2017-04-17 11:48:28 -0400882 if (unpremulOnGpu) {
883 fp = fContext->createPMToUPMEffect(std::move(fp), useConfigConversionEffect);
884 // We no longer need to do this on CPU after the read back.
885 unpremul = false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000886 }
Brian Osman409e74f2017-04-17 11:48:28 -0400887 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400888 if (!fp) {
889 return false;
890 }
Brian Osman60cd57e2017-04-06 10:19:06 -0400891
Brian Osman409e74f2017-04-17 11:48:28 -0400892 GrPaint paint;
893 paint.addColorFragmentProcessor(std::move(fp));
894 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
895 paint.setAllowSRGBInputs(true);
896 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
897 tempRTC->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect,
898 nullptr);
899 proxyToRead = tempRTC->asTextureProxyRef();
900 left = 0;
901 top = 0;
902 didTempDraw = true;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000903 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000904 }
joshualitt5c55fef2014-10-31 14:04:35 -0700905
Robert Phillipse78b7252017-04-06 07:59:41 -0400906 if (!proxyToRead) {
907 return false;
908 }
909
bsalomon39826022015-07-23 08:07:21 -0700910 if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000911 return false;
912 }
Brian Salomonc320b152018-02-20 14:05:36 -0500913 GrColorType colorTypeToRead = dstColorType;
bsalomon39826022015-07-23 08:07:21 -0700914 if (didTempDraw) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400915 this->flushSurfaceWrites(proxyToRead.get());
Brian Salomonc320b152018-02-20 14:05:36 -0500916 colorTypeToRead = tempDrawInfo.fReadColorType;
bsalomon39826022015-07-23 08:07:21 -0700917 }
Robert Phillips09dfc472017-09-13 15:25:47 -0400918
Robert Phillips6be756b2018-01-16 15:07:54 -0500919 if (!proxyToRead->instantiate(this->resourceProvider())) {
Robert Phillips09dfc472017-09-13 15:25:47 -0400920 return false;
921 }
922
923 GrSurface* surfaceToRead = proxyToRead->priv().peekSurface();
924
Brian Salomonc320b152018-02-20 14:05:36 -0500925 if (!fContext->fGpu->readPixels(surfaceToRead, proxyToRead->origin(), left, top, width, height,
926 colorTypeToRead, buffer, rowBytes)) {
bsalomon39826022015-07-23 08:07:21 -0700927 return false;
928 }
929
930 // Perform umpremul conversion if we weren't able to perform it as a draw.
931 if (unpremul) {
Brian Salomonc320b152018-02-20 14:05:36 -0500932 SkColorType colorType = GrColorTypeToSkColorType(dstColorType);
933 if (kUnknown_SkColorType == colorType || 4 != SkColorTypeBytesPerPixel(colorType)) {
reed@google.com7111d462014-03-25 16:20:24 +0000934 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000935 }
reed@google.com7111d462014-03-25 16:20:24 +0000936
Matt Sarettc7b29082017-02-09 16:22:39 -0500937 for (int y = 0; y < height; y++) {
938 SkUnpremultiplyRow<false>((uint32_t*) buffer, (const uint32_t*) buffer, width);
939 buffer = SkTAddOffset<void>(buffer, rowBytes);
940 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000941 }
942 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000943}
944
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500945bool GrContextPriv::writeSurfacePixels2(GrSurfaceContext* dst, int left, int top, int width,
946 int height, GrColorType srcColorType,
947 SkColorSpace* srcColorSpace, const void* buffer,
948 size_t rowBytes, uint32_t pixelOpsFlags) {
949 ASSERT_SINGLE_OWNER_PRIV
950 RETURN_FALSE_IF_ABANDONED_PRIV
951 SkASSERT(dst);
952 SkASSERT(buffer);
953 ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy());
954 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "writeSurfacePixels2", fContext);
955
956 if (GrColorType::kUnknown == srcColorType) {
957 return false;
958 }
959
960 if (!dst->asSurfaceProxy()->instantiate(this->resourceProvider())) {
961 return false;
962 }
963
964 GrSurfaceProxy* dstProxy = dst->asSurfaceProxy();
965 GrSurface* dstSurface = dstProxy->priv().peekSurface();
966
967 if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(),
968 GrColorTypeBytesPerPixel(srcColorType), &left, &top,
969 &width, &height, &buffer, &rowBytes)) {
970 return false;
971 }
972
Brian Salomon3d86a192018-02-27 16:46:11 -0500973 if (!fContext->caps()->surfaceSupportsWritePixels(dstSurface)) {
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500974 GrSurfaceDesc desc;
975 desc.fConfig = dstProxy->config();
976 desc.fWidth = width;
977 desc.fHeight = height;
978 desc.fSampleCnt = 1;
979 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
980 auto tempProxy =
981 this->proxyProvider()->createProxy(desc, SkBackingFit::kApprox, SkBudgeted::kYes);
982 if (!tempProxy) {
983 return false;
984 }
985 auto tempCtx = this->drawingManager()->makeTextureContext(
986 tempProxy, dst->colorSpaceInfo().refColorSpace());
987 if (!tempCtx) {
988 return false;
989 }
990 if (!this->writeSurfacePixels2(tempCtx.get(), 0, 0, width, height, srcColorType,
991 srcColorSpace, buffer, rowBytes, pixelOpsFlags)) {
992 return false;
993 }
Brian Salomon3d86a192018-02-27 16:46:11 -0500994 return dst->copy(tempProxy.get(), SkIRect::MakeWH(width, height), {left, top});
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500995 }
996
997 // TODO: Make GrSurfaceContext know its alpha type and pass src buffer's alpha type.
998 bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
999 bool convert = premul;
1000
1001 if (!valid_pixel_conversion(srcColorType, dstProxy->config(), premul)) {
1002 return false;
1003 }
1004
1005 GrColorType allowedColorType =
1006 fContext->caps()->supportedWritePixelsColorType(dstProxy->config(), srcColorType);
1007 convert = convert || (srcColorType != allowedColorType);
1008
1009 if (!dst->colorSpaceInfo().colorSpace()) {
1010 // "Legacy" mode - no color space conversions.
1011 srcColorSpace = nullptr;
1012 }
1013 convert = convert || !SkColorSpace::Equals(srcColorSpace, dst->colorSpaceInfo().colorSpace());
1014
1015 std::unique_ptr<char[]> tempBuffer;
1016 if (convert) {
1017 auto srcSkColorType = GrColorTypeToSkColorType(srcColorType);
1018 auto dstSkColorType = GrColorTypeToSkColorType(allowedColorType);
1019 if (kUnknown_SkColorType == srcSkColorType || kUnknown_SkColorType == dstSkColorType) {
1020 return false;
1021 }
1022 auto srcAlphaType = premul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType;
1023 SkPixmap src(SkImageInfo::Make(width, height, srcSkColorType, srcAlphaType,
1024 sk_ref_sp(srcColorSpace)),
1025 buffer, rowBytes);
1026 auto tempSrcII = SkImageInfo::Make(width, height, dstSkColorType, kPremul_SkAlphaType,
1027 dst->colorSpaceInfo().refColorSpace());
1028 auto size = tempSrcII.computeMinByteSize();
1029 if (!size) {
1030 return false;
1031 }
1032 tempBuffer.reset(new char[size]);
1033 SkPixmap tempSrc(tempSrcII, tempBuffer.get(), tempSrcII.minRowBytes());
1034 if (!src.readPixels(tempSrc)) {
1035 return false;
1036 }
1037 srcColorType = allowedColorType;
1038 buffer = tempSrc.addr();
1039 rowBytes = tempSrc.rowBytes();
1040 if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
1041 std::unique_ptr<char[]> row(new char[rowBytes]);
1042 for (int y = 0; y < height / 2; ++y) {
1043 memcpy(row.get(), tempSrc.addr(0, y), rowBytes);
1044 memcpy(tempSrc.writable_addr(0, y), tempSrc.addr(0, height - 1 - y), rowBytes);
1045 memcpy(tempSrc.writable_addr(0, height - 1 - y), row.get(), rowBytes);
1046 }
1047 top = dstProxy->height() - top - height;
1048 }
1049 } else if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
1050 size_t trimRowBytes = GrColorTypeBytesPerPixel(srcColorType) * width;
1051 tempBuffer.reset(new char[trimRowBytes * height]);
1052 char* dst = reinterpret_cast<char*>(tempBuffer.get()) + trimRowBytes * (height - 1);
1053 const char* src = reinterpret_cast<const char*>(buffer);
1054 for (int i = 0; i < height; ++i, src += rowBytes, dst -= trimRowBytes) {
1055 memcpy(dst, src, trimRowBytes);
1056 }
1057 buffer = tempBuffer.get();
1058 rowBytes = trimRowBytes;
1059 top = dstProxy->height() - top - height;
1060 }
1061
1062 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && dstSurface->surfacePriv().hasPendingIO()) {
1063 this->flush(nullptr); // MDB TODO: tighten this
1064 }
1065
1066 return this->getGpu()->writePixels(dstSurface, left, top, width, height, srcColorType, buffer,
1067 rowBytes);
1068}
1069
Robert Phillips7ee385e2017-03-30 08:02:11 -04001070void GrContextPriv::prepareSurfaceForExternalIO(GrSurfaceProxy* proxy) {
1071 ASSERT_SINGLE_OWNER_PRIV
1072 RETURN_IF_ABANDONED_PRIV
1073 SkASSERT(proxy);
1074 ASSERT_OWNED_PROXY_PRIV(proxy);
Greg Daniel51316782017-08-02 15:10:09 +00001075 fContext->fDrawingManager->prepareSurfaceForExternalIO(proxy, 0, nullptr);
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001076}
1077
Robert Phillips7ee385e2017-03-30 08:02:11 -04001078void GrContextPriv::flushSurfaceWrites(GrSurfaceProxy* proxy) {
1079 ASSERT_SINGLE_OWNER_PRIV
1080 RETURN_IF_ABANDONED_PRIV
1081 SkASSERT(proxy);
1082 ASSERT_OWNED_PROXY_PRIV(proxy);
1083 if (proxy->priv().hasPendingWrite()) {
1084 this->flush(proxy);
bsalomonf80bfed2014-10-07 05:56:02 -07001085 }
1086}
1087
Robert Phillips7ee385e2017-03-30 08:02:11 -04001088void GrContextPriv::flushSurfaceIO(GrSurfaceProxy* proxy) {
1089 ASSERT_SINGLE_OWNER_PRIV
1090 RETURN_IF_ABANDONED_PRIV
1091 SkASSERT(proxy);
1092 ASSERT_OWNED_PROXY_PRIV(proxy);
1093 if (proxy->priv().hasPendingIO()) {
1094 this->flush(proxy);
ajuma95243eb2016-08-24 08:19:02 -07001095 }
1096}
1097
bsalomon@google.com27847de2011-02-22 20:59:41 +00001098////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +00001099
Robert Phillips2c862492017-01-18 10:08:39 -05001100sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy> proxy,
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -05001101 sk_sp<SkColorSpace> colorSpace,
1102 const SkSurfaceProps* props) {
Brian Osman45580d32016-11-23 09:37:01 -05001103 ASSERT_SINGLE_OWNER_PRIV
1104
Brian Salomon366093f2018-02-13 09:25:22 -05001105 // sRGB pixel configs may only be used with near-sRGB gamma color spaces.
1106 if (GrPixelConfigIsSRGB(proxy->config())) {
1107 if (!colorSpace || !colorSpace->gammaCloseToSRGB()) {
1108 return nullptr;
1109 }
1110 }
Brian Osman45580d32016-11-23 09:37:01 -05001111 if (proxy->asRenderTargetProxy()) {
Robert Phillips2c862492017-01-18 10:08:39 -05001112 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -05001113 std::move(colorSpace), props);
Brian Osman45580d32016-11-23 09:37:01 -05001114 } else {
1115 SkASSERT(proxy->asTextureProxy());
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -05001116 SkASSERT(!props);
Robert Phillips2c862492017-01-18 10:08:39 -05001117 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Brian Osman45580d32016-11-23 09:37:01 -05001118 }
1119}
1120
Robert Phillipse2f7d182016-12-15 09:23:05 -05001121sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(const GrSurfaceDesc& dstDesc,
Greg Daniel65c7f662017-10-30 13:39:09 -04001122 GrMipMapped mipMapped,
Robert Phillipse2f7d182016-12-15 09:23:05 -05001123 SkBackingFit fit,
Brian Salomon366093f2018-02-13 09:25:22 -05001124 SkBudgeted isDstBudgeted,
1125 sk_sp<SkColorSpace> colorSpace,
1126 const SkSurfaceProps* props) {
Greg Daniel65c7f662017-10-30 13:39:09 -04001127 sk_sp<GrTextureProxy> proxy;
1128 if (GrMipMapped::kNo == mipMapped) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001129 proxy = this->proxyProvider()->createProxy(dstDesc, fit, isDstBudgeted);
Greg Daniel65c7f662017-10-30 13:39:09 -04001130 } else {
1131 SkASSERT(SkBackingFit::kExact == fit);
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001132 proxy = this->proxyProvider()->createMipMapProxy(dstDesc, isDstBudgeted);
Greg Daniel65c7f662017-10-30 13:39:09 -04001133 }
Robert Phillips77b3f322017-01-31 18:24:12 -05001134 if (!proxy) {
1135 return nullptr;
1136 }
Robert Phillipse2f7d182016-12-15 09:23:05 -05001137
Brian Salomon366093f2018-02-13 09:25:22 -05001138 return this->makeWrappedSurfaceContext(std::move(proxy), std::move(colorSpace), props);
Robert Phillipse2f7d182016-12-15 09:23:05 -05001139}
1140
Brian Salomond17f6582017-07-19 18:28:58 -04001141sk_sp<GrTextureContext> GrContextPriv::makeBackendTextureContext(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +00001142 GrSurfaceOrigin origin,
Brian Osmanc1e37052017-03-09 14:19:20 -05001143 sk_sp<SkColorSpace> colorSpace) {
Robert Phillips26caf892017-01-27 10:58:31 -05001144 ASSERT_SINGLE_OWNER_PRIV
1145
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001146 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->createWrappedTextureProxy(tex, origin);
Robert Phillips77b3f322017-01-31 18:24:12 -05001147 if (!proxy) {
1148 return nullptr;
1149 }
Robert Phillips26caf892017-01-27 10:58:31 -05001150
Brian Salomond17f6582017-07-19 18:28:58 -04001151 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Robert Phillips26caf892017-01-27 10:58:31 -05001152}
1153
Brian Osman11052242016-10-27 14:47:55 -04001154sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +00001155 const GrBackendTexture& tex,
1156 GrSurfaceOrigin origin,
1157 int sampleCnt,
Brian Osman11052242016-10-27 14:47:55 -04001158 sk_sp<SkColorSpace> colorSpace,
Brian Osmanc1e37052017-03-09 14:19:20 -05001159 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -07001160 ASSERT_SINGLE_OWNER_PRIV
Brian Salomonbdecacf2018-02-02 20:32:49 -05001161 SkASSERT(sampleCnt > 0);
robertphillips4fd74ae2016-08-03 14:26:53 -07001162
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001163 sk_sp<GrTextureProxy> proxy(this->proxyProvider()->createWrappedTextureProxy(tex, origin,
1164 sampleCnt));
Robert Phillips77b3f322017-01-31 18:24:12 -05001165 if (!proxy) {
1166 return nullptr;
1167 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001168
Robert Phillips37430132016-11-09 06:50:43 -05001169 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Brian Osman11052242016-10-27 14:47:55 -04001170 std::move(colorSpace), props);
robertphillips4fd74ae2016-08-03 14:26:53 -07001171}
1172
Brian Osman11052242016-10-27 14:47:55 -04001173sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext(
Greg Danielbcf612b2017-05-01 13:50:58 +00001174 const GrBackendRenderTarget& backendRT,
1175 GrSurfaceOrigin origin,
robertphillips4fd74ae2016-08-03 14:26:53 -07001176 sk_sp<SkColorSpace> colorSpace,
1177 const SkSurfaceProps* surfaceProps) {
1178 ASSERT_SINGLE_OWNER_PRIV
1179
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001180 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->createWrappedRenderTargetProxy(backendRT,
1181 origin);
Robert Phillips77b3f322017-01-31 18:24:12 -05001182 if (!proxy) {
1183 return nullptr;
1184 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001185
Robert Phillips37430132016-11-09 06:50:43 -05001186 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001187 std::move(colorSpace),
Brian Osman11052242016-10-27 14:47:55 -04001188 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -07001189}
1190
Brian Osman11052242016-10-27 14:47:55 -04001191sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +00001192 const GrBackendTexture& tex,
1193 GrSurfaceOrigin origin,
1194 int sampleCnt,
robertphillips4fd74ae2016-08-03 14:26:53 -07001195 sk_sp<SkColorSpace> colorSpace,
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001196 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -07001197 ASSERT_SINGLE_OWNER_PRIV
Brian Salomonbdecacf2018-02-02 20:32:49 -05001198 SkASSERT(sampleCnt > 0);
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001199 sk_sp<GrSurfaceProxy> proxy(this->proxyProvider()->createWrappedRenderTargetProxy(tex, origin,
1200 sampleCnt));
Robert Phillips77b3f322017-01-31 18:24:12 -05001201 if (!proxy) {
1202 return nullptr;
1203 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001204
Robert Phillips37430132016-11-09 06:50:43 -05001205 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001206 std::move(colorSpace),
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001207 props);
robertphillips77a2e522015-10-17 07:43:27 -07001208}
1209
Chris Daltonfe199b72017-05-05 11:26:15 -04001210void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
1211 fContext->fDrawingManager->addOnFlushCallbackObject(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -04001212}
1213
Robert Phillips62000362018-02-01 09:10:04 -05001214void GrContextPriv::moveOpListsToDDL(SkDeferredDisplayList* ddl) {
1215 fContext->fDrawingManager->moveOpListsToDDL(ddl);
1216}
1217
1218void GrContextPriv::copyOpListsFromDDL(const SkDeferredDisplayList* ddl,
1219 GrRenderTargetProxy* newDest) {
1220 fContext->fDrawingManager->copyOpListsFromDDL(ddl, newDest);
1221}
1222
robertphillips48fde9c2016-09-06 05:20:20 -07001223static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) {
Brian Osman78f20e02017-01-12 10:28:01 -05001224 switch (config) {
1225 case kAlpha_8_GrPixelConfig:
1226 case kRGB_565_GrPixelConfig:
1227 case kRGBA_4444_GrPixelConfig:
1228 case kBGRA_8888_GrPixelConfig:
Brian Osman44b61202018-03-01 10:49:26 -05001229 case kRGBA_1010102_GrPixelConfig:
Brian Osman78f20e02017-01-12 10:28:01 -05001230 return kRGBA_8888_GrPixelConfig;
1231 case kSBGRA_8888_GrPixelConfig:
1232 return kSRGBA_8888_GrPixelConfig;
1233 case kAlpha_half_GrPixelConfig:
1234 return kRGBA_half_GrPixelConfig;
1235 default:
1236 return kUnknown_GrPixelConfig;
1237 }
robertphillips48fde9c2016-09-06 05:20:20 -07001238}
1239
robertphillipsd728f0c2016-11-21 11:05:03 -08001240sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContextWithFallback(
1241 SkBackingFit fit,
1242 int width, int height,
1243 GrPixelConfig config,
1244 sk_sp<SkColorSpace> colorSpace,
1245 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -04001246 GrMipMapped mipMapped,
robertphillipsd728f0c2016-11-21 11:05:03 -08001247 GrSurfaceOrigin origin,
1248 const SkSurfaceProps* surfaceProps,
1249 SkBudgeted budgeted) {
Brian Salomonbdecacf2018-02-02 20:32:49 -05001250 SkASSERT(sampleCnt > 0);
1251 if (0 == this->caps()->getRenderTargetSampleCount(sampleCnt, config)) {
robertphillipsd728f0c2016-11-21 11:05:03 -08001252 config = GrPixelConfigFallback(config);
1253 }
1254
1255 return this->makeDeferredRenderTargetContext(fit, width, height, config, std::move(colorSpace),
Greg Daniel45d63032017-10-30 13:41:26 -04001256 sampleCnt, mipMapped, origin, surfaceProps,
Greg Daniele1da1d92017-10-06 15:59:27 -04001257 budgeted);
robertphillipsd728f0c2016-11-21 11:05:03 -08001258}
1259
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001260sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContext(
1261 SkBackingFit fit,
1262 int width, int height,
1263 GrPixelConfig config,
1264 sk_sp<SkColorSpace> colorSpace,
1265 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -04001266 GrMipMapped mipMapped,
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001267 GrSurfaceOrigin origin,
1268 const SkSurfaceProps* surfaceProps,
1269 SkBudgeted budgeted) {
Brian Salomonbdecacf2018-02-02 20:32:49 -05001270 SkASSERT(sampleCnt > 0);
Brian Salomon79e4d1b2017-07-13 11:17:11 -04001271 if (this->abandoned()) {
1272 return nullptr;
1273 }
1274
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001275 GrSurfaceDesc desc;
1276 desc.fFlags = kRenderTarget_GrSurfaceFlag;
1277 desc.fOrigin = origin;
1278 desc.fWidth = width;
1279 desc.fHeight = height;
1280 desc.fConfig = config;
1281 desc.fSampleCnt = sampleCnt;
1282
Greg Daniele1da1d92017-10-06 15:59:27 -04001283 sk_sp<GrTextureProxy> rtp;
Greg Daniel45d63032017-10-30 13:41:26 -04001284 if (GrMipMapped::kNo == mipMapped) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001285 rtp = fProxyProvider->createProxy(desc, fit, budgeted);
Greg Daniele1da1d92017-10-06 15:59:27 -04001286 } else {
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001287 rtp = fProxyProvider->createMipMapProxy(desc, budgeted);
Greg Daniele1da1d92017-10-06 15:59:27 -04001288 }
Robert Phillips08c5ec72017-01-30 12:26:47 -05001289 if (!rtp) {
1290 return nullptr;
1291 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001292
Robert Phillips1119dc32017-04-11 12:54:57 -04001293 sk_sp<GrRenderTargetContext> renderTargetContext(
1294 fDrawingManager->makeRenderTargetContext(std::move(rtp),
1295 std::move(colorSpace),
1296 surfaceProps));
Robert Phillips1119dc32017-04-11 12:54:57 -04001297 if (!renderTargetContext) {
1298 return nullptr;
1299 }
1300
1301 renderTargetContext->discard();
1302
1303 return renderTargetContext;
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001304}
1305
joshualitt1de610a2016-01-06 08:26:09 -08001306bool GrContext::abandoned() const {
1307 ASSERT_SINGLE_OWNER
robertphillips7761d612016-05-16 09:14:53 -07001308 return fDrawingManager->wasAbandoned();
robertphillips77a2e522015-10-17 07:43:27 -07001309}
1310
Brian Salomonaff329b2017-08-11 09:40:37 -04001311std::unique_ptr<GrFragmentProcessor> GrContext::createPMToUPMEffect(
1312 std::unique_ptr<GrFragmentProcessor> fp, bool useConfigConversionEffect) {
Robert Phillips757914d2017-01-25 15:48:30 -05001313 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -04001314 // We have specialized effects that guarantee round-trip conversion for some formats
1315 if (useConfigConversionEffect) {
1316 // We should have already called this->validPMUPMConversionExists() in this case
1317 SkASSERT(fDidTestPMConversions);
1318 // ...and it should have succeeded
1319 SkASSERT(this->validPMUPMConversionExists());
1320
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001321 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul);
Brian Osman2d2da4f2017-04-12 17:07:22 -04001322 } else {
1323 // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and
1324 // explicitly round the results. Just do the obvious, naive thing in the shader.
1325 return GrFragmentProcessor::UnpremulOutput(std::move(fp));
Robert Phillips757914d2017-01-25 15:48:30 -05001326 }
1327}
1328
Brian Salomonaff329b2017-08-11 09:40:37 -04001329std::unique_ptr<GrFragmentProcessor> GrContext::createUPMToPMEffect(
1330 std::unique_ptr<GrFragmentProcessor> fp, bool useConfigConversionEffect) {
joshualitt1de610a2016-01-06 08:26:09 -08001331 ASSERT_SINGLE_OWNER
Brian Osman2d2da4f2017-04-12 17:07:22 -04001332 // We have specialized effects that guarantee round-trip conversion for these formats
Brian Osman409e74f2017-04-17 11:48:28 -04001333 if (useConfigConversionEffect) {
1334 // We should have already called this->validPMUPMConversionExists() in this case
1335 SkASSERT(fDidTestPMConversions);
1336 // ...and it should have succeeded
1337 SkASSERT(this->validPMUPMConversionExists());
1338
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001339 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
Brian Osman2d2da4f2017-04-12 17:07:22 -04001340 } else {
1341 // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and
1342 // explicitly round the results. Just do the obvious, naive thing in the shader.
1343 return GrFragmentProcessor::PremulOutput(std::move(fp));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001344 }
1345}
1346
Brian Osman409e74f2017-04-17 11:48:28 -04001347bool GrContext::validPMUPMConversionExists() {
joshualitt1de610a2016-01-06 08:26:09 -08001348 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -04001349 if (!fDidTestPMConversions) {
Brian Osman28804f32017-04-20 10:24:36 -04001350 fPMUPMConversionsRoundTrip = GrConfigConversionEffect::TestForPreservingPMConversions(this);
Brian Osman409e74f2017-04-17 11:48:28 -04001351 fDidTestPMConversions = true;
1352 }
1353
bsalomon636e8022015-07-29 06:08:46 -07001354 // The PM<->UPM tests fail or succeed together so we only need to check one.
Brian Osman28804f32017-04-20 10:24:36 -04001355 return fPMUPMConversionsRoundTrip;
bsalomon636e8022015-07-29 06:08:46 -07001356}
1357
bsalomon37f9a262015-02-02 13:00:10 -08001358//////////////////////////////////////////////////////////////////////////////
1359
Robert Phillipsfc711a22018-02-13 17:03:00 -05001360// DDL TODO: remove 'maxResources'
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001361void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -08001362 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001363 if (maxResources) {
1364 *maxResources = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -08001365 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001366 if (maxResourceBytes) {
1367 *maxResourceBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -08001368 }
1369}
1370
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001371void GrContext::setResourceCacheLimits(int maxResources, size_t maxResourceBytes) {
joshualitt1de610a2016-01-06 08:26:09 -08001372 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001373 fResourceCache->setLimits(maxResources, maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -08001374}
1375
ericrk0a5fa482015-09-15 14:16:10 -07001376//////////////////////////////////////////////////////////////////////////////
1377
1378void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -08001379 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -07001380 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
1381}
Brian Osman71a18892017-08-10 10:23:25 -04001382
1383//////////////////////////////////////////////////////////////////////////////
1384
1385SkString GrContext::dump() const {
1386 SkDynamicMemoryWStream stream;
1387 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
1388 writer.beginObject();
1389
1390 static const char* kBackendStr[] = {
1391 "Metal",
1392 "OpenGL",
1393 "Vulkan",
1394 "Mock",
1395 };
1396 GR_STATIC_ASSERT(0 == kMetal_GrBackend);
1397 GR_STATIC_ASSERT(1 == kOpenGL_GrBackend);
1398 GR_STATIC_ASSERT(2 == kVulkan_GrBackend);
1399 GR_STATIC_ASSERT(3 == kMock_GrBackend);
1400 writer.appendString("backend", kBackendStr[fBackend]);
1401
1402 writer.appendName("caps");
1403 fCaps->dumpJSON(&writer);
1404
1405 writer.appendName("gpu");
1406 fGpu->dumpJSON(&writer);
1407
1408 // Flush JSON to the memory stream
1409 writer.endObject();
1410 writer.flush();
1411
1412 // Null terminate the JSON data in the memory stream
1413 stream.write8(0);
1414
1415 // Allocate a string big enough to hold all the data, then copy out of the stream
1416 SkString result(stream.bytesWritten());
1417 stream.copyToAndReset(result.writable_str());
1418 return result;
1419}