blob: deb817cd373a9517b1b23cbe696984a00e938467 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +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.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#include "GrContext.h"
12#include "GrTextContext.h"
13
reed@google.comac10a2d2010-12-22 21:39:39 +000014#include "SkGpuDevice.h"
15#include "SkGrTexturePixelRef.h"
16
Scroggo97c88c22011-05-11 14:05:25 +000017#include "SkColorFilter.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000018#include "SkDrawProcs.h"
19#include "SkGlyphCache.h"
reed@google.comc9aa5872011-04-05 21:05:37 +000020#include "SkUtils.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000021
22#define CACHE_LAYER_TEXTURES 1
23
24#if 0
25 extern bool (*gShouldDrawProc)();
26 #define CHECK_SHOULD_DRAW(draw) \
27 do { \
28 if (gShouldDrawProc && !gShouldDrawProc()) return; \
29 this->prepareRenderTarget(draw); \
30 } while (0)
31#else
32 #define CHECK_SHOULD_DRAW(draw) this->prepareRenderTarget(draw)
33#endif
34
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000035// we use the same texture slot on GrPaint for bitmaps and shaders
36// (since drawBitmap, drawSprite, and drawDevice ignore skia's shader)
37enum {
38 kBitmapTextureIdx = 0,
39 kShaderTextureIdx = 0
40};
41
reed@google.comcde92112011-07-06 20:00:52 +000042
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +000043#define MAX_BLUR_SIGMA 4.0f
44// FIXME: This value comes from from SkBlurMaskFilter.cpp.
45// Should probably be put in a common header someplace.
46#define MAX_BLUR_RADIUS SkIntToScalar(128)
47// This constant approximates the scaling done in the software path's
48// "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
49// IMHO, it actually should be 1: we blur "less" than we should do
50// according to the CSS and canvas specs, simply because Safari does the same.
51// Firefox used to do the same too, until 4.0 where they fixed it. So at some
52// point we should probably get rid of these scaling constants and rebaseline
53// all the blur tests.
54#define BLUR_SIGMA_SCALE 0.6f
reed@google.comac10a2d2010-12-22 21:39:39 +000055///////////////////////////////////////////////////////////////////////////////
56
57SkGpuDevice::SkAutoCachedTexture::
58 SkAutoCachedTexture(SkGpuDevice* device,
59 const SkBitmap& bitmap,
60 const GrSamplerState& sampler,
61 GrTexture** texture) {
62 GrAssert(texture);
reed@google.comac10a2d2010-12-22 21:39:39 +000063 *texture = this->set(device, bitmap, sampler);
64}
65
66SkGpuDevice::SkAutoCachedTexture::SkAutoCachedTexture() {
reed@google.comac10a2d2010-12-22 21:39:39 +000067}
68
69GrTexture* SkGpuDevice::SkAutoCachedTexture::set(SkGpuDevice* device,
70 const SkBitmap& bitmap,
71 const GrSamplerState& sampler) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000072 if (fTex.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +000073 fDevice->unlockCachedTexture(fTex);
74 }
75 fDevice = device;
76 GrTexture* texture = (GrTexture*)bitmap.getTexture();
77 if (texture) {
78 // return the native texture
bsalomon@google.com50398bf2011-07-26 20:45:30 +000079 fTex.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +000080 } else {
81 // look it up in our cache
bsalomon@google.com50398bf2011-07-26 20:45:30 +000082 fTex = device->lockCachedTexture(bitmap, sampler);
83 texture = fTex.texture();
reed@google.comac10a2d2010-12-22 21:39:39 +000084 }
85 return texture;
86}
87
88SkGpuDevice::SkAutoCachedTexture::~SkAutoCachedTexture() {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000089 if (fTex.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +000090 fDevice->unlockCachedTexture(fTex);
91 }
92}
93
94///////////////////////////////////////////////////////////////////////////////
95
96bool gDoTraceDraw;
97
98struct GrSkDrawProcs : public SkDrawProcs {
99public:
100 GrContext* fContext;
101 GrTextContext* fTextContext;
102 GrFontScaler* fFontScaler; // cached in the skia glyphcache
103};
104
105///////////////////////////////////////////////////////////////////////////////
106
bsalomon@google.com2e7b43d2011-01-18 20:57:22 +0000107GrRenderTarget* SkGpuDevice::Current3DApiRenderTarget() {
108 return (GrRenderTarget*) -1;
109}
110
reed@google.comaf951c92011-06-16 19:10:39 +0000111static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
112 switch (config) {
113 case kAlpha_8_GrPixelConfig:
114 *isOpaque = false;
115 return SkBitmap::kA8_Config;
116 case kRGB_565_GrPixelConfig:
117 *isOpaque = true;
118 return SkBitmap::kRGB_565_Config;
119 case kRGBA_4444_GrPixelConfig:
120 *isOpaque = false;
121 return SkBitmap::kARGB_4444_Config;
122 case kRGBA_8888_GrPixelConfig:
123 case kRGBX_8888_GrPixelConfig:
124 *isOpaque = (kRGBX_8888_GrPixelConfig == config);
125 return SkBitmap::kARGB_8888_Config;
126 default:
127 *isOpaque = false;
128 return SkBitmap::kNo_Config;
129 }
130}
reed@google.comac10a2d2010-12-22 21:39:39 +0000131
reed@google.comaf951c92011-06-16 19:10:39 +0000132static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000133 SkAutoTUnref<GrRenderTarget> rtunref;
reed@google.comaf951c92011-06-16 19:10:39 +0000134 if (SkGpuDevice::Current3DApiRenderTarget() == renderTarget) {
135 renderTarget = context->createRenderTargetFrom3DApiState();
bsalomon@google.coma44f7002011-08-09 15:30:41 +0000136 rtunref.reset(renderTarget);
reed@google.comaf951c92011-06-16 19:10:39 +0000137 }
138 GrTexture* texture = renderTarget->asTexture();
139 GrPixelConfig config = texture ? texture->config() : kRGBA_8888_GrPixelConfig;
140
141 bool isOpaque;
142 SkBitmap bitmap;
143 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
144 renderTarget->width(), renderTarget->height());
145 bitmap.setIsOpaque(isOpaque);
146 return bitmap;
147}
148
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000149SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
150: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
151 this->initFromRenderTarget(context, texture->asRenderTarget());
152}
153
reed@google.comaf951c92011-06-16 19:10:39 +0000154SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
155: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000156 this->initFromRenderTarget(context, renderTarget);
157}
158
159void SkGpuDevice::initFromRenderTarget(GrContext* context,
160 GrRenderTarget* renderTarget) {
reed@google.comaf951c92011-06-16 19:10:39 +0000161 fNeedPrepareRenderTarget = false;
162 fDrawProcs = NULL;
163
164 fContext = context;
165 fContext->ref();
166
reed@google.comaf951c92011-06-16 19:10:39 +0000167 fTexture = NULL;
168 fRenderTarget = NULL;
169 fNeedClear = false;
170
171 if (Current3DApiRenderTarget() == renderTarget) {
172 fRenderTarget = fContext->createRenderTargetFrom3DApiState();
173 } else {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000174 GrAssert(NULL != renderTarget);
reed@google.comaf951c92011-06-16 19:10:39 +0000175 fRenderTarget = renderTarget;
176 fRenderTarget->ref();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000177 // if this RT is also a texture, hold a ref on it
178 fTexture = fRenderTarget->asTexture();
179 SkSafeRef(fTexture);
reed@google.comaf951c92011-06-16 19:10:39 +0000180 }
181
182 SkGrRenderTargetPixelRef* pr = new SkGrRenderTargetPixelRef(fRenderTarget);
183 this->setPixelRef(pr, 0)->unref();
184}
185
186SkGpuDevice::SkGpuDevice(GrContext* context, SkBitmap::Config config, int width,
bsalomon@google.come97f0852011-06-17 13:10:25 +0000187 int height, Usage usage)
reed@google.comaf951c92011-06-16 19:10:39 +0000188: SkDevice(config, width, height, false /*isOpaque*/) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000189 fNeedPrepareRenderTarget = false;
190 fDrawProcs = NULL;
191
reed@google.com7b201d22011-01-11 18:59:23 +0000192 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000193 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000194
reed@google.comac10a2d2010-12-22 21:39:39 +0000195 fTexture = NULL;
196 fRenderTarget = NULL;
197 fNeedClear = false;
198
reed@google.comaf951c92011-06-16 19:10:39 +0000199 if (config != SkBitmap::kRGB_565_Config) {
200 config = SkBitmap::kARGB_8888_Config;
201 }
202 SkBitmap bm;
203 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000204
205#if CACHE_LAYER_TEXTURES
bsalomon@google.come97f0852011-06-17 13:10:25 +0000206 TexType type = (kSaveLayer_Usage == usage) ?
207 kSaveLayerDeviceRenderTarget_TexType :
208 kDeviceRenderTarget_TexType;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000209 fCache = this->lockCachedTexture(bm, GrSamplerState::ClampNoFilter(), type);
210 fTexture = fCache.texture();
211 if (fTexture) {
reed@google.comaf951c92011-06-16 19:10:39 +0000212 SkASSERT(NULL != fTexture->asRenderTarget());
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000213 // hold a ref directly on fTexture (even though fCache has one) to match
214 // other constructor paths. Simplifies cleanup.
215 fTexture->ref();
reed@google.comaf951c92011-06-16 19:10:39 +0000216 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000217#else
reed@google.comaf951c92011-06-16 19:10:39 +0000218 const GrTextureDesc desc = {
219 kRenderTarget_GrTextureFlagBit,
220 kNone_GrAALevel,
221 width,
222 height,
223 SkGr::Bitmap2PixelConfig(bm)
224 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000225
reed@google.comaf951c92011-06-16 19:10:39 +0000226 fTexture = fContext->createUncachedTexture(desc, NULL, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000227#endif
reed@google.comaf951c92011-06-16 19:10:39 +0000228 if (NULL != fTexture) {
229 fRenderTarget = fTexture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000230 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000231
reed@google.comaf951c92011-06-16 19:10:39 +0000232 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000233
reed@google.comaf951c92011-06-16 19:10:39 +0000234 // we defer the actual clear until our gainFocus()
235 fNeedClear = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000236
reed@google.comaf951c92011-06-16 19:10:39 +0000237 // wrap the bitmap with a pixelref to expose our texture
238 SkGrTexturePixelRef* pr = new SkGrTexturePixelRef(fTexture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000239 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000240 } else {
241 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
242 width, height);
243 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000244 }
245}
246
247SkGpuDevice::~SkGpuDevice() {
248 if (fDrawProcs) {
249 delete fDrawProcs;
250 }
251
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000252 SkSafeUnref(fTexture);
253 SkSafeUnref(fRenderTarget);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000254 if (fCache.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000255 GrAssert(NULL != fTexture);
256 GrAssert(fRenderTarget == fTexture->asRenderTarget());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000257 fContext->unlockTexture(fCache);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000258 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000259 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000260}
261
reed@google.comac10a2d2010-12-22 21:39:39 +0000262///////////////////////////////////////////////////////////////////////////////
263
264void SkGpuDevice::makeRenderTargetCurrent() {
265 fContext->setRenderTarget(fRenderTarget);
266 fContext->flush(true);
267 fNeedPrepareRenderTarget = true;
268}
269
270///////////////////////////////////////////////////////////////////////////////
271
272bool SkGpuDevice::readPixels(const SkIRect& srcRect, SkBitmap* bitmap) {
273 SkIRect bounds;
274 bounds.set(0, 0, this->width(), this->height());
275 if (!bounds.intersect(srcRect)) {
276 return false;
277 }
278
279 const int w = bounds.width();
280 const int h = bounds.height();
281 SkBitmap tmp;
282 // note we explicitly specify our rowBytes to be snug (no gap between rows)
283 tmp.setConfig(SkBitmap::kARGB_8888_Config, w, h, w * 4);
284 if (!tmp.allocPixels()) {
285 return false;
286 }
287
Scroggo813c33c2011-04-07 20:56:21 +0000288 tmp.lockPixels();
reed@google.comac10a2d2010-12-22 21:39:39 +0000289
Scroggoeb176032011-04-07 21:11:49 +0000290 bool read = fContext->readRenderTargetPixels(fRenderTarget,
291 bounds.fLeft, bounds.fTop,
292 bounds.width(), bounds.height(),
293 kRGBA_8888_GrPixelConfig,
294 tmp.getPixels());
Scroggo813c33c2011-04-07 20:56:21 +0000295 tmp.unlockPixels();
296 if (!read) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000297 return false;
298 }
299
300 tmp.swap(*bitmap);
301 return true;
302}
303
304void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y) {
305 SkAutoLockPixels alp(bitmap);
306 if (!bitmap.readyToDraw()) {
307 return;
308 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000309 GrPixelConfig config = SkGr::BitmapConfig2PixelConfig(bitmap.config(),
310 bitmap.isOpaque());
reed@google.comac10a2d2010-12-22 21:39:39 +0000311 fContext->setRenderTarget(fRenderTarget);
312 // we aren't setting the clip or matrix, so mark as dirty
313 // we don't need to set them for this call and don't have them anyway
314 fNeedPrepareRenderTarget = true;
315
316 fContext->writePixels(x, y, bitmap.width(), bitmap.height(),
317 config, bitmap.getPixels(), bitmap.rowBytes());
318}
319
320///////////////////////////////////////////////////////////////////////////////
321
322static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000323 const SkClipStack& clipStack,
reed@google.com6f8f2922011-03-04 22:27:10 +0000324 const SkRegion& clipRegion,
325 const SkIPoint& origin) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000326 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000327
328 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000329 iter.reset(clipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000330 const SkIRect& skBounds = clipRegion.getBounds();
331 GrRect bounds;
332 bounds.setLTRB(GrIntToScalar(skBounds.fLeft),
333 GrIntToScalar(skBounds.fTop),
334 GrIntToScalar(skBounds.fRight),
335 GrIntToScalar(skBounds.fBottom));
reed@google.com6f8f2922011-03-04 22:27:10 +0000336 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
337 &bounds);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000338 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000339}
340
341// call this ever each draw call, to ensure that the context reflects our state,
342// and not the state from some other canvas/device
343void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
344 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000345 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000346
347 fContext->setRenderTarget(fRenderTarget);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000348 SkASSERT(draw.fClipStack);
349 convert_matrixclip(fContext, *draw.fMatrix,
reed@google.com6f8f2922011-03-04 22:27:10 +0000350 *draw.fClipStack, *draw.fClip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000351 fNeedPrepareRenderTarget = false;
352 }
353}
354
reed@google.com46799cd2011-02-22 20:56:26 +0000355void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
356 const SkClipStack& clipStack) {
357 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000358 // We don't need to set them now because the context may not reflect this device.
359 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000360}
361
362void SkGpuDevice::gainFocus(SkCanvas* canvas, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000363 const SkRegion& clip, const SkClipStack& clipStack) {
364
reed@google.comac10a2d2010-12-22 21:39:39 +0000365 fContext->setRenderTarget(fRenderTarget);
366
bsalomon@google.comd302f142011-03-03 13:54:13 +0000367 this->INHERITED::gainFocus(canvas, matrix, clip, clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000368
reed@google.com6f8f2922011-03-04 22:27:10 +0000369 convert_matrixclip(fContext, matrix, clipStack, clip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000370
371 if (fNeedClear) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000372 fContext->clear(NULL, 0x0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000373 fNeedClear = false;
374 }
375}
376
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000377bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000378 if (NULL != fTexture) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000379 paint->setTexture(kBitmapTextureIdx, fTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000380 return true;
381 }
382 return false;
383}
384
385///////////////////////////////////////////////////////////////////////////////
386
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000387SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
388SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
389SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
390SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
391SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
392 shader_type_mismatch);
393SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 4, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000394
bsalomon@google.com5782d712011-01-21 21:03:59 +0000395static const GrSamplerState::SampleMode sk_bmp_type_to_sample_mode[] = {
396 (GrSamplerState::SampleMode) -1, // kNone_BitmapType
397 GrSamplerState::kNormal_SampleMode, // kDefault_BitmapType
398 GrSamplerState::kRadial_SampleMode, // kRadial_BitmapType
399 GrSamplerState::kSweep_SampleMode, // kSweep_BitmapType
400 GrSamplerState::kRadial2_SampleMode, // kTwoPointRadial_BitmapType
401};
402
403bool SkGpuDevice::skPaint2GrPaintNoShader(const SkPaint& skPaint,
404 bool justAlpha,
Scroggod757df22011-05-16 13:11:16 +0000405 GrPaint* grPaint,
406 bool constantColor) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000407
408 grPaint->fDither = skPaint.isDither();
409 grPaint->fAntiAlias = skPaint.isAntiAlias();
410
411 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
412 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
413
414 SkXfermode* mode = skPaint.getXfermode();
415 if (mode) {
416 if (!mode->asCoeff(&sm, &dm)) {
reed@google.com1a2e8d22011-01-21 22:08:29 +0000417 SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000418#if 0
419 return false;
420#endif
421 }
422 }
423 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
424 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
425
426 if (justAlpha) {
427 uint8_t alpha = skPaint.getAlpha();
428 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000429 // justAlpha is currently set to true only if there is a texture,
430 // so constantColor should not also be true.
431 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000432 } else {
433 grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000434 grPaint->setTexture(kShaderTextureIdx, NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000435 }
Scroggo97c88c22011-05-11 14:05:25 +0000436 SkColorFilter* colorFilter = skPaint.getColorFilter();
437 SkColor color;
438 SkXfermode::Mode filterMode;
439 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
Scroggod757df22011-05-16 13:11:16 +0000440 if (!constantColor) {
441 grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color);
442 grPaint->fColorFilterXfermode = filterMode;
443 return true;
444 }
445 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
446 grPaint->fColor = SkGr::SkColor2GrColor(filtered);
Scroggo97c88c22011-05-11 14:05:25 +0000447 }
Scroggod757df22011-05-16 13:11:16 +0000448 grPaint->resetColorFilter();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000449 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000450}
451
bsalomon@google.com5782d712011-01-21 21:03:59 +0000452bool SkGpuDevice::skPaint2GrPaintShader(const SkPaint& skPaint,
453 SkAutoCachedTexture* act,
454 const SkMatrix& ctm,
Scroggod757df22011-05-16 13:11:16 +0000455 GrPaint* grPaint,
456 bool constantColor) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000457
bsalomon@google.com5782d712011-01-21 21:03:59 +0000458 SkASSERT(NULL != act);
reed@google.comac10a2d2010-12-22 21:39:39 +0000459
bsalomon@google.com5782d712011-01-21 21:03:59 +0000460 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000461 if (NULL == shader) {
Scroggod757df22011-05-16 13:11:16 +0000462 return this->skPaint2GrPaintNoShader(skPaint,
463 false,
464 grPaint,
465 constantColor);
466 } else if (!this->skPaint2GrPaintNoShader(skPaint, true, grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000467 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000468 }
469
reed@google.comac10a2d2010-12-22 21:39:39 +0000470 SkBitmap bitmap;
471 SkMatrix matrix;
472 SkShader::TileMode tileModes[2];
473 SkScalar twoPointParams[3];
474 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, &matrix,
475 tileModes, twoPointParams);
476
bsalomon@google.com5782d712011-01-21 21:03:59 +0000477 GrSamplerState::SampleMode sampleMode = sk_bmp_type_to_sample_mode[bmptype];
478 if (-1 == sampleMode) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000479 SkShader::GradientInfo info;
480 SkColor color;
481
482 info.fColors = &color;
483 info.fColorOffsets = NULL;
484 info.fColorCount = 1;
485 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
486 SkPaint copy(skPaint);
487 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000488 // modulate the paint alpha by the shader's solid color alpha
489 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
490 copy.setColor(SkColorSetA(color, newA));
reed@google.com2be9e8b2011-07-06 21:18:09 +0000491 return this->skPaint2GrPaintNoShader(copy,
492 false,
493 grPaint,
494 constantColor);
495 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000496 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000497 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000498 GrSamplerState* sampler = grPaint->getTextureSampler(kShaderTextureIdx);
499 sampler->setSampleMode(sampleMode);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000500 if (skPaint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000501 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000502 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000503 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000504 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000505 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
506 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000507 if (GrSamplerState::kRadial2_SampleMode == sampleMode) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000508 sampler->setRadial2Params(twoPointParams[0],
509 twoPointParams[1],
510 twoPointParams[2] < 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000511 }
512
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000513 GrTexture* texture = act->set(this, bitmap, *sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000514 if (NULL == texture) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000515 SkDebugf("Couldn't convert bitmap to texture.\n");
516 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000517 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000518 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000519
520 // since our texture coords will be in local space, we wack the texture
521 // matrix to map them back into 0...1 before we load it
522 SkMatrix localM;
523 if (shader->getLocalMatrix(&localM)) {
524 SkMatrix inverse;
525 if (localM.invert(&inverse)) {
526 matrix.preConcat(inverse);
527 }
528 }
529 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000530 GrScalar sx = GrFixedToScalar(GR_Fixed1 / bitmap.width());
531 GrScalar sy = GrFixedToScalar(GR_Fixed1 / bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +0000532 matrix.postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000533 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000534 GrScalar s = GrFixedToScalar(GR_Fixed1 / bitmap.width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000535 matrix.postScale(s, s);
536 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000537 sampler->setMatrix(matrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000538
539 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000540}
541
542///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000543
544class SkPositionSource {
545public:
546 SkPositionSource(const SkPoint* points, int count)
547 : fPoints(points), fCount(count) {}
548
549 int count() const { return fCount; }
550
551 void writeValue(int i, GrPoint* dstPosition) const {
552 SkASSERT(i < fCount);
553 dstPosition->fX = SkScalarToGrScalar(fPoints[i].fX);
554 dstPosition->fY = SkScalarToGrScalar(fPoints[i].fY);
555 }
556private:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000557 const SkPoint* fPoints;
bsalomon@google.com19628322011-02-03 21:30:17 +0000558 int fCount;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000559};
560
561class SkTexCoordSource {
562public:
563 SkTexCoordSource(const SkPoint* coords)
564 : fCoords(coords) {}
565
566 void writeValue(int i, GrPoint* dstCoord) const {
567 dstCoord->fX = SkScalarToGrScalar(fCoords[i].fX);
568 dstCoord->fY = SkScalarToGrScalar(fCoords[i].fY);
569 }
570private:
571 const SkPoint* fCoords;
572};
573
574class SkColorSource {
575public:
576 SkColorSource(const SkColor* colors) : fColors(colors) {}
577
578 void writeValue(int i, GrColor* dstColor) const {
579 *dstColor = SkGr::SkColor2GrColor(fColors[i]);
580 }
581private:
582 const SkColor* fColors;
583};
584
585class SkIndexSource {
586public:
587 SkIndexSource(const uint16_t* indices, int count)
588 : fIndices(indices), fCount(count) {
589 }
590
591 int count() const { return fCount; }
592
593 void writeValue(int i, uint16_t* dstIndex) const {
594 *dstIndex = fIndices[i];
595 }
596
597private:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000598 const uint16_t* fIndices;
bsalomon@google.com19628322011-02-03 21:30:17 +0000599 int fCount;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000600};
601
602///////////////////////////////////////////////////////////////////////////////
603
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000604#if 0 // not currently being used so don't compile,
605
bsalomon@google.com5782d712011-01-21 21:03:59 +0000606// can be used for positions or texture coordinates
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000607
bsalomon@google.com5782d712011-01-21 21:03:59 +0000608class SkRectFanSource {
609public:
610 SkRectFanSource(const SkRect& rect) : fRect(rect) {}
611
612 int count() const { return 4; }
613
614 void writeValue(int i, GrPoint* dstPoint) const {
615 SkASSERT(i < 4);
616 dstPoint->fX = SkScalarToGrScalar((i % 3) ? fRect.fRight :
617 fRect.fLeft);
618 dstPoint->fY = SkScalarToGrScalar((i < 2) ? fRect.fTop :
619 fRect.fBottom);
620 }
621private:
622 const SkRect& fRect;
623};
624
625class SkIRectFanSource {
626public:
627 SkIRectFanSource(const SkIRect& rect) : fRect(rect) {}
628
629 int count() const { return 4; }
630
631 void writeValue(int i, GrPoint* dstPoint) const {
632 SkASSERT(i < 4);
633 dstPoint->fX = (i % 3) ? GrIntToScalar(fRect.fRight) :
634 GrIntToScalar(fRect.fLeft);
635 dstPoint->fY = (i < 2) ? GrIntToScalar(fRect.fTop) :
636 GrIntToScalar(fRect.fBottom);
637 }
638private:
639 const SkIRect& fRect;
640};
641
642class SkMatRectFanSource {
643public:
644 SkMatRectFanSource(const SkRect& rect, const SkMatrix& matrix)
645 : fRect(rect), fMatrix(matrix) {}
646
647 int count() const { return 4; }
648
649 void writeValue(int i, GrPoint* dstPoint) const {
650 SkASSERT(i < 4);
651
652#if SK_SCALAR_IS_GR_SCALAR
653 fMatrix.mapXY((i % 3) ? fRect.fRight : fRect.fLeft,
654 (i < 2) ? fRect.fTop : fRect.fBottom,
655 (SkPoint*)dstPoint);
656#else
657 SkPoint dst;
658 fMatrix.mapXY((i % 3) ? fRect.fRight : fRect.fLeft,
659 (i < 2) ? fRect.fTop : fRect.fBottom,
660 &dst);
661 dstPoint->fX = SkScalarToGrScalar(dst.fX);
662 dstPoint->fY = SkScalarToGrScalar(dst.fY);
663#endif
664 }
665private:
666 const SkRect& fRect;
667 const SkMatrix& fMatrix;
668};
669
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000670#endif
671
reed@google.comac10a2d2010-12-22 21:39:39 +0000672///////////////////////////////////////////////////////////////////////////////
673
bsalomon@google.com398109c2011-04-14 18:40:27 +0000674void SkGpuDevice::clear(SkColor color) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000675 fContext->clear(NULL, color);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000676}
677
reed@google.comac10a2d2010-12-22 21:39:39 +0000678void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
679 CHECK_SHOULD_DRAW(draw);
680
bsalomon@google.com5782d712011-01-21 21:03:59 +0000681 GrPaint grPaint;
682 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000683 if (!this->skPaint2GrPaintShader(paint,
684 &act,
685 *draw.fMatrix,
686 &grPaint,
687 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000688 return;
689 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000690
691 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000692}
693
694// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000695static const GrPrimitiveType gPointMode2PrimtiveType[] = {
696 kPoints_PrimitiveType,
697 kLines_PrimitiveType,
698 kLineStrip_PrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000699};
700
701void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000702 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000703 CHECK_SHOULD_DRAW(draw);
704
705 SkScalar width = paint.getStrokeWidth();
706 if (width < 0) {
707 return;
708 }
709
710 // we only handle hairlines here, else we let the SkDraw call our drawPath()
711 if (width > 0) {
712 draw.drawPoints(mode, count, pts, paint, true);
713 return;
714 }
715
bsalomon@google.com5782d712011-01-21 21:03:59 +0000716 GrPaint grPaint;
717 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000718 if (!this->skPaint2GrPaintShader(paint,
719 &act,
720 *draw.fMatrix,
721 &grPaint,
722 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000723 return;
724 }
725
reed@google.comac10a2d2010-12-22 21:39:39 +0000726#if SK_SCALAR_IS_GR_SCALAR
bsalomon@google.com5782d712011-01-21 21:03:59 +0000727 fContext->drawVertices(grPaint,
728 gPointMode2PrimtiveType[mode],
729 count,
730 (GrPoint*)pts,
731 NULL,
732 NULL,
733 NULL,
734 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000735#else
bsalomon@google.com5782d712011-01-21 21:03:59 +0000736 fContext->drawCustomVertices(grPaint,
737 gPointMode2PrimtiveType[mode],
738 SkPositionSource(pts, count));
reed@google.comac10a2d2010-12-22 21:39:39 +0000739#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000740}
741
reed@google.comc9aa5872011-04-05 21:05:37 +0000742///////////////////////////////////////////////////////////////////////////////
743
reed@google.comac10a2d2010-12-22 21:39:39 +0000744void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
745 const SkPaint& paint) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000746 CHECK_SHOULD_DRAW(draw);
747
bungeman@google.com79bd8772011-07-18 15:34:08 +0000748 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000749 SkScalar width = paint.getStrokeWidth();
750
751 /*
752 We have special code for hairline strokes, miter-strokes, and fills.
753 Anything else we just call our path code.
754 */
755 bool usePath = doStroke && width > 0 &&
756 paint.getStrokeJoin() != SkPaint::kMiter_Join;
757 // another reason we might need to call drawPath...
758 if (paint.getMaskFilter()) {
759 usePath = true;
760 }
reed@google.com67db6642011-05-26 11:46:35 +0000761 // until we aa rotated rects...
762 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
763 usePath = true;
764 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000765 // small miter limit means right angles show bevel...
766 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
767 paint.getStrokeMiter() < SK_ScalarSqrt2)
768 {
769 usePath = true;
770 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000771 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000772 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
773 usePath = true;
774 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000775
776 if (usePath) {
777 SkPath path;
778 path.addRect(rect);
779 this->drawPath(draw, path, paint, NULL, true);
780 return;
781 }
782
783 GrPaint grPaint;
784 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000785 if (!this->skPaint2GrPaintShader(paint,
786 &act,
787 *draw.fMatrix,
788 &grPaint,
789 true)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000790 return;
791 }
reed@google.com20efde72011-05-09 17:00:02 +0000792 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000793}
794
reed@google.com69302852011-02-16 18:08:07 +0000795#include "SkMaskFilter.h"
796#include "SkBounder.h"
797
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000798static GrPathFill skToGrFillType(SkPath::FillType fillType) {
799 switch (fillType) {
800 case SkPath::kWinding_FillType:
801 return kWinding_PathFill;
802 case SkPath::kEvenOdd_FillType:
803 return kEvenOdd_PathFill;
804 case SkPath::kInverseWinding_FillType:
805 return kInverseWinding_PathFill;
806 case SkPath::kInverseEvenOdd_FillType:
807 return kInverseEvenOdd_PathFill;
808 default:
809 SkDebugf("Unsupported path fill type\n");
810 return kHairLine_PathFill;
811 }
812}
813
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000814static void buildKernel(float sigma, float* kernel, int kernelWidth) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000815 int halfWidth = (kernelWidth - 1) / 2;
816 float sum = 0.0f;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000817 float denom = 1.0f / (2.0f * sigma * sigma);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000818 for (int i = 0; i < kernelWidth; ++i) {
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000819 float x = static_cast<float>(i - halfWidth);
820 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
821 // is dropped here, since we renormalize the kernel below.
822 kernel[i] = sk_float_exp(- x * x * denom);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000823 sum += kernel[i];
824 }
825 // Normalize the kernel
826 float scale = 1.0f / sum;
827 for (int i = 0; i < kernelWidth; ++i)
828 kernel[i] *= scale;
829}
830
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000831static void scaleRect(SkRect* rect, float scale) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000832 rect->fLeft *= scale;
833 rect->fTop *= scale;
834 rect->fRight *= scale;
835 rect->fBottom *= scale;
836}
837
838static bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
839 SkMaskFilter* filter, const SkMatrix& matrix,
840 const SkRegion& clip, SkBounder* bounder,
841 GrPaint* grp) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000842#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000843 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000844#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000845 SkMaskFilter::BlurInfo info;
846 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000847 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000848 return false;
849 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000850 SkScalar radius = info.fIgnoreTransform ? info.fRadius
851 : matrix.mapRadius(info.fRadius);
852 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000853 if (radius <= 0) {
854 return false;
855 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000856 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000857 SkRect srcRect = path.getBounds();
858
859 int scaleFactor = 1;
860
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000861 while (sigma > MAX_BLUR_SIGMA) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000862 scaleFactor *= 2;
863 sigma *= 0.5f;
864 }
senorblanco@chromium.org4a947d22011-07-18 21:48:35 +0000865 int halfWidth = static_cast<int>(ceilf(sigma * 3.0f));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000866 int kernelWidth = halfWidth * 2 + 1;
867
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000868 float invScale = 1.0f / scaleFactor;
869 scaleRect(&srcRect, invScale);
870 srcRect.roundOut();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000871 srcRect.inset(-halfWidth, -halfWidth);
872
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000873 SkRect clipBounds;
874 clipBounds.set(clip.getBounds());
875 scaleRect(&clipBounds, invScale);
876 clipBounds.roundOut();
877 clipBounds.inset(-halfWidth, -halfWidth);
878
879 srcRect.intersect(clipBounds);
880
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000881 scaleRect(&srcRect, scaleFactor);
882 SkRect finalRect = srcRect;
883
884 SkIRect finalIRect;
885 finalRect.roundOut(&finalIRect);
886 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000887 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000888 }
889 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000890 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000891 }
892 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
893 srcRect.offset(-srcRect.fLeft, -srcRect.fTop);
894 const GrTextureDesc desc = {
895 kRenderTarget_GrTextureFlagBit,
896 kNone_GrAALevel,
897 srcRect.width(),
898 srcRect.height(),
899 // We actually only need A8, but it often isn't supported as a
900 // render target
901 kRGBA_8888_GrPixelConfig
902 };
903
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000904 GrAutoScratchTexture srcEntry(context, desc);
905 GrAutoScratchTexture dstEntry(context, desc);
906 if (NULL == srcEntry.texture() || NULL == dstEntry.texture()) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000907 return false;
908 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000909 GrTexture* srcTexture = srcEntry.texture();
910 GrTexture* dstTexture = dstEntry.texture();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000911 if (NULL == srcTexture || NULL == dstTexture) {
912 return false;
913 }
914 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000915 // Once this code moves into GrContext, this should be changed to use
916 // an AutoClipRestore.
917 GrClip oldClip = context->getClip();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000918 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000919 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000920 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000921 GrPaint tempPaint;
922 tempPaint.reset();
923
924 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000925 tempPaint.fAntiAlias = grp->fAntiAlias;
926 if (tempPaint.fAntiAlias) {
927 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
928 // blend coeff of zero requires dual source blending support in order
929 // to properly blend partially covered pixels. This means the AA
930 // code path may not be taken. So we use a dst blend coeff of ISA. We
931 // could special case AA draws to a dst surface with known alpha=0 to
932 // use a zero dst coeff when dual source blending isn't available.
933 tempPaint.fSrcBlendCoeff = kOne_BlendCoeff;
934 tempPaint.fDstBlendCoeff = kISC_BlendCoeff;
935 }
936 // Draw hard shadow to dstTexture with path topleft at origin 0,0.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000937 context->drawPath(tempPaint, path, skToGrFillType(path.getFillType()), &offset);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000938 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000939
940 GrMatrix sampleM;
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000941 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000942 GrPaint paint;
943 paint.reset();
944 paint.getTextureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
945 paint.getTextureSampler(0)->setMatrix(sampleM);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000946 GrAutoScratchTexture origEntry;
947
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000948 if (blurType != SkMaskFilter::kNormal_BlurType) {
949 // Stash away a copy of the unblurred image.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000950 origEntry.set(context, desc);
951 if (NULL == origEntry.texture()) {
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000952 return false;
953 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000954 context->setRenderTarget(origEntry.texture()->asRenderTarget());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000955 paint.setTexture(0, srcTexture);
956 context->drawRect(paint, srcRect);
957 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000958 for (int i = 1; i < scaleFactor; i *= 2) {
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000959 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
960 paint.getTextureSampler(0)->setMatrix(sampleM);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000961 context->setRenderTarget(dstTexture->asRenderTarget());
962 SkRect dstRect(srcRect);
963 scaleRect(&dstRect, 0.5f);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000964 paint.setTexture(0, srcTexture);
965 context->drawRectToRect(paint, dstRect, srcRect);
966 srcRect = dstRect;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000967 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000968 }
969
970 SkAutoTMalloc<float> kernelStorage(kernelWidth);
971 float* kernel = kernelStorage.get();
972 buildKernel(sigma, kernel, kernelWidth);
973
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000974 // Clear out a halfWidth to the right of the srcRect to prevent the
975 // X convolution from reading garbage.
976 SkIRect clearRect = SkIRect::MakeXYWH(
977 srcRect.fRight, srcRect.fTop, halfWidth, srcRect.height());
978 context->clear(&clearRect, 0x0);
979
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000980 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000981 context->convolveInX(srcTexture, srcRect, kernel, kernelWidth);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000982 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000983
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000984 // Clear out a halfWidth below the srcRect to prevent the Y
985 // convolution from reading garbage.
986 clearRect = SkIRect::MakeXYWH(
987 srcRect.fLeft, srcRect.fBottom, srcRect.width(), halfWidth);
988 context->clear(&clearRect, 0x0);
989
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000990 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000991 context->convolveInY(srcTexture, srcRect, kernel, kernelWidth);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000992 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000993
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000994 // Clear one pixel to the right and below, to accommodate bilinear
995 // upsampling.
996 clearRect = SkIRect::MakeXYWH(
997 srcRect.fLeft, srcRect.fBottom, srcRect.width() + 1, 1);
998 context->clear(&clearRect, 0x0);
999 clearRect = SkIRect::MakeXYWH(
1000 srcRect.fRight, srcRect.fTop, 1, srcRect.height());
1001 context->clear(&clearRect, 0x0);
1002
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001003 if (scaleFactor > 1) {
1004 // FIXME: This should be mitchell, not bilinear.
1005 paint.getTextureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +00001006 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001007 paint.getTextureSampler(0)->setMatrix(sampleM);
1008 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001009 paint.setTexture(0, srcTexture);
1010 SkRect dstRect(srcRect);
1011 scaleRect(&dstRect, scaleFactor);
1012 context->drawRectToRect(paint, dstRect, srcRect);
1013 srcRect = dstRect;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001014 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001015 }
1016
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001017 if (blurType != SkMaskFilter::kNormal_BlurType) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001018 GrTexture* origTexture = origEntry.texture();
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001019 paint.getTextureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +00001020 sampleM.setIDiv(origTexture->width(), origTexture->height());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001021 paint.getTextureSampler(0)->setMatrix(sampleM);
1022 // Blend origTexture over srcTexture.
1023 context->setRenderTarget(srcTexture->asRenderTarget());
1024 paint.setTexture(0, origTexture);
1025 if (SkMaskFilter::kInner_BlurType == blurType) {
1026 // inner: dst = dst * src
1027 paint.fSrcBlendCoeff = kDC_BlendCoeff;
1028 paint.fDstBlendCoeff = kZero_BlendCoeff;
1029 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
1030 // solid: dst = src + dst - src * dst
1031 // = (1 - dst) * src + 1 * dst
1032 paint.fSrcBlendCoeff = kIDC_BlendCoeff;
1033 paint.fDstBlendCoeff = kOne_BlendCoeff;
1034 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
1035 // outer: dst = dst * (1 - src)
1036 // = 0 * src + (1 - src) * dst
1037 paint.fSrcBlendCoeff = kZero_BlendCoeff;
1038 paint.fDstBlendCoeff = kISC_BlendCoeff;
1039 }
1040 context->drawRect(paint, srcRect);
1041 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001042 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +00001043 context->setClip(oldClip);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001044
1045 if (grp->hasTextureOrMask()) {
1046 GrMatrix inverse;
1047 if (!matrix.invert(&inverse)) {
1048 return false;
1049 }
1050 grp->preConcatActiveSamplerMatrices(inverse);
1051 }
1052
1053 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
1054 // we assume the last mask index is available for use
1055 GrAssert(NULL == grp->getMask(MASK_IDX));
1056 grp->setMask(MASK_IDX, srcTexture);
1057 grp->getMaskSampler(MASK_IDX)->setClampNoFilter();
1058
1059 GrMatrix m;
1060 m.setTranslate(-finalRect.fLeft, -finalRect.fTop);
1061 m.postIDiv(srcTexture->width(), srcTexture->height());
1062 grp->getMaskSampler(MASK_IDX)->setMatrix(m);
1063 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001064 return true;
1065}
1066
reed@google.com69302852011-02-16 18:08:07 +00001067static bool drawWithMaskFilter(GrContext* context, const SkPath& path,
1068 SkMaskFilter* filter, const SkMatrix& matrix,
1069 const SkRegion& clip, SkBounder* bounder,
1070 GrPaint* grp) {
1071 SkMask srcM, dstM;
1072
1073 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
1074 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
1075 return false;
1076 }
1077
1078 SkAutoMaskImage autoSrc(&srcM, false);
1079
1080 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
1081 return false;
1082 }
1083 // this will free-up dstM when we're done (allocated in filterMask())
1084 SkAutoMaskImage autoDst(&dstM, false);
1085
1086 if (clip.quickReject(dstM.fBounds)) {
1087 return false;
1088 }
1089 if (bounder && !bounder->doIRect(dstM.fBounds)) {
1090 return false;
1091 }
1092
1093 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
1094 // the current clip (and identity matrix) and grpaint settings
1095
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001096 // used to compute inverse view, if necessary
1097 GrMatrix ivm = context->getMatrix();
1098
reed@google.com0c219b62011-02-16 21:31:18 +00001099 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +00001100
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001101 const GrTextureDesc desc = {
1102 kNone_GrTextureFlags,
1103 kNone_GrAALevel,
reed@google.com69302852011-02-16 18:08:07 +00001104 dstM.fBounds.width(),
1105 dstM.fBounds.height(),
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001106 kAlpha_8_GrPixelConfig
reed@google.com69302852011-02-16 18:08:07 +00001107 };
1108
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001109 GrAutoScratchTexture ast(context, desc);
1110 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001111
reed@google.com69302852011-02-16 18:08:07 +00001112 if (NULL == texture) {
1113 return false;
1114 }
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001115 texture->uploadTextureData(0, 0, desc.fWidth, desc.fHeight,
1116 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +00001117
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001118 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
1119 grp->preConcatActiveSamplerMatrices(ivm);
1120 }
1121
1122 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
1123 // we assume the last mask index is available for use
1124 GrAssert(NULL == grp->getMask(MASK_IDX));
1125 grp->setMask(MASK_IDX, texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001126 grp->getMaskSampler(MASK_IDX)->setClampNoFilter();
reed@google.com69302852011-02-16 18:08:07 +00001127
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001128 GrRect d;
1129 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +00001130 GrIntToScalar(dstM.fBounds.fTop),
1131 GrIntToScalar(dstM.fBounds.fRight),
1132 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001133
1134 GrMatrix m;
1135 m.setTranslate(-dstM.fBounds.fLeft, -dstM.fBounds.fTop);
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001136 m.postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001137 grp->getMaskSampler(MASK_IDX)->setMatrix(m);
1138
1139 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +00001140 return true;
1141}
reed@google.com69302852011-02-16 18:08:07 +00001142
reed@google.com0c219b62011-02-16 21:31:18 +00001143void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
reed@google.comac10a2d2010-12-22 21:39:39 +00001144 const SkPaint& paint, const SkMatrix* prePathMatrix,
1145 bool pathIsMutable) {
1146 CHECK_SHOULD_DRAW(draw);
1147
bsalomon@google.com5782d712011-01-21 21:03:59 +00001148 GrPaint grPaint;
1149 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +00001150 if (!this->skPaint2GrPaintShader(paint,
1151 &act,
1152 *draw.fMatrix,
1153 &grPaint,
1154 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001155 return;
1156 }
1157
reed@google.com0c219b62011-02-16 21:31:18 +00001158 // BEGIN lift from SkDraw::drawPath()
1159
1160 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1161 bool doFill = true;
1162 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001163
1164 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001165 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001166
reed@google.come3445642011-02-16 23:20:39 +00001167 if (!pathIsMutable) {
1168 result = &tmpPath;
1169 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001170 }
reed@google.come3445642011-02-16 23:20:39 +00001171 // should I push prePathMatrix on our MV stack temporarily, instead
1172 // of applying it here? See SkDraw.cpp
1173 pathPtr->transform(*prePathMatrix, result);
1174 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001175 }
reed@google.com0c219b62011-02-16 21:31:18 +00001176 // at this point we're done with prePathMatrix
1177 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001178
bsalomon@google.com04de7822011-03-25 18:04:43 +00001179 // This "if" is not part of the SkDraw::drawPath() lift.
1180 // When we get a 1.0 wide stroke we hairline stroke it instead of creating
1181 // a new stroked-path. This is motivated by canvas2D sites that draw
1182 // lines as 1.0 wide stroked paths. We can consider doing an alpha-modulated-
1183 // hairline for width < 1.0 when AA is enabled.
1184 static const int gMatrixMask = ~(SkMatrix::kIdentity_Mask |
1185 SkMatrix::kTranslate_Mask);
1186 if (!paint.getPathEffect() &&
1187 SkPaint::kStroke_Style == paint.getStyle() &&
1188 !(draw.fMatrix->getType() & gMatrixMask) &&
1189 SK_Scalar1 == paint.getStrokeWidth()) {
1190 doFill = false;
1191 }
1192
1193 if (doFill && (paint.getPathEffect() ||
1194 paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.com0c219b62011-02-16 21:31:18 +00001195 doFill = paint.getFillPath(*pathPtr, &tmpPath);
1196 pathPtr = &tmpPath;
1197 }
1198
1199 // END lift from SkDraw::drawPath()
1200
reed@google.com69302852011-02-16 18:08:07 +00001201 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001202 // avoid possibly allocating a new path in transform if we can
1203 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1204
1205 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001206 pathPtr->transform(*draw.fMatrix, devPathPtr);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001207 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
1208 *draw.fMatrix, *draw.fClip, draw.fBounder,
1209 &grPaint)) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001210 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
1211 *draw.fMatrix, *draw.fClip, draw.fBounder,
1212 &grPaint);
1213 }
reed@google.com69302852011-02-16 18:08:07 +00001214 return;
1215 }
reed@google.com69302852011-02-16 18:08:07 +00001216
bsalomon@google.comffca4002011-02-22 20:34:01 +00001217 GrPathFill fill = kHairLine_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001218
reed@google.com0c219b62011-02-16 21:31:18 +00001219 if (doFill) {
1220 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001221 case SkPath::kWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001222 fill = kWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001223 break;
1224 case SkPath::kEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001225 fill = kEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001226 break;
1227 case SkPath::kInverseWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001228 fill = kInverseWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001229 break;
1230 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001231 fill = kInverseEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001232 break;
1233 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001234 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001235 return;
1236 }
1237 }
1238
reed@google.com07f3ee12011-05-16 17:21:57 +00001239 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001240}
1241
reed@google.comac10a2d2010-12-22 21:39:39 +00001242void SkGpuDevice::drawBitmap(const SkDraw& draw,
1243 const SkBitmap& bitmap,
1244 const SkIRect* srcRectPtr,
1245 const SkMatrix& m,
1246 const SkPaint& paint) {
1247 CHECK_SHOULD_DRAW(draw);
1248
1249 SkIRect srcRect;
1250 if (NULL == srcRectPtr) {
1251 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1252 } else {
1253 srcRect = *srcRectPtr;
1254 }
1255
junov@google.comd935cfb2011-06-27 20:48:23 +00001256 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001257 // Convert the bitmap to a shader so that the rect can be drawn
1258 // through drawRect, which supports mask filters.
1259 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001260 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001261 if (srcRectPtr) {
1262 if (!bitmap.extractSubset(&tmp, srcRect)) {
1263 return; // extraction failed
1264 }
1265 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001266 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001267 }
1268 SkPaint paintWithTexture(paint);
1269 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1270 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001271 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001272 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001273
junov@google.com1d329782011-07-28 20:10:09 +00001274 // Transform 'm' needs to be concatenated to the draw matrix,
1275 // rather than transforming the primitive directly, so that 'm' will
1276 // also affect the behavior of the mask filter.
1277 SkMatrix drawMatrix;
1278 drawMatrix.setConcat(*draw.fMatrix, m);
1279 SkDraw transformedDraw(draw);
1280 transformedDraw.fMatrix = &drawMatrix;
1281
1282 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1283
junov@google.comd935cfb2011-06-27 20:48:23 +00001284 return;
1285 }
1286
bsalomon@google.com5782d712011-01-21 21:03:59 +00001287 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001288 if (!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001289 return;
1290 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001291 GrSamplerState* sampler = grPaint.getTextureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001292 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001293 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001294 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001295 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001296 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001297
bsalomon@google.com91958362011-06-13 17:58:13 +00001298 const int maxTextureSize = fContext->getMaxTextureSize();
1299 if (bitmap.getTexture() || (bitmap.width() <= maxTextureSize &&
1300 bitmap.height() <= maxTextureSize)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001301 // take the fast case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001302 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001303 return;
1304 }
1305
1306 // undo the translate done by SkCanvas
1307 int DX = SkMax32(0, srcRect.fLeft);
1308 int DY = SkMax32(0, srcRect.fTop);
1309 // compute clip bounds in local coordinates
1310 SkIRect clipRect;
1311 {
1312 SkRect r;
1313 r.set(draw.fClip->getBounds());
1314 SkMatrix matrix, inverse;
1315 matrix.setConcat(*draw.fMatrix, m);
1316 if (!matrix.invert(&inverse)) {
1317 return;
1318 }
1319 inverse.mapRect(&r);
1320 r.roundOut(&clipRect);
1321 // apply the canvas' translate to our local clip
1322 clipRect.offset(DX, DY);
1323 }
1324
bsalomon@google.com91958362011-06-13 17:58:13 +00001325 int nx = bitmap.width() / maxTextureSize;
1326 int ny = bitmap.height() / maxTextureSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001327 for (int x = 0; x <= nx; x++) {
1328 for (int y = 0; y <= ny; y++) {
1329 SkIRect tileR;
bsalomon@google.com91958362011-06-13 17:58:13 +00001330 tileR.set(x * maxTextureSize, y * maxTextureSize,
1331 (x + 1) * maxTextureSize, (y + 1) * maxTextureSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001332 if (!SkIRect::Intersects(tileR, clipRect)) {
1333 continue;
1334 }
1335
1336 SkIRect srcR = tileR;
1337 if (!srcR.intersect(srcRect)) {
1338 continue;
1339 }
1340
1341 SkBitmap tmpB;
1342 if (bitmap.extractSubset(&tmpB, tileR)) {
1343 // now offset it to make it "local" to our tmp bitmap
1344 srcR.offset(-tileR.fLeft, -tileR.fTop);
1345
1346 SkMatrix tmpM(m);
1347 {
1348 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1349 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1350 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1351 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001352 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001353 }
1354 }
1355 }
1356}
1357
1358/*
1359 * This is called by drawBitmap(), which has to handle images that may be too
1360 * large to be represented by a single texture.
1361 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001362 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1363 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001364 */
1365void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1366 const SkBitmap& bitmap,
1367 const SkIRect& srcRect,
1368 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001369 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001370 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1371 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001372
reed@google.com9c49bc32011-07-07 13:42:37 +00001373 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001374 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001375 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001376 return;
1377 }
1378
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001379 GrSamplerState* sampler = grPaint->getTextureSampler(kBitmapTextureIdx);
1380
1381 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1382 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
1383 sampler->setSampleMode(GrSamplerState::kNormal_SampleMode);
1384 sampler->setMatrix(GrMatrix::I());
reed@google.comac10a2d2010-12-22 21:39:39 +00001385
1386 GrTexture* texture;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001387 SkAutoCachedTexture act(this, bitmap, *sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001388 if (NULL == texture) {
1389 return;
1390 }
1391
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001392 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001393
reed@google.com20efde72011-05-09 17:00:02 +00001394 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1395 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001396 GrRect paintRect;
junov@google.com6acc9b32011-05-16 18:32:07 +00001397 paintRect.setLTRB(GrFixedToScalar((srcRect.fLeft << 16) / bitmap.width()),
1398 GrFixedToScalar((srcRect.fTop << 16) / bitmap.height()),
1399 GrFixedToScalar((srcRect.fRight << 16) / bitmap.width()),
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001400 GrFixedToScalar((srcRect.fBottom << 16) / bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001401
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001402 if (GrSamplerState::kNearest_Filter != sampler->getFilter() &&
junov@google.com6acc9b32011-05-16 18:32:07 +00001403 (srcRect.width() < bitmap.width() ||
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001404 srcRect.height() < bitmap.height())) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001405 // If drawing a subrect of the bitmap and filtering is enabled,
1406 // use a constrained texture domain to avoid color bleeding
1407 GrScalar left, top, right, bottom;
1408 if (srcRect.width() > 1) {
1409 GrScalar border = GR_ScalarHalf / bitmap.width();
1410 left = paintRect.left() + border;
1411 right = paintRect.right() - border;
1412 } else {
1413 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1414 }
1415 if (srcRect.height() > 1) {
1416 GrScalar border = GR_ScalarHalf / bitmap.height();
1417 top = paintRect.top() + border;
1418 bottom = paintRect.bottom() - border;
1419 } else {
1420 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1421 }
1422 GrRect textureDomain;
1423 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001424 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001425 }
1426
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001427 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001428}
1429
1430void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1431 int left, int top, const SkPaint& paint) {
1432 CHECK_SHOULD_DRAW(draw);
1433
1434 SkAutoLockPixels alp(bitmap);
1435 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1436 return;
1437 }
1438
bsalomon@google.com5782d712011-01-21 21:03:59 +00001439 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001440 if(!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001441 return;
1442 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001443
bsalomon@google.com5782d712011-01-21 21:03:59 +00001444 GrAutoMatrix avm(fContext, GrMatrix::I());
1445
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001446 GrSamplerState* sampler = grPaint.getTextureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001447
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001448 GrTexture* texture;
1449 sampler->setClampNoFilter();
1450 SkAutoCachedTexture act(this, bitmap, *sampler, &texture);
1451
1452 grPaint.setTexture(kBitmapTextureIdx, texture);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001453
bsalomon@google.com5782d712011-01-21 21:03:59 +00001454 fContext->drawRectToRect(grPaint,
reed@google.com20efde72011-05-09 17:00:02 +00001455 GrRect::MakeXYWH(GrIntToScalar(left),
1456 GrIntToScalar(top),
1457 GrIntToScalar(bitmap.width()),
1458 GrIntToScalar(bitmap.height())),
1459 GrRect::MakeWH(GR_Scalar1, GR_Scalar1));
reed@google.comac10a2d2010-12-22 21:39:39 +00001460}
1461
1462void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* dev,
1463 int x, int y, const SkPaint& paint) {
1464 CHECK_SHOULD_DRAW(draw);
1465
bsalomon@google.com5782d712011-01-21 21:03:59 +00001466 GrPaint grPaint;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001467 if (!((SkGpuDevice*)dev)->bindDeviceAsTexture(&grPaint) ||
Scroggod757df22011-05-16 13:11:16 +00001468 !this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001469 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001470 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001471
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001472 GrTexture* devTex = grPaint.getTexture(0);
1473 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001474
1475 const SkBitmap& bm = dev->accessBitmap(false);
1476 int w = bm.width();
1477 int h = bm.height();
1478
1479 GrAutoMatrix avm(fContext, GrMatrix::I());
1480
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001481 grPaint.getTextureSampler(kBitmapTextureIdx)->setClampNoFilter();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001482
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001483 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1484 GrIntToScalar(y),
1485 GrIntToScalar(w),
1486 GrIntToScalar(h));
1487 // The device being drawn may not fill up its texture (saveLayer uses
1488 // the approximate ).
1489 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1490 GR_Scalar1 * h / devTex->height());
1491
1492 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001493}
1494
1495///////////////////////////////////////////////////////////////////////////////
1496
1497// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001498static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1499 kTriangles_PrimitiveType,
1500 kTriangleStrip_PrimitiveType,
1501 kTriangleFan_PrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001502};
1503
1504void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1505 int vertexCount, const SkPoint vertices[],
1506 const SkPoint texs[], const SkColor colors[],
1507 SkXfermode* xmode,
1508 const uint16_t indices[], int indexCount,
1509 const SkPaint& paint) {
1510 CHECK_SHOULD_DRAW(draw);
1511
bsalomon@google.com5782d712011-01-21 21:03:59 +00001512 GrPaint grPaint;
1513 SkAutoCachedTexture act;
1514 // we ignore the shader if texs is null.
1515 if (NULL == texs) {
Scroggod757df22011-05-16 13:11:16 +00001516 if (!this->skPaint2GrPaintNoShader(paint,
1517 false,
1518 &grPaint,
1519 NULL == colors)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001520 return;
1521 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001522 } else {
reed@google.com1a2e8d22011-01-21 22:08:29 +00001523 if (!this->skPaint2GrPaintShader(paint, &act,
1524 *draw.fMatrix,
Scroggod757df22011-05-16 13:11:16 +00001525 &grPaint,
1526 NULL == colors)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001527 return;
1528 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001529 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001530
1531 if (NULL != xmode && NULL != texs && NULL != colors) {
1532 SkXfermode::Mode mode;
1533 if (!SkXfermode::IsMode(xmode, &mode) ||
1534 SkXfermode::kMultiply_Mode != mode) {
1535 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1536#if 0
1537 return
1538#endif
1539 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001540 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001541
1542#if SK_SCALAR_IS_GR_SCALAR
1543 // even if GrColor and SkColor byte offsets match we need
1544 // to perform pre-multiply.
1545 if (NULL == colors) {
1546 fContext->drawVertices(grPaint,
1547 gVertexMode2PrimitiveType[vmode],
1548 vertexCount,
1549 (GrPoint*) vertices,
1550 (GrPoint*) texs,
1551 NULL,
1552 indices,
1553 indexCount);
1554 } else
1555#endif
1556 {
1557 SkTexCoordSource texSrc(texs);
1558 SkColorSource colSrc(colors);
1559 SkIndexSource idxSrc(indices, indexCount);
1560
1561 fContext->drawCustomVertices(grPaint,
1562 gVertexMode2PrimitiveType[vmode],
1563 SkPositionSource(vertices, vertexCount),
1564 (NULL == texs) ? NULL : &texSrc,
1565 (NULL == colors) ? NULL : &colSrc,
1566 (NULL == indices) ? NULL : &idxSrc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001567 }
1568}
1569
1570///////////////////////////////////////////////////////////////////////////////
1571
1572static void GlyphCacheAuxProc(void* data) {
1573 delete (GrFontScaler*)data;
1574}
1575
1576static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1577 void* auxData;
1578 GrFontScaler* scaler = NULL;
1579 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1580 scaler = (GrFontScaler*)auxData;
1581 }
1582 if (NULL == scaler) {
1583 scaler = new SkGrFontScaler(cache);
1584 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1585 }
1586 return scaler;
1587}
1588
1589static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1590 SkFixed fx, SkFixed fy,
1591 const SkGlyph& glyph) {
1592 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1593
1594 GrSkDrawProcs* procs = (GrSkDrawProcs*)state.fDraw->fProcs;
1595
1596 if (NULL == procs->fFontScaler) {
1597 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1598 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001599
1600 /*
reed@google.com3b139f52011-06-07 17:56:25 +00001601 * What should we do with fy? (assuming horizontal/latin text)
reed@google.com39ce0ac2011-04-08 15:42:19 +00001602 *
reed@google.com3b139f52011-06-07 17:56:25 +00001603 * The raster code calls SkFixedFloorToFixed on it, as it does with fx.
1604 * It calls that rather than round, because our caller has already added
1605 * SK_FixedHalf, so that calling floor gives us the rounded integer.
1606 *
1607 * Test code between raster and gpu (they should draw the same)
1608 *
1609 * canvas->drawText("Hamburgefons", 12, 0, 16.5f, paint);
1610 *
1611 * Perhaps we should only perform this integralization if there is no
1612 * fExtMatrix...
reed@google.com39ce0ac2011-04-08 15:42:19 +00001613 */
reed@google.com3b139f52011-06-07 17:56:25 +00001614 fy = SkFixedFloorToFixed(fy);
1615
reed@google.comac10a2d2010-12-22 21:39:39 +00001616 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), fx, 0),
reed@google.com3b139f52011-06-07 17:56:25 +00001617 SkFixedFloorToFixed(fx), fy,
reed@google.comac10a2d2010-12-22 21:39:39 +00001618 procs->fFontScaler);
1619}
1620
bsalomon@google.com5782d712011-01-21 21:03:59 +00001621SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001622
1623 // deferred allocation
1624 if (NULL == fDrawProcs) {
1625 fDrawProcs = new GrSkDrawProcs;
1626 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1627 fDrawProcs->fContext = fContext;
1628 }
1629
1630 // init our (and GL's) state
1631 fDrawProcs->fTextContext = context;
1632 fDrawProcs->fFontScaler = NULL;
1633 return fDrawProcs;
1634}
1635
1636void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1637 size_t byteLength, SkScalar x, SkScalar y,
1638 const SkPaint& paint) {
1639 CHECK_SHOULD_DRAW(draw);
1640
1641 if (draw.fMatrix->getType() & SkMatrix::kPerspective_Mask) {
1642 // this guy will just call our drawPath()
1643 draw.drawText((const char*)text, byteLength, x, y, paint);
1644 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001645 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001646
1647 GrPaint grPaint;
1648 SkAutoCachedTexture act;
1649
Scroggod757df22011-05-16 13:11:16 +00001650 if (!this->skPaint2GrPaintShader(paint,
1651 &act,
1652 *draw.fMatrix,
1653 &grPaint,
1654 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001655 return;
1656 }
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001657 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001658 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001659 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1660 }
1661}
1662
1663void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1664 size_t byteLength, const SkScalar pos[],
1665 SkScalar constY, int scalarsPerPos,
1666 const SkPaint& paint) {
1667 CHECK_SHOULD_DRAW(draw);
1668
1669 if (draw.fMatrix->getType() & SkMatrix::kPerspective_Mask) {
1670 // this guy will just call our drawPath()
1671 draw.drawPosText((const char*)text, byteLength, pos, constY,
1672 scalarsPerPos, paint);
1673 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001674 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001675
1676 GrPaint grPaint;
1677 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +00001678 if (!this->skPaint2GrPaintShader(paint,
1679 &act,
1680 *draw.fMatrix,
1681 &grPaint,
1682 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001683 return;
1684 }
1685
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001686 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001687 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001688 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1689 scalarsPerPos, paint);
1690 }
1691}
1692
1693void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1694 size_t len, const SkPath& path,
1695 const SkMatrix* m, const SkPaint& paint) {
1696 CHECK_SHOULD_DRAW(draw);
1697
1698 SkASSERT(draw.fDevice == this);
1699 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1700}
1701
1702///////////////////////////////////////////////////////////////////////////////
1703
reed@google.comf67e4cf2011-03-15 20:56:58 +00001704bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1705 if (!paint.isLCDRenderText()) {
1706 // we're cool with the paint as is
1707 return false;
1708 }
1709
1710 if (paint.getShader() ||
1711 paint.getXfermode() || // unless its srcover
1712 paint.getMaskFilter() ||
1713 paint.getRasterizer() ||
1714 paint.getColorFilter() ||
1715 paint.getPathEffect() ||
1716 paint.isFakeBoldText() ||
1717 paint.getStyle() != SkPaint::kFill_Style) {
1718 // turn off lcd
1719 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1720 flags->fHinting = paint.getHinting();
1721 return true;
1722 }
1723 // we're cool with the paint as is
1724 return false;
1725}
1726
1727///////////////////////////////////////////////////////////////////////////////
1728
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001729SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(const SkBitmap& bitmap,
1730 const GrSamplerState& sampler,
1731 TexType type) {
1732 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001733 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001734
bsalomon@google.come97f0852011-06-17 13:10:25 +00001735 if (kBitmap_TexType != type) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001736 const GrTextureDesc desc = {
1737 kRenderTarget_GrTextureFlagBit,
1738 kNone_GrAALevel,
1739 bitmap.width(),
1740 bitmap.height(),
1741 SkGr::Bitmap2PixelConfig(bitmap)
1742 };
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001743 GrContext::ScratchTexMatch match;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001744 if (kSaveLayerDeviceRenderTarget_TexType == type) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001745 // we know layers will only be drawn through drawDevice.
1746 // drawDevice has been made to work with content embedded in a
1747 // larger texture so its okay to use the approximate version.
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001748 match = GrContext::kApprox_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001749 } else {
bsalomon@google.come97f0852011-06-17 13:10:25 +00001750 SkASSERT(kDeviceRenderTarget_TexType == type);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001751 match = GrContext::kExact_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001752 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001753 entry = ctx->lockScratchTexture(desc, match);
reed@google.comac10a2d2010-12-22 21:39:39 +00001754 } else {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001755 if (!bitmap.isVolatile()) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001756 GrContext::TextureKey key = bitmap.getGenerationID();
1757 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
junov@google.com4ee7ae52011-06-30 17:30:49 +00001758
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001759 entry = ctx->findAndLockTexture(key, bitmap.width(),
1760 bitmap.height(), sampler);
1761 if (NULL == entry.texture()) {
1762 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
junov@google.com4ee7ae52011-06-30 17:30:49 +00001763 bitmap);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001764 }
junov@google.com4ee7ae52011-06-30 17:30:49 +00001765 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001766 entry = sk_gr_create_bitmap_texture(ctx, gUNCACHED_KEY, sampler, bitmap);
junov@google.com4ee7ae52011-06-30 17:30:49 +00001767 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001768 if (NULL == entry.texture()) {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001769 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1770 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001771 }
1772 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001773 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001774}
1775
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001776void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1777 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001778}
1779
bsalomon@google.come97f0852011-06-17 13:10:25 +00001780SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1781 int width, int height,
1782 bool isOpaque,
1783 Usage usage) {
1784 return SkNEW_ARGS(SkGpuDevice,(this->context(), config,
1785 width, height, usage));
1786}
1787