blob: 57103af313aa76d8a102df4874981f49c007809f [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 Phillipsfd0d9702019-02-01 10:19:42 -050077 fResourceCache = new GrResourceCache(fCaps.get(), &fSingleOwner, this->contextID());
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 {
Robert Phillipsfd0d9702019-02-01 10:19:42 -050083 fProxyProvider = new GrProxyProvider(this->contextID(), 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 Phillipsfd0d9702019-02-01 10:19:42 -0500134 fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB, this, this->contextID()));
Brian Salomon91a3e522017-06-23 10:58:19 -0400135
Robert Phillipsfde6fa02018-03-02 08:53:14 -0500136 // DDL TODO: we need to think through how the task group & persistent cache
137 // get passed on to/shared between all the DDLRecorders created with this context.
Brian Osman51279982017-08-23 10:12:00 -0400138 if (options.fExecutor) {
139 fTaskGroup = skstd::make_unique<SkTaskGroup>(*options.fExecutor);
140 }
141
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400142 fPersistentCache = options.fPersistentCache;
143
Brian Salomon91a3e522017-06-23 10:58:19 -0400144 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000145}
146
bsalomon@google.com27847de2011-02-22 20:59:41 +0000147GrContext::~GrContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800148 ASSERT_SINGLE_OWNER
149
Robert Phillips2e6feed2018-01-22 15:27:20 -0500150 if (fDrawingManager) {
151 fDrawingManager->cleanup();
152 }
halcanary385fe4d2015-08-26 13:07:48 -0700153 delete fResourceProvider;
154 delete fResourceCache;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500155 delete fProxyProvider;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500156 delete fGlyphCache;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000157}
158
Robert Phillips4217ea72019-01-30 13:08:28 -0500159sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
160 return fThreadSafeProxy;
161}
162
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400163//////////////////////////////////////////////////////////////////////////////
164
bsalomon2354f842014-07-28 13:48:36 -0700165void GrContext::abandonContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800166 ASSERT_SINGLE_OWNER
167
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500168 fProxyProvider->abandon();
bsalomond309e7a2015-04-30 14:18:54 -0700169 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800170
171 // Need to abandon the drawing manager first so all the render targets
172 // will be released/forgotten before they too are abandoned.
173 fDrawingManager->abandon();
174
bsalomon@google.com205d4602011-04-25 12:43:45 +0000175 // abandon first to so destructors
176 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800177 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700178
bsalomon6e2aad42016-04-01 11:54:31 -0700179 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
180
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500181 fGlyphCache->freeAll();
bsalomon6e2aad42016-04-01 11:54:31 -0700182 fTextBlobCache->freeAll();
183}
184
Khushalc421ca12018-06-26 14:38:34 -0700185bool GrContext::abandoned() const {
186 ASSERT_SINGLE_OWNER
Brian Salomon9bc76d92019-01-24 12:18:33 -0500187 // If called from ~GrContext(), the drawing manager may already be gone.
188 return !fDrawingManager || fDrawingManager->wasAbandoned();
Khushalc421ca12018-06-26 14:38:34 -0700189}
190
bsalomon6e2aad42016-04-01 11:54:31 -0700191void GrContext::releaseResourcesAndAbandonContext() {
192 ASSERT_SINGLE_OWNER
193
Brian Salomon614c1a82018-12-19 15:42:06 -0500194 if (this->abandoned()) {
195 return;
196 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500197 fProxyProvider->abandon();
bsalomon6e2aad42016-04-01 11:54:31 -0700198 fResourceProvider->abandon();
199
200 // Need to abandon the drawing manager first so all the render targets
201 // will be released/forgotten before they too are abandoned.
202 fDrawingManager->abandon();
203
204 // Release all resources in the backend 3D API.
205 fResourceCache->releaseAll();
206
207 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000208
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500209 fGlyphCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700210 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000211}
212
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000213void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800214 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000215 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000216}
217
218void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800219 ASSERT_SINGLE_OWNER
220
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500221 fGlyphCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700222
223 fDrawingManager->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700224
225 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000226}
227
Robert Phillips6eba0632018-03-28 12:25:42 -0400228void GrContext::purgeUnlockedResources(bool scratchResourcesOnly) {
229 ASSERT_SINGLE_OWNER
230 fResourceCache->purgeUnlockedResources(scratchResourcesOnly);
231 fResourceCache->purgeAsNeeded();
232 fTextBlobCache->purgeStaleBlobs();
233}
234
Jim Van Verth76d917c2017-12-13 09:26:37 -0500235void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) {
Brian Salomon5e150852017-03-22 14:53:13 -0400236 ASSERT_SINGLE_OWNER
Chris Dalton6c3879d2018-11-01 11:13:19 -0600237
238 auto purgeTime = GrStdSteadyClock::now() - msNotUsed;
239
Jim Van Verth76d917c2017-12-13 09:26:37 -0500240 fResourceCache->purgeAsNeeded();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600241 fResourceCache->purgeResourcesNotUsedSince(purgeTime);
242
243 if (auto ccpr = fDrawingManager->getCoverageCountingPathRenderer()) {
Chris Dalton351e80c2019-01-06 22:51:00 -0700244 ccpr->purgeCacheEntriesOlderThan(fProxyProvider, purgeTime);
Chris Dalton6c3879d2018-11-01 11:13:19 -0600245 }
Jim Van Verth76d917c2017-12-13 09:26:37 -0500246
247 fTextBlobCache->purgeStaleBlobs();
Brian Salomon5e150852017-03-22 14:53:13 -0400248}
249
Derek Sollenberger5480a182017-05-25 16:43:59 -0400250void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
251 ASSERT_SINGLE_OWNER
252 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
253}
254
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000255void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800256 ASSERT_SINGLE_OWNER
257
bsalomon71cb0c22014-11-14 12:10:14 -0800258 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800259 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800260 }
261 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800262 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800263 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000264}
265
Derek Sollenbergeree479142017-05-24 11:41:33 -0400266size_t GrContext::getResourceCachePurgeableBytes() const {
267 ASSERT_SINGLE_OWNER
268 return fResourceCache->getPurgeableBytes();
269}
270
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000271////////////////////////////////////////////////////////////////////////////////
272
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400273int GrContext::maxTextureSize() const { return fCaps->maxTextureSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400274
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400275int GrContext::maxRenderTargetSize() const { return fCaps->maxRenderTargetSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400276
Brian Salomonbdecacf2018-02-02 20:32:49 -0500277bool GrContext::colorTypeSupportedAsImage(SkColorType colorType) const {
Brian Osman2b23c4b2018-06-01 12:25:08 -0400278 GrPixelConfig config = SkColorType2GrPixelConfig(colorType);
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400279 return fCaps->isConfigTexturable(config);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500280}
281
282int GrContext::maxSurfaceSampleCountForColorType(SkColorType colorType) const {
Brian Osman2b23c4b2018-06-01 12:25:08 -0400283 GrPixelConfig config = SkColorType2GrPixelConfig(colorType);
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400284 return fCaps->maxRenderTargetSampleCount(config);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500285}
286
287////////////////////////////////////////////////////////////////////////////////
288
joshualitt0db6dfa2015-04-10 07:01:30 -0700289void GrContext::TextBlobCacheOverBudgetCB(void* data) {
290 SkASSERT(data);
Brian Osman11052242016-10-27 14:47:55 -0400291 // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on
292 // GrRenderTargetContext to perform a necessary flush. The solution is to move drawText calls
293 // to below the GrContext level, but this is not trivial because they call drawPath on
294 // SkGpuDevice.
joshualitt0db6dfa2015-04-10 07:01:30 -0700295 GrContext* context = reinterpret_cast<GrContext*>(data);
296 context->flush();
297}
298
bsalomon@google.com27847de2011-02-22 20:59:41 +0000299////////////////////////////////////////////////////////////////////////////////
300
bsalomonb77a9072016-09-07 10:02:04 -0700301void GrContext::flush() {
joshualitt1de610a2016-01-06 08:26:09 -0800302 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700303 RETURN_IF_ABANDONED
Robert Phillips7ee385e2017-03-30 08:02:11 -0400304
305 fDrawingManager->flush(nullptr);
306}
307
Greg Daniel51316782017-08-02 15:10:09 +0000308GrSemaphoresSubmitted GrContext::flushAndSignalSemaphores(int numSemaphores,
309 GrBackendSemaphore signalSemaphores[]) {
310 ASSERT_SINGLE_OWNER
311 if (fDrawingManager->wasAbandoned()) { return GrSemaphoresSubmitted::kNo; }
312
313 return fDrawingManager->flush(nullptr, numSemaphores, signalSemaphores);
314}
315
Robert Phillips7ee385e2017-03-30 08:02:11 -0400316void GrContextPriv::flush(GrSurfaceProxy* proxy) {
317 ASSERT_SINGLE_OWNER_PRIV
318 RETURN_IF_ABANDONED_PRIV
319 ASSERT_OWNED_PROXY_PRIV(proxy);
320
321 fContext->fDrawingManager->flush(proxy);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000322}
323
Greg Daniela870b462019-01-08 15:49:46 -0500324////////////////////////////////////////////////////////////////////////////////
325
326void GrContext::storeVkPipelineCacheData() {
327 if (fGpu) {
328 fGpu->storeVkPipelineCacheData();
329 }
330}
331
332////////////////////////////////////////////////////////////////////////////////
333
Brian Salomonc320b152018-02-20 14:05:36 -0500334// TODO: This will be removed when GrSurfaceContexts are aware of their color types.
335// (skbug.com/6718)
Brian Osmand2ca59a2017-04-13 14:03:57 -0400336static bool valid_premul_config(GrPixelConfig config) {
Brian Salomonc320b152018-02-20 14:05:36 -0500337 switch (config) {
338 case kUnknown_GrPixelConfig: return false;
339 case kAlpha_8_GrPixelConfig: return false;
340 case kGray_8_GrPixelConfig: return false;
341 case kRGB_565_GrPixelConfig: return false;
342 case kRGBA_4444_GrPixelConfig: return true;
343 case kRGBA_8888_GrPixelConfig: return true;
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400344 case kRGB_888_GrPixelConfig: return false;
Jim Van Verth69e57852018-12-05 13:38:59 -0500345 case kRG_88_GrPixelConfig: return false;
Brian Salomonc320b152018-02-20 14:05:36 -0500346 case kBGRA_8888_GrPixelConfig: return true;
347 case kSRGBA_8888_GrPixelConfig: return true;
348 case kSBGRA_8888_GrPixelConfig: return true;
Brian Osman10fc6fd2018-03-02 11:01:10 -0500349 case kRGBA_1010102_GrPixelConfig: return true;
Brian Salomonc320b152018-02-20 14:05:36 -0500350 case kRGBA_float_GrPixelConfig: return true;
351 case kRG_float_GrPixelConfig: return false;
352 case kAlpha_half_GrPixelConfig: return false;
353 case kRGBA_half_GrPixelConfig: return true;
Jim Van Verth1676cb92019-01-15 13:24:45 -0500354 case kRGB_ETC1_GrPixelConfig: return false;
Brian Salomonc320b152018-02-20 14:05:36 -0500355 case kAlpha_8_as_Alpha_GrPixelConfig: return false;
356 case kAlpha_8_as_Red_GrPixelConfig: return false;
357 case kAlpha_half_as_Red_GrPixelConfig: return false;
358 case kGray_8_as_Lum_GrPixelConfig: return false;
359 case kGray_8_as_Red_GrPixelConfig: return false;
360 }
361 SK_ABORT("Invalid GrPixelConfig");
362 return false;
Brian Osmance425512017-03-22 14:37:50 -0400363}
364
Brian Salomonc320b152018-02-20 14:05:36 -0500365static bool valid_premul_color_type(GrColorType ct) {
366 switch (ct) {
Brian Osman10fc6fd2018-03-02 11:01:10 -0500367 case GrColorType::kUnknown: return false;
368 case GrColorType::kAlpha_8: return false;
369 case GrColorType::kRGB_565: return false;
370 case GrColorType::kABGR_4444: return true;
371 case GrColorType::kRGBA_8888: return true;
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400372 case GrColorType::kRGB_888x: return false;
Jim Van Verth69e57852018-12-05 13:38:59 -0500373 case GrColorType::kRG_88: return false;
Brian Osman10fc6fd2018-03-02 11:01:10 -0500374 case GrColorType::kBGRA_8888: return true;
375 case GrColorType::kRGBA_1010102: return true;
376 case GrColorType::kGray_8: return false;
377 case GrColorType::kAlpha_F16: return false;
378 case GrColorType::kRGBA_F16: return true;
379 case GrColorType::kRG_F32: return false;
380 case GrColorType::kRGBA_F32: return true;
Jim Van Verth1676cb92019-01-15 13:24:45 -0500381 case GrColorType::kRGB_ETC1: return false;
Brian Salomonc320b152018-02-20 14:05:36 -0500382 }
383 SK_ABORT("Invalid GrColorType");
384 return false;
385}
386
387static bool valid_pixel_conversion(GrColorType cpuColorType, GrPixelConfig gpuConfig,
Brian Osmand2ca59a2017-04-13 14:03:57 -0400388 bool premulConversion) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400389 // We only allow premul <-> unpremul conversions for some formats
Brian Salomonc320b152018-02-20 14:05:36 -0500390 if (premulConversion &&
391 (!valid_premul_color_type(cpuColorType) || !valid_premul_config(gpuConfig))) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400392 return false;
393 }
Brian Osmand2ca59a2017-04-13 14:03:57 -0400394 return true;
395}
396
Brian Salomonc320b152018-02-20 14:05:36 -0500397bool GrContextPriv::writeSurfacePixels(GrSurfaceContext* dst, int left, int top, int width,
398 int height, GrColorType srcColorType,
399 SkColorSpace* srcColorSpace, const void* buffer,
400 size_t rowBytes, uint32_t pixelOpsFlags) {
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500401 ASSERT_SINGLE_OWNER_PRIV
402 RETURN_FALSE_IF_ABANDONED_PRIV
403 SkASSERT(dst);
404 SkASSERT(buffer);
405 ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy());
Brian Salomond494f6e2018-06-01 14:13:43 -0400406 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "writeSurfacePixels", fContext);
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500407
408 if (GrColorType::kUnknown == srcColorType) {
409 return false;
410 }
411
412 if (!dst->asSurfaceProxy()->instantiate(this->resourceProvider())) {
413 return false;
414 }
415
416 GrSurfaceProxy* dstProxy = dst->asSurfaceProxy();
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400417 GrSurface* dstSurface = dstProxy->peekSurface();
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500418
419 if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(),
420 GrColorTypeBytesPerPixel(srcColorType), &left, &top,
421 &width, &height, &buffer, &rowBytes)) {
422 return false;
423 }
424
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400425 // TODO: Make GrSurfaceContext know its alpha type and pass src buffer's alpha type.
426 bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
427
428 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
429 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
Brian Salomon817847c2018-05-07 13:33:50 -0400430 bool canvas2DFastPath =
Greg Daniel4374e962018-09-28 15:09:47 -0400431 !fContext->contextPriv().caps()->avoidWritePixelsFastPath() &&
Brian Salomon817847c2018-05-07 13:33:50 -0400432 premul &&
Brian Osman34ec3742018-07-03 10:40:57 -0400433 !dst->colorSpaceInfo().colorSpace() &&
Brian Salomon817847c2018-05-07 13:33:50 -0400434 (srcColorType == GrColorType::kRGBA_8888 || srcColorType == GrColorType::kBGRA_8888) &&
435 SkToBool(dst->asRenderTargetContext()) &&
436 (dstProxy->config() == kRGBA_8888_GrPixelConfig ||
437 dstProxy->config() == kBGRA_8888_GrPixelConfig) &&
438 !(pixelOpsFlags & kDontFlush_PixelOpsFlag) &&
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400439 fContext->contextPriv().caps()->isConfigTexturable(kRGBA_8888_GrPixelConfig) &&
Brian Salomon817847c2018-05-07 13:33:50 -0400440 fContext->validPMUPMConversionExists();
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400441
Greg Daniel4065d452018-11-16 15:43:41 -0500442 const GrCaps* caps = this->caps();
443 if (!caps->surfaceSupportsWritePixels(dstSurface) ||
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400444 canvas2DFastPath) {
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400445 // We don't expect callers that are skipping flushes to require an intermediate draw.
446 SkASSERT(!(pixelOpsFlags & kDontFlush_PixelOpsFlag));
447 if (pixelOpsFlags & kDontFlush_PixelOpsFlag) {
448 return false;
449 }
450
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500451 GrSurfaceDesc desc;
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500452 desc.fWidth = width;
453 desc.fHeight = height;
454 desc.fSampleCnt = 1;
Greg Daniel4065d452018-11-16 15:43:41 -0500455
456 GrBackendFormat format;
457 if (canvas2DFastPath) {
458 desc.fConfig = kRGBA_8888_GrPixelConfig;
459 format =
460 fContext->contextPriv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
461 } else {
462 desc.fConfig = dstProxy->config();
463 format = dstProxy->backendFormat().makeTexture2D();
464 if (!format.isValid()) {
465 return false;
466 }
467 }
468
Brian Salomon2a4f9832018-03-03 22:43:43 -0500469 auto tempProxy = this->proxyProvider()->createProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500470 format, desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kApprox, SkBudgeted::kYes);
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500471 if (!tempProxy) {
472 return false;
473 }
474 auto tempCtx = this->drawingManager()->makeTextureContext(
475 tempProxy, dst->colorSpaceInfo().refColorSpace());
476 if (!tempCtx) {
477 return false;
478 }
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400479 uint32_t flags = canvas2DFastPath ? 0 : pixelOpsFlags;
Brian Salomon817847c2018-05-07 13:33:50 -0400480 // In the fast path we always write the srcData to the temp context as though it were RGBA.
481 // When the data is really BGRA the write will cause the R and B channels to be swapped in
482 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
483 // dst.
484 auto tmpColorType = canvas2DFastPath ? GrColorType::kRGBA_8888 : srcColorType;
Brian Salomond494f6e2018-06-01 14:13:43 -0400485 if (!this->writeSurfacePixels(tempCtx.get(), 0, 0, width, height, tmpColorType,
486 srcColorSpace, buffer, rowBytes, flags)) {
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500487 return false;
488 }
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400489 if (canvas2DFastPath) {
490 GrPaint paint;
491 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomon817847c2018-05-07 13:33:50 -0400492 auto fp = fContext->createUPMToPMEffect(
Brian Osman5ea96bf2018-10-02 14:58:05 -0400493 GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I()));
Brian Salomon817847c2018-05-07 13:33:50 -0400494 if (srcColorType == GrColorType::kBGRA_8888) {
495 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
496 }
497 if (!fp) {
498 return false;
499 }
500 paint.addColorFragmentProcessor(std::move(fp));
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400501 dst->asRenderTargetContext()->fillRectToRect(
502 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
503 SkRect::MakeXYWH(left, top, width, height), SkRect::MakeWH(width, height));
504 return true;
505 } else {
506 return dst->copy(tempProxy.get(), SkIRect::MakeWH(width, height), {left, top});
507 }
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500508 }
509
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500510 bool convert = premul;
511
512 if (!valid_pixel_conversion(srcColorType, dstProxy->config(), premul)) {
513 return false;
514 }
515
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400516 GrColorType allowedColorType = fContext->contextPriv().caps()->supportedWritePixelsColorType(
517 dstProxy->config(), srcColorType);
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500518 convert = convert || (srcColorType != allowedColorType);
519
520 if (!dst->colorSpaceInfo().colorSpace()) {
521 // "Legacy" mode - no color space conversions.
522 srcColorSpace = nullptr;
523 }
524 convert = convert || !SkColorSpace::Equals(srcColorSpace, dst->colorSpaceInfo().colorSpace());
525
526 std::unique_ptr<char[]> tempBuffer;
527 if (convert) {
528 auto srcSkColorType = GrColorTypeToSkColorType(srcColorType);
529 auto dstSkColorType = GrColorTypeToSkColorType(allowedColorType);
530 if (kUnknown_SkColorType == srcSkColorType || kUnknown_SkColorType == dstSkColorType) {
531 return false;
532 }
Brian Osman2091fbb2018-10-11 11:05:37 -0400533 auto srcAlphaType = SkColorTypeIsAlwaysOpaque(srcSkColorType)
534 ? kOpaque_SkAlphaType
535 : (premul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType);
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500536 SkPixmap src(SkImageInfo::Make(width, height, srcSkColorType, srcAlphaType,
537 sk_ref_sp(srcColorSpace)),
538 buffer, rowBytes);
539 auto tempSrcII = SkImageInfo::Make(width, height, dstSkColorType, kPremul_SkAlphaType,
540 dst->colorSpaceInfo().refColorSpace());
541 auto size = tempSrcII.computeMinByteSize();
542 if (!size) {
543 return false;
544 }
545 tempBuffer.reset(new char[size]);
546 SkPixmap tempSrc(tempSrcII, tempBuffer.get(), tempSrcII.minRowBytes());
547 if (!src.readPixels(tempSrc)) {
548 return false;
549 }
550 srcColorType = allowedColorType;
551 buffer = tempSrc.addr();
552 rowBytes = tempSrc.rowBytes();
553 if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
554 std::unique_ptr<char[]> row(new char[rowBytes]);
555 for (int y = 0; y < height / 2; ++y) {
556 memcpy(row.get(), tempSrc.addr(0, y), rowBytes);
557 memcpy(tempSrc.writable_addr(0, y), tempSrc.addr(0, height - 1 - y), rowBytes);
558 memcpy(tempSrc.writable_addr(0, height - 1 - y), row.get(), rowBytes);
559 }
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400560 top = dstSurface->height() - top - height;
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500561 }
562 } else if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
563 size_t trimRowBytes = GrColorTypeBytesPerPixel(srcColorType) * width;
564 tempBuffer.reset(new char[trimRowBytes * height]);
565 char* dst = reinterpret_cast<char*>(tempBuffer.get()) + trimRowBytes * (height - 1);
566 const char* src = reinterpret_cast<const char*>(buffer);
567 for (int i = 0; i < height; ++i, src += rowBytes, dst -= trimRowBytes) {
568 memcpy(dst, src, trimRowBytes);
569 }
570 buffer = tempBuffer.get();
571 rowBytes = trimRowBytes;
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400572 top = dstSurface->height() - top - height;
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500573 }
574
575 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && dstSurface->surfacePriv().hasPendingIO()) {
576 this->flush(nullptr); // MDB TODO: tighten this
577 }
578
579 return this->getGpu()->writePixels(dstSurface, left, top, width, height, srcColorType, buffer,
580 rowBytes);
581}
582
Brian Salomond494f6e2018-06-01 14:13:43 -0400583bool GrContextPriv::readSurfacePixels(GrSurfaceContext* src, int left, int top, int width,
584 int height, GrColorType dstColorType,
585 SkColorSpace* dstColorSpace, void* buffer, size_t rowBytes,
586 uint32_t pixelOpsFlags) {
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400587 ASSERT_SINGLE_OWNER_PRIV
588 RETURN_FALSE_IF_ABANDONED_PRIV
589 SkASSERT(src);
590 SkASSERT(buffer);
591 ASSERT_OWNED_PROXY_PRIV(src->asSurfaceProxy());
Brian Salomond494f6e2018-06-01 14:13:43 -0400592 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "readSurfacePixels", fContext);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400593
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400594 SkASSERT(!(pixelOpsFlags & kDontFlush_PixelOpsFlag));
595 if (pixelOpsFlags & kDontFlush_PixelOpsFlag) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400596 return false;
597 }
598
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400599 // MDB TODO: delay this instantiation until later in the method
600 if (!src->asSurfaceProxy()->instantiate(this->resourceProvider())) {
601 return false;
602 }
603
604 GrSurfaceProxy* srcProxy = src->asSurfaceProxy();
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400605 GrSurface* srcSurface = srcProxy->peekSurface();
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400606
607 if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(),
608 GrColorTypeBytesPerPixel(dstColorType), &left, &top,
609 &width, &height, &buffer, &rowBytes)) {
610 return false;
611 }
612
613 // TODO: Make GrSurfaceContext know its alpha type and pass dst buffer's alpha type.
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400614 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400615
616 if (!valid_pixel_conversion(dstColorType, srcProxy->config(), unpremul)) {
617 return false;
618 }
619
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400620 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
621 // care so much about getImageData performance. However, in order to ensure putImageData/
622 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
Brian Salomond494f6e2018-06-01 14:13:43 -0400623 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400624 // fContext->vaildaPMUPMConversionExists()).
Brian Salomon817847c2018-05-07 13:33:50 -0400625 bool canvas2DFastPath =
626 unpremul &&
Brian Osman34ec3742018-07-03 10:40:57 -0400627 !src->colorSpaceInfo().colorSpace() &&
Brian Salomon817847c2018-05-07 13:33:50 -0400628 (GrColorType::kRGBA_8888 == dstColorType || GrColorType::kBGRA_8888 == dstColorType) &&
629 SkToBool(srcProxy->asTextureProxy()) &&
630 (srcProxy->config() == kRGBA_8888_GrPixelConfig ||
631 srcProxy->config() == kBGRA_8888_GrPixelConfig) &&
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400632 fContext->contextPriv().caps()->isConfigRenderable(kRGBA_8888_GrPixelConfig) &&
Brian Salomon817847c2018-05-07 13:33:50 -0400633 fContext->validPMUPMConversionExists();
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400634
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400635 if (!fContext->contextPriv().caps()->surfaceSupportsReadPixels(srcSurface) ||
636 canvas2DFastPath) {
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400637 GrSurfaceDesc desc;
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400638 desc.fFlags = canvas2DFastPath ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
639 desc.fConfig = canvas2DFastPath ? kRGBA_8888_GrPixelConfig : srcProxy->config();
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400640 desc.fWidth = width;
641 desc.fHeight = height;
642 desc.fSampleCnt = 1;
Greg Daniel4065d452018-11-16 15:43:41 -0500643
644 GrBackendFormat format;
645 if (canvas2DFastPath) {
646 desc.fFlags = kRenderTarget_GrSurfaceFlag;
647 desc.fConfig = kRGBA_8888_GrPixelConfig;
648 format = this->caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
649 } else {
650 desc.fFlags = kNone_GrSurfaceFlags;
651 desc.fConfig = srcProxy->config();
652 format = srcProxy->backendFormat().makeTexture2D();
653 if (!format.isValid()) {
654 return false;
655 }
656 }
657
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400658 auto tempProxy = this->proxyProvider()->createProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500659 format, desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kApprox, SkBudgeted::kYes);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400660 if (!tempProxy) {
661 return false;
662 }
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400663 sk_sp<GrSurfaceContext> tempCtx;
664 if (canvas2DFastPath) {
665 tempCtx = this->drawingManager()->makeRenderTargetContext(std::move(tempProxy), nullptr,
666 nullptr);
Greg Daniel42314012018-04-23 10:57:37 -0400667 SkASSERT(tempCtx->asRenderTargetContext());
668 tempCtx->asRenderTargetContext()->discard();
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400669 } else {
670 tempCtx = this->drawingManager()->makeTextureContext(
671 std::move(tempProxy), src->colorSpaceInfo().refColorSpace());
672 }
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400673 if (!tempCtx) {
674 return false;
675 }
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400676 if (canvas2DFastPath) {
677 GrPaint paint;
678 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomon817847c2018-05-07 13:33:50 -0400679 auto fp = fContext->createPMToUPMEffect(
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400680 GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()),
Brian Osman5ea96bf2018-10-02 14:58:05 -0400681 SkMatrix::I()));
Brian Salomon817847c2018-05-07 13:33:50 -0400682 if (dstColorType == GrColorType::kBGRA_8888) {
683 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
684 dstColorType = GrColorType::kRGBA_8888;
685 }
686 if (!fp) {
687 return false;
688 }
689 paint.addColorFragmentProcessor(std::move(fp));
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400690 tempCtx->asRenderTargetContext()->fillRectToRect(
691 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
692 SkRect::MakeWH(width, height), SkRect::MakeXYWH(left, top, width, height));
693 } else if (!tempCtx->copy(srcProxy, SkIRect::MakeXYWH(left, top, width, height), {0, 0})) {
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400694 return false;
695 }
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400696 uint32_t flags = canvas2DFastPath ? 0 : pixelOpsFlags;
Brian Salomond494f6e2018-06-01 14:13:43 -0400697 return this->readSurfacePixels(tempCtx.get(), 0, 0, width, height, dstColorType,
698 dstColorSpace, buffer, rowBytes, flags);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400699 }
700
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400701 bool convert = unpremul;
702
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400703 bool flip = srcProxy->origin() == kBottomLeft_GrSurfaceOrigin;
704 if (flip) {
705 top = srcSurface->height() - top - height;
706 }
707
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400708 GrColorType allowedColorType = fContext->contextPriv().caps()->supportedReadPixelsColorType(
709 srcProxy->config(), dstColorType);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400710 convert = convert || (dstColorType != allowedColorType);
711
712 if (!src->colorSpaceInfo().colorSpace()) {
713 // "Legacy" mode - no color space conversions.
714 dstColorSpace = nullptr;
715 }
716 convert = convert || !SkColorSpace::Equals(dstColorSpace, src->colorSpaceInfo().colorSpace());
717
718 SkAutoPixmapStorage tempPixmap;
719 SkPixmap finalPixmap;
720 if (convert) {
721 SkColorType srcSkColorType = GrColorTypeToSkColorType(allowedColorType);
722 SkColorType dstSkColorType = GrColorTypeToSkColorType(dstColorType);
Brian Osman2091fbb2018-10-11 11:05:37 -0400723 bool srcAlwaysOpaque = SkColorTypeIsAlwaysOpaque(srcSkColorType);
724 bool dstAlwaysOpaque = SkColorTypeIsAlwaysOpaque(dstSkColorType);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400725 if (kUnknown_SkColorType == srcSkColorType || kUnknown_SkColorType == dstSkColorType) {
726 return false;
727 }
Brian Osman2091fbb2018-10-11 11:05:37 -0400728 auto tempAT = srcAlwaysOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400729 auto tempII = SkImageInfo::Make(width, height, srcSkColorType, tempAT,
730 src->colorSpaceInfo().refColorSpace());
Brian Osman2091fbb2018-10-11 11:05:37 -0400731 SkASSERT(!unpremul || !dstAlwaysOpaque);
732 auto finalAT = (srcAlwaysOpaque || dstAlwaysOpaque)
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400733 ? kOpaque_SkAlphaType
734 : unpremul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType;
735 auto finalII =
736 SkImageInfo::Make(width, height, dstSkColorType, finalAT, sk_ref_sp(dstColorSpace));
737 if (!SkImageInfoValidConversion(finalII, tempII)) {
738 return false;
739 }
Brian Salomon6196fc12018-07-12 16:39:23 -0400740 if (!tempPixmap.tryAlloc(tempII)) {
741 return false;
742 }
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400743 finalPixmap.reset(finalII, buffer, rowBytes);
744 buffer = tempPixmap.writable_addr();
745 rowBytes = tempPixmap.rowBytes();
Brian Salomon3d945e82018-05-14 14:54:06 -0400746 // Chrome msan bots require this.
747 sk_bzero(buffer, tempPixmap.computeByteSize());
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400748 }
749
750 if (srcSurface->surfacePriv().hasPendingWrite()) {
751 this->flush(nullptr); // MDB TODO: tighten this
752 }
753
754 if (!fContext->fGpu->readPixels(srcSurface, left, top, width, height, allowedColorType, buffer,
755 rowBytes)) {
756 return false;
757 }
758
759 if (flip) {
760 size_t trimRowBytes = GrColorTypeBytesPerPixel(allowedColorType) * width;
761 std::unique_ptr<char[]> row(new char[trimRowBytes]);
762 char* upper = reinterpret_cast<char*>(buffer);
763 char* lower = reinterpret_cast<char*>(buffer) + (height - 1) * rowBytes;
764 for (int y = 0; y < height / 2; ++y, upper += rowBytes, lower -= rowBytes) {
765 memcpy(row.get(), upper, trimRowBytes);
766 memcpy(upper, lower, trimRowBytes);
767 memcpy(lower, row.get(), trimRowBytes);
768 }
769 }
770 if (convert) {
771 if (!tempPixmap.readPixels(finalPixmap)) {
772 return false;
773 }
774 }
775 return true;
776}
777
Robert Phillips7ee385e2017-03-30 08:02:11 -0400778void GrContextPriv::prepareSurfaceForExternalIO(GrSurfaceProxy* proxy) {
779 ASSERT_SINGLE_OWNER_PRIV
780 RETURN_IF_ABANDONED_PRIV
781 SkASSERT(proxy);
782 ASSERT_OWNED_PROXY_PRIV(proxy);
Greg Daniel51316782017-08-02 15:10:09 +0000783 fContext->fDrawingManager->prepareSurfaceForExternalIO(proxy, 0, nullptr);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000784}
785
Robert Phillips7ee385e2017-03-30 08:02:11 -0400786void GrContextPriv::flushSurfaceWrites(GrSurfaceProxy* proxy) {
787 ASSERT_SINGLE_OWNER_PRIV
788 RETURN_IF_ABANDONED_PRIV
789 SkASSERT(proxy);
790 ASSERT_OWNED_PROXY_PRIV(proxy);
791 if (proxy->priv().hasPendingWrite()) {
792 this->flush(proxy);
bsalomonf80bfed2014-10-07 05:56:02 -0700793 }
794}
795
Robert Phillips7ee385e2017-03-30 08:02:11 -0400796void GrContextPriv::flushSurfaceIO(GrSurfaceProxy* proxy) {
797 ASSERT_SINGLE_OWNER_PRIV
798 RETURN_IF_ABANDONED_PRIV
799 SkASSERT(proxy);
800 ASSERT_OWNED_PROXY_PRIV(proxy);
801 if (proxy->priv().hasPendingIO()) {
802 this->flush(proxy);
ajuma95243eb2016-08-24 08:19:02 -0700803 }
804}
805
bsalomon@google.com27847de2011-02-22 20:59:41 +0000806////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000807
Robert Phillipsc994a932018-06-19 13:09:54 -0400808sk_sp<GrOpMemoryPool> GrContextPriv::refOpMemoryPool() {
809 if (!fContext->fOpMemoryPool) {
810 // DDL TODO: should the size of the memory pool be decreased in DDL mode? CPU-side memory
811 // consumed in DDL mode vs. normal mode for a single skp might be a good metric of wasted
812 // memory.
813 fContext->fOpMemoryPool = sk_sp<GrOpMemoryPool>(new GrOpMemoryPool(16384, 16384));
814 }
815
816 SkASSERT(fContext->fOpMemoryPool);
817 return fContext->fOpMemoryPool;
818}
819
820GrOpMemoryPool* GrContextPriv::opMemoryPool() {
821 return this->refOpMemoryPool().get();
822}
823
Robert Phillips2c862492017-01-18 10:08:39 -0500824sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy> proxy,
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500825 sk_sp<SkColorSpace> colorSpace,
826 const SkSurfaceProps* props) {
Brian Osman45580d32016-11-23 09:37:01 -0500827 ASSERT_SINGLE_OWNER_PRIV
828
Brian Osman45580d32016-11-23 09:37:01 -0500829 if (proxy->asRenderTargetProxy()) {
Robert Phillips2c862492017-01-18 10:08:39 -0500830 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500831 std::move(colorSpace), props);
Brian Osman45580d32016-11-23 09:37:01 -0500832 } else {
833 SkASSERT(proxy->asTextureProxy());
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500834 SkASSERT(!props);
Robert Phillips2c862492017-01-18 10:08:39 -0500835 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Brian Osman45580d32016-11-23 09:37:01 -0500836 }
837}
838
Greg Daniel4065d452018-11-16 15:43:41 -0500839sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(const GrBackendFormat& format,
840 const GrSurfaceDesc& dstDesc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500841 GrSurfaceOrigin origin,
Greg Daniel65c7f662017-10-30 13:39:09 -0400842 GrMipMapped mipMapped,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500843 SkBackingFit fit,
Brian Salomon366093f2018-02-13 09:25:22 -0500844 SkBudgeted isDstBudgeted,
845 sk_sp<SkColorSpace> colorSpace,
846 const SkSurfaceProps* props) {
Greg Daniel65c7f662017-10-30 13:39:09 -0400847 sk_sp<GrTextureProxy> proxy;
848 if (GrMipMapped::kNo == mipMapped) {
Greg Daniel4065d452018-11-16 15:43:41 -0500849 proxy = this->proxyProvider()->createProxy(format, dstDesc, origin, fit, isDstBudgeted);
Greg Daniel65c7f662017-10-30 13:39:09 -0400850 } else {
851 SkASSERT(SkBackingFit::kExact == fit);
Greg Daniel4065d452018-11-16 15:43:41 -0500852 proxy = this->proxyProvider()->createMipMapProxy(format, dstDesc, origin, isDstBudgeted);
Greg Daniel65c7f662017-10-30 13:39:09 -0400853 }
Robert Phillips77b3f322017-01-31 18:24:12 -0500854 if (!proxy) {
855 return nullptr;
856 }
Robert Phillipse2f7d182016-12-15 09:23:05 -0500857
Greg Daniel55fa6472018-03-16 16:13:10 -0400858 sk_sp<GrSurfaceContext> sContext = this->makeWrappedSurfaceContext(std::move(proxy),
859 std::move(colorSpace),
860 props);
861 if (sContext && sContext->asRenderTargetContext()) {
862 sContext->asRenderTargetContext()->discard();
863 }
864
865 return sContext;
Robert Phillipse2f7d182016-12-15 09:23:05 -0500866}
867
Brian Salomond17f6582017-07-19 18:28:58 -0400868sk_sp<GrTextureContext> GrContextPriv::makeBackendTextureContext(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000869 GrSurfaceOrigin origin,
Brian Osmanc1e37052017-03-09 14:19:20 -0500870 sk_sp<SkColorSpace> colorSpace) {
Robert Phillips26caf892017-01-27 10:58:31 -0500871 ASSERT_SINGLE_OWNER_PRIV
872
Brian Salomonc67c31c2018-12-06 10:00:03 -0500873 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->wrapBackendTexture(
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500874 tex, origin, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType);
Robert Phillips77b3f322017-01-31 18:24:12 -0500875 if (!proxy) {
876 return nullptr;
877 }
Robert Phillips26caf892017-01-27 10:58:31 -0500878
Brian Salomond17f6582017-07-19 18:28:58 -0400879 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Robert Phillips26caf892017-01-27 10:58:31 -0500880}
881
Brian Osman11052242016-10-27 14:47:55 -0400882sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000883 const GrBackendTexture& tex,
884 GrSurfaceOrigin origin,
885 int sampleCnt,
Brian Osman11052242016-10-27 14:47:55 -0400886 sk_sp<SkColorSpace> colorSpace,
Brian Osmanc1e37052017-03-09 14:19:20 -0500887 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700888 ASSERT_SINGLE_OWNER_PRIV
Brian Salomonbdecacf2018-02-02 20:32:49 -0500889 SkASSERT(sampleCnt > 0);
robertphillips4fd74ae2016-08-03 14:26:53 -0700890
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500891 sk_sp<GrTextureProxy> proxy(this->proxyProvider()->wrapRenderableBackendTexture(
892 tex, origin, sampleCnt, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo));
Robert Phillips77b3f322017-01-31 18:24:12 -0500893 if (!proxy) {
894 return nullptr;
895 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400896
Robert Phillips37430132016-11-09 06:50:43 -0500897 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Brian Osman11052242016-10-27 14:47:55 -0400898 std::move(colorSpace), props);
robertphillips4fd74ae2016-08-03 14:26:53 -0700899}
900
Brian Osman11052242016-10-27 14:47:55 -0400901sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext(
Greg Danielbcf612b2017-05-01 13:50:58 +0000902 const GrBackendRenderTarget& backendRT,
903 GrSurfaceOrigin origin,
robertphillips4fd74ae2016-08-03 14:26:53 -0700904 sk_sp<SkColorSpace> colorSpace,
905 const SkSurfaceProps* surfaceProps) {
906 ASSERT_SINGLE_OWNER_PRIV
907
Brian Salomon7578f3e2018-03-07 14:39:54 -0500908 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->wrapBackendRenderTarget(backendRT, origin);
Robert Phillips77b3f322017-01-31 18:24:12 -0500909 if (!proxy) {
910 return nullptr;
911 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400912
Robert Phillips37430132016-11-09 06:50:43 -0500913 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400914 std::move(colorSpace),
Brian Osman11052242016-10-27 14:47:55 -0400915 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -0700916}
917
Brian Osman11052242016-10-27 14:47:55 -0400918sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000919 const GrBackendTexture& tex,
920 GrSurfaceOrigin origin,
921 int sampleCnt,
robertphillips4fd74ae2016-08-03 14:26:53 -0700922 sk_sp<SkColorSpace> colorSpace,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500923 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700924 ASSERT_SINGLE_OWNER_PRIV
Brian Salomonbdecacf2018-02-02 20:32:49 -0500925 SkASSERT(sampleCnt > 0);
Brian Salomon7578f3e2018-03-07 14:39:54 -0500926 sk_sp<GrSurfaceProxy> proxy(
927 this->proxyProvider()->wrapBackendTextureAsRenderTarget(tex, origin, sampleCnt));
Robert Phillips77b3f322017-01-31 18:24:12 -0500928 if (!proxy) {
929 return nullptr;
930 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400931
Robert Phillips37430132016-11-09 06:50:43 -0500932 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400933 std::move(colorSpace),
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500934 props);
robertphillips77a2e522015-10-17 07:43:27 -0700935}
936
Greg Danielb46add82019-01-02 14:51:29 -0500937sk_sp<GrRenderTargetContext> GrContextPriv::makeVulkanSecondaryCBRenderTargetContext(
938 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo, const SkSurfaceProps* props) {
939 ASSERT_SINGLE_OWNER_PRIV
940 sk_sp<GrSurfaceProxy> proxy(
941 this->proxyProvider()->wrapVulkanSecondaryCBAsRenderTarget(imageInfo, vkInfo));
942 if (!proxy) {
943 return nullptr;
944 }
945
946 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
947 imageInfo.refColorSpace(),
948 props);
949}
950
Chris Daltonfe199b72017-05-05 11:26:15 -0400951void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
952 fContext->fDrawingManager->addOnFlushCallbackObject(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400953}
954
Robert Phillips62000362018-02-01 09:10:04 -0500955void GrContextPriv::moveOpListsToDDL(SkDeferredDisplayList* ddl) {
956 fContext->fDrawingManager->moveOpListsToDDL(ddl);
957}
958
959void GrContextPriv::copyOpListsFromDDL(const SkDeferredDisplayList* ddl,
960 GrRenderTargetProxy* newDest) {
961 fContext->fDrawingManager->copyOpListsFromDDL(ddl, newDest);
962}
963
robertphillips48fde9c2016-09-06 05:20:20 -0700964static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) {
Brian Osman78f20e02017-01-12 10:28:01 -0500965 switch (config) {
966 case kAlpha_8_GrPixelConfig:
Greg Daniel93b4ddd2018-04-16 16:16:47 -0400967 case kAlpha_8_as_Alpha_GrPixelConfig:
968 case kAlpha_8_as_Red_GrPixelConfig:
Brian Osman78f20e02017-01-12 10:28:01 -0500969 case kRGB_565_GrPixelConfig:
970 case kRGBA_4444_GrPixelConfig:
971 case kBGRA_8888_GrPixelConfig:
Brian Osman10fc6fd2018-03-02 11:01:10 -0500972 case kRGBA_1010102_GrPixelConfig:
Brian Osman9c111352018-10-16 10:18:26 -0400973 case kRGBA_half_GrPixelConfig:
Brian Osman78f20e02017-01-12 10:28:01 -0500974 return kRGBA_8888_GrPixelConfig;
975 case kSBGRA_8888_GrPixelConfig:
976 return kSRGBA_8888_GrPixelConfig;
977 case kAlpha_half_GrPixelConfig:
Greg Daniel93b4ddd2018-04-16 16:16:47 -0400978 case kAlpha_half_as_Red_GrPixelConfig:
Brian Osman78f20e02017-01-12 10:28:01 -0500979 return kRGBA_half_GrPixelConfig;
Greg Daniel93b4ddd2018-04-16 16:16:47 -0400980 case kGray_8_GrPixelConfig:
981 case kGray_8_as_Lum_GrPixelConfig:
982 case kGray_8_as_Red_GrPixelConfig:
983 return kRGB_888_GrPixelConfig;
Brian Osman78f20e02017-01-12 10:28:01 -0500984 default:
985 return kUnknown_GrPixelConfig;
986 }
robertphillips48fde9c2016-09-06 05:20:20 -0700987}
988
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500989sk_sp<GrRenderTargetContext> GrContextPriv::makeDeferredRenderTargetContextWithFallback(
Greg Daniel4065d452018-11-16 15:43:41 -0500990 const GrBackendFormat& format,
robertphillipsd728f0c2016-11-21 11:05:03 -0800991 SkBackingFit fit,
992 int width, int height,
993 GrPixelConfig config,
994 sk_sp<SkColorSpace> colorSpace,
995 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -0400996 GrMipMapped mipMapped,
robertphillipsd728f0c2016-11-21 11:05:03 -0800997 GrSurfaceOrigin origin,
998 const SkSurfaceProps* surfaceProps,
999 SkBudgeted budgeted) {
Greg Daniel4065d452018-11-16 15:43:41 -05001000 GrBackendFormat localFormat = format;
Brian Salomonbdecacf2018-02-02 20:32:49 -05001001 SkASSERT(sampleCnt > 0);
Brian Salomonc7fe0f72018-05-11 10:14:21 -04001002 if (0 == fContext->contextPriv().caps()->getRenderTargetSampleCount(sampleCnt, config)) {
robertphillipsd728f0c2016-11-21 11:05:03 -08001003 config = GrPixelConfigFallback(config);
Greg Daniel4065d452018-11-16 15:43:41 -05001004 // TODO: First we should be checking the getRenderTargetSampleCount from the GrBackendFormat
1005 // and not GrPixelConfig. Besides that, we should implement the fallback in the caps, but
1006 // for now we just convert the fallback pixel config to an SkColorType and then get the
1007 // GrBackendFormat from that.
1008 SkColorType colorType;
1009 if (!GrPixelConfigToColorType(config, &colorType)) {
1010 return nullptr;
1011 }
1012 localFormat = fContext->fCaps->getBackendFormatFromColorType(colorType);
robertphillipsd728f0c2016-11-21 11:05:03 -08001013 }
1014
Greg Daniel4065d452018-11-16 15:43:41 -05001015 return this->makeDeferredRenderTargetContext(localFormat, fit, width, height, config,
1016 std::move(colorSpace), sampleCnt, mipMapped,
1017 origin, surfaceProps, budgeted);
robertphillipsd728f0c2016-11-21 11:05:03 -08001018}
1019
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001020sk_sp<GrRenderTargetContext> GrContextPriv::makeDeferredRenderTargetContext(
Greg Daniel4065d452018-11-16 15:43:41 -05001021 const GrBackendFormat& format,
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001022 SkBackingFit fit,
1023 int width, int height,
1024 GrPixelConfig config,
1025 sk_sp<SkColorSpace> colorSpace,
1026 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -04001027 GrMipMapped mipMapped,
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001028 GrSurfaceOrigin origin,
1029 const SkSurfaceProps* surfaceProps,
1030 SkBudgeted budgeted) {
Brian Salomonbdecacf2018-02-02 20:32:49 -05001031 SkASSERT(sampleCnt > 0);
Khushalc421ca12018-06-26 14:38:34 -07001032 if (fContext->abandoned()) {
Brian Salomon79e4d1b2017-07-13 11:17:11 -04001033 return nullptr;
1034 }
1035
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001036 GrSurfaceDesc desc;
1037 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001038 desc.fWidth = width;
1039 desc.fHeight = height;
1040 desc.fConfig = config;
1041 desc.fSampleCnt = sampleCnt;
1042
Greg Daniele1da1d92017-10-06 15:59:27 -04001043 sk_sp<GrTextureProxy> rtp;
Greg Daniel45d63032017-10-30 13:41:26 -04001044 if (GrMipMapped::kNo == mipMapped) {
Greg Daniel4065d452018-11-16 15:43:41 -05001045 rtp = fContext->fProxyProvider->createProxy(format, desc, origin, fit, budgeted);
Greg Daniele1da1d92017-10-06 15:59:27 -04001046 } else {
Greg Daniel4065d452018-11-16 15:43:41 -05001047 rtp = fContext->fProxyProvider->createMipMapProxy(format, desc, origin, budgeted);
Greg Daniele1da1d92017-10-06 15:59:27 -04001048 }
Robert Phillips08c5ec72017-01-30 12:26:47 -05001049 if (!rtp) {
1050 return nullptr;
1051 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001052
Robert Phillips1119dc32017-04-11 12:54:57 -04001053 sk_sp<GrRenderTargetContext> renderTargetContext(
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001054 fContext->fDrawingManager->makeRenderTargetContext(std::move(rtp),
1055 std::move(colorSpace),
1056 surfaceProps));
Robert Phillips1119dc32017-04-11 12:54:57 -04001057 if (!renderTargetContext) {
1058 return nullptr;
1059 }
1060
1061 renderTargetContext->discard();
1062
1063 return renderTargetContext;
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001064}
1065
Robert Phillipsa0bc39d2019-01-29 13:14:47 -05001066sk_sp<GrSkSLFPFactoryCache> GrContextPriv::getFPFactoryCache() { return fContext->fFPFactoryCache; }
1067
Brian Salomonaff329b2017-08-11 09:40:37 -04001068std::unique_ptr<GrFragmentProcessor> GrContext::createPMToUPMEffect(
Brian Osman5ea96bf2018-10-02 14:58:05 -04001069 std::unique_ptr<GrFragmentProcessor> fp) {
Robert Phillips757914d2017-01-25 15:48:30 -05001070 ASSERT_SINGLE_OWNER
Brian Osman5ea96bf2018-10-02 14:58:05 -04001071 // We should have already called this->validPMUPMConversionExists() in this case
1072 SkASSERT(fDidTestPMConversions);
1073 // ...and it should have succeeded
1074 SkASSERT(this->validPMUPMConversionExists());
Brian Osman409e74f2017-04-17 11:48:28 -04001075
Brian Osman5ea96bf2018-10-02 14:58:05 -04001076 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul);
Robert Phillips757914d2017-01-25 15:48:30 -05001077}
1078
Brian Salomonaff329b2017-08-11 09:40:37 -04001079std::unique_ptr<GrFragmentProcessor> GrContext::createUPMToPMEffect(
Brian Osman5ea96bf2018-10-02 14:58:05 -04001080 std::unique_ptr<GrFragmentProcessor> fp) {
joshualitt1de610a2016-01-06 08:26:09 -08001081 ASSERT_SINGLE_OWNER
Brian Osman5ea96bf2018-10-02 14:58:05 -04001082 // We should have already called this->validPMUPMConversionExists() in this case
1083 SkASSERT(fDidTestPMConversions);
1084 // ...and it should have succeeded
1085 SkASSERT(this->validPMUPMConversionExists());
Brian Osman409e74f2017-04-17 11:48:28 -04001086
Brian Osman5ea96bf2018-10-02 14:58:05 -04001087 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001088}
1089
Brian Osman409e74f2017-04-17 11:48:28 -04001090bool GrContext::validPMUPMConversionExists() {
joshualitt1de610a2016-01-06 08:26:09 -08001091 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -04001092 if (!fDidTestPMConversions) {
Brian Osman28804f32017-04-20 10:24:36 -04001093 fPMUPMConversionsRoundTrip = GrConfigConversionEffect::TestForPreservingPMConversions(this);
Brian Osman409e74f2017-04-17 11:48:28 -04001094 fDidTestPMConversions = true;
1095 }
1096
bsalomon636e8022015-07-29 06:08:46 -07001097 // The PM<->UPM tests fail or succeed together so we only need to check one.
Brian Osman28804f32017-04-20 10:24:36 -04001098 return fPMUPMConversionsRoundTrip;
bsalomon636e8022015-07-29 06:08:46 -07001099}
1100
Khushal3e7548c2018-05-23 15:45:01 -07001101bool GrContext::supportsDistanceFieldText() const {
1102 return fCaps->shaderCaps()->supportsDistanceFieldText();
1103}
1104
bsalomon37f9a262015-02-02 13:00:10 -08001105//////////////////////////////////////////////////////////////////////////////
1106
Robert Phillipsfc711a22018-02-13 17:03:00 -05001107// DDL TODO: remove 'maxResources'
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001108void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -08001109 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001110 if (maxResources) {
1111 *maxResources = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -08001112 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001113 if (maxResourceBytes) {
1114 *maxResourceBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -08001115 }
1116}
1117
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001118void GrContext::setResourceCacheLimits(int maxResources, size_t maxResourceBytes) {
joshualitt1de610a2016-01-06 08:26:09 -08001119 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001120 fResourceCache->setLimits(maxResources, maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -08001121}
1122
ericrk0a5fa482015-09-15 14:16:10 -07001123//////////////////////////////////////////////////////////////////////////////
ericrk0a5fa482015-09-15 14:16:10 -07001124void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -08001125 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -07001126 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
Khushal71652e22018-10-29 13:05:36 -07001127 traceMemoryDump->dumpNumericValue("skia/gr_text_blob_cache", "size", "bytes",
1128 fTextBlobCache->usedBytes());
ericrk0a5fa482015-09-15 14:16:10 -07001129}
Brian Osman71a18892017-08-10 10:23:25 -04001130
1131//////////////////////////////////////////////////////////////////////////////
Kevin Lubickf4def342018-10-04 12:52:50 -04001132#ifdef SK_ENABLE_DUMP_GPU
1133#include "SkJSONWriter.h"
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001134SkString GrContextPriv::dump() const {
Brian Osman71a18892017-08-10 10:23:25 -04001135 SkDynamicMemoryWStream stream;
1136 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
1137 writer.beginObject();
1138
1139 static const char* kBackendStr[] = {
1140 "Metal",
1141 "OpenGL",
1142 "Vulkan",
1143 "Mock",
1144 };
Greg Danielbdf12ad2018-10-12 09:31:11 -04001145 GR_STATIC_ASSERT(0 == (unsigned)GrBackendApi::kMetal);
1146 GR_STATIC_ASSERT(1 == (unsigned)GrBackendApi::kOpenGL);
1147 GR_STATIC_ASSERT(2 == (unsigned)GrBackendApi::kVulkan);
1148 GR_STATIC_ASSERT(3 == (unsigned)GrBackendApi::kMock);
Robert Phillips4217ea72019-01-30 13:08:28 -05001149 writer.appendString("backend", kBackendStr[(unsigned)fContext->backend()]);
Brian Osman71a18892017-08-10 10:23:25 -04001150
1151 writer.appendName("caps");
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001152 fContext->fCaps->dumpJSON(&writer);
Brian Osman71a18892017-08-10 10:23:25 -04001153
1154 writer.appendName("gpu");
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001155 fContext->fGpu->dumpJSON(&writer);
Brian Osman71a18892017-08-10 10:23:25 -04001156
1157 // Flush JSON to the memory stream
1158 writer.endObject();
1159 writer.flush();
1160
1161 // Null terminate the JSON data in the memory stream
1162 stream.write8(0);
1163
1164 // Allocate a string big enough to hold all the data, then copy out of the stream
1165 SkString result(stream.bytesWritten());
1166 stream.copyToAndReset(result.writable_str());
1167 return result;
1168}
Kevin Lubickf4def342018-10-04 12:52:50 -04001169#endif