blob: 342fb2175e9f4055d33536553486b4c4f33297d8 [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"
joshualitt5478d422014-11-14 16:00:38 -080017#include "GrDefaultGeoProcFactory.h"
bsalomon453cf402014-11-11 14:15:57 -080018#include "GrGpuResource.h"
bsalomon3582d3e2015-02-13 14:20:05 -080019#include "GrGpuResourcePriv.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000020#include "GrDrawTargetCaps.h"
joshualitt5478d422014-11-14 16:00:38 -080021#include "GrGpu.h"
joshualittbb87b212015-05-19 14:28:04 -070022#include "GrImmediateDrawTarget.h"
commit-bot@chromium.orgdcb8ef92014-03-27 11:26:10 +000023#include "GrIndexBuffer.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000024#include "GrInOrderDrawBuffer.h"
robertphillips@google.come930a072014-04-03 00:34:27 +000025#include "GrLayerCache.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +000026#include "GrOvalRenderer.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000027#include "GrPathRenderer.h"
tomhudson@google.comd22b6e42011-06-24 15:53:40 +000028#include "GrPathUtils.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080029#include "GrRenderTargetPriv.h"
bsalomon0ea80f42015-02-11 10:49:59 -080030#include "GrResourceCache.h"
bsalomond309e7a2015-04-30 14:18:54 -070031#include "GrResourceProvider.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000032#include "GrSoftwarePathRenderer.h"
kkinnunenc6cb56f2014-06-24 00:12:27 -070033#include "GrStencilAndCoverTextContext.h"
egdanield58a0ba2014-06-11 10:30:05 -070034#include "GrStrokeInfo.h"
bsalomonafbf2d62014-09-30 12:18:44 -070035#include "GrSurfacePriv.h"
joshualittb7133be2015-04-08 09:08:31 -070036#include "GrTextBlobCache.h"
bsalomonafbf2d62014-09-30 12:18:44 -070037#include "GrTexturePriv.h"
egdanielbbcb38d2014-06-19 10:19:29 -070038#include "GrTraceMarker.h"
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +000039#include "GrTracing.h"
bsalomoncb8979d2015-05-05 09:51:38 -070040#include "GrVertices.h"
egdanield58a0ba2014-06-11 10:30:05 -070041#include "SkDashPathPriv.h"
bsalomon81beccc2014-10-13 12:32:55 -070042#include "SkConfig8888.h"
reed@google.com7111d462014-03-25 16:20:24 +000043#include "SkGr.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000044#include "SkRRect.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000045#include "SkStrokeRec.h"
commit-bot@chromium.orgdcb8ef92014-03-27 11:26:10 +000046#include "SkTLazy.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000047#include "SkTLS.h"
commit-bot@chromium.org933e65d2014-03-20 20:00:24 +000048#include "SkTraceEvent.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000049
joshualitt5478d422014-11-14 16:00:38 -080050#include "effects/GrConfigConversionEffect.h"
51#include "effects/GrDashingEffect.h"
52#include "effects/GrSingleTextureEffect.h"
53
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000054#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
joshualitt5f5a8d72015-02-25 14:09:45 -080055#define RETURN_IF_ABANDONED if (!fDrawBuffer) { return; }
56#define RETURN_FALSE_IF_ABANDONED if (!fDrawBuffer) { return false; }
57#define RETURN_NULL_IF_ABANDONED if (!fDrawBuffer) { return NULL; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000058
bsalomonf21dab92014-11-13 13:33:28 -080059class GrContext::AutoCheckFlush {
60public:
61 AutoCheckFlush(GrContext* context) : fContext(context) { SkASSERT(context); }
62
63 ~AutoCheckFlush() {
64 if (fContext->fFlushToReduceCacheSize) {
65 fContext->flush();
66 }
67 }
68
69private:
70 GrContext* fContext;
71};
72
krajcevski9c6d4d72014-08-12 07:26:25 -070073GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
74 const Options* opts) {
75 GrContext* context;
76 if (NULL == opts) {
77 context = SkNEW_ARGS(GrContext, (Options()));
78 } else {
79 context = SkNEW_ARGS(GrContext, (*opts));
80 }
81
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000082 if (context->init(backend, backendContext)) {
83 return context;
84 } else {
85 context->unref();
86 return NULL;
bsalomon@google.com27847de2011-02-22 20:59:41 +000087 }
bsalomon@google.com27847de2011-02-22 20:59:41 +000088}
89
joshualitt0acd0d32015-05-07 08:23:19 -070090static int32_t gNextID = 1;
91static int32_t next_id() {
92 int32_t id;
93 do {
94 id = sk_atomic_inc(&gNextID);
95 } while (id == SK_InvalidGenID);
96 return id;
97}
98
99GrContext::GrContext(const Options& opts) : fOptions(opts), fUniqueID(next_id()) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000100 fGpu = NULL;
bsalomond309e7a2015-04-30 14:18:54 -0700101 fResourceCache = NULL;
102 fResourceProvider = NULL;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000103 fPathRendererChain = NULL;
104 fSoftwarePathRenderer = NULL;
joshualitt7c3a2f82015-03-31 13:32:05 -0700105 fBatchFontCache = NULL;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000106 fDrawBuffer = NULL;
bsalomonf21dab92014-11-13 13:33:28 -0800107 fFlushToReduceCacheSize = false;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000108 fAARectRenderer = NULL;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000109 fOvalRenderer = NULL;
robertphillips@google.com44a91dc2013-07-25 15:32:06 +0000110 fMaxTextureSizeOverride = 1 << 20;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000111}
112
113bool GrContext::init(GrBackend backend, GrBackendContext backendContext) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000114 SkASSERT(NULL == fGpu);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000115
116 fGpu = GrGpu::Create(backend, backendContext, this);
117 if (NULL == fGpu) {
118 return false;
119 }
bsalomon33435572014-11-05 14:47:41 -0800120 this->initCommon();
121 return true;
122}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000123
bsalomon33435572014-11-05 14:47:41 -0800124void GrContext::initCommon() {
bsalomon0ea80f42015-02-11 10:49:59 -0800125 fResourceCache = SkNEW(GrResourceCache);
126 fResourceCache->setOverBudgetCallback(OverBudgetCB, this);
bsalomond309e7a2015-04-30 14:18:54 -0700127 fResourceProvider = SkNEW_ARGS(GrResourceProvider, (fGpu, fResourceCache));
commit-bot@chromium.org1836d332013-07-16 22:55:03 +0000128
robertphillips4ec84da2014-06-24 13:10:43 -0700129 fLayerCache.reset(SkNEW_ARGS(GrLayerCache, (this)));
robertphillips@google.come930a072014-04-03 00:34:27 +0000130
bsalomoned0bcad2015-05-04 10:36:42 -0700131 fAARectRenderer = SkNEW(GrAARectRenderer);
132 fOvalRenderer = SkNEW(GrOvalRenderer);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000133
134 fDidTestPMConversions = false;
135
joshualittbb87b212015-05-19 14:28:04 -0700136#ifdef IMMEDIATE_MODE
137 fDrawBuffer = SkNEW_ARGS(GrImmediateDrawTarget, (this));
138#else
robertphillipse40d3972015-05-07 09:51:43 -0700139 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (this));
joshualittbb87b212015-05-19 14:28:04 -0700140#endif
joshualitt7c3a2f82015-03-31 13:32:05 -0700141
142 // GrBatchFontCache will eventually replace GrFontCache
joshualitt62db8ba2015-04-09 08:22:37 -0700143 fBatchFontCache = SkNEW_ARGS(GrBatchFontCache, (this));
joshualittb7133be2015-04-08 09:08:31 -0700144
joshualitt0db6dfa2015-04-10 07:01:30 -0700145 fTextBlobCache.reset(SkNEW_ARGS(GrTextBlobCache, (TextBlobCacheOverBudgetCB, this)));
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000146}
147
bsalomon@google.com27847de2011-02-22 20:59:41 +0000148GrContext::~GrContext() {
bsalomon@google.com733c0622013-04-24 17:59:32 +0000149 if (NULL == fGpu) {
150 return;
151 }
152
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000153 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000154
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000155 for (int i = 0; i < fCleanUpData.count(); ++i) {
156 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
157 }
158
bsalomond309e7a2015-04-30 14:18:54 -0700159 SkDELETE(fResourceProvider);
bsalomon0ea80f42015-02-11 10:49:59 -0800160 SkDELETE(fResourceCache);
joshualitt7c3a2f82015-03-31 13:32:05 -0700161 SkDELETE(fBatchFontCache);
bsalomon33435572014-11-05 14:47:41 -0800162 SkDELETE(fDrawBuffer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000163
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000164 fAARectRenderer->unref();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000165 fOvalRenderer->unref();
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000166
bsalomon@google.com205d4602011-04-25 12:43:45 +0000167 fGpu->unref();
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000168 SkSafeUnref(fPathRendererChain);
169 SkSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000170}
171
bsalomon2354f842014-07-28 13:48:36 -0700172void GrContext::abandonContext() {
bsalomond309e7a2015-04-30 14:18:54 -0700173 fResourceProvider->abandon();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000174 // abandon first to so destructors
175 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800176 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700177
robertphillipse3371302014-09-17 06:01:06 -0700178 fGpu->contextAbandoned();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000179
bsalomon@google.com30085192011-08-19 15:42:31 +0000180 // a path renderer may be holding onto resources that
181 // are now unusable
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000182 SkSafeSetNull(fPathRendererChain);
183 SkSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000184
robertphillipse40d3972015-05-07 09:51:43 -0700185 SkDELETE(fDrawBuffer);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000186 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000187
joshualitt7c3a2f82015-03-31 13:32:05 -0700188 fBatchFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000189 fLayerCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700190 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000191}
192
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000193void GrContext::resetContext(uint32_t state) {
194 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000195}
196
197void GrContext::freeGpuResources() {
198 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000199
bsalomon49f085d2014-09-05 13:34:00 -0700200 if (fDrawBuffer) {
bsalomonc8dc1f72014-08-21 13:02:13 -0700201 fDrawBuffer->purgeResources();
202 }
robertphillips@google.comff175842012-05-14 19:31:39 +0000203
joshualitt7c3a2f82015-03-31 13:32:05 -0700204 fBatchFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000205 fLayerCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000206 // a path renderer may be holding onto resources
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000207 SkSafeSetNull(fPathRendererChain);
208 SkSafeSetNull(fSoftwarePathRenderer);
bsalomon3033b9f2015-04-13 11:09:56 -0700209
210 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000211}
212
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000213void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
bsalomon71cb0c22014-11-14 12:10:14 -0800214 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800215 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800216 }
217 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800218 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800219 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000220}
221
kkinnunenc6cb56f2014-06-24 00:12:27 -0700222GrTextContext* GrContext::createTextContext(GrRenderTarget* renderTarget,
joshualitt6e8cd962015-03-20 10:30:14 -0700223 SkGpuDevice* gpuDevice,
kkinnunenc6cb56f2014-06-24 00:12:27 -0700224 const SkDeviceProperties&
225 leakyProperties,
226 bool enableDistanceFieldFonts) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700227 if (fGpu->caps()->shaderCaps()->pathRenderingSupport() && renderTarget->isMultisampled()) {
egdaniel8dc7c3a2015-04-16 11:22:42 -0700228 GrStencilAttachment* sb = renderTarget->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800229 if (sb) {
joshualitt6e8cd962015-03-20 10:30:14 -0700230 return GrStencilAndCoverTextContext::Create(this, gpuDevice, leakyProperties);
bsalomon6bc1b5f2015-02-23 09:06:38 -0800231 }
jvanverth8c27a182014-10-14 08:45:50 -0700232 }
233
joshualitt9bd2daf2015-04-17 09:30:06 -0700234 return GrAtlasTextContext::Create(this, gpuDevice, leakyProperties, enableDistanceFieldFonts);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700235}
236
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000237////////////////////////////////////////////////////////////////////////////////
238
bsalomond2a6f4e2015-02-04 10:55:54 -0800239bool GrContext::isConfigTexturable(GrPixelConfig config) const {
240 return fGpu->caps()->isConfigTexturable(config);
241}
242
243bool GrContext::npotTextureTileSupport() const {
244 return fGpu->caps()->npotTextureTileSupport();
245}
246
bsalomon71cb0c22014-11-14 12:10:14 -0800247void GrContext::OverBudgetCB(void* data) {
bsalomon66a450f2014-11-13 13:19:10 -0800248 SkASSERT(data);
bsalomonf21dab92014-11-13 13:33:28 -0800249
bsalomon66a450f2014-11-13 13:19:10 -0800250 GrContext* context = reinterpret_cast<GrContext*>(data);
bsalomonf21dab92014-11-13 13:33:28 -0800251
252 // Flush the InOrderDrawBuffer to possibly free up some textures
253 context->fFlushToReduceCacheSize = true;
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000254}
255
joshualitt0db6dfa2015-04-10 07:01:30 -0700256void GrContext::TextBlobCacheOverBudgetCB(void* data) {
257 SkASSERT(data);
258
259 // Unlike the GrResourceCache, TextBlobs are drawn at the SkGpuDevice level, therefore they
260 // cannot use fFlushTorReduceCacheSize because it uses AutoCheckFlush. The solution is to move
261 // drawText calls to below the GrContext level, but this is not trivial because they call
262 // drawPath on SkGpuDevice
263 GrContext* context = reinterpret_cast<GrContext*>(data);
264 context->flush();
265}
266
bsalomon@google.com91958362011-06-13 17:58:13 +0000267int GrContext::getMaxTextureSize() const {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000268 return SkTMin(fGpu->caps()->maxTextureSize(), fMaxTextureSizeOverride);
bsalomon@google.com91958362011-06-13 17:58:13 +0000269}
270
271int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000272 return fGpu->caps()->maxRenderTargetSize();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000273}
274
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000275int GrContext::getMaxSampleCount() const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000276 return fGpu->caps()->maxSampleCount();
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000277}
278
bsalomon@google.com27847de2011-02-22 20:59:41 +0000279///////////////////////////////////////////////////////////////////////////////
280
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000281void GrContext::clear(const SkIRect* rect,
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000282 const GrColor color,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000283 bool canIgnoreRect,
bsalomon41ebbdd2014-08-04 08:31:39 -0700284 GrRenderTarget* renderTarget) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800285 RETURN_IF_ABANDONED
bsalomon41ebbdd2014-08-04 08:31:39 -0700286 ASSERT_OWNED_RESOURCE(renderTarget);
bsalomon89c62982014-11-03 12:08:42 -0800287 SkASSERT(renderTarget);
288
bsalomonf21dab92014-11-13 13:33:28 -0800289 AutoCheckFlush acf(this);
egdanield78a1682014-07-09 10:41:26 -0700290 GR_CREATE_TRACE_MARKER_CONTEXT("GrContext::clear", this);
joshualitt570d2f82015-02-25 13:19:48 -0800291 GrDrawTarget* target = this->prepareToDraw();
bsalomon41ebbdd2014-08-04 08:31:39 -0700292 if (NULL == target) {
293 return;
294 }
295 target->clear(rect, color, canIgnoreRect, renderTarget);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000296}
297
joshualitt25d9c152015-02-18 12:29:52 -0800298void GrContext::drawPaint(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800299 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800300 const GrPaint& origPaint,
301 const SkMatrix& viewMatrix) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800302 RETURN_IF_ABANDONED
bsalomon@google.com27847de2011-02-22 20:59:41 +0000303 // set rect to be big enough to fill the space, but not super-huge, so we
304 // don't overflow fixed-point implementations
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000305 SkRect r;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000306 r.setLTRB(0, 0,
joshualitt25d9c152015-02-18 12:29:52 -0800307 SkIntToScalar(rt->width()),
308 SkIntToScalar(rt->height()));
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000309 SkTCopyOnFirstWrite<GrPaint> paint(origPaint);
joshualitt8fc6c2d2014-12-22 15:27:05 -0800310
311 // by definition this fills the entire clip, no need for AA
312 if (paint->isAntiAlias()) {
313 paint.writable()->setAntiAlias(false);
314 }
315
316 bool isPerspective = viewMatrix.hasPerspective();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000317
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000318 // We attempt to map r by the inverse matrix and draw that. mapRect will
319 // map the four corners and bound them with a new rect. This will not
320 // produce a correct result for some perspective matrices.
joshualitt8fc6c2d2014-12-22 15:27:05 -0800321 if (!isPerspective) {
joshualitt5531d512014-12-17 15:50:11 -0800322 SkMatrix inverse;
323 if (!viewMatrix.invert(&inverse)) {
tfarina38406c82014-10-31 07:11:12 -0700324 SkDebugf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000325 return;
326 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000327 inverse.mapRect(&r);
joshualitt570d2f82015-02-25 13:19:48 -0800328 this->drawRect(rt, clip, *paint, viewMatrix, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000329 } else {
joshualitt8fc6c2d2014-12-22 15:27:05 -0800330 SkMatrix localMatrix;
331 if (!viewMatrix.invert(&localMatrix)) {
tfarina38406c82014-10-31 07:11:12 -0700332 SkDebugf("Could not invert matrix\n");
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000333 return;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000334 }
joshualitt8fc6c2d2014-12-22 15:27:05 -0800335
336 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -0800337 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -0800338 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, paint, &acf);
joshualitt8fc6c2d2014-12-22 15:27:05 -0800339 if (NULL == target) {
340 return;
341 }
342
343 GR_CREATE_TRACE_MARKER("GrContext::drawPaintWithPerspective", target);
joshualitt44701df2015-02-23 14:44:57 -0800344 target->drawRect(&pipelineBuilder,
345 paint->getColor(),
346 SkMatrix::I(),
347 r,
348 NULL,
349 &localMatrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000350 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000351}
352
bsalomon@google.com205d4602011-04-25 12:43:45 +0000353////////////////////////////////////////////////////////////////////////////////
354
bsalomonc30aaa02014-08-13 07:15:29 -0700355static inline bool is_irect(const SkRect& r) {
tfarina38406c82014-10-31 07:11:12 -0700356 return SkScalarIsInt(r.fLeft) && SkScalarIsInt(r.fTop) &&
357 SkScalarIsInt(r.fRight) && SkScalarIsInt(r.fBottom);
bsalomonc30aaa02014-08-13 07:15:29 -0700358}
359
bsalomon@google.com205d4602011-04-25 12:43:45 +0000360static bool apply_aa_to_rect(GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -0800361 GrPipelineBuilder* pipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -0800362 SkRect* devBoundRect,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000363 const SkRect& rect,
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000364 SkScalar strokeWidth,
joshualitt2e3b3e32014-12-09 13:31:14 -0800365 const SkMatrix& combinedMatrix,
366 GrColor color) {
egdaniel8dd688b2015-01-22 10:16:09 -0800367 if (pipelineBuilder->getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000368 return false;
369 }
370
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000371#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000372 if (strokeWidth >= 0) {
373#endif
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000374 if (!combinedMatrix.preservesAxisAlignment()) {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000375 return false;
376 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000377
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000378#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000379 } else {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000380 if (!combinedMatrix.preservesRightAngles()) {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000381 return false;
382 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000383 }
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000384#endif
bsalomon@google.com205d4602011-04-25 12:43:45 +0000385
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000386 combinedMatrix.mapRect(devBoundRect, rect);
derekfe638d1c2014-12-02 13:51:29 -0800387 if (!combinedMatrix.rectStaysRect()) {
388 return true;
389 }
390
bsalomonc30aaa02014-08-13 07:15:29 -0700391 if (strokeWidth < 0) {
392 return !is_irect(*devBoundRect);
393 }
robertphillips@google.com28ac96e2013-05-13 13:38:35 +0000394
bsalomon9c0822a2014-08-11 11:07:48 -0700395 return true;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000396}
397
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000398static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) {
399 return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
400 point.fY >= rect.fTop && point.fY <= rect.fBottom;
401}
402
joshualitt0823bfa2015-02-27 10:06:15 -0800403class StrokeRectBatch : public GrBatch {
404public:
405 struct Geometry {
406 GrColor fColor;
407 SkMatrix fViewMatrix;
408 SkRect fRect;
409 SkScalar fStrokeWidth;
410 };
411
joshualitt4c977862015-05-13 08:00:56 -0700412 static GrBatch* Create(const Geometry& geometry, bool snapToPixelCenters) {
413 return SkNEW_ARGS(StrokeRectBatch, (geometry, snapToPixelCenters));
joshualitt0823bfa2015-02-27 10:06:15 -0800414 }
415
mtklein36352bf2015-03-25 18:17:31 -0700416 const char* name() const override { return "StrokeRectBatch"; }
joshualitt0823bfa2015-02-27 10:06:15 -0800417
mtklein36352bf2015-03-25 18:17:31 -0700418 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt0823bfa2015-02-27 10:06:15 -0800419 // When this is called on a batch, there is only one geometry bundle
420 out->setKnownFourComponents(fGeoData[0].fColor);
421 }
422
mtklein36352bf2015-03-25 18:17:31 -0700423 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualitt0823bfa2015-02-27 10:06:15 -0800424 out->setKnownSingleComponent(0xff);
425 }
426
mtklein36352bf2015-03-25 18:17:31 -0700427 void initBatchTracker(const GrPipelineInfo& init) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800428 // Handle any color overrides
429 if (init.fColorIgnored) {
430 fGeoData[0].fColor = GrColor_ILLEGAL;
431 } else if (GrColor_ILLEGAL != init.fOverrideColor) {
432 fGeoData[0].fColor = init.fOverrideColor;
433 }
434
435 // setup batch properties
436 fBatch.fColorIgnored = init.fColorIgnored;
437 fBatch.fColor = fGeoData[0].fColor;
438 fBatch.fUsesLocalCoords = init.fUsesLocalCoords;
439 fBatch.fCoverageIgnored = init.fCoverageIgnored;
440 }
441
mtklein36352bf2015-03-25 18:17:31 -0700442 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800443 SkAutoTUnref<const GrGeometryProcessor> gp(
444 GrDefaultGeoProcFactory::Create(GrDefaultGeoProcFactory::kPosition_GPType,
445 this->color(),
joshualittb8c241a2015-05-19 08:23:30 -0700446 this->usesLocalCoords(),
447 this->coverageIgnored(),
joshualitt0823bfa2015-02-27 10:06:15 -0800448 this->viewMatrix(),
449 SkMatrix::I()));
450
451 batchTarget->initDraw(gp, pipeline);
452
joshualitt0823bfa2015-02-27 10:06:15 -0800453 size_t vertexStride = gp->getVertexStride();
454
455 SkASSERT(vertexStride == sizeof(GrDefaultGeoProcFactory::PositionAttr));
456
457 Geometry& args = fGeoData[0];
458
459 int vertexCount = kVertsPerHairlineRect;
460 if (args.fStrokeWidth > 0) {
461 vertexCount = kVertsPerStrokeRect;
462 }
463
464 const GrVertexBuffer* vertexBuffer;
465 int firstVertex;
466
robertphillipse40d3972015-05-07 09:51:43 -0700467 void* verts = batchTarget->makeVertSpace(vertexStride, vertexCount,
468 &vertexBuffer, &firstVertex);
joshualitt0823bfa2015-02-27 10:06:15 -0800469
bsalomoncb8979d2015-05-05 09:51:38 -0700470 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -0800471 SkDebugf("Could not allocate vertices\n");
472 return;
473 }
474
bsalomoncb8979d2015-05-05 09:51:38 -0700475 SkPoint* vertex = reinterpret_cast<SkPoint*>(verts);
joshualitt0823bfa2015-02-27 10:06:15 -0800476
477 GrPrimitiveType primType;
478
479 if (args.fStrokeWidth > 0) {;
480 primType = kTriangleStrip_GrPrimitiveType;
481 args.fRect.sort();
482 this->setStrokeRectStrip(vertex, args.fRect, args.fStrokeWidth);
483 } else {
484 // hairline
485 primType = kLineStrip_GrPrimitiveType;
486 vertex[0].set(args.fRect.fLeft, args.fRect.fTop);
487 vertex[1].set(args.fRect.fRight, args.fRect.fTop);
488 vertex[2].set(args.fRect.fRight, args.fRect.fBottom);
489 vertex[3].set(args.fRect.fLeft, args.fRect.fBottom);
490 vertex[4].set(args.fRect.fLeft, args.fRect.fTop);
491 }
492
bsalomoncb8979d2015-05-05 09:51:38 -0700493 GrVertices vertices;
494 vertices.init(primType, vertexBuffer, firstVertex, vertexCount);
495 batchTarget->draw(vertices);
joshualitt0823bfa2015-02-27 10:06:15 -0800496 }
497
498 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
499
500private:
joshualitt4c977862015-05-13 08:00:56 -0700501 StrokeRectBatch(const Geometry& geometry, bool snapToPixelCenters) {
joshualitt0823bfa2015-02-27 10:06:15 -0800502 this->initClassID<StrokeRectBatch>();
503
504 fBatch.fHairline = geometry.fStrokeWidth == 0;
505
506 fGeoData.push_back(geometry);
joshualitt99c7c072015-05-01 13:43:30 -0700507
508 // setup bounds
509 fBounds = geometry.fRect;
510 SkScalar rad = SkScalarHalf(geometry.fStrokeWidth);
511 fBounds.outset(rad, rad);
512 geometry.fViewMatrix.mapRect(&fBounds);
joshualitt4c977862015-05-13 08:00:56 -0700513
514 // If our caller snaps to pixel centers then we have to round out the bounds
515 if (snapToPixelCenters) {
516 fBounds.roundOut();
517 }
joshualitt0823bfa2015-02-27 10:06:15 -0800518 }
519
520 /* create a triangle strip that strokes the specified rect. There are 8
521 unique vertices, but we repeat the last 2 to close up. Alternatively we
522 could use an indices array, and then only send 8 verts, but not sure that
523 would be faster.
524 */
525 void setStrokeRectStrip(SkPoint verts[10], const SkRect& rect, SkScalar width) {
526 const SkScalar rad = SkScalarHalf(width);
527 // TODO we should be able to enable this assert, but we'd have to filter these draws
528 // this is a bug
529 //SkASSERT(rad < rect.width() / 2 && rad < rect.height() / 2);
530
531 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
532 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
533 verts[2].set(rect.fRight - rad, rect.fTop + rad);
534 verts[3].set(rect.fRight + rad, rect.fTop - rad);
535 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
536 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
537 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
538 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
539 verts[8] = verts[0];
540 verts[9] = verts[1];
541 }
542
543
544 GrColor color() const { return fBatch.fColor; }
545 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
546 bool colorIgnored() const { return fBatch.fColorIgnored; }
547 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
548 bool hairline() const { return fBatch.fHairline; }
joshualittb8c241a2015-05-19 08:23:30 -0700549 bool coverageIgnored() const { return fBatch.fCoverageIgnored; }
joshualitt0823bfa2015-02-27 10:06:15 -0800550
mtklein36352bf2015-03-25 18:17:31 -0700551 bool onCombineIfPossible(GrBatch* t) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800552 // StrokeRectBatch* that = t->cast<StrokeRectBatch>();
553
554 // NonAA stroke rects cannot batch right now
555 // TODO make these batchable
556 return false;
557 }
558
559 struct BatchTracker {
560 GrColor fColor;
561 bool fUsesLocalCoords;
562 bool fColorIgnored;
563 bool fCoverageIgnored;
564 bool fHairline;
565 };
566
567 const static int kVertsPerHairlineRect = 5;
568 const static int kVertsPerStrokeRect = 10;
569
570 BatchTracker fBatch;
571 SkSTArray<1, Geometry, true> fGeoData;
572};
573
joshualitt25d9c152015-02-18 12:29:52 -0800574void GrContext::drawRect(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800575 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800576 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -0800577 const SkMatrix& viewMatrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000578 const SkRect& rect,
bsalomon01c8da12014-08-04 09:21:30 -0700579 const GrStrokeInfo* strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800580 RETURN_IF_ABANDONED
bsalomon49f085d2014-09-05 13:34:00 -0700581 if (strokeInfo && strokeInfo->isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700582 SkPath path;
kkinnunen806236e2015-05-21 22:45:56 -0700583 path.setIsVolatile(true);
egdanield58a0ba2014-06-11 10:30:05 -0700584 path.addRect(rect);
joshualitt570d2f82015-02-25 13:19:48 -0800585 this->drawPath(rt, clip, paint, viewMatrix, path, *strokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -0700586 return;
587 }
588
bsalomonf21dab92014-11-13 13:33:28 -0800589 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -0800590 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -0800591 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700592 if (NULL == target) {
593 return;
594 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000595
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000596 GR_CREATE_TRACE_MARKER("GrContext::drawRect", target);
kkinnunend156d362015-05-18 22:23:54 -0700597 SkScalar width = NULL == strokeInfo ? -1 : strokeInfo->getWidth();
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000598
599 // Check if this is a full RT draw and can be replaced with a clear. We don't bother checking
600 // cases where the RT is fully inside a stroke.
601 if (width < 0) {
602 SkRect rtRect;
egdaniel8dd688b2015-01-22 10:16:09 -0800603 pipelineBuilder.getRenderTarget()->getBoundsRect(&rtRect);
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000604 SkRect clipSpaceRTRect = rtRect;
joshualitt570d2f82015-02-25 13:19:48 -0800605 bool checkClip = GrClip::kWideOpen_ClipType != clip.clipType();
joshualitt44701df2015-02-23 14:44:57 -0800606 if (checkClip) {
joshualitt570d2f82015-02-25 13:19:48 -0800607 clipSpaceRTRect.offset(SkIntToScalar(clip.origin().fX),
608 SkIntToScalar(clip.origin().fY));
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000609 }
610 // Does the clip contain the entire RT?
joshualitt570d2f82015-02-25 13:19:48 -0800611 if (!checkClip || clip.quickContains(clipSpaceRTRect)) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000612 SkMatrix invM;
joshualitt8059eb92014-12-29 15:10:07 -0800613 if (!viewMatrix.invert(&invM)) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000614 return;
615 }
616 // Does the rect bound the RT?
617 SkPoint srcSpaceRTQuad[4];
618 invM.mapRectToQuad(srcSpaceRTQuad, rtRect);
619 if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) &&
620 rect_contains_inclusive(rect, srcSpaceRTQuad[1]) &&
621 rect_contains_inclusive(rect, srcSpaceRTQuad[2]) &&
622 rect_contains_inclusive(rect, srcSpaceRTQuad[3])) {
623 // Will it blend?
624 GrColor clearColor;
625 if (paint.isOpaqueAndConstantColor(&clearColor)) {
joshualitt25d9c152015-02-18 12:29:52 -0800626 target->clear(NULL, clearColor, true, rt);
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000627 return;
628 }
629 }
630 }
631 }
632
joshualitt2e3b3e32014-12-09 13:31:14 -0800633 GrColor color = paint.getColor();
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000634 SkRect devBoundRect;
egdaniel8dd688b2015-01-22 10:16:09 -0800635 bool needAA = paint.isAntiAlias() && !pipelineBuilder.getRenderTarget()->isMultisampled();
636 bool doAA = needAA && apply_aa_to_rect(target, &pipelineBuilder, &devBoundRect, rect, width,
joshualitt8059eb92014-12-29 15:10:07 -0800637 viewMatrix, color);
egdanield58a0ba2014-06-11 10:30:05 -0700638
bsalomon@google.com205d4602011-04-25 12:43:45 +0000639 if (doAA) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000640 if (width >= 0) {
joshualitt9853cce2014-11-17 14:22:48 -0800641 fAARectRenderer->strokeAARect(target,
egdaniel8dd688b2015-01-22 10:16:09 -0800642 &pipelineBuilder,
joshualitt2e3b3e32014-12-09 13:31:14 -0800643 color,
joshualitt8059eb92014-12-29 15:10:07 -0800644 viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800645 rect,
joshualitt9853cce2014-11-17 14:22:48 -0800646 devBoundRect,
kkinnunend156d362015-05-18 22:23:54 -0700647 *strokeInfo);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000648 } else {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000649 // filled AA rect
joshualitt8059eb92014-12-29 15:10:07 -0800650 fAARectRenderer->fillAARect(target,
egdaniel8dd688b2015-01-22 10:16:09 -0800651 &pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800652 color,
653 viewMatrix,
654 rect,
joshualittd27f73e2014-12-29 07:43:36 -0800655 devBoundRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000656 }
657 return;
658 }
659
bsalomon@google.com27847de2011-02-22 20:59:41 +0000660 if (width >= 0) {
joshualitt0823bfa2015-02-27 10:06:15 -0800661 StrokeRectBatch::Geometry geometry;
662 geometry.fViewMatrix = viewMatrix;
663 geometry.fColor = color;
664 geometry.fRect = rect;
665 geometry.fStrokeWidth = width;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000666
joshualitt4c977862015-05-13 08:00:56 -0700667 // Non-AA hairlines are snapped to pixel centers to make which pixels are hit deterministic
668 bool snapToPixelCenters = (0 == width && !rt->isMultisampled());
669 SkAutoTUnref<GrBatch> batch(StrokeRectBatch::Create(geometry, snapToPixelCenters));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000670
bsalomond79c5492015-04-27 10:07:04 -0700671 // Depending on sub-pixel coordinates and the particular GPU, we may lose a corner of
672 // hairline rects. We jam all the vertices to pixel centers to avoid this, but not when MSAA
673 // is enabled because it can cause ugly artifacts.
674 pipelineBuilder.setState(GrPipelineBuilder::kSnapVerticesToPixelCenters_Flag,
joshualitt4c977862015-05-13 08:00:56 -0700675 snapToPixelCenters);
joshualitt99c7c072015-05-01 13:43:30 -0700676 target->drawBatch(&pipelineBuilder, batch);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000677 } else {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000678 // filled BW rect
egdaniel8dd688b2015-01-22 10:16:09 -0800679 target->drawSimpleRect(&pipelineBuilder, color, viewMatrix, rect);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000680 }
681}
682
joshualitt25d9c152015-02-18 12:29:52 -0800683void GrContext::drawNonAARectToRect(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800684 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800685 const GrPaint& paint,
joshualitt16b27892014-12-18 07:47:16 -0800686 const SkMatrix& viewMatrix,
687 const SkRect& rectToDraw,
688 const SkRect& localRect,
689 const SkMatrix* localMatrix) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800690 RETURN_IF_ABANDONED
bsalomonf21dab92014-11-13 13:33:28 -0800691 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -0800692 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -0800693 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700694 if (NULL == target) {
695 return;
696 }
bsalomon@google.com64386952013-02-08 21:22:44 +0000697
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000698 GR_CREATE_TRACE_MARKER("GrContext::drawRectToRect", target);
699
joshualitt44701df2015-02-23 14:44:57 -0800700 target->drawRect(&pipelineBuilder,
701 paint.getColor(),
702 viewMatrix,
703 rectToDraw,
704 &localRect,
egdaniel8dd688b2015-01-22 10:16:09 -0800705 localMatrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000706}
707
joshualitt0823bfa2015-02-27 10:06:15 -0800708static const GrGeometryProcessor* set_vertex_attributes(bool hasLocalCoords,
709 bool hasColors,
joshualitt56995b52014-12-11 15:44:02 -0800710 int* colorOffset,
711 int* texOffset,
joshualitt8059eb92014-12-29 15:10:07 -0800712 GrColor color,
joshualittb8c241a2015-05-19 08:23:30 -0700713 const SkMatrix& viewMatrix,
714 bool coverageIgnored) {
robertphillips@google.com42903302013-04-20 12:26:07 +0000715 *texOffset = -1;
716 *colorOffset = -1;
joshualitt5478d422014-11-14 16:00:38 -0800717 uint32_t flags = GrDefaultGeoProcFactory::kPosition_GPType;
joshualitt0823bfa2015-02-27 10:06:15 -0800718 if (hasLocalCoords && hasColors) {
joshualitt5478d422014-11-14 16:00:38 -0800719 *colorOffset = sizeof(SkPoint);
720 *texOffset = sizeof(SkPoint) + sizeof(GrColor);
721 flags |= GrDefaultGeoProcFactory::kColor_GPType |
722 GrDefaultGeoProcFactory::kLocalCoord_GPType;
joshualitt0823bfa2015-02-27 10:06:15 -0800723 } else if (hasLocalCoords) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000724 *texOffset = sizeof(SkPoint);
joshualitt5478d422014-11-14 16:00:38 -0800725 flags |= GrDefaultGeoProcFactory::kLocalCoord_GPType;
joshualitt0823bfa2015-02-27 10:06:15 -0800726 } else if (hasColors) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000727 *colorOffset = sizeof(SkPoint);
joshualitt5478d422014-11-14 16:00:38 -0800728 flags |= GrDefaultGeoProcFactory::kColor_GPType;
robertphillips@google.com42903302013-04-20 12:26:07 +0000729 }
joshualittb8c241a2015-05-19 08:23:30 -0700730 return GrDefaultGeoProcFactory::Create(flags, color, hasLocalCoords, coverageIgnored,
731 viewMatrix, SkMatrix::I());
robertphillips@google.com42903302013-04-20 12:26:07 +0000732}
733
joshualitt0823bfa2015-02-27 10:06:15 -0800734class DrawVerticesBatch : public GrBatch {
735public:
736 struct Geometry {
737 GrColor fColor;
738 SkTDArray<SkPoint> fPositions;
739 SkTDArray<uint16_t> fIndices;
740 SkTDArray<GrColor> fColors;
741 SkTDArray<SkPoint> fLocalCoords;
742 };
743
744 static GrBatch* Create(const Geometry& geometry, GrPrimitiveType primitiveType,
745 const SkMatrix& viewMatrix,
746 const SkPoint* positions, int vertexCount,
747 const uint16_t* indices, int indexCount,
joshualitt99c7c072015-05-01 13:43:30 -0700748 const GrColor* colors, const SkPoint* localCoords,
749 const SkRect& bounds) {
joshualitt0823bfa2015-02-27 10:06:15 -0800750 return SkNEW_ARGS(DrawVerticesBatch, (geometry, primitiveType, viewMatrix, positions,
751 vertexCount, indices, indexCount, colors,
joshualitt99c7c072015-05-01 13:43:30 -0700752 localCoords, bounds));
joshualitt0823bfa2015-02-27 10:06:15 -0800753 }
754
mtklein36352bf2015-03-25 18:17:31 -0700755 const char* name() const override { return "DrawVerticesBatch"; }
joshualitt0823bfa2015-02-27 10:06:15 -0800756
mtklein36352bf2015-03-25 18:17:31 -0700757 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt0823bfa2015-02-27 10:06:15 -0800758 // When this is called on a batch, there is only one geometry bundle
759 if (this->hasColors()) {
760 out->setUnknownFourComponents();
761 } else {
762 out->setKnownFourComponents(fGeoData[0].fColor);
763 }
764 }
765
mtklein36352bf2015-03-25 18:17:31 -0700766 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualittd4c7d552015-02-27 13:00:56 -0800767 out->setKnownSingleComponent(0xff);
joshualitt0823bfa2015-02-27 10:06:15 -0800768 }
769
mtklein36352bf2015-03-25 18:17:31 -0700770 void initBatchTracker(const GrPipelineInfo& init) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800771 // Handle any color overrides
772 if (init.fColorIgnored) {
773 fGeoData[0].fColor = GrColor_ILLEGAL;
774 } else if (GrColor_ILLEGAL != init.fOverrideColor) {
775 fGeoData[0].fColor = init.fOverrideColor;
776 }
777
778 // setup batch properties
779 fBatch.fColorIgnored = init.fColorIgnored;
780 fBatch.fColor = fGeoData[0].fColor;
781 fBatch.fUsesLocalCoords = init.fUsesLocalCoords;
782 fBatch.fCoverageIgnored = init.fCoverageIgnored;
783 }
784
mtklein36352bf2015-03-25 18:17:31 -0700785 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800786 int colorOffset = -1, texOffset = -1;
787 SkAutoTUnref<const GrGeometryProcessor> gp(
788 set_vertex_attributes(this->hasLocalCoords(), this->hasColors(), &colorOffset,
joshualittb8c241a2015-05-19 08:23:30 -0700789 &texOffset, this->color(), this->viewMatrix(),
790 this->coverageIgnored()));
joshualitt0823bfa2015-02-27 10:06:15 -0800791
792 batchTarget->initDraw(gp, pipeline);
793
joshualitt0823bfa2015-02-27 10:06:15 -0800794 size_t vertexStride = gp->getVertexStride();
795
796 SkASSERT(vertexStride == sizeof(SkPoint) + (this->hasLocalCoords() ? sizeof(SkPoint) : 0)
797 + (this->hasColors() ? sizeof(GrColor) : 0));
798
799 int instanceCount = fGeoData.count();
800
801 const GrVertexBuffer* vertexBuffer;
802 int firstVertex;
803
robertphillipse40d3972015-05-07 09:51:43 -0700804 void* verts = batchTarget->makeVertSpace(vertexStride, this->vertexCount(),
805 &vertexBuffer, &firstVertex);
joshualitt0823bfa2015-02-27 10:06:15 -0800806
bsalomoncb8979d2015-05-05 09:51:38 -0700807 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -0800808 SkDebugf("Could not allocate vertices\n");
809 return;
810 }
811
bsalomonb5238a72015-05-05 07:49:49 -0700812 const GrIndexBuffer* indexBuffer = NULL;
813 int firstIndex = 0;
joshualitt0823bfa2015-02-27 10:06:15 -0800814
robertphillipse40d3972015-05-07 09:51:43 -0700815 uint16_t* indices = NULL;
joshualitt0823bfa2015-02-27 10:06:15 -0800816 if (this->hasIndices()) {
robertphillipse40d3972015-05-07 09:51:43 -0700817 indices = batchTarget->makeIndexSpace(this->indexCount(), &indexBuffer, &firstIndex);
joshualitt4b31de82015-03-05 14:33:41 -0800818
819 if (!indices) {
820 SkDebugf("Could not allocate indices\n");
821 return;
822 }
joshualitt0823bfa2015-02-27 10:06:15 -0800823 }
824
825 int indexOffset = 0;
826 int vertexOffset = 0;
827 for (int i = 0; i < instanceCount; i++) {
828 const Geometry& args = fGeoData[i];
829
830 // TODO we can actually cache this interleaved and then just memcopy
831 if (this->hasIndices()) {
832 for (int j = 0; j < args.fIndices.count(); ++j, ++indexOffset) {
robertphillipse40d3972015-05-07 09:51:43 -0700833 *(indices + indexOffset) = args.fIndices[j] + vertexOffset;
joshualitt0823bfa2015-02-27 10:06:15 -0800834 }
835 }
836
837 for (int j = 0; j < args.fPositions.count(); ++j) {
bsalomoncb8979d2015-05-05 09:51:38 -0700838 *((SkPoint*)verts) = args.fPositions[j];
joshualitt0823bfa2015-02-27 10:06:15 -0800839 if (this->hasColors()) {
bsalomoncb8979d2015-05-05 09:51:38 -0700840 *(GrColor*)((intptr_t)verts + colorOffset) = args.fColors[j];
joshualitt0823bfa2015-02-27 10:06:15 -0800841 }
842 if (this->hasLocalCoords()) {
bsalomoncb8979d2015-05-05 09:51:38 -0700843 *(SkPoint*)((intptr_t)verts + texOffset) = args.fLocalCoords[j];
joshualitt0823bfa2015-02-27 10:06:15 -0800844 }
bsalomoncb8979d2015-05-05 09:51:38 -0700845 verts = (void*)((intptr_t)verts + vertexStride);
joshualitt0823bfa2015-02-27 10:06:15 -0800846 vertexOffset++;
847 }
848 }
849
bsalomoncb8979d2015-05-05 09:51:38 -0700850 GrVertices vertices;
joshualitt0823bfa2015-02-27 10:06:15 -0800851 if (this->hasIndices()) {
bsalomoncb8979d2015-05-05 09:51:38 -0700852 vertices.initIndexed(this->primitiveType(), vertexBuffer, indexBuffer, firstVertex,
bsalomonb5238a72015-05-05 07:49:49 -0700853 firstIndex, this->vertexCount(), this->indexCount());
854
joshualitt0823bfa2015-02-27 10:06:15 -0800855 } else {
bsalomoncb8979d2015-05-05 09:51:38 -0700856 vertices.init(this->primitiveType(), vertexBuffer, firstVertex, this->vertexCount());
joshualitt0823bfa2015-02-27 10:06:15 -0800857 }
bsalomoncb8979d2015-05-05 09:51:38 -0700858 batchTarget->draw(vertices);
joshualitt0823bfa2015-02-27 10:06:15 -0800859 }
860
861 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
862
863private:
864 DrawVerticesBatch(const Geometry& geometry, GrPrimitiveType primitiveType,
865 const SkMatrix& viewMatrix,
866 const SkPoint* positions, int vertexCount,
867 const uint16_t* indices, int indexCount,
joshualitt99c7c072015-05-01 13:43:30 -0700868 const GrColor* colors, const SkPoint* localCoords, const SkRect& bounds) {
joshualitt0823bfa2015-02-27 10:06:15 -0800869 this->initClassID<DrawVerticesBatch>();
870 SkASSERT(positions);
871
872 fBatch.fViewMatrix = viewMatrix;
873 Geometry& installedGeo = fGeoData.push_back(geometry);
874
875 installedGeo.fPositions.append(vertexCount, positions);
876 if (indices) {
877 installedGeo.fIndices.append(indexCount, indices);
878 fBatch.fHasIndices = true;
879 } else {
880 fBatch.fHasIndices = false;
881 }
882
883 if (colors) {
884 installedGeo.fColors.append(vertexCount, colors);
885 fBatch.fHasColors = true;
886 } else {
887 fBatch.fHasColors = false;
888 }
889
890 if (localCoords) {
891 installedGeo.fLocalCoords.append(vertexCount, localCoords);
892 fBatch.fHasLocalCoords = true;
893 } else {
894 fBatch.fHasLocalCoords = false;
895 }
896 fBatch.fVertexCount = vertexCount;
897 fBatch.fIndexCount = indexCount;
898 fBatch.fPrimitiveType = primitiveType;
joshualitt99c7c072015-05-01 13:43:30 -0700899
900 this->setBounds(bounds);
joshualitt0823bfa2015-02-27 10:06:15 -0800901 }
902
903 GrPrimitiveType primitiveType() const { return fBatch.fPrimitiveType; }
904 bool batchablePrimitiveType() const {
905 return kTriangles_GrPrimitiveType == fBatch.fPrimitiveType ||
906 kLines_GrPrimitiveType == fBatch.fPrimitiveType ||
907 kPoints_GrPrimitiveType == fBatch.fPrimitiveType;
908 }
909 GrColor color() const { return fBatch.fColor; }
910 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
911 bool colorIgnored() const { return fBatch.fColorIgnored; }
912 const SkMatrix& viewMatrix() const { return fBatch.fViewMatrix; }
913 bool hasColors() const { return fBatch.fHasColors; }
914 bool hasIndices() const { return fBatch.fHasIndices; }
915 bool hasLocalCoords() const { return fBatch.fHasLocalCoords; }
916 int vertexCount() const { return fBatch.fVertexCount; }
917 int indexCount() const { return fBatch.fIndexCount; }
joshualittb8c241a2015-05-19 08:23:30 -0700918 bool coverageIgnored() const { return fBatch.fCoverageIgnored; }
joshualitt0823bfa2015-02-27 10:06:15 -0800919
mtklein36352bf2015-03-25 18:17:31 -0700920 bool onCombineIfPossible(GrBatch* t) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800921 DrawVerticesBatch* that = t->cast<DrawVerticesBatch>();
922
923 if (!this->batchablePrimitiveType() || this->primitiveType() != that->primitiveType()) {
924 return false;
925 }
926
927 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
928
929 // We currently use a uniform viewmatrix for this batch
930 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
931 return false;
932 }
933
934 if (this->hasColors() != that->hasColors()) {
935 return false;
936 }
937
938 if (this->hasIndices() != that->hasIndices()) {
939 return false;
940 }
941
942 if (this->hasLocalCoords() != that->hasLocalCoords()) {
943 return false;
944 }
945
946 if (!this->hasColors() && this->color() != that->color()) {
947 return false;
948 }
949
950 if (this->color() != that->color()) {
951 fBatch.fColor = GrColor_ILLEGAL;
952 }
953 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
954 fBatch.fVertexCount += that->vertexCount();
955 fBatch.fIndexCount += that->indexCount();
joshualitt99c7c072015-05-01 13:43:30 -0700956
957 this->joinBounds(that->bounds());
joshualitt0823bfa2015-02-27 10:06:15 -0800958 return true;
959 }
960
961 struct BatchTracker {
962 GrPrimitiveType fPrimitiveType;
963 SkMatrix fViewMatrix;
964 GrColor fColor;
965 bool fUsesLocalCoords;
966 bool fColorIgnored;
967 bool fCoverageIgnored;
968 bool fHasColors;
969 bool fHasIndices;
970 bool fHasLocalCoords;
971 int fVertexCount;
972 int fIndexCount;
973 };
974
975 BatchTracker fBatch;
976 SkSTArray<1, Geometry, true> fGeoData;
977};
978
joshualitt25d9c152015-02-18 12:29:52 -0800979void GrContext::drawVertices(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800980 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800981 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -0800982 const SkMatrix& viewMatrix,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000983 GrPrimitiveType primitiveType,
984 int vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000985 const SkPoint positions[],
986 const SkPoint texCoords[],
bsalomon@google.com27847de2011-02-22 20:59:41 +0000987 const GrColor colors[],
988 const uint16_t indices[],
989 int indexCount) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800990 RETURN_IF_ABANDONED
bsalomonf21dab92014-11-13 13:33:28 -0800991 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -0800992 GrPipelineBuilder pipelineBuilder;
commit-bot@chromium.org5a567932014-01-08 21:26:09 +0000993
joshualitt570d2f82015-02-25 13:19:48 -0800994 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700995 if (NULL == target) {
996 return;
997 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000998
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000999 GR_CREATE_TRACE_MARKER("GrContext::drawVertices", target);
1000
joshualitta09c0462015-04-30 14:03:02 -07001001 // TODO clients should give us bounds
1002 SkRect bounds;
1003 if (!bounds.setBoundsCheck(positions, vertexCount)) {
1004 SkDebugf("drawVertices call empty bounds\n");
1005 return;
1006 }
1007
joshualitt0c14bcc2015-05-05 13:58:20 -07001008 viewMatrix.mapRect(&bounds);
1009
joshualitt4c977862015-05-13 08:00:56 -07001010 // If we don't have AA then we outset for a half pixel in each direction to account for
1011 // snapping
1012 if (!paint.isAntiAlias()) {
1013 bounds.outset(0.5f, 0.5f);
1014 }
1015
joshualitt0823bfa2015-02-27 10:06:15 -08001016 DrawVerticesBatch::Geometry geometry;
1017 geometry.fColor = paint.getColor();
joshualitt0823bfa2015-02-27 10:06:15 -08001018 SkAutoTUnref<GrBatch> batch(DrawVerticesBatch::Create(geometry, primitiveType, viewMatrix,
1019 positions, vertexCount, indices,
joshualitt99c7c072015-05-01 13:43:30 -07001020 indexCount, colors, texCoords,
1021 bounds));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001022
joshualitt99c7c072015-05-01 13:43:30 -07001023 target->drawBatch(&pipelineBuilder, batch);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001024}
1025
bsalomon@google.com06afe7b2011-04-26 15:31:40 +00001026///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com93c96602012-04-27 13:05:21 +00001027
joshualitt25d9c152015-02-18 12:29:52 -08001028void GrContext::drawRRect(GrRenderTarget*rt,
joshualitt570d2f82015-02-25 13:19:48 -08001029 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001030 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001031 const SkMatrix& viewMatrix,
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001032 const SkRRect& rrect,
egdanield58a0ba2014-06-11 10:30:05 -07001033 const GrStrokeInfo& strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001034 RETURN_IF_ABANDONED
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001035 if (rrect.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001036 return;
1037 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001038
egdanield58a0ba2014-06-11 10:30:05 -07001039 if (strokeInfo.isDashed()) {
1040 SkPath path;
kkinnunen806236e2015-05-21 22:45:56 -07001041 path.setIsVolatile(true);
egdanield58a0ba2014-06-11 10:30:05 -07001042 path.addRRect(rrect);
joshualitt570d2f82015-02-25 13:19:48 -08001043 this->drawPath(rt, clip, paint, viewMatrix, path, strokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -07001044 return;
1045 }
1046
bsalomonf21dab92014-11-13 13:33:28 -08001047 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001048 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -08001049 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001050 if (NULL == target) {
1051 return;
1052 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001053
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001054 GR_CREATE_TRACE_MARKER("GrContext::drawRRect", target);
1055
joshualitt2e3b3e32014-12-09 13:31:14 -08001056 GrColor color = paint.getColor();
joshualitt44701df2015-02-23 14:44:57 -08001057 if (!fOvalRenderer->drawRRect(target,
1058 &pipelineBuilder,
1059 color,
1060 viewMatrix,
1061 paint.isAntiAlias(),
1062 rrect,
kkinnunend156d362015-05-18 22:23:54 -07001063 strokeInfo)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001064 SkPath path;
kkinnunen806236e2015-05-21 22:45:56 -07001065 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001066 path.addRRect(rrect);
egdaniel8dd688b2015-01-22 10:16:09 -08001067 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(),
1068 path, strokeInfo);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001069 }
1070}
1071
1072///////////////////////////////////////////////////////////////////////////////
1073
joshualitt25d9c152015-02-18 12:29:52 -08001074void GrContext::drawDRRect(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -08001075 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001076 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001077 const SkMatrix& viewMatrix,
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001078 const SkRRect& outer,
1079 const SkRRect& inner) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001080 RETURN_IF_ABANDONED
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001081 if (outer.isEmpty()) {
1082 return;
1083 }
1084
bsalomonf21dab92014-11-13 13:33:28 -08001085 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001086 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -08001087 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001088
1089 GR_CREATE_TRACE_MARKER("GrContext::drawDRRect", target);
1090
joshualitt2e3b3e32014-12-09 13:31:14 -08001091 GrColor color = paint.getColor();
joshualitt44701df2015-02-23 14:44:57 -08001092 if (!fOvalRenderer->drawDRRect(target,
1093 &pipelineBuilder,
1094 color,
1095 viewMatrix,
1096 paint.isAntiAlias(),
1097 outer,
1098 inner)) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001099 SkPath path;
kkinnunen806236e2015-05-21 22:45:56 -07001100 path.setIsVolatile(true);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001101 path.addRRect(inner);
1102 path.addRRect(outer);
1103 path.setFillType(SkPath::kEvenOdd_FillType);
egdanield58a0ba2014-06-11 10:30:05 -07001104 GrStrokeInfo fillRec(SkStrokeRec::kFill_InitStyle);
egdaniel8dd688b2015-01-22 10:16:09 -08001105 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(),
1106 path, fillRec);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001107 }
1108}
1109
1110///////////////////////////////////////////////////////////////////////////////
1111
joshualitt570d2f82015-02-25 13:19:48 -08001112void GrContext::drawOval(GrRenderTarget* rt,
1113 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001114 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001115 const SkMatrix& viewMatrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001116 const SkRect& oval,
egdanield58a0ba2014-06-11 10:30:05 -07001117 const GrStrokeInfo& strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001118 RETURN_IF_ABANDONED
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001119 if (oval.isEmpty()) {
1120 return;
1121 }
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001122
egdanield58a0ba2014-06-11 10:30:05 -07001123 if (strokeInfo.isDashed()) {
1124 SkPath path;
kkinnunen806236e2015-05-21 22:45:56 -07001125 path.setIsVolatile(true);
egdanield58a0ba2014-06-11 10:30:05 -07001126 path.addOval(oval);
joshualitt570d2f82015-02-25 13:19:48 -08001127 this->drawPath(rt, clip, paint, viewMatrix, path, strokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -07001128 return;
1129 }
1130
bsalomonf21dab92014-11-13 13:33:28 -08001131 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001132 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -08001133 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001134 if (NULL == target) {
1135 return;
1136 }
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001137
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001138 GR_CREATE_TRACE_MARKER("GrContext::drawOval", target);
1139
joshualitt2e3b3e32014-12-09 13:31:14 -08001140 GrColor color = paint.getColor();
joshualitt44701df2015-02-23 14:44:57 -08001141 if (!fOvalRenderer->drawOval(target,
1142 &pipelineBuilder,
1143 color,
1144 viewMatrix,
1145 paint.isAntiAlias(),
1146 oval,
kkinnunend156d362015-05-18 22:23:54 -07001147 strokeInfo)) {
bsalomon@google.com93c96602012-04-27 13:05:21 +00001148 SkPath path;
kkinnunen806236e2015-05-21 22:45:56 -07001149 path.setIsVolatile(true);
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001150 path.addOval(oval);
egdaniel8dd688b2015-01-22 10:16:09 -08001151 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(),
1152 path, strokeInfo);
skia.committer@gmail.com98ded842013-01-23 07:06:17 +00001153 }
bsalomon@google.com150d2842012-01-12 20:19:56 +00001154}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001155
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001156// Can 'path' be drawn as a pair of filled nested rectangles?
1157static bool is_nested_rects(GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -08001158 GrPipelineBuilder* pipelineBuilder,
joshualitt2e3b3e32014-12-09 13:31:14 -08001159 GrColor color,
joshualitt8059eb92014-12-29 15:10:07 -08001160 const SkMatrix& viewMatrix,
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001161 const SkPath& path,
1162 const SkStrokeRec& stroke,
bsalomon9c0822a2014-08-11 11:07:48 -07001163 SkRect rects[2]) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001164 SkASSERT(stroke.isFillStyle());
1165
1166 if (path.isInverseFillType()) {
1167 return false;
1168 }
1169
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001170 // TODO: this restriction could be lifted if we were willing to apply
1171 // the matrix to all the points individually rather than just to the rect
joshualitt8059eb92014-12-29 15:10:07 -08001172 if (!viewMatrix.preservesAxisAlignment()) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001173 return false;
1174 }
1175
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001176 SkPath::Direction dirs[2];
caryclark95bc5f32015-04-08 08:34:15 -07001177 if (!path.isNestedFillRects(rects, dirs)) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001178 return false;
1179 }
1180
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001181 if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001182 // The two rects need to be wound opposite to each other
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001183 return false;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001184 }
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001185
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001186 // Right now, nested rects where the margin is not the same width
1187 // all around do not render correctly
1188 const SkScalar* outer = rects[0].asScalars();
1189 const SkScalar* inner = rects[1].asScalars();
1190
robertphillips183e9852014-10-21 11:25:37 -07001191 bool allEq = true;
1192
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001193 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
robertphillips183e9852014-10-21 11:25:37 -07001194 bool allGoE1 = margin >= SK_Scalar1;
1195
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001196 for (int i = 1; i < 4; ++i) {
1197 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
robertphillips183e9852014-10-21 11:25:37 -07001198 if (temp < SK_Scalar1) {
1199 allGoE1 = false;
1200 }
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001201 if (!SkScalarNearlyEqual(margin, temp)) {
robertphillips183e9852014-10-21 11:25:37 -07001202 allEq = false;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001203 }
1204 }
1205
robertphillips183e9852014-10-21 11:25:37 -07001206 return allEq || allGoE1;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001207}
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001208
joshualitt25d9c152015-02-18 12:29:52 -08001209void GrContext::drawPath(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -08001210 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001211 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001212 const SkMatrix& viewMatrix,
1213 const SkPath& path,
1214 const GrStrokeInfo& strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001215 RETURN_IF_ABANDONED
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001216 if (path.isEmpty()) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001217 if (path.isInverseFillType()) {
joshualitt570d2f82015-02-25 13:19:48 -08001218 this->drawPaint(rt, clip, paint, viewMatrix);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001219 }
1220 return;
1221 }
1222
joshualitt2e3b3e32014-12-09 13:31:14 -08001223 GrColor color = paint.getColor();
egdanield58a0ba2014-06-11 10:30:05 -07001224
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001225 // Note that internalDrawPath may sw-rasterize the path into a scratch texture.
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001226 // Scratch textures can be recycled after they are returned to the texture
1227 // cache. This presents a potential hazard for buffered drawing. However,
1228 // the writePixels that uploads to the scratch will perform a flush so we're
1229 // OK.
bsalomonf21dab92014-11-13 13:33:28 -08001230 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001231 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -08001232 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001233 if (NULL == target) {
1234 return;
1235 }
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001236
egdaniel93a37bc2014-07-21 13:47:57 -07001237 GR_CREATE_TRACE_MARKER1("GrContext::drawPath", target, "Is Convex", path.isConvex());
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001238
kkinnunen18996512015-04-26 23:18:49 -07001239 if (!strokeInfo.isDashed()) {
kkinnunen18996512015-04-26 23:18:49 -07001240 bool useCoverageAA = paint.isAntiAlias() &&
1241 !pipelineBuilder.getRenderTarget()->isMultisampled();
egdanield58a0ba2014-06-11 10:30:05 -07001242
kkinnunend156d362015-05-18 22:23:54 -07001243 if (useCoverageAA && strokeInfo.getWidth() < 0 && !path.isConvex()) {
kkinnunen18996512015-04-26 23:18:49 -07001244 // Concave AA paths are expensive - try to avoid them for special cases
1245 SkRect rects[2];
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001246
kkinnunend156d362015-05-18 22:23:54 -07001247 if (is_nested_rects(target, &pipelineBuilder, color, viewMatrix, path, strokeInfo,
kkinnunen18996512015-04-26 23:18:49 -07001248 rects)) {
1249 fAARectRenderer->fillAANestedRects(target, &pipelineBuilder, color, viewMatrix,
1250 rects);
1251 return;
1252 }
1253 }
1254 SkRect ovalRect;
1255 bool isOval = path.isOval(&ovalRect);
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001256
kkinnunen18996512015-04-26 23:18:49 -07001257 if (isOval && !path.isInverseFillType()) {
1258 if (fOvalRenderer->drawOval(target,
1259 &pipelineBuilder,
1260 color,
1261 viewMatrix,
1262 paint.isAntiAlias(),
1263 ovalRect,
kkinnunend156d362015-05-18 22:23:54 -07001264 strokeInfo)) {
kkinnunen18996512015-04-26 23:18:49 -07001265 return;
1266 }
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001267 }
1268 }
kkinnunen18996512015-04-26 23:18:49 -07001269 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(),
1270 path, strokeInfo);
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001271}
1272
joshualitt9853cce2014-11-17 14:22:48 -08001273void GrContext::internalDrawPath(GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -08001274 GrPipelineBuilder* pipelineBuilder,
joshualitt5531d512014-12-17 15:50:11 -08001275 const SkMatrix& viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -08001276 GrColor color,
joshualitt9853cce2014-11-17 14:22:48 -08001277 bool useAA,
1278 const SkPath& path,
egdanield58a0ba2014-06-11 10:30:05 -07001279 const GrStrokeInfo& strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001280 RETURN_IF_ABANDONED
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001281 SkASSERT(!path.isEmpty());
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001282
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001283 GR_CREATE_TRACE_MARKER("GrContext::internalDrawPath", target);
1284
1285
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001286 // An Assumption here is that path renderer would use some form of tweaking
1287 // the src color (either the input alpha or in the frag shader) to implement
1288 // aa. If we have some future driver-mojo path AA that can do the right
1289 // thing WRT to the blend then we'll need some query on the PR.
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001290 bool useCoverageAA = useAA &&
egdaniel0bdeec92015-02-23 12:12:54 -08001291 !pipelineBuilder->getRenderTarget()->isMultisampled();
bsalomon@google.com289533a2011-10-27 12:34:25 +00001292
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001293
1294 GrPathRendererChain::DrawType type =
1295 useCoverageAA ? GrPathRendererChain::kColorAntiAlias_DrawType :
joshualitt9853cce2014-11-17 14:22:48 -08001296 GrPathRendererChain::kColor_DrawType;
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001297
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001298 const SkPath* pathPtr = &path;
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001299 SkTLazy<SkPath> tmpPath;
kkinnunen18996512015-04-26 23:18:49 -07001300 const GrStrokeInfo* strokeInfoPtr = &strokeInfo;
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001301
1302 // Try a 1st time without stroking the path and without allowing the SW renderer
egdaniel8dd688b2015-01-22 10:16:09 -08001303 GrPathRenderer* pr = this->getPathRenderer(target, pipelineBuilder, viewMatrix, *pathPtr,
kkinnunen18996512015-04-26 23:18:49 -07001304 *strokeInfoPtr, false, type);
1305
1306 GrStrokeInfo dashlessStrokeInfo(strokeInfo, false);
1307 if (NULL == pr && strokeInfo.isDashed()) {
1308 // It didn't work above, so try again with dashed stroke converted to a dashless stroke.
kkinnunend156d362015-05-18 22:23:54 -07001309 if (!strokeInfo.applyDashToPath(tmpPath.init(), &dashlessStrokeInfo, *pathPtr)) {
bsalomon07ab9f32015-05-04 11:41:57 -07001310 return;
kkinnunen18996512015-04-26 23:18:49 -07001311 }
bsalomon07ab9f32015-05-04 11:41:57 -07001312 pathPtr = tmpPath.get();
1313 if (pathPtr->isEmpty()) {
1314 return;
1315 }
1316 strokeInfoPtr = &dashlessStrokeInfo;
kkinnunen18996512015-04-26 23:18:49 -07001317 pr = this->getPathRenderer(target, pipelineBuilder, viewMatrix, *pathPtr, *strokeInfoPtr,
1318 false, type);
1319 }
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001320
robertphillips@google.come79f3202014-02-11 16:30:21 +00001321 if (NULL == pr) {
bsalomon07ab9f32015-05-04 11:41:57 -07001322 if (!GrPathRenderer::IsStrokeHairlineOrEquivalent(*strokeInfoPtr, viewMatrix, NULL) &&
1323 !strokeInfoPtr->isFillStyle()) {
kkinnunen18996512015-04-26 23:18:49 -07001324 // It didn't work above, so try again with stroke converted to a fill.
1325 if (!tmpPath.isValid()) {
1326 tmpPath.init();
1327 }
kkinnunend156d362015-05-18 22:23:54 -07001328 dashlessStrokeInfo.setResScale(SkScalarAbs(viewMatrix.getMaxScale()));
1329 if (!dashlessStrokeInfo.applyToPath(tmpPath.get(), *pathPtr)) {
bsalomon07ab9f32015-05-04 11:41:57 -07001330 return;
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001331 }
bsalomon07ab9f32015-05-04 11:41:57 -07001332 pathPtr = tmpPath.get();
1333 if (pathPtr->isEmpty()) {
1334 return;
1335 }
kkinnunend156d362015-05-18 22:23:54 -07001336 dashlessStrokeInfo.setFillStyle();
bsalomon07ab9f32015-05-04 11:41:57 -07001337 strokeInfoPtr = &dashlessStrokeInfo;
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001338 }
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001339
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001340 // This time, allow SW renderer
kkinnunen18996512015-04-26 23:18:49 -07001341 pr = this->getPathRenderer(target, pipelineBuilder, viewMatrix, *pathPtr, *strokeInfoPtr,
1342 true, type);
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001343 }
1344
robertphillips@google.come79f3202014-02-11 16:30:21 +00001345 if (NULL == pr) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001346#ifdef SK_DEBUG
tfarina38406c82014-10-31 07:11:12 -07001347 SkDebugf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001348#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001349 return;
1350 }
1351
kkinnunen18996512015-04-26 23:18:49 -07001352 pr->drawPath(target, pipelineBuilder, color, viewMatrix, *pathPtr, *strokeInfoPtr, useCoverageAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001353}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001354
bsalomon@google.com27847de2011-02-22 20:59:41 +00001355////////////////////////////////////////////////////////////////////////////////
1356
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001357void GrContext::flush(int flagsBitfield) {
robertphillips@google.come7db8d62013-07-04 11:48:52 +00001358 if (NULL == fDrawBuffer) {
1359 return;
1360 }
1361
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001362 if (kDiscard_FlushBit & flagsBitfield) {
1363 fDrawBuffer->reset();
1364 } else {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001365 fDrawBuffer->flush();
junov@google.com53a55842011-06-08 22:55:10 +00001366 }
bsalomon3f324322015-04-08 11:01:54 -07001367 fResourceCache->notifyFlushOccurred();
bsalomonf21dab92014-11-13 13:33:28 -08001368 fFlushToReduceCacheSize = false;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001369}
1370
bsalomon81beccc2014-10-13 12:32:55 -07001371bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
1372 const void* inPixels, size_t outRowBytes, void* outPixels) {
1373 SkSrcPixelInfo srcPI;
jvanverthfa1e8a72014-12-22 08:31:49 -08001374 if (!GrPixelConfig2ColorAndProfileType(srcConfig, &srcPI.fColorType, NULL)) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001375 return false;
1376 }
bsalomon81beccc2014-10-13 12:32:55 -07001377 srcPI.fAlphaType = kUnpremul_SkAlphaType;
1378 srcPI.fPixels = inPixels;
1379 srcPI.fRowBytes = inRowBytes;
1380
1381 SkDstPixelInfo dstPI;
1382 dstPI.fColorType = srcPI.fColorType;
1383 dstPI.fAlphaType = kPremul_SkAlphaType;
1384 dstPI.fPixels = outPixels;
1385 dstPI.fRowBytes = outRowBytes;
1386
1387 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001388}
1389
bsalomon81beccc2014-10-13 12:32:55 -07001390bool GrContext::writeSurfacePixels(GrSurface* surface,
1391 int left, int top, int width, int height,
1392 GrPixelConfig srcConfig, const void* buffer, size_t rowBytes,
1393 uint32_t pixelOpsFlags) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001394 RETURN_FALSE_IF_ABANDONED
bsalomon81beccc2014-10-13 12:32:55 -07001395 {
1396 GrTexture* texture = NULL;
1397 if (!(kUnpremul_PixelOpsFlag & pixelOpsFlags) && (texture = surface->asTexture()) &&
1398 fGpu->canWriteTexturePixels(texture, srcConfig)) {
1399
1400 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) &&
1401 surface->surfacePriv().hasPendingIO()) {
1402 this->flush();
1403 }
1404 return fGpu->writeTexturePixels(texture, left, top, width, height,
1405 srcConfig, buffer, rowBytes);
1406 // Don't need to check kFlushWrites_PixelOp here, we just did a direct write so the
1407 // upload is already flushed.
1408 }
1409 }
1410
1411 // If we didn't do a direct texture write then we upload the pixels to a texture and draw.
1412 GrRenderTarget* renderTarget = surface->asRenderTarget();
1413 if (NULL == renderTarget) {
1414 return false;
1415 }
1416
1417 // We ignore the preferred config unless it is a R/B swap of the src config. In that case
1418 // we will upload the original src data to a scratch texture but we will spoof it as the swapped
1419 // config. This scratch will then have R and B swapped. We correct for this by swapping again
1420 // when drawing the scratch to the dst using a conversion effect.
1421 bool swapRAndB = false;
1422 GrPixelConfig writeConfig = srcConfig;
1423 if (GrPixelConfigSwapRAndB(srcConfig) ==
1424 fGpu->preferredWritePixelsConfig(srcConfig, renderTarget->config())) {
1425 writeConfig = GrPixelConfigSwapRAndB(srcConfig);
1426 swapRAndB = true;
1427 }
1428
bsalomonf2703d82014-10-28 14:33:06 -07001429 GrSurfaceDesc desc;
bsalomon81beccc2014-10-13 12:32:55 -07001430 desc.fWidth = width;
1431 desc.fHeight = height;
1432 desc.fConfig = writeConfig;
bsalomond309e7a2015-04-30 14:18:54 -07001433 SkAutoTUnref<GrTexture> texture(this->textureProvider()->refScratchTexture(desc,
1434 GrTextureProvider::kApprox_ScratchTexMatch));
bsalomone3059732014-10-14 11:47:22 -07001435 if (!texture) {
bsalomon81beccc2014-10-13 12:32:55 -07001436 return false;
1437 }
1438
1439 SkAutoTUnref<const GrFragmentProcessor> fp;
1440 SkMatrix textureMatrix;
1441 textureMatrix.setIDiv(texture->width(), texture->height());
1442
1443 // allocate a tmp buffer and sw convert the pixels to premul
1444 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
1445
1446 if (kUnpremul_PixelOpsFlag & pixelOpsFlags) {
1447 if (!GrPixelConfigIs8888(srcConfig)) {
1448 return false;
1449 }
1450 fp.reset(this->createUPMToPMEffect(texture, swapRAndB, textureMatrix));
1451 // handle the unpremul step on the CPU if we couldn't create an effect to do it.
1452 if (NULL == fp) {
1453 size_t tmpRowBytes = 4 * width;
1454 tmpPixels.reset(width * height);
1455 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
1456 tmpPixels.get())) {
1457 return false;
1458 }
1459 rowBytes = tmpRowBytes;
1460 buffer = tmpPixels.get();
1461 }
1462 }
1463 if (NULL == fp) {
1464 fp.reset(GrConfigConversionEffect::Create(texture,
1465 swapRAndB,
1466 GrConfigConversionEffect::kNone_PMConversion,
1467 textureMatrix));
1468 }
1469
1470 // Even if the client told us not to flush, we still flush here. The client may have known that
1471 // writes to the original surface caused no data hazards, but they can't know that the scratch
1472 // we just got is safe.
1473 if (texture->surfacePriv().hasPendingIO()) {
1474 this->flush();
1475 }
1476 if (!fGpu->writeTexturePixels(texture, 0, 0, width, height,
1477 writeConfig, buffer, rowBytes)) {
1478 return false;
1479 }
1480
1481 SkMatrix matrix;
1482 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
1483
1484 // This function can be called in the midst of drawing another object (e.g., when uploading a
1485 // SW-rasterized clip while issuing a draw). So we push the current geometry state before
1486 // drawing a rect to the render target.
1487 // The bracket ensures we pop the stack if we wind up flushing below.
1488 {
joshualitt570d2f82015-02-25 13:19:48 -08001489 GrDrawTarget* drawTarget = this->prepareToDraw();
1490 if (!drawTarget) {
1491 return false;
1492 }
joshualitt9853cce2014-11-17 14:22:48 -08001493
egdaniel8dd688b2015-01-22 10:16:09 -08001494 GrPipelineBuilder pipelineBuilder;
1495 pipelineBuilder.addColorProcessor(fp);
1496 pipelineBuilder.setRenderTarget(renderTarget);
joshualitt44701df2015-02-23 14:44:57 -08001497 drawTarget->drawSimpleRect(&pipelineBuilder,
1498 GrColor_WHITE,
1499 matrix,
joshualitt8059eb92014-12-29 15:10:07 -08001500 SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)));
bsalomon81beccc2014-10-13 12:32:55 -07001501 }
1502
1503 if (kFlushWrites_PixelOp & pixelOpsFlags) {
1504 this->flushSurfaceWrites(surface);
1505 }
1506
1507 return true;
1508}
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001509
reed@google.com7111d462014-03-25 16:20:24 +00001510// toggles between RGBA and BGRA
1511static SkColorType toggle_colortype32(SkColorType ct) {
1512 if (kRGBA_8888_SkColorType == ct) {
1513 return kBGRA_8888_SkColorType;
1514 } else {
1515 SkASSERT(kBGRA_8888_SkColorType == ct);
1516 return kRGBA_8888_SkColorType;
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001517 }
1518}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001519
bsalomon@google.com0342a852012-08-20 19:22:38 +00001520bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
1521 int left, int top, int width, int height,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001522 GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001523 uint32_t flags) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001524 RETURN_FALSE_IF_ABANDONED
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001525 ASSERT_OWNED_RESOURCE(target);
bsalomon89c62982014-11-03 12:08:42 -08001526 SkASSERT(target);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001527
bsalomonafbf2d62014-09-30 12:18:44 -07001528 if (!(kDontFlush_PixelOpsFlag & flags) && target->surfacePriv().hasPendingWrite()) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001529 this->flush();
1530 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001531
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001532 // Determine which conversions have to be applied: flipY, swapRAnd, and/or unpremul.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001533
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001534 // If fGpu->readPixels would incur a y-flip cost then we will read the pixels upside down. We'll
1535 // either do the flipY by drawing into a scratch with a matrix or on the cpu after the read.
1536 bool flipY = fGpu->readPixelsWillPayForYFlip(target, left, top,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001537 width, height, dstConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001538 rowBytes);
bsalomon@google.com9c680582013-02-06 18:17:50 +00001539 // We ignore the preferred config if it is different than our config unless it is an R/B swap.
1540 // In that case we'll perform an R and B swap while drawing to a scratch texture of the swapped
1541 // config. Then we will call readPixels on the scratch with the swapped config. The swaps during
1542 // the draw cancels out the fact that we call readPixels with a config that is R/B swapped from
1543 // dstConfig.
1544 GrPixelConfig readConfig = dstConfig;
1545 bool swapRAndB = false;
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +00001546 if (GrPixelConfigSwapRAndB(dstConfig) ==
1547 fGpu->preferredReadPixelsConfig(dstConfig, target->config())) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001548 readConfig = GrPixelConfigSwapRAndB(readConfig);
1549 swapRAndB = true;
1550 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001551
bsalomon@google.com0342a852012-08-20 19:22:38 +00001552 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001553
bsalomon@google.com9c680582013-02-06 18:17:50 +00001554 if (unpremul && !GrPixelConfigIs8888(dstConfig)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001555 // The unpremul flag is only allowed for these two configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001556 return false;
1557 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001558
bsalomon191bcc02014-11-14 11:31:13 -08001559 SkAutoTUnref<GrTexture> tempTexture;
1560
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001561 // If the src is a texture and we would have to do conversions after read pixels, we instead
1562 // do the conversions by drawing the src to a scratch texture. If we handle any of the
1563 // conversions in the draw we set the corresponding bool to false so that we don't reapply it
1564 // on the read back pixels.
1565 GrTexture* src = target->asTexture();
bsalomon49f085d2014-09-05 13:34:00 -07001566 if (src && (swapRAndB || unpremul || flipY)) {
bsalomon81beccc2014-10-13 12:32:55 -07001567 // Make the scratch a render so we can read its pixels.
bsalomonf2703d82014-10-28 14:33:06 -07001568 GrSurfaceDesc desc;
1569 desc.fFlags = kRenderTarget_GrSurfaceFlag;
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001570 desc.fWidth = width;
1571 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001572 desc.fConfig = readConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001573 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001574
bsalomon@google.com9c680582013-02-06 18:17:50 +00001575 // 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 +00001576 // match the passed rect. However, if we see many different size rectangles we will trash
1577 // our texture cache and pay the cost of creating and destroying many textures. So, we only
1578 // request an exact match when the caller is reading an entire RT.
bsalomond309e7a2015-04-30 14:18:54 -07001579 GrTextureProvider::ScratchTexMatch match = GrTextureProvider::kApprox_ScratchTexMatch;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001580 if (0 == left &&
1581 0 == top &&
1582 target->width() == width &&
1583 target->height() == height &&
1584 fGpu->fullReadPixelsIsFasterThanPartial()) {
bsalomond309e7a2015-04-30 14:18:54 -07001585 match = GrTextureProvider::kExact_ScratchTexMatch;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001586 }
bsalomond309e7a2015-04-30 14:18:54 -07001587 tempTexture.reset(this->textureProvider()->refScratchTexture(desc, match));
bsalomon191bcc02014-11-14 11:31:13 -08001588 if (tempTexture) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001589 // compute a matrix to perform the draw
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001590 SkMatrix textureMatrix;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001591 textureMatrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001592 textureMatrix.postIDiv(src->width(), src->height());
1593
joshualittb0a8a372014-09-23 09:50:21 -07001594 SkAutoTUnref<const GrFragmentProcessor> fp;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001595 if (unpremul) {
joshualittb0a8a372014-09-23 09:50:21 -07001596 fp.reset(this->createPMToUPMEffect(src, swapRAndB, textureMatrix));
1597 if (fp) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001598 unpremul = false; // we no longer need to do this on CPU after the read back.
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001599 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001600 }
1601 // If we failed to create a PM->UPM effect and have no other conversions to perform then
1602 // there is no longer any point to using the scratch.
joshualittb0a8a372014-09-23 09:50:21 -07001603 if (fp || flipY || swapRAndB) {
1604 if (!fp) {
1605 fp.reset(GrConfigConversionEffect::Create(
1606 src, swapRAndB, GrConfigConversionEffect::kNone_PMConversion,
1607 textureMatrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001608 }
1609 swapRAndB = false; // we will handle the swap in the draw.
bsalomon@google.comc4364992011-11-07 15:54:49 +00001610
robertphillips@google.com13f181f2013-03-02 12:02:08 +00001611 // We protect the existing geometry here since it may not be
1612 // clear to the caller that a draw operation (i.e., drawSimpleRect)
1613 // can be invoked in this method
joshualitt5c55fef2014-10-31 14:04:35 -07001614 {
egdaniel8dd688b2015-01-22 10:16:09 -08001615 GrPipelineBuilder pipelineBuilder;
joshualitt5c55fef2014-10-31 14:04:35 -07001616 SkASSERT(fp);
egdaniel8dd688b2015-01-22 10:16:09 -08001617 pipelineBuilder.addColorProcessor(fp);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001618
egdaniel8dd688b2015-01-22 10:16:09 -08001619 pipelineBuilder.setRenderTarget(tempTexture->asRenderTarget());
joshualitt5c55fef2014-10-31 14:04:35 -07001620 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
joshualitt44701df2015-02-23 14:44:57 -08001621 fDrawBuffer->drawSimpleRect(&pipelineBuilder,
1622 GrColor_WHITE,
1623 SkMatrix::I(),
egdaniel8dd688b2015-01-22 10:16:09 -08001624 rect);
joshualitt5c55fef2014-10-31 14:04:35 -07001625 // we want to read back from the scratch's origin
1626 left = 0;
1627 top = 0;
bsalomon191bcc02014-11-14 11:31:13 -08001628 target = tempTexture->asRenderTarget();
joshualitt5c55fef2014-10-31 14:04:35 -07001629 }
1630 this->flushSurfaceWrites(target);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001631 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001632 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001633 }
joshualitt5c55fef2014-10-31 14:04:35 -07001634
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001635 if (!fGpu->readPixels(target,
1636 left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001637 readConfig, buffer, rowBytes)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001638 return false;
1639 }
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001640 // Perform any conversions we weren't able to perform using a scratch texture.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001641 if (unpremul || swapRAndB) {
reed@google.com7111d462014-03-25 16:20:24 +00001642 SkDstPixelInfo dstPI;
jvanverthfa1e8a72014-12-22 08:31:49 -08001643 if (!GrPixelConfig2ColorAndProfileType(dstConfig, &dstPI.fColorType, NULL)) {
reed@google.com7111d462014-03-25 16:20:24 +00001644 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001645 }
reed@google.com7111d462014-03-25 16:20:24 +00001646 dstPI.fAlphaType = kUnpremul_SkAlphaType;
1647 dstPI.fPixels = buffer;
1648 dstPI.fRowBytes = rowBytes;
1649
1650 SkSrcPixelInfo srcPI;
1651 srcPI.fColorType = swapRAndB ? toggle_colortype32(dstPI.fColorType) : dstPI.fColorType;
1652 srcPI.fAlphaType = kPremul_SkAlphaType;
1653 srcPI.fPixels = buffer;
1654 srcPI.fRowBytes = rowBytes;
1655
1656 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001657 }
1658 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001659}
1660
bsalomon87a94eb2014-11-03 14:28:32 -08001661void GrContext::prepareSurfaceForExternalRead(GrSurface* surface) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001662 RETURN_IF_ABANDONED
bsalomon87a94eb2014-11-03 14:28:32 -08001663 SkASSERT(surface);
1664 ASSERT_OWNED_RESOURCE(surface);
1665 if (surface->surfacePriv().hasPendingIO()) {
1666 this->flush();
1667 }
1668 GrRenderTarget* rt = surface->asRenderTarget();
1669 if (fGpu && rt) {
1670 fGpu->resolveRenderTarget(rt);
bsalomon41ebbdd2014-08-04 08:31:39 -07001671 }
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001672}
1673
bsalomon41ebbdd2014-08-04 08:31:39 -07001674void GrContext::discardRenderTarget(GrRenderTarget* renderTarget) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001675 RETURN_IF_ABANDONED
bsalomon41ebbdd2014-08-04 08:31:39 -07001676 SkASSERT(renderTarget);
1677 ASSERT_OWNED_RESOURCE(renderTarget);
bsalomonf21dab92014-11-13 13:33:28 -08001678 AutoCheckFlush acf(this);
joshualitt570d2f82015-02-25 13:19:48 -08001679 GrDrawTarget* target = this->prepareToDraw();
bsalomon41ebbdd2014-08-04 08:31:39 -07001680 if (NULL == target) {
1681 return;
1682 }
1683 target->discard(renderTarget);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001684}
1685
bsalomonf80bfed2014-10-07 05:56:02 -07001686void GrContext::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
1687 const SkIPoint& dstPoint, uint32_t pixelOpsFlags) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001688 RETURN_IF_ABANDONED
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001689 if (NULL == src || NULL == dst) {
1690 return;
1691 }
bsalomone3d4bf22014-09-23 09:15:03 -07001692 ASSERT_OWNED_RESOURCE(src);
junov2bb52102014-09-29 10:18:59 -07001693 ASSERT_OWNED_RESOURCE(dst);
Brian Salomon34a98952014-09-24 11:41:24 -04001694
bsalomonf80bfed2014-10-07 05:56:02 -07001695 // Since we're going to the draw target and not GPU, no need to check kNoFlush
1696 // here.
junov96c118e2014-09-26 13:09:47 -07001697
joshualitt570d2f82015-02-25 13:19:48 -08001698 GrDrawTarget* target = this->prepareToDraw();
junov96c118e2014-09-26 13:09:47 -07001699 if (NULL == target) {
1700 return;
1701 }
junov96c118e2014-09-26 13:09:47 -07001702 target->copySurface(dst, src, srcRect, dstPoint);
bsalomonf80bfed2014-10-07 05:56:02 -07001703
1704 if (kFlushWrites_PixelOp & pixelOpsFlags) {
1705 this->flush();
1706 }
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001707}
1708
bsalomonf80bfed2014-10-07 05:56:02 -07001709void GrContext::flushSurfaceWrites(GrSurface* surface) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001710 RETURN_IF_ABANDONED
bsalomonf80bfed2014-10-07 05:56:02 -07001711 if (surface->surfacePriv().hasPendingWrite()) {
1712 this->flush();
1713 }
1714}
1715
egdaniel8dd688b2015-01-22 10:16:09 -08001716GrDrawTarget* GrContext::prepareToDraw(GrPipelineBuilder* pipelineBuilder,
joshualitt25d9c152015-02-18 12:29:52 -08001717 GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -08001718 const GrClip& clip,
joshualitt9853cce2014-11-17 14:22:48 -08001719 const GrPaint* paint,
1720 const AutoCheckFlush* acf) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001721 if (NULL == fGpu || NULL == fDrawBuffer) {
bsalomon41ebbdd2014-08-04 08:31:39 -07001722 return NULL;
1723 }
1724
joshualitt570d2f82015-02-25 13:19:48 -08001725 ASSERT_OWNED_RESOURCE(rt);
1726 SkASSERT(rt && paint && acf);
1727 pipelineBuilder->setFromPaint(*paint, rt, clip);
1728 return fDrawBuffer;
1729}
1730
1731GrDrawTarget* GrContext::prepareToDraw() {
1732 if (NULL == fGpu) {
1733 return NULL;
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001734 }
joshualitt5c55fef2014-10-31 14:04:35 -07001735 return fDrawBuffer;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001736}
1737
robertphillips@google.com72176b22012-05-23 13:19:12 +00001738/*
1739 * This method finds a path renderer that can draw the specified path on
1740 * the provided target.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001741 * Due to its expense, the software path renderer has split out so it can
robertphillips@google.com72176b22012-05-23 13:19:12 +00001742 * can be individually allowed/disallowed via the "allowSW" boolean.
1743 */
joshualitt9853cce2014-11-17 14:22:48 -08001744GrPathRenderer* GrContext::getPathRenderer(const GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -08001745 const GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -08001746 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -08001747 const SkPath& path,
kkinnunen18996512015-04-26 23:18:49 -07001748 const GrStrokeInfo& stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001749 bool allowSW,
1750 GrPathRendererChain::DrawType drawType,
1751 GrPathRendererChain::StencilSupport* stencilSupport) {
1752
bsalomon@google.com30085192011-08-19 15:42:31 +00001753 if (NULL == fPathRendererChain) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001754 fPathRendererChain = SkNEW_ARGS(GrPathRendererChain, (this));
bsalomon@google.com30085192011-08-19 15:42:31 +00001755 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001756
joshualitt9853cce2014-11-17 14:22:48 -08001757 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(target,
egdaniel8dd688b2015-01-22 10:16:09 -08001758 pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -08001759 viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -08001760 path,
sugoi@google.com12b4e272012-12-06 20:13:11 +00001761 stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001762 drawType,
1763 stencilSupport);
robertphillips@google.com72176b22012-05-23 13:19:12 +00001764
1765 if (NULL == pr && allowSW) {
1766 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001767 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001768 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001769 pr = fSoftwarePathRenderer;
1770 }
1771
1772 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001773}
1774
bsalomon@google.com27847de2011-02-22 20:59:41 +00001775////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00001776bool GrContext::isConfigRenderable(GrPixelConfig config, bool withMSAA) const {
1777 return fGpu->caps()->isConfigRenderable(config, withMSAA);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001778}
1779
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +00001780int GrContext::getRecommendedSampleCount(GrPixelConfig config,
1781 SkScalar dpi) const {
1782 if (!this->isConfigRenderable(config, true)) {
1783 return 0;
1784 }
1785 int chosenSampleCount = 0;
jvanverthe9c0fc62015-04-29 11:18:05 -07001786 if (fGpu->caps()->shaderCaps()->pathRenderingSupport()) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +00001787 if (dpi >= 250.0f) {
1788 chosenSampleCount = 4;
1789 } else {
1790 chosenSampleCount = 16;
1791 }
1792 }
1793 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ?
1794 chosenSampleCount : 0;
1795}
1796
bsalomon@google.com21c10c52013-06-13 17:44:07 +00001797GrDrawTarget* GrContext::getTextTarget() {
joshualitt570d2f82015-02-25 13:19:48 -08001798 return this->prepareToDraw();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001799}
1800
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001801namespace {
1802void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
1803 GrConfigConversionEffect::PMConversion pmToUPM;
1804 GrConfigConversionEffect::PMConversion upmToPM;
1805 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
1806 *pmToUPMValue = pmToUPM;
1807 *upmToPMValue = upmToPM;
1808}
1809}
1810
joshualittb0a8a372014-09-23 09:50:21 -07001811const GrFragmentProcessor* GrContext::createPMToUPMEffect(GrTexture* texture,
1812 bool swapRAndB,
1813 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001814 if (!fDidTestPMConversions) {
1815 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001816 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001817 }
1818 GrConfigConversionEffect::PMConversion pmToUPM =
1819 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
1820 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001821 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001822 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001823 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001824 }
1825}
1826
joshualittb0a8a372014-09-23 09:50:21 -07001827const GrFragmentProcessor* GrContext::createUPMToPMEffect(GrTexture* texture,
1828 bool swapRAndB,
1829 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001830 if (!fDidTestPMConversions) {
1831 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001832 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001833 }
1834 GrConfigConversionEffect::PMConversion upmToPM =
1835 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
1836 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001837 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001838 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001839 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001840 }
1841}
1842
bsalomon37f9a262015-02-02 13:00:10 -08001843//////////////////////////////////////////////////////////////////////////////
1844
1845void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
1846 if (maxTextures) {
bsalomon0ea80f42015-02-11 10:49:59 -08001847 *maxTextures = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -08001848 }
1849 if (maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -08001850 *maxTextureBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -08001851 }
1852}
1853
1854void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -08001855 fResourceCache->setLimits(maxTextures, maxTextureBytes);
bsalomon37f9a262015-02-02 13:00:10 -08001856}
1857
bsalomon37f9a262015-02-02 13:00:10 -08001858//////////////////////////////////////////////////////////////////////////////
1859
egdanielbbcb38d2014-06-19 10:19:29 -07001860void GrContext::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
1861 fGpu->addGpuTraceMarker(marker);
bsalomon49f085d2014-09-05 13:34:00 -07001862 if (fDrawBuffer) {
egdanielbbcb38d2014-06-19 10:19:29 -07001863 fDrawBuffer->addGpuTraceMarker(marker);
1864 }
1865}
1866
1867void GrContext::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
1868 fGpu->removeGpuTraceMarker(marker);
bsalomon49f085d2014-09-05 13:34:00 -07001869 if (fDrawBuffer) {
egdanielbbcb38d2014-06-19 10:19:29 -07001870 fDrawBuffer->removeGpuTraceMarker(marker);
1871 }
1872}
joshualitte5902662015-05-11 08:18:35 -07001873
1874///////////////////////////////////////////////////////////////////////////////////////////////////
1875
1876#ifdef GR_TEST_UTILS
1877
joshualitt6c891102015-05-13 08:51:49 -07001878BATCH_TEST_DEFINE(StrokeRectBatch) {
joshualitte5902662015-05-11 08:18:35 -07001879 StrokeRectBatch::Geometry geometry;
1880 geometry.fViewMatrix = GrTest::TestMatrix(random);
1881 geometry.fColor = GrRandomColor(random);
1882 geometry.fRect = GrTest::TestRect(random);
1883 geometry.fStrokeWidth = random->nextBool() ? 0.0f : 1.0f;
1884
joshualitt4c977862015-05-13 08:00:56 -07001885 return StrokeRectBatch::Create(geometry, random->nextBool());
joshualitte5902662015-05-11 08:18:35 -07001886}
1887
1888static uint32_t seed_vertices(GrPrimitiveType type) {
1889 switch (type) {
1890 case kTriangles_GrPrimitiveType:
1891 case kTriangleStrip_GrPrimitiveType:
1892 case kTriangleFan_GrPrimitiveType:
1893 return 3;
1894 case kPoints_GrPrimitiveType:
1895 return 1;
1896 case kLines_GrPrimitiveType:
1897 case kLineStrip_GrPrimitiveType:
1898 return 2;
1899 }
1900 SkFAIL("Incomplete switch\n");
1901 return 0;
1902}
1903
1904static uint32_t primitive_vertices(GrPrimitiveType type) {
1905 switch (type) {
1906 case kTriangles_GrPrimitiveType:
1907 return 3;
1908 case kLines_GrPrimitiveType:
1909 return 2;
1910 case kTriangleStrip_GrPrimitiveType:
1911 case kTriangleFan_GrPrimitiveType:
1912 case kPoints_GrPrimitiveType:
1913 case kLineStrip_GrPrimitiveType:
1914 return 1;
1915 }
1916 SkFAIL("Incomplete switch\n");
1917 return 0;
1918}
1919
1920static SkPoint random_point(SkRandom* random, SkScalar min, SkScalar max) {
1921 SkPoint p;
1922 p.fX = random->nextRangeScalar(min, max);
1923 p.fY = random->nextRangeScalar(min, max);
1924 return p;
1925}
1926
1927static void randomize_params(size_t count, size_t maxVertex, SkScalar min, SkScalar max,
1928 SkRandom* random,
1929 SkTArray<SkPoint>* positions,
1930 SkTArray<SkPoint>* texCoords, bool hasTexCoords,
1931 SkTArray<GrColor>* colors, bool hasColors,
1932 SkTArray<uint16_t>* indices, bool hasIndices) {
1933 for (uint32_t v = 0; v < count; v++) {
1934 positions->push_back(random_point(random, min, max));
1935 if (hasTexCoords) {
1936 texCoords->push_back(random_point(random, min, max));
1937 }
1938 if (hasColors) {
1939 colors->push_back(GrRandomColor(random));
1940 }
1941 if (hasIndices) {
1942 SkASSERT(maxVertex <= SK_MaxU16);
1943 indices->push_back(random->nextULessThan((uint16_t)maxVertex));
1944 }
1945 }
1946}
1947
joshualitt6c891102015-05-13 08:51:49 -07001948BATCH_TEST_DEFINE(VerticesBatch) {
joshualitte5902662015-05-11 08:18:35 -07001949 GrPrimitiveType type = GrPrimitiveType(random->nextULessThan(kLast_GrPrimitiveType + 1));
1950 uint32_t primitiveCount = random->nextRangeU(1, 100);
1951
1952 // TODO make 'sensible' indexbuffers
1953 SkTArray<SkPoint> positions;
1954 SkTArray<SkPoint> texCoords;
1955 SkTArray<GrColor> colors;
1956 SkTArray<uint16_t> indices;
1957
1958 bool hasTexCoords = random->nextBool();
1959 bool hasIndices = random->nextBool();
1960 bool hasColors = random->nextBool();
1961
1962 uint32_t vertexCount = seed_vertices(type) + (primitiveCount - 1) * primitive_vertices(type);
1963
1964 static const SkScalar kMinVertExtent = -100.f;
1965 static const SkScalar kMaxVertExtent = 100.f;
1966 randomize_params(seed_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent,
1967 random,
1968 &positions,
1969 &texCoords, hasTexCoords,
1970 &colors, hasColors,
1971 &indices, hasIndices);
1972
1973 for (uint32_t i = 1; i < primitiveCount; i++) {
1974 randomize_params(primitive_vertices(type), vertexCount, kMinVertExtent, kMaxVertExtent,
1975 random,
1976 &positions,
1977 &texCoords, hasTexCoords,
1978 &colors, hasColors,
1979 &indices, hasIndices);
1980 }
1981
1982 SkMatrix viewMatrix = GrTest::TestMatrix(random);
1983 SkRect bounds;
1984 SkDEBUGCODE(bool result = ) bounds.setBoundsCheck(positions.begin(), vertexCount);
1985 SkASSERT(result);
1986
1987 viewMatrix.mapRect(&bounds);
1988
1989 DrawVerticesBatch::Geometry geometry;
1990 geometry.fColor = GrRandomColor(random);
1991 return DrawVerticesBatch::Create(geometry, type, viewMatrix,
1992 positions.begin(), vertexCount,
1993 indices.begin(), hasIndices ? vertexCount : 0,
1994 colors.begin(),
1995 texCoords.begin(),
1996 bounds);
1997}
1998
1999#endif