blob: 6cec1da86bdc8f5da6ddeeeb12964c4cc044d1bd [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;
bsalomon6b2552f2016-09-15 13:50:26 -070098 GrPathRendererChain::Options prcOptions;
99 prcOptions.fDisableDistanceFieldRenderer = options.fDisableDistanceFieldPaths;
bsalomon39ef7fb2016-09-21 11:16:05 -0700100 prcOptions.fAllowPathMaskCaching = options.fAllowPathMaskCaching;
101 prcOptions.fDisableAllPathRenderers = options.fForceSWPathMasks;
bsalomon6b2552f2016-09-15 13:50:26 -0700102 fDrawingManager.reset(new GrDrawingManager(this, dtOptions, prcOptions, options.fImmediateMode,
bsalomonb77a9072016-09-07 10:02:04 -0700103 &fSingleOwner));
joshualitt7c3a2f82015-03-31 13:32:05 -0700104
105 // GrBatchFontCache will eventually replace GrFontCache
halcanary385fe4d2015-08-26 13:07:48 -0700106 fBatchFontCache = new GrBatchFontCache(this);
joshualittb7133be2015-04-08 09:08:31 -0700107
halcanary385fe4d2015-08-26 13:07:48 -0700108 fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB, this));
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000109}
110
bsalomon@google.com27847de2011-02-22 20:59:41 +0000111GrContext::~GrContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800112 ASSERT_SINGLE_OWNER
113
robertphillipsea461502015-05-26 11:38:03 -0700114 if (!fGpu) {
bsalomon76228632015-05-29 08:02:10 -0700115 SkASSERT(!fCaps);
bsalomon@google.com733c0622013-04-24 17:59:32 +0000116 return;
117 }
118
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000119 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000120
robertphillips77a2e522015-10-17 07:43:27 -0700121 fDrawingManager->cleanup();
robertphillips2334fb62015-06-17 05:43:33 -0700122
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000123 for (int i = 0; i < fCleanUpData.count(); ++i) {
124 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
125 }
126
halcanary385fe4d2015-08-26 13:07:48 -0700127 delete fResourceProvider;
128 delete fResourceCache;
129 delete fBatchFontCache;
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000130
bsalomon@google.com205d4602011-04-25 12:43:45 +0000131 fGpu->unref();
bsalomon76228632015-05-29 08:02:10 -0700132 fCaps->unref();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000133}
134
bsalomon41b952c2016-03-11 06:46:33 -0800135GrContextThreadSafeProxy* GrContext::threadSafeProxy() {
136 if (!fThreadSafeProxy) {
137 fThreadSafeProxy.reset(new GrContextThreadSafeProxy(fCaps, this->uniqueID()));
138 }
139 return SkRef(fThreadSafeProxy.get());
140}
141
bsalomon2354f842014-07-28 13:48:36 -0700142void GrContext::abandonContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800143 ASSERT_SINGLE_OWNER
144
bsalomond309e7a2015-04-30 14:18:54 -0700145 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800146
147 // Need to abandon the drawing manager first so all the render targets
148 // will be released/forgotten before they too are abandoned.
149 fDrawingManager->abandon();
150
bsalomon@google.com205d4602011-04-25 12:43:45 +0000151 // abandon first to so destructors
152 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800153 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700154
bsalomon6e2aad42016-04-01 11:54:31 -0700155 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
156
157 fBatchFontCache->freeAll();
bsalomon6e2aad42016-04-01 11:54:31 -0700158 fTextBlobCache->freeAll();
159}
160
161void GrContext::releaseResourcesAndAbandonContext() {
162 ASSERT_SINGLE_OWNER
163
164 fResourceProvider->abandon();
165
166 // Need to abandon the drawing manager first so all the render targets
167 // will be released/forgotten before they too are abandoned.
168 fDrawingManager->abandon();
169
170 // Release all resources in the backend 3D API.
171 fResourceCache->releaseAll();
172
173 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000174
joshualitt7c3a2f82015-03-31 13:32:05 -0700175 fBatchFontCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700176 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000177}
178
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000179void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800180 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000181 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000182}
183
184void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800185 ASSERT_SINGLE_OWNER
186
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000187 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000188
joshualitt7c3a2f82015-03-31 13:32:05 -0700189 fBatchFontCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700190
191 fDrawingManager->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700192
193 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000194}
195
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000196void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800197 ASSERT_SINGLE_OWNER
198
bsalomon71cb0c22014-11-14 12:10:14 -0800199 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800200 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800201 }
202 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800203 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800204 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000205}
206
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000207////////////////////////////////////////////////////////////////////////////////
208
joshualitt0db6dfa2015-04-10 07:01:30 -0700209void GrContext::TextBlobCacheOverBudgetCB(void* data) {
210 SkASSERT(data);
bsalomonb77a9072016-09-07 10:02:04 -0700211 // TextBlobs are drawn at the SkGpuDevice level, therefore they cannot rely on GrDrawContext
212 // to perform a necessary flush. The solution is to move drawText calls to below the GrContext
213 // level, but this is not trivial because they call drawPath on SkGpuDevice.
joshualitt0db6dfa2015-04-10 07:01:30 -0700214 GrContext* context = reinterpret_cast<GrContext*>(data);
215 context->flush();
216}
217
bsalomon@google.com27847de2011-02-22 20:59:41 +0000218////////////////////////////////////////////////////////////////////////////////
219
bsalomonb77a9072016-09-07 10:02:04 -0700220void GrContext::flush() {
joshualitt1de610a2016-01-06 08:26:09 -0800221 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700222 RETURN_IF_ABANDONED
bsalomonb77a9072016-09-07 10:02:04 -0700223 fDrawingManager->flush();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000224}
225
bsalomon81beccc2014-10-13 12:32:55 -0700226bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
227 const void* inPixels, size_t outRowBytes, void* outPixels) {
228 SkSrcPixelInfo srcPI;
brianosman396fcdb2016-07-22 06:26:11 -0700229 if (!GrPixelConfigToColorType(srcConfig, &srcPI.fColorType)) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000230 return false;
231 }
bsalomon81beccc2014-10-13 12:32:55 -0700232 srcPI.fAlphaType = kUnpremul_SkAlphaType;
233 srcPI.fPixels = inPixels;
234 srcPI.fRowBytes = inRowBytes;
235
236 SkDstPixelInfo dstPI;
237 dstPI.fColorType = srcPI.fColorType;
238 dstPI.fAlphaType = kPremul_SkAlphaType;
239 dstPI.fPixels = outPixels;
240 dstPI.fRowBytes = outRowBytes;
241
242 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000243}
244
bsalomon81beccc2014-10-13 12:32:55 -0700245bool GrContext::writeSurfacePixels(GrSurface* surface,
246 int left, int top, int width, int height,
247 GrPixelConfig srcConfig, const void* buffer, size_t rowBytes,
248 uint32_t pixelOpsFlags) {
joshualitt1de610a2016-01-06 08:26:09 -0800249 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800250 RETURN_FALSE_IF_ABANDONED
bsalomon6c6f6582015-09-10 08:12:46 -0700251 ASSERT_OWNED_RESOURCE(surface);
252 SkASSERT(surface);
joshualittbc907352016-01-13 06:45:40 -0800253 GR_AUDIT_TRAIL_AUTO_FRAME(&fAuditTrail, "GrContext::writeSurfacePixels");
bsalomon6c6f6582015-09-10 08:12:46 -0700254
255 this->testPMConversionsIfNecessary(pixelOpsFlags);
bsalomon81beccc2014-10-13 12:32:55 -0700256
bsalomone8d21e82015-07-16 08:23:13 -0700257 // 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 -0700258 // necessary and because GrGpu::getWritePixelsInfo requires it.
bsalomone8d21e82015-07-16 08:23:13 -0700259 if (!GrSurfacePriv::AdjustWritePixelParams(surface->width(), surface->height(),
260 GrBytesPerPixel(srcConfig), &left, &top, &width,
261 &height, &buffer, &rowBytes)) {
262 return false;
263 }
264
bsalomonf0674512015-07-28 13:26:15 -0700265 bool applyPremulToSrc = false;
bsalomon81beccc2014-10-13 12:32:55 -0700266 if (kUnpremul_PixelOpsFlag & pixelOpsFlags) {
267 if (!GrPixelConfigIs8888(srcConfig)) {
268 return false;
269 }
bsalomonf0674512015-07-28 13:26:15 -0700270 applyPremulToSrc = true;
271 }
bsalomon636e8022015-07-29 06:08:46 -0700272
273 GrGpu::DrawPreference drawPreference = GrGpu::kNoDraw_DrawPreference;
274 // Don't prefer to draw for the conversion (and thereby access a texture from the cache) when
275 // we've already determined that there isn't a roundtrip preserving conversion processor pair.
276 if (applyPremulToSrc && !this->didFailPMUPMConversionTest()) {
277 drawPreference = GrGpu::kCallerPrefersDraw_DrawPreference;
278 }
279
bsalomonf0674512015-07-28 13:26:15 -0700280 GrGpu::WritePixelTempDrawInfo tempDrawInfo;
cblumeed828002016-02-16 13:00:01 -0800281 if (!fGpu->getWritePixelsInfo(surface, width, height, srcConfig, &drawPreference,
bsalomonf0674512015-07-28 13:26:15 -0700282 &tempDrawInfo)) {
283 return false;
284 }
285
286 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && surface->surfacePriv().hasPendingIO()) {
287 this->flush();
288 }
289
290 SkAutoTUnref<GrTexture> tempTexture;
291 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomoneae62002015-07-31 13:59:30 -0700292 tempTexture.reset(
293 this->textureProvider()->createApproxTexture(tempDrawInfo.fTempSurfaceDesc));
bsalomonf0674512015-07-28 13:26:15 -0700294 if (!tempTexture && GrGpu::kRequireDraw_DrawPreference == drawPreference) {
295 return false;
296 }
297 }
298
299 // temp buffer for doing sw premul conversion, if needed.
300 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
301 if (tempTexture) {
bungeman06ca8ec2016-06-09 08:01:03 -0700302 sk_sp<GrFragmentProcessor> fp;
bsalomonf0674512015-07-28 13:26:15 -0700303 SkMatrix textureMatrix;
304 textureMatrix.setIDiv(tempTexture->width(), tempTexture->height());
bsalomonf0674512015-07-28 13:26:15 -0700305 if (applyPremulToSrc) {
bungeman06ca8ec2016-06-09 08:01:03 -0700306 fp = this->createUPMToPMEffect(tempTexture, tempDrawInfo.fSwizzle, textureMatrix);
bsalomonf0674512015-07-28 13:26:15 -0700307 // If premultiplying was the only reason for the draw, fall back to a straight write.
308 if (!fp) {
309 if (GrGpu::kCallerPrefersDraw_DrawPreference == drawPreference) {
halcanary96fcdcc2015-08-27 07:41:13 -0700310 tempTexture.reset(nullptr);
bsalomonf0674512015-07-28 13:26:15 -0700311 }
312 } else {
313 applyPremulToSrc = false;
314 }
315 }
316 if (tempTexture) {
317 if (!fp) {
bungeman06ca8ec2016-06-09 08:01:03 -0700318 fp = GrConfigConversionEffect::Make(tempTexture, tempDrawInfo.fSwizzle,
319 GrConfigConversionEffect::kNone_PMConversion,
320 textureMatrix);
bsalomonf0674512015-07-28 13:26:15 -0700321 if (!fp) {
322 return false;
323 }
324 }
325 GrRenderTarget* renderTarget = surface->asRenderTarget();
326 SkASSERT(renderTarget);
327 if (tempTexture->surfacePriv().hasPendingIO()) {
328 this->flush();
329 }
330 if (applyPremulToSrc) {
331 size_t tmpRowBytes = 4 * width;
332 tmpPixels.reset(width * height);
333 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
334 tmpPixels.get())) {
335 return false;
336 }
337 rowBytes = tmpRowBytes;
338 buffer = tmpPixels.get();
339 applyPremulToSrc = false;
340 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700341 if (!fGpu->writePixels(tempTexture, 0, 0, width, height,
bsalomon6c9cd552016-01-22 07:17:34 -0800342 tempDrawInfo.fWriteConfig, buffer,
bsalomon6cb3cbe2015-07-30 07:34:27 -0700343 rowBytes)) {
bsalomonf0674512015-07-28 13:26:15 -0700344 return false;
345 }
346 SkMatrix matrix;
347 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
brianosmandfe4f2e2016-07-21 13:28:36 -0700348 // TODO: Need to decide the semantics of this function for color spaces. Do we support
349 // conversion from a passed-in color space? For now, specifying nullptr means that this
350 // path will do no conversion, so it will match the behavior of the non-draw path.
robertphillips4fd74ae2016-08-03 14:26:53 -0700351 sk_sp<GrDrawContext> drawContext(this->contextPriv().makeWrappedDrawContext(
352 sk_ref_sp(renderTarget),
353 nullptr));
bsalomonf0674512015-07-28 13:26:15 -0700354 if (!drawContext) {
355 return false;
356 }
egdanielc4b72722015-11-23 13:20:41 -0800357 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700358 paint.addColorFragmentProcessor(std::move(fp));
egdanielc4b72722015-11-23 13:20:41 -0800359 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
brianosmana167e742016-05-24 06:18:48 -0700360 paint.setAllowSRGBInputs(true);
bsalomonf0674512015-07-28 13:26:15 -0700361 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
cdalton846c0512016-05-13 10:25:00 -0700362 drawContext->drawRect(GrNoClip(), paint, matrix, rect, nullptr);
bsalomonf0674512015-07-28 13:26:15 -0700363
364 if (kFlushWrites_PixelOp & pixelOpsFlags) {
365 this->flushSurfaceWrites(surface);
366 }
367 }
368 }
369 if (!tempTexture) {
bsalomonf0674512015-07-28 13:26:15 -0700370 if (applyPremulToSrc) {
bsalomon81beccc2014-10-13 12:32:55 -0700371 size_t tmpRowBytes = 4 * width;
372 tmpPixels.reset(width * height);
373 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
374 tmpPixels.get())) {
375 return false;
376 }
377 rowBytes = tmpRowBytes;
378 buffer = tmpPixels.get();
bsalomonf0674512015-07-28 13:26:15 -0700379 applyPremulToSrc = false;
bsalomon81beccc2014-10-13 12:32:55 -0700380 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700381 return fGpu->writePixels(surface, left, top, width, height, srcConfig, buffer, rowBytes);
bsalomon81beccc2014-10-13 12:32:55 -0700382 }
bsalomon81beccc2014-10-13 12:32:55 -0700383 return true;
384}
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000385
bsalomone8d21e82015-07-16 08:23:13 -0700386bool GrContext::readSurfacePixels(GrSurface* src,
387 int left, int top, int width, int height,
388 GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
389 uint32_t flags) {
joshualitt1de610a2016-01-06 08:26:09 -0800390 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800391 RETURN_FALSE_IF_ABANDONED
bsalomone8d21e82015-07-16 08:23:13 -0700392 ASSERT_OWNED_RESOURCE(src);
393 SkASSERT(src);
joshualittbc907352016-01-13 06:45:40 -0800394 GR_AUDIT_TRAIL_AUTO_FRAME(&fAuditTrail, "GrContext::readSurfacePixels");
bsalomon32ab2602015-09-09 18:57:49 -0700395
bsalomon6c6f6582015-09-10 08:12:46 -0700396 this->testPMConversionsIfNecessary(flags);
397 SkAutoMutexAcquire ama(fReadPixelsMutex);
398
bsalomone8d21e82015-07-16 08:23:13 -0700399 // Adjust the params so that if we wind up using an intermediate surface we've already done
400 // all the trimming and the temporary can be the min size required.
401 if (!GrSurfacePriv::AdjustReadPixelParams(src->width(), src->height(),
402 GrBytesPerPixel(dstConfig), &left,
403 &top, &width, &height, &buffer, &rowBytes)) {
404 return false;
405 }
406
407 if (!(kDontFlush_PixelOpsFlag & flags) && src->surfacePriv().hasPendingWrite()) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000408 this->flush();
409 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000410
bsalomone8d21e82015-07-16 08:23:13 -0700411 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.com9c680582013-02-06 18:17:50 +0000412 if (unpremul && !GrPixelConfigIs8888(dstConfig)) {
bsalomon39826022015-07-23 08:07:21 -0700413 // The unpremul flag is only allowed for 8888 configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000414 return false;
415 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000416
bsalomon636e8022015-07-29 06:08:46 -0700417 GrGpu::DrawPreference drawPreference = GrGpu::kNoDraw_DrawPreference;
418 // Don't prefer to draw for the conversion (and thereby access a texture from the cache) when
419 // we've already determined that there isn't a roundtrip preserving conversion processor pair.
420 if (unpremul && !this->didFailPMUPMConversionTest()) {
421 drawPreference = GrGpu::kCallerPrefersDraw_DrawPreference;
422 }
423
bsalomon39826022015-07-23 08:07:21 -0700424 GrGpu::ReadPixelTempDrawInfo tempDrawInfo;
425 if (!fGpu->getReadPixelsInfo(src, width, height, rowBytes, dstConfig, &drawPreference,
426 &tempDrawInfo)) {
427 return false;
428 }
bsalomon191bcc02014-11-14 11:31:13 -0800429
bsalomon6cb3cbe2015-07-30 07:34:27 -0700430 SkAutoTUnref<GrSurface> surfaceToRead(SkRef(src));
bsalomon39826022015-07-23 08:07:21 -0700431 bool didTempDraw = false;
432 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomonb117ff12016-07-19 07:24:40 -0700433 if (SkBackingFit::kExact == tempDrawInfo.fTempSurfaceFit) {
bsalomon39826022015-07-23 08:07:21 -0700434 // We only respect this when the entire src is being read. Otherwise we can trigger too
435 // many odd ball texture sizes and trash the cache.
bsalomoneae62002015-07-31 13:59:30 -0700436 if (width != src->width() || height != src->height()) {
bsalomonb117ff12016-07-19 07:24:40 -0700437 tempDrawInfo.fTempSurfaceFit= SkBackingFit::kApprox;
bsalomon39826022015-07-23 08:07:21 -0700438 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000439 }
brianosmandfe4f2e2016-07-21 13:28:36 -0700440 // TODO: Need to decide the semantics of this function for color spaces. Do we support
441 // conversion to a passed-in color space? For now, specifying nullptr means that this
442 // path will do no conversion, so it will match the behavior of the non-draw path.
robertphillips6738c702016-07-27 12:13:51 -0700443 sk_sp<GrDrawContext> tempDC = this->makeDrawContext(tempDrawInfo.fTempSurfaceFit,
bsalomonb117ff12016-07-19 07:24:40 -0700444 tempDrawInfo.fTempSurfaceDesc.fWidth,
445 tempDrawInfo.fTempSurfaceDesc.fHeight,
446 tempDrawInfo.fTempSurfaceDesc.fConfig,
brianosmandfe4f2e2016-07-21 13:28:36 -0700447 nullptr,
bsalomonb117ff12016-07-19 07:24:40 -0700448 tempDrawInfo.fTempSurfaceDesc.fSampleCnt,
449 tempDrawInfo.fTempSurfaceDesc.fOrigin);
450 if (tempDC) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000451 SkMatrix textureMatrix;
bsalomon39826022015-07-23 08:07:21 -0700452 textureMatrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000453 textureMatrix.postIDiv(src->width(), src->height());
bungeman06ca8ec2016-06-09 08:01:03 -0700454 sk_sp<GrFragmentProcessor> fp;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000455 if (unpremul) {
bungeman06ca8ec2016-06-09 08:01:03 -0700456 fp = this->createPMToUPMEffect(src->asTexture(), tempDrawInfo.fSwizzle,
457 textureMatrix);
joshualittb0a8a372014-09-23 09:50:21 -0700458 if (fp) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000459 unpremul = false; // we no longer need to do this on CPU after the read back.
bsalomon39826022015-07-23 08:07:21 -0700460 } else if (GrGpu::kCallerPrefersDraw_DrawPreference == drawPreference) {
461 // We only wanted to do the draw in order to perform the unpremul so don't
462 // bother.
bsalomonb117ff12016-07-19 07:24:40 -0700463 tempDC.reset(nullptr);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000464 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000465 }
bsalomonb117ff12016-07-19 07:24:40 -0700466 if (!fp && tempDC) {
bungeman06ca8ec2016-06-09 08:01:03 -0700467 fp = GrConfigConversionEffect::Make(src->asTexture(), tempDrawInfo.fSwizzle,
468 GrConfigConversionEffect::kNone_PMConversion,
469 textureMatrix);
bsalomon39826022015-07-23 08:07:21 -0700470 }
471 if (fp) {
egdanielc4b72722015-11-23 13:20:41 -0800472 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700473 paint.addColorFragmentProcessor(std::move(fp));
egdanielc4b72722015-11-23 13:20:41 -0800474 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
brianosmana167e742016-05-24 06:18:48 -0700475 paint.setAllowSRGBInputs(true);
bsalomon39826022015-07-23 08:07:21 -0700476 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
bsalomonb117ff12016-07-19 07:24:40 -0700477 tempDC->drawRect(GrNoClip(), paint, SkMatrix::I(), rect, nullptr);
478 surfaceToRead.reset(tempDC->asTexture().release());
bsalomon39826022015-07-23 08:07:21 -0700479 left = 0;
480 top = 0;
481 didTempDraw = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000482 }
bsalomon@google.com0342a852012-08-20 19:22:38 +0000483 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000484 }
joshualitt5c55fef2014-10-31 14:04:35 -0700485
bsalomon39826022015-07-23 08:07:21 -0700486 if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000487 return false;
488 }
bsalomon39826022015-07-23 08:07:21 -0700489 GrPixelConfig configToRead = dstConfig;
490 if (didTempDraw) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700491 this->flushSurfaceWrites(surfaceToRead);
bsalomon6c9cd552016-01-22 07:17:34 -0800492 configToRead = tempDrawInfo.fReadConfig;
bsalomon39826022015-07-23 08:07:21 -0700493 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700494 if (!fGpu->readPixels(surfaceToRead, left, top, width, height, configToRead, buffer,
495 rowBytes)) {
bsalomon39826022015-07-23 08:07:21 -0700496 return false;
497 }
498
499 // Perform umpremul conversion if we weren't able to perform it as a draw.
500 if (unpremul) {
reed@google.com7111d462014-03-25 16:20:24 +0000501 SkDstPixelInfo dstPI;
brianosman396fcdb2016-07-22 06:26:11 -0700502 if (!GrPixelConfigToColorType(dstConfig, &dstPI.fColorType)) {
reed@google.com7111d462014-03-25 16:20:24 +0000503 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000504 }
reed@google.com7111d462014-03-25 16:20:24 +0000505 dstPI.fAlphaType = kUnpremul_SkAlphaType;
506 dstPI.fPixels = buffer;
507 dstPI.fRowBytes = rowBytes;
508
509 SkSrcPixelInfo srcPI;
bsalomon39826022015-07-23 08:07:21 -0700510 srcPI.fColorType = dstPI.fColorType;
reed@google.com7111d462014-03-25 16:20:24 +0000511 srcPI.fAlphaType = kPremul_SkAlphaType;
512 srcPI.fPixels = buffer;
513 srcPI.fRowBytes = rowBytes;
514
515 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000516 }
517 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000518}
519
bsalomonc49e8682015-06-30 11:37:35 -0700520void GrContext::prepareSurfaceForExternalIO(GrSurface* surface) {
joshualitt1de610a2016-01-06 08:26:09 -0800521 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800522 RETURN_IF_ABANDONED
bsalomon87a94eb2014-11-03 14:28:32 -0800523 SkASSERT(surface);
524 ASSERT_OWNED_RESOURCE(surface);
bsalomon6a2b1942016-09-08 11:28:59 -0700525 fDrawingManager->prepareSurfaceForExternalIO(surface);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000526}
527
bsalomonb8fea972016-02-16 07:34:17 -0800528bool GrContext::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
529 const SkIPoint& dstPoint) {
joshualitt1de610a2016-01-06 08:26:09 -0800530 ASSERT_SINGLE_OWNER
bsalomonb8fea972016-02-16 07:34:17 -0800531 RETURN_FALSE_IF_ABANDONED
joshualittbc907352016-01-13 06:45:40 -0800532 GR_AUDIT_TRAIL_AUTO_FRAME(&fAuditTrail, "GrContext::copySurface");
533
robertphillipsea461502015-05-26 11:38:03 -0700534 if (!src || !dst) {
bsalomonb8fea972016-02-16 07:34:17 -0800535 return false;
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000536 }
bsalomone3d4bf22014-09-23 09:15:03 -0700537 ASSERT_OWNED_RESOURCE(src);
junov2bb52102014-09-29 10:18:59 -0700538 ASSERT_OWNED_RESOURCE(dst);
Brian Salomon34a98952014-09-24 11:41:24 -0400539
robertphillipsea461502015-05-26 11:38:03 -0700540 if (!dst->asRenderTarget()) {
bsalomonb8fea972016-02-16 07:34:17 -0800541 SkIRect clippedSrcRect;
542 SkIPoint clippedDstPoint;
543 if (!GrCopySurfaceBatch::ClipSrcRectAndDstPoint(dst, src, srcRect, dstPoint,
544 &clippedSrcRect, &clippedDstPoint)) {
545 return false;
546 }
547 // If we don't have an RT for the dst then we won't have a GrDrawContext to insert the
548 // the copy surface into. In the future we plan to have a more limited Context type
549 // (GrCopyContext?) that has the subset of GrDrawContext operations that should be
550 // allowed on textures that aren't render targets.
551 // For now we just flush any writes to the src and issue an immediate copy to the dst.
552 src->flushWrites();
553 return fGpu->copySurface(dst, src, clippedSrcRect, clippedDstPoint);
robertphillipsea461502015-05-26 11:38:03 -0700554 }
robertphillips4fd74ae2016-08-03 14:26:53 -0700555 sk_sp<GrDrawContext> drawContext(this->contextPriv().makeWrappedDrawContext(
556 sk_ref_sp(dst->asRenderTarget()),
557 nullptr));
kjlubick0eed9452016-02-11 12:05:24 -0800558 if (!drawContext) {
bsalomonb8fea972016-02-16 07:34:17 -0800559 return false;
bsalomonf80bfed2014-10-07 05:56:02 -0700560 }
kjlubick0eed9452016-02-11 12:05:24 -0800561
bsalomonb8fea972016-02-16 07:34:17 -0800562 if (!drawContext->copySurface(src, srcRect, dstPoint)) {
563 return false;
kjlubick0eed9452016-02-11 12:05:24 -0800564 }
bsalomonb8fea972016-02-16 07:34:17 -0800565 return true;
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000566}
567
bsalomonf80bfed2014-10-07 05:56:02 -0700568void GrContext::flushSurfaceWrites(GrSurface* surface) {
joshualitt1de610a2016-01-06 08:26:09 -0800569 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800570 RETURN_IF_ABANDONED
bsalomonf80bfed2014-10-07 05:56:02 -0700571 if (surface->surfacePriv().hasPendingWrite()) {
572 this->flush();
573 }
574}
575
ajuma95243eb2016-08-24 08:19:02 -0700576void GrContext::flushSurfaceIO(GrSurface* surface) {
577 ASSERT_SINGLE_OWNER
578 RETURN_IF_ABANDONED
579 if (surface->surfacePriv().hasPendingIO()) {
580 this->flush();
581 }
582}
583
bsalomon@google.com27847de2011-02-22 20:59:41 +0000584////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000585int GrContext::getRecommendedSampleCount(GrPixelConfig config,
586 SkScalar dpi) const {
joshualitt1de610a2016-01-06 08:26:09 -0800587 ASSERT_SINGLE_OWNER
588
bsalomon76228632015-05-29 08:02:10 -0700589 if (!this->caps()->isConfigRenderable(config, true)) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000590 return 0;
591 }
592 int chosenSampleCount = 0;
jvanverthe9c0fc62015-04-29 11:18:05 -0700593 if (fGpu->caps()->shaderCaps()->pathRenderingSupport()) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000594 if (dpi >= 250.0f) {
595 chosenSampleCount = 4;
596 } else {
597 chosenSampleCount = 16;
598 }
599 }
egdanieleed519e2016-01-15 11:36:18 -0800600 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ? chosenSampleCount : 0;
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000601}
602
robertphillips4fd74ae2016-08-03 14:26:53 -0700603sk_sp<GrDrawContext> GrContextPriv::makeWrappedDrawContext(sk_sp<GrRenderTarget> rt,
604 sk_sp<SkColorSpace> colorSpace,
605 const SkSurfaceProps* surfaceProps) {
606 ASSERT_SINGLE_OWNER_PRIV
csmartdaltonbde96c62016-08-31 12:54:46 -0700607 return this->drawingManager()->makeDrawContext(std::move(rt),
608 std::move(colorSpace),
609 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -0700610}
robertphillips77a2e522015-10-17 07:43:27 -0700611
robertphillips4fd74ae2016-08-03 14:26:53 -0700612sk_sp<GrDrawContext> GrContextPriv::makeBackendTextureDrawContext(const GrBackendTextureDesc& desc,
613 sk_sp<SkColorSpace> colorSpace,
614 const SkSurfaceProps* props,
615 GrWrapOwnership ownership) {
616 ASSERT_SINGLE_OWNER_PRIV
617 SkASSERT(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
618
619 sk_sp<GrSurface> surface(fContext->textureProvider()->wrapBackendTexture(desc, ownership));
620 if (!surface) {
621 return nullptr;
622 }
623
csmartdaltonbde96c62016-08-31 12:54:46 -0700624 return this->drawingManager()->makeDrawContext(sk_ref_sp(surface->asRenderTarget()),
625 std::move(colorSpace), props);
robertphillips4fd74ae2016-08-03 14:26:53 -0700626}
627
628sk_sp<GrDrawContext> GrContextPriv::makeBackendRenderTargetDrawContext(
629 const GrBackendRenderTargetDesc& desc,
630 sk_sp<SkColorSpace> colorSpace,
631 const SkSurfaceProps* surfaceProps) {
632 ASSERT_SINGLE_OWNER_PRIV
633
634 sk_sp<GrRenderTarget> rt(fContext->textureProvider()->wrapBackendRenderTarget(desc));
635 if (!rt) {
636 return nullptr;
637 }
638
csmartdaltonbde96c62016-08-31 12:54:46 -0700639 return this->drawingManager()->makeDrawContext(std::move(rt),
640 std::move(colorSpace),
641 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -0700642}
643
644sk_sp<GrDrawContext> GrContextPriv::makeBackendTextureAsRenderTargetDrawContext(
egdaniela95d46b2016-08-15 08:06:29 -0700645 const GrBackendTextureDesc& desc,
robertphillips4fd74ae2016-08-03 14:26:53 -0700646 sk_sp<SkColorSpace> colorSpace,
647 const SkSurfaceProps* surfaceProps) {
648 ASSERT_SINGLE_OWNER_PRIV
649 SkASSERT(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
650
651 sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTextureAsRenderTarget(desc));
652 if (!surface) {
653 return nullptr;
654 }
655
csmartdaltonbde96c62016-08-31 12:54:46 -0700656 return this->drawingManager()->makeDrawContext(sk_ref_sp(surface->asRenderTarget()),
657 std::move(colorSpace),
658 surfaceProps);
robertphillips77a2e522015-10-17 07:43:27 -0700659}
660
robertphillips48fde9c2016-09-06 05:20:20 -0700661static inline GrPixelConfig GrPixelConfigFallback(GrPixelConfig config) {
662 static const GrPixelConfig kFallback[] = {
663 kUnknown_GrPixelConfig, // kUnknown_GrPixelConfig
664 kRGBA_8888_GrPixelConfig, // kAlpha_8_GrPixelConfig
665 kUnknown_GrPixelConfig, // kIndex_8_GrPixelConfig
666 kRGBA_8888_GrPixelConfig, // kRGB_565_GrPixelConfig
667 kRGBA_8888_GrPixelConfig, // kRGBA_4444_GrPixelConfig
668 kUnknown_GrPixelConfig, // kRGBA_8888_GrPixelConfig
669 kRGBA_8888_GrPixelConfig, // kBGRA_8888_GrPixelConfig
670 kUnknown_GrPixelConfig, // kSRGBA_8888_GrPixelConfig
671 kSRGBA_8888_GrPixelConfig, // kSBGRA_8888_GrPixelConfig
672 kUnknown_GrPixelConfig, // kETC1_GrPixelConfig
673 kUnknown_GrPixelConfig, // kLATC_GrPixelConfig
674 kUnknown_GrPixelConfig, // kR11_EAC_GrPixelConfig
675 kUnknown_GrPixelConfig, // kASTC_12x12_GrPixelConfig
676 kUnknown_GrPixelConfig, // kRGBA_float_GrPixelConfig
677 kRGBA_half_GrPixelConfig, // kAlpha_half_GrPixelConfig
678 kUnknown_GrPixelConfig, // kRGBA_half_GrPixelConfig
679 };
680 return kFallback[config];
681
682 GR_STATIC_ASSERT(0 == kUnknown_GrPixelConfig);
683 GR_STATIC_ASSERT(1 == kAlpha_8_GrPixelConfig);
684 GR_STATIC_ASSERT(2 == kIndex_8_GrPixelConfig);
685 GR_STATIC_ASSERT(3 == kRGB_565_GrPixelConfig);
686 GR_STATIC_ASSERT(4 == kRGBA_4444_GrPixelConfig);
687 GR_STATIC_ASSERT(5 == kRGBA_8888_GrPixelConfig);
688 GR_STATIC_ASSERT(6 == kBGRA_8888_GrPixelConfig);
689 GR_STATIC_ASSERT(7 == kSRGBA_8888_GrPixelConfig);
690 GR_STATIC_ASSERT(8 == kSBGRA_8888_GrPixelConfig);
691 GR_STATIC_ASSERT(9 == kETC1_GrPixelConfig);
692 GR_STATIC_ASSERT(10 == kLATC_GrPixelConfig);
693 GR_STATIC_ASSERT(11 == kR11_EAC_GrPixelConfig);
694 GR_STATIC_ASSERT(12 == kASTC_12x12_GrPixelConfig);
695 GR_STATIC_ASSERT(13 == kRGBA_float_GrPixelConfig);
696 GR_STATIC_ASSERT(14 == kAlpha_half_GrPixelConfig);
697 GR_STATIC_ASSERT(15 == kRGBA_half_GrPixelConfig);
698 GR_STATIC_ASSERT(SK_ARRAY_COUNT(kFallback) == kGrPixelConfigCnt);
699}
700
701sk_sp<GrDrawContext> GrContext::makeDrawContextWithFallback(SkBackingFit fit,
702 int width, int height,
703 GrPixelConfig config,
704 sk_sp<SkColorSpace> colorSpace,
705 int sampleCnt,
706 GrSurfaceOrigin origin,
707 const SkSurfaceProps* surfaceProps,
708 SkBudgeted budgeted) {
709 if (!this->caps()->isConfigRenderable(config, sampleCnt > 0)) {
710 config = GrPixelConfigFallback(config);
711 }
712
713 return this->makeDrawContext(fit, width, height, config, std::move(colorSpace),
714 sampleCnt, origin, surfaceProps, budgeted);
715}
716
robertphillips6738c702016-07-27 12:13:51 -0700717sk_sp<GrDrawContext> GrContext::makeDrawContext(SkBackingFit fit,
718 int width, int height,
719 GrPixelConfig config,
720 sk_sp<SkColorSpace> colorSpace,
721 int sampleCnt,
722 GrSurfaceOrigin origin,
723 const SkSurfaceProps* surfaceProps,
724 SkBudgeted budgeted) {
robertphillips48fde9c2016-09-06 05:20:20 -0700725 if (!this->caps()->isConfigRenderable(config, sampleCnt > 0)) {
726 return nullptr;
727 }
728
robertphillipsd4c741e2016-04-28 09:55:15 -0700729 GrSurfaceDesc desc;
730 desc.fFlags = kRenderTarget_GrSurfaceFlag;
731 desc.fOrigin = origin;
732 desc.fWidth = width;
733 desc.fHeight = height;
734 desc.fConfig = config;
735 desc.fSampleCnt = sampleCnt;
736
737 sk_sp<GrTexture> tex;
robertphillips76948d42016-05-04 12:47:41 -0700738 if (SkBackingFit::kExact == fit) {
robertphillipsca6eafc2016-05-17 09:57:46 -0700739 tex.reset(this->textureProvider()->createTexture(desc, budgeted));
robertphillipsd4c741e2016-04-28 09:55:15 -0700740 } else {
741 tex.reset(this->textureProvider()->createApproxTexture(desc));
742 }
743 if (!tex) {
744 return nullptr;
745 }
746
robertphillips4fd74ae2016-08-03 14:26:53 -0700747 sk_sp<GrDrawContext> drawContext(this->contextPriv().makeWrappedDrawContext(
748 sk_ref_sp(tex->asRenderTarget()),
robertphillips6738c702016-07-27 12:13:51 -0700749 std::move(colorSpace), surfaceProps));
robertphillipsd4c741e2016-04-28 09:55:15 -0700750 if (!drawContext) {
751 return nullptr;
752 }
753
754 return drawContext;
755}
756
joshualitt1de610a2016-01-06 08:26:09 -0800757bool GrContext::abandoned() const {
758 ASSERT_SINGLE_OWNER
robertphillips7761d612016-05-16 09:14:53 -0700759 return fDrawingManager->wasAbandoned();
robertphillips77a2e522015-10-17 07:43:27 -0700760}
761
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000762namespace {
763void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
764 GrConfigConversionEffect::PMConversion pmToUPM;
765 GrConfigConversionEffect::PMConversion upmToPM;
766 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
767 *pmToUPMValue = pmToUPM;
768 *upmToPMValue = upmToPM;
769}
770}
771
bsalomon6c6f6582015-09-10 08:12:46 -0700772void GrContext::testPMConversionsIfNecessary(uint32_t flags) {
joshualitt1de610a2016-01-06 08:26:09 -0800773 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700774 if (SkToBool(kUnpremul_PixelOpsFlag & flags)) {
775 SkAutoMutexAcquire ama(fTestPMConversionsMutex);
776 if (!fDidTestPMConversions) {
777 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
778 fDidTestPMConversions = true;
779 }
780 }
781}
782
bungeman06ca8ec2016-06-09 08:01:03 -0700783sk_sp<GrFragmentProcessor> GrContext::createPMToUPMEffect(GrTexture* texture,
bsalomon6c9cd552016-01-22 07:17:34 -0800784 const GrSwizzle& swizzle,
bsalomon6c6f6582015-09-10 08:12:46 -0700785 const SkMatrix& matrix) const {
joshualitt1de610a2016-01-06 08:26:09 -0800786 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700787 // We should have already called this->testPMConversionsIfNecessary().
788 SkASSERT(fDidTestPMConversions);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000789 GrConfigConversionEffect::PMConversion pmToUPM =
790 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
791 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bungeman06ca8ec2016-06-09 08:01:03 -0700792 return GrConfigConversionEffect::Make(texture, swizzle, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000793 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700794 return nullptr;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000795 }
796}
797
bungeman06ca8ec2016-06-09 08:01:03 -0700798sk_sp<GrFragmentProcessor> GrContext::createUPMToPMEffect(GrTexture* texture,
bsalomon6c9cd552016-01-22 07:17:34 -0800799 const GrSwizzle& swizzle,
bsalomon6c6f6582015-09-10 08:12:46 -0700800 const SkMatrix& matrix) const {
joshualitt1de610a2016-01-06 08:26:09 -0800801 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700802 // We should have already called this->testPMConversionsIfNecessary().
803 SkASSERT(fDidTestPMConversions);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000804 GrConfigConversionEffect::PMConversion upmToPM =
805 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
806 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
bungeman06ca8ec2016-06-09 08:01:03 -0700807 return GrConfigConversionEffect::Make(texture, swizzle, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000808 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700809 return nullptr;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000810 }
811}
812
bsalomon636e8022015-07-29 06:08:46 -0700813bool GrContext::didFailPMUPMConversionTest() const {
joshualitt1de610a2016-01-06 08:26:09 -0800814 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700815 // We should have already called this->testPMConversionsIfNecessary().
816 SkASSERT(fDidTestPMConversions);
bsalomon636e8022015-07-29 06:08:46 -0700817 // The PM<->UPM tests fail or succeed together so we only need to check one.
bsalomon6c6f6582015-09-10 08:12:46 -0700818 return GrConfigConversionEffect::kNone_PMConversion == fPMToUPMConversion;
bsalomon636e8022015-07-29 06:08:46 -0700819}
820
bsalomon37f9a262015-02-02 13:00:10 -0800821//////////////////////////////////////////////////////////////////////////////
822
823void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800824 ASSERT_SINGLE_OWNER
bsalomon37f9a262015-02-02 13:00:10 -0800825 if (maxTextures) {
bsalomon0ea80f42015-02-11 10:49:59 -0800826 *maxTextures = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -0800827 }
828 if (maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800829 *maxTextureBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -0800830 }
831}
832
833void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
joshualitt1de610a2016-01-06 08:26:09 -0800834 ASSERT_SINGLE_OWNER
bsalomon0ea80f42015-02-11 10:49:59 -0800835 fResourceCache->setLimits(maxTextures, maxTextureBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800836}
837
ericrk0a5fa482015-09-15 14:16:10 -0700838//////////////////////////////////////////////////////////////////////////////
839
840void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -0800841 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -0700842 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
843}