blob: 47fb32312f42684ca389083851ec53c96f71349b [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);
bsalomon6a2b1942016-09-08 11:28:59 -0700521 fDrawingManager->prepareSurfaceForExternalIO(surface);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000522}
523
bsalomonb8fea972016-02-16 07:34:17 -0800524bool GrContext::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
525 const SkIPoint& dstPoint) {
joshualitt1de610a2016-01-06 08:26:09 -0800526 ASSERT_SINGLE_OWNER
bsalomonb8fea972016-02-16 07:34:17 -0800527 RETURN_FALSE_IF_ABANDONED
joshualittbc907352016-01-13 06:45:40 -0800528 GR_AUDIT_TRAIL_AUTO_FRAME(&fAuditTrail, "GrContext::copySurface");
529
robertphillipsea461502015-05-26 11:38:03 -0700530 if (!src || !dst) {
bsalomonb8fea972016-02-16 07:34:17 -0800531 return false;
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000532 }
bsalomone3d4bf22014-09-23 09:15:03 -0700533 ASSERT_OWNED_RESOURCE(src);
junov2bb52102014-09-29 10:18:59 -0700534 ASSERT_OWNED_RESOURCE(dst);
Brian Salomon34a98952014-09-24 11:41:24 -0400535
robertphillipsea461502015-05-26 11:38:03 -0700536 if (!dst->asRenderTarget()) {
bsalomonb8fea972016-02-16 07:34:17 -0800537 SkIRect clippedSrcRect;
538 SkIPoint clippedDstPoint;
539 if (!GrCopySurfaceBatch::ClipSrcRectAndDstPoint(dst, src, srcRect, dstPoint,
540 &clippedSrcRect, &clippedDstPoint)) {
541 return false;
542 }
543 // If we don't have an RT for the dst then we won't have a GrDrawContext to insert the
544 // the copy surface into. In the future we plan to have a more limited Context type
545 // (GrCopyContext?) that has the subset of GrDrawContext operations that should be
546 // allowed on textures that aren't render targets.
547 // For now we just flush any writes to the src and issue an immediate copy to the dst.
548 src->flushWrites();
549 return fGpu->copySurface(dst, src, clippedSrcRect, clippedDstPoint);
robertphillipsea461502015-05-26 11:38:03 -0700550 }
robertphillips4fd74ae2016-08-03 14:26:53 -0700551 sk_sp<GrDrawContext> drawContext(this->contextPriv().makeWrappedDrawContext(
552 sk_ref_sp(dst->asRenderTarget()),
553 nullptr));
kjlubick0eed9452016-02-11 12:05:24 -0800554 if (!drawContext) {
bsalomonb8fea972016-02-16 07:34:17 -0800555 return false;
bsalomonf80bfed2014-10-07 05:56:02 -0700556 }
kjlubick0eed9452016-02-11 12:05:24 -0800557
bsalomonb8fea972016-02-16 07:34:17 -0800558 if (!drawContext->copySurface(src, srcRect, dstPoint)) {
559 return false;
kjlubick0eed9452016-02-11 12:05:24 -0800560 }
bsalomonb8fea972016-02-16 07:34:17 -0800561 return true;
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000562}
563
bsalomonf80bfed2014-10-07 05:56:02 -0700564void GrContext::flushSurfaceWrites(GrSurface* surface) {
joshualitt1de610a2016-01-06 08:26:09 -0800565 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800566 RETURN_IF_ABANDONED
bsalomonf80bfed2014-10-07 05:56:02 -0700567 if (surface->surfacePriv().hasPendingWrite()) {
568 this->flush();
569 }
570}
571
ajuma95243eb2016-08-24 08:19:02 -0700572void GrContext::flushSurfaceIO(GrSurface* surface) {
573 ASSERT_SINGLE_OWNER
574 RETURN_IF_ABANDONED
575 if (surface->surfacePriv().hasPendingIO()) {
576 this->flush();
577 }
578}
579
bsalomon@google.com27847de2011-02-22 20:59:41 +0000580////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000581int GrContext::getRecommendedSampleCount(GrPixelConfig config,
582 SkScalar dpi) const {
joshualitt1de610a2016-01-06 08:26:09 -0800583 ASSERT_SINGLE_OWNER
584
bsalomon76228632015-05-29 08:02:10 -0700585 if (!this->caps()->isConfigRenderable(config, true)) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000586 return 0;
587 }
588 int chosenSampleCount = 0;
jvanverthe9c0fc62015-04-29 11:18:05 -0700589 if (fGpu->caps()->shaderCaps()->pathRenderingSupport()) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000590 if (dpi >= 250.0f) {
591 chosenSampleCount = 4;
592 } else {
593 chosenSampleCount = 16;
594 }
595 }
egdanieleed519e2016-01-15 11:36:18 -0800596 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ? chosenSampleCount : 0;
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000597}
598
robertphillips4fd74ae2016-08-03 14:26:53 -0700599sk_sp<GrDrawContext> GrContextPriv::makeWrappedDrawContext(sk_sp<GrRenderTarget> rt,
600 sk_sp<SkColorSpace> colorSpace,
601 const SkSurfaceProps* surfaceProps) {
602 ASSERT_SINGLE_OWNER_PRIV
csmartdaltonbde96c62016-08-31 12:54:46 -0700603 return this->drawingManager()->makeDrawContext(std::move(rt),
604 std::move(colorSpace),
605 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -0700606}
robertphillips77a2e522015-10-17 07:43:27 -0700607
robertphillips4fd74ae2016-08-03 14:26:53 -0700608sk_sp<GrDrawContext> GrContextPriv::makeBackendTextureDrawContext(const GrBackendTextureDesc& desc,
609 sk_sp<SkColorSpace> colorSpace,
610 const SkSurfaceProps* props,
611 GrWrapOwnership ownership) {
612 ASSERT_SINGLE_OWNER_PRIV
613 SkASSERT(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
614
615 sk_sp<GrSurface> surface(fContext->textureProvider()->wrapBackendTexture(desc, ownership));
616 if (!surface) {
617 return nullptr;
618 }
619
csmartdaltonbde96c62016-08-31 12:54:46 -0700620 return this->drawingManager()->makeDrawContext(sk_ref_sp(surface->asRenderTarget()),
621 std::move(colorSpace), props);
robertphillips4fd74ae2016-08-03 14:26:53 -0700622}
623
624sk_sp<GrDrawContext> GrContextPriv::makeBackendRenderTargetDrawContext(
625 const GrBackendRenderTargetDesc& desc,
626 sk_sp<SkColorSpace> colorSpace,
627 const SkSurfaceProps* surfaceProps) {
628 ASSERT_SINGLE_OWNER_PRIV
629
630 sk_sp<GrRenderTarget> rt(fContext->textureProvider()->wrapBackendRenderTarget(desc));
631 if (!rt) {
632 return nullptr;
633 }
634
csmartdaltonbde96c62016-08-31 12:54:46 -0700635 return this->drawingManager()->makeDrawContext(std::move(rt),
636 std::move(colorSpace),
637 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -0700638}
639
640sk_sp<GrDrawContext> GrContextPriv::makeBackendTextureAsRenderTargetDrawContext(
egdaniela95d46b2016-08-15 08:06:29 -0700641 const GrBackendTextureDesc& desc,
robertphillips4fd74ae2016-08-03 14:26:53 -0700642 sk_sp<SkColorSpace> colorSpace,
643 const SkSurfaceProps* surfaceProps) {
644 ASSERT_SINGLE_OWNER_PRIV
645 SkASSERT(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
646
647 sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTextureAsRenderTarget(desc));
648 if (!surface) {
649 return nullptr;
650 }
651
csmartdaltonbde96c62016-08-31 12:54:46 -0700652 return this->drawingManager()->makeDrawContext(sk_ref_sp(surface->asRenderTarget()),
653 std::move(colorSpace),
654 surfaceProps);
robertphillips77a2e522015-10-17 07:43:27 -0700655}
656
robertphillips48fde9c2016-09-06 05:20:20 -0700657static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) {
658 static const GrPixelConfig kFallback[] = {
659 kUnknown_GrPixelConfig, // kUnknown_GrPixelConfig
660 kRGBA_8888_GrPixelConfig, // kAlpha_8_GrPixelConfig
661 kUnknown_GrPixelConfig, // kIndex_8_GrPixelConfig
662 kRGBA_8888_GrPixelConfig, // kRGB_565_GrPixelConfig
663 kRGBA_8888_GrPixelConfig, // kRGBA_4444_GrPixelConfig
664 kUnknown_GrPixelConfig, // kRGBA_8888_GrPixelConfig
665 kRGBA_8888_GrPixelConfig, // kBGRA_8888_GrPixelConfig
666 kUnknown_GrPixelConfig, // kSRGBA_8888_GrPixelConfig
667 kSRGBA_8888_GrPixelConfig, // kSBGRA_8888_GrPixelConfig
668 kUnknown_GrPixelConfig, // kETC1_GrPixelConfig
669 kUnknown_GrPixelConfig, // kLATC_GrPixelConfig
670 kUnknown_GrPixelConfig, // kR11_EAC_GrPixelConfig
671 kUnknown_GrPixelConfig, // kASTC_12x12_GrPixelConfig
672 kUnknown_GrPixelConfig, // kRGBA_float_GrPixelConfig
673 kRGBA_half_GrPixelConfig, // kAlpha_half_GrPixelConfig
674 kUnknown_GrPixelConfig, // kRGBA_half_GrPixelConfig
675 };
676 return kFallback[config];
677
678 GR_STATIC_ASSERT(0 == kUnknown_GrPixelConfig);
679 GR_STATIC_ASSERT(1 == kAlpha_8_GrPixelConfig);
680 GR_STATIC_ASSERT(2 == kIndex_8_GrPixelConfig);
681 GR_STATIC_ASSERT(3 == kRGB_565_GrPixelConfig);
682 GR_STATIC_ASSERT(4 == kRGBA_4444_GrPixelConfig);
683 GR_STATIC_ASSERT(5 == kRGBA_8888_GrPixelConfig);
684 GR_STATIC_ASSERT(6 == kBGRA_8888_GrPixelConfig);
685 GR_STATIC_ASSERT(7 == kSRGBA_8888_GrPixelConfig);
686 GR_STATIC_ASSERT(8 == kSBGRA_8888_GrPixelConfig);
687 GR_STATIC_ASSERT(9 == kETC1_GrPixelConfig);
688 GR_STATIC_ASSERT(10 == kLATC_GrPixelConfig);
689 GR_STATIC_ASSERT(11 == kR11_EAC_GrPixelConfig);
690 GR_STATIC_ASSERT(12 == kASTC_12x12_GrPixelConfig);
691 GR_STATIC_ASSERT(13 == kRGBA_float_GrPixelConfig);
692 GR_STATIC_ASSERT(14 == kAlpha_half_GrPixelConfig);
693 GR_STATIC_ASSERT(15 == kRGBA_half_GrPixelConfig);
694 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kFallback) == kGrPixelConfigCnt);
695}
696
697sk_sp<GrDrawContext> GrContext::makeDrawContextWithFallback(SkBackingFit fit,
698 int width, int height,
699 GrPixelConfig config,
700 sk_sp<SkColorSpace> colorSpace,
701 int sampleCnt,
702 GrSurfaceOrigin origin,
703 const SkSurfaceProps* surfaceProps,
704 SkBudgeted budgeted) {
705 if (!this->caps()->isConfigRenderable(config, sampleCnt > 0)) {
706 config = GrPixelConfigFallback(config);
707 }
708
709 return this->makeDrawContext(fit, width, height, config, std::move(colorSpace),
710 sampleCnt, origin, surfaceProps, budgeted);
711}
712
robertphillips6738c702016-07-27 12:13:51 -0700713sk_sp<GrDrawContext> GrContext::makeDrawContext(SkBackingFit fit,
714 int width, int height,
715 GrPixelConfig config,
716 sk_sp<SkColorSpace> colorSpace,
717 int sampleCnt,
718 GrSurfaceOrigin origin,
719 const SkSurfaceProps* surfaceProps,
720 SkBudgeted budgeted) {
robertphillips48fde9c2016-09-06 05:20:20 -0700721 if (!this->caps()->isConfigRenderable(config, sampleCnt > 0)) {
722 return nullptr;
723 }
724
robertphillipsd4c741e2016-04-28 09:55:15 -0700725 GrSurfaceDesc desc;
726 desc.fFlags = kRenderTarget_GrSurfaceFlag;
727 desc.fOrigin = origin;
728 desc.fWidth = width;
729 desc.fHeight = height;
730 desc.fConfig = config;
731 desc.fSampleCnt = sampleCnt;
732
733 sk_sp<GrTexture> tex;
robertphillips76948d42016-05-04 12:47:41 -0700734 if (SkBackingFit::kExact == fit) {
robertphillipsca6eafc2016-05-17 09:57:46 -0700735 tex.reset(this->textureProvider()->createTexture(desc, budgeted));
robertphillipsd4c741e2016-04-28 09:55:15 -0700736 } else {
737 tex.reset(this->textureProvider()->createApproxTexture(desc));
738 }
739 if (!tex) {
740 return nullptr;
741 }
742
robertphillips4fd74ae2016-08-03 14:26:53 -0700743 sk_sp<GrDrawContext> drawContext(this->contextPriv().makeWrappedDrawContext(
744 sk_ref_sp(tex->asRenderTarget()),
robertphillips6738c702016-07-27 12:13:51 -0700745 std::move(colorSpace), surfaceProps));
robertphillipsd4c741e2016-04-28 09:55:15 -0700746 if (!drawContext) {
747 return nullptr;
748 }
749
750 return drawContext;
751}
752
joshualitt1de610a2016-01-06 08:26:09 -0800753bool GrContext::abandoned() const {
754 ASSERT_SINGLE_OWNER
robertphillips7761d612016-05-16 09:14:53 -0700755 return fDrawingManager->wasAbandoned();
robertphillips77a2e522015-10-17 07:43:27 -0700756}
757
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000758namespace {
759void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
760 GrConfigConversionEffect::PMConversion pmToUPM;
761 GrConfigConversionEffect::PMConversion upmToPM;
762 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
763 *pmToUPMValue = pmToUPM;
764 *upmToPMValue = upmToPM;
765}
766}
767
bsalomon6c6f6582015-09-10 08:12:46 -0700768void GrContext::testPMConversionsIfNecessary(uint32_t flags) {
joshualitt1de610a2016-01-06 08:26:09 -0800769 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700770 if (SkToBool(kUnpremul_PixelOpsFlag & flags)) {
771 SkAutoMutexAcquire ama(fTestPMConversionsMutex);
772 if (!fDidTestPMConversions) {
773 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
774 fDidTestPMConversions = true;
775 }
776 }
777}
778
bungeman06ca8ec2016-06-09 08:01:03 -0700779sk_sp<GrFragmentProcessor> GrContext::createPMToUPMEffect(GrTexture* texture,
bsalomon6c9cd552016-01-22 07:17:34 -0800780 const GrSwizzle& swizzle,
bsalomon6c6f6582015-09-10 08:12:46 -0700781 const SkMatrix& matrix) const {
joshualitt1de610a2016-01-06 08:26:09 -0800782 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700783 // We should have already called this->testPMConversionsIfNecessary().
784 SkASSERT(fDidTestPMConversions);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000785 GrConfigConversionEffect::PMConversion pmToUPM =
786 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
787 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bungeman06ca8ec2016-06-09 08:01:03 -0700788 return GrConfigConversionEffect::Make(texture, swizzle, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000789 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700790 return nullptr;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000791 }
792}
793
bungeman06ca8ec2016-06-09 08:01:03 -0700794sk_sp<GrFragmentProcessor> GrContext::createUPMToPMEffect(GrTexture* texture,
bsalomon6c9cd552016-01-22 07:17:34 -0800795 const GrSwizzle& swizzle,
bsalomon6c6f6582015-09-10 08:12:46 -0700796 const SkMatrix& matrix) const {
joshualitt1de610a2016-01-06 08:26:09 -0800797 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700798 // We should have already called this->testPMConversionsIfNecessary().
799 SkASSERT(fDidTestPMConversions);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000800 GrConfigConversionEffect::PMConversion upmToPM =
801 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
802 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
bungeman06ca8ec2016-06-09 08:01:03 -0700803 return GrConfigConversionEffect::Make(texture, swizzle, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000804 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700805 return nullptr;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000806 }
807}
808
bsalomon636e8022015-07-29 06:08:46 -0700809bool GrContext::didFailPMUPMConversionTest() const {
joshualitt1de610a2016-01-06 08:26:09 -0800810 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700811 // We should have already called this->testPMConversionsIfNecessary().
812 SkASSERT(fDidTestPMConversions);
bsalomon636e8022015-07-29 06:08:46 -0700813 // The PM<->UPM tests fail or succeed together so we only need to check one.
bsalomon6c6f6582015-09-10 08:12:46 -0700814 return GrConfigConversionEffect::kNone_PMConversion == fPMToUPMConversion;
bsalomon636e8022015-07-29 06:08:46 -0700815}
816
bsalomon37f9a262015-02-02 13:00:10 -0800817//////////////////////////////////////////////////////////////////////////////
818
819void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800820 ASSERT_SINGLE_OWNER
bsalomon37f9a262015-02-02 13:00:10 -0800821 if (maxTextures) {
bsalomon0ea80f42015-02-11 10:49:59 -0800822 *maxTextures = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -0800823 }
824 if (maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800825 *maxTextureBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -0800826 }
827}
828
829void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
joshualitt1de610a2016-01-06 08:26:09 -0800830 ASSERT_SINGLE_OWNER
bsalomon0ea80f42015-02-11 10:49:59 -0800831 fResourceCache->setLimits(maxTextures, maxTextureBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800832}
833
ericrk0a5fa482015-09-15 14:16:10 -0700834//////////////////////////////////////////////////////////////////////////////
835
836void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -0800837 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -0700838 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
839}