blob: be404790ea46c81e8c029e20545497a9097142c1 [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();
joshualitt26ffc002015-04-16 11:24:04 -0700196 fTextBlobCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000197}
198
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000199void GrContext::resetContext(uint32_t state) {
200 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000201}
202
203void GrContext::freeGpuResources() {
204 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000205
bsalomon49f085d2014-09-05 13:34:00 -0700206 if (fDrawBuffer) {
bsalomonc8dc1f72014-08-21 13:02:13 -0700207 fDrawBuffer->purgeResources();
208 }
robertphillips@google.comff175842012-05-14 19:31:39 +0000209
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000210 fAARectRenderer->reset();
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000211 fOvalRenderer->reset();
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000212
joshualitt7c3a2f82015-03-31 13:32:05 -0700213 fBatchFontCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000214 fFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000215 fLayerCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000216 // a path renderer may be holding onto resources
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000217 SkSafeSetNull(fPathRendererChain);
218 SkSafeSetNull(fSoftwarePathRenderer);
bsalomon3033b9f2015-04-13 11:09:56 -0700219
220 fResourceCache->purgeAllUnlocked();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000221}
222
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000223void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
bsalomon71cb0c22014-11-14 12:10:14 -0800224 if (resourceCount) {
bsalomon0ea80f42015-02-11 10:49:59 -0800225 *resourceCount = fResourceCache->getBudgetedResourceCount();
bsalomon71cb0c22014-11-14 12:10:14 -0800226 }
227 if (resourceBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -0800228 *resourceBytes = fResourceCache->getBudgetedResourceBytes();
bsalomon71cb0c22014-11-14 12:10:14 -0800229 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000230}
231
kkinnunenc6cb56f2014-06-24 00:12:27 -0700232GrTextContext* GrContext::createTextContext(GrRenderTarget* renderTarget,
joshualitt6e8cd962015-03-20 10:30:14 -0700233 SkGpuDevice* gpuDevice,
kkinnunenc6cb56f2014-06-24 00:12:27 -0700234 const SkDeviceProperties&
235 leakyProperties,
236 bool enableDistanceFieldFonts) {
bsalomon6bc1b5f2015-02-23 09:06:38 -0800237 if (fGpu->caps()->pathRenderingSupport() && renderTarget->isMultisampled()) {
egdaniel8dc7c3a2015-04-16 11:22:42 -0700238 GrStencilAttachment* sb = renderTarget->renderTargetPriv().attachStencilAttachment();
bsalomon6bc1b5f2015-02-23 09:06:38 -0800239 if (sb) {
joshualitt6e8cd962015-03-20 10:30:14 -0700240 return GrStencilAndCoverTextContext::Create(this, gpuDevice, leakyProperties);
bsalomon6bc1b5f2015-02-23 09:06:38 -0800241 }
jvanverth8c27a182014-10-14 08:45:50 -0700242 }
243
joshualitt7c3a2f82015-03-31 13:32:05 -0700244#ifdef USE_BITMAP_TEXTBLOBS
joshualittdbd35932015-04-02 09:19:04 -0700245 return GrAtlasTextContext::Create(this, gpuDevice, leakyProperties);
joshualitt7c3a2f82015-03-31 13:32:05 -0700246#else
joshualitt6e8cd962015-03-20 10:30:14 -0700247 return GrDistanceFieldTextContext::Create(this, gpuDevice, leakyProperties,
248 enableDistanceFieldFonts);
joshualitt7c3a2f82015-03-31 13:32:05 -0700249#endif
kkinnunenc6cb56f2014-06-24 00:12:27 -0700250}
251
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000252////////////////////////////////////////////////////////////////////////////////
bsalomond0423582015-02-06 08:49:24 -0800253enum ScratchTextureFlags {
254 kExact_ScratchTextureFlag = 0x1,
255 kNoPendingIO_ScratchTextureFlag = 0x2,
256 kNoCreate_ScratchTextureFlag = 0x4,
257};
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000258
bsalomond2a6f4e2015-02-04 10:55:54 -0800259bool GrContext::isConfigTexturable(GrPixelConfig config) const {
260 return fGpu->caps()->isConfigTexturable(config);
261}
262
263bool GrContext::npotTextureTileSupport() const {
264 return fGpu->caps()->npotTextureTileSupport();
265}
266
bsalomond0423582015-02-06 08:49:24 -0800267GrTexture* GrContext::createTexture(const GrSurfaceDesc& desc, bool budgeted, const void* srcData,
bsalomon37f9a262015-02-02 13:00:10 -0800268 size_t rowBytes) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800269 RETURN_NULL_IF_ABANDONED
bsalomond0423582015-02-06 08:49:24 -0800270 if ((desc.fFlags & kRenderTarget_GrSurfaceFlag) &&
271 !this->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
bsalomond2a6f4e2015-02-04 10:55:54 -0800272 return NULL;
273 }
bsalomond0423582015-02-06 08:49:24 -0800274 if (!GrPixelConfigIsCompressed(desc.fConfig)) {
275 static const uint32_t kFlags = kExact_ScratchTextureFlag |
276 kNoCreate_ScratchTextureFlag;
277 if (GrTexture* texture = this->internalRefScratchTexture(desc, kFlags)) {
278 if (!srcData || texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
279 srcData, rowBytes)) {
280 if (!budgeted) {
bsalomon3582d3e2015-02-13 14:20:05 -0800281 texture->resourcePriv().makeUnbudgeted();
bsalomond0423582015-02-06 08:49:24 -0800282 }
283 return texture;
284 }
285 texture->unref();
286 }
287 }
288 return fGpu->createTexture(desc, budgeted, srcData, rowBytes);
289}
bsalomond2a6f4e2015-02-04 10:55:54 -0800290
bsalomond0423582015-02-06 08:49:24 -0800291GrTexture* GrContext::refScratchTexture(const GrSurfaceDesc& desc, ScratchTexMatch match,
292 bool calledDuringFlush) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800293 RETURN_NULL_IF_ABANDONED
bsalomond0423582015-02-06 08:49:24 -0800294 // Currently we don't recycle compressed textures as scratch.
295 if (GrPixelConfigIsCompressed(desc.fConfig)) {
296 return NULL;
297 } else {
298 uint32_t flags = 0;
299 if (kExact_ScratchTexMatch == match) {
300 flags |= kExact_ScratchTextureFlag;
301 }
302 if (calledDuringFlush) {
303 flags |= kNoPendingIO_ScratchTextureFlag;
304 }
305 return this->internalRefScratchTexture(desc, flags);
306 }
307}
308
309GrTexture* GrContext::internalRefScratchTexture(const GrSurfaceDesc& inDesc, uint32_t flags) {
310 SkASSERT(!GrPixelConfigIsCompressed(inDesc.fConfig));
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000311
bsalomonf2703d82014-10-28 14:33:06 -0700312 SkTCopyOnFirstWrite<GrSurfaceDesc> desc(inDesc);
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000313
bsalomonf2703d82014-10-28 14:33:06 -0700314 if (fGpu->caps()->reuseScratchTextures() || (desc->fFlags & kRenderTarget_GrSurfaceFlag)) {
bsalomond0423582015-02-06 08:49:24 -0800315 if (!(kExact_ScratchTextureFlag & flags)) {
bsalomonbcf0a522014-10-08 08:40:09 -0700316 // bin by pow2 with a reasonable min
317 static const int MIN_SIZE = 16;
bsalomonf2703d82014-10-28 14:33:06 -0700318 GrSurfaceDesc* wdesc = desc.writable();
bsalomonbcf0a522014-10-08 08:40:09 -0700319 wdesc->fWidth = SkTMax(MIN_SIZE, GrNextPow2(desc->fWidth));
320 wdesc->fHeight = SkTMax(MIN_SIZE, GrNextPow2(desc->fHeight));
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000321 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000322
bsalomon6bc1b5f2015-02-23 09:06:38 -0800323 GrScratchKey key;
324 GrTexturePriv::ComputeScratchKey(*desc, &key);
325 uint32_t scratchFlags = 0;
326 if (kNoPendingIO_ScratchTextureFlag & flags) {
327 scratchFlags = GrResourceCache::kRequireNoPendingIO_ScratchFlag;
328 } else if (!(desc->fFlags & kRenderTarget_GrSurfaceFlag)) {
329 // If it is not a render target then it will most likely be populated by
330 // writePixels() which will trigger a flush if the texture has pending IO.
331 scratchFlags = GrResourceCache::kPreferNoPendingIO_ScratchFlag;
332 }
333 GrGpuResource* resource = fResourceCache->findAndRefScratchResource(key, scratchFlags);
334 if (resource) {
335 GrSurface* surface = static_cast<GrSurface*>(resource);
336 GrRenderTarget* rt = surface->asRenderTarget();
337 if (rt && fGpu->caps()->discardRenderTargetSupport()) {
338 rt->discard();
bsalomon000f8292014-10-15 19:04:14 -0700339 }
bsalomon6bc1b5f2015-02-23 09:06:38 -0800340 return surface->asTexture();
341 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000342 }
343
bsalomond0423582015-02-06 08:49:24 -0800344 if (!(kNoCreate_ScratchTextureFlag & flags)) {
bsalomon3582d3e2015-02-13 14:20:05 -0800345 return fGpu->createTexture(*desc, true, NULL, 0);
bsalomone167f962015-01-27 09:56:04 -0800346 }
bsalomond0423582015-02-06 08:49:24 -0800347
348 return NULL;
Brian Salomon9323b8b2014-10-07 15:07:38 -0400349}
350
bsalomon71cb0c22014-11-14 12:10:14 -0800351void GrContext::OverBudgetCB(void* data) {
bsalomon66a450f2014-11-13 13:19:10 -0800352 SkASSERT(data);
bsalomonf21dab92014-11-13 13:33:28 -0800353
bsalomon66a450f2014-11-13 13:19:10 -0800354 GrContext* context = reinterpret_cast<GrContext*>(data);
bsalomonf21dab92014-11-13 13:33:28 -0800355
356 // Flush the InOrderDrawBuffer to possibly free up some textures
357 context->fFlushToReduceCacheSize = true;
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000358}
359
joshualitt0db6dfa2015-04-10 07:01:30 -0700360void GrContext::TextBlobCacheOverBudgetCB(void* data) {
361 SkASSERT(data);
362
363 // Unlike the GrResourceCache, TextBlobs are drawn at the SkGpuDevice level, therefore they
364 // cannot use fFlushTorReduceCacheSize because it uses AutoCheckFlush. The solution is to move
365 // drawText calls to below the GrContext level, but this is not trivial because they call
366 // drawPath on SkGpuDevice
367 GrContext* context = reinterpret_cast<GrContext*>(data);
368 context->flush();
369}
370
bsalomon@google.com91958362011-06-13 17:58:13 +0000371int GrContext::getMaxTextureSize() const {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000372 return SkTMin(fGpu->caps()->maxTextureSize(), fMaxTextureSizeOverride);
bsalomon@google.com91958362011-06-13 17:58:13 +0000373}
374
375int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000376 return fGpu->caps()->maxRenderTargetSize();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000377}
378
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000379int GrContext::getMaxSampleCount() const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000380 return fGpu->caps()->maxSampleCount();
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000381}
382
bsalomon@google.com27847de2011-02-22 20:59:41 +0000383///////////////////////////////////////////////////////////////////////////////
384
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000385GrTexture* GrContext::wrapBackendTexture(const GrBackendTextureDesc& desc) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800386 RETURN_NULL_IF_ABANDONED
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000387 return fGpu->wrapBackendTexture(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000388}
389
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000390GrRenderTarget* GrContext::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800391 RETURN_NULL_IF_ABANDONED
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000392 return fGpu->wrapBackendRenderTarget(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000393}
394
bsalomon@google.com27847de2011-02-22 20:59:41 +0000395////////////////////////////////////////////////////////////////////////////////
396
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000397void GrContext::clear(const SkIRect* rect,
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000398 const GrColor color,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000399 bool canIgnoreRect,
bsalomon41ebbdd2014-08-04 08:31:39 -0700400 GrRenderTarget* renderTarget) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800401 RETURN_IF_ABANDONED
bsalomon41ebbdd2014-08-04 08:31:39 -0700402 ASSERT_OWNED_RESOURCE(renderTarget);
bsalomon89c62982014-11-03 12:08:42 -0800403 SkASSERT(renderTarget);
404
bsalomonf21dab92014-11-13 13:33:28 -0800405 AutoCheckFlush acf(this);
egdanield78a1682014-07-09 10:41:26 -0700406 GR_CREATE_TRACE_MARKER_CONTEXT("GrContext::clear", this);
joshualitt570d2f82015-02-25 13:19:48 -0800407 GrDrawTarget* target = this->prepareToDraw();
bsalomon41ebbdd2014-08-04 08:31:39 -0700408 if (NULL == target) {
409 return;
410 }
411 target->clear(rect, color, canIgnoreRect, renderTarget);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000412}
413
joshualitt25d9c152015-02-18 12:29:52 -0800414void GrContext::drawPaint(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800415 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800416 const GrPaint& origPaint,
417 const SkMatrix& viewMatrix) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800418 RETURN_IF_ABANDONED
bsalomon@google.com27847de2011-02-22 20:59:41 +0000419 // set rect to be big enough to fill the space, but not super-huge, so we
420 // don't overflow fixed-point implementations
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000421 SkRect r;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000422 r.setLTRB(0, 0,
joshualitt25d9c152015-02-18 12:29:52 -0800423 SkIntToScalar(rt->width()),
424 SkIntToScalar(rt->height()));
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000425 SkTCopyOnFirstWrite<GrPaint> paint(origPaint);
joshualitt8fc6c2d2014-12-22 15:27:05 -0800426
427 // by definition this fills the entire clip, no need for AA
428 if (paint->isAntiAlias()) {
429 paint.writable()->setAntiAlias(false);
430 }
431
432 bool isPerspective = viewMatrix.hasPerspective();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000433
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000434 // We attempt to map r by the inverse matrix and draw that. mapRect will
435 // map the four corners and bound them with a new rect. This will not
436 // produce a correct result for some perspective matrices.
joshualitt8fc6c2d2014-12-22 15:27:05 -0800437 if (!isPerspective) {
joshualitt5531d512014-12-17 15:50:11 -0800438 SkMatrix inverse;
439 if (!viewMatrix.invert(&inverse)) {
tfarina38406c82014-10-31 07:11:12 -0700440 SkDebugf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000441 return;
442 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000443 inverse.mapRect(&r);
joshualitt570d2f82015-02-25 13:19:48 -0800444 this->drawRect(rt, clip, *paint, viewMatrix, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000445 } else {
joshualitt8fc6c2d2014-12-22 15:27:05 -0800446 SkMatrix localMatrix;
447 if (!viewMatrix.invert(&localMatrix)) {
tfarina38406c82014-10-31 07:11:12 -0700448 SkDebugf("Could not invert matrix\n");
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000449 return;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000450 }
joshualitt8fc6c2d2014-12-22 15:27:05 -0800451
452 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -0800453 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -0800454 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, paint, &acf);
joshualitt8fc6c2d2014-12-22 15:27:05 -0800455 if (NULL == target) {
456 return;
457 }
458
459 GR_CREATE_TRACE_MARKER("GrContext::drawPaintWithPerspective", target);
joshualitt44701df2015-02-23 14:44:57 -0800460 target->drawRect(&pipelineBuilder,
461 paint->getColor(),
462 SkMatrix::I(),
463 r,
464 NULL,
465 &localMatrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000466 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000467}
468
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000469#ifdef SK_DEVELOPER
470void GrContext::dumpFontCache() const {
471 fFontCache->dump();
472}
473#endif
474
bsalomon@google.com205d4602011-04-25 12:43:45 +0000475////////////////////////////////////////////////////////////////////////////////
476
bsalomonc30aaa02014-08-13 07:15:29 -0700477static inline bool is_irect(const SkRect& r) {
tfarina38406c82014-10-31 07:11:12 -0700478 return SkScalarIsInt(r.fLeft) && SkScalarIsInt(r.fTop) &&
479 SkScalarIsInt(r.fRight) && SkScalarIsInt(r.fBottom);
bsalomonc30aaa02014-08-13 07:15:29 -0700480}
481
bsalomon@google.com205d4602011-04-25 12:43:45 +0000482static bool apply_aa_to_rect(GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -0800483 GrPipelineBuilder* pipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -0800484 SkRect* devBoundRect,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000485 const SkRect& rect,
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000486 SkScalar strokeWidth,
joshualitt2e3b3e32014-12-09 13:31:14 -0800487 const SkMatrix& combinedMatrix,
488 GrColor color) {
egdaniel8dd688b2015-01-22 10:16:09 -0800489 if (pipelineBuilder->getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000490 return false;
491 }
492
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000493#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000494 if (strokeWidth >= 0) {
495#endif
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000496 if (!combinedMatrix.preservesAxisAlignment()) {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000497 return false;
498 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000499
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000500#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000501 } else {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000502 if (!combinedMatrix.preservesRightAngles()) {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000503 return false;
504 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000505 }
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000506#endif
bsalomon@google.com205d4602011-04-25 12:43:45 +0000507
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000508 combinedMatrix.mapRect(devBoundRect, rect);
derekfe638d1c2014-12-02 13:51:29 -0800509 if (!combinedMatrix.rectStaysRect()) {
510 return true;
511 }
512
bsalomonc30aaa02014-08-13 07:15:29 -0700513 if (strokeWidth < 0) {
514 return !is_irect(*devBoundRect);
515 }
robertphillips@google.com28ac96e2013-05-13 13:38:35 +0000516
bsalomon9c0822a2014-08-11 11:07:48 -0700517 return true;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000518}
519
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000520static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) {
521 return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
522 point.fY >= rect.fTop && point.fY <= rect.fBottom;
523}
524
joshualitt0823bfa2015-02-27 10:06:15 -0800525class StrokeRectBatch : public GrBatch {
526public:
527 struct Geometry {
528 GrColor fColor;
529 SkMatrix fViewMatrix;
530 SkRect fRect;
531 SkScalar fStrokeWidth;
532 };
533
534 static GrBatch* Create(const Geometry& geometry) {
535 return SkNEW_ARGS(StrokeRectBatch, (geometry));
536 }
537
mtklein36352bf2015-03-25 18:17:31 -0700538 const char* name() const override { return "StrokeRectBatch"; }
joshualitt0823bfa2015-02-27 10:06:15 -0800539
mtklein36352bf2015-03-25 18:17:31 -0700540 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt0823bfa2015-02-27 10:06:15 -0800541 // When this is called on a batch, there is only one geometry bundle
542 out->setKnownFourComponents(fGeoData[0].fColor);
543 }
544
mtklein36352bf2015-03-25 18:17:31 -0700545 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualitt0823bfa2015-02-27 10:06:15 -0800546 out->setKnownSingleComponent(0xff);
547 }
548
mtklein36352bf2015-03-25 18:17:31 -0700549 void initBatchTracker(const GrPipelineInfo& init) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800550 // Handle any color overrides
551 if (init.fColorIgnored) {
552 fGeoData[0].fColor = GrColor_ILLEGAL;
553 } else if (GrColor_ILLEGAL != init.fOverrideColor) {
554 fGeoData[0].fColor = init.fOverrideColor;
555 }
556
557 // setup batch properties
558 fBatch.fColorIgnored = init.fColorIgnored;
559 fBatch.fColor = fGeoData[0].fColor;
560 fBatch.fUsesLocalCoords = init.fUsesLocalCoords;
561 fBatch.fCoverageIgnored = init.fCoverageIgnored;
562 }
563
mtklein36352bf2015-03-25 18:17:31 -0700564 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800565 SkAutoTUnref<const GrGeometryProcessor> gp(
566 GrDefaultGeoProcFactory::Create(GrDefaultGeoProcFactory::kPosition_GPType,
567 this->color(),
568 this->viewMatrix(),
569 SkMatrix::I()));
570
571 batchTarget->initDraw(gp, pipeline);
572
573 // TODO this is hacky, but the only way we have to initialize the GP is to use the
574 // GrPipelineInfo struct so we can generate the correct shader. Once we have GrBatch
575 // everywhere we can remove this nastiness
576 GrPipelineInfo init;
577 init.fColorIgnored = fBatch.fColorIgnored;
578 init.fOverrideColor = GrColor_ILLEGAL;
579 init.fCoverageIgnored = fBatch.fCoverageIgnored;
580 init.fUsesLocalCoords = this->usesLocalCoords();
581 gp->initBatchTracker(batchTarget->currentBatchTracker(), init);
582
583 size_t vertexStride = gp->getVertexStride();
584
585 SkASSERT(vertexStride == sizeof(GrDefaultGeoProcFactory::PositionAttr));
586
587 Geometry& args = fGeoData[0];
588
589 int vertexCount = kVertsPerHairlineRect;
590 if (args.fStrokeWidth > 0) {
591 vertexCount = kVertsPerStrokeRect;
592 }
593
594 const GrVertexBuffer* vertexBuffer;
595 int firstVertex;
596
597 void* vertices = batchTarget->vertexPool()->makeSpace(vertexStride,
598 vertexCount,
599 &vertexBuffer,
600 &firstVertex);
601
joshualitt4b31de82015-03-05 14:33:41 -0800602 if (!vertices) {
603 SkDebugf("Could not allocate vertices\n");
604 return;
605 }
606
joshualitt0823bfa2015-02-27 10:06:15 -0800607 SkPoint* vertex = reinterpret_cast<SkPoint*>(vertices);
608
609 GrPrimitiveType primType;
610
611 if (args.fStrokeWidth > 0) {;
612 primType = kTriangleStrip_GrPrimitiveType;
613 args.fRect.sort();
614 this->setStrokeRectStrip(vertex, args.fRect, args.fStrokeWidth);
615 } else {
616 // hairline
617 primType = kLineStrip_GrPrimitiveType;
618 vertex[0].set(args.fRect.fLeft, args.fRect.fTop);
619 vertex[1].set(args.fRect.fRight, args.fRect.fTop);
620 vertex[2].set(args.fRect.fRight, args.fRect.fBottom);
621 vertex[3].set(args.fRect.fLeft, args.fRect.fBottom);
622 vertex[4].set(args.fRect.fLeft, args.fRect.fTop);
623 }
624
625 GrDrawTarget::DrawInfo drawInfo;
626 drawInfo.setPrimitiveType(primType);
627 drawInfo.setVertexBuffer(vertexBuffer);
628 drawInfo.setStartVertex(firstVertex);
629 drawInfo.setVertexCount(vertexCount);
630 drawInfo.setStartIndex(0);
631 drawInfo.setIndexCount(0);
632 drawInfo.setInstanceCount(0);
633 drawInfo.setVerticesPerInstance(0);
634 drawInfo.setIndicesPerInstance(0);
635 batchTarget->draw(drawInfo);
636 }
637
638 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
639
640private:
641 StrokeRectBatch(const Geometry& geometry) {
642 this->initClassID<StrokeRectBatch>();
643
644 fBatch.fHairline = geometry.fStrokeWidth == 0;
645
646 fGeoData.push_back(geometry);
647 }
648
649 /* create a triangle strip that strokes the specified rect. There are 8
650 unique vertices, but we repeat the last 2 to close up. Alternatively we
651 could use an indices array, and then only send 8 verts, but not sure that
652 would be faster.
653 */
654 void setStrokeRectStrip(SkPoint verts[10], const SkRect& rect, SkScalar width) {
655 const SkScalar rad = SkScalarHalf(width);
656 // TODO we should be able to enable this assert, but we'd have to filter these draws
657 // this is a bug
658 //SkASSERT(rad < rect.width() / 2 && rad < rect.height() / 2);
659
660 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
661 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
662 verts[2].set(rect.fRight - rad, rect.fTop + rad);
663 verts[3].set(rect.fRight + rad, rect.fTop - rad);
664 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
665 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
666 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
667 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
668 verts[8] = verts[0];
669 verts[9] = verts[1];
670 }
671
672
673 GrColor color() const { return fBatch.fColor; }
674 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
675 bool colorIgnored() const { return fBatch.fColorIgnored; }
676 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
677 bool hairline() const { return fBatch.fHairline; }
678
mtklein36352bf2015-03-25 18:17:31 -0700679 bool onCombineIfPossible(GrBatch* t) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800680 // StrokeRectBatch* that = t->cast<StrokeRectBatch>();
681
682 // NonAA stroke rects cannot batch right now
683 // TODO make these batchable
684 return false;
685 }
686
687 struct BatchTracker {
688 GrColor fColor;
689 bool fUsesLocalCoords;
690 bool fColorIgnored;
691 bool fCoverageIgnored;
692 bool fHairline;
693 };
694
695 const static int kVertsPerHairlineRect = 5;
696 const static int kVertsPerStrokeRect = 10;
697
698 BatchTracker fBatch;
699 SkSTArray<1, Geometry, true> fGeoData;
700};
701
joshualitt25d9c152015-02-18 12:29:52 -0800702void GrContext::drawRect(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800703 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800704 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -0800705 const SkMatrix& viewMatrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000706 const SkRect& rect,
bsalomon01c8da12014-08-04 09:21:30 -0700707 const GrStrokeInfo* strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800708 RETURN_IF_ABANDONED
bsalomon49f085d2014-09-05 13:34:00 -0700709 if (strokeInfo && strokeInfo->isDashed()) {
egdanield58a0ba2014-06-11 10:30:05 -0700710 SkPath path;
711 path.addRect(rect);
joshualitt570d2f82015-02-25 13:19:48 -0800712 this->drawPath(rt, clip, paint, viewMatrix, path, *strokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -0700713 return;
714 }
715
bsalomonf21dab92014-11-13 13:33:28 -0800716 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -0800717 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -0800718 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700719 if (NULL == target) {
720 return;
721 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000722
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000723 GR_CREATE_TRACE_MARKER("GrContext::drawRect", target);
egdanield58a0ba2014-06-11 10:30:05 -0700724 SkScalar width = NULL == strokeInfo ? -1 : strokeInfo->getStrokeRec().getWidth();
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000725
726 // Check if this is a full RT draw and can be replaced with a clear. We don't bother checking
727 // cases where the RT is fully inside a stroke.
728 if (width < 0) {
729 SkRect rtRect;
egdaniel8dd688b2015-01-22 10:16:09 -0800730 pipelineBuilder.getRenderTarget()->getBoundsRect(&rtRect);
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000731 SkRect clipSpaceRTRect = rtRect;
joshualitt570d2f82015-02-25 13:19:48 -0800732 bool checkClip = GrClip::kWideOpen_ClipType != clip.clipType();
joshualitt44701df2015-02-23 14:44:57 -0800733 if (checkClip) {
joshualitt570d2f82015-02-25 13:19:48 -0800734 clipSpaceRTRect.offset(SkIntToScalar(clip.origin().fX),
735 SkIntToScalar(clip.origin().fY));
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000736 }
737 // Does the clip contain the entire RT?
joshualitt570d2f82015-02-25 13:19:48 -0800738 if (!checkClip || clip.quickContains(clipSpaceRTRect)) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000739 SkMatrix invM;
joshualitt8059eb92014-12-29 15:10:07 -0800740 if (!viewMatrix.invert(&invM)) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000741 return;
742 }
743 // Does the rect bound the RT?
744 SkPoint srcSpaceRTQuad[4];
745 invM.mapRectToQuad(srcSpaceRTQuad, rtRect);
746 if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) &&
747 rect_contains_inclusive(rect, srcSpaceRTQuad[1]) &&
748 rect_contains_inclusive(rect, srcSpaceRTQuad[2]) &&
749 rect_contains_inclusive(rect, srcSpaceRTQuad[3])) {
750 // Will it blend?
751 GrColor clearColor;
752 if (paint.isOpaqueAndConstantColor(&clearColor)) {
joshualitt25d9c152015-02-18 12:29:52 -0800753 target->clear(NULL, clearColor, true, rt);
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000754 return;
755 }
756 }
757 }
758 }
759
joshualitt2e3b3e32014-12-09 13:31:14 -0800760 GrColor color = paint.getColor();
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000761 SkRect devBoundRect;
egdaniel8dd688b2015-01-22 10:16:09 -0800762 bool needAA = paint.isAntiAlias() && !pipelineBuilder.getRenderTarget()->isMultisampled();
763 bool doAA = needAA && apply_aa_to_rect(target, &pipelineBuilder, &devBoundRect, rect, width,
joshualitt8059eb92014-12-29 15:10:07 -0800764 viewMatrix, color);
egdanield58a0ba2014-06-11 10:30:05 -0700765
bsalomon@google.com205d4602011-04-25 12:43:45 +0000766 if (doAA) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000767 if (width >= 0) {
bsalomon395ef052014-11-12 11:35:22 -0800768 const SkStrokeRec& strokeRec = strokeInfo->getStrokeRec();
joshualitt9853cce2014-11-17 14:22:48 -0800769 fAARectRenderer->strokeAARect(target,
egdaniel8dd688b2015-01-22 10:16:09 -0800770 &pipelineBuilder,
joshualitt2e3b3e32014-12-09 13:31:14 -0800771 color,
joshualitt8059eb92014-12-29 15:10:07 -0800772 viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800773 rect,
joshualitt9853cce2014-11-17 14:22:48 -0800774 devBoundRect,
775 strokeRec);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000776 } else {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000777 // filled AA rect
joshualitt8059eb92014-12-29 15:10:07 -0800778 fAARectRenderer->fillAARect(target,
egdaniel8dd688b2015-01-22 10:16:09 -0800779 &pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800780 color,
781 viewMatrix,
782 rect,
joshualittd27f73e2014-12-29 07:43:36 -0800783 devBoundRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000784 }
785 return;
786 }
787
bsalomon@google.com27847de2011-02-22 20:59:41 +0000788 if (width >= 0) {
joshualitt0823bfa2015-02-27 10:06:15 -0800789 StrokeRectBatch::Geometry geometry;
790 geometry.fViewMatrix = viewMatrix;
791 geometry.fColor = color;
792 geometry.fRect = rect;
793 geometry.fStrokeWidth = width;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000794
joshualitt0823bfa2015-02-27 10:06:15 -0800795 SkAutoTUnref<GrBatch> batch(StrokeRectBatch::Create(geometry));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000796
joshualitt0823bfa2015-02-27 10:06:15 -0800797 SkRect bounds = rect;
798 SkScalar rad = SkScalarHalf(width);
799 bounds.outset(rad, rad);
800 viewMatrix.mapRect(&bounds);
801 target->drawBatch(&pipelineBuilder, batch, &bounds);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000802 } else {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000803 // filled BW rect
egdaniel8dd688b2015-01-22 10:16:09 -0800804 target->drawSimpleRect(&pipelineBuilder, color, viewMatrix, rect);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000805 }
806}
807
joshualitt25d9c152015-02-18 12:29:52 -0800808void GrContext::drawNonAARectToRect(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -0800809 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -0800810 const GrPaint& paint,
joshualitt16b27892014-12-18 07:47:16 -0800811 const SkMatrix& viewMatrix,
812 const SkRect& rectToDraw,
813 const SkRect& localRect,
814 const SkMatrix* localMatrix) {
joshualitt5f5a8d72015-02-25 14:09:45 -0800815 RETURN_IF_ABANDONED
bsalomonf21dab92014-11-13 13:33:28 -0800816 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -0800817 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -0800818 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700819 if (NULL == target) {
820 return;
821 }
bsalomon@google.com64386952013-02-08 21:22:44 +0000822
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000823 GR_CREATE_TRACE_MARKER("GrContext::drawRectToRect", target);
824
joshualitt44701df2015-02-23 14:44:57 -0800825 target->drawRect(&pipelineBuilder,
826 paint.getColor(),
827 viewMatrix,
828 rectToDraw,
829 &localRect,
egdaniel8dd688b2015-01-22 10:16:09 -0800830 localMatrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000831}
832
joshualitt0823bfa2015-02-27 10:06:15 -0800833static const GrGeometryProcessor* set_vertex_attributes(bool hasLocalCoords,
834 bool hasColors,
joshualitt56995b52014-12-11 15:44:02 -0800835 int* colorOffset,
836 int* texOffset,
joshualitt8059eb92014-12-29 15:10:07 -0800837 GrColor color,
838 const SkMatrix& viewMatrix) {
robertphillips@google.com42903302013-04-20 12:26:07 +0000839 *texOffset = -1;
840 *colorOffset = -1;
joshualitt5478d422014-11-14 16:00:38 -0800841 uint32_t flags = GrDefaultGeoProcFactory::kPosition_GPType;
joshualitt0823bfa2015-02-27 10:06:15 -0800842 if (hasLocalCoords && hasColors) {
joshualitt5478d422014-11-14 16:00:38 -0800843 *colorOffset = sizeof(SkPoint);
844 *texOffset = sizeof(SkPoint) + sizeof(GrColor);
845 flags |= GrDefaultGeoProcFactory::kColor_GPType |
846 GrDefaultGeoProcFactory::kLocalCoord_GPType;
joshualitt0823bfa2015-02-27 10:06:15 -0800847 } else if (hasLocalCoords) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000848 *texOffset = sizeof(SkPoint);
joshualitt5478d422014-11-14 16:00:38 -0800849 flags |= GrDefaultGeoProcFactory::kLocalCoord_GPType;
joshualitt0823bfa2015-02-27 10:06:15 -0800850 } else if (hasColors) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000851 *colorOffset = sizeof(SkPoint);
joshualitt5478d422014-11-14 16:00:38 -0800852 flags |= GrDefaultGeoProcFactory::kColor_GPType;
robertphillips@google.com42903302013-04-20 12:26:07 +0000853 }
joshualitt8059eb92014-12-29 15:10:07 -0800854 return GrDefaultGeoProcFactory::Create(flags, color, viewMatrix, SkMatrix::I());
robertphillips@google.com42903302013-04-20 12:26:07 +0000855}
856
joshualitt0823bfa2015-02-27 10:06:15 -0800857class DrawVerticesBatch : public GrBatch {
858public:
859 struct Geometry {
860 GrColor fColor;
861 SkTDArray<SkPoint> fPositions;
862 SkTDArray<uint16_t> fIndices;
863 SkTDArray<GrColor> fColors;
864 SkTDArray<SkPoint> fLocalCoords;
865 };
866
867 static GrBatch* Create(const Geometry& geometry, GrPrimitiveType primitiveType,
868 const SkMatrix& viewMatrix,
869 const SkPoint* positions, int vertexCount,
870 const uint16_t* indices, int indexCount,
871 const GrColor* colors, const SkPoint* localCoords) {
872 return SkNEW_ARGS(DrawVerticesBatch, (geometry, primitiveType, viewMatrix, positions,
873 vertexCount, indices, indexCount, colors,
874 localCoords));
875 }
876
mtklein36352bf2015-03-25 18:17:31 -0700877 const char* name() const override { return "DrawVerticesBatch"; }
joshualitt0823bfa2015-02-27 10:06:15 -0800878
mtklein36352bf2015-03-25 18:17:31 -0700879 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt0823bfa2015-02-27 10:06:15 -0800880 // When this is called on a batch, there is only one geometry bundle
881 if (this->hasColors()) {
882 out->setUnknownFourComponents();
883 } else {
884 out->setKnownFourComponents(fGeoData[0].fColor);
885 }
886 }
887
mtklein36352bf2015-03-25 18:17:31 -0700888 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualittd4c7d552015-02-27 13:00:56 -0800889 out->setKnownSingleComponent(0xff);
joshualitt0823bfa2015-02-27 10:06:15 -0800890 }
891
mtklein36352bf2015-03-25 18:17:31 -0700892 void initBatchTracker(const GrPipelineInfo& init) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800893 // Handle any color overrides
894 if (init.fColorIgnored) {
895 fGeoData[0].fColor = GrColor_ILLEGAL;
896 } else if (GrColor_ILLEGAL != init.fOverrideColor) {
897 fGeoData[0].fColor = init.fOverrideColor;
898 }
899
900 // setup batch properties
901 fBatch.fColorIgnored = init.fColorIgnored;
902 fBatch.fColor = fGeoData[0].fColor;
903 fBatch.fUsesLocalCoords = init.fUsesLocalCoords;
904 fBatch.fCoverageIgnored = init.fCoverageIgnored;
905 }
906
mtklein36352bf2015-03-25 18:17:31 -0700907 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
joshualitt0823bfa2015-02-27 10:06:15 -0800908 int colorOffset = -1, texOffset = -1;
909 SkAutoTUnref<const GrGeometryProcessor> gp(
910 set_vertex_attributes(this->hasLocalCoords(), this->hasColors(), &colorOffset,
911 &texOffset, this->color(), this->viewMatrix()));
912
913 batchTarget->initDraw(gp, pipeline);
914
915 // TODO this is hacky, but the only way we have to initialize the GP is to use the
916 // GrPipelineInfo struct so we can generate the correct shader. Once we have GrBatch
917 // everywhere we can remove this nastiness
918 GrPipelineInfo init;
919 init.fColorIgnored = fBatch.fColorIgnored;
920 init.fOverrideColor = GrColor_ILLEGAL;
921 init.fCoverageIgnored = fBatch.fCoverageIgnored;
922 init.fUsesLocalCoords = this->usesLocalCoords();
923 gp->initBatchTracker(batchTarget->currentBatchTracker(), init);
924
925 size_t vertexStride = gp->getVertexStride();
926
927 SkASSERT(vertexStride == sizeof(SkPoint) + (this->hasLocalCoords() ? sizeof(SkPoint) : 0)
928 + (this->hasColors() ? sizeof(GrColor) : 0));
929
930 int instanceCount = fGeoData.count();
931
932 const GrVertexBuffer* vertexBuffer;
933 int firstVertex;
934
935 void* vertices = batchTarget->vertexPool()->makeSpace(vertexStride,
936 this->vertexCount(),
937 &vertexBuffer,
938 &firstVertex);
939
joshualitt4b31de82015-03-05 14:33:41 -0800940 if (!vertices) {
941 SkDebugf("Could not allocate vertices\n");
942 return;
943 }
944
joshualitt0823bfa2015-02-27 10:06:15 -0800945 const GrIndexBuffer* indexBuffer;
946 int firstIndex;
947
948 void* indices = NULL;
949 if (this->hasIndices()) {
950 indices = batchTarget->indexPool()->makeSpace(this->indexCount(),
951 &indexBuffer,
952 &firstIndex);
joshualitt4b31de82015-03-05 14:33:41 -0800953
954 if (!indices) {
955 SkDebugf("Could not allocate indices\n");
956 return;
957 }
joshualitt0823bfa2015-02-27 10:06:15 -0800958 }
959
960 int indexOffset = 0;
961 int vertexOffset = 0;
962 for (int i = 0; i < instanceCount; i++) {
963 const Geometry& args = fGeoData[i];
964
965 // TODO we can actually cache this interleaved and then just memcopy
966 if (this->hasIndices()) {
967 for (int j = 0; j < args.fIndices.count(); ++j, ++indexOffset) {
968 *((uint16_t*)indices + indexOffset) = args.fIndices[j] + vertexOffset;
969 }
970 }
971
972 for (int j = 0; j < args.fPositions.count(); ++j) {
973 *((SkPoint*)vertices) = args.fPositions[j];
974 if (this->hasColors()) {
975 *(GrColor*)((intptr_t)vertices + colorOffset) = args.fColors[j];
976 }
977 if (this->hasLocalCoords()) {
978 *(SkPoint*)((intptr_t)vertices + texOffset) = args.fLocalCoords[j];
979 }
980 vertices = (void*)((intptr_t)vertices + vertexStride);
981 vertexOffset++;
982 }
983 }
984
985 GrDrawTarget::DrawInfo drawInfo;
986 drawInfo.setPrimitiveType(this->primitiveType());
987 drawInfo.setVertexBuffer(vertexBuffer);
988 drawInfo.setStartVertex(firstVertex);
989 drawInfo.setVertexCount(this->vertexCount());
990 if (this->hasIndices()) {
991 drawInfo.setIndexBuffer(indexBuffer);
992 drawInfo.setStartIndex(firstIndex);
993 drawInfo.setIndexCount(this->indexCount());
994 } else {
995 drawInfo.setStartIndex(0);
996 drawInfo.setIndexCount(0);
997 }
998 batchTarget->draw(drawInfo);
999 }
1000
1001 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
1002
1003private:
1004 DrawVerticesBatch(const Geometry& geometry, GrPrimitiveType primitiveType,
1005 const SkMatrix& viewMatrix,
1006 const SkPoint* positions, int vertexCount,
1007 const uint16_t* indices, int indexCount,
1008 const GrColor* colors, const SkPoint* localCoords) {
1009 this->initClassID<DrawVerticesBatch>();
1010 SkASSERT(positions);
1011
1012 fBatch.fViewMatrix = viewMatrix;
1013 Geometry& installedGeo = fGeoData.push_back(geometry);
1014
1015 installedGeo.fPositions.append(vertexCount, positions);
1016 if (indices) {
1017 installedGeo.fIndices.append(indexCount, indices);
1018 fBatch.fHasIndices = true;
1019 } else {
1020 fBatch.fHasIndices = false;
1021 }
1022
1023 if (colors) {
1024 installedGeo.fColors.append(vertexCount, colors);
1025 fBatch.fHasColors = true;
1026 } else {
1027 fBatch.fHasColors = false;
1028 }
1029
1030 if (localCoords) {
1031 installedGeo.fLocalCoords.append(vertexCount, localCoords);
1032 fBatch.fHasLocalCoords = true;
1033 } else {
1034 fBatch.fHasLocalCoords = false;
1035 }
1036 fBatch.fVertexCount = vertexCount;
1037 fBatch.fIndexCount = indexCount;
1038 fBatch.fPrimitiveType = primitiveType;
1039 }
1040
1041 GrPrimitiveType primitiveType() const { return fBatch.fPrimitiveType; }
1042 bool batchablePrimitiveType() const {
1043 return kTriangles_GrPrimitiveType == fBatch.fPrimitiveType ||
1044 kLines_GrPrimitiveType == fBatch.fPrimitiveType ||
1045 kPoints_GrPrimitiveType == fBatch.fPrimitiveType;
1046 }
1047 GrColor color() const { return fBatch.fColor; }
1048 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
1049 bool colorIgnored() const { return fBatch.fColorIgnored; }
1050 const SkMatrix& viewMatrix() const { return fBatch.fViewMatrix; }
1051 bool hasColors() const { return fBatch.fHasColors; }
1052 bool hasIndices() const { return fBatch.fHasIndices; }
1053 bool hasLocalCoords() const { return fBatch.fHasLocalCoords; }
1054 int vertexCount() const { return fBatch.fVertexCount; }
1055 int indexCount() const { return fBatch.fIndexCount; }
1056
mtklein36352bf2015-03-25 18:17:31 -07001057 bool onCombineIfPossible(GrBatch* t) override {
joshualitt0823bfa2015-02-27 10:06:15 -08001058 DrawVerticesBatch* that = t->cast<DrawVerticesBatch>();
1059
1060 if (!this->batchablePrimitiveType() || this->primitiveType() != that->primitiveType()) {
1061 return false;
1062 }
1063
1064 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
1065
1066 // We currently use a uniform viewmatrix for this batch
1067 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
1068 return false;
1069 }
1070
1071 if (this->hasColors() != that->hasColors()) {
1072 return false;
1073 }
1074
1075 if (this->hasIndices() != that->hasIndices()) {
1076 return false;
1077 }
1078
1079 if (this->hasLocalCoords() != that->hasLocalCoords()) {
1080 return false;
1081 }
1082
1083 if (!this->hasColors() && this->color() != that->color()) {
1084 return false;
1085 }
1086
1087 if (this->color() != that->color()) {
1088 fBatch.fColor = GrColor_ILLEGAL;
1089 }
1090 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
1091 fBatch.fVertexCount += that->vertexCount();
1092 fBatch.fIndexCount += that->indexCount();
1093 return true;
1094 }
1095
1096 struct BatchTracker {
1097 GrPrimitiveType fPrimitiveType;
1098 SkMatrix fViewMatrix;
1099 GrColor fColor;
1100 bool fUsesLocalCoords;
1101 bool fColorIgnored;
1102 bool fCoverageIgnored;
1103 bool fHasColors;
1104 bool fHasIndices;
1105 bool fHasLocalCoords;
1106 int fVertexCount;
1107 int fIndexCount;
1108 };
1109
1110 BatchTracker fBatch;
1111 SkSTArray<1, Geometry, true> fGeoData;
1112};
1113
joshualitt25d9c152015-02-18 12:29:52 -08001114void GrContext::drawVertices(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -08001115 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001116 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001117 const SkMatrix& viewMatrix,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001118 GrPrimitiveType primitiveType,
1119 int vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001120 const SkPoint positions[],
1121 const SkPoint texCoords[],
bsalomon@google.com27847de2011-02-22 20:59:41 +00001122 const GrColor colors[],
1123 const uint16_t indices[],
1124 int indexCount) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001125 RETURN_IF_ABANDONED
bsalomonf21dab92014-11-13 13:33:28 -08001126 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001127 GrPipelineBuilder pipelineBuilder;
bsalomonf21dab92014-11-13 13:33:28 -08001128 GrDrawTarget::AutoReleaseGeometry geo; // must be inside AutoCheckFlush scope
commit-bot@chromium.org5a567932014-01-08 21:26:09 +00001129
joshualitt570d2f82015-02-25 13:19:48 -08001130 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001131 if (NULL == target) {
1132 return;
1133 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001134
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001135 GR_CREATE_TRACE_MARKER("GrContext::drawVertices", target);
1136
joshualitt0823bfa2015-02-27 10:06:15 -08001137 DrawVerticesBatch::Geometry geometry;
1138 geometry.fColor = paint.getColor();
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001139
joshualitt0823bfa2015-02-27 10:06:15 -08001140 SkAutoTUnref<GrBatch> batch(DrawVerticesBatch::Create(geometry, primitiveType, viewMatrix,
1141 positions, vertexCount, indices,
1142 indexCount,colors, texCoords));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001143
joshualitt0823bfa2015-02-27 10:06:15 -08001144 // TODO figure out bounds
1145 target->drawBatch(&pipelineBuilder, batch, NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001146}
1147
bsalomon@google.com06afe7b2011-04-26 15:31:40 +00001148///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com93c96602012-04-27 13:05:21 +00001149
joshualitt25d9c152015-02-18 12:29:52 -08001150void GrContext::drawRRect(GrRenderTarget*rt,
joshualitt570d2f82015-02-25 13:19:48 -08001151 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001152 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001153 const SkMatrix& viewMatrix,
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001154 const SkRRect& rrect,
egdanield58a0ba2014-06-11 10:30:05 -07001155 const GrStrokeInfo& strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001156 RETURN_IF_ABANDONED
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001157 if (rrect.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001158 return;
1159 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001160
egdanield58a0ba2014-06-11 10:30:05 -07001161 if (strokeInfo.isDashed()) {
1162 SkPath path;
1163 path.addRRect(rrect);
joshualitt570d2f82015-02-25 13:19:48 -08001164 this->drawPath(rt, clip, paint, viewMatrix, path, strokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -07001165 return;
1166 }
1167
bsalomonf21dab92014-11-13 13:33:28 -08001168 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001169 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -08001170 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001171 if (NULL == target) {
1172 return;
1173 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001174
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001175 GR_CREATE_TRACE_MARKER("GrContext::drawRRect", target);
1176
egdanield58a0ba2014-06-11 10:30:05 -07001177 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1178
joshualitt2e3b3e32014-12-09 13:31:14 -08001179 GrColor color = paint.getColor();
joshualitt44701df2015-02-23 14:44:57 -08001180 if (!fOvalRenderer->drawRRect(target,
1181 &pipelineBuilder,
1182 color,
1183 viewMatrix,
1184 paint.isAntiAlias(),
1185 rrect,
1186 strokeRec)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001187 SkPath path;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001188 path.addRRect(rrect);
egdaniel8dd688b2015-01-22 10:16:09 -08001189 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(),
1190 path, strokeInfo);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001191 }
1192}
1193
1194///////////////////////////////////////////////////////////////////////////////
1195
joshualitt25d9c152015-02-18 12:29:52 -08001196void GrContext::drawDRRect(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -08001197 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001198 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001199 const SkMatrix& viewMatrix,
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001200 const SkRRect& outer,
1201 const SkRRect& inner) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001202 RETURN_IF_ABANDONED
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001203 if (outer.isEmpty()) {
1204 return;
1205 }
1206
bsalomonf21dab92014-11-13 13:33:28 -08001207 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001208 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -08001209 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001210
1211 GR_CREATE_TRACE_MARKER("GrContext::drawDRRect", target);
1212
joshualitt2e3b3e32014-12-09 13:31:14 -08001213 GrColor color = paint.getColor();
joshualitt44701df2015-02-23 14:44:57 -08001214 if (!fOvalRenderer->drawDRRect(target,
1215 &pipelineBuilder,
1216 color,
1217 viewMatrix,
1218 paint.isAntiAlias(),
1219 outer,
1220 inner)) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001221 SkPath path;
1222 path.addRRect(inner);
1223 path.addRRect(outer);
1224 path.setFillType(SkPath::kEvenOdd_FillType);
1225
egdanield58a0ba2014-06-11 10:30:05 -07001226 GrStrokeInfo fillRec(SkStrokeRec::kFill_InitStyle);
egdaniel8dd688b2015-01-22 10:16:09 -08001227 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(),
1228 path, fillRec);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001229 }
1230}
1231
1232///////////////////////////////////////////////////////////////////////////////
1233
joshualitt570d2f82015-02-25 13:19:48 -08001234void GrContext::drawOval(GrRenderTarget* rt,
1235 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001236 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001237 const SkMatrix& viewMatrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001238 const SkRect& oval,
egdanield58a0ba2014-06-11 10:30:05 -07001239 const GrStrokeInfo& strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001240 RETURN_IF_ABANDONED
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001241 if (oval.isEmpty()) {
1242 return;
1243 }
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001244
egdanield58a0ba2014-06-11 10:30:05 -07001245 if (strokeInfo.isDashed()) {
1246 SkPath path;
1247 path.addOval(oval);
joshualitt570d2f82015-02-25 13:19:48 -08001248 this->drawPath(rt, clip, paint, viewMatrix, path, strokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -07001249 return;
1250 }
1251
bsalomonf21dab92014-11-13 13:33:28 -08001252 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001253 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -08001254 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001255 if (NULL == target) {
1256 return;
1257 }
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001258
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001259 GR_CREATE_TRACE_MARKER("GrContext::drawOval", target);
1260
egdanield58a0ba2014-06-11 10:30:05 -07001261 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1262
joshualitt2e3b3e32014-12-09 13:31:14 -08001263 GrColor color = paint.getColor();
joshualitt44701df2015-02-23 14:44:57 -08001264 if (!fOvalRenderer->drawOval(target,
1265 &pipelineBuilder,
1266 color,
1267 viewMatrix,
1268 paint.isAntiAlias(),
1269 oval,
1270 strokeRec)) {
bsalomon@google.com93c96602012-04-27 13:05:21 +00001271 SkPath path;
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001272 path.addOval(oval);
egdaniel8dd688b2015-01-22 10:16:09 -08001273 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(),
1274 path, strokeInfo);
skia.committer@gmail.com98ded842013-01-23 07:06:17 +00001275 }
bsalomon@google.com150d2842012-01-12 20:19:56 +00001276}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001277
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001278// Can 'path' be drawn as a pair of filled nested rectangles?
1279static bool is_nested_rects(GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -08001280 GrPipelineBuilder* pipelineBuilder,
joshualitt2e3b3e32014-12-09 13:31:14 -08001281 GrColor color,
joshualitt8059eb92014-12-29 15:10:07 -08001282 const SkMatrix& viewMatrix,
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001283 const SkPath& path,
1284 const SkStrokeRec& stroke,
bsalomon9c0822a2014-08-11 11:07:48 -07001285 SkRect rects[2]) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001286 SkASSERT(stroke.isFillStyle());
1287
1288 if (path.isInverseFillType()) {
1289 return false;
1290 }
1291
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001292 // TODO: this restriction could be lifted if we were willing to apply
1293 // the matrix to all the points individually rather than just to the rect
joshualitt8059eb92014-12-29 15:10:07 -08001294 if (!viewMatrix.preservesAxisAlignment()) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001295 return false;
1296 }
1297
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001298 SkPath::Direction dirs[2];
caryclark95bc5f32015-04-08 08:34:15 -07001299 if (!path.isNestedFillRects(rects, dirs)) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001300 return false;
1301 }
1302
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001303 if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001304 // The two rects need to be wound opposite to each other
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001305 return false;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001306 }
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001307
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001308 // Right now, nested rects where the margin is not the same width
1309 // all around do not render correctly
1310 const SkScalar* outer = rects[0].asScalars();
1311 const SkScalar* inner = rects[1].asScalars();
1312
robertphillips183e9852014-10-21 11:25:37 -07001313 bool allEq = true;
1314
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001315 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
robertphillips183e9852014-10-21 11:25:37 -07001316 bool allGoE1 = margin >= SK_Scalar1;
1317
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001318 for (int i = 1; i < 4; ++i) {
1319 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
robertphillips183e9852014-10-21 11:25:37 -07001320 if (temp < SK_Scalar1) {
1321 allGoE1 = false;
1322 }
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001323 if (!SkScalarNearlyEqual(margin, temp)) {
robertphillips183e9852014-10-21 11:25:37 -07001324 allEq = false;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001325 }
1326 }
1327
robertphillips183e9852014-10-21 11:25:37 -07001328 return allEq || allGoE1;
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001329}
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001330
joshualitt25d9c152015-02-18 12:29:52 -08001331void GrContext::drawPath(GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -08001332 const GrClip& clip,
joshualitt25d9c152015-02-18 12:29:52 -08001333 const GrPaint& paint,
joshualitt5531d512014-12-17 15:50:11 -08001334 const SkMatrix& viewMatrix,
1335 const SkPath& path,
1336 const GrStrokeInfo& strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001337 RETURN_IF_ABANDONED
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001338 if (path.isEmpty()) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001339 if (path.isInverseFillType()) {
joshualitt570d2f82015-02-25 13:19:48 -08001340 this->drawPaint(rt, clip, paint, viewMatrix);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001341 }
1342 return;
1343 }
1344
joshualitt2e3b3e32014-12-09 13:31:14 -08001345 GrColor color = paint.getColor();
egdanield58a0ba2014-06-11 10:30:05 -07001346 if (strokeInfo.isDashed()) {
egdaniele61c4112014-06-12 10:24:21 -07001347 SkPoint pts[2];
1348 if (path.isLine(pts)) {
bsalomonf21dab92014-11-13 13:33:28 -08001349 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001350 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -08001351 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001352 if (NULL == target) {
1353 return;
joshualittd27f73e2014-12-29 07:43:36 -08001354 }
egdaniele61c4112014-06-12 10:24:21 -07001355
egdaniel8dd688b2015-01-22 10:16:09 -08001356 if (GrDashingEffect::DrawDashLine(fGpu, target, &pipelineBuilder, color, viewMatrix,
1357 pts, paint, strokeInfo)) {
joshualittd27f73e2014-12-29 07:43:36 -08001358 return;
egdaniele61c4112014-06-12 10:24:21 -07001359 }
1360 }
1361
1362 // Filter dashed path into new path with the dashing applied
egdanield58a0ba2014-06-11 10:30:05 -07001363 const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo();
1364 SkTLazy<SkPath> effectPath;
1365 GrStrokeInfo newStrokeInfo(strokeInfo, false);
1366 SkStrokeRec* stroke = newStrokeInfo.getStrokeRecPtr();
1367 if (SkDashPath::FilterDashPath(effectPath.init(), path, stroke, NULL, info)) {
joshualitt570d2f82015-02-25 13:19:48 -08001368 this->drawPath(rt, clip, paint, viewMatrix, *effectPath.get(), newStrokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -07001369 return;
1370 }
1371
joshualitt570d2f82015-02-25 13:19:48 -08001372 this->drawPath(rt, clip, paint, viewMatrix, path, newStrokeInfo);
egdanield58a0ba2014-06-11 10:30:05 -07001373 return;
1374 }
1375
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001376 // Note that internalDrawPath may sw-rasterize the path into a scratch texture.
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001377 // Scratch textures can be recycled after they are returned to the texture
1378 // cache. This presents a potential hazard for buffered drawing. However,
1379 // the writePixels that uploads to the scratch will perform a flush so we're
1380 // OK.
bsalomonf21dab92014-11-13 13:33:28 -08001381 AutoCheckFlush acf(this);
egdaniel8dd688b2015-01-22 10:16:09 -08001382 GrPipelineBuilder pipelineBuilder;
joshualitt570d2f82015-02-25 13:19:48 -08001383 GrDrawTarget* target = this->prepareToDraw(&pipelineBuilder, rt, clip, &paint, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001384 if (NULL == target) {
1385 return;
1386 }
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001387
egdaniel93a37bc2014-07-21 13:47:57 -07001388 GR_CREATE_TRACE_MARKER1("GrContext::drawPath", target, "Is Convex", path.isConvex());
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001389
egdanield58a0ba2014-06-11 10:30:05 -07001390 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1391
egdaniel8dd688b2015-01-22 10:16:09 -08001392 bool useCoverageAA = paint.isAntiAlias() &&
1393 !pipelineBuilder.getRenderTarget()->isMultisampled();
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001394
egdanield58a0ba2014-06-11 10:30:05 -07001395 if (useCoverageAA && strokeRec.getWidth() < 0 && !path.isConvex()) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001396 // Concave AA paths are expensive - try to avoid them for special cases
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001397 SkRect rects[2];
1398
egdaniel8dd688b2015-01-22 10:16:09 -08001399 if (is_nested_rects(target, &pipelineBuilder, color, viewMatrix, path, strokeRec, rects)) {
joshualitt44701df2015-02-23 14:44:57 -08001400 fAARectRenderer->fillAANestedRects(target, &pipelineBuilder, color, viewMatrix, rects);
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001401 return;
1402 }
1403 }
1404
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001405 SkRect ovalRect;
1406 bool isOval = path.isOval(&ovalRect);
1407
joshualitt8059eb92014-12-29 15:10:07 -08001408 if (!isOval || path.isInverseFillType() ||
joshualitt44701df2015-02-23 14:44:57 -08001409 !fOvalRenderer->drawOval(target,
1410 &pipelineBuilder,
1411 color,
1412 viewMatrix,
1413 paint.isAntiAlias(),
1414 ovalRect,
1415 strokeRec)) {
egdaniel8dd688b2015-01-22 10:16:09 -08001416 this->internalDrawPath(target, &pipelineBuilder, viewMatrix, color, paint.isAntiAlias(),
1417 path, strokeInfo);
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001418 }
1419}
1420
joshualitt9853cce2014-11-17 14:22:48 -08001421void GrContext::internalDrawPath(GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -08001422 GrPipelineBuilder* pipelineBuilder,
joshualitt5531d512014-12-17 15:50:11 -08001423 const SkMatrix& viewMatrix,
joshualitt2e3b3e32014-12-09 13:31:14 -08001424 GrColor color,
joshualitt9853cce2014-11-17 14:22:48 -08001425 bool useAA,
1426 const SkPath& path,
egdanield58a0ba2014-06-11 10:30:05 -07001427 const GrStrokeInfo& strokeInfo) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001428 RETURN_IF_ABANDONED
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001429 SkASSERT(!path.isEmpty());
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001430
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001431 GR_CREATE_TRACE_MARKER("GrContext::internalDrawPath", target);
1432
1433
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001434 // An Assumption here is that path renderer would use some form of tweaking
1435 // the src color (either the input alpha or in the frag shader) to implement
1436 // aa. If we have some future driver-mojo path AA that can do the right
1437 // thing WRT to the blend then we'll need some query on the PR.
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001438 bool useCoverageAA = useAA &&
egdaniel0bdeec92015-02-23 12:12:54 -08001439 !pipelineBuilder->getRenderTarget()->isMultisampled();
bsalomon@google.com289533a2011-10-27 12:34:25 +00001440
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001441
1442 GrPathRendererChain::DrawType type =
1443 useCoverageAA ? GrPathRendererChain::kColorAntiAlias_DrawType :
joshualitt9853cce2014-11-17 14:22:48 -08001444 GrPathRendererChain::kColor_DrawType;
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001445
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001446 const SkPath* pathPtr = &path;
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001447 SkTLazy<SkPath> tmpPath;
egdanield58a0ba2014-06-11 10:30:05 -07001448 SkTCopyOnFirstWrite<SkStrokeRec> stroke(strokeInfo.getStrokeRec());
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001449
1450 // Try a 1st time without stroking the path and without allowing the SW renderer
egdaniel8dd688b2015-01-22 10:16:09 -08001451 GrPathRenderer* pr = this->getPathRenderer(target, pipelineBuilder, viewMatrix, *pathPtr,
1452 *stroke, false, type);
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001453
robertphillips@google.come79f3202014-02-11 16:30:21 +00001454 if (NULL == pr) {
joshualitt5531d512014-12-17 15:50:11 -08001455 if (!GrPathRenderer::IsStrokeHairlineOrEquivalent(*stroke, viewMatrix, NULL)) {
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001456 // It didn't work the 1st time, so try again with the stroked path
egdanielf103cd82015-04-13 13:06:46 -07001457 stroke.writable()->setResScale(SkScalarAbs(viewMatrix.getMaxScale()));
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001458 if (stroke->applyToPath(tmpPath.init(), *pathPtr)) {
1459 pathPtr = tmpPath.get();
1460 stroke.writable()->setFillStyle();
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001461 if (pathPtr->isEmpty()) {
1462 return;
1463 }
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001464 }
1465 }
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001466
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001467 // This time, allow SW renderer
egdaniel8dd688b2015-01-22 10:16:09 -08001468 pr = this->getPathRenderer(target, pipelineBuilder, viewMatrix, *pathPtr, *stroke, true,
1469 type);
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001470 }
1471
robertphillips@google.come79f3202014-02-11 16:30:21 +00001472 if (NULL == pr) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001473#ifdef SK_DEBUG
tfarina38406c82014-10-31 07:11:12 -07001474 SkDebugf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001475#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001476 return;
1477 }
1478
egdaniel8dd688b2015-01-22 10:16:09 -08001479 pr->drawPath(target, pipelineBuilder, color, viewMatrix, *pathPtr, *stroke, useCoverageAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001480}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001481
bsalomon@google.com27847de2011-02-22 20:59:41 +00001482////////////////////////////////////////////////////////////////////////////////
1483
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001484void GrContext::flush(int flagsBitfield) {
robertphillips@google.come7db8d62013-07-04 11:48:52 +00001485 if (NULL == fDrawBuffer) {
1486 return;
1487 }
1488
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001489 if (kDiscard_FlushBit & flagsBitfield) {
1490 fDrawBuffer->reset();
1491 } else {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001492 fDrawBuffer->flush();
junov@google.com53a55842011-06-08 22:55:10 +00001493 }
bsalomon3f324322015-04-08 11:01:54 -07001494 fResourceCache->notifyFlushOccurred();
bsalomonf21dab92014-11-13 13:33:28 -08001495 fFlushToReduceCacheSize = false;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001496}
1497
bsalomon81beccc2014-10-13 12:32:55 -07001498bool sw_convert_to_premul(GrPixelConfig srcConfig, int width, int height, size_t inRowBytes,
1499 const void* inPixels, size_t outRowBytes, void* outPixels) {
1500 SkSrcPixelInfo srcPI;
jvanverthfa1e8a72014-12-22 08:31:49 -08001501 if (!GrPixelConfig2ColorAndProfileType(srcConfig, &srcPI.fColorType, NULL)) {
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001502 return false;
1503 }
bsalomon81beccc2014-10-13 12:32:55 -07001504 srcPI.fAlphaType = kUnpremul_SkAlphaType;
1505 srcPI.fPixels = inPixels;
1506 srcPI.fRowBytes = inRowBytes;
1507
1508 SkDstPixelInfo dstPI;
1509 dstPI.fColorType = srcPI.fColorType;
1510 dstPI.fAlphaType = kPremul_SkAlphaType;
1511 dstPI.fPixels = outPixels;
1512 dstPI.fRowBytes = outRowBytes;
1513
1514 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001515}
1516
bsalomon81beccc2014-10-13 12:32:55 -07001517bool GrContext::writeSurfacePixels(GrSurface* surface,
1518 int left, int top, int width, int height,
1519 GrPixelConfig srcConfig, const void* buffer, size_t rowBytes,
1520 uint32_t pixelOpsFlags) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001521 RETURN_FALSE_IF_ABANDONED
bsalomon81beccc2014-10-13 12:32:55 -07001522 {
1523 GrTexture* texture = NULL;
1524 if (!(kUnpremul_PixelOpsFlag & pixelOpsFlags) && (texture = surface->asTexture()) &&
1525 fGpu->canWriteTexturePixels(texture, srcConfig)) {
1526
1527 if (!(kDontFlush_PixelOpsFlag & pixelOpsFlags) &&
1528 surface->surfacePriv().hasPendingIO()) {
1529 this->flush();
1530 }
1531 return fGpu->writeTexturePixels(texture, left, top, width, height,
1532 srcConfig, buffer, rowBytes);
1533 // Don't need to check kFlushWrites_PixelOp here, we just did a direct write so the
1534 // upload is already flushed.
1535 }
1536 }
1537
1538 // If we didn't do a direct texture write then we upload the pixels to a texture and draw.
1539 GrRenderTarget* renderTarget = surface->asRenderTarget();
1540 if (NULL == renderTarget) {
1541 return false;
1542 }
1543
1544 // We ignore the preferred config unless it is a R/B swap of the src config. In that case
1545 // we will upload the original src data to a scratch texture but we will spoof it as the swapped
1546 // config. This scratch will then have R and B swapped. We correct for this by swapping again
1547 // when drawing the scratch to the dst using a conversion effect.
1548 bool swapRAndB = false;
1549 GrPixelConfig writeConfig = srcConfig;
1550 if (GrPixelConfigSwapRAndB(srcConfig) ==
1551 fGpu->preferredWritePixelsConfig(srcConfig, renderTarget->config())) {
1552 writeConfig = GrPixelConfigSwapRAndB(srcConfig);
1553 swapRAndB = true;
1554 }
1555
bsalomonf2703d82014-10-28 14:33:06 -07001556 GrSurfaceDesc desc;
bsalomon81beccc2014-10-13 12:32:55 -07001557 desc.fWidth = width;
1558 desc.fHeight = height;
1559 desc.fConfig = writeConfig;
bsalomone3059732014-10-14 11:47:22 -07001560 SkAutoTUnref<GrTexture> texture(this->refScratchTexture(desc, kApprox_ScratchTexMatch));
1561 if (!texture) {
bsalomon81beccc2014-10-13 12:32:55 -07001562 return false;
1563 }
1564
1565 SkAutoTUnref<const GrFragmentProcessor> fp;
1566 SkMatrix textureMatrix;
1567 textureMatrix.setIDiv(texture->width(), texture->height());
1568
1569 // allocate a tmp buffer and sw convert the pixels to premul
1570 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
1571
1572 if (kUnpremul_PixelOpsFlag & pixelOpsFlags) {
1573 if (!GrPixelConfigIs8888(srcConfig)) {
1574 return false;
1575 }
1576 fp.reset(this->createUPMToPMEffect(texture, swapRAndB, textureMatrix));
1577 // handle the unpremul step on the CPU if we couldn't create an effect to do it.
1578 if (NULL == fp) {
1579 size_t tmpRowBytes = 4 * width;
1580 tmpPixels.reset(width * height);
1581 if (!sw_convert_to_premul(srcConfig, width, height, rowBytes, buffer, tmpRowBytes,
1582 tmpPixels.get())) {
1583 return false;
1584 }
1585 rowBytes = tmpRowBytes;
1586 buffer = tmpPixels.get();
1587 }
1588 }
1589 if (NULL == fp) {
1590 fp.reset(GrConfigConversionEffect::Create(texture,
1591 swapRAndB,
1592 GrConfigConversionEffect::kNone_PMConversion,
1593 textureMatrix));
1594 }
1595
1596 // Even if the client told us not to flush, we still flush here. The client may have known that
1597 // writes to the original surface caused no data hazards, but they can't know that the scratch
1598 // we just got is safe.
1599 if (texture->surfacePriv().hasPendingIO()) {
1600 this->flush();
1601 }
1602 if (!fGpu->writeTexturePixels(texture, 0, 0, width, height,
1603 writeConfig, buffer, rowBytes)) {
1604 return false;
1605 }
1606
1607 SkMatrix matrix;
1608 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
1609
1610 // This function can be called in the midst of drawing another object (e.g., when uploading a
1611 // SW-rasterized clip while issuing a draw). So we push the current geometry state before
1612 // drawing a rect to the render target.
1613 // The bracket ensures we pop the stack if we wind up flushing below.
1614 {
joshualitt570d2f82015-02-25 13:19:48 -08001615 GrDrawTarget* drawTarget = this->prepareToDraw();
1616 if (!drawTarget) {
1617 return false;
1618 }
joshualitt9853cce2014-11-17 14:22:48 -08001619 GrDrawTarget::AutoGeometryPush agp(drawTarget);
1620
egdaniel8dd688b2015-01-22 10:16:09 -08001621 GrPipelineBuilder pipelineBuilder;
1622 pipelineBuilder.addColorProcessor(fp);
1623 pipelineBuilder.setRenderTarget(renderTarget);
joshualitt44701df2015-02-23 14:44:57 -08001624 drawTarget->drawSimpleRect(&pipelineBuilder,
1625 GrColor_WHITE,
1626 matrix,
joshualitt8059eb92014-12-29 15:10:07 -08001627 SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)));
bsalomon81beccc2014-10-13 12:32:55 -07001628 }
1629
1630 if (kFlushWrites_PixelOp & pixelOpsFlags) {
1631 this->flushSurfaceWrites(surface);
1632 }
1633
1634 return true;
1635}
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001636
reed@google.com7111d462014-03-25 16:20:24 +00001637// toggles between RGBA and BGRA
1638static SkColorType toggle_colortype32(SkColorType ct) {
1639 if (kRGBA_8888_SkColorType == ct) {
1640 return kBGRA_8888_SkColorType;
1641 } else {
1642 SkASSERT(kBGRA_8888_SkColorType == ct);
1643 return kRGBA_8888_SkColorType;
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001644 }
1645}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001646
bsalomon@google.com0342a852012-08-20 19:22:38 +00001647bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
1648 int left, int top, int width, int height,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001649 GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001650 uint32_t flags) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001651 RETURN_FALSE_IF_ABANDONED
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001652 ASSERT_OWNED_RESOURCE(target);
bsalomon89c62982014-11-03 12:08:42 -08001653 SkASSERT(target);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001654
bsalomonafbf2d62014-09-30 12:18:44 -07001655 if (!(kDontFlush_PixelOpsFlag & flags) && target->surfacePriv().hasPendingWrite()) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001656 this->flush();
1657 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001658
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001659 // Determine which conversions have to be applied: flipY, swapRAnd, and/or unpremul.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001660
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001661 // If fGpu->readPixels would incur a y-flip cost then we will read the pixels upside down. We'll
1662 // either do the flipY by drawing into a scratch with a matrix or on the cpu after the read.
1663 bool flipY = fGpu->readPixelsWillPayForYFlip(target, left, top,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001664 width, height, dstConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001665 rowBytes);
bsalomon@google.com9c680582013-02-06 18:17:50 +00001666 // We ignore the preferred config if it is different than our config unless it is an R/B swap.
1667 // In that case we'll perform an R and B swap while drawing to a scratch texture of the swapped
1668 // config. Then we will call readPixels on the scratch with the swapped config. The swaps during
1669 // the draw cancels out the fact that we call readPixels with a config that is R/B swapped from
1670 // dstConfig.
1671 GrPixelConfig readConfig = dstConfig;
1672 bool swapRAndB = false;
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +00001673 if (GrPixelConfigSwapRAndB(dstConfig) ==
1674 fGpu->preferredReadPixelsConfig(dstConfig, target->config())) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001675 readConfig = GrPixelConfigSwapRAndB(readConfig);
1676 swapRAndB = true;
1677 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001678
bsalomon@google.com0342a852012-08-20 19:22:38 +00001679 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001680
bsalomon@google.com9c680582013-02-06 18:17:50 +00001681 if (unpremul && !GrPixelConfigIs8888(dstConfig)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001682 // The unpremul flag is only allowed for these two configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001683 return false;
1684 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001685
bsalomon191bcc02014-11-14 11:31:13 -08001686 SkAutoTUnref<GrTexture> tempTexture;
1687
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001688 // If the src is a texture and we would have to do conversions after read pixels, we instead
1689 // do the conversions by drawing the src to a scratch texture. If we handle any of the
1690 // conversions in the draw we set the corresponding bool to false so that we don't reapply it
1691 // on the read back pixels.
1692 GrTexture* src = target->asTexture();
bsalomon49f085d2014-09-05 13:34:00 -07001693 if (src && (swapRAndB || unpremul || flipY)) {
bsalomon81beccc2014-10-13 12:32:55 -07001694 // Make the scratch a render so we can read its pixels.
bsalomonf2703d82014-10-28 14:33:06 -07001695 GrSurfaceDesc desc;
1696 desc.fFlags = kRenderTarget_GrSurfaceFlag;
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001697 desc.fWidth = width;
1698 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001699 desc.fConfig = readConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001700 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001701
bsalomon@google.com9c680582013-02-06 18:17:50 +00001702 // 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 +00001703 // match the passed rect. However, if we see many different size rectangles we will trash
1704 // our texture cache and pay the cost of creating and destroying many textures. So, we only
1705 // request an exact match when the caller is reading an entire RT.
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001706 ScratchTexMatch match = kApprox_ScratchTexMatch;
1707 if (0 == left &&
1708 0 == top &&
1709 target->width() == width &&
1710 target->height() == height &&
1711 fGpu->fullReadPixelsIsFasterThanPartial()) {
1712 match = kExact_ScratchTexMatch;
1713 }
bsalomon191bcc02014-11-14 11:31:13 -08001714 tempTexture.reset(this->refScratchTexture(desc, match));
1715 if (tempTexture) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001716 // compute a matrix to perform the draw
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001717 SkMatrix textureMatrix;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001718 textureMatrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001719 textureMatrix.postIDiv(src->width(), src->height());
1720
joshualittb0a8a372014-09-23 09:50:21 -07001721 SkAutoTUnref<const GrFragmentProcessor> fp;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001722 if (unpremul) {
joshualittb0a8a372014-09-23 09:50:21 -07001723 fp.reset(this->createPMToUPMEffect(src, swapRAndB, textureMatrix));
1724 if (fp) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001725 unpremul = false; // we no longer need to do this on CPU after the read back.
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001726 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001727 }
1728 // If we failed to create a PM->UPM effect and have no other conversions to perform then
1729 // there is no longer any point to using the scratch.
joshualittb0a8a372014-09-23 09:50:21 -07001730 if (fp || flipY || swapRAndB) {
1731 if (!fp) {
1732 fp.reset(GrConfigConversionEffect::Create(
1733 src, swapRAndB, GrConfigConversionEffect::kNone_PMConversion,
1734 textureMatrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001735 }
1736 swapRAndB = false; // we will handle the swap in the draw.
bsalomon@google.comc4364992011-11-07 15:54:49 +00001737
robertphillips@google.com13f181f2013-03-02 12:02:08 +00001738 // We protect the existing geometry here since it may not be
1739 // clear to the caller that a draw operation (i.e., drawSimpleRect)
1740 // can be invoked in this method
joshualitt5c55fef2014-10-31 14:04:35 -07001741 {
joshualitt9853cce2014-11-17 14:22:48 -08001742 GrDrawTarget::AutoGeometryPush agp(fDrawBuffer);
egdaniel8dd688b2015-01-22 10:16:09 -08001743 GrPipelineBuilder pipelineBuilder;
joshualitt5c55fef2014-10-31 14:04:35 -07001744 SkASSERT(fp);
egdaniel8dd688b2015-01-22 10:16:09 -08001745 pipelineBuilder.addColorProcessor(fp);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001746
egdaniel8dd688b2015-01-22 10:16:09 -08001747 pipelineBuilder.setRenderTarget(tempTexture->asRenderTarget());
joshualitt5c55fef2014-10-31 14:04:35 -07001748 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
joshualitt44701df2015-02-23 14:44:57 -08001749 fDrawBuffer->drawSimpleRect(&pipelineBuilder,
1750 GrColor_WHITE,
1751 SkMatrix::I(),
egdaniel8dd688b2015-01-22 10:16:09 -08001752 rect);
joshualitt5c55fef2014-10-31 14:04:35 -07001753 // we want to read back from the scratch's origin
1754 left = 0;
1755 top = 0;
bsalomon191bcc02014-11-14 11:31:13 -08001756 target = tempTexture->asRenderTarget();
joshualitt5c55fef2014-10-31 14:04:35 -07001757 }
1758 this->flushSurfaceWrites(target);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001759 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001760 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001761 }
joshualitt5c55fef2014-10-31 14:04:35 -07001762
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001763 if (!fGpu->readPixels(target,
1764 left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001765 readConfig, buffer, rowBytes)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001766 return false;
1767 }
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001768 // Perform any conversions we weren't able to perform using a scratch texture.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001769 if (unpremul || swapRAndB) {
reed@google.com7111d462014-03-25 16:20:24 +00001770 SkDstPixelInfo dstPI;
jvanverthfa1e8a72014-12-22 08:31:49 -08001771 if (!GrPixelConfig2ColorAndProfileType(dstConfig, &dstPI.fColorType, NULL)) {
reed@google.com7111d462014-03-25 16:20:24 +00001772 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001773 }
reed@google.com7111d462014-03-25 16:20:24 +00001774 dstPI.fAlphaType = kUnpremul_SkAlphaType;
1775 dstPI.fPixels = buffer;
1776 dstPI.fRowBytes = rowBytes;
1777
1778 SkSrcPixelInfo srcPI;
1779 srcPI.fColorType = swapRAndB ? toggle_colortype32(dstPI.fColorType) : dstPI.fColorType;
1780 srcPI.fAlphaType = kPremul_SkAlphaType;
1781 srcPI.fPixels = buffer;
1782 srcPI.fRowBytes = rowBytes;
1783
1784 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001785 }
1786 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001787}
1788
bsalomon87a94eb2014-11-03 14:28:32 -08001789void GrContext::prepareSurfaceForExternalRead(GrSurface* surface) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001790 RETURN_IF_ABANDONED
bsalomon87a94eb2014-11-03 14:28:32 -08001791 SkASSERT(surface);
1792 ASSERT_OWNED_RESOURCE(surface);
1793 if (surface->surfacePriv().hasPendingIO()) {
1794 this->flush();
1795 }
1796 GrRenderTarget* rt = surface->asRenderTarget();
1797 if (fGpu && rt) {
1798 fGpu->resolveRenderTarget(rt);
bsalomon41ebbdd2014-08-04 08:31:39 -07001799 }
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001800}
1801
bsalomon41ebbdd2014-08-04 08:31:39 -07001802void GrContext::discardRenderTarget(GrRenderTarget* renderTarget) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001803 RETURN_IF_ABANDONED
bsalomon41ebbdd2014-08-04 08:31:39 -07001804 SkASSERT(renderTarget);
1805 ASSERT_OWNED_RESOURCE(renderTarget);
bsalomonf21dab92014-11-13 13:33:28 -08001806 AutoCheckFlush acf(this);
joshualitt570d2f82015-02-25 13:19:48 -08001807 GrDrawTarget* target = this->prepareToDraw();
bsalomon41ebbdd2014-08-04 08:31:39 -07001808 if (NULL == target) {
1809 return;
1810 }
1811 target->discard(renderTarget);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001812}
1813
bsalomonf80bfed2014-10-07 05:56:02 -07001814void GrContext::copySurface(GrSurface* dst, GrSurface* src, const SkIRect& srcRect,
1815 const SkIPoint& dstPoint, uint32_t pixelOpsFlags) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001816 RETURN_IF_ABANDONED
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001817 if (NULL == src || NULL == dst) {
1818 return;
1819 }
bsalomone3d4bf22014-09-23 09:15:03 -07001820 ASSERT_OWNED_RESOURCE(src);
junov2bb52102014-09-29 10:18:59 -07001821 ASSERT_OWNED_RESOURCE(dst);
Brian Salomon34a98952014-09-24 11:41:24 -04001822
bsalomonf80bfed2014-10-07 05:56:02 -07001823 // Since we're going to the draw target and not GPU, no need to check kNoFlush
1824 // here.
junov96c118e2014-09-26 13:09:47 -07001825
joshualitt570d2f82015-02-25 13:19:48 -08001826 GrDrawTarget* target = this->prepareToDraw();
junov96c118e2014-09-26 13:09:47 -07001827 if (NULL == target) {
1828 return;
1829 }
junov96c118e2014-09-26 13:09:47 -07001830 target->copySurface(dst, src, srcRect, dstPoint);
bsalomonf80bfed2014-10-07 05:56:02 -07001831
1832 if (kFlushWrites_PixelOp & pixelOpsFlags) {
1833 this->flush();
1834 }
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001835}
1836
bsalomonf80bfed2014-10-07 05:56:02 -07001837void GrContext::flushSurfaceWrites(GrSurface* surface) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001838 RETURN_IF_ABANDONED
bsalomonf80bfed2014-10-07 05:56:02 -07001839 if (surface->surfacePriv().hasPendingWrite()) {
1840 this->flush();
1841 }
1842}
1843
egdaniel8dd688b2015-01-22 10:16:09 -08001844GrDrawTarget* GrContext::prepareToDraw(GrPipelineBuilder* pipelineBuilder,
joshualitt25d9c152015-02-18 12:29:52 -08001845 GrRenderTarget* rt,
joshualitt570d2f82015-02-25 13:19:48 -08001846 const GrClip& clip,
joshualitt9853cce2014-11-17 14:22:48 -08001847 const GrPaint* paint,
1848 const AutoCheckFlush* acf) {
joshualitt5f5a8d72015-02-25 14:09:45 -08001849 if (NULL == fGpu || NULL == fDrawBuffer) {
bsalomon41ebbdd2014-08-04 08:31:39 -07001850 return NULL;
1851 }
1852
joshualitt570d2f82015-02-25 13:19:48 -08001853 ASSERT_OWNED_RESOURCE(rt);
1854 SkASSERT(rt && paint && acf);
1855 pipelineBuilder->setFromPaint(*paint, rt, clip);
1856 return fDrawBuffer;
1857}
1858
1859GrDrawTarget* GrContext::prepareToDraw() {
1860 if (NULL == fGpu) {
1861 return NULL;
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001862 }
joshualitt5c55fef2014-10-31 14:04:35 -07001863 return fDrawBuffer;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001864}
1865
robertphillips@google.com72176b22012-05-23 13:19:12 +00001866/*
1867 * This method finds a path renderer that can draw the specified path on
1868 * the provided target.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001869 * Due to its expense, the software path renderer has split out so it can
robertphillips@google.com72176b22012-05-23 13:19:12 +00001870 * can be individually allowed/disallowed via the "allowSW" boolean.
1871 */
joshualitt9853cce2014-11-17 14:22:48 -08001872GrPathRenderer* GrContext::getPathRenderer(const GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -08001873 const GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -08001874 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -08001875 const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001876 const SkStrokeRec& stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001877 bool allowSW,
1878 GrPathRendererChain::DrawType drawType,
1879 GrPathRendererChain::StencilSupport* stencilSupport) {
1880
bsalomon@google.com30085192011-08-19 15:42:31 +00001881 if (NULL == fPathRendererChain) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001882 fPathRendererChain = SkNEW_ARGS(GrPathRendererChain, (this));
bsalomon@google.com30085192011-08-19 15:42:31 +00001883 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001884
joshualitt9853cce2014-11-17 14:22:48 -08001885 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(target,
egdaniel8dd688b2015-01-22 10:16:09 -08001886 pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -08001887 viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -08001888 path,
sugoi@google.com12b4e272012-12-06 20:13:11 +00001889 stroke,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001890 drawType,
1891 stencilSupport);
robertphillips@google.com72176b22012-05-23 13:19:12 +00001892
1893 if (NULL == pr && allowSW) {
1894 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001895 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001896 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001897 pr = fSoftwarePathRenderer;
1898 }
1899
1900 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001901}
1902
bsalomon@google.com27847de2011-02-22 20:59:41 +00001903////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00001904bool GrContext::isConfigRenderable(GrPixelConfig config, bool withMSAA) const {
1905 return fGpu->caps()->isConfigRenderable(config, withMSAA);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001906}
1907
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +00001908int GrContext::getRecommendedSampleCount(GrPixelConfig config,
1909 SkScalar dpi) const {
1910 if (!this->isConfigRenderable(config, true)) {
1911 return 0;
1912 }
1913 int chosenSampleCount = 0;
1914 if (fGpu->caps()->pathRenderingSupport()) {
1915 if (dpi >= 250.0f) {
1916 chosenSampleCount = 4;
1917 } else {
1918 chosenSampleCount = 16;
1919 }
1920 }
1921 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ?
1922 chosenSampleCount : 0;
1923}
1924
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001925void GrContext::setupDrawBuffer() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001926 SkASSERT(NULL == fDrawBuffer);
1927 SkASSERT(NULL == fDrawBufferVBAllocPool);
1928 SkASSERT(NULL == fDrawBufferIBAllocPool);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001929
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001930 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001931 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001932 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001933 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001934 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001935 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001936 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001937 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001938
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001939 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001940 fDrawBufferVBAllocPool,
1941 fDrawBufferIBAllocPool));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001942}
1943
bsalomon@google.com21c10c52013-06-13 17:44:07 +00001944GrDrawTarget* GrContext::getTextTarget() {
joshualitt570d2f82015-02-25 13:19:48 -08001945 return this->prepareToDraw();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001946}
1947
1948const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1949 return fGpu->getQuadIndexBuffer();
1950}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001951
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001952namespace {
1953void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
1954 GrConfigConversionEffect::PMConversion pmToUPM;
1955 GrConfigConversionEffect::PMConversion upmToPM;
1956 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
1957 *pmToUPMValue = pmToUPM;
1958 *upmToPMValue = upmToPM;
1959}
1960}
1961
joshualittb0a8a372014-09-23 09:50:21 -07001962const GrFragmentProcessor* GrContext::createPMToUPMEffect(GrTexture* texture,
1963 bool swapRAndB,
1964 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001965 if (!fDidTestPMConversions) {
1966 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001967 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001968 }
1969 GrConfigConversionEffect::PMConversion pmToUPM =
1970 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
1971 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001972 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001973 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001974 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001975 }
1976}
1977
joshualittb0a8a372014-09-23 09:50:21 -07001978const GrFragmentProcessor* GrContext::createUPMToPMEffect(GrTexture* texture,
1979 bool swapRAndB,
1980 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001981 if (!fDidTestPMConversions) {
1982 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001983 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001984 }
1985 GrConfigConversionEffect::PMConversion upmToPM =
1986 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
1987 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001988 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001989 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001990 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001991 }
1992}
1993
bsalomon37f9a262015-02-02 13:00:10 -08001994//////////////////////////////////////////////////////////////////////////////
1995
1996void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
1997 if (maxTextures) {
bsalomon0ea80f42015-02-11 10:49:59 -08001998 *maxTextures = fResourceCache->getMaxResourceCount();
bsalomon37f9a262015-02-02 13:00:10 -08001999 }
2000 if (maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -08002001 *maxTextureBytes = fResourceCache->getMaxResourceBytes();
bsalomon37f9a262015-02-02 13:00:10 -08002002 }
2003}
2004
2005void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
bsalomon0ea80f42015-02-11 10:49:59 -08002006 fResourceCache->setLimits(maxTextures, maxTextureBytes);
bsalomon37f9a262015-02-02 13:00:10 -08002007}
2008
bsalomonf99e9612015-02-19 08:24:16 -08002009void GrContext::addResourceToCache(const GrUniqueKey& key, GrGpuResource* resource) {
bsalomon37f9a262015-02-02 13:00:10 -08002010 ASSERT_OWNED_RESOURCE(resource);
bsalomonf99e9612015-02-19 08:24:16 -08002011 if (!resource) {
2012 return;
bsalomon37f9a262015-02-02 13:00:10 -08002013 }
bsalomonf99e9612015-02-19 08:24:16 -08002014 resource->resourcePriv().setUniqueKey(key);
bsalomon37f9a262015-02-02 13:00:10 -08002015}
2016
bsalomon8718aaf2015-02-19 07:24:21 -08002017bool GrContext::isResourceInCache(const GrUniqueKey& key) const {
2018 return fResourceCache->hasUniqueKey(key);
commit-bot@chromium.org95a2b0e2014-05-05 19:21:16 +00002019}
2020
bsalomon8718aaf2015-02-19 07:24:21 -08002021GrGpuResource* GrContext::findAndRefCachedResource(const GrUniqueKey& key) {
2022 return fResourceCache->findAndRefUniqueResource(key);
commit-bot@chromium.org95a2b0e2014-05-05 19:21:16 +00002023}
2024
bsalomon37f9a262015-02-02 13:00:10 -08002025//////////////////////////////////////////////////////////////////////////////
2026
egdanielbbcb38d2014-06-19 10:19:29 -07002027void GrContext::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
2028 fGpu->addGpuTraceMarker(marker);
bsalomon49f085d2014-09-05 13:34:00 -07002029 if (fDrawBuffer) {
egdanielbbcb38d2014-06-19 10:19:29 -07002030 fDrawBuffer->addGpuTraceMarker(marker);
2031 }
2032}
2033
2034void GrContext::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
2035 fGpu->removeGpuTraceMarker(marker);
bsalomon49f085d2014-09-05 13:34:00 -07002036 if (fDrawBuffer) {
egdanielbbcb38d2014-06-19 10:19:29 -07002037 fDrawBuffer->removeGpuTraceMarker(marker);
2038 }
2039}
2040