blob: 63e5c2c1fca5f36a32ef4393e78864bc07ae0fe2 [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
robertphillips@google.com1947ba62012-10-17 13:35:24 +000051static const size_t MAX_TEXTURE_CACHE_COUNT = 2048;
52static const size_t MAX_TEXTURE_CACHE_BYTES = 96 * 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());
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000205 SkAutoTUnref<GrConvolutionEffect> conv(SkNEW_ARGS(GrConvolutionEffect,
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000206 (texture, direction, radius,
207 sigma)));
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000208 drawState->sampler(0)->setCustomStage(conv, sampleM);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000209 target->drawSimpleRect(rect, NULL);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000210}
211
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000212}
213
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000214
215GrTexture* GrContext::findTexture(const GrCacheKey& key) {
216 return static_cast<GrTexture*>(fTextureCache->find(key.key()));
217}
218
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000219GrTexture* GrContext::findTexture(const GrTextureDesc& desc,
220 const GrCacheData& cacheData,
221 const GrTextureParams* params) {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000222 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000223 GrResource* resource = fTextureCache->find(resourceKey);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000224 return static_cast<GrTexture*>(resource);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000225}
226
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000227bool GrContext::isTextureInCache(const GrTextureDesc& desc,
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000228 const GrCacheData& cacheData,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000229 const GrTextureParams* params) const {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000230 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000231 return fTextureCache->hasKey(resourceKey);
232}
233
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000234void GrContext::addStencilBuffer(GrStencilBuffer* sb) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000235 ASSERT_OWNED_RESOURCE(sb);
robertphillips@google.com46a86002012-08-08 10:42:44 +0000236
237 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(sb->width(),
238 sb->height(),
239 sb->numSamples());
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000240 fTextureCache->create(resourceKey, sb);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000241}
242
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000243GrStencilBuffer* GrContext::findStencilBuffer(int width, int height,
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000244 int sampleCnt) {
robertphillips@google.com46a86002012-08-08 10:42:44 +0000245 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(width,
246 height,
247 sampleCnt);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000248 GrResource* resource = fTextureCache->find(resourceKey);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000249 return static_cast<GrStencilBuffer*>(resource);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000250}
251
bsalomon@google.com27847de2011-02-22 20:59:41 +0000252static void stretchImage(void* dst,
253 int dstW,
254 int dstH,
255 void* src,
256 int srcW,
257 int srcH,
258 int bpp) {
259 GrFixed dx = (srcW << 16) / dstW;
260 GrFixed dy = (srcH << 16) / dstH;
261
262 GrFixed y = dy >> 1;
263
264 int dstXLimit = dstW*bpp;
265 for (int j = 0; j < dstH; ++j) {
266 GrFixed x = dx >> 1;
267 void* srcRow = (uint8_t*)src + (y>>16)*srcW*bpp;
268 void* dstRow = (uint8_t*)dst + j*dstW*bpp;
269 for (int i = 0; i < dstXLimit; i += bpp) {
270 memcpy((uint8_t*) dstRow + i,
271 (uint8_t*) srcRow + (x>>16)*bpp,
272 bpp);
273 x += dx;
274 }
275 y += dy;
276 }
277}
278
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000279// The desired texture is NPOT and tiled but that isn't supported by
robertphillips@google.com3319f332012-08-13 18:00:36 +0000280// the current hardware. Resize the texture to be a POT
281GrTexture* GrContext::createResizedTexture(const GrTextureDesc& desc,
282 const GrCacheData& cacheData,
283 void* srcData,
284 size_t rowBytes,
285 bool needsFiltering) {
robertphillips@google.com0d25eef2012-09-11 21:25:51 +0000286 GrTexture* clampedTexture = this->findTexture(desc, cacheData, NULL);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000287 if (NULL == clampedTexture) {
robertphillips@google.com0d25eef2012-09-11 21:25:51 +0000288 clampedTexture = this->createTexture(NULL, desc, cacheData, srcData, rowBytes);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000289
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000290 GrAssert(NULL != clampedTexture);
291 if (NULL == clampedTexture) {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000292 return NULL;
293 }
294 }
robertphillips@google.com0d25eef2012-09-11 21:25:51 +0000295
296 clampedTexture->ref();
297
robertphillips@google.com3319f332012-08-13 18:00:36 +0000298 GrTextureDesc rtDesc = desc;
299 rtDesc.fFlags = rtDesc.fFlags |
300 kRenderTarget_GrTextureFlagBit |
301 kNoStencil_GrTextureFlagBit;
302 rtDesc.fWidth = GrNextPow2(GrMax(desc.fWidth, 64));
303 rtDesc.fHeight = GrNextPow2(GrMax(desc.fHeight, 64));
304
305 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
306
307 if (NULL != texture) {
308 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
309 GrDrawState* drawState = fGpu->drawState();
310 drawState->setRenderTarget(texture->asRenderTarget());
311
312 // if filtering is not desired then we want to ensure all
313 // texels in the resampled image are copies of texels from
314 // the original.
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000315 GrTextureParams params(SkShader::kClamp_TileMode, needsFiltering);
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000316 drawState->createTextureEffect(0, clampedTexture, GrMatrix::I(), params);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000317
318 static const GrVertexLayout layout =
319 GrDrawTarget::StageTexCoordVertexLayoutBit(0,0);
320 GrDrawTarget::AutoReleaseGeometry arg(fGpu, layout, 4, 0);
321
322 if (arg.succeeded()) {
323 GrPoint* verts = (GrPoint*) arg.vertices();
324 verts[0].setIRectFan(0, 0,
325 texture->width(),
326 texture->height(),
327 2*sizeof(GrPoint));
328 verts[1].setIRectFan(0, 0, 1, 1, 2*sizeof(GrPoint));
329 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType,
330 0, 4);
331 }
332 texture->releaseRenderTarget();
333 } else {
334 // TODO: Our CPU stretch doesn't filter. But we create separate
335 // stretched textures when the sampler state is either filtered or
336 // not. Either implement filtered stretch blit on CPU or just create
337 // one when FBO case fails.
338
339 rtDesc.fFlags = kNone_GrTextureFlags;
340 // no longer need to clamp at min RT size.
341 rtDesc.fWidth = GrNextPow2(desc.fWidth);
342 rtDesc.fHeight = GrNextPow2(desc.fHeight);
343 int bpp = GrBytesPerPixel(desc.fConfig);
344 SkAutoSMalloc<128*128*4> stretchedPixels(bpp *
345 rtDesc.fWidth *
346 rtDesc.fHeight);
347 stretchImage(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
348 srcData, desc.fWidth, desc.fHeight, bpp);
349
350 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
351
352 GrTexture* texture = fGpu->createTexture(rtDesc,
353 stretchedPixels.get(),
354 stretchedRowBytes);
355 GrAssert(NULL != texture);
356 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000357
robertphillips@google.com0d25eef2012-09-11 21:25:51 +0000358 clampedTexture->unref();
robertphillips@google.com3319f332012-08-13 18:00:36 +0000359 return texture;
360}
361
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000362GrTexture* GrContext::createTexture(
bsalomon@google.comb8670992012-07-25 21:27:09 +0000363 const GrTextureParams* params,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000364 const GrTextureDesc& desc,
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000365 const GrCacheData& cacheData,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000366 void* srcData,
367 size_t rowBytes) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000368 SK_TRACE_EVENT0("GrContext::createAndLockTexture");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000369
370#if GR_DUMP_TEXTURE_UPLOAD
371 GrPrintf("GrContext::createAndLockTexture [%d %d]\n", desc.fWidth, desc.fHeight);
372#endif
373
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000374 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000375
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +0000376 SkAutoTUnref<GrTexture> texture;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000377 if (GrTexture::NeedsResizing(resourceKey)) {
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +0000378 texture.reset(this->createResizedTexture(desc, cacheData,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000379 srcData, rowBytes,
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +0000380 GrTexture::NeedsFiltering(resourceKey)));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000381 } else {
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +0000382 texture.reset(fGpu->createTexture(desc, srcData, rowBytes));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000383 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000384
385 if (NULL != texture) {
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000386 fTextureCache->create(resourceKey, texture);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000387 }
388
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000389 return texture;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000390}
391
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000392GrTexture* GrContext::lockScratchTexture(const GrTextureDesc& inDesc,
393 ScratchTexMatch match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000394 GrTextureDesc desc = inDesc;
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000395 GrCacheData cacheData(GrCacheData::kScratch_CacheID);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000396
rileya@google.com2afb8ec2012-08-23 14:08:57 +0000397 GrAssert((desc.fFlags & kRenderTarget_GrTextureFlagBit) ||
398 !(desc.fFlags & kNoStencil_GrTextureFlagBit));
399
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000400 if (kExact_ScratchTexMatch != match) {
401 // bin by pow2 with a reasonable min
402 static const int MIN_SIZE = 256;
403 desc.fWidth = GrMax(MIN_SIZE, GrNextPow2(desc.fWidth));
404 desc.fHeight = GrMax(MIN_SIZE, GrNextPow2(desc.fHeight));
405 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000406
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000407 GrResource* resource = NULL;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000408 int origWidth = desc.fWidth;
409 int origHeight = desc.fHeight;
410 bool doubledW = false;
411 bool doubledH = false;
412
413 do {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000414 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL, desc, cacheData, true);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000415 resource = fTextureCache->find(key);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000416 // if we miss, relax the fit of the flags...
417 // then try doubling width... then height.
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000418 if (NULL != resource || kExact_ScratchTexMatch == match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000419 break;
420 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000421 // We no longer try to reuse textures that were previously used as render targets in
rileya@google.com2afb8ec2012-08-23 14:08:57 +0000422 // situations where no RT is needed; doing otherwise can confuse the video driver and
423 // cause significant performance problems in some cases.
424 if (desc.fFlags & kNoStencil_GrTextureFlagBit) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000425 desc.fFlags = desc.fFlags & ~kNoStencil_GrTextureFlagBit;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000426 } else if (!doubledW) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000427 desc.fFlags = inDesc.fFlags;
428 desc.fWidth *= 2;
429 doubledW = true;
430 } else if (!doubledH) {
431 desc.fFlags = inDesc.fFlags;
432 desc.fWidth = origWidth;
433 desc.fHeight *= 2;
434 doubledH = true;
435 } else {
436 break;
437 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000438
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000439 } while (true);
440
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000441 if (NULL == resource) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000442 desc.fFlags = inDesc.fFlags;
443 desc.fWidth = origWidth;
444 desc.fHeight = origHeight;
robertphillips@google.comf2e93fc2012-09-05 19:44:18 +0000445 SkAutoTUnref<GrTexture> texture(fGpu->createTexture(desc, NULL, 0));
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000446 if (NULL != texture) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000447 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000448 texture->desc(),
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000449 cacheData,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000450 true);
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000451 fTextureCache->create(key, texture);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000452 resource = texture;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000453 }
454 }
455
456 // If the caller gives us the same desc/sampler twice we don't want
457 // to return the same texture the second time (unless it was previously
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000458 // released). So make it exclusive to hide it from future searches.
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000459 if (NULL != resource) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000460 fTextureCache->makeExclusive(resource->getCacheEntry());
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000461 }
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000462
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000463 return static_cast<GrTexture*>(resource);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000464}
465
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000466void GrContext::addExistingTextureToCache(GrTexture* texture) {
467
468 if (NULL == texture) {
469 return;
470 }
471
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000472 // This texture should already have a cache entry since it was once
473 // attached
474 GrAssert(NULL != texture->getCacheEntry());
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000475
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000476 // Conceptually, the cache entry is going to assume responsibility
477 // for the creation ref.
478 GrAssert(1 == texture->getRefCnt());
479
480 // Since this texture came from an AutoScratchTexture it should
481 // still be in the exclusive pile
482 fTextureCache->makeNonExclusive(texture->getCacheEntry());
483
robertphillips@google.com50a035d2012-09-07 19:44:33 +0000484 this->purgeCache();
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000485}
486
robertphillips@google.coma9b06232012-08-30 11:06:31 +0000487
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000488void GrContext::unlockScratchTexture(GrTexture* texture) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000489 ASSERT_OWNED_RESOURCE(texture);
490 GrAssert(NULL != texture->getCacheEntry());
491
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000492 // If this is a scratch texture we detached it from the cache
493 // while it was locked (to avoid two callers simultaneously getting
494 // the same texture).
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000495 if (GrTexture::IsScratchTexture(texture->getCacheEntry()->key())) {
robertphillips@google.com521eaf82012-08-22 11:03:19 +0000496 fTextureCache->makeNonExclusive(texture->getCacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000497 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000498
robertphillips@google.com50a035d2012-09-07 19:44:33 +0000499 this->purgeCache();
500}
501
502void GrContext::purgeCache() {
robertphillips@google.comeb9b3e12012-09-11 12:50:53 +0000503 if (NULL != fTextureCache) {
504 fTextureCache->purgeAsNeeded();
505 }
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000506}
507
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000508GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000509 void* srcData,
510 size_t rowBytes) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000511 GrTextureDesc descCopy = descIn;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000512 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000513}
514
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000515void GrContext::getTextureCacheLimits(int* maxTextures,
516 size_t* maxTextureBytes) const {
517 fTextureCache->getLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000518}
519
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000520void GrContext::setTextureCacheLimits(int maxTextures, size_t maxTextureBytes) {
521 fTextureCache->setLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000522}
523
bsalomon@google.com91958362011-06-13 17:58:13 +0000524int GrContext::getMaxTextureSize() const {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000525 return fGpu->getCaps().maxTextureSize();
bsalomon@google.com91958362011-06-13 17:58:13 +0000526}
527
528int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.comf6601872012-08-28 21:11:35 +0000529 return fGpu->getCaps().maxRenderTargetSize();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000530}
531
532///////////////////////////////////////////////////////////////////////////////
533
bsalomon@google.come269f212011-11-07 13:29:52 +0000534GrTexture* GrContext::createPlatformTexture(const GrPlatformTextureDesc& desc) {
535 return fGpu->createPlatformTexture(desc);
536}
537
538GrRenderTarget* GrContext::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
539 return fGpu->createPlatformRenderTarget(desc);
540}
541
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000542///////////////////////////////////////////////////////////////////////////////
543
bsalomon@google.comb8670992012-07-25 21:27:09 +0000544bool GrContext::supportsIndex8PixelConfig(const GrTextureParams* params,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000545 int width, int height) const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000546 const GrDrawTarget::Caps& caps = fGpu->getCaps();
bsalomon@google.comf6601872012-08-28 21:11:35 +0000547 if (!caps.eightBitPaletteSupport()) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000548 return false;
549 }
550
bsalomon@google.com27847de2011-02-22 20:59:41 +0000551 bool isPow2 = GrIsPow2(width) && GrIsPow2(height);
552
553 if (!isPow2) {
bsalomon@google.comb8670992012-07-25 21:27:09 +0000554 bool tiled = NULL != params && params->isTiled();
bsalomon@google.comf6601872012-08-28 21:11:35 +0000555 if (tiled && !caps.npotTextureTileSupport()) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000556 return false;
557 }
558 }
559 return true;
560}
561
562////////////////////////////////////////////////////////////////////////////////
563
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000564const GrClipData* GrContext::getClip() const {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000565 return fGpu->getClip();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000566}
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000567
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000568void GrContext::setClip(const GrClipData* clipData) {
569 fGpu->setClip(clipData);
robertphillips@google.com837ec432012-10-04 17:57:05 +0000570
skia.committer@gmail.comf467ce72012-10-09 02:01:37 +0000571 fDrawState->setState(GrDrawState::kClip_StateBit,
robertphillips@google.com042aff82012-10-08 18:42:14 +0000572 clipData->fClipStack && !clipData->fClipStack->isWideOpen());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000573}
574
bsalomon@google.com27847de2011-02-22 20:59:41 +0000575////////////////////////////////////////////////////////////////////////////////
576
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000577void GrContext::clear(const GrIRect* rect,
578 const GrColor color,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000579 GrRenderTarget* target) {
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000580 this->prepareToDraw(NULL, DEFAULT_BUFFERING)->clear(rect, color, target);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000581}
582
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000583void GrContext::drawPaint(const GrPaint& origPaint) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000584 // set rect to be big enough to fill the space, but not super-huge, so we
585 // don't overflow fixed-point implementations
bsalomon@google.comd302f142011-03-03 13:54:13 +0000586 GrRect r;
587 r.setLTRB(0, 0,
588 GrIntToScalar(getRenderTarget()->width()),
589 GrIntToScalar(getRenderTarget()->height()));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000590 GrMatrix inverse;
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000591 SkTCopyOnFirstWrite<GrPaint> paint(origPaint);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000592 AutoMatrix am;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000593
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000594 // We attempt to map r by the inverse matrix and draw that. mapRect will
595 // map the four corners and bound them with a new rect. This will not
596 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000597 if (!this->getMatrix().hasPerspective()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000598 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000599 GrPrintf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000600 return;
601 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000602 inverse.mapRect(&r);
603 } else {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000604 if (!am.setIdentity(this, paint.writable())) {
605 GrPrintf("Could not invert matrix\n");
606 return;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000607 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000608 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000609 // by definition this fills the entire clip, no need for AA
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000610 if (paint->isAntiAlias()) {
611 paint.writable()->setAntiAlias(false);
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000612 }
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000613 this->drawRect(*paint, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000614}
615
bsalomon@google.com205d4602011-04-25 12:43:45 +0000616////////////////////////////////////////////////////////////////////////////////
617
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000618namespace {
619inline bool disable_coverage_aa_for_blend(GrDrawTarget* target) {
620 return DISABLE_COVERAGE_AA_FOR_BLEND && !target->canApplyCoverage();
621}
622}
623
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000624////////////////////////////////////////////////////////////////////////////////
625
bsalomon@google.com27847de2011-02-22 20:59:41 +0000626/* create a triangle strip that strokes the specified triangle. There are 8
627 unique vertices, but we repreat the last 2 to close up. Alternatively we
628 could use an indices array, and then only send 8 verts, but not sure that
629 would be faster.
630 */
bsalomon@google.com205d4602011-04-25 12:43:45 +0000631static void setStrokeRectStrip(GrPoint verts[10], GrRect rect,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000632 GrScalar width) {
633 const GrScalar rad = GrScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000634 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000635
636 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
637 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
638 verts[2].set(rect.fRight - rad, rect.fTop + rad);
639 verts[3].set(rect.fRight + rad, rect.fTop - rad);
640 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
641 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
642 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
643 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
644 verts[8] = verts[0];
645 verts[9] = verts[1];
646}
647
reed@google.com20efde72011-05-09 17:00:02 +0000648/**
649 * Returns true if the rects edges are integer-aligned.
650 */
651static bool isIRect(const GrRect& r) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000652 return GrScalarIsInt(r.fLeft) && GrScalarIsInt(r.fTop) &&
reed@google.com20efde72011-05-09 17:00:02 +0000653 GrScalarIsInt(r.fRight) && GrScalarIsInt(r.fBottom);
654}
655
bsalomon@google.com205d4602011-04-25 12:43:45 +0000656static bool apply_aa_to_rect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000657 const GrRect& rect,
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000658 GrScalar width,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000659 const GrMatrix* matrix,
660 GrMatrix* combinedMatrix,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000661 GrRect* devRect,
662 bool* useVertexCoverage) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000663 // we use a simple coverage ramp to do aa on axis-aligned rects
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000664 // we check if the rect will be axis-aligned, and the rect won't land on
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000665 // integer coords.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000666
bsalomon@google.coma3108262011-10-10 14:08:47 +0000667 // we are keeping around the "tweak the alpha" trick because
668 // it is our only hope for the fixed-pipe implementation.
669 // In a shader implementation we can give a separate coverage input
bsalomon@google.com289533a2011-10-27 12:34:25 +0000670 // TODO: remove this ugliness when we drop the fixed-pipe impl
bsalomon@google.coma3108262011-10-10 14:08:47 +0000671 *useVertexCoverage = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000672 if (!target->canTweakAlphaForCoverage()) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000673 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +0000674#if GR_DEBUG
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000675 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000676#endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000677 return false;
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000678 } else {
679 *useVertexCoverage = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000680 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000681 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000682 const GrDrawState& drawState = target->getDrawState();
683 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000684 return false;
685 }
686
bsalomon@google.com471d4712011-08-23 15:45:25 +0000687 if (0 == width && target->willUseHWAALines()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000688 return false;
689 }
690
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000691 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000692 return false;
693 }
694
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000695 if (NULL != matrix &&
bsalomon@google.com205d4602011-04-25 12:43:45 +0000696 !matrix->preservesAxisAlignment()) {
697 return false;
698 }
699
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000700 *combinedMatrix = drawState.getViewMatrix();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000701 if (NULL != matrix) {
702 combinedMatrix->preConcat(*matrix);
703 GrAssert(combinedMatrix->preservesAxisAlignment());
704 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000705
bsalomon@google.com205d4602011-04-25 12:43:45 +0000706 combinedMatrix->mapRect(devRect, rect);
707 devRect->sort();
708
709 if (width < 0) {
reed@google.com20efde72011-05-09 17:00:02 +0000710 return !isIRect(*devRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000711 } else {
712 return true;
713 }
714}
715
bsalomon@google.com27847de2011-02-22 20:59:41 +0000716void GrContext::drawRect(const GrPaint& paint,
717 const GrRect& rect,
718 GrScalar width,
719 const GrMatrix* matrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000720 SK_TRACE_EVENT0("GrContext::drawRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000721
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000722 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000723 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000724
bsalomon@google.com205d4602011-04-25 12:43:45 +0000725 GrRect devRect = rect;
726 GrMatrix combinedMatrix;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000727 bool useVertexCoverage;
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000728 bool needAA = paint.isAntiAlias() &&
bsalomon@google.com289533a2011-10-27 12:34:25 +0000729 !this->getRenderTarget()->isMultisampled();
730 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix,
731 &combinedMatrix, &devRect,
732 &useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000733
734 if (doAA) {
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000735 GrDrawState::AutoDeviceCoordDraw adcd(target->drawState());
bsalomon@google.come3d32162012-07-20 13:37:06 +0000736 if (!adcd.succeeded()) {
737 return;
738 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000739 if (width >= 0) {
740 GrVec strokeSize;;
741 if (width > 0) {
742 strokeSize.set(width, width);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000743 combinedMatrix.mapVectors(&strokeSize, 1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000744 strokeSize.setAbs(strokeSize);
745 } else {
746 strokeSize.set(GR_Scalar1, GR_Scalar1);
747 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000748 fAARectRenderer->strokeAARect(this->getGpu(), target, devRect,
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000749 strokeSize, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000750 } else {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000751 fAARectRenderer->fillAARect(this->getGpu(), target,
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000752 devRect, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000753 }
754 return;
755 }
756
bsalomon@google.com27847de2011-02-22 20:59:41 +0000757 if (width >= 0) {
758 // TODO: consider making static vertex buffers for these cases.
759 // Hairline could be done by just adding closing vertex to
760 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000761
bsalomon@google.com27847de2011-02-22 20:59:41 +0000762 static const int worstCaseVertCount = 10;
bsalomon@google.come3d32162012-07-20 13:37:06 +0000763 GrDrawTarget::AutoReleaseGeometry geo(target, 0, worstCaseVertCount, 0);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000764
765 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000766 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000767 return;
768 }
769
770 GrPrimitiveType primType;
771 int vertCount;
772 GrPoint* vertex = geo.positions();
773
774 if (width > 0) {
775 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000776 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000777 setStrokeRectStrip(vertex, rect, width);
778 } else {
779 // hairline
780 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000781 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000782 vertex[0].set(rect.fLeft, rect.fTop);
783 vertex[1].set(rect.fRight, rect.fTop);
784 vertex[2].set(rect.fRight, rect.fBottom);
785 vertex[3].set(rect.fLeft, rect.fBottom);
786 vertex[4].set(rect.fLeft, rect.fTop);
787 }
788
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000789 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000790 if (NULL != matrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000791 GrDrawState* drawState = target->drawState();
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000792 avmr.set(drawState, *matrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000793 }
794
795 target->drawNonIndexed(primType, 0, vertCount);
796 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000797#if GR_STATIC_RECT_VB
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000798 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
799 if (NULL == sqVB) {
800 GrPrintf("Failed to create static rect vb.\n");
801 return;
802 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000803 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000804 GrDrawState* drawState = target->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000805 GrMatrix m;
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000806 m.setAll(rect.width(), 0, rect.fLeft,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000807 0, rect.height(), rect.fTop,
808 0, 0, GrMatrix::I()[8]);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000809
810 if (NULL != matrix) {
811 m.postConcat(*matrix);
812 }
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000813 GrDrawState::AutoViewMatrixRestore avmr(drawState, m);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000814
bsalomon@google.com47059542012-06-06 20:51:20 +0000815 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000816#else
bsalomon@google.come3d32162012-07-20 13:37:06 +0000817 target->drawSimpleRect(rect, matrix);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000818#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000819 }
820}
821
822void GrContext::drawRectToRect(const GrPaint& paint,
823 const GrRect& dstRect,
824 const GrRect& srcRect,
825 const GrMatrix* dstMatrix,
826 const GrMatrix* srcMatrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000827 SK_TRACE_EVENT0("GrContext::drawRectToRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000828
bsalomon@google.com88becf42012-10-05 14:54:42 +0000829 // srcRect refers to paint's first color stage
830 if (!paint.isColorStageEnabled(0)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000831 drawRect(paint, dstRect, -1, dstMatrix);
832 return;
833 }
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000834
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000835 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000836
837#if GR_STATIC_RECT_VB
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000838 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000839 GrDrawState* drawState = target->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000840
841 GrMatrix m;
842
843 m.setAll(dstRect.width(), 0, dstRect.fLeft,
844 0, dstRect.height(), dstRect.fTop,
845 0, 0, GrMatrix::I()[8]);
846 if (NULL != dstMatrix) {
847 m.postConcat(*dstMatrix);
848 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000849
bsalomon@google.com2fdcdeb2012-10-08 17:15:55 +0000850 // The first color stage's coords come from srcRect rather than applying a matrix to dstRect.
851 // We explicitly compute a matrix for that stage below, no need to adjust here.
852 static const uint32_t kExplicitCoordMask = 1 << GrPaint::kFirstColorStage;
853 GrDrawState::AutoViewMatrixRestore avmr(drawState, m, kExplicitCoordMask);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000854
bsalomon@google.com27847de2011-02-22 20:59:41 +0000855 m.setAll(srcRect.width(), 0, srcRect.fLeft,
856 0, srcRect.height(), srcRect.fTop,
857 0, 0, GrMatrix::I()[8]);
858 if (NULL != srcMatrix) {
859 m.postConcat(*srcMatrix);
860 }
robertphillips@google.comee0b6932012-10-18 00:17:53 +0000861
862 drawState->sampler(GrPaint::kFirstColorStage)->preConcatCoordChange(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000863
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000864 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
865 if (NULL == sqVB) {
866 GrPrintf("Failed to create static rect vb.\n");
867 return;
868 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000869 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com47059542012-06-06 20:51:20 +0000870 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000871#else
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000872 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000873
tomhudson@google.com93813632011-10-27 20:21:16 +0000874 const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
875 const GrMatrix* srcMatrices[GrDrawState::kNumStages] = {NULL};
bsalomon@google.com27847de2011-02-22 20:59:41 +0000876 srcRects[0] = &srcRect;
877 srcMatrices[0] = srcMatrix;
878
bsalomon@google.come3d32162012-07-20 13:37:06 +0000879 target->drawRect(dstRect, dstMatrix, srcRects, srcMatrices);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000880#endif
881}
882
883void GrContext::drawVertices(const GrPaint& paint,
884 GrPrimitiveType primitiveType,
885 int vertexCount,
886 const GrPoint positions[],
887 const GrPoint texCoords[],
888 const GrColor colors[],
889 const uint16_t indices[],
890 int indexCount) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000891 SK_TRACE_EVENT0("GrContext::drawVertices");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000892
893 GrDrawTarget::AutoReleaseGeometry geo;
894
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000895 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000896 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000897
bsalomon@google.come3d32162012-07-20 13:37:06 +0000898 GrVertexLayout layout = 0;
899 if (NULL != texCoords) {
900 layout |= GrDrawTarget::StageTexCoordVertexLayoutBit(0, 0);
901 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000902 if (NULL != colors) {
903 layout |= GrDrawTarget::kColor_VertexLayoutBit;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000904 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000905 int vertexSize = GrDrawTarget::VertexSize(layout);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000906
907 if (sizeof(GrPoint) != vertexSize) {
908 if (!geo.set(target, layout, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000909 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000910 return;
911 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000912 int texOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000913 int colorOffset;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000914 GrDrawTarget::VertexSizeAndOffsetsByIdx(layout,
915 texOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000916 &colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000917 NULL,
918 NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000919 void* curVertex = geo.vertices();
920
921 for (int i = 0; i < vertexCount; ++i) {
922 *((GrPoint*)curVertex) = positions[i];
923
924 if (texOffsets[0] > 0) {
925 *(GrPoint*)((intptr_t)curVertex + texOffsets[0]) = texCoords[i];
926 }
927 if (colorOffset > 0) {
928 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
929 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000930 curVertex = (void*)((intptr_t)curVertex + vertexSize);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000931 }
932 } else {
933 target->setVertexSourceToArray(layout, positions, vertexCount);
934 }
935
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000936 // we don't currently apply offscreen AA to this path. Need improved
bsalomon@google.com91958362011-06-13 17:58:13 +0000937 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000938
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000939 if (NULL != indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000940 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000941 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000942 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000943 target->drawNonIndexed(primitiveType, 0, vertexCount);
944 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000945}
946
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000947///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com150d2842012-01-12 20:19:56 +0000948namespace {
949
bsalomon@google.com93c96602012-04-27 13:05:21 +0000950struct CircleVertex {
951 GrPoint fPos;
952 GrPoint fCenter;
953 GrScalar fOuterRadius;
954 GrScalar fInnerRadius;
955};
956
957/* Returns true if will map a circle to another circle. This can be true
958 * if the matrix only includes square-scale, rotation, translation.
959 */
960inline bool isSimilarityTransformation(const SkMatrix& matrix,
961 SkScalar tol = SK_ScalarNearlyZero) {
962 if (matrix.isIdentity() || matrix.getType() == SkMatrix::kTranslate_Mask) {
963 return true;
964 }
965 if (matrix.hasPerspective()) {
966 return false;
967 }
968
969 SkScalar mx = matrix.get(SkMatrix::kMScaleX);
970 SkScalar sx = matrix.get(SkMatrix::kMSkewX);
971 SkScalar my = matrix.get(SkMatrix::kMScaleY);
972 SkScalar sy = matrix.get(SkMatrix::kMSkewY);
973
974 if (mx == 0 && sx == 0 && my == 0 && sy == 0) {
975 return false;
976 }
977
978 // it has scales or skews, but it could also be rotation, check it out.
979 SkVector vec[2];
980 vec[0].set(mx, sx);
981 vec[1].set(sy, my);
982
983 return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) &&
984 SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(),
985 SkScalarSquare(tol));
986}
987
988}
989
990// TODO: strokeWidth can't be larger than zero right now.
991// It will be fixed when drawPath() can handle strokes.
992void GrContext::drawOval(const GrPaint& paint,
993 const GrRect& rect,
994 SkScalar strokeWidth) {
bsalomon@google.com0982d352012-07-31 15:33:25 +0000995 GrAssert(strokeWidth <= 0);
996 if (!isSimilarityTransformation(this->getMatrix()) ||
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000997 !paint.isAntiAlias() ||
bsalomon@google.com93c96602012-04-27 13:05:21 +0000998 rect.height() != rect.width()) {
999 SkPath path;
1000 path.addOval(rect);
1001 GrPathFill fill = (strokeWidth == 0) ?
bsalomon@google.com0982d352012-07-31 15:33:25 +00001002 kHairLine_GrPathFill : kWinding_GrPathFill;
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +00001003 this->internalDrawPath(paint, path, fill);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001004 return;
1005 }
1006
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001007 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
1008
bsalomon@google.com0982d352012-07-31 15:33:25 +00001009 GrDrawState* drawState = target->drawState();
1010 GrDrawState::AutoStageDisable atr(fDrawState);
1011 const GrMatrix vm = drawState->getViewMatrix();
1012
bsalomon@google.com93c96602012-04-27 13:05:21 +00001013 const GrRenderTarget* rt = drawState->getRenderTarget();
1014 if (NULL == rt) {
1015 return;
1016 }
1017
bsalomon@google.com5b3e8902012-10-05 20:13:28 +00001018 GrDrawState::AutoDeviceCoordDraw adcd(drawState);
bsalomon@google.come3d32162012-07-20 13:37:06 +00001019 if (!adcd.succeeded()) {
1020 return;
1021 }
bsalomon@google.com93c96602012-04-27 13:05:21 +00001022
bsalomon@google.come3d32162012-07-20 13:37:06 +00001023 GrVertexLayout layout = GrDrawTarget::kEdge_VertexLayoutBit;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001024 GrAssert(sizeof(CircleVertex) == GrDrawTarget::VertexSize(layout));
1025
1026 GrPoint center = GrPoint::Make(rect.centerX(), rect.centerY());
1027 GrScalar radius = SkScalarHalf(rect.width());
1028
1029 vm.mapPoints(&center, 1);
1030 radius = vm.mapRadius(radius);
1031
1032 GrScalar outerRadius = radius;
1033 GrScalar innerRadius = 0;
1034 SkScalar halfWidth = 0;
1035 if (strokeWidth == 0) {
1036 halfWidth = SkScalarHalf(SK_Scalar1);
1037
1038 outerRadius += halfWidth;
1039 innerRadius = SkMaxScalar(0, radius - halfWidth);
1040 }
1041
1042 GrDrawTarget::AutoReleaseGeometry geo(target, layout, 4, 0);
1043 if (!geo.succeeded()) {
1044 GrPrintf("Failed to get space for vertices!\n");
1045 return;
1046 }
1047
1048 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
1049
robertphillips@google.coma0a66c12012-06-22 13:14:29 +00001050 // The fragment shader will extend the radius out half a pixel
1051 // to antialias. Expand the drawn rect here so all the pixels
1052 // will be captured.
1053 SkScalar L = center.fX - outerRadius - SkFloatToScalar(0.5f);
1054 SkScalar R = center.fX + outerRadius + SkFloatToScalar(0.5f);
1055 SkScalar T = center.fY - outerRadius - SkFloatToScalar(0.5f);
1056 SkScalar B = center.fY + outerRadius + SkFloatToScalar(0.5f);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001057
1058 verts[0].fPos = SkPoint::Make(L, T);
1059 verts[1].fPos = SkPoint::Make(R, T);
1060 verts[2].fPos = SkPoint::Make(L, B);
1061 verts[3].fPos = SkPoint::Make(R, B);
1062
1063 for (int i = 0; i < 4; ++i) {
1064 // this goes to fragment shader, it should be in y-points-up space.
1065 verts[i].fCenter = SkPoint::Make(center.fX, rt->height() - center.fY);
1066
1067 verts[i].fOuterRadius = outerRadius;
1068 verts[i].fInnerRadius = innerRadius;
1069 }
1070
1071 drawState->setVertexEdgeType(GrDrawState::kCircle_EdgeType);
bsalomon@google.com47059542012-06-06 20:51:20 +00001072 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001073}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001074
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +00001075void GrContext::drawPath(const GrPaint& paint, const SkPath& path, GrPathFill fill) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001076
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001077 if (path.isEmpty()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001078 if (GrIsFillInverted(fill)) {
1079 this->drawPaint(paint);
1080 }
1081 return;
1082 }
1083
bsalomon@google.com93c96602012-04-27 13:05:21 +00001084 SkRect ovalRect;
1085 if (!GrIsFillInverted(fill) && path.isOval(&ovalRect)) {
bsalomon@google.com47059542012-06-06 20:51:20 +00001086 SkScalar width = (fill == kHairLine_GrPathFill) ? 0 : -SK_Scalar1;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001087 this->drawOval(paint, ovalRect, width);
1088 return;
1089 }
1090
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +00001091 this->internalDrawPath(paint, path, fill);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001092}
1093
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +00001094void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path, GrPathFill fill) {
bsalomon@google.com93c96602012-04-27 13:05:21 +00001095
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001096 // Note that below we may sw-rasterize the path into a scratch texture.
1097 // Scratch textures can be recycled after they are returned to the texture
1098 // cache. This presents a potential hazard for buffered drawing. However,
1099 // the writePixels that uploads to the scratch will perform a flush so we're
1100 // OK.
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001101 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +00001102 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001103
bsalomon@google.comc7448ce2012-10-05 19:04:13 +00001104 bool prAA = paint.isAntiAlias() && !this->getRenderTarget()->isMultisampled();
bsalomon@google.com289533a2011-10-27 12:34:25 +00001105
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001106 // An Assumption here is that path renderer would use some form of tweaking
1107 // the src color (either the input alpha or in the frag shader) to implement
1108 // aa. If we have some future driver-mojo path AA that can do the right
1109 // thing WRT to the blend then we'll need some query on the PR.
1110 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001111#if GR_DEBUG
bsalomon@google.com979432b2011-11-05 21:38:22 +00001112 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001113#endif
bsalomon@google.com289533a2011-10-27 12:34:25 +00001114 prAA = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001115 }
bsalomon@google.com289533a2011-10-27 12:34:25 +00001116
robertphillips@google.com72176b22012-05-23 13:19:12 +00001117 GrPathRenderer* pr = this->getPathRenderer(path, fill, target, prAA, true);
bsalomon@google.com30085192011-08-19 15:42:31 +00001118 if (NULL == pr) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001119#if GR_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001120 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001121#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001122 return;
1123 }
1124
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +00001125 pr->drawPath(path, fill, target, prAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001126}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001127
bsalomon@google.com27847de2011-02-22 20:59:41 +00001128////////////////////////////////////////////////////////////////////////////////
1129
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001130void GrContext::flush(int flagsBitfield) {
1131 if (kDiscard_FlushBit & flagsBitfield) {
1132 fDrawBuffer->reset();
1133 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001134 this->flushDrawBuffer();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001135 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001136 if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001137 fGpu->forceRenderTargetFlush();
1138 }
1139}
1140
bsalomon@google.com27847de2011-02-22 20:59:41 +00001141void GrContext::flushDrawBuffer() {
junov@google.com53a55842011-06-08 22:55:10 +00001142 if (fDrawBuffer) {
robertphillips@google.com58b38182012-05-03 16:29:41 +00001143 // With addition of the AA clip path, flushing the draw buffer can
1144 // result in the generation of an AA clip mask. During this
1145 // process the SW path renderer may be invoked which recusively
1146 // calls this method (via internalWriteTexturePixels) creating
1147 // infinite recursion
1148 GrInOrderDrawBuffer* temp = fDrawBuffer;
1149 fDrawBuffer = NULL;
1150
1151 temp->flushTo(fGpu);
1152
1153 fDrawBuffer = temp;
junov@google.com53a55842011-06-08 22:55:10 +00001154 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001155}
1156
bsalomon@google.com0342a852012-08-20 19:22:38 +00001157void GrContext::writeTexturePixels(GrTexture* texture,
1158 int left, int top, int width, int height,
1159 GrPixelConfig config, const void* buffer, size_t rowBytes,
1160 uint32_t flags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001161 SK_TRACE_EVENT0("GrContext::writeTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001162 ASSERT_OWNED_RESOURCE(texture);
1163
bsalomon@google.com0342a852012-08-20 19:22:38 +00001164 // TODO: use scratch texture to perform conversion
1165 if (kUnpremul_PixelOpsFlag & flags) {
1166 return;
1167 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001168 if (!(kDontFlush_PixelOpsFlag & flags)) {
1169 this->flush();
1170 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001171
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001172 fGpu->writeTexturePixels(texture, left, top, width, height,
bsalomon@google.com6f379512011-11-16 20:36:03 +00001173 config, buffer, rowBytes);
1174}
1175
bsalomon@google.com0342a852012-08-20 19:22:38 +00001176bool GrContext::readTexturePixels(GrTexture* texture,
1177 int left, int top, int width, int height,
1178 GrPixelConfig config, void* buffer, size_t rowBytes,
1179 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001180 SK_TRACE_EVENT0("GrContext::readTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001181 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001182
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001183 // TODO: code read pixels for textures that aren't also rendertargets
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001184 GrRenderTarget* target = texture->asRenderTarget();
1185 if (NULL != target) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001186 return this->readRenderTargetPixels(target,
1187 left, top, width, height,
1188 config, buffer, rowBytes,
1189 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001190 } else {
1191 return false;
1192 }
1193}
1194
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001195#include "SkConfig8888.h"
1196
1197namespace {
1198/**
1199 * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel
1200 * formats are representable as Config8888 and so the function returns false
1201 * if the GrPixelConfig has no equivalent Config8888.
1202 */
1203bool grconfig_to_config8888(GrPixelConfig config,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001204 bool unpremul,
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001205 SkCanvas::Config8888* config8888) {
1206 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001207 case kRGBA_8888_GrPixelConfig:
1208 if (unpremul) {
1209 *config8888 = SkCanvas::kRGBA_Unpremul_Config8888;
1210 } else {
1211 *config8888 = SkCanvas::kRGBA_Premul_Config8888;
1212 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001213 return true;
bsalomon@google.com0342a852012-08-20 19:22:38 +00001214 case kBGRA_8888_GrPixelConfig:
1215 if (unpremul) {
1216 *config8888 = SkCanvas::kBGRA_Unpremul_Config8888;
1217 } else {
1218 *config8888 = SkCanvas::kBGRA_Premul_Config8888;
1219 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001220 return true;
1221 default:
1222 return false;
1223 }
1224}
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001225
1226// It returns a configuration with where the byte position of the R & B components are swapped in
1227// relation to the input config. This should only be called with the result of
1228// grconfig_to_config8888 as it will fail for other configs.
1229SkCanvas::Config8888 swap_config8888_red_and_blue(SkCanvas::Config8888 config8888) {
1230 switch (config8888) {
1231 case SkCanvas::kBGRA_Premul_Config8888:
1232 return SkCanvas::kRGBA_Premul_Config8888;
1233 case SkCanvas::kBGRA_Unpremul_Config8888:
1234 return SkCanvas::kRGBA_Unpremul_Config8888;
1235 case SkCanvas::kRGBA_Premul_Config8888:
1236 return SkCanvas::kBGRA_Premul_Config8888;
1237 case SkCanvas::kRGBA_Unpremul_Config8888:
1238 return SkCanvas::kBGRA_Unpremul_Config8888;
1239 default:
1240 GrCrash("Unexpected input");
1241 return SkCanvas::kBGRA_Unpremul_Config8888;;
1242 }
1243}
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001244}
1245
bsalomon@google.com0342a852012-08-20 19:22:38 +00001246bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
1247 int left, int top, int width, int height,
1248 GrPixelConfig config, void* buffer, size_t rowBytes,
1249 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001250 SK_TRACE_EVENT0("GrContext::readRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001251 ASSERT_OWNED_RESOURCE(target);
1252
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001253 if (NULL == target) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001254 target = fDrawState->getRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001255 if (NULL == target) {
1256 return false;
1257 }
1258 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001259
bsalomon@google.com6f379512011-11-16 20:36:03 +00001260 if (!(kDontFlush_PixelOpsFlag & flags)) {
1261 this->flush();
1262 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001263
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001264 // Determine which conversions have to be applied: flipY, swapRAnd, and/or unpremul.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001265
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001266 // If fGpu->readPixels would incur a y-flip cost then we will read the pixels upside down. We'll
1267 // either do the flipY by drawing into a scratch with a matrix or on the cpu after the read.
1268 bool flipY = fGpu->readPixelsWillPayForYFlip(target, left, top,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001269 width, height, config,
1270 rowBytes);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001271 bool swapRAndB = fGpu->preferredReadPixelsConfig(config) == GrPixelConfigSwapRAndB(config);
1272
bsalomon@google.com0342a852012-08-20 19:22:38 +00001273 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001274
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001275 // flipY will get set to false when it is handled below using a scratch. However, in that case
1276 // we still want to do the read upside down.
1277 bool readUpsideDown = flipY;
1278
1279 if (unpremul && kRGBA_8888_GrPixelConfig != config && kBGRA_8888_GrPixelConfig != config) {
1280 // The unpremul flag is only allowed for these two configs.
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001281 return false;
1282 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001283
1284 GrPixelConfig readConfig;
1285 if (swapRAndB) {
1286 readConfig = GrPixelConfigSwapRAndB(config);
1287 GrAssert(kUnknown_GrPixelConfig != config);
1288 } else {
1289 readConfig = config;
1290 }
1291
1292 // If the src is a texture and we would have to do conversions after read pixels, we instead
1293 // do the conversions by drawing the src to a scratch texture. If we handle any of the
1294 // conversions in the draw we set the corresponding bool to false so that we don't reapply it
1295 // on the read back pixels.
1296 GrTexture* src = target->asTexture();
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001297 GrAutoScratchTexture ast;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001298 if (NULL != src && (swapRAndB || unpremul || flipY)) {
1299 // Make the scratch a render target because we don't have a robust readTexturePixels as of
1300 // yet. It calls this function.
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001301 GrTextureDesc desc;
1302 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1303 desc.fWidth = width;
1304 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001305 desc.fConfig = readConfig;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001306
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001307 // When a full readback is faster than a partial we could always make the scratch exactly
1308 // match the passed rect. However, if we see many different size rectangles we will trash
1309 // our texture cache and pay the cost of creating and destroying many textures. So, we only
1310 // request an exact match when the caller is reading an entire RT.
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001311 ScratchTexMatch match = kApprox_ScratchTexMatch;
1312 if (0 == left &&
1313 0 == top &&
1314 target->width() == width &&
1315 target->height() == height &&
1316 fGpu->fullReadPixelsIsFasterThanPartial()) {
1317 match = kExact_ScratchTexMatch;
1318 }
1319 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001320 GrTexture* texture = ast.texture();
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001321 if (texture) {
1322 SkAutoTUnref<GrCustomStage> stage;
1323 if (unpremul) {
1324 stage.reset(this->createPMToUPMEffect(src, swapRAndB));
1325 }
1326 // If we failed to create a PM->UPM effect and have no other conversions to perform then
1327 // there is no longer any point to using the scratch.
1328 if (NULL != stage || flipY || swapRAndB) {
1329 if (NULL == stage) {
1330 stage.reset(GrConfigConversionEffect::Create(src, swapRAndB));
1331 GrAssert(NULL != stage);
1332 } else {
1333 unpremul = false; // we will handle the UPM conversion in the draw
1334 }
1335 swapRAndB = false; // we will handle the swap in the draw.
bsalomon@google.comc4364992011-11-07 15:54:49 +00001336
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001337 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
1338 GrDrawState* drawState = fGpu->drawState();
1339 drawState->setRenderTarget(texture->asRenderTarget());
1340 GrMatrix matrix;
1341 if (flipY) {
1342 matrix.setTranslate(SK_Scalar1 * left,
1343 SK_Scalar1 * (top + height));
1344 matrix.set(GrMatrix::kMScaleY, -GR_Scalar1);
1345 flipY = false; // the y flip will be handled in the draw
1346 } else {
1347 matrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
1348 }
1349 matrix.postIDiv(src->width(), src->height());
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +00001350 drawState->sampler(0)->setCustomStage(stage, matrix);
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001351 GrRect rect = GrRect::MakeWH(GrIntToScalar(width), GrIntToScalar(height));
1352 fGpu->drawSimpleRect(rect, NULL);
1353 // we want to read back from the scratch's origin
1354 left = 0;
1355 top = 0;
1356 target = texture->asRenderTarget();
1357 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001358 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001359 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001360 if (!fGpu->readPixels(target,
1361 left, top, width, height,
1362 readConfig, buffer, rowBytes, readUpsideDown)) {
1363 return false;
1364 }
1365 // Perform any conversions we weren't able to perfom using a scratch texture.
1366 if (unpremul || swapRAndB || flipY) {
bsalomon@google.comccaa0022012-09-25 19:55:07 +00001367 // These are initialized to suppress a warning
1368 SkCanvas::Config8888 srcC8888 = SkCanvas::kNative_Premul_Config8888;
1369 SkCanvas::Config8888 dstC8888 = SkCanvas::kNative_Premul_Config8888;
1370
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001371 bool c8888IsValid = grconfig_to_config8888(config, false, &srcC8888);
1372 grconfig_to_config8888(config, unpremul, &dstC8888);
bsalomon@google.comccaa0022012-09-25 19:55:07 +00001373
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001374 if (swapRAndB) {
1375 GrAssert(c8888IsValid); // we should only do r/b swap on 8888 configs
1376 srcC8888 = swap_config8888_red_and_blue(srcC8888);
1377 }
1378 if (flipY) {
1379 size_t tightRB = width * GrBytesPerPixel(config);
1380 if (0 == rowBytes) {
1381 rowBytes = tightRB;
1382 }
1383 SkAutoSTMalloc<256, uint8_t> tempRow(tightRB);
1384 intptr_t top = reinterpret_cast<intptr_t>(buffer);
1385 intptr_t bot = top + (height - 1) * rowBytes;
1386 while (top < bot) {
1387 uint32_t* t = reinterpret_cast<uint32_t*>(top);
1388 uint32_t* b = reinterpret_cast<uint32_t*>(bot);
1389 uint32_t* temp = reinterpret_cast<uint32_t*>(tempRow.get());
1390 memcpy(temp, t, tightRB);
1391 if (c8888IsValid) {
1392 SkConvertConfig8888Pixels(t, tightRB, dstC8888,
1393 b, tightRB, srcC8888,
1394 width, 1);
1395 SkConvertConfig8888Pixels(b, tightRB, dstC8888,
1396 temp, tightRB, srcC8888,
1397 width, 1);
1398 } else {
1399 memcpy(t, b, tightRB);
1400 memcpy(b, temp, tightRB);
1401 }
1402 top += rowBytes;
1403 bot -= rowBytes;
1404 }
1405 // The above loop does nothing on the middle row when height is odd.
1406 if (top == bot && c8888IsValid && dstC8888 != srcC8888) {
1407 uint32_t* mid = reinterpret_cast<uint32_t*>(top);
1408 SkConvertConfig8888Pixels(mid, tightRB, dstC8888, mid, tightRB, srcC8888, width, 1);
1409 }
1410 } else {
1411 // if we aren't flipping Y then we have no reason to be here other than doing
1412 // conversions for 8888 (r/b swap or upm).
1413 GrAssert(c8888IsValid);
1414 uint32_t* b32 = reinterpret_cast<uint32_t*>(buffer);
1415 SkConvertConfig8888Pixels(b32, rowBytes, dstC8888,
1416 b32, rowBytes, srcC8888,
1417 width, height);
1418 }
1419 }
1420 return true;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001421}
1422
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001423void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1424 GrAssert(target);
1425 ASSERT_OWNED_RESOURCE(target);
1426 // In the future we may track whether there are any pending draws to this
1427 // target. We don't today so we always perform a flush. We don't promise
1428 // this to our clients, though.
1429 this->flush();
1430 fGpu->resolveRenderTarget(target);
1431}
1432
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001433void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst) {
1434 if (NULL == src || NULL == dst) {
1435 return;
1436 }
1437 ASSERT_OWNED_RESOURCE(src);
1438
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001439 // Writes pending to the source texture are not tracked, so a flush
1440 // is required to ensure that the copy captures the most recent contents
1441 // of the source texture. See similar behaviour in
1442 // GrContext::resolveRenderTarget.
1443 this->flush();
1444
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001445 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001446 GrDrawState* drawState = fGpu->drawState();
1447 drawState->setRenderTarget(dst);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001448 GrMatrix sampleM;
1449 sampleM.setIDiv(src->width(), src->height());
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +00001450 drawState->createTextureEffect(0, src, sampleM);
bsalomon@google.com5db3b6c2012-01-12 20:38:57 +00001451 SkRect rect = SkRect::MakeXYWH(0, 0,
1452 SK_Scalar1 * src->width(),
1453 SK_Scalar1 * src->height());
bsalomon@google.come3d32162012-07-20 13:37:06 +00001454 fGpu->drawSimpleRect(rect, NULL);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001455}
1456
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001457void GrContext::writeRenderTargetPixels(GrRenderTarget* target,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001458 int left, int top, int width, int height,
1459 GrPixelConfig config,
1460 const void* buffer,
1461 size_t rowBytes,
1462 uint32_t flags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001463 SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001464 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001465
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001466 if (NULL == target) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001467 target = fDrawState->getRenderTarget();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001468 if (NULL == target) {
1469 return;
1470 }
1471 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001472
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001473 // TODO: when underlying api has a direct way to do this we should use it (e.g. glDrawPixels on
1474 // desktop GL).
1475
1476 // We will always call some form of writeTexturePixels and we will pass our flags on to it.
1477 // Thus, we don't perform a flush here since that call will do it (if the kNoFlush flag isn't
1478 // set.)
bsalomon@google.com27847de2011-02-22 20:59:41 +00001479
bsalomon@google.com0342a852012-08-20 19:22:38 +00001480 // If the RT is also a texture and we don't have to premultiply then take the texture path.
1481 // We expect to be at least as fast or faster since it doesn't use an intermediate texture as
1482 // we do below.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001483
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001484#if !GR_MAC_BUILD
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001485 // At least some drivers on the Mac get confused when glTexImage2D is called on a texture
1486 // attached to an FBO. The FBO still sees the old image. TODO: determine what OS versions and/or
1487 // HW is affected.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001488 if (NULL != target->asTexture() && !(kUnpremul_PixelOpsFlag & flags)) {
1489 this->writeTexturePixels(target->asTexture(),
1490 left, top, width, height,
1491 config, buffer, rowBytes, flags);
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001492 return;
1493 }
1494#endif
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001495 SkAutoTUnref<GrCustomStage> stage;
1496 bool swapRAndB = (fGpu->preferredReadPixelsConfig(config) == GrPixelConfigSwapRAndB(config));
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001497
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001498 GrPixelConfig textureConfig;
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001499 if (swapRAndB) {
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001500 textureConfig = GrPixelConfigSwapRAndB(config);
1501 } else {
1502 textureConfig = config;
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001503 }
1504
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001505 GrTextureDesc desc;
1506 desc.fWidth = width;
1507 desc.fHeight = height;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001508 desc.fConfig = textureConfig;
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001509 GrAutoScratchTexture ast(this, desc);
1510 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001511 if (NULL == texture) {
1512 return;
1513 }
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001514 // allocate a tmp buffer and sw convert the pixels to premul
1515 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(0);
1516
1517 if (kUnpremul_PixelOpsFlag & flags) {
1518 if (kRGBA_8888_GrPixelConfig != config && kBGRA_8888_GrPixelConfig != config) {
1519 return;
1520 }
1521 stage.reset(this->createUPMToPMEffect(texture, swapRAndB));
1522 if (NULL == stage) {
1523 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1524 GR_DEBUGCODE(bool success = )
1525 grconfig_to_config8888(config, true, &srcConfig8888);
1526 GrAssert(success);
1527 GR_DEBUGCODE(success = )
1528 grconfig_to_config8888(config, false, &dstConfig8888);
1529 GrAssert(success);
1530 const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer);
1531 tmpPixels.reset(width * height);
1532 SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888,
1533 src, rowBytes, srcConfig8888,
1534 width, height);
1535 buffer = tmpPixels.get();
1536 rowBytes = 4 * width;
1537 }
1538 }
1539 if (NULL == stage) {
1540 stage.reset(GrConfigConversionEffect::Create(texture, swapRAndB));
1541 GrAssert(NULL != stage);
1542 }
1543
1544 this->writeTexturePixels(texture,
1545 0, 0, width, height,
1546 textureConfig, buffer, rowBytes,
1547 flags & ~kUnpremul_PixelOpsFlag);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001548
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001549 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001550 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001551
1552 GrMatrix matrix;
1553 matrix.setTranslate(GrIntToScalar(left), GrIntToScalar(top));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001554 drawState->setViewMatrix(matrix);
1555 drawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001556
bsalomon@google.com5c638652011-07-18 19:31:59 +00001557 matrix.setIDiv(texture->width(), texture->height());
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +00001558 drawState->sampler(0)->setCustomStage(stage, matrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001559
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001560 fGpu->drawSimpleRect(GrRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)), NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001561}
1562////////////////////////////////////////////////////////////////////////////////
1563
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001564GrDrawTarget* GrContext::prepareToDraw(const GrPaint* paint, BufferedDraw buffered) {
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001565 if (kNo_BufferedDraw == buffered && kYes_BufferedDraw == fLastDrawWasBuffered) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001566 this->flushDrawBuffer();
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001567 fLastDrawWasBuffered = kNo_BufferedDraw;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001568 }
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001569 if (NULL != paint) {
bsalomon@google.comaf84e742012-10-05 13:23:24 +00001570 GrAssert(fDrawState->stagesDisabled());
1571 fDrawState->setFromPaint(*paint);
1572#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
1573 if ((paint->hasMask() || 0xff != paint->fCoverage) &&
1574 !fGpu->canApplyCoverage()) {
1575 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1576 }
1577#endif
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001578 }
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001579 if (kYes_BufferedDraw == buffered) {
1580 fDrawBuffer->setClip(fGpu->getClip());
1581 fLastDrawWasBuffered = kYes_BufferedDraw;
1582 return fDrawBuffer;
1583 } else {
1584 GrAssert(kNo_BufferedDraw == buffered);
1585 return fGpu;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001586 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001587}
1588
robertphillips@google.com72176b22012-05-23 13:19:12 +00001589/*
1590 * This method finds a path renderer that can draw the specified path on
1591 * the provided target.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001592 * Due to its expense, the software path renderer has split out so it can
robertphillips@google.com72176b22012-05-23 13:19:12 +00001593 * can be individually allowed/disallowed via the "allowSW" boolean.
1594 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001595GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
bsalomon@google.com289533a2011-10-27 12:34:25 +00001596 GrPathFill fill,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001597 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001598 bool antiAlias,
1599 bool allowSW) {
bsalomon@google.com30085192011-08-19 15:42:31 +00001600 if (NULL == fPathRendererChain) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001601 fPathRendererChain =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001602 SkNEW_ARGS(GrPathRendererChain,
1603 (this, GrPathRendererChain::kNone_UsageFlag));
bsalomon@google.com30085192011-08-19 15:42:31 +00001604 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001605
1606 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path, fill,
1607 target,
1608 antiAlias);
1609
1610 if (NULL == pr && allowSW) {
1611 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001612 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001613 }
1614
1615 pr = fSoftwarePathRenderer;
1616 }
1617
1618 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001619}
1620
bsalomon@google.com27847de2011-02-22 20:59:41 +00001621////////////////////////////////////////////////////////////////////////////////
1622
bsalomon@google.com27847de2011-02-22 20:59:41 +00001623void GrContext::setRenderTarget(GrRenderTarget* target) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001624 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001625 fDrawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001626}
1627
1628GrRenderTarget* GrContext::getRenderTarget() {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001629 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001630}
1631
1632const GrRenderTarget* GrContext::getRenderTarget() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001633 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001634}
1635
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001636bool GrContext::isConfigRenderable(GrPixelConfig config) const {
1637 return fGpu->isConfigRenderable(config);
1638}
1639
bsalomon@google.com27847de2011-02-22 20:59:41 +00001640const GrMatrix& GrContext::getMatrix() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001641 return fDrawState->getViewMatrix();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001642}
1643
1644void GrContext::setMatrix(const GrMatrix& m) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001645 fDrawState->setViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001646}
1647
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +00001648void GrContext::setIdentityMatrix() {
1649 fDrawState->viewMatrix()->reset();
1650}
1651
bsalomon@google.com27847de2011-02-22 20:59:41 +00001652void GrContext::concatMatrix(const GrMatrix& m) const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001653 fDrawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001654}
1655
1656static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
1657 intptr_t mask = 1 << shift;
1658 if (pred) {
1659 bits |= mask;
1660 } else {
1661 bits &= ~mask;
1662 }
1663 return bits;
1664}
1665
bsalomon@google.com583a1e32011-08-17 13:42:46 +00001666GrContext::GrContext(GrGpu* gpu) {
bsalomon@google.comc0af3172012-06-15 14:10:09 +00001667 ++THREAD_INSTANCE_COUNT;
1668
bsalomon@google.com27847de2011-02-22 20:59:41 +00001669 fGpu = gpu;
1670 fGpu->ref();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001671 fGpu->setContext(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001672
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001673 fDrawState = SkNEW(GrDrawState);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001674 fGpu->setDrawState(fDrawState);
1675
bsalomon@google.com30085192011-08-19 15:42:31 +00001676 fPathRendererChain = NULL;
robertphillips@google.com72176b22012-05-23 13:19:12 +00001677 fSoftwarePathRenderer = NULL;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001678
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001679 fTextureCache = SkNEW_ARGS(GrResourceCache,
1680 (MAX_TEXTURE_CACHE_COUNT,
1681 MAX_TEXTURE_CACHE_BYTES));
1682 fFontCache = SkNEW_ARGS(GrFontCache, (fGpu));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001683
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001684 fLastDrawWasBuffered = kNo_BufferedDraw;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001685
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001686 fDrawBuffer = NULL;
1687 fDrawBufferVBAllocPool = NULL;
1688 fDrawBufferIBAllocPool = NULL;
1689
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001690 fAARectRenderer = SkNEW(GrAARectRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001691
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001692 fDidTestPMConversions = false;
1693
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001694 this->setupDrawBuffer();
1695}
1696
1697void GrContext::setupDrawBuffer() {
1698
1699 GrAssert(NULL == fDrawBuffer);
1700 GrAssert(NULL == fDrawBufferVBAllocPool);
1701 GrAssert(NULL == fDrawBufferIBAllocPool);
1702
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001703 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001704 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001705 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001706 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001707 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001708 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001709 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001710 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001711
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001712 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com471d4712011-08-23 15:45:25 +00001713 fDrawBufferVBAllocPool,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001714 fDrawBufferIBAllocPool));
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001715
bsalomon@google.com27847de2011-02-22 20:59:41 +00001716 fDrawBuffer->setQuadIndexBuffer(this->getQuadIndexBuffer());
bsalomon@google.com1015e032012-06-25 18:41:04 +00001717 if (fDrawBuffer) {
1718 fDrawBuffer->setAutoFlushTarget(fGpu);
1719 fDrawBuffer->setDrawState(fDrawState);
1720 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001721}
1722
bsalomon@google.com27847de2011-02-22 20:59:41 +00001723GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) {
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001724 return prepareToDraw(&paint, DEFAULT_BUFFERING);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001725}
1726
1727const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1728 return fGpu->getQuadIndexBuffer();
1729}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001730
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001731namespace {
1732void test_pm_conversions(GrContext* ctx, int* pmToUPMValue, int* upmToPMValue) {
1733 GrConfigConversionEffect::PMConversion pmToUPM;
1734 GrConfigConversionEffect::PMConversion upmToPM;
1735 GrConfigConversionEffect::TestForPreservingPMConversions(ctx, &pmToUPM, &upmToPM);
1736 *pmToUPMValue = pmToUPM;
1737 *upmToPMValue = upmToPM;
1738}
1739}
1740
1741GrCustomStage* GrContext::createPMToUPMEffect(GrTexture* texture, bool swapRAndB) {
1742 if (!fDidTestPMConversions) {
1743 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001744 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001745 }
1746 GrConfigConversionEffect::PMConversion pmToUPM =
1747 static_cast<GrConfigConversionEffect::PMConversion>(fPMToUPMConversion);
1748 if (GrConfigConversionEffect::kNone_PMConversion != pmToUPM) {
1749 return GrConfigConversionEffect::Create(texture, swapRAndB, pmToUPM);
1750 } else {
1751 return NULL;
1752 }
1753}
1754
1755GrCustomStage* GrContext::createUPMToPMEffect(GrTexture* texture, bool swapRAndB) {
1756 if (!fDidTestPMConversions) {
1757 test_pm_conversions(this, &fPMToUPMConversion, &fUPMToPMConversion);
bsalomon@google.comd0f3f682012-08-28 13:08:14 +00001758 fDidTestPMConversions = true;
bsalomon@google.coma04e8e82012-08-27 12:53:13 +00001759 }
1760 GrConfigConversionEffect::PMConversion upmToPM =
1761 static_cast<GrConfigConversionEffect::PMConversion>(fUPMToPMConversion);
1762 if (GrConfigConversionEffect::kNone_PMConversion != upmToPM) {
1763 return GrConfigConversionEffect::Create(texture, swapRAndB, upmToPM);
1764 } else {
1765 return NULL;
1766 }
1767}
1768
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001769GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture,
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001770 bool canClobberSrc,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001771 const SkRect& rect,
1772 float sigmaX, float sigmaY) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001773 ASSERT_OWNED_RESOURCE(srcTexture);
robertphillips@google.comccb39502012-10-01 18:25:13 +00001774
1775 AutoRenderTarget art(this);
1776
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001777 AutoMatrix am;
1778 am.setIdentity(this);
1779
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001780 SkIRect clearRect;
bsalomon@google.comb505a122012-05-31 18:40:36 +00001781 int scaleFactorX, radiusX;
1782 int scaleFactorY, radiusY;
1783 sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX);
1784 sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY);
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001785
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001786 SkRect srcRect(rect);
1787 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
1788 srcRect.roundOut();
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001789 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001790 static_cast<float>(scaleFactorY));
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001791
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001792 AutoClip acs(this, srcRect);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001793
bsalomon@google.com0342a852012-08-20 19:22:38 +00001794 GrAssert(kBGRA_8888_GrPixelConfig == srcTexture->config() ||
1795 kRGBA_8888_GrPixelConfig == srcTexture->config() ||
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001796 kAlpha_8_GrPixelConfig == srcTexture->config());
1797
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001798 GrTextureDesc desc;
1799 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1800 desc.fWidth = SkScalarFloorToInt(srcRect.width());
1801 desc.fHeight = SkScalarFloorToInt(srcRect.height());
1802 desc.fConfig = srcTexture->config();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001803
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001804 GrAutoScratchTexture temp1, temp2;
1805 GrTexture* dstTexture = temp1.set(this, desc);
1806 GrTexture* tempTexture = canClobberSrc ? srcTexture : temp2.set(this, desc);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001807
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001808 GrPaint paint;
1809 paint.reset();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001810
1811 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +00001812 GrMatrix matrix;
1813 matrix.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001814 this->setRenderTarget(dstTexture->asRenderTarget());
1815 SkRect dstRect(srcRect);
1816 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +00001817 i < scaleFactorY ? 0.5f : 1.0f);
1818
bsalomon@google.com88becf42012-10-05 14:54:42 +00001819 paint.colorSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +00001820 (srcTexture, true)), matrix)->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001821 this->drawRectToRect(paint, dstRect, srcRect);
1822 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001823 srcTexture = dstTexture;
1824 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001825 }
1826
robertphillips@google.com7a396332012-05-10 15:11:27 +00001827 SkIRect srcIRect;
1828 srcRect.roundOut(&srcIRect);
1829
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001830 if (sigmaX > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001831 if (scaleFactorX > 1) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001832 // Clear out a radius to the right of the srcRect to prevent the
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001833 // X convolution from reading garbage.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001834 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001835 radiusX, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001836 this->clear(&clearRect, 0x0);
1837 }
1838
1839 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001840 GrDrawTarget* target = this->prepareToDraw(NULL, DEFAULT_BUFFERING);
1841 convolve_gaussian(target, srcTexture, srcRect, sigmaX, radiusX,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001842 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001843 srcTexture = dstTexture;
1844 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001845 }
1846
1847 if (sigmaY > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001848 if (scaleFactorY > 1 || sigmaX > 0.0f) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001849 // Clear out a radius below the srcRect to prevent the Y
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001850 // convolution from reading garbage.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001851 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001852 srcIRect.width(), radiusY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001853 this->clear(&clearRect, 0x0);
1854 }
1855
1856 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001857 GrDrawTarget* target = this->prepareToDraw(NULL, DEFAULT_BUFFERING);
1858 convolve_gaussian(target, srcTexture, srcRect, sigmaY, radiusY,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001859 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001860 srcTexture = dstTexture;
1861 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001862 }
1863
1864 if (scaleFactorX > 1 || scaleFactorY > 1) {
1865 // Clear one pixel to the right and below, to accommodate bilinear
1866 // upsampling.
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001867 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
robertphillips@google.com7a396332012-05-10 15:11:27 +00001868 srcIRect.width() + 1, 1);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001869 this->clear(&clearRect, 0x0);
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001870 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
robertphillips@google.com7a396332012-05-10 15:11:27 +00001871 1, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001872 this->clear(&clearRect, 0x0);
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +00001873 GrMatrix matrix;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001874 // FIXME: This should be mitchell, not bilinear.
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +00001875 matrix.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001876 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com88becf42012-10-05 14:54:42 +00001877 paint.colorSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +00001878 (srcTexture, true)),
1879 matrix)->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001880 SkRect dstRect(srcRect);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001881 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001882 this->drawRectToRect(paint, dstRect, srcRect);
1883 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001884 srcTexture = dstTexture;
1885 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001886 }
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001887 if (srcTexture == temp1.texture()) {
1888 return temp1.detach();
1889 } else if (srcTexture == temp2.texture()) {
1890 return temp2.detach();
1891 } else {
1892 srcTexture->ref();
1893 return srcTexture;
1894 }
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001895}
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001896
bsalomon@google.comc4364992011-11-07 15:54:49 +00001897///////////////////////////////////////////////////////////////////////////////
robertphillips@google.com59552022012-08-31 13:07:37 +00001898#if GR_CACHE_STATS
robertphillips@google.com5f9f2f52012-08-22 10:57:05 +00001899void GrContext::printCacheStats() const {
1900 fTextureCache->printStats();
1901}
1902#endif