blob: f6c66db223fec54bd3550f6922cde28b02094238 [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"
Matt Sarett485c4992017-02-14 14:18:27 -050028#include "SkConvertPixels.h"
Brian Osman3b655982017-03-07 16:58:08 -050029#include "SkGr.h"
Brian Osman71a18892017-08-10 10:23:25 -040030#include "SkJSONWriter.h"
Brian Osman51279982017-08-23 10:12:00 -040031#include "SkMakeUnique.h"
32#include "SkTaskGroup.h"
Matt Sarettc7b29082017-02-09 16:22:39 -050033#include "SkUnPreMultiplyPriv.h"
joshualitt5478d422014-11-14 16:00:38 -080034#include "effects/GrConfigConversionEffect.h"
joshualitte8042922015-12-11 06:11:21 -080035#include "text/GrTextBlobCache.h"
joshualitt5478d422014-11-14 16:00:38 -080036
Greg Daniel02611d92017-07-25 10:05:01 -040037#include "gl/GrGLGpu.h"
38#include "mock/GrMockGpu.h"
Greg Danielb76a72a2017-07-13 15:07:54 -040039#ifdef SK_METAL
40#include "mtl/GrMtlTrampoline.h"
41#endif
Robert Phillipse42edcc2017-12-13 11:50:22 -050042#include "ddl/GrDDLGpu.h"
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:
67 GrDirectContext(GrBackend backend) : INHERITED(backend) { }
68
69protected:
70
71private:
72 typedef GrContext INHERITED;
73};
74
75class SK_API GrDDLContext : public GrContext {
76public:
77 GrDDLContext(GrContextThreadSafeProxy* proxy) : INHERITED(proxy) {}
78
79protected:
80
81private:
82 typedef GrContext INHERITED;
83};
84
bsalomon682c2692015-05-22 14:01:46 -070085GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext) {
86 GrContextOptions defaultOptions;
87 return Create(backend, backendContext, defaultOptions);
88}
bsalomonf28cff72015-05-22 12:25:41 -070089
bsalomon682c2692015-05-22 14:01:46 -070090GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
91 const GrContextOptions& options) {
bsalomon682c2692015-05-22 14:01:46 -070092
Robert Phillips88260b52018-01-19 12:56:09 -050093 sk_sp<GrContext> context(new GrDirectContext(backend));
Robert Phillipse42edcc2017-12-13 11:50:22 -050094
95 context->fGpu = GrGpu::Make(backend, backendContext, options, context.get());
96 if (!context->fGpu) {
halcanary96fcdcc2015-08-27 07:41:13 -070097 return nullptr;
bsalomon@google.com27847de2011-02-22 20:59:41 +000098 }
Robert Phillipse42edcc2017-12-13 11:50:22 -050099
100 if (!context->init(options)) {
101 return nullptr;
102 }
103
Brian Salomon91a3e522017-06-23 10:58:19 -0400104 return context.release();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000105}
106
Brian Salomon384fab42017-12-07 12:33:05 -0500107sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface) {
Greg Daniel02611d92017-07-25 10:05:01 -0400108 GrContextOptions defaultOptions;
Brian Salomon384fab42017-12-07 12:33:05 -0500109 return MakeGL(std::move(interface), defaultOptions);
Greg Daniel02611d92017-07-25 10:05:01 -0400110}
111
Brian Salomon384fab42017-12-07 12:33:05 -0500112sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface,
Greg Daniel02611d92017-07-25 10:05:01 -0400113 const GrContextOptions& options) {
Robert Phillips88260b52018-01-19 12:56:09 -0500114 sk_sp<GrContext> context(new GrDirectContext(kOpenGL_GrBackend));
Robert Phillipse42edcc2017-12-13 11:50:22 -0500115
Brian Salomon384fab42017-12-07 12:33:05 -0500116 context->fGpu = GrGLGpu::Make(std::move(interface), options, context.get());
Greg Daniel02611d92017-07-25 10:05:01 -0400117 if (!context->fGpu) {
118 return nullptr;
119 }
Greg Daniel02611d92017-07-25 10:05:01 -0400120 if (!context->init(options)) {
121 return nullptr;
122 }
123 return context;
124}
125
Brian Salomon990014d2017-12-07 16:23:39 -0500126sk_sp<GrContext> GrContext::MakeGL(const GrGLInterface* interface) {
127 return MakeGL(sk_ref_sp(interface));
128}
129
130sk_sp<GrContext> GrContext::MakeGL(const GrGLInterface* interface,
131 const GrContextOptions& options) {
132 return MakeGL(sk_ref_sp(interface), options);
133}
134
Greg Daniel02611d92017-07-25 10:05:01 -0400135sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions) {
136 GrContextOptions defaultOptions;
137 return MakeMock(mockOptions, defaultOptions);
138}
139
140sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions,
141 const GrContextOptions& options) {
Robert Phillips88260b52018-01-19 12:56:09 -0500142 sk_sp<GrContext> context(new GrDirectContext(kMock_GrBackend));
Robert Phillipse42edcc2017-12-13 11:50:22 -0500143
Brian Salomon384fab42017-12-07 12:33:05 -0500144 context->fGpu = GrMockGpu::Make(mockOptions, options, context.get());
Greg Daniel02611d92017-07-25 10:05:01 -0400145 if (!context->fGpu) {
146 return nullptr;
147 }
Greg Daniel02611d92017-07-25 10:05:01 -0400148 if (!context->init(options)) {
149 return nullptr;
150 }
151 return context;
152}
153
154#ifdef SK_VULKAN
Brian Salomon384fab42017-12-07 12:33:05 -0500155sk_sp<GrContext> GrContext::MakeVulkan(sk_sp<const GrVkBackendContext> backendContext) {
Greg Daniel02611d92017-07-25 10:05:01 -0400156 GrContextOptions defaultOptions;
Brian Salomon384fab42017-12-07 12:33:05 -0500157 return MakeVulkan(std::move(backendContext), defaultOptions);
Greg Daniel02611d92017-07-25 10:05:01 -0400158}
159
Brian Salomon384fab42017-12-07 12:33:05 -0500160sk_sp<GrContext> GrContext::MakeVulkan(sk_sp<const GrVkBackendContext> backendContext,
Greg Daniel02611d92017-07-25 10:05:01 -0400161 const GrContextOptions& options) {
Robert Phillips88260b52018-01-19 12:56:09 -0500162 sk_sp<GrContext> context(new GrDirectContext(kVulkan_GrBackend));
Robert Phillipse42edcc2017-12-13 11:50:22 -0500163
Brian Salomon384fab42017-12-07 12:33:05 -0500164 context->fGpu = GrVkGpu::Make(std::move(backendContext), options, context.get());
Greg Daniel02611d92017-07-25 10:05:01 -0400165 if (!context->fGpu) {
166 return nullptr;
167 }
Greg Daniel02611d92017-07-25 10:05:01 -0400168 if (!context->init(options)) {
169 return nullptr;
170 }
171 return context;
172}
173#endif
174
Greg Danielb76a72a2017-07-13 15:07:54 -0400175#ifdef SK_METAL
Greg Daniel02611d92017-07-25 10:05:01 -0400176sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue) {
177 GrContextOptions defaultOptions;
178 return MakeMetal(device, queue, defaultOptions);
179}
180
Greg Danielb76a72a2017-07-13 15:07:54 -0400181sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) {
Robert Phillipse42edcc2017-12-13 11:50:22 -0500182 sk_sp<GrContext> context(new GrContext(kMetal_GrBackend));
183
Brian Salomon384fab42017-12-07 12:33:05 -0500184 context->fGpu = GrMtlTrampoline::MakeGpu(context.get(), options, device, queue);
Greg Danielb76a72a2017-07-13 15:07:54 -0400185 if (!context->fGpu) {
186 return nullptr;
187 }
Greg Danielb76a72a2017-07-13 15:07:54 -0400188 if (!context->init(options)) {
189 return nullptr;
190 }
191 return context;
192}
193#endif
194
joshualitt0acd0d32015-05-07 08:23:19 -0700195static int32_t gNextID = 1;
196static int32_t next_id() {
197 int32_t id;
198 do {
199 id = sk_atomic_inc(&gNextID);
200 } while (id == SK_InvalidGenID);
201 return id;
202}
203
Robert Phillipse42edcc2017-12-13 11:50:22 -0500204sk_sp<GrContext> GrContextPriv::MakeDDL(GrContextThreadSafeProxy* proxy) {
Robert Phillips88260b52018-01-19 12:56:09 -0500205 sk_sp<GrContext> context(new GrDDLContext(proxy));
Robert Phillipse42edcc2017-12-13 11:50:22 -0500206
Robert Phillips88260b52018-01-19 12:56:09 -0500207 // Note: we aren't creating a Gpu here. This causes the resource provider & cache to
208 // also not be created
Robert Phillipse42edcc2017-12-13 11:50:22 -0500209 if (!context->init(proxy->fOptions)) {
210 return nullptr;
211 }
212 return context;
213}
214
215GrContext::GrContext(GrBackend backend)
216 : fUniqueID(next_id())
217 , fBackend(backend) {
halcanary96fcdcc2015-08-27 07:41:13 -0700218 fResourceCache = nullptr;
219 fResourceProvider = nullptr;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500220 fProxyProvider = nullptr;
Brian Salomonf856fd12016-12-16 14:24:34 -0500221 fAtlasGlyphCache = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000222}
223
Robert Phillipse42edcc2017-12-13 11:50:22 -0500224GrContext::GrContext(GrContextThreadSafeProxy* proxy)
Robert Phillips88260b52018-01-19 12:56:09 -0500225 : fCaps(proxy->fCaps)
226 , fUniqueID(proxy->fContextUniqueID)
Robert Phillipse42edcc2017-12-13 11:50:22 -0500227 , fBackend(proxy->fBackend) {
228 fResourceCache = nullptr;
229 fResourceProvider = nullptr;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500230 fProxyProvider = nullptr;
Robert Phillipse42edcc2017-12-13 11:50:22 -0500231 fAtlasGlyphCache = nullptr;
Greg Danielb76a72a2017-07-13 15:07:54 -0400232}
joshualitt1de610a2016-01-06 08:26:09 -0800233
Greg Danielb76a72a2017-07-13 15:07:54 -0400234bool GrContext::init(const GrContextOptions& options) {
235 ASSERT_SINGLE_OWNER
Robert Phillips88260b52018-01-19 12:56:09 -0500236
237 if (fGpu) {
238 fCaps = fGpu->refCaps();
239 fResourceCache = new GrResourceCache(fCaps.get(), fUniqueID);
240 fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, &fSingleOwner);
241 }
242
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500243 fProxyProvider = new GrProxyProvider(fResourceProvider, fResourceCache, fCaps, &fSingleOwner);
Robert Phillips88260b52018-01-19 12:56:09 -0500244
245 if (fResourceCache) {
246 fResourceCache->setProxyProvider(fProxyProvider);
247 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500248
Robert Phillipse42edcc2017-12-13 11:50:22 -0500249 // DDL TODO: we need to think through how the task group & persistent cache
250 // get passed on to/shared between all the DDLRecorders created with this context.
251 fThreadSafeProxy.reset(new GrContextThreadSafeProxy(fCaps, this->uniqueID(), fBackend,
252 options));
commit-bot@chromium.org1836d332013-07-16 22:55:03 +0000253
Brian Osman46da1cc2017-02-14 14:15:48 -0500254 fDisableGpuYUVConversion = options.fDisableGpuYUVConversion;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000255 fDidTestPMConversions = false;
256
bsalomon6b2552f2016-09-15 13:50:26 -0700257 GrPathRendererChain::Options prcOptions;
bsalomon39ef7fb2016-09-21 11:16:05 -0700258 prcOptions.fAllowPathMaskCaching = options.fAllowPathMaskCaching;
Brian Osman195c05b2017-08-30 15:14:04 -0400259#if GR_TEST_UTILS
csmartdalton008b9d82017-02-22 12:00:42 -0700260 prcOptions.fGpuPathRenderers = options.fGpuPathRenderers;
Brian Osman195c05b2017-08-30 15:14:04 -0400261#endif
Brian Osmanb350ae22017-08-29 16:15:39 -0400262 if (options.fDisableDistanceFieldPaths) {
Brian Osman195c05b2017-08-30 15:14:04 -0400263 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
Brian Osmanb350ae22017-08-29 16:15:39 -0400264 }
Brian Salomonaf597482017-11-07 16:23:34 -0500265
266 GrAtlasTextContext::Options atlasTextContextOptions;
267 atlasTextContextOptions.fMaxDistanceFieldFontSize = options.fGlyphsAsPathsFontSize;
268 atlasTextContextOptions.fMinDistanceFieldFontSize = options.fMinDistanceFieldFontSize;
Brian Salomonb5086962017-12-13 10:59:33 -0500269 atlasTextContextOptions.fDistanceFieldVerticesAlwaysHaveW = false;
270#if SK_SUPPORT_ATLAS_TEXT
271 if (GrContextOptions::Enable::kYes == options.fDistanceFieldGlyphVerticesAlwaysHaveW) {
272 atlasTextContextOptions.fDistanceFieldVerticesAlwaysHaveW = true;
273 }
274#endif
Brian Salomonaf597482017-11-07 16:23:34 -0500275
276 fDrawingManager.reset(
277 new GrDrawingManager(this, prcOptions, atlasTextContextOptions, &fSingleOwner));
joshualitt7c3a2f82015-03-31 13:32:05 -0700278
Brian Salomon9f545bc2017-11-06 10:36:57 -0500279 GrDrawOpAtlas::AllowMultitexturing allowMultitexturing;
Jim Van Verthc02cb8a2017-11-20 22:11:30 +0000280 switch (options.fAllowMultipleGlyphCacheTextures) {
281 case GrContextOptions::Enable::kDefault:
282#ifdef SK_BUILD_FOR_IOS
283 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
284#else
285 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
286#endif
287 break;
288 case GrContextOptions::Enable::kNo:
289 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
290 break;
291 case GrContextOptions::Enable::kYes:
292 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
293 break;
Brian Salomon9f545bc2017-11-06 10:36:57 -0500294 }
295 fAtlasGlyphCache = new GrAtlasGlyphCache(this, options.fGlyphCacheTextureMaximumBytes,
296 allowMultitexturing);
Jim Van Verth106b5c42017-09-26 12:45:29 -0400297 this->contextPriv().addOnFlushCallbackObject(fAtlasGlyphCache);
joshualittb7133be2015-04-08 09:08:31 -0700298
Jim Van Verth474d6872017-12-14 13:00:05 -0500299 fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB, this, this->uniqueID()));
Brian Salomon91a3e522017-06-23 10:58:19 -0400300
Brian Osman51279982017-08-23 10:12:00 -0400301 if (options.fExecutor) {
302 fTaskGroup = skstd::make_unique<SkTaskGroup>(*options.fExecutor);
303 }
304
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400305 fPersistentCache = options.fPersistentCache;
306
Brian Salomon91a3e522017-06-23 10:58:19 -0400307 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000308}
309
bsalomon@google.com27847de2011-02-22 20:59:41 +0000310GrContext::~GrContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800311 ASSERT_SINGLE_OWNER
312
Robert Phillips88260b52018-01-19 12:56:09 -0500313 if (fGpu) {
314 this->flush();
bsalomon@google.com733c0622013-04-24 17:59:32 +0000315 }
316
Robert Phillips2e6feed2018-01-22 15:27:20 -0500317 if (fDrawingManager) {
318 fDrawingManager->cleanup();
319 }
robertphillips2334fb62015-06-17 05:43:33 -0700320
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000321 for (int i = 0; i < fCleanUpData.count(); ++i) {
322 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
323 }
324
halcanary385fe4d2015-08-26 13:07:48 -0700325 delete fResourceProvider;
326 delete fResourceCache;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500327 delete fProxyProvider;
Brian Salomonf856fd12016-12-16 14:24:34 -0500328 delete fAtlasGlyphCache;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000329}
330
bungeman6bd52842016-10-27 09:30:08 -0700331sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
bungeman6bd52842016-10-27 09:30:08 -0700332 return fThreadSafeProxy;
bsalomon41b952c2016-03-11 06:46:33 -0800333}
334
bsalomon2354f842014-07-28 13:48:36 -0700335void GrContext::abandonContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800336 ASSERT_SINGLE_OWNER
337
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500338 fProxyProvider->abandon();
bsalomond309e7a2015-04-30 14:18:54 -0700339 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800340
341 // Need to abandon the drawing manager first so all the render targets
342 // will be released/forgotten before they too are abandoned.
343 fDrawingManager->abandon();
344
bsalomon@google.com205d4602011-04-25 12:43:45 +0000345 // abandon first to so destructors
346 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800347 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700348
bsalomon6e2aad42016-04-01 11:54:31 -0700349 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
350
Brian Salomonf856fd12016-12-16 14:24:34 -0500351 fAtlasGlyphCache->freeAll();
bsalomon6e2aad42016-04-01 11:54:31 -0700352 fTextBlobCache->freeAll();
353}
354
355void GrContext::releaseResourcesAndAbandonContext() {
356 ASSERT_SINGLE_OWNER
357
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500358 fProxyProvider->abandon();
bsalomon6e2aad42016-04-01 11:54:31 -0700359 fResourceProvider->abandon();
360
361 // Need to abandon the drawing manager first so all the render targets
362 // will be released/forgotten before they too are abandoned.
363 fDrawingManager->abandon();
364
365 // Release all resources in the backend 3D API.
366 fResourceCache->releaseAll();
367
368 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000369
Brian Salomonf856fd12016-12-16 14:24:34 -0500370 fAtlasGlyphCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700371 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000372}
373
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000374void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800375 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000376 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000377}
378
379void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800380 ASSERT_SINGLE_OWNER
381
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000382 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000383
Brian Salomonf856fd12016-12-16 14:24:34 -0500384 fAtlasGlyphCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700385
386 fDrawingManager->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700387
388 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000389}
390
Jim Van Verth76d917c2017-12-13 09:26:37 -0500391void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) {
Brian Salomon5e150852017-03-22 14:53:13 -0400392 ASSERT_SINGLE_OWNER
Jim Van Verth76d917c2017-12-13 09:26:37 -0500393 fResourceCache->purgeAsNeeded();
394 fResourceCache->purgeResourcesNotUsedSince(GrStdSteadyClock::now() - msNotUsed);
395
396 fTextBlobCache->purgeStaleBlobs();
Brian Salomon5e150852017-03-22 14:53:13 -0400397}
398
Derek Sollenberger5480a182017-05-25 16:43:59 -0400399void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
400 ASSERT_SINGLE_OWNER
401 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
402}
403
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000404void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800405 ASSERT_SINGLE_OWNER
406
bsalomon71cb0c22014-11-14 12:10:14 -0800407 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800408 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800409 }
410 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800411 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800412 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000413}
414
Derek Sollenbergeree479142017-05-24 11:41:33 -0400415size_t GrContext::getResourceCachePurgeableBytes() const {
416 ASSERT_SINGLE_OWNER
417 return fResourceCache->getPurgeableBytes();
418}
419
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000420////////////////////////////////////////////////////////////////////////////////
421
joshualitt0db6dfa2015-04-10 07:01:30 -0700422void GrContext::TextBlobCacheOverBudgetCB(void* data) {
423 SkASSERT(data);
Brian Osman11052242016-10-27 14:47:55 -0400424 // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on
425 // GrRenderTargetContext to perform a necessary flush. The solution is to move drawText calls
426 // to below the GrContext level, but this is not trivial because they call drawPath on
427 // SkGpuDevice.
joshualitt0db6dfa2015-04-10 07:01:30 -0700428 GrContext* context = reinterpret_cast<GrContext*>(data);
429 context->flush();
430}
431
bsalomon@google.com27847de2011-02-22 20:59:41 +0000432////////////////////////////////////////////////////////////////////////////////
433
bsalomonb77a9072016-09-07 10:02:04 -0700434void GrContext::flush() {
joshualitt1de610a2016-01-06 08:26:09 -0800435 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700436 RETURN_IF_ABANDONED
Robert Phillips7ee385e2017-03-30 08:02:11 -0400437
438 fDrawingManager->flush(nullptr);
439}
440
Greg Daniel51316782017-08-02 15:10:09 +0000441GrSemaphoresSubmitted GrContext::flushAndSignalSemaphores(int numSemaphores,
442 GrBackendSemaphore signalSemaphores[]) {
443 ASSERT_SINGLE_OWNER
444 if (fDrawingManager->wasAbandoned()) { return GrSemaphoresSubmitted::kNo; }
445
446 return fDrawingManager->flush(nullptr, numSemaphores, signalSemaphores);
447}
448
Robert Phillips7ee385e2017-03-30 08:02:11 -0400449void GrContextPriv::flush(GrSurfaceProxy* proxy) {
450 ASSERT_SINGLE_OWNER_PRIV
451 RETURN_IF_ABANDONED_PRIV
452 ASSERT_OWNED_PROXY_PRIV(proxy);
453
454 fContext->fDrawingManager->flush(proxy);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000455}
456
bsalomon81beccc2014-10-13 12:32:55 -0700457bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
458 const void* inPixels, size_t outRowBytes, void* outPixels) {
Matt Sarettc7b29082017-02-09 16:22:39 -0500459 SkColorType colorType;
460 if (!GrPixelConfigToColorType(srcConfig, &colorType) ||
461 4 != SkColorTypeBytesPerPixel(colorType))
462 {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000463 return false;
464 }
bsalomon81beccc2014-10-13 12:32:55 -0700465
Matt Sarettc7b29082017-02-09 16:22:39 -0500466 for (int y = 0; y < height; y++) {
467 SkOpts::RGBA_to_rgbA((uint32_t*) outPixels, inPixels, width);
468 outPixels = SkTAddOffset<void>(outPixels, outRowBytes);
469 inPixels = SkTAddOffset<const void>(inPixels, inRowBytes);
470 }
bsalomon81beccc2014-10-13 12:32:55 -0700471
Matt Sarettc7b29082017-02-09 16:22:39 -0500472 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000473}
474
Brian Osmand2ca59a2017-04-13 14:03:57 -0400475static bool valid_premul_config(GrPixelConfig config) {
Brian Osmance425512017-03-22 14:37:50 -0400476 return GrPixelConfigIs8888Unorm(config) || kRGBA_half_GrPixelConfig == config;
477}
478
Brian Osmand2ca59a2017-04-13 14:03:57 -0400479static bool valid_pixel_conversion(GrPixelConfig srcConfig, GrPixelConfig dstConfig,
480 bool premulConversion) {
481 // We don't allow conversion between integer configs and float/fixed configs.
482 if (GrPixelConfigIsSint(srcConfig) != GrPixelConfigIsSint(dstConfig)) {
483 return false;
484 }
485
486 // We only allow premul <-> unpremul conversions for some formats
487 if (premulConversion && (!valid_premul_config(srcConfig) || !valid_premul_config(dstConfig))) {
488 return false;
489 }
490
491 return true;
492}
493
Brian Osman409e74f2017-04-17 11:48:28 -0400494static bool pm_upm_must_round_trip(GrPixelConfig config, SkColorSpace* colorSpace) {
495 return !colorSpace &&
496 (kRGBA_8888_GrPixelConfig == config || kBGRA_8888_GrPixelConfig == config);
497}
498
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400499bool GrContextPriv::writeSurfacePixels(GrSurfaceContext* dst,
Robert Phillipse78b7252017-04-06 07:59:41 -0400500 int left, int top, int width, int height,
501 GrPixelConfig srcConfig, SkColorSpace* srcColorSpace,
502 const void* buffer, size_t rowBytes,
503 uint32_t pixelOpsFlags) {
Brian Osmanb62ea222016-12-22 11:12:16 -0500504 // TODO: Color space conversion
505
Robert Phillipse78b7252017-04-06 07:59:41 -0400506 ASSERT_SINGLE_OWNER_PRIV
507 RETURN_FALSE_IF_ABANDONED_PRIV
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400508 SkASSERT(dst);
509 ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy());
Brian Salomondcbb9d92017-07-19 10:53:20 -0400510 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "writeSurfacePixels", fContext);
bsalomon6c6f6582015-09-10 08:12:46 -0700511
Robert Phillips6be756b2018-01-16 15:07:54 -0500512 if (!dst->asSurfaceProxy()->instantiate(this->resourceProvider())) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400513 return false;
514 }
515
Robert Phillips16d8ec62017-07-27 16:16:25 -0400516 GrSurfaceProxy* dstProxy = dst->asSurfaceProxy();
517 GrSurface* dstSurface = dstProxy->priv().peekSurface();
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400518
Brian Osmand2ca59a2017-04-13 14:03:57 -0400519 // 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 -0400520 const bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
Robert Phillips16d8ec62017-07-27 16:16:25 -0400521 if (!valid_pixel_conversion(srcConfig, dstProxy->config(), premul)) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400522 return false;
523 }
524
Brian Osman409e74f2017-04-17 11:48:28 -0400525 // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data,
526 // without any color spaces attached, and the caller wants us to premul.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400527 bool useConfigConversionEffect =
Brian Salomonf3569f02017-10-24 12:52:33 -0400528 premul && pm_upm_must_round_trip(srcConfig, srcColorSpace) &&
529 pm_upm_must_round_trip(dstProxy->config(), dst->colorSpaceInfo().colorSpace());
Brian Osman409e74f2017-04-17 11:48:28 -0400530
531 // Are we going to try to premul as part of a draw? For the non-legacy case, we always allow
532 // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly.
533 bool premulOnGpu = premul &&
534 (!useConfigConversionEffect || fContext->validPMUPMConversionExists());
bsalomon81beccc2014-10-13 12:32:55 -0700535
bsalomone8d21e82015-07-16 08:23:13 -0700536 // 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 -0700537 // necessary and because GrGpu::getWritePixelsInfo requires it.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400538 if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(),
bsalomone8d21e82015-07-16 08:23:13 -0700539 GrBytesPerPixel(srcConfig), &left, &top, &width,
540 &height, &buffer, &rowBytes)) {
541 return false;
542 }
543
Brian Osman409e74f2017-04-17 11:48:28 -0400544 GrGpu::DrawPreference drawPreference = premulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference
545 : GrGpu::kNoDraw_DrawPreference;
bsalomonf0674512015-07-28 13:26:15 -0700546 GrGpu::WritePixelTempDrawInfo tempDrawInfo;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400547 if (!fContext->fGpu->getWritePixelsInfo(dstSurface, dstProxy->origin(), width, height,
Robert Phillips16d8ec62017-07-27 16:16:25 -0400548 srcConfig, &drawPreference, &tempDrawInfo)) {
bsalomonf0674512015-07-28 13:26:15 -0700549 return false;
550 }
551
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400552 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && dstSurface->surfacePriv().hasPendingIO()) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400553 this->flush(nullptr); // MDB TODO: tighten this
bsalomonf0674512015-07-28 13:26:15 -0700554 }
555
Robert Phillips2f493142017-03-02 18:18:38 -0500556 sk_sp<GrTextureProxy> tempProxy;
bsalomonf0674512015-07-28 13:26:15 -0700557 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500558 tempProxy = this->proxyProvider()->createProxy(tempDrawInfo.fTempSurfaceDesc,
559 SkBackingFit::kApprox,
560 SkBudgeted::kYes);
Robert Phillips2f493142017-03-02 18:18:38 -0500561 if (!tempProxy && GrGpu::kRequireDraw_DrawPreference == drawPreference) {
bsalomonf0674512015-07-28 13:26:15 -0700562 return false;
563 }
564 }
565
566 // temp buffer for doing sw premul conversion, if needed.
567 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
Brian Osman409e74f2017-04-17 11:48:28 -0400568 // We need to do sw premul if we were unable to create a RT for drawing, or if we can't do the
569 // premul on the GPU
570 if (premul && (!tempProxy || !premulOnGpu)) {
571 size_t tmpRowBytes = 4 * width;
572 tmpPixels.reset(width * height);
573 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
574 tmpPixels.get())) {
575 return false;
bsalomonf0674512015-07-28 13:26:15 -0700576 }
Brian Osman409e74f2017-04-17 11:48:28 -0400577 rowBytes = tmpRowBytes;
578 buffer = tmpPixels.get();
bsalomonf0674512015-07-28 13:26:15 -0700579 }
Brian Osman409e74f2017-04-17 11:48:28 -0400580
581 if (tempProxy) {
Brian Osman2240be92017-10-18 13:15:13 -0400582 auto fp = GrSimpleTextureEffect::Make(tempProxy, SkMatrix::I());
Brian Osman409e74f2017-04-17 11:48:28 -0400583 if (premulOnGpu) {
584 fp = fContext->createUPMToPMEffect(std::move(fp), useConfigConversionEffect);
bsalomon81beccc2014-10-13 12:32:55 -0700585 }
Brian Osman409e74f2017-04-17 11:48:28 -0400586 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400587 if (!fp) {
588 return false;
589 }
Brian Osman409e74f2017-04-17 11:48:28 -0400590
Robert Phillips6be756b2018-01-16 15:07:54 -0500591 if (!tempProxy->instantiate(this->resourceProvider())) {
Brian Osman409e74f2017-04-17 11:48:28 -0400592 return false;
593 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400594 GrTexture* texture = tempProxy->priv().peekTexture();
Robert Phillips7bbbf622017-10-17 07:36:59 -0400595
596 if (tempProxy->priv().hasPendingIO()) {
597 this->flush(tempProxy.get());
598 }
599
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400600 if (!fContext->fGpu->writePixels(texture, tempProxy->origin(), 0, 0, width, height,
601 tempDrawInfo.fWriteConfig, buffer, rowBytes)) {
Brian Osman409e74f2017-04-17 11:48:28 -0400602 return false;
603 }
Robert Phillips7bbbf622017-10-17 07:36:59 -0400604 tempProxy = nullptr;
605
Brian Osman409e74f2017-04-17 11:48:28 -0400606 SkMatrix matrix;
607 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400608 GrRenderTargetContext* renderTargetContext = dst->asRenderTargetContext();
Brian Osman409e74f2017-04-17 11:48:28 -0400609 if (!renderTargetContext) {
610 return false;
611 }
612 GrPaint paint;
613 paint.addColorFragmentProcessor(std::move(fp));
614 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonf3569f02017-10-24 12:52:33 -0400615 paint.setAllowSRGBInputs(dst->colorSpaceInfo().isGammaCorrect() ||
616 GrPixelConfigIsSRGB(dst->colorSpaceInfo().config()));
Brian Osman409e74f2017-04-17 11:48:28 -0400617 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
618 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, matrix, rect,
619 nullptr);
620
621 if (kFlushWrites_PixelOp & pixelOpsFlags) {
622 this->flushSurfaceWrites(renderTargetContext->asRenderTargetProxy());
623 }
624 } else {
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400625 return fContext->fGpu->writePixels(dstSurface, dstProxy->origin(), left, top, width,
626 height, srcConfig, buffer, rowBytes);
bsalomon81beccc2014-10-13 12:32:55 -0700627 }
bsalomon81beccc2014-10-13 12:32:55 -0700628 return true;
629}
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000630
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400631bool GrContextPriv::readSurfacePixels(GrSurfaceContext* src,
Robert Phillipse78b7252017-04-06 07:59:41 -0400632 int left, int top, int width, int height,
633 GrPixelConfig dstConfig, SkColorSpace* dstColorSpace,
634 void* buffer, size_t rowBytes, uint32_t flags) {
Brian Osmanb62ea222016-12-22 11:12:16 -0500635 // TODO: Color space conversion
636
Robert Phillipse78b7252017-04-06 07:59:41 -0400637 ASSERT_SINGLE_OWNER_PRIV
638 RETURN_FALSE_IF_ABANDONED_PRIV
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400639 SkASSERT(src);
640 ASSERT_OWNED_PROXY_PRIV(src->asSurfaceProxy());
Brian Salomondcbb9d92017-07-19 10:53:20 -0400641 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "readSurfacePixels", fContext);
bsalomon32ab2602015-09-09 18:57:49 -0700642
Robert Phillipse78b7252017-04-06 07:59:41 -0400643 // MDB TODO: delay this instantiation until later in the method
Robert Phillips6be756b2018-01-16 15:07:54 -0500644 if (!src->asSurfaceProxy()->instantiate(this->resourceProvider())) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400645 return false;
646 }
647
Robert Phillips16d8ec62017-07-27 16:16:25 -0400648 GrSurfaceProxy* srcProxy = src->asSurfaceProxy();
649 GrSurface* srcSurface = srcProxy->priv().peekSurface();
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400650
Brian Osmand2ca59a2017-04-13 14:03:57 -0400651 // The src is premul but the dst is unpremul -> unpremul the src after or as part of the read
652 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
Robert Phillips16d8ec62017-07-27 16:16:25 -0400653 if (!valid_pixel_conversion(srcProxy->config(), dstConfig, unpremul)) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400654 return false;
655 }
656
Brian Osman409e74f2017-04-17 11:48:28 -0400657 // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data,
658 // without any color spaces attached, and the caller wants us to unpremul.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400659 bool useConfigConversionEffect =
Brian Salomonf3569f02017-10-24 12:52:33 -0400660 unpremul &&
661 pm_upm_must_round_trip(srcProxy->config(), src->colorSpaceInfo().colorSpace()) &&
662 pm_upm_must_round_trip(dstConfig, dstColorSpace);
Brian Osman409e74f2017-04-17 11:48:28 -0400663
664 // Are we going to try to unpremul as part of a draw? For the non-legacy case, we always allow
665 // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly.
666 bool unpremulOnGpu = unpremul &&
667 (!useConfigConversionEffect || fContext->validPMUPMConversionExists());
bsalomon6c6f6582015-09-10 08:12:46 -0700668
bsalomone8d21e82015-07-16 08:23:13 -0700669 // Adjust the params so that if we wind up using an intermediate surface we've already done
670 // all the trimming and the temporary can be the min size required.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400671 if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(),
bsalomone8d21e82015-07-16 08:23:13 -0700672 GrBytesPerPixel(dstConfig), &left,
673 &top, &width, &height, &buffer, &rowBytes)) {
674 return false;
675 }
676
Brian Osman409e74f2017-04-17 11:48:28 -0400677 GrGpu::DrawPreference drawPreference = unpremulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference
678 : GrGpu::kNoDraw_DrawPreference;
bsalomon39826022015-07-23 08:07:21 -0700679 GrGpu::ReadPixelTempDrawInfo tempDrawInfo;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400680 if (!fContext->fGpu->getReadPixelsInfo(srcSurface, srcProxy->origin(), width, height, rowBytes,
Robert Phillips16d8ec62017-07-27 16:16:25 -0400681 dstConfig, &drawPreference, &tempDrawInfo)) {
bsalomon39826022015-07-23 08:07:21 -0700682 return false;
683 }
bsalomon191bcc02014-11-14 11:31:13 -0800684
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400685 if (!(kDontFlush_PixelOpsFlag & flags) && srcSurface->surfacePriv().hasPendingWrite()) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400686 this->flush(nullptr); // MDB TODO: tighten this
687 }
688
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400689 sk_sp<GrSurfaceProxy> proxyToRead = src->asSurfaceProxyRef();
bsalomon39826022015-07-23 08:07:21 -0700690 bool didTempDraw = false;
691 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomonb117ff12016-07-19 07:24:40 -0700692 if (SkBackingFit::kExact == tempDrawInfo.fTempSurfaceFit) {
bsalomon39826022015-07-23 08:07:21 -0700693 // We only respect this when the entire src is being read. Otherwise we can trigger too
694 // many odd ball texture sizes and trash the cache.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400695 if (width != srcSurface->width() || height != srcSurface->height()) {
bsalomonb117ff12016-07-19 07:24:40 -0700696 tempDrawInfo.fTempSurfaceFit= SkBackingFit::kApprox;
bsalomon39826022015-07-23 08:07:21 -0700697 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000698 }
brianosmandfe4f2e2016-07-21 13:28:36 -0700699 // TODO: Need to decide the semantics of this function for color spaces. Do we support
700 // conversion to a passed-in color space? For now, specifying nullptr means that this
701 // path will do no conversion, so it will match the behavior of the non-draw path.
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400702 sk_sp<GrRenderTargetContext> tempRTC = fContext->makeDeferredRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400703 tempDrawInfo.fTempSurfaceFit,
bsalomonb117ff12016-07-19 07:24:40 -0700704 tempDrawInfo.fTempSurfaceDesc.fWidth,
705 tempDrawInfo.fTempSurfaceDesc.fHeight,
706 tempDrawInfo.fTempSurfaceDesc.fConfig,
brianosmandfe4f2e2016-07-21 13:28:36 -0700707 nullptr,
bsalomonb117ff12016-07-19 07:24:40 -0700708 tempDrawInfo.fTempSurfaceDesc.fSampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -0400709 GrMipMapped::kNo,
bsalomonb117ff12016-07-19 07:24:40 -0700710 tempDrawInfo.fTempSurfaceDesc.fOrigin);
Brian Osman693a5402016-10-27 15:13:22 -0400711 if (tempRTC) {
Robert Phillips67c18d62017-01-20 12:44:06 -0500712 SkMatrix textureMatrix = SkMatrix::MakeTrans(SkIntToScalar(left), SkIntToScalar(top));
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400713 sk_sp<GrTextureProxy> proxy = src->asTextureProxyRef();
Brian Osman2240be92017-10-18 13:15:13 -0400714 auto fp = GrSimpleTextureEffect::Make(std::move(proxy), textureMatrix);
Brian Osman409e74f2017-04-17 11:48:28 -0400715 if (unpremulOnGpu) {
716 fp = fContext->createPMToUPMEffect(std::move(fp), useConfigConversionEffect);
717 // We no longer need to do this on CPU after the read back.
718 unpremul = false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000719 }
Brian Osman409e74f2017-04-17 11:48:28 -0400720 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400721 if (!fp) {
722 return false;
723 }
Brian Osman60cd57e2017-04-06 10:19:06 -0400724
Brian Osman409e74f2017-04-17 11:48:28 -0400725 GrPaint paint;
726 paint.addColorFragmentProcessor(std::move(fp));
727 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
728 paint.setAllowSRGBInputs(true);
729 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
730 tempRTC->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect,
731 nullptr);
732 proxyToRead = tempRTC->asTextureProxyRef();
733 left = 0;
734 top = 0;
735 didTempDraw = true;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000736 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000737 }
joshualitt5c55fef2014-10-31 14:04:35 -0700738
Robert Phillipse78b7252017-04-06 07:59:41 -0400739 if (!proxyToRead) {
740 return false;
741 }
742
bsalomon39826022015-07-23 08:07:21 -0700743 if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000744 return false;
745 }
bsalomon39826022015-07-23 08:07:21 -0700746 GrPixelConfig configToRead = dstConfig;
747 if (didTempDraw) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400748 this->flushSurfaceWrites(proxyToRead.get());
bsalomon6c9cd552016-01-22 07:17:34 -0800749 configToRead = tempDrawInfo.fReadConfig;
bsalomon39826022015-07-23 08:07:21 -0700750 }
Robert Phillips09dfc472017-09-13 15:25:47 -0400751
Robert Phillips6be756b2018-01-16 15:07:54 -0500752 if (!proxyToRead->instantiate(this->resourceProvider())) {
Robert Phillips09dfc472017-09-13 15:25:47 -0400753 return false;
754 }
755
756 GrSurface* surfaceToRead = proxyToRead->priv().peekSurface();
757
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400758 if (!fContext->fGpu->readPixels(surfaceToRead, proxyToRead->origin(),
Robert Phillips16d8ec62017-07-27 16:16:25 -0400759 left, top, width, height, configToRead, buffer, rowBytes)) {
bsalomon39826022015-07-23 08:07:21 -0700760 return false;
761 }
762
763 // Perform umpremul conversion if we weren't able to perform it as a draw.
764 if (unpremul) {
Matt Sarettc7b29082017-02-09 16:22:39 -0500765 SkColorType colorType;
766 if (!GrPixelConfigToColorType(dstConfig, &colorType) ||
767 4 != SkColorTypeBytesPerPixel(colorType))
768 {
reed@google.com7111d462014-03-25 16:20:24 +0000769 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000770 }
reed@google.com7111d462014-03-25 16:20:24 +0000771
Matt Sarettc7b29082017-02-09 16:22:39 -0500772 for (int y = 0; y < height; y++) {
773 SkUnpremultiplyRow<false>((uint32_t*) buffer, (const uint32_t*) buffer, width);
774 buffer = SkTAddOffset<void>(buffer, rowBytes);
775 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000776 }
777 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000778}
779
Robert Phillips7ee385e2017-03-30 08:02:11 -0400780void GrContextPriv::prepareSurfaceForExternalIO(GrSurfaceProxy* proxy) {
781 ASSERT_SINGLE_OWNER_PRIV
782 RETURN_IF_ABANDONED_PRIV
783 SkASSERT(proxy);
784 ASSERT_OWNED_PROXY_PRIV(proxy);
Greg Daniel51316782017-08-02 15:10:09 +0000785 fContext->fDrawingManager->prepareSurfaceForExternalIO(proxy, 0, nullptr);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000786}
787
Robert Phillips7ee385e2017-03-30 08:02:11 -0400788void GrContextPriv::flushSurfaceWrites(GrSurfaceProxy* proxy) {
789 ASSERT_SINGLE_OWNER_PRIV
790 RETURN_IF_ABANDONED_PRIV
791 SkASSERT(proxy);
792 ASSERT_OWNED_PROXY_PRIV(proxy);
793 if (proxy->priv().hasPendingWrite()) {
794 this->flush(proxy);
bsalomonf80bfed2014-10-07 05:56:02 -0700795 }
796}
797
Robert Phillips7ee385e2017-03-30 08:02:11 -0400798void GrContextPriv::flushSurfaceIO(GrSurfaceProxy* proxy) {
799 ASSERT_SINGLE_OWNER_PRIV
800 RETURN_IF_ABANDONED_PRIV
801 SkASSERT(proxy);
802 ASSERT_OWNED_PROXY_PRIV(proxy);
803 if (proxy->priv().hasPendingIO()) {
804 this->flush(proxy);
ajuma95243eb2016-08-24 08:19:02 -0700805 }
806}
807
bsalomon@google.com27847de2011-02-22 20:59:41 +0000808////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000809int GrContext::getRecommendedSampleCount(GrPixelConfig config,
810 SkScalar dpi) const {
joshualitt1de610a2016-01-06 08:26:09 -0800811 ASSERT_SINGLE_OWNER
812
bsalomon76228632015-05-29 08:02:10 -0700813 if (!this->caps()->isConfigRenderable(config, true)) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000814 return 0;
815 }
816 int chosenSampleCount = 0;
jvanverthe9c0fc62015-04-29 11:18:05 -0700817 if (fGpu->caps()->shaderCaps()->pathRenderingSupport()) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000818 if (dpi >= 250.0f) {
819 chosenSampleCount = 4;
820 } else {
821 chosenSampleCount = 16;
822 }
823 }
Greg Daniel81e7bf82017-07-19 14:47:42 -0400824 int supportedSampleCount = fGpu->caps()->getSampleCount(chosenSampleCount, config);
825 return chosenSampleCount <= supportedSampleCount ? supportedSampleCount : 0;
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000826}
827
Robert Phillips2c862492017-01-18 10:08:39 -0500828sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy> proxy,
829 sk_sp<SkColorSpace> colorSpace) {
Brian Osman45580d32016-11-23 09:37:01 -0500830 ASSERT_SINGLE_OWNER_PRIV
831
Brian Osman45580d32016-11-23 09:37:01 -0500832 if (proxy->asRenderTargetProxy()) {
Robert Phillips2c862492017-01-18 10:08:39 -0500833 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
834 std::move(colorSpace), nullptr);
Brian Osman45580d32016-11-23 09:37:01 -0500835 } else {
836 SkASSERT(proxy->asTextureProxy());
Robert Phillips2c862492017-01-18 10:08:39 -0500837 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Brian Osman45580d32016-11-23 09:37:01 -0500838 }
839}
840
Robert Phillipse2f7d182016-12-15 09:23:05 -0500841sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(const GrSurfaceDesc& dstDesc,
Greg Daniel65c7f662017-10-30 13:39:09 -0400842 GrMipMapped mipMapped,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500843 SkBackingFit fit,
844 SkBudgeted isDstBudgeted) {
845
Greg Daniel65c7f662017-10-30 13:39:09 -0400846 sk_sp<GrTextureProxy> proxy;
847 if (GrMipMapped::kNo == mipMapped) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500848 proxy = this->proxyProvider()->createProxy(dstDesc, fit, isDstBudgeted);
Greg Daniel65c7f662017-10-30 13:39:09 -0400849 } else {
850 SkASSERT(SkBackingFit::kExact == fit);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500851 proxy = this->proxyProvider()->createMipMapProxy(dstDesc, isDstBudgeted);
Greg Daniel65c7f662017-10-30 13:39:09 -0400852 }
Robert Phillips77b3f322017-01-31 18:24:12 -0500853 if (!proxy) {
854 return nullptr;
855 }
Robert Phillipse2f7d182016-12-15 09:23:05 -0500856
Robert Phillips2c862492017-01-18 10:08:39 -0500857 return this->makeWrappedSurfaceContext(std::move(proxy), nullptr);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500858}
859
Brian Salomond17f6582017-07-19 18:28:58 -0400860sk_sp<GrTextureContext> GrContextPriv::makeBackendTextureContext(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000861 GrSurfaceOrigin origin,
Brian Osmanc1e37052017-03-09 14:19:20 -0500862 sk_sp<SkColorSpace> colorSpace) {
Robert Phillips26caf892017-01-27 10:58:31 -0500863 ASSERT_SINGLE_OWNER_PRIV
864
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500865 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->createWrappedTextureProxy(tex, origin);
Robert Phillips77b3f322017-01-31 18:24:12 -0500866 if (!proxy) {
867 return nullptr;
868 }
Robert Phillips26caf892017-01-27 10:58:31 -0500869
Brian Salomond17f6582017-07-19 18:28:58 -0400870 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Robert Phillips26caf892017-01-27 10:58:31 -0500871}
872
Brian Osman11052242016-10-27 14:47:55 -0400873sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000874 const GrBackendTexture& tex,
875 GrSurfaceOrigin origin,
876 int sampleCnt,
Brian Osman11052242016-10-27 14:47:55 -0400877 sk_sp<SkColorSpace> colorSpace,
Brian Osmanc1e37052017-03-09 14:19:20 -0500878 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700879 ASSERT_SINGLE_OWNER_PRIV
robertphillips4fd74ae2016-08-03 14:26:53 -0700880
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500881 sk_sp<GrTextureProxy> proxy(this->proxyProvider()->createWrappedTextureProxy(tex, origin,
882 sampleCnt));
Robert Phillips77b3f322017-01-31 18:24:12 -0500883 if (!proxy) {
884 return nullptr;
885 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400886
Robert Phillips37430132016-11-09 06:50:43 -0500887 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Brian Osman11052242016-10-27 14:47:55 -0400888 std::move(colorSpace), props);
robertphillips4fd74ae2016-08-03 14:26:53 -0700889}
890
Brian Osman11052242016-10-27 14:47:55 -0400891sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext(
Greg Danielbcf612b2017-05-01 13:50:58 +0000892 const GrBackendRenderTarget& backendRT,
893 GrSurfaceOrigin origin,
robertphillips4fd74ae2016-08-03 14:26:53 -0700894 sk_sp<SkColorSpace> colorSpace,
895 const SkSurfaceProps* surfaceProps) {
896 ASSERT_SINGLE_OWNER_PRIV
897
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500898 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->createWrappedRenderTargetProxy(backendRT,
899 origin);
Robert Phillips77b3f322017-01-31 18:24:12 -0500900 if (!proxy) {
901 return nullptr;
902 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400903
Robert Phillips37430132016-11-09 06:50:43 -0500904 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400905 std::move(colorSpace),
Brian Osman11052242016-10-27 14:47:55 -0400906 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -0700907}
908
Brian Osman11052242016-10-27 14:47:55 -0400909sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000910 const GrBackendTexture& tex,
911 GrSurfaceOrigin origin,
912 int sampleCnt,
robertphillips4fd74ae2016-08-03 14:26:53 -0700913 sk_sp<SkColorSpace> colorSpace,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500914 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700915 ASSERT_SINGLE_OWNER_PRIV
robertphillips4fd74ae2016-08-03 14:26:53 -0700916
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500917 sk_sp<GrSurfaceProxy> proxy(this->proxyProvider()->createWrappedRenderTargetProxy(tex, origin,
918 sampleCnt));
Robert Phillips77b3f322017-01-31 18:24:12 -0500919 if (!proxy) {
920 return nullptr;
921 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400922
Robert Phillips37430132016-11-09 06:50:43 -0500923 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400924 std::move(colorSpace),
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500925 props);
robertphillips77a2e522015-10-17 07:43:27 -0700926}
927
Chris Daltonfe199b72017-05-05 11:26:15 -0400928void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
929 fContext->fDrawingManager->addOnFlushCallbackObject(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400930}
931
932
robertphillips48fde9c2016-09-06 05:20:20 -0700933static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) {
Brian Osman78f20e02017-01-12 10:28:01 -0500934 switch (config) {
935 case kAlpha_8_GrPixelConfig:
936 case kRGB_565_GrPixelConfig:
937 case kRGBA_4444_GrPixelConfig:
938 case kBGRA_8888_GrPixelConfig:
939 return kRGBA_8888_GrPixelConfig;
940 case kSBGRA_8888_GrPixelConfig:
941 return kSRGBA_8888_GrPixelConfig;
942 case kAlpha_half_GrPixelConfig:
943 return kRGBA_half_GrPixelConfig;
944 default:
945 return kUnknown_GrPixelConfig;
946 }
robertphillips48fde9c2016-09-06 05:20:20 -0700947}
948
robertphillipsd728f0c2016-11-21 11:05:03 -0800949sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContextWithFallback(
950 SkBackingFit fit,
951 int width, int height,
952 GrPixelConfig config,
953 sk_sp<SkColorSpace> colorSpace,
954 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -0400955 GrMipMapped mipMapped,
robertphillipsd728f0c2016-11-21 11:05:03 -0800956 GrSurfaceOrigin origin,
957 const SkSurfaceProps* surfaceProps,
958 SkBudgeted budgeted) {
959 if (!this->caps()->isConfigRenderable(config, sampleCnt > 0)) {
960 config = GrPixelConfigFallback(config);
961 }
962
963 return this->makeDeferredRenderTargetContext(fit, width, height, config, std::move(colorSpace),
Greg Daniel45d63032017-10-30 13:41:26 -0400964 sampleCnt, mipMapped, origin, surfaceProps,
Greg Daniele1da1d92017-10-06 15:59:27 -0400965 budgeted);
robertphillipsd728f0c2016-11-21 11:05:03 -0800966}
967
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400968sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContext(
969 SkBackingFit fit,
970 int width, int height,
971 GrPixelConfig config,
972 sk_sp<SkColorSpace> colorSpace,
973 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -0400974 GrMipMapped mipMapped,
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400975 GrSurfaceOrigin origin,
976 const SkSurfaceProps* surfaceProps,
977 SkBudgeted budgeted) {
Brian Salomon79e4d1b2017-07-13 11:17:11 -0400978 if (this->abandoned()) {
979 return nullptr;
980 }
981
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400982 GrSurfaceDesc desc;
983 desc.fFlags = kRenderTarget_GrSurfaceFlag;
984 desc.fOrigin = origin;
985 desc.fWidth = width;
986 desc.fHeight = height;
987 desc.fConfig = config;
988 desc.fSampleCnt = sampleCnt;
989
Greg Daniele1da1d92017-10-06 15:59:27 -0400990 sk_sp<GrTextureProxy> rtp;
Greg Daniel45d63032017-10-30 13:41:26 -0400991 if (GrMipMapped::kNo == mipMapped) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500992 rtp = fProxyProvider->createProxy(desc, fit, budgeted);
Greg Daniele1da1d92017-10-06 15:59:27 -0400993 } else {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500994 rtp = fProxyProvider->createMipMapProxy(desc, budgeted);
Greg Daniele1da1d92017-10-06 15:59:27 -0400995 }
Robert Phillips08c5ec72017-01-30 12:26:47 -0500996 if (!rtp) {
997 return nullptr;
998 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400999
Robert Phillips1119dc32017-04-11 12:54:57 -04001000 sk_sp<GrRenderTargetContext> renderTargetContext(
1001 fDrawingManager->makeRenderTargetContext(std::move(rtp),
1002 std::move(colorSpace),
1003 surfaceProps));
Robert Phillips1119dc32017-04-11 12:54:57 -04001004 if (!renderTargetContext) {
1005 return nullptr;
1006 }
1007
1008 renderTargetContext->discard();
1009
1010 return renderTargetContext;
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001011}
1012
joshualitt1de610a2016-01-06 08:26:09 -08001013bool GrContext::abandoned() const {
1014 ASSERT_SINGLE_OWNER
robertphillips7761d612016-05-16 09:14:53 -07001015 return fDrawingManager->wasAbandoned();
robertphillips77a2e522015-10-17 07:43:27 -07001016}
1017
Brian Salomonaff329b2017-08-11 09:40:37 -04001018std::unique_ptr<GrFragmentProcessor> GrContext::createPMToUPMEffect(
1019 std::unique_ptr<GrFragmentProcessor> fp, bool useConfigConversionEffect) {
Robert Phillips757914d2017-01-25 15:48:30 -05001020 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -04001021 // We have specialized effects that guarantee round-trip conversion for some formats
1022 if (useConfigConversionEffect) {
1023 // We should have already called this->validPMUPMConversionExists() in this case
1024 SkASSERT(fDidTestPMConversions);
1025 // ...and it should have succeeded
1026 SkASSERT(this->validPMUPMConversionExists());
1027
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001028 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul);
Brian Osman2d2da4f2017-04-12 17:07:22 -04001029 } else {
1030 // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and
1031 // explicitly round the results. Just do the obvious, naive thing in the shader.
1032 return GrFragmentProcessor::UnpremulOutput(std::move(fp));
Robert Phillips757914d2017-01-25 15:48:30 -05001033 }
1034}
1035
Brian Salomonaff329b2017-08-11 09:40:37 -04001036std::unique_ptr<GrFragmentProcessor> GrContext::createUPMToPMEffect(
1037 std::unique_ptr<GrFragmentProcessor> fp, bool useConfigConversionEffect) {
joshualitt1de610a2016-01-06 08:26:09 -08001038 ASSERT_SINGLE_OWNER
Brian Osman2d2da4f2017-04-12 17:07:22 -04001039 // We have specialized effects that guarantee round-trip conversion for these formats
Brian Osman409e74f2017-04-17 11:48:28 -04001040 if (useConfigConversionEffect) {
1041 // We should have already called this->validPMUPMConversionExists() in this case
1042 SkASSERT(fDidTestPMConversions);
1043 // ...and it should have succeeded
1044 SkASSERT(this->validPMUPMConversionExists());
1045
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001046 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
Brian Osman2d2da4f2017-04-12 17:07:22 -04001047 } else {
1048 // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and
1049 // explicitly round the results. Just do the obvious, naive thing in the shader.
1050 return GrFragmentProcessor::PremulOutput(std::move(fp));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001051 }
1052}
1053
Brian Osman409e74f2017-04-17 11:48:28 -04001054bool GrContext::validPMUPMConversionExists() {
joshualitt1de610a2016-01-06 08:26:09 -08001055 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -04001056 if (!fDidTestPMConversions) {
Brian Osman28804f32017-04-20 10:24:36 -04001057 fPMUPMConversionsRoundTrip = GrConfigConversionEffect::TestForPreservingPMConversions(this);
Brian Osman409e74f2017-04-17 11:48:28 -04001058 fDidTestPMConversions = true;
1059 }
1060
bsalomon636e8022015-07-29 06:08:46 -07001061 // The PM<->UPM tests fail or succeed together so we only need to check one.
Brian Osman28804f32017-04-20 10:24:36 -04001062 return fPMUPMConversionsRoundTrip;
bsalomon636e8022015-07-29 06:08:46 -07001063}
1064
bsalomon37f9a262015-02-02 13:00:10 -08001065//////////////////////////////////////////////////////////////////////////////
1066
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001067void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -08001068 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001069 if (maxResources) {
1070 *maxResources = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -08001071 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001072 if (maxResourceBytes) {
1073 *maxResourceBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -08001074 }
1075}
1076
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001077void GrContext::setResourceCacheLimits(int maxResources, size_t maxResourceBytes) {
joshualitt1de610a2016-01-06 08:26:09 -08001078 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001079 fResourceCache->setLimits(maxResources, maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -08001080}
1081
ericrk0a5fa482015-09-15 14:16:10 -07001082//////////////////////////////////////////////////////////////////////////////
1083
1084void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -08001085 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -07001086 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
1087}
Brian Osman71a18892017-08-10 10:23:25 -04001088
1089//////////////////////////////////////////////////////////////////////////////
1090
1091SkString GrContext::dump() const {
1092 SkDynamicMemoryWStream stream;
1093 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
1094 writer.beginObject();
1095
1096 static const char* kBackendStr[] = {
1097 "Metal",
1098 "OpenGL",
1099 "Vulkan",
1100 "Mock",
1101 };
1102 GR_STATIC_ASSERT(0 == kMetal_GrBackend);
1103 GR_STATIC_ASSERT(1 == kOpenGL_GrBackend);
1104 GR_STATIC_ASSERT(2 == kVulkan_GrBackend);
1105 GR_STATIC_ASSERT(3 == kMock_GrBackend);
1106 writer.appendString("backend", kBackendStr[fBackend]);
1107
1108 writer.appendName("caps");
1109 fCaps->dumpJSON(&writer);
1110
1111 writer.appendName("gpu");
1112 fGpu->dumpJSON(&writer);
1113
1114 // Flush JSON to the memory stream
1115 writer.endObject();
1116 writer.flush();
1117
1118 // Null terminate the JSON data in the memory stream
1119 stream.write8(0);
1120
1121 // Allocate a string big enough to hold all the data, then copy out of the stream
1122 SkString result(stream.bytesWritten());
1123 stream.copyToAndReset(result.writable_str());
1124 return result;
1125}