blob: cf74c8ed6094bbe803dbde1e5253c77643bb3c7d [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.comb505a122012-05-31 18:40:36 +000012#include "effects/GrConvolutionEffect.h"
tomhudson@google.comaa72eab2012-07-19 18:01:07 +000013#include "effects/GrSingleTextureEffect.h"
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000014#include "effects/GrConfigConversionEffect.h"
jvanverth@google.com65eb4d52013-03-19 18:51:02 +000015#include "effects/GrCircleEdgeEffect.h"
commit-bot@chromium.orgff6ea262013-03-12 12:26:08 +000016#include "effects/GrEllipseEdgeEffect.h"
bsalomon@google.comb505a122012-05-31 18:40:36 +000017
tomhudson@google.com278cbb42011-06-30 19:37:01 +000018#include "GrBufferAllocPool.h"
bsalomon@google.com05ef5102011-05-02 21:14:59 +000019#include "GrGpu.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000020#include "GrIndexBuffer.h"
21#include "GrInOrderDrawBuffer.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000022#include "GrPathRenderer.h"
tomhudson@google.comd22b6e42011-06-24 15:53:40 +000023#include "GrPathUtils.h"
bsalomon@google.com50398bf2011-07-26 20:45:30 +000024#include "GrResourceCache.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000025#include "GrSoftwarePathRenderer.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000026#include "GrStencilBuffer.h"
tomhudson@google.com278cbb42011-06-30 19:37:01 +000027#include "GrTextStrike.h"
bsalomon@google.com82b0ec62013-02-07 21:02:36 +000028#include "SkRTConf.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000029#include "SkStrokeRec.h"
bsalomon@google.com8c2fe992011-09-13 15:27:18 +000030#include "SkTLazy.h"
bsalomon@google.comc0af3172012-06-15 14:10:09 +000031#include "SkTLS.h"
tomhudson@google.com0c8d93a2011-07-01 17:08:26 +000032#include "SkTrace.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000033
reed@google.comfa35e3d2012-06-26 20:16:17 +000034SK_DEFINE_INST_COUNT(GrContext)
35SK_DEFINE_INST_COUNT(GrDrawState)
36
bsalomon@google.com82b0ec62013-02-07 21:02:36 +000037// 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 +000038// InOrderDrawBuffer, to compare performance of using/not using InOrderDrawBuffer, or to make
bsalomon@google.com82b0ec62013-02-07 21:02:36 +000039// debugging simpler.
jvanverth@google.com65eb4d52013-03-19 18:51:02 +000040SK_CONF_DECLARE(bool, c_Defer, "gpu.deferContext", true,
41 "Defers rendering in GrContext via GrInOrderDrawBuffer.");
bsalomon@google.com82b0ec62013-02-07 21:02:36 +000042
43#define BUFFERED_DRAW (c_Defer ? kYes_BufferedDraw : kNo_BufferedDraw)
bsalomon@google.com27847de2011-02-22 20:59:41 +000044
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +000045#define MAX_BLUR_SIGMA 4.0f
46
bsalomon@google.comd46e2422011-09-23 17:40:07 +000047// When we're using coverage AA but the blend is incompatible (given gpu
48// limitations) should we disable AA or draw wrong?
bsalomon@google.com950d7a82011-09-28 15:05:33 +000049#define DISABLE_COVERAGE_AA_FOR_BLEND 1
bsalomon@google.comd46e2422011-09-23 17:40:07 +000050
reed@google.com4b2d3f32012-05-15 18:05:50 +000051#if GR_DEBUG
52 // change this to a 1 to see notifications when partial coverage fails
53 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
54#else
55 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
56#endif
57
robertphillips@google.com1947ba62012-10-17 13:35:24 +000058static const size_t MAX_TEXTURE_CACHE_COUNT = 2048;
djsollen@google.com42041e62012-10-29 19:24:45 +000059static const size_t MAX_TEXTURE_CACHE_BYTES = GR_DEFAULT_TEXTURE_CACHE_MB_LIMIT * 1024 * 1024;
bsalomon@google.com27847de2011-02-22 20:59:41 +000060
bsalomon@google.com60361492012-03-15 17:47:06 +000061static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
bsalomon@google.com27847de2011-02-22 20:59:41 +000062static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
63
bsalomon@google.com1d4edd32012-08-16 18:36:06 +000064static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = 1 << 11;
65static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = 4;
bsalomon@google.com27847de2011-02-22 20:59:41 +000066
bsalomon@google.combc4b6542011-11-19 13:56:11 +000067#define ASSERT_OWNED_RESOURCE(R) GrAssert(!(R) || (R)->getContext() == this)
68
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000069GrContext* GrContext::Create(GrBackend backend, GrBackendContext backendContext) {
70 GrContext* context = SkNEW(GrContext);
71 if (context->init(backend, backendContext)) {
72 return context;
73 } else {
74 context->unref();
75 return NULL;
bsalomon@google.com27847de2011-02-22 20:59:41 +000076 }
bsalomon@google.com27847de2011-02-22 20:59:41 +000077}
78
bsalomon@google.comc0af3172012-06-15 14:10:09 +000079namespace {
80void* CreateThreadInstanceCount() {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000081 return SkNEW_ARGS(int, (0));
bsalomon@google.comc0af3172012-06-15 14:10:09 +000082}
83void DeleteThreadInstanceCount(void* v) {
84 delete reinterpret_cast<int*>(v);
85}
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000086#define THREAD_INSTANCE_COUNT \
87 (*reinterpret_cast<int*>(SkTLS::Get(CreateThreadInstanceCount, DeleteThreadInstanceCount)))
88}
bsalomon@google.comc0af3172012-06-15 14:10:09 +000089
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000090GrContext::GrContext() {
91 ++THREAD_INSTANCE_COUNT;
92 fDrawState = NULL;
93 fGpu = NULL;
94 fPathRendererChain = NULL;
95 fSoftwarePathRenderer = NULL;
96 fTextureCache = NULL;
97 fFontCache = NULL;
98 fDrawBuffer = NULL;
99 fDrawBufferVBAllocPool = NULL;
100 fDrawBufferIBAllocPool = NULL;
101 fAARectRenderer = NULL;
102}
103
104bool GrContext::init(GrBackend backend, GrBackendContext backendContext) {
105 GrAssert(NULL == fGpu);
106
107 fGpu = GrGpu::Create(backend, backendContext, this);
108 if (NULL == fGpu) {
109 return false;
110 }
111
112 fDrawState = SkNEW(GrDrawState);
113 fGpu->setDrawState(fDrawState);
114
115
116 fTextureCache = SkNEW_ARGS(GrResourceCache,
117 (MAX_TEXTURE_CACHE_COUNT,
118 MAX_TEXTURE_CACHE_BYTES));
119 fFontCache = SkNEW_ARGS(GrFontCache, (fGpu));
120
121 fLastDrawWasBuffered = kNo_BufferedDraw;
122
123 fAARectRenderer = SkNEW(GrAARectRenderer);
124
125 fDidTestPMConversions = false;
126
127 this->setupDrawBuffer();
128
129 return true;
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000130}
131
132int GrContext::GetThreadInstanceCount() {
133 return THREAD_INSTANCE_COUNT;
134}
135
bsalomon@google.com27847de2011-02-22 20:59:41 +0000136GrContext::~GrContext() {
robertphillips@google.comcdb426d2012-09-24 19:33:59 +0000137 for (int i = 0; i < fCleanUpData.count(); ++i) {
138 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
139 }
140
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000141 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +0000142
143 // Since the gpu can hold scratch textures, give it a chance to let go
144 // of them before freeing the texture cache
145 fGpu->purgeResources();
146
bsalomon@google.com27847de2011-02-22 20:59:41 +0000147 delete fTextureCache;
robertphillips@google.comeb9b3e12012-09-11 12:50:53 +0000148 fTextureCache = NULL;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000149 delete fFontCache;
150 delete fDrawBuffer;
151 delete fDrawBufferVBAllocPool;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000152 delete fDrawBufferIBAllocPool;
bsalomon@google.com30085192011-08-19 15:42:31 +0000153
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000154 fAARectRenderer->unref();
155
bsalomon@google.com205d4602011-04-25 12:43:45 +0000156 fGpu->unref();
bsalomon@google.com30085192011-08-19 15:42:31 +0000157 GrSafeUnref(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000158 GrSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000159 fDrawState->unref();
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000160
161 --THREAD_INSTANCE_COUNT;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000162}
163
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000164void GrContext::contextLost() {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000165 this->contextDestroyed();
junov@google.com53a55842011-06-08 22:55:10 +0000166 this->setupDrawBuffer();
167}
168
169void GrContext::contextDestroyed() {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000170 // abandon first to so destructors
171 // don't try to free the resources in the API.
172 fGpu->abandonResources();
173
bsalomon@google.com30085192011-08-19 15:42:31 +0000174 // a path renderer may be holding onto resources that
175 // are now unusable
176 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000177 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000178
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000179 delete fDrawBuffer;
180 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000181
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000182 delete fDrawBufferVBAllocPool;
183 fDrawBufferVBAllocPool = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000184
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000185 delete fDrawBufferIBAllocPool;
186 fDrawBufferIBAllocPool = NULL;
187
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000188 fAARectRenderer->reset();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000189
bsalomon@google.coma2921122012-08-28 12:34:17 +0000190 fTextureCache->purgeAllUnlocked();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000191 fFontCache->freeAll();
192 fGpu->markContextDirty();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000193}
194
195void GrContext::resetContext() {
196 fGpu->markContextDirty();
197}
198
199void GrContext::freeGpuResources() {
200 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000201
robertphillips@google.comff175842012-05-14 19:31:39 +0000202 fGpu->purgeResources();
203
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000204 fAARectRenderer->reset();
205
bsalomon@google.coma2921122012-08-28 12:34:17 +0000206 fTextureCache->purgeAllUnlocked();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000207 fFontCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000208 // a path renderer may be holding onto resources
209 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000210 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000211}
212
twiz@google.com05e70242012-01-27 19:12:00 +0000213size_t GrContext::getGpuTextureCacheBytes() const {
214 return fTextureCache->getCachedResourceBytes();
215}
216
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000217////////////////////////////////////////////////////////////////////////////////
218
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000219namespace {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000220
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000221void scale_rect(SkRect* rect, float xScale, float yScale) {
robertphillips@google.com5af56062012-04-27 15:39:52 +0000222 rect->fLeft = SkScalarMul(rect->fLeft, SkFloatToScalar(xScale));
223 rect->fTop = SkScalarMul(rect->fTop, SkFloatToScalar(yScale));
224 rect->fRight = SkScalarMul(rect->fRight, SkFloatToScalar(xScale));
225 rect->fBottom = SkScalarMul(rect->fBottom, SkFloatToScalar(yScale));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000226}
227
bsalomon@google.comb505a122012-05-31 18:40:36 +0000228float adjust_sigma(float sigma, int *scaleFactor, int *radius) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000229 *scaleFactor = 1;
230 while (sigma > MAX_BLUR_SIGMA) {
231 *scaleFactor *= 2;
232 sigma *= 0.5f;
233 }
bsalomon@google.comb505a122012-05-31 18:40:36 +0000234 *radius = static_cast<int>(ceilf(sigma * 3.0f));
235 GrAssert(*radius <= GrConvolutionEffect::kMaxKernelRadius);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000236 return sigma;
237}
238
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000239void convolve_gaussian(GrDrawTarget* target,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000240 GrTexture* texture,
241 const SkRect& rect,
242 float sigma,
243 int radius,
244 Gr1DKernelEffect::Direction direction) {
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000245 GrRenderTarget* rt = target->drawState()->getRenderTarget();
246 GrDrawTarget::AutoStateRestore asr(target, GrDrawTarget::kReset_ASRInit);
247 GrDrawState* drawState = target->drawState();
248 drawState->setRenderTarget(rt);
bsalomon@google.comc5fae9e2013-01-24 19:00:02 +0000249 SkAutoTUnref<GrEffectRef> conv(GrConvolutionEffect::CreateGaussian(texture,
bsalomon@google.com6340a412013-01-22 19:55:59 +0000250 direction,
251 radius,
252 sigma));
bsalomon@google.comadc65362013-01-28 14:26:09 +0000253 drawState->setEffect(0, conv);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000254 target->drawSimpleRect(rect, NULL);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000255}
256
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000257}
258
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000259////////////////////////////////////////////////////////////////////////////////
260
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000261GrTexture* GrContext::findAndRefTexture(const GrTextureDesc& desc,
262 const GrCacheID& cacheID,
263 const GrTextureParams* params) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000264 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheID);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000265 GrResource* resource = fTextureCache->find(resourceKey);
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000266 SkSafeRef(resource);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000267 return static_cast<GrTexture*>(resource);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000268}
269
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000270bool GrContext::isTextureInCache(const GrTextureDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000271 const GrCacheID& cacheID,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000272 const GrTextureParams* params) const {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000273 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheID);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000274 return fTextureCache->hasKey(resourceKey);
275}
276
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000277void GrContext::addStencilBuffer(GrStencilBuffer* sb) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000278 ASSERT_OWNED_RESOURCE(sb);
robertphillips@google.com46a86002012-08-08 10:42:44 +0000279
280 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(sb->width(),
281 sb->height(),
282 sb->numSamples());
robertphillips@google.com209a1142012-10-31 12:25:21 +0000283 fTextureCache->addResource(resourceKey, sb);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000284}
285
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000286GrStencilBuffer* GrContext::findStencilBuffer(int width, int height,
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000287 int sampleCnt) {
robertphillips@google.com46a86002012-08-08 10:42:44 +0000288 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(width,
289 height,
290 sampleCnt);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000291 GrResource* resource = fTextureCache->find(resourceKey);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000292 return static_cast<GrStencilBuffer*>(resource);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000293}
294
bsalomon@google.com27847de2011-02-22 20:59:41 +0000295static void stretchImage(void* dst,
296 int dstW,
297 int dstH,
298 void* src,
299 int srcW,
300 int srcH,
301 int bpp) {
302 GrFixed dx = (srcW << 16) / dstW;
303 GrFixed dy = (srcH << 16) / dstH;
304
305 GrFixed y = dy >> 1;
306
307 int dstXLimit = dstW*bpp;
308 for (int j = 0; j < dstH; ++j) {
309 GrFixed x = dx >> 1;
310 void* srcRow = (uint8_t*)src + (y>>16)*srcW*bpp;
311 void* dstRow = (uint8_t*)dst + j*dstW*bpp;
312 for (int i = 0; i < dstXLimit; i += bpp) {
313 memcpy((uint8_t*) dstRow + i,
314 (uint8_t*) srcRow + (x>>16)*bpp,
315 bpp);
316 x += dx;
317 }
318 y += dy;
319 }
320}
321
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000322// The desired texture is NPOT and tiled but that isn't supported by
robertphillips@google.com3319f332012-08-13 18:00:36 +0000323// the current hardware. Resize the texture to be a POT
324GrTexture* GrContext::createResizedTexture(const GrTextureDesc& desc,
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000325 const GrCacheID& cacheID,
robertphillips@google.com3319f332012-08-13 18:00:36 +0000326 void* srcData,
327 size_t rowBytes,
328 bool needsFiltering) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000329 SkAutoTUnref<GrTexture> clampedTexture(this->findAndRefTexture(desc, cacheID, NULL));
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000330 if (NULL == clampedTexture) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000331 clampedTexture.reset(this->createTexture(NULL, desc, cacheID, srcData, rowBytes));
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000332
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000333 if (NULL == clampedTexture) {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000334 return NULL;
335 }
336 }
robertphillips@google.com0d25eef2012-09-11 21:25:51 +0000337
robertphillips@google.com3319f332012-08-13 18:00:36 +0000338 GrTextureDesc rtDesc = desc;
339 rtDesc.fFlags = rtDesc.fFlags |
340 kRenderTarget_GrTextureFlagBit |
341 kNoStencil_GrTextureFlagBit;
342 rtDesc.fWidth = GrNextPow2(GrMax(desc.fWidth, 64));
343 rtDesc.fHeight = GrNextPow2(GrMax(desc.fHeight, 64));
344
345 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
346
347 if (NULL != texture) {
348 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
349 GrDrawState* drawState = fGpu->drawState();
350 drawState->setRenderTarget(texture->asRenderTarget());
351
352 // if filtering is not desired then we want to ensure all
353 // texels in the resampled image are copies of texels from
354 // the original.
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000355 GrTextureParams params(SkShader::kClamp_TileMode, needsFiltering);
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000356 drawState->createTextureEffect(0, clampedTexture, SkMatrix::I(), params);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000357
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000358 // position + texture coordinate
359 static const GrVertexAttrib kVertexAttribs[] = {
jvanverth@google.com3b0d6312013-03-01 20:30:01 +0000360 {kVec2f_GrVertexAttribType, 0},
361 {kVec2f_GrVertexAttribType, sizeof(GrPoint)}
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000362 };
jvanverth@google.com65eb4d52013-03-19 18:51:02 +0000363 static const GrAttribBindings kAttribBindings =
364 GrDrawState::ExplicitTexCoordAttribBindingsBit(0);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000365 drawState->setAttribBindings(kAttribBindings);
366 drawState->setVertexAttribs(kVertexAttribs, SK_ARRAY_COUNT(kVertexAttribs));
367 drawState->setAttribIndex(GrDrawState::kPosition_AttribIndex, 0);
368 drawState->setAttribIndex(GrDrawState::kTexCoord_AttribIndex, 1);
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000369 GrDrawTarget::AutoReleaseGeometry arg(fGpu, 4, 0);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000370
371 if (arg.succeeded()) {
372 GrPoint* verts = (GrPoint*) arg.vertices();
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000373 verts[0].setIRectFan(0, 0, texture->width(), texture->height(), 2 * sizeof(GrPoint));
374 verts[1].setIRectFan(0, 0, 1, 1, 2 * sizeof(GrPoint));
375 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000376 }
377 texture->releaseRenderTarget();
378 } else {
379 // TODO: Our CPU stretch doesn't filter. But we create separate
bsalomon@google.com08283af2012-10-26 13:01:20 +0000380 // stretched textures when the texture params is either filtered or
robertphillips@google.com3319f332012-08-13 18:00:36 +0000381 // not. Either implement filtered stretch blit on CPU or just create
382 // one when FBO case fails.
383
384 rtDesc.fFlags = kNone_GrTextureFlags;
385 // no longer need to clamp at min RT size.
386 rtDesc.fWidth = GrNextPow2(desc.fWidth);
387 rtDesc.fHeight = GrNextPow2(desc.fHeight);
388 int bpp = GrBytesPerPixel(desc.fConfig);
bsalomon@google.com08283af2012-10-26 13:01:20 +0000389 SkAutoSMalloc<128*128*4> stretchedPixels(bpp * rtDesc.fWidth * rtDesc.fHeight);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000390 stretchImage(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
bsalomon@google.com08283af2012-10-26 13:01:20 +0000391 srcData, desc.fWidth, desc.fHeight, bpp);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000392
393 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
394
jvanverth@google.com65eb4d52013-03-19 18:51:02 +0000395 SkDEBUGCODE(GrTexture* texture = )fGpu->createTexture(rtDesc, stretchedPixels.get(),
396 stretchedRowBytes);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000397 GrAssert(NULL != texture);
398 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000399
400 return texture;
401}
402
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000403GrTexture* GrContext::createTexture(const GrTextureParams* params,
404 const GrTextureDesc& desc,
405 const GrCacheID& cacheID,
406 void* srcData,
407 size_t rowBytes) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000408 SK_TRACE_EVENT0("GrContext::createTexture");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000409
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000410 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheID);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000411
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000412 GrTexture* texture;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000413 if (GrTexture::NeedsResizing(resourceKey)) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000414 texture = this->createResizedTexture(desc, cacheID,
415 srcData, rowBytes,
416 GrTexture::NeedsFiltering(resourceKey));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000417 } else {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000418 texture= fGpu->createTexture(desc, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000419 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000420
421 if (NULL != texture) {
robertphillips@google.com209a1142012-10-31 12:25:21 +0000422 fTextureCache->addResource(resourceKey, texture);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000423 }
424
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000425 return texture;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000426}
427
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000428GrTexture* GrContext::lockAndRefScratchTexture(const GrTextureDesc& inDesc, ScratchTexMatch match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000429 GrTextureDesc desc = inDesc;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000430
rileya@google.com2afb8ec2012-08-23 14:08:57 +0000431 GrAssert((desc.fFlags & kRenderTarget_GrTextureFlagBit) ||
432 !(desc.fFlags & kNoStencil_GrTextureFlagBit));
433
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000434 if (kApprox_ScratchTexMatch == match) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000435 // bin by pow2 with a reasonable min
bsalomon@google.com40db6dc2013-03-06 16:35:49 +0000436 static const int MIN_SIZE = 16;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000437 desc.fWidth = GrMax(MIN_SIZE, GrNextPow2(desc.fWidth));
438 desc.fHeight = GrMax(MIN_SIZE, GrNextPow2(desc.fHeight));
439 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000440
robertphillips@google.com13f181f2013-03-02 12:02:08 +0000441 // Renderable A8 targets are not universally supported (e.g., not on ANGLE)
442 GrAssert(this->isConfigRenderable(kAlpha_8_GrPixelConfig) ||
443 !(desc.fFlags & kRenderTarget_GrTextureFlagBit) ||
444 (desc.fConfig != kAlpha_8_GrPixelConfig));
445
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000446 GrResource* resource = NULL;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000447 int origWidth = desc.fWidth;
448 int origHeight = desc.fHeight;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000449
450 do {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000451 GrResourceKey key = GrTexture::ComputeScratchKey(desc);
robertphillips@google.com209a1142012-10-31 12:25:21 +0000452 // Ensure we have exclusive access to the texture so future 'find' calls don't return it
453 resource = fTextureCache->find(key, GrResourceCache::kHide_OwnershipFlag);
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000454 if (NULL != resource) {
455 resource->ref();
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000456 break;
457 }
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000458 if (kExact_ScratchTexMatch == match) {
459 break;
460 }
bsalomon@google.com40db6dc2013-03-06 16:35:49 +0000461 // 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 +0000462
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000463 // We no longer try to reuse textures that were previously used as render targets in
rileya@google.com2afb8ec2012-08-23 14:08:57 +0000464 // situations where no RT is needed; doing otherwise can confuse the video driver and
465 // cause significant performance problems in some cases.
466 if (desc.fFlags & kNoStencil_GrTextureFlagBit) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000467 desc.fFlags = desc.fFlags & ~kNoStencil_GrTextureFlagBit;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000468 } else {
469 break;
470 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000471
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000472 } while (true);
473
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000474 if (NULL == resource) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000475 desc.fFlags = inDesc.fFlags;
476 desc.fWidth = origWidth;
477 desc.fHeight = origHeight;
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000478 GrTexture* texture = fGpu->createTexture(desc, NULL, 0);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000479 if (NULL != texture) {
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000480 GrResourceKey key = GrTexture::ComputeScratchKey(texture->desc());
robertphillips@google.com209a1142012-10-31 12:25:21 +0000481 // Make the resource exclusive so future 'find' calls don't return it
482 fTextureCache->addResource(key, texture, GrResourceCache::kHide_OwnershipFlag);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000483 resource = texture;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000484 }
485 }
486
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000487 return static_cast<GrTexture*>(resource);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000488}
489
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000490void GrContext::addExistingTextureToCache(GrTexture* texture) {
491
492 if (NULL == texture) {
493 return;
494 }
495
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000496 // This texture should already have a cache entry since it was once
497 // attached
498 GrAssert(NULL != texture->getCacheEntry());
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000499
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000500 // Conceptually, the cache entry is going to assume responsibility
501 // for the creation ref.
502 GrAssert(1 == texture->getRefCnt());
503
504 // Since this texture came from an AutoScratchTexture it should
505 // still be in the exclusive pile
506 fTextureCache->makeNonExclusive(texture->getCacheEntry());
507
robertphillips@google.com50a035d2012-09-07 19:44:33 +0000508 this->purgeCache();
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000509}
510
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000511
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000512void GrContext::unlockScratchTexture(GrTexture* texture) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000513 ASSERT_OWNED_RESOURCE(texture);
514 GrAssert(NULL != texture->getCacheEntry());
515
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000516 // If this is a scratch texture we detached it from the cache
517 // while it was locked (to avoid two callers simultaneously getting
518 // the same texture).
bsalomon@google.com0797c2c2012-12-20 15:13:01 +0000519 if (texture->getCacheEntry()->key().isScratch()) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000520 fTextureCache->makeNonExclusive(texture->getCacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000521 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000522
robertphillips@google.com50a035d2012-09-07 19:44:33 +0000523 this->purgeCache();
524}
525
526void GrContext::purgeCache() {
robertphillips@google.comeb9b3e12012-09-11 12:50:53 +0000527 if (NULL != fTextureCache) {
528 fTextureCache->purgeAsNeeded();
529 }
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000530}
531
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000532GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000533 void* srcData,
534 size_t rowBytes) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000535 GrTextureDesc descCopy = descIn;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000536 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000537}
538
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000539void GrContext::getTextureCacheLimits(int* maxTextures,
540 size_t* maxTextureBytes) const {
541 fTextureCache->getLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000542}
543
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000544void GrContext::setTextureCacheLimits(int maxTextures, size_t maxTextureBytes) {
545 fTextureCache->setLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000546}
547
bsalomon@google.com91958362011-06-13 17:58:13 +0000548int GrContext::getMaxTextureSize() const {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000549 return fGpu->getCaps().maxTextureSize();
bsalomon@google.com91958362011-06-13 17:58:13 +0000550}
551
552int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000553 return fGpu->getCaps().maxRenderTargetSize();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000554}
555
bsalomon@google.com8a70eef2013-03-19 13:58:55 +0000556int GrContext::getMaxSampleCount() const {
557 return fGpu->getCaps().maxSampleCount();
558}
559
bsalomon@google.com27847de2011-02-22 20:59:41 +0000560///////////////////////////////////////////////////////////////////////////////
561
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000562GrTexture* GrContext::wrapBackendTexture(const GrBackendTextureDesc& desc) {
563 return fGpu->wrapBackendTexture(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000564}
565
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000566GrRenderTarget* GrContext::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
567 return fGpu->wrapBackendRenderTarget(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000568}
569
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000570///////////////////////////////////////////////////////////////////////////////
571
bsalomon@google.comb8670992012-07-25 21:27:09 +0000572bool GrContext::supportsIndex8PixelConfig(const GrTextureParams* params,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000573 int width, int height) const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000574 const GrDrawTarget::Caps& caps = fGpu->getCaps();
bsalomon@google.comf6601872012-08-28 21:11:35 +0000575 if (!caps.eightBitPaletteSupport()) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000576 return false;
577 }
578
bsalomon@google.com27847de2011-02-22 20:59:41 +0000579 bool isPow2 = GrIsPow2(width) && GrIsPow2(height);
580
581 if (!isPow2) {
bsalomon@google.comb8670992012-07-25 21:27:09 +0000582 bool tiled = NULL != params && params->isTiled();
bsalomon@google.comf6601872012-08-28 21:11:35 +0000583 if (tiled && !caps.npotTextureTileSupport()) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000584 return false;
585 }
586 }
587 return true;
588}
589
590////////////////////////////////////////////////////////////////////////////////
591
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000592const GrClipData* GrContext::getClip() const {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000593 return fGpu->getClip();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000594}
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000595
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000596void GrContext::setClip(const GrClipData* clipData) {
597 fGpu->setClip(clipData);
robertphillips@google.com837ec432012-10-04 17:57:05 +0000598
skia.committer@gmail.comf467ce72012-10-09 02:01:37 +0000599 fDrawState->setState(GrDrawState::kClip_StateBit,
robertphillips@google.com183e34b2012-10-19 18:19:01 +0000600 clipData && clipData->fClipStack && !clipData->fClipStack->isWideOpen());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000601}
602
bsalomon@google.com27847de2011-02-22 20:59:41 +0000603////////////////////////////////////////////////////////////////////////////////
604
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000605void GrContext::clear(const GrIRect* rect,
606 const GrColor color,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000607 GrRenderTarget* target) {
bsalomon@google.com82b0ec62013-02-07 21:02:36 +0000608 this->prepareToDraw(NULL, BUFFERED_DRAW)->clear(rect, color, target);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000609}
610
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000611void GrContext::drawPaint(const GrPaint& origPaint) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000612 // set rect to be big enough to fill the space, but not super-huge, so we
613 // don't overflow fixed-point implementations
bsalomon@google.comd302f142011-03-03 13:54:13 +0000614 GrRect r;
615 r.setLTRB(0, 0,
bsalomon@google.com81712882012-11-01 17:12:34 +0000616 SkIntToScalar(getRenderTarget()->width()),
617 SkIntToScalar(getRenderTarget()->height()));
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000618 SkMatrix inverse;
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000619 SkTCopyOnFirstWrite<GrPaint> paint(origPaint);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000620 AutoMatrix am;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000621
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000622 // We attempt to map r by the inverse matrix and draw that. mapRect will
623 // map the four corners and bound them with a new rect. This will not
624 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000625 if (!this->getMatrix().hasPerspective()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000626 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000627 GrPrintf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000628 return;
629 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000630 inverse.mapRect(&r);
631 } else {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000632 if (!am.setIdentity(this, paint.writable())) {
633 GrPrintf("Could not invert matrix\n");
634 return;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000635 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000636 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000637 // by definition this fills the entire clip, no need for AA
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000638 if (paint->isAntiAlias()) {
639 paint.writable()->setAntiAlias(false);
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000640 }
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000641 this->drawRect(*paint, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000642}
643
bsalomon@google.com205d4602011-04-25 12:43:45 +0000644////////////////////////////////////////////////////////////////////////////////
645
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000646namespace {
647inline bool disable_coverage_aa_for_blend(GrDrawTarget* target) {
648 return DISABLE_COVERAGE_AA_FOR_BLEND && !target->canApplyCoverage();
649}
650}
651
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000652////////////////////////////////////////////////////////////////////////////////
653
bsalomon@google.com27847de2011-02-22 20:59:41 +0000654/* create a triangle strip that strokes the specified triangle. There are 8
655 unique vertices, but we repreat the last 2 to close up. Alternatively we
656 could use an indices array, and then only send 8 verts, but not sure that
657 would be faster.
658 */
bsalomon@google.com205d4602011-04-25 12:43:45 +0000659static void setStrokeRectStrip(GrPoint verts[10], GrRect rect,
bsalomon@google.com81712882012-11-01 17:12:34 +0000660 SkScalar width) {
661 const SkScalar rad = SkScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000662 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000663
664 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
665 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
666 verts[2].set(rect.fRight - rad, rect.fTop + rad);
667 verts[3].set(rect.fRight + rad, rect.fTop - rad);
668 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
669 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
670 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
671 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
672 verts[8] = verts[0];
673 verts[9] = verts[1];
674}
675
reed@google.com20efde72011-05-09 17:00:02 +0000676/**
677 * Returns true if the rects edges are integer-aligned.
678 */
679static bool isIRect(const GrRect& r) {
bsalomon@google.com81712882012-11-01 17:12:34 +0000680 return SkScalarIsInt(r.fLeft) && SkScalarIsInt(r.fTop) &&
681 SkScalarIsInt(r.fRight) && SkScalarIsInt(r.fBottom);
reed@google.com20efde72011-05-09 17:00:02 +0000682}
683
bsalomon@google.com205d4602011-04-25 12:43:45 +0000684static bool apply_aa_to_rect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000685 const GrRect& rect,
bsalomon@google.com81712882012-11-01 17:12:34 +0000686 SkScalar width,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000687 const SkMatrix* matrix,
688 SkMatrix* combinedMatrix,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000689 GrRect* devRect,
690 bool* useVertexCoverage) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000691 // we use a simple coverage ramp to do aa on axis-aligned rects
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000692 // we check if the rect will be axis-aligned, and the rect won't land on
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000693 // integer coords.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000694
bsalomon@google.coma3108262011-10-10 14:08:47 +0000695 // we are keeping around the "tweak the alpha" trick because
696 // it is our only hope for the fixed-pipe implementation.
697 // In a shader implementation we can give a separate coverage input
bsalomon@google.com289533a2011-10-27 12:34:25 +0000698 // TODO: remove this ugliness when we drop the fixed-pipe impl
bsalomon@google.coma3108262011-10-10 14:08:47 +0000699 *useVertexCoverage = false;
bsalomon@google.com2b446732013-02-12 16:47:41 +0000700 if (!target->getDrawState().canTweakAlphaForCoverage()) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000701 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +0000702#if GR_DEBUG
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000703 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000704#endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000705 return false;
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000706 } else {
707 *useVertexCoverage = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000708 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000709 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000710 const GrDrawState& drawState = target->getDrawState();
711 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000712 return false;
713 }
714
bsalomon@google.com471d4712011-08-23 15:45:25 +0000715 if (0 == width && target->willUseHWAALines()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000716 return false;
717 }
718
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000719 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000720 return false;
721 }
722
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000723 if (NULL != matrix &&
bsalomon@google.com205d4602011-04-25 12:43:45 +0000724 !matrix->preservesAxisAlignment()) {
725 return false;
726 }
727
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000728 *combinedMatrix = drawState.getViewMatrix();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000729 if (NULL != matrix) {
730 combinedMatrix->preConcat(*matrix);
731 GrAssert(combinedMatrix->preservesAxisAlignment());
732 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000733
bsalomon@google.com205d4602011-04-25 12:43:45 +0000734 combinedMatrix->mapRect(devRect, rect);
735 devRect->sort();
736
737 if (width < 0) {
reed@google.com20efde72011-05-09 17:00:02 +0000738 return !isIRect(*devRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000739 } else {
740 return true;
741 }
742}
743
bsalomon@google.com27847de2011-02-22 20:59:41 +0000744void GrContext::drawRect(const GrPaint& paint,
745 const GrRect& rect,
bsalomon@google.com81712882012-11-01 17:12:34 +0000746 SkScalar width,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000747 const SkMatrix* matrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000748 SK_TRACE_EVENT0("GrContext::drawRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000749
bsalomon@google.com82b0ec62013-02-07 21:02:36 +0000750 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000751 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000752
bsalomon@google.com205d4602011-04-25 12:43:45 +0000753 GrRect devRect = rect;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000754 SkMatrix combinedMatrix;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000755 bool useVertexCoverage;
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000756 bool needAA = paint.isAntiAlias() &&
bsalomon@google.com289533a2011-10-27 12:34:25 +0000757 !this->getRenderTarget()->isMultisampled();
758 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix,
759 &combinedMatrix, &devRect,
760 &useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000761
762 if (doAA) {
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000763 GrDrawState::AutoDeviceCoordDraw adcd(target->drawState());
bsalomon@google.come3d32162012-07-20 13:37:06 +0000764 if (!adcd.succeeded()) {
765 return;
766 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000767 if (width >= 0) {
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000768 GrVec strokeSize;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000769 if (width > 0) {
770 strokeSize.set(width, width);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000771 combinedMatrix.mapVectors(&strokeSize, 1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000772 strokeSize.setAbs(strokeSize);
773 } else {
bsalomon@google.com81712882012-11-01 17:12:34 +0000774 strokeSize.set(SK_Scalar1, SK_Scalar1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000775 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000776 fAARectRenderer->strokeAARect(this->getGpu(), target, devRect,
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000777 strokeSize, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000778 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000779 fAARectRenderer->fillAARect(this->getGpu(), target,
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000780 devRect, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000781 }
782 return;
783 }
784
bsalomon@google.com27847de2011-02-22 20:59:41 +0000785 if (width >= 0) {
786 // TODO: consider making static vertex buffers for these cases.
bsalomon@google.com64386952013-02-08 21:22:44 +0000787 // Hairline could be done by just adding closing vertex to
788 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000789
bsalomon@google.com27847de2011-02-22 20:59:41 +0000790 static const int worstCaseVertCount = 10;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000791 target->drawState()->setDefaultVertexAttribs();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000792 GrDrawTarget::AutoReleaseGeometry geo(target, worstCaseVertCount, 0);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000793
794 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000795 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000796 return;
797 }
798
799 GrPrimitiveType primType;
800 int vertCount;
801 GrPoint* vertex = geo.positions();
802
803 if (width > 0) {
804 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000805 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000806 setStrokeRectStrip(vertex, rect, width);
807 } else {
808 // hairline
809 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000810 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000811 vertex[0].set(rect.fLeft, rect.fTop);
812 vertex[1].set(rect.fRight, rect.fTop);
813 vertex[2].set(rect.fRight, rect.fBottom);
814 vertex[3].set(rect.fLeft, rect.fBottom);
815 vertex[4].set(rect.fLeft, rect.fTop);
816 }
817
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000818 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000819 if (NULL != matrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000820 GrDrawState* drawState = target->drawState();
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000821 avmr.set(drawState, *matrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000822 }
823
824 target->drawNonIndexed(primType, 0, vertCount);
825 } else {
bsalomon@google.com64386952013-02-08 21:22:44 +0000826#if GR_STATIC_RECT_VB
827 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
828 if (NULL == sqVB) {
829 GrPrintf("Failed to create static rect vb.\n");
830 return;
831 }
832
833 GrDrawState* drawState = target->drawState();
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000834 target->drawState()->setDefaultVertexAttribs();
bsalomon@google.com64386952013-02-08 21:22:44 +0000835 target->setVertexSourceToBuffer(sqVB);
836 SkMatrix m;
837 m.setAll(rect.width(), 0, rect.fLeft,
838 0, rect.height(), rect.fTop,
839 0, 0, SkMatrix::I()[8]);
840
841 if (NULL != matrix) {
842 m.postConcat(*matrix);
843 }
844 GrDrawState::AutoViewMatrixRestore avmr(drawState, m);
845
846 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
847#else
848 target->drawSimpleRect(rect, matrix);
849#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000850 }
851}
852
853void GrContext::drawRectToRect(const GrPaint& paint,
854 const GrRect& dstRect,
855 const GrRect& srcRect,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000856 const SkMatrix* dstMatrix,
857 const SkMatrix* srcMatrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000858 SK_TRACE_EVENT0("GrContext::drawRectToRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000859
bsalomon@google.com88becf42012-10-05 14:54:42 +0000860 // srcRect refers to paint's first color stage
861 if (!paint.isColorStageEnabled(0)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000862 drawRect(paint, dstRect, -1, dstMatrix);
863 return;
864 }
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000865
bsalomon@google.com82b0ec62013-02-07 21:02:36 +0000866 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW);
bsalomon@google.com64386952013-02-08 21:22:44 +0000867
868#if GR_STATIC_RECT_VB
869 GrDrawState::AutoStageDisable atr(fDrawState);
870 GrDrawState* drawState = target->drawState();
871
872 SkMatrix m;
873
874 m.setAll(dstRect.width(), 0, dstRect.fLeft,
875 0, dstRect.height(), dstRect.fTop,
876 0, 0, SkMatrix::I()[8]);
877 if (NULL != dstMatrix) {
878 m.postConcat(*dstMatrix);
879 }
880
881 // The first color stage's coords come from srcRect rather than applying a matrix to dstRect.
882 // We explicitly compute a matrix for that stage below, no need to adjust here.
883 static const uint32_t kExplicitCoordMask = 1 << GrPaint::kFirstColorStage;
884 GrDrawState::AutoViewMatrixRestore avmr(drawState, m, kExplicitCoordMask);
885
886 m.setAll(srcRect.width(), 0, srcRect.fLeft,
887 0, srcRect.height(), srcRect.fTop,
888 0, 0, SkMatrix::I()[8]);
889 if (NULL != srcMatrix) {
890 m.postConcat(*srcMatrix);
891 }
892
893 drawState->preConcatStageMatrices(kExplicitCoordMask, m);
894
895 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
896 if (NULL == sqVB) {
897 GrPrintf("Failed to create static rect vb.\n");
898 return;
899 }
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000900 drawState->setDefaultVertexAttribs();
bsalomon@google.com64386952013-02-08 21:22:44 +0000901 target->setVertexSourceToBuffer(sqVB);
902 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
903#else
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000904 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000905
jvanverth@google.com39768252013-02-14 15:25:44 +0000906 target->drawRect(dstRect, dstMatrix, &srcRect, srcMatrix, 0);
bsalomon@google.com64386952013-02-08 21:22:44 +0000907#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000908}
909
910void GrContext::drawVertices(const GrPaint& paint,
911 GrPrimitiveType primitiveType,
912 int vertexCount,
913 const GrPoint positions[],
914 const GrPoint texCoords[],
915 const GrColor colors[],
916 const uint16_t indices[],
917 int indexCount) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000918 SK_TRACE_EVENT0("GrContext::drawVertices");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000919
920 GrDrawTarget::AutoReleaseGeometry geo;
921
bsalomon@google.com82b0ec62013-02-07 21:02:36 +0000922 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000923 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000924
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000925 GrDrawState* drawState = target->drawState();
jvanverth@google.comb8b705b2013-02-28 16:28:34 +0000926
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000927 GrVertexAttribArray<3> attribs;
928 size_t currentOffset = 0;
929 int colorOffset = -1, texOffset = -1;
930 GrAttribBindings bindings = GrDrawState::kDefault_AttribBindings;
931
932 // set position attribute
933 drawState->setAttribIndex(GrDrawState::kPosition_AttribIndex, attribs.count());
jvanverth@google.com3b0d6312013-03-01 20:30:01 +0000934 GrVertexAttrib currAttrib = {kVec2f_GrVertexAttribType, currentOffset};
935 attribs.push_back(currAttrib);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000936 currentOffset += sizeof(GrPoint);
937
938 // set up optional texture coordinate attributes
939 if (NULL != texCoords) {
940 bindings |= GrDrawState::ExplicitTexCoordAttribBindingsBit(0);
941 drawState->setAttribIndex(GrDrawState::kTexCoord_AttribIndex, attribs.count());
jvanverth@google.com3b0d6312013-03-01 20:30:01 +0000942 currAttrib.set(kVec2f_GrVertexAttribType, currentOffset);
943 attribs.push_back(currAttrib);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000944 texOffset = currentOffset;
945 currentOffset += sizeof(GrPoint);
946 }
947
948 // set up optional color attributes
949 if (NULL != colors) {
950 bindings |= GrDrawState::kColor_AttribBindingsBit;
951 drawState->setAttribIndex(GrDrawState::kColor_AttribIndex, attribs.count());
jvanverth@google.com3b0d6312013-03-01 20:30:01 +0000952 currAttrib.set(kVec4ub_GrVertexAttribType, currentOffset);
953 attribs.push_back(currAttrib);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000954 colorOffset = currentOffset;
955 currentOffset += sizeof(GrColor);
956 }
957
958 drawState->setVertexAttribs(attribs.begin(), attribs.count());
959 drawState->setAttribBindings(bindings);
960
961 size_t vertexSize = drawState->getVertexSize();
962 GrAssert(vertexSize == currentOffset);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000963 if (sizeof(GrPoint) != vertexSize) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000964 if (!geo.set(target, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000965 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000966 return;
967 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000968 void* curVertex = geo.vertices();
969
970 for (int i = 0; i < vertexCount; ++i) {
971 *((GrPoint*)curVertex) = positions[i];
972
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000973 if (texOffset >= 0) {
jvanverth@google.com39768252013-02-14 15:25:44 +0000974 *(GrPoint*)((intptr_t)curVertex + texOffset) = texCoords[i];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000975 }
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000976 if (colorOffset >= 0) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000977 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
978 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000979 curVertex = (void*)((intptr_t)curVertex + vertexSize);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000980 }
981 } else {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000982 target->setVertexSourceToArray(positions, vertexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000983 }
984
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000985 // we don't currently apply offscreen AA to this path. Need improved
bsalomon@google.com91958362011-06-13 17:58:13 +0000986 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000987
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000988 if (NULL != indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000989 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000990 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000991 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000992 target->drawNonIndexed(primitiveType, 0, vertexCount);
993 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000994}
995
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000996///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com150d2842012-01-12 20:19:56 +0000997namespace {
998
bsalomon@google.com93c96602012-04-27 13:05:21 +0000999struct CircleVertex {
1000 GrPoint fPos;
1001 GrPoint fCenter;
bsalomon@google.com81712882012-11-01 17:12:34 +00001002 SkScalar fOuterRadius;
1003 SkScalar fInnerRadius;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001004};
1005
jvanverth@google.com65eb4d52013-03-19 18:51:02 +00001006struct EllipseVertex {
1007 GrPoint fPos;
1008 GrPoint fCenter;
1009 SkScalar fOuterXRadius;
1010 SkScalar fOuterXYRatio;
1011 SkScalar fInnerXRadius;
1012 SkScalar fInnerXYRatio;
1013};
1014
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001015inline bool circleStaysCircle(const SkMatrix& m) {
1016 return m.isSimilarity();
bsalomon@google.com93c96602012-04-27 13:05:21 +00001017}
1018
1019}
1020
bsalomon@google.com93c96602012-04-27 13:05:21 +00001021void GrContext::drawOval(const GrPaint& paint,
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001022 const GrRect& oval,
1023 const SkStrokeRec& stroke) {
1024
jvanverth@google.com65eb4d52013-03-19 18:51:02 +00001025 bool isCircle;
1026 if (!canDrawOval(paint, oval, &isCircle)) {
bsalomon@google.com93c96602012-04-27 13:05:21 +00001027 SkPath path;
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001028 path.addOval(oval);
1029 this->drawPath(paint, path, stroke);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001030 return;
skia.committer@gmail.com98ded842013-01-23 07:06:17 +00001031 }
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001032
jvanverth@google.com65eb4d52013-03-19 18:51:02 +00001033 if (isCircle) {
1034 this->internalDrawCircle(paint, oval, stroke);
1035 } else {
1036 this->internalDrawOval(paint, oval, stroke);
1037 }
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001038}
1039
jvanverth@google.com65eb4d52013-03-19 18:51:02 +00001040bool GrContext::canDrawOval(const GrPaint& paint, const GrRect& oval, bool* isCircle) const {
1041 GrAssert(isCircle != NULL);
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001042
1043 if (!paint.isAntiAlias()) {
1044 return false;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001045 }
skia.committer@gmail.com98ded842013-01-23 07:06:17 +00001046
jvanverth@google.com65eb4d52013-03-19 18:51:02 +00001047 // we can draw circles
1048 *isCircle = SkScalarNearlyEqual(oval.width(), oval.height())
1049 && circleStaysCircle(this->getMatrix());
1050 // and axis-aligned ellipses only
1051 bool isAxisAlignedEllipse = this->getMatrix().rectStaysRect();
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001052
jvanverth@google.com65eb4d52013-03-19 18:51:02 +00001053 return *isCircle || isAxisAlignedEllipse;
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001054}
1055
1056void GrContext::internalDrawOval(const GrPaint& paint,
1057 const GrRect& oval,
1058 const SkStrokeRec& stroke) {
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001059#ifdef SK_DEBUG
1060 {
1061 // we should have checked for this previously
jvanverth@google.com65eb4d52013-03-19 18:51:02 +00001062 bool isAxisAlignedEllipse = this->getMatrix().rectStaysRect();
1063 SkASSERT(paint.isAntiAlias() && isAxisAlignedEllipse);
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001064 }
1065#endif
bsalomon@google.com82b0ec62013-02-07 21:02:36 +00001066 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001067
bsalomon@google.com0982d352012-07-31 15:33:25 +00001068 GrDrawState* drawState = target->drawState();
1069 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com0982d352012-07-31 15:33:25 +00001070
bsalomon@google.com93c96602012-04-27 13:05:21 +00001071 const GrRenderTarget* rt = drawState->getRenderTarget();
1072 if (NULL == rt) {
1073 return;
1074 }
1075
jvanverth@google.com65eb4d52013-03-19 18:51:02 +00001076 const SkMatrix vm = drawState->getViewMatrix();
1077
1078 GrDrawState::AutoDeviceCoordDraw adcd(drawState);
1079 if (!adcd.succeeded()) {
1080 return;
1081 }
1082
1083 // position + edge
1084 static const GrVertexAttrib kVertexAttribs[] = {
1085 {kVec2f_GrVertexAttribType, 0},
1086 {kVec2f_GrVertexAttribType, sizeof(GrPoint)},
1087 {kVec4f_GrVertexAttribType, 2*sizeof(GrPoint)}
1088 };
1089 drawState->setVertexAttribs(kVertexAttribs, SK_ARRAY_COUNT(kVertexAttribs));
1090 drawState->setAttribIndex(GrDrawState::kPosition_AttribIndex, 0);
1091 GrAssert(sizeof(EllipseVertex) == drawState->getVertexSize());
1092
1093 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
1094 if (!geo.succeeded()) {
1095 GrPrintf("Failed to get space for vertices!\n");
1096 return;
1097 }
1098
1099 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
1100
1101 GrPoint center = GrPoint::Make(oval.centerX(), oval.centerY());
1102 vm.mapPoints(&center, 1);
1103
1104 SkStrokeRec::Style style = stroke.getStyle();
1105 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
1106 enum {
1107 // the edge effects share this stage with glyph rendering
1108 // (kGlyphMaskStage in GrTextContext) && SW path rendering
1109 // (kPathMaskStage in GrSWMaskHelper)
1110 kEdgeEffectStage = GrPaint::kTotalStages,
1111 };
1112 drawState->setAttribBindings(GrDrawState::kDefault_AttribBindings);
1113
1114 GrEffectRef* effect = GrEllipseEdgeEffect::Create(isStroked);
1115 static const int kEllipseCenterAttrIndex = 1;
1116 static const int kEllipseEdgeAttrIndex = 2;
1117 drawState->setEffect(kEdgeEffectStage, effect,
1118 kEllipseCenterAttrIndex, kEllipseEdgeAttrIndex)->unref();
1119
1120 SkRect xformedRect;
1121 vm.mapRect(&xformedRect, oval);
1122
1123 SkScalar xRadius = SkScalarHalf(xformedRect.width());
1124 SkScalar yRadius = SkScalarHalf(xformedRect.height());
1125 SkScalar innerXRadius = 0.0f;
1126 SkScalar innerRatio = 1.0f;
1127
1128 if (SkStrokeRec::kFill_Style != style) {
1129 SkScalar strokeWidth = stroke.getWidth();
1130
1131 // do (potentially) anisotropic mapping
1132 SkVector scaledStroke;
1133 scaledStroke.set(strokeWidth, strokeWidth);
1134 vm.mapVectors(&scaledStroke, 1);
1135
1136 if (SkScalarNearlyZero(scaledStroke.length())) {
1137 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
1138 } else {
1139 scaledStroke.scale(0.5f);
1140 }
1141
1142 // this is legit only if scale & translation (which should be the case at the moment)
1143 if (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style) {
1144 SkScalar innerYRadius = SkMaxScalar(0, yRadius - scaledStroke.fY);
1145 if (innerYRadius > SK_ScalarNearlyZero) {
1146 innerXRadius = SkMaxScalar(0, xRadius - scaledStroke.fX);
1147 innerRatio = innerXRadius/innerYRadius;
1148 }
1149 }
1150 xRadius += scaledStroke.fX;
1151 yRadius += scaledStroke.fY;
1152 }
1153
1154 SkScalar outerRatio = SkScalarDiv(xRadius, yRadius);
1155
1156 for (int i = 0; i < 4; ++i) {
1157 verts[i].fCenter = center;
1158 verts[i].fOuterXRadius = xRadius + 0.5f;
1159 verts[i].fOuterXYRatio = outerRatio;
1160 verts[i].fInnerXRadius = innerXRadius - 0.5f;
1161 verts[i].fInnerXYRatio = innerRatio;
1162 }
1163
1164 SkScalar L = -xRadius;
1165 SkScalar R = +xRadius;
1166 SkScalar T = -yRadius;
1167 SkScalar B = +yRadius;
1168
1169 // We've extended the outer x radius out half a pixel to antialias.
1170 // Expand the drawn rect here so all the pixels will be captured.
1171 L += center.fX - SK_ScalarHalf;
1172 R += center.fX + SK_ScalarHalf;
1173 T += center.fY - SK_ScalarHalf;
1174 B += center.fY + SK_ScalarHalf;
1175
1176 verts[0].fPos = SkPoint::Make(L, T);
1177 verts[1].fPos = SkPoint::Make(R, T);
1178 verts[2].fPos = SkPoint::Make(L, B);
1179 verts[3].fPos = SkPoint::Make(R, B);
1180
1181 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
1182}
1183
1184void GrContext::internalDrawCircle(const GrPaint& paint,
1185 const GrRect& circle,
1186 const SkStrokeRec& stroke) {
1187
1188 SkScalar radius = SkScalarHalf(circle.width());
1189
1190 SkScalar strokeWidth = stroke.getWidth();
1191 SkStrokeRec::Style style = stroke.getStyle();
1192
1193 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW);
1194
1195 GrDrawState* drawState = target->drawState();
1196 GrDrawState::AutoStageDisable atr(fDrawState);
1197
1198 const GrRenderTarget* rt = drawState->getRenderTarget();
1199 if (NULL == rt) {
1200 return;
1201 }
1202
1203 const SkMatrix vm = drawState->getViewMatrix();
1204
bsalomon@google.com5b3e8902012-10-05 20:13:28 +00001205 GrDrawState::AutoDeviceCoordDraw adcd(drawState);
bsalomon@google.come3d32162012-07-20 13:37:06 +00001206 if (!adcd.succeeded()) {
1207 return;
1208 }
bsalomon@google.com93c96602012-04-27 13:05:21 +00001209
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001210 // position + edge
1211 static const GrVertexAttrib kVertexAttribs[] = {
jvanverth@google.com3b0d6312013-03-01 20:30:01 +00001212 {kVec2f_GrVertexAttribType, 0},
1213 {kVec4f_GrVertexAttribType, sizeof(GrPoint)}
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001214 };
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001215 drawState->setVertexAttribs(kVertexAttribs, SK_ARRAY_COUNT(kVertexAttribs));
1216 drawState->setAttribIndex(GrDrawState::kPosition_AttribIndex, 0);
jvanverth@google.comb75b0a02013-02-05 20:33:30 +00001217 GrAssert(sizeof(CircleVertex) == drawState->getVertexSize());
bsalomon@google.com93c96602012-04-27 13:05:21 +00001218
jvanverth@google.comb75b0a02013-02-05 20:33:30 +00001219 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001220 if (!geo.succeeded()) {
1221 GrPrintf("Failed to get space for vertices!\n");
1222 return;
1223 }
1224
1225 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
1226
jvanverth@google.com65eb4d52013-03-19 18:51:02 +00001227 GrPoint center = GrPoint::Make(circle.centerX(), circle.centerY());
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001228 vm.mapPoints(&center, 1);
1229
jvanverth@google.com65eb4d52013-03-19 18:51:02 +00001230 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
1231 enum {
1232 // the edge effects share this stage with glyph rendering
1233 // (kGlyphMaskStage in GrTextContext) && SW path rendering
1234 // (kPathMaskStage in GrSWMaskHelper)
1235 kEdgeEffectStage = GrPaint::kTotalStages,
1236 };
1237 drawState->setAttribBindings(GrDrawState::kDefault_AttribBindings);
1238
1239 GrEffectRef* effect = GrCircleEdgeEffect::Create(isStroked);
1240 static const int kCircleEdgeAttrIndex = 1;
1241 drawState->setEffect(kEdgeEffectStage, effect, kCircleEdgeAttrIndex)->unref();
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001242
jvanverth@google.com65eb4d52013-03-19 18:51:02 +00001243 radius = vm.mapRadius(radius);
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001244
jvanverth@google.com65eb4d52013-03-19 18:51:02 +00001245 SkScalar innerRadius = -2.0f;
1246 SkScalar outerRadius = radius;
1247 SkScalar halfWidth = 0;
1248 if (style != SkStrokeRec::kFill_Style) {
1249 strokeWidth = vm.mapRadius(strokeWidth);
1250 if (SkScalarNearlyZero(strokeWidth)) {
1251 halfWidth = SK_ScalarHalf;
1252 } else {
1253 halfWidth = SkScalarHalf(strokeWidth);
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001254 }
1255
jvanverth@google.com65eb4d52013-03-19 18:51:02 +00001256 outerRadius += halfWidth;
1257 if (isStroked) {
1258 innerRadius = SkMaxScalar(0, radius - halfWidth);
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001259 }
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001260 }
1261
jvanverth@google.com65eb4d52013-03-19 18:51:02 +00001262 for (int i = 0; i < 4; ++i) {
1263 verts[i].fCenter = center;
1264 verts[i].fOuterRadius = outerRadius + 0.5f;
1265 verts[i].fInnerRadius = innerRadius - 0.5f;
1266 }
1267
1268 SkScalar L = -outerRadius;
1269 SkScalar R = +outerRadius;
1270 SkScalar T = -outerRadius;
1271 SkScalar B = +outerRadius;
1272
1273 // We've extended the outer radius out half a pixel to antialias.
1274 // Expand the drawn rect here so all the pixels will be captured.
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001275 L += center.fX - SK_ScalarHalf;
1276 R += center.fX + SK_ScalarHalf;
1277 T += center.fY - SK_ScalarHalf;
1278 B += center.fY + SK_ScalarHalf;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001279
1280 verts[0].fPos = SkPoint::Make(L, T);
1281 verts[1].fPos = SkPoint::Make(R, T);
1282 verts[2].fPos = SkPoint::Make(L, B);
1283 verts[3].fPos = SkPoint::Make(R, B);
1284
bsalomon@google.com47059542012-06-06 20:51:20 +00001285 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001286}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001287
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001288void GrContext::drawPath(const GrPaint& paint, const SkPath& path, const SkStrokeRec& stroke) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001289
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001290 if (path.isEmpty()) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001291 if (path.isInverseFillType()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001292 this->drawPaint(paint);
1293 }
1294 return;
1295 }
1296
bsalomon@google.com93c96602012-04-27 13:05:21 +00001297 SkRect ovalRect;
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001298 bool isOval = path.isOval(&ovalRect);
1299
jvanverth@google.com65eb4d52013-03-19 18:51:02 +00001300 bool isCircle;
1301 if (isOval && !path.isInverseFillType() && this->canDrawOval(paint, ovalRect, &isCircle)) {
1302 if (isCircle) {
1303 this->internalDrawCircle(paint, ovalRect, stroke);
1304 } else {
1305 this->internalDrawOval(paint, ovalRect, stroke);
1306 }
bsalomon@google.com93c96602012-04-27 13:05:21 +00001307 return;
1308 }
1309
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001310 this->internalDrawPath(paint, path, stroke);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001311}
1312
jvanverth@google.com65eb4d52013-03-19 18:51:02 +00001313void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path,
1314 const SkStrokeRec& stroke) {
bsalomon@google.com93c96602012-04-27 13:05:21 +00001315
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001316 // Note that below we may sw-rasterize the path into a scratch texture.
1317 // Scratch textures can be recycled after they are returned to the texture
1318 // cache. This presents a potential hazard for buffered drawing. However,
1319 // the writePixels that uploads to the scratch will perform a flush so we're
1320 // OK.
bsalomon@google.com82b0ec62013-02-07 21:02:36 +00001321 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +00001322 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001323
bsalomon@google.comc7448ce2012-10-05 19:04:13 +00001324 bool prAA = paint.isAntiAlias() && !this->getRenderTarget()->isMultisampled();
bsalomon@google.com289533a2011-10-27 12:34:25 +00001325
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001326 // An Assumption here is that path renderer would use some form of tweaking
1327 // the src color (either the input alpha or in the frag shader) to implement
1328 // aa. If we have some future driver-mojo path AA that can do the right
1329 // thing WRT to the blend then we'll need some query on the PR.
1330 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001331#if GR_DEBUG
bsalomon@google.com979432b2011-11-05 21:38:22 +00001332 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001333#endif
bsalomon@google.com289533a2011-10-27 12:34:25 +00001334 prAA = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001335 }
bsalomon@google.com289533a2011-10-27 12:34:25 +00001336
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001337 GrPathRendererChain::DrawType type = prAA ? GrPathRendererChain::kColorAntiAlias_DrawType :
1338 GrPathRendererChain::kColor_DrawType;
1339
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001340 const SkPath* pathPtr = &path;
1341 SkPath tmpPath;
1342 SkStrokeRec strokeRec(stroke);
1343
1344 // Try a 1st time without stroking the path and without allowing the SW renderer
1345 GrPathRenderer* pr = this->getPathRenderer(*pathPtr, strokeRec, target, false, type);
1346
1347 if (NULL == pr) {
1348 if (!strokeRec.isHairlineStyle()) {
1349 // It didn't work the 1st time, so try again with the stroked path
1350 if (strokeRec.applyToPath(&tmpPath, *pathPtr)) {
1351 pathPtr = &tmpPath;
1352 strokeRec.setFillStyle();
1353 }
1354 }
1355 // This time, allow SW renderer
1356 pr = this->getPathRenderer(*pathPtr, strokeRec, target, true, type);
1357 }
1358
bsalomon@google.com30085192011-08-19 15:42:31 +00001359 if (NULL == pr) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001360#if GR_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001361 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001362#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001363 return;
1364 }
1365
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001366 pr->drawPath(*pathPtr, strokeRec, target, prAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001367}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001368
bsalomon@google.com27847de2011-02-22 20:59:41 +00001369////////////////////////////////////////////////////////////////////////////////
1370
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001371void GrContext::flush(int flagsBitfield) {
1372 if (kDiscard_FlushBit & flagsBitfield) {
1373 fDrawBuffer->reset();
1374 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001375 this->flushDrawBuffer();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001376 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001377 if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001378 fGpu->forceRenderTargetFlush();
1379 }
1380}
1381
bsalomon@google.com27847de2011-02-22 20:59:41 +00001382void GrContext::flushDrawBuffer() {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001383 if (NULL != fDrawBuffer && !fDrawBuffer->isFlushing()) {
1384 fDrawBuffer->flush();
junov@google.com53a55842011-06-08 22:55:10 +00001385 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001386}
1387
bsalomon@google.com9c680582013-02-06 18:17:50 +00001388bool GrContext::writeTexturePixels(GrTexture* texture,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001389 int left, int top, int width, int height,
1390 GrPixelConfig config, const void* buffer, size_t rowBytes,
1391 uint32_t flags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001392 SK_TRACE_EVENT0("GrContext::writeTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001393 ASSERT_OWNED_RESOURCE(texture);
1394
bsalomon@google.com9c680582013-02-06 18:17:50 +00001395 if ((kUnpremul_PixelOpsFlag & flags) || !fGpu->canWriteTexturePixels(texture, config)) {
1396 if (NULL != texture->asRenderTarget()) {
1397 return this->writeRenderTargetPixels(texture->asRenderTarget(),
1398 left, top, width, height,
1399 config, buffer, rowBytes, flags);
1400 } else {
1401 return false;
1402 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001403 }
bsalomon@google.com9c680582013-02-06 18:17:50 +00001404
bsalomon@google.com6f379512011-11-16 20:36:03 +00001405 if (!(kDontFlush_PixelOpsFlag & flags)) {
1406 this->flush();
1407 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001408
bsalomon@google.com9c680582013-02-06 18:17:50 +00001409 return fGpu->writeTexturePixels(texture, left, top, width, height,
1410 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001411}
1412
bsalomon@google.com0342a852012-08-20 19:22:38 +00001413bool GrContext::readTexturePixels(GrTexture* texture,
1414 int left, int top, int width, int height,
1415 GrPixelConfig config, void* buffer, size_t rowBytes,
1416 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001417 SK_TRACE_EVENT0("GrContext::readTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001418 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001419
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001420 // TODO: code read pixels for textures that aren't also rendertargets
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001421 GrRenderTarget* target = texture->asRenderTarget();
1422 if (NULL != target) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001423 return this->readRenderTargetPixels(target,
1424 left, top, width, height,
1425 config, buffer, rowBytes,
1426 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001427 } else {
1428 return false;
1429 }
1430}
1431
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001432#include "SkConfig8888.h"
1433
1434namespace {
1435/**
1436 * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel
1437 * formats are representable as Config8888 and so the function returns false
1438 * if the GrPixelConfig has no equivalent Config8888.
1439 */
1440bool grconfig_to_config8888(GrPixelConfig config,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001441 bool unpremul,
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001442 SkCanvas::Config8888* config8888) {
1443 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001444 case kRGBA_8888_GrPixelConfig:
1445 if (unpremul) {
1446 *config8888 = SkCanvas::kRGBA_Unpremul_Config8888;
1447 } else {
1448 *config8888 = SkCanvas::kRGBA_Premul_Config8888;
1449 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001450 return true;
bsalomon@google.com0342a852012-08-20 19:22:38 +00001451 case kBGRA_8888_GrPixelConfig:
1452 if (unpremul) {
1453 *config8888 = SkCanvas::kBGRA_Unpremul_Config8888;
1454 } else {
1455 *config8888 = SkCanvas::kBGRA_Premul_Config8888;
1456 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001457 return true;
1458 default:
1459 return false;
1460 }
1461}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001462
1463// It returns a configuration with where the byte position of the R & B components are swapped in
1464// relation to the input config. This should only be called with the result of
1465// grconfig_to_config8888 as it will fail for other configs.
1466SkCanvas::Config8888 swap_config8888_red_and_blue(SkCanvas::Config8888 config8888) {
1467 switch (config8888) {
1468 case SkCanvas::kBGRA_Premul_Config8888:
1469 return SkCanvas::kRGBA_Premul_Config8888;
1470 case SkCanvas::kBGRA_Unpremul_Config8888:
1471 return SkCanvas::kRGBA_Unpremul_Config8888;
1472 case SkCanvas::kRGBA_Premul_Config8888:
1473 return SkCanvas::kBGRA_Premul_Config8888;
1474 case SkCanvas::kRGBA_Unpremul_Config8888:
1475 return SkCanvas::kBGRA_Unpremul_Config8888;
1476 default:
1477 GrCrash("Unexpected input");
1478 return SkCanvas::kBGRA_Unpremul_Config8888;;
1479 }
1480}
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001481}
1482
bsalomon@google.com0342a852012-08-20 19:22:38 +00001483bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
1484 int left, int top, int width, int height,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001485 GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001486 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001487 SK_TRACE_EVENT0("GrContext::readRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001488 ASSERT_OWNED_RESOURCE(target);
1489
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001490 if (NULL == target) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001491 target = fDrawState->getRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001492 if (NULL == target) {
1493 return false;
1494 }
1495 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001496
bsalomon@google.com6f379512011-11-16 20:36:03 +00001497 if (!(kDontFlush_PixelOpsFlag & flags)) {
1498 this->flush();
1499 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001500
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001501 // Determine which conversions have to be applied: flipY, swapRAnd, and/or unpremul.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001502
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001503 // If fGpu->readPixels would incur a y-flip cost then we will read the pixels upside down. We'll
1504 // either do the flipY by drawing into a scratch with a matrix or on the cpu after the read.
1505 bool flipY = fGpu->readPixelsWillPayForYFlip(target, left, top,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001506 width, height, dstConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001507 rowBytes);
bsalomon@google.com9c680582013-02-06 18:17:50 +00001508 // We ignore the preferred config if it is different than our config unless it is an R/B swap.
1509 // In that case we'll perform an R and B swap while drawing to a scratch texture of the swapped
1510 // config. Then we will call readPixels on the scratch with the swapped config. The swaps during
1511 // the draw cancels out the fact that we call readPixels with a config that is R/B swapped from
1512 // dstConfig.
1513 GrPixelConfig readConfig = dstConfig;
1514 bool swapRAndB = false;
1515 if (GrPixelConfigSwapRAndB(dstConfig) == fGpu->preferredReadPixelsConfig(dstConfig)) {
1516 readConfig = GrPixelConfigSwapRAndB(readConfig);
1517 swapRAndB = true;
1518 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001519
bsalomon@google.com0342a852012-08-20 19:22:38 +00001520 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001521
bsalomon@google.com9c680582013-02-06 18:17:50 +00001522 if (unpremul && !GrPixelConfigIs8888(dstConfig)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001523 // The unpremul flag is only allowed for these two configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001524 return false;
1525 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001526
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001527 // If the src is a texture and we would have to do conversions after read pixels, we instead
1528 // do the conversions by drawing the src to a scratch texture. If we handle any of the
1529 // conversions in the draw we set the corresponding bool to false so that we don't reapply it
1530 // on the read back pixels.
1531 GrTexture* src = target->asTexture();
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001532 GrAutoScratchTexture ast;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001533 if (NULL != src && (swapRAndB || unpremul || flipY)) {
1534 // Make the scratch a render target because we don't have a robust readTexturePixels as of
1535 // yet. It calls this function.
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001536 GrTextureDesc desc;
1537 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1538 desc.fWidth = width;
1539 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001540 desc.fConfig = readConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001541 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001542
bsalomon@google.com9c680582013-02-06 18:17:50 +00001543 // 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 +00001544 // match the passed rect. However, if we see many different size rectangles we will trash
1545 // our texture cache and pay the cost of creating and destroying many textures. So, we only
1546 // request an exact match when the caller is reading an entire RT.
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001547 ScratchTexMatch match = kApprox_ScratchTexMatch;
1548 if (0 == left &&
1549 0 == top &&
1550 target->width() == width &&
1551 target->height() == height &&
1552 fGpu->fullReadPixelsIsFasterThanPartial()) {
1553 match = kExact_ScratchTexMatch;
1554 }
1555 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001556 GrTexture* texture = ast.texture();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001557 if (texture) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001558 // compute a matrix to perform the draw
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001559 SkMatrix textureMatrix;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001560 textureMatrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001561 textureMatrix.postIDiv(src->width(), src->height());
1562
bsalomon@google.comadc65362013-01-28 14:26:09 +00001563 SkAutoTUnref<const GrEffectRef> effect;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001564 if (unpremul) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001565 effect.reset(this->createPMToUPMEffect(src, swapRAndB, textureMatrix));
1566 if (NULL != effect) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001567 unpremul = false; // we no longer need to do this on CPU after the read back.
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001568 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001569 }
1570 // If we failed to create a PM->UPM effect and have no other conversions to perform then
1571 // there is no longer any point to using the scratch.
bsalomon@google.comadc65362013-01-28 14:26:09 +00001572 if (NULL != effect || flipY || swapRAndB) {
1573 if (!effect) {
1574 effect.reset(GrConfigConversionEffect::Create(
1575 src,
1576 swapRAndB,
1577 GrConfigConversionEffect::kNone_PMConversion,
1578 textureMatrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001579 }
1580 swapRAndB = false; // we will handle the swap in the draw.
bsalomon@google.comc4364992011-11-07 15:54:49 +00001581
robertphillips@google.com13f181f2013-03-02 12:02:08 +00001582 // We protect the existing geometry here since it may not be
1583 // clear to the caller that a draw operation (i.e., drawSimpleRect)
1584 // can be invoked in this method
1585 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001586 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.comadc65362013-01-28 14:26:09 +00001587 GrAssert(effect);
bsalomon@google.com9e040ae2013-01-28 14:31:19 +00001588 drawState->setEffect(0, effect);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001589
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001590 drawState->setRenderTarget(texture->asRenderTarget());
bsalomon@google.com81712882012-11-01 17:12:34 +00001591 GrRect rect = GrRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001592 fGpu->drawSimpleRect(rect, NULL);
1593 // we want to read back from the scratch's origin
1594 left = 0;
1595 top = 0;
1596 target = texture->asRenderTarget();
1597 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001598 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001599 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001600 if (!fGpu->readPixels(target,
1601 left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001602 readConfig, buffer, rowBytes)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001603 return false;
1604 }
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001605 // Perform any conversions we weren't able to perform using a scratch texture.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001606 if (unpremul || swapRAndB) {
bsalomon@google.comccaa0022012-09-25 19:55:07 +00001607 // These are initialized to suppress a warning
1608 SkCanvas::Config8888 srcC8888 = SkCanvas::kNative_Premul_Config8888;
1609 SkCanvas::Config8888 dstC8888 = SkCanvas::kNative_Premul_Config8888;
1610
bsalomon@google.com9c680582013-02-06 18:17:50 +00001611 SkDEBUGCODE(bool c8888IsValid =) grconfig_to_config8888(dstConfig, false, &srcC8888);
1612 grconfig_to_config8888(dstConfig, unpremul, &dstC8888);
bsalomon@google.comccaa0022012-09-25 19:55:07 +00001613
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001614 if (swapRAndB) {
1615 GrAssert(c8888IsValid); // we should only do r/b swap on 8888 configs
1616 srcC8888 = swap_config8888_red_and_blue(srcC8888);
1617 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001618 GrAssert(c8888IsValid);
1619 uint32_t* b32 = reinterpret_cast<uint32_t*>(buffer);
1620 SkConvertConfig8888Pixels(b32, rowBytes, dstC8888,
1621 b32, rowBytes, srcC8888,
1622 width, height);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001623 }
1624 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001625}
1626
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001627void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1628 GrAssert(target);
1629 ASSERT_OWNED_RESOURCE(target);
1630 // In the future we may track whether there are any pending draws to this
1631 // target. We don't today so we always perform a flush. We don't promise
1632 // this to our clients, though.
1633 this->flush();
1634 fGpu->resolveRenderTarget(target);
1635}
1636
scroggo@google.coma2a31922012-12-07 19:14:45 +00001637void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst, const SkIPoint* topLeft) {
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001638 if (NULL == src || NULL == dst) {
1639 return;
1640 }
1641 ASSERT_OWNED_RESOURCE(src);
1642
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001643 // Writes pending to the source texture are not tracked, so a flush
1644 // is required to ensure that the copy captures the most recent contents
bsalomon@google.comadc65362013-01-28 14:26:09 +00001645 // of the source texture. See similar behavior in
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001646 // GrContext::resolveRenderTarget.
1647 this->flush();
1648
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001649 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001650 GrDrawState* drawState = fGpu->drawState();
1651 drawState->setRenderTarget(dst);
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001652 SkMatrix sampleM;
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001653 sampleM.setIDiv(src->width(), src->height());
scroggo@google.coma2a31922012-12-07 19:14:45 +00001654 SkIRect srcRect = SkIRect::MakeWH(dst->width(), dst->height());
1655 if (NULL != topLeft) {
1656 srcRect.offset(*topLeft);
1657 }
1658 SkIRect srcBounds = SkIRect::MakeWH(src->width(), src->height());
1659 if (!srcRect.intersect(srcBounds)) {
1660 return;
1661 }
1662 sampleM.preTranslate(SkIntToScalar(srcRect.fLeft), SkIntToScalar(srcRect.fTop));
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +00001663 drawState->createTextureEffect(0, src, sampleM);
scroggo@google.coma2a31922012-12-07 19:14:45 +00001664 SkRect dstR = SkRect::MakeWH(SkIntToScalar(srcRect.width()), SkIntToScalar(srcRect.height()));
1665 fGpu->drawSimpleRect(dstR, NULL);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001666}
1667
bsalomon@google.com9c680582013-02-06 18:17:50 +00001668bool GrContext::writeRenderTargetPixels(GrRenderTarget* target,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001669 int left, int top, int width, int height,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001670 GrPixelConfig srcConfig,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001671 const void* buffer,
1672 size_t rowBytes,
1673 uint32_t flags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001674 SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001675 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001676
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001677 if (NULL == target) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001678 target = fDrawState->getRenderTarget();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001679 if (NULL == target) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001680 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +00001681 }
1682 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001683
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001684 // TODO: when underlying api has a direct way to do this we should use it (e.g. glDrawPixels on
1685 // desktop GL).
1686
1687 // We will always call some form of writeTexturePixels and we will pass our flags on to it.
1688 // Thus, we don't perform a flush here since that call will do it (if the kNoFlush flag isn't
1689 // set.)
bsalomon@google.com27847de2011-02-22 20:59:41 +00001690
bsalomon@google.com0342a852012-08-20 19:22:38 +00001691 // If the RT is also a texture and we don't have to premultiply then take the texture path.
1692 // We expect to be at least as fast or faster since it doesn't use an intermediate texture as
1693 // we do below.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001694
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001695#if !GR_MAC_BUILD
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001696 // At least some drivers on the Mac get confused when glTexImage2D is called on a texture
1697 // attached to an FBO. The FBO still sees the old image. TODO: determine what OS versions and/or
1698 // HW is affected.
bsalomon@google.com9c680582013-02-06 18:17:50 +00001699 if (NULL != target->asTexture() && !(kUnpremul_PixelOpsFlag & flags) &&
1700 fGpu->canWriteTexturePixels(target->asTexture(), srcConfig)) {
1701 return this->writeTexturePixels(target->asTexture(),
1702 left, top, width, height,
1703 srcConfig, buffer, rowBytes, flags);
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001704 }
1705#endif
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001706
bsalomon@google.com9c680582013-02-06 18:17:50 +00001707 // We ignore the preferred config unless it is a R/B swap of the src config. In that case
1708 // we will upload the original src data to a scratch texture but we will spoof it as the swapped
1709 // config. This scratch will then have R and B swapped. We correct for this by swapping again
1710 // when drawing the scratch to the dst using a conversion effect.
1711 bool swapRAndB = false;
1712 GrPixelConfig writeConfig = srcConfig;
1713 if (fGpu->preferredWritePixelsConfig(srcConfig) == GrPixelConfigSwapRAndB(srcConfig)) {
1714 writeConfig = GrPixelConfigSwapRAndB(srcConfig);
1715 swapRAndB = true;
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001716 }
1717
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001718 GrTextureDesc desc;
1719 desc.fWidth = width;
1720 desc.fHeight = height;
bsalomon@google.com9c680582013-02-06 18:17:50 +00001721 desc.fConfig = writeConfig;
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001722 GrAutoScratchTexture ast(this, desc);
1723 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001724 if (NULL == texture) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001725 return false;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001726 }
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001727
bsalomon@google.comadc65362013-01-28 14:26:09 +00001728 SkAutoTUnref<const GrEffectRef> effect;
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001729 SkMatrix textureMatrix;
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001730 textureMatrix.setIDiv(texture->width(), texture->height());
1731
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001732 // allocate a tmp buffer and sw convert the pixels to premul
1733 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
1734
1735 if (kUnpremul_PixelOpsFlag & flags) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001736 if (!GrPixelConfigIs8888(srcConfig)) {
1737 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001738 }
bsalomon@google.comadc65362013-01-28 14:26:09 +00001739 effect.reset(this->createUPMToPMEffect(texture, swapRAndB, textureMatrix));
bsalomon@google.com9c680582013-02-06 18:17:50 +00001740 // 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 +00001741 if (NULL == effect) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001742 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1743 GR_DEBUGCODE(bool success = )
bsalomon@google.com9c680582013-02-06 18:17:50 +00001744 grconfig_to_config8888(srcConfig, true, &srcConfig8888);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001745 GrAssert(success);
1746 GR_DEBUGCODE(success = )
bsalomon@google.com9c680582013-02-06 18:17:50 +00001747 grconfig_to_config8888(srcConfig, false, &dstConfig8888);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001748 GrAssert(success);
1749 const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer);
1750 tmpPixels.reset(width * height);
1751 SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888,
1752 src, rowBytes, srcConfig8888,
1753 width, height);
1754 buffer = tmpPixels.get();
1755 rowBytes = 4 * width;
1756 }
1757 }
bsalomon@google.comadc65362013-01-28 14:26:09 +00001758 if (NULL == effect) {
1759 effect.reset(GrConfigConversionEffect::Create(texture,
1760 swapRAndB,
1761 GrConfigConversionEffect::kNone_PMConversion,
1762 textureMatrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001763 }
1764
bsalomon@google.com9c680582013-02-06 18:17:50 +00001765 if (!this->writeTexturePixels(texture,
1766 0, 0, width, height,
1767 writeConfig, buffer, rowBytes,
1768 flags & ~kUnpremul_PixelOpsFlag)) {
1769 return false;
1770 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001771
robertphillips@google.com13f181f2013-03-02 12:02:08 +00001772 // writeRenderTargetPixels can be called in the midst of drawing another
skia.committer@gmail.com0c23faf2013-03-03 07:16:29 +00001773 // object (e.g., when uploading a SW path rendering to the gpu while
robertphillips@google.com13f181f2013-03-02 12:02:08 +00001774 // drawing a rect) so preserve the current geometry.
1775 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001776 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.comadc65362013-01-28 14:26:09 +00001777 GrAssert(effect);
bsalomon@google.com9e040ae2013-01-28 14:31:19 +00001778 drawState->setEffect(0, effect);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001779
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001780 SkMatrix matrix;
bsalomon@google.com81712882012-11-01 17:12:34 +00001781 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001782 drawState->setViewMatrix(matrix);
1783 drawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001784
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001785 fGpu->drawSimpleRect(GrRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)), NULL);
bsalomon@google.com9c680582013-02-06 18:17:50 +00001786 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001787}
1788////////////////////////////////////////////////////////////////////////////////
1789
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001790GrDrawTarget* GrContext::prepareToDraw(const GrPaint* paint, BufferedDraw buffered) {
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001791 if (kNo_BufferedDraw == buffered && kYes_BufferedDraw == fLastDrawWasBuffered) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001792 this->flushDrawBuffer();
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001793 fLastDrawWasBuffered = kNo_BufferedDraw;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001794 }
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001795 if (NULL != paint) {
bsalomon@google.comaf84e742012-10-05 13:23:24 +00001796 GrAssert(fDrawState->stagesDisabled());
1797 fDrawState->setFromPaint(*paint);
1798#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
1799 if ((paint->hasMask() || 0xff != paint->fCoverage) &&
1800 !fGpu->canApplyCoverage()) {
1801 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1802 }
1803#endif
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001804 }
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001805 if (kYes_BufferedDraw == buffered) {
1806 fDrawBuffer->setClip(fGpu->getClip());
1807 fLastDrawWasBuffered = kYes_BufferedDraw;
1808 return fDrawBuffer;
1809 } else {
1810 GrAssert(kNo_BufferedDraw == buffered);
1811 return fGpu;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001812 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001813}
1814
robertphillips@google.com72176b22012-05-23 13:19:12 +00001815/*
1816 * This method finds a path renderer that can draw the specified path on
1817 * the provided target.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001818 * Due to its expense, the software path renderer has split out so it can
robertphillips@google.com72176b22012-05-23 13:19:12 +00001819 * can be individually allowed/disallowed via the "allowSW" boolean.
1820 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001821GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001822 const SkStrokeRec& stroke,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001823 const GrDrawTarget* target,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001824 bool allowSW,
1825 GrPathRendererChain::DrawType drawType,
1826 GrPathRendererChain::StencilSupport* stencilSupport) {
1827
bsalomon@google.com30085192011-08-19 15:42:31 +00001828 if (NULL == fPathRendererChain) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001829 fPathRendererChain = SkNEW_ARGS(GrPathRendererChain, (this));
bsalomon@google.com30085192011-08-19 15:42:31 +00001830 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001831
sugoi@google.com12b4e272012-12-06 20:13:11 +00001832 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path,
1833 stroke,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001834 target,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001835 drawType,
1836 stencilSupport);
robertphillips@google.com72176b22012-05-23 13:19:12 +00001837
1838 if (NULL == pr && allowSW) {
1839 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001840 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001841 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001842 pr = fSoftwarePathRenderer;
1843 }
1844
1845 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001846}
1847
bsalomon@google.com27847de2011-02-22 20:59:41 +00001848////////////////////////////////////////////////////////////////////////////////
1849
bsalomon@google.com27847de2011-02-22 20:59:41 +00001850void GrContext::setRenderTarget(GrRenderTarget* target) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001851 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001852 fDrawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001853}
1854
1855GrRenderTarget* GrContext::getRenderTarget() {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001856 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001857}
1858
1859const GrRenderTarget* GrContext::getRenderTarget() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001860 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001861}
1862
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001863bool GrContext::isConfigRenderable(GrPixelConfig config) const {
1864 return fGpu->isConfigRenderable(config);
1865}
1866
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001867const SkMatrix& GrContext::getMatrix() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001868 return fDrawState->getViewMatrix();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001869}
1870
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001871void GrContext::setMatrix(const SkMatrix& m) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001872 fDrawState->setViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001873}
1874
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +00001875void GrContext::setIdentityMatrix() {
1876 fDrawState->viewMatrix()->reset();
1877}
1878
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001879void GrContext::concatMatrix(const SkMatrix& m) const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001880 fDrawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001881}
1882
1883static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
1884 intptr_t mask = 1 << shift;
1885 if (pred) {
1886 bits |= mask;
1887 } else {
1888 bits &= ~mask;
1889 }
1890 return bits;
1891}
1892
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001893void GrContext::setupDrawBuffer() {
1894
1895 GrAssert(NULL == fDrawBuffer);
1896 GrAssert(NULL == fDrawBufferVBAllocPool);
1897 GrAssert(NULL == fDrawBufferIBAllocPool);
1898
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001899 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001900 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001901 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001902 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001903 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001904 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001905 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001906 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001907
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001908 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001909 fDrawBufferVBAllocPool,
1910 fDrawBufferIBAllocPool));
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001911
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001912 fDrawBuffer->setDrawState(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001913}
1914
bsalomon@google.com27847de2011-02-22 20:59:41 +00001915GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) {
bsalomon@google.com82b0ec62013-02-07 21:02:36 +00001916 return this->prepareToDraw(&paint, BUFFERED_DRAW);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001917}
1918
1919const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1920 return fGpu->getQuadIndexBuffer();
1921}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001922
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001923namespace {
1924void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
1925 GrConfigConversionEffect::PMConversion pmToUPM;
1926 GrConfigConversionEffect::PMConversion upmToPM;
1927 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
1928 *pmToUPMValue = pmToUPM;
1929 *upmToPMValue = upmToPM;
1930}
1931}
1932
bsalomon@google.comadc65362013-01-28 14:26:09 +00001933const GrEffectRef* GrContext::createPMToUPMEffect(GrTexture* texture,
1934 bool swapRAndB,
1935 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001936 if (!fDidTestPMConversions) {
1937 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001938 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001939 }
1940 GrConfigConversionEffect::PMConversion pmToUPM =
1941 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
1942 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001943 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001944 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001945 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001946 }
1947}
1948
bsalomon@google.comadc65362013-01-28 14:26:09 +00001949const GrEffectRef* GrContext::createUPMToPMEffect(GrTexture* texture,
1950 bool swapRAndB,
1951 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001952 if (!fDidTestPMConversions) {
1953 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001954 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001955 }
1956 GrConfigConversionEffect::PMConversion upmToPM =
1957 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
1958 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001959 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001960 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001961 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001962 }
1963}
1964
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001965GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture,
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001966 bool canClobberSrc,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001967 const SkRect& rect,
1968 float sigmaX, float sigmaY) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001969 ASSERT_OWNED_RESOURCE(srcTexture);
robertphillips@google.comccb39502012-10-01 18:25:13 +00001970
1971 AutoRenderTarget art(this);
1972
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001973 AutoMatrix am;
1974 am.setIdentity(this);
1975
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001976 SkIRect clearRect;
bsalomon@google.comb505a122012-05-31 18:40:36 +00001977 int scaleFactorX, radiusX;
1978 int scaleFactorY, radiusY;
1979 sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX);
1980 sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY);
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001981
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001982 SkRect srcRect(rect);
1983 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
1984 srcRect.roundOut();
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001985 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001986 static_cast<float>(scaleFactorY));
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001987
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001988 AutoClip acs(this, srcRect);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001989
bsalomon@google.com0342a852012-08-20 19:22:38 +00001990 GrAssert(kBGRA_8888_GrPixelConfig == srcTexture->config() ||
1991 kRGBA_8888_GrPixelConfig == srcTexture->config() ||
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001992 kAlpha_8_GrPixelConfig == srcTexture->config());
1993
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001994 GrTextureDesc desc;
1995 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1996 desc.fWidth = SkScalarFloorToInt(srcRect.width());
1997 desc.fHeight = SkScalarFloorToInt(srcRect.height());
1998 desc.fConfig = srcTexture->config();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001999
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00002000 GrAutoScratchTexture temp1, temp2;
2001 GrTexture* dstTexture = temp1.set(this, desc);
2002 GrTexture* tempTexture = canClobberSrc ? srcTexture : temp2.set(this, desc);
robertphillips@google.com7d126752012-10-19 12:56:26 +00002003 if (NULL == dstTexture || NULL == tempTexture) {
2004 return NULL;
2005 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002006
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002007 GrPaint paint;
2008 paint.reset();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002009
2010 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +00002011 SkMatrix matrix;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +00002012 matrix.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002013 this->setRenderTarget(dstTexture->asRenderTarget());
2014 SkRect dstRect(srcRect);
2015 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +00002016 i < scaleFactorY ? 0.5f : 1.0f);
2017
bsalomon@google.com68b58c92013-01-17 16:50:08 +00002018 paint.colorStage(0)->setEffect(GrSimpleTextureEffect::Create(srcTexture,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +00002019 matrix,
2020 true))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002021 this->drawRectToRect(paint, dstRect, srcRect);
2022 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00002023 srcTexture = dstTexture;
2024 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002025 }
2026
robertphillips@google.com7a396332012-05-10 15:11:27 +00002027 SkIRect srcIRect;
2028 srcRect.roundOut(&srcIRect);
2029
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002030 if (sigmaX > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002031 if (scaleFactorX > 1) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00002032 // Clear out a radius to the right of the srcRect to prevent the
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002033 // X convolution from reading garbage.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002034 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
bsalomon@google.comb505a122012-05-31 18:40:36 +00002035 radiusX, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002036 this->clear(&clearRect, 0x0);
2037 }
2038
2039 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com82b0ec62013-02-07 21:02:36 +00002040 GrDrawTarget* target = this->prepareToDraw(NULL, BUFFERED_DRAW);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00002041 convolve_gaussian(target, srcTexture, srcRect, sigmaX, radiusX,
bsalomon@google.comb505a122012-05-31 18:40:36 +00002042 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00002043 srcTexture = dstTexture;
2044 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002045 }
2046
2047 if (sigmaY > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002048 if (scaleFactorY > 1 || sigmaX > 0.0f) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00002049 // Clear out a radius below the srcRect to prevent the Y
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002050 // convolution from reading garbage.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002051 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
bsalomon@google.comb505a122012-05-31 18:40:36 +00002052 srcIRect.width(), radiusY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002053 this->clear(&clearRect, 0x0);
2054 }
2055
2056 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com82b0ec62013-02-07 21:02:36 +00002057 GrDrawTarget* target = this->prepareToDraw(NULL, BUFFERED_DRAW);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00002058 convolve_gaussian(target, srcTexture, srcRect, sigmaY, radiusY,
bsalomon@google.comb505a122012-05-31 18:40:36 +00002059 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00002060 srcTexture = dstTexture;
2061 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002062 }
2063
2064 if (scaleFactorX > 1 || scaleFactorY > 1) {
2065 // Clear one pixel to the right and below, to accommodate bilinear
2066 // upsampling.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002067 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
robertphillips@google.com7a396332012-05-10 15:11:27 +00002068 srcIRect.width() + 1, 1);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002069 this->clear(&clearRect, 0x0);
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002070 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
robertphillips@google.com7a396332012-05-10 15:11:27 +00002071 1, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002072 this->clear(&clearRect, 0x0);
bsalomon@google.comb9086a02012-11-01 18:02:54 +00002073 SkMatrix matrix;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002074 // FIXME: This should be mitchell, not bilinear.
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +00002075 matrix.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002076 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com68b58c92013-01-17 16:50:08 +00002077 paint.colorStage(0)->setEffect(GrSimpleTextureEffect::Create(srcTexture,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +00002078 matrix,
2079 true))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002080 SkRect dstRect(srcRect);
robertphillips@google.com7a396332012-05-10 15:11:27 +00002081 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002082 this->drawRectToRect(paint, dstRect, srcRect);
2083 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00002084 srcTexture = dstTexture;
2085 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002086 }
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00002087 if (srcTexture == temp1.texture()) {
2088 return temp1.detach();
2089 } else if (srcTexture == temp2.texture()) {
2090 return temp2.detach();
2091 } else {
2092 srcTexture->ref();
2093 return srcTexture;
2094 }
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00002095}
bsalomon@google.com1e266f82011-12-12 16:11:33 +00002096
bsalomon@google.comc4364992011-11-07 15:54:49 +00002097///////////////////////////////////////////////////////////////////////////////
robertphillips@google.com59552022012-08-31 13:07:37 +00002098#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002099void GrContext::printCacheStats() const {
2100 fTextureCache->printStats();
2101}
2102#endif