blob: 6fbc4fe04ffb550a7df4834eac16fcca97d04e9f [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:
Robert Phillips05518182018-02-28 14:19:04 +000080 // DDL TODO: grab a GrRestrictedAtlasManager from the proxy
Robert Phillips88260b52018-01-19 12:56:09 -050081
82private:
83 typedef GrContext INHERITED;
84};
85
bsalomon682c2692015-05-22 14:01:46 -070086GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext) {
87 GrContextOptions defaultOptions;
88 return Create(backend, backendContext, defaultOptions);
89}
bsalomonf28cff72015-05-22 12:25:41 -070090
bsalomon682c2692015-05-22 14:01:46 -070091GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
92 const GrContextOptions& options) {
bsalomon682c2692015-05-22 14:01:46 -070093
Robert Phillips88260b52018-01-19 12:56:09 -050094 sk_sp<GrContext> context(new GrDirectContext(backend));
Robert Phillipse42edcc2017-12-13 11:50:22 -050095
96 context->fGpu = GrGpu::Make(backend, backendContext, options, context.get());
97 if (!context->fGpu) {
halcanary96fcdcc2015-08-27 07:41:13 -070098 return nullptr;
bsalomon@google.com27847de2011-02-22 20:59:41 +000099 }
Robert Phillipse42edcc2017-12-13 11:50:22 -0500100
101 if (!context->init(options)) {
102 return nullptr;
103 }
104
Brian Salomon91a3e522017-06-23 10:58:19 -0400105 return context.release();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000106}
107
Brian Salomon384fab42017-12-07 12:33:05 -0500108sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface) {
Greg Daniel02611d92017-07-25 10:05:01 -0400109 GrContextOptions defaultOptions;
Brian Salomon384fab42017-12-07 12:33:05 -0500110 return MakeGL(std::move(interface), defaultOptions);
Greg Daniel02611d92017-07-25 10:05:01 -0400111}
112
Brian Salomon384fab42017-12-07 12:33:05 -0500113sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface,
Greg Daniel02611d92017-07-25 10:05:01 -0400114 const GrContextOptions& options) {
Robert Phillips88260b52018-01-19 12:56:09 -0500115 sk_sp<GrContext> context(new GrDirectContext(kOpenGL_GrBackend));
Robert Phillipse42edcc2017-12-13 11:50:22 -0500116
Brian Salomon384fab42017-12-07 12:33:05 -0500117 context->fGpu = GrGLGpu::Make(std::move(interface), options, context.get());
Greg Daniel02611d92017-07-25 10:05:01 -0400118 if (!context->fGpu) {
119 return nullptr;
120 }
Greg Daniel02611d92017-07-25 10:05:01 -0400121 if (!context->init(options)) {
122 return nullptr;
123 }
124 return context;
125}
126
Brian Salomon990014d2017-12-07 16:23:39 -0500127sk_sp<GrContext> GrContext::MakeGL(const GrGLInterface* interface) {
128 return MakeGL(sk_ref_sp(interface));
129}
130
131sk_sp<GrContext> GrContext::MakeGL(const GrGLInterface* interface,
132 const GrContextOptions& options) {
133 return MakeGL(sk_ref_sp(interface), options);
134}
135
Greg Daniel02611d92017-07-25 10:05:01 -0400136sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions) {
137 GrContextOptions defaultOptions;
138 return MakeMock(mockOptions, defaultOptions);
139}
140
141sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions,
142 const GrContextOptions& options) {
Robert Phillips88260b52018-01-19 12:56:09 -0500143 sk_sp<GrContext> context(new GrDirectContext(kMock_GrBackend));
Robert Phillipse42edcc2017-12-13 11:50:22 -0500144
Brian Salomon384fab42017-12-07 12:33:05 -0500145 context->fGpu = GrMockGpu::Make(mockOptions, options, context.get());
Greg Daniel02611d92017-07-25 10:05:01 -0400146 if (!context->fGpu) {
147 return nullptr;
148 }
Greg Daniel02611d92017-07-25 10:05:01 -0400149 if (!context->init(options)) {
150 return nullptr;
151 }
152 return context;
153}
154
155#ifdef SK_VULKAN
Brian Salomon384fab42017-12-07 12:33:05 -0500156sk_sp<GrContext> GrContext::MakeVulkan(sk_sp<const GrVkBackendContext> backendContext) {
Greg Daniel02611d92017-07-25 10:05:01 -0400157 GrContextOptions defaultOptions;
Brian Salomon384fab42017-12-07 12:33:05 -0500158 return MakeVulkan(std::move(backendContext), defaultOptions);
Greg Daniel02611d92017-07-25 10:05:01 -0400159}
160
Brian Salomon384fab42017-12-07 12:33:05 -0500161sk_sp<GrContext> GrContext::MakeVulkan(sk_sp<const GrVkBackendContext> backendContext,
Greg Daniel02611d92017-07-25 10:05:01 -0400162 const GrContextOptions& options) {
Robert Phillips88260b52018-01-19 12:56:09 -0500163 sk_sp<GrContext> context(new GrDirectContext(kVulkan_GrBackend));
Robert Phillipse42edcc2017-12-13 11:50:22 -0500164
Brian Salomon384fab42017-12-07 12:33:05 -0500165 context->fGpu = GrVkGpu::Make(std::move(backendContext), options, context.get());
Greg Daniel02611d92017-07-25 10:05:01 -0400166 if (!context->fGpu) {
167 return nullptr;
168 }
Greg Daniel02611d92017-07-25 10:05:01 -0400169 if (!context->init(options)) {
170 return nullptr;
171 }
172 return context;
173}
174#endif
175
Greg Danielb76a72a2017-07-13 15:07:54 -0400176#ifdef SK_METAL
Greg Daniel02611d92017-07-25 10:05:01 -0400177sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue) {
178 GrContextOptions defaultOptions;
179 return MakeMetal(device, queue, defaultOptions);
180}
181
Greg Danielb76a72a2017-07-13 15:07:54 -0400182sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) {
Robert Phillips05518182018-02-28 14:19:04 +0000183 sk_sp<GrContext> context(new GrContext(kMetal_GrBackend));
Robert Phillipse42edcc2017-12-13 11:50:22 -0500184
Brian Salomon384fab42017-12-07 12:33:05 -0500185 context->fGpu = GrMtlTrampoline::MakeGpu(context.get(), options, device, queue);
Greg Danielb76a72a2017-07-13 15:07:54 -0400186 if (!context->fGpu) {
187 return nullptr;
188 }
Greg Danielb76a72a2017-07-13 15:07:54 -0400189 if (!context->init(options)) {
190 return nullptr;
191 }
192 return context;
193}
194#endif
195
joshualitt0acd0d32015-05-07 08:23:19 -0700196static int32_t gNextID = 1;
197static int32_t next_id() {
198 int32_t id;
199 do {
200 id = sk_atomic_inc(&gNextID);
201 } while (id == SK_InvalidGenID);
202 return id;
203}
204
Robert Phillipse42edcc2017-12-13 11:50:22 -0500205sk_sp<GrContext> GrContextPriv::MakeDDL(GrContextThreadSafeProxy* proxy) {
Robert Phillips88260b52018-01-19 12:56:09 -0500206 sk_sp<GrContext> context(new GrDDLContext(proxy));
Robert Phillipse42edcc2017-12-13 11:50:22 -0500207
Robert Phillips88260b52018-01-19 12:56:09 -0500208 // Note: we aren't creating a Gpu here. This causes the resource provider & cache to
209 // also not be created
Robert Phillipse42edcc2017-12-13 11:50:22 -0500210 if (!context->init(proxy->fOptions)) {
211 return nullptr;
212 }
213 return context;
214}
215
216GrContext::GrContext(GrBackend backend)
217 : fUniqueID(next_id())
218 , fBackend(backend) {
halcanary96fcdcc2015-08-27 07:41:13 -0700219 fResourceCache = nullptr;
220 fResourceProvider = nullptr;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500221 fProxyProvider = nullptr;
Robert Phillipsacf17902018-02-27 16:43:18 -0500222 fGlyphCache = nullptr;
Robert Phillips05518182018-02-28 14:19:04 +0000223 fFullAtlasManager = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000224}
225
Robert Phillipse42edcc2017-12-13 11:50:22 -0500226GrContext::GrContext(GrContextThreadSafeProxy* proxy)
Robert Phillips88260b52018-01-19 12:56:09 -0500227 : fCaps(proxy->fCaps)
228 , fUniqueID(proxy->fContextUniqueID)
Robert Phillipse42edcc2017-12-13 11:50:22 -0500229 , fBackend(proxy->fBackend) {
230 fResourceCache = nullptr;
231 fResourceProvider = nullptr;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500232 fProxyProvider = nullptr;
Robert Phillipsacf17902018-02-27 16:43:18 -0500233 fGlyphCache = nullptr;
Robert Phillips05518182018-02-28 14:19:04 +0000234 fFullAtlasManager = nullptr;
Greg Danielb76a72a2017-07-13 15:07:54 -0400235}
joshualitt1de610a2016-01-06 08:26:09 -0800236
Greg Danielb76a72a2017-07-13 15:07:54 -0400237bool GrContext::init(const GrContextOptions& options) {
238 ASSERT_SINGLE_OWNER
Robert Phillips88260b52018-01-19 12:56:09 -0500239
240 if (fGpu) {
241 fCaps = fGpu->refCaps();
242 fResourceCache = new GrResourceCache(fCaps.get(), fUniqueID);
Robert Phillips4150eea2018-02-07 17:08:21 -0500243 fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, &fSingleOwner,
244 options.fExplicitlyAllocateGPUResources);
Robert Phillips88260b52018-01-19 12:56:09 -0500245 }
246
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500247 fProxyProvider = new GrProxyProvider(fResourceProvider, fResourceCache, fCaps, &fSingleOwner);
Robert Phillips88260b52018-01-19 12:56:09 -0500248
249 if (fResourceCache) {
250 fResourceCache->setProxyProvider(fProxyProvider);
251 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500252
Robert Phillipse42edcc2017-12-13 11:50:22 -0500253 // DDL TODO: we need to think through how the task group & persistent cache
254 // get passed on to/shared between all the DDLRecorders created with this context.
255 fThreadSafeProxy.reset(new GrContextThreadSafeProxy(fCaps, this->uniqueID(), fBackend,
256 options));
commit-bot@chromium.org1836d332013-07-16 22:55:03 +0000257
Brian Osman46da1cc2017-02-14 14:15:48 -0500258 fDisableGpuYUVConversion = options.fDisableGpuYUVConversion;
Brian Osman8a83ca42018-02-12 14:32:17 -0500259 fSharpenMipmappedTextures = options.fSharpenMipmappedTextures;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000260 fDidTestPMConversions = false;
261
bsalomon6b2552f2016-09-15 13:50:26 -0700262 GrPathRendererChain::Options prcOptions;
bsalomon39ef7fb2016-09-21 11:16:05 -0700263 prcOptions.fAllowPathMaskCaching = options.fAllowPathMaskCaching;
Brian Osman195c05b2017-08-30 15:14:04 -0400264#if GR_TEST_UTILS
csmartdalton008b9d82017-02-22 12:00:42 -0700265 prcOptions.fGpuPathRenderers = options.fGpuPathRenderers;
Brian Osman195c05b2017-08-30 15:14:04 -0400266#endif
Brian Osmanb350ae22017-08-29 16:15:39 -0400267 if (options.fDisableDistanceFieldPaths) {
Brian Osman195c05b2017-08-30 15:14:04 -0400268 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
Brian Osmanb350ae22017-08-29 16:15:39 -0400269 }
Brian Salomonaf597482017-11-07 16:23:34 -0500270
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500271 if (!fResourceCache) {
272 // DDL TODO: remove this crippling of the path renderer chain
273 // Disable the small path renderer bc of the proxies in the atlas. They need to be
274 // unified when the opLists are added back to the destination drawing manager.
275 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
276 }
277
Brian Salomonaf597482017-11-07 16:23:34 -0500278 GrAtlasTextContext::Options atlasTextContextOptions;
279 atlasTextContextOptions.fMaxDistanceFieldFontSize = options.fGlyphsAsPathsFontSize;
280 atlasTextContextOptions.fMinDistanceFieldFontSize = options.fMinDistanceFieldFontSize;
Brian Salomonb5086962017-12-13 10:59:33 -0500281 atlasTextContextOptions.fDistanceFieldVerticesAlwaysHaveW = false;
282#if SK_SUPPORT_ATLAS_TEXT
283 if (GrContextOptions::Enable::kYes == options.fDistanceFieldGlyphVerticesAlwaysHaveW) {
284 atlasTextContextOptions.fDistanceFieldVerticesAlwaysHaveW = true;
285 }
286#endif
Brian Salomonaf597482017-11-07 16:23:34 -0500287
Robert Phillips4150eea2018-02-07 17:08:21 -0500288 fDrawingManager.reset(new GrDrawingManager(this, prcOptions, atlasTextContextOptions,
289 &fSingleOwner, options.fSortRenderTargets));
joshualitt7c3a2f82015-03-31 13:32:05 -0700290
Robert Phillips05518182018-02-28 14:19:04 +0000291 GrDrawOpAtlas::AllowMultitexturing allowMultitexturing;
292 if (GrContextOptions::Enable::kNo == options.fAllowMultipleGlyphCacheTextures ||
293 // multitexturing supported only if range can represent the index + texcoords fully
294 !(fCaps->shaderCaps()->floatIs32Bits() || fCaps->shaderCaps()->integerSupport())) {
295 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
296 } else {
297 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
298 }
299
Robert Phillipsacf17902018-02-27 16:43:18 -0500300 fGlyphCache = new GrGlyphCache;
301
Robert Phillips05518182018-02-28 14:19:04 +0000302 // DDL TODO: in DDL-mode grab a GrRestrictedAtlasManager from the thread-proxy and
303 // do not add an onFlushCB
304 fFullAtlasManager = new GrAtlasManager(fProxyProvider, fGlyphCache,
305 options.fGlyphCacheTextureMaximumBytes,
306 allowMultitexturing);
307 this->contextPriv().addOnFlushCallbackObject(fFullAtlasManager);
308
309 fGlyphCache->setGlyphSizeLimit(fFullAtlasManager->getGlyphSizeLimit());
310
Robert Phillips303cd582018-02-14 18:54:01 -0500311 fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB,
312 this, this->uniqueID(), SkToBool(fGpu)));
Brian Salomon91a3e522017-06-23 10:58:19 -0400313
Brian Osman51279982017-08-23 10:12:00 -0400314 if (options.fExecutor) {
315 fTaskGroup = skstd::make_unique<SkTaskGroup>(*options.fExecutor);
316 }
317
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400318 fPersistentCache = options.fPersistentCache;
319
Brian Salomon91a3e522017-06-23 10:58:19 -0400320 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000321}
322
bsalomon@google.com27847de2011-02-22 20:59:41 +0000323GrContext::~GrContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800324 ASSERT_SINGLE_OWNER
325
Robert Phillips05518182018-02-28 14:19:04 +0000326 if (fGpu) {
327 this->flush();
328 }
329
Robert Phillips2e6feed2018-01-22 15:27:20 -0500330 if (fDrawingManager) {
331 fDrawingManager->cleanup();
332 }
robertphillips2334fb62015-06-17 05:43:33 -0700333
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000334 for (int i = 0; i < fCleanUpData.count(); ++i) {
335 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
336 }
337
halcanary385fe4d2015-08-26 13:07:48 -0700338 delete fResourceProvider;
339 delete fResourceCache;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500340 delete fProxyProvider;
Robert Phillipsacf17902018-02-27 16:43:18 -0500341 delete fGlyphCache;
Robert Phillips05518182018-02-28 14:19:04 +0000342 delete fFullAtlasManager;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000343}
344
bungeman6bd52842016-10-27 09:30:08 -0700345sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
bungeman6bd52842016-10-27 09:30:08 -0700346 return fThreadSafeProxy;
bsalomon41b952c2016-03-11 06:46:33 -0800347}
348
Robert Phillipsfc711a22018-02-13 17:03:00 -0500349SkSurfaceCharacterization GrContextThreadSafeProxy::createCharacterization(
350 size_t cacheMaxResourceBytes,
351 const SkImageInfo& ii, const GrBackendFormat& backendFormat,
352 int sampleCnt, GrSurfaceOrigin origin,
353 const SkSurfaceProps& surfaceProps,
354 bool isMipMapped) {
355 if (!backendFormat.isValid()) {
356 return SkSurfaceCharacterization(); // return an invalid characterization
357 }
358
359 // We're assuming GrFSAAType::kMixedSamples will never be specified via this code path
360 GrFSAAType FSAAType = sampleCnt > 1 ? GrFSAAType::kUnifiedMSAA : GrFSAAType::kNone;
361
362 if (!fCaps->mipMapSupport()) {
363 isMipMapped = false;
364 }
365
366 GrPixelConfig config = kUnknown_GrPixelConfig;
367 if (!fCaps->getConfigFromBackendFormat(backendFormat, ii.colorType(), &config)) {
368 return SkSurfaceCharacterization(); // return an invalid characterization
369 }
370
371 // This surface characterization factory assumes that the resulting characterization is
372 // textureable.
373 if (!fCaps->isConfigTexturable(config)) {
374 return SkSurfaceCharacterization(); // return an invalid characterization
375 }
376
377 return SkSurfaceCharacterization(sk_ref_sp<GrContextThreadSafeProxy>(this),
378 cacheMaxResourceBytes,
379 origin, ii.width(), ii.height(), config, FSAAType, sampleCnt,
380 SkSurfaceCharacterization::Textureable(true),
381 SkSurfaceCharacterization::MipMapped(isMipMapped),
382 ii.refColorSpace(), surfaceProps);
383}
384
bsalomon2354f842014-07-28 13:48:36 -0700385void GrContext::abandonContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800386 ASSERT_SINGLE_OWNER
387
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500388 fProxyProvider->abandon();
bsalomond309e7a2015-04-30 14:18:54 -0700389 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800390
391 // Need to abandon the drawing manager first so all the render targets
392 // will be released/forgotten before they too are abandoned.
393 fDrawingManager->abandon();
394
bsalomon@google.com205d4602011-04-25 12:43:45 +0000395 // abandon first to so destructors
396 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800397 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700398
bsalomon6e2aad42016-04-01 11:54:31 -0700399 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
400
Robert Phillipsacf17902018-02-27 16:43:18 -0500401 fGlyphCache->freeAll();
Robert Phillips05518182018-02-28 14:19:04 +0000402 fFullAtlasManager->freeAll();
bsalomon6e2aad42016-04-01 11:54:31 -0700403 fTextBlobCache->freeAll();
404}
405
406void GrContext::releaseResourcesAndAbandonContext() {
407 ASSERT_SINGLE_OWNER
408
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500409 fProxyProvider->abandon();
bsalomon6e2aad42016-04-01 11:54:31 -0700410 fResourceProvider->abandon();
411
412 // Need to abandon the drawing manager first so all the render targets
413 // will be released/forgotten before they too are abandoned.
414 fDrawingManager->abandon();
415
416 // Release all resources in the backend 3D API.
417 fResourceCache->releaseAll();
418
419 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000420
Robert Phillipsacf17902018-02-27 16:43:18 -0500421 fGlyphCache->freeAll();
Robert Phillips05518182018-02-28 14:19:04 +0000422 fFullAtlasManager->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700423 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000424}
425
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000426void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800427 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000428 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000429}
430
431void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800432 ASSERT_SINGLE_OWNER
433
Robert Phillips05518182018-02-28 14:19:04 +0000434 this->flush();
435
Robert Phillipsacf17902018-02-27 16:43:18 -0500436 fGlyphCache->freeAll();
Robert Phillips05518182018-02-28 14:19:04 +0000437 fFullAtlasManager->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700438
439 fDrawingManager->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700440
441 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000442}
443
Jim Van Verth76d917c2017-12-13 09:26:37 -0500444void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) {
Brian Salomon5e150852017-03-22 14:53:13 -0400445 ASSERT_SINGLE_OWNER
Jim Van Verth76d917c2017-12-13 09:26:37 -0500446 fResourceCache->purgeAsNeeded();
447 fResourceCache->purgeResourcesNotUsedSince(GrStdSteadyClock::now() - msNotUsed);
448
449 fTextBlobCache->purgeStaleBlobs();
Brian Salomon5e150852017-03-22 14:53:13 -0400450}
451
Derek Sollenberger5480a182017-05-25 16:43:59 -0400452void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
453 ASSERT_SINGLE_OWNER
454 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
455}
456
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000457void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800458 ASSERT_SINGLE_OWNER
459
bsalomon71cb0c22014-11-14 12:10:14 -0800460 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800461 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800462 }
463 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800464 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800465 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000466}
467
Derek Sollenbergeree479142017-05-24 11:41:33 -0400468size_t GrContext::getResourceCachePurgeableBytes() const {
469 ASSERT_SINGLE_OWNER
470 return fResourceCache->getPurgeableBytes();
471}
472
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000473////////////////////////////////////////////////////////////////////////////////
474
Brian Salomonbdecacf2018-02-02 20:32:49 -0500475bool GrContext::colorTypeSupportedAsImage(SkColorType colorType) const {
476 GrPixelConfig config = SkImageInfo2GrPixelConfig(colorType, nullptr, *this->caps());
477 return this->caps()->isConfigTexturable(config);
478}
479
480int GrContext::maxSurfaceSampleCountForColorType(SkColorType colorType) const {
481 GrPixelConfig config = SkImageInfo2GrPixelConfig(colorType, nullptr, *this->caps());
482 return this->caps()->maxRenderTargetSampleCount(config);
483}
484
485////////////////////////////////////////////////////////////////////////////////
486
joshualitt0db6dfa2015-04-10 07:01:30 -0700487void GrContext::TextBlobCacheOverBudgetCB(void* data) {
488 SkASSERT(data);
Brian Osman11052242016-10-27 14:47:55 -0400489 // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on
490 // GrRenderTargetContext to perform a necessary flush. The solution is to move drawText calls
491 // to below the GrContext level, but this is not trivial because they call drawPath on
492 // SkGpuDevice.
joshualitt0db6dfa2015-04-10 07:01:30 -0700493 GrContext* context = reinterpret_cast<GrContext*>(data);
494 context->flush();
495}
496
bsalomon@google.com27847de2011-02-22 20:59:41 +0000497////////////////////////////////////////////////////////////////////////////////
498
bsalomonb77a9072016-09-07 10:02:04 -0700499void GrContext::flush() {
joshualitt1de610a2016-01-06 08:26:09 -0800500 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700501 RETURN_IF_ABANDONED
Robert Phillips7ee385e2017-03-30 08:02:11 -0400502
503 fDrawingManager->flush(nullptr);
504}
505
Greg Daniel51316782017-08-02 15:10:09 +0000506GrSemaphoresSubmitted GrContext::flushAndSignalSemaphores(int numSemaphores,
507 GrBackendSemaphore signalSemaphores[]) {
508 ASSERT_SINGLE_OWNER
509 if (fDrawingManager->wasAbandoned()) { return GrSemaphoresSubmitted::kNo; }
510
511 return fDrawingManager->flush(nullptr, numSemaphores, signalSemaphores);
512}
513
Robert Phillips7ee385e2017-03-30 08:02:11 -0400514void GrContextPriv::flush(GrSurfaceProxy* proxy) {
515 ASSERT_SINGLE_OWNER_PRIV
516 RETURN_IF_ABANDONED_PRIV
517 ASSERT_OWNED_PROXY_PRIV(proxy);
518
519 fContext->fDrawingManager->flush(proxy);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000520}
521
Brian Salomonc320b152018-02-20 14:05:36 -0500522bool sw_convert_to_premul(GrColorType srcColorType, int width, int height, size_t inRowBytes,
bsalomon81beccc2014-10-13 12:32:55 -0700523 const void* inPixels, size_t outRowBytes, void* outPixels) {
Brian Salomonc320b152018-02-20 14:05:36 -0500524 SkColorType colorType = GrColorTypeToSkColorType(srcColorType);
525 if (kUnknown_SkColorType == colorType || 4 != SkColorTypeBytesPerPixel(colorType)) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000526 return false;
527 }
bsalomon81beccc2014-10-13 12:32:55 -0700528
Matt Sarettc7b29082017-02-09 16:22:39 -0500529 for (int y = 0; y < height; y++) {
530 SkOpts::RGBA_to_rgbA((uint32_t*) outPixels, inPixels, width);
531 outPixels = SkTAddOffset<void>(outPixels, outRowBytes);
532 inPixels = SkTAddOffset<const void>(inPixels, inRowBytes);
533 }
bsalomon81beccc2014-10-13 12:32:55 -0700534
Matt Sarettc7b29082017-02-09 16:22:39 -0500535 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000536}
537
Brian Salomonc320b152018-02-20 14:05:36 -0500538// TODO: This will be removed when GrSurfaceContexts are aware of their color types.
539// (skbug.com/6718)
Brian Osmand2ca59a2017-04-13 14:03:57 -0400540static bool valid_premul_config(GrPixelConfig config) {
Brian Salomonc320b152018-02-20 14:05:36 -0500541 switch (config) {
542 case kUnknown_GrPixelConfig: return false;
543 case kAlpha_8_GrPixelConfig: return false;
544 case kGray_8_GrPixelConfig: return false;
545 case kRGB_565_GrPixelConfig: return false;
546 case kRGBA_4444_GrPixelConfig: return true;
547 case kRGBA_8888_GrPixelConfig: return true;
548 case kBGRA_8888_GrPixelConfig: return true;
549 case kSRGBA_8888_GrPixelConfig: return true;
550 case kSBGRA_8888_GrPixelConfig: return true;
551 case kRGBA_float_GrPixelConfig: return true;
552 case kRG_float_GrPixelConfig: return false;
553 case kAlpha_half_GrPixelConfig: return false;
554 case kRGBA_half_GrPixelConfig: return true;
555 case kAlpha_8_as_Alpha_GrPixelConfig: return false;
556 case kAlpha_8_as_Red_GrPixelConfig: return false;
557 case kAlpha_half_as_Red_GrPixelConfig: return false;
558 case kGray_8_as_Lum_GrPixelConfig: return false;
559 case kGray_8_as_Red_GrPixelConfig: return false;
560 }
561 SK_ABORT("Invalid GrPixelConfig");
562 return false;
Brian Osmance425512017-03-22 14:37:50 -0400563}
564
Brian Salomonc320b152018-02-20 14:05:36 -0500565static bool valid_premul_color_type(GrColorType ct) {
566 switch (ct) {
567 case GrColorType::kUnknown: return false;
568 case GrColorType::kAlpha_8: return false;
569 case GrColorType::kRGB_565: return false;
570 case GrColorType::kABGR_4444: return true;
571 case GrColorType::kRGBA_8888: return true;
572 case GrColorType::kBGRA_8888: return true;
573 case GrColorType::kGray_8: return false;
574 case GrColorType::kAlpha_F16: return false;
575 case GrColorType::kRGBA_F16: return true;
576 case GrColorType::kRG_F32: return false;
577 case GrColorType::kRGBA_F32: return true;
578 }
579 SK_ABORT("Invalid GrColorType");
580 return false;
581}
582
583static bool valid_pixel_conversion(GrColorType cpuColorType, GrPixelConfig gpuConfig,
Brian Osmand2ca59a2017-04-13 14:03:57 -0400584 bool premulConversion) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400585 // We only allow premul <-> unpremul conversions for some formats
Brian Salomonc320b152018-02-20 14:05:36 -0500586 if (premulConversion &&
587 (!valid_premul_color_type(cpuColorType) || !valid_premul_config(gpuConfig))) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400588 return false;
589 }
590
591 return true;
592}
593
Brian Salomonc320b152018-02-20 14:05:36 -0500594static bool pm_upm_must_round_trip(GrColorType cpuColorType, const SkColorSpace* cpuColorSpace) {
595 return !cpuColorSpace &&
596 (GrColorType::kRGBA_8888 == cpuColorType || GrColorType::kBGRA_8888 == cpuColorType);
Brian Osman409e74f2017-04-17 11:48:28 -0400597}
598
Brian Salomonc320b152018-02-20 14:05:36 -0500599// TODO: This will be removed when GrSurfaceContexts are aware of their color types.
600// (skbug.com/6718)
601static bool pm_upm_must_round_trip(GrPixelConfig surfaceConfig,
602 const SkColorSpace* surfaceColorSpace) {
603 return !surfaceColorSpace &&
604 (kRGBA_8888_GrPixelConfig == surfaceConfig || kBGRA_8888_GrPixelConfig == surfaceConfig);
605}
606
607static GrSRGBConversion determine_write_pixels_srgb_conversion(GrColorType srcColorType,
608 const SkColorSpace* srcColorSpace,
609 GrSRGBEncoded dstSRGBEncoded,
610 const SkColorSpace* dstColorSpace,
611 const GrCaps& caps) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500612 // No support for sRGB-encoded alpha.
Brian Salomonc320b152018-02-20 14:05:36 -0500613 if (GrColorTypeIsAlphaOnly(srcColorType)) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500614 return GrSRGBConversion::kNone;
615 }
Brian Salomonc320b152018-02-20 14:05:36 -0500616 // No conversions without GPU support for sRGB. (Legacy mode)
617 if (!caps.srgbSupport()) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500618 return GrSRGBConversion::kNone;
619 }
Brian Salomonc320b152018-02-20 14:05:36 -0500620 // If the GrSurfaceContext has no color space then it is in legacy mode.
621 if (!dstColorSpace) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500622 return GrSRGBConversion::kNone;
623 }
624
625 bool srcColorSpaceIsSRGB = srcColorSpace && srcColorSpace->gammaCloseToSRGB();
626 bool dstColorSpaceIsSRGB = dstColorSpace->gammaCloseToSRGB();
627
628 // For now we are assuming that if color space of the dst does not have sRGB gamma then the
629 // texture format is not sRGB encoded and vice versa. Note that we already checked for "legacy"
Brian Salomonc320b152018-02-20 14:05:36 -0500630 // mode being forced on by caps above. This may change in the future. We will then have to
631 // perform shader based conversions.
Brian Salomon9b009bb2018-02-14 13:53:55 -0500632 SkASSERT(dstColorSpaceIsSRGB == (GrSRGBEncoded::kYes == dstSRGBEncoded));
633
Brian Salomon9b009bb2018-02-14 13:53:55 -0500634 if (srcColorSpaceIsSRGB == dstColorSpaceIsSRGB) {
635 return GrSRGBConversion::kNone;
636 }
637 return srcColorSpaceIsSRGB ? GrSRGBConversion::kSRGBToLinear : GrSRGBConversion::kLinearToSRGB;
638}
639
Brian Salomonc320b152018-02-20 14:05:36 -0500640static GrSRGBConversion determine_read_pixels_srgb_conversion(GrSRGBEncoded srcSRGBEncoded,
641 const SkColorSpace* srcColorSpace,
642 GrColorType dstColorType,
643 const SkColorSpace* dstColorSpace,
644 const GrCaps& caps) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500645 // This is symmetrical with the write version.
Brian Salomonc320b152018-02-20 14:05:36 -0500646 switch (determine_write_pixels_srgb_conversion(dstColorType, dstColorSpace, srcSRGBEncoded,
647 srcColorSpace, caps)) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500648 case GrSRGBConversion::kNone: return GrSRGBConversion::kNone;
649 case GrSRGBConversion::kLinearToSRGB: return GrSRGBConversion::kSRGBToLinear;
650 case GrSRGBConversion::kSRGBToLinear: return GrSRGBConversion::kLinearToSRGB;
651 }
652 return GrSRGBConversion::kNone;
653}
654
Brian Salomonc320b152018-02-20 14:05:36 -0500655bool GrContextPriv::writeSurfacePixels(GrSurfaceContext* dst, int left, int top, int width,
656 int height, GrColorType srcColorType,
657 SkColorSpace* srcColorSpace, const void* buffer,
658 size_t rowBytes, uint32_t pixelOpsFlags) {
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500659#ifndef SK_LEGACY_GPU_PIXEL_OPS
660 return this->writeSurfacePixels2(dst, left, top, width, height, srcColorType, srcColorSpace,
661 buffer, rowBytes, pixelOpsFlags);
662#endif
663
Brian Osmanb62ea222016-12-22 11:12:16 -0500664 // TODO: Color space conversion
665
Robert Phillipse78b7252017-04-06 07:59:41 -0400666 ASSERT_SINGLE_OWNER_PRIV
667 RETURN_FALSE_IF_ABANDONED_PRIV
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400668 SkASSERT(dst);
669 ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy());
Brian Salomondcbb9d92017-07-19 10:53:20 -0400670 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "writeSurfacePixels", fContext);
bsalomon6c6f6582015-09-10 08:12:46 -0700671
Robert Phillips6be756b2018-01-16 15:07:54 -0500672 if (!dst->asSurfaceProxy()->instantiate(this->resourceProvider())) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400673 return false;
674 }
675
Robert Phillips16d8ec62017-07-27 16:16:25 -0400676 GrSurfaceProxy* dstProxy = dst->asSurfaceProxy();
677 GrSurface* dstSurface = dstProxy->priv().peekSurface();
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400678
Brian Osmand2ca59a2017-04-13 14:03:57 -0400679 // 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 -0400680 const bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
Brian Salomonc320b152018-02-20 14:05:36 -0500681
682 if (!valid_pixel_conversion(srcColorType, dstProxy->config(), premul)) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400683 return false;
684 }
685
Brian Osman409e74f2017-04-17 11:48:28 -0400686 // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data,
687 // without any color spaces attached, and the caller wants us to premul.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400688 bool useConfigConversionEffect =
Brian Salomonc320b152018-02-20 14:05:36 -0500689 premul && pm_upm_must_round_trip(srcColorType, srcColorSpace) &&
Brian Salomonf3569f02017-10-24 12:52:33 -0400690 pm_upm_must_round_trip(dstProxy->config(), dst->colorSpaceInfo().colorSpace());
Brian Osman409e74f2017-04-17 11:48:28 -0400691
692 // Are we going to try to premul as part of a draw? For the non-legacy case, we always allow
693 // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly.
694 bool premulOnGpu = premul &&
695 (!useConfigConversionEffect || fContext->validPMUPMConversionExists());
bsalomon81beccc2014-10-13 12:32:55 -0700696
bsalomone8d21e82015-07-16 08:23:13 -0700697 // 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 -0700698 // necessary and because GrGpu::getWritePixelsInfo requires it.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400699 if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(),
Brian Salomonc320b152018-02-20 14:05:36 -0500700 GrColorTypeBytesPerPixel(srcColorType), &left, &top,
701 &width, &height, &buffer, &rowBytes)) {
bsalomone8d21e82015-07-16 08:23:13 -0700702 return false;
703 }
704
Brian Osman409e74f2017-04-17 11:48:28 -0400705 GrGpu::DrawPreference drawPreference = premulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference
706 : GrGpu::kNoDraw_DrawPreference;
bsalomonf0674512015-07-28 13:26:15 -0700707 GrGpu::WritePixelTempDrawInfo tempDrawInfo;
Brian Salomon9b009bb2018-02-14 13:53:55 -0500708 GrSRGBConversion srgbConversion = determine_write_pixels_srgb_conversion(
Brian Salomonc320b152018-02-20 14:05:36 -0500709 srcColorType, srcColorSpace, GrPixelConfigIsSRGBEncoded(dstProxy->config()),
710 dst->colorSpaceInfo().colorSpace(), *fContext->caps());
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400711 if (!fContext->fGpu->getWritePixelsInfo(dstSurface, dstProxy->origin(), width, height,
Brian Salomonc320b152018-02-20 14:05:36 -0500712 srcColorType, srgbConversion, &drawPreference,
Brian Salomon9b009bb2018-02-14 13:53:55 -0500713 &tempDrawInfo)) {
bsalomonf0674512015-07-28 13:26:15 -0700714 return false;
715 }
716
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400717 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && dstSurface->surfacePriv().hasPendingIO()) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400718 this->flush(nullptr); // MDB TODO: tighten this
bsalomonf0674512015-07-28 13:26:15 -0700719 }
720
Robert Phillips2f493142017-03-02 18:18:38 -0500721 sk_sp<GrTextureProxy> tempProxy;
bsalomonf0674512015-07-28 13:26:15 -0700722 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500723 tempProxy = this->proxyProvider()->createProxy(tempDrawInfo.fTempSurfaceDesc,
724 SkBackingFit::kApprox,
725 SkBudgeted::kYes);
Robert Phillips2f493142017-03-02 18:18:38 -0500726 if (!tempProxy && GrGpu::kRequireDraw_DrawPreference == drawPreference) {
bsalomonf0674512015-07-28 13:26:15 -0700727 return false;
728 }
729 }
730
731 // temp buffer for doing sw premul conversion, if needed.
732 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
Brian Osman409e74f2017-04-17 11:48:28 -0400733 // We need to do sw premul if we were unable to create a RT for drawing, or if we can't do the
734 // premul on the GPU
735 if (premul && (!tempProxy || !premulOnGpu)) {
736 size_t tmpRowBytes = 4 * width;
737 tmpPixels.reset(width * height);
Brian Salomonc320b152018-02-20 14:05:36 -0500738 if (!sw_convert_to_premul(srcColorType, width, height, rowBytes, buffer, tmpRowBytes,
Brian Osman409e74f2017-04-17 11:48:28 -0400739 tmpPixels.get())) {
740 return false;
bsalomonf0674512015-07-28 13:26:15 -0700741 }
Brian Osman409e74f2017-04-17 11:48:28 -0400742 rowBytes = tmpRowBytes;
743 buffer = tmpPixels.get();
bsalomonf0674512015-07-28 13:26:15 -0700744 }
Brian Osman409e74f2017-04-17 11:48:28 -0400745
746 if (tempProxy) {
Brian Osman2240be92017-10-18 13:15:13 -0400747 auto fp = GrSimpleTextureEffect::Make(tempProxy, SkMatrix::I());
Brian Osman409e74f2017-04-17 11:48:28 -0400748 if (premulOnGpu) {
749 fp = fContext->createUPMToPMEffect(std::move(fp), useConfigConversionEffect);
bsalomon81beccc2014-10-13 12:32:55 -0700750 }
Brian Osman409e74f2017-04-17 11:48:28 -0400751 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400752 if (!fp) {
753 return false;
754 }
Brian Osman409e74f2017-04-17 11:48:28 -0400755
Robert Phillips6be756b2018-01-16 15:07:54 -0500756 if (!tempProxy->instantiate(this->resourceProvider())) {
Brian Osman409e74f2017-04-17 11:48:28 -0400757 return false;
758 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400759 GrTexture* texture = tempProxy->priv().peekTexture();
Robert Phillips7bbbf622017-10-17 07:36:59 -0400760
761 if (tempProxy->priv().hasPendingIO()) {
762 this->flush(tempProxy.get());
763 }
764
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400765 if (!fContext->fGpu->writePixels(texture, tempProxy->origin(), 0, 0, width, height,
Brian Salomonc320b152018-02-20 14:05:36 -0500766 tempDrawInfo.fWriteColorType, buffer, rowBytes)) {
Brian Osman409e74f2017-04-17 11:48:28 -0400767 return false;
768 }
Robert Phillips7bbbf622017-10-17 07:36:59 -0400769 tempProxy = nullptr;
770
Brian Osman409e74f2017-04-17 11:48:28 -0400771 SkMatrix matrix;
772 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400773 GrRenderTargetContext* renderTargetContext = dst->asRenderTargetContext();
Brian Osman409e74f2017-04-17 11:48:28 -0400774 if (!renderTargetContext) {
775 return false;
776 }
777 GrPaint paint;
778 paint.addColorFragmentProcessor(std::move(fp));
779 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonf3569f02017-10-24 12:52:33 -0400780 paint.setAllowSRGBInputs(dst->colorSpaceInfo().isGammaCorrect() ||
781 GrPixelConfigIsSRGB(dst->colorSpaceInfo().config()));
Brian Osman409e74f2017-04-17 11:48:28 -0400782 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
783 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, matrix, rect,
784 nullptr);
785
786 if (kFlushWrites_PixelOp & pixelOpsFlags) {
787 this->flushSurfaceWrites(renderTargetContext->asRenderTargetProxy());
788 }
789 } else {
Brian Salomonc320b152018-02-20 14:05:36 -0500790 return fContext->fGpu->writePixels(dstSurface, dstProxy->origin(), left, top, width, height,
791 srcColorType, buffer, rowBytes);
bsalomon81beccc2014-10-13 12:32:55 -0700792 }
bsalomon81beccc2014-10-13 12:32:55 -0700793 return true;
794}
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000795
Brian Salomonc320b152018-02-20 14:05:36 -0500796bool GrContextPriv::readSurfacePixels(GrSurfaceContext* src, int left, int top, int width,
797 int height, GrColorType dstColorType,
798 SkColorSpace* dstColorSpace, void* buffer, size_t rowBytes,
799 uint32_t flags) {
Brian Osmanb62ea222016-12-22 11:12:16 -0500800 // TODO: Color space conversion
801
Robert Phillipse78b7252017-04-06 07:59:41 -0400802 ASSERT_SINGLE_OWNER_PRIV
803 RETURN_FALSE_IF_ABANDONED_PRIV
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400804 SkASSERT(src);
805 ASSERT_OWNED_PROXY_PRIV(src->asSurfaceProxy());
Brian Salomondcbb9d92017-07-19 10:53:20 -0400806 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "readSurfacePixels", fContext);
bsalomon32ab2602015-09-09 18:57:49 -0700807
Robert Phillipse78b7252017-04-06 07:59:41 -0400808 // MDB TODO: delay this instantiation until later in the method
Robert Phillips6be756b2018-01-16 15:07:54 -0500809 if (!src->asSurfaceProxy()->instantiate(this->resourceProvider())) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400810 return false;
811 }
812
Robert Phillips16d8ec62017-07-27 16:16:25 -0400813 GrSurfaceProxy* srcProxy = src->asSurfaceProxy();
814 GrSurface* srcSurface = srcProxy->priv().peekSurface();
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400815
Brian Osmand2ca59a2017-04-13 14:03:57 -0400816 // The src is premul but the dst is unpremul -> unpremul the src after or as part of the read
817 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
Brian Salomonc320b152018-02-20 14:05:36 -0500818
819 if (!valid_pixel_conversion(dstColorType, srcProxy->config(), unpremul)) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400820 return false;
821 }
822
Brian Osman409e74f2017-04-17 11:48:28 -0400823 // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data,
824 // without any color spaces attached, and the caller wants us to unpremul.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400825 bool useConfigConversionEffect =
Brian Salomonf3569f02017-10-24 12:52:33 -0400826 unpremul &&
827 pm_upm_must_round_trip(srcProxy->config(), src->colorSpaceInfo().colorSpace()) &&
Brian Salomonc320b152018-02-20 14:05:36 -0500828 pm_upm_must_round_trip(dstColorType, dstColorSpace);
Brian Osman409e74f2017-04-17 11:48:28 -0400829
830 // Are we going to try to unpremul as part of a draw? For the non-legacy case, we always allow
831 // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly.
832 bool unpremulOnGpu = unpremul &&
833 (!useConfigConversionEffect || fContext->validPMUPMConversionExists());
bsalomon6c6f6582015-09-10 08:12:46 -0700834
bsalomone8d21e82015-07-16 08:23:13 -0700835 // Adjust the params so that if we wind up using an intermediate surface we've already done
836 // all the trimming and the temporary can be the min size required.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400837 if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(),
Brian Salomonc320b152018-02-20 14:05:36 -0500838 GrColorTypeBytesPerPixel(dstColorType), &left, &top,
839 &width, &height, &buffer, &rowBytes)) {
bsalomone8d21e82015-07-16 08:23:13 -0700840 return false;
841 }
842
Brian Osman409e74f2017-04-17 11:48:28 -0400843 GrGpu::DrawPreference drawPreference = unpremulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference
844 : GrGpu::kNoDraw_DrawPreference;
bsalomon39826022015-07-23 08:07:21 -0700845 GrGpu::ReadPixelTempDrawInfo tempDrawInfo;
Brian Salomon9b009bb2018-02-14 13:53:55 -0500846 GrSRGBConversion srgbConversion = determine_read_pixels_srgb_conversion(
847 GrPixelConfigIsSRGBEncoded(srcProxy->config()), src->colorSpaceInfo().colorSpace(),
Brian Salomonc320b152018-02-20 14:05:36 -0500848 dstColorType, dstColorSpace, *fContext->caps());
849
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400850 if (!fContext->fGpu->getReadPixelsInfo(srcSurface, srcProxy->origin(), width, height, rowBytes,
Brian Salomonc320b152018-02-20 14:05:36 -0500851 dstColorType, srgbConversion, &drawPreference,
Brian Salomon9b009bb2018-02-14 13:53:55 -0500852 &tempDrawInfo)) {
bsalomon39826022015-07-23 08:07:21 -0700853 return false;
854 }
bsalomon191bcc02014-11-14 11:31:13 -0800855
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400856 if (!(kDontFlush_PixelOpsFlag & flags) && srcSurface->surfacePriv().hasPendingWrite()) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400857 this->flush(nullptr); // MDB TODO: tighten this
858 }
859
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400860 sk_sp<GrSurfaceProxy> proxyToRead = src->asSurfaceProxyRef();
bsalomon39826022015-07-23 08:07:21 -0700861 bool didTempDraw = false;
862 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomonb117ff12016-07-19 07:24:40 -0700863 if (SkBackingFit::kExact == tempDrawInfo.fTempSurfaceFit) {
bsalomon39826022015-07-23 08:07:21 -0700864 // We only respect this when the entire src is being read. Otherwise we can trigger too
865 // many odd ball texture sizes and trash the cache.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400866 if (width != srcSurface->width() || height != srcSurface->height()) {
bsalomonb117ff12016-07-19 07:24:40 -0700867 tempDrawInfo.fTempSurfaceFit= SkBackingFit::kApprox;
bsalomon39826022015-07-23 08:07:21 -0700868 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000869 }
brianosmandfe4f2e2016-07-21 13:28:36 -0700870 // TODO: Need to decide the semantics of this function for color spaces. Do we support
871 // conversion to a passed-in color space? For now, specifying nullptr means that this
Brian Salomon366093f2018-02-13 09:25:22 -0500872 // path will do no conversion, so it will match the behavior of the non-draw path. For
873 // now we simply infer an sRGB color space if the config is sRGB in order to avoid an
874 // illegal combination.
875 sk_sp<SkColorSpace> colorSpace;
876 if (GrPixelConfigIsSRGB(tempDrawInfo.fTempSurfaceDesc.fConfig)) {
877 colorSpace = SkColorSpace::MakeSRGB();
878 }
879 sk_sp<GrRenderTargetContext> tempRTC =
880 fContext->makeDeferredRenderTargetContext(tempDrawInfo.fTempSurfaceFit,
881 tempDrawInfo.fTempSurfaceDesc.fWidth,
882 tempDrawInfo.fTempSurfaceDesc.fHeight,
883 tempDrawInfo.fTempSurfaceDesc.fConfig,
884 std::move(colorSpace),
885 tempDrawInfo.fTempSurfaceDesc.fSampleCnt,
886 GrMipMapped::kNo,
887 tempDrawInfo.fTempSurfaceDesc.fOrigin);
Brian Osman693a5402016-10-27 15:13:22 -0400888 if (tempRTC) {
Greg Danielf44cb482018-02-27 14:26:32 -0500889 // Adding discard to appease vulkan validation warning about loading uninitialized data
890 // on draw
891 tempRTC->discard();
Robert Phillips67c18d62017-01-20 12:44:06 -0500892 SkMatrix textureMatrix = SkMatrix::MakeTrans(SkIntToScalar(left), SkIntToScalar(top));
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400893 sk_sp<GrTextureProxy> proxy = src->asTextureProxyRef();
Brian Osman2240be92017-10-18 13:15:13 -0400894 auto fp = GrSimpleTextureEffect::Make(std::move(proxy), textureMatrix);
Brian Osman409e74f2017-04-17 11:48:28 -0400895 if (unpremulOnGpu) {
896 fp = fContext->createPMToUPMEffect(std::move(fp), useConfigConversionEffect);
897 // We no longer need to do this on CPU after the read back.
898 unpremul = false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000899 }
Brian Osman409e74f2017-04-17 11:48:28 -0400900 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400901 if (!fp) {
902 return false;
903 }
Brian Osman60cd57e2017-04-06 10:19:06 -0400904
Brian Osman409e74f2017-04-17 11:48:28 -0400905 GrPaint paint;
906 paint.addColorFragmentProcessor(std::move(fp));
907 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
908 paint.setAllowSRGBInputs(true);
909 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
910 tempRTC->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect,
911 nullptr);
912 proxyToRead = tempRTC->asTextureProxyRef();
913 left = 0;
914 top = 0;
915 didTempDraw = true;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000916 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000917 }
joshualitt5c55fef2014-10-31 14:04:35 -0700918
Robert Phillipse78b7252017-04-06 07:59:41 -0400919 if (!proxyToRead) {
920 return false;
921 }
922
bsalomon39826022015-07-23 08:07:21 -0700923 if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000924 return false;
925 }
Brian Salomonc320b152018-02-20 14:05:36 -0500926 GrColorType colorTypeToRead = dstColorType;
bsalomon39826022015-07-23 08:07:21 -0700927 if (didTempDraw) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400928 this->flushSurfaceWrites(proxyToRead.get());
Brian Salomonc320b152018-02-20 14:05:36 -0500929 colorTypeToRead = tempDrawInfo.fReadColorType;
bsalomon39826022015-07-23 08:07:21 -0700930 }
Robert Phillips09dfc472017-09-13 15:25:47 -0400931
Robert Phillips6be756b2018-01-16 15:07:54 -0500932 if (!proxyToRead->instantiate(this->resourceProvider())) {
Robert Phillips09dfc472017-09-13 15:25:47 -0400933 return false;
934 }
935
936 GrSurface* surfaceToRead = proxyToRead->priv().peekSurface();
937
Brian Salomonc320b152018-02-20 14:05:36 -0500938 if (!fContext->fGpu->readPixels(surfaceToRead, proxyToRead->origin(), left, top, width, height,
939 colorTypeToRead, buffer, rowBytes)) {
bsalomon39826022015-07-23 08:07:21 -0700940 return false;
941 }
942
943 // Perform umpremul conversion if we weren't able to perform it as a draw.
944 if (unpremul) {
Brian Salomonc320b152018-02-20 14:05:36 -0500945 SkColorType colorType = GrColorTypeToSkColorType(dstColorType);
946 if (kUnknown_SkColorType == colorType || 4 != SkColorTypeBytesPerPixel(colorType)) {
reed@google.com7111d462014-03-25 16:20:24 +0000947 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000948 }
reed@google.com7111d462014-03-25 16:20:24 +0000949
Matt Sarettc7b29082017-02-09 16:22:39 -0500950 for (int y = 0; y < height; y++) {
951 SkUnpremultiplyRow<false>((uint32_t*) buffer, (const uint32_t*) buffer, width);
952 buffer = SkTAddOffset<void>(buffer, rowBytes);
953 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000954 }
955 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000956}
957
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500958bool GrContextPriv::writeSurfacePixels2(GrSurfaceContext* dst, int left, int top, int width,
959 int height, GrColorType srcColorType,
960 SkColorSpace* srcColorSpace, const void* buffer,
961 size_t rowBytes, uint32_t pixelOpsFlags) {
962 ASSERT_SINGLE_OWNER_PRIV
963 RETURN_FALSE_IF_ABANDONED_PRIV
964 SkASSERT(dst);
965 SkASSERT(buffer);
966 ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy());
967 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "writeSurfacePixels2", fContext);
968
969 if (GrColorType::kUnknown == srcColorType) {
970 return false;
971 }
972
973 if (!dst->asSurfaceProxy()->instantiate(this->resourceProvider())) {
974 return false;
975 }
976
977 GrSurfaceProxy* dstProxy = dst->asSurfaceProxy();
978 GrSurface* dstSurface = dstProxy->priv().peekSurface();
979
980 if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(),
981 GrColorTypeBytesPerPixel(srcColorType), &left, &top,
982 &width, &height, &buffer, &rowBytes)) {
983 return false;
984 }
985
Brian Salomon3d86a192018-02-27 16:46:11 -0500986 if (!fContext->caps()->surfaceSupportsWritePixels(dstSurface)) {
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500987 GrSurfaceDesc desc;
988 desc.fConfig = dstProxy->config();
989 desc.fWidth = width;
990 desc.fHeight = height;
991 desc.fSampleCnt = 1;
992 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
993 auto tempProxy =
994 this->proxyProvider()->createProxy(desc, SkBackingFit::kApprox, SkBudgeted::kYes);
995 if (!tempProxy) {
996 return false;
997 }
998 auto tempCtx = this->drawingManager()->makeTextureContext(
999 tempProxy, dst->colorSpaceInfo().refColorSpace());
1000 if (!tempCtx) {
1001 return false;
1002 }
1003 if (!this->writeSurfacePixels2(tempCtx.get(), 0, 0, width, height, srcColorType,
1004 srcColorSpace, buffer, rowBytes, pixelOpsFlags)) {
1005 return false;
1006 }
Brian Salomon3d86a192018-02-27 16:46:11 -05001007 return dst->copy(tempProxy.get(), SkIRect::MakeWH(width, height), {left, top});
Brian Salomon5f33a8c2018-02-26 14:32:39 -05001008 }
1009
1010 // TODO: Make GrSurfaceContext know its alpha type and pass src buffer's alpha type.
1011 bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
1012 bool convert = premul;
1013
1014 if (!valid_pixel_conversion(srcColorType, dstProxy->config(), premul)) {
1015 return false;
1016 }
1017
1018 GrColorType allowedColorType =
1019 fContext->caps()->supportedWritePixelsColorType(dstProxy->config(), srcColorType);
1020 convert = convert || (srcColorType != allowedColorType);
1021
1022 if (!dst->colorSpaceInfo().colorSpace()) {
1023 // "Legacy" mode - no color space conversions.
1024 srcColorSpace = nullptr;
1025 }
1026 convert = convert || !SkColorSpace::Equals(srcColorSpace, dst->colorSpaceInfo().colorSpace());
1027
1028 std::unique_ptr<char[]> tempBuffer;
1029 if (convert) {
1030 auto srcSkColorType = GrColorTypeToSkColorType(srcColorType);
1031 auto dstSkColorType = GrColorTypeToSkColorType(allowedColorType);
1032 if (kUnknown_SkColorType == srcSkColorType || kUnknown_SkColorType == dstSkColorType) {
1033 return false;
1034 }
1035 auto srcAlphaType = premul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType;
1036 SkPixmap src(SkImageInfo::Make(width, height, srcSkColorType, srcAlphaType,
1037 sk_ref_sp(srcColorSpace)),
1038 buffer, rowBytes);
1039 auto tempSrcII = SkImageInfo::Make(width, height, dstSkColorType, kPremul_SkAlphaType,
1040 dst->colorSpaceInfo().refColorSpace());
1041 auto size = tempSrcII.computeMinByteSize();
1042 if (!size) {
1043 return false;
1044 }
1045 tempBuffer.reset(new char[size]);
1046 SkPixmap tempSrc(tempSrcII, tempBuffer.get(), tempSrcII.minRowBytes());
1047 if (!src.readPixels(tempSrc)) {
1048 return false;
1049 }
1050 srcColorType = allowedColorType;
1051 buffer = tempSrc.addr();
1052 rowBytes = tempSrc.rowBytes();
1053 if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
1054 std::unique_ptr<char[]> row(new char[rowBytes]);
1055 for (int y = 0; y < height / 2; ++y) {
1056 memcpy(row.get(), tempSrc.addr(0, y), rowBytes);
1057 memcpy(tempSrc.writable_addr(0, y), tempSrc.addr(0, height - 1 - y), rowBytes);
1058 memcpy(tempSrc.writable_addr(0, height - 1 - y), row.get(), rowBytes);
1059 }
1060 top = dstProxy->height() - top - height;
1061 }
1062 } else if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
1063 size_t trimRowBytes = GrColorTypeBytesPerPixel(srcColorType) * width;
1064 tempBuffer.reset(new char[trimRowBytes * height]);
1065 char* dst = reinterpret_cast<char*>(tempBuffer.get()) + trimRowBytes * (height - 1);
1066 const char* src = reinterpret_cast<const char*>(buffer);
1067 for (int i = 0; i < height; ++i, src += rowBytes, dst -= trimRowBytes) {
1068 memcpy(dst, src, trimRowBytes);
1069 }
1070 buffer = tempBuffer.get();
1071 rowBytes = trimRowBytes;
1072 top = dstProxy->height() - top - height;
1073 }
1074
1075 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && dstSurface->surfacePriv().hasPendingIO()) {
1076 this->flush(nullptr); // MDB TODO: tighten this
1077 }
1078
1079 return this->getGpu()->writePixels(dstSurface, left, top, width, height, srcColorType, buffer,
1080 rowBytes);
1081}
1082
Robert Phillips7ee385e2017-03-30 08:02:11 -04001083void GrContextPriv::prepareSurfaceForExternalIO(GrSurfaceProxy* proxy) {
1084 ASSERT_SINGLE_OWNER_PRIV
1085 RETURN_IF_ABANDONED_PRIV
1086 SkASSERT(proxy);
1087 ASSERT_OWNED_PROXY_PRIV(proxy);
Greg Daniel51316782017-08-02 15:10:09 +00001088 fContext->fDrawingManager->prepareSurfaceForExternalIO(proxy, 0, nullptr);
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001089}
1090
Robert Phillips7ee385e2017-03-30 08:02:11 -04001091void GrContextPriv::flushSurfaceWrites(GrSurfaceProxy* proxy) {
1092 ASSERT_SINGLE_OWNER_PRIV
1093 RETURN_IF_ABANDONED_PRIV
1094 SkASSERT(proxy);
1095 ASSERT_OWNED_PROXY_PRIV(proxy);
1096 if (proxy->priv().hasPendingWrite()) {
1097 this->flush(proxy);
bsalomonf80bfed2014-10-07 05:56:02 -07001098 }
1099}
1100
Robert Phillips7ee385e2017-03-30 08:02:11 -04001101void GrContextPriv::flushSurfaceIO(GrSurfaceProxy* proxy) {
1102 ASSERT_SINGLE_OWNER_PRIV
1103 RETURN_IF_ABANDONED_PRIV
1104 SkASSERT(proxy);
1105 ASSERT_OWNED_PROXY_PRIV(proxy);
1106 if (proxy->priv().hasPendingIO()) {
1107 this->flush(proxy);
ajuma95243eb2016-08-24 08:19:02 -07001108 }
1109}
1110
bsalomon@google.com27847de2011-02-22 20:59:41 +00001111////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +00001112
Robert Phillips2c862492017-01-18 10:08:39 -05001113sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy> proxy,
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -05001114 sk_sp<SkColorSpace> colorSpace,
1115 const SkSurfaceProps* props) {
Brian Osman45580d32016-11-23 09:37:01 -05001116 ASSERT_SINGLE_OWNER_PRIV
1117
Brian Salomon366093f2018-02-13 09:25:22 -05001118 // sRGB pixel configs may only be used with near-sRGB gamma color spaces.
1119 if (GrPixelConfigIsSRGB(proxy->config())) {
1120 if (!colorSpace || !colorSpace->gammaCloseToSRGB()) {
1121 return nullptr;
1122 }
1123 }
Brian Osman45580d32016-11-23 09:37:01 -05001124 if (proxy->asRenderTargetProxy()) {
Robert Phillips2c862492017-01-18 10:08:39 -05001125 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -05001126 std::move(colorSpace), props);
Brian Osman45580d32016-11-23 09:37:01 -05001127 } else {
1128 SkASSERT(proxy->asTextureProxy());
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -05001129 SkASSERT(!props);
Robert Phillips2c862492017-01-18 10:08:39 -05001130 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Brian Osman45580d32016-11-23 09:37:01 -05001131 }
1132}
1133
Robert Phillipse2f7d182016-12-15 09:23:05 -05001134sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(const GrSurfaceDesc& dstDesc,
Greg Daniel65c7f662017-10-30 13:39:09 -04001135 GrMipMapped mipMapped,
Robert Phillipse2f7d182016-12-15 09:23:05 -05001136 SkBackingFit fit,
Brian Salomon366093f2018-02-13 09:25:22 -05001137 SkBudgeted isDstBudgeted,
1138 sk_sp<SkColorSpace> colorSpace,
1139 const SkSurfaceProps* props) {
Greg Daniel65c7f662017-10-30 13:39:09 -04001140 sk_sp<GrTextureProxy> proxy;
1141 if (GrMipMapped::kNo == mipMapped) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001142 proxy = this->proxyProvider()->createProxy(dstDesc, fit, isDstBudgeted);
Greg Daniel65c7f662017-10-30 13:39:09 -04001143 } else {
1144 SkASSERT(SkBackingFit::kExact == fit);
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001145 proxy = this->proxyProvider()->createMipMapProxy(dstDesc, isDstBudgeted);
Greg Daniel65c7f662017-10-30 13:39:09 -04001146 }
Robert Phillips77b3f322017-01-31 18:24:12 -05001147 if (!proxy) {
1148 return nullptr;
1149 }
Robert Phillipse2f7d182016-12-15 09:23:05 -05001150
Brian Salomon366093f2018-02-13 09:25:22 -05001151 return this->makeWrappedSurfaceContext(std::move(proxy), std::move(colorSpace), props);
Robert Phillipse2f7d182016-12-15 09:23:05 -05001152}
1153
Brian Salomond17f6582017-07-19 18:28:58 -04001154sk_sp<GrTextureContext> GrContextPriv::makeBackendTextureContext(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +00001155 GrSurfaceOrigin origin,
Brian Osmanc1e37052017-03-09 14:19:20 -05001156 sk_sp<SkColorSpace> colorSpace) {
Robert Phillips26caf892017-01-27 10:58:31 -05001157 ASSERT_SINGLE_OWNER_PRIV
1158
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001159 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->createWrappedTextureProxy(tex, origin);
Robert Phillips77b3f322017-01-31 18:24:12 -05001160 if (!proxy) {
1161 return nullptr;
1162 }
Robert Phillips26caf892017-01-27 10:58:31 -05001163
Brian Salomond17f6582017-07-19 18:28:58 -04001164 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Robert Phillips26caf892017-01-27 10:58:31 -05001165}
1166
Brian Osman11052242016-10-27 14:47:55 -04001167sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +00001168 const GrBackendTexture& tex,
1169 GrSurfaceOrigin origin,
1170 int sampleCnt,
Brian Osman11052242016-10-27 14:47:55 -04001171 sk_sp<SkColorSpace> colorSpace,
Brian Osmanc1e37052017-03-09 14:19:20 -05001172 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -07001173 ASSERT_SINGLE_OWNER_PRIV
Brian Salomonbdecacf2018-02-02 20:32:49 -05001174 SkASSERT(sampleCnt > 0);
robertphillips4fd74ae2016-08-03 14:26:53 -07001175
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001176 sk_sp<GrTextureProxy> proxy(this->proxyProvider()->createWrappedTextureProxy(tex, origin,
1177 sampleCnt));
Robert Phillips77b3f322017-01-31 18:24:12 -05001178 if (!proxy) {
1179 return nullptr;
1180 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001181
Robert Phillips37430132016-11-09 06:50:43 -05001182 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Brian Osman11052242016-10-27 14:47:55 -04001183 std::move(colorSpace), props);
robertphillips4fd74ae2016-08-03 14:26:53 -07001184}
1185
Brian Osman11052242016-10-27 14:47:55 -04001186sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext(
Greg Danielbcf612b2017-05-01 13:50:58 +00001187 const GrBackendRenderTarget& backendRT,
1188 GrSurfaceOrigin origin,
robertphillips4fd74ae2016-08-03 14:26:53 -07001189 sk_sp<SkColorSpace> colorSpace,
1190 const SkSurfaceProps* surfaceProps) {
1191 ASSERT_SINGLE_OWNER_PRIV
1192
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001193 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->createWrappedRenderTargetProxy(backendRT,
1194 origin);
Robert Phillips77b3f322017-01-31 18:24:12 -05001195 if (!proxy) {
1196 return nullptr;
1197 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001198
Robert Phillips37430132016-11-09 06:50:43 -05001199 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001200 std::move(colorSpace),
Brian Osman11052242016-10-27 14:47:55 -04001201 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -07001202}
1203
Brian Osman11052242016-10-27 14:47:55 -04001204sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +00001205 const GrBackendTexture& tex,
1206 GrSurfaceOrigin origin,
1207 int sampleCnt,
robertphillips4fd74ae2016-08-03 14:26:53 -07001208 sk_sp<SkColorSpace> colorSpace,
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001209 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -07001210 ASSERT_SINGLE_OWNER_PRIV
Brian Salomonbdecacf2018-02-02 20:32:49 -05001211 SkASSERT(sampleCnt > 0);
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001212 sk_sp<GrSurfaceProxy> proxy(this->proxyProvider()->createWrappedRenderTargetProxy(tex, origin,
1213 sampleCnt));
Robert Phillips77b3f322017-01-31 18:24:12 -05001214 if (!proxy) {
1215 return nullptr;
1216 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001217
Robert Phillips37430132016-11-09 06:50:43 -05001218 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001219 std::move(colorSpace),
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001220 props);
robertphillips77a2e522015-10-17 07:43:27 -07001221}
1222
Chris Daltonfe199b72017-05-05 11:26:15 -04001223void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
1224 fContext->fDrawingManager->addOnFlushCallbackObject(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -04001225}
1226
Robert Phillips62000362018-02-01 09:10:04 -05001227void GrContextPriv::moveOpListsToDDL(SkDeferredDisplayList* ddl) {
1228 fContext->fDrawingManager->moveOpListsToDDL(ddl);
1229}
1230
1231void GrContextPriv::copyOpListsFromDDL(const SkDeferredDisplayList* ddl,
1232 GrRenderTargetProxy* newDest) {
1233 fContext->fDrawingManager->copyOpListsFromDDL(ddl, newDest);
1234}
1235
robertphillips48fde9c2016-09-06 05:20:20 -07001236static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) {
Brian Osman78f20e02017-01-12 10:28:01 -05001237 switch (config) {
1238 case kAlpha_8_GrPixelConfig:
1239 case kRGB_565_GrPixelConfig:
1240 case kRGBA_4444_GrPixelConfig:
1241 case kBGRA_8888_GrPixelConfig:
1242 return kRGBA_8888_GrPixelConfig;
1243 case kSBGRA_8888_GrPixelConfig:
1244 return kSRGBA_8888_GrPixelConfig;
1245 case kAlpha_half_GrPixelConfig:
1246 return kRGBA_half_GrPixelConfig;
1247 default:
1248 return kUnknown_GrPixelConfig;
1249 }
robertphillips48fde9c2016-09-06 05:20:20 -07001250}
1251
robertphillipsd728f0c2016-11-21 11:05:03 -08001252sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContextWithFallback(
1253 SkBackingFit fit,
1254 int width, int height,
1255 GrPixelConfig config,
1256 sk_sp<SkColorSpace> colorSpace,
1257 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -04001258 GrMipMapped mipMapped,
robertphillipsd728f0c2016-11-21 11:05:03 -08001259 GrSurfaceOrigin origin,
1260 const SkSurfaceProps* surfaceProps,
1261 SkBudgeted budgeted) {
Brian Salomonbdecacf2018-02-02 20:32:49 -05001262 SkASSERT(sampleCnt > 0);
1263 if (0 == this->caps()->getRenderTargetSampleCount(sampleCnt, config)) {
robertphillipsd728f0c2016-11-21 11:05:03 -08001264 config = GrPixelConfigFallback(config);
1265 }
1266
1267 return this->makeDeferredRenderTargetContext(fit, width, height, config, std::move(colorSpace),
Greg Daniel45d63032017-10-30 13:41:26 -04001268 sampleCnt, mipMapped, origin, surfaceProps,
Greg Daniele1da1d92017-10-06 15:59:27 -04001269 budgeted);
robertphillipsd728f0c2016-11-21 11:05:03 -08001270}
1271
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001272sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContext(
1273 SkBackingFit fit,
1274 int width, int height,
1275 GrPixelConfig config,
1276 sk_sp<SkColorSpace> colorSpace,
1277 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -04001278 GrMipMapped mipMapped,
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001279 GrSurfaceOrigin origin,
1280 const SkSurfaceProps* surfaceProps,
1281 SkBudgeted budgeted) {
Brian Salomonbdecacf2018-02-02 20:32:49 -05001282 SkASSERT(sampleCnt > 0);
Brian Salomon79e4d1b2017-07-13 11:17:11 -04001283 if (this->abandoned()) {
1284 return nullptr;
1285 }
1286
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001287 GrSurfaceDesc desc;
1288 desc.fFlags = kRenderTarget_GrSurfaceFlag;
1289 desc.fOrigin = origin;
1290 desc.fWidth = width;
1291 desc.fHeight = height;
1292 desc.fConfig = config;
1293 desc.fSampleCnt = sampleCnt;
1294
Greg Daniele1da1d92017-10-06 15:59:27 -04001295 sk_sp<GrTextureProxy> rtp;
Greg Daniel45d63032017-10-30 13:41:26 -04001296 if (GrMipMapped::kNo == mipMapped) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001297 rtp = fProxyProvider->createProxy(desc, fit, budgeted);
Greg Daniele1da1d92017-10-06 15:59:27 -04001298 } else {
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001299 rtp = fProxyProvider->createMipMapProxy(desc, budgeted);
Greg Daniele1da1d92017-10-06 15:59:27 -04001300 }
Robert Phillips08c5ec72017-01-30 12:26:47 -05001301 if (!rtp) {
1302 return nullptr;
1303 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001304
Robert Phillips1119dc32017-04-11 12:54:57 -04001305 sk_sp<GrRenderTargetContext> renderTargetContext(
1306 fDrawingManager->makeRenderTargetContext(std::move(rtp),
1307 std::move(colorSpace),
1308 surfaceProps));
Robert Phillips1119dc32017-04-11 12:54:57 -04001309 if (!renderTargetContext) {
1310 return nullptr;
1311 }
1312
1313 renderTargetContext->discard();
1314
1315 return renderTargetContext;
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001316}
1317
joshualitt1de610a2016-01-06 08:26:09 -08001318bool GrContext::abandoned() const {
1319 ASSERT_SINGLE_OWNER
robertphillips7761d612016-05-16 09:14:53 -07001320 return fDrawingManager->wasAbandoned();
robertphillips77a2e522015-10-17 07:43:27 -07001321}
1322
Brian Salomonaff329b2017-08-11 09:40:37 -04001323std::unique_ptr<GrFragmentProcessor> GrContext::createPMToUPMEffect(
1324 std::unique_ptr<GrFragmentProcessor> fp, bool useConfigConversionEffect) {
Robert Phillips757914d2017-01-25 15:48:30 -05001325 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -04001326 // We have specialized effects that guarantee round-trip conversion for some formats
1327 if (useConfigConversionEffect) {
1328 // We should have already called this->validPMUPMConversionExists() in this case
1329 SkASSERT(fDidTestPMConversions);
1330 // ...and it should have succeeded
1331 SkASSERT(this->validPMUPMConversionExists());
1332
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001333 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul);
Brian Osman2d2da4f2017-04-12 17:07:22 -04001334 } else {
1335 // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and
1336 // explicitly round the results. Just do the obvious, naive thing in the shader.
1337 return GrFragmentProcessor::UnpremulOutput(std::move(fp));
Robert Phillips757914d2017-01-25 15:48:30 -05001338 }
1339}
1340
Brian Salomonaff329b2017-08-11 09:40:37 -04001341std::unique_ptr<GrFragmentProcessor> GrContext::createUPMToPMEffect(
1342 std::unique_ptr<GrFragmentProcessor> fp, bool useConfigConversionEffect) {
joshualitt1de610a2016-01-06 08:26:09 -08001343 ASSERT_SINGLE_OWNER
Brian Osman2d2da4f2017-04-12 17:07:22 -04001344 // We have specialized effects that guarantee round-trip conversion for these formats
Brian Osman409e74f2017-04-17 11:48:28 -04001345 if (useConfigConversionEffect) {
1346 // We should have already called this->validPMUPMConversionExists() in this case
1347 SkASSERT(fDidTestPMConversions);
1348 // ...and it should have succeeded
1349 SkASSERT(this->validPMUPMConversionExists());
1350
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001351 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
Brian Osman2d2da4f2017-04-12 17:07:22 -04001352 } else {
1353 // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and
1354 // explicitly round the results. Just do the obvious, naive thing in the shader.
1355 return GrFragmentProcessor::PremulOutput(std::move(fp));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001356 }
1357}
1358
Brian Osman409e74f2017-04-17 11:48:28 -04001359bool GrContext::validPMUPMConversionExists() {
joshualitt1de610a2016-01-06 08:26:09 -08001360 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -04001361 if (!fDidTestPMConversions) {
Brian Osman28804f32017-04-20 10:24:36 -04001362 fPMUPMConversionsRoundTrip = GrConfigConversionEffect::TestForPreservingPMConversions(this);
Brian Osman409e74f2017-04-17 11:48:28 -04001363 fDidTestPMConversions = true;
1364 }
1365
bsalomon636e8022015-07-29 06:08:46 -07001366 // The PM<->UPM tests fail or succeed together so we only need to check one.
Brian Osman28804f32017-04-20 10:24:36 -04001367 return fPMUPMConversionsRoundTrip;
bsalomon636e8022015-07-29 06:08:46 -07001368}
1369
bsalomon37f9a262015-02-02 13:00:10 -08001370//////////////////////////////////////////////////////////////////////////////
1371
Robert Phillipsfc711a22018-02-13 17:03:00 -05001372// DDL TODO: remove 'maxResources'
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001373void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -08001374 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001375 if (maxResources) {
1376 *maxResources = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -08001377 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001378 if (maxResourceBytes) {
1379 *maxResourceBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -08001380 }
1381}
1382
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001383void GrContext::setResourceCacheLimits(int maxResources, size_t maxResourceBytes) {
joshualitt1de610a2016-01-06 08:26:09 -08001384 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001385 fResourceCache->setLimits(maxResources, maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -08001386}
1387
ericrk0a5fa482015-09-15 14:16:10 -07001388//////////////////////////////////////////////////////////////////////////////
1389
1390void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -08001391 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -07001392 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
1393}
Brian Osman71a18892017-08-10 10:23:25 -04001394
1395//////////////////////////////////////////////////////////////////////////////
1396
1397SkString GrContext::dump() const {
1398 SkDynamicMemoryWStream stream;
1399 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
1400 writer.beginObject();
1401
1402 static const char* kBackendStr[] = {
1403 "Metal",
1404 "OpenGL",
1405 "Vulkan",
1406 "Mock",
1407 };
1408 GR_STATIC_ASSERT(0 == kMetal_GrBackend);
1409 GR_STATIC_ASSERT(1 == kOpenGL_GrBackend);
1410 GR_STATIC_ASSERT(2 == kVulkan_GrBackend);
1411 GR_STATIC_ASSERT(3 == kMock_GrBackend);
1412 writer.appendString("backend", kBackendStr[fBackend]);
1413
1414 writer.appendName("caps");
1415 fCaps->dumpJSON(&writer);
1416
1417 writer.appendName("gpu");
1418 fGpu->dumpJSON(&writer);
1419
1420 // Flush JSON to the memory stream
1421 writer.endObject();
1422 writer.flush();
1423
1424 // Null terminate the JSON data in the memory stream
1425 stream.write8(0);
1426
1427 // Allocate a string big enough to hold all the data, then copy out of the stream
1428 SkString result(stream.bytesWritten());
1429 stream.copyToAndReset(result.writable_str());
1430 return result;
1431}