blob: 357f58e6e323240234a8f226fd0e65b3ea2a76c3 [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"
robertphillipsea461502015-05-26 11:38:03 -070012#include "GrDrawContext.h"
bsalomon0ea80f42015-02-11 10:49:59 -080013#include "GrResourceCache.h"
bsalomond309e7a2015-04-30 14:18:54 -070014#include "GrResourceProvider.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000015#include "GrSoftwarePathRenderer.h"
bsalomonafbf2d62014-09-30 12:18:44 -070016#include "GrSurfacePriv.h"
robertphillips3dc6ae52015-10-20 09:54:32 -070017
bsalomon81beccc2014-10-13 12:32:55 -070018#include "SkConfig8888.h"
bsalomonf276ac52015-10-09 13:36:42 -070019#include "SkGrPriv.h"
joshualitt74417822015-08-07 11:42:16 -070020
bsalomonb8fea972016-02-16 07:34:17 -080021#include "batches/GrCopySurfaceBatch.h"
joshualitt5478d422014-11-14 16:00:38 -080022#include "effects/GrConfigConversionEffect.h"
brianosman2d1ee792016-05-05 12:24:31 -070023#include "effects/GrGammaEffect.h"
joshualitte8042922015-12-11 06:11:21 -080024#include "text/GrTextBlobCache.h"
joshualitt5478d422014-11-14 16:00:38 -080025
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000026#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
joshualitt1de610a2016-01-06 08:26:09 -080027#define ASSERT_SINGLE_OWNER \
28 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fSingleOwner);)
robertphillips4fd74ae2016-08-03 14:26:53 -070029#define ASSERT_SINGLE_OWNER_PRIV \
30 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fContext->fSingleOwner);)
robertphillips7761d612016-05-16 09:14:53 -070031#define RETURN_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return; }
32#define RETURN_FALSE_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return false; }
33#define RETURN_NULL_IF_ABANDONED if (fDrawingManager->wasAbandoned()) { return nullptr; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000034
robertphillipsea461502015-05-26 11:38:03 -070035////////////////////////////////////////////////////////////////////////////////
36
bsalomon682c2692015-05-22 14:01:46 -070037GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext) {
38 GrContextOptions defaultOptions;
39 return Create(backend, backendContext, defaultOptions);
40}
bsalomonf28cff72015-05-22 12:25:41 -070041
bsalomon682c2692015-05-22 14:01:46 -070042GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
43 const GrContextOptions& options) {
halcanary385fe4d2015-08-26 13:07:48 -070044 GrContext* context = new GrContext;
bsalomon682c2692015-05-22 14:01:46 -070045
46 if (context->init(backend, backendContext, options)) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000047 return context;
48 } else {
49 context->unref();
halcanary96fcdcc2015-08-27 07:41:13 -070050 return nullptr;
bsalomon@google.com27847de2011-02-22 20:59:41 +000051 }
bsalomon@google.com27847de2011-02-22 20:59:41 +000052}
53
joshualitt0acd0d32015-05-07 08:23:19 -070054static int32_t gNextID = 1;
55static int32_t next_id() {
56 int32_t id;
57 do {
58 id = sk_atomic_inc(&gNextID);
59 } while (id == SK_InvalidGenID);
60 return id;
61}
62
bsalomon682c2692015-05-22 14:01:46 -070063GrContext::GrContext() : fUniqueID(next_id()) {
halcanary96fcdcc2015-08-27 07:41:13 -070064 fGpu = nullptr;
65 fCaps = nullptr;
66 fResourceCache = nullptr;
67 fResourceProvider = nullptr;
halcanary96fcdcc2015-08-27 07:41:13 -070068 fBatchFontCache = nullptr;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000069}
70
bsalomon682c2692015-05-22 14:01:46 -070071bool GrContext::init(GrBackend backend, GrBackendContext backendContext,
72 const GrContextOptions& options) {
joshualitt1de610a2016-01-06 08:26:09 -080073 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -070074 SkASSERT(!fGpu);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000075
bsalomon682c2692015-05-22 14:01:46 -070076 fGpu = GrGpu::Create(backend, backendContext, options, this);
robertphillipsea461502015-05-26 11:38:03 -070077 if (!fGpu) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000078 return false;
79 }
bsalomon69cfe952015-11-30 13:27:47 -080080 this->initCommon(options);
bsalomon33435572014-11-05 14:47:41 -080081 return true;
82}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000083
bsalomon69cfe952015-11-30 13:27:47 -080084void GrContext::initCommon(const GrContextOptions& options) {
joshualitt1de610a2016-01-06 08:26:09 -080085 ASSERT_SINGLE_OWNER
86
bsalomon76228632015-05-29 08:02:10 -070087 fCaps = SkRef(fGpu->caps());
halcanary385fe4d2015-08-26 13:07:48 -070088 fResourceCache = new GrResourceCache(fCaps);
joshualitt6d0872d2016-01-11 08:27:48 -080089 fResourceProvider = new GrResourceProvider(fGpu, fResourceCache, &fSingleOwner);
commit-bot@chromium.org1836d332013-07-16 22:55:03 +000090
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000091 fDidTestPMConversions = false;
92
bsalomon69cfe952015-11-30 13:27:47 -080093 GrDrawTarget::Options dtOptions;
94 dtOptions.fClipBatchToBounds = options.fClipBatchToBounds;
bsalomon6dea83f2015-12-03 12:58:06 -080095 dtOptions.fDrawBatchBounds = options.fDrawBatchBounds;
bsalomon489147c2015-12-14 12:13:09 -080096 dtOptions.fMaxBatchLookback = options.fMaxBatchLookback;
bsalomonaecc0182016-03-07 11:50:44 -080097 dtOptions.fMaxBatchLookahead = options.fMaxBatchLookahead;
bsalomonb77a9072016-09-07 10:02:04 -070098 fDrawingManager.reset(new GrDrawingManager(this, dtOptions, options.fImmediateMode,
99 &fSingleOwner));
joshualitt7c3a2f82015-03-31 13:32:05 -0700100
101 // GrBatchFontCache will eventually replace GrFontCache
halcanary385fe4d2015-08-26 13:07:48 -0700102 fBatchFontCache = new GrBatchFontCache(this);
joshualittb7133be2015-04-08 09:08:31 -0700103
halcanary385fe4d2015-08-26 13:07:48 -0700104 fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB, this));
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000105}
106
bsalomon@google.com27847de2011-02-22 20:59:41 +0000107GrContext::~GrContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800108 ASSERT_SINGLE_OWNER
109
robertphillipsea461502015-05-26 11:38:03 -0700110 if (!fGpu) {
bsalomon76228632015-05-29 08:02:10 -0700111 SkASSERT(!fCaps);
bsalomon@google.com733c0622013-04-24 17:59:32 +0000112 return;
113 }
114
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000115 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000116
robertphillips77a2e522015-10-17 07:43:27 -0700117 fDrawingManager->cleanup();
robertphillips2334fb62015-06-17 05:43:33 -0700118
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000119 for (int i = 0; i < fCleanUpData.count(); ++i) {
120 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
121 }
122
halcanary385fe4d2015-08-26 13:07:48 -0700123 delete fResourceProvider;
124 delete fResourceCache;
125 delete fBatchFontCache;
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000126
bsalomon@google.com205d4602011-04-25 12:43:45 +0000127 fGpu->unref();
bsalomon76228632015-05-29 08:02:10 -0700128 fCaps->unref();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000129}
130
bsalomon41b952c2016-03-11 06:46:33 -0800131GrContextThreadSafeProxy* GrContext::threadSafeProxy() {
132 if (!fThreadSafeProxy) {
133 fThreadSafeProxy.reset(new GrContextThreadSafeProxy(fCaps, this->uniqueID()));
134 }
135 return SkRef(fThreadSafeProxy.get());
136}
137
bsalomon2354f842014-07-28 13:48:36 -0700138void GrContext::abandonContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800139 ASSERT_SINGLE_OWNER
140
bsalomond309e7a2015-04-30 14:18:54 -0700141 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800142
143 // Need to abandon the drawing manager first so all the render targets
144 // will be released/forgotten before they too are abandoned.
145 fDrawingManager->abandon();
146
bsalomon@google.com205d4602011-04-25 12:43:45 +0000147 // abandon first to so destructors
148 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800149 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700150
bsalomon6e2aad42016-04-01 11:54:31 -0700151 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
152
153 fBatchFontCache->freeAll();
bsalomon6e2aad42016-04-01 11:54:31 -0700154 fTextBlobCache->freeAll();
155}
156
157void GrContext::releaseResourcesAndAbandonContext() {
158 ASSERT_SINGLE_OWNER
159
160 fResourceProvider->abandon();
161
162 // Need to abandon the drawing manager first so all the render targets
163 // will be released/forgotten before they too are abandoned.
164 fDrawingManager->abandon();
165
166 // Release all resources in the backend 3D API.
167 fResourceCache->releaseAll();
168
169 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000170
joshualitt7c3a2f82015-03-31 13:32:05 -0700171 fBatchFontCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700172 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000173}
174
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000175void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800176 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000177 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000178}
179
180void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800181 ASSERT_SINGLE_OWNER
182
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000183 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000184
joshualitt7c3a2f82015-03-31 13:32:05 -0700185 fBatchFontCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700186
187 fDrawingManager->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700188
189 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000190}
191
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000192void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800193 ASSERT_SINGLE_OWNER
194
bsalomon71cb0c22014-11-14 12:10:14 -0800195 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800196 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800197 }
198 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800199 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800200 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000201}
202
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000203////////////////////////////////////////////////////////////////////////////////
204
joshualitt0db6dfa2015-04-10 07:01:30 -0700205void GrContext::TextBlobCacheOverBudgetCB(void* data) {
206 SkASSERT(data);
bsalomonb77a9072016-09-07 10:02:04 -0700207 // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on GrDrawContext
208 // to perform a necessary flush. The solution is to move drawText calls to below the GrContext
209 // level, but this is not trivial because they call drawPath on SkGpuDevice.
joshualitt0db6dfa2015-04-10 07:01:30 -0700210 GrContext* context = reinterpret_cast<GrContext*>(data);
211 context->flush();
212}
213
bsalomon@google.com27847de2011-02-22 20:59:41 +0000214////////////////////////////////////////////////////////////////////////////////
215
bsalomonb77a9072016-09-07 10:02:04 -0700216void GrContext::flush() {
joshualitt1de610a2016-01-06 08:26:09 -0800217 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700218 RETURN_IF_ABANDONED
bsalomonb77a9072016-09-07 10:02:04 -0700219 fDrawingManager->flush();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000220}
221
bsalomon81beccc2014-10-13 12:32:55 -0700222bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
223 const void* inPixels, size_t outRowBytes, void* outPixels) {
224 SkSrcPixelInfo srcPI;
brianosman396fcdb2016-07-22 06:26:11 -0700225 if (!GrPixelConfigToColorType(srcConfig, &srcPI.fColorType)) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000226 return false;
227 }
bsalomon81beccc2014-10-13 12:32:55 -0700228 srcPI.fAlphaType = kUnpremul_SkAlphaType;
229 srcPI.fPixels = inPixels;
230 srcPI.fRowBytes = inRowBytes;
231
232 SkDstPixelInfo dstPI;
233 dstPI.fColorType = srcPI.fColorType;
234 dstPI.fAlphaType = kPremul_SkAlphaType;
235 dstPI.fPixels = outPixels;
236 dstPI.fRowBytes = outRowBytes;
237
238 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000239}
240
bsalomon81beccc2014-10-13 12:32:55 -0700241bool GrContext::writeSurfacePixels(GrSurface* surface,
242 int left, int top, int width, int height,
243 GrPixelConfig srcConfig, const void* buffer, size_t rowBytes,
244 uint32_t pixelOpsFlags) {
joshualitt1de610a2016-01-06 08:26:09 -0800245 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800246 RETURN_FALSE_IF_ABANDONED
bsalomon6c6f6582015-09-10 08:12:46 -0700247 ASSERT_OWNED_RESOURCE(surface);
248 SkASSERT(surface);
joshualittbc907352016-01-13 06:45:40 -0800249 GR_AUDIT_TRAIL_AUTO_FRAME(&fAuditTrail, "GrContext::writeSurfacePixels");
bsalomon6c6f6582015-09-10 08:12:46 -0700250
251 this->testPMConversionsIfNecessary(pixelOpsFlags);
bsalomon81beccc2014-10-13 12:32:55 -0700252
bsalomone8d21e82015-07-16 08:23:13 -0700253 // 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 -0700254 // necessary and because GrGpu::getWritePixelsInfo requires it.
bsalomone8d21e82015-07-16 08:23:13 -0700255 if (!GrSurfacePriv::AdjustWritePixelParams(surface->width(), surface->height(),
256 GrBytesPerPixel(srcConfig), &left, &top, &width,
257 &height, &buffer, &rowBytes)) {
258 return false;
259 }
260
bsalomonf0674512015-07-28 13:26:15 -0700261 bool applyPremulToSrc = false;
bsalomon81beccc2014-10-13 12:32:55 -0700262 if (kUnpremul_PixelOpsFlag & pixelOpsFlags) {
263 if (!GrPixelConfigIs8888(srcConfig)) {
264 return false;
265 }
bsalomonf0674512015-07-28 13:26:15 -0700266 applyPremulToSrc = true;
267 }
bsalomon636e8022015-07-29 06:08:46 -0700268
269 GrGpu::DrawPreference drawPreference = GrGpu::kNoDraw_DrawPreference;
270 // Don't prefer to draw for the conversion (and thereby access a texture from the cache) when
271 // we've already determined that there isn't a roundtrip preserving conversion processor pair.
272 if (applyPremulToSrc && !this->didFailPMUPMConversionTest()) {
273 drawPreference = GrGpu::kCallerPrefersDraw_DrawPreference;
274 }
275
bsalomonf0674512015-07-28 13:26:15 -0700276 GrGpu::WritePixelTempDrawInfo tempDrawInfo;
cblumeed828002016-02-16 13:00:01 -0800277 if (!fGpu->getWritePixelsInfo(surface, width, height, srcConfig, &drawPreference,
bsalomonf0674512015-07-28 13:26:15 -0700278 &tempDrawInfo)) {
279 return false;
280 }
281
282 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && surface->surfacePriv().hasPendingIO()) {
283 this->flush();
284 }
285
286 SkAutoTUnref<GrTexture> tempTexture;
287 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomoneae62002015-07-31 13:59:30 -0700288 tempTexture.reset(
289 this->textureProvider()->createApproxTexture(tempDrawInfo.fTempSurfaceDesc));
bsalomonf0674512015-07-28 13:26:15 -0700290 if (!tempTexture && GrGpu::kRequireDraw_DrawPreference == drawPreference) {
291 return false;
292 }
293 }
294
295 // temp buffer for doing sw premul conversion, if needed.
296 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
297 if (tempTexture) {
bungeman06ca8ec2016-06-09 08:01:03 -0700298 sk_sp<GrFragmentProcessor> fp;
bsalomonf0674512015-07-28 13:26:15 -0700299 SkMatrix textureMatrix;
300 textureMatrix.setIDiv(tempTexture->width(), tempTexture->height());
bsalomonf0674512015-07-28 13:26:15 -0700301 if (applyPremulToSrc) {
bungeman06ca8ec2016-06-09 08:01:03 -0700302 fp = this->createUPMToPMEffect(tempTexture, tempDrawInfo.fSwizzle, textureMatrix);
bsalomonf0674512015-07-28 13:26:15 -0700303 // If premultiplying was the only reason for the draw, fall back to a straight write.
304 if (!fp) {
305 if (GrGpu::kCallerPrefersDraw_DrawPreference == drawPreference) {
halcanary96fcdcc2015-08-27 07:41:13 -0700306 tempTexture.reset(nullptr);
bsalomonf0674512015-07-28 13:26:15 -0700307 }
308 } else {
309 applyPremulToSrc = false;
310 }
311 }
312 if (tempTexture) {
313 if (!fp) {
bungeman06ca8ec2016-06-09 08:01:03 -0700314 fp = GrConfigConversionEffect::Make(tempTexture, tempDrawInfo.fSwizzle,
315 GrConfigConversionEffect::kNone_PMConversion,
316 textureMatrix);
bsalomonf0674512015-07-28 13:26:15 -0700317 if (!fp) {
318 return false;
319 }
320 }
321 GrRenderTarget* renderTarget = surface->asRenderTarget();
322 SkASSERT(renderTarget);
323 if (tempTexture->surfacePriv().hasPendingIO()) {
324 this->flush();
325 }
326 if (applyPremulToSrc) {
327 size_t tmpRowBytes = 4 * width;
328 tmpPixels.reset(width * height);
329 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
330 tmpPixels.get())) {
331 return false;
332 }
333 rowBytes = tmpRowBytes;
334 buffer = tmpPixels.get();
335 applyPremulToSrc = false;
336 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700337 if (!fGpu->writePixels(tempTexture, 0, 0, width, height,
bsalomon6c9cd552016-01-22 07:17:34 -0800338 tempDrawInfo.fWriteConfig, buffer,
bsalomon6cb3cbe2015-07-30 07:34:27 -0700339 rowBytes)) {
bsalomonf0674512015-07-28 13:26:15 -0700340 return false;
341 }
342 SkMatrix matrix;
343 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
brianosmandfe4f2e2016-07-21 13:28:36 -0700344 // TODO: Need to decide the semantics of this function for color spaces. Do we support
345 // conversion from a passed-in color space? For now, specifying nullptr means that this
346 // path will do no conversion, so it will match the behavior of the non-draw path.
robertphillips4fd74ae2016-08-03 14:26:53 -0700347 sk_sp<GrDrawContext> drawContext(this->contextPriv().makeWrappedDrawContext(
348 sk_ref_sp(renderTarget),
349 nullptr));
bsalomonf0674512015-07-28 13:26:15 -0700350 if (!drawContext) {
351 return false;
352 }
egdanielc4b72722015-11-23 13:20:41 -0800353 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700354 paint.addColorFragmentProcessor(std::move(fp));
egdanielc4b72722015-11-23 13:20:41 -0800355 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
brianosmana167e742016-05-24 06:18:48 -0700356 paint.setAllowSRGBInputs(true);
bsalomonf0674512015-07-28 13:26:15 -0700357 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
cdalton846c0512016-05-13 10:25:00 -0700358 drawContext->drawRect(GrNoClip(), paint, matrix, rect, nullptr);
bsalomonf0674512015-07-28 13:26:15 -0700359
360 if (kFlushWrites_PixelOp & pixelOpsFlags) {
361 this->flushSurfaceWrites(surface);
362 }
363 }
364 }
365 if (!tempTexture) {
bsalomonf0674512015-07-28 13:26:15 -0700366 if (applyPremulToSrc) {
bsalomon81beccc2014-10-13 12:32:55 -0700367 size_t tmpRowBytes = 4 * width;
368 tmpPixels.reset(width * height);
369 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
370 tmpPixels.get())) {
371 return false;
372 }
373 rowBytes = tmpRowBytes;
374 buffer = tmpPixels.get();
bsalomonf0674512015-07-28 13:26:15 -0700375 applyPremulToSrc = false;
bsalomon81beccc2014-10-13 12:32:55 -0700376 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700377 return fGpu->writePixels(surface, left, top, width, height, srcConfig, buffer, rowBytes);
bsalomon81beccc2014-10-13 12:32:55 -0700378 }
bsalomon81beccc2014-10-13 12:32:55 -0700379 return true;
380}
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000381
bsalomone8d21e82015-07-16 08:23:13 -0700382bool GrContext::readSurfacePixels(GrSurface* src,
383 int left, int top, int width, int height,
384 GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
385 uint32_t flags) {
joshualitt1de610a2016-01-06 08:26:09 -0800386 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800387 RETURN_FALSE_IF_ABANDONED
bsalomone8d21e82015-07-16 08:23:13 -0700388 ASSERT_OWNED_RESOURCE(src);
389 SkASSERT(src);
joshualittbc907352016-01-13 06:45:40 -0800390 GR_AUDIT_TRAIL_AUTO_FRAME(&fAuditTrail, "GrContext::readSurfacePixels");
bsalomon32ab2602015-09-09 18:57:49 -0700391
bsalomon6c6f6582015-09-10 08:12:46 -0700392 this->testPMConversionsIfNecessary(flags);
393 SkAutoMutexAcquire ama(fReadPixelsMutex);
394
bsalomone8d21e82015-07-16 08:23:13 -0700395 // Adjust the params so that if we wind up using an intermediate surface we've already done
396 // all the trimming and the temporary can be the min size required.
397 if (!GrSurfacePriv::AdjustReadPixelParams(src->width(), src->height(),
398 GrBytesPerPixel(dstConfig), &left,
399 &top, &width, &height, &buffer, &rowBytes)) {
400 return false;
401 }
402
403 if (!(kDontFlush_PixelOpsFlag & flags) && src->surfacePriv().hasPendingWrite()) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000404 this->flush();
405 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000406
bsalomone8d21e82015-07-16 08:23:13 -0700407 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.com9c680582013-02-06 18:17:50 +0000408 if (unpremul && !GrPixelConfigIs8888(dstConfig)) {
bsalomon39826022015-07-23 08:07:21 -0700409 // The unpremul flag is only allowed for 8888 configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000410 return false;
411 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000412
bsalomon636e8022015-07-29 06:08:46 -0700413 GrGpu::DrawPreference drawPreference = GrGpu::kNoDraw_DrawPreference;
414 // Don't prefer to draw for the conversion (and thereby access a texture from the cache) when
415 // we've already determined that there isn't a roundtrip preserving conversion processor pair.
416 if (unpremul && !this->didFailPMUPMConversionTest()) {
417 drawPreference = GrGpu::kCallerPrefersDraw_DrawPreference;
418 }
419
bsalomon39826022015-07-23 08:07:21 -0700420 GrGpu::ReadPixelTempDrawInfo tempDrawInfo;
421 if (!fGpu->getReadPixelsInfo(src, width, height, rowBytes, dstConfig, &drawPreference,
422 &tempDrawInfo)) {
423 return false;
424 }
bsalomon191bcc02014-11-14 11:31:13 -0800425
bsalomon6cb3cbe2015-07-30 07:34:27 -0700426 SkAutoTUnref<GrSurface> surfaceToRead(SkRef(src));
bsalomon39826022015-07-23 08:07:21 -0700427 bool didTempDraw = false;
428 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomonb117ff12016-07-19 07:24:40 -0700429 if (SkBackingFit::kExact == tempDrawInfo.fTempSurfaceFit) {
bsalomon39826022015-07-23 08:07:21 -0700430 // We only respect this when the entire src is being read. Otherwise we can trigger too
431 // many odd ball texture sizes and trash the cache.
bsalomoneae62002015-07-31 13:59:30 -0700432 if (width != src->width() || height != src->height()) {
bsalomonb117ff12016-07-19 07:24:40 -0700433 tempDrawInfo.fTempSurfaceFit= SkBackingFit::kApprox;
bsalomon39826022015-07-23 08:07:21 -0700434 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000435 }
brianosmandfe4f2e2016-07-21 13:28:36 -0700436 // TODO: Need to decide the semantics of this function for color spaces. Do we support
437 // conversion to a passed-in color space? For now, specifying nullptr means that this
438 // path will do no conversion, so it will match the behavior of the non-draw path.
robertphillips6738c702016-07-27 12:13:51 -0700439 sk_sp<GrDrawContext> tempDC = this->makeDrawContext(tempDrawInfo.fTempSurfaceFit,
bsalomonb117ff12016-07-19 07:24:40 -0700440 tempDrawInfo.fTempSurfaceDesc.fWidth,
441 tempDrawInfo.fTempSurfaceDesc.fHeight,
442 tempDrawInfo.fTempSurfaceDesc.fConfig,
brianosmandfe4f2e2016-07-21 13:28:36 -0700443 nullptr,
bsalomonb117ff12016-07-19 07:24:40 -0700444 tempDrawInfo.fTempSurfaceDesc.fSampleCnt,
445 tempDrawInfo.fTempSurfaceDesc.fOrigin);
446 if (tempDC) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000447 SkMatrix textureMatrix;
bsalomon39826022015-07-23 08:07:21 -0700448 textureMatrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000449 textureMatrix.postIDiv(src->width(), src->height());
bungeman06ca8ec2016-06-09 08:01:03 -0700450 sk_sp<GrFragmentProcessor> fp;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000451 if (unpremul) {
bungeman06ca8ec2016-06-09 08:01:03 -0700452 fp = this->createPMToUPMEffect(src->asTexture(), tempDrawInfo.fSwizzle,
453 textureMatrix);
joshualittb0a8a372014-09-23 09:50:21 -0700454 if (fp) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000455 unpremul = false; // we no longer need to do this on CPU after the read back.
bsalomon39826022015-07-23 08:07:21 -0700456 } else if (GrGpu::kCallerPrefersDraw_DrawPreference == drawPreference) {
457 // We only wanted to do the draw in order to perform the unpremul so don't
458 // bother.
bsalomonb117ff12016-07-19 07:24:40 -0700459 tempDC.reset(nullptr);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000460 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000461 }
bsalomonb117ff12016-07-19 07:24:40 -0700462 if (!fp && tempDC) {
bungeman06ca8ec2016-06-09 08:01:03 -0700463 fp = GrConfigConversionEffect::Make(src->asTexture(), tempDrawInfo.fSwizzle,
464 GrConfigConversionEffect::kNone_PMConversion,
465 textureMatrix);
bsalomon39826022015-07-23 08:07:21 -0700466 }
467 if (fp) {
egdanielc4b72722015-11-23 13:20:41 -0800468 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700469 paint.addColorFragmentProcessor(std::move(fp));
egdanielc4b72722015-11-23 13:20:41 -0800470 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
brianosmana167e742016-05-24 06:18:48 -0700471 paint.setAllowSRGBInputs(true);
bsalomon39826022015-07-23 08:07:21 -0700472 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
bsalomonb117ff12016-07-19 07:24:40 -0700473 tempDC->drawRect(GrNoClip(), paint, SkMatrix::I(), rect, nullptr);
474 surfaceToRead.reset(tempDC->asTexture().release());
bsalomon39826022015-07-23 08:07:21 -0700475 left = 0;
476 top = 0;
477 didTempDraw = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000478 }
bsalomon@google.com0342a852012-08-20 19:22:38 +0000479 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000480 }
joshualitt5c55fef2014-10-31 14:04:35 -0700481
bsalomon39826022015-07-23 08:07:21 -0700482 if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000483 return false;
484 }
bsalomon39826022015-07-23 08:07:21 -0700485 GrPixelConfig configToRead = dstConfig;
486 if (didTempDraw) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700487 this->flushSurfaceWrites(surfaceToRead);
bsalomon6c9cd552016-01-22 07:17:34 -0800488 configToRead = tempDrawInfo.fReadConfig;
bsalomon39826022015-07-23 08:07:21 -0700489 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700490 if (!fGpu->readPixels(surfaceToRead, left, top, width, height, configToRead, buffer,
491 rowBytes)) {
bsalomon39826022015-07-23 08:07:21 -0700492 return false;
493 }
494
495 // Perform umpremul conversion if we weren't able to perform it as a draw.
496 if (unpremul) {
reed@google.com7111d462014-03-25 16:20:24 +0000497 SkDstPixelInfo dstPI;
brianosman396fcdb2016-07-22 06:26:11 -0700498 if (!GrPixelConfigToColorType(dstConfig, &dstPI.fColorType)) {
reed@google.com7111d462014-03-25 16:20:24 +0000499 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000500 }
reed@google.com7111d462014-03-25 16:20:24 +0000501 dstPI.fAlphaType = kUnpremul_SkAlphaType;
502 dstPI.fPixels = buffer;
503 dstPI.fRowBytes = rowBytes;
504
505 SkSrcPixelInfo srcPI;
bsalomon39826022015-07-23 08:07:21 -0700506 srcPI.fColorType = dstPI.fColorType;
reed@google.com7111d462014-03-25 16:20:24 +0000507 srcPI.fAlphaType = kPremul_SkAlphaType;
508 srcPI.fPixels = buffer;
509 srcPI.fRowBytes = rowBytes;
510
511 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000512 }
513 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000514}
515
bsalomonc49e8682015-06-30 11:37:35 -0700516void GrContext::prepareSurfaceForExternalIO(GrSurface* surface) {
joshualitt1de610a2016-01-06 08:26:09 -0800517 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800518 RETURN_IF_ABANDONED
bsalomon87a94eb2014-11-03 14:28:32 -0800519 SkASSERT(surface);
520 ASSERT_OWNED_RESOURCE(surface);
521 if (surface->surfacePriv().hasPendingIO()) {
522 this->flush();
523 }
524 GrRenderTarget* rt = surface->asRenderTarget();
525 if (fGpu && rt) {
526 fGpu->resolveRenderTarget(rt);
bsalomon41ebbdd2014-08-04 08:31:39 -0700527 }
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000528}
529
bsalomonb8fea972016-02-16 07:34:17 -0800530bool GrContext::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
531 const SkIPoint& dstPoint) {
joshualitt1de610a2016-01-06 08:26:09 -0800532 ASSERT_SINGLE_OWNER
bsalomonb8fea972016-02-16 07:34:17 -0800533 RETURN_FALSE_IF_ABANDONED
joshualittbc907352016-01-13 06:45:40 -0800534 GR_AUDIT_TRAIL_AUTO_FRAME(&fAuditTrail, "GrContext::copySurface");
535
robertphillipsea461502015-05-26 11:38:03 -0700536 if (!src || !dst) {
bsalomonb8fea972016-02-16 07:34:17 -0800537 return false;
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000538 }
bsalomone3d4bf22014-09-23 09:15:03 -0700539 ASSERT_OWNED_RESOURCE(src);
junov2bb52102014-09-29 10:18:59 -0700540 ASSERT_OWNED_RESOURCE(dst);
Brian Salomon34a98952014-09-24 11:41:24 -0400541
robertphillipsea461502015-05-26 11:38:03 -0700542 if (!dst->asRenderTarget()) {
bsalomonb8fea972016-02-16 07:34:17 -0800543 SkIRect clippedSrcRect;
544 SkIPoint clippedDstPoint;
545 if (!GrCopySurfaceBatch::ClipSrcRectAndDstPoint(dst, src, srcRect, dstPoint,
546 &clippedSrcRect, &clippedDstPoint)) {
547 return false;
548 }
549 // If we don't have an RT for the dst then we won't have a GrDrawContext to insert the
550 // the copy surface into. In the future we plan to have a more limited Context type
551 // (GrCopyContext?) that has the subset of GrDrawContext operations that should be
552 // allowed on textures that aren't render targets.
553 // For now we just flush any writes to the src and issue an immediate copy to the dst.
554 src->flushWrites();
555 return fGpu->copySurface(dst, src, clippedSrcRect, clippedDstPoint);
robertphillipsea461502015-05-26 11:38:03 -0700556 }
robertphillips4fd74ae2016-08-03 14:26:53 -0700557 sk_sp<GrDrawContext> drawContext(this->contextPriv().makeWrappedDrawContext(
558 sk_ref_sp(dst->asRenderTarget()),
559 nullptr));
kjlubick0eed9452016-02-11 12:05:24 -0800560 if (!drawContext) {
bsalomonb8fea972016-02-16 07:34:17 -0800561 return false;
bsalomonf80bfed2014-10-07 05:56:02 -0700562 }
kjlubick0eed9452016-02-11 12:05:24 -0800563
bsalomonb8fea972016-02-16 07:34:17 -0800564 if (!drawContext->copySurface(src, srcRect, dstPoint)) {
565 return false;
kjlubick0eed9452016-02-11 12:05:24 -0800566 }
bsalomonb8fea972016-02-16 07:34:17 -0800567 return true;
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000568}
569
bsalomonf80bfed2014-10-07 05:56:02 -0700570void GrContext::flushSurfaceWrites(GrSurface* surface) {
joshualitt1de610a2016-01-06 08:26:09 -0800571 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800572 RETURN_IF_ABANDONED
bsalomonf80bfed2014-10-07 05:56:02 -0700573 if (surface->surfacePriv().hasPendingWrite()) {
574 this->flush();
575 }
576}
577
ajuma95243eb2016-08-24 08:19:02 -0700578void GrContext::flushSurfaceIO(GrSurface* surface) {
579 ASSERT_SINGLE_OWNER
580 RETURN_IF_ABANDONED
581 if (surface->surfacePriv().hasPendingIO()) {
582 this->flush();
583 }
584}
585
bsalomon@google.com27847de2011-02-22 20:59:41 +0000586////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000587int GrContext::getRecommendedSampleCount(GrPixelConfig config,
588 SkScalar dpi) const {
joshualitt1de610a2016-01-06 08:26:09 -0800589 ASSERT_SINGLE_OWNER
590
bsalomon76228632015-05-29 08:02:10 -0700591 if (!this->caps()->isConfigRenderable(config, true)) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000592 return 0;
593 }
594 int chosenSampleCount = 0;
jvanverthe9c0fc62015-04-29 11:18:05 -0700595 if (fGpu->caps()->shaderCaps()->pathRenderingSupport()) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000596 if (dpi >= 250.0f) {
597 chosenSampleCount = 4;
598 } else {
599 chosenSampleCount = 16;
600 }
601 }
egdanieleed519e2016-01-15 11:36:18 -0800602 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ? chosenSampleCount : 0;
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000603}
604
robertphillips4fd74ae2016-08-03 14:26:53 -0700605sk_sp<GrDrawContext> GrContextPriv::makeWrappedDrawContext(sk_sp<GrRenderTarget> rt,
606 sk_sp<SkColorSpace> colorSpace,
607 const SkSurfaceProps* surfaceProps) {
608 ASSERT_SINGLE_OWNER_PRIV
csmartdaltonbde96c62016-08-31 12:54:46 -0700609 return this->drawingManager()->makeDrawContext(std::move(rt),
610 std::move(colorSpace),
611 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -0700612}
robertphillips77a2e522015-10-17 07:43:27 -0700613
robertphillips4fd74ae2016-08-03 14:26:53 -0700614sk_sp<GrDrawContext> GrContextPriv::makeBackendTextureDrawContext(const GrBackendTextureDesc& desc,
615 sk_sp<SkColorSpace> colorSpace,
616 const SkSurfaceProps* props,
617 GrWrapOwnership ownership) {
618 ASSERT_SINGLE_OWNER_PRIV
619 SkASSERT(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
620
621 sk_sp<GrSurface> surface(fContext->textureProvider()->wrapBackendTexture(desc, ownership));
622 if (!surface) {
623 return nullptr;
624 }
625
csmartdaltonbde96c62016-08-31 12:54:46 -0700626 return this->drawingManager()->makeDrawContext(sk_ref_sp(surface->asRenderTarget()),
627 std::move(colorSpace), props);
robertphillips4fd74ae2016-08-03 14:26:53 -0700628}
629
630sk_sp<GrDrawContext> GrContextPriv::makeBackendRenderTargetDrawContext(
631 const GrBackendRenderTargetDesc& desc,
632 sk_sp<SkColorSpace> colorSpace,
633 const SkSurfaceProps* surfaceProps) {
634 ASSERT_SINGLE_OWNER_PRIV
635
636 sk_sp<GrRenderTarget> rt(fContext->textureProvider()->wrapBackendRenderTarget(desc));
637 if (!rt) {
638 return nullptr;
639 }
640
csmartdaltonbde96c62016-08-31 12:54:46 -0700641 return this->drawingManager()->makeDrawContext(std::move(rt),
642 std::move(colorSpace),
643 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -0700644}
645
646sk_sp<GrDrawContext> GrContextPriv::makeBackendTextureAsRenderTargetDrawContext(
egdaniela95d46b2016-08-15 08:06:29 -0700647 const GrBackendTextureDesc& desc,
robertphillips4fd74ae2016-08-03 14:26:53 -0700648 sk_sp<SkColorSpace> colorSpace,
649 const SkSurfaceProps* surfaceProps) {
650 ASSERT_SINGLE_OWNER_PRIV
651 SkASSERT(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
652
653 sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTextureAsRenderTarget(desc));
654 if (!surface) {
655 return nullptr;
656 }
657
csmartdaltonbde96c62016-08-31 12:54:46 -0700658 return this->drawingManager()->makeDrawContext(sk_ref_sp(surface->asRenderTarget()),
659 std::move(colorSpace),
660 surfaceProps);
robertphillips77a2e522015-10-17 07:43:27 -0700661}
662
robertphillips48fde9c2016-09-06 05:20:20 -0700663static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) {
664 static const GrPixelConfig kFallback[] = {
665 kUnknown_GrPixelConfig, // kUnknown_GrPixelConfig
666 kRGBA_8888_GrPixelConfig, // kAlpha_8_GrPixelConfig
667 kUnknown_GrPixelConfig, // kIndex_8_GrPixelConfig
668 kRGBA_8888_GrPixelConfig, // kRGB_565_GrPixelConfig
669 kRGBA_8888_GrPixelConfig, // kRGBA_4444_GrPixelConfig
670 kUnknown_GrPixelConfig, // kRGBA_8888_GrPixelConfig
671 kRGBA_8888_GrPixelConfig, // kBGRA_8888_GrPixelConfig
672 kUnknown_GrPixelConfig, // kSRGBA_8888_GrPixelConfig
673 kSRGBA_8888_GrPixelConfig, // kSBGRA_8888_GrPixelConfig
674 kUnknown_GrPixelConfig, // kETC1_GrPixelConfig
675 kUnknown_GrPixelConfig, // kLATC_GrPixelConfig
676 kUnknown_GrPixelConfig, // kR11_EAC_GrPixelConfig
677 kUnknown_GrPixelConfig, // kASTC_12x12_GrPixelConfig
678 kUnknown_GrPixelConfig, // kRGBA_float_GrPixelConfig
679 kRGBA_half_GrPixelConfig, // kAlpha_half_GrPixelConfig
680 kUnknown_GrPixelConfig, // kRGBA_half_GrPixelConfig
681 };
682 return kFallback[config];
683
684 GR_STATIC_ASSERT(0 == kUnknown_GrPixelConfig);
685 GR_STATIC_ASSERT(1 == kAlpha_8_GrPixelConfig);
686 GR_STATIC_ASSERT(2 == kIndex_8_GrPixelConfig);
687 GR_STATIC_ASSERT(3 == kRGB_565_GrPixelConfig);
688 GR_STATIC_ASSERT(4 == kRGBA_4444_GrPixelConfig);
689 GR_STATIC_ASSERT(5 == kRGBA_8888_GrPixelConfig);
690 GR_STATIC_ASSERT(6 == kBGRA_8888_GrPixelConfig);
691 GR_STATIC_ASSERT(7 == kSRGBA_8888_GrPixelConfig);
692 GR_STATIC_ASSERT(8 == kSBGRA_8888_GrPixelConfig);
693 GR_STATIC_ASSERT(9 == kETC1_GrPixelConfig);
694 GR_STATIC_ASSERT(10 == kLATC_GrPixelConfig);
695 GR_STATIC_ASSERT(11 == kR11_EAC_GrPixelConfig);
696 GR_STATIC_ASSERT(12 == kASTC_12x12_GrPixelConfig);
697 GR_STATIC_ASSERT(13 == kRGBA_float_GrPixelConfig);
698 GR_STATIC_ASSERT(14 == kAlpha_half_GrPixelConfig);
699 GR_STATIC_ASSERT(15 == kRGBA_half_GrPixelConfig);
700 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kFallback) == kGrPixelConfigCnt);
701}
702
703sk_sp<GrDrawContext> GrContext::makeDrawContextWithFallback(SkBackingFit fit,
704 int width, int height,
705 GrPixelConfig config,
706 sk_sp<SkColorSpace> colorSpace,
707 int sampleCnt,
708 GrSurfaceOrigin origin,
709 const SkSurfaceProps* surfaceProps,
710 SkBudgeted budgeted) {
711 if (!this->caps()->isConfigRenderable(config, sampleCnt > 0)) {
712 config = GrPixelConfigFallback(config);
713 }
714
715 return this->makeDrawContext(fit, width, height, config, std::move(colorSpace),
716 sampleCnt, origin, surfaceProps, budgeted);
717}
718
robertphillips6738c702016-07-27 12:13:51 -0700719sk_sp<GrDrawContext> GrContext::makeDrawContext(SkBackingFit fit,
720 int width, int height,
721 GrPixelConfig config,
722 sk_sp<SkColorSpace> colorSpace,
723 int sampleCnt,
724 GrSurfaceOrigin origin,
725 const SkSurfaceProps* surfaceProps,
726 SkBudgeted budgeted) {
robertphillips48fde9c2016-09-06 05:20:20 -0700727 if (!this->caps()->isConfigRenderable(config, sampleCnt > 0)) {
728 return nullptr;
729 }
730
robertphillipsd4c741e2016-04-28 09:55:15 -0700731 GrSurfaceDesc desc;
732 desc.fFlags = kRenderTarget_GrSurfaceFlag;
733 desc.fOrigin = origin;
734 desc.fWidth = width;
735 desc.fHeight = height;
736 desc.fConfig = config;
737 desc.fSampleCnt = sampleCnt;
738
739 sk_sp<GrTexture> tex;
robertphillips76948d42016-05-04 12:47:41 -0700740 if (SkBackingFit::kExact == fit) {
robertphillipsca6eafc2016-05-17 09:57:46 -0700741 tex.reset(this->textureProvider()->createTexture(desc, budgeted));
robertphillipsd4c741e2016-04-28 09:55:15 -0700742 } else {
743 tex.reset(this->textureProvider()->createApproxTexture(desc));
744 }
745 if (!tex) {
746 return nullptr;
747 }
748
robertphillips4fd74ae2016-08-03 14:26:53 -0700749 sk_sp<GrDrawContext> drawContext(this->contextPriv().makeWrappedDrawContext(
750 sk_ref_sp(tex->asRenderTarget()),
robertphillips6738c702016-07-27 12:13:51 -0700751 std::move(colorSpace), surfaceProps));
robertphillipsd4c741e2016-04-28 09:55:15 -0700752 if (!drawContext) {
753 return nullptr;
754 }
755
756 return drawContext;
757}
758
joshualitt1de610a2016-01-06 08:26:09 -0800759bool GrContext::abandoned() const {
760 ASSERT_SINGLE_OWNER
robertphillips7761d612016-05-16 09:14:53 -0700761 return fDrawingManager->wasAbandoned();
robertphillips77a2e522015-10-17 07:43:27 -0700762}
763
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000764namespace {
765void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
766 GrConfigConversionEffect::PMConversion pmToUPM;
767 GrConfigConversionEffect::PMConversion upmToPM;
768 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
769 *pmToUPMValue = pmToUPM;
770 *upmToPMValue = upmToPM;
771}
772}
773
bsalomon6c6f6582015-09-10 08:12:46 -0700774void GrContext::testPMConversionsIfNecessary(uint32_t flags) {
joshualitt1de610a2016-01-06 08:26:09 -0800775 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700776 if (SkToBool(kUnpremul_PixelOpsFlag & flags)) {
777 SkAutoMutexAcquire ama(fTestPMConversionsMutex);
778 if (!fDidTestPMConversions) {
779 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
780 fDidTestPMConversions = true;
781 }
782 }
783}
784
bungeman06ca8ec2016-06-09 08:01:03 -0700785sk_sp<GrFragmentProcessor> GrContext::createPMToUPMEffect(GrTexture* texture,
bsalomon6c9cd552016-01-22 07:17:34 -0800786 const GrSwizzle& swizzle,
bsalomon6c6f6582015-09-10 08:12:46 -0700787 const SkMatrix& matrix) const {
joshualitt1de610a2016-01-06 08:26:09 -0800788 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700789 // We should have already called this->testPMConversionsIfNecessary().
790 SkASSERT(fDidTestPMConversions);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000791 GrConfigConversionEffect::PMConversion pmToUPM =
792 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
793 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bungeman06ca8ec2016-06-09 08:01:03 -0700794 return GrConfigConversionEffect::Make(texture, swizzle, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000795 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700796 return nullptr;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000797 }
798}
799
bungeman06ca8ec2016-06-09 08:01:03 -0700800sk_sp<GrFragmentProcessor> GrContext::createUPMToPMEffect(GrTexture* texture,
bsalomon6c9cd552016-01-22 07:17:34 -0800801 const GrSwizzle& swizzle,
bsalomon6c6f6582015-09-10 08:12:46 -0700802 const SkMatrix& matrix) const {
joshualitt1de610a2016-01-06 08:26:09 -0800803 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700804 // We should have already called this->testPMConversionsIfNecessary().
805 SkASSERT(fDidTestPMConversions);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000806 GrConfigConversionEffect::PMConversion upmToPM =
807 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
808 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
bungeman06ca8ec2016-06-09 08:01:03 -0700809 return GrConfigConversionEffect::Make(texture, swizzle, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000810 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700811 return nullptr;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000812 }
813}
814
bsalomon636e8022015-07-29 06:08:46 -0700815bool GrContext::didFailPMUPMConversionTest() const {
joshualitt1de610a2016-01-06 08:26:09 -0800816 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700817 // We should have already called this->testPMConversionsIfNecessary().
818 SkASSERT(fDidTestPMConversions);
bsalomon636e8022015-07-29 06:08:46 -0700819 // The PM<->UPM tests fail or succeed together so we only need to check one.
bsalomon6c6f6582015-09-10 08:12:46 -0700820 return GrConfigConversionEffect::kNone_PMConversion == fPMToUPMConversion;
bsalomon636e8022015-07-29 06:08:46 -0700821}
822
bsalomon37f9a262015-02-02 13:00:10 -0800823//////////////////////////////////////////////////////////////////////////////
824
825void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800826 ASSERT_SINGLE_OWNER
bsalomon37f9a262015-02-02 13:00:10 -0800827 if (maxTextures) {
bsalomon0ea80f42015-02-11 10:49:59 -0800828 *maxTextures = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -0800829 }
830 if (maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800831 *maxTextureBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -0800832 }
833}
834
835void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
joshualitt1de610a2016-01-06 08:26:09 -0800836 ASSERT_SINGLE_OWNER
bsalomon0ea80f42015-02-11 10:49:59 -0800837 fResourceCache->setLimits(maxTextures, maxTextureBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800838}
839
ericrk0a5fa482015-09-15 14:16:10 -0700840//////////////////////////////////////////////////////////////////////////////
841
842void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -0800843 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -0700844 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
845}