blob: 0c227e3f32a12ebee7db5a24a3d4182f6066274a [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 Salomonc65aec92017-03-09 09:03:58 -05009#include "GrClip.h"
bsalomon682c2692015-05-22 14:01:46 -070010#include "GrContextOptions.h"
Brian Salomonc65aec92017-03-09 09:03:58 -050011#include "GrContextPriv.h"
robertphillips77a2e522015-10-17 07:43:27 -070012#include "GrDrawingManager.h"
Robert Phillips646e4292017-06-13 12:44:56 -040013#include "GrGpu.h"
Brian Osman11052242016-10-27 14:47:55 -040014#include "GrRenderTargetContext.h"
Brian Salomonc65aec92017-03-09 09:03:58 -050015#include "GrRenderTargetProxy.h"
bsalomon0ea80f42015-02-11 10:49:59 -080016#include "GrResourceCache.h"
bsalomond309e7a2015-04-30 14:18:54 -070017#include "GrResourceProvider.h"
Greg Danield85f97d2017-03-07 13:37:21 -050018#include "GrSemaphore.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000019#include "GrSoftwarePathRenderer.h"
Brian Osman45580d32016-11-23 09:37:01 -050020#include "GrSurfaceContext.h"
bsalomonafbf2d62014-09-30 12:18:44 -070021#include "GrSurfacePriv.h"
Robert Phillips757914d2017-01-25 15:48:30 -050022#include "GrSurfaceProxyPriv.h"
Robert Phillips646e4292017-06-13 12:44:56 -040023#include "GrTexture.h"
Brian Osman45580d32016-11-23 09:37:01 -050024#include "GrTextureContext.h"
Brian Salomondcbb9d92017-07-19 10:53:20 -040025#include "GrTracing.h"
Matt Sarett485c4992017-02-14 14:18:27 -050026#include "SkConvertPixels.h"
Brian Osman3b655982017-03-07 16:58:08 -050027#include "SkGr.h"
Matt Sarettc7b29082017-02-09 16:22:39 -050028#include "SkUnPreMultiplyPriv.h"
joshualitt5478d422014-11-14 16:00:38 -080029#include "effects/GrConfigConversionEffect.h"
joshualitte8042922015-12-11 06:11:21 -080030#include "text/GrTextBlobCache.h"
joshualitt5478d422014-11-14 16:00:38 -080031
Greg Danielb76a72a2017-07-13 15:07:54 -040032#ifdef SK_METAL
33#include "mtl/GrMtlTrampoline.h"
34#endif
35
Robert Phillipse78b7252017-04-06 07:59:41 -040036#define ASSERT_OWNED_PROXY(P) \
37SkASSERT(!(P) || !((P)->priv().peekTexture()) || (P)->priv().peekTexture()->getContext() == this)
Robert Phillips7ee385e2017-03-30 08:02:11 -040038#define ASSERT_OWNED_PROXY_PRIV(P) \
39SkASSERT(!(P) || !((P)->priv().peekTexture()) || (P)->priv().peekTexture()->getContext() == fContext)
40
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000041#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
joshualitt1de610a2016-01-06 08:26:09 -080042#define ASSERT_SINGLE_OWNER \
43 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fSingleOwner);)
robertphillips4fd74ae2016-08-03 14:26:53 -070044#define ASSERT_SINGLE_OWNER_PRIV \
45 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fContext->fSingleOwner);)
robertphillips7761d612016-05-16 09:14:53 -070046#define RETURN_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return; }
Robert Phillips7ee385e2017-03-30 08:02:11 -040047#define RETURN_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return; }
robertphillips7761d612016-05-16 09:14:53 -070048#define RETURN_FALSE_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return false; }
Robert Phillipse78b7252017-04-06 07:59:41 -040049#define RETURN_FALSE_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return false; }
robertphillips7761d612016-05-16 09:14:53 -070050#define RETURN_NULL_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return nullptr; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000051
robertphillipsea461502015-05-26 11:38:03 -070052////////////////////////////////////////////////////////////////////////////////
53
bsalomon682c2692015-05-22 14:01:46 -070054GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext) {
55 GrContextOptions defaultOptions;
56 return Create(backend, backendContext, defaultOptions);
57}
bsalomonf28cff72015-05-22 12:25:41 -070058
bsalomon682c2692015-05-22 14:01:46 -070059GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
60 const GrContextOptions& options) {
Brian Salomon91a3e522017-06-23 10:58:19 -040061 sk_sp<GrContext> context(new GrContext);
bsalomon682c2692015-05-22 14:01:46 -070062
Brian Salomon91a3e522017-06-23 10:58:19 -040063 if (!context->init(backend, backendContext, options)) {
halcanary96fcdcc2015-08-27 07:41:13 -070064 return nullptr;
bsalomon@google.com27847de2011-02-22 20:59:41 +000065 }
Brian Salomon91a3e522017-06-23 10:58:19 -040066 return context.release();
bsalomon@google.com27847de2011-02-22 20:59:41 +000067}
68
Greg Danielb76a72a2017-07-13 15:07:54 -040069#ifdef SK_METAL
70sk_sp<GrContext> GrContext::MakeMetal(void* device, void* queue, const GrContextOptions& options) {
71 sk_sp<GrContext> context(new GrContext);
72 context->fGpu = GrMtlTrampoline::CreateGpu(context.get(), options, device, queue);
73 if (!context->fGpu) {
74 return nullptr;
75 }
76 context->fBackend = kMetal_GrBackend;
77 if (!context->init(options)) {
78 return nullptr;
79 }
80 return context;
81}
82#endif
83
joshualitt0acd0d32015-05-07 08:23:19 -070084static int32_t gNextID = 1;
85static int32_t next_id() {
86 int32_t id;
87 do {
88 id = sk_atomic_inc(&gNextID);
89 } while (id == SK_InvalidGenID);
90 return id;
91}
92
bsalomon682c2692015-05-22 14:01:46 -070093GrContext::GrContext() : fUniqueID(next_id()) {
halcanary96fcdcc2015-08-27 07:41:13 -070094 fGpu = nullptr;
95 fCaps = nullptr;
96 fResourceCache = nullptr;
97 fResourceProvider = nullptr;
Brian Salomonf856fd12016-12-16 14:24:34 -050098 fAtlasGlyphCache = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000099}
100
bsalomon682c2692015-05-22 14:01:46 -0700101bool GrContext::init(GrBackend backend, GrBackendContext backendContext,
102 const GrContextOptions& options) {
joshualitt1de610a2016-01-06 08:26:09 -0800103 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700104 SkASSERT(!fGpu);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000105
Greg Danielfc978fd2017-04-13 09:54:12 -0400106 fBackend = backend;
107
bsalomon682c2692015-05-22 14:01:46 -0700108 fGpu = GrGpu::Create(backend, backendContext, options, this);
robertphillipsea461502015-05-26 11:38:03 -0700109 if (!fGpu) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000110 return false;
111 }
Greg Danielb76a72a2017-07-13 15:07:54 -0400112 return this->init(options);
113}
joshualitt1de610a2016-01-06 08:26:09 -0800114
Greg Danielb76a72a2017-07-13 15:07:54 -0400115bool GrContext::init(const GrContextOptions& options) {
116 ASSERT_SINGLE_OWNER
bsalomon76228632015-05-29 08:02:10 -0700117 fCaps = SkRef(fGpu->caps());
Brian Osman13dddce2017-05-09 13:19:50 -0400118 fResourceCache = new GrResourceCache(fCaps, fUniqueID);
joshualitt6d0872d2016-01-11 08:27:48 -0800119 fResourceProvider = new GrResourceProvider(fGpu, fResourceCache, &fSingleOwner);
commit-bot@chromium.org1836d332013-07-16 22:55:03 +0000120
Brian Osman46da1cc2017-02-14 14:15:48 -0500121 fDisableGpuYUVConversion = options.fDisableGpuYUVConversion;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000122 fDidTestPMConversions = false;
123
bsalomon6b2552f2016-09-15 13:50:26 -0700124 GrPathRendererChain::Options prcOptions;
bsalomon39ef7fb2016-09-21 11:16:05 -0700125 prcOptions.fAllowPathMaskCaching = options.fAllowPathMaskCaching;
csmartdalton008b9d82017-02-22 12:00:42 -0700126 prcOptions.fGpuPathRenderers = options.fGpuPathRenderers;
Robert Phillips3ea17982017-06-02 12:43:04 -0400127 fDrawingManager.reset(new GrDrawingManager(this, prcOptions, &fSingleOwner));
joshualitt7c3a2f82015-03-31 13:32:05 -0700128
Eric Karl6d342282017-05-03 17:08:42 -0700129 fAtlasGlyphCache = new GrAtlasGlyphCache(this, options.fGlyphCacheTextureMaximumBytes);
joshualittb7133be2015-04-08 09:08:31 -0700130
halcanary385fe4d2015-08-26 13:07:48 -0700131 fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB, this));
Brian Salomon91a3e522017-06-23 10:58:19 -0400132
133 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000134}
135
bsalomon@google.com27847de2011-02-22 20:59:41 +0000136GrContext::~GrContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800137 ASSERT_SINGLE_OWNER
138
robertphillipsea461502015-05-26 11:38:03 -0700139 if (!fGpu) {
bsalomon76228632015-05-29 08:02:10 -0700140 SkASSERT(!fCaps);
bsalomon@google.com733c0622013-04-24 17:59:32 +0000141 return;
142 }
143
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000144 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000145
robertphillips77a2e522015-10-17 07:43:27 -0700146 fDrawingManager->cleanup();
robertphillips2334fb62015-06-17 05:43:33 -0700147
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000148 for (int i = 0; i < fCleanUpData.count(); ++i) {
149 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
150 }
151
halcanary385fe4d2015-08-26 13:07:48 -0700152 delete fResourceProvider;
153 delete fResourceCache;
Brian Salomonf856fd12016-12-16 14:24:34 -0500154 delete fAtlasGlyphCache;
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000155
bsalomon@google.com205d4602011-04-25 12:43:45 +0000156 fGpu->unref();
bsalomon76228632015-05-29 08:02:10 -0700157 fCaps->unref();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000158}
159
bungeman6bd52842016-10-27 09:30:08 -0700160sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
bsalomon41b952c2016-03-11 06:46:33 -0800161 if (!fThreadSafeProxy) {
bungeman6bd52842016-10-27 09:30:08 -0700162 fThreadSafeProxy.reset(new GrContextThreadSafeProxy(sk_ref_sp(fCaps), this->uniqueID()));
bsalomon41b952c2016-03-11 06:46:33 -0800163 }
bungeman6bd52842016-10-27 09:30:08 -0700164 return fThreadSafeProxy;
bsalomon41b952c2016-03-11 06:46:33 -0800165}
166
bsalomon2354f842014-07-28 13:48:36 -0700167void GrContext::abandonContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800168 ASSERT_SINGLE_OWNER
169
bsalomond309e7a2015-04-30 14:18:54 -0700170 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800171
172 // Need to abandon the drawing manager first so all the render targets
173 // will be released/forgotten before they too are abandoned.
174 fDrawingManager->abandon();
175
bsalomon@google.com205d4602011-04-25 12:43:45 +0000176 // abandon first to so destructors
177 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800178 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700179
bsalomon6e2aad42016-04-01 11:54:31 -0700180 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
181
Brian Salomonf856fd12016-12-16 14:24:34 -0500182 fAtlasGlyphCache->freeAll();
bsalomon6e2aad42016-04-01 11:54:31 -0700183 fTextBlobCache->freeAll();
184}
185
186void GrContext::releaseResourcesAndAbandonContext() {
187 ASSERT_SINGLE_OWNER
188
189 fResourceProvider->abandon();
190
191 // Need to abandon the drawing manager first so all the render targets
192 // will be released/forgotten before they too are abandoned.
193 fDrawingManager->abandon();
194
195 // Release all resources in the backend 3D API.
196 fResourceCache->releaseAll();
197
198 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000199
Brian Salomonf856fd12016-12-16 14:24:34 -0500200 fAtlasGlyphCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700201 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000202}
203
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000204void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800205 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000206 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000207}
208
209void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800210 ASSERT_SINGLE_OWNER
211
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000212 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000213
Brian Salomonf856fd12016-12-16 14:24:34 -0500214 fAtlasGlyphCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700215
216 fDrawingManager->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700217
218 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000219}
220
Brian Salomon5e150852017-03-22 14:53:13 -0400221void GrContext::purgeResourcesNotUsedInMs(std::chrono::milliseconds ms) {
222 ASSERT_SINGLE_OWNER
223 fResourceCache->purgeResourcesNotUsedSince(GrStdSteadyClock::now() - ms);
224}
225
Derek Sollenberger5480a182017-05-25 16:43:59 -0400226void GrContext::purgeUnlockedResources(size_t bytesToPurge, bool preferScratchResources) {
227 ASSERT_SINGLE_OWNER
228 fResourceCache->purgeUnlockedResources(bytesToPurge, preferScratchResources);
229}
230
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000231void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800232 ASSERT_SINGLE_OWNER
233
bsalomon71cb0c22014-11-14 12:10:14 -0800234 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800235 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800236 }
237 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800238 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800239 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000240}
241
Derek Sollenbergeree479142017-05-24 11:41:33 -0400242size_t GrContext::getResourceCachePurgeableBytes() const {
243 ASSERT_SINGLE_OWNER
244 return fResourceCache->getPurgeableBytes();
245}
246
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000247////////////////////////////////////////////////////////////////////////////////
248
joshualitt0db6dfa2015-04-10 07:01:30 -0700249void GrContext::TextBlobCacheOverBudgetCB(void* data) {
250 SkASSERT(data);
Brian Osman11052242016-10-27 14:47:55 -0400251 // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on
252 // GrRenderTargetContext to perform a necessary flush. The solution is to move drawText calls
253 // to below the GrContext level, but this is not trivial because they call drawPath on
254 // SkGpuDevice.
joshualitt0db6dfa2015-04-10 07:01:30 -0700255 GrContext* context = reinterpret_cast<GrContext*>(data);
256 context->flush();
257}
258
bsalomon@google.com27847de2011-02-22 20:59:41 +0000259////////////////////////////////////////////////////////////////////////////////
260
bsalomonb77a9072016-09-07 10:02:04 -0700261void GrContext::flush() {
joshualitt1de610a2016-01-06 08:26:09 -0800262 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700263 RETURN_IF_ABANDONED
Robert Phillips7ee385e2017-03-30 08:02:11 -0400264
265 fDrawingManager->flush(nullptr);
266}
267
268void GrContextPriv::flush(GrSurfaceProxy* proxy) {
269 ASSERT_SINGLE_OWNER_PRIV
270 RETURN_IF_ABANDONED_PRIV
271 ASSERT_OWNED_PROXY_PRIV(proxy);
272
273 fContext->fDrawingManager->flush(proxy);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000274}
275
bsalomon81beccc2014-10-13 12:32:55 -0700276bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
277 const void* inPixels, size_t outRowBytes, void* outPixels) {
Matt Sarettc7b29082017-02-09 16:22:39 -0500278 SkColorType colorType;
279 if (!GrPixelConfigToColorType(srcConfig, &colorType) ||
280 4 != SkColorTypeBytesPerPixel(colorType))
281 {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000282 return false;
283 }
bsalomon81beccc2014-10-13 12:32:55 -0700284
Matt Sarettc7b29082017-02-09 16:22:39 -0500285 for (int y = 0; y < height; y++) {
286 SkOpts::RGBA_to_rgbA((uint32_t*) outPixels, inPixels, width);
287 outPixels = SkTAddOffset<void>(outPixels, outRowBytes);
288 inPixels = SkTAddOffset<const void>(inPixels, inRowBytes);
289 }
bsalomon81beccc2014-10-13 12:32:55 -0700290
Matt Sarettc7b29082017-02-09 16:22:39 -0500291 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000292}
293
Brian Osmand2ca59a2017-04-13 14:03:57 -0400294static bool valid_premul_config(GrPixelConfig config) {
Brian Osmance425512017-03-22 14:37:50 -0400295 return GrPixelConfigIs8888Unorm(config) || kRGBA_half_GrPixelConfig == config;
296}
297
Brian Osmand2ca59a2017-04-13 14:03:57 -0400298static bool valid_pixel_conversion(GrPixelConfig srcConfig, GrPixelConfig dstConfig,
299 bool premulConversion) {
300 // We don't allow conversion between integer configs and float/fixed configs.
301 if (GrPixelConfigIsSint(srcConfig) != GrPixelConfigIsSint(dstConfig)) {
302 return false;
303 }
304
305 // We only allow premul <-> unpremul conversions for some formats
306 if (premulConversion && (!valid_premul_config(srcConfig) || !valid_premul_config(dstConfig))) {
307 return false;
308 }
309
310 return true;
311}
312
Brian Osman409e74f2017-04-17 11:48:28 -0400313static bool pm_upm_must_round_trip(GrPixelConfig config, SkColorSpace* colorSpace) {
314 return !colorSpace &&
315 (kRGBA_8888_GrPixelConfig == config || kBGRA_8888_GrPixelConfig == config);
316}
317
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400318bool GrContextPriv::writeSurfacePixels(GrSurfaceContext* dst,
Robert Phillipse78b7252017-04-06 07:59:41 -0400319 int left, int top, int width, int height,
320 GrPixelConfig srcConfig, SkColorSpace* srcColorSpace,
321 const void* buffer, size_t rowBytes,
322 uint32_t pixelOpsFlags) {
Brian Osmanb62ea222016-12-22 11:12:16 -0500323 // TODO: Color space conversion
324
Robert Phillipse78b7252017-04-06 07:59:41 -0400325 ASSERT_SINGLE_OWNER_PRIV
326 RETURN_FALSE_IF_ABANDONED_PRIV
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400327 SkASSERT(dst);
328 ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy());
Brian Salomondcbb9d92017-07-19 10:53:20 -0400329 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "writeSurfacePixels", fContext);
bsalomon6c6f6582015-09-10 08:12:46 -0700330
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400331 if (!dst->asSurfaceProxy()->instantiate(fContext->resourceProvider())) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400332 return false;
333 }
334
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400335 GrSurface* dstSurface = dst->asSurfaceProxy()->priv().peekSurface();
336
Brian Osmand2ca59a2017-04-13 14:03:57 -0400337 // The src is unpremul but the dst is premul -> premul the src before or as part of the write
Brian Osman409e74f2017-04-17 11:48:28 -0400338 const bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400339 if (!valid_pixel_conversion(srcConfig, dstSurface->config(), premul)) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400340 return false;
341 }
342
Brian Osman409e74f2017-04-17 11:48:28 -0400343 // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data,
344 // without any color spaces attached, and the caller wants us to premul.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400345 bool useConfigConversionEffect =
346 premul &&
347 pm_upm_must_round_trip(srcConfig, srcColorSpace) &&
348 pm_upm_must_round_trip(dstSurface->config(), dst->getColorSpace());
Brian Osman409e74f2017-04-17 11:48:28 -0400349
350 // Are we going to try to premul as part of a draw? For the non-legacy case, we always allow
351 // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly.
352 bool premulOnGpu = premul &&
353 (!useConfigConversionEffect || fContext->validPMUPMConversionExists());
bsalomon81beccc2014-10-13 12:32:55 -0700354
bsalomone8d21e82015-07-16 08:23:13 -0700355 // Trim the params here so that if we wind up making a temporary surface it can be as small as
bsalomonf0674512015-07-28 13:26:15 -0700356 // necessary and because GrGpu::getWritePixelsInfo requires it.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400357 if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(),
bsalomone8d21e82015-07-16 08:23:13 -0700358 GrBytesPerPixel(srcConfig), &left, &top, &width,
359 &height, &buffer, &rowBytes)) {
360 return false;
361 }
362
Brian Osman409e74f2017-04-17 11:48:28 -0400363 GrGpu::DrawPreference drawPreference = premulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference
364 : GrGpu::kNoDraw_DrawPreference;
bsalomonf0674512015-07-28 13:26:15 -0700365 GrGpu::WritePixelTempDrawInfo tempDrawInfo;
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400366 if (!fContext->fGpu->getWritePixelsInfo(dstSurface, width, height, srcConfig,
Robert Phillipse78b7252017-04-06 07:59:41 -0400367 &drawPreference, &tempDrawInfo)) {
bsalomonf0674512015-07-28 13:26:15 -0700368 return false;
369 }
370
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400371 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && dstSurface->surfacePriv().hasPendingIO()) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400372 this->flush(nullptr); // MDB TODO: tighten this
bsalomonf0674512015-07-28 13:26:15 -0700373 }
374
Robert Phillips2f493142017-03-02 18:18:38 -0500375 sk_sp<GrTextureProxy> tempProxy;
bsalomonf0674512015-07-28 13:26:15 -0700376 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400377 tempProxy = GrSurfaceProxy::MakeDeferred(fContext->resourceProvider(),
Robert Phillips2f493142017-03-02 18:18:38 -0500378 tempDrawInfo.fTempSurfaceDesc,
379 SkBackingFit::kApprox,
380 SkBudgeted::kYes);
381 if (!tempProxy && GrGpu::kRequireDraw_DrawPreference == drawPreference) {
bsalomonf0674512015-07-28 13:26:15 -0700382 return false;
383 }
384 }
385
386 // temp buffer for doing sw premul conversion, if needed.
387 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
Brian Osman409e74f2017-04-17 11:48:28 -0400388 // We need to do sw premul if we were unable to create a RT for drawing, or if we can't do the
389 // premul on the GPU
390 if (premul && (!tempProxy || !premulOnGpu)) {
391 size_t tmpRowBytes = 4 * width;
392 tmpPixels.reset(width * height);
393 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
394 tmpPixels.get())) {
395 return false;
bsalomonf0674512015-07-28 13:26:15 -0700396 }
Brian Osman409e74f2017-04-17 11:48:28 -0400397 rowBytes = tmpRowBytes;
398 buffer = tmpPixels.get();
bsalomonf0674512015-07-28 13:26:15 -0700399 }
Brian Osman409e74f2017-04-17 11:48:28 -0400400
401 if (tempProxy) {
402 sk_sp<GrFragmentProcessor> fp = GrSimpleTextureEffect::Make(
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400403 tempProxy, nullptr, SkMatrix::I());
Brian Osman409e74f2017-04-17 11:48:28 -0400404 if (premulOnGpu) {
405 fp = fContext->createUPMToPMEffect(std::move(fp), useConfigConversionEffect);
bsalomon81beccc2014-10-13 12:32:55 -0700406 }
Brian Osman409e74f2017-04-17 11:48:28 -0400407 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400408 if (!fp) {
409 return false;
410 }
Brian Osman409e74f2017-04-17 11:48:28 -0400411
412 if (tempProxy->priv().hasPendingIO()) {
413 this->flush(tempProxy.get());
414 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400415 if (!tempProxy->instantiate(fContext->resourceProvider())) {
Brian Osman409e74f2017-04-17 11:48:28 -0400416 return false;
417 }
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400418 GrTexture* texture = tempProxy->priv().peekTexture();
Brian Osman409e74f2017-04-17 11:48:28 -0400419 if (!fContext->fGpu->writePixels(texture, 0, 0, width, height, tempDrawInfo.fWriteConfig,
420 buffer, rowBytes)) {
421 return false;
422 }
423 SkMatrix matrix;
424 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400425 GrRenderTargetContext* renderTargetContext = dst->asRenderTargetContext();
Brian Osman409e74f2017-04-17 11:48:28 -0400426 if (!renderTargetContext) {
427 return false;
428 }
429 GrPaint paint;
430 paint.addColorFragmentProcessor(std::move(fp));
431 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Osman7ab6a7f2017-04-25 15:01:46 -0400432 paint.setAllowSRGBInputs(SkToBool(dst->getColorSpace()) ||
433 GrPixelConfigIsSRGB(renderTargetContext->config()));
Brian Osman409e74f2017-04-17 11:48:28 -0400434 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
435 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, matrix, rect,
436 nullptr);
437
438 if (kFlushWrites_PixelOp & pixelOpsFlags) {
439 this->flushSurfaceWrites(renderTargetContext->asRenderTargetProxy());
440 }
441 } else {
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400442 return fContext->fGpu->writePixels(dstSurface, left, top, width, height, srcConfig,
Robert Phillipse78b7252017-04-06 07:59:41 -0400443 buffer, rowBytes);
bsalomon81beccc2014-10-13 12:32:55 -0700444 }
bsalomon81beccc2014-10-13 12:32:55 -0700445 return true;
446}
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000447
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400448bool GrContextPriv::readSurfacePixels(GrSurfaceContext* src,
Robert Phillipse78b7252017-04-06 07:59:41 -0400449 int left, int top, int width, int height,
450 GrPixelConfig dstConfig, SkColorSpace* dstColorSpace,
451 void* buffer, size_t rowBytes, uint32_t flags) {
Brian Osmanb62ea222016-12-22 11:12:16 -0500452 // TODO: Color space conversion
453
Robert Phillipse78b7252017-04-06 07:59:41 -0400454 ASSERT_SINGLE_OWNER_PRIV
455 RETURN_FALSE_IF_ABANDONED_PRIV
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400456 SkASSERT(src);
457 ASSERT_OWNED_PROXY_PRIV(src->asSurfaceProxy());
Brian Salomondcbb9d92017-07-19 10:53:20 -0400458 GR_CREATE_TRACE_MARKER_CONTEXT("GrContextPriv", "readSurfacePixels", fContext);
bsalomon32ab2602015-09-09 18:57:49 -0700459
Robert Phillipse78b7252017-04-06 07:59:41 -0400460 // MDB TODO: delay this instantiation until later in the method
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400461 if (!src->asSurfaceProxy()->instantiate(fContext->resourceProvider())) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400462 return false;
463 }
464
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400465 GrSurface* srcSurface = src->asSurfaceProxy()->priv().peekSurface();
466
Brian Osmand2ca59a2017-04-13 14:03:57 -0400467 // The src is premul but the dst is unpremul -> unpremul the src after or as part of the read
468 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400469 if (!valid_pixel_conversion(srcSurface->config(), dstConfig, unpremul)) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400470 return false;
471 }
472
Brian Osman409e74f2017-04-17 11:48:28 -0400473 // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data,
474 // without any color spaces attached, and the caller wants us to unpremul.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400475 bool useConfigConversionEffect =
476 unpremul &&
477 pm_upm_must_round_trip(srcSurface->config(), src->getColorSpace()) &&
478 pm_upm_must_round_trip(dstConfig, dstColorSpace);
Brian Osman409e74f2017-04-17 11:48:28 -0400479
480 // Are we going to try to unpremul as part of a draw? For the non-legacy case, we always allow
481 // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly.
482 bool unpremulOnGpu = unpremul &&
483 (!useConfigConversionEffect || fContext->validPMUPMConversionExists());
bsalomon6c6f6582015-09-10 08:12:46 -0700484
bsalomone8d21e82015-07-16 08:23:13 -0700485 // Adjust the params so that if we wind up using an intermediate surface we've already done
486 // all the trimming and the temporary can be the min size required.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400487 if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(),
bsalomone8d21e82015-07-16 08:23:13 -0700488 GrBytesPerPixel(dstConfig), &left,
489 &top, &width, &height, &buffer, &rowBytes)) {
490 return false;
491 }
492
Brian Osman409e74f2017-04-17 11:48:28 -0400493 GrGpu::DrawPreference drawPreference = unpremulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference
494 : GrGpu::kNoDraw_DrawPreference;
bsalomon39826022015-07-23 08:07:21 -0700495 GrGpu::ReadPixelTempDrawInfo tempDrawInfo;
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400496 if (!fContext->fGpu->getReadPixelsInfo(srcSurface, width, height, rowBytes, dstConfig,
Robert Phillipse78b7252017-04-06 07:59:41 -0400497 &drawPreference, &tempDrawInfo)) {
bsalomon39826022015-07-23 08:07:21 -0700498 return false;
499 }
bsalomon191bcc02014-11-14 11:31:13 -0800500
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400501 if (!(kDontFlush_PixelOpsFlag & flags) && srcSurface->surfacePriv().hasPendingWrite()) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400502 this->flush(nullptr); // MDB TODO: tighten this
503 }
504
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400505 sk_sp<GrSurfaceProxy> proxyToRead = src->asSurfaceProxyRef();
bsalomon39826022015-07-23 08:07:21 -0700506 bool didTempDraw = false;
507 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomonb117ff12016-07-19 07:24:40 -0700508 if (SkBackingFit::kExact == tempDrawInfo.fTempSurfaceFit) {
bsalomon39826022015-07-23 08:07:21 -0700509 // We only respect this when the entire src is being read. Otherwise we can trigger too
510 // many odd ball texture sizes and trash the cache.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400511 if (width != srcSurface->width() || height != srcSurface->height()) {
bsalomonb117ff12016-07-19 07:24:40 -0700512 tempDrawInfo.fTempSurfaceFit= SkBackingFit::kApprox;
bsalomon39826022015-07-23 08:07:21 -0700513 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000514 }
brianosmandfe4f2e2016-07-21 13:28:36 -0700515 // TODO: Need to decide the semantics of this function for color spaces. Do we support
516 // conversion to a passed-in color space? For now, specifying nullptr means that this
517 // path will do no conversion, so it will match the behavior of the non-draw path.
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400518 sk_sp<GrRenderTargetContext> tempRTC = fContext->makeDeferredRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400519 tempDrawInfo.fTempSurfaceFit,
bsalomonb117ff12016-07-19 07:24:40 -0700520 tempDrawInfo.fTempSurfaceDesc.fWidth,
521 tempDrawInfo.fTempSurfaceDesc.fHeight,
522 tempDrawInfo.fTempSurfaceDesc.fConfig,
brianosmandfe4f2e2016-07-21 13:28:36 -0700523 nullptr,
bsalomonb117ff12016-07-19 07:24:40 -0700524 tempDrawInfo.fTempSurfaceDesc.fSampleCnt,
525 tempDrawInfo.fTempSurfaceDesc.fOrigin);
Brian Osman693a5402016-10-27 15:13:22 -0400526 if (tempRTC) {
Robert Phillips67c18d62017-01-20 12:44:06 -0500527 SkMatrix textureMatrix = SkMatrix::MakeTrans(SkIntToScalar(left), SkIntToScalar(top));
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400528 sk_sp<GrTextureProxy> proxy = src->asTextureProxyRef();
Brian Osman409e74f2017-04-17 11:48:28 -0400529 sk_sp<GrFragmentProcessor> fp = GrSimpleTextureEffect::Make(
Robert Phillipsfbcef6e2017-06-15 12:07:18 -0400530 std::move(proxy), nullptr, textureMatrix);
Brian Osman409e74f2017-04-17 11:48:28 -0400531 if (unpremulOnGpu) {
532 fp = fContext->createPMToUPMEffect(std::move(fp), useConfigConversionEffect);
533 // We no longer need to do this on CPU after the read back.
534 unpremul = false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000535 }
Brian Osman409e74f2017-04-17 11:48:28 -0400536 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle);
Robert Phillips1c9686b2017-06-30 08:40:28 -0400537 if (!fp) {
538 return false;
539 }
Brian Osman60cd57e2017-04-06 10:19:06 -0400540
Brian Osman409e74f2017-04-17 11:48:28 -0400541 GrPaint paint;
542 paint.addColorFragmentProcessor(std::move(fp));
543 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
544 paint.setAllowSRGBInputs(true);
545 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
546 tempRTC->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect,
547 nullptr);
548 proxyToRead = tempRTC->asTextureProxyRef();
549 left = 0;
550 top = 0;
551 didTempDraw = true;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000552 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000553 }
joshualitt5c55fef2014-10-31 14:04:35 -0700554
Robert Phillipse78b7252017-04-06 07:59:41 -0400555 if (!proxyToRead) {
556 return false;
557 }
558
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400559 if (!proxyToRead->instantiate(fContext->resourceProvider())) {
Robert Phillips833dcf42016-11-18 08:44:13 -0500560 return false;
561 }
562
Robert Phillipseee4d6e2017-06-05 09:26:07 -0400563 GrSurface* surfaceToRead = proxyToRead->priv().peekSurface();
564
bsalomon39826022015-07-23 08:07:21 -0700565 if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000566 return false;
567 }
bsalomon39826022015-07-23 08:07:21 -0700568 GrPixelConfig configToRead = dstConfig;
569 if (didTempDraw) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400570 this->flushSurfaceWrites(proxyToRead.get());
bsalomon6c9cd552016-01-22 07:17:34 -0800571 configToRead = tempDrawInfo.fReadConfig;
bsalomon39826022015-07-23 08:07:21 -0700572 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400573 if (!fContext->fGpu->readPixels(surfaceToRead, left, top, width, height, configToRead,
574 buffer, rowBytes)) {
bsalomon39826022015-07-23 08:07:21 -0700575 return false;
576 }
577
578 // Perform umpremul conversion if we weren't able to perform it as a draw.
579 if (unpremul) {
Matt Sarettc7b29082017-02-09 16:22:39 -0500580 SkColorType colorType;
581 if (!GrPixelConfigToColorType(dstConfig, &colorType) ||
582 4 != SkColorTypeBytesPerPixel(colorType))
583 {
reed@google.com7111d462014-03-25 16:20:24 +0000584 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000585 }
reed@google.com7111d462014-03-25 16:20:24 +0000586
Matt Sarettc7b29082017-02-09 16:22:39 -0500587 for (int y = 0; y < height; y++) {
588 SkUnpremultiplyRow<false>((uint32_t*) buffer, (const uint32_t*) buffer, width);
589 buffer = SkTAddOffset<void>(buffer, rowBytes);
590 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000591 }
592 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000593}
594
Robert Phillips7ee385e2017-03-30 08:02:11 -0400595void GrContextPriv::prepareSurfaceForExternalIO(GrSurfaceProxy* proxy) {
596 ASSERT_SINGLE_OWNER_PRIV
597 RETURN_IF_ABANDONED_PRIV
598 SkASSERT(proxy);
599 ASSERT_OWNED_PROXY_PRIV(proxy);
600 fContext->fDrawingManager->prepareSurfaceForExternalIO(proxy);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000601}
602
Robert Phillips7ee385e2017-03-30 08:02:11 -0400603void GrContextPriv::flushSurfaceWrites(GrSurfaceProxy* proxy) {
604 ASSERT_SINGLE_OWNER_PRIV
605 RETURN_IF_ABANDONED_PRIV
606 SkASSERT(proxy);
607 ASSERT_OWNED_PROXY_PRIV(proxy);
608 if (proxy->priv().hasPendingWrite()) {
609 this->flush(proxy);
bsalomonf80bfed2014-10-07 05:56:02 -0700610 }
611}
612
Robert Phillips7ee385e2017-03-30 08:02:11 -0400613void GrContextPriv::flushSurfaceIO(GrSurfaceProxy* proxy) {
614 ASSERT_SINGLE_OWNER_PRIV
615 RETURN_IF_ABANDONED_PRIV
616 SkASSERT(proxy);
617 ASSERT_OWNED_PROXY_PRIV(proxy);
618 if (proxy->priv().hasPendingIO()) {
619 this->flush(proxy);
ajuma95243eb2016-08-24 08:19:02 -0700620 }
621}
622
bsalomon@google.com27847de2011-02-22 20:59:41 +0000623////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000624int GrContext::getRecommendedSampleCount(GrPixelConfig config,
625 SkScalar dpi) const {
joshualitt1de610a2016-01-06 08:26:09 -0800626 ASSERT_SINGLE_OWNER
627
bsalomon76228632015-05-29 08:02:10 -0700628 if (!this->caps()->isConfigRenderable(config, true)) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000629 return 0;
630 }
631 int chosenSampleCount = 0;
jvanverthe9c0fc62015-04-29 11:18:05 -0700632 if (fGpu->caps()->shaderCaps()->pathRenderingSupport()) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000633 if (dpi >= 250.0f) {
634 chosenSampleCount = 4;
635 } else {
636 chosenSampleCount = 16;
637 }
638 }
egdanieleed519e2016-01-15 11:36:18 -0800639 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ? chosenSampleCount : 0;
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000640}
641
Robert Phillips2c862492017-01-18 10:08:39 -0500642sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy> proxy,
643 sk_sp<SkColorSpace> colorSpace) {
Brian Osman45580d32016-11-23 09:37:01 -0500644 ASSERT_SINGLE_OWNER_PRIV
645
Brian Osman45580d32016-11-23 09:37:01 -0500646 if (proxy->asRenderTargetProxy()) {
Robert Phillips2c862492017-01-18 10:08:39 -0500647 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
648 std::move(colorSpace), nullptr);
Brian Osman45580d32016-11-23 09:37:01 -0500649 } else {
650 SkASSERT(proxy->asTextureProxy());
Robert Phillips2c862492017-01-18 10:08:39 -0500651 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Brian Osman45580d32016-11-23 09:37:01 -0500652 }
653}
654
Robert Phillipse2f7d182016-12-15 09:23:05 -0500655sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(const GrSurfaceDesc& dstDesc,
656 SkBackingFit fit,
657 SkBudgeted isDstBudgeted) {
658
Brian Osman32342f02017-03-04 08:12:46 -0500659 sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeDeferred(fContext->resourceProvider(),
Robert Phillips26c90e02017-03-14 14:39:29 -0400660 dstDesc, fit, isDstBudgeted);
Robert Phillips77b3f322017-01-31 18:24:12 -0500661 if (!proxy) {
662 return nullptr;
663 }
Robert Phillipse2f7d182016-12-15 09:23:05 -0500664
Robert Phillips2c862492017-01-18 10:08:39 -0500665 return this->makeWrappedSurfaceContext(std::move(proxy), nullptr);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500666}
667
Greg Daniel7ef28f32017-04-20 16:41:55 +0000668sk_sp<GrSurfaceContext> GrContextPriv::makeBackendSurfaceContext(const GrBackendTexture& tex,
669 GrSurfaceOrigin origin,
670 GrBackendTextureFlags flags,
671 int sampleCnt,
Brian Osmanc1e37052017-03-09 14:19:20 -0500672 sk_sp<SkColorSpace> colorSpace) {
Robert Phillips26caf892017-01-27 10:58:31 -0500673 ASSERT_SINGLE_OWNER_PRIV
674
Greg Daniel7ef28f32017-04-20 16:41:55 +0000675 sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTexture(tex, origin,
676 flags, sampleCnt));
Robert Phillips26caf892017-01-27 10:58:31 -0500677 if (!surface) {
678 return nullptr;
679 }
680
681 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface)));
Robert Phillips77b3f322017-01-31 18:24:12 -0500682 if (!proxy) {
683 return nullptr;
684 }
Robert Phillips26caf892017-01-27 10:58:31 -0500685
686 return this->makeWrappedSurfaceContext(std::move(proxy), std::move(colorSpace));
687}
688
Brian Osman11052242016-10-27 14:47:55 -0400689sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000690 const GrBackendTexture& tex,
691 GrSurfaceOrigin origin,
692 int sampleCnt,
Brian Osman11052242016-10-27 14:47:55 -0400693 sk_sp<SkColorSpace> colorSpace,
Brian Osmanc1e37052017-03-09 14:19:20 -0500694 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700695 ASSERT_SINGLE_OWNER_PRIV
robertphillips4fd74ae2016-08-03 14:26:53 -0700696
Greg Daniel7ef28f32017-04-20 16:41:55 +0000697 static const GrBackendTextureFlags kForceRT = kRenderTarget_GrBackendTextureFlag;
698 sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTexture(tex, origin, kForceRT,
699 sampleCnt));
robertphillips4fd74ae2016-08-03 14:26:53 -0700700 if (!surface) {
701 return nullptr;
702 }
703
Robert Phillips37430132016-11-09 06:50:43 -0500704 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface)));
Robert Phillips77b3f322017-01-31 18:24:12 -0500705 if (!proxy) {
706 return nullptr;
707 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400708
Robert Phillips37430132016-11-09 06:50:43 -0500709 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Brian Osman11052242016-10-27 14:47:55 -0400710 std::move(colorSpace), props);
robertphillips4fd74ae2016-08-03 14:26:53 -0700711}
712
Brian Osman11052242016-10-27 14:47:55 -0400713sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext(
Greg Danielbcf612b2017-05-01 13:50:58 +0000714 const GrBackendRenderTarget& backendRT,
715 GrSurfaceOrigin origin,
robertphillips4fd74ae2016-08-03 14:26:53 -0700716 sk_sp<SkColorSpace> colorSpace,
717 const SkSurfaceProps* surfaceProps) {
718 ASSERT_SINGLE_OWNER_PRIV
719
Greg Danielbcf612b2017-05-01 13:50:58 +0000720 sk_sp<GrRenderTarget> rt(fContext->resourceProvider()->wrapBackendRenderTarget(backendRT,
721 origin));
robertphillips4fd74ae2016-08-03 14:26:53 -0700722 if (!rt) {
723 return nullptr;
724 }
725
Robert Phillips37430132016-11-09 06:50:43 -0500726 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(rt)));
Robert Phillips77b3f322017-01-31 18:24:12 -0500727 if (!proxy) {
728 return nullptr;
729 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400730
Robert Phillips37430132016-11-09 06:50:43 -0500731 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400732 std::move(colorSpace),
Brian Osman11052242016-10-27 14:47:55 -0400733 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -0700734}
735
Brian Osman11052242016-10-27 14:47:55 -0400736sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000737 const GrBackendTexture& tex,
738 GrSurfaceOrigin origin,
739 int sampleCnt,
robertphillips4fd74ae2016-08-03 14:26:53 -0700740 sk_sp<SkColorSpace> colorSpace,
741 const SkSurfaceProps* surfaceProps) {
742 ASSERT_SINGLE_OWNER_PRIV
robertphillips4fd74ae2016-08-03 14:26:53 -0700743
Greg Daniel7ef28f32017-04-20 16:41:55 +0000744 sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTextureAsRenderTarget(
745 tex,
746 origin,
747 sampleCnt));
robertphillips4fd74ae2016-08-03 14:26:53 -0700748 if (!surface) {
749 return nullptr;
750 }
751
Robert Phillips37430132016-11-09 06:50:43 -0500752 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface)));
Robert Phillips77b3f322017-01-31 18:24:12 -0500753 if (!proxy) {
754 return nullptr;
755 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400756
Robert Phillips37430132016-11-09 06:50:43 -0500757 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400758 std::move(colorSpace),
759 surfaceProps);
robertphillips77a2e522015-10-17 07:43:27 -0700760}
761
Chris Daltonfe199b72017-05-05 11:26:15 -0400762void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
763 fContext->fDrawingManager->addOnFlushCallbackObject(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400764}
765
766
robertphillips48fde9c2016-09-06 05:20:20 -0700767static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) {
Brian Osman78f20e02017-01-12 10:28:01 -0500768 switch (config) {
769 case kAlpha_8_GrPixelConfig:
770 case kRGB_565_GrPixelConfig:
771 case kRGBA_4444_GrPixelConfig:
772 case kBGRA_8888_GrPixelConfig:
773 return kRGBA_8888_GrPixelConfig;
774 case kSBGRA_8888_GrPixelConfig:
775 return kSRGBA_8888_GrPixelConfig;
776 case kAlpha_half_GrPixelConfig:
777 return kRGBA_half_GrPixelConfig;
778 default:
779 return kUnknown_GrPixelConfig;
780 }
robertphillips48fde9c2016-09-06 05:20:20 -0700781}
782
robertphillipsd728f0c2016-11-21 11:05:03 -0800783sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContextWithFallback(
784 SkBackingFit fit,
785 int width, int height,
786 GrPixelConfig config,
787 sk_sp<SkColorSpace> colorSpace,
788 int sampleCnt,
789 GrSurfaceOrigin origin,
790 const SkSurfaceProps* surfaceProps,
791 SkBudgeted budgeted) {
792 if (!this->caps()->isConfigRenderable(config, sampleCnt > 0)) {
793 config = GrPixelConfigFallback(config);
794 }
795
796 return this->makeDeferredRenderTargetContext(fit, width, height, config, std::move(colorSpace),
797 sampleCnt, origin, surfaceProps, budgeted);
798}
799
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400800sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContext(
801 SkBackingFit fit,
802 int width, int height,
803 GrPixelConfig config,
804 sk_sp<SkColorSpace> colorSpace,
805 int sampleCnt,
806 GrSurfaceOrigin origin,
807 const SkSurfaceProps* surfaceProps,
808 SkBudgeted budgeted) {
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400809 SkASSERT(kDefault_GrSurfaceOrigin != origin);
810
Brian Salomon79e4d1b2017-07-13 11:17:11 -0400811 if (this->abandoned()) {
812 return nullptr;
813 }
814
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400815 GrSurfaceDesc desc;
816 desc.fFlags = kRenderTarget_GrSurfaceFlag;
817 desc.fOrigin = origin;
818 desc.fWidth = width;
819 desc.fHeight = height;
820 desc.fConfig = config;
821 desc.fSampleCnt = sampleCnt;
822
Brian Osman32342f02017-03-04 08:12:46 -0500823 sk_sp<GrTextureProxy> rtp = GrSurfaceProxy::MakeDeferred(this->resourceProvider(),
Robert Phillips26c90e02017-03-14 14:39:29 -0400824 desc, fit, budgeted);
Robert Phillips08c5ec72017-01-30 12:26:47 -0500825 if (!rtp) {
826 return nullptr;
827 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400828
Robert Phillips1119dc32017-04-11 12:54:57 -0400829 sk_sp<GrRenderTargetContext> renderTargetContext(
830 fDrawingManager->makeRenderTargetContext(std::move(rtp),
831 std::move(colorSpace),
832 surfaceProps));
Robert Phillips1119dc32017-04-11 12:54:57 -0400833 if (!renderTargetContext) {
834 return nullptr;
835 }
836
837 renderTargetContext->discard();
838
839 return renderTargetContext;
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400840}
841
joshualitt1de610a2016-01-06 08:26:09 -0800842bool GrContext::abandoned() const {
843 ASSERT_SINGLE_OWNER
robertphillips7761d612016-05-16 09:14:53 -0700844 return fDrawingManager->wasAbandoned();
robertphillips77a2e522015-10-17 07:43:27 -0700845}
846
Brian Osman60cd57e2017-04-06 10:19:06 -0400847sk_sp<GrFragmentProcessor> GrContext::createPMToUPMEffect(sk_sp<GrFragmentProcessor> fp,
Brian Osman409e74f2017-04-17 11:48:28 -0400848 bool useConfigConversionEffect) {
Robert Phillips757914d2017-01-25 15:48:30 -0500849 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -0400850 // We have specialized effects that guarantee round-trip conversion for some formats
851 if (useConfigConversionEffect) {
852 // We should have already called this->validPMUPMConversionExists() in this case
853 SkASSERT(fDidTestPMConversions);
854 // ...and it should have succeeded
855 SkASSERT(this->validPMUPMConversionExists());
856
Brian Osman28804f32017-04-20 10:24:36 -0400857 return GrConfigConversionEffect::Make(std::move(fp),
858 GrConfigConversionEffect::kToUnpremul_PMConversion);
Brian Osman2d2da4f2017-04-12 17:07:22 -0400859 } else {
860 // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and
861 // explicitly round the results. Just do the obvious, naive thing in the shader.
862 return GrFragmentProcessor::UnpremulOutput(std::move(fp));
Robert Phillips757914d2017-01-25 15:48:30 -0500863 }
864}
865
Brian Osman60cd57e2017-04-06 10:19:06 -0400866sk_sp<GrFragmentProcessor> GrContext::createUPMToPMEffect(sk_sp<GrFragmentProcessor> fp,
Brian Osman409e74f2017-04-17 11:48:28 -0400867 bool useConfigConversionEffect) {
joshualitt1de610a2016-01-06 08:26:09 -0800868 ASSERT_SINGLE_OWNER
Brian Osman2d2da4f2017-04-12 17:07:22 -0400869 // We have specialized effects that guarantee round-trip conversion for these formats
Brian Osman409e74f2017-04-17 11:48:28 -0400870 if (useConfigConversionEffect) {
871 // We should have already called this->validPMUPMConversionExists() in this case
872 SkASSERT(fDidTestPMConversions);
873 // ...and it should have succeeded
874 SkASSERT(this->validPMUPMConversionExists());
875
Brian Osman28804f32017-04-20 10:24:36 -0400876 return GrConfigConversionEffect::Make(std::move(fp),
877 GrConfigConversionEffect::kToPremul_PMConversion);
Brian Osman2d2da4f2017-04-12 17:07:22 -0400878 } else {
879 // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and
880 // explicitly round the results. Just do the obvious, naive thing in the shader.
881 return GrFragmentProcessor::PremulOutput(std::move(fp));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000882 }
883}
884
Brian Osman409e74f2017-04-17 11:48:28 -0400885bool GrContext::validPMUPMConversionExists() {
joshualitt1de610a2016-01-06 08:26:09 -0800886 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -0400887 if (!fDidTestPMConversions) {
Brian Osman28804f32017-04-20 10:24:36 -0400888 fPMUPMConversionsRoundTrip = GrConfigConversionEffect::TestForPreservingPMConversions(this);
Brian Osman409e74f2017-04-17 11:48:28 -0400889 fDidTestPMConversions = true;
890 }
891
bsalomon636e8022015-07-29 06:08:46 -0700892 // The PM<->UPM tests fail or succeed together so we only need to check one.
Brian Osman28804f32017-04-20 10:24:36 -0400893 return fPMUPMConversionsRoundTrip;
bsalomon636e8022015-07-29 06:08:46 -0700894}
895
bsalomon37f9a262015-02-02 13:00:10 -0800896//////////////////////////////////////////////////////////////////////////////
897
898void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800899 ASSERT_SINGLE_OWNER
bsalomon37f9a262015-02-02 13:00:10 -0800900 if (maxTextures) {
bsalomon0ea80f42015-02-11 10:49:59 -0800901 *maxTextures = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -0800902 }
903 if (maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800904 *maxTextureBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -0800905 }
906}
907
908void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
joshualitt1de610a2016-01-06 08:26:09 -0800909 ASSERT_SINGLE_OWNER
bsalomon0ea80f42015-02-11 10:49:59 -0800910 fResourceCache->setLimits(maxTextures, maxTextureBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800911}
912
ericrk0a5fa482015-09-15 14:16:10 -0700913//////////////////////////////////////////////////////////////////////////////
914
915void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -0800916 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -0700917 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
918}