blob: 6ea8531b15f5e6ed7a08f3096e5ca612c3476006 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
bsalomon@google.com27847de2011-02-22 20:59:41 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
bsalomon@google.com27847de2011-02-22 20:59:41 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
bsalomon@google.com1fadb202011-12-12 16:10:08 +000010#include "GrContext.h"
11
bsalomon@google.comb505a122012-05-31 18:40:36 +000012#include "effects/GrMorphologyEffect.h"
13#include "effects/GrConvolutionEffect.h"
tomhudson@google.comaa72eab2012-07-19 18:01:07 +000014#include "effects/GrSingleTextureEffect.h"
bsalomon@google.comb505a122012-05-31 18:40:36 +000015
tomhudson@google.com278cbb42011-06-30 19:37:01 +000016#include "GrBufferAllocPool.h"
bsalomon@google.com05ef5102011-05-02 21:14:59 +000017#include "GrGpu.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000018#include "GrIndexBuffer.h"
19#include "GrInOrderDrawBuffer.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000020#include "GrPathRenderer.h"
tomhudson@google.comd22b6e42011-06-24 15:53:40 +000021#include "GrPathUtils.h"
bsalomon@google.com50398bf2011-07-26 20:45:30 +000022#include "GrResourceCache.h"
robertphillips@google.com72176b22012-05-23 13:19:12 +000023#include "GrSoftwarePathRenderer.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000024#include "GrStencilBuffer.h"
tomhudson@google.com278cbb42011-06-30 19:37:01 +000025#include "GrTextStrike.h"
bsalomon@google.com8c2fe992011-09-13 15:27:18 +000026#include "SkTLazy.h"
bsalomon@google.comc0af3172012-06-15 14:10:09 +000027#include "SkTLS.h"
tomhudson@google.com0c8d93a2011-07-01 17:08:26 +000028#include "SkTrace.h"
bsalomon@google.com27847de2011-02-22 20:59:41 +000029
reed@google.comfa35e3d2012-06-26 20:16:17 +000030SK_DEFINE_INST_COUNT(GrContext)
31SK_DEFINE_INST_COUNT(GrDrawState)
32
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000033#define DEFER_TEXT_RENDERING 1
bsalomon@google.com27847de2011-02-22 20:59:41 +000034
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000035#define DEFER_PATHS 1
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +000036
bsalomon@google.com3c4d0322012-04-03 18:04:51 +000037#define BATCH_RECT_TO_RECT (1 && !GR_STATIC_RECT_VB)
bsalomon@google.com27847de2011-02-22 20:59:41 +000038
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +000039#define MAX_BLUR_SIGMA 4.0f
40
bsalomon@google.comd46e2422011-09-23 17:40:07 +000041// When we're using coverage AA but the blend is incompatible (given gpu
42// limitations) should we disable AA or draw wrong?
bsalomon@google.com950d7a82011-09-28 15:05:33 +000043#define DISABLE_COVERAGE_AA_FOR_BLEND 1
bsalomon@google.comd46e2422011-09-23 17:40:07 +000044
reed@google.com4b2d3f32012-05-15 18:05:50 +000045#if GR_DEBUG
46 // change this to a 1 to see notifications when partial coverage fails
47 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
48#else
49 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
50#endif
51
bsalomon@google.com07fc0d12012-06-22 15:15:59 +000052static const size_t MAX_TEXTURE_CACHE_COUNT = 256;
53static const size_t MAX_TEXTURE_CACHE_BYTES = 16 * 1024 * 1024;
bsalomon@google.com27847de2011-02-22 20:59:41 +000054
bsalomon@google.com60361492012-03-15 17:47:06 +000055static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
bsalomon@google.com27847de2011-02-22 20:59:41 +000056static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
57
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +000058// path rendering is the only thing we defer today that uses non-static indices
59static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = DEFER_PATHS ? 1 << 11 : 0;
60static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = DEFER_PATHS ? 4 : 0;
bsalomon@google.com27847de2011-02-22 20:59:41 +000061
bsalomon@google.combc4b6542011-11-19 13:56:11 +000062#define ASSERT_OWNED_RESOURCE(R) GrAssert(!(R) || (R)->getContext() == this)
63
bsalomon@google.com05ef5102011-05-02 21:14:59 +000064GrContext* GrContext::Create(GrEngine engine,
65 GrPlatform3DContext context3D) {
bsalomon@google.com27847de2011-02-22 20:59:41 +000066 GrContext* ctx = NULL;
67 GrGpu* fGpu = GrGpu::Create(engine, context3D);
68 if (NULL != fGpu) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000069 ctx = SkNEW_ARGS(GrContext, (fGpu));
bsalomon@google.com27847de2011-02-22 20:59:41 +000070 fGpu->unref();
71 }
72 return ctx;
73}
74
bsalomon@google.comc0af3172012-06-15 14:10:09 +000075namespace {
76void* CreateThreadInstanceCount() {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000077 return SkNEW_ARGS(int, (0));
bsalomon@google.comc0af3172012-06-15 14:10:09 +000078}
79void DeleteThreadInstanceCount(void* v) {
80 delete reinterpret_cast<int*>(v);
81}
82#define THREAD_INSTANCE_COUNT \
83 (*reinterpret_cast<int*>(SkTLS::Get(CreateThreadInstanceCount, \
84 DeleteThreadInstanceCount)))
85
86}
87
88int GrContext::GetThreadInstanceCount() {
89 return THREAD_INSTANCE_COUNT;
90}
91
bsalomon@google.com27847de2011-02-22 20:59:41 +000092GrContext::~GrContext() {
bsalomon@google.com8fe72472011-03-30 21:26:44 +000093 this->flush();
robertphillips@google.com5acc0e32012-05-17 12:01:02 +000094
95 // Since the gpu can hold scratch textures, give it a chance to let go
96 // of them before freeing the texture cache
97 fGpu->purgeResources();
98
bsalomon@google.com27847de2011-02-22 20:59:41 +000099 delete fTextureCache;
100 delete fFontCache;
101 delete fDrawBuffer;
102 delete fDrawBufferVBAllocPool;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000103 delete fDrawBufferIBAllocPool;
bsalomon@google.com30085192011-08-19 15:42:31 +0000104
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000105 fAARectRenderer->unref();
106
bsalomon@google.com205d4602011-04-25 12:43:45 +0000107 fGpu->unref();
bsalomon@google.com30085192011-08-19 15:42:31 +0000108 GrSafeUnref(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000109 GrSafeUnref(fSoftwarePathRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000110 fDrawState->unref();
bsalomon@google.comc0af3172012-06-15 14:10:09 +0000111
112 --THREAD_INSTANCE_COUNT;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000113}
114
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000115void GrContext::contextLost() {
junov@google.com53a55842011-06-08 22:55:10 +0000116 contextDestroyed();
117 this->setupDrawBuffer();
118}
119
120void GrContext::contextDestroyed() {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000121 // abandon first to so destructors
122 // don't try to free the resources in the API.
123 fGpu->abandonResources();
124
bsalomon@google.com30085192011-08-19 15:42:31 +0000125 // a path renderer may be holding onto resources that
126 // are now unusable
127 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000128 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com30085192011-08-19 15:42:31 +0000129
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000130 delete fDrawBuffer;
131 fDrawBuffer = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000132
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000133 delete fDrawBufferVBAllocPool;
134 fDrawBufferVBAllocPool = NULL;
bsalomon@google.com205d4602011-04-25 12:43:45 +0000135
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000136 delete fDrawBufferIBAllocPool;
137 fDrawBufferIBAllocPool = NULL;
138
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000139 fAARectRenderer->reset();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000140
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000141 fTextureCache->removeAll();
142 fFontCache->freeAll();
143 fGpu->markContextDirty();
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000144}
145
146void GrContext::resetContext() {
147 fGpu->markContextDirty();
148}
149
150void GrContext::freeGpuResources() {
151 this->flush();
robertphillips@google.comff175842012-05-14 19:31:39 +0000152
153 fGpu->purgeResources();
154
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000155 fAARectRenderer->reset();
156
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000157 fTextureCache->removeAll();
158 fFontCache->freeAll();
bsalomon@google.com30085192011-08-19 15:42:31 +0000159 // a path renderer may be holding onto resources
160 GrSafeSetNull(fPathRendererChain);
robertphillips@google.com72176b22012-05-23 13:19:12 +0000161 GrSafeSetNull(fSoftwarePathRenderer);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000162}
163
twiz@google.com05e70242012-01-27 19:12:00 +0000164size_t GrContext::getGpuTextureCacheBytes() const {
165 return fTextureCache->getCachedResourceBytes();
166}
167
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000168////////////////////////////////////////////////////////////////////////////////
169
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000170GrTexture* GrContext::TextureCacheEntry::texture() const {
171 if (NULL == fEntry) {
172 return NULL;
173 } else {
174 return (GrTexture*) fEntry->resource();
175 }
176}
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000177
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000178namespace {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000179
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000180void scale_rect(SkRect* rect, float xScale, float yScale) {
robertphillips@google.com5af56062012-04-27 15:39:52 +0000181 rect->fLeft = SkScalarMul(rect->fLeft, SkFloatToScalar(xScale));
182 rect->fTop = SkScalarMul(rect->fTop, SkFloatToScalar(yScale));
183 rect->fRight = SkScalarMul(rect->fRight, SkFloatToScalar(xScale));
184 rect->fBottom = SkScalarMul(rect->fBottom, SkFloatToScalar(yScale));
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000185}
186
bsalomon@google.comb505a122012-05-31 18:40:36 +0000187float adjust_sigma(float sigma, int *scaleFactor, int *radius) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000188 *scaleFactor = 1;
189 while (sigma > MAX_BLUR_SIGMA) {
190 *scaleFactor *= 2;
191 sigma *= 0.5f;
192 }
bsalomon@google.comb505a122012-05-31 18:40:36 +0000193 *radius = static_cast<int>(ceilf(sigma * 3.0f));
194 GrAssert(*radius <= GrConvolutionEffect::kMaxKernelRadius);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000195 return sigma;
196}
197
198void apply_morphology(GrGpu* gpu,
199 GrTexture* texture,
200 const SkRect& rect,
201 int radius,
bsalomon@google.comb505a122012-05-31 18:40:36 +0000202 GrContext::MorphologyType morphType,
203 Gr1DKernelEffect::Direction direction) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000204
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000205 GrRenderTarget* target = gpu->drawState()->getRenderTarget();
206 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000207 GrDrawState* drawState = gpu->drawState();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000208 drawState->setRenderTarget(target);
209 GrMatrix sampleM;
210 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000211 drawState->sampler(0)->reset(sampleM);
212 SkAutoTUnref<GrCustomStage> morph(
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000213 SkNEW_ARGS(GrMorphologyEffect, (texture, direction, radius, morphType)));
bsalomon@google.comb505a122012-05-31 18:40:36 +0000214 drawState->sampler(0)->setCustomStage(morph);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000215 gpu->drawSimpleRect(rect, NULL);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000216}
217
bsalomon@google.comb505a122012-05-31 18:40:36 +0000218void convolve_gaussian(GrGpu* gpu,
219 GrTexture* texture,
220 const SkRect& rect,
221 float sigma,
222 int radius,
223 Gr1DKernelEffect::Direction direction) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000224 GrRenderTarget* target = gpu->drawState()->getRenderTarget();
225 GrDrawTarget::AutoStateRestore asr(gpu, GrDrawTarget::kReset_ASRInit);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000226 GrDrawState* drawState = gpu->drawState();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000227 drawState->setRenderTarget(target);
228 GrMatrix sampleM;
229 sampleM.setIDiv(texture->width(), texture->height());
bsalomon@google.comb505a122012-05-31 18:40:36 +0000230 drawState->sampler(0)->reset(sampleM);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000231 SkAutoTUnref<GrConvolutionEffect> conv(SkNEW_ARGS(GrConvolutionEffect,
tomhudson@google.comfde2c0a2012-07-16 12:23:32 +0000232 (texture, direction, radius,
233 sigma)));
bsalomon@google.comb505a122012-05-31 18:40:36 +0000234 drawState->sampler(0)->setCustomStage(conv);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000235 gpu->drawSimpleRect(rect, NULL);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000236}
237
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000238}
239
bsalomon@google.comb8670992012-07-25 21:27:09 +0000240GrContext::TextureCacheEntry GrContext::findAndLockTexture(const GrTextureDesc& desc,
241 const GrTextureParams* params) {
242 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, false);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000243 return TextureCacheEntry(fTextureCache->findAndLock(resourceKey,
244 GrResourceCache::kNested_LockType));
245}
246
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000247bool GrContext::isTextureInCache(const GrTextureDesc& desc,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000248 const GrTextureParams* params) const {
249 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, false);
bsalomon@google.comfb309512011-11-30 14:13:48 +0000250 return fTextureCache->hasKey(resourceKey);
251}
252
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000253GrResourceEntry* GrContext::addAndLockStencilBuffer(GrStencilBuffer* sb) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000254 ASSERT_OWNED_RESOURCE(sb);
robertphillips@google.com46a86002012-08-08 10:42:44 +0000255
256 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(sb->width(),
257 sb->height(),
258 sb->numSamples());
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000259 return fTextureCache->createAndLock(resourceKey, sb);
260}
261
262GrStencilBuffer* GrContext::findStencilBuffer(int width, int height,
263 int sampleCnt) {
robertphillips@google.com46a86002012-08-08 10:42:44 +0000264 GrResourceKey resourceKey = GrStencilBuffer::ComputeKey(width,
265 height,
266 sampleCnt);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000267 GrResourceEntry* entry = fTextureCache->findAndLock(resourceKey,
268 GrResourceCache::kSingle_LockType);
269 if (NULL != entry) {
270 GrStencilBuffer* sb = (GrStencilBuffer*) entry->resource();
271 return sb;
272 } else {
273 return NULL;
274 }
275}
276
277void GrContext::unlockStencilBuffer(GrResourceEntry* sbEntry) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000278 ASSERT_OWNED_RESOURCE(sbEntry->resource());
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000279 fTextureCache->unlock(sbEntry);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000280}
281
282static void stretchImage(void* dst,
283 int dstW,
284 int dstH,
285 void* src,
286 int srcW,
287 int srcH,
288 int bpp) {
289 GrFixed dx = (srcW << 16) / dstW;
290 GrFixed dy = (srcH << 16) / dstH;
291
292 GrFixed y = dy >> 1;
293
294 int dstXLimit = dstW*bpp;
295 for (int j = 0; j < dstH; ++j) {
296 GrFixed x = dx >> 1;
297 void* srcRow = (uint8_t*)src + (y>>16)*srcW*bpp;
298 void* dstRow = (uint8_t*)dst + j*dstW*bpp;
299 for (int i = 0; i < dstXLimit; i += bpp) {
300 memcpy((uint8_t*) dstRow + i,
301 (uint8_t*) srcRow + (x>>16)*bpp,
302 bpp);
303 x += dx;
304 }
305 y += dy;
306 }
307}
308
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000309GrContext::TextureCacheEntry GrContext::createAndLockTexture(
bsalomon@google.comb8670992012-07-25 21:27:09 +0000310 const GrTextureParams* params,
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000311 const GrTextureDesc& desc,
312 void* srcData,
313 size_t rowBytes) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000314 SK_TRACE_EVENT0("GrContext::createAndLockTexture");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000315
316#if GR_DUMP_TEXTURE_UPLOAD
317 GrPrintf("GrContext::createAndLockTexture [%d %d]\n", desc.fWidth, desc.fHeight);
318#endif
319
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000320 TextureCacheEntry entry;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000321
bsalomon@google.comb8670992012-07-25 21:27:09 +0000322 GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, false);
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000323
324 if (GrTexture::NeedsResizing(resourceKey)) {
325 // The desired texture is NPOT and tiled but that isn't supported by
326 // the current hardware. Resize the texture to be a POT
bsalomon@google.comb8670992012-07-25 21:27:09 +0000327 GrAssert(NULL != params);
328 TextureCacheEntry clampEntry = this->findAndLockTexture(desc, NULL);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000329
330 if (NULL == clampEntry.texture()) {
bsalomon@google.comb8670992012-07-25 21:27:09 +0000331 clampEntry = this->createAndLockTexture(NULL, desc, srcData, rowBytes);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000332 GrAssert(NULL != clampEntry.texture());
333 if (NULL == clampEntry.texture()) {
334 return entry;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000335 }
336 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000337 GrTextureDesc rtDesc = desc;
338 rtDesc.fFlags = rtDesc.fFlags |
339 kRenderTarget_GrTextureFlagBit |
340 kNoStencil_GrTextureFlagBit;
bsalomon@google.com99621082011-11-15 16:47:16 +0000341 rtDesc.fWidth = GrNextPow2(GrMax(desc.fWidth, 64));
342 rtDesc.fHeight = GrNextPow2(GrMax(desc.fHeight, 64));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000343
344 GrTexture* texture = fGpu->createTexture(rtDesc, NULL, 0);
345
346 if (NULL != texture) {
bsalomon@google.comb8670992012-07-25 21:27:09 +0000347 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000348 GrDrawState* drawState = fGpu->drawState();
349 drawState->setRenderTarget(texture->asRenderTarget());
bsalomon@google.com82c7bd82011-11-09 15:32:29 +0000350
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000351 // if filtering is not desired then we want to ensure all
352 // texels in the resampled image are copies of texels from
353 // the original.
bsalomon@google.comb8670992012-07-25 21:27:09 +0000354 drawState->sampler(0)->reset(SkShader::kClamp_TileMode,
355 GrTexture::NeedsFiltering(resourceKey));
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000356 drawState->createTextureEffect(0, clampEntry.texture());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000357
358 static const GrVertexLayout layout =
359 GrDrawTarget::StageTexCoordVertexLayoutBit(0,0);
360 GrDrawTarget::AutoReleaseGeometry arg(fGpu, layout, 4, 0);
361
362 if (arg.succeeded()) {
363 GrPoint* verts = (GrPoint*) arg.vertices();
364 verts[0].setIRectFan(0, 0,
365 texture->width(),
366 texture->height(),
367 2*sizeof(GrPoint));
368 verts[1].setIRectFan(0, 0, 1, 1, 2*sizeof(GrPoint));
bsalomon@google.com47059542012-06-06 20:51:20 +0000369 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000370 0, 4);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000371 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000372 }
bsalomon@google.com1da07462011-03-10 14:51:57 +0000373 texture->releaseRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000374 } else {
375 // TODO: Our CPU stretch doesn't filter. But we create separate
376 // stretched textures when the sampler state is either filtered or
377 // not. Either implement filtered stretch blit on CPU or just create
378 // one when FBO case fails.
379
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000380 rtDesc.fFlags = kNone_GrTextureFlags;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000381 // no longer need to clamp at min RT size.
382 rtDesc.fWidth = GrNextPow2(desc.fWidth);
383 rtDesc.fHeight = GrNextPow2(desc.fHeight);
bsalomon@google.com64c4fe42011-11-05 14:51:01 +0000384 int bpp = GrBytesPerPixel(desc.fConfig);
bsalomon@google.com3582bf92011-06-30 21:32:31 +0000385 SkAutoSMalloc<128*128*4> stretchedPixels(bpp *
bsalomon@google.com27847de2011-02-22 20:59:41 +0000386 rtDesc.fWidth *
387 rtDesc.fHeight);
388 stretchImage(stretchedPixels.get(), rtDesc.fWidth, rtDesc.fHeight,
389 srcData, desc.fWidth, desc.fHeight, bpp);
390
391 size_t stretchedRowBytes = rtDesc.fWidth * bpp;
392
393 GrTexture* texture = fGpu->createTexture(rtDesc,
394 stretchedPixels.get(),
395 stretchedRowBytes);
396 GrAssert(NULL != texture);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000397 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000398 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000399 fTextureCache->unlock(clampEntry.cacheEntry());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000400
401 } else {
402 GrTexture* texture = fGpu->createTexture(desc, srcData, rowBytes);
403 if (NULL != texture) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000404 entry.set(fTextureCache->createAndLock(resourceKey, texture));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000405 }
406 }
407 return entry;
408}
409
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000410GrContext::TextureCacheEntry GrContext::lockScratchTexture(
411 const GrTextureDesc& inDesc,
412 ScratchTexMatch match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000413 GrTextureDesc desc = inDesc;
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000414 desc.fClientCacheID = kScratch_CacheID;
415
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000416 if (kExact_ScratchTexMatch != match) {
417 // bin by pow2 with a reasonable min
418 static const int MIN_SIZE = 256;
419 desc.fWidth = GrMax(MIN_SIZE, GrNextPow2(desc.fWidth));
420 desc.fHeight = GrMax(MIN_SIZE, GrNextPow2(desc.fHeight));
421 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000422
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000423 GrResourceEntry* entry;
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000424 int origWidth = desc.fWidth;
425 int origHeight = desc.fHeight;
426 bool doubledW = false;
427 bool doubledH = false;
428
429 do {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000430 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL, desc, true);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000431 entry = fTextureCache->findAndLock(key,
432 GrResourceCache::kNested_LockType);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000433 // if we miss, relax the fit of the flags...
434 // then try doubling width... then height.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000435 if (NULL != entry || kExact_ScratchTexMatch == match) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000436 break;
437 }
438 if (!(desc.fFlags & kRenderTarget_GrTextureFlagBit)) {
439 desc.fFlags = desc.fFlags | kRenderTarget_GrTextureFlagBit;
440 } else if (desc.fFlags & kNoStencil_GrTextureFlagBit) {
441 desc.fFlags = desc.fFlags & ~kNoStencil_GrTextureFlagBit;
442 } else if (!doubledW) {
443 desc.fFlags = inDesc.fFlags;
444 desc.fWidth *= 2;
445 doubledW = true;
446 } else if (!doubledH) {
447 desc.fFlags = inDesc.fFlags;
448 desc.fWidth = origWidth;
449 desc.fHeight *= 2;
450 doubledH = true;
451 } else {
452 break;
453 }
454
455 } while (true);
456
457 if (NULL == entry) {
458 desc.fFlags = inDesc.fFlags;
459 desc.fWidth = origWidth;
460 desc.fHeight = origHeight;
461 GrTexture* texture = fGpu->createTexture(desc, NULL, 0);
462 if (NULL != texture) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000463 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000464 texture->desc(),
465 true);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000466 entry = fTextureCache->createAndLock(key, texture);
467 }
468 }
469
470 // If the caller gives us the same desc/sampler twice we don't want
471 // to return the same texture the second time (unless it was previously
472 // released). So we detach the entry from the cache and reattach at release.
473 if (NULL != entry) {
474 fTextureCache->detach(entry);
475 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000476 return TextureCacheEntry(entry);
bsalomon@google.comb5b31682011-06-16 18:05:35 +0000477}
478
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000479void GrContext::addExistingTextureToCache(GrTexture* texture) {
480
481 if (NULL == texture) {
482 return;
483 }
484
485 GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
486 texture->desc(),
487 true);
488 fTextureCache->attach(key, texture);
489}
490
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000491void GrContext::unlockTexture(TextureCacheEntry entry) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +0000492 ASSERT_OWNED_RESOURCE(entry.texture());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000493 // If this is a scratch texture we detached it from the cache
494 // while it was locked (to avoid two callers simultaneously getting
495 // the same texture).
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000496 if (GrTexture::IsScratchTexture(entry.cacheEntry()->key())) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000497 fTextureCache->reattachAndUnlock(entry.cacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000498 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000499 fTextureCache->unlock(entry.cacheEntry());
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000500 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000501}
502
robertphillips@google.com15c0fea2012-06-22 12:41:43 +0000503void GrContext::freeEntry(TextureCacheEntry entry) {
504 ASSERT_OWNED_RESOURCE(entry.texture());
505
506 fTextureCache->freeEntry(entry.cacheEntry());
507}
508
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000509GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000510 void* srcData,
511 size_t rowBytes) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000512 GrTextureDesc descCopy = descIn;
513 descCopy.fClientCacheID = kUncached_CacheID;
514 return fGpu->createTexture(descCopy, srcData, rowBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000515}
516
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000517void GrContext::getTextureCacheLimits(int* maxTextures,
518 size_t* maxTextureBytes) const {
519 fTextureCache->getLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000520}
521
bsalomon@google.com07fc0d12012-06-22 15:15:59 +0000522void GrContext::setTextureCacheLimits(int maxTextures, size_t maxTextureBytes) {
523 fTextureCache->setLimits(maxTextures, maxTextureBytes);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000524}
525
bsalomon@google.com91958362011-06-13 17:58:13 +0000526int GrContext::getMaxTextureSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000527 return fGpu->getCaps().fMaxTextureSize;
bsalomon@google.com91958362011-06-13 17:58:13 +0000528}
529
530int GrContext::getMaxRenderTargetSize() const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000531 return fGpu->getCaps().fMaxRenderTargetSize;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000532}
533
534///////////////////////////////////////////////////////////////////////////////
535
bsalomon@google.come269f212011-11-07 13:29:52 +0000536GrTexture* GrContext::createPlatformTexture(const GrPlatformTextureDesc& desc) {
537 return fGpu->createPlatformTexture(desc);
538}
539
540GrRenderTarget* GrContext::createPlatformRenderTarget(const GrPlatformRenderTargetDesc& desc) {
541 return fGpu->createPlatformRenderTarget(desc);
542}
543
bsalomon@google.com5877ffd2011-04-11 17:58:48 +0000544///////////////////////////////////////////////////////////////////////////////
545
bsalomon@google.comb8670992012-07-25 21:27:09 +0000546bool GrContext::supportsIndex8PixelConfig(const GrTextureParams* params,
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000547 int width, int height) const {
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000548 const GrDrawTarget::Caps& caps = fGpu->getCaps();
549 if (!caps.f8BitPaletteSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000550 return false;
551 }
552
bsalomon@google.com27847de2011-02-22 20:59:41 +0000553 bool isPow2 = GrIsPow2(width) && GrIsPow2(height);
554
555 if (!isPow2) {
bsalomon@google.comb8670992012-07-25 21:27:09 +0000556 bool tiled = NULL != params && params->isTiled();
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000557 if (tiled && !caps.fNPOTTextureTileSupport) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000558 return false;
559 }
560 }
561 return true;
562}
563
564////////////////////////////////////////////////////////////////////////////////
565
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000566const GrClipData* GrContext::getClip() const {
567 return fGpu->getClip();
568}
bsalomon@google.com05ef5102011-05-02 21:14:59 +0000569
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000570void GrContext::setClip(const GrClipData* clipData) {
571 fGpu->setClip(clipData);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000572 fDrawState->enableState(GrDrawState::kClip_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000573}
574
bsalomon@google.com27847de2011-02-22 20:59:41 +0000575////////////////////////////////////////////////////////////////////////////////
576
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000577void GrContext::clear(const GrIRect* rect,
578 const GrColor color,
579 GrRenderTarget* target) {
bsalomon@google.com398109c2011-04-14 18:40:27 +0000580 this->flush();
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000581 fGpu->clear(rect, color, target);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000582}
583
584void GrContext::drawPaint(const GrPaint& paint) {
585 // set rect to be big enough to fill the space, but not super-huge, so we
586 // don't overflow fixed-point implementations
bsalomon@google.comd302f142011-03-03 13:54:13 +0000587 GrRect r;
588 r.setLTRB(0, 0,
589 GrIntToScalar(getRenderTarget()->width()),
590 GrIntToScalar(getRenderTarget()->height()));
bsalomon@google.com27847de2011-02-22 20:59:41 +0000591 GrMatrix inverse;
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000592 SkTLazy<GrPaint> tmpPaint;
593 const GrPaint* p = &paint;
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000594 AutoMatrix am;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000595
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000596 // We attempt to map r by the inverse matrix and draw that. mapRect will
597 // map the four corners and bound them with a new rect. This will not
598 // produce a correct result for some perspective matrices.
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000599 if (!this->getMatrix().hasPerspective()) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +0000600 if (!fDrawState->getViewInverse(&inverse)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000601 GrPrintf("Could not invert matrix\n");
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000602 return;
603 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000604 inverse.mapRect(&r);
605 } else {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000606 if (paint.hasTextureOrMask()) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000607 tmpPaint.set(paint);
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000608 p = tmpPaint.get();
bsalomon@google.come3d32162012-07-20 13:37:06 +0000609 if (!tmpPaint.get()->preConcatSamplerMatricesWithInverse(fDrawState->getViewMatrix())) {
610 GrPrintf("Could not invert matrix\n");
611 }
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000612 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000613 am.set(this, GrMatrix::I());
bsalomon@google.com27847de2011-02-22 20:59:41 +0000614 }
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000615 // by definition this fills the entire clip, no need for AA
616 if (paint.fAntiAlias) {
bsalomon@google.com8c2fe992011-09-13 15:27:18 +0000617 if (!tmpPaint.isValid()) {
618 tmpPaint.set(paint);
619 p = tmpPaint.get();
620 }
621 GrAssert(p == tmpPaint.get());
622 tmpPaint.get()->fAntiAlias = false;
bsalomon@google.com4f83be82011-09-12 13:52:51 +0000623 }
624 this->drawRect(*p, r);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000625}
626
bsalomon@google.com205d4602011-04-25 12:43:45 +0000627////////////////////////////////////////////////////////////////////////////////
628
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000629namespace {
630inline bool disable_coverage_aa_for_blend(GrDrawTarget* target) {
631 return DISABLE_COVERAGE_AA_FOR_BLEND && !target->canApplyCoverage();
632}
633}
634
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000635////////////////////////////////////////////////////////////////////////////////
636
bsalomon@google.com27847de2011-02-22 20:59:41 +0000637/* create a triangle strip that strokes the specified triangle. There are 8
638 unique vertices, but we repreat the last 2 to close up. Alternatively we
639 could use an indices array, and then only send 8 verts, but not sure that
640 would be faster.
641 */
bsalomon@google.com205d4602011-04-25 12:43:45 +0000642static void setStrokeRectStrip(GrPoint verts[10], GrRect rect,
bsalomon@google.com27847de2011-02-22 20:59:41 +0000643 GrScalar width) {
644 const GrScalar rad = GrScalarHalf(width);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000645 rect.sort();
bsalomon@google.com27847de2011-02-22 20:59:41 +0000646
647 verts[0].set(rect.fLeft + rad, rect.fTop + rad);
648 verts[1].set(rect.fLeft - rad, rect.fTop - rad);
649 verts[2].set(rect.fRight - rad, rect.fTop + rad);
650 verts[3].set(rect.fRight + rad, rect.fTop - rad);
651 verts[4].set(rect.fRight - rad, rect.fBottom - rad);
652 verts[5].set(rect.fRight + rad, rect.fBottom + rad);
653 verts[6].set(rect.fLeft + rad, rect.fBottom - rad);
654 verts[7].set(rect.fLeft - rad, rect.fBottom + rad);
655 verts[8] = verts[0];
656 verts[9] = verts[1];
657}
658
reed@google.com20efde72011-05-09 17:00:02 +0000659/**
660 * Returns true if the rects edges are integer-aligned.
661 */
662static bool isIRect(const GrRect& r) {
663 return GrScalarIsInt(r.fLeft) && GrScalarIsInt(r.fTop) &&
664 GrScalarIsInt(r.fRight) && GrScalarIsInt(r.fBottom);
665}
666
bsalomon@google.com205d4602011-04-25 12:43:45 +0000667static bool apply_aa_to_rect(GrDrawTarget* target,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000668 const GrRect& rect,
669 GrScalar width,
670 const GrMatrix* matrix,
671 GrMatrix* combinedMatrix,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000672 GrRect* devRect,
673 bool* useVertexCoverage) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000674 // we use a simple coverage ramp to do aa on axis-aligned rects
675 // we check if the rect will be axis-aligned, and the rect won't land on
676 // integer coords.
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000677
bsalomon@google.coma3108262011-10-10 14:08:47 +0000678 // we are keeping around the "tweak the alpha" trick because
679 // it is our only hope for the fixed-pipe implementation.
680 // In a shader implementation we can give a separate coverage input
bsalomon@google.com289533a2011-10-27 12:34:25 +0000681 // TODO: remove this ugliness when we drop the fixed-pipe impl
bsalomon@google.coma3108262011-10-10 14:08:47 +0000682 *useVertexCoverage = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +0000683 if (!target->canTweakAlphaForCoverage()) {
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000684 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +0000685#if GR_DEBUG
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000686 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +0000687#endif
bsalomon@google.coma3108262011-10-10 14:08:47 +0000688 return false;
bsalomon@google.com2eba7952012-01-12 13:47:37 +0000689 } else {
690 *useVertexCoverage = true;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000691 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000692 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000693 const GrDrawState& drawState = target->getDrawState();
694 if (drawState.getRenderTarget()->isMultisampled()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000695 return false;
696 }
697
bsalomon@google.com471d4712011-08-23 15:45:25 +0000698 if (0 == width && target->willUseHWAALines()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000699 return false;
700 }
701
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000702 if (!drawState.getViewMatrix().preservesAxisAlignment()) {
bsalomon@google.com205d4602011-04-25 12:43:45 +0000703 return false;
704 }
705
706 if (NULL != matrix &&
707 !matrix->preservesAxisAlignment()) {
708 return false;
709 }
710
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000711 *combinedMatrix = drawState.getViewMatrix();
bsalomon@google.com205d4602011-04-25 12:43:45 +0000712 if (NULL != matrix) {
713 combinedMatrix->preConcat(*matrix);
714 GrAssert(combinedMatrix->preservesAxisAlignment());
715 }
716
717 combinedMatrix->mapRect(devRect, rect);
718 devRect->sort();
719
720 if (width < 0) {
reed@google.com20efde72011-05-09 17:00:02 +0000721 return !isIRect(*devRect);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000722 } else {
723 return true;
724 }
725}
726
bsalomon@google.com27847de2011-02-22 20:59:41 +0000727void GrContext::drawRect(const GrPaint& paint,
728 const GrRect& rect,
729 GrScalar width,
730 const GrMatrix* matrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000731 SK_TRACE_EVENT0("GrContext::drawRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000732
733 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000734 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000735
bsalomon@google.com205d4602011-04-25 12:43:45 +0000736 GrRect devRect = rect;
737 GrMatrix combinedMatrix;
bsalomon@google.coma3108262011-10-10 14:08:47 +0000738 bool useVertexCoverage;
bsalomon@google.com289533a2011-10-27 12:34:25 +0000739 bool needAA = paint.fAntiAlias &&
740 !this->getRenderTarget()->isMultisampled();
741 bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix,
742 &combinedMatrix, &devRect,
743 &useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000744
745 if (doAA) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000746 GrDrawTarget::AutoDeviceCoordDraw adcd(target);
747 if (!adcd.succeeded()) {
748 return;
749 }
bsalomon@google.com205d4602011-04-25 12:43:45 +0000750 if (width >= 0) {
751 GrVec strokeSize;;
752 if (width > 0) {
753 strokeSize.set(width, width);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000754 combinedMatrix.mapVectors(&strokeSize, 1);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000755 strokeSize.setAbs(strokeSize);
756 } else {
757 strokeSize.set(GR_Scalar1, GR_Scalar1);
758 }
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000759 fAARectRenderer->strokeAARect(this->getGpu(), target, devRect,
760 strokeSize, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000761 } else {
robertphillips@google.comf6747b02012-06-12 00:32:28 +0000762 fAARectRenderer->fillAARect(this->getGpu(), target,
763 devRect, useVertexCoverage);
bsalomon@google.com205d4602011-04-25 12:43:45 +0000764 }
765 return;
766 }
767
bsalomon@google.com27847de2011-02-22 20:59:41 +0000768 if (width >= 0) {
769 // TODO: consider making static vertex buffers for these cases.
770 // Hairline could be done by just adding closing vertex to
771 // unitSquareVertexBuffer()
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000772
bsalomon@google.com27847de2011-02-22 20:59:41 +0000773 static const int worstCaseVertCount = 10;
bsalomon@google.come3d32162012-07-20 13:37:06 +0000774 GrDrawTarget::AutoReleaseGeometry geo(target, 0, worstCaseVertCount, 0);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000775
776 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000777 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000778 return;
779 }
780
781 GrPrimitiveType primType;
782 int vertCount;
783 GrPoint* vertex = geo.positions();
784
785 if (width > 0) {
786 vertCount = 10;
bsalomon@google.com47059542012-06-06 20:51:20 +0000787 primType = kTriangleStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000788 setStrokeRectStrip(vertex, rect, width);
789 } else {
790 // hairline
791 vertCount = 5;
bsalomon@google.com47059542012-06-06 20:51:20 +0000792 primType = kLineStrip_GrPrimitiveType;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000793 vertex[0].set(rect.fLeft, rect.fTop);
794 vertex[1].set(rect.fRight, rect.fTop);
795 vertex[2].set(rect.fRight, rect.fBottom);
796 vertex[3].set(rect.fLeft, rect.fBottom);
797 vertex[4].set(rect.fLeft, rect.fTop);
798 }
799
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000800 GrDrawState::AutoViewMatrixRestore avmr;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000801 if (NULL != matrix) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000802 GrDrawState* drawState = target->drawState();
803 avmr.set(drawState);
804 drawState->preConcatViewMatrix(*matrix);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000805 drawState->preConcatSamplerMatrices(*matrix);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000806 }
807
808 target->drawNonIndexed(primType, 0, vertCount);
809 } else {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000810#if GR_STATIC_RECT_VB
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000811 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
812 if (NULL == sqVB) {
813 GrPrintf("Failed to create static rect vb.\n");
814 return;
815 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000816 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000817 GrDrawState* drawState = target->drawState();
818 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000819 GrMatrix m;
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000820 m.setAll(rect.width(), 0, rect.fLeft,
bsalomon@google.com205d4602011-04-25 12:43:45 +0000821 0, rect.height(), rect.fTop,
822 0, 0, GrMatrix::I()[8]);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000823
824 if (NULL != matrix) {
825 m.postConcat(*matrix);
826 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000827 drawState->preConcatViewMatrix(m);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000828 drawState->preConcatSamplerMatrices(m);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000829
bsalomon@google.com47059542012-06-06 20:51:20 +0000830 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000831#else
bsalomon@google.come3d32162012-07-20 13:37:06 +0000832 target->drawSimpleRect(rect, matrix);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000833#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +0000834 }
835}
836
837void GrContext::drawRectToRect(const GrPaint& paint,
838 const GrRect& dstRect,
839 const GrRect& srcRect,
840 const GrMatrix* dstMatrix,
841 const GrMatrix* srcMatrix) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000842 SK_TRACE_EVENT0("GrContext::drawRectToRect");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000843
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000844 // srcRect refers to paint's first texture
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000845 if (!paint.isTextureStageEnabled(0)) {
bsalomon@google.com27847de2011-02-22 20:59:41 +0000846 drawRect(paint, dstRect, -1, dstMatrix);
847 return;
848 }
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000849
bsalomon@google.com27847de2011-02-22 20:59:41 +0000850 GR_STATIC_ASSERT(!BATCH_RECT_TO_RECT || !GR_STATIC_RECT_VB);
851
852#if GR_STATIC_RECT_VB
853 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000854 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000855 GrDrawState* drawState = target->drawState();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000856 GrDrawState::AutoViewMatrixRestore avmr(drawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000857
858 GrMatrix m;
859
860 m.setAll(dstRect.width(), 0, dstRect.fLeft,
861 0, dstRect.height(), dstRect.fTop,
862 0, 0, GrMatrix::I()[8]);
863 if (NULL != dstMatrix) {
864 m.postConcat(*dstMatrix);
865 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000866 drawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000867
bsalomon@google.come3d32162012-07-20 13:37:06 +0000868 // we explicitly setup the correct coords for the first stage. The others
869 // must know about the view matrix change.
870 for (int s = 1; s < GrPaint::kTotalStages; ++s) {
871 if (drawState->isStageEnabled(s)) {
872 drawState->sampler(s)->preConcatMatrix(m);
873 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000874 }
875
bsalomon@google.com27847de2011-02-22 20:59:41 +0000876 m.setAll(srcRect.width(), 0, srcRect.fLeft,
877 0, srcRect.height(), srcRect.fTop,
878 0, 0, GrMatrix::I()[8]);
879 if (NULL != srcMatrix) {
880 m.postConcat(*srcMatrix);
881 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000882 drawState->sampler(GrPaint::kFirstTextureStage)->preConcatMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000883
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000884 const GrVertexBuffer* sqVB = fGpu->getUnitSquareVertexBuffer();
885 if (NULL == sqVB) {
886 GrPrintf("Failed to create static rect vb.\n");
887 return;
888 }
bsalomon@google.come3d32162012-07-20 13:37:06 +0000889 target->setVertexSourceToBuffer(0, sqVB);
bsalomon@google.com47059542012-06-06 20:51:20 +0000890 target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000891#else
892
893 GrDrawTarget* target;
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000894#if BATCH_RECT_TO_RECT
bsalomon@google.com27847de2011-02-22 20:59:41 +0000895 target = this->prepareToDraw(paint, kBuffered_DrawCategory);
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +0000896#else
bsalomon@google.com27847de2011-02-22 20:59:41 +0000897 target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
898#endif
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000899 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000900
tomhudson@google.com93813632011-10-27 20:21:16 +0000901 const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
902 const GrMatrix* srcMatrices[GrDrawState::kNumStages] = {NULL};
bsalomon@google.com27847de2011-02-22 20:59:41 +0000903 srcRects[0] = &srcRect;
904 srcMatrices[0] = srcMatrix;
905
bsalomon@google.come3d32162012-07-20 13:37:06 +0000906 target->drawRect(dstRect, dstMatrix, srcRects, srcMatrices);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000907#endif
908}
909
910void GrContext::drawVertices(const GrPaint& paint,
911 GrPrimitiveType primitiveType,
912 int vertexCount,
913 const GrPoint positions[],
914 const GrPoint texCoords[],
915 const GrColor colors[],
916 const uint16_t indices[],
917 int indexCount) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +0000918 SK_TRACE_EVENT0("GrContext::drawVertices");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000919
920 GrDrawTarget::AutoReleaseGeometry geo;
921
922 GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +0000923 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000924
bsalomon@google.come3d32162012-07-20 13:37:06 +0000925 GrVertexLayout layout = 0;
926 if (NULL != texCoords) {
927 layout |= GrDrawTarget::StageTexCoordVertexLayoutBit(0, 0);
928 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000929 if (NULL != colors) {
930 layout |= GrDrawTarget::kColor_VertexLayoutBit;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000931 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000932 int vertexSize = GrDrawTarget::VertexSize(layout);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000933
934 if (sizeof(GrPoint) != vertexSize) {
935 if (!geo.set(target, layout, vertexCount, 0)) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000936 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +0000937 return;
938 }
tomhudson@google.com93813632011-10-27 20:21:16 +0000939 int texOffsets[GrDrawState::kMaxTexCoords];
bsalomon@google.com27847de2011-02-22 20:59:41 +0000940 int colorOffset;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000941 GrDrawTarget::VertexSizeAndOffsetsByIdx(layout,
942 texOffsets,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000943 &colorOffset,
bsalomon@google.coma3108262011-10-10 14:08:47 +0000944 NULL,
945 NULL);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000946 void* curVertex = geo.vertices();
947
948 for (int i = 0; i < vertexCount; ++i) {
949 *((GrPoint*)curVertex) = positions[i];
950
951 if (texOffsets[0] > 0) {
952 *(GrPoint*)((intptr_t)curVertex + texOffsets[0]) = texCoords[i];
953 }
954 if (colorOffset > 0) {
955 *(GrColor*)((intptr_t)curVertex + colorOffset) = colors[i];
956 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000957 curVertex = (void*)((intptr_t)curVertex + vertexSize);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000958 }
959 } else {
960 target->setVertexSourceToArray(layout, positions, vertexCount);
961 }
962
bsalomon@google.com91958362011-06-13 17:58:13 +0000963 // we don't currently apply offscreen AA to this path. Need improved
964 // management of GrDrawTarget's geometry to avoid copying points per-tile.
bsalomon@google.coma47a48d2011-04-26 20:22:11 +0000965
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000966 if (NULL != indices) {
bsalomon@google.com91958362011-06-13 17:58:13 +0000967 target->setIndexSourceToArray(indices, indexCount);
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000968 target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
bsalomon@google.com27847de2011-02-22 20:59:41 +0000969 } else {
bsalomon@google.com8295dc12011-05-02 12:53:34 +0000970 target->drawNonIndexed(primitiveType, 0, vertexCount);
971 }
bsalomon@google.com27847de2011-02-22 20:59:41 +0000972}
973
bsalomon@google.com06afe7b2011-04-26 15:31:40 +0000974///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com150d2842012-01-12 20:19:56 +0000975namespace {
976
bsalomon@google.com93c96602012-04-27 13:05:21 +0000977struct CircleVertex {
978 GrPoint fPos;
979 GrPoint fCenter;
980 GrScalar fOuterRadius;
981 GrScalar fInnerRadius;
982};
983
984/* Returns true if will map a circle to another circle. This can be true
985 * if the matrix only includes square-scale, rotation, translation.
986 */
987inline bool isSimilarityTransformation(const SkMatrix& matrix,
988 SkScalar tol = SK_ScalarNearlyZero) {
989 if (matrix.isIdentity() || matrix.getType() == SkMatrix::kTranslate_Mask) {
990 return true;
991 }
992 if (matrix.hasPerspective()) {
993 return false;
994 }
995
996 SkScalar mx = matrix.get(SkMatrix::kMScaleX);
997 SkScalar sx = matrix.get(SkMatrix::kMSkewX);
998 SkScalar my = matrix.get(SkMatrix::kMScaleY);
999 SkScalar sy = matrix.get(SkMatrix::kMSkewY);
1000
1001 if (mx == 0 && sx == 0 && my == 0 && sy == 0) {
1002 return false;
1003 }
1004
1005 // it has scales or skews, but it could also be rotation, check it out.
1006 SkVector vec[2];
1007 vec[0].set(mx, sx);
1008 vec[1].set(sy, my);
1009
1010 return SkScalarNearlyZero(vec[0].dot(vec[1]), SkScalarSquare(tol)) &&
1011 SkScalarNearlyEqual(vec[0].lengthSqd(), vec[1].lengthSqd(),
1012 SkScalarSquare(tol));
1013}
1014
1015}
1016
1017// TODO: strokeWidth can't be larger than zero right now.
1018// It will be fixed when drawPath() can handle strokes.
1019void GrContext::drawOval(const GrPaint& paint,
1020 const GrRect& rect,
1021 SkScalar strokeWidth) {
bsalomon@google.com0982d352012-07-31 15:33:25 +00001022 GrAssert(strokeWidth <= 0);
1023 if (!isSimilarityTransformation(this->getMatrix()) ||
bsalomon@google.com93c96602012-04-27 13:05:21 +00001024 !paint.fAntiAlias ||
1025 rect.height() != rect.width()) {
1026 SkPath path;
1027 path.addOval(rect);
1028 GrPathFill fill = (strokeWidth == 0) ?
bsalomon@google.com0982d352012-07-31 15:33:25 +00001029 kHairLine_GrPathFill : kWinding_GrPathFill;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001030 this->internalDrawPath(paint, path, fill, NULL);
1031 return;
1032 }
1033
bsalomon@google.com0982d352012-07-31 15:33:25 +00001034 DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
1035 kUnbuffered_DrawCategory;
1036 GrDrawTarget* target = this->prepareToDraw(paint, category);
1037 GrDrawState* drawState = target->drawState();
1038 GrDrawState::AutoStageDisable atr(fDrawState);
1039 const GrMatrix vm = drawState->getViewMatrix();
1040
bsalomon@google.com93c96602012-04-27 13:05:21 +00001041 const GrRenderTarget* rt = drawState->getRenderTarget();
1042 if (NULL == rt) {
1043 return;
1044 }
1045
bsalomon@google.come3d32162012-07-20 13:37:06 +00001046 GrDrawTarget::AutoDeviceCoordDraw adcd(target);
1047 if (!adcd.succeeded()) {
1048 return;
1049 }
bsalomon@google.com93c96602012-04-27 13:05:21 +00001050
bsalomon@google.come3d32162012-07-20 13:37:06 +00001051 GrVertexLayout layout = GrDrawTarget::kEdge_VertexLayoutBit;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001052 GrAssert(sizeof(CircleVertex) == GrDrawTarget::VertexSize(layout));
1053
1054 GrPoint center = GrPoint::Make(rect.centerX(), rect.centerY());
1055 GrScalar radius = SkScalarHalf(rect.width());
1056
1057 vm.mapPoints(&center, 1);
1058 radius = vm.mapRadius(radius);
1059
1060 GrScalar outerRadius = radius;
1061 GrScalar innerRadius = 0;
1062 SkScalar halfWidth = 0;
1063 if (strokeWidth == 0) {
1064 halfWidth = SkScalarHalf(SK_Scalar1);
1065
1066 outerRadius += halfWidth;
1067 innerRadius = SkMaxScalar(0, radius - halfWidth);
1068 }
1069
1070 GrDrawTarget::AutoReleaseGeometry geo(target, layout, 4, 0);
1071 if (!geo.succeeded()) {
1072 GrPrintf("Failed to get space for vertices!\n");
1073 return;
1074 }
1075
1076 CircleVertex* verts = reinterpret_cast<CircleVertex*>(geo.vertices());
1077
robertphillips@google.coma0a66c12012-06-22 13:14:29 +00001078 // The fragment shader will extend the radius out half a pixel
1079 // to antialias. Expand the drawn rect here so all the pixels
1080 // will be captured.
1081 SkScalar L = center.fX - outerRadius - SkFloatToScalar(0.5f);
1082 SkScalar R = center.fX + outerRadius + SkFloatToScalar(0.5f);
1083 SkScalar T = center.fY - outerRadius - SkFloatToScalar(0.5f);
1084 SkScalar B = center.fY + outerRadius + SkFloatToScalar(0.5f);
bsalomon@google.com93c96602012-04-27 13:05:21 +00001085
1086 verts[0].fPos = SkPoint::Make(L, T);
1087 verts[1].fPos = SkPoint::Make(R, T);
1088 verts[2].fPos = SkPoint::Make(L, B);
1089 verts[3].fPos = SkPoint::Make(R, B);
1090
1091 for (int i = 0; i < 4; ++i) {
1092 // this goes to fragment shader, it should be in y-points-up space.
1093 verts[i].fCenter = SkPoint::Make(center.fX, rt->height() - center.fY);
1094
1095 verts[i].fOuterRadius = outerRadius;
1096 verts[i].fInnerRadius = innerRadius;
1097 }
1098
1099 drawState->setVertexEdgeType(GrDrawState::kCircle_EdgeType);
bsalomon@google.com47059542012-06-06 20:51:20 +00001100 target->drawNonIndexed(kTriangleStrip_GrPrimitiveType, 0, 4);
bsalomon@google.com150d2842012-01-12 20:19:56 +00001101}
bsalomon@google.com27847de2011-02-22 20:59:41 +00001102
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001103void GrContext::drawPath(const GrPaint& paint, const SkPath& path,
reed@google.com07f3ee12011-05-16 17:21:57 +00001104 GrPathFill fill, const GrPoint* translate) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001105
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001106 if (path.isEmpty()) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001107 if (GrIsFillInverted(fill)) {
1108 this->drawPaint(paint);
1109 }
1110 return;
1111 }
1112
bsalomon@google.com93c96602012-04-27 13:05:21 +00001113 SkRect ovalRect;
1114 if (!GrIsFillInverted(fill) && path.isOval(&ovalRect)) {
1115 if (translate) {
1116 ovalRect.offset(*translate);
1117 }
bsalomon@google.com47059542012-06-06 20:51:20 +00001118 SkScalar width = (fill == kHairLine_GrPathFill) ? 0 : -SK_Scalar1;
bsalomon@google.com93c96602012-04-27 13:05:21 +00001119 this->drawOval(paint, ovalRect, width);
1120 return;
1121 }
1122
1123 internalDrawPath(paint, path, fill, translate);
1124}
1125
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001126void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path,
bsalomon@google.com93c96602012-04-27 13:05:21 +00001127 GrPathFill fill, const GrPoint* translate) {
1128
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001129 // Note that below we may sw-rasterize the path into a scratch texture.
1130 // Scratch textures can be recycled after they are returned to the texture
1131 // cache. This presents a potential hazard for buffered drawing. However,
1132 // the writePixels that uploads to the scratch will perform a flush so we're
1133 // OK.
1134 DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
1135 kUnbuffered_DrawCategory;
1136 GrDrawTarget* target = this->prepareToDraw(paint, category);
tomhudson@google.com7d6afdd2012-06-22 20:10:50 +00001137 GrDrawState::AutoStageDisable atr(fDrawState);
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001138
bsalomon@google.com289533a2011-10-27 12:34:25 +00001139 bool prAA = paint.fAntiAlias && !this->getRenderTarget()->isMultisampled();
1140
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001141 // An Assumption here is that path renderer would use some form of tweaking
1142 // the src color (either the input alpha or in the frag shader) to implement
1143 // aa. If we have some future driver-mojo path AA that can do the right
1144 // thing WRT to the blend then we'll need some query on the PR.
1145 if (disable_coverage_aa_for_blend(target)) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001146#if GR_DEBUG
bsalomon@google.com979432b2011-11-05 21:38:22 +00001147 //GrPrintf("Turning off AA to correctly apply blend.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001148#endif
bsalomon@google.com289533a2011-10-27 12:34:25 +00001149 prAA = false;
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001150 }
bsalomon@google.com289533a2011-10-27 12:34:25 +00001151
robertphillips@google.com72176b22012-05-23 13:19:12 +00001152 GrPathRenderer* pr = this->getPathRenderer(path, fill, target, prAA, true);
bsalomon@google.com30085192011-08-19 15:42:31 +00001153 if (NULL == pr) {
bsalomon@google.com1983f392011-10-10 15:17:58 +00001154#if GR_DEBUG
bsalomon@google.com30085192011-08-19 15:42:31 +00001155 GrPrintf("Unable to find path renderer compatible with path.\n");
bsalomon@google.com1983f392011-10-10 15:17:58 +00001156#endif
bsalomon@google.com30085192011-08-19 15:42:31 +00001157 return;
1158 }
1159
bsalomon@google.come3d32162012-07-20 13:37:06 +00001160 pr->drawPath(path, fill, translate, target, prAA);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001161}
bsalomon@google.com8295dc12011-05-02 12:53:34 +00001162
bsalomon@google.com27847de2011-02-22 20:59:41 +00001163////////////////////////////////////////////////////////////////////////////////
1164
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001165void GrContext::flush(int flagsBitfield) {
1166 if (kDiscard_FlushBit & flagsBitfield) {
1167 fDrawBuffer->reset();
1168 } else {
bsalomon@google.comc4364992011-11-07 15:54:49 +00001169 this->flushDrawBuffer();
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001170 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +00001171 if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001172 fGpu->forceRenderTargetFlush();
1173 }
1174}
1175
bsalomon@google.com27847de2011-02-22 20:59:41 +00001176void GrContext::flushDrawBuffer() {
junov@google.com53a55842011-06-08 22:55:10 +00001177 if (fDrawBuffer) {
robertphillips@google.com58b38182012-05-03 16:29:41 +00001178 // With addition of the AA clip path, flushing the draw buffer can
1179 // result in the generation of an AA clip mask. During this
1180 // process the SW path renderer may be invoked which recusively
1181 // calls this method (via internalWriteTexturePixels) creating
1182 // infinite recursion
1183 GrInOrderDrawBuffer* temp = fDrawBuffer;
1184 fDrawBuffer = NULL;
1185
1186 temp->flushTo(fGpu);
1187
1188 fDrawBuffer = temp;
junov@google.com53a55842011-06-08 22:55:10 +00001189 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001190}
1191
bsalomon@google.com6f379512011-11-16 20:36:03 +00001192void GrContext::internalWriteTexturePixels(GrTexture* texture,
1193 int left, int top,
1194 int width, int height,
1195 GrPixelConfig config,
1196 const void* buffer,
1197 size_t rowBytes,
1198 uint32_t flags) {
1199 SK_TRACE_EVENT0("GrContext::writeTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001200 ASSERT_OWNED_RESOURCE(texture);
1201
bsalomon@google.com6f379512011-11-16 20:36:03 +00001202 if (!(kDontFlush_PixelOpsFlag & flags)) {
1203 this->flush();
1204 }
1205 // TODO: use scratch texture to perform conversion
1206 if (GrPixelConfigIsUnpremultiplied(texture->config()) !=
1207 GrPixelConfigIsUnpremultiplied(config)) {
1208 return;
1209 }
1210
1211 fGpu->writeTexturePixels(texture, left, top, width, height,
1212 config, buffer, rowBytes);
1213}
1214
1215bool GrContext::internalReadTexturePixels(GrTexture* texture,
1216 int left, int top,
1217 int width, int height,
1218 GrPixelConfig config,
1219 void* buffer,
1220 size_t rowBytes,
1221 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001222 SK_TRACE_EVENT0("GrContext::readTexturePixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001223 ASSERT_OWNED_RESOURCE(texture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001224
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001225 // TODO: code read pixels for textures that aren't also rendertargets
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001226 GrRenderTarget* target = texture->asRenderTarget();
1227 if (NULL != target) {
bsalomon@google.com6f379512011-11-16 20:36:03 +00001228 return this->internalReadRenderTargetPixels(target,
1229 left, top, width, height,
1230 config, buffer, rowBytes,
1231 flags);
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001232 } else {
1233 return false;
1234 }
1235}
1236
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001237#include "SkConfig8888.h"
1238
1239namespace {
1240/**
1241 * Converts a GrPixelConfig to a SkCanvas::Config8888. Only byte-per-channel
1242 * formats are representable as Config8888 and so the function returns false
1243 * if the GrPixelConfig has no equivalent Config8888.
1244 */
1245bool grconfig_to_config8888(GrPixelConfig config,
1246 SkCanvas::Config8888* config8888) {
1247 switch (config) {
1248 case kRGBA_8888_PM_GrPixelConfig:
1249 *config8888 = SkCanvas::kRGBA_Premul_Config8888;
1250 return true;
1251 case kRGBA_8888_UPM_GrPixelConfig:
1252 *config8888 = SkCanvas::kRGBA_Unpremul_Config8888;
1253 return true;
1254 case kBGRA_8888_PM_GrPixelConfig:
1255 *config8888 = SkCanvas::kBGRA_Premul_Config8888;
1256 return true;
1257 case kBGRA_8888_UPM_GrPixelConfig:
1258 *config8888 = SkCanvas::kBGRA_Unpremul_Config8888;
1259 return true;
1260 default:
1261 return false;
1262 }
1263}
1264}
1265
bsalomon@google.com6f379512011-11-16 20:36:03 +00001266bool GrContext::internalReadRenderTargetPixels(GrRenderTarget* target,
1267 int left, int top,
1268 int width, int height,
1269 GrPixelConfig config,
1270 void* buffer,
1271 size_t rowBytes,
1272 uint32_t flags) {
tomhudson@google.com278cbb42011-06-30 19:37:01 +00001273 SK_TRACE_EVENT0("GrContext::readRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001274 ASSERT_OWNED_RESOURCE(target);
1275
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001276 if (NULL == target) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001277 target = fDrawState->getRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001278 if (NULL == target) {
1279 return false;
1280 }
1281 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001282
bsalomon@google.com6f379512011-11-16 20:36:03 +00001283 if (!(kDontFlush_PixelOpsFlag & flags)) {
1284 this->flush();
1285 }
bsalomon@google.comc4364992011-11-07 15:54:49 +00001286
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001287 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1288 GrPixelConfigIsUnpremultiplied(config) &&
1289 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1290 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1291 if (!grconfig_to_config8888(target->config(), &srcConfig8888) ||
1292 !grconfig_to_config8888(config, &dstConfig8888)) {
1293 return false;
1294 }
1295 // do read back using target's own config
1296 this->internalReadRenderTargetPixels(target,
1297 left, top,
1298 width, height,
1299 target->config(),
1300 buffer, rowBytes,
1301 kDontFlush_PixelOpsFlag);
1302 // sw convert the pixels to unpremul config
1303 uint32_t* pixels = reinterpret_cast<uint32_t*>(buffer);
1304 SkConvertConfig8888Pixels(pixels, rowBytes, dstConfig8888,
1305 pixels, rowBytes, srcConfig8888,
1306 width, height);
1307 return true;
1308 }
1309
bsalomon@google.comc4364992011-11-07 15:54:49 +00001310 GrTexture* src = target->asTexture();
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001311 bool swapRAndB = NULL != src &&
1312 fGpu->preferredReadPixelsConfig(config) ==
1313 GrPixelConfigSwapRAndB(config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001314
1315 bool flipY = NULL != src &&
1316 fGpu->readPixelsWillPayForYFlip(target, left, top,
1317 width, height, config,
1318 rowBytes);
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001319 bool alphaConversion = (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1320 GrPixelConfigIsUnpremultiplied(config));
bsalomon@google.comc4364992011-11-07 15:54:49 +00001321
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001322 if (NULL == src && alphaConversion) {
1323 // we should fallback to cpu conversion here. This could happen when
1324 // we were given an external render target by the client that is not
1325 // also a texture (e.g. FBO 0 in GL)
1326 return false;
1327 }
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001328 // we draw to a scratch texture if any of these conversion are applied
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001329 GrAutoScratchTexture ast;
bsalomon@google.com0a97be22011-11-08 19:20:57 +00001330 if (flipY || swapRAndB || alphaConversion) {
1331 GrAssert(NULL != src);
1332 if (swapRAndB) {
1333 config = GrPixelConfigSwapRAndB(config);
1334 GrAssert(kUnknown_GrPixelConfig != config);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001335 }
1336 // Make the scratch a render target because we don't have a robust
1337 // readTexturePixels as of yet (it calls this function).
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001338 GrTextureDesc desc;
1339 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1340 desc.fWidth = width;
1341 desc.fHeight = height;
1342 desc.fConfig = config;
bsalomon@google.comc4ff22a2011-11-10 21:56:21 +00001343
bsalomon@google.com56d11e02011-11-30 19:59:08 +00001344 // When a full readback is faster than a partial we could always make
1345 // the scratch exactly match the passed rect. However, if we see many
1346 // different size rectangles we will trash our texture cache and pay the
1347 // cost of creating and destroying many textures. So, we only request
1348 // an exact match when the caller is reading an entire RT.
1349 ScratchTexMatch match = kApprox_ScratchTexMatch;
1350 if (0 == left &&
1351 0 == top &&
1352 target->width() == width &&
1353 target->height() == height &&
1354 fGpu->fullReadPixelsIsFasterThanPartial()) {
1355 match = kExact_ScratchTexMatch;
1356 }
1357 ast.set(this, desc, match);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001358 GrTexture* texture = ast.texture();
1359 if (!texture) {
1360 return false;
1361 }
1362 target = texture->asRenderTarget();
bsalomon@google.comc4364992011-11-07 15:54:49 +00001363 GrAssert(NULL != target);
1364
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001365 GrDrawTarget::AutoStateRestore asr(fGpu,
1366 GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001367 GrDrawState* drawState = fGpu->drawState();
1368 drawState->setRenderTarget(target);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001369
bsalomon@google.comc4364992011-11-07 15:54:49 +00001370 GrMatrix matrix;
1371 if (flipY) {
1372 matrix.setTranslate(SK_Scalar1 * left,
1373 SK_Scalar1 * (top + height));
1374 matrix.set(GrMatrix::kMScaleY, -GR_Scalar1);
1375 } else {
1376 matrix.setTranslate(SK_Scalar1 *left, SK_Scalar1 *top);
1377 }
1378 matrix.postIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001379 drawState->sampler(0)->reset(matrix);
1380 drawState->sampler(0)->setRAndBSwap(swapRAndB);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001381 drawState->createTextureEffect(0, src);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001382 GrRect rect;
1383 rect.setXYWH(0, 0, SK_Scalar1 * width, SK_Scalar1 * height);
bsalomon@google.come3d32162012-07-20 13:37:06 +00001384 fGpu->drawSimpleRect(rect, NULL);
bsalomon@google.comc4364992011-11-07 15:54:49 +00001385 left = 0;
1386 top = 0;
1387 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001388 return fGpu->readPixels(target,
bsalomon@google.comc4364992011-11-07 15:54:49 +00001389 left, top, width, height,
1390 config, buffer, rowBytes, flipY);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001391}
1392
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001393void GrContext::resolveRenderTarget(GrRenderTarget* target) {
1394 GrAssert(target);
1395 ASSERT_OWNED_RESOURCE(target);
1396 // In the future we may track whether there are any pending draws to this
1397 // target. We don't today so we always perform a flush. We don't promise
1398 // this to our clients, though.
1399 this->flush();
1400 fGpu->resolveRenderTarget(target);
1401}
1402
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001403void GrContext::copyTexture(GrTexture* src, GrRenderTarget* dst) {
1404 if (NULL == src || NULL == dst) {
1405 return;
1406 }
1407 ASSERT_OWNED_RESOURCE(src);
1408
twiz@google.com1ac87ff2012-04-27 19:39:33 +00001409 // Writes pending to the source texture are not tracked, so a flush
1410 // is required to ensure that the copy captures the most recent contents
1411 // of the source texture. See similar behaviour in
1412 // GrContext::resolveRenderTarget.
1413 this->flush();
1414
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001415 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001416 GrDrawState* drawState = fGpu->drawState();
1417 drawState->setRenderTarget(dst);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001418 GrMatrix sampleM;
1419 sampleM.setIDiv(src->width(), src->height());
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001420 drawState->sampler(0)->reset(sampleM);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001421 drawState->createTextureEffect(0, src);
bsalomon@google.com5db3b6c2012-01-12 20:38:57 +00001422 SkRect rect = SkRect::MakeXYWH(0, 0,
1423 SK_Scalar1 * src->width(),
1424 SK_Scalar1 * src->height());
bsalomon@google.come3d32162012-07-20 13:37:06 +00001425 fGpu->drawSimpleRect(rect, NULL);
senorblanco@chromium.orgef843cd2011-12-02 19:11:17 +00001426}
1427
bsalomon@google.com6f379512011-11-16 20:36:03 +00001428void GrContext::internalWriteRenderTargetPixels(GrRenderTarget* target,
1429 int left, int top,
1430 int width, int height,
1431 GrPixelConfig config,
1432 const void* buffer,
1433 size_t rowBytes,
1434 uint32_t flags) {
1435 SK_TRACE_EVENT0("GrContext::writeRenderTargetPixels");
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001436 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com6f379512011-11-16 20:36:03 +00001437
1438 if (NULL == target) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001439 target = fDrawState->getRenderTarget();
bsalomon@google.com6f379512011-11-16 20:36:03 +00001440 if (NULL == target) {
1441 return;
1442 }
1443 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001444
1445 // TODO: when underlying api has a direct way to do this we should use it
1446 // (e.g. glDrawPixels on desktop GL).
1447
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001448 // If the RT is also a texture and we don't have to do PM/UPM conversion
1449 // then take the texture path, which we expect to be at least as fast or
1450 // faster since it doesn't use an intermediate texture as we do below.
1451
1452#if !GR_MAC_BUILD
1453 // At least some drivers on the Mac get confused when glTexImage2D is called
1454 // on a texture attached to an FBO. The FBO still sees the old image. TODO:
1455 // determine what OS versions and/or HW is affected.
1456 if (NULL != target->asTexture() &&
1457 GrPixelConfigIsUnpremultiplied(target->config()) ==
1458 GrPixelConfigIsUnpremultiplied(config)) {
1459
1460 this->internalWriteTexturePixels(target->asTexture(),
1461 left, top, width, height,
1462 config, buffer, rowBytes, flags);
1463 return;
1464 }
1465#endif
bsalomon@google.coma91e9232012-02-23 15:39:54 +00001466 if (!GrPixelConfigIsUnpremultiplied(target->config()) &&
1467 GrPixelConfigIsUnpremultiplied(config) &&
1468 !fGpu->canPreserveReadWriteUnpremulPixels()) {
1469 SkCanvas::Config8888 srcConfig8888, dstConfig8888;
1470 if (!grconfig_to_config8888(config, &srcConfig8888) ||
1471 !grconfig_to_config8888(target->config(), &dstConfig8888)) {
1472 return;
1473 }
1474 // allocate a tmp buffer and sw convert the pixels to premul
1475 SkAutoSTMalloc<128 * 128, uint32_t> tmpPixels(width * height);
1476 const uint32_t* src = reinterpret_cast<const uint32_t*>(buffer);
1477 SkConvertConfig8888Pixels(tmpPixels.get(), 4 * width, dstConfig8888,
1478 src, rowBytes, srcConfig8888,
1479 width, height);
1480 // upload the already premul pixels
1481 this->internalWriteRenderTargetPixels(target,
1482 left, top,
1483 width, height,
1484 target->config(),
1485 tmpPixels, 4 * width, flags);
1486 return;
1487 }
bsalomon@google.coma85449d2011-11-19 02:36:05 +00001488
1489 bool swapRAndB = fGpu->preferredReadPixelsConfig(config) ==
1490 GrPixelConfigSwapRAndB(config);
1491 if (swapRAndB) {
1492 config = GrPixelConfigSwapRAndB(config);
1493 }
1494
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001495 GrTextureDesc desc;
1496 desc.fWidth = width;
1497 desc.fHeight = height;
1498 desc.fConfig = config;
1499
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001500 GrAutoScratchTexture ast(this, desc);
1501 GrTexture* texture = ast.texture();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001502 if (NULL == texture) {
1503 return;
1504 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001505 this->internalWriteTexturePixels(texture, 0, 0, width, height,
1506 config, buffer, rowBytes, flags);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001507
bsalomon@google.com873ea0c2012-03-30 15:55:32 +00001508 GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001509 GrDrawState* drawState = fGpu->drawState();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001510
1511 GrMatrix matrix;
1512 matrix.setTranslate(GrIntToScalar(left), GrIntToScalar(top));
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +00001513 drawState->setViewMatrix(matrix);
1514 drawState->setRenderTarget(target);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001515
bsalomon@google.com5c638652011-07-18 19:31:59 +00001516 matrix.setIDiv(texture->width(), texture->height());
bsalomon@google.comb8670992012-07-25 21:27:09 +00001517 drawState->sampler(0)->reset(matrix);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001518 drawState->createTextureEffect(0, texture);
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001519 drawState->sampler(0)->setRAndBSwap(swapRAndB);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001520
tomhudson@google.comb213ed82012-06-25 15:22:12 +00001521 static const GrVertexLayout layout = 0;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001522 static const int VCOUNT = 4;
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001523 // TODO: Use GrGpu::drawRect here
bsalomon@google.com27847de2011-02-22 20:59:41 +00001524 GrDrawTarget::AutoReleaseGeometry geo(fGpu, layout, VCOUNT, 0);
1525 if (!geo.succeeded()) {
bsalomon@google.com6513cd02011-08-05 20:12:30 +00001526 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com27847de2011-02-22 20:59:41 +00001527 return;
1528 }
1529 ((GrPoint*)geo.vertices())->setIRectFan(0, 0, width, height);
bsalomon@google.com47059542012-06-06 20:51:20 +00001530 fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, VCOUNT);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001531}
1532////////////////////////////////////////////////////////////////////////////////
1533
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001534void GrContext::setPaint(const GrPaint& paint) {
tomhudson@google.comcb325ce2012-07-11 14:41:19 +00001535 GrAssert(fDrawState->stagesDisabled());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001536
1537 for (int i = 0; i < GrPaint::kMaxTextures; ++i) {
1538 int s = i + GrPaint::kFirstTextureStage;
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001539 if (paint.isTextureStageEnabled(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001540 *fDrawState->sampler(s) = paint.getTextureSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001541 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001542 }
1543
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001544 fDrawState->setFirstCoverageStage(GrPaint::kFirstMaskStage);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001545
1546 for (int i = 0; i < GrPaint::kMaxMasks; ++i) {
1547 int s = i + GrPaint::kFirstMaskStage;
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001548 if (paint.isMaskStageEnabled(i)) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001549 *fDrawState->sampler(s) = paint.getMaskSampler(i);
bsalomon@google.comf864ec42011-12-12 21:57:03 +00001550 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001551 }
bsalomon@google.com26936d02012-03-19 13:06:19 +00001552
1553 // disable all stages not accessible via the paint
1554 for (int s = GrPaint::kTotalStages; s < GrDrawState::kNumStages; ++s) {
tomhudson@google.com676e6602012-07-10 17:21:48 +00001555 fDrawState->disableStage(s);
bsalomon@google.com26936d02012-03-19 13:06:19 +00001556 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001557
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001558 fDrawState->setColor(paint.fColor);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001559
1560 if (paint.fDither) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001561 fDrawState->enableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001562 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001563 fDrawState->disableState(GrDrawState::kDither_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001564 }
1565 if (paint.fAntiAlias) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001566 fDrawState->enableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001567 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001568 fDrawState->disableState(GrDrawState::kHWAntialias_StateBit);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001569 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001570 if (paint.fColorMatrixEnabled) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001571 fDrawState->enableState(GrDrawState::kColorMatrix_StateBit);
1572 fDrawState->setColorMatrix(paint.fColorMatrix);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001573 } else {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001574 fDrawState->disableState(GrDrawState::kColorMatrix_StateBit);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +00001575 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001576 fDrawState->setBlendFunc(paint.fSrcBlendCoeff, paint.fDstBlendCoeff);
1577 fDrawState->setColorFilter(paint.fColorFilterColor, paint.fColorFilterXfermode);
1578 fDrawState->setCoverage(paint.fCoverage);
reed@google.com4b2d3f32012-05-15 18:05:50 +00001579#if GR_DEBUG_PARTIAL_COVERAGE_CHECK
bsalomon@google.come3d32162012-07-20 13:37:06 +00001580 if ((paint.hasMask() || 0xff != paint.fCoverage) &&
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001581 !fGpu->canApplyCoverage()) {
bsalomon@google.comd46e2422011-09-23 17:40:07 +00001582 GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
1583 }
bsalomon@google.com95cd7bd2012-03-28 15:35:05 +00001584#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001585}
1586
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001587GrDrawTarget* GrContext::prepareToDraw(const GrPaint& paint,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001588 DrawCategory category) {
1589 if (category != fLastDrawCategory) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001590 this->flushDrawBuffer();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001591 fLastDrawCategory = category;
1592 }
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001593 this->setPaint(paint);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001594 GrDrawTarget* target = fGpu;
1595 switch (category) {
bsalomon@google.com193395c2012-03-30 17:35:12 +00001596 case kUnbuffered_DrawCategory:
1597 target = fGpu;
1598 break;
1599 case kBuffered_DrawCategory:
1600 target = fDrawBuffer;
1601 fDrawBuffer->setClip(fGpu->getClip());
1602 break;
1603 default:
1604 GrCrash("Unexpected DrawCategory.");
1605 break;
bsalomon@google.com27847de2011-02-22 20:59:41 +00001606 }
1607 return target;
1608}
1609
robertphillips@google.com72176b22012-05-23 13:19:12 +00001610/*
1611 * This method finds a path renderer that can draw the specified path on
1612 * the provided target.
1613 * Due to its expense, the software path renderer has split out so it can
1614 * can be individually allowed/disallowed via the "allowSW" boolean.
1615 */
bsalomon@google.com8d033a12012-04-27 15:52:53 +00001616GrPathRenderer* GrContext::getPathRenderer(const SkPath& path,
bsalomon@google.com289533a2011-10-27 12:34:25 +00001617 GrPathFill fill,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001618 const GrDrawTarget* target,
robertphillips@google.com72176b22012-05-23 13:19:12 +00001619 bool antiAlias,
1620 bool allowSW) {
bsalomon@google.com30085192011-08-19 15:42:31 +00001621 if (NULL == fPathRendererChain) {
1622 fPathRendererChain =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001623 SkNEW_ARGS(GrPathRendererChain,
1624 (this, GrPathRendererChain::kNone_UsageFlag));
bsalomon@google.com30085192011-08-19 15:42:31 +00001625 }
robertphillips@google.com72176b22012-05-23 13:19:12 +00001626
1627 GrPathRenderer* pr = fPathRendererChain->getPathRenderer(path, fill,
1628 target,
1629 antiAlias);
1630
1631 if (NULL == pr && allowSW) {
1632 if (NULL == fSoftwarePathRenderer) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001633 fSoftwarePathRenderer = SkNEW_ARGS(GrSoftwarePathRenderer, (this));
robertphillips@google.com72176b22012-05-23 13:19:12 +00001634 }
1635
1636 pr = fSoftwarePathRenderer;
1637 }
1638
1639 return pr;
bsalomon@google.com30085192011-08-19 15:42:31 +00001640}
1641
bsalomon@google.com27847de2011-02-22 20:59:41 +00001642////////////////////////////////////////////////////////////////////////////////
1643
bsalomon@google.com27847de2011-02-22 20:59:41 +00001644void GrContext::setRenderTarget(GrRenderTarget* target) {
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001645 ASSERT_OWNED_RESOURCE(target);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001646 if (fDrawState->getRenderTarget() != target) {
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001647 this->flush(false);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001648 fDrawState->setRenderTarget(target);
bsalomon@google.comfb4ce6f2012-03-14 13:27:54 +00001649 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001650}
1651
1652GrRenderTarget* GrContext::getRenderTarget() {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001653 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001654}
1655
1656const GrRenderTarget* GrContext::getRenderTarget() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001657 return fDrawState->getRenderTarget();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001658}
1659
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001660bool GrContext::isConfigRenderable(GrPixelConfig config) const {
1661 return fGpu->isConfigRenderable(config);
1662}
1663
bsalomon@google.com27847de2011-02-22 20:59:41 +00001664const GrMatrix& GrContext::getMatrix() const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001665 return fDrawState->getViewMatrix();
bsalomon@google.com27847de2011-02-22 20:59:41 +00001666}
1667
1668void GrContext::setMatrix(const GrMatrix& m) {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001669 fDrawState->setViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001670}
1671
1672void GrContext::concatMatrix(const GrMatrix& m) const {
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001673 fDrawState->preConcatViewMatrix(m);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001674}
1675
1676static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
1677 intptr_t mask = 1 << shift;
1678 if (pred) {
1679 bits |= mask;
1680 } else {
1681 bits &= ~mask;
1682 }
1683 return bits;
1684}
1685
bsalomon@google.com583a1e32011-08-17 13:42:46 +00001686GrContext::GrContext(GrGpu* gpu) {
bsalomon@google.comc0af3172012-06-15 14:10:09 +00001687 ++THREAD_INSTANCE_COUNT;
1688
bsalomon@google.com27847de2011-02-22 20:59:41 +00001689 fGpu = gpu;
1690 fGpu->ref();
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001691 fGpu->setContext(this);
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001692
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001693 fDrawState = SkNEW(GrDrawState);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001694 fGpu->setDrawState(fDrawState);
1695
bsalomon@google.com30085192011-08-19 15:42:31 +00001696 fPathRendererChain = NULL;
robertphillips@google.com72176b22012-05-23 13:19:12 +00001697 fSoftwarePathRenderer = NULL;
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001698
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001699 fTextureCache = SkNEW_ARGS(GrResourceCache,
1700 (MAX_TEXTURE_CACHE_COUNT,
1701 MAX_TEXTURE_CACHE_BYTES));
1702 fFontCache = SkNEW_ARGS(GrFontCache, (fGpu));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001703
1704 fLastDrawCategory = kUnbuffered_DrawCategory;
1705
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001706 fDrawBuffer = NULL;
1707 fDrawBufferVBAllocPool = NULL;
1708 fDrawBufferIBAllocPool = NULL;
1709
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001710 fAARectRenderer = SkNEW(GrAARectRenderer);
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001711
bsalomon@google.com8fe72472011-03-30 21:26:44 +00001712 this->setupDrawBuffer();
1713}
1714
1715void GrContext::setupDrawBuffer() {
1716
1717 GrAssert(NULL == fDrawBuffer);
1718 GrAssert(NULL == fDrawBufferVBAllocPool);
1719 GrAssert(NULL == fDrawBufferIBAllocPool);
1720
bsalomon@google.com92edd312012-04-04 21:40:21 +00001721#if DEFER_TEXT_RENDERING || BATCH_RECT_TO_RECT || DEFER_PATHS
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001722 fDrawBufferVBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001723 SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
bsalomon@google.com27847de2011-02-22 20:59:41 +00001724 DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001725 DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS));
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001726 fDrawBufferIBAllocPool =
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001727 SkNEW_ARGS(GrIndexBufferAllocPool, (fGpu, false,
bsalomon@google.comde6ac2d2011-02-25 21:50:42 +00001728 DRAW_BUFFER_IBPOOL_BUFFER_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001729 DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS));
bsalomon@google.com27847de2011-02-22 20:59:41 +00001730
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001731 fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
bsalomon@google.com471d4712011-08-23 15:45:25 +00001732 fDrawBufferVBAllocPool,
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001733 fDrawBufferIBAllocPool));
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001734#endif
1735
1736#if BATCH_RECT_TO_RECT
bsalomon@google.com27847de2011-02-22 20:59:41 +00001737 fDrawBuffer->setQuadIndexBuffer(this->getQuadIndexBuffer());
bsalomon@google.com3c4d0322012-04-03 18:04:51 +00001738#endif
bsalomon@google.com1015e032012-06-25 18:41:04 +00001739 if (fDrawBuffer) {
1740 fDrawBuffer->setAutoFlushTarget(fGpu);
1741 fDrawBuffer->setDrawState(fDrawState);
1742 }
bsalomon@google.com27847de2011-02-22 20:59:41 +00001743}
1744
bsalomon@google.com27847de2011-02-22 20:59:41 +00001745GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) {
bsalomon@google.com27847de2011-02-22 20:59:41 +00001746#if DEFER_TEXT_RENDERING
bsalomon@google.com193395c2012-03-30 17:35:12 +00001747 return prepareToDraw(paint, kBuffered_DrawCategory);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001748#else
bsalomon@google.com10e04bf2012-03-30 14:35:04 +00001749 return prepareToDraw(paint, kUnbuffered_DrawCategory);
bsalomon@google.com27847de2011-02-22 20:59:41 +00001750#endif
bsalomon@google.com27847de2011-02-22 20:59:41 +00001751}
1752
1753const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {
1754 return fGpu->getQuadIndexBuffer();
1755}
bsalomon@google.comdfe75bc2011-03-25 12:31:16 +00001756
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001757GrTexture* GrContext::gaussianBlur(GrTexture* srcTexture,
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001758 bool canClobberSrc,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001759 const SkRect& rect,
1760 float sigmaX, float sigmaY) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001761 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001762 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001763 AutoMatrix avm(this, GrMatrix::I());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001764 SkIRect clearRect;
bsalomon@google.comb505a122012-05-31 18:40:36 +00001765 int scaleFactorX, radiusX;
1766 int scaleFactorY, radiusY;
1767 sigmaX = adjust_sigma(sigmaX, &scaleFactorX, &radiusX);
1768 sigmaY = adjust_sigma(sigmaY, &scaleFactorY, &radiusY);
bsalomon@google.combc4b6542011-11-19 13:56:11 +00001769
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001770 SkRect srcRect(rect);
1771 scale_rect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
1772 srcRect.roundOut();
robertphillips@google.com8637a362012-04-10 18:32:35 +00001773 scale_rect(&srcRect, static_cast<float>(scaleFactorX),
1774 static_cast<float>(scaleFactorY));
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001775
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001776 AutoClip acs(this, srcRect);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001777
robertphillips@google.com99a5ac02012-04-10 19:26:38 +00001778 GrAssert(kBGRA_8888_PM_GrPixelConfig == srcTexture->config() ||
1779 kRGBA_8888_PM_GrPixelConfig == srcTexture->config() ||
1780 kAlpha_8_GrPixelConfig == srcTexture->config());
1781
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001782 GrTextureDesc desc;
1783 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1784 desc.fWidth = SkScalarFloorToInt(srcRect.width());
1785 desc.fHeight = SkScalarFloorToInt(srcRect.height());
1786 desc.fConfig = srcTexture->config();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001787
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001788 GrAutoScratchTexture temp1, temp2;
1789 GrTexture* dstTexture = temp1.set(this, desc);
1790 GrTexture* tempTexture = canClobberSrc ? srcTexture : temp2.set(this, desc);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001791
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001792 GrPaint paint;
1793 paint.reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +00001794 paint.textureSampler(0)->textureParams()->setBilerp(true);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001795
1796 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
1797 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1798 srcTexture->height());
1799 this->setRenderTarget(dstTexture->asRenderTarget());
1800 SkRect dstRect(srcRect);
1801 scale_rect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
1802 i < scaleFactorY ? 0.5f : 1.0f);
tomhudson@google.comaa72eab2012-07-19 18:01:07 +00001803 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
1804 (srcTexture)))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001805 this->drawRectToRect(paint, dstRect, srcRect);
1806 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001807 srcTexture = dstTexture;
1808 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001809 }
1810
robertphillips@google.com7a396332012-05-10 15:11:27 +00001811 SkIRect srcIRect;
1812 srcRect.roundOut(&srcIRect);
1813
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001814 if (sigmaX > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001815 if (scaleFactorX > 1) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001816 // Clear out a radius to the right of the srcRect to prevent the
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001817 // X convolution from reading garbage.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001818 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001819 radiusX, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001820 this->clear(&clearRect, 0x0);
1821 }
1822
1823 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001824 convolve_gaussian(fGpu, srcTexture, srcRect, sigmaX, radiusX,
1825 Gr1DKernelEffect::kX_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001826 srcTexture = dstTexture;
1827 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001828 }
1829
1830 if (sigmaY > 0.0f) {
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001831 if (scaleFactorY > 1 || sigmaX > 0.0f) {
bsalomon@google.comb505a122012-05-31 18:40:36 +00001832 // Clear out a radius below the srcRect to prevent the Y
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001833 // convolution from reading garbage.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001834 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001835 srcIRect.width(), radiusY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001836 this->clear(&clearRect, 0x0);
1837 }
1838
1839 this->setRenderTarget(dstTexture->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001840 convolve_gaussian(fGpu, srcTexture, srcRect, sigmaY, radiusY,
1841 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001842 srcTexture = dstTexture;
1843 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001844 }
1845
1846 if (scaleFactorX > 1 || scaleFactorY > 1) {
1847 // Clear one pixel to the right and below, to accommodate bilinear
1848 // upsampling.
robertphillips@google.com7a396332012-05-10 15:11:27 +00001849 clearRect = SkIRect::MakeXYWH(srcIRect.fLeft, srcIRect.fBottom,
1850 srcIRect.width() + 1, 1);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001851 this->clear(&clearRect, 0x0);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001852 clearRect = SkIRect::MakeXYWH(srcIRect.fRight, srcIRect.fTop,
1853 1, srcIRect.height());
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001854 this->clear(&clearRect, 0x0);
1855 // FIXME: This should be mitchell, not bilinear.
bsalomon@google.comb8670992012-07-25 21:27:09 +00001856 paint.textureSampler(0)->textureParams()->setBilerp(true);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001857 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
1858 srcTexture->height());
1859 this->setRenderTarget(dstTexture->asRenderTarget());
tomhudson@google.comaa72eab2012-07-19 18:01:07 +00001860 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect,
1861 (srcTexture)))->unref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001862 SkRect dstRect(srcRect);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001863 scale_rect(&dstRect, (float) scaleFactorX, (float) scaleFactorY);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001864 this->drawRectToRect(paint, dstRect, srcRect);
1865 srcRect = dstRect;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001866 srcTexture = dstTexture;
1867 SkTSwap(dstTexture, tempTexture);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001868 }
1869 this->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001870 if (srcTexture == temp1.texture()) {
1871 return temp1.detach();
1872 } else if (srcTexture == temp2.texture()) {
1873 return temp2.detach();
1874 } else {
1875 srcTexture->ref();
1876 return srcTexture;
1877 }
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001878}
bsalomon@google.com1e266f82011-12-12 16:11:33 +00001879
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001880GrTexture* GrContext::applyMorphology(GrTexture* srcTexture,
1881 const GrRect& rect,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001882 MorphologyType morphType,
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001883 SkISize radius) {
senorblanco@chromium.orgceb44142012-03-05 20:53:36 +00001884 ASSERT_OWNED_RESOURCE(srcTexture);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001885 srcTexture->ref();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001886 GrRenderTarget* oldRenderTarget = this->getRenderTarget();
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +00001887
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001888 AutoMatrix avm(this, GrMatrix::I());
1889
1890 AutoClip acs(this, GrRect::MakeWH(SkIntToScalar(srcTexture->width()),
robertphillips@google.combeb1af72012-07-26 18:52:16 +00001891 SkIntToScalar(srcTexture->height())));
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001892 GrTextureDesc desc;
1893 desc.fFlags = kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit;
1894 desc.fWidth = SkScalarCeilToInt(rect.width());
1895 desc.fHeight = SkScalarCeilToInt(rect.height());
1896 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001897 if (radius.fWidth > 0) {
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001898 GrAutoScratchTexture ast(this, desc);
1899 this->setRenderTarget(ast.texture()->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001900 apply_morphology(fGpu, srcTexture, rect, radius.fWidth, morphType,
1901 Gr1DKernelEffect::kX_Direction);
robertphillips@google.com7a396332012-05-10 15:11:27 +00001902 SkIRect clearRect = SkIRect::MakeXYWH(
1903 SkScalarFloorToInt(rect.fLeft),
1904 SkScalarFloorToInt(rect.fBottom),
1905 SkScalarFloorToInt(rect.width()),
1906 radius.fHeight);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001907 this->clear(&clearRect, 0x0);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001908 srcTexture->unref();
1909 srcTexture = ast.detach();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001910 }
1911 if (radius.fHeight > 0) {
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001912 GrAutoScratchTexture ast(this, desc);
1913 this->setRenderTarget(ast.texture()->asRenderTarget());
bsalomon@google.comb505a122012-05-31 18:40:36 +00001914 apply_morphology(fGpu, srcTexture, rect, radius.fHeight, morphType,
1915 Gr1DKernelEffect::kY_Direction);
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001916 srcTexture->unref();
1917 srcTexture = ast.detach();
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001918 }
1919 this->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +00001920 return srcTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001921}
bsalomon@google.comc4364992011-11-07 15:54:49 +00001922
1923///////////////////////////////////////////////////////////////////////////////