blob: 82550d0d297d3c8be9ab2d72ba6c4c25790ca948 [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"
robertphillips2d70dcb2015-10-06 07:38:23 -070031#include "GrStencilAndCoverTextContext.h"
egdanield58a0ba2014-06-11 10:30:05 -070032#include "GrStrokeInfo.h"
bsalomonafbf2d62014-09-30 12:18:44 -070033#include "GrSurfacePriv.h"
joshualittb7133be2015-04-08 09:08:31 -070034#include "GrTextBlobCache.h"
bsalomonafbf2d62014-09-30 12:18:44 -070035#include "GrTexturePriv.h"
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +000036#include "GrTracing.h"
bsalomoncb8979d2015-05-05 09:51:38 -070037#include "GrVertices.h"
egdanield58a0ba2014-06-11 10:30:05 -070038#include "SkDashPathPriv.h"
bsalomon81beccc2014-10-13 12:32:55 -070039#include "SkConfig8888.h"
bsalomonf276ac52015-10-09 13:36:42 -070040#include "SkGrPriv.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000041#include "SkRRect.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000042#include "SkStrokeRec.h"
robertphillipsfcf78292015-06-19 11:49:52 -070043#include "SkSurfacePriv.h"
commit-bot@chromium.orgdcb8ef92014-03-27 11:26:10 +000044#include "SkTLazy.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000045#include "SkTLS.h"
commit-bot@chromium.org933e65d2014-03-20 20:00:24 +000046#include "SkTraceEvent.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000047
robertphillips2d70dcb2015-10-06 07:38:23 -070048
joshualitt74417822015-08-07 11:42:16 -070049#include "batches/GrBatch.h"
50
joshualitt5478d422014-11-14 16:00:38 -080051#include "effects/GrConfigConversionEffect.h"
52#include "effects/GrDashingEffect.h"
53#include "effects/GrSingleTextureEffect.h"
54
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000055#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
robertphillipsea461502015-05-26 11:38:03 -070056#define RETURN_IF_ABANDONED if (fDrawingMgr.abandoned()) { return; }
57#define RETURN_FALSE_IF_ABANDONED if (fDrawingMgr.abandoned()) { return false; }
halcanary96fcdcc2015-08-27 07:41:13 -070058#define RETURN_NULL_IF_ABANDONED if (fDrawingMgr.abandoned()) { return nullptr; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000059
bsalomonf21dab92014-11-13 13:33:28 -080060
robertphillipsea461502015-05-26 11:38:03 -070061////////////////////////////////////////////////////////////////////////////////
62
63void GrContext::DrawingMgr::init(GrContext* context) {
robertphillips2334fb62015-06-17 05:43:33 -070064 fContext = context;
bsalomonb3b9aec2015-09-10 11:16:35 -070065 fDrawTarget = new GrDrawTarget(context->getGpu(), context->resourceProvider());
robertphillips2334fb62015-06-17 05:43:33 -070066}
bsalomon781d5822015-06-16 15:03:11 -070067
robertphillips2334fb62015-06-17 05:43:33 -070068void GrContext::DrawingMgr::cleanup() {
69 SkSafeSetNull(fDrawTarget);
robertphillips2d70dcb2015-10-06 07:38:23 -070070 delete fNVPRTextContext;
71 fNVPRTextContext = nullptr;
robertphillips2334fb62015-06-17 05:43:33 -070072 for (int i = 0; i < kNumPixelGeometries; ++i) {
robertphillips2d70dcb2015-10-06 07:38:23 -070073 delete fTextContexts[i][0];
74 fTextContexts[i][0] = nullptr;
75 delete fTextContexts[i][1];
76 fTextContexts[i][1] = nullptr;
robertphillips2334fb62015-06-17 05:43:33 -070077 }
robertphillipsea461502015-05-26 11:38:03 -070078}
79
80GrContext::DrawingMgr::~DrawingMgr() {
robertphillips2334fb62015-06-17 05:43:33 -070081 this->cleanup();
robertphillipsea461502015-05-26 11:38:03 -070082}
83
84void GrContext::DrawingMgr::abandon() {
robertphillips2d70dcb2015-10-06 07:38:23 -070085 this->cleanup();
robertphillipsea461502015-05-26 11:38:03 -070086}
87
robertphillipsea461502015-05-26 11:38:03 -070088void GrContext::DrawingMgr::reset() {
89 if (fDrawTarget) {
90 fDrawTarget->reset();
91 }
92}
93
94void GrContext::DrawingMgr::flush() {
95 if (fDrawTarget) {
96 fDrawTarget->flush();
97 }
98}
99
robertphillips2d70dcb2015-10-06 07:38:23 -0700100GrTextContext* GrContext::DrawingMgr::textContext(const SkSurfaceProps& props,
101 GrRenderTarget* rt) {
102 if (this->abandoned()) {
103 return nullptr;
104 }
105
106 SkASSERT(props.pixelGeometry() < kNumPixelGeometries);
107 bool useDIF = props.isUseDeviceIndependentFonts();
108
109 if (useDIF && fContext->caps()->shaderCaps()->pathRenderingSupport() &&
110 rt->isStencilBufferMultisampled()) {
111 GrStencilAttachment* sb = fContext->resourceProvider()->attachStencilAttachment(rt);
112 if (sb) {
113 if (!fNVPRTextContext) {
114 fNVPRTextContext = GrStencilAndCoverTextContext::Create(fContext, props);
115 }
116
117 return fNVPRTextContext;
118 }
119 }
120
121 if (!fTextContexts[props.pixelGeometry()][useDIF]) {
122 fTextContexts[props.pixelGeometry()][useDIF] = GrAtlasTextContext::Create(fContext, props);
123 }
124
125 return fTextContexts[props.pixelGeometry()][useDIF];
126}
127
robertphillips2e1e51f2015-10-15 08:01:48 -0700128GrDrawContext* GrContext::DrawingMgr::drawContext(GrRenderTarget* rt,
129 const SkSurfaceProps* surfaceProps) {
robertphillipsea461502015-05-26 11:38:03 -0700130 if (this->abandoned()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700131 return nullptr;
robertphillipsea461502015-05-26 11:38:03 -0700132 }
robertphillips2334fb62015-06-17 05:43:33 -0700133
robertphillips2e1e51f2015-10-15 08:01:48 -0700134 return new GrDrawContext(fContext, rt, fDrawTarget, surfaceProps);
robertphillipsea461502015-05-26 11:38:03 -0700135}
136
137////////////////////////////////////////////////////////////////////////////////
138
bsalomonf21dab92014-11-13 13:33:28 -0800139
bsalomon682c2692015-05-22 14:01:46 -0700140GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext) {
141 GrContextOptions defaultOptions;
142 return Create(backend, backendContext, defaultOptions);
143}
bsalomonf28cff72015-05-22 12:25:41 -0700144
bsalomon682c2692015-05-22 14:01:46 -0700145GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
146 const GrContextOptions& options) {
halcanary385fe4d2015-08-26 13:07:48 -0700147 GrContext* context = new GrContext;
bsalomon682c2692015-05-22 14:01:46 -0700148
149 if (context->init(backend, backendContext, options)) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000150 return context;
151 } else {
152 context->unref();
halcanary96fcdcc2015-08-27 07:41:13 -0700153 return nullptr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000154 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000155}
156
joshualitt0acd0d32015-05-07 08:23:19 -0700157static int32_t gNextID = 1;
158static int32_t next_id() {
159 int32_t id;
160 do {
161 id = sk_atomic_inc(&gNextID);
162 } while (id == SK_InvalidGenID);
163 return id;
164}
165
bsalomon682c2692015-05-22 14:01:46 -0700166GrContext::GrContext() : fUniqueID(next_id()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700167 fGpu = nullptr;
168 fCaps = nullptr;
169 fResourceCache = nullptr;
170 fResourceProvider = nullptr;
171 fPathRendererChain = nullptr;
172 fSoftwarePathRenderer = nullptr;
173 fBatchFontCache = nullptr;
bsalomonf21dab92014-11-13 13:33:28 -0800174 fFlushToReduceCacheSize = false;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000175}
176
bsalomon682c2692015-05-22 14:01:46 -0700177bool GrContext::init(GrBackend backend, GrBackendContext backendContext,
178 const GrContextOptions& options) {
robertphillipsea461502015-05-26 11:38:03 -0700179 SkASSERT(!fGpu);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000180
bsalomon682c2692015-05-22 14:01:46 -0700181 fGpu = GrGpu::Create(backend, backendContext, options, this);
robertphillipsea461502015-05-26 11:38:03 -0700182 if (!fGpu) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000183 return false;
184 }
bsalomon33435572014-11-05 14:47:41 -0800185 this->initCommon();
186 return true;
187}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000188
bsalomon33435572014-11-05 14:47:41 -0800189void GrContext::initCommon() {
bsalomon76228632015-05-29 08:02:10 -0700190 fCaps = SkRef(fGpu->caps());
halcanary385fe4d2015-08-26 13:07:48 -0700191 fResourceCache = new GrResourceCache(fCaps);
bsalomon0ea80f42015-02-11 10:49:59 -0800192 fResourceCache->setOverBudgetCallback(OverBudgetCB, this);
halcanary385fe4d2015-08-26 13:07:48 -0700193 fResourceProvider = new GrResourceProvider(fGpu, fResourceCache);
commit-bot@chromium.org1836d332013-07-16 22:55:03 +0000194
halcanary385fe4d2015-08-26 13:07:48 -0700195 fLayerCache.reset(new GrLayerCache(this));
robertphillips@google.come930a072014-04-03 00:34:27 +0000196
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000197 fDidTestPMConversions = false;
198
robertphillipsea461502015-05-26 11:38:03 -0700199 fDrawingMgr.init(this);
joshualitt7c3a2f82015-03-31 13:32:05 -0700200
201 // GrBatchFontCache will eventually replace GrFontCache
halcanary385fe4d2015-08-26 13:07:48 -0700202 fBatchFontCache = new GrBatchFontCache(this);
joshualittb7133be2015-04-08 09:08:31 -0700203
halcanary385fe4d2015-08-26 13:07:48 -0700204 fTextBlobCache.reset(new GrTextBlobCache(TextBlobCacheOverBudgetCB, this));
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000205}
206
bsalomon@google.com27847de2011-02-22 20:59:41 +0000207GrContext::~GrContext() {
robertphillipsea461502015-05-26 11:38:03 -0700208 if (!fGpu) {
bsalomon76228632015-05-29 08:02:10 -0700209 SkASSERT(!fCaps);
bsalomon@google.com733c0622013-04-24 17:59:32 +0000210 return;
211 }
212
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000213 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000214
robertphillips2334fb62015-06-17 05:43:33 -0700215 fDrawingMgr.cleanup();
216
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000217 for (int i = 0; i < fCleanUpData.count(); ++i) {
218 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
219 }
220
halcanary385fe4d2015-08-26 13:07:48 -0700221 delete fResourceProvider;
222 delete fResourceCache;
223 delete fBatchFontCache;
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000224
bsalomon@google.com205d4602011-04-25 12:43:45 +0000225 fGpu->unref();
bsalomon76228632015-05-29 08:02:10 -0700226 fCaps->unref();
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000227 SkSafeUnref(fPathRendererChain);
228 SkSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000229}
230
bsalomon2354f842014-07-28 13:48:36 -0700231void GrContext::abandonContext() {
bsalomond309e7a2015-04-30 14:18:54 -0700232 fResourceProvider->abandon();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000233 // abandon first to so destructors
234 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800235 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700236
robertphillipse3371302014-09-17 06:01:06 -0700237 fGpu->contextAbandoned();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000238
bsalomon@google.com30085192011-08-19 15:42:31 +0000239 // a path renderer may be holding onto resources that
240 // are now unusable
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000241 SkSafeSetNull(fPathRendererChain);
242 SkSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000243
robertphillipsea461502015-05-26 11:38:03 -0700244 fDrawingMgr.abandon();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000245
joshualitt7c3a2f82015-03-31 13:32:05 -0700246 fBatchFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000247 fLayerCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700248 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000249}
250
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000251void GrContext::resetContext(uint32_t state) {
252 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000253}
254
255void GrContext::freeGpuResources() {
256 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000257
joshualitt7c3a2f82015-03-31 13:32:05 -0700258 fBatchFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000259 fLayerCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000260 // a path renderer may be holding onto resources
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000261 SkSafeSetNull(fPathRendererChain);
262 SkSafeSetNull(fSoftwarePathRenderer);
bsalomon3033b9f2015-04-13 11:09:56 -0700263
264 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000265}
266
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000267void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
bsalomon71cb0c22014-11-14 12:10:14 -0800268 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800269 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800270 }
271 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800272 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800273 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000274}
275
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000276////////////////////////////////////////////////////////////////////////////////
277
bsalomon71cb0c22014-11-14 12:10:14 -0800278void GrContext::OverBudgetCB(void* data) {
bsalomon66a450f2014-11-13 13:19:10 -0800279 SkASSERT(data);
bsalomonf21dab92014-11-13 13:33:28 -0800280
bsalomon66a450f2014-11-13 13:19:10 -0800281 GrContext* context = reinterpret_cast<GrContext*>(data);
bsalomonf21dab92014-11-13 13:33:28 -0800282
joshualittb542bae2015-07-28 09:58:39 -0700283 // Flush the GrBufferedDrawTarget to possibly free up some textures
bsalomonf21dab92014-11-13 13:33:28 -0800284 context->fFlushToReduceCacheSize = true;
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000285}
286
joshualitt0db6dfa2015-04-10 07:01:30 -0700287void GrContext::TextBlobCacheOverBudgetCB(void* data) {
288 SkASSERT(data);
289
290 // Unlike the GrResourceCache, TextBlobs are drawn at the SkGpuDevice level, therefore they
291 // cannot use fFlushTorReduceCacheSize because it uses AutoCheckFlush. The solution is to move
292 // drawText calls to below the GrContext level, but this is not trivial because they call
293 // drawPath on SkGpuDevice
294 GrContext* context = reinterpret_cast<GrContext*>(data);
295 context->flush();
296}
297
bsalomon@google.com27847de2011-02-22 20:59:41 +0000298////////////////////////////////////////////////////////////////////////////////
299
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000300void GrContext::flush(int flagsBitfield) {
robertphillipsea461502015-05-26 11:38:03 -0700301 RETURN_IF_ABANDONED
robertphillips@google.come7db8d62013-07-04 11:48:52 +0000302
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000303 if (kDiscard_FlushBit & flagsBitfield) {
robertphillipsea461502015-05-26 11:38:03 -0700304 fDrawingMgr.reset();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000305 } else {
robertphillipsea461502015-05-26 11:38:03 -0700306 fDrawingMgr.flush();
junov@google.com53a55842011-06-08 22:55:10 +0000307 }
bsalomon3f324322015-04-08 11:01:54 -0700308 fResourceCache->notifyFlushOccurred();
bsalomonf21dab92014-11-13 13:33:28 -0800309 fFlushToReduceCacheSize = false;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000310}
311
bsalomon81beccc2014-10-13 12:32:55 -0700312bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
313 const void* inPixels, size_t outRowBytes, void* outPixels) {
314 SkSrcPixelInfo srcPI;
halcanary96fcdcc2015-08-27 07:41:13 -0700315 if (!GrPixelConfig2ColorAndProfileType(srcConfig, &srcPI.fColorType, nullptr)) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000316 return false;
317 }
bsalomon81beccc2014-10-13 12:32:55 -0700318 srcPI.fAlphaType = kUnpremul_SkAlphaType;
319 srcPI.fPixels = inPixels;
320 srcPI.fRowBytes = inRowBytes;
321
322 SkDstPixelInfo dstPI;
323 dstPI.fColorType = srcPI.fColorType;
324 dstPI.fAlphaType = kPremul_SkAlphaType;
325 dstPI.fPixels = outPixels;
326 dstPI.fRowBytes = outRowBytes;
327
328 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000329}
330
bsalomon81beccc2014-10-13 12:32:55 -0700331bool GrContext::writeSurfacePixels(GrSurface* surface,
332 int left, int top, int width, int height,
333 GrPixelConfig srcConfig, const void* buffer, size_t rowBytes,
334 uint32_t pixelOpsFlags) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800335 RETURN_FALSE_IF_ABANDONED
bsalomon6c6f6582015-09-10 08:12:46 -0700336 ASSERT_OWNED_RESOURCE(surface);
337 SkASSERT(surface);
338
339 this->testPMConversionsIfNecessary(pixelOpsFlags);
bsalomon81beccc2014-10-13 12:32:55 -0700340
bsalomone8d21e82015-07-16 08:23:13 -0700341 // 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 -0700342 // necessary and because GrGpu::getWritePixelsInfo requires it.
bsalomone8d21e82015-07-16 08:23:13 -0700343 if (!GrSurfacePriv::AdjustWritePixelParams(surface->width(), surface->height(),
344 GrBytesPerPixel(srcConfig), &left, &top, &width,
345 &height, &buffer, &rowBytes)) {
346 return false;
347 }
348
bsalomonf0674512015-07-28 13:26:15 -0700349 bool applyPremulToSrc = false;
bsalomon81beccc2014-10-13 12:32:55 -0700350 if (kUnpremul_PixelOpsFlag & pixelOpsFlags) {
351 if (!GrPixelConfigIs8888(srcConfig)) {
352 return false;
353 }
bsalomonf0674512015-07-28 13:26:15 -0700354 applyPremulToSrc = true;
355 }
bsalomon636e8022015-07-29 06:08:46 -0700356
357 GrGpu::DrawPreference drawPreference = GrGpu::kNoDraw_DrawPreference;
358 // Don't prefer to draw for the conversion (and thereby access a texture from the cache) when
359 // we've already determined that there isn't a roundtrip preserving conversion processor pair.
360 if (applyPremulToSrc && !this->didFailPMUPMConversionTest()) {
361 drawPreference = GrGpu::kCallerPrefersDraw_DrawPreference;
362 }
363
bsalomonf0674512015-07-28 13:26:15 -0700364 GrGpu::WritePixelTempDrawInfo tempDrawInfo;
365 if (!fGpu->getWritePixelsInfo(surface, width, height, rowBytes, srcConfig, &drawPreference,
366 &tempDrawInfo)) {
367 return false;
368 }
369
370 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) && surface->surfacePriv().hasPendingIO()) {
371 this->flush();
372 }
373
374 SkAutoTUnref<GrTexture> tempTexture;
375 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomoneae62002015-07-31 13:59:30 -0700376 tempTexture.reset(
377 this->textureProvider()->createApproxTexture(tempDrawInfo.fTempSurfaceDesc));
bsalomonf0674512015-07-28 13:26:15 -0700378 if (!tempTexture && GrGpu::kRequireDraw_DrawPreference == drawPreference) {
379 return false;
380 }
381 }
382
383 // temp buffer for doing sw premul conversion, if needed.
384 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
385 if (tempTexture) {
386 SkAutoTUnref<const GrFragmentProcessor> fp;
387 SkMatrix textureMatrix;
388 textureMatrix.setIDiv(tempTexture->width(), tempTexture->height());
389 GrPaint paint;
390 if (applyPremulToSrc) {
bsalomon4a339522015-10-06 08:40:50 -0700391 fp.reset(this->createUPMToPMEffect(tempTexture, tempDrawInfo.fSwapRAndB,
392 textureMatrix));
bsalomonf0674512015-07-28 13:26:15 -0700393 // If premultiplying was the only reason for the draw, fall back to a straight write.
394 if (!fp) {
395 if (GrGpu::kCallerPrefersDraw_DrawPreference == drawPreference) {
halcanary96fcdcc2015-08-27 07:41:13 -0700396 tempTexture.reset(nullptr);
bsalomonf0674512015-07-28 13:26:15 -0700397 }
398 } else {
399 applyPremulToSrc = false;
400 }
401 }
402 if (tempTexture) {
403 if (!fp) {
bsalomon4a339522015-10-06 08:40:50 -0700404 fp.reset(GrConfigConversionEffect::Create(tempTexture, tempDrawInfo.fSwapRAndB,
bsalomonf0674512015-07-28 13:26:15 -0700405 GrConfigConversionEffect::kNone_PMConversion, textureMatrix));
406 if (!fp) {
407 return false;
408 }
409 }
410 GrRenderTarget* renderTarget = surface->asRenderTarget();
411 SkASSERT(renderTarget);
412 if (tempTexture->surfacePriv().hasPendingIO()) {
413 this->flush();
414 }
415 if (applyPremulToSrc) {
416 size_t tmpRowBytes = 4 * width;
417 tmpPixels.reset(width * height);
418 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
419 tmpPixels.get())) {
420 return false;
421 }
422 rowBytes = tmpRowBytes;
423 buffer = tmpPixels.get();
424 applyPremulToSrc = false;
425 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700426 if (!fGpu->writePixels(tempTexture, 0, 0, width, height,
427 tempDrawInfo.fTempSurfaceDesc.fConfig, buffer,
428 rowBytes)) {
bsalomonf0674512015-07-28 13:26:15 -0700429 return false;
430 }
431 SkMatrix matrix;
432 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
robertphillips2e1e51f2015-10-15 08:01:48 -0700433 SkAutoTUnref<GrDrawContext> drawContext(this->drawContext(renderTarget));
bsalomonf0674512015-07-28 13:26:15 -0700434 if (!drawContext) {
435 return false;
436 }
bsalomonac856c92015-08-27 06:30:17 -0700437 paint.addColorFragmentProcessor(fp);
bsalomonf0674512015-07-28 13:26:15 -0700438 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
robertphillips2e1e51f2015-10-15 08:01:48 -0700439 drawContext->drawRect(GrClip::WideOpen(), paint, matrix, rect, nullptr);
bsalomonf0674512015-07-28 13:26:15 -0700440
441 if (kFlushWrites_PixelOp & pixelOpsFlags) {
442 this->flushSurfaceWrites(surface);
443 }
444 }
445 }
446 if (!tempTexture) {
bsalomonf0674512015-07-28 13:26:15 -0700447 if (applyPremulToSrc) {
bsalomon81beccc2014-10-13 12:32:55 -0700448 size_t tmpRowBytes = 4 * width;
449 tmpPixels.reset(width * height);
450 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
451 tmpPixels.get())) {
452 return false;
453 }
454 rowBytes = tmpRowBytes;
455 buffer = tmpPixels.get();
bsalomonf0674512015-07-28 13:26:15 -0700456 applyPremulToSrc = false;
bsalomon81beccc2014-10-13 12:32:55 -0700457 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700458 return fGpu->writePixels(surface, left, top, width, height, srcConfig, buffer, rowBytes);
bsalomon81beccc2014-10-13 12:32:55 -0700459 }
bsalomon81beccc2014-10-13 12:32:55 -0700460 return true;
461}
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000462
bsalomone8d21e82015-07-16 08:23:13 -0700463bool GrContext::readSurfacePixels(GrSurface* src,
464 int left, int top, int width, int height,
465 GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
466 uint32_t flags) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800467 RETURN_FALSE_IF_ABANDONED
bsalomone8d21e82015-07-16 08:23:13 -0700468 ASSERT_OWNED_RESOURCE(src);
469 SkASSERT(src);
bsalomon32ab2602015-09-09 18:57:49 -0700470
bsalomon6c6f6582015-09-10 08:12:46 -0700471 this->testPMConversionsIfNecessary(flags);
472 SkAutoMutexAcquire ama(fReadPixelsMutex);
473
bsalomone8d21e82015-07-16 08:23:13 -0700474 // Adjust the params so that if we wind up using an intermediate surface we've already done
475 // all the trimming and the temporary can be the min size required.
476 if (!GrSurfacePriv::AdjustReadPixelParams(src->width(), src->height(),
477 GrBytesPerPixel(dstConfig), &left,
478 &top, &width, &height, &buffer, &rowBytes)) {
479 return false;
480 }
481
482 if (!(kDontFlush_PixelOpsFlag & flags) && src->surfacePriv().hasPendingWrite()) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000483 this->flush();
484 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000485
bsalomone8d21e82015-07-16 08:23:13 -0700486 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.com9c680582013-02-06 18:17:50 +0000487 if (unpremul && !GrPixelConfigIs8888(dstConfig)) {
bsalomon39826022015-07-23 08:07:21 -0700488 // The unpremul flag is only allowed for 8888 configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000489 return false;
490 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000491
bsalomon636e8022015-07-29 06:08:46 -0700492 GrGpu::DrawPreference drawPreference = GrGpu::kNoDraw_DrawPreference;
493 // Don't prefer to draw for the conversion (and thereby access a texture from the cache) when
494 // we've already determined that there isn't a roundtrip preserving conversion processor pair.
495 if (unpremul && !this->didFailPMUPMConversionTest()) {
496 drawPreference = GrGpu::kCallerPrefersDraw_DrawPreference;
497 }
498
bsalomon39826022015-07-23 08:07:21 -0700499 GrGpu::ReadPixelTempDrawInfo tempDrawInfo;
500 if (!fGpu->getReadPixelsInfo(src, width, height, rowBytes, dstConfig, &drawPreference,
501 &tempDrawInfo)) {
502 return false;
503 }
bsalomon191bcc02014-11-14 11:31:13 -0800504
bsalomon6cb3cbe2015-07-30 07:34:27 -0700505 SkAutoTUnref<GrSurface> surfaceToRead(SkRef(src));
bsalomon39826022015-07-23 08:07:21 -0700506 bool didTempDraw = false;
507 if (GrGpu::kNoDraw_DrawPreference != drawPreference) {
bsalomon39826022015-07-23 08:07:21 -0700508 if (tempDrawInfo.fUseExactScratch) {
509 // We only respect this when the entire src is being read. Otherwise we can trigger too
510 // many odd ball texture sizes and trash the cache.
bsalomoneae62002015-07-31 13:59:30 -0700511 if (width != src->width() || height != src->height()) {
512 tempDrawInfo.fUseExactScratch = false;
bsalomon39826022015-07-23 08:07:21 -0700513 }
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000514 }
bsalomon39826022015-07-23 08:07:21 -0700515 SkAutoTUnref<GrTexture> temp;
bsalomoneae62002015-07-31 13:59:30 -0700516 if (tempDrawInfo.fUseExactScratch) {
517 temp.reset(this->textureProvider()->createTexture(tempDrawInfo.fTempSurfaceDesc, true));
518 } else {
519 temp.reset(this->textureProvider()->createApproxTexture(tempDrawInfo.fTempSurfaceDesc));
520 }
bsalomon39826022015-07-23 08:07:21 -0700521 if (temp) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000522 SkMatrix textureMatrix;
bsalomon39826022015-07-23 08:07:21 -0700523 textureMatrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000524 textureMatrix.postIDiv(src->width(), src->height());
joshualitt5f10b5c2015-07-09 10:24:35 -0700525 GrPaint paint;
joshualittb0a8a372014-09-23 09:50:21 -0700526 SkAutoTUnref<const GrFragmentProcessor> fp;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000527 if (unpremul) {
bsalomon4a339522015-10-06 08:40:50 -0700528 fp.reset(this->createPMToUPMEffect(src->asTexture(), tempDrawInfo.fSwapRAndB,
bsalomon39826022015-07-23 08:07:21 -0700529 textureMatrix));
joshualittb0a8a372014-09-23 09:50:21 -0700530 if (fp) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000531 unpremul = false; // we no longer need to do this on CPU after the read back.
bsalomon39826022015-07-23 08:07:21 -0700532 } else if (GrGpu::kCallerPrefersDraw_DrawPreference == drawPreference) {
533 // We only wanted to do the draw in order to perform the unpremul so don't
534 // bother.
halcanary96fcdcc2015-08-27 07:41:13 -0700535 temp.reset(nullptr);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000536 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000537 }
bsalomon39826022015-07-23 08:07:21 -0700538 if (!fp && temp) {
bsalomon4a339522015-10-06 08:40:50 -0700539 fp.reset(GrConfigConversionEffect::Create(src->asTexture(), tempDrawInfo.fSwapRAndB,
bsalomon39826022015-07-23 08:07:21 -0700540 GrConfigConversionEffect::kNone_PMConversion, textureMatrix));
541 }
542 if (fp) {
bsalomonac856c92015-08-27 06:30:17 -0700543 paint.addColorFragmentProcessor(fp);
bsalomon39826022015-07-23 08:07:21 -0700544 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
robertphillips2e1e51f2015-10-15 08:01:48 -0700545 SkAutoTUnref<GrDrawContext> drawContext(this->drawContext(temp->asRenderTarget()));
546 drawContext->drawRect(GrClip::WideOpen(), paint, SkMatrix::I(), rect, nullptr);
bsalomon6cb3cbe2015-07-30 07:34:27 -0700547 surfaceToRead.reset(SkRef(temp.get()));
bsalomon39826022015-07-23 08:07:21 -0700548 left = 0;
549 top = 0;
550 didTempDraw = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000551 }
bsalomon@google.com0342a852012-08-20 19:22:38 +0000552 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000553 }
joshualitt5c55fef2014-10-31 14:04:35 -0700554
bsalomon39826022015-07-23 08:07:21 -0700555 if (GrGpu::kRequireDraw_DrawPreference == drawPreference && !didTempDraw) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000556 return false;
557 }
bsalomon39826022015-07-23 08:07:21 -0700558 GrPixelConfig configToRead = dstConfig;
559 if (didTempDraw) {
bsalomon6cb3cbe2015-07-30 07:34:27 -0700560 this->flushSurfaceWrites(surfaceToRead);
bsalomon39826022015-07-23 08:07:21 -0700561 // We swapped R and B while doing the temp draw. Swap back on the read.
562 if (tempDrawInfo.fSwapRAndB) {
563 configToRead = GrPixelConfigSwapRAndB(dstConfig);
564 }
565 }
bsalomon6cb3cbe2015-07-30 07:34:27 -0700566 if (!fGpu->readPixels(surfaceToRead, left, top, width, height, configToRead, buffer,
567 rowBytes)) {
bsalomon39826022015-07-23 08:07:21 -0700568 return false;
569 }
570
571 // Perform umpremul conversion if we weren't able to perform it as a draw.
572 if (unpremul) {
reed@google.com7111d462014-03-25 16:20:24 +0000573 SkDstPixelInfo dstPI;
halcanary96fcdcc2015-08-27 07:41:13 -0700574 if (!GrPixelConfig2ColorAndProfileType(dstConfig, &dstPI.fColorType, nullptr)) {
reed@google.com7111d462014-03-25 16:20:24 +0000575 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000576 }
reed@google.com7111d462014-03-25 16:20:24 +0000577 dstPI.fAlphaType = kUnpremul_SkAlphaType;
578 dstPI.fPixels = buffer;
579 dstPI.fRowBytes = rowBytes;
580
581 SkSrcPixelInfo srcPI;
bsalomon39826022015-07-23 08:07:21 -0700582 srcPI.fColorType = dstPI.fColorType;
reed@google.com7111d462014-03-25 16:20:24 +0000583 srcPI.fAlphaType = kPremul_SkAlphaType;
584 srcPI.fPixels = buffer;
585 srcPI.fRowBytes = rowBytes;
586
587 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000588 }
589 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000590}
591
bsalomonc49e8682015-06-30 11:37:35 -0700592void GrContext::prepareSurfaceForExternalIO(GrSurface* surface) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800593 RETURN_IF_ABANDONED
bsalomon87a94eb2014-11-03 14:28:32 -0800594 SkASSERT(surface);
595 ASSERT_OWNED_RESOURCE(surface);
596 if (surface->surfacePriv().hasPendingIO()) {
597 this->flush();
598 }
599 GrRenderTarget* rt = surface->asRenderTarget();
600 if (fGpu && rt) {
601 fGpu->resolveRenderTarget(rt);
bsalomon41ebbdd2014-08-04 08:31:39 -0700602 }
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000603}
604
bsalomonf80bfed2014-10-07 05:56:02 -0700605void GrContext::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
606 const SkIPoint& dstPoint, uint32_t pixelOpsFlags) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800607 RETURN_IF_ABANDONED
robertphillipsea461502015-05-26 11:38:03 -0700608 if (!src || !dst) {
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000609 return;
610 }
bsalomone3d4bf22014-09-23 09:15:03 -0700611 ASSERT_OWNED_RESOURCE(src);
junov2bb52102014-09-29 10:18:59 -0700612 ASSERT_OWNED_RESOURCE(dst);
Brian Salomon34a98952014-09-24 11:41:24 -0400613
bsalomonf80bfed2014-10-07 05:56:02 -0700614 // Since we're going to the draw target and not GPU, no need to check kNoFlush
615 // here.
robertphillipsea461502015-05-26 11:38:03 -0700616 if (!dst->asRenderTarget()) {
junov96c118e2014-09-26 13:09:47 -0700617 return;
618 }
robertphillipsea461502015-05-26 11:38:03 -0700619
robertphillips2e1e51f2015-10-15 08:01:48 -0700620 SkAutoTUnref<GrDrawContext> drawContext(this->drawContext(dst->asRenderTarget()));
robertphillipsea461502015-05-26 11:38:03 -0700621 if (!drawContext) {
622 return;
623 }
624
robertphillips2e1e51f2015-10-15 08:01:48 -0700625 drawContext->copySurface(src, srcRect, dstPoint);
bsalomonf80bfed2014-10-07 05:56:02 -0700626
627 if (kFlushWrites_PixelOp & pixelOpsFlags) {
628 this->flush();
629 }
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000630}
631
bsalomonf80bfed2014-10-07 05:56:02 -0700632void GrContext::flushSurfaceWrites(GrSurface* surface) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800633 RETURN_IF_ABANDONED
bsalomonf80bfed2014-10-07 05:56:02 -0700634 if (surface->surfacePriv().hasPendingWrite()) {
635 this->flush();
636 }
637}
638
robertphillips@google.com72176b22012-05-23 13:19:12 +0000639/*
640 * This method finds a path renderer that can draw the specified path on
641 * the provided target.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000642 * Due to its expense, the software path renderer has split out so it can
robertphillips@google.com72176b22012-05-23 13:19:12 +0000643 * can be individually allowed/disallowed via the "allowSW" boolean.
644 */
joshualitt9853cce2014-11-17 14:22:48 -0800645GrPathRenderer* GrContext::getPathRenderer(const GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -0800646 const GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800647 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800648 const SkPath& path,
kkinnunen18996512015-04-26 23:18:49 -0700649 const GrStrokeInfo& stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000650 bool allowSW,
651 GrPathRendererChain::DrawType drawType,
652 GrPathRendererChain::StencilSupport* stencilSupport) {
653
robertphillipsea461502015-05-26 11:38:03 -0700654 if (!fPathRendererChain) {
halcanary385fe4d2015-08-26 13:07:48 -0700655 fPathRendererChain = new GrPathRendererChain(this);
bsalomon@google.com30085192011-08-19 15:42:31 +0000656 }
robertphillips@google.com72176b22012-05-23 13:19:12 +0000657
joshualitt9853cce2014-11-17 14:22:48 -0800658 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(target,
egdaniel8dd688b2015-01-22 10:16:09 -0800659 pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800660 viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800661 path,
sugoi@google.com12b4e272012-12-06 20:13:11 +0000662 stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000663 drawType,
664 stencilSupport);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000665
robertphillipsea461502015-05-26 11:38:03 -0700666 if (!pr && allowSW) {
667 if (!fSoftwarePathRenderer) {
halcanary385fe4d2015-08-26 13:07:48 -0700668 fSoftwarePathRenderer = new GrSoftwarePathRenderer(this);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000669 }
robertphillips@google.com72176b22012-05-23 13:19:12 +0000670 pr = fSoftwarePathRenderer;
671 }
672
673 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +0000674}
675
bsalomon@google.com27847de2011-02-22 20:59:41 +0000676////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000677int GrContext::getRecommendedSampleCount(GrPixelConfig config,
678 SkScalar dpi) const {
bsalomon76228632015-05-29 08:02:10 -0700679 if (!this->caps()->isConfigRenderable(config, true)) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000680 return 0;
681 }
682 int chosenSampleCount = 0;
jvanverthe9c0fc62015-04-29 11:18:05 -0700683 if (fGpu->caps()->shaderCaps()->pathRenderingSupport()) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000684 if (dpi >= 250.0f) {
685 chosenSampleCount = 4;
686 } else {
687 chosenSampleCount = 16;
688 }
689 }
690 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ?
691 chosenSampleCount : 0;
692}
693
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000694namespace {
695void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
696 GrConfigConversionEffect::PMConversion pmToUPM;
697 GrConfigConversionEffect::PMConversion upmToPM;
698 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
699 *pmToUPMValue = pmToUPM;
700 *upmToPMValue = upmToPM;
701}
702}
703
bsalomon6c6f6582015-09-10 08:12:46 -0700704void GrContext::testPMConversionsIfNecessary(uint32_t flags) {
705 if (SkToBool(kUnpremul_PixelOpsFlag & flags)) {
706 SkAutoMutexAcquire ama(fTestPMConversionsMutex);
707 if (!fDidTestPMConversions) {
708 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
709 fDidTestPMConversions = true;
710 }
711 }
712}
713
bsalomon4a339522015-10-06 08:40:50 -0700714const GrFragmentProcessor* GrContext::createPMToUPMEffect(GrTexture* texture,
joshualittb0a8a372014-09-23 09:50:21 -0700715 bool swapRAndB,
bsalomon6c6f6582015-09-10 08:12:46 -0700716 const SkMatrix& matrix) const {
717 // We should have already called this->testPMConversionsIfNecessary().
718 SkASSERT(fDidTestPMConversions);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000719 GrConfigConversionEffect::PMConversion pmToUPM =
720 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
721 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bsalomon4a339522015-10-06 08:40:50 -0700722 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000723 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700724 return nullptr;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000725 }
726}
727
bsalomon4a339522015-10-06 08:40:50 -0700728const GrFragmentProcessor* GrContext::createUPMToPMEffect(GrTexture* texture,
joshualittb0a8a372014-09-23 09:50:21 -0700729 bool swapRAndB,
bsalomon6c6f6582015-09-10 08:12:46 -0700730 const SkMatrix& matrix) const {
731 // We should have already called this->testPMConversionsIfNecessary().
732 SkASSERT(fDidTestPMConversions);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000733 GrConfigConversionEffect::PMConversion upmToPM =
734 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
735 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
bsalomon4a339522015-10-06 08:40:50 -0700736 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000737 } else {
halcanary96fcdcc2015-08-27 07:41:13 -0700738 return nullptr;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000739 }
740}
741
bsalomon636e8022015-07-29 06:08:46 -0700742bool GrContext::didFailPMUPMConversionTest() const {
bsalomon6c6f6582015-09-10 08:12:46 -0700743 // We should have already called this->testPMConversionsIfNecessary().
744 SkASSERT(fDidTestPMConversions);
bsalomon636e8022015-07-29 06:08:46 -0700745 // The PM<->UPM tests fail or succeed together so we only need to check one.
bsalomon6c6f6582015-09-10 08:12:46 -0700746 return GrConfigConversionEffect::kNone_PMConversion == fPMToUPMConversion;
bsalomon636e8022015-07-29 06:08:46 -0700747}
748
bsalomon37f9a262015-02-02 13:00:10 -0800749//////////////////////////////////////////////////////////////////////////////
750
751void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
752 if (maxTextures) {
bsalomon0ea80f42015-02-11 10:49:59 -0800753 *maxTextures = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -0800754 }
755 if (maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800756 *maxTextureBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -0800757 }
758}
759
760void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800761 fResourceCache->setLimits(maxTextures, maxTextureBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800762}
763
ericrk0a5fa482015-09-15 14:16:10 -0700764//////////////////////////////////////////////////////////////////////////////
765
766void GrContext::dumpMemoryStatistics(SkTraceMemoryDump* traceMemoryDump) const {
767 fResourceCache->dumpMemoryStatistics(traceMemoryDump);
768}