blob: d3194be8f29d6d3935e70340b6765e9d613a7670 [file] [log] [blame]
bsalomon@google.com27847de2011-02-22 20:59:41 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
bsalomon@google.com27847de2011-02-22 20:59:41 +00006 */
7
Greg Daniel51316782017-08-02 15:10:09 +00008#include "GrBackendSemaphore.h"
bsalomon@google.com1fadb202011-12-12 16:10:08 +00009#include "GrContext.h"
Brian Salomonc65aec92017-03-09 09:03:58 -050010#include "GrClip.h"
bsalomon682c2692015-05-22 14:01:46 -070011#include "GrContextOptions.h"
Brian Salomonc65aec92017-03-09 09:03:58 -050012#include "GrContextPriv.h"
robertphillips77a2e522015-10-17 07:43:27 -070013#include "GrDrawingManager.h"
Robert Phillips646e4292017-06-13 12:44:56 -040014#include "GrGpu.h"
Brian Osman11052242016-10-27 14:47:55 -040015#include "GrRenderTargetContext.h"
Brian Salomonc65aec92017-03-09 09:03:58 -050016#include "GrRenderTargetProxy.h"
bsalomon0ea80f42015-02-11 10:49:59 -080017#include "GrResourceCache.h"
bsalomond309e7a2015-04-30 14:18:54 -070018#include "GrResourceProvider.h"
Greg Danield85f97d2017-03-07 13:37:21 -050019#include "GrSemaphore.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000020#include "GrSoftwarePathRenderer.h"
Brian Osman45580d32016-11-23 09:37:01 -050021#include "GrSurfaceContext.h"
bsalomonafbf2d62014-09-30 12:18:44 -070022#include "GrSurfacePriv.h"
Robert Phillips757914d2017-01-25 15:48:30 -050023#include "GrSurfaceProxyPriv.h"
Robert Phillips646e4292017-06-13 12:44:56 -040024#include "GrTexture.h"
Brian Osman45580d32016-11-23 09:37:01 -050025#include "GrTextureContext.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040026#include "GrTracing.h"
Matt Sarett485c4992017-02-14 14:18:27 -050027#include "SkConvertPixels.h"
Brian Osman3b655982017-03-07 16:58:08 -050028#include "SkGr.h"
Brian Osman71a18892017-08-10 10:23:25 -040029#include "SkJSONWriter.h"
Brian Osman51279982017-08-23 10:12:00 -040030#include "SkMakeUnique.h"
31#include "SkTaskGroup.h"
Matt Sarettc7b29082017-02-09 16:22:39 -050032#include "SkUnPreMultiplyPriv.h"
joshualitt5478d422014-11-14 16:00:38 -080033#include "effects/GrConfigConversionEffect.h"
joshualitte8042922015-12-11 06:11:21 -080034#include "text/GrTextBlobCache.h"
joshualitt5478d422014-11-14 16:00:38 -080035
Greg Daniel02611d92017-07-25 10:05:01 -040036#include "gl/GrGLGpu.h"
37#include "mock/GrMockGpu.h"
Greg Danielb76a72a2017-07-13 15:07:54 -040038#ifdef SK_METAL
39#include "mtl/GrMtlTrampoline.h"
40#endif
Greg Daniel02611d92017-07-25 10:05:01 -040041#ifdef SK_VULKAN
42#include "vk/GrVkGpu.h"
43#endif
Greg Danielb76a72a2017-07-13 15:07:54 -040044
Robert Phillipse78b7252017-04-06 07:59:41 -040045#define ASSERT_OWNED_PROXY(P) \
46SkASSERT(!(P) || !((P)->priv().peekTexture()) || (P)->priv().peekTexture()->getContext() == this)
Robert Phillips7ee385e2017-03-30 08:02:11 -040047#define ASSERT_OWNED_PROXY_PRIV(P) \
48SkASSERT(!(P) || !((P)->priv().peekTexture()) || (P)->priv().peekTexture()->getContext() == fContext)
49
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000050#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
joshualitt1de610a2016-01-06 08:26:09 -080051#define ASSERT_SINGLE_OWNER \
52 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fSingleOwner);)
robertphillips4fd74ae2016-08-03 14:26:53 -070053#define ASSERT_SINGLE_OWNER_PRIV \
54 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fContext->fSingleOwner);)
robertphillips7761d612016-05-16 09:14:53 -070055#define RETURN_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return; }
Robert Phillips7ee385e2017-03-30 08:02:11 -040056#define RETURN_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return; }
robertphillips7761d612016-05-16 09:14:53 -070057#define RETURN_FALSE_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return false; }
Robert Phillipse78b7252017-04-06 07:59:41 -040058#define RETURN_FALSE_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return false; }
robertphillips7761d612016-05-16 09:14:53 -070059#define RETURN_NULL_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return nullptr; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000060
robertphillipsea461502015-05-26 11:38:03 -070061////////////////////////////////////////////////////////////////////////////////
62
bsalomon682c2692015-05-22 14:01:46 -070063GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext) {
64 GrContextOptions defaultOptions;
65 return Create(backend, backendContext, defaultOptions);
66}
bsalomonf28cff72015-05-22 12:25:41 -070067
bsalomon682c2692015-05-22 14:01:46 -070068GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
69 const GrContextOptions& options) {
Brian Salomon91a3e522017-06-23 10:58:19 -040070 sk_sp<GrContext> context(new GrContext);
bsalomon682c2692015-05-22 14:01:46 -070071
Brian Salomon91a3e522017-06-23 10:58:19 -040072 if (!context->init(backend, backendContext, options)) {
halcanary96fcdcc2015-08-27 07:41:13 -070073 return nullptr;
bsalomon@google.com27847de2011-02-22 20:59:41 +000074 }
Brian Salomon91a3e522017-06-23 10:58:19 -040075 return context.release();
bsalomon@google.com27847de2011-02-22 20:59:41 +000076}
77
Greg Daniel02611d92017-07-25 10:05:01 -040078sk_sp<GrContext> GrContext::MakeGL(const GrGLInterface* interface) {
79 GrContextOptions defaultOptions;
80 return MakeGL(interface, defaultOptions);
81}
82
83sk_sp<GrContext> GrContext::MakeGL(const GrGLInterface* interface,
84 const GrContextOptions& options) {
85 sk_sp<GrContext> context(new GrContext);
86 context->fGpu = GrGLGpu::Create(interface, options, context.get());
87 if (!context->fGpu) {
88 return nullptr;
89 }
90 context->fBackend = kOpenGL_GrBackend;
91 if (!context->init(options)) {
92 return nullptr;
93 }
94 return context;
95}
96
97sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions) {
98 GrContextOptions defaultOptions;
99 return MakeMock(mockOptions, defaultOptions);
100}
101
102sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions,
103 const GrContextOptions& options) {
104 sk_sp<GrContext> context(new GrContext);
105 context->fGpu = GrMockGpu::Create(mockOptions, options, context.get());
106 if (!context->fGpu) {
107 return nullptr;
108 }
109 context->fBackend = kMock_GrBackend;
110 if (!context->init(options)) {
111 return nullptr;
112 }
113 return context;
114}
115
116#ifdef SK_VULKAN
117sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext* backendContext) {
118 GrContextOptions defaultOptions;
119 return MakeVulkan(backendContext, defaultOptions);
120}
121
122sk_sp<GrContext> GrContext::MakeVulkan(const GrVkBackendContext* backendContext,
123 const GrContextOptions& options) {
124 sk_sp<GrContext> context(new GrContext);
125 context->fGpu = GrVkGpu::Create(backendContext, options, context.get());
126 if (!context->fGpu) {
127 return nullptr;
128 }
129 context->fBackend = kVulkan_GrBackend;
130 if (!context->init(options)) {
131 return nullptr;
132 }
133 return context;
134}
135#endif
136
Greg Danielb76a72a2017-07-13 15:07:54 -0400137#ifdef SK_METAL
Greg Daniel02611d92017-07-25 10:05:01 -0400138sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue) {
139 GrContextOptions defaultOptions;
140 return MakeMetal(device, queue, defaultOptions);
141}
142
Greg Danielb76a72a2017-07-13 15:07:54 -0400143sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) {
144 sk_sp<GrContext> context(new GrContext);
145 context->fGpu = GrMtlTrampoline::CreateGpu(context.get(), options, device, queue);
146 if (!context->fGpu) {
147 return nullptr;
148 }
149 context->fBackend = kMetal_GrBackend;
150 if (!context->init(options)) {
151 return nullptr;
152 }
153 return context;
154}
155#endif
156
joshualitt0acd0d32015-05-07 08:23:19 -0700157static int32_t gNextID = 1;
158static int32_t next_id() {
159 int32_t id;
160 do {
161 id = sk_atomic_inc(&gNextID);
162 } while (id == SK_InvalidGenID);
163 return id;
164}
165
bsalomon682c2692015-05-22 14:01:46 -0700166GrContext::GrContext() : fUniqueID(next_id()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700167 fGpu = nullptr;
168 fCaps = nullptr;
169 fResourceCache = nullptr;
170 fResourceProvider = nullptr;
Brian Salomonf856fd12016-12-16 14:24:34 -0500171 fAtlasGlyphCache = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000172}
173
bsalomon682c2692015-05-22 14:01:46 -0700174bool GrContext::init(GrBackend backend, GrBackendContext backendContext,
175 const GrContextOptions& options) {
joshualitt1de610a2016-01-06 08:26:09 -0800176 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700177 SkASSERT(!fGpu);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000178
Greg Danielfc978fd2017-04-13 09:54:12 -0400179 fBackend = backend;
180
bsalomon682c2692015-05-22 14:01:46 -0700181 fGpu = GrGpu::Create(backend, backendContext, options, this);
robertphillipsea461502015-05-26 11:38:03 -0700182 if (!fGpu) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000183 return false;
184 }
Greg Danielb76a72a2017-07-13 15:07:54 -0400185 return this->init(options);
186}
joshualitt1de610a2016-01-06 08:26:09 -0800187
Greg Danielb76a72a2017-07-13 15:07:54 -0400188bool GrContext::init(const GrContextOptions& options) {
189 ASSERT_SINGLE_OWNER
bsalomon76228632015-05-29 08:02:10 -0700190 fCaps = SkRef(fGpu->caps());
Brian Osman13dddce2017-05-09 13:19:50 -0400191 fResourceCache = new GrResourceCache(fCaps, fUniqueID);
joshualitt6d0872d2016-01-11 08:27:48 -0800192 fResourceProvider = new GrResourceProvider(fGpu, fResourceCache, &fSingleOwner);
commit-bot@chromium.org1836d332013-07-16 22:55:03 +0000193
Brian Osman46da1cc2017-02-14 14:15:48 -0500194 fDisableGpuYUVConversion = options.fDisableGpuYUVConversion;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000195 fDidTestPMConversions = false;
196
bsalomon6b2552f2016-09-15 13:50:26 -0700197 GrPathRendererChain::Options prcOptions;
bsalomon39ef7fb2016-09-21 11:16:05 -0700198 prcOptions.fAllowPathMaskCaching = options.fAllowPathMaskCaching;
Brian Osman195c05b2017-08-30 15:14:04 -0400199#if GR_TEST_UTILS
csmartdalton008b9d82017-02-22 12:00:42 -0700200 prcOptions.fGpuPathRenderers = options.fGpuPathRenderers;
Brian Osman195c05b2017-08-30 15:14:04 -0400201#endif
Brian Osmanb350ae22017-08-29 16:15:39 -0400202 if (options.fDisableDistanceFieldPaths) {
Brian Osman195c05b2017-08-30 15:14:04 -0400203 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
Brian Osmanb350ae22017-08-29 16:15:39 -0400204 }
Robert Phillips3ea17982017-06-02 12:43:04 -0400205 fDrawingManager.reset(new GrDrawingManager(this, prcOptions, &fSingleOwner));
joshualitt7c3a2f82015-03-31 13:32:05 -0700206
Brian Salomon9f545bc2017-11-06 10:36:57 -0500207 GrDrawOpAtlas::AllowMultitexturing allowMultitexturing;
208 if (options.fAllowMultipleGlyphCacheTextures == GrContextOptions::Enable::kNo) {
209 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
210 } else {
211 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
212 }
213 fAtlasGlyphCache = new GrAtlasGlyphCache(this, options.fGlyphCacheTextureMaximumBytes,
214 allowMultitexturing);
Jim Van Verth106b5c42017-09-26 12:45:29 -0400215 this->contextPriv().addOnFlushCallbackObject(fAtlasGlyphCache);
joshualittb7133be2015-04-08 09:08:31 -0700216
halcanary385fe4d2015-08-26 13:07:48 -0700217 fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB, this));
Brian Salomon91a3e522017-06-23 10:58:19 -0400218
Brian Osman51279982017-08-23 10:12:00 -0400219 if (options.fExecutor) {
220 fTaskGroup = skstd::make_unique<SkTaskGroup>(*options.fExecutor);
221 }
222
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400223 fPersistentCache = options.fPersistentCache;
224
Brian Salomon91a3e522017-06-23 10:58:19 -0400225 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000226}
227
bsalomon@google.com27847de2011-02-22 20:59:41 +0000228GrContext::~GrContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800229 ASSERT_SINGLE_OWNER
230
robertphillipsea461502015-05-26 11:38:03 -0700231 if (!fGpu) {
bsalomon76228632015-05-29 08:02:10 -0700232 SkASSERT(!fCaps);
bsalomon@google.com733c0622013-04-24 17:59:32 +0000233 return;
234 }
235
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000236 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000237
robertphillips77a2e522015-10-17 07:43:27 -0700238 fDrawingManager->cleanup();
robertphillips2334fb62015-06-17 05:43:33 -0700239
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000240 for (int i = 0; i < fCleanUpData.count(); ++i) {
241 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
242 }
243
halcanary385fe4d2015-08-26 13:07:48 -0700244 delete fResourceProvider;
245 delete fResourceCache;
Brian Salomonf856fd12016-12-16 14:24:34 -0500246 delete fAtlasGlyphCache;
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000247
bsalomon@google.com205d4602011-04-25 12:43:45 +0000248 fGpu->unref();
bsalomon76228632015-05-29 08:02:10 -0700249 fCaps->unref();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000250}
251
bungeman6bd52842016-10-27 09:30:08 -0700252sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
bsalomon41b952c2016-03-11 06:46:33 -0800253 if (!fThreadSafeProxy) {
bungeman6bd52842016-10-27 09:30:08 -0700254 fThreadSafeProxy.reset(new GrContextThreadSafeProxy(sk_ref_sp(fCaps), this->uniqueID()));
bsalomon41b952c2016-03-11 06:46:33 -0800255 }
bungeman6bd52842016-10-27 09:30:08 -0700256 return fThreadSafeProxy;
bsalomon41b952c2016-03-11 06:46:33 -0800257}
258
bsalomon2354f842014-07-28 13:48:36 -0700259void GrContext::abandonContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800260 ASSERT_SINGLE_OWNER
261
bsalomond309e7a2015-04-30 14:18:54 -0700262 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800263
264 // Need to abandon the drawing manager first so all the render targets
265 // will be released/forgotten before they too are abandoned.
266 fDrawingManager->abandon();
267
bsalomon@google.com205d4602011-04-25 12:43:45 +0000268 // abandon first to so destructors
269 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800270 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700271
bsalomon6e2aad42016-04-01 11:54:31 -0700272 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
273
Brian Salomonf856fd12016-12-16 14:24:34 -0500274 fAtlasGlyphCache->freeAll();
bsalomon6e2aad42016-04-01 11:54:31 -0700275 fTextBlobCache->freeAll();
276}
277
278void GrContext::releaseResourcesAndAbandonContext() {
279 ASSERT_SINGLE_OWNER
280
281 fResourceProvider->abandon();
282
283 // Need to abandon the drawing manager first so all the render targets
284 // will be released/forgotten before they too are abandoned.
285 fDrawingManager->abandon();
286
287 // Release all resources in the backend 3D API.
288 fResourceCache->releaseAll();
289
290 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000291
Brian Salomonf856fd12016-12-16 14:24:34 -0500292 fAtlasGlyphCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700293 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000294}
295
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000296void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800297 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000298 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000299}
300
301void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800302 ASSERT_SINGLE_OWNER
303
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000304 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000305
Brian Salomonf856fd12016-12-16 14:24:34 -0500306 fAtlasGlyphCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700307
308 fDrawingManager->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700309
310 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000311}
312
Brian Salomon5e150852017-03-22 14:53:13 -0400313void GrContext::purgeResourcesNotUsedInMs(std::chrono::milliseconds ms) {
314 ASSERT_SINGLE_OWNER
315 fResourceCache->purgeResourcesNotUsedSince(GrStdSteadyClock::now() - ms);
316}
317
Derek Sollenberger5480a182017-05-25 16:43:59 -0400318void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
319 ASSERT_SINGLE_OWNER
320 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
321}
322
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000323void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800324 ASSERT_SINGLE_OWNER
325
bsalomon71cb0c22014-11-14 12:10:14 -0800326 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800327 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800328 }
329 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800330 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800331 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000332}
333
Derek Sollenbergeree479142017-05-24 11:41:33 -0400334size_t GrContext::getResourceCachePurgeableBytes() const {
335 ASSERT_SINGLE_OWNER
336 return fResourceCache->getPurgeableBytes();
337}
338
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000339////////////////////////////////////////////////////////////////////////////////
340
joshualitt0db6dfa2015-04-10 07:01:30 -0700341void GrContext::TextBlobCacheOverBudgetCB(void* data) {
342 SkASSERT(data);
Brian Osman11052242016-10-27 14:47:55 -0400343 // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on
344 // GrRenderTargetContext to perform a necessary flush. The solution is to move drawText calls
345 // to below the GrContext level, but this is not trivial because they call drawPath on
346 // SkGpuDevice.
joshualitt0db6dfa2015-04-10 07:01:30 -0700347 GrContext* context = reinterpret_cast<GrContext*>(data);
348 context->flush();
349}
350
bsalomon@google.com27847de2011-02-22 20:59:41 +0000351////////////////////////////////////////////////////////////////////////////////
352
bsalomonb77a9072016-09-07 10:02:04 -0700353void GrContext::flush() {
joshualitt1de610a2016-01-06 08:26:09 -0800354 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700355 RETURN_IF_ABANDONED
Robert Phillips7ee385e2017-03-30 08:02:11 -0400356
357 fDrawingManager->flush(nullptr);
358}
359
Greg Daniel51316782017-08-02 15:10:09 +0000360GrSemaphoresSubmitted GrContext::flushAndSignalSemaphores(int numSemaphores,
361 GrBackendSemaphore signalSemaphores[]) {
362 ASSERT_SINGLE_OWNER
363 if (fDrawingManager->wasAbandoned()) { return GrSemaphoresSubmitted::kNo; }
364
365 return fDrawingManager->flush(nullptr, numSemaphores, signalSemaphores);
366}
367
Robert Phillips7ee385e2017-03-30 08:02:11 -0400368void GrContextPriv::flush(GrSurfaceProxy* proxy) {
369 ASSERT_SINGLE_OWNER_PRIV
370 RETURN_IF_ABANDONED_PRIV
371 ASSERT_OWNED_PROXY_PRIV(proxy);
372
373 fContext->fDrawingManager->flush(proxy);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000374}
375
bsalomon81beccc2014-10-13 12:32:55 -0700376bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
377 const void* inPixels, size_t outRowBytes, void* outPixels) {
Matt Sarettc7b29082017-02-09 16:22:39 -0500378 SkColorType colorType;
379 if (!GrPixelConfigToColorType(srcConfig, &colorType) ||
380 4 != SkColorTypeBytesPerPixel(colorType))
381 {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000382 return false;
383 }
bsalomon81beccc2014-10-13 12:32:55 -0700384
Matt Sarettc7b29082017-02-09 16:22:39 -0500385 for (int y = 0; y < height; y++) {
386 SkOpts::RGBA_to_rgbA((uint32_t*) outPixels, inPixels, width);
387 outPixels = SkTAddOffset<void>(outPixels, outRowBytes);
388 inPixels = SkTAddOffset<const void>(inPixels, inRowBytes);
389 }
bsalomon81beccc2014-10-13 12:32:55 -0700390
Matt Sarettc7b29082017-02-09 16:22:39 -0500391 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000392}
393
Brian Osmand2ca59a2017-04-13 14:03:57 -0400394static bool valid_premul_config(GrPixelConfig config) {
Brian Osmance425512017-03-22 14:37:50 -0400395 return GrPixelConfigIs8888Unorm(config) || kRGBA_half_GrPixelConfig == config;
396}
397
Brian Osmand2ca59a2017-04-13 14:03:57 -0400398static bool valid_pixel_conversion(GrPixelConfig srcConfig, GrPixelConfig dstConfig,
399 bool premulConversion) {
400 // We don't allow conversion between integer configs and float/fixed configs.
401 if (GrPixelConfigIsSint(srcConfig) != GrPixelConfigIsSint(dstConfig)) {
402 return false;
403 }
404
405 // We only allow premul <-> unpremul conversions for some formats
406 if (premulConversion && (!valid_premul_config(srcConfig) || !valid_premul_config(dstConfig))) {
407 return false;
408 }
409
410 return true;
411}
412
Brian Osman409e74f2017-04-17 11:48:28 -0400413static bool pm_upm_must_round_trip(GrPixelConfig config, SkColorSpace* colorSpace) {
414 return !colorSpace &&
415 (kRGBA_8888_GrPixelConfig == config || kBGRA_8888_GrPixelConfig == config);
416}
417
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400418bool GrContextPriv::writeSurfacePixels(GrSurfaceContext* dst,
Robert Phillipse78b7252017-04-06 07:59:41 -0400419 int left, int top, int width, int height,
420 GrPixelConfig srcConfig, SkColorSpace* srcColorSpace,
421 const void* buffer, size_t rowBytes,
422 uint32_t pixelOpsFlags) {
Brian Osmanb62ea222016-12-22 11:12:16 -0500423 // TODO: Color space conversion
424
Robert Phillipse78b7252017-04-06 07:59:41 -0400425 ASSERT_SINGLE_OWNER_PRIV
426 RETURN_FALSE_IF_ABANDONED_PRIV
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400427 SkASSERT(dst);
428 ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy());
Brian Salomondcbb9d92017-07-19 10:53:20 -0400429 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "writeSurfacePixels", fContext);
bsalomon6c6f6582015-09-10 08:12:46 -0700430
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400431 if (!dst->asSurfaceProxy()->instantiate(fContext->resourceProvider())) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400432 return false;
433 }
434
Robert Phillips16d8ec62017-07-27 16:16:25 -0400435 GrSurfaceProxy* dstProxy = dst->asSurfaceProxy();
436 GrSurface* dstSurface = dstProxy->priv().peekSurface();
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400437
Brian Osmand2ca59a2017-04-13 14:03:57 -0400438 // 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 -0400439 const bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
Robert Phillips16d8ec62017-07-27 16:16:25 -0400440 if (!valid_pixel_conversion(srcConfig, dstProxy->config(), premul)) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400441 return false;
442 }
443
Brian Osman409e74f2017-04-17 11:48:28 -0400444 // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data,
445 // without any color spaces attached, and the caller wants us to premul.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400446 bool useConfigConversionEffect =
Brian Salomonf3569f02017-10-24 12:52:33 -0400447 premul && pm_upm_must_round_trip(srcConfig, srcColorSpace) &&
448 pm_upm_must_round_trip(dstProxy->config(), dst->colorSpaceInfo().colorSpace());
Brian Osman409e74f2017-04-17 11:48:28 -0400449
450 // Are we going to try to premul as part of a draw? For the non-legacy case, we always allow
451 // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly.
452 bool premulOnGpu = premul &&
453 (!useConfigConversionEffect || fContext->validPMUPMConversionExists());
bsalomon81beccc2014-10-13 12:32:55 -0700454
bsalomone8d21e82015-07-16 08:23:13 -0700455 // 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 -0700456 // necessary and because GrGpu::getWritePixelsInfo requires it.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400457 if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(),
bsalomone8d21e82015-07-16 08:23:13 -0700458 GrBytesPerPixel(srcConfig), &left, &top, &width,
459 &height, &buffer, &rowBytes)) {
460 return false;
461 }
462
Brian Osman409e74f2017-04-17 11:48:28 -0400463 GrGpu::DrawPreference drawPreference = premulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference
464 : GrGpu::kNoDraw_DrawPreference;
bsalomonf0674512015-07-28 13:26:15 -0700465 GrGpu::WritePixelTempDrawInfo tempDrawInfo;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400466 if (!fContext->fGpu->getWritePixelsInfo(dstSurface, dstProxy->origin(), width, height,
Robert Phillips16d8ec62017-07-27 16:16:25 -0400467 srcConfig, &drawPreference, &tempDrawInfo)) {
bsalomonf0674512015-07-28 13:26:15 -0700468 return false;
469 }
470
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400471 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && dstSurface->surfacePriv().hasPendingIO()) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400472 this->flush(nullptr); // MDB TODO: tighten this
bsalomonf0674512015-07-28 13:26:15 -0700473 }
474
Robert Phillips2f493142017-03-02 18:18:38 -0500475 sk_sp<GrTextureProxy> tempProxy;
bsalomonf0674512015-07-28 13:26:15 -0700476 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400477 tempProxy = GrSurfaceProxy::MakeDeferred(fContext->resourceProvider(),
Robert Phillips2f493142017-03-02 18:18:38 -0500478 tempDrawInfo.fTempSurfaceDesc,
479 SkBackingFit::kApprox,
480 SkBudgeted::kYes);
481 if (!tempProxy && GrGpu::kRequireDraw_DrawPreference == drawPreference) {
bsalomonf0674512015-07-28 13:26:15 -0700482 return false;
483 }
484 }
485
486 // temp buffer for doing sw premul conversion, if needed.
487 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
Brian Osman409e74f2017-04-17 11:48:28 -0400488 // We need to do sw premul if we were unable to create a RT for drawing, or if we can't do the
489 // premul on the GPU
490 if (premul && (!tempProxy || !premulOnGpu)) {
491 size_t tmpRowBytes = 4 * width;
492 tmpPixels.reset(width * height);
493 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
494 tmpPixels.get())) {
495 return false;
bsalomonf0674512015-07-28 13:26:15 -0700496 }
Brian Osman409e74f2017-04-17 11:48:28 -0400497 rowBytes = tmpRowBytes;
498 buffer = tmpPixels.get();
bsalomonf0674512015-07-28 13:26:15 -0700499 }
Brian Osman409e74f2017-04-17 11:48:28 -0400500
501 if (tempProxy) {
Brian Osman2240be92017-10-18 13:15:13 -0400502 auto fp = GrSimpleTextureEffect::Make(tempProxy, SkMatrix::I());
Brian Osman409e74f2017-04-17 11:48:28 -0400503 if (premulOnGpu) {
504 fp = fContext->createUPMToPMEffect(std::move(fp), useConfigConversionEffect);
bsalomon81beccc2014-10-13 12:32:55 -0700505 }
Brian Osman409e74f2017-04-17 11:48:28 -0400506 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400507 if (!fp) {
508 return false;
509 }
Brian Osman409e74f2017-04-17 11:48:28 -0400510
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400511 if (!tempProxy->instantiate(fContext->resourceProvider())) {
Brian Osman409e74f2017-04-17 11:48:28 -0400512 return false;
513 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400514 GrTexture* texture = tempProxy->priv().peekTexture();
Robert Phillips7bbbf622017-10-17 07:36:59 -0400515
516 if (tempProxy->priv().hasPendingIO()) {
517 this->flush(tempProxy.get());
518 }
519
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400520 if (!fContext->fGpu->writePixels(texture, tempProxy->origin(), 0, 0, width, height,
521 tempDrawInfo.fWriteConfig, buffer, rowBytes)) {
Brian Osman409e74f2017-04-17 11:48:28 -0400522 return false;
523 }
Robert Phillips7bbbf622017-10-17 07:36:59 -0400524 tempProxy = nullptr;
525
Brian Osman409e74f2017-04-17 11:48:28 -0400526 SkMatrix matrix;
527 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400528 GrRenderTargetContext* renderTargetContext = dst->asRenderTargetContext();
Brian Osman409e74f2017-04-17 11:48:28 -0400529 if (!renderTargetContext) {
530 return false;
531 }
532 GrPaint paint;
533 paint.addColorFragmentProcessor(std::move(fp));
534 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonf3569f02017-10-24 12:52:33 -0400535 paint.setAllowSRGBInputs(dst->colorSpaceInfo().isGammaCorrect() ||
536 GrPixelConfigIsSRGB(dst->colorSpaceInfo().config()));
Brian Osman409e74f2017-04-17 11:48:28 -0400537 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
538 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, matrix, rect,
539 nullptr);
540
541 if (kFlushWrites_PixelOp & pixelOpsFlags) {
542 this->flushSurfaceWrites(renderTargetContext->asRenderTargetProxy());
543 }
544 } else {
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400545 return fContext->fGpu->writePixels(dstSurface, dstProxy->origin(), left, top, width,
546 height, srcConfig, buffer, rowBytes);
bsalomon81beccc2014-10-13 12:32:55 -0700547 }
bsalomon81beccc2014-10-13 12:32:55 -0700548 return true;
549}
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000550
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400551bool GrContextPriv::readSurfacePixels(GrSurfaceContext* src,
Robert Phillipse78b7252017-04-06 07:59:41 -0400552 int left, int top, int width, int height,
553 GrPixelConfig dstConfig, SkColorSpace* dstColorSpace,
554 void* buffer, size_t rowBytes, uint32_t flags) {
Brian Osmanb62ea222016-12-22 11:12:16 -0500555 // TODO: Color space conversion
556
Robert Phillipse78b7252017-04-06 07:59:41 -0400557 ASSERT_SINGLE_OWNER_PRIV
558 RETURN_FALSE_IF_ABANDONED_PRIV
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400559 SkASSERT(src);
560 ASSERT_OWNED_PROXY_PRIV(src->asSurfaceProxy());
Brian Salomondcbb9d92017-07-19 10:53:20 -0400561 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "readSurfacePixels", fContext);
bsalomon32ab2602015-09-09 18:57:49 -0700562
Robert Phillipse78b7252017-04-06 07:59:41 -0400563 // MDB TODO: delay this instantiation until later in the method
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400564 if (!src->asSurfaceProxy()->instantiate(fContext->resourceProvider())) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400565 return false;
566 }
567
Robert Phillips16d8ec62017-07-27 16:16:25 -0400568 GrSurfaceProxy* srcProxy = src->asSurfaceProxy();
569 GrSurface* srcSurface = srcProxy->priv().peekSurface();
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400570
Brian Osmand2ca59a2017-04-13 14:03:57 -0400571 // The src is premul but the dst is unpremul -> unpremul the src after or as part of the read
572 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
Robert Phillips16d8ec62017-07-27 16:16:25 -0400573 if (!valid_pixel_conversion(srcProxy->config(), dstConfig, unpremul)) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400574 return false;
575 }
576
Brian Osman409e74f2017-04-17 11:48:28 -0400577 // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data,
578 // without any color spaces attached, and the caller wants us to unpremul.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400579 bool useConfigConversionEffect =
Brian Salomonf3569f02017-10-24 12:52:33 -0400580 unpremul &&
581 pm_upm_must_round_trip(srcProxy->config(), src->colorSpaceInfo().colorSpace()) &&
582 pm_upm_must_round_trip(dstConfig, dstColorSpace);
Brian Osman409e74f2017-04-17 11:48:28 -0400583
584 // Are we going to try to unpremul as part of a draw? For the non-legacy case, we always allow
585 // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly.
586 bool unpremulOnGpu = unpremul &&
587 (!useConfigConversionEffect || fContext->validPMUPMConversionExists());
bsalomon6c6f6582015-09-10 08:12:46 -0700588
bsalomone8d21e82015-07-16 08:23:13 -0700589 // Adjust the params so that if we wind up using an intermediate surface we've already done
590 // all the trimming and the temporary can be the min size required.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400591 if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(),
bsalomone8d21e82015-07-16 08:23:13 -0700592 GrBytesPerPixel(dstConfig), &left,
593 &top, &width, &height, &buffer, &rowBytes)) {
594 return false;
595 }
596
Brian Osman409e74f2017-04-17 11:48:28 -0400597 GrGpu::DrawPreference drawPreference = unpremulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference
598 : GrGpu::kNoDraw_DrawPreference;
bsalomon39826022015-07-23 08:07:21 -0700599 GrGpu::ReadPixelTempDrawInfo tempDrawInfo;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400600 if (!fContext->fGpu->getReadPixelsInfo(srcSurface, srcProxy->origin(), width, height, rowBytes,
Robert Phillips16d8ec62017-07-27 16:16:25 -0400601 dstConfig, &drawPreference, &tempDrawInfo)) {
bsalomon39826022015-07-23 08:07:21 -0700602 return false;
603 }
bsalomon191bcc02014-11-14 11:31:13 -0800604
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400605 if (!(kDontFlush_PixelOpsFlag & flags) && srcSurface->surfacePriv().hasPendingWrite()) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400606 this->flush(nullptr); // MDB TODO: tighten this
607 }
608
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400609 sk_sp<GrSurfaceProxy> proxyToRead = src->asSurfaceProxyRef();
bsalomon39826022015-07-23 08:07:21 -0700610 bool didTempDraw = false;
611 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomonb117ff12016-07-19 07:24:40 -0700612 if (SkBackingFit::kExact == tempDrawInfo.fTempSurfaceFit) {
bsalomon39826022015-07-23 08:07:21 -0700613 // We only respect this when the entire src is being read. Otherwise we can trigger too
614 // many odd ball texture sizes and trash the cache.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400615 if (width != srcSurface->width() || height != srcSurface->height()) {
bsalomonb117ff12016-07-19 07:24:40 -0700616 tempDrawInfo.fTempSurfaceFit= SkBackingFit::kApprox;
bsalomon39826022015-07-23 08:07:21 -0700617 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000618 }
brianosmandfe4f2e2016-07-21 13:28:36 -0700619 // TODO: Need to decide the semantics of this function for color spaces. Do we support
620 // conversion to a passed-in color space? For now, specifying nullptr means that this
621 // path will do no conversion, so it will match the behavior of the non-draw path.
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400622 sk_sp<GrRenderTargetContext> tempRTC = fContext->makeDeferredRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400623 tempDrawInfo.fTempSurfaceFit,
bsalomonb117ff12016-07-19 07:24:40 -0700624 tempDrawInfo.fTempSurfaceDesc.fWidth,
625 tempDrawInfo.fTempSurfaceDesc.fHeight,
626 tempDrawInfo.fTempSurfaceDesc.fConfig,
brianosmandfe4f2e2016-07-21 13:28:36 -0700627 nullptr,
bsalomonb117ff12016-07-19 07:24:40 -0700628 tempDrawInfo.fTempSurfaceDesc.fSampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -0400629 GrMipMapped::kNo,
bsalomonb117ff12016-07-19 07:24:40 -0700630 tempDrawInfo.fTempSurfaceDesc.fOrigin);
Brian Osman693a5402016-10-27 15:13:22 -0400631 if (tempRTC) {
Robert Phillips67c18d62017-01-20 12:44:06 -0500632 SkMatrix textureMatrix = SkMatrix::MakeTrans(SkIntToScalar(left), SkIntToScalar(top));
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400633 sk_sp<GrTextureProxy> proxy = src->asTextureProxyRef();
Brian Osman2240be92017-10-18 13:15:13 -0400634 auto fp = GrSimpleTextureEffect::Make(std::move(proxy), textureMatrix);
Brian Osman409e74f2017-04-17 11:48:28 -0400635 if (unpremulOnGpu) {
636 fp = fContext->createPMToUPMEffect(std::move(fp), useConfigConversionEffect);
637 // We no longer need to do this on CPU after the read back.
638 unpremul = false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000639 }
Brian Osman409e74f2017-04-17 11:48:28 -0400640 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400641 if (!fp) {
642 return false;
643 }
Brian Osman60cd57e2017-04-06 10:19:06 -0400644
Brian Osman409e74f2017-04-17 11:48:28 -0400645 GrPaint paint;
646 paint.addColorFragmentProcessor(std::move(fp));
647 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
648 paint.setAllowSRGBInputs(true);
649 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
650 tempRTC->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect,
651 nullptr);
652 proxyToRead = tempRTC->asTextureProxyRef();
653 left = 0;
654 top = 0;
655 didTempDraw = true;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000656 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000657 }
joshualitt5c55fef2014-10-31 14:04:35 -0700658
Robert Phillipse78b7252017-04-06 07:59:41 -0400659 if (!proxyToRead) {
660 return false;
661 }
662
bsalomon39826022015-07-23 08:07:21 -0700663 if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000664 return false;
665 }
bsalomon39826022015-07-23 08:07:21 -0700666 GrPixelConfig configToRead = dstConfig;
667 if (didTempDraw) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400668 this->flushSurfaceWrites(proxyToRead.get());
bsalomon6c9cd552016-01-22 07:17:34 -0800669 configToRead = tempDrawInfo.fReadConfig;
bsalomon39826022015-07-23 08:07:21 -0700670 }
Robert Phillips09dfc472017-09-13 15:25:47 -0400671
672 if (!proxyToRead->instantiate(fContext->resourceProvider())) {
673 return false;
674 }
675
676 GrSurface* surfaceToRead = proxyToRead->priv().peekSurface();
677
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400678 if (!fContext->fGpu->readPixels(surfaceToRead, proxyToRead->origin(),
Robert Phillips16d8ec62017-07-27 16:16:25 -0400679 left, top, width, height, configToRead, buffer, rowBytes)) {
bsalomon39826022015-07-23 08:07:21 -0700680 return false;
681 }
682
683 // Perform umpremul conversion if we weren't able to perform it as a draw.
684 if (unpremul) {
Matt Sarettc7b29082017-02-09 16:22:39 -0500685 SkColorType colorType;
686 if (!GrPixelConfigToColorType(dstConfig, &colorType) ||
687 4 != SkColorTypeBytesPerPixel(colorType))
688 {
reed@google.com7111d462014-03-25 16:20:24 +0000689 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000690 }
reed@google.com7111d462014-03-25 16:20:24 +0000691
Matt Sarettc7b29082017-02-09 16:22:39 -0500692 for (int y = 0; y < height; y++) {
693 SkUnpremultiplyRow<false>((uint32_t*) buffer, (const uint32_t*) buffer, width);
694 buffer = SkTAddOffset<void>(buffer, rowBytes);
695 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000696 }
697 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000698}
699
Robert Phillips7ee385e2017-03-30 08:02:11 -0400700void GrContextPriv::prepareSurfaceForExternalIO(GrSurfaceProxy* proxy) {
701 ASSERT_SINGLE_OWNER_PRIV
702 RETURN_IF_ABANDONED_PRIV
703 SkASSERT(proxy);
704 ASSERT_OWNED_PROXY_PRIV(proxy);
Greg Daniel51316782017-08-02 15:10:09 +0000705 fContext->fDrawingManager->prepareSurfaceForExternalIO(proxy, 0, nullptr);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000706}
707
Robert Phillips7ee385e2017-03-30 08:02:11 -0400708void GrContextPriv::flushSurfaceWrites(GrSurfaceProxy* proxy) {
709 ASSERT_SINGLE_OWNER_PRIV
710 RETURN_IF_ABANDONED_PRIV
711 SkASSERT(proxy);
712 ASSERT_OWNED_PROXY_PRIV(proxy);
713 if (proxy->priv().hasPendingWrite()) {
714 this->flush(proxy);
bsalomonf80bfed2014-10-07 05:56:02 -0700715 }
716}
717
Robert Phillips7ee385e2017-03-30 08:02:11 -0400718void GrContextPriv::flushSurfaceIO(GrSurfaceProxy* proxy) {
719 ASSERT_SINGLE_OWNER_PRIV
720 RETURN_IF_ABANDONED_PRIV
721 SkASSERT(proxy);
722 ASSERT_OWNED_PROXY_PRIV(proxy);
723 if (proxy->priv().hasPendingIO()) {
724 this->flush(proxy);
ajuma95243eb2016-08-24 08:19:02 -0700725 }
726}
727
bsalomon@google.com27847de2011-02-22 20:59:41 +0000728////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000729int GrContext::getRecommendedSampleCount(GrPixelConfig config,
730 SkScalar dpi) const {
joshualitt1de610a2016-01-06 08:26:09 -0800731 ASSERT_SINGLE_OWNER
732
bsalomon76228632015-05-29 08:02:10 -0700733 if (!this->caps()->isConfigRenderable(config, true)) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000734 return 0;
735 }
736 int chosenSampleCount = 0;
jvanverthe9c0fc62015-04-29 11:18:05 -0700737 if (fGpu->caps()->shaderCaps()->pathRenderingSupport()) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000738 if (dpi >= 250.0f) {
739 chosenSampleCount = 4;
740 } else {
741 chosenSampleCount = 16;
742 }
743 }
Greg Daniel81e7bf82017-07-19 14:47:42 -0400744 int supportedSampleCount = fGpu->caps()->getSampleCount(chosenSampleCount, config);
745 return chosenSampleCount <= supportedSampleCount ? supportedSampleCount : 0;
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000746}
747
Robert Phillips2c862492017-01-18 10:08:39 -0500748sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy> proxy,
749 sk_sp<SkColorSpace> colorSpace) {
Brian Osman45580d32016-11-23 09:37:01 -0500750 ASSERT_SINGLE_OWNER_PRIV
751
Brian Osman45580d32016-11-23 09:37:01 -0500752 if (proxy->asRenderTargetProxy()) {
Robert Phillips2c862492017-01-18 10:08:39 -0500753 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
754 std::move(colorSpace), nullptr);
Brian Osman45580d32016-11-23 09:37:01 -0500755 } else {
756 SkASSERT(proxy->asTextureProxy());
Robert Phillips2c862492017-01-18 10:08:39 -0500757 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Brian Osman45580d32016-11-23 09:37:01 -0500758 }
759}
760
Robert Phillipse2f7d182016-12-15 09:23:05 -0500761sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(const GrSurfaceDesc& dstDesc,
Greg Daniel65c7f662017-10-30 13:39:09 -0400762 GrMipMapped mipMapped,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500763 SkBackingFit fit,
764 SkBudgeted isDstBudgeted) {
765
Greg Daniel65c7f662017-10-30 13:39:09 -0400766 sk_sp<GrTextureProxy> proxy;
767 if (GrMipMapped::kNo == mipMapped) {
768 proxy = GrSurfaceProxy::MakeDeferred(fContext->resourceProvider(), dstDesc, fit,
769 isDstBudgeted);
770 } else {
771 SkASSERT(SkBackingFit::kExact == fit);
772 proxy = GrSurfaceProxy::MakeDeferredMipMap(fContext->resourceProvider(), dstDesc,
773 isDstBudgeted);
774 }
Robert Phillips77b3f322017-01-31 18:24:12 -0500775 if (!proxy) {
776 return nullptr;
777 }
Robert Phillipse2f7d182016-12-15 09:23:05 -0500778
Robert Phillips2c862492017-01-18 10:08:39 -0500779 return this->makeWrappedSurfaceContext(std::move(proxy), nullptr);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500780}
781
Brian Salomond17f6582017-07-19 18:28:58 -0400782sk_sp<GrTextureContext> GrContextPriv::makeBackendTextureContext(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000783 GrSurfaceOrigin origin,
Brian Osmanc1e37052017-03-09 14:19:20 -0500784 sk_sp<SkColorSpace> colorSpace) {
Robert Phillips26caf892017-01-27 10:58:31 -0500785 ASSERT_SINGLE_OWNER_PRIV
786
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400787 sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTexture(tex));
Robert Phillips26caf892017-01-27 10:58:31 -0500788 if (!surface) {
789 return nullptr;
790 }
791
Robert Phillips066f0202017-07-25 10:16:35 -0400792 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface), origin));
Robert Phillips77b3f322017-01-31 18:24:12 -0500793 if (!proxy) {
794 return nullptr;
795 }
Robert Phillips26caf892017-01-27 10:58:31 -0500796
Brian Salomond17f6582017-07-19 18:28:58 -0400797 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Robert Phillips26caf892017-01-27 10:58:31 -0500798}
799
Brian Osman11052242016-10-27 14:47:55 -0400800sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000801 const GrBackendTexture& tex,
802 GrSurfaceOrigin origin,
803 int sampleCnt,
Brian Osman11052242016-10-27 14:47:55 -0400804 sk_sp<SkColorSpace> colorSpace,
Brian Osmanc1e37052017-03-09 14:19:20 -0500805 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700806 ASSERT_SINGLE_OWNER_PRIV
robertphillips4fd74ae2016-08-03 14:26:53 -0700807
Brian Salomond17f6582017-07-19 18:28:58 -0400808 sk_sp<GrSurface> surface(
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400809 fContext->resourceProvider()->wrapRenderableBackendTexture(tex, sampleCnt));
robertphillips4fd74ae2016-08-03 14:26:53 -0700810 if (!surface) {
811 return nullptr;
812 }
813
Robert Phillips066f0202017-07-25 10:16:35 -0400814 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface), origin));
Robert Phillips77b3f322017-01-31 18:24:12 -0500815 if (!proxy) {
816 return nullptr;
817 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400818
Robert Phillips37430132016-11-09 06:50:43 -0500819 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Brian Osman11052242016-10-27 14:47:55 -0400820 std::move(colorSpace), props);
robertphillips4fd74ae2016-08-03 14:26:53 -0700821}
822
Brian Osman11052242016-10-27 14:47:55 -0400823sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext(
Greg Danielbcf612b2017-05-01 13:50:58 +0000824 const GrBackendRenderTarget& backendRT,
825 GrSurfaceOrigin origin,
robertphillips4fd74ae2016-08-03 14:26:53 -0700826 sk_sp<SkColorSpace> colorSpace,
827 const SkSurfaceProps* surfaceProps) {
828 ASSERT_SINGLE_OWNER_PRIV
829
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400830 sk_sp<GrRenderTarget> rt(fContext->resourceProvider()->wrapBackendRenderTarget(backendRT));
robertphillips4fd74ae2016-08-03 14:26:53 -0700831 if (!rt) {
832 return nullptr;
833 }
834
Robert Phillips066f0202017-07-25 10:16:35 -0400835 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(rt), origin));
Robert Phillips77b3f322017-01-31 18:24:12 -0500836 if (!proxy) {
837 return nullptr;
838 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400839
Robert Phillips37430132016-11-09 06:50:43 -0500840 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400841 std::move(colorSpace),
Brian Osman11052242016-10-27 14:47:55 -0400842 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -0700843}
844
Brian Osman11052242016-10-27 14:47:55 -0400845sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000846 const GrBackendTexture& tex,
847 GrSurfaceOrigin origin,
848 int sampleCnt,
robertphillips4fd74ae2016-08-03 14:26:53 -0700849 sk_sp<SkColorSpace> colorSpace,
850 const SkSurfaceProps* surfaceProps) {
851 ASSERT_SINGLE_OWNER_PRIV
robertphillips4fd74ae2016-08-03 14:26:53 -0700852
Greg Daniel7ef28f32017-04-20 16:41:55 +0000853 sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTextureAsRenderTarget(
854 tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000855 sampleCnt));
robertphillips4fd74ae2016-08-03 14:26:53 -0700856 if (!surface) {
857 return nullptr;
858 }
859
Robert Phillips066f0202017-07-25 10:16:35 -0400860 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface), origin));
Robert Phillips77b3f322017-01-31 18:24:12 -0500861 if (!proxy) {
862 return nullptr;
863 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400864
Robert Phillips37430132016-11-09 06:50:43 -0500865 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400866 std::move(colorSpace),
867 surfaceProps);
robertphillips77a2e522015-10-17 07:43:27 -0700868}
869
Chris Daltonfe199b72017-05-05 11:26:15 -0400870void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
871 fContext->fDrawingManager->addOnFlushCallbackObject(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400872}
873
874
robertphillips48fde9c2016-09-06 05:20:20 -0700875static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) {
Brian Osman78f20e02017-01-12 10:28:01 -0500876 switch (config) {
877 case kAlpha_8_GrPixelConfig:
878 case kRGB_565_GrPixelConfig:
879 case kRGBA_4444_GrPixelConfig:
880 case kBGRA_8888_GrPixelConfig:
881 return kRGBA_8888_GrPixelConfig;
882 case kSBGRA_8888_GrPixelConfig:
883 return kSRGBA_8888_GrPixelConfig;
884 case kAlpha_half_GrPixelConfig:
885 return kRGBA_half_GrPixelConfig;
886 default:
887 return kUnknown_GrPixelConfig;
888 }
robertphillips48fde9c2016-09-06 05:20:20 -0700889}
890
robertphillipsd728f0c2016-11-21 11:05:03 -0800891sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContextWithFallback(
892 SkBackingFit fit,
893 int width, int height,
894 GrPixelConfig config,
895 sk_sp<SkColorSpace> colorSpace,
896 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -0400897 GrMipMapped mipMapped,
robertphillipsd728f0c2016-11-21 11:05:03 -0800898 GrSurfaceOrigin origin,
899 const SkSurfaceProps* surfaceProps,
900 SkBudgeted budgeted) {
901 if (!this->caps()->isConfigRenderable(config, sampleCnt > 0)) {
902 config = GrPixelConfigFallback(config);
903 }
904
905 return this->makeDeferredRenderTargetContext(fit, width, height, config, std::move(colorSpace),
Greg Daniel45d63032017-10-30 13:41:26 -0400906 sampleCnt, mipMapped, origin, surfaceProps,
Greg Daniele1da1d92017-10-06 15:59:27 -0400907 budgeted);
robertphillipsd728f0c2016-11-21 11:05:03 -0800908}
909
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400910sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContext(
911 SkBackingFit fit,
912 int width, int height,
913 GrPixelConfig config,
914 sk_sp<SkColorSpace> colorSpace,
915 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -0400916 GrMipMapped mipMapped,
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400917 GrSurfaceOrigin origin,
918 const SkSurfaceProps* surfaceProps,
919 SkBudgeted budgeted) {
Brian Salomon79e4d1b2017-07-13 11:17:11 -0400920 if (this->abandoned()) {
921 return nullptr;
922 }
923
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400924 GrSurfaceDesc desc;
925 desc.fFlags = kRenderTarget_GrSurfaceFlag;
926 desc.fOrigin = origin;
927 desc.fWidth = width;
928 desc.fHeight = height;
929 desc.fConfig = config;
930 desc.fSampleCnt = sampleCnt;
931
Greg Daniele1da1d92017-10-06 15:59:27 -0400932 sk_sp<GrTextureProxy> rtp;
Greg Daniel45d63032017-10-30 13:41:26 -0400933 if (GrMipMapped::kNo == mipMapped) {
Greg Daniele1da1d92017-10-06 15:59:27 -0400934 rtp = GrSurfaceProxy::MakeDeferred(this->resourceProvider(), desc, fit, budgeted);
935 } else {
936 rtp = GrSurfaceProxy::MakeDeferredMipMap(this->resourceProvider(), desc, budgeted);
937 }
Robert Phillips08c5ec72017-01-30 12:26:47 -0500938 if (!rtp) {
939 return nullptr;
940 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400941
Robert Phillips1119dc32017-04-11 12:54:57 -0400942 sk_sp<GrRenderTargetContext> renderTargetContext(
943 fDrawingManager->makeRenderTargetContext(std::move(rtp),
944 std::move(colorSpace),
945 surfaceProps));
Robert Phillips1119dc32017-04-11 12:54:57 -0400946 if (!renderTargetContext) {
947 return nullptr;
948 }
949
950 renderTargetContext->discard();
951
952 return renderTargetContext;
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400953}
954
joshualitt1de610a2016-01-06 08:26:09 -0800955bool GrContext::abandoned() const {
956 ASSERT_SINGLE_OWNER
robertphillips7761d612016-05-16 09:14:53 -0700957 return fDrawingManager->wasAbandoned();
robertphillips77a2e522015-10-17 07:43:27 -0700958}
959
Brian Salomonaff329b2017-08-11 09:40:37 -0400960std::unique_ptr<GrFragmentProcessor> GrContext::createPMToUPMEffect(
961 std::unique_ptr<GrFragmentProcessor> fp, bool useConfigConversionEffect) {
Robert Phillips757914d2017-01-25 15:48:30 -0500962 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -0400963 // We have specialized effects that guarantee round-trip conversion for some formats
964 if (useConfigConversionEffect) {
965 // We should have already called this->validPMUPMConversionExists() in this case
966 SkASSERT(fDidTestPMConversions);
967 // ...and it should have succeeded
968 SkASSERT(this->validPMUPMConversionExists());
969
Brian Osman28804f32017-04-20 10:24:36 -0400970 return GrConfigConversionEffect::Make(std::move(fp),
971 GrConfigConversionEffect::kToUnpremul_PMConversion);
Brian Osman2d2da4f2017-04-12 17:07:22 -0400972 } else {
973 // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and
974 // explicitly round the results. Just do the obvious, naive thing in the shader.
975 return GrFragmentProcessor::UnpremulOutput(std::move(fp));
Robert Phillips757914d2017-01-25 15:48:30 -0500976 }
977}
978
Brian Salomonaff329b2017-08-11 09:40:37 -0400979std::unique_ptr<GrFragmentProcessor> GrContext::createUPMToPMEffect(
980 std::unique_ptr<GrFragmentProcessor> fp, bool useConfigConversionEffect) {
joshualitt1de610a2016-01-06 08:26:09 -0800981 ASSERT_SINGLE_OWNER
Brian Osman2d2da4f2017-04-12 17:07:22 -0400982 // We have specialized effects that guarantee round-trip conversion for these formats
Brian Osman409e74f2017-04-17 11:48:28 -0400983 if (useConfigConversionEffect) {
984 // We should have already called this->validPMUPMConversionExists() in this case
985 SkASSERT(fDidTestPMConversions);
986 // ...and it should have succeeded
987 SkASSERT(this->validPMUPMConversionExists());
988
Brian Osman28804f32017-04-20 10:24:36 -0400989 return GrConfigConversionEffect::Make(std::move(fp),
990 GrConfigConversionEffect::kToPremul_PMConversion);
Brian Osman2d2da4f2017-04-12 17:07:22 -0400991 } else {
992 // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and
993 // explicitly round the results. Just do the obvious, naive thing in the shader.
994 return GrFragmentProcessor::PremulOutput(std::move(fp));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000995 }
996}
997
Brian Osman409e74f2017-04-17 11:48:28 -0400998bool GrContext::validPMUPMConversionExists() {
joshualitt1de610a2016-01-06 08:26:09 -0800999 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -04001000 if (!fDidTestPMConversions) {
Brian Osman28804f32017-04-20 10:24:36 -04001001 fPMUPMConversionsRoundTrip = GrConfigConversionEffect::TestForPreservingPMConversions(this);
Brian Osman409e74f2017-04-17 11:48:28 -04001002 fDidTestPMConversions = true;
1003 }
1004
bsalomon636e8022015-07-29 06:08:46 -07001005 // The PM<->UPM tests fail or succeed together so we only need to check one.
Brian Osman28804f32017-04-20 10:24:36 -04001006 return fPMUPMConversionsRoundTrip;
bsalomon636e8022015-07-29 06:08:46 -07001007}
1008
bsalomon37f9a262015-02-02 13:00:10 -08001009//////////////////////////////////////////////////////////////////////////////
1010
1011void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -08001012 ASSERT_SINGLE_OWNER
bsalomon37f9a262015-02-02 13:00:10 -08001013 if (maxTextures) {
bsalomon0ea80f42015-02-11 10:49:59 -08001014 *maxTextures = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -08001015 }
1016 if (maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -08001017 *maxTextureBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -08001018 }
1019}
1020
1021void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
joshualitt1de610a2016-01-06 08:26:09 -08001022 ASSERT_SINGLE_OWNER
bsalomon0ea80f42015-02-11 10:49:59 -08001023 fResourceCache->setLimits(maxTextures, maxTextureBytes);
bsalomon37f9a262015-02-02 13:00:10 -08001024}
1025
ericrk0a5fa482015-09-15 14:16:10 -07001026//////////////////////////////////////////////////////////////////////////////
1027
1028void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -08001029 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -07001030 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
1031}
Brian Osman71a18892017-08-10 10:23:25 -04001032
1033//////////////////////////////////////////////////////////////////////////////
1034
1035SkString GrContext::dump() const {
1036 SkDynamicMemoryWStream stream;
1037 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
1038 writer.beginObject();
1039
1040 static const char* kBackendStr[] = {
1041 "Metal",
1042 "OpenGL",
1043 "Vulkan",
1044 "Mock",
1045 };
1046 GR_STATIC_ASSERT(0 == kMetal_GrBackend);
1047 GR_STATIC_ASSERT(1 == kOpenGL_GrBackend);
1048 GR_STATIC_ASSERT(2 == kVulkan_GrBackend);
1049 GR_STATIC_ASSERT(3 == kMock_GrBackend);
1050 writer.appendString("backend", kBackendStr[fBackend]);
1051
1052 writer.appendName("caps");
1053 fCaps->dumpJSON(&writer);
1054
1055 writer.appendName("gpu");
1056 fGpu->dumpJSON(&writer);
1057
1058 // Flush JSON to the memory stream
1059 writer.endObject();
1060 writer.flush();
1061
1062 // Null terminate the JSON data in the memory stream
1063 stream.write8(0);
1064
1065 // Allocate a string big enough to hold all the data, then copy out of the stream
1066 SkString result(stream.bytesWritten());
1067 stream.copyToAndReset(result.writable_str());
1068 return result;
1069}