blob: c13cd539c2ec20a6fe38580163b9af296aca70bd [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
bsalomon682c2692015-05-22 14:01:46 -070065GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext) {
66 GrContextOptions defaultOptions;
67 return Create(backend, backendContext, defaultOptions);
68}
bsalomonf28cff72015-05-22 12:25:41 -070069
bsalomon682c2692015-05-22 14:01:46 -070070GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
71 const GrContextOptions& options) {
bsalomon682c2692015-05-22 14:01:46 -070072
Robert Phillipse42edcc2017-12-13 11:50:22 -050073 sk_sp<GrContext> context(new GrContext(backend));
74
75 context->fGpu = GrGpu::Make(backend, backendContext, options, context.get());
76 if (!context->fGpu) {
halcanary96fcdcc2015-08-27 07:41:13 -070077 return nullptr;
bsalomon@google.com27847de2011-02-22 20:59:41 +000078 }
Robert Phillipse42edcc2017-12-13 11:50:22 -050079
80 if (!context->init(options)) {
81 return nullptr;
82 }
83
Brian Salomon91a3e522017-06-23 10:58:19 -040084 return context.release();
bsalomon@google.com27847de2011-02-22 20:59:41 +000085}
86
Brian Salomon384fab42017-12-07 12:33:05 -050087sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface) {
Greg Daniel02611d92017-07-25 10:05:01 -040088 GrContextOptions defaultOptions;
Brian Salomon384fab42017-12-07 12:33:05 -050089 return MakeGL(std::move(interface), defaultOptions);
Greg Daniel02611d92017-07-25 10:05:01 -040090}
91
Brian Salomon384fab42017-12-07 12:33:05 -050092sk_sp<GrContext> GrContext::MakeGL(sk_sp<const GrGLInterface> interface,
Greg Daniel02611d92017-07-25 10:05:01 -040093 const GrContextOptions& options) {
Robert Phillipse42edcc2017-12-13 11:50:22 -050094 sk_sp<GrContext> context(new GrContext(kOpenGL_GrBackend));
95
Brian Salomon384fab42017-12-07 12:33:05 -050096 context->fGpu = GrGLGpu::Make(std::move(interface), options, context.get());
Greg Daniel02611d92017-07-25 10:05:01 -040097 if (!context->fGpu) {
98 return nullptr;
99 }
Greg Daniel02611d92017-07-25 10:05:01 -0400100 if (!context->init(options)) {
101 return nullptr;
102 }
103 return context;
104}
105
Brian Salomon990014d2017-12-07 16:23:39 -0500106sk_sp<GrContext> GrContext::MakeGL(const GrGLInterface* interface) {
107 return MakeGL(sk_ref_sp(interface));
108}
109
110sk_sp<GrContext> GrContext::MakeGL(const GrGLInterface* interface,
111 const GrContextOptions& options) {
112 return MakeGL(sk_ref_sp(interface), options);
113}
114
Greg Daniel02611d92017-07-25 10:05:01 -0400115sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions) {
116 GrContextOptions defaultOptions;
117 return MakeMock(mockOptions, defaultOptions);
118}
119
120sk_sp<GrContext> GrContext::MakeMock(const GrMockOptions* mockOptions,
121 const GrContextOptions& options) {
Robert Phillipse42edcc2017-12-13 11:50:22 -0500122 sk_sp<GrContext> context(new GrContext(kMock_GrBackend));
123
Brian Salomon384fab42017-12-07 12:33:05 -0500124 context->fGpu = GrMockGpu::Make(mockOptions, options, context.get());
Greg Daniel02611d92017-07-25 10:05:01 -0400125 if (!context->fGpu) {
126 return nullptr;
127 }
Greg Daniel02611d92017-07-25 10:05:01 -0400128 if (!context->init(options)) {
129 return nullptr;
130 }
131 return context;
132}
133
134#ifdef SK_VULKAN
Brian Salomon384fab42017-12-07 12:33:05 -0500135sk_sp<GrContext> GrContext::MakeVulkan(sk_sp<const GrVkBackendContext> backendContext) {
Greg Daniel02611d92017-07-25 10:05:01 -0400136 GrContextOptions defaultOptions;
Brian Salomon384fab42017-12-07 12:33:05 -0500137 return MakeVulkan(std::move(backendContext), defaultOptions);
Greg Daniel02611d92017-07-25 10:05:01 -0400138}
139
Brian Salomon384fab42017-12-07 12:33:05 -0500140sk_sp<GrContext> GrContext::MakeVulkan(sk_sp<const GrVkBackendContext> backendContext,
Greg Daniel02611d92017-07-25 10:05:01 -0400141 const GrContextOptions& options) {
Robert Phillipse42edcc2017-12-13 11:50:22 -0500142 sk_sp<GrContext> context(new GrContext(kVulkan_GrBackend));
143
Brian Salomon384fab42017-12-07 12:33:05 -0500144 context->fGpu = GrVkGpu::Make(std::move(backendContext), 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#endif
154
Greg Danielb76a72a2017-07-13 15:07:54 -0400155#ifdef SK_METAL
Greg Daniel02611d92017-07-25 10:05:01 -0400156sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue) {
157 GrContextOptions defaultOptions;
158 return MakeMetal(device, queue, defaultOptions);
159}
160
Greg Danielb76a72a2017-07-13 15:07:54 -0400161sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) {
Robert Phillipse42edcc2017-12-13 11:50:22 -0500162 sk_sp<GrContext> context(new GrContext(kMetal_GrBackend));
163
Brian Salomon384fab42017-12-07 12:33:05 -0500164 context->fGpu = GrMtlTrampoline::MakeGpu(context.get(), options, device, queue);
Greg Danielb76a72a2017-07-13 15:07:54 -0400165 if (!context->fGpu) {
166 return nullptr;
167 }
Greg Danielb76a72a2017-07-13 15:07:54 -0400168 if (!context->init(options)) {
169 return nullptr;
170 }
171 return context;
172}
173#endif
174
joshualitt0acd0d32015-05-07 08:23:19 -0700175static int32_t gNextID = 1;
176static int32_t next_id() {
177 int32_t id;
178 do {
179 id = sk_atomic_inc(&gNextID);
180 } while (id == SK_InvalidGenID);
181 return id;
182}
183
Robert Phillipse42edcc2017-12-13 11:50:22 -0500184sk_sp<GrContext> GrContextPriv::MakeDDL(GrContextThreadSafeProxy* proxy) {
185 sk_sp<GrContext> context(new GrContext(proxy));
186
187 context->fGpu = GrDDLGpu::Make(context.get(), proxy->fCaps);
188 if (!context->fGpu) {
189 return nullptr;
190 }
191 if (!context->init(proxy->fOptions)) {
192 return nullptr;
193 }
194 return context;
195}
196
197GrContext::GrContext(GrBackend backend)
198 : fUniqueID(next_id())
199 , fBackend(backend) {
halcanary96fcdcc2015-08-27 07:41:13 -0700200 fResourceCache = nullptr;
201 fResourceProvider = nullptr;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500202 fProxyProvider = nullptr;
Brian Salomonf856fd12016-12-16 14:24:34 -0500203 fAtlasGlyphCache = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000204}
205
Robert Phillipse42edcc2017-12-13 11:50:22 -0500206GrContext::GrContext(GrContextThreadSafeProxy* proxy)
207 : fUniqueID(proxy->fContextUniqueID)
208 , fBackend(proxy->fBackend) {
209 fResourceCache = nullptr;
210 fResourceProvider = nullptr;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500211 fProxyProvider = nullptr;
Robert Phillipse42edcc2017-12-13 11:50:22 -0500212 fAtlasGlyphCache = nullptr;
Greg Danielb76a72a2017-07-13 15:07:54 -0400213}
joshualitt1de610a2016-01-06 08:26:09 -0800214
Greg Danielb76a72a2017-07-13 15:07:54 -0400215bool GrContext::init(const GrContextOptions& options) {
216 ASSERT_SINGLE_OWNER
Robert Phillipse42edcc2017-12-13 11:50:22 -0500217 fCaps = fGpu->refCaps();
Brian Salomoneace8cd2017-12-08 13:11:11 -0500218 fResourceCache = new GrResourceCache(fCaps.get(), fUniqueID);
Brian Salomon384fab42017-12-07 12:33:05 -0500219 fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, &fSingleOwner);
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500220 fProxyProvider = new GrProxyProvider(fResourceProvider, fResourceCache, fCaps, &fSingleOwner);
221 fResourceCache->setProxyProvider(fProxyProvider);
222
Robert Phillipse42edcc2017-12-13 11:50:22 -0500223 // DDL TODO: we need to think through how the task group & persistent cache
224 // get passed on to/shared between all the DDLRecorders created with this context.
225 fThreadSafeProxy.reset(new GrContextThreadSafeProxy(fCaps, this->uniqueID(), fBackend,
226 options));
commit-bot@chromium.org1836d332013-07-16 22:55:03 +0000227
Brian Osman46da1cc2017-02-14 14:15:48 -0500228 fDisableGpuYUVConversion = options.fDisableGpuYUVConversion;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000229 fDidTestPMConversions = false;
230
bsalomon6b2552f2016-09-15 13:50:26 -0700231 GrPathRendererChain::Options prcOptions;
bsalomon39ef7fb2016-09-21 11:16:05 -0700232 prcOptions.fAllowPathMaskCaching = options.fAllowPathMaskCaching;
Brian Osman195c05b2017-08-30 15:14:04 -0400233#if GR_TEST_UTILS
csmartdalton008b9d82017-02-22 12:00:42 -0700234 prcOptions.fGpuPathRenderers = options.fGpuPathRenderers;
Brian Osman195c05b2017-08-30 15:14:04 -0400235#endif
Brian Osmanb350ae22017-08-29 16:15:39 -0400236 if (options.fDisableDistanceFieldPaths) {
Brian Osman195c05b2017-08-30 15:14:04 -0400237 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
Brian Osmanb350ae22017-08-29 16:15:39 -0400238 }
Brian Salomonaf597482017-11-07 16:23:34 -0500239
240 GrAtlasTextContext::Options atlasTextContextOptions;
241 atlasTextContextOptions.fMaxDistanceFieldFontSize = options.fGlyphsAsPathsFontSize;
242 atlasTextContextOptions.fMinDistanceFieldFontSize = options.fMinDistanceFieldFontSize;
Brian Salomonb5086962017-12-13 10:59:33 -0500243 atlasTextContextOptions.fDistanceFieldVerticesAlwaysHaveW = false;
244#if SK_SUPPORT_ATLAS_TEXT
245 if (GrContextOptions::Enable::kYes == options.fDistanceFieldGlyphVerticesAlwaysHaveW) {
246 atlasTextContextOptions.fDistanceFieldVerticesAlwaysHaveW = true;
247 }
248#endif
Brian Salomonaf597482017-11-07 16:23:34 -0500249
250 fDrawingManager.reset(
251 new GrDrawingManager(this, prcOptions, atlasTextContextOptions, &fSingleOwner));
joshualitt7c3a2f82015-03-31 13:32:05 -0700252
Brian Salomon9f545bc2017-11-06 10:36:57 -0500253 GrDrawOpAtlas::AllowMultitexturing allowMultitexturing;
Jim Van Verthc02cb8a2017-11-20 22:11:30 +0000254 switch (options.fAllowMultipleGlyphCacheTextures) {
255 case GrContextOptions::Enable::kDefault:
256#ifdef SK_BUILD_FOR_IOS
257 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
258#else
259 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
260#endif
261 break;
262 case GrContextOptions::Enable::kNo:
263 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kNo;
264 break;
265 case GrContextOptions::Enable::kYes:
266 allowMultitexturing = GrDrawOpAtlas::AllowMultitexturing::kYes;
267 break;
Brian Salomon9f545bc2017-11-06 10:36:57 -0500268 }
269 fAtlasGlyphCache = new GrAtlasGlyphCache(this, options.fGlyphCacheTextureMaximumBytes,
270 allowMultitexturing);
Jim Van Verth106b5c42017-09-26 12:45:29 -0400271 this->contextPriv().addOnFlushCallbackObject(fAtlasGlyphCache);
joshualittb7133be2015-04-08 09:08:31 -0700272
Jim Van Verth474d6872017-12-14 13:00:05 -0500273 fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB, this, this->uniqueID()));
Brian Salomon91a3e522017-06-23 10:58:19 -0400274
Brian Osman51279982017-08-23 10:12:00 -0400275 if (options.fExecutor) {
276 fTaskGroup = skstd::make_unique<SkTaskGroup>(*options.fExecutor);
277 }
278
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400279 fPersistentCache = options.fPersistentCache;
280
Brian Salomon91a3e522017-06-23 10:58:19 -0400281 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000282}
283
bsalomon@google.com27847de2011-02-22 20:59:41 +0000284GrContext::~GrContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800285 ASSERT_SINGLE_OWNER
286
robertphillipsea461502015-05-26 11:38:03 -0700287 if (!fGpu) {
bsalomon76228632015-05-29 08:02:10 -0700288 SkASSERT(!fCaps);
bsalomon@google.com733c0622013-04-24 17:59:32 +0000289 return;
290 }
291
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000292 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000293
robertphillips77a2e522015-10-17 07:43:27 -0700294 fDrawingManager->cleanup();
robertphillips2334fb62015-06-17 05:43:33 -0700295
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000296 for (int i = 0; i < fCleanUpData.count(); ++i) {
297 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
298 }
299
halcanary385fe4d2015-08-26 13:07:48 -0700300 delete fResourceProvider;
301 delete fResourceCache;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500302 delete fProxyProvider;
Brian Salomonf856fd12016-12-16 14:24:34 -0500303 delete fAtlasGlyphCache;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000304}
305
bungeman6bd52842016-10-27 09:30:08 -0700306sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
bungeman6bd52842016-10-27 09:30:08 -0700307 return fThreadSafeProxy;
bsalomon41b952c2016-03-11 06:46:33 -0800308}
309
bsalomon2354f842014-07-28 13:48:36 -0700310void GrContext::abandonContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800311 ASSERT_SINGLE_OWNER
312
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500313 fProxyProvider->abandon();
bsalomond309e7a2015-04-30 14:18:54 -0700314 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800315
316 // Need to abandon the drawing manager first so all the render targets
317 // will be released/forgotten before they too are abandoned.
318 fDrawingManager->abandon();
319
bsalomon@google.com205d4602011-04-25 12:43:45 +0000320 // abandon first to so destructors
321 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800322 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700323
bsalomon6e2aad42016-04-01 11:54:31 -0700324 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
325
Brian Salomonf856fd12016-12-16 14:24:34 -0500326 fAtlasGlyphCache->freeAll();
bsalomon6e2aad42016-04-01 11:54:31 -0700327 fTextBlobCache->freeAll();
328}
329
330void GrContext::releaseResourcesAndAbandonContext() {
331 ASSERT_SINGLE_OWNER
332
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500333 fProxyProvider->abandon();
bsalomon6e2aad42016-04-01 11:54:31 -0700334 fResourceProvider->abandon();
335
336 // Need to abandon the drawing manager first so all the render targets
337 // will be released/forgotten before they too are abandoned.
338 fDrawingManager->abandon();
339
340 // Release all resources in the backend 3D API.
341 fResourceCache->releaseAll();
342
343 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000344
Brian Salomonf856fd12016-12-16 14:24:34 -0500345 fAtlasGlyphCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700346 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000347}
348
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000349void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800350 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000351 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000352}
353
354void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800355 ASSERT_SINGLE_OWNER
356
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000357 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000358
Brian Salomonf856fd12016-12-16 14:24:34 -0500359 fAtlasGlyphCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700360
361 fDrawingManager->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700362
363 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000364}
365
Jim Van Verth76d917c2017-12-13 09:26:37 -0500366void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) {
Brian Salomon5e150852017-03-22 14:53:13 -0400367 ASSERT_SINGLE_OWNER
Jim Van Verth76d917c2017-12-13 09:26:37 -0500368 fResourceCache->purgeAsNeeded();
369 fResourceCache->purgeResourcesNotUsedSince(GrStdSteadyClock::now() - msNotUsed);
370
371 fTextBlobCache->purgeStaleBlobs();
Brian Salomon5e150852017-03-22 14:53:13 -0400372}
373
Derek Sollenberger5480a182017-05-25 16:43:59 -0400374void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
375 ASSERT_SINGLE_OWNER
376 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
377}
378
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000379void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800380 ASSERT_SINGLE_OWNER
381
bsalomon71cb0c22014-11-14 12:10:14 -0800382 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800383 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800384 }
385 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800386 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800387 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000388}
389
Derek Sollenbergeree479142017-05-24 11:41:33 -0400390size_t GrContext::getResourceCachePurgeableBytes() const {
391 ASSERT_SINGLE_OWNER
392 return fResourceCache->getPurgeableBytes();
393}
394
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000395////////////////////////////////////////////////////////////////////////////////
396
joshualitt0db6dfa2015-04-10 07:01:30 -0700397void GrContext::TextBlobCacheOverBudgetCB(void* data) {
398 SkASSERT(data);
Brian Osman11052242016-10-27 14:47:55 -0400399 // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on
400 // GrRenderTargetContext to perform a necessary flush. The solution is to move drawText calls
401 // to below the GrContext level, but this is not trivial because they call drawPath on
402 // SkGpuDevice.
joshualitt0db6dfa2015-04-10 07:01:30 -0700403 GrContext* context = reinterpret_cast<GrContext*>(data);
404 context->flush();
405}
406
bsalomon@google.com27847de2011-02-22 20:59:41 +0000407////////////////////////////////////////////////////////////////////////////////
408
bsalomonb77a9072016-09-07 10:02:04 -0700409void GrContext::flush() {
joshualitt1de610a2016-01-06 08:26:09 -0800410 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700411 RETURN_IF_ABANDONED
Robert Phillips7ee385e2017-03-30 08:02:11 -0400412
413 fDrawingManager->flush(nullptr);
414}
415
Greg Daniel51316782017-08-02 15:10:09 +0000416GrSemaphoresSubmitted GrContext::flushAndSignalSemaphores(int numSemaphores,
417 GrBackendSemaphore signalSemaphores[]) {
418 ASSERT_SINGLE_OWNER
419 if (fDrawingManager->wasAbandoned()) { return GrSemaphoresSubmitted::kNo; }
420
421 return fDrawingManager->flush(nullptr, numSemaphores, signalSemaphores);
422}
423
Robert Phillips7ee385e2017-03-30 08:02:11 -0400424void GrContextPriv::flush(GrSurfaceProxy* proxy) {
425 ASSERT_SINGLE_OWNER_PRIV
426 RETURN_IF_ABANDONED_PRIV
427 ASSERT_OWNED_PROXY_PRIV(proxy);
428
429 fContext->fDrawingManager->flush(proxy);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000430}
431
bsalomon81beccc2014-10-13 12:32:55 -0700432bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
433 const void* inPixels, size_t outRowBytes, void* outPixels) {
Matt Sarettc7b29082017-02-09 16:22:39 -0500434 SkColorType colorType;
435 if (!GrPixelConfigToColorType(srcConfig, &colorType) ||
436 4 != SkColorTypeBytesPerPixel(colorType))
437 {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000438 return false;
439 }
bsalomon81beccc2014-10-13 12:32:55 -0700440
Matt Sarettc7b29082017-02-09 16:22:39 -0500441 for (int y = 0; y < height; y++) {
442 SkOpts::RGBA_to_rgbA((uint32_t*) outPixels, inPixels, width);
443 outPixels = SkTAddOffset<void>(outPixels, outRowBytes);
444 inPixels = SkTAddOffset<const void>(inPixels, inRowBytes);
445 }
bsalomon81beccc2014-10-13 12:32:55 -0700446
Matt Sarettc7b29082017-02-09 16:22:39 -0500447 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000448}
449
Brian Osmand2ca59a2017-04-13 14:03:57 -0400450static bool valid_premul_config(GrPixelConfig config) {
Brian Osmance425512017-03-22 14:37:50 -0400451 return GrPixelConfigIs8888Unorm(config) || kRGBA_half_GrPixelConfig == config;
452}
453
Brian Osmand2ca59a2017-04-13 14:03:57 -0400454static bool valid_pixel_conversion(GrPixelConfig srcConfig, GrPixelConfig dstConfig,
455 bool premulConversion) {
456 // We don't allow conversion between integer configs and float/fixed configs.
457 if (GrPixelConfigIsSint(srcConfig) != GrPixelConfigIsSint(dstConfig)) {
458 return false;
459 }
460
461 // We only allow premul <-> unpremul conversions for some formats
462 if (premulConversion && (!valid_premul_config(srcConfig) || !valid_premul_config(dstConfig))) {
463 return false;
464 }
465
466 return true;
467}
468
Brian Osman409e74f2017-04-17 11:48:28 -0400469static bool pm_upm_must_round_trip(GrPixelConfig config, SkColorSpace* colorSpace) {
470 return !colorSpace &&
471 (kRGBA_8888_GrPixelConfig == config || kBGRA_8888_GrPixelConfig == config);
472}
473
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400474bool GrContextPriv::writeSurfacePixels(GrSurfaceContext* dst,
Robert Phillipse78b7252017-04-06 07:59:41 -0400475 int left, int top, int width, int height,
476 GrPixelConfig srcConfig, SkColorSpace* srcColorSpace,
477 const void* buffer, size_t rowBytes,
478 uint32_t pixelOpsFlags) {
Brian Osmanb62ea222016-12-22 11:12:16 -0500479 // TODO: Color space conversion
480
Robert Phillipse78b7252017-04-06 07:59:41 -0400481 ASSERT_SINGLE_OWNER_PRIV
482 RETURN_FALSE_IF_ABANDONED_PRIV
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400483 SkASSERT(dst);
484 ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy());
Brian Salomondcbb9d92017-07-19 10:53:20 -0400485 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "writeSurfacePixels", fContext);
bsalomon6c6f6582015-09-10 08:12:46 -0700486
Robert Phillips6be756b2018-01-16 15:07:54 -0500487 if (!dst->asSurfaceProxy()->instantiate(this->resourceProvider())) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400488 return false;
489 }
490
Robert Phillips16d8ec62017-07-27 16:16:25 -0400491 GrSurfaceProxy* dstProxy = dst->asSurfaceProxy();
492 GrSurface* dstSurface = dstProxy->priv().peekSurface();
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400493
Brian Osmand2ca59a2017-04-13 14:03:57 -0400494 // 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 -0400495 const bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
Robert Phillips16d8ec62017-07-27 16:16:25 -0400496 if (!valid_pixel_conversion(srcConfig, dstProxy->config(), premul)) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400497 return false;
498 }
499
Brian Osman409e74f2017-04-17 11:48:28 -0400500 // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data,
501 // without any color spaces attached, and the caller wants us to premul.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400502 bool useConfigConversionEffect =
Brian Salomonf3569f02017-10-24 12:52:33 -0400503 premul && pm_upm_must_round_trip(srcConfig, srcColorSpace) &&
504 pm_upm_must_round_trip(dstProxy->config(), dst->colorSpaceInfo().colorSpace());
Brian Osman409e74f2017-04-17 11:48:28 -0400505
506 // Are we going to try to premul as part of a draw? For the non-legacy case, we always allow
507 // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly.
508 bool premulOnGpu = premul &&
509 (!useConfigConversionEffect || fContext->validPMUPMConversionExists());
bsalomon81beccc2014-10-13 12:32:55 -0700510
bsalomone8d21e82015-07-16 08:23:13 -0700511 // 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 -0700512 // necessary and because GrGpu::getWritePixelsInfo requires it.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400513 if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(),
bsalomone8d21e82015-07-16 08:23:13 -0700514 GrBytesPerPixel(srcConfig), &left, &top, &width,
515 &height, &buffer, &rowBytes)) {
516 return false;
517 }
518
Brian Osman409e74f2017-04-17 11:48:28 -0400519 GrGpu::DrawPreference drawPreference = premulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference
520 : GrGpu::kNoDraw_DrawPreference;
bsalomonf0674512015-07-28 13:26:15 -0700521 GrGpu::WritePixelTempDrawInfo tempDrawInfo;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400522 if (!fContext->fGpu->getWritePixelsInfo(dstSurface, dstProxy->origin(), width, height,
Robert Phillips16d8ec62017-07-27 16:16:25 -0400523 srcConfig, &drawPreference, &tempDrawInfo)) {
bsalomonf0674512015-07-28 13:26:15 -0700524 return false;
525 }
526
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400527 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && dstSurface->surfacePriv().hasPendingIO()) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400528 this->flush(nullptr); // MDB TODO: tighten this
bsalomonf0674512015-07-28 13:26:15 -0700529 }
530
Robert Phillips2f493142017-03-02 18:18:38 -0500531 sk_sp<GrTextureProxy> tempProxy;
bsalomonf0674512015-07-28 13:26:15 -0700532 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500533 tempProxy = this->proxyProvider()->createProxy(tempDrawInfo.fTempSurfaceDesc,
534 SkBackingFit::kApprox,
535 SkBudgeted::kYes);
Robert Phillips2f493142017-03-02 18:18:38 -0500536 if (!tempProxy && GrGpu::kRequireDraw_DrawPreference == drawPreference) {
bsalomonf0674512015-07-28 13:26:15 -0700537 return false;
538 }
539 }
540
541 // temp buffer for doing sw premul conversion, if needed.
542 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
Brian Osman409e74f2017-04-17 11:48:28 -0400543 // We need to do sw premul if we were unable to create a RT for drawing, or if we can't do the
544 // premul on the GPU
545 if (premul && (!tempProxy || !premulOnGpu)) {
546 size_t tmpRowBytes = 4 * width;
547 tmpPixels.reset(width * height);
548 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
549 tmpPixels.get())) {
550 return false;
bsalomonf0674512015-07-28 13:26:15 -0700551 }
Brian Osman409e74f2017-04-17 11:48:28 -0400552 rowBytes = tmpRowBytes;
553 buffer = tmpPixels.get();
bsalomonf0674512015-07-28 13:26:15 -0700554 }
Brian Osman409e74f2017-04-17 11:48:28 -0400555
556 if (tempProxy) {
Brian Osman2240be92017-10-18 13:15:13 -0400557 auto fp = GrSimpleTextureEffect::Make(tempProxy, SkMatrix::I());
Brian Osman409e74f2017-04-17 11:48:28 -0400558 if (premulOnGpu) {
559 fp = fContext->createUPMToPMEffect(std::move(fp), useConfigConversionEffect);
bsalomon81beccc2014-10-13 12:32:55 -0700560 }
Brian Osman409e74f2017-04-17 11:48:28 -0400561 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400562 if (!fp) {
563 return false;
564 }
Brian Osman409e74f2017-04-17 11:48:28 -0400565
Robert Phillips6be756b2018-01-16 15:07:54 -0500566 if (!tempProxy->instantiate(this->resourceProvider())) {
Brian Osman409e74f2017-04-17 11:48:28 -0400567 return false;
568 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400569 GrTexture* texture = tempProxy->priv().peekTexture();
Robert Phillips7bbbf622017-10-17 07:36:59 -0400570
571 if (tempProxy->priv().hasPendingIO()) {
572 this->flush(tempProxy.get());
573 }
574
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400575 if (!fContext->fGpu->writePixels(texture, tempProxy->origin(), 0, 0, width, height,
576 tempDrawInfo.fWriteConfig, buffer, rowBytes)) {
Brian Osman409e74f2017-04-17 11:48:28 -0400577 return false;
578 }
Robert Phillips7bbbf622017-10-17 07:36:59 -0400579 tempProxy = nullptr;
580
Brian Osman409e74f2017-04-17 11:48:28 -0400581 SkMatrix matrix;
582 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400583 GrRenderTargetContext* renderTargetContext = dst->asRenderTargetContext();
Brian Osman409e74f2017-04-17 11:48:28 -0400584 if (!renderTargetContext) {
585 return false;
586 }
587 GrPaint paint;
588 paint.addColorFragmentProcessor(std::move(fp));
589 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomonf3569f02017-10-24 12:52:33 -0400590 paint.setAllowSRGBInputs(dst->colorSpaceInfo().isGammaCorrect() ||
591 GrPixelConfigIsSRGB(dst->colorSpaceInfo().config()));
Brian Osman409e74f2017-04-17 11:48:28 -0400592 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
593 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, matrix, rect,
594 nullptr);
595
596 if (kFlushWrites_PixelOp & pixelOpsFlags) {
597 this->flushSurfaceWrites(renderTargetContext->asRenderTargetProxy());
598 }
599 } else {
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400600 return fContext->fGpu->writePixels(dstSurface, dstProxy->origin(), left, top, width,
601 height, srcConfig, buffer, rowBytes);
bsalomon81beccc2014-10-13 12:32:55 -0700602 }
bsalomon81beccc2014-10-13 12:32:55 -0700603 return true;
604}
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000605
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400606bool GrContextPriv::readSurfacePixels(GrSurfaceContext* src,
Robert Phillipse78b7252017-04-06 07:59:41 -0400607 int left, int top, int width, int height,
608 GrPixelConfig dstConfig, SkColorSpace* dstColorSpace,
609 void* buffer, size_t rowBytes, uint32_t flags) {
Brian Osmanb62ea222016-12-22 11:12:16 -0500610 // TODO: Color space conversion
611
Robert Phillipse78b7252017-04-06 07:59:41 -0400612 ASSERT_SINGLE_OWNER_PRIV
613 RETURN_FALSE_IF_ABANDONED_PRIV
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400614 SkASSERT(src);
615 ASSERT_OWNED_PROXY_PRIV(src->asSurfaceProxy());
Brian Salomondcbb9d92017-07-19 10:53:20 -0400616 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "readSurfacePixels", fContext);
bsalomon32ab2602015-09-09 18:57:49 -0700617
Robert Phillipse78b7252017-04-06 07:59:41 -0400618 // MDB TODO: delay this instantiation until later in the method
Robert Phillips6be756b2018-01-16 15:07:54 -0500619 if (!src->asSurfaceProxy()->instantiate(this->resourceProvider())) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400620 return false;
621 }
622
Robert Phillips16d8ec62017-07-27 16:16:25 -0400623 GrSurfaceProxy* srcProxy = src->asSurfaceProxy();
624 GrSurface* srcSurface = srcProxy->priv().peekSurface();
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400625
Brian Osmand2ca59a2017-04-13 14:03:57 -0400626 // The src is premul but the dst is unpremul -> unpremul the src after or as part of the read
627 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
Robert Phillips16d8ec62017-07-27 16:16:25 -0400628 if (!valid_pixel_conversion(srcProxy->config(), dstConfig, unpremul)) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400629 return false;
630 }
631
Brian Osman409e74f2017-04-17 11:48:28 -0400632 // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data,
633 // without any color spaces attached, and the caller wants us to unpremul.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400634 bool useConfigConversionEffect =
Brian Salomonf3569f02017-10-24 12:52:33 -0400635 unpremul &&
636 pm_upm_must_round_trip(srcProxy->config(), src->colorSpaceInfo().colorSpace()) &&
637 pm_upm_must_round_trip(dstConfig, dstColorSpace);
Brian Osman409e74f2017-04-17 11:48:28 -0400638
639 // Are we going to try to unpremul as part of a draw? For the non-legacy case, we always allow
640 // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly.
641 bool unpremulOnGpu = unpremul &&
642 (!useConfigConversionEffect || fContext->validPMUPMConversionExists());
bsalomon6c6f6582015-09-10 08:12:46 -0700643
bsalomone8d21e82015-07-16 08:23:13 -0700644 // Adjust the params so that if we wind up using an intermediate surface we've already done
645 // all the trimming and the temporary can be the min size required.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400646 if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(),
bsalomone8d21e82015-07-16 08:23:13 -0700647 GrBytesPerPixel(dstConfig), &left,
648 &top, &width, &height, &buffer, &rowBytes)) {
649 return false;
650 }
651
Brian Osman409e74f2017-04-17 11:48:28 -0400652 GrGpu::DrawPreference drawPreference = unpremulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference
653 : GrGpu::kNoDraw_DrawPreference;
bsalomon39826022015-07-23 08:07:21 -0700654 GrGpu::ReadPixelTempDrawInfo tempDrawInfo;
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400655 if (!fContext->fGpu->getReadPixelsInfo(srcSurface, srcProxy->origin(), width, height, rowBytes,
Robert Phillips16d8ec62017-07-27 16:16:25 -0400656 dstConfig, &drawPreference, &tempDrawInfo)) {
bsalomon39826022015-07-23 08:07:21 -0700657 return false;
658 }
bsalomon191bcc02014-11-14 11:31:13 -0800659
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400660 if (!(kDontFlush_PixelOpsFlag & flags) && srcSurface->surfacePriv().hasPendingWrite()) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400661 this->flush(nullptr); // MDB TODO: tighten this
662 }
663
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400664 sk_sp<GrSurfaceProxy> proxyToRead = src->asSurfaceProxyRef();
bsalomon39826022015-07-23 08:07:21 -0700665 bool didTempDraw = false;
666 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomonb117ff12016-07-19 07:24:40 -0700667 if (SkBackingFit::kExact == tempDrawInfo.fTempSurfaceFit) {
bsalomon39826022015-07-23 08:07:21 -0700668 // We only respect this when the entire src is being read. Otherwise we can trigger too
669 // many odd ball texture sizes and trash the cache.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400670 if (width != srcSurface->width() || height != srcSurface->height()) {
bsalomonb117ff12016-07-19 07:24:40 -0700671 tempDrawInfo.fTempSurfaceFit= SkBackingFit::kApprox;
bsalomon39826022015-07-23 08:07:21 -0700672 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000673 }
brianosmandfe4f2e2016-07-21 13:28:36 -0700674 // TODO: Need to decide the semantics of this function for color spaces. Do we support
675 // conversion to a passed-in color space? For now, specifying nullptr means that this
676 // path will do no conversion, so it will match the behavior of the non-draw path.
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400677 sk_sp<GrRenderTargetContext> tempRTC = fContext->makeDeferredRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400678 tempDrawInfo.fTempSurfaceFit,
bsalomonb117ff12016-07-19 07:24:40 -0700679 tempDrawInfo.fTempSurfaceDesc.fWidth,
680 tempDrawInfo.fTempSurfaceDesc.fHeight,
681 tempDrawInfo.fTempSurfaceDesc.fConfig,
brianosmandfe4f2e2016-07-21 13:28:36 -0700682 nullptr,
bsalomonb117ff12016-07-19 07:24:40 -0700683 tempDrawInfo.fTempSurfaceDesc.fSampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -0400684 GrMipMapped::kNo,
bsalomonb117ff12016-07-19 07:24:40 -0700685 tempDrawInfo.fTempSurfaceDesc.fOrigin);
Brian Osman693a5402016-10-27 15:13:22 -0400686 if (tempRTC) {
Robert Phillips67c18d62017-01-20 12:44:06 -0500687 SkMatrix textureMatrix = SkMatrix::MakeTrans(SkIntToScalar(left), SkIntToScalar(top));
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400688 sk_sp<GrTextureProxy> proxy = src->asTextureProxyRef();
Brian Osman2240be92017-10-18 13:15:13 -0400689 auto fp = GrSimpleTextureEffect::Make(std::move(proxy), textureMatrix);
Brian Osman409e74f2017-04-17 11:48:28 -0400690 if (unpremulOnGpu) {
691 fp = fContext->createPMToUPMEffect(std::move(fp), useConfigConversionEffect);
692 // We no longer need to do this on CPU after the read back.
693 unpremul = false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000694 }
Brian Osman409e74f2017-04-17 11:48:28 -0400695 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400696 if (!fp) {
697 return false;
698 }
Brian Osman60cd57e2017-04-06 10:19:06 -0400699
Brian Osman409e74f2017-04-17 11:48:28 -0400700 GrPaint paint;
701 paint.addColorFragmentProcessor(std::move(fp));
702 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
703 paint.setAllowSRGBInputs(true);
704 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
705 tempRTC->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect,
706 nullptr);
707 proxyToRead = tempRTC->asTextureProxyRef();
708 left = 0;
709 top = 0;
710 didTempDraw = true;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000711 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000712 }
joshualitt5c55fef2014-10-31 14:04:35 -0700713
Robert Phillipse78b7252017-04-06 07:59:41 -0400714 if (!proxyToRead) {
715 return false;
716 }
717
bsalomon39826022015-07-23 08:07:21 -0700718 if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000719 return false;
720 }
bsalomon39826022015-07-23 08:07:21 -0700721 GrPixelConfig configToRead = dstConfig;
722 if (didTempDraw) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400723 this->flushSurfaceWrites(proxyToRead.get());
bsalomon6c9cd552016-01-22 07:17:34 -0800724 configToRead = tempDrawInfo.fReadConfig;
bsalomon39826022015-07-23 08:07:21 -0700725 }
Robert Phillips09dfc472017-09-13 15:25:47 -0400726
Robert Phillips6be756b2018-01-16 15:07:54 -0500727 if (!proxyToRead->instantiate(this->resourceProvider())) {
Robert Phillips09dfc472017-09-13 15:25:47 -0400728 return false;
729 }
730
731 GrSurface* surfaceToRead = proxyToRead->priv().peekSurface();
732
Robert Phillipsb0e93a22017-08-29 08:26:54 -0400733 if (!fContext->fGpu->readPixels(surfaceToRead, proxyToRead->origin(),
Robert Phillips16d8ec62017-07-27 16:16:25 -0400734 left, top, width, height, configToRead, buffer, rowBytes)) {
bsalomon39826022015-07-23 08:07:21 -0700735 return false;
736 }
737
738 // Perform umpremul conversion if we weren't able to perform it as a draw.
739 if (unpremul) {
Matt Sarettc7b29082017-02-09 16:22:39 -0500740 SkColorType colorType;
741 if (!GrPixelConfigToColorType(dstConfig, &colorType) ||
742 4 != SkColorTypeBytesPerPixel(colorType))
743 {
reed@google.com7111d462014-03-25 16:20:24 +0000744 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000745 }
reed@google.com7111d462014-03-25 16:20:24 +0000746
Matt Sarettc7b29082017-02-09 16:22:39 -0500747 for (int y = 0; y < height; y++) {
748 SkUnpremultiplyRow<false>((uint32_t*) buffer, (const uint32_t*) buffer, width);
749 buffer = SkTAddOffset<void>(buffer, rowBytes);
750 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000751 }
752 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000753}
754
Robert Phillips7ee385e2017-03-30 08:02:11 -0400755void GrContextPriv::prepareSurfaceForExternalIO(GrSurfaceProxy* proxy) {
756 ASSERT_SINGLE_OWNER_PRIV
757 RETURN_IF_ABANDONED_PRIV
758 SkASSERT(proxy);
759 ASSERT_OWNED_PROXY_PRIV(proxy);
Greg Daniel51316782017-08-02 15:10:09 +0000760 fContext->fDrawingManager->prepareSurfaceForExternalIO(proxy, 0, nullptr);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000761}
762
Robert Phillips7ee385e2017-03-30 08:02:11 -0400763void GrContextPriv::flushSurfaceWrites(GrSurfaceProxy* proxy) {
764 ASSERT_SINGLE_OWNER_PRIV
765 RETURN_IF_ABANDONED_PRIV
766 SkASSERT(proxy);
767 ASSERT_OWNED_PROXY_PRIV(proxy);
768 if (proxy->priv().hasPendingWrite()) {
769 this->flush(proxy);
bsalomonf80bfed2014-10-07 05:56:02 -0700770 }
771}
772
Robert Phillips7ee385e2017-03-30 08:02:11 -0400773void GrContextPriv::flushSurfaceIO(GrSurfaceProxy* proxy) {
774 ASSERT_SINGLE_OWNER_PRIV
775 RETURN_IF_ABANDONED_PRIV
776 SkASSERT(proxy);
777 ASSERT_OWNED_PROXY_PRIV(proxy);
778 if (proxy->priv().hasPendingIO()) {
779 this->flush(proxy);
ajuma95243eb2016-08-24 08:19:02 -0700780 }
781}
782
bsalomon@google.com27847de2011-02-22 20:59:41 +0000783////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000784int GrContext::getRecommendedSampleCount(GrPixelConfig config,
785 SkScalar dpi) const {
joshualitt1de610a2016-01-06 08:26:09 -0800786 ASSERT_SINGLE_OWNER
787
bsalomon76228632015-05-29 08:02:10 -0700788 if (!this->caps()->isConfigRenderable(config, true)) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000789 return 0;
790 }
791 int chosenSampleCount = 0;
jvanverthe9c0fc62015-04-29 11:18:05 -0700792 if (fGpu->caps()->shaderCaps()->pathRenderingSupport()) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000793 if (dpi >= 250.0f) {
794 chosenSampleCount = 4;
795 } else {
796 chosenSampleCount = 16;
797 }
798 }
Greg Daniel81e7bf82017-07-19 14:47:42 -0400799 int supportedSampleCount = fGpu->caps()->getSampleCount(chosenSampleCount, config);
800 return chosenSampleCount <= supportedSampleCount ? supportedSampleCount : 0;
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000801}
802
Robert Phillips2c862492017-01-18 10:08:39 -0500803sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy> proxy,
804 sk_sp<SkColorSpace> colorSpace) {
Brian Osman45580d32016-11-23 09:37:01 -0500805 ASSERT_SINGLE_OWNER_PRIV
806
Brian Osman45580d32016-11-23 09:37:01 -0500807 if (proxy->asRenderTargetProxy()) {
Robert Phillips2c862492017-01-18 10:08:39 -0500808 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
809 std::move(colorSpace), nullptr);
Brian Osman45580d32016-11-23 09:37:01 -0500810 } else {
811 SkASSERT(proxy->asTextureProxy());
Robert Phillips2c862492017-01-18 10:08:39 -0500812 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Brian Osman45580d32016-11-23 09:37:01 -0500813 }
814}
815
Robert Phillipse2f7d182016-12-15 09:23:05 -0500816sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(const GrSurfaceDesc& dstDesc,
Greg Daniel65c7f662017-10-30 13:39:09 -0400817 GrMipMapped mipMapped,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500818 SkBackingFit fit,
819 SkBudgeted isDstBudgeted) {
820
Greg Daniel65c7f662017-10-30 13:39:09 -0400821 sk_sp<GrTextureProxy> proxy;
822 if (GrMipMapped::kNo == mipMapped) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500823 proxy = this->proxyProvider()->createProxy(dstDesc, fit, isDstBudgeted);
Greg Daniel65c7f662017-10-30 13:39:09 -0400824 } else {
825 SkASSERT(SkBackingFit::kExact == fit);
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500826 proxy = this->proxyProvider()->createMipMapProxy(dstDesc, isDstBudgeted);
Greg Daniel65c7f662017-10-30 13:39:09 -0400827 }
Robert Phillips77b3f322017-01-31 18:24:12 -0500828 if (!proxy) {
829 return nullptr;
830 }
Robert Phillipse2f7d182016-12-15 09:23:05 -0500831
Robert Phillips2c862492017-01-18 10:08:39 -0500832 return this->makeWrappedSurfaceContext(std::move(proxy), nullptr);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500833}
834
Brian Salomond17f6582017-07-19 18:28:58 -0400835sk_sp<GrTextureContext> GrContextPriv::makeBackendTextureContext(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000836 GrSurfaceOrigin origin,
Brian Osmanc1e37052017-03-09 14:19:20 -0500837 sk_sp<SkColorSpace> colorSpace) {
Robert Phillips26caf892017-01-27 10:58:31 -0500838 ASSERT_SINGLE_OWNER_PRIV
839
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500840 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->createWrappedTextureProxy(tex, origin);
Robert Phillips77b3f322017-01-31 18:24:12 -0500841 if (!proxy) {
842 return nullptr;
843 }
Robert Phillips26caf892017-01-27 10:58:31 -0500844
Brian Salomond17f6582017-07-19 18:28:58 -0400845 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Robert Phillips26caf892017-01-27 10:58:31 -0500846}
847
Brian Osman11052242016-10-27 14:47:55 -0400848sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000849 const GrBackendTexture& tex,
850 GrSurfaceOrigin origin,
851 int sampleCnt,
Brian Osman11052242016-10-27 14:47:55 -0400852 sk_sp<SkColorSpace> colorSpace,
Brian Osmanc1e37052017-03-09 14:19:20 -0500853 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700854 ASSERT_SINGLE_OWNER_PRIV
robertphillips4fd74ae2016-08-03 14:26:53 -0700855
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500856 sk_sp<GrTextureProxy> proxy(this->proxyProvider()->createWrappedTextureProxy(tex, origin,
857 sampleCnt));
Robert Phillips77b3f322017-01-31 18:24:12 -0500858 if (!proxy) {
859 return nullptr;
860 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400861
Robert Phillips37430132016-11-09 06:50:43 -0500862 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Brian Osman11052242016-10-27 14:47:55 -0400863 std::move(colorSpace), props);
robertphillips4fd74ae2016-08-03 14:26:53 -0700864}
865
Brian Osman11052242016-10-27 14:47:55 -0400866sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext(
Greg Danielbcf612b2017-05-01 13:50:58 +0000867 const GrBackendRenderTarget& backendRT,
868 GrSurfaceOrigin origin,
robertphillips4fd74ae2016-08-03 14:26:53 -0700869 sk_sp<SkColorSpace> colorSpace,
870 const SkSurfaceProps* surfaceProps) {
871 ASSERT_SINGLE_OWNER_PRIV
872
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500873 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->createWrappedRenderTargetProxy(backendRT,
874 origin);
Robert Phillips77b3f322017-01-31 18:24:12 -0500875 if (!proxy) {
876 return nullptr;
877 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400878
Robert Phillips37430132016-11-09 06:50:43 -0500879 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400880 std::move(colorSpace),
Brian Osman11052242016-10-27 14:47:55 -0400881 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -0700882}
883
Brian Osman11052242016-10-27 14:47:55 -0400884sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000885 const GrBackendTexture& tex,
886 GrSurfaceOrigin origin,
887 int sampleCnt,
robertphillips4fd74ae2016-08-03 14:26:53 -0700888 sk_sp<SkColorSpace> colorSpace,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500889 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700890 ASSERT_SINGLE_OWNER_PRIV
robertphillips4fd74ae2016-08-03 14:26:53 -0700891
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500892 sk_sp<GrSurfaceProxy> proxy(this->proxyProvider()->createWrappedRenderTargetProxy(tex, origin,
893 sampleCnt));
Robert Phillips77b3f322017-01-31 18:24:12 -0500894 if (!proxy) {
895 return nullptr;
896 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400897
Robert Phillips37430132016-11-09 06:50:43 -0500898 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400899 std::move(colorSpace),
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500900 props);
robertphillips77a2e522015-10-17 07:43:27 -0700901}
902
Chris Daltonfe199b72017-05-05 11:26:15 -0400903void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
904 fContext->fDrawingManager->addOnFlushCallbackObject(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400905}
906
907
robertphillips48fde9c2016-09-06 05:20:20 -0700908static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) {
Brian Osman78f20e02017-01-12 10:28:01 -0500909 switch (config) {
910 case kAlpha_8_GrPixelConfig:
911 case kRGB_565_GrPixelConfig:
912 case kRGBA_4444_GrPixelConfig:
913 case kBGRA_8888_GrPixelConfig:
914 return kRGBA_8888_GrPixelConfig;
915 case kSBGRA_8888_GrPixelConfig:
916 return kSRGBA_8888_GrPixelConfig;
917 case kAlpha_half_GrPixelConfig:
918 return kRGBA_half_GrPixelConfig;
919 default:
920 return kUnknown_GrPixelConfig;
921 }
robertphillips48fde9c2016-09-06 05:20:20 -0700922}
923
robertphillipsd728f0c2016-11-21 11:05:03 -0800924sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContextWithFallback(
925 SkBackingFit fit,
926 int width, int height,
927 GrPixelConfig config,
928 sk_sp<SkColorSpace> colorSpace,
929 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -0400930 GrMipMapped mipMapped,
robertphillipsd728f0c2016-11-21 11:05:03 -0800931 GrSurfaceOrigin origin,
932 const SkSurfaceProps* surfaceProps,
933 SkBudgeted budgeted) {
934 if (!this->caps()->isConfigRenderable(config, sampleCnt > 0)) {
935 config = GrPixelConfigFallback(config);
936 }
937
938 return this->makeDeferredRenderTargetContext(fit, width, height, config, std::move(colorSpace),
Greg Daniel45d63032017-10-30 13:41:26 -0400939 sampleCnt, mipMapped, origin, surfaceProps,
Greg Daniele1da1d92017-10-06 15:59:27 -0400940 budgeted);
robertphillipsd728f0c2016-11-21 11:05:03 -0800941}
942
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400943sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContext(
944 SkBackingFit fit,
945 int width, int height,
946 GrPixelConfig config,
947 sk_sp<SkColorSpace> colorSpace,
948 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -0400949 GrMipMapped mipMapped,
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400950 GrSurfaceOrigin origin,
951 const SkSurfaceProps* surfaceProps,
952 SkBudgeted budgeted) {
Brian Salomon79e4d1b2017-07-13 11:17:11 -0400953 if (this->abandoned()) {
954 return nullptr;
955 }
956
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400957 GrSurfaceDesc desc;
958 desc.fFlags = kRenderTarget_GrSurfaceFlag;
959 desc.fOrigin = origin;
960 desc.fWidth = width;
961 desc.fHeight = height;
962 desc.fConfig = config;
963 desc.fSampleCnt = sampleCnt;
964
Greg Daniele1da1d92017-10-06 15:59:27 -0400965 sk_sp<GrTextureProxy> rtp;
Greg Daniel45d63032017-10-30 13:41:26 -0400966 if (GrMipMapped::kNo == mipMapped) {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500967 rtp = fProxyProvider->createProxy(desc, fit, budgeted);
Greg Daniele1da1d92017-10-06 15:59:27 -0400968 } else {
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500969 rtp = fProxyProvider->createMipMapProxy(desc, budgeted);
Greg Daniele1da1d92017-10-06 15:59:27 -0400970 }
Robert Phillips08c5ec72017-01-30 12:26:47 -0500971 if (!rtp) {
972 return nullptr;
973 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400974
Robert Phillips1119dc32017-04-11 12:54:57 -0400975 sk_sp<GrRenderTargetContext> renderTargetContext(
976 fDrawingManager->makeRenderTargetContext(std::move(rtp),
977 std::move(colorSpace),
978 surfaceProps));
Robert Phillips1119dc32017-04-11 12:54:57 -0400979 if (!renderTargetContext) {
980 return nullptr;
981 }
982
983 renderTargetContext->discard();
984
985 return renderTargetContext;
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400986}
987
joshualitt1de610a2016-01-06 08:26:09 -0800988bool GrContext::abandoned() const {
989 ASSERT_SINGLE_OWNER
robertphillips7761d612016-05-16 09:14:53 -0700990 return fDrawingManager->wasAbandoned();
robertphillips77a2e522015-10-17 07:43:27 -0700991}
992
Brian Salomonaff329b2017-08-11 09:40:37 -0400993std::unique_ptr<GrFragmentProcessor> GrContext::createPMToUPMEffect(
994 std::unique_ptr<GrFragmentProcessor> fp, bool useConfigConversionEffect) {
Robert Phillips757914d2017-01-25 15:48:30 -0500995 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -0400996 // We have specialized effects that guarantee round-trip conversion for some formats
997 if (useConfigConversionEffect) {
998 // We should have already called this->validPMUPMConversionExists() in this case
999 SkASSERT(fDidTestPMConversions);
1000 // ...and it should have succeeded
1001 SkASSERT(this->validPMUPMConversionExists());
1002
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001003 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul);
Brian Osman2d2da4f2017-04-12 17:07:22 -04001004 } else {
1005 // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and
1006 // explicitly round the results. Just do the obvious, naive thing in the shader.
1007 return GrFragmentProcessor::UnpremulOutput(std::move(fp));
Robert Phillips757914d2017-01-25 15:48:30 -05001008 }
1009}
1010
Brian Salomonaff329b2017-08-11 09:40:37 -04001011std::unique_ptr<GrFragmentProcessor> GrContext::createUPMToPMEffect(
1012 std::unique_ptr<GrFragmentProcessor> fp, bool useConfigConversionEffect) {
joshualitt1de610a2016-01-06 08:26:09 -08001013 ASSERT_SINGLE_OWNER
Brian Osman2d2da4f2017-04-12 17:07:22 -04001014 // We have specialized effects that guarantee round-trip conversion for these formats
Brian Osman409e74f2017-04-17 11:48:28 -04001015 if (useConfigConversionEffect) {
1016 // We should have already called this->validPMUPMConversionExists() in this case
1017 SkASSERT(fDidTestPMConversions);
1018 // ...and it should have succeeded
1019 SkASSERT(this->validPMUPMConversionExists());
1020
Ethan Nicholasaae47c82017-11-10 15:34:03 -05001021 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
Brian Osman2d2da4f2017-04-12 17:07:22 -04001022 } else {
1023 // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and
1024 // explicitly round the results. Just do the obvious, naive thing in the shader.
1025 return GrFragmentProcessor::PremulOutput(std::move(fp));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001026 }
1027}
1028
Brian Osman409e74f2017-04-17 11:48:28 -04001029bool GrContext::validPMUPMConversionExists() {
joshualitt1de610a2016-01-06 08:26:09 -08001030 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -04001031 if (!fDidTestPMConversions) {
Brian Osman28804f32017-04-20 10:24:36 -04001032 fPMUPMConversionsRoundTrip = GrConfigConversionEffect::TestForPreservingPMConversions(this);
Brian Osman409e74f2017-04-17 11:48:28 -04001033 fDidTestPMConversions = true;
1034 }
1035
bsalomon636e8022015-07-29 06:08:46 -07001036 // The PM<->UPM tests fail or succeed together so we only need to check one.
Brian Osman28804f32017-04-20 10:24:36 -04001037 return fPMUPMConversionsRoundTrip;
bsalomon636e8022015-07-29 06:08:46 -07001038}
1039
bsalomon37f9a262015-02-02 13:00:10 -08001040//////////////////////////////////////////////////////////////////////////////
1041
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001042void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -08001043 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001044 if (maxResources) {
1045 *maxResources = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -08001046 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001047 if (maxResourceBytes) {
1048 *maxResourceBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -08001049 }
1050}
1051
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001052void GrContext::setResourceCacheLimits(int maxResources, size_t maxResourceBytes) {
joshualitt1de610a2016-01-06 08:26:09 -08001053 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001054 fResourceCache->setLimits(maxResources, maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -08001055}
1056
ericrk0a5fa482015-09-15 14:16:10 -07001057//////////////////////////////////////////////////////////////////////////////
1058
1059void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -08001060 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -07001061 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
1062}
Brian Osman71a18892017-08-10 10:23:25 -04001063
1064//////////////////////////////////////////////////////////////////////////////
1065
1066SkString GrContext::dump() const {
1067 SkDynamicMemoryWStream stream;
1068 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
1069 writer.beginObject();
1070
1071 static const char* kBackendStr[] = {
1072 "Metal",
1073 "OpenGL",
1074 "Vulkan",
1075 "Mock",
1076 };
1077 GR_STATIC_ASSERT(0 == kMetal_GrBackend);
1078 GR_STATIC_ASSERT(1 == kOpenGL_GrBackend);
1079 GR_STATIC_ASSERT(2 == kVulkan_GrBackend);
1080 GR_STATIC_ASSERT(3 == kMock_GrBackend);
1081 writer.appendString("backend", kBackendStr[fBackend]);
1082
1083 writer.appendName("caps");
1084 fCaps->dumpJSON(&writer);
1085
1086 writer.appendName("gpu");
1087 fGpu->dumpJSON(&writer);
1088
1089 // Flush JSON to the memory stream
1090 writer.endObject();
1091 writer.flush();
1092
1093 // Null terminate the JSON data in the memory stream
1094 stream.write8(0);
1095
1096 // Allocate a string big enough to hold all the data, then copy out of the stream
1097 SkString result(stream.bytesWritten());
1098 stream.copyToAndReset(result.writable_str());
1099 return result;
1100}