blob: d1fdb1803bdf4a1ae0ac07b0b0cf857ce2065e09 [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.comb505a122012-05-31 18:40:36 +000014
tomhudson@google.com278cbb42011-06-30 19:37:01 +000015#include "GrBufferAllocPool.h"
bsalomon@google.com05ef5102011-05-02 21:14:59 +000016#include "GrGpu.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000017#include "GrIndexBuffer.h"
18#include "GrInOrderDrawBuffer.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000019#include "GrPathRenderer.h"
tomhudson@google.comd22b6e42011-06-24 15:53:40 +000020#include "GrPathUtils.h"
bsalomon@google.com50398bf2011-07-26 20:45:30 +000021#include "GrResourceCache.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000022#include "GrSoftwarePathRenderer.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000023#include "GrStencilBuffer.h"
tomhudson@google.com278cbb42011-06-30 19:37:01 +000024#include "GrTextStrike.h"
bsalomon@google.com8c2fe992011-09-13 15:27:18 +000025#include "SkTLazy.h"
bsalomon@google.comc0af3172012-06-15 14:10:09 +000026#include "SkTLS.h"
tomhudson@google.com0c8d93a2011-07-01 17:08:26 +000027#include "SkTrace.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000028
reed@google.comfa35e3d2012-06-26 20:16:17 +000029SK_DEFINE_INST_COUNT(GrContext)
30SK_DEFINE_INST_COUNT(GrDrawState)
31
bsalomon@google.com1d4edd32012-08-16 18:36:06 +000032// It can be useful to set this to kNo_BufferedDraw to test whether a bug is caused by using the
33// InOrderDrawBuffer, to compare performance of using/not using InOrderDrawBuffer, or to make
34// debugging easier.
35#define DEFAULT_BUFFERING (GR_DISABLE_DRAW_BUFFERING ? kNo_BufferedDraw : kYes_BufferedDraw)
bsalomon@google.com27847de2011-02-22 20:59:41 +000036
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +000037#define MAX_BLUR_SIGMA 4.0f
38
bsalomon@google.comd46e2422011-09-23 17:40:07 +000039// When we're using coverage AA but the blend is incompatible (given gpu
40// limitations) should we disable AA or draw wrong?
bsalomon@google.com950d7a82011-09-28 15:05:33 +000041#define DISABLE_COVERAGE_AA_FOR_BLEND 1
bsalomon@google.comd46e2422011-09-23 17:40:07 +000042
reed@google.com4b2d3f32012-05-15 18:05:50 +000043#if GR_DEBUG
44 // change this to a 1 to see notifications when partial coverage fails
45 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
46#else
47 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
48#endif
49
bsalomon@google.com07fc0d12012-06-22 15:15:59 +000050static const size_t MAX_TEXTURE_CACHE_COUNT = 256;
51static const size_t MAX_TEXTURE_CACHE_BYTES = 16 * 1024 * 1024;
bsalomon@google.com27847de2011-02-22 20:59:41 +000052
bsalomon@google.com60361492012-03-15 17:47:06 +000053static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
bsalomon@google.com27847de2011-02-22 20:59:41 +000054static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
55
bsalomon@google.com1d4edd32012-08-16 18:36:06 +000056static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = 1 << 11;
57static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = 4;
bsalomon@google.com27847de2011-02-22 20:59:41 +000058
bsalomon@google.combc4b6542011-11-19 13:56:11 +000059#define ASSERT_OWNED_RESOURCE(R) GrAssert(!(R) || (R)->getContext() == this)
60
bsalomon@google.com05ef5102011-05-02 21:14:59 +000061GrContext* GrContext::Create(GrEngine engine,
62 GrPlatform3DContext context3D) {
bsalomon@google.com27847de2011-02-22 20:59:41 +000063 GrContext* ctx = NULL;
64 GrGpu* fGpu = GrGpu::Create(engine, context3D);
65 if (NULL != fGpu) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000066 ctx = SkNEW_ARGS(GrContext, (fGpu));
bsalomon@google.com27847de2011-02-22 20:59:41 +000067 fGpu->unref();
68 }
69 return ctx;
70}
71
bsalomon@google.comc0af3172012-06-15 14:10:09 +000072namespace {
73void* CreateThreadInstanceCount() {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000074 return SkNEW_ARGS(int, (0));
bsalomon@google.comc0af3172012-06-15 14:10:09 +000075}
76void DeleteThreadInstanceCount(void* v) {
77 delete reinterpret_cast<int*>(v);
78}
79#define THREAD_INSTANCE_COUNT \
80 (*reinterpret_cast<int*>(SkTLS::Get(CreateThreadInstanceCount, \
81 DeleteThreadInstanceCount)))
82
83}
84
85int GrContext::GetThreadInstanceCount() {
86 return THREAD_INSTANCE_COUNT;
87}
88
bsalomon@google.com27847de2011-02-22 20:59:41 +000089GrContext::~GrContext() {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000090 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +000091
92 // Since the gpu can hold scratch textures, give it a chance to let go
93 // of them before freeing the texture cache
94 fGpu->purgeResources();
95
bsalomon@google.com27847de2011-02-22 20:59:41 +000096 delete fTextureCache;
97 delete fFontCache;
98 delete fDrawBuffer;
99 delete fDrawBufferVBAllocPool;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000100 delete fDrawBufferIBAllocPool;
bsalomon@google.com30085192011-08-19 15:42:31 +0000101
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000102 fAARectRenderer->unref();
103
bsalomon@google.com205d4602011-04-25 12:43:45 +0000104 fGpu->unref();
bsalomon@google.com30085192011-08-19 15:42:31 +0000105 GrSafeUnref(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000106 GrSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000107 fDrawState->unref();
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000108
109 --THREAD_INSTANCE_COUNT;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000110}
111
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000112void GrContext::contextLost() {
junov@google.com53a55842011-06-08 22:55:10 +0000113 contextDestroyed();
114 this->setupDrawBuffer();
115}
116
117void GrContext::contextDestroyed() {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000118 // abandon first to so destructors
119 // don't try to free the resources in the API.
120 fGpu->abandonResources();
121
bsalomon@google.com30085192011-08-19 15:42:31 +0000122 // a path renderer may be holding onto resources that
123 // are now unusable
124 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000125 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000126
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000127 delete fDrawBuffer;
128 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000129
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000130 delete fDrawBufferVBAllocPool;
131 fDrawBufferVBAllocPool = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000132
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000133 delete fDrawBufferIBAllocPool;
134 fDrawBufferIBAllocPool = NULL;
135
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000136 fAARectRenderer->reset();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000137
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000138 fTextureCache->removeAll();
139 fFontCache->freeAll();
140 fGpu->markContextDirty();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000141}
142
143void GrContext::resetContext() {
144 fGpu->markContextDirty();
145}
146
147void GrContext::freeGpuResources() {
148 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000149
robertphillips@google.comff175842012-05-14 19:31:39 +0000150 fGpu->purgeResources();
151
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000152 fAARectRenderer->reset();
153
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000154 fTextureCache->removeAll();
155 fFontCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000156 // a path renderer may be holding onto resources
157 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000158 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000159}
160
twiz@google.com05e70242012-01-27 19:12:00 +0000161size_t GrContext::getGpuTextureCacheBytes() const {
162 return fTextureCache->getCachedResourceBytes();
163}
164
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000165////////////////////////////////////////////////////////////////////////////////
166
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000167namespace {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000168
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000169void scale_rect(SkRect* rect, float xScale, float yScale) {
robertphillips@google.com5af56062012-04-27 15:39:52 +0000170 rect->fLeft = SkScalarMul(rect->fLeft, SkFloatToScalar(xScale));
171 rect->fTop = SkScalarMul(rect->fTop, SkFloatToScalar(yScale));
172 rect->fRight = SkScalarMul(rect->fRight, SkFloatToScalar(xScale));
173 rect->fBottom = SkScalarMul(rect->fBottom, SkFloatToScalar(yScale));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000174}
175
bsalomon@google.comb505a122012-05-31 18:40:36 +0000176float adjust_sigma(float sigma, int *scaleFactor, int *radius) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000177 *scaleFactor = 1;
178 while (sigma > MAX_BLUR_SIGMA) {
179 *scaleFactor *= 2;
180 sigma *= 0.5f;
181 }
bsalomon@google.comb505a122012-05-31 18:40:36 +0000182 *radius = static_cast<int>(ceilf(sigma * 3.0f));
183 GrAssert(*radius <= GrConvolutionEffect::kMaxKernelRadius);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000184 return sigma;
185}
186
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000187void convolve_gaussian(GrDrawTarget* target,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000188 GrTexture* texture,
189 const SkRect& rect,
190 float sigma,
191 int radius,
192 Gr1DKernelEffect::Direction direction) {
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000193 GrRenderTarget* rt = target->drawState()->getRenderTarget();
194 GrDrawTarget::AutoStateRestore asr(target, GrDrawTarget::kReset_ASRInit);
195 GrDrawState* drawState = target->drawState();
196 drawState->setRenderTarget(rt);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000197 GrMatrix sampleM;
198 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000199 drawState->sampler(0)->reset(sampleM);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000200 SkAutoTUnref<GrConvolutionEffect> conv(SkNEW_ARGS(GrConvolutionEffect,
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000201 (texture, direction, radius,
202 sigma)));
bsalomon@google.comb505a122012-05-31 18:40:36 +0000203 drawState->sampler(0)->setCustomStage(conv);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000204 target->drawSimpleRect(rect, NULL);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000205}
206
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000207}
208
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000209GrTexture* GrContext::findAndLockTexture(const GrTextureDesc& desc,
210 const GrCacheData& cacheData,
211 const GrTextureParams* params) {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000212 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000213 GrResource* resource = fTextureCache->findAndLock(resourceKey,
214 GrResourceCache::kNested_LockType);
215 return static_cast<GrTexture*>(resource);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000216}
217
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000218bool GrContext::isTextureInCache(const GrTextureDesc& desc,
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000219 const GrCacheData& cacheData,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000220 const GrTextureParams* params) const {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000221 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000222 return fTextureCache->hasKey(resourceKey);
223}
224
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000225void GrContext::addAndLockStencilBuffer(GrStencilBuffer* sb) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000226 ASSERT_OWNED_RESOURCE(sb);
robertphillips@google.com46a86002012-08-08 10:42:44 +0000227
228 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(sb->width(),
229 sb->height(),
230 sb->numSamples());
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000231 fTextureCache->createAndLock(resourceKey, sb);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000232}
233
234GrStencilBuffer* GrContext::findStencilBuffer(int width, int height,
235 int sampleCnt) {
robertphillips@google.com46a86002012-08-08 10:42:44 +0000236 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(width,
237 height,
238 sampleCnt);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000239 GrResource* resource = fTextureCache->findAndLock(resourceKey,
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000240 GrResourceCache::kSingle_LockType);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000241 return static_cast<GrStencilBuffer*>(resource);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000242}
243
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000244void GrContext::unlockStencilBuffer(GrStencilBuffer* sb) {
245 ASSERT_OWNED_RESOURCE(sb);
246 GrAssert(NULL != sb->getCacheEntry());
247
248 fTextureCache->unlock(sb->getCacheEntry());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000249}
250
251static void stretchImage(void* dst,
252 int dstW,
253 int dstH,
254 void* src,
255 int srcW,
256 int srcH,
257 int bpp) {
258 GrFixed dx = (srcW << 16) / dstW;
259 GrFixed dy = (srcH << 16) / dstH;
260
261 GrFixed y = dy >> 1;
262
263 int dstXLimit = dstW*bpp;
264 for (int j = 0; j < dstH; ++j) {
265 GrFixed x = dx >> 1;
266 void* srcRow = (uint8_t*)src + (y>>16)*srcW*bpp;
267 void* dstRow = (uint8_t*)dst + j*dstW*bpp;
268 for (int i = 0; i < dstXLimit; i += bpp) {
269 memcpy((uint8_t*) dstRow + i,
270 (uint8_t*) srcRow + (x>>16)*bpp,
271 bpp);
272 x += dx;
273 }
274 y += dy;
275 }
276}
277
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000278// The desired texture is NPOT and tiled but that isn't supported by
robertphillips@google.com3319f332012-08-13 18:00:36 +0000279// the current hardware. Resize the texture to be a POT
280GrTexture* GrContext::createResizedTexture(const GrTextureDesc& desc,
281 const GrCacheData& cacheData,
282 void* srcData,
283 size_t rowBytes,
284 bool needsFiltering) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000285 GrTexture* clampedTexture = this->findAndLockTexture(desc, cacheData, NULL);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000286
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000287 if (NULL == clampedTexture) {
288 clampedTexture = this->createAndLockTexture(NULL, desc, cacheData, srcData, rowBytes);
289 GrAssert(NULL != clampedTexture);
290 if (NULL == clampedTexture) {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000291 return NULL;
292 }
293 }
294 GrTextureDesc rtDesc = desc;
295 rtDesc.fFlags = rtDesc.fFlags |
296 kRenderTarget_GrTextureFlagBit |
297 kNoStencil_GrTextureFlagBit;
298 rtDesc.fWidth = GrNextPow2(GrMax(desc.fWidth, 64));
299 rtDesc.fHeight = GrNextPow2(GrMax(desc.fHeight, 64));
300
301 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
302
303 if (NULL != texture) {
304 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
305 GrDrawState* drawState = fGpu->drawState();
306 drawState->setRenderTarget(texture->asRenderTarget());
307
308 // if filtering is not desired then we want to ensure all
309 // texels in the resampled image are copies of texels from
310 // the original.
311 drawState->sampler(0)->reset(SkShader::kClamp_TileMode,
312 needsFiltering);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000313 drawState->createTextureEffect(0, clampedTexture);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000314
315 static const GrVertexLayout layout =
316 GrDrawTarget::StageTexCoordVertexLayoutBit(0,0);
317 GrDrawTarget::AutoReleaseGeometry arg(fGpu, layout, 4, 0);
318
319 if (arg.succeeded()) {
320 GrPoint* verts = (GrPoint*) arg.vertices();
321 verts[0].setIRectFan(0, 0,
322 texture->width(),
323 texture->height(),
324 2*sizeof(GrPoint));
325 verts[1].setIRectFan(0, 0, 1, 1, 2*sizeof(GrPoint));
326 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType,
327 0, 4);
328 }
329 texture->releaseRenderTarget();
330 } else {
331 // TODO: Our CPU stretch doesn't filter. But we create separate
332 // stretched textures when the sampler state is either filtered or
333 // not. Either implement filtered stretch blit on CPU or just create
334 // one when FBO case fails.
335
336 rtDesc.fFlags = kNone_GrTextureFlags;
337 // no longer need to clamp at min RT size.
338 rtDesc.fWidth = GrNextPow2(desc.fWidth);
339 rtDesc.fHeight = GrNextPow2(desc.fHeight);
340 int bpp = GrBytesPerPixel(desc.fConfig);
341 SkAutoSMalloc<128*128*4> stretchedPixels(bpp *
342 rtDesc.fWidth *
343 rtDesc.fHeight);
344 stretchImage(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
345 srcData, desc.fWidth, desc.fHeight, bpp);
346
347 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
348
349 GrTexture* texture = fGpu->createTexture(rtDesc,
350 stretchedPixels.get(),
351 stretchedRowBytes);
352 GrAssert(NULL != texture);
353 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000354 this->unlockTexture(clampedTexture);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000355
356 return texture;
357}
358
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000359GrTexture* GrContext::createAndLockTexture(
bsalomon@google.comb8670992012-07-25 21:27:09 +0000360 const GrTextureParams* params,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000361 const GrTextureDesc& desc,
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000362 const GrCacheData& cacheData,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000363 void* srcData,
364 size_t rowBytes) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000365 SK_TRACE_EVENT0("GrContext::createAndLockTexture");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000366
367#if GR_DUMP_TEXTURE_UPLOAD
368 GrPrintf("GrContext::createAndLockTexture [%d %d]\n", desc.fWidth, desc.fHeight);
369#endif
370
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000371 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000372
robertphillips@google.com3319f332012-08-13 18:00:36 +0000373 GrTexture* texture = NULL;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000374 if (GrTexture::NeedsResizing(resourceKey)) {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000375 texture = this->createResizedTexture(desc, cacheData,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000376 srcData, rowBytes,
robertphillips@google.com3319f332012-08-13 18:00:36 +0000377 GrTexture::NeedsFiltering(resourceKey));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000378 } else {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000379 texture = fGpu->createTexture(desc, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000380 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000381
382 if (NULL != texture) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000383 fTextureCache->createAndLock(resourceKey, texture);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000384 }
385
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000386 return texture;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000387}
388
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000389GrTexture* GrContext::lockScratchTexture(const GrTextureDesc& inDesc,
390 ScratchTexMatch match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000391 GrTextureDesc desc = inDesc;
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000392 GrCacheData cacheData(GrCacheData::kScratch_CacheID);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000393
rileya@google.com2afb8ec2012-08-23 14:08:57 +0000394 GrAssert((desc.fFlags & kRenderTarget_GrTextureFlagBit) ||
395 !(desc.fFlags & kNoStencil_GrTextureFlagBit));
396
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000397 if (kExact_ScratchTexMatch != match) {
398 // bin by pow2 with a reasonable min
399 static const int MIN_SIZE = 256;
400 desc.fWidth = GrMax(MIN_SIZE, GrNextPow2(desc.fWidth));
401 desc.fHeight = GrMax(MIN_SIZE, GrNextPow2(desc.fHeight));
402 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000403
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000404 GrResource* resource = NULL;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000405 int origWidth = desc.fWidth;
406 int origHeight = desc.fHeight;
407 bool doubledW = false;
408 bool doubledH = false;
409
410 do {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000411 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL, desc, cacheData, true);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000412 resource = fTextureCache->findAndLock(key,
413 GrResourceCache::kNested_LockType);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000414 // if we miss, relax the fit of the flags...
415 // then try doubling width... then height.
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000416 if (NULL != resource || kExact_ScratchTexMatch == match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000417 break;
418 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000419 // We no longer try to reuse textures that were previously used as render targets in
rileya@google.com2afb8ec2012-08-23 14:08:57 +0000420 // situations where no RT is needed; doing otherwise can confuse the video driver and
421 // cause significant performance problems in some cases.
422 if (desc.fFlags & kNoStencil_GrTextureFlagBit) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000423 desc.fFlags = desc.fFlags & ~kNoStencil_GrTextureFlagBit;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000424 } else if (!doubledW) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000425 desc.fFlags = inDesc.fFlags;
426 desc.fWidth *= 2;
427 doubledW = true;
428 } else if (!doubledH) {
429 desc.fFlags = inDesc.fFlags;
430 desc.fWidth = origWidth;
431 desc.fHeight *= 2;
432 doubledH = true;
433 } else {
434 break;
435 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000436
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000437 } while (true);
438
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000439 if (NULL == resource) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000440 desc.fFlags = inDesc.fFlags;
441 desc.fWidth = origWidth;
442 desc.fHeight = origHeight;
443 GrTexture* texture = fGpu->createTexture(desc, NULL, 0);
444 if (NULL != texture) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000445 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000446 texture->desc(),
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000447 cacheData,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000448 true);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000449 fTextureCache->createAndLock(key, texture);
450 resource = texture;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000451 }
452 }
453
454 // If the caller gives us the same desc/sampler twice we don't want
455 // to return the same texture the second time (unless it was previously
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000456 // released). So make it exclusive to hide it from future searches.
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000457 if (NULL != resource) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000458 fTextureCache->makeExclusive(resource->getCacheEntry());
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000459 }
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000460
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000461 return static_cast<GrTexture*>(resource);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000462}
463
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000464void GrContext::addExistingTextureToCache(GrTexture* texture) {
465
466 if (NULL == texture) {
467 return;
468 }
469
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000470 // This texture should already have a cache entry since it was once
471 // attached
472 GrAssert(NULL != texture->getCacheEntry());
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000473
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000474 // Conceptually, the cache entry is going to assume responsibility
475 // for the creation ref.
476 GrAssert(1 == texture->getRefCnt());
477
478 // Since this texture came from an AutoScratchTexture it should
479 // still be in the exclusive pile
480 fTextureCache->makeNonExclusive(texture->getCacheEntry());
481
482 // and it should still be locked
483 fTextureCache->unlock(texture->getCacheEntry());
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000484}
485
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000486void GrContext::unlockTexture(GrTexture* texture) {
487 ASSERT_OWNED_RESOURCE(texture);
488 GrAssert(NULL != texture->getCacheEntry());
489
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000490 // If this is a scratch texture we detached it from the cache
491 // while it was locked (to avoid two callers simultaneously getting
492 // the same texture).
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000493 if (GrTexture::IsScratchTexture(texture->getCacheEntry()->key())) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000494 fTextureCache->makeNonExclusive(texture->getCacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000495 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000496
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000497 fTextureCache->unlock(texture->getCacheEntry());
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000498}
499
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000500GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000501 void* srcData,
502 size_t rowBytes) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000503 GrTextureDesc descCopy = descIn;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000504 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000505}
506
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000507void GrContext::getTextureCacheLimits(int* maxTextures,
508 size_t* maxTextureBytes) const {
509 fTextureCache->getLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000510}
511
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000512void GrContext::setTextureCacheLimits(int maxTextures, size_t maxTextureBytes) {
513 fTextureCache->setLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000514}
515
bsalomon@google.com91958362011-06-13 17:58:13 +0000516int GrContext::getMaxTextureSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000517 return fGpu->getCaps().fMaxTextureSize;
bsalomon@google.com91958362011-06-13 17:58:13 +0000518}
519
520int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000521 return fGpu->getCaps().fMaxRenderTargetSize;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000522}
523
524///////////////////////////////////////////////////////////////////////////////
525
bsalomon@google.come269f212011-11-07 13:29:52 +0000526GrTexture* GrContext::createPlatformTexture(const GrPlatformTextureDesc& desc) {
527 return fGpu->createPlatformTexture(desc);
528}
529
530GrRenderTarget* GrContext::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
531 return fGpu->createPlatformRenderTarget(desc);
532}
533
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000534///////////////////////////////////////////////////////////////////////////////
535
bsalomon@google.comb8670992012-07-25 21:27:09 +0000536bool GrContext::supportsIndex8PixelConfig(const GrTextureParams* params,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000537 int width, int height) const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000538 const GrDrawTarget::Caps& caps = fGpu->getCaps();
539 if (!caps.f8BitPaletteSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000540 return false;
541 }
542
bsalomon@google.com27847de2011-02-22 20:59:41 +0000543 bool isPow2 = GrIsPow2(width) && GrIsPow2(height);
544
545 if (!isPow2) {
bsalomon@google.comb8670992012-07-25 21:27:09 +0000546 bool tiled = NULL != params && params->isTiled();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000547 if (tiled && !caps.fNPOTTextureTileSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000548 return false;
549 }
550 }
551 return true;
552}
553
554////////////////////////////////////////////////////////////////////////////////
555
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000556const GrClipData* GrContext::getClip() const {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000557 return fGpu->getClip();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000558}
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000559
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000560void GrContext::setClip(const GrClipData* clipData) {
561 fGpu->setClip(clipData);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000562 fDrawState->enableState(GrDrawState::kClip_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000563}
564
bsalomon@google.com27847de2011-02-22 20:59:41 +0000565////////////////////////////////////////////////////////////////////////////////
566
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000567void GrContext::clear(const GrIRect* rect,
568 const GrColor color,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000569 GrRenderTarget* target) {
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000570 this->prepareToDraw(NULL, DEFAULT_BUFFERING)->clear(rect, color, target);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000571}
572
573void GrContext::drawPaint(const GrPaint& paint) {
574 // set rect to be big enough to fill the space, but not super-huge, so we
575 // don't overflow fixed-point implementations
bsalomon@google.comd302f142011-03-03 13:54:13 +0000576 GrRect r;
577 r.setLTRB(0, 0,
578 GrIntToScalar(getRenderTarget()->width()),
579 GrIntToScalar(getRenderTarget()->height()));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000580 GrMatrix inverse;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000581 SkTLazy<GrPaint> tmpPaint;
582 const GrPaint* p = &paint;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000583 AutoMatrix am;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000584
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000585 // We attempt to map r by the inverse matrix and draw that. mapRect will
586 // map the four corners and bound them with a new rect. This will not
587 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000588 if (!this->getMatrix().hasPerspective()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000589 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000590 GrPrintf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000591 return;
592 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000593 inverse.mapRect(&r);
594 } else {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000595 if (paint.hasTextureOrMask()) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000596 tmpPaint.set(paint);
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000597 p = tmpPaint.get();
bsalomon@google.come3d32162012-07-20 13:37:06 +0000598 if (!tmpPaint.get()->preConcatSamplerMatricesWithInverse(fDrawState->getViewMatrix())) {
599 GrPrintf("Could not invert matrix\n");
600 }
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000601 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000602 am.set(this, GrMatrix::I());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000603 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000604 // by definition this fills the entire clip, no need for AA
605 if (paint.fAntiAlias) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000606 if (!tmpPaint.isValid()) {
607 tmpPaint.set(paint);
608 p = tmpPaint.get();
609 }
610 GrAssert(p == tmpPaint.get());
611 tmpPaint.get()->fAntiAlias = false;
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000612 }
613 this->drawRect(*p, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000614}
615
bsalomon@google.com205d4602011-04-25 12:43:45 +0000616////////////////////////////////////////////////////////////////////////////////
617
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000618namespace {
619inline bool disable_coverage_aa_for_blend(GrDrawTarget* target) {
620 return DISABLE_COVERAGE_AA_FOR_BLEND && !target->canApplyCoverage();
621}
622}
623
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000624////////////////////////////////////////////////////////////////////////////////
625
bsalomon@google.com27847de2011-02-22 20:59:41 +0000626/* create a triangle strip that strokes the specified triangle. There are 8
627 unique vertices, but we repreat the last 2 to close up. Alternatively we
628 could use an indices array, and then only send 8 verts, but not sure that
629 would be faster.
630 */
bsalomon@google.com205d4602011-04-25 12:43:45 +0000631static void setStrokeRectStrip(GrPoint verts[10], GrRect rect,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000632 GrScalar width) {
633 const GrScalar rad = GrScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000634 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000635
636 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
637 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
638 verts[2].set(rect.fRight - rad, rect.fTop + rad);
639 verts[3].set(rect.fRight + rad, rect.fTop - rad);
640 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
641 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
642 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
643 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
644 verts[8] = verts[0];
645 verts[9] = verts[1];
646}
647
reed@google.com20efde72011-05-09 17:00:02 +0000648/**
649 * Returns true if the rects edges are integer-aligned.
650 */
651static bool isIRect(const GrRect& r) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000652 return GrScalarIsInt(r.fLeft) && GrScalarIsInt(r.fTop) &&
reed@google.com20efde72011-05-09 17:00:02 +0000653 GrScalarIsInt(r.fRight) && GrScalarIsInt(r.fBottom);
654}
655
bsalomon@google.com205d4602011-04-25 12:43:45 +0000656static bool apply_aa_to_rect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000657 const GrRect& rect,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000658 GrScalar width,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000659 const GrMatrix* matrix,
660 GrMatrix* combinedMatrix,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000661 GrRect* devRect,
662 bool* useVertexCoverage) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000663 // we use a simple coverage ramp to do aa on axis-aligned rects
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000664 // we check if the rect will be axis-aligned, and the rect won't land on
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000665 // integer coords.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000666
bsalomon@google.coma3108262011-10-10 14:08:47 +0000667 // we are keeping around the "tweak the alpha" trick because
668 // it is our only hope for the fixed-pipe implementation.
669 // In a shader implementation we can give a separate coverage input
bsalomon@google.com289533a2011-10-27 12:34:25 +0000670 // TODO: remove this ugliness when we drop the fixed-pipe impl
bsalomon@google.coma3108262011-10-10 14:08:47 +0000671 *useVertexCoverage = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000672 if (!target->canTweakAlphaForCoverage()) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000673 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +0000674#if GR_DEBUG
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000675 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000676#endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000677 return false;
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000678 } else {
679 *useVertexCoverage = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000680 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000681 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000682 const GrDrawState& drawState = target->getDrawState();
683 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000684 return false;
685 }
686
bsalomon@google.com471d4712011-08-23 15:45:25 +0000687 if (0 == width && target->willUseHWAALines()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000688 return false;
689 }
690
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000691 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000692 return false;
693 }
694
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000695 if (NULL != matrix &&
bsalomon@google.com205d4602011-04-25 12:43:45 +0000696 !matrix->preservesAxisAlignment()) {
697 return false;
698 }
699
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000700 *combinedMatrix = drawState.getViewMatrix();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000701 if (NULL != matrix) {
702 combinedMatrix->preConcat(*matrix);
703 GrAssert(combinedMatrix->preservesAxisAlignment());
704 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000705
bsalomon@google.com205d4602011-04-25 12:43:45 +0000706 combinedMatrix->mapRect(devRect, rect);
707 devRect->sort();
708
709 if (width < 0) {
reed@google.com20efde72011-05-09 17:00:02 +0000710 return !isIRect(*devRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000711 } else {
712 return true;
713 }
714}
715
bsalomon@google.com27847de2011-02-22 20:59:41 +0000716void GrContext::drawRect(const GrPaint& paint,
717 const GrRect& rect,
718 GrScalar width,
719 const GrMatrix* matrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000720 SK_TRACE_EVENT0("GrContext::drawRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000721
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000722 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000723 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000724
bsalomon@google.com205d4602011-04-25 12:43:45 +0000725 GrRect devRect = rect;
726 GrMatrix combinedMatrix;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000727 bool useVertexCoverage;
bsalomon@google.com289533a2011-10-27 12:34:25 +0000728 bool needAA = paint.fAntiAlias &&
729 !this->getRenderTarget()->isMultisampled();
730 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix,
731 &combinedMatrix, &devRect,
732 &useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000733
734 if (doAA) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000735 GrDrawTarget::AutoDeviceCoordDraw adcd(target);
736 if (!adcd.succeeded()) {
737 return;
738 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000739 if (width >= 0) {
740 GrVec strokeSize;;
741 if (width > 0) {
742 strokeSize.set(width, width);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000743 combinedMatrix.mapVectors(&strokeSize, 1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000744 strokeSize.setAbs(strokeSize);
745 } else {
746 strokeSize.set(GR_Scalar1, GR_Scalar1);
747 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000748 fAARectRenderer->strokeAARect(this->getGpu(), target, devRect,
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000749 strokeSize, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000750 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000751 fAARectRenderer->fillAARect(this->getGpu(), target,
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000752 devRect, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000753 }
754 return;
755 }
756
bsalomon@google.com27847de2011-02-22 20:59:41 +0000757 if (width >= 0) {
758 // TODO: consider making static vertex buffers for these cases.
759 // Hairline could be done by just adding closing vertex to
760 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000761
bsalomon@google.com27847de2011-02-22 20:59:41 +0000762 static const int worstCaseVertCount = 10;
bsalomon@google.come3d32162012-07-20 13:37:06 +0000763 GrDrawTarget::AutoReleaseGeometry geo(target, 0, worstCaseVertCount, 0);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000764
765 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000766 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000767 return;
768 }
769
770 GrPrimitiveType primType;
771 int vertCount;
772 GrPoint* vertex = geo.positions();
773
774 if (width > 0) {
775 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000776 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000777 setStrokeRectStrip(vertex, rect, width);
778 } else {
779 // hairline
780 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000781 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000782 vertex[0].set(rect.fLeft, rect.fTop);
783 vertex[1].set(rect.fRight, rect.fTop);
784 vertex[2].set(rect.fRight, rect.fBottom);
785 vertex[3].set(rect.fLeft, rect.fBottom);
786 vertex[4].set(rect.fLeft, rect.fTop);
787 }
788
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000789 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000790 if (NULL != matrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000791 GrDrawState* drawState = target->drawState();
792 avmr.set(drawState);
793 drawState->preConcatViewMatrix(*matrix);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000794 drawState->preConcatSamplerMatrices(*matrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000795 }
796
797 target->drawNonIndexed(primType, 0, vertCount);
798 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000799#if GR_STATIC_RECT_VB
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000800 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
801 if (NULL == sqVB) {
802 GrPrintf("Failed to create static rect vb.\n");
803 return;
804 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000805 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000806 GrDrawState* drawState = target->drawState();
807 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000808 GrMatrix m;
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000809 m.setAll(rect.width(), 0, rect.fLeft,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000810 0, rect.height(), rect.fTop,
811 0, 0, GrMatrix::I()[8]);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000812
813 if (NULL != matrix) {
814 m.postConcat(*matrix);
815 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000816 drawState->preConcatViewMatrix(m);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000817 drawState->preConcatSamplerMatrices(m);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000818
bsalomon@google.com47059542012-06-06 20:51:20 +0000819 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000820#else
bsalomon@google.come3d32162012-07-20 13:37:06 +0000821 target->drawSimpleRect(rect, matrix);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000822#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000823 }
824}
825
826void GrContext::drawRectToRect(const GrPaint& paint,
827 const GrRect& dstRect,
828 const GrRect& srcRect,
829 const GrMatrix* dstMatrix,
830 const GrMatrix* srcMatrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000831 SK_TRACE_EVENT0("GrContext::drawRectToRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000832
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000833 // srcRect refers to paint's first texture
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000834 if (!paint.isTextureStageEnabled(0)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000835 drawRect(paint, dstRect, -1, dstMatrix);
836 return;
837 }
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000838
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000839 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000840
841#if GR_STATIC_RECT_VB
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000842 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000843 GrDrawState* drawState = target->drawState();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000844 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000845
846 GrMatrix m;
847
848 m.setAll(dstRect.width(), 0, dstRect.fLeft,
849 0, dstRect.height(), dstRect.fTop,
850 0, 0, GrMatrix::I()[8]);
851 if (NULL != dstMatrix) {
852 m.postConcat(*dstMatrix);
853 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000854 drawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000855
bsalomon@google.come3d32162012-07-20 13:37:06 +0000856 // we explicitly setup the correct coords for the first stage. The others
857 // must know about the view matrix change.
858 for (int s = 1; s < GrPaint::kTotalStages; ++s) {
859 if (drawState->isStageEnabled(s)) {
860 drawState->sampler(s)->preConcatMatrix(m);
861 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000862 }
863
bsalomon@google.com27847de2011-02-22 20:59:41 +0000864 m.setAll(srcRect.width(), 0, srcRect.fLeft,
865 0, srcRect.height(), srcRect.fTop,
866 0, 0, GrMatrix::I()[8]);
867 if (NULL != srcMatrix) {
868 m.postConcat(*srcMatrix);
869 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000870 drawState->sampler(GrPaint::kFirstTextureStage)->preConcatMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000871
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000872 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
873 if (NULL == sqVB) {
874 GrPrintf("Failed to create static rect vb.\n");
875 return;
876 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000877 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com47059542012-06-06 20:51:20 +0000878 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000879#else
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000880 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000881
tomhudson@google.com93813632011-10-27 20:21:16 +0000882 const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
883 const GrMatrix* srcMatrices[GrDrawState::kNumStages] = {NULL};
bsalomon@google.com27847de2011-02-22 20:59:41 +0000884 srcRects[0] = &srcRect;
885 srcMatrices[0] = srcMatrix;
886
bsalomon@google.come3d32162012-07-20 13:37:06 +0000887 target->drawRect(dstRect, dstMatrix, srcRects, srcMatrices);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000888#endif
889}
890
891void GrContext::drawVertices(const GrPaint& paint,
892 GrPrimitiveType primitiveType,
893 int vertexCount,
894 const GrPoint positions[],
895 const GrPoint texCoords[],
896 const GrColor colors[],
897 const uint16_t indices[],
898 int indexCount) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000899 SK_TRACE_EVENT0("GrContext::drawVertices");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000900
901 GrDrawTarget::AutoReleaseGeometry geo;
902
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000903 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000904 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000905
bsalomon@google.come3d32162012-07-20 13:37:06 +0000906 GrVertexLayout layout = 0;
907 if (NULL != texCoords) {
908 layout |= GrDrawTarget::StageTexCoordVertexLayoutBit(0, 0);
909 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000910 if (NULL != colors) {
911 layout |= GrDrawTarget::kColor_VertexLayoutBit;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000912 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000913 int vertexSize = GrDrawTarget::VertexSize(layout);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000914
915 if (sizeof(GrPoint) != vertexSize) {
916 if (!geo.set(target, layout, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000917 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000918 return;
919 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000920 int texOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000921 int colorOffset;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000922 GrDrawTarget::VertexSizeAndOffsetsByIdx(layout,
923 texOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000924 &colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000925 NULL,
926 NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000927 void* curVertex = geo.vertices();
928
929 for (int i = 0; i < vertexCount; ++i) {
930 *((GrPoint*)curVertex) = positions[i];
931
932 if (texOffsets[0] > 0) {
933 *(GrPoint*)((intptr_t)curVertex + texOffsets[0]) = texCoords[i];
934 }
935 if (colorOffset > 0) {
936 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
937 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000938 curVertex = (void*)((intptr_t)curVertex + vertexSize);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000939 }
940 } else {
941 target->setVertexSourceToArray(layout, positions, vertexCount);
942 }
943
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000944 // we don't currently apply offscreen AA to this path. Need improved
bsalomon@google.com91958362011-06-13 17:58:13 +0000945 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000946
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000947 if (NULL != indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000948 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000949 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000950 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000951 target->drawNonIndexed(primitiveType, 0, vertexCount);
952 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000953}
954
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000955///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com150d2842012-01-12 20:19:56 +0000956namespace {
957
bsalomon@google.com93c96602012-04-27 13:05:21 +0000958struct CircleVertex {
959 GrPoint fPos;
960 GrPoint fCenter;
961 GrScalar fOuterRadius;
962 GrScalar fInnerRadius;
963};
964
965/* Returns true if will map a circle to another circle. This can be true
966 * if the matrix only includes square-scale, rotation, translation.
967 */
968inline bool isSimilarityTransformation(const SkMatrix& matrix,
969 SkScalar tol = SK_ScalarNearlyZero) {
970 if (matrix.isIdentity() || matrix.getType() == SkMatrix::kTranslate_Mask) {
971 return true;
972 }
973 if (matrix.hasPerspective()) {
974 return false;
975 }
976
977 SkScalar mx = matrix.get(SkMatrix::kMScaleX);
978 SkScalar sx = matrix.get(SkMatrix::kMSkewX);
979 SkScalar my = matrix.get(SkMatrix::kMScaleY);
980 SkScalar sy = matrix.get(SkMatrix::kMSkewY);
981
982 if (mx == 0 && sx == 0 && my == 0 && sy == 0) {
983 return false;
984 }
985
986 // it has scales or skews, but it could also be rotation, check it out.
987 SkVector vec[2];
988 vec[0].set(mx, sx);
989 vec[1].set(sy, my);
990
991 return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) &&
992 SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(),
993 SkScalarSquare(tol));
994}
995
996}
997
998// TODO: strokeWidth can't be larger than zero right now.
999// It will be fixed when drawPath() can handle strokes.
1000void GrContext::drawOval(const GrPaint& paint,
1001 const GrRect& rect,
1002 SkScalar strokeWidth) {
bsalomon@google.com0982d352012-07-31 15:33:25 +00001003 GrAssert(strokeWidth <= 0);
1004 if (!isSimilarityTransformation(this->getMatrix()) ||
bsalomon@google.com93c96602012-04-27 13:05:21 +00001005 !paint.fAntiAlias ||
1006 rect.height() != rect.width()) {
1007 SkPath path;
1008 path.addOval(rect);
1009 GrPathFill fill = (strokeWidth == 0) ?
bsalomon@google.com0982d352012-07-31 15:33:25 +00001010 kHairLine_GrPathFill : kWinding_GrPathFill;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001011 this->internalDrawPath(paint, path, fill, NULL);
1012 return;
1013 }
1014
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001015 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
1016
bsalomon@google.com0982d352012-07-31 15:33:25 +00001017 GrDrawState* drawState = target->drawState();
1018 GrDrawState::AutoStageDisable atr(fDrawState);
1019 const GrMatrix vm = drawState->getViewMatrix();
1020
bsalomon@google.com93c96602012-04-27 13:05:21 +00001021 const GrRenderTarget* rt = drawState->getRenderTarget();
1022 if (NULL == rt) {
1023 return;
1024 }
1025
bsalomon@google.come3d32162012-07-20 13:37:06 +00001026 GrDrawTarget::AutoDeviceCoordDraw adcd(target);
1027 if (!adcd.succeeded()) {
1028 return;
1029 }
bsalomon@google.com93c96602012-04-27 13:05:21 +00001030
bsalomon@google.come3d32162012-07-20 13:37:06 +00001031 GrVertexLayout layout = GrDrawTarget::kEdge_VertexLayoutBit;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001032 GrAssert(sizeof(CircleVertex) == GrDrawTarget::VertexSize(layout));
1033
1034 GrPoint center = GrPoint::Make(rect.centerX(), rect.centerY());
1035 GrScalar radius = SkScalarHalf(rect.width());
1036
1037 vm.mapPoints(&center, 1);
1038 radius = vm.mapRadius(radius);
1039
1040 GrScalar outerRadius = radius;
1041 GrScalar innerRadius = 0;
1042 SkScalar halfWidth = 0;
1043 if (strokeWidth == 0) {
1044 halfWidth = SkScalarHalf(SK_Scalar1);
1045
1046 outerRadius += halfWidth;
1047 innerRadius = SkMaxScalar(0, radius - halfWidth);
1048 }
1049
1050 GrDrawTarget::AutoReleaseGeometry geo(target, layout, 4, 0);
1051 if (!geo.succeeded()) {
1052 GrPrintf("Failed to get space for vertices!\n");
1053 return;
1054 }
1055
1056 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
1057
robertphillips@google.coma0a66c12012-06-22 13:14:29 +00001058 // The fragment shader will extend the radius out half a pixel
1059 // to antialias. Expand the drawn rect here so all the pixels
1060 // will be captured.
1061 SkScalar L = center.fX - outerRadius - SkFloatToScalar(0.5f);
1062 SkScalar R = center.fX + outerRadius + SkFloatToScalar(0.5f);
1063 SkScalar T = center.fY - outerRadius - SkFloatToScalar(0.5f);
1064 SkScalar B = center.fY + outerRadius + SkFloatToScalar(0.5f);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001065
1066 verts[0].fPos = SkPoint::Make(L, T);
1067 verts[1].fPos = SkPoint::Make(R, T);
1068 verts[2].fPos = SkPoint::Make(L, B);
1069 verts[3].fPos = SkPoint::Make(R, B);
1070
1071 for (int i = 0; i < 4; ++i) {
1072 // this goes to fragment shader, it should be in y-points-up space.
1073 verts[i].fCenter = SkPoint::Make(center.fX, rt->height() - center.fY);
1074
1075 verts[i].fOuterRadius = outerRadius;
1076 verts[i].fInnerRadius = innerRadius;
1077 }
1078
1079 drawState->setVertexEdgeType(GrDrawState::kCircle_EdgeType);
bsalomon@google.com47059542012-06-06 20:51:20 +00001080 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001081}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001082
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001083void GrContext::drawPath(const GrPaint& paint, const SkPath& path,
reed@google.com07f3ee12011-05-16 17:21:57 +00001084 GrPathFill fill, const GrPoint* translate) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001085
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001086 if (path.isEmpty()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001087 if (GrIsFillInverted(fill)) {
1088 this->drawPaint(paint);
1089 }
1090 return;
1091 }
1092
bsalomon@google.com93c96602012-04-27 13:05:21 +00001093 SkRect ovalRect;
1094 if (!GrIsFillInverted(fill) && path.isOval(&ovalRect)) {
1095 if (translate) {
1096 ovalRect.offset(*translate);
1097 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001098 SkScalar width = (fill == kHairLine_GrPathFill) ? 0 : -SK_Scalar1;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001099 this->drawOval(paint, ovalRect, width);
1100 return;
1101 }
1102
1103 internalDrawPath(paint, path, fill, translate);
1104}
1105
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001106void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +00001107 GrPathFill fill, const GrPoint* translate) {
1108
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001109 // Note that below we may sw-rasterize the path into a scratch texture.
1110 // Scratch textures can be recycled after they are returned to the texture
1111 // cache. This presents a potential hazard for buffered drawing. However,
1112 // the writePixels that uploads to the scratch will perform a flush so we're
1113 // OK.
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001114 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +00001115 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001116
bsalomon@google.com289533a2011-10-27 12:34:25 +00001117 bool prAA = paint.fAntiAlias && !this->getRenderTarget()->isMultisampled();
1118
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001119 // An Assumption here is that path renderer would use some form of tweaking
1120 // the src color (either the input alpha or in the frag shader) to implement
1121 // aa. If we have some future driver-mojo path AA that can do the right
1122 // thing WRT to the blend then we'll need some query on the PR.
1123 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001124#if GR_DEBUG
bsalomon@google.com979432b2011-11-05 21:38:22 +00001125 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001126#endif
bsalomon@google.com289533a2011-10-27 12:34:25 +00001127 prAA = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001128 }
bsalomon@google.com289533a2011-10-27 12:34:25 +00001129
robertphillips@google.com72176b22012-05-23 13:19:12 +00001130 GrPathRenderer* pr = this->getPathRenderer(path, fill, target, prAA, true);
bsalomon@google.com30085192011-08-19 15:42:31 +00001131 if (NULL == pr) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001132#if GR_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001133 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001134#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001135 return;
1136 }
1137
bsalomon@google.come3d32162012-07-20 13:37:06 +00001138 pr->drawPath(path, fill, translate, target, prAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001139}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001140
bsalomon@google.com27847de2011-02-22 20:59:41 +00001141////////////////////////////////////////////////////////////////////////////////
1142
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001143void GrContext::flush(int flagsBitfield) {
1144 if (kDiscard_FlushBit & flagsBitfield) {
1145 fDrawBuffer->reset();
1146 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001147 this->flushDrawBuffer();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001148 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001149 if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001150 fGpu->forceRenderTargetFlush();
1151 }
1152}
1153
bsalomon@google.com27847de2011-02-22 20:59:41 +00001154void GrContext::flushDrawBuffer() {
junov@google.com53a55842011-06-08 22:55:10 +00001155 if (fDrawBuffer) {
robertphillips@google.com58b38182012-05-03 16:29:41 +00001156 // With addition of the AA clip path, flushing the draw buffer can
1157 // result in the generation of an AA clip mask. During this
1158 // process the SW path renderer may be invoked which recusively
1159 // calls this method (via internalWriteTexturePixels) creating
1160 // infinite recursion
1161 GrInOrderDrawBuffer* temp = fDrawBuffer;
1162 fDrawBuffer = NULL;
1163
1164 temp->flushTo(fGpu);
1165
1166 fDrawBuffer = temp;
junov@google.com53a55842011-06-08 22:55:10 +00001167 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001168}
1169
bsalomon@google.com0342a852012-08-20 19:22:38 +00001170void GrContext::writeTexturePixels(GrTexture* texture,
1171 int left, int top, int width, int height,
1172 GrPixelConfig config, const void* buffer, size_t rowBytes,
1173 uint32_t flags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001174 SK_TRACE_EVENT0("GrContext::writeTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001175 ASSERT_OWNED_RESOURCE(texture);
1176
bsalomon@google.com0342a852012-08-20 19:22:38 +00001177 // TODO: use scratch texture to perform conversion
1178 if (kUnpremul_PixelOpsFlag & flags) {
1179 return;
1180 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001181 if (!(kDontFlush_PixelOpsFlag & flags)) {
1182 this->flush();
1183 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001184
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001185 fGpu->writeTexturePixels(texture, left, top, width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +00001186 config, buffer, rowBytes);
1187}
1188
bsalomon@google.com0342a852012-08-20 19:22:38 +00001189bool GrContext::readTexturePixels(GrTexture* texture,
1190 int left, int top, int width, int height,
1191 GrPixelConfig config, void* buffer, size_t rowBytes,
1192 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001193 SK_TRACE_EVENT0("GrContext::readTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001194 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001195
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001196 // TODO: code read pixels for textures that aren't also rendertargets
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001197 GrRenderTarget* target = texture->asRenderTarget();
1198 if (NULL != target) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001199 return this->readRenderTargetPixels(target,
1200 left, top, width, height,
1201 config, buffer, rowBytes,
1202 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001203 } else {
1204 return false;
1205 }
1206}
1207
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001208#include "SkConfig8888.h"
1209
1210namespace {
1211/**
1212 * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel
1213 * formats are representable as Config8888 and so the function returns false
1214 * if the GrPixelConfig has no equivalent Config8888.
1215 */
1216bool grconfig_to_config8888(GrPixelConfig config,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001217 bool unpremul,
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001218 SkCanvas::Config8888* config8888) {
1219 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001220 case kRGBA_8888_GrPixelConfig:
1221 if (unpremul) {
1222 *config8888 = SkCanvas::kRGBA_Unpremul_Config8888;
1223 } else {
1224 *config8888 = SkCanvas::kRGBA_Premul_Config8888;
1225 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001226 return true;
bsalomon@google.com0342a852012-08-20 19:22:38 +00001227 case kBGRA_8888_GrPixelConfig:
1228 if (unpremul) {
1229 *config8888 = SkCanvas::kBGRA_Unpremul_Config8888;
1230 } else {
1231 *config8888 = SkCanvas::kBGRA_Premul_Config8888;
1232 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001233 return true;
1234 default:
1235 return false;
1236 }
1237}
1238}
1239
bsalomon@google.com0342a852012-08-20 19:22:38 +00001240bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
1241 int left, int top, int width, int height,
1242 GrPixelConfig config, void* buffer, size_t rowBytes,
1243 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001244 SK_TRACE_EVENT0("GrContext::readRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001245 ASSERT_OWNED_RESOURCE(target);
1246
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001247 if (NULL == target) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001248 target = fDrawState->getRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001249 if (NULL == target) {
1250 return false;
1251 }
1252 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001253
bsalomon@google.com6f379512011-11-16 20:36:03 +00001254 if (!(kDontFlush_PixelOpsFlag & flags)) {
1255 this->flush();
1256 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001257
bsalomon@google.com0342a852012-08-20 19:22:38 +00001258 if ((kUnpremul_PixelOpsFlag & flags) &&
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001259 !fGpu->canPreserveReadWriteUnpremulPixels()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001260
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001261 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
bsalomon@google.com0342a852012-08-20 19:22:38 +00001262 if (!grconfig_to_config8888(target->config(), false, &srcConfig8888) ||
1263 !grconfig_to_config8888(config, true, &dstConfig8888)) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001264 return false;
1265 }
1266 // do read back using target's own config
bsalomon@google.com0342a852012-08-20 19:22:38 +00001267 this->readRenderTargetPixels(target,
1268 left, top,
1269 width, height,
1270 target->config(),
1271 buffer, rowBytes,
1272 kDontFlush_PixelOpsFlag); // we already flushed
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001273 // sw convert the pixels to unpremul config
1274 uint32_t* pixels = reinterpret_cast<uint32_t*>(buffer);
1275 SkConvertConfig8888Pixels(pixels, rowBytes, dstConfig8888,
1276 pixels, rowBytes, srcConfig8888,
1277 width, height);
1278 return true;
1279 }
1280
bsalomon@google.comc4364992011-11-07 15:54:49 +00001281 GrTexture* src = target->asTexture();
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001282 bool swapRAndB = NULL != src &&
1283 fGpu->preferredReadPixelsConfig(config) ==
1284 GrPixelConfigSwapRAndB(config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001285
1286 bool flipY = NULL != src &&
1287 fGpu->readPixelsWillPayForYFlip(target, left, top,
1288 width, height, config,
1289 rowBytes);
bsalomon@google.com0342a852012-08-20 19:22:38 +00001290 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001291
bsalomon@google.com0342a852012-08-20 19:22:38 +00001292 if (NULL == src && unpremul) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001293 // we should fallback to cpu conversion here. This could happen when
1294 // we were given an external render target by the client that is not
1295 // also a texture (e.g. FBO 0 in GL)
1296 return false;
1297 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001298 // we draw to a scratch texture if any of these conversion are applied
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001299 GrAutoScratchTexture ast;
bsalomon@google.com0342a852012-08-20 19:22:38 +00001300 if (flipY || swapRAndB || unpremul) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001301 GrAssert(NULL != src);
1302 if (swapRAndB) {
1303 config = GrPixelConfigSwapRAndB(config);
1304 GrAssert(kUnknown_GrPixelConfig != config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001305 }
1306 // Make the scratch a render target because we don't have a robust
1307 // readTexturePixels as of yet (it calls this function).
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001308 GrTextureDesc desc;
1309 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1310 desc.fWidth = width;
1311 desc.fHeight = height;
1312 desc.fConfig = config;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001313
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001314 // When a full readback is faster than a partial we could always make
1315 // the scratch exactly match the passed rect. However, if we see many
1316 // different size rectangles we will trash our texture cache and pay the
1317 // cost of creating and destroying many textures. So, we only request
1318 // an exact match when the caller is reading an entire RT.
1319 ScratchTexMatch match = kApprox_ScratchTexMatch;
1320 if (0 == left &&
1321 0 == top &&
1322 target->width() == width &&
1323 target->height() == height &&
1324 fGpu->fullReadPixelsIsFasterThanPartial()) {
1325 match = kExact_ScratchTexMatch;
1326 }
1327 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001328 GrTexture* texture = ast.texture();
1329 if (!texture) {
1330 return false;
1331 }
1332 target = texture->asRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001333 GrAssert(NULL != target);
1334
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001335 GrDrawTarget::AutoStateRestore asr(fGpu,
1336 GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001337 GrDrawState* drawState = fGpu->drawState();
1338 drawState->setRenderTarget(target);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001339
bsalomon@google.com0342a852012-08-20 19:22:38 +00001340 if (unpremul) {
1341 drawState->enableState(GrDrawState::kUnpremultiply_StageBit);
1342 }
1343
bsalomon@google.comc4364992011-11-07 15:54:49 +00001344 GrMatrix matrix;
1345 if (flipY) {
1346 matrix.setTranslate(SK_Scalar1 * left,
1347 SK_Scalar1 * (top + height));
1348 matrix.set(GrMatrix::kMScaleY, -GR_Scalar1);
1349 } else {
1350 matrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
1351 }
1352 matrix.postIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001353 drawState->sampler(0)->reset(matrix);
1354 drawState->sampler(0)->setRAndBSwap(swapRAndB);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001355 drawState->createTextureEffect(0, src);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001356 GrRect rect;
1357 rect.setXYWH(0, 0, SK_Scalar1 * width, SK_Scalar1 * height);
bsalomon@google.come3d32162012-07-20 13:37:06 +00001358 fGpu->drawSimpleRect(rect, NULL);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001359 left = 0;
1360 top = 0;
1361 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001362 return fGpu->readPixels(target,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001363 left, top, width, height,
1364 config, buffer, rowBytes, flipY);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001365}
1366
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001367void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1368 GrAssert(target);
1369 ASSERT_OWNED_RESOURCE(target);
1370 // In the future we may track whether there are any pending draws to this
1371 // target. We don't today so we always perform a flush. We don't promise
1372 // this to our clients, though.
1373 this->flush();
1374 fGpu->resolveRenderTarget(target);
1375}
1376
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001377void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst) {
1378 if (NULL == src || NULL == dst) {
1379 return;
1380 }
1381 ASSERT_OWNED_RESOURCE(src);
1382
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001383 // Writes pending to the source texture are not tracked, so a flush
1384 // is required to ensure that the copy captures the most recent contents
1385 // of the source texture. See similar behaviour in
1386 // GrContext::resolveRenderTarget.
1387 this->flush();
1388
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001389 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001390 GrDrawState* drawState = fGpu->drawState();
1391 drawState->setRenderTarget(dst);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001392 GrMatrix sampleM;
1393 sampleM.setIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001394 drawState->sampler(0)->reset(sampleM);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001395 drawState->createTextureEffect(0, src);
bsalomon@google.com5db3b6c2012-01-12 20:38:57 +00001396 SkRect rect = SkRect::MakeXYWH(0, 0,
1397 SK_Scalar1 * src->width(),
1398 SK_Scalar1 * src->height());
bsalomon@google.come3d32162012-07-20 13:37:06 +00001399 fGpu->drawSimpleRect(rect, NULL);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001400}
1401
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001402void GrContext::writeRenderTargetPixels(GrRenderTarget* target,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001403 int left, int top, int width, int height,
1404 GrPixelConfig config,
1405 const void* buffer,
1406 size_t rowBytes,
1407 uint32_t flags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001408 SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001409 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001410
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001411 if (NULL == target) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001412 target = fDrawState->getRenderTarget();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001413 if (NULL == target) {
1414 return;
1415 }
1416 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001417
1418 // TODO: when underlying api has a direct way to do this we should use it
1419 // (e.g. glDrawPixels on desktop GL).
1420
bsalomon@google.com0342a852012-08-20 19:22:38 +00001421 // If the RT is also a texture and we don't have to premultiply then take the texture path.
1422 // We expect to be at least as fast or faster since it doesn't use an intermediate texture as
1423 // we do below.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001424
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001425#if !GR_MAC_BUILD
1426 // At least some drivers on the Mac get confused when glTexImage2D is called
1427 // on a texture attached to an FBO. The FBO still sees the old image. TODO:
1428 // determine what OS versions and/or HW is affected.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001429 if (NULL != target->asTexture() && !(kUnpremul_PixelOpsFlag & flags)) {
1430 this->writeTexturePixels(target->asTexture(),
1431 left, top, width, height,
1432 config, buffer, rowBytes, flags);
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001433 return;
1434 }
1435#endif
bsalomon@google.com0342a852012-08-20 19:22:38 +00001436 if ((kUnpremul_PixelOpsFlag & flags) &&
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001437 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1438 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
bsalomon@google.com0342a852012-08-20 19:22:38 +00001439 if (!grconfig_to_config8888(config, true, &srcConfig8888) ||
1440 !grconfig_to_config8888(target->config(), false, &dstConfig8888)) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001441 return;
1442 }
1443 // allocate a tmp buffer and sw convert the pixels to premul
1444 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(width * height);
1445 const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer);
1446 SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888,
1447 src, rowBytes, srcConfig8888,
1448 width, height);
1449 // upload the already premul pixels
bsalomon@google.com0342a852012-08-20 19:22:38 +00001450 flags &= ~kUnpremul_PixelOpsFlag;
1451 this->writeRenderTargetPixels(target,
1452 left, top,
1453 width, height,
1454 target->config(),
1455 tmpPixels, 4 * width,
1456 flags);
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001457 return;
1458 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001459
1460 bool swapRAndB = fGpu->preferredReadPixelsConfig(config) ==
1461 GrPixelConfigSwapRAndB(config);
1462 if (swapRAndB) {
1463 config = GrPixelConfigSwapRAndB(config);
1464 }
1465
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001466 GrTextureDesc desc;
1467 desc.fWidth = width;
1468 desc.fHeight = height;
1469 desc.fConfig = config;
1470
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001471 GrAutoScratchTexture ast(this, desc);
1472 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001473 if (NULL == texture) {
1474 return;
1475 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001476 this->writeTexturePixels(texture, 0, 0, width, height,
1477 config, buffer, rowBytes, flags & ~kUnpremul_PixelOpsFlag);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001478
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001479 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001480 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001481
1482 GrMatrix matrix;
1483 matrix.setTranslate(GrIntToScalar(left), GrIntToScalar(top));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001484 drawState->setViewMatrix(matrix);
1485 drawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001486
bsalomon@google.com5c638652011-07-18 19:31:59 +00001487 matrix.setIDiv(texture->width(), texture->height());
bsalomon@google.comb8670992012-07-25 21:27:09 +00001488 drawState->sampler(0)->reset(matrix);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001489 drawState->createTextureEffect(0, texture);
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001490 drawState->sampler(0)->setRAndBSwap(swapRAndB);
bsalomon@google.com0342a852012-08-20 19:22:38 +00001491 drawState->sampler(0)->setPremultiply(SkToBool(kUnpremul_PixelOpsFlag & flags));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001492
tomhudson@google.comb213ed82012-06-25 15:22:12 +00001493 static const GrVertexLayout layout = 0;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001494 static const int VCOUNT = 4;
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001495 // TODO: Use GrGpu::drawRect here
bsalomon@google.com27847de2011-02-22 20:59:41 +00001496 GrDrawTarget::AutoReleaseGeometry geo(fGpu, layout, VCOUNT, 0);
1497 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001498 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +00001499 return;
1500 }
1501 ((GrPoint*)geo.vertices())->setIRectFan(0, 0, width, height);
bsalomon@google.com47059542012-06-06 20:51:20 +00001502 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, VCOUNT);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001503}
1504////////////////////////////////////////////////////////////////////////////////
1505
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001506void GrContext::setPaint(const GrPaint& paint) {
tomhudson@google.comcb325ce2012-07-11 14:41:19 +00001507 GrAssert(fDrawState->stagesDisabled());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001508
1509 for (int i = 0; i < GrPaint::kMaxTextures; ++i) {
1510 int s = i + GrPaint::kFirstTextureStage;
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001511 if (paint.isTextureStageEnabled(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001512 *fDrawState->sampler(s) = paint.getTextureSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001513 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001514 }
1515
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001516 fDrawState->setFirstCoverageStage(GrPaint::kFirstMaskStage);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001517
1518 for (int i = 0; i < GrPaint::kMaxMasks; ++i) {
1519 int s = i + GrPaint::kFirstMaskStage;
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001520 if (paint.isMaskStageEnabled(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001521 *fDrawState->sampler(s) = paint.getMaskSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001522 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001523 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001524
bsalomon@google.com26936d02012-03-19 13:06:19 +00001525 // disable all stages not accessible via the paint
1526 for (int s = GrPaint::kTotalStages; s < GrDrawState::kNumStages; ++s) {
tomhudson@google.com676e6602012-07-10 17:21:48 +00001527 fDrawState->disableStage(s);
bsalomon@google.com26936d02012-03-19 13:06:19 +00001528 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001529
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001530 fDrawState->setColor(paint.fColor);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001531
1532 if (paint.fDither) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001533 fDrawState->enableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001534 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001535 fDrawState->disableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001536 }
1537 if (paint.fAntiAlias) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001538 fDrawState->enableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001539 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001540 fDrawState->disableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001541 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001542 if (paint.fColorMatrixEnabled) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001543 fDrawState->enableState(GrDrawState::kColorMatrix_StateBit);
1544 fDrawState->setColorMatrix(paint.fColorMatrix);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001545 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001546 fDrawState->disableState(GrDrawState::kColorMatrix_StateBit);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001547 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001548 fDrawState->setBlendFunc(paint.fSrcBlendCoeff, paint.fDstBlendCoeff);
1549 fDrawState->setColorFilter(paint.fColorFilterColor, paint.fColorFilterXfermode);
1550 fDrawState->setCoverage(paint.fCoverage);
reed@google.com4b2d3f32012-05-15 18:05:50 +00001551#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
bsalomon@google.come3d32162012-07-20 13:37:06 +00001552 if ((paint.hasMask() || 0xff != paint.fCoverage) &&
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001553 !fGpu->canApplyCoverage()) {
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001554 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1555 }
bsalomon@google.com95cd7bd2012-03-28 15:35:05 +00001556#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001557}
1558
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001559GrDrawTarget* GrContext::prepareToDraw(const GrPaint* paint, BufferedDraw buffered) {
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001560 if (kNo_BufferedDraw == buffered && kYes_BufferedDraw == fLastDrawWasBuffered) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001561 this->flushDrawBuffer();
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001562 fLastDrawWasBuffered = kNo_BufferedDraw;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001563 }
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001564 if (NULL != paint) {
1565 this->setPaint(*paint);
1566 }
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001567 if (kYes_BufferedDraw == buffered) {
1568 fDrawBuffer->setClip(fGpu->getClip());
1569 fLastDrawWasBuffered = kYes_BufferedDraw;
1570 return fDrawBuffer;
1571 } else {
1572 GrAssert(kNo_BufferedDraw == buffered);
1573 return fGpu;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001574 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001575}
1576
robertphillips@google.com72176b22012-05-23 13:19:12 +00001577/*
1578 * This method finds a path renderer that can draw the specified path on
1579 * the provided target.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001580 * Due to its expense, the software path renderer has split out so it can
robertphillips@google.com72176b22012-05-23 13:19:12 +00001581 * can be individually allowed/disallowed via the "allowSW" boolean.
1582 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001583GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
bsalomon@google.com289533a2011-10-27 12:34:25 +00001584 GrPathFill fill,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001585 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001586 bool antiAlias,
1587 bool allowSW) {
bsalomon@google.com30085192011-08-19 15:42:31 +00001588 if (NULL == fPathRendererChain) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001589 fPathRendererChain =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001590 SkNEW_ARGS(GrPathRendererChain,
1591 (this, GrPathRendererChain::kNone_UsageFlag));
bsalomon@google.com30085192011-08-19 15:42:31 +00001592 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001593
1594 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path, fill,
1595 target,
1596 antiAlias);
1597
1598 if (NULL == pr && allowSW) {
1599 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001600 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001601 }
1602
1603 pr = fSoftwarePathRenderer;
1604 }
1605
1606 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001607}
1608
bsalomon@google.com27847de2011-02-22 20:59:41 +00001609////////////////////////////////////////////////////////////////////////////////
1610
bsalomon@google.com27847de2011-02-22 20:59:41 +00001611void GrContext::setRenderTarget(GrRenderTarget* target) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001612 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001613 fDrawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001614}
1615
1616GrRenderTarget* GrContext::getRenderTarget() {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001617 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001618}
1619
1620const GrRenderTarget* GrContext::getRenderTarget() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001621 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001622}
1623
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001624bool GrContext::isConfigRenderable(GrPixelConfig config) const {
1625 return fGpu->isConfigRenderable(config);
1626}
1627
bsalomon@google.com27847de2011-02-22 20:59:41 +00001628const GrMatrix& GrContext::getMatrix() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001629 return fDrawState->getViewMatrix();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001630}
1631
1632void GrContext::setMatrix(const GrMatrix& m) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001633 fDrawState->setViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001634}
1635
1636void GrContext::concatMatrix(const GrMatrix& m) const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001637 fDrawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001638}
1639
1640static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
1641 intptr_t mask = 1 << shift;
1642 if (pred) {
1643 bits |= mask;
1644 } else {
1645 bits &= ~mask;
1646 }
1647 return bits;
1648}
1649
bsalomon@google.com583a1e32011-08-17 13:42:46 +00001650GrContext::GrContext(GrGpu* gpu) {
bsalomon@google.comc0af3172012-06-15 14:10:09 +00001651 ++THREAD_INSTANCE_COUNT;
1652
bsalomon@google.com27847de2011-02-22 20:59:41 +00001653 fGpu = gpu;
1654 fGpu->ref();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001655 fGpu->setContext(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001656
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001657 fDrawState = SkNEW(GrDrawState);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001658 fGpu->setDrawState(fDrawState);
1659
bsalomon@google.com30085192011-08-19 15:42:31 +00001660 fPathRendererChain = NULL;
robertphillips@google.com72176b22012-05-23 13:19:12 +00001661 fSoftwarePathRenderer = NULL;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001662
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001663 fTextureCache = SkNEW_ARGS(GrResourceCache,
1664 (MAX_TEXTURE_CACHE_COUNT,
1665 MAX_TEXTURE_CACHE_BYTES));
1666 fFontCache = SkNEW_ARGS(GrFontCache, (fGpu));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001667
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001668 fLastDrawWasBuffered = kNo_BufferedDraw;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001669
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001670 fDrawBuffer = NULL;
1671 fDrawBufferVBAllocPool = NULL;
1672 fDrawBufferIBAllocPool = NULL;
1673
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001674 fAARectRenderer = SkNEW(GrAARectRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001675
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001676 this->setupDrawBuffer();
1677}
1678
1679void GrContext::setupDrawBuffer() {
1680
1681 GrAssert(NULL == fDrawBuffer);
1682 GrAssert(NULL == fDrawBufferVBAllocPool);
1683 GrAssert(NULL == fDrawBufferIBAllocPool);
1684
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001685 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001686 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001687 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001688 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001689 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001690 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001691 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001692 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001693
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001694 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com471d4712011-08-23 15:45:25 +00001695 fDrawBufferVBAllocPool,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001696 fDrawBufferIBAllocPool));
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001697
bsalomon@google.com27847de2011-02-22 20:59:41 +00001698 fDrawBuffer->setQuadIndexBuffer(this->getQuadIndexBuffer());
bsalomon@google.com1015e032012-06-25 18:41:04 +00001699 if (fDrawBuffer) {
1700 fDrawBuffer->setAutoFlushTarget(fGpu);
1701 fDrawBuffer->setDrawState(fDrawState);
1702 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001703}
1704
bsalomon@google.com27847de2011-02-22 20:59:41 +00001705GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) {
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001706 return prepareToDraw(&paint, DEFAULT_BUFFERING);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001707}
1708
1709const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1710 return fGpu->getQuadIndexBuffer();
1711}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001712
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001713GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture,
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001714 bool canClobberSrc,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001715 const SkRect& rect,
1716 float sigmaX, float sigmaY) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001717 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001718 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001719 AutoMatrix avm(this, GrMatrix::I());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001720 SkIRect clearRect;
bsalomon@google.comb505a122012-05-31 18:40:36 +00001721 int scaleFactorX, radiusX;
1722 int scaleFactorY, radiusY;
1723 sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX);
1724 sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY);
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001725
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001726 SkRect srcRect(rect);
1727 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
1728 srcRect.roundOut();
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001729 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001730 static_cast<float>(scaleFactorY));
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001731
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001732 AutoClip acs(this, srcRect);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001733
bsalomon@google.com0342a852012-08-20 19:22:38 +00001734 GrAssert(kBGRA_8888_GrPixelConfig == srcTexture->config() ||
1735 kRGBA_8888_GrPixelConfig == srcTexture->config() ||
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001736 kAlpha_8_GrPixelConfig == srcTexture->config());
1737
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001738 GrTextureDesc desc;
1739 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1740 desc.fWidth = SkScalarFloorToInt(srcRect.width());
1741 desc.fHeight = SkScalarFloorToInt(srcRect.height());
1742 desc.fConfig = srcTexture->config();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001743
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001744 GrAutoScratchTexture temp1, temp2;
1745 GrTexture* dstTexture = temp1.set(this, desc);
1746 GrTexture* tempTexture = canClobberSrc ? srcTexture : temp2.set(this, desc);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001747
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001748 GrPaint paint;
1749 paint.reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +00001750 paint.textureSampler(0)->textureParams()->setBilerp(true);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001751
1752 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
1753 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1754 srcTexture->height());
1755 this->setRenderTarget(dstTexture->asRenderTarget());
1756 SkRect dstRect(srcRect);
1757 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
1758 i < scaleFactorY ? 0.5f : 1.0f);
tomhudson@google.comaa72eab2012-07-19 18:01:07 +00001759 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
1760 (srcTexture)))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001761 this->drawRectToRect(paint, dstRect, srcRect);
1762 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001763 srcTexture = dstTexture;
1764 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001765 }
1766
robertphillips@google.com7a396332012-05-10 15:11:27 +00001767 SkIRect srcIRect;
1768 srcRect.roundOut(&srcIRect);
1769
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001770 if (sigmaX > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001771 if (scaleFactorX > 1) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001772 // Clear out a radius to the right of the srcRect to prevent the
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001773 // X convolution from reading garbage.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001774 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001775 radiusX, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001776 this->clear(&clearRect, 0x0);
1777 }
1778
1779 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001780 GrDrawTarget* target = this->prepareToDraw(NULL, DEFAULT_BUFFERING);
1781 convolve_gaussian(target, srcTexture, srcRect, sigmaX, radiusX,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001782 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001783 srcTexture = dstTexture;
1784 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001785 }
1786
1787 if (sigmaY > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001788 if (scaleFactorY > 1 || sigmaX > 0.0f) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001789 // Clear out a radius below the srcRect to prevent the Y
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001790 // convolution from reading garbage.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001791 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001792 srcIRect.width(), radiusY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001793 this->clear(&clearRect, 0x0);
1794 }
1795
1796 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001797 GrDrawTarget* target = this->prepareToDraw(NULL, DEFAULT_BUFFERING);
1798 convolve_gaussian(target, srcTexture, srcRect, sigmaY, radiusY,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001799 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001800 srcTexture = dstTexture;
1801 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001802 }
1803
1804 if (scaleFactorX > 1 || scaleFactorY > 1) {
1805 // Clear one pixel to the right and below, to accommodate bilinear
1806 // upsampling.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001807 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
robertphillips@google.com7a396332012-05-10 15:11:27 +00001808 srcIRect.width() + 1, 1);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001809 this->clear(&clearRect, 0x0);
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001810 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
robertphillips@google.com7a396332012-05-10 15:11:27 +00001811 1, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001812 this->clear(&clearRect, 0x0);
1813 // FIXME: This should be mitchell, not bilinear.
bsalomon@google.comb8670992012-07-25 21:27:09 +00001814 paint.textureSampler(0)->textureParams()->setBilerp(true);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001815 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1816 srcTexture->height());
1817 this->setRenderTarget(dstTexture->asRenderTarget());
tomhudson@google.comaa72eab2012-07-19 18:01:07 +00001818 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
1819 (srcTexture)))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001820 SkRect dstRect(srcRect);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001821 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001822 this->drawRectToRect(paint, dstRect, srcRect);
1823 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001824 srcTexture = dstTexture;
1825 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001826 }
1827 this->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001828 if (srcTexture == temp1.texture()) {
1829 return temp1.detach();
1830 } else if (srcTexture == temp2.texture()) {
1831 return temp2.detach();
1832 } else {
1833 srcTexture->ref();
1834 return srcTexture;
1835 }
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001836}
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001837
bsalomon@google.comc4364992011-11-07 15:54:49 +00001838///////////////////////////////////////////////////////////////////////////////
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001839#if GR_DEBUG
1840void GrContext::printCacheStats() const {
1841 fTextureCache->printStats();
1842}
1843#endif