blob: 1a5ee20721ef5bf09f48d7d5f55b75859c5d8745 [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"
bsalomonafbf2d62014-09-30 12:18:44 -070017#include "GrSurfacePriv.h"
robertphillips3dc6ae52015-10-20 09:54:32 -070018
bsalomon81beccc2014-10-13 12:32:55 -070019#include "SkConfig8888.h"
bsalomonf276ac52015-10-09 13:36:42 -070020#include "SkGrPriv.h"
joshualitt74417822015-08-07 11:42:16 -070021
bsalomonb8fea972016-02-16 07:34:17 -080022#include "batches/GrCopySurfaceBatch.h"
joshualitt5478d422014-11-14 16:00:38 -080023#include "effects/GrConfigConversionEffect.h"
brianosman2d1ee792016-05-05 12:24:31 -070024#include "effects/GrGammaEffect.h"
joshualitte8042922015-12-11 06:11:21 -080025#include "text/GrTextBlobCache.h"
joshualitt5478d422014-11-14 16:00:38 -080026
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000027#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
joshualitt1de610a2016-01-06 08:26:09 -080028#define ASSERT_SINGLE_OWNER \
29 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fSingleOwner);)
robertphillips4fd74ae2016-08-03 14:26:53 -070030#define ASSERT_SINGLE_OWNER_PRIV \
31 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fContext->fSingleOwner);)
robertphillips7761d612016-05-16 09:14:53 -070032#define RETURN_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return; }
33#define RETURN_FALSE_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return false; }
34#define RETURN_NULL_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return nullptr; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000035
robertphillipsea461502015-05-26 11:38:03 -070036////////////////////////////////////////////////////////////////////////////////
37
bsalomon682c2692015-05-22 14:01:46 -070038GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext) {
39 GrContextOptions defaultOptions;
40 return Create(backend, backendContext, defaultOptions);
41}
bsalomonf28cff72015-05-22 12:25:41 -070042
bsalomon682c2692015-05-22 14:01:46 -070043GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
44 const GrContextOptions& options) {
halcanary385fe4d2015-08-26 13:07:48 -070045 GrContext* context = new GrContext;
bsalomon682c2692015-05-22 14:01:46 -070046
47 if (context->init(backend, backendContext, options)) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000048 return context;
49 } else {
50 context->unref();
halcanary96fcdcc2015-08-27 07:41:13 -070051 return nullptr;
bsalomon@google.com27847de2011-02-22 20:59:41 +000052 }
bsalomon@google.com27847de2011-02-22 20:59:41 +000053}
54
joshualitt0acd0d32015-05-07 08:23:19 -070055static int32_t gNextID = 1;
56static int32_t next_id() {
57 int32_t id;
58 do {
59 id = sk_atomic_inc(&gNextID);
60 } while (id == SK_InvalidGenID);
61 return id;
62}
63
bsalomon682c2692015-05-22 14:01:46 -070064GrContext::GrContext() : fUniqueID(next_id()) {
halcanary96fcdcc2015-08-27 07:41:13 -070065 fGpu = nullptr;
66 fCaps = nullptr;
67 fResourceCache = nullptr;
68 fResourceProvider = nullptr;
halcanary96fcdcc2015-08-27 07:41:13 -070069 fBatchFontCache = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000070}
71
bsalomon682c2692015-05-22 14:01:46 -070072bool GrContext::init(GrBackend backend, GrBackendContext backendContext,
73 const GrContextOptions& options) {
joshualitt1de610a2016-01-06 08:26:09 -080074 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -070075 SkASSERT(!fGpu);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000076
bsalomon682c2692015-05-22 14:01:46 -070077 fGpu = GrGpu::Create(backend, backendContext, options, this);
robertphillipsea461502015-05-26 11:38:03 -070078 if (!fGpu) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000079 return false;
80 }
bsalomon69cfe952015-11-30 13:27:47 -080081 this->initCommon(options);
bsalomon33435572014-11-05 14:47:41 -080082 return true;
83}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000084
bsalomon69cfe952015-11-30 13:27:47 -080085void GrContext::initCommon(const GrContextOptions& options) {
joshualitt1de610a2016-01-06 08:26:09 -080086 ASSERT_SINGLE_OWNER
87
bsalomon76228632015-05-29 08:02:10 -070088 fCaps = SkRef(fGpu->caps());
halcanary385fe4d2015-08-26 13:07:48 -070089 fResourceCache = new GrResourceCache(fCaps);
joshualitt6d0872d2016-01-11 08:27:48 -080090 fResourceProvider = new GrResourceProvider(fGpu, fResourceCache, &fSingleOwner);
commit-bot@chromium.org1836d332013-07-16 22:55:03 +000091
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000092 fDidTestPMConversions = false;
93
Robert Phillipsf2361d22016-10-25 14:20:06 -040094 GrRenderTargetOpList::Options rtOpListOptions;
95 rtOpListOptions.fClipBatchToBounds = options.fClipBatchToBounds;
96 rtOpListOptions.fDrawBatchBounds = options.fDrawBatchBounds;
97 rtOpListOptions.fMaxBatchLookback = options.fMaxBatchLookback;
98 rtOpListOptions.fMaxBatchLookahead = options.fMaxBatchLookahead;
bsalomon6b2552f2016-09-15 13:50:26 -070099 GrPathRendererChain::Options prcOptions;
100 prcOptions.fDisableDistanceFieldRenderer = options.fDisableDistanceFieldPaths;
bsalomon39ef7fb2016-09-21 11:16:05 -0700101 prcOptions.fAllowPathMaskCaching = options.fAllowPathMaskCaching;
102 prcOptions.fDisableAllPathRenderers = options.fForceSWPathMasks;
Robert Phillipsf2361d22016-10-25 14:20:06 -0400103 fDrawingManager.reset(new GrDrawingManager(this, rtOpListOptions, prcOptions,
104 options.fImmediateMode, &fSingleOwner));
joshualitt7c3a2f82015-03-31 13:32:05 -0700105
106 // GrBatchFontCache will eventually replace GrFontCache
halcanary385fe4d2015-08-26 13:07:48 -0700107 fBatchFontCache = new GrBatchFontCache(this);
joshualittb7133be2015-04-08 09:08:31 -0700108
halcanary385fe4d2015-08-26 13:07:48 -0700109 fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB, this));
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000110}
111
bsalomon@google.com27847de2011-02-22 20:59:41 +0000112GrContext::~GrContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800113 ASSERT_SINGLE_OWNER
114
robertphillipsea461502015-05-26 11:38:03 -0700115 if (!fGpu) {
bsalomon76228632015-05-29 08:02:10 -0700116 SkASSERT(!fCaps);
bsalomon@google.com733c0622013-04-24 17:59:32 +0000117 return;
118 }
119
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000120 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000121
robertphillips77a2e522015-10-17 07:43:27 -0700122 fDrawingManager->cleanup();
robertphillips2334fb62015-06-17 05:43:33 -0700123
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000124 for (int i = 0; i < fCleanUpData.count(); ++i) {
125 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
126 }
127
halcanary385fe4d2015-08-26 13:07:48 -0700128 delete fResourceProvider;
129 delete fResourceCache;
130 delete fBatchFontCache;
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000131
bsalomon@google.com205d4602011-04-25 12:43:45 +0000132 fGpu->unref();
bsalomon76228632015-05-29 08:02:10 -0700133 fCaps->unref();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000134}
135
bungeman6bd52842016-10-27 09:30:08 -0700136sk_sp<GrContextThreadSafeProxy> GrContext::threadSafeProxy() {
bsalomon41b952c2016-03-11 06:46:33 -0800137 if (!fThreadSafeProxy) {
bungeman6bd52842016-10-27 09:30:08 -0700138 fThreadSafeProxy.reset(new GrContextThreadSafeProxy(sk_ref_sp(fCaps), this->uniqueID()));
bsalomon41b952c2016-03-11 06:46:33 -0800139 }
bungeman6bd52842016-10-27 09:30:08 -0700140 return fThreadSafeProxy;
bsalomon41b952c2016-03-11 06:46:33 -0800141}
142
bsalomon2354f842014-07-28 13:48:36 -0700143void GrContext::abandonContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800144 ASSERT_SINGLE_OWNER
145
bsalomond309e7a2015-04-30 14:18:54 -0700146 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800147
148 // Need to abandon the drawing manager first so all the render targets
149 // will be released/forgotten before they too are abandoned.
150 fDrawingManager->abandon();
151
bsalomon@google.com205d4602011-04-25 12:43:45 +0000152 // abandon first to so destructors
153 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800154 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700155
bsalomon6e2aad42016-04-01 11:54:31 -0700156 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
157
158 fBatchFontCache->freeAll();
bsalomon6e2aad42016-04-01 11:54:31 -0700159 fTextBlobCache->freeAll();
160}
161
162void GrContext::releaseResourcesAndAbandonContext() {
163 ASSERT_SINGLE_OWNER
164
165 fResourceProvider->abandon();
166
167 // Need to abandon the drawing manager first so all the render targets
168 // will be released/forgotten before they too are abandoned.
169 fDrawingManager->abandon();
170
171 // Release all resources in the backend 3D API.
172 fResourceCache->releaseAll();
173
174 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000175
joshualitt7c3a2f82015-03-31 13:32:05 -0700176 fBatchFontCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700177 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000178}
179
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000180void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800181 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000182 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000183}
184
185void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800186 ASSERT_SINGLE_OWNER
187
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000188 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000189
joshualitt7c3a2f82015-03-31 13:32:05 -0700190 fBatchFontCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700191
192 fDrawingManager->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700193
194 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000195}
196
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000197void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800198 ASSERT_SINGLE_OWNER
199
bsalomon71cb0c22014-11-14 12:10:14 -0800200 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800201 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800202 }
203 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800204 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800205 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000206}
207
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000208////////////////////////////////////////////////////////////////////////////////
209
joshualitt0db6dfa2015-04-10 07:01:30 -0700210void GrContext::TextBlobCacheOverBudgetCB(void* data) {
211 SkASSERT(data);
Brian Osman11052242016-10-27 14:47:55 -0400212 // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on
213 // GrRenderTargetContext to perform a necessary flush. The solution is to move drawText calls
214 // to below the GrContext level, but this is not trivial because they call drawPath on
215 // SkGpuDevice.
joshualitt0db6dfa2015-04-10 07:01:30 -0700216 GrContext* context = reinterpret_cast<GrContext*>(data);
217 context->flush();
218}
219
bsalomon@google.com27847de2011-02-22 20:59:41 +0000220////////////////////////////////////////////////////////////////////////////////
221
bsalomonb77a9072016-09-07 10:02:04 -0700222void GrContext::flush() {
joshualitt1de610a2016-01-06 08:26:09 -0800223 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700224 RETURN_IF_ABANDONED
bsalomonb77a9072016-09-07 10:02:04 -0700225 fDrawingManager->flush();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000226}
227
bsalomon81beccc2014-10-13 12:32:55 -0700228bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
229 const void* inPixels, size_t outRowBytes, void* outPixels) {
230 SkSrcPixelInfo srcPI;
brianosman396fcdb2016-07-22 06:26:11 -0700231 if (!GrPixelConfigToColorType(srcConfig, &srcPI.fColorType)) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000232 return false;
233 }
bsalomon81beccc2014-10-13 12:32:55 -0700234 srcPI.fAlphaType = kUnpremul_SkAlphaType;
235 srcPI.fPixels = inPixels;
236 srcPI.fRowBytes = inRowBytes;
237
238 SkDstPixelInfo dstPI;
239 dstPI.fColorType = srcPI.fColorType;
240 dstPI.fAlphaType = kPremul_SkAlphaType;
241 dstPI.fPixels = outPixels;
242 dstPI.fRowBytes = outRowBytes;
243
244 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000245}
246
bsalomon81beccc2014-10-13 12:32:55 -0700247bool GrContext::writeSurfacePixels(GrSurface* surface,
248 int left, int top, int width, int height,
249 GrPixelConfig srcConfig, const void* buffer, size_t rowBytes,
250 uint32_t pixelOpsFlags) {
joshualitt1de610a2016-01-06 08:26:09 -0800251 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800252 RETURN_FALSE_IF_ABANDONED
bsalomon6c6f6582015-09-10 08:12:46 -0700253 ASSERT_OWNED_RESOURCE(surface);
254 SkASSERT(surface);
joshualittbc907352016-01-13 06:45:40 -0800255 GR_AUDIT_TRAIL_AUTO_FRAME(&fAuditTrail, "GrContext::writeSurfacePixels");
bsalomon6c6f6582015-09-10 08:12:46 -0700256
257 this->testPMConversionsIfNecessary(pixelOpsFlags);
bsalomon81beccc2014-10-13 12:32:55 -0700258
bsalomone8d21e82015-07-16 08:23:13 -0700259 // 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 -0700260 // necessary and because GrGpu::getWritePixelsInfo requires it.
bsalomone8d21e82015-07-16 08:23:13 -0700261 if (!GrSurfacePriv::AdjustWritePixelParams(surface->width(), surface->height(),
262 GrBytesPerPixel(srcConfig), &left, &top, &width,
263 &height, &buffer, &rowBytes)) {
264 return false;
265 }
266
bsalomonf0674512015-07-28 13:26:15 -0700267 bool applyPremulToSrc = false;
bsalomon81beccc2014-10-13 12:32:55 -0700268 if (kUnpremul_PixelOpsFlag & pixelOpsFlags) {
Brian Salomonbf7b6202016-11-11 16:08:03 -0500269 if (!GrPixelConfigIs8888Unorm(srcConfig)) {
bsalomon81beccc2014-10-13 12:32:55 -0700270 return false;
271 }
bsalomonf0674512015-07-28 13:26:15 -0700272 applyPremulToSrc = true;
273 }
Brian Salomonbf7b6202016-11-11 16:08:03 -0500274 // We don't allow conversion between integer configs and float/fixed configs.
275 if (GrPixelConfigIsSint(surface->config()) != GrPixelConfigIsSint(srcConfig)) {
276 return false;
277 }
bsalomon636e8022015-07-29 06:08:46 -0700278
279 GrGpu::DrawPreference drawPreference = GrGpu::kNoDraw_DrawPreference;
280 // Don't prefer to draw for the conversion (and thereby access a texture from the cache) when
281 // we've already determined that there isn't a roundtrip preserving conversion processor pair.
282 if (applyPremulToSrc && !this->didFailPMUPMConversionTest()) {
283 drawPreference = GrGpu::kCallerPrefersDraw_DrawPreference;
284 }
285
bsalomonf0674512015-07-28 13:26:15 -0700286 GrGpu::WritePixelTempDrawInfo tempDrawInfo;
cblumeed828002016-02-16 13:00:01 -0800287 if (!fGpu->getWritePixelsInfo(surface, width, height, srcConfig, &drawPreference,
bsalomonf0674512015-07-28 13:26:15 -0700288 &tempDrawInfo)) {
289 return false;
290 }
291
292 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && surface->surfacePriv().hasPendingIO()) {
293 this->flush();
294 }
295
Hal Canary144caf52016-11-07 17:57:18 -0500296 sk_sp<GrTexture> tempTexture;
bsalomonf0674512015-07-28 13:26:15 -0700297 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomoneae62002015-07-31 13:59:30 -0700298 tempTexture.reset(
299 this->textureProvider()->createApproxTexture(tempDrawInfo.fTempSurfaceDesc));
bsalomonf0674512015-07-28 13:26:15 -0700300 if (!tempTexture && GrGpu::kRequireDraw_DrawPreference == drawPreference) {
301 return false;
302 }
303 }
304
305 // temp buffer for doing sw premul conversion, if needed.
306 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
307 if (tempTexture) {
bungeman06ca8ec2016-06-09 08:01:03 -0700308 sk_sp<GrFragmentProcessor> fp;
bsalomonf0674512015-07-28 13:26:15 -0700309 SkMatrix textureMatrix;
310 textureMatrix.setIDiv(tempTexture->width(), tempTexture->height());
bsalomonf0674512015-07-28 13:26:15 -0700311 if (applyPremulToSrc) {
Hal Canary144caf52016-11-07 17:57:18 -0500312 fp = this->createUPMToPMEffect(tempTexture.get(), tempDrawInfo.fSwizzle, textureMatrix);
bsalomonf0674512015-07-28 13:26:15 -0700313 // If premultiplying was the only reason for the draw, fall back to a straight write.
314 if (!fp) {
315 if (GrGpu::kCallerPrefersDraw_DrawPreference == drawPreference) {
halcanary96fcdcc2015-08-27 07:41:13 -0700316 tempTexture.reset(nullptr);
bsalomonf0674512015-07-28 13:26:15 -0700317 }
318 } else {
319 applyPremulToSrc = false;
320 }
321 }
322 if (tempTexture) {
323 if (!fp) {
Hal Canary144caf52016-11-07 17:57:18 -0500324 fp = GrConfigConversionEffect::Make(tempTexture.get(), tempDrawInfo.fSwizzle,
bungeman06ca8ec2016-06-09 08:01:03 -0700325 GrConfigConversionEffect::kNone_PMConversion,
326 textureMatrix);
bsalomonf0674512015-07-28 13:26:15 -0700327 if (!fp) {
328 return false;
329 }
330 }
331 GrRenderTarget* renderTarget = surface->asRenderTarget();
332 SkASSERT(renderTarget);
333 if (tempTexture->surfacePriv().hasPendingIO()) {
334 this->flush();
335 }
336 if (applyPremulToSrc) {
337 size_t tmpRowBytes = 4 * width;
338 tmpPixels.reset(width * height);
339 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
340 tmpPixels.get())) {
341 return false;
342 }
343 rowBytes = tmpRowBytes;
344 buffer = tmpPixels.get();
345 applyPremulToSrc = false;
346 }
Hal Canary144caf52016-11-07 17:57:18 -0500347 if (!fGpu->writePixels(tempTexture.get(), 0, 0, width, height,
bsalomon6c9cd552016-01-22 07:17:34 -0800348 tempDrawInfo.fWriteConfig, buffer,
bsalomon6cb3cbe2015-07-30 07:34:27 -0700349 rowBytes)) {
bsalomonf0674512015-07-28 13:26:15 -0700350 return false;
351 }
352 SkMatrix matrix;
353 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
brianosmandfe4f2e2016-07-21 13:28:36 -0700354 // TODO: Need to decide the semantics of this function for color spaces. Do we support
355 // conversion from a passed-in color space? For now, specifying nullptr means that this
356 // path will do no conversion, so it will match the behavior of the non-draw path.
Brian Osman11052242016-10-27 14:47:55 -0400357 sk_sp<GrRenderTargetContext> renderTargetContext(
358 this->contextPriv().makeWrappedRenderTargetContext(sk_ref_sp(renderTarget),
359 nullptr));
360 if (!renderTargetContext) {
bsalomonf0674512015-07-28 13:26:15 -0700361 return false;
362 }
egdanielc4b72722015-11-23 13:20:41 -0800363 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700364 paint.addColorFragmentProcessor(std::move(fp));
reed374772b2016-10-05 17:33:02 -0700365 paint.setPorterDuffXPFactory(SkBlendMode::kSrc);
brianosmana167e742016-05-24 06:18:48 -0700366 paint.setAllowSRGBInputs(true);
bsalomonf0674512015-07-28 13:26:15 -0700367 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
Brian Osman11052242016-10-27 14:47:55 -0400368 renderTargetContext->drawRect(GrNoClip(), paint, matrix, rect, nullptr);
bsalomonf0674512015-07-28 13:26:15 -0700369
370 if (kFlushWrites_PixelOp & pixelOpsFlags) {
371 this->flushSurfaceWrites(surface);
372 }
373 }
374 }
375 if (!tempTexture) {
bsalomonf0674512015-07-28 13:26:15 -0700376 if (applyPremulToSrc) {
bsalomon81beccc2014-10-13 12:32:55 -0700377 size_t tmpRowBytes = 4 * width;
378 tmpPixels.reset(width * height);
379 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
380 tmpPixels.get())) {
381 return false;
382 }
383 rowBytes = tmpRowBytes;
384 buffer = tmpPixels.get();
bsalomonf0674512015-07-28 13:26:15 -0700385 applyPremulToSrc = false;
bsalomon81beccc2014-10-13 12:32:55 -0700386 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700387 return fGpu->writePixels(surface, left, top, width, height, srcConfig, buffer, rowBytes);
bsalomon81beccc2014-10-13 12:32:55 -0700388 }
bsalomon81beccc2014-10-13 12:32:55 -0700389 return true;
390}
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000391
bsalomone8d21e82015-07-16 08:23:13 -0700392bool GrContext::readSurfacePixels(GrSurface* src,
393 int left, int top, int width, int height,
394 GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
395 uint32_t flags) {
joshualitt1de610a2016-01-06 08:26:09 -0800396 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800397 RETURN_FALSE_IF_ABANDONED
bsalomone8d21e82015-07-16 08:23:13 -0700398 ASSERT_OWNED_RESOURCE(src);
399 SkASSERT(src);
joshualittbc907352016-01-13 06:45:40 -0800400 GR_AUDIT_TRAIL_AUTO_FRAME(&fAuditTrail, "GrContext::readSurfacePixels");
bsalomon32ab2602015-09-09 18:57:49 -0700401
bsalomon6c6f6582015-09-10 08:12:46 -0700402 this->testPMConversionsIfNecessary(flags);
403 SkAutoMutexAcquire ama(fReadPixelsMutex);
404
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 Osman693a5402016-10-27 15:13:22 -0400488 tempRTC->drawRect(GrNoClip(), paint, SkMatrix::I(), rect, nullptr);
489 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
bsalomonb8fea972016-02-16 07:34:17 -0800543bool GrContext::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
544 const SkIPoint& dstPoint) {
joshualitt1de610a2016-01-06 08:26:09 -0800545 ASSERT_SINGLE_OWNER
bsalomonb8fea972016-02-16 07:34:17 -0800546 RETURN_FALSE_IF_ABANDONED
joshualittbc907352016-01-13 06:45:40 -0800547 GR_AUDIT_TRAIL_AUTO_FRAME(&fAuditTrail, "GrContext::copySurface");
548
robertphillipsea461502015-05-26 11:38:03 -0700549 if (!src || !dst) {
bsalomonb8fea972016-02-16 07:34:17 -0800550 return false;
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000551 }
bsalomone3d4bf22014-09-23 09:15:03 -0700552 ASSERT_OWNED_RESOURCE(src);
junov2bb52102014-09-29 10:18:59 -0700553 ASSERT_OWNED_RESOURCE(dst);
Brian Salomon34a98952014-09-24 11:41:24 -0400554
Brian Salomonbf7b6202016-11-11 16:08:03 -0500555 // 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
robertphillipsea461502015-05-26 11:38:03 -0700560 if (!dst->asRenderTarget()) {
bsalomonb8fea972016-02-16 07:34:17 -0800561 SkIRect clippedSrcRect;
562 SkIPoint clippedDstPoint;
563 if (!GrCopySurfaceBatch::ClipSrcRectAndDstPoint(dst, src, srcRect, dstPoint,
564 &clippedSrcRect, &clippedDstPoint)) {
565 return false;
566 }
Brian Osman11052242016-10-27 14:47:55 -0400567 // If we don't have an RT for the dst then we won't have a GrRenderTargetContext to insert
bsalomonb8fea972016-02-16 07:34:17 -0800568 // the copy surface into. In the future we plan to have a more limited Context type
Brian Osman11052242016-10-27 14:47:55 -0400569 // (GrCopyContext?) that has the subset of GrRenderTargetContext operations that should be
bsalomonb8fea972016-02-16 07:34:17 -0800570 // allowed on textures that aren't render targets.
571 // For now we just flush any writes to the src and issue an immediate copy to the dst.
572 src->flushWrites();
573 return fGpu->copySurface(dst, src, clippedSrcRect, clippedDstPoint);
robertphillipsea461502015-05-26 11:38:03 -0700574 }
Brian Osman11052242016-10-27 14:47:55 -0400575 sk_sp<GrRenderTargetContext> renderTargetContext(
576 this->contextPriv().makeWrappedRenderTargetContext(sk_ref_sp(dst->asRenderTarget()),
577 nullptr));
578 if (!renderTargetContext) {
bsalomonb8fea972016-02-16 07:34:17 -0800579 return false;
bsalomonf80bfed2014-10-07 05:56:02 -0700580 }
kjlubick0eed9452016-02-11 12:05:24 -0800581
Brian Osman11052242016-10-27 14:47:55 -0400582 if (!renderTargetContext->copySurface(src, srcRect, dstPoint)) {
bsalomonb8fea972016-02-16 07:34:17 -0800583 return false;
kjlubick0eed9452016-02-11 12:05:24 -0800584 }
bsalomonb8fea972016-02-16 07:34:17 -0800585 return true;
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000586}
587
bsalomonf80bfed2014-10-07 05:56:02 -0700588void GrContext::flushSurfaceWrites(GrSurface* surface) {
joshualitt1de610a2016-01-06 08:26:09 -0800589 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800590 RETURN_IF_ABANDONED
bsalomonf80bfed2014-10-07 05:56:02 -0700591 if (surface->surfacePriv().hasPendingWrite()) {
592 this->flush();
593 }
594}
595
ajuma95243eb2016-08-24 08:19:02 -0700596void GrContext::flushSurfaceIO(GrSurface* surface) {
597 ASSERT_SINGLE_OWNER
598 RETURN_IF_ABANDONED
599 if (surface->surfacePriv().hasPendingIO()) {
600 this->flush();
601 }
602}
603
bsalomon@google.com27847de2011-02-22 20:59:41 +0000604////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000605int GrContext::getRecommendedSampleCount(GrPixelConfig config,
606 SkScalar dpi) const {
joshualitt1de610a2016-01-06 08:26:09 -0800607 ASSERT_SINGLE_OWNER
608
bsalomon76228632015-05-29 08:02:10 -0700609 if (!this->caps()->isConfigRenderable(config, true)) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000610 return 0;
611 }
612 int chosenSampleCount = 0;
jvanverthe9c0fc62015-04-29 11:18:05 -0700613 if (fGpu->caps()->shaderCaps()->pathRenderingSupport()) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000614 if (dpi >= 250.0f) {
615 chosenSampleCount = 4;
616 } else {
617 chosenSampleCount = 16;
618 }
619 }
egdanieleed519e2016-01-15 11:36:18 -0800620 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ? chosenSampleCount : 0;
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000621}
622
Brian Osman11052242016-10-27 14:47:55 -0400623sk_sp<GrRenderTargetContext> GrContextPriv::makeWrappedRenderTargetContext(
624 sk_sp<GrRenderTarget> rt,
625 sk_sp<SkColorSpace> colorSpace,
626 const SkSurfaceProps* surfaceProps) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700627 ASSERT_SINGLE_OWNER_PRIV
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400628
Robert Phillips37430132016-11-09 06:50:43 -0500629 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(rt)));
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400630
Robert Phillips37430132016-11-09 06:50:43 -0500631 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400632 std::move(colorSpace),
Brian Osman11052242016-10-27 14:47:55 -0400633 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -0700634}
robertphillips77a2e522015-10-17 07:43:27 -0700635
Brian Osman11052242016-10-27 14:47:55 -0400636sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureRenderTargetContext(
637 const GrBackendTextureDesc& desc,
638 sk_sp<SkColorSpace> colorSpace,
639 const SkSurfaceProps* props,
640 GrWrapOwnership ownership) {
robertphillips4fd74ae2016-08-03 14:26:53 -0700641 ASSERT_SINGLE_OWNER_PRIV
642 SkASSERT(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
643
644 sk_sp<GrSurface> surface(fContext->textureProvider()->wrapBackendTexture(desc, ownership));
645 if (!surface) {
646 return nullptr;
647 }
648
Robert Phillips37430132016-11-09 06:50:43 -0500649 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface)));
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400650
Robert Phillips37430132016-11-09 06:50:43 -0500651 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Brian Osman11052242016-10-27 14:47:55 -0400652 std::move(colorSpace), props);
robertphillips4fd74ae2016-08-03 14:26:53 -0700653}
654
Brian Osman11052242016-10-27 14:47:55 -0400655sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendRenderTargetRenderTargetContext(
robertphillips4fd74ae2016-08-03 14:26:53 -0700656 const GrBackendRenderTargetDesc& desc,
657 sk_sp<SkColorSpace> colorSpace,
658 const SkSurfaceProps* surfaceProps) {
659 ASSERT_SINGLE_OWNER_PRIV
660
661 sk_sp<GrRenderTarget> rt(fContext->textureProvider()->wrapBackendRenderTarget(desc));
662 if (!rt) {
663 return nullptr;
664 }
665
Robert Phillips37430132016-11-09 06:50:43 -0500666 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(rt)));
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400667
Robert Phillips37430132016-11-09 06:50:43 -0500668 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400669 std::move(colorSpace),
Brian Osman11052242016-10-27 14:47:55 -0400670 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -0700671}
672
Brian Osman11052242016-10-27 14:47:55 -0400673sk_sp<GrRenderTargetContext> GrContextPriv::makeBackendTextureAsRenderTargetRenderTargetContext(
egdaniela95d46b2016-08-15 08:06:29 -0700674 const GrBackendTextureDesc& desc,
robertphillips4fd74ae2016-08-03 14:26:53 -0700675 sk_sp<SkColorSpace> colorSpace,
676 const SkSurfaceProps* surfaceProps) {
677 ASSERT_SINGLE_OWNER_PRIV
678 SkASSERT(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
679
680 sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTextureAsRenderTarget(desc));
681 if (!surface) {
682 return nullptr;
683 }
684
Robert Phillips37430132016-11-09 06:50:43 -0500685 sk_sp<GrSurfaceProxy> proxy(GrSurfaceProxy::MakeWrapped(std::move(surface)));
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400686
Robert Phillips37430132016-11-09 06:50:43 -0500687 return this->drawingManager()->makeRenderTargetContext(std::move(proxy),
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400688 std::move(colorSpace),
689 surfaceProps);
robertphillips77a2e522015-10-17 07:43:27 -0700690}
691
robertphillips48fde9c2016-09-06 05:20:20 -0700692static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) {
693 static const GrPixelConfig kFallback[] = {
694 kUnknown_GrPixelConfig, // kUnknown_GrPixelConfig
695 kRGBA_8888_GrPixelConfig, // kAlpha_8_GrPixelConfig
696 kUnknown_GrPixelConfig, // kIndex_8_GrPixelConfig
697 kRGBA_8888_GrPixelConfig, // kRGB_565_GrPixelConfig
698 kRGBA_8888_GrPixelConfig, // kRGBA_4444_GrPixelConfig
699 kUnknown_GrPixelConfig, // kRGBA_8888_GrPixelConfig
700 kRGBA_8888_GrPixelConfig, // kBGRA_8888_GrPixelConfig
701 kUnknown_GrPixelConfig, // kSRGBA_8888_GrPixelConfig
702 kSRGBA_8888_GrPixelConfig, // kSBGRA_8888_GrPixelConfig
Brian Salomonbf7b6202016-11-11 16:08:03 -0500703 kUnknown_GrPixelConfig, // kRGBA_8888_sint_GrPixelConfig
robertphillips48fde9c2016-09-06 05:20:20 -0700704 kUnknown_GrPixelConfig, // kETC1_GrPixelConfig
705 kUnknown_GrPixelConfig, // kLATC_GrPixelConfig
706 kUnknown_GrPixelConfig, // kR11_EAC_GrPixelConfig
707 kUnknown_GrPixelConfig, // kASTC_12x12_GrPixelConfig
708 kUnknown_GrPixelConfig, // kRGBA_float_GrPixelConfig
709 kRGBA_half_GrPixelConfig, // kAlpha_half_GrPixelConfig
710 kUnknown_GrPixelConfig, // kRGBA_half_GrPixelConfig
711 };
712 return kFallback[config];
713
714 GR_STATIC_ASSERT(0 == kUnknown_GrPixelConfig);
715 GR_STATIC_ASSERT(1 == kAlpha_8_GrPixelConfig);
716 GR_STATIC_ASSERT(2 == kIndex_8_GrPixelConfig);
717 GR_STATIC_ASSERT(3 == kRGB_565_GrPixelConfig);
718 GR_STATIC_ASSERT(4 == kRGBA_4444_GrPixelConfig);
719 GR_STATIC_ASSERT(5 == kRGBA_8888_GrPixelConfig);
720 GR_STATIC_ASSERT(6 == kBGRA_8888_GrPixelConfig);
721 GR_STATIC_ASSERT(7 == kSRGBA_8888_GrPixelConfig);
722 GR_STATIC_ASSERT(8 == kSBGRA_8888_GrPixelConfig);
Brian Salomonbf7b6202016-11-11 16:08:03 -0500723 GR_STATIC_ASSERT(9 == kRGBA_8888_sint_GrPixelConfig);
724 GR_STATIC_ASSERT(10 == kETC1_GrPixelConfig);
725 GR_STATIC_ASSERT(11 == kLATC_GrPixelConfig);
726 GR_STATIC_ASSERT(12 == kR11_EAC_GrPixelConfig);
727 GR_STATIC_ASSERT(13 == kASTC_12x12_GrPixelConfig);
728 GR_STATIC_ASSERT(14 == kRGBA_float_GrPixelConfig);
729 GR_STATIC_ASSERT(15 == kAlpha_half_GrPixelConfig);
730 GR_STATIC_ASSERT(16 == kRGBA_half_GrPixelConfig);
robertphillips48fde9c2016-09-06 05:20:20 -0700731 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kFallback) == kGrPixelConfigCnt);
732}
733
Brian Osman11052242016-10-27 14:47:55 -0400734sk_sp<GrRenderTargetContext> GrContext::makeRenderTargetContextWithFallback(
735 SkBackingFit fit,
736 int width, int height,
737 GrPixelConfig config,
738 sk_sp<SkColorSpace> colorSpace,
739 int sampleCnt,
740 GrSurfaceOrigin origin,
741 const SkSurfaceProps* surfaceProps,
742 SkBudgeted budgeted) {
robertphillips48fde9c2016-09-06 05:20:20 -0700743 if (!this->caps()->isConfigRenderable(config, sampleCnt > 0)) {
744 config = GrPixelConfigFallback(config);
745 }
746
Brian Osman11052242016-10-27 14:47:55 -0400747 return this->makeRenderTargetContext(fit, width, height, config, std::move(colorSpace),
748 sampleCnt, origin, surfaceProps, budgeted);
robertphillips48fde9c2016-09-06 05:20:20 -0700749}
750
robertphillipsd728f0c2016-11-21 11:05:03 -0800751sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContextWithFallback(
752 SkBackingFit fit,
753 int width, int height,
754 GrPixelConfig config,
755 sk_sp<SkColorSpace> colorSpace,
756 int sampleCnt,
757 GrSurfaceOrigin origin,
758 const SkSurfaceProps* surfaceProps,
759 SkBudgeted budgeted) {
760 if (!this->caps()->isConfigRenderable(config, sampleCnt > 0)) {
761 config = GrPixelConfigFallback(config);
762 }
763
764 return this->makeDeferredRenderTargetContext(fit, width, height, config, std::move(colorSpace),
765 sampleCnt, origin, surfaceProps, budgeted);
766}
767
Brian Osman11052242016-10-27 14:47:55 -0400768sk_sp<GrRenderTargetContext> GrContext::makeRenderTargetContext(SkBackingFit fit,
769 int width, int height,
770 GrPixelConfig config,
771 sk_sp<SkColorSpace> colorSpace,
772 int sampleCnt,
773 GrSurfaceOrigin origin,
774 const SkSurfaceProps* surfaceProps,
775 SkBudgeted budgeted) {
robertphillips48fde9c2016-09-06 05:20:20 -0700776 if (!this->caps()->isConfigRenderable(config, sampleCnt > 0)) {
777 return nullptr;
778 }
779
robertphillipsd4c741e2016-04-28 09:55:15 -0700780 GrSurfaceDesc desc;
781 desc.fFlags = kRenderTarget_GrSurfaceFlag;
782 desc.fOrigin = origin;
783 desc.fWidth = width;
784 desc.fHeight = height;
785 desc.fConfig = config;
786 desc.fSampleCnt = sampleCnt;
787
788 sk_sp<GrTexture> tex;
robertphillips76948d42016-05-04 12:47:41 -0700789 if (SkBackingFit::kExact == fit) {
robertphillipsca6eafc2016-05-17 09:57:46 -0700790 tex.reset(this->textureProvider()->createTexture(desc, budgeted));
robertphillipsd4c741e2016-04-28 09:55:15 -0700791 } else {
792 tex.reset(this->textureProvider()->createApproxTexture(desc));
793 }
794 if (!tex) {
795 return nullptr;
796 }
797
Brian Osman11052242016-10-27 14:47:55 -0400798 sk_sp<GrRenderTargetContext> renderTargetContext(
799 this->contextPriv().makeWrappedRenderTargetContext(sk_ref_sp(tex->asRenderTarget()),
robertphillips6738c702016-07-27 12:13:51 -0700800 std::move(colorSpace), surfaceProps));
Brian Osman11052242016-10-27 14:47:55 -0400801 if (!renderTargetContext) {
robertphillipsd4c741e2016-04-28 09:55:15 -0700802 return nullptr;
803 }
804
Brian Osman11052242016-10-27 14:47:55 -0400805 return renderTargetContext;
robertphillipsd4c741e2016-04-28 09:55:15 -0700806}
807
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400808sk_sp<GrRenderTargetContext> GrContext::makeDeferredRenderTargetContext(
809 SkBackingFit fit,
810 int width, int height,
811 GrPixelConfig config,
812 sk_sp<SkColorSpace> colorSpace,
813 int sampleCnt,
814 GrSurfaceOrigin origin,
815 const SkSurfaceProps* surfaceProps,
816 SkBudgeted budgeted) {
817 GrSurfaceDesc desc;
818 desc.fFlags = kRenderTarget_GrSurfaceFlag;
819 desc.fOrigin = origin;
820 desc.fWidth = width;
821 desc.fHeight = height;
822 desc.fConfig = config;
823 desc.fSampleCnt = sampleCnt;
824
Robert Phillips37430132016-11-09 06:50:43 -0500825 sk_sp<GrSurfaceProxy> rtp = GrSurfaceProxy::MakeDeferred(*this->caps(), desc, fit, budgeted);
Robert Phillipsc7635fa2016-10-28 13:25:24 -0400826
827 return fDrawingManager->makeRenderTargetContext(std::move(rtp),
828 std::move(colorSpace),
829 surfaceProps);
830}
831
joshualitt1de610a2016-01-06 08:26:09 -0800832bool GrContext::abandoned() const {
833 ASSERT_SINGLE_OWNER
robertphillips7761d612016-05-16 09:14:53 -0700834 return fDrawingManager->wasAbandoned();
robertphillips77a2e522015-10-17 07:43:27 -0700835}
836
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000837namespace {
838void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
839 GrConfigConversionEffect::PMConversion pmToUPM;
840 GrConfigConversionEffect::PMConversion upmToPM;
841 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
842 *pmToUPMValue = pmToUPM;
843 *upmToPMValue = upmToPM;
844}
845}
846
bsalomon6c6f6582015-09-10 08:12:46 -0700847void GrContext::testPMConversionsIfNecessary(uint32_t flags) {
joshualitt1de610a2016-01-06 08:26:09 -0800848 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700849 if (SkToBool(kUnpremul_PixelOpsFlag & flags)) {
850 SkAutoMutexAcquire ama(fTestPMConversionsMutex);
851 if (!fDidTestPMConversions) {
852 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
853 fDidTestPMConversions = true;
854 }
855 }
856}
857
bungeman06ca8ec2016-06-09 08:01:03 -0700858sk_sp<GrFragmentProcessor> GrContext::createPMToUPMEffect(GrTexture* texture,
bsalomon6c9cd552016-01-22 07:17:34 -0800859 const GrSwizzle& swizzle,
bsalomon6c6f6582015-09-10 08:12:46 -0700860 const SkMatrix& matrix) const {
joshualitt1de610a2016-01-06 08:26:09 -0800861 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700862 // We should have already called this->testPMConversionsIfNecessary().
863 SkASSERT(fDidTestPMConversions);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000864 GrConfigConversionEffect::PMConversion pmToUPM =
865 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
866 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bungeman06ca8ec2016-06-09 08:01:03 -0700867 return GrConfigConversionEffect::Make(texture, swizzle, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000868 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700869 return nullptr;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000870 }
871}
872
bungeman06ca8ec2016-06-09 08:01:03 -0700873sk_sp<GrFragmentProcessor> GrContext::createUPMToPMEffect(GrTexture* texture,
bsalomon6c9cd552016-01-22 07:17:34 -0800874 const GrSwizzle& swizzle,
bsalomon6c6f6582015-09-10 08:12:46 -0700875 const SkMatrix& matrix) const {
joshualitt1de610a2016-01-06 08:26:09 -0800876 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700877 // We should have already called this->testPMConversionsIfNecessary().
878 SkASSERT(fDidTestPMConversions);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000879 GrConfigConversionEffect::PMConversion upmToPM =
880 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
881 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
bungeman06ca8ec2016-06-09 08:01:03 -0700882 return GrConfigConversionEffect::Make(texture, swizzle, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000883 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700884 return nullptr;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000885 }
886}
887
bsalomon636e8022015-07-29 06:08:46 -0700888bool GrContext::didFailPMUPMConversionTest() const {
joshualitt1de610a2016-01-06 08:26:09 -0800889 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700890 // We should have already called this->testPMConversionsIfNecessary().
891 SkASSERT(fDidTestPMConversions);
bsalomon636e8022015-07-29 06:08:46 -0700892 // The PM<->UPM tests fail or succeed together so we only need to check one.
bsalomon6c6f6582015-09-10 08:12:46 -0700893 return GrConfigConversionEffect::kNone_PMConversion == fPMToUPMConversion;
bsalomon636e8022015-07-29 06:08:46 -0700894}
895
bsalomon37f9a262015-02-02 13:00:10 -0800896//////////////////////////////////////////////////////////////////////////////
897
898void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800899 ASSERT_SINGLE_OWNER
bsalomon37f9a262015-02-02 13:00:10 -0800900 if (maxTextures) {
bsalomon0ea80f42015-02-11 10:49:59 -0800901 *maxTextures = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -0800902 }
903 if (maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800904 *maxTextureBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -0800905 }
906}
907
908void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
joshualitt1de610a2016-01-06 08:26:09 -0800909 ASSERT_SINGLE_OWNER
bsalomon0ea80f42015-02-11 10:49:59 -0800910 fResourceCache->setLimits(maxTextures, maxTextureBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800911}
912
ericrk0a5fa482015-09-15 14:16:10 -0700913//////////////////////////////////////////////////////////////////////////////
914
915void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -0800916 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -0700917 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
918}