blob: f91f4206c2acdecd1339022c854e0e971b30e352 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
bsalomon@google.com27847de2011-02-22 20:59:41 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
bsalomon@google.com27847de2011-02-22 20:59:41 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
bsalomon@google.com1fadb202011-12-12 16:10:08 +000010#include "GrContext.h"
11
bsalomon@google.comb505a122012-05-31 18:40:36 +000012#include "effects/GrConvolutionEffect.h"
tomhudson@google.comaa72eab2012-07-19 18:01:07 +000013#include "effects/GrSingleTextureEffect.h"
bsalomon@google.coma04e8e82012-08-27 12:53:13 +000014#include "effects/GrConfigConversionEffect.h"
bsalomon@google.comb505a122012-05-31 18:40:36 +000015
tomhudson@google.com278cbb42011-06-30 19:37:01 +000016#include "GrBufferAllocPool.h"
bsalomon@google.com05ef5102011-05-02 21:14:59 +000017#include "GrGpu.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000018#include "GrIndexBuffer.h"
19#include "GrInOrderDrawBuffer.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000020#include "GrPathRenderer.h"
tomhudson@google.comd22b6e42011-06-24 15:53:40 +000021#include "GrPathUtils.h"
bsalomon@google.com50398bf2011-07-26 20:45:30 +000022#include "GrResourceCache.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000023#include "GrSoftwarePathRenderer.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000024#include "GrStencilBuffer.h"
tomhudson@google.com278cbb42011-06-30 19:37:01 +000025#include "GrTextStrike.h"
bsalomon@google.com8c2fe992011-09-13 15:27:18 +000026#include "SkTLazy.h"
bsalomon@google.comc0af3172012-06-15 14:10:09 +000027#include "SkTLS.h"
tomhudson@google.com0c8d93a2011-07-01 17:08:26 +000028#include "SkTrace.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000029
reed@google.comfa35e3d2012-06-26 20:16:17 +000030SK_DEFINE_INST_COUNT(GrContext)
31SK_DEFINE_INST_COUNT(GrDrawState)
32
bsalomon@google.com1d4edd32012-08-16 18:36:06 +000033// It can be useful to set this to kNo_BufferedDraw to test whether a bug is caused by using the
34// InOrderDrawBuffer, to compare performance of using/not using InOrderDrawBuffer, or to make
35// debugging easier.
36#define DEFAULT_BUFFERING (GR_DISABLE_DRAW_BUFFERING ? kNo_BufferedDraw : kYes_BufferedDraw)
bsalomon@google.com27847de2011-02-22 20:59:41 +000037
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +000038#define MAX_BLUR_SIGMA 4.0f
39
bsalomon@google.comd46e2422011-09-23 17:40:07 +000040// When we're using coverage AA but the blend is incompatible (given gpu
41// limitations) should we disable AA or draw wrong?
bsalomon@google.com950d7a82011-09-28 15:05:33 +000042#define DISABLE_COVERAGE_AA_FOR_BLEND 1
bsalomon@google.comd46e2422011-09-23 17:40:07 +000043
reed@google.com4b2d3f32012-05-15 18:05:50 +000044#if GR_DEBUG
45 // change this to a 1 to see notifications when partial coverage fails
46 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
47#else
48 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
49#endif
50
bsalomon@google.com07fc0d12012-06-22 15:15:59 +000051static const size_t MAX_TEXTURE_CACHE_COUNT = 256;
52static const size_t MAX_TEXTURE_CACHE_BYTES = 16 * 1024 * 1024;
bsalomon@google.com27847de2011-02-22 20:59:41 +000053
bsalomon@google.com60361492012-03-15 17:47:06 +000054static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
bsalomon@google.com27847de2011-02-22 20:59:41 +000055static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
56
bsalomon@google.com1d4edd32012-08-16 18:36:06 +000057static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = 1 << 11;
58static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = 4;
bsalomon@google.com27847de2011-02-22 20:59:41 +000059
bsalomon@google.combc4b6542011-11-19 13:56:11 +000060#define ASSERT_OWNED_RESOURCE(R) GrAssert(!(R) || (R)->getContext() == this)
61
bsalomon@google.com05ef5102011-05-02 21:14:59 +000062GrContext* GrContext::Create(GrEngine engine,
63 GrPlatform3DContext context3D) {
bsalomon@google.com27847de2011-02-22 20:59:41 +000064 GrContext* ctx = NULL;
65 GrGpu* fGpu = GrGpu::Create(engine, context3D);
66 if (NULL != fGpu) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000067 ctx = SkNEW_ARGS(GrContext, (fGpu));
bsalomon@google.com27847de2011-02-22 20:59:41 +000068 fGpu->unref();
69 }
70 return ctx;
71}
72
bsalomon@google.comc0af3172012-06-15 14:10:09 +000073namespace {
74void* CreateThreadInstanceCount() {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000075 return SkNEW_ARGS(int, (0));
bsalomon@google.comc0af3172012-06-15 14:10:09 +000076}
77void DeleteThreadInstanceCount(void* v) {
78 delete reinterpret_cast<int*>(v);
79}
80#define THREAD_INSTANCE_COUNT \
81 (*reinterpret_cast<int*>(SkTLS::Get(CreateThreadInstanceCount, \
82 DeleteThreadInstanceCount)))
83
84}
85
86int GrContext::GetThreadInstanceCount() {
87 return THREAD_INSTANCE_COUNT;
88}
89
bsalomon@google.com27847de2011-02-22 20:59:41 +000090GrContext::~GrContext() {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000091 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +000092
93 // Since the gpu can hold scratch textures, give it a chance to let go
94 // of them before freeing the texture cache
95 fGpu->purgeResources();
96
bsalomon@google.com27847de2011-02-22 20:59:41 +000097 delete fTextureCache;
98 delete fFontCache;
99 delete fDrawBuffer;
100 delete fDrawBufferVBAllocPool;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000101 delete fDrawBufferIBAllocPool;
bsalomon@google.com30085192011-08-19 15:42:31 +0000102
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000103 fAARectRenderer->unref();
104
bsalomon@google.com205d4602011-04-25 12:43:45 +0000105 fGpu->unref();
bsalomon@google.com30085192011-08-19 15:42:31 +0000106 GrSafeUnref(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000107 GrSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000108 fDrawState->unref();
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000109
110 --THREAD_INSTANCE_COUNT;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000111}
112
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000113void GrContext::contextLost() {
junov@google.com53a55842011-06-08 22:55:10 +0000114 contextDestroyed();
115 this->setupDrawBuffer();
116}
117
118void GrContext::contextDestroyed() {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000119 // abandon first to so destructors
120 // don't try to free the resources in the API.
121 fGpu->abandonResources();
122
bsalomon@google.com30085192011-08-19 15:42:31 +0000123 // a path renderer may be holding onto resources that
124 // are now unusable
125 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000126 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000127
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000128 delete fDrawBuffer;
129 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000130
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000131 delete fDrawBufferVBAllocPool;
132 fDrawBufferVBAllocPool = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000133
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000134 delete fDrawBufferIBAllocPool;
135 fDrawBufferIBAllocPool = NULL;
136
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000137 fAARectRenderer->reset();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000138
bsalomon@google.coma2921122012-08-28 12:34:17 +0000139 fTextureCache->purgeAllUnlocked();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000140 fFontCache->freeAll();
141 fGpu->markContextDirty();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000142}
143
144void GrContext::resetContext() {
145 fGpu->markContextDirty();
146}
147
148void GrContext::freeGpuResources() {
149 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000150
robertphillips@google.comff175842012-05-14 19:31:39 +0000151 fGpu->purgeResources();
152
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000153 fAARectRenderer->reset();
154
bsalomon@google.coma2921122012-08-28 12:34:17 +0000155 fTextureCache->purgeAllUnlocked();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000156 fFontCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000157 // a path renderer may be holding onto resources
158 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000159 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000160}
161
twiz@google.com05e70242012-01-27 19:12:00 +0000162size_t GrContext::getGpuTextureCacheBytes() const {
163 return fTextureCache->getCachedResourceBytes();
164}
165
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000166////////////////////////////////////////////////////////////////////////////////
167
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000168namespace {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000169
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000170void scale_rect(SkRect* rect, float xScale, float yScale) {
robertphillips@google.com5af56062012-04-27 15:39:52 +0000171 rect->fLeft = SkScalarMul(rect->fLeft, SkFloatToScalar(xScale));
172 rect->fTop = SkScalarMul(rect->fTop, SkFloatToScalar(yScale));
173 rect->fRight = SkScalarMul(rect->fRight, SkFloatToScalar(xScale));
174 rect->fBottom = SkScalarMul(rect->fBottom, SkFloatToScalar(yScale));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000175}
176
bsalomon@google.comb505a122012-05-31 18:40:36 +0000177float adjust_sigma(float sigma, int *scaleFactor, int *radius) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000178 *scaleFactor = 1;
179 while (sigma > MAX_BLUR_SIGMA) {
180 *scaleFactor *= 2;
181 sigma *= 0.5f;
182 }
bsalomon@google.comb505a122012-05-31 18:40:36 +0000183 *radius = static_cast<int>(ceilf(sigma * 3.0f));
184 GrAssert(*radius <= GrConvolutionEffect::kMaxKernelRadius);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000185 return sigma;
186}
187
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000188void convolve_gaussian(GrDrawTarget* target,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000189 GrTexture* texture,
190 const SkRect& rect,
191 float sigma,
192 int radius,
193 Gr1DKernelEffect::Direction direction) {
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000194 GrRenderTarget* rt = target->drawState()->getRenderTarget();
195 GrDrawTarget::AutoStateRestore asr(target, GrDrawTarget::kReset_ASRInit);
196 GrDrawState* drawState = target->drawState();
197 drawState->setRenderTarget(rt);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000198 GrMatrix sampleM;
199 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000200 drawState->sampler(0)->reset(sampleM);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000201 SkAutoTUnref<GrConvolutionEffect> conv(SkNEW_ARGS(GrConvolutionEffect,
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000202 (texture, direction, radius,
203 sigma)));
bsalomon@google.comb505a122012-05-31 18:40:36 +0000204 drawState->sampler(0)->setCustomStage(conv);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000205 target->drawSimpleRect(rect, NULL);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000206}
207
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000208}
209
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000210
211GrTexture* GrContext::findTexture(const GrCacheKey& key) {
212 return static_cast<GrTexture*>(fTextureCache->find(key.key()));
213}
214
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000215GrTexture* GrContext::findAndLockTexture(const GrTextureDesc& desc,
216 const GrCacheData& cacheData,
217 const GrTextureParams* params) {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000218 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000219 GrResource* resource = fTextureCache->findAndLock(resourceKey);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000220 return static_cast<GrTexture*>(resource);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000221}
222
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000223bool GrContext::isTextureInCache(const GrTextureDesc& desc,
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000224 const GrCacheData& cacheData,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000225 const GrTextureParams* params) const {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000226 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000227 return fTextureCache->hasKey(resourceKey);
228}
229
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000230void GrContext::addAndLockStencilBuffer(GrStencilBuffer* sb) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000231 ASSERT_OWNED_RESOURCE(sb);
robertphillips@google.com46a86002012-08-08 10:42:44 +0000232
233 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(sb->width(),
234 sb->height(),
235 sb->numSamples());
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000236 fTextureCache->createAndLock(resourceKey, sb);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000237}
238
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000239GrStencilBuffer* GrContext::findAndLockStencilBuffer(int width, int height,
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000240 int sampleCnt) {
robertphillips@google.com46a86002012-08-08 10:42:44 +0000241 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(width,
242 height,
243 sampleCnt);
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000244 GrResource* resource = fTextureCache->findAndLock(resourceKey);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000245 return static_cast<GrStencilBuffer*>(resource);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000246}
247
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000248void GrContext::unlockStencilBuffer(GrStencilBuffer* sb) {
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000249
250 if (NULL == sb->getCacheEntry()) {
251 // This can happen when the GrResourceCache is being deleted. If
252 // a stencil buffer was evicted before its reffing render targets,
253 // the render targets will attempt to unlock the stencil buffer
254 // when they are deleted.
255 return;
256 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000257
robertphillips@google.com667c3a32012-09-06 14:50:45 +0000258 // If the texture cache still exists we know the GrGpu & GrContext still
259 // exist so we can verify ownership.
260 ASSERT_OWNED_RESOURCE(sb);
261
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000262 fTextureCache->unlock(sb->getCacheEntry());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000263}
264
265static void stretchImage(void* dst,
266 int dstW,
267 int dstH,
268 void* src,
269 int srcW,
270 int srcH,
271 int bpp) {
272 GrFixed dx = (srcW << 16) / dstW;
273 GrFixed dy = (srcH << 16) / dstH;
274
275 GrFixed y = dy >> 1;
276
277 int dstXLimit = dstW*bpp;
278 for (int j = 0; j < dstH; ++j) {
279 GrFixed x = dx >> 1;
280 void* srcRow = (uint8_t*)src + (y>>16)*srcW*bpp;
281 void* dstRow = (uint8_t*)dst + j*dstW*bpp;
282 for (int i = 0; i < dstXLimit; i += bpp) {
283 memcpy((uint8_t*) dstRow + i,
284 (uint8_t*) srcRow + (x>>16)*bpp,
285 bpp);
286 x += dx;
287 }
288 y += dy;
289 }
290}
291
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000292// The desired texture is NPOT and tiled but that isn't supported by
robertphillips@google.com3319f332012-08-13 18:00:36 +0000293// the current hardware. Resize the texture to be a POT
294GrTexture* GrContext::createResizedTexture(const GrTextureDesc& desc,
295 const GrCacheData& cacheData,
296 void* srcData,
297 size_t rowBytes,
298 bool needsFiltering) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000299 GrTexture* clampedTexture = this->findAndLockTexture(desc, cacheData, NULL);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000300
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000301 if (NULL == clampedTexture) {
302 clampedTexture = this->createAndLockTexture(NULL, desc, cacheData, srcData, rowBytes);
303 GrAssert(NULL != clampedTexture);
304 if (NULL == clampedTexture) {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000305 return NULL;
306 }
307 }
308 GrTextureDesc rtDesc = desc;
309 rtDesc.fFlags = rtDesc.fFlags |
310 kRenderTarget_GrTextureFlagBit |
311 kNoStencil_GrTextureFlagBit;
312 rtDesc.fWidth = GrNextPow2(GrMax(desc.fWidth, 64));
313 rtDesc.fHeight = GrNextPow2(GrMax(desc.fHeight, 64));
314
315 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
316
317 if (NULL != texture) {
318 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
319 GrDrawState* drawState = fGpu->drawState();
320 drawState->setRenderTarget(texture->asRenderTarget());
321
322 // if filtering is not desired then we want to ensure all
323 // texels in the resampled image are copies of texels from
324 // the original.
325 drawState->sampler(0)->reset(SkShader::kClamp_TileMode,
326 needsFiltering);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000327 drawState->createTextureEffect(0, clampedTexture);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000328
329 static const GrVertexLayout layout =
330 GrDrawTarget::StageTexCoordVertexLayoutBit(0,0);
331 GrDrawTarget::AutoReleaseGeometry arg(fGpu, layout, 4, 0);
332
333 if (arg.succeeded()) {
334 GrPoint* verts = (GrPoint*) arg.vertices();
335 verts[0].setIRectFan(0, 0,
336 texture->width(),
337 texture->height(),
338 2*sizeof(GrPoint));
339 verts[1].setIRectFan(0, 0, 1, 1, 2*sizeof(GrPoint));
340 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType,
341 0, 4);
342 }
343 texture->releaseRenderTarget();
344 } else {
345 // TODO: Our CPU stretch doesn't filter. But we create separate
346 // stretched textures when the sampler state is either filtered or
347 // not. Either implement filtered stretch blit on CPU or just create
348 // one when FBO case fails.
349
350 rtDesc.fFlags = kNone_GrTextureFlags;
351 // no longer need to clamp at min RT size.
352 rtDesc.fWidth = GrNextPow2(desc.fWidth);
353 rtDesc.fHeight = GrNextPow2(desc.fHeight);
354 int bpp = GrBytesPerPixel(desc.fConfig);
355 SkAutoSMalloc<128*128*4> stretchedPixels(bpp *
356 rtDesc.fWidth *
357 rtDesc.fHeight);
358 stretchImage(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
359 srcData, desc.fWidth, desc.fHeight, bpp);
360
361 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
362
363 GrTexture* texture = fGpu->createTexture(rtDesc,
364 stretchedPixels.get(),
365 stretchedRowBytes);
366 GrAssert(NULL != texture);
367 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000368 this->unlockTexture(clampedTexture);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000369
370 return texture;
371}
372
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000373GrTexture* GrContext::createAndLockTexture(
bsalomon@google.comb8670992012-07-25 21:27:09 +0000374 const GrTextureParams* params,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000375 const GrTextureDesc& desc,
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000376 const GrCacheData& cacheData,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000377 void* srcData,
378 size_t rowBytes) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000379 SK_TRACE_EVENT0("GrContext::createAndLockTexture");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000380
381#if GR_DUMP_TEXTURE_UPLOAD
382 GrPrintf("GrContext::createAndLockTexture [%d %d]\n", desc.fWidth, desc.fHeight);
383#endif
384
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000385 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000386
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +0000387 SkAutoTUnref<GrTexture> texture;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000388 if (GrTexture::NeedsResizing(resourceKey)) {
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +0000389 texture.reset(this->createResizedTexture(desc, cacheData,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000390 srcData, rowBytes,
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +0000391 GrTexture::NeedsFiltering(resourceKey)));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000392 } else {
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +0000393 texture.reset(fGpu->createTexture(desc, srcData, rowBytes));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000394 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000395
396 if (NULL != texture) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000397 fTextureCache->createAndLock(resourceKey, texture);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000398 }
399
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000400 return texture;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000401}
402
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000403GrTexture* GrContext::lockScratchTexture(const GrTextureDesc& inDesc,
404 ScratchTexMatch match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000405 GrTextureDesc desc = inDesc;
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000406 GrCacheData cacheData(GrCacheData::kScratch_CacheID);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000407
rileya@google.com2afb8ec2012-08-23 14:08:57 +0000408 GrAssert((desc.fFlags & kRenderTarget_GrTextureFlagBit) ||
409 !(desc.fFlags & kNoStencil_GrTextureFlagBit));
410
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000411 if (kExact_ScratchTexMatch != match) {
412 // bin by pow2 with a reasonable min
413 static const int MIN_SIZE = 256;
414 desc.fWidth = GrMax(MIN_SIZE, GrNextPow2(desc.fWidth));
415 desc.fHeight = GrMax(MIN_SIZE, GrNextPow2(desc.fHeight));
416 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000417
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000418 GrResource* resource = NULL;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000419 int origWidth = desc.fWidth;
420 int origHeight = desc.fHeight;
421 bool doubledW = false;
422 bool doubledH = false;
423
424 do {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000425 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL, desc, cacheData, true);
robertphillips@google.comd6bbbf82012-09-05 15:46:34 +0000426 resource = fTextureCache->findAndLock(key);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000427 // if we miss, relax the fit of the flags...
428 // then try doubling width... then height.
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000429 if (NULL != resource || kExact_ScratchTexMatch == match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000430 break;
431 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000432 // We no longer try to reuse textures that were previously used as render targets in
rileya@google.com2afb8ec2012-08-23 14:08:57 +0000433 // situations where no RT is needed; doing otherwise can confuse the video driver and
434 // cause significant performance problems in some cases.
435 if (desc.fFlags & kNoStencil_GrTextureFlagBit) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000436 desc.fFlags = desc.fFlags & ~kNoStencil_GrTextureFlagBit;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000437 } else if (!doubledW) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000438 desc.fFlags = inDesc.fFlags;
439 desc.fWidth *= 2;
440 doubledW = true;
441 } else if (!doubledH) {
442 desc.fFlags = inDesc.fFlags;
443 desc.fWidth = origWidth;
444 desc.fHeight *= 2;
445 doubledH = true;
446 } else {
447 break;
448 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000449
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000450 } while (true);
451
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000452 if (NULL == resource) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000453 desc.fFlags = inDesc.fFlags;
454 desc.fWidth = origWidth;
455 desc.fHeight = origHeight;
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +0000456 SkAutoTUnref<GrTexture> texture(fGpu->createTexture(desc, NULL, 0));
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000457 if (NULL != texture) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000458 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000459 texture->desc(),
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000460 cacheData,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000461 true);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000462 fTextureCache->createAndLock(key, texture);
463 resource = texture;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000464 }
465 }
466
467 // If the caller gives us the same desc/sampler twice we don't want
468 // to return the same texture the second time (unless it was previously
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000469 // released). So make it exclusive to hide it from future searches.
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000470 if (NULL != resource) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000471 fTextureCache->makeExclusive(resource->getCacheEntry());
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000472 }
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000473
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000474 return static_cast<GrTexture*>(resource);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000475}
476
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000477void GrContext::addExistingTextureToCache(GrTexture* texture) {
478
479 if (NULL == texture) {
480 return;
481 }
482
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000483 // This texture should already have a cache entry since it was once
484 // attached
485 GrAssert(NULL != texture->getCacheEntry());
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000486
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000487 // Conceptually, the cache entry is going to assume responsibility
488 // for the creation ref.
489 GrAssert(1 == texture->getRefCnt());
490
491 // Since this texture came from an AutoScratchTexture it should
492 // still be in the exclusive pile
493 fTextureCache->makeNonExclusive(texture->getCacheEntry());
494
495 // and it should still be locked
496 fTextureCache->unlock(texture->getCacheEntry());
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000497}
498
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000499void GrContext::lockTexture(GrTexture* texture) {
500
501 if (NULL == texture->getCacheEntry()) {
502 // not in the cache
503 GrAssert(0);
504 return;
505 }
506
507 fTextureCache->lock(texture->getCacheEntry());
508}
509
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000510void GrContext::unlockTexture(GrTexture* texture) {
511 ASSERT_OWNED_RESOURCE(texture);
512 GrAssert(NULL != texture->getCacheEntry());
513
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000514 // If this is a scratch texture we detached it from the cache
515 // while it was locked (to avoid two callers simultaneously getting
516 // the same texture).
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000517 if (GrTexture::IsScratchTexture(texture->getCacheEntry()->key())) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000518 fTextureCache->makeNonExclusive(texture->getCacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000519 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000520
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000521 fTextureCache->unlock(texture->getCacheEntry());
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000522}
523
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000524GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000525 void* srcData,
526 size_t rowBytes) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000527 GrTextureDesc descCopy = descIn;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000528 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000529}
530
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000531void GrContext::getTextureCacheLimits(int* maxTextures,
532 size_t* maxTextureBytes) const {
533 fTextureCache->getLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000534}
535
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000536void GrContext::setTextureCacheLimits(int maxTextures, size_t maxTextureBytes) {
537 fTextureCache->setLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000538}
539
bsalomon@google.com91958362011-06-13 17:58:13 +0000540int GrContext::getMaxTextureSize() const {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000541 return fGpu->getCaps().maxTextureSize();
bsalomon@google.com91958362011-06-13 17:58:13 +0000542}
543
544int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000545 return fGpu->getCaps().maxRenderTargetSize();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000546}
547
548///////////////////////////////////////////////////////////////////////////////
549
bsalomon@google.come269f212011-11-07 13:29:52 +0000550GrTexture* GrContext::createPlatformTexture(const GrPlatformTextureDesc& desc) {
551 return fGpu->createPlatformTexture(desc);
552}
553
554GrRenderTarget* GrContext::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
555 return fGpu->createPlatformRenderTarget(desc);
556}
557
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000558///////////////////////////////////////////////////////////////////////////////
559
bsalomon@google.comb8670992012-07-25 21:27:09 +0000560bool GrContext::supportsIndex8PixelConfig(const GrTextureParams* params,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000561 int width, int height) const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000562 const GrDrawTarget::Caps& caps = fGpu->getCaps();
bsalomon@google.comf6601872012-08-28 21:11:35 +0000563 if (!caps.eightBitPaletteSupport()) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000564 return false;
565 }
566
bsalomon@google.com27847de2011-02-22 20:59:41 +0000567 bool isPow2 = GrIsPow2(width) && GrIsPow2(height);
568
569 if (!isPow2) {
bsalomon@google.comb8670992012-07-25 21:27:09 +0000570 bool tiled = NULL != params && params->isTiled();
bsalomon@google.comf6601872012-08-28 21:11:35 +0000571 if (tiled && !caps.npotTextureTileSupport()) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000572 return false;
573 }
574 }
575 return true;
576}
577
578////////////////////////////////////////////////////////////////////////////////
579
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000580const GrClipData* GrContext::getClip() const {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000581 return fGpu->getClip();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000582}
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000583
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000584void GrContext::setClip(const GrClipData* clipData) {
585 fGpu->setClip(clipData);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000586 fDrawState->enableState(GrDrawState::kClip_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000587}
588
bsalomon@google.com27847de2011-02-22 20:59:41 +0000589////////////////////////////////////////////////////////////////////////////////
590
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000591void GrContext::clear(const GrIRect* rect,
592 const GrColor color,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000593 GrRenderTarget* target) {
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000594 this->prepareToDraw(NULL, DEFAULT_BUFFERING)->clear(rect, color, target);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000595}
596
597void GrContext::drawPaint(const GrPaint& paint) {
598 // set rect to be big enough to fill the space, but not super-huge, so we
599 // don't overflow fixed-point implementations
bsalomon@google.comd302f142011-03-03 13:54:13 +0000600 GrRect r;
601 r.setLTRB(0, 0,
602 GrIntToScalar(getRenderTarget()->width()),
603 GrIntToScalar(getRenderTarget()->height()));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000604 GrMatrix inverse;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000605 SkTLazy<GrPaint> tmpPaint;
606 const GrPaint* p = &paint;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000607 AutoMatrix am;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000608
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000609 // We attempt to map r by the inverse matrix and draw that. mapRect will
610 // map the four corners and bound them with a new rect. This will not
611 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000612 if (!this->getMatrix().hasPerspective()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000613 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000614 GrPrintf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000615 return;
616 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000617 inverse.mapRect(&r);
618 } else {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000619 if (paint.hasTextureOrMask()) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000620 tmpPaint.set(paint);
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000621 p = tmpPaint.get();
bsalomon@google.come3d32162012-07-20 13:37:06 +0000622 if (!tmpPaint.get()->preConcatSamplerMatricesWithInverse(fDrawState->getViewMatrix())) {
623 GrPrintf("Could not invert matrix\n");
624 }
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000625 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000626 am.set(this, GrMatrix::I());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000627 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000628 // by definition this fills the entire clip, no need for AA
629 if (paint.fAntiAlias) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000630 if (!tmpPaint.isValid()) {
631 tmpPaint.set(paint);
632 p = tmpPaint.get();
633 }
634 GrAssert(p == tmpPaint.get());
635 tmpPaint.get()->fAntiAlias = false;
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000636 }
637 this->drawRect(*p, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000638}
639
bsalomon@google.com205d4602011-04-25 12:43:45 +0000640////////////////////////////////////////////////////////////////////////////////
641
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000642namespace {
643inline bool disable_coverage_aa_for_blend(GrDrawTarget* target) {
644 return DISABLE_COVERAGE_AA_FOR_BLEND && !target->canApplyCoverage();
645}
646}
647
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000648////////////////////////////////////////////////////////////////////////////////
649
bsalomon@google.com27847de2011-02-22 20:59:41 +0000650/* create a triangle strip that strokes the specified triangle. There are 8
651 unique vertices, but we repreat the last 2 to close up. Alternatively we
652 could use an indices array, and then only send 8 verts, but not sure that
653 would be faster.
654 */
bsalomon@google.com205d4602011-04-25 12:43:45 +0000655static void setStrokeRectStrip(GrPoint verts[10], GrRect rect,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000656 GrScalar width) {
657 const GrScalar rad = GrScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000658 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000659
660 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
661 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
662 verts[2].set(rect.fRight - rad, rect.fTop + rad);
663 verts[3].set(rect.fRight + rad, rect.fTop - rad);
664 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
665 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
666 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
667 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
668 verts[8] = verts[0];
669 verts[9] = verts[1];
670}
671
reed@google.com20efde72011-05-09 17:00:02 +0000672/**
673 * Returns true if the rects edges are integer-aligned.
674 */
675static bool isIRect(const GrRect& r) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000676 return GrScalarIsInt(r.fLeft) && GrScalarIsInt(r.fTop) &&
reed@google.com20efde72011-05-09 17:00:02 +0000677 GrScalarIsInt(r.fRight) && GrScalarIsInt(r.fBottom);
678}
679
bsalomon@google.com205d4602011-04-25 12:43:45 +0000680static bool apply_aa_to_rect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000681 const GrRect& rect,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000682 GrScalar width,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000683 const GrMatrix* matrix,
684 GrMatrix* combinedMatrix,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000685 GrRect* devRect,
686 bool* useVertexCoverage) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000687 // we use a simple coverage ramp to do aa on axis-aligned rects
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000688 // we check if the rect will be axis-aligned, and the rect won't land on
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000689 // integer coords.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000690
bsalomon@google.coma3108262011-10-10 14:08:47 +0000691 // we are keeping around the "tweak the alpha" trick because
692 // it is our only hope for the fixed-pipe implementation.
693 // In a shader implementation we can give a separate coverage input
bsalomon@google.com289533a2011-10-27 12:34:25 +0000694 // TODO: remove this ugliness when we drop the fixed-pipe impl
bsalomon@google.coma3108262011-10-10 14:08:47 +0000695 *useVertexCoverage = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000696 if (!target->canTweakAlphaForCoverage()) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000697 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +0000698#if GR_DEBUG
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000699 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000700#endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000701 return false;
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000702 } else {
703 *useVertexCoverage = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000704 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000705 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000706 const GrDrawState& drawState = target->getDrawState();
707 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000708 return false;
709 }
710
bsalomon@google.com471d4712011-08-23 15:45:25 +0000711 if (0 == width && target->willUseHWAALines()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000712 return false;
713 }
714
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000715 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000716 return false;
717 }
718
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000719 if (NULL != matrix &&
bsalomon@google.com205d4602011-04-25 12:43:45 +0000720 !matrix->preservesAxisAlignment()) {
721 return false;
722 }
723
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000724 *combinedMatrix = drawState.getViewMatrix();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000725 if (NULL != matrix) {
726 combinedMatrix->preConcat(*matrix);
727 GrAssert(combinedMatrix->preservesAxisAlignment());
728 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000729
bsalomon@google.com205d4602011-04-25 12:43:45 +0000730 combinedMatrix->mapRect(devRect, rect);
731 devRect->sort();
732
733 if (width < 0) {
reed@google.com20efde72011-05-09 17:00:02 +0000734 return !isIRect(*devRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000735 } else {
736 return true;
737 }
738}
739
bsalomon@google.com27847de2011-02-22 20:59:41 +0000740void GrContext::drawRect(const GrPaint& paint,
741 const GrRect& rect,
742 GrScalar width,
743 const GrMatrix* matrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000744 SK_TRACE_EVENT0("GrContext::drawRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000745
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000746 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000747 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000748
bsalomon@google.com205d4602011-04-25 12:43:45 +0000749 GrRect devRect = rect;
750 GrMatrix combinedMatrix;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000751 bool useVertexCoverage;
bsalomon@google.com289533a2011-10-27 12:34:25 +0000752 bool needAA = paint.fAntiAlias &&
753 !this->getRenderTarget()->isMultisampled();
754 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix,
755 &combinedMatrix, &devRect,
756 &useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000757
758 if (doAA) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000759 GrDrawTarget::AutoDeviceCoordDraw adcd(target);
760 if (!adcd.succeeded()) {
761 return;
762 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000763 if (width >= 0) {
764 GrVec strokeSize;;
765 if (width > 0) {
766 strokeSize.set(width, width);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000767 combinedMatrix.mapVectors(&strokeSize, 1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000768 strokeSize.setAbs(strokeSize);
769 } else {
770 strokeSize.set(GR_Scalar1, GR_Scalar1);
771 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000772 fAARectRenderer->strokeAARect(this->getGpu(), target, devRect,
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000773 strokeSize, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000774 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000775 fAARectRenderer->fillAARect(this->getGpu(), target,
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000776 devRect, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000777 }
778 return;
779 }
780
bsalomon@google.com27847de2011-02-22 20:59:41 +0000781 if (width >= 0) {
782 // TODO: consider making static vertex buffers for these cases.
783 // Hairline could be done by just adding closing vertex to
784 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000785
bsalomon@google.com27847de2011-02-22 20:59:41 +0000786 static const int worstCaseVertCount = 10;
bsalomon@google.come3d32162012-07-20 13:37:06 +0000787 GrDrawTarget::AutoReleaseGeometry geo(target, 0, worstCaseVertCount, 0);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000788
789 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000790 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000791 return;
792 }
793
794 GrPrimitiveType primType;
795 int vertCount;
796 GrPoint* vertex = geo.positions();
797
798 if (width > 0) {
799 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000800 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000801 setStrokeRectStrip(vertex, rect, width);
802 } else {
803 // hairline
804 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000805 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000806 vertex[0].set(rect.fLeft, rect.fTop);
807 vertex[1].set(rect.fRight, rect.fTop);
808 vertex[2].set(rect.fRight, rect.fBottom);
809 vertex[3].set(rect.fLeft, rect.fBottom);
810 vertex[4].set(rect.fLeft, rect.fTop);
811 }
812
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000813 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000814 if (NULL != matrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000815 GrDrawState* drawState = target->drawState();
816 avmr.set(drawState);
817 drawState->preConcatViewMatrix(*matrix);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000818 drawState->preConcatSamplerMatrices(*matrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000819 }
820
821 target->drawNonIndexed(primType, 0, vertCount);
822 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000823#if GR_STATIC_RECT_VB
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000824 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
825 if (NULL == sqVB) {
826 GrPrintf("Failed to create static rect vb.\n");
827 return;
828 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000829 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000830 GrDrawState* drawState = target->drawState();
831 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000832 GrMatrix m;
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000833 m.setAll(rect.width(), 0, rect.fLeft,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000834 0, rect.height(), rect.fTop,
835 0, 0, GrMatrix::I()[8]);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000836
837 if (NULL != matrix) {
838 m.postConcat(*matrix);
839 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000840 drawState->preConcatViewMatrix(m);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000841 drawState->preConcatSamplerMatrices(m);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000842
bsalomon@google.com47059542012-06-06 20:51:20 +0000843 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000844#else
bsalomon@google.come3d32162012-07-20 13:37:06 +0000845 target->drawSimpleRect(rect, matrix);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000846#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000847 }
848}
849
850void GrContext::drawRectToRect(const GrPaint& paint,
851 const GrRect& dstRect,
852 const GrRect& srcRect,
853 const GrMatrix* dstMatrix,
854 const GrMatrix* srcMatrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000855 SK_TRACE_EVENT0("GrContext::drawRectToRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000856
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000857 // srcRect refers to paint's first texture
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000858 if (!paint.isTextureStageEnabled(0)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000859 drawRect(paint, dstRect, -1, dstMatrix);
860 return;
861 }
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000862
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000863 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000864
865#if GR_STATIC_RECT_VB
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000866 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000867 GrDrawState* drawState = target->drawState();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000868 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000869
870 GrMatrix m;
871
872 m.setAll(dstRect.width(), 0, dstRect.fLeft,
873 0, dstRect.height(), dstRect.fTop,
874 0, 0, GrMatrix::I()[8]);
875 if (NULL != dstMatrix) {
876 m.postConcat(*dstMatrix);
877 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000878 drawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000879
bsalomon@google.come3d32162012-07-20 13:37:06 +0000880 // we explicitly setup the correct coords for the first stage. The others
881 // must know about the view matrix change.
882 for (int s = 1; s < GrPaint::kTotalStages; ++s) {
883 if (drawState->isStageEnabled(s)) {
884 drawState->sampler(s)->preConcatMatrix(m);
885 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000886 }
887
bsalomon@google.com27847de2011-02-22 20:59:41 +0000888 m.setAll(srcRect.width(), 0, srcRect.fLeft,
889 0, srcRect.height(), srcRect.fTop,
890 0, 0, GrMatrix::I()[8]);
891 if (NULL != srcMatrix) {
892 m.postConcat(*srcMatrix);
893 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000894 drawState->sampler(GrPaint::kFirstTextureStage)->preConcatMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000895
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000896 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
897 if (NULL == sqVB) {
898 GrPrintf("Failed to create static rect vb.\n");
899 return;
900 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000901 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com47059542012-06-06 20:51:20 +0000902 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000903#else
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000904 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000905
tomhudson@google.com93813632011-10-27 20:21:16 +0000906 const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
907 const GrMatrix* srcMatrices[GrDrawState::kNumStages] = {NULL};
bsalomon@google.com27847de2011-02-22 20:59:41 +0000908 srcRects[0] = &srcRect;
909 srcMatrices[0] = srcMatrix;
910
bsalomon@google.come3d32162012-07-20 13:37:06 +0000911 target->drawRect(dstRect, dstMatrix, srcRects, srcMatrices);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000912#endif
913}
914
915void GrContext::drawVertices(const GrPaint& paint,
916 GrPrimitiveType primitiveType,
917 int vertexCount,
918 const GrPoint positions[],
919 const GrPoint texCoords[],
920 const GrColor colors[],
921 const uint16_t indices[],
922 int indexCount) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000923 SK_TRACE_EVENT0("GrContext::drawVertices");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000924
925 GrDrawTarget::AutoReleaseGeometry geo;
926
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000927 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000928 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000929
bsalomon@google.come3d32162012-07-20 13:37:06 +0000930 GrVertexLayout layout = 0;
931 if (NULL != texCoords) {
932 layout |= GrDrawTarget::StageTexCoordVertexLayoutBit(0, 0);
933 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000934 if (NULL != colors) {
935 layout |= GrDrawTarget::kColor_VertexLayoutBit;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000936 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000937 int vertexSize = GrDrawTarget::VertexSize(layout);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000938
939 if (sizeof(GrPoint) != vertexSize) {
940 if (!geo.set(target, layout, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000941 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000942 return;
943 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000944 int texOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000945 int colorOffset;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000946 GrDrawTarget::VertexSizeAndOffsetsByIdx(layout,
947 texOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000948 &colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000949 NULL,
950 NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000951 void* curVertex = geo.vertices();
952
953 for (int i = 0; i < vertexCount; ++i) {
954 *((GrPoint*)curVertex) = positions[i];
955
956 if (texOffsets[0] > 0) {
957 *(GrPoint*)((intptr_t)curVertex + texOffsets[0]) = texCoords[i];
958 }
959 if (colorOffset > 0) {
960 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
961 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000962 curVertex = (void*)((intptr_t)curVertex + vertexSize);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000963 }
964 } else {
965 target->setVertexSourceToArray(layout, positions, vertexCount);
966 }
967
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000968 // we don't currently apply offscreen AA to this path. Need improved
bsalomon@google.com91958362011-06-13 17:58:13 +0000969 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000970
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000971 if (NULL != indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000972 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000973 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000974 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000975 target->drawNonIndexed(primitiveType, 0, vertexCount);
976 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000977}
978
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000979///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com150d2842012-01-12 20:19:56 +0000980namespace {
981
bsalomon@google.com93c96602012-04-27 13:05:21 +0000982struct CircleVertex {
983 GrPoint fPos;
984 GrPoint fCenter;
985 GrScalar fOuterRadius;
986 GrScalar fInnerRadius;
987};
988
989/* Returns true if will map a circle to another circle. This can be true
990 * if the matrix only includes square-scale, rotation, translation.
991 */
992inline bool isSimilarityTransformation(const SkMatrix& matrix,
993 SkScalar tol = SK_ScalarNearlyZero) {
994 if (matrix.isIdentity() || matrix.getType() == SkMatrix::kTranslate_Mask) {
995 return true;
996 }
997 if (matrix.hasPerspective()) {
998 return false;
999 }
1000
1001 SkScalar mx = matrix.get(SkMatrix::kMScaleX);
1002 SkScalar sx = matrix.get(SkMatrix::kMSkewX);
1003 SkScalar my = matrix.get(SkMatrix::kMScaleY);
1004 SkScalar sy = matrix.get(SkMatrix::kMSkewY);
1005
1006 if (mx == 0 && sx == 0 && my == 0 && sy == 0) {
1007 return false;
1008 }
1009
1010 // it has scales or skews, but it could also be rotation, check it out.
1011 SkVector vec[2];
1012 vec[0].set(mx, sx);
1013 vec[1].set(sy, my);
1014
1015 return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) &&
1016 SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(),
1017 SkScalarSquare(tol));
1018}
1019
1020}
1021
1022// TODO: strokeWidth can't be larger than zero right now.
1023// It will be fixed when drawPath() can handle strokes.
1024void GrContext::drawOval(const GrPaint& paint,
1025 const GrRect& rect,
1026 SkScalar strokeWidth) {
bsalomon@google.com0982d352012-07-31 15:33:25 +00001027 GrAssert(strokeWidth <= 0);
1028 if (!isSimilarityTransformation(this->getMatrix()) ||
bsalomon@google.com93c96602012-04-27 13:05:21 +00001029 !paint.fAntiAlias ||
1030 rect.height() != rect.width()) {
1031 SkPath path;
1032 path.addOval(rect);
1033 GrPathFill fill = (strokeWidth == 0) ?
bsalomon@google.com0982d352012-07-31 15:33:25 +00001034 kHairLine_GrPathFill : kWinding_GrPathFill;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001035 this->internalDrawPath(paint, path, fill, NULL);
1036 return;
1037 }
1038
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001039 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
1040
bsalomon@google.com0982d352012-07-31 15:33:25 +00001041 GrDrawState* drawState = target->drawState();
1042 GrDrawState::AutoStageDisable atr(fDrawState);
1043 const GrMatrix vm = drawState->getViewMatrix();
1044
bsalomon@google.com93c96602012-04-27 13:05:21 +00001045 const GrRenderTarget* rt = drawState->getRenderTarget();
1046 if (NULL == rt) {
1047 return;
1048 }
1049
bsalomon@google.come3d32162012-07-20 13:37:06 +00001050 GrDrawTarget::AutoDeviceCoordDraw adcd(target);
1051 if (!adcd.succeeded()) {
1052 return;
1053 }
bsalomon@google.com93c96602012-04-27 13:05:21 +00001054
bsalomon@google.come3d32162012-07-20 13:37:06 +00001055 GrVertexLayout layout = GrDrawTarget::kEdge_VertexLayoutBit;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001056 GrAssert(sizeof(CircleVertex) == GrDrawTarget::VertexSize(layout));
1057
1058 GrPoint center = GrPoint::Make(rect.centerX(), rect.centerY());
1059 GrScalar radius = SkScalarHalf(rect.width());
1060
1061 vm.mapPoints(&center, 1);
1062 radius = vm.mapRadius(radius);
1063
1064 GrScalar outerRadius = radius;
1065 GrScalar innerRadius = 0;
1066 SkScalar halfWidth = 0;
1067 if (strokeWidth == 0) {
1068 halfWidth = SkScalarHalf(SK_Scalar1);
1069
1070 outerRadius += halfWidth;
1071 innerRadius = SkMaxScalar(0, radius - halfWidth);
1072 }
1073
1074 GrDrawTarget::AutoReleaseGeometry geo(target, layout, 4, 0);
1075 if (!geo.succeeded()) {
1076 GrPrintf("Failed to get space for vertices!\n");
1077 return;
1078 }
1079
1080 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
1081
robertphillips@google.coma0a66c12012-06-22 13:14:29 +00001082 // The fragment shader will extend the radius out half a pixel
1083 // to antialias. Expand the drawn rect here so all the pixels
1084 // will be captured.
1085 SkScalar L = center.fX - outerRadius - SkFloatToScalar(0.5f);
1086 SkScalar R = center.fX + outerRadius + SkFloatToScalar(0.5f);
1087 SkScalar T = center.fY - outerRadius - SkFloatToScalar(0.5f);
1088 SkScalar B = center.fY + outerRadius + SkFloatToScalar(0.5f);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001089
1090 verts[0].fPos = SkPoint::Make(L, T);
1091 verts[1].fPos = SkPoint::Make(R, T);
1092 verts[2].fPos = SkPoint::Make(L, B);
1093 verts[3].fPos = SkPoint::Make(R, B);
1094
1095 for (int i = 0; i < 4; ++i) {
1096 // this goes to fragment shader, it should be in y-points-up space.
1097 verts[i].fCenter = SkPoint::Make(center.fX, rt->height() - center.fY);
1098
1099 verts[i].fOuterRadius = outerRadius;
1100 verts[i].fInnerRadius = innerRadius;
1101 }
1102
1103 drawState->setVertexEdgeType(GrDrawState::kCircle_EdgeType);
bsalomon@google.com47059542012-06-06 20:51:20 +00001104 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001105}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001106
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001107void GrContext::drawPath(const GrPaint& paint, const SkPath& path,
reed@google.com07f3ee12011-05-16 17:21:57 +00001108 GrPathFill fill, const GrPoint* translate) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001109
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001110 if (path.isEmpty()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001111 if (GrIsFillInverted(fill)) {
1112 this->drawPaint(paint);
1113 }
1114 return;
1115 }
1116
bsalomon@google.com93c96602012-04-27 13:05:21 +00001117 SkRect ovalRect;
1118 if (!GrIsFillInverted(fill) && path.isOval(&ovalRect)) {
1119 if (translate) {
1120 ovalRect.offset(*translate);
1121 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001122 SkScalar width = (fill == kHairLine_GrPathFill) ? 0 : -SK_Scalar1;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001123 this->drawOval(paint, ovalRect, width);
1124 return;
1125 }
1126
1127 internalDrawPath(paint, path, fill, translate);
1128}
1129
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001130void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +00001131 GrPathFill fill, const GrPoint* translate) {
1132
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001133 // Note that below we may sw-rasterize the path into a scratch texture.
1134 // Scratch textures can be recycled after they are returned to the texture
1135 // cache. This presents a potential hazard for buffered drawing. However,
1136 // the writePixels that uploads to the scratch will perform a flush so we're
1137 // OK.
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001138 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +00001139 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001140
bsalomon@google.com289533a2011-10-27 12:34:25 +00001141 bool prAA = paint.fAntiAlias && !this->getRenderTarget()->isMultisampled();
1142
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001143 // An Assumption here is that path renderer would use some form of tweaking
1144 // the src color (either the input alpha or in the frag shader) to implement
1145 // aa. If we have some future driver-mojo path AA that can do the right
1146 // thing WRT to the blend then we'll need some query on the PR.
1147 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001148#if GR_DEBUG
bsalomon@google.com979432b2011-11-05 21:38:22 +00001149 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001150#endif
bsalomon@google.com289533a2011-10-27 12:34:25 +00001151 prAA = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001152 }
bsalomon@google.com289533a2011-10-27 12:34:25 +00001153
robertphillips@google.com72176b22012-05-23 13:19:12 +00001154 GrPathRenderer* pr = this->getPathRenderer(path, fill, target, prAA, true);
bsalomon@google.com30085192011-08-19 15:42:31 +00001155 if (NULL == pr) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001156#if GR_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001157 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001158#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001159 return;
1160 }
1161
bsalomon@google.come3d32162012-07-20 13:37:06 +00001162 pr->drawPath(path, fill, translate, target, prAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001163}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001164
bsalomon@google.com27847de2011-02-22 20:59:41 +00001165////////////////////////////////////////////////////////////////////////////////
1166
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001167void GrContext::flush(int flagsBitfield) {
1168 if (kDiscard_FlushBit & flagsBitfield) {
1169 fDrawBuffer->reset();
1170 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001171 this->flushDrawBuffer();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001172 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001173 if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001174 fGpu->forceRenderTargetFlush();
1175 }
1176}
1177
bsalomon@google.com27847de2011-02-22 20:59:41 +00001178void GrContext::flushDrawBuffer() {
junov@google.com53a55842011-06-08 22:55:10 +00001179 if (fDrawBuffer) {
robertphillips@google.com58b38182012-05-03 16:29:41 +00001180 // With addition of the AA clip path, flushing the draw buffer can
1181 // result in the generation of an AA clip mask. During this
1182 // process the SW path renderer may be invoked which recusively
1183 // calls this method (via internalWriteTexturePixels) creating
1184 // infinite recursion
1185 GrInOrderDrawBuffer* temp = fDrawBuffer;
1186 fDrawBuffer = NULL;
1187
1188 temp->flushTo(fGpu);
1189
1190 fDrawBuffer = temp;
junov@google.com53a55842011-06-08 22:55:10 +00001191 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001192}
1193
bsalomon@google.com0342a852012-08-20 19:22:38 +00001194void GrContext::writeTexturePixels(GrTexture* texture,
1195 int left, int top, int width, int height,
1196 GrPixelConfig config, const void* buffer, size_t rowBytes,
1197 uint32_t flags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001198 SK_TRACE_EVENT0("GrContext::writeTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001199 ASSERT_OWNED_RESOURCE(texture);
1200
bsalomon@google.com0342a852012-08-20 19:22:38 +00001201 // TODO: use scratch texture to perform conversion
1202 if (kUnpremul_PixelOpsFlag & flags) {
1203 return;
1204 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001205 if (!(kDontFlush_PixelOpsFlag & flags)) {
1206 this->flush();
1207 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001208
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001209 fGpu->writeTexturePixels(texture, left, top, width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +00001210 config, buffer, rowBytes);
1211}
1212
bsalomon@google.com0342a852012-08-20 19:22:38 +00001213bool GrContext::readTexturePixels(GrTexture* texture,
1214 int left, int top, int width, int height,
1215 GrPixelConfig config, void* buffer, size_t rowBytes,
1216 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001217 SK_TRACE_EVENT0("GrContext::readTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001218 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001219
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001220 // TODO: code read pixels for textures that aren't also rendertargets
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001221 GrRenderTarget* target = texture->asRenderTarget();
1222 if (NULL != target) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001223 return this->readRenderTargetPixels(target,
1224 left, top, width, height,
1225 config, buffer, rowBytes,
1226 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001227 } else {
1228 return false;
1229 }
1230}
1231
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001232#include "SkConfig8888.h"
1233
1234namespace {
1235/**
1236 * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel
1237 * formats are representable as Config8888 and so the function returns false
1238 * if the GrPixelConfig has no equivalent Config8888.
1239 */
1240bool grconfig_to_config8888(GrPixelConfig config,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001241 bool unpremul,
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001242 SkCanvas::Config8888* config8888) {
1243 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001244 case kRGBA_8888_GrPixelConfig:
1245 if (unpremul) {
1246 *config8888 = SkCanvas::kRGBA_Unpremul_Config8888;
1247 } else {
1248 *config8888 = SkCanvas::kRGBA_Premul_Config8888;
1249 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001250 return true;
bsalomon@google.com0342a852012-08-20 19:22:38 +00001251 case kBGRA_8888_GrPixelConfig:
1252 if (unpremul) {
1253 *config8888 = SkCanvas::kBGRA_Unpremul_Config8888;
1254 } else {
1255 *config8888 = SkCanvas::kBGRA_Premul_Config8888;
1256 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001257 return true;
1258 default:
1259 return false;
1260 }
1261}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001262
1263// It returns a configuration with where the byte position of the R & B components are swapped in
1264// relation to the input config. This should only be called with the result of
1265// grconfig_to_config8888 as it will fail for other configs.
1266SkCanvas::Config8888 swap_config8888_red_and_blue(SkCanvas::Config8888 config8888) {
1267 switch (config8888) {
1268 case SkCanvas::kBGRA_Premul_Config8888:
1269 return SkCanvas::kRGBA_Premul_Config8888;
1270 case SkCanvas::kBGRA_Unpremul_Config8888:
1271 return SkCanvas::kRGBA_Unpremul_Config8888;
1272 case SkCanvas::kRGBA_Premul_Config8888:
1273 return SkCanvas::kBGRA_Premul_Config8888;
1274 case SkCanvas::kRGBA_Unpremul_Config8888:
1275 return SkCanvas::kBGRA_Unpremul_Config8888;
1276 default:
1277 GrCrash("Unexpected input");
1278 return SkCanvas::kBGRA_Unpremul_Config8888;;
1279 }
1280}
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001281}
1282
bsalomon@google.com0342a852012-08-20 19:22:38 +00001283bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
1284 int left, int top, int width, int height,
1285 GrPixelConfig config, void* buffer, size_t rowBytes,
1286 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001287 SK_TRACE_EVENT0("GrContext::readRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001288 ASSERT_OWNED_RESOURCE(target);
1289
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001290 if (NULL == target) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001291 target = fDrawState->getRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001292 if (NULL == target) {
1293 return false;
1294 }
1295 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001296
bsalomon@google.com6f379512011-11-16 20:36:03 +00001297 if (!(kDontFlush_PixelOpsFlag & flags)) {
1298 this->flush();
1299 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001300
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001301 // Determine which conversions have to be applied: flipY, swapRAnd, and/or unpremul.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001302
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001303 // If fGpu->readPixels would incur a y-flip cost then we will read the pixels upside down. We'll
1304 // either do the flipY by drawing into a scratch with a matrix or on the cpu after the read.
1305 bool flipY = fGpu->readPixelsWillPayForYFlip(target, left, top,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001306 width, height, config,
1307 rowBytes);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001308 bool swapRAndB = fGpu->preferredReadPixelsConfig(config) == GrPixelConfigSwapRAndB(config);
1309
bsalomon@google.com0342a852012-08-20 19:22:38 +00001310 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001311
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001312 // flipY will get set to false when it is handled below using a scratch. However, in that case
1313 // we still want to do the read upside down.
1314 bool readUpsideDown = flipY;
1315
1316 if (unpremul && kRGBA_8888_GrPixelConfig != config && kBGRA_8888_GrPixelConfig != config) {
1317 // The unpremul flag is only allowed for these two configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001318 return false;
1319 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001320
1321 GrPixelConfig readConfig;
1322 if (swapRAndB) {
1323 readConfig = GrPixelConfigSwapRAndB(config);
1324 GrAssert(kUnknown_GrPixelConfig != config);
1325 } else {
1326 readConfig = config;
1327 }
1328
1329 // If the src is a texture and we would have to do conversions after read pixels, we instead
1330 // do the conversions by drawing the src to a scratch texture. If we handle any of the
1331 // conversions in the draw we set the corresponding bool to false so that we don't reapply it
1332 // on the read back pixels.
1333 GrTexture* src = target->asTexture();
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001334 GrAutoScratchTexture ast;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001335 if (NULL != src && (swapRAndB || unpremul || flipY)) {
1336 // Make the scratch a render target because we don't have a robust readTexturePixels as of
1337 // yet. It calls this function.
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001338 GrTextureDesc desc;
1339 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1340 desc.fWidth = width;
1341 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001342 desc.fConfig = readConfig;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001343
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001344 // When a full readback is faster than a partial we could always make the scratch exactly
1345 // match the passed rect. However, if we see many different size rectangles we will trash
1346 // our texture cache and pay the cost of creating and destroying many textures. So, we only
1347 // request an exact match when the caller is reading an entire RT.
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001348 ScratchTexMatch match = kApprox_ScratchTexMatch;
1349 if (0 == left &&
1350 0 == top &&
1351 target->width() == width &&
1352 target->height() == height &&
1353 fGpu->fullReadPixelsIsFasterThanPartial()) {
1354 match = kExact_ScratchTexMatch;
1355 }
1356 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001357 GrTexture* texture = ast.texture();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001358 if (texture) {
1359 SkAutoTUnref<GrCustomStage> stage;
1360 if (unpremul) {
1361 stage.reset(this->createPMToUPMEffect(src, swapRAndB));
1362 }
1363 // If we failed to create a PM->UPM effect and have no other conversions to perform then
1364 // there is no longer any point to using the scratch.
1365 if (NULL != stage || flipY || swapRAndB) {
1366 if (NULL == stage) {
1367 stage.reset(GrConfigConversionEffect::Create(src, swapRAndB));
1368 GrAssert(NULL != stage);
1369 } else {
1370 unpremul = false; // we will handle the UPM conversion in the draw
1371 }
1372 swapRAndB = false; // we will handle the swap in the draw.
bsalomon@google.comc4364992011-11-07 15:54:49 +00001373
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001374 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
1375 GrDrawState* drawState = fGpu->drawState();
1376 drawState->setRenderTarget(texture->asRenderTarget());
1377 GrMatrix matrix;
1378 if (flipY) {
1379 matrix.setTranslate(SK_Scalar1 * left,
1380 SK_Scalar1 * (top + height));
1381 matrix.set(GrMatrix::kMScaleY, -GR_Scalar1);
1382 flipY = false; // the y flip will be handled in the draw
1383 } else {
1384 matrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
1385 }
1386 matrix.postIDiv(src->width(), src->height());
1387 drawState->sampler(0)->reset(matrix);
1388 drawState->sampler(0)->setCustomStage(stage);
1389 GrRect rect = GrRect::MakeWH(GrIntToScalar(width), GrIntToScalar(height));
1390 fGpu->drawSimpleRect(rect, NULL);
1391 // we want to read back from the scratch's origin
1392 left = 0;
1393 top = 0;
1394 target = texture->asRenderTarget();
1395 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001396 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001397 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001398 if (!fGpu->readPixels(target,
1399 left, top, width, height,
1400 readConfig, buffer, rowBytes, readUpsideDown)) {
1401 return false;
1402 }
1403 // Perform any conversions we weren't able to perfom using a scratch texture.
1404 if (unpremul || swapRAndB || flipY) {
1405 SkCanvas::Config8888 srcC8888;
1406 SkCanvas::Config8888 dstC8888;
1407 bool c8888IsValid = grconfig_to_config8888(config, false, &srcC8888);
1408 grconfig_to_config8888(config, unpremul, &dstC8888);
1409 if (swapRAndB) {
1410 GrAssert(c8888IsValid); // we should only do r/b swap on 8888 configs
1411 srcC8888 = swap_config8888_red_and_blue(srcC8888);
1412 }
1413 if (flipY) {
1414 size_t tightRB = width * GrBytesPerPixel(config);
1415 if (0 == rowBytes) {
1416 rowBytes = tightRB;
1417 }
1418 SkAutoSTMalloc<256, uint8_t> tempRow(tightRB);
1419 intptr_t top = reinterpret_cast<intptr_t>(buffer);
1420 intptr_t bot = top + (height - 1) * rowBytes;
1421 while (top < bot) {
1422 uint32_t* t = reinterpret_cast<uint32_t*>(top);
1423 uint32_t* b = reinterpret_cast<uint32_t*>(bot);
1424 uint32_t* temp = reinterpret_cast<uint32_t*>(tempRow.get());
1425 memcpy(temp, t, tightRB);
1426 if (c8888IsValid) {
1427 SkConvertConfig8888Pixels(t, tightRB, dstC8888,
1428 b, tightRB, srcC8888,
1429 width, 1);
1430 SkConvertConfig8888Pixels(b, tightRB, dstC8888,
1431 temp, tightRB, srcC8888,
1432 width, 1);
1433 } else {
1434 memcpy(t, b, tightRB);
1435 memcpy(b, temp, tightRB);
1436 }
1437 top += rowBytes;
1438 bot -= rowBytes;
1439 }
1440 // The above loop does nothing on the middle row when height is odd.
1441 if (top == bot && c8888IsValid && dstC8888 != srcC8888) {
1442 uint32_t* mid = reinterpret_cast<uint32_t*>(top);
1443 SkConvertConfig8888Pixels(mid, tightRB, dstC8888, mid, tightRB, srcC8888, width, 1);
1444 }
1445 } else {
1446 // if we aren't flipping Y then we have no reason to be here other than doing
1447 // conversions for 8888 (r/b swap or upm).
1448 GrAssert(c8888IsValid);
1449 uint32_t* b32 = reinterpret_cast<uint32_t*>(buffer);
1450 SkConvertConfig8888Pixels(b32, rowBytes, dstC8888,
1451 b32, rowBytes, srcC8888,
1452 width, height);
1453 }
1454 }
1455 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001456}
1457
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001458void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1459 GrAssert(target);
1460 ASSERT_OWNED_RESOURCE(target);
1461 // In the future we may track whether there are any pending draws to this
1462 // target. We don't today so we always perform a flush. We don't promise
1463 // this to our clients, though.
1464 this->flush();
1465 fGpu->resolveRenderTarget(target);
1466}
1467
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001468void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst) {
1469 if (NULL == src || NULL == dst) {
1470 return;
1471 }
1472 ASSERT_OWNED_RESOURCE(src);
1473
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001474 // Writes pending to the source texture are not tracked, so a flush
1475 // is required to ensure that the copy captures the most recent contents
1476 // of the source texture. See similar behaviour in
1477 // GrContext::resolveRenderTarget.
1478 this->flush();
1479
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001480 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001481 GrDrawState* drawState = fGpu->drawState();
1482 drawState->setRenderTarget(dst);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001483 GrMatrix sampleM;
1484 sampleM.setIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001485 drawState->sampler(0)->reset(sampleM);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001486 drawState->createTextureEffect(0, src);
bsalomon@google.com5db3b6c2012-01-12 20:38:57 +00001487 SkRect rect = SkRect::MakeXYWH(0, 0,
1488 SK_Scalar1 * src->width(),
1489 SK_Scalar1 * src->height());
bsalomon@google.come3d32162012-07-20 13:37:06 +00001490 fGpu->drawSimpleRect(rect, NULL);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001491}
1492
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001493void GrContext::writeRenderTargetPixels(GrRenderTarget* target,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001494 int left, int top, int width, int height,
1495 GrPixelConfig config,
1496 const void* buffer,
1497 size_t rowBytes,
1498 uint32_t flags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001499 SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001500 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001501
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001502 if (NULL == target) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001503 target = fDrawState->getRenderTarget();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001504 if (NULL == target) {
1505 return;
1506 }
1507 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001508
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001509 // TODO: when underlying api has a direct way to do this we should use it (e.g. glDrawPixels on
1510 // desktop GL).
1511
1512 // We will always call some form of writeTexturePixels and we will pass our flags on to it.
1513 // Thus, we don't perform a flush here since that call will do it (if the kNoFlush flag isn't
1514 // set.)
bsalomon@google.com27847de2011-02-22 20:59:41 +00001515
bsalomon@google.com0342a852012-08-20 19:22:38 +00001516 // If the RT is also a texture and we don't have to premultiply then take the texture path.
1517 // We expect to be at least as fast or faster since it doesn't use an intermediate texture as
1518 // we do below.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001519
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001520#if !GR_MAC_BUILD
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001521 // At least some drivers on the Mac get confused when glTexImage2D is called on a texture
1522 // attached to an FBO. The FBO still sees the old image. TODO: determine what OS versions and/or
1523 // HW is affected.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001524 if (NULL != target->asTexture() && !(kUnpremul_PixelOpsFlag & flags)) {
1525 this->writeTexturePixels(target->asTexture(),
1526 left, top, width, height,
1527 config, buffer, rowBytes, flags);
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001528 return;
1529 }
1530#endif
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001531 SkAutoTUnref<GrCustomStage> stage;
1532 bool swapRAndB = (fGpu->preferredReadPixelsConfig(config) == GrPixelConfigSwapRAndB(config));
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001533
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001534 GrPixelConfig textureConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001535 if (swapRAndB) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001536 textureConfig = GrPixelConfigSwapRAndB(config);
1537 } else {
1538 textureConfig = config;
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001539 }
1540
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001541 GrTextureDesc desc;
1542 desc.fWidth = width;
1543 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001544 desc.fConfig = textureConfig;
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001545 GrAutoScratchTexture ast(this, desc);
1546 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001547 if (NULL == texture) {
1548 return;
1549 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001550 // allocate a tmp buffer and sw convert the pixels to premul
1551 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
1552
1553 if (kUnpremul_PixelOpsFlag & flags) {
1554 if (kRGBA_8888_GrPixelConfig != config && kBGRA_8888_GrPixelConfig != config) {
1555 return;
1556 }
1557 stage.reset(this->createUPMToPMEffect(texture, swapRAndB));
1558 if (NULL == stage) {
1559 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1560 GR_DEBUGCODE(bool success = )
1561 grconfig_to_config8888(config, true, &srcConfig8888);
1562 GrAssert(success);
1563 GR_DEBUGCODE(success = )
1564 grconfig_to_config8888(config, false, &dstConfig8888);
1565 GrAssert(success);
1566 const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer);
1567 tmpPixels.reset(width * height);
1568 SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888,
1569 src, rowBytes, srcConfig8888,
1570 width, height);
1571 buffer = tmpPixels.get();
1572 rowBytes = 4 * width;
1573 }
1574 }
1575 if (NULL == stage) {
1576 stage.reset(GrConfigConversionEffect::Create(texture, swapRAndB));
1577 GrAssert(NULL != stage);
1578 }
1579
1580 this->writeTexturePixels(texture,
1581 0, 0, width, height,
1582 textureConfig, buffer, rowBytes,
1583 flags & ~kUnpremul_PixelOpsFlag);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001584
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001585 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001586 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001587
1588 GrMatrix matrix;
1589 matrix.setTranslate(GrIntToScalar(left), GrIntToScalar(top));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001590 drawState->setViewMatrix(matrix);
1591 drawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001592
bsalomon@google.com5c638652011-07-18 19:31:59 +00001593 matrix.setIDiv(texture->width(), texture->height());
bsalomon@google.comb8670992012-07-25 21:27:09 +00001594 drawState->sampler(0)->reset(matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001595 drawState->sampler(0)->setCustomStage(stage);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001596
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001597 fGpu->drawSimpleRect(GrRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)), NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001598}
1599////////////////////////////////////////////////////////////////////////////////
1600
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001601void GrContext::setPaint(const GrPaint& paint) {
tomhudson@google.comcb325ce2012-07-11 14:41:19 +00001602 GrAssert(fDrawState->stagesDisabled());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001603
1604 for (int i = 0; i < GrPaint::kMaxTextures; ++i) {
1605 int s = i + GrPaint::kFirstTextureStage;
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001606 if (paint.isTextureStageEnabled(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001607 *fDrawState->sampler(s) = paint.getTextureSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001608 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001609 }
1610
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001611 fDrawState->setFirstCoverageStage(GrPaint::kFirstMaskStage);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001612
1613 for (int i = 0; i < GrPaint::kMaxMasks; ++i) {
1614 int s = i + GrPaint::kFirstMaskStage;
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001615 if (paint.isMaskStageEnabled(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001616 *fDrawState->sampler(s) = paint.getMaskSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001617 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001618 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001619
bsalomon@google.com26936d02012-03-19 13:06:19 +00001620 // disable all stages not accessible via the paint
1621 for (int s = GrPaint::kTotalStages; s < GrDrawState::kNumStages; ++s) {
tomhudson@google.com676e6602012-07-10 17:21:48 +00001622 fDrawState->disableStage(s);
bsalomon@google.com26936d02012-03-19 13:06:19 +00001623 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001624
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001625 fDrawState->setColor(paint.fColor);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001626
1627 if (paint.fDither) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001628 fDrawState->enableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001629 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001630 fDrawState->disableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001631 }
1632 if (paint.fAntiAlias) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001633 fDrawState->enableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001634 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001635 fDrawState->disableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001636 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001637 if (paint.fColorMatrixEnabled) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001638 fDrawState->enableState(GrDrawState::kColorMatrix_StateBit);
1639 fDrawState->setColorMatrix(paint.fColorMatrix);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001640 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001641 fDrawState->disableState(GrDrawState::kColorMatrix_StateBit);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001642 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001643 fDrawState->setBlendFunc(paint.fSrcBlendCoeff, paint.fDstBlendCoeff);
1644 fDrawState->setColorFilter(paint.fColorFilterColor, paint.fColorFilterXfermode);
1645 fDrawState->setCoverage(paint.fCoverage);
reed@google.com4b2d3f32012-05-15 18:05:50 +00001646#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
bsalomon@google.come3d32162012-07-20 13:37:06 +00001647 if ((paint.hasMask() || 0xff != paint.fCoverage) &&
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001648 !fGpu->canApplyCoverage()) {
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001649 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1650 }
bsalomon@google.com95cd7bd2012-03-28 15:35:05 +00001651#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001652}
1653
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001654GrDrawTarget* GrContext::prepareToDraw(const GrPaint* paint, BufferedDraw buffered) {
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001655 if (kNo_BufferedDraw == buffered && kYes_BufferedDraw == fLastDrawWasBuffered) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001656 this->flushDrawBuffer();
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001657 fLastDrawWasBuffered = kNo_BufferedDraw;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001658 }
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001659 if (NULL != paint) {
1660 this->setPaint(*paint);
1661 }
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001662 if (kYes_BufferedDraw == buffered) {
1663 fDrawBuffer->setClip(fGpu->getClip());
1664 fLastDrawWasBuffered = kYes_BufferedDraw;
1665 return fDrawBuffer;
1666 } else {
1667 GrAssert(kNo_BufferedDraw == buffered);
1668 return fGpu;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001669 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001670}
1671
robertphillips@google.com72176b22012-05-23 13:19:12 +00001672/*
1673 * This method finds a path renderer that can draw the specified path on
1674 * the provided target.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001675 * Due to its expense, the software path renderer has split out so it can
robertphillips@google.com72176b22012-05-23 13:19:12 +00001676 * can be individually allowed/disallowed via the "allowSW" boolean.
1677 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001678GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
bsalomon@google.com289533a2011-10-27 12:34:25 +00001679 GrPathFill fill,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001680 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001681 bool antiAlias,
1682 bool allowSW) {
bsalomon@google.com30085192011-08-19 15:42:31 +00001683 if (NULL == fPathRendererChain) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001684 fPathRendererChain =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001685 SkNEW_ARGS(GrPathRendererChain,
1686 (this, GrPathRendererChain::kNone_UsageFlag));
bsalomon@google.com30085192011-08-19 15:42:31 +00001687 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001688
1689 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path, fill,
1690 target,
1691 antiAlias);
1692
1693 if (NULL == pr && allowSW) {
1694 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001695 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001696 }
1697
1698 pr = fSoftwarePathRenderer;
1699 }
1700
1701 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001702}
1703
bsalomon@google.com27847de2011-02-22 20:59:41 +00001704////////////////////////////////////////////////////////////////////////////////
1705
bsalomon@google.com27847de2011-02-22 20:59:41 +00001706void GrContext::setRenderTarget(GrRenderTarget* target) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001707 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001708 fDrawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001709}
1710
1711GrRenderTarget* GrContext::getRenderTarget() {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001712 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001713}
1714
1715const GrRenderTarget* GrContext::getRenderTarget() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001716 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001717}
1718
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001719bool GrContext::isConfigRenderable(GrPixelConfig config) const {
1720 return fGpu->isConfigRenderable(config);
1721}
1722
bsalomon@google.com27847de2011-02-22 20:59:41 +00001723const GrMatrix& GrContext::getMatrix() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001724 return fDrawState->getViewMatrix();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001725}
1726
1727void GrContext::setMatrix(const GrMatrix& m) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001728 fDrawState->setViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001729}
1730
1731void GrContext::concatMatrix(const GrMatrix& m) const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001732 fDrawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001733}
1734
1735static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
1736 intptr_t mask = 1 << shift;
1737 if (pred) {
1738 bits |= mask;
1739 } else {
1740 bits &= ~mask;
1741 }
1742 return bits;
1743}
1744
bsalomon@google.com583a1e32011-08-17 13:42:46 +00001745GrContext::GrContext(GrGpu* gpu) {
bsalomon@google.comc0af3172012-06-15 14:10:09 +00001746 ++THREAD_INSTANCE_COUNT;
1747
bsalomon@google.com27847de2011-02-22 20:59:41 +00001748 fGpu = gpu;
1749 fGpu->ref();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001750 fGpu->setContext(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001751
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001752 fDrawState = SkNEW(GrDrawState);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001753 fGpu->setDrawState(fDrawState);
1754
bsalomon@google.com30085192011-08-19 15:42:31 +00001755 fPathRendererChain = NULL;
robertphillips@google.com72176b22012-05-23 13:19:12 +00001756 fSoftwarePathRenderer = NULL;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001757
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001758 fTextureCache = SkNEW_ARGS(GrResourceCache,
1759 (MAX_TEXTURE_CACHE_COUNT,
1760 MAX_TEXTURE_CACHE_BYTES));
1761 fFontCache = SkNEW_ARGS(GrFontCache, (fGpu));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001762
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001763 fLastDrawWasBuffered = kNo_BufferedDraw;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001764
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001765 fDrawBuffer = NULL;
1766 fDrawBufferVBAllocPool = NULL;
1767 fDrawBufferIBAllocPool = NULL;
1768
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001769 fAARectRenderer = SkNEW(GrAARectRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001770
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001771 fDidTestPMConversions = false;
1772
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001773 this->setupDrawBuffer();
1774}
1775
1776void GrContext::setupDrawBuffer() {
1777
1778 GrAssert(NULL == fDrawBuffer);
1779 GrAssert(NULL == fDrawBufferVBAllocPool);
1780 GrAssert(NULL == fDrawBufferIBAllocPool);
1781
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001782 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001783 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001784 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001785 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001786 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001787 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001788 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001789 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001790
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001791 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com471d4712011-08-23 15:45:25 +00001792 fDrawBufferVBAllocPool,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001793 fDrawBufferIBAllocPool));
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001794
bsalomon@google.com27847de2011-02-22 20:59:41 +00001795 fDrawBuffer->setQuadIndexBuffer(this->getQuadIndexBuffer());
bsalomon@google.com1015e032012-06-25 18:41:04 +00001796 if (fDrawBuffer) {
1797 fDrawBuffer->setAutoFlushTarget(fGpu);
1798 fDrawBuffer->setDrawState(fDrawState);
1799 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001800}
1801
bsalomon@google.com27847de2011-02-22 20:59:41 +00001802GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) {
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001803 return prepareToDraw(&paint, DEFAULT_BUFFERING);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001804}
1805
1806const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1807 return fGpu->getQuadIndexBuffer();
1808}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001809
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001810namespace {
1811void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
1812 GrConfigConversionEffect::PMConversion pmToUPM;
1813 GrConfigConversionEffect::PMConversion upmToPM;
1814 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
1815 *pmToUPMValue = pmToUPM;
1816 *upmToPMValue = upmToPM;
1817}
1818}
1819
1820GrCustomStage* GrContext::createPMToUPMEffect(GrTexture* texture, bool swapRAndB) {
1821 if (!fDidTestPMConversions) {
1822 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001823 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001824 }
1825 GrConfigConversionEffect::PMConversion pmToUPM =
1826 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
1827 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
1828 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM);
1829 } else {
1830 return NULL;
1831 }
1832}
1833
1834GrCustomStage* GrContext::createUPMToPMEffect(GrTexture* texture, bool swapRAndB) {
1835 if (!fDidTestPMConversions) {
1836 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001837 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001838 }
1839 GrConfigConversionEffect::PMConversion upmToPM =
1840 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
1841 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
1842 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM);
1843 } else {
1844 return NULL;
1845 }
1846}
1847
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001848GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture,
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001849 bool canClobberSrc,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001850 const SkRect& rect,
1851 float sigmaX, float sigmaY) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001852 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001853 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001854 AutoMatrix avm(this, GrMatrix::I());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001855 SkIRect clearRect;
bsalomon@google.comb505a122012-05-31 18:40:36 +00001856 int scaleFactorX, radiusX;
1857 int scaleFactorY, radiusY;
1858 sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX);
1859 sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY);
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001860
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001861 SkRect srcRect(rect);
1862 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
1863 srcRect.roundOut();
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001864 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001865 static_cast<float>(scaleFactorY));
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001866
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001867 AutoClip acs(this, srcRect);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001868
bsalomon@google.com0342a852012-08-20 19:22:38 +00001869 GrAssert(kBGRA_8888_GrPixelConfig == srcTexture->config() ||
1870 kRGBA_8888_GrPixelConfig == srcTexture->config() ||
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001871 kAlpha_8_GrPixelConfig == srcTexture->config());
1872
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001873 GrTextureDesc desc;
1874 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1875 desc.fWidth = SkScalarFloorToInt(srcRect.width());
1876 desc.fHeight = SkScalarFloorToInt(srcRect.height());
1877 desc.fConfig = srcTexture->config();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001878
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001879 GrAutoScratchTexture temp1, temp2;
1880 GrTexture* dstTexture = temp1.set(this, desc);
1881 GrTexture* tempTexture = canClobberSrc ? srcTexture : temp2.set(this, desc);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001882
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001883 GrPaint paint;
1884 paint.reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +00001885 paint.textureSampler(0)->textureParams()->setBilerp(true);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001886
1887 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
1888 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1889 srcTexture->height());
1890 this->setRenderTarget(dstTexture->asRenderTarget());
1891 SkRect dstRect(srcRect);
1892 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
1893 i < scaleFactorY ? 0.5f : 1.0f);
tomhudson@google.comaa72eab2012-07-19 18:01:07 +00001894 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
1895 (srcTexture)))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001896 this->drawRectToRect(paint, dstRect, srcRect);
1897 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001898 srcTexture = dstTexture;
1899 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001900 }
1901
robertphillips@google.com7a396332012-05-10 15:11:27 +00001902 SkIRect srcIRect;
1903 srcRect.roundOut(&srcIRect);
1904
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001905 if (sigmaX > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001906 if (scaleFactorX > 1) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001907 // Clear out a radius to the right of the srcRect to prevent the
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001908 // X convolution from reading garbage.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001909 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001910 radiusX, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001911 this->clear(&clearRect, 0x0);
1912 }
1913
1914 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001915 GrDrawTarget* target = this->prepareToDraw(NULL, DEFAULT_BUFFERING);
1916 convolve_gaussian(target, srcTexture, srcRect, sigmaX, radiusX,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001917 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001918 srcTexture = dstTexture;
1919 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001920 }
1921
1922 if (sigmaY > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001923 if (scaleFactorY > 1 || sigmaX > 0.0f) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001924 // Clear out a radius below the srcRect to prevent the Y
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001925 // convolution from reading garbage.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001926 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001927 srcIRect.width(), radiusY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001928 this->clear(&clearRect, 0x0);
1929 }
1930
1931 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001932 GrDrawTarget* target = this->prepareToDraw(NULL, DEFAULT_BUFFERING);
1933 convolve_gaussian(target, srcTexture, srcRect, sigmaY, radiusY,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001934 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001935 srcTexture = dstTexture;
1936 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001937 }
1938
1939 if (scaleFactorX > 1 || scaleFactorY > 1) {
1940 // Clear one pixel to the right and below, to accommodate bilinear
1941 // upsampling.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001942 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
robertphillips@google.com7a396332012-05-10 15:11:27 +00001943 srcIRect.width() + 1, 1);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001944 this->clear(&clearRect, 0x0);
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001945 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
robertphillips@google.com7a396332012-05-10 15:11:27 +00001946 1, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001947 this->clear(&clearRect, 0x0);
1948 // FIXME: This should be mitchell, not bilinear.
bsalomon@google.comb8670992012-07-25 21:27:09 +00001949 paint.textureSampler(0)->textureParams()->setBilerp(true);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001950 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1951 srcTexture->height());
1952 this->setRenderTarget(dstTexture->asRenderTarget());
tomhudson@google.comaa72eab2012-07-19 18:01:07 +00001953 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
1954 (srcTexture)))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001955 SkRect dstRect(srcRect);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001956 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001957 this->drawRectToRect(paint, dstRect, srcRect);
1958 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001959 srcTexture = dstTexture;
1960 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001961 }
1962 this->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001963 if (srcTexture == temp1.texture()) {
1964 return temp1.detach();
1965 } else if (srcTexture == temp2.texture()) {
1966 return temp2.detach();
1967 } else {
1968 srcTexture->ref();
1969 return srcTexture;
1970 }
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001971}
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001972
bsalomon@google.comc4364992011-11-07 15:54:49 +00001973///////////////////////////////////////////////////////////////////////////////
robertphillips@google.com59552022012-08-31 13:07:37 +00001974#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001975void GrContext::printCacheStats() const {
1976 fTextureCache->printStats();
1977}
1978#endif