blob: e21383ebe41276b5d128213bd395ab4358ac28a6 [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 +0000170GrTexture* GrContext::TextureCacheEntry::texture() const {
171 if (NULL == fEntry) {
172 return NULL;
173 } else {
174 return (GrTexture*) fEntry->resource();
175 }
176}
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000177
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000178namespace {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000179
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000180void scale_rect(SkRect* rect, float xScale, float yScale) {
robertphillips@google.com5af56062012-04-27 15:39:52 +0000181 rect->fLeft = SkScalarMul(rect->fLeft, SkFloatToScalar(xScale));
182 rect->fTop = SkScalarMul(rect->fTop, SkFloatToScalar(yScale));
183 rect->fRight = SkScalarMul(rect->fRight, SkFloatToScalar(xScale));
184 rect->fBottom = SkScalarMul(rect->fBottom, SkFloatToScalar(yScale));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000185}
186
bsalomon@google.comb505a122012-05-31 18:40:36 +0000187float adjust_sigma(float sigma, int *scaleFactor, int *radius) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000188 *scaleFactor = 1;
189 while (sigma > MAX_BLUR_SIGMA) {
190 *scaleFactor *= 2;
191 sigma *= 0.5f;
192 }
bsalomon@google.comb505a122012-05-31 18:40:36 +0000193 *radius = static_cast<int>(ceilf(sigma * 3.0f));
194 GrAssert(*radius <= GrConvolutionEffect::kMaxKernelRadius);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000195 return sigma;
196}
197
198void apply_morphology(GrGpu* gpu,
199 GrTexture* texture,
200 const SkRect& rect,
201 int radius,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000202 GrContext::MorphologyType morphType,
203 Gr1DKernelEffect::Direction direction) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000204
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000205 GrRenderTarget* target = gpu->drawState()->getRenderTarget();
206 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000207 GrDrawState* drawState = gpu->drawState();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000208 drawState->setRenderTarget(target);
209 GrMatrix sampleM;
210 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000211 drawState->sampler(0)->reset(sampleM);
212 SkAutoTUnref<GrCustomStage> morph(
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000213 SkNEW_ARGS(GrMorphologyEffect, (texture, direction, radius, morphType)));
bsalomon@google.comb505a122012-05-31 18:40:36 +0000214 drawState->sampler(0)->setCustomStage(morph);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000215 gpu->drawSimpleRect(rect, NULL);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000216}
217
bsalomon@google.comb505a122012-05-31 18:40:36 +0000218void convolve_gaussian(GrGpu* gpu,
219 GrTexture* texture,
220 const SkRect& rect,
221 float sigma,
222 int radius,
223 Gr1DKernelEffect::Direction direction) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000224 GrRenderTarget* target = gpu->drawState()->getRenderTarget();
225 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000226 GrDrawState* drawState = gpu->drawState();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000227 drawState->setRenderTarget(target);
228 GrMatrix sampleM;
229 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000230 drawState->sampler(0)->reset(sampleM);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000231 SkAutoTUnref<GrConvolutionEffect> conv(SkNEW_ARGS(GrConvolutionEffect,
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000232 (texture, direction, radius,
233 sigma)));
bsalomon@google.comb505a122012-05-31 18:40:36 +0000234 drawState->sampler(0)->setCustomStage(conv);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000235 gpu->drawSimpleRect(rect, NULL);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000236}
237
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000238}
239
bsalomon@google.comb8670992012-07-25 21:27:09 +0000240GrContext::TextureCacheEntry GrContext::findAndLockTexture(const GrTextureDesc& desc,
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000241 const GrCacheData& cacheData,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000242 const GrTextureParams* params) {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000243 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000244 return TextureCacheEntry(fTextureCache->findAndLock(resourceKey,
245 GrResourceCache::kNested_LockType));
246}
247
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000248bool GrContext::isTextureInCache(const GrTextureDesc& desc,
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000249 const GrCacheData& cacheData,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000250 const GrTextureParams* params) const {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000251 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000252 return fTextureCache->hasKey(resourceKey);
253}
254
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000255GrResourceEntry* GrContext::addAndLockStencilBuffer(GrStencilBuffer* sb) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000256 ASSERT_OWNED_RESOURCE(sb);
robertphillips@google.com46a86002012-08-08 10:42:44 +0000257
258 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(sb->width(),
259 sb->height(),
260 sb->numSamples());
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000261 return fTextureCache->createAndLock(resourceKey, sb);
262}
263
264GrStencilBuffer* GrContext::findStencilBuffer(int width, int height,
265 int sampleCnt) {
robertphillips@google.com46a86002012-08-08 10:42:44 +0000266 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(width,
267 height,
268 sampleCnt);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000269 GrResourceEntry* entry = fTextureCache->findAndLock(resourceKey,
270 GrResourceCache::kSingle_LockType);
271 if (NULL != entry) {
272 GrStencilBuffer* sb = (GrStencilBuffer*) entry->resource();
273 return sb;
274 } else {
275 return NULL;
276 }
277}
278
279void GrContext::unlockStencilBuffer(GrResourceEntry* sbEntry) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000280 ASSERT_OWNED_RESOURCE(sbEntry->resource());
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000281 fTextureCache->unlock(sbEntry);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000282}
283
284static void stretchImage(void* dst,
285 int dstW,
286 int dstH,
287 void* src,
288 int srcW,
289 int srcH,
290 int bpp) {
291 GrFixed dx = (srcW << 16) / dstW;
292 GrFixed dy = (srcH << 16) / dstH;
293
294 GrFixed y = dy >> 1;
295
296 int dstXLimit = dstW*bpp;
297 for (int j = 0; j < dstH; ++j) {
298 GrFixed x = dx >> 1;
299 void* srcRow = (uint8_t*)src + (y>>16)*srcW*bpp;
300 void* dstRow = (uint8_t*)dst + j*dstW*bpp;
301 for (int i = 0; i < dstXLimit; i += bpp) {
302 memcpy((uint8_t*) dstRow + i,
303 (uint8_t*) srcRow + (x>>16)*bpp,
304 bpp);
305 x += dx;
306 }
307 y += dy;
308 }
309}
310
robertphillips@google.com3319f332012-08-13 18:00:36 +0000311// The desired texture is NPOT and tiled but that isn't supported by
312// the current hardware. Resize the texture to be a POT
313GrTexture* GrContext::createResizedTexture(const GrTextureDesc& desc,
314 const GrCacheData& cacheData,
315 void* srcData,
316 size_t rowBytes,
317 bool needsFiltering) {
318 TextureCacheEntry clampEntry = this->findAndLockTexture(desc, cacheData, NULL);
319
320 if (NULL == clampEntry.texture()) {
321 clampEntry = this->createAndLockTexture(NULL, desc, cacheData, srcData, rowBytes);
322 GrAssert(NULL != clampEntry.texture());
323 if (NULL == clampEntry.texture()) {
324 return NULL;
325 }
326 }
327 GrTextureDesc rtDesc = desc;
328 rtDesc.fFlags = rtDesc.fFlags |
329 kRenderTarget_GrTextureFlagBit |
330 kNoStencil_GrTextureFlagBit;
331 rtDesc.fWidth = GrNextPow2(GrMax(desc.fWidth, 64));
332 rtDesc.fHeight = GrNextPow2(GrMax(desc.fHeight, 64));
333
334 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
335
336 if (NULL != texture) {
337 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
338 GrDrawState* drawState = fGpu->drawState();
339 drawState->setRenderTarget(texture->asRenderTarget());
340
341 // if filtering is not desired then we want to ensure all
342 // texels in the resampled image are copies of texels from
343 // the original.
344 drawState->sampler(0)->reset(SkShader::kClamp_TileMode,
345 needsFiltering);
346 drawState->createTextureEffect(0, clampEntry.texture());
347
348 static const GrVertexLayout layout =
349 GrDrawTarget::StageTexCoordVertexLayoutBit(0,0);
350 GrDrawTarget::AutoReleaseGeometry arg(fGpu, layout, 4, 0);
351
352 if (arg.succeeded()) {
353 GrPoint* verts = (GrPoint*) arg.vertices();
354 verts[0].setIRectFan(0, 0,
355 texture->width(),
356 texture->height(),
357 2*sizeof(GrPoint));
358 verts[1].setIRectFan(0, 0, 1, 1, 2*sizeof(GrPoint));
359 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType,
360 0, 4);
361 }
362 texture->releaseRenderTarget();
363 } else {
364 // TODO: Our CPU stretch doesn't filter. But we create separate
365 // stretched textures when the sampler state is either filtered or
366 // not. Either implement filtered stretch blit on CPU or just create
367 // one when FBO case fails.
368
369 rtDesc.fFlags = kNone_GrTextureFlags;
370 // no longer need to clamp at min RT size.
371 rtDesc.fWidth = GrNextPow2(desc.fWidth);
372 rtDesc.fHeight = GrNextPow2(desc.fHeight);
373 int bpp = GrBytesPerPixel(desc.fConfig);
374 SkAutoSMalloc<128*128*4> stretchedPixels(bpp *
375 rtDesc.fWidth *
376 rtDesc.fHeight);
377 stretchImage(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
378 srcData, desc.fWidth, desc.fHeight, bpp);
379
380 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
381
382 GrTexture* texture = fGpu->createTexture(rtDesc,
383 stretchedPixels.get(),
384 stretchedRowBytes);
385 GrAssert(NULL != texture);
386 }
387 fTextureCache->unlock(clampEntry.cacheEntry());
388
389 return texture;
390}
391
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000392GrContext::TextureCacheEntry GrContext::createAndLockTexture(
bsalomon@google.comb8670992012-07-25 21:27:09 +0000393 const GrTextureParams* params,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000394 const GrTextureDesc& desc,
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000395 const GrCacheData& cacheData,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000396 void* srcData,
397 size_t rowBytes) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000398 SK_TRACE_EVENT0("GrContext::createAndLockTexture");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000399
400#if GR_DUMP_TEXTURE_UPLOAD
401 GrPrintf("GrContext::createAndLockTexture [%d %d]\n", desc.fWidth, desc.fHeight);
402#endif
403
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000404 TextureCacheEntry entry;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000405
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000406 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000407
robertphillips@google.com3319f332012-08-13 18:00:36 +0000408 GrTexture* texture = NULL;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000409 if (GrTexture::NeedsResizing(resourceKey)) {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000410 texture = this->createResizedTexture(desc, cacheData,
411 srcData, rowBytes,
412 GrTexture::NeedsFiltering(resourceKey));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000413 } else {
robertphillips@google.com3319f332012-08-13 18:00:36 +0000414 texture = fGpu->createTexture(desc, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000415 }
robertphillips@google.com3319f332012-08-13 18:00:36 +0000416
417 if (NULL != texture) {
418 entry.set(fTextureCache->createAndLock(resourceKey, texture));
419 }
420
bsalomon@google.com27847de2011-02-22 20:59:41 +0000421 return entry;
422}
423
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000424GrContext::TextureCacheEntry GrContext::lockScratchTexture(
425 const GrTextureDesc& inDesc,
426 ScratchTexMatch match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000427 GrTextureDesc desc = inDesc;
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000428 GrCacheData cacheData(GrCacheData::kScratch_CacheID);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000429
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000430 if (kExact_ScratchTexMatch != match) {
431 // bin by pow2 with a reasonable min
432 static const int MIN_SIZE = 256;
433 desc.fWidth = GrMax(MIN_SIZE, GrNextPow2(desc.fWidth));
434 desc.fHeight = GrMax(MIN_SIZE, GrNextPow2(desc.fHeight));
435 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000436
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000437 GrResourceEntry* entry;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000438 int origWidth = desc.fWidth;
439 int origHeight = desc.fHeight;
440 bool doubledW = false;
441 bool doubledH = false;
442
443 do {
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000444 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL, desc, cacheData, true);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000445 entry = fTextureCache->findAndLock(key,
446 GrResourceCache::kNested_LockType);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000447 // if we miss, relax the fit of the flags...
448 // then try doubling width... then height.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000449 if (NULL != entry || kExact_ScratchTexMatch == match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000450 break;
451 }
452 if (!(desc.fFlags & kRenderTarget_GrTextureFlagBit)) {
453 desc.fFlags = desc.fFlags | kRenderTarget_GrTextureFlagBit;
454 } else if (desc.fFlags & kNoStencil_GrTextureFlagBit) {
455 desc.fFlags = desc.fFlags & ~kNoStencil_GrTextureFlagBit;
456 } else if (!doubledW) {
457 desc.fFlags = inDesc.fFlags;
458 desc.fWidth *= 2;
459 doubledW = true;
460 } else if (!doubledH) {
461 desc.fFlags = inDesc.fFlags;
462 desc.fWidth = origWidth;
463 desc.fHeight *= 2;
464 doubledH = true;
465 } else {
466 break;
467 }
468
469 } while (true);
470
471 if (NULL == entry) {
472 desc.fFlags = inDesc.fFlags;
473 desc.fWidth = origWidth;
474 desc.fHeight = origHeight;
475 GrTexture* texture = fGpu->createTexture(desc, NULL, 0);
476 if (NULL != texture) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000477 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000478 texture->desc(),
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000479 cacheData,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000480 true);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000481 entry = fTextureCache->createAndLock(key, texture);
482 }
483 }
484
485 // If the caller gives us the same desc/sampler twice we don't want
486 // to return the same texture the second time (unless it was previously
487 // released). So we detach the entry from the cache and reattach at release.
488 if (NULL != entry) {
489 fTextureCache->detach(entry);
490 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000491 return TextureCacheEntry(entry);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000492}
493
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000494void GrContext::addExistingTextureToCache(GrTexture* texture) {
495
496 if (NULL == texture) {
497 return;
498 }
499
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000500 // 'texture' is a scratch texture returning to the fold
501 GrCacheData cacheData(GrCacheData::kScratch_CacheID);
502
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000503 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
504 texture->desc(),
robertphillips@google.com9c2ea842012-08-13 17:47:59 +0000505 cacheData,
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000506 true);
507 fTextureCache->attach(key, texture);
508}
509
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000510void GrContext::unlockTexture(TextureCacheEntry entry) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000511 ASSERT_OWNED_RESOURCE(entry.texture());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000512 // If this is a scratch texture we detached it from the cache
513 // while it was locked (to avoid two callers simultaneously getting
514 // the same texture).
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000515 if (GrTexture::IsScratchTexture(entry.cacheEntry()->key())) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000516 fTextureCache->reattachAndUnlock(entry.cacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000517 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000518 fTextureCache->unlock(entry.cacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000519 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000520}
521
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000522void GrContext::freeEntry(TextureCacheEntry entry) {
523 ASSERT_OWNED_RESOURCE(entry.texture());
524
525 fTextureCache->freeEntry(entry.cacheEntry());
526}
527
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000528GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000529 void* srcData,
530 size_t rowBytes) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000531 GrTextureDesc descCopy = descIn;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000532 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000533}
534
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000535void GrContext::getTextureCacheLimits(int* maxTextures,
536 size_t* maxTextureBytes) const {
537 fTextureCache->getLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000538}
539
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000540void GrContext::setTextureCacheLimits(int maxTextures, size_t maxTextureBytes) {
541 fTextureCache->setLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000542}
543
bsalomon@google.com91958362011-06-13 17:58:13 +0000544int GrContext::getMaxTextureSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000545 return fGpu->getCaps().fMaxTextureSize;
bsalomon@google.com91958362011-06-13 17:58:13 +0000546}
547
548int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000549 return fGpu->getCaps().fMaxRenderTargetSize;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000550}
551
552///////////////////////////////////////////////////////////////////////////////
553
bsalomon@google.come269f212011-11-07 13:29:52 +0000554GrTexture* GrContext::createPlatformTexture(const GrPlatformTextureDesc& desc) {
555 return fGpu->createPlatformTexture(desc);
556}
557
558GrRenderTarget* GrContext::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
559 return fGpu->createPlatformRenderTarget(desc);
560}
561
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000562///////////////////////////////////////////////////////////////////////////////
563
bsalomon@google.comb8670992012-07-25 21:27:09 +0000564bool GrContext::supportsIndex8PixelConfig(const GrTextureParams* params,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000565 int width, int height) const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000566 const GrDrawTarget::Caps& caps = fGpu->getCaps();
567 if (!caps.f8BitPaletteSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000568 return false;
569 }
570
bsalomon@google.com27847de2011-02-22 20:59:41 +0000571 bool isPow2 = GrIsPow2(width) && GrIsPow2(height);
572
573 if (!isPow2) {
bsalomon@google.comb8670992012-07-25 21:27:09 +0000574 bool tiled = NULL != params && params->isTiled();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000575 if (tiled && !caps.fNPOTTextureTileSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000576 return false;
577 }
578 }
579 return true;
580}
581
582////////////////////////////////////////////////////////////////////////////////
583
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000584const GrClipData* GrContext::getClip() const {
585 return fGpu->getClip();
586}
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000587
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000588void GrContext::setClip(const GrClipData* clipData) {
589 fGpu->setClip(clipData);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000590 fDrawState->enableState(GrDrawState::kClip_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000591}
592
bsalomon@google.com27847de2011-02-22 20:59:41 +0000593////////////////////////////////////////////////////////////////////////////////
594
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000595void GrContext::clear(const GrIRect* rect,
596 const GrColor color,
597 GrRenderTarget* target) {
bsalomon@google.com398109c2011-04-14 18:40:27 +0000598 this->flush();
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000599 fGpu->clear(rect, color, target);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000600}
601
602void GrContext::drawPaint(const GrPaint& paint) {
603 // set rect to be big enough to fill the space, but not super-huge, so we
604 // don't overflow fixed-point implementations
bsalomon@google.comd302f142011-03-03 13:54:13 +0000605 GrRect r;
606 r.setLTRB(0, 0,
607 GrIntToScalar(getRenderTarget()->width()),
608 GrIntToScalar(getRenderTarget()->height()));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000609 GrMatrix inverse;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000610 SkTLazy<GrPaint> tmpPaint;
611 const GrPaint* p = &paint;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000612 AutoMatrix am;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000613
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000614 // We attempt to map r by the inverse matrix and draw that. mapRect will
615 // map the four corners and bound them with a new rect. This will not
616 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000617 if (!this->getMatrix().hasPerspective()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000618 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000619 GrPrintf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000620 return;
621 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000622 inverse.mapRect(&r);
623 } else {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000624 if (paint.hasTextureOrMask()) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000625 tmpPaint.set(paint);
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000626 p = tmpPaint.get();
bsalomon@google.come3d32162012-07-20 13:37:06 +0000627 if (!tmpPaint.get()->preConcatSamplerMatricesWithInverse(fDrawState->getViewMatrix())) {
628 GrPrintf("Could not invert matrix\n");
629 }
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000630 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000631 am.set(this, GrMatrix::I());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000632 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000633 // by definition this fills the entire clip, no need for AA
634 if (paint.fAntiAlias) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000635 if (!tmpPaint.isValid()) {
636 tmpPaint.set(paint);
637 p = tmpPaint.get();
638 }
639 GrAssert(p == tmpPaint.get());
640 tmpPaint.get()->fAntiAlias = false;
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000641 }
642 this->drawRect(*p, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000643}
644
bsalomon@google.com205d4602011-04-25 12:43:45 +0000645////////////////////////////////////////////////////////////////////////////////
646
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000647namespace {
648inline bool disable_coverage_aa_for_blend(GrDrawTarget* target) {
649 return DISABLE_COVERAGE_AA_FOR_BLEND && !target->canApplyCoverage();
650}
651}
652
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000653////////////////////////////////////////////////////////////////////////////////
654
bsalomon@google.com27847de2011-02-22 20:59:41 +0000655/* create a triangle strip that strokes the specified triangle. There are 8
656 unique vertices, but we repreat the last 2 to close up. Alternatively we
657 could use an indices array, and then only send 8 verts, but not sure that
658 would be faster.
659 */
bsalomon@google.com205d4602011-04-25 12:43:45 +0000660static void setStrokeRectStrip(GrPoint verts[10], GrRect rect,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000661 GrScalar width) {
662 const GrScalar rad = GrScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000663 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000664
665 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
666 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
667 verts[2].set(rect.fRight - rad, rect.fTop + rad);
668 verts[3].set(rect.fRight + rad, rect.fTop - rad);
669 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
670 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
671 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
672 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
673 verts[8] = verts[0];
674 verts[9] = verts[1];
675}
676
reed@google.com20efde72011-05-09 17:00:02 +0000677/**
678 * Returns true if the rects edges are integer-aligned.
679 */
680static bool isIRect(const GrRect& r) {
681 return GrScalarIsInt(r.fLeft) && GrScalarIsInt(r.fTop) &&
682 GrScalarIsInt(r.fRight) && GrScalarIsInt(r.fBottom);
683}
684
bsalomon@google.com205d4602011-04-25 12:43:45 +0000685static bool apply_aa_to_rect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000686 const GrRect& rect,
687 GrScalar width,
688 const GrMatrix* matrix,
689 GrMatrix* combinedMatrix,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000690 GrRect* devRect,
691 bool* useVertexCoverage) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000692 // we use a simple coverage ramp to do aa on axis-aligned rects
693 // we check if the rect will be axis-aligned, and the rect won't land on
694 // integer coords.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000695
bsalomon@google.coma3108262011-10-10 14:08:47 +0000696 // we are keeping around the "tweak the alpha" trick because
697 // it is our only hope for the fixed-pipe implementation.
698 // In a shader implementation we can give a separate coverage input
bsalomon@google.com289533a2011-10-27 12:34:25 +0000699 // TODO: remove this ugliness when we drop the fixed-pipe impl
bsalomon@google.coma3108262011-10-10 14:08:47 +0000700 *useVertexCoverage = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000701 if (!target->canTweakAlphaForCoverage()) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000702 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +0000703#if GR_DEBUG
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000704 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000705#endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000706 return false;
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000707 } else {
708 *useVertexCoverage = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000709 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000710 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000711 const GrDrawState& drawState = target->getDrawState();
712 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000713 return false;
714 }
715
bsalomon@google.com471d4712011-08-23 15:45:25 +0000716 if (0 == width && target->willUseHWAALines()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000717 return false;
718 }
719
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000720 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000721 return false;
722 }
723
724 if (NULL != matrix &&
725 !matrix->preservesAxisAlignment()) {
726 return false;
727 }
728
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000729 *combinedMatrix = drawState.getViewMatrix();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000730 if (NULL != matrix) {
731 combinedMatrix->preConcat(*matrix);
732 GrAssert(combinedMatrix->preservesAxisAlignment());
733 }
734
735 combinedMatrix->mapRect(devRect, rect);
736 devRect->sort();
737
738 if (width < 0) {
reed@google.com20efde72011-05-09 17:00:02 +0000739 return !isIRect(*devRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000740 } else {
741 return true;
742 }
743}
744
bsalomon@google.com27847de2011-02-22 20:59:41 +0000745void GrContext::drawRect(const GrPaint& paint,
746 const GrRect& rect,
747 GrScalar width,
748 const GrMatrix* matrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000749 SK_TRACE_EVENT0("GrContext::drawRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000750
751 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000752 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000753
bsalomon@google.com205d4602011-04-25 12:43:45 +0000754 GrRect devRect = rect;
755 GrMatrix combinedMatrix;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000756 bool useVertexCoverage;
bsalomon@google.com289533a2011-10-27 12:34:25 +0000757 bool needAA = paint.fAntiAlias &&
758 !this->getRenderTarget()->isMultisampled();
759 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix,
760 &combinedMatrix, &devRect,
761 &useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000762
763 if (doAA) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000764 GrDrawTarget::AutoDeviceCoordDraw adcd(target);
765 if (!adcd.succeeded()) {
766 return;
767 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000768 if (width >= 0) {
769 GrVec strokeSize;;
770 if (width > 0) {
771 strokeSize.set(width, width);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000772 combinedMatrix.mapVectors(&strokeSize, 1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000773 strokeSize.setAbs(strokeSize);
774 } else {
775 strokeSize.set(GR_Scalar1, GR_Scalar1);
776 }
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000777 fAARectRenderer->strokeAARect(this->getGpu(), target, devRect,
778 strokeSize, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000779 } else {
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000780 fAARectRenderer->fillAARect(this->getGpu(), target,
781 devRect, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000782 }
783 return;
784 }
785
bsalomon@google.com27847de2011-02-22 20:59:41 +0000786 if (width >= 0) {
787 // TODO: consider making static vertex buffers for these cases.
788 // Hairline could be done by just adding closing vertex to
789 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000790
bsalomon@google.com27847de2011-02-22 20:59:41 +0000791 static const int worstCaseVertCount = 10;
bsalomon@google.come3d32162012-07-20 13:37:06 +0000792 GrDrawTarget::AutoReleaseGeometry geo(target, 0, worstCaseVertCount, 0);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000793
794 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000795 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000796 return;
797 }
798
799 GrPrimitiveType primType;
800 int vertCount;
801 GrPoint* vertex = geo.positions();
802
803 if (width > 0) {
804 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000805 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000806 setStrokeRectStrip(vertex, rect, width);
807 } else {
808 // hairline
809 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000810 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000811 vertex[0].set(rect.fLeft, rect.fTop);
812 vertex[1].set(rect.fRight, rect.fTop);
813 vertex[2].set(rect.fRight, rect.fBottom);
814 vertex[3].set(rect.fLeft, rect.fBottom);
815 vertex[4].set(rect.fLeft, rect.fTop);
816 }
817
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000818 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000819 if (NULL != matrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000820 GrDrawState* drawState = target->drawState();
821 avmr.set(drawState);
822 drawState->preConcatViewMatrix(*matrix);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000823 drawState->preConcatSamplerMatrices(*matrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000824 }
825
826 target->drawNonIndexed(primType, 0, vertCount);
827 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000828#if GR_STATIC_RECT_VB
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000829 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
830 if (NULL == sqVB) {
831 GrPrintf("Failed to create static rect vb.\n");
832 return;
833 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000834 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000835 GrDrawState* drawState = target->drawState();
836 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000837 GrMatrix m;
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000838 m.setAll(rect.width(), 0, rect.fLeft,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000839 0, rect.height(), rect.fTop,
840 0, 0, GrMatrix::I()[8]);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000841
842 if (NULL != matrix) {
843 m.postConcat(*matrix);
844 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000845 drawState->preConcatViewMatrix(m);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000846 drawState->preConcatSamplerMatrices(m);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000847
bsalomon@google.com47059542012-06-06 20:51:20 +0000848 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000849#else
bsalomon@google.come3d32162012-07-20 13:37:06 +0000850 target->drawSimpleRect(rect, matrix);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000851#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000852 }
853}
854
855void GrContext::drawRectToRect(const GrPaint& paint,
856 const GrRect& dstRect,
857 const GrRect& srcRect,
858 const GrMatrix* dstMatrix,
859 const GrMatrix* srcMatrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000860 SK_TRACE_EVENT0("GrContext::drawRectToRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000861
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000862 // srcRect refers to paint's first texture
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000863 if (!paint.isTextureStageEnabled(0)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000864 drawRect(paint, dstRect, -1, dstMatrix);
865 return;
866 }
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000867
bsalomon@google.com27847de2011-02-22 20:59:41 +0000868 GR_STATIC_ASSERT(!BATCH_RECT_TO_RECT || !GR_STATIC_RECT_VB);
869
870#if GR_STATIC_RECT_VB
871 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000872 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000873 GrDrawState* drawState = target->drawState();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000874 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000875
876 GrMatrix m;
877
878 m.setAll(dstRect.width(), 0, dstRect.fLeft,
879 0, dstRect.height(), dstRect.fTop,
880 0, 0, GrMatrix::I()[8]);
881 if (NULL != dstMatrix) {
882 m.postConcat(*dstMatrix);
883 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000884 drawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000885
bsalomon@google.come3d32162012-07-20 13:37:06 +0000886 // we explicitly setup the correct coords for the first stage. The others
887 // must know about the view matrix change.
888 for (int s = 1; s < GrPaint::kTotalStages; ++s) {
889 if (drawState->isStageEnabled(s)) {
890 drawState->sampler(s)->preConcatMatrix(m);
891 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000892 }
893
bsalomon@google.com27847de2011-02-22 20:59:41 +0000894 m.setAll(srcRect.width(), 0, srcRect.fLeft,
895 0, srcRect.height(), srcRect.fTop,
896 0, 0, GrMatrix::I()[8]);
897 if (NULL != srcMatrix) {
898 m.postConcat(*srcMatrix);
899 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000900 drawState->sampler(GrPaint::kFirstTextureStage)->preConcatMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000901
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000902 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
903 if (NULL == sqVB) {
904 GrPrintf("Failed to create static rect vb.\n");
905 return;
906 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000907 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com47059542012-06-06 20:51:20 +0000908 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000909#else
910
911 GrDrawTarget* target;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000912#if BATCH_RECT_TO_RECT
bsalomon@google.com27847de2011-02-22 20:59:41 +0000913 target = this->prepareToDraw(paint, kBuffered_DrawCategory);
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000914#else
bsalomon@google.com27847de2011-02-22 20:59:41 +0000915 target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
916#endif
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000917 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000918
tomhudson@google.com93813632011-10-27 20:21:16 +0000919 const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
920 const GrMatrix* srcMatrices[GrDrawState::kNumStages] = {NULL};
bsalomon@google.com27847de2011-02-22 20:59:41 +0000921 srcRects[0] = &srcRect;
922 srcMatrices[0] = srcMatrix;
923
bsalomon@google.come3d32162012-07-20 13:37:06 +0000924 target->drawRect(dstRect, dstMatrix, srcRects, srcMatrices);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000925#endif
926}
927
928void GrContext::drawVertices(const GrPaint& paint,
929 GrPrimitiveType primitiveType,
930 int vertexCount,
931 const GrPoint positions[],
932 const GrPoint texCoords[],
933 const GrColor colors[],
934 const uint16_t indices[],
935 int indexCount) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000936 SK_TRACE_EVENT0("GrContext::drawVertices");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000937
938 GrDrawTarget::AutoReleaseGeometry geo;
939
940 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000941 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000942
bsalomon@google.come3d32162012-07-20 13:37:06 +0000943 GrVertexLayout layout = 0;
944 if (NULL != texCoords) {
945 layout |= GrDrawTarget::StageTexCoordVertexLayoutBit(0, 0);
946 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000947 if (NULL != colors) {
948 layout |= GrDrawTarget::kColor_VertexLayoutBit;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000949 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000950 int vertexSize = GrDrawTarget::VertexSize(layout);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000951
952 if (sizeof(GrPoint) != vertexSize) {
953 if (!geo.set(target, layout, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000954 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000955 return;
956 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000957 int texOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000958 int colorOffset;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000959 GrDrawTarget::VertexSizeAndOffsetsByIdx(layout,
960 texOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000961 &colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000962 NULL,
963 NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000964 void* curVertex = geo.vertices();
965
966 for (int i = 0; i < vertexCount; ++i) {
967 *((GrPoint*)curVertex) = positions[i];
968
969 if (texOffsets[0] > 0) {
970 *(GrPoint*)((intptr_t)curVertex + texOffsets[0]) = texCoords[i];
971 }
972 if (colorOffset > 0) {
973 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
974 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000975 curVertex = (void*)((intptr_t)curVertex + vertexSize);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000976 }
977 } else {
978 target->setVertexSourceToArray(layout, positions, vertexCount);
979 }
980
bsalomon@google.com91958362011-06-13 17:58:13 +0000981 // we don't currently apply offscreen AA to this path. Need improved
982 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000983
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000984 if (NULL != indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000985 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000986 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000987 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000988 target->drawNonIndexed(primitiveType, 0, vertexCount);
989 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000990}
991
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000992///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com150d2842012-01-12 20:19:56 +0000993namespace {
994
bsalomon@google.com93c96602012-04-27 13:05:21 +0000995struct CircleVertex {
996 GrPoint fPos;
997 GrPoint fCenter;
998 GrScalar fOuterRadius;
999 GrScalar fInnerRadius;
1000};
1001
1002/* Returns true if will map a circle to another circle. This can be true
1003 * if the matrix only includes square-scale, rotation, translation.
1004 */
1005inline bool isSimilarityTransformation(const SkMatrix& matrix,
1006 SkScalar tol = SK_ScalarNearlyZero) {
1007 if (matrix.isIdentity() || matrix.getType() == SkMatrix::kTranslate_Mask) {
1008 return true;
1009 }
1010 if (matrix.hasPerspective()) {
1011 return false;
1012 }
1013
1014 SkScalar mx = matrix.get(SkMatrix::kMScaleX);
1015 SkScalar sx = matrix.get(SkMatrix::kMSkewX);
1016 SkScalar my = matrix.get(SkMatrix::kMScaleY);
1017 SkScalar sy = matrix.get(SkMatrix::kMSkewY);
1018
1019 if (mx == 0 && sx == 0 && my == 0 && sy == 0) {
1020 return false;
1021 }
1022
1023 // it has scales or skews, but it could also be rotation, check it out.
1024 SkVector vec[2];
1025 vec[0].set(mx, sx);
1026 vec[1].set(sy, my);
1027
1028 return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) &&
1029 SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(),
1030 SkScalarSquare(tol));
1031}
1032
1033}
1034
1035// TODO: strokeWidth can't be larger than zero right now.
1036// It will be fixed when drawPath() can handle strokes.
1037void GrContext::drawOval(const GrPaint& paint,
1038 const GrRect& rect,
1039 SkScalar strokeWidth) {
bsalomon@google.com0982d352012-07-31 15:33:25 +00001040 GrAssert(strokeWidth <= 0);
1041 if (!isSimilarityTransformation(this->getMatrix()) ||
bsalomon@google.com93c96602012-04-27 13:05:21 +00001042 !paint.fAntiAlias ||
1043 rect.height() != rect.width()) {
1044 SkPath path;
1045 path.addOval(rect);
1046 GrPathFill fill = (strokeWidth == 0) ?
bsalomon@google.com0982d352012-07-31 15:33:25 +00001047 kHairLine_GrPathFill : kWinding_GrPathFill;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001048 this->internalDrawPath(paint, path, fill, NULL);
1049 return;
1050 }
1051
bsalomon@google.com0982d352012-07-31 15:33:25 +00001052 DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
1053 kUnbuffered_DrawCategory;
1054 GrDrawTarget* target = this->prepareToDraw(paint, category);
1055 GrDrawState* drawState = target->drawState();
1056 GrDrawState::AutoStageDisable atr(fDrawState);
1057 const GrMatrix vm = drawState->getViewMatrix();
1058
bsalomon@google.com93c96602012-04-27 13:05:21 +00001059 const GrRenderTarget* rt = drawState->getRenderTarget();
1060 if (NULL == rt) {
1061 return;
1062 }
1063
bsalomon@google.come3d32162012-07-20 13:37:06 +00001064 GrDrawTarget::AutoDeviceCoordDraw adcd(target);
1065 if (!adcd.succeeded()) {
1066 return;
1067 }
bsalomon@google.com93c96602012-04-27 13:05:21 +00001068
bsalomon@google.come3d32162012-07-20 13:37:06 +00001069 GrVertexLayout layout = GrDrawTarget::kEdge_VertexLayoutBit;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001070 GrAssert(sizeof(CircleVertex) == GrDrawTarget::VertexSize(layout));
1071
1072 GrPoint center = GrPoint::Make(rect.centerX(), rect.centerY());
1073 GrScalar radius = SkScalarHalf(rect.width());
1074
1075 vm.mapPoints(&center, 1);
1076 radius = vm.mapRadius(radius);
1077
1078 GrScalar outerRadius = radius;
1079 GrScalar innerRadius = 0;
1080 SkScalar halfWidth = 0;
1081 if (strokeWidth == 0) {
1082 halfWidth = SkScalarHalf(SK_Scalar1);
1083
1084 outerRadius += halfWidth;
1085 innerRadius = SkMaxScalar(0, radius - halfWidth);
1086 }
1087
1088 GrDrawTarget::AutoReleaseGeometry geo(target, layout, 4, 0);
1089 if (!geo.succeeded()) {
1090 GrPrintf("Failed to get space for vertices!\n");
1091 return;
1092 }
1093
1094 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
1095
robertphillips@google.coma0a66c12012-06-22 13:14:29 +00001096 // The fragment shader will extend the radius out half a pixel
1097 // to antialias. Expand the drawn rect here so all the pixels
1098 // will be captured.
1099 SkScalar L = center.fX - outerRadius - SkFloatToScalar(0.5f);
1100 SkScalar R = center.fX + outerRadius + SkFloatToScalar(0.5f);
1101 SkScalar T = center.fY - outerRadius - SkFloatToScalar(0.5f);
1102 SkScalar B = center.fY + outerRadius + SkFloatToScalar(0.5f);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001103
1104 verts[0].fPos = SkPoint::Make(L, T);
1105 verts[1].fPos = SkPoint::Make(R, T);
1106 verts[2].fPos = SkPoint::Make(L, B);
1107 verts[3].fPos = SkPoint::Make(R, B);
1108
1109 for (int i = 0; i < 4; ++i) {
1110 // this goes to fragment shader, it should be in y-points-up space.
1111 verts[i].fCenter = SkPoint::Make(center.fX, rt->height() - center.fY);
1112
1113 verts[i].fOuterRadius = outerRadius;
1114 verts[i].fInnerRadius = innerRadius;
1115 }
1116
1117 drawState->setVertexEdgeType(GrDrawState::kCircle_EdgeType);
bsalomon@google.com47059542012-06-06 20:51:20 +00001118 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001119}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001120
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001121void GrContext::drawPath(const GrPaint& paint, const SkPath& path,
reed@google.com07f3ee12011-05-16 17:21:57 +00001122 GrPathFill fill, const GrPoint* translate) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001123
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001124 if (path.isEmpty()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001125 if (GrIsFillInverted(fill)) {
1126 this->drawPaint(paint);
1127 }
1128 return;
1129 }
1130
bsalomon@google.com93c96602012-04-27 13:05:21 +00001131 SkRect ovalRect;
1132 if (!GrIsFillInverted(fill) && path.isOval(&ovalRect)) {
1133 if (translate) {
1134 ovalRect.offset(*translate);
1135 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001136 SkScalar width = (fill == kHairLine_GrPathFill) ? 0 : -SK_Scalar1;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001137 this->drawOval(paint, ovalRect, width);
1138 return;
1139 }
1140
1141 internalDrawPath(paint, path, fill, translate);
1142}
1143
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001144void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +00001145 GrPathFill fill, const GrPoint* translate) {
1146
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001147 // Note that below we may sw-rasterize the path into a scratch texture.
1148 // Scratch textures can be recycled after they are returned to the texture
1149 // cache. This presents a potential hazard for buffered drawing. However,
1150 // the writePixels that uploads to the scratch will perform a flush so we're
1151 // OK.
1152 DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
1153 kUnbuffered_DrawCategory;
1154 GrDrawTarget* target = this->prepareToDraw(paint, category);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +00001155 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001156
bsalomon@google.com289533a2011-10-27 12:34:25 +00001157 bool prAA = paint.fAntiAlias && !this->getRenderTarget()->isMultisampled();
1158
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001159 // An Assumption here is that path renderer would use some form of tweaking
1160 // the src color (either the input alpha or in the frag shader) to implement
1161 // aa. If we have some future driver-mojo path AA that can do the right
1162 // thing WRT to the blend then we'll need some query on the PR.
1163 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001164#if GR_DEBUG
bsalomon@google.com979432b2011-11-05 21:38:22 +00001165 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001166#endif
bsalomon@google.com289533a2011-10-27 12:34:25 +00001167 prAA = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001168 }
bsalomon@google.com289533a2011-10-27 12:34:25 +00001169
robertphillips@google.com72176b22012-05-23 13:19:12 +00001170 GrPathRenderer* pr = this->getPathRenderer(path, fill, target, prAA, true);
bsalomon@google.com30085192011-08-19 15:42:31 +00001171 if (NULL == pr) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001172#if GR_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001173 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001174#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001175 return;
1176 }
1177
bsalomon@google.come3d32162012-07-20 13:37:06 +00001178 pr->drawPath(path, fill, translate, target, prAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001179}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001180
bsalomon@google.com27847de2011-02-22 20:59:41 +00001181////////////////////////////////////////////////////////////////////////////////
1182
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001183void GrContext::flush(int flagsBitfield) {
1184 if (kDiscard_FlushBit & flagsBitfield) {
1185 fDrawBuffer->reset();
1186 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001187 this->flushDrawBuffer();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001188 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001189 if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001190 fGpu->forceRenderTargetFlush();
1191 }
1192}
1193
bsalomon@google.com27847de2011-02-22 20:59:41 +00001194void GrContext::flushDrawBuffer() {
junov@google.com53a55842011-06-08 22:55:10 +00001195 if (fDrawBuffer) {
robertphillips@google.com58b38182012-05-03 16:29:41 +00001196 // With addition of the AA clip path, flushing the draw buffer can
1197 // result in the generation of an AA clip mask. During this
1198 // process the SW path renderer may be invoked which recusively
1199 // calls this method (via internalWriteTexturePixels) creating
1200 // infinite recursion
1201 GrInOrderDrawBuffer* temp = fDrawBuffer;
1202 fDrawBuffer = NULL;
1203
1204 temp->flushTo(fGpu);
1205
1206 fDrawBuffer = temp;
junov@google.com53a55842011-06-08 22:55:10 +00001207 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001208}
1209
bsalomon@google.com6f379512011-11-16 20:36:03 +00001210void GrContext::internalWriteTexturePixels(GrTexture* texture,
1211 int left, int top,
1212 int width, int height,
1213 GrPixelConfig config,
1214 const void* buffer,
1215 size_t rowBytes,
1216 uint32_t flags) {
1217 SK_TRACE_EVENT0("GrContext::writeTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001218 ASSERT_OWNED_RESOURCE(texture);
1219
bsalomon@google.com6f379512011-11-16 20:36:03 +00001220 if (!(kDontFlush_PixelOpsFlag & flags)) {
1221 this->flush();
1222 }
1223 // TODO: use scratch texture to perform conversion
1224 if (GrPixelConfigIsUnpremultiplied(texture->config()) !=
1225 GrPixelConfigIsUnpremultiplied(config)) {
1226 return;
1227 }
1228
1229 fGpu->writeTexturePixels(texture, left, top, width, height,
1230 config, buffer, rowBytes);
1231}
1232
1233bool GrContext::internalReadTexturePixels(GrTexture* texture,
1234 int left, int top,
1235 int width, int height,
1236 GrPixelConfig config,
1237 void* buffer,
1238 size_t rowBytes,
1239 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001240 SK_TRACE_EVENT0("GrContext::readTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001241 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001242
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001243 // TODO: code read pixels for textures that aren't also rendertargets
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001244 GrRenderTarget* target = texture->asRenderTarget();
1245 if (NULL != target) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001246 return this->internalReadRenderTargetPixels(target,
1247 left, top, width, height,
1248 config, buffer, rowBytes,
1249 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001250 } else {
1251 return false;
1252 }
1253}
1254
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001255#include "SkConfig8888.h"
1256
1257namespace {
1258/**
1259 * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel
1260 * formats are representable as Config8888 and so the function returns false
1261 * if the GrPixelConfig has no equivalent Config8888.
1262 */
1263bool grconfig_to_config8888(GrPixelConfig config,
1264 SkCanvas::Config8888* config8888) {
1265 switch (config) {
1266 case kRGBA_8888_PM_GrPixelConfig:
1267 *config8888 = SkCanvas::kRGBA_Premul_Config8888;
1268 return true;
1269 case kRGBA_8888_UPM_GrPixelConfig:
1270 *config8888 = SkCanvas::kRGBA_Unpremul_Config8888;
1271 return true;
1272 case kBGRA_8888_PM_GrPixelConfig:
1273 *config8888 = SkCanvas::kBGRA_Premul_Config8888;
1274 return true;
1275 case kBGRA_8888_UPM_GrPixelConfig:
1276 *config8888 = SkCanvas::kBGRA_Unpremul_Config8888;
1277 return true;
1278 default:
1279 return false;
1280 }
1281}
1282}
1283
bsalomon@google.com6f379512011-11-16 20:36:03 +00001284bool GrContext::internalReadRenderTargetPixels(GrRenderTarget* target,
1285 int left, int top,
1286 int width, int height,
1287 GrPixelConfig config,
1288 void* buffer,
1289 size_t rowBytes,
1290 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001291 SK_TRACE_EVENT0("GrContext::readRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001292 ASSERT_OWNED_RESOURCE(target);
1293
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001294 if (NULL == target) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001295 target = fDrawState->getRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001296 if (NULL == target) {
1297 return false;
1298 }
1299 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001300
bsalomon@google.com6f379512011-11-16 20:36:03 +00001301 if (!(kDontFlush_PixelOpsFlag & flags)) {
1302 this->flush();
1303 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001304
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001305 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1306 GrPixelConfigIsUnpremultiplied(config) &&
1307 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1308 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1309 if (!grconfig_to_config8888(target->config(), &srcConfig8888) ||
1310 !grconfig_to_config8888(config, &dstConfig8888)) {
1311 return false;
1312 }
1313 // do read back using target's own config
1314 this->internalReadRenderTargetPixels(target,
1315 left, top,
1316 width, height,
1317 target->config(),
1318 buffer, rowBytes,
1319 kDontFlush_PixelOpsFlag);
1320 // sw convert the pixels to unpremul config
1321 uint32_t* pixels = reinterpret_cast<uint32_t*>(buffer);
1322 SkConvertConfig8888Pixels(pixels, rowBytes, dstConfig8888,
1323 pixels, rowBytes, srcConfig8888,
1324 width, height);
1325 return true;
1326 }
1327
bsalomon@google.comc4364992011-11-07 15:54:49 +00001328 GrTexture* src = target->asTexture();
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001329 bool swapRAndB = NULL != src &&
1330 fGpu->preferredReadPixelsConfig(config) ==
1331 GrPixelConfigSwapRAndB(config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001332
1333 bool flipY = NULL != src &&
1334 fGpu->readPixelsWillPayForYFlip(target, left, top,
1335 width, height, config,
1336 rowBytes);
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001337 bool alphaConversion = (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1338 GrPixelConfigIsUnpremultiplied(config));
bsalomon@google.comc4364992011-11-07 15:54:49 +00001339
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001340 if (NULL == src && alphaConversion) {
1341 // we should fallback to cpu conversion here. This could happen when
1342 // we were given an external render target by the client that is not
1343 // also a texture (e.g. FBO 0 in GL)
1344 return false;
1345 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001346 // we draw to a scratch texture if any of these conversion are applied
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001347 GrAutoScratchTexture ast;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001348 if (flipY || swapRAndB || alphaConversion) {
1349 GrAssert(NULL != src);
1350 if (swapRAndB) {
1351 config = GrPixelConfigSwapRAndB(config);
1352 GrAssert(kUnknown_GrPixelConfig != config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001353 }
1354 // Make the scratch a render target because we don't have a robust
1355 // readTexturePixels as of yet (it calls this function).
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001356 GrTextureDesc desc;
1357 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1358 desc.fWidth = width;
1359 desc.fHeight = height;
1360 desc.fConfig = config;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001361
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001362 // When a full readback is faster than a partial we could always make
1363 // the scratch exactly match the passed rect. However, if we see many
1364 // different size rectangles we will trash our texture cache and pay the
1365 // cost of creating and destroying many textures. So, we only request
1366 // an exact match when the caller is reading an entire RT.
1367 ScratchTexMatch match = kApprox_ScratchTexMatch;
1368 if (0 == left &&
1369 0 == top &&
1370 target->width() == width &&
1371 target->height() == height &&
1372 fGpu->fullReadPixelsIsFasterThanPartial()) {
1373 match = kExact_ScratchTexMatch;
1374 }
1375 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001376 GrTexture* texture = ast.texture();
1377 if (!texture) {
1378 return false;
1379 }
1380 target = texture->asRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001381 GrAssert(NULL != target);
1382
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001383 GrDrawTarget::AutoStateRestore asr(fGpu,
1384 GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001385 GrDrawState* drawState = fGpu->drawState();
1386 drawState->setRenderTarget(target);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001387
bsalomon@google.comc4364992011-11-07 15:54:49 +00001388 GrMatrix matrix;
1389 if (flipY) {
1390 matrix.setTranslate(SK_Scalar1 * left,
1391 SK_Scalar1 * (top + height));
1392 matrix.set(GrMatrix::kMScaleY, -GR_Scalar1);
1393 } else {
1394 matrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
1395 }
1396 matrix.postIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001397 drawState->sampler(0)->reset(matrix);
1398 drawState->sampler(0)->setRAndBSwap(swapRAndB);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001399 drawState->createTextureEffect(0, src);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001400 GrRect rect;
1401 rect.setXYWH(0, 0, SK_Scalar1 * width, SK_Scalar1 * height);
bsalomon@google.come3d32162012-07-20 13:37:06 +00001402 fGpu->drawSimpleRect(rect, NULL);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001403 left = 0;
1404 top = 0;
1405 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001406 return fGpu->readPixels(target,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001407 left, top, width, height,
1408 config, buffer, rowBytes, flipY);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001409}
1410
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001411void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1412 GrAssert(target);
1413 ASSERT_OWNED_RESOURCE(target);
1414 // In the future we may track whether there are any pending draws to this
1415 // target. We don't today so we always perform a flush. We don't promise
1416 // this to our clients, though.
1417 this->flush();
1418 fGpu->resolveRenderTarget(target);
1419}
1420
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001421void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst) {
1422 if (NULL == src || NULL == dst) {
1423 return;
1424 }
1425 ASSERT_OWNED_RESOURCE(src);
1426
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001427 // Writes pending to the source texture are not tracked, so a flush
1428 // is required to ensure that the copy captures the most recent contents
1429 // of the source texture. See similar behaviour in
1430 // GrContext::resolveRenderTarget.
1431 this->flush();
1432
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001433 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001434 GrDrawState* drawState = fGpu->drawState();
1435 drawState->setRenderTarget(dst);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001436 GrMatrix sampleM;
1437 sampleM.setIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001438 drawState->sampler(0)->reset(sampleM);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001439 drawState->createTextureEffect(0, src);
bsalomon@google.com5db3b6c2012-01-12 20:38:57 +00001440 SkRect rect = SkRect::MakeXYWH(0, 0,
1441 SK_Scalar1 * src->width(),
1442 SK_Scalar1 * src->height());
bsalomon@google.come3d32162012-07-20 13:37:06 +00001443 fGpu->drawSimpleRect(rect, NULL);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001444}
1445
bsalomon@google.com6f379512011-11-16 20:36:03 +00001446void GrContext::internalWriteRenderTargetPixels(GrRenderTarget* target,
1447 int left, int top,
1448 int width, int height,
1449 GrPixelConfig config,
1450 const void* buffer,
1451 size_t rowBytes,
1452 uint32_t flags) {
1453 SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001454 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001455
1456 if (NULL == target) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001457 target = fDrawState->getRenderTarget();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001458 if (NULL == target) {
1459 return;
1460 }
1461 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001462
1463 // TODO: when underlying api has a direct way to do this we should use it
1464 // (e.g. glDrawPixels on desktop GL).
1465
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001466 // If the RT is also a texture and we don't have to do PM/UPM conversion
1467 // then take the texture path, which we expect to be at least as fast or
1468 // faster since it doesn't use an intermediate texture as we do below.
1469
1470#if !GR_MAC_BUILD
1471 // At least some drivers on the Mac get confused when glTexImage2D is called
1472 // on a texture attached to an FBO. The FBO still sees the old image. TODO:
1473 // determine what OS versions and/or HW is affected.
1474 if (NULL != target->asTexture() &&
1475 GrPixelConfigIsUnpremultiplied(target->config()) ==
1476 GrPixelConfigIsUnpremultiplied(config)) {
1477
1478 this->internalWriteTexturePixels(target->asTexture(),
1479 left, top, width, height,
1480 config, buffer, rowBytes, flags);
1481 return;
1482 }
1483#endif
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001484 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1485 GrPixelConfigIsUnpremultiplied(config) &&
1486 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1487 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1488 if (!grconfig_to_config8888(config, &srcConfig8888) ||
1489 !grconfig_to_config8888(target->config(), &dstConfig8888)) {
1490 return;
1491 }
1492 // allocate a tmp buffer and sw convert the pixels to premul
1493 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(width * height);
1494 const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer);
1495 SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888,
1496 src, rowBytes, srcConfig8888,
1497 width, height);
1498 // upload the already premul pixels
1499 this->internalWriteRenderTargetPixels(target,
1500 left, top,
1501 width, height,
1502 target->config(),
1503 tmpPixels, 4 * width, flags);
1504 return;
1505 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001506
1507 bool swapRAndB = fGpu->preferredReadPixelsConfig(config) ==
1508 GrPixelConfigSwapRAndB(config);
1509 if (swapRAndB) {
1510 config = GrPixelConfigSwapRAndB(config);
1511 }
1512
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001513 GrTextureDesc desc;
1514 desc.fWidth = width;
1515 desc.fHeight = height;
1516 desc.fConfig = config;
1517
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001518 GrAutoScratchTexture ast(this, desc);
1519 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001520 if (NULL == texture) {
1521 return;
1522 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001523 this->internalWriteTexturePixels(texture, 0, 0, width, height,
1524 config, buffer, rowBytes, flags);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001525
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001526 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001527 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001528
1529 GrMatrix matrix;
1530 matrix.setTranslate(GrIntToScalar(left), GrIntToScalar(top));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001531 drawState->setViewMatrix(matrix);
1532 drawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001533
bsalomon@google.com5c638652011-07-18 19:31:59 +00001534 matrix.setIDiv(texture->width(), texture->height());
bsalomon@google.comb8670992012-07-25 21:27:09 +00001535 drawState->sampler(0)->reset(matrix);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001536 drawState->createTextureEffect(0, texture);
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001537 drawState->sampler(0)->setRAndBSwap(swapRAndB);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001538
tomhudson@google.comb213ed82012-06-25 15:22:12 +00001539 static const GrVertexLayout layout = 0;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001540 static const int VCOUNT = 4;
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001541 // TODO: Use GrGpu::drawRect here
bsalomon@google.com27847de2011-02-22 20:59:41 +00001542 GrDrawTarget::AutoReleaseGeometry geo(fGpu, layout, VCOUNT, 0);
1543 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001544 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +00001545 return;
1546 }
1547 ((GrPoint*)geo.vertices())->setIRectFan(0, 0, width, height);
bsalomon@google.com47059542012-06-06 20:51:20 +00001548 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, VCOUNT);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001549}
1550////////////////////////////////////////////////////////////////////////////////
1551
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001552void GrContext::setPaint(const GrPaint& paint) {
tomhudson@google.comcb325ce2012-07-11 14:41:19 +00001553 GrAssert(fDrawState->stagesDisabled());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001554
1555 for (int i = 0; i < GrPaint::kMaxTextures; ++i) {
1556 int s = i + GrPaint::kFirstTextureStage;
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001557 if (paint.isTextureStageEnabled(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001558 *fDrawState->sampler(s) = paint.getTextureSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001559 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001560 }
1561
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001562 fDrawState->setFirstCoverageStage(GrPaint::kFirstMaskStage);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001563
1564 for (int i = 0; i < GrPaint::kMaxMasks; ++i) {
1565 int s = i + GrPaint::kFirstMaskStage;
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001566 if (paint.isMaskStageEnabled(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001567 *fDrawState->sampler(s) = paint.getMaskSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001568 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001569 }
bsalomon@google.com26936d02012-03-19 13:06:19 +00001570
1571 // disable all stages not accessible via the paint
1572 for (int s = GrPaint::kTotalStages; s < GrDrawState::kNumStages; ++s) {
tomhudson@google.com676e6602012-07-10 17:21:48 +00001573 fDrawState->disableStage(s);
bsalomon@google.com26936d02012-03-19 13:06:19 +00001574 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001575
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001576 fDrawState->setColor(paint.fColor);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001577
1578 if (paint.fDither) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001579 fDrawState->enableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001580 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001581 fDrawState->disableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001582 }
1583 if (paint.fAntiAlias) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001584 fDrawState->enableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001585 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001586 fDrawState->disableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001587 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001588 if (paint.fColorMatrixEnabled) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001589 fDrawState->enableState(GrDrawState::kColorMatrix_StateBit);
1590 fDrawState->setColorMatrix(paint.fColorMatrix);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001591 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001592 fDrawState->disableState(GrDrawState::kColorMatrix_StateBit);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001593 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001594 fDrawState->setBlendFunc(paint.fSrcBlendCoeff, paint.fDstBlendCoeff);
1595 fDrawState->setColorFilter(paint.fColorFilterColor, paint.fColorFilterXfermode);
1596 fDrawState->setCoverage(paint.fCoverage);
reed@google.com4b2d3f32012-05-15 18:05:50 +00001597#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
bsalomon@google.come3d32162012-07-20 13:37:06 +00001598 if ((paint.hasMask() || 0xff != paint.fCoverage) &&
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001599 !fGpu->canApplyCoverage()) {
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001600 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1601 }
bsalomon@google.com95cd7bd2012-03-28 15:35:05 +00001602#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001603}
1604
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001605GrDrawTarget* GrContext::prepareToDraw(const GrPaint& paint,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001606 DrawCategory category) {
1607 if (category != fLastDrawCategory) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001608 this->flushDrawBuffer();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001609 fLastDrawCategory = category;
1610 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001611 this->setPaint(paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001612 GrDrawTarget* target = fGpu;
1613 switch (category) {
bsalomon@google.com193395c2012-03-30 17:35:12 +00001614 case kUnbuffered_DrawCategory:
1615 target = fGpu;
1616 break;
1617 case kBuffered_DrawCategory:
1618 target = fDrawBuffer;
1619 fDrawBuffer->setClip(fGpu->getClip());
1620 break;
1621 default:
1622 GrCrash("Unexpected DrawCategory.");
1623 break;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001624 }
1625 return target;
1626}
1627
robertphillips@google.com72176b22012-05-23 13:19:12 +00001628/*
1629 * This method finds a path renderer that can draw the specified path on
1630 * the provided target.
1631 * Due to its expense, the software path renderer has split out so it can
1632 * can be individually allowed/disallowed via the "allowSW" boolean.
1633 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001634GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
bsalomon@google.com289533a2011-10-27 12:34:25 +00001635 GrPathFill fill,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001636 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001637 bool antiAlias,
1638 bool allowSW) {
bsalomon@google.com30085192011-08-19 15:42:31 +00001639 if (NULL == fPathRendererChain) {
1640 fPathRendererChain =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001641 SkNEW_ARGS(GrPathRendererChain,
1642 (this, GrPathRendererChain::kNone_UsageFlag));
bsalomon@google.com30085192011-08-19 15:42:31 +00001643 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001644
1645 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path, fill,
1646 target,
1647 antiAlias);
1648
1649 if (NULL == pr && allowSW) {
1650 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001651 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001652 }
1653
1654 pr = fSoftwarePathRenderer;
1655 }
1656
1657 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001658}
1659
bsalomon@google.com27847de2011-02-22 20:59:41 +00001660////////////////////////////////////////////////////////////////////////////////
1661
bsalomon@google.com27847de2011-02-22 20:59:41 +00001662void GrContext::setRenderTarget(GrRenderTarget* target) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001663 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001664 if (fDrawState->getRenderTarget() != target) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001665 this->flush(false);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001666 fDrawState->setRenderTarget(target);
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001667 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001668}
1669
1670GrRenderTarget* GrContext::getRenderTarget() {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001671 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001672}
1673
1674const GrRenderTarget* GrContext::getRenderTarget() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001675 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001676}
1677
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001678bool GrContext::isConfigRenderable(GrPixelConfig config) const {
1679 return fGpu->isConfigRenderable(config);
1680}
1681
bsalomon@google.com27847de2011-02-22 20:59:41 +00001682const GrMatrix& GrContext::getMatrix() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001683 return fDrawState->getViewMatrix();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001684}
1685
1686void GrContext::setMatrix(const GrMatrix& m) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001687 fDrawState->setViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001688}
1689
1690void GrContext::concatMatrix(const GrMatrix& m) const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001691 fDrawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001692}
1693
1694static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
1695 intptr_t mask = 1 << shift;
1696 if (pred) {
1697 bits |= mask;
1698 } else {
1699 bits &= ~mask;
1700 }
1701 return bits;
1702}
1703
bsalomon@google.com583a1e32011-08-17 13:42:46 +00001704GrContext::GrContext(GrGpu* gpu) {
bsalomon@google.comc0af3172012-06-15 14:10:09 +00001705 ++THREAD_INSTANCE_COUNT;
1706
bsalomon@google.com27847de2011-02-22 20:59:41 +00001707 fGpu = gpu;
1708 fGpu->ref();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001709 fGpu->setContext(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001710
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001711 fDrawState = SkNEW(GrDrawState);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001712 fGpu->setDrawState(fDrawState);
1713
bsalomon@google.com30085192011-08-19 15:42:31 +00001714 fPathRendererChain = NULL;
robertphillips@google.com72176b22012-05-23 13:19:12 +00001715 fSoftwarePathRenderer = NULL;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001716
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001717 fTextureCache = SkNEW_ARGS(GrResourceCache,
1718 (MAX_TEXTURE_CACHE_COUNT,
1719 MAX_TEXTURE_CACHE_BYTES));
1720 fFontCache = SkNEW_ARGS(GrFontCache, (fGpu));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001721
1722 fLastDrawCategory = kUnbuffered_DrawCategory;
1723
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001724 fDrawBuffer = NULL;
1725 fDrawBufferVBAllocPool = NULL;
1726 fDrawBufferIBAllocPool = NULL;
1727
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001728 fAARectRenderer = SkNEW(GrAARectRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001729
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001730 this->setupDrawBuffer();
1731}
1732
1733void GrContext::setupDrawBuffer() {
1734
1735 GrAssert(NULL == fDrawBuffer);
1736 GrAssert(NULL == fDrawBufferVBAllocPool);
1737 GrAssert(NULL == fDrawBufferIBAllocPool);
1738
bsalomon@google.com92edd312012-04-04 21:40:21 +00001739#if DEFER_TEXT_RENDERING || BATCH_RECT_TO_RECT || DEFER_PATHS
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001740 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001741 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001742 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001743 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001744 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001745 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001746 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001747 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001748
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001749 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com471d4712011-08-23 15:45:25 +00001750 fDrawBufferVBAllocPool,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001751 fDrawBufferIBAllocPool));
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001752#endif
1753
1754#if BATCH_RECT_TO_RECT
bsalomon@google.com27847de2011-02-22 20:59:41 +00001755 fDrawBuffer->setQuadIndexBuffer(this->getQuadIndexBuffer());
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001756#endif
bsalomon@google.com1015e032012-06-25 18:41:04 +00001757 if (fDrawBuffer) {
1758 fDrawBuffer->setAutoFlushTarget(fGpu);
1759 fDrawBuffer->setDrawState(fDrawState);
1760 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001761}
1762
bsalomon@google.com27847de2011-02-22 20:59:41 +00001763GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001764#if DEFER_TEXT_RENDERING
bsalomon@google.com193395c2012-03-30 17:35:12 +00001765 return prepareToDraw(paint, kBuffered_DrawCategory);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001766#else
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001767 return prepareToDraw(paint, kUnbuffered_DrawCategory);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001768#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001769}
1770
1771const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1772 return fGpu->getQuadIndexBuffer();
1773}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001774
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001775GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture,
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001776 bool canClobberSrc,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001777 const SkRect& rect,
1778 float sigmaX, float sigmaY) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001779 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001780 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001781 AutoMatrix avm(this, GrMatrix::I());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001782 SkIRect clearRect;
bsalomon@google.comb505a122012-05-31 18:40:36 +00001783 int scaleFactorX, radiusX;
1784 int scaleFactorY, radiusY;
1785 sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX);
1786 sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY);
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001787
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001788 SkRect srcRect(rect);
1789 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
1790 srcRect.roundOut();
robertphillips@google.com8637a362012-04-10 18:32:35 +00001791 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
1792 static_cast<float>(scaleFactorY));
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001793
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001794 AutoClip acs(this, srcRect);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001795
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001796 GrAssert(kBGRA_8888_PM_GrPixelConfig == srcTexture->config() ||
1797 kRGBA_8888_PM_GrPixelConfig == srcTexture->config() ||
1798 kAlpha_8_GrPixelConfig == srcTexture->config());
1799
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001800 GrTextureDesc desc;
1801 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1802 desc.fWidth = SkScalarFloorToInt(srcRect.width());
1803 desc.fHeight = SkScalarFloorToInt(srcRect.height());
1804 desc.fConfig = srcTexture->config();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001805
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001806 GrAutoScratchTexture temp1, temp2;
1807 GrTexture* dstTexture = temp1.set(this, desc);
1808 GrTexture* tempTexture = canClobberSrc ? srcTexture : temp2.set(this, desc);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001809
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001810 GrPaint paint;
1811 paint.reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +00001812 paint.textureSampler(0)->textureParams()->setBilerp(true);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001813
1814 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
1815 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1816 srcTexture->height());
1817 this->setRenderTarget(dstTexture->asRenderTarget());
1818 SkRect dstRect(srcRect);
1819 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
1820 i < scaleFactorY ? 0.5f : 1.0f);
tomhudson@google.comaa72eab2012-07-19 18:01:07 +00001821 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
1822 (srcTexture)))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001823 this->drawRectToRect(paint, dstRect, srcRect);
1824 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001825 srcTexture = dstTexture;
1826 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001827 }
1828
robertphillips@google.com7a396332012-05-10 15:11:27 +00001829 SkIRect srcIRect;
1830 srcRect.roundOut(&srcIRect);
1831
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001832 if (sigmaX > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001833 if (scaleFactorX > 1) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001834 // Clear out a radius to the right of the srcRect to prevent the
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001835 // X convolution from reading garbage.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001836 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001837 radiusX, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001838 this->clear(&clearRect, 0x0);
1839 }
1840
1841 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001842 convolve_gaussian(fGpu, srcTexture, srcRect, sigmaX, radiusX,
1843 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001844 srcTexture = dstTexture;
1845 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001846 }
1847
1848 if (sigmaY > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001849 if (scaleFactorY > 1 || sigmaX > 0.0f) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001850 // Clear out a radius below the srcRect to prevent the Y
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001851 // convolution from reading garbage.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001852 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001853 srcIRect.width(), radiusY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001854 this->clear(&clearRect, 0x0);
1855 }
1856
1857 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001858 convolve_gaussian(fGpu, srcTexture, srcRect, sigmaY, radiusY,
1859 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001860 srcTexture = dstTexture;
1861 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001862 }
1863
1864 if (scaleFactorX > 1 || scaleFactorY > 1) {
1865 // Clear one pixel to the right and below, to accommodate bilinear
1866 // upsampling.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001867 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
1868 srcIRect.width() + 1, 1);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001869 this->clear(&clearRect, 0x0);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001870 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
1871 1, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001872 this->clear(&clearRect, 0x0);
1873 // FIXME: This should be mitchell, not bilinear.
bsalomon@google.comb8670992012-07-25 21:27:09 +00001874 paint.textureSampler(0)->textureParams()->setBilerp(true);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001875 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1876 srcTexture->height());
1877 this->setRenderTarget(dstTexture->asRenderTarget());
tomhudson@google.comaa72eab2012-07-19 18:01:07 +00001878 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
1879 (srcTexture)))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001880 SkRect dstRect(srcRect);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001881 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001882 this->drawRectToRect(paint, dstRect, srcRect);
1883 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001884 srcTexture = dstTexture;
1885 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001886 }
1887 this->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001888 if (srcTexture == temp1.texture()) {
1889 return temp1.detach();
1890 } else if (srcTexture == temp2.texture()) {
1891 return temp2.detach();
1892 } else {
1893 srcTexture->ref();
1894 return srcTexture;
1895 }
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001896}
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001897
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001898GrTexture* GrContext::applyMorphology(GrTexture* srcTexture,
1899 const GrRect& rect,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001900 MorphologyType morphType,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001901 SkISize radius) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001902 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001903 srcTexture->ref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001904 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001905
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001906 AutoMatrix avm(this, GrMatrix::I());
1907
1908 AutoClip acs(this, GrRect::MakeWH(SkIntToScalar(srcTexture->width()),
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001909 SkIntToScalar(srcTexture->height())));
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001910 GrTextureDesc desc;
1911 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1912 desc.fWidth = SkScalarCeilToInt(rect.width());
1913 desc.fHeight = SkScalarCeilToInt(rect.height());
1914 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001915 if (radius.fWidth > 0) {
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001916 GrAutoScratchTexture ast(this, desc);
1917 this->setRenderTarget(ast.texture()->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001918 apply_morphology(fGpu, srcTexture, rect, radius.fWidth, morphType,
1919 Gr1DKernelEffect::kX_Direction);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001920 SkIRect clearRect = SkIRect::MakeXYWH(
1921 SkScalarFloorToInt(rect.fLeft),
1922 SkScalarFloorToInt(rect.fBottom),
1923 SkScalarFloorToInt(rect.width()),
1924 radius.fHeight);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001925 this->clear(&clearRect, 0x0);
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 if (radius.fHeight > 0) {
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001930 GrAutoScratchTexture ast(this, desc);
1931 this->setRenderTarget(ast.texture()->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001932 apply_morphology(fGpu, srcTexture, rect, radius.fHeight, morphType,
1933 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001934 srcTexture->unref();
1935 srcTexture = ast.detach();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001936 }
1937 this->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001938 return srcTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001939}
bsalomon@google.comc4364992011-11-07 15:54:49 +00001940
1941///////////////////////////////////////////////////////////////////////////////