blob: 5a0f68982b200d76515586cb9277e7d7bd61e8a1 [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"
bsalomon@google.comc0af3172012-06-15 14:10:09 +000027#include "SkTLS.h"
tomhudson@google.com0c8d93a2011-07-01 17:08:26 +000028#include "SkTrace.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000029
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000030#define DEFER_TEXT_RENDERING 1
bsalomon@google.com27847de2011-02-22 20:59:41 +000031
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000032#define DEFER_PATHS 1
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +000033
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000034#define BATCH_RECT_TO_RECT (1 && !GR_STATIC_RECT_VB)
bsalomon@google.com27847de2011-02-22 20:59:41 +000035
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +000036#define MAX_BLUR_SIGMA 4.0f
37
bsalomon@google.comd46e2422011-09-23 17:40:07 +000038// When we're using coverage AA but the blend is incompatible (given gpu
39// limitations) should we disable AA or draw wrong?
bsalomon@google.com950d7a82011-09-28 15:05:33 +000040#define DISABLE_COVERAGE_AA_FOR_BLEND 1
bsalomon@google.comd46e2422011-09-23 17:40:07 +000041
reed@google.com4b2d3f32012-05-15 18:05:50 +000042#if GR_DEBUG
43 // change this to a 1 to see notifications when partial coverage fails
44 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
45#else
46 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
47#endif
48
bsalomon@google.com8ccaddd2011-08-09 16:49:03 +000049static const size_t MAX_TEXTURE_CACHE_COUNT = 256;
50static const size_t MAX_TEXTURE_CACHE_BYTES = 16 * 1024 * 1024;
bsalomon@google.com27847de2011-02-22 20:59:41 +000051
bsalomon@google.com60361492012-03-15 17:47:06 +000052static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
bsalomon@google.com27847de2011-02-22 20:59:41 +000053static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
54
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +000055// path rendering is the only thing we defer today that uses non-static indices
56static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = DEFER_PATHS ? 1 << 11 : 0;
57static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = DEFER_PATHS ? 4 : 0;
bsalomon@google.com27847de2011-02-22 20:59:41 +000058
bsalomon@google.combc4b6542011-11-19 13:56:11 +000059#define ASSERT_OWNED_RESOURCE(R) GrAssert(!(R) || (R)->getContext() == this)
60
bsalomon@google.com05ef5102011-05-02 21:14:59 +000061GrContext* GrContext::Create(GrEngine engine,
62 GrPlatform3DContext context3D) {
bsalomon@google.com27847de2011-02-22 20:59:41 +000063 GrContext* ctx = NULL;
64 GrGpu* fGpu = GrGpu::Create(engine, context3D);
65 if (NULL != fGpu) {
66 ctx = new GrContext(fGpu);
67 fGpu->unref();
68 }
69 return ctx;
70}
71
bsalomon@google.comc0af3172012-06-15 14:10:09 +000072namespace {
73void* CreateThreadInstanceCount() {
74 return new int(0);
75}
76void DeleteThreadInstanceCount(void* v) {
77 delete reinterpret_cast<int*>(v);
78}
79#define THREAD_INSTANCE_COUNT \
80 (*reinterpret_cast<int*>(SkTLS::Get(CreateThreadInstanceCount, \
81 DeleteThreadInstanceCount)))
82
83}
84
85int GrContext::GetThreadInstanceCount() {
86 return THREAD_INSTANCE_COUNT;
87}
88
bsalomon@google.com27847de2011-02-22 20:59:41 +000089GrContext::~GrContext() {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000090 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +000091
92 // Since the gpu can hold scratch textures, give it a chance to let go
93 // of them before freeing the texture cache
94 fGpu->purgeResources();
95
bsalomon@google.com27847de2011-02-22 20:59:41 +000096 delete fTextureCache;
97 delete fFontCache;
98 delete fDrawBuffer;
99 delete fDrawBufferVBAllocPool;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000100 delete fDrawBufferIBAllocPool;
bsalomon@google.com30085192011-08-19 15:42:31 +0000101
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000102 fAARectRenderer->unref();
103
bsalomon@google.com205d4602011-04-25 12:43:45 +0000104 fGpu->unref();
bsalomon@google.com30085192011-08-19 15:42:31 +0000105 GrSafeUnref(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000106 GrSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000107 fDrawState->unref();
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000108
109 --THREAD_INSTANCE_COUNT;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000110}
111
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000112void GrContext::contextLost() {
junov@google.com53a55842011-06-08 22:55:10 +0000113 contextDestroyed();
114 this->setupDrawBuffer();
115}
116
117void GrContext::contextDestroyed() {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000118 // abandon first to so destructors
119 // don't try to free the resources in the API.
120 fGpu->abandonResources();
121
bsalomon@google.com30085192011-08-19 15:42:31 +0000122 // a path renderer may be holding onto resources that
123 // are now unusable
124 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000125 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000126
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000127 delete fDrawBuffer;
128 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000129
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000130 delete fDrawBufferVBAllocPool;
131 fDrawBufferVBAllocPool = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000132
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000133 delete fDrawBufferIBAllocPool;
134 fDrawBufferIBAllocPool = NULL;
135
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000136 fAARectRenderer->reset();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000137
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000138 fTextureCache->removeAll();
139 fFontCache->freeAll();
140 fGpu->markContextDirty();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000141}
142
143void GrContext::resetContext() {
144 fGpu->markContextDirty();
145}
146
147void GrContext::freeGpuResources() {
148 this->flush();
robertphillips@google.comff175842012-05-14 19:31:39 +0000149
150 fGpu->purgeResources();
151
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000152 fAARectRenderer->reset();
153
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000154 fTextureCache->removeAll();
155 fFontCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000156 // a path renderer may be holding onto resources
157 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000158 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000159}
160
twiz@google.com05e70242012-01-27 19:12:00 +0000161size_t GrContext::getGpuTextureCacheBytes() const {
162 return fTextureCache->getCachedResourceBytes();
163}
164
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000165////////////////////////////////////////////////////////////////////////////////
166
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000167int GrContext::PaintStageVertexLayoutBits(
168 const GrPaint& paint,
169 const bool hasTexCoords[GrPaint::kTotalStages]) {
170 int stageMask = paint.getActiveStageMask();
171 int layout = 0;
172 for (int i = 0; i < GrPaint::kTotalStages; ++i) {
173 if ((1 << i) & stageMask) {
174 if (NULL != hasTexCoords && hasTexCoords[i]) {
175 layout |= GrDrawTarget::StageTexCoordVertexLayoutBit(i, i);
176 } else {
177 layout |= GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(i);
178 }
179 }
180 }
181 return layout;
182}
183
184
185////////////////////////////////////////////////////////////////////////////////
186
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000187GrTexture* GrContext::TextureCacheEntry::texture() const {
188 if (NULL == fEntry) {
189 return NULL;
190 } else {
191 return (GrTexture*) fEntry->resource();
192 }
193}
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000194
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000195namespace {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000196
197// we should never have more than one stencil buffer with same combo of
198// (width,height,samplecount)
199void gen_stencil_key_values(int width, int height,
200 int sampleCnt, uint32_t v[4]) {
201 v[0] = width;
202 v[1] = height;
203 v[2] = sampleCnt;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000204 v[3] = GrResourceKey::kStencilBuffer_TypeBit;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000205}
206
207void gen_stencil_key_values(const GrStencilBuffer* sb,
208 uint32_t v[4]) {
209 gen_stencil_key_values(sb->width(), sb->height(),
210 sb->numSamples(), v);
211}
bsalomon@google.com82c7bd82011-11-09 15:32:29 +0000212
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000213void scale_rect(SkRect* rect, float xScale, float yScale) {
robertphillips@google.com5af56062012-04-27 15:39:52 +0000214 rect->fLeft = SkScalarMul(rect->fLeft, SkFloatToScalar(xScale));
215 rect->fTop = SkScalarMul(rect->fTop, SkFloatToScalar(yScale));
216 rect->fRight = SkScalarMul(rect->fRight, SkFloatToScalar(xScale));
217 rect->fBottom = SkScalarMul(rect->fBottom, SkFloatToScalar(yScale));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000218}
219
bsalomon@google.comb505a122012-05-31 18:40:36 +0000220float adjust_sigma(float sigma, int *scaleFactor, int *radius) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000221 *scaleFactor = 1;
222 while (sigma > MAX_BLUR_SIGMA) {
223 *scaleFactor *= 2;
224 sigma *= 0.5f;
225 }
bsalomon@google.comb505a122012-05-31 18:40:36 +0000226 *radius = static_cast<int>(ceilf(sigma * 3.0f));
227 GrAssert(*radius <= GrConvolutionEffect::kMaxKernelRadius);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000228 return sigma;
229}
230
231void apply_morphology(GrGpu* gpu,
232 GrTexture* texture,
233 const SkRect& rect,
234 int radius,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000235 GrContext::MorphologyType morphType,
236 Gr1DKernelEffect::Direction direction) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000237
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<GrCustomStage> morph(
246 new GrMorphologyEffect(direction, radius, morphType));
247 drawState->sampler(0)->setCustomStage(morph);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000248 drawState->setTexture(0, texture);
249 gpu->drawSimpleRect(rect, NULL, 1 << 0);
250}
251
bsalomon@google.comb505a122012-05-31 18:40:36 +0000252void convolve_gaussian(GrGpu* gpu,
253 GrTexture* texture,
254 const SkRect& rect,
255 float sigma,
256 int radius,
257 Gr1DKernelEffect::Direction direction) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000258 GrRenderTarget* target = gpu->drawState()->getRenderTarget();
259 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000260 GrDrawState* drawState = gpu->drawState();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000261 drawState->setRenderTarget(target);
262 GrMatrix sampleM;
263 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000264 drawState->sampler(0)->reset(sampleM);
265 SkAutoTUnref<GrConvolutionEffect> conv(new
266 GrConvolutionEffect(direction, radius));
267 conv->setGaussianKernel(sigma);
268 drawState->sampler(0)->setCustomStage(conv);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000269 drawState->setTexture(0, texture);
270 gpu->drawSimpleRect(rect, NULL, 1 << 0);
271}
272
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000273}
274
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000275GrContext::TextureCacheEntry GrContext::findAndLockTexture(
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000276 const GrTextureDesc& desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000277 const GrSamplerState* sampler) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000278 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler, desc, false);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000279 return TextureCacheEntry(fTextureCache->findAndLock(resourceKey,
280 GrResourceCache::kNested_LockType));
281}
282
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000283bool GrContext::isTextureInCache(const GrTextureDesc& desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000284 const GrSamplerState* sampler) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000285 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler, desc, false);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000286 return fTextureCache->hasKey(resourceKey);
287}
288
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000289GrResourceEntry* GrContext::addAndLockStencilBuffer(GrStencilBuffer* sb) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000290 ASSERT_OWNED_RESOURCE(sb);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000291 uint32_t v[4];
292 gen_stencil_key_values(sb, v);
293 GrResourceKey resourceKey(v);
294 return fTextureCache->createAndLock(resourceKey, sb);
295}
296
297GrStencilBuffer* GrContext::findStencilBuffer(int width, int height,
298 int sampleCnt) {
299 uint32_t v[4];
300 gen_stencil_key_values(width, height, sampleCnt, v);
301 GrResourceKey resourceKey(v);
302 GrResourceEntry* entry = fTextureCache->findAndLock(resourceKey,
303 GrResourceCache::kSingle_LockType);
304 if (NULL != entry) {
305 GrStencilBuffer* sb = (GrStencilBuffer*) entry->resource();
306 return sb;
307 } else {
308 return NULL;
309 }
310}
311
312void GrContext::unlockStencilBuffer(GrResourceEntry* sbEntry) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000313 ASSERT_OWNED_RESOURCE(sbEntry->resource());
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000314 fTextureCache->unlock(sbEntry);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000315}
316
317static void stretchImage(void* dst,
318 int dstW,
319 int dstH,
320 void* src,
321 int srcW,
322 int srcH,
323 int bpp) {
324 GrFixed dx = (srcW << 16) / dstW;
325 GrFixed dy = (srcH << 16) / dstH;
326
327 GrFixed y = dy >> 1;
328
329 int dstXLimit = dstW*bpp;
330 for (int j = 0; j < dstH; ++j) {
331 GrFixed x = dx >> 1;
332 void* srcRow = (uint8_t*)src + (y>>16)*srcW*bpp;
333 void* dstRow = (uint8_t*)dst + j*dstW*bpp;
334 for (int i = 0; i < dstXLimit; i += bpp) {
335 memcpy((uint8_t*) dstRow + i,
336 (uint8_t*) srcRow + (x>>16)*bpp,
337 bpp);
338 x += dx;
339 }
340 y += dy;
341 }
342}
343
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000344GrContext::TextureCacheEntry GrContext::createAndLockTexture(
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000345 const GrSamplerState* sampler,
346 const GrTextureDesc& desc,
347 void* srcData,
348 size_t rowBytes) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000349 SK_TRACE_EVENT0("GrContext::createAndLockTexture");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000350
351#if GR_DUMP_TEXTURE_UPLOAD
352 GrPrintf("GrContext::createAndLockTexture [%d %d]\n", desc.fWidth, desc.fHeight);
353#endif
354
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000355 TextureCacheEntry entry;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000356
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000357 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000358 desc, false);
359
360 if (GrTexture::NeedsResizing(resourceKey)) {
361 // The desired texture is NPOT and tiled but that isn't supported by
362 // the current hardware. Resize the texture to be a POT
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000363 GrAssert(NULL != sampler);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000364 TextureCacheEntry clampEntry = this->findAndLockTexture(desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000365 NULL);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000366
367 if (NULL == clampEntry.texture()) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000368 clampEntry = this->createAndLockTexture(NULL, desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000369 srcData, rowBytes);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000370 GrAssert(NULL != clampEntry.texture());
371 if (NULL == clampEntry.texture()) {
372 return entry;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000373 }
374 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000375 GrTextureDesc rtDesc = desc;
376 rtDesc.fFlags = rtDesc.fFlags |
377 kRenderTarget_GrTextureFlagBit |
378 kNoStencil_GrTextureFlagBit;
bsalomon@google.com99621082011-11-15 16:47:16 +0000379 rtDesc.fWidth = GrNextPow2(GrMax(desc.fWidth, 64));
380 rtDesc.fHeight = GrNextPow2(GrMax(desc.fHeight, 64));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000381
382 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
383
384 if (NULL != texture) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000385 GrDrawTarget::AutoStateRestore asr(fGpu,
386 GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000387 GrDrawState* drawState = fGpu->drawState();
388 drawState->setRenderTarget(texture->asRenderTarget());
389 drawState->setTexture(0, clampEntry.texture());
bsalomon@google.com82c7bd82011-11-09 15:32:29 +0000390
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000391 GrSamplerState::Filter filter;
392 // if filtering is not desired then we want to ensure all
393 // texels in the resampled image are copies of texels from
394 // the original.
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000395 if (GrTexture::NeedsFiltering(resourceKey)) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000396 filter = GrSamplerState::kBilinear_Filter;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000397 } else {
398 filter = GrSamplerState::kNearest_Filter;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000399 }
bsalomon@google.com1e266f82011-12-12 16:11:33 +0000400 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
401 filter);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000402
403 static const GrVertexLayout layout =
404 GrDrawTarget::StageTexCoordVertexLayoutBit(0,0);
405 GrDrawTarget::AutoReleaseGeometry arg(fGpu, layout, 4, 0);
406
407 if (arg.succeeded()) {
408 GrPoint* verts = (GrPoint*) arg.vertices();
409 verts[0].setIRectFan(0, 0,
410 texture->width(),
411 texture->height(),
412 2*sizeof(GrPoint));
413 verts[1].setIRectFan(0, 0, 1, 1, 2*sizeof(GrPoint));
bsalomon@google.com47059542012-06-06 20:51:20 +0000414 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000415 0, 4);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000416 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000417 }
bsalomon@google.com1da07462011-03-10 14:51:57 +0000418 texture->releaseRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000419 } else {
420 // TODO: Our CPU stretch doesn't filter. But we create separate
421 // stretched textures when the sampler state is either filtered or
422 // not. Either implement filtered stretch blit on CPU or just create
423 // one when FBO case fails.
424
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000425 rtDesc.fFlags = kNone_GrTextureFlags;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000426 // no longer need to clamp at min RT size.
427 rtDesc.fWidth = GrNextPow2(desc.fWidth);
428 rtDesc.fHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000429 int bpp = GrBytesPerPixel(desc.fConfig);
bsalomon@google.com3582bf92011-06-30 21:32:31 +0000430 SkAutoSMalloc<128*128*4> stretchedPixels(bpp *
bsalomon@google.com27847de2011-02-22 20:59:41 +0000431 rtDesc.fWidth *
432 rtDesc.fHeight);
433 stretchImage(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
434 srcData, desc.fWidth, desc.fHeight, bpp);
435
436 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
437
438 GrTexture* texture = fGpu->createTexture(rtDesc,
439 stretchedPixels.get(),
440 stretchedRowBytes);
441 GrAssert(NULL != texture);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000442 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000443 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000444 fTextureCache->unlock(clampEntry.cacheEntry());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000445
446 } else {
447 GrTexture* texture = fGpu->createTexture(desc, srcData, rowBytes);
448 if (NULL != texture) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000449 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000450 }
451 }
452 return entry;
453}
454
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000455GrContext::TextureCacheEntry GrContext::lockScratchTexture(
456 const GrTextureDesc& inDesc,
457 ScratchTexMatch match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000458 GrTextureDesc desc = inDesc;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000459 desc.fClientCacheID = kScratch_CacheID;
460
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000461 if (kExact_ScratchTexMatch != match) {
462 // bin by pow2 with a reasonable min
463 static const int MIN_SIZE = 256;
464 desc.fWidth = GrMax(MIN_SIZE, GrNextPow2(desc.fWidth));
465 desc.fHeight = GrMax(MIN_SIZE, GrNextPow2(desc.fHeight));
466 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000467
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000468 GrResourceEntry* entry;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000469 int origWidth = desc.fWidth;
470 int origHeight = desc.fHeight;
471 bool doubledW = false;
472 bool doubledH = false;
473
474 do {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000475 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL, desc, true);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000476 entry = fTextureCache->findAndLock(key,
477 GrResourceCache::kNested_LockType);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000478 // if we miss, relax the fit of the flags...
479 // then try doubling width... then height.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000480 if (NULL != entry || kExact_ScratchTexMatch == match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000481 break;
482 }
483 if (!(desc.fFlags & kRenderTarget_GrTextureFlagBit)) {
484 desc.fFlags = desc.fFlags | kRenderTarget_GrTextureFlagBit;
485 } else if (desc.fFlags & kNoStencil_GrTextureFlagBit) {
486 desc.fFlags = desc.fFlags & ~kNoStencil_GrTextureFlagBit;
487 } else if (!doubledW) {
488 desc.fFlags = inDesc.fFlags;
489 desc.fWidth *= 2;
490 doubledW = true;
491 } else if (!doubledH) {
492 desc.fFlags = inDesc.fFlags;
493 desc.fWidth = origWidth;
494 desc.fHeight *= 2;
495 doubledH = true;
496 } else {
497 break;
498 }
499
500 } while (true);
501
502 if (NULL == entry) {
503 desc.fFlags = inDesc.fFlags;
504 desc.fWidth = origWidth;
505 desc.fHeight = origHeight;
506 GrTexture* texture = fGpu->createTexture(desc, NULL, 0);
507 if (NULL != texture) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000508 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000509 texture->desc(),
510 true);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000511 entry = fTextureCache->createAndLock(key, texture);
512 }
513 }
514
515 // If the caller gives us the same desc/sampler twice we don't want
516 // to return the same texture the second time (unless it was previously
517 // released). So we detach the entry from the cache and reattach at release.
518 if (NULL != entry) {
519 fTextureCache->detach(entry);
520 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000521 return TextureCacheEntry(entry);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000522}
523
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000524void GrContext::unlockTexture(TextureCacheEntry entry) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000525 ASSERT_OWNED_RESOURCE(entry.texture());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000526 // If this is a scratch texture we detached it from the cache
527 // while it was locked (to avoid two callers simultaneously getting
528 // the same texture).
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000529 if (GrTexture::IsScratchTexture(entry.cacheEntry()->key())) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000530 fTextureCache->reattachAndUnlock(entry.cacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000531 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000532 fTextureCache->unlock(entry.cacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000533 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000534}
535
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000536GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000537 void* srcData,
538 size_t rowBytes) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000539 GrTextureDesc descCopy = descIn;
540 descCopy.fClientCacheID = kUncached_CacheID;
541 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000542}
543
544void GrContext::getTextureCacheLimits(int* maxTextures,
545 size_t* maxTextureBytes) const {
546 fTextureCache->getLimits(maxTextures, maxTextureBytes);
547}
548
549void GrContext::setTextureCacheLimits(int maxTextures, size_t maxTextureBytes) {
550 fTextureCache->setLimits(maxTextures, maxTextureBytes);
551}
552
bsalomon@google.com91958362011-06-13 17:58:13 +0000553int GrContext::getMaxTextureSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000554 return fGpu->getCaps().fMaxTextureSize;
bsalomon@google.com91958362011-06-13 17:58:13 +0000555}
556
557int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000558 return fGpu->getCaps().fMaxRenderTargetSize;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000559}
560
561///////////////////////////////////////////////////////////////////////////////
562
bsalomon@google.come269f212011-11-07 13:29:52 +0000563GrTexture* GrContext::createPlatformTexture(const GrPlatformTextureDesc& desc) {
564 return fGpu->createPlatformTexture(desc);
565}
566
567GrRenderTarget* GrContext::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
568 return fGpu->createPlatformRenderTarget(desc);
569}
570
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000571///////////////////////////////////////////////////////////////////////////////
572
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000573bool GrContext::supportsIndex8PixelConfig(const GrSamplerState* sampler,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000574 int width, int height) const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000575 const GrDrawTarget::Caps& caps = fGpu->getCaps();
576 if (!caps.f8BitPaletteSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000577 return false;
578 }
579
bsalomon@google.com27847de2011-02-22 20:59:41 +0000580 bool isPow2 = GrIsPow2(width) && GrIsPow2(height);
581
582 if (!isPow2) {
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000583 bool tiled = NULL != sampler &&
584 (sampler->getWrapX() != GrSamplerState::kClamp_WrapMode ||
585 sampler->getWrapY() != GrSamplerState::kClamp_WrapMode);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000586 if (tiled && !caps.fNPOTTextureTileSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000587 return false;
588 }
589 }
590 return true;
591}
592
593////////////////////////////////////////////////////////////////////////////////
594
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000595const GrClip& GrContext::getClip() const { return fGpu->getClip(); }
596
bsalomon@google.com27847de2011-02-22 20:59:41 +0000597void GrContext::setClip(const GrClip& clip) {
598 fGpu->setClip(clip);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000599 fDrawState->enableState(GrDrawState::kClip_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000600}
601
602void GrContext::setClip(const GrIRect& rect) {
603 GrClip clip;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000604 clip.setFromIRect(rect);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000605 fGpu->setClip(clip);
606}
607
608////////////////////////////////////////////////////////////////////////////////
609
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000610void GrContext::clear(const GrIRect* rect, const GrColor color) {
bsalomon@google.com398109c2011-04-14 18:40:27 +0000611 this->flush();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000612 fGpu->clear(rect, color);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000613}
614
615void GrContext::drawPaint(const GrPaint& paint) {
616 // set rect to be big enough to fill the space, but not super-huge, so we
617 // don't overflow fixed-point implementations
bsalomon@google.comd302f142011-03-03 13:54:13 +0000618 GrRect r;
619 r.setLTRB(0, 0,
620 GrIntToScalar(getRenderTarget()->width()),
621 GrIntToScalar(getRenderTarget()->height()));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000622 GrMatrix inverse;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000623 SkTLazy<GrPaint> tmpPaint;
624 const GrPaint* p = &paint;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000625 GrAutoMatrix am;
626
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000627 // We attempt to map r by the inverse matrix and draw that. mapRect will
628 // map the four corners and bound them with a new rect. This will not
629 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000630 if (!this->getMatrix().hasPerspective()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000631 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000632 GrPrintf("Could not invert matrix");
633 return;
634 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000635 inverse.mapRect(&r);
636 } else {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000637 if (paint.getActiveMaskStageMask() || paint.getActiveStageMask()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000638 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000639 GrPrintf("Could not invert matrix");
640 return;
641 }
642 tmpPaint.set(paint);
643 tmpPaint.get()->preConcatActiveSamplerMatrices(inverse);
644 p = tmpPaint.get();
645 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000646 am.set(this, GrMatrix::I());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000647 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000648 // by definition this fills the entire clip, no need for AA
649 if (paint.fAntiAlias) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000650 if (!tmpPaint.isValid()) {
651 tmpPaint.set(paint);
652 p = tmpPaint.get();
653 }
654 GrAssert(p == tmpPaint.get());
655 tmpPaint.get()->fAntiAlias = false;
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000656 }
657 this->drawRect(*p, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000658}
659
bsalomon@google.com205d4602011-04-25 12:43:45 +0000660////////////////////////////////////////////////////////////////////////////////
661
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000662namespace {
663inline bool disable_coverage_aa_for_blend(GrDrawTarget* target) {
664 return DISABLE_COVERAGE_AA_FOR_BLEND && !target->canApplyCoverage();
665}
666}
667
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000668////////////////////////////////////////////////////////////////////////////////
669
bsalomon@google.com27847de2011-02-22 20:59:41 +0000670/* create a triangle strip that strokes the specified triangle. There are 8
671 unique vertices, but we repreat the last 2 to close up. Alternatively we
672 could use an indices array, and then only send 8 verts, but not sure that
673 would be faster.
674 */
bsalomon@google.com205d4602011-04-25 12:43:45 +0000675static void setStrokeRectStrip(GrPoint verts[10], GrRect rect,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000676 GrScalar width) {
677 const GrScalar rad = GrScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000678 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000679
680 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
681 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
682 verts[2].set(rect.fRight - rad, rect.fTop + rad);
683 verts[3].set(rect.fRight + rad, rect.fTop - rad);
684 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
685 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
686 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
687 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
688 verts[8] = verts[0];
689 verts[9] = verts[1];
690}
691
reed@google.com20efde72011-05-09 17:00:02 +0000692/**
693 * Returns true if the rects edges are integer-aligned.
694 */
695static bool isIRect(const GrRect& r) {
696 return GrScalarIsInt(r.fLeft) && GrScalarIsInt(r.fTop) &&
697 GrScalarIsInt(r.fRight) && GrScalarIsInt(r.fBottom);
698}
699
bsalomon@google.com205d4602011-04-25 12:43:45 +0000700static bool apply_aa_to_rect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000701 const GrRect& rect,
702 GrScalar width,
703 const GrMatrix* matrix,
704 GrMatrix* combinedMatrix,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000705 GrRect* devRect,
706 bool* useVertexCoverage) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000707 // we use a simple coverage ramp to do aa on axis-aligned rects
708 // we check if the rect will be axis-aligned, and the rect won't land on
709 // integer coords.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000710
bsalomon@google.coma3108262011-10-10 14:08:47 +0000711 // we are keeping around the "tweak the alpha" trick because
712 // it is our only hope for the fixed-pipe implementation.
713 // In a shader implementation we can give a separate coverage input
bsalomon@google.com289533a2011-10-27 12:34:25 +0000714 // TODO: remove this ugliness when we drop the fixed-pipe impl
bsalomon@google.coma3108262011-10-10 14:08:47 +0000715 *useVertexCoverage = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000716 if (!target->canTweakAlphaForCoverage()) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000717 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +0000718#if GR_DEBUG
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000719 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000720#endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000721 return false;
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000722 } else {
723 *useVertexCoverage = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000724 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000725 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000726 const GrDrawState& drawState = target->getDrawState();
727 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000728 return false;
729 }
730
bsalomon@google.com471d4712011-08-23 15:45:25 +0000731 if (0 == width && target->willUseHWAALines()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000732 return false;
733 }
734
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000735 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000736 return false;
737 }
738
739 if (NULL != matrix &&
740 !matrix->preservesAxisAlignment()) {
741 return false;
742 }
743
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000744 *combinedMatrix = drawState.getViewMatrix();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000745 if (NULL != matrix) {
746 combinedMatrix->preConcat(*matrix);
747 GrAssert(combinedMatrix->preservesAxisAlignment());
748 }
749
750 combinedMatrix->mapRect(devRect, rect);
751 devRect->sort();
752
753 if (width < 0) {
reed@google.com20efde72011-05-09 17:00:02 +0000754 return !isIRect(*devRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000755 } else {
756 return true;
757 }
758}
759
bsalomon@google.com27847de2011-02-22 20:59:41 +0000760void GrContext::drawRect(const GrPaint& paint,
761 const GrRect& rect,
762 GrScalar width,
763 const GrMatrix* matrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000764 SK_TRACE_EVENT0("GrContext::drawRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000765
766 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
robertphillips@google.com972265d2012-06-13 18:49:30 +0000767 GrDrawState::AutoTextureRelease atr(fDrawState);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000768 int stageMask = paint.getActiveStageMask();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000769
bsalomon@google.com205d4602011-04-25 12:43:45 +0000770 GrRect devRect = rect;
771 GrMatrix combinedMatrix;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000772 bool useVertexCoverage;
bsalomon@google.com289533a2011-10-27 12:34:25 +0000773 bool needAA = paint.fAntiAlias &&
774 !this->getRenderTarget()->isMultisampled();
775 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix,
776 &combinedMatrix, &devRect,
777 &useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000778
779 if (doAA) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000780 GrDrawTarget::AutoDeviceCoordDraw adcd(target, stageMask);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000781 if (width >= 0) {
782 GrVec strokeSize;;
783 if (width > 0) {
784 strokeSize.set(width, width);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000785 combinedMatrix.mapVectors(&strokeSize, 1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000786 strokeSize.setAbs(strokeSize);
787 } else {
788 strokeSize.set(GR_Scalar1, GR_Scalar1);
789 }
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000790 fAARectRenderer->strokeAARect(this->getGpu(), target, devRect,
791 strokeSize, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000792 } else {
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000793 fAARectRenderer->fillAARect(this->getGpu(), target,
794 devRect, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000795 }
796 return;
797 }
798
bsalomon@google.com27847de2011-02-22 20:59:41 +0000799 if (width >= 0) {
800 // TODO: consider making static vertex buffers for these cases.
801 // Hairline could be done by just adding closing vertex to
802 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000803 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
804
bsalomon@google.com27847de2011-02-22 20:59:41 +0000805 static const int worstCaseVertCount = 10;
806 GrDrawTarget::AutoReleaseGeometry geo(target, layout, worstCaseVertCount, 0);
807
808 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000809 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000810 return;
811 }
812
813 GrPrimitiveType primType;
814 int vertCount;
815 GrPoint* vertex = geo.positions();
816
817 if (width > 0) {
818 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000819 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000820 setStrokeRectStrip(vertex, rect, width);
821 } else {
822 // hairline
823 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000824 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000825 vertex[0].set(rect.fLeft, rect.fTop);
826 vertex[1].set(rect.fRight, rect.fTop);
827 vertex[2].set(rect.fRight, rect.fBottom);
828 vertex[3].set(rect.fLeft, rect.fBottom);
829 vertex[4].set(rect.fLeft, rect.fTop);
830 }
831
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000832 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000833 if (NULL != matrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000834 GrDrawState* drawState = target->drawState();
835 avmr.set(drawState);
836 drawState->preConcatViewMatrix(*matrix);
837 drawState->preConcatSamplerMatrices(stageMask, *matrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000838 }
839
840 target->drawNonIndexed(primType, 0, vertCount);
841 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000842#if GR_STATIC_RECT_VB
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000843 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000844 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
845 if (NULL == sqVB) {
846 GrPrintf("Failed to create static rect vb.\n");
847 return;
848 }
849 target->setVertexSourceToBuffer(layout, sqVB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000850 GrDrawState* drawState = target->drawState();
851 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000852 GrMatrix m;
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000853 m.setAll(rect.width(), 0, rect.fLeft,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000854 0, rect.height(), rect.fTop,
855 0, 0, GrMatrix::I()[8]);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000856
857 if (NULL != matrix) {
858 m.postConcat(*matrix);
859 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000860 drawState->preConcatViewMatrix(m);
861 drawState->preConcatSamplerMatrices(stageMask, m);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000862
bsalomon@google.com47059542012-06-06 20:51:20 +0000863 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000864#else
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000865 target->drawSimpleRect(rect, matrix, stageMask);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000866#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000867 }
868}
869
870void GrContext::drawRectToRect(const GrPaint& paint,
871 const GrRect& dstRect,
872 const GrRect& srcRect,
873 const GrMatrix* dstMatrix,
874 const GrMatrix* srcMatrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000875 SK_TRACE_EVENT0("GrContext::drawRectToRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000876
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000877 // srcRect refers to paint's first texture
878 if (NULL == paint.getTexture(0)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000879 drawRect(paint, dstRect, -1, dstMatrix);
880 return;
881 }
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000882
bsalomon@google.com27847de2011-02-22 20:59:41 +0000883 GR_STATIC_ASSERT(!BATCH_RECT_TO_RECT || !GR_STATIC_RECT_VB);
884
885#if GR_STATIC_RECT_VB
886 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
robertphillips@google.com972265d2012-06-13 18:49:30 +0000887 GrDrawState::AutoTextureRelease atr(fDrawState);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000888 GrDrawState* drawState = target->drawState();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000889 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000890 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000891
892 GrMatrix m;
893
894 m.setAll(dstRect.width(), 0, dstRect.fLeft,
895 0, dstRect.height(), dstRect.fTop,
896 0, 0, GrMatrix::I()[8]);
897 if (NULL != dstMatrix) {
898 m.postConcat(*dstMatrix);
899 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000900 drawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000901
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000902 // srcRect refers to first stage
903 int otherStageMask = paint.getActiveStageMask() &
904 (~(1 << GrPaint::kFirstTextureStage));
905 if (otherStageMask) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000906 drawState->preConcatSamplerMatrices(otherStageMask, m);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000907 }
908
bsalomon@google.com27847de2011-02-22 20:59:41 +0000909 m.setAll(srcRect.width(), 0, srcRect.fLeft,
910 0, srcRect.height(), srcRect.fTop,
911 0, 0, GrMatrix::I()[8]);
912 if (NULL != srcMatrix) {
913 m.postConcat(*srcMatrix);
914 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000915 drawState->sampler(GrPaint::kFirstTextureStage)->preConcatMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000916
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000917 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
918 if (NULL == sqVB) {
919 GrPrintf("Failed to create static rect vb.\n");
920 return;
921 }
922 target->setVertexSourceToBuffer(layout, sqVB);
bsalomon@google.com47059542012-06-06 20:51:20 +0000923 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000924#else
925
926 GrDrawTarget* target;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000927#if BATCH_RECT_TO_RECT
bsalomon@google.com27847de2011-02-22 20:59:41 +0000928 target = this->prepareToDraw(paint, kBuffered_DrawCategory);
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000929#else
bsalomon@google.com27847de2011-02-22 20:59:41 +0000930 target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
931#endif
robertphillips@google.com972265d2012-06-13 18:49:30 +0000932 GrDrawState::AutoTextureRelease atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000933
tomhudson@google.com93813632011-10-27 20:21:16 +0000934 const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
935 const GrMatrix* srcMatrices[GrDrawState::kNumStages] = {NULL};
bsalomon@google.com27847de2011-02-22 20:59:41 +0000936 srcRects[0] = &srcRect;
937 srcMatrices[0] = srcMatrix;
938
939 target->drawRect(dstRect, dstMatrix, 1, srcRects, srcMatrices);
940#endif
941}
942
943void GrContext::drawVertices(const GrPaint& paint,
944 GrPrimitiveType primitiveType,
945 int vertexCount,
946 const GrPoint positions[],
947 const GrPoint texCoords[],
948 const GrColor colors[],
949 const uint16_t indices[],
950 int indexCount) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000951 SK_TRACE_EVENT0("GrContext::drawVertices");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000952
953 GrDrawTarget::AutoReleaseGeometry geo;
954
955 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
robertphillips@google.com972265d2012-06-13 18:49:30 +0000956 GrDrawState::AutoTextureRelease atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000957
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000958 bool hasTexCoords[GrPaint::kTotalStages] = {
959 NULL != texCoords, // texCoordSrc provides explicit stage 0 coords
960 0 // remaining stages use positions
961 };
962
963 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, hasTexCoords);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000964
965 if (NULL != colors) {
966 layout |= GrDrawTarget::kColor_VertexLayoutBit;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000967 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000968 int vertexSize = GrDrawTarget::VertexSize(layout);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000969
970 if (sizeof(GrPoint) != vertexSize) {
971 if (!geo.set(target, layout, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000972 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000973 return;
974 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000975 int texOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000976 int colorOffset;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000977 GrDrawTarget::VertexSizeAndOffsetsByIdx(layout,
978 texOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000979 &colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000980 NULL,
981 NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000982 void* curVertex = geo.vertices();
983
984 for (int i = 0; i < vertexCount; ++i) {
985 *((GrPoint*)curVertex) = positions[i];
986
987 if (texOffsets[0] > 0) {
988 *(GrPoint*)((intptr_t)curVertex + texOffsets[0]) = texCoords[i];
989 }
990 if (colorOffset > 0) {
991 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
992 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000993 curVertex = (void*)((intptr_t)curVertex + vertexSize);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000994 }
995 } else {
996 target->setVertexSourceToArray(layout, positions, vertexCount);
997 }
998
bsalomon@google.com91958362011-06-13 17:58:13 +0000999 // we don't currently apply offscreen AA to this path. Need improved
1000 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +00001001
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001002 if (NULL != indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001003 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001004 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001005 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001006 target->drawNonIndexed(primitiveType, 0, vertexCount);
1007 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001008}
1009
bsalomon@google.com06afe7b2011-04-26 15:31:40 +00001010///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com150d2842012-01-12 20:19:56 +00001011namespace {
1012
bsalomon@google.com93c96602012-04-27 13:05:21 +00001013struct CircleVertex {
1014 GrPoint fPos;
1015 GrPoint fCenter;
1016 GrScalar fOuterRadius;
1017 GrScalar fInnerRadius;
1018};
1019
1020/* Returns true if will map a circle to another circle. This can be true
1021 * if the matrix only includes square-scale, rotation, translation.
1022 */
1023inline bool isSimilarityTransformation(const SkMatrix& matrix,
1024 SkScalar tol = SK_ScalarNearlyZero) {
1025 if (matrix.isIdentity() || matrix.getType() == SkMatrix::kTranslate_Mask) {
1026 return true;
1027 }
1028 if (matrix.hasPerspective()) {
1029 return false;
1030 }
1031
1032 SkScalar mx = matrix.get(SkMatrix::kMScaleX);
1033 SkScalar sx = matrix.get(SkMatrix::kMSkewX);
1034 SkScalar my = matrix.get(SkMatrix::kMScaleY);
1035 SkScalar sy = matrix.get(SkMatrix::kMSkewY);
1036
1037 if (mx == 0 && sx == 0 && my == 0 && sy == 0) {
1038 return false;
1039 }
1040
1041 // it has scales or skews, but it could also be rotation, check it out.
1042 SkVector vec[2];
1043 vec[0].set(mx, sx);
1044 vec[1].set(sy, my);
1045
1046 return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) &&
1047 SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(),
1048 SkScalarSquare(tol));
1049}
1050
1051}
1052
1053// TODO: strokeWidth can't be larger than zero right now.
1054// It will be fixed when drawPath() can handle strokes.
1055void GrContext::drawOval(const GrPaint& paint,
1056 const GrRect& rect,
1057 SkScalar strokeWidth) {
1058 DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
1059 kUnbuffered_DrawCategory;
1060 GrDrawTarget* target = this->prepareToDraw(paint, category);
robertphillips@google.com972265d2012-06-13 18:49:30 +00001061 GrDrawState::AutoTextureRelease atr(fDrawState);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001062 GrDrawState* drawState = target->drawState();
1063 GrMatrix vm = drawState->getViewMatrix();
1064
1065 if (!isSimilarityTransformation(vm) ||
1066 !paint.fAntiAlias ||
1067 rect.height() != rect.width()) {
1068 SkPath path;
1069 path.addOval(rect);
1070 GrPathFill fill = (strokeWidth == 0) ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001071 kHairLine_GrPathFill : kWinding_GrPathFill;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001072 this->internalDrawPath(paint, path, fill, NULL);
1073 return;
1074 }
1075
1076 const GrRenderTarget* rt = drawState->getRenderTarget();
1077 if (NULL == rt) {
1078 return;
1079 }
1080
1081 GrDrawTarget::AutoDeviceCoordDraw adcd(target, paint.getActiveStageMask());
1082
1083 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
1084 layout |= GrDrawTarget::kEdge_VertexLayoutBit;
1085 GrAssert(sizeof(CircleVertex) == GrDrawTarget::VertexSize(layout));
1086
1087 GrPoint center = GrPoint::Make(rect.centerX(), rect.centerY());
1088 GrScalar radius = SkScalarHalf(rect.width());
1089
1090 vm.mapPoints(&center, 1);
1091 radius = vm.mapRadius(radius);
1092
1093 GrScalar outerRadius = radius;
1094 GrScalar innerRadius = 0;
1095 SkScalar halfWidth = 0;
1096 if (strokeWidth == 0) {
1097 halfWidth = SkScalarHalf(SK_Scalar1);
1098
1099 outerRadius += halfWidth;
1100 innerRadius = SkMaxScalar(0, radius - halfWidth);
1101 }
1102
1103 GrDrawTarget::AutoReleaseGeometry geo(target, layout, 4, 0);
1104 if (!geo.succeeded()) {
1105 GrPrintf("Failed to get space for vertices!\n");
1106 return;
1107 }
1108
1109 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
1110
1111 SkScalar L = center.fX - outerRadius;
1112 SkScalar R = center.fX + outerRadius;
1113 SkScalar T = center.fY - outerRadius;
1114 SkScalar B = center.fY + outerRadius;
1115
1116 verts[0].fPos = SkPoint::Make(L, T);
1117 verts[1].fPos = SkPoint::Make(R, T);
1118 verts[2].fPos = SkPoint::Make(L, B);
1119 verts[3].fPos = SkPoint::Make(R, B);
1120
1121 for (int i = 0; i < 4; ++i) {
1122 // this goes to fragment shader, it should be in y-points-up space.
1123 verts[i].fCenter = SkPoint::Make(center.fX, rt->height() - center.fY);
1124
1125 verts[i].fOuterRadius = outerRadius;
1126 verts[i].fInnerRadius = innerRadius;
1127 }
1128
1129 drawState->setVertexEdgeType(GrDrawState::kCircle_EdgeType);
bsalomon@google.com47059542012-06-06 20:51:20 +00001130 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001131}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001132
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001133void GrContext::drawPath(const GrPaint& paint, const SkPath& path,
reed@google.com07f3ee12011-05-16 17:21:57 +00001134 GrPathFill fill, const GrPoint* translate) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001135
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001136 if (path.isEmpty()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001137 if (GrIsFillInverted(fill)) {
1138 this->drawPaint(paint);
1139 }
1140 return;
1141 }
1142
bsalomon@google.com93c96602012-04-27 13:05:21 +00001143 SkRect ovalRect;
1144 if (!GrIsFillInverted(fill) && path.isOval(&ovalRect)) {
1145 if (translate) {
1146 ovalRect.offset(*translate);
1147 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001148 SkScalar width = (fill == kHairLine_GrPathFill) ? 0 : -SK_Scalar1;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001149 this->drawOval(paint, ovalRect, width);
1150 return;
1151 }
1152
1153 internalDrawPath(paint, path, fill, translate);
1154}
1155
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001156void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +00001157 GrPathFill fill, const GrPoint* translate) {
1158
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001159 // Note that below we may sw-rasterize the path into a scratch texture.
1160 // Scratch textures can be recycled after they are returned to the texture
1161 // cache. This presents a potential hazard for buffered drawing. However,
1162 // the writePixels that uploads to the scratch will perform a flush so we're
1163 // OK.
1164 DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
1165 kUnbuffered_DrawCategory;
1166 GrDrawTarget* target = this->prepareToDraw(paint, category);
robertphillips@google.com972265d2012-06-13 18:49:30 +00001167 GrDrawState::AutoTextureRelease atr(fDrawState);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001168 GrDrawState::StageMask stageMask = paint.getActiveStageMask();
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001169
bsalomon@google.com289533a2011-10-27 12:34:25 +00001170 bool prAA = paint.fAntiAlias && !this->getRenderTarget()->isMultisampled();
1171
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001172 // An Assumption here is that path renderer would use some form of tweaking
1173 // the src color (either the input alpha or in the frag shader) to implement
1174 // aa. If we have some future driver-mojo path AA that can do the right
1175 // thing WRT to the blend then we'll need some query on the PR.
1176 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001177#if GR_DEBUG
bsalomon@google.com979432b2011-11-05 21:38:22 +00001178 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001179#endif
bsalomon@google.com289533a2011-10-27 12:34:25 +00001180 prAA = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001181 }
bsalomon@google.com289533a2011-10-27 12:34:25 +00001182
robertphillips@google.com72176b22012-05-23 13:19:12 +00001183 GrPathRenderer* pr = this->getPathRenderer(path, fill, target, prAA, true);
bsalomon@google.com30085192011-08-19 15:42:31 +00001184 if (NULL == pr) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001185#if GR_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001186 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001187#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001188 return;
1189 }
1190
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001191 pr->drawPath(path, fill, translate, target, stageMask, prAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001192}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001193
bsalomon@google.com27847de2011-02-22 20:59:41 +00001194////////////////////////////////////////////////////////////////////////////////
1195
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001196void GrContext::flush(int flagsBitfield) {
1197 if (kDiscard_FlushBit & flagsBitfield) {
1198 fDrawBuffer->reset();
1199 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001200 this->flushDrawBuffer();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001201 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001202 if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001203 fGpu->forceRenderTargetFlush();
1204 }
1205}
1206
bsalomon@google.com27847de2011-02-22 20:59:41 +00001207void GrContext::flushDrawBuffer() {
junov@google.com53a55842011-06-08 22:55:10 +00001208 if (fDrawBuffer) {
robertphillips@google.com58b38182012-05-03 16:29:41 +00001209 // With addition of the AA clip path, flushing the draw buffer can
1210 // result in the generation of an AA clip mask. During this
1211 // process the SW path renderer may be invoked which recusively
1212 // calls this method (via internalWriteTexturePixels) creating
1213 // infinite recursion
1214 GrInOrderDrawBuffer* temp = fDrawBuffer;
1215 fDrawBuffer = NULL;
1216
1217 temp->flushTo(fGpu);
1218
1219 fDrawBuffer = temp;
junov@google.com53a55842011-06-08 22:55:10 +00001220 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001221}
1222
bsalomon@google.com6f379512011-11-16 20:36:03 +00001223void GrContext::internalWriteTexturePixels(GrTexture* texture,
1224 int left, int top,
1225 int width, int height,
1226 GrPixelConfig config,
1227 const void* buffer,
1228 size_t rowBytes,
1229 uint32_t flags) {
1230 SK_TRACE_EVENT0("GrContext::writeTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001231 ASSERT_OWNED_RESOURCE(texture);
1232
bsalomon@google.com6f379512011-11-16 20:36:03 +00001233 if (!(kDontFlush_PixelOpsFlag & flags)) {
1234 this->flush();
1235 }
1236 // TODO: use scratch texture to perform conversion
1237 if (GrPixelConfigIsUnpremultiplied(texture->config()) !=
1238 GrPixelConfigIsUnpremultiplied(config)) {
1239 return;
1240 }
1241
1242 fGpu->writeTexturePixels(texture, left, top, width, height,
1243 config, buffer, rowBytes);
1244}
1245
1246bool GrContext::internalReadTexturePixels(GrTexture* texture,
1247 int left, int top,
1248 int width, int height,
1249 GrPixelConfig config,
1250 void* buffer,
1251 size_t rowBytes,
1252 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001253 SK_TRACE_EVENT0("GrContext::readTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001254 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001255
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001256 // TODO: code read pixels for textures that aren't also rendertargets
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001257 GrRenderTarget* target = texture->asRenderTarget();
1258 if (NULL != target) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001259 return this->internalReadRenderTargetPixels(target,
1260 left, top, width, height,
1261 config, buffer, rowBytes,
1262 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001263 } else {
1264 return false;
1265 }
1266}
1267
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001268#include "SkConfig8888.h"
1269
1270namespace {
1271/**
1272 * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel
1273 * formats are representable as Config8888 and so the function returns false
1274 * if the GrPixelConfig has no equivalent Config8888.
1275 */
1276bool grconfig_to_config8888(GrPixelConfig config,
1277 SkCanvas::Config8888* config8888) {
1278 switch (config) {
1279 case kRGBA_8888_PM_GrPixelConfig:
1280 *config8888 = SkCanvas::kRGBA_Premul_Config8888;
1281 return true;
1282 case kRGBA_8888_UPM_GrPixelConfig:
1283 *config8888 = SkCanvas::kRGBA_Unpremul_Config8888;
1284 return true;
1285 case kBGRA_8888_PM_GrPixelConfig:
1286 *config8888 = SkCanvas::kBGRA_Premul_Config8888;
1287 return true;
1288 case kBGRA_8888_UPM_GrPixelConfig:
1289 *config8888 = SkCanvas::kBGRA_Unpremul_Config8888;
1290 return true;
1291 default:
1292 return false;
1293 }
1294}
1295}
1296
bsalomon@google.com6f379512011-11-16 20:36:03 +00001297bool GrContext::internalReadRenderTargetPixels(GrRenderTarget* target,
1298 int left, int top,
1299 int width, int height,
1300 GrPixelConfig config,
1301 void* buffer,
1302 size_t rowBytes,
1303 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001304 SK_TRACE_EVENT0("GrContext::readRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001305 ASSERT_OWNED_RESOURCE(target);
1306
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001307 if (NULL == target) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001308 target = fDrawState->getRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001309 if (NULL == target) {
1310 return false;
1311 }
1312 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001313
bsalomon@google.com6f379512011-11-16 20:36:03 +00001314 if (!(kDontFlush_PixelOpsFlag & flags)) {
1315 this->flush();
1316 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001317
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001318 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1319 GrPixelConfigIsUnpremultiplied(config) &&
1320 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1321 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1322 if (!grconfig_to_config8888(target->config(), &srcConfig8888) ||
1323 !grconfig_to_config8888(config, &dstConfig8888)) {
1324 return false;
1325 }
1326 // do read back using target's own config
1327 this->internalReadRenderTargetPixels(target,
1328 left, top,
1329 width, height,
1330 target->config(),
1331 buffer, rowBytes,
1332 kDontFlush_PixelOpsFlag);
1333 // sw convert the pixels to unpremul config
1334 uint32_t* pixels = reinterpret_cast<uint32_t*>(buffer);
1335 SkConvertConfig8888Pixels(pixels, rowBytes, dstConfig8888,
1336 pixels, rowBytes, srcConfig8888,
1337 width, height);
1338 return true;
1339 }
1340
bsalomon@google.comc4364992011-11-07 15:54:49 +00001341 GrTexture* src = target->asTexture();
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001342 bool swapRAndB = NULL != src &&
1343 fGpu->preferredReadPixelsConfig(config) ==
1344 GrPixelConfigSwapRAndB(config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001345
1346 bool flipY = NULL != src &&
1347 fGpu->readPixelsWillPayForYFlip(target, left, top,
1348 width, height, config,
1349 rowBytes);
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001350 bool alphaConversion = (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1351 GrPixelConfigIsUnpremultiplied(config));
bsalomon@google.comc4364992011-11-07 15:54:49 +00001352
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001353 if (NULL == src && alphaConversion) {
1354 // we should fallback to cpu conversion here. This could happen when
1355 // we were given an external render target by the client that is not
1356 // also a texture (e.g. FBO 0 in GL)
1357 return false;
1358 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001359 // we draw to a scratch texture if any of these conversion are applied
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001360 GrAutoScratchTexture ast;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001361 if (flipY || swapRAndB || alphaConversion) {
1362 GrAssert(NULL != src);
1363 if (swapRAndB) {
1364 config = GrPixelConfigSwapRAndB(config);
1365 GrAssert(kUnknown_GrPixelConfig != config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001366 }
1367 // Make the scratch a render target because we don't have a robust
1368 // readTexturePixels as of yet (it calls this function).
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001369 GrTextureDesc desc;
1370 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1371 desc.fWidth = width;
1372 desc.fHeight = height;
1373 desc.fConfig = config;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001374
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001375 // When a full readback is faster than a partial we could always make
1376 // the scratch exactly match the passed rect. However, if we see many
1377 // different size rectangles we will trash our texture cache and pay the
1378 // cost of creating and destroying many textures. So, we only request
1379 // an exact match when the caller is reading an entire RT.
1380 ScratchTexMatch match = kApprox_ScratchTexMatch;
1381 if (0 == left &&
1382 0 == top &&
1383 target->width() == width &&
1384 target->height() == height &&
1385 fGpu->fullReadPixelsIsFasterThanPartial()) {
1386 match = kExact_ScratchTexMatch;
1387 }
1388 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001389 GrTexture* texture = ast.texture();
1390 if (!texture) {
1391 return false;
1392 }
1393 target = texture->asRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001394 GrAssert(NULL != target);
1395
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001396 GrDrawTarget::AutoStateRestore asr(fGpu,
1397 GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001398 GrDrawState* drawState = fGpu->drawState();
1399 drawState->setRenderTarget(target);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001400
bsalomon@google.comc4364992011-11-07 15:54:49 +00001401 GrMatrix matrix;
1402 if (flipY) {
1403 matrix.setTranslate(SK_Scalar1 * left,
1404 SK_Scalar1 * (top + height));
1405 matrix.set(GrMatrix::kMScaleY, -GR_Scalar1);
1406 } else {
1407 matrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
1408 }
1409 matrix.postIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001410 drawState->sampler(0)->reset(matrix);
1411 drawState->sampler(0)->setRAndBSwap(swapRAndB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001412 drawState->setTexture(0, src);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001413 GrRect rect;
1414 rect.setXYWH(0, 0, SK_Scalar1 * width, SK_Scalar1 * height);
1415 fGpu->drawSimpleRect(rect, NULL, 0x1);
1416 left = 0;
1417 top = 0;
1418 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001419 return fGpu->readPixels(target,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001420 left, top, width, height,
1421 config, buffer, rowBytes, flipY);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001422}
1423
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001424void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1425 GrAssert(target);
1426 ASSERT_OWNED_RESOURCE(target);
1427 // In the future we may track whether there are any pending draws to this
1428 // target. We don't today so we always perform a flush. We don't promise
1429 // this to our clients, though.
1430 this->flush();
1431 fGpu->resolveRenderTarget(target);
1432}
1433
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001434void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst) {
1435 if (NULL == src || NULL == dst) {
1436 return;
1437 }
1438 ASSERT_OWNED_RESOURCE(src);
1439
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001440 // Writes pending to the source texture are not tracked, so a flush
1441 // is required to ensure that the copy captures the most recent contents
1442 // of the source texture. See similar behaviour in
1443 // GrContext::resolveRenderTarget.
1444 this->flush();
1445
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001446 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001447 GrDrawState* drawState = fGpu->drawState();
1448 drawState->setRenderTarget(dst);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001449 GrMatrix sampleM;
1450 sampleM.setIDiv(src->width(), src->height());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001451 drawState->setTexture(0, src);
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001452 drawState->sampler(0)->reset(sampleM);
bsalomon@google.com5db3b6c2012-01-12 20:38:57 +00001453 SkRect rect = SkRect::MakeXYWH(0, 0,
1454 SK_Scalar1 * src->width(),
1455 SK_Scalar1 * src->height());
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001456 fGpu->drawSimpleRect(rect, NULL, 1 << 0);
1457}
1458
bsalomon@google.com6f379512011-11-16 20:36:03 +00001459void GrContext::internalWriteRenderTargetPixels(GrRenderTarget* target,
1460 int left, int top,
1461 int width, int height,
1462 GrPixelConfig config,
1463 const void* buffer,
1464 size_t rowBytes,
1465 uint32_t flags) {
1466 SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001467 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001468
1469 if (NULL == target) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001470 target = fDrawState->getRenderTarget();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001471 if (NULL == target) {
1472 return;
1473 }
1474 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001475
1476 // TODO: when underlying api has a direct way to do this we should use it
1477 // (e.g. glDrawPixels on desktop GL).
1478
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001479 // If the RT is also a texture and we don't have to do PM/UPM conversion
1480 // then take the texture path, which we expect to be at least as fast or
1481 // faster since it doesn't use an intermediate texture as we do below.
1482
1483#if !GR_MAC_BUILD
1484 // At least some drivers on the Mac get confused when glTexImage2D is called
1485 // on a texture attached to an FBO. The FBO still sees the old image. TODO:
1486 // determine what OS versions and/or HW is affected.
1487 if (NULL != target->asTexture() &&
1488 GrPixelConfigIsUnpremultiplied(target->config()) ==
1489 GrPixelConfigIsUnpremultiplied(config)) {
1490
1491 this->internalWriteTexturePixels(target->asTexture(),
1492 left, top, width, height,
1493 config, buffer, rowBytes, flags);
1494 return;
1495 }
1496#endif
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001497 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1498 GrPixelConfigIsUnpremultiplied(config) &&
1499 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1500 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1501 if (!grconfig_to_config8888(config, &srcConfig8888) ||
1502 !grconfig_to_config8888(target->config(), &dstConfig8888)) {
1503 return;
1504 }
1505 // allocate a tmp buffer and sw convert the pixels to premul
1506 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(width * height);
1507 const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer);
1508 SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888,
1509 src, rowBytes, srcConfig8888,
1510 width, height);
1511 // upload the already premul pixels
1512 this->internalWriteRenderTargetPixels(target,
1513 left, top,
1514 width, height,
1515 target->config(),
1516 tmpPixels, 4 * width, flags);
1517 return;
1518 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001519
1520 bool swapRAndB = fGpu->preferredReadPixelsConfig(config) ==
1521 GrPixelConfigSwapRAndB(config);
1522 if (swapRAndB) {
1523 config = GrPixelConfigSwapRAndB(config);
1524 }
1525
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001526 GrTextureDesc desc;
1527 desc.fWidth = width;
1528 desc.fHeight = height;
1529 desc.fConfig = config;
1530
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001531 GrAutoScratchTexture ast(this, desc);
1532 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001533 if (NULL == texture) {
1534 return;
1535 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001536 this->internalWriteTexturePixels(texture, 0, 0, width, height,
1537 config, buffer, rowBytes, flags);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001538
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001539 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001540 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001541
1542 GrMatrix matrix;
1543 matrix.setTranslate(GrIntToScalar(left), GrIntToScalar(top));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001544 drawState->setViewMatrix(matrix);
1545 drawState->setRenderTarget(target);
1546 drawState->setTexture(0, texture);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001547
bsalomon@google.com5c638652011-07-18 19:31:59 +00001548 matrix.setIDiv(texture->width(), texture->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001549 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
1550 GrSamplerState::kNearest_Filter,
1551 matrix);
1552 drawState->sampler(0)->setRAndBSwap(swapRAndB);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001553
1554 GrVertexLayout layout = GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(0);
1555 static const int VCOUNT = 4;
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001556 // TODO: Use GrGpu::drawRect here
bsalomon@google.com27847de2011-02-22 20:59:41 +00001557 GrDrawTarget::AutoReleaseGeometry geo(fGpu, layout, VCOUNT, 0);
1558 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001559 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +00001560 return;
1561 }
1562 ((GrPoint*)geo.vertices())->setIRectFan(0, 0, width, height);
bsalomon@google.com47059542012-06-06 20:51:20 +00001563 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, VCOUNT);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001564}
1565////////////////////////////////////////////////////////////////////////////////
1566
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001567void GrContext::setPaint(const GrPaint& paint) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001568
1569 for (int i = 0; i < GrPaint::kMaxTextures; ++i) {
1570 int s = i + GrPaint::kFirstTextureStage;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001571 fDrawState->setTexture(s, paint.getTexture(i));
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001572 ASSERT_OWNED_RESOURCE(paint.getTexture(i));
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001573 if (paint.getTexture(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001574 *fDrawState->sampler(s) = paint.getTextureSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001575 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001576 }
1577
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001578 fDrawState->setFirstCoverageStage(GrPaint::kFirstMaskStage);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001579
1580 for (int i = 0; i < GrPaint::kMaxMasks; ++i) {
1581 int s = i + GrPaint::kFirstMaskStage;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001582 fDrawState->setTexture(s, paint.getMask(i));
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001583 ASSERT_OWNED_RESOURCE(paint.getMask(i));
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001584 if (paint.getMask(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001585 *fDrawState->sampler(s) = paint.getMaskSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001586 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001587 }
bsalomon@google.com26936d02012-03-19 13:06:19 +00001588
1589 // disable all stages not accessible via the paint
1590 for (int s = GrPaint::kTotalStages; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001591 fDrawState->setTexture(s, NULL);
bsalomon@google.com26936d02012-03-19 13:06:19 +00001592 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001593
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001594 fDrawState->setColor(paint.fColor);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001595
1596 if (paint.fDither) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001597 fDrawState->enableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001598 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001599 fDrawState->disableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001600 }
1601 if (paint.fAntiAlias) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001602 fDrawState->enableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001603 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001604 fDrawState->disableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001605 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001606 if (paint.fColorMatrixEnabled) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001607 fDrawState->enableState(GrDrawState::kColorMatrix_StateBit);
1608 fDrawState->setColorMatrix(paint.fColorMatrix);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001609 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001610 fDrawState->disableState(GrDrawState::kColorMatrix_StateBit);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001611 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001612 fDrawState->setBlendFunc(paint.fSrcBlendCoeff, paint.fDstBlendCoeff);
1613 fDrawState->setColorFilter(paint.fColorFilterColor, paint.fColorFilterXfermode);
1614 fDrawState->setCoverage(paint.fCoverage);
reed@google.com4b2d3f32012-05-15 18:05:50 +00001615#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
bsalomon@google.come79c8152012-03-29 19:07:12 +00001616 if ((paint.getActiveMaskStageMask() || 0xff != paint.fCoverage) &&
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001617 !fGpu->canApplyCoverage()) {
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001618 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1619 }
bsalomon@google.com95cd7bd2012-03-28 15:35:05 +00001620#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001621}
1622
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001623GrDrawTarget* GrContext::prepareToDraw(const GrPaint& paint,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001624 DrawCategory category) {
1625 if (category != fLastDrawCategory) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001626 this->flushDrawBuffer();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001627 fLastDrawCategory = category;
1628 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001629 this->setPaint(paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001630 GrDrawTarget* target = fGpu;
1631 switch (category) {
bsalomon@google.com193395c2012-03-30 17:35:12 +00001632 case kUnbuffered_DrawCategory:
1633 target = fGpu;
1634 break;
1635 case kBuffered_DrawCategory:
1636 target = fDrawBuffer;
1637 fDrawBuffer->setClip(fGpu->getClip());
1638 break;
1639 default:
1640 GrCrash("Unexpected DrawCategory.");
1641 break;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001642 }
1643 return target;
1644}
1645
robertphillips@google.com72176b22012-05-23 13:19:12 +00001646/*
1647 * This method finds a path renderer that can draw the specified path on
1648 * the provided target.
1649 * Due to its expense, the software path renderer has split out so it can
1650 * can be individually allowed/disallowed via the "allowSW" boolean.
1651 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001652GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
bsalomon@google.com289533a2011-10-27 12:34:25 +00001653 GrPathFill fill,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001654 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001655 bool antiAlias,
1656 bool allowSW) {
bsalomon@google.com30085192011-08-19 15:42:31 +00001657 if (NULL == fPathRendererChain) {
1658 fPathRendererChain =
1659 new GrPathRendererChain(this, GrPathRendererChain::kNone_UsageFlag);
1660 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001661
1662 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path, fill,
1663 target,
1664 antiAlias);
1665
1666 if (NULL == pr && allowSW) {
1667 if (NULL == fSoftwarePathRenderer) {
1668 fSoftwarePathRenderer = new GrSoftwarePathRenderer(this);
1669 }
1670
1671 pr = fSoftwarePathRenderer;
1672 }
1673
1674 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001675}
1676
bsalomon@google.com27847de2011-02-22 20:59:41 +00001677////////////////////////////////////////////////////////////////////////////////
1678
bsalomon@google.com27847de2011-02-22 20:59:41 +00001679void GrContext::setRenderTarget(GrRenderTarget* target) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001680 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001681 if (fDrawState->getRenderTarget() != target) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001682 this->flush(false);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001683 fDrawState->setRenderTarget(target);
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001684 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001685}
1686
1687GrRenderTarget* GrContext::getRenderTarget() {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001688 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001689}
1690
1691const GrRenderTarget* GrContext::getRenderTarget() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001692 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001693}
1694
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001695bool GrContext::isConfigRenderable(GrPixelConfig config) const {
1696 return fGpu->isConfigRenderable(config);
1697}
1698
bsalomon@google.com27847de2011-02-22 20:59:41 +00001699const GrMatrix& GrContext::getMatrix() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001700 return fDrawState->getViewMatrix();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001701}
1702
1703void GrContext::setMatrix(const GrMatrix& m) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001704 fDrawState->setViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001705}
1706
1707void GrContext::concatMatrix(const GrMatrix& m) const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001708 fDrawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001709}
1710
1711static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
1712 intptr_t mask = 1 << shift;
1713 if (pred) {
1714 bits |= mask;
1715 } else {
1716 bits &= ~mask;
1717 }
1718 return bits;
1719}
1720
bsalomon@google.com583a1e32011-08-17 13:42:46 +00001721GrContext::GrContext(GrGpu* gpu) {
bsalomon@google.comc0af3172012-06-15 14:10:09 +00001722 ++THREAD_INSTANCE_COUNT;
1723
bsalomon@google.com27847de2011-02-22 20:59:41 +00001724 fGpu = gpu;
1725 fGpu->ref();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001726 fGpu->setContext(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001727
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001728 fDrawState = new GrDrawState();
1729 fGpu->setDrawState(fDrawState);
1730
bsalomon@google.com30085192011-08-19 15:42:31 +00001731 fPathRendererChain = NULL;
robertphillips@google.com72176b22012-05-23 13:19:12 +00001732 fSoftwarePathRenderer = NULL;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001733
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001734 fTextureCache = new GrResourceCache(MAX_TEXTURE_CACHE_COUNT,
1735 MAX_TEXTURE_CACHE_BYTES);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001736 fFontCache = new GrFontCache(fGpu);
1737
1738 fLastDrawCategory = kUnbuffered_DrawCategory;
1739
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001740 fDrawBuffer = NULL;
1741 fDrawBufferVBAllocPool = NULL;
1742 fDrawBufferIBAllocPool = NULL;
1743
robertphillips@google.comf6747b02012-06-12 00:32:28 +00001744 fAARectRenderer = new GrAARectRenderer;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001745
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001746 this->setupDrawBuffer();
1747}
1748
1749void GrContext::setupDrawBuffer() {
1750
1751 GrAssert(NULL == fDrawBuffer);
1752 GrAssert(NULL == fDrawBufferVBAllocPool);
1753 GrAssert(NULL == fDrawBufferIBAllocPool);
1754
bsalomon@google.com92edd312012-04-04 21:40:21 +00001755#if DEFER_TEXT_RENDERING || BATCH_RECT_TO_RECT || DEFER_PATHS
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001756 fDrawBufferVBAllocPool =
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001757 new GrVertexBufferAllocPool(fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001758 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
1759 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS);
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001760 fDrawBufferIBAllocPool =
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001761 new GrIndexBufferAllocPool(fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001762 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001763 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS);
1764
bsalomon@google.com471d4712011-08-23 15:45:25 +00001765 fDrawBuffer = new GrInOrderDrawBuffer(fGpu,
1766 fDrawBufferVBAllocPool,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001767 fDrawBufferIBAllocPool);
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001768#endif
1769
1770#if BATCH_RECT_TO_RECT
bsalomon@google.com27847de2011-02-22 20:59:41 +00001771 fDrawBuffer->setQuadIndexBuffer(this->getQuadIndexBuffer());
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001772#endif
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001773 fDrawBuffer->setAutoFlushTarget(fGpu);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001774 fDrawBuffer->setDrawState(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001775}
1776
bsalomon@google.com27847de2011-02-22 20:59:41 +00001777GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001778#if DEFER_TEXT_RENDERING
bsalomon@google.com193395c2012-03-30 17:35:12 +00001779 return prepareToDraw(paint, kBuffered_DrawCategory);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001780#else
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001781 return prepareToDraw(paint, kUnbuffered_DrawCategory);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001782#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001783}
1784
1785const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1786 return fGpu->getQuadIndexBuffer();
1787}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001788
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001789GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture,
1790 GrAutoScratchTexture* temp1,
1791 GrAutoScratchTexture* temp2,
1792 const SkRect& rect,
1793 float sigmaX, float sigmaY) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001794 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001795 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
1796 GrClip oldClip = this->getClip();
1797 GrTexture* origTexture = srcTexture;
1798 GrAutoMatrix avm(this, GrMatrix::I());
1799 SkIRect clearRect;
bsalomon@google.comb505a122012-05-31 18:40:36 +00001800 int scaleFactorX, radiusX;
1801 int scaleFactorY, radiusY;
1802 sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX);
1803 sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY);
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001804
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001805 SkRect srcRect(rect);
1806 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
1807 srcRect.roundOut();
robertphillips@google.com8637a362012-04-10 18:32:35 +00001808 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
1809 static_cast<float>(scaleFactorY));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001810 this->setClip(srcRect);
1811
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001812 GrAssert(kBGRA_8888_PM_GrPixelConfig == srcTexture->config() ||
1813 kRGBA_8888_PM_GrPixelConfig == srcTexture->config() ||
1814 kAlpha_8_GrPixelConfig == srcTexture->config());
1815
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001816 GrTextureDesc desc;
1817 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1818 desc.fWidth = SkScalarFloorToInt(srcRect.width());
1819 desc.fHeight = SkScalarFloorToInt(srcRect.height());
1820 desc.fConfig = srcTexture->config();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001821
1822 temp1->set(this, desc);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001823 if (temp2) {
1824 temp2->set(this, desc);
1825 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001826
1827 GrTexture* dstTexture = temp1->texture();
1828 GrPaint paint;
1829 paint.reset();
1830 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
1831
1832 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
1833 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1834 srcTexture->height());
1835 this->setRenderTarget(dstTexture->asRenderTarget());
1836 SkRect dstRect(srcRect);
1837 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
1838 i < scaleFactorY ? 0.5f : 1.0f);
1839 paint.setTexture(0, srcTexture);
1840 this->drawRectToRect(paint, dstRect, srcRect);
1841 srcRect = dstRect;
1842 SkTSwap(srcTexture, dstTexture);
1843 // If temp2 is non-NULL, don't render back to origTexture
robertphillips@google.com972265d2012-06-13 18:49:30 +00001844 if (temp2 && dstTexture == origTexture) {
1845 dstTexture = temp2->texture();
1846 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001847 }
1848
robertphillips@google.com7a396332012-05-10 15:11:27 +00001849 SkIRect srcIRect;
1850 srcRect.roundOut(&srcIRect);
1851
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001852 if (sigmaX > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001853 if (scaleFactorX > 1) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001854 // Clear out a radius to the right of the srcRect to prevent the
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001855 // X convolution from reading garbage.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001856 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001857 radiusX, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001858 this->clear(&clearRect, 0x0);
1859 }
1860
1861 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001862 convolve_gaussian(fGpu, srcTexture, srcRect, sigmaX, radiusX,
1863 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001864 SkTSwap(srcTexture, dstTexture);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001865 if (temp2 && dstTexture == origTexture) {
1866 dstTexture = temp2->texture();
1867 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001868 }
1869
1870 if (sigmaY > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001871 if (scaleFactorY > 1 || sigmaX > 0.0f) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001872 // Clear out a radius below the srcRect to prevent the Y
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001873 // convolution from reading garbage.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001874 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001875 srcIRect.width(), radiusY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001876 this->clear(&clearRect, 0x0);
1877 }
1878
1879 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001880 convolve_gaussian(fGpu, srcTexture, srcRect, sigmaY, radiusY,
1881 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001882 SkTSwap(srcTexture, dstTexture);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001883 if (temp2 && dstTexture == origTexture) {
1884 dstTexture = temp2->texture();
1885 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001886 }
1887
1888 if (scaleFactorX > 1 || scaleFactorY > 1) {
1889 // Clear one pixel to the right and below, to accommodate bilinear
1890 // upsampling.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001891 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
1892 srcIRect.width() + 1, 1);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001893 this->clear(&clearRect, 0x0);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001894 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
1895 1, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001896 this->clear(&clearRect, 0x0);
1897 // FIXME: This should be mitchell, not bilinear.
1898 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
1899 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1900 srcTexture->height());
1901 this->setRenderTarget(dstTexture->asRenderTarget());
1902 paint.setTexture(0, srcTexture);
1903 SkRect dstRect(srcRect);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001904 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001905 this->drawRectToRect(paint, dstRect, srcRect);
1906 srcRect = dstRect;
1907 SkTSwap(srcTexture, dstTexture);
1908 }
1909 this->setRenderTarget(oldRenderTarget);
1910 this->setClip(oldClip);
1911 return srcTexture;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001912}
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001913
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001914GrTexture* GrContext::applyMorphology(GrTexture* srcTexture,
1915 const GrRect& rect,
1916 GrTexture* temp1, GrTexture* temp2,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001917 MorphologyType morphType,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001918 SkISize radius) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001919 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001920 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
1921 GrAutoMatrix avm(this, GrMatrix::I());
1922 GrClip oldClip = this->getClip();
robertphillips@google.com7a396332012-05-10 15:11:27 +00001923 this->setClip(GrRect::MakeWH(SkIntToScalar(srcTexture->width()),
1924 SkIntToScalar(srcTexture->height())));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001925 if (radius.fWidth > 0) {
1926 this->setRenderTarget(temp1->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001927 apply_morphology(fGpu, srcTexture, rect, radius.fWidth, morphType,
1928 Gr1DKernelEffect::kX_Direction);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001929 SkIRect clearRect = SkIRect::MakeXYWH(
1930 SkScalarFloorToInt(rect.fLeft),
1931 SkScalarFloorToInt(rect.fBottom),
1932 SkScalarFloorToInt(rect.width()),
1933 radius.fHeight);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001934 this->clear(&clearRect, 0x0);
1935 srcTexture = temp1;
1936 }
1937 if (radius.fHeight > 0) {
1938 this->setRenderTarget(temp2->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001939 apply_morphology(fGpu, srcTexture, rect, radius.fHeight, morphType,
1940 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001941 srcTexture = temp2;
1942 }
1943 this->setRenderTarget(oldRenderTarget);
1944 this->setClip(oldClip);
1945 return srcTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001946}
bsalomon@google.comc4364992011-11-07 15:54:49 +00001947
robertphillips@google.com49d9fd52012-05-23 11:44:08 +00001948void GrContext::postClipPush() {
1949 fGpu->postClipPush();
1950}
1951
1952void GrContext::preClipPop() {
1953 fGpu->preClipPop();
1954};
1955
bsalomon@google.comc4364992011-11-07 15:54:49 +00001956///////////////////////////////////////////////////////////////////////////////