blob: 9b01eb27e28cce161751880b7c5c8724f81d1d74 [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.com50a035d2012-09-07 19:44:33 +0000497 this->purgeCache();
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000498}
499
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000500void GrContext::lockTexture(GrTexture* texture) {
501
502 if (NULL == texture->getCacheEntry()) {
503 // not in the cache
504 GrAssert(0);
505 return;
506 }
507
508 fTextureCache->lock(texture->getCacheEntry());
509}
510
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000511void GrContext::unlockTexture(GrTexture* texture) {
512 ASSERT_OWNED_RESOURCE(texture);
513 GrAssert(NULL != texture->getCacheEntry());
514
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000515 // If this is a scratch texture we detached it from the cache
516 // while it was locked (to avoid two callers simultaneously getting
517 // the same texture).
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000518 if (GrTexture::IsScratchTexture(texture->getCacheEntry()->key())) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000519 fTextureCache->makeNonExclusive(texture->getCacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000520 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000521
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000522 fTextureCache->unlock(texture->getCacheEntry());
robertphillips@google.com50a035d2012-09-07 19:44:33 +0000523 this->purgeCache();
524}
525
526void GrContext::purgeCache() {
527 fTextureCache->purgeAsNeeded();
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000528}
529
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000530GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000531 void* srcData,
532 size_t rowBytes) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000533 GrTextureDesc descCopy = descIn;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000534 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000535}
536
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000537void GrContext::getTextureCacheLimits(int* maxTextures,
538 size_t* maxTextureBytes) const {
539 fTextureCache->getLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000540}
541
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000542void GrContext::setTextureCacheLimits(int maxTextures, size_t maxTextureBytes) {
543 fTextureCache->setLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000544}
545
bsalomon@google.com91958362011-06-13 17:58:13 +0000546int GrContext::getMaxTextureSize() const {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000547 return fGpu->getCaps().maxTextureSize();
bsalomon@google.com91958362011-06-13 17:58:13 +0000548}
549
550int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000551 return fGpu->getCaps().maxRenderTargetSize();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000552}
553
554///////////////////////////////////////////////////////////////////////////////
555
bsalomon@google.come269f212011-11-07 13:29:52 +0000556GrTexture* GrContext::createPlatformTexture(const GrPlatformTextureDesc& desc) {
557 return fGpu->createPlatformTexture(desc);
558}
559
560GrRenderTarget* GrContext::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
561 return fGpu->createPlatformRenderTarget(desc);
562}
563
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000564///////////////////////////////////////////////////////////////////////////////
565
bsalomon@google.comb8670992012-07-25 21:27:09 +0000566bool GrContext::supportsIndex8PixelConfig(const GrTextureParams* params,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000567 int width, int height) const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000568 const GrDrawTarget::Caps& caps = fGpu->getCaps();
bsalomon@google.comf6601872012-08-28 21:11:35 +0000569 if (!caps.eightBitPaletteSupport()) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000570 return false;
571 }
572
bsalomon@google.com27847de2011-02-22 20:59:41 +0000573 bool isPow2 = GrIsPow2(width) && GrIsPow2(height);
574
575 if (!isPow2) {
bsalomon@google.comb8670992012-07-25 21:27:09 +0000576 bool tiled = NULL != params && params->isTiled();
bsalomon@google.comf6601872012-08-28 21:11:35 +0000577 if (tiled && !caps.npotTextureTileSupport()) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000578 return false;
579 }
580 }
581 return true;
582}
583
584////////////////////////////////////////////////////////////////////////////////
585
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000586const GrClipData* GrContext::getClip() const {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000587 return fGpu->getClip();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000588}
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000589
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000590void GrContext::setClip(const GrClipData* clipData) {
591 fGpu->setClip(clipData);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000592 fDrawState->enableState(GrDrawState::kClip_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000593}
594
bsalomon@google.com27847de2011-02-22 20:59:41 +0000595////////////////////////////////////////////////////////////////////////////////
596
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000597void GrContext::clear(const GrIRect* rect,
598 const GrColor color,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000599 GrRenderTarget* target) {
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000600 this->prepareToDraw(NULL, DEFAULT_BUFFERING)->clear(rect, color, target);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000601}
602
603void GrContext::drawPaint(const GrPaint& paint) {
604 // set rect to be big enough to fill the space, but not super-huge, so we
605 // don't overflow fixed-point implementations
bsalomon@google.comd302f142011-03-03 13:54:13 +0000606 GrRect r;
607 r.setLTRB(0, 0,
608 GrIntToScalar(getRenderTarget()->width()),
609 GrIntToScalar(getRenderTarget()->height()));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000610 GrMatrix inverse;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000611 SkTLazy<GrPaint> tmpPaint;
612 const GrPaint* p = &paint;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000613 AutoMatrix am;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000614
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000615 // We attempt to map r by the inverse matrix and draw that. mapRect will
616 // map the four corners and bound them with a new rect. This will not
617 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000618 if (!this->getMatrix().hasPerspective()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000619 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000620 GrPrintf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000621 return;
622 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000623 inverse.mapRect(&r);
624 } else {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000625 if (paint.hasTextureOrMask()) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000626 tmpPaint.set(paint);
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000627 p = tmpPaint.get();
bsalomon@google.come3d32162012-07-20 13:37:06 +0000628 if (!tmpPaint.get()->preConcatSamplerMatricesWithInverse(fDrawState->getViewMatrix())) {
629 GrPrintf("Could not invert matrix\n");
630 }
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000631 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000632 am.set(this, GrMatrix::I());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000633 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000634 // by definition this fills the entire clip, no need for AA
635 if (paint.fAntiAlias) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000636 if (!tmpPaint.isValid()) {
637 tmpPaint.set(paint);
638 p = tmpPaint.get();
639 }
640 GrAssert(p == tmpPaint.get());
641 tmpPaint.get()->fAntiAlias = false;
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000642 }
643 this->drawRect(*p, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000644}
645
bsalomon@google.com205d4602011-04-25 12:43:45 +0000646////////////////////////////////////////////////////////////////////////////////
647
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000648namespace {
649inline bool disable_coverage_aa_for_blend(GrDrawTarget* target) {
650 return DISABLE_COVERAGE_AA_FOR_BLEND && !target->canApplyCoverage();
651}
652}
653
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000654////////////////////////////////////////////////////////////////////////////////
655
bsalomon@google.com27847de2011-02-22 20:59:41 +0000656/* create a triangle strip that strokes the specified triangle. There are 8
657 unique vertices, but we repreat the last 2 to close up. Alternatively we
658 could use an indices array, and then only send 8 verts, but not sure that
659 would be faster.
660 */
bsalomon@google.com205d4602011-04-25 12:43:45 +0000661static void setStrokeRectStrip(GrPoint verts[10], GrRect rect,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000662 GrScalar width) {
663 const GrScalar rad = GrScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000664 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000665
666 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
667 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
668 verts[2].set(rect.fRight - rad, rect.fTop + rad);
669 verts[3].set(rect.fRight + rad, rect.fTop - rad);
670 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
671 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
672 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
673 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
674 verts[8] = verts[0];
675 verts[9] = verts[1];
676}
677
reed@google.com20efde72011-05-09 17:00:02 +0000678/**
679 * Returns true if the rects edges are integer-aligned.
680 */
681static bool isIRect(const GrRect& r) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000682 return GrScalarIsInt(r.fLeft) && GrScalarIsInt(r.fTop) &&
reed@google.com20efde72011-05-09 17:00:02 +0000683 GrScalarIsInt(r.fRight) && GrScalarIsInt(r.fBottom);
684}
685
bsalomon@google.com205d4602011-04-25 12:43:45 +0000686static bool apply_aa_to_rect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000687 const GrRect& rect,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000688 GrScalar width,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000689 const GrMatrix* matrix,
690 GrMatrix* combinedMatrix,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000691 GrRect* devRect,
692 bool* useVertexCoverage) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000693 // we use a simple coverage ramp to do aa on axis-aligned rects
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000694 // we check if the rect will be axis-aligned, and the rect won't land on
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000695 // integer coords.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000696
bsalomon@google.coma3108262011-10-10 14:08:47 +0000697 // we are keeping around the "tweak the alpha" trick because
698 // it is our only hope for the fixed-pipe implementation.
699 // In a shader implementation we can give a separate coverage input
bsalomon@google.com289533a2011-10-27 12:34:25 +0000700 // TODO: remove this ugliness when we drop the fixed-pipe impl
bsalomon@google.coma3108262011-10-10 14:08:47 +0000701 *useVertexCoverage = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000702 if (!target->canTweakAlphaForCoverage()) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000703 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +0000704#if GR_DEBUG
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000705 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000706#endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000707 return false;
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000708 } else {
709 *useVertexCoverage = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000710 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000711 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000712 const GrDrawState& drawState = target->getDrawState();
713 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000714 return false;
715 }
716
bsalomon@google.com471d4712011-08-23 15:45:25 +0000717 if (0 == width && target->willUseHWAALines()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000718 return false;
719 }
720
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000721 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000722 return false;
723 }
724
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000725 if (NULL != matrix &&
bsalomon@google.com205d4602011-04-25 12:43:45 +0000726 !matrix->preservesAxisAlignment()) {
727 return false;
728 }
729
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000730 *combinedMatrix = drawState.getViewMatrix();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000731 if (NULL != matrix) {
732 combinedMatrix->preConcat(*matrix);
733 GrAssert(combinedMatrix->preservesAxisAlignment());
734 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000735
bsalomon@google.com205d4602011-04-25 12:43:45 +0000736 combinedMatrix->mapRect(devRect, rect);
737 devRect->sort();
738
739 if (width < 0) {
reed@google.com20efde72011-05-09 17:00:02 +0000740 return !isIRect(*devRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000741 } else {
742 return true;
743 }
744}
745
bsalomon@google.com27847de2011-02-22 20:59:41 +0000746void GrContext::drawRect(const GrPaint& paint,
747 const GrRect& rect,
748 GrScalar width,
749 const GrMatrix* matrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000750 SK_TRACE_EVENT0("GrContext::drawRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000751
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000752 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000753 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000754
bsalomon@google.com205d4602011-04-25 12:43:45 +0000755 GrRect devRect = rect;
756 GrMatrix combinedMatrix;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000757 bool useVertexCoverage;
bsalomon@google.com289533a2011-10-27 12:34:25 +0000758 bool needAA = paint.fAntiAlias &&
759 !this->getRenderTarget()->isMultisampled();
760 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix,
761 &combinedMatrix, &devRect,
762 &useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000763
764 if (doAA) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000765 GrDrawTarget::AutoDeviceCoordDraw adcd(target);
766 if (!adcd.succeeded()) {
767 return;
768 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000769 if (width >= 0) {
770 GrVec strokeSize;;
771 if (width > 0) {
772 strokeSize.set(width, width);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000773 combinedMatrix.mapVectors(&strokeSize, 1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000774 strokeSize.setAbs(strokeSize);
775 } else {
776 strokeSize.set(GR_Scalar1, GR_Scalar1);
777 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000778 fAARectRenderer->strokeAARect(this->getGpu(), target, devRect,
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000779 strokeSize, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000780 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000781 fAARectRenderer->fillAARect(this->getGpu(), target,
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000782 devRect, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000783 }
784 return;
785 }
786
bsalomon@google.com27847de2011-02-22 20:59:41 +0000787 if (width >= 0) {
788 // TODO: consider making static vertex buffers for these cases.
789 // Hairline could be done by just adding closing vertex to
790 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000791
bsalomon@google.com27847de2011-02-22 20:59:41 +0000792 static const int worstCaseVertCount = 10;
bsalomon@google.come3d32162012-07-20 13:37:06 +0000793 GrDrawTarget::AutoReleaseGeometry geo(target, 0, worstCaseVertCount, 0);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000794
795 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000796 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000797 return;
798 }
799
800 GrPrimitiveType primType;
801 int vertCount;
802 GrPoint* vertex = geo.positions();
803
804 if (width > 0) {
805 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000806 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000807 setStrokeRectStrip(vertex, rect, width);
808 } else {
809 // hairline
810 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000811 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000812 vertex[0].set(rect.fLeft, rect.fTop);
813 vertex[1].set(rect.fRight, rect.fTop);
814 vertex[2].set(rect.fRight, rect.fBottom);
815 vertex[3].set(rect.fLeft, rect.fBottom);
816 vertex[4].set(rect.fLeft, rect.fTop);
817 }
818
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000819 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000820 if (NULL != matrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000821 GrDrawState* drawState = target->drawState();
822 avmr.set(drawState);
823 drawState->preConcatViewMatrix(*matrix);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000824 drawState->preConcatSamplerMatrices(*matrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000825 }
826
827 target->drawNonIndexed(primType, 0, vertCount);
828 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000829#if GR_STATIC_RECT_VB
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000830 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
831 if (NULL == sqVB) {
832 GrPrintf("Failed to create static rect vb.\n");
833 return;
834 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000835 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000836 GrDrawState* drawState = target->drawState();
837 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000838 GrMatrix m;
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000839 m.setAll(rect.width(), 0, rect.fLeft,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000840 0, rect.height(), rect.fTop,
841 0, 0, GrMatrix::I()[8]);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000842
843 if (NULL != matrix) {
844 m.postConcat(*matrix);
845 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000846 drawState->preConcatViewMatrix(m);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000847 drawState->preConcatSamplerMatrices(m);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000848
bsalomon@google.com47059542012-06-06 20:51:20 +0000849 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000850#else
bsalomon@google.come3d32162012-07-20 13:37:06 +0000851 target->drawSimpleRect(rect, matrix);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000852#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000853 }
854}
855
856void GrContext::drawRectToRect(const GrPaint& paint,
857 const GrRect& dstRect,
858 const GrRect& srcRect,
859 const GrMatrix* dstMatrix,
860 const GrMatrix* srcMatrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000861 SK_TRACE_EVENT0("GrContext::drawRectToRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000862
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000863 // srcRect refers to paint's first texture
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000864 if (!paint.isTextureStageEnabled(0)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000865 drawRect(paint, dstRect, -1, dstMatrix);
866 return;
867 }
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000868
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000869 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000870
871#if GR_STATIC_RECT_VB
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000872 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000873 GrDrawState* drawState = target->drawState();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000874 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000875
876 GrMatrix m;
877
878 m.setAll(dstRect.width(), 0, dstRect.fLeft,
879 0, dstRect.height(), dstRect.fTop,
880 0, 0, GrMatrix::I()[8]);
881 if (NULL != dstMatrix) {
882 m.postConcat(*dstMatrix);
883 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000884 drawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000885
bsalomon@google.come3d32162012-07-20 13:37:06 +0000886 // we explicitly setup the correct coords for the first stage. The others
887 // must know about the view matrix change.
888 for (int s = 1; s < GrPaint::kTotalStages; ++s) {
889 if (drawState->isStageEnabled(s)) {
890 drawState->sampler(s)->preConcatMatrix(m);
891 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000892 }
893
bsalomon@google.com27847de2011-02-22 20:59:41 +0000894 m.setAll(srcRect.width(), 0, srcRect.fLeft,
895 0, srcRect.height(), srcRect.fTop,
896 0, 0, GrMatrix::I()[8]);
897 if (NULL != srcMatrix) {
898 m.postConcat(*srcMatrix);
899 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000900 drawState->sampler(GrPaint::kFirstTextureStage)->preConcatMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000901
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000902 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
903 if (NULL == sqVB) {
904 GrPrintf("Failed to create static rect vb.\n");
905 return;
906 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000907 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com47059542012-06-06 20:51:20 +0000908 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000909#else
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000910 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000911
tomhudson@google.com93813632011-10-27 20:21:16 +0000912 const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
913 const GrMatrix* srcMatrices[GrDrawState::kNumStages] = {NULL};
bsalomon@google.com27847de2011-02-22 20:59:41 +0000914 srcRects[0] = &srcRect;
915 srcMatrices[0] = srcMatrix;
916
bsalomon@google.come3d32162012-07-20 13:37:06 +0000917 target->drawRect(dstRect, dstMatrix, srcRects, srcMatrices);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000918#endif
919}
920
921void GrContext::drawVertices(const GrPaint& paint,
922 GrPrimitiveType primitiveType,
923 int vertexCount,
924 const GrPoint positions[],
925 const GrPoint texCoords[],
926 const GrColor colors[],
927 const uint16_t indices[],
928 int indexCount) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000929 SK_TRACE_EVENT0("GrContext::drawVertices");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000930
931 GrDrawTarget::AutoReleaseGeometry geo;
932
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000933 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000934 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000935
bsalomon@google.come3d32162012-07-20 13:37:06 +0000936 GrVertexLayout layout = 0;
937 if (NULL != texCoords) {
938 layout |= GrDrawTarget::StageTexCoordVertexLayoutBit(0, 0);
939 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000940 if (NULL != colors) {
941 layout |= GrDrawTarget::kColor_VertexLayoutBit;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000942 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000943 int vertexSize = GrDrawTarget::VertexSize(layout);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000944
945 if (sizeof(GrPoint) != vertexSize) {
946 if (!geo.set(target, layout, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000947 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000948 return;
949 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000950 int texOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000951 int colorOffset;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000952 GrDrawTarget::VertexSizeAndOffsetsByIdx(layout,
953 texOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000954 &colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000955 NULL,
956 NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000957 void* curVertex = geo.vertices();
958
959 for (int i = 0; i < vertexCount; ++i) {
960 *((GrPoint*)curVertex) = positions[i];
961
962 if (texOffsets[0] > 0) {
963 *(GrPoint*)((intptr_t)curVertex + texOffsets[0]) = texCoords[i];
964 }
965 if (colorOffset > 0) {
966 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
967 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000968 curVertex = (void*)((intptr_t)curVertex + vertexSize);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000969 }
970 } else {
971 target->setVertexSourceToArray(layout, positions, vertexCount);
972 }
973
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000974 // we don't currently apply offscreen AA to this path. Need improved
bsalomon@google.com91958362011-06-13 17:58:13 +0000975 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000976
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000977 if (NULL != indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000978 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000979 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000980 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000981 target->drawNonIndexed(primitiveType, 0, vertexCount);
982 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000983}
984
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000985///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com150d2842012-01-12 20:19:56 +0000986namespace {
987
bsalomon@google.com93c96602012-04-27 13:05:21 +0000988struct CircleVertex {
989 GrPoint fPos;
990 GrPoint fCenter;
991 GrScalar fOuterRadius;
992 GrScalar fInnerRadius;
993};
994
995/* Returns true if will map a circle to another circle. This can be true
996 * if the matrix only includes square-scale, rotation, translation.
997 */
998inline bool isSimilarityTransformation(const SkMatrix& matrix,
999 SkScalar tol = SK_ScalarNearlyZero) {
1000 if (matrix.isIdentity() || matrix.getType() == SkMatrix::kTranslate_Mask) {
1001 return true;
1002 }
1003 if (matrix.hasPerspective()) {
1004 return false;
1005 }
1006
1007 SkScalar mx = matrix.get(SkMatrix::kMScaleX);
1008 SkScalar sx = matrix.get(SkMatrix::kMSkewX);
1009 SkScalar my = matrix.get(SkMatrix::kMScaleY);
1010 SkScalar sy = matrix.get(SkMatrix::kMSkewY);
1011
1012 if (mx == 0 && sx == 0 && my == 0 && sy == 0) {
1013 return false;
1014 }
1015
1016 // it has scales or skews, but it could also be rotation, check it out.
1017 SkVector vec[2];
1018 vec[0].set(mx, sx);
1019 vec[1].set(sy, my);
1020
1021 return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) &&
1022 SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(),
1023 SkScalarSquare(tol));
1024}
1025
1026}
1027
1028// TODO: strokeWidth can't be larger than zero right now.
1029// It will be fixed when drawPath() can handle strokes.
1030void GrContext::drawOval(const GrPaint& paint,
1031 const GrRect& rect,
1032 SkScalar strokeWidth) {
bsalomon@google.com0982d352012-07-31 15:33:25 +00001033 GrAssert(strokeWidth <= 0);
1034 if (!isSimilarityTransformation(this->getMatrix()) ||
bsalomon@google.com93c96602012-04-27 13:05:21 +00001035 !paint.fAntiAlias ||
1036 rect.height() != rect.width()) {
1037 SkPath path;
1038 path.addOval(rect);
1039 GrPathFill fill = (strokeWidth == 0) ?
bsalomon@google.com0982d352012-07-31 15:33:25 +00001040 kHairLine_GrPathFill : kWinding_GrPathFill;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001041 this->internalDrawPath(paint, path, fill, NULL);
1042 return;
1043 }
1044
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001045 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
1046
bsalomon@google.com0982d352012-07-31 15:33:25 +00001047 GrDrawState* drawState = target->drawState();
1048 GrDrawState::AutoStageDisable atr(fDrawState);
1049 const GrMatrix vm = drawState->getViewMatrix();
1050
bsalomon@google.com93c96602012-04-27 13:05:21 +00001051 const GrRenderTarget* rt = drawState->getRenderTarget();
1052 if (NULL == rt) {
1053 return;
1054 }
1055
bsalomon@google.come3d32162012-07-20 13:37:06 +00001056 GrDrawTarget::AutoDeviceCoordDraw adcd(target);
1057 if (!adcd.succeeded()) {
1058 return;
1059 }
bsalomon@google.com93c96602012-04-27 13:05:21 +00001060
bsalomon@google.come3d32162012-07-20 13:37:06 +00001061 GrVertexLayout layout = GrDrawTarget::kEdge_VertexLayoutBit;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001062 GrAssert(sizeof(CircleVertex) == GrDrawTarget::VertexSize(layout));
1063
1064 GrPoint center = GrPoint::Make(rect.centerX(), rect.centerY());
1065 GrScalar radius = SkScalarHalf(rect.width());
1066
1067 vm.mapPoints(&center, 1);
1068 radius = vm.mapRadius(radius);
1069
1070 GrScalar outerRadius = radius;
1071 GrScalar innerRadius = 0;
1072 SkScalar halfWidth = 0;
1073 if (strokeWidth == 0) {
1074 halfWidth = SkScalarHalf(SK_Scalar1);
1075
1076 outerRadius += halfWidth;
1077 innerRadius = SkMaxScalar(0, radius - halfWidth);
1078 }
1079
1080 GrDrawTarget::AutoReleaseGeometry geo(target, layout, 4, 0);
1081 if (!geo.succeeded()) {
1082 GrPrintf("Failed to get space for vertices!\n");
1083 return;
1084 }
1085
1086 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
1087
robertphillips@google.coma0a66c12012-06-22 13:14:29 +00001088 // The fragment shader will extend the radius out half a pixel
1089 // to antialias. Expand the drawn rect here so all the pixels
1090 // will be captured.
1091 SkScalar L = center.fX - outerRadius - SkFloatToScalar(0.5f);
1092 SkScalar R = center.fX + outerRadius + SkFloatToScalar(0.5f);
1093 SkScalar T = center.fY - outerRadius - SkFloatToScalar(0.5f);
1094 SkScalar B = center.fY + outerRadius + SkFloatToScalar(0.5f);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001095
1096 verts[0].fPos = SkPoint::Make(L, T);
1097 verts[1].fPos = SkPoint::Make(R, T);
1098 verts[2].fPos = SkPoint::Make(L, B);
1099 verts[3].fPos = SkPoint::Make(R, B);
1100
1101 for (int i = 0; i < 4; ++i) {
1102 // this goes to fragment shader, it should be in y-points-up space.
1103 verts[i].fCenter = SkPoint::Make(center.fX, rt->height() - center.fY);
1104
1105 verts[i].fOuterRadius = outerRadius;
1106 verts[i].fInnerRadius = innerRadius;
1107 }
1108
1109 drawState->setVertexEdgeType(GrDrawState::kCircle_EdgeType);
bsalomon@google.com47059542012-06-06 20:51:20 +00001110 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001111}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001112
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001113void GrContext::drawPath(const GrPaint& paint, const SkPath& path,
reed@google.com07f3ee12011-05-16 17:21:57 +00001114 GrPathFill fill, const GrPoint* translate) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001115
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001116 if (path.isEmpty()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001117 if (GrIsFillInverted(fill)) {
1118 this->drawPaint(paint);
1119 }
1120 return;
1121 }
1122
bsalomon@google.com93c96602012-04-27 13:05:21 +00001123 SkRect ovalRect;
1124 if (!GrIsFillInverted(fill) && path.isOval(&ovalRect)) {
1125 if (translate) {
1126 ovalRect.offset(*translate);
1127 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001128 SkScalar width = (fill == kHairLine_GrPathFill) ? 0 : -SK_Scalar1;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001129 this->drawOval(paint, ovalRect, width);
1130 return;
1131 }
1132
1133 internalDrawPath(paint, path, fill, translate);
1134}
1135
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001136void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +00001137 GrPathFill fill, const GrPoint* translate) {
1138
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001139 // Note that below we may sw-rasterize the path into a scratch texture.
1140 // Scratch textures can be recycled after they are returned to the texture
1141 // cache. This presents a potential hazard for buffered drawing. However,
1142 // the writePixels that uploads to the scratch will perform a flush so we're
1143 // OK.
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001144 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +00001145 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001146
bsalomon@google.com289533a2011-10-27 12:34:25 +00001147 bool prAA = paint.fAntiAlias && !this->getRenderTarget()->isMultisampled();
1148
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001149 // An Assumption here is that path renderer would use some form of tweaking
1150 // the src color (either the input alpha or in the frag shader) to implement
1151 // aa. If we have some future driver-mojo path AA that can do the right
1152 // thing WRT to the blend then we'll need some query on the PR.
1153 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001154#if GR_DEBUG
bsalomon@google.com979432b2011-11-05 21:38:22 +00001155 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001156#endif
bsalomon@google.com289533a2011-10-27 12:34:25 +00001157 prAA = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001158 }
bsalomon@google.com289533a2011-10-27 12:34:25 +00001159
robertphillips@google.com72176b22012-05-23 13:19:12 +00001160 GrPathRenderer* pr = this->getPathRenderer(path, fill, target, prAA, true);
bsalomon@google.com30085192011-08-19 15:42:31 +00001161 if (NULL == pr) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001162#if GR_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001163 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001164#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001165 return;
1166 }
1167
bsalomon@google.come3d32162012-07-20 13:37:06 +00001168 pr->drawPath(path, fill, translate, target, prAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001169}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001170
bsalomon@google.com27847de2011-02-22 20:59:41 +00001171////////////////////////////////////////////////////////////////////////////////
1172
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001173void GrContext::flush(int flagsBitfield) {
1174 if (kDiscard_FlushBit & flagsBitfield) {
1175 fDrawBuffer->reset();
1176 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001177 this->flushDrawBuffer();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001178 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001179 if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001180 fGpu->forceRenderTargetFlush();
1181 }
1182}
1183
bsalomon@google.com27847de2011-02-22 20:59:41 +00001184void GrContext::flushDrawBuffer() {
junov@google.com53a55842011-06-08 22:55:10 +00001185 if (fDrawBuffer) {
robertphillips@google.com58b38182012-05-03 16:29:41 +00001186 // With addition of the AA clip path, flushing the draw buffer can
1187 // result in the generation of an AA clip mask. During this
1188 // process the SW path renderer may be invoked which recusively
1189 // calls this method (via internalWriteTexturePixels) creating
1190 // infinite recursion
1191 GrInOrderDrawBuffer* temp = fDrawBuffer;
1192 fDrawBuffer = NULL;
1193
1194 temp->flushTo(fGpu);
1195
1196 fDrawBuffer = temp;
junov@google.com53a55842011-06-08 22:55:10 +00001197 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001198}
1199
bsalomon@google.com0342a852012-08-20 19:22:38 +00001200void GrContext::writeTexturePixels(GrTexture* texture,
1201 int left, int top, int width, int height,
1202 GrPixelConfig config, const void* buffer, size_t rowBytes,
1203 uint32_t flags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001204 SK_TRACE_EVENT0("GrContext::writeTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001205 ASSERT_OWNED_RESOURCE(texture);
1206
bsalomon@google.com0342a852012-08-20 19:22:38 +00001207 // TODO: use scratch texture to perform conversion
1208 if (kUnpremul_PixelOpsFlag & flags) {
1209 return;
1210 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001211 if (!(kDontFlush_PixelOpsFlag & flags)) {
1212 this->flush();
1213 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001214
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001215 fGpu->writeTexturePixels(texture, left, top, width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +00001216 config, buffer, rowBytes);
1217}
1218
bsalomon@google.com0342a852012-08-20 19:22:38 +00001219bool GrContext::readTexturePixels(GrTexture* texture,
1220 int left, int top, int width, int height,
1221 GrPixelConfig config, void* buffer, size_t rowBytes,
1222 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001223 SK_TRACE_EVENT0("GrContext::readTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001224 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001225
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001226 // TODO: code read pixels for textures that aren't also rendertargets
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001227 GrRenderTarget* target = texture->asRenderTarget();
1228 if (NULL != target) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001229 return this->readRenderTargetPixels(target,
1230 left, top, width, height,
1231 config, buffer, rowBytes,
1232 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001233 } else {
1234 return false;
1235 }
1236}
1237
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001238#include "SkConfig8888.h"
1239
1240namespace {
1241/**
1242 * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel
1243 * formats are representable as Config8888 and so the function returns false
1244 * if the GrPixelConfig has no equivalent Config8888.
1245 */
1246bool grconfig_to_config8888(GrPixelConfig config,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001247 bool unpremul,
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001248 SkCanvas::Config8888* config8888) {
1249 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001250 case kRGBA_8888_GrPixelConfig:
1251 if (unpremul) {
1252 *config8888 = SkCanvas::kRGBA_Unpremul_Config8888;
1253 } else {
1254 *config8888 = SkCanvas::kRGBA_Premul_Config8888;
1255 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001256 return true;
bsalomon@google.com0342a852012-08-20 19:22:38 +00001257 case kBGRA_8888_GrPixelConfig:
1258 if (unpremul) {
1259 *config8888 = SkCanvas::kBGRA_Unpremul_Config8888;
1260 } else {
1261 *config8888 = SkCanvas::kBGRA_Premul_Config8888;
1262 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001263 return true;
1264 default:
1265 return false;
1266 }
1267}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001268
1269// It returns a configuration with where the byte position of the R & B components are swapped in
1270// relation to the input config. This should only be called with the result of
1271// grconfig_to_config8888 as it will fail for other configs.
1272SkCanvas::Config8888 swap_config8888_red_and_blue(SkCanvas::Config8888 config8888) {
1273 switch (config8888) {
1274 case SkCanvas::kBGRA_Premul_Config8888:
1275 return SkCanvas::kRGBA_Premul_Config8888;
1276 case SkCanvas::kBGRA_Unpremul_Config8888:
1277 return SkCanvas::kRGBA_Unpremul_Config8888;
1278 case SkCanvas::kRGBA_Premul_Config8888:
1279 return SkCanvas::kBGRA_Premul_Config8888;
1280 case SkCanvas::kRGBA_Unpremul_Config8888:
1281 return SkCanvas::kBGRA_Unpremul_Config8888;
1282 default:
1283 GrCrash("Unexpected input");
1284 return SkCanvas::kBGRA_Unpremul_Config8888;;
1285 }
1286}
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001287}
1288
bsalomon@google.com0342a852012-08-20 19:22:38 +00001289bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
1290 int left, int top, int width, int height,
1291 GrPixelConfig config, void* buffer, size_t rowBytes,
1292 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001293 SK_TRACE_EVENT0("GrContext::readRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001294 ASSERT_OWNED_RESOURCE(target);
1295
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001296 if (NULL == target) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001297 target = fDrawState->getRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001298 if (NULL == target) {
1299 return false;
1300 }
1301 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001302
bsalomon@google.com6f379512011-11-16 20:36:03 +00001303 if (!(kDontFlush_PixelOpsFlag & flags)) {
1304 this->flush();
1305 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001306
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001307 // Determine which conversions have to be applied: flipY, swapRAnd, and/or unpremul.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001308
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001309 // If fGpu->readPixels would incur a y-flip cost then we will read the pixels upside down. We'll
1310 // either do the flipY by drawing into a scratch with a matrix or on the cpu after the read.
1311 bool flipY = fGpu->readPixelsWillPayForYFlip(target, left, top,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001312 width, height, config,
1313 rowBytes);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001314 bool swapRAndB = fGpu->preferredReadPixelsConfig(config) == GrPixelConfigSwapRAndB(config);
1315
bsalomon@google.com0342a852012-08-20 19:22:38 +00001316 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001317
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001318 // flipY will get set to false when it is handled below using a scratch. However, in that case
1319 // we still want to do the read upside down.
1320 bool readUpsideDown = flipY;
1321
1322 if (unpremul && kRGBA_8888_GrPixelConfig != config && kBGRA_8888_GrPixelConfig != config) {
1323 // The unpremul flag is only allowed for these two configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001324 return false;
1325 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001326
1327 GrPixelConfig readConfig;
1328 if (swapRAndB) {
1329 readConfig = GrPixelConfigSwapRAndB(config);
1330 GrAssert(kUnknown_GrPixelConfig != config);
1331 } else {
1332 readConfig = config;
1333 }
1334
1335 // If the src is a texture and we would have to do conversions after read pixels, we instead
1336 // do the conversions by drawing the src to a scratch texture. If we handle any of the
1337 // conversions in the draw we set the corresponding bool to false so that we don't reapply it
1338 // on the read back pixels.
1339 GrTexture* src = target->asTexture();
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001340 GrAutoScratchTexture ast;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001341 if (NULL != src && (swapRAndB || unpremul || flipY)) {
1342 // Make the scratch a render target because we don't have a robust readTexturePixels as of
1343 // yet. It calls this function.
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001344 GrTextureDesc desc;
1345 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1346 desc.fWidth = width;
1347 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001348 desc.fConfig = readConfig;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001349
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001350 // When a full readback is faster than a partial we could always make the scratch exactly
1351 // match the passed rect. However, if we see many different size rectangles we will trash
1352 // our texture cache and pay the cost of creating and destroying many textures. So, we only
1353 // request an exact match when the caller is reading an entire RT.
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001354 ScratchTexMatch match = kApprox_ScratchTexMatch;
1355 if (0 == left &&
1356 0 == top &&
1357 target->width() == width &&
1358 target->height() == height &&
1359 fGpu->fullReadPixelsIsFasterThanPartial()) {
1360 match = kExact_ScratchTexMatch;
1361 }
1362 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001363 GrTexture* texture = ast.texture();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001364 if (texture) {
1365 SkAutoTUnref<GrCustomStage> stage;
1366 if (unpremul) {
1367 stage.reset(this->createPMToUPMEffect(src, swapRAndB));
1368 }
1369 // If we failed to create a PM->UPM effect and have no other conversions to perform then
1370 // there is no longer any point to using the scratch.
1371 if (NULL != stage || flipY || swapRAndB) {
1372 if (NULL == stage) {
1373 stage.reset(GrConfigConversionEffect::Create(src, swapRAndB));
1374 GrAssert(NULL != stage);
1375 } else {
1376 unpremul = false; // we will handle the UPM conversion in the draw
1377 }
1378 swapRAndB = false; // we will handle the swap in the draw.
bsalomon@google.comc4364992011-11-07 15:54:49 +00001379
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001380 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
1381 GrDrawState* drawState = fGpu->drawState();
1382 drawState->setRenderTarget(texture->asRenderTarget());
1383 GrMatrix matrix;
1384 if (flipY) {
1385 matrix.setTranslate(SK_Scalar1 * left,
1386 SK_Scalar1 * (top + height));
1387 matrix.set(GrMatrix::kMScaleY, -GR_Scalar1);
1388 flipY = false; // the y flip will be handled in the draw
1389 } else {
1390 matrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
1391 }
1392 matrix.postIDiv(src->width(), src->height());
1393 drawState->sampler(0)->reset(matrix);
1394 drawState->sampler(0)->setCustomStage(stage);
1395 GrRect rect = GrRect::MakeWH(GrIntToScalar(width), GrIntToScalar(height));
1396 fGpu->drawSimpleRect(rect, NULL);
1397 // we want to read back from the scratch's origin
1398 left = 0;
1399 top = 0;
1400 target = texture->asRenderTarget();
1401 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001402 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001403 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001404 if (!fGpu->readPixels(target,
1405 left, top, width, height,
1406 readConfig, buffer, rowBytes, readUpsideDown)) {
1407 return false;
1408 }
1409 // Perform any conversions we weren't able to perfom using a scratch texture.
1410 if (unpremul || swapRAndB || flipY) {
1411 SkCanvas::Config8888 srcC8888;
1412 SkCanvas::Config8888 dstC8888;
1413 bool c8888IsValid = grconfig_to_config8888(config, false, &srcC8888);
1414 grconfig_to_config8888(config, unpremul, &dstC8888);
1415 if (swapRAndB) {
1416 GrAssert(c8888IsValid); // we should only do r/b swap on 8888 configs
1417 srcC8888 = swap_config8888_red_and_blue(srcC8888);
1418 }
1419 if (flipY) {
1420 size_t tightRB = width * GrBytesPerPixel(config);
1421 if (0 == rowBytes) {
1422 rowBytes = tightRB;
1423 }
1424 SkAutoSTMalloc<256, uint8_t> tempRow(tightRB);
1425 intptr_t top = reinterpret_cast<intptr_t>(buffer);
1426 intptr_t bot = top + (height - 1) * rowBytes;
1427 while (top < bot) {
1428 uint32_t* t = reinterpret_cast<uint32_t*>(top);
1429 uint32_t* b = reinterpret_cast<uint32_t*>(bot);
1430 uint32_t* temp = reinterpret_cast<uint32_t*>(tempRow.get());
1431 memcpy(temp, t, tightRB);
1432 if (c8888IsValid) {
1433 SkConvertConfig8888Pixels(t, tightRB, dstC8888,
1434 b, tightRB, srcC8888,
1435 width, 1);
1436 SkConvertConfig8888Pixels(b, tightRB, dstC8888,
1437 temp, tightRB, srcC8888,
1438 width, 1);
1439 } else {
1440 memcpy(t, b, tightRB);
1441 memcpy(b, temp, tightRB);
1442 }
1443 top += rowBytes;
1444 bot -= rowBytes;
1445 }
1446 // The above loop does nothing on the middle row when height is odd.
1447 if (top == bot && c8888IsValid && dstC8888 != srcC8888) {
1448 uint32_t* mid = reinterpret_cast<uint32_t*>(top);
1449 SkConvertConfig8888Pixels(mid, tightRB, dstC8888, mid, tightRB, srcC8888, width, 1);
1450 }
1451 } else {
1452 // if we aren't flipping Y then we have no reason to be here other than doing
1453 // conversions for 8888 (r/b swap or upm).
1454 GrAssert(c8888IsValid);
1455 uint32_t* b32 = reinterpret_cast<uint32_t*>(buffer);
1456 SkConvertConfig8888Pixels(b32, rowBytes, dstC8888,
1457 b32, rowBytes, srcC8888,
1458 width, height);
1459 }
1460 }
1461 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001462}
1463
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001464void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1465 GrAssert(target);
1466 ASSERT_OWNED_RESOURCE(target);
1467 // In the future we may track whether there are any pending draws to this
1468 // target. We don't today so we always perform a flush. We don't promise
1469 // this to our clients, though.
1470 this->flush();
1471 fGpu->resolveRenderTarget(target);
1472}
1473
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001474void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst) {
1475 if (NULL == src || NULL == dst) {
1476 return;
1477 }
1478 ASSERT_OWNED_RESOURCE(src);
1479
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001480 // Writes pending to the source texture are not tracked, so a flush
1481 // is required to ensure that the copy captures the most recent contents
1482 // of the source texture. See similar behaviour in
1483 // GrContext::resolveRenderTarget.
1484 this->flush();
1485
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001486 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001487 GrDrawState* drawState = fGpu->drawState();
1488 drawState->setRenderTarget(dst);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001489 GrMatrix sampleM;
1490 sampleM.setIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001491 drawState->sampler(0)->reset(sampleM);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001492 drawState->createTextureEffect(0, src);
bsalomon@google.com5db3b6c2012-01-12 20:38:57 +00001493 SkRect rect = SkRect::MakeXYWH(0, 0,
1494 SK_Scalar1 * src->width(),
1495 SK_Scalar1 * src->height());
bsalomon@google.come3d32162012-07-20 13:37:06 +00001496 fGpu->drawSimpleRect(rect, NULL);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001497}
1498
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001499void GrContext::writeRenderTargetPixels(GrRenderTarget* target,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001500 int left, int top, int width, int height,
1501 GrPixelConfig config,
1502 const void* buffer,
1503 size_t rowBytes,
1504 uint32_t flags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001505 SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001506 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001507
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001508 if (NULL == target) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001509 target = fDrawState->getRenderTarget();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001510 if (NULL == target) {
1511 return;
1512 }
1513 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001514
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001515 // TODO: when underlying api has a direct way to do this we should use it (e.g. glDrawPixels on
1516 // desktop GL).
1517
1518 // We will always call some form of writeTexturePixels and we will pass our flags on to it.
1519 // Thus, we don't perform a flush here since that call will do it (if the kNoFlush flag isn't
1520 // set.)
bsalomon@google.com27847de2011-02-22 20:59:41 +00001521
bsalomon@google.com0342a852012-08-20 19:22:38 +00001522 // If the RT is also a texture and we don't have to premultiply then take the texture path.
1523 // We expect to be at least as fast or faster since it doesn't use an intermediate texture as
1524 // we do below.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001525
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001526#if !GR_MAC_BUILD
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001527 // At least some drivers on the Mac get confused when glTexImage2D is called on a texture
1528 // attached to an FBO. The FBO still sees the old image. TODO: determine what OS versions and/or
1529 // HW is affected.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001530 if (NULL != target->asTexture() && !(kUnpremul_PixelOpsFlag & flags)) {
1531 this->writeTexturePixels(target->asTexture(),
1532 left, top, width, height,
1533 config, buffer, rowBytes, flags);
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001534 return;
1535 }
1536#endif
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001537 SkAutoTUnref<GrCustomStage> stage;
1538 bool swapRAndB = (fGpu->preferredReadPixelsConfig(config) == GrPixelConfigSwapRAndB(config));
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001539
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001540 GrPixelConfig textureConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001541 if (swapRAndB) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001542 textureConfig = GrPixelConfigSwapRAndB(config);
1543 } else {
1544 textureConfig = config;
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001545 }
1546
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001547 GrTextureDesc desc;
1548 desc.fWidth = width;
1549 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001550 desc.fConfig = textureConfig;
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001551 GrAutoScratchTexture ast(this, desc);
1552 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001553 if (NULL == texture) {
1554 return;
1555 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001556 // allocate a tmp buffer and sw convert the pixels to premul
1557 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
1558
1559 if (kUnpremul_PixelOpsFlag & flags) {
1560 if (kRGBA_8888_GrPixelConfig != config && kBGRA_8888_GrPixelConfig != config) {
1561 return;
1562 }
1563 stage.reset(this->createUPMToPMEffect(texture, swapRAndB));
1564 if (NULL == stage) {
1565 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1566 GR_DEBUGCODE(bool success = )
1567 grconfig_to_config8888(config, true, &srcConfig8888);
1568 GrAssert(success);
1569 GR_DEBUGCODE(success = )
1570 grconfig_to_config8888(config, false, &dstConfig8888);
1571 GrAssert(success);
1572 const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer);
1573 tmpPixels.reset(width * height);
1574 SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888,
1575 src, rowBytes, srcConfig8888,
1576 width, height);
1577 buffer = tmpPixels.get();
1578 rowBytes = 4 * width;
1579 }
1580 }
1581 if (NULL == stage) {
1582 stage.reset(GrConfigConversionEffect::Create(texture, swapRAndB));
1583 GrAssert(NULL != stage);
1584 }
1585
1586 this->writeTexturePixels(texture,
1587 0, 0, width, height,
1588 textureConfig, buffer, rowBytes,
1589 flags & ~kUnpremul_PixelOpsFlag);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001590
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001591 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001592 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001593
1594 GrMatrix matrix;
1595 matrix.setTranslate(GrIntToScalar(left), GrIntToScalar(top));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001596 drawState->setViewMatrix(matrix);
1597 drawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001598
bsalomon@google.com5c638652011-07-18 19:31:59 +00001599 matrix.setIDiv(texture->width(), texture->height());
bsalomon@google.comb8670992012-07-25 21:27:09 +00001600 drawState->sampler(0)->reset(matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001601 drawState->sampler(0)->setCustomStage(stage);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001602
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001603 fGpu->drawSimpleRect(GrRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)), NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001604}
1605////////////////////////////////////////////////////////////////////////////////
1606
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001607void GrContext::setPaint(const GrPaint& paint) {
tomhudson@google.comcb325ce2012-07-11 14:41:19 +00001608 GrAssert(fDrawState->stagesDisabled());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001609
1610 for (int i = 0; i < GrPaint::kMaxTextures; ++i) {
1611 int s = i + GrPaint::kFirstTextureStage;
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001612 if (paint.isTextureStageEnabled(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001613 *fDrawState->sampler(s) = paint.getTextureSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001614 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001615 }
1616
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001617 fDrawState->setFirstCoverageStage(GrPaint::kFirstMaskStage);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001618
1619 for (int i = 0; i < GrPaint::kMaxMasks; ++i) {
1620 int s = i + GrPaint::kFirstMaskStage;
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001621 if (paint.isMaskStageEnabled(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001622 *fDrawState->sampler(s) = paint.getMaskSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001623 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001624 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001625
bsalomon@google.com26936d02012-03-19 13:06:19 +00001626 // disable all stages not accessible via the paint
1627 for (int s = GrPaint::kTotalStages; s < GrDrawState::kNumStages; ++s) {
tomhudson@google.com676e6602012-07-10 17:21:48 +00001628 fDrawState->disableStage(s);
bsalomon@google.com26936d02012-03-19 13:06:19 +00001629 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001630
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001631 fDrawState->setColor(paint.fColor);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001632
1633 if (paint.fDither) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001634 fDrawState->enableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001635 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001636 fDrawState->disableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001637 }
1638 if (paint.fAntiAlias) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001639 fDrawState->enableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001640 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001641 fDrawState->disableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001642 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001643 if (paint.fColorMatrixEnabled) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001644 fDrawState->enableState(GrDrawState::kColorMatrix_StateBit);
1645 fDrawState->setColorMatrix(paint.fColorMatrix);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001646 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001647 fDrawState->disableState(GrDrawState::kColorMatrix_StateBit);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001648 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001649 fDrawState->setBlendFunc(paint.fSrcBlendCoeff, paint.fDstBlendCoeff);
1650 fDrawState->setColorFilter(paint.fColorFilterColor, paint.fColorFilterXfermode);
1651 fDrawState->setCoverage(paint.fCoverage);
reed@google.com4b2d3f32012-05-15 18:05:50 +00001652#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
bsalomon@google.come3d32162012-07-20 13:37:06 +00001653 if ((paint.hasMask() || 0xff != paint.fCoverage) &&
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001654 !fGpu->canApplyCoverage()) {
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001655 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1656 }
bsalomon@google.com95cd7bd2012-03-28 15:35:05 +00001657#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001658}
1659
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001660GrDrawTarget* GrContext::prepareToDraw(const GrPaint* paint, BufferedDraw buffered) {
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001661 if (kNo_BufferedDraw == buffered && kYes_BufferedDraw == fLastDrawWasBuffered) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001662 this->flushDrawBuffer();
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001663 fLastDrawWasBuffered = kNo_BufferedDraw;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001664 }
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001665 if (NULL != paint) {
1666 this->setPaint(*paint);
1667 }
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001668 if (kYes_BufferedDraw == buffered) {
1669 fDrawBuffer->setClip(fGpu->getClip());
1670 fLastDrawWasBuffered = kYes_BufferedDraw;
1671 return fDrawBuffer;
1672 } else {
1673 GrAssert(kNo_BufferedDraw == buffered);
1674 return fGpu;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001675 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001676}
1677
robertphillips@google.com72176b22012-05-23 13:19:12 +00001678/*
1679 * This method finds a path renderer that can draw the specified path on
1680 * the provided target.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001681 * Due to its expense, the software path renderer has split out so it can
robertphillips@google.com72176b22012-05-23 13:19:12 +00001682 * can be individually allowed/disallowed via the "allowSW" boolean.
1683 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001684GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
bsalomon@google.com289533a2011-10-27 12:34:25 +00001685 GrPathFill fill,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001686 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001687 bool antiAlias,
1688 bool allowSW) {
bsalomon@google.com30085192011-08-19 15:42:31 +00001689 if (NULL == fPathRendererChain) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001690 fPathRendererChain =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001691 SkNEW_ARGS(GrPathRendererChain,
1692 (this, GrPathRendererChain::kNone_UsageFlag));
bsalomon@google.com30085192011-08-19 15:42:31 +00001693 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001694
1695 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path, fill,
1696 target,
1697 antiAlias);
1698
1699 if (NULL == pr && allowSW) {
1700 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001701 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001702 }
1703
1704 pr = fSoftwarePathRenderer;
1705 }
1706
1707 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001708}
1709
bsalomon@google.com27847de2011-02-22 20:59:41 +00001710////////////////////////////////////////////////////////////////////////////////
1711
bsalomon@google.com27847de2011-02-22 20:59:41 +00001712void GrContext::setRenderTarget(GrRenderTarget* target) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001713 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001714 fDrawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001715}
1716
1717GrRenderTarget* GrContext::getRenderTarget() {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001718 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001719}
1720
1721const GrRenderTarget* GrContext::getRenderTarget() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001722 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001723}
1724
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001725bool GrContext::isConfigRenderable(GrPixelConfig config) const {
1726 return fGpu->isConfigRenderable(config);
1727}
1728
bsalomon@google.com27847de2011-02-22 20:59:41 +00001729const GrMatrix& GrContext::getMatrix() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001730 return fDrawState->getViewMatrix();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001731}
1732
1733void GrContext::setMatrix(const GrMatrix& m) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001734 fDrawState->setViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001735}
1736
1737void GrContext::concatMatrix(const GrMatrix& m) const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001738 fDrawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001739}
1740
1741static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
1742 intptr_t mask = 1 << shift;
1743 if (pred) {
1744 bits |= mask;
1745 } else {
1746 bits &= ~mask;
1747 }
1748 return bits;
1749}
1750
bsalomon@google.com583a1e32011-08-17 13:42:46 +00001751GrContext::GrContext(GrGpu* gpu) {
bsalomon@google.comc0af3172012-06-15 14:10:09 +00001752 ++THREAD_INSTANCE_COUNT;
1753
bsalomon@google.com27847de2011-02-22 20:59:41 +00001754 fGpu = gpu;
1755 fGpu->ref();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001756 fGpu->setContext(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001757
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001758 fDrawState = SkNEW(GrDrawState);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001759 fGpu->setDrawState(fDrawState);
1760
bsalomon@google.com30085192011-08-19 15:42:31 +00001761 fPathRendererChain = NULL;
robertphillips@google.com72176b22012-05-23 13:19:12 +00001762 fSoftwarePathRenderer = NULL;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001763
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001764 fTextureCache = SkNEW_ARGS(GrResourceCache,
1765 (MAX_TEXTURE_CACHE_COUNT,
1766 MAX_TEXTURE_CACHE_BYTES));
1767 fFontCache = SkNEW_ARGS(GrFontCache, (fGpu));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001768
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001769 fLastDrawWasBuffered = kNo_BufferedDraw;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001770
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001771 fDrawBuffer = NULL;
1772 fDrawBufferVBAllocPool = NULL;
1773 fDrawBufferIBAllocPool = NULL;
1774
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001775 fAARectRenderer = SkNEW(GrAARectRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001776
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001777 fDidTestPMConversions = false;
1778
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001779 this->setupDrawBuffer();
1780}
1781
1782void GrContext::setupDrawBuffer() {
1783
1784 GrAssert(NULL == fDrawBuffer);
1785 GrAssert(NULL == fDrawBufferVBAllocPool);
1786 GrAssert(NULL == fDrawBufferIBAllocPool);
1787
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001788 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001789 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001790 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001791 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001792 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001793 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001794 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001795 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001796
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001797 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com471d4712011-08-23 15:45:25 +00001798 fDrawBufferVBAllocPool,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001799 fDrawBufferIBAllocPool));
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001800
bsalomon@google.com27847de2011-02-22 20:59:41 +00001801 fDrawBuffer->setQuadIndexBuffer(this->getQuadIndexBuffer());
bsalomon@google.com1015e032012-06-25 18:41:04 +00001802 if (fDrawBuffer) {
1803 fDrawBuffer->setAutoFlushTarget(fGpu);
1804 fDrawBuffer->setDrawState(fDrawState);
1805 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001806}
1807
bsalomon@google.com27847de2011-02-22 20:59:41 +00001808GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) {
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001809 return prepareToDraw(&paint, DEFAULT_BUFFERING);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001810}
1811
1812const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1813 return fGpu->getQuadIndexBuffer();
1814}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001815
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001816namespace {
1817void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
1818 GrConfigConversionEffect::PMConversion pmToUPM;
1819 GrConfigConversionEffect::PMConversion upmToPM;
1820 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
1821 *pmToUPMValue = pmToUPM;
1822 *upmToPMValue = upmToPM;
1823}
1824}
1825
1826GrCustomStage* GrContext::createPMToUPMEffect(GrTexture* texture, bool swapRAndB) {
1827 if (!fDidTestPMConversions) {
1828 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001829 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001830 }
1831 GrConfigConversionEffect::PMConversion pmToUPM =
1832 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
1833 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
1834 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM);
1835 } else {
1836 return NULL;
1837 }
1838}
1839
1840GrCustomStage* GrContext::createUPMToPMEffect(GrTexture* texture, bool swapRAndB) {
1841 if (!fDidTestPMConversions) {
1842 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001843 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001844 }
1845 GrConfigConversionEffect::PMConversion upmToPM =
1846 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
1847 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
1848 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM);
1849 } else {
1850 return NULL;
1851 }
1852}
1853
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001854GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture,
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001855 bool canClobberSrc,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001856 const SkRect& rect,
1857 float sigmaX, float sigmaY) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001858 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001859 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001860 AutoMatrix avm(this, GrMatrix::I());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001861 SkIRect clearRect;
bsalomon@google.comb505a122012-05-31 18:40:36 +00001862 int scaleFactorX, radiusX;
1863 int scaleFactorY, radiusY;
1864 sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX);
1865 sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY);
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001866
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001867 SkRect srcRect(rect);
1868 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
1869 srcRect.roundOut();
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001870 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001871 static_cast<float>(scaleFactorY));
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001872
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001873 AutoClip acs(this, srcRect);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001874
bsalomon@google.com0342a852012-08-20 19:22:38 +00001875 GrAssert(kBGRA_8888_GrPixelConfig == srcTexture->config() ||
1876 kRGBA_8888_GrPixelConfig == srcTexture->config() ||
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001877 kAlpha_8_GrPixelConfig == srcTexture->config());
1878
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001879 GrTextureDesc desc;
1880 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1881 desc.fWidth = SkScalarFloorToInt(srcRect.width());
1882 desc.fHeight = SkScalarFloorToInt(srcRect.height());
1883 desc.fConfig = srcTexture->config();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001884
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001885 GrAutoScratchTexture temp1, temp2;
1886 GrTexture* dstTexture = temp1.set(this, desc);
1887 GrTexture* tempTexture = canClobberSrc ? srcTexture : temp2.set(this, desc);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001888
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001889 GrPaint paint;
1890 paint.reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +00001891 paint.textureSampler(0)->textureParams()->setBilerp(true);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001892
1893 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
1894 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1895 srcTexture->height());
1896 this->setRenderTarget(dstTexture->asRenderTarget());
1897 SkRect dstRect(srcRect);
1898 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
1899 i < scaleFactorY ? 0.5f : 1.0f);
tomhudson@google.comaa72eab2012-07-19 18:01:07 +00001900 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
1901 (srcTexture)))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001902 this->drawRectToRect(paint, dstRect, srcRect);
1903 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001904 srcTexture = dstTexture;
1905 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001906 }
1907
robertphillips@google.com7a396332012-05-10 15:11:27 +00001908 SkIRect srcIRect;
1909 srcRect.roundOut(&srcIRect);
1910
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001911 if (sigmaX > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001912 if (scaleFactorX > 1) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001913 // Clear out a radius to the right of the srcRect to prevent the
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001914 // X convolution from reading garbage.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001915 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001916 radiusX, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001917 this->clear(&clearRect, 0x0);
1918 }
1919
1920 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001921 GrDrawTarget* target = this->prepareToDraw(NULL, DEFAULT_BUFFERING);
1922 convolve_gaussian(target, srcTexture, srcRect, sigmaX, radiusX,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001923 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001924 srcTexture = dstTexture;
1925 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001926 }
1927
1928 if (sigmaY > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001929 if (scaleFactorY > 1 || sigmaX > 0.0f) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001930 // Clear out a radius below the srcRect to prevent the Y
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001931 // convolution from reading garbage.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001932 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001933 srcIRect.width(), radiusY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001934 this->clear(&clearRect, 0x0);
1935 }
1936
1937 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001938 GrDrawTarget* target = this->prepareToDraw(NULL, DEFAULT_BUFFERING);
1939 convolve_gaussian(target, srcTexture, srcRect, sigmaY, radiusY,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001940 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001941 srcTexture = dstTexture;
1942 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001943 }
1944
1945 if (scaleFactorX > 1 || scaleFactorY > 1) {
1946 // Clear one pixel to the right and below, to accommodate bilinear
1947 // upsampling.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001948 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
robertphillips@google.com7a396332012-05-10 15:11:27 +00001949 srcIRect.width() + 1, 1);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001950 this->clear(&clearRect, 0x0);
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001951 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
robertphillips@google.com7a396332012-05-10 15:11:27 +00001952 1, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001953 this->clear(&clearRect, 0x0);
1954 // FIXME: This should be mitchell, not bilinear.
bsalomon@google.comb8670992012-07-25 21:27:09 +00001955 paint.textureSampler(0)->textureParams()->setBilerp(true);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001956 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1957 srcTexture->height());
1958 this->setRenderTarget(dstTexture->asRenderTarget());
tomhudson@google.comaa72eab2012-07-19 18:01:07 +00001959 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
1960 (srcTexture)))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001961 SkRect dstRect(srcRect);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001962 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001963 this->drawRectToRect(paint, dstRect, srcRect);
1964 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001965 srcTexture = dstTexture;
1966 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001967 }
1968 this->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001969 if (srcTexture == temp1.texture()) {
1970 return temp1.detach();
1971 } else if (srcTexture == temp2.texture()) {
1972 return temp2.detach();
1973 } else {
1974 srcTexture->ref();
1975 return srcTexture;
1976 }
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001977}
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001978
bsalomon@google.comc4364992011-11-07 15:54:49 +00001979///////////////////////////////////////////////////////////////////////////////
robertphillips@google.com59552022012-08-31 13:07:37 +00001980#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001981void GrContext::printCacheStats() const {
1982 fTextureCache->printStats();
1983}
1984#endif