blob: 301c676af741f42fffd68524a43466b3d7acf6ac [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"
tomhudson@google.com278cbb42011-06-30 19:37:01 +000016#include "GrBufferAllocPool.h"
joshualitt5478d422014-11-14 16:00:38 -080017#include "GrDefaultGeoProcFactory.h"
bsalomon453cf402014-11-11 14:15:57 -080018#include "GrGpuResource.h"
bsalomon3582d3e2015-02-13 14:20:05 -080019#include "GrGpuResourcePriv.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000020#include "GrDrawTargetCaps.h"
joshualitt5478d422014-11-14 16:00:38 -080021#include "GrGpu.h"
commit-bot@chromium.orgdcb8ef92014-03-27 11:26:10 +000022#include "GrIndexBuffer.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000023#include "GrInOrderDrawBuffer.h"
robertphillips@google.come930a072014-04-03 00:34:27 +000024#include "GrLayerCache.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +000025#include "GrOvalRenderer.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000026#include "GrPathRenderer.h"
tomhudson@google.comd22b6e42011-06-24 15:53:40 +000027#include "GrPathUtils.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080028#include "GrRenderTargetPriv.h"
bsalomon0ea80f42015-02-11 10:49:59 -080029#include "GrResourceCache.h"
bsalomond309e7a2015-04-30 14:18:54 -070030#include "GrResourceProvider.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000031#include "GrSoftwarePathRenderer.h"
kkinnunenc6cb56f2014-06-24 00:12:27 -070032#include "GrStencilAndCoverTextContext.h"
egdanield58a0ba2014-06-11 10:30:05 -070033#include "GrStrokeInfo.h"
bsalomonafbf2d62014-09-30 12:18:44 -070034#include "GrSurfacePriv.h"
joshualittb7133be2015-04-08 09:08:31 -070035#include "GrTextBlobCache.h"
bsalomonafbf2d62014-09-30 12:18:44 -070036#include "GrTexturePriv.h"
egdanielbbcb38d2014-06-19 10:19:29 -070037#include "GrTraceMarker.h"
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +000038#include "GrTracing.h"
egdanield58a0ba2014-06-11 10:30:05 -070039#include "SkDashPathPriv.h"
bsalomon81beccc2014-10-13 12:32:55 -070040#include "SkConfig8888.h"
reed@google.com7111d462014-03-25 16:20:24 +000041#include "SkGr.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000042#include "SkRRect.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000043#include "SkStrokeRec.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
joshualitt5478d422014-11-14 16:00:38 -080048#include "effects/GrConfigConversionEffect.h"
49#include "effects/GrDashingEffect.h"
50#include "effects/GrSingleTextureEffect.h"
51
bsalomon@google.com60361492012-03-15 17:47:06 +000052static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
bsalomon@google.com27847de2011-02-22 20:59:41 +000053static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
54
bsalomon@google.com1d4edd32012-08-16 18:36:06 +000055static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = 1 << 11;
56static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = 4;
bsalomon@google.com27847de2011-02-22 20:59:41 +000057
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000058#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
joshualitt5f5a8d72015-02-25 14:09:45 -080059#define RETURN_IF_ABANDONED if (!fDrawBuffer) { return; }
60#define RETURN_FALSE_IF_ABANDONED if (!fDrawBuffer) { return false; }
61#define RETURN_NULL_IF_ABANDONED if (!fDrawBuffer) { return NULL; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000062
bsalomonf21dab92014-11-13 13:33:28 -080063class GrContext::AutoCheckFlush {
64public:
65 AutoCheckFlush(GrContext* context) : fContext(context) { SkASSERT(context); }
66
67 ~AutoCheckFlush() {
68 if (fContext->fFlushToReduceCacheSize) {
69 fContext->flush();
70 }
71 }
72
73private:
74 GrContext* fContext;
75};
76
krajcevski9c6d4d72014-08-12 07:26:25 -070077GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
78 const Options* opts) {
79 GrContext* context;
80 if (NULL == opts) {
81 context = SkNEW_ARGS(GrContext, (Options()));
82 } else {
83 context = SkNEW_ARGS(GrContext, (*opts));
84 }
85
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000086 if (context->init(backend, backendContext)) {
87 return context;
88 } else {
89 context->unref();
90 return NULL;
bsalomon@google.com27847de2011-02-22 20:59:41 +000091 }
bsalomon@google.com27847de2011-02-22 20:59:41 +000092}
93
krajcevski9c6d4d72014-08-12 07:26:25 -070094GrContext::GrContext(const Options& opts) : fOptions(opts) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000095 fGpu = NULL;
bsalomond309e7a2015-04-30 14:18:54 -070096 fResourceCache = NULL;
97 fResourceProvider = NULL;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000098 fPathRendererChain = NULL;
99 fSoftwarePathRenderer = NULL;
joshualitt7c3a2f82015-03-31 13:32:05 -0700100 fBatchFontCache = NULL;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000101 fDrawBuffer = NULL;
102 fDrawBufferVBAllocPool = NULL;
103 fDrawBufferIBAllocPool = NULL;
bsalomonf21dab92014-11-13 13:33:28 -0800104 fFlushToReduceCacheSize = false;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000105 fAARectRenderer = NULL;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000106 fOvalRenderer = NULL;
robertphillips@google.com44a91dc2013-07-25 15:32:06 +0000107 fMaxTextureSizeOverride = 1 << 20;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000108}
109
110bool GrContext::init(GrBackend backend, GrBackendContext backendContext) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000111 SkASSERT(NULL == fGpu);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000112
113 fGpu = GrGpu::Create(backend, backendContext, this);
114 if (NULL == fGpu) {
115 return false;
116 }
bsalomon33435572014-11-05 14:47:41 -0800117 this->initCommon();
118 return true;
119}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000120
bsalomon33435572014-11-05 14:47:41 -0800121void GrContext::initCommon() {
bsalomon0ea80f42015-02-11 10:49:59 -0800122 fResourceCache = SkNEW(GrResourceCache);
123 fResourceCache->setOverBudgetCallback(OverBudgetCB, this);
bsalomond309e7a2015-04-30 14:18:54 -0700124 fResourceProvider = SkNEW_ARGS(GrResourceProvider, (fGpu, fResourceCache));
commit-bot@chromium.org1836d332013-07-16 22:55:03 +0000125
robertphillips4ec84da2014-06-24 13:10:43 -0700126 fLayerCache.reset(SkNEW_ARGS(GrLayerCache, (this)));
robertphillips@google.come930a072014-04-03 00:34:27 +0000127
joshualittb44293e2014-10-28 08:12:18 -0700128 fAARectRenderer = SkNEW_ARGS(GrAARectRenderer, (fGpu));
joshualitt5531d512014-12-17 15:50:11 -0800129 fOvalRenderer = SkNEW_ARGS(GrOvalRenderer, (fGpu));
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000130
131 fDidTestPMConversions = false;
132
133 this->setupDrawBuffer();
joshualitt7c3a2f82015-03-31 13:32:05 -0700134
135 // GrBatchFontCache will eventually replace GrFontCache
joshualitt62db8ba2015-04-09 08:22:37 -0700136 fBatchFontCache = SkNEW_ARGS(GrBatchFontCache, (this));
joshualittb7133be2015-04-08 09:08:31 -0700137
joshualitt0db6dfa2015-04-10 07:01:30 -0700138 fTextBlobCache.reset(SkNEW_ARGS(GrTextBlobCache, (TextBlobCacheOverBudgetCB, this)));
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000139}
140
bsalomon@google.com27847de2011-02-22 20:59:41 +0000141GrContext::~GrContext() {
bsalomon@google.com733c0622013-04-24 17:59:32 +0000142 if (NULL == fGpu) {
143 return;
144 }
145
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000146 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000147
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000148 for (int i = 0; i < fCleanUpData.count(); ++i) {
149 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
150 }
151
bsalomond309e7a2015-04-30 14:18:54 -0700152 SkDELETE(fResourceProvider);
bsalomon0ea80f42015-02-11 10:49:59 -0800153 SkDELETE(fResourceCache);
joshualitt7c3a2f82015-03-31 13:32:05 -0700154 SkDELETE(fBatchFontCache);
bsalomon33435572014-11-05 14:47:41 -0800155 SkDELETE(fDrawBuffer);
156 SkDELETE(fDrawBufferVBAllocPool);
157 SkDELETE(fDrawBufferIBAllocPool);
bsalomon@google.com30085192011-08-19 15:42:31 +0000158
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000159 fAARectRenderer->unref();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000160 fOvalRenderer->unref();
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000161
bsalomon@google.com205d4602011-04-25 12:43:45 +0000162 fGpu->unref();
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000163 SkSafeUnref(fPathRendererChain);
164 SkSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000165}
166
bsalomon2354f842014-07-28 13:48:36 -0700167void GrContext::abandonContext() {
bsalomond309e7a2015-04-30 14:18:54 -0700168 fResourceProvider->abandon();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000169 // abandon first to so destructors
170 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800171 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700172
robertphillipse3371302014-09-17 06:01:06 -0700173 fGpu->contextAbandoned();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000174
bsalomon@google.com30085192011-08-19 15:42:31 +0000175 // a path renderer may be holding onto resources that
176 // are now unusable
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000177 SkSafeSetNull(fPathRendererChain);
178 SkSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000179
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000180 delete fDrawBuffer;
181 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000182
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000183 delete fDrawBufferVBAllocPool;
184 fDrawBufferVBAllocPool = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000185
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000186 delete fDrawBufferIBAllocPool;
187 fDrawBufferIBAllocPool = NULL;
188
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000189 fAARectRenderer->reset();
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000190 fOvalRenderer->reset();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000191
joshualitt7c3a2f82015-03-31 13:32:05 -0700192 fBatchFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000193 fLayerCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700194 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000195}
196
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000197void GrContext::resetContext(uint32_t state) {
198 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000199}
200
201void GrContext::freeGpuResources() {
202 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000203
bsalomon49f085d2014-09-05 13:34:00 -0700204 if (fDrawBuffer) {
bsalomonc8dc1f72014-08-21 13:02:13 -0700205 fDrawBuffer->purgeResources();
206 }
robertphillips@google.comff175842012-05-14 19:31:39 +0000207
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000208 fAARectRenderer->reset();
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000209 fOvalRenderer->reset();
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000210
joshualitt7c3a2f82015-03-31 13:32:05 -0700211 fBatchFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000212 fLayerCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000213 // a path renderer may be holding onto resources
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000214 SkSafeSetNull(fPathRendererChain);
215 SkSafeSetNull(fSoftwarePathRenderer);
bsalomon3033b9f2015-04-13 11:09:56 -0700216
217 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000218}
219
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000220void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
bsalomon71cb0c22014-11-14 12:10:14 -0800221 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800222 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800223 }
224 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800225 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800226 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000227}
228
kkinnunenc6cb56f2014-06-24 00:12:27 -0700229GrTextContext* GrContext::createTextContext(GrRenderTarget* renderTarget,
joshualitt6e8cd962015-03-20 10:30:14 -0700230 SkGpuDevice* gpuDevice,
kkinnunenc6cb56f2014-06-24 00:12:27 -0700231 const SkDeviceProperties&
232 leakyProperties,
233 bool enableDistanceFieldFonts) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700234 if (fGpu->caps()->shaderCaps()->pathRenderingSupport() && renderTarget->isMultisampled()) {
egdaniel8dc7c3a2015-04-16 11:22:42 -0700235 GrStencilAttachment* sb = renderTarget->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800236 if (sb) {
joshualitt6e8cd962015-03-20 10:30:14 -0700237 return GrStencilAndCoverTextContext::Create(this, gpuDevice, leakyProperties);
bsalomon6bc1b5f2015-02-23 09:06:38 -0800238 }
jvanverth8c27a182014-10-14 08:45:50 -0700239 }
240
joshualitt9bd2daf2015-04-17 09:30:06 -0700241 return GrAtlasTextContext::Create(this, gpuDevice, leakyProperties, enableDistanceFieldFonts);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700242}
243
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000244////////////////////////////////////////////////////////////////////////////////
245
bsalomond2a6f4e2015-02-04 10:55:54 -0800246bool GrContext::isConfigTexturable(GrPixelConfig config) const {
247 return fGpu->caps()->isConfigTexturable(config);
248}
249
250bool GrContext::npotTextureTileSupport() const {
251 return fGpu->caps()->npotTextureTileSupport();
252}
253
bsalomon71cb0c22014-11-14 12:10:14 -0800254void GrContext::OverBudgetCB(void* data) {
bsalomon66a450f2014-11-13 13:19:10 -0800255 SkASSERT(data);
bsalomonf21dab92014-11-13 13:33:28 -0800256
bsalomon66a450f2014-11-13 13:19:10 -0800257 GrContext* context = reinterpret_cast<GrContext*>(data);
bsalomonf21dab92014-11-13 13:33:28 -0800258
259 // Flush the InOrderDrawBuffer to possibly free up some textures
260 context->fFlushToReduceCacheSize = true;
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000261}
262
joshualitt0db6dfa2015-04-10 07:01:30 -0700263void GrContext::TextBlobCacheOverBudgetCB(void* data) {
264 SkASSERT(data);
265
266 // Unlike the GrResourceCache, TextBlobs are drawn at the SkGpuDevice level, therefore they
267 // cannot use fFlushTorReduceCacheSize because it uses AutoCheckFlush. The solution is to move
268 // drawText calls to below the GrContext level, but this is not trivial because they call
269 // drawPath on SkGpuDevice
270 GrContext* context = reinterpret_cast<GrContext*>(data);
271 context->flush();
272}
273
bsalomon@google.com91958362011-06-13 17:58:13 +0000274int GrContext::getMaxTextureSize() const {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000275 return SkTMin(fGpu->caps()->maxTextureSize(), fMaxTextureSizeOverride);
bsalomon@google.com91958362011-06-13 17:58:13 +0000276}
277
278int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000279 return fGpu->caps()->maxRenderTargetSize();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000280}
281
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000282int GrContext::getMaxSampleCount() const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000283 return fGpu->caps()->maxSampleCount();
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000284}
285
bsalomon@google.com27847de2011-02-22 20:59:41 +0000286///////////////////////////////////////////////////////////////////////////////
287
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000288void GrContext::clear(const SkIRect* rect,
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000289 const GrColor color,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000290 bool canIgnoreRect,
bsalomon41ebbdd2014-08-04 08:31:39 -0700291 GrRenderTarget* renderTarget) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800292 RETURN_IF_ABANDONED
bsalomon41ebbdd2014-08-04 08:31:39 -0700293 ASSERT_OWNED_RESOURCE(renderTarget);
bsalomon89c62982014-11-03 12:08:42 -0800294 SkASSERT(renderTarget);
295
bsalomonf21dab92014-11-13 13:33:28 -0800296 AutoCheckFlush acf(this);
egdanield78a1682014-07-09 10:41:26 -0700297 GR_CREATE_TRACE_MARKER_CONTEXT("GrContext::clear", this);
joshualitt570d2f82015-02-25 13:19:48 -0800298 GrDrawTarget* target = this->prepareToDraw();
bsalomon41ebbdd2014-08-04 08:31:39 -0700299 if (NULL == target) {
300 return;
301 }
302 target->clear(rect, color, canIgnoreRect, renderTarget);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000303}
304
joshualitt25d9c152015-02-18 12:29:52 -0800305void GrContext::drawPaint(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800306 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800307 const GrPaint& origPaint,
308 const SkMatrix& viewMatrix) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800309 RETURN_IF_ABANDONED
bsalomon@google.com27847de2011-02-22 20:59:41 +0000310 // set rect to be big enough to fill the space, but not super-huge, so we
311 // don't overflow fixed-point implementations
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000312 SkRect r;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000313 r.setLTRB(0, 0,
joshualitt25d9c152015-02-18 12:29:52 -0800314 SkIntToScalar(rt->width()),
315 SkIntToScalar(rt->height()));
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000316 SkTCopyOnFirstWrite<GrPaint> paint(origPaint);
joshualitt8fc6c2d2014-12-22 15:27:05 -0800317
318 // by definition this fills the entire clip, no need for AA
319 if (paint->isAntiAlias()) {
320 paint.writable()->setAntiAlias(false);
321 }
322
323 bool isPerspective = viewMatrix.hasPerspective();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000324
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000325 // We attempt to map r by the inverse matrix and draw that. mapRect will
326 // map the four corners and bound them with a new rect. This will not
327 // produce a correct result for some perspective matrices.
joshualitt8fc6c2d2014-12-22 15:27:05 -0800328 if (!isPerspective) {
joshualitt5531d512014-12-17 15:50:11 -0800329 SkMatrix inverse;
330 if (!viewMatrix.invert(&inverse)) {
tfarina38406c82014-10-31 07:11:12 -0700331 SkDebugf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000332 return;
333 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000334 inverse.mapRect(&r);
joshualitt570d2f82015-02-25 13:19:48 -0800335 this->drawRect(rt, clip, *paint, viewMatrix, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000336 } else {
joshualitt8fc6c2d2014-12-22 15:27:05 -0800337 SkMatrix localMatrix;
338 if (!viewMatrix.invert(&localMatrix)) {
tfarina38406c82014-10-31 07:11:12 -0700339 SkDebugf("Could not invert matrix\n");
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000340 return;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000341 }
joshualitt8fc6c2d2014-12-22 15:27:05 -0800342
343 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -0800344 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -0800345 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, paint, &acf);
joshualitt8fc6c2d2014-12-22 15:27:05 -0800346 if (NULL == target) {
347 return;
348 }
349
350 GR_CREATE_TRACE_MARKER("GrContext::drawPaintWithPerspective", target);
joshualitt44701df2015-02-23 14:44:57 -0800351 target->drawRect(&pipelineBuilder,
352 paint->getColor(),
353 SkMatrix::I(),
354 r,
355 NULL,
356 &localMatrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000357 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000358}
359
bsalomon@google.com205d4602011-04-25 12:43:45 +0000360////////////////////////////////////////////////////////////////////////////////
361
bsalomonc30aaa02014-08-13 07:15:29 -0700362static inline bool is_irect(const SkRect& r) {
tfarina38406c82014-10-31 07:11:12 -0700363 return SkScalarIsInt(r.fLeft) && SkScalarIsInt(r.fTop) &&
364 SkScalarIsInt(r.fRight) && SkScalarIsInt(r.fBottom);
bsalomonc30aaa02014-08-13 07:15:29 -0700365}
366
bsalomon@google.com205d4602011-04-25 12:43:45 +0000367static bool apply_aa_to_rect(GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -0800368 GrPipelineBuilder* pipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -0800369 SkRect* devBoundRect,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000370 const SkRect& rect,
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000371 SkScalar strokeWidth,
joshualitt2e3b3e32014-12-09 13:31:14 -0800372 const SkMatrix& combinedMatrix,
373 GrColor color) {
egdaniel8dd688b2015-01-22 10:16:09 -0800374 if (pipelineBuilder->getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000375 return false;
376 }
377
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000378#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000379 if (strokeWidth >= 0) {
380#endif
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000381 if (!combinedMatrix.preservesAxisAlignment()) {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000382 return false;
383 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000384
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000385#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000386 } else {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000387 if (!combinedMatrix.preservesRightAngles()) {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000388 return false;
389 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000390 }
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000391#endif
bsalomon@google.com205d4602011-04-25 12:43:45 +0000392
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000393 combinedMatrix.mapRect(devBoundRect, rect);
derekfe638d1c2014-12-02 13:51:29 -0800394 if (!combinedMatrix.rectStaysRect()) {
395 return true;
396 }
397
bsalomonc30aaa02014-08-13 07:15:29 -0700398 if (strokeWidth < 0) {
399 return !is_irect(*devBoundRect);
400 }
robertphillips@google.com28ac96e2013-05-13 13:38:35 +0000401
bsalomon9c0822a2014-08-11 11:07:48 -0700402 return true;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000403}
404
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000405static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) {
406 return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
407 point.fY >= rect.fTop && point.fY <= rect.fBottom;
408}
409
joshualitt0823bfa2015-02-27 10:06:15 -0800410class StrokeRectBatch : public GrBatch {
411public:
412 struct Geometry {
413 GrColor fColor;
414 SkMatrix fViewMatrix;
415 SkRect fRect;
416 SkScalar fStrokeWidth;
417 };
418
419 static GrBatch* Create(const Geometry& geometry) {
420 return SkNEW_ARGS(StrokeRectBatch, (geometry));
421 }
422
mtklein36352bf2015-03-25 18:17:31 -0700423 const char* name() const override { return "StrokeRectBatch"; }
joshualitt0823bfa2015-02-27 10:06:15 -0800424
mtklein36352bf2015-03-25 18:17:31 -0700425 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt0823bfa2015-02-27 10:06:15 -0800426 // When this is called on a batch, there is only one geometry bundle
427 out->setKnownFourComponents(fGeoData[0].fColor);
428 }
429
mtklein36352bf2015-03-25 18:17:31 -0700430 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualitt0823bfa2015-02-27 10:06:15 -0800431 out->setKnownSingleComponent(0xff);
432 }
433
mtklein36352bf2015-03-25 18:17:31 -0700434 void initBatchTracker(const GrPipelineInfo& init) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800435 // Handle any color overrides
436 if (init.fColorIgnored) {
437 fGeoData[0].fColor = GrColor_ILLEGAL;
438 } else if (GrColor_ILLEGAL != init.fOverrideColor) {
439 fGeoData[0].fColor = init.fOverrideColor;
440 }
441
442 // setup batch properties
443 fBatch.fColorIgnored = init.fColorIgnored;
444 fBatch.fColor = fGeoData[0].fColor;
445 fBatch.fUsesLocalCoords = init.fUsesLocalCoords;
446 fBatch.fCoverageIgnored = init.fCoverageIgnored;
447 }
448
mtklein36352bf2015-03-25 18:17:31 -0700449 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800450 SkAutoTUnref<const GrGeometryProcessor> gp(
451 GrDefaultGeoProcFactory::Create(GrDefaultGeoProcFactory::kPosition_GPType,
452 this->color(),
453 this->viewMatrix(),
454 SkMatrix::I()));
455
456 batchTarget->initDraw(gp, pipeline);
457
458 // TODO this is hacky, but the only way we have to initialize the GP is to use the
459 // GrPipelineInfo struct so we can generate the correct shader. Once we have GrBatch
460 // everywhere we can remove this nastiness
461 GrPipelineInfo init;
462 init.fColorIgnored = fBatch.fColorIgnored;
463 init.fOverrideColor = GrColor_ILLEGAL;
464 init.fCoverageIgnored = fBatch.fCoverageIgnored;
465 init.fUsesLocalCoords = this->usesLocalCoords();
466 gp->initBatchTracker(batchTarget->currentBatchTracker(), init);
467
468 size_t vertexStride = gp->getVertexStride();
469
470 SkASSERT(vertexStride == sizeof(GrDefaultGeoProcFactory::PositionAttr));
471
472 Geometry& args = fGeoData[0];
473
474 int vertexCount = kVertsPerHairlineRect;
475 if (args.fStrokeWidth > 0) {
476 vertexCount = kVertsPerStrokeRect;
477 }
478
479 const GrVertexBuffer* vertexBuffer;
480 int firstVertex;
481
482 void* vertices = batchTarget->vertexPool()->makeSpace(vertexStride,
483 vertexCount,
484 &vertexBuffer,
485 &firstVertex);
486
joshualitt4b31de82015-03-05 14:33:41 -0800487 if (!vertices) {
488 SkDebugf("Could not allocate vertices\n");
489 return;
490 }
491
joshualitt0823bfa2015-02-27 10:06:15 -0800492 SkPoint* vertex = reinterpret_cast<SkPoint*>(vertices);
493
494 GrPrimitiveType primType;
495
496 if (args.fStrokeWidth > 0) {;
497 primType = kTriangleStrip_GrPrimitiveType;
498 args.fRect.sort();
499 this->setStrokeRectStrip(vertex, args.fRect, args.fStrokeWidth);
500 } else {
501 // hairline
502 primType = kLineStrip_GrPrimitiveType;
503 vertex[0].set(args.fRect.fLeft, args.fRect.fTop);
504 vertex[1].set(args.fRect.fRight, args.fRect.fTop);
505 vertex[2].set(args.fRect.fRight, args.fRect.fBottom);
506 vertex[3].set(args.fRect.fLeft, args.fRect.fBottom);
507 vertex[4].set(args.fRect.fLeft, args.fRect.fTop);
508 }
509
510 GrDrawTarget::DrawInfo drawInfo;
511 drawInfo.setPrimitiveType(primType);
512 drawInfo.setVertexBuffer(vertexBuffer);
513 drawInfo.setStartVertex(firstVertex);
514 drawInfo.setVertexCount(vertexCount);
515 drawInfo.setStartIndex(0);
516 drawInfo.setIndexCount(0);
517 drawInfo.setInstanceCount(0);
518 drawInfo.setVerticesPerInstance(0);
519 drawInfo.setIndicesPerInstance(0);
520 batchTarget->draw(drawInfo);
521 }
522
523 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
524
525private:
526 StrokeRectBatch(const Geometry& geometry) {
527 this->initClassID<StrokeRectBatch>();
528
529 fBatch.fHairline = geometry.fStrokeWidth == 0;
530
531 fGeoData.push_back(geometry);
532 }
533
534 /* create a triangle strip that strokes the specified rect. There are 8
535 unique vertices, but we repeat the last 2 to close up. Alternatively we
536 could use an indices array, and then only send 8 verts, but not sure that
537 would be faster.
538 */
539 void setStrokeRectStrip(SkPoint verts[10], const SkRect& rect, SkScalar width) {
540 const SkScalar rad = SkScalarHalf(width);
541 // TODO we should be able to enable this assert, but we'd have to filter these draws
542 // this is a bug
543 //SkASSERT(rad < rect.width() / 2 && rad < rect.height() / 2);
544
545 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
546 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
547 verts[2].set(rect.fRight - rad, rect.fTop + rad);
548 verts[3].set(rect.fRight + rad, rect.fTop - rad);
549 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
550 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
551 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
552 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
553 verts[8] = verts[0];
554 verts[9] = verts[1];
555 }
556
557
558 GrColor color() const { return fBatch.fColor; }
559 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
560 bool colorIgnored() const { return fBatch.fColorIgnored; }
561 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
562 bool hairline() const { return fBatch.fHairline; }
563
mtklein36352bf2015-03-25 18:17:31 -0700564 bool onCombineIfPossible(GrBatch* t) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800565 // StrokeRectBatch* that = t->cast<StrokeRectBatch>();
566
567 // NonAA stroke rects cannot batch right now
568 // TODO make these batchable
569 return false;
570 }
571
572 struct BatchTracker {
573 GrColor fColor;
574 bool fUsesLocalCoords;
575 bool fColorIgnored;
576 bool fCoverageIgnored;
577 bool fHairline;
578 };
579
580 const static int kVertsPerHairlineRect = 5;
581 const static int kVertsPerStrokeRect = 10;
582
583 BatchTracker fBatch;
584 SkSTArray<1, Geometry, true> fGeoData;
585};
586
joshualitt25d9c152015-02-18 12:29:52 -0800587void GrContext::drawRect(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800588 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800589 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -0800590 const SkMatrix& viewMatrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000591 const SkRect& rect,
bsalomon01c8da12014-08-04 09:21:30 -0700592 const GrStrokeInfo* strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800593 RETURN_IF_ABANDONED
bsalomon49f085d2014-09-05 13:34:00 -0700594 if (strokeInfo && strokeInfo->isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700595 SkPath path;
596 path.addRect(rect);
joshualitt570d2f82015-02-25 13:19:48 -0800597 this->drawPath(rt, clip, paint, viewMatrix, path, *strokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -0700598 return;
599 }
600
bsalomonf21dab92014-11-13 13:33:28 -0800601 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -0800602 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -0800603 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700604 if (NULL == target) {
605 return;
606 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000607
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000608 GR_CREATE_TRACE_MARKER("GrContext::drawRect", target);
egdanield58a0ba2014-06-11 10:30:05 -0700609 SkScalar width = NULL == strokeInfo ? -1 : strokeInfo->getStrokeRec().getWidth();
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000610
611 // Check if this is a full RT draw and can be replaced with a clear. We don't bother checking
612 // cases where the RT is fully inside a stroke.
613 if (width < 0) {
614 SkRect rtRect;
egdaniel8dd688b2015-01-22 10:16:09 -0800615 pipelineBuilder.getRenderTarget()->getBoundsRect(&rtRect);
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000616 SkRect clipSpaceRTRect = rtRect;
joshualitt570d2f82015-02-25 13:19:48 -0800617 bool checkClip = GrClip::kWideOpen_ClipType != clip.clipType();
joshualitt44701df2015-02-23 14:44:57 -0800618 if (checkClip) {
joshualitt570d2f82015-02-25 13:19:48 -0800619 clipSpaceRTRect.offset(SkIntToScalar(clip.origin().fX),
620 SkIntToScalar(clip.origin().fY));
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000621 }
622 // Does the clip contain the entire RT?
joshualitt570d2f82015-02-25 13:19:48 -0800623 if (!checkClip || clip.quickContains(clipSpaceRTRect)) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000624 SkMatrix invM;
joshualitt8059eb92014-12-29 15:10:07 -0800625 if (!viewMatrix.invert(&invM)) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000626 return;
627 }
628 // Does the rect bound the RT?
629 SkPoint srcSpaceRTQuad[4];
630 invM.mapRectToQuad(srcSpaceRTQuad, rtRect);
631 if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) &&
632 rect_contains_inclusive(rect, srcSpaceRTQuad[1]) &&
633 rect_contains_inclusive(rect, srcSpaceRTQuad[2]) &&
634 rect_contains_inclusive(rect, srcSpaceRTQuad[3])) {
635 // Will it blend?
636 GrColor clearColor;
637 if (paint.isOpaqueAndConstantColor(&clearColor)) {
joshualitt25d9c152015-02-18 12:29:52 -0800638 target->clear(NULL, clearColor, true, rt);
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000639 return;
640 }
641 }
642 }
643 }
644
joshualitt2e3b3e32014-12-09 13:31:14 -0800645 GrColor color = paint.getColor();
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000646 SkRect devBoundRect;
egdaniel8dd688b2015-01-22 10:16:09 -0800647 bool needAA = paint.isAntiAlias() && !pipelineBuilder.getRenderTarget()->isMultisampled();
648 bool doAA = needAA && apply_aa_to_rect(target, &pipelineBuilder, &devBoundRect, rect, width,
joshualitt8059eb92014-12-29 15:10:07 -0800649 viewMatrix, color);
egdanield58a0ba2014-06-11 10:30:05 -0700650
bsalomon@google.com205d4602011-04-25 12:43:45 +0000651 if (doAA) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000652 if (width >= 0) {
bsalomon395ef052014-11-12 11:35:22 -0800653 const SkStrokeRec& strokeRec = strokeInfo->getStrokeRec();
joshualitt9853cce2014-11-17 14:22:48 -0800654 fAARectRenderer->strokeAARect(target,
egdaniel8dd688b2015-01-22 10:16:09 -0800655 &pipelineBuilder,
joshualitt2e3b3e32014-12-09 13:31:14 -0800656 color,
joshualitt8059eb92014-12-29 15:10:07 -0800657 viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800658 rect,
joshualitt9853cce2014-11-17 14:22:48 -0800659 devBoundRect,
660 strokeRec);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000661 } else {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000662 // filled AA rect
joshualitt8059eb92014-12-29 15:10:07 -0800663 fAARectRenderer->fillAARect(target,
egdaniel8dd688b2015-01-22 10:16:09 -0800664 &pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800665 color,
666 viewMatrix,
667 rect,
joshualittd27f73e2014-12-29 07:43:36 -0800668 devBoundRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000669 }
670 return;
671 }
672
bsalomon@google.com27847de2011-02-22 20:59:41 +0000673 if (width >= 0) {
joshualitt0823bfa2015-02-27 10:06:15 -0800674 StrokeRectBatch::Geometry geometry;
675 geometry.fViewMatrix = viewMatrix;
676 geometry.fColor = color;
677 geometry.fRect = rect;
678 geometry.fStrokeWidth = width;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000679
joshualitt0823bfa2015-02-27 10:06:15 -0800680 SkAutoTUnref<GrBatch> batch(StrokeRectBatch::Create(geometry));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000681
joshualitt0823bfa2015-02-27 10:06:15 -0800682 SkRect bounds = rect;
683 SkScalar rad = SkScalarHalf(width);
684 bounds.outset(rad, rad);
685 viewMatrix.mapRect(&bounds);
bsalomond79c5492015-04-27 10:07:04 -0700686 // Depending on sub-pixel coordinates and the particular GPU, we may lose a corner of
687 // hairline rects. We jam all the vertices to pixel centers to avoid this, but not when MSAA
688 // is enabled because it can cause ugly artifacts.
689 pipelineBuilder.setState(GrPipelineBuilder::kSnapVerticesToPixelCenters_Flag,
690 0 == width && !rt->isMultisampled());
joshualitt0823bfa2015-02-27 10:06:15 -0800691 target->drawBatch(&pipelineBuilder, batch, &bounds);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000692 } else {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000693 // filled BW rect
egdaniel8dd688b2015-01-22 10:16:09 -0800694 target->drawSimpleRect(&pipelineBuilder, color, viewMatrix, rect);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000695 }
696}
697
joshualitt25d9c152015-02-18 12:29:52 -0800698void GrContext::drawNonAARectToRect(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800699 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800700 const GrPaint& paint,
joshualitt16b27892014-12-18 07:47:16 -0800701 const SkMatrix& viewMatrix,
702 const SkRect& rectToDraw,
703 const SkRect& localRect,
704 const SkMatrix* localMatrix) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800705 RETURN_IF_ABANDONED
bsalomonf21dab92014-11-13 13:33:28 -0800706 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -0800707 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -0800708 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700709 if (NULL == target) {
710 return;
711 }
bsalomon@google.com64386952013-02-08 21:22:44 +0000712
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000713 GR_CREATE_TRACE_MARKER("GrContext::drawRectToRect", target);
714
joshualitt44701df2015-02-23 14:44:57 -0800715 target->drawRect(&pipelineBuilder,
716 paint.getColor(),
717 viewMatrix,
718 rectToDraw,
719 &localRect,
egdaniel8dd688b2015-01-22 10:16:09 -0800720 localMatrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000721}
722
joshualitt0823bfa2015-02-27 10:06:15 -0800723static const GrGeometryProcessor* set_vertex_attributes(bool hasLocalCoords,
724 bool hasColors,
joshualitt56995b52014-12-11 15:44:02 -0800725 int* colorOffset,
726 int* texOffset,
joshualitt8059eb92014-12-29 15:10:07 -0800727 GrColor color,
728 const SkMatrix& viewMatrix) {
robertphillips@google.com42903302013-04-20 12:26:07 +0000729 *texOffset = -1;
730 *colorOffset = -1;
joshualitt5478d422014-11-14 16:00:38 -0800731 uint32_t flags = GrDefaultGeoProcFactory::kPosition_GPType;
joshualitt0823bfa2015-02-27 10:06:15 -0800732 if (hasLocalCoords && hasColors) {
joshualitt5478d422014-11-14 16:00:38 -0800733 *colorOffset = sizeof(SkPoint);
734 *texOffset = sizeof(SkPoint) + sizeof(GrColor);
735 flags |= GrDefaultGeoProcFactory::kColor_GPType |
736 GrDefaultGeoProcFactory::kLocalCoord_GPType;
joshualitt0823bfa2015-02-27 10:06:15 -0800737 } else if (hasLocalCoords) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000738 *texOffset = sizeof(SkPoint);
joshualitt5478d422014-11-14 16:00:38 -0800739 flags |= GrDefaultGeoProcFactory::kLocalCoord_GPType;
joshualitt0823bfa2015-02-27 10:06:15 -0800740 } else if (hasColors) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000741 *colorOffset = sizeof(SkPoint);
joshualitt5478d422014-11-14 16:00:38 -0800742 flags |= GrDefaultGeoProcFactory::kColor_GPType;
robertphillips@google.com42903302013-04-20 12:26:07 +0000743 }
joshualitt8059eb92014-12-29 15:10:07 -0800744 return GrDefaultGeoProcFactory::Create(flags, color, viewMatrix, SkMatrix::I());
robertphillips@google.com42903302013-04-20 12:26:07 +0000745}
746
joshualitt0823bfa2015-02-27 10:06:15 -0800747class DrawVerticesBatch : public GrBatch {
748public:
749 struct Geometry {
750 GrColor fColor;
751 SkTDArray<SkPoint> fPositions;
752 SkTDArray<uint16_t> fIndices;
753 SkTDArray<GrColor> fColors;
754 SkTDArray<SkPoint> fLocalCoords;
755 };
756
757 static GrBatch* Create(const Geometry& geometry, GrPrimitiveType primitiveType,
758 const SkMatrix& viewMatrix,
759 const SkPoint* positions, int vertexCount,
760 const uint16_t* indices, int indexCount,
761 const GrColor* colors, const SkPoint* localCoords) {
762 return SkNEW_ARGS(DrawVerticesBatch, (geometry, primitiveType, viewMatrix, positions,
763 vertexCount, indices, indexCount, colors,
764 localCoords));
765 }
766
mtklein36352bf2015-03-25 18:17:31 -0700767 const char* name() const override { return "DrawVerticesBatch"; }
joshualitt0823bfa2015-02-27 10:06:15 -0800768
mtklein36352bf2015-03-25 18:17:31 -0700769 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt0823bfa2015-02-27 10:06:15 -0800770 // When this is called on a batch, there is only one geometry bundle
771 if (this->hasColors()) {
772 out->setUnknownFourComponents();
773 } else {
774 out->setKnownFourComponents(fGeoData[0].fColor);
775 }
776 }
777
mtklein36352bf2015-03-25 18:17:31 -0700778 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualittd4c7d552015-02-27 13:00:56 -0800779 out->setKnownSingleComponent(0xff);
joshualitt0823bfa2015-02-27 10:06:15 -0800780 }
781
mtklein36352bf2015-03-25 18:17:31 -0700782 void initBatchTracker(const GrPipelineInfo& init) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800783 // Handle any color overrides
784 if (init.fColorIgnored) {
785 fGeoData[0].fColor = GrColor_ILLEGAL;
786 } else if (GrColor_ILLEGAL != init.fOverrideColor) {
787 fGeoData[0].fColor = init.fOverrideColor;
788 }
789
790 // setup batch properties
791 fBatch.fColorIgnored = init.fColorIgnored;
792 fBatch.fColor = fGeoData[0].fColor;
793 fBatch.fUsesLocalCoords = init.fUsesLocalCoords;
794 fBatch.fCoverageIgnored = init.fCoverageIgnored;
795 }
796
mtklein36352bf2015-03-25 18:17:31 -0700797 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800798 int colorOffset = -1, texOffset = -1;
799 SkAutoTUnref<const GrGeometryProcessor> gp(
800 set_vertex_attributes(this->hasLocalCoords(), this->hasColors(), &colorOffset,
801 &texOffset, this->color(), this->viewMatrix()));
802
803 batchTarget->initDraw(gp, pipeline);
804
805 // TODO this is hacky, but the only way we have to initialize the GP is to use the
806 // GrPipelineInfo struct so we can generate the correct shader. Once we have GrBatch
807 // everywhere we can remove this nastiness
808 GrPipelineInfo init;
809 init.fColorIgnored = fBatch.fColorIgnored;
810 init.fOverrideColor = GrColor_ILLEGAL;
811 init.fCoverageIgnored = fBatch.fCoverageIgnored;
812 init.fUsesLocalCoords = this->usesLocalCoords();
813 gp->initBatchTracker(batchTarget->currentBatchTracker(), init);
814
815 size_t vertexStride = gp->getVertexStride();
816
817 SkASSERT(vertexStride == sizeof(SkPoint) + (this->hasLocalCoords() ? sizeof(SkPoint) : 0)
818 + (this->hasColors() ? sizeof(GrColor) : 0));
819
820 int instanceCount = fGeoData.count();
821
822 const GrVertexBuffer* vertexBuffer;
823 int firstVertex;
824
825 void* vertices = batchTarget->vertexPool()->makeSpace(vertexStride,
826 this->vertexCount(),
827 &vertexBuffer,
828 &firstVertex);
829
joshualitt4b31de82015-03-05 14:33:41 -0800830 if (!vertices) {
831 SkDebugf("Could not allocate vertices\n");
832 return;
833 }
834
joshualitt0823bfa2015-02-27 10:06:15 -0800835 const GrIndexBuffer* indexBuffer;
836 int firstIndex;
837
838 void* indices = NULL;
839 if (this->hasIndices()) {
840 indices = batchTarget->indexPool()->makeSpace(this->indexCount(),
841 &indexBuffer,
842 &firstIndex);
joshualitt4b31de82015-03-05 14:33:41 -0800843
844 if (!indices) {
845 SkDebugf("Could not allocate indices\n");
846 return;
847 }
joshualitt0823bfa2015-02-27 10:06:15 -0800848 }
849
850 int indexOffset = 0;
851 int vertexOffset = 0;
852 for (int i = 0; i < instanceCount; i++) {
853 const Geometry& args = fGeoData[i];
854
855 // TODO we can actually cache this interleaved and then just memcopy
856 if (this->hasIndices()) {
857 for (int j = 0; j < args.fIndices.count(); ++j, ++indexOffset) {
858 *((uint16_t*)indices + indexOffset) = args.fIndices[j] + vertexOffset;
859 }
860 }
861
862 for (int j = 0; j < args.fPositions.count(); ++j) {
863 *((SkPoint*)vertices) = args.fPositions[j];
864 if (this->hasColors()) {
865 *(GrColor*)((intptr_t)vertices + colorOffset) = args.fColors[j];
866 }
867 if (this->hasLocalCoords()) {
868 *(SkPoint*)((intptr_t)vertices + texOffset) = args.fLocalCoords[j];
869 }
870 vertices = (void*)((intptr_t)vertices + vertexStride);
871 vertexOffset++;
872 }
873 }
874
875 GrDrawTarget::DrawInfo drawInfo;
876 drawInfo.setPrimitiveType(this->primitiveType());
877 drawInfo.setVertexBuffer(vertexBuffer);
878 drawInfo.setStartVertex(firstVertex);
879 drawInfo.setVertexCount(this->vertexCount());
880 if (this->hasIndices()) {
881 drawInfo.setIndexBuffer(indexBuffer);
882 drawInfo.setStartIndex(firstIndex);
883 drawInfo.setIndexCount(this->indexCount());
884 } else {
885 drawInfo.setStartIndex(0);
886 drawInfo.setIndexCount(0);
887 }
888 batchTarget->draw(drawInfo);
889 }
890
891 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
892
893private:
894 DrawVerticesBatch(const Geometry& geometry, GrPrimitiveType primitiveType,
895 const SkMatrix& viewMatrix,
896 const SkPoint* positions, int vertexCount,
897 const uint16_t* indices, int indexCount,
898 const GrColor* colors, const SkPoint* localCoords) {
899 this->initClassID<DrawVerticesBatch>();
900 SkASSERT(positions);
901
902 fBatch.fViewMatrix = viewMatrix;
903 Geometry& installedGeo = fGeoData.push_back(geometry);
904
905 installedGeo.fPositions.append(vertexCount, positions);
906 if (indices) {
907 installedGeo.fIndices.append(indexCount, indices);
908 fBatch.fHasIndices = true;
909 } else {
910 fBatch.fHasIndices = false;
911 }
912
913 if (colors) {
914 installedGeo.fColors.append(vertexCount, colors);
915 fBatch.fHasColors = true;
916 } else {
917 fBatch.fHasColors = false;
918 }
919
920 if (localCoords) {
921 installedGeo.fLocalCoords.append(vertexCount, localCoords);
922 fBatch.fHasLocalCoords = true;
923 } else {
924 fBatch.fHasLocalCoords = false;
925 }
926 fBatch.fVertexCount = vertexCount;
927 fBatch.fIndexCount = indexCount;
928 fBatch.fPrimitiveType = primitiveType;
929 }
930
931 GrPrimitiveType primitiveType() const { return fBatch.fPrimitiveType; }
932 bool batchablePrimitiveType() const {
933 return kTriangles_GrPrimitiveType == fBatch.fPrimitiveType ||
934 kLines_GrPrimitiveType == fBatch.fPrimitiveType ||
935 kPoints_GrPrimitiveType == fBatch.fPrimitiveType;
936 }
937 GrColor color() const { return fBatch.fColor; }
938 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
939 bool colorIgnored() const { return fBatch.fColorIgnored; }
940 const SkMatrix& viewMatrix() const { return fBatch.fViewMatrix; }
941 bool hasColors() const { return fBatch.fHasColors; }
942 bool hasIndices() const { return fBatch.fHasIndices; }
943 bool hasLocalCoords() const { return fBatch.fHasLocalCoords; }
944 int vertexCount() const { return fBatch.fVertexCount; }
945 int indexCount() const { return fBatch.fIndexCount; }
946
mtklein36352bf2015-03-25 18:17:31 -0700947 bool onCombineIfPossible(GrBatch* t) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800948 DrawVerticesBatch* that = t->cast<DrawVerticesBatch>();
949
950 if (!this->batchablePrimitiveType() || this->primitiveType() != that->primitiveType()) {
951 return false;
952 }
953
954 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
955
956 // We currently use a uniform viewmatrix for this batch
957 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
958 return false;
959 }
960
961 if (this->hasColors() != that->hasColors()) {
962 return false;
963 }
964
965 if (this->hasIndices() != that->hasIndices()) {
966 return false;
967 }
968
969 if (this->hasLocalCoords() != that->hasLocalCoords()) {
970 return false;
971 }
972
973 if (!this->hasColors() && this->color() != that->color()) {
974 return false;
975 }
976
977 if (this->color() != that->color()) {
978 fBatch.fColor = GrColor_ILLEGAL;
979 }
980 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
981 fBatch.fVertexCount += that->vertexCount();
982 fBatch.fIndexCount += that->indexCount();
983 return true;
984 }
985
986 struct BatchTracker {
987 GrPrimitiveType fPrimitiveType;
988 SkMatrix fViewMatrix;
989 GrColor fColor;
990 bool fUsesLocalCoords;
991 bool fColorIgnored;
992 bool fCoverageIgnored;
993 bool fHasColors;
994 bool fHasIndices;
995 bool fHasLocalCoords;
996 int fVertexCount;
997 int fIndexCount;
998 };
999
1000 BatchTracker fBatch;
1001 SkSTArray<1, Geometry, true> fGeoData;
1002};
1003
joshualitt25d9c152015-02-18 12:29:52 -08001004void GrContext::drawVertices(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -08001005 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001006 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001007 const SkMatrix& viewMatrix,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001008 GrPrimitiveType primitiveType,
1009 int vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001010 const SkPoint positions[],
1011 const SkPoint texCoords[],
bsalomon@google.com27847de2011-02-22 20:59:41 +00001012 const GrColor colors[],
1013 const uint16_t indices[],
1014 int indexCount) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001015 RETURN_IF_ABANDONED
bsalomonf21dab92014-11-13 13:33:28 -08001016 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001017 GrPipelineBuilder pipelineBuilder;
commit-bot@chromium.org5a567932014-01-08 21:26:09 +00001018
joshualitt570d2f82015-02-25 13:19:48 -08001019 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001020 if (NULL == target) {
1021 return;
1022 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001023
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001024 GR_CREATE_TRACE_MARKER("GrContext::drawVertices", target);
1025
joshualitta09c0462015-04-30 14:03:02 -07001026 // TODO clients should give us bounds
1027 SkRect bounds;
1028 if (!bounds.setBoundsCheck(positions, vertexCount)) {
1029 SkDebugf("drawVertices call empty bounds\n");
1030 return;
1031 }
1032
joshualitt0823bfa2015-02-27 10:06:15 -08001033 DrawVerticesBatch::Geometry geometry;
1034 geometry.fColor = paint.getColor();
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001035
joshualitt0823bfa2015-02-27 10:06:15 -08001036 SkAutoTUnref<GrBatch> batch(DrawVerticesBatch::Create(geometry, primitiveType, viewMatrix,
1037 positions, vertexCount, indices,
1038 indexCount,colors, texCoords));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001039
joshualitt0823bfa2015-02-27 10:06:15 -08001040 // TODO figure out bounds
joshualitta09c0462015-04-30 14:03:02 -07001041 target->drawBatch(&pipelineBuilder, batch, &bounds);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001042}
1043
bsalomon@google.com06afe7b2011-04-26 15:31:40 +00001044///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com93c96602012-04-27 13:05:21 +00001045
joshualitt25d9c152015-02-18 12:29:52 -08001046void GrContext::drawRRect(GrRenderTarget*rt,
joshualitt570d2f82015-02-25 13:19:48 -08001047 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001048 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001049 const SkMatrix& viewMatrix,
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001050 const SkRRect& rrect,
egdanield58a0ba2014-06-11 10:30:05 -07001051 const GrStrokeInfo& strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001052 RETURN_IF_ABANDONED
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001053 if (rrect.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001054 return;
1055 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001056
egdanield58a0ba2014-06-11 10:30:05 -07001057 if (strokeInfo.isDashed()) {
1058 SkPath path;
1059 path.addRRect(rrect);
joshualitt570d2f82015-02-25 13:19:48 -08001060 this->drawPath(rt, clip, paint, viewMatrix, path, strokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -07001061 return;
1062 }
1063
bsalomonf21dab92014-11-13 13:33:28 -08001064 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001065 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -08001066 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001067 if (NULL == target) {
1068 return;
1069 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001070
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001071 GR_CREATE_TRACE_MARKER("GrContext::drawRRect", target);
1072
egdanield58a0ba2014-06-11 10:30:05 -07001073 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1074
joshualitt2e3b3e32014-12-09 13:31:14 -08001075 GrColor color = paint.getColor();
joshualitt44701df2015-02-23 14:44:57 -08001076 if (!fOvalRenderer->drawRRect(target,
1077 &pipelineBuilder,
1078 color,
1079 viewMatrix,
1080 paint.isAntiAlias(),
1081 rrect,
1082 strokeRec)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001083 SkPath path;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001084 path.addRRect(rrect);
egdaniel8dd688b2015-01-22 10:16:09 -08001085 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(),
1086 path, strokeInfo);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001087 }
1088}
1089
1090///////////////////////////////////////////////////////////////////////////////
1091
joshualitt25d9c152015-02-18 12:29:52 -08001092void GrContext::drawDRRect(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -08001093 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001094 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001095 const SkMatrix& viewMatrix,
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001096 const SkRRect& outer,
1097 const SkRRect& inner) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001098 RETURN_IF_ABANDONED
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001099 if (outer.isEmpty()) {
1100 return;
1101 }
1102
bsalomonf21dab92014-11-13 13:33:28 -08001103 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001104 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -08001105 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001106
1107 GR_CREATE_TRACE_MARKER("GrContext::drawDRRect", target);
1108
joshualitt2e3b3e32014-12-09 13:31:14 -08001109 GrColor color = paint.getColor();
joshualitt44701df2015-02-23 14:44:57 -08001110 if (!fOvalRenderer->drawDRRect(target,
1111 &pipelineBuilder,
1112 color,
1113 viewMatrix,
1114 paint.isAntiAlias(),
1115 outer,
1116 inner)) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001117 SkPath path;
1118 path.addRRect(inner);
1119 path.addRRect(outer);
1120 path.setFillType(SkPath::kEvenOdd_FillType);
1121
egdanield58a0ba2014-06-11 10:30:05 -07001122 GrStrokeInfo fillRec(SkStrokeRec::kFill_InitStyle);
egdaniel8dd688b2015-01-22 10:16:09 -08001123 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(),
1124 path, fillRec);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001125 }
1126}
1127
1128///////////////////////////////////////////////////////////////////////////////
1129
joshualitt570d2f82015-02-25 13:19:48 -08001130void GrContext::drawOval(GrRenderTarget* rt,
1131 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001132 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001133 const SkMatrix& viewMatrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001134 const SkRect& oval,
egdanield58a0ba2014-06-11 10:30:05 -07001135 const GrStrokeInfo& strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001136 RETURN_IF_ABANDONED
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001137 if (oval.isEmpty()) {
1138 return;
1139 }
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001140
egdanield58a0ba2014-06-11 10:30:05 -07001141 if (strokeInfo.isDashed()) {
1142 SkPath path;
1143 path.addOval(oval);
joshualitt570d2f82015-02-25 13:19:48 -08001144 this->drawPath(rt, clip, paint, viewMatrix, path, strokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -07001145 return;
1146 }
1147
bsalomonf21dab92014-11-13 13:33:28 -08001148 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001149 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -08001150 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001151 if (NULL == target) {
1152 return;
1153 }
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001154
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001155 GR_CREATE_TRACE_MARKER("GrContext::drawOval", target);
1156
egdanield58a0ba2014-06-11 10:30:05 -07001157 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1158
joshualitt2e3b3e32014-12-09 13:31:14 -08001159 GrColor color = paint.getColor();
joshualitt44701df2015-02-23 14:44:57 -08001160 if (!fOvalRenderer->drawOval(target,
1161 &pipelineBuilder,
1162 color,
1163 viewMatrix,
1164 paint.isAntiAlias(),
1165 oval,
1166 strokeRec)) {
bsalomon@google.com93c96602012-04-27 13:05:21 +00001167 SkPath path;
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001168 path.addOval(oval);
egdaniel8dd688b2015-01-22 10:16:09 -08001169 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(),
1170 path, strokeInfo);
skia.committer@gmail.com98ded842013-01-23 07:06:17 +00001171 }
bsalomon@google.com150d2842012-01-12 20:19:56 +00001172}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001173
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001174// Can 'path' be drawn as a pair of filled nested rectangles?
1175static bool is_nested_rects(GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -08001176 GrPipelineBuilder* pipelineBuilder,
joshualitt2e3b3e32014-12-09 13:31:14 -08001177 GrColor color,
joshualitt8059eb92014-12-29 15:10:07 -08001178 const SkMatrix& viewMatrix,
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001179 const SkPath& path,
1180 const SkStrokeRec& stroke,
bsalomon9c0822a2014-08-11 11:07:48 -07001181 SkRect rects[2]) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001182 SkASSERT(stroke.isFillStyle());
1183
1184 if (path.isInverseFillType()) {
1185 return false;
1186 }
1187
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001188 // TODO: this restriction could be lifted if we were willing to apply
1189 // the matrix to all the points individually rather than just to the rect
joshualitt8059eb92014-12-29 15:10:07 -08001190 if (!viewMatrix.preservesAxisAlignment()) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001191 return false;
1192 }
1193
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001194 SkPath::Direction dirs[2];
caryclark95bc5f32015-04-08 08:34:15 -07001195 if (!path.isNestedFillRects(rects, dirs)) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001196 return false;
1197 }
1198
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001199 if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001200 // The two rects need to be wound opposite to each other
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001201 return false;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001202 }
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001203
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001204 // Right now, nested rects where the margin is not the same width
1205 // all around do not render correctly
1206 const SkScalar* outer = rects[0].asScalars();
1207 const SkScalar* inner = rects[1].asScalars();
1208
robertphillips183e9852014-10-21 11:25:37 -07001209 bool allEq = true;
1210
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001211 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
robertphillips183e9852014-10-21 11:25:37 -07001212 bool allGoE1 = margin >= SK_Scalar1;
1213
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001214 for (int i = 1; i < 4; ++i) {
1215 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
robertphillips183e9852014-10-21 11:25:37 -07001216 if (temp < SK_Scalar1) {
1217 allGoE1 = false;
1218 }
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001219 if (!SkScalarNearlyEqual(margin, temp)) {
robertphillips183e9852014-10-21 11:25:37 -07001220 allEq = false;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001221 }
1222 }
1223
robertphillips183e9852014-10-21 11:25:37 -07001224 return allEq || allGoE1;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001225}
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001226
joshualitt25d9c152015-02-18 12:29:52 -08001227void GrContext::drawPath(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -08001228 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001229 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001230 const SkMatrix& viewMatrix,
1231 const SkPath& path,
1232 const GrStrokeInfo& strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001233 RETURN_IF_ABANDONED
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001234 if (path.isEmpty()) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001235 if (path.isInverseFillType()) {
joshualitt570d2f82015-02-25 13:19:48 -08001236 this->drawPaint(rt, clip, paint, viewMatrix);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001237 }
1238 return;
1239 }
1240
joshualitt2e3b3e32014-12-09 13:31:14 -08001241 GrColor color = paint.getColor();
egdanield58a0ba2014-06-11 10:30:05 -07001242
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001243 // Note that internalDrawPath may sw-rasterize the path into a scratch texture.
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001244 // Scratch textures can be recycled after they are returned to the texture
1245 // cache. This presents a potential hazard for buffered drawing. However,
1246 // the writePixels that uploads to the scratch will perform a flush so we're
1247 // OK.
bsalomonf21dab92014-11-13 13:33:28 -08001248 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001249 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -08001250 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001251 if (NULL == target) {
1252 return;
1253 }
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001254
egdaniel93a37bc2014-07-21 13:47:57 -07001255 GR_CREATE_TRACE_MARKER1("GrContext::drawPath", target, "Is Convex", path.isConvex());
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001256
kkinnunen18996512015-04-26 23:18:49 -07001257 if (!strokeInfo.isDashed()) {
1258 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1259 bool useCoverageAA = paint.isAntiAlias() &&
1260 !pipelineBuilder.getRenderTarget()->isMultisampled();
egdanield58a0ba2014-06-11 10:30:05 -07001261
kkinnunen18996512015-04-26 23:18:49 -07001262 if (useCoverageAA && strokeRec.getWidth() < 0 && !path.isConvex()) {
1263 // Concave AA paths are expensive - try to avoid them for special cases
1264 SkRect rects[2];
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001265
kkinnunen18996512015-04-26 23:18:49 -07001266 if (is_nested_rects(target, &pipelineBuilder, color, viewMatrix, path, strokeRec,
1267 rects)) {
1268 fAARectRenderer->fillAANestedRects(target, &pipelineBuilder, color, viewMatrix,
1269 rects);
1270 return;
1271 }
1272 }
1273 SkRect ovalRect;
1274 bool isOval = path.isOval(&ovalRect);
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001275
kkinnunen18996512015-04-26 23:18:49 -07001276 if (isOval && !path.isInverseFillType()) {
1277 if (fOvalRenderer->drawOval(target,
1278 &pipelineBuilder,
1279 color,
1280 viewMatrix,
1281 paint.isAntiAlias(),
1282 ovalRect,
1283 strokeRec)) {
1284 return;
1285 }
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001286 }
1287 }
kkinnunen18996512015-04-26 23:18:49 -07001288 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(),
1289 path, strokeInfo);
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001290}
1291
joshualitt9853cce2014-11-17 14:22:48 -08001292void GrContext::internalDrawPath(GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -08001293 GrPipelineBuilder* pipelineBuilder,
joshualitt5531d512014-12-17 15:50:11 -08001294 const SkMatrix& viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -08001295 GrColor color,
joshualitt9853cce2014-11-17 14:22:48 -08001296 bool useAA,
1297 const SkPath& path,
egdanield58a0ba2014-06-11 10:30:05 -07001298 const GrStrokeInfo& strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001299 RETURN_IF_ABANDONED
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001300 SkASSERT(!path.isEmpty());
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001301
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001302 GR_CREATE_TRACE_MARKER("GrContext::internalDrawPath", target);
1303
1304
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001305 // An Assumption here is that path renderer would use some form of tweaking
1306 // the src color (either the input alpha or in the frag shader) to implement
1307 // aa. If we have some future driver-mojo path AA that can do the right
1308 // thing WRT to the blend then we'll need some query on the PR.
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001309 bool useCoverageAA = useAA &&
egdaniel0bdeec92015-02-23 12:12:54 -08001310 !pipelineBuilder->getRenderTarget()->isMultisampled();
bsalomon@google.com289533a2011-10-27 12:34:25 +00001311
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001312
1313 GrPathRendererChain::DrawType type =
1314 useCoverageAA ? GrPathRendererChain::kColorAntiAlias_DrawType :
joshualitt9853cce2014-11-17 14:22:48 -08001315 GrPathRendererChain::kColor_DrawType;
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001316
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001317 const SkPath* pathPtr = &path;
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001318 SkTLazy<SkPath> tmpPath;
kkinnunen18996512015-04-26 23:18:49 -07001319 const GrStrokeInfo* strokeInfoPtr = &strokeInfo;
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001320
1321 // Try a 1st time without stroking the path and without allowing the SW renderer
egdaniel8dd688b2015-01-22 10:16:09 -08001322 GrPathRenderer* pr = this->getPathRenderer(target, pipelineBuilder, viewMatrix, *pathPtr,
kkinnunen18996512015-04-26 23:18:49 -07001323 *strokeInfoPtr, false, type);
1324
1325 GrStrokeInfo dashlessStrokeInfo(strokeInfo, false);
1326 if (NULL == pr && strokeInfo.isDashed()) {
1327 // It didn't work above, so try again with dashed stroke converted to a dashless stroke.
1328 if (strokeInfo.applyDash(tmpPath.init(), &dashlessStrokeInfo, *pathPtr)) {
1329 pathPtr = tmpPath.get();
1330 if (pathPtr->isEmpty()) {
1331 return;
1332 }
1333 strokeInfoPtr = &dashlessStrokeInfo;
1334 }
1335 pr = this->getPathRenderer(target, pipelineBuilder, viewMatrix, *pathPtr, *strokeInfoPtr,
1336 false, type);
1337 }
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001338
robertphillips@google.come79f3202014-02-11 16:30:21 +00001339 if (NULL == pr) {
kkinnunen18996512015-04-26 23:18:49 -07001340 if (!GrPathRenderer::IsStrokeHairlineOrEquivalent(*strokeInfoPtr, viewMatrix, NULL)) {
1341 // It didn't work above, so try again with stroke converted to a fill.
1342 if (!tmpPath.isValid()) {
1343 tmpPath.init();
1344 }
1345 SkStrokeRec* strokeRec = dashlessStrokeInfo.getStrokeRecPtr();
1346 strokeRec->setResScale(SkScalarAbs(viewMatrix.getMaxScale()));
1347 if (strokeRec->applyToPath(tmpPath.get(), *pathPtr)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001348 pathPtr = tmpPath.get();
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001349 if (pathPtr->isEmpty()) {
1350 return;
1351 }
kkinnunen18996512015-04-26 23:18:49 -07001352 strokeRec->setFillStyle();
1353 strokeInfoPtr = &dashlessStrokeInfo;
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001354 }
1355 }
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001356
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001357 // This time, allow SW renderer
kkinnunen18996512015-04-26 23:18:49 -07001358 pr = this->getPathRenderer(target, pipelineBuilder, viewMatrix, *pathPtr, *strokeInfoPtr,
1359 true, type);
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001360 }
1361
robertphillips@google.come79f3202014-02-11 16:30:21 +00001362 if (NULL == pr) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001363#ifdef SK_DEBUG
tfarina38406c82014-10-31 07:11:12 -07001364 SkDebugf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001365#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001366 return;
1367 }
1368
kkinnunen18996512015-04-26 23:18:49 -07001369 pr->drawPath(target, pipelineBuilder, color, viewMatrix, *pathPtr, *strokeInfoPtr, useCoverageAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001370}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001371
bsalomon@google.com27847de2011-02-22 20:59:41 +00001372////////////////////////////////////////////////////////////////////////////////
1373
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001374void GrContext::flush(int flagsBitfield) {
robertphillips@google.come7db8d62013-07-04 11:48:52 +00001375 if (NULL == fDrawBuffer) {
1376 return;
1377 }
1378
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001379 if (kDiscard_FlushBit & flagsBitfield) {
1380 fDrawBuffer->reset();
1381 } else {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001382 fDrawBuffer->flush();
junov@google.com53a55842011-06-08 22:55:10 +00001383 }
bsalomon3f324322015-04-08 11:01:54 -07001384 fResourceCache->notifyFlushOccurred();
bsalomonf21dab92014-11-13 13:33:28 -08001385 fFlushToReduceCacheSize = false;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001386}
1387
bsalomon81beccc2014-10-13 12:32:55 -07001388bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
1389 const void* inPixels, size_t outRowBytes, void* outPixels) {
1390 SkSrcPixelInfo srcPI;
jvanverthfa1e8a72014-12-22 08:31:49 -08001391 if (!GrPixelConfig2ColorAndProfileType(srcConfig, &srcPI.fColorType, NULL)) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001392 return false;
1393 }
bsalomon81beccc2014-10-13 12:32:55 -07001394 srcPI.fAlphaType = kUnpremul_SkAlphaType;
1395 srcPI.fPixels = inPixels;
1396 srcPI.fRowBytes = inRowBytes;
1397
1398 SkDstPixelInfo dstPI;
1399 dstPI.fColorType = srcPI.fColorType;
1400 dstPI.fAlphaType = kPremul_SkAlphaType;
1401 dstPI.fPixels = outPixels;
1402 dstPI.fRowBytes = outRowBytes;
1403
1404 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001405}
1406
bsalomon81beccc2014-10-13 12:32:55 -07001407bool GrContext::writeSurfacePixels(GrSurface* surface,
1408 int left, int top, int width, int height,
1409 GrPixelConfig srcConfig, const void* buffer, size_t rowBytes,
1410 uint32_t pixelOpsFlags) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001411 RETURN_FALSE_IF_ABANDONED
bsalomon81beccc2014-10-13 12:32:55 -07001412 {
1413 GrTexture* texture = NULL;
1414 if (!(kUnpremul_PixelOpsFlag & pixelOpsFlags) && (texture = surface->asTexture()) &&
1415 fGpu->canWriteTexturePixels(texture, srcConfig)) {
1416
1417 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) &&
1418 surface->surfacePriv().hasPendingIO()) {
1419 this->flush();
1420 }
1421 return fGpu->writeTexturePixels(texture, left, top, width, height,
1422 srcConfig, buffer, rowBytes);
1423 // Don't need to check kFlushWrites_PixelOp here, we just did a direct write so the
1424 // upload is already flushed.
1425 }
1426 }
1427
1428 // If we didn't do a direct texture write then we upload the pixels to a texture and draw.
1429 GrRenderTarget* renderTarget = surface->asRenderTarget();
1430 if (NULL == renderTarget) {
1431 return false;
1432 }
1433
1434 // We ignore the preferred config unless it is a R/B swap of the src config. In that case
1435 // we will upload the original src data to a scratch texture but we will spoof it as the swapped
1436 // config. This scratch will then have R and B swapped. We correct for this by swapping again
1437 // when drawing the scratch to the dst using a conversion effect.
1438 bool swapRAndB = false;
1439 GrPixelConfig writeConfig = srcConfig;
1440 if (GrPixelConfigSwapRAndB(srcConfig) ==
1441 fGpu->preferredWritePixelsConfig(srcConfig, renderTarget->config())) {
1442 writeConfig = GrPixelConfigSwapRAndB(srcConfig);
1443 swapRAndB = true;
1444 }
1445
bsalomonf2703d82014-10-28 14:33:06 -07001446 GrSurfaceDesc desc;
bsalomon81beccc2014-10-13 12:32:55 -07001447 desc.fWidth = width;
1448 desc.fHeight = height;
1449 desc.fConfig = writeConfig;
bsalomond309e7a2015-04-30 14:18:54 -07001450 SkAutoTUnref<GrTexture> texture(this->textureProvider()->refScratchTexture(desc,
1451 GrTextureProvider::kApprox_ScratchTexMatch));
bsalomone3059732014-10-14 11:47:22 -07001452 if (!texture) {
bsalomon81beccc2014-10-13 12:32:55 -07001453 return false;
1454 }
1455
1456 SkAutoTUnref<const GrFragmentProcessor> fp;
1457 SkMatrix textureMatrix;
1458 textureMatrix.setIDiv(texture->width(), texture->height());
1459
1460 // allocate a tmp buffer and sw convert the pixels to premul
1461 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
1462
1463 if (kUnpremul_PixelOpsFlag & pixelOpsFlags) {
1464 if (!GrPixelConfigIs8888(srcConfig)) {
1465 return false;
1466 }
1467 fp.reset(this->createUPMToPMEffect(texture, swapRAndB, textureMatrix));
1468 // handle the unpremul step on the CPU if we couldn't create an effect to do it.
1469 if (NULL == fp) {
1470 size_t tmpRowBytes = 4 * width;
1471 tmpPixels.reset(width * height);
1472 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
1473 tmpPixels.get())) {
1474 return false;
1475 }
1476 rowBytes = tmpRowBytes;
1477 buffer = tmpPixels.get();
1478 }
1479 }
1480 if (NULL == fp) {
1481 fp.reset(GrConfigConversionEffect::Create(texture,
1482 swapRAndB,
1483 GrConfigConversionEffect::kNone_PMConversion,
1484 textureMatrix));
1485 }
1486
1487 // Even if the client told us not to flush, we still flush here. The client may have known that
1488 // writes to the original surface caused no data hazards, but they can't know that the scratch
1489 // we just got is safe.
1490 if (texture->surfacePriv().hasPendingIO()) {
1491 this->flush();
1492 }
1493 if (!fGpu->writeTexturePixels(texture, 0, 0, width, height,
1494 writeConfig, buffer, rowBytes)) {
1495 return false;
1496 }
1497
1498 SkMatrix matrix;
1499 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
1500
1501 // This function can be called in the midst of drawing another object (e.g., when uploading a
1502 // SW-rasterized clip while issuing a draw). So we push the current geometry state before
1503 // drawing a rect to the render target.
1504 // The bracket ensures we pop the stack if we wind up flushing below.
1505 {
joshualitt570d2f82015-02-25 13:19:48 -08001506 GrDrawTarget* drawTarget = this->prepareToDraw();
1507 if (!drawTarget) {
1508 return false;
1509 }
joshualitt9853cce2014-11-17 14:22:48 -08001510
egdaniel8dd688b2015-01-22 10:16:09 -08001511 GrPipelineBuilder pipelineBuilder;
1512 pipelineBuilder.addColorProcessor(fp);
1513 pipelineBuilder.setRenderTarget(renderTarget);
joshualitt44701df2015-02-23 14:44:57 -08001514 drawTarget->drawSimpleRect(&pipelineBuilder,
1515 GrColor_WHITE,
1516 matrix,
joshualitt8059eb92014-12-29 15:10:07 -08001517 SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)));
bsalomon81beccc2014-10-13 12:32:55 -07001518 }
1519
1520 if (kFlushWrites_PixelOp & pixelOpsFlags) {
1521 this->flushSurfaceWrites(surface);
1522 }
1523
1524 return true;
1525}
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001526
reed@google.com7111d462014-03-25 16:20:24 +00001527// toggles between RGBA and BGRA
1528static SkColorType toggle_colortype32(SkColorType ct) {
1529 if (kRGBA_8888_SkColorType == ct) {
1530 return kBGRA_8888_SkColorType;
1531 } else {
1532 SkASSERT(kBGRA_8888_SkColorType == ct);
1533 return kRGBA_8888_SkColorType;
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001534 }
1535}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001536
bsalomon@google.com0342a852012-08-20 19:22:38 +00001537bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
1538 int left, int top, int width, int height,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001539 GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001540 uint32_t flags) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001541 RETURN_FALSE_IF_ABANDONED
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001542 ASSERT_OWNED_RESOURCE(target);
bsalomon89c62982014-11-03 12:08:42 -08001543 SkASSERT(target);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001544
bsalomonafbf2d62014-09-30 12:18:44 -07001545 if (!(kDontFlush_PixelOpsFlag & flags) && target->surfacePriv().hasPendingWrite()) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001546 this->flush();
1547 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001548
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001549 // Determine which conversions have to be applied: flipY, swapRAnd, and/or unpremul.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001550
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001551 // If fGpu->readPixels would incur a y-flip cost then we will read the pixels upside down. We'll
1552 // either do the flipY by drawing into a scratch with a matrix or on the cpu after the read.
1553 bool flipY = fGpu->readPixelsWillPayForYFlip(target, left, top,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001554 width, height, dstConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001555 rowBytes);
bsalomon@google.com9c680582013-02-06 18:17:50 +00001556 // We ignore the preferred config if it is different than our config unless it is an R/B swap.
1557 // In that case we'll perform an R and B swap while drawing to a scratch texture of the swapped
1558 // config. Then we will call readPixels on the scratch with the swapped config. The swaps during
1559 // the draw cancels out the fact that we call readPixels with a config that is R/B swapped from
1560 // dstConfig.
1561 GrPixelConfig readConfig = dstConfig;
1562 bool swapRAndB = false;
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +00001563 if (GrPixelConfigSwapRAndB(dstConfig) ==
1564 fGpu->preferredReadPixelsConfig(dstConfig, target->config())) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001565 readConfig = GrPixelConfigSwapRAndB(readConfig);
1566 swapRAndB = true;
1567 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001568
bsalomon@google.com0342a852012-08-20 19:22:38 +00001569 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001570
bsalomon@google.com9c680582013-02-06 18:17:50 +00001571 if (unpremul && !GrPixelConfigIs8888(dstConfig)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001572 // The unpremul flag is only allowed for these two configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001573 return false;
1574 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001575
bsalomon191bcc02014-11-14 11:31:13 -08001576 SkAutoTUnref<GrTexture> tempTexture;
1577
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001578 // If the src is a texture and we would have to do conversions after read pixels, we instead
1579 // do the conversions by drawing the src to a scratch texture. If we handle any of the
1580 // conversions in the draw we set the corresponding bool to false so that we don't reapply it
1581 // on the read back pixels.
1582 GrTexture* src = target->asTexture();
bsalomon49f085d2014-09-05 13:34:00 -07001583 if (src && (swapRAndB || unpremul || flipY)) {
bsalomon81beccc2014-10-13 12:32:55 -07001584 // Make the scratch a render so we can read its pixels.
bsalomonf2703d82014-10-28 14:33:06 -07001585 GrSurfaceDesc desc;
1586 desc.fFlags = kRenderTarget_GrSurfaceFlag;
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001587 desc.fWidth = width;
1588 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001589 desc.fConfig = readConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001590 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001591
bsalomon@google.com9c680582013-02-06 18:17:50 +00001592 // 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 +00001593 // match the passed rect. However, if we see many different size rectangles we will trash
1594 // our texture cache and pay the cost of creating and destroying many textures. So, we only
1595 // request an exact match when the caller is reading an entire RT.
bsalomond309e7a2015-04-30 14:18:54 -07001596 GrTextureProvider::ScratchTexMatch match = GrTextureProvider::kApprox_ScratchTexMatch;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001597 if (0 == left &&
1598 0 == top &&
1599 target->width() == width &&
1600 target->height() == height &&
1601 fGpu->fullReadPixelsIsFasterThanPartial()) {
bsalomond309e7a2015-04-30 14:18:54 -07001602 match = GrTextureProvider::kExact_ScratchTexMatch;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001603 }
bsalomond309e7a2015-04-30 14:18:54 -07001604 tempTexture.reset(this->textureProvider()->refScratchTexture(desc, match));
bsalomon191bcc02014-11-14 11:31:13 -08001605 if (tempTexture) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001606 // compute a matrix to perform the draw
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001607 SkMatrix textureMatrix;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001608 textureMatrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001609 textureMatrix.postIDiv(src->width(), src->height());
1610
joshualittb0a8a372014-09-23 09:50:21 -07001611 SkAutoTUnref<const GrFragmentProcessor> fp;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001612 if (unpremul) {
joshualittb0a8a372014-09-23 09:50:21 -07001613 fp.reset(this->createPMToUPMEffect(src, swapRAndB, textureMatrix));
1614 if (fp) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001615 unpremul = false; // we no longer need to do this on CPU after the read back.
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001616 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001617 }
1618 // If we failed to create a PM->UPM effect and have no other conversions to perform then
1619 // there is no longer any point to using the scratch.
joshualittb0a8a372014-09-23 09:50:21 -07001620 if (fp || flipY || swapRAndB) {
1621 if (!fp) {
1622 fp.reset(GrConfigConversionEffect::Create(
1623 src, swapRAndB, GrConfigConversionEffect::kNone_PMConversion,
1624 textureMatrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001625 }
1626 swapRAndB = false; // we will handle the swap in the draw.
bsalomon@google.comc4364992011-11-07 15:54:49 +00001627
robertphillips@google.com13f181f2013-03-02 12:02:08 +00001628 // We protect the existing geometry here since it may not be
1629 // clear to the caller that a draw operation (i.e., drawSimpleRect)
1630 // can be invoked in this method
joshualitt5c55fef2014-10-31 14:04:35 -07001631 {
egdaniel8dd688b2015-01-22 10:16:09 -08001632 GrPipelineBuilder pipelineBuilder;
joshualitt5c55fef2014-10-31 14:04:35 -07001633 SkASSERT(fp);
egdaniel8dd688b2015-01-22 10:16:09 -08001634 pipelineBuilder.addColorProcessor(fp);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001635
egdaniel8dd688b2015-01-22 10:16:09 -08001636 pipelineBuilder.setRenderTarget(tempTexture->asRenderTarget());
joshualitt5c55fef2014-10-31 14:04:35 -07001637 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
joshualitt44701df2015-02-23 14:44:57 -08001638 fDrawBuffer->drawSimpleRect(&pipelineBuilder,
1639 GrColor_WHITE,
1640 SkMatrix::I(),
egdaniel8dd688b2015-01-22 10:16:09 -08001641 rect);
joshualitt5c55fef2014-10-31 14:04:35 -07001642 // we want to read back from the scratch's origin
1643 left = 0;
1644 top = 0;
bsalomon191bcc02014-11-14 11:31:13 -08001645 target = tempTexture->asRenderTarget();
joshualitt5c55fef2014-10-31 14:04:35 -07001646 }
1647 this->flushSurfaceWrites(target);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001648 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001649 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001650 }
joshualitt5c55fef2014-10-31 14:04:35 -07001651
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001652 if (!fGpu->readPixels(target,
1653 left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001654 readConfig, buffer, rowBytes)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001655 return false;
1656 }
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001657 // Perform any conversions we weren't able to perform using a scratch texture.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001658 if (unpremul || swapRAndB) {
reed@google.com7111d462014-03-25 16:20:24 +00001659 SkDstPixelInfo dstPI;
jvanverthfa1e8a72014-12-22 08:31:49 -08001660 if (!GrPixelConfig2ColorAndProfileType(dstConfig, &dstPI.fColorType, NULL)) {
reed@google.com7111d462014-03-25 16:20:24 +00001661 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001662 }
reed@google.com7111d462014-03-25 16:20:24 +00001663 dstPI.fAlphaType = kUnpremul_SkAlphaType;
1664 dstPI.fPixels = buffer;
1665 dstPI.fRowBytes = rowBytes;
1666
1667 SkSrcPixelInfo srcPI;
1668 srcPI.fColorType = swapRAndB ? toggle_colortype32(dstPI.fColorType) : dstPI.fColorType;
1669 srcPI.fAlphaType = kPremul_SkAlphaType;
1670 srcPI.fPixels = buffer;
1671 srcPI.fRowBytes = rowBytes;
1672
1673 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001674 }
1675 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001676}
1677
bsalomon87a94eb2014-11-03 14:28:32 -08001678void GrContext::prepareSurfaceForExternalRead(GrSurface* surface) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001679 RETURN_IF_ABANDONED
bsalomon87a94eb2014-11-03 14:28:32 -08001680 SkASSERT(surface);
1681 ASSERT_OWNED_RESOURCE(surface);
1682 if (surface->surfacePriv().hasPendingIO()) {
1683 this->flush();
1684 }
1685 GrRenderTarget* rt = surface->asRenderTarget();
1686 if (fGpu && rt) {
1687 fGpu->resolveRenderTarget(rt);
bsalomon41ebbdd2014-08-04 08:31:39 -07001688 }
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001689}
1690
bsalomon41ebbdd2014-08-04 08:31:39 -07001691void GrContext::discardRenderTarget(GrRenderTarget* renderTarget) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001692 RETURN_IF_ABANDONED
bsalomon41ebbdd2014-08-04 08:31:39 -07001693 SkASSERT(renderTarget);
1694 ASSERT_OWNED_RESOURCE(renderTarget);
bsalomonf21dab92014-11-13 13:33:28 -08001695 AutoCheckFlush acf(this);
joshualitt570d2f82015-02-25 13:19:48 -08001696 GrDrawTarget* target = this->prepareToDraw();
bsalomon41ebbdd2014-08-04 08:31:39 -07001697 if (NULL == target) {
1698 return;
1699 }
1700 target->discard(renderTarget);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001701}
1702
bsalomonf80bfed2014-10-07 05:56:02 -07001703void GrContext::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
1704 const SkIPoint& dstPoint, uint32_t pixelOpsFlags) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001705 RETURN_IF_ABANDONED
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001706 if (NULL == src || NULL == dst) {
1707 return;
1708 }
bsalomone3d4bf22014-09-23 09:15:03 -07001709 ASSERT_OWNED_RESOURCE(src);
junov2bb52102014-09-29 10:18:59 -07001710 ASSERT_OWNED_RESOURCE(dst);
Brian Salomon34a98952014-09-24 11:41:24 -04001711
bsalomonf80bfed2014-10-07 05:56:02 -07001712 // Since we're going to the draw target and not GPU, no need to check kNoFlush
1713 // here.
junov96c118e2014-09-26 13:09:47 -07001714
joshualitt570d2f82015-02-25 13:19:48 -08001715 GrDrawTarget* target = this->prepareToDraw();
junov96c118e2014-09-26 13:09:47 -07001716 if (NULL == target) {
1717 return;
1718 }
junov96c118e2014-09-26 13:09:47 -07001719 target->copySurface(dst, src, srcRect, dstPoint);
bsalomonf80bfed2014-10-07 05:56:02 -07001720
1721 if (kFlushWrites_PixelOp & pixelOpsFlags) {
1722 this->flush();
1723 }
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001724}
1725
bsalomonf80bfed2014-10-07 05:56:02 -07001726void GrContext::flushSurfaceWrites(GrSurface* surface) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001727 RETURN_IF_ABANDONED
bsalomonf80bfed2014-10-07 05:56:02 -07001728 if (surface->surfacePriv().hasPendingWrite()) {
1729 this->flush();
1730 }
1731}
1732
egdaniel8dd688b2015-01-22 10:16:09 -08001733GrDrawTarget* GrContext::prepareToDraw(GrPipelineBuilder* pipelineBuilder,
joshualitt25d9c152015-02-18 12:29:52 -08001734 GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -08001735 const GrClip& clip,
joshualitt9853cce2014-11-17 14:22:48 -08001736 const GrPaint* paint,
1737 const AutoCheckFlush* acf) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001738 if (NULL == fGpu || NULL == fDrawBuffer) {
bsalomon41ebbdd2014-08-04 08:31:39 -07001739 return NULL;
1740 }
1741
joshualitt570d2f82015-02-25 13:19:48 -08001742 ASSERT_OWNED_RESOURCE(rt);
1743 SkASSERT(rt && paint && acf);
1744 pipelineBuilder->setFromPaint(*paint, rt, clip);
1745 return fDrawBuffer;
1746}
1747
1748GrDrawTarget* GrContext::prepareToDraw() {
1749 if (NULL == fGpu) {
1750 return NULL;
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001751 }
joshualitt5c55fef2014-10-31 14:04:35 -07001752 return fDrawBuffer;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001753}
1754
robertphillips@google.com72176b22012-05-23 13:19:12 +00001755/*
1756 * This method finds a path renderer that can draw the specified path on
1757 * the provided target.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001758 * Due to its expense, the software path renderer has split out so it can
robertphillips@google.com72176b22012-05-23 13:19:12 +00001759 * can be individually allowed/disallowed via the "allowSW" boolean.
1760 */
joshualitt9853cce2014-11-17 14:22:48 -08001761GrPathRenderer* GrContext::getPathRenderer(const GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -08001762 const GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -08001763 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -08001764 const SkPath& path,
kkinnunen18996512015-04-26 23:18:49 -07001765 const GrStrokeInfo& stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001766 bool allowSW,
1767 GrPathRendererChain::DrawType drawType,
1768 GrPathRendererChain::StencilSupport* stencilSupport) {
1769
bsalomon@google.com30085192011-08-19 15:42:31 +00001770 if (NULL == fPathRendererChain) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001771 fPathRendererChain = SkNEW_ARGS(GrPathRendererChain, (this));
bsalomon@google.com30085192011-08-19 15:42:31 +00001772 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001773
joshualitt9853cce2014-11-17 14:22:48 -08001774 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(target,
egdaniel8dd688b2015-01-22 10:16:09 -08001775 pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -08001776 viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -08001777 path,
sugoi@google.com12b4e272012-12-06 20:13:11 +00001778 stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001779 drawType,
1780 stencilSupport);
robertphillips@google.com72176b22012-05-23 13:19:12 +00001781
1782 if (NULL == pr && allowSW) {
1783 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001784 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001785 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001786 pr = fSoftwarePathRenderer;
1787 }
1788
1789 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001790}
1791
bsalomon@google.com27847de2011-02-22 20:59:41 +00001792////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00001793bool GrContext::isConfigRenderable(GrPixelConfig config, bool withMSAA) const {
1794 return fGpu->caps()->isConfigRenderable(config, withMSAA);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001795}
1796
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +00001797int GrContext::getRecommendedSampleCount(GrPixelConfig config,
1798 SkScalar dpi) const {
1799 if (!this->isConfigRenderable(config, true)) {
1800 return 0;
1801 }
1802 int chosenSampleCount = 0;
jvanverthe9c0fc62015-04-29 11:18:05 -07001803 if (fGpu->caps()->shaderCaps()->pathRenderingSupport()) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +00001804 if (dpi >= 250.0f) {
1805 chosenSampleCount = 4;
1806 } else {
1807 chosenSampleCount = 16;
1808 }
1809 }
1810 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ?
1811 chosenSampleCount : 0;
1812}
1813
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001814void GrContext::setupDrawBuffer() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001815 SkASSERT(NULL == fDrawBuffer);
1816 SkASSERT(NULL == fDrawBufferVBAllocPool);
1817 SkASSERT(NULL == fDrawBufferIBAllocPool);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001818
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001819 fDrawBufferVBAllocPool =
bsalomonecb8e3e2015-04-29 04:33:52 -07001820 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu,
1821 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
1822 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001823 fDrawBufferIBAllocPool =
bsalomonecb8e3e2015-04-29 04:33:52 -07001824 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu,
1825 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
1826 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001827
bsalomona73239a2015-04-28 13:35:17 -07001828 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (this,
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001829 fDrawBufferVBAllocPool,
1830 fDrawBufferIBAllocPool));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001831}
1832
bsalomon@google.com21c10c52013-06-13 17:44:07 +00001833GrDrawTarget* GrContext::getTextTarget() {
joshualitt570d2f82015-02-25 13:19:48 -08001834 return this->prepareToDraw();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001835}
1836
1837const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1838 return fGpu->getQuadIndexBuffer();
1839}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001840
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001841namespace {
1842void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
1843 GrConfigConversionEffect::PMConversion pmToUPM;
1844 GrConfigConversionEffect::PMConversion upmToPM;
1845 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
1846 *pmToUPMValue = pmToUPM;
1847 *upmToPMValue = upmToPM;
1848}
1849}
1850
joshualittb0a8a372014-09-23 09:50:21 -07001851const GrFragmentProcessor* GrContext::createPMToUPMEffect(GrTexture* texture,
1852 bool swapRAndB,
1853 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001854 if (!fDidTestPMConversions) {
1855 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001856 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001857 }
1858 GrConfigConversionEffect::PMConversion pmToUPM =
1859 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
1860 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001861 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001862 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001863 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001864 }
1865}
1866
joshualittb0a8a372014-09-23 09:50:21 -07001867const GrFragmentProcessor* GrContext::createUPMToPMEffect(GrTexture* texture,
1868 bool swapRAndB,
1869 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001870 if (!fDidTestPMConversions) {
1871 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001872 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001873 }
1874 GrConfigConversionEffect::PMConversion upmToPM =
1875 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
1876 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001877 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001878 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001879 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001880 }
1881}
1882
bsalomon37f9a262015-02-02 13:00:10 -08001883//////////////////////////////////////////////////////////////////////////////
1884
1885void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
1886 if (maxTextures) {
bsalomon0ea80f42015-02-11 10:49:59 -08001887 *maxTextures = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -08001888 }
1889 if (maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -08001890 *maxTextureBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -08001891 }
1892}
1893
1894void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -08001895 fResourceCache->setLimits(maxTextures, maxTextureBytes);
bsalomon37f9a262015-02-02 13:00:10 -08001896}
1897
bsalomon37f9a262015-02-02 13:00:10 -08001898//////////////////////////////////////////////////////////////////////////////
1899
egdanielbbcb38d2014-06-19 10:19:29 -07001900void GrContext::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
1901 fGpu->addGpuTraceMarker(marker);
bsalomon49f085d2014-09-05 13:34:00 -07001902 if (fDrawBuffer) {
egdanielbbcb38d2014-06-19 10:19:29 -07001903 fDrawBuffer->addGpuTraceMarker(marker);
1904 }
1905}
1906
1907void GrContext::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
1908 fGpu->removeGpuTraceMarker(marker);
bsalomon49f085d2014-09-05 13:34:00 -07001909 if (fDrawBuffer) {
egdanielbbcb38d2014-06-19 10:19:29 -07001910 fDrawBuffer->removeGpuTraceMarker(marker);
1911 }
1912}