blob: 4798297a3aa959e6eaff25e15397b60e137eb490 [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 };
bsalomon@google.comae81d5c2013-03-20 17:32:27 +0000363
364 static const GrAttribBindings kAttribBindings = GrDrawState::kLocalCoords_AttribBindingsBit;
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);
bsalomon@google.comae81d5c2013-03-20 17:32:27 +0000368 drawState->setAttribIndex(GrDrawState::kLocalCoords_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,
bsalomon@google.comae81d5c2013-03-20 17:32:27 +0000855 const GrRect& localRect,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000856 const SkMatrix* dstMatrix,
bsalomon@google.comae81d5c2013-03-20 17:32:27 +0000857 const SkMatrix* localMatrix) {
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.com82b0ec62013-02-07 21:02:36 +0000860 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW);
bsalomon@google.comae81d5c2013-03-20 17:32:27 +0000861 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com64386952013-02-08 21:22:44 +0000862
863#if GR_STATIC_RECT_VB
bsalomon@google.com64386952013-02-08 21:22:44 +0000864 GrDrawState* drawState = target->drawState();
865
866 SkMatrix m;
867
868 m.setAll(dstRect.width(), 0, dstRect.fLeft,
869 0, dstRect.height(), dstRect.fTop,
870 0, 0, SkMatrix::I()[8]);
871 if (NULL != dstMatrix) {
872 m.postConcat(*dstMatrix);
873 }
874
bsalomon@google.comae81d5c2013-03-20 17:32:27 +0000875 // This code path plays a little fast and loose with the notion of local coords and coord
876 // change matrices in order to account for localRect and localMatrix. The unit square VB only
877 // has one set of coords. Rather than using AutoViewMatrixRestore we instead directly set concat
878 // with m and then call GrDrawState::localCoordChange() with a matrix that accounts for
879 // localRect and localMatrix. This code path is preventing some encapsulation in GrDrawState.
880 SkMatrix savedViewMatrix = drawState->getViewMatrix();
881 drawState->preConcatViewMatrix(m);
bsalomon@google.com64386952013-02-08 21:22:44 +0000882
bsalomon@google.comae81d5c2013-03-20 17:32:27 +0000883 m.setAll(localRect.width(), 0, localRect.fLeft,
884 0, localRect.height(), localRect.fTop,
885 0, 0, SkMatrix::I()[8]);
886 if (NULL != localMatrix) {
887 m.postConcat(*localMatrix);
bsalomon@google.com64386952013-02-08 21:22:44 +0000888 }
bsalomon@google.comae81d5c2013-03-20 17:32:27 +0000889 drawState->localCoordChange(m);
bsalomon@google.com64386952013-02-08 21:22:44 +0000890
891 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
892 if (NULL == sqVB) {
893 GrPrintf("Failed to create static rect vb.\n");
894 return;
895 }
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000896 drawState->setDefaultVertexAttribs();
bsalomon@google.com64386952013-02-08 21:22:44 +0000897 target->setVertexSourceToBuffer(sqVB);
898 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.comae81d5c2013-03-20 17:32:27 +0000899 drawState->setViewMatrix(savedViewMatrix);
bsalomon@google.com64386952013-02-08 21:22:44 +0000900#else
bsalomon@google.comae81d5c2013-03-20 17:32:27 +0000901 target->drawRect(dstRect, dstMatrix, &localRect, localMatrix);
bsalomon@google.com64386952013-02-08 21:22:44 +0000902#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000903}
904
905void GrContext::drawVertices(const GrPaint& paint,
906 GrPrimitiveType primitiveType,
907 int vertexCount,
908 const GrPoint positions[],
909 const GrPoint texCoords[],
910 const GrColor colors[],
911 const uint16_t indices[],
912 int indexCount) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000913 SK_TRACE_EVENT0("GrContext::drawVertices");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000914
915 GrDrawTarget::AutoReleaseGeometry geo;
916
bsalomon@google.com82b0ec62013-02-07 21:02:36 +0000917 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000918 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000919
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000920 GrDrawState* drawState = target->drawState();
jvanverth@google.comb8b705b2013-02-28 16:28:34 +0000921
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000922 GrVertexAttribArray<3> attribs;
923 size_t currentOffset = 0;
924 int colorOffset = -1, texOffset = -1;
925 GrAttribBindings bindings = GrDrawState::kDefault_AttribBindings;
926
927 // set position attribute
928 drawState->setAttribIndex(GrDrawState::kPosition_AttribIndex, attribs.count());
jvanverth@google.com3b0d6312013-03-01 20:30:01 +0000929 GrVertexAttrib currAttrib = {kVec2f_GrVertexAttribType, currentOffset};
930 attribs.push_back(currAttrib);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000931 currentOffset += sizeof(GrPoint);
932
933 // set up optional texture coordinate attributes
934 if (NULL != texCoords) {
bsalomon@google.comae81d5c2013-03-20 17:32:27 +0000935 bindings |= GrDrawState::kLocalCoords_AttribBindingsBit;
936 drawState->setAttribIndex(GrDrawState::kLocalCoords_AttribIndex, attribs.count());
jvanverth@google.com3b0d6312013-03-01 20:30:01 +0000937 currAttrib.set(kVec2f_GrVertexAttribType, currentOffset);
938 attribs.push_back(currAttrib);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000939 texOffset = currentOffset;
940 currentOffset += sizeof(GrPoint);
941 }
942
943 // set up optional color attributes
944 if (NULL != colors) {
945 bindings |= GrDrawState::kColor_AttribBindingsBit;
946 drawState->setAttribIndex(GrDrawState::kColor_AttribIndex, attribs.count());
jvanverth@google.com3b0d6312013-03-01 20:30:01 +0000947 currAttrib.set(kVec4ub_GrVertexAttribType, currentOffset);
948 attribs.push_back(currAttrib);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000949 colorOffset = currentOffset;
950 currentOffset += sizeof(GrColor);
951 }
952
953 drawState->setVertexAttribs(attribs.begin(), attribs.count());
954 drawState->setAttribBindings(bindings);
955
956 size_t vertexSize = drawState->getVertexSize();
957 GrAssert(vertexSize == currentOffset);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000958 if (sizeof(GrPoint) != vertexSize) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000959 if (!geo.set(target, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000960 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000961 return;
962 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000963 void* curVertex = geo.vertices();
964
965 for (int i = 0; i < vertexCount; ++i) {
966 *((GrPoint*)curVertex) = positions[i];
967
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000968 if (texOffset >= 0) {
jvanverth@google.com39768252013-02-14 15:25:44 +0000969 *(GrPoint*)((intptr_t)curVertex + texOffset) = texCoords[i];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000970 }
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000971 if (colorOffset >= 0) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000972 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
973 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000974 curVertex = (void*)((intptr_t)curVertex + vertexSize);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000975 }
976 } else {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000977 target->setVertexSourceToArray(positions, vertexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000978 }
979
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000980 // we don't currently apply offscreen AA to this path. Need improved
bsalomon@google.com91958362011-06-13 17:58:13 +0000981 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000982
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000983 if (NULL != indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000984 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000985 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000986 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000987 target->drawNonIndexed(primitiveType, 0, vertexCount);
988 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000989}
990
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000991///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com150d2842012-01-12 20:19:56 +0000992namespace {
993
bsalomon@google.com93c96602012-04-27 13:05:21 +0000994struct CircleVertex {
995 GrPoint fPos;
996 GrPoint fCenter;
bsalomon@google.com81712882012-11-01 17:12:34 +0000997 SkScalar fOuterRadius;
998 SkScalar fInnerRadius;
bsalomon@google.com93c96602012-04-27 13:05:21 +0000999};
1000
jvanverth@google.com65eb4d52013-03-19 18:51:02 +00001001struct EllipseVertex {
1002 GrPoint fPos;
1003 GrPoint fCenter;
1004 SkScalar fOuterXRadius;
1005 SkScalar fOuterXYRatio;
1006 SkScalar fInnerXRadius;
1007 SkScalar fInnerXYRatio;
1008};
1009
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001010inline bool circleStaysCircle(const SkMatrix& m) {
1011 return m.isSimilarity();
bsalomon@google.com93c96602012-04-27 13:05:21 +00001012}
1013
1014}
1015
bsalomon@google.com93c96602012-04-27 13:05:21 +00001016void GrContext::drawOval(const GrPaint& paint,
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001017 const GrRect& oval,
1018 const SkStrokeRec& stroke) {
1019
jvanverth@google.com65eb4d52013-03-19 18:51:02 +00001020 bool isCircle;
1021 if (!canDrawOval(paint, oval, &isCircle)) {
bsalomon@google.com93c96602012-04-27 13:05:21 +00001022 SkPath path;
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001023 path.addOval(oval);
1024 this->drawPath(paint, path, stroke);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001025 return;
skia.committer@gmail.com98ded842013-01-23 07:06:17 +00001026 }
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001027
jvanverth@google.com65eb4d52013-03-19 18:51:02 +00001028 if (isCircle) {
1029 this->internalDrawCircle(paint, oval, stroke);
1030 } else {
1031 this->internalDrawOval(paint, oval, stroke);
1032 }
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001033}
1034
jvanverth@google.com65eb4d52013-03-19 18:51:02 +00001035bool GrContext::canDrawOval(const GrPaint& paint, const GrRect& oval, bool* isCircle) const {
1036 GrAssert(isCircle != NULL);
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001037
1038 if (!paint.isAntiAlias()) {
1039 return false;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001040 }
skia.committer@gmail.com98ded842013-01-23 07:06:17 +00001041
skia.committer@gmail.com01c34ee2013-03-20 07:01:02 +00001042 // we can draw circles
jvanverth@google.com65eb4d52013-03-19 18:51:02 +00001043 *isCircle = SkScalarNearlyEqual(oval.width(), oval.height())
1044 && circleStaysCircle(this->getMatrix());
1045 // and axis-aligned ellipses only
1046 bool isAxisAlignedEllipse = this->getMatrix().rectStaysRect();
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001047
jvanverth@google.com65eb4d52013-03-19 18:51:02 +00001048 return *isCircle || isAxisAlignedEllipse;
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001049}
1050
1051void GrContext::internalDrawOval(const GrPaint& paint,
1052 const GrRect& oval,
1053 const SkStrokeRec& stroke) {
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001054#ifdef SK_DEBUG
1055 {
1056 // we should have checked for this previously
jvanverth@google.com65eb4d52013-03-19 18:51:02 +00001057 bool isAxisAlignedEllipse = this->getMatrix().rectStaysRect();
1058 SkASSERT(paint.isAntiAlias() && isAxisAlignedEllipse);
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001059 }
1060#endif
bsalomon@google.com82b0ec62013-02-07 21:02:36 +00001061 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001062
bsalomon@google.com0982d352012-07-31 15:33:25 +00001063 GrDrawState* drawState = target->drawState();
1064 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com0982d352012-07-31 15:33:25 +00001065
bsalomon@google.com93c96602012-04-27 13:05:21 +00001066 const GrRenderTarget* rt = drawState->getRenderTarget();
1067 if (NULL == rt) {
1068 return;
1069 }
1070
jvanverth@google.com65eb4d52013-03-19 18:51:02 +00001071 const SkMatrix vm = drawState->getViewMatrix();
1072
1073 GrDrawState::AutoDeviceCoordDraw adcd(drawState);
1074 if (!adcd.succeeded()) {
1075 return;
1076 }
1077
1078 // position + edge
1079 static const GrVertexAttrib kVertexAttribs[] = {
1080 {kVec2f_GrVertexAttribType, 0},
1081 {kVec2f_GrVertexAttribType, sizeof(GrPoint)},
1082 {kVec4f_GrVertexAttribType, 2*sizeof(GrPoint)}
1083 };
1084 drawState->setVertexAttribs(kVertexAttribs, SK_ARRAY_COUNT(kVertexAttribs));
1085 drawState->setAttribIndex(GrDrawState::kPosition_AttribIndex, 0);
1086 GrAssert(sizeof(EllipseVertex) == drawState->getVertexSize());
1087
1088 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
1089 if (!geo.succeeded()) {
1090 GrPrintf("Failed to get space for vertices!\n");
1091 return;
1092 }
1093
1094 EllipseVertex* verts = reinterpret_cast<EllipseVertex*>(geo.vertices());
1095
1096 GrPoint center = GrPoint::Make(oval.centerX(), oval.centerY());
1097 vm.mapPoints(&center, 1);
1098
1099 SkStrokeRec::Style style = stroke.getStyle();
1100 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
1101 enum {
1102 // the edge effects share this stage with glyph rendering
1103 // (kGlyphMaskStage in GrTextContext) && SW path rendering
1104 // (kPathMaskStage in GrSWMaskHelper)
1105 kEdgeEffectStage = GrPaint::kTotalStages,
1106 };
1107 drawState->setAttribBindings(GrDrawState::kDefault_AttribBindings);
1108
1109 GrEffectRef* effect = GrEllipseEdgeEffect::Create(isStroked);
1110 static const int kEllipseCenterAttrIndex = 1;
1111 static const int kEllipseEdgeAttrIndex = 2;
1112 drawState->setEffect(kEdgeEffectStage, effect,
1113 kEllipseCenterAttrIndex, kEllipseEdgeAttrIndex)->unref();
1114
1115 SkRect xformedRect;
1116 vm.mapRect(&xformedRect, oval);
1117
1118 SkScalar xRadius = SkScalarHalf(xformedRect.width());
1119 SkScalar yRadius = SkScalarHalf(xformedRect.height());
1120 SkScalar innerXRadius = 0.0f;
1121 SkScalar innerRatio = 1.0f;
1122
1123 if (SkStrokeRec::kFill_Style != style) {
1124 SkScalar strokeWidth = stroke.getWidth();
1125
1126 // do (potentially) anisotropic mapping
1127 SkVector scaledStroke;
1128 scaledStroke.set(strokeWidth, strokeWidth);
1129 vm.mapVectors(&scaledStroke, 1);
1130
1131 if (SkScalarNearlyZero(scaledStroke.length())) {
1132 scaledStroke.set(SK_ScalarHalf, SK_ScalarHalf);
1133 } else {
1134 scaledStroke.scale(0.5f);
1135 }
1136
1137 // this is legit only if scale & translation (which should be the case at the moment)
1138 if (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style) {
1139 SkScalar innerYRadius = SkMaxScalar(0, yRadius - scaledStroke.fY);
1140 if (innerYRadius > SK_ScalarNearlyZero) {
1141 innerXRadius = SkMaxScalar(0, xRadius - scaledStroke.fX);
1142 innerRatio = innerXRadius/innerYRadius;
1143 }
1144 }
1145 xRadius += scaledStroke.fX;
1146 yRadius += scaledStroke.fY;
1147 }
1148
1149 SkScalar outerRatio = SkScalarDiv(xRadius, yRadius);
1150
1151 for (int i = 0; i < 4; ++i) {
1152 verts[i].fCenter = center;
1153 verts[i].fOuterXRadius = xRadius + 0.5f;
1154 verts[i].fOuterXYRatio = outerRatio;
1155 verts[i].fInnerXRadius = innerXRadius - 0.5f;
1156 verts[i].fInnerXYRatio = innerRatio;
1157 }
1158
1159 SkScalar L = -xRadius;
1160 SkScalar R = +xRadius;
1161 SkScalar T = -yRadius;
1162 SkScalar B = +yRadius;
1163
1164 // We've extended the outer x radius out half a pixel to antialias.
1165 // Expand the drawn rect here so all the pixels will be captured.
1166 L += center.fX - SK_ScalarHalf;
1167 R += center.fX + SK_ScalarHalf;
1168 T += center.fY - SK_ScalarHalf;
1169 B += center.fY + SK_ScalarHalf;
1170
1171 verts[0].fPos = SkPoint::Make(L, T);
1172 verts[1].fPos = SkPoint::Make(R, T);
1173 verts[2].fPos = SkPoint::Make(L, B);
1174 verts[3].fPos = SkPoint::Make(R, B);
1175
1176 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
1177}
1178
1179void GrContext::internalDrawCircle(const GrPaint& paint,
1180 const GrRect& circle,
1181 const SkStrokeRec& stroke) {
1182
1183 SkScalar radius = SkScalarHalf(circle.width());
1184
1185 SkScalar strokeWidth = stroke.getWidth();
1186 SkStrokeRec::Style style = stroke.getStyle();
1187
1188 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW);
1189
1190 GrDrawState* drawState = target->drawState();
1191 GrDrawState::AutoStageDisable atr(fDrawState);
1192
1193 const GrRenderTarget* rt = drawState->getRenderTarget();
1194 if (NULL == rt) {
1195 return;
1196 }
1197
1198 const SkMatrix vm = drawState->getViewMatrix();
1199
bsalomon@google.com5b3e8902012-10-05 20:13:28 +00001200 GrDrawState::AutoDeviceCoordDraw adcd(drawState);
bsalomon@google.come3d32162012-07-20 13:37:06 +00001201 if (!adcd.succeeded()) {
1202 return;
1203 }
bsalomon@google.com93c96602012-04-27 13:05:21 +00001204
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001205 // position + edge
1206 static const GrVertexAttrib kVertexAttribs[] = {
jvanverth@google.com3b0d6312013-03-01 20:30:01 +00001207 {kVec2f_GrVertexAttribType, 0},
1208 {kVec4f_GrVertexAttribType, sizeof(GrPoint)}
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001209 };
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001210 drawState->setVertexAttribs(kVertexAttribs, SK_ARRAY_COUNT(kVertexAttribs));
1211 drawState->setAttribIndex(GrDrawState::kPosition_AttribIndex, 0);
jvanverth@google.comb75b0a02013-02-05 20:33:30 +00001212 GrAssert(sizeof(CircleVertex) == drawState->getVertexSize());
bsalomon@google.com93c96602012-04-27 13:05:21 +00001213
jvanverth@google.comb75b0a02013-02-05 20:33:30 +00001214 GrDrawTarget::AutoReleaseGeometry geo(target, 4, 0);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001215 if (!geo.succeeded()) {
1216 GrPrintf("Failed to get space for vertices!\n");
1217 return;
1218 }
1219
1220 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
1221
jvanverth@google.com65eb4d52013-03-19 18:51:02 +00001222 GrPoint center = GrPoint::Make(circle.centerX(), circle.centerY());
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001223 vm.mapPoints(&center, 1);
1224
jvanverth@google.com65eb4d52013-03-19 18:51:02 +00001225 bool isStroked = (SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style);
1226 enum {
1227 // the edge effects share this stage with glyph rendering
1228 // (kGlyphMaskStage in GrTextContext) && SW path rendering
1229 // (kPathMaskStage in GrSWMaskHelper)
1230 kEdgeEffectStage = GrPaint::kTotalStages,
1231 };
1232 drawState->setAttribBindings(GrDrawState::kDefault_AttribBindings);
skia.committer@gmail.com01c34ee2013-03-20 07:01:02 +00001233
jvanverth@google.com65eb4d52013-03-19 18:51:02 +00001234 GrEffectRef* effect = GrCircleEdgeEffect::Create(isStroked);
1235 static const int kCircleEdgeAttrIndex = 1;
1236 drawState->setEffect(kEdgeEffectStage, effect, kCircleEdgeAttrIndex)->unref();
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001237
jvanverth@google.com65eb4d52013-03-19 18:51:02 +00001238 radius = vm.mapRadius(radius);
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001239
jvanverth@google.com65eb4d52013-03-19 18:51:02 +00001240 SkScalar innerRadius = -2.0f;
1241 SkScalar outerRadius = radius;
1242 SkScalar halfWidth = 0;
1243 if (style != SkStrokeRec::kFill_Style) {
1244 strokeWidth = vm.mapRadius(strokeWidth);
1245 if (SkScalarNearlyZero(strokeWidth)) {
1246 halfWidth = SK_ScalarHalf;
1247 } else {
1248 halfWidth = SkScalarHalf(strokeWidth);
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001249 }
1250
jvanverth@google.com65eb4d52013-03-19 18:51:02 +00001251 outerRadius += halfWidth;
1252 if (isStroked) {
1253 innerRadius = SkMaxScalar(0, radius - halfWidth);
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001254 }
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001255 }
1256
jvanverth@google.com65eb4d52013-03-19 18:51:02 +00001257 for (int i = 0; i < 4; ++i) {
1258 verts[i].fCenter = center;
1259 verts[i].fOuterRadius = outerRadius + 0.5f;
1260 verts[i].fInnerRadius = innerRadius - 0.5f;
1261 }
1262
1263 SkScalar L = -outerRadius;
1264 SkScalar R = +outerRadius;
1265 SkScalar T = -outerRadius;
1266 SkScalar B = +outerRadius;
1267
1268 // We've extended the outer radius out half a pixel to antialias.
1269 // Expand the drawn rect here so all the pixels will be captured.
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001270 L += center.fX - SK_ScalarHalf;
1271 R += center.fX + SK_ScalarHalf;
1272 T += center.fY - SK_ScalarHalf;
1273 B += center.fY + SK_ScalarHalf;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001274
1275 verts[0].fPos = SkPoint::Make(L, T);
1276 verts[1].fPos = SkPoint::Make(R, T);
1277 verts[2].fPos = SkPoint::Make(L, B);
1278 verts[3].fPos = SkPoint::Make(R, B);
1279
bsalomon@google.com47059542012-06-06 20:51:20 +00001280 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001281}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001282
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001283void GrContext::drawPath(const GrPaint& paint, const SkPath& path, const SkStrokeRec& stroke) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001284
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001285 if (path.isEmpty()) {
sugoi@google.com12b4e272012-12-06 20:13:11 +00001286 if (path.isInverseFillType()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001287 this->drawPaint(paint);
1288 }
1289 return;
1290 }
1291
bsalomon@google.com93c96602012-04-27 13:05:21 +00001292 SkRect ovalRect;
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001293 bool isOval = path.isOval(&ovalRect);
1294
jvanverth@google.com65eb4d52013-03-19 18:51:02 +00001295 bool isCircle;
1296 if (isOval && !path.isInverseFillType() && this->canDrawOval(paint, ovalRect, &isCircle)) {
1297 if (isCircle) {
1298 this->internalDrawCircle(paint, ovalRect, stroke);
1299 } else {
1300 this->internalDrawOval(paint, ovalRect, stroke);
1301 }
bsalomon@google.com93c96602012-04-27 13:05:21 +00001302 return;
1303 }
1304
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001305 this->internalDrawPath(paint, path, stroke);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001306}
1307
jvanverth@google.com65eb4d52013-03-19 18:51:02 +00001308void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path,
1309 const SkStrokeRec& stroke) {
bsalomon@google.com93c96602012-04-27 13:05:21 +00001310
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001311 // Note that below we may sw-rasterize the path into a scratch texture.
1312 // Scratch textures can be recycled after they are returned to the texture
1313 // cache. This presents a potential hazard for buffered drawing. However,
1314 // the writePixels that uploads to the scratch will perform a flush so we're
1315 // OK.
bsalomon@google.com82b0ec62013-02-07 21:02:36 +00001316 GrDrawTarget* target = this->prepareToDraw(&paint, BUFFERED_DRAW);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +00001317 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001318
bsalomon@google.comc7448ce2012-10-05 19:04:13 +00001319 bool prAA = paint.isAntiAlias() && !this->getRenderTarget()->isMultisampled();
bsalomon@google.com289533a2011-10-27 12:34:25 +00001320
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001321 // An Assumption here is that path renderer would use some form of tweaking
1322 // the src color (either the input alpha or in the frag shader) to implement
1323 // aa. If we have some future driver-mojo path AA that can do the right
1324 // thing WRT to the blend then we'll need some query on the PR.
1325 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001326#if GR_DEBUG
bsalomon@google.com979432b2011-11-05 21:38:22 +00001327 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001328#endif
bsalomon@google.com289533a2011-10-27 12:34:25 +00001329 prAA = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001330 }
bsalomon@google.com289533a2011-10-27 12:34:25 +00001331
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001332 GrPathRendererChain::DrawType type = prAA ? GrPathRendererChain::kColorAntiAlias_DrawType :
1333 GrPathRendererChain::kColor_DrawType;
1334
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001335 const SkPath* pathPtr = &path;
1336 SkPath tmpPath;
1337 SkStrokeRec strokeRec(stroke);
1338
1339 // Try a 1st time without stroking the path and without allowing the SW renderer
1340 GrPathRenderer* pr = this->getPathRenderer(*pathPtr, strokeRec, target, false, type);
1341
1342 if (NULL == pr) {
1343 if (!strokeRec.isHairlineStyle()) {
1344 // It didn't work the 1st time, so try again with the stroked path
1345 if (strokeRec.applyToPath(&tmpPath, *pathPtr)) {
1346 pathPtr = &tmpPath;
1347 strokeRec.setFillStyle();
1348 }
1349 }
1350 // This time, allow SW renderer
1351 pr = this->getPathRenderer(*pathPtr, strokeRec, target, true, type);
1352 }
1353
bsalomon@google.com30085192011-08-19 15:42:31 +00001354 if (NULL == pr) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001355#if GR_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001356 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001357#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001358 return;
1359 }
1360
robertphillips@google.comeb928ea2013-01-08 13:45:09 +00001361 pr->drawPath(*pathPtr, strokeRec, target, prAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001362}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001363
bsalomon@google.com27847de2011-02-22 20:59:41 +00001364////////////////////////////////////////////////////////////////////////////////
1365
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001366void GrContext::flush(int flagsBitfield) {
1367 if (kDiscard_FlushBit & flagsBitfield) {
1368 fDrawBuffer->reset();
1369 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001370 this->flushDrawBuffer();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001371 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001372 if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001373 fGpu->forceRenderTargetFlush();
1374 }
1375}
1376
bsalomon@google.com27847de2011-02-22 20:59:41 +00001377void GrContext::flushDrawBuffer() {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001378 if (NULL != fDrawBuffer && !fDrawBuffer->isFlushing()) {
1379 fDrawBuffer->flush();
junov@google.com53a55842011-06-08 22:55:10 +00001380 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001381}
1382
bsalomon@google.com9c680582013-02-06 18:17:50 +00001383bool GrContext::writeTexturePixels(GrTexture* texture,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001384 int left, int top, int width, int height,
1385 GrPixelConfig config, const void* buffer, size_t rowBytes,
1386 uint32_t flags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001387 SK_TRACE_EVENT0("GrContext::writeTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001388 ASSERT_OWNED_RESOURCE(texture);
1389
bsalomon@google.com9c680582013-02-06 18:17:50 +00001390 if ((kUnpremul_PixelOpsFlag & flags) || !fGpu->canWriteTexturePixels(texture, config)) {
1391 if (NULL != texture->asRenderTarget()) {
1392 return this->writeRenderTargetPixels(texture->asRenderTarget(),
1393 left, top, width, height,
1394 config, buffer, rowBytes, flags);
1395 } else {
1396 return false;
1397 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001398 }
bsalomon@google.com9c680582013-02-06 18:17:50 +00001399
bsalomon@google.com6f379512011-11-16 20:36:03 +00001400 if (!(kDontFlush_PixelOpsFlag & flags)) {
1401 this->flush();
1402 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001403
bsalomon@google.com9c680582013-02-06 18:17:50 +00001404 return fGpu->writeTexturePixels(texture, left, top, width, height,
1405 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001406}
1407
bsalomon@google.com0342a852012-08-20 19:22:38 +00001408bool GrContext::readTexturePixels(GrTexture* texture,
1409 int left, int top, int width, int height,
1410 GrPixelConfig config, void* buffer, size_t rowBytes,
1411 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001412 SK_TRACE_EVENT0("GrContext::readTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001413 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001414
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001415 // TODO: code read pixels for textures that aren't also rendertargets
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001416 GrRenderTarget* target = texture->asRenderTarget();
1417 if (NULL != target) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001418 return this->readRenderTargetPixels(target,
1419 left, top, width, height,
1420 config, buffer, rowBytes,
1421 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001422 } else {
1423 return false;
1424 }
1425}
1426
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001427#include "SkConfig8888.h"
1428
1429namespace {
1430/**
1431 * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel
1432 * formats are representable as Config8888 and so the function returns false
1433 * if the GrPixelConfig has no equivalent Config8888.
1434 */
1435bool grconfig_to_config8888(GrPixelConfig config,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001436 bool unpremul,
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001437 SkCanvas::Config8888* config8888) {
1438 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001439 case kRGBA_8888_GrPixelConfig:
1440 if (unpremul) {
1441 *config8888 = SkCanvas::kRGBA_Unpremul_Config8888;
1442 } else {
1443 *config8888 = SkCanvas::kRGBA_Premul_Config8888;
1444 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001445 return true;
bsalomon@google.com0342a852012-08-20 19:22:38 +00001446 case kBGRA_8888_GrPixelConfig:
1447 if (unpremul) {
1448 *config8888 = SkCanvas::kBGRA_Unpremul_Config8888;
1449 } else {
1450 *config8888 = SkCanvas::kBGRA_Premul_Config8888;
1451 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001452 return true;
1453 default:
1454 return false;
1455 }
1456}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001457
1458// It returns a configuration with where the byte position of the R & B components are swapped in
1459// relation to the input config. This should only be called with the result of
1460// grconfig_to_config8888 as it will fail for other configs.
1461SkCanvas::Config8888 swap_config8888_red_and_blue(SkCanvas::Config8888 config8888) {
1462 switch (config8888) {
1463 case SkCanvas::kBGRA_Premul_Config8888:
1464 return SkCanvas::kRGBA_Premul_Config8888;
1465 case SkCanvas::kBGRA_Unpremul_Config8888:
1466 return SkCanvas::kRGBA_Unpremul_Config8888;
1467 case SkCanvas::kRGBA_Premul_Config8888:
1468 return SkCanvas::kBGRA_Premul_Config8888;
1469 case SkCanvas::kRGBA_Unpremul_Config8888:
1470 return SkCanvas::kBGRA_Unpremul_Config8888;
1471 default:
1472 GrCrash("Unexpected input");
1473 return SkCanvas::kBGRA_Unpremul_Config8888;;
1474 }
1475}
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001476}
1477
bsalomon@google.com0342a852012-08-20 19:22:38 +00001478bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
1479 int left, int top, int width, int height,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001480 GrPixelConfig dstConfig, void* buffer, size_t rowBytes,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001481 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001482 SK_TRACE_EVENT0("GrContext::readRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001483 ASSERT_OWNED_RESOURCE(target);
1484
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001485 if (NULL == target) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001486 target = fDrawState->getRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001487 if (NULL == target) {
1488 return false;
1489 }
1490 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001491
bsalomon@google.com6f379512011-11-16 20:36:03 +00001492 if (!(kDontFlush_PixelOpsFlag & flags)) {
1493 this->flush();
1494 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001495
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001496 // Determine which conversions have to be applied: flipY, swapRAnd, and/or unpremul.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001497
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001498 // If fGpu->readPixels would incur a y-flip cost then we will read the pixels upside down. We'll
1499 // either do the flipY by drawing into a scratch with a matrix or on the cpu after the read.
1500 bool flipY = fGpu->readPixelsWillPayForYFlip(target, left, top,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001501 width, height, dstConfig,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001502 rowBytes);
bsalomon@google.com9c680582013-02-06 18:17:50 +00001503 // We ignore the preferred config if it is different than our config unless it is an R/B swap.
1504 // In that case we'll perform an R and B swap while drawing to a scratch texture of the swapped
1505 // config. Then we will call readPixels on the scratch with the swapped config. The swaps during
1506 // the draw cancels out the fact that we call readPixels with a config that is R/B swapped from
1507 // dstConfig.
1508 GrPixelConfig readConfig = dstConfig;
1509 bool swapRAndB = false;
1510 if (GrPixelConfigSwapRAndB(dstConfig) == fGpu->preferredReadPixelsConfig(dstConfig)) {
1511 readConfig = GrPixelConfigSwapRAndB(readConfig);
1512 swapRAndB = true;
1513 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001514
bsalomon@google.com0342a852012-08-20 19:22:38 +00001515 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001516
bsalomon@google.com9c680582013-02-06 18:17:50 +00001517 if (unpremul && !GrPixelConfigIs8888(dstConfig)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001518 // The unpremul flag is only allowed for these two configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001519 return false;
1520 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001521
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001522 // If the src is a texture and we would have to do conversions after read pixels, we instead
1523 // do the conversions by drawing the src to a scratch texture. If we handle any of the
1524 // conversions in the draw we set the corresponding bool to false so that we don't reapply it
1525 // on the read back pixels.
1526 GrTexture* src = target->asTexture();
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001527 GrAutoScratchTexture ast;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001528 if (NULL != src && (swapRAndB || unpremul || flipY)) {
1529 // Make the scratch a render target because we don't have a robust readTexturePixels as of
1530 // yet. It calls this function.
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001531 GrTextureDesc desc;
1532 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1533 desc.fWidth = width;
1534 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001535 desc.fConfig = readConfig;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001536 desc.fOrigin = kTopLeft_GrSurfaceOrigin;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001537
bsalomon@google.com9c680582013-02-06 18:17:50 +00001538 // 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 +00001539 // match the passed rect. However, if we see many different size rectangles we will trash
1540 // our texture cache and pay the cost of creating and destroying many textures. So, we only
1541 // request an exact match when the caller is reading an entire RT.
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001542 ScratchTexMatch match = kApprox_ScratchTexMatch;
1543 if (0 == left &&
1544 0 == top &&
1545 target->width() == width &&
1546 target->height() == height &&
1547 fGpu->fullReadPixelsIsFasterThanPartial()) {
1548 match = kExact_ScratchTexMatch;
1549 }
1550 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001551 GrTexture* texture = ast.texture();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001552 if (texture) {
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001553 // compute a matrix to perform the draw
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001554 SkMatrix textureMatrix;
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001555 textureMatrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001556 textureMatrix.postIDiv(src->width(), src->height());
1557
bsalomon@google.comadc65362013-01-28 14:26:09 +00001558 SkAutoTUnref<const GrEffectRef> effect;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001559 if (unpremul) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001560 effect.reset(this->createPMToUPMEffect(src, swapRAndB, textureMatrix));
1561 if (NULL != effect) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001562 unpremul = false; // we no longer need to do this on CPU after the read back.
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001563 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001564 }
1565 // If we failed to create a PM->UPM effect and have no other conversions to perform then
1566 // there is no longer any point to using the scratch.
bsalomon@google.comadc65362013-01-28 14:26:09 +00001567 if (NULL != effect || flipY || swapRAndB) {
1568 if (!effect) {
1569 effect.reset(GrConfigConversionEffect::Create(
1570 src,
1571 swapRAndB,
1572 GrConfigConversionEffect::kNone_PMConversion,
1573 textureMatrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001574 }
1575 swapRAndB = false; // we will handle the swap in the draw.
bsalomon@google.comc4364992011-11-07 15:54:49 +00001576
robertphillips@google.com13f181f2013-03-02 12:02:08 +00001577 // We protect the existing geometry here since it may not be
1578 // clear to the caller that a draw operation (i.e., drawSimpleRect)
1579 // can be invoked in this method
1580 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001581 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.comadc65362013-01-28 14:26:09 +00001582 GrAssert(effect);
bsalomon@google.com9e040ae2013-01-28 14:31:19 +00001583 drawState->setEffect(0, effect);
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001584
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001585 drawState->setRenderTarget(texture->asRenderTarget());
bsalomon@google.com81712882012-11-01 17:12:34 +00001586 GrRect rect = GrRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001587 fGpu->drawSimpleRect(rect, NULL);
1588 // we want to read back from the scratch's origin
1589 left = 0;
1590 top = 0;
1591 target = texture->asRenderTarget();
1592 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001593 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001594 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001595 if (!fGpu->readPixels(target,
1596 left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001597 readConfig, buffer, rowBytes)) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001598 return false;
1599 }
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001600 // Perform any conversions we weren't able to perform using a scratch texture.
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001601 if (unpremul || swapRAndB) {
bsalomon@google.comccaa0022012-09-25 19:55:07 +00001602 // These are initialized to suppress a warning
1603 SkCanvas::Config8888 srcC8888 = SkCanvas::kNative_Premul_Config8888;
1604 SkCanvas::Config8888 dstC8888 = SkCanvas::kNative_Premul_Config8888;
1605
bsalomon@google.com9c680582013-02-06 18:17:50 +00001606 SkDEBUGCODE(bool c8888IsValid =) grconfig_to_config8888(dstConfig, false, &srcC8888);
1607 grconfig_to_config8888(dstConfig, unpremul, &dstC8888);
bsalomon@google.comccaa0022012-09-25 19:55:07 +00001608
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001609 if (swapRAndB) {
1610 GrAssert(c8888IsValid); // we should only do r/b swap on 8888 configs
1611 srcC8888 = swap_config8888_red_and_blue(srcC8888);
1612 }
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +00001613 GrAssert(c8888IsValid);
1614 uint32_t* b32 = reinterpret_cast<uint32_t*>(buffer);
1615 SkConvertConfig8888Pixels(b32, rowBytes, dstC8888,
1616 b32, rowBytes, srcC8888,
1617 width, height);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001618 }
1619 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001620}
1621
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001622void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1623 GrAssert(target);
1624 ASSERT_OWNED_RESOURCE(target);
1625 // In the future we may track whether there are any pending draws to this
1626 // target. We don't today so we always perform a flush. We don't promise
1627 // this to our clients, though.
1628 this->flush();
1629 fGpu->resolveRenderTarget(target);
1630}
1631
scroggo@google.coma2a31922012-12-07 19:14:45 +00001632void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst, const SkIPoint* topLeft) {
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001633 if (NULL == src || NULL == dst) {
1634 return;
1635 }
1636 ASSERT_OWNED_RESOURCE(src);
1637
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001638 // Writes pending to the source texture are not tracked, so a flush
1639 // is required to ensure that the copy captures the most recent contents
bsalomon@google.comadc65362013-01-28 14:26:09 +00001640 // of the source texture. See similar behavior in
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001641 // GrContext::resolveRenderTarget.
1642 this->flush();
1643
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001644 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001645 GrDrawState* drawState = fGpu->drawState();
1646 drawState->setRenderTarget(dst);
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001647 SkMatrix sampleM;
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001648 sampleM.setIDiv(src->width(), src->height());
scroggo@google.coma2a31922012-12-07 19:14:45 +00001649 SkIRect srcRect = SkIRect::MakeWH(dst->width(), dst->height());
1650 if (NULL != topLeft) {
1651 srcRect.offset(*topLeft);
1652 }
1653 SkIRect srcBounds = SkIRect::MakeWH(src->width(), src->height());
1654 if (!srcRect.intersect(srcBounds)) {
1655 return;
1656 }
1657 sampleM.preTranslate(SkIntToScalar(srcRect.fLeft), SkIntToScalar(srcRect.fTop));
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +00001658 drawState->createTextureEffect(0, src, sampleM);
scroggo@google.coma2a31922012-12-07 19:14:45 +00001659 SkRect dstR = SkRect::MakeWH(SkIntToScalar(srcRect.width()), SkIntToScalar(srcRect.height()));
1660 fGpu->drawSimpleRect(dstR, NULL);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001661}
1662
bsalomon@google.com9c680582013-02-06 18:17:50 +00001663bool GrContext::writeRenderTargetPixels(GrRenderTarget* target,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001664 int left, int top, int width, int height,
bsalomon@google.com9c680582013-02-06 18:17:50 +00001665 GrPixelConfig srcConfig,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001666 const void* buffer,
1667 size_t rowBytes,
1668 uint32_t flags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001669 SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001670 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001671
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001672 if (NULL == target) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001673 target = fDrawState->getRenderTarget();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001674 if (NULL == target) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001675 return false;
bsalomon@google.com6f379512011-11-16 20:36:03 +00001676 }
1677 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001678
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001679 // TODO: when underlying api has a direct way to do this we should use it (e.g. glDrawPixels on
1680 // desktop GL).
1681
1682 // We will always call some form of writeTexturePixels and we will pass our flags on to it.
1683 // Thus, we don't perform a flush here since that call will do it (if the kNoFlush flag isn't
1684 // set.)
bsalomon@google.com27847de2011-02-22 20:59:41 +00001685
bsalomon@google.com0342a852012-08-20 19:22:38 +00001686 // If the RT is also a texture and we don't have to premultiply then take the texture path.
1687 // We expect to be at least as fast or faster since it doesn't use an intermediate texture as
1688 // we do below.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001689
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001690#if !GR_MAC_BUILD
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001691 // At least some drivers on the Mac get confused when glTexImage2D is called on a texture
1692 // attached to an FBO. The FBO still sees the old image. TODO: determine what OS versions and/or
1693 // HW is affected.
bsalomon@google.com9c680582013-02-06 18:17:50 +00001694 if (NULL != target->asTexture() && !(kUnpremul_PixelOpsFlag & flags) &&
1695 fGpu->canWriteTexturePixels(target->asTexture(), srcConfig)) {
1696 return this->writeTexturePixels(target->asTexture(),
1697 left, top, width, height,
1698 srcConfig, buffer, rowBytes, flags);
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001699 }
1700#endif
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001701
bsalomon@google.com9c680582013-02-06 18:17:50 +00001702 // We ignore the preferred config unless it is a R/B swap of the src config. In that case
1703 // we will upload the original src data to a scratch texture but we will spoof it as the swapped
1704 // config. This scratch will then have R and B swapped. We correct for this by swapping again
1705 // when drawing the scratch to the dst using a conversion effect.
1706 bool swapRAndB = false;
1707 GrPixelConfig writeConfig = srcConfig;
1708 if (fGpu->preferredWritePixelsConfig(srcConfig) == GrPixelConfigSwapRAndB(srcConfig)) {
1709 writeConfig = GrPixelConfigSwapRAndB(srcConfig);
1710 swapRAndB = true;
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001711 }
1712
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001713 GrTextureDesc desc;
1714 desc.fWidth = width;
1715 desc.fHeight = height;
bsalomon@google.com9c680582013-02-06 18:17:50 +00001716 desc.fConfig = writeConfig;
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001717 GrAutoScratchTexture ast(this, desc);
1718 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001719 if (NULL == texture) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001720 return false;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001721 }
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001722
bsalomon@google.comadc65362013-01-28 14:26:09 +00001723 SkAutoTUnref<const GrEffectRef> effect;
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001724 SkMatrix textureMatrix;
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +00001725 textureMatrix.setIDiv(texture->width(), texture->height());
1726
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001727 // allocate a tmp buffer and sw convert the pixels to premul
1728 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
1729
1730 if (kUnpremul_PixelOpsFlag & flags) {
bsalomon@google.com9c680582013-02-06 18:17:50 +00001731 if (!GrPixelConfigIs8888(srcConfig)) {
1732 return false;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001733 }
bsalomon@google.comadc65362013-01-28 14:26:09 +00001734 effect.reset(this->createUPMToPMEffect(texture, swapRAndB, textureMatrix));
bsalomon@google.com9c680582013-02-06 18:17:50 +00001735 // 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 +00001736 if (NULL == effect) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001737 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1738 GR_DEBUGCODE(bool success = )
bsalomon@google.com9c680582013-02-06 18:17:50 +00001739 grconfig_to_config8888(srcConfig, true, &srcConfig8888);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001740 GrAssert(success);
1741 GR_DEBUGCODE(success = )
bsalomon@google.com9c680582013-02-06 18:17:50 +00001742 grconfig_to_config8888(srcConfig, false, &dstConfig8888);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001743 GrAssert(success);
1744 const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer);
1745 tmpPixels.reset(width * height);
1746 SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888,
1747 src, rowBytes, srcConfig8888,
1748 width, height);
1749 buffer = tmpPixels.get();
1750 rowBytes = 4 * width;
1751 }
1752 }
bsalomon@google.comadc65362013-01-28 14:26:09 +00001753 if (NULL == effect) {
1754 effect.reset(GrConfigConversionEffect::Create(texture,
1755 swapRAndB,
1756 GrConfigConversionEffect::kNone_PMConversion,
1757 textureMatrix));
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001758 }
1759
bsalomon@google.com9c680582013-02-06 18:17:50 +00001760 if (!this->writeTexturePixels(texture,
1761 0, 0, width, height,
1762 writeConfig, buffer, rowBytes,
1763 flags & ~kUnpremul_PixelOpsFlag)) {
1764 return false;
1765 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001766
robertphillips@google.com13f181f2013-03-02 12:02:08 +00001767 // writeRenderTargetPixels can be called in the midst of drawing another
skia.committer@gmail.com0c23faf2013-03-03 07:16:29 +00001768 // object (e.g., when uploading a SW path rendering to the gpu while
robertphillips@google.com13f181f2013-03-02 12:02:08 +00001769 // drawing a rect) so preserve the current geometry.
1770 GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001771 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.comadc65362013-01-28 14:26:09 +00001772 GrAssert(effect);
bsalomon@google.com9e040ae2013-01-28 14:31:19 +00001773 drawState->setEffect(0, effect);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001774
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001775 SkMatrix matrix;
bsalomon@google.com81712882012-11-01 17:12:34 +00001776 matrix.setTranslate(SkIntToScalar(left), SkIntToScalar(top));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001777 drawState->setViewMatrix(matrix);
1778 drawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001779
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001780 fGpu->drawSimpleRect(GrRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)), NULL);
bsalomon@google.com9c680582013-02-06 18:17:50 +00001781 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001782}
1783////////////////////////////////////////////////////////////////////////////////
1784
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001785GrDrawTarget* GrContext::prepareToDraw(const GrPaint* paint, BufferedDraw buffered) {
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001786 if (kNo_BufferedDraw == buffered && kYes_BufferedDraw == fLastDrawWasBuffered) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001787 this->flushDrawBuffer();
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001788 fLastDrawWasBuffered = kNo_BufferedDraw;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001789 }
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001790 if (NULL != paint) {
bsalomon@google.comaf84e742012-10-05 13:23:24 +00001791 GrAssert(fDrawState->stagesDisabled());
1792 fDrawState->setFromPaint(*paint);
1793#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
1794 if ((paint->hasMask() || 0xff != paint->fCoverage) &&
1795 !fGpu->canApplyCoverage()) {
1796 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1797 }
1798#endif
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001799 }
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001800 if (kYes_BufferedDraw == buffered) {
1801 fDrawBuffer->setClip(fGpu->getClip());
1802 fLastDrawWasBuffered = kYes_BufferedDraw;
1803 return fDrawBuffer;
1804 } else {
1805 GrAssert(kNo_BufferedDraw == buffered);
1806 return fGpu;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001807 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001808}
1809
robertphillips@google.com72176b22012-05-23 13:19:12 +00001810/*
1811 * This method finds a path renderer that can draw the specified path on
1812 * the provided target.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001813 * Due to its expense, the software path renderer has split out so it can
robertphillips@google.com72176b22012-05-23 13:19:12 +00001814 * can be individually allowed/disallowed via the "allowSW" boolean.
1815 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001816GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001817 const SkStrokeRec& stroke,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001818 const GrDrawTarget* target,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001819 bool allowSW,
1820 GrPathRendererChain::DrawType drawType,
1821 GrPathRendererChain::StencilSupport* stencilSupport) {
1822
bsalomon@google.com30085192011-08-19 15:42:31 +00001823 if (NULL == fPathRendererChain) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001824 fPathRendererChain = SkNEW_ARGS(GrPathRendererChain, (this));
bsalomon@google.com30085192011-08-19 15:42:31 +00001825 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001826
sugoi@google.com12b4e272012-12-06 20:13:11 +00001827 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path,
1828 stroke,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001829 target,
bsalomon@google.com45a15f52012-12-10 19:10:17 +00001830 drawType,
1831 stencilSupport);
robertphillips@google.com72176b22012-05-23 13:19:12 +00001832
1833 if (NULL == pr && allowSW) {
1834 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001835 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001836 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001837 pr = fSoftwarePathRenderer;
1838 }
1839
1840 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001841}
1842
bsalomon@google.com27847de2011-02-22 20:59:41 +00001843////////////////////////////////////////////////////////////////////////////////
1844
bsalomon@google.com27847de2011-02-22 20:59:41 +00001845void GrContext::setRenderTarget(GrRenderTarget* target) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001846 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001847 fDrawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001848}
1849
1850GrRenderTarget* GrContext::getRenderTarget() {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001851 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001852}
1853
1854const GrRenderTarget* GrContext::getRenderTarget() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001855 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001856}
1857
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001858bool GrContext::isConfigRenderable(GrPixelConfig config) const {
1859 return fGpu->isConfigRenderable(config);
1860}
1861
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001862const SkMatrix& GrContext::getMatrix() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001863 return fDrawState->getViewMatrix();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001864}
1865
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001866void GrContext::setMatrix(const SkMatrix& m) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001867 fDrawState->setViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001868}
1869
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +00001870void GrContext::setIdentityMatrix() {
1871 fDrawState->viewMatrix()->reset();
1872}
1873
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001874void GrContext::concatMatrix(const SkMatrix& m) const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001875 fDrawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001876}
1877
1878static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
1879 intptr_t mask = 1 << shift;
1880 if (pred) {
1881 bits |= mask;
1882 } else {
1883 bits &= ~mask;
1884 }
1885 return bits;
1886}
1887
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001888void GrContext::setupDrawBuffer() {
1889
1890 GrAssert(NULL == fDrawBuffer);
1891 GrAssert(NULL == fDrawBufferVBAllocPool);
1892 GrAssert(NULL == fDrawBufferIBAllocPool);
1893
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001894 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001895 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001896 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001897 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001898 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001899 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001900 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001901 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001902
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001903 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001904 fDrawBufferVBAllocPool,
1905 fDrawBufferIBAllocPool));
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001906
bsalomon@google.com6e4e6502013-02-25 20:12:45 +00001907 fDrawBuffer->setDrawState(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001908}
1909
bsalomon@google.com27847de2011-02-22 20:59:41 +00001910GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) {
bsalomon@google.com82b0ec62013-02-07 21:02:36 +00001911 return this->prepareToDraw(&paint, BUFFERED_DRAW);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001912}
1913
1914const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1915 return fGpu->getQuadIndexBuffer();
1916}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001917
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001918namespace {
1919void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
1920 GrConfigConversionEffect::PMConversion pmToUPM;
1921 GrConfigConversionEffect::PMConversion upmToPM;
1922 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
1923 *pmToUPMValue = pmToUPM;
1924 *upmToPMValue = upmToPM;
1925}
1926}
1927
bsalomon@google.comadc65362013-01-28 14:26:09 +00001928const GrEffectRef* GrContext::createPMToUPMEffect(GrTexture* texture,
1929 bool swapRAndB,
1930 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001931 if (!fDidTestPMConversions) {
1932 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001933 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001934 }
1935 GrConfigConversionEffect::PMConversion pmToUPM =
1936 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
1937 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001938 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001939 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001940 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001941 }
1942}
1943
bsalomon@google.comadc65362013-01-28 14:26:09 +00001944const GrEffectRef* GrContext::createUPMToPMEffect(GrTexture* texture,
1945 bool swapRAndB,
1946 const SkMatrix& matrix) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001947 if (!fDidTestPMConversions) {
1948 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001949 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001950 }
1951 GrConfigConversionEffect::PMConversion upmToPM =
1952 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
1953 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001954 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001955 } else {
bsalomon@google.comadc65362013-01-28 14:26:09 +00001956 return NULL;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001957 }
1958}
1959
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001960GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture,
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001961 bool canClobberSrc,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001962 const SkRect& rect,
1963 float sigmaX, float sigmaY) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001964 ASSERT_OWNED_RESOURCE(srcTexture);
robertphillips@google.comccb39502012-10-01 18:25:13 +00001965
1966 AutoRenderTarget art(this);
1967
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001968 AutoMatrix am;
1969 am.setIdentity(this);
1970
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001971 SkIRect clearRect;
bsalomon@google.comb505a122012-05-31 18:40:36 +00001972 int scaleFactorX, radiusX;
1973 int scaleFactorY, radiusY;
1974 sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX);
1975 sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY);
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001976
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001977 SkRect srcRect(rect);
1978 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
1979 srcRect.roundOut();
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001980 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001981 static_cast<float>(scaleFactorY));
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001982
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001983 AutoClip acs(this, srcRect);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001984
bsalomon@google.com0342a852012-08-20 19:22:38 +00001985 GrAssert(kBGRA_8888_GrPixelConfig == srcTexture->config() ||
1986 kRGBA_8888_GrPixelConfig == srcTexture->config() ||
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001987 kAlpha_8_GrPixelConfig == srcTexture->config());
1988
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001989 GrTextureDesc desc;
1990 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1991 desc.fWidth = SkScalarFloorToInt(srcRect.width());
1992 desc.fHeight = SkScalarFloorToInt(srcRect.height());
1993 desc.fConfig = srcTexture->config();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001994
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001995 GrAutoScratchTexture temp1, temp2;
1996 GrTexture* dstTexture = temp1.set(this, desc);
1997 GrTexture* tempTexture = canClobberSrc ? srcTexture : temp2.set(this, desc);
robertphillips@google.com7d126752012-10-19 12:56:26 +00001998 if (NULL == dstTexture || NULL == tempTexture) {
1999 return NULL;
2000 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002001
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002002 GrPaint paint;
2003 paint.reset();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002004
2005 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +00002006 SkMatrix matrix;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +00002007 matrix.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002008 this->setRenderTarget(dstTexture->asRenderTarget());
2009 SkRect dstRect(srcRect);
2010 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +00002011 i < scaleFactorY ? 0.5f : 1.0f);
2012
bsalomon@google.com68b58c92013-01-17 16:50:08 +00002013 paint.colorStage(0)->setEffect(GrSimpleTextureEffect::Create(srcTexture,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +00002014 matrix,
2015 true))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002016 this->drawRectToRect(paint, dstRect, srcRect);
2017 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00002018 srcTexture = dstTexture;
2019 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002020 }
2021
robertphillips@google.com7a396332012-05-10 15:11:27 +00002022 SkIRect srcIRect;
2023 srcRect.roundOut(&srcIRect);
2024
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002025 if (sigmaX > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002026 if (scaleFactorX > 1) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00002027 // Clear out a radius to the right of the srcRect to prevent the
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002028 // X convolution from reading garbage.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002029 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
bsalomon@google.comb505a122012-05-31 18:40:36 +00002030 radiusX, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002031 this->clear(&clearRect, 0x0);
2032 }
2033
2034 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com82b0ec62013-02-07 21:02:36 +00002035 GrDrawTarget* target = this->prepareToDraw(NULL, BUFFERED_DRAW);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00002036 convolve_gaussian(target, srcTexture, srcRect, sigmaX, radiusX,
bsalomon@google.comb505a122012-05-31 18:40:36 +00002037 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00002038 srcTexture = dstTexture;
2039 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002040 }
2041
2042 if (sigmaY > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002043 if (scaleFactorY > 1 || sigmaX > 0.0f) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00002044 // Clear out a radius below the srcRect to prevent the Y
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002045 // convolution from reading garbage.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002046 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
bsalomon@google.comb505a122012-05-31 18:40:36 +00002047 srcIRect.width(), radiusY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002048 this->clear(&clearRect, 0x0);
2049 }
2050
2051 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com82b0ec62013-02-07 21:02:36 +00002052 GrDrawTarget* target = this->prepareToDraw(NULL, BUFFERED_DRAW);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00002053 convolve_gaussian(target, srcTexture, srcRect, sigmaY, radiusY,
bsalomon@google.comb505a122012-05-31 18:40:36 +00002054 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00002055 srcTexture = dstTexture;
2056 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002057 }
2058
2059 if (scaleFactorX > 1 || scaleFactorY > 1) {
2060 // Clear one pixel to the right and below, to accommodate bilinear
2061 // upsampling.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002062 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
robertphillips@google.com7a396332012-05-10 15:11:27 +00002063 srcIRect.width() + 1, 1);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002064 this->clear(&clearRect, 0x0);
rmistry@google.comfbfcd562012-08-23 18:09:54 +00002065 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
robertphillips@google.com7a396332012-05-10 15:11:27 +00002066 1, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002067 this->clear(&clearRect, 0x0);
bsalomon@google.comb9086a02012-11-01 18:02:54 +00002068 SkMatrix matrix;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002069 // FIXME: This should be mitchell, not bilinear.
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +00002070 matrix.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002071 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com68b58c92013-01-17 16:50:08 +00002072 paint.colorStage(0)->setEffect(GrSimpleTextureEffect::Create(srcTexture,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +00002073 matrix,
2074 true))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002075 SkRect dstRect(srcRect);
robertphillips@google.com7a396332012-05-10 15:11:27 +00002076 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002077 this->drawRectToRect(paint, dstRect, srcRect);
2078 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00002079 srcTexture = dstTexture;
2080 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00002081 }
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00002082 if (srcTexture == temp1.texture()) {
2083 return temp1.detach();
2084 } else if (srcTexture == temp2.texture()) {
2085 return temp2.detach();
2086 } else {
2087 srcTexture->ref();
2088 return srcTexture;
2089 }
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00002090}
bsalomon@google.com1e266f82011-12-12 16:11:33 +00002091
bsalomon@google.comc4364992011-11-07 15:54:49 +00002092///////////////////////////////////////////////////////////////////////////////
robertphillips@google.com59552022012-08-31 13:07:37 +00002093#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00002094void GrContext::printCacheStats() const {
2095 fTextureCache->printStats();
2096}
2097#endif