blob: 65503420c93a7242bdc18ba23beb1a8d88952956 [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"
robertphillips4fd74ae2016-08-03 14:26:53 -07009#include "GrContextPriv.h"
bsalomon682c2692015-05-22 14:01:46 -070010#include "GrContextOptions.h"
robertphillips77a2e522015-10-17 07:43:27 -070011#include "GrDrawingManager.h"
Brian Osman11052242016-10-27 14:47:55 -040012#include "GrRenderTargetContext.h"
bsalomon0ea80f42015-02-11 10:49:59 -080013#include "GrResourceCache.h"
bsalomond309e7a2015-04-30 14:18:54 -070014#include "GrResourceProvider.h"
Robert Phillipsc7635fa2016-10-28 13:25:24 -040015#include "GrRenderTargetProxy.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000016#include "GrSoftwarePathRenderer.h"
Brian Osman45580d32016-11-23 09:37:01 -050017#include "GrSurfaceContext.h"
bsalomonafbf2d62014-09-30 12:18:44 -070018#include "GrSurfacePriv.h"
Brian Osman45580d32016-11-23 09:37:01 -050019#include "GrTextureContext.h"
robertphillips3dc6ae52015-10-20 09:54:32 -070020
bsalomon81beccc2014-10-13 12:32:55 -070021#include "SkConfig8888.h"
bsalomonf276ac52015-10-09 13:36:42 -070022#include "SkGrPriv.h"
joshualitt74417822015-08-07 11:42:16 -070023
bsalomonb8fea972016-02-16 07:34:17 -080024#include "batches/GrCopySurfaceBatch.h"
joshualitt5478d422014-11-14 16:00:38 -080025#include "effects/GrConfigConversionEffect.h"
brianosman2d1ee792016-05-05 12:24:31 -070026#include "effects/GrGammaEffect.h"
joshualitte8042922015-12-11 06:11:21 -080027#include "text/GrTextBlobCache.h"
joshualitt5478d422014-11-14 16:00:38 -080028
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000029#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
joshualitt1de610a2016-01-06 08:26:09 -080030#define ASSERT_SINGLE_OWNER \
31 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fSingleOwner);)
robertphillips4fd74ae2016-08-03 14:26:53 -070032#define ASSERT_SINGLE_OWNER_PRIV \
33 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fContext->fSingleOwner);)
robertphillips7761d612016-05-16 09:14:53 -070034#define RETURN_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return; }
35#define RETURN_FALSE_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return false; }
36#define RETURN_NULL_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return nullptr; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000037
robertphillipsea461502015-05-26 11:38:03 -070038////////////////////////////////////////////////////////////////////////////////
39
bsalomon682c2692015-05-22 14:01:46 -070040GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext) {
41 GrContextOptions defaultOptions;
42 return Create(backend, backendContext, defaultOptions);
43}
bsalomonf28cff72015-05-22 12:25:41 -070044
bsalomon682c2692015-05-22 14:01:46 -070045GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
46 const GrContextOptions& options) {
halcanary385fe4d2015-08-26 13:07:48 -070047 GrContext* context = new GrContext;
bsalomon682c2692015-05-22 14:01:46 -070048
49 if (context->init(backend, backendContext, options)) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000050 return context;
51 } else {
52 context->unref();
halcanary96fcdcc2015-08-27 07:41:13 -070053 return nullptr;
bsalomon@google.com27847de2011-02-22 20:59:41 +000054 }
bsalomon@google.com27847de2011-02-22 20:59:41 +000055}
56
joshualitt0acd0d32015-05-07 08:23:19 -070057static int32_t gNextID = 1;
58static int32_t next_id() {
59 int32_t id;
60 do {
61 id = sk_atomic_inc(&gNextID);
62 } while (id == SK_InvalidGenID);
63 return id;
64}
65
bsalomon682c2692015-05-22 14:01:46 -070066GrContext::GrContext() : fUniqueID(next_id()) {
halcanary96fcdcc2015-08-27 07:41:13 -070067 fGpu = nullptr;
68 fCaps = nullptr;
69 fResourceCache = nullptr;
70 fResourceProvider = nullptr;
halcanary96fcdcc2015-08-27 07:41:13 -070071 fBatchFontCache = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000072}
73
bsalomon682c2692015-05-22 14:01:46 -070074bool GrContext::init(GrBackend backend, GrBackendContext backendContext,
75 const GrContextOptions& options) {
joshualitt1de610a2016-01-06 08:26:09 -080076 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -070077 SkASSERT(!fGpu);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000078
bsalomon682c2692015-05-22 14:01:46 -070079 fGpu = GrGpu::Create(backend, backendContext, options, this);
robertphillipsea461502015-05-26 11:38:03 -070080 if (!fGpu) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000081 return false;
82 }
bsalomon69cfe952015-11-30 13:27:47 -080083 this->initCommon(options);
bsalomon33435572014-11-05 14:47:41 -080084 return true;
85}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000086
bsalomon69cfe952015-11-30 13:27:47 -080087void GrContext::initCommon(const GrContextOptions& options) {
joshualitt1de610a2016-01-06 08:26:09 -080088 ASSERT_SINGLE_OWNER
89
bsalomon76228632015-05-29 08:02:10 -070090 fCaps = SkRef(fGpu->caps());
halcanary385fe4d2015-08-26 13:07:48 -070091 fResourceCache = new GrResourceCache(fCaps);
joshualitt6d0872d2016-01-11 08:27:48 -080092 fResourceProvider = new GrResourceProvider(fGpu, fResourceCache, &fSingleOwner);
commit-bot@chromium.org1836d332013-07-16 22:55:03 +000093
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000094 fDidTestPMConversions = false;
95
Robert Phillipsf2361d22016-10-25 14:20:06 -040096 GrRenderTargetOpList::Options rtOpListOptions;
97 rtOpListOptions.fClipBatchToBounds = options.fClipBatchToBounds;
Robert Phillipsf2361d22016-10-25 14:20:06 -040098 rtOpListOptions.fMaxBatchLookback = options.fMaxBatchLookback;
99 rtOpListOptions.fMaxBatchLookahead = options.fMaxBatchLookahead;
bsalomon6b2552f2016-09-15 13:50:26 -0700100 GrPathRendererChain::Options prcOptions;
101 prcOptions.fDisableDistanceFieldRenderer = options.fDisableDistanceFieldPaths;
bsalomon39ef7fb2016-09-21 11:16:05 -0700102 prcOptions.fAllowPathMaskCaching = options.fAllowPathMaskCaching;
103 prcOptions.fDisableAllPathRenderers = options.fForceSWPathMasks;
Robert Phillipsf2361d22016-10-25 14:20:06 -0400104 fDrawingManager.reset(new GrDrawingManager(this, rtOpListOptions, prcOptions,
105 options.fImmediateMode, &fSingleOwner));
joshualitt7c3a2f82015-03-31 13:32:05 -0700106
107 // GrBatchFontCache will eventually replace GrFontCache
halcanary385fe4d2015-08-26 13:07:48 -0700108 fBatchFontCache = new GrBatchFontCache(this);
joshualittb7133be2015-04-08 09:08:31 -0700109
halcanary385fe4d2015-08-26 13:07:48 -0700110 fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB, this));
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000111}
112
bsalomon@google.com27847de2011-02-22 20:59:41 +0000113GrContext::~GrContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800114 ASSERT_SINGLE_OWNER
115
robertphillipsea461502015-05-26 11:38:03 -0700116 if (!fGpu) {
bsalomon76228632015-05-29 08:02:10 -0700117 SkASSERT(!fCaps);
bsalomon@google.com733c0622013-04-24 17:59:32 +0000118 return;
119 }
120
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000121 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000122
robertphillips77a2e522015-10-17 07:43:27 -0700123 fDrawingManager->cleanup();
robertphillips2334fb62015-06-17 05:43:33 -0700124
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000125 for (int i = 0; i < fCleanUpData.count(); ++i) {
126 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
127 }
128
halcanary385fe4d2015-08-26 13:07:48 -0700129 delete fResourceProvider;
130 delete fResourceCache;
131 delete fBatchFontCache;
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000132
bsalomon@google.com205d4602011-04-25 12:43:45 +0000133 fGpu->unref();
bsalomon76228632015-05-29 08:02:10 -0700134 fCaps->unref();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000135}
136
bungeman6bd52842016-10-27 09:30:08 -0700137sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
bsalomon41b952c2016-03-11 06:46:33 -0800138 if (!fThreadSafeProxy) {
bungeman6bd52842016-10-27 09:30:08 -0700139 fThreadSafeProxy.reset(new GrContextThreadSafeProxy(sk_ref_sp(fCaps), this->uniqueID()));
bsalomon41b952c2016-03-11 06:46:33 -0800140 }
bungeman6bd52842016-10-27 09:30:08 -0700141 return fThreadSafeProxy;
bsalomon41b952c2016-03-11 06:46:33 -0800142}
143
bsalomon2354f842014-07-28 13:48:36 -0700144void GrContext::abandonContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800145 ASSERT_SINGLE_OWNER
146
bsalomond309e7a2015-04-30 14:18:54 -0700147 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800148
149 // Need to abandon the drawing manager first so all the render targets
150 // will be released/forgotten before they too are abandoned.
151 fDrawingManager->abandon();
152
bsalomon@google.com205d4602011-04-25 12:43:45 +0000153 // abandon first to so destructors
154 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800155 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700156
bsalomon6e2aad42016-04-01 11:54:31 -0700157 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
158
159 fBatchFontCache->freeAll();
bsalomon6e2aad42016-04-01 11:54:31 -0700160 fTextBlobCache->freeAll();
161}
162
163void GrContext::releaseResourcesAndAbandonContext() {
164 ASSERT_SINGLE_OWNER
165
166 fResourceProvider->abandon();
167
168 // Need to abandon the drawing manager first so all the render targets
169 // will be released/forgotten before they too are abandoned.
170 fDrawingManager->abandon();
171
172 // Release all resources in the backend 3D API.
173 fResourceCache->releaseAll();
174
175 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000176
joshualitt7c3a2f82015-03-31 13:32:05 -0700177 fBatchFontCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700178 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000179}
180
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000181void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800182 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000183 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000184}
185
186void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800187 ASSERT_SINGLE_OWNER
188
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000189 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000190
joshualitt7c3a2f82015-03-31 13:32:05 -0700191 fBatchFontCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700192
193 fDrawingManager->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700194
195 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000196}
197
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000198void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800199 ASSERT_SINGLE_OWNER
200
bsalomon71cb0c22014-11-14 12:10:14 -0800201 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800202 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800203 }
204 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800205 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800206 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000207}
208
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000209////////////////////////////////////////////////////////////////////////////////
210
joshualitt0db6dfa2015-04-10 07:01:30 -0700211void GrContext::TextBlobCacheOverBudgetCB(void* data) {
212 SkASSERT(data);
Brian Osman11052242016-10-27 14:47:55 -0400213 // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on
214 // GrRenderTargetContext to perform a necessary flush. The solution is to move drawText calls
215 // to below the GrContext level, but this is not trivial because they call drawPath on
216 // SkGpuDevice.
joshualitt0db6dfa2015-04-10 07:01:30 -0700217 GrContext* context = reinterpret_cast<GrContext*>(data);
218 context->flush();
219}
220
bsalomon@google.com27847de2011-02-22 20:59:41 +0000221////////////////////////////////////////////////////////////////////////////////
222
bsalomonb77a9072016-09-07 10:02:04 -0700223void GrContext::flush() {
joshualitt1de610a2016-01-06 08:26:09 -0800224 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700225 RETURN_IF_ABANDONED
bsalomonb77a9072016-09-07 10:02:04 -0700226 fDrawingManager->flush();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000227}
228
bsalomon81beccc2014-10-13 12:32:55 -0700229bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
230 const void* inPixels, size_t outRowBytes, void* outPixels) {
231 SkSrcPixelInfo srcPI;
brianosman396fcdb2016-07-22 06:26:11 -0700232 if (!GrPixelConfigToColorType(srcConfig, &srcPI.fColorType)) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000233 return false;
234 }
bsalomon81beccc2014-10-13 12:32:55 -0700235 srcPI.fAlphaType = kUnpremul_SkAlphaType;
236 srcPI.fPixels = inPixels;
237 srcPI.fRowBytes = inRowBytes;
238
239 SkDstPixelInfo dstPI;
240 dstPI.fColorType = srcPI.fColorType;
241 dstPI.fAlphaType = kPremul_SkAlphaType;
242 dstPI.fPixels = outPixels;
243 dstPI.fRowBytes = outRowBytes;
244
245 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000246}
247
bsalomon81beccc2014-10-13 12:32:55 -0700248bool GrContext::writeSurfacePixels(GrSurface* surface,
249 int left, int top, int width, int height,
250 GrPixelConfig srcConfig, const void* buffer, size_t rowBytes,
251 uint32_t pixelOpsFlags) {
joshualitt1de610a2016-01-06 08:26:09 -0800252 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800253 RETURN_FALSE_IF_ABANDONED
bsalomon6c6f6582015-09-10 08:12:46 -0700254 ASSERT_OWNED_RESOURCE(surface);
255 SkASSERT(surface);
joshualittbc907352016-01-13 06:45:40 -0800256 GR_AUDIT_TRAIL_AUTO_FRAME(&fAuditTrail, "GrContext::writeSurfacePixels");
bsalomon6c6f6582015-09-10 08:12:46 -0700257
258 this->testPMConversionsIfNecessary(pixelOpsFlags);
bsalomon81beccc2014-10-13 12:32:55 -0700259
bsalomone8d21e82015-07-16 08:23:13 -0700260 // 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 -0700261 // necessary and because GrGpu::getWritePixelsInfo requires it.
bsalomone8d21e82015-07-16 08:23:13 -0700262 if (!GrSurfacePriv::AdjustWritePixelParams(surface->width(), surface->height(),
263 GrBytesPerPixel(srcConfig), &left, &top, &width,
264 &height, &buffer, &rowBytes)) {
265 return false;
266 }
267
bsalomonf0674512015-07-28 13:26:15 -0700268 bool applyPremulToSrc = false;
bsalomon81beccc2014-10-13 12:32:55 -0700269 if (kUnpremul_PixelOpsFlag & pixelOpsFlags) {
Brian Salomonbf7b6202016-11-11 16:08:03 -0500270 if (!GrPixelConfigIs8888Unorm(srcConfig)) {
bsalomon81beccc2014-10-13 12:32:55 -0700271 return false;
272 }
bsalomonf0674512015-07-28 13:26:15 -0700273 applyPremulToSrc = true;
274 }
Brian Salomonbf7b6202016-11-11 16:08:03 -0500275 // We don't allow conversion between integer configs and float/fixed configs.
276 if (GrPixelConfigIsSint(surface->config()) != GrPixelConfigIsSint(srcConfig)) {
277 return false;
278 }
bsalomon636e8022015-07-29 06:08:46 -0700279
280 GrGpu::DrawPreference drawPreference = GrGpu::kNoDraw_DrawPreference;
281 // Don't prefer to draw for the conversion (and thereby access a texture from the cache) when
282 // we've already determined that there isn't a roundtrip preserving conversion processor pair.
283 if (applyPremulToSrc && !this->didFailPMUPMConversionTest()) {
284 drawPreference = GrGpu::kCallerPrefersDraw_DrawPreference;
285 }
286
bsalomonf0674512015-07-28 13:26:15 -0700287 GrGpu::WritePixelTempDrawInfo tempDrawInfo;
cblumeed828002016-02-16 13:00:01 -0800288 if (!fGpu->getWritePixelsInfo(surface, width, height, srcConfig, &drawPreference,
bsalomonf0674512015-07-28 13:26:15 -0700289 &tempDrawInfo)) {
290 return false;
291 }
292
293 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && surface->surfacePriv().hasPendingIO()) {
294 this->flush();
295 }
296
Hal Canary144caf52016-11-07 17:57:18 -0500297 sk_sp<GrTexture> tempTexture;
bsalomonf0674512015-07-28 13:26:15 -0700298 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomoneae62002015-07-31 13:59:30 -0700299 tempTexture.reset(
300 this->textureProvider()->createApproxTexture(tempDrawInfo.fTempSurfaceDesc));
bsalomonf0674512015-07-28 13:26:15 -0700301 if (!tempTexture && GrGpu::kRequireDraw_DrawPreference == drawPreference) {
302 return false;
303 }
304 }
305
306 // temp buffer for doing sw premul conversion, if needed.
307 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
308 if (tempTexture) {
bungeman06ca8ec2016-06-09 08:01:03 -0700309 sk_sp<GrFragmentProcessor> fp;
bsalomonf0674512015-07-28 13:26:15 -0700310 SkMatrix textureMatrix;
311 textureMatrix.setIDiv(tempTexture->width(), tempTexture->height());
bsalomonf0674512015-07-28 13:26:15 -0700312 if (applyPremulToSrc) {
Hal Canary144caf52016-11-07 17:57:18 -0500313 fp = this->createUPMToPMEffect(tempTexture.get(), tempDrawInfo.fSwizzle, textureMatrix);
bsalomonf0674512015-07-28 13:26:15 -0700314 // If premultiplying was the only reason for the draw, fall back to a straight write.
315 if (!fp) {
316 if (GrGpu::kCallerPrefersDraw_DrawPreference == drawPreference) {
halcanary96fcdcc2015-08-27 07:41:13 -0700317 tempTexture.reset(nullptr);
bsalomonf0674512015-07-28 13:26:15 -0700318 }
319 } else {
320 applyPremulToSrc = false;
321 }
322 }
323 if (tempTexture) {
324 if (!fp) {
Hal Canary144caf52016-11-07 17:57:18 -0500325 fp = GrConfigConversionEffect::Make(tempTexture.get(), tempDrawInfo.fSwizzle,
bungeman06ca8ec2016-06-09 08:01:03 -0700326 GrConfigConversionEffect::kNone_PMConversion,
327 textureMatrix);
bsalomonf0674512015-07-28 13:26:15 -0700328 if (!fp) {
329 return false;
330 }
331 }
332 GrRenderTarget* renderTarget = surface->asRenderTarget();
333 SkASSERT(renderTarget);
334 if (tempTexture->surfacePriv().hasPendingIO()) {
335 this->flush();
336 }
337 if (applyPremulToSrc) {
338 size_t tmpRowBytes = 4 * width;
339 tmpPixels.reset(width * height);
340 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
341 tmpPixels.get())) {
342 return false;
343 }
344 rowBytes = tmpRowBytes;
345 buffer = tmpPixels.get();
346 applyPremulToSrc = false;
347 }
Hal Canary144caf52016-11-07 17:57:18 -0500348 if (!fGpu->writePixels(tempTexture.get(), 0, 0, width, height,
bsalomon6c9cd552016-01-22 07:17:34 -0800349 tempDrawInfo.fWriteConfig, buffer,
bsalomon6cb3cbe2015-07-30 07:34:27 -0700350 rowBytes)) {
bsalomonf0674512015-07-28 13:26:15 -0700351 return false;
352 }
353 SkMatrix matrix;
354 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
brianosmandfe4f2e2016-07-21 13:28:36 -0700355 // TODO: Need to decide the semantics of this function for color spaces. Do we support
356 // conversion from a passed-in color space? For now, specifying nullptr means that this
357 // path will do no conversion, so it will match the behavior of the non-draw path.
Brian Osman11052242016-10-27 14:47:55 -0400358 sk_sp<GrRenderTargetContext> renderTargetContext(
359 this->contextPriv().makeWrappedRenderTargetContext(sk_ref_sp(renderTarget),
360 nullptr));
361 if (!renderTargetContext) {
bsalomonf0674512015-07-28 13:26:15 -0700362 return false;
363 }
egdanielc4b72722015-11-23 13:20:41 -0800364 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700365 paint.addColorFragmentProcessor(std::move(fp));
reed374772b2016-10-05 17:33:02 -0700366 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
brianosmana167e742016-05-24 06:18:48 -0700367 paint.setAllowSRGBInputs(true);
bsalomonf0674512015-07-28 13:26:15 -0700368 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500369 renderTargetContext->drawRect(GrNoClip(), paint, GrAA::kNo, matrix, rect, nullptr);
bsalomonf0674512015-07-28 13:26:15 -0700370
371 if (kFlushWrites_PixelOp & pixelOpsFlags) {
372 this->flushSurfaceWrites(surface);
373 }
374 }
375 }
376 if (!tempTexture) {
bsalomonf0674512015-07-28 13:26:15 -0700377 if (applyPremulToSrc) {
bsalomon81beccc2014-10-13 12:32:55 -0700378 size_t tmpRowBytes = 4 * width;
379 tmpPixels.reset(width * height);
380 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
381 tmpPixels.get())) {
382 return false;
383 }
384 rowBytes = tmpRowBytes;
385 buffer = tmpPixels.get();
bsalomonf0674512015-07-28 13:26:15 -0700386 applyPremulToSrc = false;
bsalomon81beccc2014-10-13 12:32:55 -0700387 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700388 return fGpu->writePixels(surface, left, top, width, height, srcConfig, buffer, rowBytes);
bsalomon81beccc2014-10-13 12:32:55 -0700389 }
bsalomon81beccc2014-10-13 12:32:55 -0700390 return true;
391}
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000392
bsalomone8d21e82015-07-16 08:23:13 -0700393bool GrContext::readSurfacePixels(GrSurface* src,
394 int left, int top, int width, int height,
395 GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
396 uint32_t flags) {
joshualitt1de610a2016-01-06 08:26:09 -0800397 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800398 RETURN_FALSE_IF_ABANDONED
bsalomone8d21e82015-07-16 08:23:13 -0700399 ASSERT_OWNED_RESOURCE(src);
400 SkASSERT(src);
joshualittbc907352016-01-13 06:45:40 -0800401 GR_AUDIT_TRAIL_AUTO_FRAME(&fAuditTrail, "GrContext::readSurfacePixels");
bsalomon32ab2602015-09-09 18:57:49 -0700402
bsalomon6c6f6582015-09-10 08:12:46 -0700403 this->testPMConversionsIfNecessary(flags);
bsalomon6c6f6582015-09-10 08:12:46 -0700404
bsalomone8d21e82015-07-16 08:23:13 -0700405 // Adjust the params so that if we wind up using an intermediate surface we've already done
406 // all the trimming and the temporary can be the min size required.
407 if (!GrSurfacePriv::AdjustReadPixelParams(src->width(), src->height(),
408 GrBytesPerPixel(dstConfig), &left,
409 &top, &width, &height, &buffer, &rowBytes)) {
410 return false;
411 }
412
413 if (!(kDontFlush_PixelOpsFlag & flags) && src->surfacePriv().hasPendingWrite()) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000414 this->flush();
415 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000416
bsalomone8d21e82015-07-16 08:23:13 -0700417 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500418 if (unpremul && !GrPixelConfigIs8888Unorm(dstConfig)) {
bsalomon39826022015-07-23 08:07:21 -0700419 // The unpremul flag is only allowed for 8888 configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000420 return false;
421 }
Brian Salomonbf7b6202016-11-11 16:08:03 -0500422 // We don't allow conversion between integer configs and float/fixed configs.
423 if (GrPixelConfigIsSint(src->config()) != GrPixelConfigIsSint(dstConfig)) {
424 return false;
425 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000426
bsalomon636e8022015-07-29 06:08:46 -0700427 GrGpu::DrawPreference drawPreference = GrGpu::kNoDraw_DrawPreference;
428 // Don't prefer to draw for the conversion (and thereby access a texture from the cache) when
429 // we've already determined that there isn't a roundtrip preserving conversion processor pair.
430 if (unpremul && !this->didFailPMUPMConversionTest()) {
431 drawPreference = GrGpu::kCallerPrefersDraw_DrawPreference;
432 }
433
bsalomon39826022015-07-23 08:07:21 -0700434 GrGpu::ReadPixelTempDrawInfo tempDrawInfo;
435 if (!fGpu->getReadPixelsInfo(src, width, height, rowBytes, dstConfig, &drawPreference,
436 &tempDrawInfo)) {
437 return false;
438 }
bsalomon191bcc02014-11-14 11:31:13 -0800439
Hal Canary144caf52016-11-07 17:57:18 -0500440 sk_sp<GrSurface> surfaceToRead(SkRef(src));
bsalomon39826022015-07-23 08:07:21 -0700441 bool didTempDraw = false;
442 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomonb117ff12016-07-19 07:24:40 -0700443 if (SkBackingFit::kExact == tempDrawInfo.fTempSurfaceFit) {
bsalomon39826022015-07-23 08:07:21 -0700444 // We only respect this when the entire src is being read. Otherwise we can trigger too
445 // many odd ball texture sizes and trash the cache.
bsalomoneae62002015-07-31 13:59:30 -0700446 if (width != src->width() || height != src->height()) {
bsalomonb117ff12016-07-19 07:24:40 -0700447 tempDrawInfo.fTempSurfaceFit= SkBackingFit::kApprox;
bsalomon39826022015-07-23 08:07:21 -0700448 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000449 }
brianosmandfe4f2e2016-07-21 13:28:36 -0700450 // TODO: Need to decide the semantics of this function for color spaces. Do we support
451 // conversion to a passed-in color space? For now, specifying nullptr means that this
452 // path will do no conversion, so it will match the behavior of the non-draw path.
Brian Osman693a5402016-10-27 15:13:22 -0400453 sk_sp<GrRenderTargetContext> tempRTC = this->makeRenderTargetContext(
Brian Osman11052242016-10-27 14:47:55 -0400454 tempDrawInfo.fTempSurfaceFit,
bsalomonb117ff12016-07-19 07:24:40 -0700455 tempDrawInfo.fTempSurfaceDesc.fWidth,
456 tempDrawInfo.fTempSurfaceDesc.fHeight,
457 tempDrawInfo.fTempSurfaceDesc.fConfig,
brianosmandfe4f2e2016-07-21 13:28:36 -0700458 nullptr,
bsalomonb117ff12016-07-19 07:24:40 -0700459 tempDrawInfo.fTempSurfaceDesc.fSampleCnt,
460 tempDrawInfo.fTempSurfaceDesc.fOrigin);
Brian Osman693a5402016-10-27 15:13:22 -0400461 if (tempRTC) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000462 SkMatrix textureMatrix;
bsalomon39826022015-07-23 08:07:21 -0700463 textureMatrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000464 textureMatrix.postIDiv(src->width(), src->height());
bungeman06ca8ec2016-06-09 08:01:03 -0700465 sk_sp<GrFragmentProcessor> fp;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000466 if (unpremul) {
bungeman06ca8ec2016-06-09 08:01:03 -0700467 fp = this->createPMToUPMEffect(src->asTexture(), tempDrawInfo.fSwizzle,
468 textureMatrix);
joshualittb0a8a372014-09-23 09:50:21 -0700469 if (fp) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000470 unpremul = false; // we no longer need to do this on CPU after the read back.
bsalomon39826022015-07-23 08:07:21 -0700471 } else if (GrGpu::kCallerPrefersDraw_DrawPreference == drawPreference) {
472 // We only wanted to do the draw in order to perform the unpremul so don't
473 // bother.
Brian Osman693a5402016-10-27 15:13:22 -0400474 tempRTC.reset(nullptr);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000475 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000476 }
Brian Osman693a5402016-10-27 15:13:22 -0400477 if (!fp && tempRTC) {
bungeman06ca8ec2016-06-09 08:01:03 -0700478 fp = GrConfigConversionEffect::Make(src->asTexture(), tempDrawInfo.fSwizzle,
479 GrConfigConversionEffect::kNone_PMConversion,
480 textureMatrix);
bsalomon39826022015-07-23 08:07:21 -0700481 }
482 if (fp) {
egdanielc4b72722015-11-23 13:20:41 -0800483 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700484 paint.addColorFragmentProcessor(std::move(fp));
reed374772b2016-10-05 17:33:02 -0700485 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
brianosmana167e742016-05-24 06:18:48 -0700486 paint.setAllowSRGBInputs(true);
bsalomon39826022015-07-23 08:07:21 -0700487 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
Brian Salomon0e8fc8b2016-12-09 15:10:07 -0500488 tempRTC->drawRect(GrNoClip(), paint, GrAA::kNo, SkMatrix::I(), rect, nullptr);
Brian Osman693a5402016-10-27 15:13:22 -0400489 surfaceToRead.reset(tempRTC->asTexture().release());
bsalomon39826022015-07-23 08:07:21 -0700490 left = 0;
491 top = 0;
492 didTempDraw = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000493 }
bsalomon@google.com0342a852012-08-20 19:22:38 +0000494 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000495 }
joshualitt5c55fef2014-10-31 14:04:35 -0700496
Robert Phillips833dcf42016-11-18 08:44:13 -0500497 if (!surfaceToRead) {
498 return false;
499 }
500
bsalomon39826022015-07-23 08:07:21 -0700501 if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000502 return false;
503 }
bsalomon39826022015-07-23 08:07:21 -0700504 GrPixelConfig configToRead = dstConfig;
505 if (didTempDraw) {
Hal Canary144caf52016-11-07 17:57:18 -0500506 this->flushSurfaceWrites(surfaceToRead.get());
bsalomon6c9cd552016-01-22 07:17:34 -0800507 configToRead = tempDrawInfo.fReadConfig;
bsalomon39826022015-07-23 08:07:21 -0700508 }
Hal Canary144caf52016-11-07 17:57:18 -0500509 if (!fGpu->readPixels(surfaceToRead.get(), left, top, width, height, configToRead, buffer,
510 rowBytes)) {
bsalomon39826022015-07-23 08:07:21 -0700511 return false;
512 }
513
514 // Perform umpremul conversion if we weren't able to perform it as a draw.
515 if (unpremul) {
reed@google.com7111d462014-03-25 16:20:24 +0000516 SkDstPixelInfo dstPI;
brianosman396fcdb2016-07-22 06:26:11 -0700517 if (!GrPixelConfigToColorType(dstConfig, &dstPI.fColorType)) {
reed@google.com7111d462014-03-25 16:20:24 +0000518 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000519 }
reed@google.com7111d462014-03-25 16:20:24 +0000520 dstPI.fAlphaType = kUnpremul_SkAlphaType;
521 dstPI.fPixels = buffer;
522 dstPI.fRowBytes = rowBytes;
523
524 SkSrcPixelInfo srcPI;
bsalomon39826022015-07-23 08:07:21 -0700525 srcPI.fColorType = dstPI.fColorType;
reed@google.com7111d462014-03-25 16:20:24 +0000526 srcPI.fAlphaType = kPremul_SkAlphaType;
527 srcPI.fPixels = buffer;
528 srcPI.fRowBytes = rowBytes;
529
530 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000531 }
532 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000533}
534
bsalomonc49e8682015-06-30 11:37:35 -0700535void GrContext::prepareSurfaceForExternalIO(GrSurface* surface) {
joshualitt1de610a2016-01-06 08:26:09 -0800536 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800537 RETURN_IF_ABANDONED
bsalomon87a94eb2014-11-03 14:28:32 -0800538 SkASSERT(surface);
539 ASSERT_OWNED_RESOURCE(surface);
bsalomon6a2b1942016-09-08 11:28:59 -0700540 fDrawingManager->prepareSurfaceForExternalIO(surface);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000541}
542
Robert Phillipsd316e772016-12-14 12:04:46 +0000543bool GrContext::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
544 const SkIPoint& dstPoint) {
545 ASSERT_SINGLE_OWNER
546 RETURN_FALSE_IF_ABANDONED
547 GR_AUDIT_TRAIL_AUTO_FRAME(&fAuditTrail, "GrContext::copySurface");
548
549 if (!src || !dst) {
550 return false;
551 }
552 ASSERT_OWNED_RESOURCE(src);
553 ASSERT_OWNED_RESOURCE(dst);
554
555 // We don't allow conversion between integer configs and float/fixed configs.
556 if (GrPixelConfigIsSint(dst->config()) != GrPixelConfigIsSint(src->config())) {
557 return false;
558 }
559
560#ifndef ENABLE_MDB
561 // We can't yet fully defer copies to textures, so GrTextureContext::copySurface will
562 // execute the copy immediately. Ensure the data is ready.
563 src->flushWrites();
564#endif
565
566 sk_sp<GrSurfaceContext> surfaceContext(
567 this->contextPriv().makeWrappedSurfaceContext(sk_ref_sp(dst)));
568
569 if (!surfaceContext) {
570 return false;
571 }
572
573 if (!surfaceContext->copySurface(src, srcRect, dstPoint)) {
574 return false;
575 }
576
577 return true;
578}
579
bsalomonf80bfed2014-10-07 05:56:02 -0700580void GrContext::flushSurfaceWrites(GrSurface* surface) {
joshualitt1de610a2016-01-06 08:26:09 -0800581 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800582 RETURN_IF_ABANDONED
bsalomonf80bfed2014-10-07 05:56:02 -0700583 if (surface->surfacePriv().hasPendingWrite()) {
584 this->flush();
585 }
586}
587
ajuma95243eb2016-08-24 08:19:02 -0700588void GrContext::flushSurfaceIO(GrSurface* surface) {
589 ASSERT_SINGLE_OWNER
590 RETURN_IF_ABANDONED
591 if (surface->surfacePriv().hasPendingIO()) {
592 this->flush();
593 }
594}
595
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500596sk_sp<GrSurfaceContext> GrContextPriv::makeDeferredSurfaceContext(const GrSurfaceDesc& dstDesc,
597 SkBackingFit fit,
598 SkBudgeted isDstBudgeted) {
599
600 sk_sp<GrSurfaceProxy> proxy = GrSurfaceProxy::MakeDeferred(*fContext->caps(), dstDesc,
601 fit, isDstBudgeted);
602
603 if (proxy->asRenderTargetProxy()) {
604 return this->drawingManager()->makeRenderTargetContext(std::move(proxy), nullptr, nullptr);
605 } else {
606 SkASSERT(proxy->asTextureProxy());
607 return this->drawingManager()->makeTextureContext(std::move(proxy));
608 }
609
610 return nullptr;
611}
612
bsalomon@google.com27847de2011-02-22 20:59:41 +0000613////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000614int GrContext::getRecommendedSampleCount(GrPixelConfig config,
615 SkScalar dpi) const {
joshualitt1de610a2016-01-06 08:26:09 -0800616 ASSERT_SINGLE_OWNER
617
bsalomon76228632015-05-29 08:02:10 -0700618 if (!this->caps()->isConfigRenderable(config, true)) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000619 return 0;
620 }
621 int chosenSampleCount = 0;
jvanverthe9c0fc62015-04-29 11:18:05 -0700622 if (fGpu->caps()->shaderCaps()->pathRenderingSupport()) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000623 if (dpi >= 250.0f) {
624 chosenSampleCount = 4;
625 } else {
626 chosenSampleCount = 16;
627 }
628 }
egdanieleed519e2016-01-15 11:36:18 -0800629 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ? chosenSampleCount : 0;
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000630}
631
Brian Osman11052242016-10-27 14:47:55 -0400632sk_sp<GrRenderTargetContext> GrContextPriv::makeWrappedRenderTargetContext(
633 sk_sp<GrRenderTarget> rt,
634 sk_sp<SkColorSpace> colorSpace,
635 const SkSurfaceProps* surfaceProps) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700636 ASSERT_SINGLE_OWNER_PRIV
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400637
Robert Phillips37430132016-11-09 06:50:43 -0500638 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(rt)));
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400639
Robert Phillips37430132016-11-09 06:50:43 -0500640 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400641 std::move(colorSpace),
Brian Osman11052242016-10-27 14:47:55 -0400642 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -0700643}
robertphillips77a2e522015-10-17 07:43:27 -0700644
Brian Osman45580d32016-11-23 09:37:01 -0500645sk_sp<GrSurfaceContext> GrContextPriv::makeWrappedSurfaceContext(sk_sp<GrSurface> surface) {
646 ASSERT_SINGLE_OWNER_PRIV
647
648 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface)));
649
650 if (proxy->asRenderTargetProxy()) {
651 return this->drawingManager()->makeRenderTargetContext(std::move(proxy), nullptr, nullptr);
652 } else {
653 SkASSERT(proxy->asTextureProxy());
654 return this->drawingManager()->makeTextureContext(std::move(proxy));
655 }
656}
657
Brian Osman11052242016-10-27 14:47:55 -0400658sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext(
Robert Phillipse305cc1f2016-12-14 12:19:05 -0500659 const GrBackendTextureDesc& desc,
Brian Osman11052242016-10-27 14:47:55 -0400660 sk_sp<SkColorSpace> colorSpace,
661 const SkSurfaceProps* props,
662 GrWrapOwnership ownership) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700663 ASSERT_SINGLE_OWNER_PRIV
664 SkASSERT(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
665
666 sk_sp<GrSurface> surface(fContext->textureProvider()->wrapBackendTexture(desc, ownership));
667 if (!surface) {
668 return nullptr;
669 }
670
Robert Phillips37430132016-11-09 06:50:43 -0500671 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface)));
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400672
Robert Phillips37430132016-11-09 06:50:43 -0500673 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Brian Osman11052242016-10-27 14:47:55 -0400674 std::move(colorSpace), props);
robertphillips4fd74ae2016-08-03 14:26:53 -0700675}
676
Brian Osman11052242016-10-27 14:47:55 -0400677sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext(
robertphillips4fd74ae2016-08-03 14:26:53 -0700678 const GrBackendRenderTargetDesc& desc,
679 sk_sp<SkColorSpace> colorSpace,
680 const SkSurfaceProps* surfaceProps) {
681 ASSERT_SINGLE_OWNER_PRIV
682
683 sk_sp<GrRenderTarget> rt(fContext->textureProvider()->wrapBackendRenderTarget(desc));
684 if (!rt) {
685 return nullptr;
686 }
687
Robert Phillips37430132016-11-09 06:50:43 -0500688 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(rt)));
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400689
Robert Phillips37430132016-11-09 06:50:43 -0500690 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400691 std::move(colorSpace),
Brian Osman11052242016-10-27 14:47:55 -0400692 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -0700693}
694
Brian Osman11052242016-10-27 14:47:55 -0400695sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext(
egdaniela95d46b2016-08-15 08:06:29 -0700696 const GrBackendTextureDesc& desc,
robertphillips4fd74ae2016-08-03 14:26:53 -0700697 sk_sp<SkColorSpace> colorSpace,
698 const SkSurfaceProps* surfaceProps) {
699 ASSERT_SINGLE_OWNER_PRIV
700 SkASSERT(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
701
702 sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTextureAsRenderTarget(desc));
703 if (!surface) {
704 return nullptr;
705 }
706
Robert Phillips37430132016-11-09 06:50:43 -0500707 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface)));
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400708
Robert Phillips37430132016-11-09 06:50:43 -0500709 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400710 std::move(colorSpace),
711 surfaceProps);
robertphillips77a2e522015-10-17 07:43:27 -0700712}
713
robertphillips48fde9c2016-09-06 05:20:20 -0700714static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) {
715 static const GrPixelConfig kFallback[] = {
716 kUnknown_GrPixelConfig, // kUnknown_GrPixelConfig
717 kRGBA_8888_GrPixelConfig, // kAlpha_8_GrPixelConfig
718 kUnknown_GrPixelConfig, // kIndex_8_GrPixelConfig
719 kRGBA_8888_GrPixelConfig, // kRGB_565_GrPixelConfig
720 kRGBA_8888_GrPixelConfig, // kRGBA_4444_GrPixelConfig
721 kUnknown_GrPixelConfig, // kRGBA_8888_GrPixelConfig
722 kRGBA_8888_GrPixelConfig, // kBGRA_8888_GrPixelConfig
723 kUnknown_GrPixelConfig, // kSRGBA_8888_GrPixelConfig
724 kSRGBA_8888_GrPixelConfig, // kSBGRA_8888_GrPixelConfig
Brian Salomonbf7b6202016-11-11 16:08:03 -0500725 kUnknown_GrPixelConfig, // kRGBA_8888_sint_GrPixelConfig
robertphillips48fde9c2016-09-06 05:20:20 -0700726 kUnknown_GrPixelConfig, // kETC1_GrPixelConfig
727 kUnknown_GrPixelConfig, // kLATC_GrPixelConfig
728 kUnknown_GrPixelConfig, // kR11_EAC_GrPixelConfig
729 kUnknown_GrPixelConfig, // kASTC_12x12_GrPixelConfig
730 kUnknown_GrPixelConfig, // kRGBA_float_GrPixelConfig
731 kRGBA_half_GrPixelConfig, // kAlpha_half_GrPixelConfig
732 kUnknown_GrPixelConfig, // kRGBA_half_GrPixelConfig
733 };
734 return kFallback[config];
735
736 GR_STATIC_ASSERT(0 == kUnknown_GrPixelConfig);
737 GR_STATIC_ASSERT(1 == kAlpha_8_GrPixelConfig);
738 GR_STATIC_ASSERT(2 == kIndex_8_GrPixelConfig);
739 GR_STATIC_ASSERT(3 == kRGB_565_GrPixelConfig);
740 GR_STATIC_ASSERT(4 == kRGBA_4444_GrPixelConfig);
741 GR_STATIC_ASSERT(5 == kRGBA_8888_GrPixelConfig);
742 GR_STATIC_ASSERT(6 == kBGRA_8888_GrPixelConfig);
743 GR_STATIC_ASSERT(7 == kSRGBA_8888_GrPixelConfig);
744 GR_STATIC_ASSERT(8 == kSBGRA_8888_GrPixelConfig);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500745 GR_STATIC_ASSERT(9 == kRGBA_8888_sint_GrPixelConfig);
746 GR_STATIC_ASSERT(10 == kETC1_GrPixelConfig);
747 GR_STATIC_ASSERT(11 == kLATC_GrPixelConfig);
748 GR_STATIC_ASSERT(12 == kR11_EAC_GrPixelConfig);
749 GR_STATIC_ASSERT(13 == kASTC_12x12_GrPixelConfig);
750 GR_STATIC_ASSERT(14 == kRGBA_float_GrPixelConfig);
751 GR_STATIC_ASSERT(15 == kAlpha_half_GrPixelConfig);
752 GR_STATIC_ASSERT(16 == kRGBA_half_GrPixelConfig);
robertphillips48fde9c2016-09-06 05:20:20 -0700753 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kFallback) == kGrPixelConfigCnt);
754}
755
Brian Osman11052242016-10-27 14:47:55 -0400756sk_sp<GrRenderTargetContext> GrContext::makeRenderTargetContextWithFallback(
757 SkBackingFit fit,
758 int width, int height,
759 GrPixelConfig config,
760 sk_sp<SkColorSpace> colorSpace,
761 int sampleCnt,
762 GrSurfaceOrigin origin,
763 const SkSurfaceProps* surfaceProps,
764 SkBudgeted budgeted) {
robertphillips48fde9c2016-09-06 05:20:20 -0700765 if (!this->caps()->isConfigRenderable(config, sampleCnt > 0)) {
766 config = GrPixelConfigFallback(config);
767 }
768
Brian Osman11052242016-10-27 14:47:55 -0400769 return this->makeRenderTargetContext(fit, width, height, config, std::move(colorSpace),
770 sampleCnt, origin, surfaceProps, budgeted);
robertphillips48fde9c2016-09-06 05:20:20 -0700771}
772
robertphillipsd728f0c2016-11-21 11:05:03 -0800773sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContextWithFallback(
774 SkBackingFit fit,
775 int width, int height,
776 GrPixelConfig config,
777 sk_sp<SkColorSpace> colorSpace,
778 int sampleCnt,
779 GrSurfaceOrigin origin,
780 const SkSurfaceProps* surfaceProps,
781 SkBudgeted budgeted) {
782 if (!this->caps()->isConfigRenderable(config, sampleCnt > 0)) {
783 config = GrPixelConfigFallback(config);
784 }
785
786 return this->makeDeferredRenderTargetContext(fit, width, height, config, std::move(colorSpace),
787 sampleCnt, origin, surfaceProps, budgeted);
788}
789
Brian Osman11052242016-10-27 14:47:55 -0400790sk_sp<GrRenderTargetContext> GrContext::makeRenderTargetContext(SkBackingFit fit,
791 int width, int height,
792 GrPixelConfig config,
793 sk_sp<SkColorSpace> colorSpace,
794 int sampleCnt,
795 GrSurfaceOrigin origin,
796 const SkSurfaceProps* surfaceProps,
797 SkBudgeted budgeted) {
robertphillips48fde9c2016-09-06 05:20:20 -0700798 if (!this->caps()->isConfigRenderable(config, sampleCnt > 0)) {
799 return nullptr;
800 }
801
robertphillipsd4c741e2016-04-28 09:55:15 -0700802 GrSurfaceDesc desc;
803 desc.fFlags = kRenderTarget_GrSurfaceFlag;
804 desc.fOrigin = origin;
805 desc.fWidth = width;
806 desc.fHeight = height;
807 desc.fConfig = config;
808 desc.fSampleCnt = sampleCnt;
809
810 sk_sp<GrTexture> tex;
robertphillips76948d42016-05-04 12:47:41 -0700811 if (SkBackingFit::kExact == fit) {
robertphillipsca6eafc2016-05-17 09:57:46 -0700812 tex.reset(this->textureProvider()->createTexture(desc, budgeted));
robertphillipsd4c741e2016-04-28 09:55:15 -0700813 } else {
814 tex.reset(this->textureProvider()->createApproxTexture(desc));
815 }
816 if (!tex) {
817 return nullptr;
818 }
819
Brian Osman11052242016-10-27 14:47:55 -0400820 sk_sp<GrRenderTargetContext> renderTargetContext(
821 this->contextPriv().makeWrappedRenderTargetContext(sk_ref_sp(tex->asRenderTarget()),
robertphillips6738c702016-07-27 12:13:51 -0700822 std::move(colorSpace), surfaceProps));
Brian Osman11052242016-10-27 14:47:55 -0400823 if (!renderTargetContext) {
robertphillipsd4c741e2016-04-28 09:55:15 -0700824 return nullptr;
825 }
826
Brian Osman11052242016-10-27 14:47:55 -0400827 return renderTargetContext;
robertphillipsd4c741e2016-04-28 09:55:15 -0700828}
829
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400830sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContext(
831 SkBackingFit fit,
832 int width, int height,
833 GrPixelConfig config,
834 sk_sp<SkColorSpace> colorSpace,
835 int sampleCnt,
836 GrSurfaceOrigin origin,
837 const SkSurfaceProps* surfaceProps,
838 SkBudgeted budgeted) {
839 GrSurfaceDesc desc;
840 desc.fFlags = kRenderTarget_GrSurfaceFlag;
841 desc.fOrigin = origin;
842 desc.fWidth = width;
843 desc.fHeight = height;
844 desc.fConfig = config;
845 desc.fSampleCnt = sampleCnt;
846
Robert Phillips37430132016-11-09 06:50:43 -0500847 sk_sp<GrSurfaceProxy> rtp = GrSurfaceProxy::MakeDeferred(*this->caps(), desc, fit, budgeted);
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400848
849 return fDrawingManager->makeRenderTargetContext(std::move(rtp),
850 std::move(colorSpace),
851 surfaceProps);
852}
853
joshualitt1de610a2016-01-06 08:26:09 -0800854bool GrContext::abandoned() const {
855 ASSERT_SINGLE_OWNER
robertphillips7761d612016-05-16 09:14:53 -0700856 return fDrawingManager->wasAbandoned();
robertphillips77a2e522015-10-17 07:43:27 -0700857}
858
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000859namespace {
860void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
861 GrConfigConversionEffect::PMConversion pmToUPM;
862 GrConfigConversionEffect::PMConversion upmToPM;
863 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
864 *pmToUPMValue = pmToUPM;
865 *upmToPMValue = upmToPM;
866}
867}
868
bsalomon6c6f6582015-09-10 08:12:46 -0700869void GrContext::testPMConversionsIfNecessary(uint32_t flags) {
joshualitt1de610a2016-01-06 08:26:09 -0800870 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700871 if (SkToBool(kUnpremul_PixelOpsFlag & flags)) {
bsalomon6c6f6582015-09-10 08:12:46 -0700872 if (!fDidTestPMConversions) {
873 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
874 fDidTestPMConversions = true;
875 }
876 }
877}
878
bungeman06ca8ec2016-06-09 08:01:03 -0700879sk_sp<GrFragmentProcessor> GrContext::createPMToUPMEffect(GrTexture* texture,
bsalomon6c9cd552016-01-22 07:17:34 -0800880 const GrSwizzle& swizzle,
bsalomon6c6f6582015-09-10 08:12:46 -0700881 const SkMatrix& matrix) const {
joshualitt1de610a2016-01-06 08:26:09 -0800882 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700883 // We should have already called this->testPMConversionsIfNecessary().
884 SkASSERT(fDidTestPMConversions);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000885 GrConfigConversionEffect::PMConversion pmToUPM =
886 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
887 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bungeman06ca8ec2016-06-09 08:01:03 -0700888 return GrConfigConversionEffect::Make(texture, swizzle, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000889 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700890 return nullptr;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000891 }
892}
893
bungeman06ca8ec2016-06-09 08:01:03 -0700894sk_sp<GrFragmentProcessor> GrContext::createUPMToPMEffect(GrTexture* texture,
bsalomon6c9cd552016-01-22 07:17:34 -0800895 const GrSwizzle& swizzle,
bsalomon6c6f6582015-09-10 08:12:46 -0700896 const SkMatrix& matrix) const {
joshualitt1de610a2016-01-06 08:26:09 -0800897 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700898 // We should have already called this->testPMConversionsIfNecessary().
899 SkASSERT(fDidTestPMConversions);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000900 GrConfigConversionEffect::PMConversion upmToPM =
901 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
902 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
bungeman06ca8ec2016-06-09 08:01:03 -0700903 return GrConfigConversionEffect::Make(texture, swizzle, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000904 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700905 return nullptr;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000906 }
907}
908
bsalomon636e8022015-07-29 06:08:46 -0700909bool GrContext::didFailPMUPMConversionTest() const {
joshualitt1de610a2016-01-06 08:26:09 -0800910 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700911 // We should have already called this->testPMConversionsIfNecessary().
912 SkASSERT(fDidTestPMConversions);
bsalomon636e8022015-07-29 06:08:46 -0700913 // The PM<->UPM tests fail or succeed together so we only need to check one.
bsalomon6c6f6582015-09-10 08:12:46 -0700914 return GrConfigConversionEffect::kNone_PMConversion == fPMToUPMConversion;
bsalomon636e8022015-07-29 06:08:46 -0700915}
916
bsalomon37f9a262015-02-02 13:00:10 -0800917//////////////////////////////////////////////////////////////////////////////
918
919void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800920 ASSERT_SINGLE_OWNER
bsalomon37f9a262015-02-02 13:00:10 -0800921 if (maxTextures) {
bsalomon0ea80f42015-02-11 10:49:59 -0800922 *maxTextures = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -0800923 }
924 if (maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800925 *maxTextureBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -0800926 }
927}
928
929void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
joshualitt1de610a2016-01-06 08:26:09 -0800930 ASSERT_SINGLE_OWNER
bsalomon0ea80f42015-02-11 10:49:59 -0800931 fResourceCache->setLimits(maxTextures, maxTextureBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800932}
933
ericrk0a5fa482015-09-15 14:16:10 -0700934//////////////////////////////////////////////////////////////////////////////
935
936void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -0800937 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -0700938 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
939}