blob: a8b680850203e140ab5c30114fbca9801d95e8d3 [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.com3c4d0322012-04-03 18:04:51 +000033#define DEFER_TEXT_RENDERING 1
bsalomon@google.com27847de2011-02-22 20:59:41 +000034
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000035#define DEFER_PATHS 1
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +000036
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000037#define BATCH_RECT_TO_RECT (1 && !GR_STATIC_RECT_VB)
bsalomon@google.com27847de2011-02-22 20:59:41 +000038
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +000039#define MAX_BLUR_SIGMA 4.0f
40
bsalomon@google.comd46e2422011-09-23 17:40:07 +000041// When we're using coverage AA but the blend is incompatible (given gpu
42// limitations) should we disable AA or draw wrong?
bsalomon@google.com950d7a82011-09-28 15:05:33 +000043#define DISABLE_COVERAGE_AA_FOR_BLEND 1
bsalomon@google.comd46e2422011-09-23 17:40:07 +000044
reed@google.com4b2d3f32012-05-15 18:05:50 +000045#if GR_DEBUG
46 // change this to a 1 to see notifications when partial coverage fails
47 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
48#else
49 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
50#endif
51
bsalomon@google.com07fc0d12012-06-22 15:15:59 +000052static const size_t MAX_TEXTURE_CACHE_COUNT = 256;
53static const size_t MAX_TEXTURE_CACHE_BYTES = 16 * 1024 * 1024;
bsalomon@google.com27847de2011-02-22 20:59:41 +000054
bsalomon@google.com60361492012-03-15 17:47:06 +000055static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
bsalomon@google.com27847de2011-02-22 20:59:41 +000056static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
57
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +000058// path rendering is the only thing we defer today that uses non-static indices
59static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = DEFER_PATHS ? 1 << 11 : 0;
60static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = DEFER_PATHS ? 4 : 0;
bsalomon@google.com27847de2011-02-22 20:59:41 +000061
bsalomon@google.combc4b6542011-11-19 13:56:11 +000062#define ASSERT_OWNED_RESOURCE(R) GrAssert(!(R) || (R)->getContext() == this)
63
bsalomon@google.com05ef5102011-05-02 21:14:59 +000064GrContext* GrContext::Create(GrEngine engine,
65 GrPlatform3DContext context3D) {
bsalomon@google.com27847de2011-02-22 20:59:41 +000066 GrContext* ctx = NULL;
67 GrGpu* fGpu = GrGpu::Create(engine, context3D);
68 if (NULL != fGpu) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000069 ctx = SkNEW_ARGS(GrContext, (fGpu));
bsalomon@google.com27847de2011-02-22 20:59:41 +000070 fGpu->unref();
71 }
72 return ctx;
73}
74
bsalomon@google.comc0af3172012-06-15 14:10:09 +000075namespace {
76void* CreateThreadInstanceCount() {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000077 return SkNEW_ARGS(int, (0));
bsalomon@google.comc0af3172012-06-15 14:10:09 +000078}
79void DeleteThreadInstanceCount(void* v) {
80 delete reinterpret_cast<int*>(v);
81}
82#define THREAD_INSTANCE_COUNT \
83 (*reinterpret_cast<int*>(SkTLS::Get(CreateThreadInstanceCount, \
84 DeleteThreadInstanceCount)))
85
86}
87
88int GrContext::GetThreadInstanceCount() {
89 return THREAD_INSTANCE_COUNT;
90}
91
bsalomon@google.com27847de2011-02-22 20:59:41 +000092GrContext::~GrContext() {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000093 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +000094
95 // Since the gpu can hold scratch textures, give it a chance to let go
96 // of them before freeing the texture cache
97 fGpu->purgeResources();
98
bsalomon@google.com27847de2011-02-22 20:59:41 +000099 delete fTextureCache;
100 delete fFontCache;
101 delete fDrawBuffer;
102 delete fDrawBufferVBAllocPool;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000103 delete fDrawBufferIBAllocPool;
bsalomon@google.com30085192011-08-19 15:42:31 +0000104
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000105 fAARectRenderer->unref();
106
bsalomon@google.com205d4602011-04-25 12:43:45 +0000107 fGpu->unref();
bsalomon@google.com30085192011-08-19 15:42:31 +0000108 GrSafeUnref(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000109 GrSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000110 fDrawState->unref();
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000111
112 --THREAD_INSTANCE_COUNT;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000113}
114
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000115void GrContext::contextLost() {
junov@google.com53a55842011-06-08 22:55:10 +0000116 contextDestroyed();
117 this->setupDrawBuffer();
118}
119
120void GrContext::contextDestroyed() {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000121 // abandon first to so destructors
122 // don't try to free the resources in the API.
123 fGpu->abandonResources();
124
bsalomon@google.com30085192011-08-19 15:42:31 +0000125 // a path renderer may be holding onto resources that
126 // are now unusable
127 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000128 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000129
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000130 delete fDrawBuffer;
131 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000132
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000133 delete fDrawBufferVBAllocPool;
134 fDrawBufferVBAllocPool = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000135
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000136 delete fDrawBufferIBAllocPool;
137 fDrawBufferIBAllocPool = NULL;
138
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000139 fAARectRenderer->reset();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000140
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000141 fTextureCache->removeAll();
142 fFontCache->freeAll();
143 fGpu->markContextDirty();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000144}
145
146void GrContext::resetContext() {
147 fGpu->markContextDirty();
148}
149
150void GrContext::freeGpuResources() {
151 this->flush();
robertphillips@google.comff175842012-05-14 19:31:39 +0000152
153 fGpu->purgeResources();
154
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000155 fAARectRenderer->reset();
156
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000157 fTextureCache->removeAll();
158 fFontCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000159 // a path renderer may be holding onto resources
160 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000161 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000162}
163
twiz@google.com05e70242012-01-27 19:12:00 +0000164size_t GrContext::getGpuTextureCacheBytes() const {
165 return fTextureCache->getCachedResourceBytes();
166}
167
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000168////////////////////////////////////////////////////////////////////////////////
169
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000170namespace {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000171
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000172void scale_rect(SkRect* rect, float xScale, float yScale) {
robertphillips@google.com5af56062012-04-27 15:39:52 +0000173 rect->fLeft = SkScalarMul(rect->fLeft, SkFloatToScalar(xScale));
174 rect->fTop = SkScalarMul(rect->fTop, SkFloatToScalar(yScale));
175 rect->fRight = SkScalarMul(rect->fRight, SkFloatToScalar(xScale));
176 rect->fBottom = SkScalarMul(rect->fBottom, SkFloatToScalar(yScale));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000177}
178
bsalomon@google.comb505a122012-05-31 18:40:36 +0000179float adjust_sigma(float sigma, int *scaleFactor, int *radius) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000180 *scaleFactor = 1;
181 while (sigma > MAX_BLUR_SIGMA) {
182 *scaleFactor *= 2;
183 sigma *= 0.5f;
184 }
bsalomon@google.comb505a122012-05-31 18:40:36 +0000185 *radius = static_cast<int>(ceilf(sigma * 3.0f));
186 GrAssert(*radius <= GrConvolutionEffect::kMaxKernelRadius);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000187 return sigma;
188}
189
190void apply_morphology(GrGpu* gpu,
191 GrTexture* texture,
192 const SkRect& rect,
193 int radius,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000194 GrContext::MorphologyType morphType,
195 Gr1DKernelEffect::Direction direction) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000196
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000197 GrRenderTarget* target = gpu->drawState()->getRenderTarget();
198 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000199 GrDrawState* drawState = gpu->drawState();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000200 drawState->setRenderTarget(target);
201 GrMatrix sampleM;
202 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000203 drawState->sampler(0)->reset(sampleM);
204 SkAutoTUnref<GrCustomStage> morph(
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000205 SkNEW_ARGS(GrMorphologyEffect, (texture, direction, radius, morphType)));
bsalomon@google.comb505a122012-05-31 18:40:36 +0000206 drawState->sampler(0)->setCustomStage(morph);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000207 gpu->drawSimpleRect(rect, NULL);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000208}
209
bsalomon@google.comb505a122012-05-31 18:40:36 +0000210void convolve_gaussian(GrGpu* gpu,
211 GrTexture* texture,
212 const SkRect& rect,
213 float sigma,
214 int radius,
215 Gr1DKernelEffect::Direction direction) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000216 GrRenderTarget* target = gpu->drawState()->getRenderTarget();
217 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000218 GrDrawState* drawState = gpu->drawState();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000219 drawState->setRenderTarget(target);
220 GrMatrix sampleM;
221 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000222 drawState->sampler(0)->reset(sampleM);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000223 SkAutoTUnref<GrConvolutionEffect> conv(SkNEW_ARGS(GrConvolutionEffect,
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000224 (texture, direction, radius,
225 sigma)));
bsalomon@google.comb505a122012-05-31 18:40:36 +0000226 drawState->sampler(0)->setCustomStage(conv);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000227 gpu->drawSimpleRect(rect, NULL);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000228}
229
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000230}
231
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000232GrTexture* GrContext::findAndLockTexture(const GrTextureDesc& desc,
233 const GrCacheData& cacheData,
234 const GrTextureParams* params) {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000235 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000236 GrResource* resource = fTextureCache->findAndLock(resourceKey,
237 GrResourceCache::kNested_LockType);
238 return static_cast<GrTexture*>(resource);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000239}
240
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000241bool GrContext::isTextureInCache(const GrTextureDesc& desc,
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000242 const GrCacheData& cacheData,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000243 const GrTextureParams* params) const {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000244 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000245 return fTextureCache->hasKey(resourceKey);
246}
247
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000248void GrContext::addAndLockStencilBuffer(GrStencilBuffer* sb) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000249 ASSERT_OWNED_RESOURCE(sb);
robertphillips@google.com46a86002012-08-08 10:42:44 +0000250
251 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(sb->width(),
252 sb->height(),
253 sb->numSamples());
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000254 fTextureCache->createAndLock(resourceKey, sb);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000255}
256
257GrStencilBuffer* GrContext::findStencilBuffer(int width, int height,
258 int sampleCnt) {
robertphillips@google.com46a86002012-08-08 10:42:44 +0000259 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(width,
260 height,
261 sampleCnt);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000262 GrResource* resource = fTextureCache->findAndLock(resourceKey,
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000263 GrResourceCache::kSingle_LockType);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000264 return static_cast<GrStencilBuffer*>(resource);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000265}
266
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000267void GrContext::unlockStencilBuffer(GrStencilBuffer* sb) {
268 ASSERT_OWNED_RESOURCE(sb);
269 GrAssert(NULL != sb->getCacheEntry());
270
271 fTextureCache->unlock(sb->getCacheEntry());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000272}
273
274static void stretchImage(void* dst,
275 int dstW,
276 int dstH,
277 void* src,
278 int srcW,
279 int srcH,
280 int bpp) {
281 GrFixed dx = (srcW << 16) / dstW;
282 GrFixed dy = (srcH << 16) / dstH;
283
284 GrFixed y = dy >> 1;
285
286 int dstXLimit = dstW*bpp;
287 for (int j = 0; j < dstH; ++j) {
288 GrFixed x = dx >> 1;
289 void* srcRow = (uint8_t*)src + (y>>16)*srcW*bpp;
290 void* dstRow = (uint8_t*)dst + j*dstW*bpp;
291 for (int i = 0; i < dstXLimit; i += bpp) {
292 memcpy((uint8_t*) dstRow + i,
293 (uint8_t*) srcRow + (x>>16)*bpp,
294 bpp);
295 x += dx;
296 }
297 y += dy;
298 }
299}
300
robertphillips@google.com3319f332012-08-13 18:00:36 +0000301// The desired texture is NPOT and tiled but that isn't supported by
302// the current hardware. Resize the texture to be a POT
303GrTexture* GrContext::createResizedTexture(const GrTextureDesc& desc,
304 const GrCacheData& cacheData,
305 void* srcData,
306 size_t rowBytes,
307 bool needsFiltering) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000308 GrTexture* clampedTexture = this->findAndLockTexture(desc, cacheData, NULL);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000309
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000310 if (NULL == clampedTexture) {
311 clampedTexture = this->createAndLockTexture(NULL, desc, cacheData, srcData, rowBytes);
312 GrAssert(NULL != clampedTexture);
313 if (NULL == clampedTexture) {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000314 return NULL;
315 }
316 }
317 GrTextureDesc rtDesc = desc;
318 rtDesc.fFlags = rtDesc.fFlags |
319 kRenderTarget_GrTextureFlagBit |
320 kNoStencil_GrTextureFlagBit;
321 rtDesc.fWidth = GrNextPow2(GrMax(desc.fWidth, 64));
322 rtDesc.fHeight = GrNextPow2(GrMax(desc.fHeight, 64));
323
324 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
325
326 if (NULL != texture) {
327 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
328 GrDrawState* drawState = fGpu->drawState();
329 drawState->setRenderTarget(texture->asRenderTarget());
330
331 // if filtering is not desired then we want to ensure all
332 // texels in the resampled image are copies of texels from
333 // the original.
334 drawState->sampler(0)->reset(SkShader::kClamp_TileMode,
335 needsFiltering);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000336 drawState->createTextureEffect(0, clampedTexture);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000337
338 static const GrVertexLayout layout =
339 GrDrawTarget::StageTexCoordVertexLayoutBit(0,0);
340 GrDrawTarget::AutoReleaseGeometry arg(fGpu, layout, 4, 0);
341
342 if (arg.succeeded()) {
343 GrPoint* verts = (GrPoint*) arg.vertices();
344 verts[0].setIRectFan(0, 0,
345 texture->width(),
346 texture->height(),
347 2*sizeof(GrPoint));
348 verts[1].setIRectFan(0, 0, 1, 1, 2*sizeof(GrPoint));
349 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType,
350 0, 4);
351 }
352 texture->releaseRenderTarget();
353 } else {
354 // TODO: Our CPU stretch doesn't filter. But we create separate
355 // stretched textures when the sampler state is either filtered or
356 // not. Either implement filtered stretch blit on CPU or just create
357 // one when FBO case fails.
358
359 rtDesc.fFlags = kNone_GrTextureFlags;
360 // no longer need to clamp at min RT size.
361 rtDesc.fWidth = GrNextPow2(desc.fWidth);
362 rtDesc.fHeight = GrNextPow2(desc.fHeight);
363 int bpp = GrBytesPerPixel(desc.fConfig);
364 SkAutoSMalloc<128*128*4> stretchedPixels(bpp *
365 rtDesc.fWidth *
366 rtDesc.fHeight);
367 stretchImage(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
368 srcData, desc.fWidth, desc.fHeight, bpp);
369
370 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
371
372 GrTexture* texture = fGpu->createTexture(rtDesc,
373 stretchedPixels.get(),
374 stretchedRowBytes);
375 GrAssert(NULL != texture);
376 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000377 this->unlockTexture(clampedTexture);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000378
379 return texture;
380}
381
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000382GrTexture* GrContext::createAndLockTexture(
bsalomon@google.comb8670992012-07-25 21:27:09 +0000383 const GrTextureParams* params,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000384 const GrTextureDesc& desc,
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000385 const GrCacheData& cacheData,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000386 void* srcData,
387 size_t rowBytes) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000388 SK_TRACE_EVENT0("GrContext::createAndLockTexture");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000389
390#if GR_DUMP_TEXTURE_UPLOAD
391 GrPrintf("GrContext::createAndLockTexture [%d %d]\n", desc.fWidth, desc.fHeight);
392#endif
393
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000394 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000395
robertphillips@google.com3319f332012-08-13 18:00:36 +0000396 GrTexture* texture = NULL;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000397 if (GrTexture::NeedsResizing(resourceKey)) {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000398 texture = this->createResizedTexture(desc, cacheData,
399 srcData, rowBytes,
400 GrTexture::NeedsFiltering(resourceKey));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000401 } else {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000402 texture = fGpu->createTexture(desc, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000403 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000404
405 if (NULL != texture) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000406 fTextureCache->createAndLock(resourceKey, texture);
robertphillips@google.com3319f332012-08-13 18:00:36 +0000407 }
408
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000409 return texture;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000410}
411
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000412GrTexture* GrContext::lockScratchTexture(const GrTextureDesc& inDesc,
413 ScratchTexMatch match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000414 GrTextureDesc desc = inDesc;
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000415 GrCacheData cacheData(GrCacheData::kScratch_CacheID);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000416
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000417 if (kExact_ScratchTexMatch != match) {
418 // bin by pow2 with a reasonable min
419 static const int MIN_SIZE = 256;
420 desc.fWidth = GrMax(MIN_SIZE, GrNextPow2(desc.fWidth));
421 desc.fHeight = GrMax(MIN_SIZE, GrNextPow2(desc.fHeight));
422 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000423
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000424 GrResource* resource = NULL;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000425 int origWidth = desc.fWidth;
426 int origHeight = desc.fHeight;
427 bool doubledW = false;
428 bool doubledH = false;
429
430 do {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000431 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL, desc, cacheData, true);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000432 resource = fTextureCache->findAndLock(key,
433 GrResourceCache::kNested_LockType);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000434 // if we miss, relax the fit of the flags...
435 // then try doubling width... then height.
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000436 if (NULL != resource || kExact_ScratchTexMatch == match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000437 break;
438 }
439 if (!(desc.fFlags & kRenderTarget_GrTextureFlagBit)) {
440 desc.fFlags = desc.fFlags | kRenderTarget_GrTextureFlagBit;
441 } else if (desc.fFlags & kNoStencil_GrTextureFlagBit) {
442 desc.fFlags = desc.fFlags & ~kNoStencil_GrTextureFlagBit;
443 } else if (!doubledW) {
444 desc.fFlags = inDesc.fFlags;
445 desc.fWidth *= 2;
446 doubledW = true;
447 } else if (!doubledH) {
448 desc.fFlags = inDesc.fFlags;
449 desc.fWidth = origWidth;
450 desc.fHeight *= 2;
451 doubledH = true;
452 } else {
453 break;
454 }
455
456 } while (true);
457
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000458 if (NULL == resource) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000459 desc.fFlags = inDesc.fFlags;
460 desc.fWidth = origWidth;
461 desc.fHeight = origHeight;
462 GrTexture* texture = fGpu->createTexture(desc, NULL, 0);
463 if (NULL != texture) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000464 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000465 texture->desc(),
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000466 cacheData,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000467 true);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000468 fTextureCache->createAndLock(key, texture);
469 resource = texture;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000470 }
471 }
472
473 // If the caller gives us the same desc/sampler twice we don't want
474 // to return the same texture the second time (unless it was previously
475 // released). So we detach the entry from the cache and reattach at release.
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000476 if (NULL != resource) {
477 fTextureCache->detach(resource->getCacheEntry());
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000478 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000479 return static_cast<GrTexture*>(resource);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000480}
481
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000482void GrContext::addExistingTextureToCache(GrTexture* texture) {
483
484 if (NULL == texture) {
485 return;
486 }
487
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000488 // 'texture' is a scratch texture returning to the fold
489 GrCacheData cacheData(GrCacheData::kScratch_CacheID);
490
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000491 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
492 texture->desc(),
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000493 cacheData,
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000494 true);
495 fTextureCache->attach(key, texture);
496}
497
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000498void GrContext::unlockTexture(GrTexture* texture) {
499 ASSERT_OWNED_RESOURCE(texture);
500 GrAssert(NULL != texture->getCacheEntry());
501
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000502 // If this is a scratch texture we detached it from the cache
503 // while it was locked (to avoid two callers simultaneously getting
504 // the same texture).
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000505 if (GrTexture::IsScratchTexture(texture->getCacheEntry()->key())) {
506 fTextureCache->reattachAndUnlock(texture->getCacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000507 } else {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000508 fTextureCache->unlock(texture->getCacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000509 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000510}
511
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000512void GrContext::freeEntry(GrTexture* texture) {
513 ASSERT_OWNED_RESOURCE(texture);
514 GrAssert(NULL != texture->getCacheEntry());
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000515
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000516 fTextureCache->freeEntry(texture->getCacheEntry());
517 texture->setCacheEntry(NULL);
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000518}
519
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000520GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000521 void* srcData,
522 size_t rowBytes) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000523 GrTextureDesc descCopy = descIn;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000524 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000525}
526
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000527void GrContext::getTextureCacheLimits(int* maxTextures,
528 size_t* maxTextureBytes) const {
529 fTextureCache->getLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000530}
531
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000532void GrContext::setTextureCacheLimits(int maxTextures, size_t maxTextureBytes) {
533 fTextureCache->setLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000534}
535
bsalomon@google.com91958362011-06-13 17:58:13 +0000536int GrContext::getMaxTextureSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000537 return fGpu->getCaps().fMaxTextureSize;
bsalomon@google.com91958362011-06-13 17:58:13 +0000538}
539
540int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000541 return fGpu->getCaps().fMaxRenderTargetSize;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000542}
543
544///////////////////////////////////////////////////////////////////////////////
545
bsalomon@google.come269f212011-11-07 13:29:52 +0000546GrTexture* GrContext::createPlatformTexture(const GrPlatformTextureDesc& desc) {
547 return fGpu->createPlatformTexture(desc);
548}
549
550GrRenderTarget* GrContext::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
551 return fGpu->createPlatformRenderTarget(desc);
552}
553
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000554///////////////////////////////////////////////////////////////////////////////
555
bsalomon@google.comb8670992012-07-25 21:27:09 +0000556bool GrContext::supportsIndex8PixelConfig(const GrTextureParams* params,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000557 int width, int height) const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000558 const GrDrawTarget::Caps& caps = fGpu->getCaps();
559 if (!caps.f8BitPaletteSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000560 return false;
561 }
562
bsalomon@google.com27847de2011-02-22 20:59:41 +0000563 bool isPow2 = GrIsPow2(width) && GrIsPow2(height);
564
565 if (!isPow2) {
bsalomon@google.comb8670992012-07-25 21:27:09 +0000566 bool tiled = NULL != params && params->isTiled();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000567 if (tiled && !caps.fNPOTTextureTileSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000568 return false;
569 }
570 }
571 return true;
572}
573
574////////////////////////////////////////////////////////////////////////////////
575
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000576const GrClipData* GrContext::getClip() const {
577 return fGpu->getClip();
578}
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000579
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000580void GrContext::setClip(const GrClipData* clipData) {
581 fGpu->setClip(clipData);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000582 fDrawState->enableState(GrDrawState::kClip_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000583}
584
bsalomon@google.com27847de2011-02-22 20:59:41 +0000585////////////////////////////////////////////////////////////////////////////////
586
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000587void GrContext::clear(const GrIRect* rect,
588 const GrColor color,
589 GrRenderTarget* target) {
bsalomon@google.com398109c2011-04-14 18:40:27 +0000590 this->flush();
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000591 fGpu->clear(rect, color, target);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000592}
593
594void GrContext::drawPaint(const GrPaint& paint) {
595 // set rect to be big enough to fill the space, but not super-huge, so we
596 // don't overflow fixed-point implementations
bsalomon@google.comd302f142011-03-03 13:54:13 +0000597 GrRect r;
598 r.setLTRB(0, 0,
599 GrIntToScalar(getRenderTarget()->width()),
600 GrIntToScalar(getRenderTarget()->height()));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000601 GrMatrix inverse;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000602 SkTLazy<GrPaint> tmpPaint;
603 const GrPaint* p = &paint;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000604 AutoMatrix am;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000605
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000606 // We attempt to map r by the inverse matrix and draw that. mapRect will
607 // map the four corners and bound them with a new rect. This will not
608 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000609 if (!this->getMatrix().hasPerspective()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000610 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000611 GrPrintf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000612 return;
613 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000614 inverse.mapRect(&r);
615 } else {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000616 if (paint.hasTextureOrMask()) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000617 tmpPaint.set(paint);
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000618 p = tmpPaint.get();
bsalomon@google.come3d32162012-07-20 13:37:06 +0000619 if (!tmpPaint.get()->preConcatSamplerMatricesWithInverse(fDrawState->getViewMatrix())) {
620 GrPrintf("Could not invert matrix\n");
621 }
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000622 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000623 am.set(this, GrMatrix::I());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000624 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000625 // by definition this fills the entire clip, no need for AA
626 if (paint.fAntiAlias) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000627 if (!tmpPaint.isValid()) {
628 tmpPaint.set(paint);
629 p = tmpPaint.get();
630 }
631 GrAssert(p == tmpPaint.get());
632 tmpPaint.get()->fAntiAlias = false;
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000633 }
634 this->drawRect(*p, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000635}
636
bsalomon@google.com205d4602011-04-25 12:43:45 +0000637////////////////////////////////////////////////////////////////////////////////
638
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000639namespace {
640inline bool disable_coverage_aa_for_blend(GrDrawTarget* target) {
641 return DISABLE_COVERAGE_AA_FOR_BLEND && !target->canApplyCoverage();
642}
643}
644
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000645////////////////////////////////////////////////////////////////////////////////
646
bsalomon@google.com27847de2011-02-22 20:59:41 +0000647/* create a triangle strip that strokes the specified triangle. There are 8
648 unique vertices, but we repreat the last 2 to close up. Alternatively we
649 could use an indices array, and then only send 8 verts, but not sure that
650 would be faster.
651 */
bsalomon@google.com205d4602011-04-25 12:43:45 +0000652static void setStrokeRectStrip(GrPoint verts[10], GrRect rect,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000653 GrScalar width) {
654 const GrScalar rad = GrScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000655 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000656
657 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
658 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
659 verts[2].set(rect.fRight - rad, rect.fTop + rad);
660 verts[3].set(rect.fRight + rad, rect.fTop - rad);
661 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
662 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
663 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
664 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
665 verts[8] = verts[0];
666 verts[9] = verts[1];
667}
668
reed@google.com20efde72011-05-09 17:00:02 +0000669/**
670 * Returns true if the rects edges are integer-aligned.
671 */
672static bool isIRect(const GrRect& r) {
673 return GrScalarIsInt(r.fLeft) && GrScalarIsInt(r.fTop) &&
674 GrScalarIsInt(r.fRight) && GrScalarIsInt(r.fBottom);
675}
676
bsalomon@google.com205d4602011-04-25 12:43:45 +0000677static bool apply_aa_to_rect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000678 const GrRect& rect,
679 GrScalar width,
680 const GrMatrix* matrix,
681 GrMatrix* combinedMatrix,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000682 GrRect* devRect,
683 bool* useVertexCoverage) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000684 // we use a simple coverage ramp to do aa on axis-aligned rects
685 // we check if the rect will be axis-aligned, and the rect won't land on
686 // integer coords.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000687
bsalomon@google.coma3108262011-10-10 14:08:47 +0000688 // we are keeping around the "tweak the alpha" trick because
689 // it is our only hope for the fixed-pipe implementation.
690 // In a shader implementation we can give a separate coverage input
bsalomon@google.com289533a2011-10-27 12:34:25 +0000691 // TODO: remove this ugliness when we drop the fixed-pipe impl
bsalomon@google.coma3108262011-10-10 14:08:47 +0000692 *useVertexCoverage = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000693 if (!target->canTweakAlphaForCoverage()) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000694 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +0000695#if GR_DEBUG
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000696 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000697#endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000698 return false;
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000699 } else {
700 *useVertexCoverage = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000701 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000702 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000703 const GrDrawState& drawState = target->getDrawState();
704 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000705 return false;
706 }
707
bsalomon@google.com471d4712011-08-23 15:45:25 +0000708 if (0 == width && target->willUseHWAALines()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000709 return false;
710 }
711
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000712 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000713 return false;
714 }
715
716 if (NULL != matrix &&
717 !matrix->preservesAxisAlignment()) {
718 return false;
719 }
720
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000721 *combinedMatrix = drawState.getViewMatrix();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000722 if (NULL != matrix) {
723 combinedMatrix->preConcat(*matrix);
724 GrAssert(combinedMatrix->preservesAxisAlignment());
725 }
726
727 combinedMatrix->mapRect(devRect, rect);
728 devRect->sort();
729
730 if (width < 0) {
reed@google.com20efde72011-05-09 17:00:02 +0000731 return !isIRect(*devRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000732 } else {
733 return true;
734 }
735}
736
bsalomon@google.com27847de2011-02-22 20:59:41 +0000737void GrContext::drawRect(const GrPaint& paint,
738 const GrRect& rect,
739 GrScalar width,
740 const GrMatrix* matrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000741 SK_TRACE_EVENT0("GrContext::drawRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000742
743 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000744 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000745
bsalomon@google.com205d4602011-04-25 12:43:45 +0000746 GrRect devRect = rect;
747 GrMatrix combinedMatrix;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000748 bool useVertexCoverage;
bsalomon@google.com289533a2011-10-27 12:34:25 +0000749 bool needAA = paint.fAntiAlias &&
750 !this->getRenderTarget()->isMultisampled();
751 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix,
752 &combinedMatrix, &devRect,
753 &useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000754
755 if (doAA) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000756 GrDrawTarget::AutoDeviceCoordDraw adcd(target);
757 if (!adcd.succeeded()) {
758 return;
759 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000760 if (width >= 0) {
761 GrVec strokeSize;;
762 if (width > 0) {
763 strokeSize.set(width, width);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000764 combinedMatrix.mapVectors(&strokeSize, 1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000765 strokeSize.setAbs(strokeSize);
766 } else {
767 strokeSize.set(GR_Scalar1, GR_Scalar1);
768 }
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000769 fAARectRenderer->strokeAARect(this->getGpu(), target, devRect,
770 strokeSize, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000771 } else {
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000772 fAARectRenderer->fillAARect(this->getGpu(), target,
773 devRect, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000774 }
775 return;
776 }
777
bsalomon@google.com27847de2011-02-22 20:59:41 +0000778 if (width >= 0) {
779 // TODO: consider making static vertex buffers for these cases.
780 // Hairline could be done by just adding closing vertex to
781 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000782
bsalomon@google.com27847de2011-02-22 20:59:41 +0000783 static const int worstCaseVertCount = 10;
bsalomon@google.come3d32162012-07-20 13:37:06 +0000784 GrDrawTarget::AutoReleaseGeometry geo(target, 0, worstCaseVertCount, 0);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000785
786 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000787 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000788 return;
789 }
790
791 GrPrimitiveType primType;
792 int vertCount;
793 GrPoint* vertex = geo.positions();
794
795 if (width > 0) {
796 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000797 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000798 setStrokeRectStrip(vertex, rect, width);
799 } else {
800 // hairline
801 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000802 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000803 vertex[0].set(rect.fLeft, rect.fTop);
804 vertex[1].set(rect.fRight, rect.fTop);
805 vertex[2].set(rect.fRight, rect.fBottom);
806 vertex[3].set(rect.fLeft, rect.fBottom);
807 vertex[4].set(rect.fLeft, rect.fTop);
808 }
809
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000810 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000811 if (NULL != matrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000812 GrDrawState* drawState = target->drawState();
813 avmr.set(drawState);
814 drawState->preConcatViewMatrix(*matrix);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000815 drawState->preConcatSamplerMatrices(*matrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000816 }
817
818 target->drawNonIndexed(primType, 0, vertCount);
819 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000820#if GR_STATIC_RECT_VB
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000821 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
822 if (NULL == sqVB) {
823 GrPrintf("Failed to create static rect vb.\n");
824 return;
825 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000826 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000827 GrDrawState* drawState = target->drawState();
828 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000829 GrMatrix m;
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000830 m.setAll(rect.width(), 0, rect.fLeft,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000831 0, rect.height(), rect.fTop,
832 0, 0, GrMatrix::I()[8]);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000833
834 if (NULL != matrix) {
835 m.postConcat(*matrix);
836 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000837 drawState->preConcatViewMatrix(m);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000838 drawState->preConcatSamplerMatrices(m);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000839
bsalomon@google.com47059542012-06-06 20:51:20 +0000840 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000841#else
bsalomon@google.come3d32162012-07-20 13:37:06 +0000842 target->drawSimpleRect(rect, matrix);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000843#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000844 }
845}
846
847void GrContext::drawRectToRect(const GrPaint& paint,
848 const GrRect& dstRect,
849 const GrRect& srcRect,
850 const GrMatrix* dstMatrix,
851 const GrMatrix* srcMatrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000852 SK_TRACE_EVENT0("GrContext::drawRectToRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000853
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000854 // srcRect refers to paint's first texture
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000855 if (!paint.isTextureStageEnabled(0)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000856 drawRect(paint, dstRect, -1, dstMatrix);
857 return;
858 }
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000859
bsalomon@google.com27847de2011-02-22 20:59:41 +0000860 GR_STATIC_ASSERT(!BATCH_RECT_TO_RECT || !GR_STATIC_RECT_VB);
861
862#if GR_STATIC_RECT_VB
863 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000864 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000865 GrDrawState* drawState = target->drawState();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000866 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000867
868 GrMatrix m;
869
870 m.setAll(dstRect.width(), 0, dstRect.fLeft,
871 0, dstRect.height(), dstRect.fTop,
872 0, 0, GrMatrix::I()[8]);
873 if (NULL != dstMatrix) {
874 m.postConcat(*dstMatrix);
875 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000876 drawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000877
bsalomon@google.come3d32162012-07-20 13:37:06 +0000878 // we explicitly setup the correct coords for the first stage. The others
879 // must know about the view matrix change.
880 for (int s = 1; s < GrPaint::kTotalStages; ++s) {
881 if (drawState->isStageEnabled(s)) {
882 drawState->sampler(s)->preConcatMatrix(m);
883 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000884 }
885
bsalomon@google.com27847de2011-02-22 20:59:41 +0000886 m.setAll(srcRect.width(), 0, srcRect.fLeft,
887 0, srcRect.height(), srcRect.fTop,
888 0, 0, GrMatrix::I()[8]);
889 if (NULL != srcMatrix) {
890 m.postConcat(*srcMatrix);
891 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000892 drawState->sampler(GrPaint::kFirstTextureStage)->preConcatMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000893
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000894 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
895 if (NULL == sqVB) {
896 GrPrintf("Failed to create static rect vb.\n");
897 return;
898 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000899 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com47059542012-06-06 20:51:20 +0000900 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000901#else
902
903 GrDrawTarget* target;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000904#if BATCH_RECT_TO_RECT
bsalomon@google.com27847de2011-02-22 20:59:41 +0000905 target = this->prepareToDraw(paint, kBuffered_DrawCategory);
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000906#else
bsalomon@google.com27847de2011-02-22 20:59:41 +0000907 target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
908#endif
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000909 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000910
tomhudson@google.com93813632011-10-27 20:21:16 +0000911 const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
912 const GrMatrix* srcMatrices[GrDrawState::kNumStages] = {NULL};
bsalomon@google.com27847de2011-02-22 20:59:41 +0000913 srcRects[0] = &srcRect;
914 srcMatrices[0] = srcMatrix;
915
bsalomon@google.come3d32162012-07-20 13:37:06 +0000916 target->drawRect(dstRect, dstMatrix, srcRects, srcMatrices);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000917#endif
918}
919
920void GrContext::drawVertices(const GrPaint& paint,
921 GrPrimitiveType primitiveType,
922 int vertexCount,
923 const GrPoint positions[],
924 const GrPoint texCoords[],
925 const GrColor colors[],
926 const uint16_t indices[],
927 int indexCount) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000928 SK_TRACE_EVENT0("GrContext::drawVertices");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000929
930 GrDrawTarget::AutoReleaseGeometry geo;
931
932 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000933 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000934
bsalomon@google.come3d32162012-07-20 13:37:06 +0000935 GrVertexLayout layout = 0;
936 if (NULL != texCoords) {
937 layout |= GrDrawTarget::StageTexCoordVertexLayoutBit(0, 0);
938 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000939 if (NULL != colors) {
940 layout |= GrDrawTarget::kColor_VertexLayoutBit;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000941 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000942 int vertexSize = GrDrawTarget::VertexSize(layout);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000943
944 if (sizeof(GrPoint) != vertexSize) {
945 if (!geo.set(target, layout, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000946 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000947 return;
948 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000949 int texOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000950 int colorOffset;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000951 GrDrawTarget::VertexSizeAndOffsetsByIdx(layout,
952 texOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000953 &colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000954 NULL,
955 NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000956 void* curVertex = geo.vertices();
957
958 for (int i = 0; i < vertexCount; ++i) {
959 *((GrPoint*)curVertex) = positions[i];
960
961 if (texOffsets[0] > 0) {
962 *(GrPoint*)((intptr_t)curVertex + texOffsets[0]) = texCoords[i];
963 }
964 if (colorOffset > 0) {
965 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
966 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000967 curVertex = (void*)((intptr_t)curVertex + vertexSize);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000968 }
969 } else {
970 target->setVertexSourceToArray(layout, positions, vertexCount);
971 }
972
bsalomon@google.com91958362011-06-13 17:58:13 +0000973 // we don't currently apply offscreen AA to this path. Need improved
974 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000975
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000976 if (NULL != indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000977 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000978 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000979 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000980 target->drawNonIndexed(primitiveType, 0, vertexCount);
981 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000982}
983
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000984///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com150d2842012-01-12 20:19:56 +0000985namespace {
986
bsalomon@google.com93c96602012-04-27 13:05:21 +0000987struct CircleVertex {
988 GrPoint fPos;
989 GrPoint fCenter;
990 GrScalar fOuterRadius;
991 GrScalar fInnerRadius;
992};
993
994/* Returns true if will map a circle to another circle. This can be true
995 * if the matrix only includes square-scale, rotation, translation.
996 */
997inline bool isSimilarityTransformation(const SkMatrix& matrix,
998 SkScalar tol = SK_ScalarNearlyZero) {
999 if (matrix.isIdentity() || matrix.getType() == SkMatrix::kTranslate_Mask) {
1000 return true;
1001 }
1002 if (matrix.hasPerspective()) {
1003 return false;
1004 }
1005
1006 SkScalar mx = matrix.get(SkMatrix::kMScaleX);
1007 SkScalar sx = matrix.get(SkMatrix::kMSkewX);
1008 SkScalar my = matrix.get(SkMatrix::kMScaleY);
1009 SkScalar sy = matrix.get(SkMatrix::kMSkewY);
1010
1011 if (mx == 0 && sx == 0 && my == 0 && sy == 0) {
1012 return false;
1013 }
1014
1015 // it has scales or skews, but it could also be rotation, check it out.
1016 SkVector vec[2];
1017 vec[0].set(mx, sx);
1018 vec[1].set(sy, my);
1019
1020 return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) &&
1021 SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(),
1022 SkScalarSquare(tol));
1023}
1024
1025}
1026
1027// TODO: strokeWidth can't be larger than zero right now.
1028// It will be fixed when drawPath() can handle strokes.
1029void GrContext::drawOval(const GrPaint& paint,
1030 const GrRect& rect,
1031 SkScalar strokeWidth) {
bsalomon@google.com0982d352012-07-31 15:33:25 +00001032 GrAssert(strokeWidth <= 0);
1033 if (!isSimilarityTransformation(this->getMatrix()) ||
bsalomon@google.com93c96602012-04-27 13:05:21 +00001034 !paint.fAntiAlias ||
1035 rect.height() != rect.width()) {
1036 SkPath path;
1037 path.addOval(rect);
1038 GrPathFill fill = (strokeWidth == 0) ?
bsalomon@google.com0982d352012-07-31 15:33:25 +00001039 kHairLine_GrPathFill : kWinding_GrPathFill;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001040 this->internalDrawPath(paint, path, fill, NULL);
1041 return;
1042 }
1043
bsalomon@google.com0982d352012-07-31 15:33:25 +00001044 DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
1045 kUnbuffered_DrawCategory;
1046 GrDrawTarget* target = this->prepareToDraw(paint, category);
1047 GrDrawState* drawState = target->drawState();
1048 GrDrawState::AutoStageDisable atr(fDrawState);
1049 const GrMatrix vm = drawState->getViewMatrix();
1050
bsalomon@google.com93c96602012-04-27 13:05:21 +00001051 const GrRenderTarget* rt = drawState->getRenderTarget();
1052 if (NULL == rt) {
1053 return;
1054 }
1055
bsalomon@google.come3d32162012-07-20 13:37:06 +00001056 GrDrawTarget::AutoDeviceCoordDraw adcd(target);
1057 if (!adcd.succeeded()) {
1058 return;
1059 }
bsalomon@google.com93c96602012-04-27 13:05:21 +00001060
bsalomon@google.come3d32162012-07-20 13:37:06 +00001061 GrVertexLayout layout = GrDrawTarget::kEdge_VertexLayoutBit;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001062 GrAssert(sizeof(CircleVertex) == GrDrawTarget::VertexSize(layout));
1063
1064 GrPoint center = GrPoint::Make(rect.centerX(), rect.centerY());
1065 GrScalar radius = SkScalarHalf(rect.width());
1066
1067 vm.mapPoints(&center, 1);
1068 radius = vm.mapRadius(radius);
1069
1070 GrScalar outerRadius = radius;
1071 GrScalar innerRadius = 0;
1072 SkScalar halfWidth = 0;
1073 if (strokeWidth == 0) {
1074 halfWidth = SkScalarHalf(SK_Scalar1);
1075
1076 outerRadius += halfWidth;
1077 innerRadius = SkMaxScalar(0, radius - halfWidth);
1078 }
1079
1080 GrDrawTarget::AutoReleaseGeometry geo(target, layout, 4, 0);
1081 if (!geo.succeeded()) {
1082 GrPrintf("Failed to get space for vertices!\n");
1083 return;
1084 }
1085
1086 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
1087
robertphillips@google.coma0a66c12012-06-22 13:14:29 +00001088 // The fragment shader will extend the radius out half a pixel
1089 // to antialias. Expand the drawn rect here so all the pixels
1090 // will be captured.
1091 SkScalar L = center.fX - outerRadius - SkFloatToScalar(0.5f);
1092 SkScalar R = center.fX + outerRadius + SkFloatToScalar(0.5f);
1093 SkScalar T = center.fY - outerRadius - SkFloatToScalar(0.5f);
1094 SkScalar B = center.fY + outerRadius + SkFloatToScalar(0.5f);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001095
1096 verts[0].fPos = SkPoint::Make(L, T);
1097 verts[1].fPos = SkPoint::Make(R, T);
1098 verts[2].fPos = SkPoint::Make(L, B);
1099 verts[3].fPos = SkPoint::Make(R, B);
1100
1101 for (int i = 0; i < 4; ++i) {
1102 // this goes to fragment shader, it should be in y-points-up space.
1103 verts[i].fCenter = SkPoint::Make(center.fX, rt->height() - center.fY);
1104
1105 verts[i].fOuterRadius = outerRadius;
1106 verts[i].fInnerRadius = innerRadius;
1107 }
1108
1109 drawState->setVertexEdgeType(GrDrawState::kCircle_EdgeType);
bsalomon@google.com47059542012-06-06 20:51:20 +00001110 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001111}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001112
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001113void GrContext::drawPath(const GrPaint& paint, const SkPath& path,
reed@google.com07f3ee12011-05-16 17:21:57 +00001114 GrPathFill fill, const GrPoint* translate) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001115
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001116 if (path.isEmpty()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001117 if (GrIsFillInverted(fill)) {
1118 this->drawPaint(paint);
1119 }
1120 return;
1121 }
1122
bsalomon@google.com93c96602012-04-27 13:05:21 +00001123 SkRect ovalRect;
1124 if (!GrIsFillInverted(fill) && path.isOval(&ovalRect)) {
1125 if (translate) {
1126 ovalRect.offset(*translate);
1127 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001128 SkScalar width = (fill == kHairLine_GrPathFill) ? 0 : -SK_Scalar1;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001129 this->drawOval(paint, ovalRect, width);
1130 return;
1131 }
1132
1133 internalDrawPath(paint, path, fill, translate);
1134}
1135
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001136void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +00001137 GrPathFill fill, const GrPoint* translate) {
1138
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001139 // Note that below we may sw-rasterize the path into a scratch texture.
1140 // Scratch textures can be recycled after they are returned to the texture
1141 // cache. This presents a potential hazard for buffered drawing. However,
1142 // the writePixels that uploads to the scratch will perform a flush so we're
1143 // OK.
1144 DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
1145 kUnbuffered_DrawCategory;
1146 GrDrawTarget* target = this->prepareToDraw(paint, category);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +00001147 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001148
bsalomon@google.com289533a2011-10-27 12:34:25 +00001149 bool prAA = paint.fAntiAlias && !this->getRenderTarget()->isMultisampled();
1150
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001151 // An Assumption here is that path renderer would use some form of tweaking
1152 // the src color (either the input alpha or in the frag shader) to implement
1153 // aa. If we have some future driver-mojo path AA that can do the right
1154 // thing WRT to the blend then we'll need some query on the PR.
1155 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001156#if GR_DEBUG
bsalomon@google.com979432b2011-11-05 21:38:22 +00001157 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001158#endif
bsalomon@google.com289533a2011-10-27 12:34:25 +00001159 prAA = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001160 }
bsalomon@google.com289533a2011-10-27 12:34:25 +00001161
robertphillips@google.com72176b22012-05-23 13:19:12 +00001162 GrPathRenderer* pr = this->getPathRenderer(path, fill, target, prAA, true);
bsalomon@google.com30085192011-08-19 15:42:31 +00001163 if (NULL == pr) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001164#if GR_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001165 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001166#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001167 return;
1168 }
1169
bsalomon@google.come3d32162012-07-20 13:37:06 +00001170 pr->drawPath(path, fill, translate, target, prAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001171}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001172
bsalomon@google.com27847de2011-02-22 20:59:41 +00001173////////////////////////////////////////////////////////////////////////////////
1174
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001175void GrContext::flush(int flagsBitfield) {
1176 if (kDiscard_FlushBit & flagsBitfield) {
1177 fDrawBuffer->reset();
1178 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001179 this->flushDrawBuffer();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001180 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001181 if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001182 fGpu->forceRenderTargetFlush();
1183 }
1184}
1185
bsalomon@google.com27847de2011-02-22 20:59:41 +00001186void GrContext::flushDrawBuffer() {
junov@google.com53a55842011-06-08 22:55:10 +00001187 if (fDrawBuffer) {
robertphillips@google.com58b38182012-05-03 16:29:41 +00001188 // With addition of the AA clip path, flushing the draw buffer can
1189 // result in the generation of an AA clip mask. During this
1190 // process the SW path renderer may be invoked which recusively
1191 // calls this method (via internalWriteTexturePixels) creating
1192 // infinite recursion
1193 GrInOrderDrawBuffer* temp = fDrawBuffer;
1194 fDrawBuffer = NULL;
1195
1196 temp->flushTo(fGpu);
1197
1198 fDrawBuffer = temp;
junov@google.com53a55842011-06-08 22:55:10 +00001199 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001200}
1201
bsalomon@google.com6f379512011-11-16 20:36:03 +00001202void GrContext::internalWriteTexturePixels(GrTexture* texture,
1203 int left, int top,
1204 int width, int height,
1205 GrPixelConfig config,
1206 const void* buffer,
1207 size_t rowBytes,
1208 uint32_t flags) {
1209 SK_TRACE_EVENT0("GrContext::writeTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001210 ASSERT_OWNED_RESOURCE(texture);
1211
bsalomon@google.com6f379512011-11-16 20:36:03 +00001212 if (!(kDontFlush_PixelOpsFlag & flags)) {
1213 this->flush();
1214 }
1215 // TODO: use scratch texture to perform conversion
1216 if (GrPixelConfigIsUnpremultiplied(texture->config()) !=
1217 GrPixelConfigIsUnpremultiplied(config)) {
1218 return;
1219 }
1220
1221 fGpu->writeTexturePixels(texture, left, top, width, height,
1222 config, buffer, rowBytes);
1223}
1224
1225bool GrContext::internalReadTexturePixels(GrTexture* texture,
1226 int left, int top,
1227 int width, int height,
1228 GrPixelConfig config,
1229 void* buffer,
1230 size_t rowBytes,
1231 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001232 SK_TRACE_EVENT0("GrContext::readTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001233 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001234
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001235 // TODO: code read pixels for textures that aren't also rendertargets
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001236 GrRenderTarget* target = texture->asRenderTarget();
1237 if (NULL != target) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001238 return this->internalReadRenderTargetPixels(target,
1239 left, top, width, height,
1240 config, buffer, rowBytes,
1241 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001242 } else {
1243 return false;
1244 }
1245}
1246
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001247#include "SkConfig8888.h"
1248
1249namespace {
1250/**
1251 * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel
1252 * formats are representable as Config8888 and so the function returns false
1253 * if the GrPixelConfig has no equivalent Config8888.
1254 */
1255bool grconfig_to_config8888(GrPixelConfig config,
1256 SkCanvas::Config8888* config8888) {
1257 switch (config) {
1258 case kRGBA_8888_PM_GrPixelConfig:
1259 *config8888 = SkCanvas::kRGBA_Premul_Config8888;
1260 return true;
1261 case kRGBA_8888_UPM_GrPixelConfig:
1262 *config8888 = SkCanvas::kRGBA_Unpremul_Config8888;
1263 return true;
1264 case kBGRA_8888_PM_GrPixelConfig:
1265 *config8888 = SkCanvas::kBGRA_Premul_Config8888;
1266 return true;
1267 case kBGRA_8888_UPM_GrPixelConfig:
1268 *config8888 = SkCanvas::kBGRA_Unpremul_Config8888;
1269 return true;
1270 default:
1271 return false;
1272 }
1273}
1274}
1275
bsalomon@google.com6f379512011-11-16 20:36:03 +00001276bool GrContext::internalReadRenderTargetPixels(GrRenderTarget* target,
1277 int left, int top,
1278 int width, int height,
1279 GrPixelConfig config,
1280 void* buffer,
1281 size_t rowBytes,
1282 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001283 SK_TRACE_EVENT0("GrContext::readRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001284 ASSERT_OWNED_RESOURCE(target);
1285
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001286 if (NULL == target) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001287 target = fDrawState->getRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001288 if (NULL == target) {
1289 return false;
1290 }
1291 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001292
bsalomon@google.com6f379512011-11-16 20:36:03 +00001293 if (!(kDontFlush_PixelOpsFlag & flags)) {
1294 this->flush();
1295 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001296
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001297 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1298 GrPixelConfigIsUnpremultiplied(config) &&
1299 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1300 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1301 if (!grconfig_to_config8888(target->config(), &srcConfig8888) ||
1302 !grconfig_to_config8888(config, &dstConfig8888)) {
1303 return false;
1304 }
1305 // do read back using target's own config
1306 this->internalReadRenderTargetPixels(target,
1307 left, top,
1308 width, height,
1309 target->config(),
1310 buffer, rowBytes,
1311 kDontFlush_PixelOpsFlag);
1312 // sw convert the pixels to unpremul config
1313 uint32_t* pixels = reinterpret_cast<uint32_t*>(buffer);
1314 SkConvertConfig8888Pixels(pixels, rowBytes, dstConfig8888,
1315 pixels, rowBytes, srcConfig8888,
1316 width, height);
1317 return true;
1318 }
1319
bsalomon@google.comc4364992011-11-07 15:54:49 +00001320 GrTexture* src = target->asTexture();
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001321 bool swapRAndB = NULL != src &&
1322 fGpu->preferredReadPixelsConfig(config) ==
1323 GrPixelConfigSwapRAndB(config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001324
1325 bool flipY = NULL != src &&
1326 fGpu->readPixelsWillPayForYFlip(target, left, top,
1327 width, height, config,
1328 rowBytes);
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001329 bool alphaConversion = (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1330 GrPixelConfigIsUnpremultiplied(config));
bsalomon@google.comc4364992011-11-07 15:54:49 +00001331
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001332 if (NULL == src && alphaConversion) {
1333 // we should fallback to cpu conversion here. This could happen when
1334 // we were given an external render target by the client that is not
1335 // also a texture (e.g. FBO 0 in GL)
1336 return false;
1337 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001338 // we draw to a scratch texture if any of these conversion are applied
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001339 GrAutoScratchTexture ast;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001340 if (flipY || swapRAndB || alphaConversion) {
1341 GrAssert(NULL != src);
1342 if (swapRAndB) {
1343 config = GrPixelConfigSwapRAndB(config);
1344 GrAssert(kUnknown_GrPixelConfig != config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001345 }
1346 // Make the scratch a render target because we don't have a robust
1347 // readTexturePixels as of yet (it calls this function).
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001348 GrTextureDesc desc;
1349 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1350 desc.fWidth = width;
1351 desc.fHeight = height;
1352 desc.fConfig = config;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001353
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001354 // When a full readback is faster than a partial we could always make
1355 // the scratch exactly match the passed rect. However, if we see many
1356 // different size rectangles we will trash our texture cache and pay the
1357 // cost of creating and destroying many textures. So, we only request
1358 // an exact match when the caller is reading an entire RT.
1359 ScratchTexMatch match = kApprox_ScratchTexMatch;
1360 if (0 == left &&
1361 0 == top &&
1362 target->width() == width &&
1363 target->height() == height &&
1364 fGpu->fullReadPixelsIsFasterThanPartial()) {
1365 match = kExact_ScratchTexMatch;
1366 }
1367 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001368 GrTexture* texture = ast.texture();
1369 if (!texture) {
1370 return false;
1371 }
1372 target = texture->asRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001373 GrAssert(NULL != target);
1374
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001375 GrDrawTarget::AutoStateRestore asr(fGpu,
1376 GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001377 GrDrawState* drawState = fGpu->drawState();
1378 drawState->setRenderTarget(target);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001379
bsalomon@google.comc4364992011-11-07 15:54:49 +00001380 GrMatrix matrix;
1381 if (flipY) {
1382 matrix.setTranslate(SK_Scalar1 * left,
1383 SK_Scalar1 * (top + height));
1384 matrix.set(GrMatrix::kMScaleY, -GR_Scalar1);
1385 } else {
1386 matrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
1387 }
1388 matrix.postIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001389 drawState->sampler(0)->reset(matrix);
1390 drawState->sampler(0)->setRAndBSwap(swapRAndB);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001391 drawState->createTextureEffect(0, src);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001392 GrRect rect;
1393 rect.setXYWH(0, 0, SK_Scalar1 * width, SK_Scalar1 * height);
bsalomon@google.come3d32162012-07-20 13:37:06 +00001394 fGpu->drawSimpleRect(rect, NULL);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001395 left = 0;
1396 top = 0;
1397 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001398 return fGpu->readPixels(target,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001399 left, top, width, height,
1400 config, buffer, rowBytes, flipY);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001401}
1402
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001403void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1404 GrAssert(target);
1405 ASSERT_OWNED_RESOURCE(target);
1406 // In the future we may track whether there are any pending draws to this
1407 // target. We don't today so we always perform a flush. We don't promise
1408 // this to our clients, though.
1409 this->flush();
1410 fGpu->resolveRenderTarget(target);
1411}
1412
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001413void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst) {
1414 if (NULL == src || NULL == dst) {
1415 return;
1416 }
1417 ASSERT_OWNED_RESOURCE(src);
1418
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001419 // Writes pending to the source texture are not tracked, so a flush
1420 // is required to ensure that the copy captures the most recent contents
1421 // of the source texture. See similar behaviour in
1422 // GrContext::resolveRenderTarget.
1423 this->flush();
1424
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001425 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001426 GrDrawState* drawState = fGpu->drawState();
1427 drawState->setRenderTarget(dst);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001428 GrMatrix sampleM;
1429 sampleM.setIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001430 drawState->sampler(0)->reset(sampleM);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001431 drawState->createTextureEffect(0, src);
bsalomon@google.com5db3b6c2012-01-12 20:38:57 +00001432 SkRect rect = SkRect::MakeXYWH(0, 0,
1433 SK_Scalar1 * src->width(),
1434 SK_Scalar1 * src->height());
bsalomon@google.come3d32162012-07-20 13:37:06 +00001435 fGpu->drawSimpleRect(rect, NULL);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001436}
1437
bsalomon@google.com6f379512011-11-16 20:36:03 +00001438void GrContext::internalWriteRenderTargetPixels(GrRenderTarget* target,
1439 int left, int top,
1440 int width, int height,
1441 GrPixelConfig config,
1442 const void* buffer,
1443 size_t rowBytes,
1444 uint32_t flags) {
1445 SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001446 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001447
1448 if (NULL == target) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001449 target = fDrawState->getRenderTarget();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001450 if (NULL == target) {
1451 return;
1452 }
1453 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001454
1455 // TODO: when underlying api has a direct way to do this we should use it
1456 // (e.g. glDrawPixels on desktop GL).
1457
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001458 // If the RT is also a texture and we don't have to do PM/UPM conversion
1459 // then take the texture path, which we expect to be at least as fast or
1460 // faster since it doesn't use an intermediate texture as we do below.
1461
1462#if !GR_MAC_BUILD
1463 // At least some drivers on the Mac get confused when glTexImage2D is called
1464 // on a texture attached to an FBO. The FBO still sees the old image. TODO:
1465 // determine what OS versions and/or HW is affected.
1466 if (NULL != target->asTexture() &&
1467 GrPixelConfigIsUnpremultiplied(target->config()) ==
1468 GrPixelConfigIsUnpremultiplied(config)) {
1469
1470 this->internalWriteTexturePixels(target->asTexture(),
1471 left, top, width, height,
1472 config, buffer, rowBytes, flags);
1473 return;
1474 }
1475#endif
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001476 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1477 GrPixelConfigIsUnpremultiplied(config) &&
1478 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1479 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1480 if (!grconfig_to_config8888(config, &srcConfig8888) ||
1481 !grconfig_to_config8888(target->config(), &dstConfig8888)) {
1482 return;
1483 }
1484 // allocate a tmp buffer and sw convert the pixels to premul
1485 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(width * height);
1486 const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer);
1487 SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888,
1488 src, rowBytes, srcConfig8888,
1489 width, height);
1490 // upload the already premul pixels
1491 this->internalWriteRenderTargetPixels(target,
1492 left, top,
1493 width, height,
1494 target->config(),
1495 tmpPixels, 4 * width, flags);
1496 return;
1497 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001498
1499 bool swapRAndB = fGpu->preferredReadPixelsConfig(config) ==
1500 GrPixelConfigSwapRAndB(config);
1501 if (swapRAndB) {
1502 config = GrPixelConfigSwapRAndB(config);
1503 }
1504
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001505 GrTextureDesc desc;
1506 desc.fWidth = width;
1507 desc.fHeight = height;
1508 desc.fConfig = config;
1509
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001510 GrAutoScratchTexture ast(this, desc);
1511 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001512 if (NULL == texture) {
1513 return;
1514 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001515 this->internalWriteTexturePixels(texture, 0, 0, width, height,
1516 config, buffer, rowBytes, flags);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001517
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001518 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001519 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001520
1521 GrMatrix matrix;
1522 matrix.setTranslate(GrIntToScalar(left), GrIntToScalar(top));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001523 drawState->setViewMatrix(matrix);
1524 drawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001525
bsalomon@google.com5c638652011-07-18 19:31:59 +00001526 matrix.setIDiv(texture->width(), texture->height());
bsalomon@google.comb8670992012-07-25 21:27:09 +00001527 drawState->sampler(0)->reset(matrix);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001528 drawState->createTextureEffect(0, texture);
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001529 drawState->sampler(0)->setRAndBSwap(swapRAndB);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001530
tomhudson@google.comb213ed82012-06-25 15:22:12 +00001531 static const GrVertexLayout layout = 0;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001532 static const int VCOUNT = 4;
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001533 // TODO: Use GrGpu::drawRect here
bsalomon@google.com27847de2011-02-22 20:59:41 +00001534 GrDrawTarget::AutoReleaseGeometry geo(fGpu, layout, VCOUNT, 0);
1535 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001536 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +00001537 return;
1538 }
1539 ((GrPoint*)geo.vertices())->setIRectFan(0, 0, width, height);
bsalomon@google.com47059542012-06-06 20:51:20 +00001540 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, VCOUNT);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001541}
1542////////////////////////////////////////////////////////////////////////////////
1543
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001544void GrContext::setPaint(const GrPaint& paint) {
tomhudson@google.comcb325ce2012-07-11 14:41:19 +00001545 GrAssert(fDrawState->stagesDisabled());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001546
1547 for (int i = 0; i < GrPaint::kMaxTextures; ++i) {
1548 int s = i + GrPaint::kFirstTextureStage;
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001549 if (paint.isTextureStageEnabled(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001550 *fDrawState->sampler(s) = paint.getTextureSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001551 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001552 }
1553
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001554 fDrawState->setFirstCoverageStage(GrPaint::kFirstMaskStage);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001555
1556 for (int i = 0; i < GrPaint::kMaxMasks; ++i) {
1557 int s = i + GrPaint::kFirstMaskStage;
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001558 if (paint.isMaskStageEnabled(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001559 *fDrawState->sampler(s) = paint.getMaskSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001560 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001561 }
bsalomon@google.com26936d02012-03-19 13:06:19 +00001562
1563 // disable all stages not accessible via the paint
1564 for (int s = GrPaint::kTotalStages; s < GrDrawState::kNumStages; ++s) {
tomhudson@google.com676e6602012-07-10 17:21:48 +00001565 fDrawState->disableStage(s);
bsalomon@google.com26936d02012-03-19 13:06:19 +00001566 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001567
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001568 fDrawState->setColor(paint.fColor);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001569
1570 if (paint.fDither) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001571 fDrawState->enableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001572 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001573 fDrawState->disableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001574 }
1575 if (paint.fAntiAlias) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001576 fDrawState->enableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001577 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001578 fDrawState->disableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001579 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001580 if (paint.fColorMatrixEnabled) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001581 fDrawState->enableState(GrDrawState::kColorMatrix_StateBit);
1582 fDrawState->setColorMatrix(paint.fColorMatrix);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001583 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001584 fDrawState->disableState(GrDrawState::kColorMatrix_StateBit);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001585 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001586 fDrawState->setBlendFunc(paint.fSrcBlendCoeff, paint.fDstBlendCoeff);
1587 fDrawState->setColorFilter(paint.fColorFilterColor, paint.fColorFilterXfermode);
1588 fDrawState->setCoverage(paint.fCoverage);
reed@google.com4b2d3f32012-05-15 18:05:50 +00001589#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
bsalomon@google.come3d32162012-07-20 13:37:06 +00001590 if ((paint.hasMask() || 0xff != paint.fCoverage) &&
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001591 !fGpu->canApplyCoverage()) {
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001592 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1593 }
bsalomon@google.com95cd7bd2012-03-28 15:35:05 +00001594#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001595}
1596
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001597GrDrawTarget* GrContext::prepareToDraw(const GrPaint& paint,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001598 DrawCategory category) {
1599 if (category != fLastDrawCategory) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001600 this->flushDrawBuffer();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001601 fLastDrawCategory = category;
1602 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001603 this->setPaint(paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001604 GrDrawTarget* target = fGpu;
1605 switch (category) {
bsalomon@google.com193395c2012-03-30 17:35:12 +00001606 case kUnbuffered_DrawCategory:
1607 target = fGpu;
1608 break;
1609 case kBuffered_DrawCategory:
1610 target = fDrawBuffer;
1611 fDrawBuffer->setClip(fGpu->getClip());
1612 break;
1613 default:
1614 GrCrash("Unexpected DrawCategory.");
1615 break;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001616 }
1617 return target;
1618}
1619
robertphillips@google.com72176b22012-05-23 13:19:12 +00001620/*
1621 * This method finds a path renderer that can draw the specified path on
1622 * the provided target.
1623 * Due to its expense, the software path renderer has split out so it can
1624 * can be individually allowed/disallowed via the "allowSW" boolean.
1625 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001626GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
bsalomon@google.com289533a2011-10-27 12:34:25 +00001627 GrPathFill fill,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001628 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001629 bool antiAlias,
1630 bool allowSW) {
bsalomon@google.com30085192011-08-19 15:42:31 +00001631 if (NULL == fPathRendererChain) {
1632 fPathRendererChain =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001633 SkNEW_ARGS(GrPathRendererChain,
1634 (this, GrPathRendererChain::kNone_UsageFlag));
bsalomon@google.com30085192011-08-19 15:42:31 +00001635 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001636
1637 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path, fill,
1638 target,
1639 antiAlias);
1640
1641 if (NULL == pr && allowSW) {
1642 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001643 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001644 }
1645
1646 pr = fSoftwarePathRenderer;
1647 }
1648
1649 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001650}
1651
bsalomon@google.com27847de2011-02-22 20:59:41 +00001652////////////////////////////////////////////////////////////////////////////////
1653
bsalomon@google.com27847de2011-02-22 20:59:41 +00001654void GrContext::setRenderTarget(GrRenderTarget* target) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001655 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001656 if (fDrawState->getRenderTarget() != target) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001657 this->flush(false);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001658 fDrawState->setRenderTarget(target);
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001659 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001660}
1661
1662GrRenderTarget* GrContext::getRenderTarget() {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001663 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001664}
1665
1666const GrRenderTarget* GrContext::getRenderTarget() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001667 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001668}
1669
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001670bool GrContext::isConfigRenderable(GrPixelConfig config) const {
1671 return fGpu->isConfigRenderable(config);
1672}
1673
bsalomon@google.com27847de2011-02-22 20:59:41 +00001674const GrMatrix& GrContext::getMatrix() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001675 return fDrawState->getViewMatrix();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001676}
1677
1678void GrContext::setMatrix(const GrMatrix& m) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001679 fDrawState->setViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001680}
1681
1682void GrContext::concatMatrix(const GrMatrix& m) const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001683 fDrawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001684}
1685
1686static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
1687 intptr_t mask = 1 << shift;
1688 if (pred) {
1689 bits |= mask;
1690 } else {
1691 bits &= ~mask;
1692 }
1693 return bits;
1694}
1695
bsalomon@google.com583a1e32011-08-17 13:42:46 +00001696GrContext::GrContext(GrGpu* gpu) {
bsalomon@google.comc0af3172012-06-15 14:10:09 +00001697 ++THREAD_INSTANCE_COUNT;
1698
bsalomon@google.com27847de2011-02-22 20:59:41 +00001699 fGpu = gpu;
1700 fGpu->ref();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001701 fGpu->setContext(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001702
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001703 fDrawState = SkNEW(GrDrawState);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001704 fGpu->setDrawState(fDrawState);
1705
bsalomon@google.com30085192011-08-19 15:42:31 +00001706 fPathRendererChain = NULL;
robertphillips@google.com72176b22012-05-23 13:19:12 +00001707 fSoftwarePathRenderer = NULL;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001708
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001709 fTextureCache = SkNEW_ARGS(GrResourceCache,
1710 (MAX_TEXTURE_CACHE_COUNT,
1711 MAX_TEXTURE_CACHE_BYTES));
1712 fFontCache = SkNEW_ARGS(GrFontCache, (fGpu));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001713
1714 fLastDrawCategory = kUnbuffered_DrawCategory;
1715
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001716 fDrawBuffer = NULL;
1717 fDrawBufferVBAllocPool = NULL;
1718 fDrawBufferIBAllocPool = NULL;
1719
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001720 fAARectRenderer = SkNEW(GrAARectRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001721
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001722 this->setupDrawBuffer();
1723}
1724
1725void GrContext::setupDrawBuffer() {
1726
1727 GrAssert(NULL == fDrawBuffer);
1728 GrAssert(NULL == fDrawBufferVBAllocPool);
1729 GrAssert(NULL == fDrawBufferIBAllocPool);
1730
bsalomon@google.com92edd312012-04-04 21:40:21 +00001731#if DEFER_TEXT_RENDERING || BATCH_RECT_TO_RECT || DEFER_PATHS
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001732 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001733 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001734 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001735 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001736 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001737 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001738 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001739 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001740
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001741 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com471d4712011-08-23 15:45:25 +00001742 fDrawBufferVBAllocPool,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001743 fDrawBufferIBAllocPool));
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001744#endif
1745
1746#if BATCH_RECT_TO_RECT
bsalomon@google.com27847de2011-02-22 20:59:41 +00001747 fDrawBuffer->setQuadIndexBuffer(this->getQuadIndexBuffer());
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001748#endif
bsalomon@google.com1015e032012-06-25 18:41:04 +00001749 if (fDrawBuffer) {
1750 fDrawBuffer->setAutoFlushTarget(fGpu);
1751 fDrawBuffer->setDrawState(fDrawState);
1752 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001753}
1754
bsalomon@google.com27847de2011-02-22 20:59:41 +00001755GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001756#if DEFER_TEXT_RENDERING
bsalomon@google.com193395c2012-03-30 17:35:12 +00001757 return prepareToDraw(paint, kBuffered_DrawCategory);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001758#else
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001759 return prepareToDraw(paint, kUnbuffered_DrawCategory);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001760#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001761}
1762
1763const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1764 return fGpu->getQuadIndexBuffer();
1765}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001766
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001767GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture,
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001768 bool canClobberSrc,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001769 const SkRect& rect,
1770 float sigmaX, float sigmaY) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001771 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001772 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001773 AutoMatrix avm(this, GrMatrix::I());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001774 SkIRect clearRect;
bsalomon@google.comb505a122012-05-31 18:40:36 +00001775 int scaleFactorX, radiusX;
1776 int scaleFactorY, radiusY;
1777 sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX);
1778 sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY);
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001779
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001780 SkRect srcRect(rect);
1781 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
1782 srcRect.roundOut();
robertphillips@google.com8637a362012-04-10 18:32:35 +00001783 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
1784 static_cast<float>(scaleFactorY));
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001785
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001786 AutoClip acs(this, srcRect);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001787
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001788 GrAssert(kBGRA_8888_PM_GrPixelConfig == srcTexture->config() ||
1789 kRGBA_8888_PM_GrPixelConfig == srcTexture->config() ||
1790 kAlpha_8_GrPixelConfig == srcTexture->config());
1791
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001792 GrTextureDesc desc;
1793 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1794 desc.fWidth = SkScalarFloorToInt(srcRect.width());
1795 desc.fHeight = SkScalarFloorToInt(srcRect.height());
1796 desc.fConfig = srcTexture->config();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001797
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001798 GrAutoScratchTexture temp1, temp2;
1799 GrTexture* dstTexture = temp1.set(this, desc);
1800 GrTexture* tempTexture = canClobberSrc ? srcTexture : temp2.set(this, desc);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001801
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001802 GrPaint paint;
1803 paint.reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +00001804 paint.textureSampler(0)->textureParams()->setBilerp(true);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001805
1806 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
1807 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1808 srcTexture->height());
1809 this->setRenderTarget(dstTexture->asRenderTarget());
1810 SkRect dstRect(srcRect);
1811 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
1812 i < scaleFactorY ? 0.5f : 1.0f);
tomhudson@google.comaa72eab2012-07-19 18:01:07 +00001813 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
1814 (srcTexture)))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001815 this->drawRectToRect(paint, dstRect, srcRect);
1816 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001817 srcTexture = dstTexture;
1818 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001819 }
1820
robertphillips@google.com7a396332012-05-10 15:11:27 +00001821 SkIRect srcIRect;
1822 srcRect.roundOut(&srcIRect);
1823
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001824 if (sigmaX > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001825 if (scaleFactorX > 1) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001826 // Clear out a radius to the right of the srcRect to prevent the
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001827 // X convolution from reading garbage.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001828 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001829 radiusX, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001830 this->clear(&clearRect, 0x0);
1831 }
1832
1833 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001834 convolve_gaussian(fGpu, srcTexture, srcRect, sigmaX, radiusX,
1835 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001836 srcTexture = dstTexture;
1837 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001838 }
1839
1840 if (sigmaY > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001841 if (scaleFactorY > 1 || sigmaX > 0.0f) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001842 // Clear out a radius below the srcRect to prevent the Y
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001843 // convolution from reading garbage.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001844 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001845 srcIRect.width(), radiusY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001846 this->clear(&clearRect, 0x0);
1847 }
1848
1849 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001850 convolve_gaussian(fGpu, srcTexture, srcRect, sigmaY, radiusY,
1851 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001852 srcTexture = dstTexture;
1853 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001854 }
1855
1856 if (scaleFactorX > 1 || scaleFactorY > 1) {
1857 // Clear one pixel to the right and below, to accommodate bilinear
1858 // upsampling.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001859 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
1860 srcIRect.width() + 1, 1);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001861 this->clear(&clearRect, 0x0);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001862 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
1863 1, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001864 this->clear(&clearRect, 0x0);
1865 // FIXME: This should be mitchell, not bilinear.
bsalomon@google.comb8670992012-07-25 21:27:09 +00001866 paint.textureSampler(0)->textureParams()->setBilerp(true);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001867 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1868 srcTexture->height());
1869 this->setRenderTarget(dstTexture->asRenderTarget());
tomhudson@google.comaa72eab2012-07-19 18:01:07 +00001870 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
1871 (srcTexture)))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001872 SkRect dstRect(srcRect);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001873 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001874 this->drawRectToRect(paint, dstRect, srcRect);
1875 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001876 srcTexture = dstTexture;
1877 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001878 }
1879 this->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001880 if (srcTexture == temp1.texture()) {
1881 return temp1.detach();
1882 } else if (srcTexture == temp2.texture()) {
1883 return temp2.detach();
1884 } else {
1885 srcTexture->ref();
1886 return srcTexture;
1887 }
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001888}
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001889
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001890GrTexture* GrContext::applyMorphology(GrTexture* srcTexture,
1891 const GrRect& rect,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001892 MorphologyType morphType,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001893 SkISize radius) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001894 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001895 srcTexture->ref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001896 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001897
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001898 AutoMatrix avm(this, GrMatrix::I());
1899
1900 AutoClip acs(this, GrRect::MakeWH(SkIntToScalar(srcTexture->width()),
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001901 SkIntToScalar(srcTexture->height())));
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001902 GrTextureDesc desc;
1903 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1904 desc.fWidth = SkScalarCeilToInt(rect.width());
1905 desc.fHeight = SkScalarCeilToInt(rect.height());
1906 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001907 if (radius.fWidth > 0) {
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001908 GrAutoScratchTexture ast(this, desc);
1909 this->setRenderTarget(ast.texture()->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001910 apply_morphology(fGpu, srcTexture, rect, radius.fWidth, morphType,
1911 Gr1DKernelEffect::kX_Direction);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001912 SkIRect clearRect = SkIRect::MakeXYWH(
1913 SkScalarFloorToInt(rect.fLeft),
1914 SkScalarFloorToInt(rect.fBottom),
1915 SkScalarFloorToInt(rect.width()),
1916 radius.fHeight);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001917 this->clear(&clearRect, 0x0);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001918 srcTexture->unref();
1919 srcTexture = ast.detach();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001920 }
1921 if (radius.fHeight > 0) {
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001922 GrAutoScratchTexture ast(this, desc);
1923 this->setRenderTarget(ast.texture()->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001924 apply_morphology(fGpu, srcTexture, rect, radius.fHeight, morphType,
1925 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001926 srcTexture->unref();
1927 srcTexture = ast.detach();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001928 }
1929 this->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001930 return srcTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001931}
bsalomon@google.comc4364992011-11-07 15:54:49 +00001932
1933///////////////////////////////////////////////////////////////////////////////