blob: 26bef45d426790a49dc56b87eae5303fbd71d056 [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() {
robertphillips@google.comcdb426d2012-09-24 19:33:59 +000091 for (int i = 0; i < fCleanUpData.count(); ++i) {
92 (*fCleanUpData[i].fFunc)(this, fCleanUpData[i].fInfo);
93 }
94
bsalomon@google.com8fe72472011-03-30 21:26:44 +000095 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +000096
97 // Since the gpu can hold scratch textures, give it a chance to let go
98 // of them before freeing the texture cache
99 fGpu->purgeResources();
100
bsalomon@google.com27847de2011-02-22 20:59:41 +0000101 delete fTextureCache;
robertphillips@google.comeb9b3e12012-09-11 12:50:53 +0000102 fTextureCache = NULL;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000103 delete fFontCache;
104 delete fDrawBuffer;
105 delete fDrawBufferVBAllocPool;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000106 delete fDrawBufferIBAllocPool;
bsalomon@google.com30085192011-08-19 15:42:31 +0000107
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000108 fAARectRenderer->unref();
109
bsalomon@google.com205d4602011-04-25 12:43:45 +0000110 fGpu->unref();
bsalomon@google.com30085192011-08-19 15:42:31 +0000111 GrSafeUnref(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000112 GrSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000113 fDrawState->unref();
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000114
115 --THREAD_INSTANCE_COUNT;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000116}
117
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000118void GrContext::contextLost() {
junov@google.com53a55842011-06-08 22:55:10 +0000119 contextDestroyed();
120 this->setupDrawBuffer();
121}
122
123void GrContext::contextDestroyed() {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000124 // abandon first to so destructors
125 // don't try to free the resources in the API.
126 fGpu->abandonResources();
127
bsalomon@google.com30085192011-08-19 15:42:31 +0000128 // a path renderer may be holding onto resources that
129 // are now unusable
130 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000131 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000132
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000133 delete fDrawBuffer;
134 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000135
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000136 delete fDrawBufferVBAllocPool;
137 fDrawBufferVBAllocPool = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000138
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000139 delete fDrawBufferIBAllocPool;
140 fDrawBufferIBAllocPool = NULL;
141
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000142 fAARectRenderer->reset();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000143
bsalomon@google.coma2921122012-08-28 12:34:17 +0000144 fTextureCache->purgeAllUnlocked();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000145 fFontCache->freeAll();
146 fGpu->markContextDirty();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000147}
148
149void GrContext::resetContext() {
150 fGpu->markContextDirty();
151}
152
153void GrContext::freeGpuResources() {
154 this->flush();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000155
robertphillips@google.comff175842012-05-14 19:31:39 +0000156 fGpu->purgeResources();
157
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000158 fAARectRenderer->reset();
159
bsalomon@google.coma2921122012-08-28 12:34:17 +0000160 fTextureCache->purgeAllUnlocked();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000161 fFontCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000162 // a path renderer may be holding onto resources
163 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000164 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000165}
166
twiz@google.com05e70242012-01-27 19:12:00 +0000167size_t GrContext::getGpuTextureCacheBytes() const {
168 return fTextureCache->getCachedResourceBytes();
169}
170
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000171////////////////////////////////////////////////////////////////////////////////
172
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000173namespace {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000174
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000175void scale_rect(SkRect* rect, float xScale, float yScale) {
robertphillips@google.com5af56062012-04-27 15:39:52 +0000176 rect->fLeft = SkScalarMul(rect->fLeft, SkFloatToScalar(xScale));
177 rect->fTop = SkScalarMul(rect->fTop, SkFloatToScalar(yScale));
178 rect->fRight = SkScalarMul(rect->fRight, SkFloatToScalar(xScale));
179 rect->fBottom = SkScalarMul(rect->fBottom, SkFloatToScalar(yScale));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000180}
181
bsalomon@google.comb505a122012-05-31 18:40:36 +0000182float adjust_sigma(float sigma, int *scaleFactor, int *radius) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000183 *scaleFactor = 1;
184 while (sigma > MAX_BLUR_SIGMA) {
185 *scaleFactor *= 2;
186 sigma *= 0.5f;
187 }
bsalomon@google.comb505a122012-05-31 18:40:36 +0000188 *radius = static_cast<int>(ceilf(sigma * 3.0f));
189 GrAssert(*radius <= GrConvolutionEffect::kMaxKernelRadius);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000190 return sigma;
191}
192
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000193void convolve_gaussian(GrDrawTarget* target,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000194 GrTexture* texture,
195 const SkRect& rect,
196 float sigma,
197 int radius,
198 Gr1DKernelEffect::Direction direction) {
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000199 GrRenderTarget* rt = target->drawState()->getRenderTarget();
200 GrDrawTarget::AutoStateRestore asr(target, GrDrawTarget::kReset_ASRInit);
201 GrDrawState* drawState = target->drawState();
202 drawState->setRenderTarget(rt);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000203 GrMatrix sampleM;
204 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000205 drawState->sampler(0)->reset(sampleM);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000206 SkAutoTUnref<GrConvolutionEffect> conv(SkNEW_ARGS(GrConvolutionEffect,
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000207 (texture, direction, radius,
208 sigma)));
bsalomon@google.comb505a122012-05-31 18:40:36 +0000209 drawState->sampler(0)->setCustomStage(conv);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000210 target->drawSimpleRect(rect, NULL);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000211}
212
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000213}
214
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000215
216GrTexture* GrContext::findTexture(const GrCacheKey& key) {
217 return static_cast<GrTexture*>(fTextureCache->find(key.key()));
218}
219
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000220GrTexture* GrContext::findTexture(const GrTextureDesc& desc,
221 const GrCacheData& cacheData,
222 const GrTextureParams* params) {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000223 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000224 GrResource* resource = fTextureCache->find(resourceKey);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000225 return static_cast<GrTexture*>(resource);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000226}
227
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000228bool GrContext::isTextureInCache(const GrTextureDesc& desc,
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000229 const GrCacheData& cacheData,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000230 const GrTextureParams* params) const {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000231 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000232 return fTextureCache->hasKey(resourceKey);
233}
234
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000235void GrContext::addStencilBuffer(GrStencilBuffer* sb) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000236 ASSERT_OWNED_RESOURCE(sb);
robertphillips@google.com46a86002012-08-08 10:42:44 +0000237
238 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(sb->width(),
239 sb->height(),
240 sb->numSamples());
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000241 fTextureCache->create(resourceKey, sb);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000242}
243
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000244GrStencilBuffer* GrContext::findStencilBuffer(int width, int height,
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000245 int sampleCnt) {
robertphillips@google.com46a86002012-08-08 10:42:44 +0000246 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(width,
247 height,
248 sampleCnt);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000249 GrResource* resource = fTextureCache->find(resourceKey);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000250 return static_cast<GrStencilBuffer*>(resource);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000251}
252
bsalomon@google.com27847de2011-02-22 20:59:41 +0000253static void stretchImage(void* dst,
254 int dstW,
255 int dstH,
256 void* src,
257 int srcW,
258 int srcH,
259 int bpp) {
260 GrFixed dx = (srcW << 16) / dstW;
261 GrFixed dy = (srcH << 16) / dstH;
262
263 GrFixed y = dy >> 1;
264
265 int dstXLimit = dstW*bpp;
266 for (int j = 0; j < dstH; ++j) {
267 GrFixed x = dx >> 1;
268 void* srcRow = (uint8_t*)src + (y>>16)*srcW*bpp;
269 void* dstRow = (uint8_t*)dst + j*dstW*bpp;
270 for (int i = 0; i < dstXLimit; i += bpp) {
271 memcpy((uint8_t*) dstRow + i,
272 (uint8_t*) srcRow + (x>>16)*bpp,
273 bpp);
274 x += dx;
275 }
276 y += dy;
277 }
278}
279
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000280// The desired texture is NPOT and tiled but that isn't supported by
robertphillips@google.com3319f332012-08-13 18:00:36 +0000281// the current hardware. Resize the texture to be a POT
282GrTexture* GrContext::createResizedTexture(const GrTextureDesc& desc,
283 const GrCacheData& cacheData,
284 void* srcData,
285 size_t rowBytes,
286 bool needsFiltering) {
robertphillips@google.com0d25eef2012-09-11 21:25:51 +0000287 GrTexture* clampedTexture = this->findTexture(desc, cacheData, NULL);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000288 if (NULL == clampedTexture) {
robertphillips@google.com0d25eef2012-09-11 21:25:51 +0000289 clampedTexture = this->createTexture(NULL, desc, cacheData, srcData, rowBytes);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000290
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000291 GrAssert(NULL != clampedTexture);
292 if (NULL == clampedTexture) {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000293 return NULL;
294 }
295 }
robertphillips@google.com0d25eef2012-09-11 21:25:51 +0000296
297 clampedTexture->ref();
298
robertphillips@google.com3319f332012-08-13 18:00:36 +0000299 GrTextureDesc rtDesc = desc;
300 rtDesc.fFlags = rtDesc.fFlags |
301 kRenderTarget_GrTextureFlagBit |
302 kNoStencil_GrTextureFlagBit;
303 rtDesc.fWidth = GrNextPow2(GrMax(desc.fWidth, 64));
304 rtDesc.fHeight = GrNextPow2(GrMax(desc.fHeight, 64));
305
306 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
307
308 if (NULL != texture) {
309 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
310 GrDrawState* drawState = fGpu->drawState();
311 drawState->setRenderTarget(texture->asRenderTarget());
312
313 // if filtering is not desired then we want to ensure all
314 // texels in the resampled image are copies of texels from
315 // the original.
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000316 drawState->sampler(0)->reset();
317 GrTextureParams params(SkShader::kClamp_TileMode, needsFiltering);
318 drawState->createTextureEffect(0, clampedTexture, params);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000319
320 static const GrVertexLayout layout =
321 GrDrawTarget::StageTexCoordVertexLayoutBit(0,0);
322 GrDrawTarget::AutoReleaseGeometry arg(fGpu, layout, 4, 0);
323
324 if (arg.succeeded()) {
325 GrPoint* verts = (GrPoint*) arg.vertices();
326 verts[0].setIRectFan(0, 0,
327 texture->width(),
328 texture->height(),
329 2*sizeof(GrPoint));
330 verts[1].setIRectFan(0, 0, 1, 1, 2*sizeof(GrPoint));
331 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType,
332 0, 4);
333 }
334 texture->releaseRenderTarget();
335 } else {
336 // TODO: Our CPU stretch doesn't filter. But we create separate
337 // stretched textures when the sampler state is either filtered or
338 // not. Either implement filtered stretch blit on CPU or just create
339 // one when FBO case fails.
340
341 rtDesc.fFlags = kNone_GrTextureFlags;
342 // no longer need to clamp at min RT size.
343 rtDesc.fWidth = GrNextPow2(desc.fWidth);
344 rtDesc.fHeight = GrNextPow2(desc.fHeight);
345 int bpp = GrBytesPerPixel(desc.fConfig);
346 SkAutoSMalloc<128*128*4> stretchedPixels(bpp *
347 rtDesc.fWidth *
348 rtDesc.fHeight);
349 stretchImage(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
350 srcData, desc.fWidth, desc.fHeight, bpp);
351
352 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
353
354 GrTexture* texture = fGpu->createTexture(rtDesc,
355 stretchedPixels.get(),
356 stretchedRowBytes);
357 GrAssert(NULL != texture);
358 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000359
robertphillips@google.com0d25eef2012-09-11 21:25:51 +0000360 clampedTexture->unref();
robertphillips@google.com3319f332012-08-13 18:00:36 +0000361 return texture;
362}
363
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000364GrTexture* GrContext::createTexture(
bsalomon@google.comb8670992012-07-25 21:27:09 +0000365 const GrTextureParams* params,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000366 const GrTextureDesc& desc,
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000367 const GrCacheData& cacheData,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000368 void* srcData,
369 size_t rowBytes) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000370 SK_TRACE_EVENT0("GrContext::createAndLockTexture");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000371
372#if GR_DUMP_TEXTURE_UPLOAD
373 GrPrintf("GrContext::createAndLockTexture [%d %d]\n", desc.fWidth, desc.fHeight);
374#endif
375
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000376 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000377
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +0000378 SkAutoTUnref<GrTexture> texture;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000379 if (GrTexture::NeedsResizing(resourceKey)) {
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +0000380 texture.reset(this->createResizedTexture(desc, cacheData,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000381 srcData, rowBytes,
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +0000382 GrTexture::NeedsFiltering(resourceKey)));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000383 } else {
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +0000384 texture.reset(fGpu->createTexture(desc, srcData, rowBytes));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000385 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000386
387 if (NULL != texture) {
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000388 fTextureCache->create(resourceKey, texture);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000389 }
390
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000391 return texture;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000392}
393
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000394GrTexture* GrContext::lockScratchTexture(const GrTextureDesc& inDesc,
395 ScratchTexMatch match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000396 GrTextureDesc desc = inDesc;
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000397 GrCacheData cacheData(GrCacheData::kScratch_CacheID);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000398
rileya@google.com2afb8ec2012-08-23 14:08:57 +0000399 GrAssert((desc.fFlags & kRenderTarget_GrTextureFlagBit) ||
400 !(desc.fFlags & kNoStencil_GrTextureFlagBit));
401
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000402 if (kExact_ScratchTexMatch != match) {
403 // bin by pow2 with a reasonable min
404 static const int MIN_SIZE = 256;
405 desc.fWidth = GrMax(MIN_SIZE, GrNextPow2(desc.fWidth));
406 desc.fHeight = GrMax(MIN_SIZE, GrNextPow2(desc.fHeight));
407 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000408
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000409 GrResource* resource = NULL;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000410 int origWidth = desc.fWidth;
411 int origHeight = desc.fHeight;
412 bool doubledW = false;
413 bool doubledH = false;
414
415 do {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000416 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL, desc, cacheData, true);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000417 resource = fTextureCache->find(key);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000418 // if we miss, relax the fit of the flags...
419 // then try doubling width... then height.
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000420 if (NULL != resource || kExact_ScratchTexMatch == match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000421 break;
422 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000423 // We no longer try to reuse textures that were previously used as render targets in
rileya@google.com2afb8ec2012-08-23 14:08:57 +0000424 // situations where no RT is needed; doing otherwise can confuse the video driver and
425 // cause significant performance problems in some cases.
426 if (desc.fFlags & kNoStencil_GrTextureFlagBit) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000427 desc.fFlags = desc.fFlags & ~kNoStencil_GrTextureFlagBit;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000428 } else if (!doubledW) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000429 desc.fFlags = inDesc.fFlags;
430 desc.fWidth *= 2;
431 doubledW = true;
432 } else if (!doubledH) {
433 desc.fFlags = inDesc.fFlags;
434 desc.fWidth = origWidth;
435 desc.fHeight *= 2;
436 doubledH = true;
437 } else {
438 break;
439 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000440
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000441 } while (true);
442
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000443 if (NULL == resource) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000444 desc.fFlags = inDesc.fFlags;
445 desc.fWidth = origWidth;
446 desc.fHeight = origHeight;
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +0000447 SkAutoTUnref<GrTexture> texture(fGpu->createTexture(desc, NULL, 0));
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000448 if (NULL != texture) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000449 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000450 texture->desc(),
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000451 cacheData,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000452 true);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000453 fTextureCache->create(key, texture);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000454 resource = texture;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000455 }
456 }
457
458 // If the caller gives us the same desc/sampler twice we don't want
459 // to return the same texture the second time (unless it was previously
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000460 // released). So make it exclusive to hide it from future searches.
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000461 if (NULL != resource) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000462 fTextureCache->makeExclusive(resource->getCacheEntry());
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000463 }
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000464
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000465 return static_cast<GrTexture*>(resource);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000466}
467
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000468void GrContext::addExistingTextureToCache(GrTexture* texture) {
469
470 if (NULL == texture) {
471 return;
472 }
473
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000474 // This texture should already have a cache entry since it was once
475 // attached
476 GrAssert(NULL != texture->getCacheEntry());
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000477
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000478 // Conceptually, the cache entry is going to assume responsibility
479 // for the creation ref.
480 GrAssert(1 == texture->getRefCnt());
481
482 // Since this texture came from an AutoScratchTexture it should
483 // still be in the exclusive pile
484 fTextureCache->makeNonExclusive(texture->getCacheEntry());
485
robertphillips@google.com50a035d2012-09-07 19:44:33 +0000486 this->purgeCache();
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000487}
488
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000489
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000490void GrContext::unlockScratchTexture(GrTexture* texture) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000491 ASSERT_OWNED_RESOURCE(texture);
492 GrAssert(NULL != texture->getCacheEntry());
493
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000494 // If this is a scratch texture we detached it from the cache
495 // while it was locked (to avoid two callers simultaneously getting
496 // the same texture).
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000497 if (GrTexture::IsScratchTexture(texture->getCacheEntry()->key())) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000498 fTextureCache->makeNonExclusive(texture->getCacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000499 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000500
robertphillips@google.com50a035d2012-09-07 19:44:33 +0000501 this->purgeCache();
502}
503
504void GrContext::purgeCache() {
robertphillips@google.comeb9b3e12012-09-11 12:50:53 +0000505 if (NULL != fTextureCache) {
506 fTextureCache->purgeAsNeeded();
507 }
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000508}
509
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000510GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000511 void* srcData,
512 size_t rowBytes) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000513 GrTextureDesc descCopy = descIn;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000514 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000515}
516
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000517void GrContext::getTextureCacheLimits(int* maxTextures,
518 size_t* maxTextureBytes) const {
519 fTextureCache->getLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000520}
521
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000522void GrContext::setTextureCacheLimits(int maxTextures, size_t maxTextureBytes) {
523 fTextureCache->setLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000524}
525
bsalomon@google.com91958362011-06-13 17:58:13 +0000526int GrContext::getMaxTextureSize() const {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000527 return fGpu->getCaps().maxTextureSize();
bsalomon@google.com91958362011-06-13 17:58:13 +0000528}
529
530int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000531 return fGpu->getCaps().maxRenderTargetSize();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000532}
533
534///////////////////////////////////////////////////////////////////////////////
535
bsalomon@google.come269f212011-11-07 13:29:52 +0000536GrTexture* GrContext::createPlatformTexture(const GrPlatformTextureDesc& desc) {
537 return fGpu->createPlatformTexture(desc);
538}
539
540GrRenderTarget* GrContext::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
541 return fGpu->createPlatformRenderTarget(desc);
542}
543
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000544///////////////////////////////////////////////////////////////////////////////
545
bsalomon@google.comb8670992012-07-25 21:27:09 +0000546bool GrContext::supportsIndex8PixelConfig(const GrTextureParams* params,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000547 int width, int height) const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000548 const GrDrawTarget::Caps& caps = fGpu->getCaps();
bsalomon@google.comf6601872012-08-28 21:11:35 +0000549 if (!caps.eightBitPaletteSupport()) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000550 return false;
551 }
552
bsalomon@google.com27847de2011-02-22 20:59:41 +0000553 bool isPow2 = GrIsPow2(width) && GrIsPow2(height);
554
555 if (!isPow2) {
bsalomon@google.comb8670992012-07-25 21:27:09 +0000556 bool tiled = NULL != params && params->isTiled();
bsalomon@google.comf6601872012-08-28 21:11:35 +0000557 if (tiled && !caps.npotTextureTileSupport()) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000558 return false;
559 }
560 }
561 return true;
562}
563
564////////////////////////////////////////////////////////////////////////////////
565
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000566const GrClipData* GrContext::getClip() const {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000567 return fGpu->getClip();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000568}
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000569
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000570void GrContext::setClip(const GrClipData* clipData) {
571 fGpu->setClip(clipData);
robertphillips@google.com837ec432012-10-04 17:57:05 +0000572
573 if (clipData->fClipStack->isWideOpen()) {
574 fDrawState->disableState(GrDrawState::kClip_StateBit);
575 } else {
576 fDrawState->enableState(GrDrawState::kClip_StateBit);
577 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000578}
579
bsalomon@google.com27847de2011-02-22 20:59:41 +0000580////////////////////////////////////////////////////////////////////////////////
581
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000582void GrContext::clear(const GrIRect* rect,
583 const GrColor color,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000584 GrRenderTarget* target) {
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000585 this->prepareToDraw(NULL, DEFAULT_BUFFERING)->clear(rect, color, target);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000586}
587
588void GrContext::drawPaint(const GrPaint& paint) {
589 // set rect to be big enough to fill the space, but not super-huge, so we
590 // don't overflow fixed-point implementations
bsalomon@google.comd302f142011-03-03 13:54:13 +0000591 GrRect r;
592 r.setLTRB(0, 0,
593 GrIntToScalar(getRenderTarget()->width()),
594 GrIntToScalar(getRenderTarget()->height()));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000595 GrMatrix inverse;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000596 SkTLazy<GrPaint> tmpPaint;
597 const GrPaint* p = &paint;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000598 AutoMatrix am;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000599
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000600 // We attempt to map r by the inverse matrix and draw that. mapRect will
601 // map the four corners and bound them with a new rect. This will not
602 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000603 if (!this->getMatrix().hasPerspective()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000604 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000605 GrPrintf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000606 return;
607 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000608 inverse.mapRect(&r);
609 } else {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000610 if (paint.hasTextureOrMask()) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000611 tmpPaint.set(paint);
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000612 p = tmpPaint.get();
bsalomon@google.come3d32162012-07-20 13:37:06 +0000613 if (!tmpPaint.get()->preConcatSamplerMatricesWithInverse(fDrawState->getViewMatrix())) {
614 GrPrintf("Could not invert matrix\n");
615 }
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000616 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000617 am.set(this, GrMatrix::I());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000618 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000619 // by definition this fills the entire clip, no need for AA
620 if (paint.fAntiAlias) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000621 if (!tmpPaint.isValid()) {
622 tmpPaint.set(paint);
623 p = tmpPaint.get();
624 }
625 GrAssert(p == tmpPaint.get());
626 tmpPaint.get()->fAntiAlias = false;
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000627 }
628 this->drawRect(*p, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000629}
630
bsalomon@google.com205d4602011-04-25 12:43:45 +0000631////////////////////////////////////////////////////////////////////////////////
632
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000633namespace {
634inline bool disable_coverage_aa_for_blend(GrDrawTarget* target) {
635 return DISABLE_COVERAGE_AA_FOR_BLEND && !target->canApplyCoverage();
636}
637}
638
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000639////////////////////////////////////////////////////////////////////////////////
640
bsalomon@google.com27847de2011-02-22 20:59:41 +0000641/* create a triangle strip that strokes the specified triangle. There are 8
642 unique vertices, but we repreat the last 2 to close up. Alternatively we
643 could use an indices array, and then only send 8 verts, but not sure that
644 would be faster.
645 */
bsalomon@google.com205d4602011-04-25 12:43:45 +0000646static void setStrokeRectStrip(GrPoint verts[10], GrRect rect,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000647 GrScalar width) {
648 const GrScalar rad = GrScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000649 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000650
651 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
652 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
653 verts[2].set(rect.fRight - rad, rect.fTop + rad);
654 verts[3].set(rect.fRight + rad, rect.fTop - rad);
655 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
656 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
657 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
658 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
659 verts[8] = verts[0];
660 verts[9] = verts[1];
661}
662
reed@google.com20efde72011-05-09 17:00:02 +0000663/**
664 * Returns true if the rects edges are integer-aligned.
665 */
666static bool isIRect(const GrRect& r) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000667 return GrScalarIsInt(r.fLeft) && GrScalarIsInt(r.fTop) &&
reed@google.com20efde72011-05-09 17:00:02 +0000668 GrScalarIsInt(r.fRight) && GrScalarIsInt(r.fBottom);
669}
670
bsalomon@google.com205d4602011-04-25 12:43:45 +0000671static bool apply_aa_to_rect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000672 const GrRect& rect,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000673 GrScalar width,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000674 const GrMatrix* matrix,
675 GrMatrix* combinedMatrix,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000676 GrRect* devRect,
677 bool* useVertexCoverage) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000678 // we use a simple coverage ramp to do aa on axis-aligned rects
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000679 // we check if the rect will be axis-aligned, and the rect won't land on
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000680 // integer coords.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000681
bsalomon@google.coma3108262011-10-10 14:08:47 +0000682 // we are keeping around the "tweak the alpha" trick because
683 // it is our only hope for the fixed-pipe implementation.
684 // In a shader implementation we can give a separate coverage input
bsalomon@google.com289533a2011-10-27 12:34:25 +0000685 // TODO: remove this ugliness when we drop the fixed-pipe impl
bsalomon@google.coma3108262011-10-10 14:08:47 +0000686 *useVertexCoverage = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000687 if (!target->canTweakAlphaForCoverage()) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000688 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +0000689#if GR_DEBUG
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000690 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000691#endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000692 return false;
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000693 } else {
694 *useVertexCoverage = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000695 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000696 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000697 const GrDrawState& drawState = target->getDrawState();
698 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000699 return false;
700 }
701
bsalomon@google.com471d4712011-08-23 15:45:25 +0000702 if (0 == width && target->willUseHWAALines()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000703 return false;
704 }
705
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000706 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000707 return false;
708 }
709
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000710 if (NULL != matrix &&
bsalomon@google.com205d4602011-04-25 12:43:45 +0000711 !matrix->preservesAxisAlignment()) {
712 return false;
713 }
714
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000715 *combinedMatrix = drawState.getViewMatrix();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000716 if (NULL != matrix) {
717 combinedMatrix->preConcat(*matrix);
718 GrAssert(combinedMatrix->preservesAxisAlignment());
719 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000720
bsalomon@google.com205d4602011-04-25 12:43:45 +0000721 combinedMatrix->mapRect(devRect, rect);
722 devRect->sort();
723
724 if (width < 0) {
reed@google.com20efde72011-05-09 17:00:02 +0000725 return !isIRect(*devRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000726 } else {
727 return true;
728 }
729}
730
bsalomon@google.com27847de2011-02-22 20:59:41 +0000731void GrContext::drawRect(const GrPaint& paint,
732 const GrRect& rect,
733 GrScalar width,
734 const GrMatrix* matrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000735 SK_TRACE_EVENT0("GrContext::drawRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000736
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000737 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000738 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000739
bsalomon@google.com205d4602011-04-25 12:43:45 +0000740 GrRect devRect = rect;
741 GrMatrix combinedMatrix;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000742 bool useVertexCoverage;
bsalomon@google.com289533a2011-10-27 12:34:25 +0000743 bool needAA = paint.fAntiAlias &&
744 !this->getRenderTarget()->isMultisampled();
745 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix,
746 &combinedMatrix, &devRect,
747 &useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000748
749 if (doAA) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000750 GrDrawTarget::AutoDeviceCoordDraw adcd(target);
751 if (!adcd.succeeded()) {
752 return;
753 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000754 if (width >= 0) {
755 GrVec strokeSize;;
756 if (width > 0) {
757 strokeSize.set(width, width);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000758 combinedMatrix.mapVectors(&strokeSize, 1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000759 strokeSize.setAbs(strokeSize);
760 } else {
761 strokeSize.set(GR_Scalar1, GR_Scalar1);
762 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000763 fAARectRenderer->strokeAARect(this->getGpu(), target, devRect,
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000764 strokeSize, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000765 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000766 fAARectRenderer->fillAARect(this->getGpu(), target,
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000767 devRect, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000768 }
769 return;
770 }
771
bsalomon@google.com27847de2011-02-22 20:59:41 +0000772 if (width >= 0) {
773 // TODO: consider making static vertex buffers for these cases.
774 // Hairline could be done by just adding closing vertex to
775 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000776
bsalomon@google.com27847de2011-02-22 20:59:41 +0000777 static const int worstCaseVertCount = 10;
bsalomon@google.come3d32162012-07-20 13:37:06 +0000778 GrDrawTarget::AutoReleaseGeometry geo(target, 0, worstCaseVertCount, 0);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000779
780 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000781 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000782 return;
783 }
784
785 GrPrimitiveType primType;
786 int vertCount;
787 GrPoint* vertex = geo.positions();
788
789 if (width > 0) {
790 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000791 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000792 setStrokeRectStrip(vertex, rect, width);
793 } else {
794 // hairline
795 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000796 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000797 vertex[0].set(rect.fLeft, rect.fTop);
798 vertex[1].set(rect.fRight, rect.fTop);
799 vertex[2].set(rect.fRight, rect.fBottom);
800 vertex[3].set(rect.fLeft, rect.fBottom);
801 vertex[4].set(rect.fLeft, rect.fTop);
802 }
803
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000804 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000805 if (NULL != matrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000806 GrDrawState* drawState = target->drawState();
807 avmr.set(drawState);
808 drawState->preConcatViewMatrix(*matrix);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000809 drawState->preConcatSamplerMatrices(*matrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000810 }
811
812 target->drawNonIndexed(primType, 0, vertCount);
813 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000814#if GR_STATIC_RECT_VB
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000815 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
816 if (NULL == sqVB) {
817 GrPrintf("Failed to create static rect vb.\n");
818 return;
819 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000820 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000821 GrDrawState* drawState = target->drawState();
822 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000823 GrMatrix m;
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000824 m.setAll(rect.width(), 0, rect.fLeft,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000825 0, rect.height(), rect.fTop,
826 0, 0, GrMatrix::I()[8]);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000827
828 if (NULL != matrix) {
829 m.postConcat(*matrix);
830 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000831 drawState->preConcatViewMatrix(m);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000832 drawState->preConcatSamplerMatrices(m);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000833
bsalomon@google.com47059542012-06-06 20:51:20 +0000834 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000835#else
bsalomon@google.come3d32162012-07-20 13:37:06 +0000836 target->drawSimpleRect(rect, matrix);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000837#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000838 }
839}
840
841void GrContext::drawRectToRect(const GrPaint& paint,
842 const GrRect& dstRect,
843 const GrRect& srcRect,
844 const GrMatrix* dstMatrix,
845 const GrMatrix* srcMatrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000846 SK_TRACE_EVENT0("GrContext::drawRectToRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000847
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000848 // srcRect refers to paint's first texture
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000849 if (!paint.isTextureStageEnabled(0)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000850 drawRect(paint, dstRect, -1, dstMatrix);
851 return;
852 }
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000853
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000854 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000855
856#if GR_STATIC_RECT_VB
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000857 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000858 GrDrawState* drawState = target->drawState();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000859 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000860
861 GrMatrix m;
862
863 m.setAll(dstRect.width(), 0, dstRect.fLeft,
864 0, dstRect.height(), dstRect.fTop,
865 0, 0, GrMatrix::I()[8]);
866 if (NULL != dstMatrix) {
867 m.postConcat(*dstMatrix);
868 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000869 drawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000870
bsalomon@google.come3d32162012-07-20 13:37:06 +0000871 // we explicitly setup the correct coords for the first stage. The others
872 // must know about the view matrix change.
873 for (int s = 1; s < GrPaint::kTotalStages; ++s) {
874 if (drawState->isStageEnabled(s)) {
875 drawState->sampler(s)->preConcatMatrix(m);
876 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000877 }
878
bsalomon@google.com27847de2011-02-22 20:59:41 +0000879 m.setAll(srcRect.width(), 0, srcRect.fLeft,
880 0, srcRect.height(), srcRect.fTop,
881 0, 0, GrMatrix::I()[8]);
882 if (NULL != srcMatrix) {
883 m.postConcat(*srcMatrix);
884 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000885 drawState->sampler(GrPaint::kFirstTextureStage)->preConcatMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000886
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000887 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
888 if (NULL == sqVB) {
889 GrPrintf("Failed to create static rect vb.\n");
890 return;
891 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000892 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com47059542012-06-06 20:51:20 +0000893 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000894#else
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000895 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000896
tomhudson@google.com93813632011-10-27 20:21:16 +0000897 const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
898 const GrMatrix* srcMatrices[GrDrawState::kNumStages] = {NULL};
bsalomon@google.com27847de2011-02-22 20:59:41 +0000899 srcRects[0] = &srcRect;
900 srcMatrices[0] = srcMatrix;
901
bsalomon@google.come3d32162012-07-20 13:37:06 +0000902 target->drawRect(dstRect, dstMatrix, srcRects, srcMatrices);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000903#endif
904}
905
906void GrContext::drawVertices(const GrPaint& paint,
907 GrPrimitiveType primitiveType,
908 int vertexCount,
909 const GrPoint positions[],
910 const GrPoint texCoords[],
911 const GrColor colors[],
912 const uint16_t indices[],
913 int indexCount) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000914 SK_TRACE_EVENT0("GrContext::drawVertices");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000915
916 GrDrawTarget::AutoReleaseGeometry geo;
917
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000918 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000919 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000920
bsalomon@google.come3d32162012-07-20 13:37:06 +0000921 GrVertexLayout layout = 0;
922 if (NULL != texCoords) {
923 layout |= GrDrawTarget::StageTexCoordVertexLayoutBit(0, 0);
924 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000925 if (NULL != colors) {
926 layout |= GrDrawTarget::kColor_VertexLayoutBit;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000927 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000928 int vertexSize = GrDrawTarget::VertexSize(layout);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000929
930 if (sizeof(GrPoint) != vertexSize) {
931 if (!geo.set(target, layout, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000932 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000933 return;
934 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000935 int texOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000936 int colorOffset;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000937 GrDrawTarget::VertexSizeAndOffsetsByIdx(layout,
938 texOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000939 &colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000940 NULL,
941 NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000942 void* curVertex = geo.vertices();
943
944 for (int i = 0; i < vertexCount; ++i) {
945 *((GrPoint*)curVertex) = positions[i];
946
947 if (texOffsets[0] > 0) {
948 *(GrPoint*)((intptr_t)curVertex + texOffsets[0]) = texCoords[i];
949 }
950 if (colorOffset > 0) {
951 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
952 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000953 curVertex = (void*)((intptr_t)curVertex + vertexSize);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000954 }
955 } else {
956 target->setVertexSourceToArray(layout, positions, vertexCount);
957 }
958
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000959 // we don't currently apply offscreen AA to this path. Need improved
bsalomon@google.com91958362011-06-13 17:58:13 +0000960 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000961
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000962 if (NULL != indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000963 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000964 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000965 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000966 target->drawNonIndexed(primitiveType, 0, vertexCount);
967 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000968}
969
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000970///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com150d2842012-01-12 20:19:56 +0000971namespace {
972
bsalomon@google.com93c96602012-04-27 13:05:21 +0000973struct CircleVertex {
974 GrPoint fPos;
975 GrPoint fCenter;
976 GrScalar fOuterRadius;
977 GrScalar fInnerRadius;
978};
979
980/* Returns true if will map a circle to another circle. This can be true
981 * if the matrix only includes square-scale, rotation, translation.
982 */
983inline bool isSimilarityTransformation(const SkMatrix& matrix,
984 SkScalar tol = SK_ScalarNearlyZero) {
985 if (matrix.isIdentity() || matrix.getType() == SkMatrix::kTranslate_Mask) {
986 return true;
987 }
988 if (matrix.hasPerspective()) {
989 return false;
990 }
991
992 SkScalar mx = matrix.get(SkMatrix::kMScaleX);
993 SkScalar sx = matrix.get(SkMatrix::kMSkewX);
994 SkScalar my = matrix.get(SkMatrix::kMScaleY);
995 SkScalar sy = matrix.get(SkMatrix::kMSkewY);
996
997 if (mx == 0 && sx == 0 && my == 0 && sy == 0) {
998 return false;
999 }
1000
1001 // it has scales or skews, but it could also be rotation, check it out.
1002 SkVector vec[2];
1003 vec[0].set(mx, sx);
1004 vec[1].set(sy, my);
1005
1006 return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) &&
1007 SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(),
1008 SkScalarSquare(tol));
1009}
1010
1011}
1012
1013// TODO: strokeWidth can't be larger than zero right now.
1014// It will be fixed when drawPath() can handle strokes.
1015void GrContext::drawOval(const GrPaint& paint,
1016 const GrRect& rect,
1017 SkScalar strokeWidth) {
bsalomon@google.com0982d352012-07-31 15:33:25 +00001018 GrAssert(strokeWidth <= 0);
1019 if (!isSimilarityTransformation(this->getMatrix()) ||
bsalomon@google.com93c96602012-04-27 13:05:21 +00001020 !paint.fAntiAlias ||
1021 rect.height() != rect.width()) {
1022 SkPath path;
1023 path.addOval(rect);
1024 GrPathFill fill = (strokeWidth == 0) ?
bsalomon@google.com0982d352012-07-31 15:33:25 +00001025 kHairLine_GrPathFill : kWinding_GrPathFill;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001026 this->internalDrawPath(paint, path, fill, NULL);
1027 return;
1028 }
1029
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001030 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
1031
bsalomon@google.com0982d352012-07-31 15:33:25 +00001032 GrDrawState* drawState = target->drawState();
1033 GrDrawState::AutoStageDisable atr(fDrawState);
1034 const GrMatrix vm = drawState->getViewMatrix();
1035
bsalomon@google.com93c96602012-04-27 13:05:21 +00001036 const GrRenderTarget* rt = drawState->getRenderTarget();
1037 if (NULL == rt) {
1038 return;
1039 }
1040
bsalomon@google.come3d32162012-07-20 13:37:06 +00001041 GrDrawTarget::AutoDeviceCoordDraw adcd(target);
1042 if (!adcd.succeeded()) {
1043 return;
1044 }
bsalomon@google.com93c96602012-04-27 13:05:21 +00001045
bsalomon@google.come3d32162012-07-20 13:37:06 +00001046 GrVertexLayout layout = GrDrawTarget::kEdge_VertexLayoutBit;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001047 GrAssert(sizeof(CircleVertex) == GrDrawTarget::VertexSize(layout));
1048
1049 GrPoint center = GrPoint::Make(rect.centerX(), rect.centerY());
1050 GrScalar radius = SkScalarHalf(rect.width());
1051
1052 vm.mapPoints(&center, 1);
1053 radius = vm.mapRadius(radius);
1054
1055 GrScalar outerRadius = radius;
1056 GrScalar innerRadius = 0;
1057 SkScalar halfWidth = 0;
1058 if (strokeWidth == 0) {
1059 halfWidth = SkScalarHalf(SK_Scalar1);
1060
1061 outerRadius += halfWidth;
1062 innerRadius = SkMaxScalar(0, radius - halfWidth);
1063 }
1064
1065 GrDrawTarget::AutoReleaseGeometry geo(target, layout, 4, 0);
1066 if (!geo.succeeded()) {
1067 GrPrintf("Failed to get space for vertices!\n");
1068 return;
1069 }
1070
1071 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
1072
robertphillips@google.coma0a66c12012-06-22 13:14:29 +00001073 // The fragment shader will extend the radius out half a pixel
1074 // to antialias. Expand the drawn rect here so all the pixels
1075 // will be captured.
1076 SkScalar L = center.fX - outerRadius - SkFloatToScalar(0.5f);
1077 SkScalar R = center.fX + outerRadius + SkFloatToScalar(0.5f);
1078 SkScalar T = center.fY - outerRadius - SkFloatToScalar(0.5f);
1079 SkScalar B = center.fY + outerRadius + SkFloatToScalar(0.5f);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001080
1081 verts[0].fPos = SkPoint::Make(L, T);
1082 verts[1].fPos = SkPoint::Make(R, T);
1083 verts[2].fPos = SkPoint::Make(L, B);
1084 verts[3].fPos = SkPoint::Make(R, B);
1085
1086 for (int i = 0; i < 4; ++i) {
1087 // this goes to fragment shader, it should be in y-points-up space.
1088 verts[i].fCenter = SkPoint::Make(center.fX, rt->height() - center.fY);
1089
1090 verts[i].fOuterRadius = outerRadius;
1091 verts[i].fInnerRadius = innerRadius;
1092 }
1093
1094 drawState->setVertexEdgeType(GrDrawState::kCircle_EdgeType);
bsalomon@google.com47059542012-06-06 20:51:20 +00001095 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001096}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001097
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001098void GrContext::drawPath(const GrPaint& paint, const SkPath& path,
reed@google.com07f3ee12011-05-16 17:21:57 +00001099 GrPathFill fill, const GrPoint* translate) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001100
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001101 if (path.isEmpty()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001102 if (GrIsFillInverted(fill)) {
1103 this->drawPaint(paint);
1104 }
1105 return;
1106 }
1107
bsalomon@google.com93c96602012-04-27 13:05:21 +00001108 SkRect ovalRect;
1109 if (!GrIsFillInverted(fill) && path.isOval(&ovalRect)) {
1110 if (translate) {
1111 ovalRect.offset(*translate);
1112 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001113 SkScalar width = (fill == kHairLine_GrPathFill) ? 0 : -SK_Scalar1;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001114 this->drawOval(paint, ovalRect, width);
1115 return;
1116 }
1117
1118 internalDrawPath(paint, path, fill, translate);
1119}
1120
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001121void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +00001122 GrPathFill fill, const GrPoint* translate) {
1123
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001124 // Note that below we may sw-rasterize the path into a scratch texture.
1125 // Scratch textures can be recycled after they are returned to the texture
1126 // cache. This presents a potential hazard for buffered drawing. However,
1127 // the writePixels that uploads to the scratch will perform a flush so we're
1128 // OK.
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001129 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +00001130 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001131
bsalomon@google.com289533a2011-10-27 12:34:25 +00001132 bool prAA = paint.fAntiAlias && !this->getRenderTarget()->isMultisampled();
1133
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001134 // An Assumption here is that path renderer would use some form of tweaking
1135 // the src color (either the input alpha or in the frag shader) to implement
1136 // aa. If we have some future driver-mojo path AA that can do the right
1137 // thing WRT to the blend then we'll need some query on the PR.
1138 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001139#if GR_DEBUG
bsalomon@google.com979432b2011-11-05 21:38:22 +00001140 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001141#endif
bsalomon@google.com289533a2011-10-27 12:34:25 +00001142 prAA = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001143 }
bsalomon@google.com289533a2011-10-27 12:34:25 +00001144
robertphillips@google.com72176b22012-05-23 13:19:12 +00001145 GrPathRenderer* pr = this->getPathRenderer(path, fill, target, prAA, true);
bsalomon@google.com30085192011-08-19 15:42:31 +00001146 if (NULL == pr) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001147#if GR_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001148 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001149#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001150 return;
1151 }
1152
bsalomon@google.come3d32162012-07-20 13:37:06 +00001153 pr->drawPath(path, fill, translate, target, prAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001154}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001155
bsalomon@google.com27847de2011-02-22 20:59:41 +00001156////////////////////////////////////////////////////////////////////////////////
1157
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001158void GrContext::flush(int flagsBitfield) {
1159 if (kDiscard_FlushBit & flagsBitfield) {
1160 fDrawBuffer->reset();
1161 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001162 this->flushDrawBuffer();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001163 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001164 if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001165 fGpu->forceRenderTargetFlush();
1166 }
1167}
1168
bsalomon@google.com27847de2011-02-22 20:59:41 +00001169void GrContext::flushDrawBuffer() {
junov@google.com53a55842011-06-08 22:55:10 +00001170 if (fDrawBuffer) {
robertphillips@google.com58b38182012-05-03 16:29:41 +00001171 // With addition of the AA clip path, flushing the draw buffer can
1172 // result in the generation of an AA clip mask. During this
1173 // process the SW path renderer may be invoked which recusively
1174 // calls this method (via internalWriteTexturePixels) creating
1175 // infinite recursion
1176 GrInOrderDrawBuffer* temp = fDrawBuffer;
1177 fDrawBuffer = NULL;
1178
1179 temp->flushTo(fGpu);
1180
1181 fDrawBuffer = temp;
junov@google.com53a55842011-06-08 22:55:10 +00001182 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001183}
1184
bsalomon@google.com0342a852012-08-20 19:22:38 +00001185void GrContext::writeTexturePixels(GrTexture* texture,
1186 int left, int top, int width, int height,
1187 GrPixelConfig config, const void* buffer, size_t rowBytes,
1188 uint32_t flags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001189 SK_TRACE_EVENT0("GrContext::writeTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001190 ASSERT_OWNED_RESOURCE(texture);
1191
bsalomon@google.com0342a852012-08-20 19:22:38 +00001192 // TODO: use scratch texture to perform conversion
1193 if (kUnpremul_PixelOpsFlag & flags) {
1194 return;
1195 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001196 if (!(kDontFlush_PixelOpsFlag & flags)) {
1197 this->flush();
1198 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001199
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001200 fGpu->writeTexturePixels(texture, left, top, width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +00001201 config, buffer, rowBytes);
1202}
1203
bsalomon@google.com0342a852012-08-20 19:22:38 +00001204bool GrContext::readTexturePixels(GrTexture* texture,
1205 int left, int top, int width, int height,
1206 GrPixelConfig config, void* buffer, size_t rowBytes,
1207 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001208 SK_TRACE_EVENT0("GrContext::readTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001209 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001210
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001211 // TODO: code read pixels for textures that aren't also rendertargets
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001212 GrRenderTarget* target = texture->asRenderTarget();
1213 if (NULL != target) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001214 return this->readRenderTargetPixels(target,
1215 left, top, width, height,
1216 config, buffer, rowBytes,
1217 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001218 } else {
1219 return false;
1220 }
1221}
1222
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001223#include "SkConfig8888.h"
1224
1225namespace {
1226/**
1227 * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel
1228 * formats are representable as Config8888 and so the function returns false
1229 * if the GrPixelConfig has no equivalent Config8888.
1230 */
1231bool grconfig_to_config8888(GrPixelConfig config,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001232 bool unpremul,
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001233 SkCanvas::Config8888* config8888) {
1234 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001235 case kRGBA_8888_GrPixelConfig:
1236 if (unpremul) {
1237 *config8888 = SkCanvas::kRGBA_Unpremul_Config8888;
1238 } else {
1239 *config8888 = SkCanvas::kRGBA_Premul_Config8888;
1240 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001241 return true;
bsalomon@google.com0342a852012-08-20 19:22:38 +00001242 case kBGRA_8888_GrPixelConfig:
1243 if (unpremul) {
1244 *config8888 = SkCanvas::kBGRA_Unpremul_Config8888;
1245 } else {
1246 *config8888 = SkCanvas::kBGRA_Premul_Config8888;
1247 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001248 return true;
1249 default:
1250 return false;
1251 }
1252}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001253
1254// It returns a configuration with where the byte position of the R & B components are swapped in
1255// relation to the input config. This should only be called with the result of
1256// grconfig_to_config8888 as it will fail for other configs.
1257SkCanvas::Config8888 swap_config8888_red_and_blue(SkCanvas::Config8888 config8888) {
1258 switch (config8888) {
1259 case SkCanvas::kBGRA_Premul_Config8888:
1260 return SkCanvas::kRGBA_Premul_Config8888;
1261 case SkCanvas::kBGRA_Unpremul_Config8888:
1262 return SkCanvas::kRGBA_Unpremul_Config8888;
1263 case SkCanvas::kRGBA_Premul_Config8888:
1264 return SkCanvas::kBGRA_Premul_Config8888;
1265 case SkCanvas::kRGBA_Unpremul_Config8888:
1266 return SkCanvas::kBGRA_Unpremul_Config8888;
1267 default:
1268 GrCrash("Unexpected input");
1269 return SkCanvas::kBGRA_Unpremul_Config8888;;
1270 }
1271}
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001272}
1273
bsalomon@google.com0342a852012-08-20 19:22:38 +00001274bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
1275 int left, int top, int width, int height,
1276 GrPixelConfig config, void* buffer, size_t rowBytes,
1277 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001278 SK_TRACE_EVENT0("GrContext::readRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001279 ASSERT_OWNED_RESOURCE(target);
1280
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001281 if (NULL == target) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001282 target = fDrawState->getRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001283 if (NULL == target) {
1284 return false;
1285 }
1286 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001287
bsalomon@google.com6f379512011-11-16 20:36:03 +00001288 if (!(kDontFlush_PixelOpsFlag & flags)) {
1289 this->flush();
1290 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001291
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001292 // Determine which conversions have to be applied: flipY, swapRAnd, and/or unpremul.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001293
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001294 // If fGpu->readPixels would incur a y-flip cost then we will read the pixels upside down. We'll
1295 // either do the flipY by drawing into a scratch with a matrix or on the cpu after the read.
1296 bool flipY = fGpu->readPixelsWillPayForYFlip(target, left, top,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001297 width, height, config,
1298 rowBytes);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001299 bool swapRAndB = fGpu->preferredReadPixelsConfig(config) == GrPixelConfigSwapRAndB(config);
1300
bsalomon@google.com0342a852012-08-20 19:22:38 +00001301 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001302
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001303 // flipY will get set to false when it is handled below using a scratch. However, in that case
1304 // we still want to do the read upside down.
1305 bool readUpsideDown = flipY;
1306
1307 if (unpremul && kRGBA_8888_GrPixelConfig != config && kBGRA_8888_GrPixelConfig != config) {
1308 // The unpremul flag is only allowed for these two configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001309 return false;
1310 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001311
1312 GrPixelConfig readConfig;
1313 if (swapRAndB) {
1314 readConfig = GrPixelConfigSwapRAndB(config);
1315 GrAssert(kUnknown_GrPixelConfig != config);
1316 } else {
1317 readConfig = config;
1318 }
1319
1320 // If the src is a texture and we would have to do conversions after read pixels, we instead
1321 // do the conversions by drawing the src to a scratch texture. If we handle any of the
1322 // conversions in the draw we set the corresponding bool to false so that we don't reapply it
1323 // on the read back pixels.
1324 GrTexture* src = target->asTexture();
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001325 GrAutoScratchTexture ast;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001326 if (NULL != src && (swapRAndB || unpremul || flipY)) {
1327 // Make the scratch a render target because we don't have a robust readTexturePixels as of
1328 // yet. It calls this function.
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001329 GrTextureDesc desc;
1330 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1331 desc.fWidth = width;
1332 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001333 desc.fConfig = readConfig;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001334
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001335 // When a full readback is faster than a partial we could always make the scratch exactly
1336 // match the passed rect. However, if we see many different size rectangles we will trash
1337 // our texture cache and pay the cost of creating and destroying many textures. So, we only
1338 // request an exact match when the caller is reading an entire RT.
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001339 ScratchTexMatch match = kApprox_ScratchTexMatch;
1340 if (0 == left &&
1341 0 == top &&
1342 target->width() == width &&
1343 target->height() == height &&
1344 fGpu->fullReadPixelsIsFasterThanPartial()) {
1345 match = kExact_ScratchTexMatch;
1346 }
1347 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001348 GrTexture* texture = ast.texture();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001349 if (texture) {
1350 SkAutoTUnref<GrCustomStage> stage;
1351 if (unpremul) {
1352 stage.reset(this->createPMToUPMEffect(src, swapRAndB));
1353 }
1354 // If we failed to create a PM->UPM effect and have no other conversions to perform then
1355 // there is no longer any point to using the scratch.
1356 if (NULL != stage || flipY || swapRAndB) {
1357 if (NULL == stage) {
1358 stage.reset(GrConfigConversionEffect::Create(src, swapRAndB));
1359 GrAssert(NULL != stage);
1360 } else {
1361 unpremul = false; // we will handle the UPM conversion in the draw
1362 }
1363 swapRAndB = false; // we will handle the swap in the draw.
bsalomon@google.comc4364992011-11-07 15:54:49 +00001364
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001365 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
1366 GrDrawState* drawState = fGpu->drawState();
1367 drawState->setRenderTarget(texture->asRenderTarget());
1368 GrMatrix matrix;
1369 if (flipY) {
1370 matrix.setTranslate(SK_Scalar1 * left,
1371 SK_Scalar1 * (top + height));
1372 matrix.set(GrMatrix::kMScaleY, -GR_Scalar1);
1373 flipY = false; // the y flip will be handled in the draw
1374 } else {
1375 matrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
1376 }
1377 matrix.postIDiv(src->width(), src->height());
1378 drawState->sampler(0)->reset(matrix);
1379 drawState->sampler(0)->setCustomStage(stage);
1380 GrRect rect = GrRect::MakeWH(GrIntToScalar(width), GrIntToScalar(height));
1381 fGpu->drawSimpleRect(rect, NULL);
1382 // we want to read back from the scratch's origin
1383 left = 0;
1384 top = 0;
1385 target = texture->asRenderTarget();
1386 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001387 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001388 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001389 if (!fGpu->readPixels(target,
1390 left, top, width, height,
1391 readConfig, buffer, rowBytes, readUpsideDown)) {
1392 return false;
1393 }
1394 // Perform any conversions we weren't able to perfom using a scratch texture.
1395 if (unpremul || swapRAndB || flipY) {
bsalomon@google.comccaa0022012-09-25 19:55:07 +00001396 // These are initialized to suppress a warning
1397 SkCanvas::Config8888 srcC8888 = SkCanvas::kNative_Premul_Config8888;
1398 SkCanvas::Config8888 dstC8888 = SkCanvas::kNative_Premul_Config8888;
1399
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001400 bool c8888IsValid = grconfig_to_config8888(config, false, &srcC8888);
1401 grconfig_to_config8888(config, unpremul, &dstC8888);
bsalomon@google.comccaa0022012-09-25 19:55:07 +00001402
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001403 if (swapRAndB) {
1404 GrAssert(c8888IsValid); // we should only do r/b swap on 8888 configs
1405 srcC8888 = swap_config8888_red_and_blue(srcC8888);
1406 }
1407 if (flipY) {
1408 size_t tightRB = width * GrBytesPerPixel(config);
1409 if (0 == rowBytes) {
1410 rowBytes = tightRB;
1411 }
1412 SkAutoSTMalloc<256, uint8_t> tempRow(tightRB);
1413 intptr_t top = reinterpret_cast<intptr_t>(buffer);
1414 intptr_t bot = top + (height - 1) * rowBytes;
1415 while (top < bot) {
1416 uint32_t* t = reinterpret_cast<uint32_t*>(top);
1417 uint32_t* b = reinterpret_cast<uint32_t*>(bot);
1418 uint32_t* temp = reinterpret_cast<uint32_t*>(tempRow.get());
1419 memcpy(temp, t, tightRB);
1420 if (c8888IsValid) {
1421 SkConvertConfig8888Pixels(t, tightRB, dstC8888,
1422 b, tightRB, srcC8888,
1423 width, 1);
1424 SkConvertConfig8888Pixels(b, tightRB, dstC8888,
1425 temp, tightRB, srcC8888,
1426 width, 1);
1427 } else {
1428 memcpy(t, b, tightRB);
1429 memcpy(b, temp, tightRB);
1430 }
1431 top += rowBytes;
1432 bot -= rowBytes;
1433 }
1434 // The above loop does nothing on the middle row when height is odd.
1435 if (top == bot && c8888IsValid && dstC8888 != srcC8888) {
1436 uint32_t* mid = reinterpret_cast<uint32_t*>(top);
1437 SkConvertConfig8888Pixels(mid, tightRB, dstC8888, mid, tightRB, srcC8888, width, 1);
1438 }
1439 } else {
1440 // if we aren't flipping Y then we have no reason to be here other than doing
1441 // conversions for 8888 (r/b swap or upm).
1442 GrAssert(c8888IsValid);
1443 uint32_t* b32 = reinterpret_cast<uint32_t*>(buffer);
1444 SkConvertConfig8888Pixels(b32, rowBytes, dstC8888,
1445 b32, rowBytes, srcC8888,
1446 width, height);
1447 }
1448 }
1449 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001450}
1451
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001452void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1453 GrAssert(target);
1454 ASSERT_OWNED_RESOURCE(target);
1455 // In the future we may track whether there are any pending draws to this
1456 // target. We don't today so we always perform a flush. We don't promise
1457 // this to our clients, though.
1458 this->flush();
1459 fGpu->resolveRenderTarget(target);
1460}
1461
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001462void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst) {
1463 if (NULL == src || NULL == dst) {
1464 return;
1465 }
1466 ASSERT_OWNED_RESOURCE(src);
1467
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001468 // Writes pending to the source texture are not tracked, so a flush
1469 // is required to ensure that the copy captures the most recent contents
1470 // of the source texture. See similar behaviour in
1471 // GrContext::resolveRenderTarget.
1472 this->flush();
1473
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001474 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001475 GrDrawState* drawState = fGpu->drawState();
1476 drawState->setRenderTarget(dst);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001477 GrMatrix sampleM;
1478 sampleM.setIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001479 drawState->sampler(0)->reset(sampleM);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001480 drawState->createTextureEffect(0, src);
bsalomon@google.com5db3b6c2012-01-12 20:38:57 +00001481 SkRect rect = SkRect::MakeXYWH(0, 0,
1482 SK_Scalar1 * src->width(),
1483 SK_Scalar1 * src->height());
bsalomon@google.come3d32162012-07-20 13:37:06 +00001484 fGpu->drawSimpleRect(rect, NULL);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001485}
1486
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001487void GrContext::writeRenderTargetPixels(GrRenderTarget* target,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001488 int left, int top, int width, int height,
1489 GrPixelConfig config,
1490 const void* buffer,
1491 size_t rowBytes,
1492 uint32_t flags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001493 SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001494 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001495
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001496 if (NULL == target) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001497 target = fDrawState->getRenderTarget();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001498 if (NULL == target) {
1499 return;
1500 }
1501 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001502
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001503 // TODO: when underlying api has a direct way to do this we should use it (e.g. glDrawPixels on
1504 // desktop GL).
1505
1506 // We will always call some form of writeTexturePixels and we will pass our flags on to it.
1507 // Thus, we don't perform a flush here since that call will do it (if the kNoFlush flag isn't
1508 // set.)
bsalomon@google.com27847de2011-02-22 20:59:41 +00001509
bsalomon@google.com0342a852012-08-20 19:22:38 +00001510 // If the RT is also a texture and we don't have to premultiply then take the texture path.
1511 // We expect to be at least as fast or faster since it doesn't use an intermediate texture as
1512 // we do below.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001513
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001514#if !GR_MAC_BUILD
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001515 // At least some drivers on the Mac get confused when glTexImage2D is called on a texture
1516 // attached to an FBO. The FBO still sees the old image. TODO: determine what OS versions and/or
1517 // HW is affected.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001518 if (NULL != target->asTexture() && !(kUnpremul_PixelOpsFlag & flags)) {
1519 this->writeTexturePixels(target->asTexture(),
1520 left, top, width, height,
1521 config, buffer, rowBytes, flags);
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001522 return;
1523 }
1524#endif
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001525 SkAutoTUnref<GrCustomStage> stage;
1526 bool swapRAndB = (fGpu->preferredReadPixelsConfig(config) == GrPixelConfigSwapRAndB(config));
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001527
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001528 GrPixelConfig textureConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001529 if (swapRAndB) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001530 textureConfig = GrPixelConfigSwapRAndB(config);
1531 } else {
1532 textureConfig = config;
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001533 }
1534
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001535 GrTextureDesc desc;
1536 desc.fWidth = width;
1537 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001538 desc.fConfig = textureConfig;
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001539 GrAutoScratchTexture ast(this, desc);
1540 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001541 if (NULL == texture) {
1542 return;
1543 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001544 // allocate a tmp buffer and sw convert the pixels to premul
1545 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
1546
1547 if (kUnpremul_PixelOpsFlag & flags) {
1548 if (kRGBA_8888_GrPixelConfig != config && kBGRA_8888_GrPixelConfig != config) {
1549 return;
1550 }
1551 stage.reset(this->createUPMToPMEffect(texture, swapRAndB));
1552 if (NULL == stage) {
1553 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1554 GR_DEBUGCODE(bool success = )
1555 grconfig_to_config8888(config, true, &srcConfig8888);
1556 GrAssert(success);
1557 GR_DEBUGCODE(success = )
1558 grconfig_to_config8888(config, false, &dstConfig8888);
1559 GrAssert(success);
1560 const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer);
1561 tmpPixels.reset(width * height);
1562 SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888,
1563 src, rowBytes, srcConfig8888,
1564 width, height);
1565 buffer = tmpPixels.get();
1566 rowBytes = 4 * width;
1567 }
1568 }
1569 if (NULL == stage) {
1570 stage.reset(GrConfigConversionEffect::Create(texture, swapRAndB));
1571 GrAssert(NULL != stage);
1572 }
1573
1574 this->writeTexturePixels(texture,
1575 0, 0, width, height,
1576 textureConfig, buffer, rowBytes,
1577 flags & ~kUnpremul_PixelOpsFlag);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001578
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001579 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001580 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001581
1582 GrMatrix matrix;
1583 matrix.setTranslate(GrIntToScalar(left), GrIntToScalar(top));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001584 drawState->setViewMatrix(matrix);
1585 drawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001586
bsalomon@google.com5c638652011-07-18 19:31:59 +00001587 matrix.setIDiv(texture->width(), texture->height());
bsalomon@google.comb8670992012-07-25 21:27:09 +00001588 drawState->sampler(0)->reset(matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001589 drawState->sampler(0)->setCustomStage(stage);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001590
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001591 fGpu->drawSimpleRect(GrRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)), NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001592}
1593////////////////////////////////////////////////////////////////////////////////
1594
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001595void GrContext::setPaint(const GrPaint& paint) {
tomhudson@google.comcb325ce2012-07-11 14:41:19 +00001596 GrAssert(fDrawState->stagesDisabled());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001597
1598 for (int i = 0; i < GrPaint::kMaxTextures; ++i) {
1599 int s = i + GrPaint::kFirstTextureStage;
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001600 if (paint.isTextureStageEnabled(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001601 *fDrawState->sampler(s) = paint.getTextureSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001602 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001603 }
1604
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001605 fDrawState->setFirstCoverageStage(GrPaint::kFirstMaskStage);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001606
1607 for (int i = 0; i < GrPaint::kMaxMasks; ++i) {
1608 int s = i + GrPaint::kFirstMaskStage;
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001609 if (paint.isMaskStageEnabled(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001610 *fDrawState->sampler(s) = paint.getMaskSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001611 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001612 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001613
bsalomon@google.com26936d02012-03-19 13:06:19 +00001614 // disable all stages not accessible via the paint
1615 for (int s = GrPaint::kTotalStages; s < GrDrawState::kNumStages; ++s) {
tomhudson@google.com676e6602012-07-10 17:21:48 +00001616 fDrawState->disableStage(s);
bsalomon@google.com26936d02012-03-19 13:06:19 +00001617 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001618
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001619 fDrawState->setColor(paint.fColor);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001620
1621 if (paint.fDither) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001622 fDrawState->enableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001623 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001624 fDrawState->disableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001625 }
1626 if (paint.fAntiAlias) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001627 fDrawState->enableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001628 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001629 fDrawState->disableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001630 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001631 if (paint.fColorMatrixEnabled) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001632 fDrawState->enableState(GrDrawState::kColorMatrix_StateBit);
1633 fDrawState->setColorMatrix(paint.fColorMatrix);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001634 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001635 fDrawState->disableState(GrDrawState::kColorMatrix_StateBit);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001636 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001637 fDrawState->setBlendFunc(paint.fSrcBlendCoeff, paint.fDstBlendCoeff);
1638 fDrawState->setColorFilter(paint.fColorFilterColor, paint.fColorFilterXfermode);
1639 fDrawState->setCoverage(paint.fCoverage);
reed@google.com4b2d3f32012-05-15 18:05:50 +00001640#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
bsalomon@google.come3d32162012-07-20 13:37:06 +00001641 if ((paint.hasMask() || 0xff != paint.fCoverage) &&
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001642 !fGpu->canApplyCoverage()) {
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001643 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1644 }
bsalomon@google.com95cd7bd2012-03-28 15:35:05 +00001645#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001646}
1647
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001648GrDrawTarget* GrContext::prepareToDraw(const GrPaint* paint, BufferedDraw buffered) {
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001649 if (kNo_BufferedDraw == buffered && kYes_BufferedDraw == fLastDrawWasBuffered) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001650 this->flushDrawBuffer();
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001651 fLastDrawWasBuffered = kNo_BufferedDraw;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001652 }
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001653 if (NULL != paint) {
1654 this->setPaint(*paint);
1655 }
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001656 if (kYes_BufferedDraw == buffered) {
1657 fDrawBuffer->setClip(fGpu->getClip());
1658 fLastDrawWasBuffered = kYes_BufferedDraw;
1659 return fDrawBuffer;
1660 } else {
1661 GrAssert(kNo_BufferedDraw == buffered);
1662 return fGpu;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001663 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001664}
1665
robertphillips@google.com72176b22012-05-23 13:19:12 +00001666/*
1667 * This method finds a path renderer that can draw the specified path on
1668 * the provided target.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001669 * Due to its expense, the software path renderer has split out so it can
robertphillips@google.com72176b22012-05-23 13:19:12 +00001670 * can be individually allowed/disallowed via the "allowSW" boolean.
1671 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001672GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
bsalomon@google.com289533a2011-10-27 12:34:25 +00001673 GrPathFill fill,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001674 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001675 bool antiAlias,
1676 bool allowSW) {
bsalomon@google.com30085192011-08-19 15:42:31 +00001677 if (NULL == fPathRendererChain) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001678 fPathRendererChain =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001679 SkNEW_ARGS(GrPathRendererChain,
1680 (this, GrPathRendererChain::kNone_UsageFlag));
bsalomon@google.com30085192011-08-19 15:42:31 +00001681 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001682
1683 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path, fill,
1684 target,
1685 antiAlias);
1686
1687 if (NULL == pr && allowSW) {
1688 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001689 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001690 }
1691
1692 pr = fSoftwarePathRenderer;
1693 }
1694
1695 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001696}
1697
bsalomon@google.com27847de2011-02-22 20:59:41 +00001698////////////////////////////////////////////////////////////////////////////////
1699
bsalomon@google.com27847de2011-02-22 20:59:41 +00001700void GrContext::setRenderTarget(GrRenderTarget* target) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001701 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001702 fDrawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001703}
1704
1705GrRenderTarget* GrContext::getRenderTarget() {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001706 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001707}
1708
1709const GrRenderTarget* GrContext::getRenderTarget() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001710 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001711}
1712
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001713bool GrContext::isConfigRenderable(GrPixelConfig config) const {
1714 return fGpu->isConfigRenderable(config);
1715}
1716
bsalomon@google.com27847de2011-02-22 20:59:41 +00001717const GrMatrix& GrContext::getMatrix() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001718 return fDrawState->getViewMatrix();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001719}
1720
1721void GrContext::setMatrix(const GrMatrix& m) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001722 fDrawState->setViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001723}
1724
1725void GrContext::concatMatrix(const GrMatrix& m) const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001726 fDrawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001727}
1728
1729static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
1730 intptr_t mask = 1 << shift;
1731 if (pred) {
1732 bits |= mask;
1733 } else {
1734 bits &= ~mask;
1735 }
1736 return bits;
1737}
1738
bsalomon@google.com583a1e32011-08-17 13:42:46 +00001739GrContext::GrContext(GrGpu* gpu) {
bsalomon@google.comc0af3172012-06-15 14:10:09 +00001740 ++THREAD_INSTANCE_COUNT;
1741
bsalomon@google.com27847de2011-02-22 20:59:41 +00001742 fGpu = gpu;
1743 fGpu->ref();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001744 fGpu->setContext(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001745
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001746 fDrawState = SkNEW(GrDrawState);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001747 fGpu->setDrawState(fDrawState);
1748
bsalomon@google.com30085192011-08-19 15:42:31 +00001749 fPathRendererChain = NULL;
robertphillips@google.com72176b22012-05-23 13:19:12 +00001750 fSoftwarePathRenderer = NULL;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001751
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001752 fTextureCache = SkNEW_ARGS(GrResourceCache,
1753 (MAX_TEXTURE_CACHE_COUNT,
1754 MAX_TEXTURE_CACHE_BYTES));
1755 fFontCache = SkNEW_ARGS(GrFontCache, (fGpu));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001756
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001757 fLastDrawWasBuffered = kNo_BufferedDraw;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001758
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001759 fDrawBuffer = NULL;
1760 fDrawBufferVBAllocPool = NULL;
1761 fDrawBufferIBAllocPool = NULL;
1762
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001763 fAARectRenderer = SkNEW(GrAARectRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001764
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001765 fDidTestPMConversions = false;
1766
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001767 this->setupDrawBuffer();
1768}
1769
1770void GrContext::setupDrawBuffer() {
1771
1772 GrAssert(NULL == fDrawBuffer);
1773 GrAssert(NULL == fDrawBufferVBAllocPool);
1774 GrAssert(NULL == fDrawBufferIBAllocPool);
1775
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001776 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001777 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001778 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001779 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001780 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001781 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001782 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001783 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001784
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001785 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com471d4712011-08-23 15:45:25 +00001786 fDrawBufferVBAllocPool,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001787 fDrawBufferIBAllocPool));
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001788
bsalomon@google.com27847de2011-02-22 20:59:41 +00001789 fDrawBuffer->setQuadIndexBuffer(this->getQuadIndexBuffer());
bsalomon@google.com1015e032012-06-25 18:41:04 +00001790 if (fDrawBuffer) {
1791 fDrawBuffer->setAutoFlushTarget(fGpu);
1792 fDrawBuffer->setDrawState(fDrawState);
1793 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001794}
1795
bsalomon@google.com27847de2011-02-22 20:59:41 +00001796GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) {
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001797 return prepareToDraw(&paint, DEFAULT_BUFFERING);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001798}
1799
1800const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1801 return fGpu->getQuadIndexBuffer();
1802}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001803
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001804namespace {
1805void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
1806 GrConfigConversionEffect::PMConversion pmToUPM;
1807 GrConfigConversionEffect::PMConversion upmToPM;
1808 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
1809 *pmToUPMValue = pmToUPM;
1810 *upmToPMValue = upmToPM;
1811}
1812}
1813
1814GrCustomStage* GrContext::createPMToUPMEffect(GrTexture* texture, bool swapRAndB) {
1815 if (!fDidTestPMConversions) {
1816 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001817 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001818 }
1819 GrConfigConversionEffect::PMConversion pmToUPM =
1820 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
1821 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
1822 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM);
1823 } else {
1824 return NULL;
1825 }
1826}
1827
1828GrCustomStage* GrContext::createUPMToPMEffect(GrTexture* texture, bool swapRAndB) {
1829 if (!fDidTestPMConversions) {
1830 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001831 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001832 }
1833 GrConfigConversionEffect::PMConversion upmToPM =
1834 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
1835 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
1836 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM);
1837 } else {
1838 return NULL;
1839 }
1840}
1841
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001842GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture,
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001843 bool canClobberSrc,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001844 const SkRect& rect,
1845 float sigmaX, float sigmaY) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001846 ASSERT_OWNED_RESOURCE(srcTexture);
robertphillips@google.comccb39502012-10-01 18:25:13 +00001847
1848 AutoRenderTarget art(this);
1849
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001850 AutoMatrix avm(this, GrMatrix::I());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001851 SkIRect clearRect;
bsalomon@google.comb505a122012-05-31 18:40:36 +00001852 int scaleFactorX, radiusX;
1853 int scaleFactorY, radiusY;
1854 sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX);
1855 sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY);
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001856
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001857 SkRect srcRect(rect);
1858 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
1859 srcRect.roundOut();
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001860 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001861 static_cast<float>(scaleFactorY));
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001862
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001863 AutoClip acs(this, srcRect);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001864
bsalomon@google.com0342a852012-08-20 19:22:38 +00001865 GrAssert(kBGRA_8888_GrPixelConfig == srcTexture->config() ||
1866 kRGBA_8888_GrPixelConfig == srcTexture->config() ||
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001867 kAlpha_8_GrPixelConfig == srcTexture->config());
1868
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001869 GrTextureDesc desc;
1870 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1871 desc.fWidth = SkScalarFloorToInt(srcRect.width());
1872 desc.fHeight = SkScalarFloorToInt(srcRect.height());
1873 desc.fConfig = srcTexture->config();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001874
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001875 GrAutoScratchTexture temp1, temp2;
1876 GrTexture* dstTexture = temp1.set(this, desc);
1877 GrTexture* tempTexture = canClobberSrc ? srcTexture : temp2.set(this, desc);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001878
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001879 GrPaint paint;
1880 paint.reset();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001881
1882 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
1883 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1884 srcTexture->height());
1885 this->setRenderTarget(dstTexture->asRenderTarget());
1886 SkRect dstRect(srcRect);
1887 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
1888 i < scaleFactorY ? 0.5f : 1.0f);
tomhudson@google.comaa72eab2012-07-19 18:01:07 +00001889 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001890 (srcTexture, true)))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001891 this->drawRectToRect(paint, dstRect, srcRect);
1892 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001893 srcTexture = dstTexture;
1894 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001895 }
1896
robertphillips@google.com7a396332012-05-10 15:11:27 +00001897 SkIRect srcIRect;
1898 srcRect.roundOut(&srcIRect);
1899
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001900 if (sigmaX > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001901 if (scaleFactorX > 1) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001902 // Clear out a radius to the right of the srcRect to prevent the
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001903 // X convolution from reading garbage.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001904 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001905 radiusX, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001906 this->clear(&clearRect, 0x0);
1907 }
1908
1909 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001910 GrDrawTarget* target = this->prepareToDraw(NULL, DEFAULT_BUFFERING);
1911 convolve_gaussian(target, srcTexture, srcRect, sigmaX, radiusX,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001912 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001913 srcTexture = dstTexture;
1914 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001915 }
1916
1917 if (sigmaY > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001918 if (scaleFactorY > 1 || sigmaX > 0.0f) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001919 // Clear out a radius below the srcRect to prevent the Y
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001920 // convolution from reading garbage.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001921 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001922 srcIRect.width(), radiusY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001923 this->clear(&clearRect, 0x0);
1924 }
1925
1926 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001927 GrDrawTarget* target = this->prepareToDraw(NULL, DEFAULT_BUFFERING);
1928 convolve_gaussian(target, srcTexture, srcRect, sigmaY, radiusY,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001929 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001930 srcTexture = dstTexture;
1931 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001932 }
1933
1934 if (scaleFactorX > 1 || scaleFactorY > 1) {
1935 // Clear one pixel to the right and below, to accommodate bilinear
1936 // upsampling.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001937 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
robertphillips@google.com7a396332012-05-10 15:11:27 +00001938 srcIRect.width() + 1, 1);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001939 this->clear(&clearRect, 0x0);
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001940 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
robertphillips@google.com7a396332012-05-10 15:11:27 +00001941 1, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001942 this->clear(&clearRect, 0x0);
1943 // FIXME: This should be mitchell, not bilinear.
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001944 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1945 srcTexture->height());
1946 this->setRenderTarget(dstTexture->asRenderTarget());
tomhudson@google.comaa72eab2012-07-19 18:01:07 +00001947 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001948 (srcTexture, true)))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001949 SkRect dstRect(srcRect);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001950 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001951 this->drawRectToRect(paint, dstRect, srcRect);
1952 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001953 srcTexture = dstTexture;
1954 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001955 }
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001956 if (srcTexture == temp1.texture()) {
1957 return temp1.detach();
1958 } else if (srcTexture == temp2.texture()) {
1959 return temp2.detach();
1960 } else {
1961 srcTexture->ref();
1962 return srcTexture;
1963 }
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001964}
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001965
bsalomon@google.comc4364992011-11-07 15:54:49 +00001966///////////////////////////////////////////////////////////////////////////////
robertphillips@google.com59552022012-08-31 13:07:37 +00001967#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001968void GrContext::printCacheStats() const {
1969 fTextureCache->printStats();
1970}
1971#endif