blob: 05ca6877137afd84b1a577d175e5041c74e02b71 [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.com0d25eef2012-09-11 21:25:51 +0000283 GrTexture* clampedTexture = this->findTexture(desc, cacheData, NULL);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000284 if (NULL == clampedTexture) {
robertphillips@google.com0d25eef2012-09-11 21:25:51 +0000285 clampedTexture = this->createTexture(NULL, desc, cacheData, srcData, rowBytes);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000286
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000287 GrAssert(NULL != clampedTexture);
288 if (NULL == clampedTexture) {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000289 return NULL;
290 }
291 }
robertphillips@google.com0d25eef2012-09-11 21:25:51 +0000292
293 clampedTexture->ref();
294
robertphillips@google.com3319f332012-08-13 18:00:36 +0000295 GrTextureDesc rtDesc = desc;
296 rtDesc.fFlags = rtDesc.fFlags |
297 kRenderTarget_GrTextureFlagBit |
298 kNoStencil_GrTextureFlagBit;
299 rtDesc.fWidth = GrNextPow2(GrMax(desc.fWidth, 64));
300 rtDesc.fHeight = GrNextPow2(GrMax(desc.fHeight, 64));
301
302 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
303
304 if (NULL != texture) {
305 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
306 GrDrawState* drawState = fGpu->drawState();
307 drawState->setRenderTarget(texture->asRenderTarget());
308
309 // if filtering is not desired then we want to ensure all
310 // texels in the resampled image are copies of texels from
311 // the original.
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000312 drawState->sampler(0)->reset();
313 GrTextureParams params(SkShader::kClamp_TileMode, needsFiltering);
314 drawState->createTextureEffect(0, clampedTexture, params);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000315
316 static const GrVertexLayout layout =
317 GrDrawTarget::StageTexCoordVertexLayoutBit(0,0);
318 GrDrawTarget::AutoReleaseGeometry arg(fGpu, layout, 4, 0);
319
320 if (arg.succeeded()) {
321 GrPoint* verts = (GrPoint*) arg.vertices();
322 verts[0].setIRectFan(0, 0,
323 texture->width(),
324 texture->height(),
325 2*sizeof(GrPoint));
326 verts[1].setIRectFan(0, 0, 1, 1, 2*sizeof(GrPoint));
327 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType,
328 0, 4);
329 }
330 texture->releaseRenderTarget();
331 } else {
332 // TODO: Our CPU stretch doesn't filter. But we create separate
333 // stretched textures when the sampler state is either filtered or
334 // not. Either implement filtered stretch blit on CPU or just create
335 // one when FBO case fails.
336
337 rtDesc.fFlags = kNone_GrTextureFlags;
338 // no longer need to clamp at min RT size.
339 rtDesc.fWidth = GrNextPow2(desc.fWidth);
340 rtDesc.fHeight = GrNextPow2(desc.fHeight);
341 int bpp = GrBytesPerPixel(desc.fConfig);
342 SkAutoSMalloc<128*128*4> stretchedPixels(bpp *
343 rtDesc.fWidth *
344 rtDesc.fHeight);
345 stretchImage(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
346 srcData, desc.fWidth, desc.fHeight, bpp);
347
348 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
349
350 GrTexture* texture = fGpu->createTexture(rtDesc,
351 stretchedPixels.get(),
352 stretchedRowBytes);
353 GrAssert(NULL != texture);
354 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000355
robertphillips@google.com0d25eef2012-09-11 21:25:51 +0000356 clampedTexture->unref();
robertphillips@google.com3319f332012-08-13 18:00:36 +0000357 return texture;
358}
359
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000360GrTexture* GrContext::createTexture(
bsalomon@google.comb8670992012-07-25 21:27:09 +0000361 const GrTextureParams* params,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000362 const GrTextureDesc& desc,
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000363 const GrCacheData& cacheData,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000364 void* srcData,
365 size_t rowBytes) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000366 SK_TRACE_EVENT0("GrContext::createAndLockTexture");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000367
368#if GR_DUMP_TEXTURE_UPLOAD
369 GrPrintf("GrContext::createAndLockTexture [%d %d]\n", desc.fWidth, desc.fHeight);
370#endif
371
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000372 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000373
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +0000374 SkAutoTUnref<GrTexture> texture;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000375 if (GrTexture::NeedsResizing(resourceKey)) {
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +0000376 texture.reset(this->createResizedTexture(desc, cacheData,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000377 srcData, rowBytes,
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +0000378 GrTexture::NeedsFiltering(resourceKey)));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000379 } else {
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +0000380 texture.reset(fGpu->createTexture(desc, srcData, rowBytes));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000381 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000382
383 if (NULL != texture) {
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000384 fTextureCache->create(resourceKey, texture);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000385 }
386
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000387 return texture;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000388}
389
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000390GrTexture* GrContext::lockScratchTexture(const GrTextureDesc& inDesc,
391 ScratchTexMatch match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000392 GrTextureDesc desc = inDesc;
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000393 GrCacheData cacheData(GrCacheData::kScratch_CacheID);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000394
rileya@google.com2afb8ec2012-08-23 14:08:57 +0000395 GrAssert((desc.fFlags & kRenderTarget_GrTextureFlagBit) ||
396 !(desc.fFlags & kNoStencil_GrTextureFlagBit));
397
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000398 if (kExact_ScratchTexMatch != match) {
399 // bin by pow2 with a reasonable min
400 static const int MIN_SIZE = 256;
401 desc.fWidth = GrMax(MIN_SIZE, GrNextPow2(desc.fWidth));
402 desc.fHeight = GrMax(MIN_SIZE, GrNextPow2(desc.fHeight));
403 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000404
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000405 GrResource* resource = NULL;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000406 int origWidth = desc.fWidth;
407 int origHeight = desc.fHeight;
408 bool doubledW = false;
409 bool doubledH = false;
410
411 do {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000412 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL, desc, cacheData, true);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000413 resource = fTextureCache->find(key);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000414 // if we miss, relax the fit of the flags...
415 // then try doubling width... then height.
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000416 if (NULL != resource || kExact_ScratchTexMatch == match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000417 break;
418 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000419 // We no longer try to reuse textures that were previously used as render targets in
rileya@google.com2afb8ec2012-08-23 14:08:57 +0000420 // situations where no RT is needed; doing otherwise can confuse the video driver and
421 // cause significant performance problems in some cases.
422 if (desc.fFlags & kNoStencil_GrTextureFlagBit) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000423 desc.fFlags = desc.fFlags & ~kNoStencil_GrTextureFlagBit;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000424 } else if (!doubledW) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000425 desc.fFlags = inDesc.fFlags;
426 desc.fWidth *= 2;
427 doubledW = true;
428 } else if (!doubledH) {
429 desc.fFlags = inDesc.fFlags;
430 desc.fWidth = origWidth;
431 desc.fHeight *= 2;
432 doubledH = true;
433 } else {
434 break;
435 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000436
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000437 } while (true);
438
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000439 if (NULL == resource) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000440 desc.fFlags = inDesc.fFlags;
441 desc.fWidth = origWidth;
442 desc.fHeight = origHeight;
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +0000443 SkAutoTUnref<GrTexture> texture(fGpu->createTexture(desc, NULL, 0));
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000444 if (NULL != texture) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000445 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000446 texture->desc(),
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000447 cacheData,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000448 true);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000449 fTextureCache->create(key, texture);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000450 resource = texture;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000451 }
452 }
453
454 // If the caller gives us the same desc/sampler twice we don't want
455 // to return the same texture the second time (unless it was previously
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000456 // released). So make it exclusive to hide it from future searches.
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000457 if (NULL != resource) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000458 fTextureCache->makeExclusive(resource->getCacheEntry());
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000459 }
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000460
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000461 return static_cast<GrTexture*>(resource);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000462}
463
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000464void GrContext::addExistingTextureToCache(GrTexture* texture) {
465
466 if (NULL == texture) {
467 return;
468 }
469
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000470 // This texture should already have a cache entry since it was once
471 // attached
472 GrAssert(NULL != texture->getCacheEntry());
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000473
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000474 // Conceptually, the cache entry is going to assume responsibility
475 // for the creation ref.
476 GrAssert(1 == texture->getRefCnt());
477
478 // Since this texture came from an AutoScratchTexture it should
479 // still be in the exclusive pile
480 fTextureCache->makeNonExclusive(texture->getCacheEntry());
481
robertphillips@google.com50a035d2012-09-07 19:44:33 +0000482 this->purgeCache();
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000483}
484
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000485
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000486void GrContext::unlockScratchTexture(GrTexture* texture) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000487 ASSERT_OWNED_RESOURCE(texture);
488 GrAssert(NULL != texture->getCacheEntry());
489
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000490 // If this is a scratch texture we detached it from the cache
491 // while it was locked (to avoid two callers simultaneously getting
492 // the same texture).
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000493 if (GrTexture::IsScratchTexture(texture->getCacheEntry()->key())) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000494 fTextureCache->makeNonExclusive(texture->getCacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000495 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000496
robertphillips@google.com50a035d2012-09-07 19:44:33 +0000497 this->purgeCache();
498}
499
500void GrContext::purgeCache() {
robertphillips@google.comeb9b3e12012-09-11 12:50:53 +0000501 if (NULL != fTextureCache) {
502 fTextureCache->purgeAsNeeded();
503 }
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000504}
505
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000506GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000507 void* srcData,
508 size_t rowBytes) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000509 GrTextureDesc descCopy = descIn;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000510 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000511}
512
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000513void GrContext::getTextureCacheLimits(int* maxTextures,
514 size_t* maxTextureBytes) const {
515 fTextureCache->getLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000516}
517
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000518void GrContext::setTextureCacheLimits(int maxTextures, size_t maxTextureBytes) {
519 fTextureCache->setLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000520}
521
bsalomon@google.com91958362011-06-13 17:58:13 +0000522int GrContext::getMaxTextureSize() const {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000523 return fGpu->getCaps().maxTextureSize();
bsalomon@google.com91958362011-06-13 17:58:13 +0000524}
525
526int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000527 return fGpu->getCaps().maxRenderTargetSize();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000528}
529
530///////////////////////////////////////////////////////////////////////////////
531
bsalomon@google.come269f212011-11-07 13:29:52 +0000532GrTexture* GrContext::createPlatformTexture(const GrPlatformTextureDesc& desc) {
533 return fGpu->createPlatformTexture(desc);
534}
535
536GrRenderTarget* GrContext::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
537 return fGpu->createPlatformRenderTarget(desc);
538}
539
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000540///////////////////////////////////////////////////////////////////////////////
541
bsalomon@google.comb8670992012-07-25 21:27:09 +0000542bool GrContext::supportsIndex8PixelConfig(const GrTextureParams* params,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000543 int width, int height) const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000544 const GrDrawTarget::Caps& caps = fGpu->getCaps();
bsalomon@google.comf6601872012-08-28 21:11:35 +0000545 if (!caps.eightBitPaletteSupport()) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000546 return false;
547 }
548
bsalomon@google.com27847de2011-02-22 20:59:41 +0000549 bool isPow2 = GrIsPow2(width) && GrIsPow2(height);
550
551 if (!isPow2) {
bsalomon@google.comb8670992012-07-25 21:27:09 +0000552 bool tiled = NULL != params && params->isTiled();
bsalomon@google.comf6601872012-08-28 21:11:35 +0000553 if (tiled && !caps.npotTextureTileSupport()) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000554 return false;
555 }
556 }
557 return true;
558}
559
560////////////////////////////////////////////////////////////////////////////////
561
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000562const GrClipData* GrContext::getClip() const {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000563 return fGpu->getClip();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000564}
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000565
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000566void GrContext::setClip(const GrClipData* clipData) {
567 fGpu->setClip(clipData);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000568 fDrawState->enableState(GrDrawState::kClip_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000569}
570
bsalomon@google.com27847de2011-02-22 20:59:41 +0000571////////////////////////////////////////////////////////////////////////////////
572
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000573void GrContext::clear(const GrIRect* rect,
574 const GrColor color,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000575 GrRenderTarget* target) {
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000576 this->prepareToDraw(NULL, DEFAULT_BUFFERING)->clear(rect, color, target);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000577}
578
579void GrContext::drawPaint(const GrPaint& paint) {
580 // set rect to be big enough to fill the space, but not super-huge, so we
581 // don't overflow fixed-point implementations
bsalomon@google.comd302f142011-03-03 13:54:13 +0000582 GrRect r;
583 r.setLTRB(0, 0,
584 GrIntToScalar(getRenderTarget()->width()),
585 GrIntToScalar(getRenderTarget()->height()));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000586 GrMatrix inverse;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000587 SkTLazy<GrPaint> tmpPaint;
588 const GrPaint* p = &paint;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000589 AutoMatrix am;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000590
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000591 // We attempt to map r by the inverse matrix and draw that. mapRect will
592 // map the four corners and bound them with a new rect. This will not
593 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000594 if (!this->getMatrix().hasPerspective()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000595 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000596 GrPrintf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000597 return;
598 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000599 inverse.mapRect(&r);
600 } else {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000601 if (paint.hasTextureOrMask()) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000602 tmpPaint.set(paint);
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000603 p = tmpPaint.get();
bsalomon@google.come3d32162012-07-20 13:37:06 +0000604 if (!tmpPaint.get()->preConcatSamplerMatricesWithInverse(fDrawState->getViewMatrix())) {
605 GrPrintf("Could not invert matrix\n");
606 }
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000607 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000608 am.set(this, GrMatrix::I());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000609 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000610 // by definition this fills the entire clip, no need for AA
611 if (paint.fAntiAlias) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000612 if (!tmpPaint.isValid()) {
613 tmpPaint.set(paint);
614 p = tmpPaint.get();
615 }
616 GrAssert(p == tmpPaint.get());
617 tmpPaint.get()->fAntiAlias = false;
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000618 }
619 this->drawRect(*p, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000620}
621
bsalomon@google.com205d4602011-04-25 12:43:45 +0000622////////////////////////////////////////////////////////////////////////////////
623
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000624namespace {
625inline bool disable_coverage_aa_for_blend(GrDrawTarget* target) {
626 return DISABLE_COVERAGE_AA_FOR_BLEND && !target->canApplyCoverage();
627}
628}
629
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000630////////////////////////////////////////////////////////////////////////////////
631
bsalomon@google.com27847de2011-02-22 20:59:41 +0000632/* create a triangle strip that strokes the specified triangle. There are 8
633 unique vertices, but we repreat the last 2 to close up. Alternatively we
634 could use an indices array, and then only send 8 verts, but not sure that
635 would be faster.
636 */
bsalomon@google.com205d4602011-04-25 12:43:45 +0000637static void setStrokeRectStrip(GrPoint verts[10], GrRect rect,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000638 GrScalar width) {
639 const GrScalar rad = GrScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000640 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000641
642 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
643 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
644 verts[2].set(rect.fRight - rad, rect.fTop + rad);
645 verts[3].set(rect.fRight + rad, rect.fTop - rad);
646 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
647 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
648 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
649 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
650 verts[8] = verts[0];
651 verts[9] = verts[1];
652}
653
reed@google.com20efde72011-05-09 17:00:02 +0000654/**
655 * Returns true if the rects edges are integer-aligned.
656 */
657static bool isIRect(const GrRect& r) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000658 return GrScalarIsInt(r.fLeft) && GrScalarIsInt(r.fTop) &&
reed@google.com20efde72011-05-09 17:00:02 +0000659 GrScalarIsInt(r.fRight) && GrScalarIsInt(r.fBottom);
660}
661
bsalomon@google.com205d4602011-04-25 12:43:45 +0000662static bool apply_aa_to_rect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000663 const GrRect& rect,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000664 GrScalar width,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000665 const GrMatrix* matrix,
666 GrMatrix* combinedMatrix,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000667 GrRect* devRect,
668 bool* useVertexCoverage) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000669 // we use a simple coverage ramp to do aa on axis-aligned rects
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000670 // we check if the rect will be axis-aligned, and the rect won't land on
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000671 // integer coords.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000672
bsalomon@google.coma3108262011-10-10 14:08:47 +0000673 // we are keeping around the "tweak the alpha" trick because
674 // it is our only hope for the fixed-pipe implementation.
675 // In a shader implementation we can give a separate coverage input
bsalomon@google.com289533a2011-10-27 12:34:25 +0000676 // TODO: remove this ugliness when we drop the fixed-pipe impl
bsalomon@google.coma3108262011-10-10 14:08:47 +0000677 *useVertexCoverage = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000678 if (!target->canTweakAlphaForCoverage()) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000679 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +0000680#if GR_DEBUG
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000681 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000682#endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000683 return false;
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000684 } else {
685 *useVertexCoverage = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000686 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000687 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000688 const GrDrawState& drawState = target->getDrawState();
689 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000690 return false;
691 }
692
bsalomon@google.com471d4712011-08-23 15:45:25 +0000693 if (0 == width && target->willUseHWAALines()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000694 return false;
695 }
696
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000697 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000698 return false;
699 }
700
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000701 if (NULL != matrix &&
bsalomon@google.com205d4602011-04-25 12:43:45 +0000702 !matrix->preservesAxisAlignment()) {
703 return false;
704 }
705
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000706 *combinedMatrix = drawState.getViewMatrix();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000707 if (NULL != matrix) {
708 combinedMatrix->preConcat(*matrix);
709 GrAssert(combinedMatrix->preservesAxisAlignment());
710 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000711
bsalomon@google.com205d4602011-04-25 12:43:45 +0000712 combinedMatrix->mapRect(devRect, rect);
713 devRect->sort();
714
715 if (width < 0) {
reed@google.com20efde72011-05-09 17:00:02 +0000716 return !isIRect(*devRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000717 } else {
718 return true;
719 }
720}
721
bsalomon@google.com27847de2011-02-22 20:59:41 +0000722void GrContext::drawRect(const GrPaint& paint,
723 const GrRect& rect,
724 GrScalar width,
725 const GrMatrix* matrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000726 SK_TRACE_EVENT0("GrContext::drawRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000727
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000728 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000729 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000730
bsalomon@google.com205d4602011-04-25 12:43:45 +0000731 GrRect devRect = rect;
732 GrMatrix combinedMatrix;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000733 bool useVertexCoverage;
bsalomon@google.com289533a2011-10-27 12:34:25 +0000734 bool needAA = paint.fAntiAlias &&
735 !this->getRenderTarget()->isMultisampled();
736 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix,
737 &combinedMatrix, &devRect,
738 &useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000739
740 if (doAA) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000741 GrDrawTarget::AutoDeviceCoordDraw adcd(target);
742 if (!adcd.succeeded()) {
743 return;
744 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000745 if (width >= 0) {
746 GrVec strokeSize;;
747 if (width > 0) {
748 strokeSize.set(width, width);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000749 combinedMatrix.mapVectors(&strokeSize, 1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000750 strokeSize.setAbs(strokeSize);
751 } else {
752 strokeSize.set(GR_Scalar1, GR_Scalar1);
753 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000754 fAARectRenderer->strokeAARect(this->getGpu(), target, devRect,
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000755 strokeSize, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000756 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000757 fAARectRenderer->fillAARect(this->getGpu(), target,
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000758 devRect, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000759 }
760 return;
761 }
762
bsalomon@google.com27847de2011-02-22 20:59:41 +0000763 if (width >= 0) {
764 // TODO: consider making static vertex buffers for these cases.
765 // Hairline could be done by just adding closing vertex to
766 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000767
bsalomon@google.com27847de2011-02-22 20:59:41 +0000768 static const int worstCaseVertCount = 10;
bsalomon@google.come3d32162012-07-20 13:37:06 +0000769 GrDrawTarget::AutoReleaseGeometry geo(target, 0, worstCaseVertCount, 0);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000770
771 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000772 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000773 return;
774 }
775
776 GrPrimitiveType primType;
777 int vertCount;
778 GrPoint* vertex = geo.positions();
779
780 if (width > 0) {
781 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000782 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000783 setStrokeRectStrip(vertex, rect, width);
784 } else {
785 // hairline
786 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000787 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000788 vertex[0].set(rect.fLeft, rect.fTop);
789 vertex[1].set(rect.fRight, rect.fTop);
790 vertex[2].set(rect.fRight, rect.fBottom);
791 vertex[3].set(rect.fLeft, rect.fBottom);
792 vertex[4].set(rect.fLeft, rect.fTop);
793 }
794
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000795 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000796 if (NULL != matrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000797 GrDrawState* drawState = target->drawState();
798 avmr.set(drawState);
799 drawState->preConcatViewMatrix(*matrix);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000800 drawState->preConcatSamplerMatrices(*matrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000801 }
802
803 target->drawNonIndexed(primType, 0, vertCount);
804 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000805#if GR_STATIC_RECT_VB
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000806 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
807 if (NULL == sqVB) {
808 GrPrintf("Failed to create static rect vb.\n");
809 return;
810 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000811 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000812 GrDrawState* drawState = target->drawState();
813 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000814 GrMatrix m;
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000815 m.setAll(rect.width(), 0, rect.fLeft,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000816 0, rect.height(), rect.fTop,
817 0, 0, GrMatrix::I()[8]);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000818
819 if (NULL != matrix) {
820 m.postConcat(*matrix);
821 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000822 drawState->preConcatViewMatrix(m);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000823 drawState->preConcatSamplerMatrices(m);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000824
bsalomon@google.com47059542012-06-06 20:51:20 +0000825 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000826#else
bsalomon@google.come3d32162012-07-20 13:37:06 +0000827 target->drawSimpleRect(rect, matrix);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000828#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000829 }
830}
831
832void GrContext::drawRectToRect(const GrPaint& paint,
833 const GrRect& dstRect,
834 const GrRect& srcRect,
835 const GrMatrix* dstMatrix,
836 const GrMatrix* srcMatrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000837 SK_TRACE_EVENT0("GrContext::drawRectToRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000838
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000839 // srcRect refers to paint's first texture
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000840 if (!paint.isTextureStageEnabled(0)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000841 drawRect(paint, dstRect, -1, dstMatrix);
842 return;
843 }
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000844
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000845 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000846
847#if GR_STATIC_RECT_VB
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000848 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000849 GrDrawState* drawState = target->drawState();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000850 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000851
852 GrMatrix m;
853
854 m.setAll(dstRect.width(), 0, dstRect.fLeft,
855 0, dstRect.height(), dstRect.fTop,
856 0, 0, GrMatrix::I()[8]);
857 if (NULL != dstMatrix) {
858 m.postConcat(*dstMatrix);
859 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000860 drawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000861
bsalomon@google.come3d32162012-07-20 13:37:06 +0000862 // we explicitly setup the correct coords for the first stage. The others
863 // must know about the view matrix change.
864 for (int s = 1; s < GrPaint::kTotalStages; ++s) {
865 if (drawState->isStageEnabled(s)) {
866 drawState->sampler(s)->preConcatMatrix(m);
867 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000868 }
869
bsalomon@google.com27847de2011-02-22 20:59:41 +0000870 m.setAll(srcRect.width(), 0, srcRect.fLeft,
871 0, srcRect.height(), srcRect.fTop,
872 0, 0, GrMatrix::I()[8]);
873 if (NULL != srcMatrix) {
874 m.postConcat(*srcMatrix);
875 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000876 drawState->sampler(GrPaint::kFirstTextureStage)->preConcatMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000877
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000878 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
879 if (NULL == sqVB) {
880 GrPrintf("Failed to create static rect vb.\n");
881 return;
882 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000883 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com47059542012-06-06 20:51:20 +0000884 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000885#else
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000886 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000887
tomhudson@google.com93813632011-10-27 20:21:16 +0000888 const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
889 const GrMatrix* srcMatrices[GrDrawState::kNumStages] = {NULL};
bsalomon@google.com27847de2011-02-22 20:59:41 +0000890 srcRects[0] = &srcRect;
891 srcMatrices[0] = srcMatrix;
892
bsalomon@google.come3d32162012-07-20 13:37:06 +0000893 target->drawRect(dstRect, dstMatrix, srcRects, srcMatrices);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000894#endif
895}
896
897void GrContext::drawVertices(const GrPaint& paint,
898 GrPrimitiveType primitiveType,
899 int vertexCount,
900 const GrPoint positions[],
901 const GrPoint texCoords[],
902 const GrColor colors[],
903 const uint16_t indices[],
904 int indexCount) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000905 SK_TRACE_EVENT0("GrContext::drawVertices");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000906
907 GrDrawTarget::AutoReleaseGeometry geo;
908
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000909 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000910 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000911
bsalomon@google.come3d32162012-07-20 13:37:06 +0000912 GrVertexLayout layout = 0;
913 if (NULL != texCoords) {
914 layout |= GrDrawTarget::StageTexCoordVertexLayoutBit(0, 0);
915 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000916 if (NULL != colors) {
917 layout |= GrDrawTarget::kColor_VertexLayoutBit;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000918 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000919 int vertexSize = GrDrawTarget::VertexSize(layout);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000920
921 if (sizeof(GrPoint) != vertexSize) {
922 if (!geo.set(target, layout, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000923 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000924 return;
925 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000926 int texOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000927 int colorOffset;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000928 GrDrawTarget::VertexSizeAndOffsetsByIdx(layout,
929 texOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000930 &colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000931 NULL,
932 NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000933 void* curVertex = geo.vertices();
934
935 for (int i = 0; i < vertexCount; ++i) {
936 *((GrPoint*)curVertex) = positions[i];
937
938 if (texOffsets[0] > 0) {
939 *(GrPoint*)((intptr_t)curVertex + texOffsets[0]) = texCoords[i];
940 }
941 if (colorOffset > 0) {
942 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
943 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000944 curVertex = (void*)((intptr_t)curVertex + vertexSize);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000945 }
946 } else {
947 target->setVertexSourceToArray(layout, positions, vertexCount);
948 }
949
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000950 // we don't currently apply offscreen AA to this path. Need improved
bsalomon@google.com91958362011-06-13 17:58:13 +0000951 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000952
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000953 if (NULL != indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000954 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000955 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000956 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000957 target->drawNonIndexed(primitiveType, 0, vertexCount);
958 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000959}
960
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000961///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com150d2842012-01-12 20:19:56 +0000962namespace {
963
bsalomon@google.com93c96602012-04-27 13:05:21 +0000964struct CircleVertex {
965 GrPoint fPos;
966 GrPoint fCenter;
967 GrScalar fOuterRadius;
968 GrScalar fInnerRadius;
969};
970
971/* Returns true if will map a circle to another circle. This can be true
972 * if the matrix only includes square-scale, rotation, translation.
973 */
974inline bool isSimilarityTransformation(const SkMatrix& matrix,
975 SkScalar tol = SK_ScalarNearlyZero) {
976 if (matrix.isIdentity() || matrix.getType() == SkMatrix::kTranslate_Mask) {
977 return true;
978 }
979 if (matrix.hasPerspective()) {
980 return false;
981 }
982
983 SkScalar mx = matrix.get(SkMatrix::kMScaleX);
984 SkScalar sx = matrix.get(SkMatrix::kMSkewX);
985 SkScalar my = matrix.get(SkMatrix::kMScaleY);
986 SkScalar sy = matrix.get(SkMatrix::kMSkewY);
987
988 if (mx == 0 && sx == 0 && my == 0 && sy == 0) {
989 return false;
990 }
991
992 // it has scales or skews, but it could also be rotation, check it out.
993 SkVector vec[2];
994 vec[0].set(mx, sx);
995 vec[1].set(sy, my);
996
997 return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) &&
998 SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(),
999 SkScalarSquare(tol));
1000}
1001
1002}
1003
1004// TODO: strokeWidth can't be larger than zero right now.
1005// It will be fixed when drawPath() can handle strokes.
1006void GrContext::drawOval(const GrPaint& paint,
1007 const GrRect& rect,
1008 SkScalar strokeWidth) {
bsalomon@google.com0982d352012-07-31 15:33:25 +00001009 GrAssert(strokeWidth <= 0);
1010 if (!isSimilarityTransformation(this->getMatrix()) ||
bsalomon@google.com93c96602012-04-27 13:05:21 +00001011 !paint.fAntiAlias ||
1012 rect.height() != rect.width()) {
1013 SkPath path;
1014 path.addOval(rect);
1015 GrPathFill fill = (strokeWidth == 0) ?
bsalomon@google.com0982d352012-07-31 15:33:25 +00001016 kHairLine_GrPathFill : kWinding_GrPathFill;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001017 this->internalDrawPath(paint, path, fill, NULL);
1018 return;
1019 }
1020
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001021 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
1022
bsalomon@google.com0982d352012-07-31 15:33:25 +00001023 GrDrawState* drawState = target->drawState();
1024 GrDrawState::AutoStageDisable atr(fDrawState);
1025 const GrMatrix vm = drawState->getViewMatrix();
1026
bsalomon@google.com93c96602012-04-27 13:05:21 +00001027 const GrRenderTarget* rt = drawState->getRenderTarget();
1028 if (NULL == rt) {
1029 return;
1030 }
1031
bsalomon@google.come3d32162012-07-20 13:37:06 +00001032 GrDrawTarget::AutoDeviceCoordDraw adcd(target);
1033 if (!adcd.succeeded()) {
1034 return;
1035 }
bsalomon@google.com93c96602012-04-27 13:05:21 +00001036
bsalomon@google.come3d32162012-07-20 13:37:06 +00001037 GrVertexLayout layout = GrDrawTarget::kEdge_VertexLayoutBit;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001038 GrAssert(sizeof(CircleVertex) == GrDrawTarget::VertexSize(layout));
1039
1040 GrPoint center = GrPoint::Make(rect.centerX(), rect.centerY());
1041 GrScalar radius = SkScalarHalf(rect.width());
1042
1043 vm.mapPoints(&center, 1);
1044 radius = vm.mapRadius(radius);
1045
1046 GrScalar outerRadius = radius;
1047 GrScalar innerRadius = 0;
1048 SkScalar halfWidth = 0;
1049 if (strokeWidth == 0) {
1050 halfWidth = SkScalarHalf(SK_Scalar1);
1051
1052 outerRadius += halfWidth;
1053 innerRadius = SkMaxScalar(0, radius - halfWidth);
1054 }
1055
1056 GrDrawTarget::AutoReleaseGeometry geo(target, layout, 4, 0);
1057 if (!geo.succeeded()) {
1058 GrPrintf("Failed to get space for vertices!\n");
1059 return;
1060 }
1061
1062 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
1063
robertphillips@google.coma0a66c12012-06-22 13:14:29 +00001064 // The fragment shader will extend the radius out half a pixel
1065 // to antialias. Expand the drawn rect here so all the pixels
1066 // will be captured.
1067 SkScalar L = center.fX - outerRadius - SkFloatToScalar(0.5f);
1068 SkScalar R = center.fX + outerRadius + SkFloatToScalar(0.5f);
1069 SkScalar T = center.fY - outerRadius - SkFloatToScalar(0.5f);
1070 SkScalar B = center.fY + outerRadius + SkFloatToScalar(0.5f);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001071
1072 verts[0].fPos = SkPoint::Make(L, T);
1073 verts[1].fPos = SkPoint::Make(R, T);
1074 verts[2].fPos = SkPoint::Make(L, B);
1075 verts[3].fPos = SkPoint::Make(R, B);
1076
1077 for (int i = 0; i < 4; ++i) {
1078 // this goes to fragment shader, it should be in y-points-up space.
1079 verts[i].fCenter = SkPoint::Make(center.fX, rt->height() - center.fY);
1080
1081 verts[i].fOuterRadius = outerRadius;
1082 verts[i].fInnerRadius = innerRadius;
1083 }
1084
1085 drawState->setVertexEdgeType(GrDrawState::kCircle_EdgeType);
bsalomon@google.com47059542012-06-06 20:51:20 +00001086 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001087}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001088
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001089void GrContext::drawPath(const GrPaint& paint, const SkPath& path,
reed@google.com07f3ee12011-05-16 17:21:57 +00001090 GrPathFill fill, const GrPoint* translate) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001091
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001092 if (path.isEmpty()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001093 if (GrIsFillInverted(fill)) {
1094 this->drawPaint(paint);
1095 }
1096 return;
1097 }
1098
bsalomon@google.com93c96602012-04-27 13:05:21 +00001099 SkRect ovalRect;
1100 if (!GrIsFillInverted(fill) && path.isOval(&ovalRect)) {
1101 if (translate) {
1102 ovalRect.offset(*translate);
1103 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001104 SkScalar width = (fill == kHairLine_GrPathFill) ? 0 : -SK_Scalar1;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001105 this->drawOval(paint, ovalRect, width);
1106 return;
1107 }
1108
1109 internalDrawPath(paint, path, fill, translate);
1110}
1111
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001112void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +00001113 GrPathFill fill, const GrPoint* translate) {
1114
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001115 // Note that below we may sw-rasterize the path into a scratch texture.
1116 // Scratch textures can be recycled after they are returned to the texture
1117 // cache. This presents a potential hazard for buffered drawing. However,
1118 // the writePixels that uploads to the scratch will perform a flush so we're
1119 // OK.
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001120 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +00001121 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001122
bsalomon@google.com289533a2011-10-27 12:34:25 +00001123 bool prAA = paint.fAntiAlias && !this->getRenderTarget()->isMultisampled();
1124
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001125 // An Assumption here is that path renderer would use some form of tweaking
1126 // the src color (either the input alpha or in the frag shader) to implement
1127 // aa. If we have some future driver-mojo path AA that can do the right
1128 // thing WRT to the blend then we'll need some query on the PR.
1129 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001130#if GR_DEBUG
bsalomon@google.com979432b2011-11-05 21:38:22 +00001131 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001132#endif
bsalomon@google.com289533a2011-10-27 12:34:25 +00001133 prAA = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001134 }
bsalomon@google.com289533a2011-10-27 12:34:25 +00001135
robertphillips@google.com72176b22012-05-23 13:19:12 +00001136 GrPathRenderer* pr = this->getPathRenderer(path, fill, target, prAA, true);
bsalomon@google.com30085192011-08-19 15:42:31 +00001137 if (NULL == pr) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001138#if GR_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001139 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001140#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001141 return;
1142 }
1143
bsalomon@google.come3d32162012-07-20 13:37:06 +00001144 pr->drawPath(path, fill, translate, target, prAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001145}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001146
bsalomon@google.com27847de2011-02-22 20:59:41 +00001147////////////////////////////////////////////////////////////////////////////////
1148
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001149void GrContext::flush(int flagsBitfield) {
1150 if (kDiscard_FlushBit & flagsBitfield) {
1151 fDrawBuffer->reset();
1152 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001153 this->flushDrawBuffer();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001154 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001155 if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001156 fGpu->forceRenderTargetFlush();
1157 }
1158}
1159
bsalomon@google.com27847de2011-02-22 20:59:41 +00001160void GrContext::flushDrawBuffer() {
junov@google.com53a55842011-06-08 22:55:10 +00001161 if (fDrawBuffer) {
robertphillips@google.com58b38182012-05-03 16:29:41 +00001162 // With addition of the AA clip path, flushing the draw buffer can
1163 // result in the generation of an AA clip mask. During this
1164 // process the SW path renderer may be invoked which recusively
1165 // calls this method (via internalWriteTexturePixels) creating
1166 // infinite recursion
1167 GrInOrderDrawBuffer* temp = fDrawBuffer;
1168 fDrawBuffer = NULL;
1169
1170 temp->flushTo(fGpu);
1171
1172 fDrawBuffer = temp;
junov@google.com53a55842011-06-08 22:55:10 +00001173 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001174}
1175
bsalomon@google.com0342a852012-08-20 19:22:38 +00001176void GrContext::writeTexturePixels(GrTexture* texture,
1177 int left, int top, int width, int height,
1178 GrPixelConfig config, const void* buffer, size_t rowBytes,
1179 uint32_t flags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001180 SK_TRACE_EVENT0("GrContext::writeTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001181 ASSERT_OWNED_RESOURCE(texture);
1182
bsalomon@google.com0342a852012-08-20 19:22:38 +00001183 // TODO: use scratch texture to perform conversion
1184 if (kUnpremul_PixelOpsFlag & flags) {
1185 return;
1186 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001187 if (!(kDontFlush_PixelOpsFlag & flags)) {
1188 this->flush();
1189 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001190
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001191 fGpu->writeTexturePixels(texture, left, top, width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +00001192 config, buffer, rowBytes);
1193}
1194
bsalomon@google.com0342a852012-08-20 19:22:38 +00001195bool GrContext::readTexturePixels(GrTexture* texture,
1196 int left, int top, int width, int height,
1197 GrPixelConfig config, void* buffer, size_t rowBytes,
1198 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001199 SK_TRACE_EVENT0("GrContext::readTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001200 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001201
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001202 // TODO: code read pixels for textures that aren't also rendertargets
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001203 GrRenderTarget* target = texture->asRenderTarget();
1204 if (NULL != target) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001205 return this->readRenderTargetPixels(target,
1206 left, top, width, height,
1207 config, buffer, rowBytes,
1208 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001209 } else {
1210 return false;
1211 }
1212}
1213
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001214#include "SkConfig8888.h"
1215
1216namespace {
1217/**
1218 * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel
1219 * formats are representable as Config8888 and so the function returns false
1220 * if the GrPixelConfig has no equivalent Config8888.
1221 */
1222bool grconfig_to_config8888(GrPixelConfig config,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001223 bool unpremul,
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001224 SkCanvas::Config8888* config8888) {
1225 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001226 case kRGBA_8888_GrPixelConfig:
1227 if (unpremul) {
1228 *config8888 = SkCanvas::kRGBA_Unpremul_Config8888;
1229 } else {
1230 *config8888 = SkCanvas::kRGBA_Premul_Config8888;
1231 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001232 return true;
bsalomon@google.com0342a852012-08-20 19:22:38 +00001233 case kBGRA_8888_GrPixelConfig:
1234 if (unpremul) {
1235 *config8888 = SkCanvas::kBGRA_Unpremul_Config8888;
1236 } else {
1237 *config8888 = SkCanvas::kBGRA_Premul_Config8888;
1238 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001239 return true;
1240 default:
1241 return false;
1242 }
1243}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001244
1245// It returns a configuration with where the byte position of the R & B components are swapped in
1246// relation to the input config. This should only be called with the result of
1247// grconfig_to_config8888 as it will fail for other configs.
1248SkCanvas::Config8888 swap_config8888_red_and_blue(SkCanvas::Config8888 config8888) {
1249 switch (config8888) {
1250 case SkCanvas::kBGRA_Premul_Config8888:
1251 return SkCanvas::kRGBA_Premul_Config8888;
1252 case SkCanvas::kBGRA_Unpremul_Config8888:
1253 return SkCanvas::kRGBA_Unpremul_Config8888;
1254 case SkCanvas::kRGBA_Premul_Config8888:
1255 return SkCanvas::kBGRA_Premul_Config8888;
1256 case SkCanvas::kRGBA_Unpremul_Config8888:
1257 return SkCanvas::kBGRA_Unpremul_Config8888;
1258 default:
1259 GrCrash("Unexpected input");
1260 return SkCanvas::kBGRA_Unpremul_Config8888;;
1261 }
1262}
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001263}
1264
bsalomon@google.com0342a852012-08-20 19:22:38 +00001265bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
1266 int left, int top, int width, int height,
1267 GrPixelConfig config, void* buffer, size_t rowBytes,
1268 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001269 SK_TRACE_EVENT0("GrContext::readRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001270 ASSERT_OWNED_RESOURCE(target);
1271
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001272 if (NULL == target) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001273 target = fDrawState->getRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001274 if (NULL == target) {
1275 return false;
1276 }
1277 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001278
bsalomon@google.com6f379512011-11-16 20:36:03 +00001279 if (!(kDontFlush_PixelOpsFlag & flags)) {
1280 this->flush();
1281 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001282
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001283 // Determine which conversions have to be applied: flipY, swapRAnd, and/or unpremul.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001284
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001285 // If fGpu->readPixels would incur a y-flip cost then we will read the pixels upside down. We'll
1286 // either do the flipY by drawing into a scratch with a matrix or on the cpu after the read.
1287 bool flipY = fGpu->readPixelsWillPayForYFlip(target, left, top,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001288 width, height, config,
1289 rowBytes);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001290 bool swapRAndB = fGpu->preferredReadPixelsConfig(config) == GrPixelConfigSwapRAndB(config);
1291
bsalomon@google.com0342a852012-08-20 19:22:38 +00001292 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001293
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001294 // flipY will get set to false when it is handled below using a scratch. However, in that case
1295 // we still want to do the read upside down.
1296 bool readUpsideDown = flipY;
1297
1298 if (unpremul && kRGBA_8888_GrPixelConfig != config && kBGRA_8888_GrPixelConfig != config) {
1299 // The unpremul flag is only allowed for these two configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001300 return false;
1301 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001302
1303 GrPixelConfig readConfig;
1304 if (swapRAndB) {
1305 readConfig = GrPixelConfigSwapRAndB(config);
1306 GrAssert(kUnknown_GrPixelConfig != config);
1307 } else {
1308 readConfig = config;
1309 }
1310
1311 // If the src is a texture and we would have to do conversions after read pixels, we instead
1312 // do the conversions by drawing the src to a scratch texture. If we handle any of the
1313 // conversions in the draw we set the corresponding bool to false so that we don't reapply it
1314 // on the read back pixels.
1315 GrTexture* src = target->asTexture();
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001316 GrAutoScratchTexture ast;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001317 if (NULL != src && (swapRAndB || unpremul || flipY)) {
1318 // Make the scratch a render target because we don't have a robust readTexturePixels as of
1319 // yet. It calls this function.
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001320 GrTextureDesc desc;
1321 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1322 desc.fWidth = width;
1323 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001324 desc.fConfig = readConfig;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001325
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001326 // When a full readback is faster than a partial we could always make the scratch exactly
1327 // match the passed rect. However, if we see many different size rectangles we will trash
1328 // our texture cache and pay the cost of creating and destroying many textures. So, we only
1329 // request an exact match when the caller is reading an entire RT.
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001330 ScratchTexMatch match = kApprox_ScratchTexMatch;
1331 if (0 == left &&
1332 0 == top &&
1333 target->width() == width &&
1334 target->height() == height &&
1335 fGpu->fullReadPixelsIsFasterThanPartial()) {
1336 match = kExact_ScratchTexMatch;
1337 }
1338 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001339 GrTexture* texture = ast.texture();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001340 if (texture) {
1341 SkAutoTUnref<GrCustomStage> stage;
1342 if (unpremul) {
1343 stage.reset(this->createPMToUPMEffect(src, swapRAndB));
1344 }
1345 // If we failed to create a PM->UPM effect and have no other conversions to perform then
1346 // there is no longer any point to using the scratch.
1347 if (NULL != stage || flipY || swapRAndB) {
1348 if (NULL == stage) {
1349 stage.reset(GrConfigConversionEffect::Create(src, swapRAndB));
1350 GrAssert(NULL != stage);
1351 } else {
1352 unpremul = false; // we will handle the UPM conversion in the draw
1353 }
1354 swapRAndB = false; // we will handle the swap in the draw.
bsalomon@google.comc4364992011-11-07 15:54:49 +00001355
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001356 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
1357 GrDrawState* drawState = fGpu->drawState();
1358 drawState->setRenderTarget(texture->asRenderTarget());
1359 GrMatrix matrix;
1360 if (flipY) {
1361 matrix.setTranslate(SK_Scalar1 * left,
1362 SK_Scalar1 * (top + height));
1363 matrix.set(GrMatrix::kMScaleY, -GR_Scalar1);
1364 flipY = false; // the y flip will be handled in the draw
1365 } else {
1366 matrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
1367 }
1368 matrix.postIDiv(src->width(), src->height());
1369 drawState->sampler(0)->reset(matrix);
1370 drawState->sampler(0)->setCustomStage(stage);
1371 GrRect rect = GrRect::MakeWH(GrIntToScalar(width), GrIntToScalar(height));
1372 fGpu->drawSimpleRect(rect, NULL);
1373 // we want to read back from the scratch's origin
1374 left = 0;
1375 top = 0;
1376 target = texture->asRenderTarget();
1377 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001378 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001379 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001380 if (!fGpu->readPixels(target,
1381 left, top, width, height,
1382 readConfig, buffer, rowBytes, readUpsideDown)) {
1383 return false;
1384 }
1385 // Perform any conversions we weren't able to perfom using a scratch texture.
1386 if (unpremul || swapRAndB || flipY) {
1387 SkCanvas::Config8888 srcC8888;
1388 SkCanvas::Config8888 dstC8888;
1389 bool c8888IsValid = grconfig_to_config8888(config, false, &srcC8888);
1390 grconfig_to_config8888(config, unpremul, &dstC8888);
1391 if (swapRAndB) {
1392 GrAssert(c8888IsValid); // we should only do r/b swap on 8888 configs
1393 srcC8888 = swap_config8888_red_and_blue(srcC8888);
1394 }
1395 if (flipY) {
1396 size_t tightRB = width * GrBytesPerPixel(config);
1397 if (0 == rowBytes) {
1398 rowBytes = tightRB;
1399 }
1400 SkAutoSTMalloc<256, uint8_t> tempRow(tightRB);
1401 intptr_t top = reinterpret_cast<intptr_t>(buffer);
1402 intptr_t bot = top + (height - 1) * rowBytes;
1403 while (top < bot) {
1404 uint32_t* t = reinterpret_cast<uint32_t*>(top);
1405 uint32_t* b = reinterpret_cast<uint32_t*>(bot);
1406 uint32_t* temp = reinterpret_cast<uint32_t*>(tempRow.get());
1407 memcpy(temp, t, tightRB);
1408 if (c8888IsValid) {
1409 SkConvertConfig8888Pixels(t, tightRB, dstC8888,
1410 b, tightRB, srcC8888,
1411 width, 1);
1412 SkConvertConfig8888Pixels(b, tightRB, dstC8888,
1413 temp, tightRB, srcC8888,
1414 width, 1);
1415 } else {
1416 memcpy(t, b, tightRB);
1417 memcpy(b, temp, tightRB);
1418 }
1419 top += rowBytes;
1420 bot -= rowBytes;
1421 }
1422 // The above loop does nothing on the middle row when height is odd.
1423 if (top == bot && c8888IsValid && dstC8888 != srcC8888) {
1424 uint32_t* mid = reinterpret_cast<uint32_t*>(top);
1425 SkConvertConfig8888Pixels(mid, tightRB, dstC8888, mid, tightRB, srcC8888, width, 1);
1426 }
1427 } else {
1428 // if we aren't flipping Y then we have no reason to be here other than doing
1429 // conversions for 8888 (r/b swap or upm).
1430 GrAssert(c8888IsValid);
1431 uint32_t* b32 = reinterpret_cast<uint32_t*>(buffer);
1432 SkConvertConfig8888Pixels(b32, rowBytes, dstC8888,
1433 b32, rowBytes, srcC8888,
1434 width, height);
1435 }
1436 }
1437 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001438}
1439
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001440void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1441 GrAssert(target);
1442 ASSERT_OWNED_RESOURCE(target);
1443 // In the future we may track whether there are any pending draws to this
1444 // target. We don't today so we always perform a flush. We don't promise
1445 // this to our clients, though.
1446 this->flush();
1447 fGpu->resolveRenderTarget(target);
1448}
1449
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001450void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst) {
1451 if (NULL == src || NULL == dst) {
1452 return;
1453 }
1454 ASSERT_OWNED_RESOURCE(src);
1455
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001456 // Writes pending to the source texture are not tracked, so a flush
1457 // is required to ensure that the copy captures the most recent contents
1458 // of the source texture. See similar behaviour in
1459 // GrContext::resolveRenderTarget.
1460 this->flush();
1461
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001462 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001463 GrDrawState* drawState = fGpu->drawState();
1464 drawState->setRenderTarget(dst);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001465 GrMatrix sampleM;
1466 sampleM.setIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001467 drawState->sampler(0)->reset(sampleM);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001468 drawState->createTextureEffect(0, src);
bsalomon@google.com5db3b6c2012-01-12 20:38:57 +00001469 SkRect rect = SkRect::MakeXYWH(0, 0,
1470 SK_Scalar1 * src->width(),
1471 SK_Scalar1 * src->height());
bsalomon@google.come3d32162012-07-20 13:37:06 +00001472 fGpu->drawSimpleRect(rect, NULL);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001473}
1474
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001475void GrContext::writeRenderTargetPixels(GrRenderTarget* target,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001476 int left, int top, int width, int height,
1477 GrPixelConfig config,
1478 const void* buffer,
1479 size_t rowBytes,
1480 uint32_t flags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001481 SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001482 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001483
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001484 if (NULL == target) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001485 target = fDrawState->getRenderTarget();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001486 if (NULL == target) {
1487 return;
1488 }
1489 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001490
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001491 // TODO: when underlying api has a direct way to do this we should use it (e.g. glDrawPixels on
1492 // desktop GL).
1493
1494 // We will always call some form of writeTexturePixels and we will pass our flags on to it.
1495 // Thus, we don't perform a flush here since that call will do it (if the kNoFlush flag isn't
1496 // set.)
bsalomon@google.com27847de2011-02-22 20:59:41 +00001497
bsalomon@google.com0342a852012-08-20 19:22:38 +00001498 // If the RT is also a texture and we don't have to premultiply then take the texture path.
1499 // We expect to be at least as fast or faster since it doesn't use an intermediate texture as
1500 // we do below.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001501
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001502#if !GR_MAC_BUILD
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001503 // At least some drivers on the Mac get confused when glTexImage2D is called on a texture
1504 // attached to an FBO. The FBO still sees the old image. TODO: determine what OS versions and/or
1505 // HW is affected.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001506 if (NULL != target->asTexture() && !(kUnpremul_PixelOpsFlag & flags)) {
1507 this->writeTexturePixels(target->asTexture(),
1508 left, top, width, height,
1509 config, buffer, rowBytes, flags);
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001510 return;
1511 }
1512#endif
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001513 SkAutoTUnref<GrCustomStage> stage;
1514 bool swapRAndB = (fGpu->preferredReadPixelsConfig(config) == GrPixelConfigSwapRAndB(config));
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001515
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001516 GrPixelConfig textureConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001517 if (swapRAndB) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001518 textureConfig = GrPixelConfigSwapRAndB(config);
1519 } else {
1520 textureConfig = config;
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001521 }
1522
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001523 GrTextureDesc desc;
1524 desc.fWidth = width;
1525 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001526 desc.fConfig = textureConfig;
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001527 GrAutoScratchTexture ast(this, desc);
1528 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001529 if (NULL == texture) {
1530 return;
1531 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001532 // allocate a tmp buffer and sw convert the pixels to premul
1533 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
1534
1535 if (kUnpremul_PixelOpsFlag & flags) {
1536 if (kRGBA_8888_GrPixelConfig != config && kBGRA_8888_GrPixelConfig != config) {
1537 return;
1538 }
1539 stage.reset(this->createUPMToPMEffect(texture, swapRAndB));
1540 if (NULL == stage) {
1541 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1542 GR_DEBUGCODE(bool success = )
1543 grconfig_to_config8888(config, true, &srcConfig8888);
1544 GrAssert(success);
1545 GR_DEBUGCODE(success = )
1546 grconfig_to_config8888(config, false, &dstConfig8888);
1547 GrAssert(success);
1548 const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer);
1549 tmpPixels.reset(width * height);
1550 SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888,
1551 src, rowBytes, srcConfig8888,
1552 width, height);
1553 buffer = tmpPixels.get();
1554 rowBytes = 4 * width;
1555 }
1556 }
1557 if (NULL == stage) {
1558 stage.reset(GrConfigConversionEffect::Create(texture, swapRAndB));
1559 GrAssert(NULL != stage);
1560 }
1561
1562 this->writeTexturePixels(texture,
1563 0, 0, width, height,
1564 textureConfig, buffer, rowBytes,
1565 flags & ~kUnpremul_PixelOpsFlag);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001566
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001567 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001568 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001569
1570 GrMatrix matrix;
1571 matrix.setTranslate(GrIntToScalar(left), GrIntToScalar(top));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001572 drawState->setViewMatrix(matrix);
1573 drawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001574
bsalomon@google.com5c638652011-07-18 19:31:59 +00001575 matrix.setIDiv(texture->width(), texture->height());
bsalomon@google.comb8670992012-07-25 21:27:09 +00001576 drawState->sampler(0)->reset(matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001577 drawState->sampler(0)->setCustomStage(stage);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001578
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001579 fGpu->drawSimpleRect(GrRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)), NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001580}
1581////////////////////////////////////////////////////////////////////////////////
1582
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001583void GrContext::setPaint(const GrPaint& paint) {
tomhudson@google.comcb325ce2012-07-11 14:41:19 +00001584 GrAssert(fDrawState->stagesDisabled());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001585
1586 for (int i = 0; i < GrPaint::kMaxTextures; ++i) {
1587 int s = i + GrPaint::kFirstTextureStage;
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001588 if (paint.isTextureStageEnabled(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001589 *fDrawState->sampler(s) = paint.getTextureSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001590 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001591 }
1592
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001593 fDrawState->setFirstCoverageStage(GrPaint::kFirstMaskStage);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001594
1595 for (int i = 0; i < GrPaint::kMaxMasks; ++i) {
1596 int s = i + GrPaint::kFirstMaskStage;
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001597 if (paint.isMaskStageEnabled(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001598 *fDrawState->sampler(s) = paint.getMaskSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001599 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001600 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001601
bsalomon@google.com26936d02012-03-19 13:06:19 +00001602 // disable all stages not accessible via the paint
1603 for (int s = GrPaint::kTotalStages; s < GrDrawState::kNumStages; ++s) {
tomhudson@google.com676e6602012-07-10 17:21:48 +00001604 fDrawState->disableStage(s);
bsalomon@google.com26936d02012-03-19 13:06:19 +00001605 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001606
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001607 fDrawState->setColor(paint.fColor);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001608
1609 if (paint.fDither) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001610 fDrawState->enableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001611 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001612 fDrawState->disableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001613 }
1614 if (paint.fAntiAlias) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001615 fDrawState->enableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001616 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001617 fDrawState->disableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001618 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001619 if (paint.fColorMatrixEnabled) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001620 fDrawState->enableState(GrDrawState::kColorMatrix_StateBit);
1621 fDrawState->setColorMatrix(paint.fColorMatrix);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001622 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001623 fDrawState->disableState(GrDrawState::kColorMatrix_StateBit);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001624 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001625 fDrawState->setBlendFunc(paint.fSrcBlendCoeff, paint.fDstBlendCoeff);
1626 fDrawState->setColorFilter(paint.fColorFilterColor, paint.fColorFilterXfermode);
1627 fDrawState->setCoverage(paint.fCoverage);
reed@google.com4b2d3f32012-05-15 18:05:50 +00001628#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
bsalomon@google.come3d32162012-07-20 13:37:06 +00001629 if ((paint.hasMask() || 0xff != paint.fCoverage) &&
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001630 !fGpu->canApplyCoverage()) {
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001631 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1632 }
bsalomon@google.com95cd7bd2012-03-28 15:35:05 +00001633#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001634}
1635
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001636GrDrawTarget* GrContext::prepareToDraw(const GrPaint* paint, BufferedDraw buffered) {
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001637 if (kNo_BufferedDraw == buffered && kYes_BufferedDraw == fLastDrawWasBuffered) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001638 this->flushDrawBuffer();
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001639 fLastDrawWasBuffered = kNo_BufferedDraw;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001640 }
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001641 if (NULL != paint) {
1642 this->setPaint(*paint);
1643 }
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001644 if (kYes_BufferedDraw == buffered) {
1645 fDrawBuffer->setClip(fGpu->getClip());
1646 fLastDrawWasBuffered = kYes_BufferedDraw;
1647 return fDrawBuffer;
1648 } else {
1649 GrAssert(kNo_BufferedDraw == buffered);
1650 return fGpu;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001651 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001652}
1653
robertphillips@google.com72176b22012-05-23 13:19:12 +00001654/*
1655 * This method finds a path renderer that can draw the specified path on
1656 * the provided target.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001657 * Due to its expense, the software path renderer has split out so it can
robertphillips@google.com72176b22012-05-23 13:19:12 +00001658 * can be individually allowed/disallowed via the "allowSW" boolean.
1659 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001660GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
bsalomon@google.com289533a2011-10-27 12:34:25 +00001661 GrPathFill fill,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001662 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001663 bool antiAlias,
1664 bool allowSW) {
bsalomon@google.com30085192011-08-19 15:42:31 +00001665 if (NULL == fPathRendererChain) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001666 fPathRendererChain =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001667 SkNEW_ARGS(GrPathRendererChain,
1668 (this, GrPathRendererChain::kNone_UsageFlag));
bsalomon@google.com30085192011-08-19 15:42:31 +00001669 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001670
1671 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path, fill,
1672 target,
1673 antiAlias);
1674
1675 if (NULL == pr && allowSW) {
1676 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001677 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001678 }
1679
1680 pr = fSoftwarePathRenderer;
1681 }
1682
1683 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001684}
1685
bsalomon@google.com27847de2011-02-22 20:59:41 +00001686////////////////////////////////////////////////////////////////////////////////
1687
bsalomon@google.com27847de2011-02-22 20:59:41 +00001688void GrContext::setRenderTarget(GrRenderTarget* target) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001689 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001690 fDrawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001691}
1692
1693GrRenderTarget* GrContext::getRenderTarget() {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001694 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001695}
1696
1697const GrRenderTarget* GrContext::getRenderTarget() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001698 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001699}
1700
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001701bool GrContext::isConfigRenderable(GrPixelConfig config) const {
1702 return fGpu->isConfigRenderable(config);
1703}
1704
bsalomon@google.com27847de2011-02-22 20:59:41 +00001705const GrMatrix& GrContext::getMatrix() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001706 return fDrawState->getViewMatrix();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001707}
1708
1709void GrContext::setMatrix(const GrMatrix& m) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001710 fDrawState->setViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001711}
1712
1713void GrContext::concatMatrix(const GrMatrix& m) const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001714 fDrawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001715}
1716
1717static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
1718 intptr_t mask = 1 << shift;
1719 if (pred) {
1720 bits |= mask;
1721 } else {
1722 bits &= ~mask;
1723 }
1724 return bits;
1725}
1726
bsalomon@google.com583a1e32011-08-17 13:42:46 +00001727GrContext::GrContext(GrGpu* gpu) {
bsalomon@google.comc0af3172012-06-15 14:10:09 +00001728 ++THREAD_INSTANCE_COUNT;
1729
bsalomon@google.com27847de2011-02-22 20:59:41 +00001730 fGpu = gpu;
1731 fGpu->ref();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001732 fGpu->setContext(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001733
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001734 fDrawState = SkNEW(GrDrawState);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001735 fGpu->setDrawState(fDrawState);
1736
bsalomon@google.com30085192011-08-19 15:42:31 +00001737 fPathRendererChain = NULL;
robertphillips@google.com72176b22012-05-23 13:19:12 +00001738 fSoftwarePathRenderer = NULL;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001739
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001740 fTextureCache = SkNEW_ARGS(GrResourceCache,
1741 (MAX_TEXTURE_CACHE_COUNT,
1742 MAX_TEXTURE_CACHE_BYTES));
1743 fFontCache = SkNEW_ARGS(GrFontCache, (fGpu));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001744
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001745 fLastDrawWasBuffered = kNo_BufferedDraw;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001746
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001747 fDrawBuffer = NULL;
1748 fDrawBufferVBAllocPool = NULL;
1749 fDrawBufferIBAllocPool = NULL;
1750
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001751 fAARectRenderer = SkNEW(GrAARectRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001752
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001753 fDidTestPMConversions = false;
1754
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001755 this->setupDrawBuffer();
1756}
1757
1758void GrContext::setupDrawBuffer() {
1759
1760 GrAssert(NULL == fDrawBuffer);
1761 GrAssert(NULL == fDrawBufferVBAllocPool);
1762 GrAssert(NULL == fDrawBufferIBAllocPool);
1763
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001764 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001765 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001766 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001767 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001768 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001769 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001770 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001771 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001772
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001773 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com471d4712011-08-23 15:45:25 +00001774 fDrawBufferVBAllocPool,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001775 fDrawBufferIBAllocPool));
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001776
bsalomon@google.com27847de2011-02-22 20:59:41 +00001777 fDrawBuffer->setQuadIndexBuffer(this->getQuadIndexBuffer());
bsalomon@google.com1015e032012-06-25 18:41:04 +00001778 if (fDrawBuffer) {
1779 fDrawBuffer->setAutoFlushTarget(fGpu);
1780 fDrawBuffer->setDrawState(fDrawState);
1781 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001782}
1783
bsalomon@google.com27847de2011-02-22 20:59:41 +00001784GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) {
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001785 return prepareToDraw(&paint, DEFAULT_BUFFERING);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001786}
1787
1788const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1789 return fGpu->getQuadIndexBuffer();
1790}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001791
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001792namespace {
1793void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
1794 GrConfigConversionEffect::PMConversion pmToUPM;
1795 GrConfigConversionEffect::PMConversion upmToPM;
1796 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
1797 *pmToUPMValue = pmToUPM;
1798 *upmToPMValue = upmToPM;
1799}
1800}
1801
1802GrCustomStage* GrContext::createPMToUPMEffect(GrTexture* texture, bool swapRAndB) {
1803 if (!fDidTestPMConversions) {
1804 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001805 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001806 }
1807 GrConfigConversionEffect::PMConversion pmToUPM =
1808 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
1809 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
1810 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM);
1811 } else {
1812 return NULL;
1813 }
1814}
1815
1816GrCustomStage* GrContext::createUPMToPMEffect(GrTexture* texture, bool swapRAndB) {
1817 if (!fDidTestPMConversions) {
1818 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001819 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001820 }
1821 GrConfigConversionEffect::PMConversion upmToPM =
1822 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
1823 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
1824 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM);
1825 } else {
1826 return NULL;
1827 }
1828}
1829
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001830GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture,
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001831 bool canClobberSrc,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001832 const SkRect& rect,
1833 float sigmaX, float sigmaY) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001834 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001835 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001836 AutoMatrix avm(this, GrMatrix::I());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001837 SkIRect clearRect;
bsalomon@google.comb505a122012-05-31 18:40:36 +00001838 int scaleFactorX, radiusX;
1839 int scaleFactorY, radiusY;
1840 sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX);
1841 sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY);
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001842
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001843 SkRect srcRect(rect);
1844 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
1845 srcRect.roundOut();
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001846 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001847 static_cast<float>(scaleFactorY));
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001848
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001849 AutoClip acs(this, srcRect);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001850
bsalomon@google.com0342a852012-08-20 19:22:38 +00001851 GrAssert(kBGRA_8888_GrPixelConfig == srcTexture->config() ||
1852 kRGBA_8888_GrPixelConfig == srcTexture->config() ||
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001853 kAlpha_8_GrPixelConfig == srcTexture->config());
1854
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001855 GrTextureDesc desc;
1856 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1857 desc.fWidth = SkScalarFloorToInt(srcRect.width());
1858 desc.fHeight = SkScalarFloorToInt(srcRect.height());
1859 desc.fConfig = srcTexture->config();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001860
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001861 GrAutoScratchTexture temp1, temp2;
1862 GrTexture* dstTexture = temp1.set(this, desc);
1863 GrTexture* tempTexture = canClobberSrc ? srcTexture : temp2.set(this, desc);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001864
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001865 GrPaint paint;
1866 paint.reset();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001867
1868 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
1869 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1870 srcTexture->height());
1871 this->setRenderTarget(dstTexture->asRenderTarget());
1872 SkRect dstRect(srcRect);
1873 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
1874 i < scaleFactorY ? 0.5f : 1.0f);
tomhudson@google.comaa72eab2012-07-19 18:01:07 +00001875 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001876 (srcTexture, true)))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001877 this->drawRectToRect(paint, dstRect, srcRect);
1878 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001879 srcTexture = dstTexture;
1880 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001881 }
1882
robertphillips@google.com7a396332012-05-10 15:11:27 +00001883 SkIRect srcIRect;
1884 srcRect.roundOut(&srcIRect);
1885
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001886 if (sigmaX > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001887 if (scaleFactorX > 1) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001888 // Clear out a radius to the right of the srcRect to prevent the
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001889 // X convolution from reading garbage.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001890 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001891 radiusX, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001892 this->clear(&clearRect, 0x0);
1893 }
1894
1895 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001896 GrDrawTarget* target = this->prepareToDraw(NULL, DEFAULT_BUFFERING);
1897 convolve_gaussian(target, srcTexture, srcRect, sigmaX, radiusX,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001898 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001899 srcTexture = dstTexture;
1900 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001901 }
1902
1903 if (sigmaY > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001904 if (scaleFactorY > 1 || sigmaX > 0.0f) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001905 // Clear out a radius below the srcRect to prevent the Y
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001906 // convolution from reading garbage.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001907 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001908 srcIRect.width(), radiusY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001909 this->clear(&clearRect, 0x0);
1910 }
1911
1912 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001913 GrDrawTarget* target = this->prepareToDraw(NULL, DEFAULT_BUFFERING);
1914 convolve_gaussian(target, srcTexture, srcRect, sigmaY, radiusY,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001915 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001916 srcTexture = dstTexture;
1917 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001918 }
1919
1920 if (scaleFactorX > 1 || scaleFactorY > 1) {
1921 // Clear one pixel to the right and below, to accommodate bilinear
1922 // upsampling.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001923 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
robertphillips@google.com7a396332012-05-10 15:11:27 +00001924 srcIRect.width() + 1, 1);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001925 this->clear(&clearRect, 0x0);
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001926 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
robertphillips@google.com7a396332012-05-10 15:11:27 +00001927 1, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001928 this->clear(&clearRect, 0x0);
1929 // FIXME: This should be mitchell, not bilinear.
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,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001934 (srcTexture, true)))->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