blob: f2419ae57ef34d64c6da25a1c3fb4cadc570503a [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();
robertphillips@google.comff175842012-05-14 19:31:39 +0000149
150 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
robertphillips@google.com3319f332012-08-13 18:00:36 +0000278// The desired texture is NPOT and tiled but that isn't supported by
279// 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,
376 srcData, rowBytes,
377 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
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000394 if (kExact_ScratchTexMatch != match) {
395 // bin by pow2 with a reasonable min
396 static const int MIN_SIZE = 256;
397 desc.fWidth = GrMax(MIN_SIZE, GrNextPow2(desc.fWidth));
398 desc.fHeight = GrMax(MIN_SIZE, GrNextPow2(desc.fHeight));
399 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000400
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000401 GrResource* resource = NULL;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000402 int origWidth = desc.fWidth;
403 int origHeight = desc.fHeight;
404 bool doubledW = false;
405 bool doubledH = false;
406
407 do {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000408 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL, desc, cacheData, true);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000409 resource = fTextureCache->findAndLock(key,
410 GrResourceCache::kNested_LockType);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000411 // if we miss, relax the fit of the flags...
412 // then try doubling width... then height.
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000413 if (NULL != resource || kExact_ScratchTexMatch == match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000414 break;
415 }
416 if (!(desc.fFlags & kRenderTarget_GrTextureFlagBit)) {
417 desc.fFlags = desc.fFlags | kRenderTarget_GrTextureFlagBit;
418 } else if (desc.fFlags & kNoStencil_GrTextureFlagBit) {
419 desc.fFlags = desc.fFlags & ~kNoStencil_GrTextureFlagBit;
420 } else if (!doubledW) {
421 desc.fFlags = inDesc.fFlags;
422 desc.fWidth *= 2;
423 doubledW = true;
424 } else if (!doubledH) {
425 desc.fFlags = inDesc.fFlags;
426 desc.fWidth = origWidth;
427 desc.fHeight *= 2;
428 doubledH = true;
429 } else {
430 break;
431 }
432
433 } while (true);
434
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000435 if (NULL == resource) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000436 desc.fFlags = inDesc.fFlags;
437 desc.fWidth = origWidth;
438 desc.fHeight = origHeight;
439 GrTexture* texture = fGpu->createTexture(desc, NULL, 0);
440 if (NULL != texture) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000441 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000442 texture->desc(),
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000443 cacheData,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000444 true);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000445 fTextureCache->createAndLock(key, texture);
446 resource = texture;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000447 }
448 }
449
450 // If the caller gives us the same desc/sampler twice we don't want
451 // to return the same texture the second time (unless it was previously
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000452 // released). So make it exclusive to hide it from future searches.
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000453 if (NULL != resource) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000454 fTextureCache->makeExclusive(resource->getCacheEntry());
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000455 }
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000456
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000457 return static_cast<GrTexture*>(resource);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000458}
459
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000460void GrContext::addExistingTextureToCache(GrTexture* texture) {
461
462 if (NULL == texture) {
463 return;
464 }
465
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000466 // This texture should already have a cache entry since it was once
467 // attached
468 GrAssert(NULL != texture->getCacheEntry());
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000469
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000470 // Conceptually, the cache entry is going to assume responsibility
471 // for the creation ref.
472 GrAssert(1 == texture->getRefCnt());
473
474 // Since this texture came from an AutoScratchTexture it should
475 // still be in the exclusive pile
476 fTextureCache->makeNonExclusive(texture->getCacheEntry());
477
478 // and it should still be locked
479 fTextureCache->unlock(texture->getCacheEntry());
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000480}
481
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000482void GrContext::unlockTexture(GrTexture* texture) {
483 ASSERT_OWNED_RESOURCE(texture);
484 GrAssert(NULL != texture->getCacheEntry());
485
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000486 // If this is a scratch texture we detached it from the cache
487 // while it was locked (to avoid two callers simultaneously getting
488 // the same texture).
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000489 if (GrTexture::IsScratchTexture(texture->getCacheEntry()->key())) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000490 fTextureCache->makeNonExclusive(texture->getCacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000491 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000492
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000493 fTextureCache->unlock(texture->getCacheEntry());
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000494}
495
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000496GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000497 void* srcData,
498 size_t rowBytes) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000499 GrTextureDesc descCopy = descIn;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000500 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000501}
502
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000503void GrContext::getTextureCacheLimits(int* maxTextures,
504 size_t* maxTextureBytes) const {
505 fTextureCache->getLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000506}
507
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000508void GrContext::setTextureCacheLimits(int maxTextures, size_t maxTextureBytes) {
509 fTextureCache->setLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000510}
511
bsalomon@google.com91958362011-06-13 17:58:13 +0000512int GrContext::getMaxTextureSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000513 return fGpu->getCaps().fMaxTextureSize;
bsalomon@google.com91958362011-06-13 17:58:13 +0000514}
515
516int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000517 return fGpu->getCaps().fMaxRenderTargetSize;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000518}
519
520///////////////////////////////////////////////////////////////////////////////
521
bsalomon@google.come269f212011-11-07 13:29:52 +0000522GrTexture* GrContext::createPlatformTexture(const GrPlatformTextureDesc& desc) {
523 return fGpu->createPlatformTexture(desc);
524}
525
526GrRenderTarget* GrContext::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
527 return fGpu->createPlatformRenderTarget(desc);
528}
529
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000530///////////////////////////////////////////////////////////////////////////////
531
bsalomon@google.comb8670992012-07-25 21:27:09 +0000532bool GrContext::supportsIndex8PixelConfig(const GrTextureParams* params,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000533 int width, int height) const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000534 const GrDrawTarget::Caps& caps = fGpu->getCaps();
535 if (!caps.f8BitPaletteSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000536 return false;
537 }
538
bsalomon@google.com27847de2011-02-22 20:59:41 +0000539 bool isPow2 = GrIsPow2(width) && GrIsPow2(height);
540
541 if (!isPow2) {
bsalomon@google.comb8670992012-07-25 21:27:09 +0000542 bool tiled = NULL != params && params->isTiled();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000543 if (tiled && !caps.fNPOTTextureTileSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000544 return false;
545 }
546 }
547 return true;
548}
549
550////////////////////////////////////////////////////////////////////////////////
551
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000552const GrClipData* GrContext::getClip() const {
553 return fGpu->getClip();
554}
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000555
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000556void GrContext::setClip(const GrClipData* clipData) {
557 fGpu->setClip(clipData);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000558 fDrawState->enableState(GrDrawState::kClip_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000559}
560
bsalomon@google.com27847de2011-02-22 20:59:41 +0000561////////////////////////////////////////////////////////////////////////////////
562
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000563void GrContext::clear(const GrIRect* rect,
564 const GrColor color,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000565 GrRenderTarget* target) {
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000566 this->prepareToDraw(NULL, DEFAULT_BUFFERING)->clear(rect, color, target);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000567}
568
569void GrContext::drawPaint(const GrPaint& paint) {
570 // set rect to be big enough to fill the space, but not super-huge, so we
571 // don't overflow fixed-point implementations
bsalomon@google.comd302f142011-03-03 13:54:13 +0000572 GrRect r;
573 r.setLTRB(0, 0,
574 GrIntToScalar(getRenderTarget()->width()),
575 GrIntToScalar(getRenderTarget()->height()));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000576 GrMatrix inverse;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000577 SkTLazy<GrPaint> tmpPaint;
578 const GrPaint* p = &paint;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000579 AutoMatrix am;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000580
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000581 // We attempt to map r by the inverse matrix and draw that. mapRect will
582 // map the four corners and bound them with a new rect. This will not
583 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000584 if (!this->getMatrix().hasPerspective()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000585 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000586 GrPrintf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000587 return;
588 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000589 inverse.mapRect(&r);
590 } else {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000591 if (paint.hasTextureOrMask()) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000592 tmpPaint.set(paint);
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000593 p = tmpPaint.get();
bsalomon@google.come3d32162012-07-20 13:37:06 +0000594 if (!tmpPaint.get()->preConcatSamplerMatricesWithInverse(fDrawState->getViewMatrix())) {
595 GrPrintf("Could not invert matrix\n");
596 }
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000597 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000598 am.set(this, GrMatrix::I());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000599 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000600 // by definition this fills the entire clip, no need for AA
601 if (paint.fAntiAlias) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000602 if (!tmpPaint.isValid()) {
603 tmpPaint.set(paint);
604 p = tmpPaint.get();
605 }
606 GrAssert(p == tmpPaint.get());
607 tmpPaint.get()->fAntiAlias = false;
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000608 }
609 this->drawRect(*p, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000610}
611
bsalomon@google.com205d4602011-04-25 12:43:45 +0000612////////////////////////////////////////////////////////////////////////////////
613
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000614namespace {
615inline bool disable_coverage_aa_for_blend(GrDrawTarget* target) {
616 return DISABLE_COVERAGE_AA_FOR_BLEND && !target->canApplyCoverage();
617}
618}
619
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000620////////////////////////////////////////////////////////////////////////////////
621
bsalomon@google.com27847de2011-02-22 20:59:41 +0000622/* create a triangle strip that strokes the specified triangle. There are 8
623 unique vertices, but we repreat the last 2 to close up. Alternatively we
624 could use an indices array, and then only send 8 verts, but not sure that
625 would be faster.
626 */
bsalomon@google.com205d4602011-04-25 12:43:45 +0000627static void setStrokeRectStrip(GrPoint verts[10], GrRect rect,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000628 GrScalar width) {
629 const GrScalar rad = GrScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000630 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000631
632 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
633 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
634 verts[2].set(rect.fRight - rad, rect.fTop + rad);
635 verts[3].set(rect.fRight + rad, rect.fTop - rad);
636 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
637 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
638 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
639 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
640 verts[8] = verts[0];
641 verts[9] = verts[1];
642}
643
reed@google.com20efde72011-05-09 17:00:02 +0000644/**
645 * Returns true if the rects edges are integer-aligned.
646 */
647static bool isIRect(const GrRect& r) {
648 return GrScalarIsInt(r.fLeft) && GrScalarIsInt(r.fTop) &&
649 GrScalarIsInt(r.fRight) && GrScalarIsInt(r.fBottom);
650}
651
bsalomon@google.com205d4602011-04-25 12:43:45 +0000652static bool apply_aa_to_rect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000653 const GrRect& rect,
654 GrScalar width,
655 const GrMatrix* matrix,
656 GrMatrix* combinedMatrix,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000657 GrRect* devRect,
658 bool* useVertexCoverage) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000659 // we use a simple coverage ramp to do aa on axis-aligned rects
660 // we check if the rect will be axis-aligned, and the rect won't land on
661 // integer coords.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000662
bsalomon@google.coma3108262011-10-10 14:08:47 +0000663 // we are keeping around the "tweak the alpha" trick because
664 // it is our only hope for the fixed-pipe implementation.
665 // In a shader implementation we can give a separate coverage input
bsalomon@google.com289533a2011-10-27 12:34:25 +0000666 // TODO: remove this ugliness when we drop the fixed-pipe impl
bsalomon@google.coma3108262011-10-10 14:08:47 +0000667 *useVertexCoverage = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000668 if (!target->canTweakAlphaForCoverage()) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000669 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +0000670#if GR_DEBUG
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000671 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000672#endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000673 return false;
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000674 } else {
675 *useVertexCoverage = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000676 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000677 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000678 const GrDrawState& drawState = target->getDrawState();
679 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000680 return false;
681 }
682
bsalomon@google.com471d4712011-08-23 15:45:25 +0000683 if (0 == width && target->willUseHWAALines()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000684 return false;
685 }
686
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000687 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000688 return false;
689 }
690
691 if (NULL != matrix &&
692 !matrix->preservesAxisAlignment()) {
693 return false;
694 }
695
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000696 *combinedMatrix = drawState.getViewMatrix();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000697 if (NULL != matrix) {
698 combinedMatrix->preConcat(*matrix);
699 GrAssert(combinedMatrix->preservesAxisAlignment());
700 }
701
702 combinedMatrix->mapRect(devRect, rect);
703 devRect->sort();
704
705 if (width < 0) {
reed@google.com20efde72011-05-09 17:00:02 +0000706 return !isIRect(*devRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000707 } else {
708 return true;
709 }
710}
711
bsalomon@google.com27847de2011-02-22 20:59:41 +0000712void GrContext::drawRect(const GrPaint& paint,
713 const GrRect& rect,
714 GrScalar width,
715 const GrMatrix* matrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000716 SK_TRACE_EVENT0("GrContext::drawRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000717
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000718 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000719 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000720
bsalomon@google.com205d4602011-04-25 12:43:45 +0000721 GrRect devRect = rect;
722 GrMatrix combinedMatrix;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000723 bool useVertexCoverage;
bsalomon@google.com289533a2011-10-27 12:34:25 +0000724 bool needAA = paint.fAntiAlias &&
725 !this->getRenderTarget()->isMultisampled();
726 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix,
727 &combinedMatrix, &devRect,
728 &useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000729
730 if (doAA) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000731 GrDrawTarget::AutoDeviceCoordDraw adcd(target);
732 if (!adcd.succeeded()) {
733 return;
734 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000735 if (width >= 0) {
736 GrVec strokeSize;;
737 if (width > 0) {
738 strokeSize.set(width, width);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000739 combinedMatrix.mapVectors(&strokeSize, 1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000740 strokeSize.setAbs(strokeSize);
741 } else {
742 strokeSize.set(GR_Scalar1, GR_Scalar1);
743 }
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000744 fAARectRenderer->strokeAARect(this->getGpu(), target, devRect,
745 strokeSize, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000746 } else {
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000747 fAARectRenderer->fillAARect(this->getGpu(), target,
748 devRect, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000749 }
750 return;
751 }
752
bsalomon@google.com27847de2011-02-22 20:59:41 +0000753 if (width >= 0) {
754 // TODO: consider making static vertex buffers for these cases.
755 // Hairline could be done by just adding closing vertex to
756 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000757
bsalomon@google.com27847de2011-02-22 20:59:41 +0000758 static const int worstCaseVertCount = 10;
bsalomon@google.come3d32162012-07-20 13:37:06 +0000759 GrDrawTarget::AutoReleaseGeometry geo(target, 0, worstCaseVertCount, 0);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000760
761 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000762 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000763 return;
764 }
765
766 GrPrimitiveType primType;
767 int vertCount;
768 GrPoint* vertex = geo.positions();
769
770 if (width > 0) {
771 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000772 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000773 setStrokeRectStrip(vertex, rect, width);
774 } else {
775 // hairline
776 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000777 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000778 vertex[0].set(rect.fLeft, rect.fTop);
779 vertex[1].set(rect.fRight, rect.fTop);
780 vertex[2].set(rect.fRight, rect.fBottom);
781 vertex[3].set(rect.fLeft, rect.fBottom);
782 vertex[4].set(rect.fLeft, rect.fTop);
783 }
784
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000785 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000786 if (NULL != matrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000787 GrDrawState* drawState = target->drawState();
788 avmr.set(drawState);
789 drawState->preConcatViewMatrix(*matrix);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000790 drawState->preConcatSamplerMatrices(*matrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000791 }
792
793 target->drawNonIndexed(primType, 0, vertCount);
794 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000795#if GR_STATIC_RECT_VB
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000796 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
797 if (NULL == sqVB) {
798 GrPrintf("Failed to create static rect vb.\n");
799 return;
800 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000801 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000802 GrDrawState* drawState = target->drawState();
803 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000804 GrMatrix m;
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000805 m.setAll(rect.width(), 0, rect.fLeft,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000806 0, rect.height(), rect.fTop,
807 0, 0, GrMatrix::I()[8]);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000808
809 if (NULL != matrix) {
810 m.postConcat(*matrix);
811 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000812 drawState->preConcatViewMatrix(m);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000813 drawState->preConcatSamplerMatrices(m);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000814
bsalomon@google.com47059542012-06-06 20:51:20 +0000815 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000816#else
bsalomon@google.come3d32162012-07-20 13:37:06 +0000817 target->drawSimpleRect(rect, matrix);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000818#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000819 }
820}
821
822void GrContext::drawRectToRect(const GrPaint& paint,
823 const GrRect& dstRect,
824 const GrRect& srcRect,
825 const GrMatrix* dstMatrix,
826 const GrMatrix* srcMatrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000827 SK_TRACE_EVENT0("GrContext::drawRectToRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000828
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000829 // srcRect refers to paint's first texture
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000830 if (!paint.isTextureStageEnabled(0)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000831 drawRect(paint, dstRect, -1, dstMatrix);
832 return;
833 }
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000834
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000835 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000836
837#if GR_STATIC_RECT_VB
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000838 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000839 GrDrawState* drawState = target->drawState();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000840 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000841
842 GrMatrix m;
843
844 m.setAll(dstRect.width(), 0, dstRect.fLeft,
845 0, dstRect.height(), dstRect.fTop,
846 0, 0, GrMatrix::I()[8]);
847 if (NULL != dstMatrix) {
848 m.postConcat(*dstMatrix);
849 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000850 drawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000851
bsalomon@google.come3d32162012-07-20 13:37:06 +0000852 // we explicitly setup the correct coords for the first stage. The others
853 // must know about the view matrix change.
854 for (int s = 1; s < GrPaint::kTotalStages; ++s) {
855 if (drawState->isStageEnabled(s)) {
856 drawState->sampler(s)->preConcatMatrix(m);
857 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000858 }
859
bsalomon@google.com27847de2011-02-22 20:59:41 +0000860 m.setAll(srcRect.width(), 0, srcRect.fLeft,
861 0, srcRect.height(), srcRect.fTop,
862 0, 0, GrMatrix::I()[8]);
863 if (NULL != srcMatrix) {
864 m.postConcat(*srcMatrix);
865 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000866 drawState->sampler(GrPaint::kFirstTextureStage)->preConcatMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000867
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000868 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
869 if (NULL == sqVB) {
870 GrPrintf("Failed to create static rect vb.\n");
871 return;
872 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000873 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com47059542012-06-06 20:51:20 +0000874 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000875#else
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000876 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000877
tomhudson@google.com93813632011-10-27 20:21:16 +0000878 const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
879 const GrMatrix* srcMatrices[GrDrawState::kNumStages] = {NULL};
bsalomon@google.com27847de2011-02-22 20:59:41 +0000880 srcRects[0] = &srcRect;
881 srcMatrices[0] = srcMatrix;
882
bsalomon@google.come3d32162012-07-20 13:37:06 +0000883 target->drawRect(dstRect, dstMatrix, srcRects, srcMatrices);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000884#endif
885}
886
887void GrContext::drawVertices(const GrPaint& paint,
888 GrPrimitiveType primitiveType,
889 int vertexCount,
890 const GrPoint positions[],
891 const GrPoint texCoords[],
892 const GrColor colors[],
893 const uint16_t indices[],
894 int indexCount) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000895 SK_TRACE_EVENT0("GrContext::drawVertices");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000896
897 GrDrawTarget::AutoReleaseGeometry geo;
898
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000899 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000900 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000901
bsalomon@google.come3d32162012-07-20 13:37:06 +0000902 GrVertexLayout layout = 0;
903 if (NULL != texCoords) {
904 layout |= GrDrawTarget::StageTexCoordVertexLayoutBit(0, 0);
905 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000906 if (NULL != colors) {
907 layout |= GrDrawTarget::kColor_VertexLayoutBit;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000908 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000909 int vertexSize = GrDrawTarget::VertexSize(layout);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000910
911 if (sizeof(GrPoint) != vertexSize) {
912 if (!geo.set(target, layout, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000913 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000914 return;
915 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000916 int texOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000917 int colorOffset;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000918 GrDrawTarget::VertexSizeAndOffsetsByIdx(layout,
919 texOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000920 &colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000921 NULL,
922 NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000923 void* curVertex = geo.vertices();
924
925 for (int i = 0; i < vertexCount; ++i) {
926 *((GrPoint*)curVertex) = positions[i];
927
928 if (texOffsets[0] > 0) {
929 *(GrPoint*)((intptr_t)curVertex + texOffsets[0]) = texCoords[i];
930 }
931 if (colorOffset > 0) {
932 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
933 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000934 curVertex = (void*)((intptr_t)curVertex + vertexSize);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000935 }
936 } else {
937 target->setVertexSourceToArray(layout, positions, vertexCount);
938 }
939
bsalomon@google.com91958362011-06-13 17:58:13 +0000940 // we don't currently apply offscreen AA to this path. Need improved
941 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000942
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000943 if (NULL != indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000944 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000945 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000946 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000947 target->drawNonIndexed(primitiveType, 0, vertexCount);
948 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000949}
950
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000951///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com150d2842012-01-12 20:19:56 +0000952namespace {
953
bsalomon@google.com93c96602012-04-27 13:05:21 +0000954struct CircleVertex {
955 GrPoint fPos;
956 GrPoint fCenter;
957 GrScalar fOuterRadius;
958 GrScalar fInnerRadius;
959};
960
961/* Returns true if will map a circle to another circle. This can be true
962 * if the matrix only includes square-scale, rotation, translation.
963 */
964inline bool isSimilarityTransformation(const SkMatrix& matrix,
965 SkScalar tol = SK_ScalarNearlyZero) {
966 if (matrix.isIdentity() || matrix.getType() == SkMatrix::kTranslate_Mask) {
967 return true;
968 }
969 if (matrix.hasPerspective()) {
970 return false;
971 }
972
973 SkScalar mx = matrix.get(SkMatrix::kMScaleX);
974 SkScalar sx = matrix.get(SkMatrix::kMSkewX);
975 SkScalar my = matrix.get(SkMatrix::kMScaleY);
976 SkScalar sy = matrix.get(SkMatrix::kMSkewY);
977
978 if (mx == 0 && sx == 0 && my == 0 && sy == 0) {
979 return false;
980 }
981
982 // it has scales or skews, but it could also be rotation, check it out.
983 SkVector vec[2];
984 vec[0].set(mx, sx);
985 vec[1].set(sy, my);
986
987 return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) &&
988 SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(),
989 SkScalarSquare(tol));
990}
991
992}
993
994// TODO: strokeWidth can't be larger than zero right now.
995// It will be fixed when drawPath() can handle strokes.
996void GrContext::drawOval(const GrPaint& paint,
997 const GrRect& rect,
998 SkScalar strokeWidth) {
bsalomon@google.com0982d352012-07-31 15:33:25 +0000999 GrAssert(strokeWidth <= 0);
1000 if (!isSimilarityTransformation(this->getMatrix()) ||
bsalomon@google.com93c96602012-04-27 13:05:21 +00001001 !paint.fAntiAlias ||
1002 rect.height() != rect.width()) {
1003 SkPath path;
1004 path.addOval(rect);
1005 GrPathFill fill = (strokeWidth == 0) ?
bsalomon@google.com0982d352012-07-31 15:33:25 +00001006 kHairLine_GrPathFill : kWinding_GrPathFill;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001007 this->internalDrawPath(paint, path, fill, NULL);
1008 return;
1009 }
1010
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001011 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
1012
bsalomon@google.com0982d352012-07-31 15:33:25 +00001013 GrDrawState* drawState = target->drawState();
1014 GrDrawState::AutoStageDisable atr(fDrawState);
1015 const GrMatrix vm = drawState->getViewMatrix();
1016
bsalomon@google.com93c96602012-04-27 13:05:21 +00001017 const GrRenderTarget* rt = drawState->getRenderTarget();
1018 if (NULL == rt) {
1019 return;
1020 }
1021
bsalomon@google.come3d32162012-07-20 13:37:06 +00001022 GrDrawTarget::AutoDeviceCoordDraw adcd(target);
1023 if (!adcd.succeeded()) {
1024 return;
1025 }
bsalomon@google.com93c96602012-04-27 13:05:21 +00001026
bsalomon@google.come3d32162012-07-20 13:37:06 +00001027 GrVertexLayout layout = GrDrawTarget::kEdge_VertexLayoutBit;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001028 GrAssert(sizeof(CircleVertex) == GrDrawTarget::VertexSize(layout));
1029
1030 GrPoint center = GrPoint::Make(rect.centerX(), rect.centerY());
1031 GrScalar radius = SkScalarHalf(rect.width());
1032
1033 vm.mapPoints(&center, 1);
1034 radius = vm.mapRadius(radius);
1035
1036 GrScalar outerRadius = radius;
1037 GrScalar innerRadius = 0;
1038 SkScalar halfWidth = 0;
1039 if (strokeWidth == 0) {
1040 halfWidth = SkScalarHalf(SK_Scalar1);
1041
1042 outerRadius += halfWidth;
1043 innerRadius = SkMaxScalar(0, radius - halfWidth);
1044 }
1045
1046 GrDrawTarget::AutoReleaseGeometry geo(target, layout, 4, 0);
1047 if (!geo.succeeded()) {
1048 GrPrintf("Failed to get space for vertices!\n");
1049 return;
1050 }
1051
1052 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
1053
robertphillips@google.coma0a66c12012-06-22 13:14:29 +00001054 // The fragment shader will extend the radius out half a pixel
1055 // to antialias. Expand the drawn rect here so all the pixels
1056 // will be captured.
1057 SkScalar L = center.fX - outerRadius - SkFloatToScalar(0.5f);
1058 SkScalar R = center.fX + outerRadius + SkFloatToScalar(0.5f);
1059 SkScalar T = center.fY - outerRadius - SkFloatToScalar(0.5f);
1060 SkScalar B = center.fY + outerRadius + SkFloatToScalar(0.5f);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001061
1062 verts[0].fPos = SkPoint::Make(L, T);
1063 verts[1].fPos = SkPoint::Make(R, T);
1064 verts[2].fPos = SkPoint::Make(L, B);
1065 verts[3].fPos = SkPoint::Make(R, B);
1066
1067 for (int i = 0; i < 4; ++i) {
1068 // this goes to fragment shader, it should be in y-points-up space.
1069 verts[i].fCenter = SkPoint::Make(center.fX, rt->height() - center.fY);
1070
1071 verts[i].fOuterRadius = outerRadius;
1072 verts[i].fInnerRadius = innerRadius;
1073 }
1074
1075 drawState->setVertexEdgeType(GrDrawState::kCircle_EdgeType);
bsalomon@google.com47059542012-06-06 20:51:20 +00001076 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001077}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001078
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001079void GrContext::drawPath(const GrPaint& paint, const SkPath& path,
reed@google.com07f3ee12011-05-16 17:21:57 +00001080 GrPathFill fill, const GrPoint* translate) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001081
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001082 if (path.isEmpty()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001083 if (GrIsFillInverted(fill)) {
1084 this->drawPaint(paint);
1085 }
1086 return;
1087 }
1088
bsalomon@google.com93c96602012-04-27 13:05:21 +00001089 SkRect ovalRect;
1090 if (!GrIsFillInverted(fill) && path.isOval(&ovalRect)) {
1091 if (translate) {
1092 ovalRect.offset(*translate);
1093 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001094 SkScalar width = (fill == kHairLine_GrPathFill) ? 0 : -SK_Scalar1;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001095 this->drawOval(paint, ovalRect, width);
1096 return;
1097 }
1098
1099 internalDrawPath(paint, path, fill, translate);
1100}
1101
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001102void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +00001103 GrPathFill fill, const GrPoint* translate) {
1104
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001105 // Note that below we may sw-rasterize the path into a scratch texture.
1106 // Scratch textures can be recycled after they are returned to the texture
1107 // cache. This presents a potential hazard for buffered drawing. However,
1108 // the writePixels that uploads to the scratch will perform a flush so we're
1109 // OK.
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001110 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +00001111 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001112
bsalomon@google.com289533a2011-10-27 12:34:25 +00001113 bool prAA = paint.fAntiAlias && !this->getRenderTarget()->isMultisampled();
1114
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001115 // An Assumption here is that path renderer would use some form of tweaking
1116 // the src color (either the input alpha or in the frag shader) to implement
1117 // aa. If we have some future driver-mojo path AA that can do the right
1118 // thing WRT to the blend then we'll need some query on the PR.
1119 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001120#if GR_DEBUG
bsalomon@google.com979432b2011-11-05 21:38:22 +00001121 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001122#endif
bsalomon@google.com289533a2011-10-27 12:34:25 +00001123 prAA = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001124 }
bsalomon@google.com289533a2011-10-27 12:34:25 +00001125
robertphillips@google.com72176b22012-05-23 13:19:12 +00001126 GrPathRenderer* pr = this->getPathRenderer(path, fill, target, prAA, true);
bsalomon@google.com30085192011-08-19 15:42:31 +00001127 if (NULL == pr) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001128#if GR_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001129 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001130#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001131 return;
1132 }
1133
bsalomon@google.come3d32162012-07-20 13:37:06 +00001134 pr->drawPath(path, fill, translate, target, prAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001135}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001136
bsalomon@google.com27847de2011-02-22 20:59:41 +00001137////////////////////////////////////////////////////////////////////////////////
1138
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001139void GrContext::flush(int flagsBitfield) {
1140 if (kDiscard_FlushBit & flagsBitfield) {
1141 fDrawBuffer->reset();
1142 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001143 this->flushDrawBuffer();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001144 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001145 if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001146 fGpu->forceRenderTargetFlush();
1147 }
1148}
1149
bsalomon@google.com27847de2011-02-22 20:59:41 +00001150void GrContext::flushDrawBuffer() {
junov@google.com53a55842011-06-08 22:55:10 +00001151 if (fDrawBuffer) {
robertphillips@google.com58b38182012-05-03 16:29:41 +00001152 // With addition of the AA clip path, flushing the draw buffer can
1153 // result in the generation of an AA clip mask. During this
1154 // process the SW path renderer may be invoked which recusively
1155 // calls this method (via internalWriteTexturePixels) creating
1156 // infinite recursion
1157 GrInOrderDrawBuffer* temp = fDrawBuffer;
1158 fDrawBuffer = NULL;
1159
1160 temp->flushTo(fGpu);
1161
1162 fDrawBuffer = temp;
junov@google.com53a55842011-06-08 22:55:10 +00001163 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001164}
1165
bsalomon@google.com0342a852012-08-20 19:22:38 +00001166void GrContext::writeTexturePixels(GrTexture* texture,
1167 int left, int top, int width, int height,
1168 GrPixelConfig config, const void* buffer, size_t rowBytes,
1169 uint32_t flags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001170 SK_TRACE_EVENT0("GrContext::writeTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001171 ASSERT_OWNED_RESOURCE(texture);
1172
bsalomon@google.com0342a852012-08-20 19:22:38 +00001173 // TODO: use scratch texture to perform conversion
1174 if (kUnpremul_PixelOpsFlag & flags) {
1175 return;
1176 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001177 if (!(kDontFlush_PixelOpsFlag & flags)) {
1178 this->flush();
1179 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001180
1181 fGpu->writeTexturePixels(texture, left, top, width, height,
1182 config, buffer, rowBytes);
1183}
1184
bsalomon@google.com0342a852012-08-20 19:22:38 +00001185bool GrContext::readTexturePixels(GrTexture* texture,
1186 int left, int top, int width, int height,
1187 GrPixelConfig config, void* buffer, size_t rowBytes,
1188 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001189 SK_TRACE_EVENT0("GrContext::readTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001190 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001191
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001192 // TODO: code read pixels for textures that aren't also rendertargets
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001193 GrRenderTarget* target = texture->asRenderTarget();
1194 if (NULL != target) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001195 return this->readRenderTargetPixels(target,
1196 left, top, width, height,
1197 config, buffer, rowBytes,
1198 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001199 } else {
1200 return false;
1201 }
1202}
1203
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001204#include "SkConfig8888.h"
1205
1206namespace {
1207/**
1208 * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel
1209 * formats are representable as Config8888 and so the function returns false
1210 * if the GrPixelConfig has no equivalent Config8888.
1211 */
1212bool grconfig_to_config8888(GrPixelConfig config,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001213 bool unpremul,
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001214 SkCanvas::Config8888* config8888) {
1215 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001216 case kRGBA_8888_GrPixelConfig:
1217 if (unpremul) {
1218 *config8888 = SkCanvas::kRGBA_Unpremul_Config8888;
1219 } else {
1220 *config8888 = SkCanvas::kRGBA_Premul_Config8888;
1221 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001222 return true;
bsalomon@google.com0342a852012-08-20 19:22:38 +00001223 case kBGRA_8888_GrPixelConfig:
1224 if (unpremul) {
1225 *config8888 = SkCanvas::kBGRA_Unpremul_Config8888;
1226 } else {
1227 *config8888 = SkCanvas::kBGRA_Premul_Config8888;
1228 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001229 return true;
1230 default:
1231 return false;
1232 }
1233}
1234}
1235
bsalomon@google.com0342a852012-08-20 19:22:38 +00001236bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
1237 int left, int top, int width, int height,
1238 GrPixelConfig config, void* buffer, size_t rowBytes,
1239 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001240 SK_TRACE_EVENT0("GrContext::readRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001241 ASSERT_OWNED_RESOURCE(target);
1242
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001243 if (NULL == target) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001244 target = fDrawState->getRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001245 if (NULL == target) {
1246 return false;
1247 }
1248 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001249
bsalomon@google.com6f379512011-11-16 20:36:03 +00001250 if (!(kDontFlush_PixelOpsFlag & flags)) {
1251 this->flush();
1252 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001253
bsalomon@google.com0342a852012-08-20 19:22:38 +00001254 if ((kUnpremul_PixelOpsFlag & flags) &&
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001255 !fGpu->canPreserveReadWriteUnpremulPixels()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001256
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001257 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
bsalomon@google.com0342a852012-08-20 19:22:38 +00001258 if (!grconfig_to_config8888(target->config(), false, &srcConfig8888) ||
1259 !grconfig_to_config8888(config, true, &dstConfig8888)) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001260 return false;
1261 }
1262 // do read back using target's own config
bsalomon@google.com0342a852012-08-20 19:22:38 +00001263 this->readRenderTargetPixels(target,
1264 left, top,
1265 width, height,
1266 target->config(),
1267 buffer, rowBytes,
1268 kDontFlush_PixelOpsFlag); // we already flushed
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001269 // sw convert the pixels to unpremul config
1270 uint32_t* pixels = reinterpret_cast<uint32_t*>(buffer);
1271 SkConvertConfig8888Pixels(pixels, rowBytes, dstConfig8888,
1272 pixels, rowBytes, srcConfig8888,
1273 width, height);
1274 return true;
1275 }
1276
bsalomon@google.comc4364992011-11-07 15:54:49 +00001277 GrTexture* src = target->asTexture();
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001278 bool swapRAndB = NULL != src &&
1279 fGpu->preferredReadPixelsConfig(config) ==
1280 GrPixelConfigSwapRAndB(config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001281
1282 bool flipY = NULL != src &&
1283 fGpu->readPixelsWillPayForYFlip(target, left, top,
1284 width, height, config,
1285 rowBytes);
bsalomon@google.com0342a852012-08-20 19:22:38 +00001286 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001287
bsalomon@google.com0342a852012-08-20 19:22:38 +00001288 if (NULL == src && unpremul) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001289 // we should fallback to cpu conversion here. This could happen when
1290 // we were given an external render target by the client that is not
1291 // also a texture (e.g. FBO 0 in GL)
1292 return false;
1293 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001294 // we draw to a scratch texture if any of these conversion are applied
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001295 GrAutoScratchTexture ast;
bsalomon@google.com0342a852012-08-20 19:22:38 +00001296 if (flipY || swapRAndB || unpremul) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001297 GrAssert(NULL != src);
1298 if (swapRAndB) {
1299 config = GrPixelConfigSwapRAndB(config);
1300 GrAssert(kUnknown_GrPixelConfig != config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001301 }
1302 // Make the scratch a render target because we don't have a robust
1303 // readTexturePixels as of yet (it calls this function).
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001304 GrTextureDesc desc;
1305 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1306 desc.fWidth = width;
1307 desc.fHeight = height;
1308 desc.fConfig = config;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001309
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001310 // When a full readback is faster than a partial we could always make
1311 // the scratch exactly match the passed rect. However, if we see many
1312 // different size rectangles we will trash our texture cache and pay the
1313 // cost of creating and destroying many textures. So, we only request
1314 // an exact match when the caller is reading an entire RT.
1315 ScratchTexMatch match = kApprox_ScratchTexMatch;
1316 if (0 == left &&
1317 0 == top &&
1318 target->width() == width &&
1319 target->height() == height &&
1320 fGpu->fullReadPixelsIsFasterThanPartial()) {
1321 match = kExact_ScratchTexMatch;
1322 }
1323 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001324 GrTexture* texture = ast.texture();
1325 if (!texture) {
1326 return false;
1327 }
1328 target = texture->asRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001329 GrAssert(NULL != target);
1330
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001331 GrDrawTarget::AutoStateRestore asr(fGpu,
1332 GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001333 GrDrawState* drawState = fGpu->drawState();
1334 drawState->setRenderTarget(target);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001335
bsalomon@google.com0342a852012-08-20 19:22:38 +00001336 if (unpremul) {
1337 drawState->enableState(GrDrawState::kUnpremultiply_StageBit);
1338 }
1339
bsalomon@google.comc4364992011-11-07 15:54:49 +00001340 GrMatrix matrix;
1341 if (flipY) {
1342 matrix.setTranslate(SK_Scalar1 * left,
1343 SK_Scalar1 * (top + height));
1344 matrix.set(GrMatrix::kMScaleY, -GR_Scalar1);
1345 } else {
1346 matrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
1347 }
1348 matrix.postIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001349 drawState->sampler(0)->reset(matrix);
1350 drawState->sampler(0)->setRAndBSwap(swapRAndB);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001351 drawState->createTextureEffect(0, src);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001352 GrRect rect;
1353 rect.setXYWH(0, 0, SK_Scalar1 * width, SK_Scalar1 * height);
bsalomon@google.come3d32162012-07-20 13:37:06 +00001354 fGpu->drawSimpleRect(rect, NULL);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001355 left = 0;
1356 top = 0;
1357 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001358 return fGpu->readPixels(target,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001359 left, top, width, height,
1360 config, buffer, rowBytes, flipY);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001361}
1362
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001363void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1364 GrAssert(target);
1365 ASSERT_OWNED_RESOURCE(target);
1366 // In the future we may track whether there are any pending draws to this
1367 // target. We don't today so we always perform a flush. We don't promise
1368 // this to our clients, though.
1369 this->flush();
1370 fGpu->resolveRenderTarget(target);
1371}
1372
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001373void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst) {
1374 if (NULL == src || NULL == dst) {
1375 return;
1376 }
1377 ASSERT_OWNED_RESOURCE(src);
1378
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001379 // Writes pending to the source texture are not tracked, so a flush
1380 // is required to ensure that the copy captures the most recent contents
1381 // of the source texture. See similar behaviour in
1382 // GrContext::resolveRenderTarget.
1383 this->flush();
1384
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001385 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001386 GrDrawState* drawState = fGpu->drawState();
1387 drawState->setRenderTarget(dst);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001388 GrMatrix sampleM;
1389 sampleM.setIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001390 drawState->sampler(0)->reset(sampleM);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001391 drawState->createTextureEffect(0, src);
bsalomon@google.com5db3b6c2012-01-12 20:38:57 +00001392 SkRect rect = SkRect::MakeXYWH(0, 0,
1393 SK_Scalar1 * src->width(),
1394 SK_Scalar1 * src->height());
bsalomon@google.come3d32162012-07-20 13:37:06 +00001395 fGpu->drawSimpleRect(rect, NULL);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001396}
1397
bsalomon@google.com0342a852012-08-20 19:22:38 +00001398void GrContext::writeRenderTargetPixels(GrRenderTarget* target,
1399 int left, int top, int width, int height,
1400 GrPixelConfig config,
1401 const void* buffer,
1402 size_t rowBytes,
1403 uint32_t flags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001404 SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001405 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001406
1407 if (NULL == target) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001408 target = fDrawState->getRenderTarget();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001409 if (NULL == target) {
1410 return;
1411 }
1412 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001413
1414 // TODO: when underlying api has a direct way to do this we should use it
1415 // (e.g. glDrawPixels on desktop GL).
1416
bsalomon@google.com0342a852012-08-20 19:22:38 +00001417 // If the RT is also a texture and we don't have to premultiply then take the texture path.
1418 // We expect to be at least as fast or faster since it doesn't use an intermediate texture as
1419 // we do below.
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001420
1421#if !GR_MAC_BUILD
1422 // At least some drivers on the Mac get confused when glTexImage2D is called
1423 // on a texture attached to an FBO. The FBO still sees the old image. TODO:
1424 // determine what OS versions and/or HW is affected.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001425 if (NULL != target->asTexture() && !(kUnpremul_PixelOpsFlag & flags)) {
1426 this->writeTexturePixels(target->asTexture(),
1427 left, top, width, height,
1428 config, buffer, rowBytes, flags);
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001429 return;
1430 }
1431#endif
bsalomon@google.com0342a852012-08-20 19:22:38 +00001432 if ((kUnpremul_PixelOpsFlag & flags) &&
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001433 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1434 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
bsalomon@google.com0342a852012-08-20 19:22:38 +00001435 if (!grconfig_to_config8888(config, true, &srcConfig8888) ||
1436 !grconfig_to_config8888(target->config(), false, &dstConfig8888)) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001437 return;
1438 }
1439 // allocate a tmp buffer and sw convert the pixels to premul
1440 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(width * height);
1441 const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer);
1442 SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888,
1443 src, rowBytes, srcConfig8888,
1444 width, height);
1445 // upload the already premul pixels
bsalomon@google.com0342a852012-08-20 19:22:38 +00001446 flags &= ~kUnpremul_PixelOpsFlag;
1447 this->writeRenderTargetPixels(target,
1448 left, top,
1449 width, height,
1450 target->config(),
1451 tmpPixels, 4 * width,
1452 flags);
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001453 return;
1454 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001455
1456 bool swapRAndB = fGpu->preferredReadPixelsConfig(config) ==
1457 GrPixelConfigSwapRAndB(config);
1458 if (swapRAndB) {
1459 config = GrPixelConfigSwapRAndB(config);
1460 }
1461
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001462 GrTextureDesc desc;
1463 desc.fWidth = width;
1464 desc.fHeight = height;
1465 desc.fConfig = config;
1466
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001467 GrAutoScratchTexture ast(this, desc);
1468 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001469 if (NULL == texture) {
1470 return;
1471 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001472 this->writeTexturePixels(texture, 0, 0, width, height,
1473 config, buffer, rowBytes, flags & ~kUnpremul_PixelOpsFlag);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001474
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001475 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001476 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001477
1478 GrMatrix matrix;
1479 matrix.setTranslate(GrIntToScalar(left), GrIntToScalar(top));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001480 drawState->setViewMatrix(matrix);
1481 drawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001482
bsalomon@google.com5c638652011-07-18 19:31:59 +00001483 matrix.setIDiv(texture->width(), texture->height());
bsalomon@google.comb8670992012-07-25 21:27:09 +00001484 drawState->sampler(0)->reset(matrix);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001485 drawState->createTextureEffect(0, texture);
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001486 drawState->sampler(0)->setRAndBSwap(swapRAndB);
bsalomon@google.com0342a852012-08-20 19:22:38 +00001487 drawState->sampler(0)->setPremultiply(SkToBool(kUnpremul_PixelOpsFlag & flags));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001488
tomhudson@google.comb213ed82012-06-25 15:22:12 +00001489 static const GrVertexLayout layout = 0;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001490 static const int VCOUNT = 4;
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001491 // TODO: Use GrGpu::drawRect here
bsalomon@google.com27847de2011-02-22 20:59:41 +00001492 GrDrawTarget::AutoReleaseGeometry geo(fGpu, layout, VCOUNT, 0);
1493 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001494 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +00001495 return;
1496 }
1497 ((GrPoint*)geo.vertices())->setIRectFan(0, 0, width, height);
bsalomon@google.com47059542012-06-06 20:51:20 +00001498 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, VCOUNT);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001499}
1500////////////////////////////////////////////////////////////////////////////////
1501
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001502void GrContext::setPaint(const GrPaint& paint) {
tomhudson@google.comcb325ce2012-07-11 14:41:19 +00001503 GrAssert(fDrawState->stagesDisabled());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001504
1505 for (int i = 0; i < GrPaint::kMaxTextures; ++i) {
1506 int s = i + GrPaint::kFirstTextureStage;
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001507 if (paint.isTextureStageEnabled(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001508 *fDrawState->sampler(s) = paint.getTextureSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001509 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001510 }
1511
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001512 fDrawState->setFirstCoverageStage(GrPaint::kFirstMaskStage);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001513
1514 for (int i = 0; i < GrPaint::kMaxMasks; ++i) {
1515 int s = i + GrPaint::kFirstMaskStage;
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001516 if (paint.isMaskStageEnabled(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001517 *fDrawState->sampler(s) = paint.getMaskSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001518 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001519 }
bsalomon@google.com26936d02012-03-19 13:06:19 +00001520
1521 // disable all stages not accessible via the paint
1522 for (int s = GrPaint::kTotalStages; s < GrDrawState::kNumStages; ++s) {
tomhudson@google.com676e6602012-07-10 17:21:48 +00001523 fDrawState->disableStage(s);
bsalomon@google.com26936d02012-03-19 13:06:19 +00001524 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001525
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001526 fDrawState->setColor(paint.fColor);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001527
1528 if (paint.fDither) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001529 fDrawState->enableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001530 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001531 fDrawState->disableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001532 }
1533 if (paint.fAntiAlias) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001534 fDrawState->enableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001535 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001536 fDrawState->disableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001537 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001538 if (paint.fColorMatrixEnabled) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001539 fDrawState->enableState(GrDrawState::kColorMatrix_StateBit);
1540 fDrawState->setColorMatrix(paint.fColorMatrix);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001541 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001542 fDrawState->disableState(GrDrawState::kColorMatrix_StateBit);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001543 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001544 fDrawState->setBlendFunc(paint.fSrcBlendCoeff, paint.fDstBlendCoeff);
1545 fDrawState->setColorFilter(paint.fColorFilterColor, paint.fColorFilterXfermode);
1546 fDrawState->setCoverage(paint.fCoverage);
reed@google.com4b2d3f32012-05-15 18:05:50 +00001547#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
bsalomon@google.come3d32162012-07-20 13:37:06 +00001548 if ((paint.hasMask() || 0xff != paint.fCoverage) &&
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001549 !fGpu->canApplyCoverage()) {
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001550 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1551 }
bsalomon@google.com95cd7bd2012-03-28 15:35:05 +00001552#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001553}
1554
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001555GrDrawTarget* GrContext::prepareToDraw(const GrPaint* paint, BufferedDraw buffered) {
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001556 if (kNo_BufferedDraw == buffered && kYes_BufferedDraw == fLastDrawWasBuffered) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001557 this->flushDrawBuffer();
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001558 fLastDrawWasBuffered = kNo_BufferedDraw;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001559 }
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001560 if (NULL != paint) {
1561 this->setPaint(*paint);
1562 }
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001563 if (kYes_BufferedDraw == buffered) {
1564 fDrawBuffer->setClip(fGpu->getClip());
1565 fLastDrawWasBuffered = kYes_BufferedDraw;
1566 return fDrawBuffer;
1567 } else {
1568 GrAssert(kNo_BufferedDraw == buffered);
1569 return fGpu;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001570 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001571}
1572
robertphillips@google.com72176b22012-05-23 13:19:12 +00001573/*
1574 * This method finds a path renderer that can draw the specified path on
1575 * the provided target.
1576 * Due to its expense, the software path renderer has split out so it can
1577 * can be individually allowed/disallowed via the "allowSW" boolean.
1578 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001579GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
bsalomon@google.com289533a2011-10-27 12:34:25 +00001580 GrPathFill fill,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001581 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001582 bool antiAlias,
1583 bool allowSW) {
bsalomon@google.com30085192011-08-19 15:42:31 +00001584 if (NULL == fPathRendererChain) {
1585 fPathRendererChain =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001586 SkNEW_ARGS(GrPathRendererChain,
1587 (this, GrPathRendererChain::kNone_UsageFlag));
bsalomon@google.com30085192011-08-19 15:42:31 +00001588 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001589
1590 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path, fill,
1591 target,
1592 antiAlias);
1593
1594 if (NULL == pr && allowSW) {
1595 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001596 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001597 }
1598
1599 pr = fSoftwarePathRenderer;
1600 }
1601
1602 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001603}
1604
bsalomon@google.com27847de2011-02-22 20:59:41 +00001605////////////////////////////////////////////////////////////////////////////////
1606
bsalomon@google.com27847de2011-02-22 20:59:41 +00001607void GrContext::setRenderTarget(GrRenderTarget* target) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001608 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001609 fDrawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001610}
1611
1612GrRenderTarget* GrContext::getRenderTarget() {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001613 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001614}
1615
1616const GrRenderTarget* GrContext::getRenderTarget() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001617 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001618}
1619
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001620bool GrContext::isConfigRenderable(GrPixelConfig config) const {
1621 return fGpu->isConfigRenderable(config);
1622}
1623
bsalomon@google.com27847de2011-02-22 20:59:41 +00001624const GrMatrix& GrContext::getMatrix() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001625 return fDrawState->getViewMatrix();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001626}
1627
1628void GrContext::setMatrix(const GrMatrix& m) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001629 fDrawState->setViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001630}
1631
1632void GrContext::concatMatrix(const GrMatrix& m) const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001633 fDrawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001634}
1635
1636static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
1637 intptr_t mask = 1 << shift;
1638 if (pred) {
1639 bits |= mask;
1640 } else {
1641 bits &= ~mask;
1642 }
1643 return bits;
1644}
1645
bsalomon@google.com583a1e32011-08-17 13:42:46 +00001646GrContext::GrContext(GrGpu* gpu) {
bsalomon@google.comc0af3172012-06-15 14:10:09 +00001647 ++THREAD_INSTANCE_COUNT;
1648
bsalomon@google.com27847de2011-02-22 20:59:41 +00001649 fGpu = gpu;
1650 fGpu->ref();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001651 fGpu->setContext(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001652
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001653 fDrawState = SkNEW(GrDrawState);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001654 fGpu->setDrawState(fDrawState);
1655
bsalomon@google.com30085192011-08-19 15:42:31 +00001656 fPathRendererChain = NULL;
robertphillips@google.com72176b22012-05-23 13:19:12 +00001657 fSoftwarePathRenderer = NULL;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001658
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001659 fTextureCache = SkNEW_ARGS(GrResourceCache,
1660 (MAX_TEXTURE_CACHE_COUNT,
1661 MAX_TEXTURE_CACHE_BYTES));
1662 fFontCache = SkNEW_ARGS(GrFontCache, (fGpu));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001663
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001664 fLastDrawWasBuffered = kNo_BufferedDraw;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001665
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001666 fDrawBuffer = NULL;
1667 fDrawBufferVBAllocPool = NULL;
1668 fDrawBufferIBAllocPool = NULL;
1669
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001670 fAARectRenderer = SkNEW(GrAARectRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001671
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001672 this->setupDrawBuffer();
1673}
1674
1675void GrContext::setupDrawBuffer() {
1676
1677 GrAssert(NULL == fDrawBuffer);
1678 GrAssert(NULL == fDrawBufferVBAllocPool);
1679 GrAssert(NULL == fDrawBufferIBAllocPool);
1680
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001681 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001682 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001683 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001684 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001685 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001686 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001687 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001688 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001689
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001690 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com471d4712011-08-23 15:45:25 +00001691 fDrawBufferVBAllocPool,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001692 fDrawBufferIBAllocPool));
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001693
bsalomon@google.com27847de2011-02-22 20:59:41 +00001694 fDrawBuffer->setQuadIndexBuffer(this->getQuadIndexBuffer());
bsalomon@google.com1015e032012-06-25 18:41:04 +00001695 if (fDrawBuffer) {
1696 fDrawBuffer->setAutoFlushTarget(fGpu);
1697 fDrawBuffer->setDrawState(fDrawState);
1698 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001699}
1700
bsalomon@google.com27847de2011-02-22 20:59:41 +00001701GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) {
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001702 return prepareToDraw(&paint, DEFAULT_BUFFERING);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001703}
1704
1705const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1706 return fGpu->getQuadIndexBuffer();
1707}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001708
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001709GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture,
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001710 bool canClobberSrc,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001711 const SkRect& rect,
1712 float sigmaX, float sigmaY) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001713 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001714 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001715 AutoMatrix avm(this, GrMatrix::I());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001716 SkIRect clearRect;
bsalomon@google.comb505a122012-05-31 18:40:36 +00001717 int scaleFactorX, radiusX;
1718 int scaleFactorY, radiusY;
1719 sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX);
1720 sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY);
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001721
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001722 SkRect srcRect(rect);
1723 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
1724 srcRect.roundOut();
robertphillips@google.com8637a362012-04-10 18:32:35 +00001725 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
1726 static_cast<float>(scaleFactorY));
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001727
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001728 AutoClip acs(this, srcRect);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001729
bsalomon@google.com0342a852012-08-20 19:22:38 +00001730 GrAssert(kBGRA_8888_GrPixelConfig == srcTexture->config() ||
1731 kRGBA_8888_GrPixelConfig == srcTexture->config() ||
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001732 kAlpha_8_GrPixelConfig == srcTexture->config());
1733
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001734 GrTextureDesc desc;
1735 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1736 desc.fWidth = SkScalarFloorToInt(srcRect.width());
1737 desc.fHeight = SkScalarFloorToInt(srcRect.height());
1738 desc.fConfig = srcTexture->config();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001739
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001740 GrAutoScratchTexture temp1, temp2;
1741 GrTexture* dstTexture = temp1.set(this, desc);
1742 GrTexture* tempTexture = canClobberSrc ? srcTexture : temp2.set(this, desc);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001743
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001744 GrPaint paint;
1745 paint.reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +00001746 paint.textureSampler(0)->textureParams()->setBilerp(true);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001747
1748 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
1749 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1750 srcTexture->height());
1751 this->setRenderTarget(dstTexture->asRenderTarget());
1752 SkRect dstRect(srcRect);
1753 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
1754 i < scaleFactorY ? 0.5f : 1.0f);
tomhudson@google.comaa72eab2012-07-19 18:01:07 +00001755 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
1756 (srcTexture)))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001757 this->drawRectToRect(paint, dstRect, srcRect);
1758 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001759 srcTexture = dstTexture;
1760 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001761 }
1762
robertphillips@google.com7a396332012-05-10 15:11:27 +00001763 SkIRect srcIRect;
1764 srcRect.roundOut(&srcIRect);
1765
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001766 if (sigmaX > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001767 if (scaleFactorX > 1) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001768 // Clear out a radius to the right of the srcRect to prevent the
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001769 // X convolution from reading garbage.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001770 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001771 radiusX, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001772 this->clear(&clearRect, 0x0);
1773 }
1774
1775 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001776 GrDrawTarget* target = this->prepareToDraw(NULL, DEFAULT_BUFFERING);
1777 convolve_gaussian(target, srcTexture, srcRect, sigmaX, radiusX,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001778 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001779 srcTexture = dstTexture;
1780 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001781 }
1782
1783 if (sigmaY > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001784 if (scaleFactorY > 1 || sigmaX > 0.0f) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001785 // Clear out a radius below the srcRect to prevent the Y
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001786 // convolution from reading garbage.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001787 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001788 srcIRect.width(), radiusY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001789 this->clear(&clearRect, 0x0);
1790 }
1791
1792 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001793 GrDrawTarget* target = this->prepareToDraw(NULL, DEFAULT_BUFFERING);
1794 convolve_gaussian(target, srcTexture, srcRect, sigmaY, radiusY,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001795 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001796 srcTexture = dstTexture;
1797 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001798 }
1799
1800 if (scaleFactorX > 1 || scaleFactorY > 1) {
1801 // Clear one pixel to the right and below, to accommodate bilinear
1802 // upsampling.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001803 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
1804 srcIRect.width() + 1, 1);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001805 this->clear(&clearRect, 0x0);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001806 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
1807 1, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001808 this->clear(&clearRect, 0x0);
1809 // FIXME: This should be mitchell, not bilinear.
bsalomon@google.comb8670992012-07-25 21:27:09 +00001810 paint.textureSampler(0)->textureParams()->setBilerp(true);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001811 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1812 srcTexture->height());
1813 this->setRenderTarget(dstTexture->asRenderTarget());
tomhudson@google.comaa72eab2012-07-19 18:01:07 +00001814 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
1815 (srcTexture)))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001816 SkRect dstRect(srcRect);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001817 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001818 this->drawRectToRect(paint, dstRect, srcRect);
1819 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001820 srcTexture = dstTexture;
1821 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001822 }
1823 this->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001824 if (srcTexture == temp1.texture()) {
1825 return temp1.detach();
1826 } else if (srcTexture == temp2.texture()) {
1827 return temp2.detach();
1828 } else {
1829 srcTexture->ref();
1830 return srcTexture;
1831 }
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001832}
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001833
bsalomon@google.comc4364992011-11-07 15:54:49 +00001834///////////////////////////////////////////////////////////////////////////////
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001835#if GR_DEBUG
1836void GrContext::printCacheStats() const {
1837 fTextureCache->printStats();
1838}
1839#endif