blob: 49d220f53728c2037fbab50c658bf1e82d9b434e [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/GrMorphologyEffect.h"
13#include "effects/GrConvolutionEffect.h"
tomhudson@google.comaa72eab2012-07-19 18:01:07 +000014#include "effects/GrSingleTextureEffect.h"
bsalomon@google.comb505a122012-05-31 18:40:36 +000015
tomhudson@google.com278cbb42011-06-30 19:37:01 +000016#include "GrBufferAllocPool.h"
bsalomon@google.com05ef5102011-05-02 21:14:59 +000017#include "GrGpu.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000018#include "GrIndexBuffer.h"
19#include "GrInOrderDrawBuffer.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000020#include "GrPathRenderer.h"
tomhudson@google.comd22b6e42011-06-24 15:53:40 +000021#include "GrPathUtils.h"
bsalomon@google.com50398bf2011-07-26 20:45:30 +000022#include "GrResourceCache.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000023#include "GrSoftwarePathRenderer.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000024#include "GrStencilBuffer.h"
tomhudson@google.com278cbb42011-06-30 19:37:01 +000025#include "GrTextStrike.h"
bsalomon@google.com8c2fe992011-09-13 15:27:18 +000026#include "SkTLazy.h"
bsalomon@google.comc0af3172012-06-15 14:10:09 +000027#include "SkTLS.h"
tomhudson@google.com0c8d93a2011-07-01 17:08:26 +000028#include "SkTrace.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000029
reed@google.comfa35e3d2012-06-26 20:16:17 +000030SK_DEFINE_INST_COUNT(GrContext)
31SK_DEFINE_INST_COUNT(GrDrawState)
32
bsalomon@google.com1d4edd32012-08-16 18:36:06 +000033// It can be useful to set this to kNo_BufferedDraw to test whether a bug is caused by using the
34// InOrderDrawBuffer, to compare performance of using/not using InOrderDrawBuffer, or to make
35// debugging easier.
36#define DEFAULT_BUFFERING (GR_DISABLE_DRAW_BUFFERING ? kNo_BufferedDraw : kYes_BufferedDraw)
bsalomon@google.com27847de2011-02-22 20:59:41 +000037
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +000038#define MAX_BLUR_SIGMA 4.0f
39
bsalomon@google.comd46e2422011-09-23 17:40:07 +000040// When we're using coverage AA but the blend is incompatible (given gpu
41// limitations) should we disable AA or draw wrong?
bsalomon@google.com950d7a82011-09-28 15:05:33 +000042#define DISABLE_COVERAGE_AA_FOR_BLEND 1
bsalomon@google.comd46e2422011-09-23 17:40:07 +000043
reed@google.com4b2d3f32012-05-15 18:05:50 +000044#if GR_DEBUG
45 // change this to a 1 to see notifications when partial coverage fails
46 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
47#else
48 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
49#endif
50
bsalomon@google.com07fc0d12012-06-22 15:15:59 +000051static const size_t MAX_TEXTURE_CACHE_COUNT = 256;
52static const size_t MAX_TEXTURE_CACHE_BYTES = 16 * 1024 * 1024;
bsalomon@google.com27847de2011-02-22 20:59:41 +000053
bsalomon@google.com60361492012-03-15 17:47:06 +000054static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
bsalomon@google.com27847de2011-02-22 20:59:41 +000055static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
56
bsalomon@google.com1d4edd32012-08-16 18:36:06 +000057static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = 1 << 11;
58static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = 4;
bsalomon@google.com27847de2011-02-22 20:59:41 +000059
bsalomon@google.combc4b6542011-11-19 13:56:11 +000060#define ASSERT_OWNED_RESOURCE(R) GrAssert(!(R) || (R)->getContext() == this)
61
bsalomon@google.com05ef5102011-05-02 21:14:59 +000062GrContext* GrContext::Create(GrEngine engine,
63 GrPlatform3DContext context3D) {
bsalomon@google.com27847de2011-02-22 20:59:41 +000064 GrContext* ctx = NULL;
65 GrGpu* fGpu = GrGpu::Create(engine, context3D);
66 if (NULL != fGpu) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000067 ctx = SkNEW_ARGS(GrContext, (fGpu));
bsalomon@google.com27847de2011-02-22 20:59:41 +000068 fGpu->unref();
69 }
70 return ctx;
71}
72
bsalomon@google.comc0af3172012-06-15 14:10:09 +000073namespace {
74void* CreateThreadInstanceCount() {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000075 return SkNEW_ARGS(int, (0));
bsalomon@google.comc0af3172012-06-15 14:10:09 +000076}
77void DeleteThreadInstanceCount(void* v) {
78 delete reinterpret_cast<int*>(v);
79}
80#define THREAD_INSTANCE_COUNT \
81 (*reinterpret_cast<int*>(SkTLS::Get(CreateThreadInstanceCount, \
82 DeleteThreadInstanceCount)))
83
84}
85
86int GrContext::GetThreadInstanceCount() {
87 return THREAD_INSTANCE_COUNT;
88}
89
bsalomon@google.com27847de2011-02-22 20:59:41 +000090GrContext::~GrContext() {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000091 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +000092
93 // Since the gpu can hold scratch textures, give it a chance to let go
94 // of them before freeing the texture cache
95 fGpu->purgeResources();
96
bsalomon@google.com27847de2011-02-22 20:59:41 +000097 delete fTextureCache;
98 delete fFontCache;
99 delete fDrawBuffer;
100 delete fDrawBufferVBAllocPool;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000101 delete fDrawBufferIBAllocPool;
bsalomon@google.com30085192011-08-19 15:42:31 +0000102
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000103 fAARectRenderer->unref();
104
bsalomon@google.com205d4602011-04-25 12:43:45 +0000105 fGpu->unref();
bsalomon@google.com30085192011-08-19 15:42:31 +0000106 GrSafeUnref(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000107 GrSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000108 fDrawState->unref();
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000109
110 --THREAD_INSTANCE_COUNT;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000111}
112
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000113void GrContext::contextLost() {
junov@google.com53a55842011-06-08 22:55:10 +0000114 contextDestroyed();
115 this->setupDrawBuffer();
116}
117
118void GrContext::contextDestroyed() {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000119 // abandon first to so destructors
120 // don't try to free the resources in the API.
121 fGpu->abandonResources();
122
bsalomon@google.com30085192011-08-19 15:42:31 +0000123 // a path renderer may be holding onto resources that
124 // are now unusable
125 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000126 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000127
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000128 delete fDrawBuffer;
129 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000130
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000131 delete fDrawBufferVBAllocPool;
132 fDrawBufferVBAllocPool = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000133
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000134 delete fDrawBufferIBAllocPool;
135 fDrawBufferIBAllocPool = NULL;
136
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000137 fAARectRenderer->reset();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000138
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000139 fTextureCache->removeAll();
140 fFontCache->freeAll();
141 fGpu->markContextDirty();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000142}
143
144void GrContext::resetContext() {
145 fGpu->markContextDirty();
146}
147
148void GrContext::freeGpuResources() {
149 this->flush();
robertphillips@google.comff175842012-05-14 19:31:39 +0000150
151 fGpu->purgeResources();
152
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000153 fAARectRenderer->reset();
154
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000155 fTextureCache->removeAll();
156 fFontCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000157 // a path renderer may be holding onto resources
158 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000159 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000160}
161
twiz@google.com05e70242012-01-27 19:12:00 +0000162size_t GrContext::getGpuTextureCacheBytes() const {
163 return fTextureCache->getCachedResourceBytes();
164}
165
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000166////////////////////////////////////////////////////////////////////////////////
167
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000168namespace {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000169
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000170void scale_rect(SkRect* rect, float xScale, float yScale) {
robertphillips@google.com5af56062012-04-27 15:39:52 +0000171 rect->fLeft = SkScalarMul(rect->fLeft, SkFloatToScalar(xScale));
172 rect->fTop = SkScalarMul(rect->fTop, SkFloatToScalar(yScale));
173 rect->fRight = SkScalarMul(rect->fRight, SkFloatToScalar(xScale));
174 rect->fBottom = SkScalarMul(rect->fBottom, SkFloatToScalar(yScale));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000175}
176
bsalomon@google.comb505a122012-05-31 18:40:36 +0000177float adjust_sigma(float sigma, int *scaleFactor, int *radius) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000178 *scaleFactor = 1;
179 while (sigma > MAX_BLUR_SIGMA) {
180 *scaleFactor *= 2;
181 sigma *= 0.5f;
182 }
bsalomon@google.comb505a122012-05-31 18:40:36 +0000183 *radius = static_cast<int>(ceilf(sigma * 3.0f));
184 GrAssert(*radius <= GrConvolutionEffect::kMaxKernelRadius);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000185 return sigma;
186}
187
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000188void apply_morphology(GrDrawTarget* target,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000189 GrTexture* texture,
190 const SkRect& rect,
191 int radius,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000192 GrContext::MorphologyType morphType,
193 Gr1DKernelEffect::Direction direction) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000194
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000195 GrRenderTarget* rt = target->drawState()->getRenderTarget();
196 GrDrawTarget::AutoStateRestore asr(target, GrDrawTarget::kReset_ASRInit);
197 GrDrawState* drawState = target->drawState();
198 drawState->setRenderTarget(rt);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000199 GrMatrix sampleM;
200 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000201 drawState->sampler(0)->reset(sampleM);
202 SkAutoTUnref<GrCustomStage> morph(
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000203 SkNEW_ARGS(GrMorphologyEffect, (texture, direction, radius, morphType)));
bsalomon@google.comb505a122012-05-31 18:40:36 +0000204 drawState->sampler(0)->setCustomStage(morph);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000205 target->drawSimpleRect(rect, NULL);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000206}
207
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000208void convolve_gaussian(GrDrawTarget* target,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000209 GrTexture* texture,
210 const SkRect& rect,
211 float sigma,
212 int radius,
213 Gr1DKernelEffect::Direction direction) {
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000214 GrRenderTarget* rt = target->drawState()->getRenderTarget();
215 GrDrawTarget::AutoStateRestore asr(target, GrDrawTarget::kReset_ASRInit);
216 GrDrawState* drawState = target->drawState();
217 drawState->setRenderTarget(rt);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000218 GrMatrix sampleM;
219 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000220 drawState->sampler(0)->reset(sampleM);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000221 SkAutoTUnref<GrConvolutionEffect> conv(SkNEW_ARGS(GrConvolutionEffect,
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000222 (texture, direction, radius,
223 sigma)));
bsalomon@google.comb505a122012-05-31 18:40:36 +0000224 drawState->sampler(0)->setCustomStage(conv);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000225 target->drawSimpleRect(rect, NULL);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000226}
227
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000228}
229
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000230GrTexture* GrContext::findAndLockTexture(const GrTextureDesc& desc,
231 const GrCacheData& cacheData,
232 const GrTextureParams* params) {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000233 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000234 GrResource* resource = fTextureCache->findAndLock(resourceKey,
235 GrResourceCache::kNested_LockType);
236 return static_cast<GrTexture*>(resource);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000237}
238
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000239bool GrContext::isTextureInCache(const GrTextureDesc& desc,
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000240 const GrCacheData& cacheData,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000241 const GrTextureParams* params) const {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000242 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000243 return fTextureCache->hasKey(resourceKey);
244}
245
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000246void GrContext::addAndLockStencilBuffer(GrStencilBuffer* sb) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000247 ASSERT_OWNED_RESOURCE(sb);
robertphillips@google.com46a86002012-08-08 10:42:44 +0000248
249 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(sb->width(),
250 sb->height(),
251 sb->numSamples());
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000252 fTextureCache->createAndLock(resourceKey, sb);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000253}
254
255GrStencilBuffer* GrContext::findStencilBuffer(int width, int height,
256 int sampleCnt) {
robertphillips@google.com46a86002012-08-08 10:42:44 +0000257 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(width,
258 height,
259 sampleCnt);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000260 GrResource* resource = fTextureCache->findAndLock(resourceKey,
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000261 GrResourceCache::kSingle_LockType);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000262 return static_cast<GrStencilBuffer*>(resource);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000263}
264
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000265void GrContext::unlockStencilBuffer(GrStencilBuffer* sb) {
266 ASSERT_OWNED_RESOURCE(sb);
267 GrAssert(NULL != sb->getCacheEntry());
268
269 fTextureCache->unlock(sb->getCacheEntry());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000270}
271
272static void stretchImage(void* dst,
273 int dstW,
274 int dstH,
275 void* src,
276 int srcW,
277 int srcH,
278 int bpp) {
279 GrFixed dx = (srcW << 16) / dstW;
280 GrFixed dy = (srcH << 16) / dstH;
281
282 GrFixed y = dy >> 1;
283
284 int dstXLimit = dstW*bpp;
285 for (int j = 0; j < dstH; ++j) {
286 GrFixed x = dx >> 1;
287 void* srcRow = (uint8_t*)src + (y>>16)*srcW*bpp;
288 void* dstRow = (uint8_t*)dst + j*dstW*bpp;
289 for (int i = 0; i < dstXLimit; i += bpp) {
290 memcpy((uint8_t*) dstRow + i,
291 (uint8_t*) srcRow + (x>>16)*bpp,
292 bpp);
293 x += dx;
294 }
295 y += dy;
296 }
297}
298
robertphillips@google.com3319f332012-08-13 18:00:36 +0000299// The desired texture is NPOT and tiled but that isn't supported by
300// the current hardware. Resize the texture to be a POT
301GrTexture* GrContext::createResizedTexture(const GrTextureDesc& desc,
302 const GrCacheData& cacheData,
303 void* srcData,
304 size_t rowBytes,
305 bool needsFiltering) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000306 GrTexture* clampedTexture = this->findAndLockTexture(desc, cacheData, NULL);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000307
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000308 if (NULL == clampedTexture) {
309 clampedTexture = this->createAndLockTexture(NULL, desc, cacheData, srcData, rowBytes);
310 GrAssert(NULL != clampedTexture);
311 if (NULL == clampedTexture) {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000312 return NULL;
313 }
314 }
315 GrTextureDesc rtDesc = desc;
316 rtDesc.fFlags = rtDesc.fFlags |
317 kRenderTarget_GrTextureFlagBit |
318 kNoStencil_GrTextureFlagBit;
319 rtDesc.fWidth = GrNextPow2(GrMax(desc.fWidth, 64));
320 rtDesc.fHeight = GrNextPow2(GrMax(desc.fHeight, 64));
321
322 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
323
324 if (NULL != texture) {
325 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
326 GrDrawState* drawState = fGpu->drawState();
327 drawState->setRenderTarget(texture->asRenderTarget());
328
329 // if filtering is not desired then we want to ensure all
330 // texels in the resampled image are copies of texels from
331 // the original.
332 drawState->sampler(0)->reset(SkShader::kClamp_TileMode,
333 needsFiltering);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000334 drawState->createTextureEffect(0, clampedTexture);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000335
336 static const GrVertexLayout layout =
337 GrDrawTarget::StageTexCoordVertexLayoutBit(0,0);
338 GrDrawTarget::AutoReleaseGeometry arg(fGpu, layout, 4, 0);
339
340 if (arg.succeeded()) {
341 GrPoint* verts = (GrPoint*) arg.vertices();
342 verts[0].setIRectFan(0, 0,
343 texture->width(),
344 texture->height(),
345 2*sizeof(GrPoint));
346 verts[1].setIRectFan(0, 0, 1, 1, 2*sizeof(GrPoint));
347 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType,
348 0, 4);
349 }
350 texture->releaseRenderTarget();
351 } else {
352 // TODO: Our CPU stretch doesn't filter. But we create separate
353 // stretched textures when the sampler state is either filtered or
354 // not. Either implement filtered stretch blit on CPU or just create
355 // one when FBO case fails.
356
357 rtDesc.fFlags = kNone_GrTextureFlags;
358 // no longer need to clamp at min RT size.
359 rtDesc.fWidth = GrNextPow2(desc.fWidth);
360 rtDesc.fHeight = GrNextPow2(desc.fHeight);
361 int bpp = GrBytesPerPixel(desc.fConfig);
362 SkAutoSMalloc<128*128*4> stretchedPixels(bpp *
363 rtDesc.fWidth *
364 rtDesc.fHeight);
365 stretchImage(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
366 srcData, desc.fWidth, desc.fHeight, bpp);
367
368 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
369
370 GrTexture* texture = fGpu->createTexture(rtDesc,
371 stretchedPixels.get(),
372 stretchedRowBytes);
373 GrAssert(NULL != texture);
374 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000375 this->unlockTexture(clampedTexture);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000376
377 return texture;
378}
379
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000380GrTexture* GrContext::createAndLockTexture(
bsalomon@google.comb8670992012-07-25 21:27:09 +0000381 const GrTextureParams* params,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000382 const GrTextureDesc& desc,
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000383 const GrCacheData& cacheData,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000384 void* srcData,
385 size_t rowBytes) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000386 SK_TRACE_EVENT0("GrContext::createAndLockTexture");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000387
388#if GR_DUMP_TEXTURE_UPLOAD
389 GrPrintf("GrContext::createAndLockTexture [%d %d]\n", desc.fWidth, desc.fHeight);
390#endif
391
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000392 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000393
robertphillips@google.com3319f332012-08-13 18:00:36 +0000394 GrTexture* texture = NULL;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000395 if (GrTexture::NeedsResizing(resourceKey)) {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000396 texture = this->createResizedTexture(desc, cacheData,
397 srcData, rowBytes,
398 GrTexture::NeedsFiltering(resourceKey));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000399 } else {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000400 texture = fGpu->createTexture(desc, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000401 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000402
403 if (NULL != texture) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000404 fTextureCache->createAndLock(resourceKey, texture);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000405 }
406
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000407 return texture;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000408}
409
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000410GrTexture* GrContext::lockScratchTexture(const GrTextureDesc& inDesc,
411 ScratchTexMatch match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000412 GrTextureDesc desc = inDesc;
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000413 GrCacheData cacheData(GrCacheData::kScratch_CacheID);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000414
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000415 if (kExact_ScratchTexMatch != match) {
416 // bin by pow2 with a reasonable min
417 static const int MIN_SIZE = 256;
418 desc.fWidth = GrMax(MIN_SIZE, GrNextPow2(desc.fWidth));
419 desc.fHeight = GrMax(MIN_SIZE, GrNextPow2(desc.fHeight));
420 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000421
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000422 GrResource* resource = NULL;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000423 int origWidth = desc.fWidth;
424 int origHeight = desc.fHeight;
425 bool doubledW = false;
426 bool doubledH = false;
427
428 do {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000429 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL, desc, cacheData, true);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000430 resource = fTextureCache->findAndLock(key,
431 GrResourceCache::kNested_LockType);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000432 // if we miss, relax the fit of the flags...
433 // then try doubling width... then height.
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000434 if (NULL != resource || kExact_ScratchTexMatch == match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000435 break;
436 }
437 if (!(desc.fFlags & kRenderTarget_GrTextureFlagBit)) {
438 desc.fFlags = desc.fFlags | kRenderTarget_GrTextureFlagBit;
439 } else if (desc.fFlags & kNoStencil_GrTextureFlagBit) {
440 desc.fFlags = desc.fFlags & ~kNoStencil_GrTextureFlagBit;
441 } else if (!doubledW) {
442 desc.fFlags = inDesc.fFlags;
443 desc.fWidth *= 2;
444 doubledW = true;
445 } else if (!doubledH) {
446 desc.fFlags = inDesc.fFlags;
447 desc.fWidth = origWidth;
448 desc.fHeight *= 2;
449 doubledH = true;
450 } else {
451 break;
452 }
453
454 } while (true);
455
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000456 if (NULL == resource) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000457 desc.fFlags = inDesc.fFlags;
458 desc.fWidth = origWidth;
459 desc.fHeight = origHeight;
460 GrTexture* texture = fGpu->createTexture(desc, NULL, 0);
461 if (NULL != texture) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000462 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000463 texture->desc(),
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000464 cacheData,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000465 true);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000466 fTextureCache->createAndLock(key, texture);
467 resource = texture;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000468 }
469 }
470
471 // If the caller gives us the same desc/sampler twice we don't want
472 // to return the same texture the second time (unless it was previously
473 // released). So we detach the entry from the cache and reattach at release.
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000474 if (NULL != resource) {
475 fTextureCache->detach(resource->getCacheEntry());
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000476 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000477 return static_cast<GrTexture*>(resource);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000478}
479
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000480void GrContext::addExistingTextureToCache(GrTexture* texture) {
481
482 if (NULL == texture) {
483 return;
484 }
485
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000486 // 'texture' is a scratch texture returning to the fold
487 GrCacheData cacheData(GrCacheData::kScratch_CacheID);
488
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000489 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
490 texture->desc(),
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000491 cacheData,
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000492 true);
493 fTextureCache->attach(key, texture);
494}
495
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000496void GrContext::unlockTexture(GrTexture* texture) {
497 ASSERT_OWNED_RESOURCE(texture);
498 GrAssert(NULL != texture->getCacheEntry());
499
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000500 // If this is a scratch texture we detached it from the cache
501 // while it was locked (to avoid two callers simultaneously getting
502 // the same texture).
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000503 if (GrTexture::IsScratchTexture(texture->getCacheEntry()->key())) {
504 fTextureCache->reattachAndUnlock(texture->getCacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000505 } else {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000506 fTextureCache->unlock(texture->getCacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000507 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000508}
509
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000510void GrContext::freeEntry(GrTexture* texture) {
511 ASSERT_OWNED_RESOURCE(texture);
512 GrAssert(NULL != texture->getCacheEntry());
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000513
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000514 fTextureCache->freeEntry(texture->getCacheEntry());
515 texture->setCacheEntry(NULL);
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000516}
517
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000518GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000519 void* srcData,
520 size_t rowBytes) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000521 GrTextureDesc descCopy = descIn;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000522 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000523}
524
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000525void GrContext::getTextureCacheLimits(int* maxTextures,
526 size_t* maxTextureBytes) const {
527 fTextureCache->getLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000528}
529
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000530void GrContext::setTextureCacheLimits(int maxTextures, size_t maxTextureBytes) {
531 fTextureCache->setLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000532}
533
bsalomon@google.com91958362011-06-13 17:58:13 +0000534int GrContext::getMaxTextureSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000535 return fGpu->getCaps().fMaxTextureSize;
bsalomon@google.com91958362011-06-13 17:58:13 +0000536}
537
538int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000539 return fGpu->getCaps().fMaxRenderTargetSize;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000540}
541
542///////////////////////////////////////////////////////////////////////////////
543
bsalomon@google.come269f212011-11-07 13:29:52 +0000544GrTexture* GrContext::createPlatformTexture(const GrPlatformTextureDesc& desc) {
545 return fGpu->createPlatformTexture(desc);
546}
547
548GrRenderTarget* GrContext::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
549 return fGpu->createPlatformRenderTarget(desc);
550}
551
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000552///////////////////////////////////////////////////////////////////////////////
553
bsalomon@google.comb8670992012-07-25 21:27:09 +0000554bool GrContext::supportsIndex8PixelConfig(const GrTextureParams* params,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000555 int width, int height) const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000556 const GrDrawTarget::Caps& caps = fGpu->getCaps();
557 if (!caps.f8BitPaletteSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000558 return false;
559 }
560
bsalomon@google.com27847de2011-02-22 20:59:41 +0000561 bool isPow2 = GrIsPow2(width) && GrIsPow2(height);
562
563 if (!isPow2) {
bsalomon@google.comb8670992012-07-25 21:27:09 +0000564 bool tiled = NULL != params && params->isTiled();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000565 if (tiled && !caps.fNPOTTextureTileSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000566 return false;
567 }
568 }
569 return true;
570}
571
572////////////////////////////////////////////////////////////////////////////////
573
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000574const GrClipData* GrContext::getClip() const {
575 return fGpu->getClip();
576}
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000577
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000578void GrContext::setClip(const GrClipData* clipData) {
579 fGpu->setClip(clipData);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000580 fDrawState->enableState(GrDrawState::kClip_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000581}
582
bsalomon@google.com27847de2011-02-22 20:59:41 +0000583////////////////////////////////////////////////////////////////////////////////
584
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000585void GrContext::clear(const GrIRect* rect,
586 const GrColor color,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000587 GrRenderTarget* target) {
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000588 this->prepareToDraw(NULL, DEFAULT_BUFFERING)->clear(rect, color, target);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000589}
590
591void GrContext::drawPaint(const GrPaint& paint) {
592 // set rect to be big enough to fill the space, but not super-huge, so we
593 // don't overflow fixed-point implementations
bsalomon@google.comd302f142011-03-03 13:54:13 +0000594 GrRect r;
595 r.setLTRB(0, 0,
596 GrIntToScalar(getRenderTarget()->width()),
597 GrIntToScalar(getRenderTarget()->height()));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000598 GrMatrix inverse;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000599 SkTLazy<GrPaint> tmpPaint;
600 const GrPaint* p = &paint;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000601 AutoMatrix am;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000602
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000603 // We attempt to map r by the inverse matrix and draw that. mapRect will
604 // map the four corners and bound them with a new rect. This will not
605 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000606 if (!this->getMatrix().hasPerspective()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000607 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000608 GrPrintf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000609 return;
610 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000611 inverse.mapRect(&r);
612 } else {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000613 if (paint.hasTextureOrMask()) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000614 tmpPaint.set(paint);
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000615 p = tmpPaint.get();
bsalomon@google.come3d32162012-07-20 13:37:06 +0000616 if (!tmpPaint.get()->preConcatSamplerMatricesWithInverse(fDrawState->getViewMatrix())) {
617 GrPrintf("Could not invert matrix\n");
618 }
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000619 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000620 am.set(this, GrMatrix::I());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000621 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000622 // by definition this fills the entire clip, no need for AA
623 if (paint.fAntiAlias) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000624 if (!tmpPaint.isValid()) {
625 tmpPaint.set(paint);
626 p = tmpPaint.get();
627 }
628 GrAssert(p == tmpPaint.get());
629 tmpPaint.get()->fAntiAlias = false;
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000630 }
631 this->drawRect(*p, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000632}
633
bsalomon@google.com205d4602011-04-25 12:43:45 +0000634////////////////////////////////////////////////////////////////////////////////
635
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000636namespace {
637inline bool disable_coverage_aa_for_blend(GrDrawTarget* target) {
638 return DISABLE_COVERAGE_AA_FOR_BLEND && !target->canApplyCoverage();
639}
640}
641
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000642////////////////////////////////////////////////////////////////////////////////
643
bsalomon@google.com27847de2011-02-22 20:59:41 +0000644/* create a triangle strip that strokes the specified triangle. There are 8
645 unique vertices, but we repreat the last 2 to close up. Alternatively we
646 could use an indices array, and then only send 8 verts, but not sure that
647 would be faster.
648 */
bsalomon@google.com205d4602011-04-25 12:43:45 +0000649static void setStrokeRectStrip(GrPoint verts[10], GrRect rect,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000650 GrScalar width) {
651 const GrScalar rad = GrScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000652 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000653
654 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
655 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
656 verts[2].set(rect.fRight - rad, rect.fTop + rad);
657 verts[3].set(rect.fRight + rad, rect.fTop - rad);
658 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
659 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
660 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
661 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
662 verts[8] = verts[0];
663 verts[9] = verts[1];
664}
665
reed@google.com20efde72011-05-09 17:00:02 +0000666/**
667 * Returns true if the rects edges are integer-aligned.
668 */
669static bool isIRect(const GrRect& r) {
670 return GrScalarIsInt(r.fLeft) && GrScalarIsInt(r.fTop) &&
671 GrScalarIsInt(r.fRight) && GrScalarIsInt(r.fBottom);
672}
673
bsalomon@google.com205d4602011-04-25 12:43:45 +0000674static bool apply_aa_to_rect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000675 const GrRect& rect,
676 GrScalar width,
677 const GrMatrix* matrix,
678 GrMatrix* combinedMatrix,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000679 GrRect* devRect,
680 bool* useVertexCoverage) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000681 // we use a simple coverage ramp to do aa on axis-aligned rects
682 // we check if the rect will be axis-aligned, and the rect won't land on
683 // integer coords.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000684
bsalomon@google.coma3108262011-10-10 14:08:47 +0000685 // we are keeping around the "tweak the alpha" trick because
686 // it is our only hope for the fixed-pipe implementation.
687 // In a shader implementation we can give a separate coverage input
bsalomon@google.com289533a2011-10-27 12:34:25 +0000688 // TODO: remove this ugliness when we drop the fixed-pipe impl
bsalomon@google.coma3108262011-10-10 14:08:47 +0000689 *useVertexCoverage = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000690 if (!target->canTweakAlphaForCoverage()) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000691 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +0000692#if GR_DEBUG
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000693 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000694#endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000695 return false;
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000696 } else {
697 *useVertexCoverage = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000698 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000699 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000700 const GrDrawState& drawState = target->getDrawState();
701 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000702 return false;
703 }
704
bsalomon@google.com471d4712011-08-23 15:45:25 +0000705 if (0 == width && target->willUseHWAALines()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000706 return false;
707 }
708
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000709 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000710 return false;
711 }
712
713 if (NULL != matrix &&
714 !matrix->preservesAxisAlignment()) {
715 return false;
716 }
717
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000718 *combinedMatrix = drawState.getViewMatrix();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000719 if (NULL != matrix) {
720 combinedMatrix->preConcat(*matrix);
721 GrAssert(combinedMatrix->preservesAxisAlignment());
722 }
723
724 combinedMatrix->mapRect(devRect, rect);
725 devRect->sort();
726
727 if (width < 0) {
reed@google.com20efde72011-05-09 17:00:02 +0000728 return !isIRect(*devRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000729 } else {
730 return true;
731 }
732}
733
bsalomon@google.com27847de2011-02-22 20:59:41 +0000734void GrContext::drawRect(const GrPaint& paint,
735 const GrRect& rect,
736 GrScalar width,
737 const GrMatrix* matrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000738 SK_TRACE_EVENT0("GrContext::drawRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000739
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000740 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000741 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000742
bsalomon@google.com205d4602011-04-25 12:43:45 +0000743 GrRect devRect = rect;
744 GrMatrix combinedMatrix;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000745 bool useVertexCoverage;
bsalomon@google.com289533a2011-10-27 12:34:25 +0000746 bool needAA = paint.fAntiAlias &&
747 !this->getRenderTarget()->isMultisampled();
748 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix,
749 &combinedMatrix, &devRect,
750 &useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000751
752 if (doAA) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000753 GrDrawTarget::AutoDeviceCoordDraw adcd(target);
754 if (!adcd.succeeded()) {
755 return;
756 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000757 if (width >= 0) {
758 GrVec strokeSize;;
759 if (width > 0) {
760 strokeSize.set(width, width);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000761 combinedMatrix.mapVectors(&strokeSize, 1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000762 strokeSize.setAbs(strokeSize);
763 } else {
764 strokeSize.set(GR_Scalar1, GR_Scalar1);
765 }
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000766 fAARectRenderer->strokeAARect(this->getGpu(), target, devRect,
767 strokeSize, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000768 } else {
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000769 fAARectRenderer->fillAARect(this->getGpu(), target,
770 devRect, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000771 }
772 return;
773 }
774
bsalomon@google.com27847de2011-02-22 20:59:41 +0000775 if (width >= 0) {
776 // TODO: consider making static vertex buffers for these cases.
777 // Hairline could be done by just adding closing vertex to
778 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000779
bsalomon@google.com27847de2011-02-22 20:59:41 +0000780 static const int worstCaseVertCount = 10;
bsalomon@google.come3d32162012-07-20 13:37:06 +0000781 GrDrawTarget::AutoReleaseGeometry geo(target, 0, worstCaseVertCount, 0);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000782
783 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000784 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000785 return;
786 }
787
788 GrPrimitiveType primType;
789 int vertCount;
790 GrPoint* vertex = geo.positions();
791
792 if (width > 0) {
793 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000794 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000795 setStrokeRectStrip(vertex, rect, width);
796 } else {
797 // hairline
798 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000799 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000800 vertex[0].set(rect.fLeft, rect.fTop);
801 vertex[1].set(rect.fRight, rect.fTop);
802 vertex[2].set(rect.fRight, rect.fBottom);
803 vertex[3].set(rect.fLeft, rect.fBottom);
804 vertex[4].set(rect.fLeft, rect.fTop);
805 }
806
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000807 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000808 if (NULL != matrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000809 GrDrawState* drawState = target->drawState();
810 avmr.set(drawState);
811 drawState->preConcatViewMatrix(*matrix);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000812 drawState->preConcatSamplerMatrices(*matrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000813 }
814
815 target->drawNonIndexed(primType, 0, vertCount);
816 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000817#if GR_STATIC_RECT_VB
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000818 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
819 if (NULL == sqVB) {
820 GrPrintf("Failed to create static rect vb.\n");
821 return;
822 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000823 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000824 GrDrawState* drawState = target->drawState();
825 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000826 GrMatrix m;
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000827 m.setAll(rect.width(), 0, rect.fLeft,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000828 0, rect.height(), rect.fTop,
829 0, 0, GrMatrix::I()[8]);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000830
831 if (NULL != matrix) {
832 m.postConcat(*matrix);
833 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000834 drawState->preConcatViewMatrix(m);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000835 drawState->preConcatSamplerMatrices(m);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000836
bsalomon@google.com47059542012-06-06 20:51:20 +0000837 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000838#else
bsalomon@google.come3d32162012-07-20 13:37:06 +0000839 target->drawSimpleRect(rect, matrix);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000840#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000841 }
842}
843
844void GrContext::drawRectToRect(const GrPaint& paint,
845 const GrRect& dstRect,
846 const GrRect& srcRect,
847 const GrMatrix* dstMatrix,
848 const GrMatrix* srcMatrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000849 SK_TRACE_EVENT0("GrContext::drawRectToRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000850
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000851 // srcRect refers to paint's first texture
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000852 if (!paint.isTextureStageEnabled(0)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000853 drawRect(paint, dstRect, -1, dstMatrix);
854 return;
855 }
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000856
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000857 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000858
859#if GR_STATIC_RECT_VB
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000860 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000861 GrDrawState* drawState = target->drawState();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000862 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000863
864 GrMatrix m;
865
866 m.setAll(dstRect.width(), 0, dstRect.fLeft,
867 0, dstRect.height(), dstRect.fTop,
868 0, 0, GrMatrix::I()[8]);
869 if (NULL != dstMatrix) {
870 m.postConcat(*dstMatrix);
871 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000872 drawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000873
bsalomon@google.come3d32162012-07-20 13:37:06 +0000874 // we explicitly setup the correct coords for the first stage. The others
875 // must know about the view matrix change.
876 for (int s = 1; s < GrPaint::kTotalStages; ++s) {
877 if (drawState->isStageEnabled(s)) {
878 drawState->sampler(s)->preConcatMatrix(m);
879 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000880 }
881
bsalomon@google.com27847de2011-02-22 20:59:41 +0000882 m.setAll(srcRect.width(), 0, srcRect.fLeft,
883 0, srcRect.height(), srcRect.fTop,
884 0, 0, GrMatrix::I()[8]);
885 if (NULL != srcMatrix) {
886 m.postConcat(*srcMatrix);
887 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000888 drawState->sampler(GrPaint::kFirstTextureStage)->preConcatMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000889
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000890 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
891 if (NULL == sqVB) {
892 GrPrintf("Failed to create static rect vb.\n");
893 return;
894 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000895 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com47059542012-06-06 20:51:20 +0000896 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000897#else
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000898 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000899
tomhudson@google.com93813632011-10-27 20:21:16 +0000900 const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
901 const GrMatrix* srcMatrices[GrDrawState::kNumStages] = {NULL};
bsalomon@google.com27847de2011-02-22 20:59:41 +0000902 srcRects[0] = &srcRect;
903 srcMatrices[0] = srcMatrix;
904
bsalomon@google.come3d32162012-07-20 13:37:06 +0000905 target->drawRect(dstRect, dstMatrix, srcRects, srcMatrices);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000906#endif
907}
908
909void GrContext::drawVertices(const GrPaint& paint,
910 GrPrimitiveType primitiveType,
911 int vertexCount,
912 const GrPoint positions[],
913 const GrPoint texCoords[],
914 const GrColor colors[],
915 const uint16_t indices[],
916 int indexCount) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000917 SK_TRACE_EVENT0("GrContext::drawVertices");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000918
919 GrDrawTarget::AutoReleaseGeometry geo;
920
bsalomon@google.com07ea2db2012-08-17 14:06:49 +0000921 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000922 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000923
bsalomon@google.come3d32162012-07-20 13:37:06 +0000924 GrVertexLayout layout = 0;
925 if (NULL != texCoords) {
926 layout |= GrDrawTarget::StageTexCoordVertexLayoutBit(0, 0);
927 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000928 if (NULL != colors) {
929 layout |= GrDrawTarget::kColor_VertexLayoutBit;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000930 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000931 int vertexSize = GrDrawTarget::VertexSize(layout);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000932
933 if (sizeof(GrPoint) != vertexSize) {
934 if (!geo.set(target, layout, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000935 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000936 return;
937 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000938 int texOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000939 int colorOffset;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000940 GrDrawTarget::VertexSizeAndOffsetsByIdx(layout,
941 texOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000942 &colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000943 NULL,
944 NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000945 void* curVertex = geo.vertices();
946
947 for (int i = 0; i < vertexCount; ++i) {
948 *((GrPoint*)curVertex) = positions[i];
949
950 if (texOffsets[0] > 0) {
951 *(GrPoint*)((intptr_t)curVertex + texOffsets[0]) = texCoords[i];
952 }
953 if (colorOffset > 0) {
954 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
955 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000956 curVertex = (void*)((intptr_t)curVertex + vertexSize);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000957 }
958 } else {
959 target->setVertexSourceToArray(layout, positions, vertexCount);
960 }
961
bsalomon@google.com91958362011-06-13 17:58:13 +0000962 // we don't currently apply offscreen AA to this path. Need improved
963 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000964
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000965 if (NULL != indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000966 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000967 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000968 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000969 target->drawNonIndexed(primitiveType, 0, vertexCount);
970 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000971}
972
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000973///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com150d2842012-01-12 20:19:56 +0000974namespace {
975
bsalomon@google.com93c96602012-04-27 13:05:21 +0000976struct CircleVertex {
977 GrPoint fPos;
978 GrPoint fCenter;
979 GrScalar fOuterRadius;
980 GrScalar fInnerRadius;
981};
982
983/* Returns true if will map a circle to another circle. This can be true
984 * if the matrix only includes square-scale, rotation, translation.
985 */
986inline bool isSimilarityTransformation(const SkMatrix& matrix,
987 SkScalar tol = SK_ScalarNearlyZero) {
988 if (matrix.isIdentity() || matrix.getType() == SkMatrix::kTranslate_Mask) {
989 return true;
990 }
991 if (matrix.hasPerspective()) {
992 return false;
993 }
994
995 SkScalar mx = matrix.get(SkMatrix::kMScaleX);
996 SkScalar sx = matrix.get(SkMatrix::kMSkewX);
997 SkScalar my = matrix.get(SkMatrix::kMScaleY);
998 SkScalar sy = matrix.get(SkMatrix::kMSkewY);
999
1000 if (mx == 0 && sx == 0 && my == 0 && sy == 0) {
1001 return false;
1002 }
1003
1004 // it has scales or skews, but it could also be rotation, check it out.
1005 SkVector vec[2];
1006 vec[0].set(mx, sx);
1007 vec[1].set(sy, my);
1008
1009 return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) &&
1010 SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(),
1011 SkScalarSquare(tol));
1012}
1013
1014}
1015
1016// TODO: strokeWidth can't be larger than zero right now.
1017// It will be fixed when drawPath() can handle strokes.
1018void GrContext::drawOval(const GrPaint& paint,
1019 const GrRect& rect,
1020 SkScalar strokeWidth) {
bsalomon@google.com0982d352012-07-31 15:33:25 +00001021 GrAssert(strokeWidth <= 0);
1022 if (!isSimilarityTransformation(this->getMatrix()) ||
bsalomon@google.com93c96602012-04-27 13:05:21 +00001023 !paint.fAntiAlias ||
1024 rect.height() != rect.width()) {
1025 SkPath path;
1026 path.addOval(rect);
1027 GrPathFill fill = (strokeWidth == 0) ?
bsalomon@google.com0982d352012-07-31 15:33:25 +00001028 kHairLine_GrPathFill : kWinding_GrPathFill;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001029 this->internalDrawPath(paint, path, fill, NULL);
1030 return;
1031 }
1032
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001033 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
1034
bsalomon@google.com0982d352012-07-31 15:33:25 +00001035 GrDrawState* drawState = target->drawState();
1036 GrDrawState::AutoStageDisable atr(fDrawState);
1037 const GrMatrix vm = drawState->getViewMatrix();
1038
bsalomon@google.com93c96602012-04-27 13:05:21 +00001039 const GrRenderTarget* rt = drawState->getRenderTarget();
1040 if (NULL == rt) {
1041 return;
1042 }
1043
bsalomon@google.come3d32162012-07-20 13:37:06 +00001044 GrDrawTarget::AutoDeviceCoordDraw adcd(target);
1045 if (!adcd.succeeded()) {
1046 return;
1047 }
bsalomon@google.com93c96602012-04-27 13:05:21 +00001048
bsalomon@google.come3d32162012-07-20 13:37:06 +00001049 GrVertexLayout layout = GrDrawTarget::kEdge_VertexLayoutBit;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001050 GrAssert(sizeof(CircleVertex) == GrDrawTarget::VertexSize(layout));
1051
1052 GrPoint center = GrPoint::Make(rect.centerX(), rect.centerY());
1053 GrScalar radius = SkScalarHalf(rect.width());
1054
1055 vm.mapPoints(&center, 1);
1056 radius = vm.mapRadius(radius);
1057
1058 GrScalar outerRadius = radius;
1059 GrScalar innerRadius = 0;
1060 SkScalar halfWidth = 0;
1061 if (strokeWidth == 0) {
1062 halfWidth = SkScalarHalf(SK_Scalar1);
1063
1064 outerRadius += halfWidth;
1065 innerRadius = SkMaxScalar(0, radius - halfWidth);
1066 }
1067
1068 GrDrawTarget::AutoReleaseGeometry geo(target, layout, 4, 0);
1069 if (!geo.succeeded()) {
1070 GrPrintf("Failed to get space for vertices!\n");
1071 return;
1072 }
1073
1074 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
1075
robertphillips@google.coma0a66c12012-06-22 13:14:29 +00001076 // The fragment shader will extend the radius out half a pixel
1077 // to antialias. Expand the drawn rect here so all the pixels
1078 // will be captured.
1079 SkScalar L = center.fX - outerRadius - SkFloatToScalar(0.5f);
1080 SkScalar R = center.fX + outerRadius + SkFloatToScalar(0.5f);
1081 SkScalar T = center.fY - outerRadius - SkFloatToScalar(0.5f);
1082 SkScalar B = center.fY + outerRadius + SkFloatToScalar(0.5f);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001083
1084 verts[0].fPos = SkPoint::Make(L, T);
1085 verts[1].fPos = SkPoint::Make(R, T);
1086 verts[2].fPos = SkPoint::Make(L, B);
1087 verts[3].fPos = SkPoint::Make(R, B);
1088
1089 for (int i = 0; i < 4; ++i) {
1090 // this goes to fragment shader, it should be in y-points-up space.
1091 verts[i].fCenter = SkPoint::Make(center.fX, rt->height() - center.fY);
1092
1093 verts[i].fOuterRadius = outerRadius;
1094 verts[i].fInnerRadius = innerRadius;
1095 }
1096
1097 drawState->setVertexEdgeType(GrDrawState::kCircle_EdgeType);
bsalomon@google.com47059542012-06-06 20:51:20 +00001098 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001099}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001100
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001101void GrContext::drawPath(const GrPaint& paint, const SkPath& path,
reed@google.com07f3ee12011-05-16 17:21:57 +00001102 GrPathFill fill, const GrPoint* translate) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001103
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001104 if (path.isEmpty()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001105 if (GrIsFillInverted(fill)) {
1106 this->drawPaint(paint);
1107 }
1108 return;
1109 }
1110
bsalomon@google.com93c96602012-04-27 13:05:21 +00001111 SkRect ovalRect;
1112 if (!GrIsFillInverted(fill) && path.isOval(&ovalRect)) {
1113 if (translate) {
1114 ovalRect.offset(*translate);
1115 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001116 SkScalar width = (fill == kHairLine_GrPathFill) ? 0 : -SK_Scalar1;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001117 this->drawOval(paint, ovalRect, width);
1118 return;
1119 }
1120
1121 internalDrawPath(paint, path, fill, translate);
1122}
1123
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001124void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +00001125 GrPathFill fill, const GrPoint* translate) {
1126
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001127 // Note that below we may sw-rasterize the path into a scratch texture.
1128 // Scratch textures can be recycled after they are returned to the texture
1129 // cache. This presents a potential hazard for buffered drawing. However,
1130 // the writePixels that uploads to the scratch will perform a flush so we're
1131 // OK.
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001132 GrDrawTarget* target = this->prepareToDraw(&paint, DEFAULT_BUFFERING);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +00001133 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001134
bsalomon@google.com289533a2011-10-27 12:34:25 +00001135 bool prAA = paint.fAntiAlias && !this->getRenderTarget()->isMultisampled();
1136
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001137 // An Assumption here is that path renderer would use some form of tweaking
1138 // the src color (either the input alpha or in the frag shader) to implement
1139 // aa. If we have some future driver-mojo path AA that can do the right
1140 // thing WRT to the blend then we'll need some query on the PR.
1141 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001142#if GR_DEBUG
bsalomon@google.com979432b2011-11-05 21:38:22 +00001143 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001144#endif
bsalomon@google.com289533a2011-10-27 12:34:25 +00001145 prAA = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001146 }
bsalomon@google.com289533a2011-10-27 12:34:25 +00001147
robertphillips@google.com72176b22012-05-23 13:19:12 +00001148 GrPathRenderer* pr = this->getPathRenderer(path, fill, target, prAA, true);
bsalomon@google.com30085192011-08-19 15:42:31 +00001149 if (NULL == pr) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001150#if GR_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001151 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001152#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001153 return;
1154 }
1155
bsalomon@google.come3d32162012-07-20 13:37:06 +00001156 pr->drawPath(path, fill, translate, target, prAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001157}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001158
bsalomon@google.com27847de2011-02-22 20:59:41 +00001159////////////////////////////////////////////////////////////////////////////////
1160
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001161void GrContext::flush(int flagsBitfield) {
1162 if (kDiscard_FlushBit & flagsBitfield) {
1163 fDrawBuffer->reset();
1164 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001165 this->flushDrawBuffer();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001166 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001167 if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001168 fGpu->forceRenderTargetFlush();
1169 }
1170}
1171
bsalomon@google.com27847de2011-02-22 20:59:41 +00001172void GrContext::flushDrawBuffer() {
junov@google.com53a55842011-06-08 22:55:10 +00001173 if (fDrawBuffer) {
robertphillips@google.com58b38182012-05-03 16:29:41 +00001174 // With addition of the AA clip path, flushing the draw buffer can
1175 // result in the generation of an AA clip mask. During this
1176 // process the SW path renderer may be invoked which recusively
1177 // calls this method (via internalWriteTexturePixels) creating
1178 // infinite recursion
1179 GrInOrderDrawBuffer* temp = fDrawBuffer;
1180 fDrawBuffer = NULL;
1181
1182 temp->flushTo(fGpu);
1183
1184 fDrawBuffer = temp;
junov@google.com53a55842011-06-08 22:55:10 +00001185 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001186}
1187
bsalomon@google.com0342a852012-08-20 19:22:38 +00001188void GrContext::writeTexturePixels(GrTexture* texture,
1189 int left, int top, int width, int height,
1190 GrPixelConfig config, const void* buffer, size_t rowBytes,
1191 uint32_t flags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001192 SK_TRACE_EVENT0("GrContext::writeTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001193 ASSERT_OWNED_RESOURCE(texture);
1194
bsalomon@google.com0342a852012-08-20 19:22:38 +00001195 // TODO: use scratch texture to perform conversion
1196 if (kUnpremul_PixelOpsFlag & flags) {
1197 return;
1198 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001199 if (!(kDontFlush_PixelOpsFlag & flags)) {
1200 this->flush();
1201 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001202
1203 fGpu->writeTexturePixels(texture, left, top, width, height,
1204 config, buffer, rowBytes);
1205}
1206
bsalomon@google.com0342a852012-08-20 19:22:38 +00001207bool GrContext::readTexturePixels(GrTexture* texture,
1208 int left, int top, int width, int height,
1209 GrPixelConfig config, void* buffer, size_t rowBytes,
1210 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001211 SK_TRACE_EVENT0("GrContext::readTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001212 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001213
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001214 // TODO: code read pixels for textures that aren't also rendertargets
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001215 GrRenderTarget* target = texture->asRenderTarget();
1216 if (NULL != target) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001217 return this->readRenderTargetPixels(target,
1218 left, top, width, height,
1219 config, buffer, rowBytes,
1220 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001221 } else {
1222 return false;
1223 }
1224}
1225
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001226#include "SkConfig8888.h"
1227
1228namespace {
1229/**
1230 * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel
1231 * formats are representable as Config8888 and so the function returns false
1232 * if the GrPixelConfig has no equivalent Config8888.
1233 */
1234bool grconfig_to_config8888(GrPixelConfig config,
bsalomon@google.com0342a852012-08-20 19:22:38 +00001235 bool unpremul,
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001236 SkCanvas::Config8888* config8888) {
1237 switch (config) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001238 case kRGBA_8888_GrPixelConfig:
1239 if (unpremul) {
1240 *config8888 = SkCanvas::kRGBA_Unpremul_Config8888;
1241 } else {
1242 *config8888 = SkCanvas::kRGBA_Premul_Config8888;
1243 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001244 return true;
bsalomon@google.com0342a852012-08-20 19:22:38 +00001245 case kBGRA_8888_GrPixelConfig:
1246 if (unpremul) {
1247 *config8888 = SkCanvas::kBGRA_Unpremul_Config8888;
1248 } else {
1249 *config8888 = SkCanvas::kBGRA_Premul_Config8888;
1250 }
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001251 return true;
1252 default:
1253 return false;
1254 }
1255}
1256}
1257
bsalomon@google.com0342a852012-08-20 19:22:38 +00001258bool GrContext::readRenderTargetPixels(GrRenderTarget* target,
1259 int left, int top, int width, int height,
1260 GrPixelConfig config, void* buffer, size_t rowBytes,
1261 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001262 SK_TRACE_EVENT0("GrContext::readRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001263 ASSERT_OWNED_RESOURCE(target);
1264
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001265 if (NULL == target) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001266 target = fDrawState->getRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001267 if (NULL == target) {
1268 return false;
1269 }
1270 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001271
bsalomon@google.com6f379512011-11-16 20:36:03 +00001272 if (!(kDontFlush_PixelOpsFlag & flags)) {
1273 this->flush();
1274 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001275
bsalomon@google.com0342a852012-08-20 19:22:38 +00001276 if ((kUnpremul_PixelOpsFlag & flags) &&
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001277 !fGpu->canPreserveReadWriteUnpremulPixels()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +00001278
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001279 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
bsalomon@google.com0342a852012-08-20 19:22:38 +00001280 if (!grconfig_to_config8888(target->config(), false, &srcConfig8888) ||
1281 !grconfig_to_config8888(config, true, &dstConfig8888)) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001282 return false;
1283 }
1284 // do read back using target's own config
bsalomon@google.com0342a852012-08-20 19:22:38 +00001285 this->readRenderTargetPixels(target,
1286 left, top,
1287 width, height,
1288 target->config(),
1289 buffer, rowBytes,
1290 kDontFlush_PixelOpsFlag); // we already flushed
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001291 // sw convert the pixels to unpremul config
1292 uint32_t* pixels = reinterpret_cast<uint32_t*>(buffer);
1293 SkConvertConfig8888Pixels(pixels, rowBytes, dstConfig8888,
1294 pixels, rowBytes, srcConfig8888,
1295 width, height);
1296 return true;
1297 }
1298
bsalomon@google.comc4364992011-11-07 15:54:49 +00001299 GrTexture* src = target->asTexture();
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001300 bool swapRAndB = NULL != src &&
1301 fGpu->preferredReadPixelsConfig(config) ==
1302 GrPixelConfigSwapRAndB(config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001303
1304 bool flipY = NULL != src &&
1305 fGpu->readPixelsWillPayForYFlip(target, left, top,
1306 width, height, config,
1307 rowBytes);
bsalomon@google.com0342a852012-08-20 19:22:38 +00001308 bool unpremul = SkToBool(kUnpremul_PixelOpsFlag & flags);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001309
bsalomon@google.com0342a852012-08-20 19:22:38 +00001310 if (NULL == src && unpremul) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001311 // we should fallback to cpu conversion here. This could happen when
1312 // we were given an external render target by the client that is not
1313 // also a texture (e.g. FBO 0 in GL)
1314 return false;
1315 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001316 // we draw to a scratch texture if any of these conversion are applied
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001317 GrAutoScratchTexture ast;
bsalomon@google.com0342a852012-08-20 19:22:38 +00001318 if (flipY || swapRAndB || unpremul) {
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001319 GrAssert(NULL != src);
1320 if (swapRAndB) {
1321 config = GrPixelConfigSwapRAndB(config);
1322 GrAssert(kUnknown_GrPixelConfig != config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001323 }
1324 // Make the scratch a render target because we don't have a robust
1325 // readTexturePixels as of yet (it calls this function).
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001326 GrTextureDesc desc;
1327 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1328 desc.fWidth = width;
1329 desc.fHeight = height;
1330 desc.fConfig = config;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001331
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001332 // When a full readback is faster than a partial we could always make
1333 // the scratch exactly match the passed rect. However, if we see many
1334 // different size rectangles we will trash our texture cache and pay the
1335 // cost of creating and destroying many textures. So, we only request
1336 // an exact match when the caller is reading an entire RT.
1337 ScratchTexMatch match = kApprox_ScratchTexMatch;
1338 if (0 == left &&
1339 0 == top &&
1340 target->width() == width &&
1341 target->height() == height &&
1342 fGpu->fullReadPixelsIsFasterThanPartial()) {
1343 match = kExact_ScratchTexMatch;
1344 }
1345 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001346 GrTexture* texture = ast.texture();
1347 if (!texture) {
1348 return false;
1349 }
1350 target = texture->asRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001351 GrAssert(NULL != target);
1352
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001353 GrDrawTarget::AutoStateRestore asr(fGpu,
1354 GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001355 GrDrawState* drawState = fGpu->drawState();
1356 drawState->setRenderTarget(target);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001357
bsalomon@google.com0342a852012-08-20 19:22:38 +00001358 if (unpremul) {
1359 drawState->enableState(GrDrawState::kUnpremultiply_StageBit);
1360 }
1361
bsalomon@google.comc4364992011-11-07 15:54:49 +00001362 GrMatrix matrix;
1363 if (flipY) {
1364 matrix.setTranslate(SK_Scalar1 * left,
1365 SK_Scalar1 * (top + height));
1366 matrix.set(GrMatrix::kMScaleY, -GR_Scalar1);
1367 } else {
1368 matrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
1369 }
1370 matrix.postIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001371 drawState->sampler(0)->reset(matrix);
1372 drawState->sampler(0)->setRAndBSwap(swapRAndB);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001373 drawState->createTextureEffect(0, src);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001374 GrRect rect;
1375 rect.setXYWH(0, 0, SK_Scalar1 * width, SK_Scalar1 * height);
bsalomon@google.come3d32162012-07-20 13:37:06 +00001376 fGpu->drawSimpleRect(rect, NULL);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001377 left = 0;
1378 top = 0;
1379 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001380 return fGpu->readPixels(target,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001381 left, top, width, height,
1382 config, buffer, rowBytes, flipY);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001383}
1384
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001385void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1386 GrAssert(target);
1387 ASSERT_OWNED_RESOURCE(target);
1388 // In the future we may track whether there are any pending draws to this
1389 // target. We don't today so we always perform a flush. We don't promise
1390 // this to our clients, though.
1391 this->flush();
1392 fGpu->resolveRenderTarget(target);
1393}
1394
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001395void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst) {
1396 if (NULL == src || NULL == dst) {
1397 return;
1398 }
1399 ASSERT_OWNED_RESOURCE(src);
1400
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001401 // Writes pending to the source texture are not tracked, so a flush
1402 // is required to ensure that the copy captures the most recent contents
1403 // of the source texture. See similar behaviour in
1404 // GrContext::resolveRenderTarget.
1405 this->flush();
1406
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001407 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001408 GrDrawState* drawState = fGpu->drawState();
1409 drawState->setRenderTarget(dst);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001410 GrMatrix sampleM;
1411 sampleM.setIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001412 drawState->sampler(0)->reset(sampleM);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001413 drawState->createTextureEffect(0, src);
bsalomon@google.com5db3b6c2012-01-12 20:38:57 +00001414 SkRect rect = SkRect::MakeXYWH(0, 0,
1415 SK_Scalar1 * src->width(),
1416 SK_Scalar1 * src->height());
bsalomon@google.come3d32162012-07-20 13:37:06 +00001417 fGpu->drawSimpleRect(rect, NULL);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001418}
1419
bsalomon@google.com0342a852012-08-20 19:22:38 +00001420void GrContext::writeRenderTargetPixels(GrRenderTarget* target,
1421 int left, int top, int width, int height,
1422 GrPixelConfig config,
1423 const void* buffer,
1424 size_t rowBytes,
1425 uint32_t flags) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001426 SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001427 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001428
1429 if (NULL == target) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001430 target = fDrawState->getRenderTarget();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001431 if (NULL == target) {
1432 return;
1433 }
1434 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001435
1436 // TODO: when underlying api has a direct way to do this we should use it
1437 // (e.g. glDrawPixels on desktop GL).
1438
bsalomon@google.com0342a852012-08-20 19:22:38 +00001439 // If the RT is also a texture and we don't have to premultiply then take the texture path.
1440 // We expect to be at least as fast or faster since it doesn't use an intermediate texture as
1441 // we do below.
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001442
1443#if !GR_MAC_BUILD
1444 // At least some drivers on the Mac get confused when glTexImage2D is called
1445 // on a texture attached to an FBO. The FBO still sees the old image. TODO:
1446 // determine what OS versions and/or HW is affected.
bsalomon@google.com0342a852012-08-20 19:22:38 +00001447 if (NULL != target->asTexture() && !(kUnpremul_PixelOpsFlag & flags)) {
1448 this->writeTexturePixels(target->asTexture(),
1449 left, top, width, height,
1450 config, buffer, rowBytes, flags);
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001451 return;
1452 }
1453#endif
bsalomon@google.com0342a852012-08-20 19:22:38 +00001454 if ((kUnpremul_PixelOpsFlag & flags) &&
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001455 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1456 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
bsalomon@google.com0342a852012-08-20 19:22:38 +00001457 if (!grconfig_to_config8888(config, true, &srcConfig8888) ||
1458 !grconfig_to_config8888(target->config(), false, &dstConfig8888)) {
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001459 return;
1460 }
1461 // allocate a tmp buffer and sw convert the pixels to premul
1462 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(width * height);
1463 const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer);
1464 SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888,
1465 src, rowBytes, srcConfig8888,
1466 width, height);
1467 // upload the already premul pixels
bsalomon@google.com0342a852012-08-20 19:22:38 +00001468 flags &= ~kUnpremul_PixelOpsFlag;
1469 this->writeRenderTargetPixels(target,
1470 left, top,
1471 width, height,
1472 target->config(),
1473 tmpPixels, 4 * width,
1474 flags);
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001475 return;
1476 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001477
1478 bool swapRAndB = fGpu->preferredReadPixelsConfig(config) ==
1479 GrPixelConfigSwapRAndB(config);
1480 if (swapRAndB) {
1481 config = GrPixelConfigSwapRAndB(config);
1482 }
1483
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001484 GrTextureDesc desc;
1485 desc.fWidth = width;
1486 desc.fHeight = height;
1487 desc.fConfig = config;
1488
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001489 GrAutoScratchTexture ast(this, desc);
1490 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001491 if (NULL == texture) {
1492 return;
1493 }
bsalomon@google.com0342a852012-08-20 19:22:38 +00001494 this->writeTexturePixels(texture, 0, 0, width, height,
1495 config, buffer, rowBytes, flags & ~kUnpremul_PixelOpsFlag);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001496
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001497 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001498 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001499
1500 GrMatrix matrix;
1501 matrix.setTranslate(GrIntToScalar(left), GrIntToScalar(top));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001502 drawState->setViewMatrix(matrix);
1503 drawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001504
bsalomon@google.com5c638652011-07-18 19:31:59 +00001505 matrix.setIDiv(texture->width(), texture->height());
bsalomon@google.comb8670992012-07-25 21:27:09 +00001506 drawState->sampler(0)->reset(matrix);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001507 drawState->createTextureEffect(0, texture);
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001508 drawState->sampler(0)->setRAndBSwap(swapRAndB);
bsalomon@google.com0342a852012-08-20 19:22:38 +00001509 drawState->sampler(0)->setPremultiply(SkToBool(kUnpremul_PixelOpsFlag & flags));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001510
tomhudson@google.comb213ed82012-06-25 15:22:12 +00001511 static const GrVertexLayout layout = 0;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001512 static const int VCOUNT = 4;
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001513 // TODO: Use GrGpu::drawRect here
bsalomon@google.com27847de2011-02-22 20:59:41 +00001514 GrDrawTarget::AutoReleaseGeometry geo(fGpu, layout, VCOUNT, 0);
1515 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001516 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +00001517 return;
1518 }
1519 ((GrPoint*)geo.vertices())->setIRectFan(0, 0, width, height);
bsalomon@google.com47059542012-06-06 20:51:20 +00001520 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, VCOUNT);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001521}
1522////////////////////////////////////////////////////////////////////////////////
1523
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001524void GrContext::setPaint(const GrPaint& paint) {
tomhudson@google.comcb325ce2012-07-11 14:41:19 +00001525 GrAssert(fDrawState->stagesDisabled());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001526
1527 for (int i = 0; i < GrPaint::kMaxTextures; ++i) {
1528 int s = i + GrPaint::kFirstTextureStage;
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001529 if (paint.isTextureStageEnabled(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001530 *fDrawState->sampler(s) = paint.getTextureSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001531 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001532 }
1533
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001534 fDrawState->setFirstCoverageStage(GrPaint::kFirstMaskStage);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001535
1536 for (int i = 0; i < GrPaint::kMaxMasks; ++i) {
1537 int s = i + GrPaint::kFirstMaskStage;
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001538 if (paint.isMaskStageEnabled(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001539 *fDrawState->sampler(s) = paint.getMaskSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001540 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001541 }
bsalomon@google.com26936d02012-03-19 13:06:19 +00001542
1543 // disable all stages not accessible via the paint
1544 for (int s = GrPaint::kTotalStages; s < GrDrawState::kNumStages; ++s) {
tomhudson@google.com676e6602012-07-10 17:21:48 +00001545 fDrawState->disableStage(s);
bsalomon@google.com26936d02012-03-19 13:06:19 +00001546 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001547
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001548 fDrawState->setColor(paint.fColor);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001549
1550 if (paint.fDither) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001551 fDrawState->enableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001552 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001553 fDrawState->disableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001554 }
1555 if (paint.fAntiAlias) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001556 fDrawState->enableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001557 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001558 fDrawState->disableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001559 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001560 if (paint.fColorMatrixEnabled) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001561 fDrawState->enableState(GrDrawState::kColorMatrix_StateBit);
1562 fDrawState->setColorMatrix(paint.fColorMatrix);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001563 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001564 fDrawState->disableState(GrDrawState::kColorMatrix_StateBit);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001565 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001566 fDrawState->setBlendFunc(paint.fSrcBlendCoeff, paint.fDstBlendCoeff);
1567 fDrawState->setColorFilter(paint.fColorFilterColor, paint.fColorFilterXfermode);
1568 fDrawState->setCoverage(paint.fCoverage);
reed@google.com4b2d3f32012-05-15 18:05:50 +00001569#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
bsalomon@google.come3d32162012-07-20 13:37:06 +00001570 if ((paint.hasMask() || 0xff != paint.fCoverage) &&
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001571 !fGpu->canApplyCoverage()) {
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001572 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1573 }
bsalomon@google.com95cd7bd2012-03-28 15:35:05 +00001574#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001575}
1576
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001577GrDrawTarget* GrContext::prepareToDraw(const GrPaint* paint, BufferedDraw buffered) {
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001578 if (kNo_BufferedDraw == buffered && kYes_BufferedDraw == fLastDrawWasBuffered) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001579 this->flushDrawBuffer();
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001580 fLastDrawWasBuffered = kNo_BufferedDraw;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001581 }
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001582 if (NULL != paint) {
1583 this->setPaint(*paint);
1584 }
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001585 if (kYes_BufferedDraw == buffered) {
1586 fDrawBuffer->setClip(fGpu->getClip());
1587 fLastDrawWasBuffered = kYes_BufferedDraw;
1588 return fDrawBuffer;
1589 } else {
1590 GrAssert(kNo_BufferedDraw == buffered);
1591 return fGpu;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001592 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001593}
1594
robertphillips@google.com72176b22012-05-23 13:19:12 +00001595/*
1596 * This method finds a path renderer that can draw the specified path on
1597 * the provided target.
1598 * Due to its expense, the software path renderer has split out so it can
1599 * can be individually allowed/disallowed via the "allowSW" boolean.
1600 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001601GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
bsalomon@google.com289533a2011-10-27 12:34:25 +00001602 GrPathFill fill,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001603 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001604 bool antiAlias,
1605 bool allowSW) {
bsalomon@google.com30085192011-08-19 15:42:31 +00001606 if (NULL == fPathRendererChain) {
1607 fPathRendererChain =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001608 SkNEW_ARGS(GrPathRendererChain,
1609 (this, GrPathRendererChain::kNone_UsageFlag));
bsalomon@google.com30085192011-08-19 15:42:31 +00001610 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001611
1612 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path, fill,
1613 target,
1614 antiAlias);
1615
1616 if (NULL == pr && allowSW) {
1617 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001618 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001619 }
1620
1621 pr = fSoftwarePathRenderer;
1622 }
1623
1624 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001625}
1626
bsalomon@google.com27847de2011-02-22 20:59:41 +00001627////////////////////////////////////////////////////////////////////////////////
1628
bsalomon@google.com27847de2011-02-22 20:59:41 +00001629void GrContext::setRenderTarget(GrRenderTarget* target) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001630 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001631 fDrawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001632}
1633
1634GrRenderTarget* GrContext::getRenderTarget() {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001635 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001636}
1637
1638const GrRenderTarget* GrContext::getRenderTarget() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001639 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001640}
1641
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001642bool GrContext::isConfigRenderable(GrPixelConfig config) const {
1643 return fGpu->isConfigRenderable(config);
1644}
1645
bsalomon@google.com27847de2011-02-22 20:59:41 +00001646const GrMatrix& GrContext::getMatrix() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001647 return fDrawState->getViewMatrix();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001648}
1649
1650void GrContext::setMatrix(const GrMatrix& m) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001651 fDrawState->setViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001652}
1653
1654void GrContext::concatMatrix(const GrMatrix& m) const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001655 fDrawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001656}
1657
1658static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
1659 intptr_t mask = 1 << shift;
1660 if (pred) {
1661 bits |= mask;
1662 } else {
1663 bits &= ~mask;
1664 }
1665 return bits;
1666}
1667
bsalomon@google.com583a1e32011-08-17 13:42:46 +00001668GrContext::GrContext(GrGpu* gpu) {
bsalomon@google.comc0af3172012-06-15 14:10:09 +00001669 ++THREAD_INSTANCE_COUNT;
1670
bsalomon@google.com27847de2011-02-22 20:59:41 +00001671 fGpu = gpu;
1672 fGpu->ref();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001673 fGpu->setContext(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001674
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001675 fDrawState = SkNEW(GrDrawState);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001676 fGpu->setDrawState(fDrawState);
1677
bsalomon@google.com30085192011-08-19 15:42:31 +00001678 fPathRendererChain = NULL;
robertphillips@google.com72176b22012-05-23 13:19:12 +00001679 fSoftwarePathRenderer = NULL;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001680
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001681 fTextureCache = SkNEW_ARGS(GrResourceCache,
1682 (MAX_TEXTURE_CACHE_COUNT,
1683 MAX_TEXTURE_CACHE_BYTES));
1684 fFontCache = SkNEW_ARGS(GrFontCache, (fGpu));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001685
bsalomon@google.com1d4edd32012-08-16 18:36:06 +00001686 fLastDrawWasBuffered = kNo_BufferedDraw;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001687
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001688 fDrawBuffer = NULL;
1689 fDrawBufferVBAllocPool = NULL;
1690 fDrawBufferIBAllocPool = NULL;
1691
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001692 fAARectRenderer = SkNEW(GrAARectRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001693
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
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001731GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture,
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001732 bool canClobberSrc,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001733 const SkRect& rect,
1734 float sigmaX, float sigmaY) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001735 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001736 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001737 AutoMatrix avm(this, GrMatrix::I());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001738 SkIRect clearRect;
bsalomon@google.comb505a122012-05-31 18:40:36 +00001739 int scaleFactorX, radiusX;
1740 int scaleFactorY, radiusY;
1741 sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX);
1742 sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY);
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001743
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001744 SkRect srcRect(rect);
1745 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
1746 srcRect.roundOut();
robertphillips@google.com8637a362012-04-10 18:32:35 +00001747 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
1748 static_cast<float>(scaleFactorY));
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001749
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001750 AutoClip acs(this, srcRect);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001751
bsalomon@google.com0342a852012-08-20 19:22:38 +00001752 GrAssert(kBGRA_8888_GrPixelConfig == srcTexture->config() ||
1753 kRGBA_8888_GrPixelConfig == srcTexture->config() ||
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001754 kAlpha_8_GrPixelConfig == srcTexture->config());
1755
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001756 GrTextureDesc desc;
1757 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1758 desc.fWidth = SkScalarFloorToInt(srcRect.width());
1759 desc.fHeight = SkScalarFloorToInt(srcRect.height());
1760 desc.fConfig = srcTexture->config();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001761
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001762 GrAutoScratchTexture temp1, temp2;
1763 GrTexture* dstTexture = temp1.set(this, desc);
1764 GrTexture* tempTexture = canClobberSrc ? srcTexture : temp2.set(this, desc);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001765
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001766 GrPaint paint;
1767 paint.reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +00001768 paint.textureSampler(0)->textureParams()->setBilerp(true);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001769
1770 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
1771 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1772 srcTexture->height());
1773 this->setRenderTarget(dstTexture->asRenderTarget());
1774 SkRect dstRect(srcRect);
1775 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
1776 i < scaleFactorY ? 0.5f : 1.0f);
tomhudson@google.comaa72eab2012-07-19 18:01:07 +00001777 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
1778 (srcTexture)))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001779 this->drawRectToRect(paint, dstRect, srcRect);
1780 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001781 srcTexture = dstTexture;
1782 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001783 }
1784
robertphillips@google.com7a396332012-05-10 15:11:27 +00001785 SkIRect srcIRect;
1786 srcRect.roundOut(&srcIRect);
1787
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001788 if (sigmaX > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001789 if (scaleFactorX > 1) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001790 // Clear out a radius to the right of the srcRect to prevent the
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001791 // X convolution from reading garbage.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001792 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001793 radiusX, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001794 this->clear(&clearRect, 0x0);
1795 }
1796
1797 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001798 GrDrawTarget* target = this->prepareToDraw(NULL, DEFAULT_BUFFERING);
1799 convolve_gaussian(target, srcTexture, srcRect, sigmaX, radiusX,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001800 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001801 srcTexture = dstTexture;
1802 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001803 }
1804
1805 if (sigmaY > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001806 if (scaleFactorY > 1 || sigmaX > 0.0f) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001807 // Clear out a radius below the srcRect to prevent the Y
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001808 // convolution from reading garbage.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001809 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001810 srcIRect.width(), radiusY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001811 this->clear(&clearRect, 0x0);
1812 }
1813
1814 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001815 GrDrawTarget* target = this->prepareToDraw(NULL, DEFAULT_BUFFERING);
1816 convolve_gaussian(target, srcTexture, srcRect, sigmaY, radiusY,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001817 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001818 srcTexture = dstTexture;
1819 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001820 }
1821
1822 if (scaleFactorX > 1 || scaleFactorY > 1) {
1823 // Clear one pixel to the right and below, to accommodate bilinear
1824 // upsampling.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001825 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
1826 srcIRect.width() + 1, 1);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001827 this->clear(&clearRect, 0x0);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001828 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
1829 1, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001830 this->clear(&clearRect, 0x0);
1831 // FIXME: This should be mitchell, not bilinear.
bsalomon@google.comb8670992012-07-25 21:27:09 +00001832 paint.textureSampler(0)->textureParams()->setBilerp(true);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001833 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1834 srcTexture->height());
1835 this->setRenderTarget(dstTexture->asRenderTarget());
tomhudson@google.comaa72eab2012-07-19 18:01:07 +00001836 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
1837 (srcTexture)))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001838 SkRect dstRect(srcRect);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001839 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001840 this->drawRectToRect(paint, dstRect, srcRect);
1841 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001842 srcTexture = dstTexture;
1843 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001844 }
1845 this->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001846 if (srcTexture == temp1.texture()) {
1847 return temp1.detach();
1848 } else if (srcTexture == temp2.texture()) {
1849 return temp2.detach();
1850 } else {
1851 srcTexture->ref();
1852 return srcTexture;
1853 }
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001854}
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001855
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001856GrTexture* GrContext::applyMorphology(GrTexture* srcTexture,
1857 const GrRect& rect,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001858 MorphologyType morphType,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001859 SkISize radius) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001860 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001861 srcTexture->ref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001862 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001863
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001864 AutoMatrix avm(this, GrMatrix::I());
1865
1866 AutoClip acs(this, GrRect::MakeWH(SkIntToScalar(srcTexture->width()),
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001867 SkIntToScalar(srcTexture->height())));
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001868 GrTextureDesc desc;
1869 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1870 desc.fWidth = SkScalarCeilToInt(rect.width());
1871 desc.fHeight = SkScalarCeilToInt(rect.height());
bsalomon@google.com0342a852012-08-20 19:22:38 +00001872 desc.fConfig = kRGBA_8888_GrPixelConfig;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001873 if (radius.fWidth > 0) {
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001874 GrAutoScratchTexture ast(this, desc);
1875 this->setRenderTarget(ast.texture()->asRenderTarget());
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001876 GrDrawTarget* target = this->prepareToDraw(NULL, DEFAULT_BUFFERING);
1877 apply_morphology(target, srcTexture, rect, radius.fWidth, morphType,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001878 Gr1DKernelEffect::kX_Direction);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001879 SkIRect clearRect = SkIRect::MakeXYWH(
1880 SkScalarFloorToInt(rect.fLeft),
1881 SkScalarFloorToInt(rect.fBottom),
1882 SkScalarFloorToInt(rect.width()),
1883 radius.fHeight);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001884 this->clear(&clearRect, 0x0);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001885 srcTexture->unref();
1886 srcTexture = ast.detach();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001887 }
1888 if (radius.fHeight > 0) {
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001889 GrAutoScratchTexture ast(this, desc);
1890 this->setRenderTarget(ast.texture()->asRenderTarget());
bsalomon@google.com07ea2db2012-08-17 14:06:49 +00001891 GrDrawTarget* target = this->prepareToDraw(NULL, DEFAULT_BUFFERING);
1892 apply_morphology(target, srcTexture, rect, radius.fHeight, morphType,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001893 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001894 srcTexture->unref();
1895 srcTexture = ast.detach();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001896 }
1897 this->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001898 return srcTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001899}
bsalomon@google.comc4364992011-11-07 15:54:49 +00001900
1901///////////////////////////////////////////////////////////////////////////////