blob: e1588aed161c3f8896695f2c6cbe67573305741a [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
Robert Phillipsc1541ae2019-02-04 12:05:37 -050062GrContext::GrContext(GrBackendApi backend, const GrContextOptions& options, int32_t id)
63 : INHERITED(backend, options, 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 Phillipsbb606772019-02-04 17:50:57 -050070bool GrContext::init(sk_sp<const GrCaps> caps, sk_sp<GrSkSLFPFactoryCache> FPFactoryCache) {
Greg Danielb76a72a2017-07-13 15:07:54 -040071 ASSERT_SINGLE_OWNER
Robert Phillipsfde6fa02018-03-02 08:53:14 -050072 SkASSERT(fThreadSafeProxy); // needs to have been initialized by derived classes
Robert Phillips88260b52018-01-19 12:56:09 -050073
Robert Phillipsbb606772019-02-04 17:50:57 -050074 if (!INHERITED::init(std::move(caps), std::move(FPFactoryCache))) {
75 return false;
76 }
77
78 SkASSERT(this->caps());
79
Robert Phillips88260b52018-01-19 12:56:09 -050080 if (fGpu) {
Robert Phillipsbb606772019-02-04 17:50:57 -050081 fResourceCache = new GrResourceCache(this->caps(), &fSingleOwner, this->contextID());
Robert Phillips4150eea2018-02-07 17:08:21 -050082 fResourceProvider = new GrResourceProvider(fGpu.get(), fResourceCache, &fSingleOwner,
Robert Phillipsc1541ae2019-02-04 12:05:37 -050083 this->options().fExplicitlyAllocateGPUResources);
Robert Phillipsbb606772019-02-04 17:50:57 -050084 fProxyProvider = new GrProxyProvider(fResourceProvider, fResourceCache,
85 this->refCaps(), &fSingleOwner);
Brian Salomon238069b2018-07-11 15:58:57 -040086 } else {
Robert Phillipsbb606772019-02-04 17:50:57 -050087 fProxyProvider = new GrProxyProvider(this->contextID(), this->refCaps(), &fSingleOwner);
Robert Phillips88260b52018-01-19 12:56:09 -050088 }
89
Robert Phillips88260b52018-01-19 12:56:09 -050090 if (fResourceCache) {
91 fResourceCache->setProxyProvider(fProxyProvider);
92 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -050093
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000094 fDidTestPMConversions = false;
95
bsalomon6b2552f2016-09-15 13:50:26 -070096 GrPathRendererChain::Options prcOptions;
Robert Phillipsc1541ae2019-02-04 12:05:37 -050097 prcOptions.fAllowPathMaskCaching = this->options().fAllowPathMaskCaching;
Brian Osman195c05b2017-08-30 15:14:04 -040098#if GR_TEST_UTILS
Robert Phillipsc1541ae2019-02-04 12:05:37 -050099 prcOptions.fGpuPathRenderers = this->options().fGpuPathRenderers;
Brian Osman195c05b2017-08-30 15:14:04 -0400100#endif
Robert Phillipsc1541ae2019-02-04 12:05:37 -0500101 if (this->options().fDisableCoverageCountingPaths) {
Chris Daltonef125092018-06-26 18:16:47 -0600102 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kCoverageCounting;
103 }
Robert Phillipsc1541ae2019-02-04 12:05:37 -0500104 if (this->options().fDisableDistanceFieldPaths) {
Brian Osman195c05b2017-08-30 15:14:04 -0400105 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
Brian Osmanb350ae22017-08-29 16:15:39 -0400106 }
Brian Salomonaf597482017-11-07 16:23:34 -0500107
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500108 if (!fResourceCache) {
109 // DDL TODO: remove this crippling of the path renderer chain
110 // Disable the small path renderer bc of the proxies in the atlas. They need to be
111 // unified when the opLists are added back to the destination drawing manager.
112 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kSmall;
Robert Phillips5ffcb4d2018-07-03 16:39:40 -0400113 prcOptions.fGpuPathRenderers &= ~GpuPathRenderers::kStencilAndCover;
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500114 }
115
Herb Derby26cbe512018-05-24 14:39:01 -0400116 GrTextContext::Options textContextOptions;
Robert Phillipsc1541ae2019-02-04 12:05:37 -0500117 textContextOptions.fMaxDistanceFieldFontSize = this->options().fGlyphsAsPathsFontSize;
118 textContextOptions.fMinDistanceFieldFontSize = this->options().fMinDistanceFieldFontSize;
Herb Derby26cbe512018-05-24 14:39:01 -0400119 textContextOptions.fDistanceFieldVerticesAlwaysHaveW = false;
Brian Salomonb5086962017-12-13 10:59:33 -0500120#if SK_SUPPORT_ATLAS_TEXT
Robert Phillipsc1541ae2019-02-04 12:05:37 -0500121 if (GrContextOptions::Enable::kYes == this->options().fDistanceFieldGlyphVerticesAlwaysHaveW) {
Herb Derby26cbe512018-05-24 14:39:01 -0400122 textContextOptions.fDistanceFieldVerticesAlwaysHaveW = true;
Brian Salomonb5086962017-12-13 10:59:33 -0500123 }
124#endif
Brian Salomonaf597482017-11-07 16:23:34 -0500125
Robert Phillips64ecdce2018-04-02 10:26:39 -0400126 bool explicitlyAllocatingResources = fResourceProvider
127 ? fResourceProvider->explicitlyAllocateGPUResources()
128 : false;
Herb Derby26cbe512018-05-24 14:39:01 -0400129 fDrawingManager.reset(new GrDrawingManager(this, prcOptions, textContextOptions,
Robert Phillips64ecdce2018-04-02 10:26:39 -0400130 &fSingleOwner, explicitlyAllocatingResources,
Robert Phillipsc1541ae2019-02-04 12:05:37 -0500131 this->options().fSortRenderTargets,
132 this->options().fReduceOpListSplitting));
joshualitt7c3a2f82015-03-31 13:32:05 -0700133
Robert Phillipsbb606772019-02-04 17:50:57 -0500134 fGlyphCache = new GrStrikeCache(this->caps(), this->options().fGlyphCacheTextureMaximumBytes);
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500135
Robert Phillipsfd0d9702019-02-01 10:19:42 -0500136 fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB, this, this->contextID()));
Brian Salomon91a3e522017-06-23 10:58:19 -0400137
Robert Phillipsfde6fa02018-03-02 08:53:14 -0500138 // DDL TODO: we need to think through how the task group & persistent cache
139 // get passed on to/shared between all the DDLRecorders created with this context.
Robert Phillipsc1541ae2019-02-04 12:05:37 -0500140 if (this->options().fExecutor) {
141 fTaskGroup = skstd::make_unique<SkTaskGroup>(*this->options().fExecutor);
Brian Osman51279982017-08-23 10:12:00 -0400142 }
143
Robert Phillipsc1541ae2019-02-04 12:05:37 -0500144 fPersistentCache = this->options().fPersistentCache;
Ethan Nicholasd1b2eec2017-11-01 15:45:43 -0400145
Brian Salomon91a3e522017-06-23 10:58:19 -0400146 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000147}
148
bsalomon@google.com27847de2011-02-22 20:59:41 +0000149GrContext::~GrContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800150 ASSERT_SINGLE_OWNER
151
Robert Phillips2e6feed2018-01-22 15:27:20 -0500152 if (fDrawingManager) {
153 fDrawingManager->cleanup();
154 }
halcanary385fe4d2015-08-26 13:07:48 -0700155 delete fResourceProvider;
156 delete fResourceCache;
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500157 delete fProxyProvider;
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500158 delete fGlyphCache;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000159}
160
Robert Phillips4217ea72019-01-30 13:08:28 -0500161sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
162 return fThreadSafeProxy;
163}
164
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400165//////////////////////////////////////////////////////////////////////////////
166
bsalomon2354f842014-07-28 13:48:36 -0700167void GrContext::abandonContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800168 ASSERT_SINGLE_OWNER
169
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500170 fProxyProvider->abandon();
bsalomond309e7a2015-04-30 14:18:54 -0700171 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800172
173 // Need to abandon the drawing manager first so all the render targets
174 // will be released/forgotten before they too are abandoned.
175 fDrawingManager->abandon();
176
bsalomon@google.com205d4602011-04-25 12:43:45 +0000177 // abandon first to so destructors
178 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800179 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700180
bsalomon6e2aad42016-04-01 11:54:31 -0700181 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
182
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500183 fGlyphCache->freeAll();
bsalomon6e2aad42016-04-01 11:54:31 -0700184 fTextBlobCache->freeAll();
185}
186
Khushalc421ca12018-06-26 14:38:34 -0700187bool GrContext::abandoned() const {
188 ASSERT_SINGLE_OWNER
Brian Salomon9bc76d92019-01-24 12:18:33 -0500189 // If called from ~GrContext(), the drawing manager may already be gone.
190 return !fDrawingManager || fDrawingManager->wasAbandoned();
Khushalc421ca12018-06-26 14:38:34 -0700191}
192
bsalomon6e2aad42016-04-01 11:54:31 -0700193void GrContext::releaseResourcesAndAbandonContext() {
194 ASSERT_SINGLE_OWNER
195
Brian Salomon614c1a82018-12-19 15:42:06 -0500196 if (this->abandoned()) {
197 return;
198 }
Robert Phillips1afd4cd2018-01-08 13:40:32 -0500199 fProxyProvider->abandon();
bsalomon6e2aad42016-04-01 11:54:31 -0700200 fResourceProvider->abandon();
201
202 // Need to abandon the drawing manager first so all the render targets
203 // will be released/forgotten before they too are abandoned.
204 fDrawingManager->abandon();
205
206 // Release all resources in the backend 3D API.
207 fResourceCache->releaseAll();
208
209 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000210
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500211 fGlyphCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700212 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000213}
214
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000215void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800216 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000217 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000218}
219
220void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800221 ASSERT_SINGLE_OWNER
222
Robert Phillipsc4039ea2018-03-01 11:36:45 -0500223 fGlyphCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700224
225 fDrawingManager->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700226
227 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000228}
229
Robert Phillips6eba0632018-03-28 12:25:42 -0400230void GrContext::purgeUnlockedResources(bool scratchResourcesOnly) {
231 ASSERT_SINGLE_OWNER
232 fResourceCache->purgeUnlockedResources(scratchResourcesOnly);
233 fResourceCache->purgeAsNeeded();
234 fTextBlobCache->purgeStaleBlobs();
235}
236
Jim Van Verth76d917c2017-12-13 09:26:37 -0500237void GrContext::performDeferredCleanup(std::chrono::milliseconds msNotUsed) {
Brian Salomon5e150852017-03-22 14:53:13 -0400238 ASSERT_SINGLE_OWNER
Chris Dalton6c3879d2018-11-01 11:13:19 -0600239
240 auto purgeTime = GrStdSteadyClock::now() - msNotUsed;
241
Jim Van Verth76d917c2017-12-13 09:26:37 -0500242 fResourceCache->purgeAsNeeded();
Chris Dalton6c3879d2018-11-01 11:13:19 -0600243 fResourceCache->purgeResourcesNotUsedSince(purgeTime);
244
245 if (auto ccpr = fDrawingManager->getCoverageCountingPathRenderer()) {
Chris Dalton351e80c2019-01-06 22:51:00 -0700246 ccpr->purgeCacheEntriesOlderThan(fProxyProvider, purgeTime);
Chris Dalton6c3879d2018-11-01 11:13:19 -0600247 }
Jim Van Verth76d917c2017-12-13 09:26:37 -0500248
249 fTextBlobCache->purgeStaleBlobs();
Brian Salomon5e150852017-03-22 14:53:13 -0400250}
251
Derek Sollenberger5480a182017-05-25 16:43:59 -0400252void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
253 ASSERT_SINGLE_OWNER
254 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
255}
256
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000257void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800258 ASSERT_SINGLE_OWNER
259
bsalomon71cb0c22014-11-14 12:10:14 -0800260 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800261 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800262 }
263 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800264 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800265 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000266}
267
Derek Sollenbergeree479142017-05-24 11:41:33 -0400268size_t GrContext::getResourceCachePurgeableBytes() const {
269 ASSERT_SINGLE_OWNER
270 return fResourceCache->getPurgeableBytes();
271}
272
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000273////////////////////////////////////////////////////////////////////////////////
274
Robert Phillipsbb606772019-02-04 17:50:57 -0500275int GrContext::maxTextureSize() const { return this->caps()->maxTextureSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400276
Robert Phillipsbb606772019-02-04 17:50:57 -0500277int GrContext::maxRenderTargetSize() const { return this->caps()->maxRenderTargetSize(); }
Brian Salomonf932a632018-04-05 12:46:09 -0400278
Brian Salomonbdecacf2018-02-02 20:32:49 -0500279bool GrContext::colorTypeSupportedAsImage(SkColorType colorType) const {
Brian Osman2b23c4b2018-06-01 12:25:08 -0400280 GrPixelConfig config = SkColorType2GrPixelConfig(colorType);
Robert Phillipsbb606772019-02-04 17:50:57 -0500281 return this->caps()->isConfigTexturable(config);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500282}
283
284int GrContext::maxSurfaceSampleCountForColorType(SkColorType colorType) const {
Brian Osman2b23c4b2018-06-01 12:25:08 -0400285 GrPixelConfig config = SkColorType2GrPixelConfig(colorType);
Robert Phillipsbb606772019-02-04 17:50:57 -0500286 return this->caps()->maxRenderTargetSampleCount(config);
Brian Salomonbdecacf2018-02-02 20:32:49 -0500287}
288
289////////////////////////////////////////////////////////////////////////////////
290
joshualitt0db6dfa2015-04-10 07:01:30 -0700291void GrContext::TextBlobCacheOverBudgetCB(void* data) {
292 SkASSERT(data);
Brian Osman11052242016-10-27 14:47:55 -0400293 // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on
294 // GrRenderTargetContext to perform a necessary flush. The solution is to move drawText calls
295 // to below the GrContext level, but this is not trivial because they call drawPath on
296 // SkGpuDevice.
joshualitt0db6dfa2015-04-10 07:01:30 -0700297 GrContext* context = reinterpret_cast<GrContext*>(data);
298 context->flush();
299}
300
bsalomon@google.com27847de2011-02-22 20:59:41 +0000301////////////////////////////////////////////////////////////////////////////////
302
bsalomonb77a9072016-09-07 10:02:04 -0700303void GrContext::flush() {
joshualitt1de610a2016-01-06 08:26:09 -0800304 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700305 RETURN_IF_ABANDONED
Robert Phillips7ee385e2017-03-30 08:02:11 -0400306
307 fDrawingManager->flush(nullptr);
308}
309
Greg Daniel51316782017-08-02 15:10:09 +0000310GrSemaphoresSubmitted GrContext::flushAndSignalSemaphores(int numSemaphores,
311 GrBackendSemaphore signalSemaphores[]) {
312 ASSERT_SINGLE_OWNER
313 if (fDrawingManager->wasAbandoned()) { return GrSemaphoresSubmitted::kNo; }
314
315 return fDrawingManager->flush(nullptr, numSemaphores, signalSemaphores);
316}
317
Robert Phillips7ee385e2017-03-30 08:02:11 -0400318void GrContextPriv::flush(GrSurfaceProxy* proxy) {
319 ASSERT_SINGLE_OWNER_PRIV
320 RETURN_IF_ABANDONED_PRIV
321 ASSERT_OWNED_PROXY_PRIV(proxy);
322
323 fContext->fDrawingManager->flush(proxy);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000324}
325
Greg Daniela870b462019-01-08 15:49:46 -0500326////////////////////////////////////////////////////////////////////////////////
327
328void GrContext::storeVkPipelineCacheData() {
329 if (fGpu) {
330 fGpu->storeVkPipelineCacheData();
331 }
332}
333
334////////////////////////////////////////////////////////////////////////////////
335
Brian Salomonc320b152018-02-20 14:05:36 -0500336// TODO: This will be removed when GrSurfaceContexts are aware of their color types.
337// (skbug.com/6718)
Brian Osmand2ca59a2017-04-13 14:03:57 -0400338static bool valid_premul_config(GrPixelConfig config) {
Brian Salomonc320b152018-02-20 14:05:36 -0500339 switch (config) {
340 case kUnknown_GrPixelConfig: return false;
341 case kAlpha_8_GrPixelConfig: return false;
342 case kGray_8_GrPixelConfig: return false;
343 case kRGB_565_GrPixelConfig: return false;
344 case kRGBA_4444_GrPixelConfig: return true;
345 case kRGBA_8888_GrPixelConfig: return true;
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400346 case kRGB_888_GrPixelConfig: return false;
Jim Van Verth69e57852018-12-05 13:38:59 -0500347 case kRG_88_GrPixelConfig: return false;
Brian Salomonc320b152018-02-20 14:05:36 -0500348 case kBGRA_8888_GrPixelConfig: return true;
349 case kSRGBA_8888_GrPixelConfig: return true;
350 case kSBGRA_8888_GrPixelConfig: return true;
Brian Osman10fc6fd2018-03-02 11:01:10 -0500351 case kRGBA_1010102_GrPixelConfig: return true;
Brian Salomonc320b152018-02-20 14:05:36 -0500352 case kRGBA_float_GrPixelConfig: return true;
353 case kRG_float_GrPixelConfig: return false;
354 case kAlpha_half_GrPixelConfig: return false;
355 case kRGBA_half_GrPixelConfig: return true;
Jim Van Verth1676cb92019-01-15 13:24:45 -0500356 case kRGB_ETC1_GrPixelConfig: return false;
Brian Salomonc320b152018-02-20 14:05:36 -0500357 case kAlpha_8_as_Alpha_GrPixelConfig: return false;
358 case kAlpha_8_as_Red_GrPixelConfig: return false;
359 case kAlpha_half_as_Red_GrPixelConfig: return false;
360 case kGray_8_as_Lum_GrPixelConfig: return false;
361 case kGray_8_as_Red_GrPixelConfig: return false;
362 }
363 SK_ABORT("Invalid GrPixelConfig");
364 return false;
Brian Osmance425512017-03-22 14:37:50 -0400365}
366
Brian Salomonc320b152018-02-20 14:05:36 -0500367static bool valid_premul_color_type(GrColorType ct) {
368 switch (ct) {
Brian Osman10fc6fd2018-03-02 11:01:10 -0500369 case GrColorType::kUnknown: return false;
370 case GrColorType::kAlpha_8: return false;
371 case GrColorType::kRGB_565: return false;
372 case GrColorType::kABGR_4444: return true;
373 case GrColorType::kRGBA_8888: return true;
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400374 case GrColorType::kRGB_888x: return false;
Jim Van Verth69e57852018-12-05 13:38:59 -0500375 case GrColorType::kRG_88: return false;
Brian Osman10fc6fd2018-03-02 11:01:10 -0500376 case GrColorType::kBGRA_8888: return true;
377 case GrColorType::kRGBA_1010102: return true;
378 case GrColorType::kGray_8: return false;
379 case GrColorType::kAlpha_F16: return false;
380 case GrColorType::kRGBA_F16: return true;
381 case GrColorType::kRG_F32: return false;
382 case GrColorType::kRGBA_F32: return true;
Jim Van Verth1676cb92019-01-15 13:24:45 -0500383 case GrColorType::kRGB_ETC1: return false;
Brian Salomonc320b152018-02-20 14:05:36 -0500384 }
385 SK_ABORT("Invalid GrColorType");
386 return false;
387}
388
389static bool valid_pixel_conversion(GrColorType cpuColorType, GrPixelConfig gpuConfig,
Brian Osmand2ca59a2017-04-13 14:03:57 -0400390 bool premulConversion) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400391 // We only allow premul <-> unpremul conversions for some formats
Brian Salomonc320b152018-02-20 14:05:36 -0500392 if (premulConversion &&
393 (!valid_premul_color_type(cpuColorType) || !valid_premul_config(gpuConfig))) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400394 return false;
395 }
Brian Osmand2ca59a2017-04-13 14:03:57 -0400396 return true;
397}
398
Brian Salomonc320b152018-02-20 14:05:36 -0500399bool GrContextPriv::writeSurfacePixels(GrSurfaceContext* dst, int left, int top, int width,
400 int height, GrColorType srcColorType,
401 SkColorSpace* srcColorSpace, const void* buffer,
402 size_t rowBytes, uint32_t pixelOpsFlags) {
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500403 ASSERT_SINGLE_OWNER_PRIV
404 RETURN_FALSE_IF_ABANDONED_PRIV
405 SkASSERT(dst);
406 SkASSERT(buffer);
407 ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy());
Brian Salomond494f6e2018-06-01 14:13:43 -0400408 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "writeSurfacePixels", fContext);
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500409
410 if (GrColorType::kUnknown == srcColorType) {
411 return false;
412 }
413
414 if (!dst->asSurfaceProxy()->instantiate(this->resourceProvider())) {
415 return false;
416 }
417
418 GrSurfaceProxy* dstProxy = dst->asSurfaceProxy();
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400419 GrSurface* dstSurface = dstProxy->peekSurface();
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500420
421 if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(),
422 GrColorTypeBytesPerPixel(srcColorType), &left, &top,
423 &width, &height, &buffer, &rowBytes)) {
424 return false;
425 }
426
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400427 // TODO: Make GrSurfaceContext know its alpha type and pass src buffer's alpha type.
428 bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
429
430 // For canvas2D putImageData performance we have a special code path for unpremul RGBA_8888 srcs
431 // that are premultiplied on the GPU. This is kept as narrow as possible for now.
Brian Salomon817847c2018-05-07 13:33:50 -0400432 bool canvas2DFastPath =
Robert Phillips9da87e02019-02-04 13:26:26 -0500433 !fContext->priv().caps()->avoidWritePixelsFastPath() &&
Brian Salomon817847c2018-05-07 13:33:50 -0400434 premul &&
Brian Osman34ec3742018-07-03 10:40:57 -0400435 !dst->colorSpaceInfo().colorSpace() &&
Brian Salomon817847c2018-05-07 13:33:50 -0400436 (srcColorType == GrColorType::kRGBA_8888 || srcColorType == GrColorType::kBGRA_8888) &&
437 SkToBool(dst->asRenderTargetContext()) &&
438 (dstProxy->config() == kRGBA_8888_GrPixelConfig ||
439 dstProxy->config() == kBGRA_8888_GrPixelConfig) &&
440 !(pixelOpsFlags & kDontFlush_PixelOpsFlag) &&
Robert Phillips9da87e02019-02-04 13:26:26 -0500441 fContext->priv().caps()->isConfigTexturable(kRGBA_8888_GrPixelConfig) &&
Brian Salomon817847c2018-05-07 13:33:50 -0400442 fContext->validPMUPMConversionExists();
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400443
Greg Daniel4065d452018-11-16 15:43:41 -0500444 const GrCaps* caps = this->caps();
445 if (!caps->surfaceSupportsWritePixels(dstSurface) ||
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400446 canvas2DFastPath) {
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400447 // We don't expect callers that are skipping flushes to require an intermediate draw.
448 SkASSERT(!(pixelOpsFlags & kDontFlush_PixelOpsFlag));
449 if (pixelOpsFlags & kDontFlush_PixelOpsFlag) {
450 return false;
451 }
452
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500453 GrSurfaceDesc desc;
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500454 desc.fWidth = width;
455 desc.fHeight = height;
456 desc.fSampleCnt = 1;
Greg Daniel4065d452018-11-16 15:43:41 -0500457
458 GrBackendFormat format;
459 if (canvas2DFastPath) {
460 desc.fConfig = kRGBA_8888_GrPixelConfig;
461 format =
Robert Phillips9da87e02019-02-04 13:26:26 -0500462 fContext->priv().caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
Greg Daniel4065d452018-11-16 15:43:41 -0500463 } else {
464 desc.fConfig = dstProxy->config();
465 format = dstProxy->backendFormat().makeTexture2D();
466 if (!format.isValid()) {
467 return false;
468 }
469 }
470
Brian Salomon2a4f9832018-03-03 22:43:43 -0500471 auto tempProxy = this->proxyProvider()->createProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500472 format, desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kApprox, SkBudgeted::kYes);
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500473 if (!tempProxy) {
474 return false;
475 }
476 auto tempCtx = this->drawingManager()->makeTextureContext(
477 tempProxy, dst->colorSpaceInfo().refColorSpace());
478 if (!tempCtx) {
479 return false;
480 }
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400481 uint32_t flags = canvas2DFastPath ? 0 : pixelOpsFlags;
Brian Salomon817847c2018-05-07 13:33:50 -0400482 // In the fast path we always write the srcData to the temp context as though it were RGBA.
483 // When the data is really BGRA the write will cause the R and B channels to be swapped in
484 // the intermediate surface which gets corrected by a swizzle effect when drawing to the
485 // dst.
486 auto tmpColorType = canvas2DFastPath ? GrColorType::kRGBA_8888 : srcColorType;
Brian Salomond494f6e2018-06-01 14:13:43 -0400487 if (!this->writeSurfacePixels(tempCtx.get(), 0, 0, width, height, tmpColorType,
488 srcColorSpace, buffer, rowBytes, flags)) {
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500489 return false;
490 }
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400491 if (canvas2DFastPath) {
492 GrPaint paint;
493 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomon817847c2018-05-07 13:33:50 -0400494 auto fp = fContext->createUPMToPMEffect(
Brian Osman5ea96bf2018-10-02 14:58:05 -0400495 GrSimpleTextureEffect::Make(std::move(tempProxy), SkMatrix::I()));
Brian Salomon817847c2018-05-07 13:33:50 -0400496 if (srcColorType == GrColorType::kBGRA_8888) {
497 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
498 }
499 if (!fp) {
500 return false;
501 }
502 paint.addColorFragmentProcessor(std::move(fp));
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400503 dst->asRenderTargetContext()->fillRectToRect(
504 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
505 SkRect::MakeXYWH(left, top, width, height), SkRect::MakeWH(width, height));
506 return true;
507 } else {
508 return dst->copy(tempProxy.get(), SkIRect::MakeWH(width, height), {left, top});
509 }
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500510 }
511
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500512 bool convert = premul;
513
514 if (!valid_pixel_conversion(srcColorType, dstProxy->config(), premul)) {
515 return false;
516 }
517
Robert Phillips9da87e02019-02-04 13:26:26 -0500518 GrColorType allowedColorType = fContext->priv().caps()->supportedWritePixelsColorType(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400519 dstProxy->config(), srcColorType);
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500520 convert = convert || (srcColorType != allowedColorType);
521
522 if (!dst->colorSpaceInfo().colorSpace()) {
523 // "Legacy" mode - no color space conversions.
524 srcColorSpace = nullptr;
525 }
526 convert = convert || !SkColorSpace::Equals(srcColorSpace, dst->colorSpaceInfo().colorSpace());
527
528 std::unique_ptr<char[]> tempBuffer;
529 if (convert) {
530 auto srcSkColorType = GrColorTypeToSkColorType(srcColorType);
531 auto dstSkColorType = GrColorTypeToSkColorType(allowedColorType);
532 if (kUnknown_SkColorType == srcSkColorType || kUnknown_SkColorType == dstSkColorType) {
533 return false;
534 }
Brian Osman2091fbb2018-10-11 11:05:37 -0400535 auto srcAlphaType = SkColorTypeIsAlwaysOpaque(srcSkColorType)
536 ? kOpaque_SkAlphaType
537 : (premul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType);
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500538 SkPixmap src(SkImageInfo::Make(width, height, srcSkColorType, srcAlphaType,
539 sk_ref_sp(srcColorSpace)),
540 buffer, rowBytes);
541 auto tempSrcII = SkImageInfo::Make(width, height, dstSkColorType, kPremul_SkAlphaType,
542 dst->colorSpaceInfo().refColorSpace());
543 auto size = tempSrcII.computeMinByteSize();
544 if (!size) {
545 return false;
546 }
547 tempBuffer.reset(new char[size]);
548 SkPixmap tempSrc(tempSrcII, tempBuffer.get(), tempSrcII.minRowBytes());
549 if (!src.readPixels(tempSrc)) {
550 return false;
551 }
552 srcColorType = allowedColorType;
553 buffer = tempSrc.addr();
554 rowBytes = tempSrc.rowBytes();
555 if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
556 std::unique_ptr<char[]> row(new char[rowBytes]);
557 for (int y = 0; y < height / 2; ++y) {
558 memcpy(row.get(), tempSrc.addr(0, y), rowBytes);
559 memcpy(tempSrc.writable_addr(0, y), tempSrc.addr(0, height - 1 - y), rowBytes);
560 memcpy(tempSrc.writable_addr(0, height - 1 - y), row.get(), rowBytes);
561 }
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400562 top = dstSurface->height() - top - height;
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500563 }
564 } else if (dstProxy->origin() == kBottomLeft_GrSurfaceOrigin) {
565 size_t trimRowBytes = GrColorTypeBytesPerPixel(srcColorType) * width;
566 tempBuffer.reset(new char[trimRowBytes * height]);
567 char* dst = reinterpret_cast<char*>(tempBuffer.get()) + trimRowBytes * (height - 1);
568 const char* src = reinterpret_cast<const char*>(buffer);
569 for (int i = 0; i < height; ++i, src += rowBytes, dst -= trimRowBytes) {
570 memcpy(dst, src, trimRowBytes);
571 }
572 buffer = tempBuffer.get();
573 rowBytes = trimRowBytes;
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400574 top = dstSurface->height() - top - height;
Brian Salomon5f33a8c2018-02-26 14:32:39 -0500575 }
576
577 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && dstSurface->surfacePriv().hasPendingIO()) {
578 this->flush(nullptr); // MDB TODO: tighten this
579 }
580
581 return this->getGpu()->writePixels(dstSurface, left, top, width, height, srcColorType, buffer,
582 rowBytes);
583}
584
Brian Salomond494f6e2018-06-01 14:13:43 -0400585bool GrContextPriv::readSurfacePixels(GrSurfaceContext* src, int left, int top, int width,
586 int height, GrColorType dstColorType,
587 SkColorSpace* dstColorSpace, void* buffer, size_t rowBytes,
588 uint32_t pixelOpsFlags) {
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400589 ASSERT_SINGLE_OWNER_PRIV
590 RETURN_FALSE_IF_ABANDONED_PRIV
591 SkASSERT(src);
592 SkASSERT(buffer);
593 ASSERT_OWNED_PROXY_PRIV(src->asSurfaceProxy());
Brian Salomond494f6e2018-06-01 14:13:43 -0400594 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "readSurfacePixels", fContext);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400595
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400596 SkASSERT(!(pixelOpsFlags & kDontFlush_PixelOpsFlag));
597 if (pixelOpsFlags & kDontFlush_PixelOpsFlag) {
Brian Salomon5fba7ad2018-03-22 10:01:16 -0400598 return false;
599 }
600
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400601 // MDB TODO: delay this instantiation until later in the method
602 if (!src->asSurfaceProxy()->instantiate(this->resourceProvider())) {
603 return false;
604 }
605
606 GrSurfaceProxy* srcProxy = src->asSurfaceProxy();
Brian Salomonfd98c2c2018-07-31 17:25:29 -0400607 GrSurface* srcSurface = srcProxy->peekSurface();
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400608
609 if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(),
610 GrColorTypeBytesPerPixel(dstColorType), &left, &top,
611 &width, &height, &buffer, &rowBytes)) {
612 return false;
613 }
614
615 // TODO: Make GrSurfaceContext know its alpha type and pass dst buffer's alpha type.
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400616 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400617
618 if (!valid_pixel_conversion(dstColorType, srcProxy->config(), unpremul)) {
619 return false;
620 }
621
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400622 // This is the getImageData equivalent to the canvas2D putImageData fast path. We probably don't
623 // care so much about getImageData performance. However, in order to ensure putImageData/
624 // getImageData in "legacy" mode are round-trippable we use the GPU to do the complementary
Brian Salomond494f6e2018-06-01 14:13:43 -0400625 // unpremul step to writeSurfacePixels's premul step (which is determined empirically in
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400626 // fContext->vaildaPMUPMConversionExists()).
Brian Salomon817847c2018-05-07 13:33:50 -0400627 bool canvas2DFastPath =
628 unpremul &&
Brian Osman34ec3742018-07-03 10:40:57 -0400629 !src->colorSpaceInfo().colorSpace() &&
Brian Salomon817847c2018-05-07 13:33:50 -0400630 (GrColorType::kRGBA_8888 == dstColorType || GrColorType::kBGRA_8888 == dstColorType) &&
631 SkToBool(srcProxy->asTextureProxy()) &&
632 (srcProxy->config() == kRGBA_8888_GrPixelConfig ||
633 srcProxy->config() == kBGRA_8888_GrPixelConfig) &&
Robert Phillips9da87e02019-02-04 13:26:26 -0500634 fContext->priv().caps()->isConfigRenderable(kRGBA_8888_GrPixelConfig) &&
Brian Salomon817847c2018-05-07 13:33:50 -0400635 fContext->validPMUPMConversionExists();
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400636
Robert Phillips9da87e02019-02-04 13:26:26 -0500637 if (!fContext->priv().caps()->surfaceSupportsReadPixels(srcSurface) ||
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400638 canvas2DFastPath) {
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400639 GrSurfaceDesc desc;
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400640 desc.fFlags = canvas2DFastPath ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags;
641 desc.fConfig = canvas2DFastPath ? kRGBA_8888_GrPixelConfig : srcProxy->config();
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400642 desc.fWidth = width;
643 desc.fHeight = height;
644 desc.fSampleCnt = 1;
Greg Daniel4065d452018-11-16 15:43:41 -0500645
646 GrBackendFormat format;
647 if (canvas2DFastPath) {
648 desc.fFlags = kRenderTarget_GrSurfaceFlag;
649 desc.fConfig = kRGBA_8888_GrPixelConfig;
650 format = this->caps()->getBackendFormatFromColorType(kRGBA_8888_SkColorType);
651 } else {
652 desc.fFlags = kNone_GrSurfaceFlags;
653 desc.fConfig = srcProxy->config();
654 format = srcProxy->backendFormat().makeTexture2D();
655 if (!format.isValid()) {
656 return false;
657 }
658 }
659
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400660 auto tempProxy = this->proxyProvider()->createProxy(
Greg Daniel4065d452018-11-16 15:43:41 -0500661 format, desc, kTopLeft_GrSurfaceOrigin, SkBackingFit::kApprox, SkBudgeted::kYes);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400662 if (!tempProxy) {
663 return false;
664 }
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400665 sk_sp<GrSurfaceContext> tempCtx;
666 if (canvas2DFastPath) {
667 tempCtx = this->drawingManager()->makeRenderTargetContext(std::move(tempProxy), nullptr,
668 nullptr);
Greg Daniel42314012018-04-23 10:57:37 -0400669 SkASSERT(tempCtx->asRenderTargetContext());
670 tempCtx->asRenderTargetContext()->discard();
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400671 } else {
672 tempCtx = this->drawingManager()->makeTextureContext(
673 std::move(tempProxy), src->colorSpaceInfo().refColorSpace());
674 }
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400675 if (!tempCtx) {
676 return false;
677 }
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400678 if (canvas2DFastPath) {
679 GrPaint paint;
680 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Salomon817847c2018-05-07 13:33:50 -0400681 auto fp = fContext->createPMToUPMEffect(
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400682 GrSimpleTextureEffect::Make(sk_ref_sp(srcProxy->asTextureProxy()),
Brian Osman5ea96bf2018-10-02 14:58:05 -0400683 SkMatrix::I()));
Brian Salomon817847c2018-05-07 13:33:50 -0400684 if (dstColorType == GrColorType::kBGRA_8888) {
685 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), GrSwizzle::BGRA());
686 dstColorType = GrColorType::kRGBA_8888;
687 }
688 if (!fp) {
689 return false;
690 }
691 paint.addColorFragmentProcessor(std::move(fp));
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400692 tempCtx->asRenderTargetContext()->fillRectToRect(
693 GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(),
694 SkRect::MakeWH(width, height), SkRect::MakeXYWH(left, top, width, height));
695 } else if (!tempCtx->copy(srcProxy, SkIRect::MakeXYWH(left, top, width, height), {0, 0})) {
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400696 return false;
697 }
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400698 uint32_t flags = canvas2DFastPath ? 0 : pixelOpsFlags;
Brian Salomond494f6e2018-06-01 14:13:43 -0400699 return this->readSurfacePixels(tempCtx.get(), 0, 0, width, height, dstColorType,
700 dstColorSpace, buffer, rowBytes, flags);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400701 }
702
Brian Salomonf3c4e0c2018-04-06 20:32:06 -0400703 bool convert = unpremul;
704
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400705 bool flip = srcProxy->origin() == kBottomLeft_GrSurfaceOrigin;
706 if (flip) {
707 top = srcSurface->height() - top - height;
708 }
709
Robert Phillips9da87e02019-02-04 13:26:26 -0500710 GrColorType allowedColorType = fContext->priv().caps()->supportedReadPixelsColorType(
Brian Salomonc7fe0f72018-05-11 10:14:21 -0400711 srcProxy->config(), dstColorType);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400712 convert = convert || (dstColorType != allowedColorType);
713
714 if (!src->colorSpaceInfo().colorSpace()) {
715 // "Legacy" mode - no color space conversions.
716 dstColorSpace = nullptr;
717 }
718 convert = convert || !SkColorSpace::Equals(dstColorSpace, src->colorSpaceInfo().colorSpace());
719
720 SkAutoPixmapStorage tempPixmap;
721 SkPixmap finalPixmap;
722 if (convert) {
723 SkColorType srcSkColorType = GrColorTypeToSkColorType(allowedColorType);
724 SkColorType dstSkColorType = GrColorTypeToSkColorType(dstColorType);
Brian Osman2091fbb2018-10-11 11:05:37 -0400725 bool srcAlwaysOpaque = SkColorTypeIsAlwaysOpaque(srcSkColorType);
726 bool dstAlwaysOpaque = SkColorTypeIsAlwaysOpaque(dstSkColorType);
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400727 if (kUnknown_SkColorType == srcSkColorType || kUnknown_SkColorType == dstSkColorType) {
728 return false;
729 }
Brian Osman2091fbb2018-10-11 11:05:37 -0400730 auto tempAT = srcAlwaysOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400731 auto tempII = SkImageInfo::Make(width, height, srcSkColorType, tempAT,
732 src->colorSpaceInfo().refColorSpace());
Brian Osman2091fbb2018-10-11 11:05:37 -0400733 SkASSERT(!unpremul || !dstAlwaysOpaque);
734 auto finalAT = (srcAlwaysOpaque || dstAlwaysOpaque)
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400735 ? kOpaque_SkAlphaType
736 : unpremul ? kUnpremul_SkAlphaType : kPremul_SkAlphaType;
737 auto finalII =
738 SkImageInfo::Make(width, height, dstSkColorType, finalAT, sk_ref_sp(dstColorSpace));
739 if (!SkImageInfoValidConversion(finalII, tempII)) {
740 return false;
741 }
Brian Salomon6196fc12018-07-12 16:39:23 -0400742 if (!tempPixmap.tryAlloc(tempII)) {
743 return false;
744 }
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400745 finalPixmap.reset(finalII, buffer, rowBytes);
746 buffer = tempPixmap.writable_addr();
747 rowBytes = tempPixmap.rowBytes();
Brian Salomon3d945e82018-05-14 14:54:06 -0400748 // Chrome msan bots require this.
749 sk_bzero(buffer, tempPixmap.computeByteSize());
Brian Salomon19eaf2d2018-03-19 16:06:44 -0400750 }
751
752 if (srcSurface->surfacePriv().hasPendingWrite()) {
753 this->flush(nullptr); // MDB TODO: tighten this
754 }
755
756 if (!fContext->fGpu->readPixels(srcSurface, left, top, width, height, allowedColorType, buffer,
757 rowBytes)) {
758 return false;
759 }
760
761 if (flip) {
762 size_t trimRowBytes = GrColorTypeBytesPerPixel(allowedColorType) * width;
763 std::unique_ptr<char[]> row(new char[trimRowBytes]);
764 char* upper = reinterpret_cast<char*>(buffer);
765 char* lower = reinterpret_cast<char*>(buffer) + (height - 1) * rowBytes;
766 for (int y = 0; y < height / 2; ++y, upper += rowBytes, lower -= rowBytes) {
767 memcpy(row.get(), upper, trimRowBytes);
768 memcpy(upper, lower, trimRowBytes);
769 memcpy(lower, row.get(), trimRowBytes);
770 }
771 }
772 if (convert) {
773 if (!tempPixmap.readPixels(finalPixmap)) {
774 return false;
775 }
776 }
777 return true;
778}
779
Robert Phillips7ee385e2017-03-30 08:02:11 -0400780void GrContextPriv::prepareSurfaceForExternalIO(GrSurfaceProxy* proxy) {
781 ASSERT_SINGLE_OWNER_PRIV
782 RETURN_IF_ABANDONED_PRIV
783 SkASSERT(proxy);
784 ASSERT_OWNED_PROXY_PRIV(proxy);
Greg Daniel51316782017-08-02 15:10:09 +0000785 fContext->fDrawingManager->prepareSurfaceForExternalIO(proxy, 0, nullptr);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000786}
787
Robert Phillips7ee385e2017-03-30 08:02:11 -0400788void GrContextPriv::flushSurfaceWrites(GrSurfaceProxy* proxy) {
789 ASSERT_SINGLE_OWNER_PRIV
790 RETURN_IF_ABANDONED_PRIV
791 SkASSERT(proxy);
792 ASSERT_OWNED_PROXY_PRIV(proxy);
793 if (proxy->priv().hasPendingWrite()) {
794 this->flush(proxy);
bsalomonf80bfed2014-10-07 05:56:02 -0700795 }
796}
797
Robert Phillips7ee385e2017-03-30 08:02:11 -0400798void GrContextPriv::flushSurfaceIO(GrSurfaceProxy* proxy) {
799 ASSERT_SINGLE_OWNER_PRIV
800 RETURN_IF_ABANDONED_PRIV
801 SkASSERT(proxy);
802 ASSERT_OWNED_PROXY_PRIV(proxy);
803 if (proxy->priv().hasPendingIO()) {
804 this->flush(proxy);
ajuma95243eb2016-08-24 08:19:02 -0700805 }
806}
807
bsalomon@google.com27847de2011-02-22 20:59:41 +0000808////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000809
Robert Phillipsc994a932018-06-19 13:09:54 -0400810sk_sp<GrOpMemoryPool> GrContextPriv::refOpMemoryPool() {
811 if (!fContext->fOpMemoryPool) {
812 // DDL TODO: should the size of the memory pool be decreased in DDL mode? CPU-side memory
813 // consumed in DDL mode vs. normal mode for a single skp might be a good metric of wasted
814 // memory.
815 fContext->fOpMemoryPool = sk_sp<GrOpMemoryPool>(new GrOpMemoryPool(16384, 16384));
816 }
817
818 SkASSERT(fContext->fOpMemoryPool);
819 return fContext->fOpMemoryPool;
820}
821
822GrOpMemoryPool* GrContextPriv::opMemoryPool() {
823 return this->refOpMemoryPool().get();
824}
825
Robert Phillips2c862492017-01-18 10:08:39 -0500826sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy> proxy,
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500827 sk_sp<SkColorSpace> colorSpace,
828 const SkSurfaceProps* props) {
Brian Osman45580d32016-11-23 09:37:01 -0500829 ASSERT_SINGLE_OWNER_PRIV
830
Brian Osman45580d32016-11-23 09:37:01 -0500831 if (proxy->asRenderTargetProxy()) {
Robert Phillips2c862492017-01-18 10:08:39 -0500832 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500833 std::move(colorSpace), props);
Brian Osman45580d32016-11-23 09:37:01 -0500834 } else {
835 SkASSERT(proxy->asTextureProxy());
Robert Phillipsd5f9cdd2018-01-31 09:29:48 -0500836 SkASSERT(!props);
Robert Phillips2c862492017-01-18 10:08:39 -0500837 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Brian Osman45580d32016-11-23 09:37:01 -0500838 }
839}
840
Greg Daniel4065d452018-11-16 15:43:41 -0500841sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(const GrBackendFormat& format,
842 const GrSurfaceDesc& dstDesc,
Brian Salomon2a4f9832018-03-03 22:43:43 -0500843 GrSurfaceOrigin origin,
Greg Daniel65c7f662017-10-30 13:39:09 -0400844 GrMipMapped mipMapped,
Robert Phillipse2f7d182016-12-15 09:23:05 -0500845 SkBackingFit fit,
Brian Salomon366093f2018-02-13 09:25:22 -0500846 SkBudgeted isDstBudgeted,
847 sk_sp<SkColorSpace> colorSpace,
848 const SkSurfaceProps* props) {
Greg Daniel65c7f662017-10-30 13:39:09 -0400849 sk_sp<GrTextureProxy> proxy;
850 if (GrMipMapped::kNo == mipMapped) {
Greg Daniel4065d452018-11-16 15:43:41 -0500851 proxy = this->proxyProvider()->createProxy(format, dstDesc, origin, fit, isDstBudgeted);
Greg Daniel65c7f662017-10-30 13:39:09 -0400852 } else {
853 SkASSERT(SkBackingFit::kExact == fit);
Greg Daniel4065d452018-11-16 15:43:41 -0500854 proxy = this->proxyProvider()->createMipMapProxy(format, dstDesc, origin, isDstBudgeted);
Greg Daniel65c7f662017-10-30 13:39:09 -0400855 }
Robert Phillips77b3f322017-01-31 18:24:12 -0500856 if (!proxy) {
857 return nullptr;
858 }
Robert Phillipse2f7d182016-12-15 09:23:05 -0500859
Greg Daniel55fa6472018-03-16 16:13:10 -0400860 sk_sp<GrSurfaceContext> sContext = this->makeWrappedSurfaceContext(std::move(proxy),
861 std::move(colorSpace),
862 props);
863 if (sContext && sContext->asRenderTargetContext()) {
864 sContext->asRenderTargetContext()->discard();
865 }
866
867 return sContext;
Robert Phillipse2f7d182016-12-15 09:23:05 -0500868}
869
Brian Salomond17f6582017-07-19 18:28:58 -0400870sk_sp<GrTextureContext> GrContextPriv::makeBackendTextureContext(const GrBackendTexture& tex,
Greg Daniel7ef28f32017-04-20 16:41:55 +0000871 GrSurfaceOrigin origin,
Brian Osmanc1e37052017-03-09 14:19:20 -0500872 sk_sp<SkColorSpace> colorSpace) {
Robert Phillips26caf892017-01-27 10:58:31 -0500873 ASSERT_SINGLE_OWNER_PRIV
874
Brian Salomonc67c31c2018-12-06 10:00:03 -0500875 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->wrapBackendTexture(
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500876 tex, origin, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo, kRW_GrIOType);
Robert Phillips77b3f322017-01-31 18:24:12 -0500877 if (!proxy) {
878 return nullptr;
879 }
Robert Phillips26caf892017-01-27 10:58:31 -0500880
Brian Salomond17f6582017-07-19 18:28:58 -0400881 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Robert Phillips26caf892017-01-27 10:58:31 -0500882}
883
Brian Osman11052242016-10-27 14:47:55 -0400884sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000885 const GrBackendTexture& tex,
886 GrSurfaceOrigin origin,
887 int sampleCnt,
Brian Osman11052242016-10-27 14:47:55 -0400888 sk_sp<SkColorSpace> colorSpace,
Brian Osmanc1e37052017-03-09 14:19:20 -0500889 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700890 ASSERT_SINGLE_OWNER_PRIV
Brian Salomonbdecacf2018-02-02 20:32:49 -0500891 SkASSERT(sampleCnt > 0);
robertphillips4fd74ae2016-08-03 14:26:53 -0700892
Brian Salomonaa6ca0a2019-01-24 16:03:07 -0500893 sk_sp<GrTextureProxy> proxy(this->proxyProvider()->wrapRenderableBackendTexture(
894 tex, origin, sampleCnt, kBorrow_GrWrapOwnership, GrWrapCacheable::kNo));
Robert Phillips77b3f322017-01-31 18:24:12 -0500895 if (!proxy) {
896 return nullptr;
897 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400898
Robert Phillips37430132016-11-09 06:50:43 -0500899 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Brian Osman11052242016-10-27 14:47:55 -0400900 std::move(colorSpace), props);
robertphillips4fd74ae2016-08-03 14:26:53 -0700901}
902
Brian Osman11052242016-10-27 14:47:55 -0400903sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext(
Greg Danielbcf612b2017-05-01 13:50:58 +0000904 const GrBackendRenderTarget& backendRT,
905 GrSurfaceOrigin origin,
robertphillips4fd74ae2016-08-03 14:26:53 -0700906 sk_sp<SkColorSpace> colorSpace,
907 const SkSurfaceProps* surfaceProps) {
908 ASSERT_SINGLE_OWNER_PRIV
909
Brian Salomon7578f3e2018-03-07 14:39:54 -0500910 sk_sp<GrSurfaceProxy> proxy = this->proxyProvider()->wrapBackendRenderTarget(backendRT, origin);
Robert Phillips77b3f322017-01-31 18:24:12 -0500911 if (!proxy) {
912 return nullptr;
913 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400914
Robert Phillips37430132016-11-09 06:50:43 -0500915 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400916 std::move(colorSpace),
Brian Osman11052242016-10-27 14:47:55 -0400917 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -0700918}
919
Brian Osman11052242016-10-27 14:47:55 -0400920sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000921 const GrBackendTexture& tex,
922 GrSurfaceOrigin origin,
923 int sampleCnt,
robertphillips4fd74ae2016-08-03 14:26:53 -0700924 sk_sp<SkColorSpace> colorSpace,
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500925 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700926 ASSERT_SINGLE_OWNER_PRIV
Brian Salomonbdecacf2018-02-02 20:32:49 -0500927 SkASSERT(sampleCnt > 0);
Brian Salomon7578f3e2018-03-07 14:39:54 -0500928 sk_sp<GrSurfaceProxy> proxy(
929 this->proxyProvider()->wrapBackendTextureAsRenderTarget(tex, origin, sampleCnt));
Robert Phillips77b3f322017-01-31 18:24:12 -0500930 if (!proxy) {
931 return nullptr;
932 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400933
Robert Phillips37430132016-11-09 06:50:43 -0500934 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400935 std::move(colorSpace),
Robert Phillips0bd24dc2018-01-16 08:06:32 -0500936 props);
robertphillips77a2e522015-10-17 07:43:27 -0700937}
938
Greg Danielb46add82019-01-02 14:51:29 -0500939sk_sp<GrRenderTargetContext> GrContextPriv::makeVulkanSecondaryCBRenderTargetContext(
940 const SkImageInfo& imageInfo, const GrVkDrawableInfo& vkInfo, const SkSurfaceProps* props) {
941 ASSERT_SINGLE_OWNER_PRIV
942 sk_sp<GrSurfaceProxy> proxy(
943 this->proxyProvider()->wrapVulkanSecondaryCBAsRenderTarget(imageInfo, vkInfo));
944 if (!proxy) {
945 return nullptr;
946 }
947
948 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
949 imageInfo.refColorSpace(),
950 props);
951}
952
Chris Daltonfe199b72017-05-05 11:26:15 -0400953void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
954 fContext->fDrawingManager->addOnFlushCallbackObject(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400955}
956
Robert Phillips62000362018-02-01 09:10:04 -0500957void GrContextPriv::moveOpListsToDDL(SkDeferredDisplayList* ddl) {
958 fContext->fDrawingManager->moveOpListsToDDL(ddl);
959}
960
961void GrContextPriv::copyOpListsFromDDL(const SkDeferredDisplayList* ddl,
962 GrRenderTargetProxy* newDest) {
963 fContext->fDrawingManager->copyOpListsFromDDL(ddl, newDest);
964}
965
robertphillips48fde9c2016-09-06 05:20:20 -0700966static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) {
Brian Osman78f20e02017-01-12 10:28:01 -0500967 switch (config) {
968 case kAlpha_8_GrPixelConfig:
Greg Daniel93b4ddd2018-04-16 16:16:47 -0400969 case kAlpha_8_as_Alpha_GrPixelConfig:
970 case kAlpha_8_as_Red_GrPixelConfig:
Brian Osman78f20e02017-01-12 10:28:01 -0500971 case kRGB_565_GrPixelConfig:
972 case kRGBA_4444_GrPixelConfig:
973 case kBGRA_8888_GrPixelConfig:
Brian Osman10fc6fd2018-03-02 11:01:10 -0500974 case kRGBA_1010102_GrPixelConfig:
Brian Osman9c111352018-10-16 10:18:26 -0400975 case kRGBA_half_GrPixelConfig:
Brian Osman78f20e02017-01-12 10:28:01 -0500976 return kRGBA_8888_GrPixelConfig;
977 case kSBGRA_8888_GrPixelConfig:
978 return kSRGBA_8888_GrPixelConfig;
979 case kAlpha_half_GrPixelConfig:
Greg Daniel93b4ddd2018-04-16 16:16:47 -0400980 case kAlpha_half_as_Red_GrPixelConfig:
Brian Osman78f20e02017-01-12 10:28:01 -0500981 return kRGBA_half_GrPixelConfig;
Greg Daniel93b4ddd2018-04-16 16:16:47 -0400982 case kGray_8_GrPixelConfig:
983 case kGray_8_as_Lum_GrPixelConfig:
984 case kGray_8_as_Red_GrPixelConfig:
985 return kRGB_888_GrPixelConfig;
Brian Osman78f20e02017-01-12 10:28:01 -0500986 default:
987 return kUnknown_GrPixelConfig;
988 }
robertphillips48fde9c2016-09-06 05:20:20 -0700989}
990
Robert Phillips0c4b7b12018-03-06 08:20:37 -0500991sk_sp<GrRenderTargetContext> GrContextPriv::makeDeferredRenderTargetContextWithFallback(
Greg Daniel4065d452018-11-16 15:43:41 -0500992 const GrBackendFormat& format,
robertphillipsd728f0c2016-11-21 11:05:03 -0800993 SkBackingFit fit,
994 int width, int height,
995 GrPixelConfig config,
996 sk_sp<SkColorSpace> colorSpace,
997 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -0400998 GrMipMapped mipMapped,
robertphillipsd728f0c2016-11-21 11:05:03 -0800999 GrSurfaceOrigin origin,
1000 const SkSurfaceProps* surfaceProps,
1001 SkBudgeted budgeted) {
Greg Daniel4065d452018-11-16 15:43:41 -05001002 GrBackendFormat localFormat = format;
Brian Salomonbdecacf2018-02-02 20:32:49 -05001003 SkASSERT(sampleCnt > 0);
Robert Phillips9da87e02019-02-04 13:26:26 -05001004 if (0 == fContext->priv().caps()->getRenderTargetSampleCount(sampleCnt, config)) {
robertphillipsd728f0c2016-11-21 11:05:03 -08001005 config = GrPixelConfigFallback(config);
Greg Daniel4065d452018-11-16 15:43:41 -05001006 // TODO: First we should be checking the getRenderTargetSampleCount from the GrBackendFormat
1007 // and not GrPixelConfig. Besides that, we should implement the fallback in the caps, but
1008 // for now we just convert the fallback pixel config to an SkColorType and then get the
1009 // GrBackendFormat from that.
1010 SkColorType colorType;
1011 if (!GrPixelConfigToColorType(config, &colorType)) {
1012 return nullptr;
1013 }
Robert Phillipsbb606772019-02-04 17:50:57 -05001014 localFormat = fContext->caps()->getBackendFormatFromColorType(colorType);
robertphillipsd728f0c2016-11-21 11:05:03 -08001015 }
1016
Greg Daniel4065d452018-11-16 15:43:41 -05001017 return this->makeDeferredRenderTargetContext(localFormat, fit, width, height, config,
1018 std::move(colorSpace), sampleCnt, mipMapped,
1019 origin, surfaceProps, budgeted);
robertphillipsd728f0c2016-11-21 11:05:03 -08001020}
1021
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001022sk_sp<GrRenderTargetContext> GrContextPriv::makeDeferredRenderTargetContext(
Greg Daniel4065d452018-11-16 15:43:41 -05001023 const GrBackendFormat& format,
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001024 SkBackingFit fit,
1025 int width, int height,
1026 GrPixelConfig config,
1027 sk_sp<SkColorSpace> colorSpace,
1028 int sampleCnt,
Greg Daniel45d63032017-10-30 13:41:26 -04001029 GrMipMapped mipMapped,
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001030 GrSurfaceOrigin origin,
1031 const SkSurfaceProps* surfaceProps,
1032 SkBudgeted budgeted) {
Brian Salomonbdecacf2018-02-02 20:32:49 -05001033 SkASSERT(sampleCnt > 0);
Khushalc421ca12018-06-26 14:38:34 -07001034 if (fContext->abandoned()) {
Brian Salomon79e4d1b2017-07-13 11:17:11 -04001035 return nullptr;
1036 }
1037
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001038 GrSurfaceDesc desc;
1039 desc.fFlags = kRenderTarget_GrSurfaceFlag;
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001040 desc.fWidth = width;
1041 desc.fHeight = height;
1042 desc.fConfig = config;
1043 desc.fSampleCnt = sampleCnt;
1044
Greg Daniele1da1d92017-10-06 15:59:27 -04001045 sk_sp<GrTextureProxy> rtp;
Greg Daniel45d63032017-10-30 13:41:26 -04001046 if (GrMipMapped::kNo == mipMapped) {
Greg Daniel4065d452018-11-16 15:43:41 -05001047 rtp = fContext->fProxyProvider->createProxy(format, desc, origin, fit, budgeted);
Greg Daniele1da1d92017-10-06 15:59:27 -04001048 } else {
Greg Daniel4065d452018-11-16 15:43:41 -05001049 rtp = fContext->fProxyProvider->createMipMapProxy(format, desc, origin, budgeted);
Greg Daniele1da1d92017-10-06 15:59:27 -04001050 }
Robert Phillips08c5ec72017-01-30 12:26:47 -05001051 if (!rtp) {
1052 return nullptr;
1053 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001054
Robert Phillips1119dc32017-04-11 12:54:57 -04001055 sk_sp<GrRenderTargetContext> renderTargetContext(
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001056 fContext->fDrawingManager->makeRenderTargetContext(std::move(rtp),
1057 std::move(colorSpace),
1058 surfaceProps));
Robert Phillips1119dc32017-04-11 12:54:57 -04001059 if (!renderTargetContext) {
1060 return nullptr;
1061 }
1062
1063 renderTargetContext->discard();
1064
1065 return renderTargetContext;
Robert Phillipsc7635fa2016-10-28 13:25:24 -04001066}
1067
Robert Phillipsbb606772019-02-04 17:50:57 -05001068sk_sp<GrSkSLFPFactoryCache> GrContextPriv::fpFactoryCache() {
1069 return fContext->fpFactoryCache();
1070}
Robert Phillipsa0bc39d2019-01-29 13:14:47 -05001071
Brian Salomonaff329b2017-08-11 09:40:37 -04001072std::unique_ptr<GrFragmentProcessor> GrContext::createPMToUPMEffect(
Brian Osman5ea96bf2018-10-02 14:58:05 -04001073 std::unique_ptr<GrFragmentProcessor> fp) {
Robert Phillips757914d2017-01-25 15:48:30 -05001074 ASSERT_SINGLE_OWNER
Brian Osman5ea96bf2018-10-02 14:58:05 -04001075 // We should have already called this->validPMUPMConversionExists() in this case
1076 SkASSERT(fDidTestPMConversions);
1077 // ...and it should have succeeded
1078 SkASSERT(this->validPMUPMConversionExists());
Brian Osman409e74f2017-04-17 11:48:28 -04001079
Brian Osman5ea96bf2018-10-02 14:58:05 -04001080 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToUnpremul);
Robert Phillips757914d2017-01-25 15:48:30 -05001081}
1082
Brian Salomonaff329b2017-08-11 09:40:37 -04001083std::unique_ptr<GrFragmentProcessor> GrContext::createUPMToPMEffect(
Brian Osman5ea96bf2018-10-02 14:58:05 -04001084 std::unique_ptr<GrFragmentProcessor> fp) {
joshualitt1de610a2016-01-06 08:26:09 -08001085 ASSERT_SINGLE_OWNER
Brian Osman5ea96bf2018-10-02 14:58:05 -04001086 // We should have already called this->validPMUPMConversionExists() in this case
1087 SkASSERT(fDidTestPMConversions);
1088 // ...and it should have succeeded
1089 SkASSERT(this->validPMUPMConversionExists());
Brian Osman409e74f2017-04-17 11:48:28 -04001090
Brian Osman5ea96bf2018-10-02 14:58:05 -04001091 return GrConfigConversionEffect::Make(std::move(fp), PMConversion::kToPremul);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001092}
1093
Brian Osman409e74f2017-04-17 11:48:28 -04001094bool GrContext::validPMUPMConversionExists() {
joshualitt1de610a2016-01-06 08:26:09 -08001095 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -04001096 if (!fDidTestPMConversions) {
Brian Osman28804f32017-04-20 10:24:36 -04001097 fPMUPMConversionsRoundTrip = GrConfigConversionEffect::TestForPreservingPMConversions(this);
Brian Osman409e74f2017-04-17 11:48:28 -04001098 fDidTestPMConversions = true;
1099 }
1100
bsalomon636e8022015-07-29 06:08:46 -07001101 // The PM<->UPM tests fail or succeed together so we only need to check one.
Brian Osman28804f32017-04-20 10:24:36 -04001102 return fPMUPMConversionsRoundTrip;
bsalomon636e8022015-07-29 06:08:46 -07001103}
1104
Khushal3e7548c2018-05-23 15:45:01 -07001105bool GrContext::supportsDistanceFieldText() const {
Robert Phillipsbb606772019-02-04 17:50:57 -05001106 return this->caps()->shaderCaps()->supportsDistanceFieldText();
Khushal3e7548c2018-05-23 15:45:01 -07001107}
1108
bsalomon37f9a262015-02-02 13:00:10 -08001109//////////////////////////////////////////////////////////////////////////////
1110
Robert Phillipsfc711a22018-02-13 17:03:00 -05001111// DDL TODO: remove 'maxResources'
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001112void GrContext::getResourceCacheLimits(int* maxResources, size_t* maxResourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -08001113 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001114 if (maxResources) {
1115 *maxResources = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -08001116 }
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001117 if (maxResourceBytes) {
1118 *maxResourceBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -08001119 }
1120}
1121
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001122void GrContext::setResourceCacheLimits(int maxResources, size_t maxResourceBytes) {
joshualitt1de610a2016-01-06 08:26:09 -08001123 ASSERT_SINGLE_OWNER
Robert Phillips8d1e67e2017-12-04 13:48:14 -05001124 fResourceCache->setLimits(maxResources, maxResourceBytes);
bsalomon37f9a262015-02-02 13:00:10 -08001125}
1126
ericrk0a5fa482015-09-15 14:16:10 -07001127//////////////////////////////////////////////////////////////////////////////
ericrk0a5fa482015-09-15 14:16:10 -07001128void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -08001129 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -07001130 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
Khushal71652e22018-10-29 13:05:36 -07001131 traceMemoryDump->dumpNumericValue("skia/gr_text_blob_cache", "size", "bytes",
1132 fTextBlobCache->usedBytes());
ericrk0a5fa482015-09-15 14:16:10 -07001133}
Brian Osman71a18892017-08-10 10:23:25 -04001134
1135//////////////////////////////////////////////////////////////////////////////
Kevin Lubickf4def342018-10-04 12:52:50 -04001136#ifdef SK_ENABLE_DUMP_GPU
1137#include "SkJSONWriter.h"
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001138SkString GrContextPriv::dump() const {
Brian Osman71a18892017-08-10 10:23:25 -04001139 SkDynamicMemoryWStream stream;
1140 SkJSONWriter writer(&stream, SkJSONWriter::Mode::kPretty);
1141 writer.beginObject();
1142
1143 static const char* kBackendStr[] = {
1144 "Metal",
1145 "OpenGL",
1146 "Vulkan",
1147 "Mock",
1148 };
Greg Danielbdf12ad2018-10-12 09:31:11 -04001149 GR_STATIC_ASSERT(0 == (unsigned)GrBackendApi::kMetal);
1150 GR_STATIC_ASSERT(1 == (unsigned)GrBackendApi::kOpenGL);
1151 GR_STATIC_ASSERT(2 == (unsigned)GrBackendApi::kVulkan);
1152 GR_STATIC_ASSERT(3 == (unsigned)GrBackendApi::kMock);
Robert Phillips4217ea72019-01-30 13:08:28 -05001153 writer.appendString("backend", kBackendStr[(unsigned)fContext->backend()]);
Brian Osman71a18892017-08-10 10:23:25 -04001154
1155 writer.appendName("caps");
Robert Phillipsbb606772019-02-04 17:50:57 -05001156 fContext->caps()->dumpJSON(&writer);
Brian Osman71a18892017-08-10 10:23:25 -04001157
1158 writer.appendName("gpu");
Robert Phillips0c4b7b12018-03-06 08:20:37 -05001159 fContext->fGpu->dumpJSON(&writer);
Brian Osman71a18892017-08-10 10:23:25 -04001160
1161 // Flush JSON to the memory stream
1162 writer.endObject();
1163 writer.flush();
1164
1165 // Null terminate the JSON data in the memory stream
1166 stream.write8(0);
1167
1168 // Allocate a string big enough to hold all the data, then copy out of the stream
1169 SkString result(stream.bytesWritten());
1170 stream.copyToAndReset(result.writable_str());
1171 return result;
1172}
Kevin Lubickf4def342018-10-04 12:52:50 -04001173#endif