blob: 2b5014d6a969dcb000c41e2daa5751408218727e [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.com8fe72472011-03-30 21:26:44 +0000139 fTextureCache->removeAll();
140 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.com8fe72472011-03-30 21:26:44 +0000155 fTextureCache->removeAll();
156 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.com1f47f4f2012-08-16 14:49:16 +0000210GrTexture* GrContext::findAndLockTexture(const GrTextureDesc& desc,
211 const GrCacheData& cacheData,
212 const GrTextureParams* params) {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000213 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000214 GrResource* resource = fTextureCache->findAndLock(resourceKey,
215 GrResourceCache::kNested_LockType);
216 return static_cast<GrTexture*>(resource);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000217}
218
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000219bool GrContext::isTextureInCache(const GrTextureDesc& desc,
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000220 const GrCacheData& cacheData,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000221 const GrTextureParams* params) const {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000222 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000223 return fTextureCache->hasKey(resourceKey);
224}
225
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000226void GrContext::addAndLockStencilBuffer(GrStencilBuffer* sb) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000227 ASSERT_OWNED_RESOURCE(sb);
robertphillips@google.com46a86002012-08-08 10:42:44 +0000228
229 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(sb->width(),
230 sb->height(),
231 sb->numSamples());
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000232 fTextureCache->createAndLock(resourceKey, sb);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000233}
234
235GrStencilBuffer* GrContext::findStencilBuffer(int width, int height,
236 int sampleCnt) {
robertphillips@google.com46a86002012-08-08 10:42:44 +0000237 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(width,
238 height,
239 sampleCnt);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000240 GrResource* resource = fTextureCache->findAndLock(resourceKey,
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000241 GrResourceCache::kSingle_LockType);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000242 return static_cast<GrStencilBuffer*>(resource);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000243}
244
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000245void GrContext::unlockStencilBuffer(GrStencilBuffer* sb) {
246 ASSERT_OWNED_RESOURCE(sb);
247 GrAssert(NULL != sb->getCacheEntry());
248
249 fTextureCache->unlock(sb->getCacheEntry());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000250}
251
252static void stretchImage(void* dst,
253 int dstW,
254 int dstH,
255 void* src,
256 int srcW,
257 int srcH,
258 int bpp) {
259 GrFixed dx = (srcW << 16) / dstW;
260 GrFixed dy = (srcH << 16) / dstH;
261
262 GrFixed y = dy >> 1;
263
264 int dstXLimit = dstW*bpp;
265 for (int j = 0; j < dstH; ++j) {
266 GrFixed x = dx >> 1;
267 void* srcRow = (uint8_t*)src + (y>>16)*srcW*bpp;
268 void* dstRow = (uint8_t*)dst + j*dstW*bpp;
269 for (int i = 0; i < dstXLimit; i += bpp) {
270 memcpy((uint8_t*) dstRow + i,
271 (uint8_t*) srcRow + (x>>16)*bpp,
272 bpp);
273 x += dx;
274 }
275 y += dy;
276 }
277}
278
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000279// The desired texture is NPOT and tiled but that isn't supported by
robertphillips@google.com3319f332012-08-13 18:00:36 +0000280// the current hardware. Resize the texture to be a POT
281GrTexture* GrContext::createResizedTexture(const GrTextureDesc& desc,
282 const GrCacheData& cacheData,
283 void* srcData,
284 size_t rowBytes,
285 bool needsFiltering) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000286 GrTexture* clampedTexture = this->findAndLockTexture(desc, cacheData, NULL);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000287
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000288 if (NULL == clampedTexture) {
289 clampedTexture = this->createAndLockTexture(NULL, desc, cacheData, srcData, rowBytes);
290 GrAssert(NULL != clampedTexture);
291 if (NULL == clampedTexture) {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000292 return NULL;
293 }
294 }
295 GrTextureDesc rtDesc = desc;
296 rtDesc.fFlags = rtDesc.fFlags |
297 kRenderTarget_GrTextureFlagBit |
298 kNoStencil_GrTextureFlagBit;
299 rtDesc.fWidth = GrNextPow2(GrMax(desc.fWidth, 64));
300 rtDesc.fHeight = GrNextPow2(GrMax(desc.fHeight, 64));
301
302 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
303
304 if (NULL != texture) {
305 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
306 GrDrawState* drawState = fGpu->drawState();
307 drawState->setRenderTarget(texture->asRenderTarget());
308
309 // if filtering is not desired then we want to ensure all
310 // texels in the resampled image are copies of texels from
311 // the original.
312 drawState->sampler(0)->reset(SkShader::kClamp_TileMode,
313 needsFiltering);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000314 drawState->createTextureEffect(0, clampedTexture);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000315
316 static const GrVertexLayout layout =
317 GrDrawTarget::StageTexCoordVertexLayoutBit(0,0);
318 GrDrawTarget::AutoReleaseGeometry arg(fGpu, layout, 4, 0);
319
320 if (arg.succeeded()) {
321 GrPoint* verts = (GrPoint*) arg.vertices();
322 verts[0].setIRectFan(0, 0,
323 texture->width(),
324 texture->height(),
325 2*sizeof(GrPoint));
326 verts[1].setIRectFan(0, 0, 1, 1, 2*sizeof(GrPoint));
327 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType,
328 0, 4);
329 }
330 texture->releaseRenderTarget();
331 } else {
332 // TODO: Our CPU stretch doesn't filter. But we create separate
333 // stretched textures when the sampler state is either filtered or
334 // not. Either implement filtered stretch blit on CPU or just create
335 // one when FBO case fails.
336
337 rtDesc.fFlags = kNone_GrTextureFlags;
338 // no longer need to clamp at min RT size.
339 rtDesc.fWidth = GrNextPow2(desc.fWidth);
340 rtDesc.fHeight = GrNextPow2(desc.fHeight);
341 int bpp = GrBytesPerPixel(desc.fConfig);
342 SkAutoSMalloc<128*128*4> stretchedPixels(bpp *
343 rtDesc.fWidth *
344 rtDesc.fHeight);
345 stretchImage(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
346 srcData, desc.fWidth, desc.fHeight, bpp);
347
348 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
349
350 GrTexture* texture = fGpu->createTexture(rtDesc,
351 stretchedPixels.get(),
352 stretchedRowBytes);
353 GrAssert(NULL != texture);
354 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000355 this->unlockTexture(clampedTexture);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000356
357 return texture;
358}
359
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000360GrTexture* GrContext::createAndLockTexture(
bsalomon@google.comb8670992012-07-25 21:27:09 +0000361 const GrTextureParams* params,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000362 const GrTextureDesc& desc,
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000363 const GrCacheData& cacheData,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000364 void* srcData,
365 size_t rowBytes) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000366 SK_TRACE_EVENT0("GrContext::createAndLockTexture");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000367
368#if GR_DUMP_TEXTURE_UPLOAD
369 GrPrintf("GrContext::createAndLockTexture [%d %d]\n", desc.fWidth, desc.fHeight);
370#endif
371
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000372 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000373
robertphillips@google.com3319f332012-08-13 18:00:36 +0000374 GrTexture* texture = NULL;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000375 if (GrTexture::NeedsResizing(resourceKey)) {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000376 texture = this->createResizedTexture(desc, cacheData,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000377 srcData, rowBytes,
robertphillips@google.com3319f332012-08-13 18:00:36 +0000378 GrTexture::NeedsFiltering(resourceKey));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000379 } else {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000380 texture = fGpu->createTexture(desc, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000381 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000382
383 if (NULL != texture) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000384 fTextureCache->createAndLock(resourceKey, texture);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000385 }
386
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000387 return texture;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000388}
389
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000390GrTexture* GrContext::lockScratchTexture(const GrTextureDesc& inDesc,
391 ScratchTexMatch match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000392 GrTextureDesc desc = inDesc;
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000393 GrCacheData cacheData(GrCacheData::kScratch_CacheID);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000394
rileya@google.com2afb8ec2012-08-23 14:08:57 +0000395 GrAssert((desc.fFlags & kRenderTarget_GrTextureFlagBit) ||
396 !(desc.fFlags & kNoStencil_GrTextureFlagBit));
397
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000398 if (kExact_ScratchTexMatch != match) {
399 // bin by pow2 with a reasonable min
400 static const int MIN_SIZE = 256;
401 desc.fWidth = GrMax(MIN_SIZE, GrNextPow2(desc.fWidth));
402 desc.fHeight = GrMax(MIN_SIZE, GrNextPow2(desc.fHeight));
403 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000404
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000405 GrResource* resource = NULL;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000406 int origWidth = desc.fWidth;
407 int origHeight = desc.fHeight;
408 bool doubledW = false;
409 bool doubledH = false;
410
411 do {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000412 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL, desc, cacheData, true);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000413 resource = fTextureCache->findAndLock(key,
414 GrResourceCache::kNested_LockType);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000415 // if we miss, relax the fit of the flags...
416 // then try doubling width... then height.
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000417 if (NULL != resource || kExact_ScratchTexMatch == match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000418 break;
419 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000420 // We no longer try to reuse textures that were previously used as render targets in
rileya@google.com2afb8ec2012-08-23 14:08:57 +0000421 // situations where no RT is needed; doing otherwise can confuse the video driver and
422 // cause significant performance problems in some cases.
423 if (desc.fFlags & kNoStencil_GrTextureFlagBit) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000424 desc.fFlags = desc.fFlags & ~kNoStencil_GrTextureFlagBit;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000425 } else if (!doubledW) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000426 desc.fFlags = inDesc.fFlags;
427 desc.fWidth *= 2;
428 doubledW = true;
429 } else if (!doubledH) {
430 desc.fFlags = inDesc.fFlags;
431 desc.fWidth = origWidth;
432 desc.fHeight *= 2;
433 doubledH = true;
434 } else {
435 break;
436 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000437
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000438 } while (true);
439
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000440 if (NULL == resource) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000441 desc.fFlags = inDesc.fFlags;
442 desc.fWidth = origWidth;
443 desc.fHeight = origHeight;
444 GrTexture* texture = fGpu->createTexture(desc, NULL, 0);
445 if (NULL != texture) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000446 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000447 texture->desc(),
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000448 cacheData,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000449 true);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000450 fTextureCache->createAndLock(key, texture);
451 resource = texture;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000452 }
453 }
454
455 // If the caller gives us the same desc/sampler twice we don't want
456 // to return the same texture the second time (unless it was previously
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000457 // released). So make it exclusive to hide it from future searches.
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000458 if (NULL != resource) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000459 fTextureCache->makeExclusive(resource->getCacheEntry());
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000460 }
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000461
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000462 return static_cast<GrTexture*>(resource);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000463}
464
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000465void GrContext::addExistingTextureToCache(GrTexture* texture) {
466
467 if (NULL == texture) {
468 return;
469 }
470
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000471 // This texture should already have a cache entry since it was once
472 // attached
473 GrAssert(NULL != texture->getCacheEntry());
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000474
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000475 // Conceptually, the cache entry is going to assume responsibility
476 // for the creation ref.
477 GrAssert(1 == texture->getRefCnt());
478
479 // Since this texture came from an AutoScratchTexture it should
480 // still be in the exclusive pile
481 fTextureCache->makeNonExclusive(texture->getCacheEntry());
482
483 // and it should still be locked
484 fTextureCache->unlock(texture->getCacheEntry());
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000485}
486
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000487void GrContext::unlockTexture(GrTexture* texture) {
488 ASSERT_OWNED_RESOURCE(texture);
489 GrAssert(NULL != texture->getCacheEntry());
490
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000491 // If this is a scratch texture we detached it from the cache
492 // while it was locked (to avoid two callers simultaneously getting
493 // the same texture).
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000494 if (GrTexture::IsScratchTexture(texture->getCacheEntry()->key())) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000495 fTextureCache->makeNonExclusive(texture->getCacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000496 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000497
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000498 fTextureCache->unlock(texture->getCacheEntry());
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000499}
500
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000501GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000502 void* srcData,
503 size_t rowBytes) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000504 GrTextureDesc descCopy = descIn;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000505 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000506}
507
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000508void GrContext::getTextureCacheLimits(int* maxTextures,
509 size_t* maxTextureBytes) const {
510 fTextureCache->getLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000511}
512
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000513void GrContext::setTextureCacheLimits(int maxTextures, size_t maxTextureBytes) {
514 fTextureCache->setLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000515}
516
bsalomon@google.com91958362011-06-13 17:58:13 +0000517int GrContext::getMaxTextureSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000518 return fGpu->getCaps().fMaxTextureSize;
bsalomon@google.com91958362011-06-13 17:58:13 +0000519}
520
521int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000522 return fGpu->getCaps().fMaxRenderTargetSize;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000523}
524
525///////////////////////////////////////////////////////////////////////////////
526
bsalomon@google.come269f212011-11-07 13:29:52 +0000527GrTexture* GrContext::createPlatformTexture(const GrPlatformTextureDesc& desc) {
528 return fGpu->createPlatformTexture(desc);
529}
530
531GrRenderTarget* GrContext::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
532 return fGpu->createPlatformRenderTarget(desc);
533}
534
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000535///////////////////////////////////////////////////////////////////////////////
536
bsalomon@google.comb8670992012-07-25 21:27:09 +0000537bool GrContext::supportsIndex8PixelConfig(const GrTextureParams* params,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000538 int width, int height) const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000539 const GrDrawTarget::Caps& caps = fGpu->getCaps();
540 if (!caps.f8BitPaletteSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000541 return false;
542 }
543
bsalomon@google.com27847de2011-02-22 20:59:41 +0000544 bool isPow2 = GrIsPow2(width) && GrIsPow2(height);
545
546 if (!isPow2) {
bsalomon@google.comb8670992012-07-25 21:27:09 +0000547 bool tiled = NULL != params && params->isTiled();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000548 if (tiled && !caps.fNPOTTextureTileSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000549 return false;
550 }
551 }
552 return true;
553}
554
555////////////////////////////////////////////////////////////////////////////////
556
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000557const GrClipData* GrContext::getClip() const {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000558 return fGpu->getClip();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000559}
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000560
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000561void GrContext::setClip(const GrClipData* clipData) {
562 fGpu->setClip(clipData);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000563 fDrawState->enableState(GrDrawState::kClip_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000564}
565
bsalomon@google.com27847de2011-02-22 20:59:41 +0000566////////////////////////////////////////////////////////////////////////////////
567
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000568void GrContext::clear(const GrIRect* rect,
569 const GrColor color,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000570 GrRenderTarget* target) {
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000571 this->prepareToDraw(NULL, DEFAULT_BUFFERING)->clear(rect, color, target);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000572}
573
574void GrContext::drawPaint(const GrPaint& paint) {
575 // set rect to be big enough to fill the space, but not super-huge, so we
576 // don't overflow fixed-point implementations
bsalomon@google.comd302f142011-03-03 13:54:13 +0000577 GrRect r;
578 r.setLTRB(0, 0,
579 GrIntToScalar(getRenderTarget()->width()),
580 GrIntToScalar(getRenderTarget()->height()));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000581 GrMatrix inverse;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000582 SkTLazy<GrPaint> tmpPaint;
583 const GrPaint* p = &paint;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000584 AutoMatrix am;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000585
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000586 // We attempt to map r by the inverse matrix and draw that. mapRect will
587 // map the four corners and bound them with a new rect. This will not
588 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000589 if (!this->getMatrix().hasPerspective()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000590 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000591 GrPrintf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000592 return;
593 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000594 inverse.mapRect(&r);
595 } else {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000596 if (paint.hasTextureOrMask()) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000597 tmpPaint.set(paint);
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000598 p = tmpPaint.get();
bsalomon@google.come3d32162012-07-20 13:37:06 +0000599 if (!tmpPaint.get()->preConcatSamplerMatricesWithInverse(fDrawState->getViewMatrix())) {
600 GrPrintf("Could not invert matrix\n");
601 }
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000602 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000603 am.set(this, GrMatrix::I());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000604 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000605 // by definition this fills the entire clip, no need for AA
606 if (paint.fAntiAlias) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000607 if (!tmpPaint.isValid()) {
608 tmpPaint.set(paint);
609 p = tmpPaint.get();
610 }
611 GrAssert(p == tmpPaint.get());
612 tmpPaint.get()->fAntiAlias = false;
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000613 }
614 this->drawRect(*p, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000615}
616
bsalomon@google.com205d4602011-04-25 12:43:45 +0000617////////////////////////////////////////////////////////////////////////////////
618
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000619namespace {
620inline bool disable_coverage_aa_for_blend(GrDrawTarget* target) {
621 return DISABLE_COVERAGE_AA_FOR_BLEND && !target->canApplyCoverage();
622}
623}
624
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000625////////////////////////////////////////////////////////////////////////////////
626
bsalomon@google.com27847de2011-02-22 20:59:41 +0000627/* create a triangle strip that strokes the specified triangle. There are 8
628 unique vertices, but we repreat the last 2 to close up. Alternatively we
629 could use an indices array, and then only send 8 verts, but not sure that
630 would be faster.
631 */
bsalomon@google.com205d4602011-04-25 12:43:45 +0000632static void setStrokeRectStrip(GrPoint verts[10], GrRect rect,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000633 GrScalar width) {
634 const GrScalar rad = GrScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000635 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000636
637 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
638 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
639 verts[2].set(rect.fRight - rad, rect.fTop + rad);
640 verts[3].set(rect.fRight + rad, rect.fTop - rad);
641 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
642 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
643 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
644 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
645 verts[8] = verts[0];
646 verts[9] = verts[1];
647}
648
reed@google.com20efde72011-05-09 17:00:02 +0000649/**
650 * Returns true if the rects edges are integer-aligned.
651 */
652static bool isIRect(const GrRect& r) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000653 return GrScalarIsInt(r.fLeft) && GrScalarIsInt(r.fTop) &&
reed@google.com20efde72011-05-09 17:00:02 +0000654 GrScalarIsInt(r.fRight) && GrScalarIsInt(r.fBottom);
655}
656
bsalomon@google.com205d4602011-04-25 12:43:45 +0000657static bool apply_aa_to_rect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000658 const GrRect& rect,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000659 GrScalar width,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000660 const GrMatrix* matrix,
661 GrMatrix* combinedMatrix,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000662 GrRect* devRect,
663 bool* useVertexCoverage) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000664 // we use a simple coverage ramp to do aa on axis-aligned rects
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000665 // we check if the rect will be axis-aligned, and the rect won't land on
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000666 // integer coords.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000667
bsalomon@google.coma3108262011-10-10 14:08:47 +0000668 // we are keeping around the "tweak the alpha" trick because
669 // it is our only hope for the fixed-pipe implementation.
670 // In a shader implementation we can give a separate coverage input
bsalomon@google.com289533a2011-10-27 12:34:25 +0000671 // TODO: remove this ugliness when we drop the fixed-pipe impl
bsalomon@google.coma3108262011-10-10 14:08:47 +0000672 *useVertexCoverage = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000673 if (!target->canTweakAlphaForCoverage()) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000674 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +0000675#if GR_DEBUG
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000676 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000677#endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000678 return false;
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000679 } else {
680 *useVertexCoverage = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000681 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000682 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000683 const GrDrawState& drawState = target->getDrawState();
684 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000685 return false;
686 }
687
bsalomon@google.com471d4712011-08-23 15:45:25 +0000688 if (0 == width && target->willUseHWAALines()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000689 return false;
690 }
691
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000692 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000693 return false;
694 }
695
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000696 if (NULL != matrix &&
bsalomon@google.com205d4602011-04-25 12:43:45 +0000697 !matrix->preservesAxisAlignment()) {
698 return false;
699 }
700
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000701 *combinedMatrix = drawState.getViewMatrix();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000702 if (NULL != matrix) {
703 combinedMatrix->preConcat(*matrix);
704 GrAssert(combinedMatrix->preservesAxisAlignment());
705 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000706
bsalomon@google.com205d4602011-04-25 12:43:45 +0000707 combinedMatrix->mapRect(devRect, rect);
708 devRect->sort();
709
710 if (width < 0) {
reed@google.com20efde72011-05-09 17:00:02 +0000711 return !isIRect(*devRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000712 } else {
713 return true;
714 }
715}
716
bsalomon@google.com27847de2011-02-22 20:59:41 +0000717void GrContext::drawRect(const GrPaint& paint,
718 const GrRect& rect,
719 GrScalar width,
720 const GrMatrix* matrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000721 SK_TRACE_EVENT0("GrContext::drawRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000722
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000723 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000724 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000725
bsalomon@google.com205d4602011-04-25 12:43:45 +0000726 GrRect devRect = rect;
727 GrMatrix combinedMatrix;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000728 bool useVertexCoverage;
bsalomon@google.com289533a2011-10-27 12:34:25 +0000729 bool needAA = paint.fAntiAlias &&
730 !this->getRenderTarget()->isMultisampled();
731 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix,
732 &combinedMatrix, &devRect,
733 &useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000734
735 if (doAA) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000736 GrDrawTarget::AutoDeviceCoordDraw adcd(target);
737 if (!adcd.succeeded()) {
738 return;
739 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000740 if (width >= 0) {
741 GrVec strokeSize;;
742 if (width > 0) {
743 strokeSize.set(width, width);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000744 combinedMatrix.mapVectors(&strokeSize, 1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000745 strokeSize.setAbs(strokeSize);
746 } else {
747 strokeSize.set(GR_Scalar1, GR_Scalar1);
748 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000749 fAARectRenderer->strokeAARect(this->getGpu(), target, devRect,
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000750 strokeSize, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000751 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000752 fAARectRenderer->fillAARect(this->getGpu(), target,
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000753 devRect, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000754 }
755 return;
756 }
757
bsalomon@google.com27847de2011-02-22 20:59:41 +0000758 if (width >= 0) {
759 // TODO: consider making static vertex buffers for these cases.
760 // Hairline could be done by just adding closing vertex to
761 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000762
bsalomon@google.com27847de2011-02-22 20:59:41 +0000763 static const int worstCaseVertCount = 10;
bsalomon@google.come3d32162012-07-20 13:37:06 +0000764 GrDrawTarget::AutoReleaseGeometry geo(target, 0, worstCaseVertCount, 0);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000765
766 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000767 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000768 return;
769 }
770
771 GrPrimitiveType primType;
772 int vertCount;
773 GrPoint* vertex = geo.positions();
774
775 if (width > 0) {
776 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000777 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000778 setStrokeRectStrip(vertex, rect, width);
779 } else {
780 // hairline
781 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000782 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000783 vertex[0].set(rect.fLeft, rect.fTop);
784 vertex[1].set(rect.fRight, rect.fTop);
785 vertex[2].set(rect.fRight, rect.fBottom);
786 vertex[3].set(rect.fLeft, rect.fBottom);
787 vertex[4].set(rect.fLeft, rect.fTop);
788 }
789
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000790 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000791 if (NULL != matrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000792 GrDrawState* drawState = target->drawState();
793 avmr.set(drawState);
794 drawState->preConcatViewMatrix(*matrix);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000795 drawState->preConcatSamplerMatrices(*matrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000796 }
797
798 target->drawNonIndexed(primType, 0, vertCount);
799 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000800#if GR_STATIC_RECT_VB
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000801 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
802 if (NULL == sqVB) {
803 GrPrintf("Failed to create static rect vb.\n");
804 return;
805 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000806 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000807 GrDrawState* drawState = target->drawState();
808 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000809 GrMatrix m;
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000810 m.setAll(rect.width(), 0, rect.fLeft,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000811 0, rect.height(), rect.fTop,
812 0, 0, GrMatrix::I()[8]);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000813
814 if (NULL != matrix) {
815 m.postConcat(*matrix);
816 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000817 drawState->preConcatViewMatrix(m);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000818 drawState->preConcatSamplerMatrices(m);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000819
bsalomon@google.com47059542012-06-06 20:51:20 +0000820 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000821#else
bsalomon@google.come3d32162012-07-20 13:37:06 +0000822 target->drawSimpleRect(rect, matrix);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000823#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000824 }
825}
826
827void GrContext::drawRectToRect(const GrPaint& paint,
828 const GrRect& dstRect,
829 const GrRect& srcRect,
830 const GrMatrix* dstMatrix,
831 const GrMatrix* srcMatrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000832 SK_TRACE_EVENT0("GrContext::drawRectToRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000833
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000834 // srcRect refers to paint's first texture
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000835 if (!paint.isTextureStageEnabled(0)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000836 drawRect(paint, dstRect, -1, dstMatrix);
837 return;
838 }
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000839
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000840 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000841
842#if GR_STATIC_RECT_VB
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000843 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000844 GrDrawState* drawState = target->drawState();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000845 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000846
847 GrMatrix m;
848
849 m.setAll(dstRect.width(), 0, dstRect.fLeft,
850 0, dstRect.height(), dstRect.fTop,
851 0, 0, GrMatrix::I()[8]);
852 if (NULL != dstMatrix) {
853 m.postConcat(*dstMatrix);
854 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000855 drawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000856
bsalomon@google.come3d32162012-07-20 13:37:06 +0000857 // we explicitly setup the correct coords for the first stage. The others
858 // must know about the view matrix change.
859 for (int s = 1; s < GrPaint::kTotalStages; ++s) {
860 if (drawState->isStageEnabled(s)) {
861 drawState->sampler(s)->preConcatMatrix(m);
862 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000863 }
864
bsalomon@google.com27847de2011-02-22 20:59:41 +0000865 m.setAll(srcRect.width(), 0, srcRect.fLeft,
866 0, srcRect.height(), srcRect.fTop,
867 0, 0, GrMatrix::I()[8]);
868 if (NULL != srcMatrix) {
869 m.postConcat(*srcMatrix);
870 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000871 drawState->sampler(GrPaint::kFirstTextureStage)->preConcatMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000872
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000873 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
874 if (NULL == sqVB) {
875 GrPrintf("Failed to create static rect vb.\n");
876 return;
877 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000878 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com47059542012-06-06 20:51:20 +0000879 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000880#else
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000881 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000882
tomhudson@google.com93813632011-10-27 20:21:16 +0000883 const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
884 const GrMatrix* srcMatrices[GrDrawState::kNumStages] = {NULL};
bsalomon@google.com27847de2011-02-22 20:59:41 +0000885 srcRects[0] = &srcRect;
886 srcMatrices[0] = srcMatrix;
887
bsalomon@google.come3d32162012-07-20 13:37:06 +0000888 target->drawRect(dstRect, dstMatrix, srcRects, srcMatrices);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000889#endif
890}
891
892void GrContext::drawVertices(const GrPaint& paint,
893 GrPrimitiveType primitiveType,
894 int vertexCount,
895 const GrPoint positions[],
896 const GrPoint texCoords[],
897 const GrColor colors[],
898 const uint16_t indices[],
899 int indexCount) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000900 SK_TRACE_EVENT0("GrContext::drawVertices");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000901
902 GrDrawTarget::AutoReleaseGeometry geo;
903
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000904 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000905 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000906
bsalomon@google.come3d32162012-07-20 13:37:06 +0000907 GrVertexLayout layout = 0;
908 if (NULL != texCoords) {
909 layout |= GrDrawTarget::StageTexCoordVertexLayoutBit(0, 0);
910 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000911 if (NULL != colors) {
912 layout |= GrDrawTarget::kColor_VertexLayoutBit;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000913 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000914 int vertexSize = GrDrawTarget::VertexSize(layout);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000915
916 if (sizeof(GrPoint) != vertexSize) {
917 if (!geo.set(target, layout, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000918 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000919 return;
920 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000921 int texOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000922 int colorOffset;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000923 GrDrawTarget::VertexSizeAndOffsetsByIdx(layout,
924 texOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000925 &colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000926 NULL,
927 NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000928 void* curVertex = geo.vertices();
929
930 for (int i = 0; i < vertexCount; ++i) {
931 *((GrPoint*)curVertex) = positions[i];
932
933 if (texOffsets[0] > 0) {
934 *(GrPoint*)((intptr_t)curVertex + texOffsets[0]) = texCoords[i];
935 }
936 if (colorOffset > 0) {
937 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
938 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000939 curVertex = (void*)((intptr_t)curVertex + vertexSize);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000940 }
941 } else {
942 target->setVertexSourceToArray(layout, positions, vertexCount);
943 }
944
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000945 // we don't currently apply offscreen AA to this path. Need improved
bsalomon@google.com91958362011-06-13 17:58:13 +0000946 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000947
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000948 if (NULL != indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000949 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000950 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000951 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000952 target->drawNonIndexed(primitiveType, 0, vertexCount);
953 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000954}
955
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000956///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com150d2842012-01-12 20:19:56 +0000957namespace {
958
bsalomon@google.com93c96602012-04-27 13:05:21 +0000959struct CircleVertex {
960 GrPoint fPos;
961 GrPoint fCenter;
962 GrScalar fOuterRadius;
963 GrScalar fInnerRadius;
964};
965
966/* Returns true if will map a circle to another circle. This can be true
967 * if the matrix only includes square-scale, rotation, translation.
968 */
969inline bool isSimilarityTransformation(const SkMatrix& matrix,
970 SkScalar tol = SK_ScalarNearlyZero) {
971 if (matrix.isIdentity() || matrix.getType() == SkMatrix::kTranslate_Mask) {
972 return true;
973 }
974 if (matrix.hasPerspective()) {
975 return false;
976 }
977
978 SkScalar mx = matrix.get(SkMatrix::kMScaleX);
979 SkScalar sx = matrix.get(SkMatrix::kMSkewX);
980 SkScalar my = matrix.get(SkMatrix::kMScaleY);
981 SkScalar sy = matrix.get(SkMatrix::kMSkewY);
982
983 if (mx == 0 && sx == 0 && my == 0 && sy == 0) {
984 return false;
985 }
986
987 // it has scales or skews, but it could also be rotation, check it out.
988 SkVector vec[2];
989 vec[0].set(mx, sx);
990 vec[1].set(sy, my);
991
992 return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) &&
993 SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(),
994 SkScalarSquare(tol));
995}
996
997}
998
999// TODO: strokeWidth can't be larger than zero right now.
1000// It will be fixed when drawPath() can handle strokes.
1001void GrContext::drawOval(const GrPaint& paint,
1002 const GrRect& rect,
1003 SkScalar strokeWidth) {
bsalomon@google.com0982d352012-07-31 15:33:25 +00001004 GrAssert(strokeWidth <= 0);
1005 if (!isSimilarityTransformation(this->getMatrix()) ||
bsalomon@google.com93c96602012-04-27 13:05:21 +00001006 !paint.fAntiAlias ||
1007 rect.height() != rect.width()) {
1008 SkPath path;
1009 path.addOval(rect);
1010 GrPathFill fill = (strokeWidth == 0) ?
bsalomon@google.com0982d352012-07-31 15:33:25 +00001011 kHairLine_GrPathFill : kWinding_GrPathFill;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001012 this->internalDrawPath(paint, path, fill, NULL);
1013 return;
1014 }
1015
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001016 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
1017
bsalomon@google.com0982d352012-07-31 15:33:25 +00001018 GrDrawState* drawState = target->drawState();
1019 GrDrawState::AutoStageDisable atr(fDrawState);
1020 const GrMatrix vm = drawState->getViewMatrix();
1021
bsalomon@google.com93c96602012-04-27 13:05:21 +00001022 const GrRenderTarget* rt = drawState->getRenderTarget();
1023 if (NULL == rt) {
1024 return;
1025 }
1026
bsalomon@google.come3d32162012-07-20 13:37:06 +00001027 GrDrawTarget::AutoDeviceCoordDraw adcd(target);
1028 if (!adcd.succeeded()) {
1029 return;
1030 }
bsalomon@google.com93c96602012-04-27 13:05:21 +00001031
bsalomon@google.come3d32162012-07-20 13:37:06 +00001032 GrVertexLayout layout = GrDrawTarget::kEdge_VertexLayoutBit;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001033 GrAssert(sizeof(CircleVertex) == GrDrawTarget::VertexSize(layout));
1034
1035 GrPoint center = GrPoint::Make(rect.centerX(), rect.centerY());
1036 GrScalar radius = SkScalarHalf(rect.width());
1037
1038 vm.mapPoints(&center, 1);
1039 radius = vm.mapRadius(radius);
1040
1041 GrScalar outerRadius = radius;
1042 GrScalar innerRadius = 0;
1043 SkScalar halfWidth = 0;
1044 if (strokeWidth == 0) {
1045 halfWidth = SkScalarHalf(SK_Scalar1);
1046
1047 outerRadius += halfWidth;
1048 innerRadius = SkMaxScalar(0, radius - halfWidth);
1049 }
1050
1051 GrDrawTarget::AutoReleaseGeometry geo(target, layout, 4, 0);
1052 if (!geo.succeeded()) {
1053 GrPrintf("Failed to get space for vertices!\n");
1054 return;
1055 }
1056
1057 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
1058
robertphillips@google.coma0a66c12012-06-22 13:14:29 +00001059 // The fragment shader will extend the radius out half a pixel
1060 // to antialias. Expand the drawn rect here so all the pixels
1061 // will be captured.
1062 SkScalar L = center.fX - outerRadius - SkFloatToScalar(0.5f);
1063 SkScalar R = center.fX + outerRadius + SkFloatToScalar(0.5f);
1064 SkScalar T = center.fY - outerRadius - SkFloatToScalar(0.5f);
1065 SkScalar B = center.fY + outerRadius + SkFloatToScalar(0.5f);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001066
1067 verts[0].fPos = SkPoint::Make(L, T);
1068 verts[1].fPos = SkPoint::Make(R, T);
1069 verts[2].fPos = SkPoint::Make(L, B);
1070 verts[3].fPos = SkPoint::Make(R, B);
1071
1072 for (int i = 0; i < 4; ++i) {
1073 // this goes to fragment shader, it should be in y-points-up space.
1074 verts[i].fCenter = SkPoint::Make(center.fX, rt->height() - center.fY);
1075
1076 verts[i].fOuterRadius = outerRadius;
1077 verts[i].fInnerRadius = innerRadius;
1078 }
1079
1080 drawState->setVertexEdgeType(GrDrawState::kCircle_EdgeType);
bsalomon@google.com47059542012-06-06 20:51:20 +00001081 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001082}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001083
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001084void GrContext::drawPath(const GrPaint& paint, const SkPath& path,
reed@google.com07f3ee12011-05-16 17:21:57 +00001085 GrPathFill fill, const GrPoint* translate) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001086
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001087 if (path.isEmpty()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001088 if (GrIsFillInverted(fill)) {
1089 this->drawPaint(paint);
1090 }
1091 return;
1092 }
1093
bsalomon@google.com93c96602012-04-27 13:05:21 +00001094 SkRect ovalRect;
1095 if (!GrIsFillInverted(fill) && path.isOval(&ovalRect)) {
1096 if (translate) {
1097 ovalRect.offset(*translate);
1098 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001099 SkScalar width = (fill == kHairLine_GrPathFill) ? 0 : -SK_Scalar1;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001100 this->drawOval(paint, ovalRect, width);
1101 return;
1102 }
1103
1104 internalDrawPath(paint, path, fill, translate);
1105}
1106
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001107void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +00001108 GrPathFill fill, const GrPoint* translate) {
1109
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001110 // Note that below we may sw-rasterize the path into a scratch texture.
1111 // Scratch textures can be recycled after they are returned to the texture
1112 // cache. This presents a potential hazard for buffered drawing. However,
1113 // the writePixels that uploads to the scratch will perform a flush so we're
1114 // OK.
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001115 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +00001116 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001117
bsalomon@google.com289533a2011-10-27 12:34:25 +00001118 bool prAA = paint.fAntiAlias && !this->getRenderTarget()->isMultisampled();
1119
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001120 // An Assumption here is that path renderer would use some form of tweaking
1121 // the src color (either the input alpha or in the frag shader) to implement
1122 // aa. If we have some future driver-mojo path AA that can do the right
1123 // thing WRT to the blend then we'll need some query on the PR.
1124 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001125#if GR_DEBUG
bsalomon@google.com979432b2011-11-05 21:38:22 +00001126 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001127#endif
bsalomon@google.com289533a2011-10-27 12:34:25 +00001128 prAA = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001129 }
bsalomon@google.com289533a2011-10-27 12:34:25 +00001130
robertphillips@google.com72176b22012-05-23 13:19:12 +00001131 GrPathRenderer* pr = this->getPathRenderer(path, fill, target, prAA, true);
bsalomon@google.com30085192011-08-19 15:42:31 +00001132 if (NULL == pr) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001133#if GR_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001134 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001135#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001136 return;
1137 }
1138
bsalomon@google.come3d32162012-07-20 13:37:06 +00001139 pr->drawPath(path, fill, translate, target, prAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001140}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001141
bsalomon@google.com27847de2011-02-22 20:59:41 +00001142////////////////////////////////////////////////////////////////////////////////
1143
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001144void GrContext::flush(int flagsBitfield) {
1145 if (kDiscard_FlushBit & flagsBitfield) {
1146 fDrawBuffer->reset();
1147 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001148 this->flushDrawBuffer();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001149 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001150 if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001151 fGpu->forceRenderTargetFlush();
1152 }
1153}
1154
bsalomon@google.com27847de2011-02-22 20:59:41 +00001155void GrContext::flushDrawBuffer() {
junov@google.com53a55842011-06-08 22:55:10 +00001156 if (fDrawBuffer) {
robertphillips@google.com58b38182012-05-03 16:29:41 +00001157 // With addition of the AA clip path, flushing the draw buffer can
1158 // result in the generation of an AA clip mask. During this
1159 // process the SW path renderer may be invoked which recusively
1160 // calls this method (via internalWriteTexturePixels) creating
1161 // infinite recursion
1162 GrInOrderDrawBuffer* temp = fDrawBuffer;
1163 fDrawBuffer = NULL;
1164
1165 temp->flushTo(fGpu);
1166
1167 fDrawBuffer = temp;
junov@google.com53a55842011-06-08 22:55:10 +00001168 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001169}
1170
bsalomon@google.com0342a852012-08-20 19:22:38 +00001171void GrContext::writeTexturePixels(GrTexture* texture,
1172 int left, int top, int width, int height,
1173 GrPixelConfig config, const void* buffer, size_t rowBytes,
1174 uint32_t flags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001175 SK_TRACE_EVENT0("GrContext::writeTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001176 ASSERT_OWNED_RESOURCE(texture);
1177
bsalomon@google.com0342a852012-08-20 19:22:38 +00001178 // TODO: use scratch texture to perform conversion
1179 if (kUnpremul_PixelOpsFlag & flags) {
1180 return;
1181 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001182 if (!(kDontFlush_PixelOpsFlag & flags)) {
1183 this->flush();
1184 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001185
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001186 fGpu->writeTexturePixels(texture, left, top, width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +00001187 config, buffer, rowBytes);
1188}
1189
bsalomon@google.com0342a852012-08-20 19:22:38 +00001190bool GrContext::readTexturePixels(GrTexture* texture,
1191 int left, int top, int width, int height,
1192 GrPixelConfig config, void* buffer, size_t rowBytes,
1193 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001194 SK_TRACE_EVENT0("GrContext::readTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001195 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001196
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001197 // TODO: code read pixels for textures that aren't also rendertargets
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001198 GrRenderTarget* target = texture->asRenderTarget();
1199 if (NULL != target) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001200 return this->readRenderTargetPixels(target,
1201 left, top, width, height,
1202 config, buffer, rowBytes,
1203 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001204 } else {
1205 return false;
1206 }
1207}
1208
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001209#include "SkConfig8888.h"
1210
1211namespace {
1212/**
1213 * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel
1214 * formats are representable as Config8888 and so the function returns false
1215 * if the GrPixelConfig has no equivalent Config8888.
1216 */
1217bool grconfig_to_config8888(GrPixelConfig config,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001218 bool unpremul,
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001219 SkCanvas::Config8888* config8888) {
1220 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001221 case kRGBA_8888_GrPixelConfig:
1222 if (unpremul) {
1223 *config8888 = SkCanvas::kRGBA_Unpremul_Config8888;
1224 } else {
1225 *config8888 = SkCanvas::kRGBA_Premul_Config8888;
1226 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001227 return true;
bsalomon@google.com0342a852012-08-20 19:22:38 +00001228 case kBGRA_8888_GrPixelConfig:
1229 if (unpremul) {
1230 *config8888 = SkCanvas::kBGRA_Unpremul_Config8888;
1231 } else {
1232 *config8888 = SkCanvas::kBGRA_Premul_Config8888;
1233 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001234 return true;
1235 default:
1236 return false;
1237 }
1238}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001239
1240// It returns a configuration with where the byte position of the R & B components are swapped in
1241// relation to the input config. This should only be called with the result of
1242// grconfig_to_config8888 as it will fail for other configs.
1243SkCanvas::Config8888 swap_config8888_red_and_blue(SkCanvas::Config8888 config8888) {
1244 switch (config8888) {
1245 case SkCanvas::kBGRA_Premul_Config8888:
1246 return SkCanvas::kRGBA_Premul_Config8888;
1247 case SkCanvas::kBGRA_Unpremul_Config8888:
1248 return SkCanvas::kRGBA_Unpremul_Config8888;
1249 case SkCanvas::kRGBA_Premul_Config8888:
1250 return SkCanvas::kBGRA_Premul_Config8888;
1251 case SkCanvas::kRGBA_Unpremul_Config8888:
1252 return SkCanvas::kBGRA_Unpremul_Config8888;
1253 default:
1254 GrCrash("Unexpected input");
1255 return SkCanvas::kBGRA_Unpremul_Config8888;;
1256 }
1257}
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001258}
1259
bsalomon@google.com0342a852012-08-20 19:22:38 +00001260bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
1261 int left, int top, int width, int height,
1262 GrPixelConfig config, void* buffer, size_t rowBytes,
1263 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001264 SK_TRACE_EVENT0("GrContext::readRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001265 ASSERT_OWNED_RESOURCE(target);
1266
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001267 if (NULL == target) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001268 target = fDrawState->getRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001269 if (NULL == target) {
1270 return false;
1271 }
1272 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001273
bsalomon@google.com6f379512011-11-16 20:36:03 +00001274 if (!(kDontFlush_PixelOpsFlag & flags)) {
1275 this->flush();
1276 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001277
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001278 // Determine which conversions have to be applied: flipY, swapRAnd, and/or unpremul.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001279
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001280 // If fGpu->readPixels would incur a y-flip cost then we will read the pixels upside down. We'll
1281 // either do the flipY by drawing into a scratch with a matrix or on the cpu after the read.
1282 bool flipY = fGpu->readPixelsWillPayForYFlip(target, left, top,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001283 width, height, config,
1284 rowBytes);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001285 bool swapRAndB = fGpu->preferredReadPixelsConfig(config) == GrPixelConfigSwapRAndB(config);
1286
bsalomon@google.com0342a852012-08-20 19:22:38 +00001287 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001288
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001289 // flipY will get set to false when it is handled below using a scratch. However, in that case
1290 // we still want to do the read upside down.
1291 bool readUpsideDown = flipY;
1292
1293 if (unpremul && kRGBA_8888_GrPixelConfig != config && kBGRA_8888_GrPixelConfig != config) {
1294 // The unpremul flag is only allowed for these two configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001295 return false;
1296 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001297
1298 GrPixelConfig readConfig;
1299 if (swapRAndB) {
1300 readConfig = GrPixelConfigSwapRAndB(config);
1301 GrAssert(kUnknown_GrPixelConfig != config);
1302 } else {
1303 readConfig = config;
1304 }
1305
1306 // If the src is a texture and we would have to do conversions after read pixels, we instead
1307 // do the conversions by drawing the src to a scratch texture. If we handle any of the
1308 // conversions in the draw we set the corresponding bool to false so that we don't reapply it
1309 // on the read back pixels.
1310 GrTexture* src = target->asTexture();
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001311 GrAutoScratchTexture ast;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001312 if (NULL != src && (swapRAndB || unpremul || flipY)) {
1313 // Make the scratch a render target because we don't have a robust readTexturePixels as of
1314 // yet. It calls this function.
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001315 GrTextureDesc desc;
1316 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1317 desc.fWidth = width;
1318 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001319 desc.fConfig = readConfig;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001320
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001321 // When a full readback is faster than a partial we could always make the scratch exactly
1322 // match the passed rect. However, if we see many different size rectangles we will trash
1323 // our texture cache and pay the cost of creating and destroying many textures. So, we only
1324 // request an exact match when the caller is reading an entire RT.
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001325 ScratchTexMatch match = kApprox_ScratchTexMatch;
1326 if (0 == left &&
1327 0 == top &&
1328 target->width() == width &&
1329 target->height() == height &&
1330 fGpu->fullReadPixelsIsFasterThanPartial()) {
1331 match = kExact_ScratchTexMatch;
1332 }
1333 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001334 GrTexture* texture = ast.texture();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001335 if (texture) {
1336 SkAutoTUnref<GrCustomStage> stage;
1337 if (unpremul) {
1338 stage.reset(this->createPMToUPMEffect(src, swapRAndB));
1339 }
1340 // If we failed to create a PM->UPM effect and have no other conversions to perform then
1341 // there is no longer any point to using the scratch.
1342 if (NULL != stage || flipY || swapRAndB) {
1343 if (NULL == stage) {
1344 stage.reset(GrConfigConversionEffect::Create(src, swapRAndB));
1345 GrAssert(NULL != stage);
1346 } else {
1347 unpremul = false; // we will handle the UPM conversion in the draw
1348 }
1349 swapRAndB = false; // we will handle the swap in the draw.
bsalomon@google.comc4364992011-11-07 15:54:49 +00001350
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001351 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
1352 GrDrawState* drawState = fGpu->drawState();
1353 drawState->setRenderTarget(texture->asRenderTarget());
1354 GrMatrix matrix;
1355 if (flipY) {
1356 matrix.setTranslate(SK_Scalar1 * left,
1357 SK_Scalar1 * (top + height));
1358 matrix.set(GrMatrix::kMScaleY, -GR_Scalar1);
1359 flipY = false; // the y flip will be handled in the draw
1360 } else {
1361 matrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
1362 }
1363 matrix.postIDiv(src->width(), src->height());
1364 drawState->sampler(0)->reset(matrix);
1365 drawState->sampler(0)->setCustomStage(stage);
1366 GrRect rect = GrRect::MakeWH(GrIntToScalar(width), GrIntToScalar(height));
1367 fGpu->drawSimpleRect(rect, NULL);
1368 // we want to read back from the scratch's origin
1369 left = 0;
1370 top = 0;
1371 target = texture->asRenderTarget();
1372 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001373 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001374 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001375 if (!fGpu->readPixels(target,
1376 left, top, width, height,
1377 readConfig, buffer, rowBytes, readUpsideDown)) {
1378 return false;
1379 }
1380 // Perform any conversions we weren't able to perfom using a scratch texture.
1381 if (unpremul || swapRAndB || flipY) {
1382 SkCanvas::Config8888 srcC8888;
1383 SkCanvas::Config8888 dstC8888;
1384 bool c8888IsValid = grconfig_to_config8888(config, false, &srcC8888);
1385 grconfig_to_config8888(config, unpremul, &dstC8888);
1386 if (swapRAndB) {
1387 GrAssert(c8888IsValid); // we should only do r/b swap on 8888 configs
1388 srcC8888 = swap_config8888_red_and_blue(srcC8888);
1389 }
1390 if (flipY) {
1391 size_t tightRB = width * GrBytesPerPixel(config);
1392 if (0 == rowBytes) {
1393 rowBytes = tightRB;
1394 }
1395 SkAutoSTMalloc<256, uint8_t> tempRow(tightRB);
1396 intptr_t top = reinterpret_cast<intptr_t>(buffer);
1397 intptr_t bot = top + (height - 1) * rowBytes;
1398 while (top < bot) {
1399 uint32_t* t = reinterpret_cast<uint32_t*>(top);
1400 uint32_t* b = reinterpret_cast<uint32_t*>(bot);
1401 uint32_t* temp = reinterpret_cast<uint32_t*>(tempRow.get());
1402 memcpy(temp, t, tightRB);
1403 if (c8888IsValid) {
1404 SkConvertConfig8888Pixels(t, tightRB, dstC8888,
1405 b, tightRB, srcC8888,
1406 width, 1);
1407 SkConvertConfig8888Pixels(b, tightRB, dstC8888,
1408 temp, tightRB, srcC8888,
1409 width, 1);
1410 } else {
1411 memcpy(t, b, tightRB);
1412 memcpy(b, temp, tightRB);
1413 }
1414 top += rowBytes;
1415 bot -= rowBytes;
1416 }
1417 // The above loop does nothing on the middle row when height is odd.
1418 if (top == bot && c8888IsValid && dstC8888 != srcC8888) {
1419 uint32_t* mid = reinterpret_cast<uint32_t*>(top);
1420 SkConvertConfig8888Pixels(mid, tightRB, dstC8888, mid, tightRB, srcC8888, width, 1);
1421 }
1422 } else {
1423 // if we aren't flipping Y then we have no reason to be here other than doing
1424 // conversions for 8888 (r/b swap or upm).
1425 GrAssert(c8888IsValid);
1426 uint32_t* b32 = reinterpret_cast<uint32_t*>(buffer);
1427 SkConvertConfig8888Pixels(b32, rowBytes, dstC8888,
1428 b32, rowBytes, srcC8888,
1429 width, height);
1430 }
1431 }
1432 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001433}
1434
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001435void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1436 GrAssert(target);
1437 ASSERT_OWNED_RESOURCE(target);
1438 // In the future we may track whether there are any pending draws to this
1439 // target. We don't today so we always perform a flush. We don't promise
1440 // this to our clients, though.
1441 this->flush();
1442 fGpu->resolveRenderTarget(target);
1443}
1444
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001445void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst) {
1446 if (NULL == src || NULL == dst) {
1447 return;
1448 }
1449 ASSERT_OWNED_RESOURCE(src);
1450
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001451 // Writes pending to the source texture are not tracked, so a flush
1452 // is required to ensure that the copy captures the most recent contents
1453 // of the source texture. See similar behaviour in
1454 // GrContext::resolveRenderTarget.
1455 this->flush();
1456
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001457 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001458 GrDrawState* drawState = fGpu->drawState();
1459 drawState->setRenderTarget(dst);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001460 GrMatrix sampleM;
1461 sampleM.setIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001462 drawState->sampler(0)->reset(sampleM);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001463 drawState->createTextureEffect(0, src);
bsalomon@google.com5db3b6c2012-01-12 20:38:57 +00001464 SkRect rect = SkRect::MakeXYWH(0, 0,
1465 SK_Scalar1 * src->width(),
1466 SK_Scalar1 * src->height());
bsalomon@google.come3d32162012-07-20 13:37:06 +00001467 fGpu->drawSimpleRect(rect, NULL);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001468}
1469
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001470void GrContext::writeRenderTargetPixels(GrRenderTarget* target,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001471 int left, int top, int width, int height,
1472 GrPixelConfig config,
1473 const void* buffer,
1474 size_t rowBytes,
1475 uint32_t flags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001476 SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001477 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001478
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001479 if (NULL == target) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001480 target = fDrawState->getRenderTarget();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001481 if (NULL == target) {
1482 return;
1483 }
1484 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001485
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001486 // TODO: when underlying api has a direct way to do this we should use it (e.g. glDrawPixels on
1487 // desktop GL).
1488
1489 // We will always call some form of writeTexturePixels and we will pass our flags on to it.
1490 // Thus, we don't perform a flush here since that call will do it (if the kNoFlush flag isn't
1491 // set.)
bsalomon@google.com27847de2011-02-22 20:59:41 +00001492
bsalomon@google.com0342a852012-08-20 19:22:38 +00001493 // If the RT is also a texture and we don't have to premultiply then take the texture path.
1494 // We expect to be at least as fast or faster since it doesn't use an intermediate texture as
1495 // we do below.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001496
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001497#if !GR_MAC_BUILD
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001498 // At least some drivers on the Mac get confused when glTexImage2D is called on a texture
1499 // attached to an FBO. The FBO still sees the old image. TODO: determine what OS versions and/or
1500 // HW is affected.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001501 if (NULL != target->asTexture() && !(kUnpremul_PixelOpsFlag & flags)) {
1502 this->writeTexturePixels(target->asTexture(),
1503 left, top, width, height,
1504 config, buffer, rowBytes, flags);
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001505 return;
1506 }
1507#endif
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001508 SkAutoTUnref<GrCustomStage> stage;
1509 bool swapRAndB = (fGpu->preferredReadPixelsConfig(config) == GrPixelConfigSwapRAndB(config));
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001510
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001511 GrPixelConfig textureConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001512 if (swapRAndB) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001513 textureConfig = GrPixelConfigSwapRAndB(config);
1514 } else {
1515 textureConfig = config;
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001516 }
1517
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001518 GrTextureDesc desc;
1519 desc.fWidth = width;
1520 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001521 desc.fConfig = textureConfig;
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001522 GrAutoScratchTexture ast(this, desc);
1523 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001524 if (NULL == texture) {
1525 return;
1526 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001527 // allocate a tmp buffer and sw convert the pixels to premul
1528 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
1529
1530 if (kUnpremul_PixelOpsFlag & flags) {
1531 if (kRGBA_8888_GrPixelConfig != config && kBGRA_8888_GrPixelConfig != config) {
1532 return;
1533 }
1534 stage.reset(this->createUPMToPMEffect(texture, swapRAndB));
1535 if (NULL == stage) {
1536 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1537 GR_DEBUGCODE(bool success = )
1538 grconfig_to_config8888(config, true, &srcConfig8888);
1539 GrAssert(success);
1540 GR_DEBUGCODE(success = )
1541 grconfig_to_config8888(config, false, &dstConfig8888);
1542 GrAssert(success);
1543 const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer);
1544 tmpPixels.reset(width * height);
1545 SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888,
1546 src, rowBytes, srcConfig8888,
1547 width, height);
1548 buffer = tmpPixels.get();
1549 rowBytes = 4 * width;
1550 }
1551 }
1552 if (NULL == stage) {
1553 stage.reset(GrConfigConversionEffect::Create(texture, swapRAndB));
1554 GrAssert(NULL != stage);
1555 }
1556
1557 this->writeTexturePixels(texture,
1558 0, 0, width, height,
1559 textureConfig, buffer, rowBytes,
1560 flags & ~kUnpremul_PixelOpsFlag);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001561
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001562 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001563 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001564
1565 GrMatrix matrix;
1566 matrix.setTranslate(GrIntToScalar(left), GrIntToScalar(top));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001567 drawState->setViewMatrix(matrix);
1568 drawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001569
bsalomon@google.com5c638652011-07-18 19:31:59 +00001570 matrix.setIDiv(texture->width(), texture->height());
bsalomon@google.comb8670992012-07-25 21:27:09 +00001571 drawState->sampler(0)->reset(matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001572 drawState->sampler(0)->setCustomStage(stage);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001573
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001574 fGpu->drawSimpleRect(GrRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)), NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001575}
1576////////////////////////////////////////////////////////////////////////////////
1577
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001578void GrContext::setPaint(const GrPaint& paint) {
tomhudson@google.comcb325ce2012-07-11 14:41:19 +00001579 GrAssert(fDrawState->stagesDisabled());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001580
1581 for (int i = 0; i < GrPaint::kMaxTextures; ++i) {
1582 int s = i + GrPaint::kFirstTextureStage;
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001583 if (paint.isTextureStageEnabled(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001584 *fDrawState->sampler(s) = paint.getTextureSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001585 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001586 }
1587
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001588 fDrawState->setFirstCoverageStage(GrPaint::kFirstMaskStage);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001589
1590 for (int i = 0; i < GrPaint::kMaxMasks; ++i) {
1591 int s = i + GrPaint::kFirstMaskStage;
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001592 if (paint.isMaskStageEnabled(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001593 *fDrawState->sampler(s) = paint.getMaskSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001594 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001595 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001596
bsalomon@google.com26936d02012-03-19 13:06:19 +00001597 // disable all stages not accessible via the paint
1598 for (int s = GrPaint::kTotalStages; s < GrDrawState::kNumStages; ++s) {
tomhudson@google.com676e6602012-07-10 17:21:48 +00001599 fDrawState->disableStage(s);
bsalomon@google.com26936d02012-03-19 13:06:19 +00001600 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001601
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001602 fDrawState->setColor(paint.fColor);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001603
1604 if (paint.fDither) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001605 fDrawState->enableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001606 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001607 fDrawState->disableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001608 }
1609 if (paint.fAntiAlias) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001610 fDrawState->enableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001611 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001612 fDrawState->disableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001613 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001614 if (paint.fColorMatrixEnabled) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001615 fDrawState->enableState(GrDrawState::kColorMatrix_StateBit);
1616 fDrawState->setColorMatrix(paint.fColorMatrix);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001617 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001618 fDrawState->disableState(GrDrawState::kColorMatrix_StateBit);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001619 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001620 fDrawState->setBlendFunc(paint.fSrcBlendCoeff, paint.fDstBlendCoeff);
1621 fDrawState->setColorFilter(paint.fColorFilterColor, paint.fColorFilterXfermode);
1622 fDrawState->setCoverage(paint.fCoverage);
reed@google.com4b2d3f32012-05-15 18:05:50 +00001623#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
bsalomon@google.come3d32162012-07-20 13:37:06 +00001624 if ((paint.hasMask() || 0xff != paint.fCoverage) &&
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001625 !fGpu->canApplyCoverage()) {
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001626 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1627 }
bsalomon@google.com95cd7bd2012-03-28 15:35:05 +00001628#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001629}
1630
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001631GrDrawTarget* GrContext::prepareToDraw(const GrPaint* paint, BufferedDraw buffered) {
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001632 if (kNo_BufferedDraw == buffered && kYes_BufferedDraw == fLastDrawWasBuffered) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001633 this->flushDrawBuffer();
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001634 fLastDrawWasBuffered = kNo_BufferedDraw;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001635 }
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001636 if (NULL != paint) {
1637 this->setPaint(*paint);
1638 }
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001639 if (kYes_BufferedDraw == buffered) {
1640 fDrawBuffer->setClip(fGpu->getClip());
1641 fLastDrawWasBuffered = kYes_BufferedDraw;
1642 return fDrawBuffer;
1643 } else {
1644 GrAssert(kNo_BufferedDraw == buffered);
1645 return fGpu;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001646 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001647}
1648
robertphillips@google.com72176b22012-05-23 13:19:12 +00001649/*
1650 * This method finds a path renderer that can draw the specified path on
1651 * the provided target.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001652 * Due to its expense, the software path renderer has split out so it can
robertphillips@google.com72176b22012-05-23 13:19:12 +00001653 * can be individually allowed/disallowed via the "allowSW" boolean.
1654 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001655GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
bsalomon@google.com289533a2011-10-27 12:34:25 +00001656 GrPathFill fill,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001657 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001658 bool antiAlias,
1659 bool allowSW) {
bsalomon@google.com30085192011-08-19 15:42:31 +00001660 if (NULL == fPathRendererChain) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001661 fPathRendererChain =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001662 SkNEW_ARGS(GrPathRendererChain,
1663 (this, GrPathRendererChain::kNone_UsageFlag));
bsalomon@google.com30085192011-08-19 15:42:31 +00001664 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001665
1666 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path, fill,
1667 target,
1668 antiAlias);
1669
1670 if (NULL == pr && allowSW) {
1671 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001672 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001673 }
1674
1675 pr = fSoftwarePathRenderer;
1676 }
1677
1678 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001679}
1680
bsalomon@google.com27847de2011-02-22 20:59:41 +00001681////////////////////////////////////////////////////////////////////////////////
1682
bsalomon@google.com27847de2011-02-22 20:59:41 +00001683void GrContext::setRenderTarget(GrRenderTarget* target) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001684 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001685 fDrawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001686}
1687
1688GrRenderTarget* GrContext::getRenderTarget() {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001689 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001690}
1691
1692const GrRenderTarget* GrContext::getRenderTarget() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001693 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001694}
1695
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001696bool GrContext::isConfigRenderable(GrPixelConfig config) const {
1697 return fGpu->isConfigRenderable(config);
1698}
1699
bsalomon@google.com27847de2011-02-22 20:59:41 +00001700const GrMatrix& GrContext::getMatrix() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001701 return fDrawState->getViewMatrix();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001702}
1703
1704void GrContext::setMatrix(const GrMatrix& m) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001705 fDrawState->setViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001706}
1707
1708void GrContext::concatMatrix(const GrMatrix& m) const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001709 fDrawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001710}
1711
1712static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
1713 intptr_t mask = 1 << shift;
1714 if (pred) {
1715 bits |= mask;
1716 } else {
1717 bits &= ~mask;
1718 }
1719 return bits;
1720}
1721
bsalomon@google.com583a1e32011-08-17 13:42:46 +00001722GrContext::GrContext(GrGpu* gpu) {
bsalomon@google.comc0af3172012-06-15 14:10:09 +00001723 ++THREAD_INSTANCE_COUNT;
1724
bsalomon@google.com27847de2011-02-22 20:59:41 +00001725 fGpu = gpu;
1726 fGpu->ref();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001727 fGpu->setContext(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001728
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001729 fDrawState = SkNEW(GrDrawState);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001730 fGpu->setDrawState(fDrawState);
1731
bsalomon@google.com30085192011-08-19 15:42:31 +00001732 fPathRendererChain = NULL;
robertphillips@google.com72176b22012-05-23 13:19:12 +00001733 fSoftwarePathRenderer = NULL;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001734
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001735 fTextureCache = SkNEW_ARGS(GrResourceCache,
1736 (MAX_TEXTURE_CACHE_COUNT,
1737 MAX_TEXTURE_CACHE_BYTES));
1738 fFontCache = SkNEW_ARGS(GrFontCache, (fGpu));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001739
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001740 fLastDrawWasBuffered = kNo_BufferedDraw;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001741
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001742 fDrawBuffer = NULL;
1743 fDrawBufferVBAllocPool = NULL;
1744 fDrawBufferIBAllocPool = NULL;
1745
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001746 fAARectRenderer = SkNEW(GrAARectRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001747
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001748 fDidTestPMConversions = false;
1749
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001750 this->setupDrawBuffer();
1751}
1752
1753void GrContext::setupDrawBuffer() {
1754
1755 GrAssert(NULL == fDrawBuffer);
1756 GrAssert(NULL == fDrawBufferVBAllocPool);
1757 GrAssert(NULL == fDrawBufferIBAllocPool);
1758
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001759 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001760 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001761 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001762 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001763 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001764 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001765 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001766 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001767
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001768 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com471d4712011-08-23 15:45:25 +00001769 fDrawBufferVBAllocPool,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001770 fDrawBufferIBAllocPool));
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001771
bsalomon@google.com27847de2011-02-22 20:59:41 +00001772 fDrawBuffer->setQuadIndexBuffer(this->getQuadIndexBuffer());
bsalomon@google.com1015e032012-06-25 18:41:04 +00001773 if (fDrawBuffer) {
1774 fDrawBuffer->setAutoFlushTarget(fGpu);
1775 fDrawBuffer->setDrawState(fDrawState);
1776 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001777}
1778
bsalomon@google.com27847de2011-02-22 20:59:41 +00001779GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) {
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001780 return prepareToDraw(&paint, DEFAULT_BUFFERING);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001781}
1782
1783const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1784 return fGpu->getQuadIndexBuffer();
1785}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001786
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001787namespace {
1788void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
1789 GrConfigConversionEffect::PMConversion pmToUPM;
1790 GrConfigConversionEffect::PMConversion upmToPM;
1791 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
1792 *pmToUPMValue = pmToUPM;
1793 *upmToPMValue = upmToPM;
1794}
1795}
1796
1797GrCustomStage* GrContext::createPMToUPMEffect(GrTexture* texture, bool swapRAndB) {
1798 if (!fDidTestPMConversions) {
1799 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
1800 }
1801 GrConfigConversionEffect::PMConversion pmToUPM =
1802 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
1803 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
1804 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM);
1805 } else {
1806 return NULL;
1807 }
1808}
1809
1810GrCustomStage* GrContext::createUPMToPMEffect(GrTexture* texture, bool swapRAndB) {
1811 if (!fDidTestPMConversions) {
1812 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
1813 }
1814 GrConfigConversionEffect::PMConversion upmToPM =
1815 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
1816 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
1817 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM);
1818 } else {
1819 return NULL;
1820 }
1821}
1822
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001823GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture,
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001824 bool canClobberSrc,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001825 const SkRect& rect,
1826 float sigmaX, float sigmaY) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001827 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001828 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001829 AutoMatrix avm(this, GrMatrix::I());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001830 SkIRect clearRect;
bsalomon@google.comb505a122012-05-31 18:40:36 +00001831 int scaleFactorX, radiusX;
1832 int scaleFactorY, radiusY;
1833 sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX);
1834 sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY);
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001835
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001836 SkRect srcRect(rect);
1837 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
1838 srcRect.roundOut();
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001839 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001840 static_cast<float>(scaleFactorY));
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001841
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001842 AutoClip acs(this, srcRect);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001843
bsalomon@google.com0342a852012-08-20 19:22:38 +00001844 GrAssert(kBGRA_8888_GrPixelConfig == srcTexture->config() ||
1845 kRGBA_8888_GrPixelConfig == srcTexture->config() ||
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001846 kAlpha_8_GrPixelConfig == srcTexture->config());
1847
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001848 GrTextureDesc desc;
1849 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1850 desc.fWidth = SkScalarFloorToInt(srcRect.width());
1851 desc.fHeight = SkScalarFloorToInt(srcRect.height());
1852 desc.fConfig = srcTexture->config();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001853
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001854 GrAutoScratchTexture temp1, temp2;
1855 GrTexture* dstTexture = temp1.set(this, desc);
1856 GrTexture* tempTexture = canClobberSrc ? srcTexture : temp2.set(this, desc);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001857
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001858 GrPaint paint;
1859 paint.reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +00001860 paint.textureSampler(0)->textureParams()->setBilerp(true);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001861
1862 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
1863 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1864 srcTexture->height());
1865 this->setRenderTarget(dstTexture->asRenderTarget());
1866 SkRect dstRect(srcRect);
1867 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
1868 i < scaleFactorY ? 0.5f : 1.0f);
tomhudson@google.comaa72eab2012-07-19 18:01:07 +00001869 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
1870 (srcTexture)))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001871 this->drawRectToRect(paint, dstRect, srcRect);
1872 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001873 srcTexture = dstTexture;
1874 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001875 }
1876
robertphillips@google.com7a396332012-05-10 15:11:27 +00001877 SkIRect srcIRect;
1878 srcRect.roundOut(&srcIRect);
1879
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001880 if (sigmaX > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001881 if (scaleFactorX > 1) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001882 // Clear out a radius to the right of the srcRect to prevent the
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001883 // X convolution from reading garbage.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001884 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001885 radiusX, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001886 this->clear(&clearRect, 0x0);
1887 }
1888
1889 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001890 GrDrawTarget* target = this->prepareToDraw(NULL, DEFAULT_BUFFERING);
1891 convolve_gaussian(target, srcTexture, srcRect, sigmaX, radiusX,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001892 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001893 srcTexture = dstTexture;
1894 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001895 }
1896
1897 if (sigmaY > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001898 if (scaleFactorY > 1 || sigmaX > 0.0f) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001899 // Clear out a radius below the srcRect to prevent the Y
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001900 // convolution from reading garbage.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001901 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001902 srcIRect.width(), radiusY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001903 this->clear(&clearRect, 0x0);
1904 }
1905
1906 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001907 GrDrawTarget* target = this->prepareToDraw(NULL, DEFAULT_BUFFERING);
1908 convolve_gaussian(target, srcTexture, srcRect, sigmaY, radiusY,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001909 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001910 srcTexture = dstTexture;
1911 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001912 }
1913
1914 if (scaleFactorX > 1 || scaleFactorY > 1) {
1915 // Clear one pixel to the right and below, to accommodate bilinear
1916 // upsampling.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001917 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
robertphillips@google.com7a396332012-05-10 15:11:27 +00001918 srcIRect.width() + 1, 1);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001919 this->clear(&clearRect, 0x0);
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001920 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
robertphillips@google.com7a396332012-05-10 15:11:27 +00001921 1, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001922 this->clear(&clearRect, 0x0);
1923 // FIXME: This should be mitchell, not bilinear.
bsalomon@google.comb8670992012-07-25 21:27:09 +00001924 paint.textureSampler(0)->textureParams()->setBilerp(true);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001925 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1926 srcTexture->height());
1927 this->setRenderTarget(dstTexture->asRenderTarget());
tomhudson@google.comaa72eab2012-07-19 18:01:07 +00001928 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
1929 (srcTexture)))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001930 SkRect dstRect(srcRect);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001931 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001932 this->drawRectToRect(paint, dstRect, srcRect);
1933 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001934 srcTexture = dstTexture;
1935 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001936 }
1937 this->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001938 if (srcTexture == temp1.texture()) {
1939 return temp1.detach();
1940 } else if (srcTexture == temp2.texture()) {
1941 return temp2.detach();
1942 } else {
1943 srcTexture->ref();
1944 return srcTexture;
1945 }
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001946}
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001947
bsalomon@google.comc4364992011-11-07 15:54:49 +00001948///////////////////////////////////////////////////////////////////////////////
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001949#if GR_DEBUG
1950void GrContext::printCacheStats() const {
1951 fTextureCache->printStats();
1952}
1953#endif