blob: 4d6b6b8d0c3e6427c680d471586f6664578f2785 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
bsalomon@google.com27847de2011-02-22 20:59:41 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
bsalomon@google.com27847de2011-02-22 20:59:41 +00007 */
8
bsalomon@google.com1fadb202011-12-12 16:10:08 +00009#include "GrContext.h"
10
joshualitt7c3a2f82015-03-31 13:32:05 -070011#include "GrBatchFontCache.h"
bsalomon75398562015-08-17 12:55:38 -070012#include "GrBatchFlushState.h"
joshualitte5902662015-05-11 08:18:35 -070013#include "GrBatchTest.h"
bsalomoneb1cb5c2015-05-22 08:01:09 -070014#include "GrCaps.h"
bsalomon682c2692015-05-22 14:01:46 -070015#include "GrContextOptions.h"
joshualitt5478d422014-11-14 16:00:38 -080016#include "GrDefaultGeoProcFactory.h"
robertphillipsea461502015-05-26 11:38:03 -070017#include "GrDrawContext.h"
bsalomon512be532015-09-10 10:42:55 -070018#include "GrDrawTarget.h"
bsalomon453cf402014-11-11 14:15:57 -080019#include "GrGpuResource.h"
bsalomon3582d3e2015-02-13 14:20:05 -080020#include "GrGpuResourcePriv.h"
joshualitt5478d422014-11-14 16:00:38 -080021#include "GrGpu.h"
commit-bot@chromium.orgdcb8ef92014-03-27 11:26:10 +000022#include "GrIndexBuffer.h"
robertphillips@google.come930a072014-04-03 00:34:27 +000023#include "GrLayerCache.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +000024#include "GrOvalRenderer.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000025#include "GrPathRenderer.h"
tomhudson@google.comd22b6e42011-06-24 15:53:40 +000026#include "GrPathUtils.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080027#include "GrRenderTargetPriv.h"
bsalomon0ea80f42015-02-11 10:49:59 -080028#include "GrResourceCache.h"
bsalomond309e7a2015-04-30 14:18:54 -070029#include "GrResourceProvider.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000030#include "GrSoftwarePathRenderer.h"
egdanield58a0ba2014-06-11 10:30:05 -070031#include "GrStrokeInfo.h"
bsalomonafbf2d62014-09-30 12:18:44 -070032#include "GrSurfacePriv.h"
joshualittb7133be2015-04-08 09:08:31 -070033#include "GrTextBlobCache.h"
bsalomonafbf2d62014-09-30 12:18:44 -070034#include "GrTexturePriv.h"
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +000035#include "GrTracing.h"
bsalomoncb8979d2015-05-05 09:51:38 -070036#include "GrVertices.h"
egdanield58a0ba2014-06-11 10:30:05 -070037#include "SkDashPathPriv.h"
bsalomon81beccc2014-10-13 12:32:55 -070038#include "SkConfig8888.h"
reed@google.com7111d462014-03-25 16:20:24 +000039#include "SkGr.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000040#include "SkRRect.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000041#include "SkStrokeRec.h"
robertphillipsfcf78292015-06-19 11:49:52 -070042#include "SkSurfacePriv.h"
commit-bot@chromium.orgdcb8ef92014-03-27 11:26:10 +000043#include "SkTLazy.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000044#include "SkTLS.h"
commit-bot@chromium.org933e65d2014-03-20 20:00:24 +000045#include "SkTraceEvent.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000046
joshualitt74417822015-08-07 11:42:16 -070047#include "batches/GrBatch.h"
48
joshualitt5478d422014-11-14 16:00:38 -080049#include "effects/GrConfigConversionEffect.h"
50#include "effects/GrDashingEffect.h"
51#include "effects/GrSingleTextureEffect.h"
52
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000053#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
robertphillipsea461502015-05-26 11:38:03 -070054#define RETURN_IF_ABANDONED if (fDrawingMgr.abandoned()) { return; }
55#define RETURN_FALSE_IF_ABANDONED if (fDrawingMgr.abandoned()) { return false; }
halcanary96fcdcc2015-08-27 07:41:13 -070056#define RETURN_NULL_IF_ABANDONED if (fDrawingMgr.abandoned()) { return nullptr; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000057
bsalomonf21dab92014-11-13 13:33:28 -080058
robertphillipsea461502015-05-26 11:38:03 -070059////////////////////////////////////////////////////////////////////////////////
60
61void GrContext::DrawingMgr::init(GrContext* context) {
robertphillips2334fb62015-06-17 05:43:33 -070062 fContext = context;
bsalomonb3b9aec2015-09-10 11:16:35 -070063 fDrawTarget = new GrDrawTarget(context->getGpu(), context->resourceProvider());
robertphillips2334fb62015-06-17 05:43:33 -070064}
bsalomon781d5822015-06-16 15:03:11 -070065
robertphillips2334fb62015-06-17 05:43:33 -070066void GrContext::DrawingMgr::cleanup() {
67 SkSafeSetNull(fDrawTarget);
68 for (int i = 0; i < kNumPixelGeometries; ++i) {
69 SkSafeSetNull(fDrawContext[i][0]);
70 SkSafeSetNull(fDrawContext[i][1]);
71 }
robertphillipsea461502015-05-26 11:38:03 -070072}
73
74GrContext::DrawingMgr::~DrawingMgr() {
robertphillips2334fb62015-06-17 05:43:33 -070075 this->cleanup();
robertphillipsea461502015-05-26 11:38:03 -070076}
77
78void GrContext::DrawingMgr::abandon() {
79 SkSafeSetNull(fDrawTarget);
robertphillips2334fb62015-06-17 05:43:33 -070080 for (int i = 0; i < kNumPixelGeometries; ++i) {
81 for (int j = 0; j < kNumDFTOptions; ++j) {
82 if (fDrawContext[i][j]) {
83 SkSafeSetNull(fDrawContext[i][j]->fDrawTarget);
84 SkSafeSetNull(fDrawContext[i][j]);
85 }
86 }
87 }
robertphillipsea461502015-05-26 11:38:03 -070088}
89
90void GrContext::DrawingMgr::purgeResources() {
91 if (fDrawTarget) {
92 fDrawTarget->purgeResources();
bsalomonf21dab92014-11-13 13:33:28 -080093 }
robertphillipsea461502015-05-26 11:38:03 -070094}
bsalomonf21dab92014-11-13 13:33:28 -080095
robertphillipsea461502015-05-26 11:38:03 -070096void GrContext::DrawingMgr::reset() {
97 if (fDrawTarget) {
98 fDrawTarget->reset();
99 }
100}
101
102void GrContext::DrawingMgr::flush() {
103 if (fDrawTarget) {
104 fDrawTarget->flush();
105 }
106}
107
ericrk0a5fa482015-09-15 14:16:10 -0700108GrDrawContext* GrContext::DrawingMgr::drawContext(const SkSurfaceProps* surfaceProps) {
robertphillipsea461502015-05-26 11:38:03 -0700109 if (this->abandoned()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700110 return nullptr;
robertphillipsea461502015-05-26 11:38:03 -0700111 }
robertphillips2334fb62015-06-17 05:43:33 -0700112
robertphillipsfcf78292015-06-19 11:49:52 -0700113 const SkSurfaceProps props(SkSurfacePropsCopyOrDefault(surfaceProps));
114
115 SkASSERT(props.pixelGeometry() < kNumPixelGeometries);
bsalomonafcd7cd2015-08-31 12:39:41 -0700116 if (!fDrawContext[props.pixelGeometry()][props.isUseDeviceIndependentFonts()]) {
117 fDrawContext[props.pixelGeometry()][props.isUseDeviceIndependentFonts()] =
halcanary385fe4d2015-08-26 13:07:48 -0700118 new GrDrawContext(fContext, fDrawTarget, props);
robertphillips2334fb62015-06-17 05:43:33 -0700119 }
120
robertphillipsc9a37062015-09-01 08:34:28 -0700121 // For now, everyone gets a faux creation ref
ericrk0a5fa482015-09-15 14:16:10 -0700122 return SkRef(fDrawContext[props.pixelGeometry()][props.isUseDeviceIndependentFonts()]);
robertphillipsea461502015-05-26 11:38:03 -0700123}
124
125////////////////////////////////////////////////////////////////////////////////
126
bsalomonf21dab92014-11-13 13:33:28 -0800127
bsalomon682c2692015-05-22 14:01:46 -0700128GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext) {
129 GrContextOptions defaultOptions;
130 return Create(backend, backendContext, defaultOptions);
131}
bsalomonf28cff72015-05-22 12:25:41 -0700132
bsalomon682c2692015-05-22 14:01:46 -0700133GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
134 const GrContextOptions& options) {
halcanary385fe4d2015-08-26 13:07:48 -0700135 GrContext* context = new GrContext;
bsalomon682c2692015-05-22 14:01:46 -0700136
137 if (context->init(backend, backendContext, options)) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000138 return context;
139 } else {
140 context->unref();
halcanary96fcdcc2015-08-27 07:41:13 -0700141 return nullptr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000142 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000143}
144
joshualitt0acd0d32015-05-07 08:23:19 -0700145static int32_t gNextID = 1;
146static int32_t next_id() {
147 int32_t id;
148 do {
149 id = sk_atomic_inc(&gNextID);
150 } while (id == SK_InvalidGenID);
151 return id;
152}
153
bsalomon682c2692015-05-22 14:01:46 -0700154GrContext::GrContext() : fUniqueID(next_id()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700155 fGpu = nullptr;
156 fCaps = nullptr;
157 fResourceCache = nullptr;
158 fResourceProvider = nullptr;
159 fPathRendererChain = nullptr;
160 fSoftwarePathRenderer = nullptr;
161 fBatchFontCache = nullptr;
bsalomonf21dab92014-11-13 13:33:28 -0800162 fFlushToReduceCacheSize = false;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000163}
164
bsalomon682c2692015-05-22 14:01:46 -0700165bool GrContext::init(GrBackend backend, GrBackendContext backendContext,
166 const GrContextOptions& options) {
robertphillipsea461502015-05-26 11:38:03 -0700167 SkASSERT(!fGpu);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000168
bsalomon682c2692015-05-22 14:01:46 -0700169 fGpu = GrGpu::Create(backend, backendContext, options, this);
robertphillipsea461502015-05-26 11:38:03 -0700170 if (!fGpu) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000171 return false;
172 }
bsalomon33435572014-11-05 14:47:41 -0800173 this->initCommon();
174 return true;
175}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000176
bsalomon33435572014-11-05 14:47:41 -0800177void GrContext::initCommon() {
bsalomon76228632015-05-29 08:02:10 -0700178 fCaps = SkRef(fGpu->caps());
halcanary385fe4d2015-08-26 13:07:48 -0700179 fResourceCache = new GrResourceCache(fCaps);
bsalomon0ea80f42015-02-11 10:49:59 -0800180 fResourceCache->setOverBudgetCallback(OverBudgetCB, this);
halcanary385fe4d2015-08-26 13:07:48 -0700181 fResourceProvider = new GrResourceProvider(fGpu, fResourceCache);
commit-bot@chromium.org1836d332013-07-16 22:55:03 +0000182
halcanary385fe4d2015-08-26 13:07:48 -0700183 fLayerCache.reset(new GrLayerCache(this));
robertphillips@google.come930a072014-04-03 00:34:27 +0000184
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000185 fDidTestPMConversions = false;
186
robertphillipsea461502015-05-26 11:38:03 -0700187 fDrawingMgr.init(this);
joshualitt7c3a2f82015-03-31 13:32:05 -0700188
189 // GrBatchFontCache will eventually replace GrFontCache
halcanary385fe4d2015-08-26 13:07:48 -0700190 fBatchFontCache = new GrBatchFontCache(this);
joshualittb7133be2015-04-08 09:08:31 -0700191
halcanary385fe4d2015-08-26 13:07:48 -0700192 fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB, this));
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000193}
194
bsalomon@google.com27847de2011-02-22 20:59:41 +0000195GrContext::~GrContext() {
robertphillipsea461502015-05-26 11:38:03 -0700196 if (!fGpu) {
bsalomon76228632015-05-29 08:02:10 -0700197 SkASSERT(!fCaps);
bsalomon@google.com733c0622013-04-24 17:59:32 +0000198 return;
199 }
200
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000201 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000202
robertphillips2334fb62015-06-17 05:43:33 -0700203 fDrawingMgr.cleanup();
204
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000205 for (int i = 0; i < fCleanUpData.count(); ++i) {
206 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
207 }
208
halcanary385fe4d2015-08-26 13:07:48 -0700209 delete fResourceProvider;
210 delete fResourceCache;
211 delete fBatchFontCache;
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000212
bsalomon@google.com205d4602011-04-25 12:43:45 +0000213 fGpu->unref();
bsalomon76228632015-05-29 08:02:10 -0700214 fCaps->unref();
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000215 SkSafeUnref(fPathRendererChain);
216 SkSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000217}
218
bsalomon2354f842014-07-28 13:48:36 -0700219void GrContext::abandonContext() {
bsalomond309e7a2015-04-30 14:18:54 -0700220 fResourceProvider->abandon();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000221 // abandon first to so destructors
222 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800223 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700224
robertphillipse3371302014-09-17 06:01:06 -0700225 fGpu->contextAbandoned();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000226
bsalomon@google.com30085192011-08-19 15:42:31 +0000227 // a path renderer may be holding onto resources that
228 // are now unusable
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000229 SkSafeSetNull(fPathRendererChain);
230 SkSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000231
robertphillipsea461502015-05-26 11:38:03 -0700232 fDrawingMgr.abandon();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000233
joshualitt7c3a2f82015-03-31 13:32:05 -0700234 fBatchFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000235 fLayerCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700236 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000237}
238
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000239void GrContext::resetContext(uint32_t state) {
240 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000241}
242
243void GrContext::freeGpuResources() {
244 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000245
robertphillipsea461502015-05-26 11:38:03 -0700246 fDrawingMgr.purgeResources();
robertphillips@google.comff175842012-05-14 19:31:39 +0000247
joshualitt7c3a2f82015-03-31 13:32:05 -0700248 fBatchFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000249 fLayerCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000250 // a path renderer may be holding onto resources
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000251 SkSafeSetNull(fPathRendererChain);
252 SkSafeSetNull(fSoftwarePathRenderer);
bsalomon3033b9f2015-04-13 11:09:56 -0700253
254 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000255}
256
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000257void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
bsalomon71cb0c22014-11-14 12:10:14 -0800258 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800259 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800260 }
261 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800262 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800263 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000264}
265
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000266////////////////////////////////////////////////////////////////////////////////
267
bsalomon71cb0c22014-11-14 12:10:14 -0800268void GrContext::OverBudgetCB(void* data) {
bsalomon66a450f2014-11-13 13:19:10 -0800269 SkASSERT(data);
bsalomonf21dab92014-11-13 13:33:28 -0800270
bsalomon66a450f2014-11-13 13:19:10 -0800271 GrContext* context = reinterpret_cast<GrContext*>(data);
bsalomonf21dab92014-11-13 13:33:28 -0800272
joshualittb542bae2015-07-28 09:58:39 -0700273 // Flush the GrBufferedDrawTarget to possibly free up some textures
bsalomonf21dab92014-11-13 13:33:28 -0800274 context->fFlushToReduceCacheSize = true;
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000275}
276
joshualitt0db6dfa2015-04-10 07:01:30 -0700277void GrContext::TextBlobCacheOverBudgetCB(void* data) {
278 SkASSERT(data);
279
280 // Unlike the GrResourceCache, TextBlobs are drawn at the SkGpuDevice level, therefore they
281 // cannot use fFlushTorReduceCacheSize because it uses AutoCheckFlush. The solution is to move
282 // drawText calls to below the GrContext level, but this is not trivial because they call
283 // drawPath on SkGpuDevice
284 GrContext* context = reinterpret_cast<GrContext*>(data);
285 context->flush();
286}
287
bsalomon@google.com27847de2011-02-22 20:59:41 +0000288////////////////////////////////////////////////////////////////////////////////
289
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000290void GrContext::flush(int flagsBitfield) {
robertphillipsea461502015-05-26 11:38:03 -0700291 RETURN_IF_ABANDONED
robertphillips@google.come7db8d62013-07-04 11:48:52 +0000292
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000293 if (kDiscard_FlushBit & flagsBitfield) {
robertphillipsea461502015-05-26 11:38:03 -0700294 fDrawingMgr.reset();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000295 } else {
robertphillipsea461502015-05-26 11:38:03 -0700296 fDrawingMgr.flush();
junov@google.com53a55842011-06-08 22:55:10 +0000297 }
bsalomon3f324322015-04-08 11:01:54 -0700298 fResourceCache->notifyFlushOccurred();
bsalomonf21dab92014-11-13 13:33:28 -0800299 fFlushToReduceCacheSize = false;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000300}
301
bsalomon81beccc2014-10-13 12:32:55 -0700302bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
303 const void* inPixels, size_t outRowBytes, void* outPixels) {
304 SkSrcPixelInfo srcPI;
halcanary96fcdcc2015-08-27 07:41:13 -0700305 if (!GrPixelConfig2ColorAndProfileType(srcConfig, &srcPI.fColorType, nullptr)) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000306 return false;
307 }
bsalomon81beccc2014-10-13 12:32:55 -0700308 srcPI.fAlphaType = kUnpremul_SkAlphaType;
309 srcPI.fPixels = inPixels;
310 srcPI.fRowBytes = inRowBytes;
311
312 SkDstPixelInfo dstPI;
313 dstPI.fColorType = srcPI.fColorType;
314 dstPI.fAlphaType = kPremul_SkAlphaType;
315 dstPI.fPixels = outPixels;
316 dstPI.fRowBytes = outRowBytes;
317
318 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000319}
320
bsalomon81beccc2014-10-13 12:32:55 -0700321bool GrContext::writeSurfacePixels(GrSurface* surface,
322 int left, int top, int width, int height,
323 GrPixelConfig srcConfig, const void* buffer, size_t rowBytes,
324 uint32_t pixelOpsFlags) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800325 RETURN_FALSE_IF_ABANDONED
bsalomon6c6f6582015-09-10 08:12:46 -0700326 ASSERT_OWNED_RESOURCE(surface);
327 SkASSERT(surface);
328
329 this->testPMConversionsIfNecessary(pixelOpsFlags);
bsalomon81beccc2014-10-13 12:32:55 -0700330
bsalomone8d21e82015-07-16 08:23:13 -0700331 // 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 -0700332 // necessary and because GrGpu::getWritePixelsInfo requires it.
bsalomone8d21e82015-07-16 08:23:13 -0700333 if (!GrSurfacePriv::AdjustWritePixelParams(surface->width(), surface->height(),
334 GrBytesPerPixel(srcConfig), &left, &top, &width,
335 &height, &buffer, &rowBytes)) {
336 return false;
337 }
338
bsalomonf0674512015-07-28 13:26:15 -0700339 bool applyPremulToSrc = false;
bsalomon81beccc2014-10-13 12:32:55 -0700340 if (kUnpremul_PixelOpsFlag & pixelOpsFlags) {
341 if (!GrPixelConfigIs8888(srcConfig)) {
342 return false;
343 }
bsalomonf0674512015-07-28 13:26:15 -0700344 applyPremulToSrc = true;
345 }
bsalomon636e8022015-07-29 06:08:46 -0700346
347 GrGpu::DrawPreference drawPreference = GrGpu::kNoDraw_DrawPreference;
348 // Don't prefer to draw for the conversion (and thereby access a texture from the cache) when
349 // we've already determined that there isn't a roundtrip preserving conversion processor pair.
350 if (applyPremulToSrc && !this->didFailPMUPMConversionTest()) {
351 drawPreference = GrGpu::kCallerPrefersDraw_DrawPreference;
352 }
353
bsalomonf0674512015-07-28 13:26:15 -0700354 GrGpu::WritePixelTempDrawInfo tempDrawInfo;
355 if (!fGpu->getWritePixelsInfo(surface, width, height, rowBytes, srcConfig, &drawPreference,
356 &tempDrawInfo)) {
357 return false;
358 }
359
360 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && surface->surfacePriv().hasPendingIO()) {
361 this->flush();
362 }
363
364 SkAutoTUnref<GrTexture> tempTexture;
365 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomoneae62002015-07-31 13:59:30 -0700366 tempTexture.reset(
367 this->textureProvider()->createApproxTexture(tempDrawInfo.fTempSurfaceDesc));
bsalomonf0674512015-07-28 13:26:15 -0700368 if (!tempTexture && GrGpu::kRequireDraw_DrawPreference == drawPreference) {
369 return false;
370 }
371 }
372
373 // temp buffer for doing sw premul conversion, if needed.
374 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
375 if (tempTexture) {
376 SkAutoTUnref<const GrFragmentProcessor> fp;
377 SkMatrix textureMatrix;
378 textureMatrix.setIDiv(tempTexture->width(), tempTexture->height());
379 GrPaint paint;
380 if (applyPremulToSrc) {
381 fp.reset(this->createUPMToPMEffect(paint.getProcessorDataManager(), tempTexture,
382 tempDrawInfo.fSwapRAndB, textureMatrix));
383 // If premultiplying was the only reason for the draw, fall back to a straight write.
384 if (!fp) {
385 if (GrGpu::kCallerPrefersDraw_DrawPreference == drawPreference) {
halcanary96fcdcc2015-08-27 07:41:13 -0700386 tempTexture.reset(nullptr);
bsalomonf0674512015-07-28 13:26:15 -0700387 }
388 } else {
389 applyPremulToSrc = false;
390 }
391 }
392 if (tempTexture) {
393 if (!fp) {
394 fp.reset(GrConfigConversionEffect::Create(
395 paint.getProcessorDataManager(), tempTexture, tempDrawInfo.fSwapRAndB,
396 GrConfigConversionEffect::kNone_PMConversion, textureMatrix));
397 if (!fp) {
398 return false;
399 }
400 }
401 GrRenderTarget* renderTarget = surface->asRenderTarget();
402 SkASSERT(renderTarget);
403 if (tempTexture->surfacePriv().hasPendingIO()) {
404 this->flush();
405 }
406 if (applyPremulToSrc) {
407 size_t tmpRowBytes = 4 * width;
408 tmpPixels.reset(width * height);
409 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
410 tmpPixels.get())) {
411 return false;
412 }
413 rowBytes = tmpRowBytes;
414 buffer = tmpPixels.get();
415 applyPremulToSrc = false;
416 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700417 if (!fGpu->writePixels(tempTexture, 0, 0, width, height,
418 tempDrawInfo.fTempSurfaceDesc.fConfig, buffer,
419 rowBytes)) {
bsalomonf0674512015-07-28 13:26:15 -0700420 return false;
421 }
422 SkMatrix matrix;
423 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
robertphillipsc9a37062015-09-01 08:34:28 -0700424 SkAutoTUnref<GrDrawContext> drawContext(this->drawContext());
bsalomonf0674512015-07-28 13:26:15 -0700425 if (!drawContext) {
426 return false;
427 }
bsalomonac856c92015-08-27 06:30:17 -0700428 paint.addColorFragmentProcessor(fp);
bsalomonf0674512015-07-28 13:26:15 -0700429 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
halcanary96fcdcc2015-08-27 07:41:13 -0700430 drawContext->drawRect(renderTarget, GrClip::WideOpen(), paint, matrix, rect, nullptr);
bsalomonf0674512015-07-28 13:26:15 -0700431
432 if (kFlushWrites_PixelOp & pixelOpsFlags) {
433 this->flushSurfaceWrites(surface);
434 }
435 }
436 }
437 if (!tempTexture) {
bsalomonf0674512015-07-28 13:26:15 -0700438 if (applyPremulToSrc) {
bsalomon81beccc2014-10-13 12:32:55 -0700439 size_t tmpRowBytes = 4 * width;
440 tmpPixels.reset(width * height);
441 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
442 tmpPixels.get())) {
443 return false;
444 }
445 rowBytes = tmpRowBytes;
446 buffer = tmpPixels.get();
bsalomonf0674512015-07-28 13:26:15 -0700447 applyPremulToSrc = false;
bsalomon81beccc2014-10-13 12:32:55 -0700448 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700449 return fGpu->writePixels(surface, left, top, width, height, srcConfig, buffer, rowBytes);
bsalomon81beccc2014-10-13 12:32:55 -0700450 }
bsalomon81beccc2014-10-13 12:32:55 -0700451 return true;
452}
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000453
bsalomone8d21e82015-07-16 08:23:13 -0700454bool GrContext::readSurfacePixels(GrSurface* src,
455 int left, int top, int width, int height,
456 GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
457 uint32_t flags) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800458 RETURN_FALSE_IF_ABANDONED
bsalomone8d21e82015-07-16 08:23:13 -0700459 ASSERT_OWNED_RESOURCE(src);
460 SkASSERT(src);
bsalomon32ab2602015-09-09 18:57:49 -0700461
bsalomon6c6f6582015-09-10 08:12:46 -0700462 this->testPMConversionsIfNecessary(flags);
463 SkAutoMutexAcquire ama(fReadPixelsMutex);
464
bsalomone8d21e82015-07-16 08:23:13 -0700465 // Adjust the params so that if we wind up using an intermediate surface we've already done
466 // all the trimming and the temporary can be the min size required.
467 if (!GrSurfacePriv::AdjustReadPixelParams(src->width(), src->height(),
468 GrBytesPerPixel(dstConfig), &left,
469 &top, &width, &height, &buffer, &rowBytes)) {
470 return false;
471 }
472
473 if (!(kDontFlush_PixelOpsFlag & flags) && src->surfacePriv().hasPendingWrite()) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000474 this->flush();
475 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000476
bsalomone8d21e82015-07-16 08:23:13 -0700477 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.com9c680582013-02-06 18:17:50 +0000478 if (unpremul && !GrPixelConfigIs8888(dstConfig)) {
bsalomon39826022015-07-23 08:07:21 -0700479 // The unpremul flag is only allowed for 8888 configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000480 return false;
481 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000482
bsalomon636e8022015-07-29 06:08:46 -0700483 GrGpu::DrawPreference drawPreference = GrGpu::kNoDraw_DrawPreference;
484 // Don't prefer to draw for the conversion (and thereby access a texture from the cache) when
485 // we've already determined that there isn't a roundtrip preserving conversion processor pair.
486 if (unpremul && !this->didFailPMUPMConversionTest()) {
487 drawPreference = GrGpu::kCallerPrefersDraw_DrawPreference;
488 }
489
bsalomon39826022015-07-23 08:07:21 -0700490 GrGpu::ReadPixelTempDrawInfo tempDrawInfo;
491 if (!fGpu->getReadPixelsInfo(src, width, height, rowBytes, dstConfig, &drawPreference,
492 &tempDrawInfo)) {
493 return false;
494 }
bsalomon191bcc02014-11-14 11:31:13 -0800495
bsalomon6cb3cbe2015-07-30 07:34:27 -0700496 SkAutoTUnref<GrSurface> surfaceToRead(SkRef(src));
bsalomon39826022015-07-23 08:07:21 -0700497 bool didTempDraw = false;
498 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomon39826022015-07-23 08:07:21 -0700499 if (tempDrawInfo.fUseExactScratch) {
500 // We only respect this when the entire src is being read. Otherwise we can trigger too
501 // many odd ball texture sizes and trash the cache.
bsalomoneae62002015-07-31 13:59:30 -0700502 if (width != src->width() || height != src->height()) {
503 tempDrawInfo.fUseExactScratch = false;
bsalomon39826022015-07-23 08:07:21 -0700504 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000505 }
bsalomon39826022015-07-23 08:07:21 -0700506 SkAutoTUnref<GrTexture> temp;
bsalomoneae62002015-07-31 13:59:30 -0700507 if (tempDrawInfo.fUseExactScratch) {
508 temp.reset(this->textureProvider()->createTexture(tempDrawInfo.fTempSurfaceDesc, true));
509 } else {
510 temp.reset(this->textureProvider()->createApproxTexture(tempDrawInfo.fTempSurfaceDesc));
511 }
bsalomon39826022015-07-23 08:07:21 -0700512 if (temp) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000513 SkMatrix textureMatrix;
bsalomon39826022015-07-23 08:07:21 -0700514 textureMatrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000515 textureMatrix.postIDiv(src->width(), src->height());
joshualitt5f10b5c2015-07-09 10:24:35 -0700516 GrPaint paint;
joshualittb0a8a372014-09-23 09:50:21 -0700517 SkAutoTUnref<const GrFragmentProcessor> fp;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000518 if (unpremul) {
bsalomon39826022015-07-23 08:07:21 -0700519 fp.reset(this->createPMToUPMEffect(
520 paint.getProcessorDataManager(), src->asTexture(), tempDrawInfo.fSwapRAndB,
521 textureMatrix));
joshualittb0a8a372014-09-23 09:50:21 -0700522 if (fp) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000523 unpremul = false; // we no longer need to do this on CPU after the read back.
bsalomon39826022015-07-23 08:07:21 -0700524 } else if (GrGpu::kCallerPrefersDraw_DrawPreference == drawPreference) {
525 // We only wanted to do the draw in order to perform the unpremul so don't
526 // bother.
halcanary96fcdcc2015-08-27 07:41:13 -0700527 temp.reset(nullptr);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000528 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000529 }
bsalomon39826022015-07-23 08:07:21 -0700530 if (!fp && temp) {
531 fp.reset(GrConfigConversionEffect::Create(
532 paint.getProcessorDataManager(), src->asTexture(), tempDrawInfo.fSwapRAndB,
533 GrConfigConversionEffect::kNone_PMConversion, textureMatrix));
534 }
535 if (fp) {
bsalomonac856c92015-08-27 06:30:17 -0700536 paint.addColorFragmentProcessor(fp);
bsalomon39826022015-07-23 08:07:21 -0700537 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
robertphillipsc9a37062015-09-01 08:34:28 -0700538 SkAutoTUnref<GrDrawContext> drawContext(this->drawContext());
bsalomon39826022015-07-23 08:07:21 -0700539 drawContext->drawRect(temp->asRenderTarget(), GrClip::WideOpen(), paint,
halcanary96fcdcc2015-08-27 07:41:13 -0700540 SkMatrix::I(), rect, nullptr);
bsalomon6cb3cbe2015-07-30 07:34:27 -0700541 surfaceToRead.reset(SkRef(temp.get()));
bsalomon39826022015-07-23 08:07:21 -0700542 left = 0;
543 top = 0;
544 didTempDraw = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000545 }
bsalomon@google.com0342a852012-08-20 19:22:38 +0000546 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000547 }
joshualitt5c55fef2014-10-31 14:04:35 -0700548
bsalomon39826022015-07-23 08:07:21 -0700549 if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000550 return false;
551 }
bsalomon39826022015-07-23 08:07:21 -0700552 GrPixelConfig configToRead = dstConfig;
553 if (didTempDraw) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700554 this->flushSurfaceWrites(surfaceToRead);
bsalomon39826022015-07-23 08:07:21 -0700555 // We swapped R and B while doing the temp draw. Swap back on the read.
556 if (tempDrawInfo.fSwapRAndB) {
557 configToRead = GrPixelConfigSwapRAndB(dstConfig);
558 }
559 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700560 if (!fGpu->readPixels(surfaceToRead, left, top, width, height, configToRead, buffer,
561 rowBytes)) {
bsalomon39826022015-07-23 08:07:21 -0700562 return false;
563 }
564
565 // Perform umpremul conversion if we weren't able to perform it as a draw.
566 if (unpremul) {
reed@google.com7111d462014-03-25 16:20:24 +0000567 SkDstPixelInfo dstPI;
halcanary96fcdcc2015-08-27 07:41:13 -0700568 if (!GrPixelConfig2ColorAndProfileType(dstConfig, &dstPI.fColorType, nullptr)) {
reed@google.com7111d462014-03-25 16:20:24 +0000569 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000570 }
reed@google.com7111d462014-03-25 16:20:24 +0000571 dstPI.fAlphaType = kUnpremul_SkAlphaType;
572 dstPI.fPixels = buffer;
573 dstPI.fRowBytes = rowBytes;
574
575 SkSrcPixelInfo srcPI;
bsalomon39826022015-07-23 08:07:21 -0700576 srcPI.fColorType = dstPI.fColorType;
reed@google.com7111d462014-03-25 16:20:24 +0000577 srcPI.fAlphaType = kPremul_SkAlphaType;
578 srcPI.fPixels = buffer;
579 srcPI.fRowBytes = rowBytes;
580
581 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000582 }
583 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000584}
585
bsalomonc49e8682015-06-30 11:37:35 -0700586void GrContext::prepareSurfaceForExternalIO(GrSurface* surface) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800587 RETURN_IF_ABANDONED
bsalomon87a94eb2014-11-03 14:28:32 -0800588 SkASSERT(surface);
589 ASSERT_OWNED_RESOURCE(surface);
590 if (surface->surfacePriv().hasPendingIO()) {
591 this->flush();
592 }
593 GrRenderTarget* rt = surface->asRenderTarget();
594 if (fGpu && rt) {
595 fGpu->resolveRenderTarget(rt);
bsalomon41ebbdd2014-08-04 08:31:39 -0700596 }
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000597}
598
bsalomonf80bfed2014-10-07 05:56:02 -0700599void GrContext::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
600 const SkIPoint& dstPoint, uint32_t pixelOpsFlags) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800601 RETURN_IF_ABANDONED
robertphillipsea461502015-05-26 11:38:03 -0700602 if (!src || !dst) {
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000603 return;
604 }
bsalomone3d4bf22014-09-23 09:15:03 -0700605 ASSERT_OWNED_RESOURCE(src);
junov2bb52102014-09-29 10:18:59 -0700606 ASSERT_OWNED_RESOURCE(dst);
Brian Salomon34a98952014-09-24 11:41:24 -0400607
bsalomonf80bfed2014-10-07 05:56:02 -0700608 // Since we're going to the draw target and not GPU, no need to check kNoFlush
609 // here.
robertphillipsea461502015-05-26 11:38:03 -0700610 if (!dst->asRenderTarget()) {
junov96c118e2014-09-26 13:09:47 -0700611 return;
612 }
robertphillipsea461502015-05-26 11:38:03 -0700613
robertphillipsc9a37062015-09-01 08:34:28 -0700614 SkAutoTUnref<GrDrawContext> drawContext(this->drawContext());
robertphillipsea461502015-05-26 11:38:03 -0700615 if (!drawContext) {
616 return;
617 }
618
619 drawContext->copySurface(dst->asRenderTarget(), src, srcRect, dstPoint);
bsalomonf80bfed2014-10-07 05:56:02 -0700620
621 if (kFlushWrites_PixelOp & pixelOpsFlags) {
622 this->flush();
623 }
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000624}
625
bsalomonf80bfed2014-10-07 05:56:02 -0700626void GrContext::flushSurfaceWrites(GrSurface* surface) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800627 RETURN_IF_ABANDONED
bsalomonf80bfed2014-10-07 05:56:02 -0700628 if (surface->surfacePriv().hasPendingWrite()) {
629 this->flush();
630 }
631}
632
robertphillips@google.com72176b22012-05-23 13:19:12 +0000633/*
634 * This method finds a path renderer that can draw the specified path on
635 * the provided target.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000636 * Due to its expense, the software path renderer has split out so it can
robertphillips@google.com72176b22012-05-23 13:19:12 +0000637 * can be individually allowed/disallowed via the "allowSW" boolean.
638 */
joshualitt9853cce2014-11-17 14:22:48 -0800639GrPathRenderer* GrContext::getPathRenderer(const GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -0800640 const GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800641 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800642 const SkPath& path,
kkinnunen18996512015-04-26 23:18:49 -0700643 const GrStrokeInfo& stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000644 bool allowSW,
645 GrPathRendererChain::DrawType drawType,
646 GrPathRendererChain::StencilSupport* stencilSupport) {
647
robertphillipsea461502015-05-26 11:38:03 -0700648 if (!fPathRendererChain) {
halcanary385fe4d2015-08-26 13:07:48 -0700649 fPathRendererChain = new GrPathRendererChain(this);
bsalomon@google.com30085192011-08-19 15:42:31 +0000650 }
robertphillips@google.com72176b22012-05-23 13:19:12 +0000651
joshualitt9853cce2014-11-17 14:22:48 -0800652 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(target,
egdaniel8dd688b2015-01-22 10:16:09 -0800653 pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800654 viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800655 path,
sugoi@google.com12b4e272012-12-06 20:13:11 +0000656 stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000657 drawType,
658 stencilSupport);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000659
robertphillipsea461502015-05-26 11:38:03 -0700660 if (!pr && allowSW) {
661 if (!fSoftwarePathRenderer) {
halcanary385fe4d2015-08-26 13:07:48 -0700662 fSoftwarePathRenderer = new GrSoftwarePathRenderer(this);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000663 }
robertphillips@google.com72176b22012-05-23 13:19:12 +0000664 pr = fSoftwarePathRenderer;
665 }
666
667 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +0000668}
669
bsalomon@google.com27847de2011-02-22 20:59:41 +0000670////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000671int GrContext::getRecommendedSampleCount(GrPixelConfig config,
672 SkScalar dpi) const {
bsalomon76228632015-05-29 08:02:10 -0700673 if (!this->caps()->isConfigRenderable(config, true)) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000674 return 0;
675 }
676 int chosenSampleCount = 0;
jvanverthe9c0fc62015-04-29 11:18:05 -0700677 if (fGpu->caps()->shaderCaps()->pathRenderingSupport()) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000678 if (dpi >= 250.0f) {
679 chosenSampleCount = 4;
680 } else {
681 chosenSampleCount = 16;
682 }
683 }
684 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ?
685 chosenSampleCount : 0;
686}
687
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000688namespace {
689void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
690 GrConfigConversionEffect::PMConversion pmToUPM;
691 GrConfigConversionEffect::PMConversion upmToPM;
692 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
693 *pmToUPMValue = pmToUPM;
694 *upmToPMValue = upmToPM;
695}
696}
697
bsalomon6c6f6582015-09-10 08:12:46 -0700698void GrContext::testPMConversionsIfNecessary(uint32_t flags) {
699 if (SkToBool(kUnpremul_PixelOpsFlag & flags)) {
700 SkAutoMutexAcquire ama(fTestPMConversionsMutex);
701 if (!fDidTestPMConversions) {
702 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
703 fDidTestPMConversions = true;
704 }
705 }
706}
707
joshualitt5f10b5c2015-07-09 10:24:35 -0700708const GrFragmentProcessor* GrContext::createPMToUPMEffect(GrProcessorDataManager* procDataManager,
709 GrTexture* texture,
joshualittb0a8a372014-09-23 09:50:21 -0700710 bool swapRAndB,
bsalomon6c6f6582015-09-10 08:12:46 -0700711 const SkMatrix& matrix) const {
712 // We should have already called this->testPMConversionsIfNecessary().
713 SkASSERT(fDidTestPMConversions);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000714 GrConfigConversionEffect::PMConversion pmToUPM =
715 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
716 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
joshualitt5f10b5c2015-07-09 10:24:35 -0700717 return GrConfigConversionEffect::Create(procDataManager, texture, swapRAndB, pmToUPM,
718 matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000719 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700720 return nullptr;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000721 }
722}
723
joshualitt5f10b5c2015-07-09 10:24:35 -0700724const GrFragmentProcessor* GrContext::createUPMToPMEffect(GrProcessorDataManager* procDataManager,
725 GrTexture* texture,
joshualittb0a8a372014-09-23 09:50:21 -0700726 bool swapRAndB,
bsalomon6c6f6582015-09-10 08:12:46 -0700727 const SkMatrix& matrix) const {
728 // We should have already called this->testPMConversionsIfNecessary().
729 SkASSERT(fDidTestPMConversions);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000730 GrConfigConversionEffect::PMConversion upmToPM =
731 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
732 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
joshualitt5f10b5c2015-07-09 10:24:35 -0700733 return GrConfigConversionEffect::Create(procDataManager, texture, swapRAndB, upmToPM,
734 matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000735 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700736 return nullptr;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000737 }
738}
739
bsalomon636e8022015-07-29 06:08:46 -0700740bool GrContext::didFailPMUPMConversionTest() const {
bsalomon6c6f6582015-09-10 08:12:46 -0700741 // We should have already called this->testPMConversionsIfNecessary().
742 SkASSERT(fDidTestPMConversions);
bsalomon636e8022015-07-29 06:08:46 -0700743 // The PM<->UPM tests fail or succeed together so we only need to check one.
bsalomon6c6f6582015-09-10 08:12:46 -0700744 return GrConfigConversionEffect::kNone_PMConversion == fPMToUPMConversion;
bsalomon636e8022015-07-29 06:08:46 -0700745}
746
bsalomon37f9a262015-02-02 13:00:10 -0800747//////////////////////////////////////////////////////////////////////////////
748
749void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
750 if (maxTextures) {
bsalomon0ea80f42015-02-11 10:49:59 -0800751 *maxTextures = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -0800752 }
753 if (maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800754 *maxTextureBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -0800755 }
756}
757
758void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800759 fResourceCache->setLimits(maxTextures, maxTextureBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800760}
761
ericrk0a5fa482015-09-15 14:16:10 -0700762//////////////////////////////////////////////////////////////////////////////
763
764void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
765 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
766}