blob: c9342a5e6a75aa564c2ef4bfe53eac573bce8bd0 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
bsalomon@google.com27847de2011-02-22 20:59:41 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
bsalomon@google.com27847de2011-02-22 20:59:41 +00007 */
8
bsalomon@google.com1fadb202011-12-12 16:10:08 +00009#include "GrContext.h"
10
jvanverth@google.combfe2b9d2013-09-06 16:57:29 +000011#include "GrAARectRenderer.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070012#include "GrAtlasTextContext.h"
joshualitt0823bfa2015-02-27 10:06:15 -080013#include "GrBatch.h"
joshualitt7c3a2f82015-03-31 13:32:05 -070014#include "GrBatchFontCache.h"
joshualitt0823bfa2015-02-27 10:06:15 -080015#include "GrBatchTarget.h"
tomhudson@google.com278cbb42011-06-30 19:37:01 +000016#include "GrBufferAllocPool.h"
joshualitt5478d422014-11-14 16:00:38 -080017#include "GrDefaultGeoProcFactory.h"
bsalomon453cf402014-11-11 14:15:57 -080018#include "GrGpuResource.h"
bsalomon3582d3e2015-02-13 14:20:05 -080019#include "GrGpuResourcePriv.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000020#include "GrDrawTargetCaps.h"
joshualitt5478d422014-11-14 16:00:38 -080021#include "GrGpu.h"
commit-bot@chromium.orgdcb8ef92014-03-27 11:26:10 +000022#include "GrIndexBuffer.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000023#include "GrInOrderDrawBuffer.h"
robertphillips@google.come930a072014-04-03 00:34:27 +000024#include "GrLayerCache.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +000025#include "GrOvalRenderer.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000026#include "GrPathRenderer.h"
tomhudson@google.comd22b6e42011-06-24 15:53:40 +000027#include "GrPathUtils.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080028#include "GrRenderTargetPriv.h"
bsalomon0ea80f42015-02-11 10:49:59 -080029#include "GrResourceCache.h"
bsalomond309e7a2015-04-30 14:18:54 -070030#include "GrResourceProvider.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000031#include "GrSoftwarePathRenderer.h"
kkinnunenc6cb56f2014-06-24 00:12:27 -070032#include "GrStencilAndCoverTextContext.h"
egdanield58a0ba2014-06-11 10:30:05 -070033#include "GrStrokeInfo.h"
bsalomonafbf2d62014-09-30 12:18:44 -070034#include "GrSurfacePriv.h"
joshualittb7133be2015-04-08 09:08:31 -070035#include "GrTextBlobCache.h"
bsalomonafbf2d62014-09-30 12:18:44 -070036#include "GrTexturePriv.h"
egdanielbbcb38d2014-06-19 10:19:29 -070037#include "GrTraceMarker.h"
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +000038#include "GrTracing.h"
bsalomoncb8979d2015-05-05 09:51:38 -070039#include "GrVertices.h"
egdanield58a0ba2014-06-11 10:30:05 -070040#include "SkDashPathPriv.h"
bsalomon81beccc2014-10-13 12:32:55 -070041#include "SkConfig8888.h"
reed@google.com7111d462014-03-25 16:20:24 +000042#include "SkGr.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000043#include "SkRRect.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000044#include "SkStrokeRec.h"
commit-bot@chromium.orgdcb8ef92014-03-27 11:26:10 +000045#include "SkTLazy.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000046#include "SkTLS.h"
commit-bot@chromium.org933e65d2014-03-20 20:00:24 +000047#include "SkTraceEvent.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000048
joshualitt5478d422014-11-14 16:00:38 -080049#include "effects/GrConfigConversionEffect.h"
50#include "effects/GrDashingEffect.h"
51#include "effects/GrSingleTextureEffect.h"
52
bsalomon@google.com60361492012-03-15 17:47:06 +000053static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
bsalomon@google.com27847de2011-02-22 20:59:41 +000054static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
55
bsalomon@google.com1d4edd32012-08-16 18:36:06 +000056static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = 1 << 11;
57static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = 4;
bsalomon@google.com27847de2011-02-22 20:59:41 +000058
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000059#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
joshualitt5f5a8d72015-02-25 14:09:45 -080060#define RETURN_IF_ABANDONED if (!fDrawBuffer) { return; }
61#define RETURN_FALSE_IF_ABANDONED if (!fDrawBuffer) { return false; }
62#define RETURN_NULL_IF_ABANDONED if (!fDrawBuffer) { return NULL; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000063
bsalomonf21dab92014-11-13 13:33:28 -080064class GrContext::AutoCheckFlush {
65public:
66 AutoCheckFlush(GrContext* context) : fContext(context) { SkASSERT(context); }
67
68 ~AutoCheckFlush() {
69 if (fContext->fFlushToReduceCacheSize) {
70 fContext->flush();
71 }
72 }
73
74private:
75 GrContext* fContext;
76};
77
krajcevski9c6d4d72014-08-12 07:26:25 -070078GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
79 const Options* opts) {
80 GrContext* context;
81 if (NULL == opts) {
82 context = SkNEW_ARGS(GrContext, (Options()));
83 } else {
84 context = SkNEW_ARGS(GrContext, (*opts));
85 }
86
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000087 if (context->init(backend, backendContext)) {
88 return context;
89 } else {
90 context->unref();
91 return NULL;
bsalomon@google.com27847de2011-02-22 20:59:41 +000092 }
bsalomon@google.com27847de2011-02-22 20:59:41 +000093}
94
joshualitt0acd0d32015-05-07 08:23:19 -070095static int32_t gNextID = 1;
96static int32_t next_id() {
97 int32_t id;
98 do {
99 id = sk_atomic_inc(&gNextID);
100 } while (id == SK_InvalidGenID);
101 return id;
102}
103
104GrContext::GrContext(const Options& opts) : fOptions(opts), fUniqueID(next_id()) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000105 fGpu = NULL;
bsalomond309e7a2015-04-30 14:18:54 -0700106 fResourceCache = NULL;
107 fResourceProvider = NULL;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000108 fPathRendererChain = NULL;
109 fSoftwarePathRenderer = NULL;
joshualitt7c3a2f82015-03-31 13:32:05 -0700110 fBatchFontCache = NULL;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000111 fDrawBuffer = NULL;
112 fDrawBufferVBAllocPool = NULL;
113 fDrawBufferIBAllocPool = NULL;
bsalomonf21dab92014-11-13 13:33:28 -0800114 fFlushToReduceCacheSize = false;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000115 fAARectRenderer = NULL;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000116 fOvalRenderer = NULL;
robertphillips@google.com44a91dc2013-07-25 15:32:06 +0000117 fMaxTextureSizeOverride = 1 << 20;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000118}
119
120bool GrContext::init(GrBackend backend, GrBackendContext backendContext) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000121 SkASSERT(NULL == fGpu);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000122
123 fGpu = GrGpu::Create(backend, backendContext, this);
124 if (NULL == fGpu) {
125 return false;
126 }
bsalomon33435572014-11-05 14:47:41 -0800127 this->initCommon();
128 return true;
129}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000130
bsalomon33435572014-11-05 14:47:41 -0800131void GrContext::initCommon() {
bsalomon0ea80f42015-02-11 10:49:59 -0800132 fResourceCache = SkNEW(GrResourceCache);
133 fResourceCache->setOverBudgetCallback(OverBudgetCB, this);
bsalomond309e7a2015-04-30 14:18:54 -0700134 fResourceProvider = SkNEW_ARGS(GrResourceProvider, (fGpu, fResourceCache));
commit-bot@chromium.org1836d332013-07-16 22:55:03 +0000135
robertphillips4ec84da2014-06-24 13:10:43 -0700136 fLayerCache.reset(SkNEW_ARGS(GrLayerCache, (this)));
robertphillips@google.come930a072014-04-03 00:34:27 +0000137
bsalomoned0bcad2015-05-04 10:36:42 -0700138 fAARectRenderer = SkNEW(GrAARectRenderer);
139 fOvalRenderer = SkNEW(GrOvalRenderer);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000140
141 fDidTestPMConversions = false;
142
143 this->setupDrawBuffer();
joshualitt7c3a2f82015-03-31 13:32:05 -0700144
145 // GrBatchFontCache will eventually replace GrFontCache
joshualitt62db8ba2015-04-09 08:22:37 -0700146 fBatchFontCache = SkNEW_ARGS(GrBatchFontCache, (this));
joshualittb7133be2015-04-08 09:08:31 -0700147
joshualitt0db6dfa2015-04-10 07:01:30 -0700148 fTextBlobCache.reset(SkNEW_ARGS(GrTextBlobCache, (TextBlobCacheOverBudgetCB, this)));
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000149}
150
bsalomon@google.com27847de2011-02-22 20:59:41 +0000151GrContext::~GrContext() {
bsalomon@google.com733c0622013-04-24 17:59:32 +0000152 if (NULL == fGpu) {
153 return;
154 }
155
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000156 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000157
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000158 for (int i = 0; i < fCleanUpData.count(); ++i) {
159 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
160 }
161
bsalomond309e7a2015-04-30 14:18:54 -0700162 SkDELETE(fResourceProvider);
bsalomon0ea80f42015-02-11 10:49:59 -0800163 SkDELETE(fResourceCache);
joshualitt7c3a2f82015-03-31 13:32:05 -0700164 SkDELETE(fBatchFontCache);
bsalomon33435572014-11-05 14:47:41 -0800165 SkDELETE(fDrawBuffer);
166 SkDELETE(fDrawBufferVBAllocPool);
167 SkDELETE(fDrawBufferIBAllocPool);
bsalomon@google.com30085192011-08-19 15:42:31 +0000168
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000169 fAARectRenderer->unref();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000170 fOvalRenderer->unref();
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000171
bsalomon@google.com205d4602011-04-25 12:43:45 +0000172 fGpu->unref();
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000173 SkSafeUnref(fPathRendererChain);
174 SkSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000175}
176
bsalomon2354f842014-07-28 13:48:36 -0700177void GrContext::abandonContext() {
bsalomond309e7a2015-04-30 14:18:54 -0700178 fResourceProvider->abandon();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000179 // abandon first to so destructors
180 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800181 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700182
robertphillipse3371302014-09-17 06:01:06 -0700183 fGpu->contextAbandoned();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000184
bsalomon@google.com30085192011-08-19 15:42:31 +0000185 // a path renderer may be holding onto resources that
186 // are now unusable
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000187 SkSafeSetNull(fPathRendererChain);
188 SkSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000189
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000190 delete fDrawBuffer;
191 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000192
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000193 delete fDrawBufferVBAllocPool;
194 fDrawBufferVBAllocPool = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000195
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000196 delete fDrawBufferIBAllocPool;
197 fDrawBufferIBAllocPool = NULL;
198
joshualitt7c3a2f82015-03-31 13:32:05 -0700199 fBatchFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000200 fLayerCache->freeAll();
joshualitt26ffc002015-04-16 11:24:04 -0700201 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000202}
203
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000204void GrContext::resetContext(uint32_t state) {
205 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000206}
207
208void GrContext::freeGpuResources() {
209 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000210
bsalomon49f085d2014-09-05 13:34:00 -0700211 if (fDrawBuffer) {
bsalomonc8dc1f72014-08-21 13:02:13 -0700212 fDrawBuffer->purgeResources();
213 }
robertphillips@google.comff175842012-05-14 19:31:39 +0000214
joshualitt7c3a2f82015-03-31 13:32:05 -0700215 fBatchFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000216 fLayerCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000217 // a path renderer may be holding onto resources
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000218 SkSafeSetNull(fPathRendererChain);
219 SkSafeSetNull(fSoftwarePathRenderer);
bsalomon3033b9f2015-04-13 11:09:56 -0700220
221 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000222}
223
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000224void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
bsalomon71cb0c22014-11-14 12:10:14 -0800225 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800226 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800227 }
228 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800229 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800230 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000231}
232
kkinnunenc6cb56f2014-06-24 00:12:27 -0700233GrTextContext* GrContext::createTextContext(GrRenderTarget* renderTarget,
joshualitt6e8cd962015-03-20 10:30:14 -0700234 SkGpuDevice* gpuDevice,
kkinnunenc6cb56f2014-06-24 00:12:27 -0700235 const SkDeviceProperties&
236 leakyProperties,
237 bool enableDistanceFieldFonts) {
jvanverthe9c0fc62015-04-29 11:18:05 -0700238 if (fGpu->caps()->shaderCaps()->pathRenderingSupport() && renderTarget->isMultisampled()) {
egdaniel8dc7c3a2015-04-16 11:22:42 -0700239 GrStencilAttachment* sb = renderTarget->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800240 if (sb) {
joshualitt6e8cd962015-03-20 10:30:14 -0700241 return GrStencilAndCoverTextContext::Create(this, gpuDevice, leakyProperties);
bsalomon6bc1b5f2015-02-23 09:06:38 -0800242 }
jvanverth8c27a182014-10-14 08:45:50 -0700243 }
244
joshualitt9bd2daf2015-04-17 09:30:06 -0700245 return GrAtlasTextContext::Create(this, gpuDevice, leakyProperties, enableDistanceFieldFonts);
kkinnunenc6cb56f2014-06-24 00:12:27 -0700246}
247
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000248////////////////////////////////////////////////////////////////////////////////
249
bsalomond2a6f4e2015-02-04 10:55:54 -0800250bool GrContext::isConfigTexturable(GrPixelConfig config) const {
251 return fGpu->caps()->isConfigTexturable(config);
252}
253
254bool GrContext::npotTextureTileSupport() const {
255 return fGpu->caps()->npotTextureTileSupport();
256}
257
bsalomon71cb0c22014-11-14 12:10:14 -0800258void GrContext::OverBudgetCB(void* data) {
bsalomon66a450f2014-11-13 13:19:10 -0800259 SkASSERT(data);
bsalomonf21dab92014-11-13 13:33:28 -0800260
bsalomon66a450f2014-11-13 13:19:10 -0800261 GrContext* context = reinterpret_cast<GrContext*>(data);
bsalomonf21dab92014-11-13 13:33:28 -0800262
263 // Flush the InOrderDrawBuffer to possibly free up some textures
264 context->fFlushToReduceCacheSize = true;
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000265}
266
joshualitt0db6dfa2015-04-10 07:01:30 -0700267void GrContext::TextBlobCacheOverBudgetCB(void* data) {
268 SkASSERT(data);
269
270 // Unlike the GrResourceCache, TextBlobs are drawn at the SkGpuDevice level, therefore they
271 // cannot use fFlushTorReduceCacheSize because it uses AutoCheckFlush. The solution is to move
272 // drawText calls to below the GrContext level, but this is not trivial because they call
273 // drawPath on SkGpuDevice
274 GrContext* context = reinterpret_cast<GrContext*>(data);
275 context->flush();
276}
277
bsalomon@google.com91958362011-06-13 17:58:13 +0000278int GrContext::getMaxTextureSize() const {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000279 return SkTMin(fGpu->caps()->maxTextureSize(), fMaxTextureSizeOverride);
bsalomon@google.com91958362011-06-13 17:58:13 +0000280}
281
282int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000283 return fGpu->caps()->maxRenderTargetSize();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000284}
285
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000286int GrContext::getMaxSampleCount() const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000287 return fGpu->caps()->maxSampleCount();
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000288}
289
bsalomon@google.com27847de2011-02-22 20:59:41 +0000290///////////////////////////////////////////////////////////////////////////////
291
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000292void GrContext::clear(const SkIRect* rect,
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000293 const GrColor color,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000294 bool canIgnoreRect,
bsalomon41ebbdd2014-08-04 08:31:39 -0700295 GrRenderTarget* renderTarget) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800296 RETURN_IF_ABANDONED
bsalomon41ebbdd2014-08-04 08:31:39 -0700297 ASSERT_OWNED_RESOURCE(renderTarget);
bsalomon89c62982014-11-03 12:08:42 -0800298 SkASSERT(renderTarget);
299
bsalomonf21dab92014-11-13 13:33:28 -0800300 AutoCheckFlush acf(this);
egdanield78a1682014-07-09 10:41:26 -0700301 GR_CREATE_TRACE_MARKER_CONTEXT("GrContext::clear", this);
joshualitt570d2f82015-02-25 13:19:48 -0800302 GrDrawTarget* target = this->prepareToDraw();
bsalomon41ebbdd2014-08-04 08:31:39 -0700303 if (NULL == target) {
304 return;
305 }
306 target->clear(rect, color, canIgnoreRect, renderTarget);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000307}
308
joshualitt25d9c152015-02-18 12:29:52 -0800309void GrContext::drawPaint(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800310 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800311 const GrPaint& origPaint,
312 const SkMatrix& viewMatrix) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800313 RETURN_IF_ABANDONED
bsalomon@google.com27847de2011-02-22 20:59:41 +0000314 // set rect to be big enough to fill the space, but not super-huge, so we
315 // don't overflow fixed-point implementations
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000316 SkRect r;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000317 r.setLTRB(0, 0,
joshualitt25d9c152015-02-18 12:29:52 -0800318 SkIntToScalar(rt->width()),
319 SkIntToScalar(rt->height()));
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000320 SkTCopyOnFirstWrite<GrPaint> paint(origPaint);
joshualitt8fc6c2d2014-12-22 15:27:05 -0800321
322 // by definition this fills the entire clip, no need for AA
323 if (paint->isAntiAlias()) {
324 paint.writable()->setAntiAlias(false);
325 }
326
327 bool isPerspective = viewMatrix.hasPerspective();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000328
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000329 // We attempt to map r by the inverse matrix and draw that. mapRect will
330 // map the four corners and bound them with a new rect. This will not
331 // produce a correct result for some perspective matrices.
joshualitt8fc6c2d2014-12-22 15:27:05 -0800332 if (!isPerspective) {
joshualitt5531d512014-12-17 15:50:11 -0800333 SkMatrix inverse;
334 if (!viewMatrix.invert(&inverse)) {
tfarina38406c82014-10-31 07:11:12 -0700335 SkDebugf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000336 return;
337 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000338 inverse.mapRect(&r);
joshualitt570d2f82015-02-25 13:19:48 -0800339 this->drawRect(rt, clip, *paint, viewMatrix, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000340 } else {
joshualitt8fc6c2d2014-12-22 15:27:05 -0800341 SkMatrix localMatrix;
342 if (!viewMatrix.invert(&localMatrix)) {
tfarina38406c82014-10-31 07:11:12 -0700343 SkDebugf("Could not invert matrix\n");
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000344 return;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000345 }
joshualitt8fc6c2d2014-12-22 15:27:05 -0800346
347 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -0800348 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -0800349 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, paint, &acf);
joshualitt8fc6c2d2014-12-22 15:27:05 -0800350 if (NULL == target) {
351 return;
352 }
353
354 GR_CREATE_TRACE_MARKER("GrContext::drawPaintWithPerspective", target);
joshualitt44701df2015-02-23 14:44:57 -0800355 target->drawRect(&pipelineBuilder,
356 paint->getColor(),
357 SkMatrix::I(),
358 r,
359 NULL,
360 &localMatrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000361 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000362}
363
bsalomon@google.com205d4602011-04-25 12:43:45 +0000364////////////////////////////////////////////////////////////////////////////////
365
bsalomonc30aaa02014-08-13 07:15:29 -0700366static inline bool is_irect(const SkRect& r) {
tfarina38406c82014-10-31 07:11:12 -0700367 return SkScalarIsInt(r.fLeft) && SkScalarIsInt(r.fTop) &&
368 SkScalarIsInt(r.fRight) && SkScalarIsInt(r.fBottom);
bsalomonc30aaa02014-08-13 07:15:29 -0700369}
370
bsalomon@google.com205d4602011-04-25 12:43:45 +0000371static bool apply_aa_to_rect(GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -0800372 GrPipelineBuilder* pipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -0800373 SkRect* devBoundRect,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000374 const SkRect& rect,
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000375 SkScalar strokeWidth,
joshualitt2e3b3e32014-12-09 13:31:14 -0800376 const SkMatrix& combinedMatrix,
377 GrColor color) {
egdaniel8dd688b2015-01-22 10:16:09 -0800378 if (pipelineBuilder->getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000379 return false;
380 }
381
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000382#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000383 if (strokeWidth >= 0) {
384#endif
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000385 if (!combinedMatrix.preservesAxisAlignment()) {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000386 return false;
387 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000388
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000389#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000390 } else {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000391 if (!combinedMatrix.preservesRightAngles()) {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000392 return false;
393 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000394 }
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000395#endif
bsalomon@google.com205d4602011-04-25 12:43:45 +0000396
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000397 combinedMatrix.mapRect(devBoundRect, rect);
derekfe638d1c2014-12-02 13:51:29 -0800398 if (!combinedMatrix.rectStaysRect()) {
399 return true;
400 }
401
bsalomonc30aaa02014-08-13 07:15:29 -0700402 if (strokeWidth < 0) {
403 return !is_irect(*devBoundRect);
404 }
robertphillips@google.com28ac96e2013-05-13 13:38:35 +0000405
bsalomon9c0822a2014-08-11 11:07:48 -0700406 return true;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000407}
408
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000409static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) {
410 return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
411 point.fY >= rect.fTop && point.fY <= rect.fBottom;
412}
413
joshualitt0823bfa2015-02-27 10:06:15 -0800414class StrokeRectBatch : public GrBatch {
415public:
416 struct Geometry {
417 GrColor fColor;
418 SkMatrix fViewMatrix;
419 SkRect fRect;
420 SkScalar fStrokeWidth;
421 };
422
423 static GrBatch* Create(const Geometry& geometry) {
424 return SkNEW_ARGS(StrokeRectBatch, (geometry));
425 }
426
mtklein36352bf2015-03-25 18:17:31 -0700427 const char* name() const override { return "StrokeRectBatch"; }
joshualitt0823bfa2015-02-27 10:06:15 -0800428
mtklein36352bf2015-03-25 18:17:31 -0700429 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt0823bfa2015-02-27 10:06:15 -0800430 // When this is called on a batch, there is only one geometry bundle
431 out->setKnownFourComponents(fGeoData[0].fColor);
432 }
433
mtklein36352bf2015-03-25 18:17:31 -0700434 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualitt0823bfa2015-02-27 10:06:15 -0800435 out->setKnownSingleComponent(0xff);
436 }
437
mtklein36352bf2015-03-25 18:17:31 -0700438 void initBatchTracker(const GrPipelineInfo& init) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800439 // Handle any color overrides
440 if (init.fColorIgnored) {
441 fGeoData[0].fColor = GrColor_ILLEGAL;
442 } else if (GrColor_ILLEGAL != init.fOverrideColor) {
443 fGeoData[0].fColor = init.fOverrideColor;
444 }
445
446 // setup batch properties
447 fBatch.fColorIgnored = init.fColorIgnored;
448 fBatch.fColor = fGeoData[0].fColor;
449 fBatch.fUsesLocalCoords = init.fUsesLocalCoords;
450 fBatch.fCoverageIgnored = init.fCoverageIgnored;
451 }
452
mtklein36352bf2015-03-25 18:17:31 -0700453 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800454 SkAutoTUnref<const GrGeometryProcessor> gp(
455 GrDefaultGeoProcFactory::Create(GrDefaultGeoProcFactory::kPosition_GPType,
456 this->color(),
457 this->viewMatrix(),
458 SkMatrix::I()));
459
460 batchTarget->initDraw(gp, pipeline);
461
462 // TODO this is hacky, but the only way we have to initialize the GP is to use the
463 // GrPipelineInfo struct so we can generate the correct shader. Once we have GrBatch
464 // everywhere we can remove this nastiness
465 GrPipelineInfo init;
466 init.fColorIgnored = fBatch.fColorIgnored;
467 init.fOverrideColor = GrColor_ILLEGAL;
468 init.fCoverageIgnored = fBatch.fCoverageIgnored;
469 init.fUsesLocalCoords = this->usesLocalCoords();
470 gp->initBatchTracker(batchTarget->currentBatchTracker(), init);
471
472 size_t vertexStride = gp->getVertexStride();
473
474 SkASSERT(vertexStride == sizeof(GrDefaultGeoProcFactory::PositionAttr));
475
476 Geometry& args = fGeoData[0];
477
478 int vertexCount = kVertsPerHairlineRect;
479 if (args.fStrokeWidth > 0) {
480 vertexCount = kVertsPerStrokeRect;
481 }
482
483 const GrVertexBuffer* vertexBuffer;
484 int firstVertex;
485
bsalomoncb8979d2015-05-05 09:51:38 -0700486 void* verts = batchTarget->vertexPool()->makeSpace(vertexStride,
487 vertexCount,
488 &vertexBuffer,
489 &firstVertex);
joshualitt0823bfa2015-02-27 10:06:15 -0800490
bsalomoncb8979d2015-05-05 09:51:38 -0700491 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -0800492 SkDebugf("Could not allocate vertices\n");
493 return;
494 }
495
bsalomoncb8979d2015-05-05 09:51:38 -0700496 SkPoint* vertex = reinterpret_cast<SkPoint*>(verts);
joshualitt0823bfa2015-02-27 10:06:15 -0800497
498 GrPrimitiveType primType;
499
500 if (args.fStrokeWidth > 0) {;
501 primType = kTriangleStrip_GrPrimitiveType;
502 args.fRect.sort();
503 this->setStrokeRectStrip(vertex, args.fRect, args.fStrokeWidth);
504 } else {
505 // hairline
506 primType = kLineStrip_GrPrimitiveType;
507 vertex[0].set(args.fRect.fLeft, args.fRect.fTop);
508 vertex[1].set(args.fRect.fRight, args.fRect.fTop);
509 vertex[2].set(args.fRect.fRight, args.fRect.fBottom);
510 vertex[3].set(args.fRect.fLeft, args.fRect.fBottom);
511 vertex[4].set(args.fRect.fLeft, args.fRect.fTop);
512 }
513
bsalomoncb8979d2015-05-05 09:51:38 -0700514 GrVertices vertices;
515 vertices.init(primType, vertexBuffer, firstVertex, vertexCount);
516 batchTarget->draw(vertices);
joshualitt0823bfa2015-02-27 10:06:15 -0800517 }
518
519 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
520
521private:
522 StrokeRectBatch(const Geometry& geometry) {
523 this->initClassID<StrokeRectBatch>();
524
525 fBatch.fHairline = geometry.fStrokeWidth == 0;
526
527 fGeoData.push_back(geometry);
joshualitt99c7c072015-05-01 13:43:30 -0700528
529 // setup bounds
530 fBounds = geometry.fRect;
531 SkScalar rad = SkScalarHalf(geometry.fStrokeWidth);
532 fBounds.outset(rad, rad);
533 geometry.fViewMatrix.mapRect(&fBounds);
joshualitt0823bfa2015-02-27 10:06:15 -0800534 }
535
536 /* create a triangle strip that strokes the specified rect. There are 8
537 unique vertices, but we repeat the last 2 to close up. Alternatively we
538 could use an indices array, and then only send 8 verts, but not sure that
539 would be faster.
540 */
541 void setStrokeRectStrip(SkPoint verts[10], const SkRect& rect, SkScalar width) {
542 const SkScalar rad = SkScalarHalf(width);
543 // TODO we should be able to enable this assert, but we'd have to filter these draws
544 // this is a bug
545 //SkASSERT(rad < rect.width() / 2 && rad < rect.height() / 2);
546
547 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
548 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
549 verts[2].set(rect.fRight - rad, rect.fTop + rad);
550 verts[3].set(rect.fRight + rad, rect.fTop - rad);
551 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
552 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
553 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
554 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
555 verts[8] = verts[0];
556 verts[9] = verts[1];
557 }
558
559
560 GrColor color() const { return fBatch.fColor; }
561 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
562 bool colorIgnored() const { return fBatch.fColorIgnored; }
563 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
564 bool hairline() const { return fBatch.fHairline; }
565
mtklein36352bf2015-03-25 18:17:31 -0700566 bool onCombineIfPossible(GrBatch* t) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800567 // StrokeRectBatch* that = t->cast<StrokeRectBatch>();
568
569 // NonAA stroke rects cannot batch right now
570 // TODO make these batchable
571 return false;
572 }
573
574 struct BatchTracker {
575 GrColor fColor;
576 bool fUsesLocalCoords;
577 bool fColorIgnored;
578 bool fCoverageIgnored;
579 bool fHairline;
580 };
581
582 const static int kVertsPerHairlineRect = 5;
583 const static int kVertsPerStrokeRect = 10;
584
585 BatchTracker fBatch;
586 SkSTArray<1, Geometry, true> fGeoData;
587};
588
joshualitt25d9c152015-02-18 12:29:52 -0800589void GrContext::drawRect(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800590 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800591 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -0800592 const SkMatrix& viewMatrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000593 const SkRect& rect,
bsalomon01c8da12014-08-04 09:21:30 -0700594 const GrStrokeInfo* strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800595 RETURN_IF_ABANDONED
bsalomon49f085d2014-09-05 13:34:00 -0700596 if (strokeInfo && strokeInfo->isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700597 SkPath path;
598 path.addRect(rect);
joshualitt570d2f82015-02-25 13:19:48 -0800599 this->drawPath(rt, clip, paint, viewMatrix, path, *strokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -0700600 return;
601 }
602
bsalomonf21dab92014-11-13 13:33:28 -0800603 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -0800604 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -0800605 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700606 if (NULL == target) {
607 return;
608 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000609
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000610 GR_CREATE_TRACE_MARKER("GrContext::drawRect", target);
egdanield58a0ba2014-06-11 10:30:05 -0700611 SkScalar width = NULL == strokeInfo ? -1 : strokeInfo->getStrokeRec().getWidth();
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000612
613 // Check if this is a full RT draw and can be replaced with a clear. We don't bother checking
614 // cases where the RT is fully inside a stroke.
615 if (width < 0) {
616 SkRect rtRect;
egdaniel8dd688b2015-01-22 10:16:09 -0800617 pipelineBuilder.getRenderTarget()->getBoundsRect(&rtRect);
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000618 SkRect clipSpaceRTRect = rtRect;
joshualitt570d2f82015-02-25 13:19:48 -0800619 bool checkClip = GrClip::kWideOpen_ClipType != clip.clipType();
joshualitt44701df2015-02-23 14:44:57 -0800620 if (checkClip) {
joshualitt570d2f82015-02-25 13:19:48 -0800621 clipSpaceRTRect.offset(SkIntToScalar(clip.origin().fX),
622 SkIntToScalar(clip.origin().fY));
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000623 }
624 // Does the clip contain the entire RT?
joshualitt570d2f82015-02-25 13:19:48 -0800625 if (!checkClip || clip.quickContains(clipSpaceRTRect)) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000626 SkMatrix invM;
joshualitt8059eb92014-12-29 15:10:07 -0800627 if (!viewMatrix.invert(&invM)) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000628 return;
629 }
630 // Does the rect bound the RT?
631 SkPoint srcSpaceRTQuad[4];
632 invM.mapRectToQuad(srcSpaceRTQuad, rtRect);
633 if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) &&
634 rect_contains_inclusive(rect, srcSpaceRTQuad[1]) &&
635 rect_contains_inclusive(rect, srcSpaceRTQuad[2]) &&
636 rect_contains_inclusive(rect, srcSpaceRTQuad[3])) {
637 // Will it blend?
638 GrColor clearColor;
639 if (paint.isOpaqueAndConstantColor(&clearColor)) {
joshualitt25d9c152015-02-18 12:29:52 -0800640 target->clear(NULL, clearColor, true, rt);
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000641 return;
642 }
643 }
644 }
645 }
646
joshualitt2e3b3e32014-12-09 13:31:14 -0800647 GrColor color = paint.getColor();
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000648 SkRect devBoundRect;
egdaniel8dd688b2015-01-22 10:16:09 -0800649 bool needAA = paint.isAntiAlias() && !pipelineBuilder.getRenderTarget()->isMultisampled();
650 bool doAA = needAA && apply_aa_to_rect(target, &pipelineBuilder, &devBoundRect, rect, width,
joshualitt8059eb92014-12-29 15:10:07 -0800651 viewMatrix, color);
egdanield58a0ba2014-06-11 10:30:05 -0700652
bsalomon@google.com205d4602011-04-25 12:43:45 +0000653 if (doAA) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000654 if (width >= 0) {
bsalomon395ef052014-11-12 11:35:22 -0800655 const SkStrokeRec& strokeRec = strokeInfo->getStrokeRec();
joshualitt9853cce2014-11-17 14:22:48 -0800656 fAARectRenderer->strokeAARect(target,
egdaniel8dd688b2015-01-22 10:16:09 -0800657 &pipelineBuilder,
joshualitt2e3b3e32014-12-09 13:31:14 -0800658 color,
joshualitt8059eb92014-12-29 15:10:07 -0800659 viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800660 rect,
joshualitt9853cce2014-11-17 14:22:48 -0800661 devBoundRect,
662 strokeRec);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000663 } else {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000664 // filled AA rect
joshualitt8059eb92014-12-29 15:10:07 -0800665 fAARectRenderer->fillAARect(target,
egdaniel8dd688b2015-01-22 10:16:09 -0800666 &pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800667 color,
668 viewMatrix,
669 rect,
joshualittd27f73e2014-12-29 07:43:36 -0800670 devBoundRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000671 }
672 return;
673 }
674
bsalomon@google.com27847de2011-02-22 20:59:41 +0000675 if (width >= 0) {
joshualitt0823bfa2015-02-27 10:06:15 -0800676 StrokeRectBatch::Geometry geometry;
677 geometry.fViewMatrix = viewMatrix;
678 geometry.fColor = color;
679 geometry.fRect = rect;
680 geometry.fStrokeWidth = width;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000681
joshualitt0823bfa2015-02-27 10:06:15 -0800682 SkAutoTUnref<GrBatch> batch(StrokeRectBatch::Create(geometry));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000683
bsalomond79c5492015-04-27 10:07:04 -0700684 // Depending on sub-pixel coordinates and the particular GPU, we may lose a corner of
685 // hairline rects. We jam all the vertices to pixel centers to avoid this, but not when MSAA
686 // is enabled because it can cause ugly artifacts.
687 pipelineBuilder.setState(GrPipelineBuilder::kSnapVerticesToPixelCenters_Flag,
688 0 == width && !rt->isMultisampled());
joshualitt99c7c072015-05-01 13:43:30 -0700689 target->drawBatch(&pipelineBuilder, batch);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000690 } else {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000691 // filled BW rect
egdaniel8dd688b2015-01-22 10:16:09 -0800692 target->drawSimpleRect(&pipelineBuilder, color, viewMatrix, rect);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000693 }
694}
695
joshualitt25d9c152015-02-18 12:29:52 -0800696void GrContext::drawNonAARectToRect(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800697 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800698 const GrPaint& paint,
joshualitt16b27892014-12-18 07:47:16 -0800699 const SkMatrix& viewMatrix,
700 const SkRect& rectToDraw,
701 const SkRect& localRect,
702 const SkMatrix* localMatrix) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800703 RETURN_IF_ABANDONED
bsalomonf21dab92014-11-13 13:33:28 -0800704 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -0800705 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -0800706 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700707 if (NULL == target) {
708 return;
709 }
bsalomon@google.com64386952013-02-08 21:22:44 +0000710
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000711 GR_CREATE_TRACE_MARKER("GrContext::drawRectToRect", target);
712
joshualitt44701df2015-02-23 14:44:57 -0800713 target->drawRect(&pipelineBuilder,
714 paint.getColor(),
715 viewMatrix,
716 rectToDraw,
717 &localRect,
egdaniel8dd688b2015-01-22 10:16:09 -0800718 localMatrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000719}
720
joshualitt0823bfa2015-02-27 10:06:15 -0800721static const GrGeometryProcessor* set_vertex_attributes(bool hasLocalCoords,
722 bool hasColors,
joshualitt56995b52014-12-11 15:44:02 -0800723 int* colorOffset,
724 int* texOffset,
joshualitt8059eb92014-12-29 15:10:07 -0800725 GrColor color,
726 const SkMatrix& viewMatrix) {
robertphillips@google.com42903302013-04-20 12:26:07 +0000727 *texOffset = -1;
728 *colorOffset = -1;
joshualitt5478d422014-11-14 16:00:38 -0800729 uint32_t flags = GrDefaultGeoProcFactory::kPosition_GPType;
joshualitt0823bfa2015-02-27 10:06:15 -0800730 if (hasLocalCoords && hasColors) {
joshualitt5478d422014-11-14 16:00:38 -0800731 *colorOffset = sizeof(SkPoint);
732 *texOffset = sizeof(SkPoint) + sizeof(GrColor);
733 flags |= GrDefaultGeoProcFactory::kColor_GPType |
734 GrDefaultGeoProcFactory::kLocalCoord_GPType;
joshualitt0823bfa2015-02-27 10:06:15 -0800735 } else if (hasLocalCoords) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000736 *texOffset = sizeof(SkPoint);
joshualitt5478d422014-11-14 16:00:38 -0800737 flags |= GrDefaultGeoProcFactory::kLocalCoord_GPType;
joshualitt0823bfa2015-02-27 10:06:15 -0800738 } else if (hasColors) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000739 *colorOffset = sizeof(SkPoint);
joshualitt5478d422014-11-14 16:00:38 -0800740 flags |= GrDefaultGeoProcFactory::kColor_GPType;
robertphillips@google.com42903302013-04-20 12:26:07 +0000741 }
joshualitt8059eb92014-12-29 15:10:07 -0800742 return GrDefaultGeoProcFactory::Create(flags, color, viewMatrix, SkMatrix::I());
robertphillips@google.com42903302013-04-20 12:26:07 +0000743}
744
joshualitt0823bfa2015-02-27 10:06:15 -0800745class DrawVerticesBatch : public GrBatch {
746public:
747 struct Geometry {
748 GrColor fColor;
749 SkTDArray<SkPoint> fPositions;
750 SkTDArray<uint16_t> fIndices;
751 SkTDArray<GrColor> fColors;
752 SkTDArray<SkPoint> fLocalCoords;
753 };
754
755 static GrBatch* Create(const Geometry& geometry, GrPrimitiveType primitiveType,
756 const SkMatrix& viewMatrix,
757 const SkPoint* positions, int vertexCount,
758 const uint16_t* indices, int indexCount,
joshualitt99c7c072015-05-01 13:43:30 -0700759 const GrColor* colors, const SkPoint* localCoords,
760 const SkRect& bounds) {
joshualitt0823bfa2015-02-27 10:06:15 -0800761 return SkNEW_ARGS(DrawVerticesBatch, (geometry, primitiveType, viewMatrix, positions,
762 vertexCount, indices, indexCount, colors,
joshualitt99c7c072015-05-01 13:43:30 -0700763 localCoords, bounds));
joshualitt0823bfa2015-02-27 10:06:15 -0800764 }
765
mtklein36352bf2015-03-25 18:17:31 -0700766 const char* name() const override { return "DrawVerticesBatch"; }
joshualitt0823bfa2015-02-27 10:06:15 -0800767
mtklein36352bf2015-03-25 18:17:31 -0700768 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt0823bfa2015-02-27 10:06:15 -0800769 // When this is called on a batch, there is only one geometry bundle
770 if (this->hasColors()) {
771 out->setUnknownFourComponents();
772 } else {
773 out->setKnownFourComponents(fGeoData[0].fColor);
774 }
775 }
776
mtklein36352bf2015-03-25 18:17:31 -0700777 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualittd4c7d552015-02-27 13:00:56 -0800778 out->setKnownSingleComponent(0xff);
joshualitt0823bfa2015-02-27 10:06:15 -0800779 }
780
mtklein36352bf2015-03-25 18:17:31 -0700781 void initBatchTracker(const GrPipelineInfo& init) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800782 // Handle any color overrides
783 if (init.fColorIgnored) {
784 fGeoData[0].fColor = GrColor_ILLEGAL;
785 } else if (GrColor_ILLEGAL != init.fOverrideColor) {
786 fGeoData[0].fColor = init.fOverrideColor;
787 }
788
789 // setup batch properties
790 fBatch.fColorIgnored = init.fColorIgnored;
791 fBatch.fColor = fGeoData[0].fColor;
792 fBatch.fUsesLocalCoords = init.fUsesLocalCoords;
793 fBatch.fCoverageIgnored = init.fCoverageIgnored;
794 }
795
mtklein36352bf2015-03-25 18:17:31 -0700796 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800797 int colorOffset = -1, texOffset = -1;
798 SkAutoTUnref<const GrGeometryProcessor> gp(
799 set_vertex_attributes(this->hasLocalCoords(), this->hasColors(), &colorOffset,
800 &texOffset, this->color(), this->viewMatrix()));
801
802 batchTarget->initDraw(gp, pipeline);
803
804 // TODO this is hacky, but the only way we have to initialize the GP is to use the
805 // GrPipelineInfo struct so we can generate the correct shader. Once we have GrBatch
806 // everywhere we can remove this nastiness
807 GrPipelineInfo init;
808 init.fColorIgnored = fBatch.fColorIgnored;
809 init.fOverrideColor = GrColor_ILLEGAL;
810 init.fCoverageIgnored = fBatch.fCoverageIgnored;
811 init.fUsesLocalCoords = this->usesLocalCoords();
812 gp->initBatchTracker(batchTarget->currentBatchTracker(), init);
813
814 size_t vertexStride = gp->getVertexStride();
815
816 SkASSERT(vertexStride == sizeof(SkPoint) + (this->hasLocalCoords() ? sizeof(SkPoint) : 0)
817 + (this->hasColors() ? sizeof(GrColor) : 0));
818
819 int instanceCount = fGeoData.count();
820
821 const GrVertexBuffer* vertexBuffer;
822 int firstVertex;
823
bsalomoncb8979d2015-05-05 09:51:38 -0700824 void* verts = batchTarget->vertexPool()->makeSpace(vertexStride,
825 this->vertexCount(),
826 &vertexBuffer,
joshualitt0823bfa2015-02-27 10:06:15 -0800827 &firstVertex);
828
bsalomoncb8979d2015-05-05 09:51:38 -0700829 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -0800830 SkDebugf("Could not allocate vertices\n");
831 return;
832 }
833
bsalomonb5238a72015-05-05 07:49:49 -0700834 const GrIndexBuffer* indexBuffer = NULL;
835 int firstIndex = 0;
joshualitt0823bfa2015-02-27 10:06:15 -0800836
837 void* indices = NULL;
838 if (this->hasIndices()) {
839 indices = batchTarget->indexPool()->makeSpace(this->indexCount(),
840 &indexBuffer,
841 &firstIndex);
joshualitt4b31de82015-03-05 14:33:41 -0800842
843 if (!indices) {
844 SkDebugf("Could not allocate indices\n");
845 return;
846 }
joshualitt0823bfa2015-02-27 10:06:15 -0800847 }
848
849 int indexOffset = 0;
850 int vertexOffset = 0;
851 for (int i = 0; i < instanceCount; i++) {
852 const Geometry& args = fGeoData[i];
853
854 // TODO we can actually cache this interleaved and then just memcopy
855 if (this->hasIndices()) {
856 for (int j = 0; j < args.fIndices.count(); ++j, ++indexOffset) {
857 *((uint16_t*)indices + indexOffset) = args.fIndices[j] + vertexOffset;
858 }
859 }
860
861 for (int j = 0; j < args.fPositions.count(); ++j) {
bsalomoncb8979d2015-05-05 09:51:38 -0700862 *((SkPoint*)verts) = args.fPositions[j];
joshualitt0823bfa2015-02-27 10:06:15 -0800863 if (this->hasColors()) {
bsalomoncb8979d2015-05-05 09:51:38 -0700864 *(GrColor*)((intptr_t)verts + colorOffset) = args.fColors[j];
joshualitt0823bfa2015-02-27 10:06:15 -0800865 }
866 if (this->hasLocalCoords()) {
bsalomoncb8979d2015-05-05 09:51:38 -0700867 *(SkPoint*)((intptr_t)verts + texOffset) = args.fLocalCoords[j];
joshualitt0823bfa2015-02-27 10:06:15 -0800868 }
bsalomoncb8979d2015-05-05 09:51:38 -0700869 verts = (void*)((intptr_t)verts + vertexStride);
joshualitt0823bfa2015-02-27 10:06:15 -0800870 vertexOffset++;
871 }
872 }
873
bsalomoncb8979d2015-05-05 09:51:38 -0700874 GrVertices vertices;
joshualitt0823bfa2015-02-27 10:06:15 -0800875 if (this->hasIndices()) {
bsalomoncb8979d2015-05-05 09:51:38 -0700876 vertices.initIndexed(this->primitiveType(), vertexBuffer, indexBuffer, firstVertex,
bsalomonb5238a72015-05-05 07:49:49 -0700877 firstIndex, this->vertexCount(), this->indexCount());
878
joshualitt0823bfa2015-02-27 10:06:15 -0800879 } else {
bsalomoncb8979d2015-05-05 09:51:38 -0700880 vertices.init(this->primitiveType(), vertexBuffer, firstVertex, this->vertexCount());
joshualitt0823bfa2015-02-27 10:06:15 -0800881 }
bsalomoncb8979d2015-05-05 09:51:38 -0700882 batchTarget->draw(vertices);
joshualitt0823bfa2015-02-27 10:06:15 -0800883 }
884
885 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
886
887private:
888 DrawVerticesBatch(const Geometry& geometry, GrPrimitiveType primitiveType,
889 const SkMatrix& viewMatrix,
890 const SkPoint* positions, int vertexCount,
891 const uint16_t* indices, int indexCount,
joshualitt99c7c072015-05-01 13:43:30 -0700892 const GrColor* colors, const SkPoint* localCoords, const SkRect& bounds) {
joshualitt0823bfa2015-02-27 10:06:15 -0800893 this->initClassID<DrawVerticesBatch>();
894 SkASSERT(positions);
895
896 fBatch.fViewMatrix = viewMatrix;
897 Geometry& installedGeo = fGeoData.push_back(geometry);
898
899 installedGeo.fPositions.append(vertexCount, positions);
900 if (indices) {
901 installedGeo.fIndices.append(indexCount, indices);
902 fBatch.fHasIndices = true;
903 } else {
904 fBatch.fHasIndices = false;
905 }
906
907 if (colors) {
908 installedGeo.fColors.append(vertexCount, colors);
909 fBatch.fHasColors = true;
910 } else {
911 fBatch.fHasColors = false;
912 }
913
914 if (localCoords) {
915 installedGeo.fLocalCoords.append(vertexCount, localCoords);
916 fBatch.fHasLocalCoords = true;
917 } else {
918 fBatch.fHasLocalCoords = false;
919 }
920 fBatch.fVertexCount = vertexCount;
921 fBatch.fIndexCount = indexCount;
922 fBatch.fPrimitiveType = primitiveType;
joshualitt99c7c072015-05-01 13:43:30 -0700923
924 this->setBounds(bounds);
joshualitt0823bfa2015-02-27 10:06:15 -0800925 }
926
927 GrPrimitiveType primitiveType() const { return fBatch.fPrimitiveType; }
928 bool batchablePrimitiveType() const {
929 return kTriangles_GrPrimitiveType == fBatch.fPrimitiveType ||
930 kLines_GrPrimitiveType == fBatch.fPrimitiveType ||
931 kPoints_GrPrimitiveType == fBatch.fPrimitiveType;
932 }
933 GrColor color() const { return fBatch.fColor; }
934 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
935 bool colorIgnored() const { return fBatch.fColorIgnored; }
936 const SkMatrix& viewMatrix() const { return fBatch.fViewMatrix; }
937 bool hasColors() const { return fBatch.fHasColors; }
938 bool hasIndices() const { return fBatch.fHasIndices; }
939 bool hasLocalCoords() const { return fBatch.fHasLocalCoords; }
940 int vertexCount() const { return fBatch.fVertexCount; }
941 int indexCount() const { return fBatch.fIndexCount; }
942
mtklein36352bf2015-03-25 18:17:31 -0700943 bool onCombineIfPossible(GrBatch* t) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800944 DrawVerticesBatch* that = t->cast<DrawVerticesBatch>();
945
946 if (!this->batchablePrimitiveType() || this->primitiveType() != that->primitiveType()) {
947 return false;
948 }
949
950 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
951
952 // We currently use a uniform viewmatrix for this batch
953 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
954 return false;
955 }
956
957 if (this->hasColors() != that->hasColors()) {
958 return false;
959 }
960
961 if (this->hasIndices() != that->hasIndices()) {
962 return false;
963 }
964
965 if (this->hasLocalCoords() != that->hasLocalCoords()) {
966 return false;
967 }
968
969 if (!this->hasColors() && this->color() != that->color()) {
970 return false;
971 }
972
973 if (this->color() != that->color()) {
974 fBatch.fColor = GrColor_ILLEGAL;
975 }
976 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
977 fBatch.fVertexCount += that->vertexCount();
978 fBatch.fIndexCount += that->indexCount();
joshualitt99c7c072015-05-01 13:43:30 -0700979
980 this->joinBounds(that->bounds());
joshualitt0823bfa2015-02-27 10:06:15 -0800981 return true;
982 }
983
984 struct BatchTracker {
985 GrPrimitiveType fPrimitiveType;
986 SkMatrix fViewMatrix;
987 GrColor fColor;
988 bool fUsesLocalCoords;
989 bool fColorIgnored;
990 bool fCoverageIgnored;
991 bool fHasColors;
992 bool fHasIndices;
993 bool fHasLocalCoords;
994 int fVertexCount;
995 int fIndexCount;
996 };
997
998 BatchTracker fBatch;
999 SkSTArray<1, Geometry, true> fGeoData;
1000};
1001
joshualitt25d9c152015-02-18 12:29:52 -08001002void GrContext::drawVertices(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -08001003 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001004 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001005 const SkMatrix& viewMatrix,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001006 GrPrimitiveType primitiveType,
1007 int vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001008 const SkPoint positions[],
1009 const SkPoint texCoords[],
bsalomon@google.com27847de2011-02-22 20:59:41 +00001010 const GrColor colors[],
1011 const uint16_t indices[],
1012 int indexCount) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001013 RETURN_IF_ABANDONED
bsalomonf21dab92014-11-13 13:33:28 -08001014 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001015 GrPipelineBuilder pipelineBuilder;
commit-bot@chromium.org5a567932014-01-08 21:26:09 +00001016
joshualitt570d2f82015-02-25 13:19:48 -08001017 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001018 if (NULL == target) {
1019 return;
1020 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001021
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001022 GR_CREATE_TRACE_MARKER("GrContext::drawVertices", target);
1023
joshualitta09c0462015-04-30 14:03:02 -07001024 // TODO clients should give us bounds
1025 SkRect bounds;
1026 if (!bounds.setBoundsCheck(positions, vertexCount)) {
1027 SkDebugf("drawVertices call empty bounds\n");
1028 return;
1029 }
1030
joshualitt0c14bcc2015-05-05 13:58:20 -07001031 viewMatrix.mapRect(&bounds);
1032
joshualitt0823bfa2015-02-27 10:06:15 -08001033 DrawVerticesBatch::Geometry geometry;
1034 geometry.fColor = paint.getColor();
joshualitt0823bfa2015-02-27 10:06:15 -08001035 SkAutoTUnref<GrBatch> batch(DrawVerticesBatch::Create(geometry, primitiveType, viewMatrix,
1036 positions, vertexCount, indices,
joshualitt99c7c072015-05-01 13:43:30 -07001037 indexCount, colors, texCoords,
1038 bounds));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001039
joshualitt99c7c072015-05-01 13:43:30 -07001040 target->drawBatch(&pipelineBuilder, batch);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001041}
1042
bsalomon@google.com06afe7b2011-04-26 15:31:40 +00001043///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com93c96602012-04-27 13:05:21 +00001044
joshualitt25d9c152015-02-18 12:29:52 -08001045void GrContext::drawRRect(GrRenderTarget*rt,
joshualitt570d2f82015-02-25 13:19:48 -08001046 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001047 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001048 const SkMatrix& viewMatrix,
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001049 const SkRRect& rrect,
egdanield58a0ba2014-06-11 10:30:05 -07001050 const GrStrokeInfo& strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001051 RETURN_IF_ABANDONED
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001052 if (rrect.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001053 return;
1054 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001055
egdanield58a0ba2014-06-11 10:30:05 -07001056 if (strokeInfo.isDashed()) {
1057 SkPath path;
1058 path.addRRect(rrect);
joshualitt570d2f82015-02-25 13:19:48 -08001059 this->drawPath(rt, clip, paint, viewMatrix, path, strokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -07001060 return;
1061 }
1062
bsalomonf21dab92014-11-13 13:33:28 -08001063 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001064 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -08001065 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001066 if (NULL == target) {
1067 return;
1068 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001069
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001070 GR_CREATE_TRACE_MARKER("GrContext::drawRRect", target);
1071
egdanield58a0ba2014-06-11 10:30:05 -07001072 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1073
joshualitt2e3b3e32014-12-09 13:31:14 -08001074 GrColor color = paint.getColor();
joshualitt44701df2015-02-23 14:44:57 -08001075 if (!fOvalRenderer->drawRRect(target,
1076 &pipelineBuilder,
1077 color,
1078 viewMatrix,
1079 paint.isAntiAlias(),
1080 rrect,
1081 strokeRec)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001082 SkPath path;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001083 path.addRRect(rrect);
egdaniel8dd688b2015-01-22 10:16:09 -08001084 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(),
1085 path, strokeInfo);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001086 }
1087}
1088
1089///////////////////////////////////////////////////////////////////////////////
1090
joshualitt25d9c152015-02-18 12:29:52 -08001091void GrContext::drawDRRect(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -08001092 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001093 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001094 const SkMatrix& viewMatrix,
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001095 const SkRRect& outer,
1096 const SkRRect& inner) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001097 RETURN_IF_ABANDONED
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001098 if (outer.isEmpty()) {
1099 return;
1100 }
1101
bsalomonf21dab92014-11-13 13:33:28 -08001102 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001103 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -08001104 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001105
1106 GR_CREATE_TRACE_MARKER("GrContext::drawDRRect", target);
1107
joshualitt2e3b3e32014-12-09 13:31:14 -08001108 GrColor color = paint.getColor();
joshualitt44701df2015-02-23 14:44:57 -08001109 if (!fOvalRenderer->drawDRRect(target,
1110 &pipelineBuilder,
1111 color,
1112 viewMatrix,
1113 paint.isAntiAlias(),
1114 outer,
1115 inner)) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001116 SkPath path;
1117 path.addRRect(inner);
1118 path.addRRect(outer);
1119 path.setFillType(SkPath::kEvenOdd_FillType);
1120
egdanield58a0ba2014-06-11 10:30:05 -07001121 GrStrokeInfo fillRec(SkStrokeRec::kFill_InitStyle);
egdaniel8dd688b2015-01-22 10:16:09 -08001122 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(),
1123 path, fillRec);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001124 }
1125}
1126
1127///////////////////////////////////////////////////////////////////////////////
1128
joshualitt570d2f82015-02-25 13:19:48 -08001129void GrContext::drawOval(GrRenderTarget* rt,
1130 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001131 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001132 const SkMatrix& viewMatrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001133 const SkRect& oval,
egdanield58a0ba2014-06-11 10:30:05 -07001134 const GrStrokeInfo& strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001135 RETURN_IF_ABANDONED
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001136 if (oval.isEmpty()) {
1137 return;
1138 }
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001139
egdanield58a0ba2014-06-11 10:30:05 -07001140 if (strokeInfo.isDashed()) {
1141 SkPath path;
1142 path.addOval(oval);
joshualitt570d2f82015-02-25 13:19:48 -08001143 this->drawPath(rt, clip, paint, viewMatrix, path, strokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -07001144 return;
1145 }
1146
bsalomonf21dab92014-11-13 13:33:28 -08001147 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001148 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -08001149 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001150 if (NULL == target) {
1151 return;
1152 }
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001153
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001154 GR_CREATE_TRACE_MARKER("GrContext::drawOval", target);
1155
egdanield58a0ba2014-06-11 10:30:05 -07001156 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1157
joshualitt2e3b3e32014-12-09 13:31:14 -08001158 GrColor color = paint.getColor();
joshualitt44701df2015-02-23 14:44:57 -08001159 if (!fOvalRenderer->drawOval(target,
1160 &pipelineBuilder,
1161 color,
1162 viewMatrix,
1163 paint.isAntiAlias(),
1164 oval,
1165 strokeRec)) {
bsalomon@google.com93c96602012-04-27 13:05:21 +00001166 SkPath path;
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001167 path.addOval(oval);
egdaniel8dd688b2015-01-22 10:16:09 -08001168 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(),
1169 path, strokeInfo);
skia.committer@gmail.com98ded842013-01-23 07:06:17 +00001170 }
bsalomon@google.com150d2842012-01-12 20:19:56 +00001171}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001172
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001173// Can 'path' be drawn as a pair of filled nested rectangles?
1174static bool is_nested_rects(GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -08001175 GrPipelineBuilder* pipelineBuilder,
joshualitt2e3b3e32014-12-09 13:31:14 -08001176 GrColor color,
joshualitt8059eb92014-12-29 15:10:07 -08001177 const SkMatrix& viewMatrix,
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001178 const SkPath& path,
1179 const SkStrokeRec& stroke,
bsalomon9c0822a2014-08-11 11:07:48 -07001180 SkRect rects[2]) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001181 SkASSERT(stroke.isFillStyle());
1182
1183 if (path.isInverseFillType()) {
1184 return false;
1185 }
1186
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001187 // TODO: this restriction could be lifted if we were willing to apply
1188 // the matrix to all the points individually rather than just to the rect
joshualitt8059eb92014-12-29 15:10:07 -08001189 if (!viewMatrix.preservesAxisAlignment()) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001190 return false;
1191 }
1192
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001193 SkPath::Direction dirs[2];
caryclark95bc5f32015-04-08 08:34:15 -07001194 if (!path.isNestedFillRects(rects, dirs)) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001195 return false;
1196 }
1197
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001198 if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001199 // The two rects need to be wound opposite to each other
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001200 return false;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001201 }
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001202
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001203 // Right now, nested rects where the margin is not the same width
1204 // all around do not render correctly
1205 const SkScalar* outer = rects[0].asScalars();
1206 const SkScalar* inner = rects[1].asScalars();
1207
robertphillips183e9852014-10-21 11:25:37 -07001208 bool allEq = true;
1209
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001210 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
robertphillips183e9852014-10-21 11:25:37 -07001211 bool allGoE1 = margin >= SK_Scalar1;
1212
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001213 for (int i = 1; i < 4; ++i) {
1214 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
robertphillips183e9852014-10-21 11:25:37 -07001215 if (temp < SK_Scalar1) {
1216 allGoE1 = false;
1217 }
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001218 if (!SkScalarNearlyEqual(margin, temp)) {
robertphillips183e9852014-10-21 11:25:37 -07001219 allEq = false;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001220 }
1221 }
1222
robertphillips183e9852014-10-21 11:25:37 -07001223 return allEq || allGoE1;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001224}
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001225
joshualitt25d9c152015-02-18 12:29:52 -08001226void GrContext::drawPath(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -08001227 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001228 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001229 const SkMatrix& viewMatrix,
1230 const SkPath& path,
1231 const GrStrokeInfo& strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001232 RETURN_IF_ABANDONED
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001233 if (path.isEmpty()) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001234 if (path.isInverseFillType()) {
joshualitt570d2f82015-02-25 13:19:48 -08001235 this->drawPaint(rt, clip, paint, viewMatrix);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001236 }
1237 return;
1238 }
1239
joshualitt2e3b3e32014-12-09 13:31:14 -08001240 GrColor color = paint.getColor();
egdanield58a0ba2014-06-11 10:30:05 -07001241
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001242 // Note that internalDrawPath may sw-rasterize the path into a scratch texture.
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001243 // Scratch textures can be recycled after they are returned to the texture
1244 // cache. This presents a potential hazard for buffered drawing. However,
1245 // the writePixels that uploads to the scratch will perform a flush so we're
1246 // OK.
bsalomonf21dab92014-11-13 13:33:28 -08001247 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001248 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -08001249 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001250 if (NULL == target) {
1251 return;
1252 }
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001253
egdaniel93a37bc2014-07-21 13:47:57 -07001254 GR_CREATE_TRACE_MARKER1("GrContext::drawPath", target, "Is Convex", path.isConvex());
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001255
kkinnunen18996512015-04-26 23:18:49 -07001256 if (!strokeInfo.isDashed()) {
1257 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1258 bool useCoverageAA = paint.isAntiAlias() &&
1259 !pipelineBuilder.getRenderTarget()->isMultisampled();
egdanield58a0ba2014-06-11 10:30:05 -07001260
kkinnunen18996512015-04-26 23:18:49 -07001261 if (useCoverageAA && strokeRec.getWidth() < 0 && !path.isConvex()) {
1262 // Concave AA paths are expensive - try to avoid them for special cases
1263 SkRect rects[2];
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001264
kkinnunen18996512015-04-26 23:18:49 -07001265 if (is_nested_rects(target, &pipelineBuilder, color, viewMatrix, path, strokeRec,
1266 rects)) {
1267 fAARectRenderer->fillAANestedRects(target, &pipelineBuilder, color, viewMatrix,
1268 rects);
1269 return;
1270 }
1271 }
1272 SkRect ovalRect;
1273 bool isOval = path.isOval(&ovalRect);
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001274
kkinnunen18996512015-04-26 23:18:49 -07001275 if (isOval && !path.isInverseFillType()) {
1276 if (fOvalRenderer->drawOval(target,
1277 &pipelineBuilder,
1278 color,
1279 viewMatrix,
1280 paint.isAntiAlias(),
1281 ovalRect,
1282 strokeRec)) {
1283 return;
1284 }
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001285 }
1286 }
kkinnunen18996512015-04-26 23:18:49 -07001287 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(),
1288 path, strokeInfo);
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001289}
1290
joshualitt9853cce2014-11-17 14:22:48 -08001291void GrContext::internalDrawPath(GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -08001292 GrPipelineBuilder* pipelineBuilder,
joshualitt5531d512014-12-17 15:50:11 -08001293 const SkMatrix& viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -08001294 GrColor color,
joshualitt9853cce2014-11-17 14:22:48 -08001295 bool useAA,
1296 const SkPath& path,
egdanield58a0ba2014-06-11 10:30:05 -07001297 const GrStrokeInfo& strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001298 RETURN_IF_ABANDONED
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001299 SkASSERT(!path.isEmpty());
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001300
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001301 GR_CREATE_TRACE_MARKER("GrContext::internalDrawPath", target);
1302
1303
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001304 // An Assumption here is that path renderer would use some form of tweaking
1305 // the src color (either the input alpha or in the frag shader) to implement
1306 // aa. If we have some future driver-mojo path AA that can do the right
1307 // thing WRT to the blend then we'll need some query on the PR.
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001308 bool useCoverageAA = useAA &&
egdaniel0bdeec92015-02-23 12:12:54 -08001309 !pipelineBuilder->getRenderTarget()->isMultisampled();
bsalomon@google.com289533a2011-10-27 12:34:25 +00001310
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001311
1312 GrPathRendererChain::DrawType type =
1313 useCoverageAA ? GrPathRendererChain::kColorAntiAlias_DrawType :
joshualitt9853cce2014-11-17 14:22:48 -08001314 GrPathRendererChain::kColor_DrawType;
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001315
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001316 const SkPath* pathPtr = &path;
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001317 SkTLazy<SkPath> tmpPath;
kkinnunen18996512015-04-26 23:18:49 -07001318 const GrStrokeInfo* strokeInfoPtr = &strokeInfo;
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001319
1320 // Try a 1st time without stroking the path and without allowing the SW renderer
egdaniel8dd688b2015-01-22 10:16:09 -08001321 GrPathRenderer* pr = this->getPathRenderer(target, pipelineBuilder, viewMatrix, *pathPtr,
kkinnunen18996512015-04-26 23:18:49 -07001322 *strokeInfoPtr, false, type);
1323
1324 GrStrokeInfo dashlessStrokeInfo(strokeInfo, false);
1325 if (NULL == pr && strokeInfo.isDashed()) {
1326 // It didn't work above, so try again with dashed stroke converted to a dashless stroke.
bsalomon07ab9f32015-05-04 11:41:57 -07001327 if (!strokeInfo.applyDash(tmpPath.init(), &dashlessStrokeInfo, *pathPtr)) {
1328 return;
kkinnunen18996512015-04-26 23:18:49 -07001329 }
bsalomon07ab9f32015-05-04 11:41:57 -07001330 pathPtr = tmpPath.get();
1331 if (pathPtr->isEmpty()) {
1332 return;
1333 }
1334 strokeInfoPtr = &dashlessStrokeInfo;
kkinnunen18996512015-04-26 23:18:49 -07001335 pr = this->getPathRenderer(target, pipelineBuilder, viewMatrix, *pathPtr, *strokeInfoPtr,
1336 false, type);
1337 }
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001338
robertphillips@google.come79f3202014-02-11 16:30:21 +00001339 if (NULL == pr) {
bsalomon07ab9f32015-05-04 11:41:57 -07001340 if (!GrPathRenderer::IsStrokeHairlineOrEquivalent(*strokeInfoPtr, viewMatrix, NULL) &&
1341 !strokeInfoPtr->isFillStyle()) {
kkinnunen18996512015-04-26 23:18:49 -07001342 // It didn't work above, so try again with stroke converted to a fill.
1343 if (!tmpPath.isValid()) {
1344 tmpPath.init();
1345 }
1346 SkStrokeRec* strokeRec = dashlessStrokeInfo.getStrokeRecPtr();
1347 strokeRec->setResScale(SkScalarAbs(viewMatrix.getMaxScale()));
bsalomon07ab9f32015-05-04 11:41:57 -07001348 if (!strokeRec->applyToPath(tmpPath.get(), *pathPtr)) {
1349 return;
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001350 }
bsalomon07ab9f32015-05-04 11:41:57 -07001351 pathPtr = tmpPath.get();
1352 if (pathPtr->isEmpty()) {
1353 return;
1354 }
1355 strokeRec->setFillStyle();
1356 strokeInfoPtr = &dashlessStrokeInfo;
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001357 }
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001358
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001359 // This time, allow SW renderer
kkinnunen18996512015-04-26 23:18:49 -07001360 pr = this->getPathRenderer(target, pipelineBuilder, viewMatrix, *pathPtr, *strokeInfoPtr,
1361 true, type);
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001362 }
1363
robertphillips@google.come79f3202014-02-11 16:30:21 +00001364 if (NULL == pr) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001365#ifdef SK_DEBUG
tfarina38406c82014-10-31 07:11:12 -07001366 SkDebugf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001367#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001368 return;
1369 }
1370
kkinnunen18996512015-04-26 23:18:49 -07001371 pr->drawPath(target, pipelineBuilder, color, viewMatrix, *pathPtr, *strokeInfoPtr, useCoverageAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001372}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001373
bsalomon@google.com27847de2011-02-22 20:59:41 +00001374////////////////////////////////////////////////////////////////////////////////
1375
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001376void GrContext::flush(int flagsBitfield) {
robertphillips@google.come7db8d62013-07-04 11:48:52 +00001377 if (NULL == fDrawBuffer) {
1378 return;
1379 }
1380
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001381 if (kDiscard_FlushBit & flagsBitfield) {
1382 fDrawBuffer->reset();
1383 } else {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001384 fDrawBuffer->flush();
junov@google.com53a55842011-06-08 22:55:10 +00001385 }
bsalomon3f324322015-04-08 11:01:54 -07001386 fResourceCache->notifyFlushOccurred();
bsalomonf21dab92014-11-13 13:33:28 -08001387 fFlushToReduceCacheSize = false;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001388}
1389
bsalomon81beccc2014-10-13 12:32:55 -07001390bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
1391 const void* inPixels, size_t outRowBytes, void* outPixels) {
1392 SkSrcPixelInfo srcPI;
jvanverthfa1e8a72014-12-22 08:31:49 -08001393 if (!GrPixelConfig2ColorAndProfileType(srcConfig, &srcPI.fColorType, NULL)) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001394 return false;
1395 }
bsalomon81beccc2014-10-13 12:32:55 -07001396 srcPI.fAlphaType = kUnpremul_SkAlphaType;
1397 srcPI.fPixels = inPixels;
1398 srcPI.fRowBytes = inRowBytes;
1399
1400 SkDstPixelInfo dstPI;
1401 dstPI.fColorType = srcPI.fColorType;
1402 dstPI.fAlphaType = kPremul_SkAlphaType;
1403 dstPI.fPixels = outPixels;
1404 dstPI.fRowBytes = outRowBytes;
1405
1406 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001407}
1408
bsalomon81beccc2014-10-13 12:32:55 -07001409bool GrContext::writeSurfacePixels(GrSurface* surface,
1410 int left, int top, int width, int height,
1411 GrPixelConfig srcConfig, const void* buffer, size_t rowBytes,
1412 uint32_t pixelOpsFlags) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001413 RETURN_FALSE_IF_ABANDONED
bsalomon81beccc2014-10-13 12:32:55 -07001414 {
1415 GrTexture* texture = NULL;
1416 if (!(kUnpremul_PixelOpsFlag & pixelOpsFlags) && (texture = surface->asTexture()) &&
1417 fGpu->canWriteTexturePixels(texture, srcConfig)) {
1418
1419 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) &&
1420 surface->surfacePriv().hasPendingIO()) {
1421 this->flush();
1422 }
1423 return fGpu->writeTexturePixels(texture, left, top, width, height,
1424 srcConfig, buffer, rowBytes);
1425 // Don't need to check kFlushWrites_PixelOp here, we just did a direct write so the
1426 // upload is already flushed.
1427 }
1428 }
1429
1430 // If we didn't do a direct texture write then we upload the pixels to a texture and draw.
1431 GrRenderTarget* renderTarget = surface->asRenderTarget();
1432 if (NULL == renderTarget) {
1433 return false;
1434 }
1435
1436 // We ignore the preferred config unless it is a R/B swap of the src config. In that case
1437 // we will upload the original src data to a scratch texture but we will spoof it as the swapped
1438 // config. This scratch will then have R and B swapped. We correct for this by swapping again
1439 // when drawing the scratch to the dst using a conversion effect.
1440 bool swapRAndB = false;
1441 GrPixelConfig writeConfig = srcConfig;
1442 if (GrPixelConfigSwapRAndB(srcConfig) ==
1443 fGpu->preferredWritePixelsConfig(srcConfig, renderTarget->config())) {
1444 writeConfig = GrPixelConfigSwapRAndB(srcConfig);
1445 swapRAndB = true;
1446 }
1447
bsalomonf2703d82014-10-28 14:33:06 -07001448 GrSurfaceDesc desc;
bsalomon81beccc2014-10-13 12:32:55 -07001449 desc.fWidth = width;
1450 desc.fHeight = height;
1451 desc.fConfig = writeConfig;
bsalomond309e7a2015-04-30 14:18:54 -07001452 SkAutoTUnref<GrTexture> texture(this->textureProvider()->refScratchTexture(desc,
1453 GrTextureProvider::kApprox_ScratchTexMatch));
bsalomone3059732014-10-14 11:47:22 -07001454 if (!texture) {
bsalomon81beccc2014-10-13 12:32:55 -07001455 return false;
1456 }
1457
1458 SkAutoTUnref<const GrFragmentProcessor> fp;
1459 SkMatrix textureMatrix;
1460 textureMatrix.setIDiv(texture->width(), texture->height());
1461
1462 // allocate a tmp buffer and sw convert the pixels to premul
1463 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
1464
1465 if (kUnpremul_PixelOpsFlag & pixelOpsFlags) {
1466 if (!GrPixelConfigIs8888(srcConfig)) {
1467 return false;
1468 }
1469 fp.reset(this->createUPMToPMEffect(texture, swapRAndB, textureMatrix));
1470 // handle the unpremul step on the CPU if we couldn't create an effect to do it.
1471 if (NULL == fp) {
1472 size_t tmpRowBytes = 4 * width;
1473 tmpPixels.reset(width * height);
1474 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
1475 tmpPixels.get())) {
1476 return false;
1477 }
1478 rowBytes = tmpRowBytes;
1479 buffer = tmpPixels.get();
1480 }
1481 }
1482 if (NULL == fp) {
1483 fp.reset(GrConfigConversionEffect::Create(texture,
1484 swapRAndB,
1485 GrConfigConversionEffect::kNone_PMConversion,
1486 textureMatrix));
1487 }
1488
1489 // Even if the client told us not to flush, we still flush here. The client may have known that
1490 // writes to the original surface caused no data hazards, but they can't know that the scratch
1491 // we just got is safe.
1492 if (texture->surfacePriv().hasPendingIO()) {
1493 this->flush();
1494 }
1495 if (!fGpu->writeTexturePixels(texture, 0, 0, width, height,
1496 writeConfig, buffer, rowBytes)) {
1497 return false;
1498 }
1499
1500 SkMatrix matrix;
1501 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
1502
1503 // This function can be called in the midst of drawing another object (e.g., when uploading a
1504 // SW-rasterized clip while issuing a draw). So we push the current geometry state before
1505 // drawing a rect to the render target.
1506 // The bracket ensures we pop the stack if we wind up flushing below.
1507 {
joshualitt570d2f82015-02-25 13:19:48 -08001508 GrDrawTarget* drawTarget = this->prepareToDraw();
1509 if (!drawTarget) {
1510 return false;
1511 }
joshualitt9853cce2014-11-17 14:22:48 -08001512
egdaniel8dd688b2015-01-22 10:16:09 -08001513 GrPipelineBuilder pipelineBuilder;
1514 pipelineBuilder.addColorProcessor(fp);
1515 pipelineBuilder.setRenderTarget(renderTarget);
joshualitt44701df2015-02-23 14:44:57 -08001516 drawTarget->drawSimpleRect(&pipelineBuilder,
1517 GrColor_WHITE,
1518 matrix,
joshualitt8059eb92014-12-29 15:10:07 -08001519 SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)));
bsalomon81beccc2014-10-13 12:32:55 -07001520 }
1521
1522 if (kFlushWrites_PixelOp & pixelOpsFlags) {
1523 this->flushSurfaceWrites(surface);
1524 }
1525
1526 return true;
1527}
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001528
reed@google.com7111d462014-03-25 16:20:24 +00001529// toggles between RGBA and BGRA
1530static SkColorType toggle_colortype32(SkColorType ct) {
1531 if (kRGBA_8888_SkColorType == ct) {
1532 return kBGRA_8888_SkColorType;
1533 } else {
1534 SkASSERT(kBGRA_8888_SkColorType == ct);
1535 return kRGBA_8888_SkColorType;
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001536 }
1537}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001538
bsalomon@google.com0342a852012-08-20 19:22:38 +00001539bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
1540 int left, int top, int width, int height,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001541 GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001542 uint32_t flags) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001543 RETURN_FALSE_IF_ABANDONED
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001544 ASSERT_OWNED_RESOURCE(target);
bsalomon89c62982014-11-03 12:08:42 -08001545 SkASSERT(target);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001546
bsalomonafbf2d62014-09-30 12:18:44 -07001547 if (!(kDontFlush_PixelOpsFlag & flags) && target->surfacePriv().hasPendingWrite()) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001548 this->flush();
1549 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001550
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001551 // Determine which conversions have to be applied: flipY, swapRAnd, and/or unpremul.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001552
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001553 // If fGpu->readPixels would incur a y-flip cost then we will read the pixels upside down. We'll
1554 // either do the flipY by drawing into a scratch with a matrix or on the cpu after the read.
1555 bool flipY = fGpu->readPixelsWillPayForYFlip(target, left, top,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001556 width, height, dstConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001557 rowBytes);
bsalomon@google.com9c680582013-02-06 18:17:50 +00001558 // We ignore the preferred config if it is different than our config unless it is an R/B swap.
1559 // In that case we'll perform an R and B swap while drawing to a scratch texture of the swapped
1560 // config. Then we will call readPixels on the scratch with the swapped config. The swaps during
1561 // the draw cancels out the fact that we call readPixels with a config that is R/B swapped from
1562 // dstConfig.
1563 GrPixelConfig readConfig = dstConfig;
1564 bool swapRAndB = false;
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +00001565 if (GrPixelConfigSwapRAndB(dstConfig) ==
1566 fGpu->preferredReadPixelsConfig(dstConfig, target->config())) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001567 readConfig = GrPixelConfigSwapRAndB(readConfig);
1568 swapRAndB = true;
1569 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001570
bsalomon@google.com0342a852012-08-20 19:22:38 +00001571 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001572
bsalomon@google.com9c680582013-02-06 18:17:50 +00001573 if (unpremul && !GrPixelConfigIs8888(dstConfig)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001574 // The unpremul flag is only allowed for these two configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001575 return false;
1576 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001577
bsalomon191bcc02014-11-14 11:31:13 -08001578 SkAutoTUnref<GrTexture> tempTexture;
1579
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001580 // If the src is a texture and we would have to do conversions after read pixels, we instead
1581 // do the conversions by drawing the src to a scratch texture. If we handle any of the
1582 // conversions in the draw we set the corresponding bool to false so that we don't reapply it
1583 // on the read back pixels.
1584 GrTexture* src = target->asTexture();
bsalomon49f085d2014-09-05 13:34:00 -07001585 if (src && (swapRAndB || unpremul || flipY)) {
bsalomon81beccc2014-10-13 12:32:55 -07001586 // Make the scratch a render so we can read its pixels.
bsalomonf2703d82014-10-28 14:33:06 -07001587 GrSurfaceDesc desc;
1588 desc.fFlags = kRenderTarget_GrSurfaceFlag;
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001589 desc.fWidth = width;
1590 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001591 desc.fConfig = readConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001592 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001593
bsalomon@google.com9c680582013-02-06 18:17:50 +00001594 // 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 +00001595 // match the passed rect. However, if we see many different size rectangles we will trash
1596 // our texture cache and pay the cost of creating and destroying many textures. So, we only
1597 // request an exact match when the caller is reading an entire RT.
bsalomond309e7a2015-04-30 14:18:54 -07001598 GrTextureProvider::ScratchTexMatch match = GrTextureProvider::kApprox_ScratchTexMatch;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001599 if (0 == left &&
1600 0 == top &&
1601 target->width() == width &&
1602 target->height() == height &&
1603 fGpu->fullReadPixelsIsFasterThanPartial()) {
bsalomond309e7a2015-04-30 14:18:54 -07001604 match = GrTextureProvider::kExact_ScratchTexMatch;
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001605 }
bsalomond309e7a2015-04-30 14:18:54 -07001606 tempTexture.reset(this->textureProvider()->refScratchTexture(desc, match));
bsalomon191bcc02014-11-14 11:31:13 -08001607 if (tempTexture) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001608 // compute a matrix to perform the draw
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001609 SkMatrix textureMatrix;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001610 textureMatrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001611 textureMatrix.postIDiv(src->width(), src->height());
1612
joshualittb0a8a372014-09-23 09:50:21 -07001613 SkAutoTUnref<const GrFragmentProcessor> fp;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001614 if (unpremul) {
joshualittb0a8a372014-09-23 09:50:21 -07001615 fp.reset(this->createPMToUPMEffect(src, swapRAndB, textureMatrix));
1616 if (fp) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001617 unpremul = false; // we no longer need to do this on CPU after the read back.
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001618 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001619 }
1620 // If we failed to create a PM->UPM effect and have no other conversions to perform then
1621 // there is no longer any point to using the scratch.
joshualittb0a8a372014-09-23 09:50:21 -07001622 if (fp || flipY || swapRAndB) {
1623 if (!fp) {
1624 fp.reset(GrConfigConversionEffect::Create(
1625 src, swapRAndB, GrConfigConversionEffect::kNone_PMConversion,
1626 textureMatrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001627 }
1628 swapRAndB = false; // we will handle the swap in the draw.
bsalomon@google.comc4364992011-11-07 15:54:49 +00001629
robertphillips@google.com13f181f2013-03-02 12:02:08 +00001630 // We protect the existing geometry here since it may not be
1631 // clear to the caller that a draw operation (i.e., drawSimpleRect)
1632 // can be invoked in this method
joshualitt5c55fef2014-10-31 14:04:35 -07001633 {
egdaniel8dd688b2015-01-22 10:16:09 -08001634 GrPipelineBuilder pipelineBuilder;
joshualitt5c55fef2014-10-31 14:04:35 -07001635 SkASSERT(fp);
egdaniel8dd688b2015-01-22 10:16:09 -08001636 pipelineBuilder.addColorProcessor(fp);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001637
egdaniel8dd688b2015-01-22 10:16:09 -08001638 pipelineBuilder.setRenderTarget(tempTexture->asRenderTarget());
joshualitt5c55fef2014-10-31 14:04:35 -07001639 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
joshualitt44701df2015-02-23 14:44:57 -08001640 fDrawBuffer->drawSimpleRect(&pipelineBuilder,
1641 GrColor_WHITE,
1642 SkMatrix::I(),
egdaniel8dd688b2015-01-22 10:16:09 -08001643 rect);
joshualitt5c55fef2014-10-31 14:04:35 -07001644 // we want to read back from the scratch's origin
1645 left = 0;
1646 top = 0;
bsalomon191bcc02014-11-14 11:31:13 -08001647 target = tempTexture->asRenderTarget();
joshualitt5c55fef2014-10-31 14:04:35 -07001648 }
1649 this->flushSurfaceWrites(target);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001650 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001651 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001652 }
joshualitt5c55fef2014-10-31 14:04:35 -07001653
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001654 if (!fGpu->readPixels(target,
1655 left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001656 readConfig, buffer, rowBytes)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001657 return false;
1658 }
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001659 // Perform any conversions we weren't able to perform using a scratch texture.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001660 if (unpremul || swapRAndB) {
reed@google.com7111d462014-03-25 16:20:24 +00001661 SkDstPixelInfo dstPI;
jvanverthfa1e8a72014-12-22 08:31:49 -08001662 if (!GrPixelConfig2ColorAndProfileType(dstConfig, &dstPI.fColorType, NULL)) {
reed@google.com7111d462014-03-25 16:20:24 +00001663 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001664 }
reed@google.com7111d462014-03-25 16:20:24 +00001665 dstPI.fAlphaType = kUnpremul_SkAlphaType;
1666 dstPI.fPixels = buffer;
1667 dstPI.fRowBytes = rowBytes;
1668
1669 SkSrcPixelInfo srcPI;
1670 srcPI.fColorType = swapRAndB ? toggle_colortype32(dstPI.fColorType) : dstPI.fColorType;
1671 srcPI.fAlphaType = kPremul_SkAlphaType;
1672 srcPI.fPixels = buffer;
1673 srcPI.fRowBytes = rowBytes;
1674
1675 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001676 }
1677 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001678}
1679
bsalomon87a94eb2014-11-03 14:28:32 -08001680void GrContext::prepareSurfaceForExternalRead(GrSurface* surface) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001681 RETURN_IF_ABANDONED
bsalomon87a94eb2014-11-03 14:28:32 -08001682 SkASSERT(surface);
1683 ASSERT_OWNED_RESOURCE(surface);
1684 if (surface->surfacePriv().hasPendingIO()) {
1685 this->flush();
1686 }
1687 GrRenderTarget* rt = surface->asRenderTarget();
1688 if (fGpu && rt) {
1689 fGpu->resolveRenderTarget(rt);
bsalomon41ebbdd2014-08-04 08:31:39 -07001690 }
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001691}
1692
bsalomon41ebbdd2014-08-04 08:31:39 -07001693void GrContext::discardRenderTarget(GrRenderTarget* renderTarget) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001694 RETURN_IF_ABANDONED
bsalomon41ebbdd2014-08-04 08:31:39 -07001695 SkASSERT(renderTarget);
1696 ASSERT_OWNED_RESOURCE(renderTarget);
bsalomonf21dab92014-11-13 13:33:28 -08001697 AutoCheckFlush acf(this);
joshualitt570d2f82015-02-25 13:19:48 -08001698 GrDrawTarget* target = this->prepareToDraw();
bsalomon41ebbdd2014-08-04 08:31:39 -07001699 if (NULL == target) {
1700 return;
1701 }
1702 target->discard(renderTarget);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001703}
1704
bsalomonf80bfed2014-10-07 05:56:02 -07001705void GrContext::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
1706 const SkIPoint& dstPoint, uint32_t pixelOpsFlags) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001707 RETURN_IF_ABANDONED
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001708 if (NULL == src || NULL == dst) {
1709 return;
1710 }
bsalomone3d4bf22014-09-23 09:15:03 -07001711 ASSERT_OWNED_RESOURCE(src);
junov2bb52102014-09-29 10:18:59 -07001712 ASSERT_OWNED_RESOURCE(dst);
Brian Salomon34a98952014-09-24 11:41:24 -04001713
bsalomonf80bfed2014-10-07 05:56:02 -07001714 // Since we're going to the draw target and not GPU, no need to check kNoFlush
1715 // here.
junov96c118e2014-09-26 13:09:47 -07001716
joshualitt570d2f82015-02-25 13:19:48 -08001717 GrDrawTarget* target = this->prepareToDraw();
junov96c118e2014-09-26 13:09:47 -07001718 if (NULL == target) {
1719 return;
1720 }
junov96c118e2014-09-26 13:09:47 -07001721 target->copySurface(dst, src, srcRect, dstPoint);
bsalomonf80bfed2014-10-07 05:56:02 -07001722
1723 if (kFlushWrites_PixelOp & pixelOpsFlags) {
1724 this->flush();
1725 }
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001726}
1727
bsalomonf80bfed2014-10-07 05:56:02 -07001728void GrContext::flushSurfaceWrites(GrSurface* surface) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001729 RETURN_IF_ABANDONED
bsalomonf80bfed2014-10-07 05:56:02 -07001730 if (surface->surfacePriv().hasPendingWrite()) {
1731 this->flush();
1732 }
1733}
1734
egdaniel8dd688b2015-01-22 10:16:09 -08001735GrDrawTarget* GrContext::prepareToDraw(GrPipelineBuilder* pipelineBuilder,
joshualitt25d9c152015-02-18 12:29:52 -08001736 GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -08001737 const GrClip& clip,
joshualitt9853cce2014-11-17 14:22:48 -08001738 const GrPaint* paint,
1739 const AutoCheckFlush* acf) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001740 if (NULL == fGpu || NULL == fDrawBuffer) {
bsalomon41ebbdd2014-08-04 08:31:39 -07001741 return NULL;
1742 }
1743
joshualitt570d2f82015-02-25 13:19:48 -08001744 ASSERT_OWNED_RESOURCE(rt);
1745 SkASSERT(rt && paint && acf);
1746 pipelineBuilder->setFromPaint(*paint, rt, clip);
1747 return fDrawBuffer;
1748}
1749
1750GrDrawTarget* GrContext::prepareToDraw() {
1751 if (NULL == fGpu) {
1752 return NULL;
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001753 }
joshualitt5c55fef2014-10-31 14:04:35 -07001754 return fDrawBuffer;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001755}
1756
robertphillips@google.com72176b22012-05-23 13:19:12 +00001757/*
1758 * This method finds a path renderer that can draw the specified path on
1759 * the provided target.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001760 * Due to its expense, the software path renderer has split out so it can
robertphillips@google.com72176b22012-05-23 13:19:12 +00001761 * can be individually allowed/disallowed via the "allowSW" boolean.
1762 */
joshualitt9853cce2014-11-17 14:22:48 -08001763GrPathRenderer* GrContext::getPathRenderer(const GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -08001764 const GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -08001765 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -08001766 const SkPath& path,
kkinnunen18996512015-04-26 23:18:49 -07001767 const GrStrokeInfo& stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001768 bool allowSW,
1769 GrPathRendererChain::DrawType drawType,
1770 GrPathRendererChain::StencilSupport* stencilSupport) {
1771
bsalomon@google.com30085192011-08-19 15:42:31 +00001772 if (NULL == fPathRendererChain) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001773 fPathRendererChain = SkNEW_ARGS(GrPathRendererChain, (this));
bsalomon@google.com30085192011-08-19 15:42:31 +00001774 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001775
joshualitt9853cce2014-11-17 14:22:48 -08001776 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(target,
egdaniel8dd688b2015-01-22 10:16:09 -08001777 pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -08001778 viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -08001779 path,
sugoi@google.com12b4e272012-12-06 20:13:11 +00001780 stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001781 drawType,
1782 stencilSupport);
robertphillips@google.com72176b22012-05-23 13:19:12 +00001783
1784 if (NULL == pr && allowSW) {
1785 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001786 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001787 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001788 pr = fSoftwarePathRenderer;
1789 }
1790
1791 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001792}
1793
bsalomon@google.com27847de2011-02-22 20:59:41 +00001794////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00001795bool GrContext::isConfigRenderable(GrPixelConfig config, bool withMSAA) const {
1796 return fGpu->caps()->isConfigRenderable(config, withMSAA);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001797}
1798
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +00001799int GrContext::getRecommendedSampleCount(GrPixelConfig config,
1800 SkScalar dpi) const {
1801 if (!this->isConfigRenderable(config, true)) {
1802 return 0;
1803 }
1804 int chosenSampleCount = 0;
jvanverthe9c0fc62015-04-29 11:18:05 -07001805 if (fGpu->caps()->shaderCaps()->pathRenderingSupport()) {
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +00001806 if (dpi >= 250.0f) {
1807 chosenSampleCount = 4;
1808 } else {
1809 chosenSampleCount = 16;
1810 }
1811 }
1812 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ?
1813 chosenSampleCount : 0;
1814}
1815
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001816void GrContext::setupDrawBuffer() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001817 SkASSERT(NULL == fDrawBuffer);
1818 SkASSERT(NULL == fDrawBufferVBAllocPool);
1819 SkASSERT(NULL == fDrawBufferIBAllocPool);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001820
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001821 fDrawBufferVBAllocPool =
bsalomonecb8e3e2015-04-29 04:33:52 -07001822 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu,
1823 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
1824 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001825 fDrawBufferIBAllocPool =
bsalomonecb8e3e2015-04-29 04:33:52 -07001826 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu,
1827 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
1828 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001829
bsalomona73239a2015-04-28 13:35:17 -07001830 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (this,
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001831 fDrawBufferVBAllocPool,
1832 fDrawBufferIBAllocPool));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001833}
1834
bsalomon@google.com21c10c52013-06-13 17:44:07 +00001835GrDrawTarget* GrContext::getTextTarget() {
joshualitt570d2f82015-02-25 13:19:48 -08001836 return this->prepareToDraw();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001837}
1838
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001839namespace {
1840void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
1841 GrConfigConversionEffect::PMConversion pmToUPM;
1842 GrConfigConversionEffect::PMConversion upmToPM;
1843 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
1844 *pmToUPMValue = pmToUPM;
1845 *upmToPMValue = upmToPM;
1846}
1847}
1848
joshualittb0a8a372014-09-23 09:50:21 -07001849const GrFragmentProcessor* GrContext::createPMToUPMEffect(GrTexture* texture,
1850 bool swapRAndB,
1851 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001852 if (!fDidTestPMConversions) {
1853 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001854 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001855 }
1856 GrConfigConversionEffect::PMConversion pmToUPM =
1857 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
1858 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001859 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001860 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001861 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001862 }
1863}
1864
joshualittb0a8a372014-09-23 09:50:21 -07001865const GrFragmentProcessor* GrContext::createUPMToPMEffect(GrTexture* texture,
1866 bool swapRAndB,
1867 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001868 if (!fDidTestPMConversions) {
1869 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001870 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001871 }
1872 GrConfigConversionEffect::PMConversion upmToPM =
1873 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
1874 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001875 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001876 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001877 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001878 }
1879}
1880
bsalomon37f9a262015-02-02 13:00:10 -08001881//////////////////////////////////////////////////////////////////////////////
1882
1883void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
1884 if (maxTextures) {
bsalomon0ea80f42015-02-11 10:49:59 -08001885 *maxTextures = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -08001886 }
1887 if (maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -08001888 *maxTextureBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -08001889 }
1890}
1891
1892void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -08001893 fResourceCache->setLimits(maxTextures, maxTextureBytes);
bsalomon37f9a262015-02-02 13:00:10 -08001894}
1895
bsalomon37f9a262015-02-02 13:00:10 -08001896//////////////////////////////////////////////////////////////////////////////
1897
egdanielbbcb38d2014-06-19 10:19:29 -07001898void GrContext::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
1899 fGpu->addGpuTraceMarker(marker);
bsalomon49f085d2014-09-05 13:34:00 -07001900 if (fDrawBuffer) {
egdanielbbcb38d2014-06-19 10:19:29 -07001901 fDrawBuffer->addGpuTraceMarker(marker);
1902 }
1903}
1904
1905void GrContext::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
1906 fGpu->removeGpuTraceMarker(marker);
bsalomon49f085d2014-09-05 13:34:00 -07001907 if (fDrawBuffer) {
egdanielbbcb38d2014-06-19 10:19:29 -07001908 fDrawBuffer->removeGpuTraceMarker(marker);
1909 }
1910}