blob: 49cbec4db942d6c2b3439dc53ec6d33d60b183fa [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
180// we should never have more than one stencil buffer with same combo of
181// (width,height,samplecount)
182void gen_stencil_key_values(int width, int height,
183 int sampleCnt, uint32_t v[4]) {
184 v[0] = width;
185 v[1] = height;
186 v[2] = sampleCnt;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000187 v[3] = GrResourceKey::kStencilBuffer_TypeBit;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000188}
189
190void gen_stencil_key_values(const GrStencilBuffer* sb,
191 uint32_t v[4]) {
192 gen_stencil_key_values(sb->width(), sb->height(),
193 sb->numSamples(), v);
194}
bsalomon@google.com82c7bd82011-11-09 15:32:29 +0000195
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000196void scale_rect(SkRect* rect, float xScale, float yScale) {
robertphillips@google.com5af56062012-04-27 15:39:52 +0000197 rect->fLeft = SkScalarMul(rect->fLeft, SkFloatToScalar(xScale));
198 rect->fTop = SkScalarMul(rect->fTop, SkFloatToScalar(yScale));
199 rect->fRight = SkScalarMul(rect->fRight, SkFloatToScalar(xScale));
200 rect->fBottom = SkScalarMul(rect->fBottom, SkFloatToScalar(yScale));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000201}
202
bsalomon@google.comb505a122012-05-31 18:40:36 +0000203float adjust_sigma(float sigma, int *scaleFactor, int *radius) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000204 *scaleFactor = 1;
205 while (sigma > MAX_BLUR_SIGMA) {
206 *scaleFactor *= 2;
207 sigma *= 0.5f;
208 }
bsalomon@google.comb505a122012-05-31 18:40:36 +0000209 *radius = static_cast<int>(ceilf(sigma * 3.0f));
210 GrAssert(*radius <= GrConvolutionEffect::kMaxKernelRadius);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000211 return sigma;
212}
213
214void apply_morphology(GrGpu* gpu,
215 GrTexture* texture,
216 const SkRect& rect,
217 int radius,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000218 GrContext::MorphologyType morphType,
219 Gr1DKernelEffect::Direction direction) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000220
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000221 GrRenderTarget* target = gpu->drawState()->getRenderTarget();
222 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000223 GrDrawState* drawState = gpu->drawState();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000224 drawState->setRenderTarget(target);
225 GrMatrix sampleM;
226 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000227 drawState->sampler(0)->reset(sampleM);
228 SkAutoTUnref<GrCustomStage> morph(
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000229 SkNEW_ARGS(GrMorphologyEffect, (texture, direction, radius, morphType)));
bsalomon@google.comb505a122012-05-31 18:40:36 +0000230 drawState->sampler(0)->setCustomStage(morph);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000231 gpu->drawSimpleRect(rect, NULL);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000232}
233
bsalomon@google.comb505a122012-05-31 18:40:36 +0000234void convolve_gaussian(GrGpu* gpu,
235 GrTexture* texture,
236 const SkRect& rect,
237 float sigma,
238 int radius,
239 Gr1DKernelEffect::Direction direction) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000240 GrRenderTarget* target = gpu->drawState()->getRenderTarget();
241 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000242 GrDrawState* drawState = gpu->drawState();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000243 drawState->setRenderTarget(target);
244 GrMatrix sampleM;
245 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000246 drawState->sampler(0)->reset(sampleM);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000247 SkAutoTUnref<GrConvolutionEffect> conv(SkNEW_ARGS(GrConvolutionEffect,
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000248 (texture, direction, radius,
249 sigma)));
bsalomon@google.comb505a122012-05-31 18:40:36 +0000250 drawState->sampler(0)->setCustomStage(conv);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000251 gpu->drawSimpleRect(rect, NULL);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000252}
253
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000254}
255
bsalomon@google.comb8670992012-07-25 21:27:09 +0000256GrContext::TextureCacheEntry GrContext::findAndLockTexture(const GrTextureDesc& desc,
257 const GrTextureParams* params) {
258 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, false);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000259 return TextureCacheEntry(fTextureCache->findAndLock(resourceKey,
260 GrResourceCache::kNested_LockType));
261}
262
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000263bool GrContext::isTextureInCache(const GrTextureDesc& desc,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000264 const GrTextureParams* params) const {
265 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, false);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000266 return fTextureCache->hasKey(resourceKey);
267}
268
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000269GrResourceEntry* GrContext::addAndLockStencilBuffer(GrStencilBuffer* sb) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000270 ASSERT_OWNED_RESOURCE(sb);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000271 uint32_t v[4];
272 gen_stencil_key_values(sb, v);
273 GrResourceKey resourceKey(v);
274 return fTextureCache->createAndLock(resourceKey, sb);
275}
276
277GrStencilBuffer* GrContext::findStencilBuffer(int width, int height,
278 int sampleCnt) {
279 uint32_t v[4];
280 gen_stencil_key_values(width, height, sampleCnt, v);
281 GrResourceKey resourceKey(v);
282 GrResourceEntry* entry = fTextureCache->findAndLock(resourceKey,
283 GrResourceCache::kSingle_LockType);
284 if (NULL != entry) {
285 GrStencilBuffer* sb = (GrStencilBuffer*) entry->resource();
286 return sb;
287 } else {
288 return NULL;
289 }
290}
291
292void GrContext::unlockStencilBuffer(GrResourceEntry* sbEntry) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000293 ASSERT_OWNED_RESOURCE(sbEntry->resource());
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000294 fTextureCache->unlock(sbEntry);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000295}
296
297static void stretchImage(void* dst,
298 int dstW,
299 int dstH,
300 void* src,
301 int srcW,
302 int srcH,
303 int bpp) {
304 GrFixed dx = (srcW << 16) / dstW;
305 GrFixed dy = (srcH << 16) / dstH;
306
307 GrFixed y = dy >> 1;
308
309 int dstXLimit = dstW*bpp;
310 for (int j = 0; j < dstH; ++j) {
311 GrFixed x = dx >> 1;
312 void* srcRow = (uint8_t*)src + (y>>16)*srcW*bpp;
313 void* dstRow = (uint8_t*)dst + j*dstW*bpp;
314 for (int i = 0; i < dstXLimit; i += bpp) {
315 memcpy((uint8_t*) dstRow + i,
316 (uint8_t*) srcRow + (x>>16)*bpp,
317 bpp);
318 x += dx;
319 }
320 y += dy;
321 }
322}
323
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000324GrContext::TextureCacheEntry GrContext::createAndLockTexture(
bsalomon@google.comb8670992012-07-25 21:27:09 +0000325 const GrTextureParams* params,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000326 const GrTextureDesc& desc,
327 void* srcData,
328 size_t rowBytes) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000329 SK_TRACE_EVENT0("GrContext::createAndLockTexture");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000330
331#if GR_DUMP_TEXTURE_UPLOAD
332 GrPrintf("GrContext::createAndLockTexture [%d %d]\n", desc.fWidth, desc.fHeight);
333#endif
334
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000335 TextureCacheEntry entry;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000336
bsalomon@google.comb8670992012-07-25 21:27:09 +0000337 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, false);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000338
339 if (GrTexture::NeedsResizing(resourceKey)) {
340 // The desired texture is NPOT and tiled but that isn't supported by
341 // the current hardware. Resize the texture to be a POT
bsalomon@google.comb8670992012-07-25 21:27:09 +0000342 GrAssert(NULL != params);
343 TextureCacheEntry clampEntry = this->findAndLockTexture(desc, NULL);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000344
345 if (NULL == clampEntry.texture()) {
bsalomon@google.comb8670992012-07-25 21:27:09 +0000346 clampEntry = this->createAndLockTexture(NULL, desc, srcData, rowBytes);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000347 GrAssert(NULL != clampEntry.texture());
348 if (NULL == clampEntry.texture()) {
349 return entry;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000350 }
351 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000352 GrTextureDesc rtDesc = desc;
353 rtDesc.fFlags = rtDesc.fFlags |
354 kRenderTarget_GrTextureFlagBit |
355 kNoStencil_GrTextureFlagBit;
bsalomon@google.com99621082011-11-15 16:47:16 +0000356 rtDesc.fWidth = GrNextPow2(GrMax(desc.fWidth, 64));
357 rtDesc.fHeight = GrNextPow2(GrMax(desc.fHeight, 64));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000358
359 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
360
361 if (NULL != texture) {
bsalomon@google.comb8670992012-07-25 21:27:09 +0000362 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000363 GrDrawState* drawState = fGpu->drawState();
364 drawState->setRenderTarget(texture->asRenderTarget());
bsalomon@google.com82c7bd82011-11-09 15:32:29 +0000365
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000366 // if filtering is not desired then we want to ensure all
367 // texels in the resampled image are copies of texels from
368 // the original.
bsalomon@google.comb8670992012-07-25 21:27:09 +0000369 drawState->sampler(0)->reset(SkShader::kClamp_TileMode,
370 GrTexture::NeedsFiltering(resourceKey));
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000371 drawState->createTextureEffect(0, clampEntry.texture());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000372
373 static const GrVertexLayout layout =
374 GrDrawTarget::StageTexCoordVertexLayoutBit(0,0);
375 GrDrawTarget::AutoReleaseGeometry arg(fGpu, layout, 4, 0);
376
377 if (arg.succeeded()) {
378 GrPoint* verts = (GrPoint*) arg.vertices();
379 verts[0].setIRectFan(0, 0,
380 texture->width(),
381 texture->height(),
382 2*sizeof(GrPoint));
383 verts[1].setIRectFan(0, 0, 1, 1, 2*sizeof(GrPoint));
bsalomon@google.com47059542012-06-06 20:51:20 +0000384 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000385 0, 4);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000386 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000387 }
bsalomon@google.com1da07462011-03-10 14:51:57 +0000388 texture->releaseRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000389 } else {
390 // TODO: Our CPU stretch doesn't filter. But we create separate
391 // stretched textures when the sampler state is either filtered or
392 // not. Either implement filtered stretch blit on CPU or just create
393 // one when FBO case fails.
394
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000395 rtDesc.fFlags = kNone_GrTextureFlags;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000396 // no longer need to clamp at min RT size.
397 rtDesc.fWidth = GrNextPow2(desc.fWidth);
398 rtDesc.fHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000399 int bpp = GrBytesPerPixel(desc.fConfig);
bsalomon@google.com3582bf92011-06-30 21:32:31 +0000400 SkAutoSMalloc<128*128*4> stretchedPixels(bpp *
bsalomon@google.com27847de2011-02-22 20:59:41 +0000401 rtDesc.fWidth *
402 rtDesc.fHeight);
403 stretchImage(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
404 srcData, desc.fWidth, desc.fHeight, bpp);
405
406 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
407
408 GrTexture* texture = fGpu->createTexture(rtDesc,
409 stretchedPixels.get(),
410 stretchedRowBytes);
411 GrAssert(NULL != texture);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000412 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000413 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000414 fTextureCache->unlock(clampEntry.cacheEntry());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000415
416 } else {
417 GrTexture* texture = fGpu->createTexture(desc, srcData, rowBytes);
418 if (NULL != texture) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000419 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000420 }
421 }
422 return entry;
423}
424
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000425GrContext::TextureCacheEntry GrContext::lockScratchTexture(
426 const GrTextureDesc& inDesc,
427 ScratchTexMatch match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000428 GrTextureDesc desc = inDesc;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000429 desc.fClientCacheID = kScratch_CacheID;
430
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000431 if (kExact_ScratchTexMatch != match) {
432 // bin by pow2 with a reasonable min
433 static const int MIN_SIZE = 256;
434 desc.fWidth = GrMax(MIN_SIZE, GrNextPow2(desc.fWidth));
435 desc.fHeight = GrMax(MIN_SIZE, GrNextPow2(desc.fHeight));
436 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000437
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000438 GrResourceEntry* entry;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000439 int origWidth = desc.fWidth;
440 int origHeight = desc.fHeight;
441 bool doubledW = false;
442 bool doubledH = false;
443
444 do {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000445 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL, desc, true);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000446 entry = fTextureCache->findAndLock(key,
447 GrResourceCache::kNested_LockType);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000448 // if we miss, relax the fit of the flags...
449 // then try doubling width... then height.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000450 if (NULL != entry || kExact_ScratchTexMatch == match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000451 break;
452 }
453 if (!(desc.fFlags & kRenderTarget_GrTextureFlagBit)) {
454 desc.fFlags = desc.fFlags | kRenderTarget_GrTextureFlagBit;
455 } else if (desc.fFlags & kNoStencil_GrTextureFlagBit) {
456 desc.fFlags = desc.fFlags & ~kNoStencil_GrTextureFlagBit;
457 } else if (!doubledW) {
458 desc.fFlags = inDesc.fFlags;
459 desc.fWidth *= 2;
460 doubledW = true;
461 } else if (!doubledH) {
462 desc.fFlags = inDesc.fFlags;
463 desc.fWidth = origWidth;
464 desc.fHeight *= 2;
465 doubledH = true;
466 } else {
467 break;
468 }
469
470 } while (true);
471
472 if (NULL == entry) {
473 desc.fFlags = inDesc.fFlags;
474 desc.fWidth = origWidth;
475 desc.fHeight = origHeight;
476 GrTexture* texture = fGpu->createTexture(desc, NULL, 0);
477 if (NULL != texture) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000478 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000479 texture->desc(),
480 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
500 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
501 texture->desc(),
502 true);
503 fTextureCache->attach(key, texture);
504}
505
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000506void GrContext::unlockTexture(TextureCacheEntry entry) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000507 ASSERT_OWNED_RESOURCE(entry.texture());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000508 // If this is a scratch texture we detached it from the cache
509 // while it was locked (to avoid two callers simultaneously getting
510 // the same texture).
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000511 if (GrTexture::IsScratchTexture(entry.cacheEntry()->key())) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000512 fTextureCache->reattachAndUnlock(entry.cacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000513 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000514 fTextureCache->unlock(entry.cacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000515 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000516}
517
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000518void GrContext::freeEntry(TextureCacheEntry entry) {
519 ASSERT_OWNED_RESOURCE(entry.texture());
520
521 fTextureCache->freeEntry(entry.cacheEntry());
522}
523
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000524GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000525 void* srcData,
526 size_t rowBytes) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000527 GrTextureDesc descCopy = descIn;
528 descCopy.fClientCacheID = kUncached_CacheID;
529 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000530}
531
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000532void GrContext::getTextureCacheLimits(int* maxTextures,
533 size_t* maxTextureBytes) const {
534 fTextureCache->getLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000535}
536
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000537void GrContext::setTextureCacheLimits(int maxTextures, size_t maxTextureBytes) {
538 fTextureCache->setLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000539}
540
bsalomon@google.com91958362011-06-13 17:58:13 +0000541int GrContext::getMaxTextureSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000542 return fGpu->getCaps().fMaxTextureSize;
bsalomon@google.com91958362011-06-13 17:58:13 +0000543}
544
545int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000546 return fGpu->getCaps().fMaxRenderTargetSize;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000547}
548
549///////////////////////////////////////////////////////////////////////////////
550
bsalomon@google.come269f212011-11-07 13:29:52 +0000551GrTexture* GrContext::createPlatformTexture(const GrPlatformTextureDesc& desc) {
552 return fGpu->createPlatformTexture(desc);
553}
554
555GrRenderTarget* GrContext::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
556 return fGpu->createPlatformRenderTarget(desc);
557}
558
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000559///////////////////////////////////////////////////////////////////////////////
560
bsalomon@google.comb8670992012-07-25 21:27:09 +0000561bool GrContext::supportsIndex8PixelConfig(const GrTextureParams* params,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000562 int width, int height) const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000563 const GrDrawTarget::Caps& caps = fGpu->getCaps();
564 if (!caps.f8BitPaletteSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000565 return false;
566 }
567
bsalomon@google.com27847de2011-02-22 20:59:41 +0000568 bool isPow2 = GrIsPow2(width) && GrIsPow2(height);
569
570 if (!isPow2) {
bsalomon@google.comb8670992012-07-25 21:27:09 +0000571 bool tiled = NULL != params && params->isTiled();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000572 if (tiled && !caps.fNPOTTextureTileSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000573 return false;
574 }
575 }
576 return true;
577}
578
579////////////////////////////////////////////////////////////////////////////////
580
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000581const GrClipData* GrContext::getClip() const {
582 return fGpu->getClip();
583}
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000584
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000585void GrContext::setClip(const GrClipData* clipData) {
586 fGpu->setClip(clipData);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000587 fDrawState->enableState(GrDrawState::kClip_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000588}
589
bsalomon@google.com27847de2011-02-22 20:59:41 +0000590////////////////////////////////////////////////////////////////////////////////
591
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000592void GrContext::clear(const GrIRect* rect,
593 const GrColor color,
594 GrRenderTarget* target) {
bsalomon@google.com398109c2011-04-14 18:40:27 +0000595 this->flush();
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000596 fGpu->clear(rect, color, target);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000597}
598
599void GrContext::drawPaint(const GrPaint& paint) {
600 // set rect to be big enough to fill the space, but not super-huge, so we
601 // don't overflow fixed-point implementations
bsalomon@google.comd302f142011-03-03 13:54:13 +0000602 GrRect r;
603 r.setLTRB(0, 0,
604 GrIntToScalar(getRenderTarget()->width()),
605 GrIntToScalar(getRenderTarget()->height()));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000606 GrMatrix inverse;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000607 SkTLazy<GrPaint> tmpPaint;
608 const GrPaint* p = &paint;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000609 AutoMatrix am;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000610
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000611 // We attempt to map r by the inverse matrix and draw that. mapRect will
612 // map the four corners and bound them with a new rect. This will not
613 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000614 if (!this->getMatrix().hasPerspective()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000615 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000616 GrPrintf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000617 return;
618 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000619 inverse.mapRect(&r);
620 } else {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000621 if (paint.hasTextureOrMask()) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000622 tmpPaint.set(paint);
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000623 p = tmpPaint.get();
bsalomon@google.come3d32162012-07-20 13:37:06 +0000624 if (!tmpPaint.get()->preConcatSamplerMatricesWithInverse(fDrawState->getViewMatrix())) {
625 GrPrintf("Could not invert matrix\n");
626 }
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000627 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000628 am.set(this, GrMatrix::I());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000629 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000630 // by definition this fills the entire clip, no need for AA
631 if (paint.fAntiAlias) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000632 if (!tmpPaint.isValid()) {
633 tmpPaint.set(paint);
634 p = tmpPaint.get();
635 }
636 GrAssert(p == tmpPaint.get());
637 tmpPaint.get()->fAntiAlias = false;
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000638 }
639 this->drawRect(*p, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000640}
641
bsalomon@google.com205d4602011-04-25 12:43:45 +0000642////////////////////////////////////////////////////////////////////////////////
643
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000644namespace {
645inline bool disable_coverage_aa_for_blend(GrDrawTarget* target) {
646 return DISABLE_COVERAGE_AA_FOR_BLEND && !target->canApplyCoverage();
647}
648}
649
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000650////////////////////////////////////////////////////////////////////////////////
651
bsalomon@google.com27847de2011-02-22 20:59:41 +0000652/* create a triangle strip that strokes the specified triangle. There are 8
653 unique vertices, but we repreat the last 2 to close up. Alternatively we
654 could use an indices array, and then only send 8 verts, but not sure that
655 would be faster.
656 */
bsalomon@google.com205d4602011-04-25 12:43:45 +0000657static void setStrokeRectStrip(GrPoint verts[10], GrRect rect,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000658 GrScalar width) {
659 const GrScalar rad = GrScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000660 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000661
662 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
663 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
664 verts[2].set(rect.fRight - rad, rect.fTop + rad);
665 verts[3].set(rect.fRight + rad, rect.fTop - rad);
666 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
667 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
668 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
669 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
670 verts[8] = verts[0];
671 verts[9] = verts[1];
672}
673
reed@google.com20efde72011-05-09 17:00:02 +0000674/**
675 * Returns true if the rects edges are integer-aligned.
676 */
677static bool isIRect(const GrRect& r) {
678 return GrScalarIsInt(r.fLeft) && GrScalarIsInt(r.fTop) &&
679 GrScalarIsInt(r.fRight) && GrScalarIsInt(r.fBottom);
680}
681
bsalomon@google.com205d4602011-04-25 12:43:45 +0000682static bool apply_aa_to_rect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000683 const GrRect& rect,
684 GrScalar width,
685 const GrMatrix* matrix,
686 GrMatrix* combinedMatrix,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000687 GrRect* devRect,
688 bool* useVertexCoverage) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000689 // we use a simple coverage ramp to do aa on axis-aligned rects
690 // we check if the rect will be axis-aligned, and the rect won't land on
691 // integer coords.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000692
bsalomon@google.coma3108262011-10-10 14:08:47 +0000693 // we are keeping around the "tweak the alpha" trick because
694 // it is our only hope for the fixed-pipe implementation.
695 // In a shader implementation we can give a separate coverage input
bsalomon@google.com289533a2011-10-27 12:34:25 +0000696 // TODO: remove this ugliness when we drop the fixed-pipe impl
bsalomon@google.coma3108262011-10-10 14:08:47 +0000697 *useVertexCoverage = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000698 if (!target->canTweakAlphaForCoverage()) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000699 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +0000700#if GR_DEBUG
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000701 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000702#endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000703 return false;
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000704 } else {
705 *useVertexCoverage = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000706 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000707 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000708 const GrDrawState& drawState = target->getDrawState();
709 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000710 return false;
711 }
712
bsalomon@google.com471d4712011-08-23 15:45:25 +0000713 if (0 == width && target->willUseHWAALines()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000714 return false;
715 }
716
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000717 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000718 return false;
719 }
720
721 if (NULL != matrix &&
722 !matrix->preservesAxisAlignment()) {
723 return false;
724 }
725
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000726 *combinedMatrix = drawState.getViewMatrix();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000727 if (NULL != matrix) {
728 combinedMatrix->preConcat(*matrix);
729 GrAssert(combinedMatrix->preservesAxisAlignment());
730 }
731
732 combinedMatrix->mapRect(devRect, rect);
733 devRect->sort();
734
735 if (width < 0) {
reed@google.com20efde72011-05-09 17:00:02 +0000736 return !isIRect(*devRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000737 } else {
738 return true;
739 }
740}
741
bsalomon@google.com27847de2011-02-22 20:59:41 +0000742void GrContext::drawRect(const GrPaint& paint,
743 const GrRect& rect,
744 GrScalar width,
745 const GrMatrix* matrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000746 SK_TRACE_EVENT0("GrContext::drawRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000747
748 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000749 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000750
bsalomon@google.com205d4602011-04-25 12:43:45 +0000751 GrRect devRect = rect;
752 GrMatrix combinedMatrix;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000753 bool useVertexCoverage;
bsalomon@google.com289533a2011-10-27 12:34:25 +0000754 bool needAA = paint.fAntiAlias &&
755 !this->getRenderTarget()->isMultisampled();
756 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix,
757 &combinedMatrix, &devRect,
758 &useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000759
760 if (doAA) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000761 GrDrawTarget::AutoDeviceCoordDraw adcd(target);
762 if (!adcd.succeeded()) {
763 return;
764 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000765 if (width >= 0) {
766 GrVec strokeSize;;
767 if (width > 0) {
768 strokeSize.set(width, width);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000769 combinedMatrix.mapVectors(&strokeSize, 1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000770 strokeSize.setAbs(strokeSize);
771 } else {
772 strokeSize.set(GR_Scalar1, GR_Scalar1);
773 }
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000774 fAARectRenderer->strokeAARect(this->getGpu(), target, devRect,
775 strokeSize, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000776 } else {
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000777 fAARectRenderer->fillAARect(this->getGpu(), target,
778 devRect, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000779 }
780 return;
781 }
782
bsalomon@google.com27847de2011-02-22 20:59:41 +0000783 if (width >= 0) {
784 // TODO: consider making static vertex buffers for these cases.
785 // Hairline could be done by just adding closing vertex to
786 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000787
bsalomon@google.com27847de2011-02-22 20:59:41 +0000788 static const int worstCaseVertCount = 10;
bsalomon@google.come3d32162012-07-20 13:37:06 +0000789 GrDrawTarget::AutoReleaseGeometry geo(target, 0, worstCaseVertCount, 0);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000790
791 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000792 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000793 return;
794 }
795
796 GrPrimitiveType primType;
797 int vertCount;
798 GrPoint* vertex = geo.positions();
799
800 if (width > 0) {
801 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000802 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000803 setStrokeRectStrip(vertex, rect, width);
804 } else {
805 // hairline
806 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000807 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000808 vertex[0].set(rect.fLeft, rect.fTop);
809 vertex[1].set(rect.fRight, rect.fTop);
810 vertex[2].set(rect.fRight, rect.fBottom);
811 vertex[3].set(rect.fLeft, rect.fBottom);
812 vertex[4].set(rect.fLeft, rect.fTop);
813 }
814
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000815 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000816 if (NULL != matrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000817 GrDrawState* drawState = target->drawState();
818 avmr.set(drawState);
819 drawState->preConcatViewMatrix(*matrix);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000820 drawState->preConcatSamplerMatrices(*matrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000821 }
822
823 target->drawNonIndexed(primType, 0, vertCount);
824 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000825#if GR_STATIC_RECT_VB
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000826 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
827 if (NULL == sqVB) {
828 GrPrintf("Failed to create static rect vb.\n");
829 return;
830 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000831 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000832 GrDrawState* drawState = target->drawState();
833 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000834 GrMatrix m;
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000835 m.setAll(rect.width(), 0, rect.fLeft,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000836 0, rect.height(), rect.fTop,
837 0, 0, GrMatrix::I()[8]);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000838
839 if (NULL != matrix) {
840 m.postConcat(*matrix);
841 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000842 drawState->preConcatViewMatrix(m);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000843 drawState->preConcatSamplerMatrices(m);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000844
bsalomon@google.com47059542012-06-06 20:51:20 +0000845 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000846#else
bsalomon@google.come3d32162012-07-20 13:37:06 +0000847 target->drawSimpleRect(rect, matrix);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000848#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000849 }
850}
851
852void GrContext::drawRectToRect(const GrPaint& paint,
853 const GrRect& dstRect,
854 const GrRect& srcRect,
855 const GrMatrix* dstMatrix,
856 const GrMatrix* srcMatrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000857 SK_TRACE_EVENT0("GrContext::drawRectToRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000858
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000859 // srcRect refers to paint's first texture
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000860 if (!paint.isTextureStageEnabled(0)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000861 drawRect(paint, dstRect, -1, dstMatrix);
862 return;
863 }
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000864
bsalomon@google.com27847de2011-02-22 20:59:41 +0000865 GR_STATIC_ASSERT(!BATCH_RECT_TO_RECT || !GR_STATIC_RECT_VB);
866
867#if GR_STATIC_RECT_VB
868 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000869 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000870 GrDrawState* drawState = target->drawState();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000871 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000872
873 GrMatrix m;
874
875 m.setAll(dstRect.width(), 0, dstRect.fLeft,
876 0, dstRect.height(), dstRect.fTop,
877 0, 0, GrMatrix::I()[8]);
878 if (NULL != dstMatrix) {
879 m.postConcat(*dstMatrix);
880 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000881 drawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000882
bsalomon@google.come3d32162012-07-20 13:37:06 +0000883 // we explicitly setup the correct coords for the first stage. The others
884 // must know about the view matrix change.
885 for (int s = 1; s < GrPaint::kTotalStages; ++s) {
886 if (drawState->isStageEnabled(s)) {
887 drawState->sampler(s)->preConcatMatrix(m);
888 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000889 }
890
bsalomon@google.com27847de2011-02-22 20:59:41 +0000891 m.setAll(srcRect.width(), 0, srcRect.fLeft,
892 0, srcRect.height(), srcRect.fTop,
893 0, 0, GrMatrix::I()[8]);
894 if (NULL != srcMatrix) {
895 m.postConcat(*srcMatrix);
896 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000897 drawState->sampler(GrPaint::kFirstTextureStage)->preConcatMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000898
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000899 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
900 if (NULL == sqVB) {
901 GrPrintf("Failed to create static rect vb.\n");
902 return;
903 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000904 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com47059542012-06-06 20:51:20 +0000905 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000906#else
907
908 GrDrawTarget* target;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000909#if BATCH_RECT_TO_RECT
bsalomon@google.com27847de2011-02-22 20:59:41 +0000910 target = this->prepareToDraw(paint, kBuffered_DrawCategory);
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000911#else
bsalomon@google.com27847de2011-02-22 20:59:41 +0000912 target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
913#endif
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000914 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000915
tomhudson@google.com93813632011-10-27 20:21:16 +0000916 const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
917 const GrMatrix* srcMatrices[GrDrawState::kNumStages] = {NULL};
bsalomon@google.com27847de2011-02-22 20:59:41 +0000918 srcRects[0] = &srcRect;
919 srcMatrices[0] = srcMatrix;
920
bsalomon@google.come3d32162012-07-20 13:37:06 +0000921 target->drawRect(dstRect, dstMatrix, srcRects, srcMatrices);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000922#endif
923}
924
925void GrContext::drawVertices(const GrPaint& paint,
926 GrPrimitiveType primitiveType,
927 int vertexCount,
928 const GrPoint positions[],
929 const GrPoint texCoords[],
930 const GrColor colors[],
931 const uint16_t indices[],
932 int indexCount) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000933 SK_TRACE_EVENT0("GrContext::drawVertices");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000934
935 GrDrawTarget::AutoReleaseGeometry geo;
936
937 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000938 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000939
bsalomon@google.come3d32162012-07-20 13:37:06 +0000940 GrVertexLayout layout = 0;
941 if (NULL != texCoords) {
942 layout |= GrDrawTarget::StageTexCoordVertexLayoutBit(0, 0);
943 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000944 if (NULL != colors) {
945 layout |= GrDrawTarget::kColor_VertexLayoutBit;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000946 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000947 int vertexSize = GrDrawTarget::VertexSize(layout);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000948
949 if (sizeof(GrPoint) != vertexSize) {
950 if (!geo.set(target, layout, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000951 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000952 return;
953 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000954 int texOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000955 int colorOffset;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000956 GrDrawTarget::VertexSizeAndOffsetsByIdx(layout,
957 texOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000958 &colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000959 NULL,
960 NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000961 void* curVertex = geo.vertices();
962
963 for (int i = 0; i < vertexCount; ++i) {
964 *((GrPoint*)curVertex) = positions[i];
965
966 if (texOffsets[0] > 0) {
967 *(GrPoint*)((intptr_t)curVertex + texOffsets[0]) = texCoords[i];
968 }
969 if (colorOffset > 0) {
970 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
971 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000972 curVertex = (void*)((intptr_t)curVertex + vertexSize);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000973 }
974 } else {
975 target->setVertexSourceToArray(layout, positions, vertexCount);
976 }
977
bsalomon@google.com91958362011-06-13 17:58:13 +0000978 // we don't currently apply offscreen AA to this path. Need improved
979 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000980
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000981 if (NULL != indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000982 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000983 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000984 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000985 target->drawNonIndexed(primitiveType, 0, vertexCount);
986 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000987}
988
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000989///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com150d2842012-01-12 20:19:56 +0000990namespace {
991
bsalomon@google.com93c96602012-04-27 13:05:21 +0000992struct CircleVertex {
993 GrPoint fPos;
994 GrPoint fCenter;
995 GrScalar fOuterRadius;
996 GrScalar fInnerRadius;
997};
998
999/* Returns true if will map a circle to another circle. This can be true
1000 * if the matrix only includes square-scale, rotation, translation.
1001 */
1002inline bool isSimilarityTransformation(const SkMatrix& matrix,
1003 SkScalar tol = SK_ScalarNearlyZero) {
1004 if (matrix.isIdentity() || matrix.getType() == SkMatrix::kTranslate_Mask) {
1005 return true;
1006 }
1007 if (matrix.hasPerspective()) {
1008 return false;
1009 }
1010
1011 SkScalar mx = matrix.get(SkMatrix::kMScaleX);
1012 SkScalar sx = matrix.get(SkMatrix::kMSkewX);
1013 SkScalar my = matrix.get(SkMatrix::kMScaleY);
1014 SkScalar sy = matrix.get(SkMatrix::kMSkewY);
1015
1016 if (mx == 0 && sx == 0 && my == 0 && sy == 0) {
1017 return false;
1018 }
1019
1020 // it has scales or skews, but it could also be rotation, check it out.
1021 SkVector vec[2];
1022 vec[0].set(mx, sx);
1023 vec[1].set(sy, my);
1024
1025 return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) &&
1026 SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(),
1027 SkScalarSquare(tol));
1028}
1029
1030}
1031
1032// TODO: strokeWidth can't be larger than zero right now.
1033// It will be fixed when drawPath() can handle strokes.
1034void GrContext::drawOval(const GrPaint& paint,
1035 const GrRect& rect,
1036 SkScalar strokeWidth) {
bsalomon@google.com0982d352012-07-31 15:33:25 +00001037 GrAssert(strokeWidth <= 0);
1038 if (!isSimilarityTransformation(this->getMatrix()) ||
bsalomon@google.com93c96602012-04-27 13:05:21 +00001039 !paint.fAntiAlias ||
1040 rect.height() != rect.width()) {
1041 SkPath path;
1042 path.addOval(rect);
1043 GrPathFill fill = (strokeWidth == 0) ?
bsalomon@google.com0982d352012-07-31 15:33:25 +00001044 kHairLine_GrPathFill : kWinding_GrPathFill;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001045 this->internalDrawPath(paint, path, fill, NULL);
1046 return;
1047 }
1048
bsalomon@google.com0982d352012-07-31 15:33:25 +00001049 DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
1050 kUnbuffered_DrawCategory;
1051 GrDrawTarget* target = this->prepareToDraw(paint, category);
1052 GrDrawState* drawState = target->drawState();
1053 GrDrawState::AutoStageDisable atr(fDrawState);
1054 const GrMatrix vm = drawState->getViewMatrix();
1055
bsalomon@google.com93c96602012-04-27 13:05:21 +00001056 const GrRenderTarget* rt = drawState->getRenderTarget();
1057 if (NULL == rt) {
1058 return;
1059 }
1060
bsalomon@google.come3d32162012-07-20 13:37:06 +00001061 GrDrawTarget::AutoDeviceCoordDraw adcd(target);
1062 if (!adcd.succeeded()) {
1063 return;
1064 }
bsalomon@google.com93c96602012-04-27 13:05:21 +00001065
bsalomon@google.come3d32162012-07-20 13:37:06 +00001066 GrVertexLayout layout = GrDrawTarget::kEdge_VertexLayoutBit;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001067 GrAssert(sizeof(CircleVertex) == GrDrawTarget::VertexSize(layout));
1068
1069 GrPoint center = GrPoint::Make(rect.centerX(), rect.centerY());
1070 GrScalar radius = SkScalarHalf(rect.width());
1071
1072 vm.mapPoints(&center, 1);
1073 radius = vm.mapRadius(radius);
1074
1075 GrScalar outerRadius = radius;
1076 GrScalar innerRadius = 0;
1077 SkScalar halfWidth = 0;
1078 if (strokeWidth == 0) {
1079 halfWidth = SkScalarHalf(SK_Scalar1);
1080
1081 outerRadius += halfWidth;
1082 innerRadius = SkMaxScalar(0, radius - halfWidth);
1083 }
1084
1085 GrDrawTarget::AutoReleaseGeometry geo(target, layout, 4, 0);
1086 if (!geo.succeeded()) {
1087 GrPrintf("Failed to get space for vertices!\n");
1088 return;
1089 }
1090
1091 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
1092
robertphillips@google.coma0a66c12012-06-22 13:14:29 +00001093 // The fragment shader will extend the radius out half a pixel
1094 // to antialias. Expand the drawn rect here so all the pixels
1095 // will be captured.
1096 SkScalar L = center.fX - outerRadius - SkFloatToScalar(0.5f);
1097 SkScalar R = center.fX + outerRadius + SkFloatToScalar(0.5f);
1098 SkScalar T = center.fY - outerRadius - SkFloatToScalar(0.5f);
1099 SkScalar B = center.fY + outerRadius + SkFloatToScalar(0.5f);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001100
1101 verts[0].fPos = SkPoint::Make(L, T);
1102 verts[1].fPos = SkPoint::Make(R, T);
1103 verts[2].fPos = SkPoint::Make(L, B);
1104 verts[3].fPos = SkPoint::Make(R, B);
1105
1106 for (int i = 0; i < 4; ++i) {
1107 // this goes to fragment shader, it should be in y-points-up space.
1108 verts[i].fCenter = SkPoint::Make(center.fX, rt->height() - center.fY);
1109
1110 verts[i].fOuterRadius = outerRadius;
1111 verts[i].fInnerRadius = innerRadius;
1112 }
1113
1114 drawState->setVertexEdgeType(GrDrawState::kCircle_EdgeType);
bsalomon@google.com47059542012-06-06 20:51:20 +00001115 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001116}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001117
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001118void GrContext::drawPath(const GrPaint& paint, const SkPath& path,
reed@google.com07f3ee12011-05-16 17:21:57 +00001119 GrPathFill fill, const GrPoint* translate) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001120
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001121 if (path.isEmpty()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001122 if (GrIsFillInverted(fill)) {
1123 this->drawPaint(paint);
1124 }
1125 return;
1126 }
1127
bsalomon@google.com93c96602012-04-27 13:05:21 +00001128 SkRect ovalRect;
1129 if (!GrIsFillInverted(fill) && path.isOval(&ovalRect)) {
1130 if (translate) {
1131 ovalRect.offset(*translate);
1132 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001133 SkScalar width = (fill == kHairLine_GrPathFill) ? 0 : -SK_Scalar1;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001134 this->drawOval(paint, ovalRect, width);
1135 return;
1136 }
1137
1138 internalDrawPath(paint, path, fill, translate);
1139}
1140
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001141void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +00001142 GrPathFill fill, const GrPoint* translate) {
1143
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001144 // Note that below we may sw-rasterize the path into a scratch texture.
1145 // Scratch textures can be recycled after they are returned to the texture
1146 // cache. This presents a potential hazard for buffered drawing. However,
1147 // the writePixels that uploads to the scratch will perform a flush so we're
1148 // OK.
1149 DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
1150 kUnbuffered_DrawCategory;
1151 GrDrawTarget* target = this->prepareToDraw(paint, category);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +00001152 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001153
bsalomon@google.com289533a2011-10-27 12:34:25 +00001154 bool prAA = paint.fAntiAlias && !this->getRenderTarget()->isMultisampled();
1155
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001156 // An Assumption here is that path renderer would use some form of tweaking
1157 // the src color (either the input alpha or in the frag shader) to implement
1158 // aa. If we have some future driver-mojo path AA that can do the right
1159 // thing WRT to the blend then we'll need some query on the PR.
1160 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001161#if GR_DEBUG
bsalomon@google.com979432b2011-11-05 21:38:22 +00001162 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001163#endif
bsalomon@google.com289533a2011-10-27 12:34:25 +00001164 prAA = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001165 }
bsalomon@google.com289533a2011-10-27 12:34:25 +00001166
robertphillips@google.com72176b22012-05-23 13:19:12 +00001167 GrPathRenderer* pr = this->getPathRenderer(path, fill, target, prAA, true);
bsalomon@google.com30085192011-08-19 15:42:31 +00001168 if (NULL == pr) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001169#if GR_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001170 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001171#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001172 return;
1173 }
1174
bsalomon@google.come3d32162012-07-20 13:37:06 +00001175 pr->drawPath(path, fill, translate, target, prAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001176}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001177
bsalomon@google.com27847de2011-02-22 20:59:41 +00001178////////////////////////////////////////////////////////////////////////////////
1179
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001180void GrContext::flush(int flagsBitfield) {
1181 if (kDiscard_FlushBit & flagsBitfield) {
1182 fDrawBuffer->reset();
1183 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001184 this->flushDrawBuffer();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001185 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001186 if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001187 fGpu->forceRenderTargetFlush();
1188 }
1189}
1190
bsalomon@google.com27847de2011-02-22 20:59:41 +00001191void GrContext::flushDrawBuffer() {
junov@google.com53a55842011-06-08 22:55:10 +00001192 if (fDrawBuffer) {
robertphillips@google.com58b38182012-05-03 16:29:41 +00001193 // With addition of the AA clip path, flushing the draw buffer can
1194 // result in the generation of an AA clip mask. During this
1195 // process the SW path renderer may be invoked which recusively
1196 // calls this method (via internalWriteTexturePixels) creating
1197 // infinite recursion
1198 GrInOrderDrawBuffer* temp = fDrawBuffer;
1199 fDrawBuffer = NULL;
1200
1201 temp->flushTo(fGpu);
1202
1203 fDrawBuffer = temp;
junov@google.com53a55842011-06-08 22:55:10 +00001204 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001205}
1206
bsalomon@google.com6f379512011-11-16 20:36:03 +00001207void GrContext::internalWriteTexturePixels(GrTexture* texture,
1208 int left, int top,
1209 int width, int height,
1210 GrPixelConfig config,
1211 const void* buffer,
1212 size_t rowBytes,
1213 uint32_t flags) {
1214 SK_TRACE_EVENT0("GrContext::writeTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001215 ASSERT_OWNED_RESOURCE(texture);
1216
bsalomon@google.com6f379512011-11-16 20:36:03 +00001217 if (!(kDontFlush_PixelOpsFlag & flags)) {
1218 this->flush();
1219 }
1220 // TODO: use scratch texture to perform conversion
1221 if (GrPixelConfigIsUnpremultiplied(texture->config()) !=
1222 GrPixelConfigIsUnpremultiplied(config)) {
1223 return;
1224 }
1225
1226 fGpu->writeTexturePixels(texture, left, top, width, height,
1227 config, buffer, rowBytes);
1228}
1229
1230bool GrContext::internalReadTexturePixels(GrTexture* texture,
1231 int left, int top,
1232 int width, int height,
1233 GrPixelConfig config,
1234 void* buffer,
1235 size_t rowBytes,
1236 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001237 SK_TRACE_EVENT0("GrContext::readTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001238 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001239
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001240 // TODO: code read pixels for textures that aren't also rendertargets
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001241 GrRenderTarget* target = texture->asRenderTarget();
1242 if (NULL != target) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001243 return this->internalReadRenderTargetPixels(target,
1244 left, top, width, height,
1245 config, buffer, rowBytes,
1246 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001247 } else {
1248 return false;
1249 }
1250}
1251
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001252#include "SkConfig8888.h"
1253
1254namespace {
1255/**
1256 * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel
1257 * formats are representable as Config8888 and so the function returns false
1258 * if the GrPixelConfig has no equivalent Config8888.
1259 */
1260bool grconfig_to_config8888(GrPixelConfig config,
1261 SkCanvas::Config8888* config8888) {
1262 switch (config) {
1263 case kRGBA_8888_PM_GrPixelConfig:
1264 *config8888 = SkCanvas::kRGBA_Premul_Config8888;
1265 return true;
1266 case kRGBA_8888_UPM_GrPixelConfig:
1267 *config8888 = SkCanvas::kRGBA_Unpremul_Config8888;
1268 return true;
1269 case kBGRA_8888_PM_GrPixelConfig:
1270 *config8888 = SkCanvas::kBGRA_Premul_Config8888;
1271 return true;
1272 case kBGRA_8888_UPM_GrPixelConfig:
1273 *config8888 = SkCanvas::kBGRA_Unpremul_Config8888;
1274 return true;
1275 default:
1276 return false;
1277 }
1278}
1279}
1280
bsalomon@google.com6f379512011-11-16 20:36:03 +00001281bool GrContext::internalReadRenderTargetPixels(GrRenderTarget* target,
1282 int left, int top,
1283 int width, int height,
1284 GrPixelConfig config,
1285 void* buffer,
1286 size_t rowBytes,
1287 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001288 SK_TRACE_EVENT0("GrContext::readRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001289 ASSERT_OWNED_RESOURCE(target);
1290
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001291 if (NULL == target) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001292 target = fDrawState->getRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001293 if (NULL == target) {
1294 return false;
1295 }
1296 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001297
bsalomon@google.com6f379512011-11-16 20:36:03 +00001298 if (!(kDontFlush_PixelOpsFlag & flags)) {
1299 this->flush();
1300 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001301
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001302 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1303 GrPixelConfigIsUnpremultiplied(config) &&
1304 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1305 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1306 if (!grconfig_to_config8888(target->config(), &srcConfig8888) ||
1307 !grconfig_to_config8888(config, &dstConfig8888)) {
1308 return false;
1309 }
1310 // do read back using target's own config
1311 this->internalReadRenderTargetPixels(target,
1312 left, top,
1313 width, height,
1314 target->config(),
1315 buffer, rowBytes,
1316 kDontFlush_PixelOpsFlag);
1317 // sw convert the pixels to unpremul config
1318 uint32_t* pixels = reinterpret_cast<uint32_t*>(buffer);
1319 SkConvertConfig8888Pixels(pixels, rowBytes, dstConfig8888,
1320 pixels, rowBytes, srcConfig8888,
1321 width, height);
1322 return true;
1323 }
1324
bsalomon@google.comc4364992011-11-07 15:54:49 +00001325 GrTexture* src = target->asTexture();
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001326 bool swapRAndB = NULL != src &&
1327 fGpu->preferredReadPixelsConfig(config) ==
1328 GrPixelConfigSwapRAndB(config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001329
1330 bool flipY = NULL != src &&
1331 fGpu->readPixelsWillPayForYFlip(target, left, top,
1332 width, height, config,
1333 rowBytes);
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001334 bool alphaConversion = (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1335 GrPixelConfigIsUnpremultiplied(config));
bsalomon@google.comc4364992011-11-07 15:54:49 +00001336
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001337 if (NULL == src && alphaConversion) {
1338 // we should fallback to cpu conversion here. This could happen when
1339 // we were given an external render target by the client that is not
1340 // also a texture (e.g. FBO 0 in GL)
1341 return false;
1342 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001343 // we draw to a scratch texture if any of these conversion are applied
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001344 GrAutoScratchTexture ast;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001345 if (flipY || swapRAndB || alphaConversion) {
1346 GrAssert(NULL != src);
1347 if (swapRAndB) {
1348 config = GrPixelConfigSwapRAndB(config);
1349 GrAssert(kUnknown_GrPixelConfig != config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001350 }
1351 // Make the scratch a render target because we don't have a robust
1352 // readTexturePixels as of yet (it calls this function).
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001353 GrTextureDesc desc;
1354 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1355 desc.fWidth = width;
1356 desc.fHeight = height;
1357 desc.fConfig = config;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001358
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001359 // When a full readback is faster than a partial we could always make
1360 // the scratch exactly match the passed rect. However, if we see many
1361 // different size rectangles we will trash our texture cache and pay the
1362 // cost of creating and destroying many textures. So, we only request
1363 // an exact match when the caller is reading an entire RT.
1364 ScratchTexMatch match = kApprox_ScratchTexMatch;
1365 if (0 == left &&
1366 0 == top &&
1367 target->width() == width &&
1368 target->height() == height &&
1369 fGpu->fullReadPixelsIsFasterThanPartial()) {
1370 match = kExact_ScratchTexMatch;
1371 }
1372 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001373 GrTexture* texture = ast.texture();
1374 if (!texture) {
1375 return false;
1376 }
1377 target = texture->asRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001378 GrAssert(NULL != target);
1379
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001380 GrDrawTarget::AutoStateRestore asr(fGpu,
1381 GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001382 GrDrawState* drawState = fGpu->drawState();
1383 drawState->setRenderTarget(target);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001384
bsalomon@google.comc4364992011-11-07 15:54:49 +00001385 GrMatrix matrix;
1386 if (flipY) {
1387 matrix.setTranslate(SK_Scalar1 * left,
1388 SK_Scalar1 * (top + height));
1389 matrix.set(GrMatrix::kMScaleY, -GR_Scalar1);
1390 } else {
1391 matrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
1392 }
1393 matrix.postIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001394 drawState->sampler(0)->reset(matrix);
1395 drawState->sampler(0)->setRAndBSwap(swapRAndB);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001396 drawState->createTextureEffect(0, src);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001397 GrRect rect;
1398 rect.setXYWH(0, 0, SK_Scalar1 * width, SK_Scalar1 * height);
bsalomon@google.come3d32162012-07-20 13:37:06 +00001399 fGpu->drawSimpleRect(rect, NULL);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001400 left = 0;
1401 top = 0;
1402 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001403 return fGpu->readPixels(target,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001404 left, top, width, height,
1405 config, buffer, rowBytes, flipY);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001406}
1407
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001408void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1409 GrAssert(target);
1410 ASSERT_OWNED_RESOURCE(target);
1411 // In the future we may track whether there are any pending draws to this
1412 // target. We don't today so we always perform a flush. We don't promise
1413 // this to our clients, though.
1414 this->flush();
1415 fGpu->resolveRenderTarget(target);
1416}
1417
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001418void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst) {
1419 if (NULL == src || NULL == dst) {
1420 return;
1421 }
1422 ASSERT_OWNED_RESOURCE(src);
1423
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001424 // Writes pending to the source texture are not tracked, so a flush
1425 // is required to ensure that the copy captures the most recent contents
1426 // of the source texture. See similar behaviour in
1427 // GrContext::resolveRenderTarget.
1428 this->flush();
1429
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001430 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001431 GrDrawState* drawState = fGpu->drawState();
1432 drawState->setRenderTarget(dst);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001433 GrMatrix sampleM;
1434 sampleM.setIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001435 drawState->sampler(0)->reset(sampleM);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001436 drawState->createTextureEffect(0, src);
bsalomon@google.com5db3b6c2012-01-12 20:38:57 +00001437 SkRect rect = SkRect::MakeXYWH(0, 0,
1438 SK_Scalar1 * src->width(),
1439 SK_Scalar1 * src->height());
bsalomon@google.come3d32162012-07-20 13:37:06 +00001440 fGpu->drawSimpleRect(rect, NULL);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001441}
1442
bsalomon@google.com6f379512011-11-16 20:36:03 +00001443void GrContext::internalWriteRenderTargetPixels(GrRenderTarget* target,
1444 int left, int top,
1445 int width, int height,
1446 GrPixelConfig config,
1447 const void* buffer,
1448 size_t rowBytes,
1449 uint32_t flags) {
1450 SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001451 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001452
1453 if (NULL == target) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001454 target = fDrawState->getRenderTarget();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001455 if (NULL == target) {
1456 return;
1457 }
1458 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001459
1460 // TODO: when underlying api has a direct way to do this we should use it
1461 // (e.g. glDrawPixels on desktop GL).
1462
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001463 // If the RT is also a texture and we don't have to do PM/UPM conversion
1464 // then take the texture path, which we expect to be at least as fast or
1465 // faster since it doesn't use an intermediate texture as we do below.
1466
1467#if !GR_MAC_BUILD
1468 // At least some drivers on the Mac get confused when glTexImage2D is called
1469 // on a texture attached to an FBO. The FBO still sees the old image. TODO:
1470 // determine what OS versions and/or HW is affected.
1471 if (NULL != target->asTexture() &&
1472 GrPixelConfigIsUnpremultiplied(target->config()) ==
1473 GrPixelConfigIsUnpremultiplied(config)) {
1474
1475 this->internalWriteTexturePixels(target->asTexture(),
1476 left, top, width, height,
1477 config, buffer, rowBytes, flags);
1478 return;
1479 }
1480#endif
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001481 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1482 GrPixelConfigIsUnpremultiplied(config) &&
1483 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1484 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1485 if (!grconfig_to_config8888(config, &srcConfig8888) ||
1486 !grconfig_to_config8888(target->config(), &dstConfig8888)) {
1487 return;
1488 }
1489 // allocate a tmp buffer and sw convert the pixels to premul
1490 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(width * height);
1491 const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer);
1492 SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888,
1493 src, rowBytes, srcConfig8888,
1494 width, height);
1495 // upload the already premul pixels
1496 this->internalWriteRenderTargetPixels(target,
1497 left, top,
1498 width, height,
1499 target->config(),
1500 tmpPixels, 4 * width, flags);
1501 return;
1502 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001503
1504 bool swapRAndB = fGpu->preferredReadPixelsConfig(config) ==
1505 GrPixelConfigSwapRAndB(config);
1506 if (swapRAndB) {
1507 config = GrPixelConfigSwapRAndB(config);
1508 }
1509
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001510 GrTextureDesc desc;
1511 desc.fWidth = width;
1512 desc.fHeight = height;
1513 desc.fConfig = config;
1514
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001515 GrAutoScratchTexture ast(this, desc);
1516 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001517 if (NULL == texture) {
1518 return;
1519 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001520 this->internalWriteTexturePixels(texture, 0, 0, width, height,
1521 config, buffer, rowBytes, flags);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001522
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001523 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001524 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001525
1526 GrMatrix matrix;
1527 matrix.setTranslate(GrIntToScalar(left), GrIntToScalar(top));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001528 drawState->setViewMatrix(matrix);
1529 drawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001530
bsalomon@google.com5c638652011-07-18 19:31:59 +00001531 matrix.setIDiv(texture->width(), texture->height());
bsalomon@google.comb8670992012-07-25 21:27:09 +00001532 drawState->sampler(0)->reset(matrix);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001533 drawState->createTextureEffect(0, texture);
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001534 drawState->sampler(0)->setRAndBSwap(swapRAndB);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001535
tomhudson@google.comb213ed82012-06-25 15:22:12 +00001536 static const GrVertexLayout layout = 0;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001537 static const int VCOUNT = 4;
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001538 // TODO: Use GrGpu::drawRect here
bsalomon@google.com27847de2011-02-22 20:59:41 +00001539 GrDrawTarget::AutoReleaseGeometry geo(fGpu, layout, VCOUNT, 0);
1540 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001541 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +00001542 return;
1543 }
1544 ((GrPoint*)geo.vertices())->setIRectFan(0, 0, width, height);
bsalomon@google.com47059542012-06-06 20:51:20 +00001545 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, VCOUNT);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001546}
1547////////////////////////////////////////////////////////////////////////////////
1548
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001549void GrContext::setPaint(const GrPaint& paint) {
tomhudson@google.comcb325ce2012-07-11 14:41:19 +00001550 GrAssert(fDrawState->stagesDisabled());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001551
1552 for (int i = 0; i < GrPaint::kMaxTextures; ++i) {
1553 int s = i + GrPaint::kFirstTextureStage;
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001554 if (paint.isTextureStageEnabled(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001555 *fDrawState->sampler(s) = paint.getTextureSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001556 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001557 }
1558
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001559 fDrawState->setFirstCoverageStage(GrPaint::kFirstMaskStage);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001560
1561 for (int i = 0; i < GrPaint::kMaxMasks; ++i) {
1562 int s = i + GrPaint::kFirstMaskStage;
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001563 if (paint.isMaskStageEnabled(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001564 *fDrawState->sampler(s) = paint.getMaskSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001565 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001566 }
bsalomon@google.com26936d02012-03-19 13:06:19 +00001567
1568 // disable all stages not accessible via the paint
1569 for (int s = GrPaint::kTotalStages; s < GrDrawState::kNumStages; ++s) {
tomhudson@google.com676e6602012-07-10 17:21:48 +00001570 fDrawState->disableStage(s);
bsalomon@google.com26936d02012-03-19 13:06:19 +00001571 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001572
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001573 fDrawState->setColor(paint.fColor);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001574
1575 if (paint.fDither) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001576 fDrawState->enableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001577 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001578 fDrawState->disableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001579 }
1580 if (paint.fAntiAlias) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001581 fDrawState->enableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001582 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001583 fDrawState->disableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001584 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001585 if (paint.fColorMatrixEnabled) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001586 fDrawState->enableState(GrDrawState::kColorMatrix_StateBit);
1587 fDrawState->setColorMatrix(paint.fColorMatrix);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001588 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001589 fDrawState->disableState(GrDrawState::kColorMatrix_StateBit);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001590 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001591 fDrawState->setBlendFunc(paint.fSrcBlendCoeff, paint.fDstBlendCoeff);
1592 fDrawState->setColorFilter(paint.fColorFilterColor, paint.fColorFilterXfermode);
1593 fDrawState->setCoverage(paint.fCoverage);
reed@google.com4b2d3f32012-05-15 18:05:50 +00001594#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
bsalomon@google.come3d32162012-07-20 13:37:06 +00001595 if ((paint.hasMask() || 0xff != paint.fCoverage) &&
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001596 !fGpu->canApplyCoverage()) {
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001597 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1598 }
bsalomon@google.com95cd7bd2012-03-28 15:35:05 +00001599#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001600}
1601
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001602GrDrawTarget* GrContext::prepareToDraw(const GrPaint& paint,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001603 DrawCategory category) {
1604 if (category != fLastDrawCategory) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001605 this->flushDrawBuffer();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001606 fLastDrawCategory = category;
1607 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001608 this->setPaint(paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001609 GrDrawTarget* target = fGpu;
1610 switch (category) {
bsalomon@google.com193395c2012-03-30 17:35:12 +00001611 case kUnbuffered_DrawCategory:
1612 target = fGpu;
1613 break;
1614 case kBuffered_DrawCategory:
1615 target = fDrawBuffer;
1616 fDrawBuffer->setClip(fGpu->getClip());
1617 break;
1618 default:
1619 GrCrash("Unexpected DrawCategory.");
1620 break;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001621 }
1622 return target;
1623}
1624
robertphillips@google.com72176b22012-05-23 13:19:12 +00001625/*
1626 * This method finds a path renderer that can draw the specified path on
1627 * the provided target.
1628 * Due to its expense, the software path renderer has split out so it can
1629 * can be individually allowed/disallowed via the "allowSW" boolean.
1630 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001631GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
bsalomon@google.com289533a2011-10-27 12:34:25 +00001632 GrPathFill fill,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001633 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001634 bool antiAlias,
1635 bool allowSW) {
bsalomon@google.com30085192011-08-19 15:42:31 +00001636 if (NULL == fPathRendererChain) {
1637 fPathRendererChain =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001638 SkNEW_ARGS(GrPathRendererChain,
1639 (this, GrPathRendererChain::kNone_UsageFlag));
bsalomon@google.com30085192011-08-19 15:42:31 +00001640 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001641
1642 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path, fill,
1643 target,
1644 antiAlias);
1645
1646 if (NULL == pr && allowSW) {
1647 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001648 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001649 }
1650
1651 pr = fSoftwarePathRenderer;
1652 }
1653
1654 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001655}
1656
bsalomon@google.com27847de2011-02-22 20:59:41 +00001657////////////////////////////////////////////////////////////////////////////////
1658
bsalomon@google.com27847de2011-02-22 20:59:41 +00001659void GrContext::setRenderTarget(GrRenderTarget* target) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001660 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001661 if (fDrawState->getRenderTarget() != target) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001662 this->flush(false);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001663 fDrawState->setRenderTarget(target);
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001664 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001665}
1666
1667GrRenderTarget* GrContext::getRenderTarget() {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001668 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001669}
1670
1671const GrRenderTarget* GrContext::getRenderTarget() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001672 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001673}
1674
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001675bool GrContext::isConfigRenderable(GrPixelConfig config) const {
1676 return fGpu->isConfigRenderable(config);
1677}
1678
bsalomon@google.com27847de2011-02-22 20:59:41 +00001679const GrMatrix& GrContext::getMatrix() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001680 return fDrawState->getViewMatrix();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001681}
1682
1683void GrContext::setMatrix(const GrMatrix& m) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001684 fDrawState->setViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001685}
1686
1687void GrContext::concatMatrix(const GrMatrix& m) const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001688 fDrawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001689}
1690
1691static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
1692 intptr_t mask = 1 << shift;
1693 if (pred) {
1694 bits |= mask;
1695 } else {
1696 bits &= ~mask;
1697 }
1698 return bits;
1699}
1700
bsalomon@google.com583a1e32011-08-17 13:42:46 +00001701GrContext::GrContext(GrGpu* gpu) {
bsalomon@google.comc0af3172012-06-15 14:10:09 +00001702 ++THREAD_INSTANCE_COUNT;
1703
bsalomon@google.com27847de2011-02-22 20:59:41 +00001704 fGpu = gpu;
1705 fGpu->ref();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001706 fGpu->setContext(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001707
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001708 fDrawState = SkNEW(GrDrawState);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001709 fGpu->setDrawState(fDrawState);
1710
bsalomon@google.com30085192011-08-19 15:42:31 +00001711 fPathRendererChain = NULL;
robertphillips@google.com72176b22012-05-23 13:19:12 +00001712 fSoftwarePathRenderer = NULL;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001713
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001714 fTextureCache = SkNEW_ARGS(GrResourceCache,
1715 (MAX_TEXTURE_CACHE_COUNT,
1716 MAX_TEXTURE_CACHE_BYTES));
1717 fFontCache = SkNEW_ARGS(GrFontCache, (fGpu));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001718
1719 fLastDrawCategory = kUnbuffered_DrawCategory;
1720
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001721 fDrawBuffer = NULL;
1722 fDrawBufferVBAllocPool = NULL;
1723 fDrawBufferIBAllocPool = NULL;
1724
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001725 fAARectRenderer = SkNEW(GrAARectRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001726
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001727 this->setupDrawBuffer();
1728}
1729
1730void GrContext::setupDrawBuffer() {
1731
1732 GrAssert(NULL == fDrawBuffer);
1733 GrAssert(NULL == fDrawBufferVBAllocPool);
1734 GrAssert(NULL == fDrawBufferIBAllocPool);
1735
bsalomon@google.com92edd312012-04-04 21:40:21 +00001736#if DEFER_TEXT_RENDERING || BATCH_RECT_TO_RECT || DEFER_PATHS
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001737 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001738 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001739 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001740 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001741 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001742 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001743 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001744 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001745
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001746 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com471d4712011-08-23 15:45:25 +00001747 fDrawBufferVBAllocPool,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001748 fDrawBufferIBAllocPool));
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001749#endif
1750
1751#if BATCH_RECT_TO_RECT
bsalomon@google.com27847de2011-02-22 20:59:41 +00001752 fDrawBuffer->setQuadIndexBuffer(this->getQuadIndexBuffer());
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001753#endif
bsalomon@google.com1015e032012-06-25 18:41:04 +00001754 if (fDrawBuffer) {
1755 fDrawBuffer->setAutoFlushTarget(fGpu);
1756 fDrawBuffer->setDrawState(fDrawState);
1757 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001758}
1759
bsalomon@google.com27847de2011-02-22 20:59:41 +00001760GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001761#if DEFER_TEXT_RENDERING
bsalomon@google.com193395c2012-03-30 17:35:12 +00001762 return prepareToDraw(paint, kBuffered_DrawCategory);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001763#else
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001764 return prepareToDraw(paint, kUnbuffered_DrawCategory);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001765#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001766}
1767
1768const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1769 return fGpu->getQuadIndexBuffer();
1770}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001771
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001772GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture,
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001773 bool canClobberSrc,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001774 const SkRect& rect,
1775 float sigmaX, float sigmaY) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001776 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001777 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001778 AutoMatrix avm(this, GrMatrix::I());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001779 SkIRect clearRect;
bsalomon@google.comb505a122012-05-31 18:40:36 +00001780 int scaleFactorX, radiusX;
1781 int scaleFactorY, radiusY;
1782 sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX);
1783 sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY);
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001784
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001785 SkRect srcRect(rect);
1786 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
1787 srcRect.roundOut();
robertphillips@google.com8637a362012-04-10 18:32:35 +00001788 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
1789 static_cast<float>(scaleFactorY));
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001790
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001791 AutoClip acs(this, srcRect);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001792
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001793 GrAssert(kBGRA_8888_PM_GrPixelConfig == srcTexture->config() ||
1794 kRGBA_8888_PM_GrPixelConfig == srcTexture->config() ||
1795 kAlpha_8_GrPixelConfig == srcTexture->config());
1796
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001797 GrTextureDesc desc;
1798 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1799 desc.fWidth = SkScalarFloorToInt(srcRect.width());
1800 desc.fHeight = SkScalarFloorToInt(srcRect.height());
1801 desc.fConfig = srcTexture->config();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001802
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001803 GrAutoScratchTexture temp1, temp2;
1804 GrTexture* dstTexture = temp1.set(this, desc);
1805 GrTexture* tempTexture = canClobberSrc ? srcTexture : temp2.set(this, desc);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001806
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001807 GrPaint paint;
1808 paint.reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +00001809 paint.textureSampler(0)->textureParams()->setBilerp(true);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001810
1811 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
1812 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1813 srcTexture->height());
1814 this->setRenderTarget(dstTexture->asRenderTarget());
1815 SkRect dstRect(srcRect);
1816 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
1817 i < scaleFactorY ? 0.5f : 1.0f);
tomhudson@google.comaa72eab2012-07-19 18:01:07 +00001818 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
1819 (srcTexture)))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001820 this->drawRectToRect(paint, dstRect, srcRect);
1821 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001822 srcTexture = dstTexture;
1823 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001824 }
1825
robertphillips@google.com7a396332012-05-10 15:11:27 +00001826 SkIRect srcIRect;
1827 srcRect.roundOut(&srcIRect);
1828
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001829 if (sigmaX > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001830 if (scaleFactorX > 1) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001831 // Clear out a radius to the right of the srcRect to prevent the
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001832 // X convolution from reading garbage.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001833 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001834 radiusX, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001835 this->clear(&clearRect, 0x0);
1836 }
1837
1838 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001839 convolve_gaussian(fGpu, srcTexture, srcRect, sigmaX, radiusX,
1840 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001841 srcTexture = dstTexture;
1842 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001843 }
1844
1845 if (sigmaY > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001846 if (scaleFactorY > 1 || sigmaX > 0.0f) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001847 // Clear out a radius below the srcRect to prevent the Y
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001848 // convolution from reading garbage.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001849 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001850 srcIRect.width(), radiusY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001851 this->clear(&clearRect, 0x0);
1852 }
1853
1854 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001855 convolve_gaussian(fGpu, srcTexture, srcRect, sigmaY, radiusY,
1856 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001857 srcTexture = dstTexture;
1858 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001859 }
1860
1861 if (scaleFactorX > 1 || scaleFactorY > 1) {
1862 // Clear one pixel to the right and below, to accommodate bilinear
1863 // upsampling.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001864 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
1865 srcIRect.width() + 1, 1);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001866 this->clear(&clearRect, 0x0);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001867 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
1868 1, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001869 this->clear(&clearRect, 0x0);
1870 // FIXME: This should be mitchell, not bilinear.
bsalomon@google.comb8670992012-07-25 21:27:09 +00001871 paint.textureSampler(0)->textureParams()->setBilerp(true);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001872 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1873 srcTexture->height());
1874 this->setRenderTarget(dstTexture->asRenderTarget());
tomhudson@google.comaa72eab2012-07-19 18:01:07 +00001875 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
1876 (srcTexture)))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001877 SkRect dstRect(srcRect);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001878 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001879 this->drawRectToRect(paint, dstRect, srcRect);
1880 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001881 srcTexture = dstTexture;
1882 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001883 }
1884 this->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001885 if (srcTexture == temp1.texture()) {
1886 return temp1.detach();
1887 } else if (srcTexture == temp2.texture()) {
1888 return temp2.detach();
1889 } else {
1890 srcTexture->ref();
1891 return srcTexture;
1892 }
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001893}
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001894
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001895GrTexture* GrContext::applyMorphology(GrTexture* srcTexture,
1896 const GrRect& rect,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001897 MorphologyType morphType,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001898 SkISize radius) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001899 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001900 srcTexture->ref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001901 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001902
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001903 AutoMatrix avm(this, GrMatrix::I());
1904
1905 AutoClip acs(this, GrRect::MakeWH(SkIntToScalar(srcTexture->width()),
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001906 SkIntToScalar(srcTexture->height())));
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001907 GrTextureDesc desc;
1908 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1909 desc.fWidth = SkScalarCeilToInt(rect.width());
1910 desc.fHeight = SkScalarCeilToInt(rect.height());
1911 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001912 if (radius.fWidth > 0) {
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001913 GrAutoScratchTexture ast(this, desc);
1914 this->setRenderTarget(ast.texture()->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001915 apply_morphology(fGpu, srcTexture, rect, radius.fWidth, morphType,
1916 Gr1DKernelEffect::kX_Direction);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001917 SkIRect clearRect = SkIRect::MakeXYWH(
1918 SkScalarFloorToInt(rect.fLeft),
1919 SkScalarFloorToInt(rect.fBottom),
1920 SkScalarFloorToInt(rect.width()),
1921 radius.fHeight);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001922 this->clear(&clearRect, 0x0);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001923 srcTexture->unref();
1924 srcTexture = ast.detach();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001925 }
1926 if (radius.fHeight > 0) {
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001927 GrAutoScratchTexture ast(this, desc);
1928 this->setRenderTarget(ast.texture()->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001929 apply_morphology(fGpu, srcTexture, rect, radius.fHeight, morphType,
1930 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001931 srcTexture->unref();
1932 srcTexture = ast.detach();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001933 }
1934 this->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001935 return srcTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001936}
bsalomon@google.comc4364992011-11-07 15:54:49 +00001937
1938///////////////////////////////////////////////////////////////////////////////