blob: 92be35c78cbeae2c84502ea03e6397236c639f67 [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());
halcanary385fe4d2015-08-26 13:07:48 -0700100 fResourceCache = new GrResourceCache(fCaps);
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
Robert Phillipsf2361d22016-10-25 14:20:06 -0400106 GrRenderTargetOpList::Options rtOpListOptions;
Brian Salomon09d994e2016-12-21 11:14:46 -0500107 rtOpListOptions.fMaxOpCombineLookback = options.fMaxOpCombineLookback;
108 rtOpListOptions.fMaxOpCombineLookahead = options.fMaxOpCombineLookahead;
bsalomon6b2552f2016-09-15 13:50:26 -0700109 GrPathRendererChain::Options prcOptions;
bsalomon39ef7fb2016-09-21 11:16:05 -0700110 prcOptions.fAllowPathMaskCaching = options.fAllowPathMaskCaching;
csmartdalton008b9d82017-02-22 12:00:42 -0700111 prcOptions.fGpuPathRenderers = options.fGpuPathRenderers;
Robert Phillipsf2361d22016-10-25 14:20:06 -0400112 fDrawingManager.reset(new GrDrawingManager(this, rtOpListOptions, prcOptions,
113 options.fImmediateMode, &fSingleOwner));
joshualitt7c3a2f82015-03-31 13:32:05 -0700114
Brian Salomonf856fd12016-12-16 14:24:34 -0500115 fAtlasGlyphCache = new GrAtlasGlyphCache(this);
joshualittb7133be2015-04-08 09:08:31 -0700116
halcanary385fe4d2015-08-26 13:07:48 -0700117 fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB, this));
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000118}
119
bsalomon@google.com27847de2011-02-22 20:59:41 +0000120GrContext::~GrContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800121 ASSERT_SINGLE_OWNER
122
robertphillipsea461502015-05-26 11:38:03 -0700123 if (!fGpu) {
bsalomon76228632015-05-29 08:02:10 -0700124 SkASSERT(!fCaps);
bsalomon@google.com733c0622013-04-24 17:59:32 +0000125 return;
126 }
127
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000128 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000129
robertphillips77a2e522015-10-17 07:43:27 -0700130 fDrawingManager->cleanup();
robertphillips2334fb62015-06-17 05:43:33 -0700131
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000132 for (int i = 0; i < fCleanUpData.count(); ++i) {
133 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
134 }
135
halcanary385fe4d2015-08-26 13:07:48 -0700136 delete fResourceProvider;
137 delete fResourceCache;
Brian Salomonf856fd12016-12-16 14:24:34 -0500138 delete fAtlasGlyphCache;
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000139
bsalomon@google.com205d4602011-04-25 12:43:45 +0000140 fGpu->unref();
bsalomon76228632015-05-29 08:02:10 -0700141 fCaps->unref();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000142}
143
bungeman6bd52842016-10-27 09:30:08 -0700144sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
bsalomon41b952c2016-03-11 06:46:33 -0800145 if (!fThreadSafeProxy) {
bungeman6bd52842016-10-27 09:30:08 -0700146 fThreadSafeProxy.reset(new GrContextThreadSafeProxy(sk_ref_sp(fCaps), this->uniqueID()));
bsalomon41b952c2016-03-11 06:46:33 -0800147 }
bungeman6bd52842016-10-27 09:30:08 -0700148 return fThreadSafeProxy;
bsalomon41b952c2016-03-11 06:46:33 -0800149}
150
bsalomon2354f842014-07-28 13:48:36 -0700151void GrContext::abandonContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800152 ASSERT_SINGLE_OWNER
153
bsalomond309e7a2015-04-30 14:18:54 -0700154 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800155
156 // Need to abandon the drawing manager first so all the render targets
157 // will be released/forgotten before they too are abandoned.
158 fDrawingManager->abandon();
159
bsalomon@google.com205d4602011-04-25 12:43:45 +0000160 // abandon first to so destructors
161 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800162 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700163
bsalomon6e2aad42016-04-01 11:54:31 -0700164 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
165
Brian Salomonf856fd12016-12-16 14:24:34 -0500166 fAtlasGlyphCache->freeAll();
bsalomon6e2aad42016-04-01 11:54:31 -0700167 fTextBlobCache->freeAll();
168}
169
170void GrContext::releaseResourcesAndAbandonContext() {
171 ASSERT_SINGLE_OWNER
172
173 fResourceProvider->abandon();
174
175 // Need to abandon the drawing manager first so all the render targets
176 // will be released/forgotten before they too are abandoned.
177 fDrawingManager->abandon();
178
179 // Release all resources in the backend 3D API.
180 fResourceCache->releaseAll();
181
182 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000183
Brian Salomonf856fd12016-12-16 14:24:34 -0500184 fAtlasGlyphCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700185 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000186}
187
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000188void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800189 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000190 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000191}
192
193void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800194 ASSERT_SINGLE_OWNER
195
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000196 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000197
Brian Salomonf856fd12016-12-16 14:24:34 -0500198 fAtlasGlyphCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700199
200 fDrawingManager->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700201
202 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000203}
204
Brian Salomon5e150852017-03-22 14:53:13 -0400205void GrContext::purgeResourcesNotUsedInMs(std::chrono::milliseconds ms) {
206 ASSERT_SINGLE_OWNER
207 fResourceCache->purgeResourcesNotUsedSince(GrStdSteadyClock::now() - ms);
208}
209
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000210void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800211 ASSERT_SINGLE_OWNER
212
bsalomon71cb0c22014-11-14 12:10:14 -0800213 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800214 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800215 }
216 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800217 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800218 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000219}
220
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000221////////////////////////////////////////////////////////////////////////////////
222
joshualitt0db6dfa2015-04-10 07:01:30 -0700223void GrContext::TextBlobCacheOverBudgetCB(void* data) {
224 SkASSERT(data);
Brian Osman11052242016-10-27 14:47:55 -0400225 // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on
226 // GrRenderTargetContext to perform a necessary flush. The solution is to move drawText calls
227 // to below the GrContext level, but this is not trivial because they call drawPath on
228 // SkGpuDevice.
joshualitt0db6dfa2015-04-10 07:01:30 -0700229 GrContext* context = reinterpret_cast<GrContext*>(data);
230 context->flush();
231}
232
bsalomon@google.com27847de2011-02-22 20:59:41 +0000233////////////////////////////////////////////////////////////////////////////////
234
bsalomonb77a9072016-09-07 10:02:04 -0700235void GrContext::flush() {
joshualitt1de610a2016-01-06 08:26:09 -0800236 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700237 RETURN_IF_ABANDONED
Robert Phillips7ee385e2017-03-30 08:02:11 -0400238
239 fDrawingManager->flush(nullptr);
240}
241
242void GrContextPriv::flush(GrSurfaceProxy* proxy) {
243 ASSERT_SINGLE_OWNER_PRIV
244 RETURN_IF_ABANDONED_PRIV
245 ASSERT_OWNED_PROXY_PRIV(proxy);
246
247 fContext->fDrawingManager->flush(proxy);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000248}
249
bsalomon81beccc2014-10-13 12:32:55 -0700250bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
251 const void* inPixels, size_t outRowBytes, void* outPixels) {
Matt Sarettc7b29082017-02-09 16:22:39 -0500252 SkColorType colorType;
253 if (!GrPixelConfigToColorType(srcConfig, &colorType) ||
254 4 != SkColorTypeBytesPerPixel(colorType))
255 {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000256 return false;
257 }
bsalomon81beccc2014-10-13 12:32:55 -0700258
Matt Sarettc7b29082017-02-09 16:22:39 -0500259 for (int y = 0; y < height; y++) {
260 SkOpts::RGBA_to_rgbA((uint32_t*) outPixels, inPixels, width);
261 outPixels = SkTAddOffset<void>(outPixels, outRowBytes);
262 inPixels = SkTAddOffset<const void>(inPixels, inRowBytes);
263 }
bsalomon81beccc2014-10-13 12:32:55 -0700264
Matt Sarettc7b29082017-02-09 16:22:39 -0500265 return true;
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000266}
267
Brian Osmand2ca59a2017-04-13 14:03:57 -0400268static bool valid_premul_config(GrPixelConfig config) {
Brian Osmance425512017-03-22 14:37:50 -0400269 return GrPixelConfigIs8888Unorm(config) || kRGBA_half_GrPixelConfig == config;
270}
271
Brian Osmand2ca59a2017-04-13 14:03:57 -0400272static bool valid_pixel_conversion(GrPixelConfig srcConfig, GrPixelConfig dstConfig,
273 bool premulConversion) {
274 // We don't allow conversion between integer configs and float/fixed configs.
275 if (GrPixelConfigIsSint(srcConfig) != GrPixelConfigIsSint(dstConfig)) {
276 return false;
277 }
278
279 // We only allow premul <-> unpremul conversions for some formats
280 if (premulConversion && (!valid_premul_config(srcConfig) || !valid_premul_config(dstConfig))) {
281 return false;
282 }
283
284 return true;
285}
286
Brian Osman409e74f2017-04-17 11:48:28 -0400287static bool pm_upm_must_round_trip(GrPixelConfig config, SkColorSpace* colorSpace) {
288 return !colorSpace &&
289 (kRGBA_8888_GrPixelConfig == config || kBGRA_8888_GrPixelConfig == config);
290}
291
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400292bool GrContextPriv::writeSurfacePixels(GrSurfaceContext* dst,
Robert Phillipse78b7252017-04-06 07:59:41 -0400293 int left, int top, int width, int height,
294 GrPixelConfig srcConfig, SkColorSpace* srcColorSpace,
295 const void* buffer, size_t rowBytes,
296 uint32_t pixelOpsFlags) {
Brian Osmanb62ea222016-12-22 11:12:16 -0500297 // TODO: Color space conversion
298
Robert Phillipse78b7252017-04-06 07:59:41 -0400299 ASSERT_SINGLE_OWNER_PRIV
300 RETURN_FALSE_IF_ABANDONED_PRIV
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400301 SkASSERT(dst);
302 ASSERT_OWNED_PROXY_PRIV(dst->asSurfaceProxy());
Robert Phillipse78b7252017-04-06 07:59:41 -0400303 GR_AUDIT_TRAIL_AUTO_FRAME(&fContext->fAuditTrail, "GrContextPriv::writeSurfacePixels");
bsalomon6c6f6582015-09-10 08:12:46 -0700304
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400305 GrSurface* dstSurface = dst->asSurfaceProxy()->instantiate(fContext->resourceProvider());
306 if (!dstSurface) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400307 return false;
308 }
309
Brian Osmand2ca59a2017-04-13 14:03:57 -0400310 // 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 -0400311 const bool premul = SkToBool(kUnpremul_PixelOpsFlag & pixelOpsFlags);
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400312 if (!valid_pixel_conversion(srcConfig, dstSurface->config(), premul)) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400313 return false;
314 }
315
Brian Osman409e74f2017-04-17 11:48:28 -0400316 // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data,
317 // without any color spaces attached, and the caller wants us to premul.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400318 bool useConfigConversionEffect =
319 premul &&
320 pm_upm_must_round_trip(srcConfig, srcColorSpace) &&
321 pm_upm_must_round_trip(dstSurface->config(), dst->getColorSpace());
Brian Osman409e74f2017-04-17 11:48:28 -0400322
323 // Are we going to try to premul as part of a draw? For the non-legacy case, we always allow
324 // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly.
325 bool premulOnGpu = premul &&
326 (!useConfigConversionEffect || fContext->validPMUPMConversionExists());
bsalomon81beccc2014-10-13 12:32:55 -0700327
bsalomone8d21e82015-07-16 08:23:13 -0700328 // 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 -0700329 // necessary and because GrGpu::getWritePixelsInfo requires it.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400330 if (!GrSurfacePriv::AdjustWritePixelParams(dstSurface->width(), dstSurface->height(),
bsalomone8d21e82015-07-16 08:23:13 -0700331 GrBytesPerPixel(srcConfig), &left, &top, &width,
332 &height, &buffer, &rowBytes)) {
333 return false;
334 }
335
Brian Osman409e74f2017-04-17 11:48:28 -0400336 GrGpu::DrawPreference drawPreference = premulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference
337 : GrGpu::kNoDraw_DrawPreference;
bsalomonf0674512015-07-28 13:26:15 -0700338 GrGpu::WritePixelTempDrawInfo tempDrawInfo;
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400339 if (!fContext->fGpu->getWritePixelsInfo(dstSurface, width, height, srcConfig,
Robert Phillipse78b7252017-04-06 07:59:41 -0400340 &drawPreference, &tempDrawInfo)) {
bsalomonf0674512015-07-28 13:26:15 -0700341 return false;
342 }
343
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400344 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && dstSurface->surfacePriv().hasPendingIO()) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400345 this->flush(nullptr); // MDB TODO: tighten this
bsalomonf0674512015-07-28 13:26:15 -0700346 }
347
Robert Phillips2f493142017-03-02 18:18:38 -0500348 sk_sp<GrTextureProxy> tempProxy;
bsalomonf0674512015-07-28 13:26:15 -0700349 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400350 tempProxy = GrSurfaceProxy::MakeDeferred(fContext->resourceProvider(),
Robert Phillips2f493142017-03-02 18:18:38 -0500351 tempDrawInfo.fTempSurfaceDesc,
352 SkBackingFit::kApprox,
353 SkBudgeted::kYes);
354 if (!tempProxy && GrGpu::kRequireDraw_DrawPreference == drawPreference) {
bsalomonf0674512015-07-28 13:26:15 -0700355 return false;
356 }
357 }
358
359 // temp buffer for doing sw premul conversion, if needed.
360 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
Brian Osman409e74f2017-04-17 11:48:28 -0400361 // We need to do sw premul if we were unable to create a RT for drawing, or if we can't do the
362 // premul on the GPU
363 if (premul && (!tempProxy || !premulOnGpu)) {
364 size_t tmpRowBytes = 4 * width;
365 tmpPixels.reset(width * height);
366 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
367 tmpPixels.get())) {
368 return false;
bsalomonf0674512015-07-28 13:26:15 -0700369 }
Brian Osman409e74f2017-04-17 11:48:28 -0400370 rowBytes = tmpRowBytes;
371 buffer = tmpPixels.get();
bsalomonf0674512015-07-28 13:26:15 -0700372 }
Brian Osman409e74f2017-04-17 11:48:28 -0400373
374 if (tempProxy) {
375 sk_sp<GrFragmentProcessor> fp = GrSimpleTextureEffect::Make(
376 fContext->resourceProvider(), tempProxy, nullptr, SkMatrix::I());
377 if (premulOnGpu) {
378 fp = fContext->createUPMToPMEffect(std::move(fp), useConfigConversionEffect);
bsalomon81beccc2014-10-13 12:32:55 -0700379 }
Brian Osman409e74f2017-04-17 11:48:28 -0400380 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle);
381 SkASSERT(fp);
382
383 if (tempProxy->priv().hasPendingIO()) {
384 this->flush(tempProxy.get());
385 }
386 GrTexture* texture = tempProxy->instantiate(fContext->resourceProvider());
387 if (!texture) {
388 return false;
389 }
390 if (!fContext->fGpu->writePixels(texture, 0, 0, width, height, tempDrawInfo.fWriteConfig,
391 buffer, rowBytes)) {
392 return false;
393 }
394 SkMatrix matrix;
395 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400396 GrRenderTargetContext* renderTargetContext = dst->asRenderTargetContext();
Brian Osman409e74f2017-04-17 11:48:28 -0400397 if (!renderTargetContext) {
398 return false;
399 }
400 GrPaint paint;
401 paint.addColorFragmentProcessor(std::move(fp));
402 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
Brian Osman7ab6a7f2017-04-25 15:01:46 -0400403 paint.setAllowSRGBInputs(SkToBool(dst->getColorSpace()) ||
404 GrPixelConfigIsSRGB(renderTargetContext->config()));
Brian Osman409e74f2017-04-17 11:48:28 -0400405 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
406 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, matrix, rect,
407 nullptr);
408
409 if (kFlushWrites_PixelOp & pixelOpsFlags) {
410 this->flushSurfaceWrites(renderTargetContext->asRenderTargetProxy());
411 }
412 } else {
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400413 return fContext->fGpu->writePixels(dstSurface, left, top, width, height, srcConfig,
Robert Phillipse78b7252017-04-06 07:59:41 -0400414 buffer, rowBytes);
bsalomon81beccc2014-10-13 12:32:55 -0700415 }
bsalomon81beccc2014-10-13 12:32:55 -0700416 return true;
417}
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000418
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400419bool GrContextPriv::readSurfacePixels(GrSurfaceContext* src,
Robert Phillipse78b7252017-04-06 07:59:41 -0400420 int left, int top, int width, int height,
421 GrPixelConfig dstConfig, SkColorSpace* dstColorSpace,
422 void* buffer, size_t rowBytes, uint32_t flags) {
Brian Osmanb62ea222016-12-22 11:12:16 -0500423 // TODO: Color space conversion
424
Robert Phillipse78b7252017-04-06 07:59:41 -0400425 ASSERT_SINGLE_OWNER_PRIV
426 RETURN_FALSE_IF_ABANDONED_PRIV
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400427 SkASSERT(src);
428 ASSERT_OWNED_PROXY_PRIV(src->asSurfaceProxy());
Robert Phillipse78b7252017-04-06 07:59:41 -0400429 GR_AUDIT_TRAIL_AUTO_FRAME(&fContext->fAuditTrail, "GrContextPriv::readSurfacePixels");
bsalomon32ab2602015-09-09 18:57:49 -0700430
Robert Phillipse78b7252017-04-06 07:59:41 -0400431 // MDB TODO: delay this instantiation until later in the method
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400432 GrSurface* srcSurface = src->asSurfaceProxy()->instantiate(fContext->resourceProvider());
433 if (!srcSurface) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400434 return false;
435 }
436
Brian Osmand2ca59a2017-04-13 14:03:57 -0400437 // The src is premul but the dst is unpremul -> unpremul the src after or as part of the read
438 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400439 if (!valid_pixel_conversion(srcSurface->config(), dstConfig, unpremul)) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400440 return false;
441 }
442
Brian Osman409e74f2017-04-17 11:48:28 -0400443 // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data,
444 // without any color spaces attached, and the caller wants us to unpremul.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400445 bool useConfigConversionEffect =
446 unpremul &&
447 pm_upm_must_round_trip(srcSurface->config(), src->getColorSpace()) &&
448 pm_upm_must_round_trip(dstConfig, dstColorSpace);
Brian Osman409e74f2017-04-17 11:48:28 -0400449
450 // Are we going to try to unpremul as part of a draw? For the non-legacy case, we always allow
451 // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly.
452 bool unpremulOnGpu = unpremul &&
453 (!useConfigConversionEffect || fContext->validPMUPMConversionExists());
bsalomon6c6f6582015-09-10 08:12:46 -0700454
bsalomone8d21e82015-07-16 08:23:13 -0700455 // Adjust the params so that if we wind up using an intermediate surface we've already done
456 // all the trimming and the temporary can be the min size required.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400457 if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(),
bsalomone8d21e82015-07-16 08:23:13 -0700458 GrBytesPerPixel(dstConfig), &left,
459 &top, &width, &height, &buffer, &rowBytes)) {
460 return false;
461 }
462
Brian Osman409e74f2017-04-17 11:48:28 -0400463 GrGpu::DrawPreference drawPreference = unpremulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference
464 : GrGpu::kNoDraw_DrawPreference;
bsalomon39826022015-07-23 08:07:21 -0700465 GrGpu::ReadPixelTempDrawInfo tempDrawInfo;
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400466 if (!fContext->fGpu->getReadPixelsInfo(srcSurface, width, height, rowBytes, dstConfig,
Robert Phillipse78b7252017-04-06 07:59:41 -0400467 &drawPreference, &tempDrawInfo)) {
bsalomon39826022015-07-23 08:07:21 -0700468 return false;
469 }
bsalomon191bcc02014-11-14 11:31:13 -0800470
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400471 if (!(kDontFlush_PixelOpsFlag & flags) && srcSurface->surfacePriv().hasPendingWrite()) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400472 this->flush(nullptr); // MDB TODO: tighten this
473 }
474
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400475 sk_sp<GrSurfaceProxy> proxyToRead = src->asSurfaceProxyRef();
bsalomon39826022015-07-23 08:07:21 -0700476 bool didTempDraw = false;
477 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomonb117ff12016-07-19 07:24:40 -0700478 if (SkBackingFit::kExact == tempDrawInfo.fTempSurfaceFit) {
bsalomon39826022015-07-23 08:07:21 -0700479 // We only respect this when the entire src is being read. Otherwise we can trigger too
480 // many odd ball texture sizes and trash the cache.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400481 if (width != srcSurface->width() || height != srcSurface->height()) {
bsalomonb117ff12016-07-19 07:24:40 -0700482 tempDrawInfo.fTempSurfaceFit= SkBackingFit::kApprox;
bsalomon39826022015-07-23 08:07:21 -0700483 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000484 }
brianosmandfe4f2e2016-07-21 13:28:36 -0700485 // TODO: Need to decide the semantics of this function for color spaces. Do we support
486 // conversion to a passed-in color space? For now, specifying nullptr means that this
487 // path will do no conversion, so it will match the behavior of the non-draw path.
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400488 sk_sp<GrRenderTargetContext> tempRTC = fContext->makeDeferredRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400489 tempDrawInfo.fTempSurfaceFit,
bsalomonb117ff12016-07-19 07:24:40 -0700490 tempDrawInfo.fTempSurfaceDesc.fWidth,
491 tempDrawInfo.fTempSurfaceDesc.fHeight,
492 tempDrawInfo.fTempSurfaceDesc.fConfig,
brianosmandfe4f2e2016-07-21 13:28:36 -0700493 nullptr,
bsalomonb117ff12016-07-19 07:24:40 -0700494 tempDrawInfo.fTempSurfaceDesc.fSampleCnt,
495 tempDrawInfo.fTempSurfaceDesc.fOrigin);
Brian Osman693a5402016-10-27 15:13:22 -0400496 if (tempRTC) {
Robert Phillips67c18d62017-01-20 12:44:06 -0500497 SkMatrix textureMatrix = SkMatrix::MakeTrans(SkIntToScalar(left), SkIntToScalar(top));
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400498 sk_sp<GrTextureProxy> proxy = src->asTextureProxyRef();
Brian Osman409e74f2017-04-17 11:48:28 -0400499 sk_sp<GrFragmentProcessor> fp = GrSimpleTextureEffect::Make(
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400500 fContext->resourceProvider(), std::move(proxy), nullptr, textureMatrix);
Brian Osman409e74f2017-04-17 11:48:28 -0400501 if (unpremulOnGpu) {
502 fp = fContext->createPMToUPMEffect(std::move(fp), useConfigConversionEffect);
503 // We no longer need to do this on CPU after the read back.
504 unpremul = false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000505 }
Brian Osman409e74f2017-04-17 11:48:28 -0400506 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle);
507 SkASSERT(fp);
Brian Osman60cd57e2017-04-06 10:19:06 -0400508
Brian Osman409e74f2017-04-17 11:48:28 -0400509 GrPaint paint;
510 paint.addColorFragmentProcessor(std::move(fp));
511 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
512 paint.setAllowSRGBInputs(true);
513 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
514 tempRTC->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect,
515 nullptr);
516 proxyToRead = tempRTC->asTextureProxyRef();
517 left = 0;
518 top = 0;
519 didTempDraw = true;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000520 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000521 }
joshualitt5c55fef2014-10-31 14:04:35 -0700522
Robert Phillipse78b7252017-04-06 07:59:41 -0400523 if (!proxyToRead) {
524 return false;
525 }
526
527 GrSurface* surfaceToRead = proxyToRead->instantiate(fContext->resourceProvider());
Robert Phillips833dcf42016-11-18 08:44:13 -0500528 if (!surfaceToRead) {
529 return false;
530 }
531
bsalomon39826022015-07-23 08:07:21 -0700532 if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000533 return false;
534 }
bsalomon39826022015-07-23 08:07:21 -0700535 GrPixelConfig configToRead = dstConfig;
536 if (didTempDraw) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400537 this->flushSurfaceWrites(proxyToRead.get());
bsalomon6c9cd552016-01-22 07:17:34 -0800538 configToRead = tempDrawInfo.fReadConfig;
bsalomon39826022015-07-23 08:07:21 -0700539 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400540 if (!fContext->fGpu->readPixels(surfaceToRead, left, top, width, height, configToRead,
541 buffer, rowBytes)) {
bsalomon39826022015-07-23 08:07:21 -0700542 return false;
543 }
544
545 // Perform umpremul conversion if we weren't able to perform it as a draw.
546 if (unpremul) {
Matt Sarettc7b29082017-02-09 16:22:39 -0500547 SkColorType colorType;
548 if (!GrPixelConfigToColorType(dstConfig, &colorType) ||
549 4 != SkColorTypeBytesPerPixel(colorType))
550 {
reed@google.com7111d462014-03-25 16:20:24 +0000551 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000552 }
reed@google.com7111d462014-03-25 16:20:24 +0000553
Matt Sarettc7b29082017-02-09 16:22:39 -0500554 for (int y = 0; y < height; y++) {
555 SkUnpremultiplyRow<false>((uint32_t*) buffer, (const uint32_t*) buffer, width);
556 buffer = SkTAddOffset<void>(buffer, rowBytes);
557 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000558 }
559 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000560}
561
Robert Phillips7ee385e2017-03-30 08:02:11 -0400562void GrContextPriv::prepareSurfaceForExternalIO(GrSurfaceProxy* proxy) {
563 ASSERT_SINGLE_OWNER_PRIV
564 RETURN_IF_ABANDONED_PRIV
565 SkASSERT(proxy);
566 ASSERT_OWNED_PROXY_PRIV(proxy);
567 fContext->fDrawingManager->prepareSurfaceForExternalIO(proxy);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000568}
569
Robert Phillips7ee385e2017-03-30 08:02:11 -0400570void GrContextPriv::flushSurfaceWrites(GrSurfaceProxy* proxy) {
571 ASSERT_SINGLE_OWNER_PRIV
572 RETURN_IF_ABANDONED_PRIV
573 SkASSERT(proxy);
574 ASSERT_OWNED_PROXY_PRIV(proxy);
575 if (proxy->priv().hasPendingWrite()) {
576 this->flush(proxy);
bsalomonf80bfed2014-10-07 05:56:02 -0700577 }
578}
579
Robert Phillips7ee385e2017-03-30 08:02:11 -0400580void GrContextPriv::flushSurfaceIO(GrSurfaceProxy* proxy) {
581 ASSERT_SINGLE_OWNER_PRIV
582 RETURN_IF_ABANDONED_PRIV
583 SkASSERT(proxy);
584 ASSERT_OWNED_PROXY_PRIV(proxy);
585 if (proxy->priv().hasPendingIO()) {
586 this->flush(proxy);
ajuma95243eb2016-08-24 08:19:02 -0700587 }
588}
589
bsalomon@google.com27847de2011-02-22 20:59:41 +0000590////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000591int GrContext::getRecommendedSampleCount(GrPixelConfig config,
592 SkScalar dpi) const {
joshualitt1de610a2016-01-06 08:26:09 -0800593 ASSERT_SINGLE_OWNER
594
bsalomon76228632015-05-29 08:02:10 -0700595 if (!this->caps()->isConfigRenderable(config, true)) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000596 return 0;
597 }
598 int chosenSampleCount = 0;
jvanverthe9c0fc62015-04-29 11:18:05 -0700599 if (fGpu->caps()->shaderCaps()->pathRenderingSupport()) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000600 if (dpi >= 250.0f) {
601 chosenSampleCount = 4;
602 } else {
603 chosenSampleCount = 16;
604 }
605 }
egdanieleed519e2016-01-15 11:36:18 -0800606 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ? chosenSampleCount : 0;
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000607}
608
Brian Osman11052242016-10-27 14:47:55 -0400609sk_sp<GrRenderTargetContext> GrContextPriv::makeWrappedRenderTargetContext(
610 sk_sp<GrRenderTarget> rt,
611 sk_sp<SkColorSpace> colorSpace,
612 const SkSurfaceProps* surfaceProps) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700613 ASSERT_SINGLE_OWNER_PRIV
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400614
Robert Phillips37430132016-11-09 06:50:43 -0500615 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(rt)));
Robert Phillips77b3f322017-01-31 18:24:12 -0500616 if (!proxy) {
617 return nullptr;
618 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400619
Robert Phillips37430132016-11-09 06:50:43 -0500620 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400621 std::move(colorSpace),
Brian Osman11052242016-10-27 14:47:55 -0400622 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -0700623}
robertphillips77a2e522015-10-17 07:43:27 -0700624
Robert Phillips2c862492017-01-18 10:08:39 -0500625sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy> proxy,
626 sk_sp<SkColorSpace> colorSpace) {
Brian Osman45580d32016-11-23 09:37:01 -0500627 ASSERT_SINGLE_OWNER_PRIV
628
Brian Osman45580d32016-11-23 09:37:01 -0500629 if (proxy->asRenderTargetProxy()) {
Robert Phillips2c862492017-01-18 10:08:39 -0500630 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
631 std::move(colorSpace), nullptr);
Brian Osman45580d32016-11-23 09:37:01 -0500632 } else {
633 SkASSERT(proxy->asTextureProxy());
Robert Phillips2c862492017-01-18 10:08:39 -0500634 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Brian Osman45580d32016-11-23 09:37:01 -0500635 }
636}
637
Robert Phillips31c26082016-12-14 15:12:15 -0500638sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurface> surface) {
639 ASSERT_SINGLE_OWNER_PRIV
640
641 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface)));
Robert Phillips77b3f322017-01-31 18:24:12 -0500642 if (!proxy) {
643 return nullptr;
644 }
Robert Phillips31c26082016-12-14 15:12:15 -0500645
Robert Phillips2c862492017-01-18 10:08:39 -0500646 return this->makeWrappedSurfaceContext(std::move(proxy), nullptr);
Robert Phillips31c26082016-12-14 15:12:15 -0500647}
648
Robert Phillipse2f7d182016-12-15 09:23:05 -0500649sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(const GrSurfaceDesc& dstDesc,
650 SkBackingFit fit,
651 SkBudgeted isDstBudgeted) {
652
Brian Osman32342f02017-03-04 08:12:46 -0500653 sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeDeferred(fContext->resourceProvider(),
Robert Phillips26c90e02017-03-14 14:39:29 -0400654 dstDesc, fit, isDstBudgeted);
Robert Phillips77b3f322017-01-31 18:24:12 -0500655 if (!proxy) {
656 return nullptr;
657 }
Robert Phillipse2f7d182016-12-15 09:23:05 -0500658
Robert Phillips2c862492017-01-18 10:08:39 -0500659 return this->makeWrappedSurfaceContext(std::move(proxy), nullptr);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500660}
661
Greg Daniel7ef28f32017-04-20 16:41:55 +0000662sk_sp<GrSurfaceContext> GrContextPriv::makeBackendSurfaceContext(const GrBackendTexture& tex,
663 GrSurfaceOrigin origin,
664 GrBackendTextureFlags flags,
665 int sampleCnt,
Brian Osmanc1e37052017-03-09 14:19:20 -0500666 sk_sp<SkColorSpace> colorSpace) {
Robert Phillips26caf892017-01-27 10:58:31 -0500667 ASSERT_SINGLE_OWNER_PRIV
668
Greg Daniel7ef28f32017-04-20 16:41:55 +0000669 sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTexture(tex, origin,
670 flags, sampleCnt));
Robert Phillips26caf892017-01-27 10:58:31 -0500671 if (!surface) {
672 return nullptr;
673 }
674
675 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface)));
Robert Phillips77b3f322017-01-31 18:24:12 -0500676 if (!proxy) {
677 return nullptr;
678 }
Robert Phillips26caf892017-01-27 10:58:31 -0500679
680 return this->makeWrappedSurfaceContext(std::move(proxy), std::move(colorSpace));
681}
682
Brian Osman11052242016-10-27 14:47:55 -0400683sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000684 const GrBackendTexture& tex,
685 GrSurfaceOrigin origin,
686 int sampleCnt,
Brian Osman11052242016-10-27 14:47:55 -0400687 sk_sp<SkColorSpace> colorSpace,
Brian Osmanc1e37052017-03-09 14:19:20 -0500688 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700689 ASSERT_SINGLE_OWNER_PRIV
robertphillips4fd74ae2016-08-03 14:26:53 -0700690
Greg Daniel7ef28f32017-04-20 16:41:55 +0000691 static const GrBackendTextureFlags kForceRT = kRenderTarget_GrBackendTextureFlag;
692 sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTexture(tex, origin, kForceRT,
693 sampleCnt));
robertphillips4fd74ae2016-08-03 14:26:53 -0700694 if (!surface) {
695 return nullptr;
696 }
697
Robert Phillips37430132016-11-09 06:50:43 -0500698 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface)));
Robert Phillips77b3f322017-01-31 18:24:12 -0500699 if (!proxy) {
700 return nullptr;
701 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400702
Robert Phillips37430132016-11-09 06:50:43 -0500703 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Brian Osman11052242016-10-27 14:47:55 -0400704 std::move(colorSpace), props);
robertphillips4fd74ae2016-08-03 14:26:53 -0700705}
706
Brian Osman11052242016-10-27 14:47:55 -0400707sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext(
robertphillips4fd74ae2016-08-03 14:26:53 -0700708 const GrBackendRenderTargetDesc& desc,
709 sk_sp<SkColorSpace> colorSpace,
710 const SkSurfaceProps* surfaceProps) {
711 ASSERT_SINGLE_OWNER_PRIV
712
Brian Osman32342f02017-03-04 08:12:46 -0500713 sk_sp<GrRenderTarget> rt(fContext->resourceProvider()->wrapBackendRenderTarget(desc));
robertphillips4fd74ae2016-08-03 14:26:53 -0700714 if (!rt) {
715 return nullptr;
716 }
717
Robert Phillips37430132016-11-09 06:50:43 -0500718 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(rt)));
Robert Phillips77b3f322017-01-31 18:24:12 -0500719 if (!proxy) {
720 return nullptr;
721 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400722
Robert Phillips37430132016-11-09 06:50:43 -0500723 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400724 std::move(colorSpace),
Brian Osman11052242016-10-27 14:47:55 -0400725 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -0700726}
727
Brian Osman11052242016-10-27 14:47:55 -0400728sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext(
Greg Daniel7ef28f32017-04-20 16:41:55 +0000729 const GrBackendTexture& tex,
730 GrSurfaceOrigin origin,
731 int sampleCnt,
robertphillips4fd74ae2016-08-03 14:26:53 -0700732 sk_sp<SkColorSpace> colorSpace,
733 const SkSurfaceProps* surfaceProps) {
734 ASSERT_SINGLE_OWNER_PRIV
robertphillips4fd74ae2016-08-03 14:26:53 -0700735
Greg Daniel7ef28f32017-04-20 16:41:55 +0000736 sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTextureAsRenderTarget(
737 tex,
738 origin,
739 sampleCnt));
robertphillips4fd74ae2016-08-03 14:26:53 -0700740 if (!surface) {
741 return nullptr;
742 }
743
Robert Phillips37430132016-11-09 06:50:43 -0500744 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface)));
Robert Phillips77b3f322017-01-31 18:24:12 -0500745 if (!proxy) {
746 return nullptr;
747 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400748
Robert Phillips37430132016-11-09 06:50:43 -0500749 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400750 std::move(colorSpace),
751 surfaceProps);
robertphillips77a2e522015-10-17 07:43:27 -0700752}
753
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400754void GrContextPriv::addPreFlushCallbackObject(sk_sp<GrPreFlushCallbackObject> preFlushCBObject) {
755 fContext->fDrawingManager->addPreFlushCallbackObject(std::move(preFlushCBObject));
756}
757
758
robertphillips48fde9c2016-09-06 05:20:20 -0700759static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) {
Brian Osman78f20e02017-01-12 10:28:01 -0500760 switch (config) {
761 case kAlpha_8_GrPixelConfig:
762 case kRGB_565_GrPixelConfig:
763 case kRGBA_4444_GrPixelConfig:
764 case kBGRA_8888_GrPixelConfig:
765 return kRGBA_8888_GrPixelConfig;
766 case kSBGRA_8888_GrPixelConfig:
767 return kSRGBA_8888_GrPixelConfig;
768 case kAlpha_half_GrPixelConfig:
769 return kRGBA_half_GrPixelConfig;
770 default:
771 return kUnknown_GrPixelConfig;
772 }
robertphillips48fde9c2016-09-06 05:20:20 -0700773}
774
robertphillipsd728f0c2016-11-21 11:05:03 -0800775sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContextWithFallback(
776 SkBackingFit fit,
777 int width, int height,
778 GrPixelConfig config,
779 sk_sp<SkColorSpace> colorSpace,
780 int sampleCnt,
781 GrSurfaceOrigin origin,
782 const SkSurfaceProps* surfaceProps,
783 SkBudgeted budgeted) {
784 if (!this->caps()->isConfigRenderable(config, sampleCnt > 0)) {
785 config = GrPixelConfigFallback(config);
786 }
787
788 return this->makeDeferredRenderTargetContext(fit, width, height, config, std::move(colorSpace),
789 sampleCnt, origin, surfaceProps, budgeted);
790}
791
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400792sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContext(
793 SkBackingFit fit,
794 int width, int height,
795 GrPixelConfig config,
796 sk_sp<SkColorSpace> colorSpace,
797 int sampleCnt,
798 GrSurfaceOrigin origin,
799 const SkSurfaceProps* surfaceProps,
800 SkBudgeted budgeted) {
Robert Phillipsdd3b3f42017-04-24 10:57:28 -0400801 SkASSERT(kDefault_GrSurfaceOrigin != origin);
802
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400803 GrSurfaceDesc desc;
804 desc.fFlags = kRenderTarget_GrSurfaceFlag;
805 desc.fOrigin = origin;
806 desc.fWidth = width;
807 desc.fHeight = height;
808 desc.fConfig = config;
809 desc.fSampleCnt = sampleCnt;
810
Brian Osman32342f02017-03-04 08:12:46 -0500811 sk_sp<GrTextureProxy> rtp = GrSurfaceProxy::MakeDeferred(this->resourceProvider(),
Robert Phillips26c90e02017-03-14 14:39:29 -0400812 desc, fit, budgeted);
Robert Phillips08c5ec72017-01-30 12:26:47 -0500813 if (!rtp) {
814 return nullptr;
815 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400816
Robert Phillips1119dc32017-04-11 12:54:57 -0400817 sk_sp<GrRenderTargetContext> renderTargetContext(
818 fDrawingManager->makeRenderTargetContext(std::move(rtp),
819 std::move(colorSpace),
820 surfaceProps));
821
822 if (!renderTargetContext) {
823 return nullptr;
824 }
825
826 renderTargetContext->discard();
827
828 return renderTargetContext;
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400829}
830
joshualitt1de610a2016-01-06 08:26:09 -0800831bool GrContext::abandoned() const {
832 ASSERT_SINGLE_OWNER
robertphillips7761d612016-05-16 09:14:53 -0700833 return fDrawingManager->wasAbandoned();
robertphillips77a2e522015-10-17 07:43:27 -0700834}
835
Brian Osman60cd57e2017-04-06 10:19:06 -0400836sk_sp<GrFragmentProcessor> GrContext::createPMToUPMEffect(sk_sp<GrFragmentProcessor> fp,
Brian Osman409e74f2017-04-17 11:48:28 -0400837 bool useConfigConversionEffect) {
Robert Phillips757914d2017-01-25 15:48:30 -0500838 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -0400839 // We have specialized effects that guarantee round-trip conversion for some formats
840 if (useConfigConversionEffect) {
841 // We should have already called this->validPMUPMConversionExists() in this case
842 SkASSERT(fDidTestPMConversions);
843 // ...and it should have succeeded
844 SkASSERT(this->validPMUPMConversionExists());
845
Brian Osman28804f32017-04-20 10:24:36 -0400846 return GrConfigConversionEffect::Make(std::move(fp),
847 GrConfigConversionEffect::kToUnpremul_PMConversion);
Brian Osman2d2da4f2017-04-12 17:07:22 -0400848 } else {
849 // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and
850 // explicitly round the results. Just do the obvious, naive thing in the shader.
851 return GrFragmentProcessor::UnpremulOutput(std::move(fp));
Robert Phillips757914d2017-01-25 15:48:30 -0500852 }
853}
854
Brian Osman60cd57e2017-04-06 10:19:06 -0400855sk_sp<GrFragmentProcessor> GrContext::createUPMToPMEffect(sk_sp<GrFragmentProcessor> fp,
Brian Osman409e74f2017-04-17 11:48:28 -0400856 bool useConfigConversionEffect) {
joshualitt1de610a2016-01-06 08:26:09 -0800857 ASSERT_SINGLE_OWNER
Brian Osman2d2da4f2017-04-12 17:07:22 -0400858 // We have specialized effects that guarantee round-trip conversion for these formats
Brian Osman409e74f2017-04-17 11:48:28 -0400859 if (useConfigConversionEffect) {
860 // We should have already called this->validPMUPMConversionExists() in this case
861 SkASSERT(fDidTestPMConversions);
862 // ...and it should have succeeded
863 SkASSERT(this->validPMUPMConversionExists());
864
Brian Osman28804f32017-04-20 10:24:36 -0400865 return GrConfigConversionEffect::Make(std::move(fp),
866 GrConfigConversionEffect::kToPremul_PMConversion);
Brian Osman2d2da4f2017-04-12 17:07:22 -0400867 } else {
868 // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and
869 // explicitly round the results. Just do the obvious, naive thing in the shader.
870 return GrFragmentProcessor::PremulOutput(std::move(fp));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000871 }
872}
873
Brian Osman409e74f2017-04-17 11:48:28 -0400874bool GrContext::validPMUPMConversionExists() {
joshualitt1de610a2016-01-06 08:26:09 -0800875 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -0400876 if (!fDidTestPMConversions) {
Brian Osman28804f32017-04-20 10:24:36 -0400877 fPMUPMConversionsRoundTrip = GrConfigConversionEffect::TestForPreservingPMConversions(this);
Brian Osman409e74f2017-04-17 11:48:28 -0400878 fDidTestPMConversions = true;
879 }
880
bsalomon636e8022015-07-29 06:08:46 -0700881 // The PM<->UPM tests fail or succeed together so we only need to check one.
Brian Osman28804f32017-04-20 10:24:36 -0400882 return fPMUPMConversionsRoundTrip;
bsalomon636e8022015-07-29 06:08:46 -0700883}
884
bsalomon37f9a262015-02-02 13:00:10 -0800885//////////////////////////////////////////////////////////////////////////////
886
887void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800888 ASSERT_SINGLE_OWNER
bsalomon37f9a262015-02-02 13:00:10 -0800889 if (maxTextures) {
bsalomon0ea80f42015-02-11 10:49:59 -0800890 *maxTextures = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -0800891 }
892 if (maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800893 *maxTextureBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -0800894 }
895}
896
897void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
joshualitt1de610a2016-01-06 08:26:09 -0800898 ASSERT_SINGLE_OWNER
bsalomon0ea80f42015-02-11 10:49:59 -0800899 fResourceCache->setLimits(maxTextures, maxTextureBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800900}
901
ericrk0a5fa482015-09-15 14:16:10 -0700902//////////////////////////////////////////////////////////////////////////////
903
904void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -0800905 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -0700906 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
907}