blob: 04e3f8fa9ff16aee0b03f47eaf8b1393a3512ae9 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
bsalomon@google.com27847de2011-02-22 20:59:41 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
bsalomon@google.com27847de2011-02-22 20:59:41 +00007 */
8
bsalomon@google.com1fadb202011-12-12 16:10:08 +00009#include "GrContext.h"
10
jvanverth@google.combfe2b9d2013-09-06 16:57:29 +000011#include "GrAARectRenderer.h"
joshualitt1d89e8d2015-04-01 12:40:54 -070012#include "GrAtlasTextContext.h"
joshualitt0823bfa2015-02-27 10:06:15 -080013#include "GrBatch.h"
joshualitt7c3a2f82015-03-31 13:32:05 -070014#include "GrBatchFontCache.h"
joshualitt0823bfa2015-02-27 10:06:15 -080015#include "GrBatchTarget.h"
tomhudson@google.com278cbb42011-06-30 19:37:01 +000016#include "GrBufferAllocPool.h"
joshualitt5478d422014-11-14 16:00:38 -080017#include "GrDefaultGeoProcFactory.h"
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"
joshualittb7133be2015-04-08 09:08:31 -070036#include "GrTextBlobCache.h"
bsalomonafbf2d62014-09-30 12:18:44 -070037#include "GrTexturePriv.h"
egdanielbbcb38d2014-06-19 10:19:29 -070038#include "GrTraceMarker.h"
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +000039#include "GrTracing.h"
egdanield58a0ba2014-06-11 10:30:05 -070040#include "SkDashPathPriv.h"
bsalomon81beccc2014-10-13 12:32:55 -070041#include "SkConfig8888.h"
reed@google.com7111d462014-03-25 16:20:24 +000042#include "SkGr.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000043#include "SkRRect.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000044#include "SkStrokeRec.h"
commit-bot@chromium.orgdcb8ef92014-03-27 11:26:10 +000045#include "SkTLazy.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000046#include "SkTLS.h"
commit-bot@chromium.org933e65d2014-03-20 20:00:24 +000047#include "SkTraceEvent.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000048
joshualitt5478d422014-11-14 16:00:38 -080049#include "effects/GrConfigConversionEffect.h"
50#include "effects/GrDashingEffect.h"
51#include "effects/GrSingleTextureEffect.h"
52
bsalomon@google.com60361492012-03-15 17:47:06 +000053static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
bsalomon@google.com27847de2011-02-22 20:59:41 +000054static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
55
bsalomon@google.com1d4edd32012-08-16 18:36:06 +000056static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = 1 << 11;
57static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = 4;
bsalomon@google.com27847de2011-02-22 20:59:41 +000058
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000059#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
joshualitt5f5a8d72015-02-25 14:09:45 -080060#define RETURN_IF_ABANDONED if (!fDrawBuffer) { return; }
61#define RETURN_FALSE_IF_ABANDONED if (!fDrawBuffer) { return false; }
62#define RETURN_NULL_IF_ABANDONED if (!fDrawBuffer) { return NULL; }
bsalomon@google.combc4b6542011-11-19 13:56:11 +000063
bsalomonf21dab92014-11-13 13:33:28 -080064class GrContext::AutoCheckFlush {
65public:
66 AutoCheckFlush(GrContext* context) : fContext(context) { SkASSERT(context); }
67
68 ~AutoCheckFlush() {
69 if (fContext->fFlushToReduceCacheSize) {
70 fContext->flush();
71 }
72 }
73
74private:
75 GrContext* fContext;
76};
77
krajcevski9c6d4d72014-08-12 07:26:25 -070078GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
79 const Options* opts) {
80 GrContext* context;
81 if (NULL == opts) {
82 context = SkNEW_ARGS(GrContext, (Options()));
83 } else {
84 context = SkNEW_ARGS(GrContext, (*opts));
85 }
86
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000087 if (context->init(backend, backendContext)) {
88 return context;
89 } else {
90 context->unref();
91 return NULL;
bsalomon@google.com27847de2011-02-22 20:59:41 +000092 }
bsalomon@google.com27847de2011-02-22 20:59:41 +000093}
94
krajcevski9c6d4d72014-08-12 07:26:25 -070095GrContext::GrContext(const Options& opts) : fOptions(opts) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000096 fGpu = NULL;
97 fPathRendererChain = NULL;
98 fSoftwarePathRenderer = NULL;
bsalomon0ea80f42015-02-11 10:49:59 -080099 fResourceCache = NULL;
joshualitt7c3a2f82015-03-31 13:32:05 -0700100 fBatchFontCache = NULL;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000101 fFontCache = NULL;
102 fDrawBuffer = NULL;
103 fDrawBufferVBAllocPool = NULL;
104 fDrawBufferIBAllocPool = NULL;
bsalomonf21dab92014-11-13 13:33:28 -0800105 fFlushToReduceCacheSize = false;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000106 fAARectRenderer = NULL;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000107 fOvalRenderer = NULL;
robertphillips@google.com44a91dc2013-07-25 15:32:06 +0000108 fMaxTextureSizeOverride = 1 << 20;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000109}
110
111bool GrContext::init(GrBackend backend, GrBackendContext backendContext) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000112 SkASSERT(NULL == fGpu);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000113
114 fGpu = GrGpu::Create(backend, backendContext, this);
115 if (NULL == fGpu) {
116 return false;
117 }
bsalomon33435572014-11-05 14:47:41 -0800118 this->initCommon();
119 return true;
120}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000121
bsalomon33435572014-11-05 14:47:41 -0800122void GrContext::initCommon() {
bsalomon0ea80f42015-02-11 10:49:59 -0800123 fResourceCache = SkNEW(GrResourceCache);
124 fResourceCache->setOverBudgetCallback(OverBudgetCB, this);
commit-bot@chromium.org1836d332013-07-16 22:55:03 +0000125
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000126 fFontCache = SkNEW_ARGS(GrFontCache, (fGpu));
127
robertphillips4ec84da2014-06-24 13:10:43 -0700128 fLayerCache.reset(SkNEW_ARGS(GrLayerCache, (this)));
robertphillips@google.come930a072014-04-03 00:34:27 +0000129
joshualittb44293e2014-10-28 08:12:18 -0700130 fAARectRenderer = SkNEW_ARGS(GrAARectRenderer, (fGpu));
joshualitt5531d512014-12-17 15:50:11 -0800131 fOvalRenderer = SkNEW_ARGS(GrOvalRenderer, (fGpu));
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000132
133 fDidTestPMConversions = false;
134
135 this->setupDrawBuffer();
joshualitt7c3a2f82015-03-31 13:32:05 -0700136
137 // GrBatchFontCache will eventually replace GrFontCache
joshualitt62db8ba2015-04-09 08:22:37 -0700138 fBatchFontCache = SkNEW_ARGS(GrBatchFontCache, (this));
joshualittb7133be2015-04-08 09:08:31 -0700139
joshualitt0db6dfa2015-04-10 07:01:30 -0700140 fTextBlobCache.reset(SkNEW_ARGS(GrTextBlobCache, (TextBlobCacheOverBudgetCB, this)));
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000141}
142
bsalomon@google.com27847de2011-02-22 20:59:41 +0000143GrContext::~GrContext() {
bsalomon@google.com733c0622013-04-24 17:59:32 +0000144 if (NULL == fGpu) {
145 return;
146 }
147
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000148 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000149
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000150 for (int i = 0; i < fCleanUpData.count(); ++i) {
151 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
152 }
153
bsalomon0ea80f42015-02-11 10:49:59 -0800154 SkDELETE(fResourceCache);
joshualitt7c3a2f82015-03-31 13:32:05 -0700155 SkDELETE(fBatchFontCache);
bsalomon33435572014-11-05 14:47:41 -0800156 SkDELETE(fFontCache);
157 SkDELETE(fDrawBuffer);
158 SkDELETE(fDrawBufferVBAllocPool);
159 SkDELETE(fDrawBufferIBAllocPool);
bsalomon@google.com30085192011-08-19 15:42:31 +0000160
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000161 fAARectRenderer->unref();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000162 fOvalRenderer->unref();
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000163
bsalomon@google.com205d4602011-04-25 12:43:45 +0000164 fGpu->unref();
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000165 SkSafeUnref(fPathRendererChain);
166 SkSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000167}
168
bsalomon2354f842014-07-28 13:48:36 -0700169void GrContext::abandonContext() {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000170 // abandon first to so destructors
171 // don't try to free the resources in the API.
bsalomon0ea80f42015-02-11 10:49:59 -0800172 fResourceCache->abandonAll();
bsalomonc8dc1f72014-08-21 13:02:13 -0700173
robertphillipse3371302014-09-17 06:01:06 -0700174 fGpu->contextAbandoned();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000175
bsalomon@google.com30085192011-08-19 15:42:31 +0000176 // a path renderer may be holding onto resources that
177 // are now unusable
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000178 SkSafeSetNull(fPathRendererChain);
179 SkSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000180
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000181 delete fDrawBuffer;
182 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000183
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000184 delete fDrawBufferVBAllocPool;
185 fDrawBufferVBAllocPool = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000186
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000187 delete fDrawBufferIBAllocPool;
188 fDrawBufferIBAllocPool = NULL;
189
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000190 fAARectRenderer->reset();
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000191 fOvalRenderer->reset();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000192
joshualitt7c3a2f82015-03-31 13:32:05 -0700193 fBatchFontCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000194 fFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000195 fLayerCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000196}
197
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000198void GrContext::resetContext(uint32_t state) {
199 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000200}
201
202void GrContext::freeGpuResources() {
203 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000204
bsalomon49f085d2014-09-05 13:34:00 -0700205 if (fDrawBuffer) {
bsalomonc8dc1f72014-08-21 13:02:13 -0700206 fDrawBuffer->purgeResources();
207 }
robertphillips@google.comff175842012-05-14 19:31:39 +0000208
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000209 fAARectRenderer->reset();
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000210 fOvalRenderer->reset();
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000211
joshualitt7c3a2f82015-03-31 13:32:05 -0700212 fBatchFontCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000213 fFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000214 fLayerCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000215 // a path renderer may be holding onto resources
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000216 SkSafeSetNull(fPathRendererChain);
217 SkSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000218}
219
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000220void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
bsalomon71cb0c22014-11-14 12:10:14 -0800221 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800222 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800223 }
224 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800225 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800226 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000227}
228
kkinnunenc6cb56f2014-06-24 00:12:27 -0700229GrTextContext* GrContext::createTextContext(GrRenderTarget* renderTarget,
joshualitt6e8cd962015-03-20 10:30:14 -0700230 SkGpuDevice* gpuDevice,
kkinnunenc6cb56f2014-06-24 00:12:27 -0700231 const SkDeviceProperties&
232 leakyProperties,
233 bool enableDistanceFieldFonts) {
bsalomon6bc1b5f2015-02-23 09:06:38 -0800234 if (fGpu->caps()->pathRenderingSupport() && renderTarget->isMultisampled()) {
235 GrStencilBuffer* sb = renderTarget->renderTargetPriv().attachStencilBuffer();
236 if (sb) {
joshualitt6e8cd962015-03-20 10:30:14 -0700237 return GrStencilAndCoverTextContext::Create(this, gpuDevice, leakyProperties);
bsalomon6bc1b5f2015-02-23 09:06:38 -0800238 }
jvanverth8c27a182014-10-14 08:45:50 -0700239 }
240
joshualitt7c3a2f82015-03-31 13:32:05 -0700241#ifdef USE_BITMAP_TEXTBLOBS
joshualittdbd35932015-04-02 09:19:04 -0700242 return GrAtlasTextContext::Create(this, gpuDevice, leakyProperties);
joshualitt7c3a2f82015-03-31 13:32:05 -0700243#else
joshualitt6e8cd962015-03-20 10:30:14 -0700244 return GrDistanceFieldTextContext::Create(this, gpuDevice, leakyProperties,
245 enableDistanceFieldFonts);
joshualitt7c3a2f82015-03-31 13:32:05 -0700246#endif
kkinnunenc6cb56f2014-06-24 00:12:27 -0700247}
248
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000249////////////////////////////////////////////////////////////////////////////////
bsalomond0423582015-02-06 08:49:24 -0800250enum ScratchTextureFlags {
251 kExact_ScratchTextureFlag = 0x1,
252 kNoPendingIO_ScratchTextureFlag = 0x2,
253 kNoCreate_ScratchTextureFlag = 0x4,
254};
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000255
bsalomond2a6f4e2015-02-04 10:55:54 -0800256bool GrContext::isConfigTexturable(GrPixelConfig config) const {
257 return fGpu->caps()->isConfigTexturable(config);
258}
259
260bool GrContext::npotTextureTileSupport() const {
261 return fGpu->caps()->npotTextureTileSupport();
262}
263
bsalomond0423582015-02-06 08:49:24 -0800264GrTexture* GrContext::createTexture(const GrSurfaceDesc& desc, bool budgeted, const void* srcData,
bsalomon37f9a262015-02-02 13:00:10 -0800265 size_t rowBytes) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800266 RETURN_NULL_IF_ABANDONED
bsalomond0423582015-02-06 08:49:24 -0800267 if ((desc.fFlags & kRenderTarget_GrSurfaceFlag) &&
268 !this->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
bsalomond2a6f4e2015-02-04 10:55:54 -0800269 return NULL;
270 }
bsalomond0423582015-02-06 08:49:24 -0800271 if (!GrPixelConfigIsCompressed(desc.fConfig)) {
272 static const uint32_t kFlags = kExact_ScratchTextureFlag |
273 kNoCreate_ScratchTextureFlag;
274 if (GrTexture* texture = this->internalRefScratchTexture(desc, kFlags)) {
275 if (!srcData || texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
276 srcData, rowBytes)) {
277 if (!budgeted) {
bsalomon3582d3e2015-02-13 14:20:05 -0800278 texture->resourcePriv().makeUnbudgeted();
bsalomond0423582015-02-06 08:49:24 -0800279 }
280 return texture;
281 }
282 texture->unref();
283 }
284 }
285 return fGpu->createTexture(desc, budgeted, srcData, rowBytes);
286}
bsalomond2a6f4e2015-02-04 10:55:54 -0800287
bsalomond0423582015-02-06 08:49:24 -0800288GrTexture* GrContext::refScratchTexture(const GrSurfaceDesc& desc, ScratchTexMatch match,
289 bool calledDuringFlush) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800290 RETURN_NULL_IF_ABANDONED
bsalomond0423582015-02-06 08:49:24 -0800291 // Currently we don't recycle compressed textures as scratch.
292 if (GrPixelConfigIsCompressed(desc.fConfig)) {
293 return NULL;
294 } else {
295 uint32_t flags = 0;
296 if (kExact_ScratchTexMatch == match) {
297 flags |= kExact_ScratchTextureFlag;
298 }
299 if (calledDuringFlush) {
300 flags |= kNoPendingIO_ScratchTextureFlag;
301 }
302 return this->internalRefScratchTexture(desc, flags);
303 }
304}
305
306GrTexture* GrContext::internalRefScratchTexture(const GrSurfaceDesc& inDesc, uint32_t flags) {
307 SkASSERT(!GrPixelConfigIsCompressed(inDesc.fConfig));
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000308
bsalomonf2703d82014-10-28 14:33:06 -0700309 SkTCopyOnFirstWrite<GrSurfaceDesc> desc(inDesc);
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000310
bsalomonf2703d82014-10-28 14:33:06 -0700311 if (fGpu->caps()->reuseScratchTextures() || (desc->fFlags & kRenderTarget_GrSurfaceFlag)) {
bsalomond0423582015-02-06 08:49:24 -0800312 if (!(kExact_ScratchTextureFlag & flags)) {
bsalomonbcf0a522014-10-08 08:40:09 -0700313 // bin by pow2 with a reasonable min
314 static const int MIN_SIZE = 16;
bsalomonf2703d82014-10-28 14:33:06 -0700315 GrSurfaceDesc* wdesc = desc.writable();
bsalomonbcf0a522014-10-08 08:40:09 -0700316 wdesc->fWidth = SkTMax(MIN_SIZE, GrNextPow2(desc->fWidth));
317 wdesc->fHeight = SkTMax(MIN_SIZE, GrNextPow2(desc->fHeight));
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000318 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000319
bsalomon6bc1b5f2015-02-23 09:06:38 -0800320 GrScratchKey key;
321 GrTexturePriv::ComputeScratchKey(*desc, &key);
322 uint32_t scratchFlags = 0;
323 if (kNoPendingIO_ScratchTextureFlag & flags) {
324 scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag;
325 } else if (!(desc->fFlags & kRenderTarget_GrSurfaceFlag)) {
326 // If it is not a render target then it will most likely be populated by
327 // writePixels() which will trigger a flush if the texture has pending IO.
328 scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag;
329 }
330 GrGpuResource* resource = fResourceCache->findAndRefScratchResource(key, scratchFlags);
331 if (resource) {
332 GrSurface* surface = static_cast<GrSurface*>(resource);
333 GrRenderTarget* rt = surface->asRenderTarget();
334 if (rt && fGpu->caps()->discardRenderTargetSupport()) {
335 rt->discard();
bsalomon000f8292014-10-15 19:04:14 -0700336 }
bsalomon6bc1b5f2015-02-23 09:06:38 -0800337 return surface->asTexture();
338 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000339 }
340
bsalomond0423582015-02-06 08:49:24 -0800341 if (!(kNoCreate_ScratchTextureFlag & flags)) {
bsalomon3582d3e2015-02-13 14:20:05 -0800342 return fGpu->createTexture(*desc, true, NULL, 0);
bsalomone167f962015-01-27 09:56:04 -0800343 }
bsalomond0423582015-02-06 08:49:24 -0800344
345 return NULL;
Brian Salomon9323b8b2014-10-07 15:07:38 -0400346}
347
bsalomon71cb0c22014-11-14 12:10:14 -0800348void GrContext::OverBudgetCB(void* data) {
bsalomon66a450f2014-11-13 13:19:10 -0800349 SkASSERT(data);
bsalomonf21dab92014-11-13 13:33:28 -0800350
bsalomon66a450f2014-11-13 13:19:10 -0800351 GrContext* context = reinterpret_cast<GrContext*>(data);
bsalomonf21dab92014-11-13 13:33:28 -0800352
353 // Flush the InOrderDrawBuffer to possibly free up some textures
354 context->fFlushToReduceCacheSize = true;
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000355}
356
joshualitt0db6dfa2015-04-10 07:01:30 -0700357void GrContext::TextBlobCacheOverBudgetCB(void* data) {
358 SkASSERT(data);
359
360 // Unlike the GrResourceCache, TextBlobs are drawn at the SkGpuDevice level, therefore they
361 // cannot use fFlushTorReduceCacheSize because it uses AutoCheckFlush. The solution is to move
362 // drawText calls to below the GrContext level, but this is not trivial because they call
363 // drawPath on SkGpuDevice
364 GrContext* context = reinterpret_cast<GrContext*>(data);
365 context->flush();
366}
367
bsalomon@google.com91958362011-06-13 17:58:13 +0000368int GrContext::getMaxTextureSize() const {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000369 return SkTMin(fGpu->caps()->maxTextureSize(), fMaxTextureSizeOverride);
bsalomon@google.com91958362011-06-13 17:58:13 +0000370}
371
372int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000373 return fGpu->caps()->maxRenderTargetSize();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000374}
375
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000376int GrContext::getMaxSampleCount() const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000377 return fGpu->caps()->maxSampleCount();
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000378}
379
bsalomon@google.com27847de2011-02-22 20:59:41 +0000380///////////////////////////////////////////////////////////////////////////////
381
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000382GrTexture* GrContext::wrapBackendTexture(const GrBackendTextureDesc& desc) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800383 RETURN_NULL_IF_ABANDONED
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000384 return fGpu->wrapBackendTexture(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000385}
386
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000387GrRenderTarget* GrContext::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800388 RETURN_NULL_IF_ABANDONED
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000389 return fGpu->wrapBackendRenderTarget(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000390}
391
bsalomon@google.com27847de2011-02-22 20:59:41 +0000392////////////////////////////////////////////////////////////////////////////////
393
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000394void GrContext::clear(const SkIRect* rect,
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000395 const GrColor color,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000396 bool canIgnoreRect,
bsalomon41ebbdd2014-08-04 08:31:39 -0700397 GrRenderTarget* renderTarget) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800398 RETURN_IF_ABANDONED
bsalomon41ebbdd2014-08-04 08:31:39 -0700399 ASSERT_OWNED_RESOURCE(renderTarget);
bsalomon89c62982014-11-03 12:08:42 -0800400 SkASSERT(renderTarget);
401
bsalomonf21dab92014-11-13 13:33:28 -0800402 AutoCheckFlush acf(this);
egdanield78a1682014-07-09 10:41:26 -0700403 GR_CREATE_TRACE_MARKER_CONTEXT("GrContext::clear", this);
joshualitt570d2f82015-02-25 13:19:48 -0800404 GrDrawTarget* target = this->prepareToDraw();
bsalomon41ebbdd2014-08-04 08:31:39 -0700405 if (NULL == target) {
406 return;
407 }
408 target->clear(rect, color, canIgnoreRect, renderTarget);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000409}
410
joshualitt25d9c152015-02-18 12:29:52 -0800411void GrContext::drawPaint(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800412 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800413 const GrPaint& origPaint,
414 const SkMatrix& viewMatrix) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800415 RETURN_IF_ABANDONED
bsalomon@google.com27847de2011-02-22 20:59:41 +0000416 // set rect to be big enough to fill the space, but not super-huge, so we
417 // don't overflow fixed-point implementations
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000418 SkRect r;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000419 r.setLTRB(0, 0,
joshualitt25d9c152015-02-18 12:29:52 -0800420 SkIntToScalar(rt->width()),
421 SkIntToScalar(rt->height()));
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000422 SkTCopyOnFirstWrite<GrPaint> paint(origPaint);
joshualitt8fc6c2d2014-12-22 15:27:05 -0800423
424 // by definition this fills the entire clip, no need for AA
425 if (paint->isAntiAlias()) {
426 paint.writable()->setAntiAlias(false);
427 }
428
429 bool isPerspective = viewMatrix.hasPerspective();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000430
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000431 // We attempt to map r by the inverse matrix and draw that. mapRect will
432 // map the four corners and bound them with a new rect. This will not
433 // produce a correct result for some perspective matrices.
joshualitt8fc6c2d2014-12-22 15:27:05 -0800434 if (!isPerspective) {
joshualitt5531d512014-12-17 15:50:11 -0800435 SkMatrix inverse;
436 if (!viewMatrix.invert(&inverse)) {
tfarina38406c82014-10-31 07:11:12 -0700437 SkDebugf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000438 return;
439 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000440 inverse.mapRect(&r);
joshualitt570d2f82015-02-25 13:19:48 -0800441 this->drawRect(rt, clip, *paint, viewMatrix, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000442 } else {
joshualitt8fc6c2d2014-12-22 15:27:05 -0800443 SkMatrix localMatrix;
444 if (!viewMatrix.invert(&localMatrix)) {
tfarina38406c82014-10-31 07:11:12 -0700445 SkDebugf("Could not invert matrix\n");
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000446 return;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000447 }
joshualitt8fc6c2d2014-12-22 15:27:05 -0800448
449 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -0800450 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -0800451 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, paint, &acf);
joshualitt8fc6c2d2014-12-22 15:27:05 -0800452 if (NULL == target) {
453 return;
454 }
455
456 GR_CREATE_TRACE_MARKER("GrContext::drawPaintWithPerspective", target);
joshualitt44701df2015-02-23 14:44:57 -0800457 target->drawRect(&pipelineBuilder,
458 paint->getColor(),
459 SkMatrix::I(),
460 r,
461 NULL,
462 &localMatrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000463 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000464}
465
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000466#ifdef SK_DEVELOPER
467void GrContext::dumpFontCache() const {
468 fFontCache->dump();
469}
470#endif
471
bsalomon@google.com205d4602011-04-25 12:43:45 +0000472////////////////////////////////////////////////////////////////////////////////
473
bsalomonc30aaa02014-08-13 07:15:29 -0700474static inline bool is_irect(const SkRect& r) {
tfarina38406c82014-10-31 07:11:12 -0700475 return SkScalarIsInt(r.fLeft) && SkScalarIsInt(r.fTop) &&
476 SkScalarIsInt(r.fRight) && SkScalarIsInt(r.fBottom);
bsalomonc30aaa02014-08-13 07:15:29 -0700477}
478
bsalomon@google.com205d4602011-04-25 12:43:45 +0000479static bool apply_aa_to_rect(GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -0800480 GrPipelineBuilder* pipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -0800481 SkRect* devBoundRect,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000482 const SkRect& rect,
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000483 SkScalar strokeWidth,
joshualitt2e3b3e32014-12-09 13:31:14 -0800484 const SkMatrix& combinedMatrix,
485 GrColor color) {
egdaniel8dd688b2015-01-22 10:16:09 -0800486 if (pipelineBuilder->getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000487 return false;
488 }
489
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000490#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000491 if (strokeWidth >= 0) {
492#endif
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000493 if (!combinedMatrix.preservesAxisAlignment()) {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000494 return false;
495 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000496
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000497#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000498 } else {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000499 if (!combinedMatrix.preservesRightAngles()) {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000500 return false;
501 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000502 }
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000503#endif
bsalomon@google.com205d4602011-04-25 12:43:45 +0000504
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000505 combinedMatrix.mapRect(devBoundRect, rect);
derekfe638d1c2014-12-02 13:51:29 -0800506 if (!combinedMatrix.rectStaysRect()) {
507 return true;
508 }
509
bsalomonc30aaa02014-08-13 07:15:29 -0700510 if (strokeWidth < 0) {
511 return !is_irect(*devBoundRect);
512 }
robertphillips@google.com28ac96e2013-05-13 13:38:35 +0000513
bsalomon9c0822a2014-08-11 11:07:48 -0700514 return true;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000515}
516
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000517static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) {
518 return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
519 point.fY >= rect.fTop && point.fY <= rect.fBottom;
520}
521
joshualitt0823bfa2015-02-27 10:06:15 -0800522class StrokeRectBatch : public GrBatch {
523public:
524 struct Geometry {
525 GrColor fColor;
526 SkMatrix fViewMatrix;
527 SkRect fRect;
528 SkScalar fStrokeWidth;
529 };
530
531 static GrBatch* Create(const Geometry& geometry) {
532 return SkNEW_ARGS(StrokeRectBatch, (geometry));
533 }
534
mtklein36352bf2015-03-25 18:17:31 -0700535 const char* name() const override { return "StrokeRectBatch"; }
joshualitt0823bfa2015-02-27 10:06:15 -0800536
mtklein36352bf2015-03-25 18:17:31 -0700537 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt0823bfa2015-02-27 10:06:15 -0800538 // When this is called on a batch, there is only one geometry bundle
539 out->setKnownFourComponents(fGeoData[0].fColor);
540 }
541
mtklein36352bf2015-03-25 18:17:31 -0700542 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualitt0823bfa2015-02-27 10:06:15 -0800543 out->setKnownSingleComponent(0xff);
544 }
545
mtklein36352bf2015-03-25 18:17:31 -0700546 void initBatchTracker(const GrPipelineInfo& init) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800547 // Handle any color overrides
548 if (init.fColorIgnored) {
549 fGeoData[0].fColor = GrColor_ILLEGAL;
550 } else if (GrColor_ILLEGAL != init.fOverrideColor) {
551 fGeoData[0].fColor = init.fOverrideColor;
552 }
553
554 // setup batch properties
555 fBatch.fColorIgnored = init.fColorIgnored;
556 fBatch.fColor = fGeoData[0].fColor;
557 fBatch.fUsesLocalCoords = init.fUsesLocalCoords;
558 fBatch.fCoverageIgnored = init.fCoverageIgnored;
559 }
560
mtklein36352bf2015-03-25 18:17:31 -0700561 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800562 SkAutoTUnref<const GrGeometryProcessor> gp(
563 GrDefaultGeoProcFactory::Create(GrDefaultGeoProcFactory::kPosition_GPType,
564 this->color(),
565 this->viewMatrix(),
566 SkMatrix::I()));
567
568 batchTarget->initDraw(gp, pipeline);
569
570 // TODO this is hacky, but the only way we have to initialize the GP is to use the
571 // GrPipelineInfo struct so we can generate the correct shader. Once we have GrBatch
572 // everywhere we can remove this nastiness
573 GrPipelineInfo init;
574 init.fColorIgnored = fBatch.fColorIgnored;
575 init.fOverrideColor = GrColor_ILLEGAL;
576 init.fCoverageIgnored = fBatch.fCoverageIgnored;
577 init.fUsesLocalCoords = this->usesLocalCoords();
578 gp->initBatchTracker(batchTarget->currentBatchTracker(), init);
579
580 size_t vertexStride = gp->getVertexStride();
581
582 SkASSERT(vertexStride == sizeof(GrDefaultGeoProcFactory::PositionAttr));
583
584 Geometry& args = fGeoData[0];
585
586 int vertexCount = kVertsPerHairlineRect;
587 if (args.fStrokeWidth > 0) {
588 vertexCount = kVertsPerStrokeRect;
589 }
590
591 const GrVertexBuffer* vertexBuffer;
592 int firstVertex;
593
594 void* vertices = batchTarget->vertexPool()->makeSpace(vertexStride,
595 vertexCount,
596 &vertexBuffer,
597 &firstVertex);
598
joshualitt4b31de82015-03-05 14:33:41 -0800599 if (!vertices) {
600 SkDebugf("Could not allocate vertices\n");
601 return;
602 }
603
joshualitt0823bfa2015-02-27 10:06:15 -0800604 SkPoint* vertex = reinterpret_cast<SkPoint*>(vertices);
605
606 GrPrimitiveType primType;
607
608 if (args.fStrokeWidth > 0) {;
609 primType = kTriangleStrip_GrPrimitiveType;
610 args.fRect.sort();
611 this->setStrokeRectStrip(vertex, args.fRect, args.fStrokeWidth);
612 } else {
613 // hairline
614 primType = kLineStrip_GrPrimitiveType;
615 vertex[0].set(args.fRect.fLeft, args.fRect.fTop);
616 vertex[1].set(args.fRect.fRight, args.fRect.fTop);
617 vertex[2].set(args.fRect.fRight, args.fRect.fBottom);
618 vertex[3].set(args.fRect.fLeft, args.fRect.fBottom);
619 vertex[4].set(args.fRect.fLeft, args.fRect.fTop);
620 }
621
622 GrDrawTarget::DrawInfo drawInfo;
623 drawInfo.setPrimitiveType(primType);
624 drawInfo.setVertexBuffer(vertexBuffer);
625 drawInfo.setStartVertex(firstVertex);
626 drawInfo.setVertexCount(vertexCount);
627 drawInfo.setStartIndex(0);
628 drawInfo.setIndexCount(0);
629 drawInfo.setInstanceCount(0);
630 drawInfo.setVerticesPerInstance(0);
631 drawInfo.setIndicesPerInstance(0);
632 batchTarget->draw(drawInfo);
633 }
634
635 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
636
637private:
638 StrokeRectBatch(const Geometry& geometry) {
639 this->initClassID<StrokeRectBatch>();
640
641 fBatch.fHairline = geometry.fStrokeWidth == 0;
642
643 fGeoData.push_back(geometry);
644 }
645
646 /* create a triangle strip that strokes the specified rect. There are 8
647 unique vertices, but we repeat the last 2 to close up. Alternatively we
648 could use an indices array, and then only send 8 verts, but not sure that
649 would be faster.
650 */
651 void setStrokeRectStrip(SkPoint verts[10], const SkRect& rect, SkScalar width) {
652 const SkScalar rad = SkScalarHalf(width);
653 // TODO we should be able to enable this assert, but we'd have to filter these draws
654 // this is a bug
655 //SkASSERT(rad < rect.width() / 2 && rad < rect.height() / 2);
656
657 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
658 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
659 verts[2].set(rect.fRight - rad, rect.fTop + rad);
660 verts[3].set(rect.fRight + rad, rect.fTop - rad);
661 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
662 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
663 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
664 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
665 verts[8] = verts[0];
666 verts[9] = verts[1];
667 }
668
669
670 GrColor color() const { return fBatch.fColor; }
671 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
672 bool colorIgnored() const { return fBatch.fColorIgnored; }
673 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
674 bool hairline() const { return fBatch.fHairline; }
675
mtklein36352bf2015-03-25 18:17:31 -0700676 bool onCombineIfPossible(GrBatch* t) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800677 // StrokeRectBatch* that = t->cast<StrokeRectBatch>();
678
679 // NonAA stroke rects cannot batch right now
680 // TODO make these batchable
681 return false;
682 }
683
684 struct BatchTracker {
685 GrColor fColor;
686 bool fUsesLocalCoords;
687 bool fColorIgnored;
688 bool fCoverageIgnored;
689 bool fHairline;
690 };
691
692 const static int kVertsPerHairlineRect = 5;
693 const static int kVertsPerStrokeRect = 10;
694
695 BatchTracker fBatch;
696 SkSTArray<1, Geometry, true> fGeoData;
697};
698
joshualitt25d9c152015-02-18 12:29:52 -0800699void GrContext::drawRect(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800700 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800701 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -0800702 const SkMatrix& viewMatrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000703 const SkRect& rect,
bsalomon01c8da12014-08-04 09:21:30 -0700704 const GrStrokeInfo* strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800705 RETURN_IF_ABANDONED
bsalomon49f085d2014-09-05 13:34:00 -0700706 if (strokeInfo && strokeInfo->isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700707 SkPath path;
708 path.addRect(rect);
joshualitt570d2f82015-02-25 13:19:48 -0800709 this->drawPath(rt, clip, paint, viewMatrix, path, *strokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -0700710 return;
711 }
712
bsalomonf21dab92014-11-13 13:33:28 -0800713 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -0800714 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -0800715 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700716 if (NULL == target) {
717 return;
718 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000719
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000720 GR_CREATE_TRACE_MARKER("GrContext::drawRect", target);
egdanield58a0ba2014-06-11 10:30:05 -0700721 SkScalar width = NULL == strokeInfo ? -1 : strokeInfo->getStrokeRec().getWidth();
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000722
723 // Check if this is a full RT draw and can be replaced with a clear. We don't bother checking
724 // cases where the RT is fully inside a stroke.
725 if (width < 0) {
726 SkRect rtRect;
egdaniel8dd688b2015-01-22 10:16:09 -0800727 pipelineBuilder.getRenderTarget()->getBoundsRect(&rtRect);
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000728 SkRect clipSpaceRTRect = rtRect;
joshualitt570d2f82015-02-25 13:19:48 -0800729 bool checkClip = GrClip::kWideOpen_ClipType != clip.clipType();
joshualitt44701df2015-02-23 14:44:57 -0800730 if (checkClip) {
joshualitt570d2f82015-02-25 13:19:48 -0800731 clipSpaceRTRect.offset(SkIntToScalar(clip.origin().fX),
732 SkIntToScalar(clip.origin().fY));
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000733 }
734 // Does the clip contain the entire RT?
joshualitt570d2f82015-02-25 13:19:48 -0800735 if (!checkClip || clip.quickContains(clipSpaceRTRect)) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000736 SkMatrix invM;
joshualitt8059eb92014-12-29 15:10:07 -0800737 if (!viewMatrix.invert(&invM)) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000738 return;
739 }
740 // Does the rect bound the RT?
741 SkPoint srcSpaceRTQuad[4];
742 invM.mapRectToQuad(srcSpaceRTQuad, rtRect);
743 if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) &&
744 rect_contains_inclusive(rect, srcSpaceRTQuad[1]) &&
745 rect_contains_inclusive(rect, srcSpaceRTQuad[2]) &&
746 rect_contains_inclusive(rect, srcSpaceRTQuad[3])) {
747 // Will it blend?
748 GrColor clearColor;
749 if (paint.isOpaqueAndConstantColor(&clearColor)) {
joshualitt25d9c152015-02-18 12:29:52 -0800750 target->clear(NULL, clearColor, true, rt);
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000751 return;
752 }
753 }
754 }
755 }
756
joshualitt2e3b3e32014-12-09 13:31:14 -0800757 GrColor color = paint.getColor();
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000758 SkRect devBoundRect;
egdaniel8dd688b2015-01-22 10:16:09 -0800759 bool needAA = paint.isAntiAlias() && !pipelineBuilder.getRenderTarget()->isMultisampled();
760 bool doAA = needAA && apply_aa_to_rect(target, &pipelineBuilder, &devBoundRect, rect, width,
joshualitt8059eb92014-12-29 15:10:07 -0800761 viewMatrix, color);
egdanield58a0ba2014-06-11 10:30:05 -0700762
bsalomon@google.com205d4602011-04-25 12:43:45 +0000763 if (doAA) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000764 if (width >= 0) {
bsalomon395ef052014-11-12 11:35:22 -0800765 const SkStrokeRec& strokeRec = strokeInfo->getStrokeRec();
joshualitt9853cce2014-11-17 14:22:48 -0800766 fAARectRenderer->strokeAARect(target,
egdaniel8dd688b2015-01-22 10:16:09 -0800767 &pipelineBuilder,
joshualitt2e3b3e32014-12-09 13:31:14 -0800768 color,
joshualitt8059eb92014-12-29 15:10:07 -0800769 viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800770 rect,
joshualitt9853cce2014-11-17 14:22:48 -0800771 devBoundRect,
772 strokeRec);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000773 } else {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000774 // filled AA rect
joshualitt8059eb92014-12-29 15:10:07 -0800775 fAARectRenderer->fillAARect(target,
egdaniel8dd688b2015-01-22 10:16:09 -0800776 &pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800777 color,
778 viewMatrix,
779 rect,
joshualittd27f73e2014-12-29 07:43:36 -0800780 devBoundRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000781 }
782 return;
783 }
784
bsalomon@google.com27847de2011-02-22 20:59:41 +0000785 if (width >= 0) {
joshualitt0823bfa2015-02-27 10:06:15 -0800786 StrokeRectBatch::Geometry geometry;
787 geometry.fViewMatrix = viewMatrix;
788 geometry.fColor = color;
789 geometry.fRect = rect;
790 geometry.fStrokeWidth = width;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000791
joshualitt0823bfa2015-02-27 10:06:15 -0800792 SkAutoTUnref<GrBatch> batch(StrokeRectBatch::Create(geometry));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000793
joshualitt0823bfa2015-02-27 10:06:15 -0800794 SkRect bounds = rect;
795 SkScalar rad = SkScalarHalf(width);
796 bounds.outset(rad, rad);
797 viewMatrix.mapRect(&bounds);
798 target->drawBatch(&pipelineBuilder, batch, &bounds);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000799 } else {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000800 // filled BW rect
egdaniel8dd688b2015-01-22 10:16:09 -0800801 target->drawSimpleRect(&pipelineBuilder, color, viewMatrix, rect);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000802 }
803}
804
joshualitt25d9c152015-02-18 12:29:52 -0800805void GrContext::drawNonAARectToRect(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800806 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800807 const GrPaint& paint,
joshualitt16b27892014-12-18 07:47:16 -0800808 const SkMatrix& viewMatrix,
809 const SkRect& rectToDraw,
810 const SkRect& localRect,
811 const SkMatrix* localMatrix) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800812 RETURN_IF_ABANDONED
bsalomonf21dab92014-11-13 13:33:28 -0800813 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -0800814 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -0800815 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700816 if (NULL == target) {
817 return;
818 }
bsalomon@google.com64386952013-02-08 21:22:44 +0000819
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000820 GR_CREATE_TRACE_MARKER("GrContext::drawRectToRect", target);
821
joshualitt44701df2015-02-23 14:44:57 -0800822 target->drawRect(&pipelineBuilder,
823 paint.getColor(),
824 viewMatrix,
825 rectToDraw,
826 &localRect,
egdaniel8dd688b2015-01-22 10:16:09 -0800827 localMatrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000828}
829
joshualitt0823bfa2015-02-27 10:06:15 -0800830static const GrGeometryProcessor* set_vertex_attributes(bool hasLocalCoords,
831 bool hasColors,
joshualitt56995b52014-12-11 15:44:02 -0800832 int* colorOffset,
833 int* texOffset,
joshualitt8059eb92014-12-29 15:10:07 -0800834 GrColor color,
835 const SkMatrix& viewMatrix) {
robertphillips@google.com42903302013-04-20 12:26:07 +0000836 *texOffset = -1;
837 *colorOffset = -1;
joshualitt5478d422014-11-14 16:00:38 -0800838 uint32_t flags = GrDefaultGeoProcFactory::kPosition_GPType;
joshualitt0823bfa2015-02-27 10:06:15 -0800839 if (hasLocalCoords && hasColors) {
joshualitt5478d422014-11-14 16:00:38 -0800840 *colorOffset = sizeof(SkPoint);
841 *texOffset = sizeof(SkPoint) + sizeof(GrColor);
842 flags |= GrDefaultGeoProcFactory::kColor_GPType |
843 GrDefaultGeoProcFactory::kLocalCoord_GPType;
joshualitt0823bfa2015-02-27 10:06:15 -0800844 } else if (hasLocalCoords) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000845 *texOffset = sizeof(SkPoint);
joshualitt5478d422014-11-14 16:00:38 -0800846 flags |= GrDefaultGeoProcFactory::kLocalCoord_GPType;
joshualitt0823bfa2015-02-27 10:06:15 -0800847 } else if (hasColors) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000848 *colorOffset = sizeof(SkPoint);
joshualitt5478d422014-11-14 16:00:38 -0800849 flags |= GrDefaultGeoProcFactory::kColor_GPType;
robertphillips@google.com42903302013-04-20 12:26:07 +0000850 }
joshualitt8059eb92014-12-29 15:10:07 -0800851 return GrDefaultGeoProcFactory::Create(flags, color, viewMatrix, SkMatrix::I());
robertphillips@google.com42903302013-04-20 12:26:07 +0000852}
853
joshualitt0823bfa2015-02-27 10:06:15 -0800854class DrawVerticesBatch : public GrBatch {
855public:
856 struct Geometry {
857 GrColor fColor;
858 SkTDArray<SkPoint> fPositions;
859 SkTDArray<uint16_t> fIndices;
860 SkTDArray<GrColor> fColors;
861 SkTDArray<SkPoint> fLocalCoords;
862 };
863
864 static GrBatch* Create(const Geometry& geometry, GrPrimitiveType primitiveType,
865 const SkMatrix& viewMatrix,
866 const SkPoint* positions, int vertexCount,
867 const uint16_t* indices, int indexCount,
868 const GrColor* colors, const SkPoint* localCoords) {
869 return SkNEW_ARGS(DrawVerticesBatch, (geometry, primitiveType, viewMatrix, positions,
870 vertexCount, indices, indexCount, colors,
871 localCoords));
872 }
873
mtklein36352bf2015-03-25 18:17:31 -0700874 const char* name() const override { return "DrawVerticesBatch"; }
joshualitt0823bfa2015-02-27 10:06:15 -0800875
mtklein36352bf2015-03-25 18:17:31 -0700876 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt0823bfa2015-02-27 10:06:15 -0800877 // When this is called on a batch, there is only one geometry bundle
878 if (this->hasColors()) {
879 out->setUnknownFourComponents();
880 } else {
881 out->setKnownFourComponents(fGeoData[0].fColor);
882 }
883 }
884
mtklein36352bf2015-03-25 18:17:31 -0700885 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualittd4c7d552015-02-27 13:00:56 -0800886 out->setKnownSingleComponent(0xff);
joshualitt0823bfa2015-02-27 10:06:15 -0800887 }
888
mtklein36352bf2015-03-25 18:17:31 -0700889 void initBatchTracker(const GrPipelineInfo& init) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800890 // Handle any color overrides
891 if (init.fColorIgnored) {
892 fGeoData[0].fColor = GrColor_ILLEGAL;
893 } else if (GrColor_ILLEGAL != init.fOverrideColor) {
894 fGeoData[0].fColor = init.fOverrideColor;
895 }
896
897 // setup batch properties
898 fBatch.fColorIgnored = init.fColorIgnored;
899 fBatch.fColor = fGeoData[0].fColor;
900 fBatch.fUsesLocalCoords = init.fUsesLocalCoords;
901 fBatch.fCoverageIgnored = init.fCoverageIgnored;
902 }
903
mtklein36352bf2015-03-25 18:17:31 -0700904 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800905 int colorOffset = -1, texOffset = -1;
906 SkAutoTUnref<const GrGeometryProcessor> gp(
907 set_vertex_attributes(this->hasLocalCoords(), this->hasColors(), &colorOffset,
908 &texOffset, this->color(), this->viewMatrix()));
909
910 batchTarget->initDraw(gp, pipeline);
911
912 // TODO this is hacky, but the only way we have to initialize the GP is to use the
913 // GrPipelineInfo struct so we can generate the correct shader. Once we have GrBatch
914 // everywhere we can remove this nastiness
915 GrPipelineInfo init;
916 init.fColorIgnored = fBatch.fColorIgnored;
917 init.fOverrideColor = GrColor_ILLEGAL;
918 init.fCoverageIgnored = fBatch.fCoverageIgnored;
919 init.fUsesLocalCoords = this->usesLocalCoords();
920 gp->initBatchTracker(batchTarget->currentBatchTracker(), init);
921
922 size_t vertexStride = gp->getVertexStride();
923
924 SkASSERT(vertexStride == sizeof(SkPoint) + (this->hasLocalCoords() ? sizeof(SkPoint) : 0)
925 + (this->hasColors() ? sizeof(GrColor) : 0));
926
927 int instanceCount = fGeoData.count();
928
929 const GrVertexBuffer* vertexBuffer;
930 int firstVertex;
931
932 void* vertices = batchTarget->vertexPool()->makeSpace(vertexStride,
933 this->vertexCount(),
934 &vertexBuffer,
935 &firstVertex);
936
joshualitt4b31de82015-03-05 14:33:41 -0800937 if (!vertices) {
938 SkDebugf("Could not allocate vertices\n");
939 return;
940 }
941
joshualitt0823bfa2015-02-27 10:06:15 -0800942 const GrIndexBuffer* indexBuffer;
943 int firstIndex;
944
945 void* indices = NULL;
946 if (this->hasIndices()) {
947 indices = batchTarget->indexPool()->makeSpace(this->indexCount(),
948 &indexBuffer,
949 &firstIndex);
joshualitt4b31de82015-03-05 14:33:41 -0800950
951 if (!indices) {
952 SkDebugf("Could not allocate indices\n");
953 return;
954 }
joshualitt0823bfa2015-02-27 10:06:15 -0800955 }
956
957 int indexOffset = 0;
958 int vertexOffset = 0;
959 for (int i = 0; i < instanceCount; i++) {
960 const Geometry& args = fGeoData[i];
961
962 // TODO we can actually cache this interleaved and then just memcopy
963 if (this->hasIndices()) {
964 for (int j = 0; j < args.fIndices.count(); ++j, ++indexOffset) {
965 *((uint16_t*)indices + indexOffset) = args.fIndices[j] + vertexOffset;
966 }
967 }
968
969 for (int j = 0; j < args.fPositions.count(); ++j) {
970 *((SkPoint*)vertices) = args.fPositions[j];
971 if (this->hasColors()) {
972 *(GrColor*)((intptr_t)vertices + colorOffset) = args.fColors[j];
973 }
974 if (this->hasLocalCoords()) {
975 *(SkPoint*)((intptr_t)vertices + texOffset) = args.fLocalCoords[j];
976 }
977 vertices = (void*)((intptr_t)vertices + vertexStride);
978 vertexOffset++;
979 }
980 }
981
982 GrDrawTarget::DrawInfo drawInfo;
983 drawInfo.setPrimitiveType(this->primitiveType());
984 drawInfo.setVertexBuffer(vertexBuffer);
985 drawInfo.setStartVertex(firstVertex);
986 drawInfo.setVertexCount(this->vertexCount());
987 if (this->hasIndices()) {
988 drawInfo.setIndexBuffer(indexBuffer);
989 drawInfo.setStartIndex(firstIndex);
990 drawInfo.setIndexCount(this->indexCount());
991 } else {
992 drawInfo.setStartIndex(0);
993 drawInfo.setIndexCount(0);
994 }
995 batchTarget->draw(drawInfo);
996 }
997
998 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
999
1000private:
1001 DrawVerticesBatch(const Geometry& geometry, GrPrimitiveType primitiveType,
1002 const SkMatrix& viewMatrix,
1003 const SkPoint* positions, int vertexCount,
1004 const uint16_t* indices, int indexCount,
1005 const GrColor* colors, const SkPoint* localCoords) {
1006 this->initClassID<DrawVerticesBatch>();
1007 SkASSERT(positions);
1008
1009 fBatch.fViewMatrix = viewMatrix;
1010 Geometry& installedGeo = fGeoData.push_back(geometry);
1011
1012 installedGeo.fPositions.append(vertexCount, positions);
1013 if (indices) {
1014 installedGeo.fIndices.append(indexCount, indices);
1015 fBatch.fHasIndices = true;
1016 } else {
1017 fBatch.fHasIndices = false;
1018 }
1019
1020 if (colors) {
1021 installedGeo.fColors.append(vertexCount, colors);
1022 fBatch.fHasColors = true;
1023 } else {
1024 fBatch.fHasColors = false;
1025 }
1026
1027 if (localCoords) {
1028 installedGeo.fLocalCoords.append(vertexCount, localCoords);
1029 fBatch.fHasLocalCoords = true;
1030 } else {
1031 fBatch.fHasLocalCoords = false;
1032 }
1033 fBatch.fVertexCount = vertexCount;
1034 fBatch.fIndexCount = indexCount;
1035 fBatch.fPrimitiveType = primitiveType;
1036 }
1037
1038 GrPrimitiveType primitiveType() const { return fBatch.fPrimitiveType; }
1039 bool batchablePrimitiveType() const {
1040 return kTriangles_GrPrimitiveType == fBatch.fPrimitiveType ||
1041 kLines_GrPrimitiveType == fBatch.fPrimitiveType ||
1042 kPoints_GrPrimitiveType == fBatch.fPrimitiveType;
1043 }
1044 GrColor color() const { return fBatch.fColor; }
1045 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
1046 bool colorIgnored() const { return fBatch.fColorIgnored; }
1047 const SkMatrix& viewMatrix() const { return fBatch.fViewMatrix; }
1048 bool hasColors() const { return fBatch.fHasColors; }
1049 bool hasIndices() const { return fBatch.fHasIndices; }
1050 bool hasLocalCoords() const { return fBatch.fHasLocalCoords; }
1051 int vertexCount() const { return fBatch.fVertexCount; }
1052 int indexCount() const { return fBatch.fIndexCount; }
1053
mtklein36352bf2015-03-25 18:17:31 -07001054 bool onCombineIfPossible(GrBatch* t) override {
joshualitt0823bfa2015-02-27 10:06:15 -08001055 DrawVerticesBatch* that = t->cast<DrawVerticesBatch>();
1056
1057 if (!this->batchablePrimitiveType() || this->primitiveType() != that->primitiveType()) {
1058 return false;
1059 }
1060
1061 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
1062
1063 // We currently use a uniform viewmatrix for this batch
1064 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
1065 return false;
1066 }
1067
1068 if (this->hasColors() != that->hasColors()) {
1069 return false;
1070 }
1071
1072 if (this->hasIndices() != that->hasIndices()) {
1073 return false;
1074 }
1075
1076 if (this->hasLocalCoords() != that->hasLocalCoords()) {
1077 return false;
1078 }
1079
1080 if (!this->hasColors() && this->color() != that->color()) {
1081 return false;
1082 }
1083
1084 if (this->color() != that->color()) {
1085 fBatch.fColor = GrColor_ILLEGAL;
1086 }
1087 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
1088 fBatch.fVertexCount += that->vertexCount();
1089 fBatch.fIndexCount += that->indexCount();
1090 return true;
1091 }
1092
1093 struct BatchTracker {
1094 GrPrimitiveType fPrimitiveType;
1095 SkMatrix fViewMatrix;
1096 GrColor fColor;
1097 bool fUsesLocalCoords;
1098 bool fColorIgnored;
1099 bool fCoverageIgnored;
1100 bool fHasColors;
1101 bool fHasIndices;
1102 bool fHasLocalCoords;
1103 int fVertexCount;
1104 int fIndexCount;
1105 };
1106
1107 BatchTracker fBatch;
1108 SkSTArray<1, Geometry, true> fGeoData;
1109};
1110
joshualitt25d9c152015-02-18 12:29:52 -08001111void GrContext::drawVertices(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -08001112 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001113 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001114 const SkMatrix& viewMatrix,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001115 GrPrimitiveType primitiveType,
1116 int vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001117 const SkPoint positions[],
1118 const SkPoint texCoords[],
bsalomon@google.com27847de2011-02-22 20:59:41 +00001119 const GrColor colors[],
1120 const uint16_t indices[],
1121 int indexCount) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001122 RETURN_IF_ABANDONED
bsalomonf21dab92014-11-13 13:33:28 -08001123 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001124 GrPipelineBuilder pipelineBuilder;
bsalomonf21dab92014-11-13 13:33:28 -08001125 GrDrawTarget::AutoReleaseGeometry geo; // must be inside AutoCheckFlush scope
commit-bot@chromium.org5a567932014-01-08 21:26:09 +00001126
joshualitt570d2f82015-02-25 13:19:48 -08001127 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001128 if (NULL == target) {
1129 return;
1130 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001131
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001132 GR_CREATE_TRACE_MARKER("GrContext::drawVertices", target);
1133
joshualitt0823bfa2015-02-27 10:06:15 -08001134 DrawVerticesBatch::Geometry geometry;
1135 geometry.fColor = paint.getColor();
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001136
joshualitt0823bfa2015-02-27 10:06:15 -08001137 SkAutoTUnref<GrBatch> batch(DrawVerticesBatch::Create(geometry, primitiveType, viewMatrix,
1138 positions, vertexCount, indices,
1139 indexCount,colors, texCoords));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001140
joshualitt0823bfa2015-02-27 10:06:15 -08001141 // TODO figure out bounds
1142 target->drawBatch(&pipelineBuilder, batch, NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001143}
1144
bsalomon@google.com06afe7b2011-04-26 15:31:40 +00001145///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com93c96602012-04-27 13:05:21 +00001146
joshualitt25d9c152015-02-18 12:29:52 -08001147void GrContext::drawRRect(GrRenderTarget*rt,
joshualitt570d2f82015-02-25 13:19:48 -08001148 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001149 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001150 const SkMatrix& viewMatrix,
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001151 const SkRRect& rrect,
egdanield58a0ba2014-06-11 10:30:05 -07001152 const GrStrokeInfo& strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001153 RETURN_IF_ABANDONED
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001154 if (rrect.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001155 return;
1156 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001157
egdanield58a0ba2014-06-11 10:30:05 -07001158 if (strokeInfo.isDashed()) {
1159 SkPath path;
1160 path.addRRect(rrect);
joshualitt570d2f82015-02-25 13:19:48 -08001161 this->drawPath(rt, clip, paint, viewMatrix, path, strokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -07001162 return;
1163 }
1164
bsalomonf21dab92014-11-13 13:33:28 -08001165 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001166 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -08001167 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001168 if (NULL == target) {
1169 return;
1170 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001171
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001172 GR_CREATE_TRACE_MARKER("GrContext::drawRRect", target);
1173
egdanield58a0ba2014-06-11 10:30:05 -07001174 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1175
joshualitt2e3b3e32014-12-09 13:31:14 -08001176 GrColor color = paint.getColor();
joshualitt44701df2015-02-23 14:44:57 -08001177 if (!fOvalRenderer->drawRRect(target,
1178 &pipelineBuilder,
1179 color,
1180 viewMatrix,
1181 paint.isAntiAlias(),
1182 rrect,
1183 strokeRec)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001184 SkPath path;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001185 path.addRRect(rrect);
egdaniel8dd688b2015-01-22 10:16:09 -08001186 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(),
1187 path, strokeInfo);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001188 }
1189}
1190
1191///////////////////////////////////////////////////////////////////////////////
1192
joshualitt25d9c152015-02-18 12:29:52 -08001193void GrContext::drawDRRect(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -08001194 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001195 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001196 const SkMatrix& viewMatrix,
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001197 const SkRRect& outer,
1198 const SkRRect& inner) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001199 RETURN_IF_ABANDONED
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001200 if (outer.isEmpty()) {
1201 return;
1202 }
1203
bsalomonf21dab92014-11-13 13:33:28 -08001204 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001205 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -08001206 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001207
1208 GR_CREATE_TRACE_MARKER("GrContext::drawDRRect", target);
1209
joshualitt2e3b3e32014-12-09 13:31:14 -08001210 GrColor color = paint.getColor();
joshualitt44701df2015-02-23 14:44:57 -08001211 if (!fOvalRenderer->drawDRRect(target,
1212 &pipelineBuilder,
1213 color,
1214 viewMatrix,
1215 paint.isAntiAlias(),
1216 outer,
1217 inner)) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001218 SkPath path;
1219 path.addRRect(inner);
1220 path.addRRect(outer);
1221 path.setFillType(SkPath::kEvenOdd_FillType);
1222
egdanield58a0ba2014-06-11 10:30:05 -07001223 GrStrokeInfo fillRec(SkStrokeRec::kFill_InitStyle);
egdaniel8dd688b2015-01-22 10:16:09 -08001224 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(),
1225 path, fillRec);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001226 }
1227}
1228
1229///////////////////////////////////////////////////////////////////////////////
1230
joshualitt570d2f82015-02-25 13:19:48 -08001231void GrContext::drawOval(GrRenderTarget* rt,
1232 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001233 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001234 const SkMatrix& viewMatrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001235 const SkRect& oval,
egdanield58a0ba2014-06-11 10:30:05 -07001236 const GrStrokeInfo& strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001237 RETURN_IF_ABANDONED
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001238 if (oval.isEmpty()) {
1239 return;
1240 }
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001241
egdanield58a0ba2014-06-11 10:30:05 -07001242 if (strokeInfo.isDashed()) {
1243 SkPath path;
1244 path.addOval(oval);
joshualitt570d2f82015-02-25 13:19:48 -08001245 this->drawPath(rt, clip, paint, viewMatrix, path, strokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -07001246 return;
1247 }
1248
bsalomonf21dab92014-11-13 13:33:28 -08001249 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001250 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -08001251 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001252 if (NULL == target) {
1253 return;
1254 }
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001255
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001256 GR_CREATE_TRACE_MARKER("GrContext::drawOval", target);
1257
egdanield58a0ba2014-06-11 10:30:05 -07001258 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1259
joshualitt2e3b3e32014-12-09 13:31:14 -08001260 GrColor color = paint.getColor();
joshualitt44701df2015-02-23 14:44:57 -08001261 if (!fOvalRenderer->drawOval(target,
1262 &pipelineBuilder,
1263 color,
1264 viewMatrix,
1265 paint.isAntiAlias(),
1266 oval,
1267 strokeRec)) {
bsalomon@google.com93c96602012-04-27 13:05:21 +00001268 SkPath path;
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001269 path.addOval(oval);
egdaniel8dd688b2015-01-22 10:16:09 -08001270 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(),
1271 path, strokeInfo);
skia.committer@gmail.com98ded842013-01-23 07:06:17 +00001272 }
bsalomon@google.com150d2842012-01-12 20:19:56 +00001273}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001274
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001275// Can 'path' be drawn as a pair of filled nested rectangles?
1276static bool is_nested_rects(GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -08001277 GrPipelineBuilder* pipelineBuilder,
joshualitt2e3b3e32014-12-09 13:31:14 -08001278 GrColor color,
joshualitt8059eb92014-12-29 15:10:07 -08001279 const SkMatrix& viewMatrix,
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001280 const SkPath& path,
1281 const SkStrokeRec& stroke,
bsalomon9c0822a2014-08-11 11:07:48 -07001282 SkRect rects[2]) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001283 SkASSERT(stroke.isFillStyle());
1284
1285 if (path.isInverseFillType()) {
1286 return false;
1287 }
1288
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001289 // TODO: this restriction could be lifted if we were willing to apply
1290 // the matrix to all the points individually rather than just to the rect
joshualitt8059eb92014-12-29 15:10:07 -08001291 if (!viewMatrix.preservesAxisAlignment()) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001292 return false;
1293 }
1294
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001295 SkPath::Direction dirs[2];
caryclark95bc5f32015-04-08 08:34:15 -07001296 if (!path.isNestedFillRects(rects, dirs)) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001297 return false;
1298 }
1299
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001300 if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001301 // The two rects need to be wound opposite to each other
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001302 return false;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001303 }
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001304
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001305 // Right now, nested rects where the margin is not the same width
1306 // all around do not render correctly
1307 const SkScalar* outer = rects[0].asScalars();
1308 const SkScalar* inner = rects[1].asScalars();
1309
robertphillips183e9852014-10-21 11:25:37 -07001310 bool allEq = true;
1311
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001312 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
robertphillips183e9852014-10-21 11:25:37 -07001313 bool allGoE1 = margin >= SK_Scalar1;
1314
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001315 for (int i = 1; i < 4; ++i) {
1316 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
robertphillips183e9852014-10-21 11:25:37 -07001317 if (temp < SK_Scalar1) {
1318 allGoE1 = false;
1319 }
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001320 if (!SkScalarNearlyEqual(margin, temp)) {
robertphillips183e9852014-10-21 11:25:37 -07001321 allEq = false;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001322 }
1323 }
1324
robertphillips183e9852014-10-21 11:25:37 -07001325 return allEq || allGoE1;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001326}
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001327
joshualitt25d9c152015-02-18 12:29:52 -08001328void GrContext::drawPath(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -08001329 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001330 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001331 const SkMatrix& viewMatrix,
1332 const SkPath& path,
1333 const GrStrokeInfo& strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001334 RETURN_IF_ABANDONED
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001335 if (path.isEmpty()) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001336 if (path.isInverseFillType()) {
joshualitt570d2f82015-02-25 13:19:48 -08001337 this->drawPaint(rt, clip, paint, viewMatrix);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001338 }
1339 return;
1340 }
1341
joshualitt2e3b3e32014-12-09 13:31:14 -08001342 GrColor color = paint.getColor();
egdanield58a0ba2014-06-11 10:30:05 -07001343 if (strokeInfo.isDashed()) {
egdaniele61c4112014-06-12 10:24:21 -07001344 SkPoint pts[2];
1345 if (path.isLine(pts)) {
bsalomonf21dab92014-11-13 13:33:28 -08001346 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001347 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -08001348 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001349 if (NULL == target) {
1350 return;
joshualittd27f73e2014-12-29 07:43:36 -08001351 }
egdaniele61c4112014-06-12 10:24:21 -07001352
egdaniel8dd688b2015-01-22 10:16:09 -08001353 if (GrDashingEffect::DrawDashLine(fGpu, target, &pipelineBuilder, color, viewMatrix,
1354 pts, paint, strokeInfo)) {
joshualittd27f73e2014-12-29 07:43:36 -08001355 return;
egdaniele61c4112014-06-12 10:24:21 -07001356 }
1357 }
1358
1359 // Filter dashed path into new path with the dashing applied
egdanield58a0ba2014-06-11 10:30:05 -07001360 const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo();
1361 SkTLazy<SkPath> effectPath;
1362 GrStrokeInfo newStrokeInfo(strokeInfo, false);
1363 SkStrokeRec* stroke = newStrokeInfo.getStrokeRecPtr();
1364 if (SkDashPath::FilterDashPath(effectPath.init(), path, stroke, NULL, info)) {
joshualitt570d2f82015-02-25 13:19:48 -08001365 this->drawPath(rt, clip, paint, viewMatrix, *effectPath.get(), newStrokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -07001366 return;
1367 }
1368
joshualitt570d2f82015-02-25 13:19:48 -08001369 this->drawPath(rt, clip, paint, viewMatrix, path, newStrokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -07001370 return;
1371 }
1372
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001373 // Note that internalDrawPath may sw-rasterize the path into a scratch texture.
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001374 // Scratch textures can be recycled after they are returned to the texture
1375 // cache. This presents a potential hazard for buffered drawing. However,
1376 // the writePixels that uploads to the scratch will perform a flush so we're
1377 // OK.
bsalomonf21dab92014-11-13 13:33:28 -08001378 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001379 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -08001380 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001381 if (NULL == target) {
1382 return;
1383 }
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001384
egdaniel93a37bc2014-07-21 13:47:57 -07001385 GR_CREATE_TRACE_MARKER1("GrContext::drawPath", target, "Is Convex", path.isConvex());
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001386
egdanield58a0ba2014-06-11 10:30:05 -07001387 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1388
egdaniel8dd688b2015-01-22 10:16:09 -08001389 bool useCoverageAA = paint.isAntiAlias() &&
1390 !pipelineBuilder.getRenderTarget()->isMultisampled();
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001391
egdanield58a0ba2014-06-11 10:30:05 -07001392 if (useCoverageAA && strokeRec.getWidth() < 0 && !path.isConvex()) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001393 // Concave AA paths are expensive - try to avoid them for special cases
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001394 SkRect rects[2];
1395
egdaniel8dd688b2015-01-22 10:16:09 -08001396 if (is_nested_rects(target, &pipelineBuilder, color, viewMatrix, path, strokeRec, rects)) {
joshualitt44701df2015-02-23 14:44:57 -08001397 fAARectRenderer->fillAANestedRects(target, &pipelineBuilder, color, viewMatrix, rects);
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001398 return;
1399 }
1400 }
1401
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001402 SkRect ovalRect;
1403 bool isOval = path.isOval(&ovalRect);
1404
joshualitt8059eb92014-12-29 15:10:07 -08001405 if (!isOval || path.isInverseFillType() ||
joshualitt44701df2015-02-23 14:44:57 -08001406 !fOvalRenderer->drawOval(target,
1407 &pipelineBuilder,
1408 color,
1409 viewMatrix,
1410 paint.isAntiAlias(),
1411 ovalRect,
1412 strokeRec)) {
egdaniel8dd688b2015-01-22 10:16:09 -08001413 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(),
1414 path, strokeInfo);
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001415 }
1416}
1417
joshualitt9853cce2014-11-17 14:22:48 -08001418void GrContext::internalDrawPath(GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -08001419 GrPipelineBuilder* pipelineBuilder,
joshualitt5531d512014-12-17 15:50:11 -08001420 const SkMatrix& viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -08001421 GrColor color,
joshualitt9853cce2014-11-17 14:22:48 -08001422 bool useAA,
1423 const SkPath& path,
egdanield58a0ba2014-06-11 10:30:05 -07001424 const GrStrokeInfo& strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001425 RETURN_IF_ABANDONED
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001426 SkASSERT(!path.isEmpty());
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001427
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001428 GR_CREATE_TRACE_MARKER("GrContext::internalDrawPath", target);
1429
1430
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001431 // An Assumption here is that path renderer would use some form of tweaking
1432 // the src color (either the input alpha or in the frag shader) to implement
1433 // aa. If we have some future driver-mojo path AA that can do the right
1434 // thing WRT to the blend then we'll need some query on the PR.
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001435 bool useCoverageAA = useAA &&
egdaniel0bdeec92015-02-23 12:12:54 -08001436 !pipelineBuilder->getRenderTarget()->isMultisampled();
bsalomon@google.com289533a2011-10-27 12:34:25 +00001437
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001438
1439 GrPathRendererChain::DrawType type =
1440 useCoverageAA ? GrPathRendererChain::kColorAntiAlias_DrawType :
joshualitt9853cce2014-11-17 14:22:48 -08001441 GrPathRendererChain::kColor_DrawType;
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001442
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001443 const SkPath* pathPtr = &path;
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001444 SkTLazy<SkPath> tmpPath;
egdanield58a0ba2014-06-11 10:30:05 -07001445 SkTCopyOnFirstWrite<SkStrokeRec> stroke(strokeInfo.getStrokeRec());
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001446
1447 // Try a 1st time without stroking the path and without allowing the SW renderer
egdaniel8dd688b2015-01-22 10:16:09 -08001448 GrPathRenderer* pr = this->getPathRenderer(target, pipelineBuilder, viewMatrix, *pathPtr,
1449 *stroke, false, type);
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001450
robertphillips@google.come79f3202014-02-11 16:30:21 +00001451 if (NULL == pr) {
joshualitt5531d512014-12-17 15:50:11 -08001452 if (!GrPathRenderer::IsStrokeHairlineOrEquivalent(*stroke, viewMatrix, NULL)) {
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001453 // It didn't work the 1st time, so try again with the stroked path
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001454 if (stroke->applyToPath(tmpPath.init(), *pathPtr)) {
1455 pathPtr = tmpPath.get();
1456 stroke.writable()->setFillStyle();
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001457 if (pathPtr->isEmpty()) {
1458 return;
1459 }
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001460 }
1461 }
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001462
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001463 // This time, allow SW renderer
egdaniel8dd688b2015-01-22 10:16:09 -08001464 pr = this->getPathRenderer(target, pipelineBuilder, viewMatrix, *pathPtr, *stroke, true,
1465 type);
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001466 }
1467
robertphillips@google.come79f3202014-02-11 16:30:21 +00001468 if (NULL == pr) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001469#ifdef SK_DEBUG
tfarina38406c82014-10-31 07:11:12 -07001470 SkDebugf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001471#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001472 return;
1473 }
1474
egdaniel8dd688b2015-01-22 10:16:09 -08001475 pr->drawPath(target, pipelineBuilder, color, viewMatrix, *pathPtr, *stroke, useCoverageAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001476}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001477
bsalomon@google.com27847de2011-02-22 20:59:41 +00001478////////////////////////////////////////////////////////////////////////////////
1479
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001480void GrContext::flush(int flagsBitfield) {
robertphillips@google.come7db8d62013-07-04 11:48:52 +00001481 if (NULL == fDrawBuffer) {
1482 return;
1483 }
1484
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001485 if (kDiscard_FlushBit & flagsBitfield) {
1486 fDrawBuffer->reset();
1487 } else {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001488 fDrawBuffer->flush();
junov@google.com53a55842011-06-08 22:55:10 +00001489 }
bsalomon3f324322015-04-08 11:01:54 -07001490 fResourceCache->notifyFlushOccurred();
bsalomonf21dab92014-11-13 13:33:28 -08001491 fFlushToReduceCacheSize = false;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001492}
1493
bsalomon81beccc2014-10-13 12:32:55 -07001494bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
1495 const void* inPixels, size_t outRowBytes, void* outPixels) {
1496 SkSrcPixelInfo srcPI;
jvanverthfa1e8a72014-12-22 08:31:49 -08001497 if (!GrPixelConfig2ColorAndProfileType(srcConfig, &srcPI.fColorType, NULL)) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001498 return false;
1499 }
bsalomon81beccc2014-10-13 12:32:55 -07001500 srcPI.fAlphaType = kUnpremul_SkAlphaType;
1501 srcPI.fPixels = inPixels;
1502 srcPI.fRowBytes = inRowBytes;
1503
1504 SkDstPixelInfo dstPI;
1505 dstPI.fColorType = srcPI.fColorType;
1506 dstPI.fAlphaType = kPremul_SkAlphaType;
1507 dstPI.fPixels = outPixels;
1508 dstPI.fRowBytes = outRowBytes;
1509
1510 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001511}
1512
bsalomon81beccc2014-10-13 12:32:55 -07001513bool GrContext::writeSurfacePixels(GrSurface* surface,
1514 int left, int top, int width, int height,
1515 GrPixelConfig srcConfig, const void* buffer, size_t rowBytes,
1516 uint32_t pixelOpsFlags) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001517 RETURN_FALSE_IF_ABANDONED
bsalomon81beccc2014-10-13 12:32:55 -07001518 {
1519 GrTexture* texture = NULL;
1520 if (!(kUnpremul_PixelOpsFlag & pixelOpsFlags) && (texture = surface->asTexture()) &&
1521 fGpu->canWriteTexturePixels(texture, srcConfig)) {
1522
1523 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) &&
1524 surface->surfacePriv().hasPendingIO()) {
1525 this->flush();
1526 }
1527 return fGpu->writeTexturePixels(texture, left, top, width, height,
1528 srcConfig, buffer, rowBytes);
1529 // Don't need to check kFlushWrites_PixelOp here, we just did a direct write so the
1530 // upload is already flushed.
1531 }
1532 }
1533
1534 // If we didn't do a direct texture write then we upload the pixels to a texture and draw.
1535 GrRenderTarget* renderTarget = surface->asRenderTarget();
1536 if (NULL == renderTarget) {
1537 return false;
1538 }
1539
1540 // We ignore the preferred config unless it is a R/B swap of the src config. In that case
1541 // we will upload the original src data to a scratch texture but we will spoof it as the swapped
1542 // config. This scratch will then have R and B swapped. We correct for this by swapping again
1543 // when drawing the scratch to the dst using a conversion effect.
1544 bool swapRAndB = false;
1545 GrPixelConfig writeConfig = srcConfig;
1546 if (GrPixelConfigSwapRAndB(srcConfig) ==
1547 fGpu->preferredWritePixelsConfig(srcConfig, renderTarget->config())) {
1548 writeConfig = GrPixelConfigSwapRAndB(srcConfig);
1549 swapRAndB = true;
1550 }
1551
bsalomonf2703d82014-10-28 14:33:06 -07001552 GrSurfaceDesc desc;
bsalomon81beccc2014-10-13 12:32:55 -07001553 desc.fWidth = width;
1554 desc.fHeight = height;
1555 desc.fConfig = writeConfig;
bsalomone3059732014-10-14 11:47:22 -07001556 SkAutoTUnref<GrTexture> texture(this->refScratchTexture(desc, kApprox_ScratchTexMatch));
1557 if (!texture) {
bsalomon81beccc2014-10-13 12:32:55 -07001558 return false;
1559 }
1560
1561 SkAutoTUnref<const GrFragmentProcessor> fp;
1562 SkMatrix textureMatrix;
1563 textureMatrix.setIDiv(texture->width(), texture->height());
1564
1565 // allocate a tmp buffer and sw convert the pixels to premul
1566 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
1567
1568 if (kUnpremul_PixelOpsFlag & pixelOpsFlags) {
1569 if (!GrPixelConfigIs8888(srcConfig)) {
1570 return false;
1571 }
1572 fp.reset(this->createUPMToPMEffect(texture, swapRAndB, textureMatrix));
1573 // handle the unpremul step on the CPU if we couldn't create an effect to do it.
1574 if (NULL == fp) {
1575 size_t tmpRowBytes = 4 * width;
1576 tmpPixels.reset(width * height);
1577 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
1578 tmpPixels.get())) {
1579 return false;
1580 }
1581 rowBytes = tmpRowBytes;
1582 buffer = tmpPixels.get();
1583 }
1584 }
1585 if (NULL == fp) {
1586 fp.reset(GrConfigConversionEffect::Create(texture,
1587 swapRAndB,
1588 GrConfigConversionEffect::kNone_PMConversion,
1589 textureMatrix));
1590 }
1591
1592 // Even if the client told us not to flush, we still flush here. The client may have known that
1593 // writes to the original surface caused no data hazards, but they can't know that the scratch
1594 // we just got is safe.
1595 if (texture->surfacePriv().hasPendingIO()) {
1596 this->flush();
1597 }
1598 if (!fGpu->writeTexturePixels(texture, 0, 0, width, height,
1599 writeConfig, buffer, rowBytes)) {
1600 return false;
1601 }
1602
1603 SkMatrix matrix;
1604 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
1605
1606 // This function can be called in the midst of drawing another object (e.g., when uploading a
1607 // SW-rasterized clip while issuing a draw). So we push the current geometry state before
1608 // drawing a rect to the render target.
1609 // The bracket ensures we pop the stack if we wind up flushing below.
1610 {
joshualitt570d2f82015-02-25 13:19:48 -08001611 GrDrawTarget* drawTarget = this->prepareToDraw();
1612 if (!drawTarget) {
1613 return false;
1614 }
joshualitt9853cce2014-11-17 14:22:48 -08001615 GrDrawTarget::AutoGeometryPush agp(drawTarget);
1616
egdaniel8dd688b2015-01-22 10:16:09 -08001617 GrPipelineBuilder pipelineBuilder;
1618 pipelineBuilder.addColorProcessor(fp);
1619 pipelineBuilder.setRenderTarget(renderTarget);
joshualitt44701df2015-02-23 14:44:57 -08001620 drawTarget->drawSimpleRect(&pipelineBuilder,
1621 GrColor_WHITE,
1622 matrix,
joshualitt8059eb92014-12-29 15:10:07 -08001623 SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)));
bsalomon81beccc2014-10-13 12:32:55 -07001624 }
1625
1626 if (kFlushWrites_PixelOp & pixelOpsFlags) {
1627 this->flushSurfaceWrites(surface);
1628 }
1629
1630 return true;
1631}
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001632
reed@google.com7111d462014-03-25 16:20:24 +00001633// toggles between RGBA and BGRA
1634static SkColorType toggle_colortype32(SkColorType ct) {
1635 if (kRGBA_8888_SkColorType == ct) {
1636 return kBGRA_8888_SkColorType;
1637 } else {
1638 SkASSERT(kBGRA_8888_SkColorType == ct);
1639 return kRGBA_8888_SkColorType;
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001640 }
1641}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001642
bsalomon@google.com0342a852012-08-20 19:22:38 +00001643bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
1644 int left, int top, int width, int height,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001645 GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001646 uint32_t flags) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001647 RETURN_FALSE_IF_ABANDONED
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001648 ASSERT_OWNED_RESOURCE(target);
bsalomon89c62982014-11-03 12:08:42 -08001649 SkASSERT(target);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001650
bsalomonafbf2d62014-09-30 12:18:44 -07001651 if (!(kDontFlush_PixelOpsFlag & flags) && target->surfacePriv().hasPendingWrite()) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001652 this->flush();
1653 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001654
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001655 // Determine which conversions have to be applied: flipY, swapRAnd, and/or unpremul.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001656
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001657 // If fGpu->readPixels would incur a y-flip cost then we will read the pixels upside down. We'll
1658 // either do the flipY by drawing into a scratch with a matrix or on the cpu after the read.
1659 bool flipY = fGpu->readPixelsWillPayForYFlip(target, left, top,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001660 width, height, dstConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001661 rowBytes);
bsalomon@google.com9c680582013-02-06 18:17:50 +00001662 // We ignore the preferred config if it is different than our config unless it is an R/B swap.
1663 // In that case we'll perform an R and B swap while drawing to a scratch texture of the swapped
1664 // config. Then we will call readPixels on the scratch with the swapped config. The swaps during
1665 // the draw cancels out the fact that we call readPixels with a config that is R/B swapped from
1666 // dstConfig.
1667 GrPixelConfig readConfig = dstConfig;
1668 bool swapRAndB = false;
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +00001669 if (GrPixelConfigSwapRAndB(dstConfig) ==
1670 fGpu->preferredReadPixelsConfig(dstConfig, target->config())) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001671 readConfig = GrPixelConfigSwapRAndB(readConfig);
1672 swapRAndB = true;
1673 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001674
bsalomon@google.com0342a852012-08-20 19:22:38 +00001675 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001676
bsalomon@google.com9c680582013-02-06 18:17:50 +00001677 if (unpremul && !GrPixelConfigIs8888(dstConfig)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001678 // The unpremul flag is only allowed for these two configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001679 return false;
1680 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001681
bsalomon191bcc02014-11-14 11:31:13 -08001682 SkAutoTUnref<GrTexture> tempTexture;
1683
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001684 // If the src is a texture and we would have to do conversions after read pixels, we instead
1685 // do the conversions by drawing the src to a scratch texture. If we handle any of the
1686 // conversions in the draw we set the corresponding bool to false so that we don't reapply it
1687 // on the read back pixels.
1688 GrTexture* src = target->asTexture();
bsalomon49f085d2014-09-05 13:34:00 -07001689 if (src && (swapRAndB || unpremul || flipY)) {
bsalomon81beccc2014-10-13 12:32:55 -07001690 // Make the scratch a render so we can read its pixels.
bsalomonf2703d82014-10-28 14:33:06 -07001691 GrSurfaceDesc desc;
1692 desc.fFlags = kRenderTarget_GrSurfaceFlag;
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001693 desc.fWidth = width;
1694 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001695 desc.fConfig = readConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001696 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001697
bsalomon@google.com9c680582013-02-06 18:17:50 +00001698 // 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 +00001699 // match the passed rect. However, if we see many different size rectangles we will trash
1700 // our texture cache and pay the cost of creating and destroying many textures. So, we only
1701 // request an exact match when the caller is reading an entire RT.
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001702 ScratchTexMatch match = kApprox_ScratchTexMatch;
1703 if (0 == left &&
1704 0 == top &&
1705 target->width() == width &&
1706 target->height() == height &&
1707 fGpu->fullReadPixelsIsFasterThanPartial()) {
1708 match = kExact_ScratchTexMatch;
1709 }
bsalomon191bcc02014-11-14 11:31:13 -08001710 tempTexture.reset(this->refScratchTexture(desc, match));
1711 if (tempTexture) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001712 // compute a matrix to perform the draw
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001713 SkMatrix textureMatrix;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001714 textureMatrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001715 textureMatrix.postIDiv(src->width(), src->height());
1716
joshualittb0a8a372014-09-23 09:50:21 -07001717 SkAutoTUnref<const GrFragmentProcessor> fp;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001718 if (unpremul) {
joshualittb0a8a372014-09-23 09:50:21 -07001719 fp.reset(this->createPMToUPMEffect(src, swapRAndB, textureMatrix));
1720 if (fp) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001721 unpremul = false; // we no longer need to do this on CPU after the read back.
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001722 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001723 }
1724 // If we failed to create a PM->UPM effect and have no other conversions to perform then
1725 // there is no longer any point to using the scratch.
joshualittb0a8a372014-09-23 09:50:21 -07001726 if (fp || flipY || swapRAndB) {
1727 if (!fp) {
1728 fp.reset(GrConfigConversionEffect::Create(
1729 src, swapRAndB, GrConfigConversionEffect::kNone_PMConversion,
1730 textureMatrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001731 }
1732 swapRAndB = false; // we will handle the swap in the draw.
bsalomon@google.comc4364992011-11-07 15:54:49 +00001733
robertphillips@google.com13f181f2013-03-02 12:02:08 +00001734 // We protect the existing geometry here since it may not be
1735 // clear to the caller that a draw operation (i.e., drawSimpleRect)
1736 // can be invoked in this method
joshualitt5c55fef2014-10-31 14:04:35 -07001737 {
joshualitt9853cce2014-11-17 14:22:48 -08001738 GrDrawTarget::AutoGeometryPush agp(fDrawBuffer);
egdaniel8dd688b2015-01-22 10:16:09 -08001739 GrPipelineBuilder pipelineBuilder;
joshualitt5c55fef2014-10-31 14:04:35 -07001740 SkASSERT(fp);
egdaniel8dd688b2015-01-22 10:16:09 -08001741 pipelineBuilder.addColorProcessor(fp);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001742
egdaniel8dd688b2015-01-22 10:16:09 -08001743 pipelineBuilder.setRenderTarget(tempTexture->asRenderTarget());
joshualitt5c55fef2014-10-31 14:04:35 -07001744 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
joshualitt44701df2015-02-23 14:44:57 -08001745 fDrawBuffer->drawSimpleRect(&pipelineBuilder,
1746 GrColor_WHITE,
1747 SkMatrix::I(),
egdaniel8dd688b2015-01-22 10:16:09 -08001748 rect);
joshualitt5c55fef2014-10-31 14:04:35 -07001749 // we want to read back from the scratch's origin
1750 left = 0;
1751 top = 0;
bsalomon191bcc02014-11-14 11:31:13 -08001752 target = tempTexture->asRenderTarget();
joshualitt5c55fef2014-10-31 14:04:35 -07001753 }
1754 this->flushSurfaceWrites(target);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001755 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001756 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001757 }
joshualitt5c55fef2014-10-31 14:04:35 -07001758
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001759 if (!fGpu->readPixels(target,
1760 left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001761 readConfig, buffer, rowBytes)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001762 return false;
1763 }
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001764 // Perform any conversions we weren't able to perform using a scratch texture.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001765 if (unpremul || swapRAndB) {
reed@google.com7111d462014-03-25 16:20:24 +00001766 SkDstPixelInfo dstPI;
jvanverthfa1e8a72014-12-22 08:31:49 -08001767 if (!GrPixelConfig2ColorAndProfileType(dstConfig, &dstPI.fColorType, NULL)) {
reed@google.com7111d462014-03-25 16:20:24 +00001768 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001769 }
reed@google.com7111d462014-03-25 16:20:24 +00001770 dstPI.fAlphaType = kUnpremul_SkAlphaType;
1771 dstPI.fPixels = buffer;
1772 dstPI.fRowBytes = rowBytes;
1773
1774 SkSrcPixelInfo srcPI;
1775 srcPI.fColorType = swapRAndB ? toggle_colortype32(dstPI.fColorType) : dstPI.fColorType;
1776 srcPI.fAlphaType = kPremul_SkAlphaType;
1777 srcPI.fPixels = buffer;
1778 srcPI.fRowBytes = rowBytes;
1779
1780 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001781 }
1782 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001783}
1784
bsalomon87a94eb2014-11-03 14:28:32 -08001785void GrContext::prepareSurfaceForExternalRead(GrSurface* surface) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001786 RETURN_IF_ABANDONED
bsalomon87a94eb2014-11-03 14:28:32 -08001787 SkASSERT(surface);
1788 ASSERT_OWNED_RESOURCE(surface);
1789 if (surface->surfacePriv().hasPendingIO()) {
1790 this->flush();
1791 }
1792 GrRenderTarget* rt = surface->asRenderTarget();
1793 if (fGpu && rt) {
1794 fGpu->resolveRenderTarget(rt);
bsalomon41ebbdd2014-08-04 08:31:39 -07001795 }
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001796}
1797
bsalomon41ebbdd2014-08-04 08:31:39 -07001798void GrContext::discardRenderTarget(GrRenderTarget* renderTarget) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001799 RETURN_IF_ABANDONED
bsalomon41ebbdd2014-08-04 08:31:39 -07001800 SkASSERT(renderTarget);
1801 ASSERT_OWNED_RESOURCE(renderTarget);
bsalomonf21dab92014-11-13 13:33:28 -08001802 AutoCheckFlush acf(this);
joshualitt570d2f82015-02-25 13:19:48 -08001803 GrDrawTarget* target = this->prepareToDraw();
bsalomon41ebbdd2014-08-04 08:31:39 -07001804 if (NULL == target) {
1805 return;
1806 }
1807 target->discard(renderTarget);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001808}
1809
bsalomonf80bfed2014-10-07 05:56:02 -07001810void GrContext::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
1811 const SkIPoint& dstPoint, uint32_t pixelOpsFlags) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001812 RETURN_IF_ABANDONED
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001813 if (NULL == src || NULL == dst) {
1814 return;
1815 }
bsalomone3d4bf22014-09-23 09:15:03 -07001816 ASSERT_OWNED_RESOURCE(src);
junov2bb52102014-09-29 10:18:59 -07001817 ASSERT_OWNED_RESOURCE(dst);
Brian Salomon34a98952014-09-24 11:41:24 -04001818
bsalomonf80bfed2014-10-07 05:56:02 -07001819 // Since we're going to the draw target and not GPU, no need to check kNoFlush
1820 // here.
junov96c118e2014-09-26 13:09:47 -07001821
joshualitt570d2f82015-02-25 13:19:48 -08001822 GrDrawTarget* target = this->prepareToDraw();
junov96c118e2014-09-26 13:09:47 -07001823 if (NULL == target) {
1824 return;
1825 }
junov96c118e2014-09-26 13:09:47 -07001826 target->copySurface(dst, src, srcRect, dstPoint);
bsalomonf80bfed2014-10-07 05:56:02 -07001827
1828 if (kFlushWrites_PixelOp & pixelOpsFlags) {
1829 this->flush();
1830 }
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001831}
1832
bsalomonf80bfed2014-10-07 05:56:02 -07001833void GrContext::flushSurfaceWrites(GrSurface* surface) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001834 RETURN_IF_ABANDONED
bsalomonf80bfed2014-10-07 05:56:02 -07001835 if (surface->surfacePriv().hasPendingWrite()) {
1836 this->flush();
1837 }
1838}
1839
egdaniel8dd688b2015-01-22 10:16:09 -08001840GrDrawTarget* GrContext::prepareToDraw(GrPipelineBuilder* pipelineBuilder,
joshualitt25d9c152015-02-18 12:29:52 -08001841 GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -08001842 const GrClip& clip,
joshualitt9853cce2014-11-17 14:22:48 -08001843 const GrPaint* paint,
1844 const AutoCheckFlush* acf) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001845 if (NULL == fGpu || NULL == fDrawBuffer) {
bsalomon41ebbdd2014-08-04 08:31:39 -07001846 return NULL;
1847 }
1848
joshualitt570d2f82015-02-25 13:19:48 -08001849 ASSERT_OWNED_RESOURCE(rt);
1850 SkASSERT(rt && paint && acf);
1851 pipelineBuilder->setFromPaint(*paint, rt, clip);
1852 return fDrawBuffer;
1853}
1854
1855GrDrawTarget* GrContext::prepareToDraw() {
1856 if (NULL == fGpu) {
1857 return NULL;
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001858 }
joshualitt5c55fef2014-10-31 14:04:35 -07001859 return fDrawBuffer;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001860}
1861
robertphillips@google.com72176b22012-05-23 13:19:12 +00001862/*
1863 * This method finds a path renderer that can draw the specified path on
1864 * the provided target.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001865 * Due to its expense, the software path renderer has split out so it can
robertphillips@google.com72176b22012-05-23 13:19:12 +00001866 * can be individually allowed/disallowed via the "allowSW" boolean.
1867 */
joshualitt9853cce2014-11-17 14:22:48 -08001868GrPathRenderer* GrContext::getPathRenderer(const GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -08001869 const GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -08001870 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -08001871 const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001872 const SkStrokeRec& stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001873 bool allowSW,
1874 GrPathRendererChain::DrawType drawType,
1875 GrPathRendererChain::StencilSupport* stencilSupport) {
1876
bsalomon@google.com30085192011-08-19 15:42:31 +00001877 if (NULL == fPathRendererChain) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001878 fPathRendererChain = SkNEW_ARGS(GrPathRendererChain, (this));
bsalomon@google.com30085192011-08-19 15:42:31 +00001879 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001880
joshualitt9853cce2014-11-17 14:22:48 -08001881 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(target,
egdaniel8dd688b2015-01-22 10:16:09 -08001882 pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -08001883 viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -08001884 path,
sugoi@google.com12b4e272012-12-06 20:13:11 +00001885 stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001886 drawType,
1887 stencilSupport);
robertphillips@google.com72176b22012-05-23 13:19:12 +00001888
1889 if (NULL == pr && allowSW) {
1890 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001891 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001892 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001893 pr = fSoftwarePathRenderer;
1894 }
1895
1896 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001897}
1898
bsalomon@google.com27847de2011-02-22 20:59:41 +00001899////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00001900bool GrContext::isConfigRenderable(GrPixelConfig config, bool withMSAA) const {
1901 return fGpu->caps()->isConfigRenderable(config, withMSAA);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001902}
1903
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +00001904int GrContext::getRecommendedSampleCount(GrPixelConfig config,
1905 SkScalar dpi) const {
1906 if (!this->isConfigRenderable(config, true)) {
1907 return 0;
1908 }
1909 int chosenSampleCount = 0;
1910 if (fGpu->caps()->pathRenderingSupport()) {
1911 if (dpi >= 250.0f) {
1912 chosenSampleCount = 4;
1913 } else {
1914 chosenSampleCount = 16;
1915 }
1916 }
1917 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ?
1918 chosenSampleCount : 0;
1919}
1920
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001921void GrContext::setupDrawBuffer() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001922 SkASSERT(NULL == fDrawBuffer);
1923 SkASSERT(NULL == fDrawBufferVBAllocPool);
1924 SkASSERT(NULL == fDrawBufferIBAllocPool);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001925
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001926 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001927 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001928 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001929 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001930 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001931 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001932 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001933 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001934
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001935 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001936 fDrawBufferVBAllocPool,
1937 fDrawBufferIBAllocPool));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001938}
1939
bsalomon@google.com21c10c52013-06-13 17:44:07 +00001940GrDrawTarget* GrContext::getTextTarget() {
joshualitt570d2f82015-02-25 13:19:48 -08001941 return this->prepareToDraw();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001942}
1943
1944const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1945 return fGpu->getQuadIndexBuffer();
1946}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001947
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001948namespace {
1949void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
1950 GrConfigConversionEffect::PMConversion pmToUPM;
1951 GrConfigConversionEffect::PMConversion upmToPM;
1952 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
1953 *pmToUPMValue = pmToUPM;
1954 *upmToPMValue = upmToPM;
1955}
1956}
1957
joshualittb0a8a372014-09-23 09:50:21 -07001958const GrFragmentProcessor* GrContext::createPMToUPMEffect(GrTexture* texture,
1959 bool swapRAndB,
1960 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001961 if (!fDidTestPMConversions) {
1962 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001963 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001964 }
1965 GrConfigConversionEffect::PMConversion pmToUPM =
1966 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
1967 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001968 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001969 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001970 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001971 }
1972}
1973
joshualittb0a8a372014-09-23 09:50:21 -07001974const GrFragmentProcessor* GrContext::createUPMToPMEffect(GrTexture* texture,
1975 bool swapRAndB,
1976 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001977 if (!fDidTestPMConversions) {
1978 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001979 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001980 }
1981 GrConfigConversionEffect::PMConversion upmToPM =
1982 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
1983 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001984 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001985 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001986 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001987 }
1988}
1989
bsalomon37f9a262015-02-02 13:00:10 -08001990//////////////////////////////////////////////////////////////////////////////
1991
1992void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
1993 if (maxTextures) {
bsalomon0ea80f42015-02-11 10:49:59 -08001994 *maxTextures = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -08001995 }
1996 if (maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -08001997 *maxTextureBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -08001998 }
1999}
2000
2001void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -08002002 fResourceCache->setLimits(maxTextures, maxTextureBytes);
bsalomon37f9a262015-02-02 13:00:10 -08002003}
2004
bsalomonf99e9612015-02-19 08:24:16 -08002005void GrContext::addResourceToCache(const GrUniqueKey& key, GrGpuResource* resource) {
bsalomon37f9a262015-02-02 13:00:10 -08002006 ASSERT_OWNED_RESOURCE(resource);
bsalomonf99e9612015-02-19 08:24:16 -08002007 if (!resource) {
2008 return;
bsalomon37f9a262015-02-02 13:00:10 -08002009 }
bsalomonf99e9612015-02-19 08:24:16 -08002010 resource->resourcePriv().setUniqueKey(key);
bsalomon37f9a262015-02-02 13:00:10 -08002011}
2012
bsalomon8718aaf2015-02-19 07:24:21 -08002013bool GrContext::isResourceInCache(const GrUniqueKey& key) const {
2014 return fResourceCache->hasUniqueKey(key);
commit-bot@chromium.org95a2b0e2014-05-05 19:21:16 +00002015}
2016
bsalomon8718aaf2015-02-19 07:24:21 -08002017GrGpuResource* GrContext::findAndRefCachedResource(const GrUniqueKey& key) {
2018 return fResourceCache->findAndRefUniqueResource(key);
commit-bot@chromium.org95a2b0e2014-05-05 19:21:16 +00002019}
2020
bsalomon37f9a262015-02-02 13:00:10 -08002021//////////////////////////////////////////////////////////////////////////////
2022
egdanielbbcb38d2014-06-19 10:19:29 -07002023void GrContext::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
2024 fGpu->addGpuTraceMarker(marker);
bsalomon49f085d2014-09-05 13:34:00 -07002025 if (fDrawBuffer) {
egdanielbbcb38d2014-06-19 10:19:29 -07002026 fDrawBuffer->addGpuTraceMarker(marker);
2027 }
2028}
2029
2030void GrContext::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
2031 fGpu->removeGpuTraceMarker(marker);
bsalomon49f085d2014-09-05 13:34:00 -07002032 if (fDrawBuffer) {
egdanielbbcb38d2014-06-19 10:19:29 -07002033 fDrawBuffer->removeGpuTraceMarker(marker);
2034 }
2035}
2036