blob: 3ed0b6baa26fc37c9483c44b0fa9b30aee85dd7b [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
bsalomon@google.com27847de2011-02-22 20:59:41 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
bsalomon@google.com27847de2011-02-22 20:59:41 +00007 */
8
bsalomon@google.com1fadb202011-12-12 16:10:08 +00009#include "GrContext.h"
10
jvanverth@google.combfe2b9d2013-09-06 16:57:29 +000011#include "GrAARectRenderer.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070012#include "GrAtlasTextContext.h"
joshualitt0823bfa2015-02-27 10:06:15 -080013#include "GrBatch.h"
joshualitt7c3a2f82015-03-31 13:32:05 -070014#include "GrBatchFontCache.h"
joshualitt0823bfa2015-02-27 10:06:15 -080015#include "GrBatchTarget.h"
joshualitte5902662015-05-11 08:18:35 -070016#include "GrBatchTest.h"
bsalomoneb1cb5c2015-05-22 08:01:09 -070017#include "GrCaps.h"
bsalomonf28cff72015-05-22 12:25:41 -070018#include "GrContextOptions.h"
joshualitt5478d422014-11-14 16:00:38 -080019#include "GrDefaultGeoProcFactory.h"
bsalomon453cf402014-11-11 14:15:57 -080020#include "GrGpuResource.h"
bsalomon3582d3e2015-02-13 14:20:05 -080021#include "GrGpuResourcePriv.h"
joshualitt5478d422014-11-14 16:00:38 -080022#include "GrGpu.h"
joshualittbb87b212015-05-19 14:28:04 -070023#include "GrImmediateDrawTarget.h"
commit-bot@chromium.orgdcb8ef92014-03-27 11:26:10 +000024#include "GrIndexBuffer.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000025#include "GrInOrderDrawBuffer.h"
robertphillips@google.come930a072014-04-03 00:34:27 +000026#include "GrLayerCache.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +000027#include "GrOvalRenderer.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000028#include "GrPathRenderer.h"
tomhudson@google.comd22b6e42011-06-24 15:53:40 +000029#include "GrPathUtils.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080030#include "GrRenderTargetPriv.h"
bsalomon0ea80f42015-02-11 10:49:59 -080031#include "GrResourceCache.h"
bsalomond309e7a2015-04-30 14:18:54 -070032#include "GrResourceProvider.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000033#include "GrSoftwarePathRenderer.h"
kkinnunenc6cb56f2014-06-24 00:12:27 -070034#include "GrStencilAndCoverTextContext.h"
egdanield58a0ba2014-06-11 10:30:05 -070035#include "GrStrokeInfo.h"
bsalomonafbf2d62014-09-30 12:18:44 -070036#include "GrSurfacePriv.h"
joshualittb7133be2015-04-08 09:08:31 -070037#include "GrTextBlobCache.h"
bsalomonafbf2d62014-09-30 12:18:44 -070038#include "GrTexturePriv.h"
egdanielbbcb38d2014-06-19 10:19:29 -070039#include "GrTraceMarker.h"
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +000040#include "GrTracing.h"
bsalomoncb8979d2015-05-05 09:51:38 -070041#include "GrVertices.h"
egdanield58a0ba2014-06-11 10:30:05 -070042#include "SkDashPathPriv.h"
bsalomon81beccc2014-10-13 12:32:55 -070043#include "SkConfig8888.h"
reed@google.com7111d462014-03-25 16:20:24 +000044#include "SkGr.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000045#include "SkRRect.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000046#include "SkStrokeRec.h"
commit-bot@chromium.orgdcb8ef92014-03-27 11:26:10 +000047#include "SkTLazy.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000048#include "SkTLS.h"
commit-bot@chromium.org933e65d2014-03-20 20:00:24 +000049#include "SkTraceEvent.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000050
joshualitt5478d422014-11-14 16:00:38 -080051#include "effects/GrConfigConversionEffect.h"
52#include "effects/GrDashingEffect.h"
53#include "effects/GrSingleTextureEffect.h"
54
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000055#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
joshualitt5f5a8d72015-02-25 14:09:45 -080056#define RETURN_IF_ABANDONED if (!fDrawBuffer) { return; }
57#define RETURN_FALSE_IF_ABANDONED if (!fDrawBuffer) { return false; }
58#define RETURN_NULL_IF_ABANDONED if (!fDrawBuffer) { return NULL; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000059
bsalomonf21dab92014-11-13 13:33:28 -080060class GrContext::AutoCheckFlush {
61public:
62 AutoCheckFlush(GrContext* context) : fContext(context) { SkASSERT(context); }
63
64 ~AutoCheckFlush() {
65 if (fContext->fFlushToReduceCacheSize) {
66 fContext->flush();
67 }
68 }
69
70private:
71 GrContext* fContext;
72};
73
bsalomonf28cff72015-05-22 12:25:41 -070074GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext) {
75 GrContextOptions defaultOptions;
76 return Create(backend, backendContext, defaultOptions);
77}
krajcevski9c6d4d72014-08-12 07:26:25 -070078
bsalomonf28cff72015-05-22 12:25:41 -070079GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
80 const GrContextOptions& options) {
81 GrContext* context = SkNEW(GrContext);
82
83 if (context->init(backend, backendContext, options)) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000084 return context;
85 } else {
86 context->unref();
87 return NULL;
bsalomon@google.com27847de2011-02-22 20:59:41 +000088 }
bsalomon@google.com27847de2011-02-22 20:59:41 +000089}
90
joshualitt0acd0d32015-05-07 08:23:19 -070091static int32_t gNextID = 1;
92static int32_t next_id() {
93 int32_t id;
94 do {
95 id = sk_atomic_inc(&gNextID);
96 } while (id == SK_InvalidGenID);
97 return id;
98}
99
bsalomonf28cff72015-05-22 12:25:41 -0700100GrContext::GrContext() : fUniqueID(next_id()) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000101 fGpu = NULL;
bsalomond309e7a2015-04-30 14:18:54 -0700102 fResourceCache = NULL;
103 fResourceProvider = NULL;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000104 fPathRendererChain = NULL;
105 fSoftwarePathRenderer = NULL;
joshualitt7c3a2f82015-03-31 13:32:05 -0700106 fBatchFontCache = NULL;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000107 fDrawBuffer = NULL;
bsalomonf21dab92014-11-13 13:33:28 -0800108 fFlushToReduceCacheSize = false;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000109 fAARectRenderer = NULL;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000110 fOvalRenderer = NULL;
robertphillips@google.com44a91dc2013-07-25 15:32:06 +0000111 fMaxTextureSizeOverride = 1 << 20;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000112}
113
bsalomonf28cff72015-05-22 12:25:41 -0700114bool GrContext::init(GrBackend backend, GrBackendContext backendContext,
115 const GrContextOptions& options) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000116 SkASSERT(NULL == fGpu);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000117
bsalomonf28cff72015-05-22 12:25:41 -0700118 fGpu = GrGpu::Create(backend, backendContext, options, this);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000119 if (NULL == fGpu) {
120 return false;
121 }
bsalomon33435572014-11-05 14:47:41 -0800122 this->initCommon();
123 return true;
124}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000125
bsalomon33435572014-11-05 14:47:41 -0800126void GrContext::initCommon() {
bsalomon0ea80f42015-02-11 10:49:59 -0800127 fResourceCache = SkNEW(GrResourceCache);
128 fResourceCache->setOverBudgetCallback(OverBudgetCB, this);
bsalomond309e7a2015-04-30 14:18:54 -0700129 fResourceProvider = SkNEW_ARGS(GrResourceProvider, (fGpu, fResourceCache));
commit-bot@chromium.org1836d332013-07-16 22:55:03 +0000130
robertphillips4ec84da2014-06-24 13:10:43 -0700131 fLayerCache.reset(SkNEW_ARGS(GrLayerCache, (this)));
robertphillips@google.come930a072014-04-03 00:34:27 +0000132
bsalomoned0bcad2015-05-04 10:36:42 -0700133 fAARectRenderer = SkNEW(GrAARectRenderer);
134 fOvalRenderer = SkNEW(GrOvalRenderer);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000135
136 fDidTestPMConversions = false;
137
joshualittbb87b212015-05-19 14:28:04 -0700138#ifdef IMMEDIATE_MODE
139 fDrawBuffer = SkNEW_ARGS(GrImmediateDrawTarget, (this));
140#else
robertphillipse40d3972015-05-07 09:51:43 -0700141 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (this));
joshualittbb87b212015-05-19 14:28:04 -0700142#endif
joshualitt7c3a2f82015-03-31 13:32:05 -0700143
144 // GrBatchFontCache will eventually replace GrFontCache
joshualitt62db8ba2015-04-09 08:22:37 -0700145 fBatchFontCache = SkNEW_ARGS(GrBatchFontCache, (this));
joshualittb7133be2015-04-08 09:08:31 -0700146
joshualitt0db6dfa2015-04-10 07:01:30 -0700147 fTextBlobCache.reset(SkNEW_ARGS(GrTextBlobCache, (TextBlobCacheOverBudgetCB, this)));
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000148}
149
bsalomon@google.com27847de2011-02-22 20:59:41 +0000150GrContext::~GrContext() {
bsalomon@google.com733c0622013-04-24 17:59:32 +0000151 if (NULL == fGpu) {
152 return;
153 }
154
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000155 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000156
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000157 for (int i = 0; i < fCleanUpData.count(); ++i) {
158 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
159 }
160
bsalomond309e7a2015-04-30 14:18:54 -0700161 SkDELETE(fResourceProvider);
bsalomon0ea80f42015-02-11 10:49:59 -0800162 SkDELETE(fResourceCache);
joshualitt7c3a2f82015-03-31 13:32:05 -0700163 SkDELETE(fBatchFontCache);
bsalomon33435572014-11-05 14:47:41 -0800164 SkDELETE(fDrawBuffer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000165
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000166 fAARectRenderer->unref();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000167 fOvalRenderer->unref();
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000168
bsalomon@google.com205d4602011-04-25 12:43:45 +0000169 fGpu->unref();
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000170 SkSafeUnref(fPathRendererChain);
171 SkSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000172}
173
bsalomon2354f842014-07-28 13:48:36 -0700174void GrContext::abandonContext() {
bsalomond309e7a2015-04-30 14:18:54 -0700175 fResourceProvider->abandon();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000176 // abandon first to so destructors
177 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800178 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700179
robertphillipse3371302014-09-17 06:01:06 -0700180 fGpu->contextAbandoned();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000181
bsalomon@google.com30085192011-08-19 15:42:31 +0000182 // a path renderer may be holding onto resources that
183 // are now unusable
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000184 SkSafeSetNull(fPathRendererChain);
185 SkSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000186
robertphillipse40d3972015-05-07 09:51:43 -0700187 SkDELETE(fDrawBuffer);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000188 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000189
joshualitt7c3a2f82015-03-31 13:32:05 -0700190 fBatchFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000191 fLayerCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700192 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000193}
194
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000195void GrContext::resetContext(uint32_t state) {
196 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000197}
198
199void GrContext::freeGpuResources() {
200 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000201
bsalomon49f085d2014-09-05 13:34:00 -0700202 if (fDrawBuffer) {
bsalomonc8dc1f72014-08-21 13:02:13 -0700203 fDrawBuffer->purgeResources();
204 }
robertphillips@google.comff175842012-05-14 19:31:39 +0000205
joshualitt7c3a2f82015-03-31 13:32:05 -0700206 fBatchFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000207 fLayerCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000208 // a path renderer may be holding onto resources
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000209 SkSafeSetNull(fPathRendererChain);
210 SkSafeSetNull(fSoftwarePathRenderer);
bsalomon3033b9f2015-04-13 11:09:56 -0700211
212 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000213}
214
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000215void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
bsalomon71cb0c22014-11-14 12:10:14 -0800216 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800217 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800218 }
219 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800220 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800221 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000222}
223
kkinnunenc6cb56f2014-06-24 00:12:27 -0700224GrTextContext* GrContext::createTextContext(GrRenderTarget* renderTarget,
joshualitt6e8cd962015-03-20 10:30:14 -0700225 SkGpuDevice* gpuDevice,
kkinnunenc6cb56f2014-06-24 00:12:27 -0700226 const SkDeviceProperties&
227 leakyProperties,
228 bool enableDistanceFieldFonts) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700229 if (fGpu->caps()->shaderCaps()->pathRenderingSupport() && renderTarget->isMultisampled()) {
egdaniel8dc7c3a2015-04-16 11:22:42 -0700230 GrStencilAttachment* sb = renderTarget->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800231 if (sb) {
joshualitt6e8cd962015-03-20 10:30:14 -0700232 return GrStencilAndCoverTextContext::Create(this, gpuDevice, leakyProperties);
bsalomon6bc1b5f2015-02-23 09:06:38 -0800233 }
jvanverth8c27a182014-10-14 08:45:50 -0700234 }
235
joshualitt9bd2daf2015-04-17 09:30:06 -0700236 return GrAtlasTextContext::Create(this, gpuDevice, leakyProperties, enableDistanceFieldFonts);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700237}
238
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000239////////////////////////////////////////////////////////////////////////////////
240
bsalomond2a6f4e2015-02-04 10:55:54 -0800241bool GrContext::isConfigTexturable(GrPixelConfig config) const {
242 return fGpu->caps()->isConfigTexturable(config);
243}
244
245bool GrContext::npotTextureTileSupport() const {
246 return fGpu->caps()->npotTextureTileSupport();
247}
248
bsalomon71cb0c22014-11-14 12:10:14 -0800249void GrContext::OverBudgetCB(void* data) {
bsalomon66a450f2014-11-13 13:19:10 -0800250 SkASSERT(data);
bsalomonf21dab92014-11-13 13:33:28 -0800251
bsalomon66a450f2014-11-13 13:19:10 -0800252 GrContext* context = reinterpret_cast<GrContext*>(data);
bsalomonf21dab92014-11-13 13:33:28 -0800253
254 // Flush the InOrderDrawBuffer to possibly free up some textures
255 context->fFlushToReduceCacheSize = true;
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000256}
257
joshualitt0db6dfa2015-04-10 07:01:30 -0700258void GrContext::TextBlobCacheOverBudgetCB(void* data) {
259 SkASSERT(data);
260
261 // Unlike the GrResourceCache, TextBlobs are drawn at the SkGpuDevice level, therefore they
262 // cannot use fFlushTorReduceCacheSize because it uses AutoCheckFlush. The solution is to move
263 // drawText calls to below the GrContext level, but this is not trivial because they call
264 // drawPath on SkGpuDevice
265 GrContext* context = reinterpret_cast<GrContext*>(data);
266 context->flush();
267}
268
bsalomon@google.com91958362011-06-13 17:58:13 +0000269int GrContext::getMaxTextureSize() const {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000270 return SkTMin(fGpu->caps()->maxTextureSize(), fMaxTextureSizeOverride);
bsalomon@google.com91958362011-06-13 17:58:13 +0000271}
272
273int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000274 return fGpu->caps()->maxRenderTargetSize();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000275}
276
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000277int GrContext::getMaxSampleCount() const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000278 return fGpu->caps()->maxSampleCount();
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000279}
280
bsalomon@google.com27847de2011-02-22 20:59:41 +0000281///////////////////////////////////////////////////////////////////////////////
282
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000283void GrContext::clear(const SkIRect* rect,
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000284 const GrColor color,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000285 bool canIgnoreRect,
bsalomon41ebbdd2014-08-04 08:31:39 -0700286 GrRenderTarget* renderTarget) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800287 RETURN_IF_ABANDONED
bsalomon41ebbdd2014-08-04 08:31:39 -0700288 ASSERT_OWNED_RESOURCE(renderTarget);
bsalomon89c62982014-11-03 12:08:42 -0800289 SkASSERT(renderTarget);
290
bsalomonf21dab92014-11-13 13:33:28 -0800291 AutoCheckFlush acf(this);
egdanield78a1682014-07-09 10:41:26 -0700292 GR_CREATE_TRACE_MARKER_CONTEXT("GrContext::clear", this);
joshualitt570d2f82015-02-25 13:19:48 -0800293 GrDrawTarget* target = this->prepareToDraw();
bsalomon41ebbdd2014-08-04 08:31:39 -0700294 if (NULL == target) {
295 return;
296 }
297 target->clear(rect, color, canIgnoreRect, renderTarget);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000298}
299
joshualitt25d9c152015-02-18 12:29:52 -0800300void GrContext::drawPaint(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800301 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800302 const GrPaint& origPaint,
303 const SkMatrix& viewMatrix) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800304 RETURN_IF_ABANDONED
bsalomon@google.com27847de2011-02-22 20:59:41 +0000305 // set rect to be big enough to fill the space, but not super-huge, so we
306 // don't overflow fixed-point implementations
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000307 SkRect r;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000308 r.setLTRB(0, 0,
joshualitt25d9c152015-02-18 12:29:52 -0800309 SkIntToScalar(rt->width()),
310 SkIntToScalar(rt->height()));
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000311 SkTCopyOnFirstWrite<GrPaint> paint(origPaint);
joshualitt8fc6c2d2014-12-22 15:27:05 -0800312
313 // by definition this fills the entire clip, no need for AA
314 if (paint->isAntiAlias()) {
315 paint.writable()->setAntiAlias(false);
316 }
317
318 bool isPerspective = viewMatrix.hasPerspective();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000319
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000320 // We attempt to map r by the inverse matrix and draw that. mapRect will
321 // map the four corners and bound them with a new rect. This will not
322 // produce a correct result for some perspective matrices.
joshualitt8fc6c2d2014-12-22 15:27:05 -0800323 if (!isPerspective) {
joshualitt5531d512014-12-17 15:50:11 -0800324 SkMatrix inverse;
325 if (!viewMatrix.invert(&inverse)) {
tfarina38406c82014-10-31 07:11:12 -0700326 SkDebugf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000327 return;
328 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000329 inverse.mapRect(&r);
joshualitt570d2f82015-02-25 13:19:48 -0800330 this->drawRect(rt, clip, *paint, viewMatrix, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000331 } else {
joshualitt8fc6c2d2014-12-22 15:27:05 -0800332 SkMatrix localMatrix;
333 if (!viewMatrix.invert(&localMatrix)) {
tfarina38406c82014-10-31 07:11:12 -0700334 SkDebugf("Could not invert matrix\n");
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000335 return;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000336 }
joshualitt8fc6c2d2014-12-22 15:27:05 -0800337
338 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -0800339 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -0800340 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, paint, &acf);
joshualitt8fc6c2d2014-12-22 15:27:05 -0800341 if (NULL == target) {
342 return;
343 }
344
345 GR_CREATE_TRACE_MARKER("GrContext::drawPaintWithPerspective", target);
joshualitt44701df2015-02-23 14:44:57 -0800346 target->drawRect(&pipelineBuilder,
347 paint->getColor(),
348 SkMatrix::I(),
349 r,
350 NULL,
351 &localMatrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000352 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000353}
354
bsalomon@google.com205d4602011-04-25 12:43:45 +0000355////////////////////////////////////////////////////////////////////////////////
356
bsalomonc30aaa02014-08-13 07:15:29 -0700357static inline bool is_irect(const SkRect& r) {
tfarina38406c82014-10-31 07:11:12 -0700358 return SkScalarIsInt(r.fLeft) && SkScalarIsInt(r.fTop) &&
359 SkScalarIsInt(r.fRight) && SkScalarIsInt(r.fBottom);
bsalomonc30aaa02014-08-13 07:15:29 -0700360}
361
bsalomon@google.com205d4602011-04-25 12:43:45 +0000362static bool apply_aa_to_rect(GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -0800363 GrPipelineBuilder* pipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -0800364 SkRect* devBoundRect,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000365 const SkRect& rect,
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000366 SkScalar strokeWidth,
joshualitt2e3b3e32014-12-09 13:31:14 -0800367 const SkMatrix& combinedMatrix,
368 GrColor color) {
egdaniel8dd688b2015-01-22 10:16:09 -0800369 if (pipelineBuilder->getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000370 return false;
371 }
372
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000373#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000374 if (strokeWidth >= 0) {
375#endif
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000376 if (!combinedMatrix.preservesAxisAlignment()) {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000377 return false;
378 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000379
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000380#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000381 } else {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000382 if (!combinedMatrix.preservesRightAngles()) {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000383 return false;
384 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000385 }
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000386#endif
bsalomon@google.com205d4602011-04-25 12:43:45 +0000387
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000388 combinedMatrix.mapRect(devBoundRect, rect);
derekfe638d1c2014-12-02 13:51:29 -0800389 if (!combinedMatrix.rectStaysRect()) {
390 return true;
391 }
392
bsalomonc30aaa02014-08-13 07:15:29 -0700393 if (strokeWidth < 0) {
394 return !is_irect(*devBoundRect);
395 }
robertphillips@google.com28ac96e2013-05-13 13:38:35 +0000396
bsalomon9c0822a2014-08-11 11:07:48 -0700397 return true;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000398}
399
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000400static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) {
401 return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
402 point.fY >= rect.fTop && point.fY <= rect.fBottom;
403}
404
joshualitt0823bfa2015-02-27 10:06:15 -0800405class StrokeRectBatch : public GrBatch {
406public:
407 struct Geometry {
408 GrColor fColor;
409 SkMatrix fViewMatrix;
410 SkRect fRect;
411 SkScalar fStrokeWidth;
412 };
413
joshualitt4c977862015-05-13 08:00:56 -0700414 static GrBatch* Create(const Geometry& geometry, bool snapToPixelCenters) {
415 return SkNEW_ARGS(StrokeRectBatch, (geometry, snapToPixelCenters));
joshualitt0823bfa2015-02-27 10:06:15 -0800416 }
417
mtklein36352bf2015-03-25 18:17:31 -0700418 const char* name() const override { return "StrokeRectBatch"; }
joshualitt0823bfa2015-02-27 10:06:15 -0800419
mtklein36352bf2015-03-25 18:17:31 -0700420 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt0823bfa2015-02-27 10:06:15 -0800421 // When this is called on a batch, there is only one geometry bundle
422 out->setKnownFourComponents(fGeoData[0].fColor);
423 }
424
mtklein36352bf2015-03-25 18:17:31 -0700425 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualitt0823bfa2015-02-27 10:06:15 -0800426 out->setKnownSingleComponent(0xff);
427 }
428
mtklein36352bf2015-03-25 18:17:31 -0700429 void initBatchTracker(const GrPipelineInfo& init) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800430 // Handle any color overrides
431 if (init.fColorIgnored) {
432 fGeoData[0].fColor = GrColor_ILLEGAL;
433 } else if (GrColor_ILLEGAL != init.fOverrideColor) {
434 fGeoData[0].fColor = init.fOverrideColor;
435 }
436
437 // setup batch properties
438 fBatch.fColorIgnored = init.fColorIgnored;
439 fBatch.fColor = fGeoData[0].fColor;
440 fBatch.fUsesLocalCoords = init.fUsesLocalCoords;
441 fBatch.fCoverageIgnored = init.fCoverageIgnored;
442 }
443
mtklein36352bf2015-03-25 18:17:31 -0700444 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800445 SkAutoTUnref<const GrGeometryProcessor> gp(
446 GrDefaultGeoProcFactory::Create(GrDefaultGeoProcFactory::kPosition_GPType,
447 this->color(),
joshualittb8c241a2015-05-19 08:23:30 -0700448 this->usesLocalCoords(),
449 this->coverageIgnored(),
joshualitt0823bfa2015-02-27 10:06:15 -0800450 this->viewMatrix(),
451 SkMatrix::I()));
452
453 batchTarget->initDraw(gp, pipeline);
454
joshualitt0823bfa2015-02-27 10:06:15 -0800455 size_t vertexStride = gp->getVertexStride();
456
457 SkASSERT(vertexStride == sizeof(GrDefaultGeoProcFactory::PositionAttr));
458
459 Geometry& args = fGeoData[0];
460
461 int vertexCount = kVertsPerHairlineRect;
462 if (args.fStrokeWidth > 0) {
463 vertexCount = kVertsPerStrokeRect;
464 }
465
466 const GrVertexBuffer* vertexBuffer;
467 int firstVertex;
468
robertphillipse40d3972015-05-07 09:51:43 -0700469 void* verts = batchTarget->makeVertSpace(vertexStride, vertexCount,
470 &vertexBuffer, &firstVertex);
joshualitt0823bfa2015-02-27 10:06:15 -0800471
bsalomoncb8979d2015-05-05 09:51:38 -0700472 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -0800473 SkDebugf("Could not allocate vertices\n");
474 return;
475 }
476
bsalomoncb8979d2015-05-05 09:51:38 -0700477 SkPoint* vertex = reinterpret_cast<SkPoint*>(verts);
joshualitt0823bfa2015-02-27 10:06:15 -0800478
479 GrPrimitiveType primType;
480
481 if (args.fStrokeWidth > 0) {;
482 primType = kTriangleStrip_GrPrimitiveType;
483 args.fRect.sort();
484 this->setStrokeRectStrip(vertex, args.fRect, args.fStrokeWidth);
485 } else {
486 // hairline
487 primType = kLineStrip_GrPrimitiveType;
488 vertex[0].set(args.fRect.fLeft, args.fRect.fTop);
489 vertex[1].set(args.fRect.fRight, args.fRect.fTop);
490 vertex[2].set(args.fRect.fRight, args.fRect.fBottom);
491 vertex[3].set(args.fRect.fLeft, args.fRect.fBottom);
492 vertex[4].set(args.fRect.fLeft, args.fRect.fTop);
493 }
494
bsalomoncb8979d2015-05-05 09:51:38 -0700495 GrVertices vertices;
496 vertices.init(primType, vertexBuffer, firstVertex, vertexCount);
497 batchTarget->draw(vertices);
joshualitt0823bfa2015-02-27 10:06:15 -0800498 }
499
500 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
501
502private:
joshualitt4c977862015-05-13 08:00:56 -0700503 StrokeRectBatch(const Geometry& geometry, bool snapToPixelCenters) {
joshualitt0823bfa2015-02-27 10:06:15 -0800504 this->initClassID<StrokeRectBatch>();
505
506 fBatch.fHairline = geometry.fStrokeWidth == 0;
507
508 fGeoData.push_back(geometry);
joshualitt99c7c072015-05-01 13:43:30 -0700509
510 // setup bounds
511 fBounds = geometry.fRect;
512 SkScalar rad = SkScalarHalf(geometry.fStrokeWidth);
513 fBounds.outset(rad, rad);
514 geometry.fViewMatrix.mapRect(&fBounds);
joshualitt4c977862015-05-13 08:00:56 -0700515
516 // If our caller snaps to pixel centers then we have to round out the bounds
517 if (snapToPixelCenters) {
518 fBounds.roundOut();
519 }
joshualitt0823bfa2015-02-27 10:06:15 -0800520 }
521
522 /* create a triangle strip that strokes the specified rect. There are 8
523 unique vertices, but we repeat the last 2 to close up. Alternatively we
524 could use an indices array, and then only send 8 verts, but not sure that
525 would be faster.
526 */
527 void setStrokeRectStrip(SkPoint verts[10], const SkRect& rect, SkScalar width) {
528 const SkScalar rad = SkScalarHalf(width);
529 // TODO we should be able to enable this assert, but we'd have to filter these draws
530 // this is a bug
531 //SkASSERT(rad < rect.width() / 2 && rad < rect.height() / 2);
532
533 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
534 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
535 verts[2].set(rect.fRight - rad, rect.fTop + rad);
536 verts[3].set(rect.fRight + rad, rect.fTop - rad);
537 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
538 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
539 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
540 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
541 verts[8] = verts[0];
542 verts[9] = verts[1];
543 }
544
545
546 GrColor color() const { return fBatch.fColor; }
547 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
548 bool colorIgnored() const { return fBatch.fColorIgnored; }
549 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
550 bool hairline() const { return fBatch.fHairline; }
joshualittb8c241a2015-05-19 08:23:30 -0700551 bool coverageIgnored() const { return fBatch.fCoverageIgnored; }
joshualitt0823bfa2015-02-27 10:06:15 -0800552
mtklein36352bf2015-03-25 18:17:31 -0700553 bool onCombineIfPossible(GrBatch* t) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800554 // StrokeRectBatch* that = t->cast<StrokeRectBatch>();
555
556 // NonAA stroke rects cannot batch right now
557 // TODO make these batchable
558 return false;
559 }
560
561 struct BatchTracker {
562 GrColor fColor;
563 bool fUsesLocalCoords;
564 bool fColorIgnored;
565 bool fCoverageIgnored;
566 bool fHairline;
567 };
568
569 const static int kVertsPerHairlineRect = 5;
570 const static int kVertsPerStrokeRect = 10;
571
572 BatchTracker fBatch;
573 SkSTArray<1, Geometry, true> fGeoData;
574};
575
joshualitt25d9c152015-02-18 12:29:52 -0800576void GrContext::drawRect(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800577 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800578 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -0800579 const SkMatrix& viewMatrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000580 const SkRect& rect,
bsalomon01c8da12014-08-04 09:21:30 -0700581 const GrStrokeInfo* strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800582 RETURN_IF_ABANDONED
bsalomon49f085d2014-09-05 13:34:00 -0700583 if (strokeInfo && strokeInfo->isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700584 SkPath path;
kkinnunen806236e2015-05-21 22:45:56 -0700585 path.setIsVolatile(true);
egdanield58a0ba2014-06-11 10:30:05 -0700586 path.addRect(rect);
joshualitt570d2f82015-02-25 13:19:48 -0800587 this->drawPath(rt, clip, paint, viewMatrix, path, *strokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -0700588 return;
589 }
590
bsalomonf21dab92014-11-13 13:33:28 -0800591 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -0800592 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -0800593 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700594 if (NULL == target) {
595 return;
596 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000597
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000598 GR_CREATE_TRACE_MARKER("GrContext::drawRect", target);
kkinnunend156d362015-05-18 22:23:54 -0700599 SkScalar width = NULL == strokeInfo ? -1 : strokeInfo->getWidth();
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000600
601 // Check if this is a full RT draw and can be replaced with a clear. We don't bother checking
602 // cases where the RT is fully inside a stroke.
603 if (width < 0) {
604 SkRect rtRect;
egdaniel8dd688b2015-01-22 10:16:09 -0800605 pipelineBuilder.getRenderTarget()->getBoundsRect(&rtRect);
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000606 SkRect clipSpaceRTRect = rtRect;
joshualitt570d2f82015-02-25 13:19:48 -0800607 bool checkClip = GrClip::kWideOpen_ClipType != clip.clipType();
joshualitt44701df2015-02-23 14:44:57 -0800608 if (checkClip) {
joshualitt570d2f82015-02-25 13:19:48 -0800609 clipSpaceRTRect.offset(SkIntToScalar(clip.origin().fX),
610 SkIntToScalar(clip.origin().fY));
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000611 }
612 // Does the clip contain the entire RT?
joshualitt570d2f82015-02-25 13:19:48 -0800613 if (!checkClip || clip.quickContains(clipSpaceRTRect)) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000614 SkMatrix invM;
joshualitt8059eb92014-12-29 15:10:07 -0800615 if (!viewMatrix.invert(&invM)) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000616 return;
617 }
618 // Does the rect bound the RT?
619 SkPoint srcSpaceRTQuad[4];
620 invM.mapRectToQuad(srcSpaceRTQuad, rtRect);
621 if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) &&
622 rect_contains_inclusive(rect, srcSpaceRTQuad[1]) &&
623 rect_contains_inclusive(rect, srcSpaceRTQuad[2]) &&
624 rect_contains_inclusive(rect, srcSpaceRTQuad[3])) {
625 // Will it blend?
626 GrColor clearColor;
627 if (paint.isOpaqueAndConstantColor(&clearColor)) {
joshualitt25d9c152015-02-18 12:29:52 -0800628 target->clear(NULL, clearColor, true, rt);
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000629 return;
630 }
631 }
632 }
633 }
634
joshualitt2e3b3e32014-12-09 13:31:14 -0800635 GrColor color = paint.getColor();
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000636 SkRect devBoundRect;
egdaniel8dd688b2015-01-22 10:16:09 -0800637 bool needAA = paint.isAntiAlias() && !pipelineBuilder.getRenderTarget()->isMultisampled();
638 bool doAA = needAA && apply_aa_to_rect(target, &pipelineBuilder, &devBoundRect, rect, width,
joshualitt8059eb92014-12-29 15:10:07 -0800639 viewMatrix, color);
egdanield58a0ba2014-06-11 10:30:05 -0700640
bsalomon@google.com205d4602011-04-25 12:43:45 +0000641 if (doAA) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000642 if (width >= 0) {
joshualitt9853cce2014-11-17 14:22:48 -0800643 fAARectRenderer->strokeAARect(target,
egdaniel8dd688b2015-01-22 10:16:09 -0800644 &pipelineBuilder,
joshualitt2e3b3e32014-12-09 13:31:14 -0800645 color,
joshualitt8059eb92014-12-29 15:10:07 -0800646 viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800647 rect,
joshualitt9853cce2014-11-17 14:22:48 -0800648 devBoundRect,
kkinnunend156d362015-05-18 22:23:54 -0700649 *strokeInfo);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000650 } else {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000651 // filled AA rect
joshualitt8059eb92014-12-29 15:10:07 -0800652 fAARectRenderer->fillAARect(target,
egdaniel8dd688b2015-01-22 10:16:09 -0800653 &pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800654 color,
655 viewMatrix,
656 rect,
joshualittd27f73e2014-12-29 07:43:36 -0800657 devBoundRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000658 }
659 return;
660 }
661
bsalomon@google.com27847de2011-02-22 20:59:41 +0000662 if (width >= 0) {
joshualitt0823bfa2015-02-27 10:06:15 -0800663 StrokeRectBatch::Geometry geometry;
664 geometry.fViewMatrix = viewMatrix;
665 geometry.fColor = color;
666 geometry.fRect = rect;
667 geometry.fStrokeWidth = width;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000668
joshualitt4c977862015-05-13 08:00:56 -0700669 // Non-AA hairlines are snapped to pixel centers to make which pixels are hit deterministic
670 bool snapToPixelCenters = (0 == width && !rt->isMultisampled());
671 SkAutoTUnref<GrBatch> batch(StrokeRectBatch::Create(geometry, snapToPixelCenters));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000672
bsalomond79c5492015-04-27 10:07:04 -0700673 // Depending on sub-pixel coordinates and the particular GPU, we may lose a corner of
674 // hairline rects. We jam all the vertices to pixel centers to avoid this, but not when MSAA
675 // is enabled because it can cause ugly artifacts.
676 pipelineBuilder.setState(GrPipelineBuilder::kSnapVerticesToPixelCenters_Flag,
joshualitt4c977862015-05-13 08:00:56 -0700677 snapToPixelCenters);
joshualitt99c7c072015-05-01 13:43:30 -0700678 target->drawBatch(&pipelineBuilder, batch);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000679 } else {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000680 // filled BW rect
egdaniel8dd688b2015-01-22 10:16:09 -0800681 target->drawSimpleRect(&pipelineBuilder, color, viewMatrix, rect);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000682 }
683}
684
joshualitt25d9c152015-02-18 12:29:52 -0800685void GrContext::drawNonAARectToRect(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800686 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800687 const GrPaint& paint,
joshualitt16b27892014-12-18 07:47:16 -0800688 const SkMatrix& viewMatrix,
689 const SkRect& rectToDraw,
690 const SkRect& localRect,
691 const SkMatrix* localMatrix) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800692 RETURN_IF_ABANDONED
bsalomonf21dab92014-11-13 13:33:28 -0800693 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -0800694 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -0800695 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700696 if (NULL == target) {
697 return;
698 }
bsalomon@google.com64386952013-02-08 21:22:44 +0000699
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000700 GR_CREATE_TRACE_MARKER("GrContext::drawRectToRect", target);
701
joshualitt44701df2015-02-23 14:44:57 -0800702 target->drawRect(&pipelineBuilder,
703 paint.getColor(),
704 viewMatrix,
705 rectToDraw,
706 &localRect,
egdaniel8dd688b2015-01-22 10:16:09 -0800707 localMatrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000708}
709
joshualitt0823bfa2015-02-27 10:06:15 -0800710static const GrGeometryProcessor* set_vertex_attributes(bool hasLocalCoords,
711 bool hasColors,
joshualitt56995b52014-12-11 15:44:02 -0800712 int* colorOffset,
713 int* texOffset,
joshualitt8059eb92014-12-29 15:10:07 -0800714 GrColor color,
joshualittb8c241a2015-05-19 08:23:30 -0700715 const SkMatrix& viewMatrix,
716 bool coverageIgnored) {
robertphillips@google.com42903302013-04-20 12:26:07 +0000717 *texOffset = -1;
718 *colorOffset = -1;
joshualitt5478d422014-11-14 16:00:38 -0800719 uint32_t flags = GrDefaultGeoProcFactory::kPosition_GPType;
joshualitt0823bfa2015-02-27 10:06:15 -0800720 if (hasLocalCoords && hasColors) {
joshualitt5478d422014-11-14 16:00:38 -0800721 *colorOffset = sizeof(SkPoint);
722 *texOffset = sizeof(SkPoint) + sizeof(GrColor);
723 flags |= GrDefaultGeoProcFactory::kColor_GPType |
724 GrDefaultGeoProcFactory::kLocalCoord_GPType;
joshualitt0823bfa2015-02-27 10:06:15 -0800725 } else if (hasLocalCoords) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000726 *texOffset = sizeof(SkPoint);
joshualitt5478d422014-11-14 16:00:38 -0800727 flags |= GrDefaultGeoProcFactory::kLocalCoord_GPType;
joshualitt0823bfa2015-02-27 10:06:15 -0800728 } else if (hasColors) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000729 *colorOffset = sizeof(SkPoint);
joshualitt5478d422014-11-14 16:00:38 -0800730 flags |= GrDefaultGeoProcFactory::kColor_GPType;
robertphillips@google.com42903302013-04-20 12:26:07 +0000731 }
joshualittb8c241a2015-05-19 08:23:30 -0700732 return GrDefaultGeoProcFactory::Create(flags, color, hasLocalCoords, coverageIgnored,
733 viewMatrix, SkMatrix::I());
robertphillips@google.com42903302013-04-20 12:26:07 +0000734}
735
joshualitt0823bfa2015-02-27 10:06:15 -0800736class DrawVerticesBatch : public GrBatch {
737public:
738 struct Geometry {
739 GrColor fColor;
740 SkTDArray<SkPoint> fPositions;
741 SkTDArray<uint16_t> fIndices;
742 SkTDArray<GrColor> fColors;
743 SkTDArray<SkPoint> fLocalCoords;
744 };
745
746 static GrBatch* Create(const Geometry& geometry, GrPrimitiveType primitiveType,
747 const SkMatrix& viewMatrix,
748 const SkPoint* positions, int vertexCount,
749 const uint16_t* indices, int indexCount,
joshualitt99c7c072015-05-01 13:43:30 -0700750 const GrColor* colors, const SkPoint* localCoords,
751 const SkRect& bounds) {
joshualitt0823bfa2015-02-27 10:06:15 -0800752 return SkNEW_ARGS(DrawVerticesBatch, (geometry, primitiveType, viewMatrix, positions,
753 vertexCount, indices, indexCount, colors,
joshualitt99c7c072015-05-01 13:43:30 -0700754 localCoords, bounds));
joshualitt0823bfa2015-02-27 10:06:15 -0800755 }
756
mtklein36352bf2015-03-25 18:17:31 -0700757 const char* name() const override { return "DrawVerticesBatch"; }
joshualitt0823bfa2015-02-27 10:06:15 -0800758
mtklein36352bf2015-03-25 18:17:31 -0700759 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt0823bfa2015-02-27 10:06:15 -0800760 // When this is called on a batch, there is only one geometry bundle
761 if (this->hasColors()) {
762 out->setUnknownFourComponents();
763 } else {
764 out->setKnownFourComponents(fGeoData[0].fColor);
765 }
766 }
767
mtklein36352bf2015-03-25 18:17:31 -0700768 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualittd4c7d552015-02-27 13:00:56 -0800769 out->setKnownSingleComponent(0xff);
joshualitt0823bfa2015-02-27 10:06:15 -0800770 }
771
mtklein36352bf2015-03-25 18:17:31 -0700772 void initBatchTracker(const GrPipelineInfo& init) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800773 // Handle any color overrides
774 if (init.fColorIgnored) {
775 fGeoData[0].fColor = GrColor_ILLEGAL;
776 } else if (GrColor_ILLEGAL != init.fOverrideColor) {
777 fGeoData[0].fColor = init.fOverrideColor;
778 }
779
780 // setup batch properties
781 fBatch.fColorIgnored = init.fColorIgnored;
782 fBatch.fColor = fGeoData[0].fColor;
783 fBatch.fUsesLocalCoords = init.fUsesLocalCoords;
784 fBatch.fCoverageIgnored = init.fCoverageIgnored;
785 }
786
mtklein36352bf2015-03-25 18:17:31 -0700787 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800788 int colorOffset = -1, texOffset = -1;
789 SkAutoTUnref<const GrGeometryProcessor> gp(
790 set_vertex_attributes(this->hasLocalCoords(), this->hasColors(), &colorOffset,
joshualittb8c241a2015-05-19 08:23:30 -0700791 &texOffset, this->color(), this->viewMatrix(),
792 this->coverageIgnored()));
joshualitt0823bfa2015-02-27 10:06:15 -0800793
794 batchTarget->initDraw(gp, pipeline);
795
joshualitt0823bfa2015-02-27 10:06:15 -0800796 size_t vertexStride = gp->getVertexStride();
797
798 SkASSERT(vertexStride == sizeof(SkPoint) + (this->hasLocalCoords() ? sizeof(SkPoint) : 0)
799 + (this->hasColors() ? sizeof(GrColor) : 0));
800
801 int instanceCount = fGeoData.count();
802
803 const GrVertexBuffer* vertexBuffer;
804 int firstVertex;
805
robertphillipse40d3972015-05-07 09:51:43 -0700806 void* verts = batchTarget->makeVertSpace(vertexStride, this->vertexCount(),
807 &vertexBuffer, &firstVertex);
joshualitt0823bfa2015-02-27 10:06:15 -0800808
bsalomoncb8979d2015-05-05 09:51:38 -0700809 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -0800810 SkDebugf("Could not allocate vertices\n");
811 return;
812 }
813
bsalomonb5238a72015-05-05 07:49:49 -0700814 const GrIndexBuffer* indexBuffer = NULL;
815 int firstIndex = 0;
joshualitt0823bfa2015-02-27 10:06:15 -0800816
robertphillipse40d3972015-05-07 09:51:43 -0700817 uint16_t* indices = NULL;
joshualitt0823bfa2015-02-27 10:06:15 -0800818 if (this->hasIndices()) {
robertphillipse40d3972015-05-07 09:51:43 -0700819 indices = batchTarget->makeIndexSpace(this->indexCount(), &indexBuffer, &firstIndex);
joshualitt4b31de82015-03-05 14:33:41 -0800820
821 if (!indices) {
822 SkDebugf("Could not allocate indices\n");
823 return;
824 }
joshualitt0823bfa2015-02-27 10:06:15 -0800825 }
826
827 int indexOffset = 0;
828 int vertexOffset = 0;
829 for (int i = 0; i < instanceCount; i++) {
830 const Geometry& args = fGeoData[i];
831
832 // TODO we can actually cache this interleaved and then just memcopy
833 if (this->hasIndices()) {
834 for (int j = 0; j < args.fIndices.count(); ++j, ++indexOffset) {
robertphillipse40d3972015-05-07 09:51:43 -0700835 *(indices + indexOffset) = args.fIndices[j] + vertexOffset;
joshualitt0823bfa2015-02-27 10:06:15 -0800836 }
837 }
838
839 for (int j = 0; j < args.fPositions.count(); ++j) {
bsalomoncb8979d2015-05-05 09:51:38 -0700840 *((SkPoint*)verts) = args.fPositions[j];
joshualitt0823bfa2015-02-27 10:06:15 -0800841 if (this->hasColors()) {
bsalomoncb8979d2015-05-05 09:51:38 -0700842 *(GrColor*)((intptr_t)verts + colorOffset) = args.fColors[j];
joshualitt0823bfa2015-02-27 10:06:15 -0800843 }
844 if (this->hasLocalCoords()) {
bsalomoncb8979d2015-05-05 09:51:38 -0700845 *(SkPoint*)((intptr_t)verts + texOffset) = args.fLocalCoords[j];
joshualitt0823bfa2015-02-27 10:06:15 -0800846 }
bsalomoncb8979d2015-05-05 09:51:38 -0700847 verts = (void*)((intptr_t)verts + vertexStride);
joshualitt0823bfa2015-02-27 10:06:15 -0800848 vertexOffset++;
849 }
850 }
851
bsalomoncb8979d2015-05-05 09:51:38 -0700852 GrVertices vertices;
joshualitt0823bfa2015-02-27 10:06:15 -0800853 if (this->hasIndices()) {
bsalomoncb8979d2015-05-05 09:51:38 -0700854 vertices.initIndexed(this->primitiveType(), vertexBuffer, indexBuffer, firstVertex,
bsalomonb5238a72015-05-05 07:49:49 -0700855 firstIndex, this->vertexCount(), this->indexCount());
856
joshualitt0823bfa2015-02-27 10:06:15 -0800857 } else {
bsalomoncb8979d2015-05-05 09:51:38 -0700858 vertices.init(this->primitiveType(), vertexBuffer, firstVertex, this->vertexCount());
joshualitt0823bfa2015-02-27 10:06:15 -0800859 }
bsalomoncb8979d2015-05-05 09:51:38 -0700860 batchTarget->draw(vertices);
joshualitt0823bfa2015-02-27 10:06:15 -0800861 }
862
863 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
864
865private:
866 DrawVerticesBatch(const Geometry& geometry, GrPrimitiveType primitiveType,
867 const SkMatrix& viewMatrix,
868 const SkPoint* positions, int vertexCount,
869 const uint16_t* indices, int indexCount,
joshualitt99c7c072015-05-01 13:43:30 -0700870 const GrColor* colors, const SkPoint* localCoords, const SkRect& bounds) {
joshualitt0823bfa2015-02-27 10:06:15 -0800871 this->initClassID<DrawVerticesBatch>();
872 SkASSERT(positions);
873
874 fBatch.fViewMatrix = viewMatrix;
875 Geometry& installedGeo = fGeoData.push_back(geometry);
876
877 installedGeo.fPositions.append(vertexCount, positions);
878 if (indices) {
879 installedGeo.fIndices.append(indexCount, indices);
880 fBatch.fHasIndices = true;
881 } else {
882 fBatch.fHasIndices = false;
883 }
884
885 if (colors) {
886 installedGeo.fColors.append(vertexCount, colors);
887 fBatch.fHasColors = true;
888 } else {
889 fBatch.fHasColors = false;
890 }
891
892 if (localCoords) {
893 installedGeo.fLocalCoords.append(vertexCount, localCoords);
894 fBatch.fHasLocalCoords = true;
895 } else {
896 fBatch.fHasLocalCoords = false;
897 }
898 fBatch.fVertexCount = vertexCount;
899 fBatch.fIndexCount = indexCount;
900 fBatch.fPrimitiveType = primitiveType;
joshualitt99c7c072015-05-01 13:43:30 -0700901
902 this->setBounds(bounds);
joshualitt0823bfa2015-02-27 10:06:15 -0800903 }
904
905 GrPrimitiveType primitiveType() const { return fBatch.fPrimitiveType; }
906 bool batchablePrimitiveType() const {
907 return kTriangles_GrPrimitiveType == fBatch.fPrimitiveType ||
908 kLines_GrPrimitiveType == fBatch.fPrimitiveType ||
909 kPoints_GrPrimitiveType == fBatch.fPrimitiveType;
910 }
911 GrColor color() const { return fBatch.fColor; }
912 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
913 bool colorIgnored() const { return fBatch.fColorIgnored; }
914 const SkMatrix& viewMatrix() const { return fBatch.fViewMatrix; }
915 bool hasColors() const { return fBatch.fHasColors; }
916 bool hasIndices() const { return fBatch.fHasIndices; }
917 bool hasLocalCoords() const { return fBatch.fHasLocalCoords; }
918 int vertexCount() const { return fBatch.fVertexCount; }
919 int indexCount() const { return fBatch.fIndexCount; }
joshualittb8c241a2015-05-19 08:23:30 -0700920 bool coverageIgnored() const { return fBatch.fCoverageIgnored; }
joshualitt0823bfa2015-02-27 10:06:15 -0800921
mtklein36352bf2015-03-25 18:17:31 -0700922 bool onCombineIfPossible(GrBatch* t) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800923 DrawVerticesBatch* that = t->cast<DrawVerticesBatch>();
924
925 if (!this->batchablePrimitiveType() || this->primitiveType() != that->primitiveType()) {
926 return false;
927 }
928
929 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
930
931 // We currently use a uniform viewmatrix for this batch
932 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
933 return false;
934 }
935
936 if (this->hasColors() != that->hasColors()) {
937 return false;
938 }
939
940 if (this->hasIndices() != that->hasIndices()) {
941 return false;
942 }
943
944 if (this->hasLocalCoords() != that->hasLocalCoords()) {
945 return false;
946 }
947
948 if (!this->hasColors() && this->color() != that->color()) {
949 return false;
950 }
951
952 if (this->color() != that->color()) {
953 fBatch.fColor = GrColor_ILLEGAL;
954 }
955 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
956 fBatch.fVertexCount += that->vertexCount();
957 fBatch.fIndexCount += that->indexCount();
joshualitt99c7c072015-05-01 13:43:30 -0700958
959 this->joinBounds(that->bounds());
joshualitt0823bfa2015-02-27 10:06:15 -0800960 return true;
961 }
962
963 struct BatchTracker {
964 GrPrimitiveType fPrimitiveType;
965 SkMatrix fViewMatrix;
966 GrColor fColor;
967 bool fUsesLocalCoords;
968 bool fColorIgnored;
969 bool fCoverageIgnored;
970 bool fHasColors;
971 bool fHasIndices;
972 bool fHasLocalCoords;
973 int fVertexCount;
974 int fIndexCount;
975 };
976
977 BatchTracker fBatch;
978 SkSTArray<1, Geometry, true> fGeoData;
979};
980
joshualitt25d9c152015-02-18 12:29:52 -0800981void GrContext::drawVertices(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800982 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800983 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -0800984 const SkMatrix& viewMatrix,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000985 GrPrimitiveType primitiveType,
986 int vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000987 const SkPoint positions[],
988 const SkPoint texCoords[],
bsalomon@google.com27847de2011-02-22 20:59:41 +0000989 const GrColor colors[],
990 const uint16_t indices[],
991 int indexCount) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800992 RETURN_IF_ABANDONED
bsalomonf21dab92014-11-13 13:33:28 -0800993 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -0800994 GrPipelineBuilder pipelineBuilder;
commit-bot@chromium.org5a567932014-01-08 21:26:09 +0000995
joshualitt570d2f82015-02-25 13:19:48 -0800996 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700997 if (NULL == target) {
998 return;
999 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001000
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001001 GR_CREATE_TRACE_MARKER("GrContext::drawVertices", target);
1002
joshualitta09c0462015-04-30 14:03:02 -07001003 // TODO clients should give us bounds
1004 SkRect bounds;
1005 if (!bounds.setBoundsCheck(positions, vertexCount)) {
1006 SkDebugf("drawVertices call empty bounds\n");
1007 return;
1008 }
1009
joshualitt0c14bcc2015-05-05 13:58:20 -07001010 viewMatrix.mapRect(&bounds);
1011
joshualitt4c977862015-05-13 08:00:56 -07001012 // If we don't have AA then we outset for a half pixel in each direction to account for
1013 // snapping
1014 if (!paint.isAntiAlias()) {
1015 bounds.outset(0.5f, 0.5f);
1016 }
1017
joshualitt0823bfa2015-02-27 10:06:15 -08001018 DrawVerticesBatch::Geometry geometry;
1019 geometry.fColor = paint.getColor();
joshualitt0823bfa2015-02-27 10:06:15 -08001020 SkAutoTUnref<GrBatch> batch(DrawVerticesBatch::Create(geometry, primitiveType, viewMatrix,
1021 positions, vertexCount, indices,
joshualitt99c7c072015-05-01 13:43:30 -07001022 indexCount, colors, texCoords,
1023 bounds));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001024
joshualitt99c7c072015-05-01 13:43:30 -07001025 target->drawBatch(&pipelineBuilder, batch);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001026}
1027
bsalomon@google.com06afe7b2011-04-26 15:31:40 +00001028///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com93c96602012-04-27 13:05:21 +00001029
joshualitt25d9c152015-02-18 12:29:52 -08001030void GrContext::drawRRect(GrRenderTarget*rt,
joshualitt570d2f82015-02-25 13:19:48 -08001031 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001032 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001033 const SkMatrix& viewMatrix,
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001034 const SkRRect& rrect,
egdanield58a0ba2014-06-11 10:30:05 -07001035 const GrStrokeInfo& strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001036 RETURN_IF_ABANDONED
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001037 if (rrect.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001038 return;
1039 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001040
egdanield58a0ba2014-06-11 10:30:05 -07001041 if (strokeInfo.isDashed()) {
1042 SkPath path;
kkinnunen806236e2015-05-21 22:45:56 -07001043 path.setIsVolatile(true);
egdanield58a0ba2014-06-11 10:30:05 -07001044 path.addRRect(rrect);
joshualitt570d2f82015-02-25 13:19:48 -08001045 this->drawPath(rt, clip, paint, viewMatrix, path, strokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -07001046 return;
1047 }
1048
bsalomonf21dab92014-11-13 13:33:28 -08001049 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001050 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -08001051 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001052 if (NULL == target) {
1053 return;
1054 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001055
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001056 GR_CREATE_TRACE_MARKER("GrContext::drawRRect", target);
1057
joshualitt2e3b3e32014-12-09 13:31:14 -08001058 GrColor color = paint.getColor();
joshualitt44701df2015-02-23 14:44:57 -08001059 if (!fOvalRenderer->drawRRect(target,
1060 &pipelineBuilder,
1061 color,
1062 viewMatrix,
1063 paint.isAntiAlias(),
1064 rrect,
kkinnunend156d362015-05-18 22:23:54 -07001065 strokeInfo)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001066 SkPath path;
kkinnunen806236e2015-05-21 22:45:56 -07001067 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001068 path.addRRect(rrect);
egdaniel8dd688b2015-01-22 10:16:09 -08001069 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(),
1070 path, strokeInfo);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001071 }
1072}
1073
1074///////////////////////////////////////////////////////////////////////////////
1075
joshualitt25d9c152015-02-18 12:29:52 -08001076void GrContext::drawDRRect(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -08001077 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001078 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001079 const SkMatrix& viewMatrix,
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001080 const SkRRect& outer,
1081 const SkRRect& inner) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001082 RETURN_IF_ABANDONED
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001083 if (outer.isEmpty()) {
1084 return;
1085 }
1086
bsalomonf21dab92014-11-13 13:33:28 -08001087 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001088 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -08001089 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001090
1091 GR_CREATE_TRACE_MARKER("GrContext::drawDRRect", target);
1092
joshualitt2e3b3e32014-12-09 13:31:14 -08001093 GrColor color = paint.getColor();
joshualitt44701df2015-02-23 14:44:57 -08001094 if (!fOvalRenderer->drawDRRect(target,
1095 &pipelineBuilder,
1096 color,
1097 viewMatrix,
1098 paint.isAntiAlias(),
1099 outer,
1100 inner)) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001101 SkPath path;
kkinnunen806236e2015-05-21 22:45:56 -07001102 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001103 path.addRRect(inner);
1104 path.addRRect(outer);
1105 path.setFillType(SkPath::kEvenOdd_FillType);
egdanield58a0ba2014-06-11 10:30:05 -07001106 GrStrokeInfo fillRec(SkStrokeRec::kFill_InitStyle);
egdaniel8dd688b2015-01-22 10:16:09 -08001107 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(),
1108 path, fillRec);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001109 }
1110}
1111
1112///////////////////////////////////////////////////////////////////////////////
1113
joshualitt570d2f82015-02-25 13:19:48 -08001114void GrContext::drawOval(GrRenderTarget* rt,
1115 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001116 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001117 const SkMatrix& viewMatrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001118 const SkRect& oval,
egdanield58a0ba2014-06-11 10:30:05 -07001119 const GrStrokeInfo& strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001120 RETURN_IF_ABANDONED
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001121 if (oval.isEmpty()) {
1122 return;
1123 }
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001124
egdanield58a0ba2014-06-11 10:30:05 -07001125 if (strokeInfo.isDashed()) {
1126 SkPath path;
kkinnunen806236e2015-05-21 22:45:56 -07001127 path.setIsVolatile(true);
egdanield58a0ba2014-06-11 10:30:05 -07001128 path.addOval(oval);
joshualitt570d2f82015-02-25 13:19:48 -08001129 this->drawPath(rt, clip, paint, viewMatrix, path, strokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -07001130 return;
1131 }
1132
bsalomonf21dab92014-11-13 13:33:28 -08001133 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001134 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -08001135 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001136 if (NULL == target) {
1137 return;
1138 }
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001139
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001140 GR_CREATE_TRACE_MARKER("GrContext::drawOval", target);
1141
joshualitt2e3b3e32014-12-09 13:31:14 -08001142 GrColor color = paint.getColor();
joshualitt44701df2015-02-23 14:44:57 -08001143 if (!fOvalRenderer->drawOval(target,
1144 &pipelineBuilder,
1145 color,
1146 viewMatrix,
1147 paint.isAntiAlias(),
1148 oval,
kkinnunend156d362015-05-18 22:23:54 -07001149 strokeInfo)) {
bsalomon@google.com93c96602012-04-27 13:05:21 +00001150 SkPath path;
kkinnunen806236e2015-05-21 22:45:56 -07001151 path.setIsVolatile(true);
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001152 path.addOval(oval);
egdaniel8dd688b2015-01-22 10:16:09 -08001153 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(),
1154 path, strokeInfo);
skia.committer@gmail.com98ded842013-01-23 07:06:17 +00001155 }
bsalomon@google.com150d2842012-01-12 20:19:56 +00001156}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001157
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001158// Can 'path' be drawn as a pair of filled nested rectangles?
1159static bool is_nested_rects(GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -08001160 GrPipelineBuilder* pipelineBuilder,
joshualitt2e3b3e32014-12-09 13:31:14 -08001161 GrColor color,
joshualitt8059eb92014-12-29 15:10:07 -08001162 const SkMatrix& viewMatrix,
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001163 const SkPath& path,
1164 const SkStrokeRec& stroke,
bsalomon9c0822a2014-08-11 11:07:48 -07001165 SkRect rects[2]) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001166 SkASSERT(stroke.isFillStyle());
1167
1168 if (path.isInverseFillType()) {
1169 return false;
1170 }
1171
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001172 // TODO: this restriction could be lifted if we were willing to apply
1173 // the matrix to all the points individually rather than just to the rect
joshualitt8059eb92014-12-29 15:10:07 -08001174 if (!viewMatrix.preservesAxisAlignment()) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001175 return false;
1176 }
1177
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001178 SkPath::Direction dirs[2];
caryclark95bc5f32015-04-08 08:34:15 -07001179 if (!path.isNestedFillRects(rects, dirs)) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001180 return false;
1181 }
1182
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001183 if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001184 // The two rects need to be wound opposite to each other
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001185 return false;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001186 }
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001187
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001188 // Right now, nested rects where the margin is not the same width
1189 // all around do not render correctly
1190 const SkScalar* outer = rects[0].asScalars();
1191 const SkScalar* inner = rects[1].asScalars();
1192
robertphillips183e9852014-10-21 11:25:37 -07001193 bool allEq = true;
1194
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001195 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
robertphillips183e9852014-10-21 11:25:37 -07001196 bool allGoE1 = margin >= SK_Scalar1;
1197
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001198 for (int i = 1; i < 4; ++i) {
1199 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
robertphillips183e9852014-10-21 11:25:37 -07001200 if (temp < SK_Scalar1) {
1201 allGoE1 = false;
1202 }
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001203 if (!SkScalarNearlyEqual(margin, temp)) {
robertphillips183e9852014-10-21 11:25:37 -07001204 allEq = false;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001205 }
1206 }
1207
robertphillips183e9852014-10-21 11:25:37 -07001208 return allEq || allGoE1;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001209}
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001210
joshualitt25d9c152015-02-18 12:29:52 -08001211void GrContext::drawPath(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -08001212 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001213 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001214 const SkMatrix& viewMatrix,
1215 const SkPath& path,
1216 const GrStrokeInfo& strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001217 RETURN_IF_ABANDONED
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001218 if (path.isEmpty()) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001219 if (path.isInverseFillType()) {
joshualitt570d2f82015-02-25 13:19:48 -08001220 this->drawPaint(rt, clip, paint, viewMatrix);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001221 }
1222 return;
1223 }
1224
joshualitt2e3b3e32014-12-09 13:31:14 -08001225 GrColor color = paint.getColor();
egdanield58a0ba2014-06-11 10:30:05 -07001226
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001227 // Note that internalDrawPath may sw-rasterize the path into a scratch texture.
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001228 // Scratch textures can be recycled after they are returned to the texture
1229 // cache. This presents a potential hazard for buffered drawing. However,
1230 // the writePixels that uploads to the scratch will perform a flush so we're
1231 // OK.
bsalomonf21dab92014-11-13 13:33:28 -08001232 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001233 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -08001234 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001235 if (NULL == target) {
1236 return;
1237 }
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001238
egdaniel93a37bc2014-07-21 13:47:57 -07001239 GR_CREATE_TRACE_MARKER1("GrContext::drawPath", target, "Is Convex", path.isConvex());
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001240
kkinnunen18996512015-04-26 23:18:49 -07001241 if (!strokeInfo.isDashed()) {
kkinnunen18996512015-04-26 23:18:49 -07001242 bool useCoverageAA = paint.isAntiAlias() &&
1243 !pipelineBuilder.getRenderTarget()->isMultisampled();
egdanield58a0ba2014-06-11 10:30:05 -07001244
kkinnunend156d362015-05-18 22:23:54 -07001245 if (useCoverageAA && strokeInfo.getWidth() < 0 && !path.isConvex()) {
kkinnunen18996512015-04-26 23:18:49 -07001246 // Concave AA paths are expensive - try to avoid them for special cases
1247 SkRect rects[2];
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001248
kkinnunend156d362015-05-18 22:23:54 -07001249 if (is_nested_rects(target, &pipelineBuilder, color, viewMatrix, path, strokeInfo,
kkinnunen18996512015-04-26 23:18:49 -07001250 rects)) {
1251 fAARectRenderer->fillAANestedRects(target, &pipelineBuilder, color, viewMatrix,
1252 rects);
1253 return;
1254 }
1255 }
1256 SkRect ovalRect;
1257 bool isOval = path.isOval(&ovalRect);
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001258
kkinnunen18996512015-04-26 23:18:49 -07001259 if (isOval && !path.isInverseFillType()) {
1260 if (fOvalRenderer->drawOval(target,
1261 &pipelineBuilder,
1262 color,
1263 viewMatrix,
1264 paint.isAntiAlias(),
1265 ovalRect,
kkinnunend156d362015-05-18 22:23:54 -07001266 strokeInfo)) {
kkinnunen18996512015-04-26 23:18:49 -07001267 return;
1268 }
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001269 }
1270 }
kkinnunen18996512015-04-26 23:18:49 -07001271 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(),
1272 path, strokeInfo);
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001273}
1274
joshualitt9853cce2014-11-17 14:22:48 -08001275void GrContext::internalDrawPath(GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -08001276 GrPipelineBuilder* pipelineBuilder,
joshualitt5531d512014-12-17 15:50:11 -08001277 const SkMatrix& viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -08001278 GrColor color,
joshualitt9853cce2014-11-17 14:22:48 -08001279 bool useAA,
1280 const SkPath& path,
egdanield58a0ba2014-06-11 10:30:05 -07001281 const GrStrokeInfo& strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001282 RETURN_IF_ABANDONED
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001283 SkASSERT(!path.isEmpty());
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001284
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001285 GR_CREATE_TRACE_MARKER("GrContext::internalDrawPath", target);
1286
1287
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001288 // An Assumption here is that path renderer would use some form of tweaking
1289 // the src color (either the input alpha or in the frag shader) to implement
1290 // aa. If we have some future driver-mojo path AA that can do the right
1291 // thing WRT to the blend then we'll need some query on the PR.
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001292 bool useCoverageAA = useAA &&
egdaniel0bdeec92015-02-23 12:12:54 -08001293 !pipelineBuilder->getRenderTarget()->isMultisampled();
bsalomon@google.com289533a2011-10-27 12:34:25 +00001294
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001295
1296 GrPathRendererChain::DrawType type =
1297 useCoverageAA ? GrPathRendererChain::kColorAntiAlias_DrawType :
joshualitt9853cce2014-11-17 14:22:48 -08001298 GrPathRendererChain::kColor_DrawType;
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001299
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001300 const SkPath* pathPtr = &path;
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001301 SkTLazy<SkPath> tmpPath;
kkinnunen18996512015-04-26 23:18:49 -07001302 const GrStrokeInfo* strokeInfoPtr = &strokeInfo;
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001303
1304 // Try a 1st time without stroking the path and without allowing the SW renderer
egdaniel8dd688b2015-01-22 10:16:09 -08001305 GrPathRenderer* pr = this->getPathRenderer(target, pipelineBuilder, viewMatrix, *pathPtr,
kkinnunen18996512015-04-26 23:18:49 -07001306 *strokeInfoPtr, false, type);
1307
1308 GrStrokeInfo dashlessStrokeInfo(strokeInfo, false);
1309 if (NULL == pr && strokeInfo.isDashed()) {
1310 // It didn't work above, so try again with dashed stroke converted to a dashless stroke.
kkinnunend156d362015-05-18 22:23:54 -07001311 if (!strokeInfo.applyDashToPath(tmpPath.init(), &dashlessStrokeInfo, *pathPtr)) {
bsalomon07ab9f32015-05-04 11:41:57 -07001312 return;
kkinnunen18996512015-04-26 23:18:49 -07001313 }
bsalomon07ab9f32015-05-04 11:41:57 -07001314 pathPtr = tmpPath.get();
1315 if (pathPtr->isEmpty()) {
1316 return;
1317 }
1318 strokeInfoPtr = &dashlessStrokeInfo;
kkinnunen18996512015-04-26 23:18:49 -07001319 pr = this->getPathRenderer(target, pipelineBuilder, viewMatrix, *pathPtr, *strokeInfoPtr,
1320 false, type);
1321 }
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001322
robertphillips@google.come79f3202014-02-11 16:30:21 +00001323 if (NULL == pr) {
bsalomon07ab9f32015-05-04 11:41:57 -07001324 if (!GrPathRenderer::IsStrokeHairlineOrEquivalent(*strokeInfoPtr, viewMatrix, NULL) &&
1325 !strokeInfoPtr->isFillStyle()) {
kkinnunen18996512015-04-26 23:18:49 -07001326 // It didn't work above, so try again with stroke converted to a fill.
1327 if (!tmpPath.isValid()) {
1328 tmpPath.init();
1329 }
kkinnunend156d362015-05-18 22:23:54 -07001330 dashlessStrokeInfo.setResScale(SkScalarAbs(viewMatrix.getMaxScale()));
1331 if (!dashlessStrokeInfo.applyToPath(tmpPath.get(), *pathPtr)) {
bsalomon07ab9f32015-05-04 11:41:57 -07001332 return;
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001333 }
bsalomon07ab9f32015-05-04 11:41:57 -07001334 pathPtr = tmpPath.get();
1335 if (pathPtr->isEmpty()) {
1336 return;
1337 }
kkinnunend156d362015-05-18 22:23:54 -07001338 dashlessStrokeInfo.setFillStyle();
bsalomon07ab9f32015-05-04 11:41:57 -07001339 strokeInfoPtr = &dashlessStrokeInfo;
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001340 }
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001341
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001342 // This time, allow SW renderer
kkinnunen18996512015-04-26 23:18:49 -07001343 pr = this->getPathRenderer(target, pipelineBuilder, viewMatrix, *pathPtr, *strokeInfoPtr,
1344 true, type);
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001345 }
1346
robertphillips@google.come79f3202014-02-11 16:30:21 +00001347 if (NULL == pr) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001348#ifdef SK_DEBUG
tfarina38406c82014-10-31 07:11:12 -07001349 SkDebugf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001350#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001351 return;
1352 }
1353
kkinnunen18996512015-04-26 23:18:49 -07001354 pr->drawPath(target, pipelineBuilder, color, viewMatrix, *pathPtr, *strokeInfoPtr, useCoverageAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001355}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001356
bsalomon@google.com27847de2011-02-22 20:59:41 +00001357////////////////////////////////////////////////////////////////////////////////
1358
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001359void GrContext::flush(int flagsBitfield) {
robertphillips@google.come7db8d62013-07-04 11:48:52 +00001360 if (NULL == fDrawBuffer) {
1361 return;
1362 }
1363
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001364 if (kDiscard_FlushBit & flagsBitfield) {
1365 fDrawBuffer->reset();
1366 } else {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001367 fDrawBuffer->flush();
junov@google.com53a55842011-06-08 22:55:10 +00001368 }
bsalomon3f324322015-04-08 11:01:54 -07001369 fResourceCache->notifyFlushOccurred();
bsalomonf21dab92014-11-13 13:33:28 -08001370 fFlushToReduceCacheSize = false;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001371}
1372
bsalomon81beccc2014-10-13 12:32:55 -07001373bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
1374 const void* inPixels, size_t outRowBytes, void* outPixels) {
1375 SkSrcPixelInfo srcPI;
jvanverthfa1e8a72014-12-22 08:31:49 -08001376 if (!GrPixelConfig2ColorAndProfileType(srcConfig, &srcPI.fColorType, NULL)) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001377 return false;
1378 }
bsalomon81beccc2014-10-13 12:32:55 -07001379 srcPI.fAlphaType = kUnpremul_SkAlphaType;
1380 srcPI.fPixels = inPixels;
1381 srcPI.fRowBytes = inRowBytes;
1382
1383 SkDstPixelInfo dstPI;
1384 dstPI.fColorType = srcPI.fColorType;
1385 dstPI.fAlphaType = kPremul_SkAlphaType;
1386 dstPI.fPixels = outPixels;
1387 dstPI.fRowBytes = outRowBytes;
1388
1389 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001390}
1391
bsalomon81beccc2014-10-13 12:32:55 -07001392bool GrContext::writeSurfacePixels(GrSurface* surface,
1393 int left, int top, int width, int height,
1394 GrPixelConfig srcConfig, const void* buffer, size_t rowBytes,
1395 uint32_t pixelOpsFlags) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001396 RETURN_FALSE_IF_ABANDONED
bsalomon81beccc2014-10-13 12:32:55 -07001397 {
1398 GrTexture* texture = NULL;
1399 if (!(kUnpremul_PixelOpsFlag & pixelOpsFlags) && (texture = surface->asTexture()) &&
1400 fGpu->canWriteTexturePixels(texture, srcConfig)) {
1401
1402 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) &&
1403 surface->surfacePriv().hasPendingIO()) {
1404 this->flush();
1405 }
1406 return fGpu->writeTexturePixels(texture, left, top, width, height,
1407 srcConfig, buffer, rowBytes);
1408 // Don't need to check kFlushWrites_PixelOp here, we just did a direct write so the
1409 // upload is already flushed.
1410 }
1411 }
1412
1413 // If we didn't do a direct texture write then we upload the pixels to a texture and draw.
1414 GrRenderTarget* renderTarget = surface->asRenderTarget();
1415 if (NULL == renderTarget) {
1416 return false;
1417 }
1418
1419 // We ignore the preferred config unless it is a R/B swap of the src config. In that case
1420 // we will upload the original src data to a scratch texture but we will spoof it as the swapped
1421 // config. This scratch will then have R and B swapped. We correct for this by swapping again
1422 // when drawing the scratch to the dst using a conversion effect.
1423 bool swapRAndB = false;
1424 GrPixelConfig writeConfig = srcConfig;
1425 if (GrPixelConfigSwapRAndB(srcConfig) ==
1426 fGpu->preferredWritePixelsConfig(srcConfig, renderTarget->config())) {
1427 writeConfig = GrPixelConfigSwapRAndB(srcConfig);
1428 swapRAndB = true;
1429 }
1430
bsalomonf2703d82014-10-28 14:33:06 -07001431 GrSurfaceDesc desc;
bsalomon81beccc2014-10-13 12:32:55 -07001432 desc.fWidth = width;
1433 desc.fHeight = height;
1434 desc.fConfig = writeConfig;
bsalomond309e7a2015-04-30 14:18:54 -07001435 SkAutoTUnref<GrTexture> texture(this->textureProvider()->refScratchTexture(desc,
1436 GrTextureProvider::kApprox_ScratchTexMatch));
bsalomone3059732014-10-14 11:47:22 -07001437 if (!texture) {
bsalomon81beccc2014-10-13 12:32:55 -07001438 return false;
1439 }
1440
1441 SkAutoTUnref<const GrFragmentProcessor> fp;
1442 SkMatrix textureMatrix;
1443 textureMatrix.setIDiv(texture->width(), texture->height());
1444
1445 // allocate a tmp buffer and sw convert the pixels to premul
1446 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
1447
1448 if (kUnpremul_PixelOpsFlag & pixelOpsFlags) {
1449 if (!GrPixelConfigIs8888(srcConfig)) {
1450 return false;
1451 }
1452 fp.reset(this->createUPMToPMEffect(texture, swapRAndB, textureMatrix));
1453 // handle the unpremul step on the CPU if we couldn't create an effect to do it.
1454 if (NULL == fp) {
1455 size_t tmpRowBytes = 4 * width;
1456 tmpPixels.reset(width * height);
1457 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
1458 tmpPixels.get())) {
1459 return false;
1460 }
1461 rowBytes = tmpRowBytes;
1462 buffer = tmpPixels.get();
1463 }
1464 }
1465 if (NULL == fp) {
1466 fp.reset(GrConfigConversionEffect::Create(texture,
1467 swapRAndB,
1468 GrConfigConversionEffect::kNone_PMConversion,
1469 textureMatrix));
1470 }
1471
1472 // Even if the client told us not to flush, we still flush here. The client may have known that
1473 // writes to the original surface caused no data hazards, but they can't know that the scratch
1474 // we just got is safe.
1475 if (texture->surfacePriv().hasPendingIO()) {
1476 this->flush();
1477 }
1478 if (!fGpu->writeTexturePixels(texture, 0, 0, width, height,
1479 writeConfig, buffer, rowBytes)) {
1480 return false;
1481 }
1482
1483 SkMatrix matrix;
1484 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
1485
1486 // This function can be called in the midst of drawing another object (e.g., when uploading a
1487 // SW-rasterized clip while issuing a draw). So we push the current geometry state before
1488 // drawing a rect to the render target.
1489 // The bracket ensures we pop the stack if we wind up flushing below.
1490 {
joshualitt570d2f82015-02-25 13:19:48 -08001491 GrDrawTarget* drawTarget = this->prepareToDraw();
1492 if (!drawTarget) {
1493 return false;
1494 }
joshualitt9853cce2014-11-17 14:22:48 -08001495
egdaniel8dd688b2015-01-22 10:16:09 -08001496 GrPipelineBuilder pipelineBuilder;
1497 pipelineBuilder.addColorProcessor(fp);
1498 pipelineBuilder.setRenderTarget(renderTarget);
joshualitt44701df2015-02-23 14:44:57 -08001499 drawTarget->drawSimpleRect(&pipelineBuilder,
1500 GrColor_WHITE,
1501 matrix,
joshualitt8059eb92014-12-29 15:10:07 -08001502 SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)));
bsalomon81beccc2014-10-13 12:32:55 -07001503 }
1504
1505 if (kFlushWrites_PixelOp & pixelOpsFlags) {
1506 this->flushSurfaceWrites(surface);
1507 }
1508
1509 return true;
1510}
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001511
reed@google.com7111d462014-03-25 16:20:24 +00001512// toggles between RGBA and BGRA
1513static SkColorType toggle_colortype32(SkColorType ct) {
1514 if (kRGBA_8888_SkColorType == ct) {
1515 return kBGRA_8888_SkColorType;
1516 } else {
1517 SkASSERT(kBGRA_8888_SkColorType == ct);
1518 return kRGBA_8888_SkColorType;
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001519 }
1520}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001521
bsalomon@google.com0342a852012-08-20 19:22:38 +00001522bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
1523 int left, int top, int width, int height,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001524 GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001525 uint32_t flags) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001526 RETURN_FALSE_IF_ABANDONED
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001527 ASSERT_OWNED_RESOURCE(target);
bsalomon89c62982014-11-03 12:08:42 -08001528 SkASSERT(target);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001529
bsalomonafbf2d62014-09-30 12:18:44 -07001530 if (!(kDontFlush_PixelOpsFlag & flags) && target->surfacePriv().hasPendingWrite()) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001531 this->flush();
1532 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001533
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001534 // Determine which conversions have to be applied: flipY, swapRAnd, and/or unpremul.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001535
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001536 // If fGpu->readPixels would incur a y-flip cost then we will read the pixels upside down. We'll
1537 // either do the flipY by drawing into a scratch with a matrix or on the cpu after the read.
1538 bool flipY = fGpu->readPixelsWillPayForYFlip(target, left, top,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001539 width, height, dstConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001540 rowBytes);
bsalomon@google.com9c680582013-02-06 18:17:50 +00001541 // We ignore the preferred config if it is different than our config unless it is an R/B swap.
1542 // In that case we'll perform an R and B swap while drawing to a scratch texture of the swapped
1543 // config. Then we will call readPixels on the scratch with the swapped config. The swaps during
1544 // the draw cancels out the fact that we call readPixels with a config that is R/B swapped from
1545 // dstConfig.
1546 GrPixelConfig readConfig = dstConfig;
1547 bool swapRAndB = false;
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +00001548 if (GrPixelConfigSwapRAndB(dstConfig) ==
1549 fGpu->preferredReadPixelsConfig(dstConfig, target->config())) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001550 readConfig = GrPixelConfigSwapRAndB(readConfig);
1551 swapRAndB = true;
1552 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001553
bsalomon@google.com0342a852012-08-20 19:22:38 +00001554 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001555
bsalomon@google.com9c680582013-02-06 18:17:50 +00001556 if (unpremul && !GrPixelConfigIs8888(dstConfig)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001557 // The unpremul flag is only allowed for these two configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001558 return false;
1559 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001560
bsalomon191bcc02014-11-14 11:31:13 -08001561 SkAutoTUnref<GrTexture> tempTexture;
1562
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001563 // If the src is a texture and we would have to do conversions after read pixels, we instead
1564 // do the conversions by drawing the src to a scratch texture. If we handle any of the
1565 // conversions in the draw we set the corresponding bool to false so that we don't reapply it
1566 // on the read back pixels.
1567 GrTexture* src = target->asTexture();
bsalomon49f085d2014-09-05 13:34:00 -07001568 if (src && (swapRAndB || unpremul || flipY)) {
bsalomon81beccc2014-10-13 12:32:55 -07001569 // Make the scratch a render so we can read its pixels.
bsalomonf2703d82014-10-28 14:33:06 -07001570 GrSurfaceDesc desc;
1571 desc.fFlags = kRenderTarget_GrSurfaceFlag;
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001572 desc.fWidth = width;
1573 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001574 desc.fConfig = readConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001575 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001576
bsalomon@google.com9c680582013-02-06 18:17:50 +00001577 // 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 +00001578 // match the passed rect. However, if we see many different size rectangles we will trash
1579 // our texture cache and pay the cost of creating and destroying many textures. So, we only
1580 // request an exact match when the caller is reading an entire RT.
bsalomond309e7a2015-04-30 14:18:54 -07001581 GrTextureProvider::ScratchTexMatch match = GrTextureProvider::kApprox_ScratchTexMatch;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001582 if (0 == left &&
1583 0 == top &&
1584 target->width() == width &&
1585 target->height() == height &&
1586 fGpu->fullReadPixelsIsFasterThanPartial()) {
bsalomond309e7a2015-04-30 14:18:54 -07001587 match = GrTextureProvider::kExact_ScratchTexMatch;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001588 }
bsalomond309e7a2015-04-30 14:18:54 -07001589 tempTexture.reset(this->textureProvider()->refScratchTexture(desc, match));
bsalomon191bcc02014-11-14 11:31:13 -08001590 if (tempTexture) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001591 // compute a matrix to perform the draw
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001592 SkMatrix textureMatrix;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001593 textureMatrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001594 textureMatrix.postIDiv(src->width(), src->height());
1595
joshualittb0a8a372014-09-23 09:50:21 -07001596 SkAutoTUnref<const GrFragmentProcessor> fp;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001597 if (unpremul) {
joshualittb0a8a372014-09-23 09:50:21 -07001598 fp.reset(this->createPMToUPMEffect(src, swapRAndB, textureMatrix));
1599 if (fp) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001600 unpremul = false; // we no longer need to do this on CPU after the read back.
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001601 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001602 }
1603 // If we failed to create a PM->UPM effect and have no other conversions to perform then
1604 // there is no longer any point to using the scratch.
joshualittb0a8a372014-09-23 09:50:21 -07001605 if (fp || flipY || swapRAndB) {
1606 if (!fp) {
1607 fp.reset(GrConfigConversionEffect::Create(
1608 src, swapRAndB, GrConfigConversionEffect::kNone_PMConversion,
1609 textureMatrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001610 }
1611 swapRAndB = false; // we will handle the swap in the draw.
bsalomon@google.comc4364992011-11-07 15:54:49 +00001612
robertphillips@google.com13f181f2013-03-02 12:02:08 +00001613 // We protect the existing geometry here since it may not be
1614 // clear to the caller that a draw operation (i.e., drawSimpleRect)
1615 // can be invoked in this method
joshualitt5c55fef2014-10-31 14:04:35 -07001616 {
egdaniel8dd688b2015-01-22 10:16:09 -08001617 GrPipelineBuilder pipelineBuilder;
joshualitt5c55fef2014-10-31 14:04:35 -07001618 SkASSERT(fp);
egdaniel8dd688b2015-01-22 10:16:09 -08001619 pipelineBuilder.addColorProcessor(fp);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001620
egdaniel8dd688b2015-01-22 10:16:09 -08001621 pipelineBuilder.setRenderTarget(tempTexture->asRenderTarget());
joshualitt5c55fef2014-10-31 14:04:35 -07001622 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
joshualitt44701df2015-02-23 14:44:57 -08001623 fDrawBuffer->drawSimpleRect(&pipelineBuilder,
1624 GrColor_WHITE,
1625 SkMatrix::I(),
egdaniel8dd688b2015-01-22 10:16:09 -08001626 rect);
joshualitt5c55fef2014-10-31 14:04:35 -07001627 // we want to read back from the scratch's origin
1628 left = 0;
1629 top = 0;
bsalomon191bcc02014-11-14 11:31:13 -08001630 target = tempTexture->asRenderTarget();
joshualitt5c55fef2014-10-31 14:04:35 -07001631 }
1632 this->flushSurfaceWrites(target);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001633 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001634 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001635 }
joshualitt5c55fef2014-10-31 14:04:35 -07001636
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001637 if (!fGpu->readPixels(target,
1638 left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001639 readConfig, buffer, rowBytes)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001640 return false;
1641 }
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001642 // Perform any conversions we weren't able to perform using a scratch texture.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001643 if (unpremul || swapRAndB) {
reed@google.com7111d462014-03-25 16:20:24 +00001644 SkDstPixelInfo dstPI;
jvanverthfa1e8a72014-12-22 08:31:49 -08001645 if (!GrPixelConfig2ColorAndProfileType(dstConfig, &dstPI.fColorType, NULL)) {
reed@google.com7111d462014-03-25 16:20:24 +00001646 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001647 }
reed@google.com7111d462014-03-25 16:20:24 +00001648 dstPI.fAlphaType = kUnpremul_SkAlphaType;
1649 dstPI.fPixels = buffer;
1650 dstPI.fRowBytes = rowBytes;
1651
1652 SkSrcPixelInfo srcPI;
1653 srcPI.fColorType = swapRAndB ? toggle_colortype32(dstPI.fColorType) : dstPI.fColorType;
1654 srcPI.fAlphaType = kPremul_SkAlphaType;
1655 srcPI.fPixels = buffer;
1656 srcPI.fRowBytes = rowBytes;
1657
1658 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001659 }
1660 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001661}
1662
bsalomon87a94eb2014-11-03 14:28:32 -08001663void GrContext::prepareSurfaceForExternalRead(GrSurface* surface) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001664 RETURN_IF_ABANDONED
bsalomon87a94eb2014-11-03 14:28:32 -08001665 SkASSERT(surface);
1666 ASSERT_OWNED_RESOURCE(surface);
1667 if (surface->surfacePriv().hasPendingIO()) {
1668 this->flush();
1669 }
1670 GrRenderTarget* rt = surface->asRenderTarget();
1671 if (fGpu && rt) {
1672 fGpu->resolveRenderTarget(rt);
bsalomon41ebbdd2014-08-04 08:31:39 -07001673 }
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001674}
1675
bsalomon41ebbdd2014-08-04 08:31:39 -07001676void GrContext::discardRenderTarget(GrRenderTarget* renderTarget) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001677 RETURN_IF_ABANDONED
bsalomon41ebbdd2014-08-04 08:31:39 -07001678 SkASSERT(renderTarget);
1679 ASSERT_OWNED_RESOURCE(renderTarget);
bsalomonf21dab92014-11-13 13:33:28 -08001680 AutoCheckFlush acf(this);
joshualitt570d2f82015-02-25 13:19:48 -08001681 GrDrawTarget* target = this->prepareToDraw();
bsalomon41ebbdd2014-08-04 08:31:39 -07001682 if (NULL == target) {
1683 return;
1684 }
1685 target->discard(renderTarget);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001686}
1687
bsalomonf80bfed2014-10-07 05:56:02 -07001688void GrContext::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
1689 const SkIPoint& dstPoint, uint32_t pixelOpsFlags) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001690 RETURN_IF_ABANDONED
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001691 if (NULL == src || NULL == dst) {
1692 return;
1693 }
bsalomone3d4bf22014-09-23 09:15:03 -07001694 ASSERT_OWNED_RESOURCE(src);
junov2bb52102014-09-29 10:18:59 -07001695 ASSERT_OWNED_RESOURCE(dst);
Brian Salomon34a98952014-09-24 11:41:24 -04001696
bsalomonf80bfed2014-10-07 05:56:02 -07001697 // Since we're going to the draw target and not GPU, no need to check kNoFlush
1698 // here.
junov96c118e2014-09-26 13:09:47 -07001699
joshualitt570d2f82015-02-25 13:19:48 -08001700 GrDrawTarget* target = this->prepareToDraw();
junov96c118e2014-09-26 13:09:47 -07001701 if (NULL == target) {
1702 return;
1703 }
junov96c118e2014-09-26 13:09:47 -07001704 target->copySurface(dst, src, srcRect, dstPoint);
bsalomonf80bfed2014-10-07 05:56:02 -07001705
1706 if (kFlushWrites_PixelOp & pixelOpsFlags) {
1707 this->flush();
1708 }
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001709}
1710
bsalomonf80bfed2014-10-07 05:56:02 -07001711void GrContext::flushSurfaceWrites(GrSurface* surface) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001712 RETURN_IF_ABANDONED
bsalomonf80bfed2014-10-07 05:56:02 -07001713 if (surface->surfacePriv().hasPendingWrite()) {
1714 this->flush();
1715 }
1716}
1717
egdaniel8dd688b2015-01-22 10:16:09 -08001718GrDrawTarget* GrContext::prepareToDraw(GrPipelineBuilder* pipelineBuilder,
joshualitt25d9c152015-02-18 12:29:52 -08001719 GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -08001720 const GrClip& clip,
joshualitt9853cce2014-11-17 14:22:48 -08001721 const GrPaint* paint,
1722 const AutoCheckFlush* acf) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001723 if (NULL == fGpu || NULL == fDrawBuffer) {
bsalomon41ebbdd2014-08-04 08:31:39 -07001724 return NULL;
1725 }
1726
joshualitt570d2f82015-02-25 13:19:48 -08001727 ASSERT_OWNED_RESOURCE(rt);
1728 SkASSERT(rt && paint && acf);
1729 pipelineBuilder->setFromPaint(*paint, rt, clip);
1730 return fDrawBuffer;
1731}
1732
1733GrDrawTarget* GrContext::prepareToDraw() {
1734 if (NULL == fGpu) {
1735 return NULL;
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001736 }
joshualitt5c55fef2014-10-31 14:04:35 -07001737 return fDrawBuffer;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001738}
1739
robertphillips@google.com72176b22012-05-23 13:19:12 +00001740/*
1741 * This method finds a path renderer that can draw the specified path on
1742 * the provided target.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001743 * Due to its expense, the software path renderer has split out so it can
robertphillips@google.com72176b22012-05-23 13:19:12 +00001744 * can be individually allowed/disallowed via the "allowSW" boolean.
1745 */
joshualitt9853cce2014-11-17 14:22:48 -08001746GrPathRenderer* GrContext::getPathRenderer(const GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -08001747 const GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -08001748 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -08001749 const SkPath& path,
kkinnunen18996512015-04-26 23:18:49 -07001750 const GrStrokeInfo& stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001751 bool allowSW,
1752 GrPathRendererChain::DrawType drawType,
1753 GrPathRendererChain::StencilSupport* stencilSupport) {
1754
bsalomon@google.com30085192011-08-19 15:42:31 +00001755 if (NULL == fPathRendererChain) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001756 fPathRendererChain = SkNEW_ARGS(GrPathRendererChain, (this));
bsalomon@google.com30085192011-08-19 15:42:31 +00001757 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001758
joshualitt9853cce2014-11-17 14:22:48 -08001759 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(target,
egdaniel8dd688b2015-01-22 10:16:09 -08001760 pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -08001761 viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -08001762 path,
sugoi@google.com12b4e272012-12-06 20:13:11 +00001763 stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001764 drawType,
1765 stencilSupport);
robertphillips@google.com72176b22012-05-23 13:19:12 +00001766
1767 if (NULL == pr && allowSW) {
1768 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001769 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001770 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001771 pr = fSoftwarePathRenderer;
1772 }
1773
1774 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001775}
1776
bsalomon@google.com27847de2011-02-22 20:59:41 +00001777////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00001778bool GrContext::isConfigRenderable(GrPixelConfig config, bool withMSAA) const {
1779 return fGpu->caps()->isConfigRenderable(config, withMSAA);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001780}
1781
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +00001782int GrContext::getRecommendedSampleCount(GrPixelConfig config,
1783 SkScalar dpi) const {
1784 if (!this->isConfigRenderable(config, true)) {
1785 return 0;
1786 }
1787 int chosenSampleCount = 0;
jvanverthe9c0fc62015-04-29 11:18:05 -07001788 if (fGpu->caps()->shaderCaps()->pathRenderingSupport()) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +00001789 if (dpi >= 250.0f) {
1790 chosenSampleCount = 4;
1791 } else {
1792 chosenSampleCount = 16;
1793 }
1794 }
1795 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ?
1796 chosenSampleCount : 0;
1797}
1798
bsalomon@google.com21c10c52013-06-13 17:44:07 +00001799GrDrawTarget* GrContext::getTextTarget() {
joshualitt570d2f82015-02-25 13:19:48 -08001800 return this->prepareToDraw();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001801}
1802
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001803namespace {
1804void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
1805 GrConfigConversionEffect::PMConversion pmToUPM;
1806 GrConfigConversionEffect::PMConversion upmToPM;
1807 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
1808 *pmToUPMValue = pmToUPM;
1809 *upmToPMValue = upmToPM;
1810}
1811}
1812
joshualittb0a8a372014-09-23 09:50:21 -07001813const GrFragmentProcessor* GrContext::createPMToUPMEffect(GrTexture* texture,
1814 bool swapRAndB,
1815 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001816 if (!fDidTestPMConversions) {
1817 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001818 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001819 }
1820 GrConfigConversionEffect::PMConversion pmToUPM =
1821 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
1822 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001823 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001824 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001825 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001826 }
1827}
1828
joshualittb0a8a372014-09-23 09:50:21 -07001829const GrFragmentProcessor* GrContext::createUPMToPMEffect(GrTexture* texture,
1830 bool swapRAndB,
1831 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001832 if (!fDidTestPMConversions) {
1833 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001834 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001835 }
1836 GrConfigConversionEffect::PMConversion upmToPM =
1837 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
1838 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001839 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001840 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001841 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001842 }
1843}
1844
bsalomon37f9a262015-02-02 13:00:10 -08001845//////////////////////////////////////////////////////////////////////////////
1846
1847void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
1848 if (maxTextures) {
bsalomon0ea80f42015-02-11 10:49:59 -08001849 *maxTextures = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -08001850 }
1851 if (maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -08001852 *maxTextureBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -08001853 }
1854}
1855
1856void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -08001857 fResourceCache->setLimits(maxTextures, maxTextureBytes);
bsalomon37f9a262015-02-02 13:00:10 -08001858}
1859
bsalomon37f9a262015-02-02 13:00:10 -08001860//////////////////////////////////////////////////////////////////////////////
1861
egdanielbbcb38d2014-06-19 10:19:29 -07001862void GrContext::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
1863 fGpu->addGpuTraceMarker(marker);
bsalomon49f085d2014-09-05 13:34:00 -07001864 if (fDrawBuffer) {
egdanielbbcb38d2014-06-19 10:19:29 -07001865 fDrawBuffer->addGpuTraceMarker(marker);
1866 }
1867}
1868
1869void GrContext::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
1870 fGpu->removeGpuTraceMarker(marker);
bsalomon49f085d2014-09-05 13:34:00 -07001871 if (fDrawBuffer) {
egdanielbbcb38d2014-06-19 10:19:29 -07001872 fDrawBuffer->removeGpuTraceMarker(marker);
1873 }
1874}
joshualitte5902662015-05-11 08:18:35 -07001875
1876///////////////////////////////////////////////////////////////////////////////////////////////////
1877
1878#ifdef GR_TEST_UTILS
1879
joshualitt6c891102015-05-13 08:51:49 -07001880BATCH_TEST_DEFINE(StrokeRectBatch) {
joshualitte5902662015-05-11 08:18:35 -07001881 StrokeRectBatch::Geometry geometry;
1882 geometry.fViewMatrix = GrTest::TestMatrix(random);
1883 geometry.fColor = GrRandomColor(random);
1884 geometry.fRect = GrTest::TestRect(random);
1885 geometry.fStrokeWidth = random->nextBool() ? 0.0f : 1.0f;
1886
joshualitt4c977862015-05-13 08:00:56 -07001887 return StrokeRectBatch::Create(geometry, random->nextBool());
joshualitte5902662015-05-11 08:18:35 -07001888}
1889
1890static uint32_t seed_vertices(GrPrimitiveType type) {
1891 switch (type) {
1892 case kTriangles_GrPrimitiveType:
1893 case kTriangleStrip_GrPrimitiveType:
1894 case kTriangleFan_GrPrimitiveType:
1895 return 3;
1896 case kPoints_GrPrimitiveType:
1897 return 1;
1898 case kLines_GrPrimitiveType:
1899 case kLineStrip_GrPrimitiveType:
1900 return 2;
1901 }
1902 SkFAIL("Incomplete switch\n");
1903 return 0;
1904}
1905
1906static uint32_t primitive_vertices(GrPrimitiveType type) {
1907 switch (type) {
1908 case kTriangles_GrPrimitiveType:
1909 return 3;
1910 case kLines_GrPrimitiveType:
1911 return 2;
1912 case kTriangleStrip_GrPrimitiveType:
1913 case kTriangleFan_GrPrimitiveType:
1914 case kPoints_GrPrimitiveType:
1915 case kLineStrip_GrPrimitiveType:
1916 return 1;
1917 }
1918 SkFAIL("Incomplete switch\n");
1919 return 0;
1920}
1921
1922static SkPoint random_point(SkRandom* random, SkScalar min, SkScalar max) {
1923 SkPoint p;
1924 p.fX = random->nextRangeScalar(min, max);
1925 p.fY = random->nextRangeScalar(min, max);
1926 return p;
1927}
1928
1929static void randomize_params(size_t count, size_t maxVertex, SkScalar min, SkScalar max,
1930 SkRandom* random,
1931 SkTArray<SkPoint>* positions,
1932 SkTArray<SkPoint>* texCoords, bool hasTexCoords,
1933 SkTArray<GrColor>* colors, bool hasColors,
1934 SkTArray<uint16_t>* indices, bool hasIndices) {
1935 for (uint32_t v = 0; v < count; v++) {
1936 positions->push_back(random_point(random, min, max));
1937 if (hasTexCoords) {
1938 texCoords->push_back(random_point(random, min, max));
1939 }
1940 if (hasColors) {
1941 colors->push_back(GrRandomColor(random));
1942 }
1943 if (hasIndices) {
1944 SkASSERT(maxVertex <= SK_MaxU16);
1945 indices->push_back(random->nextULessThan((uint16_t)maxVertex));
1946 }
1947 }
1948}
1949
joshualitt6c891102015-05-13 08:51:49 -07001950BATCH_TEST_DEFINE(VerticesBatch) {
joshualitte5902662015-05-11 08:18:35 -07001951 GrPrimitiveType type = GrPrimitiveType(random->nextULessThan(kLast_GrPrimitiveType + 1));
1952 uint32_t primitiveCount = random->nextRangeU(1, 100);
1953
1954 // TODO make 'sensible' indexbuffers
1955 SkTArray<SkPoint> positions;
1956 SkTArray<SkPoint> texCoords;
1957 SkTArray<GrColor> colors;
1958 SkTArray<uint16_t> indices;
1959
1960 bool hasTexCoords = random->nextBool();
1961 bool hasIndices = random->nextBool();
1962 bool hasColors = random->nextBool();
1963
1964 uint32_t vertexCount = seed_vertices(type) + (primitiveCount - 1) * primitive_vertices(type);
1965
1966 static const SkScalar kMinVertExtent = -100.f;
1967 static const SkScalar kMaxVertExtent = 100.f;
1968 randomize_params(seed_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent,
1969 random,
1970 &positions,
1971 &texCoords, hasTexCoords,
1972 &colors, hasColors,
1973 &indices, hasIndices);
1974
1975 for (uint32_t i = 1; i < primitiveCount; i++) {
1976 randomize_params(primitive_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent,
1977 random,
1978 &positions,
1979 &texCoords, hasTexCoords,
1980 &colors, hasColors,
1981 &indices, hasIndices);
1982 }
1983
1984 SkMatrix viewMatrix = GrTest::TestMatrix(random);
1985 SkRect bounds;
1986 SkDEBUGCODE(bool result = ) bounds.setBoundsCheck(positions.begin(), vertexCount);
1987 SkASSERT(result);
1988
1989 viewMatrix.mapRect(&bounds);
1990
1991 DrawVerticesBatch::Geometry geometry;
1992 geometry.fColor = GrRandomColor(random);
1993 return DrawVerticesBatch::Create(geometry, type, viewMatrix,
1994 positions.begin(), vertexCount,
1995 indices.begin(), hasIndices ? vertexCount : 0,
1996 colors.begin(),
1997 texCoords.begin(),
1998 bounds);
1999}
2000
2001#endif