blob: a186bd87bc101f1cf9107ccc1903172adf354622 [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"
bsalomon682c2692015-05-22 14:01:46 -07009#include "GrContextOptions.h"
robertphillips77a2e522015-10-17 07:43:27 -070010#include "GrDrawingManager.h"
robertphillipsea461502015-05-26 11:38:03 -070011#include "GrDrawContext.h"
robertphillips@google.come930a072014-04-03 00:34:27 +000012#include "GrLayerCache.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"
joshualitte8042922015-12-11 06:11:21 -080023#include "text/GrTextBlobCache.h"
joshualitt5478d422014-11-14 16:00:38 -080024
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000025#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
joshualitt1de610a2016-01-06 08:26:09 -080026#define ASSERT_SINGLE_OWNER \
27 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(&fSingleOwner);)
robertphillips77a2e522015-10-17 07:43:27 -070028#define RETURN_IF_ABANDONED if (fDrawingManager->abandoned()) { return; }
29#define RETURN_FALSE_IF_ABANDONED if (fDrawingManager->abandoned()) { return false; }
30#define RETURN_NULL_IF_ABANDONED if (fDrawingManager->abandoned()) { return nullptr; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000031
robertphillipsea461502015-05-26 11:38:03 -070032////////////////////////////////////////////////////////////////////////////////
33
bsalomon682c2692015-05-22 14:01:46 -070034GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext) {
35 GrContextOptions defaultOptions;
36 return Create(backend, backendContext, defaultOptions);
37}
bsalomonf28cff72015-05-22 12:25:41 -070038
bsalomon682c2692015-05-22 14:01:46 -070039GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
40 const GrContextOptions& options) {
halcanary385fe4d2015-08-26 13:07:48 -070041 GrContext* context = new GrContext;
bsalomon682c2692015-05-22 14:01:46 -070042
43 if (context->init(backend, backendContext, options)) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000044 return context;
45 } else {
46 context->unref();
halcanary96fcdcc2015-08-27 07:41:13 -070047 return nullptr;
bsalomon@google.com27847de2011-02-22 20:59:41 +000048 }
bsalomon@google.com27847de2011-02-22 20:59:41 +000049}
50
joshualitt0acd0d32015-05-07 08:23:19 -070051static int32_t gNextID = 1;
52static int32_t next_id() {
53 int32_t id;
54 do {
55 id = sk_atomic_inc(&gNextID);
56 } while (id == SK_InvalidGenID);
57 return id;
58}
59
bsalomon682c2692015-05-22 14:01:46 -070060GrContext::GrContext() : fUniqueID(next_id()) {
halcanary96fcdcc2015-08-27 07:41:13 -070061 fGpu = nullptr;
62 fCaps = nullptr;
63 fResourceCache = nullptr;
64 fResourceProvider = nullptr;
halcanary96fcdcc2015-08-27 07:41:13 -070065 fBatchFontCache = nullptr;
bsalomonf21dab92014-11-13 13:33:28 -080066 fFlushToReduceCacheSize = false;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000067}
68
bsalomon682c2692015-05-22 14:01:46 -070069bool GrContext::init(GrBackend backend, GrBackendContext backendContext,
70 const GrContextOptions& options) {
joshualitt1de610a2016-01-06 08:26:09 -080071 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -070072 SkASSERT(!fGpu);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000073
bsalomon682c2692015-05-22 14:01:46 -070074 fGpu = GrGpu::Create(backend, backendContext, options, this);
robertphillipsea461502015-05-26 11:38:03 -070075 if (!fGpu) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000076 return false;
77 }
bsalomon69cfe952015-11-30 13:27:47 -080078 this->initCommon(options);
bsalomon33435572014-11-05 14:47:41 -080079 return true;
80}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000081
bsalomon69cfe952015-11-30 13:27:47 -080082void GrContext::initCommon(const GrContextOptions& options) {
joshualitt1de610a2016-01-06 08:26:09 -080083 ASSERT_SINGLE_OWNER
84
bsalomon76228632015-05-29 08:02:10 -070085 fCaps = SkRef(fGpu->caps());
halcanary385fe4d2015-08-26 13:07:48 -070086 fResourceCache = new GrResourceCache(fCaps);
bsalomon0ea80f42015-02-11 10:49:59 -080087 fResourceCache->setOverBudgetCallback(OverBudgetCB, this);
joshualitt6d0872d2016-01-11 08:27:48 -080088 fResourceProvider = new GrResourceProvider(fGpu, fResourceCache, &fSingleOwner);
commit-bot@chromium.org1836d332013-07-16 22:55:03 +000089
halcanary385fe4d2015-08-26 13:07:48 -070090 fLayerCache.reset(new GrLayerCache(this));
robertphillips@google.come930a072014-04-03 00:34:27 +000091
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000092 fDidTestPMConversions = false;
93
bsalomon69cfe952015-11-30 13:27:47 -080094 GrDrawTarget::Options dtOptions;
95 dtOptions.fClipBatchToBounds = options.fClipBatchToBounds;
bsalomon6dea83f2015-12-03 12:58:06 -080096 dtOptions.fDrawBatchBounds = options.fDrawBatchBounds;
bsalomon489147c2015-12-14 12:13:09 -080097 dtOptions.fMaxBatchLookback = options.fMaxBatchLookback;
bsalomonaecc0182016-03-07 11:50:44 -080098 dtOptions.fMaxBatchLookahead = options.fMaxBatchLookahead;
joshualittde8dc7e2016-01-08 10:09:13 -080099 fDrawingManager.reset(new GrDrawingManager(this, dtOptions, &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();
154 fLayerCache->freeAll();
155 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();
robertphillips@google.come930a072014-04-03 00:34:27 +0000173 fLayerCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700174 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000175}
176
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000177void GrContext::resetContext(uint32_t state) {
joshualitt1de610a2016-01-06 08:26:09 -0800178 ASSERT_SINGLE_OWNER
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000179 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000180}
181
182void GrContext::freeGpuResources() {
joshualitt1de610a2016-01-06 08:26:09 -0800183 ASSERT_SINGLE_OWNER
184
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000185 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000186
joshualitt7c3a2f82015-03-31 13:32:05 -0700187 fBatchFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000188 fLayerCache->freeAll();
robertphillips68737822015-10-29 12:12:21 -0700189
190 fDrawingManager->freeGpuResources();
bsalomon3033b9f2015-04-13 11:09:56 -0700191
192 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000193}
194
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000195void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800196 ASSERT_SINGLE_OWNER
197
bsalomon71cb0c22014-11-14 12:10:14 -0800198 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800199 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800200 }
201 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800202 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800203 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000204}
205
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000206////////////////////////////////////////////////////////////////////////////////
207
bsalomon71cb0c22014-11-14 12:10:14 -0800208void GrContext::OverBudgetCB(void* data) {
bsalomon66a450f2014-11-13 13:19:10 -0800209 SkASSERT(data);
bsalomonf21dab92014-11-13 13:33:28 -0800210
bsalomon66a450f2014-11-13 13:19:10 -0800211 GrContext* context = reinterpret_cast<GrContext*>(data);
bsalomonf21dab92014-11-13 13:33:28 -0800212
joshualittb542bae2015-07-28 09:58:39 -0700213 // Flush the GrBufferedDrawTarget to possibly free up some textures
bsalomonf21dab92014-11-13 13:33:28 -0800214 context->fFlushToReduceCacheSize = true;
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000215}
216
joshualitt0db6dfa2015-04-10 07:01:30 -0700217void GrContext::TextBlobCacheOverBudgetCB(void* data) {
218 SkASSERT(data);
219
220 // Unlike the GrResourceCache, TextBlobs are drawn at the SkGpuDevice level, therefore they
221 // cannot use fFlushTorReduceCacheSize because it uses AutoCheckFlush. The solution is to move
222 // drawText calls to below the GrContext level, but this is not trivial because they call
223 // drawPath on SkGpuDevice
224 GrContext* context = reinterpret_cast<GrContext*>(data);
225 context->flush();
226}
227
bsalomon@google.com27847de2011-02-22 20:59:41 +0000228////////////////////////////////////////////////////////////////////////////////
229
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000230void GrContext::flush(int flagsBitfield) {
joshualitt1de610a2016-01-06 08:26:09 -0800231 ASSERT_SINGLE_OWNER
robertphillipsea461502015-05-26 11:38:03 -0700232 RETURN_IF_ABANDONED
robertphillips@google.come7db8d62013-07-04 11:48:52 +0000233
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000234 if (kDiscard_FlushBit & flagsBitfield) {
robertphillips77a2e522015-10-17 07:43:27 -0700235 fDrawingManager->reset();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000236 } else {
robertphillips77a2e522015-10-17 07:43:27 -0700237 fDrawingManager->flush();
junov@google.com53a55842011-06-08 22:55:10 +0000238 }
bsalomon3f324322015-04-08 11:01:54 -0700239 fResourceCache->notifyFlushOccurred();
bsalomonf21dab92014-11-13 13:33:28 -0800240 fFlushToReduceCacheSize = false;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000241}
242
bsalomon81beccc2014-10-13 12:32:55 -0700243bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
244 const void* inPixels, size_t outRowBytes, void* outPixels) {
245 SkSrcPixelInfo srcPI;
halcanary96fcdcc2015-08-27 07:41:13 -0700246 if (!GrPixelConfig2ColorAndProfileType(srcConfig, &srcPI.fColorType, nullptr)) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000247 return false;
248 }
bsalomon81beccc2014-10-13 12:32:55 -0700249 srcPI.fAlphaType = kUnpremul_SkAlphaType;
250 srcPI.fPixels = inPixels;
251 srcPI.fRowBytes = inRowBytes;
252
253 SkDstPixelInfo dstPI;
254 dstPI.fColorType = srcPI.fColorType;
255 dstPI.fAlphaType = kPremul_SkAlphaType;
256 dstPI.fPixels = outPixels;
257 dstPI.fRowBytes = outRowBytes;
258
259 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000260}
261
bsalomon81beccc2014-10-13 12:32:55 -0700262bool GrContext::writeSurfacePixels(GrSurface* surface,
263 int left, int top, int width, int height,
264 GrPixelConfig srcConfig, const void* buffer, size_t rowBytes,
265 uint32_t pixelOpsFlags) {
joshualitt1de610a2016-01-06 08:26:09 -0800266 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800267 RETURN_FALSE_IF_ABANDONED
bsalomon6c6f6582015-09-10 08:12:46 -0700268 ASSERT_OWNED_RESOURCE(surface);
269 SkASSERT(surface);
joshualittbc907352016-01-13 06:45:40 -0800270 GR_AUDIT_TRAIL_AUTO_FRAME(&fAuditTrail, "GrContext::writeSurfacePixels");
bsalomon6c6f6582015-09-10 08:12:46 -0700271
272 this->testPMConversionsIfNecessary(pixelOpsFlags);
bsalomon81beccc2014-10-13 12:32:55 -0700273
bsalomone8d21e82015-07-16 08:23:13 -0700274 // 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 -0700275 // necessary and because GrGpu::getWritePixelsInfo requires it.
bsalomone8d21e82015-07-16 08:23:13 -0700276 if (!GrSurfacePriv::AdjustWritePixelParams(surface->width(), surface->height(),
277 GrBytesPerPixel(srcConfig), &left, &top, &width,
278 &height, &buffer, &rowBytes)) {
279 return false;
280 }
281
bsalomonf0674512015-07-28 13:26:15 -0700282 bool applyPremulToSrc = false;
bsalomon81beccc2014-10-13 12:32:55 -0700283 if (kUnpremul_PixelOpsFlag & pixelOpsFlags) {
284 if (!GrPixelConfigIs8888(srcConfig)) {
285 return false;
286 }
bsalomonf0674512015-07-28 13:26:15 -0700287 applyPremulToSrc = true;
288 }
bsalomon636e8022015-07-29 06:08:46 -0700289
290 GrGpu::DrawPreference drawPreference = GrGpu::kNoDraw_DrawPreference;
291 // Don't prefer to draw for the conversion (and thereby access a texture from the cache) when
292 // we've already determined that there isn't a roundtrip preserving conversion processor pair.
293 if (applyPremulToSrc && !this->didFailPMUPMConversionTest()) {
294 drawPreference = GrGpu::kCallerPrefersDraw_DrawPreference;
295 }
296
bsalomonf0674512015-07-28 13:26:15 -0700297 GrGpu::WritePixelTempDrawInfo tempDrawInfo;
cblumeed828002016-02-16 13:00:01 -0800298 if (!fGpu->getWritePixelsInfo(surface, width, height, srcConfig, &drawPreference,
bsalomonf0674512015-07-28 13:26:15 -0700299 &tempDrawInfo)) {
300 return false;
301 }
302
303 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && surface->surfacePriv().hasPendingIO()) {
304 this->flush();
305 }
306
307 SkAutoTUnref<GrTexture> tempTexture;
308 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomoneae62002015-07-31 13:59:30 -0700309 tempTexture.reset(
310 this->textureProvider()->createApproxTexture(tempDrawInfo.fTempSurfaceDesc));
bsalomonf0674512015-07-28 13:26:15 -0700311 if (!tempTexture && GrGpu::kRequireDraw_DrawPreference == drawPreference) {
312 return false;
313 }
314 }
315
316 // temp buffer for doing sw premul conversion, if needed.
317 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
318 if (tempTexture) {
319 SkAutoTUnref<const GrFragmentProcessor> fp;
320 SkMatrix textureMatrix;
321 textureMatrix.setIDiv(tempTexture->width(), tempTexture->height());
bsalomonf0674512015-07-28 13:26:15 -0700322 if (applyPremulToSrc) {
bsalomon6c9cd552016-01-22 07:17:34 -0800323 fp.reset(this->createUPMToPMEffect(tempTexture, tempDrawInfo.fSwizzle,
bsalomon4a339522015-10-06 08:40:50 -0700324 textureMatrix));
bsalomonf0674512015-07-28 13:26:15 -0700325 // If premultiplying was the only reason for the draw, fall back to a straight write.
326 if (!fp) {
327 if (GrGpu::kCallerPrefersDraw_DrawPreference == drawPreference) {
halcanary96fcdcc2015-08-27 07:41:13 -0700328 tempTexture.reset(nullptr);
bsalomonf0674512015-07-28 13:26:15 -0700329 }
330 } else {
331 applyPremulToSrc = false;
332 }
333 }
334 if (tempTexture) {
335 if (!fp) {
bsalomon6c9cd552016-01-22 07:17:34 -0800336 fp.reset(GrConfigConversionEffect::Create(tempTexture, tempDrawInfo.fSwizzle,
bsalomonf0674512015-07-28 13:26:15 -0700337 GrConfigConversionEffect::kNone_PMConversion, textureMatrix));
338 if (!fp) {
339 return false;
340 }
341 }
342 GrRenderTarget* renderTarget = surface->asRenderTarget();
343 SkASSERT(renderTarget);
344 if (tempTexture->surfacePriv().hasPendingIO()) {
345 this->flush();
346 }
347 if (applyPremulToSrc) {
348 size_t tmpRowBytes = 4 * width;
349 tmpPixels.reset(width * height);
350 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
351 tmpPixels.get())) {
352 return false;
353 }
354 rowBytes = tmpRowBytes;
355 buffer = tmpPixels.get();
356 applyPremulToSrc = false;
357 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700358 if (!fGpu->writePixels(tempTexture, 0, 0, width, height,
bsalomon6c9cd552016-01-22 07:17:34 -0800359 tempDrawInfo.fWriteConfig, buffer,
bsalomon6cb3cbe2015-07-30 07:34:27 -0700360 rowBytes)) {
bsalomonf0674512015-07-28 13:26:15 -0700361 return false;
362 }
363 SkMatrix matrix;
364 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
robertphillips6c7e3252016-04-27 10:47:51 -0700365 sk_sp<GrDrawContext> drawContext(this->drawContext(sk_ref_sp(renderTarget)));
bsalomonf0674512015-07-28 13:26:15 -0700366 if (!drawContext) {
367 return false;
368 }
brianosman898235c2016-04-06 07:38:23 -0700369 // SRGBTODO: AllowSRGBInputs? (We could force it on here, so we don't need the
370 // per-texture override in config conversion effect?)
egdanielc4b72722015-11-23 13:20:41 -0800371 GrPaint paint;
bsalomonac856c92015-08-27 06:30:17 -0700372 paint.addColorFragmentProcessor(fp);
egdanielc4b72722015-11-23 13:20:41 -0800373 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
bsalomonf0674512015-07-28 13:26:15 -0700374 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
robertphillips2e1e51f2015-10-15 08:01:48 -0700375 drawContext->drawRect(GrClip::WideOpen(), paint, matrix, rect, nullptr);
bsalomonf0674512015-07-28 13:26:15 -0700376
377 if (kFlushWrites_PixelOp & pixelOpsFlags) {
378 this->flushSurfaceWrites(surface);
379 }
380 }
381 }
382 if (!tempTexture) {
bsalomonf0674512015-07-28 13:26:15 -0700383 if (applyPremulToSrc) {
bsalomon81beccc2014-10-13 12:32:55 -0700384 size_t tmpRowBytes = 4 * width;
385 tmpPixels.reset(width * height);
386 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
387 tmpPixels.get())) {
388 return false;
389 }
390 rowBytes = tmpRowBytes;
391 buffer = tmpPixels.get();
bsalomonf0674512015-07-28 13:26:15 -0700392 applyPremulToSrc = false;
bsalomon81beccc2014-10-13 12:32:55 -0700393 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700394 return fGpu->writePixels(surface, left, top, width, height, srcConfig, buffer, rowBytes);
bsalomon81beccc2014-10-13 12:32:55 -0700395 }
bsalomon81beccc2014-10-13 12:32:55 -0700396 return true;
397}
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000398
bsalomone8d21e82015-07-16 08:23:13 -0700399bool GrContext::readSurfacePixels(GrSurface* src,
400 int left, int top, int width, int height,
401 GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
402 uint32_t flags) {
joshualitt1de610a2016-01-06 08:26:09 -0800403 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800404 RETURN_FALSE_IF_ABANDONED
bsalomone8d21e82015-07-16 08:23:13 -0700405 ASSERT_OWNED_RESOURCE(src);
406 SkASSERT(src);
joshualittbc907352016-01-13 06:45:40 -0800407 GR_AUDIT_TRAIL_AUTO_FRAME(&fAuditTrail, "GrContext::readSurfacePixels");
bsalomon32ab2602015-09-09 18:57:49 -0700408
bsalomon6c6f6582015-09-10 08:12:46 -0700409 this->testPMConversionsIfNecessary(flags);
410 SkAutoMutexAcquire ama(fReadPixelsMutex);
411
bsalomone8d21e82015-07-16 08:23:13 -0700412 // Adjust the params so that if we wind up using an intermediate surface we've already done
413 // all the trimming and the temporary can be the min size required.
414 if (!GrSurfacePriv::AdjustReadPixelParams(src->width(), src->height(),
415 GrBytesPerPixel(dstConfig), &left,
416 &top, &width, &height, &buffer, &rowBytes)) {
417 return false;
418 }
419
420 if (!(kDontFlush_PixelOpsFlag & flags) && src->surfacePriv().hasPendingWrite()) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000421 this->flush();
422 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000423
bsalomone8d21e82015-07-16 08:23:13 -0700424 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.com9c680582013-02-06 18:17:50 +0000425 if (unpremul && !GrPixelConfigIs8888(dstConfig)) {
bsalomon39826022015-07-23 08:07:21 -0700426 // The unpremul flag is only allowed for 8888 configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000427 return false;
428 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000429
bsalomon636e8022015-07-29 06:08:46 -0700430 GrGpu::DrawPreference drawPreference = GrGpu::kNoDraw_DrawPreference;
431 // Don't prefer to draw for the conversion (and thereby access a texture from the cache) when
432 // we've already determined that there isn't a roundtrip preserving conversion processor pair.
433 if (unpremul && !this->didFailPMUPMConversionTest()) {
434 drawPreference = GrGpu::kCallerPrefersDraw_DrawPreference;
435 }
436
bsalomon39826022015-07-23 08:07:21 -0700437 GrGpu::ReadPixelTempDrawInfo tempDrawInfo;
438 if (!fGpu->getReadPixelsInfo(src, width, height, rowBytes, dstConfig, &drawPreference,
439 &tempDrawInfo)) {
440 return false;
441 }
bsalomon191bcc02014-11-14 11:31:13 -0800442
bsalomon6cb3cbe2015-07-30 07:34:27 -0700443 SkAutoTUnref<GrSurface> surfaceToRead(SkRef(src));
bsalomon39826022015-07-23 08:07:21 -0700444 bool didTempDraw = false;
445 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomon39826022015-07-23 08:07:21 -0700446 if (tempDrawInfo.fUseExactScratch) {
447 // We only respect this when the entire src is being read. Otherwise we can trigger too
448 // many odd ball texture sizes and trash the cache.
bsalomoneae62002015-07-31 13:59:30 -0700449 if (width != src->width() || height != src->height()) {
450 tempDrawInfo.fUseExactScratch = false;
bsalomon39826022015-07-23 08:07:21 -0700451 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000452 }
bsalomon39826022015-07-23 08:07:21 -0700453 SkAutoTUnref<GrTexture> temp;
bsalomoneae62002015-07-31 13:59:30 -0700454 if (tempDrawInfo.fUseExactScratch) {
bsalomon5ec26ae2016-02-25 08:33:02 -0800455 temp.reset(this->textureProvider()->createTexture(tempDrawInfo.fTempSurfaceDesc,
456 SkBudgeted::kYes));
bsalomoneae62002015-07-31 13:59:30 -0700457 } else {
458 temp.reset(this->textureProvider()->createApproxTexture(tempDrawInfo.fTempSurfaceDesc));
459 }
bsalomon39826022015-07-23 08:07:21 -0700460 if (temp) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000461 SkMatrix textureMatrix;
bsalomon39826022015-07-23 08:07:21 -0700462 textureMatrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000463 textureMatrix.postIDiv(src->width(), src->height());
joshualittb0a8a372014-09-23 09:50:21 -0700464 SkAutoTUnref<const GrFragmentProcessor> fp;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000465 if (unpremul) {
bsalomon6c9cd552016-01-22 07:17:34 -0800466 fp.reset(this->createPMToUPMEffect(src->asTexture(), tempDrawInfo.fSwizzle,
bsalomon39826022015-07-23 08:07:21 -0700467 textureMatrix));
joshualittb0a8a372014-09-23 09:50:21 -0700468 if (fp) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000469 unpremul = false; // we no longer need to do this on CPU after the read back.
bsalomon39826022015-07-23 08:07:21 -0700470 } else if (GrGpu::kCallerPrefersDraw_DrawPreference == drawPreference) {
471 // We only wanted to do the draw in order to perform the unpremul so don't
472 // bother.
halcanary96fcdcc2015-08-27 07:41:13 -0700473 temp.reset(nullptr);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000474 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000475 }
bsalomon39826022015-07-23 08:07:21 -0700476 if (!fp && temp) {
bsalomon6c9cd552016-01-22 07:17:34 -0800477 fp.reset(GrConfigConversionEffect::Create(src->asTexture(), tempDrawInfo.fSwizzle,
bsalomon39826022015-07-23 08:07:21 -0700478 GrConfigConversionEffect::kNone_PMConversion, textureMatrix));
479 }
480 if (fp) {
brianosman898235c2016-04-06 07:38:23 -0700481 // SRGBTODO: AllowSRGBInputs? (We could force it on here, so we don't need the
482 // per-texture override in config conversion effect?)
egdanielc4b72722015-11-23 13:20:41 -0800483 GrPaint paint;
bsalomonac856c92015-08-27 06:30:17 -0700484 paint.addColorFragmentProcessor(fp);
egdanielc4b72722015-11-23 13:20:41 -0800485 paint.setPorterDuffXPFactory(SkXfermode::kSrc_Mode);
bsalomon39826022015-07-23 08:07:21 -0700486 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
robertphillips6c7e3252016-04-27 10:47:51 -0700487 sk_sp<GrDrawContext> drawContext(
488 this->drawContext(sk_ref_sp(temp->asRenderTarget())));
robertphillips2e1e51f2015-10-15 08:01:48 -0700489 drawContext->drawRect(GrClip::WideOpen(), paint, SkMatrix::I(), rect, nullptr);
bsalomon6cb3cbe2015-07-30 07:34:27 -0700490 surfaceToRead.reset(SkRef(temp.get()));
bsalomon39826022015-07-23 08:07:21 -0700491 left = 0;
492 top = 0;
493 didTempDraw = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000494 }
bsalomon@google.com0342a852012-08-20 19:22:38 +0000495 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000496 }
joshualitt5c55fef2014-10-31 14:04:35 -0700497
bsalomon39826022015-07-23 08:07:21 -0700498 if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000499 return false;
500 }
bsalomon39826022015-07-23 08:07:21 -0700501 GrPixelConfig configToRead = dstConfig;
502 if (didTempDraw) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700503 this->flushSurfaceWrites(surfaceToRead);
bsalomon6c9cd552016-01-22 07:17:34 -0800504 configToRead = tempDrawInfo.fReadConfig;
bsalomon39826022015-07-23 08:07:21 -0700505 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700506 if (!fGpu->readPixels(surfaceToRead, left, top, width, height, configToRead, buffer,
507 rowBytes)) {
bsalomon39826022015-07-23 08:07:21 -0700508 return false;
509 }
510
511 // Perform umpremul conversion if we weren't able to perform it as a draw.
512 if (unpremul) {
reed@google.com7111d462014-03-25 16:20:24 +0000513 SkDstPixelInfo dstPI;
halcanary96fcdcc2015-08-27 07:41:13 -0700514 if (!GrPixelConfig2ColorAndProfileType(dstConfig, &dstPI.fColorType, nullptr)) {
reed@google.com7111d462014-03-25 16:20:24 +0000515 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000516 }
reed@google.com7111d462014-03-25 16:20:24 +0000517 dstPI.fAlphaType = kUnpremul_SkAlphaType;
518 dstPI.fPixels = buffer;
519 dstPI.fRowBytes = rowBytes;
520
521 SkSrcPixelInfo srcPI;
bsalomon39826022015-07-23 08:07:21 -0700522 srcPI.fColorType = dstPI.fColorType;
reed@google.com7111d462014-03-25 16:20:24 +0000523 srcPI.fAlphaType = kPremul_SkAlphaType;
524 srcPI.fPixels = buffer;
525 srcPI.fRowBytes = rowBytes;
526
527 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000528 }
529 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000530}
531
bsalomonc49e8682015-06-30 11:37:35 -0700532void GrContext::prepareSurfaceForExternalIO(GrSurface* surface) {
joshualitt1de610a2016-01-06 08:26:09 -0800533 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800534 RETURN_IF_ABANDONED
bsalomon87a94eb2014-11-03 14:28:32 -0800535 SkASSERT(surface);
536 ASSERT_OWNED_RESOURCE(surface);
537 if (surface->surfacePriv().hasPendingIO()) {
538 this->flush();
539 }
540 GrRenderTarget* rt = surface->asRenderTarget();
541 if (fGpu && rt) {
542 fGpu->resolveRenderTarget(rt);
bsalomon41ebbdd2014-08-04 08:31:39 -0700543 }
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000544}
545
bsalomonb8fea972016-02-16 07:34:17 -0800546bool GrContext::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
547 const SkIPoint& dstPoint) {
joshualitt1de610a2016-01-06 08:26:09 -0800548 ASSERT_SINGLE_OWNER
bsalomonb8fea972016-02-16 07:34:17 -0800549 RETURN_FALSE_IF_ABANDONED
joshualittbc907352016-01-13 06:45:40 -0800550 GR_AUDIT_TRAIL_AUTO_FRAME(&fAuditTrail, "GrContext::copySurface");
551
robertphillipsea461502015-05-26 11:38:03 -0700552 if (!src || !dst) {
bsalomonb8fea972016-02-16 07:34:17 -0800553 return false;
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000554 }
bsalomone3d4bf22014-09-23 09:15:03 -0700555 ASSERT_OWNED_RESOURCE(src);
junov2bb52102014-09-29 10:18:59 -0700556 ASSERT_OWNED_RESOURCE(dst);
Brian Salomon34a98952014-09-24 11:41:24 -0400557
robertphillipsea461502015-05-26 11:38:03 -0700558 if (!dst->asRenderTarget()) {
bsalomonb8fea972016-02-16 07:34:17 -0800559 SkIRect clippedSrcRect;
560 SkIPoint clippedDstPoint;
561 if (!GrCopySurfaceBatch::ClipSrcRectAndDstPoint(dst, src, srcRect, dstPoint,
562 &clippedSrcRect, &clippedDstPoint)) {
563 return false;
564 }
565 // If we don't have an RT for the dst then we won't have a GrDrawContext to insert the
566 // the copy surface into. In the future we plan to have a more limited Context type
567 // (GrCopyContext?) that has the subset of GrDrawContext operations that should be
568 // allowed on textures that aren't render targets.
569 // For now we just flush any writes to the src and issue an immediate copy to the dst.
570 src->flushWrites();
571 return fGpu->copySurface(dst, src, clippedSrcRect, clippedDstPoint);
robertphillipsea461502015-05-26 11:38:03 -0700572 }
robertphillips6c7e3252016-04-27 10:47:51 -0700573 sk_sp<GrDrawContext> drawContext(this->drawContext(sk_ref_sp(dst->asRenderTarget())));
kjlubick0eed9452016-02-11 12:05:24 -0800574 if (!drawContext) {
bsalomonb8fea972016-02-16 07:34:17 -0800575 return false;
bsalomonf80bfed2014-10-07 05:56:02 -0700576 }
kjlubick0eed9452016-02-11 12:05:24 -0800577
bsalomonb8fea972016-02-16 07:34:17 -0800578 if (!drawContext->copySurface(src, srcRect, dstPoint)) {
579 return false;
kjlubick0eed9452016-02-11 12:05:24 -0800580 }
bsalomonb8fea972016-02-16 07:34:17 -0800581 return true;
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000582}
583
bsalomonf80bfed2014-10-07 05:56:02 -0700584void GrContext::flushSurfaceWrites(GrSurface* surface) {
joshualitt1de610a2016-01-06 08:26:09 -0800585 ASSERT_SINGLE_OWNER
joshualitt5f5a8d72015-02-25 14:09:45 -0800586 RETURN_IF_ABANDONED
bsalomonf80bfed2014-10-07 05:56:02 -0700587 if (surface->surfacePriv().hasPendingWrite()) {
588 this->flush();
589 }
590}
591
bsalomon@google.com27847de2011-02-22 20:59:41 +0000592////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000593int GrContext::getRecommendedSampleCount(GrPixelConfig config,
594 SkScalar dpi) const {
joshualitt1de610a2016-01-06 08:26:09 -0800595 ASSERT_SINGLE_OWNER
596
bsalomon76228632015-05-29 08:02:10 -0700597 if (!this->caps()->isConfigRenderable(config, true)) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000598 return 0;
599 }
600 int chosenSampleCount = 0;
jvanverthe9c0fc62015-04-29 11:18:05 -0700601 if (fGpu->caps()->shaderCaps()->pathRenderingSupport()) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000602 if (dpi >= 250.0f) {
603 chosenSampleCount = 4;
604 } else {
605 chosenSampleCount = 16;
606 }
607 }
egdanieleed519e2016-01-15 11:36:18 -0800608 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ? chosenSampleCount : 0;
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000609}
610
robertphillips77a2e522015-10-17 07:43:27 -0700611
robertphillips6c7e3252016-04-27 10:47:51 -0700612sk_sp<GrDrawContext> GrContext::drawContext(sk_sp<GrRenderTarget> rt,
613 const SkSurfaceProps* surfaceProps) {
joshualitt1de610a2016-01-06 08:26:09 -0800614 ASSERT_SINGLE_OWNER
robertphillips6c7e3252016-04-27 10:47:51 -0700615 return fDrawingManager->drawContext(std::move(rt), surfaceProps);
robertphillips77a2e522015-10-17 07:43:27 -0700616}
617
robertphillipsd4c741e2016-04-28 09:55:15 -0700618sk_sp<GrDrawContext> GrContext::newDrawContext(BackingFit fit,
619 int width, int height,
620 GrPixelConfig config,
621 int sampleCnt,
622 GrSurfaceOrigin origin) {
623 GrSurfaceDesc desc;
624 desc.fFlags = kRenderTarget_GrSurfaceFlag;
625 desc.fOrigin = origin;
626 desc.fWidth = width;
627 desc.fHeight = height;
628 desc.fConfig = config;
629 desc.fSampleCnt = sampleCnt;
630
631 sk_sp<GrTexture> tex;
632 if (kTight_BackingFit == fit) {
633 tex.reset(this->textureProvider()->createTexture(desc, SkBudgeted::kYes));
634 } else {
635 tex.reset(this->textureProvider()->createApproxTexture(desc));
636 }
637 if (!tex) {
638 return nullptr;
639 }
640
641 sk_sp<GrDrawContext> drawContext(this->drawContext(sk_ref_sp(tex->asRenderTarget())));
642 if (!drawContext) {
643 return nullptr;
644 }
645
646 return drawContext;
647}
648
joshualitt1de610a2016-01-06 08:26:09 -0800649bool GrContext::abandoned() const {
650 ASSERT_SINGLE_OWNER
robertphillips77a2e522015-10-17 07:43:27 -0700651 return fDrawingManager->abandoned();
652}
653
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000654namespace {
655void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
656 GrConfigConversionEffect::PMConversion pmToUPM;
657 GrConfigConversionEffect::PMConversion upmToPM;
658 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
659 *pmToUPMValue = pmToUPM;
660 *upmToPMValue = upmToPM;
661}
662}
663
bsalomon6c6f6582015-09-10 08:12:46 -0700664void GrContext::testPMConversionsIfNecessary(uint32_t flags) {
joshualitt1de610a2016-01-06 08:26:09 -0800665 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700666 if (SkToBool(kUnpremul_PixelOpsFlag & flags)) {
667 SkAutoMutexAcquire ama(fTestPMConversionsMutex);
668 if (!fDidTestPMConversions) {
669 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
670 fDidTestPMConversions = true;
671 }
672 }
673}
674
bsalomon4a339522015-10-06 08:40:50 -0700675const GrFragmentProcessor* GrContext::createPMToUPMEffect(GrTexture* texture,
bsalomon6c9cd552016-01-22 07:17:34 -0800676 const GrSwizzle& swizzle,
bsalomon6c6f6582015-09-10 08:12:46 -0700677 const SkMatrix& matrix) const {
joshualitt1de610a2016-01-06 08:26:09 -0800678 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700679 // We should have already called this->testPMConversionsIfNecessary().
680 SkASSERT(fDidTestPMConversions);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000681 GrConfigConversionEffect::PMConversion pmToUPM =
682 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
683 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bsalomon6c9cd552016-01-22 07:17:34 -0800684 return GrConfigConversionEffect::Create(texture, swizzle, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000685 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700686 return nullptr;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000687 }
688}
689
bsalomon4a339522015-10-06 08:40:50 -0700690const GrFragmentProcessor* GrContext::createUPMToPMEffect(GrTexture* texture,
bsalomon6c9cd552016-01-22 07:17:34 -0800691 const GrSwizzle& swizzle,
bsalomon6c6f6582015-09-10 08:12:46 -0700692 const SkMatrix& matrix) const {
joshualitt1de610a2016-01-06 08:26:09 -0800693 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700694 // We should have already called this->testPMConversionsIfNecessary().
695 SkASSERT(fDidTestPMConversions);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000696 GrConfigConversionEffect::PMConversion upmToPM =
697 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
698 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
bsalomon6c9cd552016-01-22 07:17:34 -0800699 return GrConfigConversionEffect::Create(texture, swizzle, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000700 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700701 return nullptr;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000702 }
703}
704
bsalomon636e8022015-07-29 06:08:46 -0700705bool GrContext::didFailPMUPMConversionTest() const {
joshualitt1de610a2016-01-06 08:26:09 -0800706 ASSERT_SINGLE_OWNER
bsalomon6c6f6582015-09-10 08:12:46 -0700707 // We should have already called this->testPMConversionsIfNecessary().
708 SkASSERT(fDidTestPMConversions);
bsalomon636e8022015-07-29 06:08:46 -0700709 // The PM<->UPM tests fail or succeed together so we only need to check one.
bsalomon6c6f6582015-09-10 08:12:46 -0700710 return GrConfigConversionEffect::kNone_PMConversion == fPMToUPMConversion;
bsalomon636e8022015-07-29 06:08:46 -0700711}
712
bsalomon37f9a262015-02-02 13:00:10 -0800713//////////////////////////////////////////////////////////////////////////////
714
715void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
joshualitt1de610a2016-01-06 08:26:09 -0800716 ASSERT_SINGLE_OWNER
bsalomon37f9a262015-02-02 13:00:10 -0800717 if (maxTextures) {
bsalomon0ea80f42015-02-11 10:49:59 -0800718 *maxTextures = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -0800719 }
720 if (maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800721 *maxTextureBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -0800722 }
723}
724
725void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
joshualitt1de610a2016-01-06 08:26:09 -0800726 ASSERT_SINGLE_OWNER
bsalomon0ea80f42015-02-11 10:49:59 -0800727 fResourceCache->setLimits(maxTextures, maxTextureBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800728}
729
ericrk0a5fa482015-09-15 14:16:10 -0700730//////////////////////////////////////////////////////////////////////////////
731
732void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
joshualitt1de610a2016-01-06 08:26:09 -0800733 ASSERT_SINGLE_OWNER
ericrk0a5fa482015-09-15 14:16:10 -0700734 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
735}