blob: ee3a4e1afb841a8f051bb180b65e0315a5e016ea [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.comf6eac8a2012-10-16 14:31:26 +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.comf6eac8a2012-10-16 14:31:26 +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.comf6eac8a2012-10-16 14:31:26 +0000316 drawState->sampler(0)->reset();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000317 GrTextureParams params(SkShader::kClamp_TileMode, needsFiltering);
bsalomon@google.comf6eac8a2012-10-16 14:31:26 +0000318 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
skia.committer@gmail.comf467ce72012-10-09 02:01:37 +0000573 fDrawState->setState(GrDrawState::kClip_StateBit,
robertphillips@google.com042aff82012-10-08 18:42:14 +0000574 clipData->fClipStack && !clipData->fClipStack->isWideOpen());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000575}
576
bsalomon@google.com27847de2011-02-22 20:59:41 +0000577////////////////////////////////////////////////////////////////////////////////
578
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000579void GrContext::clear(const GrIRect* rect,
580 const GrColor color,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000581 GrRenderTarget* target) {
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000582 this->prepareToDraw(NULL, DEFAULT_BUFFERING)->clear(rect, color, target);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000583}
584
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000585void GrContext::drawPaint(const GrPaint& origPaint) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000586 // set rect to be big enough to fill the space, but not super-huge, so we
587 // don't overflow fixed-point implementations
bsalomon@google.comd302f142011-03-03 13:54:13 +0000588 GrRect r;
589 r.setLTRB(0, 0,
590 GrIntToScalar(getRenderTarget()->width()),
591 GrIntToScalar(getRenderTarget()->height()));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000592 GrMatrix inverse;
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000593 SkTCopyOnFirstWrite<GrPaint> paint(origPaint);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000594 AutoMatrix am;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000595
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000596 // We attempt to map r by the inverse matrix and draw that. mapRect will
597 // map the four corners and bound them with a new rect. This will not
598 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000599 if (!this->getMatrix().hasPerspective()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000600 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000601 GrPrintf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000602 return;
603 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000604 inverse.mapRect(&r);
605 } else {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000606 if (!am.setIdentity(this, paint.writable())) {
607 GrPrintf("Could not invert matrix\n");
608 return;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000609 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000610 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000611 // by definition this fills the entire clip, no need for AA
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000612 if (paint->isAntiAlias()) {
613 paint.writable()->setAntiAlias(false);
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000614 }
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000615 this->drawRect(*paint, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000616}
617
bsalomon@google.com205d4602011-04-25 12:43:45 +0000618////////////////////////////////////////////////////////////////////////////////
619
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000620namespace {
621inline bool disable_coverage_aa_for_blend(GrDrawTarget* target) {
622 return DISABLE_COVERAGE_AA_FOR_BLEND && !target->canApplyCoverage();
623}
624}
625
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000626////////////////////////////////////////////////////////////////////////////////
627
bsalomon@google.com27847de2011-02-22 20:59:41 +0000628/* create a triangle strip that strokes the specified triangle. There are 8
629 unique vertices, but we repreat the last 2 to close up. Alternatively we
630 could use an indices array, and then only send 8 verts, but not sure that
631 would be faster.
632 */
bsalomon@google.com205d4602011-04-25 12:43:45 +0000633static void setStrokeRectStrip(GrPoint verts[10], GrRect rect,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000634 GrScalar width) {
635 const GrScalar rad = GrScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000636 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000637
638 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
639 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
640 verts[2].set(rect.fRight - rad, rect.fTop + rad);
641 verts[3].set(rect.fRight + rad, rect.fTop - rad);
642 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
643 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
644 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
645 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
646 verts[8] = verts[0];
647 verts[9] = verts[1];
648}
649
reed@google.com20efde72011-05-09 17:00:02 +0000650/**
651 * Returns true if the rects edges are integer-aligned.
652 */
653static bool isIRect(const GrRect& r) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000654 return GrScalarIsInt(r.fLeft) && GrScalarIsInt(r.fTop) &&
reed@google.com20efde72011-05-09 17:00:02 +0000655 GrScalarIsInt(r.fRight) && GrScalarIsInt(r.fBottom);
656}
657
bsalomon@google.com205d4602011-04-25 12:43:45 +0000658static bool apply_aa_to_rect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000659 const GrRect& rect,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000660 GrScalar width,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000661 const GrMatrix* matrix,
662 GrMatrix* combinedMatrix,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000663 GrRect* devRect,
664 bool* useVertexCoverage) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000665 // we use a simple coverage ramp to do aa on axis-aligned rects
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000666 // we check if the rect will be axis-aligned, and the rect won't land on
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000667 // integer coords.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000668
bsalomon@google.coma3108262011-10-10 14:08:47 +0000669 // we are keeping around the "tweak the alpha" trick because
670 // it is our only hope for the fixed-pipe implementation.
671 // In a shader implementation we can give a separate coverage input
bsalomon@google.com289533a2011-10-27 12:34:25 +0000672 // TODO: remove this ugliness when we drop the fixed-pipe impl
bsalomon@google.coma3108262011-10-10 14:08:47 +0000673 *useVertexCoverage = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000674 if (!target->canTweakAlphaForCoverage()) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000675 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +0000676#if GR_DEBUG
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000677 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000678#endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000679 return false;
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000680 } else {
681 *useVertexCoverage = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000682 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000683 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000684 const GrDrawState& drawState = target->getDrawState();
685 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000686 return false;
687 }
688
bsalomon@google.com471d4712011-08-23 15:45:25 +0000689 if (0 == width && target->willUseHWAALines()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000690 return false;
691 }
692
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000693 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000694 return false;
695 }
696
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000697 if (NULL != matrix &&
bsalomon@google.com205d4602011-04-25 12:43:45 +0000698 !matrix->preservesAxisAlignment()) {
699 return false;
700 }
701
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000702 *combinedMatrix = drawState.getViewMatrix();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000703 if (NULL != matrix) {
704 combinedMatrix->preConcat(*matrix);
705 GrAssert(combinedMatrix->preservesAxisAlignment());
706 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000707
bsalomon@google.com205d4602011-04-25 12:43:45 +0000708 combinedMatrix->mapRect(devRect, rect);
709 devRect->sort();
710
711 if (width < 0) {
reed@google.com20efde72011-05-09 17:00:02 +0000712 return !isIRect(*devRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000713 } else {
714 return true;
715 }
716}
717
bsalomon@google.com27847de2011-02-22 20:59:41 +0000718void GrContext::drawRect(const GrPaint& paint,
719 const GrRect& rect,
720 GrScalar width,
721 const GrMatrix* matrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000722 SK_TRACE_EVENT0("GrContext::drawRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000723
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000724 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000725 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000726
bsalomon@google.com205d4602011-04-25 12:43:45 +0000727 GrRect devRect = rect;
728 GrMatrix combinedMatrix;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000729 bool useVertexCoverage;
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000730 bool needAA = paint.isAntiAlias() &&
bsalomon@google.com289533a2011-10-27 12:34:25 +0000731 !this->getRenderTarget()->isMultisampled();
732 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix,
733 &combinedMatrix, &devRect,
734 &useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000735
736 if (doAA) {
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000737 GrDrawState::AutoDeviceCoordDraw adcd(target->drawState());
bsalomon@google.come3d32162012-07-20 13:37:06 +0000738 if (!adcd.succeeded()) {
739 return;
740 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000741 if (width >= 0) {
742 GrVec strokeSize;;
743 if (width > 0) {
744 strokeSize.set(width, width);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000745 combinedMatrix.mapVectors(&strokeSize, 1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000746 strokeSize.setAbs(strokeSize);
747 } else {
748 strokeSize.set(GR_Scalar1, GR_Scalar1);
749 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000750 fAARectRenderer->strokeAARect(this->getGpu(), target, devRect,
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000751 strokeSize, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000752 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000753 fAARectRenderer->fillAARect(this->getGpu(), target,
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000754 devRect, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000755 }
756 return;
757 }
758
bsalomon@google.com27847de2011-02-22 20:59:41 +0000759 if (width >= 0) {
760 // TODO: consider making static vertex buffers for these cases.
761 // Hairline could be done by just adding closing vertex to
762 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000763
bsalomon@google.com27847de2011-02-22 20:59:41 +0000764 static const int worstCaseVertCount = 10;
bsalomon@google.come3d32162012-07-20 13:37:06 +0000765 GrDrawTarget::AutoReleaseGeometry geo(target, 0, worstCaseVertCount, 0);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000766
767 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000768 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000769 return;
770 }
771
772 GrPrimitiveType primType;
773 int vertCount;
774 GrPoint* vertex = geo.positions();
775
776 if (width > 0) {
777 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000778 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000779 setStrokeRectStrip(vertex, rect, width);
780 } else {
781 // hairline
782 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000783 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000784 vertex[0].set(rect.fLeft, rect.fTop);
785 vertex[1].set(rect.fRight, rect.fTop);
786 vertex[2].set(rect.fRight, rect.fBottom);
787 vertex[3].set(rect.fLeft, rect.fBottom);
788 vertex[4].set(rect.fLeft, rect.fTop);
789 }
790
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000791 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000792 if (NULL != matrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000793 GrDrawState* drawState = target->drawState();
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000794 avmr.set(drawState, *matrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000795 }
796
797 target->drawNonIndexed(primType, 0, vertCount);
798 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000799#if GR_STATIC_RECT_VB
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000800 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
801 if (NULL == sqVB) {
802 GrPrintf("Failed to create static rect vb.\n");
803 return;
804 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000805 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000806 GrDrawState* drawState = target->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000807 GrMatrix m;
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000808 m.setAll(rect.width(), 0, rect.fLeft,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000809 0, rect.height(), rect.fTop,
810 0, 0, GrMatrix::I()[8]);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000811
812 if (NULL != matrix) {
813 m.postConcat(*matrix);
814 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000815 GrDrawState::AutoViewMatrixRestore avmr(drawState, m);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000816
bsalomon@google.com47059542012-06-06 20:51:20 +0000817 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000818#else
bsalomon@google.come3d32162012-07-20 13:37:06 +0000819 target->drawSimpleRect(rect, matrix);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000820#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000821 }
822}
823
824void GrContext::drawRectToRect(const GrPaint& paint,
825 const GrRect& dstRect,
826 const GrRect& srcRect,
827 const GrMatrix* dstMatrix,
828 const GrMatrix* srcMatrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000829 SK_TRACE_EVENT0("GrContext::drawRectToRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000830
bsalomon@google.com88becf42012-10-05 14:54:42 +0000831 // srcRect refers to paint's first color stage
832 if (!paint.isColorStageEnabled(0)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000833 drawRect(paint, dstRect, -1, dstMatrix);
834 return;
835 }
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000836
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000837 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000838
839#if GR_STATIC_RECT_VB
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000840 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000841 GrDrawState* drawState = target->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000842
843 GrMatrix m;
844
845 m.setAll(dstRect.width(), 0, dstRect.fLeft,
846 0, dstRect.height(), dstRect.fTop,
847 0, 0, GrMatrix::I()[8]);
848 if (NULL != dstMatrix) {
849 m.postConcat(*dstMatrix);
850 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000851
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000852 // The first color stage's coords come from srcRect rather than applying a matrix to dstRect.
853 // We explicitly compute a matrix for that stage below, no need to adjust here.
854 static const uint32_t kExplicitCoordMask = 1 << GrPaint::kFirstColorStage;
855 GrDrawState::AutoViewMatrixRestore avmr(drawState, m, kExplicitCoordMask);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000856
bsalomon@google.com27847de2011-02-22 20:59:41 +0000857 m.setAll(srcRect.width(), 0, srcRect.fLeft,
858 0, srcRect.height(), srcRect.fTop,
859 0, 0, GrMatrix::I()[8]);
860 if (NULL != srcMatrix) {
861 m.postConcat(*srcMatrix);
862 }
bsalomon@google.com903a4e22012-10-05 18:08:27 +0000863 drawState->sampler(GrPaint::kFirstColorStage)->preConcatMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000864
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000865 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
866 if (NULL == sqVB) {
867 GrPrintf("Failed to create static rect vb.\n");
868 return;
869 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000870 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com47059542012-06-06 20:51:20 +0000871 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000872#else
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000873 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000874
tomhudson@google.com93813632011-10-27 20:21:16 +0000875 const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
876 const GrMatrix* srcMatrices[GrDrawState::kNumStages] = {NULL};
bsalomon@google.com27847de2011-02-22 20:59:41 +0000877 srcRects[0] = &srcRect;
878 srcMatrices[0] = srcMatrix;
879
bsalomon@google.come3d32162012-07-20 13:37:06 +0000880 target->drawRect(dstRect, dstMatrix, srcRects, srcMatrices);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000881#endif
882}
883
884void GrContext::drawVertices(const GrPaint& paint,
885 GrPrimitiveType primitiveType,
886 int vertexCount,
887 const GrPoint positions[],
888 const GrPoint texCoords[],
889 const GrColor colors[],
890 const uint16_t indices[],
891 int indexCount) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000892 SK_TRACE_EVENT0("GrContext::drawVertices");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000893
894 GrDrawTarget::AutoReleaseGeometry geo;
895
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000896 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000897 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000898
bsalomon@google.come3d32162012-07-20 13:37:06 +0000899 GrVertexLayout layout = 0;
900 if (NULL != texCoords) {
901 layout |= GrDrawTarget::StageTexCoordVertexLayoutBit(0, 0);
902 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000903 if (NULL != colors) {
904 layout |= GrDrawTarget::kColor_VertexLayoutBit;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000905 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000906 int vertexSize = GrDrawTarget::VertexSize(layout);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000907
908 if (sizeof(GrPoint) != vertexSize) {
909 if (!geo.set(target, layout, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000910 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000911 return;
912 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000913 int texOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000914 int colorOffset;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000915 GrDrawTarget::VertexSizeAndOffsetsByIdx(layout,
916 texOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000917 &colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000918 NULL,
919 NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000920 void* curVertex = geo.vertices();
921
922 for (int i = 0; i < vertexCount; ++i) {
923 *((GrPoint*)curVertex) = positions[i];
924
925 if (texOffsets[0] > 0) {
926 *(GrPoint*)((intptr_t)curVertex + texOffsets[0]) = texCoords[i];
927 }
928 if (colorOffset > 0) {
929 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
930 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000931 curVertex = (void*)((intptr_t)curVertex + vertexSize);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000932 }
933 } else {
934 target->setVertexSourceToArray(layout, positions, vertexCount);
935 }
936
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000937 // we don't currently apply offscreen AA to this path. Need improved
bsalomon@google.com91958362011-06-13 17:58:13 +0000938 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000939
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000940 if (NULL != indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000941 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000942 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000943 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000944 target->drawNonIndexed(primitiveType, 0, vertexCount);
945 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000946}
947
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000948///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com150d2842012-01-12 20:19:56 +0000949namespace {
950
bsalomon@google.com93c96602012-04-27 13:05:21 +0000951struct CircleVertex {
952 GrPoint fPos;
953 GrPoint fCenter;
954 GrScalar fOuterRadius;
955 GrScalar fInnerRadius;
956};
957
958/* Returns true if will map a circle to another circle. This can be true
959 * if the matrix only includes square-scale, rotation, translation.
960 */
961inline bool isSimilarityTransformation(const SkMatrix& matrix,
962 SkScalar tol = SK_ScalarNearlyZero) {
963 if (matrix.isIdentity() || matrix.getType() == SkMatrix::kTranslate_Mask) {
964 return true;
965 }
966 if (matrix.hasPerspective()) {
967 return false;
968 }
969
970 SkScalar mx = matrix.get(SkMatrix::kMScaleX);
971 SkScalar sx = matrix.get(SkMatrix::kMSkewX);
972 SkScalar my = matrix.get(SkMatrix::kMScaleY);
973 SkScalar sy = matrix.get(SkMatrix::kMSkewY);
974
975 if (mx == 0 && sx == 0 && my == 0 && sy == 0) {
976 return false;
977 }
978
979 // it has scales or skews, but it could also be rotation, check it out.
980 SkVector vec[2];
981 vec[0].set(mx, sx);
982 vec[1].set(sy, my);
983
984 return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) &&
985 SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(),
986 SkScalarSquare(tol));
987}
988
989}
990
991// TODO: strokeWidth can't be larger than zero right now.
992// It will be fixed when drawPath() can handle strokes.
993void GrContext::drawOval(const GrPaint& paint,
994 const GrRect& rect,
995 SkScalar strokeWidth) {
bsalomon@google.com0982d352012-07-31 15:33:25 +0000996 GrAssert(strokeWidth <= 0);
997 if (!isSimilarityTransformation(this->getMatrix()) ||
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000998 !paint.isAntiAlias() ||
bsalomon@google.com93c96602012-04-27 13:05:21 +0000999 rect.height() != rect.width()) {
1000 SkPath path;
1001 path.addOval(rect);
1002 GrPathFill fill = (strokeWidth == 0) ?
bsalomon@google.com0982d352012-07-31 15:33:25 +00001003 kHairLine_GrPathFill : kWinding_GrPathFill;
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +00001004 this->internalDrawPath(paint, path, fill);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001005 return;
1006 }
1007
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001008 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
1009
bsalomon@google.com0982d352012-07-31 15:33:25 +00001010 GrDrawState* drawState = target->drawState();
1011 GrDrawState::AutoStageDisable atr(fDrawState);
1012 const GrMatrix vm = drawState->getViewMatrix();
1013
bsalomon@google.com93c96602012-04-27 13:05:21 +00001014 const GrRenderTarget* rt = drawState->getRenderTarget();
1015 if (NULL == rt) {
1016 return;
1017 }
1018
bsalomon@google.com5b3e8902012-10-05 20:13:28 +00001019 GrDrawState::AutoDeviceCoordDraw adcd(drawState);
bsalomon@google.come3d32162012-07-20 13:37:06 +00001020 if (!adcd.succeeded()) {
1021 return;
1022 }
bsalomon@google.com93c96602012-04-27 13:05:21 +00001023
bsalomon@google.come3d32162012-07-20 13:37:06 +00001024 GrVertexLayout layout = GrDrawTarget::kEdge_VertexLayoutBit;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001025 GrAssert(sizeof(CircleVertex) == GrDrawTarget::VertexSize(layout));
1026
1027 GrPoint center = GrPoint::Make(rect.centerX(), rect.centerY());
1028 GrScalar radius = SkScalarHalf(rect.width());
1029
1030 vm.mapPoints(&center, 1);
1031 radius = vm.mapRadius(radius);
1032
1033 GrScalar outerRadius = radius;
1034 GrScalar innerRadius = 0;
1035 SkScalar halfWidth = 0;
1036 if (strokeWidth == 0) {
1037 halfWidth = SkScalarHalf(SK_Scalar1);
1038
1039 outerRadius += halfWidth;
1040 innerRadius = SkMaxScalar(0, radius - halfWidth);
1041 }
1042
1043 GrDrawTarget::AutoReleaseGeometry geo(target, layout, 4, 0);
1044 if (!geo.succeeded()) {
1045 GrPrintf("Failed to get space for vertices!\n");
1046 return;
1047 }
1048
1049 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
1050
robertphillips@google.coma0a66c12012-06-22 13:14:29 +00001051 // The fragment shader will extend the radius out half a pixel
1052 // to antialias. Expand the drawn rect here so all the pixels
1053 // will be captured.
1054 SkScalar L = center.fX - outerRadius - SkFloatToScalar(0.5f);
1055 SkScalar R = center.fX + outerRadius + SkFloatToScalar(0.5f);
1056 SkScalar T = center.fY - outerRadius - SkFloatToScalar(0.5f);
1057 SkScalar B = center.fY + outerRadius + SkFloatToScalar(0.5f);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001058
1059 verts[0].fPos = SkPoint::Make(L, T);
1060 verts[1].fPos = SkPoint::Make(R, T);
1061 verts[2].fPos = SkPoint::Make(L, B);
1062 verts[3].fPos = SkPoint::Make(R, B);
1063
1064 for (int i = 0; i < 4; ++i) {
1065 // this goes to fragment shader, it should be in y-points-up space.
1066 verts[i].fCenter = SkPoint::Make(center.fX, rt->height() - center.fY);
1067
1068 verts[i].fOuterRadius = outerRadius;
1069 verts[i].fInnerRadius = innerRadius;
1070 }
1071
1072 drawState->setVertexEdgeType(GrDrawState::kCircle_EdgeType);
bsalomon@google.com47059542012-06-06 20:51:20 +00001073 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001074}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001075
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +00001076void GrContext::drawPath(const GrPaint& paint, const SkPath& path, GrPathFill fill) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001077
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001078 if (path.isEmpty()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001079 if (GrIsFillInverted(fill)) {
1080 this->drawPaint(paint);
1081 }
1082 return;
1083 }
1084
bsalomon@google.com93c96602012-04-27 13:05:21 +00001085 SkRect ovalRect;
1086 if (!GrIsFillInverted(fill) && path.isOval(&ovalRect)) {
bsalomon@google.com47059542012-06-06 20:51:20 +00001087 SkScalar width = (fill == kHairLine_GrPathFill) ? 0 : -SK_Scalar1;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001088 this->drawOval(paint, ovalRect, width);
1089 return;
1090 }
1091
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +00001092 this->internalDrawPath(paint, path, fill);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001093}
1094
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +00001095void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path, GrPathFill fill) {
bsalomon@google.com93c96602012-04-27 13:05:21 +00001096
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001097 // Note that below we may sw-rasterize the path into a scratch texture.
1098 // Scratch textures can be recycled after they are returned to the texture
1099 // cache. This presents a potential hazard for buffered drawing. However,
1100 // the writePixels that uploads to the scratch will perform a flush so we're
1101 // OK.
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001102 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +00001103 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001104
bsalomon@google.comc7448ce2012-10-05 19:04:13 +00001105 bool prAA = paint.isAntiAlias() && !this->getRenderTarget()->isMultisampled();
bsalomon@google.com289533a2011-10-27 12:34:25 +00001106
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001107 // An Assumption here is that path renderer would use some form of tweaking
1108 // the src color (either the input alpha or in the frag shader) to implement
1109 // aa. If we have some future driver-mojo path AA that can do the right
1110 // thing WRT to the blend then we'll need some query on the PR.
1111 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001112#if GR_DEBUG
bsalomon@google.com979432b2011-11-05 21:38:22 +00001113 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001114#endif
bsalomon@google.com289533a2011-10-27 12:34:25 +00001115 prAA = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001116 }
bsalomon@google.com289533a2011-10-27 12:34:25 +00001117
robertphillips@google.com72176b22012-05-23 13:19:12 +00001118 GrPathRenderer* pr = this->getPathRenderer(path, fill, target, prAA, true);
bsalomon@google.com30085192011-08-19 15:42:31 +00001119 if (NULL == pr) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001120#if GR_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001121 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001122#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001123 return;
1124 }
1125
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +00001126 pr->drawPath(path, fill, target, prAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001127}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001128
bsalomon@google.com27847de2011-02-22 20:59:41 +00001129////////////////////////////////////////////////////////////////////////////////
1130
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001131void GrContext::flush(int flagsBitfield) {
1132 if (kDiscard_FlushBit & flagsBitfield) {
1133 fDrawBuffer->reset();
1134 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001135 this->flushDrawBuffer();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001136 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001137 if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001138 fGpu->forceRenderTargetFlush();
1139 }
1140}
1141
bsalomon@google.com27847de2011-02-22 20:59:41 +00001142void GrContext::flushDrawBuffer() {
junov@google.com53a55842011-06-08 22:55:10 +00001143 if (fDrawBuffer) {
robertphillips@google.com58b38182012-05-03 16:29:41 +00001144 // With addition of the AA clip path, flushing the draw buffer can
1145 // result in the generation of an AA clip mask. During this
1146 // process the SW path renderer may be invoked which recusively
1147 // calls this method (via internalWriteTexturePixels) creating
1148 // infinite recursion
1149 GrInOrderDrawBuffer* temp = fDrawBuffer;
1150 fDrawBuffer = NULL;
1151
1152 temp->flushTo(fGpu);
1153
1154 fDrawBuffer = temp;
junov@google.com53a55842011-06-08 22:55:10 +00001155 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001156}
1157
bsalomon@google.com0342a852012-08-20 19:22:38 +00001158void GrContext::writeTexturePixels(GrTexture* texture,
1159 int left, int top, int width, int height,
1160 GrPixelConfig config, const void* buffer, size_t rowBytes,
1161 uint32_t flags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001162 SK_TRACE_EVENT0("GrContext::writeTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001163 ASSERT_OWNED_RESOURCE(texture);
1164
bsalomon@google.com0342a852012-08-20 19:22:38 +00001165 // TODO: use scratch texture to perform conversion
1166 if (kUnpremul_PixelOpsFlag & flags) {
1167 return;
1168 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001169 if (!(kDontFlush_PixelOpsFlag & flags)) {
1170 this->flush();
1171 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001172
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001173 fGpu->writeTexturePixels(texture, left, top, width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +00001174 config, buffer, rowBytes);
1175}
1176
bsalomon@google.com0342a852012-08-20 19:22:38 +00001177bool GrContext::readTexturePixels(GrTexture* texture,
1178 int left, int top, int width, int height,
1179 GrPixelConfig config, void* buffer, size_t rowBytes,
1180 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001181 SK_TRACE_EVENT0("GrContext::readTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001182 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001183
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001184 // TODO: code read pixels for textures that aren't also rendertargets
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001185 GrRenderTarget* target = texture->asRenderTarget();
1186 if (NULL != target) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001187 return this->readRenderTargetPixels(target,
1188 left, top, width, height,
1189 config, buffer, rowBytes,
1190 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001191 } else {
1192 return false;
1193 }
1194}
1195
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001196#include "SkConfig8888.h"
1197
1198namespace {
1199/**
1200 * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel
1201 * formats are representable as Config8888 and so the function returns false
1202 * if the GrPixelConfig has no equivalent Config8888.
1203 */
1204bool grconfig_to_config8888(GrPixelConfig config,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001205 bool unpremul,
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001206 SkCanvas::Config8888* config8888) {
1207 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001208 case kRGBA_8888_GrPixelConfig:
1209 if (unpremul) {
1210 *config8888 = SkCanvas::kRGBA_Unpremul_Config8888;
1211 } else {
1212 *config8888 = SkCanvas::kRGBA_Premul_Config8888;
1213 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001214 return true;
bsalomon@google.com0342a852012-08-20 19:22:38 +00001215 case kBGRA_8888_GrPixelConfig:
1216 if (unpremul) {
1217 *config8888 = SkCanvas::kBGRA_Unpremul_Config8888;
1218 } else {
1219 *config8888 = SkCanvas::kBGRA_Premul_Config8888;
1220 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001221 return true;
1222 default:
1223 return false;
1224 }
1225}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001226
1227// It returns a configuration with where the byte position of the R & B components are swapped in
1228// relation to the input config. This should only be called with the result of
1229// grconfig_to_config8888 as it will fail for other configs.
1230SkCanvas::Config8888 swap_config8888_red_and_blue(SkCanvas::Config8888 config8888) {
1231 switch (config8888) {
1232 case SkCanvas::kBGRA_Premul_Config8888:
1233 return SkCanvas::kRGBA_Premul_Config8888;
1234 case SkCanvas::kBGRA_Unpremul_Config8888:
1235 return SkCanvas::kRGBA_Unpremul_Config8888;
1236 case SkCanvas::kRGBA_Premul_Config8888:
1237 return SkCanvas::kBGRA_Premul_Config8888;
1238 case SkCanvas::kRGBA_Unpremul_Config8888:
1239 return SkCanvas::kBGRA_Unpremul_Config8888;
1240 default:
1241 GrCrash("Unexpected input");
1242 return SkCanvas::kBGRA_Unpremul_Config8888;;
1243 }
1244}
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001245}
1246
bsalomon@google.com0342a852012-08-20 19:22:38 +00001247bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
1248 int left, int top, int width, int height,
1249 GrPixelConfig config, void* buffer, size_t rowBytes,
1250 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001251 SK_TRACE_EVENT0("GrContext::readRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001252 ASSERT_OWNED_RESOURCE(target);
1253
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001254 if (NULL == target) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001255 target = fDrawState->getRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001256 if (NULL == target) {
1257 return false;
1258 }
1259 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001260
bsalomon@google.com6f379512011-11-16 20:36:03 +00001261 if (!(kDontFlush_PixelOpsFlag & flags)) {
1262 this->flush();
1263 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001264
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001265 // Determine which conversions have to be applied: flipY, swapRAnd, and/or unpremul.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001266
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001267 // If fGpu->readPixels would incur a y-flip cost then we will read the pixels upside down. We'll
1268 // either do the flipY by drawing into a scratch with a matrix or on the cpu after the read.
1269 bool flipY = fGpu->readPixelsWillPayForYFlip(target, left, top,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001270 width, height, config,
1271 rowBytes);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001272 bool swapRAndB = fGpu->preferredReadPixelsConfig(config) == GrPixelConfigSwapRAndB(config);
1273
bsalomon@google.com0342a852012-08-20 19:22:38 +00001274 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001275
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001276 // flipY will get set to false when it is handled below using a scratch. However, in that case
1277 // we still want to do the read upside down.
1278 bool readUpsideDown = flipY;
1279
1280 if (unpremul && kRGBA_8888_GrPixelConfig != config && kBGRA_8888_GrPixelConfig != config) {
1281 // The unpremul flag is only allowed for these two configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001282 return false;
1283 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001284
1285 GrPixelConfig readConfig;
1286 if (swapRAndB) {
1287 readConfig = GrPixelConfigSwapRAndB(config);
1288 GrAssert(kUnknown_GrPixelConfig != config);
1289 } else {
1290 readConfig = config;
1291 }
1292
1293 // If the src is a texture and we would have to do conversions after read pixels, we instead
1294 // do the conversions by drawing the src to a scratch texture. If we handle any of the
1295 // conversions in the draw we set the corresponding bool to false so that we don't reapply it
1296 // on the read back pixels.
1297 GrTexture* src = target->asTexture();
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001298 GrAutoScratchTexture ast;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001299 if (NULL != src && (swapRAndB || unpremul || flipY)) {
1300 // Make the scratch a render target because we don't have a robust readTexturePixels as of
1301 // yet. It calls this function.
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001302 GrTextureDesc desc;
1303 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1304 desc.fWidth = width;
1305 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001306 desc.fConfig = readConfig;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001307
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001308 // When a full readback is faster than a partial we could always make the scratch exactly
1309 // match the passed rect. However, if we see many different size rectangles we will trash
1310 // our texture cache and pay the cost of creating and destroying many textures. So, we only
1311 // request an exact match when the caller is reading an entire RT.
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001312 ScratchTexMatch match = kApprox_ScratchTexMatch;
1313 if (0 == left &&
1314 0 == top &&
1315 target->width() == width &&
1316 target->height() == height &&
1317 fGpu->fullReadPixelsIsFasterThanPartial()) {
1318 match = kExact_ScratchTexMatch;
1319 }
1320 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001321 GrTexture* texture = ast.texture();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001322 if (texture) {
1323 SkAutoTUnref<GrCustomStage> stage;
1324 if (unpremul) {
1325 stage.reset(this->createPMToUPMEffect(src, swapRAndB));
1326 }
1327 // If we failed to create a PM->UPM effect and have no other conversions to perform then
1328 // there is no longer any point to using the scratch.
1329 if (NULL != stage || flipY || swapRAndB) {
1330 if (NULL == stage) {
1331 stage.reset(GrConfigConversionEffect::Create(src, swapRAndB));
1332 GrAssert(NULL != stage);
1333 } else {
1334 unpremul = false; // we will handle the UPM conversion in the draw
1335 }
1336 swapRAndB = false; // we will handle the swap in the draw.
bsalomon@google.comc4364992011-11-07 15:54:49 +00001337
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001338 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
1339 GrDrawState* drawState = fGpu->drawState();
1340 drawState->setRenderTarget(texture->asRenderTarget());
1341 GrMatrix matrix;
1342 if (flipY) {
1343 matrix.setTranslate(SK_Scalar1 * left,
1344 SK_Scalar1 * (top + height));
1345 matrix.set(GrMatrix::kMScaleY, -GR_Scalar1);
1346 flipY = false; // the y flip will be handled in the draw
1347 } else {
1348 matrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
1349 }
1350 matrix.postIDiv(src->width(), src->height());
bsalomon@google.comf6eac8a2012-10-16 14:31:26 +00001351 drawState->sampler(0)->reset(matrix);
1352 drawState->sampler(0)->setCustomStage(stage);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001353 GrRect rect = GrRect::MakeWH(GrIntToScalar(width), GrIntToScalar(height));
1354 fGpu->drawSimpleRect(rect, NULL);
1355 // we want to read back from the scratch's origin
1356 left = 0;
1357 top = 0;
1358 target = texture->asRenderTarget();
1359 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001360 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001361 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001362 if (!fGpu->readPixels(target,
1363 left, top, width, height,
1364 readConfig, buffer, rowBytes, readUpsideDown)) {
1365 return false;
1366 }
1367 // Perform any conversions we weren't able to perfom using a scratch texture.
1368 if (unpremul || swapRAndB || flipY) {
bsalomon@google.comccaa0022012-09-25 19:55:07 +00001369 // These are initialized to suppress a warning
1370 SkCanvas::Config8888 srcC8888 = SkCanvas::kNative_Premul_Config8888;
1371 SkCanvas::Config8888 dstC8888 = SkCanvas::kNative_Premul_Config8888;
1372
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001373 bool c8888IsValid = grconfig_to_config8888(config, false, &srcC8888);
1374 grconfig_to_config8888(config, unpremul, &dstC8888);
bsalomon@google.comccaa0022012-09-25 19:55:07 +00001375
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001376 if (swapRAndB) {
1377 GrAssert(c8888IsValid); // we should only do r/b swap on 8888 configs
1378 srcC8888 = swap_config8888_red_and_blue(srcC8888);
1379 }
1380 if (flipY) {
1381 size_t tightRB = width * GrBytesPerPixel(config);
1382 if (0 == rowBytes) {
1383 rowBytes = tightRB;
1384 }
1385 SkAutoSTMalloc<256, uint8_t> tempRow(tightRB);
1386 intptr_t top = reinterpret_cast<intptr_t>(buffer);
1387 intptr_t bot = top + (height - 1) * rowBytes;
1388 while (top < bot) {
1389 uint32_t* t = reinterpret_cast<uint32_t*>(top);
1390 uint32_t* b = reinterpret_cast<uint32_t*>(bot);
1391 uint32_t* temp = reinterpret_cast<uint32_t*>(tempRow.get());
1392 memcpy(temp, t, tightRB);
1393 if (c8888IsValid) {
1394 SkConvertConfig8888Pixels(t, tightRB, dstC8888,
1395 b, tightRB, srcC8888,
1396 width, 1);
1397 SkConvertConfig8888Pixels(b, tightRB, dstC8888,
1398 temp, tightRB, srcC8888,
1399 width, 1);
1400 } else {
1401 memcpy(t, b, tightRB);
1402 memcpy(b, temp, tightRB);
1403 }
1404 top += rowBytes;
1405 bot -= rowBytes;
1406 }
1407 // The above loop does nothing on the middle row when height is odd.
1408 if (top == bot && c8888IsValid && dstC8888 != srcC8888) {
1409 uint32_t* mid = reinterpret_cast<uint32_t*>(top);
1410 SkConvertConfig8888Pixels(mid, tightRB, dstC8888, mid, tightRB, srcC8888, width, 1);
1411 }
1412 } else {
1413 // if we aren't flipping Y then we have no reason to be here other than doing
1414 // conversions for 8888 (r/b swap or upm).
1415 GrAssert(c8888IsValid);
1416 uint32_t* b32 = reinterpret_cast<uint32_t*>(buffer);
1417 SkConvertConfig8888Pixels(b32, rowBytes, dstC8888,
1418 b32, rowBytes, srcC8888,
1419 width, height);
1420 }
1421 }
1422 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001423}
1424
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001425void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1426 GrAssert(target);
1427 ASSERT_OWNED_RESOURCE(target);
1428 // In the future we may track whether there are any pending draws to this
1429 // target. We don't today so we always perform a flush. We don't promise
1430 // this to our clients, though.
1431 this->flush();
1432 fGpu->resolveRenderTarget(target);
1433}
1434
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001435void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst) {
1436 if (NULL == src || NULL == dst) {
1437 return;
1438 }
1439 ASSERT_OWNED_RESOURCE(src);
1440
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001441 // Writes pending to the source texture are not tracked, so a flush
1442 // is required to ensure that the copy captures the most recent contents
1443 // of the source texture. See similar behaviour in
1444 // GrContext::resolveRenderTarget.
1445 this->flush();
1446
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001447 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001448 GrDrawState* drawState = fGpu->drawState();
1449 drawState->setRenderTarget(dst);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001450 GrMatrix sampleM;
1451 sampleM.setIDiv(src->width(), src->height());
bsalomon@google.comf6eac8a2012-10-16 14:31:26 +00001452 drawState->sampler(0)->reset(sampleM);
1453 drawState->createTextureEffect(0, src);
bsalomon@google.com5db3b6c2012-01-12 20:38:57 +00001454 SkRect rect = SkRect::MakeXYWH(0, 0,
1455 SK_Scalar1 * src->width(),
1456 SK_Scalar1 * src->height());
bsalomon@google.come3d32162012-07-20 13:37:06 +00001457 fGpu->drawSimpleRect(rect, NULL);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001458}
1459
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001460void GrContext::writeRenderTargetPixels(GrRenderTarget* target,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001461 int left, int top, int width, int height,
1462 GrPixelConfig config,
1463 const void* buffer,
1464 size_t rowBytes,
1465 uint32_t flags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001466 SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001467 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001468
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001469 if (NULL == target) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001470 target = fDrawState->getRenderTarget();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001471 if (NULL == target) {
1472 return;
1473 }
1474 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001475
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001476 // TODO: when underlying api has a direct way to do this we should use it (e.g. glDrawPixels on
1477 // desktop GL).
1478
1479 // We will always call some form of writeTexturePixels and we will pass our flags on to it.
1480 // Thus, we don't perform a flush here since that call will do it (if the kNoFlush flag isn't
1481 // set.)
bsalomon@google.com27847de2011-02-22 20:59:41 +00001482
bsalomon@google.com0342a852012-08-20 19:22:38 +00001483 // If the RT is also a texture and we don't have to premultiply then take the texture path.
1484 // We expect to be at least as fast or faster since it doesn't use an intermediate texture as
1485 // we do below.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001486
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001487#if !GR_MAC_BUILD
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001488 // At least some drivers on the Mac get confused when glTexImage2D is called on a texture
1489 // attached to an FBO. The FBO still sees the old image. TODO: determine what OS versions and/or
1490 // HW is affected.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001491 if (NULL != target->asTexture() && !(kUnpremul_PixelOpsFlag & flags)) {
1492 this->writeTexturePixels(target->asTexture(),
1493 left, top, width, height,
1494 config, buffer, rowBytes, flags);
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001495 return;
1496 }
1497#endif
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001498 SkAutoTUnref<GrCustomStage> stage;
1499 bool swapRAndB = (fGpu->preferredReadPixelsConfig(config) == GrPixelConfigSwapRAndB(config));
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001500
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001501 GrPixelConfig textureConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001502 if (swapRAndB) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001503 textureConfig = GrPixelConfigSwapRAndB(config);
1504 } else {
1505 textureConfig = config;
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001506 }
1507
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001508 GrTextureDesc desc;
1509 desc.fWidth = width;
1510 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001511 desc.fConfig = textureConfig;
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001512 GrAutoScratchTexture ast(this, desc);
1513 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001514 if (NULL == texture) {
1515 return;
1516 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001517 // allocate a tmp buffer and sw convert the pixels to premul
1518 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
1519
1520 if (kUnpremul_PixelOpsFlag & flags) {
1521 if (kRGBA_8888_GrPixelConfig != config && kBGRA_8888_GrPixelConfig != config) {
1522 return;
1523 }
1524 stage.reset(this->createUPMToPMEffect(texture, swapRAndB));
1525 if (NULL == stage) {
1526 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1527 GR_DEBUGCODE(bool success = )
1528 grconfig_to_config8888(config, true, &srcConfig8888);
1529 GrAssert(success);
1530 GR_DEBUGCODE(success = )
1531 grconfig_to_config8888(config, false, &dstConfig8888);
1532 GrAssert(success);
1533 const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer);
1534 tmpPixels.reset(width * height);
1535 SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888,
1536 src, rowBytes, srcConfig8888,
1537 width, height);
1538 buffer = tmpPixels.get();
1539 rowBytes = 4 * width;
1540 }
1541 }
1542 if (NULL == stage) {
1543 stage.reset(GrConfigConversionEffect::Create(texture, swapRAndB));
1544 GrAssert(NULL != stage);
1545 }
1546
1547 this->writeTexturePixels(texture,
1548 0, 0, width, height,
1549 textureConfig, buffer, rowBytes,
1550 flags & ~kUnpremul_PixelOpsFlag);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001551
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001552 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001553 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001554
1555 GrMatrix matrix;
1556 matrix.setTranslate(GrIntToScalar(left), GrIntToScalar(top));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001557 drawState->setViewMatrix(matrix);
1558 drawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001559
bsalomon@google.com5c638652011-07-18 19:31:59 +00001560 matrix.setIDiv(texture->width(), texture->height());
bsalomon@google.comf6eac8a2012-10-16 14:31:26 +00001561 drawState->sampler(0)->reset(matrix);
1562 drawState->sampler(0)->setCustomStage(stage);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001563
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001564 fGpu->drawSimpleRect(GrRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)), NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001565}
1566////////////////////////////////////////////////////////////////////////////////
1567
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001568GrDrawTarget* GrContext::prepareToDraw(const GrPaint* paint, BufferedDraw buffered) {
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001569 if (kNo_BufferedDraw == buffered && kYes_BufferedDraw == fLastDrawWasBuffered) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001570 this->flushDrawBuffer();
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001571 fLastDrawWasBuffered = kNo_BufferedDraw;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001572 }
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001573 if (NULL != paint) {
bsalomon@google.comaf84e742012-10-05 13:23:24 +00001574 GrAssert(fDrawState->stagesDisabled());
1575 fDrawState->setFromPaint(*paint);
1576#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
1577 if ((paint->hasMask() || 0xff != paint->fCoverage) &&
1578 !fGpu->canApplyCoverage()) {
1579 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1580 }
1581#endif
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001582 }
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001583 if (kYes_BufferedDraw == buffered) {
1584 fDrawBuffer->setClip(fGpu->getClip());
1585 fLastDrawWasBuffered = kYes_BufferedDraw;
1586 return fDrawBuffer;
1587 } else {
1588 GrAssert(kNo_BufferedDraw == buffered);
1589 return fGpu;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001590 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001591}
1592
robertphillips@google.com72176b22012-05-23 13:19:12 +00001593/*
1594 * This method finds a path renderer that can draw the specified path on
1595 * the provided target.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001596 * Due to its expense, the software path renderer has split out so it can
robertphillips@google.com72176b22012-05-23 13:19:12 +00001597 * can be individually allowed/disallowed via the "allowSW" boolean.
1598 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001599GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
bsalomon@google.com289533a2011-10-27 12:34:25 +00001600 GrPathFill fill,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001601 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001602 bool antiAlias,
1603 bool allowSW) {
bsalomon@google.com30085192011-08-19 15:42:31 +00001604 if (NULL == fPathRendererChain) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001605 fPathRendererChain =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001606 SkNEW_ARGS(GrPathRendererChain,
1607 (this, GrPathRendererChain::kNone_UsageFlag));
bsalomon@google.com30085192011-08-19 15:42:31 +00001608 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001609
1610 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path, fill,
1611 target,
1612 antiAlias);
1613
1614 if (NULL == pr && allowSW) {
1615 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001616 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001617 }
1618
1619 pr = fSoftwarePathRenderer;
1620 }
1621
1622 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001623}
1624
bsalomon@google.com27847de2011-02-22 20:59:41 +00001625////////////////////////////////////////////////////////////////////////////////
1626
bsalomon@google.com27847de2011-02-22 20:59:41 +00001627void GrContext::setRenderTarget(GrRenderTarget* target) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001628 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001629 fDrawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001630}
1631
1632GrRenderTarget* GrContext::getRenderTarget() {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001633 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001634}
1635
1636const GrRenderTarget* GrContext::getRenderTarget() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001637 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001638}
1639
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001640bool GrContext::isConfigRenderable(GrPixelConfig config) const {
1641 return fGpu->isConfigRenderable(config);
1642}
1643
bsalomon@google.com27847de2011-02-22 20:59:41 +00001644const GrMatrix& GrContext::getMatrix() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001645 return fDrawState->getViewMatrix();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001646}
1647
1648void GrContext::setMatrix(const GrMatrix& m) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001649 fDrawState->setViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001650}
1651
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +00001652void GrContext::setIdentityMatrix() {
1653 fDrawState->viewMatrix()->reset();
1654}
1655
bsalomon@google.com27847de2011-02-22 20:59:41 +00001656void GrContext::concatMatrix(const GrMatrix& m) const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001657 fDrawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001658}
1659
1660static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
1661 intptr_t mask = 1 << shift;
1662 if (pred) {
1663 bits |= mask;
1664 } else {
1665 bits &= ~mask;
1666 }
1667 return bits;
1668}
1669
bsalomon@google.com583a1e32011-08-17 13:42:46 +00001670GrContext::GrContext(GrGpu* gpu) {
bsalomon@google.comc0af3172012-06-15 14:10:09 +00001671 ++THREAD_INSTANCE_COUNT;
1672
bsalomon@google.com27847de2011-02-22 20:59:41 +00001673 fGpu = gpu;
1674 fGpu->ref();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001675 fGpu->setContext(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001676
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001677 fDrawState = SkNEW(GrDrawState);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001678 fGpu->setDrawState(fDrawState);
1679
bsalomon@google.com30085192011-08-19 15:42:31 +00001680 fPathRendererChain = NULL;
robertphillips@google.com72176b22012-05-23 13:19:12 +00001681 fSoftwarePathRenderer = NULL;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001682
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001683 fTextureCache = SkNEW_ARGS(GrResourceCache,
1684 (MAX_TEXTURE_CACHE_COUNT,
1685 MAX_TEXTURE_CACHE_BYTES));
1686 fFontCache = SkNEW_ARGS(GrFontCache, (fGpu));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001687
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001688 fLastDrawWasBuffered = kNo_BufferedDraw;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001689
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001690 fDrawBuffer = NULL;
1691 fDrawBufferVBAllocPool = NULL;
1692 fDrawBufferIBAllocPool = NULL;
1693
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001694 fAARectRenderer = SkNEW(GrAARectRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001695
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001696 fDidTestPMConversions = false;
1697
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001698 this->setupDrawBuffer();
1699}
1700
1701void GrContext::setupDrawBuffer() {
1702
1703 GrAssert(NULL == fDrawBuffer);
1704 GrAssert(NULL == fDrawBufferVBAllocPool);
1705 GrAssert(NULL == fDrawBufferIBAllocPool);
1706
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001707 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001708 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001709 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001710 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001711 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001712 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001713 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001714 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001715
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001716 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com471d4712011-08-23 15:45:25 +00001717 fDrawBufferVBAllocPool,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001718 fDrawBufferIBAllocPool));
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001719
bsalomon@google.com27847de2011-02-22 20:59:41 +00001720 fDrawBuffer->setQuadIndexBuffer(this->getQuadIndexBuffer());
bsalomon@google.com1015e032012-06-25 18:41:04 +00001721 if (fDrawBuffer) {
1722 fDrawBuffer->setAutoFlushTarget(fGpu);
1723 fDrawBuffer->setDrawState(fDrawState);
1724 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001725}
1726
bsalomon@google.com27847de2011-02-22 20:59:41 +00001727GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) {
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001728 return prepareToDraw(&paint, DEFAULT_BUFFERING);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001729}
1730
1731const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1732 return fGpu->getQuadIndexBuffer();
1733}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001734
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001735namespace {
1736void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
1737 GrConfigConversionEffect::PMConversion pmToUPM;
1738 GrConfigConversionEffect::PMConversion upmToPM;
1739 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
1740 *pmToUPMValue = pmToUPM;
1741 *upmToPMValue = upmToPM;
1742}
1743}
1744
1745GrCustomStage* GrContext::createPMToUPMEffect(GrTexture* texture, bool swapRAndB) {
1746 if (!fDidTestPMConversions) {
1747 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001748 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001749 }
1750 GrConfigConversionEffect::PMConversion pmToUPM =
1751 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
1752 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
1753 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM);
1754 } else {
1755 return NULL;
1756 }
1757}
1758
1759GrCustomStage* GrContext::createUPMToPMEffect(GrTexture* texture, bool swapRAndB) {
1760 if (!fDidTestPMConversions) {
1761 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001762 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001763 }
1764 GrConfigConversionEffect::PMConversion upmToPM =
1765 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
1766 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
1767 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM);
1768 } else {
1769 return NULL;
1770 }
1771}
1772
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001773GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture,
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001774 bool canClobberSrc,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001775 const SkRect& rect,
1776 float sigmaX, float sigmaY) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001777 ASSERT_OWNED_RESOURCE(srcTexture);
robertphillips@google.comccb39502012-10-01 18:25:13 +00001778
1779 AutoRenderTarget art(this);
1780
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001781 AutoMatrix am;
1782 am.setIdentity(this);
1783
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001784 SkIRect clearRect;
bsalomon@google.comb505a122012-05-31 18:40:36 +00001785 int scaleFactorX, radiusX;
1786 int scaleFactorY, radiusY;
1787 sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX);
1788 sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY);
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001789
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001790 SkRect srcRect(rect);
1791 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
1792 srcRect.roundOut();
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001793 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001794 static_cast<float>(scaleFactorY));
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001795
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001796 AutoClip acs(this, srcRect);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001797
bsalomon@google.com0342a852012-08-20 19:22:38 +00001798 GrAssert(kBGRA_8888_GrPixelConfig == srcTexture->config() ||
1799 kRGBA_8888_GrPixelConfig == srcTexture->config() ||
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001800 kAlpha_8_GrPixelConfig == srcTexture->config());
1801
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001802 GrTextureDesc desc;
1803 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1804 desc.fWidth = SkScalarFloorToInt(srcRect.width());
1805 desc.fHeight = SkScalarFloorToInt(srcRect.height());
1806 desc.fConfig = srcTexture->config();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001807
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001808 GrAutoScratchTexture temp1, temp2;
1809 GrTexture* dstTexture = temp1.set(this, desc);
1810 GrTexture* tempTexture = canClobberSrc ? srcTexture : temp2.set(this, desc);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001811
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001812 GrPaint paint;
1813 paint.reset();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001814
1815 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
bsalomon@google.comf6eac8a2012-10-16 14:31:26 +00001816 paint.colorSampler(0)->matrix()->setIDiv(srcTexture->width(),
1817 srcTexture->height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001818 this->setRenderTarget(dstTexture->asRenderTarget());
1819 SkRect dstRect(srcRect);
1820 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
bsalomon@google.comf6eac8a2012-10-16 14:31:26 +00001821 i < scaleFactorY ? 0.5f : 1.0f);
bsalomon@google.com88becf42012-10-05 14:54:42 +00001822 paint.colorSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
bsalomon@google.comf6eac8a2012-10-16 14:31:26 +00001823 (srcTexture, true)))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001824 this->drawRectToRect(paint, dstRect, srcRect);
1825 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001826 srcTexture = dstTexture;
1827 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001828 }
1829
robertphillips@google.com7a396332012-05-10 15:11:27 +00001830 SkIRect srcIRect;
1831 srcRect.roundOut(&srcIRect);
1832
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001833 if (sigmaX > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001834 if (scaleFactorX > 1) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001835 // Clear out a radius to the right of the srcRect to prevent the
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001836 // X convolution from reading garbage.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001837 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001838 radiusX, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001839 this->clear(&clearRect, 0x0);
1840 }
1841
1842 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001843 GrDrawTarget* target = this->prepareToDraw(NULL, DEFAULT_BUFFERING);
1844 convolve_gaussian(target, srcTexture, srcRect, sigmaX, radiusX,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001845 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001846 srcTexture = dstTexture;
1847 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001848 }
1849
1850 if (sigmaY > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001851 if (scaleFactorY > 1 || sigmaX > 0.0f) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001852 // Clear out a radius below the srcRect to prevent the Y
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001853 // convolution from reading garbage.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001854 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001855 srcIRect.width(), radiusY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001856 this->clear(&clearRect, 0x0);
1857 }
1858
1859 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001860 GrDrawTarget* target = this->prepareToDraw(NULL, DEFAULT_BUFFERING);
1861 convolve_gaussian(target, srcTexture, srcRect, sigmaY, radiusY,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001862 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001863 srcTexture = dstTexture;
1864 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001865 }
1866
1867 if (scaleFactorX > 1 || scaleFactorY > 1) {
1868 // Clear one pixel to the right and below, to accommodate bilinear
1869 // upsampling.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001870 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
robertphillips@google.com7a396332012-05-10 15:11:27 +00001871 srcIRect.width() + 1, 1);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001872 this->clear(&clearRect, 0x0);
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001873 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
robertphillips@google.com7a396332012-05-10 15:11:27 +00001874 1, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001875 this->clear(&clearRect, 0x0);
1876 // FIXME: This should be mitchell, not bilinear.
bsalomon@google.comf6eac8a2012-10-16 14:31:26 +00001877 paint.colorSampler(0)->matrix()->setIDiv(srcTexture->width(),
1878 srcTexture->height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001879 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com88becf42012-10-05 14:54:42 +00001880 paint.colorSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
bsalomon@google.comf6eac8a2012-10-16 14:31:26 +00001881 (srcTexture, true)))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001882 SkRect dstRect(srcRect);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001883 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001884 this->drawRectToRect(paint, dstRect, srcRect);
1885 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001886 srcTexture = dstTexture;
1887 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001888 }
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001889 if (srcTexture == temp1.texture()) {
1890 return temp1.detach();
1891 } else if (srcTexture == temp2.texture()) {
1892 return temp2.detach();
1893 } else {
1894 srcTexture->ref();
1895 return srcTexture;
1896 }
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001897}
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001898
bsalomon@google.comc4364992011-11-07 15:54:49 +00001899///////////////////////////////////////////////////////////////////////////////
robertphillips@google.com59552022012-08-31 13:07:37 +00001900#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001901void GrContext::printCacheStats() const {
1902 fTextureCache->printStats();
1903}
1904#endif