blob: a1f1c253a7ce45c5c4f2918891a412d9797e371c [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"
joshualitt0823bfa2015-02-27 10:06:15 -080012#include "GrBatch.h"
joshualitt7c3a2f82015-03-31 13:32:05 -070013#include "GrBatchFontCache.h"
joshualitt0823bfa2015-02-27 10:06:15 -080014#include "GrBatchTarget.h"
joshualitt7c3a2f82015-03-31 13:32:05 -070015#include "GrBitmapTextContext.h"
tomhudson@google.com278cbb42011-06-30 19:37:01 +000016#include "GrBufferAllocPool.h"
joshualitt5478d422014-11-14 16:00:38 -080017#include "GrDefaultGeoProcFactory.h"
jvanverth787cdf92014-12-04 10:46:50 -080018#include "GrFontCache.h"
bsalomon453cf402014-11-11 14:15:57 -080019#include "GrGpuResource.h"
bsalomon3582d3e2015-02-13 14:20:05 -080020#include "GrGpuResourcePriv.h"
kkinnunenc6cb56f2014-06-24 00:12:27 -070021#include "GrDistanceFieldTextContext.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000022#include "GrDrawTargetCaps.h"
joshualitt5478d422014-11-14 16:00:38 -080023#include "GrGpu.h"
commit-bot@chromium.orgdcb8ef92014-03-27 11:26:10 +000024#include "GrIndexBuffer.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000025#include "GrInOrderDrawBuffer.h"
robertphillips@google.come930a072014-04-03 00:34:27 +000026#include "GrLayerCache.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +000027#include "GrOvalRenderer.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000028#include "GrPathRenderer.h"
tomhudson@google.comd22b6e42011-06-24 15:53:40 +000029#include "GrPathUtils.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080030#include "GrRenderTargetPriv.h"
bsalomon0ea80f42015-02-11 10:49:59 -080031#include "GrResourceCache.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000032#include "GrSoftwarePathRenderer.h"
kkinnunenc6cb56f2014-06-24 00:12:27 -070033#include "GrStencilAndCoverTextContext.h"
egdanield58a0ba2014-06-11 10:30:05 -070034#include "GrStrokeInfo.h"
bsalomonafbf2d62014-09-30 12:18:44 -070035#include "GrSurfacePriv.h"
bsalomonafbf2d62014-09-30 12:18:44 -070036#include "GrTexturePriv.h"
egdanielbbcb38d2014-06-19 10:19:29 -070037#include "GrTraceMarker.h"
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +000038#include "GrTracing.h"
egdanield58a0ba2014-06-11 10:30:05 -070039#include "SkDashPathPriv.h"
bsalomon81beccc2014-10-13 12:32:55 -070040#include "SkConfig8888.h"
reed@google.com7111d462014-03-25 16:20:24 +000041#include "SkGr.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000042#include "SkRRect.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000043#include "SkStrokeRec.h"
commit-bot@chromium.orgdcb8ef92014-03-27 11:26:10 +000044#include "SkTLazy.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000045#include "SkTLS.h"
commit-bot@chromium.org933e65d2014-03-20 20:00:24 +000046#include "SkTraceEvent.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000047
joshualitt5478d422014-11-14 16:00:38 -080048#include "effects/GrConfigConversionEffect.h"
49#include "effects/GrDashingEffect.h"
50#include "effects/GrSingleTextureEffect.h"
51
bsalomon@google.com60361492012-03-15 17:47:06 +000052static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
bsalomon@google.com27847de2011-02-22 20:59:41 +000053static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
54
bsalomon@google.com1d4edd32012-08-16 18:36:06 +000055static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = 1 << 11;
56static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = 4;
bsalomon@google.com27847de2011-02-22 20:59:41 +000057
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000058#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
joshualitt5f5a8d72015-02-25 14:09:45 -080059#define RETURN_IF_ABANDONED if (!fDrawBuffer) { return; }
60#define RETURN_FALSE_IF_ABANDONED if (!fDrawBuffer) { return false; }
61#define RETURN_NULL_IF_ABANDONED if (!fDrawBuffer) { return NULL; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000062
bsalomonf21dab92014-11-13 13:33:28 -080063class GrContext::AutoCheckFlush {
64public:
65 AutoCheckFlush(GrContext* context) : fContext(context) { SkASSERT(context); }
66
67 ~AutoCheckFlush() {
68 if (fContext->fFlushToReduceCacheSize) {
69 fContext->flush();
70 }
71 }
72
73private:
74 GrContext* fContext;
75};
76
krajcevski9c6d4d72014-08-12 07:26:25 -070077GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
78 const Options* opts) {
79 GrContext* context;
80 if (NULL == opts) {
81 context = SkNEW_ARGS(GrContext, (Options()));
82 } else {
83 context = SkNEW_ARGS(GrContext, (*opts));
84 }
85
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000086 if (context->init(backend, backendContext)) {
87 return context;
88 } else {
89 context->unref();
90 return NULL;
bsalomon@google.com27847de2011-02-22 20:59:41 +000091 }
bsalomon@google.com27847de2011-02-22 20:59:41 +000092}
93
krajcevski9c6d4d72014-08-12 07:26:25 -070094GrContext::GrContext(const Options& opts) : fOptions(opts) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000095 fGpu = NULL;
96 fPathRendererChain = NULL;
97 fSoftwarePathRenderer = NULL;
bsalomon0ea80f42015-02-11 10:49:59 -080098 fResourceCache = NULL;
joshualitt7c3a2f82015-03-31 13:32:05 -070099 fBatchFontCache = NULL;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000100 fFontCache = NULL;
101 fDrawBuffer = NULL;
102 fDrawBufferVBAllocPool = NULL;
103 fDrawBufferIBAllocPool = NULL;
bsalomonf21dab92014-11-13 13:33:28 -0800104 fFlushToReduceCacheSize = false;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000105 fAARectRenderer = NULL;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000106 fOvalRenderer = NULL;
robertphillips@google.com44a91dc2013-07-25 15:32:06 +0000107 fMaxTextureSizeOverride = 1 << 20;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000108}
109
110bool GrContext::init(GrBackend backend, GrBackendContext backendContext) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000111 SkASSERT(NULL == fGpu);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000112
113 fGpu = GrGpu::Create(backend, backendContext, this);
114 if (NULL == fGpu) {
115 return false;
116 }
bsalomon33435572014-11-05 14:47:41 -0800117 this->initCommon();
118 return true;
119}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000120
bsalomon33435572014-11-05 14:47:41 -0800121void GrContext::initCommon() {
bsalomon0ea80f42015-02-11 10:49:59 -0800122 fResourceCache = SkNEW(GrResourceCache);
123 fResourceCache->setOverBudgetCallback(OverBudgetCB, this);
commit-bot@chromium.org1836d332013-07-16 22:55:03 +0000124
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000125 fFontCache = SkNEW_ARGS(GrFontCache, (fGpu));
126
robertphillips4ec84da2014-06-24 13:10:43 -0700127 fLayerCache.reset(SkNEW_ARGS(GrLayerCache, (this)));
robertphillips@google.come930a072014-04-03 00:34:27 +0000128
joshualittb44293e2014-10-28 08:12:18 -0700129 fAARectRenderer = SkNEW_ARGS(GrAARectRenderer, (fGpu));
joshualitt5531d512014-12-17 15:50:11 -0800130 fOvalRenderer = SkNEW_ARGS(GrOvalRenderer, (fGpu));
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000131
132 fDidTestPMConversions = false;
133
134 this->setupDrawBuffer();
joshualitt7c3a2f82015-03-31 13:32:05 -0700135
136 // GrBatchFontCache will eventually replace GrFontCache
137 fBatchFontCache = SkNEW(GrBatchFontCache);
138 fBatchFontCache->init(this);
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000139}
140
bsalomon@google.com27847de2011-02-22 20:59:41 +0000141GrContext::~GrContext() {
bsalomon@google.com733c0622013-04-24 17:59:32 +0000142 if (NULL == fGpu) {
143 return;
144 }
145
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000146 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000147
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000148 for (int i = 0; i < fCleanUpData.count(); ++i) {
149 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
150 }
151
bsalomon0ea80f42015-02-11 10:49:59 -0800152 SkDELETE(fResourceCache);
joshualitt7c3a2f82015-03-31 13:32:05 -0700153 SkDELETE(fBatchFontCache);
bsalomon33435572014-11-05 14:47:41 -0800154 SkDELETE(fFontCache);
155 SkDELETE(fDrawBuffer);
156 SkDELETE(fDrawBufferVBAllocPool);
157 SkDELETE(fDrawBufferIBAllocPool);
bsalomon@google.com30085192011-08-19 15:42:31 +0000158
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000159 fAARectRenderer->unref();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000160 fOvalRenderer->unref();
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000161
bsalomon@google.com205d4602011-04-25 12:43:45 +0000162 fGpu->unref();
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000163 SkSafeUnref(fPathRendererChain);
164 SkSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000165}
166
bsalomon2354f842014-07-28 13:48:36 -0700167void GrContext::abandonContext() {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000168 // abandon first to so destructors
169 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800170 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700171
robertphillipse3371302014-09-17 06:01:06 -0700172 fGpu->contextAbandoned();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000173
bsalomon@google.com30085192011-08-19 15:42:31 +0000174 // a path renderer may be holding onto resources that
175 // are now unusable
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000176 SkSafeSetNull(fPathRendererChain);
177 SkSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000178
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000179 delete fDrawBuffer;
180 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000181
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000182 delete fDrawBufferVBAllocPool;
183 fDrawBufferVBAllocPool = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000184
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000185 delete fDrawBufferIBAllocPool;
186 fDrawBufferIBAllocPool = NULL;
187
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000188 fAARectRenderer->reset();
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000189 fOvalRenderer->reset();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000190
joshualitt7c3a2f82015-03-31 13:32:05 -0700191 fBatchFontCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000192 fFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000193 fLayerCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000194}
195
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000196void GrContext::resetContext(uint32_t state) {
197 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000198}
199
200void GrContext::freeGpuResources() {
201 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000202
bsalomon49f085d2014-09-05 13:34:00 -0700203 if (fDrawBuffer) {
bsalomonc8dc1f72014-08-21 13:02:13 -0700204 fDrawBuffer->purgeResources();
205 }
robertphillips@google.comff175842012-05-14 19:31:39 +0000206
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000207 fAARectRenderer->reset();
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000208 fOvalRenderer->reset();
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000209
joshualitt7c3a2f82015-03-31 13:32:05 -0700210 fBatchFontCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000211 fFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000212 fLayerCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000213 // a path renderer may be holding onto resources
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000214 SkSafeSetNull(fPathRendererChain);
215 SkSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000216}
217
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000218void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
bsalomon71cb0c22014-11-14 12:10:14 -0800219 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800220 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800221 }
222 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800223 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800224 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000225}
226
kkinnunenc6cb56f2014-06-24 00:12:27 -0700227GrTextContext* GrContext::createTextContext(GrRenderTarget* renderTarget,
joshualitt6e8cd962015-03-20 10:30:14 -0700228 SkGpuDevice* gpuDevice,
kkinnunenc6cb56f2014-06-24 00:12:27 -0700229 const SkDeviceProperties&
230 leakyProperties,
231 bool enableDistanceFieldFonts) {
bsalomon6bc1b5f2015-02-23 09:06:38 -0800232 if (fGpu->caps()->pathRenderingSupport() && renderTarget->isMultisampled()) {
233 GrStencilBuffer* sb = renderTarget->renderTargetPriv().attachStencilBuffer();
234 if (sb) {
joshualitt6e8cd962015-03-20 10:30:14 -0700235 return GrStencilAndCoverTextContext::Create(this, gpuDevice, leakyProperties);
bsalomon6bc1b5f2015-02-23 09:06:38 -0800236 }
jvanverth8c27a182014-10-14 08:45:50 -0700237 }
238
joshualitt7c3a2f82015-03-31 13:32:05 -0700239#ifdef USE_BITMAP_TEXTBLOBS
240 return GrBitmapTextContextB::Create(this, gpuDevice, leakyProperties);
241#else
joshualitt6e8cd962015-03-20 10:30:14 -0700242 return GrDistanceFieldTextContext::Create(this, gpuDevice, leakyProperties,
243 enableDistanceFieldFonts);
joshualitt7c3a2f82015-03-31 13:32:05 -0700244#endif
kkinnunenc6cb56f2014-06-24 00:12:27 -0700245}
246
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000247////////////////////////////////////////////////////////////////////////////////
bsalomond0423582015-02-06 08:49:24 -0800248enum ScratchTextureFlags {
249 kExact_ScratchTextureFlag = 0x1,
250 kNoPendingIO_ScratchTextureFlag = 0x2,
251 kNoCreate_ScratchTextureFlag = 0x4,
252};
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000253
bsalomond2a6f4e2015-02-04 10:55:54 -0800254bool GrContext::isConfigTexturable(GrPixelConfig config) const {
255 return fGpu->caps()->isConfigTexturable(config);
256}
257
258bool GrContext::npotTextureTileSupport() const {
259 return fGpu->caps()->npotTextureTileSupport();
260}
261
bsalomond0423582015-02-06 08:49:24 -0800262GrTexture* GrContext::createTexture(const GrSurfaceDesc& desc, bool budgeted, const void* srcData,
bsalomon37f9a262015-02-02 13:00:10 -0800263 size_t rowBytes) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800264 RETURN_NULL_IF_ABANDONED
bsalomond0423582015-02-06 08:49:24 -0800265 if ((desc.fFlags & kRenderTarget_GrSurfaceFlag) &&
266 !this->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
bsalomond2a6f4e2015-02-04 10:55:54 -0800267 return NULL;
268 }
bsalomond0423582015-02-06 08:49:24 -0800269 if (!GrPixelConfigIsCompressed(desc.fConfig)) {
270 static const uint32_t kFlags = kExact_ScratchTextureFlag |
271 kNoCreate_ScratchTextureFlag;
272 if (GrTexture* texture = this->internalRefScratchTexture(desc, kFlags)) {
273 if (!srcData || texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
274 srcData, rowBytes)) {
275 if (!budgeted) {
bsalomon3582d3e2015-02-13 14:20:05 -0800276 texture->resourcePriv().makeUnbudgeted();
bsalomond0423582015-02-06 08:49:24 -0800277 }
278 return texture;
279 }
280 texture->unref();
281 }
282 }
283 return fGpu->createTexture(desc, budgeted, srcData, rowBytes);
284}
bsalomond2a6f4e2015-02-04 10:55:54 -0800285
bsalomond0423582015-02-06 08:49:24 -0800286GrTexture* GrContext::refScratchTexture(const GrSurfaceDesc& desc, ScratchTexMatch match,
287 bool calledDuringFlush) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800288 RETURN_NULL_IF_ABANDONED
bsalomond0423582015-02-06 08:49:24 -0800289 // Currently we don't recycle compressed textures as scratch.
290 if (GrPixelConfigIsCompressed(desc.fConfig)) {
291 return NULL;
292 } else {
293 uint32_t flags = 0;
294 if (kExact_ScratchTexMatch == match) {
295 flags |= kExact_ScratchTextureFlag;
296 }
297 if (calledDuringFlush) {
298 flags |= kNoPendingIO_ScratchTextureFlag;
299 }
300 return this->internalRefScratchTexture(desc, flags);
301 }
302}
303
304GrTexture* GrContext::internalRefScratchTexture(const GrSurfaceDesc& inDesc, uint32_t flags) {
305 SkASSERT(!GrPixelConfigIsCompressed(inDesc.fConfig));
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000306
bsalomonf2703d82014-10-28 14:33:06 -0700307 SkTCopyOnFirstWrite<GrSurfaceDesc> desc(inDesc);
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000308
bsalomonf2703d82014-10-28 14:33:06 -0700309 if (fGpu->caps()->reuseScratchTextures() || (desc->fFlags & kRenderTarget_GrSurfaceFlag)) {
bsalomond0423582015-02-06 08:49:24 -0800310 if (!(kExact_ScratchTextureFlag & flags)) {
bsalomonbcf0a522014-10-08 08:40:09 -0700311 // bin by pow2 with a reasonable min
312 static const int MIN_SIZE = 16;
bsalomonf2703d82014-10-28 14:33:06 -0700313 GrSurfaceDesc* wdesc = desc.writable();
bsalomonbcf0a522014-10-08 08:40:09 -0700314 wdesc->fWidth = SkTMax(MIN_SIZE, GrNextPow2(desc->fWidth));
315 wdesc->fHeight = SkTMax(MIN_SIZE, GrNextPow2(desc->fHeight));
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000316 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000317
bsalomon6bc1b5f2015-02-23 09:06:38 -0800318 GrScratchKey key;
319 GrTexturePriv::ComputeScratchKey(*desc, &key);
320 uint32_t scratchFlags = 0;
321 if (kNoPendingIO_ScratchTextureFlag & flags) {
322 scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag;
323 } else if (!(desc->fFlags & kRenderTarget_GrSurfaceFlag)) {
324 // If it is not a render target then it will most likely be populated by
325 // writePixels() which will trigger a flush if the texture has pending IO.
326 scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag;
327 }
328 GrGpuResource* resource = fResourceCache->findAndRefScratchResource(key, scratchFlags);
329 if (resource) {
330 GrSurface* surface = static_cast<GrSurface*>(resource);
331 GrRenderTarget* rt = surface->asRenderTarget();
332 if (rt && fGpu->caps()->discardRenderTargetSupport()) {
333 rt->discard();
bsalomon000f8292014-10-15 19:04:14 -0700334 }
bsalomon6bc1b5f2015-02-23 09:06:38 -0800335 return surface->asTexture();
336 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000337 }
338
bsalomond0423582015-02-06 08:49:24 -0800339 if (!(kNoCreate_ScratchTextureFlag & flags)) {
bsalomon3582d3e2015-02-13 14:20:05 -0800340 return fGpu->createTexture(*desc, true, NULL, 0);
bsalomone167f962015-01-27 09:56:04 -0800341 }
bsalomond0423582015-02-06 08:49:24 -0800342
343 return NULL;
Brian Salomon9323b8b2014-10-07 15:07:38 -0400344}
345
bsalomon71cb0c22014-11-14 12:10:14 -0800346void GrContext::OverBudgetCB(void* data) {
bsalomon66a450f2014-11-13 13:19:10 -0800347 SkASSERT(data);
bsalomonf21dab92014-11-13 13:33:28 -0800348
bsalomon66a450f2014-11-13 13:19:10 -0800349 GrContext* context = reinterpret_cast<GrContext*>(data);
bsalomonf21dab92014-11-13 13:33:28 -0800350
351 // Flush the InOrderDrawBuffer to possibly free up some textures
352 context->fFlushToReduceCacheSize = true;
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000353}
354
bsalomon@google.com91958362011-06-13 17:58:13 +0000355int GrContext::getMaxTextureSize() const {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000356 return SkTMin(fGpu->caps()->maxTextureSize(), fMaxTextureSizeOverride);
bsalomon@google.com91958362011-06-13 17:58:13 +0000357}
358
359int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000360 return fGpu->caps()->maxRenderTargetSize();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000361}
362
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000363int GrContext::getMaxSampleCount() const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000364 return fGpu->caps()->maxSampleCount();
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000365}
366
bsalomon@google.com27847de2011-02-22 20:59:41 +0000367///////////////////////////////////////////////////////////////////////////////
368
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000369GrTexture* GrContext::wrapBackendTexture(const GrBackendTextureDesc& desc) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800370 RETURN_NULL_IF_ABANDONED
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000371 return fGpu->wrapBackendTexture(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000372}
373
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000374GrRenderTarget* GrContext::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800375 RETURN_NULL_IF_ABANDONED
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000376 return fGpu->wrapBackendRenderTarget(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000377}
378
bsalomon@google.com27847de2011-02-22 20:59:41 +0000379////////////////////////////////////////////////////////////////////////////////
380
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000381void GrContext::clear(const SkIRect* rect,
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000382 const GrColor color,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000383 bool canIgnoreRect,
bsalomon41ebbdd2014-08-04 08:31:39 -0700384 GrRenderTarget* renderTarget) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800385 RETURN_IF_ABANDONED
bsalomon41ebbdd2014-08-04 08:31:39 -0700386 ASSERT_OWNED_RESOURCE(renderTarget);
bsalomon89c62982014-11-03 12:08:42 -0800387 SkASSERT(renderTarget);
388
bsalomonf21dab92014-11-13 13:33:28 -0800389 AutoCheckFlush acf(this);
egdanield78a1682014-07-09 10:41:26 -0700390 GR_CREATE_TRACE_MARKER_CONTEXT("GrContext::clear", this);
joshualitt570d2f82015-02-25 13:19:48 -0800391 GrDrawTarget* target = this->prepareToDraw();
bsalomon41ebbdd2014-08-04 08:31:39 -0700392 if (NULL == target) {
393 return;
394 }
395 target->clear(rect, color, canIgnoreRect, renderTarget);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000396}
397
joshualitt25d9c152015-02-18 12:29:52 -0800398void GrContext::drawPaint(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800399 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800400 const GrPaint& origPaint,
401 const SkMatrix& viewMatrix) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800402 RETURN_IF_ABANDONED
bsalomon@google.com27847de2011-02-22 20:59:41 +0000403 // set rect to be big enough to fill the space, but not super-huge, so we
404 // don't overflow fixed-point implementations
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000405 SkRect r;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000406 r.setLTRB(0, 0,
joshualitt25d9c152015-02-18 12:29:52 -0800407 SkIntToScalar(rt->width()),
408 SkIntToScalar(rt->height()));
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000409 SkTCopyOnFirstWrite<GrPaint> paint(origPaint);
joshualitt8fc6c2d2014-12-22 15:27:05 -0800410
411 // by definition this fills the entire clip, no need for AA
412 if (paint->isAntiAlias()) {
413 paint.writable()->setAntiAlias(false);
414 }
415
416 bool isPerspective = viewMatrix.hasPerspective();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000417
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000418 // We attempt to map r by the inverse matrix and draw that. mapRect will
419 // map the four corners and bound them with a new rect. This will not
420 // produce a correct result for some perspective matrices.
joshualitt8fc6c2d2014-12-22 15:27:05 -0800421 if (!isPerspective) {
joshualitt5531d512014-12-17 15:50:11 -0800422 SkMatrix inverse;
423 if (!viewMatrix.invert(&inverse)) {
tfarina38406c82014-10-31 07:11:12 -0700424 SkDebugf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000425 return;
426 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000427 inverse.mapRect(&r);
joshualitt570d2f82015-02-25 13:19:48 -0800428 this->drawRect(rt, clip, *paint, viewMatrix, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000429 } else {
joshualitt8fc6c2d2014-12-22 15:27:05 -0800430 SkMatrix localMatrix;
431 if (!viewMatrix.invert(&localMatrix)) {
tfarina38406c82014-10-31 07:11:12 -0700432 SkDebugf("Could not invert matrix\n");
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000433 return;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000434 }
joshualitt8fc6c2d2014-12-22 15:27:05 -0800435
436 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -0800437 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -0800438 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, paint, &acf);
joshualitt8fc6c2d2014-12-22 15:27:05 -0800439 if (NULL == target) {
440 return;
441 }
442
443 GR_CREATE_TRACE_MARKER("GrContext::drawPaintWithPerspective", target);
joshualitt44701df2015-02-23 14:44:57 -0800444 target->drawRect(&pipelineBuilder,
445 paint->getColor(),
446 SkMatrix::I(),
447 r,
448 NULL,
449 &localMatrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000450 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000451}
452
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000453#ifdef SK_DEVELOPER
454void GrContext::dumpFontCache() const {
455 fFontCache->dump();
456}
457#endif
458
bsalomon@google.com205d4602011-04-25 12:43:45 +0000459////////////////////////////////////////////////////////////////////////////////
460
bsalomonc30aaa02014-08-13 07:15:29 -0700461static inline bool is_irect(const SkRect& r) {
tfarina38406c82014-10-31 07:11:12 -0700462 return SkScalarIsInt(r.fLeft) && SkScalarIsInt(r.fTop) &&
463 SkScalarIsInt(r.fRight) && SkScalarIsInt(r.fBottom);
bsalomonc30aaa02014-08-13 07:15:29 -0700464}
465
bsalomon@google.com205d4602011-04-25 12:43:45 +0000466static bool apply_aa_to_rect(GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -0800467 GrPipelineBuilder* pipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -0800468 SkRect* devBoundRect,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000469 const SkRect& rect,
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000470 SkScalar strokeWidth,
joshualitt2e3b3e32014-12-09 13:31:14 -0800471 const SkMatrix& combinedMatrix,
472 GrColor color) {
egdaniel8dd688b2015-01-22 10:16:09 -0800473 if (pipelineBuilder->getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000474 return false;
475 }
476
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000477#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000478 if (strokeWidth >= 0) {
479#endif
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000480 if (!combinedMatrix.preservesAxisAlignment()) {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000481 return false;
482 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000483
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000484#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000485 } else {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000486 if (!combinedMatrix.preservesRightAngles()) {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000487 return false;
488 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000489 }
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000490#endif
bsalomon@google.com205d4602011-04-25 12:43:45 +0000491
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000492 combinedMatrix.mapRect(devBoundRect, rect);
derekfe638d1c2014-12-02 13:51:29 -0800493 if (!combinedMatrix.rectStaysRect()) {
494 return true;
495 }
496
bsalomonc30aaa02014-08-13 07:15:29 -0700497 if (strokeWidth < 0) {
498 return !is_irect(*devBoundRect);
499 }
robertphillips@google.com28ac96e2013-05-13 13:38:35 +0000500
bsalomon9c0822a2014-08-11 11:07:48 -0700501 return true;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000502}
503
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000504static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) {
505 return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
506 point.fY >= rect.fTop && point.fY <= rect.fBottom;
507}
508
joshualitt0823bfa2015-02-27 10:06:15 -0800509class StrokeRectBatch : public GrBatch {
510public:
511 struct Geometry {
512 GrColor fColor;
513 SkMatrix fViewMatrix;
514 SkRect fRect;
515 SkScalar fStrokeWidth;
516 };
517
518 static GrBatch* Create(const Geometry& geometry) {
519 return SkNEW_ARGS(StrokeRectBatch, (geometry));
520 }
521
mtklein36352bf2015-03-25 18:17:31 -0700522 const char* name() const override { return "StrokeRectBatch"; }
joshualitt0823bfa2015-02-27 10:06:15 -0800523
mtklein36352bf2015-03-25 18:17:31 -0700524 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt0823bfa2015-02-27 10:06:15 -0800525 // When this is called on a batch, there is only one geometry bundle
526 out->setKnownFourComponents(fGeoData[0].fColor);
527 }
528
mtklein36352bf2015-03-25 18:17:31 -0700529 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualitt0823bfa2015-02-27 10:06:15 -0800530 out->setKnownSingleComponent(0xff);
531 }
532
mtklein36352bf2015-03-25 18:17:31 -0700533 void initBatchTracker(const GrPipelineInfo& init) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800534 // Handle any color overrides
535 if (init.fColorIgnored) {
536 fGeoData[0].fColor = GrColor_ILLEGAL;
537 } else if (GrColor_ILLEGAL != init.fOverrideColor) {
538 fGeoData[0].fColor = init.fOverrideColor;
539 }
540
541 // setup batch properties
542 fBatch.fColorIgnored = init.fColorIgnored;
543 fBatch.fColor = fGeoData[0].fColor;
544 fBatch.fUsesLocalCoords = init.fUsesLocalCoords;
545 fBatch.fCoverageIgnored = init.fCoverageIgnored;
546 }
547
mtklein36352bf2015-03-25 18:17:31 -0700548 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800549 SkAutoTUnref<const GrGeometryProcessor> gp(
550 GrDefaultGeoProcFactory::Create(GrDefaultGeoProcFactory::kPosition_GPType,
551 this->color(),
552 this->viewMatrix(),
553 SkMatrix::I()));
554
555 batchTarget->initDraw(gp, pipeline);
556
557 // TODO this is hacky, but the only way we have to initialize the GP is to use the
558 // GrPipelineInfo struct so we can generate the correct shader. Once we have GrBatch
559 // everywhere we can remove this nastiness
560 GrPipelineInfo init;
561 init.fColorIgnored = fBatch.fColorIgnored;
562 init.fOverrideColor = GrColor_ILLEGAL;
563 init.fCoverageIgnored = fBatch.fCoverageIgnored;
564 init.fUsesLocalCoords = this->usesLocalCoords();
565 gp->initBatchTracker(batchTarget->currentBatchTracker(), init);
566
567 size_t vertexStride = gp->getVertexStride();
568
569 SkASSERT(vertexStride == sizeof(GrDefaultGeoProcFactory::PositionAttr));
570
571 Geometry& args = fGeoData[0];
572
573 int vertexCount = kVertsPerHairlineRect;
574 if (args.fStrokeWidth > 0) {
575 vertexCount = kVertsPerStrokeRect;
576 }
577
578 const GrVertexBuffer* vertexBuffer;
579 int firstVertex;
580
581 void* vertices = batchTarget->vertexPool()->makeSpace(vertexStride,
582 vertexCount,
583 &vertexBuffer,
584 &firstVertex);
585
joshualitt4b31de82015-03-05 14:33:41 -0800586 if (!vertices) {
587 SkDebugf("Could not allocate vertices\n");
588 return;
589 }
590
joshualitt0823bfa2015-02-27 10:06:15 -0800591 SkPoint* vertex = reinterpret_cast<SkPoint*>(vertices);
592
593 GrPrimitiveType primType;
594
595 if (args.fStrokeWidth > 0) {;
596 primType = kTriangleStrip_GrPrimitiveType;
597 args.fRect.sort();
598 this->setStrokeRectStrip(vertex, args.fRect, args.fStrokeWidth);
599 } else {
600 // hairline
601 primType = kLineStrip_GrPrimitiveType;
602 vertex[0].set(args.fRect.fLeft, args.fRect.fTop);
603 vertex[1].set(args.fRect.fRight, args.fRect.fTop);
604 vertex[2].set(args.fRect.fRight, args.fRect.fBottom);
605 vertex[3].set(args.fRect.fLeft, args.fRect.fBottom);
606 vertex[4].set(args.fRect.fLeft, args.fRect.fTop);
607 }
608
609 GrDrawTarget::DrawInfo drawInfo;
610 drawInfo.setPrimitiveType(primType);
611 drawInfo.setVertexBuffer(vertexBuffer);
612 drawInfo.setStartVertex(firstVertex);
613 drawInfo.setVertexCount(vertexCount);
614 drawInfo.setStartIndex(0);
615 drawInfo.setIndexCount(0);
616 drawInfo.setInstanceCount(0);
617 drawInfo.setVerticesPerInstance(0);
618 drawInfo.setIndicesPerInstance(0);
619 batchTarget->draw(drawInfo);
620 }
621
622 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
623
624private:
625 StrokeRectBatch(const Geometry& geometry) {
626 this->initClassID<StrokeRectBatch>();
627
628 fBatch.fHairline = geometry.fStrokeWidth == 0;
629
630 fGeoData.push_back(geometry);
631 }
632
633 /* create a triangle strip that strokes the specified rect. There are 8
634 unique vertices, but we repeat the last 2 to close up. Alternatively we
635 could use an indices array, and then only send 8 verts, but not sure that
636 would be faster.
637 */
638 void setStrokeRectStrip(SkPoint verts[10], const SkRect& rect, SkScalar width) {
639 const SkScalar rad = SkScalarHalf(width);
640 // TODO we should be able to enable this assert, but we'd have to filter these draws
641 // this is a bug
642 //SkASSERT(rad < rect.width() / 2 && rad < rect.height() / 2);
643
644 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
645 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
646 verts[2].set(rect.fRight - rad, rect.fTop + rad);
647 verts[3].set(rect.fRight + rad, rect.fTop - rad);
648 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
649 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
650 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
651 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
652 verts[8] = verts[0];
653 verts[9] = verts[1];
654 }
655
656
657 GrColor color() const { return fBatch.fColor; }
658 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
659 bool colorIgnored() const { return fBatch.fColorIgnored; }
660 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
661 bool hairline() const { return fBatch.fHairline; }
662
mtklein36352bf2015-03-25 18:17:31 -0700663 bool onCombineIfPossible(GrBatch* t) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800664 // StrokeRectBatch* that = t->cast<StrokeRectBatch>();
665
666 // NonAA stroke rects cannot batch right now
667 // TODO make these batchable
668 return false;
669 }
670
671 struct BatchTracker {
672 GrColor fColor;
673 bool fUsesLocalCoords;
674 bool fColorIgnored;
675 bool fCoverageIgnored;
676 bool fHairline;
677 };
678
679 const static int kVertsPerHairlineRect = 5;
680 const static int kVertsPerStrokeRect = 10;
681
682 BatchTracker fBatch;
683 SkSTArray<1, Geometry, true> fGeoData;
684};
685
joshualitt25d9c152015-02-18 12:29:52 -0800686void GrContext::drawRect(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800687 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800688 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -0800689 const SkMatrix& viewMatrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000690 const SkRect& rect,
bsalomon01c8da12014-08-04 09:21:30 -0700691 const GrStrokeInfo* strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800692 RETURN_IF_ABANDONED
bsalomon49f085d2014-09-05 13:34:00 -0700693 if (strokeInfo && strokeInfo->isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700694 SkPath path;
695 path.addRect(rect);
joshualitt570d2f82015-02-25 13:19:48 -0800696 this->drawPath(rt, clip, paint, viewMatrix, path, *strokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -0700697 return;
698 }
699
bsalomonf21dab92014-11-13 13:33:28 -0800700 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -0800701 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -0800702 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700703 if (NULL == target) {
704 return;
705 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000706
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000707 GR_CREATE_TRACE_MARKER("GrContext::drawRect", target);
egdanield58a0ba2014-06-11 10:30:05 -0700708 SkScalar width = NULL == strokeInfo ? -1 : strokeInfo->getStrokeRec().getWidth();
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000709
710 // Check if this is a full RT draw and can be replaced with a clear. We don't bother checking
711 // cases where the RT is fully inside a stroke.
712 if (width < 0) {
713 SkRect rtRect;
egdaniel8dd688b2015-01-22 10:16:09 -0800714 pipelineBuilder.getRenderTarget()->getBoundsRect(&rtRect);
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000715 SkRect clipSpaceRTRect = rtRect;
joshualitt570d2f82015-02-25 13:19:48 -0800716 bool checkClip = GrClip::kWideOpen_ClipType != clip.clipType();
joshualitt44701df2015-02-23 14:44:57 -0800717 if (checkClip) {
joshualitt570d2f82015-02-25 13:19:48 -0800718 clipSpaceRTRect.offset(SkIntToScalar(clip.origin().fX),
719 SkIntToScalar(clip.origin().fY));
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000720 }
721 // Does the clip contain the entire RT?
joshualitt570d2f82015-02-25 13:19:48 -0800722 if (!checkClip || clip.quickContains(clipSpaceRTRect)) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000723 SkMatrix invM;
joshualitt8059eb92014-12-29 15:10:07 -0800724 if (!viewMatrix.invert(&invM)) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000725 return;
726 }
727 // Does the rect bound the RT?
728 SkPoint srcSpaceRTQuad[4];
729 invM.mapRectToQuad(srcSpaceRTQuad, rtRect);
730 if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) &&
731 rect_contains_inclusive(rect, srcSpaceRTQuad[1]) &&
732 rect_contains_inclusive(rect, srcSpaceRTQuad[2]) &&
733 rect_contains_inclusive(rect, srcSpaceRTQuad[3])) {
734 // Will it blend?
735 GrColor clearColor;
736 if (paint.isOpaqueAndConstantColor(&clearColor)) {
joshualitt25d9c152015-02-18 12:29:52 -0800737 target->clear(NULL, clearColor, true, rt);
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000738 return;
739 }
740 }
741 }
742 }
743
joshualitt2e3b3e32014-12-09 13:31:14 -0800744 GrColor color = paint.getColor();
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000745 SkRect devBoundRect;
egdaniel8dd688b2015-01-22 10:16:09 -0800746 bool needAA = paint.isAntiAlias() && !pipelineBuilder.getRenderTarget()->isMultisampled();
747 bool doAA = needAA && apply_aa_to_rect(target, &pipelineBuilder, &devBoundRect, rect, width,
joshualitt8059eb92014-12-29 15:10:07 -0800748 viewMatrix, color);
egdanield58a0ba2014-06-11 10:30:05 -0700749
bsalomon@google.com205d4602011-04-25 12:43:45 +0000750 if (doAA) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000751 if (width >= 0) {
bsalomon395ef052014-11-12 11:35:22 -0800752 const SkStrokeRec& strokeRec = strokeInfo->getStrokeRec();
joshualitt9853cce2014-11-17 14:22:48 -0800753 fAARectRenderer->strokeAARect(target,
egdaniel8dd688b2015-01-22 10:16:09 -0800754 &pipelineBuilder,
joshualitt2e3b3e32014-12-09 13:31:14 -0800755 color,
joshualitt8059eb92014-12-29 15:10:07 -0800756 viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800757 rect,
joshualitt9853cce2014-11-17 14:22:48 -0800758 devBoundRect,
759 strokeRec);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000760 } else {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000761 // filled AA rect
joshualitt8059eb92014-12-29 15:10:07 -0800762 fAARectRenderer->fillAARect(target,
egdaniel8dd688b2015-01-22 10:16:09 -0800763 &pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800764 color,
765 viewMatrix,
766 rect,
joshualittd27f73e2014-12-29 07:43:36 -0800767 devBoundRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000768 }
769 return;
770 }
771
bsalomon@google.com27847de2011-02-22 20:59:41 +0000772 if (width >= 0) {
joshualitt0823bfa2015-02-27 10:06:15 -0800773 StrokeRectBatch::Geometry geometry;
774 geometry.fViewMatrix = viewMatrix;
775 geometry.fColor = color;
776 geometry.fRect = rect;
777 geometry.fStrokeWidth = width;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000778
joshualitt0823bfa2015-02-27 10:06:15 -0800779 SkAutoTUnref<GrBatch> batch(StrokeRectBatch::Create(geometry));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000780
joshualitt0823bfa2015-02-27 10:06:15 -0800781 SkRect bounds = rect;
782 SkScalar rad = SkScalarHalf(width);
783 bounds.outset(rad, rad);
784 viewMatrix.mapRect(&bounds);
785 target->drawBatch(&pipelineBuilder, batch, &bounds);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000786 } else {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000787 // filled BW rect
egdaniel8dd688b2015-01-22 10:16:09 -0800788 target->drawSimpleRect(&pipelineBuilder, color, viewMatrix, rect);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000789 }
790}
791
joshualitt25d9c152015-02-18 12:29:52 -0800792void GrContext::drawNonAARectToRect(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800793 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800794 const GrPaint& paint,
joshualitt16b27892014-12-18 07:47:16 -0800795 const SkMatrix& viewMatrix,
796 const SkRect& rectToDraw,
797 const SkRect& localRect,
798 const SkMatrix* localMatrix) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800799 RETURN_IF_ABANDONED
bsalomonf21dab92014-11-13 13:33:28 -0800800 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -0800801 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -0800802 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700803 if (NULL == target) {
804 return;
805 }
bsalomon@google.com64386952013-02-08 21:22:44 +0000806
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000807 GR_CREATE_TRACE_MARKER("GrContext::drawRectToRect", target);
808
joshualitt44701df2015-02-23 14:44:57 -0800809 target->drawRect(&pipelineBuilder,
810 paint.getColor(),
811 viewMatrix,
812 rectToDraw,
813 &localRect,
egdaniel8dd688b2015-01-22 10:16:09 -0800814 localMatrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000815}
816
joshualitt0823bfa2015-02-27 10:06:15 -0800817static const GrGeometryProcessor* set_vertex_attributes(bool hasLocalCoords,
818 bool hasColors,
joshualitt56995b52014-12-11 15:44:02 -0800819 int* colorOffset,
820 int* texOffset,
joshualitt8059eb92014-12-29 15:10:07 -0800821 GrColor color,
822 const SkMatrix& viewMatrix) {
robertphillips@google.com42903302013-04-20 12:26:07 +0000823 *texOffset = -1;
824 *colorOffset = -1;
joshualitt5478d422014-11-14 16:00:38 -0800825 uint32_t flags = GrDefaultGeoProcFactory::kPosition_GPType;
joshualitt0823bfa2015-02-27 10:06:15 -0800826 if (hasLocalCoords && hasColors) {
joshualitt5478d422014-11-14 16:00:38 -0800827 *colorOffset = sizeof(SkPoint);
828 *texOffset = sizeof(SkPoint) + sizeof(GrColor);
829 flags |= GrDefaultGeoProcFactory::kColor_GPType |
830 GrDefaultGeoProcFactory::kLocalCoord_GPType;
joshualitt0823bfa2015-02-27 10:06:15 -0800831 } else if (hasLocalCoords) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000832 *texOffset = sizeof(SkPoint);
joshualitt5478d422014-11-14 16:00:38 -0800833 flags |= GrDefaultGeoProcFactory::kLocalCoord_GPType;
joshualitt0823bfa2015-02-27 10:06:15 -0800834 } else if (hasColors) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000835 *colorOffset = sizeof(SkPoint);
joshualitt5478d422014-11-14 16:00:38 -0800836 flags |= GrDefaultGeoProcFactory::kColor_GPType;
robertphillips@google.com42903302013-04-20 12:26:07 +0000837 }
joshualitt8059eb92014-12-29 15:10:07 -0800838 return GrDefaultGeoProcFactory::Create(flags, color, viewMatrix, SkMatrix::I());
robertphillips@google.com42903302013-04-20 12:26:07 +0000839}
840
joshualitt0823bfa2015-02-27 10:06:15 -0800841class DrawVerticesBatch : public GrBatch {
842public:
843 struct Geometry {
844 GrColor fColor;
845 SkTDArray<SkPoint> fPositions;
846 SkTDArray<uint16_t> fIndices;
847 SkTDArray<GrColor> fColors;
848 SkTDArray<SkPoint> fLocalCoords;
849 };
850
851 static GrBatch* Create(const Geometry& geometry, GrPrimitiveType primitiveType,
852 const SkMatrix& viewMatrix,
853 const SkPoint* positions, int vertexCount,
854 const uint16_t* indices, int indexCount,
855 const GrColor* colors, const SkPoint* localCoords) {
856 return SkNEW_ARGS(DrawVerticesBatch, (geometry, primitiveType, viewMatrix, positions,
857 vertexCount, indices, indexCount, colors,
858 localCoords));
859 }
860
mtklein36352bf2015-03-25 18:17:31 -0700861 const char* name() const override { return "DrawVerticesBatch"; }
joshualitt0823bfa2015-02-27 10:06:15 -0800862
mtklein36352bf2015-03-25 18:17:31 -0700863 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt0823bfa2015-02-27 10:06:15 -0800864 // When this is called on a batch, there is only one geometry bundle
865 if (this->hasColors()) {
866 out->setUnknownFourComponents();
867 } else {
868 out->setKnownFourComponents(fGeoData[0].fColor);
869 }
870 }
871
mtklein36352bf2015-03-25 18:17:31 -0700872 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualittd4c7d552015-02-27 13:00:56 -0800873 out->setKnownSingleComponent(0xff);
joshualitt0823bfa2015-02-27 10:06:15 -0800874 }
875
mtklein36352bf2015-03-25 18:17:31 -0700876 void initBatchTracker(const GrPipelineInfo& init) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800877 // Handle any color overrides
878 if (init.fColorIgnored) {
879 fGeoData[0].fColor = GrColor_ILLEGAL;
880 } else if (GrColor_ILLEGAL != init.fOverrideColor) {
881 fGeoData[0].fColor = init.fOverrideColor;
882 }
883
884 // setup batch properties
885 fBatch.fColorIgnored = init.fColorIgnored;
886 fBatch.fColor = fGeoData[0].fColor;
887 fBatch.fUsesLocalCoords = init.fUsesLocalCoords;
888 fBatch.fCoverageIgnored = init.fCoverageIgnored;
889 }
890
mtklein36352bf2015-03-25 18:17:31 -0700891 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800892 int colorOffset = -1, texOffset = -1;
893 SkAutoTUnref<const GrGeometryProcessor> gp(
894 set_vertex_attributes(this->hasLocalCoords(), this->hasColors(), &colorOffset,
895 &texOffset, this->color(), this->viewMatrix()));
896
897 batchTarget->initDraw(gp, pipeline);
898
899 // TODO this is hacky, but the only way we have to initialize the GP is to use the
900 // GrPipelineInfo struct so we can generate the correct shader. Once we have GrBatch
901 // everywhere we can remove this nastiness
902 GrPipelineInfo init;
903 init.fColorIgnored = fBatch.fColorIgnored;
904 init.fOverrideColor = GrColor_ILLEGAL;
905 init.fCoverageIgnored = fBatch.fCoverageIgnored;
906 init.fUsesLocalCoords = this->usesLocalCoords();
907 gp->initBatchTracker(batchTarget->currentBatchTracker(), init);
908
909 size_t vertexStride = gp->getVertexStride();
910
911 SkASSERT(vertexStride == sizeof(SkPoint) + (this->hasLocalCoords() ? sizeof(SkPoint) : 0)
912 + (this->hasColors() ? sizeof(GrColor) : 0));
913
914 int instanceCount = fGeoData.count();
915
916 const GrVertexBuffer* vertexBuffer;
917 int firstVertex;
918
919 void* vertices = batchTarget->vertexPool()->makeSpace(vertexStride,
920 this->vertexCount(),
921 &vertexBuffer,
922 &firstVertex);
923
joshualitt4b31de82015-03-05 14:33:41 -0800924 if (!vertices) {
925 SkDebugf("Could not allocate vertices\n");
926 return;
927 }
928
joshualitt0823bfa2015-02-27 10:06:15 -0800929 const GrIndexBuffer* indexBuffer;
930 int firstIndex;
931
932 void* indices = NULL;
933 if (this->hasIndices()) {
934 indices = batchTarget->indexPool()->makeSpace(this->indexCount(),
935 &indexBuffer,
936 &firstIndex);
joshualitt4b31de82015-03-05 14:33:41 -0800937
938 if (!indices) {
939 SkDebugf("Could not allocate indices\n");
940 return;
941 }
joshualitt0823bfa2015-02-27 10:06:15 -0800942 }
943
944 int indexOffset = 0;
945 int vertexOffset = 0;
946 for (int i = 0; i < instanceCount; i++) {
947 const Geometry& args = fGeoData[i];
948
949 // TODO we can actually cache this interleaved and then just memcopy
950 if (this->hasIndices()) {
951 for (int j = 0; j < args.fIndices.count(); ++j, ++indexOffset) {
952 *((uint16_t*)indices + indexOffset) = args.fIndices[j] + vertexOffset;
953 }
954 }
955
956 for (int j = 0; j < args.fPositions.count(); ++j) {
957 *((SkPoint*)vertices) = args.fPositions[j];
958 if (this->hasColors()) {
959 *(GrColor*)((intptr_t)vertices + colorOffset) = args.fColors[j];
960 }
961 if (this->hasLocalCoords()) {
962 *(SkPoint*)((intptr_t)vertices + texOffset) = args.fLocalCoords[j];
963 }
964 vertices = (void*)((intptr_t)vertices + vertexStride);
965 vertexOffset++;
966 }
967 }
968
969 GrDrawTarget::DrawInfo drawInfo;
970 drawInfo.setPrimitiveType(this->primitiveType());
971 drawInfo.setVertexBuffer(vertexBuffer);
972 drawInfo.setStartVertex(firstVertex);
973 drawInfo.setVertexCount(this->vertexCount());
974 if (this->hasIndices()) {
975 drawInfo.setIndexBuffer(indexBuffer);
976 drawInfo.setStartIndex(firstIndex);
977 drawInfo.setIndexCount(this->indexCount());
978 } else {
979 drawInfo.setStartIndex(0);
980 drawInfo.setIndexCount(0);
981 }
982 batchTarget->draw(drawInfo);
983 }
984
985 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
986
987private:
988 DrawVerticesBatch(const Geometry& geometry, GrPrimitiveType primitiveType,
989 const SkMatrix& viewMatrix,
990 const SkPoint* positions, int vertexCount,
991 const uint16_t* indices, int indexCount,
992 const GrColor* colors, const SkPoint* localCoords) {
993 this->initClassID<DrawVerticesBatch>();
994 SkASSERT(positions);
995
996 fBatch.fViewMatrix = viewMatrix;
997 Geometry& installedGeo = fGeoData.push_back(geometry);
998
999 installedGeo.fPositions.append(vertexCount, positions);
1000 if (indices) {
1001 installedGeo.fIndices.append(indexCount, indices);
1002 fBatch.fHasIndices = true;
1003 } else {
1004 fBatch.fHasIndices = false;
1005 }
1006
1007 if (colors) {
1008 installedGeo.fColors.append(vertexCount, colors);
1009 fBatch.fHasColors = true;
1010 } else {
1011 fBatch.fHasColors = false;
1012 }
1013
1014 if (localCoords) {
1015 installedGeo.fLocalCoords.append(vertexCount, localCoords);
1016 fBatch.fHasLocalCoords = true;
1017 } else {
1018 fBatch.fHasLocalCoords = false;
1019 }
1020 fBatch.fVertexCount = vertexCount;
1021 fBatch.fIndexCount = indexCount;
1022 fBatch.fPrimitiveType = primitiveType;
1023 }
1024
1025 GrPrimitiveType primitiveType() const { return fBatch.fPrimitiveType; }
1026 bool batchablePrimitiveType() const {
1027 return kTriangles_GrPrimitiveType == fBatch.fPrimitiveType ||
1028 kLines_GrPrimitiveType == fBatch.fPrimitiveType ||
1029 kPoints_GrPrimitiveType == fBatch.fPrimitiveType;
1030 }
1031 GrColor color() const { return fBatch.fColor; }
1032 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
1033 bool colorIgnored() const { return fBatch.fColorIgnored; }
1034 const SkMatrix& viewMatrix() const { return fBatch.fViewMatrix; }
1035 bool hasColors() const { return fBatch.fHasColors; }
1036 bool hasIndices() const { return fBatch.fHasIndices; }
1037 bool hasLocalCoords() const { return fBatch.fHasLocalCoords; }
1038 int vertexCount() const { return fBatch.fVertexCount; }
1039 int indexCount() const { return fBatch.fIndexCount; }
1040
mtklein36352bf2015-03-25 18:17:31 -07001041 bool onCombineIfPossible(GrBatch* t) override {
joshualitt0823bfa2015-02-27 10:06:15 -08001042 DrawVerticesBatch* that = t->cast<DrawVerticesBatch>();
1043
1044 if (!this->batchablePrimitiveType() || this->primitiveType() != that->primitiveType()) {
1045 return false;
1046 }
1047
1048 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
1049
1050 // We currently use a uniform viewmatrix for this batch
1051 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
1052 return false;
1053 }
1054
1055 if (this->hasColors() != that->hasColors()) {
1056 return false;
1057 }
1058
1059 if (this->hasIndices() != that->hasIndices()) {
1060 return false;
1061 }
1062
1063 if (this->hasLocalCoords() != that->hasLocalCoords()) {
1064 return false;
1065 }
1066
1067 if (!this->hasColors() && this->color() != that->color()) {
1068 return false;
1069 }
1070
1071 if (this->color() != that->color()) {
1072 fBatch.fColor = GrColor_ILLEGAL;
1073 }
1074 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
1075 fBatch.fVertexCount += that->vertexCount();
1076 fBatch.fIndexCount += that->indexCount();
1077 return true;
1078 }
1079
1080 struct BatchTracker {
1081 GrPrimitiveType fPrimitiveType;
1082 SkMatrix fViewMatrix;
1083 GrColor fColor;
1084 bool fUsesLocalCoords;
1085 bool fColorIgnored;
1086 bool fCoverageIgnored;
1087 bool fHasColors;
1088 bool fHasIndices;
1089 bool fHasLocalCoords;
1090 int fVertexCount;
1091 int fIndexCount;
1092 };
1093
1094 BatchTracker fBatch;
1095 SkSTArray<1, Geometry, true> fGeoData;
1096};
1097
joshualitt25d9c152015-02-18 12:29:52 -08001098void GrContext::drawVertices(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -08001099 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001100 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001101 const SkMatrix& viewMatrix,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001102 GrPrimitiveType primitiveType,
1103 int vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001104 const SkPoint positions[],
1105 const SkPoint texCoords[],
bsalomon@google.com27847de2011-02-22 20:59:41 +00001106 const GrColor colors[],
1107 const uint16_t indices[],
1108 int indexCount) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001109 RETURN_IF_ABANDONED
bsalomonf21dab92014-11-13 13:33:28 -08001110 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001111 GrPipelineBuilder pipelineBuilder;
bsalomonf21dab92014-11-13 13:33:28 -08001112 GrDrawTarget::AutoReleaseGeometry geo; // must be inside AutoCheckFlush scope
commit-bot@chromium.org5a567932014-01-08 21:26:09 +00001113
joshualitt570d2f82015-02-25 13:19:48 -08001114 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001115 if (NULL == target) {
1116 return;
1117 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001118
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001119 GR_CREATE_TRACE_MARKER("GrContext::drawVertices", target);
1120
joshualitt0823bfa2015-02-27 10:06:15 -08001121 DrawVerticesBatch::Geometry geometry;
1122 geometry.fColor = paint.getColor();
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001123
joshualitt0823bfa2015-02-27 10:06:15 -08001124 SkAutoTUnref<GrBatch> batch(DrawVerticesBatch::Create(geometry, primitiveType, viewMatrix,
1125 positions, vertexCount, indices,
1126 indexCount,colors, texCoords));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001127
joshualitt0823bfa2015-02-27 10:06:15 -08001128 // TODO figure out bounds
1129 target->drawBatch(&pipelineBuilder, batch, NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001130}
1131
bsalomon@google.com06afe7b2011-04-26 15:31:40 +00001132///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com93c96602012-04-27 13:05:21 +00001133
joshualitt25d9c152015-02-18 12:29:52 -08001134void GrContext::drawRRect(GrRenderTarget*rt,
joshualitt570d2f82015-02-25 13:19:48 -08001135 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001136 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001137 const SkMatrix& viewMatrix,
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001138 const SkRRect& rrect,
egdanield58a0ba2014-06-11 10:30:05 -07001139 const GrStrokeInfo& strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001140 RETURN_IF_ABANDONED
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001141 if (rrect.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001142 return;
1143 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001144
egdanield58a0ba2014-06-11 10:30:05 -07001145 if (strokeInfo.isDashed()) {
1146 SkPath path;
1147 path.addRRect(rrect);
joshualitt570d2f82015-02-25 13:19:48 -08001148 this->drawPath(rt, clip, paint, viewMatrix, path, strokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -07001149 return;
1150 }
1151
bsalomonf21dab92014-11-13 13:33:28 -08001152 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001153 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -08001154 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001155 if (NULL == target) {
1156 return;
1157 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001158
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001159 GR_CREATE_TRACE_MARKER("GrContext::drawRRect", target);
1160
egdanield58a0ba2014-06-11 10:30:05 -07001161 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1162
joshualitt2e3b3e32014-12-09 13:31:14 -08001163 GrColor color = paint.getColor();
joshualitt44701df2015-02-23 14:44:57 -08001164 if (!fOvalRenderer->drawRRect(target,
1165 &pipelineBuilder,
1166 color,
1167 viewMatrix,
1168 paint.isAntiAlias(),
1169 rrect,
1170 strokeRec)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001171 SkPath path;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001172 path.addRRect(rrect);
egdaniel8dd688b2015-01-22 10:16:09 -08001173 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(),
1174 path, strokeInfo);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001175 }
1176}
1177
1178///////////////////////////////////////////////////////////////////////////////
1179
joshualitt25d9c152015-02-18 12:29:52 -08001180void GrContext::drawDRRect(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -08001181 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001182 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001183 const SkMatrix& viewMatrix,
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001184 const SkRRect& outer,
1185 const SkRRect& inner) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001186 RETURN_IF_ABANDONED
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001187 if (outer.isEmpty()) {
1188 return;
1189 }
1190
bsalomonf21dab92014-11-13 13:33:28 -08001191 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001192 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -08001193 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001194
1195 GR_CREATE_TRACE_MARKER("GrContext::drawDRRect", target);
1196
joshualitt2e3b3e32014-12-09 13:31:14 -08001197 GrColor color = paint.getColor();
joshualitt44701df2015-02-23 14:44:57 -08001198 if (!fOvalRenderer->drawDRRect(target,
1199 &pipelineBuilder,
1200 color,
1201 viewMatrix,
1202 paint.isAntiAlias(),
1203 outer,
1204 inner)) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001205 SkPath path;
1206 path.addRRect(inner);
1207 path.addRRect(outer);
1208 path.setFillType(SkPath::kEvenOdd_FillType);
1209
egdanield58a0ba2014-06-11 10:30:05 -07001210 GrStrokeInfo fillRec(SkStrokeRec::kFill_InitStyle);
egdaniel8dd688b2015-01-22 10:16:09 -08001211 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(),
1212 path, fillRec);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001213 }
1214}
1215
1216///////////////////////////////////////////////////////////////////////////////
1217
joshualitt570d2f82015-02-25 13:19:48 -08001218void GrContext::drawOval(GrRenderTarget* rt,
1219 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001220 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001221 const SkMatrix& viewMatrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001222 const SkRect& oval,
egdanield58a0ba2014-06-11 10:30:05 -07001223 const GrStrokeInfo& strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001224 RETURN_IF_ABANDONED
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001225 if (oval.isEmpty()) {
1226 return;
1227 }
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001228
egdanield58a0ba2014-06-11 10:30:05 -07001229 if (strokeInfo.isDashed()) {
1230 SkPath path;
1231 path.addOval(oval);
joshualitt570d2f82015-02-25 13:19:48 -08001232 this->drawPath(rt, clip, paint, viewMatrix, path, strokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -07001233 return;
1234 }
1235
bsalomonf21dab92014-11-13 13:33:28 -08001236 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001237 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -08001238 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001239 if (NULL == target) {
1240 return;
1241 }
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001242
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001243 GR_CREATE_TRACE_MARKER("GrContext::drawOval", target);
1244
egdanield58a0ba2014-06-11 10:30:05 -07001245 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1246
joshualitt2e3b3e32014-12-09 13:31:14 -08001247 GrColor color = paint.getColor();
joshualitt44701df2015-02-23 14:44:57 -08001248 if (!fOvalRenderer->drawOval(target,
1249 &pipelineBuilder,
1250 color,
1251 viewMatrix,
1252 paint.isAntiAlias(),
1253 oval,
1254 strokeRec)) {
bsalomon@google.com93c96602012-04-27 13:05:21 +00001255 SkPath path;
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001256 path.addOval(oval);
egdaniel8dd688b2015-01-22 10:16:09 -08001257 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(),
1258 path, strokeInfo);
skia.committer@gmail.com98ded842013-01-23 07:06:17 +00001259 }
bsalomon@google.com150d2842012-01-12 20:19:56 +00001260}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001261
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001262// Can 'path' be drawn as a pair of filled nested rectangles?
1263static bool is_nested_rects(GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -08001264 GrPipelineBuilder* pipelineBuilder,
joshualitt2e3b3e32014-12-09 13:31:14 -08001265 GrColor color,
joshualitt8059eb92014-12-29 15:10:07 -08001266 const SkMatrix& viewMatrix,
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001267 const SkPath& path,
1268 const SkStrokeRec& stroke,
bsalomon9c0822a2014-08-11 11:07:48 -07001269 SkRect rects[2]) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001270 SkASSERT(stroke.isFillStyle());
1271
1272 if (path.isInverseFillType()) {
1273 return false;
1274 }
1275
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001276 // TODO: this restriction could be lifted if we were willing to apply
1277 // the matrix to all the points individually rather than just to the rect
joshualitt8059eb92014-12-29 15:10:07 -08001278 if (!viewMatrix.preservesAxisAlignment()) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001279 return false;
1280 }
1281
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001282 SkPath::Direction dirs[2];
1283 if (!path.isNestedRects(rects, dirs)) {
1284 return false;
1285 }
1286
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001287 if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001288 // The two rects need to be wound opposite to each other
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001289 return false;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001290 }
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001291
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001292 // Right now, nested rects where the margin is not the same width
1293 // all around do not render correctly
1294 const SkScalar* outer = rects[0].asScalars();
1295 const SkScalar* inner = rects[1].asScalars();
1296
robertphillips183e9852014-10-21 11:25:37 -07001297 bool allEq = true;
1298
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001299 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
robertphillips183e9852014-10-21 11:25:37 -07001300 bool allGoE1 = margin >= SK_Scalar1;
1301
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001302 for (int i = 1; i < 4; ++i) {
1303 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
robertphillips183e9852014-10-21 11:25:37 -07001304 if (temp < SK_Scalar1) {
1305 allGoE1 = false;
1306 }
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001307 if (!SkScalarNearlyEqual(margin, temp)) {
robertphillips183e9852014-10-21 11:25:37 -07001308 allEq = false;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001309 }
1310 }
1311
robertphillips183e9852014-10-21 11:25:37 -07001312 return allEq || allGoE1;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001313}
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001314
joshualitt25d9c152015-02-18 12:29:52 -08001315void GrContext::drawPath(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -08001316 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001317 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001318 const SkMatrix& viewMatrix,
1319 const SkPath& path,
1320 const GrStrokeInfo& strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001321 RETURN_IF_ABANDONED
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001322 if (path.isEmpty()) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001323 if (path.isInverseFillType()) {
joshualitt570d2f82015-02-25 13:19:48 -08001324 this->drawPaint(rt, clip, paint, viewMatrix);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001325 }
1326 return;
1327 }
1328
joshualitt2e3b3e32014-12-09 13:31:14 -08001329 GrColor color = paint.getColor();
egdanield58a0ba2014-06-11 10:30:05 -07001330 if (strokeInfo.isDashed()) {
egdaniele61c4112014-06-12 10:24:21 -07001331 SkPoint pts[2];
1332 if (path.isLine(pts)) {
bsalomonf21dab92014-11-13 13:33:28 -08001333 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001334 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -08001335 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001336 if (NULL == target) {
1337 return;
joshualittd27f73e2014-12-29 07:43:36 -08001338 }
egdaniele61c4112014-06-12 10:24:21 -07001339
egdaniel8dd688b2015-01-22 10:16:09 -08001340 if (GrDashingEffect::DrawDashLine(fGpu, target, &pipelineBuilder, color, viewMatrix,
1341 pts, paint, strokeInfo)) {
joshualittd27f73e2014-12-29 07:43:36 -08001342 return;
egdaniele61c4112014-06-12 10:24:21 -07001343 }
1344 }
1345
1346 // Filter dashed path into new path with the dashing applied
egdanield58a0ba2014-06-11 10:30:05 -07001347 const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo();
1348 SkTLazy<SkPath> effectPath;
1349 GrStrokeInfo newStrokeInfo(strokeInfo, false);
1350 SkStrokeRec* stroke = newStrokeInfo.getStrokeRecPtr();
1351 if (SkDashPath::FilterDashPath(effectPath.init(), path, stroke, NULL, info)) {
joshualitt570d2f82015-02-25 13:19:48 -08001352 this->drawPath(rt, clip, paint, viewMatrix, *effectPath.get(), newStrokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -07001353 return;
1354 }
1355
joshualitt570d2f82015-02-25 13:19:48 -08001356 this->drawPath(rt, clip, paint, viewMatrix, path, newStrokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -07001357 return;
1358 }
1359
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001360 // Note that internalDrawPath may sw-rasterize the path into a scratch texture.
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001361 // Scratch textures can be recycled after they are returned to the texture
1362 // cache. This presents a potential hazard for buffered drawing. However,
1363 // the writePixels that uploads to the scratch will perform a flush so we're
1364 // OK.
bsalomonf21dab92014-11-13 13:33:28 -08001365 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001366 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -08001367 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001368 if (NULL == target) {
1369 return;
1370 }
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001371
egdaniel93a37bc2014-07-21 13:47:57 -07001372 GR_CREATE_TRACE_MARKER1("GrContext::drawPath", target, "Is Convex", path.isConvex());
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001373
egdanield58a0ba2014-06-11 10:30:05 -07001374 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1375
egdaniel8dd688b2015-01-22 10:16:09 -08001376 bool useCoverageAA = paint.isAntiAlias() &&
1377 !pipelineBuilder.getRenderTarget()->isMultisampled();
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001378
egdanield58a0ba2014-06-11 10:30:05 -07001379 if (useCoverageAA && strokeRec.getWidth() < 0 && !path.isConvex()) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001380 // Concave AA paths are expensive - try to avoid them for special cases
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001381 SkRect rects[2];
1382
egdaniel8dd688b2015-01-22 10:16:09 -08001383 if (is_nested_rects(target, &pipelineBuilder, color, viewMatrix, path, strokeRec, rects)) {
joshualitt44701df2015-02-23 14:44:57 -08001384 fAARectRenderer->fillAANestedRects(target, &pipelineBuilder, color, viewMatrix, rects);
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001385 return;
1386 }
1387 }
1388
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001389 SkRect ovalRect;
1390 bool isOval = path.isOval(&ovalRect);
1391
joshualitt8059eb92014-12-29 15:10:07 -08001392 if (!isOval || path.isInverseFillType() ||
joshualitt44701df2015-02-23 14:44:57 -08001393 !fOvalRenderer->drawOval(target,
1394 &pipelineBuilder,
1395 color,
1396 viewMatrix,
1397 paint.isAntiAlias(),
1398 ovalRect,
1399 strokeRec)) {
egdaniel8dd688b2015-01-22 10:16:09 -08001400 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(),
1401 path, strokeInfo);
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001402 }
1403}
1404
joshualitt9853cce2014-11-17 14:22:48 -08001405void GrContext::internalDrawPath(GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -08001406 GrPipelineBuilder* pipelineBuilder,
joshualitt5531d512014-12-17 15:50:11 -08001407 const SkMatrix& viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -08001408 GrColor color,
joshualitt9853cce2014-11-17 14:22:48 -08001409 bool useAA,
1410 const SkPath& path,
egdanield58a0ba2014-06-11 10:30:05 -07001411 const GrStrokeInfo& strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001412 RETURN_IF_ABANDONED
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001413 SkASSERT(!path.isEmpty());
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001414
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001415 GR_CREATE_TRACE_MARKER("GrContext::internalDrawPath", target);
1416
1417
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001418 // An Assumption here is that path renderer would use some form of tweaking
1419 // the src color (either the input alpha or in the frag shader) to implement
1420 // aa. If we have some future driver-mojo path AA that can do the right
1421 // thing WRT to the blend then we'll need some query on the PR.
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001422 bool useCoverageAA = useAA &&
egdaniel0bdeec92015-02-23 12:12:54 -08001423 !pipelineBuilder->getRenderTarget()->isMultisampled();
bsalomon@google.com289533a2011-10-27 12:34:25 +00001424
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001425
1426 GrPathRendererChain::DrawType type =
1427 useCoverageAA ? GrPathRendererChain::kColorAntiAlias_DrawType :
joshualitt9853cce2014-11-17 14:22:48 -08001428 GrPathRendererChain::kColor_DrawType;
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001429
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001430 const SkPath* pathPtr = &path;
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001431 SkTLazy<SkPath> tmpPath;
egdanield58a0ba2014-06-11 10:30:05 -07001432 SkTCopyOnFirstWrite<SkStrokeRec> stroke(strokeInfo.getStrokeRec());
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001433
1434 // Try a 1st time without stroking the path and without allowing the SW renderer
egdaniel8dd688b2015-01-22 10:16:09 -08001435 GrPathRenderer* pr = this->getPathRenderer(target, pipelineBuilder, viewMatrix, *pathPtr,
1436 *stroke, false, type);
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001437
robertphillips@google.come79f3202014-02-11 16:30:21 +00001438 if (NULL == pr) {
joshualitt5531d512014-12-17 15:50:11 -08001439 if (!GrPathRenderer::IsStrokeHairlineOrEquivalent(*stroke, viewMatrix, NULL)) {
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001440 // It didn't work the 1st time, so try again with the stroked path
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001441 if (stroke->applyToPath(tmpPath.init(), *pathPtr)) {
1442 pathPtr = tmpPath.get();
1443 stroke.writable()->setFillStyle();
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001444 if (pathPtr->isEmpty()) {
1445 return;
1446 }
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001447 }
1448 }
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001449
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001450 // This time, allow SW renderer
egdaniel8dd688b2015-01-22 10:16:09 -08001451 pr = this->getPathRenderer(target, pipelineBuilder, viewMatrix, *pathPtr, *stroke, true,
1452 type);
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001453 }
1454
robertphillips@google.come79f3202014-02-11 16:30:21 +00001455 if (NULL == pr) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001456#ifdef SK_DEBUG
tfarina38406c82014-10-31 07:11:12 -07001457 SkDebugf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001458#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001459 return;
1460 }
1461
egdaniel8dd688b2015-01-22 10:16:09 -08001462 pr->drawPath(target, pipelineBuilder, color, viewMatrix, *pathPtr, *stroke, useCoverageAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001463}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001464
bsalomon@google.com27847de2011-02-22 20:59:41 +00001465////////////////////////////////////////////////////////////////////////////////
1466
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001467void GrContext::flush(int flagsBitfield) {
robertphillips@google.come7db8d62013-07-04 11:48:52 +00001468 if (NULL == fDrawBuffer) {
1469 return;
1470 }
1471
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001472 if (kDiscard_FlushBit & flagsBitfield) {
1473 fDrawBuffer->reset();
1474 } else {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001475 fDrawBuffer->flush();
junov@google.com53a55842011-06-08 22:55:10 +00001476 }
bsalomonf21dab92014-11-13 13:33:28 -08001477 fFlushToReduceCacheSize = false;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001478}
1479
bsalomon81beccc2014-10-13 12:32:55 -07001480bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
1481 const void* inPixels, size_t outRowBytes, void* outPixels) {
1482 SkSrcPixelInfo srcPI;
jvanverthfa1e8a72014-12-22 08:31:49 -08001483 if (!GrPixelConfig2ColorAndProfileType(srcConfig, &srcPI.fColorType, NULL)) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001484 return false;
1485 }
bsalomon81beccc2014-10-13 12:32:55 -07001486 srcPI.fAlphaType = kUnpremul_SkAlphaType;
1487 srcPI.fPixels = inPixels;
1488 srcPI.fRowBytes = inRowBytes;
1489
1490 SkDstPixelInfo dstPI;
1491 dstPI.fColorType = srcPI.fColorType;
1492 dstPI.fAlphaType = kPremul_SkAlphaType;
1493 dstPI.fPixels = outPixels;
1494 dstPI.fRowBytes = outRowBytes;
1495
1496 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001497}
1498
bsalomon81beccc2014-10-13 12:32:55 -07001499bool GrContext::writeSurfacePixels(GrSurface* surface,
1500 int left, int top, int width, int height,
1501 GrPixelConfig srcConfig, const void* buffer, size_t rowBytes,
1502 uint32_t pixelOpsFlags) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001503 RETURN_FALSE_IF_ABANDONED
bsalomon81beccc2014-10-13 12:32:55 -07001504 {
1505 GrTexture* texture = NULL;
1506 if (!(kUnpremul_PixelOpsFlag & pixelOpsFlags) && (texture = surface->asTexture()) &&
1507 fGpu->canWriteTexturePixels(texture, srcConfig)) {
1508
1509 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) &&
1510 surface->surfacePriv().hasPendingIO()) {
1511 this->flush();
1512 }
1513 return fGpu->writeTexturePixels(texture, left, top, width, height,
1514 srcConfig, buffer, rowBytes);
1515 // Don't need to check kFlushWrites_PixelOp here, we just did a direct write so the
1516 // upload is already flushed.
1517 }
1518 }
1519
1520 // If we didn't do a direct texture write then we upload the pixels to a texture and draw.
1521 GrRenderTarget* renderTarget = surface->asRenderTarget();
1522 if (NULL == renderTarget) {
1523 return false;
1524 }
1525
1526 // We ignore the preferred config unless it is a R/B swap of the src config. In that case
1527 // we will upload the original src data to a scratch texture but we will spoof it as the swapped
1528 // config. This scratch will then have R and B swapped. We correct for this by swapping again
1529 // when drawing the scratch to the dst using a conversion effect.
1530 bool swapRAndB = false;
1531 GrPixelConfig writeConfig = srcConfig;
1532 if (GrPixelConfigSwapRAndB(srcConfig) ==
1533 fGpu->preferredWritePixelsConfig(srcConfig, renderTarget->config())) {
1534 writeConfig = GrPixelConfigSwapRAndB(srcConfig);
1535 swapRAndB = true;
1536 }
1537
bsalomonf2703d82014-10-28 14:33:06 -07001538 GrSurfaceDesc desc;
bsalomon81beccc2014-10-13 12:32:55 -07001539 desc.fWidth = width;
1540 desc.fHeight = height;
1541 desc.fConfig = writeConfig;
bsalomone3059732014-10-14 11:47:22 -07001542 SkAutoTUnref<GrTexture> texture(this->refScratchTexture(desc, kApprox_ScratchTexMatch));
1543 if (!texture) {
bsalomon81beccc2014-10-13 12:32:55 -07001544 return false;
1545 }
1546
1547 SkAutoTUnref<const GrFragmentProcessor> fp;
1548 SkMatrix textureMatrix;
1549 textureMatrix.setIDiv(texture->width(), texture->height());
1550
1551 // allocate a tmp buffer and sw convert the pixels to premul
1552 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
1553
1554 if (kUnpremul_PixelOpsFlag & pixelOpsFlags) {
1555 if (!GrPixelConfigIs8888(srcConfig)) {
1556 return false;
1557 }
1558 fp.reset(this->createUPMToPMEffect(texture, swapRAndB, textureMatrix));
1559 // handle the unpremul step on the CPU if we couldn't create an effect to do it.
1560 if (NULL == fp) {
1561 size_t tmpRowBytes = 4 * width;
1562 tmpPixels.reset(width * height);
1563 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
1564 tmpPixels.get())) {
1565 return false;
1566 }
1567 rowBytes = tmpRowBytes;
1568 buffer = tmpPixels.get();
1569 }
1570 }
1571 if (NULL == fp) {
1572 fp.reset(GrConfigConversionEffect::Create(texture,
1573 swapRAndB,
1574 GrConfigConversionEffect::kNone_PMConversion,
1575 textureMatrix));
1576 }
1577
1578 // Even if the client told us not to flush, we still flush here. The client may have known that
1579 // writes to the original surface caused no data hazards, but they can't know that the scratch
1580 // we just got is safe.
1581 if (texture->surfacePriv().hasPendingIO()) {
1582 this->flush();
1583 }
1584 if (!fGpu->writeTexturePixels(texture, 0, 0, width, height,
1585 writeConfig, buffer, rowBytes)) {
1586 return false;
1587 }
1588
1589 SkMatrix matrix;
1590 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
1591
1592 // This function can be called in the midst of drawing another object (e.g., when uploading a
1593 // SW-rasterized clip while issuing a draw). So we push the current geometry state before
1594 // drawing a rect to the render target.
1595 // The bracket ensures we pop the stack if we wind up flushing below.
1596 {
joshualitt570d2f82015-02-25 13:19:48 -08001597 GrDrawTarget* drawTarget = this->prepareToDraw();
1598 if (!drawTarget) {
1599 return false;
1600 }
joshualitt9853cce2014-11-17 14:22:48 -08001601 GrDrawTarget::AutoGeometryPush agp(drawTarget);
1602
egdaniel8dd688b2015-01-22 10:16:09 -08001603 GrPipelineBuilder pipelineBuilder;
1604 pipelineBuilder.addColorProcessor(fp);
1605 pipelineBuilder.setRenderTarget(renderTarget);
joshualitt44701df2015-02-23 14:44:57 -08001606 drawTarget->drawSimpleRect(&pipelineBuilder,
1607 GrColor_WHITE,
1608 matrix,
joshualitt8059eb92014-12-29 15:10:07 -08001609 SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)));
bsalomon81beccc2014-10-13 12:32:55 -07001610 }
1611
1612 if (kFlushWrites_PixelOp & pixelOpsFlags) {
1613 this->flushSurfaceWrites(surface);
1614 }
1615
1616 return true;
1617}
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001618
reed@google.com7111d462014-03-25 16:20:24 +00001619// toggles between RGBA and BGRA
1620static SkColorType toggle_colortype32(SkColorType ct) {
1621 if (kRGBA_8888_SkColorType == ct) {
1622 return kBGRA_8888_SkColorType;
1623 } else {
1624 SkASSERT(kBGRA_8888_SkColorType == ct);
1625 return kRGBA_8888_SkColorType;
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001626 }
1627}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001628
bsalomon@google.com0342a852012-08-20 19:22:38 +00001629bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
1630 int left, int top, int width, int height,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001631 GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001632 uint32_t flags) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001633 RETURN_FALSE_IF_ABANDONED
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001634 ASSERT_OWNED_RESOURCE(target);
bsalomon89c62982014-11-03 12:08:42 -08001635 SkASSERT(target);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001636
bsalomonafbf2d62014-09-30 12:18:44 -07001637 if (!(kDontFlush_PixelOpsFlag & flags) && target->surfacePriv().hasPendingWrite()) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001638 this->flush();
1639 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001640
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001641 // Determine which conversions have to be applied: flipY, swapRAnd, and/or unpremul.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001642
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001643 // If fGpu->readPixels would incur a y-flip cost then we will read the pixels upside down. We'll
1644 // either do the flipY by drawing into a scratch with a matrix or on the cpu after the read.
1645 bool flipY = fGpu->readPixelsWillPayForYFlip(target, left, top,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001646 width, height, dstConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001647 rowBytes);
bsalomon@google.com9c680582013-02-06 18:17:50 +00001648 // We ignore the preferred config if it is different than our config unless it is an R/B swap.
1649 // In that case we'll perform an R and B swap while drawing to a scratch texture of the swapped
1650 // config. Then we will call readPixels on the scratch with the swapped config. The swaps during
1651 // the draw cancels out the fact that we call readPixels with a config that is R/B swapped from
1652 // dstConfig.
1653 GrPixelConfig readConfig = dstConfig;
1654 bool swapRAndB = false;
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +00001655 if (GrPixelConfigSwapRAndB(dstConfig) ==
1656 fGpu->preferredReadPixelsConfig(dstConfig, target->config())) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001657 readConfig = GrPixelConfigSwapRAndB(readConfig);
1658 swapRAndB = true;
1659 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001660
bsalomon@google.com0342a852012-08-20 19:22:38 +00001661 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001662
bsalomon@google.com9c680582013-02-06 18:17:50 +00001663 if (unpremul && !GrPixelConfigIs8888(dstConfig)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001664 // The unpremul flag is only allowed for these two configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001665 return false;
1666 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001667
bsalomon191bcc02014-11-14 11:31:13 -08001668 SkAutoTUnref<GrTexture> tempTexture;
1669
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001670 // If the src is a texture and we would have to do conversions after read pixels, we instead
1671 // do the conversions by drawing the src to a scratch texture. If we handle any of the
1672 // conversions in the draw we set the corresponding bool to false so that we don't reapply it
1673 // on the read back pixels.
1674 GrTexture* src = target->asTexture();
bsalomon49f085d2014-09-05 13:34:00 -07001675 if (src && (swapRAndB || unpremul || flipY)) {
bsalomon81beccc2014-10-13 12:32:55 -07001676 // Make the scratch a render so we can read its pixels.
bsalomonf2703d82014-10-28 14:33:06 -07001677 GrSurfaceDesc desc;
1678 desc.fFlags = kRenderTarget_GrSurfaceFlag;
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001679 desc.fWidth = width;
1680 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001681 desc.fConfig = readConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001682 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001683
bsalomon@google.com9c680582013-02-06 18:17:50 +00001684 // 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 +00001685 // match the passed rect. However, if we see many different size rectangles we will trash
1686 // our texture cache and pay the cost of creating and destroying many textures. So, we only
1687 // request an exact match when the caller is reading an entire RT.
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001688 ScratchTexMatch match = kApprox_ScratchTexMatch;
1689 if (0 == left &&
1690 0 == top &&
1691 target->width() == width &&
1692 target->height() == height &&
1693 fGpu->fullReadPixelsIsFasterThanPartial()) {
1694 match = kExact_ScratchTexMatch;
1695 }
bsalomon191bcc02014-11-14 11:31:13 -08001696 tempTexture.reset(this->refScratchTexture(desc, match));
1697 if (tempTexture) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001698 // compute a matrix to perform the draw
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001699 SkMatrix textureMatrix;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001700 textureMatrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001701 textureMatrix.postIDiv(src->width(), src->height());
1702
joshualittb0a8a372014-09-23 09:50:21 -07001703 SkAutoTUnref<const GrFragmentProcessor> fp;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001704 if (unpremul) {
joshualittb0a8a372014-09-23 09:50:21 -07001705 fp.reset(this->createPMToUPMEffect(src, swapRAndB, textureMatrix));
1706 if (fp) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001707 unpremul = false; // we no longer need to do this on CPU after the read back.
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001708 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001709 }
1710 // If we failed to create a PM->UPM effect and have no other conversions to perform then
1711 // there is no longer any point to using the scratch.
joshualittb0a8a372014-09-23 09:50:21 -07001712 if (fp || flipY || swapRAndB) {
1713 if (!fp) {
1714 fp.reset(GrConfigConversionEffect::Create(
1715 src, swapRAndB, GrConfigConversionEffect::kNone_PMConversion,
1716 textureMatrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001717 }
1718 swapRAndB = false; // we will handle the swap in the draw.
bsalomon@google.comc4364992011-11-07 15:54:49 +00001719
robertphillips@google.com13f181f2013-03-02 12:02:08 +00001720 // We protect the existing geometry here since it may not be
1721 // clear to the caller that a draw operation (i.e., drawSimpleRect)
1722 // can be invoked in this method
joshualitt5c55fef2014-10-31 14:04:35 -07001723 {
joshualitt9853cce2014-11-17 14:22:48 -08001724 GrDrawTarget::AutoGeometryPush agp(fDrawBuffer);
egdaniel8dd688b2015-01-22 10:16:09 -08001725 GrPipelineBuilder pipelineBuilder;
joshualitt5c55fef2014-10-31 14:04:35 -07001726 SkASSERT(fp);
egdaniel8dd688b2015-01-22 10:16:09 -08001727 pipelineBuilder.addColorProcessor(fp);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001728
egdaniel8dd688b2015-01-22 10:16:09 -08001729 pipelineBuilder.setRenderTarget(tempTexture->asRenderTarget());
joshualitt5c55fef2014-10-31 14:04:35 -07001730 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
joshualitt44701df2015-02-23 14:44:57 -08001731 fDrawBuffer->drawSimpleRect(&pipelineBuilder,
1732 GrColor_WHITE,
1733 SkMatrix::I(),
egdaniel8dd688b2015-01-22 10:16:09 -08001734 rect);
joshualitt5c55fef2014-10-31 14:04:35 -07001735 // we want to read back from the scratch's origin
1736 left = 0;
1737 top = 0;
bsalomon191bcc02014-11-14 11:31:13 -08001738 target = tempTexture->asRenderTarget();
joshualitt5c55fef2014-10-31 14:04:35 -07001739 }
1740 this->flushSurfaceWrites(target);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001741 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001742 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001743 }
joshualitt5c55fef2014-10-31 14:04:35 -07001744
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001745 if (!fGpu->readPixels(target,
1746 left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001747 readConfig, buffer, rowBytes)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001748 return false;
1749 }
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001750 // Perform any conversions we weren't able to perform using a scratch texture.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001751 if (unpremul || swapRAndB) {
reed@google.com7111d462014-03-25 16:20:24 +00001752 SkDstPixelInfo dstPI;
jvanverthfa1e8a72014-12-22 08:31:49 -08001753 if (!GrPixelConfig2ColorAndProfileType(dstConfig, &dstPI.fColorType, NULL)) {
reed@google.com7111d462014-03-25 16:20:24 +00001754 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001755 }
reed@google.com7111d462014-03-25 16:20:24 +00001756 dstPI.fAlphaType = kUnpremul_SkAlphaType;
1757 dstPI.fPixels = buffer;
1758 dstPI.fRowBytes = rowBytes;
1759
1760 SkSrcPixelInfo srcPI;
1761 srcPI.fColorType = swapRAndB ? toggle_colortype32(dstPI.fColorType) : dstPI.fColorType;
1762 srcPI.fAlphaType = kPremul_SkAlphaType;
1763 srcPI.fPixels = buffer;
1764 srcPI.fRowBytes = rowBytes;
1765
1766 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001767 }
1768 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001769}
1770
bsalomon87a94eb2014-11-03 14:28:32 -08001771void GrContext::prepareSurfaceForExternalRead(GrSurface* surface) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001772 RETURN_IF_ABANDONED
bsalomon87a94eb2014-11-03 14:28:32 -08001773 SkASSERT(surface);
1774 ASSERT_OWNED_RESOURCE(surface);
1775 if (surface->surfacePriv().hasPendingIO()) {
1776 this->flush();
1777 }
1778 GrRenderTarget* rt = surface->asRenderTarget();
1779 if (fGpu && rt) {
1780 fGpu->resolveRenderTarget(rt);
bsalomon41ebbdd2014-08-04 08:31:39 -07001781 }
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001782}
1783
bsalomon41ebbdd2014-08-04 08:31:39 -07001784void GrContext::discardRenderTarget(GrRenderTarget* renderTarget) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001785 RETURN_IF_ABANDONED
bsalomon41ebbdd2014-08-04 08:31:39 -07001786 SkASSERT(renderTarget);
1787 ASSERT_OWNED_RESOURCE(renderTarget);
bsalomonf21dab92014-11-13 13:33:28 -08001788 AutoCheckFlush acf(this);
joshualitt570d2f82015-02-25 13:19:48 -08001789 GrDrawTarget* target = this->prepareToDraw();
bsalomon41ebbdd2014-08-04 08:31:39 -07001790 if (NULL == target) {
1791 return;
1792 }
1793 target->discard(renderTarget);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001794}
1795
bsalomonf80bfed2014-10-07 05:56:02 -07001796void GrContext::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
1797 const SkIPoint& dstPoint, uint32_t pixelOpsFlags) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001798 RETURN_IF_ABANDONED
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001799 if (NULL == src || NULL == dst) {
1800 return;
1801 }
bsalomone3d4bf22014-09-23 09:15:03 -07001802 ASSERT_OWNED_RESOURCE(src);
junov2bb52102014-09-29 10:18:59 -07001803 ASSERT_OWNED_RESOURCE(dst);
Brian Salomon34a98952014-09-24 11:41:24 -04001804
bsalomonf80bfed2014-10-07 05:56:02 -07001805 // Since we're going to the draw target and not GPU, no need to check kNoFlush
1806 // here.
junov96c118e2014-09-26 13:09:47 -07001807
joshualitt570d2f82015-02-25 13:19:48 -08001808 GrDrawTarget* target = this->prepareToDraw();
junov96c118e2014-09-26 13:09:47 -07001809 if (NULL == target) {
1810 return;
1811 }
junov96c118e2014-09-26 13:09:47 -07001812 target->copySurface(dst, src, srcRect, dstPoint);
bsalomonf80bfed2014-10-07 05:56:02 -07001813
1814 if (kFlushWrites_PixelOp & pixelOpsFlags) {
1815 this->flush();
1816 }
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001817}
1818
bsalomonf80bfed2014-10-07 05:56:02 -07001819void GrContext::flushSurfaceWrites(GrSurface* surface) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001820 RETURN_IF_ABANDONED
bsalomonf80bfed2014-10-07 05:56:02 -07001821 if (surface->surfacePriv().hasPendingWrite()) {
1822 this->flush();
1823 }
1824}
1825
egdaniel8dd688b2015-01-22 10:16:09 -08001826GrDrawTarget* GrContext::prepareToDraw(GrPipelineBuilder* pipelineBuilder,
joshualitt25d9c152015-02-18 12:29:52 -08001827 GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -08001828 const GrClip& clip,
joshualitt9853cce2014-11-17 14:22:48 -08001829 const GrPaint* paint,
1830 const AutoCheckFlush* acf) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001831 if (NULL == fGpu || NULL == fDrawBuffer) {
bsalomon41ebbdd2014-08-04 08:31:39 -07001832 return NULL;
1833 }
1834
joshualitt570d2f82015-02-25 13:19:48 -08001835 ASSERT_OWNED_RESOURCE(rt);
1836 SkASSERT(rt && paint && acf);
1837 pipelineBuilder->setFromPaint(*paint, rt, clip);
1838 return fDrawBuffer;
1839}
1840
1841GrDrawTarget* GrContext::prepareToDraw() {
1842 if (NULL == fGpu) {
1843 return NULL;
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001844 }
joshualitt5c55fef2014-10-31 14:04:35 -07001845 return fDrawBuffer;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001846}
1847
robertphillips@google.com72176b22012-05-23 13:19:12 +00001848/*
1849 * This method finds a path renderer that can draw the specified path on
1850 * the provided target.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001851 * Due to its expense, the software path renderer has split out so it can
robertphillips@google.com72176b22012-05-23 13:19:12 +00001852 * can be individually allowed/disallowed via the "allowSW" boolean.
1853 */
joshualitt9853cce2014-11-17 14:22:48 -08001854GrPathRenderer* GrContext::getPathRenderer(const GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -08001855 const GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -08001856 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -08001857 const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001858 const SkStrokeRec& stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001859 bool allowSW,
1860 GrPathRendererChain::DrawType drawType,
1861 GrPathRendererChain::StencilSupport* stencilSupport) {
1862
bsalomon@google.com30085192011-08-19 15:42:31 +00001863 if (NULL == fPathRendererChain) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001864 fPathRendererChain = SkNEW_ARGS(GrPathRendererChain, (this));
bsalomon@google.com30085192011-08-19 15:42:31 +00001865 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001866
joshualitt9853cce2014-11-17 14:22:48 -08001867 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(target,
egdaniel8dd688b2015-01-22 10:16:09 -08001868 pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -08001869 viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -08001870 path,
sugoi@google.com12b4e272012-12-06 20:13:11 +00001871 stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001872 drawType,
1873 stencilSupport);
robertphillips@google.com72176b22012-05-23 13:19:12 +00001874
1875 if (NULL == pr && allowSW) {
1876 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001877 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001878 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001879 pr = fSoftwarePathRenderer;
1880 }
1881
1882 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001883}
1884
bsalomon@google.com27847de2011-02-22 20:59:41 +00001885////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00001886bool GrContext::isConfigRenderable(GrPixelConfig config, bool withMSAA) const {
1887 return fGpu->caps()->isConfigRenderable(config, withMSAA);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001888}
1889
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +00001890int GrContext::getRecommendedSampleCount(GrPixelConfig config,
1891 SkScalar dpi) const {
1892 if (!this->isConfigRenderable(config, true)) {
1893 return 0;
1894 }
1895 int chosenSampleCount = 0;
1896 if (fGpu->caps()->pathRenderingSupport()) {
1897 if (dpi >= 250.0f) {
1898 chosenSampleCount = 4;
1899 } else {
1900 chosenSampleCount = 16;
1901 }
1902 }
1903 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ?
1904 chosenSampleCount : 0;
1905}
1906
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001907void GrContext::setupDrawBuffer() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001908 SkASSERT(NULL == fDrawBuffer);
1909 SkASSERT(NULL == fDrawBufferVBAllocPool);
1910 SkASSERT(NULL == fDrawBufferIBAllocPool);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001911
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001912 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001913 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001914 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001915 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001916 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001917 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001918 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001919 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001920
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001921 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001922 fDrawBufferVBAllocPool,
1923 fDrawBufferIBAllocPool));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001924}
1925
bsalomon@google.com21c10c52013-06-13 17:44:07 +00001926GrDrawTarget* GrContext::getTextTarget() {
joshualitt570d2f82015-02-25 13:19:48 -08001927 return this->prepareToDraw();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001928}
1929
1930const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1931 return fGpu->getQuadIndexBuffer();
1932}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001933
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001934namespace {
1935void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
1936 GrConfigConversionEffect::PMConversion pmToUPM;
1937 GrConfigConversionEffect::PMConversion upmToPM;
1938 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
1939 *pmToUPMValue = pmToUPM;
1940 *upmToPMValue = upmToPM;
1941}
1942}
1943
joshualittb0a8a372014-09-23 09:50:21 -07001944const GrFragmentProcessor* GrContext::createPMToUPMEffect(GrTexture* texture,
1945 bool swapRAndB,
1946 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001947 if (!fDidTestPMConversions) {
1948 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001949 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001950 }
1951 GrConfigConversionEffect::PMConversion pmToUPM =
1952 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
1953 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001954 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001955 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001956 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001957 }
1958}
1959
joshualittb0a8a372014-09-23 09:50:21 -07001960const GrFragmentProcessor* GrContext::createUPMToPMEffect(GrTexture* texture,
1961 bool swapRAndB,
1962 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001963 if (!fDidTestPMConversions) {
1964 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001965 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001966 }
1967 GrConfigConversionEffect::PMConversion upmToPM =
1968 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
1969 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001970 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001971 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001972 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001973 }
1974}
1975
bsalomon37f9a262015-02-02 13:00:10 -08001976//////////////////////////////////////////////////////////////////////////////
1977
1978void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
1979 if (maxTextures) {
bsalomon0ea80f42015-02-11 10:49:59 -08001980 *maxTextures = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -08001981 }
1982 if (maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -08001983 *maxTextureBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -08001984 }
1985}
1986
1987void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -08001988 fResourceCache->setLimits(maxTextures, maxTextureBytes);
bsalomon37f9a262015-02-02 13:00:10 -08001989}
1990
bsalomonf99e9612015-02-19 08:24:16 -08001991void GrContext::addResourceToCache(const GrUniqueKey& key, GrGpuResource* resource) {
bsalomon37f9a262015-02-02 13:00:10 -08001992 ASSERT_OWNED_RESOURCE(resource);
bsalomonf99e9612015-02-19 08:24:16 -08001993 if (!resource) {
1994 return;
bsalomon37f9a262015-02-02 13:00:10 -08001995 }
bsalomonf99e9612015-02-19 08:24:16 -08001996 resource->resourcePriv().setUniqueKey(key);
bsalomon37f9a262015-02-02 13:00:10 -08001997}
1998
bsalomon8718aaf2015-02-19 07:24:21 -08001999bool GrContext::isResourceInCache(const GrUniqueKey& key) const {
2000 return fResourceCache->hasUniqueKey(key);
commit-bot@chromium.org95a2b0e2014-05-05 19:21:16 +00002001}
2002
bsalomon8718aaf2015-02-19 07:24:21 -08002003GrGpuResource* GrContext::findAndRefCachedResource(const GrUniqueKey& key) {
2004 return fResourceCache->findAndRefUniqueResource(key);
commit-bot@chromium.org95a2b0e2014-05-05 19:21:16 +00002005}
2006
bsalomon37f9a262015-02-02 13:00:10 -08002007//////////////////////////////////////////////////////////////////////////////
2008
egdanielbbcb38d2014-06-19 10:19:29 -07002009void GrContext::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
2010 fGpu->addGpuTraceMarker(marker);
bsalomon49f085d2014-09-05 13:34:00 -07002011 if (fDrawBuffer) {
egdanielbbcb38d2014-06-19 10:19:29 -07002012 fDrawBuffer->addGpuTraceMarker(marker);
2013 }
2014}
2015
2016void GrContext::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
2017 fGpu->removeGpuTraceMarker(marker);
bsalomon49f085d2014-09-05 13:34:00 -07002018 if (fDrawBuffer) {
egdanielbbcb38d2014-06-19 10:19:29 -07002019 fDrawBuffer->removeGpuTraceMarker(marker);
2020 }
2021}
2022