blob: 2dba56c391b50bb8bd3482ca904ca4262e7450ec [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);
bsalomon3033b9f2015-04-13 11:09:56 -0700218
219 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000220}
221
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000222void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
bsalomon71cb0c22014-11-14 12:10:14 -0800223 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800224 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800225 }
226 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800227 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800228 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000229}
230
kkinnunenc6cb56f2014-06-24 00:12:27 -0700231GrTextContext* GrContext::createTextContext(GrRenderTarget* renderTarget,
joshualitt6e8cd962015-03-20 10:30:14 -0700232 SkGpuDevice* gpuDevice,
kkinnunenc6cb56f2014-06-24 00:12:27 -0700233 const SkDeviceProperties&
234 leakyProperties,
235 bool enableDistanceFieldFonts) {
bsalomon6bc1b5f2015-02-23 09:06:38 -0800236 if (fGpu->caps()->pathRenderingSupport() && renderTarget->isMultisampled()) {
237 GrStencilBuffer* sb = renderTarget->renderTargetPriv().attachStencilBuffer();
238 if (sb) {
joshualitt6e8cd962015-03-20 10:30:14 -0700239 return GrStencilAndCoverTextContext::Create(this, gpuDevice, leakyProperties);
bsalomon6bc1b5f2015-02-23 09:06:38 -0800240 }
jvanverth8c27a182014-10-14 08:45:50 -0700241 }
242
joshualitt7c3a2f82015-03-31 13:32:05 -0700243#ifdef USE_BITMAP_TEXTBLOBS
joshualittdbd35932015-04-02 09:19:04 -0700244 return GrAtlasTextContext::Create(this, gpuDevice, leakyProperties);
joshualitt7c3a2f82015-03-31 13:32:05 -0700245#else
joshualitt6e8cd962015-03-20 10:30:14 -0700246 return GrDistanceFieldTextContext::Create(this, gpuDevice, leakyProperties,
247 enableDistanceFieldFonts);
joshualitt7c3a2f82015-03-31 13:32:05 -0700248#endif
kkinnunenc6cb56f2014-06-24 00:12:27 -0700249}
250
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000251////////////////////////////////////////////////////////////////////////////////
bsalomond0423582015-02-06 08:49:24 -0800252enum ScratchTextureFlags {
253 kExact_ScratchTextureFlag = 0x1,
254 kNoPendingIO_ScratchTextureFlag = 0x2,
255 kNoCreate_ScratchTextureFlag = 0x4,
256};
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000257
bsalomond2a6f4e2015-02-04 10:55:54 -0800258bool GrContext::isConfigTexturable(GrPixelConfig config) const {
259 return fGpu->caps()->isConfigTexturable(config);
260}
261
262bool GrContext::npotTextureTileSupport() const {
263 return fGpu->caps()->npotTextureTileSupport();
264}
265
bsalomond0423582015-02-06 08:49:24 -0800266GrTexture* GrContext::createTexture(const GrSurfaceDesc& desc, bool budgeted, const void* srcData,
bsalomon37f9a262015-02-02 13:00:10 -0800267 size_t rowBytes) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800268 RETURN_NULL_IF_ABANDONED
bsalomond0423582015-02-06 08:49:24 -0800269 if ((desc.fFlags & kRenderTarget_GrSurfaceFlag) &&
270 !this->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
bsalomond2a6f4e2015-02-04 10:55:54 -0800271 return NULL;
272 }
bsalomond0423582015-02-06 08:49:24 -0800273 if (!GrPixelConfigIsCompressed(desc.fConfig)) {
274 static const uint32_t kFlags = kExact_ScratchTextureFlag |
275 kNoCreate_ScratchTextureFlag;
276 if (GrTexture* texture = this->internalRefScratchTexture(desc, kFlags)) {
277 if (!srcData || texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
278 srcData, rowBytes)) {
279 if (!budgeted) {
bsalomon3582d3e2015-02-13 14:20:05 -0800280 texture->resourcePriv().makeUnbudgeted();
bsalomond0423582015-02-06 08:49:24 -0800281 }
282 return texture;
283 }
284 texture->unref();
285 }
286 }
287 return fGpu->createTexture(desc, budgeted, srcData, rowBytes);
288}
bsalomond2a6f4e2015-02-04 10:55:54 -0800289
bsalomond0423582015-02-06 08:49:24 -0800290GrTexture* GrContext::refScratchTexture(const GrSurfaceDesc& desc, ScratchTexMatch match,
291 bool calledDuringFlush) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800292 RETURN_NULL_IF_ABANDONED
bsalomond0423582015-02-06 08:49:24 -0800293 // Currently we don't recycle compressed textures as scratch.
294 if (GrPixelConfigIsCompressed(desc.fConfig)) {
295 return NULL;
296 } else {
297 uint32_t flags = 0;
298 if (kExact_ScratchTexMatch == match) {
299 flags |= kExact_ScratchTextureFlag;
300 }
301 if (calledDuringFlush) {
302 flags |= kNoPendingIO_ScratchTextureFlag;
303 }
304 return this->internalRefScratchTexture(desc, flags);
305 }
306}
307
308GrTexture* GrContext::internalRefScratchTexture(const GrSurfaceDesc& inDesc, uint32_t flags) {
309 SkASSERT(!GrPixelConfigIsCompressed(inDesc.fConfig));
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000310
bsalomonf2703d82014-10-28 14:33:06 -0700311 SkTCopyOnFirstWrite<GrSurfaceDesc> desc(inDesc);
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000312
bsalomonf2703d82014-10-28 14:33:06 -0700313 if (fGpu->caps()->reuseScratchTextures() || (desc->fFlags & kRenderTarget_GrSurfaceFlag)) {
bsalomond0423582015-02-06 08:49:24 -0800314 if (!(kExact_ScratchTextureFlag & flags)) {
bsalomonbcf0a522014-10-08 08:40:09 -0700315 // bin by pow2 with a reasonable min
316 static const int MIN_SIZE = 16;
bsalomonf2703d82014-10-28 14:33:06 -0700317 GrSurfaceDesc* wdesc = desc.writable();
bsalomonbcf0a522014-10-08 08:40:09 -0700318 wdesc->fWidth = SkTMax(MIN_SIZE, GrNextPow2(desc->fWidth));
319 wdesc->fHeight = SkTMax(MIN_SIZE, GrNextPow2(desc->fHeight));
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000320 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000321
bsalomon6bc1b5f2015-02-23 09:06:38 -0800322 GrScratchKey key;
323 GrTexturePriv::ComputeScratchKey(*desc, &key);
324 uint32_t scratchFlags = 0;
325 if (kNoPendingIO_ScratchTextureFlag & flags) {
326 scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag;
327 } else if (!(desc->fFlags & kRenderTarget_GrSurfaceFlag)) {
328 // If it is not a render target then it will most likely be populated by
329 // writePixels() which will trigger a flush if the texture has pending IO.
330 scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag;
331 }
332 GrGpuResource* resource = fResourceCache->findAndRefScratchResource(key, scratchFlags);
333 if (resource) {
334 GrSurface* surface = static_cast<GrSurface*>(resource);
335 GrRenderTarget* rt = surface->asRenderTarget();
336 if (rt && fGpu->caps()->discardRenderTargetSupport()) {
337 rt->discard();
bsalomon000f8292014-10-15 19:04:14 -0700338 }
bsalomon6bc1b5f2015-02-23 09:06:38 -0800339 return surface->asTexture();
340 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000341 }
342
bsalomond0423582015-02-06 08:49:24 -0800343 if (!(kNoCreate_ScratchTextureFlag & flags)) {
bsalomon3582d3e2015-02-13 14:20:05 -0800344 return fGpu->createTexture(*desc, true, NULL, 0);
bsalomone167f962015-01-27 09:56:04 -0800345 }
bsalomond0423582015-02-06 08:49:24 -0800346
347 return NULL;
Brian Salomon9323b8b2014-10-07 15:07:38 -0400348}
349
bsalomon71cb0c22014-11-14 12:10:14 -0800350void GrContext::OverBudgetCB(void* data) {
bsalomon66a450f2014-11-13 13:19:10 -0800351 SkASSERT(data);
bsalomonf21dab92014-11-13 13:33:28 -0800352
bsalomon66a450f2014-11-13 13:19:10 -0800353 GrContext* context = reinterpret_cast<GrContext*>(data);
bsalomonf21dab92014-11-13 13:33:28 -0800354
355 // Flush the InOrderDrawBuffer to possibly free up some textures
356 context->fFlushToReduceCacheSize = true;
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000357}
358
joshualitt0db6dfa2015-04-10 07:01:30 -0700359void GrContext::TextBlobCacheOverBudgetCB(void* data) {
360 SkASSERT(data);
361
362 // Unlike the GrResourceCache, TextBlobs are drawn at the SkGpuDevice level, therefore they
363 // cannot use fFlushTorReduceCacheSize because it uses AutoCheckFlush. The solution is to move
364 // drawText calls to below the GrContext level, but this is not trivial because they call
365 // drawPath on SkGpuDevice
366 GrContext* context = reinterpret_cast<GrContext*>(data);
367 context->flush();
368}
369
bsalomon@google.com91958362011-06-13 17:58:13 +0000370int GrContext::getMaxTextureSize() const {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000371 return SkTMin(fGpu->caps()->maxTextureSize(), fMaxTextureSizeOverride);
bsalomon@google.com91958362011-06-13 17:58:13 +0000372}
373
374int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000375 return fGpu->caps()->maxRenderTargetSize();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000376}
377
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000378int GrContext::getMaxSampleCount() const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000379 return fGpu->caps()->maxSampleCount();
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000380}
381
bsalomon@google.com27847de2011-02-22 20:59:41 +0000382///////////////////////////////////////////////////////////////////////////////
383
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000384GrTexture* GrContext::wrapBackendTexture(const GrBackendTextureDesc& desc) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800385 RETURN_NULL_IF_ABANDONED
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000386 return fGpu->wrapBackendTexture(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000387}
388
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000389GrRenderTarget* GrContext::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800390 RETURN_NULL_IF_ABANDONED
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000391 return fGpu->wrapBackendRenderTarget(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000392}
393
bsalomon@google.com27847de2011-02-22 20:59:41 +0000394////////////////////////////////////////////////////////////////////////////////
395
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000396void GrContext::clear(const SkIRect* rect,
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000397 const GrColor color,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000398 bool canIgnoreRect,
bsalomon41ebbdd2014-08-04 08:31:39 -0700399 GrRenderTarget* renderTarget) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800400 RETURN_IF_ABANDONED
bsalomon41ebbdd2014-08-04 08:31:39 -0700401 ASSERT_OWNED_RESOURCE(renderTarget);
bsalomon89c62982014-11-03 12:08:42 -0800402 SkASSERT(renderTarget);
403
bsalomonf21dab92014-11-13 13:33:28 -0800404 AutoCheckFlush acf(this);
egdanield78a1682014-07-09 10:41:26 -0700405 GR_CREATE_TRACE_MARKER_CONTEXT("GrContext::clear", this);
joshualitt570d2f82015-02-25 13:19:48 -0800406 GrDrawTarget* target = this->prepareToDraw();
bsalomon41ebbdd2014-08-04 08:31:39 -0700407 if (NULL == target) {
408 return;
409 }
410 target->clear(rect, color, canIgnoreRect, renderTarget);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000411}
412
joshualitt25d9c152015-02-18 12:29:52 -0800413void GrContext::drawPaint(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800414 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800415 const GrPaint& origPaint,
416 const SkMatrix& viewMatrix) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800417 RETURN_IF_ABANDONED
bsalomon@google.com27847de2011-02-22 20:59:41 +0000418 // set rect to be big enough to fill the space, but not super-huge, so we
419 // don't overflow fixed-point implementations
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000420 SkRect r;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000421 r.setLTRB(0, 0,
joshualitt25d9c152015-02-18 12:29:52 -0800422 SkIntToScalar(rt->width()),
423 SkIntToScalar(rt->height()));
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000424 SkTCopyOnFirstWrite<GrPaint> paint(origPaint);
joshualitt8fc6c2d2014-12-22 15:27:05 -0800425
426 // by definition this fills the entire clip, no need for AA
427 if (paint->isAntiAlias()) {
428 paint.writable()->setAntiAlias(false);
429 }
430
431 bool isPerspective = viewMatrix.hasPerspective();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000432
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000433 // We attempt to map r by the inverse matrix and draw that. mapRect will
434 // map the four corners and bound them with a new rect. This will not
435 // produce a correct result for some perspective matrices.
joshualitt8fc6c2d2014-12-22 15:27:05 -0800436 if (!isPerspective) {
joshualitt5531d512014-12-17 15:50:11 -0800437 SkMatrix inverse;
438 if (!viewMatrix.invert(&inverse)) {
tfarina38406c82014-10-31 07:11:12 -0700439 SkDebugf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000440 return;
441 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000442 inverse.mapRect(&r);
joshualitt570d2f82015-02-25 13:19:48 -0800443 this->drawRect(rt, clip, *paint, viewMatrix, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000444 } else {
joshualitt8fc6c2d2014-12-22 15:27:05 -0800445 SkMatrix localMatrix;
446 if (!viewMatrix.invert(&localMatrix)) {
tfarina38406c82014-10-31 07:11:12 -0700447 SkDebugf("Could not invert matrix\n");
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000448 return;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000449 }
joshualitt8fc6c2d2014-12-22 15:27:05 -0800450
451 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -0800452 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -0800453 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, paint, &acf);
joshualitt8fc6c2d2014-12-22 15:27:05 -0800454 if (NULL == target) {
455 return;
456 }
457
458 GR_CREATE_TRACE_MARKER("GrContext::drawPaintWithPerspective", target);
joshualitt44701df2015-02-23 14:44:57 -0800459 target->drawRect(&pipelineBuilder,
460 paint->getColor(),
461 SkMatrix::I(),
462 r,
463 NULL,
464 &localMatrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000465 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000466}
467
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000468#ifdef SK_DEVELOPER
469void GrContext::dumpFontCache() const {
470 fFontCache->dump();
471}
472#endif
473
bsalomon@google.com205d4602011-04-25 12:43:45 +0000474////////////////////////////////////////////////////////////////////////////////
475
bsalomonc30aaa02014-08-13 07:15:29 -0700476static inline bool is_irect(const SkRect& r) {
tfarina38406c82014-10-31 07:11:12 -0700477 return SkScalarIsInt(r.fLeft) && SkScalarIsInt(r.fTop) &&
478 SkScalarIsInt(r.fRight) && SkScalarIsInt(r.fBottom);
bsalomonc30aaa02014-08-13 07:15:29 -0700479}
480
bsalomon@google.com205d4602011-04-25 12:43:45 +0000481static bool apply_aa_to_rect(GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -0800482 GrPipelineBuilder* pipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -0800483 SkRect* devBoundRect,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000484 const SkRect& rect,
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000485 SkScalar strokeWidth,
joshualitt2e3b3e32014-12-09 13:31:14 -0800486 const SkMatrix& combinedMatrix,
487 GrColor color) {
egdaniel8dd688b2015-01-22 10:16:09 -0800488 if (pipelineBuilder->getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000489 return false;
490 }
491
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000492#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000493 if (strokeWidth >= 0) {
494#endif
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000495 if (!combinedMatrix.preservesAxisAlignment()) {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000496 return false;
497 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000498
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000499#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000500 } else {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000501 if (!combinedMatrix.preservesRightAngles()) {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000502 return false;
503 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000504 }
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000505#endif
bsalomon@google.com205d4602011-04-25 12:43:45 +0000506
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000507 combinedMatrix.mapRect(devBoundRect, rect);
derekfe638d1c2014-12-02 13:51:29 -0800508 if (!combinedMatrix.rectStaysRect()) {
509 return true;
510 }
511
bsalomonc30aaa02014-08-13 07:15:29 -0700512 if (strokeWidth < 0) {
513 return !is_irect(*devBoundRect);
514 }
robertphillips@google.com28ac96e2013-05-13 13:38:35 +0000515
bsalomon9c0822a2014-08-11 11:07:48 -0700516 return true;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000517}
518
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000519static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) {
520 return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
521 point.fY >= rect.fTop && point.fY <= rect.fBottom;
522}
523
joshualitt0823bfa2015-02-27 10:06:15 -0800524class StrokeRectBatch : public GrBatch {
525public:
526 struct Geometry {
527 GrColor fColor;
528 SkMatrix fViewMatrix;
529 SkRect fRect;
530 SkScalar fStrokeWidth;
531 };
532
533 static GrBatch* Create(const Geometry& geometry) {
534 return SkNEW_ARGS(StrokeRectBatch, (geometry));
535 }
536
mtklein36352bf2015-03-25 18:17:31 -0700537 const char* name() const override { return "StrokeRectBatch"; }
joshualitt0823bfa2015-02-27 10:06:15 -0800538
mtklein36352bf2015-03-25 18:17:31 -0700539 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt0823bfa2015-02-27 10:06:15 -0800540 // When this is called on a batch, there is only one geometry bundle
541 out->setKnownFourComponents(fGeoData[0].fColor);
542 }
543
mtklein36352bf2015-03-25 18:17:31 -0700544 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualitt0823bfa2015-02-27 10:06:15 -0800545 out->setKnownSingleComponent(0xff);
546 }
547
mtklein36352bf2015-03-25 18:17:31 -0700548 void initBatchTracker(const GrPipelineInfo& init) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800549 // Handle any color overrides
550 if (init.fColorIgnored) {
551 fGeoData[0].fColor = GrColor_ILLEGAL;
552 } else if (GrColor_ILLEGAL != init.fOverrideColor) {
553 fGeoData[0].fColor = init.fOverrideColor;
554 }
555
556 // setup batch properties
557 fBatch.fColorIgnored = init.fColorIgnored;
558 fBatch.fColor = fGeoData[0].fColor;
559 fBatch.fUsesLocalCoords = init.fUsesLocalCoords;
560 fBatch.fCoverageIgnored = init.fCoverageIgnored;
561 }
562
mtklein36352bf2015-03-25 18:17:31 -0700563 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800564 SkAutoTUnref<const GrGeometryProcessor> gp(
565 GrDefaultGeoProcFactory::Create(GrDefaultGeoProcFactory::kPosition_GPType,
566 this->color(),
567 this->viewMatrix(),
568 SkMatrix::I()));
569
570 batchTarget->initDraw(gp, pipeline);
571
572 // TODO this is hacky, but the only way we have to initialize the GP is to use the
573 // GrPipelineInfo struct so we can generate the correct shader. Once we have GrBatch
574 // everywhere we can remove this nastiness
575 GrPipelineInfo init;
576 init.fColorIgnored = fBatch.fColorIgnored;
577 init.fOverrideColor = GrColor_ILLEGAL;
578 init.fCoverageIgnored = fBatch.fCoverageIgnored;
579 init.fUsesLocalCoords = this->usesLocalCoords();
580 gp->initBatchTracker(batchTarget->currentBatchTracker(), init);
581
582 size_t vertexStride = gp->getVertexStride();
583
584 SkASSERT(vertexStride == sizeof(GrDefaultGeoProcFactory::PositionAttr));
585
586 Geometry& args = fGeoData[0];
587
588 int vertexCount = kVertsPerHairlineRect;
589 if (args.fStrokeWidth > 0) {
590 vertexCount = kVertsPerStrokeRect;
591 }
592
593 const GrVertexBuffer* vertexBuffer;
594 int firstVertex;
595
596 void* vertices = batchTarget->vertexPool()->makeSpace(vertexStride,
597 vertexCount,
598 &vertexBuffer,
599 &firstVertex);
600
joshualitt4b31de82015-03-05 14:33:41 -0800601 if (!vertices) {
602 SkDebugf("Could not allocate vertices\n");
603 return;
604 }
605
joshualitt0823bfa2015-02-27 10:06:15 -0800606 SkPoint* vertex = reinterpret_cast<SkPoint*>(vertices);
607
608 GrPrimitiveType primType;
609
610 if (args.fStrokeWidth > 0) {;
611 primType = kTriangleStrip_GrPrimitiveType;
612 args.fRect.sort();
613 this->setStrokeRectStrip(vertex, args.fRect, args.fStrokeWidth);
614 } else {
615 // hairline
616 primType = kLineStrip_GrPrimitiveType;
617 vertex[0].set(args.fRect.fLeft, args.fRect.fTop);
618 vertex[1].set(args.fRect.fRight, args.fRect.fTop);
619 vertex[2].set(args.fRect.fRight, args.fRect.fBottom);
620 vertex[3].set(args.fRect.fLeft, args.fRect.fBottom);
621 vertex[4].set(args.fRect.fLeft, args.fRect.fTop);
622 }
623
624 GrDrawTarget::DrawInfo drawInfo;
625 drawInfo.setPrimitiveType(primType);
626 drawInfo.setVertexBuffer(vertexBuffer);
627 drawInfo.setStartVertex(firstVertex);
628 drawInfo.setVertexCount(vertexCount);
629 drawInfo.setStartIndex(0);
630 drawInfo.setIndexCount(0);
631 drawInfo.setInstanceCount(0);
632 drawInfo.setVerticesPerInstance(0);
633 drawInfo.setIndicesPerInstance(0);
634 batchTarget->draw(drawInfo);
635 }
636
637 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
638
639private:
640 StrokeRectBatch(const Geometry& geometry) {
641 this->initClassID<StrokeRectBatch>();
642
643 fBatch.fHairline = geometry.fStrokeWidth == 0;
644
645 fGeoData.push_back(geometry);
646 }
647
648 /* create a triangle strip that strokes the specified rect. There are 8
649 unique vertices, but we repeat the last 2 to close up. Alternatively we
650 could use an indices array, and then only send 8 verts, but not sure that
651 would be faster.
652 */
653 void setStrokeRectStrip(SkPoint verts[10], const SkRect& rect, SkScalar width) {
654 const SkScalar rad = SkScalarHalf(width);
655 // TODO we should be able to enable this assert, but we'd have to filter these draws
656 // this is a bug
657 //SkASSERT(rad < rect.width() / 2 && rad < rect.height() / 2);
658
659 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
660 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
661 verts[2].set(rect.fRight - rad, rect.fTop + rad);
662 verts[3].set(rect.fRight + rad, rect.fTop - rad);
663 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
664 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
665 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
666 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
667 verts[8] = verts[0];
668 verts[9] = verts[1];
669 }
670
671
672 GrColor color() const { return fBatch.fColor; }
673 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
674 bool colorIgnored() const { return fBatch.fColorIgnored; }
675 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
676 bool hairline() const { return fBatch.fHairline; }
677
mtklein36352bf2015-03-25 18:17:31 -0700678 bool onCombineIfPossible(GrBatch* t) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800679 // StrokeRectBatch* that = t->cast<StrokeRectBatch>();
680
681 // NonAA stroke rects cannot batch right now
682 // TODO make these batchable
683 return false;
684 }
685
686 struct BatchTracker {
687 GrColor fColor;
688 bool fUsesLocalCoords;
689 bool fColorIgnored;
690 bool fCoverageIgnored;
691 bool fHairline;
692 };
693
694 const static int kVertsPerHairlineRect = 5;
695 const static int kVertsPerStrokeRect = 10;
696
697 BatchTracker fBatch;
698 SkSTArray<1, Geometry, true> fGeoData;
699};
700
joshualitt25d9c152015-02-18 12:29:52 -0800701void GrContext::drawRect(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800702 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800703 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -0800704 const SkMatrix& viewMatrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000705 const SkRect& rect,
bsalomon01c8da12014-08-04 09:21:30 -0700706 const GrStrokeInfo* strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800707 RETURN_IF_ABANDONED
bsalomon49f085d2014-09-05 13:34:00 -0700708 if (strokeInfo && strokeInfo->isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700709 SkPath path;
710 path.addRect(rect);
joshualitt570d2f82015-02-25 13:19:48 -0800711 this->drawPath(rt, clip, paint, viewMatrix, path, *strokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -0700712 return;
713 }
714
bsalomonf21dab92014-11-13 13:33:28 -0800715 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -0800716 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -0800717 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700718 if (NULL == target) {
719 return;
720 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000721
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000722 GR_CREATE_TRACE_MARKER("GrContext::drawRect", target);
egdanield58a0ba2014-06-11 10:30:05 -0700723 SkScalar width = NULL == strokeInfo ? -1 : strokeInfo->getStrokeRec().getWidth();
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000724
725 // Check if this is a full RT draw and can be replaced with a clear. We don't bother checking
726 // cases where the RT is fully inside a stroke.
727 if (width < 0) {
728 SkRect rtRect;
egdaniel8dd688b2015-01-22 10:16:09 -0800729 pipelineBuilder.getRenderTarget()->getBoundsRect(&rtRect);
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000730 SkRect clipSpaceRTRect = rtRect;
joshualitt570d2f82015-02-25 13:19:48 -0800731 bool checkClip = GrClip::kWideOpen_ClipType != clip.clipType();
joshualitt44701df2015-02-23 14:44:57 -0800732 if (checkClip) {
joshualitt570d2f82015-02-25 13:19:48 -0800733 clipSpaceRTRect.offset(SkIntToScalar(clip.origin().fX),
734 SkIntToScalar(clip.origin().fY));
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000735 }
736 // Does the clip contain the entire RT?
joshualitt570d2f82015-02-25 13:19:48 -0800737 if (!checkClip || clip.quickContains(clipSpaceRTRect)) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000738 SkMatrix invM;
joshualitt8059eb92014-12-29 15:10:07 -0800739 if (!viewMatrix.invert(&invM)) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000740 return;
741 }
742 // Does the rect bound the RT?
743 SkPoint srcSpaceRTQuad[4];
744 invM.mapRectToQuad(srcSpaceRTQuad, rtRect);
745 if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) &&
746 rect_contains_inclusive(rect, srcSpaceRTQuad[1]) &&
747 rect_contains_inclusive(rect, srcSpaceRTQuad[2]) &&
748 rect_contains_inclusive(rect, srcSpaceRTQuad[3])) {
749 // Will it blend?
750 GrColor clearColor;
751 if (paint.isOpaqueAndConstantColor(&clearColor)) {
joshualitt25d9c152015-02-18 12:29:52 -0800752 target->clear(NULL, clearColor, true, rt);
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000753 return;
754 }
755 }
756 }
757 }
758
joshualitt2e3b3e32014-12-09 13:31:14 -0800759 GrColor color = paint.getColor();
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000760 SkRect devBoundRect;
egdaniel8dd688b2015-01-22 10:16:09 -0800761 bool needAA = paint.isAntiAlias() && !pipelineBuilder.getRenderTarget()->isMultisampled();
762 bool doAA = needAA && apply_aa_to_rect(target, &pipelineBuilder, &devBoundRect, rect, width,
joshualitt8059eb92014-12-29 15:10:07 -0800763 viewMatrix, color);
egdanield58a0ba2014-06-11 10:30:05 -0700764
bsalomon@google.com205d4602011-04-25 12:43:45 +0000765 if (doAA) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000766 if (width >= 0) {
bsalomon395ef052014-11-12 11:35:22 -0800767 const SkStrokeRec& strokeRec = strokeInfo->getStrokeRec();
joshualitt9853cce2014-11-17 14:22:48 -0800768 fAARectRenderer->strokeAARect(target,
egdaniel8dd688b2015-01-22 10:16:09 -0800769 &pipelineBuilder,
joshualitt2e3b3e32014-12-09 13:31:14 -0800770 color,
joshualitt8059eb92014-12-29 15:10:07 -0800771 viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800772 rect,
joshualitt9853cce2014-11-17 14:22:48 -0800773 devBoundRect,
774 strokeRec);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000775 } else {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000776 // filled AA rect
joshualitt8059eb92014-12-29 15:10:07 -0800777 fAARectRenderer->fillAARect(target,
egdaniel8dd688b2015-01-22 10:16:09 -0800778 &pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800779 color,
780 viewMatrix,
781 rect,
joshualittd27f73e2014-12-29 07:43:36 -0800782 devBoundRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000783 }
784 return;
785 }
786
bsalomon@google.com27847de2011-02-22 20:59:41 +0000787 if (width >= 0) {
joshualitt0823bfa2015-02-27 10:06:15 -0800788 StrokeRectBatch::Geometry geometry;
789 geometry.fViewMatrix = viewMatrix;
790 geometry.fColor = color;
791 geometry.fRect = rect;
792 geometry.fStrokeWidth = width;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000793
joshualitt0823bfa2015-02-27 10:06:15 -0800794 SkAutoTUnref<GrBatch> batch(StrokeRectBatch::Create(geometry));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000795
joshualitt0823bfa2015-02-27 10:06:15 -0800796 SkRect bounds = rect;
797 SkScalar rad = SkScalarHalf(width);
798 bounds.outset(rad, rad);
799 viewMatrix.mapRect(&bounds);
800 target->drawBatch(&pipelineBuilder, batch, &bounds);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000801 } else {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000802 // filled BW rect
egdaniel8dd688b2015-01-22 10:16:09 -0800803 target->drawSimpleRect(&pipelineBuilder, color, viewMatrix, rect);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000804 }
805}
806
joshualitt25d9c152015-02-18 12:29:52 -0800807void GrContext::drawNonAARectToRect(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800808 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800809 const GrPaint& paint,
joshualitt16b27892014-12-18 07:47:16 -0800810 const SkMatrix& viewMatrix,
811 const SkRect& rectToDraw,
812 const SkRect& localRect,
813 const SkMatrix* localMatrix) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800814 RETURN_IF_ABANDONED
bsalomonf21dab92014-11-13 13:33:28 -0800815 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -0800816 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -0800817 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700818 if (NULL == target) {
819 return;
820 }
bsalomon@google.com64386952013-02-08 21:22:44 +0000821
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000822 GR_CREATE_TRACE_MARKER("GrContext::drawRectToRect", target);
823
joshualitt44701df2015-02-23 14:44:57 -0800824 target->drawRect(&pipelineBuilder,
825 paint.getColor(),
826 viewMatrix,
827 rectToDraw,
828 &localRect,
egdaniel8dd688b2015-01-22 10:16:09 -0800829 localMatrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000830}
831
joshualitt0823bfa2015-02-27 10:06:15 -0800832static const GrGeometryProcessor* set_vertex_attributes(bool hasLocalCoords,
833 bool hasColors,
joshualitt56995b52014-12-11 15:44:02 -0800834 int* colorOffset,
835 int* texOffset,
joshualitt8059eb92014-12-29 15:10:07 -0800836 GrColor color,
837 const SkMatrix& viewMatrix) {
robertphillips@google.com42903302013-04-20 12:26:07 +0000838 *texOffset = -1;
839 *colorOffset = -1;
joshualitt5478d422014-11-14 16:00:38 -0800840 uint32_t flags = GrDefaultGeoProcFactory::kPosition_GPType;
joshualitt0823bfa2015-02-27 10:06:15 -0800841 if (hasLocalCoords && hasColors) {
joshualitt5478d422014-11-14 16:00:38 -0800842 *colorOffset = sizeof(SkPoint);
843 *texOffset = sizeof(SkPoint) + sizeof(GrColor);
844 flags |= GrDefaultGeoProcFactory::kColor_GPType |
845 GrDefaultGeoProcFactory::kLocalCoord_GPType;
joshualitt0823bfa2015-02-27 10:06:15 -0800846 } else if (hasLocalCoords) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000847 *texOffset = sizeof(SkPoint);
joshualitt5478d422014-11-14 16:00:38 -0800848 flags |= GrDefaultGeoProcFactory::kLocalCoord_GPType;
joshualitt0823bfa2015-02-27 10:06:15 -0800849 } else if (hasColors) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000850 *colorOffset = sizeof(SkPoint);
joshualitt5478d422014-11-14 16:00:38 -0800851 flags |= GrDefaultGeoProcFactory::kColor_GPType;
robertphillips@google.com42903302013-04-20 12:26:07 +0000852 }
joshualitt8059eb92014-12-29 15:10:07 -0800853 return GrDefaultGeoProcFactory::Create(flags, color, viewMatrix, SkMatrix::I());
robertphillips@google.com42903302013-04-20 12:26:07 +0000854}
855
joshualitt0823bfa2015-02-27 10:06:15 -0800856class DrawVerticesBatch : public GrBatch {
857public:
858 struct Geometry {
859 GrColor fColor;
860 SkTDArray<SkPoint> fPositions;
861 SkTDArray<uint16_t> fIndices;
862 SkTDArray<GrColor> fColors;
863 SkTDArray<SkPoint> fLocalCoords;
864 };
865
866 static GrBatch* Create(const Geometry& geometry, GrPrimitiveType primitiveType,
867 const SkMatrix& viewMatrix,
868 const SkPoint* positions, int vertexCount,
869 const uint16_t* indices, int indexCount,
870 const GrColor* colors, const SkPoint* localCoords) {
871 return SkNEW_ARGS(DrawVerticesBatch, (geometry, primitiveType, viewMatrix, positions,
872 vertexCount, indices, indexCount, colors,
873 localCoords));
874 }
875
mtklein36352bf2015-03-25 18:17:31 -0700876 const char* name() const override { return "DrawVerticesBatch"; }
joshualitt0823bfa2015-02-27 10:06:15 -0800877
mtklein36352bf2015-03-25 18:17:31 -0700878 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt0823bfa2015-02-27 10:06:15 -0800879 // When this is called on a batch, there is only one geometry bundle
880 if (this->hasColors()) {
881 out->setUnknownFourComponents();
882 } else {
883 out->setKnownFourComponents(fGeoData[0].fColor);
884 }
885 }
886
mtklein36352bf2015-03-25 18:17:31 -0700887 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualittd4c7d552015-02-27 13:00:56 -0800888 out->setKnownSingleComponent(0xff);
joshualitt0823bfa2015-02-27 10:06:15 -0800889 }
890
mtklein36352bf2015-03-25 18:17:31 -0700891 void initBatchTracker(const GrPipelineInfo& init) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800892 // Handle any color overrides
893 if (init.fColorIgnored) {
894 fGeoData[0].fColor = GrColor_ILLEGAL;
895 } else if (GrColor_ILLEGAL != init.fOverrideColor) {
896 fGeoData[0].fColor = init.fOverrideColor;
897 }
898
899 // setup batch properties
900 fBatch.fColorIgnored = init.fColorIgnored;
901 fBatch.fColor = fGeoData[0].fColor;
902 fBatch.fUsesLocalCoords = init.fUsesLocalCoords;
903 fBatch.fCoverageIgnored = init.fCoverageIgnored;
904 }
905
mtklein36352bf2015-03-25 18:17:31 -0700906 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800907 int colorOffset = -1, texOffset = -1;
908 SkAutoTUnref<const GrGeometryProcessor> gp(
909 set_vertex_attributes(this->hasLocalCoords(), this->hasColors(), &colorOffset,
910 &texOffset, this->color(), this->viewMatrix()));
911
912 batchTarget->initDraw(gp, pipeline);
913
914 // TODO this is hacky, but the only way we have to initialize the GP is to use the
915 // GrPipelineInfo struct so we can generate the correct shader. Once we have GrBatch
916 // everywhere we can remove this nastiness
917 GrPipelineInfo init;
918 init.fColorIgnored = fBatch.fColorIgnored;
919 init.fOverrideColor = GrColor_ILLEGAL;
920 init.fCoverageIgnored = fBatch.fCoverageIgnored;
921 init.fUsesLocalCoords = this->usesLocalCoords();
922 gp->initBatchTracker(batchTarget->currentBatchTracker(), init);
923
924 size_t vertexStride = gp->getVertexStride();
925
926 SkASSERT(vertexStride == sizeof(SkPoint) + (this->hasLocalCoords() ? sizeof(SkPoint) : 0)
927 + (this->hasColors() ? sizeof(GrColor) : 0));
928
929 int instanceCount = fGeoData.count();
930
931 const GrVertexBuffer* vertexBuffer;
932 int firstVertex;
933
934 void* vertices = batchTarget->vertexPool()->makeSpace(vertexStride,
935 this->vertexCount(),
936 &vertexBuffer,
937 &firstVertex);
938
joshualitt4b31de82015-03-05 14:33:41 -0800939 if (!vertices) {
940 SkDebugf("Could not allocate vertices\n");
941 return;
942 }
943
joshualitt0823bfa2015-02-27 10:06:15 -0800944 const GrIndexBuffer* indexBuffer;
945 int firstIndex;
946
947 void* indices = NULL;
948 if (this->hasIndices()) {
949 indices = batchTarget->indexPool()->makeSpace(this->indexCount(),
950 &indexBuffer,
951 &firstIndex);
joshualitt4b31de82015-03-05 14:33:41 -0800952
953 if (!indices) {
954 SkDebugf("Could not allocate indices\n");
955 return;
956 }
joshualitt0823bfa2015-02-27 10:06:15 -0800957 }
958
959 int indexOffset = 0;
960 int vertexOffset = 0;
961 for (int i = 0; i < instanceCount; i++) {
962 const Geometry& args = fGeoData[i];
963
964 // TODO we can actually cache this interleaved and then just memcopy
965 if (this->hasIndices()) {
966 for (int j = 0; j < args.fIndices.count(); ++j, ++indexOffset) {
967 *((uint16_t*)indices + indexOffset) = args.fIndices[j] + vertexOffset;
968 }
969 }
970
971 for (int j = 0; j < args.fPositions.count(); ++j) {
972 *((SkPoint*)vertices) = args.fPositions[j];
973 if (this->hasColors()) {
974 *(GrColor*)((intptr_t)vertices + colorOffset) = args.fColors[j];
975 }
976 if (this->hasLocalCoords()) {
977 *(SkPoint*)((intptr_t)vertices + texOffset) = args.fLocalCoords[j];
978 }
979 vertices = (void*)((intptr_t)vertices + vertexStride);
980 vertexOffset++;
981 }
982 }
983
984 GrDrawTarget::DrawInfo drawInfo;
985 drawInfo.setPrimitiveType(this->primitiveType());
986 drawInfo.setVertexBuffer(vertexBuffer);
987 drawInfo.setStartVertex(firstVertex);
988 drawInfo.setVertexCount(this->vertexCount());
989 if (this->hasIndices()) {
990 drawInfo.setIndexBuffer(indexBuffer);
991 drawInfo.setStartIndex(firstIndex);
992 drawInfo.setIndexCount(this->indexCount());
993 } else {
994 drawInfo.setStartIndex(0);
995 drawInfo.setIndexCount(0);
996 }
997 batchTarget->draw(drawInfo);
998 }
999
1000 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
1001
1002private:
1003 DrawVerticesBatch(const Geometry& geometry, GrPrimitiveType primitiveType,
1004 const SkMatrix& viewMatrix,
1005 const SkPoint* positions, int vertexCount,
1006 const uint16_t* indices, int indexCount,
1007 const GrColor* colors, const SkPoint* localCoords) {
1008 this->initClassID<DrawVerticesBatch>();
1009 SkASSERT(positions);
1010
1011 fBatch.fViewMatrix = viewMatrix;
1012 Geometry& installedGeo = fGeoData.push_back(geometry);
1013
1014 installedGeo.fPositions.append(vertexCount, positions);
1015 if (indices) {
1016 installedGeo.fIndices.append(indexCount, indices);
1017 fBatch.fHasIndices = true;
1018 } else {
1019 fBatch.fHasIndices = false;
1020 }
1021
1022 if (colors) {
1023 installedGeo.fColors.append(vertexCount, colors);
1024 fBatch.fHasColors = true;
1025 } else {
1026 fBatch.fHasColors = false;
1027 }
1028
1029 if (localCoords) {
1030 installedGeo.fLocalCoords.append(vertexCount, localCoords);
1031 fBatch.fHasLocalCoords = true;
1032 } else {
1033 fBatch.fHasLocalCoords = false;
1034 }
1035 fBatch.fVertexCount = vertexCount;
1036 fBatch.fIndexCount = indexCount;
1037 fBatch.fPrimitiveType = primitiveType;
1038 }
1039
1040 GrPrimitiveType primitiveType() const { return fBatch.fPrimitiveType; }
1041 bool batchablePrimitiveType() const {
1042 return kTriangles_GrPrimitiveType == fBatch.fPrimitiveType ||
1043 kLines_GrPrimitiveType == fBatch.fPrimitiveType ||
1044 kPoints_GrPrimitiveType == fBatch.fPrimitiveType;
1045 }
1046 GrColor color() const { return fBatch.fColor; }
1047 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
1048 bool colorIgnored() const { return fBatch.fColorIgnored; }
1049 const SkMatrix& viewMatrix() const { return fBatch.fViewMatrix; }
1050 bool hasColors() const { return fBatch.fHasColors; }
1051 bool hasIndices() const { return fBatch.fHasIndices; }
1052 bool hasLocalCoords() const { return fBatch.fHasLocalCoords; }
1053 int vertexCount() const { return fBatch.fVertexCount; }
1054 int indexCount() const { return fBatch.fIndexCount; }
1055
mtklein36352bf2015-03-25 18:17:31 -07001056 bool onCombineIfPossible(GrBatch* t) override {
joshualitt0823bfa2015-02-27 10:06:15 -08001057 DrawVerticesBatch* that = t->cast<DrawVerticesBatch>();
1058
1059 if (!this->batchablePrimitiveType() || this->primitiveType() != that->primitiveType()) {
1060 return false;
1061 }
1062
1063 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
1064
1065 // We currently use a uniform viewmatrix for this batch
1066 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
1067 return false;
1068 }
1069
1070 if (this->hasColors() != that->hasColors()) {
1071 return false;
1072 }
1073
1074 if (this->hasIndices() != that->hasIndices()) {
1075 return false;
1076 }
1077
1078 if (this->hasLocalCoords() != that->hasLocalCoords()) {
1079 return false;
1080 }
1081
1082 if (!this->hasColors() && this->color() != that->color()) {
1083 return false;
1084 }
1085
1086 if (this->color() != that->color()) {
1087 fBatch.fColor = GrColor_ILLEGAL;
1088 }
1089 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
1090 fBatch.fVertexCount += that->vertexCount();
1091 fBatch.fIndexCount += that->indexCount();
1092 return true;
1093 }
1094
1095 struct BatchTracker {
1096 GrPrimitiveType fPrimitiveType;
1097 SkMatrix fViewMatrix;
1098 GrColor fColor;
1099 bool fUsesLocalCoords;
1100 bool fColorIgnored;
1101 bool fCoverageIgnored;
1102 bool fHasColors;
1103 bool fHasIndices;
1104 bool fHasLocalCoords;
1105 int fVertexCount;
1106 int fIndexCount;
1107 };
1108
1109 BatchTracker fBatch;
1110 SkSTArray<1, Geometry, true> fGeoData;
1111};
1112
joshualitt25d9c152015-02-18 12:29:52 -08001113void GrContext::drawVertices(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -08001114 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001115 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001116 const SkMatrix& viewMatrix,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001117 GrPrimitiveType primitiveType,
1118 int vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001119 const SkPoint positions[],
1120 const SkPoint texCoords[],
bsalomon@google.com27847de2011-02-22 20:59:41 +00001121 const GrColor colors[],
1122 const uint16_t indices[],
1123 int indexCount) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001124 RETURN_IF_ABANDONED
bsalomonf21dab92014-11-13 13:33:28 -08001125 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001126 GrPipelineBuilder pipelineBuilder;
bsalomonf21dab92014-11-13 13:33:28 -08001127 GrDrawTarget::AutoReleaseGeometry geo; // must be inside AutoCheckFlush scope
commit-bot@chromium.org5a567932014-01-08 21:26:09 +00001128
joshualitt570d2f82015-02-25 13:19:48 -08001129 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001130 if (NULL == target) {
1131 return;
1132 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001133
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001134 GR_CREATE_TRACE_MARKER("GrContext::drawVertices", target);
1135
joshualitt0823bfa2015-02-27 10:06:15 -08001136 DrawVerticesBatch::Geometry geometry;
1137 geometry.fColor = paint.getColor();
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001138
joshualitt0823bfa2015-02-27 10:06:15 -08001139 SkAutoTUnref<GrBatch> batch(DrawVerticesBatch::Create(geometry, primitiveType, viewMatrix,
1140 positions, vertexCount, indices,
1141 indexCount,colors, texCoords));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001142
joshualitt0823bfa2015-02-27 10:06:15 -08001143 // TODO figure out bounds
1144 target->drawBatch(&pipelineBuilder, batch, NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001145}
1146
bsalomon@google.com06afe7b2011-04-26 15:31:40 +00001147///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com93c96602012-04-27 13:05:21 +00001148
joshualitt25d9c152015-02-18 12:29:52 -08001149void GrContext::drawRRect(GrRenderTarget*rt,
joshualitt570d2f82015-02-25 13:19:48 -08001150 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001151 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001152 const SkMatrix& viewMatrix,
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001153 const SkRRect& rrect,
egdanield58a0ba2014-06-11 10:30:05 -07001154 const GrStrokeInfo& strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001155 RETURN_IF_ABANDONED
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001156 if (rrect.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001157 return;
1158 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001159
egdanield58a0ba2014-06-11 10:30:05 -07001160 if (strokeInfo.isDashed()) {
1161 SkPath path;
1162 path.addRRect(rrect);
joshualitt570d2f82015-02-25 13:19:48 -08001163 this->drawPath(rt, clip, paint, viewMatrix, path, strokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -07001164 return;
1165 }
1166
bsalomonf21dab92014-11-13 13:33:28 -08001167 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001168 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -08001169 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001170 if (NULL == target) {
1171 return;
1172 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001173
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001174 GR_CREATE_TRACE_MARKER("GrContext::drawRRect", target);
1175
egdanield58a0ba2014-06-11 10:30:05 -07001176 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1177
joshualitt2e3b3e32014-12-09 13:31:14 -08001178 GrColor color = paint.getColor();
joshualitt44701df2015-02-23 14:44:57 -08001179 if (!fOvalRenderer->drawRRect(target,
1180 &pipelineBuilder,
1181 color,
1182 viewMatrix,
1183 paint.isAntiAlias(),
1184 rrect,
1185 strokeRec)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001186 SkPath path;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001187 path.addRRect(rrect);
egdaniel8dd688b2015-01-22 10:16:09 -08001188 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(),
1189 path, strokeInfo);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001190 }
1191}
1192
1193///////////////////////////////////////////////////////////////////////////////
1194
joshualitt25d9c152015-02-18 12:29:52 -08001195void GrContext::drawDRRect(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -08001196 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001197 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001198 const SkMatrix& viewMatrix,
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001199 const SkRRect& outer,
1200 const SkRRect& inner) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001201 RETURN_IF_ABANDONED
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001202 if (outer.isEmpty()) {
1203 return;
1204 }
1205
bsalomonf21dab92014-11-13 13:33:28 -08001206 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001207 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -08001208 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001209
1210 GR_CREATE_TRACE_MARKER("GrContext::drawDRRect", target);
1211
joshualitt2e3b3e32014-12-09 13:31:14 -08001212 GrColor color = paint.getColor();
joshualitt44701df2015-02-23 14:44:57 -08001213 if (!fOvalRenderer->drawDRRect(target,
1214 &pipelineBuilder,
1215 color,
1216 viewMatrix,
1217 paint.isAntiAlias(),
1218 outer,
1219 inner)) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001220 SkPath path;
1221 path.addRRect(inner);
1222 path.addRRect(outer);
1223 path.setFillType(SkPath::kEvenOdd_FillType);
1224
egdanield58a0ba2014-06-11 10:30:05 -07001225 GrStrokeInfo fillRec(SkStrokeRec::kFill_InitStyle);
egdaniel8dd688b2015-01-22 10:16:09 -08001226 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(),
1227 path, fillRec);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001228 }
1229}
1230
1231///////////////////////////////////////////////////////////////////////////////
1232
joshualitt570d2f82015-02-25 13:19:48 -08001233void GrContext::drawOval(GrRenderTarget* rt,
1234 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001235 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001236 const SkMatrix& viewMatrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001237 const SkRect& oval,
egdanield58a0ba2014-06-11 10:30:05 -07001238 const GrStrokeInfo& strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001239 RETURN_IF_ABANDONED
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001240 if (oval.isEmpty()) {
1241 return;
1242 }
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001243
egdanield58a0ba2014-06-11 10:30:05 -07001244 if (strokeInfo.isDashed()) {
1245 SkPath path;
1246 path.addOval(oval);
joshualitt570d2f82015-02-25 13:19:48 -08001247 this->drawPath(rt, clip, paint, viewMatrix, path, strokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -07001248 return;
1249 }
1250
bsalomonf21dab92014-11-13 13:33:28 -08001251 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001252 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -08001253 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001254 if (NULL == target) {
1255 return;
1256 }
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001257
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001258 GR_CREATE_TRACE_MARKER("GrContext::drawOval", target);
1259
egdanield58a0ba2014-06-11 10:30:05 -07001260 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1261
joshualitt2e3b3e32014-12-09 13:31:14 -08001262 GrColor color = paint.getColor();
joshualitt44701df2015-02-23 14:44:57 -08001263 if (!fOvalRenderer->drawOval(target,
1264 &pipelineBuilder,
1265 color,
1266 viewMatrix,
1267 paint.isAntiAlias(),
1268 oval,
1269 strokeRec)) {
bsalomon@google.com93c96602012-04-27 13:05:21 +00001270 SkPath path;
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001271 path.addOval(oval);
egdaniel8dd688b2015-01-22 10:16:09 -08001272 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(),
1273 path, strokeInfo);
skia.committer@gmail.com98ded842013-01-23 07:06:17 +00001274 }
bsalomon@google.com150d2842012-01-12 20:19:56 +00001275}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001276
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001277// Can 'path' be drawn as a pair of filled nested rectangles?
1278static bool is_nested_rects(GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -08001279 GrPipelineBuilder* pipelineBuilder,
joshualitt2e3b3e32014-12-09 13:31:14 -08001280 GrColor color,
joshualitt8059eb92014-12-29 15:10:07 -08001281 const SkMatrix& viewMatrix,
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001282 const SkPath& path,
1283 const SkStrokeRec& stroke,
bsalomon9c0822a2014-08-11 11:07:48 -07001284 SkRect rects[2]) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001285 SkASSERT(stroke.isFillStyle());
1286
1287 if (path.isInverseFillType()) {
1288 return false;
1289 }
1290
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001291 // TODO: this restriction could be lifted if we were willing to apply
1292 // the matrix to all the points individually rather than just to the rect
joshualitt8059eb92014-12-29 15:10:07 -08001293 if (!viewMatrix.preservesAxisAlignment()) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001294 return false;
1295 }
1296
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001297 SkPath::Direction dirs[2];
caryclark95bc5f32015-04-08 08:34:15 -07001298 if (!path.isNestedFillRects(rects, dirs)) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001299 return false;
1300 }
1301
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001302 if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001303 // The two rects need to be wound opposite to each other
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001304 return false;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001305 }
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001306
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001307 // Right now, nested rects where the margin is not the same width
1308 // all around do not render correctly
1309 const SkScalar* outer = rects[0].asScalars();
1310 const SkScalar* inner = rects[1].asScalars();
1311
robertphillips183e9852014-10-21 11:25:37 -07001312 bool allEq = true;
1313
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001314 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
robertphillips183e9852014-10-21 11:25:37 -07001315 bool allGoE1 = margin >= SK_Scalar1;
1316
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001317 for (int i = 1; i < 4; ++i) {
1318 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
robertphillips183e9852014-10-21 11:25:37 -07001319 if (temp < SK_Scalar1) {
1320 allGoE1 = false;
1321 }
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001322 if (!SkScalarNearlyEqual(margin, temp)) {
robertphillips183e9852014-10-21 11:25:37 -07001323 allEq = false;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001324 }
1325 }
1326
robertphillips183e9852014-10-21 11:25:37 -07001327 return allEq || allGoE1;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001328}
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001329
joshualitt25d9c152015-02-18 12:29:52 -08001330void GrContext::drawPath(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -08001331 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001332 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001333 const SkMatrix& viewMatrix,
1334 const SkPath& path,
1335 const GrStrokeInfo& strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001336 RETURN_IF_ABANDONED
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001337 if (path.isEmpty()) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001338 if (path.isInverseFillType()) {
joshualitt570d2f82015-02-25 13:19:48 -08001339 this->drawPaint(rt, clip, paint, viewMatrix);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001340 }
1341 return;
1342 }
1343
joshualitt2e3b3e32014-12-09 13:31:14 -08001344 GrColor color = paint.getColor();
egdanield58a0ba2014-06-11 10:30:05 -07001345 if (strokeInfo.isDashed()) {
egdaniele61c4112014-06-12 10:24:21 -07001346 SkPoint pts[2];
1347 if (path.isLine(pts)) {
bsalomonf21dab92014-11-13 13:33:28 -08001348 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001349 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -08001350 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001351 if (NULL == target) {
1352 return;
joshualittd27f73e2014-12-29 07:43:36 -08001353 }
egdaniele61c4112014-06-12 10:24:21 -07001354
egdaniel8dd688b2015-01-22 10:16:09 -08001355 if (GrDashingEffect::DrawDashLine(fGpu, target, &pipelineBuilder, color, viewMatrix,
1356 pts, paint, strokeInfo)) {
joshualittd27f73e2014-12-29 07:43:36 -08001357 return;
egdaniele61c4112014-06-12 10:24:21 -07001358 }
1359 }
1360
1361 // Filter dashed path into new path with the dashing applied
egdanield58a0ba2014-06-11 10:30:05 -07001362 const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo();
1363 SkTLazy<SkPath> effectPath;
1364 GrStrokeInfo newStrokeInfo(strokeInfo, false);
1365 SkStrokeRec* stroke = newStrokeInfo.getStrokeRecPtr();
1366 if (SkDashPath::FilterDashPath(effectPath.init(), path, stroke, NULL, info)) {
joshualitt570d2f82015-02-25 13:19:48 -08001367 this->drawPath(rt, clip, paint, viewMatrix, *effectPath.get(), newStrokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -07001368 return;
1369 }
1370
joshualitt570d2f82015-02-25 13:19:48 -08001371 this->drawPath(rt, clip, paint, viewMatrix, path, newStrokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -07001372 return;
1373 }
1374
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001375 // Note that internalDrawPath may sw-rasterize the path into a scratch texture.
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001376 // Scratch textures can be recycled after they are returned to the texture
1377 // cache. This presents a potential hazard for buffered drawing. However,
1378 // the writePixels that uploads to the scratch will perform a flush so we're
1379 // OK.
bsalomonf21dab92014-11-13 13:33:28 -08001380 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001381 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -08001382 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001383 if (NULL == target) {
1384 return;
1385 }
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001386
egdaniel93a37bc2014-07-21 13:47:57 -07001387 GR_CREATE_TRACE_MARKER1("GrContext::drawPath", target, "Is Convex", path.isConvex());
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001388
egdanield58a0ba2014-06-11 10:30:05 -07001389 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1390
egdaniel8dd688b2015-01-22 10:16:09 -08001391 bool useCoverageAA = paint.isAntiAlias() &&
1392 !pipelineBuilder.getRenderTarget()->isMultisampled();
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001393
egdanield58a0ba2014-06-11 10:30:05 -07001394 if (useCoverageAA && strokeRec.getWidth() < 0 && !path.isConvex()) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001395 // Concave AA paths are expensive - try to avoid them for special cases
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001396 SkRect rects[2];
1397
egdaniel8dd688b2015-01-22 10:16:09 -08001398 if (is_nested_rects(target, &pipelineBuilder, color, viewMatrix, path, strokeRec, rects)) {
joshualitt44701df2015-02-23 14:44:57 -08001399 fAARectRenderer->fillAANestedRects(target, &pipelineBuilder, color, viewMatrix, rects);
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001400 return;
1401 }
1402 }
1403
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001404 SkRect ovalRect;
1405 bool isOval = path.isOval(&ovalRect);
1406
joshualitt8059eb92014-12-29 15:10:07 -08001407 if (!isOval || path.isInverseFillType() ||
joshualitt44701df2015-02-23 14:44:57 -08001408 !fOvalRenderer->drawOval(target,
1409 &pipelineBuilder,
1410 color,
1411 viewMatrix,
1412 paint.isAntiAlias(),
1413 ovalRect,
1414 strokeRec)) {
egdaniel8dd688b2015-01-22 10:16:09 -08001415 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(),
1416 path, strokeInfo);
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001417 }
1418}
1419
joshualitt9853cce2014-11-17 14:22:48 -08001420void GrContext::internalDrawPath(GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -08001421 GrPipelineBuilder* pipelineBuilder,
joshualitt5531d512014-12-17 15:50:11 -08001422 const SkMatrix& viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -08001423 GrColor color,
joshualitt9853cce2014-11-17 14:22:48 -08001424 bool useAA,
1425 const SkPath& path,
egdanield58a0ba2014-06-11 10:30:05 -07001426 const GrStrokeInfo& strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001427 RETURN_IF_ABANDONED
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001428 SkASSERT(!path.isEmpty());
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001429
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001430 GR_CREATE_TRACE_MARKER("GrContext::internalDrawPath", target);
1431
1432
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001433 // An Assumption here is that path renderer would use some form of tweaking
1434 // the src color (either the input alpha or in the frag shader) to implement
1435 // aa. If we have some future driver-mojo path AA that can do the right
1436 // thing WRT to the blend then we'll need some query on the PR.
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001437 bool useCoverageAA = useAA &&
egdaniel0bdeec92015-02-23 12:12:54 -08001438 !pipelineBuilder->getRenderTarget()->isMultisampled();
bsalomon@google.com289533a2011-10-27 12:34:25 +00001439
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001440
1441 GrPathRendererChain::DrawType type =
1442 useCoverageAA ? GrPathRendererChain::kColorAntiAlias_DrawType :
joshualitt9853cce2014-11-17 14:22:48 -08001443 GrPathRendererChain::kColor_DrawType;
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001444
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001445 const SkPath* pathPtr = &path;
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001446 SkTLazy<SkPath> tmpPath;
egdanield58a0ba2014-06-11 10:30:05 -07001447 SkTCopyOnFirstWrite<SkStrokeRec> stroke(strokeInfo.getStrokeRec());
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001448
1449 // Try a 1st time without stroking the path and without allowing the SW renderer
egdaniel8dd688b2015-01-22 10:16:09 -08001450 GrPathRenderer* pr = this->getPathRenderer(target, pipelineBuilder, viewMatrix, *pathPtr,
1451 *stroke, false, type);
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001452
robertphillips@google.come79f3202014-02-11 16:30:21 +00001453 if (NULL == pr) {
joshualitt5531d512014-12-17 15:50:11 -08001454 if (!GrPathRenderer::IsStrokeHairlineOrEquivalent(*stroke, viewMatrix, NULL)) {
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001455 // It didn't work the 1st time, so try again with the stroked path
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001456 if (stroke->applyToPath(tmpPath.init(), *pathPtr)) {
1457 pathPtr = tmpPath.get();
1458 stroke.writable()->setFillStyle();
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001459 if (pathPtr->isEmpty()) {
1460 return;
1461 }
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001462 }
1463 }
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001464
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001465 // This time, allow SW renderer
egdaniel8dd688b2015-01-22 10:16:09 -08001466 pr = this->getPathRenderer(target, pipelineBuilder, viewMatrix, *pathPtr, *stroke, true,
1467 type);
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001468 }
1469
robertphillips@google.come79f3202014-02-11 16:30:21 +00001470 if (NULL == pr) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001471#ifdef SK_DEBUG
tfarina38406c82014-10-31 07:11:12 -07001472 SkDebugf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001473#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001474 return;
1475 }
1476
egdaniel8dd688b2015-01-22 10:16:09 -08001477 pr->drawPath(target, pipelineBuilder, color, viewMatrix, *pathPtr, *stroke, useCoverageAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001478}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001479
bsalomon@google.com27847de2011-02-22 20:59:41 +00001480////////////////////////////////////////////////////////////////////////////////
1481
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001482void GrContext::flush(int flagsBitfield) {
robertphillips@google.come7db8d62013-07-04 11:48:52 +00001483 if (NULL == fDrawBuffer) {
1484 return;
1485 }
1486
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001487 if (kDiscard_FlushBit & flagsBitfield) {
1488 fDrawBuffer->reset();
1489 } else {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001490 fDrawBuffer->flush();
junov@google.com53a55842011-06-08 22:55:10 +00001491 }
bsalomon3f324322015-04-08 11:01:54 -07001492 fResourceCache->notifyFlushOccurred();
bsalomonf21dab92014-11-13 13:33:28 -08001493 fFlushToReduceCacheSize = false;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001494}
1495
bsalomon81beccc2014-10-13 12:32:55 -07001496bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
1497 const void* inPixels, size_t outRowBytes, void* outPixels) {
1498 SkSrcPixelInfo srcPI;
jvanverthfa1e8a72014-12-22 08:31:49 -08001499 if (!GrPixelConfig2ColorAndProfileType(srcConfig, &srcPI.fColorType, NULL)) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001500 return false;
1501 }
bsalomon81beccc2014-10-13 12:32:55 -07001502 srcPI.fAlphaType = kUnpremul_SkAlphaType;
1503 srcPI.fPixels = inPixels;
1504 srcPI.fRowBytes = inRowBytes;
1505
1506 SkDstPixelInfo dstPI;
1507 dstPI.fColorType = srcPI.fColorType;
1508 dstPI.fAlphaType = kPremul_SkAlphaType;
1509 dstPI.fPixels = outPixels;
1510 dstPI.fRowBytes = outRowBytes;
1511
1512 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001513}
1514
bsalomon81beccc2014-10-13 12:32:55 -07001515bool GrContext::writeSurfacePixels(GrSurface* surface,
1516 int left, int top, int width, int height,
1517 GrPixelConfig srcConfig, const void* buffer, size_t rowBytes,
1518 uint32_t pixelOpsFlags) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001519 RETURN_FALSE_IF_ABANDONED
bsalomon81beccc2014-10-13 12:32:55 -07001520 {
1521 GrTexture* texture = NULL;
1522 if (!(kUnpremul_PixelOpsFlag & pixelOpsFlags) && (texture = surface->asTexture()) &&
1523 fGpu->canWriteTexturePixels(texture, srcConfig)) {
1524
1525 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) &&
1526 surface->surfacePriv().hasPendingIO()) {
1527 this->flush();
1528 }
1529 return fGpu->writeTexturePixels(texture, left, top, width, height,
1530 srcConfig, buffer, rowBytes);
1531 // Don't need to check kFlushWrites_PixelOp here, we just did a direct write so the
1532 // upload is already flushed.
1533 }
1534 }
1535
1536 // If we didn't do a direct texture write then we upload the pixels to a texture and draw.
1537 GrRenderTarget* renderTarget = surface->asRenderTarget();
1538 if (NULL == renderTarget) {
1539 return false;
1540 }
1541
1542 // We ignore the preferred config unless it is a R/B swap of the src config. In that case
1543 // we will upload the original src data to a scratch texture but we will spoof it as the swapped
1544 // config. This scratch will then have R and B swapped. We correct for this by swapping again
1545 // when drawing the scratch to the dst using a conversion effect.
1546 bool swapRAndB = false;
1547 GrPixelConfig writeConfig = srcConfig;
1548 if (GrPixelConfigSwapRAndB(srcConfig) ==
1549 fGpu->preferredWritePixelsConfig(srcConfig, renderTarget->config())) {
1550 writeConfig = GrPixelConfigSwapRAndB(srcConfig);
1551 swapRAndB = true;
1552 }
1553
bsalomonf2703d82014-10-28 14:33:06 -07001554 GrSurfaceDesc desc;
bsalomon81beccc2014-10-13 12:32:55 -07001555 desc.fWidth = width;
1556 desc.fHeight = height;
1557 desc.fConfig = writeConfig;
bsalomone3059732014-10-14 11:47:22 -07001558 SkAutoTUnref<GrTexture> texture(this->refScratchTexture(desc, kApprox_ScratchTexMatch));
1559 if (!texture) {
bsalomon81beccc2014-10-13 12:32:55 -07001560 return false;
1561 }
1562
1563 SkAutoTUnref<const GrFragmentProcessor> fp;
1564 SkMatrix textureMatrix;
1565 textureMatrix.setIDiv(texture->width(), texture->height());
1566
1567 // allocate a tmp buffer and sw convert the pixels to premul
1568 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
1569
1570 if (kUnpremul_PixelOpsFlag & pixelOpsFlags) {
1571 if (!GrPixelConfigIs8888(srcConfig)) {
1572 return false;
1573 }
1574 fp.reset(this->createUPMToPMEffect(texture, swapRAndB, textureMatrix));
1575 // handle the unpremul step on the CPU if we couldn't create an effect to do it.
1576 if (NULL == fp) {
1577 size_t tmpRowBytes = 4 * width;
1578 tmpPixels.reset(width * height);
1579 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
1580 tmpPixels.get())) {
1581 return false;
1582 }
1583 rowBytes = tmpRowBytes;
1584 buffer = tmpPixels.get();
1585 }
1586 }
1587 if (NULL == fp) {
1588 fp.reset(GrConfigConversionEffect::Create(texture,
1589 swapRAndB,
1590 GrConfigConversionEffect::kNone_PMConversion,
1591 textureMatrix));
1592 }
1593
1594 // Even if the client told us not to flush, we still flush here. The client may have known that
1595 // writes to the original surface caused no data hazards, but they can't know that the scratch
1596 // we just got is safe.
1597 if (texture->surfacePriv().hasPendingIO()) {
1598 this->flush();
1599 }
1600 if (!fGpu->writeTexturePixels(texture, 0, 0, width, height,
1601 writeConfig, buffer, rowBytes)) {
1602 return false;
1603 }
1604
1605 SkMatrix matrix;
1606 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
1607
1608 // This function can be called in the midst of drawing another object (e.g., when uploading a
1609 // SW-rasterized clip while issuing a draw). So we push the current geometry state before
1610 // drawing a rect to the render target.
1611 // The bracket ensures we pop the stack if we wind up flushing below.
1612 {
joshualitt570d2f82015-02-25 13:19:48 -08001613 GrDrawTarget* drawTarget = this->prepareToDraw();
1614 if (!drawTarget) {
1615 return false;
1616 }
joshualitt9853cce2014-11-17 14:22:48 -08001617 GrDrawTarget::AutoGeometryPush agp(drawTarget);
1618
egdaniel8dd688b2015-01-22 10:16:09 -08001619 GrPipelineBuilder pipelineBuilder;
1620 pipelineBuilder.addColorProcessor(fp);
1621 pipelineBuilder.setRenderTarget(renderTarget);
joshualitt44701df2015-02-23 14:44:57 -08001622 drawTarget->drawSimpleRect(&pipelineBuilder,
1623 GrColor_WHITE,
1624 matrix,
joshualitt8059eb92014-12-29 15:10:07 -08001625 SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)));
bsalomon81beccc2014-10-13 12:32:55 -07001626 }
1627
1628 if (kFlushWrites_PixelOp & pixelOpsFlags) {
1629 this->flushSurfaceWrites(surface);
1630 }
1631
1632 return true;
1633}
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001634
reed@google.com7111d462014-03-25 16:20:24 +00001635// toggles between RGBA and BGRA
1636static SkColorType toggle_colortype32(SkColorType ct) {
1637 if (kRGBA_8888_SkColorType == ct) {
1638 return kBGRA_8888_SkColorType;
1639 } else {
1640 SkASSERT(kBGRA_8888_SkColorType == ct);
1641 return kRGBA_8888_SkColorType;
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001642 }
1643}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001644
bsalomon@google.com0342a852012-08-20 19:22:38 +00001645bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
1646 int left, int top, int width, int height,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001647 GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001648 uint32_t flags) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001649 RETURN_FALSE_IF_ABANDONED
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001650 ASSERT_OWNED_RESOURCE(target);
bsalomon89c62982014-11-03 12:08:42 -08001651 SkASSERT(target);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001652
bsalomonafbf2d62014-09-30 12:18:44 -07001653 if (!(kDontFlush_PixelOpsFlag & flags) && target->surfacePriv().hasPendingWrite()) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001654 this->flush();
1655 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001656
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001657 // Determine which conversions have to be applied: flipY, swapRAnd, and/or unpremul.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001658
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001659 // If fGpu->readPixels would incur a y-flip cost then we will read the pixels upside down. We'll
1660 // either do the flipY by drawing into a scratch with a matrix or on the cpu after the read.
1661 bool flipY = fGpu->readPixelsWillPayForYFlip(target, left, top,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001662 width, height, dstConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001663 rowBytes);
bsalomon@google.com9c680582013-02-06 18:17:50 +00001664 // We ignore the preferred config if it is different than our config unless it is an R/B swap.
1665 // In that case we'll perform an R and B swap while drawing to a scratch texture of the swapped
1666 // config. Then we will call readPixels on the scratch with the swapped config. The swaps during
1667 // the draw cancels out the fact that we call readPixels with a config that is R/B swapped from
1668 // dstConfig.
1669 GrPixelConfig readConfig = dstConfig;
1670 bool swapRAndB = false;
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +00001671 if (GrPixelConfigSwapRAndB(dstConfig) ==
1672 fGpu->preferredReadPixelsConfig(dstConfig, target->config())) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001673 readConfig = GrPixelConfigSwapRAndB(readConfig);
1674 swapRAndB = true;
1675 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001676
bsalomon@google.com0342a852012-08-20 19:22:38 +00001677 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001678
bsalomon@google.com9c680582013-02-06 18:17:50 +00001679 if (unpremul && !GrPixelConfigIs8888(dstConfig)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001680 // The unpremul flag is only allowed for these two configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001681 return false;
1682 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001683
bsalomon191bcc02014-11-14 11:31:13 -08001684 SkAutoTUnref<GrTexture> tempTexture;
1685
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001686 // If the src is a texture and we would have to do conversions after read pixels, we instead
1687 // do the conversions by drawing the src to a scratch texture. If we handle any of the
1688 // conversions in the draw we set the corresponding bool to false so that we don't reapply it
1689 // on the read back pixels.
1690 GrTexture* src = target->asTexture();
bsalomon49f085d2014-09-05 13:34:00 -07001691 if (src && (swapRAndB || unpremul || flipY)) {
bsalomon81beccc2014-10-13 12:32:55 -07001692 // Make the scratch a render so we can read its pixels.
bsalomonf2703d82014-10-28 14:33:06 -07001693 GrSurfaceDesc desc;
1694 desc.fFlags = kRenderTarget_GrSurfaceFlag;
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001695 desc.fWidth = width;
1696 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001697 desc.fConfig = readConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001698 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001699
bsalomon@google.com9c680582013-02-06 18:17:50 +00001700 // 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 +00001701 // match the passed rect. However, if we see many different size rectangles we will trash
1702 // our texture cache and pay the cost of creating and destroying many textures. So, we only
1703 // request an exact match when the caller is reading an entire RT.
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001704 ScratchTexMatch match = kApprox_ScratchTexMatch;
1705 if (0 == left &&
1706 0 == top &&
1707 target->width() == width &&
1708 target->height() == height &&
1709 fGpu->fullReadPixelsIsFasterThanPartial()) {
1710 match = kExact_ScratchTexMatch;
1711 }
bsalomon191bcc02014-11-14 11:31:13 -08001712 tempTexture.reset(this->refScratchTexture(desc, match));
1713 if (tempTexture) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001714 // compute a matrix to perform the draw
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001715 SkMatrix textureMatrix;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001716 textureMatrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001717 textureMatrix.postIDiv(src->width(), src->height());
1718
joshualittb0a8a372014-09-23 09:50:21 -07001719 SkAutoTUnref<const GrFragmentProcessor> fp;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001720 if (unpremul) {
joshualittb0a8a372014-09-23 09:50:21 -07001721 fp.reset(this->createPMToUPMEffect(src, swapRAndB, textureMatrix));
1722 if (fp) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001723 unpremul = false; // we no longer need to do this on CPU after the read back.
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001724 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001725 }
1726 // If we failed to create a PM->UPM effect and have no other conversions to perform then
1727 // there is no longer any point to using the scratch.
joshualittb0a8a372014-09-23 09:50:21 -07001728 if (fp || flipY || swapRAndB) {
1729 if (!fp) {
1730 fp.reset(GrConfigConversionEffect::Create(
1731 src, swapRAndB, GrConfigConversionEffect::kNone_PMConversion,
1732 textureMatrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001733 }
1734 swapRAndB = false; // we will handle the swap in the draw.
bsalomon@google.comc4364992011-11-07 15:54:49 +00001735
robertphillips@google.com13f181f2013-03-02 12:02:08 +00001736 // We protect the existing geometry here since it may not be
1737 // clear to the caller that a draw operation (i.e., drawSimpleRect)
1738 // can be invoked in this method
joshualitt5c55fef2014-10-31 14:04:35 -07001739 {
joshualitt9853cce2014-11-17 14:22:48 -08001740 GrDrawTarget::AutoGeometryPush agp(fDrawBuffer);
egdaniel8dd688b2015-01-22 10:16:09 -08001741 GrPipelineBuilder pipelineBuilder;
joshualitt5c55fef2014-10-31 14:04:35 -07001742 SkASSERT(fp);
egdaniel8dd688b2015-01-22 10:16:09 -08001743 pipelineBuilder.addColorProcessor(fp);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001744
egdaniel8dd688b2015-01-22 10:16:09 -08001745 pipelineBuilder.setRenderTarget(tempTexture->asRenderTarget());
joshualitt5c55fef2014-10-31 14:04:35 -07001746 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
joshualitt44701df2015-02-23 14:44:57 -08001747 fDrawBuffer->drawSimpleRect(&pipelineBuilder,
1748 GrColor_WHITE,
1749 SkMatrix::I(),
egdaniel8dd688b2015-01-22 10:16:09 -08001750 rect);
joshualitt5c55fef2014-10-31 14:04:35 -07001751 // we want to read back from the scratch's origin
1752 left = 0;
1753 top = 0;
bsalomon191bcc02014-11-14 11:31:13 -08001754 target = tempTexture->asRenderTarget();
joshualitt5c55fef2014-10-31 14:04:35 -07001755 }
1756 this->flushSurfaceWrites(target);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001757 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001758 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001759 }
joshualitt5c55fef2014-10-31 14:04:35 -07001760
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001761 if (!fGpu->readPixels(target,
1762 left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001763 readConfig, buffer, rowBytes)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001764 return false;
1765 }
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001766 // Perform any conversions we weren't able to perform using a scratch texture.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001767 if (unpremul || swapRAndB) {
reed@google.com7111d462014-03-25 16:20:24 +00001768 SkDstPixelInfo dstPI;
jvanverthfa1e8a72014-12-22 08:31:49 -08001769 if (!GrPixelConfig2ColorAndProfileType(dstConfig, &dstPI.fColorType, NULL)) {
reed@google.com7111d462014-03-25 16:20:24 +00001770 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001771 }
reed@google.com7111d462014-03-25 16:20:24 +00001772 dstPI.fAlphaType = kUnpremul_SkAlphaType;
1773 dstPI.fPixels = buffer;
1774 dstPI.fRowBytes = rowBytes;
1775
1776 SkSrcPixelInfo srcPI;
1777 srcPI.fColorType = swapRAndB ? toggle_colortype32(dstPI.fColorType) : dstPI.fColorType;
1778 srcPI.fAlphaType = kPremul_SkAlphaType;
1779 srcPI.fPixels = buffer;
1780 srcPI.fRowBytes = rowBytes;
1781
1782 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001783 }
1784 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001785}
1786
bsalomon87a94eb2014-11-03 14:28:32 -08001787void GrContext::prepareSurfaceForExternalRead(GrSurface* surface) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001788 RETURN_IF_ABANDONED
bsalomon87a94eb2014-11-03 14:28:32 -08001789 SkASSERT(surface);
1790 ASSERT_OWNED_RESOURCE(surface);
1791 if (surface->surfacePriv().hasPendingIO()) {
1792 this->flush();
1793 }
1794 GrRenderTarget* rt = surface->asRenderTarget();
1795 if (fGpu && rt) {
1796 fGpu->resolveRenderTarget(rt);
bsalomon41ebbdd2014-08-04 08:31:39 -07001797 }
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001798}
1799
bsalomon41ebbdd2014-08-04 08:31:39 -07001800void GrContext::discardRenderTarget(GrRenderTarget* renderTarget) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001801 RETURN_IF_ABANDONED
bsalomon41ebbdd2014-08-04 08:31:39 -07001802 SkASSERT(renderTarget);
1803 ASSERT_OWNED_RESOURCE(renderTarget);
bsalomonf21dab92014-11-13 13:33:28 -08001804 AutoCheckFlush acf(this);
joshualitt570d2f82015-02-25 13:19:48 -08001805 GrDrawTarget* target = this->prepareToDraw();
bsalomon41ebbdd2014-08-04 08:31:39 -07001806 if (NULL == target) {
1807 return;
1808 }
1809 target->discard(renderTarget);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001810}
1811
bsalomonf80bfed2014-10-07 05:56:02 -07001812void GrContext::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
1813 const SkIPoint& dstPoint, uint32_t pixelOpsFlags) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001814 RETURN_IF_ABANDONED
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001815 if (NULL == src || NULL == dst) {
1816 return;
1817 }
bsalomone3d4bf22014-09-23 09:15:03 -07001818 ASSERT_OWNED_RESOURCE(src);
junov2bb52102014-09-29 10:18:59 -07001819 ASSERT_OWNED_RESOURCE(dst);
Brian Salomon34a98952014-09-24 11:41:24 -04001820
bsalomonf80bfed2014-10-07 05:56:02 -07001821 // Since we're going to the draw target and not GPU, no need to check kNoFlush
1822 // here.
junov96c118e2014-09-26 13:09:47 -07001823
joshualitt570d2f82015-02-25 13:19:48 -08001824 GrDrawTarget* target = this->prepareToDraw();
junov96c118e2014-09-26 13:09:47 -07001825 if (NULL == target) {
1826 return;
1827 }
junov96c118e2014-09-26 13:09:47 -07001828 target->copySurface(dst, src, srcRect, dstPoint);
bsalomonf80bfed2014-10-07 05:56:02 -07001829
1830 if (kFlushWrites_PixelOp & pixelOpsFlags) {
1831 this->flush();
1832 }
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001833}
1834
bsalomonf80bfed2014-10-07 05:56:02 -07001835void GrContext::flushSurfaceWrites(GrSurface* surface) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001836 RETURN_IF_ABANDONED
bsalomonf80bfed2014-10-07 05:56:02 -07001837 if (surface->surfacePriv().hasPendingWrite()) {
1838 this->flush();
1839 }
1840}
1841
egdaniel8dd688b2015-01-22 10:16:09 -08001842GrDrawTarget* GrContext::prepareToDraw(GrPipelineBuilder* pipelineBuilder,
joshualitt25d9c152015-02-18 12:29:52 -08001843 GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -08001844 const GrClip& clip,
joshualitt9853cce2014-11-17 14:22:48 -08001845 const GrPaint* paint,
1846 const AutoCheckFlush* acf) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001847 if (NULL == fGpu || NULL == fDrawBuffer) {
bsalomon41ebbdd2014-08-04 08:31:39 -07001848 return NULL;
1849 }
1850
joshualitt570d2f82015-02-25 13:19:48 -08001851 ASSERT_OWNED_RESOURCE(rt);
1852 SkASSERT(rt && paint && acf);
1853 pipelineBuilder->setFromPaint(*paint, rt, clip);
1854 return fDrawBuffer;
1855}
1856
1857GrDrawTarget* GrContext::prepareToDraw() {
1858 if (NULL == fGpu) {
1859 return NULL;
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001860 }
joshualitt5c55fef2014-10-31 14:04:35 -07001861 return fDrawBuffer;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001862}
1863
robertphillips@google.com72176b22012-05-23 13:19:12 +00001864/*
1865 * This method finds a path renderer that can draw the specified path on
1866 * the provided target.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001867 * Due to its expense, the software path renderer has split out so it can
robertphillips@google.com72176b22012-05-23 13:19:12 +00001868 * can be individually allowed/disallowed via the "allowSW" boolean.
1869 */
joshualitt9853cce2014-11-17 14:22:48 -08001870GrPathRenderer* GrContext::getPathRenderer(const GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -08001871 const GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -08001872 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -08001873 const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001874 const SkStrokeRec& stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001875 bool allowSW,
1876 GrPathRendererChain::DrawType drawType,
1877 GrPathRendererChain::StencilSupport* stencilSupport) {
1878
bsalomon@google.com30085192011-08-19 15:42:31 +00001879 if (NULL == fPathRendererChain) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001880 fPathRendererChain = SkNEW_ARGS(GrPathRendererChain, (this));
bsalomon@google.com30085192011-08-19 15:42:31 +00001881 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001882
joshualitt9853cce2014-11-17 14:22:48 -08001883 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(target,
egdaniel8dd688b2015-01-22 10:16:09 -08001884 pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -08001885 viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -08001886 path,
sugoi@google.com12b4e272012-12-06 20:13:11 +00001887 stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001888 drawType,
1889 stencilSupport);
robertphillips@google.com72176b22012-05-23 13:19:12 +00001890
1891 if (NULL == pr && allowSW) {
1892 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001893 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001894 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001895 pr = fSoftwarePathRenderer;
1896 }
1897
1898 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001899}
1900
bsalomon@google.com27847de2011-02-22 20:59:41 +00001901////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00001902bool GrContext::isConfigRenderable(GrPixelConfig config, bool withMSAA) const {
1903 return fGpu->caps()->isConfigRenderable(config, withMSAA);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001904}
1905
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +00001906int GrContext::getRecommendedSampleCount(GrPixelConfig config,
1907 SkScalar dpi) const {
1908 if (!this->isConfigRenderable(config, true)) {
1909 return 0;
1910 }
1911 int chosenSampleCount = 0;
1912 if (fGpu->caps()->pathRenderingSupport()) {
1913 if (dpi >= 250.0f) {
1914 chosenSampleCount = 4;
1915 } else {
1916 chosenSampleCount = 16;
1917 }
1918 }
1919 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ?
1920 chosenSampleCount : 0;
1921}
1922
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001923void GrContext::setupDrawBuffer() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001924 SkASSERT(NULL == fDrawBuffer);
1925 SkASSERT(NULL == fDrawBufferVBAllocPool);
1926 SkASSERT(NULL == fDrawBufferIBAllocPool);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001927
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001928 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001929 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001930 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001931 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001932 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001933 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001934 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001935 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001936
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001937 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001938 fDrawBufferVBAllocPool,
1939 fDrawBufferIBAllocPool));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001940}
1941
bsalomon@google.com21c10c52013-06-13 17:44:07 +00001942GrDrawTarget* GrContext::getTextTarget() {
joshualitt570d2f82015-02-25 13:19:48 -08001943 return this->prepareToDraw();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001944}
1945
1946const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1947 return fGpu->getQuadIndexBuffer();
1948}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001949
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001950namespace {
1951void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
1952 GrConfigConversionEffect::PMConversion pmToUPM;
1953 GrConfigConversionEffect::PMConversion upmToPM;
1954 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
1955 *pmToUPMValue = pmToUPM;
1956 *upmToPMValue = upmToPM;
1957}
1958}
1959
joshualittb0a8a372014-09-23 09:50:21 -07001960const GrFragmentProcessor* GrContext::createPMToUPMEffect(GrTexture* texture,
1961 bool swapRAndB,
1962 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001963 if (!fDidTestPMConversions) {
1964 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001965 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001966 }
1967 GrConfigConversionEffect::PMConversion pmToUPM =
1968 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
1969 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001970 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001971 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001972 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001973 }
1974}
1975
joshualittb0a8a372014-09-23 09:50:21 -07001976const GrFragmentProcessor* GrContext::createUPMToPMEffect(GrTexture* texture,
1977 bool swapRAndB,
1978 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001979 if (!fDidTestPMConversions) {
1980 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001981 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001982 }
1983 GrConfigConversionEffect::PMConversion upmToPM =
1984 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
1985 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001986 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001987 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001988 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001989 }
1990}
1991
bsalomon37f9a262015-02-02 13:00:10 -08001992//////////////////////////////////////////////////////////////////////////////
1993
1994void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
1995 if (maxTextures) {
bsalomon0ea80f42015-02-11 10:49:59 -08001996 *maxTextures = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -08001997 }
1998 if (maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -08001999 *maxTextureBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -08002000 }
2001}
2002
2003void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -08002004 fResourceCache->setLimits(maxTextures, maxTextureBytes);
bsalomon37f9a262015-02-02 13:00:10 -08002005}
2006
bsalomonf99e9612015-02-19 08:24:16 -08002007void GrContext::addResourceToCache(const GrUniqueKey& key, GrGpuResource* resource) {
bsalomon37f9a262015-02-02 13:00:10 -08002008 ASSERT_OWNED_RESOURCE(resource);
bsalomonf99e9612015-02-19 08:24:16 -08002009 if (!resource) {
2010 return;
bsalomon37f9a262015-02-02 13:00:10 -08002011 }
bsalomonf99e9612015-02-19 08:24:16 -08002012 resource->resourcePriv().setUniqueKey(key);
bsalomon37f9a262015-02-02 13:00:10 -08002013}
2014
bsalomon8718aaf2015-02-19 07:24:21 -08002015bool GrContext::isResourceInCache(const GrUniqueKey& key) const {
2016 return fResourceCache->hasUniqueKey(key);
commit-bot@chromium.org95a2b0e2014-05-05 19:21:16 +00002017}
2018
bsalomon8718aaf2015-02-19 07:24:21 -08002019GrGpuResource* GrContext::findAndRefCachedResource(const GrUniqueKey& key) {
2020 return fResourceCache->findAndRefUniqueResource(key);
commit-bot@chromium.org95a2b0e2014-05-05 19:21:16 +00002021}
2022
bsalomon37f9a262015-02-02 13:00:10 -08002023//////////////////////////////////////////////////////////////////////////////
2024
egdanielbbcb38d2014-06-19 10:19:29 -07002025void GrContext::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
2026 fGpu->addGpuTraceMarker(marker);
bsalomon49f085d2014-09-05 13:34:00 -07002027 if (fDrawBuffer) {
egdanielbbcb38d2014-06-19 10:19:29 -07002028 fDrawBuffer->addGpuTraceMarker(marker);
2029 }
2030}
2031
2032void GrContext::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
2033 fGpu->removeGpuTraceMarker(marker);
bsalomon49f085d2014-09-05 13:34:00 -07002034 if (fDrawBuffer) {
egdanielbbcb38d2014-06-19 10:19:29 -07002035 fDrawBuffer->removeGpuTraceMarker(marker);
2036 }
2037}
2038