blob: b6c8b930e7296e6971115516ffb660517fb71b0c [file] [log] [blame]
bsalomon@google.com27847de2011-02-22 20:59:41 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
bsalomon@google.com27847de2011-02-22 20:59:41 +00006 */
7
bsalomon@google.com1fadb202011-12-12 16:10:08 +00008#include "GrContext.h"
Brian Salomon5f33a8c2018-02-26 14:32:39 -05009#include "GrBackendSemaphore.h"
Brian Salomonc65aec92017-03-09 09:03:58 -050010#include "GrClip.h"
bsalomon682c2692015-05-22 14:01:46 -070011#include "GrContextOptions.h"
Brian Salomonc65aec92017-03-09 09:03:58 -050012#include "GrContextPriv.h"
robertphillips77a2e522015-10-17 07:43:27 -070013#include "GrDrawingManager.h"
Robert Phillips646e4292017-06-13 12:44:56 -040014#include "GrGpu.h"
Robert Phillipsc994a932018-06-19 13:09:54 -040015#include "GrMemoryPool.h"
Robert Phillips1afd4cd2018-01-08 13:40:32 -050016#include "GrProxyProvider.h"
Brian Osman11052242016-10-27 14:47:55 -040017#include "GrRenderTargetContext.h"
Brian Salomonc65aec92017-03-09 09:03:58 -050018#include "GrRenderTargetProxy.h"
bsalomon0ea80f42015-02-11 10:49:59 -080019#include "GrResourceCache.h"
bsalomond309e7a2015-04-30 14:18:54 -070020#include "GrResourceProvider.h"
Greg Danield85f97d2017-03-07 13:37:21 -050021#include "GrSemaphore.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000022#include "GrSoftwarePathRenderer.h"
Brian Osman45580d32016-11-23 09:37:01 -050023#include "GrSurfaceContext.h"
bsalomonafbf2d62014-09-30 12:18:44 -070024#include "GrSurfacePriv.h"
Robert Phillips757914d2017-01-25 15:48:30 -050025#include "GrSurfaceProxyPriv.h"
Robert Phillips646e4292017-06-13 12:44:56 -040026#include "GrTexture.h"
Brian Osman45580d32016-11-23 09:37:01 -050027#include "GrTextureContext.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040028#include "GrTracing.h"
Brian Salomon19eaf2d2018-03-19 16:06:44 -040029#include "SkAutoPixmapStorage.h"
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -050030#include "SkDeferredDisplayList.h"
Brian Osman3b655982017-03-07 16:58:08 -050031#include "SkGr.h"
Mike Reed7fcfb622018-02-09 13:26:46 -050032#include "SkImageInfoPriv.h"
Brian Osman51279982017-08-23 10:12:00 -040033#include "SkMakeUnique.h"
Robert Phillips6b6fcc72018-03-30 13:57:00 -040034#include "SkSurface_Gpu.h"
Brian Osman51279982017-08-23 10:12:00 -040035#include "SkTaskGroup.h"
Khushal71652e22018-10-29 13:05:36 -070036#include "SkTraceMemoryDump.h"
joshualitt5478d422014-11-14 16:00:38 -080037#include "effects/GrConfigConversionEffect.h"
Ethan Nicholas00543112018-07-31 09:44:36 -040038#include "effects/GrSkSLFP.h"
Chris Dalton6c3879d2018-11-01 11:13:19 -060039#include "ccpr/GrCoverageCountingPathRenderer.h"
Brian Salomon5f33a8c2018-02-26 14:32:39 -050040#include "text/GrTextBlobCache.h"
Mike Klein0ec1c572018-12-04 11:52:51 -050041#include <atomic>
42#include <unordered_map>
Greg Danielb76a72a2017-07-13 15:07:54 -040043
Robert Phillipse78b7252017-04-06 07:59:41 -040044#define ASSERT_OWNED_PROXY(P) \
Brian Salomonfd98c2c2018-07-31 17:25:29 -040045 SkASSERT(!(P) || !((P)->peekTexture()) || (P)->peekTexture()->getContext() == this)
Robert Phillips7ee385e2017-03-30 08:02:11 -040046#define ASSERT_OWNED_PROXY_PRIV(P) \
Brian Salomonfd98c2c2018-07-31 17:25:29 -040047 SkASSERT(!(P) || !((P)->peekTexture()) || (P)->peekTexture()->getContext() == fContext)
Robert Phillips7ee385e2017-03-30 08:02:11 -040048
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000049#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
joshualitt1de610a2016-01-06 08:26:09 -080050#define ASSERT_SINGLE_OWNER \
51 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fSingleOwner);)
robertphillips4fd74ae2016-08-03 14:26:53 -070052#define ASSERT_SINGLE_OWNER_PRIV \
53 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fContext->fSingleOwner);)
robertphillips7761d612016-05-16 09:14:53 -070054#define RETURN_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return; }
Robert Phillips7ee385e2017-03-30 08:02:11 -040055#define RETURN_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return; }
robertphillips7761d612016-05-16 09:14:53 -070056#define RETURN_FALSE_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return false; }
Robert Phillipse78b7252017-04-06 07:59:41 -040057#define RETURN_FALSE_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return false; }
robertphillips7761d612016-05-16 09:14:53 -070058#define RETURN_NULL_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return nullptr; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000059
robertphillipsea461502015-05-26 11:38:03 -070060////////////////////////////////////////////////////////////////////////////////
61
Greg Danielbdf12ad2018-10-12 09:31:11 -040062GrContext::GrContext(GrBackendApi backend, int32_t id)
Robert Phillips4217ea72019-01-30 13:08:28 -050063 : INHERITED(backend, id) {
halcanary96fcdcc2015-08-27 07:41:13 -070064 fResourceCache = nullptr;
65 fResourceProvider = nullptr;
Robert Phillips1afd4cd2018-01-08 13:40:32 -050066 fProxyProvider = nullptr;
Robert Phillipsc4039ea2018-03-01 11:36:45 -050067 fGlyphCache = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000068}
69
Robert Phillipsfde6fa02018-03-02 08:53:14 -050070bool GrContext::initCommon(const GrContextOptions& options) {
Greg Danielb76a72a2017-07-13 15:07:54 -040071 ASSERT_SINGLE_OWNER
Robert Phillipsfde6fa02018-03-02 08:53:14 -050072 SkASSERT(fCaps); // needs to have been initialized by derived classes
73 SkASSERT(fThreadSafeProxy); // needs to have been initialized by derived classes
Robert Phillips88260b52018-01-19 12:56:09 -050074
75 if (fGpu) {
76 fCaps = fGpu->refCaps();
Robert Phillips4217ea72019-01-30 13:08:28 -050077 fResourceCache = new GrResourceCache(fCaps.get(), &fSingleOwner, this->uniqueID());
Robert Phillips4150eea2018-02-07 17:08:21 -050078 fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, &fSingleOwner,
79 options.fExplicitlyAllocateGPUResources);
Brian Salomon238069b2018-07-11 15:58:57 -040080 fProxyProvider =
81 new GrProxyProvider(fResourceProvider, fResourceCache, fCaps, &fSingleOwner);
82 } else {
83 fProxyProvider = new GrProxyProvider(this->uniqueID(), fCaps, &fSingleOwner);
Robert Phillips88260b52018-01-19 12:56:09 -050084 }
85
Robert Phillips88260b52018-01-19 12:56:09 -050086 if (fResourceCache) {
87 fResourceCache->setProxyProvider(fProxyProvider);
88 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050089
Brian Osman46da1cc2017-02-14 14:15:48 -050090 fDisableGpuYUVConversion = options.fDisableGpuYUVConversion;
Brian Osman8a83ca42018-02-12 14:32:17 -050091 fSharpenMipmappedTextures = options.fSharpenMipmappedTextures;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000092 fDidTestPMConversions = false;
93
bsalomon6b2552f2016-09-15 13:50:26 -070094 GrPathRendererChain::Options prcOptions;
bsalomon39ef7fb2016-09-21 11:16:05 -070095 prcOptions.fAllowPathMaskCaching = options.fAllowPathMaskCaching;
Brian Osman195c05b2017-08-30 15:14:04 -040096#if GR_TEST_UTILS
csmartdalton008b9d82017-02-22 12:00:42 -070097 prcOptions.fGpuPathRenderers = options.fGpuPathRenderers;
Brian Osman195c05b2017-08-30 15:14:04 -040098#endif
Chris Daltonef125092018-06-26 18:16:47 -060099 if (options.fDisableCoverageCountingPaths) {
100 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kCoverageCounting;
101 }
Brian Osmanb350ae22017-08-29 16:15:39 -0400102 if (options.fDisableDistanceFieldPaths) {
Brian Osman195c05b2017-08-30 15:14:04 -0400103 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
Brian Osmanb350ae22017-08-29 16:15:39 -0400104 }
Brian Salomonaf597482017-11-07 16:23:34 -0500105
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500106 if (!fResourceCache) {
107 // DDL TODO: remove this crippling of the path renderer chain
108 // Disable the small path renderer bc of the proxies in the atlas. They need to be
109 // unified when the opLists are added back to the destination drawing manager.
110 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
Robert Phillips5ffcb4d2018-07-03 16:39:40 -0400111 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kStencilAndCover;
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500112 }
113
Herb Derby26cbe512018-05-24 14:39:01 -0400114 GrTextContext::Options textContextOptions;
115 textContextOptions.fMaxDistanceFieldFontSize = options.fGlyphsAsPathsFontSize;
116 textContextOptions.fMinDistanceFieldFontSize = options.fMinDistanceFieldFontSize;
117 textContextOptions.fDistanceFieldVerticesAlwaysHaveW = false;
Brian Salomonb5086962017-12-13 10:59:33 -0500118#if SK_SUPPORT_ATLAS_TEXT
119 if (GrContextOptions::Enable::kYes == options.fDistanceFieldGlyphVerticesAlwaysHaveW) {
Herb Derby26cbe512018-05-24 14:39:01 -0400120 textContextOptions.fDistanceFieldVerticesAlwaysHaveW = true;
Brian Salomonb5086962017-12-13 10:59:33 -0500121 }
122#endif
Brian Salomonaf597482017-11-07 16:23:34 -0500123
Robert Phillips64ecdce2018-04-02 10:26:39 -0400124 bool explicitlyAllocatingResources = fResourceProvider
125 ? fResourceProvider->explicitlyAllocateGPUResources()
126 : false;
Herb Derby26cbe512018-05-24 14:39:01 -0400127 fDrawingManager.reset(new GrDrawingManager(this, prcOptions, textContextOptions,
Robert Phillips64ecdce2018-04-02 10:26:39 -0400128 &fSingleOwner, explicitlyAllocatingResources,
Robert Phillips46acf9d2018-10-09 09:31:40 -0400129 options.fSortRenderTargets,
130 options.fReduceOpListSplitting));
joshualitt7c3a2f82015-03-31 13:32:05 -0700131
Herb Derby081e6f32019-01-16 13:46:02 -0500132 fGlyphCache = new GrStrikeCache(fCaps.get(), options.fGlyphCacheTextureMaximumBytes);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500133
Robert Phillips303cd582018-02-14 18:54:01 -0500134 fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB,
Herb Derbyb12175f2018-05-23 16:38:09 -0400135 this, this->uniqueID()));
Brian Salomon91a3e522017-06-23 10:58:19 -0400136
Robert Phillipsfde6fa02018-03-02 08:53:14 -0500137 // DDL TODO: we need to think through how the task group & persistent cache
138 // get passed on to/shared between all the DDLRecorders created with this context.
Brian Osman51279982017-08-23 10:12:00 -0400139 if (options.fExecutor) {
140 fTaskGroup = skstd::make_unique<SkTaskGroup>(*options.fExecutor);
141 }
142
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400143 fPersistentCache = options.fPersistentCache;
144
Brian Salomon91a3e522017-06-23 10:58:19 -0400145 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000146}
147
bsalomon@google.com27847de2011-02-22 20:59:41 +0000148GrContext::~GrContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800149 ASSERT_SINGLE_OWNER
150
Robert Phillips2e6feed2018-01-22 15:27:20 -0500151 if (fDrawingManager) {
152 fDrawingManager->cleanup();
153 }
halcanary385fe4d2015-08-26 13:07:48 -0700154 delete fResourceProvider;
155 delete fResourceCache;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500156 delete fProxyProvider;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500157 delete fGlyphCache;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000158}
159
Robert Phillips4217ea72019-01-30 13:08:28 -0500160sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
161 return fThreadSafeProxy;
162}
163
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400164//////////////////////////////////////////////////////////////////////////////
165
bsalomon2354f842014-07-28 13:48:36 -0700166void GrContext::abandonContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800167 ASSERT_SINGLE_OWNER
168
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500169 fProxyProvider->abandon();
bsalomond309e7a2015-04-30 14:18:54 -0700170 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800171
172 // Need to abandon the drawing manager first so all the render targets
173 // will be released/forgotten before they too are abandoned.
174 fDrawingManager->abandon();
175
bsalomon@google.com205d4602011-04-25 12:43:45 +0000176 // abandon first to so destructors
177 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800178 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700179
bsalomon6e2aad42016-04-01 11:54:31 -0700180 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
181
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500182 fGlyphCache->freeAll();
bsalomon6e2aad42016-04-01 11:54:31 -0700183 fTextBlobCache->freeAll();
184}
185
Khushalc421ca12018-06-26 14:38:34 -0700186bool GrContext::abandoned() const {
187 ASSERT_SINGLE_OWNER
Brian Salomon9bc76d92019-01-24 12:18:33 -0500188 // If called from ~GrContext(), the drawing manager may already be gone.
189 return !fDrawingManager || fDrawingManager->wasAbandoned();
Khushalc421ca12018-06-26 14:38:34 -0700190}
191
bsalomon6e2aad42016-04-01 11:54:31 -0700192void GrContext::releaseResourcesAndAbandonContext() {
193 ASSERT_SINGLE_OWNER
194
Brian Salomon614c1a82018-12-19 15:42:06 -0500195 if (this->abandoned()) {
196 return;
197 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500198 fProxyProvider->abandon();
bsalomon6e2aad42016-04-01 11:54:31 -0700199 fResourceProvider->abandon();
200
201 // Need to abandon the drawing manager first so all the render targets
202 // will be released/forgotten before they too are abandoned.
203 fDrawingManager->abandon();
204
205 // Release all resources in the backend 3D API.
206 fResourceCache->releaseAll();
207
208 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000209
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500210 fGlyphCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700211 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000212}
213
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000214void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800215 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000216 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000217}
218
219void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800220 ASSERT_SINGLE_OWNER
221
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500222 fGlyphCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700223
224 fDrawingManager->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700225
226 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000227}
228
Robert Phillips6eba0632018-03-28 12:25:42 -0400229void GrContext::purgeUnlockedResources(bool scratchResourcesOnly) {
230 ASSERT_SINGLE_OWNER
231 fResourceCache->purgeUnlockedResources(scratchResourcesOnly);
232 fResourceCache->purgeAsNeeded();
233 fTextBlobCache->purgeStaleBlobs();
234}
235
Jim Van Verth76d917c2017-12-13 09:26:37 -0500236void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) {
Brian Salomon5e150852017-03-22 14:53:13 -0400237 ASSERT_SINGLE_OWNER
Chris Dalton6c3879d2018-11-01 11:13:19 -0600238
239 auto purgeTime = GrStdSteadyClock::now() - msNotUsed;
240
Jim Van Verth76d917c2017-12-13 09:26:37 -0500241 fResourceCache->purgeAsNeeded();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600242 fResourceCache->purgeResourcesNotUsedSince(purgeTime);
243
244 if (auto ccpr = fDrawingManager->getCoverageCountingPathRenderer()) {
Chris Dalton351e80c2019-01-06 22:51:00 -0700245 ccpr->purgeCacheEntriesOlderThan(fProxyProvider, purgeTime);
Chris Dalton6c3879d2018-11-01 11:13:19 -0600246 }
Jim Van Verth76d917c2017-12-13 09:26:37 -0500247
248 fTextBlobCache->purgeStaleBlobs();
Brian Salomon5e150852017-03-22 14:53:13 -0400249}
250
Derek Sollenberger5480a182017-05-25 16:43:59 -0400251void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
252 ASSERT_SINGLE_OWNER
253 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
254}
255
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000256void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800257 ASSERT_SINGLE_OWNER
258
bsalomon71cb0c22014-11-14 12:10:14 -0800259 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800260 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800261 }
262 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800263 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800264 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000265}
266
Derek Sollenbergeree479142017-05-24 11:41:33 -0400267size_t GrContext::getResourceCachePurgeableBytes() const {
268 ASSERT_SINGLE_OWNER
269 return fResourceCache->getPurgeableBytes();
270}
271
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000272////////////////////////////////////////////////////////////////////////////////
273
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400274int GrContext::maxTextureSize() const { return fCaps->maxTextureSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400275
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400276int GrContext::maxRenderTargetSize() const { return fCaps->maxRenderTargetSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400277
Brian Salomonbdecacf2018-02-02 20:32:49 -0500278bool GrContext::colorTypeSupportedAsImage(SkColorType colorType) const {
Brian Osman2b23c4b2018-06-01 12:25:08 -0400279 GrPixelConfig config = SkColorType2GrPixelConfig(colorType);
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400280 return fCaps->isConfigTexturable(config);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500281}
282
283int GrContext::maxSurfaceSampleCountForColorType(SkColorType colorType) const {
Brian Osman2b23c4b2018-06-01 12:25:08 -0400284 GrPixelConfig config = SkColorType2GrPixelConfig(colorType);
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400285 return fCaps->maxRenderTargetSampleCount(config);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500286}
287
288////////////////////////////////////////////////////////////////////////////////
289
joshualitt0db6dfa2015-04-10 07:01:30 -0700290void GrContext::TextBlobCacheOverBudgetCB(void* data) {
291 SkASSERT(data);
Brian Osman11052242016-10-27 14:47:55 -0400292 // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on
293 // GrRenderTargetContext to perform a necessary flush. The solution is to move drawText calls
294 // to below the GrContext level, but this is not trivial because they call drawPath on
295 // SkGpuDevice.
joshualitt0db6dfa2015-04-10 07:01:30 -0700296 GrContext* context = reinterpret_cast<GrContext*>(data);
297 context->flush();
298}
299
bsalomon@google.com27847de2011-02-22 20:59:41 +0000300////////////////////////////////////////////////////////////////////////////////
301
bsalomonb77a9072016-09-07 10:02:04 -0700302void GrContext::flush() {
joshualitt1de610a2016-01-06 08:26:09 -0800303 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700304 RETURN_IF_ABANDONED
Robert Phillips7ee385e2017-03-30 08:02:11 -0400305
306 fDrawingManager->flush(nullptr);
307}
308
Greg Daniel51316782017-08-02 15:10:09 +0000309GrSemaphoresSubmitted GrContext::flushAndSignalSemaphores(int numSemaphores,
310 GrBackendSemaphore signalSemaphores[]) {
311 ASSERT_SINGLE_OWNER
312 if (fDrawingManager->wasAbandoned()) { return GrSemaphoresSubmitted::kNo; }
313
314 return fDrawingManager->flush(nullptr, numSemaphores, signalSemaphores);
315}
316
Robert Phillips7ee385e2017-03-30 08:02:11 -0400317void GrContextPriv::flush(GrSurfaceProxy* proxy) {
318 ASSERT_SINGLE_OWNER_PRIV
319 RETURN_IF_ABANDONED_PRIV
320 ASSERT_OWNED_PROXY_PRIV(proxy);
321
322 fContext->fDrawingManager->flush(proxy);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000323}
324
Greg Daniela870b462019-01-08 15:49:46 -0500325////////////////////////////////////////////////////////////////////////////////
326
327void GrContext::storeVkPipelineCacheData() {
328 if (fGpu) {
329 fGpu->storeVkPipelineCacheData();
330 }
331}
332
333////////////////////////////////////////////////////////////////////////////////
334
Brian Salomonc320b152018-02-20 14:05:36 -0500335// TODO: This will be removed when GrSurfaceContexts are aware of their color types.
336// (skbug.com/6718)
Brian Osmand2ca59a2017-04-13 14:03:57 -0400337static bool valid_premul_config(GrPixelConfig config) {
Brian Salomonc320b152018-02-20 14:05:36 -0500338 switch (config) {
339 case kUnknown_GrPixelConfig: return false;
340 case kAlpha_8_GrPixelConfig: return false;
341 case kGray_8_GrPixelConfig: return false;
342 case kRGB_565_GrPixelConfig: return false;
343 case kRGBA_4444_GrPixelConfig: return true;
344 case kRGBA_8888_GrPixelConfig: return true;
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400345 case kRGB_888_GrPixelConfig: return false;
Jim Van Verth69e57852018-12-05 13:38:59 -0500346 case kRG_88_GrPixelConfig: return false;
Brian Salomonc320b152018-02-20 14:05:36 -0500347 case kBGRA_8888_GrPixelConfig: return true;
348 case kSRGBA_8888_GrPixelConfig: return true;
349 case kSBGRA_8888_GrPixelConfig: return true;
Brian Osman10fc6fd2018-03-02 11:01:10 -0500350 case kRGBA_1010102_GrPixelConfig: return true;
Brian Salomonc320b152018-02-20 14:05:36 -0500351 case kRGBA_float_GrPixelConfig: return true;
352 case kRG_float_GrPixelConfig: return false;
353 case kAlpha_half_GrPixelConfig: return false;
354 case kRGBA_half_GrPixelConfig: return true;
Jim Van Verth1676cb92019-01-15 13:24:45 -0500355 case kRGB_ETC1_GrPixelConfig: return false;
Brian Salomonc320b152018-02-20 14:05:36 -0500356 case kAlpha_8_as_Alpha_GrPixelConfig: return false;
357 case kAlpha_8_as_Red_GrPixelConfig: return false;
358 case kAlpha_half_as_Red_GrPixelConfig: return false;
359 case kGray_8_as_Lum_GrPixelConfig: return false;
360 case kGray_8_as_Red_GrPixelConfig: return false;
361 }
362 SK_ABORT("Invalid GrPixelConfig");
363 return false;
Brian Osmance425512017-03-22 14:37:50 -0400364}
365
Brian Salomonc320b152018-02-20 14:05:36 -0500366static bool valid_premul_color_type(GrColorType ct) {
367 switch (ct) {
Brian Osman10fc6fd2018-03-02 11:01:10 -0500368 case GrColorType::kUnknown: return false;
369 case GrColorType::kAlpha_8: return false;
370 case GrColorType::kRGB_565: return false;
371 case GrColorType::kABGR_4444: return true;
372 case GrColorType::kRGBA_8888: return true;
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400373 case GrColorType::kRGB_888x: return false;
Jim Van Verth69e57852018-12-05 13:38:59 -0500374 case GrColorType::kRG_88: return false;
Brian Osman10fc6fd2018-03-02 11:01:10 -0500375 case GrColorType::kBGRA_8888: return true;
376 case GrColorType::kRGBA_1010102: return true;
377 case GrColorType::kGray_8: return false;
378 case GrColorType::kAlpha_F16: return false;
379 case GrColorType::kRGBA_F16: return true;
380 case GrColorType::kRG_F32: return false;
381 case GrColorType::kRGBA_F32: return true;
Jim Van Verth1676cb92019-01-15 13:24:45 -0500382 case GrColorType::kRGB_ETC1: return false;
Brian Salomonc320b152018-02-20 14:05:36 -0500383 }
384 SK_ABORT("Invalid GrColorType");
385 return false;
386}
387
388static bool valid_pixel_conversion(GrColorType cpuColorType, GrPixelConfig gpuConfig,
Brian Osmand2ca59a2017-04-13 14:03:57 -0400389 bool premulConversion) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400390 // We only allow premul <-> unpremul conversions for some formats
Brian Salomonc320b152018-02-20 14:05:36 -0500391 if (premulConversion &&
392 (!valid_premul_color_type(cpuColorType) || !valid_premul_config(gpuConfig))) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400393 return false;
394 }
Brian Osmand2ca59a2017-04-13 14:03:57 -0400395 return true;
396}
397
Brian Salomonc320b152018-02-20 14:05:36 -0500398bool GrContextPriv::writeSurfacePixels(GrSurfaceContext* dst, int left, int top, int width,
399 int height, GrColorType srcColorType,
400 SkColorSpace* srcColorSpace, const void* buffer,
401 size_t rowBytes, uint32_t pixelOpsFlags) {
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500402 ASSERT_SINGLE_OWNER_PRIV
403 RETURN_FALSE_IF_ABANDONED_PRIV
404 SkASSERT(dst);
405 SkASSERT(buffer);
406 ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy());
Brian Salomond494f6e2018-06-01 14:13:43 -0400407 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "writeSurfacePixels", fContext);
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500408
409 if (GrColorType::kUnknown == srcColorType) {
410 return false;
411 }
412
413 if (!dst->asSurfaceProxy()->instantiate(this->resourceProvider())) {
414 return false;
415 }
416
417 GrSurfaceProxy* dstProxy = dst->asSurfaceProxy();
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400418 GrSurface* dstSurface = dstProxy->peekSurface();
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500419
420 if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(),
421 GrColorTypeBytesPerPixel(srcColorType), &left, &top,
422 &width, &height, &buffer, &rowBytes)) {
423 return false;
424 }
425
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400426 // TODO: Make GrSurfaceContext know its alpha type and pass src buffer's alpha type.
427 bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
428
429 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
430 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
Brian Salomon817847c2018-05-07 13:33:50 -0400431 bool canvas2DFastPath =
Greg Daniel4374e962018-09-28 15:09:47 -0400432 !fContext->contextPriv().caps()->avoidWritePixelsFastPath() &&
Brian Salomon817847c2018-05-07 13:33:50 -0400433 premul &&
Brian Osman34ec3742018-07-03 10:40:57 -0400434 !dst->colorSpaceInfo().colorSpace() &&
Brian Salomon817847c2018-05-07 13:33:50 -0400435 (srcColorType == GrColorType::kRGBA_8888 || srcColorType == GrColorType::kBGRA_8888) &&
436 SkToBool(dst->asRenderTargetContext()) &&
437 (dstProxy->config() == kRGBA_8888_GrPixelConfig ||
438 dstProxy->config() == kBGRA_8888_GrPixelConfig) &&
439 !(pixelOpsFlags & kDontFlush_PixelOpsFlag) &&
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400440 fContext->contextPriv().caps()->isConfigTexturable(kRGBA_8888_GrPixelConfig) &&
Brian Salomon817847c2018-05-07 13:33:50 -0400441 fContext->validPMUPMConversionExists();
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400442
Greg Daniel4065d452018-11-16 15:43:41 -0500443 const GrCaps* caps = this->caps();
444 if (!caps->surfaceSupportsWritePixels(dstSurface) ||
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400445 canvas2DFastPath) {
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400446 // We don't expect callers that are skipping flushes to require an intermediate draw.
447 SkASSERT(!(pixelOpsFlags & kDontFlush_PixelOpsFlag));
448 if (pixelOpsFlags & kDontFlush_PixelOpsFlag) {
449 return false;
450 }
451
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500452 GrSurfaceDesc desc;
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500453 desc.fWidth = width;
454 desc.fHeight = height;
455 desc.fSampleCnt = 1;
Greg Daniel4065d452018-11-16 15:43:41 -0500456
457 GrBackendFormat format;
458 if (canvas2DFastPath) {
459 desc.fConfig = kRGBA_8888_GrPixelConfig;
460 format =
461 fContext->contextPriv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
462 } else {
463 desc.fConfig = dstProxy->config();
464 format = dstProxy->backendFormat().makeTexture2D();
465 if (!format.isValid()) {
466 return false;
467 }
468 }
469
Brian Salomon2a4f9832018-03-03 22:43:43 -0500470 auto tempProxy = this->proxyProvider()->createProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500471 format, desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kApprox, SkBudgeted::kYes);
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500472 if (!tempProxy) {
473 return false;
474 }
475 auto tempCtx = this->drawingManager()->makeTextureContext(
476 tempProxy, dst->colorSpaceInfo().refColorSpace());
477 if (!tempCtx) {
478 return false;
479 }
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400480 uint32_t flags = canvas2DFastPath ? 0 : pixelOpsFlags;
Brian Salomon817847c2018-05-07 13:33:50 -0400481 // In the fast path we always write the srcData to the temp context as though it were RGBA.
482 // When the data is really BGRA the write will cause the R and B channels to be swapped in
483 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
484 // dst.
485 auto tmpColorType = canvas2DFastPath ? GrColorType::kRGBA_8888 : srcColorType;
Brian Salomond494f6e2018-06-01 14:13:43 -0400486 if (!this->writeSurfacePixels(tempCtx.get(), 0, 0, width, height, tmpColorType,
487 srcColorSpace, buffer, rowBytes, flags)) {
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500488 return false;
489 }
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400490 if (canvas2DFastPath) {
491 GrPaint paint;
492 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomon817847c2018-05-07 13:33:50 -0400493 auto fp = fContext->createUPMToPMEffect(
Brian Osman5ea96bf2018-10-02 14:58:05 -0400494 GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I()));
Brian Salomon817847c2018-05-07 13:33:50 -0400495 if (srcColorType == GrColorType::kBGRA_8888) {
496 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
497 }
498 if (!fp) {
499 return false;
500 }
501 paint.addColorFragmentProcessor(std::move(fp));
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400502 dst->asRenderTargetContext()->fillRectToRect(
503 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
504 SkRect::MakeXYWH(left, top, width, height), SkRect::MakeWH(width, height));
505 return true;
506 } else {
507 return dst->copy(tempProxy.get(), SkIRect::MakeWH(width, height), {left, top});
508 }
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500509 }
510
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500511 bool convert = premul;
512
513 if (!valid_pixel_conversion(srcColorType, dstProxy->config(), premul)) {
514 return false;
515 }
516
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400517 GrColorType allowedColorType = fContext->contextPriv().caps()->supportedWritePixelsColorType(
518 dstProxy->config(), srcColorType);
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500519 convert = convert || (srcColorType != allowedColorType);
520
521 if (!dst->colorSpaceInfo().colorSpace()) {
522 // "Legacy" mode - no color space conversions.
523 srcColorSpace = nullptr;
524 }
525 convert = convert || !SkColorSpace::Equals(srcColorSpace, dst->colorSpaceInfo().colorSpace());
526
527 std::unique_ptr<char[]> tempBuffer;
528 if (convert) {
529 auto srcSkColorType = GrColorTypeToSkColorType(srcColorType);
530 auto dstSkColorType = GrColorTypeToSkColorType(allowedColorType);
531 if (kUnknown_SkColorType == srcSkColorType || kUnknown_SkColorType == dstSkColorType) {
532 return false;
533 }
Brian Osman2091fbb2018-10-11 11:05:37 -0400534 auto srcAlphaType = SkColorTypeIsAlwaysOpaque(srcSkColorType)
535 ? kOpaque_SkAlphaType
536 : (premul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType);
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500537 SkPixmap src(SkImageInfo::Make(width, height, srcSkColorType, srcAlphaType,
538 sk_ref_sp(srcColorSpace)),
539 buffer, rowBytes);
540 auto tempSrcII = SkImageInfo::Make(width, height, dstSkColorType, kPremul_SkAlphaType,
541 dst->colorSpaceInfo().refColorSpace());
542 auto size = tempSrcII.computeMinByteSize();
543 if (!size) {
544 return false;
545 }
546 tempBuffer.reset(new char[size]);
547 SkPixmap tempSrc(tempSrcII, tempBuffer.get(), tempSrcII.minRowBytes());
548 if (!src.readPixels(tempSrc)) {
549 return false;
550 }
551 srcColorType = allowedColorType;
552 buffer = tempSrc.addr();
553 rowBytes = tempSrc.rowBytes();
554 if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
555 std::unique_ptr<char[]> row(new char[rowBytes]);
556 for (int y = 0; y < height / 2; ++y) {
557 memcpy(row.get(), tempSrc.addr(0, y), rowBytes);
558 memcpy(tempSrc.writable_addr(0, y), tempSrc.addr(0, height - 1 - y), rowBytes);
559 memcpy(tempSrc.writable_addr(0, height - 1 - y), row.get(), rowBytes);
560 }
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400561 top = dstSurface->height() - top - height;
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500562 }
563 } else if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
564 size_t trimRowBytes = GrColorTypeBytesPerPixel(srcColorType) * width;
565 tempBuffer.reset(new char[trimRowBytes * height]);
566 char* dst = reinterpret_cast<char*>(tempBuffer.get()) + trimRowBytes * (height - 1);
567 const char* src = reinterpret_cast<const char*>(buffer);
568 for (int i = 0; i < height; ++i, src += rowBytes, dst -= trimRowBytes) {
569 memcpy(dst, src, trimRowBytes);
570 }
571 buffer = tempBuffer.get();
572 rowBytes = trimRowBytes;
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400573 top = dstSurface->height() - top - height;
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500574 }
575
576 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && dstSurface->surfacePriv().hasPendingIO()) {
577 this->flush(nullptr); // MDB TODO: tighten this
578 }
579
580 return this->getGpu()->writePixels(dstSurface, left, top, width, height, srcColorType, buffer,
581 rowBytes);
582}
583
Brian Salomond494f6e2018-06-01 14:13:43 -0400584bool GrContextPriv::readSurfacePixels(GrSurfaceContext* src, int left, int top, int width,
585 int height, GrColorType dstColorType,
586 SkColorSpace* dstColorSpace, void* buffer, size_t rowBytes,
587 uint32_t pixelOpsFlags) {
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400588 ASSERT_SINGLE_OWNER_PRIV
589 RETURN_FALSE_IF_ABANDONED_PRIV
590 SkASSERT(src);
591 SkASSERT(buffer);
592 ASSERT_OWNED_PROXY_PRIV(src->asSurfaceProxy());
Brian Salomond494f6e2018-06-01 14:13:43 -0400593 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "readSurfacePixels", fContext);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400594
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400595 SkASSERT(!(pixelOpsFlags & kDontFlush_PixelOpsFlag));
596 if (pixelOpsFlags & kDontFlush_PixelOpsFlag) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400597 return false;
598 }
599
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400600 // MDB TODO: delay this instantiation until later in the method
601 if (!src->asSurfaceProxy()->instantiate(this->resourceProvider())) {
602 return false;
603 }
604
605 GrSurfaceProxy* srcProxy = src->asSurfaceProxy();
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400606 GrSurface* srcSurface = srcProxy->peekSurface();
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400607
608 if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(),
609 GrColorTypeBytesPerPixel(dstColorType), &left, &top,
610 &width, &height, &buffer, &rowBytes)) {
611 return false;
612 }
613
614 // TODO: Make GrSurfaceContext know its alpha type and pass dst buffer's alpha type.
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400615 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400616
617 if (!valid_pixel_conversion(dstColorType, srcProxy->config(), unpremul)) {
618 return false;
619 }
620
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400621 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
622 // care so much about getImageData performance. However, in order to ensure putImageData/
623 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
Brian Salomond494f6e2018-06-01 14:13:43 -0400624 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400625 // fContext->vaildaPMUPMConversionExists()).
Brian Salomon817847c2018-05-07 13:33:50 -0400626 bool canvas2DFastPath =
627 unpremul &&
Brian Osman34ec3742018-07-03 10:40:57 -0400628 !src->colorSpaceInfo().colorSpace() &&
Brian Salomon817847c2018-05-07 13:33:50 -0400629 (GrColorType::kRGBA_8888 == dstColorType || GrColorType::kBGRA_8888 == dstColorType) &&
630 SkToBool(srcProxy->asTextureProxy()) &&
631 (srcProxy->config() == kRGBA_8888_GrPixelConfig ||
632 srcProxy->config() == kBGRA_8888_GrPixelConfig) &&
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400633 fContext->contextPriv().caps()->isConfigRenderable(kRGBA_8888_GrPixelConfig) &&
Brian Salomon817847c2018-05-07 13:33:50 -0400634 fContext->validPMUPMConversionExists();
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400635
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400636 if (!fContext->contextPriv().caps()->surfaceSupportsReadPixels(srcSurface) ||
637 canvas2DFastPath) {
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400638 GrSurfaceDesc desc;
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400639 desc.fFlags = canvas2DFastPath ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
640 desc.fConfig = canvas2DFastPath ? kRGBA_8888_GrPixelConfig : srcProxy->config();
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400641 desc.fWidth = width;
642 desc.fHeight = height;
643 desc.fSampleCnt = 1;
Greg Daniel4065d452018-11-16 15:43:41 -0500644
645 GrBackendFormat format;
646 if (canvas2DFastPath) {
647 desc.fFlags = kRenderTarget_GrSurfaceFlag;
648 desc.fConfig = kRGBA_8888_GrPixelConfig;
649 format = this->caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
650 } else {
651 desc.fFlags = kNone_GrSurfaceFlags;
652 desc.fConfig = srcProxy->config();
653 format = srcProxy->backendFormat().makeTexture2D();
654 if (!format.isValid()) {
655 return false;
656 }
657 }
658
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400659 auto tempProxy = this->proxyProvider()->createProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500660 format, desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kApprox, SkBudgeted::kYes);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400661 if (!tempProxy) {
662 return false;
663 }
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400664 sk_sp<GrSurfaceContext> tempCtx;
665 if (canvas2DFastPath) {
666 tempCtx = this->drawingManager()->makeRenderTargetContext(std::move(tempProxy), nullptr,
667 nullptr);
Greg Daniel42314012018-04-23 10:57:37 -0400668 SkASSERT(tempCtx->asRenderTargetContext());
669 tempCtx->asRenderTargetContext()->discard();
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400670 } else {
671 tempCtx = this->drawingManager()->makeTextureContext(
672 std::move(tempProxy), src->colorSpaceInfo().refColorSpace());
673 }
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400674 if (!tempCtx) {
675 return false;
676 }
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400677 if (canvas2DFastPath) {
678 GrPaint paint;
679 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomon817847c2018-05-07 13:33:50 -0400680 auto fp = fContext->createPMToUPMEffect(
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400681 GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()),
Brian Osman5ea96bf2018-10-02 14:58:05 -0400682 SkMatrix::I()));
Brian Salomon817847c2018-05-07 13:33:50 -0400683 if (dstColorType == GrColorType::kBGRA_8888) {
684 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
685 dstColorType = GrColorType::kRGBA_8888;
686 }
687 if (!fp) {
688 return false;
689 }
690 paint.addColorFragmentProcessor(std::move(fp));
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400691 tempCtx->asRenderTargetContext()->fillRectToRect(
692 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
693 SkRect::MakeWH(width, height), SkRect::MakeXYWH(left, top, width, height));
694 } else if (!tempCtx->copy(srcProxy, SkIRect::MakeXYWH(left, top, width, height), {0, 0})) {
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400695 return false;
696 }
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400697 uint32_t flags = canvas2DFastPath ? 0 : pixelOpsFlags;
Brian Salomond494f6e2018-06-01 14:13:43 -0400698 return this->readSurfacePixels(tempCtx.get(), 0, 0, width, height, dstColorType,
699 dstColorSpace, buffer, rowBytes, flags);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400700 }
701
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400702 bool convert = unpremul;
703
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400704 bool flip = srcProxy->origin() == kBottomLeft_GrSurfaceOrigin;
705 if (flip) {
706 top = srcSurface->height() - top - height;
707 }
708
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400709 GrColorType allowedColorType = fContext->contextPriv().caps()->supportedReadPixelsColorType(
710 srcProxy->config(), dstColorType);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400711 convert = convert || (dstColorType != allowedColorType);
712
713 if (!src->colorSpaceInfo().colorSpace()) {
714 // "Legacy" mode - no color space conversions.
715 dstColorSpace = nullptr;
716 }
717 convert = convert || !SkColorSpace::Equals(dstColorSpace, src->colorSpaceInfo().colorSpace());
718
719 SkAutoPixmapStorage tempPixmap;
720 SkPixmap finalPixmap;
721 if (convert) {
722 SkColorType srcSkColorType = GrColorTypeToSkColorType(allowedColorType);
723 SkColorType dstSkColorType = GrColorTypeToSkColorType(dstColorType);
Brian Osman2091fbb2018-10-11 11:05:37 -0400724 bool srcAlwaysOpaque = SkColorTypeIsAlwaysOpaque(srcSkColorType);
725 bool dstAlwaysOpaque = SkColorTypeIsAlwaysOpaque(dstSkColorType);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400726 if (kUnknown_SkColorType == srcSkColorType || kUnknown_SkColorType == dstSkColorType) {
727 return false;
728 }
Brian Osman2091fbb2018-10-11 11:05:37 -0400729 auto tempAT = srcAlwaysOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400730 auto tempII = SkImageInfo::Make(width, height, srcSkColorType, tempAT,
731 src->colorSpaceInfo().refColorSpace());
Brian Osman2091fbb2018-10-11 11:05:37 -0400732 SkASSERT(!unpremul || !dstAlwaysOpaque);
733 auto finalAT = (srcAlwaysOpaque || dstAlwaysOpaque)
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400734 ? kOpaque_SkAlphaType
735 : unpremul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType;
736 auto finalII =
737 SkImageInfo::Make(width, height, dstSkColorType, finalAT, sk_ref_sp(dstColorSpace));
738 if (!SkImageInfoValidConversion(finalII, tempII)) {
739 return false;
740 }
Brian Salomon6196fc12018-07-12 16:39:23 -0400741 if (!tempPixmap.tryAlloc(tempII)) {
742 return false;
743 }
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400744 finalPixmap.reset(finalII, buffer, rowBytes);
745 buffer = tempPixmap.writable_addr();
746 rowBytes = tempPixmap.rowBytes();
Brian Salomon3d945e82018-05-14 14:54:06 -0400747 // Chrome msan bots require this.
748 sk_bzero(buffer, tempPixmap.computeByteSize());
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400749 }
750
751 if (srcSurface->surfacePriv().hasPendingWrite()) {
752 this->flush(nullptr); // MDB TODO: tighten this
753 }
754
755 if (!fContext->fGpu->readPixels(srcSurface, left, top, width, height, allowedColorType, buffer,
756 rowBytes)) {
757 return false;
758 }
759
760 if (flip) {
761 size_t trimRowBytes = GrColorTypeBytesPerPixel(allowedColorType) * width;
762 std::unique_ptr<char[]> row(new char[trimRowBytes]);
763 char* upper = reinterpret_cast<char*>(buffer);
764 char* lower = reinterpret_cast<char*>(buffer) + (height - 1) * rowBytes;
765 for (int y = 0; y < height / 2; ++y, upper += rowBytes, lower -= rowBytes) {
766 memcpy(row.get(), upper, trimRowBytes);
767 memcpy(upper, lower, trimRowBytes);
768 memcpy(lower, row.get(), trimRowBytes);
769 }
770 }
771 if (convert) {
772 if (!tempPixmap.readPixels(finalPixmap)) {
773 return false;
774 }
775 }
776 return true;
777}
778
Robert Phillips7ee385e2017-03-30 08:02:11 -0400779void GrContextPriv::prepareSurfaceForExternalIO(GrSurfaceProxy* proxy) {
780 ASSERT_SINGLE_OWNER_PRIV
781 RETURN_IF_ABANDONED_PRIV
782 SkASSERT(proxy);
783 ASSERT_OWNED_PROXY_PRIV(proxy);
Greg Daniel51316782017-08-02 15:10:09 +0000784 fContext->fDrawingManager->prepareSurfaceForExternalIO(proxy, 0, nullptr);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000785}
786
Robert Phillips7ee385e2017-03-30 08:02:11 -0400787void GrContextPriv::flushSurfaceWrites(GrSurfaceProxy* proxy) {
788 ASSERT_SINGLE_OWNER_PRIV
789 RETURN_IF_ABANDONED_PRIV
790 SkASSERT(proxy);
791 ASSERT_OWNED_PROXY_PRIV(proxy);
792 if (proxy->priv().hasPendingWrite()) {
793 this->flush(proxy);
bsalomonf80bfed2014-10-07 05:56:02 -0700794 }
795}
796
Robert Phillips7ee385e2017-03-30 08:02:11 -0400797void GrContextPriv::flushSurfaceIO(GrSurfaceProxy* proxy) {
798 ASSERT_SINGLE_OWNER_PRIV
799 RETURN_IF_ABANDONED_PRIV
800 SkASSERT(proxy);
801 ASSERT_OWNED_PROXY_PRIV(proxy);
802 if (proxy->priv().hasPendingIO()) {
803 this->flush(proxy);
ajuma95243eb2016-08-24 08:19:02 -0700804 }
805}
806
bsalomon@google.com27847de2011-02-22 20:59:41 +0000807////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000808
Robert Phillipsc994a932018-06-19 13:09:54 -0400809sk_sp<GrOpMemoryPool> GrContextPriv::refOpMemoryPool() {
810 if (!fContext->fOpMemoryPool) {
811 // DDL TODO: should the size of the memory pool be decreased in DDL mode? CPU-side memory
812 // consumed in DDL mode vs. normal mode for a single skp might be a good metric of wasted
813 // memory.
814 fContext->fOpMemoryPool = sk_sp<GrOpMemoryPool>(new GrOpMemoryPool(16384, 16384));
815 }
816
817 SkASSERT(fContext->fOpMemoryPool);
818 return fContext->fOpMemoryPool;
819}
820
821GrOpMemoryPool* GrContextPriv::opMemoryPool() {
822 return this->refOpMemoryPool().get();
823}
824
Robert Phillips2c862492017-01-18 10:08:39 -0500825sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy> proxy,
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500826 sk_sp<SkColorSpace> colorSpace,
827 const SkSurfaceProps* props) {
Brian Osman45580d32016-11-23 09:37:01 -0500828 ASSERT_SINGLE_OWNER_PRIV
829
Brian Osman45580d32016-11-23 09:37:01 -0500830 if (proxy->asRenderTargetProxy()) {
Robert Phillips2c862492017-01-18 10:08:39 -0500831 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500832 std::move(colorSpace), props);
Brian Osman45580d32016-11-23 09:37:01 -0500833 } else {
834 SkASSERT(proxy->asTextureProxy());
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500835 SkASSERT(!props);
Robert Phillips2c862492017-01-18 10:08:39 -0500836 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Brian Osman45580d32016-11-23 09:37:01 -0500837 }
838}
839
Greg Daniel4065d452018-11-16 15:43:41 -0500840sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(const GrBackendFormat& format,
841 const GrSurfaceDesc& dstDesc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500842 GrSurfaceOrigin origin,
Greg Daniel65c7f662017-10-30 13:39:09 -0400843 GrMipMapped mipMapped,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500844 SkBackingFit fit,
Brian Salomon366093f2018-02-13 09:25:22 -0500845 SkBudgeted isDstBudgeted,
846 sk_sp<SkColorSpace> colorSpace,
847 const SkSurfaceProps* props) {
Greg Daniel65c7f662017-10-30 13:39:09 -0400848 sk_sp<GrTextureProxy> proxy;
849 if (GrMipMapped::kNo == mipMapped) {
Greg Daniel4065d452018-11-16 15:43:41 -0500850 proxy = this->proxyProvider()->createProxy(format, dstDesc, origin, fit, isDstBudgeted);
Greg Daniel65c7f662017-10-30 13:39:09 -0400851 } else {
852 SkASSERT(SkBackingFit::kExact == fit);
Greg Daniel4065d452018-11-16 15:43:41 -0500853 proxy = this->proxyProvider()->createMipMapProxy(format, dstDesc, origin, isDstBudgeted);
Greg Daniel65c7f662017-10-30 13:39:09 -0400854 }
Robert Phillips77b3f322017-01-31 18:24:12 -0500855 if (!proxy) {
856 return nullptr;
857 }
Robert Phillipse2f7d182016-12-15 09:23:05 -0500858
Greg Daniel55fa6472018-03-16 16:13:10 -0400859 sk_sp<GrSurfaceContext> sContext = this->makeWrappedSurfaceContext(std::move(proxy),
860 std::move(colorSpace),
861 props);
862 if (sContext && sContext->asRenderTargetContext()) {
863 sContext->asRenderTargetContext()->discard();
864 }
865
866 return sContext;
Robert Phillipse2f7d182016-12-15 09:23:05 -0500867}
868
Brian Salomond17f6582017-07-19 18:28:58 -0400869sk_sp<GrTextureContext> GrContextPriv::makeBackendTextureContext(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000870 GrSurfaceOrigin origin,
Brian Osmanc1e37052017-03-09 14:19:20 -0500871 sk_sp<SkColorSpace> colorSpace) {
Robert Phillips26caf892017-01-27 10:58:31 -0500872 ASSERT_SINGLE_OWNER_PRIV
873
Brian Salomonc67c31c2018-12-06 10:00:03 -0500874 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->wrapBackendTexture(
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500875 tex, origin, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType);
Robert Phillips77b3f322017-01-31 18:24:12 -0500876 if (!proxy) {
877 return nullptr;
878 }
Robert Phillips26caf892017-01-27 10:58:31 -0500879
Brian Salomond17f6582017-07-19 18:28:58 -0400880 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Robert Phillips26caf892017-01-27 10:58:31 -0500881}
882
Brian Osman11052242016-10-27 14:47:55 -0400883sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000884 const GrBackendTexture& tex,
885 GrSurfaceOrigin origin,
886 int sampleCnt,
Brian Osman11052242016-10-27 14:47:55 -0400887 sk_sp<SkColorSpace> colorSpace,
Brian Osmanc1e37052017-03-09 14:19:20 -0500888 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700889 ASSERT_SINGLE_OWNER_PRIV
Brian Salomonbdecacf2018-02-02 20:32:49 -0500890 SkASSERT(sampleCnt > 0);
robertphillips4fd74ae2016-08-03 14:26:53 -0700891
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500892 sk_sp<GrTextureProxy> proxy(this->proxyProvider()->wrapRenderableBackendTexture(
893 tex, origin, sampleCnt, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo));
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),
Brian Osman11052242016-10-27 14:47:55 -0400899 std::move(colorSpace), props);
robertphillips4fd74ae2016-08-03 14:26:53 -0700900}
901
Brian Osman11052242016-10-27 14:47:55 -0400902sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext(
Greg Danielbcf612b2017-05-01 13:50:58 +0000903 const GrBackendRenderTarget& backendRT,
904 GrSurfaceOrigin origin,
robertphillips4fd74ae2016-08-03 14:26:53 -0700905 sk_sp<SkColorSpace> colorSpace,
906 const SkSurfaceProps* surfaceProps) {
907 ASSERT_SINGLE_OWNER_PRIV
908
Brian Salomon7578f3e2018-03-07 14:39:54 -0500909 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->wrapBackendRenderTarget(backendRT, origin);
Robert Phillips77b3f322017-01-31 18:24:12 -0500910 if (!proxy) {
911 return nullptr;
912 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400913
Robert Phillips37430132016-11-09 06:50:43 -0500914 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400915 std::move(colorSpace),
Brian Osman11052242016-10-27 14:47:55 -0400916 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -0700917}
918
Brian Osman11052242016-10-27 14:47:55 -0400919sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000920 const GrBackendTexture& tex,
921 GrSurfaceOrigin origin,
922 int sampleCnt,
robertphillips4fd74ae2016-08-03 14:26:53 -0700923 sk_sp<SkColorSpace> colorSpace,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500924 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700925 ASSERT_SINGLE_OWNER_PRIV
Brian Salomonbdecacf2018-02-02 20:32:49 -0500926 SkASSERT(sampleCnt > 0);
Brian Salomon7578f3e2018-03-07 14:39:54 -0500927 sk_sp<GrSurfaceProxy> proxy(
928 this->proxyProvider()->wrapBackendTextureAsRenderTarget(tex, origin, sampleCnt));
Robert Phillips77b3f322017-01-31 18:24:12 -0500929 if (!proxy) {
930 return nullptr;
931 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400932
Robert Phillips37430132016-11-09 06:50:43 -0500933 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400934 std::move(colorSpace),
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500935 props);
robertphillips77a2e522015-10-17 07:43:27 -0700936}
937
Greg Danielb46add82019-01-02 14:51:29 -0500938sk_sp<GrRenderTargetContext> GrContextPriv::makeVulkanSecondaryCBRenderTargetContext(
939 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo, const SkSurfaceProps* props) {
940 ASSERT_SINGLE_OWNER_PRIV
941 sk_sp<GrSurfaceProxy> proxy(
942 this->proxyProvider()->wrapVulkanSecondaryCBAsRenderTarget(imageInfo, vkInfo));
943 if (!proxy) {
944 return nullptr;
945 }
946
947 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
948 imageInfo.refColorSpace(),
949 props);
950}
951
Chris Daltonfe199b72017-05-05 11:26:15 -0400952void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
953 fContext->fDrawingManager->addOnFlushCallbackObject(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400954}
955
Robert Phillips62000362018-02-01 09:10:04 -0500956void GrContextPriv::moveOpListsToDDL(SkDeferredDisplayList* ddl) {
957 fContext->fDrawingManager->moveOpListsToDDL(ddl);
958}
959
960void GrContextPriv::copyOpListsFromDDL(const SkDeferredDisplayList* ddl,
961 GrRenderTargetProxy* newDest) {
962 fContext->fDrawingManager->copyOpListsFromDDL(ddl, newDest);
963}
964
robertphillips48fde9c2016-09-06 05:20:20 -0700965static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) {
Brian Osman78f20e02017-01-12 10:28:01 -0500966 switch (config) {
967 case kAlpha_8_GrPixelConfig:
Greg Daniel93b4ddd2018-04-16 16:16:47 -0400968 case kAlpha_8_as_Alpha_GrPixelConfig:
969 case kAlpha_8_as_Red_GrPixelConfig:
Brian Osman78f20e02017-01-12 10:28:01 -0500970 case kRGB_565_GrPixelConfig:
971 case kRGBA_4444_GrPixelConfig:
972 case kBGRA_8888_GrPixelConfig:
Brian Osman10fc6fd2018-03-02 11:01:10 -0500973 case kRGBA_1010102_GrPixelConfig:
Brian Osman9c111352018-10-16 10:18:26 -0400974 case kRGBA_half_GrPixelConfig:
Brian Osman78f20e02017-01-12 10:28:01 -0500975 return kRGBA_8888_GrPixelConfig;
976 case kSBGRA_8888_GrPixelConfig:
977 return kSRGBA_8888_GrPixelConfig;
978 case kAlpha_half_GrPixelConfig:
Greg Daniel93b4ddd2018-04-16 16:16:47 -0400979 case kAlpha_half_as_Red_GrPixelConfig:
Brian Osman78f20e02017-01-12 10:28:01 -0500980 return kRGBA_half_GrPixelConfig;
Greg Daniel93b4ddd2018-04-16 16:16:47 -0400981 case kGray_8_GrPixelConfig:
982 case kGray_8_as_Lum_GrPixelConfig:
983 case kGray_8_as_Red_GrPixelConfig:
984 return kRGB_888_GrPixelConfig;
Brian Osman78f20e02017-01-12 10:28:01 -0500985 default:
986 return kUnknown_GrPixelConfig;
987 }
robertphillips48fde9c2016-09-06 05:20:20 -0700988}
989
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500990sk_sp<GrRenderTargetContext> GrContextPriv::makeDeferredRenderTargetContextWithFallback(
Greg Daniel4065d452018-11-16 15:43:41 -0500991 const GrBackendFormat& format,
robertphillipsd728f0c2016-11-21 11:05:03 -0800992 SkBackingFit fit,
993 int width, int height,
994 GrPixelConfig config,
995 sk_sp<SkColorSpace> colorSpace,
996 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -0400997 GrMipMapped mipMapped,
robertphillipsd728f0c2016-11-21 11:05:03 -0800998 GrSurfaceOrigin origin,
999 const SkSurfaceProps* surfaceProps,
1000 SkBudgeted budgeted) {
Greg Daniel4065d452018-11-16 15:43:41 -05001001 GrBackendFormat localFormat = format;
Brian Salomonbdecacf2018-02-02 20:32:49 -05001002 SkASSERT(sampleCnt > 0);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001003 if (0 == fContext->contextPriv().caps()->getRenderTargetSampleCount(sampleCnt, config)) {
robertphillipsd728f0c2016-11-21 11:05:03 -08001004 config = GrPixelConfigFallback(config);
Greg Daniel4065d452018-11-16 15:43:41 -05001005 // TODO: First we should be checking the getRenderTargetSampleCount from the GrBackendFormat
1006 // and not GrPixelConfig. Besides that, we should implement the fallback in the caps, but
1007 // for now we just convert the fallback pixel config to an SkColorType and then get the
1008 // GrBackendFormat from that.
1009 SkColorType colorType;
1010 if (!GrPixelConfigToColorType(config, &colorType)) {
1011 return nullptr;
1012 }
1013 localFormat = fContext->fCaps->getBackendFormatFromColorType(colorType);
robertphillipsd728f0c2016-11-21 11:05:03 -08001014 }
1015
Greg Daniel4065d452018-11-16 15:43:41 -05001016 return this->makeDeferredRenderTargetContext(localFormat, fit, width, height, config,
1017 std::move(colorSpace), sampleCnt, mipMapped,
1018 origin, surfaceProps, budgeted);
robertphillipsd728f0c2016-11-21 11:05:03 -08001019}
1020
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001021sk_sp<GrRenderTargetContext> GrContextPriv::makeDeferredRenderTargetContext(
Greg Daniel4065d452018-11-16 15:43:41 -05001022 const GrBackendFormat& format,
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001023 SkBackingFit fit,
1024 int width, int height,
1025 GrPixelConfig config,
1026 sk_sp<SkColorSpace> colorSpace,
1027 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -04001028 GrMipMapped mipMapped,
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001029 GrSurfaceOrigin origin,
1030 const SkSurfaceProps* surfaceProps,
1031 SkBudgeted budgeted) {
Brian Salomonbdecacf2018-02-02 20:32:49 -05001032 SkASSERT(sampleCnt > 0);
Khushalc421ca12018-06-26 14:38:34 -07001033 if (fContext->abandoned()) {
Brian Salomon79e4d1b2017-07-13 11:17:11 -04001034 return nullptr;
1035 }
1036
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001037 GrSurfaceDesc desc;
1038 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001039 desc.fWidth = width;
1040 desc.fHeight = height;
1041 desc.fConfig = config;
1042 desc.fSampleCnt = sampleCnt;
1043
Greg Daniele1da1d92017-10-06 15:59:27 -04001044 sk_sp<GrTextureProxy> rtp;
Greg Daniel45d63032017-10-30 13:41:26 -04001045 if (GrMipMapped::kNo == mipMapped) {
Greg Daniel4065d452018-11-16 15:43:41 -05001046 rtp = fContext->fProxyProvider->createProxy(format, desc, origin, fit, budgeted);
Greg Daniele1da1d92017-10-06 15:59:27 -04001047 } else {
Greg Daniel4065d452018-11-16 15:43:41 -05001048 rtp = fContext->fProxyProvider->createMipMapProxy(format, desc, origin, budgeted);
Greg Daniele1da1d92017-10-06 15:59:27 -04001049 }
Robert Phillips08c5ec72017-01-30 12:26:47 -05001050 if (!rtp) {
1051 return nullptr;
1052 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001053
Robert Phillips1119dc32017-04-11 12:54:57 -04001054 sk_sp<GrRenderTargetContext> renderTargetContext(
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001055 fContext->fDrawingManager->makeRenderTargetContext(std::move(rtp),
1056 std::move(colorSpace),
1057 surfaceProps));
Robert Phillips1119dc32017-04-11 12:54:57 -04001058 if (!renderTargetContext) {
1059 return nullptr;
1060 }
1061
1062 renderTargetContext->discard();
1063
1064 return renderTargetContext;
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001065}
1066
Robert Phillipsa0bc39d2019-01-29 13:14:47 -05001067sk_sp<GrSkSLFPFactoryCache> GrContextPriv::getFPFactoryCache() { return fContext->fFPFactoryCache; }
1068
Brian Salomonaff329b2017-08-11 09:40:37 -04001069std::unique_ptr<GrFragmentProcessor> GrContext::createPMToUPMEffect(
Brian Osman5ea96bf2018-10-02 14:58:05 -04001070 std::unique_ptr<GrFragmentProcessor> fp) {
Robert Phillips757914d2017-01-25 15:48:30 -05001071 ASSERT_SINGLE_OWNER
Brian Osman5ea96bf2018-10-02 14:58:05 -04001072 // We should have already called this->validPMUPMConversionExists() in this case
1073 SkASSERT(fDidTestPMConversions);
1074 // ...and it should have succeeded
1075 SkASSERT(this->validPMUPMConversionExists());
Brian Osman409e74f2017-04-17 11:48:28 -04001076
Brian Osman5ea96bf2018-10-02 14:58:05 -04001077 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul);
Robert Phillips757914d2017-01-25 15:48:30 -05001078}
1079
Brian Salomonaff329b2017-08-11 09:40:37 -04001080std::unique_ptr<GrFragmentProcessor> GrContext::createUPMToPMEffect(
Brian Osman5ea96bf2018-10-02 14:58:05 -04001081 std::unique_ptr<GrFragmentProcessor> fp) {
joshualitt1de610a2016-01-06 08:26:09 -08001082 ASSERT_SINGLE_OWNER
Brian Osman5ea96bf2018-10-02 14:58:05 -04001083 // We should have already called this->validPMUPMConversionExists() in this case
1084 SkASSERT(fDidTestPMConversions);
1085 // ...and it should have succeeded
1086 SkASSERT(this->validPMUPMConversionExists());
Brian Osman409e74f2017-04-17 11:48:28 -04001087
Brian Osman5ea96bf2018-10-02 14:58:05 -04001088 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001089}
1090
Brian Osman409e74f2017-04-17 11:48:28 -04001091bool GrContext::validPMUPMConversionExists() {
joshualitt1de610a2016-01-06 08:26:09 -08001092 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -04001093 if (!fDidTestPMConversions) {
Brian Osman28804f32017-04-20 10:24:36 -04001094 fPMUPMConversionsRoundTrip = GrConfigConversionEffect::TestForPreservingPMConversions(this);
Brian Osman409e74f2017-04-17 11:48:28 -04001095 fDidTestPMConversions = true;
1096 }
1097
bsalomon636e8022015-07-29 06:08:46 -07001098 // The PM<->UPM tests fail or succeed together so we only need to check one.
Brian Osman28804f32017-04-20 10:24:36 -04001099 return fPMUPMConversionsRoundTrip;
bsalomon636e8022015-07-29 06:08:46 -07001100}
1101
Khushal3e7548c2018-05-23 15:45:01 -07001102bool GrContext::supportsDistanceFieldText() const {
1103 return fCaps->shaderCaps()->supportsDistanceFieldText();
1104}
1105
bsalomon37f9a262015-02-02 13:00:10 -08001106//////////////////////////////////////////////////////////////////////////////
1107
Robert Phillipsfc711a22018-02-13 17:03:00 -05001108// DDL TODO: remove 'maxResources'
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001109void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -08001110 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001111 if (maxResources) {
1112 *maxResources = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -08001113 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001114 if (maxResourceBytes) {
1115 *maxResourceBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -08001116 }
1117}
1118
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001119void GrContext::setResourceCacheLimits(int maxResources, size_t maxResourceBytes) {
joshualitt1de610a2016-01-06 08:26:09 -08001120 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001121 fResourceCache->setLimits(maxResources, maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -08001122}
1123
ericrk0a5fa482015-09-15 14:16:10 -07001124//////////////////////////////////////////////////////////////////////////////
ericrk0a5fa482015-09-15 14:16:10 -07001125void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -08001126 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -07001127 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
Khushal71652e22018-10-29 13:05:36 -07001128 traceMemoryDump->dumpNumericValue("skia/gr_text_blob_cache", "size", "bytes",
1129 fTextBlobCache->usedBytes());
ericrk0a5fa482015-09-15 14:16:10 -07001130}
Brian Osman71a18892017-08-10 10:23:25 -04001131
1132//////////////////////////////////////////////////////////////////////////////
Kevin Lubickf4def342018-10-04 12:52:50 -04001133#ifdef SK_ENABLE_DUMP_GPU
1134#include "SkJSONWriter.h"
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001135SkString GrContextPriv::dump() const {
Brian Osman71a18892017-08-10 10:23:25 -04001136 SkDynamicMemoryWStream stream;
1137 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
1138 writer.beginObject();
1139
1140 static const char* kBackendStr[] = {
1141 "Metal",
1142 "OpenGL",
1143 "Vulkan",
1144 "Mock",
1145 };
Greg Danielbdf12ad2018-10-12 09:31:11 -04001146 GR_STATIC_ASSERT(0 == (unsigned)GrBackendApi::kMetal);
1147 GR_STATIC_ASSERT(1 == (unsigned)GrBackendApi::kOpenGL);
1148 GR_STATIC_ASSERT(2 == (unsigned)GrBackendApi::kVulkan);
1149 GR_STATIC_ASSERT(3 == (unsigned)GrBackendApi::kMock);
Robert Phillips4217ea72019-01-30 13:08:28 -05001150 writer.appendString("backend", kBackendStr[(unsigned)fContext->backend()]);
Brian Osman71a18892017-08-10 10:23:25 -04001151
1152 writer.appendName("caps");
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001153 fContext->fCaps->dumpJSON(&writer);
Brian Osman71a18892017-08-10 10:23:25 -04001154
1155 writer.appendName("gpu");
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001156 fContext->fGpu->dumpJSON(&writer);
Brian Osman71a18892017-08-10 10:23:25 -04001157
1158 // Flush JSON to the memory stream
1159 writer.endObject();
1160 writer.flush();
1161
1162 // Null terminate the JSON data in the memory stream
1163 stream.write8(0);
1164
1165 // Allocate a string big enough to hold all the data, then copy out of the stream
1166 SkString result(stream.bytesWritten());
1167 stream.copyToAndReset(result.writable_str());
1168 return result;
1169}
Kevin Lubickf4def342018-10-04 12:52:50 -04001170#endif