blob: d2b312f7ca29d2a7c039e1776b29dadef675849c [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.com8f7e1da2012-06-21 19:48:32 +000049static const size_t kDefaultTextureCacheBudget = 16 * 1024 * 1024;
bsalomon@google.com27847de2011-02-22 20:59:41 +000050
bsalomon@google.com60361492012-03-15 17:47:06 +000051static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
bsalomon@google.com27847de2011-02-22 20:59:41 +000052static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
53
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +000054// path rendering is the only thing we defer today that uses non-static indices
55static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = DEFER_PATHS ? 1 << 11 : 0;
56static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = DEFER_PATHS ? 4 : 0;
bsalomon@google.com27847de2011-02-22 20:59:41 +000057
bsalomon@google.combc4b6542011-11-19 13:56:11 +000058#define ASSERT_OWNED_RESOURCE(R) GrAssert(!(R) || (R)->getContext() == this)
59
bsalomon@google.com05ef5102011-05-02 21:14:59 +000060GrContext* GrContext::Create(GrEngine engine,
61 GrPlatform3DContext context3D) {
bsalomon@google.com27847de2011-02-22 20:59:41 +000062 GrContext* ctx = NULL;
63 GrGpu* fGpu = GrGpu::Create(engine, context3D);
64 if (NULL != fGpu) {
65 ctx = new GrContext(fGpu);
66 fGpu->unref();
67 }
68 return ctx;
69}
70
bsalomon@google.comc0af3172012-06-15 14:10:09 +000071namespace {
72void* CreateThreadInstanceCount() {
73 return new int(0);
74}
75void DeleteThreadInstanceCount(void* v) {
76 delete reinterpret_cast<int*>(v);
77}
78#define THREAD_INSTANCE_COUNT \
79 (*reinterpret_cast<int*>(SkTLS::Get(CreateThreadInstanceCount, \
80 DeleteThreadInstanceCount)))
81
82}
83
84int GrContext::GetThreadInstanceCount() {
85 return THREAD_INSTANCE_COUNT;
86}
87
bsalomon@google.com27847de2011-02-22 20:59:41 +000088GrContext::~GrContext() {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000089 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +000090
91 // Since the gpu can hold scratch textures, give it a chance to let go
92 // of them before freeing the texture cache
93 fGpu->purgeResources();
94
bsalomon@google.com27847de2011-02-22 20:59:41 +000095 delete fTextureCache;
96 delete fFontCache;
97 delete fDrawBuffer;
98 delete fDrawBufferVBAllocPool;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +000099 delete fDrawBufferIBAllocPool;
bsalomon@google.com30085192011-08-19 15:42:31 +0000100
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000101 fAARectRenderer->unref();
102
bsalomon@google.com205d4602011-04-25 12:43:45 +0000103 fGpu->unref();
bsalomon@google.com30085192011-08-19 15:42:31 +0000104 GrSafeUnref(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000105 GrSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000106 fDrawState->unref();
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000107
108 --THREAD_INSTANCE_COUNT;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000109}
110
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000111void GrContext::contextLost() {
junov@google.com53a55842011-06-08 22:55:10 +0000112 contextDestroyed();
113 this->setupDrawBuffer();
114}
115
116void GrContext::contextDestroyed() {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000117 // abandon first to so destructors
118 // don't try to free the resources in the API.
119 fGpu->abandonResources();
120
bsalomon@google.com30085192011-08-19 15:42:31 +0000121 // a path renderer may be holding onto resources that
122 // are now unusable
123 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000124 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000125
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000126 delete fDrawBuffer;
127 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000128
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000129 delete fDrawBufferVBAllocPool;
130 fDrawBufferVBAllocPool = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000131
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000132 delete fDrawBufferIBAllocPool;
133 fDrawBufferIBAllocPool = NULL;
134
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000135 fAARectRenderer->reset();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000136
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000137 fTextureCache->removeAll();
138 fFontCache->freeAll();
139 fGpu->markContextDirty();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000140}
141
142void GrContext::resetContext() {
143 fGpu->markContextDirty();
144}
145
146void GrContext::freeGpuResources() {
147 this->flush();
robertphillips@google.comff175842012-05-14 19:31:39 +0000148
149 fGpu->purgeResources();
150
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000151 fAARectRenderer->reset();
152
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000153 fTextureCache->removeAll();
154 fFontCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000155 // a path renderer may be holding onto resources
156 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000157 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000158}
159
twiz@google.com05e70242012-01-27 19:12:00 +0000160size_t GrContext::getGpuTextureCacheBytes() const {
161 return fTextureCache->getCachedResourceBytes();
162}
163
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000164////////////////////////////////////////////////////////////////////////////////
165
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000166int GrContext::PaintStageVertexLayoutBits(
167 const GrPaint& paint,
168 const bool hasTexCoords[GrPaint::kTotalStages]) {
169 int stageMask = paint.getActiveStageMask();
170 int layout = 0;
171 for (int i = 0; i < GrPaint::kTotalStages; ++i) {
172 if ((1 << i) & stageMask) {
173 if (NULL != hasTexCoords && hasTexCoords[i]) {
174 layout |= GrDrawTarget::StageTexCoordVertexLayoutBit(i, i);
175 } else {
176 layout |= GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(i);
177 }
178 }
179 }
180 return layout;
181}
182
183
184////////////////////////////////////////////////////////////////////////////////
185
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000186GrTexture* GrContext::TextureCacheEntry::texture() const {
187 if (NULL == fEntry) {
188 return NULL;
189 } else {
190 return (GrTexture*) fEntry->resource();
191 }
192}
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000193
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000194namespace {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000195
196// we should never have more than one stencil buffer with same combo of
197// (width,height,samplecount)
198void gen_stencil_key_values(int width, int height,
199 int sampleCnt, uint32_t v[4]) {
200 v[0] = width;
201 v[1] = height;
202 v[2] = sampleCnt;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000203 v[3] = GrResourceKey::kStencilBuffer_TypeBit;
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000204}
205
206void gen_stencil_key_values(const GrStencilBuffer* sb,
207 uint32_t v[4]) {
208 gen_stencil_key_values(sb->width(), sb->height(),
209 sb->numSamples(), v);
210}
bsalomon@google.com82c7bd82011-11-09 15:32:29 +0000211
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000212void scale_rect(SkRect* rect, float xScale, float yScale) {
robertphillips@google.com5af56062012-04-27 15:39:52 +0000213 rect->fLeft = SkScalarMul(rect->fLeft, SkFloatToScalar(xScale));
214 rect->fTop = SkScalarMul(rect->fTop, SkFloatToScalar(yScale));
215 rect->fRight = SkScalarMul(rect->fRight, SkFloatToScalar(xScale));
216 rect->fBottom = SkScalarMul(rect->fBottom, SkFloatToScalar(yScale));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000217}
218
bsalomon@google.comb505a122012-05-31 18:40:36 +0000219float adjust_sigma(float sigma, int *scaleFactor, int *radius) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000220 *scaleFactor = 1;
221 while (sigma > MAX_BLUR_SIGMA) {
222 *scaleFactor *= 2;
223 sigma *= 0.5f;
224 }
bsalomon@google.comb505a122012-05-31 18:40:36 +0000225 *radius = static_cast<int>(ceilf(sigma * 3.0f));
226 GrAssert(*radius <= GrConvolutionEffect::kMaxKernelRadius);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000227 return sigma;
228}
229
230void apply_morphology(GrGpu* gpu,
231 GrTexture* texture,
232 const SkRect& rect,
233 int radius,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000234 GrContext::MorphologyType morphType,
235 Gr1DKernelEffect::Direction direction) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000236
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000237 GrRenderTarget* target = gpu->drawState()->getRenderTarget();
238 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000239 GrDrawState* drawState = gpu->drawState();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000240 drawState->setRenderTarget(target);
241 GrMatrix sampleM;
242 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000243 drawState->sampler(0)->reset(sampleM);
244 SkAutoTUnref<GrCustomStage> morph(
245 new GrMorphologyEffect(direction, radius, morphType));
246 drawState->sampler(0)->setCustomStage(morph);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000247 drawState->setTexture(0, texture);
248 gpu->drawSimpleRect(rect, NULL, 1 << 0);
249}
250
bsalomon@google.comb505a122012-05-31 18:40:36 +0000251void convolve_gaussian(GrGpu* gpu,
252 GrTexture* texture,
253 const SkRect& rect,
254 float sigma,
255 int radius,
256 Gr1DKernelEffect::Direction direction) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000257 GrRenderTarget* target = gpu->drawState()->getRenderTarget();
258 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000259 GrDrawState* drawState = gpu->drawState();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000260 drawState->setRenderTarget(target);
261 GrMatrix sampleM;
262 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000263 drawState->sampler(0)->reset(sampleM);
264 SkAutoTUnref<GrConvolutionEffect> conv(new
265 GrConvolutionEffect(direction, radius));
266 conv->setGaussianKernel(sigma);
267 drawState->sampler(0)->setCustomStage(conv);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000268 drawState->setTexture(0, texture);
269 gpu->drawSimpleRect(rect, NULL, 1 << 0);
270}
271
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000272}
273
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000274GrContext::TextureCacheEntry GrContext::findAndLockTexture(
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000275 const GrTextureDesc& desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000276 const GrSamplerState* sampler) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000277 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler, desc, false);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000278 return TextureCacheEntry(fTextureCache->findAndLock(resourceKey,
279 GrResourceCache::kNested_LockType));
280}
281
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000282bool GrContext::isTextureInCache(const GrTextureDesc& desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000283 const GrSamplerState* sampler) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000284 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler, desc, false);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000285 return fTextureCache->hasKey(resourceKey);
286}
287
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000288GrResourceEntry* GrContext::addAndLockStencilBuffer(GrStencilBuffer* sb) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000289 ASSERT_OWNED_RESOURCE(sb);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000290 uint32_t v[4];
291 gen_stencil_key_values(sb, v);
292 GrResourceKey resourceKey(v);
293 return fTextureCache->createAndLock(resourceKey, sb);
294}
295
296GrStencilBuffer* GrContext::findStencilBuffer(int width, int height,
297 int sampleCnt) {
298 uint32_t v[4];
299 gen_stencil_key_values(width, height, sampleCnt, v);
300 GrResourceKey resourceKey(v);
301 GrResourceEntry* entry = fTextureCache->findAndLock(resourceKey,
302 GrResourceCache::kSingle_LockType);
303 if (NULL != entry) {
304 GrStencilBuffer* sb = (GrStencilBuffer*) entry->resource();
305 return sb;
306 } else {
307 return NULL;
308 }
309}
310
311void GrContext::unlockStencilBuffer(GrResourceEntry* sbEntry) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000312 ASSERT_OWNED_RESOURCE(sbEntry->resource());
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000313 fTextureCache->unlock(sbEntry);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000314}
315
316static void stretchImage(void* dst,
317 int dstW,
318 int dstH,
319 void* src,
320 int srcW,
321 int srcH,
322 int bpp) {
323 GrFixed dx = (srcW << 16) / dstW;
324 GrFixed dy = (srcH << 16) / dstH;
325
326 GrFixed y = dy >> 1;
327
328 int dstXLimit = dstW*bpp;
329 for (int j = 0; j < dstH; ++j) {
330 GrFixed x = dx >> 1;
331 void* srcRow = (uint8_t*)src + (y>>16)*srcW*bpp;
332 void* dstRow = (uint8_t*)dst + j*dstW*bpp;
333 for (int i = 0; i < dstXLimit; i += bpp) {
334 memcpy((uint8_t*) dstRow + i,
335 (uint8_t*) srcRow + (x>>16)*bpp,
336 bpp);
337 x += dx;
338 }
339 y += dy;
340 }
341}
342
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000343GrContext::TextureCacheEntry GrContext::createAndLockTexture(
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000344 const GrSamplerState* sampler,
345 const GrTextureDesc& desc,
346 void* srcData,
347 size_t rowBytes) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000348 SK_TRACE_EVENT0("GrContext::createAndLockTexture");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000349
350#if GR_DUMP_TEXTURE_UPLOAD
351 GrPrintf("GrContext::createAndLockTexture [%d %d]\n", desc.fWidth, desc.fHeight);
352#endif
353
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000354 TextureCacheEntry entry;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000355
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000356 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, sampler,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000357 desc, false);
358
359 if (GrTexture::NeedsResizing(resourceKey)) {
360 // The desired texture is NPOT and tiled but that isn't supported by
361 // the current hardware. Resize the texture to be a POT
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000362 GrAssert(NULL != sampler);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000363 TextureCacheEntry clampEntry = this->findAndLockTexture(desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000364 NULL);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000365
366 if (NULL == clampEntry.texture()) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000367 clampEntry = this->createAndLockTexture(NULL, desc,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000368 srcData, rowBytes);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000369 GrAssert(NULL != clampEntry.texture());
370 if (NULL == clampEntry.texture()) {
371 return entry;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000372 }
373 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000374 GrTextureDesc rtDesc = desc;
375 rtDesc.fFlags = rtDesc.fFlags |
376 kRenderTarget_GrTextureFlagBit |
377 kNoStencil_GrTextureFlagBit;
bsalomon@google.com99621082011-11-15 16:47:16 +0000378 rtDesc.fWidth = GrNextPow2(GrMax(desc.fWidth, 64));
379 rtDesc.fHeight = GrNextPow2(GrMax(desc.fHeight, 64));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000380
381 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
382
383 if (NULL != texture) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000384 GrDrawTarget::AutoStateRestore asr(fGpu,
385 GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000386 GrDrawState* drawState = fGpu->drawState();
387 drawState->setRenderTarget(texture->asRenderTarget());
388 drawState->setTexture(0, clampEntry.texture());
bsalomon@google.com82c7bd82011-11-09 15:32:29 +0000389
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000390 GrSamplerState::Filter filter;
391 // if filtering is not desired then we want to ensure all
392 // texels in the resampled image are copies of texels from
393 // the original.
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000394 if (GrTexture::NeedsFiltering(resourceKey)) {
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000395 filter = GrSamplerState::kBilinear_Filter;
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000396 } else {
397 filter = GrSamplerState::kNearest_Filter;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000398 }
bsalomon@google.com1e266f82011-12-12 16:11:33 +0000399 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
400 filter);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000401
402 static const GrVertexLayout layout =
403 GrDrawTarget::StageTexCoordVertexLayoutBit(0,0);
404 GrDrawTarget::AutoReleaseGeometry arg(fGpu, layout, 4, 0);
405
406 if (arg.succeeded()) {
407 GrPoint* verts = (GrPoint*) arg.vertices();
408 verts[0].setIRectFan(0, 0,
409 texture->width(),
410 texture->height(),
411 2*sizeof(GrPoint));
412 verts[1].setIRectFan(0, 0, 1, 1, 2*sizeof(GrPoint));
bsalomon@google.com47059542012-06-06 20:51:20 +0000413 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000414 0, 4);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000415 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000416 }
bsalomon@google.com1da07462011-03-10 14:51:57 +0000417 texture->releaseRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000418 } else {
419 // TODO: Our CPU stretch doesn't filter. But we create separate
420 // stretched textures when the sampler state is either filtered or
421 // not. Either implement filtered stretch blit on CPU or just create
422 // one when FBO case fails.
423
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000424 rtDesc.fFlags = kNone_GrTextureFlags;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000425 // no longer need to clamp at min RT size.
426 rtDesc.fWidth = GrNextPow2(desc.fWidth);
427 rtDesc.fHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000428 int bpp = GrBytesPerPixel(desc.fConfig);
bsalomon@google.com3582bf92011-06-30 21:32:31 +0000429 SkAutoSMalloc<128*128*4> stretchedPixels(bpp *
bsalomon@google.com27847de2011-02-22 20:59:41 +0000430 rtDesc.fWidth *
431 rtDesc.fHeight);
432 stretchImage(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
433 srcData, desc.fWidth, desc.fHeight, bpp);
434
435 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
436
437 GrTexture* texture = fGpu->createTexture(rtDesc,
438 stretchedPixels.get(),
439 stretchedRowBytes);
440 GrAssert(NULL != texture);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000441 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000442 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000443 fTextureCache->unlock(clampEntry.cacheEntry());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000444
445 } else {
446 GrTexture* texture = fGpu->createTexture(desc, srcData, rowBytes);
447 if (NULL != texture) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000448 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000449 }
450 }
451 return entry;
452}
453
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000454GrContext::TextureCacheEntry GrContext::lockScratchTexture(
455 const GrTextureDesc& inDesc,
456 ScratchTexMatch match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000457 GrTextureDesc desc = inDesc;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000458 desc.fClientCacheID = kScratch_CacheID;
459
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000460 if (kExact_ScratchTexMatch != match) {
461 // bin by pow2 with a reasonable min
462 static const int MIN_SIZE = 256;
463 desc.fWidth = GrMax(MIN_SIZE, GrNextPow2(desc.fWidth));
464 desc.fHeight = GrMax(MIN_SIZE, GrNextPow2(desc.fHeight));
465 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000466
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000467 GrResourceEntry* entry;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000468 int origWidth = desc.fWidth;
469 int origHeight = desc.fHeight;
470 bool doubledW = false;
471 bool doubledH = false;
472
473 do {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000474 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL, desc, true);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000475 entry = fTextureCache->findAndLock(key,
476 GrResourceCache::kNested_LockType);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000477 // if we miss, relax the fit of the flags...
478 // then try doubling width... then height.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000479 if (NULL != entry || kExact_ScratchTexMatch == match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000480 break;
481 }
482 if (!(desc.fFlags & kRenderTarget_GrTextureFlagBit)) {
483 desc.fFlags = desc.fFlags | kRenderTarget_GrTextureFlagBit;
484 } else if (desc.fFlags & kNoStencil_GrTextureFlagBit) {
485 desc.fFlags = desc.fFlags & ~kNoStencil_GrTextureFlagBit;
486 } else if (!doubledW) {
487 desc.fFlags = inDesc.fFlags;
488 desc.fWidth *= 2;
489 doubledW = true;
490 } else if (!doubledH) {
491 desc.fFlags = inDesc.fFlags;
492 desc.fWidth = origWidth;
493 desc.fHeight *= 2;
494 doubledH = true;
495 } else {
496 break;
497 }
498
499 } while (true);
500
501 if (NULL == entry) {
502 desc.fFlags = inDesc.fFlags;
503 desc.fWidth = origWidth;
504 desc.fHeight = origHeight;
505 GrTexture* texture = fGpu->createTexture(desc, NULL, 0);
506 if (NULL != texture) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000507 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000508 texture->desc(),
509 true);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000510 entry = fTextureCache->createAndLock(key, texture);
511 }
512 }
513
514 // If the caller gives us the same desc/sampler twice we don't want
515 // to return the same texture the second time (unless it was previously
516 // released). So we detach the entry from the cache and reattach at release.
517 if (NULL != entry) {
518 fTextureCache->detach(entry);
519 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000520 return TextureCacheEntry(entry);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000521}
522
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000523void GrContext::unlockTexture(TextureCacheEntry entry) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000524 ASSERT_OWNED_RESOURCE(entry.texture());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000525 // If this is a scratch texture we detached it from the cache
526 // while it was locked (to avoid two callers simultaneously getting
527 // the same texture).
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000528 if (GrTexture::IsScratchTexture(entry.cacheEntry()->key())) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000529 fTextureCache->reattachAndUnlock(entry.cacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000530 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000531 fTextureCache->unlock(entry.cacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000532 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000533}
534
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000535GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000536 void* srcData,
537 size_t rowBytes) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000538 GrTextureDesc descCopy = descIn;
539 descCopy.fClientCacheID = kUncached_CacheID;
540 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000541}
542
bsalomon@google.com8f7e1da2012-06-21 19:48:32 +0000543size_t GrContext::getTextureCacheBudget() const {
544 return fTextureCache->getBudget();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000545}
546
bsalomon@google.com8f7e1da2012-06-21 19:48:32 +0000547void GrContext::setTextureCacheBudget(size_t maxTextureBytes) {
548 fTextureCache->setBudget(maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000549}
550
bsalomon@google.com91958362011-06-13 17:58:13 +0000551int GrContext::getMaxTextureSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000552 return fGpu->getCaps().fMaxTextureSize;
bsalomon@google.com91958362011-06-13 17:58:13 +0000553}
554
555int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000556 return fGpu->getCaps().fMaxRenderTargetSize;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000557}
558
559///////////////////////////////////////////////////////////////////////////////
560
bsalomon@google.come269f212011-11-07 13:29:52 +0000561GrTexture* GrContext::createPlatformTexture(const GrPlatformTextureDesc& desc) {
562 return fGpu->createPlatformTexture(desc);
563}
564
565GrRenderTarget* GrContext::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
566 return fGpu->createPlatformRenderTarget(desc);
567}
568
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000569///////////////////////////////////////////////////////////////////////////////
570
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000571bool GrContext::supportsIndex8PixelConfig(const GrSamplerState* sampler,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000572 int width, int height) const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000573 const GrDrawTarget::Caps& caps = fGpu->getCaps();
574 if (!caps.f8BitPaletteSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000575 return false;
576 }
577
bsalomon@google.com27847de2011-02-22 20:59:41 +0000578 bool isPow2 = GrIsPow2(width) && GrIsPow2(height);
579
580 if (!isPow2) {
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000581 bool tiled = NULL != sampler &&
582 (sampler->getWrapX() != GrSamplerState::kClamp_WrapMode ||
583 sampler->getWrapY() != GrSamplerState::kClamp_WrapMode);
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000584 if (tiled && !caps.fNPOTTextureTileSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000585 return false;
586 }
587 }
588 return true;
589}
590
591////////////////////////////////////////////////////////////////////////////////
592
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000593const GrClip& GrContext::getClip() const { return fGpu->getClip(); }
594
bsalomon@google.com27847de2011-02-22 20:59:41 +0000595void GrContext::setClip(const GrClip& clip) {
596 fGpu->setClip(clip);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000597 fDrawState->enableState(GrDrawState::kClip_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000598}
599
600void GrContext::setClip(const GrIRect& rect) {
601 GrClip clip;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000602 clip.setFromIRect(rect);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000603 fGpu->setClip(clip);
604}
605
606////////////////////////////////////////////////////////////////////////////////
607
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000608void GrContext::clear(const GrIRect* rect, const GrColor color) {
bsalomon@google.com398109c2011-04-14 18:40:27 +0000609 this->flush();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000610 fGpu->clear(rect, color);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000611}
612
613void GrContext::drawPaint(const GrPaint& paint) {
614 // set rect to be big enough to fill the space, but not super-huge, so we
615 // don't overflow fixed-point implementations
bsalomon@google.comd302f142011-03-03 13:54:13 +0000616 GrRect r;
617 r.setLTRB(0, 0,
618 GrIntToScalar(getRenderTarget()->width()),
619 GrIntToScalar(getRenderTarget()->height()));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000620 GrMatrix inverse;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000621 SkTLazy<GrPaint> tmpPaint;
622 const GrPaint* p = &paint;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000623 GrAutoMatrix am;
624
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000625 // We attempt to map r by the inverse matrix and draw that. mapRect will
626 // map the four corners and bound them with a new rect. This will not
627 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000628 if (!this->getMatrix().hasPerspective()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000629 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000630 GrPrintf("Could not invert matrix");
631 return;
632 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000633 inverse.mapRect(&r);
634 } else {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000635 if (paint.getActiveMaskStageMask() || paint.getActiveStageMask()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000636 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000637 GrPrintf("Could not invert matrix");
638 return;
639 }
640 tmpPaint.set(paint);
641 tmpPaint.get()->preConcatActiveSamplerMatrices(inverse);
642 p = tmpPaint.get();
643 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000644 am.set(this, GrMatrix::I());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000645 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000646 // by definition this fills the entire clip, no need for AA
647 if (paint.fAntiAlias) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000648 if (!tmpPaint.isValid()) {
649 tmpPaint.set(paint);
650 p = tmpPaint.get();
651 }
652 GrAssert(p == tmpPaint.get());
653 tmpPaint.get()->fAntiAlias = false;
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000654 }
655 this->drawRect(*p, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000656}
657
bsalomon@google.com205d4602011-04-25 12:43:45 +0000658////////////////////////////////////////////////////////////////////////////////
659
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000660namespace {
661inline bool disable_coverage_aa_for_blend(GrDrawTarget* target) {
662 return DISABLE_COVERAGE_AA_FOR_BLEND && !target->canApplyCoverage();
663}
664}
665
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000666////////////////////////////////////////////////////////////////////////////////
667
bsalomon@google.com27847de2011-02-22 20:59:41 +0000668/* create a triangle strip that strokes the specified triangle. There are 8
669 unique vertices, but we repreat the last 2 to close up. Alternatively we
670 could use an indices array, and then only send 8 verts, but not sure that
671 would be faster.
672 */
bsalomon@google.com205d4602011-04-25 12:43:45 +0000673static void setStrokeRectStrip(GrPoint verts[10], GrRect rect,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000674 GrScalar width) {
675 const GrScalar rad = GrScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000676 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000677
678 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
679 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
680 verts[2].set(rect.fRight - rad, rect.fTop + rad);
681 verts[3].set(rect.fRight + rad, rect.fTop - rad);
682 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
683 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
684 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
685 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
686 verts[8] = verts[0];
687 verts[9] = verts[1];
688}
689
reed@google.com20efde72011-05-09 17:00:02 +0000690/**
691 * Returns true if the rects edges are integer-aligned.
692 */
693static bool isIRect(const GrRect& r) {
694 return GrScalarIsInt(r.fLeft) && GrScalarIsInt(r.fTop) &&
695 GrScalarIsInt(r.fRight) && GrScalarIsInt(r.fBottom);
696}
697
bsalomon@google.com205d4602011-04-25 12:43:45 +0000698static bool apply_aa_to_rect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000699 const GrRect& rect,
700 GrScalar width,
701 const GrMatrix* matrix,
702 GrMatrix* combinedMatrix,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000703 GrRect* devRect,
704 bool* useVertexCoverage) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000705 // we use a simple coverage ramp to do aa on axis-aligned rects
706 // we check if the rect will be axis-aligned, and the rect won't land on
707 // integer coords.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000708
bsalomon@google.coma3108262011-10-10 14:08:47 +0000709 // we are keeping around the "tweak the alpha" trick because
710 // it is our only hope for the fixed-pipe implementation.
711 // In a shader implementation we can give a separate coverage input
bsalomon@google.com289533a2011-10-27 12:34:25 +0000712 // TODO: remove this ugliness when we drop the fixed-pipe impl
bsalomon@google.coma3108262011-10-10 14:08:47 +0000713 *useVertexCoverage = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000714 if (!target->canTweakAlphaForCoverage()) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000715 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +0000716#if GR_DEBUG
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000717 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000718#endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000719 return false;
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000720 } else {
721 *useVertexCoverage = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000722 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000723 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000724 const GrDrawState& drawState = target->getDrawState();
725 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000726 return false;
727 }
728
bsalomon@google.com471d4712011-08-23 15:45:25 +0000729 if (0 == width && target->willUseHWAALines()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000730 return false;
731 }
732
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000733 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000734 return false;
735 }
736
737 if (NULL != matrix &&
738 !matrix->preservesAxisAlignment()) {
739 return false;
740 }
741
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000742 *combinedMatrix = drawState.getViewMatrix();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000743 if (NULL != matrix) {
744 combinedMatrix->preConcat(*matrix);
745 GrAssert(combinedMatrix->preservesAxisAlignment());
746 }
747
748 combinedMatrix->mapRect(devRect, rect);
749 devRect->sort();
750
751 if (width < 0) {
reed@google.com20efde72011-05-09 17:00:02 +0000752 return !isIRect(*devRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000753 } else {
754 return true;
755 }
756}
757
bsalomon@google.com27847de2011-02-22 20:59:41 +0000758void GrContext::drawRect(const GrPaint& paint,
759 const GrRect& rect,
760 GrScalar width,
761 const GrMatrix* matrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000762 SK_TRACE_EVENT0("GrContext::drawRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000763
764 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
robertphillips@google.com972265d2012-06-13 18:49:30 +0000765 GrDrawState::AutoTextureRelease atr(fDrawState);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000766 int stageMask = paint.getActiveStageMask();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000767
bsalomon@google.com205d4602011-04-25 12:43:45 +0000768 GrRect devRect = rect;
769 GrMatrix combinedMatrix;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000770 bool useVertexCoverage;
bsalomon@google.com289533a2011-10-27 12:34:25 +0000771 bool needAA = paint.fAntiAlias &&
772 !this->getRenderTarget()->isMultisampled();
773 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix,
774 &combinedMatrix, &devRect,
775 &useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000776
777 if (doAA) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000778 GrDrawTarget::AutoDeviceCoordDraw adcd(target, stageMask);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000779 if (width >= 0) {
780 GrVec strokeSize;;
781 if (width > 0) {
782 strokeSize.set(width, width);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000783 combinedMatrix.mapVectors(&strokeSize, 1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000784 strokeSize.setAbs(strokeSize);
785 } else {
786 strokeSize.set(GR_Scalar1, GR_Scalar1);
787 }
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000788 fAARectRenderer->strokeAARect(this->getGpu(), target, devRect,
789 strokeSize, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000790 } else {
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000791 fAARectRenderer->fillAARect(this->getGpu(), target,
792 devRect, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000793 }
794 return;
795 }
796
bsalomon@google.com27847de2011-02-22 20:59:41 +0000797 if (width >= 0) {
798 // TODO: consider making static vertex buffers for these cases.
799 // Hairline could be done by just adding closing vertex to
800 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000801 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
802
bsalomon@google.com27847de2011-02-22 20:59:41 +0000803 static const int worstCaseVertCount = 10;
804 GrDrawTarget::AutoReleaseGeometry geo(target, layout, worstCaseVertCount, 0);
805
806 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000807 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000808 return;
809 }
810
811 GrPrimitiveType primType;
812 int vertCount;
813 GrPoint* vertex = geo.positions();
814
815 if (width > 0) {
816 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000817 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000818 setStrokeRectStrip(vertex, rect, width);
819 } else {
820 // hairline
821 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000822 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000823 vertex[0].set(rect.fLeft, rect.fTop);
824 vertex[1].set(rect.fRight, rect.fTop);
825 vertex[2].set(rect.fRight, rect.fBottom);
826 vertex[3].set(rect.fLeft, rect.fBottom);
827 vertex[4].set(rect.fLeft, rect.fTop);
828 }
829
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000830 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000831 if (NULL != matrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000832 GrDrawState* drawState = target->drawState();
833 avmr.set(drawState);
834 drawState->preConcatViewMatrix(*matrix);
835 drawState->preConcatSamplerMatrices(stageMask, *matrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000836 }
837
838 target->drawNonIndexed(primType, 0, vertCount);
839 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000840#if GR_STATIC_RECT_VB
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000841 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000842 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
843 if (NULL == sqVB) {
844 GrPrintf("Failed to create static rect vb.\n");
845 return;
846 }
847 target->setVertexSourceToBuffer(layout, sqVB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000848 GrDrawState* drawState = target->drawState();
849 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000850 GrMatrix m;
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000851 m.setAll(rect.width(), 0, rect.fLeft,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000852 0, rect.height(), rect.fTop,
853 0, 0, GrMatrix::I()[8]);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000854
855 if (NULL != matrix) {
856 m.postConcat(*matrix);
857 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000858 drawState->preConcatViewMatrix(m);
859 drawState->preConcatSamplerMatrices(stageMask, m);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000860
bsalomon@google.com47059542012-06-06 20:51:20 +0000861 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000862#else
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000863 target->drawSimpleRect(rect, matrix, stageMask);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000864#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000865 }
866}
867
868void GrContext::drawRectToRect(const GrPaint& paint,
869 const GrRect& dstRect,
870 const GrRect& srcRect,
871 const GrMatrix* dstMatrix,
872 const GrMatrix* srcMatrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000873 SK_TRACE_EVENT0("GrContext::drawRectToRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000874
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000875 // srcRect refers to paint's first texture
876 if (NULL == paint.getTexture(0)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000877 drawRect(paint, dstRect, -1, dstMatrix);
878 return;
879 }
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000880
bsalomon@google.com27847de2011-02-22 20:59:41 +0000881 GR_STATIC_ASSERT(!BATCH_RECT_TO_RECT || !GR_STATIC_RECT_VB);
882
883#if GR_STATIC_RECT_VB
884 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
robertphillips@google.com972265d2012-06-13 18:49:30 +0000885 GrDrawState::AutoTextureRelease atr(fDrawState);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000886 GrDrawState* drawState = target->drawState();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000887 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000888 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000889
890 GrMatrix m;
891
892 m.setAll(dstRect.width(), 0, dstRect.fLeft,
893 0, dstRect.height(), dstRect.fTop,
894 0, 0, GrMatrix::I()[8]);
895 if (NULL != dstMatrix) {
896 m.postConcat(*dstMatrix);
897 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000898 drawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000899
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000900 // srcRect refers to first stage
901 int otherStageMask = paint.getActiveStageMask() &
902 (~(1 << GrPaint::kFirstTextureStage));
903 if (otherStageMask) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000904 drawState->preConcatSamplerMatrices(otherStageMask, m);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000905 }
906
bsalomon@google.com27847de2011-02-22 20:59:41 +0000907 m.setAll(srcRect.width(), 0, srcRect.fLeft,
908 0, srcRect.height(), srcRect.fTop,
909 0, 0, GrMatrix::I()[8]);
910 if (NULL != srcMatrix) {
911 m.postConcat(*srcMatrix);
912 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000913 drawState->sampler(GrPaint::kFirstTextureStage)->preConcatMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000914
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000915 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
916 if (NULL == sqVB) {
917 GrPrintf("Failed to create static rect vb.\n");
918 return;
919 }
920 target->setVertexSourceToBuffer(layout, sqVB);
bsalomon@google.com47059542012-06-06 20:51:20 +0000921 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000922#else
923
924 GrDrawTarget* target;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000925#if BATCH_RECT_TO_RECT
bsalomon@google.com27847de2011-02-22 20:59:41 +0000926 target = this->prepareToDraw(paint, kBuffered_DrawCategory);
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000927#else
bsalomon@google.com27847de2011-02-22 20:59:41 +0000928 target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
929#endif
robertphillips@google.com972265d2012-06-13 18:49:30 +0000930 GrDrawState::AutoTextureRelease atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000931
tomhudson@google.com93813632011-10-27 20:21:16 +0000932 const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
933 const GrMatrix* srcMatrices[GrDrawState::kNumStages] = {NULL};
bsalomon@google.com27847de2011-02-22 20:59:41 +0000934 srcRects[0] = &srcRect;
935 srcMatrices[0] = srcMatrix;
936
937 target->drawRect(dstRect, dstMatrix, 1, srcRects, srcMatrices);
938#endif
939}
940
941void GrContext::drawVertices(const GrPaint& paint,
942 GrPrimitiveType primitiveType,
943 int vertexCount,
944 const GrPoint positions[],
945 const GrPoint texCoords[],
946 const GrColor colors[],
947 const uint16_t indices[],
948 int indexCount) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000949 SK_TRACE_EVENT0("GrContext::drawVertices");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000950
951 GrDrawTarget::AutoReleaseGeometry geo;
952
953 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
robertphillips@google.com972265d2012-06-13 18:49:30 +0000954 GrDrawState::AutoTextureRelease atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000955
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000956 bool hasTexCoords[GrPaint::kTotalStages] = {
957 NULL != texCoords, // texCoordSrc provides explicit stage 0 coords
958 0 // remaining stages use positions
959 };
960
961 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, hasTexCoords);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000962
963 if (NULL != colors) {
964 layout |= GrDrawTarget::kColor_VertexLayoutBit;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000965 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000966 int vertexSize = GrDrawTarget::VertexSize(layout);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000967
968 if (sizeof(GrPoint) != vertexSize) {
969 if (!geo.set(target, layout, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000970 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000971 return;
972 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000973 int texOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000974 int colorOffset;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000975 GrDrawTarget::VertexSizeAndOffsetsByIdx(layout,
976 texOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000977 &colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000978 NULL,
979 NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000980 void* curVertex = geo.vertices();
981
982 for (int i = 0; i < vertexCount; ++i) {
983 *((GrPoint*)curVertex) = positions[i];
984
985 if (texOffsets[0] > 0) {
986 *(GrPoint*)((intptr_t)curVertex + texOffsets[0]) = texCoords[i];
987 }
988 if (colorOffset > 0) {
989 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
990 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000991 curVertex = (void*)((intptr_t)curVertex + vertexSize);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000992 }
993 } else {
994 target->setVertexSourceToArray(layout, positions, vertexCount);
995 }
996
bsalomon@google.com91958362011-06-13 17:58:13 +0000997 // we don't currently apply offscreen AA to this path. Need improved
998 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000999
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001000 if (NULL != indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001001 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001002 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001003 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001004 target->drawNonIndexed(primitiveType, 0, vertexCount);
1005 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001006}
1007
bsalomon@google.com06afe7b2011-04-26 15:31:40 +00001008///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com150d2842012-01-12 20:19:56 +00001009namespace {
1010
bsalomon@google.com93c96602012-04-27 13:05:21 +00001011struct CircleVertex {
1012 GrPoint fPos;
1013 GrPoint fCenter;
1014 GrScalar fOuterRadius;
1015 GrScalar fInnerRadius;
1016};
1017
1018/* Returns true if will map a circle to another circle. This can be true
1019 * if the matrix only includes square-scale, rotation, translation.
1020 */
1021inline bool isSimilarityTransformation(const SkMatrix& matrix,
1022 SkScalar tol = SK_ScalarNearlyZero) {
1023 if (matrix.isIdentity() || matrix.getType() == SkMatrix::kTranslate_Mask) {
1024 return true;
1025 }
1026 if (matrix.hasPerspective()) {
1027 return false;
1028 }
1029
1030 SkScalar mx = matrix.get(SkMatrix::kMScaleX);
1031 SkScalar sx = matrix.get(SkMatrix::kMSkewX);
1032 SkScalar my = matrix.get(SkMatrix::kMScaleY);
1033 SkScalar sy = matrix.get(SkMatrix::kMSkewY);
1034
1035 if (mx == 0 && sx == 0 && my == 0 && sy == 0) {
1036 return false;
1037 }
1038
1039 // it has scales or skews, but it could also be rotation, check it out.
1040 SkVector vec[2];
1041 vec[0].set(mx, sx);
1042 vec[1].set(sy, my);
1043
1044 return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) &&
1045 SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(),
1046 SkScalarSquare(tol));
1047}
1048
1049}
1050
1051// TODO: strokeWidth can't be larger than zero right now.
1052// It will be fixed when drawPath() can handle strokes.
1053void GrContext::drawOval(const GrPaint& paint,
1054 const GrRect& rect,
1055 SkScalar strokeWidth) {
1056 DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
1057 kUnbuffered_DrawCategory;
1058 GrDrawTarget* target = this->prepareToDraw(paint, category);
robertphillips@google.com972265d2012-06-13 18:49:30 +00001059 GrDrawState::AutoTextureRelease atr(fDrawState);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001060 GrDrawState* drawState = target->drawState();
1061 GrMatrix vm = drawState->getViewMatrix();
1062
1063 if (!isSimilarityTransformation(vm) ||
1064 !paint.fAntiAlias ||
1065 rect.height() != rect.width()) {
1066 SkPath path;
1067 path.addOval(rect);
1068 GrPathFill fill = (strokeWidth == 0) ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001069 kHairLine_GrPathFill : kWinding_GrPathFill;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001070 this->internalDrawPath(paint, path, fill, NULL);
1071 return;
1072 }
1073
1074 const GrRenderTarget* rt = drawState->getRenderTarget();
1075 if (NULL == rt) {
1076 return;
1077 }
1078
1079 GrDrawTarget::AutoDeviceCoordDraw adcd(target, paint.getActiveStageMask());
1080
1081 GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
1082 layout |= GrDrawTarget::kEdge_VertexLayoutBit;
1083 GrAssert(sizeof(CircleVertex) == GrDrawTarget::VertexSize(layout));
1084
1085 GrPoint center = GrPoint::Make(rect.centerX(), rect.centerY());
1086 GrScalar radius = SkScalarHalf(rect.width());
1087
1088 vm.mapPoints(&center, 1);
1089 radius = vm.mapRadius(radius);
1090
1091 GrScalar outerRadius = radius;
1092 GrScalar innerRadius = 0;
1093 SkScalar halfWidth = 0;
1094 if (strokeWidth == 0) {
1095 halfWidth = SkScalarHalf(SK_Scalar1);
1096
1097 outerRadius += halfWidth;
1098 innerRadius = SkMaxScalar(0, radius - halfWidth);
1099 }
1100
1101 GrDrawTarget::AutoReleaseGeometry geo(target, layout, 4, 0);
1102 if (!geo.succeeded()) {
1103 GrPrintf("Failed to get space for vertices!\n");
1104 return;
1105 }
1106
1107 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
1108
1109 SkScalar L = center.fX - outerRadius;
1110 SkScalar R = center.fX + outerRadius;
1111 SkScalar T = center.fY - outerRadius;
1112 SkScalar B = center.fY + outerRadius;
1113
1114 verts[0].fPos = SkPoint::Make(L, T);
1115 verts[1].fPos = SkPoint::Make(R, T);
1116 verts[2].fPos = SkPoint::Make(L, B);
1117 verts[3].fPos = SkPoint::Make(R, B);
1118
1119 for (int i = 0; i < 4; ++i) {
1120 // this goes to fragment shader, it should be in y-points-up space.
1121 verts[i].fCenter = SkPoint::Make(center.fX, rt->height() - center.fY);
1122
1123 verts[i].fOuterRadius = outerRadius;
1124 verts[i].fInnerRadius = innerRadius;
1125 }
1126
1127 drawState->setVertexEdgeType(GrDrawState::kCircle_EdgeType);
bsalomon@google.com47059542012-06-06 20:51:20 +00001128 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001129}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001130
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001131void GrContext::drawPath(const GrPaint& paint, const SkPath& path,
reed@google.com07f3ee12011-05-16 17:21:57 +00001132 GrPathFill fill, const GrPoint* translate) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001133
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001134 if (path.isEmpty()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001135 if (GrIsFillInverted(fill)) {
1136 this->drawPaint(paint);
1137 }
1138 return;
1139 }
1140
bsalomon@google.com93c96602012-04-27 13:05:21 +00001141 SkRect ovalRect;
1142 if (!GrIsFillInverted(fill) && path.isOval(&ovalRect)) {
1143 if (translate) {
1144 ovalRect.offset(*translate);
1145 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001146 SkScalar width = (fill == kHairLine_GrPathFill) ? 0 : -SK_Scalar1;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001147 this->drawOval(paint, ovalRect, width);
1148 return;
1149 }
1150
1151 internalDrawPath(paint, path, fill, translate);
1152}
1153
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001154void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +00001155 GrPathFill fill, const GrPoint* translate) {
1156
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001157 // Note that below we may sw-rasterize the path into a scratch texture.
1158 // Scratch textures can be recycled after they are returned to the texture
1159 // cache. This presents a potential hazard for buffered drawing. However,
1160 // the writePixels that uploads to the scratch will perform a flush so we're
1161 // OK.
1162 DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
1163 kUnbuffered_DrawCategory;
1164 GrDrawTarget* target = this->prepareToDraw(paint, category);
robertphillips@google.com972265d2012-06-13 18:49:30 +00001165 GrDrawState::AutoTextureRelease atr(fDrawState);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001166 GrDrawState::StageMask stageMask = paint.getActiveStageMask();
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001167
bsalomon@google.com289533a2011-10-27 12:34:25 +00001168 bool prAA = paint.fAntiAlias && !this->getRenderTarget()->isMultisampled();
1169
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001170 // An Assumption here is that path renderer would use some form of tweaking
1171 // the src color (either the input alpha or in the frag shader) to implement
1172 // aa. If we have some future driver-mojo path AA that can do the right
1173 // thing WRT to the blend then we'll need some query on the PR.
1174 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001175#if GR_DEBUG
bsalomon@google.com979432b2011-11-05 21:38:22 +00001176 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001177#endif
bsalomon@google.com289533a2011-10-27 12:34:25 +00001178 prAA = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001179 }
bsalomon@google.com289533a2011-10-27 12:34:25 +00001180
robertphillips@google.com72176b22012-05-23 13:19:12 +00001181 GrPathRenderer* pr = this->getPathRenderer(path, fill, target, prAA, true);
bsalomon@google.com30085192011-08-19 15:42:31 +00001182 if (NULL == pr) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001183#if GR_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001184 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001185#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001186 return;
1187 }
1188
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001189 pr->drawPath(path, fill, translate, target, stageMask, prAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001190}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001191
bsalomon@google.com27847de2011-02-22 20:59:41 +00001192////////////////////////////////////////////////////////////////////////////////
1193
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001194void GrContext::flush(int flagsBitfield) {
1195 if (kDiscard_FlushBit & flagsBitfield) {
1196 fDrawBuffer->reset();
1197 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001198 this->flushDrawBuffer();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001199 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001200 if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001201 fGpu->forceRenderTargetFlush();
1202 }
1203}
1204
bsalomon@google.com27847de2011-02-22 20:59:41 +00001205void GrContext::flushDrawBuffer() {
junov@google.com53a55842011-06-08 22:55:10 +00001206 if (fDrawBuffer) {
robertphillips@google.com58b38182012-05-03 16:29:41 +00001207 // With addition of the AA clip path, flushing the draw buffer can
1208 // result in the generation of an AA clip mask. During this
1209 // process the SW path renderer may be invoked which recusively
1210 // calls this method (via internalWriteTexturePixels) creating
1211 // infinite recursion
1212 GrInOrderDrawBuffer* temp = fDrawBuffer;
1213 fDrawBuffer = NULL;
1214
1215 temp->flushTo(fGpu);
1216
1217 fDrawBuffer = temp;
junov@google.com53a55842011-06-08 22:55:10 +00001218 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001219}
1220
bsalomon@google.com6f379512011-11-16 20:36:03 +00001221void GrContext::internalWriteTexturePixels(GrTexture* texture,
1222 int left, int top,
1223 int width, int height,
1224 GrPixelConfig config,
1225 const void* buffer,
1226 size_t rowBytes,
1227 uint32_t flags) {
1228 SK_TRACE_EVENT0("GrContext::writeTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001229 ASSERT_OWNED_RESOURCE(texture);
1230
bsalomon@google.com6f379512011-11-16 20:36:03 +00001231 if (!(kDontFlush_PixelOpsFlag & flags)) {
1232 this->flush();
1233 }
1234 // TODO: use scratch texture to perform conversion
1235 if (GrPixelConfigIsUnpremultiplied(texture->config()) !=
1236 GrPixelConfigIsUnpremultiplied(config)) {
1237 return;
1238 }
1239
1240 fGpu->writeTexturePixels(texture, left, top, width, height,
1241 config, buffer, rowBytes);
1242}
1243
1244bool GrContext::internalReadTexturePixels(GrTexture* texture,
1245 int left, int top,
1246 int width, int height,
1247 GrPixelConfig config,
1248 void* buffer,
1249 size_t rowBytes,
1250 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001251 SK_TRACE_EVENT0("GrContext::readTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001252 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001253
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001254 // TODO: code read pixels for textures that aren't also rendertargets
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001255 GrRenderTarget* target = texture->asRenderTarget();
1256 if (NULL != target) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001257 return this->internalReadRenderTargetPixels(target,
1258 left, top, width, height,
1259 config, buffer, rowBytes,
1260 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001261 } else {
1262 return false;
1263 }
1264}
1265
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001266#include "SkConfig8888.h"
1267
1268namespace {
1269/**
1270 * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel
1271 * formats are representable as Config8888 and so the function returns false
1272 * if the GrPixelConfig has no equivalent Config8888.
1273 */
1274bool grconfig_to_config8888(GrPixelConfig config,
1275 SkCanvas::Config8888* config8888) {
1276 switch (config) {
1277 case kRGBA_8888_PM_GrPixelConfig:
1278 *config8888 = SkCanvas::kRGBA_Premul_Config8888;
1279 return true;
1280 case kRGBA_8888_UPM_GrPixelConfig:
1281 *config8888 = SkCanvas::kRGBA_Unpremul_Config8888;
1282 return true;
1283 case kBGRA_8888_PM_GrPixelConfig:
1284 *config8888 = SkCanvas::kBGRA_Premul_Config8888;
1285 return true;
1286 case kBGRA_8888_UPM_GrPixelConfig:
1287 *config8888 = SkCanvas::kBGRA_Unpremul_Config8888;
1288 return true;
1289 default:
1290 return false;
1291 }
1292}
1293}
1294
bsalomon@google.com6f379512011-11-16 20:36:03 +00001295bool GrContext::internalReadRenderTargetPixels(GrRenderTarget* target,
1296 int left, int top,
1297 int width, int height,
1298 GrPixelConfig config,
1299 void* buffer,
1300 size_t rowBytes,
1301 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001302 SK_TRACE_EVENT0("GrContext::readRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001303 ASSERT_OWNED_RESOURCE(target);
1304
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001305 if (NULL == target) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001306 target = fDrawState->getRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001307 if (NULL == target) {
1308 return false;
1309 }
1310 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001311
bsalomon@google.com6f379512011-11-16 20:36:03 +00001312 if (!(kDontFlush_PixelOpsFlag & flags)) {
1313 this->flush();
1314 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001315
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001316 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1317 GrPixelConfigIsUnpremultiplied(config) &&
1318 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1319 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1320 if (!grconfig_to_config8888(target->config(), &srcConfig8888) ||
1321 !grconfig_to_config8888(config, &dstConfig8888)) {
1322 return false;
1323 }
1324 // do read back using target's own config
1325 this->internalReadRenderTargetPixels(target,
1326 left, top,
1327 width, height,
1328 target->config(),
1329 buffer, rowBytes,
1330 kDontFlush_PixelOpsFlag);
1331 // sw convert the pixels to unpremul config
1332 uint32_t* pixels = reinterpret_cast<uint32_t*>(buffer);
1333 SkConvertConfig8888Pixels(pixels, rowBytes, dstConfig8888,
1334 pixels, rowBytes, srcConfig8888,
1335 width, height);
1336 return true;
1337 }
1338
bsalomon@google.comc4364992011-11-07 15:54:49 +00001339 GrTexture* src = target->asTexture();
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001340 bool swapRAndB = NULL != src &&
1341 fGpu->preferredReadPixelsConfig(config) ==
1342 GrPixelConfigSwapRAndB(config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001343
1344 bool flipY = NULL != src &&
1345 fGpu->readPixelsWillPayForYFlip(target, left, top,
1346 width, height, config,
1347 rowBytes);
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001348 bool alphaConversion = (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1349 GrPixelConfigIsUnpremultiplied(config));
bsalomon@google.comc4364992011-11-07 15:54:49 +00001350
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001351 if (NULL == src && alphaConversion) {
1352 // we should fallback to cpu conversion here. This could happen when
1353 // we were given an external render target by the client that is not
1354 // also a texture (e.g. FBO 0 in GL)
1355 return false;
1356 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001357 // we draw to a scratch texture if any of these conversion are applied
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001358 GrAutoScratchTexture ast;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001359 if (flipY || swapRAndB || alphaConversion) {
1360 GrAssert(NULL != src);
1361 if (swapRAndB) {
1362 config = GrPixelConfigSwapRAndB(config);
1363 GrAssert(kUnknown_GrPixelConfig != config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001364 }
1365 // Make the scratch a render target because we don't have a robust
1366 // readTexturePixels as of yet (it calls this function).
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001367 GrTextureDesc desc;
1368 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1369 desc.fWidth = width;
1370 desc.fHeight = height;
1371 desc.fConfig = config;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001372
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001373 // When a full readback is faster than a partial we could always make
1374 // the scratch exactly match the passed rect. However, if we see many
1375 // different size rectangles we will trash our texture cache and pay the
1376 // cost of creating and destroying many textures. So, we only request
1377 // an exact match when the caller is reading an entire RT.
1378 ScratchTexMatch match = kApprox_ScratchTexMatch;
1379 if (0 == left &&
1380 0 == top &&
1381 target->width() == width &&
1382 target->height() == height &&
1383 fGpu->fullReadPixelsIsFasterThanPartial()) {
1384 match = kExact_ScratchTexMatch;
1385 }
1386 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001387 GrTexture* texture = ast.texture();
1388 if (!texture) {
1389 return false;
1390 }
1391 target = texture->asRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001392 GrAssert(NULL != target);
1393
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001394 GrDrawTarget::AutoStateRestore asr(fGpu,
1395 GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001396 GrDrawState* drawState = fGpu->drawState();
1397 drawState->setRenderTarget(target);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001398
bsalomon@google.comc4364992011-11-07 15:54:49 +00001399 GrMatrix matrix;
1400 if (flipY) {
1401 matrix.setTranslate(SK_Scalar1 * left,
1402 SK_Scalar1 * (top + height));
1403 matrix.set(GrMatrix::kMScaleY, -GR_Scalar1);
1404 } else {
1405 matrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
1406 }
1407 matrix.postIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001408 drawState->sampler(0)->reset(matrix);
1409 drawState->sampler(0)->setRAndBSwap(swapRAndB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001410 drawState->setTexture(0, src);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001411 GrRect rect;
1412 rect.setXYWH(0, 0, SK_Scalar1 * width, SK_Scalar1 * height);
1413 fGpu->drawSimpleRect(rect, NULL, 0x1);
1414 left = 0;
1415 top = 0;
1416 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001417 return fGpu->readPixels(target,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001418 left, top, width, height,
1419 config, buffer, rowBytes, flipY);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001420}
1421
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001422void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1423 GrAssert(target);
1424 ASSERT_OWNED_RESOURCE(target);
1425 // In the future we may track whether there are any pending draws to this
1426 // target. We don't today so we always perform a flush. We don't promise
1427 // this to our clients, though.
1428 this->flush();
1429 fGpu->resolveRenderTarget(target);
1430}
1431
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001432void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst) {
1433 if (NULL == src || NULL == dst) {
1434 return;
1435 }
1436 ASSERT_OWNED_RESOURCE(src);
1437
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001438 // Writes pending to the source texture are not tracked, so a flush
1439 // is required to ensure that the copy captures the most recent contents
1440 // of the source texture. See similar behaviour in
1441 // GrContext::resolveRenderTarget.
1442 this->flush();
1443
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001444 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001445 GrDrawState* drawState = fGpu->drawState();
1446 drawState->setRenderTarget(dst);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001447 GrMatrix sampleM;
1448 sampleM.setIDiv(src->width(), src->height());
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001449 drawState->setTexture(0, src);
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001450 drawState->sampler(0)->reset(sampleM);
bsalomon@google.com5db3b6c2012-01-12 20:38:57 +00001451 SkRect rect = SkRect::MakeXYWH(0, 0,
1452 SK_Scalar1 * src->width(),
1453 SK_Scalar1 * src->height());
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001454 fGpu->drawSimpleRect(rect, NULL, 1 << 0);
1455}
1456
bsalomon@google.com6f379512011-11-16 20:36:03 +00001457void GrContext::internalWriteRenderTargetPixels(GrRenderTarget* target,
1458 int left, int top,
1459 int width, int height,
1460 GrPixelConfig config,
1461 const void* buffer,
1462 size_t rowBytes,
1463 uint32_t flags) {
1464 SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001465 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001466
1467 if (NULL == target) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001468 target = fDrawState->getRenderTarget();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001469 if (NULL == target) {
1470 return;
1471 }
1472 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001473
1474 // TODO: when underlying api has a direct way to do this we should use it
1475 // (e.g. glDrawPixels on desktop GL).
1476
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001477 // If the RT is also a texture and we don't have to do PM/UPM conversion
1478 // then take the texture path, which we expect to be at least as fast or
1479 // faster since it doesn't use an intermediate texture as we do below.
1480
1481#if !GR_MAC_BUILD
1482 // At least some drivers on the Mac get confused when glTexImage2D is called
1483 // on a texture attached to an FBO. The FBO still sees the old image. TODO:
1484 // determine what OS versions and/or HW is affected.
1485 if (NULL != target->asTexture() &&
1486 GrPixelConfigIsUnpremultiplied(target->config()) ==
1487 GrPixelConfigIsUnpremultiplied(config)) {
1488
1489 this->internalWriteTexturePixels(target->asTexture(),
1490 left, top, width, height,
1491 config, buffer, rowBytes, flags);
1492 return;
1493 }
1494#endif
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001495 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1496 GrPixelConfigIsUnpremultiplied(config) &&
1497 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1498 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1499 if (!grconfig_to_config8888(config, &srcConfig8888) ||
1500 !grconfig_to_config8888(target->config(), &dstConfig8888)) {
1501 return;
1502 }
1503 // allocate a tmp buffer and sw convert the pixels to premul
1504 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(width * height);
1505 const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer);
1506 SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888,
1507 src, rowBytes, srcConfig8888,
1508 width, height);
1509 // upload the already premul pixels
1510 this->internalWriteRenderTargetPixels(target,
1511 left, top,
1512 width, height,
1513 target->config(),
1514 tmpPixels, 4 * width, flags);
1515 return;
1516 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001517
1518 bool swapRAndB = fGpu->preferredReadPixelsConfig(config) ==
1519 GrPixelConfigSwapRAndB(config);
1520 if (swapRAndB) {
1521 config = GrPixelConfigSwapRAndB(config);
1522 }
1523
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001524 GrTextureDesc desc;
1525 desc.fWidth = width;
1526 desc.fHeight = height;
1527 desc.fConfig = config;
1528
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001529 GrAutoScratchTexture ast(this, desc);
1530 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001531 if (NULL == texture) {
1532 return;
1533 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001534 this->internalWriteTexturePixels(texture, 0, 0, width, height,
1535 config, buffer, rowBytes, flags);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001536
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001537 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001538 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001539
1540 GrMatrix matrix;
1541 matrix.setTranslate(GrIntToScalar(left), GrIntToScalar(top));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001542 drawState->setViewMatrix(matrix);
1543 drawState->setRenderTarget(target);
1544 drawState->setTexture(0, texture);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001545
bsalomon@google.com5c638652011-07-18 19:31:59 +00001546 matrix.setIDiv(texture->width(), texture->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001547 drawState->sampler(0)->reset(GrSamplerState::kClamp_WrapMode,
1548 GrSamplerState::kNearest_Filter,
1549 matrix);
1550 drawState->sampler(0)->setRAndBSwap(swapRAndB);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001551
1552 GrVertexLayout layout = GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(0);
1553 static const int VCOUNT = 4;
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001554 // TODO: Use GrGpu::drawRect here
bsalomon@google.com27847de2011-02-22 20:59:41 +00001555 GrDrawTarget::AutoReleaseGeometry geo(fGpu, layout, VCOUNT, 0);
1556 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001557 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +00001558 return;
1559 }
1560 ((GrPoint*)geo.vertices())->setIRectFan(0, 0, width, height);
bsalomon@google.com47059542012-06-06 20:51:20 +00001561 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, VCOUNT);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001562}
1563////////////////////////////////////////////////////////////////////////////////
1564
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001565void GrContext::setPaint(const GrPaint& paint) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001566
1567 for (int i = 0; i < GrPaint::kMaxTextures; ++i) {
1568 int s = i + GrPaint::kFirstTextureStage;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001569 fDrawState->setTexture(s, paint.getTexture(i));
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001570 ASSERT_OWNED_RESOURCE(paint.getTexture(i));
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001571 if (paint.getTexture(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001572 *fDrawState->sampler(s) = paint.getTextureSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001573 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001574 }
1575
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001576 fDrawState->setFirstCoverageStage(GrPaint::kFirstMaskStage);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001577
1578 for (int i = 0; i < GrPaint::kMaxMasks; ++i) {
1579 int s = i + GrPaint::kFirstMaskStage;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001580 fDrawState->setTexture(s, paint.getMask(i));
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001581 ASSERT_OWNED_RESOURCE(paint.getMask(i));
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001582 if (paint.getMask(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001583 *fDrawState->sampler(s) = paint.getMaskSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001584 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001585 }
bsalomon@google.com26936d02012-03-19 13:06:19 +00001586
1587 // disable all stages not accessible via the paint
1588 for (int s = GrPaint::kTotalStages; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001589 fDrawState->setTexture(s, NULL);
bsalomon@google.com26936d02012-03-19 13:06:19 +00001590 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001591
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001592 fDrawState->setColor(paint.fColor);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001593
1594 if (paint.fDither) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001595 fDrawState->enableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001596 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001597 fDrawState->disableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001598 }
1599 if (paint.fAntiAlias) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001600 fDrawState->enableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001601 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001602 fDrawState->disableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001603 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001604 if (paint.fColorMatrixEnabled) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001605 fDrawState->enableState(GrDrawState::kColorMatrix_StateBit);
1606 fDrawState->setColorMatrix(paint.fColorMatrix);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001607 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001608 fDrawState->disableState(GrDrawState::kColorMatrix_StateBit);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001609 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001610 fDrawState->setBlendFunc(paint.fSrcBlendCoeff, paint.fDstBlendCoeff);
1611 fDrawState->setColorFilter(paint.fColorFilterColor, paint.fColorFilterXfermode);
1612 fDrawState->setCoverage(paint.fCoverage);
reed@google.com4b2d3f32012-05-15 18:05:50 +00001613#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
bsalomon@google.come79c8152012-03-29 19:07:12 +00001614 if ((paint.getActiveMaskStageMask() || 0xff != paint.fCoverage) &&
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001615 !fGpu->canApplyCoverage()) {
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001616 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1617 }
bsalomon@google.com95cd7bd2012-03-28 15:35:05 +00001618#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001619}
1620
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001621GrDrawTarget* GrContext::prepareToDraw(const GrPaint& paint,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001622 DrawCategory category) {
1623 if (category != fLastDrawCategory) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001624 this->flushDrawBuffer();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001625 fLastDrawCategory = category;
1626 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001627 this->setPaint(paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001628 GrDrawTarget* target = fGpu;
1629 switch (category) {
bsalomon@google.com193395c2012-03-30 17:35:12 +00001630 case kUnbuffered_DrawCategory:
1631 target = fGpu;
1632 break;
1633 case kBuffered_DrawCategory:
1634 target = fDrawBuffer;
1635 fDrawBuffer->setClip(fGpu->getClip());
1636 break;
1637 default:
1638 GrCrash("Unexpected DrawCategory.");
1639 break;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001640 }
1641 return target;
1642}
1643
robertphillips@google.com72176b22012-05-23 13:19:12 +00001644/*
1645 * This method finds a path renderer that can draw the specified path on
1646 * the provided target.
1647 * Due to its expense, the software path renderer has split out so it can
1648 * can be individually allowed/disallowed via the "allowSW" boolean.
1649 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001650GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
bsalomon@google.com289533a2011-10-27 12:34:25 +00001651 GrPathFill fill,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001652 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001653 bool antiAlias,
1654 bool allowSW) {
bsalomon@google.com30085192011-08-19 15:42:31 +00001655 if (NULL == fPathRendererChain) {
1656 fPathRendererChain =
1657 new GrPathRendererChain(this, GrPathRendererChain::kNone_UsageFlag);
1658 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001659
1660 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path, fill,
1661 target,
1662 antiAlias);
1663
1664 if (NULL == pr && allowSW) {
1665 if (NULL == fSoftwarePathRenderer) {
1666 fSoftwarePathRenderer = new GrSoftwarePathRenderer(this);
1667 }
1668
1669 pr = fSoftwarePathRenderer;
1670 }
1671
1672 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001673}
1674
bsalomon@google.com27847de2011-02-22 20:59:41 +00001675////////////////////////////////////////////////////////////////////////////////
1676
bsalomon@google.com27847de2011-02-22 20:59:41 +00001677void GrContext::setRenderTarget(GrRenderTarget* target) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001678 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001679 if (fDrawState->getRenderTarget() != target) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001680 this->flush(false);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001681 fDrawState->setRenderTarget(target);
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001682 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001683}
1684
1685GrRenderTarget* GrContext::getRenderTarget() {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001686 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001687}
1688
1689const GrRenderTarget* GrContext::getRenderTarget() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001690 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001691}
1692
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001693bool GrContext::isConfigRenderable(GrPixelConfig config) const {
1694 return fGpu->isConfigRenderable(config);
1695}
1696
bsalomon@google.com27847de2011-02-22 20:59:41 +00001697const GrMatrix& GrContext::getMatrix() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001698 return fDrawState->getViewMatrix();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001699}
1700
1701void GrContext::setMatrix(const GrMatrix& m) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001702 fDrawState->setViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001703}
1704
1705void GrContext::concatMatrix(const GrMatrix& m) const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001706 fDrawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001707}
1708
1709static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
1710 intptr_t mask = 1 << shift;
1711 if (pred) {
1712 bits |= mask;
1713 } else {
1714 bits &= ~mask;
1715 }
1716 return bits;
1717}
1718
bsalomon@google.com583a1e32011-08-17 13:42:46 +00001719GrContext::GrContext(GrGpu* gpu) {
bsalomon@google.comc0af3172012-06-15 14:10:09 +00001720 ++THREAD_INSTANCE_COUNT;
1721
bsalomon@google.com27847de2011-02-22 20:59:41 +00001722 fGpu = gpu;
1723 fGpu->ref();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001724 fGpu->setContext(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001725
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001726 fDrawState = new GrDrawState();
1727 fGpu->setDrawState(fDrawState);
1728
bsalomon@google.com30085192011-08-19 15:42:31 +00001729 fPathRendererChain = NULL;
robertphillips@google.com72176b22012-05-23 13:19:12 +00001730 fSoftwarePathRenderer = NULL;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001731
bsalomon@google.com8f7e1da2012-06-21 19:48:32 +00001732 fTextureCache = new GrResourceCache(kDefaultTextureCacheBudget);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001733 fFontCache = new GrFontCache(fGpu);
1734
1735 fLastDrawCategory = kUnbuffered_DrawCategory;
1736
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001737 fDrawBuffer = NULL;
1738 fDrawBufferVBAllocPool = NULL;
1739 fDrawBufferIBAllocPool = NULL;
1740
robertphillips@google.comf6747b02012-06-12 00:32:28 +00001741 fAARectRenderer = new GrAARectRenderer;
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001742
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001743 this->setupDrawBuffer();
1744}
1745
1746void GrContext::setupDrawBuffer() {
1747
1748 GrAssert(NULL == fDrawBuffer);
1749 GrAssert(NULL == fDrawBufferVBAllocPool);
1750 GrAssert(NULL == fDrawBufferIBAllocPool);
1751
bsalomon@google.com92edd312012-04-04 21:40:21 +00001752#if DEFER_TEXT_RENDERING || BATCH_RECT_TO_RECT || DEFER_PATHS
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001753 fDrawBufferVBAllocPool =
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001754 new GrVertexBufferAllocPool(fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001755 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
1756 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS);
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001757 fDrawBufferIBAllocPool =
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001758 new GrIndexBufferAllocPool(fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001759 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001760 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS);
1761
bsalomon@google.com471d4712011-08-23 15:45:25 +00001762 fDrawBuffer = new GrInOrderDrawBuffer(fGpu,
1763 fDrawBufferVBAllocPool,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001764 fDrawBufferIBAllocPool);
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001765#endif
1766
1767#if BATCH_RECT_TO_RECT
bsalomon@google.com27847de2011-02-22 20:59:41 +00001768 fDrawBuffer->setQuadIndexBuffer(this->getQuadIndexBuffer());
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001769#endif
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001770 fDrawBuffer->setAutoFlushTarget(fGpu);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001771 fDrawBuffer->setDrawState(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001772}
1773
bsalomon@google.com27847de2011-02-22 20:59:41 +00001774GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001775#if DEFER_TEXT_RENDERING
bsalomon@google.com193395c2012-03-30 17:35:12 +00001776 return prepareToDraw(paint, kBuffered_DrawCategory);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001777#else
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001778 return prepareToDraw(paint, kUnbuffered_DrawCategory);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001779#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001780}
1781
1782const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1783 return fGpu->getQuadIndexBuffer();
1784}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001785
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001786GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture,
1787 GrAutoScratchTexture* temp1,
1788 GrAutoScratchTexture* temp2,
1789 const SkRect& rect,
1790 float sigmaX, float sigmaY) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001791 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001792 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
1793 GrClip oldClip = this->getClip();
1794 GrTexture* origTexture = srcTexture;
1795 GrAutoMatrix avm(this, GrMatrix::I());
1796 SkIRect clearRect;
bsalomon@google.comb505a122012-05-31 18:40:36 +00001797 int scaleFactorX, radiusX;
1798 int scaleFactorY, radiusY;
1799 sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX);
1800 sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY);
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001801
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001802 SkRect srcRect(rect);
1803 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
1804 srcRect.roundOut();
robertphillips@google.com8637a362012-04-10 18:32:35 +00001805 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
1806 static_cast<float>(scaleFactorY));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001807 this->setClip(srcRect);
1808
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001809 GrAssert(kBGRA_8888_PM_GrPixelConfig == srcTexture->config() ||
1810 kRGBA_8888_PM_GrPixelConfig == srcTexture->config() ||
1811 kAlpha_8_GrPixelConfig == srcTexture->config());
1812
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001813 GrTextureDesc desc;
1814 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1815 desc.fWidth = SkScalarFloorToInt(srcRect.width());
1816 desc.fHeight = SkScalarFloorToInt(srcRect.height());
1817 desc.fConfig = srcTexture->config();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001818
1819 temp1->set(this, desc);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001820 if (temp2) {
1821 temp2->set(this, desc);
1822 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001823
1824 GrTexture* dstTexture = temp1->texture();
1825 GrPaint paint;
1826 paint.reset();
1827 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
1828
1829 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
1830 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1831 srcTexture->height());
1832 this->setRenderTarget(dstTexture->asRenderTarget());
1833 SkRect dstRect(srcRect);
1834 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
1835 i < scaleFactorY ? 0.5f : 1.0f);
1836 paint.setTexture(0, srcTexture);
1837 this->drawRectToRect(paint, dstRect, srcRect);
1838 srcRect = dstRect;
1839 SkTSwap(srcTexture, dstTexture);
1840 // If temp2 is non-NULL, don't render back to origTexture
robertphillips@google.com972265d2012-06-13 18:49:30 +00001841 if (temp2 && dstTexture == origTexture) {
1842 dstTexture = temp2->texture();
1843 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001844 }
1845
robertphillips@google.com7a396332012-05-10 15:11:27 +00001846 SkIRect srcIRect;
1847 srcRect.roundOut(&srcIRect);
1848
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001849 if (sigmaX > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001850 if (scaleFactorX > 1) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001851 // Clear out a radius to the right of the srcRect to prevent the
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001852 // X convolution from reading garbage.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001853 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001854 radiusX, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001855 this->clear(&clearRect, 0x0);
1856 }
1857
1858 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001859 convolve_gaussian(fGpu, srcTexture, srcRect, sigmaX, radiusX,
1860 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001861 SkTSwap(srcTexture, dstTexture);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001862 if (temp2 && dstTexture == origTexture) {
1863 dstTexture = temp2->texture();
1864 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001865 }
1866
1867 if (sigmaY > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001868 if (scaleFactorY > 1 || sigmaX > 0.0f) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001869 // Clear out a radius below the srcRect to prevent the Y
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001870 // convolution from reading garbage.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001871 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001872 srcIRect.width(), radiusY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001873 this->clear(&clearRect, 0x0);
1874 }
1875
1876 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001877 convolve_gaussian(fGpu, srcTexture, srcRect, sigmaY, radiusY,
1878 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001879 SkTSwap(srcTexture, dstTexture);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001880 if (temp2 && dstTexture == origTexture) {
1881 dstTexture = temp2->texture();
1882 }
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001883 }
1884
1885 if (scaleFactorX > 1 || scaleFactorY > 1) {
1886 // Clear one pixel to the right and below, to accommodate bilinear
1887 // upsampling.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001888 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
1889 srcIRect.width() + 1, 1);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001890 this->clear(&clearRect, 0x0);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001891 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
1892 1, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001893 this->clear(&clearRect, 0x0);
1894 // FIXME: This should be mitchell, not bilinear.
1895 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
1896 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1897 srcTexture->height());
1898 this->setRenderTarget(dstTexture->asRenderTarget());
1899 paint.setTexture(0, srcTexture);
1900 SkRect dstRect(srcRect);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001901 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001902 this->drawRectToRect(paint, dstRect, srcRect);
1903 srcRect = dstRect;
1904 SkTSwap(srcTexture, dstTexture);
1905 }
1906 this->setRenderTarget(oldRenderTarget);
1907 this->setClip(oldClip);
1908 return srcTexture;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001909}
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001910
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001911GrTexture* GrContext::applyMorphology(GrTexture* srcTexture,
1912 const GrRect& rect,
1913 GrTexture* temp1, GrTexture* temp2,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001914 MorphologyType morphType,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001915 SkISize radius) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001916 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001917 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
1918 GrAutoMatrix avm(this, GrMatrix::I());
1919 GrClip oldClip = this->getClip();
robertphillips@google.com7a396332012-05-10 15:11:27 +00001920 this->setClip(GrRect::MakeWH(SkIntToScalar(srcTexture->width()),
1921 SkIntToScalar(srcTexture->height())));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001922 if (radius.fWidth > 0) {
1923 this->setRenderTarget(temp1->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001924 apply_morphology(fGpu, srcTexture, rect, radius.fWidth, morphType,
1925 Gr1DKernelEffect::kX_Direction);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001926 SkIRect clearRect = SkIRect::MakeXYWH(
1927 SkScalarFloorToInt(rect.fLeft),
1928 SkScalarFloorToInt(rect.fBottom),
1929 SkScalarFloorToInt(rect.width()),
1930 radius.fHeight);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001931 this->clear(&clearRect, 0x0);
1932 srcTexture = temp1;
1933 }
1934 if (radius.fHeight > 0) {
1935 this->setRenderTarget(temp2->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001936 apply_morphology(fGpu, srcTexture, rect, radius.fHeight, morphType,
1937 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001938 srcTexture = temp2;
1939 }
1940 this->setRenderTarget(oldRenderTarget);
1941 this->setClip(oldClip);
1942 return srcTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001943}
bsalomon@google.comc4364992011-11-07 15:54:49 +00001944
robertphillips@google.com49d9fd52012-05-23 11:44:08 +00001945void GrContext::postClipPush() {
1946 fGpu->postClipPush();
1947}
1948
1949void GrContext::preClipPop() {
1950 fGpu->preClipPop();
1951};
1952
bsalomon@google.comc4364992011-11-07 15:54:49 +00001953///////////////////////////////////////////////////////////////////////////////