blob: 0af319715bd2f70ae82d281b61d6ac9b84531129 [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"
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"
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -050028
Matt Sarett485c4992017-02-14 14:18:27 -050029#include "SkConvertPixels.h"
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -050030#include "SkDeferredDisplayList.h"
Brian Osman3b655982017-03-07 16:58:08 -050031#include "SkGr.h"
Mike Reed7fcfb622018-02-09 13:26:46 -050032#include "SkImageInfoPriv.h"
Brian Osman71a18892017-08-10 10:23:25 -040033#include "SkJSONWriter.h"
Brian Osman51279982017-08-23 10:12:00 -040034#include "SkMakeUnique.h"
35#include "SkTaskGroup.h"
Matt Sarettc7b29082017-02-09 16:22:39 -050036#include "SkUnPreMultiplyPriv.h"
joshualitt5478d422014-11-14 16:00:38 -080037#include "effects/GrConfigConversionEffect.h"
joshualitte8042922015-12-11 06:11:21 -080038#include "text/GrTextBlobCache.h"
joshualitt5478d422014-11-14 16:00:38 -080039
Greg Daniel02611d92017-07-25 10:05:01 -040040#include "gl/GrGLGpu.h"
41#include "mock/GrMockGpu.h"
Greg Danielb76a72a2017-07-13 15:07:54 -040042#ifdef SK_METAL
43#include "mtl/GrMtlTrampoline.h"
44#endif
Greg Daniel02611d92017-07-25 10:05:01 -040045#ifdef SK_VULKAN
46#include "vk/GrVkGpu.h"
47#endif
Greg Danielb76a72a2017-07-13 15:07:54 -040048
Robert Phillipse78b7252017-04-06 07:59:41 -040049#define ASSERT_OWNED_PROXY(P) \
50SkASSERT(!(P) || !((P)->priv().peekTexture()) || (P)->priv().peekTexture()->getContext() == this)
Robert Phillips7ee385e2017-03-30 08:02:11 -040051#define ASSERT_OWNED_PROXY_PRIV(P) \
52SkASSERT(!(P) || !((P)->priv().peekTexture()) || (P)->priv().peekTexture()->getContext() == fContext)
53
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000054#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
joshualitt1de610a2016-01-06 08:26:09 -080055#define ASSERT_SINGLE_OWNER \
56 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fSingleOwner);)
robertphillips4fd74ae2016-08-03 14:26:53 -070057#define ASSERT_SINGLE_OWNER_PRIV \
58 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fContext->fSingleOwner);)
robertphillips7761d612016-05-16 09:14:53 -070059#define RETURN_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return; }
Robert Phillips7ee385e2017-03-30 08:02:11 -040060#define RETURN_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return; }
robertphillips7761d612016-05-16 09:14:53 -070061#define RETURN_FALSE_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return false; }
Robert Phillipse78b7252017-04-06 07:59:41 -040062#define RETURN_FALSE_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return false; }
robertphillips7761d612016-05-16 09:14:53 -070063#define RETURN_NULL_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return nullptr; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000064
robertphillipsea461502015-05-26 11:38:03 -070065////////////////////////////////////////////////////////////////////////////////
66
Robert Phillips88260b52018-01-19 12:56:09 -050067class SK_API GrDirectContext : public GrContext {
68public:
69 GrDirectContext(GrBackend backend) : INHERITED(backend) { }
70
71protected:
72
73private:
74 typedef GrContext INHERITED;
75};
76
77class SK_API GrDDLContext : public GrContext {
78public:
79 GrDDLContext(GrContextThreadSafeProxy* proxy) : INHERITED(proxy) {}
80
81protected:
82
83private:
84 typedef GrContext INHERITED;
85};
86
bsalomon682c2692015-05-22 14:01:46 -070087GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext) {
88 GrContextOptions defaultOptions;
89 return Create(backend, backendContext, defaultOptions);
90}
bsalomonf28cff72015-05-22 12:25:41 -070091
bsalomon682c2692015-05-22 14:01:46 -070092GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
93 const GrContextOptions& options) {
bsalomon682c2692015-05-22 14:01:46 -070094
Robert Phillips88260b52018-01-19 12:56:09 -050095 sk_sp<GrContext> context(new GrDirectContext(backend));
Robert Phillipse42edcc2017-12-13 11:50:22 -050096
97 context->fGpu = GrGpu::Make(backend, backendContext, options, context.get());
98 if (!context->fGpu) {
halcanary96fcdcc2015-08-27 07:41:13 -070099 return nullptr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000100 }
Robert Phillipse42edcc2017-12-13 11:50:22 -0500101
102 if (!context->init(options)) {
103 return nullptr;
104 }
105
Brian Salomon91a3e522017-06-23 10:58:19 -0400106 return context.release();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000107}
108
Brian Salomon384fab42017-12-07 12:33:05 -0500109sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface) {
Greg Daniel02611d92017-07-25 10:05:01 -0400110 GrContextOptions defaultOptions;
Brian Salomon384fab42017-12-07 12:33:05 -0500111 return MakeGL(std::move(interface), defaultOptions);
Greg Daniel02611d92017-07-25 10:05:01 -0400112}
113
Brian Salomon384fab42017-12-07 12:33:05 -0500114sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface,
Greg Daniel02611d92017-07-25 10:05:01 -0400115 const GrContextOptions& options) {
Robert Phillips88260b52018-01-19 12:56:09 -0500116 sk_sp<GrContext> context(new GrDirectContext(kOpenGL_GrBackend));
Robert Phillipse42edcc2017-12-13 11:50:22 -0500117
Brian Salomon384fab42017-12-07 12:33:05 -0500118 context->fGpu = GrGLGpu::Make(std::move(interface), options, context.get());
Greg Daniel02611d92017-07-25 10:05:01 -0400119 if (!context->fGpu) {
120 return nullptr;
121 }
Greg Daniel02611d92017-07-25 10:05:01 -0400122 if (!context->init(options)) {
123 return nullptr;
124 }
125 return context;
126}
127
Brian Salomon990014d2017-12-07 16:23:39 -0500128sk_sp<GrContext> GrContext::MakeGL(const GrGLInterface* interface) {
129 return MakeGL(sk_ref_sp(interface));
130}
131
132sk_sp<GrContext> GrContext::MakeGL(const GrGLInterface* interface,
133 const GrContextOptions& options) {
134 return MakeGL(sk_ref_sp(interface), options);
135}
136
Greg Daniel02611d92017-07-25 10:05:01 -0400137sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions) {
138 GrContextOptions defaultOptions;
139 return MakeMock(mockOptions, defaultOptions);
140}
141
142sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions,
143 const GrContextOptions& options) {
Robert Phillips88260b52018-01-19 12:56:09 -0500144 sk_sp<GrContext> context(new GrDirectContext(kMock_GrBackend));
Robert Phillipse42edcc2017-12-13 11:50:22 -0500145
Brian Salomon384fab42017-12-07 12:33:05 -0500146 context->fGpu = GrMockGpu::Make(mockOptions, options, context.get());
Greg Daniel02611d92017-07-25 10:05:01 -0400147 if (!context->fGpu) {
148 return nullptr;
149 }
Greg Daniel02611d92017-07-25 10:05:01 -0400150 if (!context->init(options)) {
151 return nullptr;
152 }
153 return context;
154}
155
156#ifdef SK_VULKAN
Brian Salomon384fab42017-12-07 12:33:05 -0500157sk_sp<GrContext> GrContext::MakeVulkan(sk_sp<const GrVkBackendContext> backendContext) {
Greg Daniel02611d92017-07-25 10:05:01 -0400158 GrContextOptions defaultOptions;
Brian Salomon384fab42017-12-07 12:33:05 -0500159 return MakeVulkan(std::move(backendContext), defaultOptions);
Greg Daniel02611d92017-07-25 10:05:01 -0400160}
161
Brian Salomon384fab42017-12-07 12:33:05 -0500162sk_sp<GrContext> GrContext::MakeVulkan(sk_sp<const GrVkBackendContext> backendContext,
Greg Daniel02611d92017-07-25 10:05:01 -0400163 const GrContextOptions& options) {
Robert Phillips88260b52018-01-19 12:56:09 -0500164 sk_sp<GrContext> context(new GrDirectContext(kVulkan_GrBackend));
Robert Phillipse42edcc2017-12-13 11:50:22 -0500165
Brian Salomon384fab42017-12-07 12:33:05 -0500166 context->fGpu = GrVkGpu::Make(std::move(backendContext), options, context.get());
Greg Daniel02611d92017-07-25 10:05:01 -0400167 if (!context->fGpu) {
168 return nullptr;
169 }
Greg Daniel02611d92017-07-25 10:05:01 -0400170 if (!context->init(options)) {
171 return nullptr;
172 }
173 return context;
174}
175#endif
176
Greg Danielb76a72a2017-07-13 15:07:54 -0400177#ifdef SK_METAL
Greg Daniel02611d92017-07-25 10:05:01 -0400178sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue) {
179 GrContextOptions defaultOptions;
180 return MakeMetal(device, queue, defaultOptions);
181}
182
Greg Danielb76a72a2017-07-13 15:07:54 -0400183sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) {
Robert Phillipse42edcc2017-12-13 11:50:22 -0500184 sk_sp<GrContext> context(new GrContext(kMetal_GrBackend));
185
Brian Salomon384fab42017-12-07 12:33:05 -0500186 context->fGpu = GrMtlTrampoline::MakeGpu(context.get(), options, device, queue);
Greg Danielb76a72a2017-07-13 15:07:54 -0400187 if (!context->fGpu) {
188 return nullptr;
189 }
Greg Danielb76a72a2017-07-13 15:07:54 -0400190 if (!context->init(options)) {
191 return nullptr;
192 }
193 return context;
194}
195#endif
196
joshualitt0acd0d32015-05-07 08:23:19 -0700197static int32_t gNextID = 1;
198static int32_t next_id() {
199 int32_t id;
200 do {
201 id = sk_atomic_inc(&gNextID);
202 } while (id == SK_InvalidGenID);
203 return id;
204}
205
Robert Phillipse42edcc2017-12-13 11:50:22 -0500206sk_sp<GrContext> GrContextPriv::MakeDDL(GrContextThreadSafeProxy* proxy) {
Robert Phillips88260b52018-01-19 12:56:09 -0500207 sk_sp<GrContext> context(new GrDDLContext(proxy));
Robert Phillipse42edcc2017-12-13 11:50:22 -0500208
Robert Phillips88260b52018-01-19 12:56:09 -0500209 // Note: we aren't creating a Gpu here. This causes the resource provider & cache to
210 // also not be created
Robert Phillipse42edcc2017-12-13 11:50:22 -0500211 if (!context->init(proxy->fOptions)) {
212 return nullptr;
213 }
214 return context;
215}
216
217GrContext::GrContext(GrBackend backend)
218 : fUniqueID(next_id())
219 , fBackend(backend) {
halcanary96fcdcc2015-08-27 07:41:13 -0700220 fResourceCache = nullptr;
221 fResourceProvider = nullptr;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500222 fProxyProvider = nullptr;
Brian Salomonf856fd12016-12-16 14:24:34 -0500223 fAtlasGlyphCache = 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 Phillipse42edcc2017-12-13 11:50:22 -0500233 fAtlasGlyphCache = nullptr;
Greg Danielb76a72a2017-07-13 15:07:54 -0400234}
joshualitt1de610a2016-01-06 08:26:09 -0800235
Greg Danielb76a72a2017-07-13 15:07:54 -0400236bool GrContext::init(const GrContextOptions& options) {
237 ASSERT_SINGLE_OWNER
Robert Phillips88260b52018-01-19 12:56:09 -0500238
239 if (fGpu) {
240 fCaps = fGpu->refCaps();
241 fResourceCache = new GrResourceCache(fCaps.get(), fUniqueID);
Robert Phillips4150eea2018-02-07 17:08:21 -0500242 fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, &fSingleOwner,
243 options.fExplicitlyAllocateGPUResources);
Robert Phillips88260b52018-01-19 12:56:09 -0500244 }
245
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500246 fProxyProvider = new GrProxyProvider(fResourceProvider, fResourceCache, fCaps, &fSingleOwner);
Robert Phillips88260b52018-01-19 12:56:09 -0500247
248 if (fResourceCache) {
249 fResourceCache->setProxyProvider(fProxyProvider);
250 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500251
Robert Phillipse42edcc2017-12-13 11:50:22 -0500252 // DDL TODO: we need to think through how the task group & persistent cache
253 // get passed on to/shared between all the DDLRecorders created with this context.
254 fThreadSafeProxy.reset(new GrContextThreadSafeProxy(fCaps, this->uniqueID(), fBackend,
255 options));
commit-bot@chromium.org1836d332013-07-16 22:55:03 +0000256
Brian Osman46da1cc2017-02-14 14:15:48 -0500257 fDisableGpuYUVConversion = options.fDisableGpuYUVConversion;
Brian Osman8a83ca42018-02-12 14:32:17 -0500258 fSharpenMipmappedTextures = options.fSharpenMipmappedTextures;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000259 fDidTestPMConversions = false;
260
bsalomon6b2552f2016-09-15 13:50:26 -0700261 GrPathRendererChain::Options prcOptions;
bsalomon39ef7fb2016-09-21 11:16:05 -0700262 prcOptions.fAllowPathMaskCaching = options.fAllowPathMaskCaching;
Brian Osman195c05b2017-08-30 15:14:04 -0400263#if GR_TEST_UTILS
csmartdalton008b9d82017-02-22 12:00:42 -0700264 prcOptions.fGpuPathRenderers = options.fGpuPathRenderers;
Brian Osman195c05b2017-08-30 15:14:04 -0400265#endif
Brian Osmanb350ae22017-08-29 16:15:39 -0400266 if (options.fDisableDistanceFieldPaths) {
Brian Osman195c05b2017-08-30 15:14:04 -0400267 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
Brian Osmanb350ae22017-08-29 16:15:39 -0400268 }
Brian Salomonaf597482017-11-07 16:23:34 -0500269
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500270 if (!fResourceCache) {
271 // DDL TODO: remove this crippling of the path renderer chain
272 // Disable the small path renderer bc of the proxies in the atlas. They need to be
273 // unified when the opLists are added back to the destination drawing manager.
274 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
275 }
276
Brian Salomonaf597482017-11-07 16:23:34 -0500277 GrAtlasTextContext::Options atlasTextContextOptions;
278 atlasTextContextOptions.fMaxDistanceFieldFontSize = options.fGlyphsAsPathsFontSize;
279 atlasTextContextOptions.fMinDistanceFieldFontSize = options.fMinDistanceFieldFontSize;
Brian Salomonb5086962017-12-13 10:59:33 -0500280 atlasTextContextOptions.fDistanceFieldVerticesAlwaysHaveW = false;
281#if SK_SUPPORT_ATLAS_TEXT
282 if (GrContextOptions::Enable::kYes == options.fDistanceFieldGlyphVerticesAlwaysHaveW) {
283 atlasTextContextOptions.fDistanceFieldVerticesAlwaysHaveW = true;
284 }
285#endif
Brian Salomonaf597482017-11-07 16:23:34 -0500286
Robert Phillips4150eea2018-02-07 17:08:21 -0500287 fDrawingManager.reset(new GrDrawingManager(this, prcOptions, atlasTextContextOptions,
288 &fSingleOwner, options.fSortRenderTargets));
joshualitt7c3a2f82015-03-31 13:32:05 -0700289
Brian Salomon9f545bc2017-11-06 10:36:57 -0500290 GrDrawOpAtlas::AllowMultitexturing allowMultitexturing;
Jim Van Verthfc4f7682018-01-25 16:26:25 -0500291 if (GrContextOptions::Enable::kNo == options.fAllowMultipleGlyphCacheTextures ||
292 // multitexturing supported only if range can represent the index + texcoords fully
293 !(fCaps->shaderCaps()->floatIs32Bits() || fCaps->shaderCaps()->integerSupport())) {
294 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
295 } else {
296 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
Brian Salomon9f545bc2017-11-06 10:36:57 -0500297 }
Robert Phillips934c3d02018-02-26 16:49:16 +0000298 fAtlasGlyphCache = new GrAtlasGlyphCache(fProxyProvider, options.fGlyphCacheTextureMaximumBytes,
Brian Salomon9f545bc2017-11-06 10:36:57 -0500299 allowMultitexturing);
Jim Van Verth106b5c42017-09-26 12:45:29 -0400300 this->contextPriv().addOnFlushCallbackObject(fAtlasGlyphCache);
joshualittb7133be2015-04-08 09:08:31 -0700301
Robert Phillips303cd582018-02-14 18:54:01 -0500302 fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB,
303 this, this->uniqueID(), SkToBool(fGpu)));
Brian Salomon91a3e522017-06-23 10:58:19 -0400304
Brian Osman51279982017-08-23 10:12:00 -0400305 if (options.fExecutor) {
306 fTaskGroup = skstd::make_unique<SkTaskGroup>(*options.fExecutor);
307 }
308
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400309 fPersistentCache = options.fPersistentCache;
310
Brian Salomon91a3e522017-06-23 10:58:19 -0400311 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000312}
313
bsalomon@google.com27847de2011-02-22 20:59:41 +0000314GrContext::~GrContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800315 ASSERT_SINGLE_OWNER
316
Robert Phillips88260b52018-01-19 12:56:09 -0500317 if (fGpu) {
318 this->flush();
bsalomon@google.com733c0622013-04-24 17:59:32 +0000319 }
320
Robert Phillips2e6feed2018-01-22 15:27:20 -0500321 if (fDrawingManager) {
322 fDrawingManager->cleanup();
323 }
robertphillips2334fb62015-06-17 05:43:33 -0700324
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000325 for (int i = 0; i < fCleanUpData.count(); ++i) {
326 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
327 }
328
halcanary385fe4d2015-08-26 13:07:48 -0700329 delete fResourceProvider;
330 delete fResourceCache;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500331 delete fProxyProvider;
Brian Salomonf856fd12016-12-16 14:24:34 -0500332 delete fAtlasGlyphCache;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000333}
334
bungeman6bd52842016-10-27 09:30:08 -0700335sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
bungeman6bd52842016-10-27 09:30:08 -0700336 return fThreadSafeProxy;
bsalomon41b952c2016-03-11 06:46:33 -0800337}
338
Robert Phillipsfc711a22018-02-13 17:03:00 -0500339SkSurfaceCharacterization GrContextThreadSafeProxy::createCharacterization(
340 size_t cacheMaxResourceBytes,
341 const SkImageInfo& ii, const GrBackendFormat& backendFormat,
342 int sampleCnt, GrSurfaceOrigin origin,
343 const SkSurfaceProps& surfaceProps,
344 bool isMipMapped) {
345 if (!backendFormat.isValid()) {
346 return SkSurfaceCharacterization(); // return an invalid characterization
347 }
348
349 // We're assuming GrFSAAType::kMixedSamples will never be specified via this code path
350 GrFSAAType FSAAType = sampleCnt > 1 ? GrFSAAType::kUnifiedMSAA : GrFSAAType::kNone;
351
352 if (!fCaps->mipMapSupport()) {
353 isMipMapped = false;
354 }
355
356 GrPixelConfig config = kUnknown_GrPixelConfig;
357 if (!fCaps->getConfigFromBackendFormat(backendFormat, ii.colorType(), &config)) {
358 return SkSurfaceCharacterization(); // return an invalid characterization
359 }
360
361 // This surface characterization factory assumes that the resulting characterization is
362 // textureable.
363 if (!fCaps->isConfigTexturable(config)) {
364 return SkSurfaceCharacterization(); // return an invalid characterization
365 }
366
367 return SkSurfaceCharacterization(sk_ref_sp<GrContextThreadSafeProxy>(this),
368 cacheMaxResourceBytes,
369 origin, ii.width(), ii.height(), config, FSAAType, sampleCnt,
370 SkSurfaceCharacterization::Textureable(true),
371 SkSurfaceCharacterization::MipMapped(isMipMapped),
372 ii.refColorSpace(), surfaceProps);
373}
374
bsalomon2354f842014-07-28 13:48:36 -0700375void GrContext::abandonContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800376 ASSERT_SINGLE_OWNER
377
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500378 fProxyProvider->abandon();
bsalomond309e7a2015-04-30 14:18:54 -0700379 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800380
381 // Need to abandon the drawing manager first so all the render targets
382 // will be released/forgotten before they too are abandoned.
383 fDrawingManager->abandon();
384
bsalomon@google.com205d4602011-04-25 12:43:45 +0000385 // abandon first to so destructors
386 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800387 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700388
bsalomon6e2aad42016-04-01 11:54:31 -0700389 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
390
Brian Salomonf856fd12016-12-16 14:24:34 -0500391 fAtlasGlyphCache->freeAll();
bsalomon6e2aad42016-04-01 11:54:31 -0700392 fTextBlobCache->freeAll();
393}
394
395void GrContext::releaseResourcesAndAbandonContext() {
396 ASSERT_SINGLE_OWNER
397
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500398 fProxyProvider->abandon();
bsalomon6e2aad42016-04-01 11:54:31 -0700399 fResourceProvider->abandon();
400
401 // Need to abandon the drawing manager first so all the render targets
402 // will be released/forgotten before they too are abandoned.
403 fDrawingManager->abandon();
404
405 // Release all resources in the backend 3D API.
406 fResourceCache->releaseAll();
407
408 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000409
Brian Salomonf856fd12016-12-16 14:24:34 -0500410 fAtlasGlyphCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700411 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000412}
413
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000414void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800415 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000416 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000417}
418
419void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800420 ASSERT_SINGLE_OWNER
421
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000422 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000423
Brian Salomonf856fd12016-12-16 14:24:34 -0500424 fAtlasGlyphCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700425
426 fDrawingManager->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700427
428 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000429}
430
Jim Van Verth76d917c2017-12-13 09:26:37 -0500431void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) {
Brian Salomon5e150852017-03-22 14:53:13 -0400432 ASSERT_SINGLE_OWNER
Jim Van Verth76d917c2017-12-13 09:26:37 -0500433 fResourceCache->purgeAsNeeded();
434 fResourceCache->purgeResourcesNotUsedSince(GrStdSteadyClock::now() - msNotUsed);
435
436 fTextBlobCache->purgeStaleBlobs();
Brian Salomon5e150852017-03-22 14:53:13 -0400437}
438
Derek Sollenberger5480a182017-05-25 16:43:59 -0400439void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
440 ASSERT_SINGLE_OWNER
441 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
442}
443
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000444void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800445 ASSERT_SINGLE_OWNER
446
bsalomon71cb0c22014-11-14 12:10:14 -0800447 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800448 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800449 }
450 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800451 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800452 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000453}
454
Derek Sollenbergeree479142017-05-24 11:41:33 -0400455size_t GrContext::getResourceCachePurgeableBytes() const {
456 ASSERT_SINGLE_OWNER
457 return fResourceCache->getPurgeableBytes();
458}
459
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000460////////////////////////////////////////////////////////////////////////////////
461
Brian Salomonbdecacf2018-02-02 20:32:49 -0500462bool GrContext::colorTypeSupportedAsImage(SkColorType colorType) const {
463 GrPixelConfig config = SkImageInfo2GrPixelConfig(colorType, nullptr, *this->caps());
464 return this->caps()->isConfigTexturable(config);
465}
466
467int GrContext::maxSurfaceSampleCountForColorType(SkColorType colorType) const {
468 GrPixelConfig config = SkImageInfo2GrPixelConfig(colorType, nullptr, *this->caps());
469 return this->caps()->maxRenderTargetSampleCount(config);
470}
471
472////////////////////////////////////////////////////////////////////////////////
473
joshualitt0db6dfa2015-04-10 07:01:30 -0700474void GrContext::TextBlobCacheOverBudgetCB(void* data) {
475 SkASSERT(data);
Brian Osman11052242016-10-27 14:47:55 -0400476 // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on
477 // GrRenderTargetContext to perform a necessary flush. The solution is to move drawText calls
478 // to below the GrContext level, but this is not trivial because they call drawPath on
479 // SkGpuDevice.
joshualitt0db6dfa2015-04-10 07:01:30 -0700480 GrContext* context = reinterpret_cast<GrContext*>(data);
481 context->flush();
482}
483
bsalomon@google.com27847de2011-02-22 20:59:41 +0000484////////////////////////////////////////////////////////////////////////////////
485
bsalomonb77a9072016-09-07 10:02:04 -0700486void GrContext::flush() {
joshualitt1de610a2016-01-06 08:26:09 -0800487 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700488 RETURN_IF_ABANDONED
Robert Phillips7ee385e2017-03-30 08:02:11 -0400489
490 fDrawingManager->flush(nullptr);
491}
492
Greg Daniel51316782017-08-02 15:10:09 +0000493GrSemaphoresSubmitted GrContext::flushAndSignalSemaphores(int numSemaphores,
494 GrBackendSemaphore signalSemaphores[]) {
495 ASSERT_SINGLE_OWNER
496 if (fDrawingManager->wasAbandoned()) { return GrSemaphoresSubmitted::kNo; }
497
498 return fDrawingManager->flush(nullptr, numSemaphores, signalSemaphores);
499}
500
Robert Phillips7ee385e2017-03-30 08:02:11 -0400501void GrContextPriv::flush(GrSurfaceProxy* proxy) {
502 ASSERT_SINGLE_OWNER_PRIV
503 RETURN_IF_ABANDONED_PRIV
504 ASSERT_OWNED_PROXY_PRIV(proxy);
505
506 fContext->fDrawingManager->flush(proxy);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000507}
508
Brian Salomonc320b152018-02-20 14:05:36 -0500509bool sw_convert_to_premul(GrColorType srcColorType, int width, int height, size_t inRowBytes,
bsalomon81beccc2014-10-13 12:32:55 -0700510 const void* inPixels, size_t outRowBytes, void* outPixels) {
Brian Salomonc320b152018-02-20 14:05:36 -0500511 SkColorType colorType = GrColorTypeToSkColorType(srcColorType);
512 if (kUnknown_SkColorType == colorType || 4 != SkColorTypeBytesPerPixel(colorType)) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000513 return false;
514 }
bsalomon81beccc2014-10-13 12:32:55 -0700515
Matt Sarettc7b29082017-02-09 16:22:39 -0500516 for (int y = 0; y < height; y++) {
517 SkOpts::RGBA_to_rgbA((uint32_t*) outPixels, inPixels, width);
518 outPixels = SkTAddOffset<void>(outPixels, outRowBytes);
519 inPixels = SkTAddOffset<const void>(inPixels, inRowBytes);
520 }
bsalomon81beccc2014-10-13 12:32:55 -0700521
Matt Sarettc7b29082017-02-09 16:22:39 -0500522 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000523}
524
Brian Salomonc320b152018-02-20 14:05:36 -0500525// TODO: This will be removed when GrSurfaceContexts are aware of their color types.
526// (skbug.com/6718)
Brian Osmand2ca59a2017-04-13 14:03:57 -0400527static bool valid_premul_config(GrPixelConfig config) {
Brian Salomonc320b152018-02-20 14:05:36 -0500528 switch (config) {
529 case kUnknown_GrPixelConfig: return false;
530 case kAlpha_8_GrPixelConfig: return false;
531 case kGray_8_GrPixelConfig: return false;
532 case kRGB_565_GrPixelConfig: return false;
533 case kRGBA_4444_GrPixelConfig: return true;
534 case kRGBA_8888_GrPixelConfig: return true;
535 case kBGRA_8888_GrPixelConfig: return true;
536 case kSRGBA_8888_GrPixelConfig: return true;
537 case kSBGRA_8888_GrPixelConfig: return true;
538 case kRGBA_float_GrPixelConfig: return true;
539 case kRG_float_GrPixelConfig: return false;
540 case kAlpha_half_GrPixelConfig: return false;
541 case kRGBA_half_GrPixelConfig: return true;
542 case kAlpha_8_as_Alpha_GrPixelConfig: return false;
543 case kAlpha_8_as_Red_GrPixelConfig: return false;
544 case kAlpha_half_as_Red_GrPixelConfig: return false;
545 case kGray_8_as_Lum_GrPixelConfig: return false;
546 case kGray_8_as_Red_GrPixelConfig: return false;
547 }
548 SK_ABORT("Invalid GrPixelConfig");
549 return false;
Brian Osmance425512017-03-22 14:37:50 -0400550}
551
Brian Salomonc320b152018-02-20 14:05:36 -0500552static bool valid_premul_color_type(GrColorType ct) {
553 switch (ct) {
554 case GrColorType::kUnknown: return false;
555 case GrColorType::kAlpha_8: return false;
556 case GrColorType::kRGB_565: return false;
557 case GrColorType::kABGR_4444: return true;
558 case GrColorType::kRGBA_8888: return true;
559 case GrColorType::kBGRA_8888: return true;
560 case GrColorType::kGray_8: return false;
561 case GrColorType::kAlpha_F16: return false;
562 case GrColorType::kRGBA_F16: return true;
563 case GrColorType::kRG_F32: return false;
564 case GrColorType::kRGBA_F32: return true;
565 }
566 SK_ABORT("Invalid GrColorType");
567 return false;
568}
569
570static bool valid_pixel_conversion(GrColorType cpuColorType, GrPixelConfig gpuConfig,
Brian Osmand2ca59a2017-04-13 14:03:57 -0400571 bool premulConversion) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400572 // We only allow premul <-> unpremul conversions for some formats
Brian Salomonc320b152018-02-20 14:05:36 -0500573 if (premulConversion &&
574 (!valid_premul_color_type(cpuColorType) || !valid_premul_config(gpuConfig))) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400575 return false;
576 }
577
578 return true;
579}
580
Brian Salomonc320b152018-02-20 14:05:36 -0500581static bool pm_upm_must_round_trip(GrColorType cpuColorType, const SkColorSpace* cpuColorSpace) {
582 return !cpuColorSpace &&
583 (GrColorType::kRGBA_8888 == cpuColorType || GrColorType::kBGRA_8888 == cpuColorType);
Brian Osman409e74f2017-04-17 11:48:28 -0400584}
585
Brian Salomonc320b152018-02-20 14:05:36 -0500586// TODO: This will be removed when GrSurfaceContexts are aware of their color types.
587// (skbug.com/6718)
588static bool pm_upm_must_round_trip(GrPixelConfig surfaceConfig,
589 const SkColorSpace* surfaceColorSpace) {
590 return !surfaceColorSpace &&
591 (kRGBA_8888_GrPixelConfig == surfaceConfig || kBGRA_8888_GrPixelConfig == surfaceConfig);
592}
593
594static GrSRGBConversion determine_write_pixels_srgb_conversion(GrColorType srcColorType,
595 const SkColorSpace* srcColorSpace,
596 GrSRGBEncoded dstSRGBEncoded,
597 const SkColorSpace* dstColorSpace,
598 const GrCaps& caps) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500599 // No support for sRGB-encoded alpha.
Brian Salomonc320b152018-02-20 14:05:36 -0500600 if (GrColorTypeIsAlphaOnly(srcColorType)) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500601 return GrSRGBConversion::kNone;
602 }
Brian Salomonc320b152018-02-20 14:05:36 -0500603 // No conversions without GPU support for sRGB. (Legacy mode)
604 if (!caps.srgbSupport()) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500605 return GrSRGBConversion::kNone;
606 }
Brian Salomonc320b152018-02-20 14:05:36 -0500607 // If the GrSurfaceContext has no color space then it is in legacy mode.
608 if (!dstColorSpace) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500609 return GrSRGBConversion::kNone;
610 }
611
612 bool srcColorSpaceIsSRGB = srcColorSpace && srcColorSpace->gammaCloseToSRGB();
613 bool dstColorSpaceIsSRGB = dstColorSpace->gammaCloseToSRGB();
614
615 // For now we are assuming that if color space of the dst does not have sRGB gamma then the
616 // texture format is not sRGB encoded and vice versa. Note that we already checked for "legacy"
Brian Salomonc320b152018-02-20 14:05:36 -0500617 // mode being forced on by caps above. This may change in the future. We will then have to
618 // perform shader based conversions.
Brian Salomon9b009bb2018-02-14 13:53:55 -0500619 SkASSERT(dstColorSpaceIsSRGB == (GrSRGBEncoded::kYes == dstSRGBEncoded));
620
Brian Salomon9b009bb2018-02-14 13:53:55 -0500621 if (srcColorSpaceIsSRGB == dstColorSpaceIsSRGB) {
622 return GrSRGBConversion::kNone;
623 }
624 return srcColorSpaceIsSRGB ? GrSRGBConversion::kSRGBToLinear : GrSRGBConversion::kLinearToSRGB;
625}
626
Brian Salomonc320b152018-02-20 14:05:36 -0500627static GrSRGBConversion determine_read_pixels_srgb_conversion(GrSRGBEncoded srcSRGBEncoded,
628 const SkColorSpace* srcColorSpace,
629 GrColorType dstColorType,
630 const SkColorSpace* dstColorSpace,
631 const GrCaps& caps) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500632 // This is symmetrical with the write version.
Brian Salomonc320b152018-02-20 14:05:36 -0500633 switch (determine_write_pixels_srgb_conversion(dstColorType, dstColorSpace, srcSRGBEncoded,
634 srcColorSpace, caps)) {
Brian Salomon9b009bb2018-02-14 13:53:55 -0500635 case GrSRGBConversion::kNone: return GrSRGBConversion::kNone;
636 case GrSRGBConversion::kLinearToSRGB: return GrSRGBConversion::kSRGBToLinear;
637 case GrSRGBConversion::kSRGBToLinear: return GrSRGBConversion::kLinearToSRGB;
638 }
639 return GrSRGBConversion::kNone;
640}
641
Brian Salomonc320b152018-02-20 14:05:36 -0500642bool GrContextPriv::writeSurfacePixels(GrSurfaceContext* dst, int left, int top, int width,
643 int height, GrColorType srcColorType,
644 SkColorSpace* srcColorSpace, const void* buffer,
645 size_t rowBytes, uint32_t pixelOpsFlags) {
Brian Osmanb62ea222016-12-22 11:12:16 -0500646 // TODO: Color space conversion
647
Robert Phillipse78b7252017-04-06 07:59:41 -0400648 ASSERT_SINGLE_OWNER_PRIV
649 RETURN_FALSE_IF_ABANDONED_PRIV
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400650 SkASSERT(dst);
651 ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy());
Brian Salomondcbb9d92017-07-19 10:53:20 -0400652 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "writeSurfacePixels", fContext);
bsalomon6c6f6582015-09-10 08:12:46 -0700653
Robert Phillips6be756b2018-01-16 15:07:54 -0500654 if (!dst->asSurfaceProxy()->instantiate(this->resourceProvider())) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400655 return false;
656 }
657
Robert Phillips16d8ec62017-07-27 16:16:25 -0400658 GrSurfaceProxy* dstProxy = dst->asSurfaceProxy();
659 GrSurface* dstSurface = dstProxy->priv().peekSurface();
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400660
Brian Osmand2ca59a2017-04-13 14:03:57 -0400661 // 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 -0400662 const bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
Brian Salomonc320b152018-02-20 14:05:36 -0500663
664 if (!valid_pixel_conversion(srcColorType, dstProxy->config(), premul)) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400665 return false;
666 }
667
Brian Osman409e74f2017-04-17 11:48:28 -0400668 // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data,
669 // without any color spaces attached, and the caller wants us to premul.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400670 bool useConfigConversionEffect =
Brian Salomonc320b152018-02-20 14:05:36 -0500671 premul && pm_upm_must_round_trip(srcColorType, srcColorSpace) &&
Brian Salomonf3569f02017-10-24 12:52:33 -0400672 pm_upm_must_round_trip(dstProxy->config(), dst->colorSpaceInfo().colorSpace());
Brian Osman409e74f2017-04-17 11:48:28 -0400673
674 // Are we going to try to premul as part of a draw? For the non-legacy case, we always allow
675 // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly.
676 bool premulOnGpu = premul &&
677 (!useConfigConversionEffect || fContext->validPMUPMConversionExists());
bsalomon81beccc2014-10-13 12:32:55 -0700678
bsalomone8d21e82015-07-16 08:23:13 -0700679 // 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 -0700680 // necessary and because GrGpu::getWritePixelsInfo requires it.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400681 if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(),
Brian Salomonc320b152018-02-20 14:05:36 -0500682 GrColorTypeBytesPerPixel(srcColorType), &left, &top,
683 &width, &height, &buffer, &rowBytes)) {
bsalomone8d21e82015-07-16 08:23:13 -0700684 return false;
685 }
686
Brian Osman409e74f2017-04-17 11:48:28 -0400687 GrGpu::DrawPreference drawPreference = premulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference
688 : GrGpu::kNoDraw_DrawPreference;
bsalomonf0674512015-07-28 13:26:15 -0700689 GrGpu::WritePixelTempDrawInfo tempDrawInfo;
Brian Salomon9b009bb2018-02-14 13:53:55 -0500690 GrSRGBConversion srgbConversion = determine_write_pixels_srgb_conversion(
Brian Salomonc320b152018-02-20 14:05:36 -0500691 srcColorType, srcColorSpace, GrPixelConfigIsSRGBEncoded(dstProxy->config()),
692 dst->colorSpaceInfo().colorSpace(), *fContext->caps());
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400693 if (!fContext->fGpu->getWritePixelsInfo(dstSurface, dstProxy->origin(), width, height,
Brian Salomonc320b152018-02-20 14:05:36 -0500694 srcColorType, srgbConversion, &drawPreference,
Brian Salomon9b009bb2018-02-14 13:53:55 -0500695 &tempDrawInfo)) {
bsalomonf0674512015-07-28 13:26:15 -0700696 return false;
697 }
698
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400699 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && dstSurface->surfacePriv().hasPendingIO()) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400700 this->flush(nullptr); // MDB TODO: tighten this
bsalomonf0674512015-07-28 13:26:15 -0700701 }
702
Robert Phillips2f493142017-03-02 18:18:38 -0500703 sk_sp<GrTextureProxy> tempProxy;
bsalomonf0674512015-07-28 13:26:15 -0700704 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500705 tempProxy = this->proxyProvider()->createProxy(tempDrawInfo.fTempSurfaceDesc,
706 SkBackingFit::kApprox,
707 SkBudgeted::kYes);
Robert Phillips2f493142017-03-02 18:18:38 -0500708 if (!tempProxy && GrGpu::kRequireDraw_DrawPreference == drawPreference) {
bsalomonf0674512015-07-28 13:26:15 -0700709 return false;
710 }
711 }
712
713 // temp buffer for doing sw premul conversion, if needed.
714 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
Brian Osman409e74f2017-04-17 11:48:28 -0400715 // We need to do sw premul if we were unable to create a RT for drawing, or if we can't do the
716 // premul on the GPU
717 if (premul && (!tempProxy || !premulOnGpu)) {
718 size_t tmpRowBytes = 4 * width;
719 tmpPixels.reset(width * height);
Brian Salomonc320b152018-02-20 14:05:36 -0500720 if (!sw_convert_to_premul(srcColorType, width, height, rowBytes, buffer, tmpRowBytes,
Brian Osman409e74f2017-04-17 11:48:28 -0400721 tmpPixels.get())) {
722 return false;
bsalomonf0674512015-07-28 13:26:15 -0700723 }
Brian Osman409e74f2017-04-17 11:48:28 -0400724 rowBytes = tmpRowBytes;
725 buffer = tmpPixels.get();
bsalomonf0674512015-07-28 13:26:15 -0700726 }
Brian Osman409e74f2017-04-17 11:48:28 -0400727
728 if (tempProxy) {
Brian Osman2240be92017-10-18 13:15:13 -0400729 auto fp = GrSimpleTextureEffect::Make(tempProxy, SkMatrix::I());
Brian Osman409e74f2017-04-17 11:48:28 -0400730 if (premulOnGpu) {
731 fp = fContext->createUPMToPMEffect(std::move(fp), useConfigConversionEffect);
bsalomon81beccc2014-10-13 12:32:55 -0700732 }
Brian Osman409e74f2017-04-17 11:48:28 -0400733 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400734 if (!fp) {
735 return false;
736 }
Brian Osman409e74f2017-04-17 11:48:28 -0400737
Robert Phillips6be756b2018-01-16 15:07:54 -0500738 if (!tempProxy->instantiate(this->resourceProvider())) {
Brian Osman409e74f2017-04-17 11:48:28 -0400739 return false;
740 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400741 GrTexture* texture = tempProxy->priv().peekTexture();
Robert Phillips7bbbf622017-10-17 07:36:59 -0400742
743 if (tempProxy->priv().hasPendingIO()) {
744 this->flush(tempProxy.get());
745 }
746
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400747 if (!fContext->fGpu->writePixels(texture, tempProxy->origin(), 0, 0, width, height,
Brian Salomonc320b152018-02-20 14:05:36 -0500748 tempDrawInfo.fWriteColorType, buffer, rowBytes)) {
Brian Osman409e74f2017-04-17 11:48:28 -0400749 return false;
750 }
Robert Phillips7bbbf622017-10-17 07:36:59 -0400751 tempProxy = nullptr;
752
Brian Osman409e74f2017-04-17 11:48:28 -0400753 SkMatrix matrix;
754 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400755 GrRenderTargetContext* renderTargetContext = dst->asRenderTargetContext();
Brian Osman409e74f2017-04-17 11:48:28 -0400756 if (!renderTargetContext) {
757 return false;
758 }
759 GrPaint paint;
760 paint.addColorFragmentProcessor(std::move(fp));
761 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonf3569f02017-10-24 12:52:33 -0400762 paint.setAllowSRGBInputs(dst->colorSpaceInfo().isGammaCorrect() ||
763 GrPixelConfigIsSRGB(dst->colorSpaceInfo().config()));
Brian Osman409e74f2017-04-17 11:48:28 -0400764 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
765 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, matrix, rect,
766 nullptr);
767
768 if (kFlushWrites_PixelOp & pixelOpsFlags) {
769 this->flushSurfaceWrites(renderTargetContext->asRenderTargetProxy());
770 }
771 } else {
Brian Salomonc320b152018-02-20 14:05:36 -0500772 return fContext->fGpu->writePixels(dstSurface, dstProxy->origin(), left, top, width, height,
773 srcColorType, buffer, rowBytes);
bsalomon81beccc2014-10-13 12:32:55 -0700774 }
bsalomon81beccc2014-10-13 12:32:55 -0700775 return true;
776}
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000777
Brian Salomonc320b152018-02-20 14:05:36 -0500778bool GrContextPriv::readSurfacePixels(GrSurfaceContext* src, int left, int top, int width,
779 int height, GrColorType dstColorType,
780 SkColorSpace* dstColorSpace, void* buffer, size_t rowBytes,
781 uint32_t flags) {
Brian Osmanb62ea222016-12-22 11:12:16 -0500782 // TODO: Color space conversion
783
Robert Phillipse78b7252017-04-06 07:59:41 -0400784 ASSERT_SINGLE_OWNER_PRIV
785 RETURN_FALSE_IF_ABANDONED_PRIV
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400786 SkASSERT(src);
787 ASSERT_OWNED_PROXY_PRIV(src->asSurfaceProxy());
Brian Salomondcbb9d92017-07-19 10:53:20 -0400788 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "readSurfacePixels", fContext);
bsalomon32ab2602015-09-09 18:57:49 -0700789
Robert Phillipse78b7252017-04-06 07:59:41 -0400790 // MDB TODO: delay this instantiation until later in the method
Robert Phillips6be756b2018-01-16 15:07:54 -0500791 if (!src->asSurfaceProxy()->instantiate(this->resourceProvider())) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400792 return false;
793 }
794
Robert Phillips16d8ec62017-07-27 16:16:25 -0400795 GrSurfaceProxy* srcProxy = src->asSurfaceProxy();
796 GrSurface* srcSurface = srcProxy->priv().peekSurface();
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400797
Brian Osmand2ca59a2017-04-13 14:03:57 -0400798 // The src is premul but the dst is unpremul -> unpremul the src after or as part of the read
799 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
Brian Salomonc320b152018-02-20 14:05:36 -0500800
801 if (!valid_pixel_conversion(dstColorType, srcProxy->config(), unpremul)) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400802 return false;
803 }
804
Brian Osman409e74f2017-04-17 11:48:28 -0400805 // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data,
806 // without any color spaces attached, and the caller wants us to unpremul.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400807 bool useConfigConversionEffect =
Brian Salomonf3569f02017-10-24 12:52:33 -0400808 unpremul &&
809 pm_upm_must_round_trip(srcProxy->config(), src->colorSpaceInfo().colorSpace()) &&
Brian Salomonc320b152018-02-20 14:05:36 -0500810 pm_upm_must_round_trip(dstColorType, dstColorSpace);
Brian Osman409e74f2017-04-17 11:48:28 -0400811
812 // Are we going to try to unpremul as part of a draw? For the non-legacy case, we always allow
813 // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly.
814 bool unpremulOnGpu = unpremul &&
815 (!useConfigConversionEffect || fContext->validPMUPMConversionExists());
bsalomon6c6f6582015-09-10 08:12:46 -0700816
bsalomone8d21e82015-07-16 08:23:13 -0700817 // Adjust the params so that if we wind up using an intermediate surface we've already done
818 // all the trimming and the temporary can be the min size required.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400819 if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(),
Brian Salomonc320b152018-02-20 14:05:36 -0500820 GrColorTypeBytesPerPixel(dstColorType), &left, &top,
821 &width, &height, &buffer, &rowBytes)) {
bsalomone8d21e82015-07-16 08:23:13 -0700822 return false;
823 }
824
Brian Osman409e74f2017-04-17 11:48:28 -0400825 GrGpu::DrawPreference drawPreference = unpremulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference
826 : GrGpu::kNoDraw_DrawPreference;
bsalomon39826022015-07-23 08:07:21 -0700827 GrGpu::ReadPixelTempDrawInfo tempDrawInfo;
Brian Salomon9b009bb2018-02-14 13:53:55 -0500828 GrSRGBConversion srgbConversion = determine_read_pixels_srgb_conversion(
829 GrPixelConfigIsSRGBEncoded(srcProxy->config()), src->colorSpaceInfo().colorSpace(),
Brian Salomonc320b152018-02-20 14:05:36 -0500830 dstColorType, dstColorSpace, *fContext->caps());
831
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400832 if (!fContext->fGpu->getReadPixelsInfo(srcSurface, srcProxy->origin(), width, height, rowBytes,
Brian Salomonc320b152018-02-20 14:05:36 -0500833 dstColorType, srgbConversion, &drawPreference,
Brian Salomon9b009bb2018-02-14 13:53:55 -0500834 &tempDrawInfo)) {
bsalomon39826022015-07-23 08:07:21 -0700835 return false;
836 }
bsalomon191bcc02014-11-14 11:31:13 -0800837
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400838 if (!(kDontFlush_PixelOpsFlag & flags) && srcSurface->surfacePriv().hasPendingWrite()) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400839 this->flush(nullptr); // MDB TODO: tighten this
840 }
841
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400842 sk_sp<GrSurfaceProxy> proxyToRead = src->asSurfaceProxyRef();
bsalomon39826022015-07-23 08:07:21 -0700843 bool didTempDraw = false;
844 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomonb117ff12016-07-19 07:24:40 -0700845 if (SkBackingFit::kExact == tempDrawInfo.fTempSurfaceFit) {
bsalomon39826022015-07-23 08:07:21 -0700846 // We only respect this when the entire src is being read. Otherwise we can trigger too
847 // many odd ball texture sizes and trash the cache.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400848 if (width != srcSurface->width() || height != srcSurface->height()) {
bsalomonb117ff12016-07-19 07:24:40 -0700849 tempDrawInfo.fTempSurfaceFit= SkBackingFit::kApprox;
bsalomon39826022015-07-23 08:07:21 -0700850 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000851 }
brianosmandfe4f2e2016-07-21 13:28:36 -0700852 // TODO: Need to decide the semantics of this function for color spaces. Do we support
853 // conversion to a passed-in color space? For now, specifying nullptr means that this
Brian Salomon366093f2018-02-13 09:25:22 -0500854 // path will do no conversion, so it will match the behavior of the non-draw path. For
855 // now we simply infer an sRGB color space if the config is sRGB in order to avoid an
856 // illegal combination.
857 sk_sp<SkColorSpace> colorSpace;
858 if (GrPixelConfigIsSRGB(tempDrawInfo.fTempSurfaceDesc.fConfig)) {
859 colorSpace = SkColorSpace::MakeSRGB();
860 }
861 sk_sp<GrRenderTargetContext> tempRTC =
862 fContext->makeDeferredRenderTargetContext(tempDrawInfo.fTempSurfaceFit,
863 tempDrawInfo.fTempSurfaceDesc.fWidth,
864 tempDrawInfo.fTempSurfaceDesc.fHeight,
865 tempDrawInfo.fTempSurfaceDesc.fConfig,
866 std::move(colorSpace),
867 tempDrawInfo.fTempSurfaceDesc.fSampleCnt,
868 GrMipMapped::kNo,
869 tempDrawInfo.fTempSurfaceDesc.fOrigin);
Brian Osman693a5402016-10-27 15:13:22 -0400870 if (tempRTC) {
Robert Phillips67c18d62017-01-20 12:44:06 -0500871 SkMatrix textureMatrix = SkMatrix::MakeTrans(SkIntToScalar(left), SkIntToScalar(top));
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400872 sk_sp<GrTextureProxy> proxy = src->asTextureProxyRef();
Brian Osman2240be92017-10-18 13:15:13 -0400873 auto fp = GrSimpleTextureEffect::Make(std::move(proxy), textureMatrix);
Brian Osman409e74f2017-04-17 11:48:28 -0400874 if (unpremulOnGpu) {
875 fp = fContext->createPMToUPMEffect(std::move(fp), useConfigConversionEffect);
876 // We no longer need to do this on CPU after the read back.
877 unpremul = false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000878 }
Brian Osman409e74f2017-04-17 11:48:28 -0400879 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400880 if (!fp) {
881 return false;
882 }
Brian Osman60cd57e2017-04-06 10:19:06 -0400883
Brian Osman409e74f2017-04-17 11:48:28 -0400884 GrPaint paint;
885 paint.addColorFragmentProcessor(std::move(fp));
886 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
887 paint.setAllowSRGBInputs(true);
888 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
889 tempRTC->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect,
890 nullptr);
891 proxyToRead = tempRTC->asTextureProxyRef();
892 left = 0;
893 top = 0;
894 didTempDraw = true;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000895 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000896 }
joshualitt5c55fef2014-10-31 14:04:35 -0700897
Robert Phillipse78b7252017-04-06 07:59:41 -0400898 if (!proxyToRead) {
899 return false;
900 }
901
bsalomon39826022015-07-23 08:07:21 -0700902 if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000903 return false;
904 }
Brian Salomonc320b152018-02-20 14:05:36 -0500905 GrColorType colorTypeToRead = dstColorType;
bsalomon39826022015-07-23 08:07:21 -0700906 if (didTempDraw) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400907 this->flushSurfaceWrites(proxyToRead.get());
Brian Salomonc320b152018-02-20 14:05:36 -0500908 colorTypeToRead = tempDrawInfo.fReadColorType;
bsalomon39826022015-07-23 08:07:21 -0700909 }
Robert Phillips09dfc472017-09-13 15:25:47 -0400910
Robert Phillips6be756b2018-01-16 15:07:54 -0500911 if (!proxyToRead->instantiate(this->resourceProvider())) {
Robert Phillips09dfc472017-09-13 15:25:47 -0400912 return false;
913 }
914
915 GrSurface* surfaceToRead = proxyToRead->priv().peekSurface();
916
Brian Salomonc320b152018-02-20 14:05:36 -0500917 if (!fContext->fGpu->readPixels(surfaceToRead, proxyToRead->origin(), left, top, width, height,
918 colorTypeToRead, buffer, rowBytes)) {
bsalomon39826022015-07-23 08:07:21 -0700919 return false;
920 }
921
922 // Perform umpremul conversion if we weren't able to perform it as a draw.
923 if (unpremul) {
Brian Salomonc320b152018-02-20 14:05:36 -0500924 SkColorType colorType = GrColorTypeToSkColorType(dstColorType);
925 if (kUnknown_SkColorType == colorType || 4 != SkColorTypeBytesPerPixel(colorType)) {
reed@google.com7111d462014-03-25 16:20:24 +0000926 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000927 }
reed@google.com7111d462014-03-25 16:20:24 +0000928
Matt Sarettc7b29082017-02-09 16:22:39 -0500929 for (int y = 0; y < height; y++) {
930 SkUnpremultiplyRow<false>((uint32_t*) buffer, (const uint32_t*) buffer, width);
931 buffer = SkTAddOffset<void>(buffer, rowBytes);
932 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000933 }
934 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000935}
936
Robert Phillips7ee385e2017-03-30 08:02:11 -0400937void GrContextPriv::prepareSurfaceForExternalIO(GrSurfaceProxy* proxy) {
938 ASSERT_SINGLE_OWNER_PRIV
939 RETURN_IF_ABANDONED_PRIV
940 SkASSERT(proxy);
941 ASSERT_OWNED_PROXY_PRIV(proxy);
Greg Daniel51316782017-08-02 15:10:09 +0000942 fContext->fDrawingManager->prepareSurfaceForExternalIO(proxy, 0, nullptr);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000943}
944
Robert Phillips7ee385e2017-03-30 08:02:11 -0400945void GrContextPriv::flushSurfaceWrites(GrSurfaceProxy* proxy) {
946 ASSERT_SINGLE_OWNER_PRIV
947 RETURN_IF_ABANDONED_PRIV
948 SkASSERT(proxy);
949 ASSERT_OWNED_PROXY_PRIV(proxy);
950 if (proxy->priv().hasPendingWrite()) {
951 this->flush(proxy);
bsalomonf80bfed2014-10-07 05:56:02 -0700952 }
953}
954
Robert Phillips7ee385e2017-03-30 08:02:11 -0400955void GrContextPriv::flushSurfaceIO(GrSurfaceProxy* proxy) {
956 ASSERT_SINGLE_OWNER_PRIV
957 RETURN_IF_ABANDONED_PRIV
958 SkASSERT(proxy);
959 ASSERT_OWNED_PROXY_PRIV(proxy);
960 if (proxy->priv().hasPendingIO()) {
961 this->flush(proxy);
ajuma95243eb2016-08-24 08:19:02 -0700962 }
963}
964
bsalomon@google.com27847de2011-02-22 20:59:41 +0000965////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000966
Robert Phillips2c862492017-01-18 10:08:39 -0500967sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy> proxy,
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500968 sk_sp<SkColorSpace> colorSpace,
969 const SkSurfaceProps* props) {
Brian Osman45580d32016-11-23 09:37:01 -0500970 ASSERT_SINGLE_OWNER_PRIV
971
Brian Salomon366093f2018-02-13 09:25:22 -0500972 // sRGB pixel configs may only be used with near-sRGB gamma color spaces.
973 if (GrPixelConfigIsSRGB(proxy->config())) {
974 if (!colorSpace || !colorSpace->gammaCloseToSRGB()) {
975 return nullptr;
976 }
977 }
Brian Osman45580d32016-11-23 09:37:01 -0500978 if (proxy->asRenderTargetProxy()) {
Robert Phillips2c862492017-01-18 10:08:39 -0500979 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500980 std::move(colorSpace), props);
Brian Osman45580d32016-11-23 09:37:01 -0500981 } else {
982 SkASSERT(proxy->asTextureProxy());
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500983 SkASSERT(!props);
Robert Phillips2c862492017-01-18 10:08:39 -0500984 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Brian Osman45580d32016-11-23 09:37:01 -0500985 }
986}
987
Robert Phillipse2f7d182016-12-15 09:23:05 -0500988sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(const GrSurfaceDesc& dstDesc,
Greg Daniel65c7f662017-10-30 13:39:09 -0400989 GrMipMapped mipMapped,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500990 SkBackingFit fit,
Brian Salomon366093f2018-02-13 09:25:22 -0500991 SkBudgeted isDstBudgeted,
992 sk_sp<SkColorSpace> colorSpace,
993 const SkSurfaceProps* props) {
Greg Daniel65c7f662017-10-30 13:39:09 -0400994 sk_sp<GrTextureProxy> proxy;
995 if (GrMipMapped::kNo == mipMapped) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500996 proxy = this->proxyProvider()->createProxy(dstDesc, fit, isDstBudgeted);
Greg Daniel65c7f662017-10-30 13:39:09 -0400997 } else {
998 SkASSERT(SkBackingFit::kExact == fit);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500999 proxy = this->proxyProvider()->createMipMapProxy(dstDesc, isDstBudgeted);
Greg Daniel65c7f662017-10-30 13:39:09 -04001000 }
Robert Phillips77b3f322017-01-31 18:24:12 -05001001 if (!proxy) {
1002 return nullptr;
1003 }
Robert Phillipse2f7d182016-12-15 09:23:05 -05001004
Brian Salomon366093f2018-02-13 09:25:22 -05001005 return this->makeWrappedSurfaceContext(std::move(proxy), std::move(colorSpace), props);
Robert Phillipse2f7d182016-12-15 09:23:05 -05001006}
1007
Brian Salomond17f6582017-07-19 18:28:58 -04001008sk_sp<GrTextureContext> GrContextPriv::makeBackendTextureContext(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +00001009 GrSurfaceOrigin origin,
Brian Osmanc1e37052017-03-09 14:19:20 -05001010 sk_sp<SkColorSpace> colorSpace) {
Robert Phillips26caf892017-01-27 10:58:31 -05001011 ASSERT_SINGLE_OWNER_PRIV
1012
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001013 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->createWrappedTextureProxy(tex, origin);
Robert Phillips77b3f322017-01-31 18:24:12 -05001014 if (!proxy) {
1015 return nullptr;
1016 }
Robert Phillips26caf892017-01-27 10:58:31 -05001017
Brian Salomond17f6582017-07-19 18:28:58 -04001018 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Robert Phillips26caf892017-01-27 10:58:31 -05001019}
1020
Brian Osman11052242016-10-27 14:47:55 -04001021sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +00001022 const GrBackendTexture& tex,
1023 GrSurfaceOrigin origin,
1024 int sampleCnt,
Brian Osman11052242016-10-27 14:47:55 -04001025 sk_sp<SkColorSpace> colorSpace,
Brian Osmanc1e37052017-03-09 14:19:20 -05001026 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -07001027 ASSERT_SINGLE_OWNER_PRIV
Brian Salomonbdecacf2018-02-02 20:32:49 -05001028 SkASSERT(sampleCnt > 0);
robertphillips4fd74ae2016-08-03 14:26:53 -07001029
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001030 sk_sp<GrTextureProxy> proxy(this->proxyProvider()->createWrappedTextureProxy(tex, origin,
1031 sampleCnt));
Robert Phillips77b3f322017-01-31 18:24:12 -05001032 if (!proxy) {
1033 return nullptr;
1034 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001035
Robert Phillips37430132016-11-09 06:50:43 -05001036 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Brian Osman11052242016-10-27 14:47:55 -04001037 std::move(colorSpace), props);
robertphillips4fd74ae2016-08-03 14:26:53 -07001038}
1039
Brian Osman11052242016-10-27 14:47:55 -04001040sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext(
Greg Danielbcf612b2017-05-01 13:50:58 +00001041 const GrBackendRenderTarget& backendRT,
1042 GrSurfaceOrigin origin,
robertphillips4fd74ae2016-08-03 14:26:53 -07001043 sk_sp<SkColorSpace> colorSpace,
1044 const SkSurfaceProps* surfaceProps) {
1045 ASSERT_SINGLE_OWNER_PRIV
1046
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001047 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->createWrappedRenderTargetProxy(backendRT,
1048 origin);
Robert Phillips77b3f322017-01-31 18:24:12 -05001049 if (!proxy) {
1050 return nullptr;
1051 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001052
Robert Phillips37430132016-11-09 06:50:43 -05001053 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001054 std::move(colorSpace),
Brian Osman11052242016-10-27 14:47:55 -04001055 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -07001056}
1057
Brian Osman11052242016-10-27 14:47:55 -04001058sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +00001059 const GrBackendTexture& tex,
1060 GrSurfaceOrigin origin,
1061 int sampleCnt,
robertphillips4fd74ae2016-08-03 14:26:53 -07001062 sk_sp<SkColorSpace> colorSpace,
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001063 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -07001064 ASSERT_SINGLE_OWNER_PRIV
Brian Salomonbdecacf2018-02-02 20:32:49 -05001065 SkASSERT(sampleCnt > 0);
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001066 sk_sp<GrSurfaceProxy> proxy(this->proxyProvider()->createWrappedRenderTargetProxy(tex, origin,
1067 sampleCnt));
Robert Phillips77b3f322017-01-31 18:24:12 -05001068 if (!proxy) {
1069 return nullptr;
1070 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001071
Robert Phillips37430132016-11-09 06:50:43 -05001072 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001073 std::move(colorSpace),
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001074 props);
robertphillips77a2e522015-10-17 07:43:27 -07001075}
1076
Chris Daltonfe199b72017-05-05 11:26:15 -04001077void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
1078 fContext->fDrawingManager->addOnFlushCallbackObject(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -04001079}
1080
Robert Phillips62000362018-02-01 09:10:04 -05001081void GrContextPriv::moveOpListsToDDL(SkDeferredDisplayList* ddl) {
1082 fContext->fDrawingManager->moveOpListsToDDL(ddl);
1083}
1084
1085void GrContextPriv::copyOpListsFromDDL(const SkDeferredDisplayList* ddl,
1086 GrRenderTargetProxy* newDest) {
1087 fContext->fDrawingManager->copyOpListsFromDDL(ddl, newDest);
1088}
1089
robertphillips48fde9c2016-09-06 05:20:20 -07001090static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) {
Brian Osman78f20e02017-01-12 10:28:01 -05001091 switch (config) {
1092 case kAlpha_8_GrPixelConfig:
1093 case kRGB_565_GrPixelConfig:
1094 case kRGBA_4444_GrPixelConfig:
1095 case kBGRA_8888_GrPixelConfig:
1096 return kRGBA_8888_GrPixelConfig;
1097 case kSBGRA_8888_GrPixelConfig:
1098 return kSRGBA_8888_GrPixelConfig;
1099 case kAlpha_half_GrPixelConfig:
1100 return kRGBA_half_GrPixelConfig;
1101 default:
1102 return kUnknown_GrPixelConfig;
1103 }
robertphillips48fde9c2016-09-06 05:20:20 -07001104}
1105
robertphillipsd728f0c2016-11-21 11:05:03 -08001106sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContextWithFallback(
1107 SkBackingFit fit,
1108 int width, int height,
1109 GrPixelConfig config,
1110 sk_sp<SkColorSpace> colorSpace,
1111 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -04001112 GrMipMapped mipMapped,
robertphillipsd728f0c2016-11-21 11:05:03 -08001113 GrSurfaceOrigin origin,
1114 const SkSurfaceProps* surfaceProps,
1115 SkBudgeted budgeted) {
Brian Salomonbdecacf2018-02-02 20:32:49 -05001116 SkASSERT(sampleCnt > 0);
1117 if (0 == this->caps()->getRenderTargetSampleCount(sampleCnt, config)) {
robertphillipsd728f0c2016-11-21 11:05:03 -08001118 config = GrPixelConfigFallback(config);
1119 }
1120
1121 return this->makeDeferredRenderTargetContext(fit, width, height, config, std::move(colorSpace),
Greg Daniel45d63032017-10-30 13:41:26 -04001122 sampleCnt, mipMapped, origin, surfaceProps,
Greg Daniele1da1d92017-10-06 15:59:27 -04001123 budgeted);
robertphillipsd728f0c2016-11-21 11:05:03 -08001124}
1125
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001126sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContext(
1127 SkBackingFit fit,
1128 int width, int height,
1129 GrPixelConfig config,
1130 sk_sp<SkColorSpace> colorSpace,
1131 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -04001132 GrMipMapped mipMapped,
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001133 GrSurfaceOrigin origin,
1134 const SkSurfaceProps* surfaceProps,
1135 SkBudgeted budgeted) {
Brian Salomonbdecacf2018-02-02 20:32:49 -05001136 SkASSERT(sampleCnt > 0);
Brian Salomon79e4d1b2017-07-13 11:17:11 -04001137 if (this->abandoned()) {
1138 return nullptr;
1139 }
1140
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001141 GrSurfaceDesc desc;
1142 desc.fFlags = kRenderTarget_GrSurfaceFlag;
1143 desc.fOrigin = origin;
1144 desc.fWidth = width;
1145 desc.fHeight = height;
1146 desc.fConfig = config;
1147 desc.fSampleCnt = sampleCnt;
1148
Greg Daniele1da1d92017-10-06 15:59:27 -04001149 sk_sp<GrTextureProxy> rtp;
Greg Daniel45d63032017-10-30 13:41:26 -04001150 if (GrMipMapped::kNo == mipMapped) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001151 rtp = fProxyProvider->createProxy(desc, fit, budgeted);
Greg Daniele1da1d92017-10-06 15:59:27 -04001152 } else {
Robert Phillips0bd24dc2018-01-16 08:06:32 -05001153 rtp = fProxyProvider->createMipMapProxy(desc, budgeted);
Greg Daniele1da1d92017-10-06 15:59:27 -04001154 }
Robert Phillips08c5ec72017-01-30 12:26:47 -05001155 if (!rtp) {
1156 return nullptr;
1157 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001158
Robert Phillips1119dc32017-04-11 12:54:57 -04001159 sk_sp<GrRenderTargetContext> renderTargetContext(
1160 fDrawingManager->makeRenderTargetContext(std::move(rtp),
1161 std::move(colorSpace),
1162 surfaceProps));
Robert Phillips1119dc32017-04-11 12:54:57 -04001163 if (!renderTargetContext) {
1164 return nullptr;
1165 }
1166
1167 renderTargetContext->discard();
1168
1169 return renderTargetContext;
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001170}
1171
joshualitt1de610a2016-01-06 08:26:09 -08001172bool GrContext::abandoned() const {
1173 ASSERT_SINGLE_OWNER
robertphillips7761d612016-05-16 09:14:53 -07001174 return fDrawingManager->wasAbandoned();
robertphillips77a2e522015-10-17 07:43:27 -07001175}
1176
Brian Salomonaff329b2017-08-11 09:40:37 -04001177std::unique_ptr<GrFragmentProcessor> GrContext::createPMToUPMEffect(
1178 std::unique_ptr<GrFragmentProcessor> fp, bool useConfigConversionEffect) {
Robert Phillips757914d2017-01-25 15:48:30 -05001179 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -04001180 // We have specialized effects that guarantee round-trip conversion for some formats
1181 if (useConfigConversionEffect) {
1182 // We should have already called this->validPMUPMConversionExists() in this case
1183 SkASSERT(fDidTestPMConversions);
1184 // ...and it should have succeeded
1185 SkASSERT(this->validPMUPMConversionExists());
1186
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001187 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul);
Brian Osman2d2da4f2017-04-12 17:07:22 -04001188 } else {
1189 // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and
1190 // explicitly round the results. Just do the obvious, naive thing in the shader.
1191 return GrFragmentProcessor::UnpremulOutput(std::move(fp));
Robert Phillips757914d2017-01-25 15:48:30 -05001192 }
1193}
1194
Brian Salomonaff329b2017-08-11 09:40:37 -04001195std::unique_ptr<GrFragmentProcessor> GrContext::createUPMToPMEffect(
1196 std::unique_ptr<GrFragmentProcessor> fp, bool useConfigConversionEffect) {
joshualitt1de610a2016-01-06 08:26:09 -08001197 ASSERT_SINGLE_OWNER
Brian Osman2d2da4f2017-04-12 17:07:22 -04001198 // We have specialized effects that guarantee round-trip conversion for these formats
Brian Osman409e74f2017-04-17 11:48:28 -04001199 if (useConfigConversionEffect) {
1200 // We should have already called this->validPMUPMConversionExists() in this case
1201 SkASSERT(fDidTestPMConversions);
1202 // ...and it should have succeeded
1203 SkASSERT(this->validPMUPMConversionExists());
1204
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001205 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
Brian Osman2d2da4f2017-04-12 17:07:22 -04001206 } else {
1207 // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and
1208 // explicitly round the results. Just do the obvious, naive thing in the shader.
1209 return GrFragmentProcessor::PremulOutput(std::move(fp));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001210 }
1211}
1212
Brian Osman409e74f2017-04-17 11:48:28 -04001213bool GrContext::validPMUPMConversionExists() {
joshualitt1de610a2016-01-06 08:26:09 -08001214 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -04001215 if (!fDidTestPMConversions) {
Brian Osman28804f32017-04-20 10:24:36 -04001216 fPMUPMConversionsRoundTrip = GrConfigConversionEffect::TestForPreservingPMConversions(this);
Brian Osman409e74f2017-04-17 11:48:28 -04001217 fDidTestPMConversions = true;
1218 }
1219
bsalomon636e8022015-07-29 06:08:46 -07001220 // The PM<->UPM tests fail or succeed together so we only need to check one.
Brian Osman28804f32017-04-20 10:24:36 -04001221 return fPMUPMConversionsRoundTrip;
bsalomon636e8022015-07-29 06:08:46 -07001222}
1223
bsalomon37f9a262015-02-02 13:00:10 -08001224//////////////////////////////////////////////////////////////////////////////
1225
Robert Phillipsfc711a22018-02-13 17:03:00 -05001226// DDL TODO: remove 'maxResources'
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001227void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -08001228 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001229 if (maxResources) {
1230 *maxResources = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -08001231 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001232 if (maxResourceBytes) {
1233 *maxResourceBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -08001234 }
1235}
1236
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001237void GrContext::setResourceCacheLimits(int maxResources, size_t maxResourceBytes) {
joshualitt1de610a2016-01-06 08:26:09 -08001238 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001239 fResourceCache->setLimits(maxResources, maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -08001240}
1241
ericrk0a5fa482015-09-15 14:16:10 -07001242//////////////////////////////////////////////////////////////////////////////
1243
1244void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -08001245 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -07001246 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
1247}
Brian Osman71a18892017-08-10 10:23:25 -04001248
1249//////////////////////////////////////////////////////////////////////////////
1250
1251SkString GrContext::dump() const {
1252 SkDynamicMemoryWStream stream;
1253 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
1254 writer.beginObject();
1255
1256 static const char* kBackendStr[] = {
1257 "Metal",
1258 "OpenGL",
1259 "Vulkan",
1260 "Mock",
1261 };
1262 GR_STATIC_ASSERT(0 == kMetal_GrBackend);
1263 GR_STATIC_ASSERT(1 == kOpenGL_GrBackend);
1264 GR_STATIC_ASSERT(2 == kVulkan_GrBackend);
1265 GR_STATIC_ASSERT(3 == kMock_GrBackend);
1266 writer.appendString("backend", kBackendStr[fBackend]);
1267
1268 writer.appendName("caps");
1269 fCaps->dumpJSON(&writer);
1270
1271 writer.appendName("gpu");
1272 fGpu->dumpJSON(&writer);
1273
1274 // Flush JSON to the memory stream
1275 writer.endObject();
1276 writer.flush();
1277
1278 // Null terminate the JSON data in the memory stream
1279 stream.write8(0);
1280
1281 // Allocate a string big enough to hold all the data, then copy out of the stream
1282 SkString result(stream.bytesWritten());
1283 stream.copyToAndReset(result.writable_str());
1284 return result;
1285}