blob: 3cc2c89bfb30eb118c11b95466a2cfac1361fe06 [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"
14
tomhudson@google.com278cbb42011-06-30 19:37:01 +000015#include "GrBufferAllocPool.h"
16#include "GrClipIterator.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"
tomhudson@google.com0c8d93a2011-07-01 17:08:26 +000027#include "SkTrace.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000028
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000029#define DEFER_TEXT_RENDERING 1
bsalomon@google.com27847de2011-02-22 20:59:41 +000030
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000031#define DEFER_PATHS 1
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +000032
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000033#define BATCH_RECT_TO_RECT (1 && !GR_STATIC_RECT_VB)
bsalomon@google.com27847de2011-02-22 20:59:41 +000034
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +000035#define MAX_BLUR_SIGMA 4.0f
36
bsalomon@google.comd46e2422011-09-23 17:40:07 +000037// When we're using coverage AA but the blend is incompatible (given gpu
38// limitations) should we disable AA or draw wrong?
bsalomon@google.com950d7a82011-09-28 15:05:33 +000039#define DISABLE_COVERAGE_AA_FOR_BLEND 1
bsalomon@google.comd46e2422011-09-23 17:40:07 +000040
reed@google.com4b2d3f32012-05-15 18:05:50 +000041#if GR_DEBUG
42 // change this to a 1 to see notifications when partial coverage fails
43 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
44#else
45 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
46#endif
47
bsalomon@google.com8ccaddd2011-08-09 16:49:03 +000048static const size_t MAX_TEXTURE_CACHE_COUNT = 256;
49static const size_t MAX_TEXTURE_CACHE_BYTES = 16 * 1024 * 1024;
bsalomon@google.com27847de2011-02-22 20:59:41 +000050
bsalomon@google.com60361492012-03-15 17:47:06 +000051static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
bsalomon@google.com27847de2011-02-22 20:59:41 +000052static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
53
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +000054// path rendering is the only thing we defer today that uses non-static indices
55static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = DEFER_PATHS ? 1 << 11 : 0;
56static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = DEFER_PATHS ? 4 : 0;
bsalomon@google.com27847de2011-02-22 20:59:41 +000057
bsalomon@google.combc4b6542011-11-19 13:56:11 +000058#define ASSERT_OWNED_RESOURCE(R) GrAssert(!(R) || (R)->getContext() == this)
59
bsalomon@google.com05ef5102011-05-02 21:14:59 +000060GrContext* GrContext::Create(GrEngine engine,
61 GrPlatform3DContext context3D) {
bsalomon@google.com27847de2011-02-22 20:59:41 +000062 GrContext* ctx = NULL;
63 GrGpu* fGpu = GrGpu::Create(engine, context3D);
64 if (NULL != fGpu) {
65 ctx = new GrContext(fGpu);
66 fGpu->unref();
67 }
68 return ctx;
69}
70
bsalomon@google.com27847de2011-02-22 20:59:41 +000071GrContext::~GrContext() {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000072 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +000073
74 // Since the gpu can hold scratch textures, give it a chance to let go
75 // of them before freeing the texture cache
76 fGpu->purgeResources();
77
bsalomon@google.com27847de2011-02-22 20:59:41 +000078 delete fTextureCache;
79 delete fFontCache;
80 delete fDrawBuffer;
81 delete fDrawBufferVBAllocPool;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +000082 delete fDrawBufferIBAllocPool;
bsalomon@google.com30085192011-08-19 15:42:31 +000083
robertphillips@google.comf6747b02012-06-12 00:32:28 +000084 fAARectRenderer->unref();
85
bsalomon@google.com205d4602011-04-25 12:43:45 +000086 fGpu->unref();
bsalomon@google.com30085192011-08-19 15:42:31 +000087 GrSafeUnref(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +000088 GrSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +000089 fDrawState->unref();
bsalomon@google.com27847de2011-02-22 20:59:41 +000090}
91
bsalomon@google.com8fe72472011-03-30 21:26:44 +000092void GrContext::contextLost() {
junov@google.com53a55842011-06-08 22:55:10 +000093 contextDestroyed();
94 this->setupDrawBuffer();
95}
96
97void GrContext::contextDestroyed() {
bsalomon@google.com205d4602011-04-25 12:43:45 +000098 // abandon first to so destructors
99 // don't try to free the resources in the API.
100 fGpu->abandonResources();
101
bsalomon@google.com30085192011-08-19 15:42:31 +0000102 // a path renderer may be holding onto resources that
103 // are now unusable
104 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000105 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000106
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000107 delete fDrawBuffer;
108 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000109
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000110 delete fDrawBufferVBAllocPool;
111 fDrawBufferVBAllocPool = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000112
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000113 delete fDrawBufferIBAllocPool;
114 fDrawBufferIBAllocPool = NULL;
115
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000116 fAARectRenderer->reset();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000117
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000118 fTextureCache->removeAll();
119 fFontCache->freeAll();
120 fGpu->markContextDirty();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000121}
122
123void GrContext::resetContext() {
124 fGpu->markContextDirty();
125}
126
127void GrContext::freeGpuResources() {
128 this->flush();
robertphillips@google.comff175842012-05-14 19:31:39 +0000129
130 fGpu->purgeResources();
131
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000132 fAARectRenderer->reset();
133
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000134 fTextureCache->removeAll();
135 fFontCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000136 // a path renderer may be holding onto resources
137 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000138 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000139}
140
twiz@google.com05e70242012-01-27 19:12:00 +0000141size_t GrContext::getGpuTextureCacheBytes() const {
142 return fTextureCache->getCachedResourceBytes();
143}
144
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000145////////////////////////////////////////////////////////////////////////////////
146
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000147int GrContext::PaintStageVertexLayoutBits(
148 const GrPaint& paint,
149 const bool hasTexCoords[GrPaint::kTotalStages]) {
150 int stageMask = paint.getActiveStageMask();
151 int layout = 0;
152 for (int i = 0; i < GrPaint::kTotalStages; ++i) {
153 if ((1 << i) & stageMask) {
154 if (NULL != hasTexCoords && hasTexCoords[i]) {
155 layout |= GrDrawTarget::StageTexCoordVertexLayoutBit(i, i);
156 } else {
157 layout |= GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(i);
158 }
159 }
160 }
161 return layout;
162}
163
164
165////////////////////////////////////////////////////////////////////////////////
166
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000167GrTexture* GrContext::TextureCacheEntry::texture() const {
168 if (NULL == fEntry) {
169 return NULL;
170 } else {
171 return (GrTexture*) fEntry->resource();
172 }
173}
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000174
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000175namespace {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000176
177// we should never have more than one stencil buffer with same combo of
178// (width,height,samplecount)
179void gen_stencil_key_values(int width, int height,
180 int sampleCnt, uint32_t v[4]) {
181 v[0] = width;
182 v[1] = height;
183 v[2] = sampleCnt;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000184 v[3] = GrResourceKey::kStencilBuffer_TypeBit;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000185}
186
187void gen_stencil_key_values(const GrStencilBuffer* sb,
188 uint32_t v[4]) {
189 gen_stencil_key_values(sb->width(), sb->height(),
190 sb->numSamples(), v);
191}
bsalomon@google.com82c7bd82011-11-09 15:32:29 +0000192
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000193void scale_rect(SkRect* rect, float xScale, float yScale) {
robertphillips@google.com5af56062012-04-27 15:39:52 +0000194 rect->fLeft = SkScalarMul(rect->fLeft, SkFloatToScalar(xScale));
195 rect->fTop = SkScalarMul(rect->fTop, SkFloatToScalar(yScale));
196 rect->fRight = SkScalarMul(rect->fRight, SkFloatToScalar(xScale));
197 rect->fBottom = SkScalarMul(rect->fBottom, SkFloatToScalar(yScale));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000198}
199
bsalomon@google.comb505a122012-05-31 18:40:36 +0000200float adjust_sigma(float sigma, int *scaleFactor, int *radius) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000201 *scaleFactor = 1;
202 while (sigma > MAX_BLUR_SIGMA) {
203 *scaleFactor *= 2;
204 sigma *= 0.5f;
205 }
bsalomon@google.comb505a122012-05-31 18:40:36 +0000206 *radius = static_cast<int>(ceilf(sigma * 3.0f));
207 GrAssert(*radius <= GrConvolutionEffect::kMaxKernelRadius);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000208 return sigma;
209}
210
211void apply_morphology(GrGpu* gpu,
212 GrTexture* texture,
213 const SkRect& rect,
214 int radius,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000215 GrContext::MorphologyType morphType,
216 Gr1DKernelEffect::Direction direction) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000217
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000218 GrRenderTarget* target = gpu->drawState()->getRenderTarget();
219 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000220 GrDrawState* drawState = gpu->drawState();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000221 drawState->setRenderTarget(target);
222 GrMatrix sampleM;
223 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000224 drawState->sampler(0)->reset(sampleM);
225 SkAutoTUnref<GrCustomStage> morph(
226 new GrMorphologyEffect(direction, radius, morphType));
227 drawState->sampler(0)->setCustomStage(morph);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000228 drawState->setTexture(0, texture);
229 gpu->drawSimpleRect(rect, NULL, 1 << 0);
230}
231
bsalomon@google.comb505a122012-05-31 18:40:36 +0000232void convolve_gaussian(GrGpu* gpu,
233 GrTexture* texture,
234 const SkRect& rect,
235 float sigma,
236 int radius,
237 Gr1DKernelEffect::Direction direction) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000238 GrRenderTarget* target = gpu->drawState()->getRenderTarget();
239 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000240 GrDrawState* drawState = gpu->drawState();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000241 drawState->setRenderTarget(target);
242 GrMatrix sampleM;
243 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000244 drawState->sampler(0)->reset(sampleM);
245 SkAutoTUnref<GrConvolutionEffect> conv(new
246 GrConvolutionEffect(direction, radius));
247 conv->setGaussianKernel(sigma);
248 drawState->sampler(0)->setCustomStage(conv);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000249 drawState->setTexture(0, texture);
250 gpu->drawSimpleRect(rect, NULL, 1 << 0);
251}
252
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000253}
254
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000255GrContext::TextureCacheEntry GrContext::findAndLockTexture(
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000256 const GrTextureDesc& desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000257 const GrSamplerState* sampler) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000258 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler, 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.com1fadb202011-12-12 16:10:08 +0000264 const GrSamplerState* sampler) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000265 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler, 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.com1fadb202011-12-12 16:10:08 +0000325 const GrSamplerState* sampler,
326 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
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000337 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000338 desc, false);
339
340 if (GrTexture::NeedsResizing(resourceKey)) {
341 // The desired texture is NPOT and tiled but that isn't supported by
342 // the current hardware. Resize the texture to be a POT
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000343 GrAssert(NULL != sampler);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000344 TextureCacheEntry clampEntry = this->findAndLockTexture(desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000345 NULL);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000346
347 if (NULL == clampEntry.texture()) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000348 clampEntry = this->createAndLockTexture(NULL, desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000349 srcData, rowBytes);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000350 GrAssert(NULL != clampEntry.texture());
351 if (NULL == clampEntry.texture()) {
352 return entry;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000353 }
354 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000355 GrTextureDesc rtDesc = desc;
356 rtDesc.fFlags = rtDesc.fFlags |
357 kRenderTarget_GrTextureFlagBit |
358 kNoStencil_GrTextureFlagBit;
bsalomon@google.com99621082011-11-15 16:47:16 +0000359 rtDesc.fWidth = GrNextPow2(GrMax(desc.fWidth, 64));
360 rtDesc.fHeight = GrNextPow2(GrMax(desc.fHeight, 64));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000361
362 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
363
364 if (NULL != texture) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000365 GrDrawTarget::AutoStateRestore asr(fGpu,
366 GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000367 GrDrawState* drawState = fGpu->drawState();
368 drawState->setRenderTarget(texture->asRenderTarget());
369 drawState->setTexture(0, clampEntry.texture());
bsalomon@google.com82c7bd82011-11-09 15:32:29 +0000370
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000371 GrSamplerState::Filter filter;
372 // if filtering is not desired then we want to ensure all
373 // texels in the resampled image are copies of texels from
374 // the original.
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000375 if (GrTexture::NeedsFiltering(resourceKey)) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000376 filter = GrSamplerState::kBilinear_Filter;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000377 } else {
378 filter = GrSamplerState::kNearest_Filter;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000379 }
bsalomon@google.com1e266f82011-12-12 16:11:33 +0000380 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
381 filter);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000382
383 static const GrVertexLayout layout =
384 GrDrawTarget::StageTexCoordVertexLayoutBit(0,0);
385 GrDrawTarget::AutoReleaseGeometry arg(fGpu, layout, 4, 0);
386
387 if (arg.succeeded()) {
388 GrPoint* verts = (GrPoint*) arg.vertices();
389 verts[0].setIRectFan(0, 0,
390 texture->width(),
391 texture->height(),
392 2*sizeof(GrPoint));
393 verts[1].setIRectFan(0, 0, 1, 1, 2*sizeof(GrPoint));
bsalomon@google.com47059542012-06-06 20:51:20 +0000394 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000395 0, 4);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000396 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000397 }
bsalomon@google.com1da07462011-03-10 14:51:57 +0000398 texture->releaseRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000399 } else {
400 // TODO: Our CPU stretch doesn't filter. But we create separate
401 // stretched textures when the sampler state is either filtered or
402 // not. Either implement filtered stretch blit on CPU or just create
403 // one when FBO case fails.
404
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000405 rtDesc.fFlags = kNone_GrTextureFlags;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000406 // no longer need to clamp at min RT size.
407 rtDesc.fWidth = GrNextPow2(desc.fWidth);
408 rtDesc.fHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000409 int bpp = GrBytesPerPixel(desc.fConfig);
bsalomon@google.com3582bf92011-06-30 21:32:31 +0000410 SkAutoSMalloc<128*128*4> stretchedPixels(bpp *
bsalomon@google.com27847de2011-02-22 20:59:41 +0000411 rtDesc.fWidth *
412 rtDesc.fHeight);
413 stretchImage(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
414 srcData, desc.fWidth, desc.fHeight, bpp);
415
416 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
417
418 GrTexture* texture = fGpu->createTexture(rtDesc,
419 stretchedPixels.get(),
420 stretchedRowBytes);
421 GrAssert(NULL != texture);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000422 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000423 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000424 fTextureCache->unlock(clampEntry.cacheEntry());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000425
426 } else {
427 GrTexture* texture = fGpu->createTexture(desc, srcData, rowBytes);
428 if (NULL != texture) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000429 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000430 }
431 }
432 return entry;
433}
434
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000435GrContext::TextureCacheEntry GrContext::lockScratchTexture(
436 const GrTextureDesc& inDesc,
437 ScratchTexMatch match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000438 GrTextureDesc desc = inDesc;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000439 desc.fClientCacheID = kScratch_CacheID;
440
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000441 if (kExact_ScratchTexMatch != match) {
442 // bin by pow2 with a reasonable min
443 static const int MIN_SIZE = 256;
444 desc.fWidth = GrMax(MIN_SIZE, GrNextPow2(desc.fWidth));
445 desc.fHeight = GrMax(MIN_SIZE, GrNextPow2(desc.fHeight));
446 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000447
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000448 GrResourceEntry* entry;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000449 int origWidth = desc.fWidth;
450 int origHeight = desc.fHeight;
451 bool doubledW = false;
452 bool doubledH = false;
453
454 do {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000455 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL, desc, true);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000456 entry = fTextureCache->findAndLock(key,
457 GrResourceCache::kNested_LockType);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000458 // if we miss, relax the fit of the flags...
459 // then try doubling width... then height.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000460 if (NULL != entry || kExact_ScratchTexMatch == match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000461 break;
462 }
463 if (!(desc.fFlags & kRenderTarget_GrTextureFlagBit)) {
464 desc.fFlags = desc.fFlags | kRenderTarget_GrTextureFlagBit;
465 } else if (desc.fFlags & kNoStencil_GrTextureFlagBit) {
466 desc.fFlags = desc.fFlags & ~kNoStencil_GrTextureFlagBit;
467 } else if (!doubledW) {
468 desc.fFlags = inDesc.fFlags;
469 desc.fWidth *= 2;
470 doubledW = true;
471 } else if (!doubledH) {
472 desc.fFlags = inDesc.fFlags;
473 desc.fWidth = origWidth;
474 desc.fHeight *= 2;
475 doubledH = true;
476 } else {
477 break;
478 }
479
480 } while (true);
481
482 if (NULL == entry) {
483 desc.fFlags = inDesc.fFlags;
484 desc.fWidth = origWidth;
485 desc.fHeight = origHeight;
486 GrTexture* texture = fGpu->createTexture(desc, NULL, 0);
487 if (NULL != texture) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000488 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000489 texture->desc(),
490 true);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000491 entry = fTextureCache->createAndLock(key, texture);
492 }
493 }
494
495 // If the caller gives us the same desc/sampler twice we don't want
496 // to return the same texture the second time (unless it was previously
497 // released). So we detach the entry from the cache and reattach at release.
498 if (NULL != entry) {
499 fTextureCache->detach(entry);
500 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000501 return TextureCacheEntry(entry);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000502}
503
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000504void GrContext::unlockTexture(TextureCacheEntry entry) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000505 ASSERT_OWNED_RESOURCE(entry.texture());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000506 // If this is a scratch texture we detached it from the cache
507 // while it was locked (to avoid two callers simultaneously getting
508 // the same texture).
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000509 if (GrTexture::IsScratchTexture(entry.cacheEntry()->key())) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000510 fTextureCache->reattachAndUnlock(entry.cacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000511 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000512 fTextureCache->unlock(entry.cacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000513 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000514}
515
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000516GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000517 void* srcData,
518 size_t rowBytes) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000519 GrTextureDesc descCopy = descIn;
520 descCopy.fClientCacheID = kUncached_CacheID;
521 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000522}
523
524void GrContext::getTextureCacheLimits(int* maxTextures,
525 size_t* maxTextureBytes) const {
526 fTextureCache->getLimits(maxTextures, maxTextureBytes);
527}
528
529void GrContext::setTextureCacheLimits(int maxTextures, size_t maxTextureBytes) {
530 fTextureCache->setLimits(maxTextures, maxTextureBytes);
531}
532
bsalomon@google.com91958362011-06-13 17:58:13 +0000533int GrContext::getMaxTextureSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000534 return fGpu->getCaps().fMaxTextureSize;
bsalomon@google.com91958362011-06-13 17:58:13 +0000535}
536
537int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000538 return fGpu->getCaps().fMaxRenderTargetSize;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000539}
540
541///////////////////////////////////////////////////////////////////////////////
542
bsalomon@google.come269f212011-11-07 13:29:52 +0000543GrTexture* GrContext::createPlatformTexture(const GrPlatformTextureDesc& desc) {
544 return fGpu->createPlatformTexture(desc);
545}
546
547GrRenderTarget* GrContext::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
548 return fGpu->createPlatformRenderTarget(desc);
549}
550
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000551///////////////////////////////////////////////////////////////////////////////
552
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000553bool GrContext::supportsIndex8PixelConfig(const GrSamplerState* sampler,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000554 int width, int height) const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000555 const GrDrawTarget::Caps& caps = fGpu->getCaps();
556 if (!caps.f8BitPaletteSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000557 return false;
558 }
559
bsalomon@google.com27847de2011-02-22 20:59:41 +0000560 bool isPow2 = GrIsPow2(width) && GrIsPow2(height);
561
562 if (!isPow2) {
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000563 bool tiled = NULL != sampler &&
564 (sampler->getWrapX() != GrSamplerState::kClamp_WrapMode ||
565 sampler->getWrapY() != GrSamplerState::kClamp_WrapMode);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000566 if (tiled && !caps.fNPOTTextureTileSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000567 return false;
568 }
569 }
570 return true;
571}
572
573////////////////////////////////////////////////////////////////////////////////
574
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000575const GrClip& GrContext::getClip() const { return fGpu->getClip(); }
576
bsalomon@google.com27847de2011-02-22 20:59:41 +0000577void GrContext::setClip(const GrClip& clip) {
578 fGpu->setClip(clip);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000579 fDrawState->enableState(GrDrawState::kClip_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000580}
581
582void GrContext::setClip(const GrIRect& rect) {
583 GrClip clip;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000584 clip.setFromIRect(rect);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000585 fGpu->setClip(clip);
586}
587
588////////////////////////////////////////////////////////////////////////////////
589
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000590void GrContext::clear(const GrIRect* rect, const GrColor color) {
bsalomon@google.com398109c2011-04-14 18:40:27 +0000591 this->flush();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000592 fGpu->clear(rect, color);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000593}
594
595void GrContext::drawPaint(const GrPaint& paint) {
596 // set rect to be big enough to fill the space, but not super-huge, so we
597 // don't overflow fixed-point implementations
bsalomon@google.comd302f142011-03-03 13:54:13 +0000598 GrRect r;
599 r.setLTRB(0, 0,
600 GrIntToScalar(getRenderTarget()->width()),
601 GrIntToScalar(getRenderTarget()->height()));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000602 GrMatrix inverse;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000603 SkTLazy<GrPaint> tmpPaint;
604 const GrPaint* p = &paint;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000605 GrAutoMatrix am;
606
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000607 // We attempt to map r by the inverse matrix and draw that. mapRect will
608 // map the four corners and bound them with a new rect. This will not
609 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000610 if (!this->getMatrix().hasPerspective()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000611 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000612 GrPrintf("Could not invert matrix");
613 return;
614 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000615 inverse.mapRect(&r);
616 } else {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000617 if (paint.getActiveMaskStageMask() || paint.getActiveStageMask()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000618 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000619 GrPrintf("Could not invert matrix");
620 return;
621 }
622 tmpPaint.set(paint);
623 tmpPaint.get()->preConcatActiveSamplerMatrices(inverse);
624 p = tmpPaint.get();
625 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000626 am.set(this, GrMatrix::I());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000627 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000628 // by definition this fills the entire clip, no need for AA
629 if (paint.fAntiAlias) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000630 if (!tmpPaint.isValid()) {
631 tmpPaint.set(paint);
632 p = tmpPaint.get();
633 }
634 GrAssert(p == tmpPaint.get());
635 tmpPaint.get()->fAntiAlias = false;
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000636 }
637 this->drawRect(*p, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000638}
639
bsalomon@google.com205d4602011-04-25 12:43:45 +0000640////////////////////////////////////////////////////////////////////////////////
641
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000642namespace {
643inline bool disable_coverage_aa_for_blend(GrDrawTarget* target) {
644 return DISABLE_COVERAGE_AA_FOR_BLEND && !target->canApplyCoverage();
645}
646}
647
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000648////////////////////////////////////////////////////////////////////////////////
649
bsalomon@google.com27847de2011-02-22 20:59:41 +0000650/* create a triangle strip that strokes the specified triangle. There are 8
651 unique vertices, but we repreat the last 2 to close up. Alternatively we
652 could use an indices array, and then only send 8 verts, but not sure that
653 would be faster.
654 */
bsalomon@google.com205d4602011-04-25 12:43:45 +0000655static void setStrokeRectStrip(GrPoint verts[10], GrRect rect,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000656 GrScalar width) {
657 const GrScalar rad = GrScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000658 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000659
660 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
661 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
662 verts[2].set(rect.fRight - rad, rect.fTop + rad);
663 verts[3].set(rect.fRight + rad, rect.fTop - rad);
664 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
665 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
666 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
667 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
668 verts[8] = verts[0];
669 verts[9] = verts[1];
670}
671
reed@google.com20efde72011-05-09 17:00:02 +0000672/**
673 * Returns true if the rects edges are integer-aligned.
674 */
675static bool isIRect(const GrRect& r) {
676 return GrScalarIsInt(r.fLeft) && GrScalarIsInt(r.fTop) &&
677 GrScalarIsInt(r.fRight) && GrScalarIsInt(r.fBottom);
678}
679
bsalomon@google.com205d4602011-04-25 12:43:45 +0000680static bool apply_aa_to_rect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000681 const GrRect& rect,
682 GrScalar width,
683 const GrMatrix* matrix,
684 GrMatrix* combinedMatrix,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000685 GrRect* devRect,
686 bool* useVertexCoverage) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000687 // we use a simple coverage ramp to do aa on axis-aligned rects
688 // we check if the rect will be axis-aligned, and the rect won't land on
689 // integer coords.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000690
bsalomon@google.coma3108262011-10-10 14:08:47 +0000691 // we are keeping around the "tweak the alpha" trick because
692 // it is our only hope for the fixed-pipe implementation.
693 // In a shader implementation we can give a separate coverage input
bsalomon@google.com289533a2011-10-27 12:34:25 +0000694 // TODO: remove this ugliness when we drop the fixed-pipe impl
bsalomon@google.coma3108262011-10-10 14:08:47 +0000695 *useVertexCoverage = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000696 if (!target->canTweakAlphaForCoverage()) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000697 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +0000698#if GR_DEBUG
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000699 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000700#endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000701 return false;
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000702 } else {
703 *useVertexCoverage = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000704 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000705 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000706 const GrDrawState& drawState = target->getDrawState();
707 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000708 return false;
709 }
710
bsalomon@google.com471d4712011-08-23 15:45:25 +0000711 if (0 == width && target->willUseHWAALines()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000712 return false;
713 }
714
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000715 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000716 return false;
717 }
718
719 if (NULL != matrix &&
720 !matrix->preservesAxisAlignment()) {
721 return false;
722 }
723
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000724 *combinedMatrix = drawState.getViewMatrix();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000725 if (NULL != matrix) {
726 combinedMatrix->preConcat(*matrix);
727 GrAssert(combinedMatrix->preservesAxisAlignment());
728 }
729
730 combinedMatrix->mapRect(devRect, rect);
731 devRect->sort();
732
733 if (width < 0) {
reed@google.com20efde72011-05-09 17:00:02 +0000734 return !isIRect(*devRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000735 } else {
736 return true;
737 }
738}
739
bsalomon@google.com27847de2011-02-22 20:59:41 +0000740void GrContext::drawRect(const GrPaint& paint,
741 const GrRect& rect,
742 GrScalar width,
743 const GrMatrix* matrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000744 SK_TRACE_EVENT0("GrContext::drawRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000745
746 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
robertphillips@google.com972265d2012-06-13 18:49:30 +0000747 GrDrawState::AutoTextureRelease atr(fDrawState);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000748 int stageMask = paint.getActiveStageMask();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000749
bsalomon@google.com205d4602011-04-25 12:43:45 +0000750 GrRect devRect = rect;
751 GrMatrix combinedMatrix;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000752 bool useVertexCoverage;
bsalomon@google.com289533a2011-10-27 12:34:25 +0000753 bool needAA = paint.fAntiAlias &&
754 !this->getRenderTarget()->isMultisampled();
755 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix,
756 &combinedMatrix, &devRect,
757 &useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000758
759 if (doAA) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000760 GrDrawTarget::AutoDeviceCoordDraw adcd(target, stageMask);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000761 if (width >= 0) {
762 GrVec strokeSize;;
763 if (width > 0) {
764 strokeSize.set(width, width);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000765 combinedMatrix.mapVectors(&strokeSize, 1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000766 strokeSize.setAbs(strokeSize);
767 } else {
768 strokeSize.set(GR_Scalar1, GR_Scalar1);
769 }
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000770 fAARectRenderer->strokeAARect(this->getGpu(), target, devRect,
771 strokeSize, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000772 } else {
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000773 fAARectRenderer->fillAARect(this->getGpu(), target,
774 devRect, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000775 }
776 return;
777 }
778
bsalomon@google.com27847de2011-02-22 20:59:41 +0000779 if (width >= 0) {
780 // TODO: consider making static vertex buffers for these cases.
781 // Hairline could be done by just adding closing vertex to
782 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000783 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
784
bsalomon@google.com27847de2011-02-22 20:59:41 +0000785 static const int worstCaseVertCount = 10;
786 GrDrawTarget::AutoReleaseGeometry geo(target, layout, worstCaseVertCount, 0);
787
788 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000789 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000790 return;
791 }
792
793 GrPrimitiveType primType;
794 int vertCount;
795 GrPoint* vertex = geo.positions();
796
797 if (width > 0) {
798 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000799 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000800 setStrokeRectStrip(vertex, rect, width);
801 } else {
802 // hairline
803 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000804 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000805 vertex[0].set(rect.fLeft, rect.fTop);
806 vertex[1].set(rect.fRight, rect.fTop);
807 vertex[2].set(rect.fRight, rect.fBottom);
808 vertex[3].set(rect.fLeft, rect.fBottom);
809 vertex[4].set(rect.fLeft, rect.fTop);
810 }
811
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000812 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000813 if (NULL != matrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000814 GrDrawState* drawState = target->drawState();
815 avmr.set(drawState);
816 drawState->preConcatViewMatrix(*matrix);
817 drawState->preConcatSamplerMatrices(stageMask, *matrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000818 }
819
820 target->drawNonIndexed(primType, 0, vertCount);
821 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000822#if GR_STATIC_RECT_VB
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000823 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000824 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
825 if (NULL == sqVB) {
826 GrPrintf("Failed to create static rect vb.\n");
827 return;
828 }
829 target->setVertexSourceToBuffer(layout, sqVB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000830 GrDrawState* drawState = target->drawState();
831 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000832 GrMatrix m;
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000833 m.setAll(rect.width(), 0, rect.fLeft,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000834 0, rect.height(), rect.fTop,
835 0, 0, GrMatrix::I()[8]);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000836
837 if (NULL != matrix) {
838 m.postConcat(*matrix);
839 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000840 drawState->preConcatViewMatrix(m);
841 drawState->preConcatSamplerMatrices(stageMask, m);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000842
bsalomon@google.com47059542012-06-06 20:51:20 +0000843 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000844#else
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000845 target->drawSimpleRect(rect, matrix, stageMask);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000846#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000847 }
848}
849
850void GrContext::drawRectToRect(const GrPaint& paint,
851 const GrRect& dstRect,
852 const GrRect& srcRect,
853 const GrMatrix* dstMatrix,
854 const GrMatrix* srcMatrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000855 SK_TRACE_EVENT0("GrContext::drawRectToRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000856
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000857 // srcRect refers to paint's first texture
858 if (NULL == paint.getTexture(0)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000859 drawRect(paint, dstRect, -1, dstMatrix);
860 return;
861 }
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000862
bsalomon@google.com27847de2011-02-22 20:59:41 +0000863 GR_STATIC_ASSERT(!BATCH_RECT_TO_RECT || !GR_STATIC_RECT_VB);
864
865#if GR_STATIC_RECT_VB
866 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
robertphillips@google.com972265d2012-06-13 18:49:30 +0000867 GrDrawState::AutoTextureRelease atr(fDrawState);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000868 GrDrawState* drawState = target->drawState();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000869 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000870 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000871
872 GrMatrix m;
873
874 m.setAll(dstRect.width(), 0, dstRect.fLeft,
875 0, dstRect.height(), dstRect.fTop,
876 0, 0, GrMatrix::I()[8]);
877 if (NULL != dstMatrix) {
878 m.postConcat(*dstMatrix);
879 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000880 drawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000881
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000882 // srcRect refers to first stage
883 int otherStageMask = paint.getActiveStageMask() &
884 (~(1 << GrPaint::kFirstTextureStage));
885 if (otherStageMask) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000886 drawState->preConcatSamplerMatrices(otherStageMask, m);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000887 }
888
bsalomon@google.com27847de2011-02-22 20:59:41 +0000889 m.setAll(srcRect.width(), 0, srcRect.fLeft,
890 0, srcRect.height(), srcRect.fTop,
891 0, 0, GrMatrix::I()[8]);
892 if (NULL != srcMatrix) {
893 m.postConcat(*srcMatrix);
894 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000895 drawState->sampler(GrPaint::kFirstTextureStage)->preConcatMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000896
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000897 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
898 if (NULL == sqVB) {
899 GrPrintf("Failed to create static rect vb.\n");
900 return;
901 }
902 target->setVertexSourceToBuffer(layout, sqVB);
bsalomon@google.com47059542012-06-06 20:51:20 +0000903 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000904#else
905
906 GrDrawTarget* target;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000907#if BATCH_RECT_TO_RECT
bsalomon@google.com27847de2011-02-22 20:59:41 +0000908 target = this->prepareToDraw(paint, kBuffered_DrawCategory);
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000909#else
bsalomon@google.com27847de2011-02-22 20:59:41 +0000910 target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
911#endif
robertphillips@google.com972265d2012-06-13 18:49:30 +0000912 GrDrawState::AutoTextureRelease atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000913
tomhudson@google.com93813632011-10-27 20:21:16 +0000914 const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
915 const GrMatrix* srcMatrices[GrDrawState::kNumStages] = {NULL};
bsalomon@google.com27847de2011-02-22 20:59:41 +0000916 srcRects[0] = &srcRect;
917 srcMatrices[0] = srcMatrix;
918
919 target->drawRect(dstRect, dstMatrix, 1, srcRects, srcMatrices);
920#endif
921}
922
923void GrContext::drawVertices(const GrPaint& paint,
924 GrPrimitiveType primitiveType,
925 int vertexCount,
926 const GrPoint positions[],
927 const GrPoint texCoords[],
928 const GrColor colors[],
929 const uint16_t indices[],
930 int indexCount) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000931 SK_TRACE_EVENT0("GrContext::drawVertices");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000932
933 GrDrawTarget::AutoReleaseGeometry geo;
934
935 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
robertphillips@google.com972265d2012-06-13 18:49:30 +0000936 GrDrawState::AutoTextureRelease atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000937
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000938 bool hasTexCoords[GrPaint::kTotalStages] = {
939 NULL != texCoords, // texCoordSrc provides explicit stage 0 coords
940 0 // remaining stages use positions
941 };
942
943 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, hasTexCoords);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000944
945 if (NULL != colors) {
946 layout |= GrDrawTarget::kColor_VertexLayoutBit;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000947 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000948 int vertexSize = GrDrawTarget::VertexSize(layout);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000949
950 if (sizeof(GrPoint) != vertexSize) {
951 if (!geo.set(target, layout, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000952 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000953 return;
954 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000955 int texOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000956 int colorOffset;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000957 GrDrawTarget::VertexSizeAndOffsetsByIdx(layout,
958 texOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000959 &colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000960 NULL,
961 NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000962 void* curVertex = geo.vertices();
963
964 for (int i = 0; i < vertexCount; ++i) {
965 *((GrPoint*)curVertex) = positions[i];
966
967 if (texOffsets[0] > 0) {
968 *(GrPoint*)((intptr_t)curVertex + texOffsets[0]) = texCoords[i];
969 }
970 if (colorOffset > 0) {
971 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
972 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000973 curVertex = (void*)((intptr_t)curVertex + vertexSize);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000974 }
975 } else {
976 target->setVertexSourceToArray(layout, positions, vertexCount);
977 }
978
bsalomon@google.com91958362011-06-13 17:58:13 +0000979 // we don't currently apply offscreen AA to this path. Need improved
980 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000981
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000982 if (NULL != indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000983 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000984 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000985 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000986 target->drawNonIndexed(primitiveType, 0, vertexCount);
987 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000988}
989
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000990///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com150d2842012-01-12 20:19:56 +0000991namespace {
992
bsalomon@google.com93c96602012-04-27 13:05:21 +0000993struct CircleVertex {
994 GrPoint fPos;
995 GrPoint fCenter;
996 GrScalar fOuterRadius;
997 GrScalar fInnerRadius;
998};
999
1000/* Returns true if will map a circle to another circle. This can be true
1001 * if the matrix only includes square-scale, rotation, translation.
1002 */
1003inline bool isSimilarityTransformation(const SkMatrix& matrix,
1004 SkScalar tol = SK_ScalarNearlyZero) {
1005 if (matrix.isIdentity() || matrix.getType() == SkMatrix::kTranslate_Mask) {
1006 return true;
1007 }
1008 if (matrix.hasPerspective()) {
1009 return false;
1010 }
1011
1012 SkScalar mx = matrix.get(SkMatrix::kMScaleX);
1013 SkScalar sx = matrix.get(SkMatrix::kMSkewX);
1014 SkScalar my = matrix.get(SkMatrix::kMScaleY);
1015 SkScalar sy = matrix.get(SkMatrix::kMSkewY);
1016
1017 if (mx == 0 && sx == 0 && my == 0 && sy == 0) {
1018 return false;
1019 }
1020
1021 // it has scales or skews, but it could also be rotation, check it out.
1022 SkVector vec[2];
1023 vec[0].set(mx, sx);
1024 vec[1].set(sy, my);
1025
1026 return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) &&
1027 SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(),
1028 SkScalarSquare(tol));
1029}
1030
1031}
1032
1033// TODO: strokeWidth can't be larger than zero right now.
1034// It will be fixed when drawPath() can handle strokes.
1035void GrContext::drawOval(const GrPaint& paint,
1036 const GrRect& rect,
1037 SkScalar strokeWidth) {
1038 DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
1039 kUnbuffered_DrawCategory;
1040 GrDrawTarget* target = this->prepareToDraw(paint, category);
robertphillips@google.com972265d2012-06-13 18:49:30 +00001041 GrDrawState::AutoTextureRelease atr(fDrawState);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001042 GrDrawState* drawState = target->drawState();
1043 GrMatrix vm = drawState->getViewMatrix();
1044
1045 if (!isSimilarityTransformation(vm) ||
1046 !paint.fAntiAlias ||
1047 rect.height() != rect.width()) {
1048 SkPath path;
1049 path.addOval(rect);
1050 GrPathFill fill = (strokeWidth == 0) ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001051 kHairLine_GrPathFill : kWinding_GrPathFill;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001052 this->internalDrawPath(paint, path, fill, NULL);
1053 return;
1054 }
1055
1056 const GrRenderTarget* rt = drawState->getRenderTarget();
1057 if (NULL == rt) {
1058 return;
1059 }
1060
1061 GrDrawTarget::AutoDeviceCoordDraw adcd(target, paint.getActiveStageMask());
1062
1063 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
1064 layout |= GrDrawTarget::kEdge_VertexLayoutBit;
1065 GrAssert(sizeof(CircleVertex) == GrDrawTarget::VertexSize(layout));
1066
1067 GrPoint center = GrPoint::Make(rect.centerX(), rect.centerY());
1068 GrScalar radius = SkScalarHalf(rect.width());
1069
1070 vm.mapPoints(&center, 1);
1071 radius = vm.mapRadius(radius);
1072
1073 GrScalar outerRadius = radius;
1074 GrScalar innerRadius = 0;
1075 SkScalar halfWidth = 0;
1076 if (strokeWidth == 0) {
1077 halfWidth = SkScalarHalf(SK_Scalar1);
1078
1079 outerRadius += halfWidth;
1080 innerRadius = SkMaxScalar(0, radius - halfWidth);
1081 }
1082
1083 GrDrawTarget::AutoReleaseGeometry geo(target, layout, 4, 0);
1084 if (!geo.succeeded()) {
1085 GrPrintf("Failed to get space for vertices!\n");
1086 return;
1087 }
1088
1089 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
1090
1091 SkScalar L = center.fX - outerRadius;
1092 SkScalar R = center.fX + outerRadius;
1093 SkScalar T = center.fY - outerRadius;
1094 SkScalar B = center.fY + outerRadius;
1095
1096 verts[0].fPos = SkPoint::Make(L, T);
1097 verts[1].fPos = SkPoint::Make(R, T);
1098 verts[2].fPos = SkPoint::Make(L, B);
1099 verts[3].fPos = SkPoint::Make(R, B);
1100
1101 for (int i = 0; i < 4; ++i) {
1102 // this goes to fragment shader, it should be in y-points-up space.
1103 verts[i].fCenter = SkPoint::Make(center.fX, rt->height() - center.fY);
1104
1105 verts[i].fOuterRadius = outerRadius;
1106 verts[i].fInnerRadius = innerRadius;
1107 }
1108
1109 drawState->setVertexEdgeType(GrDrawState::kCircle_EdgeType);
bsalomon@google.com47059542012-06-06 20:51:20 +00001110 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001111}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001112
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001113void GrContext::drawPath(const GrPaint& paint, const SkPath& path,
reed@google.com07f3ee12011-05-16 17:21:57 +00001114 GrPathFill fill, const GrPoint* translate) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001115
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001116 if (path.isEmpty()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001117 if (GrIsFillInverted(fill)) {
1118 this->drawPaint(paint);
1119 }
1120 return;
1121 }
1122
bsalomon@google.com93c96602012-04-27 13:05:21 +00001123 SkRect ovalRect;
1124 if (!GrIsFillInverted(fill) && path.isOval(&ovalRect)) {
1125 if (translate) {
1126 ovalRect.offset(*translate);
1127 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001128 SkScalar width = (fill == kHairLine_GrPathFill) ? 0 : -SK_Scalar1;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001129 this->drawOval(paint, ovalRect, width);
1130 return;
1131 }
1132
1133 internalDrawPath(paint, path, fill, translate);
1134}
1135
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001136void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +00001137 GrPathFill fill, const GrPoint* translate) {
1138
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001139 // Note that below we may sw-rasterize the path into a scratch texture.
1140 // Scratch textures can be recycled after they are returned to the texture
1141 // cache. This presents a potential hazard for buffered drawing. However,
1142 // the writePixels that uploads to the scratch will perform a flush so we're
1143 // OK.
1144 DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
1145 kUnbuffered_DrawCategory;
1146 GrDrawTarget* target = this->prepareToDraw(paint, category);
robertphillips@google.com972265d2012-06-13 18:49:30 +00001147 GrDrawState::AutoTextureRelease atr(fDrawState);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001148 GrDrawState::StageMask stageMask = paint.getActiveStageMask();
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001149
bsalomon@google.com289533a2011-10-27 12:34:25 +00001150 bool prAA = paint.fAntiAlias && !this->getRenderTarget()->isMultisampled();
1151
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001152 // An Assumption here is that path renderer would use some form of tweaking
1153 // the src color (either the input alpha or in the frag shader) to implement
1154 // aa. If we have some future driver-mojo path AA that can do the right
1155 // thing WRT to the blend then we'll need some query on the PR.
1156 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001157#if GR_DEBUG
bsalomon@google.com979432b2011-11-05 21:38:22 +00001158 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001159#endif
bsalomon@google.com289533a2011-10-27 12:34:25 +00001160 prAA = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001161 }
bsalomon@google.com289533a2011-10-27 12:34:25 +00001162
robertphillips@google.com72176b22012-05-23 13:19:12 +00001163 GrPathRenderer* pr = this->getPathRenderer(path, fill, target, prAA, true);
bsalomon@google.com30085192011-08-19 15:42:31 +00001164 if (NULL == pr) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001165#if GR_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001166 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001167#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001168 return;
1169 }
1170
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001171 pr->drawPath(path, fill, translate, target, stageMask, prAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001172}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001173
bsalomon@google.com27847de2011-02-22 20:59:41 +00001174////////////////////////////////////////////////////////////////////////////////
1175
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001176void GrContext::flush(int flagsBitfield) {
1177 if (kDiscard_FlushBit & flagsBitfield) {
1178 fDrawBuffer->reset();
1179 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001180 this->flushDrawBuffer();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001181 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001182 if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001183 fGpu->forceRenderTargetFlush();
1184 }
1185}
1186
bsalomon@google.com27847de2011-02-22 20:59:41 +00001187void GrContext::flushDrawBuffer() {
junov@google.com53a55842011-06-08 22:55:10 +00001188 if (fDrawBuffer) {
robertphillips@google.com58b38182012-05-03 16:29:41 +00001189 // With addition of the AA clip path, flushing the draw buffer can
1190 // result in the generation of an AA clip mask. During this
1191 // process the SW path renderer may be invoked which recusively
1192 // calls this method (via internalWriteTexturePixels) creating
1193 // infinite recursion
1194 GrInOrderDrawBuffer* temp = fDrawBuffer;
1195 fDrawBuffer = NULL;
1196
1197 temp->flushTo(fGpu);
1198
1199 fDrawBuffer = temp;
junov@google.com53a55842011-06-08 22:55:10 +00001200 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001201}
1202
bsalomon@google.com6f379512011-11-16 20:36:03 +00001203void GrContext::internalWriteTexturePixels(GrTexture* texture,
1204 int left, int top,
1205 int width, int height,
1206 GrPixelConfig config,
1207 const void* buffer,
1208 size_t rowBytes,
1209 uint32_t flags) {
1210 SK_TRACE_EVENT0("GrContext::writeTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001211 ASSERT_OWNED_RESOURCE(texture);
1212
bsalomon@google.com6f379512011-11-16 20:36:03 +00001213 if (!(kDontFlush_PixelOpsFlag & flags)) {
1214 this->flush();
1215 }
1216 // TODO: use scratch texture to perform conversion
1217 if (GrPixelConfigIsUnpremultiplied(texture->config()) !=
1218 GrPixelConfigIsUnpremultiplied(config)) {
1219 return;
1220 }
1221
1222 fGpu->writeTexturePixels(texture, left, top, width, height,
1223 config, buffer, rowBytes);
1224}
1225
1226bool GrContext::internalReadTexturePixels(GrTexture* texture,
1227 int left, int top,
1228 int width, int height,
1229 GrPixelConfig config,
1230 void* buffer,
1231 size_t rowBytes,
1232 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001233 SK_TRACE_EVENT0("GrContext::readTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001234 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001235
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001236 // TODO: code read pixels for textures that aren't also rendertargets
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001237 GrRenderTarget* target = texture->asRenderTarget();
1238 if (NULL != target) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001239 return this->internalReadRenderTargetPixels(target,
1240 left, top, width, height,
1241 config, buffer, rowBytes,
1242 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001243 } else {
1244 return false;
1245 }
1246}
1247
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001248#include "SkConfig8888.h"
1249
1250namespace {
1251/**
1252 * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel
1253 * formats are representable as Config8888 and so the function returns false
1254 * if the GrPixelConfig has no equivalent Config8888.
1255 */
1256bool grconfig_to_config8888(GrPixelConfig config,
1257 SkCanvas::Config8888* config8888) {
1258 switch (config) {
1259 case kRGBA_8888_PM_GrPixelConfig:
1260 *config8888 = SkCanvas::kRGBA_Premul_Config8888;
1261 return true;
1262 case kRGBA_8888_UPM_GrPixelConfig:
1263 *config8888 = SkCanvas::kRGBA_Unpremul_Config8888;
1264 return true;
1265 case kBGRA_8888_PM_GrPixelConfig:
1266 *config8888 = SkCanvas::kBGRA_Premul_Config8888;
1267 return true;
1268 case kBGRA_8888_UPM_GrPixelConfig:
1269 *config8888 = SkCanvas::kBGRA_Unpremul_Config8888;
1270 return true;
1271 default:
1272 return false;
1273 }
1274}
1275}
1276
bsalomon@google.com6f379512011-11-16 20:36:03 +00001277bool GrContext::internalReadRenderTargetPixels(GrRenderTarget* target,
1278 int left, int top,
1279 int width, int height,
1280 GrPixelConfig config,
1281 void* buffer,
1282 size_t rowBytes,
1283 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001284 SK_TRACE_EVENT0("GrContext::readRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001285 ASSERT_OWNED_RESOURCE(target);
1286
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001287 if (NULL == target) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001288 target = fDrawState->getRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001289 if (NULL == target) {
1290 return false;
1291 }
1292 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001293
bsalomon@google.com6f379512011-11-16 20:36:03 +00001294 if (!(kDontFlush_PixelOpsFlag & flags)) {
1295 this->flush();
1296 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001297
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001298 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1299 GrPixelConfigIsUnpremultiplied(config) &&
1300 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1301 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1302 if (!grconfig_to_config8888(target->config(), &srcConfig8888) ||
1303 !grconfig_to_config8888(config, &dstConfig8888)) {
1304 return false;
1305 }
1306 // do read back using target's own config
1307 this->internalReadRenderTargetPixels(target,
1308 left, top,
1309 width, height,
1310 target->config(),
1311 buffer, rowBytes,
1312 kDontFlush_PixelOpsFlag);
1313 // sw convert the pixels to unpremul config
1314 uint32_t* pixels = reinterpret_cast<uint32_t*>(buffer);
1315 SkConvertConfig8888Pixels(pixels, rowBytes, dstConfig8888,
1316 pixels, rowBytes, srcConfig8888,
1317 width, height);
1318 return true;
1319 }
1320
bsalomon@google.comc4364992011-11-07 15:54:49 +00001321 GrTexture* src = target->asTexture();
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001322 bool swapRAndB = NULL != src &&
1323 fGpu->preferredReadPixelsConfig(config) ==
1324 GrPixelConfigSwapRAndB(config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001325
1326 bool flipY = NULL != src &&
1327 fGpu->readPixelsWillPayForYFlip(target, left, top,
1328 width, height, config,
1329 rowBytes);
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001330 bool alphaConversion = (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1331 GrPixelConfigIsUnpremultiplied(config));
bsalomon@google.comc4364992011-11-07 15:54:49 +00001332
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001333 if (NULL == src && alphaConversion) {
1334 // we should fallback to cpu conversion here. This could happen when
1335 // we were given an external render target by the client that is not
1336 // also a texture (e.g. FBO 0 in GL)
1337 return false;
1338 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001339 // we draw to a scratch texture if any of these conversion are applied
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001340 GrAutoScratchTexture ast;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001341 if (flipY || swapRAndB || alphaConversion) {
1342 GrAssert(NULL != src);
1343 if (swapRAndB) {
1344 config = GrPixelConfigSwapRAndB(config);
1345 GrAssert(kUnknown_GrPixelConfig != config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001346 }
1347 // Make the scratch a render target because we don't have a robust
1348 // readTexturePixels as of yet (it calls this function).
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001349 GrTextureDesc desc;
1350 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1351 desc.fWidth = width;
1352 desc.fHeight = height;
1353 desc.fConfig = config;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001354
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001355 // When a full readback is faster than a partial we could always make
1356 // the scratch exactly match the passed rect. However, if we see many
1357 // different size rectangles we will trash our texture cache and pay the
1358 // cost of creating and destroying many textures. So, we only request
1359 // an exact match when the caller is reading an entire RT.
1360 ScratchTexMatch match = kApprox_ScratchTexMatch;
1361 if (0 == left &&
1362 0 == top &&
1363 target->width() == width &&
1364 target->height() == height &&
1365 fGpu->fullReadPixelsIsFasterThanPartial()) {
1366 match = kExact_ScratchTexMatch;
1367 }
1368 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001369 GrTexture* texture = ast.texture();
1370 if (!texture) {
1371 return false;
1372 }
1373 target = texture->asRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001374 GrAssert(NULL != target);
1375
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001376 GrDrawTarget::AutoStateRestore asr(fGpu,
1377 GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001378 GrDrawState* drawState = fGpu->drawState();
1379 drawState->setRenderTarget(target);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001380
bsalomon@google.comc4364992011-11-07 15:54:49 +00001381 GrMatrix matrix;
1382 if (flipY) {
1383 matrix.setTranslate(SK_Scalar1 * left,
1384 SK_Scalar1 * (top + height));
1385 matrix.set(GrMatrix::kMScaleY, -GR_Scalar1);
1386 } else {
1387 matrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
1388 }
1389 matrix.postIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001390 drawState->sampler(0)->reset(matrix);
1391 drawState->sampler(0)->setRAndBSwap(swapRAndB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001392 drawState->setTexture(0, src);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001393 GrRect rect;
1394 rect.setXYWH(0, 0, SK_Scalar1 * width, SK_Scalar1 * height);
1395 fGpu->drawSimpleRect(rect, NULL, 0x1);
1396 left = 0;
1397 top = 0;
1398 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001399 return fGpu->readPixels(target,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001400 left, top, width, height,
1401 config, buffer, rowBytes, flipY);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001402}
1403
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001404void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1405 GrAssert(target);
1406 ASSERT_OWNED_RESOURCE(target);
1407 // In the future we may track whether there are any pending draws to this
1408 // target. We don't today so we always perform a flush. We don't promise
1409 // this to our clients, though.
1410 this->flush();
1411 fGpu->resolveRenderTarget(target);
1412}
1413
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001414void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst) {
1415 if (NULL == src || NULL == dst) {
1416 return;
1417 }
1418 ASSERT_OWNED_RESOURCE(src);
1419
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001420 // Writes pending to the source texture are not tracked, so a flush
1421 // is required to ensure that the copy captures the most recent contents
1422 // of the source texture. See similar behaviour in
1423 // GrContext::resolveRenderTarget.
1424 this->flush();
1425
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001426 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001427 GrDrawState* drawState = fGpu->drawState();
1428 drawState->setRenderTarget(dst);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001429 GrMatrix sampleM;
1430 sampleM.setIDiv(src->width(), src->height());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001431 drawState->setTexture(0, src);
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001432 drawState->sampler(0)->reset(sampleM);
bsalomon@google.com5db3b6c2012-01-12 20:38:57 +00001433 SkRect rect = SkRect::MakeXYWH(0, 0,
1434 SK_Scalar1 * src->width(),
1435 SK_Scalar1 * src->height());
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001436 fGpu->drawSimpleRect(rect, NULL, 1 << 0);
1437}
1438
bsalomon@google.com6f379512011-11-16 20:36:03 +00001439void GrContext::internalWriteRenderTargetPixels(GrRenderTarget* target,
1440 int left, int top,
1441 int width, int height,
1442 GrPixelConfig config,
1443 const void* buffer,
1444 size_t rowBytes,
1445 uint32_t flags) {
1446 SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001447 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001448
1449 if (NULL == target) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001450 target = fDrawState->getRenderTarget();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001451 if (NULL == target) {
1452 return;
1453 }
1454 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001455
1456 // TODO: when underlying api has a direct way to do this we should use it
1457 // (e.g. glDrawPixels on desktop GL).
1458
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001459 // If the RT is also a texture and we don't have to do PM/UPM conversion
1460 // then take the texture path, which we expect to be at least as fast or
1461 // faster since it doesn't use an intermediate texture as we do below.
1462
1463#if !GR_MAC_BUILD
1464 // At least some drivers on the Mac get confused when glTexImage2D is called
1465 // on a texture attached to an FBO. The FBO still sees the old image. TODO:
1466 // determine what OS versions and/or HW is affected.
1467 if (NULL != target->asTexture() &&
1468 GrPixelConfigIsUnpremultiplied(target->config()) ==
1469 GrPixelConfigIsUnpremultiplied(config)) {
1470
1471 this->internalWriteTexturePixels(target->asTexture(),
1472 left, top, width, height,
1473 config, buffer, rowBytes, flags);
1474 return;
1475 }
1476#endif
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001477 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1478 GrPixelConfigIsUnpremultiplied(config) &&
1479 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1480 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1481 if (!grconfig_to_config8888(config, &srcConfig8888) ||
1482 !grconfig_to_config8888(target->config(), &dstConfig8888)) {
1483 return;
1484 }
1485 // allocate a tmp buffer and sw convert the pixels to premul
1486 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(width * height);
1487 const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer);
1488 SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888,
1489 src, rowBytes, srcConfig8888,
1490 width, height);
1491 // upload the already premul pixels
1492 this->internalWriteRenderTargetPixels(target,
1493 left, top,
1494 width, height,
1495 target->config(),
1496 tmpPixels, 4 * width, flags);
1497 return;
1498 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001499
1500 bool swapRAndB = fGpu->preferredReadPixelsConfig(config) ==
1501 GrPixelConfigSwapRAndB(config);
1502 if (swapRAndB) {
1503 config = GrPixelConfigSwapRAndB(config);
1504 }
1505
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001506 GrTextureDesc desc;
1507 desc.fWidth = width;
1508 desc.fHeight = height;
1509 desc.fConfig = config;
1510
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001511 GrAutoScratchTexture ast(this, desc);
1512 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001513 if (NULL == texture) {
1514 return;
1515 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001516 this->internalWriteTexturePixels(texture, 0, 0, width, height,
1517 config, buffer, rowBytes, flags);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001518
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001519 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001520 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001521
1522 GrMatrix matrix;
1523 matrix.setTranslate(GrIntToScalar(left), GrIntToScalar(top));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001524 drawState->setViewMatrix(matrix);
1525 drawState->setRenderTarget(target);
1526 drawState->setTexture(0, texture);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001527
bsalomon@google.com5c638652011-07-18 19:31:59 +00001528 matrix.setIDiv(texture->width(), texture->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001529 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
1530 GrSamplerState::kNearest_Filter,
1531 matrix);
1532 drawState->sampler(0)->setRAndBSwap(swapRAndB);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001533
1534 GrVertexLayout layout = GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(0);
1535 static const int VCOUNT = 4;
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001536 // TODO: Use GrGpu::drawRect here
bsalomon@google.com27847de2011-02-22 20:59:41 +00001537 GrDrawTarget::AutoReleaseGeometry geo(fGpu, layout, VCOUNT, 0);
1538 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001539 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +00001540 return;
1541 }
1542 ((GrPoint*)geo.vertices())->setIRectFan(0, 0, width, height);
bsalomon@google.com47059542012-06-06 20:51:20 +00001543 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, VCOUNT);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001544}
1545////////////////////////////////////////////////////////////////////////////////
1546
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001547void GrContext::setPaint(const GrPaint& paint) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001548
1549 for (int i = 0; i < GrPaint::kMaxTextures; ++i) {
1550 int s = i + GrPaint::kFirstTextureStage;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001551 fDrawState->setTexture(s, paint.getTexture(i));
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001552 ASSERT_OWNED_RESOURCE(paint.getTexture(i));
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001553 if (paint.getTexture(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001554 *fDrawState->sampler(s) = paint.getTextureSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001555 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001556 }
1557
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001558 fDrawState->setFirstCoverageStage(GrPaint::kFirstMaskStage);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001559
1560 for (int i = 0; i < GrPaint::kMaxMasks; ++i) {
1561 int s = i + GrPaint::kFirstMaskStage;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001562 fDrawState->setTexture(s, paint.getMask(i));
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001563 ASSERT_OWNED_RESOURCE(paint.getMask(i));
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001564 if (paint.getMask(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001565 *fDrawState->sampler(s) = paint.getMaskSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001566 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001567 }
bsalomon@google.com26936d02012-03-19 13:06:19 +00001568
1569 // disable all stages not accessible via the paint
1570 for (int s = GrPaint::kTotalStages; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001571 fDrawState->setTexture(s, NULL);
bsalomon@google.com26936d02012-03-19 13:06:19 +00001572 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001573
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001574 fDrawState->setColor(paint.fColor);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001575
1576 if (paint.fDither) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001577 fDrawState->enableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001578 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001579 fDrawState->disableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001580 }
1581 if (paint.fAntiAlias) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001582 fDrawState->enableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001583 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001584 fDrawState->disableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001585 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001586 if (paint.fColorMatrixEnabled) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001587 fDrawState->enableState(GrDrawState::kColorMatrix_StateBit);
1588 fDrawState->setColorMatrix(paint.fColorMatrix);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001589 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001590 fDrawState->disableState(GrDrawState::kColorMatrix_StateBit);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001591 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001592 fDrawState->setBlendFunc(paint.fSrcBlendCoeff, paint.fDstBlendCoeff);
1593 fDrawState->setColorFilter(paint.fColorFilterColor, paint.fColorFilterXfermode);
1594 fDrawState->setCoverage(paint.fCoverage);
reed@google.com4b2d3f32012-05-15 18:05:50 +00001595#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
bsalomon@google.come79c8152012-03-29 19:07:12 +00001596 if ((paint.getActiveMaskStageMask() || 0xff != paint.fCoverage) &&
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001597 !fGpu->canApplyCoverage()) {
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001598 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1599 }
bsalomon@google.com95cd7bd2012-03-28 15:35:05 +00001600#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001601}
1602
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001603GrDrawTarget* GrContext::prepareToDraw(const GrPaint& paint,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001604 DrawCategory category) {
1605 if (category != fLastDrawCategory) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001606 this->flushDrawBuffer();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001607 fLastDrawCategory = category;
1608 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001609 this->setPaint(paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001610 GrDrawTarget* target = fGpu;
1611 switch (category) {
bsalomon@google.com193395c2012-03-30 17:35:12 +00001612 case kUnbuffered_DrawCategory:
1613 target = fGpu;
1614 break;
1615 case kBuffered_DrawCategory:
1616 target = fDrawBuffer;
1617 fDrawBuffer->setClip(fGpu->getClip());
1618 break;
1619 default:
1620 GrCrash("Unexpected DrawCategory.");
1621 break;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001622 }
1623 return target;
1624}
1625
robertphillips@google.com72176b22012-05-23 13:19:12 +00001626/*
1627 * This method finds a path renderer that can draw the specified path on
1628 * the provided target.
1629 * Due to its expense, the software path renderer has split out so it can
1630 * can be individually allowed/disallowed via the "allowSW" boolean.
1631 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001632GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
bsalomon@google.com289533a2011-10-27 12:34:25 +00001633 GrPathFill fill,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001634 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001635 bool antiAlias,
1636 bool allowSW) {
bsalomon@google.com30085192011-08-19 15:42:31 +00001637 if (NULL == fPathRendererChain) {
1638 fPathRendererChain =
1639 new GrPathRendererChain(this, GrPathRendererChain::kNone_UsageFlag);
1640 }
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) {
1648 fSoftwarePathRenderer = new GrSoftwarePathRenderer(this);
1649 }
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.com27847de2011-02-22 20:59:41 +00001702 fGpu = gpu;
1703 fGpu->ref();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001704 fGpu->setContext(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001705
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001706 fDrawState = new GrDrawState();
1707 fGpu->setDrawState(fDrawState);
1708
bsalomon@google.com30085192011-08-19 15:42:31 +00001709 fPathRendererChain = NULL;
robertphillips@google.com72176b22012-05-23 13:19:12 +00001710 fSoftwarePathRenderer = NULL;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001711
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001712 fTextureCache = new GrResourceCache(MAX_TEXTURE_CACHE_COUNT,
1713 MAX_TEXTURE_CACHE_BYTES);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001714 fFontCache = new GrFontCache(fGpu);
1715
1716 fLastDrawCategory = kUnbuffered_DrawCategory;
1717
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001718 fDrawBuffer = NULL;
1719 fDrawBufferVBAllocPool = NULL;
1720 fDrawBufferIBAllocPool = NULL;
1721
robertphillips@google.comf6747b02012-06-12 00:32:28 +00001722 fAARectRenderer = new GrAARectRenderer;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001723
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001724 this->setupDrawBuffer();
1725}
1726
1727void GrContext::setupDrawBuffer() {
1728
1729 GrAssert(NULL == fDrawBuffer);
1730 GrAssert(NULL == fDrawBufferVBAllocPool);
1731 GrAssert(NULL == fDrawBufferIBAllocPool);
1732
bsalomon@google.com92edd312012-04-04 21:40:21 +00001733#if DEFER_TEXT_RENDERING || BATCH_RECT_TO_RECT || DEFER_PATHS
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001734 fDrawBufferVBAllocPool =
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001735 new GrVertexBufferAllocPool(fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001736 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
1737 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS);
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001738 fDrawBufferIBAllocPool =
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001739 new GrIndexBufferAllocPool(fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001740 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001741 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS);
1742
bsalomon@google.com471d4712011-08-23 15:45:25 +00001743 fDrawBuffer = new GrInOrderDrawBuffer(fGpu,
1744 fDrawBufferVBAllocPool,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001745 fDrawBufferIBAllocPool);
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001746#endif
1747
1748#if BATCH_RECT_TO_RECT
bsalomon@google.com27847de2011-02-22 20:59:41 +00001749 fDrawBuffer->setQuadIndexBuffer(this->getQuadIndexBuffer());
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001750#endif
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001751 fDrawBuffer->setAutoFlushTarget(fGpu);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001752 fDrawBuffer->setDrawState(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001753}
1754
bsalomon@google.com27847de2011-02-22 20:59:41 +00001755GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001756#if DEFER_TEXT_RENDERING
bsalomon@google.com193395c2012-03-30 17:35:12 +00001757 return prepareToDraw(paint, kBuffered_DrawCategory);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001758#else
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001759 return prepareToDraw(paint, kUnbuffered_DrawCategory);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001760#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001761}
1762
1763const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1764 return fGpu->getQuadIndexBuffer();
1765}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001766
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001767GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture,
1768 GrAutoScratchTexture* temp1,
1769 GrAutoScratchTexture* temp2,
1770 const SkRect& rect,
1771 float sigmaX, float sigmaY) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001772 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001773 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
1774 GrClip oldClip = this->getClip();
1775 GrTexture* origTexture = srcTexture;
1776 GrAutoMatrix avm(this, GrMatrix::I());
1777 SkIRect clearRect;
bsalomon@google.comb505a122012-05-31 18:40:36 +00001778 int scaleFactorX, radiusX;
1779 int scaleFactorY, radiusY;
1780 sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX);
1781 sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY);
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001782
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001783 SkRect srcRect(rect);
1784 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
1785 srcRect.roundOut();
robertphillips@google.com8637a362012-04-10 18:32:35 +00001786 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
1787 static_cast<float>(scaleFactorY));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001788 this->setClip(srcRect);
1789
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001790 GrAssert(kBGRA_8888_PM_GrPixelConfig == srcTexture->config() ||
1791 kRGBA_8888_PM_GrPixelConfig == srcTexture->config() ||
1792 kAlpha_8_GrPixelConfig == srcTexture->config());
1793
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001794 GrTextureDesc desc;
1795 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1796 desc.fWidth = SkScalarFloorToInt(srcRect.width());
1797 desc.fHeight = SkScalarFloorToInt(srcRect.height());
1798 desc.fConfig = srcTexture->config();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001799
1800 temp1->set(this, desc);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001801 if (temp2) {
1802 temp2->set(this, desc);
1803 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001804
1805 GrTexture* dstTexture = temp1->texture();
1806 GrPaint paint;
1807 paint.reset();
1808 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
1809
1810 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
1811 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1812 srcTexture->height());
1813 this->setRenderTarget(dstTexture->asRenderTarget());
1814 SkRect dstRect(srcRect);
1815 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
1816 i < scaleFactorY ? 0.5f : 1.0f);
1817 paint.setTexture(0, srcTexture);
1818 this->drawRectToRect(paint, dstRect, srcRect);
1819 srcRect = dstRect;
1820 SkTSwap(srcTexture, dstTexture);
1821 // If temp2 is non-NULL, don't render back to origTexture
robertphillips@google.com972265d2012-06-13 18:49:30 +00001822 if (temp2 && dstTexture == origTexture) {
1823 dstTexture = temp2->texture();
1824 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001825 }
1826
robertphillips@google.com7a396332012-05-10 15:11:27 +00001827 SkIRect srcIRect;
1828 srcRect.roundOut(&srcIRect);
1829
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001830 if (sigmaX > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001831 if (scaleFactorX > 1) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001832 // Clear out a radius to the right of the srcRect to prevent the
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001833 // X convolution from reading garbage.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001834 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001835 radiusX, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001836 this->clear(&clearRect, 0x0);
1837 }
1838
1839 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001840 convolve_gaussian(fGpu, srcTexture, srcRect, sigmaX, radiusX,
1841 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001842 SkTSwap(srcTexture, dstTexture);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001843 if (temp2 && dstTexture == origTexture) {
1844 dstTexture = temp2->texture();
1845 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001846 }
1847
1848 if (sigmaY > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001849 if (scaleFactorY > 1 || sigmaX > 0.0f) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001850 // Clear out a radius below the srcRect to prevent the Y
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001851 // convolution from reading garbage.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001852 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001853 srcIRect.width(), radiusY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001854 this->clear(&clearRect, 0x0);
1855 }
1856
1857 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001858 convolve_gaussian(fGpu, srcTexture, srcRect, sigmaY, radiusY,
1859 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001860 SkTSwap(srcTexture, dstTexture);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001861 if (temp2 && dstTexture == origTexture) {
1862 dstTexture = temp2->texture();
1863 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001864 }
1865
1866 if (scaleFactorX > 1 || scaleFactorY > 1) {
1867 // Clear one pixel to the right and below, to accommodate bilinear
1868 // upsampling.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001869 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
1870 srcIRect.width() + 1, 1);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001871 this->clear(&clearRect, 0x0);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001872 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
1873 1, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001874 this->clear(&clearRect, 0x0);
1875 // FIXME: This should be mitchell, not bilinear.
1876 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
1877 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1878 srcTexture->height());
1879 this->setRenderTarget(dstTexture->asRenderTarget());
1880 paint.setTexture(0, srcTexture);
1881 SkRect dstRect(srcRect);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001882 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001883 this->drawRectToRect(paint, dstRect, srcRect);
1884 srcRect = dstRect;
1885 SkTSwap(srcTexture, dstTexture);
1886 }
1887 this->setRenderTarget(oldRenderTarget);
1888 this->setClip(oldClip);
1889 return srcTexture;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001890}
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001891
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001892GrTexture* GrContext::applyMorphology(GrTexture* srcTexture,
1893 const GrRect& rect,
1894 GrTexture* temp1, GrTexture* temp2,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001895 MorphologyType morphType,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001896 SkISize radius) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001897 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001898 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
1899 GrAutoMatrix avm(this, GrMatrix::I());
1900 GrClip oldClip = this->getClip();
robertphillips@google.com7a396332012-05-10 15:11:27 +00001901 this->setClip(GrRect::MakeWH(SkIntToScalar(srcTexture->width()),
1902 SkIntToScalar(srcTexture->height())));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001903 if (radius.fWidth > 0) {
1904 this->setRenderTarget(temp1->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001905 apply_morphology(fGpu, srcTexture, rect, radius.fWidth, morphType,
1906 Gr1DKernelEffect::kX_Direction);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001907 SkIRect clearRect = SkIRect::MakeXYWH(
1908 SkScalarFloorToInt(rect.fLeft),
1909 SkScalarFloorToInt(rect.fBottom),
1910 SkScalarFloorToInt(rect.width()),
1911 radius.fHeight);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001912 this->clear(&clearRect, 0x0);
1913 srcTexture = temp1;
1914 }
1915 if (radius.fHeight > 0) {
1916 this->setRenderTarget(temp2->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001917 apply_morphology(fGpu, srcTexture, rect, radius.fHeight, morphType,
1918 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001919 srcTexture = temp2;
1920 }
1921 this->setRenderTarget(oldRenderTarget);
1922 this->setClip(oldClip);
1923 return srcTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001924}
bsalomon@google.comc4364992011-11-07 15:54:49 +00001925
robertphillips@google.com49d9fd52012-05-23 11:44:08 +00001926void GrContext::postClipPush() {
1927 fGpu->postClipPush();
1928}
1929
1930void GrContext::preClipPop() {
1931 fGpu->preClipPop();
1932};
1933
bsalomon@google.comc4364992011-11-07 15:54:49 +00001934///////////////////////////////////////////////////////////////////////////////