blob: 5f4e663580a7e525cdac0f07f110a9b7f70e909e [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;
bsalomonf21dab92014-11-13 13:33:28 -080069 fFlushToReduceCacheSize = false;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000070}
71
bsalomon682c2692015-05-22 14:01:46 -070072bool GrContext::init(GrBackend backend, GrBackendContext backendContext,
73 const GrContextOptions& options) {
joshualitt1de610a2016-01-06 08:26:09 -080074 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -070075 SkASSERT(!fGpu);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000076
bsalomon682c2692015-05-22 14:01:46 -070077 fGpu = GrGpu::Create(backend, backendContext, options, this);
robertphillipsea461502015-05-26 11:38:03 -070078 if (!fGpu) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000079 return false;
80 }
bsalomon69cfe952015-11-30 13:27:47 -080081 this->initCommon(options);
bsalomon33435572014-11-05 14:47:41 -080082 return true;
83}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000084
bsalomon69cfe952015-11-30 13:27:47 -080085void GrContext::initCommon(const GrContextOptions& options) {
joshualitt1de610a2016-01-06 08:26:09 -080086 ASSERT_SINGLE_OWNER
87
bsalomon76228632015-05-29 08:02:10 -070088 fCaps = SkRef(fGpu->caps());
halcanary385fe4d2015-08-26 13:07:48 -070089 fResourceCache = new GrResourceCache(fCaps);
bsalomon0ea80f42015-02-11 10:49:59 -080090 fResourceCache->setOverBudgetCallback(OverBudgetCB, this);
joshualitt6d0872d2016-01-11 08:27:48 -080091 fResourceProvider = new GrResourceProvider(fGpu, fResourceCache, &fSingleOwner);
commit-bot@chromium.org1836d332013-07-16 22:55:03 +000092
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000093 fDidTestPMConversions = false;
94
bsalomon69cfe952015-11-30 13:27:47 -080095 GrDrawTarget::Options dtOptions;
96 dtOptions.fClipBatchToBounds = options.fClipBatchToBounds;
bsalomon6dea83f2015-12-03 12:58:06 -080097 dtOptions.fDrawBatchBounds = options.fDrawBatchBounds;
bsalomon489147c2015-12-14 12:13:09 -080098 dtOptions.fMaxBatchLookback = options.fMaxBatchLookback;
bsalomonaecc0182016-03-07 11:50:44 -080099 dtOptions.fMaxBatchLookahead = options.fMaxBatchLookahead;
joshualittde8dc7e2016-01-08 10:09:13 -0800100 fDrawingManager.reset(new GrDrawingManager(this, dtOptions, &fSingleOwner));
joshualitt7c3a2f82015-03-31 13:32:05 -0700101
102 // GrBatchFontCache will eventually replace GrFontCache
halcanary385fe4d2015-08-26 13:07:48 -0700103 fBatchFontCache = new GrBatchFontCache(this);
joshualittb7133be2015-04-08 09:08:31 -0700104
halcanary385fe4d2015-08-26 13:07:48 -0700105 fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB, this));
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000106}
107
bsalomon@google.com27847de2011-02-22 20:59:41 +0000108GrContext::~GrContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800109 ASSERT_SINGLE_OWNER
110
robertphillipsea461502015-05-26 11:38:03 -0700111 if (!fGpu) {
bsalomon76228632015-05-29 08:02:10 -0700112 SkASSERT(!fCaps);
bsalomon@google.com733c0622013-04-24 17:59:32 +0000113 return;
114 }
115
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000116 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000117
robertphillips77a2e522015-10-17 07:43:27 -0700118 fDrawingManager->cleanup();
robertphillips2334fb62015-06-17 05:43:33 -0700119
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000120 for (int i = 0; i < fCleanUpData.count(); ++i) {
121 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
122 }
123
halcanary385fe4d2015-08-26 13:07:48 -0700124 delete fResourceProvider;
125 delete fResourceCache;
126 delete fBatchFontCache;
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000127
bsalomon@google.com205d4602011-04-25 12:43:45 +0000128 fGpu->unref();
bsalomon76228632015-05-29 08:02:10 -0700129 fCaps->unref();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000130}
131
bsalomon41b952c2016-03-11 06:46:33 -0800132GrContextThreadSafeProxy* GrContext::threadSafeProxy() {
133 if (!fThreadSafeProxy) {
134 fThreadSafeProxy.reset(new GrContextThreadSafeProxy(fCaps, this->uniqueID()));
135 }
136 return SkRef(fThreadSafeProxy.get());
137}
138
bsalomon2354f842014-07-28 13:48:36 -0700139void GrContext::abandonContext() {
joshualitt1de610a2016-01-06 08:26:09 -0800140 ASSERT_SINGLE_OWNER
141
bsalomond309e7a2015-04-30 14:18:54 -0700142 fResourceProvider->abandon();
robertphillips0dfa62c2015-11-16 06:23:31 -0800143
144 // Need to abandon the drawing manager first so all the render targets
145 // will be released/forgotten before they too are abandoned.
146 fDrawingManager->abandon();
147
bsalomon@google.com205d4602011-04-25 12:43:45 +0000148 // abandon first to so destructors
149 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800150 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700151
bsalomon6e2aad42016-04-01 11:54:31 -0700152 fGpu->disconnect(GrGpu::DisconnectType::kAbandon);
153
154 fBatchFontCache->freeAll();
bsalomon6e2aad42016-04-01 11:54:31 -0700155 fTextBlobCache->freeAll();
156}
157
158void GrContext::releaseResourcesAndAbandonContext() {
159 ASSERT_SINGLE_OWNER
160
161 fResourceProvider->abandon();
162
163 // Need to abandon the drawing manager first so all the render targets
164 // will be released/forgotten before they too are abandoned.
165 fDrawingManager->abandon();
166
167 // Release all resources in the backend 3D API.
168 fResourceCache->releaseAll();
169
170 fGpu->disconnect(GrGpu::DisconnectType::kCleanup);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000171
joshualitt7c3a2f82015-03-31 13:32:05 -0700172 fBatchFontCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700173 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000174}
175
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000176void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800177 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000178 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000179}
180
181void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800182 ASSERT_SINGLE_OWNER
183
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000184 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000185
joshualitt7c3a2f82015-03-31 13:32:05 -0700186 fBatchFontCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700187
188 fDrawingManager->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700189
190 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000191}
192
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000193void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800194 ASSERT_SINGLE_OWNER
195
bsalomon71cb0c22014-11-14 12:10:14 -0800196 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800197 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800198 }
199 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800200 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800201 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000202}
203
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000204////////////////////////////////////////////////////////////////////////////////
205
bsalomon71cb0c22014-11-14 12:10:14 -0800206void GrContext::OverBudgetCB(void* data) {
bsalomon66a450f2014-11-13 13:19:10 -0800207 SkASSERT(data);
bsalomonf21dab92014-11-13 13:33:28 -0800208
bsalomon66a450f2014-11-13 13:19:10 -0800209 GrContext* context = reinterpret_cast<GrContext*>(data);
bsalomonf21dab92014-11-13 13:33:28 -0800210
joshualittb542bae2015-07-28 09:58:39 -0700211 // Flush the GrBufferedDrawTarget to possibly free up some textures
bsalomonf21dab92014-11-13 13:33:28 -0800212 context->fFlushToReduceCacheSize = true;
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000213}
214
joshualitt0db6dfa2015-04-10 07:01:30 -0700215void GrContext::TextBlobCacheOverBudgetCB(void* data) {
216 SkASSERT(data);
217
218 // Unlike the GrResourceCache, TextBlobs are drawn at the SkGpuDevice level, therefore they
219 // cannot use fFlushTorReduceCacheSize because it uses AutoCheckFlush. The solution is to move
220 // drawText calls to below the GrContext level, but this is not trivial because they call
221 // drawPath on SkGpuDevice
222 GrContext* context = reinterpret_cast<GrContext*>(data);
223 context->flush();
224}
225
bsalomon@google.com27847de2011-02-22 20:59:41 +0000226////////////////////////////////////////////////////////////////////////////////
227
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000228void GrContext::flush(int flagsBitfield) {
joshualitt1de610a2016-01-06 08:26:09 -0800229 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700230 RETURN_IF_ABANDONED
robertphillips@google.come7db8d62013-07-04 11:48:52 +0000231
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000232 if (kDiscard_FlushBit & flagsBitfield) {
robertphillips77a2e522015-10-17 07:43:27 -0700233 fDrawingManager->reset();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000234 } else {
robertphillips77a2e522015-10-17 07:43:27 -0700235 fDrawingManager->flush();
junov@google.com53a55842011-06-08 22:55:10 +0000236 }
bsalomon3f324322015-04-08 11:01:54 -0700237 fResourceCache->notifyFlushOccurred();
bsalomonf21dab92014-11-13 13:33:28 -0800238 fFlushToReduceCacheSize = false;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000239}
240
bsalomon81beccc2014-10-13 12:32:55 -0700241bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
242 const void* inPixels, size_t outRowBytes, void* outPixels) {
243 SkSrcPixelInfo srcPI;
brianosman396fcdb2016-07-22 06:26:11 -0700244 if (!GrPixelConfigToColorType(srcConfig, &srcPI.fColorType)) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000245 return false;
246 }
bsalomon81beccc2014-10-13 12:32:55 -0700247 srcPI.fAlphaType = kUnpremul_SkAlphaType;
248 srcPI.fPixels = inPixels;
249 srcPI.fRowBytes = inRowBytes;
250
251 SkDstPixelInfo dstPI;
252 dstPI.fColorType = srcPI.fColorType;
253 dstPI.fAlphaType = kPremul_SkAlphaType;
254 dstPI.fPixels = outPixels;
255 dstPI.fRowBytes = outRowBytes;
256
257 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000258}
259
bsalomon81beccc2014-10-13 12:32:55 -0700260bool GrContext::writeSurfacePixels(GrSurface* surface,
261 int left, int top, int width, int height,
262 GrPixelConfig srcConfig, const void* buffer, size_t rowBytes,
263 uint32_t pixelOpsFlags) {
joshualitt1de610a2016-01-06 08:26:09 -0800264 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800265 RETURN_FALSE_IF_ABANDONED
bsalomon6c6f6582015-09-10 08:12:46 -0700266 ASSERT_OWNED_RESOURCE(surface);
267 SkASSERT(surface);
joshualittbc907352016-01-13 06:45:40 -0800268 GR_AUDIT_TRAIL_AUTO_FRAME(&fAuditTrail, "GrContext::writeSurfacePixels");
bsalomon6c6f6582015-09-10 08:12:46 -0700269
270 this->testPMConversionsIfNecessary(pixelOpsFlags);
bsalomon81beccc2014-10-13 12:32:55 -0700271
bsalomone8d21e82015-07-16 08:23:13 -0700272 // 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 -0700273 // necessary and because GrGpu::getWritePixelsInfo requires it.
bsalomone8d21e82015-07-16 08:23:13 -0700274 if (!GrSurfacePriv::AdjustWritePixelParams(surface->width(), surface->height(),
275 GrBytesPerPixel(srcConfig), &left, &top, &width,
276 &height, &buffer, &rowBytes)) {
277 return false;
278 }
279
bsalomonf0674512015-07-28 13:26:15 -0700280 bool applyPremulToSrc = false;
bsalomon81beccc2014-10-13 12:32:55 -0700281 if (kUnpremul_PixelOpsFlag & pixelOpsFlags) {
282 if (!GrPixelConfigIs8888(srcConfig)) {
283 return false;
284 }
bsalomonf0674512015-07-28 13:26:15 -0700285 applyPremulToSrc = true;
286 }
bsalomon636e8022015-07-29 06:08:46 -0700287
288 GrGpu::DrawPreference drawPreference = GrGpu::kNoDraw_DrawPreference;
289 // Don't prefer to draw for the conversion (and thereby access a texture from the cache) when
290 // we've already determined that there isn't a roundtrip preserving conversion processor pair.
291 if (applyPremulToSrc && !this->didFailPMUPMConversionTest()) {
292 drawPreference = GrGpu::kCallerPrefersDraw_DrawPreference;
293 }
294
bsalomonf0674512015-07-28 13:26:15 -0700295 GrGpu::WritePixelTempDrawInfo tempDrawInfo;
cblumeed828002016-02-16 13:00:01 -0800296 if (!fGpu->getWritePixelsInfo(surface, width, height, srcConfig, &drawPreference,
bsalomonf0674512015-07-28 13:26:15 -0700297 &tempDrawInfo)) {
298 return false;
299 }
300
301 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && surface->surfacePriv().hasPendingIO()) {
302 this->flush();
303 }
304
305 SkAutoTUnref<GrTexture> tempTexture;
306 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomoneae62002015-07-31 13:59:30 -0700307 tempTexture.reset(
308 this->textureProvider()->createApproxTexture(tempDrawInfo.fTempSurfaceDesc));
bsalomonf0674512015-07-28 13:26:15 -0700309 if (!tempTexture && GrGpu::kRequireDraw_DrawPreference == drawPreference) {
310 return false;
311 }
312 }
313
314 // temp buffer for doing sw premul conversion, if needed.
315 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
316 if (tempTexture) {
bungeman06ca8ec2016-06-09 08:01:03 -0700317 sk_sp<GrFragmentProcessor> fp;
bsalomonf0674512015-07-28 13:26:15 -0700318 SkMatrix textureMatrix;
319 textureMatrix.setIDiv(tempTexture->width(), tempTexture->height());
bsalomonf0674512015-07-28 13:26:15 -0700320 if (applyPremulToSrc) {
bungeman06ca8ec2016-06-09 08:01:03 -0700321 fp = this->createUPMToPMEffect(tempTexture, tempDrawInfo.fSwizzle, textureMatrix);
bsalomonf0674512015-07-28 13:26:15 -0700322 // If premultiplying was the only reason for the draw, fall back to a straight write.
323 if (!fp) {
324 if (GrGpu::kCallerPrefersDraw_DrawPreference == drawPreference) {
halcanary96fcdcc2015-08-27 07:41:13 -0700325 tempTexture.reset(nullptr);
bsalomonf0674512015-07-28 13:26:15 -0700326 }
327 } else {
328 applyPremulToSrc = false;
329 }
330 }
331 if (tempTexture) {
332 if (!fp) {
bungeman06ca8ec2016-06-09 08:01:03 -0700333 fp = GrConfigConversionEffect::Make(tempTexture, tempDrawInfo.fSwizzle,
334 GrConfigConversionEffect::kNone_PMConversion,
335 textureMatrix);
bsalomonf0674512015-07-28 13:26:15 -0700336 if (!fp) {
337 return false;
338 }
339 }
340 GrRenderTarget* renderTarget = surface->asRenderTarget();
341 SkASSERT(renderTarget);
342 if (tempTexture->surfacePriv().hasPendingIO()) {
343 this->flush();
344 }
345 if (applyPremulToSrc) {
346 size_t tmpRowBytes = 4 * width;
347 tmpPixels.reset(width * height);
348 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
349 tmpPixels.get())) {
350 return false;
351 }
352 rowBytes = tmpRowBytes;
353 buffer = tmpPixels.get();
354 applyPremulToSrc = false;
355 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700356 if (!fGpu->writePixels(tempTexture, 0, 0, width, height,
bsalomon6c9cd552016-01-22 07:17:34 -0800357 tempDrawInfo.fWriteConfig, buffer,
bsalomon6cb3cbe2015-07-30 07:34:27 -0700358 rowBytes)) {
bsalomonf0674512015-07-28 13:26:15 -0700359 return false;
360 }
361 SkMatrix matrix;
362 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
brianosmandfe4f2e2016-07-21 13:28:36 -0700363 // TODO: Need to decide the semantics of this function for color spaces. Do we support
364 // conversion from a passed-in color space? For now, specifying nullptr means that this
365 // path will do no conversion, so it will match the behavior of the non-draw path.
robertphillips4fd74ae2016-08-03 14:26:53 -0700366 sk_sp<GrDrawContext> drawContext(this->contextPriv().makeWrappedDrawContext(
367 sk_ref_sp(renderTarget),
368 nullptr));
bsalomonf0674512015-07-28 13:26:15 -0700369 if (!drawContext) {
370 return false;
371 }
egdanielc4b72722015-11-23 13:20:41 -0800372 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700373 paint.addColorFragmentProcessor(std::move(fp));
egdanielc4b72722015-11-23 13:20:41 -0800374 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
brianosmana167e742016-05-24 06:18:48 -0700375 paint.setAllowSRGBInputs(true);
bsalomonf0674512015-07-28 13:26:15 -0700376 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
cdalton846c0512016-05-13 10:25:00 -0700377 drawContext->drawRect(GrNoClip(), paint, matrix, rect, nullptr);
bsalomonf0674512015-07-28 13:26:15 -0700378
379 if (kFlushWrites_PixelOp & pixelOpsFlags) {
380 this->flushSurfaceWrites(surface);
381 }
382 }
383 }
384 if (!tempTexture) {
bsalomonf0674512015-07-28 13:26:15 -0700385 if (applyPremulToSrc) {
bsalomon81beccc2014-10-13 12:32:55 -0700386 size_t tmpRowBytes = 4 * width;
387 tmpPixels.reset(width * height);
388 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
389 tmpPixels.get())) {
390 return false;
391 }
392 rowBytes = tmpRowBytes;
393 buffer = tmpPixels.get();
bsalomonf0674512015-07-28 13:26:15 -0700394 applyPremulToSrc = false;
bsalomon81beccc2014-10-13 12:32:55 -0700395 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700396 return fGpu->writePixels(surface, left, top, width, height, srcConfig, buffer, rowBytes);
bsalomon81beccc2014-10-13 12:32:55 -0700397 }
bsalomon81beccc2014-10-13 12:32:55 -0700398 return true;
399}
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000400
bsalomone8d21e82015-07-16 08:23:13 -0700401bool GrContext::readSurfacePixels(GrSurface* src,
402 int left, int top, int width, int height,
403 GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
404 uint32_t flags) {
joshualitt1de610a2016-01-06 08:26:09 -0800405 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800406 RETURN_FALSE_IF_ABANDONED
bsalomone8d21e82015-07-16 08:23:13 -0700407 ASSERT_OWNED_RESOURCE(src);
408 SkASSERT(src);
joshualittbc907352016-01-13 06:45:40 -0800409 GR_AUDIT_TRAIL_AUTO_FRAME(&fAuditTrail, "GrContext::readSurfacePixels");
bsalomon32ab2602015-09-09 18:57:49 -0700410
bsalomon6c6f6582015-09-10 08:12:46 -0700411 this->testPMConversionsIfNecessary(flags);
412 SkAutoMutexAcquire ama(fReadPixelsMutex);
413
bsalomone8d21e82015-07-16 08:23:13 -0700414 // Adjust the params so that if we wind up using an intermediate surface we've already done
415 // all the trimming and the temporary can be the min size required.
416 if (!GrSurfacePriv::AdjustReadPixelParams(src->width(), src->height(),
417 GrBytesPerPixel(dstConfig), &left,
418 &top, &width, &height, &buffer, &rowBytes)) {
419 return false;
420 }
421
422 if (!(kDontFlush_PixelOpsFlag & flags) && src->surfacePriv().hasPendingWrite()) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000423 this->flush();
424 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000425
bsalomone8d21e82015-07-16 08:23:13 -0700426 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.com9c680582013-02-06 18:17:50 +0000427 if (unpremul && !GrPixelConfigIs8888(dstConfig)) {
bsalomon39826022015-07-23 08:07:21 -0700428 // The unpremul flag is only allowed for 8888 configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000429 return false;
430 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000431
bsalomon636e8022015-07-29 06:08:46 -0700432 GrGpu::DrawPreference drawPreference = GrGpu::kNoDraw_DrawPreference;
433 // Don't prefer to draw for the conversion (and thereby access a texture from the cache) when
434 // we've already determined that there isn't a roundtrip preserving conversion processor pair.
435 if (unpremul && !this->didFailPMUPMConversionTest()) {
436 drawPreference = GrGpu::kCallerPrefersDraw_DrawPreference;
437 }
438
bsalomon39826022015-07-23 08:07:21 -0700439 GrGpu::ReadPixelTempDrawInfo tempDrawInfo;
440 if (!fGpu->getReadPixelsInfo(src, width, height, rowBytes, dstConfig, &drawPreference,
441 &tempDrawInfo)) {
442 return false;
443 }
bsalomon191bcc02014-11-14 11:31:13 -0800444
bsalomon6cb3cbe2015-07-30 07:34:27 -0700445 SkAutoTUnref<GrSurface> surfaceToRead(SkRef(src));
bsalomon39826022015-07-23 08:07:21 -0700446 bool didTempDraw = false;
447 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomonb117ff12016-07-19 07:24:40 -0700448 if (SkBackingFit::kExact == tempDrawInfo.fTempSurfaceFit) {
bsalomon39826022015-07-23 08:07:21 -0700449 // We only respect this when the entire src is being read. Otherwise we can trigger too
450 // many odd ball texture sizes and trash the cache.
bsalomoneae62002015-07-31 13:59:30 -0700451 if (width != src->width() || height != src->height()) {
bsalomonb117ff12016-07-19 07:24:40 -0700452 tempDrawInfo.fTempSurfaceFit= SkBackingFit::kApprox;
bsalomon39826022015-07-23 08:07:21 -0700453 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000454 }
brianosmandfe4f2e2016-07-21 13:28:36 -0700455 // TODO: Need to decide the semantics of this function for color spaces. Do we support
456 // conversion to a passed-in color space? For now, specifying nullptr means that this
457 // path will do no conversion, so it will match the behavior of the non-draw path.
robertphillips6738c702016-07-27 12:13:51 -0700458 sk_sp<GrDrawContext> tempDC = this->makeDrawContext(tempDrawInfo.fTempSurfaceFit,
bsalomonb117ff12016-07-19 07:24:40 -0700459 tempDrawInfo.fTempSurfaceDesc.fWidth,
460 tempDrawInfo.fTempSurfaceDesc.fHeight,
461 tempDrawInfo.fTempSurfaceDesc.fConfig,
brianosmandfe4f2e2016-07-21 13:28:36 -0700462 nullptr,
bsalomonb117ff12016-07-19 07:24:40 -0700463 tempDrawInfo.fTempSurfaceDesc.fSampleCnt,
464 tempDrawInfo.fTempSurfaceDesc.fOrigin);
465 if (tempDC) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000466 SkMatrix textureMatrix;
bsalomon39826022015-07-23 08:07:21 -0700467 textureMatrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000468 textureMatrix.postIDiv(src->width(), src->height());
bungeman06ca8ec2016-06-09 08:01:03 -0700469 sk_sp<GrFragmentProcessor> fp;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000470 if (unpremul) {
bungeman06ca8ec2016-06-09 08:01:03 -0700471 fp = this->createPMToUPMEffect(src->asTexture(), tempDrawInfo.fSwizzle,
472 textureMatrix);
joshualittb0a8a372014-09-23 09:50:21 -0700473 if (fp) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000474 unpremul = false; // we no longer need to do this on CPU after the read back.
bsalomon39826022015-07-23 08:07:21 -0700475 } else if (GrGpu::kCallerPrefersDraw_DrawPreference == drawPreference) {
476 // We only wanted to do the draw in order to perform the unpremul so don't
477 // bother.
bsalomonb117ff12016-07-19 07:24:40 -0700478 tempDC.reset(nullptr);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000479 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000480 }
bsalomonb117ff12016-07-19 07:24:40 -0700481 if (!fp && tempDC) {
bungeman06ca8ec2016-06-09 08:01:03 -0700482 fp = GrConfigConversionEffect::Make(src->asTexture(), tempDrawInfo.fSwizzle,
483 GrConfigConversionEffect::kNone_PMConversion,
484 textureMatrix);
bsalomon39826022015-07-23 08:07:21 -0700485 }
486 if (fp) {
egdanielc4b72722015-11-23 13:20:41 -0800487 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700488 paint.addColorFragmentProcessor(std::move(fp));
egdanielc4b72722015-11-23 13:20:41 -0800489 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
brianosmana167e742016-05-24 06:18:48 -0700490 paint.setAllowSRGBInputs(true);
bsalomon39826022015-07-23 08:07:21 -0700491 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
bsalomonb117ff12016-07-19 07:24:40 -0700492 tempDC->drawRect(GrNoClip(), paint, SkMatrix::I(), rect, nullptr);
493 surfaceToRead.reset(tempDC->asTexture().release());
bsalomon39826022015-07-23 08:07:21 -0700494 left = 0;
495 top = 0;
496 didTempDraw = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000497 }
bsalomon@google.com0342a852012-08-20 19:22:38 +0000498 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000499 }
joshualitt5c55fef2014-10-31 14:04:35 -0700500
bsalomon39826022015-07-23 08:07:21 -0700501 if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000502 return false;
503 }
bsalomon39826022015-07-23 08:07:21 -0700504 GrPixelConfig configToRead = dstConfig;
505 if (didTempDraw) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700506 this->flushSurfaceWrites(surfaceToRead);
bsalomon6c9cd552016-01-22 07:17:34 -0800507 configToRead = tempDrawInfo.fReadConfig;
bsalomon39826022015-07-23 08:07:21 -0700508 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700509 if (!fGpu->readPixels(surfaceToRead, left, top, width, height, configToRead, buffer,
510 rowBytes)) {
bsalomon39826022015-07-23 08:07:21 -0700511 return false;
512 }
513
514 // Perform umpremul conversion if we weren't able to perform it as a draw.
515 if (unpremul) {
reed@google.com7111d462014-03-25 16:20:24 +0000516 SkDstPixelInfo dstPI;
brianosman396fcdb2016-07-22 06:26:11 -0700517 if (!GrPixelConfigToColorType(dstConfig, &dstPI.fColorType)) {
reed@google.com7111d462014-03-25 16:20:24 +0000518 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000519 }
reed@google.com7111d462014-03-25 16:20:24 +0000520 dstPI.fAlphaType = kUnpremul_SkAlphaType;
521 dstPI.fPixels = buffer;
522 dstPI.fRowBytes = rowBytes;
523
524 SkSrcPixelInfo srcPI;
bsalomon39826022015-07-23 08:07:21 -0700525 srcPI.fColorType = dstPI.fColorType;
reed@google.com7111d462014-03-25 16:20:24 +0000526 srcPI.fAlphaType = kPremul_SkAlphaType;
527 srcPI.fPixels = buffer;
528 srcPI.fRowBytes = rowBytes;
529
530 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000531 }
532 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000533}
534
bsalomonc49e8682015-06-30 11:37:35 -0700535void GrContext::prepareSurfaceForExternalIO(GrSurface* surface) {
joshualitt1de610a2016-01-06 08:26:09 -0800536 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800537 RETURN_IF_ABANDONED
bsalomon87a94eb2014-11-03 14:28:32 -0800538 SkASSERT(surface);
539 ASSERT_OWNED_RESOURCE(surface);
540 if (surface->surfacePriv().hasPendingIO()) {
541 this->flush();
542 }
543 GrRenderTarget* rt = surface->asRenderTarget();
544 if (fGpu && rt) {
545 fGpu->resolveRenderTarget(rt);
bsalomon41ebbdd2014-08-04 08:31:39 -0700546 }
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000547}
548
bsalomonb8fea972016-02-16 07:34:17 -0800549bool GrContext::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
550 const SkIPoint& dstPoint) {
joshualitt1de610a2016-01-06 08:26:09 -0800551 ASSERT_SINGLE_OWNER
bsalomonb8fea972016-02-16 07:34:17 -0800552 RETURN_FALSE_IF_ABANDONED
joshualittbc907352016-01-13 06:45:40 -0800553 GR_AUDIT_TRAIL_AUTO_FRAME(&fAuditTrail, "GrContext::copySurface");
554
robertphillipsea461502015-05-26 11:38:03 -0700555 if (!src || !dst) {
bsalomonb8fea972016-02-16 07:34:17 -0800556 return false;
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000557 }
bsalomone3d4bf22014-09-23 09:15:03 -0700558 ASSERT_OWNED_RESOURCE(src);
junov2bb52102014-09-29 10:18:59 -0700559 ASSERT_OWNED_RESOURCE(dst);
Brian Salomon34a98952014-09-24 11:41:24 -0400560
robertphillipsea461502015-05-26 11:38:03 -0700561 if (!dst->asRenderTarget()) {
bsalomonb8fea972016-02-16 07:34:17 -0800562 SkIRect clippedSrcRect;
563 SkIPoint clippedDstPoint;
564 if (!GrCopySurfaceBatch::ClipSrcRectAndDstPoint(dst, src, srcRect, dstPoint,
565 &clippedSrcRect, &clippedDstPoint)) {
566 return false;
567 }
568 // If we don't have an RT for the dst then we won't have a GrDrawContext to insert the
569 // the copy surface into. In the future we plan to have a more limited Context type
570 // (GrCopyContext?) that has the subset of GrDrawContext operations that should be
571 // allowed on textures that aren't render targets.
572 // For now we just flush any writes to the src and issue an immediate copy to the dst.
573 src->flushWrites();
574 return fGpu->copySurface(dst, src, clippedSrcRect, clippedDstPoint);
robertphillipsea461502015-05-26 11:38:03 -0700575 }
robertphillips4fd74ae2016-08-03 14:26:53 -0700576 sk_sp<GrDrawContext> drawContext(this->contextPriv().makeWrappedDrawContext(
577 sk_ref_sp(dst->asRenderTarget()),
578 nullptr));
kjlubick0eed9452016-02-11 12:05:24 -0800579 if (!drawContext) {
bsalomonb8fea972016-02-16 07:34:17 -0800580 return false;
bsalomonf80bfed2014-10-07 05:56:02 -0700581 }
kjlubick0eed9452016-02-11 12:05:24 -0800582
bsalomonb8fea972016-02-16 07:34:17 -0800583 if (!drawContext->copySurface(src, srcRect, dstPoint)) {
584 return false;
kjlubick0eed9452016-02-11 12:05:24 -0800585 }
bsalomonb8fea972016-02-16 07:34:17 -0800586 return true;
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000587}
588
bsalomonf80bfed2014-10-07 05:56:02 -0700589void GrContext::flushSurfaceWrites(GrSurface* surface) {
joshualitt1de610a2016-01-06 08:26:09 -0800590 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800591 RETURN_IF_ABANDONED
bsalomonf80bfed2014-10-07 05:56:02 -0700592 if (surface->surfacePriv().hasPendingWrite()) {
593 this->flush();
594 }
595}
596
ajuma95243eb2016-08-24 08:19:02 -0700597void GrContext::flushSurfaceIO(GrSurface* surface) {
598 ASSERT_SINGLE_OWNER
599 RETURN_IF_ABANDONED
600 if (surface->surfacePriv().hasPendingIO()) {
601 this->flush();
602 }
603}
604
bsalomon@google.com27847de2011-02-22 20:59:41 +0000605////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000606int GrContext::getRecommendedSampleCount(GrPixelConfig config,
607 SkScalar dpi) const {
joshualitt1de610a2016-01-06 08:26:09 -0800608 ASSERT_SINGLE_OWNER
609
bsalomon76228632015-05-29 08:02:10 -0700610 if (!this->caps()->isConfigRenderable(config, true)) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000611 return 0;
612 }
613 int chosenSampleCount = 0;
jvanverthe9c0fc62015-04-29 11:18:05 -0700614 if (fGpu->caps()->shaderCaps()->pathRenderingSupport()) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000615 if (dpi >= 250.0f) {
616 chosenSampleCount = 4;
617 } else {
618 chosenSampleCount = 16;
619 }
620 }
egdanieleed519e2016-01-15 11:36:18 -0800621 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ? chosenSampleCount : 0;
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000622}
623
robertphillips4fd74ae2016-08-03 14:26:53 -0700624sk_sp<GrDrawContext> GrContextPriv::makeWrappedDrawContext(sk_sp<GrRenderTarget> rt,
625 sk_sp<SkColorSpace> colorSpace,
626 const SkSurfaceProps* surfaceProps) {
627 ASSERT_SINGLE_OWNER_PRIV
robertphillips29f9fe42016-08-05 09:28:20 -0700628 return fContext->drawingManager()->makeDrawContext(std::move(rt),
629 std::move(colorSpace),
630 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -0700631}
robertphillips77a2e522015-10-17 07:43:27 -0700632
robertphillips4fd74ae2016-08-03 14:26:53 -0700633sk_sp<GrDrawContext> GrContextPriv::makeBackendTextureDrawContext(const GrBackendTextureDesc& desc,
634 sk_sp<SkColorSpace> colorSpace,
635 const SkSurfaceProps* props,
636 GrWrapOwnership ownership) {
637 ASSERT_SINGLE_OWNER_PRIV
638 SkASSERT(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
639
640 sk_sp<GrSurface> surface(fContext->textureProvider()->wrapBackendTexture(desc, ownership));
641 if (!surface) {
642 return nullptr;
643 }
644
robertphillips29f9fe42016-08-05 09:28:20 -0700645 return fContext->drawingManager()->makeDrawContext(sk_ref_sp(surface->asRenderTarget()),
646 std::move(colorSpace), props);
robertphillips4fd74ae2016-08-03 14:26:53 -0700647}
648
649sk_sp<GrDrawContext> GrContextPriv::makeBackendRenderTargetDrawContext(
650 const GrBackendRenderTargetDesc& desc,
651 sk_sp<SkColorSpace> colorSpace,
652 const SkSurfaceProps* surfaceProps) {
653 ASSERT_SINGLE_OWNER_PRIV
654
655 sk_sp<GrRenderTarget> rt(fContext->textureProvider()->wrapBackendRenderTarget(desc));
656 if (!rt) {
657 return nullptr;
658 }
659
robertphillips29f9fe42016-08-05 09:28:20 -0700660 return fContext->drawingManager()->makeDrawContext(std::move(rt),
661 std::move(colorSpace),
662 surfaceProps);
robertphillips4fd74ae2016-08-03 14:26:53 -0700663}
664
665sk_sp<GrDrawContext> GrContextPriv::makeBackendTextureAsRenderTargetDrawContext(
egdaniela95d46b2016-08-15 08:06:29 -0700666 const GrBackendTextureDesc& desc,
robertphillips4fd74ae2016-08-03 14:26:53 -0700667 sk_sp<SkColorSpace> colorSpace,
668 const SkSurfaceProps* surfaceProps) {
669 ASSERT_SINGLE_OWNER_PRIV
670 SkASSERT(desc.fFlags & kRenderTarget_GrBackendTextureFlag);
671
672 sk_sp<GrSurface> surface(fContext->resourceProvider()->wrapBackendTextureAsRenderTarget(desc));
673 if (!surface) {
674 return nullptr;
675 }
676
robertphillips29f9fe42016-08-05 09:28:20 -0700677 return fContext->drawingManager()->makeDrawContext(sk_ref_sp(surface->asRenderTarget()),
678 std::move(colorSpace),
679 surfaceProps);
robertphillips77a2e522015-10-17 07:43:27 -0700680}
681
robertphillips6738c702016-07-27 12:13:51 -0700682sk_sp<GrDrawContext> GrContext::makeDrawContext(SkBackingFit fit,
683 int width, int height,
684 GrPixelConfig config,
685 sk_sp<SkColorSpace> colorSpace,
686 int sampleCnt,
687 GrSurfaceOrigin origin,
688 const SkSurfaceProps* surfaceProps,
689 SkBudgeted budgeted) {
robertphillipsd4c741e2016-04-28 09:55:15 -0700690 GrSurfaceDesc desc;
691 desc.fFlags = kRenderTarget_GrSurfaceFlag;
692 desc.fOrigin = origin;
693 desc.fWidth = width;
694 desc.fHeight = height;
695 desc.fConfig = config;
696 desc.fSampleCnt = sampleCnt;
697
698 sk_sp<GrTexture> tex;
robertphillips76948d42016-05-04 12:47:41 -0700699 if (SkBackingFit::kExact == fit) {
robertphillipsca6eafc2016-05-17 09:57:46 -0700700 tex.reset(this->textureProvider()->createTexture(desc, budgeted));
robertphillipsd4c741e2016-04-28 09:55:15 -0700701 } else {
702 tex.reset(this->textureProvider()->createApproxTexture(desc));
703 }
704 if (!tex) {
705 return nullptr;
706 }
707
robertphillips4fd74ae2016-08-03 14:26:53 -0700708 sk_sp<GrDrawContext> drawContext(this->contextPriv().makeWrappedDrawContext(
709 sk_ref_sp(tex->asRenderTarget()),
robertphillips6738c702016-07-27 12:13:51 -0700710 std::move(colorSpace), surfaceProps));
robertphillipsd4c741e2016-04-28 09:55:15 -0700711 if (!drawContext) {
712 return nullptr;
713 }
714
715 return drawContext;
716}
717
joshualitt1de610a2016-01-06 08:26:09 -0800718bool GrContext::abandoned() const {
719 ASSERT_SINGLE_OWNER
robertphillips7761d612016-05-16 09:14:53 -0700720 return fDrawingManager->wasAbandoned();
robertphillips77a2e522015-10-17 07:43:27 -0700721}
722
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000723namespace {
724void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
725 GrConfigConversionEffect::PMConversion pmToUPM;
726 GrConfigConversionEffect::PMConversion upmToPM;
727 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
728 *pmToUPMValue = pmToUPM;
729 *upmToPMValue = upmToPM;
730}
731}
732
bsalomon6c6f6582015-09-10 08:12:46 -0700733void GrContext::testPMConversionsIfNecessary(uint32_t flags) {
joshualitt1de610a2016-01-06 08:26:09 -0800734 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700735 if (SkToBool(kUnpremul_PixelOpsFlag & flags)) {
736 SkAutoMutexAcquire ama(fTestPMConversionsMutex);
737 if (!fDidTestPMConversions) {
738 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
739 fDidTestPMConversions = true;
740 }
741 }
742}
743
bungeman06ca8ec2016-06-09 08:01:03 -0700744sk_sp<GrFragmentProcessor> GrContext::createPMToUPMEffect(GrTexture* texture,
bsalomon6c9cd552016-01-22 07:17:34 -0800745 const GrSwizzle& swizzle,
bsalomon6c6f6582015-09-10 08:12:46 -0700746 const SkMatrix& matrix) const {
joshualitt1de610a2016-01-06 08:26:09 -0800747 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700748 // We should have already called this->testPMConversionsIfNecessary().
749 SkASSERT(fDidTestPMConversions);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000750 GrConfigConversionEffect::PMConversion pmToUPM =
751 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
752 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bungeman06ca8ec2016-06-09 08:01:03 -0700753 return GrConfigConversionEffect::Make(texture, swizzle, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000754 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700755 return nullptr;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000756 }
757}
758
bungeman06ca8ec2016-06-09 08:01:03 -0700759sk_sp<GrFragmentProcessor> GrContext::createUPMToPMEffect(GrTexture* texture,
bsalomon6c9cd552016-01-22 07:17:34 -0800760 const GrSwizzle& swizzle,
bsalomon6c6f6582015-09-10 08:12:46 -0700761 const SkMatrix& matrix) const {
joshualitt1de610a2016-01-06 08:26:09 -0800762 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700763 // We should have already called this->testPMConversionsIfNecessary().
764 SkASSERT(fDidTestPMConversions);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000765 GrConfigConversionEffect::PMConversion upmToPM =
766 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
767 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
bungeman06ca8ec2016-06-09 08:01:03 -0700768 return GrConfigConversionEffect::Make(texture, swizzle, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000769 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700770 return nullptr;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000771 }
772}
773
bsalomon636e8022015-07-29 06:08:46 -0700774bool GrContext::didFailPMUPMConversionTest() const {
joshualitt1de610a2016-01-06 08:26:09 -0800775 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700776 // We should have already called this->testPMConversionsIfNecessary().
777 SkASSERT(fDidTestPMConversions);
bsalomon636e8022015-07-29 06:08:46 -0700778 // The PM<->UPM tests fail or succeed together so we only need to check one.
bsalomon6c6f6582015-09-10 08:12:46 -0700779 return GrConfigConversionEffect::kNone_PMConversion == fPMToUPMConversion;
bsalomon636e8022015-07-29 06:08:46 -0700780}
781
bsalomon37f9a262015-02-02 13:00:10 -0800782//////////////////////////////////////////////////////////////////////////////
783
784void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800785 ASSERT_SINGLE_OWNER
bsalomon37f9a262015-02-02 13:00:10 -0800786 if (maxTextures) {
bsalomon0ea80f42015-02-11 10:49:59 -0800787 *maxTextures = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -0800788 }
789 if (maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800790 *maxTextureBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -0800791 }
792}
793
794void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
joshualitt1de610a2016-01-06 08:26:09 -0800795 ASSERT_SINGLE_OWNER
bsalomon0ea80f42015-02-11 10:49:59 -0800796 fResourceCache->setLimits(maxTextures, maxTextureBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800797}
798
ericrk0a5fa482015-09-15 14:16:10 -0700799//////////////////////////////////////////////////////////////////////////////
800
801void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -0800802 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -0700803 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
804}