blob: b9314c8e7b5901d56f67f56276443608b1425f7e [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;
robertphillips@google.comeb9b3e12012-09-11 12:50:53 +000098 fTextureCache = NULL;
bsalomon@google.com27847de2011-02-22 20:59:41 +000099 delete fFontCache;
100 delete fDrawBuffer;
101 delete fDrawBufferVBAllocPool;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000102 delete fDrawBufferIBAllocPool;
bsalomon@google.com30085192011-08-19 15:42:31 +0000103
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000104 fAARectRenderer->unref();
105
bsalomon@google.com205d4602011-04-25 12:43:45 +0000106 fGpu->unref();
bsalomon@google.com30085192011-08-19 15:42:31 +0000107 GrSafeUnref(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000108 GrSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000109 fDrawState->unref();
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000110
111 --THREAD_INSTANCE_COUNT;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000112}
113
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000114void GrContext::contextLost() {
junov@google.com53a55842011-06-08 22:55:10 +0000115 contextDestroyed();
116 this->setupDrawBuffer();
117}
118
119void GrContext::contextDestroyed() {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000120 // abandon first to so destructors
121 // don't try to free the resources in the API.
122 fGpu->abandonResources();
123
bsalomon@google.com30085192011-08-19 15:42:31 +0000124 // a path renderer may be holding onto resources that
125 // are now unusable
126 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000127 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000128
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000129 delete fDrawBuffer;
130 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000131
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000132 delete fDrawBufferVBAllocPool;
133 fDrawBufferVBAllocPool = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000134
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000135 delete fDrawBufferIBAllocPool;
136 fDrawBufferIBAllocPool = NULL;
137
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000138 fAARectRenderer->reset();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000139
bsalomon@google.coma2921122012-08-28 12:34:17 +0000140 fTextureCache->purgeAllUnlocked();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000141 fFontCache->freeAll();
142 fGpu->markContextDirty();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000143}
144
145void GrContext::resetContext() {
146 fGpu->markContextDirty();
147}
148
149void GrContext::freeGpuResources() {
150 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000151
robertphillips@google.comff175842012-05-14 19:31:39 +0000152 fGpu->purgeResources();
153
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000154 fAARectRenderer->reset();
155
bsalomon@google.coma2921122012-08-28 12:34:17 +0000156 fTextureCache->purgeAllUnlocked();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000157 fFontCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000158 // a path renderer may be holding onto resources
159 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000160 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000161}
162
twiz@google.com05e70242012-01-27 19:12:00 +0000163size_t GrContext::getGpuTextureCacheBytes() const {
164 return fTextureCache->getCachedResourceBytes();
165}
166
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000167////////////////////////////////////////////////////////////////////////////////
168
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000169namespace {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000170
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000171void scale_rect(SkRect* rect, float xScale, float yScale) {
robertphillips@google.com5af56062012-04-27 15:39:52 +0000172 rect->fLeft = SkScalarMul(rect->fLeft, SkFloatToScalar(xScale));
173 rect->fTop = SkScalarMul(rect->fTop, SkFloatToScalar(yScale));
174 rect->fRight = SkScalarMul(rect->fRight, SkFloatToScalar(xScale));
175 rect->fBottom = SkScalarMul(rect->fBottom, SkFloatToScalar(yScale));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000176}
177
bsalomon@google.comb505a122012-05-31 18:40:36 +0000178float adjust_sigma(float sigma, int *scaleFactor, int *radius) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000179 *scaleFactor = 1;
180 while (sigma > MAX_BLUR_SIGMA) {
181 *scaleFactor *= 2;
182 sigma *= 0.5f;
183 }
bsalomon@google.comb505a122012-05-31 18:40:36 +0000184 *radius = static_cast<int>(ceilf(sigma * 3.0f));
185 GrAssert(*radius <= GrConvolutionEffect::kMaxKernelRadius);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000186 return sigma;
187}
188
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000189void convolve_gaussian(GrDrawTarget* target,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000190 GrTexture* texture,
191 const SkRect& rect,
192 float sigma,
193 int radius,
194 Gr1DKernelEffect::Direction direction) {
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000195 GrRenderTarget* rt = target->drawState()->getRenderTarget();
196 GrDrawTarget::AutoStateRestore asr(target, GrDrawTarget::kReset_ASRInit);
197 GrDrawState* drawState = target->drawState();
198 drawState->setRenderTarget(rt);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000199 GrMatrix sampleM;
200 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000201 drawState->sampler(0)->reset(sampleM);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000202 SkAutoTUnref<GrConvolutionEffect> conv(SkNEW_ARGS(GrConvolutionEffect,
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000203 (texture, direction, radius,
204 sigma)));
bsalomon@google.comb505a122012-05-31 18:40:36 +0000205 drawState->sampler(0)->setCustomStage(conv);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000206 target->drawSimpleRect(rect, NULL);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000207}
208
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000209}
210
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000211
212GrTexture* GrContext::findTexture(const GrCacheKey& key) {
213 return static_cast<GrTexture*>(fTextureCache->find(key.key()));
214}
215
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000216GrTexture* GrContext::findTexture(const GrTextureDesc& desc,
217 const GrCacheData& cacheData,
218 const GrTextureParams* params) {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000219 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000220 GrResource* resource = fTextureCache->find(resourceKey);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000221 return static_cast<GrTexture*>(resource);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000222}
223
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000224bool GrContext::isTextureInCache(const GrTextureDesc& desc,
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000225 const GrCacheData& cacheData,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000226 const GrTextureParams* params) const {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000227 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000228 return fTextureCache->hasKey(resourceKey);
229}
230
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000231void GrContext::addStencilBuffer(GrStencilBuffer* sb) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000232 ASSERT_OWNED_RESOURCE(sb);
robertphillips@google.com46a86002012-08-08 10:42:44 +0000233
234 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(sb->width(),
235 sb->height(),
236 sb->numSamples());
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000237 fTextureCache->create(resourceKey, sb);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000238}
239
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000240GrStencilBuffer* GrContext::findStencilBuffer(int width, int height,
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000241 int sampleCnt) {
robertphillips@google.com46a86002012-08-08 10:42:44 +0000242 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(width,
243 height,
244 sampleCnt);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000245 GrResource* resource = fTextureCache->find(resourceKey);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000246 return static_cast<GrStencilBuffer*>(resource);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000247}
248
bsalomon@google.com27847de2011-02-22 20:59:41 +0000249static void stretchImage(void* dst,
250 int dstW,
251 int dstH,
252 void* src,
253 int srcW,
254 int srcH,
255 int bpp) {
256 GrFixed dx = (srcW << 16) / dstW;
257 GrFixed dy = (srcH << 16) / dstH;
258
259 GrFixed y = dy >> 1;
260
261 int dstXLimit = dstW*bpp;
262 for (int j = 0; j < dstH; ++j) {
263 GrFixed x = dx >> 1;
264 void* srcRow = (uint8_t*)src + (y>>16)*srcW*bpp;
265 void* dstRow = (uint8_t*)dst + j*dstW*bpp;
266 for (int i = 0; i < dstXLimit; i += bpp) {
267 memcpy((uint8_t*) dstRow + i,
268 (uint8_t*) srcRow + (x>>16)*bpp,
269 bpp);
270 x += dx;
271 }
272 y += dy;
273 }
274}
275
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000276// The desired texture is NPOT and tiled but that isn't supported by
robertphillips@google.com3319f332012-08-13 18:00:36 +0000277// the current hardware. Resize the texture to be a POT
278GrTexture* GrContext::createResizedTexture(const GrTextureDesc& desc,
279 const GrCacheData& cacheData,
280 void* srcData,
281 size_t rowBytes,
282 bool needsFiltering) {
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000283 SkAutoTUnref<GrTexture> clampedTexture(this->findTexture(desc, cacheData, NULL));
robertphillips@google.com3319f332012-08-13 18:00:36 +0000284
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000285 if (NULL == clampedTexture) {
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000286 clampedTexture.reset(
287 this->createTexture(NULL, desc, cacheData, srcData, rowBytes));
288
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000289 GrAssert(NULL != clampedTexture);
290 if (NULL == clampedTexture) {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000291 return NULL;
292 }
293 }
294 GrTextureDesc rtDesc = desc;
295 rtDesc.fFlags = rtDesc.fFlags |
296 kRenderTarget_GrTextureFlagBit |
297 kNoStencil_GrTextureFlagBit;
298 rtDesc.fWidth = GrNextPow2(GrMax(desc.fWidth, 64));
299 rtDesc.fHeight = GrNextPow2(GrMax(desc.fHeight, 64));
300
301 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
302
303 if (NULL != texture) {
304 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
305 GrDrawState* drawState = fGpu->drawState();
306 drawState->setRenderTarget(texture->asRenderTarget());
307
308 // if filtering is not desired then we want to ensure all
309 // texels in the resampled image are copies of texels from
310 // the original.
311 drawState->sampler(0)->reset(SkShader::kClamp_TileMode,
312 needsFiltering);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000313 drawState->createTextureEffect(0, clampedTexture);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000314
315 static const GrVertexLayout layout =
316 GrDrawTarget::StageTexCoordVertexLayoutBit(0,0);
317 GrDrawTarget::AutoReleaseGeometry arg(fGpu, layout, 4, 0);
318
319 if (arg.succeeded()) {
320 GrPoint* verts = (GrPoint*) arg.vertices();
321 verts[0].setIRectFan(0, 0,
322 texture->width(),
323 texture->height(),
324 2*sizeof(GrPoint));
325 verts[1].setIRectFan(0, 0, 1, 1, 2*sizeof(GrPoint));
326 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType,
327 0, 4);
328 }
329 texture->releaseRenderTarget();
330 } else {
331 // TODO: Our CPU stretch doesn't filter. But we create separate
332 // stretched textures when the sampler state is either filtered or
333 // not. Either implement filtered stretch blit on CPU or just create
334 // one when FBO case fails.
335
336 rtDesc.fFlags = kNone_GrTextureFlags;
337 // no longer need to clamp at min RT size.
338 rtDesc.fWidth = GrNextPow2(desc.fWidth);
339 rtDesc.fHeight = GrNextPow2(desc.fHeight);
340 int bpp = GrBytesPerPixel(desc.fConfig);
341 SkAutoSMalloc<128*128*4> stretchedPixels(bpp *
342 rtDesc.fWidth *
343 rtDesc.fHeight);
344 stretchImage(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
345 srcData, desc.fWidth, desc.fHeight, bpp);
346
347 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
348
349 GrTexture* texture = fGpu->createTexture(rtDesc,
350 stretchedPixels.get(),
351 stretchedRowBytes);
352 GrAssert(NULL != texture);
353 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000354
355 return texture;
356}
357
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000358GrTexture* GrContext::createTexture(
bsalomon@google.comb8670992012-07-25 21:27:09 +0000359 const GrTextureParams* params,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000360 const GrTextureDesc& desc,
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000361 const GrCacheData& cacheData,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000362 void* srcData,
363 size_t rowBytes) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000364 SK_TRACE_EVENT0("GrContext::createAndLockTexture");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000365
366#if GR_DUMP_TEXTURE_UPLOAD
367 GrPrintf("GrContext::createAndLockTexture [%d %d]\n", desc.fWidth, desc.fHeight);
368#endif
369
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000370 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000371
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +0000372 SkAutoTUnref<GrTexture> texture;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000373 if (GrTexture::NeedsResizing(resourceKey)) {
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +0000374 texture.reset(this->createResizedTexture(desc, cacheData,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000375 srcData, rowBytes,
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +0000376 GrTexture::NeedsFiltering(resourceKey)));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000377 } else {
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +0000378 texture.reset(fGpu->createTexture(desc, srcData, rowBytes));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000379 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000380
381 if (NULL != texture) {
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000382 fTextureCache->create(resourceKey, texture);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000383 }
384
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000385 return texture;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000386}
387
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000388GrTexture* GrContext::lockScratchTexture(const GrTextureDesc& inDesc,
389 ScratchTexMatch match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000390 GrTextureDesc desc = inDesc;
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000391 GrCacheData cacheData(GrCacheData::kScratch_CacheID);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000392
rileya@google.com2afb8ec2012-08-23 14:08:57 +0000393 GrAssert((desc.fFlags & kRenderTarget_GrTextureFlagBit) ||
394 !(desc.fFlags & kNoStencil_GrTextureFlagBit));
395
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000396 if (kExact_ScratchTexMatch != match) {
397 // bin by pow2 with a reasonable min
398 static const int MIN_SIZE = 256;
399 desc.fWidth = GrMax(MIN_SIZE, GrNextPow2(desc.fWidth));
400 desc.fHeight = GrMax(MIN_SIZE, GrNextPow2(desc.fHeight));
401 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000402
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000403 GrResource* resource = NULL;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000404 int origWidth = desc.fWidth;
405 int origHeight = desc.fHeight;
406 bool doubledW = false;
407 bool doubledH = false;
408
409 do {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000410 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL, desc, cacheData, true);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000411 resource = fTextureCache->find(key);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000412 // if we miss, relax the fit of the flags...
413 // then try doubling width... then height.
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000414 if (NULL != resource || kExact_ScratchTexMatch == match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000415 break;
416 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000417 // We no longer try to reuse textures that were previously used as render targets in
rileya@google.com2afb8ec2012-08-23 14:08:57 +0000418 // situations where no RT is needed; doing otherwise can confuse the video driver and
419 // cause significant performance problems in some cases.
420 if (desc.fFlags & kNoStencil_GrTextureFlagBit) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000421 desc.fFlags = desc.fFlags & ~kNoStencil_GrTextureFlagBit;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000422 } else if (!doubledW) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000423 desc.fFlags = inDesc.fFlags;
424 desc.fWidth *= 2;
425 doubledW = true;
426 } else if (!doubledH) {
427 desc.fFlags = inDesc.fFlags;
428 desc.fWidth = origWidth;
429 desc.fHeight *= 2;
430 doubledH = true;
431 } else {
432 break;
433 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000434
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000435 } while (true);
436
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000437 if (NULL == resource) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000438 desc.fFlags = inDesc.fFlags;
439 desc.fWidth = origWidth;
440 desc.fHeight = origHeight;
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +0000441 SkAutoTUnref<GrTexture> texture(fGpu->createTexture(desc, NULL, 0));
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000442 if (NULL != texture) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000443 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000444 texture->desc(),
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000445 cacheData,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000446 true);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000447 fTextureCache->create(key, texture);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000448 resource = texture;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000449 }
450 }
451
452 // If the caller gives us the same desc/sampler twice we don't want
453 // to return the same texture the second time (unless it was previously
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000454 // released). So make it exclusive to hide it from future searches.
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000455 if (NULL != resource) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000456 fTextureCache->makeExclusive(resource->getCacheEntry());
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000457 }
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000458
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000459 return static_cast<GrTexture*>(resource);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000460}
461
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000462void GrContext::addExistingTextureToCache(GrTexture* texture) {
463
464 if (NULL == texture) {
465 return;
466 }
467
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000468 // This texture should already have a cache entry since it was once
469 // attached
470 GrAssert(NULL != texture->getCacheEntry());
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000471
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000472 // Conceptually, the cache entry is going to assume responsibility
473 // for the creation ref.
474 GrAssert(1 == texture->getRefCnt());
475
476 // Since this texture came from an AutoScratchTexture it should
477 // still be in the exclusive pile
478 fTextureCache->makeNonExclusive(texture->getCacheEntry());
479
robertphillips@google.com50a035d2012-09-07 19:44:33 +0000480 this->purgeCache();
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000481}
482
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000483
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000484void GrContext::unlockScratchTexture(GrTexture* texture) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000485 ASSERT_OWNED_RESOURCE(texture);
486 GrAssert(NULL != texture->getCacheEntry());
487
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000488 // If this is a scratch texture we detached it from the cache
489 // while it was locked (to avoid two callers simultaneously getting
490 // the same texture).
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000491 if (GrTexture::IsScratchTexture(texture->getCacheEntry()->key())) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000492 fTextureCache->makeNonExclusive(texture->getCacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000493 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000494
robertphillips@google.com50a035d2012-09-07 19:44:33 +0000495 this->purgeCache();
496}
497
498void GrContext::purgeCache() {
robertphillips@google.comeb9b3e12012-09-11 12:50:53 +0000499 if (NULL != fTextureCache) {
500 fTextureCache->purgeAsNeeded();
501 }
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000502}
503
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000504GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000505 void* srcData,
506 size_t rowBytes) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000507 GrTextureDesc descCopy = descIn;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000508 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000509}
510
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000511void GrContext::getTextureCacheLimits(int* maxTextures,
512 size_t* maxTextureBytes) const {
513 fTextureCache->getLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000514}
515
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000516void GrContext::setTextureCacheLimits(int maxTextures, size_t maxTextureBytes) {
517 fTextureCache->setLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000518}
519
bsalomon@google.com91958362011-06-13 17:58:13 +0000520int GrContext::getMaxTextureSize() const {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000521 return fGpu->getCaps().maxTextureSize();
bsalomon@google.com91958362011-06-13 17:58:13 +0000522}
523
524int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000525 return fGpu->getCaps().maxRenderTargetSize();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000526}
527
528///////////////////////////////////////////////////////////////////////////////
529
bsalomon@google.come269f212011-11-07 13:29:52 +0000530GrTexture* GrContext::createPlatformTexture(const GrPlatformTextureDesc& desc) {
531 return fGpu->createPlatformTexture(desc);
532}
533
534GrRenderTarget* GrContext::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
535 return fGpu->createPlatformRenderTarget(desc);
536}
537
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000538///////////////////////////////////////////////////////////////////////////////
539
bsalomon@google.comb8670992012-07-25 21:27:09 +0000540bool GrContext::supportsIndex8PixelConfig(const GrTextureParams* params,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000541 int width, int height) const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000542 const GrDrawTarget::Caps& caps = fGpu->getCaps();
bsalomon@google.comf6601872012-08-28 21:11:35 +0000543 if (!caps.eightBitPaletteSupport()) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000544 return false;
545 }
546
bsalomon@google.com27847de2011-02-22 20:59:41 +0000547 bool isPow2 = GrIsPow2(width) && GrIsPow2(height);
548
549 if (!isPow2) {
bsalomon@google.comb8670992012-07-25 21:27:09 +0000550 bool tiled = NULL != params && params->isTiled();
bsalomon@google.comf6601872012-08-28 21:11:35 +0000551 if (tiled && !caps.npotTextureTileSupport()) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000552 return false;
553 }
554 }
555 return true;
556}
557
558////////////////////////////////////////////////////////////////////////////////
559
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000560const GrClipData* GrContext::getClip() const {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000561 return fGpu->getClip();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000562}
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000563
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000564void GrContext::setClip(const GrClipData* clipData) {
565 fGpu->setClip(clipData);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000566 fDrawState->enableState(GrDrawState::kClip_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000567}
568
bsalomon@google.com27847de2011-02-22 20:59:41 +0000569////////////////////////////////////////////////////////////////////////////////
570
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000571void GrContext::clear(const GrIRect* rect,
572 const GrColor color,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000573 GrRenderTarget* target) {
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000574 this->prepareToDraw(NULL, DEFAULT_BUFFERING)->clear(rect, color, target);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000575}
576
577void GrContext::drawPaint(const GrPaint& paint) {
578 // set rect to be big enough to fill the space, but not super-huge, so we
579 // don't overflow fixed-point implementations
bsalomon@google.comd302f142011-03-03 13:54:13 +0000580 GrRect r;
581 r.setLTRB(0, 0,
582 GrIntToScalar(getRenderTarget()->width()),
583 GrIntToScalar(getRenderTarget()->height()));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000584 GrMatrix inverse;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000585 SkTLazy<GrPaint> tmpPaint;
586 const GrPaint* p = &paint;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000587 AutoMatrix am;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000588
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000589 // We attempt to map r by the inverse matrix and draw that. mapRect will
590 // map the four corners and bound them with a new rect. This will not
591 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000592 if (!this->getMatrix().hasPerspective()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000593 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000594 GrPrintf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000595 return;
596 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000597 inverse.mapRect(&r);
598 } else {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000599 if (paint.hasTextureOrMask()) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000600 tmpPaint.set(paint);
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000601 p = tmpPaint.get();
bsalomon@google.come3d32162012-07-20 13:37:06 +0000602 if (!tmpPaint.get()->preConcatSamplerMatricesWithInverse(fDrawState->getViewMatrix())) {
603 GrPrintf("Could not invert matrix\n");
604 }
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000605 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000606 am.set(this, GrMatrix::I());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000607 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000608 // by definition this fills the entire clip, no need for AA
609 if (paint.fAntiAlias) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000610 if (!tmpPaint.isValid()) {
611 tmpPaint.set(paint);
612 p = tmpPaint.get();
613 }
614 GrAssert(p == tmpPaint.get());
615 tmpPaint.get()->fAntiAlias = false;
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000616 }
617 this->drawRect(*p, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000618}
619
bsalomon@google.com205d4602011-04-25 12:43:45 +0000620////////////////////////////////////////////////////////////////////////////////
621
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000622namespace {
623inline bool disable_coverage_aa_for_blend(GrDrawTarget* target) {
624 return DISABLE_COVERAGE_AA_FOR_BLEND && !target->canApplyCoverage();
625}
626}
627
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000628////////////////////////////////////////////////////////////////////////////////
629
bsalomon@google.com27847de2011-02-22 20:59:41 +0000630/* create a triangle strip that strokes the specified triangle. There are 8
631 unique vertices, but we repreat the last 2 to close up. Alternatively we
632 could use an indices array, and then only send 8 verts, but not sure that
633 would be faster.
634 */
bsalomon@google.com205d4602011-04-25 12:43:45 +0000635static void setStrokeRectStrip(GrPoint verts[10], GrRect rect,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000636 GrScalar width) {
637 const GrScalar rad = GrScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000638 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000639
640 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
641 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
642 verts[2].set(rect.fRight - rad, rect.fTop + rad);
643 verts[3].set(rect.fRight + rad, rect.fTop - rad);
644 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
645 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
646 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
647 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
648 verts[8] = verts[0];
649 verts[9] = verts[1];
650}
651
reed@google.com20efde72011-05-09 17:00:02 +0000652/**
653 * Returns true if the rects edges are integer-aligned.
654 */
655static bool isIRect(const GrRect& r) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000656 return GrScalarIsInt(r.fLeft) && GrScalarIsInt(r.fTop) &&
reed@google.com20efde72011-05-09 17:00:02 +0000657 GrScalarIsInt(r.fRight) && GrScalarIsInt(r.fBottom);
658}
659
bsalomon@google.com205d4602011-04-25 12:43:45 +0000660static bool apply_aa_to_rect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000661 const GrRect& rect,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000662 GrScalar width,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000663 const GrMatrix* matrix,
664 GrMatrix* combinedMatrix,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000665 GrRect* devRect,
666 bool* useVertexCoverage) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000667 // we use a simple coverage ramp to do aa on axis-aligned rects
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000668 // we check if the rect will be axis-aligned, and the rect won't land on
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000669 // integer coords.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000670
bsalomon@google.coma3108262011-10-10 14:08:47 +0000671 // we are keeping around the "tweak the alpha" trick because
672 // it is our only hope for the fixed-pipe implementation.
673 // In a shader implementation we can give a separate coverage input
bsalomon@google.com289533a2011-10-27 12:34:25 +0000674 // TODO: remove this ugliness when we drop the fixed-pipe impl
bsalomon@google.coma3108262011-10-10 14:08:47 +0000675 *useVertexCoverage = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000676 if (!target->canTweakAlphaForCoverage()) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000677 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +0000678#if GR_DEBUG
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000679 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000680#endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000681 return false;
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000682 } else {
683 *useVertexCoverage = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000684 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000685 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000686 const GrDrawState& drawState = target->getDrawState();
687 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000688 return false;
689 }
690
bsalomon@google.com471d4712011-08-23 15:45:25 +0000691 if (0 == width && target->willUseHWAALines()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000692 return false;
693 }
694
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000695 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000696 return false;
697 }
698
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000699 if (NULL != matrix &&
bsalomon@google.com205d4602011-04-25 12:43:45 +0000700 !matrix->preservesAxisAlignment()) {
701 return false;
702 }
703
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000704 *combinedMatrix = drawState.getViewMatrix();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000705 if (NULL != matrix) {
706 combinedMatrix->preConcat(*matrix);
707 GrAssert(combinedMatrix->preservesAxisAlignment());
708 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000709
bsalomon@google.com205d4602011-04-25 12:43:45 +0000710 combinedMatrix->mapRect(devRect, rect);
711 devRect->sort();
712
713 if (width < 0) {
reed@google.com20efde72011-05-09 17:00:02 +0000714 return !isIRect(*devRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000715 } else {
716 return true;
717 }
718}
719
bsalomon@google.com27847de2011-02-22 20:59:41 +0000720void GrContext::drawRect(const GrPaint& paint,
721 const GrRect& rect,
722 GrScalar width,
723 const GrMatrix* matrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000724 SK_TRACE_EVENT0("GrContext::drawRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000725
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000726 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000727 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000728
bsalomon@google.com205d4602011-04-25 12:43:45 +0000729 GrRect devRect = rect;
730 GrMatrix combinedMatrix;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000731 bool useVertexCoverage;
bsalomon@google.com289533a2011-10-27 12:34:25 +0000732 bool needAA = paint.fAntiAlias &&
733 !this->getRenderTarget()->isMultisampled();
734 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix,
735 &combinedMatrix, &devRect,
736 &useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000737
738 if (doAA) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000739 GrDrawTarget::AutoDeviceCoordDraw adcd(target);
740 if (!adcd.succeeded()) {
741 return;
742 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000743 if (width >= 0) {
744 GrVec strokeSize;;
745 if (width > 0) {
746 strokeSize.set(width, width);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000747 combinedMatrix.mapVectors(&strokeSize, 1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000748 strokeSize.setAbs(strokeSize);
749 } else {
750 strokeSize.set(GR_Scalar1, GR_Scalar1);
751 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000752 fAARectRenderer->strokeAARect(this->getGpu(), target, devRect,
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000753 strokeSize, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000754 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000755 fAARectRenderer->fillAARect(this->getGpu(), target,
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000756 devRect, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000757 }
758 return;
759 }
760
bsalomon@google.com27847de2011-02-22 20:59:41 +0000761 if (width >= 0) {
762 // TODO: consider making static vertex buffers for these cases.
763 // Hairline could be done by just adding closing vertex to
764 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000765
bsalomon@google.com27847de2011-02-22 20:59:41 +0000766 static const int worstCaseVertCount = 10;
bsalomon@google.come3d32162012-07-20 13:37:06 +0000767 GrDrawTarget::AutoReleaseGeometry geo(target, 0, worstCaseVertCount, 0);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000768
769 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000770 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000771 return;
772 }
773
774 GrPrimitiveType primType;
775 int vertCount;
776 GrPoint* vertex = geo.positions();
777
778 if (width > 0) {
779 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000780 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000781 setStrokeRectStrip(vertex, rect, width);
782 } else {
783 // hairline
784 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000785 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000786 vertex[0].set(rect.fLeft, rect.fTop);
787 vertex[1].set(rect.fRight, rect.fTop);
788 vertex[2].set(rect.fRight, rect.fBottom);
789 vertex[3].set(rect.fLeft, rect.fBottom);
790 vertex[4].set(rect.fLeft, rect.fTop);
791 }
792
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000793 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000794 if (NULL != matrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000795 GrDrawState* drawState = target->drawState();
796 avmr.set(drawState);
797 drawState->preConcatViewMatrix(*matrix);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000798 drawState->preConcatSamplerMatrices(*matrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000799 }
800
801 target->drawNonIndexed(primType, 0, vertCount);
802 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000803#if GR_STATIC_RECT_VB
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000804 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
805 if (NULL == sqVB) {
806 GrPrintf("Failed to create static rect vb.\n");
807 return;
808 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000809 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000810 GrDrawState* drawState = target->drawState();
811 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000812 GrMatrix m;
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000813 m.setAll(rect.width(), 0, rect.fLeft,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000814 0, rect.height(), rect.fTop,
815 0, 0, GrMatrix::I()[8]);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000816
817 if (NULL != matrix) {
818 m.postConcat(*matrix);
819 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000820 drawState->preConcatViewMatrix(m);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000821 drawState->preConcatSamplerMatrices(m);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000822
bsalomon@google.com47059542012-06-06 20:51:20 +0000823 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000824#else
bsalomon@google.come3d32162012-07-20 13:37:06 +0000825 target->drawSimpleRect(rect, matrix);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000826#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000827 }
828}
829
830void GrContext::drawRectToRect(const GrPaint& paint,
831 const GrRect& dstRect,
832 const GrRect& srcRect,
833 const GrMatrix* dstMatrix,
834 const GrMatrix* srcMatrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000835 SK_TRACE_EVENT0("GrContext::drawRectToRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000836
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000837 // srcRect refers to paint's first texture
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000838 if (!paint.isTextureStageEnabled(0)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000839 drawRect(paint, dstRect, -1, dstMatrix);
840 return;
841 }
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000842
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000843 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000844
845#if GR_STATIC_RECT_VB
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000846 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000847 GrDrawState* drawState = target->drawState();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000848 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000849
850 GrMatrix m;
851
852 m.setAll(dstRect.width(), 0, dstRect.fLeft,
853 0, dstRect.height(), dstRect.fTop,
854 0, 0, GrMatrix::I()[8]);
855 if (NULL != dstMatrix) {
856 m.postConcat(*dstMatrix);
857 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000858 drawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000859
bsalomon@google.come3d32162012-07-20 13:37:06 +0000860 // we explicitly setup the correct coords for the first stage. The others
861 // must know about the view matrix change.
862 for (int s = 1; s < GrPaint::kTotalStages; ++s) {
863 if (drawState->isStageEnabled(s)) {
864 drawState->sampler(s)->preConcatMatrix(m);
865 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000866 }
867
bsalomon@google.com27847de2011-02-22 20:59:41 +0000868 m.setAll(srcRect.width(), 0, srcRect.fLeft,
869 0, srcRect.height(), srcRect.fTop,
870 0, 0, GrMatrix::I()[8]);
871 if (NULL != srcMatrix) {
872 m.postConcat(*srcMatrix);
873 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000874 drawState->sampler(GrPaint::kFirstTextureStage)->preConcatMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000875
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000876 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
877 if (NULL == sqVB) {
878 GrPrintf("Failed to create static rect vb.\n");
879 return;
880 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000881 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com47059542012-06-06 20:51:20 +0000882 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000883#else
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000884 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000885
tomhudson@google.com93813632011-10-27 20:21:16 +0000886 const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
887 const GrMatrix* srcMatrices[GrDrawState::kNumStages] = {NULL};
bsalomon@google.com27847de2011-02-22 20:59:41 +0000888 srcRects[0] = &srcRect;
889 srcMatrices[0] = srcMatrix;
890
bsalomon@google.come3d32162012-07-20 13:37:06 +0000891 target->drawRect(dstRect, dstMatrix, srcRects, srcMatrices);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000892#endif
893}
894
895void GrContext::drawVertices(const GrPaint& paint,
896 GrPrimitiveType primitiveType,
897 int vertexCount,
898 const GrPoint positions[],
899 const GrPoint texCoords[],
900 const GrColor colors[],
901 const uint16_t indices[],
902 int indexCount) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000903 SK_TRACE_EVENT0("GrContext::drawVertices");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000904
905 GrDrawTarget::AutoReleaseGeometry geo;
906
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000907 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000908 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000909
bsalomon@google.come3d32162012-07-20 13:37:06 +0000910 GrVertexLayout layout = 0;
911 if (NULL != texCoords) {
912 layout |= GrDrawTarget::StageTexCoordVertexLayoutBit(0, 0);
913 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000914 if (NULL != colors) {
915 layout |= GrDrawTarget::kColor_VertexLayoutBit;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000916 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000917 int vertexSize = GrDrawTarget::VertexSize(layout);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000918
919 if (sizeof(GrPoint) != vertexSize) {
920 if (!geo.set(target, layout, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000921 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000922 return;
923 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000924 int texOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000925 int colorOffset;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000926 GrDrawTarget::VertexSizeAndOffsetsByIdx(layout,
927 texOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000928 &colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000929 NULL,
930 NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000931 void* curVertex = geo.vertices();
932
933 for (int i = 0; i < vertexCount; ++i) {
934 *((GrPoint*)curVertex) = positions[i];
935
936 if (texOffsets[0] > 0) {
937 *(GrPoint*)((intptr_t)curVertex + texOffsets[0]) = texCoords[i];
938 }
939 if (colorOffset > 0) {
940 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
941 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000942 curVertex = (void*)((intptr_t)curVertex + vertexSize);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000943 }
944 } else {
945 target->setVertexSourceToArray(layout, positions, vertexCount);
946 }
947
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000948 // we don't currently apply offscreen AA to this path. Need improved
bsalomon@google.com91958362011-06-13 17:58:13 +0000949 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000950
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000951 if (NULL != indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000952 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000953 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000954 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000955 target->drawNonIndexed(primitiveType, 0, vertexCount);
956 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000957}
958
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000959///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com150d2842012-01-12 20:19:56 +0000960namespace {
961
bsalomon@google.com93c96602012-04-27 13:05:21 +0000962struct CircleVertex {
963 GrPoint fPos;
964 GrPoint fCenter;
965 GrScalar fOuterRadius;
966 GrScalar fInnerRadius;
967};
968
969/* Returns true if will map a circle to another circle. This can be true
970 * if the matrix only includes square-scale, rotation, translation.
971 */
972inline bool isSimilarityTransformation(const SkMatrix& matrix,
973 SkScalar tol = SK_ScalarNearlyZero) {
974 if (matrix.isIdentity() || matrix.getType() == SkMatrix::kTranslate_Mask) {
975 return true;
976 }
977 if (matrix.hasPerspective()) {
978 return false;
979 }
980
981 SkScalar mx = matrix.get(SkMatrix::kMScaleX);
982 SkScalar sx = matrix.get(SkMatrix::kMSkewX);
983 SkScalar my = matrix.get(SkMatrix::kMScaleY);
984 SkScalar sy = matrix.get(SkMatrix::kMSkewY);
985
986 if (mx == 0 && sx == 0 && my == 0 && sy == 0) {
987 return false;
988 }
989
990 // it has scales or skews, but it could also be rotation, check it out.
991 SkVector vec[2];
992 vec[0].set(mx, sx);
993 vec[1].set(sy, my);
994
995 return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) &&
996 SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(),
997 SkScalarSquare(tol));
998}
999
1000}
1001
1002// TODO: strokeWidth can't be larger than zero right now.
1003// It will be fixed when drawPath() can handle strokes.
1004void GrContext::drawOval(const GrPaint& paint,
1005 const GrRect& rect,
1006 SkScalar strokeWidth) {
bsalomon@google.com0982d352012-07-31 15:33:25 +00001007 GrAssert(strokeWidth <= 0);
1008 if (!isSimilarityTransformation(this->getMatrix()) ||
bsalomon@google.com93c96602012-04-27 13:05:21 +00001009 !paint.fAntiAlias ||
1010 rect.height() != rect.width()) {
1011 SkPath path;
1012 path.addOval(rect);
1013 GrPathFill fill = (strokeWidth == 0) ?
bsalomon@google.com0982d352012-07-31 15:33:25 +00001014 kHairLine_GrPathFill : kWinding_GrPathFill;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001015 this->internalDrawPath(paint, path, fill, NULL);
1016 return;
1017 }
1018
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001019 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
1020
bsalomon@google.com0982d352012-07-31 15:33:25 +00001021 GrDrawState* drawState = target->drawState();
1022 GrDrawState::AutoStageDisable atr(fDrawState);
1023 const GrMatrix vm = drawState->getViewMatrix();
1024
bsalomon@google.com93c96602012-04-27 13:05:21 +00001025 const GrRenderTarget* rt = drawState->getRenderTarget();
1026 if (NULL == rt) {
1027 return;
1028 }
1029
bsalomon@google.come3d32162012-07-20 13:37:06 +00001030 GrDrawTarget::AutoDeviceCoordDraw adcd(target);
1031 if (!adcd.succeeded()) {
1032 return;
1033 }
bsalomon@google.com93c96602012-04-27 13:05:21 +00001034
bsalomon@google.come3d32162012-07-20 13:37:06 +00001035 GrVertexLayout layout = GrDrawTarget::kEdge_VertexLayoutBit;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001036 GrAssert(sizeof(CircleVertex) == GrDrawTarget::VertexSize(layout));
1037
1038 GrPoint center = GrPoint::Make(rect.centerX(), rect.centerY());
1039 GrScalar radius = SkScalarHalf(rect.width());
1040
1041 vm.mapPoints(&center, 1);
1042 radius = vm.mapRadius(radius);
1043
1044 GrScalar outerRadius = radius;
1045 GrScalar innerRadius = 0;
1046 SkScalar halfWidth = 0;
1047 if (strokeWidth == 0) {
1048 halfWidth = SkScalarHalf(SK_Scalar1);
1049
1050 outerRadius += halfWidth;
1051 innerRadius = SkMaxScalar(0, radius - halfWidth);
1052 }
1053
1054 GrDrawTarget::AutoReleaseGeometry geo(target, layout, 4, 0);
1055 if (!geo.succeeded()) {
1056 GrPrintf("Failed to get space for vertices!\n");
1057 return;
1058 }
1059
1060 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
1061
robertphillips@google.coma0a66c12012-06-22 13:14:29 +00001062 // The fragment shader will extend the radius out half a pixel
1063 // to antialias. Expand the drawn rect here so all the pixels
1064 // will be captured.
1065 SkScalar L = center.fX - outerRadius - SkFloatToScalar(0.5f);
1066 SkScalar R = center.fX + outerRadius + SkFloatToScalar(0.5f);
1067 SkScalar T = center.fY - outerRadius - SkFloatToScalar(0.5f);
1068 SkScalar B = center.fY + outerRadius + SkFloatToScalar(0.5f);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001069
1070 verts[0].fPos = SkPoint::Make(L, T);
1071 verts[1].fPos = SkPoint::Make(R, T);
1072 verts[2].fPos = SkPoint::Make(L, B);
1073 verts[3].fPos = SkPoint::Make(R, B);
1074
1075 for (int i = 0; i < 4; ++i) {
1076 // this goes to fragment shader, it should be in y-points-up space.
1077 verts[i].fCenter = SkPoint::Make(center.fX, rt->height() - center.fY);
1078
1079 verts[i].fOuterRadius = outerRadius;
1080 verts[i].fInnerRadius = innerRadius;
1081 }
1082
1083 drawState->setVertexEdgeType(GrDrawState::kCircle_EdgeType);
bsalomon@google.com47059542012-06-06 20:51:20 +00001084 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001085}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001086
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001087void GrContext::drawPath(const GrPaint& paint, const SkPath& path,
reed@google.com07f3ee12011-05-16 17:21:57 +00001088 GrPathFill fill, const GrPoint* translate) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001089
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001090 if (path.isEmpty()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001091 if (GrIsFillInverted(fill)) {
1092 this->drawPaint(paint);
1093 }
1094 return;
1095 }
1096
bsalomon@google.com93c96602012-04-27 13:05:21 +00001097 SkRect ovalRect;
1098 if (!GrIsFillInverted(fill) && path.isOval(&ovalRect)) {
1099 if (translate) {
1100 ovalRect.offset(*translate);
1101 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001102 SkScalar width = (fill == kHairLine_GrPathFill) ? 0 : -SK_Scalar1;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001103 this->drawOval(paint, ovalRect, width);
1104 return;
1105 }
1106
1107 internalDrawPath(paint, path, fill, translate);
1108}
1109
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001110void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +00001111 GrPathFill fill, const GrPoint* translate) {
1112
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001113 // Note that below we may sw-rasterize the path into a scratch texture.
1114 // Scratch textures can be recycled after they are returned to the texture
1115 // cache. This presents a potential hazard for buffered drawing. However,
1116 // the writePixels that uploads to the scratch will perform a flush so we're
1117 // OK.
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001118 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +00001119 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001120
bsalomon@google.com289533a2011-10-27 12:34:25 +00001121 bool prAA = paint.fAntiAlias && !this->getRenderTarget()->isMultisampled();
1122
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001123 // An Assumption here is that path renderer would use some form of tweaking
1124 // the src color (either the input alpha or in the frag shader) to implement
1125 // aa. If we have some future driver-mojo path AA that can do the right
1126 // thing WRT to the blend then we'll need some query on the PR.
1127 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001128#if GR_DEBUG
bsalomon@google.com979432b2011-11-05 21:38:22 +00001129 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001130#endif
bsalomon@google.com289533a2011-10-27 12:34:25 +00001131 prAA = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001132 }
bsalomon@google.com289533a2011-10-27 12:34:25 +00001133
robertphillips@google.com72176b22012-05-23 13:19:12 +00001134 GrPathRenderer* pr = this->getPathRenderer(path, fill, target, prAA, true);
bsalomon@google.com30085192011-08-19 15:42:31 +00001135 if (NULL == pr) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001136#if GR_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001137 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001138#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001139 return;
1140 }
1141
bsalomon@google.come3d32162012-07-20 13:37:06 +00001142 pr->drawPath(path, fill, translate, target, prAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001143}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001144
bsalomon@google.com27847de2011-02-22 20:59:41 +00001145////////////////////////////////////////////////////////////////////////////////
1146
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001147void GrContext::flush(int flagsBitfield) {
1148 if (kDiscard_FlushBit & flagsBitfield) {
1149 fDrawBuffer->reset();
1150 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001151 this->flushDrawBuffer();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001152 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001153 if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001154 fGpu->forceRenderTargetFlush();
1155 }
1156}
1157
bsalomon@google.com27847de2011-02-22 20:59:41 +00001158void GrContext::flushDrawBuffer() {
junov@google.com53a55842011-06-08 22:55:10 +00001159 if (fDrawBuffer) {
robertphillips@google.com58b38182012-05-03 16:29:41 +00001160 // With addition of the AA clip path, flushing the draw buffer can
1161 // result in the generation of an AA clip mask. During this
1162 // process the SW path renderer may be invoked which recusively
1163 // calls this method (via internalWriteTexturePixels) creating
1164 // infinite recursion
1165 GrInOrderDrawBuffer* temp = fDrawBuffer;
1166 fDrawBuffer = NULL;
1167
1168 temp->flushTo(fGpu);
1169
1170 fDrawBuffer = temp;
junov@google.com53a55842011-06-08 22:55:10 +00001171 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001172}
1173
bsalomon@google.com0342a852012-08-20 19:22:38 +00001174void GrContext::writeTexturePixels(GrTexture* texture,
1175 int left, int top, int width, int height,
1176 GrPixelConfig config, const void* buffer, size_t rowBytes,
1177 uint32_t flags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001178 SK_TRACE_EVENT0("GrContext::writeTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001179 ASSERT_OWNED_RESOURCE(texture);
1180
bsalomon@google.com0342a852012-08-20 19:22:38 +00001181 // TODO: use scratch texture to perform conversion
1182 if (kUnpremul_PixelOpsFlag & flags) {
1183 return;
1184 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001185 if (!(kDontFlush_PixelOpsFlag & flags)) {
1186 this->flush();
1187 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001188
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001189 fGpu->writeTexturePixels(texture, left, top, width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +00001190 config, buffer, rowBytes);
1191}
1192
bsalomon@google.com0342a852012-08-20 19:22:38 +00001193bool GrContext::readTexturePixels(GrTexture* texture,
1194 int left, int top, int width, int height,
1195 GrPixelConfig config, void* buffer, size_t rowBytes,
1196 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001197 SK_TRACE_EVENT0("GrContext::readTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001198 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001199
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001200 // TODO: code read pixels for textures that aren't also rendertargets
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001201 GrRenderTarget* target = texture->asRenderTarget();
1202 if (NULL != target) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001203 return this->readRenderTargetPixels(target,
1204 left, top, width, height,
1205 config, buffer, rowBytes,
1206 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001207 } else {
1208 return false;
1209 }
1210}
1211
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001212#include "SkConfig8888.h"
1213
1214namespace {
1215/**
1216 * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel
1217 * formats are representable as Config8888 and so the function returns false
1218 * if the GrPixelConfig has no equivalent Config8888.
1219 */
1220bool grconfig_to_config8888(GrPixelConfig config,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001221 bool unpremul,
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001222 SkCanvas::Config8888* config8888) {
1223 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001224 case kRGBA_8888_GrPixelConfig:
1225 if (unpremul) {
1226 *config8888 = SkCanvas::kRGBA_Unpremul_Config8888;
1227 } else {
1228 *config8888 = SkCanvas::kRGBA_Premul_Config8888;
1229 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001230 return true;
bsalomon@google.com0342a852012-08-20 19:22:38 +00001231 case kBGRA_8888_GrPixelConfig:
1232 if (unpremul) {
1233 *config8888 = SkCanvas::kBGRA_Unpremul_Config8888;
1234 } else {
1235 *config8888 = SkCanvas::kBGRA_Premul_Config8888;
1236 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001237 return true;
1238 default:
1239 return false;
1240 }
1241}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001242
1243// It returns a configuration with where the byte position of the R & B components are swapped in
1244// relation to the input config. This should only be called with the result of
1245// grconfig_to_config8888 as it will fail for other configs.
1246SkCanvas::Config8888 swap_config8888_red_and_blue(SkCanvas::Config8888 config8888) {
1247 switch (config8888) {
1248 case SkCanvas::kBGRA_Premul_Config8888:
1249 return SkCanvas::kRGBA_Premul_Config8888;
1250 case SkCanvas::kBGRA_Unpremul_Config8888:
1251 return SkCanvas::kRGBA_Unpremul_Config8888;
1252 case SkCanvas::kRGBA_Premul_Config8888:
1253 return SkCanvas::kBGRA_Premul_Config8888;
1254 case SkCanvas::kRGBA_Unpremul_Config8888:
1255 return SkCanvas::kBGRA_Unpremul_Config8888;
1256 default:
1257 GrCrash("Unexpected input");
1258 return SkCanvas::kBGRA_Unpremul_Config8888;;
1259 }
1260}
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001261}
1262
bsalomon@google.com0342a852012-08-20 19:22:38 +00001263bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
1264 int left, int top, int width, int height,
1265 GrPixelConfig config, void* buffer, size_t rowBytes,
1266 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001267 SK_TRACE_EVENT0("GrContext::readRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001268 ASSERT_OWNED_RESOURCE(target);
1269
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001270 if (NULL == target) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001271 target = fDrawState->getRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001272 if (NULL == target) {
1273 return false;
1274 }
1275 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001276
bsalomon@google.com6f379512011-11-16 20:36:03 +00001277 if (!(kDontFlush_PixelOpsFlag & flags)) {
1278 this->flush();
1279 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001280
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001281 // Determine which conversions have to be applied: flipY, swapRAnd, and/or unpremul.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001282
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001283 // If fGpu->readPixels would incur a y-flip cost then we will read the pixels upside down. We'll
1284 // either do the flipY by drawing into a scratch with a matrix or on the cpu after the read.
1285 bool flipY = fGpu->readPixelsWillPayForYFlip(target, left, top,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001286 width, height, config,
1287 rowBytes);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001288 bool swapRAndB = fGpu->preferredReadPixelsConfig(config) == GrPixelConfigSwapRAndB(config);
1289
bsalomon@google.com0342a852012-08-20 19:22:38 +00001290 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001291
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001292 // flipY will get set to false when it is handled below using a scratch. However, in that case
1293 // we still want to do the read upside down.
1294 bool readUpsideDown = flipY;
1295
1296 if (unpremul && kRGBA_8888_GrPixelConfig != config && kBGRA_8888_GrPixelConfig != config) {
1297 // The unpremul flag is only allowed for these two configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001298 return false;
1299 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001300
1301 GrPixelConfig readConfig;
1302 if (swapRAndB) {
1303 readConfig = GrPixelConfigSwapRAndB(config);
1304 GrAssert(kUnknown_GrPixelConfig != config);
1305 } else {
1306 readConfig = config;
1307 }
1308
1309 // If the src is a texture and we would have to do conversions after read pixels, we instead
1310 // do the conversions by drawing the src to a scratch texture. If we handle any of the
1311 // conversions in the draw we set the corresponding bool to false so that we don't reapply it
1312 // on the read back pixels.
1313 GrTexture* src = target->asTexture();
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001314 GrAutoScratchTexture ast;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001315 if (NULL != src && (swapRAndB || unpremul || flipY)) {
1316 // Make the scratch a render target because we don't have a robust readTexturePixels as of
1317 // yet. It calls this function.
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001318 GrTextureDesc desc;
1319 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1320 desc.fWidth = width;
1321 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001322 desc.fConfig = readConfig;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001323
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001324 // When a full readback is faster than a partial we could always make the scratch exactly
1325 // match the passed rect. However, if we see many different size rectangles we will trash
1326 // our texture cache and pay the cost of creating and destroying many textures. So, we only
1327 // request an exact match when the caller is reading an entire RT.
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001328 ScratchTexMatch match = kApprox_ScratchTexMatch;
1329 if (0 == left &&
1330 0 == top &&
1331 target->width() == width &&
1332 target->height() == height &&
1333 fGpu->fullReadPixelsIsFasterThanPartial()) {
1334 match = kExact_ScratchTexMatch;
1335 }
1336 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001337 GrTexture* texture = ast.texture();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001338 if (texture) {
1339 SkAutoTUnref<GrCustomStage> stage;
1340 if (unpremul) {
1341 stage.reset(this->createPMToUPMEffect(src, swapRAndB));
1342 }
1343 // If we failed to create a PM->UPM effect and have no other conversions to perform then
1344 // there is no longer any point to using the scratch.
1345 if (NULL != stage || flipY || swapRAndB) {
1346 if (NULL == stage) {
1347 stage.reset(GrConfigConversionEffect::Create(src, swapRAndB));
1348 GrAssert(NULL != stage);
1349 } else {
1350 unpremul = false; // we will handle the UPM conversion in the draw
1351 }
1352 swapRAndB = false; // we will handle the swap in the draw.
bsalomon@google.comc4364992011-11-07 15:54:49 +00001353
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001354 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
1355 GrDrawState* drawState = fGpu->drawState();
1356 drawState->setRenderTarget(texture->asRenderTarget());
1357 GrMatrix matrix;
1358 if (flipY) {
1359 matrix.setTranslate(SK_Scalar1 * left,
1360 SK_Scalar1 * (top + height));
1361 matrix.set(GrMatrix::kMScaleY, -GR_Scalar1);
1362 flipY = false; // the y flip will be handled in the draw
1363 } else {
1364 matrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
1365 }
1366 matrix.postIDiv(src->width(), src->height());
1367 drawState->sampler(0)->reset(matrix);
1368 drawState->sampler(0)->setCustomStage(stage);
1369 GrRect rect = GrRect::MakeWH(GrIntToScalar(width), GrIntToScalar(height));
1370 fGpu->drawSimpleRect(rect, NULL);
1371 // we want to read back from the scratch's origin
1372 left = 0;
1373 top = 0;
1374 target = texture->asRenderTarget();
1375 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001376 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001377 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001378 if (!fGpu->readPixels(target,
1379 left, top, width, height,
1380 readConfig, buffer, rowBytes, readUpsideDown)) {
1381 return false;
1382 }
1383 // Perform any conversions we weren't able to perfom using a scratch texture.
1384 if (unpremul || swapRAndB || flipY) {
1385 SkCanvas::Config8888 srcC8888;
1386 SkCanvas::Config8888 dstC8888;
1387 bool c8888IsValid = grconfig_to_config8888(config, false, &srcC8888);
1388 grconfig_to_config8888(config, unpremul, &dstC8888);
1389 if (swapRAndB) {
1390 GrAssert(c8888IsValid); // we should only do r/b swap on 8888 configs
1391 srcC8888 = swap_config8888_red_and_blue(srcC8888);
1392 }
1393 if (flipY) {
1394 size_t tightRB = width * GrBytesPerPixel(config);
1395 if (0 == rowBytes) {
1396 rowBytes = tightRB;
1397 }
1398 SkAutoSTMalloc<256, uint8_t> tempRow(tightRB);
1399 intptr_t top = reinterpret_cast<intptr_t>(buffer);
1400 intptr_t bot = top + (height - 1) * rowBytes;
1401 while (top < bot) {
1402 uint32_t* t = reinterpret_cast<uint32_t*>(top);
1403 uint32_t* b = reinterpret_cast<uint32_t*>(bot);
1404 uint32_t* temp = reinterpret_cast<uint32_t*>(tempRow.get());
1405 memcpy(temp, t, tightRB);
1406 if (c8888IsValid) {
1407 SkConvertConfig8888Pixels(t, tightRB, dstC8888,
1408 b, tightRB, srcC8888,
1409 width, 1);
1410 SkConvertConfig8888Pixels(b, tightRB, dstC8888,
1411 temp, tightRB, srcC8888,
1412 width, 1);
1413 } else {
1414 memcpy(t, b, tightRB);
1415 memcpy(b, temp, tightRB);
1416 }
1417 top += rowBytes;
1418 bot -= rowBytes;
1419 }
1420 // The above loop does nothing on the middle row when height is odd.
1421 if (top == bot && c8888IsValid && dstC8888 != srcC8888) {
1422 uint32_t* mid = reinterpret_cast<uint32_t*>(top);
1423 SkConvertConfig8888Pixels(mid, tightRB, dstC8888, mid, tightRB, srcC8888, width, 1);
1424 }
1425 } else {
1426 // if we aren't flipping Y then we have no reason to be here other than doing
1427 // conversions for 8888 (r/b swap or upm).
1428 GrAssert(c8888IsValid);
1429 uint32_t* b32 = reinterpret_cast<uint32_t*>(buffer);
1430 SkConvertConfig8888Pixels(b32, rowBytes, dstC8888,
1431 b32, rowBytes, srcC8888,
1432 width, height);
1433 }
1434 }
1435 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001436}
1437
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001438void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1439 GrAssert(target);
1440 ASSERT_OWNED_RESOURCE(target);
1441 // In the future we may track whether there are any pending draws to this
1442 // target. We don't today so we always perform a flush. We don't promise
1443 // this to our clients, though.
1444 this->flush();
1445 fGpu->resolveRenderTarget(target);
1446}
1447
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001448void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst) {
1449 if (NULL == src || NULL == dst) {
1450 return;
1451 }
1452 ASSERT_OWNED_RESOURCE(src);
1453
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001454 // Writes pending to the source texture are not tracked, so a flush
1455 // is required to ensure that the copy captures the most recent contents
1456 // of the source texture. See similar behaviour in
1457 // GrContext::resolveRenderTarget.
1458 this->flush();
1459
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001460 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001461 GrDrawState* drawState = fGpu->drawState();
1462 drawState->setRenderTarget(dst);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001463 GrMatrix sampleM;
1464 sampleM.setIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001465 drawState->sampler(0)->reset(sampleM);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001466 drawState->createTextureEffect(0, src);
bsalomon@google.com5db3b6c2012-01-12 20:38:57 +00001467 SkRect rect = SkRect::MakeXYWH(0, 0,
1468 SK_Scalar1 * src->width(),
1469 SK_Scalar1 * src->height());
bsalomon@google.come3d32162012-07-20 13:37:06 +00001470 fGpu->drawSimpleRect(rect, NULL);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001471}
1472
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001473void GrContext::writeRenderTargetPixels(GrRenderTarget* target,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001474 int left, int top, int width, int height,
1475 GrPixelConfig config,
1476 const void* buffer,
1477 size_t rowBytes,
1478 uint32_t flags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001479 SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001480 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001481
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001482 if (NULL == target) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001483 target = fDrawState->getRenderTarget();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001484 if (NULL == target) {
1485 return;
1486 }
1487 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001488
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001489 // TODO: when underlying api has a direct way to do this we should use it (e.g. glDrawPixels on
1490 // desktop GL).
1491
1492 // We will always call some form of writeTexturePixels and we will pass our flags on to it.
1493 // Thus, we don't perform a flush here since that call will do it (if the kNoFlush flag isn't
1494 // set.)
bsalomon@google.com27847de2011-02-22 20:59:41 +00001495
bsalomon@google.com0342a852012-08-20 19:22:38 +00001496 // If the RT is also a texture and we don't have to premultiply then take the texture path.
1497 // We expect to be at least as fast or faster since it doesn't use an intermediate texture as
1498 // we do below.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001499
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001500#if !GR_MAC_BUILD
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001501 // At least some drivers on the Mac get confused when glTexImage2D is called on a texture
1502 // attached to an FBO. The FBO still sees the old image. TODO: determine what OS versions and/or
1503 // HW is affected.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001504 if (NULL != target->asTexture() && !(kUnpremul_PixelOpsFlag & flags)) {
1505 this->writeTexturePixels(target->asTexture(),
1506 left, top, width, height,
1507 config, buffer, rowBytes, flags);
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001508 return;
1509 }
1510#endif
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001511 SkAutoTUnref<GrCustomStage> stage;
1512 bool swapRAndB = (fGpu->preferredReadPixelsConfig(config) == GrPixelConfigSwapRAndB(config));
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001513
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001514 GrPixelConfig textureConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001515 if (swapRAndB) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001516 textureConfig = GrPixelConfigSwapRAndB(config);
1517 } else {
1518 textureConfig = config;
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001519 }
1520
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001521 GrTextureDesc desc;
1522 desc.fWidth = width;
1523 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001524 desc.fConfig = textureConfig;
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001525 GrAutoScratchTexture ast(this, desc);
1526 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001527 if (NULL == texture) {
1528 return;
1529 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001530 // allocate a tmp buffer and sw convert the pixels to premul
1531 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
1532
1533 if (kUnpremul_PixelOpsFlag & flags) {
1534 if (kRGBA_8888_GrPixelConfig != config && kBGRA_8888_GrPixelConfig != config) {
1535 return;
1536 }
1537 stage.reset(this->createUPMToPMEffect(texture, swapRAndB));
1538 if (NULL == stage) {
1539 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1540 GR_DEBUGCODE(bool success = )
1541 grconfig_to_config8888(config, true, &srcConfig8888);
1542 GrAssert(success);
1543 GR_DEBUGCODE(success = )
1544 grconfig_to_config8888(config, false, &dstConfig8888);
1545 GrAssert(success);
1546 const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer);
1547 tmpPixels.reset(width * height);
1548 SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888,
1549 src, rowBytes, srcConfig8888,
1550 width, height);
1551 buffer = tmpPixels.get();
1552 rowBytes = 4 * width;
1553 }
1554 }
1555 if (NULL == stage) {
1556 stage.reset(GrConfigConversionEffect::Create(texture, swapRAndB));
1557 GrAssert(NULL != stage);
1558 }
1559
1560 this->writeTexturePixels(texture,
1561 0, 0, width, height,
1562 textureConfig, buffer, rowBytes,
1563 flags & ~kUnpremul_PixelOpsFlag);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001564
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001565 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001566 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001567
1568 GrMatrix matrix;
1569 matrix.setTranslate(GrIntToScalar(left), GrIntToScalar(top));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001570 drawState->setViewMatrix(matrix);
1571 drawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001572
bsalomon@google.com5c638652011-07-18 19:31:59 +00001573 matrix.setIDiv(texture->width(), texture->height());
bsalomon@google.comb8670992012-07-25 21:27:09 +00001574 drawState->sampler(0)->reset(matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001575 drawState->sampler(0)->setCustomStage(stage);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001576
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001577 fGpu->drawSimpleRect(GrRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)), NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001578}
1579////////////////////////////////////////////////////////////////////////////////
1580
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001581void GrContext::setPaint(const GrPaint& paint) {
tomhudson@google.comcb325ce2012-07-11 14:41:19 +00001582 GrAssert(fDrawState->stagesDisabled());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001583
1584 for (int i = 0; i < GrPaint::kMaxTextures; ++i) {
1585 int s = i + GrPaint::kFirstTextureStage;
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001586 if (paint.isTextureStageEnabled(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001587 *fDrawState->sampler(s) = paint.getTextureSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001588 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001589 }
1590
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001591 fDrawState->setFirstCoverageStage(GrPaint::kFirstMaskStage);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001592
1593 for (int i = 0; i < GrPaint::kMaxMasks; ++i) {
1594 int s = i + GrPaint::kFirstMaskStage;
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001595 if (paint.isMaskStageEnabled(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001596 *fDrawState->sampler(s) = paint.getMaskSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001597 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001598 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001599
bsalomon@google.com26936d02012-03-19 13:06:19 +00001600 // disable all stages not accessible via the paint
1601 for (int s = GrPaint::kTotalStages; s < GrDrawState::kNumStages; ++s) {
tomhudson@google.com676e6602012-07-10 17:21:48 +00001602 fDrawState->disableStage(s);
bsalomon@google.com26936d02012-03-19 13:06:19 +00001603 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001604
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001605 fDrawState->setColor(paint.fColor);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001606
1607 if (paint.fDither) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001608 fDrawState->enableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001609 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001610 fDrawState->disableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001611 }
1612 if (paint.fAntiAlias) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001613 fDrawState->enableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001614 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001615 fDrawState->disableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001616 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001617 if (paint.fColorMatrixEnabled) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001618 fDrawState->enableState(GrDrawState::kColorMatrix_StateBit);
1619 fDrawState->setColorMatrix(paint.fColorMatrix);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001620 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001621 fDrawState->disableState(GrDrawState::kColorMatrix_StateBit);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001622 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001623 fDrawState->setBlendFunc(paint.fSrcBlendCoeff, paint.fDstBlendCoeff);
1624 fDrawState->setColorFilter(paint.fColorFilterColor, paint.fColorFilterXfermode);
1625 fDrawState->setCoverage(paint.fCoverage);
reed@google.com4b2d3f32012-05-15 18:05:50 +00001626#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
bsalomon@google.come3d32162012-07-20 13:37:06 +00001627 if ((paint.hasMask() || 0xff != paint.fCoverage) &&
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001628 !fGpu->canApplyCoverage()) {
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001629 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1630 }
bsalomon@google.com95cd7bd2012-03-28 15:35:05 +00001631#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001632}
1633
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001634GrDrawTarget* GrContext::prepareToDraw(const GrPaint* paint, BufferedDraw buffered) {
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001635 if (kNo_BufferedDraw == buffered && kYes_BufferedDraw == fLastDrawWasBuffered) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001636 this->flushDrawBuffer();
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001637 fLastDrawWasBuffered = kNo_BufferedDraw;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001638 }
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001639 if (NULL != paint) {
1640 this->setPaint(*paint);
1641 }
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001642 if (kYes_BufferedDraw == buffered) {
1643 fDrawBuffer->setClip(fGpu->getClip());
1644 fLastDrawWasBuffered = kYes_BufferedDraw;
1645 return fDrawBuffer;
1646 } else {
1647 GrAssert(kNo_BufferedDraw == buffered);
1648 return fGpu;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001649 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001650}
1651
robertphillips@google.com72176b22012-05-23 13:19:12 +00001652/*
1653 * This method finds a path renderer that can draw the specified path on
1654 * the provided target.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001655 * Due to its expense, the software path renderer has split out so it can
robertphillips@google.com72176b22012-05-23 13:19:12 +00001656 * can be individually allowed/disallowed via the "allowSW" boolean.
1657 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001658GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
bsalomon@google.com289533a2011-10-27 12:34:25 +00001659 GrPathFill fill,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001660 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001661 bool antiAlias,
1662 bool allowSW) {
bsalomon@google.com30085192011-08-19 15:42:31 +00001663 if (NULL == fPathRendererChain) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001664 fPathRendererChain =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001665 SkNEW_ARGS(GrPathRendererChain,
1666 (this, GrPathRendererChain::kNone_UsageFlag));
bsalomon@google.com30085192011-08-19 15:42:31 +00001667 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001668
1669 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path, fill,
1670 target,
1671 antiAlias);
1672
1673 if (NULL == pr && allowSW) {
1674 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001675 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001676 }
1677
1678 pr = fSoftwarePathRenderer;
1679 }
1680
1681 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001682}
1683
bsalomon@google.com27847de2011-02-22 20:59:41 +00001684////////////////////////////////////////////////////////////////////////////////
1685
bsalomon@google.com27847de2011-02-22 20:59:41 +00001686void GrContext::setRenderTarget(GrRenderTarget* target) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001687 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001688 fDrawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001689}
1690
1691GrRenderTarget* GrContext::getRenderTarget() {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001692 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001693}
1694
1695const GrRenderTarget* GrContext::getRenderTarget() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001696 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001697}
1698
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001699bool GrContext::isConfigRenderable(GrPixelConfig config) const {
1700 return fGpu->isConfigRenderable(config);
1701}
1702
bsalomon@google.com27847de2011-02-22 20:59:41 +00001703const GrMatrix& GrContext::getMatrix() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001704 return fDrawState->getViewMatrix();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001705}
1706
1707void GrContext::setMatrix(const GrMatrix& m) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001708 fDrawState->setViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001709}
1710
1711void GrContext::concatMatrix(const GrMatrix& m) const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001712 fDrawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001713}
1714
1715static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
1716 intptr_t mask = 1 << shift;
1717 if (pred) {
1718 bits |= mask;
1719 } else {
1720 bits &= ~mask;
1721 }
1722 return bits;
1723}
1724
bsalomon@google.com583a1e32011-08-17 13:42:46 +00001725GrContext::GrContext(GrGpu* gpu) {
bsalomon@google.comc0af3172012-06-15 14:10:09 +00001726 ++THREAD_INSTANCE_COUNT;
1727
bsalomon@google.com27847de2011-02-22 20:59:41 +00001728 fGpu = gpu;
1729 fGpu->ref();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001730 fGpu->setContext(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001731
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001732 fDrawState = SkNEW(GrDrawState);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001733 fGpu->setDrawState(fDrawState);
1734
bsalomon@google.com30085192011-08-19 15:42:31 +00001735 fPathRendererChain = NULL;
robertphillips@google.com72176b22012-05-23 13:19:12 +00001736 fSoftwarePathRenderer = NULL;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001737
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001738 fTextureCache = SkNEW_ARGS(GrResourceCache,
1739 (MAX_TEXTURE_CACHE_COUNT,
1740 MAX_TEXTURE_CACHE_BYTES));
1741 fFontCache = SkNEW_ARGS(GrFontCache, (fGpu));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001742
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001743 fLastDrawWasBuffered = kNo_BufferedDraw;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001744
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001745 fDrawBuffer = NULL;
1746 fDrawBufferVBAllocPool = NULL;
1747 fDrawBufferIBAllocPool = NULL;
1748
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001749 fAARectRenderer = SkNEW(GrAARectRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001750
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001751 fDidTestPMConversions = false;
1752
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001753 this->setupDrawBuffer();
1754}
1755
1756void GrContext::setupDrawBuffer() {
1757
1758 GrAssert(NULL == fDrawBuffer);
1759 GrAssert(NULL == fDrawBufferVBAllocPool);
1760 GrAssert(NULL == fDrawBufferIBAllocPool);
1761
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001762 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001763 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001764 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001765 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001766 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001767 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001768 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001769 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001770
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001771 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com471d4712011-08-23 15:45:25 +00001772 fDrawBufferVBAllocPool,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001773 fDrawBufferIBAllocPool));
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001774
bsalomon@google.com27847de2011-02-22 20:59:41 +00001775 fDrawBuffer->setQuadIndexBuffer(this->getQuadIndexBuffer());
bsalomon@google.com1015e032012-06-25 18:41:04 +00001776 if (fDrawBuffer) {
1777 fDrawBuffer->setAutoFlushTarget(fGpu);
1778 fDrawBuffer->setDrawState(fDrawState);
1779 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001780}
1781
bsalomon@google.com27847de2011-02-22 20:59:41 +00001782GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) {
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001783 return prepareToDraw(&paint, DEFAULT_BUFFERING);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001784}
1785
1786const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1787 return fGpu->getQuadIndexBuffer();
1788}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001789
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001790namespace {
1791void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
1792 GrConfigConversionEffect::PMConversion pmToUPM;
1793 GrConfigConversionEffect::PMConversion upmToPM;
1794 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
1795 *pmToUPMValue = pmToUPM;
1796 *upmToPMValue = upmToPM;
1797}
1798}
1799
1800GrCustomStage* GrContext::createPMToUPMEffect(GrTexture* texture, bool swapRAndB) {
1801 if (!fDidTestPMConversions) {
1802 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001803 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001804 }
1805 GrConfigConversionEffect::PMConversion pmToUPM =
1806 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
1807 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
1808 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM);
1809 } else {
1810 return NULL;
1811 }
1812}
1813
1814GrCustomStage* GrContext::createUPMToPMEffect(GrTexture* texture, bool swapRAndB) {
1815 if (!fDidTestPMConversions) {
1816 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001817 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001818 }
1819 GrConfigConversionEffect::PMConversion upmToPM =
1820 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
1821 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
1822 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM);
1823 } else {
1824 return NULL;
1825 }
1826}
1827
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001828GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture,
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001829 bool canClobberSrc,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001830 const SkRect& rect,
1831 float sigmaX, float sigmaY) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001832 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001833 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001834 AutoMatrix avm(this, GrMatrix::I());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001835 SkIRect clearRect;
bsalomon@google.comb505a122012-05-31 18:40:36 +00001836 int scaleFactorX, radiusX;
1837 int scaleFactorY, radiusY;
1838 sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX);
1839 sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY);
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001840
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001841 SkRect srcRect(rect);
1842 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
1843 srcRect.roundOut();
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001844 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001845 static_cast<float>(scaleFactorY));
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001846
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001847 AutoClip acs(this, srcRect);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001848
bsalomon@google.com0342a852012-08-20 19:22:38 +00001849 GrAssert(kBGRA_8888_GrPixelConfig == srcTexture->config() ||
1850 kRGBA_8888_GrPixelConfig == srcTexture->config() ||
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001851 kAlpha_8_GrPixelConfig == srcTexture->config());
1852
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001853 GrTextureDesc desc;
1854 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1855 desc.fWidth = SkScalarFloorToInt(srcRect.width());
1856 desc.fHeight = SkScalarFloorToInt(srcRect.height());
1857 desc.fConfig = srcTexture->config();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001858
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001859 GrAutoScratchTexture temp1, temp2;
1860 GrTexture* dstTexture = temp1.set(this, desc);
1861 GrTexture* tempTexture = canClobberSrc ? srcTexture : temp2.set(this, desc);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001862
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001863 GrPaint paint;
1864 paint.reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +00001865 paint.textureSampler(0)->textureParams()->setBilerp(true);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001866
1867 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
1868 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1869 srcTexture->height());
1870 this->setRenderTarget(dstTexture->asRenderTarget());
1871 SkRect dstRect(srcRect);
1872 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
1873 i < scaleFactorY ? 0.5f : 1.0f);
tomhudson@google.comaa72eab2012-07-19 18:01:07 +00001874 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
1875 (srcTexture)))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001876 this->drawRectToRect(paint, dstRect, srcRect);
1877 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001878 srcTexture = dstTexture;
1879 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001880 }
1881
robertphillips@google.com7a396332012-05-10 15:11:27 +00001882 SkIRect srcIRect;
1883 srcRect.roundOut(&srcIRect);
1884
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001885 if (sigmaX > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001886 if (scaleFactorX > 1) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001887 // Clear out a radius to the right of the srcRect to prevent the
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001888 // X convolution from reading garbage.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001889 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001890 radiusX, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001891 this->clear(&clearRect, 0x0);
1892 }
1893
1894 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001895 GrDrawTarget* target = this->prepareToDraw(NULL, DEFAULT_BUFFERING);
1896 convolve_gaussian(target, srcTexture, srcRect, sigmaX, radiusX,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001897 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001898 srcTexture = dstTexture;
1899 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001900 }
1901
1902 if (sigmaY > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001903 if (scaleFactorY > 1 || sigmaX > 0.0f) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001904 // Clear out a radius below the srcRect to prevent the Y
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001905 // convolution from reading garbage.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001906 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001907 srcIRect.width(), radiusY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001908 this->clear(&clearRect, 0x0);
1909 }
1910
1911 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001912 GrDrawTarget* target = this->prepareToDraw(NULL, DEFAULT_BUFFERING);
1913 convolve_gaussian(target, srcTexture, srcRect, sigmaY, radiusY,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001914 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001915 srcTexture = dstTexture;
1916 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001917 }
1918
1919 if (scaleFactorX > 1 || scaleFactorY > 1) {
1920 // Clear one pixel to the right and below, to accommodate bilinear
1921 // upsampling.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001922 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
robertphillips@google.com7a396332012-05-10 15:11:27 +00001923 srcIRect.width() + 1, 1);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001924 this->clear(&clearRect, 0x0);
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001925 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
robertphillips@google.com7a396332012-05-10 15:11:27 +00001926 1, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001927 this->clear(&clearRect, 0x0);
1928 // FIXME: This should be mitchell, not bilinear.
bsalomon@google.comb8670992012-07-25 21:27:09 +00001929 paint.textureSampler(0)->textureParams()->setBilerp(true);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001930 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1931 srcTexture->height());
1932 this->setRenderTarget(dstTexture->asRenderTarget());
tomhudson@google.comaa72eab2012-07-19 18:01:07 +00001933 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
1934 (srcTexture)))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001935 SkRect dstRect(srcRect);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001936 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001937 this->drawRectToRect(paint, dstRect, srcRect);
1938 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001939 srcTexture = dstTexture;
1940 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001941 }
1942 this->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001943 if (srcTexture == temp1.texture()) {
1944 return temp1.detach();
1945 } else if (srcTexture == temp2.texture()) {
1946 return temp2.detach();
1947 } else {
1948 srcTexture->ref();
1949 return srcTexture;
1950 }
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001951}
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001952
bsalomon@google.comc4364992011-11-07 15:54:49 +00001953///////////////////////////////////////////////////////////////////////////////
robertphillips@google.com59552022012-08-31 13:07:37 +00001954#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001955void GrContext::printCacheStats() const {
1956 fTextureCache->printStats();
1957}
1958#endif