blob: 2c724b51ee3ed32e1817e134e20363315ccfa97f [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
bsalomon@google.com27847de2011-02-22 20:59:41 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
bsalomon@google.com27847de2011-02-22 20:59:41 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
bsalomon@google.com1fadb202011-12-12 16:10:08 +000010#include "GrContext.h"
11
bsalomon@google.comb505a122012-05-31 18:40:36 +000012#include "effects/GrConvolutionEffect.h"
tomhudson@google.comaa72eab2012-07-19 18:01:07 +000013#include "effects/GrSingleTextureEffect.h"
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000014#include "effects/GrConfigConversionEffect.h"
bsalomon@google.comb505a122012-05-31 18:40:36 +000015
tomhudson@google.com278cbb42011-06-30 19:37:01 +000016#include "GrBufferAllocPool.h"
bsalomon@google.com05ef5102011-05-02 21:14:59 +000017#include "GrGpu.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000018#include "GrIndexBuffer.h"
19#include "GrInOrderDrawBuffer.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000020#include "GrPathRenderer.h"
tomhudson@google.comd22b6e42011-06-24 15:53:40 +000021#include "GrPathUtils.h"
bsalomon@google.com50398bf2011-07-26 20:45:30 +000022#include "GrResourceCache.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000023#include "GrSoftwarePathRenderer.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000024#include "GrStencilBuffer.h"
tomhudson@google.com278cbb42011-06-30 19:37:01 +000025#include "GrTextStrike.h"
bsalomon@google.com8c2fe992011-09-13 15:27:18 +000026#include "SkTLazy.h"
bsalomon@google.comc0af3172012-06-15 14:10:09 +000027#include "SkTLS.h"
tomhudson@google.com0c8d93a2011-07-01 17:08:26 +000028#include "SkTrace.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000029
reed@google.comfa35e3d2012-06-26 20:16:17 +000030SK_DEFINE_INST_COUNT(GrContext)
31SK_DEFINE_INST_COUNT(GrDrawState)
32
bsalomon@google.com1d4edd32012-08-16 18:36:06 +000033// It can be useful to set this to kNo_BufferedDraw to test whether a bug is caused by using the
34// InOrderDrawBuffer, to compare performance of using/not using InOrderDrawBuffer, or to make
35// debugging easier.
36#define DEFAULT_BUFFERING (GR_DISABLE_DRAW_BUFFERING ? kNo_BufferedDraw : kYes_BufferedDraw)
bsalomon@google.com27847de2011-02-22 20:59:41 +000037
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +000038#define MAX_BLUR_SIGMA 4.0f
39
bsalomon@google.comd46e2422011-09-23 17:40:07 +000040// When we're using coverage AA but the blend is incompatible (given gpu
41// limitations) should we disable AA or draw wrong?
bsalomon@google.com950d7a82011-09-28 15:05:33 +000042#define DISABLE_COVERAGE_AA_FOR_BLEND 1
bsalomon@google.comd46e2422011-09-23 17:40:07 +000043
reed@google.com4b2d3f32012-05-15 18:05:50 +000044#if GR_DEBUG
45 // change this to a 1 to see notifications when partial coverage fails
46 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
47#else
48 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
49#endif
50
bsalomon@google.com07fc0d12012-06-22 15:15:59 +000051static const size_t MAX_TEXTURE_CACHE_COUNT = 256;
52static const size_t MAX_TEXTURE_CACHE_BYTES = 16 * 1024 * 1024;
bsalomon@google.com27847de2011-02-22 20:59:41 +000053
bsalomon@google.com60361492012-03-15 17:47:06 +000054static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
bsalomon@google.com27847de2011-02-22 20:59:41 +000055static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
56
bsalomon@google.com1d4edd32012-08-16 18:36:06 +000057static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = 1 << 11;
58static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = 4;
bsalomon@google.com27847de2011-02-22 20:59:41 +000059
bsalomon@google.combc4b6542011-11-19 13:56:11 +000060#define ASSERT_OWNED_RESOURCE(R) GrAssert(!(R) || (R)->getContext() == this)
61
bsalomon@google.com05ef5102011-05-02 21:14:59 +000062GrContext* GrContext::Create(GrEngine engine,
63 GrPlatform3DContext context3D) {
bsalomon@google.com27847de2011-02-22 20:59:41 +000064 GrContext* ctx = NULL;
65 GrGpu* fGpu = GrGpu::Create(engine, context3D);
66 if (NULL != fGpu) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000067 ctx = SkNEW_ARGS(GrContext, (fGpu));
bsalomon@google.com27847de2011-02-22 20:59:41 +000068 fGpu->unref();
69 }
70 return ctx;
71}
72
bsalomon@google.comc0af3172012-06-15 14:10:09 +000073namespace {
74void* CreateThreadInstanceCount() {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000075 return SkNEW_ARGS(int, (0));
bsalomon@google.comc0af3172012-06-15 14:10:09 +000076}
77void DeleteThreadInstanceCount(void* v) {
78 delete reinterpret_cast<int*>(v);
79}
80#define THREAD_INSTANCE_COUNT \
81 (*reinterpret_cast<int*>(SkTLS::Get(CreateThreadInstanceCount, \
82 DeleteThreadInstanceCount)))
83
84}
85
86int GrContext::GetThreadInstanceCount() {
87 return THREAD_INSTANCE_COUNT;
88}
89
bsalomon@google.com27847de2011-02-22 20:59:41 +000090GrContext::~GrContext() {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000091 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +000092
93 // Since the gpu can hold scratch textures, give it a chance to let go
94 // of them before freeing the texture cache
95 fGpu->purgeResources();
96
bsalomon@google.com27847de2011-02-22 20:59:41 +000097 delete fTextureCache;
98 delete fFontCache;
99 delete fDrawBuffer;
100 delete fDrawBufferVBAllocPool;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000101 delete fDrawBufferIBAllocPool;
bsalomon@google.com30085192011-08-19 15:42:31 +0000102
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000103 fAARectRenderer->unref();
104
bsalomon@google.com205d4602011-04-25 12:43:45 +0000105 fGpu->unref();
bsalomon@google.com30085192011-08-19 15:42:31 +0000106 GrSafeUnref(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000107 GrSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000108 fDrawState->unref();
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000109
110 --THREAD_INSTANCE_COUNT;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000111}
112
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000113void GrContext::contextLost() {
junov@google.com53a55842011-06-08 22:55:10 +0000114 contextDestroyed();
115 this->setupDrawBuffer();
116}
117
118void GrContext::contextDestroyed() {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000119 // abandon first to so destructors
120 // don't try to free the resources in the API.
121 fGpu->abandonResources();
122
bsalomon@google.com30085192011-08-19 15:42:31 +0000123 // a path renderer may be holding onto resources that
124 // are now unusable
125 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000126 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000127
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000128 delete fDrawBuffer;
129 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000130
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000131 delete fDrawBufferVBAllocPool;
132 fDrawBufferVBAllocPool = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000133
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000134 delete fDrawBufferIBAllocPool;
135 fDrawBufferIBAllocPool = NULL;
136
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000137 fAARectRenderer->reset();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000138
bsalomon@google.coma2921122012-08-28 12:34:17 +0000139 fTextureCache->purgeAllUnlocked();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000140 fFontCache->freeAll();
141 fGpu->markContextDirty();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000142}
143
144void GrContext::resetContext() {
145 fGpu->markContextDirty();
146}
147
148void GrContext::freeGpuResources() {
149 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000150
robertphillips@google.comff175842012-05-14 19:31:39 +0000151 fGpu->purgeResources();
152
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000153 fAARectRenderer->reset();
154
bsalomon@google.coma2921122012-08-28 12:34:17 +0000155 fTextureCache->purgeAllUnlocked();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000156 fFontCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000157 // a path renderer may be holding onto resources
158 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000159 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000160}
161
twiz@google.com05e70242012-01-27 19:12:00 +0000162size_t GrContext::getGpuTextureCacheBytes() const {
163 return fTextureCache->getCachedResourceBytes();
164}
165
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000166////////////////////////////////////////////////////////////////////////////////
167
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000168namespace {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000169
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000170void scale_rect(SkRect* rect, float xScale, float yScale) {
robertphillips@google.com5af56062012-04-27 15:39:52 +0000171 rect->fLeft = SkScalarMul(rect->fLeft, SkFloatToScalar(xScale));
172 rect->fTop = SkScalarMul(rect->fTop, SkFloatToScalar(yScale));
173 rect->fRight = SkScalarMul(rect->fRight, SkFloatToScalar(xScale));
174 rect->fBottom = SkScalarMul(rect->fBottom, SkFloatToScalar(yScale));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000175}
176
bsalomon@google.comb505a122012-05-31 18:40:36 +0000177float adjust_sigma(float sigma, int *scaleFactor, int *radius) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000178 *scaleFactor = 1;
179 while (sigma > MAX_BLUR_SIGMA) {
180 *scaleFactor *= 2;
181 sigma *= 0.5f;
182 }
bsalomon@google.comb505a122012-05-31 18:40:36 +0000183 *radius = static_cast<int>(ceilf(sigma * 3.0f));
184 GrAssert(*radius <= GrConvolutionEffect::kMaxKernelRadius);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000185 return sigma;
186}
187
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000188void convolve_gaussian(GrDrawTarget* target,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000189 GrTexture* texture,
190 const SkRect& rect,
191 float sigma,
192 int radius,
193 Gr1DKernelEffect::Direction direction) {
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000194 GrRenderTarget* rt = target->drawState()->getRenderTarget();
195 GrDrawTarget::AutoStateRestore asr(target, GrDrawTarget::kReset_ASRInit);
196 GrDrawState* drawState = target->drawState();
197 drawState->setRenderTarget(rt);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000198 GrMatrix sampleM;
199 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000200 drawState->sampler(0)->reset(sampleM);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000201 SkAutoTUnref<GrConvolutionEffect> conv(SkNEW_ARGS(GrConvolutionEffect,
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000202 (texture, direction, radius,
203 sigma)));
bsalomon@google.comb505a122012-05-31 18:40:36 +0000204 drawState->sampler(0)->setCustomStage(conv);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000205 target->drawSimpleRect(rect, NULL);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000206}
207
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000208}
209
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000210
211GrTexture* GrContext::findTexture(const GrCacheKey& key) {
212 return static_cast<GrTexture*>(fTextureCache->find(key.key()));
213}
214
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000215GrTexture* GrContext::findAndLockTexture(const GrTextureDesc& desc,
216 const GrCacheData& cacheData,
217 const GrTextureParams* params) {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000218 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000219 GrResource* resource = fTextureCache->findAndLock(resourceKey,
220 GrResourceCache::kNested_LockType);
221 return static_cast<GrTexture*>(resource);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000222}
223
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000224bool GrContext::isTextureInCache(const GrTextureDesc& desc,
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000225 const GrCacheData& cacheData,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000226 const GrTextureParams* params) const {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000227 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000228 return fTextureCache->hasKey(resourceKey);
229}
230
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000231void GrContext::addAndLockStencilBuffer(GrStencilBuffer* sb) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000232 ASSERT_OWNED_RESOURCE(sb);
robertphillips@google.com46a86002012-08-08 10:42:44 +0000233
234 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(sb->width(),
235 sb->height(),
236 sb->numSamples());
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000237 fTextureCache->createAndLock(resourceKey, sb);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000238}
239
240GrStencilBuffer* GrContext::findStencilBuffer(int width, int height,
241 int sampleCnt) {
robertphillips@google.com46a86002012-08-08 10:42:44 +0000242 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(width,
243 height,
244 sampleCnt);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000245 GrResource* resource = fTextureCache->findAndLock(resourceKey,
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000246 GrResourceCache::kSingle_LockType);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000247 return static_cast<GrStencilBuffer*>(resource);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000248}
249
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000250void GrContext::unlockStencilBuffer(GrStencilBuffer* sb) {
251 ASSERT_OWNED_RESOURCE(sb);
252 GrAssert(NULL != sb->getCacheEntry());
253
254 fTextureCache->unlock(sb->getCacheEntry());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000255}
256
257static void stretchImage(void* dst,
258 int dstW,
259 int dstH,
260 void* src,
261 int srcW,
262 int srcH,
263 int bpp) {
264 GrFixed dx = (srcW << 16) / dstW;
265 GrFixed dy = (srcH << 16) / dstH;
266
267 GrFixed y = dy >> 1;
268
269 int dstXLimit = dstW*bpp;
270 for (int j = 0; j < dstH; ++j) {
271 GrFixed x = dx >> 1;
272 void* srcRow = (uint8_t*)src + (y>>16)*srcW*bpp;
273 void* dstRow = (uint8_t*)dst + j*dstW*bpp;
274 for (int i = 0; i < dstXLimit; i += bpp) {
275 memcpy((uint8_t*) dstRow + i,
276 (uint8_t*) srcRow + (x>>16)*bpp,
277 bpp);
278 x += dx;
279 }
280 y += dy;
281 }
282}
283
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000284// The desired texture is NPOT and tiled but that isn't supported by
robertphillips@google.com3319f332012-08-13 18:00:36 +0000285// the current hardware. Resize the texture to be a POT
286GrTexture* GrContext::createResizedTexture(const GrTextureDesc& desc,
287 const GrCacheData& cacheData,
288 void* srcData,
289 size_t rowBytes,
290 bool needsFiltering) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000291 GrTexture* clampedTexture = this->findAndLockTexture(desc, cacheData, NULL);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000292
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000293 if (NULL == clampedTexture) {
294 clampedTexture = this->createAndLockTexture(NULL, desc, cacheData, srcData, rowBytes);
295 GrAssert(NULL != clampedTexture);
296 if (NULL == clampedTexture) {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000297 return NULL;
298 }
299 }
300 GrTextureDesc rtDesc = desc;
301 rtDesc.fFlags = rtDesc.fFlags |
302 kRenderTarget_GrTextureFlagBit |
303 kNoStencil_GrTextureFlagBit;
304 rtDesc.fWidth = GrNextPow2(GrMax(desc.fWidth, 64));
305 rtDesc.fHeight = GrNextPow2(GrMax(desc.fHeight, 64));
306
307 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
308
309 if (NULL != texture) {
310 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
311 GrDrawState* drawState = fGpu->drawState();
312 drawState->setRenderTarget(texture->asRenderTarget());
313
314 // if filtering is not desired then we want to ensure all
315 // texels in the resampled image are copies of texels from
316 // the original.
317 drawState->sampler(0)->reset(SkShader::kClamp_TileMode,
318 needsFiltering);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000319 drawState->createTextureEffect(0, clampedTexture);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000320
321 static const GrVertexLayout layout =
322 GrDrawTarget::StageTexCoordVertexLayoutBit(0,0);
323 GrDrawTarget::AutoReleaseGeometry arg(fGpu, layout, 4, 0);
324
325 if (arg.succeeded()) {
326 GrPoint* verts = (GrPoint*) arg.vertices();
327 verts[0].setIRectFan(0, 0,
328 texture->width(),
329 texture->height(),
330 2*sizeof(GrPoint));
331 verts[1].setIRectFan(0, 0, 1, 1, 2*sizeof(GrPoint));
332 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType,
333 0, 4);
334 }
335 texture->releaseRenderTarget();
336 } else {
337 // TODO: Our CPU stretch doesn't filter. But we create separate
338 // stretched textures when the sampler state is either filtered or
339 // not. Either implement filtered stretch blit on CPU or just create
340 // one when FBO case fails.
341
342 rtDesc.fFlags = kNone_GrTextureFlags;
343 // no longer need to clamp at min RT size.
344 rtDesc.fWidth = GrNextPow2(desc.fWidth);
345 rtDesc.fHeight = GrNextPow2(desc.fHeight);
346 int bpp = GrBytesPerPixel(desc.fConfig);
347 SkAutoSMalloc<128*128*4> stretchedPixels(bpp *
348 rtDesc.fWidth *
349 rtDesc.fHeight);
350 stretchImage(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
351 srcData, desc.fWidth, desc.fHeight, bpp);
352
353 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
354
355 GrTexture* texture = fGpu->createTexture(rtDesc,
356 stretchedPixels.get(),
357 stretchedRowBytes);
358 GrAssert(NULL != texture);
359 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000360 this->unlockTexture(clampedTexture);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000361
362 return texture;
363}
364
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000365GrTexture* GrContext::createAndLockTexture(
bsalomon@google.comb8670992012-07-25 21:27:09 +0000366 const GrTextureParams* params,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000367 const GrTextureDesc& desc,
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000368 const GrCacheData& cacheData,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000369 void* srcData,
370 size_t rowBytes) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000371 SK_TRACE_EVENT0("GrContext::createAndLockTexture");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000372
373#if GR_DUMP_TEXTURE_UPLOAD
374 GrPrintf("GrContext::createAndLockTexture [%d %d]\n", desc.fWidth, desc.fHeight);
375#endif
376
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000377 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000378
robertphillips@google.com3319f332012-08-13 18:00:36 +0000379 GrTexture* texture = NULL;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000380 if (GrTexture::NeedsResizing(resourceKey)) {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000381 texture = this->createResizedTexture(desc, cacheData,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000382 srcData, rowBytes,
robertphillips@google.com3319f332012-08-13 18:00:36 +0000383 GrTexture::NeedsFiltering(resourceKey));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000384 } else {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000385 texture = fGpu->createTexture(desc, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000386 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000387
388 if (NULL != texture) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000389 fTextureCache->createAndLock(resourceKey, texture);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000390 }
391
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000392 return texture;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000393}
394
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000395GrTexture* GrContext::lockScratchTexture(const GrTextureDesc& inDesc,
396 ScratchTexMatch match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000397 GrTextureDesc desc = inDesc;
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000398 GrCacheData cacheData(GrCacheData::kScratch_CacheID);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000399
rileya@google.com2afb8ec2012-08-23 14:08:57 +0000400 GrAssert((desc.fFlags & kRenderTarget_GrTextureFlagBit) ||
401 !(desc.fFlags & kNoStencil_GrTextureFlagBit));
402
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000403 if (kExact_ScratchTexMatch != match) {
404 // bin by pow2 with a reasonable min
405 static const int MIN_SIZE = 256;
406 desc.fWidth = GrMax(MIN_SIZE, GrNextPow2(desc.fWidth));
407 desc.fHeight = GrMax(MIN_SIZE, GrNextPow2(desc.fHeight));
408 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000409
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000410 GrResource* resource = NULL;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000411 int origWidth = desc.fWidth;
412 int origHeight = desc.fHeight;
413 bool doubledW = false;
414 bool doubledH = false;
415
416 do {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000417 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL, desc, cacheData, true);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000418 resource = fTextureCache->findAndLock(key,
419 GrResourceCache::kNested_LockType);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000420 // if we miss, relax the fit of the flags...
421 // then try doubling width... then height.
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000422 if (NULL != resource || kExact_ScratchTexMatch == match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000423 break;
424 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000425 // We no longer try to reuse textures that were previously used as render targets in
rileya@google.com2afb8ec2012-08-23 14:08:57 +0000426 // situations where no RT is needed; doing otherwise can confuse the video driver and
427 // cause significant performance problems in some cases.
428 if (desc.fFlags & kNoStencil_GrTextureFlagBit) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000429 desc.fFlags = desc.fFlags & ~kNoStencil_GrTextureFlagBit;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000430 } else if (!doubledW) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000431 desc.fFlags = inDesc.fFlags;
432 desc.fWidth *= 2;
433 doubledW = true;
434 } else if (!doubledH) {
435 desc.fFlags = inDesc.fFlags;
436 desc.fWidth = origWidth;
437 desc.fHeight *= 2;
438 doubledH = true;
439 } else {
440 break;
441 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000442
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000443 } while (true);
444
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000445 if (NULL == resource) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000446 desc.fFlags = inDesc.fFlags;
447 desc.fWidth = origWidth;
448 desc.fHeight = origHeight;
449 GrTexture* texture = fGpu->createTexture(desc, NULL, 0);
450 if (NULL != texture) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000451 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000452 texture->desc(),
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000453 cacheData,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000454 true);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000455 fTextureCache->createAndLock(key, texture);
456 resource = texture;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000457 }
458 }
459
460 // If the caller gives us the same desc/sampler twice we don't want
461 // to return the same texture the second time (unless it was previously
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000462 // released). So make it exclusive to hide it from future searches.
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000463 if (NULL != resource) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000464 fTextureCache->makeExclusive(resource->getCacheEntry());
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000465 }
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000466
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000467 return static_cast<GrTexture*>(resource);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000468}
469
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000470void GrContext::addExistingTextureToCache(GrTexture* texture) {
471
472 if (NULL == texture) {
473 return;
474 }
475
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000476 // This texture should already have a cache entry since it was once
477 // attached
478 GrAssert(NULL != texture->getCacheEntry());
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000479
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000480 // Conceptually, the cache entry is going to assume responsibility
481 // for the creation ref.
482 GrAssert(1 == texture->getRefCnt());
483
484 // Since this texture came from an AutoScratchTexture it should
485 // still be in the exclusive pile
486 fTextureCache->makeNonExclusive(texture->getCacheEntry());
487
488 // and it should still be locked
489 fTextureCache->unlock(texture->getCacheEntry());
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000490}
491
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000492void GrContext::lockTexture(GrTexture* texture) {
493
494 if (NULL == texture->getCacheEntry()) {
495 // not in the cache
496 GrAssert(0);
497 return;
498 }
499
500 fTextureCache->lock(texture->getCacheEntry());
501}
502
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000503void GrContext::unlockTexture(GrTexture* texture) {
504 ASSERT_OWNED_RESOURCE(texture);
505 GrAssert(NULL != texture->getCacheEntry());
506
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000507 // If this is a scratch texture we detached it from the cache
508 // while it was locked (to avoid two callers simultaneously getting
509 // the same texture).
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000510 if (GrTexture::IsScratchTexture(texture->getCacheEntry()->key())) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000511 fTextureCache->makeNonExclusive(texture->getCacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000512 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000513
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000514 fTextureCache->unlock(texture->getCacheEntry());
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000515}
516
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000517GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000518 void* srcData,
519 size_t rowBytes) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000520 GrTextureDesc descCopy = descIn;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000521 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000522}
523
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000524void GrContext::getTextureCacheLimits(int* maxTextures,
525 size_t* maxTextureBytes) const {
526 fTextureCache->getLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000527}
528
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000529void GrContext::setTextureCacheLimits(int maxTextures, size_t maxTextureBytes) {
530 fTextureCache->setLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000531}
532
bsalomon@google.com91958362011-06-13 17:58:13 +0000533int GrContext::getMaxTextureSize() const {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000534 return fGpu->getCaps().maxTextureSize();
bsalomon@google.com91958362011-06-13 17:58:13 +0000535}
536
537int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000538 return fGpu->getCaps().maxRenderTargetSize();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000539}
540
541///////////////////////////////////////////////////////////////////////////////
542
bsalomon@google.come269f212011-11-07 13:29:52 +0000543GrTexture* GrContext::createPlatformTexture(const GrPlatformTextureDesc& desc) {
544 return fGpu->createPlatformTexture(desc);
545}
546
547GrRenderTarget* GrContext::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
548 return fGpu->createPlatformRenderTarget(desc);
549}
550
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000551///////////////////////////////////////////////////////////////////////////////
552
bsalomon@google.comb8670992012-07-25 21:27:09 +0000553bool GrContext::supportsIndex8PixelConfig(const GrTextureParams* params,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000554 int width, int height) const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000555 const GrDrawTarget::Caps& caps = fGpu->getCaps();
bsalomon@google.comf6601872012-08-28 21:11:35 +0000556 if (!caps.eightBitPaletteSupport()) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000557 return false;
558 }
559
bsalomon@google.com27847de2011-02-22 20:59:41 +0000560 bool isPow2 = GrIsPow2(width) && GrIsPow2(height);
561
562 if (!isPow2) {
bsalomon@google.comb8670992012-07-25 21:27:09 +0000563 bool tiled = NULL != params && params->isTiled();
bsalomon@google.comf6601872012-08-28 21:11:35 +0000564 if (tiled && !caps.npotTextureTileSupport()) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000565 return false;
566 }
567 }
568 return true;
569}
570
571////////////////////////////////////////////////////////////////////////////////
572
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000573const GrClipData* GrContext::getClip() const {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000574 return fGpu->getClip();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000575}
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000576
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000577void GrContext::setClip(const GrClipData* clipData) {
578 fGpu->setClip(clipData);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000579 fDrawState->enableState(GrDrawState::kClip_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000580}
581
bsalomon@google.com27847de2011-02-22 20:59:41 +0000582////////////////////////////////////////////////////////////////////////////////
583
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000584void GrContext::clear(const GrIRect* rect,
585 const GrColor color,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000586 GrRenderTarget* target) {
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000587 this->prepareToDraw(NULL, DEFAULT_BUFFERING)->clear(rect, color, target);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000588}
589
590void GrContext::drawPaint(const GrPaint& paint) {
591 // set rect to be big enough to fill the space, but not super-huge, so we
592 // don't overflow fixed-point implementations
bsalomon@google.comd302f142011-03-03 13:54:13 +0000593 GrRect r;
594 r.setLTRB(0, 0,
595 GrIntToScalar(getRenderTarget()->width()),
596 GrIntToScalar(getRenderTarget()->height()));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000597 GrMatrix inverse;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000598 SkTLazy<GrPaint> tmpPaint;
599 const GrPaint* p = &paint;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000600 AutoMatrix am;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000601
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000602 // We attempt to map r by the inverse matrix and draw that. mapRect will
603 // map the four corners and bound them with a new rect. This will not
604 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000605 if (!this->getMatrix().hasPerspective()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000606 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000607 GrPrintf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000608 return;
609 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000610 inverse.mapRect(&r);
611 } else {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000612 if (paint.hasTextureOrMask()) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000613 tmpPaint.set(paint);
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000614 p = tmpPaint.get();
bsalomon@google.come3d32162012-07-20 13:37:06 +0000615 if (!tmpPaint.get()->preConcatSamplerMatricesWithInverse(fDrawState->getViewMatrix())) {
616 GrPrintf("Could not invert matrix\n");
617 }
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000618 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000619 am.set(this, GrMatrix::I());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000620 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000621 // by definition this fills the entire clip, no need for AA
622 if (paint.fAntiAlias) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000623 if (!tmpPaint.isValid()) {
624 tmpPaint.set(paint);
625 p = tmpPaint.get();
626 }
627 GrAssert(p == tmpPaint.get());
628 tmpPaint.get()->fAntiAlias = false;
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000629 }
630 this->drawRect(*p, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000631}
632
bsalomon@google.com205d4602011-04-25 12:43:45 +0000633////////////////////////////////////////////////////////////////////////////////
634
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000635namespace {
636inline bool disable_coverage_aa_for_blend(GrDrawTarget* target) {
637 return DISABLE_COVERAGE_AA_FOR_BLEND && !target->canApplyCoverage();
638}
639}
640
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000641////////////////////////////////////////////////////////////////////////////////
642
bsalomon@google.com27847de2011-02-22 20:59:41 +0000643/* create a triangle strip that strokes the specified triangle. There are 8
644 unique vertices, but we repreat the last 2 to close up. Alternatively we
645 could use an indices array, and then only send 8 verts, but not sure that
646 would be faster.
647 */
bsalomon@google.com205d4602011-04-25 12:43:45 +0000648static void setStrokeRectStrip(GrPoint verts[10], GrRect rect,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000649 GrScalar width) {
650 const GrScalar rad = GrScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000651 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000652
653 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
654 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
655 verts[2].set(rect.fRight - rad, rect.fTop + rad);
656 verts[3].set(rect.fRight + rad, rect.fTop - rad);
657 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
658 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
659 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
660 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
661 verts[8] = verts[0];
662 verts[9] = verts[1];
663}
664
reed@google.com20efde72011-05-09 17:00:02 +0000665/**
666 * Returns true if the rects edges are integer-aligned.
667 */
668static bool isIRect(const GrRect& r) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000669 return GrScalarIsInt(r.fLeft) && GrScalarIsInt(r.fTop) &&
reed@google.com20efde72011-05-09 17:00:02 +0000670 GrScalarIsInt(r.fRight) && GrScalarIsInt(r.fBottom);
671}
672
bsalomon@google.com205d4602011-04-25 12:43:45 +0000673static bool apply_aa_to_rect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000674 const GrRect& rect,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000675 GrScalar width,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000676 const GrMatrix* matrix,
677 GrMatrix* combinedMatrix,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000678 GrRect* devRect,
679 bool* useVertexCoverage) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000680 // we use a simple coverage ramp to do aa on axis-aligned rects
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000681 // we check if the rect will be axis-aligned, and the rect won't land on
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000682 // integer coords.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000683
bsalomon@google.coma3108262011-10-10 14:08:47 +0000684 // we are keeping around the "tweak the alpha" trick because
685 // it is our only hope for the fixed-pipe implementation.
686 // In a shader implementation we can give a separate coverage input
bsalomon@google.com289533a2011-10-27 12:34:25 +0000687 // TODO: remove this ugliness when we drop the fixed-pipe impl
bsalomon@google.coma3108262011-10-10 14:08:47 +0000688 *useVertexCoverage = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000689 if (!target->canTweakAlphaForCoverage()) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000690 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +0000691#if GR_DEBUG
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000692 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000693#endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000694 return false;
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000695 } else {
696 *useVertexCoverage = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000697 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000698 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000699 const GrDrawState& drawState = target->getDrawState();
700 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000701 return false;
702 }
703
bsalomon@google.com471d4712011-08-23 15:45:25 +0000704 if (0 == width && target->willUseHWAALines()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000705 return false;
706 }
707
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000708 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000709 return false;
710 }
711
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000712 if (NULL != matrix &&
bsalomon@google.com205d4602011-04-25 12:43:45 +0000713 !matrix->preservesAxisAlignment()) {
714 return false;
715 }
716
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000717 *combinedMatrix = drawState.getViewMatrix();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000718 if (NULL != matrix) {
719 combinedMatrix->preConcat(*matrix);
720 GrAssert(combinedMatrix->preservesAxisAlignment());
721 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000722
bsalomon@google.com205d4602011-04-25 12:43:45 +0000723 combinedMatrix->mapRect(devRect, rect);
724 devRect->sort();
725
726 if (width < 0) {
reed@google.com20efde72011-05-09 17:00:02 +0000727 return !isIRect(*devRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000728 } else {
729 return true;
730 }
731}
732
bsalomon@google.com27847de2011-02-22 20:59:41 +0000733void GrContext::drawRect(const GrPaint& paint,
734 const GrRect& rect,
735 GrScalar width,
736 const GrMatrix* matrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000737 SK_TRACE_EVENT0("GrContext::drawRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000738
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000739 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000740 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000741
bsalomon@google.com205d4602011-04-25 12:43:45 +0000742 GrRect devRect = rect;
743 GrMatrix combinedMatrix;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000744 bool useVertexCoverage;
bsalomon@google.com289533a2011-10-27 12:34:25 +0000745 bool needAA = paint.fAntiAlias &&
746 !this->getRenderTarget()->isMultisampled();
747 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix,
748 &combinedMatrix, &devRect,
749 &useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000750
751 if (doAA) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000752 GrDrawTarget::AutoDeviceCoordDraw adcd(target);
753 if (!adcd.succeeded()) {
754 return;
755 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000756 if (width >= 0) {
757 GrVec strokeSize;;
758 if (width > 0) {
759 strokeSize.set(width, width);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000760 combinedMatrix.mapVectors(&strokeSize, 1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000761 strokeSize.setAbs(strokeSize);
762 } else {
763 strokeSize.set(GR_Scalar1, GR_Scalar1);
764 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000765 fAARectRenderer->strokeAARect(this->getGpu(), target, devRect,
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000766 strokeSize, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000767 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000768 fAARectRenderer->fillAARect(this->getGpu(), target,
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000769 devRect, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000770 }
771 return;
772 }
773
bsalomon@google.com27847de2011-02-22 20:59:41 +0000774 if (width >= 0) {
775 // TODO: consider making static vertex buffers for these cases.
776 // Hairline could be done by just adding closing vertex to
777 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000778
bsalomon@google.com27847de2011-02-22 20:59:41 +0000779 static const int worstCaseVertCount = 10;
bsalomon@google.come3d32162012-07-20 13:37:06 +0000780 GrDrawTarget::AutoReleaseGeometry geo(target, 0, worstCaseVertCount, 0);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000781
782 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000783 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000784 return;
785 }
786
787 GrPrimitiveType primType;
788 int vertCount;
789 GrPoint* vertex = geo.positions();
790
791 if (width > 0) {
792 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000793 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000794 setStrokeRectStrip(vertex, rect, width);
795 } else {
796 // hairline
797 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000798 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000799 vertex[0].set(rect.fLeft, rect.fTop);
800 vertex[1].set(rect.fRight, rect.fTop);
801 vertex[2].set(rect.fRight, rect.fBottom);
802 vertex[3].set(rect.fLeft, rect.fBottom);
803 vertex[4].set(rect.fLeft, rect.fTop);
804 }
805
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000806 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000807 if (NULL != matrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000808 GrDrawState* drawState = target->drawState();
809 avmr.set(drawState);
810 drawState->preConcatViewMatrix(*matrix);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000811 drawState->preConcatSamplerMatrices(*matrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000812 }
813
814 target->drawNonIndexed(primType, 0, vertCount);
815 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000816#if GR_STATIC_RECT_VB
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000817 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
818 if (NULL == sqVB) {
819 GrPrintf("Failed to create static rect vb.\n");
820 return;
821 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000822 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000823 GrDrawState* drawState = target->drawState();
824 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000825 GrMatrix m;
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000826 m.setAll(rect.width(), 0, rect.fLeft,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000827 0, rect.height(), rect.fTop,
828 0, 0, GrMatrix::I()[8]);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000829
830 if (NULL != matrix) {
831 m.postConcat(*matrix);
832 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000833 drawState->preConcatViewMatrix(m);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000834 drawState->preConcatSamplerMatrices(m);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000835
bsalomon@google.com47059542012-06-06 20:51:20 +0000836 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000837#else
bsalomon@google.come3d32162012-07-20 13:37:06 +0000838 target->drawSimpleRect(rect, matrix);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000839#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000840 }
841}
842
843void GrContext::drawRectToRect(const GrPaint& paint,
844 const GrRect& dstRect,
845 const GrRect& srcRect,
846 const GrMatrix* dstMatrix,
847 const GrMatrix* srcMatrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000848 SK_TRACE_EVENT0("GrContext::drawRectToRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000849
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000850 // srcRect refers to paint's first texture
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000851 if (!paint.isTextureStageEnabled(0)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000852 drawRect(paint, dstRect, -1, dstMatrix);
853 return;
854 }
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000855
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000856 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000857
858#if GR_STATIC_RECT_VB
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000859 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000860 GrDrawState* drawState = target->drawState();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000861 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000862
863 GrMatrix m;
864
865 m.setAll(dstRect.width(), 0, dstRect.fLeft,
866 0, dstRect.height(), dstRect.fTop,
867 0, 0, GrMatrix::I()[8]);
868 if (NULL != dstMatrix) {
869 m.postConcat(*dstMatrix);
870 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000871 drawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000872
bsalomon@google.come3d32162012-07-20 13:37:06 +0000873 // we explicitly setup the correct coords for the first stage. The others
874 // must know about the view matrix change.
875 for (int s = 1; s < GrPaint::kTotalStages; ++s) {
876 if (drawState->isStageEnabled(s)) {
877 drawState->sampler(s)->preConcatMatrix(m);
878 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000879 }
880
bsalomon@google.com27847de2011-02-22 20:59:41 +0000881 m.setAll(srcRect.width(), 0, srcRect.fLeft,
882 0, srcRect.height(), srcRect.fTop,
883 0, 0, GrMatrix::I()[8]);
884 if (NULL != srcMatrix) {
885 m.postConcat(*srcMatrix);
886 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000887 drawState->sampler(GrPaint::kFirstTextureStage)->preConcatMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000888
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000889 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
890 if (NULL == sqVB) {
891 GrPrintf("Failed to create static rect vb.\n");
892 return;
893 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000894 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com47059542012-06-06 20:51:20 +0000895 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000896#else
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000897 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000898
tomhudson@google.com93813632011-10-27 20:21:16 +0000899 const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
900 const GrMatrix* srcMatrices[GrDrawState::kNumStages] = {NULL};
bsalomon@google.com27847de2011-02-22 20:59:41 +0000901 srcRects[0] = &srcRect;
902 srcMatrices[0] = srcMatrix;
903
bsalomon@google.come3d32162012-07-20 13:37:06 +0000904 target->drawRect(dstRect, dstMatrix, srcRects, srcMatrices);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000905#endif
906}
907
908void GrContext::drawVertices(const GrPaint& paint,
909 GrPrimitiveType primitiveType,
910 int vertexCount,
911 const GrPoint positions[],
912 const GrPoint texCoords[],
913 const GrColor colors[],
914 const uint16_t indices[],
915 int indexCount) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000916 SK_TRACE_EVENT0("GrContext::drawVertices");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000917
918 GrDrawTarget::AutoReleaseGeometry geo;
919
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000920 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000921 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000922
bsalomon@google.come3d32162012-07-20 13:37:06 +0000923 GrVertexLayout layout = 0;
924 if (NULL != texCoords) {
925 layout |= GrDrawTarget::StageTexCoordVertexLayoutBit(0, 0);
926 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000927 if (NULL != colors) {
928 layout |= GrDrawTarget::kColor_VertexLayoutBit;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000929 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000930 int vertexSize = GrDrawTarget::VertexSize(layout);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000931
932 if (sizeof(GrPoint) != vertexSize) {
933 if (!geo.set(target, layout, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000934 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000935 return;
936 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000937 int texOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000938 int colorOffset;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000939 GrDrawTarget::VertexSizeAndOffsetsByIdx(layout,
940 texOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000941 &colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000942 NULL,
943 NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000944 void* curVertex = geo.vertices();
945
946 for (int i = 0; i < vertexCount; ++i) {
947 *((GrPoint*)curVertex) = positions[i];
948
949 if (texOffsets[0] > 0) {
950 *(GrPoint*)((intptr_t)curVertex + texOffsets[0]) = texCoords[i];
951 }
952 if (colorOffset > 0) {
953 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
954 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000955 curVertex = (void*)((intptr_t)curVertex + vertexSize);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000956 }
957 } else {
958 target->setVertexSourceToArray(layout, positions, vertexCount);
959 }
960
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000961 // we don't currently apply offscreen AA to this path. Need improved
bsalomon@google.com91958362011-06-13 17:58:13 +0000962 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000963
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000964 if (NULL != indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000965 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000966 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000967 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000968 target->drawNonIndexed(primitiveType, 0, vertexCount);
969 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000970}
971
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000972///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com150d2842012-01-12 20:19:56 +0000973namespace {
974
bsalomon@google.com93c96602012-04-27 13:05:21 +0000975struct CircleVertex {
976 GrPoint fPos;
977 GrPoint fCenter;
978 GrScalar fOuterRadius;
979 GrScalar fInnerRadius;
980};
981
982/* Returns true if will map a circle to another circle. This can be true
983 * if the matrix only includes square-scale, rotation, translation.
984 */
985inline bool isSimilarityTransformation(const SkMatrix& matrix,
986 SkScalar tol = SK_ScalarNearlyZero) {
987 if (matrix.isIdentity() || matrix.getType() == SkMatrix::kTranslate_Mask) {
988 return true;
989 }
990 if (matrix.hasPerspective()) {
991 return false;
992 }
993
994 SkScalar mx = matrix.get(SkMatrix::kMScaleX);
995 SkScalar sx = matrix.get(SkMatrix::kMSkewX);
996 SkScalar my = matrix.get(SkMatrix::kMScaleY);
997 SkScalar sy = matrix.get(SkMatrix::kMSkewY);
998
999 if (mx == 0 && sx == 0 && my == 0 && sy == 0) {
1000 return false;
1001 }
1002
1003 // it has scales or skews, but it could also be rotation, check it out.
1004 SkVector vec[2];
1005 vec[0].set(mx, sx);
1006 vec[1].set(sy, my);
1007
1008 return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) &&
1009 SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(),
1010 SkScalarSquare(tol));
1011}
1012
1013}
1014
1015// TODO: strokeWidth can't be larger than zero right now.
1016// It will be fixed when drawPath() can handle strokes.
1017void GrContext::drawOval(const GrPaint& paint,
1018 const GrRect& rect,
1019 SkScalar strokeWidth) {
bsalomon@google.com0982d352012-07-31 15:33:25 +00001020 GrAssert(strokeWidth <= 0);
1021 if (!isSimilarityTransformation(this->getMatrix()) ||
bsalomon@google.com93c96602012-04-27 13:05:21 +00001022 !paint.fAntiAlias ||
1023 rect.height() != rect.width()) {
1024 SkPath path;
1025 path.addOval(rect);
1026 GrPathFill fill = (strokeWidth == 0) ?
bsalomon@google.com0982d352012-07-31 15:33:25 +00001027 kHairLine_GrPathFill : kWinding_GrPathFill;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001028 this->internalDrawPath(paint, path, fill, NULL);
1029 return;
1030 }
1031
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001032 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
1033
bsalomon@google.com0982d352012-07-31 15:33:25 +00001034 GrDrawState* drawState = target->drawState();
1035 GrDrawState::AutoStageDisable atr(fDrawState);
1036 const GrMatrix vm = drawState->getViewMatrix();
1037
bsalomon@google.com93c96602012-04-27 13:05:21 +00001038 const GrRenderTarget* rt = drawState->getRenderTarget();
1039 if (NULL == rt) {
1040 return;
1041 }
1042
bsalomon@google.come3d32162012-07-20 13:37:06 +00001043 GrDrawTarget::AutoDeviceCoordDraw adcd(target);
1044 if (!adcd.succeeded()) {
1045 return;
1046 }
bsalomon@google.com93c96602012-04-27 13:05:21 +00001047
bsalomon@google.come3d32162012-07-20 13:37:06 +00001048 GrVertexLayout layout = GrDrawTarget::kEdge_VertexLayoutBit;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001049 GrAssert(sizeof(CircleVertex) == GrDrawTarget::VertexSize(layout));
1050
1051 GrPoint center = GrPoint::Make(rect.centerX(), rect.centerY());
1052 GrScalar radius = SkScalarHalf(rect.width());
1053
1054 vm.mapPoints(&center, 1);
1055 radius = vm.mapRadius(radius);
1056
1057 GrScalar outerRadius = radius;
1058 GrScalar innerRadius = 0;
1059 SkScalar halfWidth = 0;
1060 if (strokeWidth == 0) {
1061 halfWidth = SkScalarHalf(SK_Scalar1);
1062
1063 outerRadius += halfWidth;
1064 innerRadius = SkMaxScalar(0, radius - halfWidth);
1065 }
1066
1067 GrDrawTarget::AutoReleaseGeometry geo(target, layout, 4, 0);
1068 if (!geo.succeeded()) {
1069 GrPrintf("Failed to get space for vertices!\n");
1070 return;
1071 }
1072
1073 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
1074
robertphillips@google.coma0a66c12012-06-22 13:14:29 +00001075 // The fragment shader will extend the radius out half a pixel
1076 // to antialias. Expand the drawn rect here so all the pixels
1077 // will be captured.
1078 SkScalar L = center.fX - outerRadius - SkFloatToScalar(0.5f);
1079 SkScalar R = center.fX + outerRadius + SkFloatToScalar(0.5f);
1080 SkScalar T = center.fY - outerRadius - SkFloatToScalar(0.5f);
1081 SkScalar B = center.fY + outerRadius + SkFloatToScalar(0.5f);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001082
1083 verts[0].fPos = SkPoint::Make(L, T);
1084 verts[1].fPos = SkPoint::Make(R, T);
1085 verts[2].fPos = SkPoint::Make(L, B);
1086 verts[3].fPos = SkPoint::Make(R, B);
1087
1088 for (int i = 0; i < 4; ++i) {
1089 // this goes to fragment shader, it should be in y-points-up space.
1090 verts[i].fCenter = SkPoint::Make(center.fX, rt->height() - center.fY);
1091
1092 verts[i].fOuterRadius = outerRadius;
1093 verts[i].fInnerRadius = innerRadius;
1094 }
1095
1096 drawState->setVertexEdgeType(GrDrawState::kCircle_EdgeType);
bsalomon@google.com47059542012-06-06 20:51:20 +00001097 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001098}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001099
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001100void GrContext::drawPath(const GrPaint& paint, const SkPath& path,
reed@google.com07f3ee12011-05-16 17:21:57 +00001101 GrPathFill fill, const GrPoint* translate) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001102
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001103 if (path.isEmpty()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001104 if (GrIsFillInverted(fill)) {
1105 this->drawPaint(paint);
1106 }
1107 return;
1108 }
1109
bsalomon@google.com93c96602012-04-27 13:05:21 +00001110 SkRect ovalRect;
1111 if (!GrIsFillInverted(fill) && path.isOval(&ovalRect)) {
1112 if (translate) {
1113 ovalRect.offset(*translate);
1114 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001115 SkScalar width = (fill == kHairLine_GrPathFill) ? 0 : -SK_Scalar1;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001116 this->drawOval(paint, ovalRect, width);
1117 return;
1118 }
1119
1120 internalDrawPath(paint, path, fill, translate);
1121}
1122
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001123void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +00001124 GrPathFill fill, const GrPoint* translate) {
1125
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001126 // Note that below we may sw-rasterize the path into a scratch texture.
1127 // Scratch textures can be recycled after they are returned to the texture
1128 // cache. This presents a potential hazard for buffered drawing. However,
1129 // the writePixels that uploads to the scratch will perform a flush so we're
1130 // OK.
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001131 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +00001132 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001133
bsalomon@google.com289533a2011-10-27 12:34:25 +00001134 bool prAA = paint.fAntiAlias && !this->getRenderTarget()->isMultisampled();
1135
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001136 // An Assumption here is that path renderer would use some form of tweaking
1137 // the src color (either the input alpha or in the frag shader) to implement
1138 // aa. If we have some future driver-mojo path AA that can do the right
1139 // thing WRT to the blend then we'll need some query on the PR.
1140 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001141#if GR_DEBUG
bsalomon@google.com979432b2011-11-05 21:38:22 +00001142 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001143#endif
bsalomon@google.com289533a2011-10-27 12:34:25 +00001144 prAA = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001145 }
bsalomon@google.com289533a2011-10-27 12:34:25 +00001146
robertphillips@google.com72176b22012-05-23 13:19:12 +00001147 GrPathRenderer* pr = this->getPathRenderer(path, fill, target, prAA, true);
bsalomon@google.com30085192011-08-19 15:42:31 +00001148 if (NULL == pr) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001149#if GR_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001150 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001151#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001152 return;
1153 }
1154
bsalomon@google.come3d32162012-07-20 13:37:06 +00001155 pr->drawPath(path, fill, translate, target, prAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001156}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001157
bsalomon@google.com27847de2011-02-22 20:59:41 +00001158////////////////////////////////////////////////////////////////////////////////
1159
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001160void GrContext::flush(int flagsBitfield) {
1161 if (kDiscard_FlushBit & flagsBitfield) {
1162 fDrawBuffer->reset();
1163 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001164 this->flushDrawBuffer();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001165 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001166 if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001167 fGpu->forceRenderTargetFlush();
1168 }
1169}
1170
bsalomon@google.com27847de2011-02-22 20:59:41 +00001171void GrContext::flushDrawBuffer() {
junov@google.com53a55842011-06-08 22:55:10 +00001172 if (fDrawBuffer) {
robertphillips@google.com58b38182012-05-03 16:29:41 +00001173 // With addition of the AA clip path, flushing the draw buffer can
1174 // result in the generation of an AA clip mask. During this
1175 // process the SW path renderer may be invoked which recusively
1176 // calls this method (via internalWriteTexturePixels) creating
1177 // infinite recursion
1178 GrInOrderDrawBuffer* temp = fDrawBuffer;
1179 fDrawBuffer = NULL;
1180
1181 temp->flushTo(fGpu);
1182
1183 fDrawBuffer = temp;
junov@google.com53a55842011-06-08 22:55:10 +00001184 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001185}
1186
bsalomon@google.com0342a852012-08-20 19:22:38 +00001187void GrContext::writeTexturePixels(GrTexture* texture,
1188 int left, int top, int width, int height,
1189 GrPixelConfig config, const void* buffer, size_t rowBytes,
1190 uint32_t flags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001191 SK_TRACE_EVENT0("GrContext::writeTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001192 ASSERT_OWNED_RESOURCE(texture);
1193
bsalomon@google.com0342a852012-08-20 19:22:38 +00001194 // TODO: use scratch texture to perform conversion
1195 if (kUnpremul_PixelOpsFlag & flags) {
1196 return;
1197 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001198 if (!(kDontFlush_PixelOpsFlag & flags)) {
1199 this->flush();
1200 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001201
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001202 fGpu->writeTexturePixels(texture, left, top, width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +00001203 config, buffer, rowBytes);
1204}
1205
bsalomon@google.com0342a852012-08-20 19:22:38 +00001206bool GrContext::readTexturePixels(GrTexture* texture,
1207 int left, int top, int width, int height,
1208 GrPixelConfig config, void* buffer, size_t rowBytes,
1209 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001210 SK_TRACE_EVENT0("GrContext::readTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001211 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001212
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001213 // TODO: code read pixels for textures that aren't also rendertargets
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001214 GrRenderTarget* target = texture->asRenderTarget();
1215 if (NULL != target) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001216 return this->readRenderTargetPixels(target,
1217 left, top, width, height,
1218 config, buffer, rowBytes,
1219 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001220 } else {
1221 return false;
1222 }
1223}
1224
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001225#include "SkConfig8888.h"
1226
1227namespace {
1228/**
1229 * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel
1230 * formats are representable as Config8888 and so the function returns false
1231 * if the GrPixelConfig has no equivalent Config8888.
1232 */
1233bool grconfig_to_config8888(GrPixelConfig config,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001234 bool unpremul,
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001235 SkCanvas::Config8888* config8888) {
1236 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001237 case kRGBA_8888_GrPixelConfig:
1238 if (unpremul) {
1239 *config8888 = SkCanvas::kRGBA_Unpremul_Config8888;
1240 } else {
1241 *config8888 = SkCanvas::kRGBA_Premul_Config8888;
1242 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001243 return true;
bsalomon@google.com0342a852012-08-20 19:22:38 +00001244 case kBGRA_8888_GrPixelConfig:
1245 if (unpremul) {
1246 *config8888 = SkCanvas::kBGRA_Unpremul_Config8888;
1247 } else {
1248 *config8888 = SkCanvas::kBGRA_Premul_Config8888;
1249 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001250 return true;
1251 default:
1252 return false;
1253 }
1254}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001255
1256// It returns a configuration with where the byte position of the R & B components are swapped in
1257// relation to the input config. This should only be called with the result of
1258// grconfig_to_config8888 as it will fail for other configs.
1259SkCanvas::Config8888 swap_config8888_red_and_blue(SkCanvas::Config8888 config8888) {
1260 switch (config8888) {
1261 case SkCanvas::kBGRA_Premul_Config8888:
1262 return SkCanvas::kRGBA_Premul_Config8888;
1263 case SkCanvas::kBGRA_Unpremul_Config8888:
1264 return SkCanvas::kRGBA_Unpremul_Config8888;
1265 case SkCanvas::kRGBA_Premul_Config8888:
1266 return SkCanvas::kBGRA_Premul_Config8888;
1267 case SkCanvas::kRGBA_Unpremul_Config8888:
1268 return SkCanvas::kBGRA_Unpremul_Config8888;
1269 default:
1270 GrCrash("Unexpected input");
1271 return SkCanvas::kBGRA_Unpremul_Config8888;;
1272 }
1273}
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001274}
1275
bsalomon@google.com0342a852012-08-20 19:22:38 +00001276bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
1277 int left, int top, int width, int height,
1278 GrPixelConfig config, void* buffer, size_t rowBytes,
1279 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001280 SK_TRACE_EVENT0("GrContext::readRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001281 ASSERT_OWNED_RESOURCE(target);
1282
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001283 if (NULL == target) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001284 target = fDrawState->getRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001285 if (NULL == target) {
1286 return false;
1287 }
1288 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001289
bsalomon@google.com6f379512011-11-16 20:36:03 +00001290 if (!(kDontFlush_PixelOpsFlag & flags)) {
1291 this->flush();
1292 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001293
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001294 // Determine which conversions have to be applied: flipY, swapRAnd, and/or unpremul.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001295
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001296 // If fGpu->readPixels would incur a y-flip cost then we will read the pixels upside down. We'll
1297 // either do the flipY by drawing into a scratch with a matrix or on the cpu after the read.
1298 bool flipY = fGpu->readPixelsWillPayForYFlip(target, left, top,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001299 width, height, config,
1300 rowBytes);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001301 bool swapRAndB = fGpu->preferredReadPixelsConfig(config) == GrPixelConfigSwapRAndB(config);
1302
bsalomon@google.com0342a852012-08-20 19:22:38 +00001303 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001304
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001305 // flipY will get set to false when it is handled below using a scratch. However, in that case
1306 // we still want to do the read upside down.
1307 bool readUpsideDown = flipY;
1308
1309 if (unpremul && kRGBA_8888_GrPixelConfig != config && kBGRA_8888_GrPixelConfig != config) {
1310 // The unpremul flag is only allowed for these two configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001311 return false;
1312 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001313
1314 GrPixelConfig readConfig;
1315 if (swapRAndB) {
1316 readConfig = GrPixelConfigSwapRAndB(config);
1317 GrAssert(kUnknown_GrPixelConfig != config);
1318 } else {
1319 readConfig = config;
1320 }
1321
1322 // If the src is a texture and we would have to do conversions after read pixels, we instead
1323 // do the conversions by drawing the src to a scratch texture. If we handle any of the
1324 // conversions in the draw we set the corresponding bool to false so that we don't reapply it
1325 // on the read back pixels.
1326 GrTexture* src = target->asTexture();
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001327 GrAutoScratchTexture ast;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001328 if (NULL != src && (swapRAndB || unpremul || flipY)) {
1329 // Make the scratch a render target because we don't have a robust readTexturePixels as of
1330 // yet. It calls this function.
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001331 GrTextureDesc desc;
1332 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1333 desc.fWidth = width;
1334 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001335 desc.fConfig = readConfig;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001336
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001337 // When a full readback is faster than a partial we could always make the scratch exactly
1338 // match the passed rect. However, if we see many different size rectangles we will trash
1339 // our texture cache and pay the cost of creating and destroying many textures. So, we only
1340 // request an exact match when the caller is reading an entire RT.
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001341 ScratchTexMatch match = kApprox_ScratchTexMatch;
1342 if (0 == left &&
1343 0 == top &&
1344 target->width() == width &&
1345 target->height() == height &&
1346 fGpu->fullReadPixelsIsFasterThanPartial()) {
1347 match = kExact_ScratchTexMatch;
1348 }
1349 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001350 GrTexture* texture = ast.texture();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001351 if (texture) {
1352 SkAutoTUnref<GrCustomStage> stage;
1353 if (unpremul) {
1354 stage.reset(this->createPMToUPMEffect(src, swapRAndB));
1355 }
1356 // If we failed to create a PM->UPM effect and have no other conversions to perform then
1357 // there is no longer any point to using the scratch.
1358 if (NULL != stage || flipY || swapRAndB) {
1359 if (NULL == stage) {
1360 stage.reset(GrConfigConversionEffect::Create(src, swapRAndB));
1361 GrAssert(NULL != stage);
1362 } else {
1363 unpremul = false; // we will handle the UPM conversion in the draw
1364 }
1365 swapRAndB = false; // we will handle the swap in the draw.
bsalomon@google.comc4364992011-11-07 15:54:49 +00001366
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001367 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
1368 GrDrawState* drawState = fGpu->drawState();
1369 drawState->setRenderTarget(texture->asRenderTarget());
1370 GrMatrix matrix;
1371 if (flipY) {
1372 matrix.setTranslate(SK_Scalar1 * left,
1373 SK_Scalar1 * (top + height));
1374 matrix.set(GrMatrix::kMScaleY, -GR_Scalar1);
1375 flipY = false; // the y flip will be handled in the draw
1376 } else {
1377 matrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
1378 }
1379 matrix.postIDiv(src->width(), src->height());
1380 drawState->sampler(0)->reset(matrix);
1381 drawState->sampler(0)->setCustomStage(stage);
1382 GrRect rect = GrRect::MakeWH(GrIntToScalar(width), GrIntToScalar(height));
1383 fGpu->drawSimpleRect(rect, NULL);
1384 // we want to read back from the scratch's origin
1385 left = 0;
1386 top = 0;
1387 target = texture->asRenderTarget();
1388 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001389 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001390 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001391 if (!fGpu->readPixels(target,
1392 left, top, width, height,
1393 readConfig, buffer, rowBytes, readUpsideDown)) {
1394 return false;
1395 }
1396 // Perform any conversions we weren't able to perfom using a scratch texture.
1397 if (unpremul || swapRAndB || flipY) {
1398 SkCanvas::Config8888 srcC8888;
1399 SkCanvas::Config8888 dstC8888;
1400 bool c8888IsValid = grconfig_to_config8888(config, false, &srcC8888);
1401 grconfig_to_config8888(config, unpremul, &dstC8888);
1402 if (swapRAndB) {
1403 GrAssert(c8888IsValid); // we should only do r/b swap on 8888 configs
1404 srcC8888 = swap_config8888_red_and_blue(srcC8888);
1405 }
1406 if (flipY) {
1407 size_t tightRB = width * GrBytesPerPixel(config);
1408 if (0 == rowBytes) {
1409 rowBytes = tightRB;
1410 }
1411 SkAutoSTMalloc<256, uint8_t> tempRow(tightRB);
1412 intptr_t top = reinterpret_cast<intptr_t>(buffer);
1413 intptr_t bot = top + (height - 1) * rowBytes;
1414 while (top < bot) {
1415 uint32_t* t = reinterpret_cast<uint32_t*>(top);
1416 uint32_t* b = reinterpret_cast<uint32_t*>(bot);
1417 uint32_t* temp = reinterpret_cast<uint32_t*>(tempRow.get());
1418 memcpy(temp, t, tightRB);
1419 if (c8888IsValid) {
1420 SkConvertConfig8888Pixels(t, tightRB, dstC8888,
1421 b, tightRB, srcC8888,
1422 width, 1);
1423 SkConvertConfig8888Pixels(b, tightRB, dstC8888,
1424 temp, tightRB, srcC8888,
1425 width, 1);
1426 } else {
1427 memcpy(t, b, tightRB);
1428 memcpy(b, temp, tightRB);
1429 }
1430 top += rowBytes;
1431 bot -= rowBytes;
1432 }
1433 // The above loop does nothing on the middle row when height is odd.
1434 if (top == bot && c8888IsValid && dstC8888 != srcC8888) {
1435 uint32_t* mid = reinterpret_cast<uint32_t*>(top);
1436 SkConvertConfig8888Pixels(mid, tightRB, dstC8888, mid, tightRB, srcC8888, width, 1);
1437 }
1438 } else {
1439 // if we aren't flipping Y then we have no reason to be here other than doing
1440 // conversions for 8888 (r/b swap or upm).
1441 GrAssert(c8888IsValid);
1442 uint32_t* b32 = reinterpret_cast<uint32_t*>(buffer);
1443 SkConvertConfig8888Pixels(b32, rowBytes, dstC8888,
1444 b32, rowBytes, srcC8888,
1445 width, height);
1446 }
1447 }
1448 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001449}
1450
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001451void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1452 GrAssert(target);
1453 ASSERT_OWNED_RESOURCE(target);
1454 // In the future we may track whether there are any pending draws to this
1455 // target. We don't today so we always perform a flush. We don't promise
1456 // this to our clients, though.
1457 this->flush();
1458 fGpu->resolveRenderTarget(target);
1459}
1460
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001461void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst) {
1462 if (NULL == src || NULL == dst) {
1463 return;
1464 }
1465 ASSERT_OWNED_RESOURCE(src);
1466
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001467 // Writes pending to the source texture are not tracked, so a flush
1468 // is required to ensure that the copy captures the most recent contents
1469 // of the source texture. See similar behaviour in
1470 // GrContext::resolveRenderTarget.
1471 this->flush();
1472
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001473 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001474 GrDrawState* drawState = fGpu->drawState();
1475 drawState->setRenderTarget(dst);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001476 GrMatrix sampleM;
1477 sampleM.setIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001478 drawState->sampler(0)->reset(sampleM);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001479 drawState->createTextureEffect(0, src);
bsalomon@google.com5db3b6c2012-01-12 20:38:57 +00001480 SkRect rect = SkRect::MakeXYWH(0, 0,
1481 SK_Scalar1 * src->width(),
1482 SK_Scalar1 * src->height());
bsalomon@google.come3d32162012-07-20 13:37:06 +00001483 fGpu->drawSimpleRect(rect, NULL);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001484}
1485
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001486void GrContext::writeRenderTargetPixels(GrRenderTarget* target,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001487 int left, int top, int width, int height,
1488 GrPixelConfig config,
1489 const void* buffer,
1490 size_t rowBytes,
1491 uint32_t flags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001492 SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001493 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001494
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001495 if (NULL == target) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001496 target = fDrawState->getRenderTarget();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001497 if (NULL == target) {
1498 return;
1499 }
1500 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001501
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001502 // TODO: when underlying api has a direct way to do this we should use it (e.g. glDrawPixels on
1503 // desktop GL).
1504
1505 // We will always call some form of writeTexturePixels and we will pass our flags on to it.
1506 // Thus, we don't perform a flush here since that call will do it (if the kNoFlush flag isn't
1507 // set.)
bsalomon@google.com27847de2011-02-22 20:59:41 +00001508
bsalomon@google.com0342a852012-08-20 19:22:38 +00001509 // If the RT is also a texture and we don't have to premultiply then take the texture path.
1510 // We expect to be at least as fast or faster since it doesn't use an intermediate texture as
1511 // we do below.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001512
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001513#if !GR_MAC_BUILD
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001514 // At least some drivers on the Mac get confused when glTexImage2D is called on a texture
1515 // attached to an FBO. The FBO still sees the old image. TODO: determine what OS versions and/or
1516 // HW is affected.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001517 if (NULL != target->asTexture() && !(kUnpremul_PixelOpsFlag & flags)) {
1518 this->writeTexturePixels(target->asTexture(),
1519 left, top, width, height,
1520 config, buffer, rowBytes, flags);
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001521 return;
1522 }
1523#endif
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001524 SkAutoTUnref<GrCustomStage> stage;
1525 bool swapRAndB = (fGpu->preferredReadPixelsConfig(config) == GrPixelConfigSwapRAndB(config));
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001526
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001527 GrPixelConfig textureConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001528 if (swapRAndB) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001529 textureConfig = GrPixelConfigSwapRAndB(config);
1530 } else {
1531 textureConfig = config;
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001532 }
1533
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001534 GrTextureDesc desc;
1535 desc.fWidth = width;
1536 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001537 desc.fConfig = textureConfig;
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001538 GrAutoScratchTexture ast(this, desc);
1539 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001540 if (NULL == texture) {
1541 return;
1542 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001543 // allocate a tmp buffer and sw convert the pixels to premul
1544 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
1545
1546 if (kUnpremul_PixelOpsFlag & flags) {
1547 if (kRGBA_8888_GrPixelConfig != config && kBGRA_8888_GrPixelConfig != config) {
1548 return;
1549 }
1550 stage.reset(this->createUPMToPMEffect(texture, swapRAndB));
1551 if (NULL == stage) {
1552 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1553 GR_DEBUGCODE(bool success = )
1554 grconfig_to_config8888(config, true, &srcConfig8888);
1555 GrAssert(success);
1556 GR_DEBUGCODE(success = )
1557 grconfig_to_config8888(config, false, &dstConfig8888);
1558 GrAssert(success);
1559 const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer);
1560 tmpPixels.reset(width * height);
1561 SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888,
1562 src, rowBytes, srcConfig8888,
1563 width, height);
1564 buffer = tmpPixels.get();
1565 rowBytes = 4 * width;
1566 }
1567 }
1568 if (NULL == stage) {
1569 stage.reset(GrConfigConversionEffect::Create(texture, swapRAndB));
1570 GrAssert(NULL != stage);
1571 }
1572
1573 this->writeTexturePixels(texture,
1574 0, 0, width, height,
1575 textureConfig, buffer, rowBytes,
1576 flags & ~kUnpremul_PixelOpsFlag);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001577
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001578 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001579 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001580
1581 GrMatrix matrix;
1582 matrix.setTranslate(GrIntToScalar(left), GrIntToScalar(top));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001583 drawState->setViewMatrix(matrix);
1584 drawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001585
bsalomon@google.com5c638652011-07-18 19:31:59 +00001586 matrix.setIDiv(texture->width(), texture->height());
bsalomon@google.comb8670992012-07-25 21:27:09 +00001587 drawState->sampler(0)->reset(matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001588 drawState->sampler(0)->setCustomStage(stage);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001589
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001590 fGpu->drawSimpleRect(GrRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)), NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001591}
1592////////////////////////////////////////////////////////////////////////////////
1593
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001594void GrContext::setPaint(const GrPaint& paint) {
tomhudson@google.comcb325ce2012-07-11 14:41:19 +00001595 GrAssert(fDrawState->stagesDisabled());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001596
1597 for (int i = 0; i < GrPaint::kMaxTextures; ++i) {
1598 int s = i + GrPaint::kFirstTextureStage;
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001599 if (paint.isTextureStageEnabled(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001600 *fDrawState->sampler(s) = paint.getTextureSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001601 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001602 }
1603
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001604 fDrawState->setFirstCoverageStage(GrPaint::kFirstMaskStage);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001605
1606 for (int i = 0; i < GrPaint::kMaxMasks; ++i) {
1607 int s = i + GrPaint::kFirstMaskStage;
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001608 if (paint.isMaskStageEnabled(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001609 *fDrawState->sampler(s) = paint.getMaskSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001610 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001611 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001612
bsalomon@google.com26936d02012-03-19 13:06:19 +00001613 // disable all stages not accessible via the paint
1614 for (int s = GrPaint::kTotalStages; s < GrDrawState::kNumStages; ++s) {
tomhudson@google.com676e6602012-07-10 17:21:48 +00001615 fDrawState->disableStage(s);
bsalomon@google.com26936d02012-03-19 13:06:19 +00001616 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001617
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001618 fDrawState->setColor(paint.fColor);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001619
1620 if (paint.fDither) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001621 fDrawState->enableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001622 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001623 fDrawState->disableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001624 }
1625 if (paint.fAntiAlias) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001626 fDrawState->enableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001627 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001628 fDrawState->disableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001629 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001630 if (paint.fColorMatrixEnabled) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001631 fDrawState->enableState(GrDrawState::kColorMatrix_StateBit);
1632 fDrawState->setColorMatrix(paint.fColorMatrix);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001633 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001634 fDrawState->disableState(GrDrawState::kColorMatrix_StateBit);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001635 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001636 fDrawState->setBlendFunc(paint.fSrcBlendCoeff, paint.fDstBlendCoeff);
1637 fDrawState->setColorFilter(paint.fColorFilterColor, paint.fColorFilterXfermode);
1638 fDrawState->setCoverage(paint.fCoverage);
reed@google.com4b2d3f32012-05-15 18:05:50 +00001639#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
bsalomon@google.come3d32162012-07-20 13:37:06 +00001640 if ((paint.hasMask() || 0xff != paint.fCoverage) &&
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001641 !fGpu->canApplyCoverage()) {
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001642 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1643 }
bsalomon@google.com95cd7bd2012-03-28 15:35:05 +00001644#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001645}
1646
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001647GrDrawTarget* GrContext::prepareToDraw(const GrPaint* paint, BufferedDraw buffered) {
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001648 if (kNo_BufferedDraw == buffered && kYes_BufferedDraw == fLastDrawWasBuffered) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001649 this->flushDrawBuffer();
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001650 fLastDrawWasBuffered = kNo_BufferedDraw;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001651 }
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001652 if (NULL != paint) {
1653 this->setPaint(*paint);
1654 }
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001655 if (kYes_BufferedDraw == buffered) {
1656 fDrawBuffer->setClip(fGpu->getClip());
1657 fLastDrawWasBuffered = kYes_BufferedDraw;
1658 return fDrawBuffer;
1659 } else {
1660 GrAssert(kNo_BufferedDraw == buffered);
1661 return fGpu;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001662 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001663}
1664
robertphillips@google.com72176b22012-05-23 13:19:12 +00001665/*
1666 * This method finds a path renderer that can draw the specified path on
1667 * the provided target.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001668 * Due to its expense, the software path renderer has split out so it can
robertphillips@google.com72176b22012-05-23 13:19:12 +00001669 * can be individually allowed/disallowed via the "allowSW" boolean.
1670 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001671GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
bsalomon@google.com289533a2011-10-27 12:34:25 +00001672 GrPathFill fill,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001673 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001674 bool antiAlias,
1675 bool allowSW) {
bsalomon@google.com30085192011-08-19 15:42:31 +00001676 if (NULL == fPathRendererChain) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001677 fPathRendererChain =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001678 SkNEW_ARGS(GrPathRendererChain,
1679 (this, GrPathRendererChain::kNone_UsageFlag));
bsalomon@google.com30085192011-08-19 15:42:31 +00001680 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001681
1682 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path, fill,
1683 target,
1684 antiAlias);
1685
1686 if (NULL == pr && allowSW) {
1687 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001688 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001689 }
1690
1691 pr = fSoftwarePathRenderer;
1692 }
1693
1694 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001695}
1696
bsalomon@google.com27847de2011-02-22 20:59:41 +00001697////////////////////////////////////////////////////////////////////////////////
1698
bsalomon@google.com27847de2011-02-22 20:59:41 +00001699void GrContext::setRenderTarget(GrRenderTarget* target) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001700 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001701 fDrawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001702}
1703
1704GrRenderTarget* GrContext::getRenderTarget() {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001705 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001706}
1707
1708const GrRenderTarget* GrContext::getRenderTarget() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001709 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001710}
1711
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001712bool GrContext::isConfigRenderable(GrPixelConfig config) const {
1713 return fGpu->isConfigRenderable(config);
1714}
1715
bsalomon@google.com27847de2011-02-22 20:59:41 +00001716const GrMatrix& GrContext::getMatrix() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001717 return fDrawState->getViewMatrix();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001718}
1719
1720void GrContext::setMatrix(const GrMatrix& m) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001721 fDrawState->setViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001722}
1723
1724void GrContext::concatMatrix(const GrMatrix& m) const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001725 fDrawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001726}
1727
1728static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
1729 intptr_t mask = 1 << shift;
1730 if (pred) {
1731 bits |= mask;
1732 } else {
1733 bits &= ~mask;
1734 }
1735 return bits;
1736}
1737
bsalomon@google.com583a1e32011-08-17 13:42:46 +00001738GrContext::GrContext(GrGpu* gpu) {
bsalomon@google.comc0af3172012-06-15 14:10:09 +00001739 ++THREAD_INSTANCE_COUNT;
1740
bsalomon@google.com27847de2011-02-22 20:59:41 +00001741 fGpu = gpu;
1742 fGpu->ref();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001743 fGpu->setContext(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001744
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001745 fDrawState = SkNEW(GrDrawState);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001746 fGpu->setDrawState(fDrawState);
1747
bsalomon@google.com30085192011-08-19 15:42:31 +00001748 fPathRendererChain = NULL;
robertphillips@google.com72176b22012-05-23 13:19:12 +00001749 fSoftwarePathRenderer = NULL;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001750
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001751 fTextureCache = SkNEW_ARGS(GrResourceCache,
1752 (MAX_TEXTURE_CACHE_COUNT,
1753 MAX_TEXTURE_CACHE_BYTES));
1754 fFontCache = SkNEW_ARGS(GrFontCache, (fGpu));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001755
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001756 fLastDrawWasBuffered = kNo_BufferedDraw;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001757
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001758 fDrawBuffer = NULL;
1759 fDrawBufferVBAllocPool = NULL;
1760 fDrawBufferIBAllocPool = NULL;
1761
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001762 fAARectRenderer = SkNEW(GrAARectRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001763
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001764 fDidTestPMConversions = false;
1765
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001766 this->setupDrawBuffer();
1767}
1768
1769void GrContext::setupDrawBuffer() {
1770
1771 GrAssert(NULL == fDrawBuffer);
1772 GrAssert(NULL == fDrawBufferVBAllocPool);
1773 GrAssert(NULL == fDrawBufferIBAllocPool);
1774
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001775 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001776 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001777 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001778 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001779 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001780 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001781 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001782 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001783
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001784 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com471d4712011-08-23 15:45:25 +00001785 fDrawBufferVBAllocPool,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001786 fDrawBufferIBAllocPool));
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001787
bsalomon@google.com27847de2011-02-22 20:59:41 +00001788 fDrawBuffer->setQuadIndexBuffer(this->getQuadIndexBuffer());
bsalomon@google.com1015e032012-06-25 18:41:04 +00001789 if (fDrawBuffer) {
1790 fDrawBuffer->setAutoFlushTarget(fGpu);
1791 fDrawBuffer->setDrawState(fDrawState);
1792 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001793}
1794
bsalomon@google.com27847de2011-02-22 20:59:41 +00001795GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) {
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001796 return prepareToDraw(&paint, DEFAULT_BUFFERING);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001797}
1798
1799const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1800 return fGpu->getQuadIndexBuffer();
1801}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001802
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001803namespace {
1804void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
1805 GrConfigConversionEffect::PMConversion pmToUPM;
1806 GrConfigConversionEffect::PMConversion upmToPM;
1807 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
1808 *pmToUPMValue = pmToUPM;
1809 *upmToPMValue = upmToPM;
1810}
1811}
1812
1813GrCustomStage* GrContext::createPMToUPMEffect(GrTexture* texture, bool swapRAndB) {
1814 if (!fDidTestPMConversions) {
1815 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001816 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001817 }
1818 GrConfigConversionEffect::PMConversion pmToUPM =
1819 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
1820 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
1821 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM);
1822 } else {
1823 return NULL;
1824 }
1825}
1826
1827GrCustomStage* GrContext::createUPMToPMEffect(GrTexture* texture, bool swapRAndB) {
1828 if (!fDidTestPMConversions) {
1829 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001830 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001831 }
1832 GrConfigConversionEffect::PMConversion upmToPM =
1833 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
1834 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
1835 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM);
1836 } else {
1837 return NULL;
1838 }
1839}
1840
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001841GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture,
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001842 bool canClobberSrc,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001843 const SkRect& rect,
1844 float sigmaX, float sigmaY) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001845 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001846 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001847 AutoMatrix avm(this, GrMatrix::I());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001848 SkIRect clearRect;
bsalomon@google.comb505a122012-05-31 18:40:36 +00001849 int scaleFactorX, radiusX;
1850 int scaleFactorY, radiusY;
1851 sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX);
1852 sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY);
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001853
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001854 SkRect srcRect(rect);
1855 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
1856 srcRect.roundOut();
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001857 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001858 static_cast<float>(scaleFactorY));
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001859
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001860 AutoClip acs(this, srcRect);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001861
bsalomon@google.com0342a852012-08-20 19:22:38 +00001862 GrAssert(kBGRA_8888_GrPixelConfig == srcTexture->config() ||
1863 kRGBA_8888_GrPixelConfig == srcTexture->config() ||
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001864 kAlpha_8_GrPixelConfig == srcTexture->config());
1865
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001866 GrTextureDesc desc;
1867 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1868 desc.fWidth = SkScalarFloorToInt(srcRect.width());
1869 desc.fHeight = SkScalarFloorToInt(srcRect.height());
1870 desc.fConfig = srcTexture->config();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001871
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001872 GrAutoScratchTexture temp1, temp2;
1873 GrTexture* dstTexture = temp1.set(this, desc);
1874 GrTexture* tempTexture = canClobberSrc ? srcTexture : temp2.set(this, desc);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001875
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001876 GrPaint paint;
1877 paint.reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +00001878 paint.textureSampler(0)->textureParams()->setBilerp(true);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001879
1880 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
1881 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1882 srcTexture->height());
1883 this->setRenderTarget(dstTexture->asRenderTarget());
1884 SkRect dstRect(srcRect);
1885 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
1886 i < scaleFactorY ? 0.5f : 1.0f);
tomhudson@google.comaa72eab2012-07-19 18:01:07 +00001887 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
1888 (srcTexture)))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001889 this->drawRectToRect(paint, dstRect, srcRect);
1890 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001891 srcTexture = dstTexture;
1892 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001893 }
1894
robertphillips@google.com7a396332012-05-10 15:11:27 +00001895 SkIRect srcIRect;
1896 srcRect.roundOut(&srcIRect);
1897
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001898 if (sigmaX > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001899 if (scaleFactorX > 1) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001900 // Clear out a radius to the right of the srcRect to prevent the
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001901 // X convolution from reading garbage.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001902 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001903 radiusX, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001904 this->clear(&clearRect, 0x0);
1905 }
1906
1907 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001908 GrDrawTarget* target = this->prepareToDraw(NULL, DEFAULT_BUFFERING);
1909 convolve_gaussian(target, srcTexture, srcRect, sigmaX, radiusX,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001910 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001911 srcTexture = dstTexture;
1912 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001913 }
1914
1915 if (sigmaY > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001916 if (scaleFactorY > 1 || sigmaX > 0.0f) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001917 // Clear out a radius below the srcRect to prevent the Y
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001918 // convolution from reading garbage.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001919 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001920 srcIRect.width(), radiusY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001921 this->clear(&clearRect, 0x0);
1922 }
1923
1924 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001925 GrDrawTarget* target = this->prepareToDraw(NULL, DEFAULT_BUFFERING);
1926 convolve_gaussian(target, srcTexture, srcRect, sigmaY, radiusY,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001927 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001928 srcTexture = dstTexture;
1929 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001930 }
1931
1932 if (scaleFactorX > 1 || scaleFactorY > 1) {
1933 // Clear one pixel to the right and below, to accommodate bilinear
1934 // upsampling.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001935 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
robertphillips@google.com7a396332012-05-10 15:11:27 +00001936 srcIRect.width() + 1, 1);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001937 this->clear(&clearRect, 0x0);
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001938 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
robertphillips@google.com7a396332012-05-10 15:11:27 +00001939 1, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001940 this->clear(&clearRect, 0x0);
1941 // FIXME: This should be mitchell, not bilinear.
bsalomon@google.comb8670992012-07-25 21:27:09 +00001942 paint.textureSampler(0)->textureParams()->setBilerp(true);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001943 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1944 srcTexture->height());
1945 this->setRenderTarget(dstTexture->asRenderTarget());
tomhudson@google.comaa72eab2012-07-19 18:01:07 +00001946 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
1947 (srcTexture)))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001948 SkRect dstRect(srcRect);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001949 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001950 this->drawRectToRect(paint, dstRect, srcRect);
1951 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001952 srcTexture = dstTexture;
1953 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001954 }
1955 this->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001956 if (srcTexture == temp1.texture()) {
1957 return temp1.detach();
1958 } else if (srcTexture == temp2.texture()) {
1959 return temp2.detach();
1960 } else {
1961 srcTexture->ref();
1962 return srcTexture;
1963 }
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001964}
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001965
bsalomon@google.comc4364992011-11-07 15:54:49 +00001966///////////////////////////////////////////////////////////////////////////////
robertphillips@google.com59552022012-08-31 13:07:37 +00001967#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001968void GrContext::printCacheStats() const {
1969 fTextureCache->printStats();
1970}
1971#endif