blob: 85d53119b614891fd8bb6d910794e1c8de8edd15 [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
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
bsalomon@google.com1fadb202011-12-12 16:10:08 +000010#include "GrContext.h"
11
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000012#include "effects/GrConfigConversionEffect.h"
egdaniele61c4112014-06-12 10:24:21 -070013#include "effects/GrDashingEffect.h"
14#include "effects/GrSingleTextureEffect.h"
bsalomon@google.comb505a122012-05-31 18:40:36 +000015
jvanverth@google.combfe2b9d2013-09-06 16:57:29 +000016#include "GrAARectRenderer.h"
tomhudson@google.com278cbb42011-06-30 19:37:01 +000017#include "GrBufferAllocPool.h"
commit-bot@chromium.orgdcb8ef92014-03-27 11:26:10 +000018#include "GrGpu.h"
kkinnunenc6cb56f2014-06-24 00:12:27 -070019#include "GrDistanceFieldTextContext.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000020#include "GrDrawTargetCaps.h"
commit-bot@chromium.orgdcb8ef92014-03-27 11:26:10 +000021#include "GrIndexBuffer.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000022#include "GrInOrderDrawBuffer.h"
robertphillips@google.come930a072014-04-03 00:34:27 +000023#include "GrLayerCache.h"
commit-bot@chromium.org81312832013-03-22 18:34:09 +000024#include "GrOvalRenderer.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000025#include "GrPathRenderer.h"
tomhudson@google.comd22b6e42011-06-24 15:53:40 +000026#include "GrPathUtils.h"
bsalomon@google.com50398bf2011-07-26 20:45:30 +000027#include "GrResourceCache.h"
bsalomonc8dc1f72014-08-21 13:02:13 -070028#include "GrResourceCache2.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000029#include "GrSoftwarePathRenderer.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000030#include "GrStencilBuffer.h"
kkinnunenc6cb56f2014-06-24 00:12:27 -070031#include "GrStencilAndCoverTextContext.h"
egdanield58a0ba2014-06-11 10:30:05 -070032#include "GrStrokeInfo.h"
tomhudson@google.com278cbb42011-06-30 19:37:01 +000033#include "GrTextStrike.h"
egdanielbbcb38d2014-06-19 10:19:29 -070034#include "GrTraceMarker.h"
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +000035#include "GrTracing.h"
egdanield58a0ba2014-06-11 10:30:05 -070036#include "SkDashPathPriv.h"
reed@google.com7111d462014-03-25 16:20:24 +000037#include "SkGr.h"
commit-bot@chromium.orgdcb8ef92014-03-27 11:26:10 +000038#include "SkRTConf.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000039#include "SkRRect.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000040#include "SkStrokeRec.h"
commit-bot@chromium.orgdcb8ef92014-03-27 11:26:10 +000041#include "SkTLazy.h"
commit-bot@chromium.org47841822014-03-27 14:19:17 +000042#include "SkTLS.h"
commit-bot@chromium.org933e65d2014-03-20 20:00:24 +000043#include "SkTraceEvent.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000044
bsalomon@google.com82b0ec62013-02-07 21:02:36 +000045// It can be useful to set this to false to test whether a bug is caused by using the
bsalomon@google.com1d4edd32012-08-16 18:36:06 +000046// InOrderDrawBuffer, to compare performance of using/not using InOrderDrawBuffer, or to make
bsalomon@google.com82b0ec62013-02-07 21:02:36 +000047// debugging simpler.
bsalomon@google.come932c982013-09-10 19:47:01 +000048SK_CONF_DECLARE(bool, c_Defer, "gpu.deferContext", true,
jvanverth@google.com65eb4d52013-03-19 18:51:02 +000049 "Defers rendering in GrContext via GrInOrderDrawBuffer.");
bsalomon@google.com82b0ec62013-02-07 21:02:36 +000050
51#define BUFFERED_DRAW (c_Defer ? kYes_BufferedDraw : kNo_BufferedDraw)
bsalomon@google.com27847de2011-02-22 20:59:41 +000052
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000053#ifdef SK_DEBUG
reed@google.com4b2d3f32012-05-15 18:05:50 +000054 // change this to a 1 to see notifications when partial coverage fails
55 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
56#else
57 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
58#endif
59
robertphillips@google.com4e5559a2013-10-30 17:04:16 +000060static const size_t MAX_RESOURCE_CACHE_COUNT = GR_DEFAULT_RESOURCE_CACHE_COUNT_LIMIT;
61static const size_t MAX_RESOURCE_CACHE_BYTES = GR_DEFAULT_RESOURCE_CACHE_MB_LIMIT * 1024 * 1024;
bsalomon@google.com27847de2011-02-22 20:59:41 +000062
bsalomon@google.com60361492012-03-15 17:47:06 +000063static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
bsalomon@google.com27847de2011-02-22 20:59:41 +000064static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
65
bsalomon@google.com1d4edd32012-08-16 18:36:06 +000066static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = 1 << 11;
67static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = 4;
bsalomon@google.com27847de2011-02-22 20:59:41 +000068
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000069#define ASSERT_OWNED_RESOURCE(R) SkASSERT(!(R) || (R)->getContext() == this)
bsalomon@google.combc4b6542011-11-19 13:56:11 +000070
bsalomon@google.comeb6879f2013-06-13 19:34:18 +000071// Glorified typedef to avoid including GrDrawState.h in GrContext.h
72class GrContext::AutoRestoreEffects : public GrDrawState::AutoRestoreEffects {};
73
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +000074class GrContext::AutoCheckFlush {
75public:
76 AutoCheckFlush(GrContext* context) : fContext(context) { SkASSERT(NULL != context); }
77
78 ~AutoCheckFlush() {
79 if (fContext->fFlushToReduceCacheSize) {
80 fContext->flush();
81 }
82 }
83
84private:
85 GrContext* fContext;
86};
87
krajcevski9c6d4d72014-08-12 07:26:25 -070088GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext,
89 const Options* opts) {
90 GrContext* context;
91 if (NULL == opts) {
92 context = SkNEW_ARGS(GrContext, (Options()));
93 } else {
94 context = SkNEW_ARGS(GrContext, (*opts));
95 }
96
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000097 if (context->init(backend, backendContext)) {
98 return context;
99 } else {
100 context->unref();
101 return NULL;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000102 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000103}
104
krajcevski9c6d4d72014-08-12 07:26:25 -0700105GrContext::GrContext(const Options& opts) : fOptions(opts) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000106 fDrawState = NULL;
107 fGpu = NULL;
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000108 fClip = NULL;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000109 fPathRendererChain = NULL;
110 fSoftwarePathRenderer = NULL;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000111 fResourceCache = NULL;
bsalomonc8dc1f72014-08-21 13:02:13 -0700112 fResourceCache2 = NULL;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000113 fFontCache = NULL;
114 fDrawBuffer = NULL;
115 fDrawBufferVBAllocPool = NULL;
116 fDrawBufferIBAllocPool = NULL;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000117 fFlushToReduceCacheSize = false;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000118 fAARectRenderer = NULL;
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000119 fOvalRenderer = NULL;
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000120 fViewMatrix.reset();
robertphillips@google.com44a91dc2013-07-25 15:32:06 +0000121 fMaxTextureSizeOverride = 1 << 20;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000122}
123
124bool GrContext::init(GrBackend backend, GrBackendContext backendContext) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000125 SkASSERT(NULL == fGpu);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000126
127 fGpu = GrGpu::Create(backend, backendContext, this);
128 if (NULL == fGpu) {
129 return false;
130 }
131
132 fDrawState = SkNEW(GrDrawState);
133 fGpu->setDrawState(fDrawState);
134
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000135 fResourceCache = SkNEW_ARGS(GrResourceCache, (MAX_RESOURCE_CACHE_COUNT,
136 MAX_RESOURCE_CACHE_BYTES));
137 fResourceCache->setOverbudgetCallback(OverbudgetCB, this);
bsalomonc8dc1f72014-08-21 13:02:13 -0700138 fResourceCache2 = SkNEW(GrResourceCache2);
commit-bot@chromium.org1836d332013-07-16 22:55:03 +0000139
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000140 fFontCache = SkNEW_ARGS(GrFontCache, (fGpu));
141
robertphillips4ec84da2014-06-24 13:10:43 -0700142 fLayerCache.reset(SkNEW_ARGS(GrLayerCache, (this)));
robertphillips@google.come930a072014-04-03 00:34:27 +0000143
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000144 fLastDrawWasBuffered = kNo_BufferedDraw;
145
146 fAARectRenderer = SkNEW(GrAARectRenderer);
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000147 fOvalRenderer = SkNEW(GrOvalRenderer);
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000148
149 fDidTestPMConversions = false;
150
151 this->setupDrawBuffer();
152
153 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000154}
155
bsalomon@google.com27847de2011-02-22 20:59:41 +0000156GrContext::~GrContext() {
bsalomon@google.com733c0622013-04-24 17:59:32 +0000157 if (NULL == fGpu) {
158 return;
159 }
160
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000161 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000162
robertphillips@google.com950b1b02013-10-21 17:37:28 +0000163 for (int i = 0; i < fCleanUpData.count(); ++i) {
164 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
165 }
166
bsalomonc8dc1f72014-08-21 13:02:13 -0700167 delete fResourceCache2;
168 fResourceCache2 = NULL;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000169 delete fResourceCache;
170 fResourceCache = NULL;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000171 delete fFontCache;
172 delete fDrawBuffer;
173 delete fDrawBufferVBAllocPool;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000174 delete fDrawBufferIBAllocPool;
bsalomon@google.com30085192011-08-19 15:42:31 +0000175
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000176 fAARectRenderer->unref();
commit-bot@chromium.org81312832013-03-22 18:34:09 +0000177 fOvalRenderer->unref();
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000178
bsalomon@google.com205d4602011-04-25 12:43:45 +0000179 fGpu->unref();
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000180 SkSafeUnref(fPathRendererChain);
181 SkSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000182 fDrawState->unref();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000183}
184
bsalomon2354f842014-07-28 13:48:36 -0700185void GrContext::abandonContext() {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000186 // abandon first to so destructors
187 // don't try to free the resources in the API.
bsalomonc8dc1f72014-08-21 13:02:13 -0700188 fResourceCache2->abandonAll();
189
190 fGpu->contextAbandonded();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000191
bsalomon@google.com30085192011-08-19 15:42:31 +0000192 // a path renderer may be holding onto resources that
193 // are now unusable
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000194 SkSafeSetNull(fPathRendererChain);
195 SkSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000196
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000197 delete fDrawBuffer;
198 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000199
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000200 delete fDrawBufferVBAllocPool;
201 fDrawBufferVBAllocPool = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000202
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000203 delete fDrawBufferIBAllocPool;
204 fDrawBufferIBAllocPool = NULL;
205
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000206 fAARectRenderer->reset();
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000207 fOvalRenderer->reset();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000208
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000209 fResourceCache->purgeAllUnlocked();
commit-bot@chromium.org5c8ee252013-11-01 15:23:44 +0000210
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000211 fFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000212 fLayerCache->freeAll();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000213}
214
bsalomon@google.com0a208a12013-06-28 18:57:35 +0000215void GrContext::resetContext(uint32_t state) {
216 fGpu->markContextDirty(state);
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000217}
218
219void GrContext::freeGpuResources() {
220 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000221
robertphillips@google.comff175842012-05-14 19:31:39 +0000222 fGpu->purgeResources();
bsalomonc8dc1f72014-08-21 13:02:13 -0700223 if (NULL != fDrawBuffer) {
224 fDrawBuffer->purgeResources();
225 }
robertphillips@google.comff175842012-05-14 19:31:39 +0000226
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000227 fAARectRenderer->reset();
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +0000228 fOvalRenderer->reset();
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000229
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000230 fResourceCache->purgeAllUnlocked();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000231 fFontCache->freeAll();
robertphillips@google.come930a072014-04-03 00:34:27 +0000232 fLayerCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000233 // a path renderer may be holding onto resources
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000234 SkSafeSetNull(fPathRendererChain);
235 SkSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000236}
237
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000238void GrContext::getResourceCacheUsage(int* resourceCount, size_t* resourceBytes) const {
239 if (NULL != resourceCount) {
240 *resourceCount = fResourceCache->getCachedResourceCount();
241 }
242 if (NULL != resourceBytes) {
243 *resourceBytes = fResourceCache->getCachedResourceBytes();
244 }
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000245}
246
kkinnunenc6cb56f2014-06-24 00:12:27 -0700247GrTextContext* GrContext::createTextContext(GrRenderTarget* renderTarget,
248 const SkDeviceProperties&
249 leakyProperties,
250 bool enableDistanceFieldFonts) {
251 if (fGpu->caps()->pathRenderingSupport()) {
252 if (renderTarget->getStencilBuffer() && renderTarget->isMultisampled()) {
253 return SkNEW_ARGS(GrStencilAndCoverTextContext, (this, leakyProperties));
254 }
255 }
256 return SkNEW_ARGS(GrDistanceFieldTextContext, (this, leakyProperties,
257 enableDistanceFieldFonts));
258}
259
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000260////////////////////////////////////////////////////////////////////////////////
261
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000262GrTexture* GrContext::findAndRefTexture(const GrTextureDesc& desc,
263 const GrCacheID& cacheID,
264 const GrTextureParams* params) {
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +0000265 GrResourceKey resourceKey = GrTextureImpl::ComputeKey(fGpu, params, desc, cacheID);
bsalomon6d3fe022014-07-25 08:35:45 -0700266 GrGpuResource* resource = fResourceCache->find(resourceKey);
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000267 SkSafeRef(resource);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000268 return static_cast<GrTexture*>(resource);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000269}
270
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000271bool GrContext::isTextureInCache(const GrTextureDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000272 const GrCacheID& cacheID,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000273 const GrTextureParams* params) const {
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +0000274 GrResourceKey resourceKey = GrTextureImpl::ComputeKey(fGpu, params, desc, cacheID);
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000275 return fResourceCache->hasKey(resourceKey);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000276}
277
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000278void GrContext::addStencilBuffer(GrStencilBuffer* sb) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000279 ASSERT_OWNED_RESOURCE(sb);
robertphillips@google.com46a86002012-08-08 10:42:44 +0000280
281 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(sb->width(),
282 sb->height(),
283 sb->numSamples());
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000284 fResourceCache->addResource(resourceKey, sb);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000285}
286
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000287GrStencilBuffer* GrContext::findStencilBuffer(int width, int height,
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000288 int sampleCnt) {
robertphillips@google.com46a86002012-08-08 10:42:44 +0000289 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(width,
290 height,
291 sampleCnt);
bsalomon6d3fe022014-07-25 08:35:45 -0700292 GrGpuResource* resource = fResourceCache->find(resourceKey);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000293 return static_cast<GrStencilBuffer*>(resource);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000294}
295
commit-bot@chromium.orgb0ce4b62014-05-16 17:22:51 +0000296static void stretch_image(void* dst,
297 int dstW,
298 int dstH,
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000299 const void* src,
commit-bot@chromium.orgb0ce4b62014-05-16 17:22:51 +0000300 int srcW,
301 int srcH,
302 size_t bpp) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000303 SkFixed dx = (srcW << 16) / dstW;
304 SkFixed dy = (srcH << 16) / dstH;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000305
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000306 SkFixed y = dy >> 1;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000307
robertphillips@google.com8b169312013-10-15 17:47:36 +0000308 size_t dstXLimit = dstW*bpp;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000309 for (int j = 0; j < dstH; ++j) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000310 SkFixed x = dx >> 1;
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000311 const uint8_t* srcRow = reinterpret_cast<const uint8_t *>(src) + (y>>16)*srcW*bpp;
312 uint8_t* dstRow = reinterpret_cast<uint8_t *>(dst) + j*dstW*bpp;
robertphillips@google.com8b169312013-10-15 17:47:36 +0000313 for (size_t i = 0; i < dstXLimit; i += bpp) {
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000314 memcpy(dstRow + i, srcRow + (x>>16)*bpp, bpp);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000315 x += dx;
316 }
317 y += dy;
318 }
319}
320
robertphillips@google.com42903302013-04-20 12:26:07 +0000321namespace {
322
323// position + local coordinate
324extern const GrVertexAttrib gVertexAttribs[] = {
325 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000326 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kLocalCoord_GrVertexAttribBinding}
robertphillips@google.com42903302013-04-20 12:26:07 +0000327};
328
329};
330
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000331// The desired texture is NPOT and tiled but that isn't supported by
robertphillips@google.com3319f332012-08-13 18:00:36 +0000332// the current hardware. Resize the texture to be a POT
333GrTexture* GrContext::createResizedTexture(const GrTextureDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000334 const GrCacheID& cacheID,
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000335 const void* srcData,
robertphillips@google.com3319f332012-08-13 18:00:36 +0000336 size_t rowBytes,
humper@google.comb86add12013-07-25 18:49:07 +0000337 bool filter) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000338 SkAutoTUnref<GrTexture> clampedTexture(this->findAndRefTexture(desc, cacheID, NULL));
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000339 if (NULL == clampedTexture) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000340 clampedTexture.reset(this->createTexture(NULL, desc, cacheID, srcData, rowBytes));
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000341
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000342 if (NULL == clampedTexture) {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000343 return NULL;
344 }
345 }
robertphillips@google.com0d25eef2012-09-11 21:25:51 +0000346
robertphillips@google.com3319f332012-08-13 18:00:36 +0000347 GrTextureDesc rtDesc = desc;
348 rtDesc.fFlags = rtDesc.fFlags |
349 kRenderTarget_GrTextureFlagBit |
350 kNoStencil_GrTextureFlagBit;
commit-bot@chromium.org5898dce2013-09-18 18:52:16 +0000351 rtDesc.fWidth = GrNextPow2(desc.fWidth);
352 rtDesc.fHeight = GrNextPow2(desc.fHeight);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000353
354 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
355
356 if (NULL != texture) {
357 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
358 GrDrawState* drawState = fGpu->drawState();
359 drawState->setRenderTarget(texture->asRenderTarget());
360
361 // if filtering is not desired then we want to ensure all
362 // texels in the resampled image are copies of texels from
363 // the original.
humper@google.comb86add12013-07-25 18:49:07 +0000364 GrTextureParams params(SkShader::kClamp_TileMode, filter ? GrTextureParams::kBilerp_FilterMode :
365 GrTextureParams::kNone_FilterMode);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000366 drawState->addColorTextureEffect(clampedTexture, SkMatrix::I(), params);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000367
egdaniel7b3d5ee2014-08-28 05:41:14 -0700368 drawState->setVertexAttribs<gVertexAttribs>(SK_ARRAY_COUNT(gVertexAttribs),
369 2 * sizeof(SkPoint));
jvanverth@google.com054ae992013-04-01 20:06:51 +0000370
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000371 GrDrawTarget::AutoReleaseGeometry arg(fGpu, 4, 0);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000372
373 if (arg.succeeded()) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000374 SkPoint* verts = (SkPoint*) arg.vertices();
375 verts[0].setIRectFan(0, 0, texture->width(), texture->height(), 2 * sizeof(SkPoint));
376 verts[1].setIRectFan(0, 0, 1, 1, 2 * sizeof(SkPoint));
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000377 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000378 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000379 } else {
380 // TODO: Our CPU stretch doesn't filter. But we create separate
bsalomon@google.com08283af2012-10-26 13:01:20 +0000381 // stretched textures when the texture params is either filtered or
robertphillips@google.com3319f332012-08-13 18:00:36 +0000382 // not. Either implement filtered stretch blit on CPU or just create
383 // one when FBO case fails.
384
385 rtDesc.fFlags = kNone_GrTextureFlags;
386 // no longer need to clamp at min RT size.
387 rtDesc.fWidth = GrNextPow2(desc.fWidth);
388 rtDesc.fHeight = GrNextPow2(desc.fHeight);
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000389
390 // We shouldn't be resizing a compressed texture.
391 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
392
robertphillips@google.com8b169312013-10-15 17:47:36 +0000393 size_t bpp = GrBytesPerPixel(desc.fConfig);
georgeb62508b2014-08-12 18:00:47 -0700394 GrAutoMalloc<128*128*4> stretchedPixels(bpp * rtDesc.fWidth * rtDesc.fHeight);
commit-bot@chromium.orgb0ce4b62014-05-16 17:22:51 +0000395 stretch_image(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
396 srcData, desc.fWidth, desc.fHeight, bpp);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000397
398 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
399
commit-bot@chromium.orgb0ce4b62014-05-16 17:22:51 +0000400 texture = fGpu->createTexture(rtDesc, stretchedPixels.get(), stretchedRowBytes);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000401 SkASSERT(NULL != texture);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000402 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000403
404 return texture;
405}
406
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000407GrTexture* GrContext::createTexture(const GrTextureParams* params,
408 const GrTextureDesc& desc,
409 const GrCacheID& cacheID,
commit-bot@chromium.orgf9bd04f2014-05-29 19:26:48 +0000410 const void* srcData,
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000411 size_t rowBytes,
412 GrResourceKey* cacheKey) {
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +0000413 GrResourceKey resourceKey = GrTextureImpl::ComputeKey(fGpu, params, desc, cacheID);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000414
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000415 GrTexture* texture;
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +0000416 if (GrTextureImpl::NeedsResizing(resourceKey)) {
krajcevski9c0e6292014-06-02 07:38:14 -0700417 // We do not know how to resize compressed textures.
418 SkASSERT(!GrPixelConfigIsCompressed(desc.fConfig));
419
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000420 texture = this->createResizedTexture(desc, cacheID,
421 srcData, rowBytes,
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +0000422 GrTextureImpl::NeedsBilerp(resourceKey));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000423 } else {
krajcevski9c0e6292014-06-02 07:38:14 -0700424 texture = fGpu->createTexture(desc, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000425 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000426
427 if (NULL != texture) {
robertphillips@google.com1622a6d2013-07-18 17:11:45 +0000428 // Adding a resource could put us overbudget. Try to free up the
429 // necessary space before adding it.
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000430 fResourceCache->purgeAsNeeded(1, texture->gpuMemorySize());
431 fResourceCache->addResource(resourceKey, texture);
commit-bot@chromium.org50a30432013-10-24 17:44:27 +0000432
433 if (NULL != cacheKey) {
434 *cacheKey = resourceKey;
435 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000436 }
437
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000438 return texture;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000439}
440
skia.committer@gmail.com1f3c7382013-07-20 07:00:58 +0000441static GrTexture* create_scratch_texture(GrGpu* gpu,
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000442 GrResourceCache* resourceCache,
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000443 const GrTextureDesc& desc) {
444 GrTexture* texture = gpu->createTexture(desc, NULL, 0);
445 if (NULL != texture) {
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +0000446 GrResourceKey key = GrTextureImpl::ComputeScratchKey(texture->desc());
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000447 // Adding a resource could put us overbudget. Try to free up the
448 // necessary space before adding it.
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000449 resourceCache->purgeAsNeeded(1, texture->gpuMemorySize());
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000450 // Make the resource exclusive so future 'find' calls don't return it
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000451 resourceCache->addResource(key, texture, GrResourceCache::kHide_OwnershipFlag);
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000452 }
453 return texture;
454}
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000455
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000456GrTexture* GrContext::lockAndRefScratchTexture(const GrTextureDesc& inDesc, ScratchTexMatch match) {
457
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000458 SkASSERT((inDesc.fFlags & kRenderTarget_GrTextureFlagBit) ||
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000459 !(inDesc.fFlags & kNoStencil_GrTextureFlagBit));
460
461 // Renderable A8 targets are not universally supported (e.g., not on ANGLE)
bsalomon@google.com8aaac8d2013-10-15 14:35:06 +0000462 SkASSERT(this->isConfigRenderable(kAlpha_8_GrPixelConfig, inDesc.fSampleCnt > 0) ||
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000463 !(inDesc.fFlags & kRenderTarget_GrTextureFlagBit) ||
464 (inDesc.fConfig != kAlpha_8_GrPixelConfig));
465
robertphillips@google.com2d2e5c42013-10-30 21:30:43 +0000466 if (!fGpu->caps()->reuseScratchTextures() &&
467 !(inDesc.fFlags & kRenderTarget_GrTextureFlagBit)) {
468 // If we're never recycling this texture we can always make it the right size
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000469 return create_scratch_texture(fGpu, fResourceCache, inDesc);
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000470 }
471
472 GrTextureDesc desc = inDesc;
rileya@google.com2afb8ec2012-08-23 14:08:57 +0000473
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000474 if (kApprox_ScratchTexMatch == match) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000475 // bin by pow2 with a reasonable min
bsalomon@google.com40db6dc2013-03-06 16:35:49 +0000476 static const int MIN_SIZE = 16;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000477 desc.fWidth = SkTMax(MIN_SIZE, GrNextPow2(desc.fWidth));
478 desc.fHeight = SkTMax(MIN_SIZE, GrNextPow2(desc.fHeight));
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000479 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000480
bsalomon6d3fe022014-07-25 08:35:45 -0700481 GrGpuResource* resource = NULL;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000482 int origWidth = desc.fWidth;
483 int origHeight = desc.fHeight;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000484
485 do {
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +0000486 GrResourceKey key = GrTextureImpl::ComputeScratchKey(desc);
robertphillips@google.com209a1142012-10-31 12:25:21 +0000487 // Ensure we have exclusive access to the texture so future 'find' calls don't return it
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000488 resource = fResourceCache->find(key, GrResourceCache::kHide_OwnershipFlag);
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000489 if (NULL != resource) {
490 resource->ref();
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000491 break;
492 }
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000493 if (kExact_ScratchTexMatch == match) {
494 break;
495 }
bsalomon@google.com40db6dc2013-03-06 16:35:49 +0000496 // We had a cache miss and we are in approx mode, relax the fit of the flags.
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000497
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000498 // We no longer try to reuse textures that were previously used as render targets in
rileya@google.com2afb8ec2012-08-23 14:08:57 +0000499 // situations where no RT is needed; doing otherwise can confuse the video driver and
500 // cause significant performance problems in some cases.
501 if (desc.fFlags & kNoStencil_GrTextureFlagBit) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000502 desc.fFlags = desc.fFlags & ~kNoStencil_GrTextureFlagBit;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000503 } else {
504 break;
505 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000506
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000507 } while (true);
508
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000509 if (NULL == resource) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000510 desc.fFlags = inDesc.fFlags;
511 desc.fWidth = origWidth;
512 desc.fHeight = origHeight;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000513 resource = create_scratch_texture(fGpu, fResourceCache, desc);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000514 }
515
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000516 return static_cast<GrTexture*>(resource);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000517}
518
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000519void GrContext::addExistingTextureToCache(GrTexture* texture) {
520
521 if (NULL == texture) {
522 return;
523 }
524
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000525 // This texture should already have a cache entry since it was once
526 // attached
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000527 SkASSERT(NULL != texture->getCacheEntry());
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000528
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000529 // Conceptually, the cache entry is going to assume responsibility
robertphillips@google.com9ef04262013-10-29 14:06:15 +0000530 // for the creation ref. Assert refcnt == 1.
bsalomon944bcf02014-07-29 08:01:52 -0700531 // Except that this also gets called when the texture is prematurely
532 // abandoned. In that case the ref count may be > 1.
533 // SkASSERT(texture->unique());
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000534
robertphillips@google.com2d2e5c42013-10-30 21:30:43 +0000535 if (fGpu->caps()->reuseScratchTextures() || NULL != texture->asRenderTarget()) {
robertphillips@google.com9ef04262013-10-29 14:06:15 +0000536 // Since this texture came from an AutoScratchTexture it should
537 // still be in the exclusive pile. Recycle it.
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000538 fResourceCache->makeNonExclusive(texture->getCacheEntry());
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000539 this->purgeCache();
bsalomondcabb052014-07-21 14:24:01 -0700540 } else {
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000541 // When we aren't reusing textures we know this scratch texture
542 // will never be reused and would be just wasting time in the cache
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000543 fResourceCache->makeNonExclusive(texture->getCacheEntry());
544 fResourceCache->deleteResource(texture->getCacheEntry());
robertphillips@google.come4eaea22013-07-19 16:51:46 +0000545 }
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000546}
547
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000548void GrContext::unlockScratchTexture(GrTexture* texture) {
bsalomonc8dc1f72014-08-21 13:02:13 -0700549 if (texture->wasDestroyed()) {
550 if (texture->getCacheEntry()->key().isScratch()) {
551 // This texture was detached from the cache but the cache still had a ref to it but
bsalomon02e36f22014-08-22 12:01:46 -0700552 // not a pointer to it. This will unref the texture and delete its resource cache
553 // entry.
554 delete texture->getCacheEntry();
bsalomonc8dc1f72014-08-21 13:02:13 -0700555 }
556 return;
557 }
558
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000559 ASSERT_OWNED_RESOURCE(texture);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000560 SkASSERT(NULL != texture->getCacheEntry());
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000561
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000562 // If this is a scratch texture we detached it from the cache
563 // while it was locked (to avoid two callers simultaneously getting
564 // the same texture).
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000565 if (texture->getCacheEntry()->key().isScratch()) {
robertphillips@google.com2d2e5c42013-10-30 21:30:43 +0000566 if (fGpu->caps()->reuseScratchTextures() || NULL != texture->asRenderTarget()) {
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000567 fResourceCache->makeNonExclusive(texture->getCacheEntry());
robertphillips@google.com9ef04262013-10-29 14:06:15 +0000568 this->purgeCache();
bsalomondcabb052014-07-21 14:24:01 -0700569 } else if (texture->unique()) {
skia.committer@gmail.comb77f0f42013-10-30 07:01:56 +0000570 // Only the cache now knows about this texture. Since we're never
571 // reusing scratch textures (in this code path) it would just be
robertphillips@google.com9ef04262013-10-29 14:06:15 +0000572 // wasting time sitting in the cache.
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000573 fResourceCache->makeNonExclusive(texture->getCacheEntry());
574 fResourceCache->deleteResource(texture->getCacheEntry());
robertphillips@google.com9ef04262013-10-29 14:06:15 +0000575 } else {
bsalomondcabb052014-07-21 14:24:01 -0700576 // In this case (there is still a non-cache ref) but we don't really
skia.committer@gmail.comb77f0f42013-10-30 07:01:56 +0000577 // want to readd it to the cache (since it will never be reused).
robertphillips@google.com9ef04262013-10-29 14:06:15 +0000578 // Instead, give up the cache's ref and leave the decision up to
579 // addExistingTextureToCache once its ref count reaches 0. For
580 // this to work we need to leave it in the exclusive list.
commit-bot@chromium.orge49157f2014-05-09 20:46:48 +0000581 texture->impl()->setFlag((GrTextureFlags) GrTextureImpl::kReturnToCache_FlagBit);
robertphillips@google.com9ef04262013-10-29 14:06:15 +0000582 // Give up the cache's ref to the texture
583 texture->unref();
584 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000585 }
robertphillips@google.com50a035d2012-09-07 19:44:33 +0000586}
587
588void GrContext::purgeCache() {
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000589 if (NULL != fResourceCache) {
590 fResourceCache->purgeAsNeeded();
robertphillips@google.comeb9b3e12012-09-11 12:50:53 +0000591 }
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000592}
593
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000594bool GrContext::OverbudgetCB(void* data) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000595 SkASSERT(NULL != data);
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000596
597 GrContext* context = reinterpret_cast<GrContext*>(data);
598
599 // Flush the InOrderDrawBuffer to possibly free up some textures
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000600 context->fFlushToReduceCacheSize = true;
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000601
commit-bot@chromium.orgcae27fe2013-07-10 10:14:35 +0000602 return true;
603}
604
605
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000606GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000607 void* srcData,
608 size_t rowBytes) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000609 GrTextureDesc descCopy = descIn;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000610 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000611}
612
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000613void GrContext::getResourceCacheLimits(int* maxTextures, size_t* maxTextureBytes) const {
614 fResourceCache->getLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000615}
616
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000617void GrContext::setResourceCacheLimits(int maxTextures, size_t maxTextureBytes) {
618 fResourceCache->setLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000619}
620
bsalomon@google.com91958362011-06-13 17:58:13 +0000621int GrContext::getMaxTextureSize() const {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000622 return SkTMin(fGpu->caps()->maxTextureSize(), fMaxTextureSizeOverride);
bsalomon@google.com91958362011-06-13 17:58:13 +0000623}
624
625int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000626 return fGpu->caps()->maxRenderTargetSize();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000627}
628
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000629int GrContext::getMaxSampleCount() const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000630 return fGpu->caps()->maxSampleCount();
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000631}
632
bsalomon@google.com27847de2011-02-22 20:59:41 +0000633///////////////////////////////////////////////////////////////////////////////
634
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000635GrTexture* GrContext::wrapBackendTexture(const GrBackendTextureDesc& desc) {
636 return fGpu->wrapBackendTexture(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000637}
638
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000639GrRenderTarget* GrContext::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
640 return fGpu->wrapBackendRenderTarget(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000641}
642
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000643///////////////////////////////////////////////////////////////////////////////
644
bsalomon@google.comb8670992012-07-25 21:27:09 +0000645bool GrContext::supportsIndex8PixelConfig(const GrTextureParams* params,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000646 int width, int height) const {
bsalomon@google.comc26d94f2013-03-25 18:19:00 +0000647 const GrDrawTargetCaps* caps = fGpu->caps();
commit-bot@chromium.org6e7ddaa2014-05-30 13:55:58 +0000648 if (!caps->isConfigTexturable(kIndex_8_GrPixelConfig)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000649 return false;
650 }
651
tfarinaf9dae782014-06-06 06:35:28 -0700652 bool isPow2 = SkIsPow2(width) && SkIsPow2(height);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000653
654 if (!isPow2) {
bsalomon@google.comb8670992012-07-25 21:27:09 +0000655 bool tiled = NULL != params && params->isTiled();
bsalomon@google.combcce8922013-03-25 15:38:39 +0000656 if (tiled && !caps->npotTextureTileSupport()) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000657 return false;
658 }
659 }
660 return true;
661}
662
bsalomon@google.com27847de2011-02-22 20:59:41 +0000663
bsalomon@google.com27847de2011-02-22 20:59:41 +0000664////////////////////////////////////////////////////////////////////////////////
665
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000666void GrContext::clear(const SkIRect* rect,
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000667 const GrColor color,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000668 bool canIgnoreRect,
bsalomon41ebbdd2014-08-04 08:31:39 -0700669 GrRenderTarget* renderTarget) {
670 ASSERT_OWNED_RESOURCE(renderTarget);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000671 AutoRestoreEffects are;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000672 AutoCheckFlush acf(this);
egdanield78a1682014-07-09 10:41:26 -0700673 GR_CREATE_TRACE_MARKER_CONTEXT("GrContext::clear", this);
bsalomon41ebbdd2014-08-04 08:31:39 -0700674 GrDrawTarget* target = this->prepareToDraw(NULL, BUFFERED_DRAW, &are, &acf);
675 if (NULL == target) {
676 return;
677 }
678 target->clear(rect, color, canIgnoreRect, renderTarget);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000679}
680
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000681void GrContext::drawPaint(const GrPaint& origPaint) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000682 // set rect to be big enough to fill the space, but not super-huge, so we
683 // don't overflow fixed-point implementations
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000684 SkRect r;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000685 r.setLTRB(0, 0,
bsalomon@google.com81712882012-11-01 17:12:34 +0000686 SkIntToScalar(getRenderTarget()->width()),
687 SkIntToScalar(getRenderTarget()->height()));
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000688 SkMatrix inverse;
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000689 SkTCopyOnFirstWrite<GrPaint> paint(origPaint);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000690 AutoMatrix am;
egdanield78a1682014-07-09 10:41:26 -0700691 GR_CREATE_TRACE_MARKER_CONTEXT("GrContext::drawPaint", this);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000692
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000693 // We attempt to map r by the inverse matrix and draw that. mapRect will
694 // map the four corners and bound them with a new rect. This will not
695 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000696 if (!this->getMatrix().hasPerspective()) {
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000697 if (!fViewMatrix.invert(&inverse)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000698 GrPrintf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000699 return;
700 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000701 inverse.mapRect(&r);
702 } else {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000703 if (!am.setIdentity(this, paint.writable())) {
704 GrPrintf("Could not invert matrix\n");
705 return;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000706 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000707 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000708 // by definition this fills the entire clip, no need for AA
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000709 if (paint->isAntiAlias()) {
710 paint.writable()->setAntiAlias(false);
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000711 }
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000712 this->drawRect(*paint, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000713}
714
commit-bot@chromium.org03e3e892013-10-02 18:19:17 +0000715#ifdef SK_DEVELOPER
716void GrContext::dumpFontCache() const {
717 fFontCache->dump();
718}
719#endif
720
bsalomon@google.com205d4602011-04-25 12:43:45 +0000721////////////////////////////////////////////////////////////////////////////////
722
bsalomon@google.com27847de2011-02-22 20:59:41 +0000723/* create a triangle strip that strokes the specified triangle. There are 8
724 unique vertices, but we repreat the last 2 to close up. Alternatively we
725 could use an indices array, and then only send 8 verts, but not sure that
726 would be faster.
727 */
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000728static void setStrokeRectStrip(SkPoint verts[10], SkRect rect,
bsalomon@google.com81712882012-11-01 17:12:34 +0000729 SkScalar width) {
730 const SkScalar rad = SkScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000731 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000732
733 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
734 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
735 verts[2].set(rect.fRight - rad, rect.fTop + rad);
736 verts[3].set(rect.fRight + rad, rect.fTop - rad);
737 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
738 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
739 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
740 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
741 verts[8] = verts[0];
742 verts[9] = verts[1];
743}
744
bsalomonc30aaa02014-08-13 07:15:29 -0700745static inline bool is_irect(const SkRect& r) {
746 return SkScalarIsInt(r.fLeft) && SkScalarIsInt(r.fTop) &&
747 SkScalarIsInt(r.fRight) && SkScalarIsInt(r.fBottom);
748}
749
bsalomon@google.com205d4602011-04-25 12:43:45 +0000750static bool apply_aa_to_rect(GrDrawTarget* target,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000751 const SkRect& rect,
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000752 SkScalar strokeWidth,
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000753 const SkMatrix& combinedMatrix,
bsalomon9c0822a2014-08-11 11:07:48 -0700754 SkRect* devBoundRect) {
755 if (!target->getDrawState().canTweakAlphaForCoverage() &&
756 target->shouldDisableCoverageAAForBlend()) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000757#ifdef SK_DEBUG
bsalomon9c0822a2014-08-11 11:07:48 -0700758 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000759#endif
bsalomon9c0822a2014-08-11 11:07:48 -0700760 return false;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000761 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000762 const GrDrawState& drawState = target->getDrawState();
763 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000764 return false;
765 }
766
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000767#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000768 if (strokeWidth >= 0) {
769#endif
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000770 if (!combinedMatrix.preservesAxisAlignment()) {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000771 return false;
772 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000773
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000774#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000775 } else {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000776 if (!combinedMatrix.preservesRightAngles()) {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000777 return false;
778 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000779 }
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000780#endif
bsalomon@google.com205d4602011-04-25 12:43:45 +0000781
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000782 combinedMatrix.mapRect(devBoundRect, rect);
bsalomonc30aaa02014-08-13 07:15:29 -0700783 if (strokeWidth < 0) {
784 return !is_irect(*devBoundRect);
785 }
robertphillips@google.com28ac96e2013-05-13 13:38:35 +0000786
bsalomon9c0822a2014-08-11 11:07:48 -0700787 return true;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000788}
789
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000790static inline bool rect_contains_inclusive(const SkRect& rect, const SkPoint& point) {
791 return point.fX >= rect.fLeft && point.fX <= rect.fRight &&
792 point.fY >= rect.fTop && point.fY <= rect.fBottom;
793}
794
bsalomon@google.com27847de2011-02-22 20:59:41 +0000795void GrContext::drawRect(const GrPaint& paint,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000796 const SkRect& rect,
bsalomon01c8da12014-08-04 09:21:30 -0700797 const GrStrokeInfo* strokeInfo) {
egdanield58a0ba2014-06-11 10:30:05 -0700798 if (NULL != strokeInfo && strokeInfo->isDashed()) {
799 SkPath path;
800 path.addRect(rect);
801 this->drawPath(paint, path, *strokeInfo);
802 return;
803 }
804
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000805 AutoRestoreEffects are;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000806 AutoCheckFlush acf(this);
807 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700808 if (NULL == target) {
809 return;
810 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000811
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000812 GR_CREATE_TRACE_MARKER("GrContext::drawRect", target);
egdanield58a0ba2014-06-11 10:30:05 -0700813 SkScalar width = NULL == strokeInfo ? -1 : strokeInfo->getStrokeRec().getWidth();
bsalomon01c8da12014-08-04 09:21:30 -0700814 SkMatrix matrix = target->drawState()->getViewMatrix();
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000815
816 // Check if this is a full RT draw and can be replaced with a clear. We don't bother checking
817 // cases where the RT is fully inside a stroke.
818 if (width < 0) {
819 SkRect rtRect;
820 target->getDrawState().getRenderTarget()->getBoundsRect(&rtRect);
821 SkRect clipSpaceRTRect = rtRect;
822 bool checkClip = false;
823 if (NULL != this->getClip()) {
824 checkClip = true;
825 clipSpaceRTRect.offset(SkIntToScalar(this->getClip()->fOrigin.fX),
826 SkIntToScalar(this->getClip()->fOrigin.fY));
827 }
828 // Does the clip contain the entire RT?
829 if (!checkClip || target->getClip()->fClipStack->quickContains(clipSpaceRTRect)) {
830 SkMatrix invM;
bsalomon01c8da12014-08-04 09:21:30 -0700831 if (!matrix.invert(&invM)) {
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000832 return;
833 }
834 // Does the rect bound the RT?
835 SkPoint srcSpaceRTQuad[4];
836 invM.mapRectToQuad(srcSpaceRTQuad, rtRect);
837 if (rect_contains_inclusive(rect, srcSpaceRTQuad[0]) &&
838 rect_contains_inclusive(rect, srcSpaceRTQuad[1]) &&
839 rect_contains_inclusive(rect, srcSpaceRTQuad[2]) &&
840 rect_contains_inclusive(rect, srcSpaceRTQuad[3])) {
841 // Will it blend?
842 GrColor clearColor;
843 if (paint.isOpaqueAndConstantColor(&clearColor)) {
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000844 target->clear(NULL, clearColor, true);
commit-bot@chromium.org24ab3b02013-08-14 21:56:37 +0000845 return;
846 }
847 }
848 }
849 }
850
851 SkRect devBoundRect;
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000852 bool needAA = paint.isAntiAlias() &&
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +0000853 !target->getDrawState().getRenderTarget()->isMultisampled();
bsalomon9c0822a2014-08-11 11:07:48 -0700854 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix, &devBoundRect);
egdanield58a0ba2014-06-11 10:30:05 -0700855
856 const SkStrokeRec& strokeRec = strokeInfo->getStrokeRec();
857
bsalomon@google.com205d4602011-04-25 12:43:45 +0000858 if (doAA) {
bsalomon@google.com137f1342013-05-29 21:27:53 +0000859 GrDrawState::AutoViewMatrixRestore avmr;
860 if (!avmr.setIdentity(target->drawState())) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000861 return;
862 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000863 if (width >= 0) {
commit-bot@chromium.org6006d0f2013-11-06 10:08:21 +0000864 fAARectRenderer->strokeAARect(this->getGpu(), target, rect,
bsalomon01c8da12014-08-04 09:21:30 -0700865 matrix, devBoundRect,
bsalomon9c0822a2014-08-11 11:07:48 -0700866 strokeRec);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000867 } else {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000868 // filled AA rect
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000869 fAARectRenderer->fillAARect(this->getGpu(), target,
bsalomon9c0822a2014-08-11 11:07:48 -0700870 rect, matrix, devBoundRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000871 }
872 return;
873 }
874
bsalomon@google.com27847de2011-02-22 20:59:41 +0000875 if (width >= 0) {
876 // TODO: consider making static vertex buffers for these cases.
bsalomon@google.com64386952013-02-08 21:22:44 +0000877 // Hairline could be done by just adding closing vertex to
878 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000879
bsalomon@google.com27847de2011-02-22 20:59:41 +0000880 static const int worstCaseVertCount = 10;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000881 target->drawState()->setDefaultVertexAttribs();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000882 GrDrawTarget::AutoReleaseGeometry geo(target, worstCaseVertCount, 0);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000883
884 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000885 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000886 return;
887 }
888
889 GrPrimitiveType primType;
890 int vertCount;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000891 SkPoint* vertex = geo.positions();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000892
893 if (width > 0) {
894 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000895 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000896 setStrokeRectStrip(vertex, rect, width);
897 } else {
898 // hairline
899 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000900 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000901 vertex[0].set(rect.fLeft, rect.fTop);
902 vertex[1].set(rect.fRight, rect.fTop);
903 vertex[2].set(rect.fRight, rect.fBottom);
904 vertex[3].set(rect.fLeft, rect.fBottom);
905 vertex[4].set(rect.fLeft, rect.fTop);
906 }
907
bsalomon@google.com27847de2011-02-22 20:59:41 +0000908 target->drawNonIndexed(primType, 0, vertCount);
909 } else {
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000910 // filled BW rect
bsalomon01c8da12014-08-04 09:21:30 -0700911 target->drawSimpleRect(rect);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000912 }
913}
914
915void GrContext::drawRectToRect(const GrPaint& paint,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000916 const SkRect& dstRect,
917 const SkRect& localRect,
bsalomon@google.comc7818882013-03-20 19:19:53 +0000918 const SkMatrix* localMatrix) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000919 AutoRestoreEffects are;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000920 AutoCheckFlush acf(this);
921 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700922 if (NULL == target) {
923 return;
924 }
bsalomon@google.com64386952013-02-08 21:22:44 +0000925
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000926 GR_CREATE_TRACE_MARKER("GrContext::drawRectToRect", target);
927
bsalomon01c8da12014-08-04 09:21:30 -0700928 target->drawRect(dstRect, &localRect, localMatrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000929}
930
robertphillips@google.com42903302013-04-20 12:26:07 +0000931namespace {
932
933extern const GrVertexAttrib gPosUVColorAttribs[] = {
934 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding },
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000935 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kLocalCoord_GrVertexAttribBinding },
936 {kVec4ub_GrVertexAttribType, 2*sizeof(SkPoint), kColor_GrVertexAttribBinding}
robertphillips@google.com42903302013-04-20 12:26:07 +0000937};
938
egdaniel7b3d5ee2014-08-28 05:41:14 -0700939static const size_t kPosUVAttribsSize = 2 * sizeof(SkPoint);
940static const size_t kPosUVColorAttribsSize = 2 * sizeof(SkPoint) + sizeof(GrColor);
941
robertphillips@google.com42903302013-04-20 12:26:07 +0000942extern const GrVertexAttrib gPosColorAttribs[] = {
943 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000944 {kVec4ub_GrVertexAttribType, sizeof(SkPoint), kColor_GrVertexAttribBinding},
robertphillips@google.com42903302013-04-20 12:26:07 +0000945};
946
egdaniel7b3d5ee2014-08-28 05:41:14 -0700947static const size_t kPosAttribsSize = sizeof(SkPoint);
948static const size_t kPosColorAttribsSize = sizeof(SkPoint) + sizeof(GrColor);
949
robertphillips@google.com42903302013-04-20 12:26:07 +0000950static void set_vertex_attributes(GrDrawState* drawState,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000951 const SkPoint* texCoords,
robertphillips@google.com42903302013-04-20 12:26:07 +0000952 const GrColor* colors,
953 int* colorOffset,
954 int* texOffset) {
955 *texOffset = -1;
956 *colorOffset = -1;
957
958 if (NULL != texCoords && NULL != colors) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000959 *texOffset = sizeof(SkPoint);
960 *colorOffset = 2*sizeof(SkPoint);
egdaniel7b3d5ee2014-08-28 05:41:14 -0700961 drawState->setVertexAttribs<gPosUVColorAttribs>(3, kPosUVColorAttribsSize);
robertphillips@google.com42903302013-04-20 12:26:07 +0000962 } else if (NULL != texCoords) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000963 *texOffset = sizeof(SkPoint);
egdaniel7b3d5ee2014-08-28 05:41:14 -0700964 drawState->setVertexAttribs<gPosUVColorAttribs>(2, kPosUVAttribsSize);
robertphillips@google.com42903302013-04-20 12:26:07 +0000965 } else if (NULL != colors) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000966 *colorOffset = sizeof(SkPoint);
egdaniel7b3d5ee2014-08-28 05:41:14 -0700967 drawState->setVertexAttribs<gPosColorAttribs>(2, kPosColorAttribsSize);
robertphillips@google.com42903302013-04-20 12:26:07 +0000968 } else {
egdaniel7b3d5ee2014-08-28 05:41:14 -0700969 drawState->setVertexAttribs<gPosColorAttribs>(1, kPosAttribsSize);
robertphillips@google.com42903302013-04-20 12:26:07 +0000970 }
971}
972
973};
974
bsalomon@google.com27847de2011-02-22 20:59:41 +0000975void GrContext::drawVertices(const GrPaint& paint,
976 GrPrimitiveType primitiveType,
977 int vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000978 const SkPoint positions[],
979 const SkPoint texCoords[],
bsalomon@google.com27847de2011-02-22 20:59:41 +0000980 const GrColor colors[],
981 const uint16_t indices[],
982 int indexCount) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000983 AutoRestoreEffects are;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000984 AutoCheckFlush acf(this);
commit-bot@chromium.org5a567932014-01-08 21:26:09 +0000985 GrDrawTarget::AutoReleaseGeometry geo; // must be inside AutoCheckFlush scope
986
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +0000987 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -0700988 if (NULL == target) {
989 return;
990 }
egdaniele61c4112014-06-12 10:24:21 -0700991 GrDrawState* drawState = target->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000992
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000993 GR_CREATE_TRACE_MARKER("GrContext::drawVertices", target);
994
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000995 int colorOffset = -1, texOffset = -1;
robertphillips@google.com42903302013-04-20 12:26:07 +0000996 set_vertex_attributes(drawState, texCoords, colors, &colorOffset, &texOffset);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000997
egdaniel7b3d5ee2014-08-28 05:41:14 -0700998 size_t VertexStride = drawState->getVertexStride();
999 if (sizeof(SkPoint) != VertexStride) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +00001000 if (!geo.set(target, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001001 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +00001002 return;
1003 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001004 void* curVertex = geo.vertices();
1005
1006 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001007 *((SkPoint*)curVertex) = positions[i];
bsalomon@google.com27847de2011-02-22 20:59:41 +00001008
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001009 if (texOffset >= 0) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001010 *(SkPoint*)((intptr_t)curVertex + texOffset) = texCoords[i];
bsalomon@google.com27847de2011-02-22 20:59:41 +00001011 }
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001012 if (colorOffset >= 0) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001013 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
1014 }
egdaniel7b3d5ee2014-08-28 05:41:14 -07001015 curVertex = (void*)((intptr_t)curVertex + VertexStride);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001016 }
1017 } else {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +00001018 target->setVertexSourceToArray(positions, vertexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001019 }
1020
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001021 // we don't currently apply offscreen AA to this path. Need improved
bsalomon@google.com91958362011-06-13 17:58:13 +00001022 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001023
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001024 if (NULL != indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001025 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001026 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com0406b9e2013-04-02 21:00:15 +00001027 target->resetIndexSource();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001028 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001029 target->drawNonIndexed(primitiveType, 0, vertexCount);
1030 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001031}
1032
bsalomon@google.com06afe7b2011-04-26 15:31:40 +00001033///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com93c96602012-04-27 13:05:21 +00001034
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001035void GrContext::drawRRect(const GrPaint& paint,
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001036 const SkRRect& rrect,
egdanield58a0ba2014-06-11 10:30:05 -07001037 const GrStrokeInfo& strokeInfo) {
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001038 if (rrect.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001039 return;
1040 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001041
egdanield58a0ba2014-06-11 10:30:05 -07001042 if (strokeInfo.isDashed()) {
1043 SkPath path;
1044 path.addRRect(rrect);
1045 this->drawPath(paint, path, strokeInfo);
1046 return;
1047 }
1048
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001049 AutoRestoreEffects are;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +00001050 AutoCheckFlush acf(this);
1051 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001052 if (NULL == target) {
1053 return;
1054 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001055
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001056 GR_CREATE_TRACE_MARKER("GrContext::drawRRect", target);
1057
egdanield58a0ba2014-06-11 10:30:05 -07001058 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1059
1060 if (!fOvalRenderer->drawRRect(target, this, paint.isAntiAlias(), rrect, strokeRec)) {
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001061 SkPath path;
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001062 path.addRRect(rrect);
egdanield58a0ba2014-06-11 10:30:05 -07001063 this->internalDrawPath(target, paint.isAntiAlias(), path, strokeInfo);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001064 }
1065}
1066
1067///////////////////////////////////////////////////////////////////////////////
1068
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001069void GrContext::drawDRRect(const GrPaint& paint,
1070 const SkRRect& outer,
1071 const SkRRect& inner) {
1072 if (outer.isEmpty()) {
1073 return;
1074 }
1075
1076 AutoRestoreEffects are;
1077 AutoCheckFlush acf(this);
1078 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf);
1079
1080 GR_CREATE_TRACE_MARKER("GrContext::drawDRRect", target);
1081
1082 if (!fOvalRenderer->drawDRRect(target, this, paint.isAntiAlias(), outer, inner)) {
1083 SkPath path;
1084 path.addRRect(inner);
1085 path.addRRect(outer);
1086 path.setFillType(SkPath::kEvenOdd_FillType);
1087
egdanield58a0ba2014-06-11 10:30:05 -07001088 GrStrokeInfo fillRec(SkStrokeRec::kFill_InitStyle);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +00001089 this->internalDrawPath(target, paint.isAntiAlias(), path, fillRec);
1090 }
1091}
1092
1093///////////////////////////////////////////////////////////////////////////////
1094
bsalomon@google.com93c96602012-04-27 13:05:21 +00001095void GrContext::drawOval(const GrPaint& paint,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001096 const SkRect& oval,
egdanield58a0ba2014-06-11 10:30:05 -07001097 const GrStrokeInfo& strokeInfo) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001098 if (oval.isEmpty()) {
1099 return;
1100 }
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001101
egdanield58a0ba2014-06-11 10:30:05 -07001102 if (strokeInfo.isDashed()) {
1103 SkPath path;
1104 path.addOval(oval);
1105 this->drawPath(paint, path, strokeInfo);
1106 return;
1107 }
1108
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001109 AutoRestoreEffects are;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +00001110 AutoCheckFlush acf(this);
1111 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001112 if (NULL == target) {
1113 return;
1114 }
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001115
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001116 GR_CREATE_TRACE_MARKER("GrContext::drawOval", target);
1117
egdanield58a0ba2014-06-11 10:30:05 -07001118 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1119
1120
1121 if (!fOvalRenderer->drawOval(target, this, paint.isAntiAlias(), oval, strokeRec)) {
bsalomon@google.com93c96602012-04-27 13:05:21 +00001122 SkPath path;
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001123 path.addOval(oval);
egdanield58a0ba2014-06-11 10:30:05 -07001124 this->internalDrawPath(target, paint.isAntiAlias(), path, strokeInfo);
skia.committer@gmail.com98ded842013-01-23 07:06:17 +00001125 }
bsalomon@google.com150d2842012-01-12 20:19:56 +00001126}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001127
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001128// Can 'path' be drawn as a pair of filled nested rectangles?
1129static bool is_nested_rects(GrDrawTarget* target,
1130 const SkPath& path,
1131 const SkStrokeRec& stroke,
bsalomon9c0822a2014-08-11 11:07:48 -07001132 SkRect rects[2]) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001133 SkASSERT(stroke.isFillStyle());
1134
1135 if (path.isInverseFillType()) {
1136 return false;
1137 }
1138
1139 const GrDrawState& drawState = target->getDrawState();
1140
1141 // TODO: this restriction could be lifted if we were willing to apply
1142 // the matrix to all the points individually rather than just to the rect
1143 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
1144 return false;
1145 }
1146
bsalomon9c0822a2014-08-11 11:07:48 -07001147 if (!target->getDrawState().canTweakAlphaForCoverage() &&
1148 target->shouldDisableCoverageAAForBlend()) {
1149 return false;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001150 }
1151
1152 SkPath::Direction dirs[2];
1153 if (!path.isNestedRects(rects, dirs)) {
1154 return false;
1155 }
1156
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001157 if (SkPath::kWinding_FillType == path.getFillType() && dirs[0] == dirs[1]) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001158 // The two rects need to be wound opposite to each other
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001159 return false;
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001160 }
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001161
robertphillips@google.com8d3c6402013-08-20 12:11:31 +00001162 // Right now, nested rects where the margin is not the same width
1163 // all around do not render correctly
1164 const SkScalar* outer = rects[0].asScalars();
1165 const SkScalar* inner = rects[1].asScalars();
1166
1167 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
1168 for (int i = 1; i < 4; ++i) {
1169 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
1170 if (!SkScalarNearlyEqual(margin, temp)) {
1171 return false;
1172 }
1173 }
1174
1175 return true;
1176}
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001177
egdanield58a0ba2014-06-11 10:30:05 -07001178void GrContext::drawPath(const GrPaint& paint, const SkPath& path, const GrStrokeInfo& strokeInfo) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001179
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001180 if (path.isEmpty()) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001181 if (path.isInverseFillType()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001182 this->drawPaint(paint);
1183 }
1184 return;
1185 }
1186
egdanield58a0ba2014-06-11 10:30:05 -07001187 if (strokeInfo.isDashed()) {
egdaniele61c4112014-06-12 10:24:21 -07001188 SkPoint pts[2];
1189 if (path.isLine(pts)) {
1190 AutoRestoreEffects are;
1191 AutoCheckFlush acf(this);
1192 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001193 if (NULL == target) {
1194 return;
1195 }
egdaniele61c4112014-06-12 10:24:21 -07001196 GrDrawState* drawState = target->drawState();
1197
1198 SkMatrix origViewMatrix = drawState->getViewMatrix();
1199 GrDrawState::AutoViewMatrixRestore avmr;
1200 if (avmr.setIdentity(target->drawState())) {
1201 if (GrDashingEffect::DrawDashLine(pts, paint, strokeInfo, fGpu, target,
1202 origViewMatrix)) {
1203 return;
1204 }
1205 }
1206 }
1207
1208 // Filter dashed path into new path with the dashing applied
egdanield58a0ba2014-06-11 10:30:05 -07001209 const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo();
1210 SkTLazy<SkPath> effectPath;
1211 GrStrokeInfo newStrokeInfo(strokeInfo, false);
1212 SkStrokeRec* stroke = newStrokeInfo.getStrokeRecPtr();
1213 if (SkDashPath::FilterDashPath(effectPath.init(), path, stroke, NULL, info)) {
1214 this->drawPath(paint, *effectPath.get(), newStrokeInfo);
1215 return;
1216 }
1217
1218 this->drawPath(paint, path, newStrokeInfo);
1219 return;
1220 }
1221
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001222 // Note that internalDrawPath may sw-rasterize the path into a scratch texture.
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001223 // Scratch textures can be recycled after they are returned to the texture
1224 // cache. This presents a potential hazard for buffered drawing. However,
1225 // the writePixels that uploads to the scratch will perform a flush so we're
1226 // OK.
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001227 AutoRestoreEffects are;
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +00001228 AutoCheckFlush acf(this);
1229 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW, &are, &acf);
bsalomon41ebbdd2014-08-04 08:31:39 -07001230 if (NULL == target) {
1231 return;
1232 }
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001233 GrDrawState* drawState = target->drawState();
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001234
egdaniel93a37bc2014-07-21 13:47:57 -07001235 GR_CREATE_TRACE_MARKER1("GrContext::drawPath", target, "Is Convex", path.isConvex());
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001236
egdanield58a0ba2014-06-11 10:30:05 -07001237 const SkStrokeRec& strokeRec = strokeInfo.getStrokeRec();
1238
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001239 bool useCoverageAA = paint.isAntiAlias() && !drawState->getRenderTarget()->isMultisampled();
1240
egdanield58a0ba2014-06-11 10:30:05 -07001241 if (useCoverageAA && strokeRec.getWidth() < 0 && !path.isConvex()) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001242 // Concave AA paths are expensive - try to avoid them for special cases
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001243 SkRect rects[2];
1244
bsalomon9c0822a2014-08-11 11:07:48 -07001245 if (is_nested_rects(target, path, strokeRec, rects)) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001246 SkMatrix origViewMatrix = drawState->getViewMatrix();
bsalomon@google.com137f1342013-05-29 21:27:53 +00001247 GrDrawState::AutoViewMatrixRestore avmr;
1248 if (!avmr.setIdentity(target->drawState())) {
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001249 return;
1250 }
1251
bsalomon9c0822a2014-08-11 11:07:48 -07001252 fAARectRenderer->fillAANestedRects(this->getGpu(), target, rects, origViewMatrix);
robertphillips@google.com83d1a682013-05-17 12:50:27 +00001253 return;
1254 }
1255 }
1256
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001257 SkRect ovalRect;
1258 bool isOval = path.isOval(&ovalRect);
1259
skia.committer@gmail.com7e328512013-03-23 07:01:28 +00001260 if (!isOval || path.isInverseFillType()
egdanield58a0ba2014-06-11 10:30:05 -07001261 || !fOvalRenderer->drawOval(target, this, paint.isAntiAlias(), ovalRect, strokeRec)) {
1262 this->internalDrawPath(target, paint.isAntiAlias(), path, strokeInfo);
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001263 }
1264}
1265
bsalomon@google.com1b20a102013-11-08 14:42:56 +00001266void GrContext::internalDrawPath(GrDrawTarget* target, bool useAA, const SkPath& path,
egdanield58a0ba2014-06-11 10:30:05 -07001267 const GrStrokeInfo& strokeInfo) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001268 SkASSERT(!path.isEmpty());
commit-bot@chromium.org81312832013-03-22 18:34:09 +00001269
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +00001270 GR_CREATE_TRACE_MARKER("GrContext::internalDrawPath", target);
1271
1272
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001273 // An Assumption here is that path renderer would use some form of tweaking
1274 // the src color (either the input alpha or in the frag shader) to implement
1275 // aa. If we have some future driver-mojo path AA that can do the right
1276 // thing WRT to the blend then we'll need some query on the PR.
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001277 bool useCoverageAA = useAA &&
1278 !target->getDrawState().getRenderTarget()->isMultisampled() &&
1279 !target->shouldDisableCoverageAAForBlend();
bsalomon@google.com289533a2011-10-27 12:34:25 +00001280
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001281
1282 GrPathRendererChain::DrawType type =
1283 useCoverageAA ? GrPathRendererChain::kColorAntiAlias_DrawType :
robertphillips@google.come79f3202014-02-11 16:30:21 +00001284 GrPathRendererChain::kColor_DrawType;
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001285
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001286 const SkPath* pathPtr = &path;
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001287 SkTLazy<SkPath> tmpPath;
egdanield58a0ba2014-06-11 10:30:05 -07001288 SkTCopyOnFirstWrite<SkStrokeRec> stroke(strokeInfo.getStrokeRec());
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001289
1290 // Try a 1st time without stroking the path and without allowing the SW renderer
robertphillips@google.come79f3202014-02-11 16:30:21 +00001291 GrPathRenderer* pr = this->getPathRenderer(*pathPtr, *stroke, target, false, type);
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001292
robertphillips@google.come79f3202014-02-11 16:30:21 +00001293 if (NULL == pr) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001294 if (!GrPathRenderer::IsStrokeHairlineOrEquivalent(*stroke, this->getMatrix(), NULL)) {
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001295 // It didn't work the 1st time, so try again with the stroked path
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +00001296 if (stroke->applyToPath(tmpPath.init(), *pathPtr)) {
1297 pathPtr = tmpPath.get();
1298 stroke.writable()->setFillStyle();
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001299 if (pathPtr->isEmpty()) {
1300 return;
1301 }
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001302 }
1303 }
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +00001304
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001305 // This time, allow SW renderer
robertphillips@google.come79f3202014-02-11 16:30:21 +00001306 pr = this->getPathRenderer(*pathPtr, *stroke, target, true, type);
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001307 }
1308
robertphillips@google.come79f3202014-02-11 16:30:21 +00001309 if (NULL == pr) {
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +00001310#ifdef SK_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001311 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001312#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001313 return;
1314 }
1315
robertphillips@google.come79f3202014-02-11 16:30:21 +00001316 pr->drawPath(*pathPtr, *stroke, target, useCoverageAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001317}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001318
bsalomon@google.com27847de2011-02-22 20:59:41 +00001319////////////////////////////////////////////////////////////////////////////////
1320
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001321void GrContext::flush(int flagsBitfield) {
robertphillips@google.come7db8d62013-07-04 11:48:52 +00001322 if (NULL == fDrawBuffer) {
1323 return;
1324 }
1325
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001326 if (kDiscard_FlushBit & flagsBitfield) {
1327 fDrawBuffer->reset();
1328 } else {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001329 fDrawBuffer->flush();
junov@google.com53a55842011-06-08 22:55:10 +00001330 }
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +00001331 fFlushToReduceCacheSize = false;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001332}
1333
bsalomon@google.com9c680582013-02-06 18:17:50 +00001334bool GrContext::writeTexturePixels(GrTexture* texture,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001335 int left, int top, int width, int height,
1336 GrPixelConfig config, const void* buffer, size_t rowBytes,
1337 uint32_t flags) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001338 ASSERT_OWNED_RESOURCE(texture);
1339
bsalomon@google.com9c680582013-02-06 18:17:50 +00001340 if ((kUnpremul_PixelOpsFlag & flags) || !fGpu->canWriteTexturePixels(texture, config)) {
1341 if (NULL != texture->asRenderTarget()) {
1342 return this->writeRenderTargetPixels(texture->asRenderTarget(),
1343 left, top, width, height,
1344 config, buffer, rowBytes, flags);
1345 } else {
1346 return false;
1347 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001348 }
bsalomon@google.com9c680582013-02-06 18:17:50 +00001349
bsalomon@google.com6f379512011-11-16 20:36:03 +00001350 if (!(kDontFlush_PixelOpsFlag & flags)) {
1351 this->flush();
1352 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001353
bsalomon@google.com9c680582013-02-06 18:17:50 +00001354 return fGpu->writeTexturePixels(texture, left, top, width, height,
1355 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001356}
1357
bsalomon@google.com0342a852012-08-20 19:22:38 +00001358bool GrContext::readTexturePixels(GrTexture* texture,
1359 int left, int top, int width, int height,
1360 GrPixelConfig config, void* buffer, size_t rowBytes,
1361 uint32_t flags) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001362 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001363
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001364 GrRenderTarget* target = texture->asRenderTarget();
1365 if (NULL != target) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001366 return this->readRenderTargetPixels(target,
1367 left, top, width, height,
1368 config, buffer, rowBytes,
1369 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001370 } else {
commit-bot@chromium.org3f80dd52013-10-23 15:32:39 +00001371 // TODO: make this more efficient for cases where we're reading the entire
1372 // texture, i.e., use GetTexImage() instead
1373
1374 // create scratch rendertarget and read from that
1375 GrAutoScratchTexture ast;
1376 GrTextureDesc desc;
1377 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1378 desc.fWidth = width;
1379 desc.fHeight = height;
1380 desc.fConfig = config;
1381 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
1382 ast.set(this, desc, kExact_ScratchTexMatch);
1383 GrTexture* dst = ast.texture();
1384 if (NULL != dst && NULL != (target = dst->asRenderTarget())) {
1385 this->copyTexture(texture, target, NULL);
1386 return this->readRenderTargetPixels(target,
1387 left, top, width, height,
1388 config, buffer, rowBytes,
1389 flags);
1390 }
skia.committer@gmail.com583b18a2013-10-24 07:01:59 +00001391
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001392 return false;
1393 }
1394}
1395
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001396#include "SkConfig8888.h"
1397
reed@google.com7111d462014-03-25 16:20:24 +00001398// toggles between RGBA and BGRA
1399static SkColorType toggle_colortype32(SkColorType ct) {
1400 if (kRGBA_8888_SkColorType == ct) {
1401 return kBGRA_8888_SkColorType;
1402 } else {
1403 SkASSERT(kBGRA_8888_SkColorType == ct);
1404 return kRGBA_8888_SkColorType;
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001405 }
1406}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001407
bsalomon@google.com0342a852012-08-20 19:22:38 +00001408bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
1409 int left, int top, int width, int height,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001410 GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001411 uint32_t flags) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001412 ASSERT_OWNED_RESOURCE(target);
1413
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001414 if (NULL == target) {
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001415 target = fRenderTarget.get();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001416 if (NULL == target) {
1417 return false;
1418 }
1419 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001420
bsalomon@google.com6f379512011-11-16 20:36:03 +00001421 if (!(kDontFlush_PixelOpsFlag & flags)) {
1422 this->flush();
1423 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001424
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001425 // Determine which conversions have to be applied: flipY, swapRAnd, and/or unpremul.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001426
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001427 // If fGpu->readPixels would incur a y-flip cost then we will read the pixels upside down. We'll
1428 // either do the flipY by drawing into a scratch with a matrix or on the cpu after the read.
1429 bool flipY = fGpu->readPixelsWillPayForYFlip(target, left, top,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001430 width, height, dstConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001431 rowBytes);
bsalomon@google.com9c680582013-02-06 18:17:50 +00001432 // We ignore the preferred config if it is different than our config unless it is an R/B swap.
1433 // In that case we'll perform an R and B swap while drawing to a scratch texture of the swapped
1434 // config. Then we will call readPixels on the scratch with the swapped config. The swaps during
1435 // the draw cancels out the fact that we call readPixels with a config that is R/B swapped from
1436 // dstConfig.
1437 GrPixelConfig readConfig = dstConfig;
1438 bool swapRAndB = false;
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +00001439 if (GrPixelConfigSwapRAndB(dstConfig) ==
1440 fGpu->preferredReadPixelsConfig(dstConfig, target->config())) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001441 readConfig = GrPixelConfigSwapRAndB(readConfig);
1442 swapRAndB = true;
1443 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001444
bsalomon@google.com0342a852012-08-20 19:22:38 +00001445 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001446
bsalomon@google.com9c680582013-02-06 18:17:50 +00001447 if (unpremul && !GrPixelConfigIs8888(dstConfig)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001448 // The unpremul flag is only allowed for these two configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001449 return false;
1450 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001451
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001452 // If the src is a texture and we would have to do conversions after read pixels, we instead
1453 // do the conversions by drawing the src to a scratch texture. If we handle any of the
1454 // conversions in the draw we set the corresponding bool to false so that we don't reapply it
1455 // on the read back pixels.
1456 GrTexture* src = target->asTexture();
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001457 GrAutoScratchTexture ast;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001458 if (NULL != src && (swapRAndB || unpremul || flipY)) {
1459 // Make the scratch a render target because we don't have a robust readTexturePixels as of
1460 // yet. It calls this function.
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001461 GrTextureDesc desc;
1462 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1463 desc.fWidth = width;
1464 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001465 desc.fConfig = readConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001466 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001467
bsalomon@google.com9c680582013-02-06 18:17:50 +00001468 // 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 +00001469 // match the passed rect. However, if we see many different size rectangles we will trash
1470 // our texture cache and pay the cost of creating and destroying many textures. So, we only
1471 // request an exact match when the caller is reading an entire RT.
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001472 ScratchTexMatch match = kApprox_ScratchTexMatch;
1473 if (0 == left &&
1474 0 == top &&
1475 target->width() == width &&
1476 target->height() == height &&
1477 fGpu->fullReadPixelsIsFasterThanPartial()) {
1478 match = kExact_ScratchTexMatch;
1479 }
1480 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001481 GrTexture* texture = ast.texture();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001482 if (texture) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001483 // compute a matrix to perform the draw
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001484 SkMatrix textureMatrix;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001485 textureMatrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001486 textureMatrix.postIDiv(src->width(), src->height());
1487
bsalomon83d081a2014-07-08 09:56:10 -07001488 SkAutoTUnref<const GrEffect> effect;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001489 if (unpremul) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001490 effect.reset(this->createPMToUPMEffect(src, swapRAndB, textureMatrix));
1491 if (NULL != effect) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001492 unpremul = false; // we no longer need to do this on CPU after the read back.
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001493 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001494 }
1495 // If we failed to create a PM->UPM effect and have no other conversions to perform then
1496 // there is no longer any point to using the scratch.
bsalomon@google.comadc65362013-01-28 14:26:09 +00001497 if (NULL != effect || flipY || swapRAndB) {
1498 if (!effect) {
1499 effect.reset(GrConfigConversionEffect::Create(
1500 src,
1501 swapRAndB,
1502 GrConfigConversionEffect::kNone_PMConversion,
1503 textureMatrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001504 }
1505 swapRAndB = false; // we will handle the swap in the draw.
bsalomon@google.comc4364992011-11-07 15:54:49 +00001506
robertphillips@google.com13f181f2013-03-02 12:02:08 +00001507 // We protect the existing geometry here since it may not be
1508 // clear to the caller that a draw operation (i.e., drawSimpleRect)
1509 // can be invoked in this method
1510 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001511 GrDrawState* drawState = fGpu->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001512 SkASSERT(effect);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001513 drawState->addColorEffect(effect);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001514
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001515 drawState->setRenderTarget(texture->asRenderTarget());
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001516 SkRect rect = SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
bsalomon01c8da12014-08-04 09:21:30 -07001517 fGpu->drawSimpleRect(rect);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001518 // we want to read back from the scratch's origin
1519 left = 0;
1520 top = 0;
1521 target = texture->asRenderTarget();
1522 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001523 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001524 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001525 if (!fGpu->readPixels(target,
1526 left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001527 readConfig, buffer, rowBytes)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001528 return false;
1529 }
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001530 // Perform any conversions we weren't able to perform using a scratch texture.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001531 if (unpremul || swapRAndB) {
reed@google.com7111d462014-03-25 16:20:24 +00001532 SkDstPixelInfo dstPI;
1533 if (!GrPixelConfig2ColorType(dstConfig, &dstPI.fColorType)) {
1534 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001535 }
reed@google.com7111d462014-03-25 16:20:24 +00001536 dstPI.fAlphaType = kUnpremul_SkAlphaType;
1537 dstPI.fPixels = buffer;
1538 dstPI.fRowBytes = rowBytes;
1539
1540 SkSrcPixelInfo srcPI;
1541 srcPI.fColorType = swapRAndB ? toggle_colortype32(dstPI.fColorType) : dstPI.fColorType;
1542 srcPI.fAlphaType = kPremul_SkAlphaType;
1543 srcPI.fPixels = buffer;
1544 srcPI.fRowBytes = rowBytes;
1545
1546 return srcPI.convertPixelsTo(&dstPI, width, height);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001547 }
1548 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001549}
1550
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001551void GrContext::resolveRenderTarget(GrRenderTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001552 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001553 ASSERT_OWNED_RESOURCE(target);
1554 // In the future we may track whether there are any pending draws to this
1555 // target. We don't today so we always perform a flush. We don't promise
1556 // this to our clients, though.
1557 this->flush();
bsalomon41ebbdd2014-08-04 08:31:39 -07001558 if (NULL != fGpu) {
1559 fGpu->resolveRenderTarget(target);
1560 }
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001561}
1562
bsalomon41ebbdd2014-08-04 08:31:39 -07001563void GrContext::discardRenderTarget(GrRenderTarget* renderTarget) {
1564 SkASSERT(renderTarget);
1565 ASSERT_OWNED_RESOURCE(renderTarget);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001566 AutoRestoreEffects are;
1567 AutoCheckFlush acf(this);
bsalomon41ebbdd2014-08-04 08:31:39 -07001568 GrDrawTarget* target = this->prepareToDraw(NULL, BUFFERED_DRAW, &are, &acf);
1569 if (NULL == target) {
1570 return;
1571 }
1572 target->discard(renderTarget);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +00001573}
1574
scroggo@google.coma2a31922012-12-07 19:14:45 +00001575void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst, const SkIPoint* topLeft) {
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001576 if (NULL == src || NULL == dst) {
1577 return;
1578 }
1579 ASSERT_OWNED_RESOURCE(src);
1580
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001581 // Writes pending to the source texture are not tracked, so a flush
1582 // is required to ensure that the copy captures the most recent contents
bsalomon@google.comadc65362013-01-28 14:26:09 +00001583 // of the source texture. See similar behavior in
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001584 // GrContext::resolveRenderTarget.
1585 this->flush();
1586
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001587 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001588 GrDrawState* drawState = fGpu->drawState();
1589 drawState->setRenderTarget(dst);
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001590 SkMatrix sampleM;
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001591 sampleM.setIDiv(src->width(), src->height());
scroggo@google.coma2a31922012-12-07 19:14:45 +00001592 SkIRect srcRect = SkIRect::MakeWH(dst->width(), dst->height());
1593 if (NULL != topLeft) {
1594 srcRect.offset(*topLeft);
1595 }
1596 SkIRect srcBounds = SkIRect::MakeWH(src->width(), src->height());
1597 if (!srcRect.intersect(srcBounds)) {
1598 return;
1599 }
1600 sampleM.preTranslate(SkIntToScalar(srcRect.fLeft), SkIntToScalar(srcRect.fTop));
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001601 drawState->addColorTextureEffect(src, sampleM);
scroggo@google.coma2a31922012-12-07 19:14:45 +00001602 SkRect dstR = SkRect::MakeWH(SkIntToScalar(srcRect.width()), SkIntToScalar(srcRect.height()));
bsalomon01c8da12014-08-04 09:21:30 -07001603 fGpu->drawSimpleRect(dstR);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001604}
1605
bsalomon@google.com9c680582013-02-06 18:17:50 +00001606bool GrContext::writeRenderTargetPixels(GrRenderTarget* target,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001607 int left, int top, int width, int height,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001608 GrPixelConfig srcConfig,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001609 const void* buffer,
1610 size_t rowBytes,
1611 uint32_t flags) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001612 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001613
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001614 if (NULL == target) {
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001615 target = fRenderTarget.get();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001616 if (NULL == target) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001617 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +00001618 }
1619 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001620
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001621 // TODO: when underlying api has a direct way to do this we should use it (e.g. glDrawPixels on
1622 // desktop GL).
1623
1624 // We will always call some form of writeTexturePixels and we will pass our flags on to it.
1625 // Thus, we don't perform a flush here since that call will do it (if the kNoFlush flag isn't
1626 // set.)
bsalomon@google.com27847de2011-02-22 20:59:41 +00001627
bsalomon@google.com0342a852012-08-20 19:22:38 +00001628 // If the RT is also a texture and we don't have to premultiply then take the texture path.
1629 // We expect to be at least as fast or faster since it doesn't use an intermediate texture as
1630 // we do below.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001631
commit-bot@chromium.org43823302013-09-25 20:57:51 +00001632#if !defined(SK_BUILD_FOR_MAC)
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001633 // At least some drivers on the Mac get confused when glTexImage2D is called on a texture
1634 // attached to an FBO. The FBO still sees the old image. TODO: determine what OS versions and/or
1635 // HW is affected.
bsalomon@google.com9c680582013-02-06 18:17:50 +00001636 if (NULL != target->asTexture() && !(kUnpremul_PixelOpsFlag & flags) &&
1637 fGpu->canWriteTexturePixels(target->asTexture(), srcConfig)) {
1638 return this->writeTexturePixels(target->asTexture(),
1639 left, top, width, height,
1640 srcConfig, buffer, rowBytes, flags);
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001641 }
1642#endif
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001643
bsalomon@google.com9c680582013-02-06 18:17:50 +00001644 // We ignore the preferred config unless it is a R/B swap of the src config. In that case
1645 // we will upload the original src data to a scratch texture but we will spoof it as the swapped
1646 // config. This scratch will then have R and B swapped. We correct for this by swapping again
1647 // when drawing the scratch to the dst using a conversion effect.
1648 bool swapRAndB = false;
1649 GrPixelConfig writeConfig = srcConfig;
commit-bot@chromium.org5d1d79a2013-05-24 18:52:52 +00001650 if (GrPixelConfigSwapRAndB(srcConfig) ==
1651 fGpu->preferredWritePixelsConfig(srcConfig, target->config())) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001652 writeConfig = GrPixelConfigSwapRAndB(srcConfig);
1653 swapRAndB = true;
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001654 }
1655
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001656 GrTextureDesc desc;
1657 desc.fWidth = width;
1658 desc.fHeight = height;
bsalomon@google.com9c680582013-02-06 18:17:50 +00001659 desc.fConfig = writeConfig;
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001660 GrAutoScratchTexture ast(this, desc);
1661 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001662 if (NULL == texture) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001663 return false;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001664 }
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001665
bsalomon83d081a2014-07-08 09:56:10 -07001666 SkAutoTUnref<const GrEffect> effect;
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001667 SkMatrix textureMatrix;
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001668 textureMatrix.setIDiv(texture->width(), texture->height());
1669
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001670 // allocate a tmp buffer and sw convert the pixels to premul
1671 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
1672
1673 if (kUnpremul_PixelOpsFlag & flags) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001674 if (!GrPixelConfigIs8888(srcConfig)) {
1675 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001676 }
bsalomon@google.comadc65362013-01-28 14:26:09 +00001677 effect.reset(this->createUPMToPMEffect(texture, swapRAndB, textureMatrix));
bsalomon@google.com9c680582013-02-06 18:17:50 +00001678 // handle the unpremul step on the CPU if we couldn't create an effect to do it.
bsalomon@google.comadc65362013-01-28 14:26:09 +00001679 if (NULL == effect) {
reed@google.com7111d462014-03-25 16:20:24 +00001680 SkSrcPixelInfo srcPI;
1681 if (!GrPixelConfig2ColorType(srcConfig, &srcPI.fColorType)) {
1682 return false;
1683 }
1684 srcPI.fAlphaType = kUnpremul_SkAlphaType;
1685 srcPI.fPixels = buffer;
1686 srcPI.fRowBytes = rowBytes;
1687
commit-bot@chromium.org231f6b82014-03-25 13:38:44 +00001688 tmpPixels.reset(width * height);
reed@google.com7111d462014-03-25 16:20:24 +00001689
1690 SkDstPixelInfo dstPI;
1691 dstPI.fColorType = srcPI.fColorType;
1692 dstPI.fAlphaType = kPremul_SkAlphaType;
1693 dstPI.fPixels = tmpPixels.get();
1694 dstPI.fRowBytes = 4 * width;
1695
1696 if (!srcPI.convertPixelsTo(&dstPI, width, height)) {
1697 return false;
1698 }
1699
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001700 buffer = tmpPixels.get();
1701 rowBytes = 4 * width;
1702 }
1703 }
bsalomon@google.comadc65362013-01-28 14:26:09 +00001704 if (NULL == effect) {
1705 effect.reset(GrConfigConversionEffect::Create(texture,
1706 swapRAndB,
1707 GrConfigConversionEffect::kNone_PMConversion,
1708 textureMatrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001709 }
1710
bsalomon@google.com9c680582013-02-06 18:17:50 +00001711 if (!this->writeTexturePixels(texture,
1712 0, 0, width, height,
1713 writeConfig, buffer, rowBytes,
1714 flags & ~kUnpremul_PixelOpsFlag)) {
1715 return false;
1716 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001717
robertphillips@google.com13f181f2013-03-02 12:02:08 +00001718 // writeRenderTargetPixels can be called in the midst of drawing another
skia.committer@gmail.com0c23faf2013-03-03 07:16:29 +00001719 // object (e.g., when uploading a SW path rendering to the gpu while
robertphillips@google.com13f181f2013-03-02 12:02:08 +00001720 // drawing a rect) so preserve the current geometry.
bsalomon@google.com137f1342013-05-29 21:27:53 +00001721 SkMatrix matrix;
1722 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
1723 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &matrix);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001724 GrDrawState* drawState = fGpu->drawState();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001725 SkASSERT(effect);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001726 drawState->addColorEffect(effect);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001727
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001728 drawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001729
bsalomon01c8da12014-08-04 09:21:30 -07001730 fGpu->drawSimpleRect(SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)));
bsalomon@google.com9c680582013-02-06 18:17:50 +00001731 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001732}
1733////////////////////////////////////////////////////////////////////////////////
1734
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001735GrDrawTarget* GrContext::prepareToDraw(const GrPaint* paint,
1736 BufferedDraw buffered,
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +00001737 AutoRestoreEffects* are,
1738 AutoCheckFlush* acf) {
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001739 // All users of this draw state should be freeing up all effects when they're done.
1740 // Otherwise effects that own resources may keep those resources alive indefinitely.
joshualittbd769d02014-09-04 08:56:46 -07001741 SkASSERT(0 == fDrawState->numColorStages() && 0 == fDrawState->numCoverageStages() &&
1742 !fDrawState->hasGeometryProcessor());
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001743
bsalomon41ebbdd2014-08-04 08:31:39 -07001744 if (NULL == fGpu) {
1745 return NULL;
1746 }
1747
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001748 if (kNo_BufferedDraw == buffered && kYes_BufferedDraw == fLastDrawWasBuffered) {
robertphillips@google.com1267fbd2013-07-03 18:37:27 +00001749 fDrawBuffer->flush();
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001750 fLastDrawWasBuffered = kNo_BufferedDraw;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001751 }
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001752 ASSERT_OWNED_RESOURCE(fRenderTarget.get());
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001753 if (NULL != paint) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001754 SkASSERT(NULL != are);
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +00001755 SkASSERT(NULL != acf);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001756 are->set(fDrawState);
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001757 fDrawState->setFromPaint(*paint, fViewMatrix, fRenderTarget.get());
bsalomon@google.comaf84e742012-10-05 13:23:24 +00001758#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
1759 if ((paint->hasMask() || 0xff != paint->fCoverage) &&
bsalomon62c447d2014-08-08 08:08:50 -07001760 !fDrawState->couldApplyCoverage(fGpu->caps())) {
bsalomon@google.comaf84e742012-10-05 13:23:24 +00001761 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1762 }
1763#endif
bsalomon9c0822a2014-08-11 11:07:48 -07001764 // Clear any vertex attributes configured for the previous use of the
1765 // GrDrawState which can effect which blend optimizations are in effect.
1766 fDrawState->setDefaultVertexAttribs();
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001767 } else {
bsalomon@google.com137f1342013-05-29 21:27:53 +00001768 fDrawState->reset(fViewMatrix);
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001769 fDrawState->setRenderTarget(fRenderTarget.get());
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001770 }
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001771 GrDrawTarget* target;
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001772 if (kYes_BufferedDraw == buffered) {
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001773 fLastDrawWasBuffered = kYes_BufferedDraw;
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001774 target = fDrawBuffer;
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001775 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001776 SkASSERT(kNo_BufferedDraw == buffered);
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001777 fLastDrawWasBuffered = kNo_BufferedDraw;
1778 target = fGpu;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001779 }
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001780 fDrawState->setState(GrDrawState::kClip_StateBit, NULL != fClip &&
1781 !fClip->fClipStack->isWideOpen());
1782 target->setClip(fClip);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001783 SkASSERT(fDrawState == target->drawState());
commit-bot@chromium.orgbb6a3172013-05-28 17:25:49 +00001784 return target;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001785}
1786
robertphillips@google.com72176b22012-05-23 13:19:12 +00001787/*
1788 * This method finds a path renderer that can draw the specified path on
1789 * the provided target.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001790 * Due to its expense, the software path renderer has split out so it can
robertphillips@google.com72176b22012-05-23 13:19:12 +00001791 * can be individually allowed/disallowed via the "allowSW" boolean.
1792 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001793GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001794 const SkStrokeRec& stroke,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001795 const GrDrawTarget* target,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001796 bool allowSW,
1797 GrPathRendererChain::DrawType drawType,
1798 GrPathRendererChain::StencilSupport* stencilSupport) {
1799
bsalomon@google.com30085192011-08-19 15:42:31 +00001800 if (NULL == fPathRendererChain) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001801 fPathRendererChain = SkNEW_ARGS(GrPathRendererChain, (this));
bsalomon@google.com30085192011-08-19 15:42:31 +00001802 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001803
sugoi@google.com12b4e272012-12-06 20:13:11 +00001804 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path,
1805 stroke,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001806 target,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001807 drawType,
1808 stencilSupport);
robertphillips@google.com72176b22012-05-23 13:19:12 +00001809
1810 if (NULL == pr && allowSW) {
1811 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001812 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001813 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001814 pr = fSoftwarePathRenderer;
1815 }
1816
1817 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001818}
1819
bsalomon@google.com27847de2011-02-22 20:59:41 +00001820////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +00001821bool GrContext::isConfigRenderable(GrPixelConfig config, bool withMSAA) const {
1822 return fGpu->caps()->isConfigRenderable(config, withMSAA);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001823}
1824
commit-bot@chromium.orgb471a322014-03-10 07:40:03 +00001825int GrContext::getRecommendedSampleCount(GrPixelConfig config,
1826 SkScalar dpi) const {
1827 if (!this->isConfigRenderable(config, true)) {
1828 return 0;
1829 }
1830 int chosenSampleCount = 0;
1831 if (fGpu->caps()->pathRenderingSupport()) {
1832 if (dpi >= 250.0f) {
1833 chosenSampleCount = 4;
1834 } else {
1835 chosenSampleCount = 16;
1836 }
1837 }
1838 return chosenSampleCount <= fGpu->caps()->maxSampleCount() ?
1839 chosenSampleCount : 0;
1840}
1841
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001842void GrContext::setupDrawBuffer() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +00001843 SkASSERT(NULL == fDrawBuffer);
1844 SkASSERT(NULL == fDrawBufferVBAllocPool);
1845 SkASSERT(NULL == fDrawBufferIBAllocPool);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001846
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001847 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001848 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001849 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001850 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001851 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001852 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001853 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001854 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001855
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001856 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001857 fDrawBufferVBAllocPool,
1858 fDrawBufferIBAllocPool));
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001859
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001860 fDrawBuffer->setDrawState(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001861}
1862
bsalomon@google.com21c10c52013-06-13 17:44:07 +00001863GrDrawTarget* GrContext::getTextTarget() {
commit-bot@chromium.org5dbb1492013-10-04 16:23:58 +00001864 return this->prepareToDraw(NULL, BUFFERED_DRAW, NULL, NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001865}
1866
1867const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1868 return fGpu->getQuadIndexBuffer();
1869}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001870
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001871namespace {
1872void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
1873 GrConfigConversionEffect::PMConversion pmToUPM;
1874 GrConfigConversionEffect::PMConversion upmToPM;
1875 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
1876 *pmToUPMValue = pmToUPM;
1877 *upmToPMValue = upmToPM;
1878}
1879}
1880
bsalomon83d081a2014-07-08 09:56:10 -07001881const GrEffect* GrContext::createPMToUPMEffect(GrTexture* texture,
1882 bool swapRAndB,
1883 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001884 if (!fDidTestPMConversions) {
1885 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001886 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001887 }
1888 GrConfigConversionEffect::PMConversion pmToUPM =
1889 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
1890 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001891 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001892 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001893 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001894 }
1895}
1896
bsalomon83d081a2014-07-08 09:56:10 -07001897const GrEffect* GrContext::createUPMToPMEffect(GrTexture* texture,
1898 bool swapRAndB,
1899 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001900 if (!fDidTestPMConversions) {
1901 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001902 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001903 }
1904 GrConfigConversionEffect::PMConversion upmToPM =
1905 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
1906 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001907 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001908 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001909 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001910 }
1911}
1912
commit-bot@chromium.org5c8ee252013-11-01 15:23:44 +00001913GrPath* GrContext::createPath(const SkPath& inPath, const SkStrokeRec& stroke) {
1914 SkASSERT(fGpu->caps()->pathRenderingSupport());
1915
commit-bot@chromium.org95c20032014-05-09 14:29:32 +00001916 // TODO: now we add to fResourceCache. This should change to fResourceCache.
commit-bot@chromium.org5c8ee252013-11-01 15:23:44 +00001917 GrResourceKey resourceKey = GrPath::ComputeKey(inPath, stroke);
commit-bot@chromium.org95c20032014-05-09 14:29:32 +00001918 GrPath* path = static_cast<GrPath*>(fResourceCache->find(resourceKey));
commit-bot@chromium.org5c8ee252013-11-01 15:23:44 +00001919 if (NULL != path && path->isEqualTo(inPath, stroke)) {
1920 path->ref();
1921 } else {
1922 path = fGpu->createPath(inPath, stroke);
commit-bot@chromium.org95c20032014-05-09 14:29:32 +00001923 fResourceCache->purgeAsNeeded(1, path->gpuMemorySize());
1924 fResourceCache->addResource(resourceKey, path);
commit-bot@chromium.org5c8ee252013-11-01 15:23:44 +00001925 }
1926 return path;
1927}
1928
bsalomon6d3fe022014-07-25 08:35:45 -07001929void GrContext::addResourceToCache(const GrResourceKey& resourceKey, GrGpuResource* resource) {
commit-bot@chromium.org95c20032014-05-09 14:29:32 +00001930 fResourceCache->purgeAsNeeded(1, resource->gpuMemorySize());
1931 fResourceCache->addResource(resourceKey, resource);
commit-bot@chromium.org95a2b0e2014-05-05 19:21:16 +00001932}
1933
bsalomon6d3fe022014-07-25 08:35:45 -07001934GrGpuResource* GrContext::findAndRefCachedResource(const GrResourceKey& resourceKey) {
1935 GrGpuResource* resource = fResourceCache->find(resourceKey);
commit-bot@chromium.org95a2b0e2014-05-05 19:21:16 +00001936 SkSafeRef(resource);
1937 return resource;
1938}
1939
egdanielbbcb38d2014-06-19 10:19:29 -07001940void GrContext::addGpuTraceMarker(const GrGpuTraceMarker* marker) {
1941 fGpu->addGpuTraceMarker(marker);
1942 if (NULL != fDrawBuffer) {
1943 fDrawBuffer->addGpuTraceMarker(marker);
1944 }
1945}
1946
1947void GrContext::removeGpuTraceMarker(const GrGpuTraceMarker* marker) {
1948 fGpu->removeGpuTraceMarker(marker);
1949 if (NULL != fDrawBuffer) {
1950 fDrawBuffer->removeGpuTraceMarker(marker);
1951 }
1952}
1953
bsalomon@google.comc4364992011-11-07 15:54:49 +00001954///////////////////////////////////////////////////////////////////////////////
robertphillips@google.com59552022012-08-31 13:07:37 +00001955#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001956void GrContext::printCacheStats() const {
commit-bot@chromium.org95c20032014-05-09 14:29:32 +00001957 fResourceCache->printStats();
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001958}
1959#endif