blob: ca98ffb416a7f6fec0364cedbc38b8abcda5c6f5 [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
jvanverth@google.combfe2b9d2013-09-06 16:57:29 +000011#include "GrAARectRenderer.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070012#include "GrAtlasTextContext.h"
joshualitt0823bfa2015-02-27 10:06:15 -080013#include "GrBatch.h"
joshualitt7c3a2f82015-03-31 13:32:05 -070014#include "GrBatchFontCache.h"
joshualitt0823bfa2015-02-27 10:06:15 -080015#include "GrBatchTarget.h"
joshualitte5902662015-05-11 08:18:35 -070016#include "GrBatchTest.h"
bsalomoneb1cb5c2015-05-22 08:01:09 -070017#include "GrCaps.h"
bsalomon682c2692015-05-22 14:01:46 -070018#include "GrContextOptions.h"
joshualitt5478d422014-11-14 16:00:38 -080019#include "GrDefaultGeoProcFactory.h"
robertphillipsea461502015-05-26 11:38:03 -070020#include "GrDrawContext.h"
bsalomon453cf402014-11-11 14:15:57 -080021#include "GrGpuResource.h"
bsalomon3582d3e2015-02-13 14:20:05 -080022#include "GrGpuResourcePriv.h"
joshualitt5478d422014-11-14 16:00:38 -080023#include "GrGpu.h"
joshualittbb87b212015-05-19 14:28:04 -070024#include "GrImmediateDrawTarget.h"
commit-bot@chromium.orgdcb8ef92014-03-27 11:26:10 +000025#include "GrIndexBuffer.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000026#include "GrInOrderDrawBuffer.h"
robertphillips@google.come930a072014-04-03 00:34:27 +000027#include "GrLayerCache.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +000028#include "GrOvalRenderer.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000029#include "GrPathRenderer.h"
tomhudson@google.comd22b6e42011-06-24 15:53:40 +000030#include "GrPathUtils.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080031#include "GrRenderTargetPriv.h"
bsalomon0ea80f42015-02-11 10:49:59 -080032#include "GrResourceCache.h"
bsalomond309e7a2015-04-30 14:18:54 -070033#include "GrResourceProvider.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000034#include "GrSoftwarePathRenderer.h"
kkinnunenc6cb56f2014-06-24 00:12:27 -070035#include "GrStencilAndCoverTextContext.h"
egdanield58a0ba2014-06-11 10:30:05 -070036#include "GrStrokeInfo.h"
bsalomonafbf2d62014-09-30 12:18:44 -070037#include "GrSurfacePriv.h"
joshualittb7133be2015-04-08 09:08:31 -070038#include "GrTextBlobCache.h"
bsalomonafbf2d62014-09-30 12:18:44 -070039#include "GrTexturePriv.h"
egdanielbbcb38d2014-06-19 10:19:29 -070040#include "GrTraceMarker.h"
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +000041#include "GrTracing.h"
bsalomoncb8979d2015-05-05 09:51:38 -070042#include "GrVertices.h"
egdanield58a0ba2014-06-11 10:30:05 -070043#include "SkDashPathPriv.h"
bsalomon81beccc2014-10-13 12:32:55 -070044#include "SkConfig8888.h"
reed@google.com7111d462014-03-25 16:20:24 +000045#include "SkGr.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000046#include "SkRRect.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000047#include "SkStrokeRec.h"
commit-bot@chromium.orgdcb8ef92014-03-27 11:26:10 +000048#include "SkTLazy.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000049#include "SkTLS.h"
commit-bot@chromium.org933e65d2014-03-20 20:00:24 +000050#include "SkTraceEvent.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000051
joshualitt5478d422014-11-14 16:00:38 -080052#include "effects/GrConfigConversionEffect.h"
53#include "effects/GrDashingEffect.h"
54#include "effects/GrSingleTextureEffect.h"
55
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000056#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
robertphillipsea461502015-05-26 11:38:03 -070057#define RETURN_IF_ABANDONED if (fDrawingMgr.abandoned()) { return; }
58#define RETURN_FALSE_IF_ABANDONED if (fDrawingMgr.abandoned()) { return false; }
59#define RETURN_NULL_IF_ABANDONED if (fDrawingMgr.abandoned()) { return NULL; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000060
bsalomonf21dab92014-11-13 13:33:28 -080061
robertphillipsea461502015-05-26 11:38:03 -070062////////////////////////////////////////////////////////////////////////////////
63
64void GrContext::DrawingMgr::init(GrContext* context) {
65#ifdef IMMEDIATE_MODE
66 fDrawTarget = SkNEW_ARGS(GrImmediateDrawTarget, (context));
67#else
68 fDrawTarget = SkNEW_ARGS(GrInOrderDrawBuffer, (context));
69#endif
70
71 fDrawContext = SkNEW_ARGS(GrDrawContext, (context, fDrawTarget));
72}
73
74GrContext::DrawingMgr::~DrawingMgr() {
75 SkSafeUnref(fDrawTarget);
76 SkSafeUnref(fDrawContext);
77}
78
79void GrContext::DrawingMgr::abandon() {
80 SkSafeSetNull(fDrawTarget);
robertphillips4b195e52015-05-26 14:37:00 -070081 SkSafeSetNull(fDrawContext->fDrawTarget);
robertphillipsea461502015-05-26 11:38:03 -070082 SkSafeSetNull(fDrawContext);
83}
84
85void GrContext::DrawingMgr::purgeResources() {
86 if (fDrawTarget) {
87 fDrawTarget->purgeResources();
bsalomonf21dab92014-11-13 13:33:28 -080088 }
robertphillipsea461502015-05-26 11:38:03 -070089}
bsalomonf21dab92014-11-13 13:33:28 -080090
robertphillipsea461502015-05-26 11:38:03 -070091void GrContext::DrawingMgr::reset() {
92 if (fDrawTarget) {
93 fDrawTarget->reset();
94 }
95}
96
97void GrContext::DrawingMgr::flush() {
98 if (fDrawTarget) {
99 fDrawTarget->flush();
100 }
101}
102
103GrDrawContext* GrContext::DrawingMgr::drawContext() {
104 if (this->abandoned()) {
105 return NULL;
106 }
107 return fDrawContext;
108}
109
110////////////////////////////////////////////////////////////////////////////////
111
bsalomonf21dab92014-11-13 13:33:28 -0800112
bsalomon682c2692015-05-22 14:01:46 -0700113GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext) {
114 GrContextOptions defaultOptions;
115 return Create(backend, backendContext, defaultOptions);
116}
bsalomonf28cff72015-05-22 12:25:41 -0700117
bsalomon682c2692015-05-22 14:01:46 -0700118GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
119 const GrContextOptions& options) {
120 GrContext* context = SkNEW(GrContext);
121
122 if (context->init(backend, backendContext, options)) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000123 return context;
124 } else {
125 context->unref();
126 return NULL;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000127 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000128}
129
joshualitt0acd0d32015-05-07 08:23:19 -0700130static int32_t gNextID = 1;
131static int32_t next_id() {
132 int32_t id;
133 do {
134 id = sk_atomic_inc(&gNextID);
135 } while (id == SK_InvalidGenID);
136 return id;
137}
138
bsalomon682c2692015-05-22 14:01:46 -0700139GrContext::GrContext() : fUniqueID(next_id()) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000140 fGpu = NULL;
bsalomon76228632015-05-29 08:02:10 -0700141 fCaps = NULL;
bsalomond309e7a2015-04-30 14:18:54 -0700142 fResourceCache = NULL;
143 fResourceProvider = NULL;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000144 fPathRendererChain = NULL;
145 fSoftwarePathRenderer = NULL;
joshualitt7c3a2f82015-03-31 13:32:05 -0700146 fBatchFontCache = NULL;
bsalomonf21dab92014-11-13 13:33:28 -0800147 fFlushToReduceCacheSize = false;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000148}
149
bsalomon682c2692015-05-22 14:01:46 -0700150bool GrContext::init(GrBackend backend, GrBackendContext backendContext,
151 const GrContextOptions& options) {
robertphillipsea461502015-05-26 11:38:03 -0700152 SkASSERT(!fGpu);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000153
bsalomon682c2692015-05-22 14:01:46 -0700154 fGpu = GrGpu::Create(backend, backendContext, options, this);
robertphillipsea461502015-05-26 11:38:03 -0700155 if (!fGpu) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000156 return false;
157 }
bsalomon33435572014-11-05 14:47:41 -0800158 this->initCommon();
159 return true;
160}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000161
bsalomon33435572014-11-05 14:47:41 -0800162void GrContext::initCommon() {
bsalomon76228632015-05-29 08:02:10 -0700163 fCaps = SkRef(fGpu->caps());
bsalomon0ea80f42015-02-11 10:49:59 -0800164 fResourceCache = SkNEW(GrResourceCache);
165 fResourceCache->setOverBudgetCallback(OverBudgetCB, this);
bsalomond309e7a2015-04-30 14:18:54 -0700166 fResourceProvider = SkNEW_ARGS(GrResourceProvider, (fGpu, fResourceCache));
commit-bot@chromium.org1836d332013-07-16 22:55:03 +0000167
robertphillips4ec84da2014-06-24 13:10:43 -0700168 fLayerCache.reset(SkNEW_ARGS(GrLayerCache, (this)));
robertphillips@google.come930a072014-04-03 00:34:27 +0000169
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000170 fDidTestPMConversions = false;
171
robertphillipsea461502015-05-26 11:38:03 -0700172 fDrawingMgr.init(this);
joshualitt7c3a2f82015-03-31 13:32:05 -0700173
174 // GrBatchFontCache will eventually replace GrFontCache
joshualitt62db8ba2015-04-09 08:22:37 -0700175 fBatchFontCache = SkNEW_ARGS(GrBatchFontCache, (this));
joshualittb7133be2015-04-08 09:08:31 -0700176
joshualitt0db6dfa2015-04-10 07:01:30 -0700177 fTextBlobCache.reset(SkNEW_ARGS(GrTextBlobCache, (TextBlobCacheOverBudgetCB, this)));
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000178}
179
bsalomon@google.com27847de2011-02-22 20:59:41 +0000180GrContext::~GrContext() {
robertphillipsea461502015-05-26 11:38:03 -0700181 if (!fGpu) {
bsalomon76228632015-05-29 08:02:10 -0700182 SkASSERT(!fCaps);
bsalomon@google.com733c0622013-04-24 17:59:32 +0000183 return;
184 }
185
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000186 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000187
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000188 for (int i = 0; i < fCleanUpData.count(); ++i) {
189 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
190 }
191
bsalomond309e7a2015-04-30 14:18:54 -0700192 SkDELETE(fResourceProvider);
bsalomon0ea80f42015-02-11 10:49:59 -0800193 SkDELETE(fResourceCache);
joshualitt7c3a2f82015-03-31 13:32:05 -0700194 SkDELETE(fBatchFontCache);
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000195
bsalomon@google.com205d4602011-04-25 12:43:45 +0000196 fGpu->unref();
bsalomon76228632015-05-29 08:02:10 -0700197 fCaps->unref();
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000198 SkSafeUnref(fPathRendererChain);
199 SkSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000200}
201
bsalomon2354f842014-07-28 13:48:36 -0700202void GrContext::abandonContext() {
bsalomond309e7a2015-04-30 14:18:54 -0700203 fResourceProvider->abandon();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000204 // abandon first to so destructors
205 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800206 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700207
robertphillipse3371302014-09-17 06:01:06 -0700208 fGpu->contextAbandoned();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000209
bsalomon@google.com30085192011-08-19 15:42:31 +0000210 // a path renderer may be holding onto resources that
211 // are now unusable
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000212 SkSafeSetNull(fPathRendererChain);
213 SkSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000214
robertphillipsea461502015-05-26 11:38:03 -0700215 fDrawingMgr.abandon();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000216
joshualitt7c3a2f82015-03-31 13:32:05 -0700217 fBatchFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000218 fLayerCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700219 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000220}
221
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000222void GrContext::resetContext(uint32_t state) {
223 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000224}
225
226void GrContext::freeGpuResources() {
227 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000228
robertphillipsea461502015-05-26 11:38:03 -0700229 fDrawingMgr.purgeResources();
robertphillips@google.comff175842012-05-14 19:31:39 +0000230
joshualitt7c3a2f82015-03-31 13:32:05 -0700231 fBatchFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000232 fLayerCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000233 // a path renderer may be holding onto resources
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000234 SkSafeSetNull(fPathRendererChain);
235 SkSafeSetNull(fSoftwarePathRenderer);
bsalomon3033b9f2015-04-13 11:09:56 -0700236
237 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000238}
239
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000240void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
bsalomon71cb0c22014-11-14 12:10:14 -0800241 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800242 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800243 }
244 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800245 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800246 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000247}
248
kkinnunenc6cb56f2014-06-24 00:12:27 -0700249GrTextContext* GrContext::createTextContext(GrRenderTarget* renderTarget,
250 const SkDeviceProperties&
251 leakyProperties,
252 bool enableDistanceFieldFonts) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700253 if (fGpu->caps()->shaderCaps()->pathRenderingSupport() && renderTarget->isMultisampled()) {
egdaniel8dc7c3a2015-04-16 11:22:42 -0700254 GrStencilAttachment* sb = renderTarget->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800255 if (sb) {
robertphillipsccb1b572015-05-27 11:02:55 -0700256 return GrStencilAndCoverTextContext::Create(this, leakyProperties);
bsalomon6bc1b5f2015-02-23 09:06:38 -0800257 }
jvanverth8c27a182014-10-14 08:45:50 -0700258 }
259
robertphillipsccb1b572015-05-27 11:02:55 -0700260 return GrAtlasTextContext::Create(this, leakyProperties, enableDistanceFieldFonts);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700261}
262
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000263////////////////////////////////////////////////////////////////////////////////
264
bsalomon71cb0c22014-11-14 12:10:14 -0800265void GrContext::OverBudgetCB(void* data) {
bsalomon66a450f2014-11-13 13:19:10 -0800266 SkASSERT(data);
bsalomonf21dab92014-11-13 13:33:28 -0800267
bsalomon66a450f2014-11-13 13:19:10 -0800268 GrContext* context = reinterpret_cast<GrContext*>(data);
bsalomonf21dab92014-11-13 13:33:28 -0800269
270 // Flush the InOrderDrawBuffer to possibly free up some textures
271 context->fFlushToReduceCacheSize = true;
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000272}
273
joshualitt0db6dfa2015-04-10 07:01:30 -0700274void GrContext::TextBlobCacheOverBudgetCB(void* data) {
275 SkASSERT(data);
276
277 // Unlike the GrResourceCache, TextBlobs are drawn at the SkGpuDevice level, therefore they
278 // cannot use fFlushTorReduceCacheSize because it uses AutoCheckFlush. The solution is to move
279 // drawText calls to below the GrContext level, but this is not trivial because they call
280 // drawPath on SkGpuDevice
281 GrContext* context = reinterpret_cast<GrContext*>(data);
282 context->flush();
283}
284
bsalomon@google.com27847de2011-02-22 20:59:41 +0000285////////////////////////////////////////////////////////////////////////////////
286
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000287void GrContext::flush(int flagsBitfield) {
robertphillipsea461502015-05-26 11:38:03 -0700288 RETURN_IF_ABANDONED
robertphillips@google.come7db8d62013-07-04 11:48:52 +0000289
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000290 if (kDiscard_FlushBit & flagsBitfield) {
robertphillipsea461502015-05-26 11:38:03 -0700291 fDrawingMgr.reset();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000292 } else {
robertphillipsea461502015-05-26 11:38:03 -0700293 fDrawingMgr.flush();
junov@google.com53a55842011-06-08 22:55:10 +0000294 }
bsalomon3f324322015-04-08 11:01:54 -0700295 fResourceCache->notifyFlushOccurred();
bsalomonf21dab92014-11-13 13:33:28 -0800296 fFlushToReduceCacheSize = false;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000297}
298
bsalomon81beccc2014-10-13 12:32:55 -0700299bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
300 const void* inPixels, size_t outRowBytes, void* outPixels) {
301 SkSrcPixelInfo srcPI;
jvanverthfa1e8a72014-12-22 08:31:49 -0800302 if (!GrPixelConfig2ColorAndProfileType(srcConfig, &srcPI.fColorType, NULL)) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000303 return false;
304 }
bsalomon81beccc2014-10-13 12:32:55 -0700305 srcPI.fAlphaType = kUnpremul_SkAlphaType;
306 srcPI.fPixels = inPixels;
307 srcPI.fRowBytes = inRowBytes;
308
309 SkDstPixelInfo dstPI;
310 dstPI.fColorType = srcPI.fColorType;
311 dstPI.fAlphaType = kPremul_SkAlphaType;
312 dstPI.fPixels = outPixels;
313 dstPI.fRowBytes = outRowBytes;
314
315 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000316}
317
bsalomon81beccc2014-10-13 12:32:55 -0700318bool GrContext::writeSurfacePixels(GrSurface* surface,
319 int left, int top, int width, int height,
320 GrPixelConfig srcConfig, const void* buffer, size_t rowBytes,
321 uint32_t pixelOpsFlags) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800322 RETURN_FALSE_IF_ABANDONED
bsalomon81beccc2014-10-13 12:32:55 -0700323 {
324 GrTexture* texture = NULL;
325 if (!(kUnpremul_PixelOpsFlag & pixelOpsFlags) && (texture = surface->asTexture()) &&
326 fGpu->canWriteTexturePixels(texture, srcConfig)) {
327
328 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) &&
329 surface->surfacePriv().hasPendingIO()) {
330 this->flush();
331 }
332 return fGpu->writeTexturePixels(texture, left, top, width, height,
333 srcConfig, buffer, rowBytes);
334 // Don't need to check kFlushWrites_PixelOp here, we just did a direct write so the
335 // upload is already flushed.
336 }
337 }
338
339 // If we didn't do a direct texture write then we upload the pixels to a texture and draw.
340 GrRenderTarget* renderTarget = surface->asRenderTarget();
robertphillipsea461502015-05-26 11:38:03 -0700341 if (!renderTarget) {
bsalomon81beccc2014-10-13 12:32:55 -0700342 return false;
343 }
344
345 // We ignore the preferred config unless it is a R/B swap of the src config. In that case
346 // we will upload the original src data to a scratch texture but we will spoof it as the swapped
347 // config. This scratch will then have R and B swapped. We correct for this by swapping again
348 // when drawing the scratch to the dst using a conversion effect.
349 bool swapRAndB = false;
350 GrPixelConfig writeConfig = srcConfig;
351 if (GrPixelConfigSwapRAndB(srcConfig) ==
352 fGpu->preferredWritePixelsConfig(srcConfig, renderTarget->config())) {
353 writeConfig = GrPixelConfigSwapRAndB(srcConfig);
354 swapRAndB = true;
355 }
356
bsalomonf2703d82014-10-28 14:33:06 -0700357 GrSurfaceDesc desc;
bsalomon81beccc2014-10-13 12:32:55 -0700358 desc.fWidth = width;
359 desc.fHeight = height;
360 desc.fConfig = writeConfig;
bsalomond309e7a2015-04-30 14:18:54 -0700361 SkAutoTUnref<GrTexture> texture(this->textureProvider()->refScratchTexture(desc,
362 GrTextureProvider::kApprox_ScratchTexMatch));
bsalomone3059732014-10-14 11:47:22 -0700363 if (!texture) {
bsalomon81beccc2014-10-13 12:32:55 -0700364 return false;
365 }
366
367 SkAutoTUnref<const GrFragmentProcessor> fp;
368 SkMatrix textureMatrix;
369 textureMatrix.setIDiv(texture->width(), texture->height());
370
371 // allocate a tmp buffer and sw convert the pixels to premul
372 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
373
374 if (kUnpremul_PixelOpsFlag & pixelOpsFlags) {
375 if (!GrPixelConfigIs8888(srcConfig)) {
376 return false;
377 }
378 fp.reset(this->createUPMToPMEffect(texture, swapRAndB, textureMatrix));
379 // handle the unpremul step on the CPU if we couldn't create an effect to do it.
robertphillipsea461502015-05-26 11:38:03 -0700380 if (!fp) {
bsalomon81beccc2014-10-13 12:32:55 -0700381 size_t tmpRowBytes = 4 * width;
382 tmpPixels.reset(width * height);
383 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
384 tmpPixels.get())) {
385 return false;
386 }
387 rowBytes = tmpRowBytes;
388 buffer = tmpPixels.get();
389 }
390 }
robertphillipsea461502015-05-26 11:38:03 -0700391 if (!fp) {
bsalomon81beccc2014-10-13 12:32:55 -0700392 fp.reset(GrConfigConversionEffect::Create(texture,
393 swapRAndB,
394 GrConfigConversionEffect::kNone_PMConversion,
395 textureMatrix));
396 }
397
398 // Even if the client told us not to flush, we still flush here. The client may have known that
399 // writes to the original surface caused no data hazards, but they can't know that the scratch
400 // we just got is safe.
401 if (texture->surfacePriv().hasPendingIO()) {
402 this->flush();
403 }
404 if (!fGpu->writeTexturePixels(texture, 0, 0, width, height,
405 writeConfig, buffer, rowBytes)) {
406 return false;
407 }
408
409 SkMatrix matrix;
410 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
411
robertphillipsea461502015-05-26 11:38:03 -0700412 GrDrawContext* drawContext = this->drawContext();
413 if (!drawContext) {
414 return false;
bsalomon81beccc2014-10-13 12:32:55 -0700415 }
416
robertphillipsea461502015-05-26 11:38:03 -0700417 GrPaint paint;
418 paint.addColorProcessor(fp);
419
420 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
421
422 drawContext->drawRect(renderTarget, GrClip::WideOpen(), paint, matrix, rect, NULL);
423
bsalomon81beccc2014-10-13 12:32:55 -0700424 if (kFlushWrites_PixelOp & pixelOpsFlags) {
425 this->flushSurfaceWrites(surface);
426 }
427
428 return true;
429}
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000430
reed@google.com7111d462014-03-25 16:20:24 +0000431// toggles between RGBA and BGRA
432static SkColorType toggle_colortype32(SkColorType ct) {
433 if (kRGBA_8888_SkColorType == ct) {
434 return kBGRA_8888_SkColorType;
435 } else {
436 SkASSERT(kBGRA_8888_SkColorType == ct);
437 return kRGBA_8888_SkColorType;
bsalomon@google.coma91e9232012-02-23 15:39:54 +0000438 }
439}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000440
bsalomon@google.com0342a852012-08-20 19:22:38 +0000441bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
442 int left, int top, int width, int height,
bsalomon@google.com9c680582013-02-06 18:17:50 +0000443 GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
bsalomon@google.com0342a852012-08-20 19:22:38 +0000444 uint32_t flags) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800445 RETURN_FALSE_IF_ABANDONED
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000446 ASSERT_OWNED_RESOURCE(target);
bsalomon89c62982014-11-03 12:08:42 -0800447 SkASSERT(target);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000448
bsalomonafbf2d62014-09-30 12:18:44 -0700449 if (!(kDontFlush_PixelOpsFlag & flags) && target->surfacePriv().hasPendingWrite()) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000450 this->flush();
451 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000452
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000453 // Determine which conversions have to be applied: flipY, swapRAnd, and/or unpremul.
bsalomon@google.com0342a852012-08-20 19:22:38 +0000454
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000455 // If fGpu->readPixels would incur a y-flip cost then we will read the pixels upside down. We'll
456 // either do the flipY by drawing into a scratch with a matrix or on the cpu after the read.
457 bool flipY = fGpu->readPixelsWillPayForYFlip(target, left, top,
bsalomon@google.com9c680582013-02-06 18:17:50 +0000458 width, height, dstConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +0000459 rowBytes);
bsalomon@google.com9c680582013-02-06 18:17:50 +0000460 // We ignore the preferred config if it is different than our config unless it is an R/B swap.
461 // In that case we'll perform an R and B swap while drawing to a scratch texture of the swapped
462 // config. Then we will call readPixels on the scratch with the swapped config. The swaps during
463 // the draw cancels out the fact that we call readPixels with a config that is R/B swapped from
464 // dstConfig.
465 GrPixelConfig readConfig = dstConfig;
466 bool swapRAndB = false;
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +0000467 if (GrPixelConfigSwapRAndB(dstConfig) ==
468 fGpu->preferredReadPixelsConfig(dstConfig, target->config())) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000469 readConfig = GrPixelConfigSwapRAndB(readConfig);
470 swapRAndB = true;
471 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000472
bsalomon@google.com0342a852012-08-20 19:22:38 +0000473 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000474
bsalomon@google.com9c680582013-02-06 18:17:50 +0000475 if (unpremul && !GrPixelConfigIs8888(dstConfig)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000476 // The unpremul flag is only allowed for these two configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +0000477 return false;
478 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000479
bsalomon191bcc02014-11-14 11:31:13 -0800480 SkAutoTUnref<GrTexture> tempTexture;
481
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000482 // If the src is a texture and we would have to do conversions after read pixels, we instead
483 // do the conversions by drawing the src to a scratch texture. If we handle any of the
484 // conversions in the draw we set the corresponding bool to false so that we don't reapply it
485 // on the read back pixels.
486 GrTexture* src = target->asTexture();
bsalomon49f085d2014-09-05 13:34:00 -0700487 if (src && (swapRAndB || unpremul || flipY)) {
bsalomon81beccc2014-10-13 12:32:55 -0700488 // Make the scratch a render so we can read its pixels.
bsalomonf2703d82014-10-28 14:33:06 -0700489 GrSurfaceDesc desc;
490 desc.fFlags = kRenderTarget_GrSurfaceFlag;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000491 desc.fWidth = width;
492 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000493 desc.fConfig = readConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000494 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +0000495
bsalomon@google.com9c680582013-02-06 18:17:50 +0000496 // When a full read back is faster than a partial we could always make the scratch exactly
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000497 // match the passed rect. However, if we see many different size rectangles we will trash
498 // our texture cache and pay the cost of creating and destroying many textures. So, we only
499 // request an exact match when the caller is reading an entire RT.
bsalomond309e7a2015-04-30 14:18:54 -0700500 GrTextureProvider::ScratchTexMatch match = GrTextureProvider::kApprox_ScratchTexMatch;
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000501 if (0 == left &&
502 0 == top &&
503 target->width() == width &&
504 target->height() == height &&
505 fGpu->fullReadPixelsIsFasterThanPartial()) {
bsalomond309e7a2015-04-30 14:18:54 -0700506 match = GrTextureProvider::kExact_ScratchTexMatch;
bsalomon@google.com56d11e02011-11-30 19:59:08 +0000507 }
bsalomond309e7a2015-04-30 14:18:54 -0700508 tempTexture.reset(this->textureProvider()->refScratchTexture(desc, match));
bsalomon191bcc02014-11-14 11:31:13 -0800509 if (tempTexture) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000510 // compute a matrix to perform the draw
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000511 SkMatrix textureMatrix;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000512 textureMatrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000513 textureMatrix.postIDiv(src->width(), src->height());
514
joshualittb0a8a372014-09-23 09:50:21 -0700515 SkAutoTUnref<const GrFragmentProcessor> fp;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000516 if (unpremul) {
joshualittb0a8a372014-09-23 09:50:21 -0700517 fp.reset(this->createPMToUPMEffect(src, swapRAndB, textureMatrix));
518 if (fp) {
bsalomon@google.com9c680582013-02-06 18:17:50 +0000519 unpremul = false; // we no longer need to do this on CPU after the read back.
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000520 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000521 }
522 // If we failed to create a PM->UPM effect and have no other conversions to perform then
523 // there is no longer any point to using the scratch.
joshualittb0a8a372014-09-23 09:50:21 -0700524 if (fp || flipY || swapRAndB) {
525 if (!fp) {
526 fp.reset(GrConfigConversionEffect::Create(
527 src, swapRAndB, GrConfigConversionEffect::kNone_PMConversion,
528 textureMatrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000529 }
530 swapRAndB = false; // we will handle the swap in the draw.
bsalomon@google.comc4364992011-11-07 15:54:49 +0000531
robertphillips@google.com13f181f2013-03-02 12:02:08 +0000532 // We protect the existing geometry here since it may not be
533 // clear to the caller that a draw operation (i.e., drawSimpleRect)
534 // can be invoked in this method
joshualitt5c55fef2014-10-31 14:04:35 -0700535 {
robertphillipsea461502015-05-26 11:38:03 -0700536 GrDrawContext* drawContext = this->drawContext();
537 if (!drawContext) {
538 return false;
539 }
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000540
robertphillipsea461502015-05-26 11:38:03 -0700541 GrPaint paint;
542 paint.addColorProcessor(fp);
543
joshualitt5c55fef2014-10-31 14:04:35 -0700544 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
robertphillipsea461502015-05-26 11:38:03 -0700545
546 drawContext->drawRect(tempTexture->asRenderTarget(), GrClip::WideOpen(), paint,
547 SkMatrix::I(), rect, NULL);
548
joshualitt5c55fef2014-10-31 14:04:35 -0700549 // we want to read back from the scratch's origin
550 left = 0;
551 top = 0;
bsalomon191bcc02014-11-14 11:31:13 -0800552 target = tempTexture->asRenderTarget();
joshualitt5c55fef2014-10-31 14:04:35 -0700553 }
554 this->flushSurfaceWrites(target);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000555 }
bsalomon@google.com0342a852012-08-20 19:22:38 +0000556 }
bsalomon@google.comc4364992011-11-07 15:54:49 +0000557 }
joshualitt5c55fef2014-10-31 14:04:35 -0700558
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000559 if (!fGpu->readPixels(target,
560 left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000561 readConfig, buffer, rowBytes)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000562 return false;
563 }
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000564 // Perform any conversions we weren't able to perform using a scratch texture.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000565 if (unpremul || swapRAndB) {
reed@google.com7111d462014-03-25 16:20:24 +0000566 SkDstPixelInfo dstPI;
jvanverthfa1e8a72014-12-22 08:31:49 -0800567 if (!GrPixelConfig2ColorAndProfileType(dstConfig, &dstPI.fColorType, NULL)) {
reed@google.com7111d462014-03-25 16:20:24 +0000568 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000569 }
reed@google.com7111d462014-03-25 16:20:24 +0000570 dstPI.fAlphaType = kUnpremul_SkAlphaType;
571 dstPI.fPixels = buffer;
572 dstPI.fRowBytes = rowBytes;
573
574 SkSrcPixelInfo srcPI;
575 srcPI.fColorType = swapRAndB ? toggle_colortype32(dstPI.fColorType) : dstPI.fColorType;
576 srcPI.fAlphaType = kPremul_SkAlphaType;
577 srcPI.fPixels = buffer;
578 srcPI.fRowBytes = rowBytes;
579
580 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000581 }
582 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000583}
584
bsalomon87a94eb2014-11-03 14:28:32 -0800585void GrContext::prepareSurfaceForExternalRead(GrSurface* surface) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800586 RETURN_IF_ABANDONED
bsalomon87a94eb2014-11-03 14:28:32 -0800587 SkASSERT(surface);
588 ASSERT_OWNED_RESOURCE(surface);
589 if (surface->surfacePriv().hasPendingIO()) {
590 this->flush();
591 }
592 GrRenderTarget* rt = surface->asRenderTarget();
593 if (fGpu && rt) {
594 fGpu->resolveRenderTarget(rt);
bsalomon41ebbdd2014-08-04 08:31:39 -0700595 }
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000596}
597
bsalomonf80bfed2014-10-07 05:56:02 -0700598void GrContext::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
599 const SkIPoint& dstPoint, uint32_t pixelOpsFlags) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800600 RETURN_IF_ABANDONED
robertphillipsea461502015-05-26 11:38:03 -0700601 if (!src || !dst) {
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000602 return;
603 }
bsalomone3d4bf22014-09-23 09:15:03 -0700604 ASSERT_OWNED_RESOURCE(src);
junov2bb52102014-09-29 10:18:59 -0700605 ASSERT_OWNED_RESOURCE(dst);
Brian Salomon34a98952014-09-24 11:41:24 -0400606
bsalomonf80bfed2014-10-07 05:56:02 -0700607 // Since we're going to the draw target and not GPU, no need to check kNoFlush
608 // here.
robertphillipsea461502015-05-26 11:38:03 -0700609 if (!dst->asRenderTarget()) {
junov96c118e2014-09-26 13:09:47 -0700610 return;
611 }
robertphillipsea461502015-05-26 11:38:03 -0700612
613 GrDrawContext* drawContext = this->drawContext();
614 if (!drawContext) {
615 return;
616 }
617
618 drawContext->copySurface(dst->asRenderTarget(), src, srcRect, dstPoint);
bsalomonf80bfed2014-10-07 05:56:02 -0700619
620 if (kFlushWrites_PixelOp & pixelOpsFlags) {
621 this->flush();
622 }
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +0000623}
624
bsalomonf80bfed2014-10-07 05:56:02 -0700625void GrContext::flushSurfaceWrites(GrSurface* surface) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800626 RETURN_IF_ABANDONED
bsalomonf80bfed2014-10-07 05:56:02 -0700627 if (surface->surfacePriv().hasPendingWrite()) {
628 this->flush();
629 }
630}
631
robertphillips@google.com72176b22012-05-23 13:19:12 +0000632/*
633 * This method finds a path renderer that can draw the specified path on
634 * the provided target.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000635 * Due to its expense, the software path renderer has split out so it can
robertphillips@google.com72176b22012-05-23 13:19:12 +0000636 * can be individually allowed/disallowed via the "allowSW" boolean.
637 */
joshualitt9853cce2014-11-17 14:22:48 -0800638GrPathRenderer* GrContext::getPathRenderer(const GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -0800639 const GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800640 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800641 const SkPath& path,
kkinnunen18996512015-04-26 23:18:49 -0700642 const GrStrokeInfo& stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000643 bool allowSW,
644 GrPathRendererChain::DrawType drawType,
645 GrPathRendererChain::StencilSupport* stencilSupport) {
646
robertphillipsea461502015-05-26 11:38:03 -0700647 if (!fPathRendererChain) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000648 fPathRendererChain = SkNEW_ARGS(GrPathRendererChain, (this));
bsalomon@google.com30085192011-08-19 15:42:31 +0000649 }
robertphillips@google.com72176b22012-05-23 13:19:12 +0000650
joshualitt9853cce2014-11-17 14:22:48 -0800651 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(target,
egdaniel8dd688b2015-01-22 10:16:09 -0800652 pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800653 viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800654 path,
sugoi@google.com12b4e272012-12-06 20:13:11 +0000655 stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000656 drawType,
657 stencilSupport);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000658
robertphillipsea461502015-05-26 11:38:03 -0700659 if (!pr && allowSW) {
660 if (!fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000661 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +0000662 }
robertphillips@google.com72176b22012-05-23 13:19:12 +0000663 pr = fSoftwarePathRenderer;
664 }
665
666 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +0000667}
668
bsalomon@google.com27847de2011-02-22 20:59:41 +0000669////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000670int GrContext::getRecommendedSampleCount(GrPixelConfig config,
671 SkScalar dpi) const {
bsalomon76228632015-05-29 08:02:10 -0700672 if (!this->caps()->isConfigRenderable(config, true)) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000673 return 0;
674 }
675 int chosenSampleCount = 0;
jvanverthe9c0fc62015-04-29 11:18:05 -0700676 if (fGpu->caps()->shaderCaps()->pathRenderingSupport()) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +0000677 if (dpi >= 250.0f) {
678 chosenSampleCount = 4;
679 } else {
680 chosenSampleCount = 16;
681 }
682 }
683 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ?
684 chosenSampleCount : 0;
685}
686
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000687namespace {
688void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
689 GrConfigConversionEffect::PMConversion pmToUPM;
690 GrConfigConversionEffect::PMConversion upmToPM;
691 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
692 *pmToUPMValue = pmToUPM;
693 *upmToPMValue = upmToPM;
694}
695}
696
joshualittb0a8a372014-09-23 09:50:21 -0700697const GrFragmentProcessor* GrContext::createPMToUPMEffect(GrTexture* texture,
698 bool swapRAndB,
699 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000700 if (!fDidTestPMConversions) {
701 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +0000702 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000703 }
704 GrConfigConversionEffect::PMConversion pmToUPM =
705 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
706 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +0000707 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000708 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +0000709 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000710 }
711}
712
joshualittb0a8a372014-09-23 09:50:21 -0700713const GrFragmentProcessor* GrContext::createUPMToPMEffect(GrTexture* texture,
714 bool swapRAndB,
715 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000716 if (!fDidTestPMConversions) {
717 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +0000718 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000719 }
720 GrConfigConversionEffect::PMConversion upmToPM =
721 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
722 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +0000723 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000724 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +0000725 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +0000726 }
727}
728
bsalomon37f9a262015-02-02 13:00:10 -0800729//////////////////////////////////////////////////////////////////////////////
730
731void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
732 if (maxTextures) {
bsalomon0ea80f42015-02-11 10:49:59 -0800733 *maxTextures = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -0800734 }
735 if (maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800736 *maxTextureBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -0800737 }
738}
739
740void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800741 fResourceCache->setLimits(maxTextures, maxTextureBytes);
bsalomon37f9a262015-02-02 13:00:10 -0800742}
743
bsalomon37f9a262015-02-02 13:00:10 -0800744//////////////////////////////////////////////////////////////////////////////
745
egdanielbbcb38d2014-06-19 10:19:29 -0700746void GrContext::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
747 fGpu->addGpuTraceMarker(marker);
egdanielbbcb38d2014-06-19 10:19:29 -0700748}
749
750void GrContext::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
751 fGpu->removeGpuTraceMarker(marker);
egdanielbbcb38d2014-06-19 10:19:29 -0700752}
joshualitte5902662015-05-11 08:18:35 -0700753