blob: 02ffe3c8a498f6d66aa6537b8faba6031d0e0687 [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);
403 paint.setAllowSRGBInputs(true);
404 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
405 renderTargetContext->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, matrix, rect,
406 nullptr);
407
408 if (kFlushWrites_PixelOp & pixelOpsFlags) {
409 this->flushSurfaceWrites(renderTargetContext->asRenderTargetProxy());
410 }
411 } else {
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400412 return fContext->fGpu->writePixels(dstSurface, left, top, width, height, srcConfig,
Robert Phillipse78b7252017-04-06 07:59:41 -0400413 buffer, rowBytes);
bsalomon81beccc2014-10-13 12:32:55 -0700414 }
bsalomon81beccc2014-10-13 12:32:55 -0700415 return true;
416}
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000417
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400418bool GrContextPriv::readSurfacePixels(GrSurfaceContext* src,
Robert Phillipse78b7252017-04-06 07:59:41 -0400419 int left, int top, int width, int height,
420 GrPixelConfig dstConfig, SkColorSpace* dstColorSpace,
421 void* buffer, size_t rowBytes, uint32_t flags) {
Brian Osmanb62ea222016-12-22 11:12:16 -0500422 // TODO: Color space conversion
423
Robert Phillipse78b7252017-04-06 07:59:41 -0400424 ASSERT_SINGLE_OWNER_PRIV
425 RETURN_FALSE_IF_ABANDONED_PRIV
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400426 SkASSERT(src);
427 ASSERT_OWNED_PROXY_PRIV(src->asSurfaceProxy());
Robert Phillipse78b7252017-04-06 07:59:41 -0400428 GR_AUDIT_TRAIL_AUTO_FRAME(&fContext->fAuditTrail, "GrContextPriv::readSurfacePixels");
bsalomon32ab2602015-09-09 18:57:49 -0700429
Robert Phillipse78b7252017-04-06 07:59:41 -0400430 // MDB TODO: delay this instantiation until later in the method
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400431 GrSurface* srcSurface = src->asSurfaceProxy()->instantiate(fContext->resourceProvider());
432 if (!srcSurface) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400433 return false;
434 }
435
Brian Osmand2ca59a2017-04-13 14:03:57 -0400436 // The src is premul but the dst is unpremul -> unpremul the src after or as part of the read
437 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400438 if (!valid_pixel_conversion(srcSurface->config(), dstConfig, unpremul)) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400439 return false;
440 }
441
Brian Osman409e74f2017-04-17 11:48:28 -0400442 // We need to guarantee round-trip conversion if we are reading and writing 8888 non-sRGB data,
443 // without any color spaces attached, and the caller wants us to unpremul.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400444 bool useConfigConversionEffect =
445 unpremul &&
446 pm_upm_must_round_trip(srcSurface->config(), src->getColorSpace()) &&
447 pm_upm_must_round_trip(dstConfig, dstColorSpace);
Brian Osman409e74f2017-04-17 11:48:28 -0400448
449 // Are we going to try to unpremul as part of a draw? For the non-legacy case, we always allow
450 // this. GrConfigConversionEffect fails on some GPUs, so only allow this if it works perfectly.
451 bool unpremulOnGpu = unpremul &&
452 (!useConfigConversionEffect || fContext->validPMUPMConversionExists());
bsalomon6c6f6582015-09-10 08:12:46 -0700453
bsalomone8d21e82015-07-16 08:23:13 -0700454 // Adjust the params so that if we wind up using an intermediate surface we've already done
455 // all the trimming and the temporary can be the min size required.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400456 if (!GrSurfacePriv::AdjustReadPixelParams(srcSurface->width(), srcSurface->height(),
bsalomone8d21e82015-07-16 08:23:13 -0700457 GrBytesPerPixel(dstConfig), &left,
458 &top, &width, &height, &buffer, &rowBytes)) {
459 return false;
460 }
461
Brian Osman409e74f2017-04-17 11:48:28 -0400462 GrGpu::DrawPreference drawPreference = unpremulOnGpu ? GrGpu::kCallerPrefersDraw_DrawPreference
463 : GrGpu::kNoDraw_DrawPreference;
bsalomon39826022015-07-23 08:07:21 -0700464 GrGpu::ReadPixelTempDrawInfo tempDrawInfo;
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400465 if (!fContext->fGpu->getReadPixelsInfo(srcSurface, width, height, rowBytes, dstConfig,
Robert Phillipse78b7252017-04-06 07:59:41 -0400466 &drawPreference, &tempDrawInfo)) {
bsalomon39826022015-07-23 08:07:21 -0700467 return false;
468 }
bsalomon191bcc02014-11-14 11:31:13 -0800469
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400470 if (!(kDontFlush_PixelOpsFlag & flags) && srcSurface->surfacePriv().hasPendingWrite()) {
Brian Osmand2ca59a2017-04-13 14:03:57 -0400471 this->flush(nullptr); // MDB TODO: tighten this
472 }
473
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400474 sk_sp<GrSurfaceProxy> proxyToRead = src->asSurfaceProxyRef();
bsalomon39826022015-07-23 08:07:21 -0700475 bool didTempDraw = false;
476 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomonb117ff12016-07-19 07:24:40 -0700477 if (SkBackingFit::kExact == tempDrawInfo.fTempSurfaceFit) {
bsalomon39826022015-07-23 08:07:21 -0700478 // We only respect this when the entire src is being read. Otherwise we can trigger too
479 // many odd ball texture sizes and trash the cache.
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400480 if (width != srcSurface->width() || height != srcSurface->height()) {
bsalomonb117ff12016-07-19 07:24:40 -0700481 tempDrawInfo.fTempSurfaceFit= SkBackingFit::kApprox;
bsalomon39826022015-07-23 08:07:21 -0700482 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000483 }
brianosmandfe4f2e2016-07-21 13:28:36 -0700484 // TODO: Need to decide the semantics of this function for color spaces. Do we support
485 // conversion to a passed-in color space? For now, specifying nullptr means that this
486 // path will do no conversion, so it will match the behavior of the non-draw path.
Robert Phillips93429212017-04-11 12:23:17 +0000487 sk_sp<GrRenderTargetContext> tempRTC = fContext->makeRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400488 tempDrawInfo.fTempSurfaceFit,
bsalomonb117ff12016-07-19 07:24:40 -0700489 tempDrawInfo.fTempSurfaceDesc.fWidth,
490 tempDrawInfo.fTempSurfaceDesc.fHeight,
491 tempDrawInfo.fTempSurfaceDesc.fConfig,
brianosmandfe4f2e2016-07-21 13:28:36 -0700492 nullptr,
bsalomonb117ff12016-07-19 07:24:40 -0700493 tempDrawInfo.fTempSurfaceDesc.fSampleCnt,
494 tempDrawInfo.fTempSurfaceDesc.fOrigin);
Brian Osman693a5402016-10-27 15:13:22 -0400495 if (tempRTC) {
Robert Phillips67c18d62017-01-20 12:44:06 -0500496 SkMatrix textureMatrix = SkMatrix::MakeTrans(SkIntToScalar(left), SkIntToScalar(top));
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400497 sk_sp<GrTextureProxy> proxy = src->asTextureProxyRef();
Brian Osman409e74f2017-04-17 11:48:28 -0400498 sk_sp<GrFragmentProcessor> fp = GrSimpleTextureEffect::Make(
Robert Phillipsf41c22f2017-04-18 07:48:58 -0400499 fContext->resourceProvider(), std::move(proxy), nullptr, textureMatrix);
Brian Osman409e74f2017-04-17 11:48:28 -0400500 if (unpremulOnGpu) {
501 fp = fContext->createPMToUPMEffect(std::move(fp), useConfigConversionEffect);
502 // We no longer need to do this on CPU after the read back.
503 unpremul = false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000504 }
Brian Osman409e74f2017-04-17 11:48:28 -0400505 fp = GrFragmentProcessor::SwizzleOutput(std::move(fp), tempDrawInfo.fSwizzle);
506 SkASSERT(fp);
Brian Osman60cd57e2017-04-06 10:19:06 -0400507
Brian Osman409e74f2017-04-17 11:48:28 -0400508 GrPaint paint;
509 paint.addColorFragmentProcessor(std::move(fp));
510 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
511 paint.setAllowSRGBInputs(true);
512 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
513 tempRTC->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), rect,
514 nullptr);
515 proxyToRead = tempRTC->asTextureProxyRef();
516 left = 0;
517 top = 0;
518 didTempDraw = true;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000519 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000520 }
joshualitt5c55fef2014-10-31 14:04:35 -0700521
Robert Phillipse78b7252017-04-06 07:59:41 -0400522 if (!proxyToRead) {
523 return false;
524 }
525
526 GrSurface* surfaceToRead = proxyToRead->instantiate(fContext->resourceProvider());
Robert Phillips833dcf42016-11-18 08:44:13 -0500527 if (!surfaceToRead) {
528 return false;
529 }
530
bsalomon39826022015-07-23 08:07:21 -0700531 if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000532 return false;
533 }
bsalomon39826022015-07-23 08:07:21 -0700534 GrPixelConfig configToRead = dstConfig;
535 if (didTempDraw) {
Robert Phillipse78b7252017-04-06 07:59:41 -0400536 this->flushSurfaceWrites(proxyToRead.get());
bsalomon6c9cd552016-01-22 07:17:34 -0800537 configToRead = tempDrawInfo.fReadConfig;
bsalomon39826022015-07-23 08:07:21 -0700538 }
Robert Phillipse78b7252017-04-06 07:59:41 -0400539 if (!fContext->fGpu->readPixels(surfaceToRead, left, top, width, height, configToRead,
540 buffer, rowBytes)) {
bsalomon39826022015-07-23 08:07:21 -0700541 return false;
542 }
543
544 // Perform umpremul conversion if we weren't able to perform it as a draw.
545 if (unpremul) {
Matt Sarettc7b29082017-02-09 16:22:39 -0500546 SkColorType colorType;
547 if (!GrPixelConfigToColorType(dstConfig, &colorType) ||
548 4 != SkColorTypeBytesPerPixel(colorType))
549 {
reed@google.com7111d462014-03-25 16:20:24 +0000550 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000551 }
reed@google.com7111d462014-03-25 16:20:24 +0000552
Matt Sarettc7b29082017-02-09 16:22:39 -0500553 for (int y = 0; y < height; y++) {
554 SkUnpremultiplyRow<false>((uint32_t*) buffer, (const uint32_t*) buffer, width);
555 buffer = SkTAddOffset<void>(buffer, rowBytes);
556 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000557 }
558 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000559}
560
Robert Phillips7ee385e2017-03-30 08:02:11 -0400561void GrContextPriv::prepareSurfaceForExternalIO(GrSurfaceProxy* proxy) {
562 ASSERT_SINGLE_OWNER_PRIV
563 RETURN_IF_ABANDONED_PRIV
564 SkASSERT(proxy);
565 ASSERT_OWNED_PROXY_PRIV(proxy);
566 fContext->fDrawingManager->prepareSurfaceForExternalIO(proxy);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000567}
568
Robert Phillips7ee385e2017-03-30 08:02:11 -0400569void GrContextPriv::flushSurfaceWrites(GrSurfaceProxy* proxy) {
570 ASSERT_SINGLE_OWNER_PRIV
571 RETURN_IF_ABANDONED_PRIV
572 SkASSERT(proxy);
573 ASSERT_OWNED_PROXY_PRIV(proxy);
574 if (proxy->priv().hasPendingWrite()) {
575 this->flush(proxy);
bsalomonf80bfed2014-10-07 05:56:02 -0700576 }
577}
578
Robert Phillips7ee385e2017-03-30 08:02:11 -0400579void GrContextPriv::flushSurfaceIO(GrSurfaceProxy* proxy) {
580 ASSERT_SINGLE_OWNER_PRIV
581 RETURN_IF_ABANDONED_PRIV
582 SkASSERT(proxy);
583 ASSERT_OWNED_PROXY_PRIV(proxy);
584 if (proxy->priv().hasPendingIO()) {
585 this->flush(proxy);
ajuma95243eb2016-08-24 08:19:02 -0700586 }
587}
588
bsalomon@google.com27847de2011-02-22 20:59:41 +0000589////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000590int GrContext::getRecommendedSampleCount(GrPixelConfig config,
591 SkScalar dpi) const {
joshualitt1de610a2016-01-06 08:26:09 -0800592 ASSERT_SINGLE_OWNER
593
bsalomon76228632015-05-29 08:02:10 -0700594 if (!this->caps()->isConfigRenderable(config, true)) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000595 return 0;
596 }
597 int chosenSampleCount = 0;
jvanverthe9c0fc62015-04-29 11:18:05 -0700598 if (fGpu->caps()->shaderCaps()->pathRenderingSupport()) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000599 if (dpi >= 250.0f) {
600 chosenSampleCount = 4;
601 } else {
602 chosenSampleCount = 16;
603 }
604 }
egdanieleed519e2016-01-15 11:36:18 -0800605 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ? chosenSampleCount : 0;
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000606}
607
Brian Osman11052242016-10-27 14:47:55 -0400608sk_sp<GrRenderTargetContext> GrContextPriv::makeWrappedRenderTargetContext(
609 sk_sp<GrRenderTarget> rt,
610 sk_sp<SkColorSpace> colorSpace,
611 const SkSurfaceProps* surfaceProps) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700612 ASSERT_SINGLE_OWNER_PRIV
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400613
Robert Phillips37430132016-11-09 06:50:43 -0500614 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(rt)));
Robert Phillips77b3f322017-01-31 18:24:12 -0500615 if (!proxy) {
616 return nullptr;
617 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400618
Robert Phillips37430132016-11-09 06:50:43 -0500619 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400620 std::move(colorSpace),
Brian Osman11052242016-10-27 14:47:55 -0400621 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -0700622}
robertphillips77a2e522015-10-17 07:43:27 -0700623
Robert Phillips2c862492017-01-18 10:08:39 -0500624sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurfaceProxy> proxy,
625 sk_sp<SkColorSpace> colorSpace) {
Brian Osman45580d32016-11-23 09:37:01 -0500626 ASSERT_SINGLE_OWNER_PRIV
627
Brian Osman45580d32016-11-23 09:37:01 -0500628 if (proxy->asRenderTargetProxy()) {
Robert Phillips2c862492017-01-18 10:08:39 -0500629 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
630 std::move(colorSpace), nullptr);
Brian Osman45580d32016-11-23 09:37:01 -0500631 } else {
632 SkASSERT(proxy->asTextureProxy());
Robert Phillips2c862492017-01-18 10:08:39 -0500633 return this->drawingManager()->makeTextureContext(std::move(proxy), std::move(colorSpace));
Brian Osman45580d32016-11-23 09:37:01 -0500634 }
635}
636
Robert Phillips31c26082016-12-14 15:12:15 -0500637sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurface> surface) {
638 ASSERT_SINGLE_OWNER_PRIV
639
640 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface)));
Robert Phillips77b3f322017-01-31 18:24:12 -0500641 if (!proxy) {
642 return nullptr;
643 }
Robert Phillips31c26082016-12-14 15:12:15 -0500644
Robert Phillips2c862492017-01-18 10:08:39 -0500645 return this->makeWrappedSurfaceContext(std::move(proxy), nullptr);
Robert Phillips31c26082016-12-14 15:12:15 -0500646}
647
Robert Phillipse2f7d182016-12-15 09:23:05 -0500648sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(const GrSurfaceDesc& dstDesc,
649 SkBackingFit fit,
650 SkBudgeted isDstBudgeted) {
651
Brian Osman32342f02017-03-04 08:12:46 -0500652 sk_sp<GrTextureProxy> proxy = GrSurfaceProxy::MakeDeferred(fContext->resourceProvider(),
Robert Phillips26c90e02017-03-14 14:39:29 -0400653 dstDesc, fit, isDstBudgeted);
Robert Phillips77b3f322017-01-31 18:24:12 -0500654 if (!proxy) {
655 return nullptr;
656 }
Robert Phillipse2f7d182016-12-15 09:23:05 -0500657
Robert Phillips2c862492017-01-18 10:08:39 -0500658 return this->makeWrappedSurfaceContext(std::move(proxy), nullptr);
Robert Phillipse2f7d182016-12-15 09:23:05 -0500659}
660
Stan Iliev7fa5c312017-04-19 00:23:39 +0000661sk_sp<GrSurfaceContext> GrContextPriv::makeBackendSurfaceContext(const GrBackendTextureDesc& desc,
Brian Osmanc1e37052017-03-09 14:19:20 -0500662 sk_sp<SkColorSpace> colorSpace) {
Robert Phillips26caf892017-01-27 10:58:31 -0500663 ASSERT_SINGLE_OWNER_PRIV
664
Stan Iliev7fa5c312017-04-19 00:23:39 +0000665 sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTexture(desc));
Robert Phillips26caf892017-01-27 10:58:31 -0500666 if (!surface) {
667 return nullptr;
668 }
669
670 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface)));
Robert Phillips77b3f322017-01-31 18:24:12 -0500671 if (!proxy) {
672 return nullptr;
673 }
Robert Phillips26caf892017-01-27 10:58:31 -0500674
675 return this->makeWrappedSurfaceContext(std::move(proxy), std::move(colorSpace));
676}
677
Brian Osman11052242016-10-27 14:47:55 -0400678sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext(
Stan Iliev7fa5c312017-04-19 00:23:39 +0000679 const GrBackendTextureDesc& desc,
Brian Osman11052242016-10-27 14:47:55 -0400680 sk_sp<SkColorSpace> colorSpace,
Brian Osmanc1e37052017-03-09 14:19:20 -0500681 const SkSurfaceProps* props) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700682 ASSERT_SINGLE_OWNER_PRIV
Stan Iliev7fa5c312017-04-19 00:23:39 +0000683 SkASSERT(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
robertphillips4fd74ae2016-08-03 14:26:53 -0700684
Stan Iliev7fa5c312017-04-19 00:23:39 +0000685 sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTexture(desc));
robertphillips4fd74ae2016-08-03 14:26:53 -0700686 if (!surface) {
687 return nullptr;
688 }
689
Robert Phillips37430132016-11-09 06:50:43 -0500690 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface)));
Robert Phillips77b3f322017-01-31 18:24:12 -0500691 if (!proxy) {
692 return nullptr;
693 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400694
Robert Phillips37430132016-11-09 06:50:43 -0500695 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Brian Osman11052242016-10-27 14:47:55 -0400696 std::move(colorSpace), props);
robertphillips4fd74ae2016-08-03 14:26:53 -0700697}
698
Brian Osman11052242016-10-27 14:47:55 -0400699sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext(
robertphillips4fd74ae2016-08-03 14:26:53 -0700700 const GrBackendRenderTargetDesc& desc,
701 sk_sp<SkColorSpace> colorSpace,
702 const SkSurfaceProps* surfaceProps) {
703 ASSERT_SINGLE_OWNER_PRIV
704
Brian Osman32342f02017-03-04 08:12:46 -0500705 sk_sp<GrRenderTarget> rt(fContext->resourceProvider()->wrapBackendRenderTarget(desc));
robertphillips4fd74ae2016-08-03 14:26:53 -0700706 if (!rt) {
707 return nullptr;
708 }
709
Robert Phillips37430132016-11-09 06:50:43 -0500710 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(rt)));
Robert Phillips77b3f322017-01-31 18:24:12 -0500711 if (!proxy) {
712 return nullptr;
713 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400714
Robert Phillips37430132016-11-09 06:50:43 -0500715 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400716 std::move(colorSpace),
Brian Osman11052242016-10-27 14:47:55 -0400717 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -0700718}
719
Brian Osman11052242016-10-27 14:47:55 -0400720sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext(
Stan Iliev7fa5c312017-04-19 00:23:39 +0000721 const GrBackendTextureDesc& desc,
robertphillips4fd74ae2016-08-03 14:26:53 -0700722 sk_sp<SkColorSpace> colorSpace,
723 const SkSurfaceProps* surfaceProps) {
724 ASSERT_SINGLE_OWNER_PRIV
Stan Iliev7fa5c312017-04-19 00:23:39 +0000725 SkASSERT(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
robertphillips4fd74ae2016-08-03 14:26:53 -0700726
Stan Iliev7fa5c312017-04-19 00:23:39 +0000727 sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTextureAsRenderTarget(desc));
robertphillips4fd74ae2016-08-03 14:26:53 -0700728 if (!surface) {
729 return nullptr;
730 }
731
Robert Phillips37430132016-11-09 06:50:43 -0500732 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface)));
Robert Phillips77b3f322017-01-31 18:24:12 -0500733 if (!proxy) {
734 return nullptr;
735 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400736
Robert Phillips37430132016-11-09 06:50:43 -0500737 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400738 std::move(colorSpace),
739 surfaceProps);
robertphillips77a2e522015-10-17 07:43:27 -0700740}
741
Robert Phillipseb35f4d2017-03-21 07:56:47 -0400742void GrContextPriv::addPreFlushCallbackObject(sk_sp<GrPreFlushCallbackObject> preFlushCBObject) {
743 fContext->fDrawingManager->addPreFlushCallbackObject(std::move(preFlushCBObject));
744}
745
746
robertphillips48fde9c2016-09-06 05:20:20 -0700747static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) {
Brian Osman78f20e02017-01-12 10:28:01 -0500748 switch (config) {
749 case kAlpha_8_GrPixelConfig:
750 case kRGB_565_GrPixelConfig:
751 case kRGBA_4444_GrPixelConfig:
752 case kBGRA_8888_GrPixelConfig:
753 return kRGBA_8888_GrPixelConfig;
754 case kSBGRA_8888_GrPixelConfig:
755 return kSRGBA_8888_GrPixelConfig;
756 case kAlpha_half_GrPixelConfig:
757 return kRGBA_half_GrPixelConfig;
758 default:
759 return kUnknown_GrPixelConfig;
760 }
robertphillips48fde9c2016-09-06 05:20:20 -0700761}
762
Robert Phillips93429212017-04-11 12:23:17 +0000763sk_sp<GrRenderTargetContext> GrContext::makeRenderTargetContextWithFallback(
764 SkBackingFit fit,
765 int width, int height,
766 GrPixelConfig config,
767 sk_sp<SkColorSpace> colorSpace,
768 int sampleCnt,
769 GrSurfaceOrigin origin,
770 const SkSurfaceProps* surfaceProps,
771 SkBudgeted budgeted) {
772 if (!this->caps()->isConfigRenderable(config, sampleCnt > 0)) {
773 config = GrPixelConfigFallback(config);
774 }
775
776 return this->makeRenderTargetContext(fit, width, height, config, std::move(colorSpace),
777 sampleCnt, origin, surfaceProps, budgeted);
778}
779
robertphillipsd728f0c2016-11-21 11:05:03 -0800780sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContextWithFallback(
781 SkBackingFit fit,
782 int width, int height,
783 GrPixelConfig config,
784 sk_sp<SkColorSpace> colorSpace,
785 int sampleCnt,
786 GrSurfaceOrigin origin,
787 const SkSurfaceProps* surfaceProps,
788 SkBudgeted budgeted) {
789 if (!this->caps()->isConfigRenderable(config, sampleCnt > 0)) {
790 config = GrPixelConfigFallback(config);
791 }
792
793 return this->makeDeferredRenderTargetContext(fit, width, height, config, std::move(colorSpace),
794 sampleCnt, origin, surfaceProps, budgeted);
795}
796
Robert Phillips93429212017-04-11 12:23:17 +0000797sk_sp<GrRenderTargetContext> GrContext::makeRenderTargetContext(SkBackingFit fit,
798 int width, int height,
799 GrPixelConfig config,
800 sk_sp<SkColorSpace> colorSpace,
801 int sampleCnt,
802 GrSurfaceOrigin origin,
803 const SkSurfaceProps* surfaceProps,
804 SkBudgeted budgeted) {
805 if (!this->caps()->isConfigRenderable(config, sampleCnt > 0)) {
806 return nullptr;
807 }
808
809 GrSurfaceDesc desc;
810 desc.fFlags = kRenderTarget_GrSurfaceFlag;
811 desc.fOrigin = origin;
812 desc.fWidth = width;
813 desc.fHeight = height;
814 desc.fConfig = config;
815 desc.fSampleCnt = sampleCnt;
816
817 sk_sp<GrTexture> tex;
818 if (SkBackingFit::kExact == fit) {
819 tex = this->resourceProvider()->createTexture(desc, budgeted);
820 } else {
821 tex.reset(this->resourceProvider()->createApproxTexture(desc, 0));
822 }
823 if (!tex) {
824 return nullptr;
825 }
826
827 sk_sp<GrRenderTargetContext> renderTargetContext(
828 this->contextPriv().makeWrappedRenderTargetContext(sk_ref_sp(tex->asRenderTarget()),
829 std::move(colorSpace), surfaceProps));
830 if (!renderTargetContext) {
831 return nullptr;
832 }
833
Robert Phillips1119dc32017-04-11 12:54:57 -0400834 renderTargetContext->discard();
835
Robert Phillips93429212017-04-11 12:23:17 +0000836 return renderTargetContext;
837}
838
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400839sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContext(
840 SkBackingFit fit,
841 int width, int height,
842 GrPixelConfig config,
843 sk_sp<SkColorSpace> colorSpace,
844 int sampleCnt,
845 GrSurfaceOrigin origin,
846 const SkSurfaceProps* surfaceProps,
847 SkBudgeted budgeted) {
848 GrSurfaceDesc desc;
849 desc.fFlags = kRenderTarget_GrSurfaceFlag;
850 desc.fOrigin = origin;
851 desc.fWidth = width;
852 desc.fHeight = height;
853 desc.fConfig = config;
854 desc.fSampleCnt = sampleCnt;
855
Brian Osman32342f02017-03-04 08:12:46 -0500856 sk_sp<GrTextureProxy> rtp = GrSurfaceProxy::MakeDeferred(this->resourceProvider(),
Robert Phillips26c90e02017-03-14 14:39:29 -0400857 desc, fit, budgeted);
Robert Phillips08c5ec72017-01-30 12:26:47 -0500858 if (!rtp) {
859 return nullptr;
860 }
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400861
Robert Phillips1119dc32017-04-11 12:54:57 -0400862 sk_sp<GrRenderTargetContext> renderTargetContext(
863 fDrawingManager->makeRenderTargetContext(std::move(rtp),
864 std::move(colorSpace),
865 surfaceProps));
866
867 if (!renderTargetContext) {
868 return nullptr;
869 }
870
871 renderTargetContext->discard();
872
873 return renderTargetContext;
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400874}
875
joshualitt1de610a2016-01-06 08:26:09 -0800876bool GrContext::abandoned() const {
877 ASSERT_SINGLE_OWNER
robertphillips7761d612016-05-16 09:14:53 -0700878 return fDrawingManager->wasAbandoned();
robertphillips77a2e522015-10-17 07:43:27 -0700879}
880
Brian Osman60cd57e2017-04-06 10:19:06 -0400881sk_sp<GrFragmentProcessor> GrContext::createPMToUPMEffect(sk_sp<GrFragmentProcessor> fp,
Brian Osman409e74f2017-04-17 11:48:28 -0400882 bool useConfigConversionEffect) {
Robert Phillips757914d2017-01-25 15:48:30 -0500883 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -0400884 // We have specialized effects that guarantee round-trip conversion for some formats
885 if (useConfigConversionEffect) {
886 // We should have already called this->validPMUPMConversionExists() in this case
887 SkASSERT(fDidTestPMConversions);
888 // ...and it should have succeeded
889 SkASSERT(this->validPMUPMConversionExists());
890
Brian Osman28804f32017-04-20 10:24:36 -0400891 return GrConfigConversionEffect::Make(std::move(fp),
892 GrConfigConversionEffect::kToUnpremul_PMConversion);
Brian Osman2d2da4f2017-04-12 17:07:22 -0400893 } else {
894 // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and
895 // explicitly round the results. Just do the obvious, naive thing in the shader.
896 return GrFragmentProcessor::UnpremulOutput(std::move(fp));
Robert Phillips757914d2017-01-25 15:48:30 -0500897 }
898}
899
Brian Osman60cd57e2017-04-06 10:19:06 -0400900sk_sp<GrFragmentProcessor> GrContext::createUPMToPMEffect(sk_sp<GrFragmentProcessor> fp,
Brian Osman409e74f2017-04-17 11:48:28 -0400901 bool useConfigConversionEffect) {
joshualitt1de610a2016-01-06 08:26:09 -0800902 ASSERT_SINGLE_OWNER
Brian Osman2d2da4f2017-04-12 17:07:22 -0400903 // We have specialized effects that guarantee round-trip conversion for these formats
Brian Osman409e74f2017-04-17 11:48:28 -0400904 if (useConfigConversionEffect) {
905 // We should have already called this->validPMUPMConversionExists() in this case
906 SkASSERT(fDidTestPMConversions);
907 // ...and it should have succeeded
908 SkASSERT(this->validPMUPMConversionExists());
909
Brian Osman28804f32017-04-20 10:24:36 -0400910 return GrConfigConversionEffect::Make(std::move(fp),
911 GrConfigConversionEffect::kToPremul_PMConversion);
Brian Osman2d2da4f2017-04-12 17:07:22 -0400912 } else {
913 // For everything else (sRGB, half-float, etc...), it doesn't make sense to try and
914 // explicitly round the results. Just do the obvious, naive thing in the shader.
915 return GrFragmentProcessor::PremulOutput(std::move(fp));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000916 }
917}
918
Brian Osman409e74f2017-04-17 11:48:28 -0400919bool GrContext::validPMUPMConversionExists() {
joshualitt1de610a2016-01-06 08:26:09 -0800920 ASSERT_SINGLE_OWNER
Brian Osman409e74f2017-04-17 11:48:28 -0400921 if (!fDidTestPMConversions) {
Brian Osman28804f32017-04-20 10:24:36 -0400922 fPMUPMConversionsRoundTrip = GrConfigConversionEffect::TestForPreservingPMConversions(this);
Brian Osman409e74f2017-04-17 11:48:28 -0400923 fDidTestPMConversions = true;
924 }
925
bsalomon636e8022015-07-29 06:08:46 -0700926 // The PM<->UPM tests fail or succeed together so we only need to check one.
Brian Osman28804f32017-04-20 10:24:36 -0400927 return fPMUPMConversionsRoundTrip;
bsalomon636e8022015-07-29 06:08:46 -0700928}
929
bsalomon37f9a262015-02-02 13:00:10 -0800930//////////////////////////////////////////////////////////////////////////////
931
932void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800933 ASSERT_SINGLE_OWNER
bsalomon37f9a262015-02-02 13:00:10 -0800934 if (maxTextures) {
bsalomon0ea80f42015-02-11 10:49:59 -0800935 *maxTextures = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -0800936 }
937 if (maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800938 *maxTextureBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -0800939 }
940}
941
942void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
joshualitt1de610a2016-01-06 08:26:09 -0800943 ASSERT_SINGLE_OWNER
bsalomon0ea80f42015-02-11 10:49:59 -0800944 fResourceCache->setLimits(maxTextures, maxTextureBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800945}
946
ericrk0a5fa482015-09-15 14:16:10 -0700947//////////////////////////////////////////////////////////////////////////////
948
949void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -0800950 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -0700951 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
952}