blob: 1455800cbdaf2f016d43f6b0519b8e2f8c6eaba0 [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"
Brian Osman11052242016-10-27 14:47:55 -040013#include "GrRenderTargetContext.h"
Brian Salomonc65aec92017-03-09 09:03:58 -050014#include "GrRenderTargetProxy.h"
bsalomon0ea80f42015-02-11 10:49:59 -080015#include "GrResourceCache.h"
bsalomond309e7a2015-04-30 14:18:54 -070016#include "GrResourceProvider.h"
Greg Danield85f97d2017-03-07 13:37:21 -050017#include "GrSemaphore.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000018#include "GrSoftwarePathRenderer.h"
Brian Osman45580d32016-11-23 09:37:01 -050019#include "GrSurfaceContext.h"
bsalomonafbf2d62014-09-30 12:18:44 -070020#include "GrSurfacePriv.h"
Robert Phillips757914d2017-01-25 15:48:30 -050021#include "GrSurfaceProxyPriv.h"
Brian Osman45580d32016-11-23 09:37:01 -050022#include "GrTextureContext.h"
Matt Sarett485c4992017-02-14 14:18:27 -050023#include "SkConvertPixels.h"
Brian Osman3b655982017-03-07 16:58:08 -050024#include "SkGr.h"
Matt Sarettc7b29082017-02-09 16:22:39 -050025#include "SkUnPreMultiplyPriv.h"
joshualitt5478d422014-11-14 16:00:38 -080026#include "effects/GrConfigConversionEffect.h"
joshualitte8042922015-12-11 06:11:21 -080027#include "text/GrTextBlobCache.h"
joshualitt5478d422014-11-14 16:00:38 -080028
Robert Phillipse78b7252017-04-06 07:59:41 -040029#define ASSERT_OWNED_PROXY(P) \
30SkASSERT(!(P) || !((P)->priv().peekTexture()) || (P)->priv().peekTexture()->getContext() == this)
Robert Phillips7ee385e2017-03-30 08:02:11 -040031#define ASSERT_OWNED_PROXY_PRIV(P) \
32SkASSERT(!(P) || !((P)->priv().peekTexture()) || (P)->priv().peekTexture()->getContext() == fContext)
33
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000034#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
joshualitt1de610a2016-01-06 08:26:09 -080035#define ASSERT_SINGLE_OWNER \
36 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fSingleOwner);)
robertphillips4fd74ae2016-08-03 14:26:53 -070037#define ASSERT_SINGLE_OWNER_PRIV \
38 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fContext->fSingleOwner);)
robertphillips7761d612016-05-16 09:14:53 -070039#define RETURN_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return; }
Robert Phillips7ee385e2017-03-30 08:02:11 -040040#define RETURN_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return; }
robertphillips7761d612016-05-16 09:14:53 -070041#define RETURN_FALSE_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return false; }
Robert Phillipse78b7252017-04-06 07:59:41 -040042#define RETURN_FALSE_IF_ABANDONED_PRIV if (fContext->fDrawingManager->wasAbandoned()) { return false; }
robertphillips7761d612016-05-16 09:14:53 -070043#define RETURN_NULL_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return nullptr; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000044
robertphillipsea461502015-05-26 11:38:03 -070045////////////////////////////////////////////////////////////////////////////////
46
bsalomon682c2692015-05-22 14:01:46 -070047GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext) {
48 GrContextOptions defaultOptions;
49 return Create(backend, backendContext, defaultOptions);
50}
bsalomonf28cff72015-05-22 12:25:41 -070051
bsalomon682c2692015-05-22 14:01:46 -070052GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
53 const GrContextOptions& options) {
halcanary385fe4d2015-08-26 13:07:48 -070054 GrContext* context = new GrContext;
bsalomon682c2692015-05-22 14:01:46 -070055
56 if (context->init(backend, backendContext, options)) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000057 return context;
58 } else {
59 context->unref();
halcanary96fcdcc2015-08-27 07:41:13 -070060 return nullptr;
bsalomon@google.com27847de2011-02-22 20:59:41 +000061 }
bsalomon@google.com27847de2011-02-22 20:59:41 +000062}
63
joshualitt0acd0d32015-05-07 08:23:19 -070064static int32_t gNextID = 1;
65static int32_t next_id() {
66 int32_t id;
67 do {
68 id = sk_atomic_inc(&gNextID);
69 } while (id == SK_InvalidGenID);
70 return id;
71}
72
bsalomon682c2692015-05-22 14:01:46 -070073GrContext::GrContext() : fUniqueID(next_id()) {
halcanary96fcdcc2015-08-27 07:41:13 -070074 fGpu = nullptr;
75 fCaps = nullptr;
76 fResourceCache = nullptr;
77 fResourceProvider = nullptr;
Brian Salomonf856fd12016-12-16 14:24:34 -050078 fAtlasGlyphCache = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000079}
80
bsalomon682c2692015-05-22 14:01:46 -070081bool GrContext::init(GrBackend backend, GrBackendContext backendContext,
82 const GrContextOptions& options) {
joshualitt1de610a2016-01-06 08:26:09 -080083 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -070084 SkASSERT(!fGpu);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000085
Greg Danielfc978fd2017-04-13 09:54:12 -040086 fBackend = backend;
87
bsalomon682c2692015-05-22 14:01:46 -070088 fGpu = GrGpu::Create(backend, backendContext, options, this);
robertphillipsea461502015-05-26 11:38:03 -070089 if (!fGpu) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000090 return false;
91 }
bsalomon69cfe952015-11-30 13:27:47 -080092 this->initCommon(options);
bsalomon33435572014-11-05 14:47:41 -080093 return true;
94}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000095
bsalomon69cfe952015-11-30 13:27:47 -080096void GrContext::initCommon(const GrContextOptions& options) {
joshualitt1de610a2016-01-06 08:26:09 -080097 ASSERT_SINGLE_OWNER
98
bsalomon76228632015-05-29 08:02:10 -070099 fCaps = SkRef(fGpu->caps());
Brian Osman13dddce2017-05-09 13:19:50 -0400100 fResourceCache = new GrResourceCache(fCaps, fUniqueID);
joshualitt6d0872d2016-01-11 08:27:48 -0800101 fResourceProvider = new GrResourceProvider(fGpu, fResourceCache, &fSingleOwner);
commit-bot@chromium.org1836d332013-07-16 22:55:03 +0000102
Brian Osman46da1cc2017-02-14 14:15:48 -0500103 fDisableGpuYUVConversion = options.fDisableGpuYUVConversion;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000104 fDidTestPMConversions = false;
105
bsalomon6b2552f2016-09-15 13:50:26 -0700106 GrPathRendererChain::Options prcOptions;
bsalomon39ef7fb2016-09-21 11:16:05 -0700107 prcOptions.fAllowPathMaskCaching = options.fAllowPathMaskCaching;
csmartdalton008b9d82017-02-22 12:00:42 -0700108 prcOptions.fGpuPathRenderers = options.fGpuPathRenderers;
Robert Phillips8185f592017-04-26 08:31:08 -0400109 fDrawingManager.reset(new GrDrawingManager(this, prcOptions,
Robert Phillipsf2361d22016-10-25 14:20:06 -0400110 options.fImmediateMode, &fSingleOwner));
joshualitt7c3a2f82015-03-31 13:32:05 -0700111
Eric Karl6d342282017-05-03 17:08:42 -0700112 fAtlasGlyphCache = new GrAtlasGlyphCache(this, options.fGlyphCacheTextureMaximumBytes);
joshualittb7133be2015-04-08 09:08:31 -0700113
halcanary385fe4d2015-08-26 13:07:48 -0700114 fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB, this));
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000115}
116
bsalomon@google.com27847de2011-02-22 20:59:41 +0000117GrContext::~GrContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800118 ASSERT_SINGLE_OWNER
119
robertphillipsea461502015-05-26 11:38:03 -0700120 if (!fGpu) {
bsalomon76228632015-05-29 08:02:10 -0700121 SkASSERT(!fCaps);
bsalomon@google.com733c0622013-04-24 17:59:32 +0000122 return;
123 }
124
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000125 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000126
robertphillips77a2e522015-10-17 07:43:27 -0700127 fDrawingManager->cleanup();
robertphillips2334fb62015-06-17 05:43:33 -0700128
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000129 for (int i = 0; i < fCleanUpData.count(); ++i) {
130 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
131 }
132
halcanary385fe4d2015-08-26 13:07:48 -0700133 delete fResourceProvider;
134 delete fResourceCache;
Brian Salomonf856fd12016-12-16 14:24:34 -0500135 delete fAtlasGlyphCache;
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000136
bsalomon@google.com205d4602011-04-25 12:43:45 +0000137 fGpu->unref();
bsalomon76228632015-05-29 08:02:10 -0700138 fCaps->unref();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000139}
140
bungeman6bd52842016-10-27 09:30:08 -0700141sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
bsalomon41b952c2016-03-11 06:46:33 -0800142 if (!fThreadSafeProxy) {
bungeman6bd52842016-10-27 09:30:08 -0700143 fThreadSafeProxy.reset(new GrContextThreadSafeProxy(sk_ref_sp(fCaps), this->uniqueID()));
bsalomon41b952c2016-03-11 06:46:33 -0800144 }
bungeman6bd52842016-10-27 09:30:08 -0700145 return fThreadSafeProxy;
bsalomon41b952c2016-03-11 06:46:33 -0800146}
147
bsalomon2354f842014-07-28 13:48:36 -0700148void GrContext::abandonContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800149 ASSERT_SINGLE_OWNER
150
bsalomond309e7a2015-04-30 14:18:54 -0700151 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800152
153 // Need to abandon the drawing manager first so all the render targets
154 // will be released/forgotten before they too are abandoned.
155 fDrawingManager->abandon();
156
bsalomon@google.com205d4602011-04-25 12:43:45 +0000157 // abandon first to so destructors
158 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800159 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700160
bsalomon6e2aad42016-04-01 11:54:31 -0700161 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
162
Brian Salomonf856fd12016-12-16 14:24:34 -0500163 fAtlasGlyphCache->freeAll();
bsalomon6e2aad42016-04-01 11:54:31 -0700164 fTextBlobCache->freeAll();
165}
166
167void GrContext::releaseResourcesAndAbandonContext() {
168 ASSERT_SINGLE_OWNER
169
170 fResourceProvider->abandon();
171
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
176 // Release all resources in the backend 3D API.
177 fResourceCache->releaseAll();
178
179 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000180
Brian Salomonf856fd12016-12-16 14:24:34 -0500181 fAtlasGlyphCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700182 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000183}
184
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000185void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800186 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000187 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000188}
189
190void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800191 ASSERT_SINGLE_OWNER
192
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000193 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000194
Brian Salomonf856fd12016-12-16 14:24:34 -0500195 fAtlasGlyphCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700196
197 fDrawingManager->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700198
199 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000200}
201
Brian Salomon5e150852017-03-22 14:53:13 -0400202void GrContext::purgeResourcesNotUsedInMs(std::chrono::milliseconds ms) {
203 ASSERT_SINGLE_OWNER
204 fResourceCache->purgeResourcesNotUsedSince(GrStdSteadyClock::now() - ms);
205}
206
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000207void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800208 ASSERT_SINGLE_OWNER
209
bsalomon71cb0c22014-11-14 12:10:14 -0800210 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800211 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800212 }
213 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800214 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800215 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000216}
217
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000218////////////////////////////////////////////////////////////////////////////////
219
joshualitt0db6dfa2015-04-10 07:01:30 -0700220void GrContext::TextBlobCacheOverBudgetCB(void* data) {
221 SkASSERT(data);
Brian Osman11052242016-10-27 14:47:55 -0400222 // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on
223 // GrRenderTargetContext to perform a necessary flush. The solution is to move drawText calls
224 // to below the GrContext level, but this is not trivial because they call drawPath on
225 // SkGpuDevice.
joshualitt0db6dfa2015-04-10 07:01:30 -0700226 GrContext* context = reinterpret_cast<GrContext*>(data);
227 context->flush();
228}
229
bsalomon@google.com27847de2011-02-22 20:59:41 +0000230////////////////////////////////////////////////////////////////////////////////
231
bsalomonb77a9072016-09-07 10:02:04 -0700232void GrContext::flush() {
joshualitt1de610a2016-01-06 08:26:09 -0800233 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700234 RETURN_IF_ABANDONED
Robert Phillips7ee385e2017-03-30 08:02:11 -0400235
236 fDrawingManager->flush(nullptr);
237}
238
239void GrContextPriv::flush(GrSurfaceProxy* proxy) {
240 ASSERT_SINGLE_OWNER_PRIV
241 RETURN_IF_ABANDONED_PRIV
242 ASSERT_OWNED_PROXY_PRIV(proxy);
243
244 fContext->fDrawingManager->flush(proxy);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000245}
246
bsalomon81beccc2014-10-13 12:32:55 -0700247bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
248 const void* inPixels, size_t outRowBytes, void* outPixels) {
Matt Sarettc7b29082017-02-09 16:22:39 -0500249 SkColorType colorType;
250 if (!GrPixelConfigToColorType(srcConfig, &colorType) ||
251 4 != SkColorTypeBytesPerPixel(colorType))
252 {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000253 return false;
254 }
bsalomon81beccc2014-10-13 12:32:55 -0700255
Matt Sarettc7b29082017-02-09 16:22:39 -0500256 for (int y = 0; y < height; y++) {
257 SkOpts::RGBA_to_rgbA((uint32_t*) outPixels, inPixels, width);
258 outPixels = SkTAddOffset<void>(outPixels, outRowBytes);
259 inPixels = SkTAddOffset<const void>(inPixels, inRowBytes);
260 }
bsalomon81beccc2014-10-13 12:32:55 -0700261
Matt Sarettc7b29082017-02-09 16:22:39 -0500262 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000263}
264
Brian Osmand2ca59a2017-04-13 14:03:57 -0400265static bool valid_premul_config(GrPixelConfig config) {
Brian Osmance425512017-03-22 14:37:50 -0400266 return GrPixelConfigIs8888Unorm(config) || kRGBA_half_GrPixelConfig == config;
267}
268
Brian Osmand2ca59a2017-04-13 14:03:57 -0400269static bool valid_pixel_conversion(GrPixelConfig srcConfig, GrPixelConfig dstConfig,
270 bool premulConversion) {
271 // We don't allow conversion between integer configs and float/fixed configs.
272 if (GrPixelConfigIsSint(srcConfig) != GrPixelConfigIsSint(dstConfig)) {
273 return false;
274 }
275
276 // We only allow premul <-> unpremul conversions for some formats
277 if (premulConversion && (!valid_premul_config(srcConfig) || !valid_premul_config(dstConfig))) {
278 return false;
279 }
280
281 return true;
282}
283
Brian Osman409e74f2017-04-17 11:48:28 -0400284static bool pm_upm_must_round_trip(GrPixelConfig config, SkColorSpace* colorSpace) {
285 return !colorSpace &&
286 (kRGBA_8888_GrPixelConfig == config || kBGRA_8888_GrPixelConfig == config);
287}
288
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400289bool GrContextPriv::writeSurfacePixels(GrSurfaceContext* dst,
Robert Phillipse78b7252017-04-06 07:59:41 -0400290 int left, int top, int width, int height,
291 GrPixelConfig srcConfig, SkColorSpace* srcColorSpace,
292 const void* buffer, size_t rowBytes,
293 uint32_t pixelOpsFlags) {
Brian Osmanb62ea222016-12-22 11:12:16 -0500294 // TODO: Color space conversion
295
Robert Phillipse78b7252017-04-06 07:59:41 -0400296 ASSERT_SINGLE_OWNER_PRIV
297 RETURN_FALSE_IF_ABANDONED_PRIV
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400298 SkASSERT(dst);
299 ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy());
Robert Phillipse78b7252017-04-06 07:59:41 -0400300 GR_AUDIT_TRAIL_AUTO_FRAME(&fContext->fAuditTrail, "GrContextPriv::writeSurfacePixels");
bsalomon6c6f6582015-09-10 08:12:46 -0700301
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400302 GrSurface* dstSurface = dst->asSurfaceProxy()->instantiate(fContext->resourceProvider());
303 if (!dstSurface) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400304 return false;
305 }
306
Brian Osmand2ca59a2017-04-13 14:03:57 -0400307 // 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 -0400308 const bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400309 if (!valid_pixel_conversion(srcConfig, dstSurface->config(), premul)) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400310 return false;
311 }
312
Brian Osman409e74f2017-04-17 11:48:28 -0400313 // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data,
314 // without any color spaces attached, and the caller wants us to premul.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400315 bool useConfigConversionEffect =
316 premul &&
317 pm_upm_must_round_trip(srcConfig, srcColorSpace) &&
318 pm_upm_must_round_trip(dstSurface->config(), dst->getColorSpace());
Brian Osman409e74f2017-04-17 11:48:28 -0400319
320 // Are we going to try to premul as part of a draw? For the non-legacy case, we always allow
321 // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly.
322 bool premulOnGpu = premul &&
323 (!useConfigConversionEffect || fContext->validPMUPMConversionExists());
bsalomon81beccc2014-10-13 12:32:55 -0700324
bsalomone8d21e82015-07-16 08:23:13 -0700325 // 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 -0700326 // necessary and because GrGpu::getWritePixelsInfo requires it.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400327 if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(),
bsalomone8d21e82015-07-16 08:23:13 -0700328 GrBytesPerPixel(srcConfig), &left, &top, &width,
329 &height, &buffer, &rowBytes)) {
330 return false;
331 }
332
Brian Osman409e74f2017-04-17 11:48:28 -0400333 GrGpu::DrawPreference drawPreference = premulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference
334 : GrGpu::kNoDraw_DrawPreference;
bsalomonf0674512015-07-28 13:26:15 -0700335 GrGpu::WritePixelTempDrawInfo tempDrawInfo;
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400336 if (!fContext->fGpu->getWritePixelsInfo(dstSurface, width, height, srcConfig,
Robert Phillipse78b7252017-04-06 07:59:41 -0400337 &drawPreference, &tempDrawInfo)) {
bsalomonf0674512015-07-28 13:26:15 -0700338 return false;
339 }
340
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400341 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && dstSurface->surfacePriv().hasPendingIO()) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400342 this->flush(nullptr); // MDB TODO: tighten this
bsalomonf0674512015-07-28 13:26:15 -0700343 }
344
Robert Phillips2f493142017-03-02 18:18:38 -0500345 sk_sp<GrTextureProxy> tempProxy;
bsalomonf0674512015-07-28 13:26:15 -0700346 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400347 tempProxy = GrSurfaceProxy::MakeDeferred(fContext->resourceProvider(),
Robert Phillips2f493142017-03-02 18:18:38 -0500348 tempDrawInfo.fTempSurfaceDesc,
349 SkBackingFit::kApprox,
350 SkBudgeted::kYes);
351 if (!tempProxy && GrGpu::kRequireDraw_DrawPreference == drawPreference) {
bsalomonf0674512015-07-28 13:26:15 -0700352 return false;
353 }
354 }
355
356 // temp buffer for doing sw premul conversion, if needed.
357 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
Brian Osman409e74f2017-04-17 11:48:28 -0400358 // We need to do sw premul if we were unable to create a RT for drawing, or if we can't do the
359 // premul on the GPU
360 if (premul && (!tempProxy || !premulOnGpu)) {
361 size_t tmpRowBytes = 4 * width;
362 tmpPixels.reset(width * height);
363 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
364 tmpPixels.get())) {
365 return false;
bsalomonf0674512015-07-28 13:26:15 -0700366 }
Brian Osman409e74f2017-04-17 11:48:28 -0400367 rowBytes = tmpRowBytes;
368 buffer = tmpPixels.get();
bsalomonf0674512015-07-28 13:26:15 -0700369 }
Brian Osman409e74f2017-04-17 11:48:28 -0400370
371 if (tempProxy) {
372 sk_sp<GrFragmentProcessor> fp = GrSimpleTextureEffect::Make(
373 fContext->resourceProvider(), tempProxy, nullptr, SkMatrix::I());
374 if (premulOnGpu) {
375 fp = fContext->createUPMToPMEffect(std::move(fp), useConfigConversionEffect);
bsalomon81beccc2014-10-13 12:32:55 -0700376 }
Brian Osman409e74f2017-04-17 11:48:28 -0400377 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle);
378 SkASSERT(fp);
379
380 if (tempProxy->priv().hasPendingIO()) {
381 this->flush(tempProxy.get());
382 }
Brian Salomonbb5711a2017-05-17 13:49:59 -0400383 GrTexture* texture = tempProxy->instantiateTexture(fContext->resourceProvider());
Brian Osman409e74f2017-04-17 11:48:28 -0400384 if (!texture) {
385 return false;
386 }
387 if (!fContext->fGpu->writePixels(texture, 0, 0, width, height, tempDrawInfo.fWriteConfig,
388 buffer, rowBytes)) {
389 return false;
390 }
391 SkMatrix matrix;
392 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400393 GrRenderTargetContext* renderTargetContext = dst->asRenderTargetContext();
Brian Osman409e74f2017-04-17 11:48:28 -0400394 if (!renderTargetContext) {
395 return false;
396 }
397 GrPaint paint;
398 paint.addColorFragmentProcessor(std::move(fp));
399 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Osman7ab6a7f2017-04-25 15:01:46 -0400400 paint.setAllowSRGBInputs(SkToBool(dst->getColorSpace()) ||
401 GrPixelConfigIsSRGB(renderTargetContext->config()));
Brian Osman409e74f2017-04-17 11:48:28 -0400402 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
403 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, matrix, rect,
404 nullptr);
405
406 if (kFlushWrites_PixelOp & pixelOpsFlags) {
407 this->flushSurfaceWrites(renderTargetContext->asRenderTargetProxy());
408 }
409 } else {
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400410 return fContext->fGpu->writePixels(dstSurface, left, top, width, height, srcConfig,
Robert Phillipse78b7252017-04-06 07:59:41 -0400411 buffer, rowBytes);
bsalomon81beccc2014-10-13 12:32:55 -0700412 }
bsalomon81beccc2014-10-13 12:32:55 -0700413 return true;
414}
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000415
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400416bool GrContextPriv::readSurfacePixels(GrSurfaceContext* src,
Robert Phillipse78b7252017-04-06 07:59:41 -0400417 int left, int top, int width, int height,
418 GrPixelConfig dstConfig, SkColorSpace* dstColorSpace,
419 void* buffer, size_t rowBytes, uint32_t flags) {
Brian Osmanb62ea222016-12-22 11:12:16 -0500420 // TODO: Color space conversion
421
Robert Phillipse78b7252017-04-06 07:59:41 -0400422 ASSERT_SINGLE_OWNER_PRIV
423 RETURN_FALSE_IF_ABANDONED_PRIV
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400424 SkASSERT(src);
425 ASSERT_OWNED_PROXY_PRIV(src->asSurfaceProxy());
Robert Phillipse78b7252017-04-06 07:59:41 -0400426 GR_AUDIT_TRAIL_AUTO_FRAME(&fContext->fAuditTrail, "GrContextPriv::readSurfacePixels");
bsalomon32ab2602015-09-09 18:57:49 -0700427
Robert Phillipse78b7252017-04-06 07:59:41 -0400428 // MDB TODO: delay this instantiation until later in the method
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400429 GrSurface* srcSurface = src->asSurfaceProxy()->instantiate(fContext->resourceProvider());
430 if (!srcSurface) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400431 return false;
432 }
433
Brian Osmand2ca59a2017-04-13 14:03:57 -0400434 // The src is premul but the dst is unpremul -> unpremul the src after or as part of the read
435 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400436 if (!valid_pixel_conversion(srcSurface->config(), dstConfig, unpremul)) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400437 return false;
438 }
439
Brian Osman409e74f2017-04-17 11:48:28 -0400440 // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data,
441 // without any color spaces attached, and the caller wants us to unpremul.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400442 bool useConfigConversionEffect =
443 unpremul &&
444 pm_upm_must_round_trip(srcSurface->config(), src->getColorSpace()) &&
445 pm_upm_must_round_trip(dstConfig, dstColorSpace);
Brian Osman409e74f2017-04-17 11:48:28 -0400446
447 // Are we going to try to unpremul as part of a draw? For the non-legacy case, we always allow
448 // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly.
449 bool unpremulOnGpu = unpremul &&
450 (!useConfigConversionEffect || fContext->validPMUPMConversionExists());
bsalomon6c6f6582015-09-10 08:12:46 -0700451
bsalomone8d21e82015-07-16 08:23:13 -0700452 // Adjust the params so that if we wind up using an intermediate surface we've already done
453 // all the trimming and the temporary can be the min size required.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400454 if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(),
bsalomone8d21e82015-07-16 08:23:13 -0700455 GrBytesPerPixel(dstConfig), &left,
456 &top, &width, &height, &buffer, &rowBytes)) {
457 return false;
458 }
459
Brian Osman409e74f2017-04-17 11:48:28 -0400460 GrGpu::DrawPreference drawPreference = unpremulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference
461 : GrGpu::kNoDraw_DrawPreference;
bsalomon39826022015-07-23 08:07:21 -0700462 GrGpu::ReadPixelTempDrawInfo tempDrawInfo;
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400463 if (!fContext->fGpu->getReadPixelsInfo(srcSurface, width, height, rowBytes, dstConfig,
Robert Phillipse78b7252017-04-06 07:59:41 -0400464 &drawPreference, &tempDrawInfo)) {
bsalomon39826022015-07-23 08:07:21 -0700465 return false;
466 }
bsalomon191bcc02014-11-14 11:31:13 -0800467
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400468 if (!(kDontFlush_PixelOpsFlag & flags) && srcSurface->surfacePriv().hasPendingWrite()) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400469 this->flush(nullptr); // MDB TODO: tighten this
470 }
471
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400472 sk_sp<GrSurfaceProxy> proxyToRead = src->asSurfaceProxyRef();
bsalomon39826022015-07-23 08:07:21 -0700473 bool didTempDraw = false;
474 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomonb117ff12016-07-19 07:24:40 -0700475 if (SkBackingFit::kExact == tempDrawInfo.fTempSurfaceFit) {
bsalomon39826022015-07-23 08:07:21 -0700476 // We only respect this when the entire src is being read. Otherwise we can trigger too
477 // many odd ball texture sizes and trash the cache.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400478 if (width != srcSurface->width() || height != srcSurface->height()) {
bsalomonb117ff12016-07-19 07:24:40 -0700479 tempDrawInfo.fTempSurfaceFit= SkBackingFit::kApprox;
bsalomon39826022015-07-23 08:07:21 -0700480 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000481 }
brianosmandfe4f2e2016-07-21 13:28:36 -0700482 // TODO: Need to decide the semantics of this function for color spaces. Do we support
483 // conversion to a passed-in color space? For now, specifying nullptr means that this
484 // path will do no conversion, so it will match the behavior of the non-draw path.
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400485 sk_sp<GrRenderTargetContext> tempRTC = fContext->makeDeferredRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400486 tempDrawInfo.fTempSurfaceFit,
bsalomonb117ff12016-07-19 07:24:40 -0700487 tempDrawInfo.fTempSurfaceDesc.fWidth,
488 tempDrawInfo.fTempSurfaceDesc.fHeight,
489 tempDrawInfo.fTempSurfaceDesc.fConfig,
brianosmandfe4f2e2016-07-21 13:28:36 -0700490 nullptr,
bsalomonb117ff12016-07-19 07:24:40 -0700491 tempDrawInfo.fTempSurfaceDesc.fSampleCnt,
492 tempDrawInfo.fTempSurfaceDesc.fOrigin);
Brian Osman693a5402016-10-27 15:13:22 -0400493 if (tempRTC) {
Robert Phillips67c18d62017-01-20 12:44:06 -0500494 SkMatrix textureMatrix = SkMatrix::MakeTrans(SkIntToScalar(left), SkIntToScalar(top));
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400495 sk_sp<GrTextureProxy> proxy = src->asTextureProxyRef();
Brian Osman409e74f2017-04-17 11:48:28 -0400496 sk_sp<GrFragmentProcessor> fp = GrSimpleTextureEffect::Make(
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400497 fContext->resourceProvider(), std::move(proxy), nullptr, textureMatrix);
Brian Osman409e74f2017-04-17 11:48:28 -0400498 if (unpremulOnGpu) {
499 fp = fContext->createPMToUPMEffect(std::move(fp), useConfigConversionEffect);
500 // We no longer need to do this on CPU after the read back.
501 unpremul = false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000502 }
Brian Osman409e74f2017-04-17 11:48:28 -0400503 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle);
504 SkASSERT(fp);
Brian Osman60cd57e2017-04-06 10:19:06 -0400505
Brian Osman409e74f2017-04-17 11:48:28 -0400506 GrPaint paint;
507 paint.addColorFragmentProcessor(std::move(fp));
508 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
509 paint.setAllowSRGBInputs(true);
510 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
511 tempRTC->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect,
512 nullptr);
513 proxyToRead = tempRTC->asTextureProxyRef();
514 left = 0;
515 top = 0;
516 didTempDraw = true;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000517 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000518 }
joshualitt5c55fef2014-10-31 14:04:35 -0700519
Robert Phillipse78b7252017-04-06 07:59:41 -0400520 if (!proxyToRead) {
521 return false;
522 }
523
524 GrSurface* surfaceToRead = proxyToRead->instantiate(fContext->resourceProvider());
Robert Phillips833dcf42016-11-18 08:44:13 -0500525 if (!surfaceToRead) {
526 return false;
527 }
528
bsalomon39826022015-07-23 08:07:21 -0700529 if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000530 return false;
531 }
bsalomon39826022015-07-23 08:07:21 -0700532 GrPixelConfig configToRead = dstConfig;
533 if (didTempDraw) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400534 this->flushSurfaceWrites(proxyToRead.get());
bsalomon6c9cd552016-01-22 07:17:34 -0800535 configToRead = tempDrawInfo.fReadConfig;
bsalomon39826022015-07-23 08:07:21 -0700536 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400537 if (!fContext->fGpu->readPixels(surfaceToRead, left, top, width, height, configToRead,
538 buffer, rowBytes)) {
bsalomon39826022015-07-23 08:07:21 -0700539 return false;
540 }
541
542 // Perform umpremul conversion if we weren't able to perform it as a draw.
543 if (unpremul) {
Matt Sarettc7b29082017-02-09 16:22:39 -0500544 SkColorType colorType;
545 if (!GrPixelConfigToColorType(dstConfig, &colorType) ||
546 4 != SkColorTypeBytesPerPixel(colorType))
547 {
reed@google.com7111d462014-03-25 16:20:24 +0000548 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000549 }
reed@google.com7111d462014-03-25 16:20:24 +0000550
Matt Sarettc7b29082017-02-09 16:22:39 -0500551 for (int y = 0; y < height; y++) {
552 SkUnpremultiplyRow<false>((uint32_t*) buffer, (const uint32_t*) buffer, width);
553 buffer = SkTAddOffset<void>(buffer, rowBytes);
554 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000555 }
556 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000557}
558
Robert Phillips7ee385e2017-03-30 08:02:11 -0400559void GrContextPriv::prepareSurfaceForExternalIO(GrSurfaceProxy* proxy) {
560 ASSERT_SINGLE_OWNER_PRIV
561 RETURN_IF_ABANDONED_PRIV
562 SkASSERT(proxy);
563 ASSERT_OWNED_PROXY_PRIV(proxy);
564 fContext->fDrawingManager->prepareSurfaceForExternalIO(proxy);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000565}
566
Robert Phillips7ee385e2017-03-30 08:02:11 -0400567void GrContextPriv::flushSurfaceWrites(GrSurfaceProxy* proxy) {
568 ASSERT_SINGLE_OWNER_PRIV
569 RETURN_IF_ABANDONED_PRIV
570 SkASSERT(proxy);
571 ASSERT_OWNED_PROXY_PRIV(proxy);
572 if (proxy->priv().hasPendingWrite()) {
573 this->flush(proxy);
bsalomonf80bfed2014-10-07 05:56:02 -0700574 }
575}
576
Robert Phillips7ee385e2017-03-30 08:02:11 -0400577void GrContextPriv::flushSurfaceIO(GrSurfaceProxy* proxy) {
578 ASSERT_SINGLE_OWNER_PRIV
579 RETURN_IF_ABANDONED_PRIV
580 SkASSERT(proxy);
581 ASSERT_OWNED_PROXY_PRIV(proxy);
582 if (proxy->priv().hasPendingIO()) {
583 this->flush(proxy);
ajuma95243eb2016-08-24 08:19:02 -0700584 }
585}
586
bsalomon@google.com27847de2011-02-22 20:59:41 +0000587////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000588int GrContext::getRecommendedSampleCount(GrPixelConfig config,
589 SkScalar dpi) const {
joshualitt1de610a2016-01-06 08:26:09 -0800590 ASSERT_SINGLE_OWNER
591
bsalomon76228632015-05-29 08:02:10 -0700592 if (!this->caps()->isConfigRenderable(config, true)) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000593 return 0;
594 }
595 int chosenSampleCount = 0;
jvanverthe9c0fc62015-04-29 11:18:05 -0700596 if (fGpu->caps()->shaderCaps()->pathRenderingSupport()) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000597 if (dpi >= 250.0f) {
598 chosenSampleCount = 4;
599 } else {
600 chosenSampleCount = 16;
601 }
602 }
egdanieleed519e2016-01-15 11:36:18 -0800603 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ? chosenSampleCount : 0;
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000604}
605
Brian Osman11052242016-10-27 14:47:55 -0400606sk_sp<GrRenderTargetContext> GrContextPriv::makeWrappedRenderTargetContext(
607 sk_sp<GrRenderTarget> rt,
608 sk_sp<SkColorSpace> colorSpace,
609 const SkSurfaceProps* surfaceProps) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700610 ASSERT_SINGLE_OWNER_PRIV
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400611
Robert Phillips37430132016-11-09 06:50:43 -0500612 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(rt)));
Robert Phillips77b3f322017-01-31 18:24:12 -0500613 if (!proxy) {
614 return nullptr;
615 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400616
Robert Phillips37430132016-11-09 06:50:43 -0500617 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400618 std::move(colorSpace),
Brian Osman11052242016-10-27 14:47:55 -0400619 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -0700620}
robertphillips77a2e522015-10-17 07:43:27 -0700621
Robert Phillips2c862492017-01-18 10:08:39 -0500622sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy> proxy,
623 sk_sp<SkColorSpace> colorSpace) {
Brian Osman45580d32016-11-23 09:37:01 -0500624 ASSERT_SINGLE_OWNER_PRIV
625
Brian Osman45580d32016-11-23 09:37:01 -0500626 if (proxy->asRenderTargetProxy()) {
Robert Phillips2c862492017-01-18 10:08:39 -0500627 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
628 std::move(colorSpace), nullptr);
Brian Osman45580d32016-11-23 09:37:01 -0500629 } else {
630 SkASSERT(proxy->asTextureProxy());
Robert Phillips2c862492017-01-18 10:08:39 -0500631 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Brian Osman45580d32016-11-23 09:37:01 -0500632 }
633}
634
Robert Phillips31c26082016-12-14 15:12:15 -0500635sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurface> surface) {
636 ASSERT_SINGLE_OWNER_PRIV
637
638 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface)));
Robert Phillips77b3f322017-01-31 18:24:12 -0500639 if (!proxy) {
640 return nullptr;
641 }
Robert Phillips31c26082016-12-14 15:12:15 -0500642
Robert Phillips2c862492017-01-18 10:08:39 -0500643 return this->makeWrappedSurfaceContext(std::move(proxy), nullptr);
Robert Phillips31c26082016-12-14 15:12:15 -0500644}
645
Robert Phillipse2f7d182016-12-15 09:23:05 -0500646sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(const GrSurfaceDesc& dstDesc,
647 SkBackingFit fit,
648 SkBudgeted isDstBudgeted) {
649
Brian Osman32342f02017-03-04 08:12:46 -0500650 sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeDeferred(fContext->resourceProvider(),
Robert Phillips26c90e02017-03-14 14:39:29 -0400651 dstDesc, fit, isDstBudgeted);
Robert Phillips77b3f322017-01-31 18:24:12 -0500652 if (!proxy) {
653 return nullptr;
654 }
Robert Phillipse2f7d182016-12-15 09:23:05 -0500655
Robert Phillips2c862492017-01-18 10:08:39 -0500656 return this->makeWrappedSurfaceContext(std::move(proxy), nullptr);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500657}
658
Greg Daniel7ef28f32017-04-20 16:41:55 +0000659sk_sp<GrSurfaceContext> GrContextPriv::makeBackendSurfaceContext(const GrBackendTexture& tex,
660 GrSurfaceOrigin origin,
661 GrBackendTextureFlags flags,
662 int sampleCnt,
Brian Osmanc1e37052017-03-09 14:19:20 -0500663 sk_sp<SkColorSpace> colorSpace) {
Robert Phillips26caf892017-01-27 10:58:31 -0500664 ASSERT_SINGLE_OWNER_PRIV
665
Greg Daniel7ef28f32017-04-20 16:41:55 +0000666 sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTexture(tex, origin,
667 flags, sampleCnt));
Robert Phillips26caf892017-01-27 10:58:31 -0500668 if (!surface) {
669 return nullptr;
670 }
671
672 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface)));
Robert Phillips77b3f322017-01-31 18:24:12 -0500673 if (!proxy) {
674 return nullptr;
675 }
Robert Phillips26caf892017-01-27 10:58:31 -0500676
677 return this->makeWrappedSurfaceContext(std::move(proxy), std::move(colorSpace));
678}
679
Brian Osman11052242016-10-27 14:47:55 -0400680sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000681 const GrBackendTexture& tex,
682 GrSurfaceOrigin origin,
683 int sampleCnt,
Brian Osman11052242016-10-27 14:47:55 -0400684 sk_sp<SkColorSpace> colorSpace,
Brian Osmanc1e37052017-03-09 14:19:20 -0500685 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700686 ASSERT_SINGLE_OWNER_PRIV
robertphillips4fd74ae2016-08-03 14:26:53 -0700687
Greg Daniel7ef28f32017-04-20 16:41:55 +0000688 static const GrBackendTextureFlags kForceRT = kRenderTarget_GrBackendTextureFlag;
689 sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTexture(tex, origin, kForceRT,
690 sampleCnt));
robertphillips4fd74ae2016-08-03 14:26:53 -0700691 if (!surface) {
692 return nullptr;
693 }
694
Robert Phillips37430132016-11-09 06:50:43 -0500695 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface)));
Robert Phillips77b3f322017-01-31 18:24:12 -0500696 if (!proxy) {
697 return nullptr;
698 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400699
Robert Phillips37430132016-11-09 06:50:43 -0500700 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Brian Osman11052242016-10-27 14:47:55 -0400701 std::move(colorSpace), props);
robertphillips4fd74ae2016-08-03 14:26:53 -0700702}
703
Brian Osman11052242016-10-27 14:47:55 -0400704sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext(
Greg Danielbcf612b2017-05-01 13:50:58 +0000705 const GrBackendRenderTarget& backendRT,
706 GrSurfaceOrigin origin,
robertphillips4fd74ae2016-08-03 14:26:53 -0700707 sk_sp<SkColorSpace> colorSpace,
708 const SkSurfaceProps* surfaceProps) {
709 ASSERT_SINGLE_OWNER_PRIV
710
Greg Danielbcf612b2017-05-01 13:50:58 +0000711 sk_sp<GrRenderTarget> rt(fContext->resourceProvider()->wrapBackendRenderTarget(backendRT,
712 origin));
robertphillips4fd74ae2016-08-03 14:26:53 -0700713 if (!rt) {
714 return nullptr;
715 }
716
Robert Phillips37430132016-11-09 06:50:43 -0500717 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(rt)));
Robert Phillips77b3f322017-01-31 18:24:12 -0500718 if (!proxy) {
719 return nullptr;
720 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400721
Robert Phillips37430132016-11-09 06:50:43 -0500722 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400723 std::move(colorSpace),
Brian Osman11052242016-10-27 14:47:55 -0400724 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -0700725}
726
Brian Osman11052242016-10-27 14:47:55 -0400727sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000728 const GrBackendTexture& tex,
729 GrSurfaceOrigin origin,
730 int sampleCnt,
robertphillips4fd74ae2016-08-03 14:26:53 -0700731 sk_sp<SkColorSpace> colorSpace,
732 const SkSurfaceProps* surfaceProps) {
733 ASSERT_SINGLE_OWNER_PRIV
robertphillips4fd74ae2016-08-03 14:26:53 -0700734
Greg Daniel7ef28f32017-04-20 16:41:55 +0000735 sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTextureAsRenderTarget(
736 tex,
737 origin,
738 sampleCnt));
robertphillips4fd74ae2016-08-03 14:26:53 -0700739 if (!surface) {
740 return nullptr;
741 }
742
Robert Phillips37430132016-11-09 06:50:43 -0500743 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface)));
Robert Phillips77b3f322017-01-31 18:24:12 -0500744 if (!proxy) {
745 return nullptr;
746 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400747
Robert Phillips37430132016-11-09 06:50:43 -0500748 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400749 std::move(colorSpace),
750 surfaceProps);
robertphillips77a2e522015-10-17 07:43:27 -0700751}
752
Chris Daltonfe199b72017-05-05 11:26:15 -0400753void GrContextPriv::addOnFlushCallbackObject(GrOnFlushCallbackObject* onFlushCBObject) {
754 fContext->fDrawingManager->addOnFlushCallbackObject(onFlushCBObject);
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400755}
756
757
robertphillips48fde9c2016-09-06 05:20:20 -0700758static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) {
Brian Osman78f20e02017-01-12 10:28:01 -0500759 switch (config) {
760 case kAlpha_8_GrPixelConfig:
761 case kRGB_565_GrPixelConfig:
762 case kRGBA_4444_GrPixelConfig:
763 case kBGRA_8888_GrPixelConfig:
764 return kRGBA_8888_GrPixelConfig;
765 case kSBGRA_8888_GrPixelConfig:
766 return kSRGBA_8888_GrPixelConfig;
767 case kAlpha_half_GrPixelConfig:
768 return kRGBA_half_GrPixelConfig;
769 default:
770 return kUnknown_GrPixelConfig;
771 }
robertphillips48fde9c2016-09-06 05:20:20 -0700772}
773
robertphillipsd728f0c2016-11-21 11:05:03 -0800774sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContextWithFallback(
775 SkBackingFit fit,
776 int width, int height,
777 GrPixelConfig config,
778 sk_sp<SkColorSpace> colorSpace,
779 int sampleCnt,
780 GrSurfaceOrigin origin,
781 const SkSurfaceProps* surfaceProps,
782 SkBudgeted budgeted) {
783 if (!this->caps()->isConfigRenderable(config, sampleCnt > 0)) {
784 config = GrPixelConfigFallback(config);
785 }
786
787 return this->makeDeferredRenderTargetContext(fit, width, height, config, std::move(colorSpace),
788 sampleCnt, origin, surfaceProps, budgeted);
789}
790
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400791sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContext(
792 SkBackingFit fit,
793 int width, int height,
794 GrPixelConfig config,
795 sk_sp<SkColorSpace> colorSpace,
796 int sampleCnt,
797 GrSurfaceOrigin origin,
798 const SkSurfaceProps* surfaceProps,
799 SkBudgeted budgeted) {
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400800 SkASSERT(kDefault_GrSurfaceOrigin != origin);
801
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400802 GrSurfaceDesc desc;
803 desc.fFlags = kRenderTarget_GrSurfaceFlag;
804 desc.fOrigin = origin;
805 desc.fWidth = width;
806 desc.fHeight = height;
807 desc.fConfig = config;
808 desc.fSampleCnt = sampleCnt;
809
Brian Osman32342f02017-03-04 08:12:46 -0500810 sk_sp<GrTextureProxy> rtp = GrSurfaceProxy::MakeDeferred(this->resourceProvider(),
Robert Phillips26c90e02017-03-14 14:39:29 -0400811 desc, fit, budgeted);
Robert Phillips08c5ec72017-01-30 12:26:47 -0500812 if (!rtp) {
813 return nullptr;
814 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400815
Robert Phillips1119dc32017-04-11 12:54:57 -0400816 sk_sp<GrRenderTargetContext> renderTargetContext(
817 fDrawingManager->makeRenderTargetContext(std::move(rtp),
818 std::move(colorSpace),
819 surfaceProps));
820
821 if (!renderTargetContext) {
822 return nullptr;
823 }
824
825 renderTargetContext->discard();
826
827 return renderTargetContext;
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400828}
829
joshualitt1de610a2016-01-06 08:26:09 -0800830bool GrContext::abandoned() const {
831 ASSERT_SINGLE_OWNER
robertphillips7761d612016-05-16 09:14:53 -0700832 return fDrawingManager->wasAbandoned();
robertphillips77a2e522015-10-17 07:43:27 -0700833}
834
Brian Osman60cd57e2017-04-06 10:19:06 -0400835sk_sp<GrFragmentProcessor> GrContext::createPMToUPMEffect(sk_sp<GrFragmentProcessor> fp,
Brian Osman409e74f2017-04-17 11:48:28 -0400836 bool useConfigConversionEffect) {
Robert Phillips757914d2017-01-25 15:48:30 -0500837 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -0400838 // We have specialized effects that guarantee round-trip conversion for some formats
839 if (useConfigConversionEffect) {
840 // We should have already called this->validPMUPMConversionExists() in this case
841 SkASSERT(fDidTestPMConversions);
842 // ...and it should have succeeded
843 SkASSERT(this->validPMUPMConversionExists());
844
Brian Osman28804f32017-04-20 10:24:36 -0400845 return GrConfigConversionEffect::Make(std::move(fp),
846 GrConfigConversionEffect::kToUnpremul_PMConversion);
Brian Osman2d2da4f2017-04-12 17:07:22 -0400847 } else {
848 // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and
849 // explicitly round the results. Just do the obvious, naive thing in the shader.
850 return GrFragmentProcessor::UnpremulOutput(std::move(fp));
Robert Phillips757914d2017-01-25 15:48:30 -0500851 }
852}
853
Brian Osman60cd57e2017-04-06 10:19:06 -0400854sk_sp<GrFragmentProcessor> GrContext::createUPMToPMEffect(sk_sp<GrFragmentProcessor> fp,
Brian Osman409e74f2017-04-17 11:48:28 -0400855 bool useConfigConversionEffect) {
joshualitt1de610a2016-01-06 08:26:09 -0800856 ASSERT_SINGLE_OWNER
Brian Osman2d2da4f2017-04-12 17:07:22 -0400857 // We have specialized effects that guarantee round-trip conversion for these formats
Brian Osman409e74f2017-04-17 11:48:28 -0400858 if (useConfigConversionEffect) {
859 // We should have already called this->validPMUPMConversionExists() in this case
860 SkASSERT(fDidTestPMConversions);
861 // ...and it should have succeeded
862 SkASSERT(this->validPMUPMConversionExists());
863
Brian Osman28804f32017-04-20 10:24:36 -0400864 return GrConfigConversionEffect::Make(std::move(fp),
865 GrConfigConversionEffect::kToPremul_PMConversion);
Brian Osman2d2da4f2017-04-12 17:07:22 -0400866 } else {
867 // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and
868 // explicitly round the results. Just do the obvious, naive thing in the shader.
869 return GrFragmentProcessor::PremulOutput(std::move(fp));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000870 }
871}
872
Brian Osman409e74f2017-04-17 11:48:28 -0400873bool GrContext::validPMUPMConversionExists() {
joshualitt1de610a2016-01-06 08:26:09 -0800874 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -0400875 if (!fDidTestPMConversions) {
Brian Osman28804f32017-04-20 10:24:36 -0400876 fPMUPMConversionsRoundTrip = GrConfigConversionEffect::TestForPreservingPMConversions(this);
Brian Osman409e74f2017-04-17 11:48:28 -0400877 fDidTestPMConversions = true;
878 }
879
bsalomon636e8022015-07-29 06:08:46 -0700880 // The PM<->UPM tests fail or succeed together so we only need to check one.
Brian Osman28804f32017-04-20 10:24:36 -0400881 return fPMUPMConversionsRoundTrip;
bsalomon636e8022015-07-29 06:08:46 -0700882}
883
bsalomon37f9a262015-02-02 13:00:10 -0800884//////////////////////////////////////////////////////////////////////////////
885
886void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800887 ASSERT_SINGLE_OWNER
bsalomon37f9a262015-02-02 13:00:10 -0800888 if (maxTextures) {
bsalomon0ea80f42015-02-11 10:49:59 -0800889 *maxTextures = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -0800890 }
891 if (maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800892 *maxTextureBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -0800893 }
894}
895
896void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
joshualitt1de610a2016-01-06 08:26:09 -0800897 ASSERT_SINGLE_OWNER
bsalomon0ea80f42015-02-11 10:49:59 -0800898 fResourceCache->setLimits(maxTextures, maxTextureBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800899}
900
ericrk0a5fa482015-09-15 14:16:10 -0700901//////////////////////////////////////////////////////////////////////////////
902
903void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -0800904 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -0700905 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
906}