blob: 874ed8e422cb5e322555f8d10db31d9aaf9e7dc6 [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.com79bd8772011-07-18 15:34:08 +0000765 // until we can both stroke and fill rectangles
766 // with large enough miter limit...
767 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
768 usePath = true;
769 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000770
771 if (usePath) {
772 SkPath path;
773 path.addRect(rect);
774 this->drawPath(draw, path, paint, NULL, true);
775 return;
776 }
777
778 GrPaint grPaint;
779 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000780 if (!this->skPaint2GrPaintShader(paint,
781 &act,
782 *draw.fMatrix,
783 &grPaint,
784 true)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000785 return;
786 }
reed@google.com20efde72011-05-09 17:00:02 +0000787 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000788}
789
reed@google.com69302852011-02-16 18:08:07 +0000790#include "SkMaskFilter.h"
791#include "SkBounder.h"
792
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000793static GrPathFill skToGrFillType(SkPath::FillType fillType) {
794 switch (fillType) {
795 case SkPath::kWinding_FillType:
796 return kWinding_PathFill;
797 case SkPath::kEvenOdd_FillType:
798 return kEvenOdd_PathFill;
799 case SkPath::kInverseWinding_FillType:
800 return kInverseWinding_PathFill;
801 case SkPath::kInverseEvenOdd_FillType:
802 return kInverseEvenOdd_PathFill;
803 default:
804 SkDebugf("Unsupported path fill type\n");
805 return kHairLine_PathFill;
806 }
807}
808
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000809static void buildKernel(float sigma, float* kernel, int kernelWidth) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000810 int halfWidth = (kernelWidth - 1) / 2;
811 float sum = 0.0f;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000812 float denom = 1.0f / (2.0f * sigma * sigma);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000813 for (int i = 0; i < kernelWidth; ++i) {
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000814 float x = static_cast<float>(i - halfWidth);
815 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
816 // is dropped here, since we renormalize the kernel below.
817 kernel[i] = sk_float_exp(- x * x * denom);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000818 sum += kernel[i];
819 }
820 // Normalize the kernel
821 float scale = 1.0f / sum;
822 for (int i = 0; i < kernelWidth; ++i)
823 kernel[i] *= scale;
824}
825
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000826static void scaleRect(SkRect* rect, float scale) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000827 rect->fLeft *= scale;
828 rect->fTop *= scale;
829 rect->fRight *= scale;
830 rect->fBottom *= scale;
831}
832
833static bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
834 SkMaskFilter* filter, const SkMatrix& matrix,
835 const SkRegion& clip, SkBounder* bounder,
836 GrPaint* grp) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000837#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000838 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000839#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000840 SkMaskFilter::BlurInfo info;
841 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000842 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000843 return false;
844 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000845 SkScalar radius = info.fIgnoreTransform ? info.fRadius
846 : matrix.mapRadius(info.fRadius);
847 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000848 if (radius <= 0) {
849 return false;
850 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000851 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000852 SkRect srcRect = path.getBounds();
853
854 int scaleFactor = 1;
855
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000856 while (sigma > MAX_BLUR_SIGMA) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000857 scaleFactor *= 2;
858 sigma *= 0.5f;
859 }
senorblanco@chromium.org4a947d22011-07-18 21:48:35 +0000860 int halfWidth = static_cast<int>(ceilf(sigma * 3.0f));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000861 int kernelWidth = halfWidth * 2 + 1;
862
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000863 float invScale = 1.0f / scaleFactor;
864 scaleRect(&srcRect, invScale);
865 srcRect.roundOut();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000866 srcRect.inset(-halfWidth, -halfWidth);
867
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000868 SkRect clipBounds;
869 clipBounds.set(clip.getBounds());
870 scaleRect(&clipBounds, invScale);
871 clipBounds.roundOut();
872 clipBounds.inset(-halfWidth, -halfWidth);
873
874 srcRect.intersect(clipBounds);
875
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000876 scaleRect(&srcRect, scaleFactor);
877 SkRect finalRect = srcRect;
878
879 SkIRect finalIRect;
880 finalRect.roundOut(&finalIRect);
881 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000882 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000883 }
884 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000885 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000886 }
887 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
888 srcRect.offset(-srcRect.fLeft, -srcRect.fTop);
889 const GrTextureDesc desc = {
890 kRenderTarget_GrTextureFlagBit,
891 kNone_GrAALevel,
892 srcRect.width(),
893 srcRect.height(),
894 // We actually only need A8, but it often isn't supported as a
895 // render target
896 kRGBA_8888_GrPixelConfig
897 };
898
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000899 GrAutoScratchTexture srcEntry(context, desc);
900 GrAutoScratchTexture dstEntry(context, desc);
901 if (NULL == srcEntry.texture() || NULL == dstEntry.texture()) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000902 return false;
903 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000904 GrTexture* srcTexture = srcEntry.texture();
905 GrTexture* dstTexture = dstEntry.texture();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000906 if (NULL == srcTexture || NULL == dstTexture) {
907 return false;
908 }
909 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000910 // Once this code moves into GrContext, this should be changed to use
911 // an AutoClipRestore.
912 GrClip oldClip = context->getClip();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000913 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000914 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000915 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000916 GrPaint tempPaint;
917 tempPaint.reset();
918
919 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000920 tempPaint.fAntiAlias = grp->fAntiAlias;
921 if (tempPaint.fAntiAlias) {
922 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
923 // blend coeff of zero requires dual source blending support in order
924 // to properly blend partially covered pixels. This means the AA
925 // code path may not be taken. So we use a dst blend coeff of ISA. We
926 // could special case AA draws to a dst surface with known alpha=0 to
927 // use a zero dst coeff when dual source blending isn't available.
928 tempPaint.fSrcBlendCoeff = kOne_BlendCoeff;
929 tempPaint.fDstBlendCoeff = kISC_BlendCoeff;
930 }
931 // Draw hard shadow to dstTexture with path topleft at origin 0,0.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000932 context->drawPath(tempPaint, path, skToGrFillType(path.getFillType()), &offset);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000933 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000934
935 GrMatrix sampleM;
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000936 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000937 GrPaint paint;
938 paint.reset();
939 paint.getTextureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
940 paint.getTextureSampler(0)->setMatrix(sampleM);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000941 GrAutoScratchTexture origEntry;
942
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000943 if (blurType != SkMaskFilter::kNormal_BlurType) {
944 // Stash away a copy of the unblurred image.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000945 origEntry.set(context, desc);
946 if (NULL == origEntry.texture()) {
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000947 return false;
948 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000949 context->setRenderTarget(origEntry.texture()->asRenderTarget());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000950 paint.setTexture(0, srcTexture);
951 context->drawRect(paint, srcRect);
952 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000953 for (int i = 1; i < scaleFactor; i *= 2) {
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000954 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
955 paint.getTextureSampler(0)->setMatrix(sampleM);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000956 context->setRenderTarget(dstTexture->asRenderTarget());
957 SkRect dstRect(srcRect);
958 scaleRect(&dstRect, 0.5f);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000959 paint.setTexture(0, srcTexture);
960 context->drawRectToRect(paint, dstRect, srcRect);
961 srcRect = dstRect;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000962 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000963 }
964
965 SkAutoTMalloc<float> kernelStorage(kernelWidth);
966 float* kernel = kernelStorage.get();
967 buildKernel(sigma, kernel, kernelWidth);
968
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000969 // Clear out a halfWidth to the right of the srcRect to prevent the
970 // X convolution from reading garbage.
971 SkIRect clearRect = SkIRect::MakeXYWH(
972 srcRect.fRight, srcRect.fTop, halfWidth, srcRect.height());
973 context->clear(&clearRect, 0x0);
974
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000975 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000976 context->convolveInX(srcTexture, srcRect, kernel, kernelWidth);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000977 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000978
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000979 // Clear out a halfWidth below the srcRect to prevent the Y
980 // convolution from reading garbage.
981 clearRect = SkIRect::MakeXYWH(
982 srcRect.fLeft, srcRect.fBottom, srcRect.width(), halfWidth);
983 context->clear(&clearRect, 0x0);
984
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000985 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000986 context->convolveInY(srcTexture, srcRect, kernel, kernelWidth);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000987 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000988
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000989 // Clear one pixel to the right and below, to accommodate bilinear
990 // upsampling.
991 clearRect = SkIRect::MakeXYWH(
992 srcRect.fLeft, srcRect.fBottom, srcRect.width() + 1, 1);
993 context->clear(&clearRect, 0x0);
994 clearRect = SkIRect::MakeXYWH(
995 srcRect.fRight, srcRect.fTop, 1, srcRect.height());
996 context->clear(&clearRect, 0x0);
997
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000998 if (scaleFactor > 1) {
999 // FIXME: This should be mitchell, not bilinear.
1000 paint.getTextureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +00001001 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001002 paint.getTextureSampler(0)->setMatrix(sampleM);
1003 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001004 paint.setTexture(0, srcTexture);
1005 SkRect dstRect(srcRect);
1006 scaleRect(&dstRect, scaleFactor);
1007 context->drawRectToRect(paint, dstRect, srcRect);
1008 srcRect = dstRect;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001009 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001010 }
1011
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001012 if (blurType != SkMaskFilter::kNormal_BlurType) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001013 GrTexture* origTexture = origEntry.texture();
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001014 paint.getTextureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +00001015 sampleM.setIDiv(origTexture->width(), origTexture->height());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001016 paint.getTextureSampler(0)->setMatrix(sampleM);
1017 // Blend origTexture over srcTexture.
1018 context->setRenderTarget(srcTexture->asRenderTarget());
1019 paint.setTexture(0, origTexture);
1020 if (SkMaskFilter::kInner_BlurType == blurType) {
1021 // inner: dst = dst * src
1022 paint.fSrcBlendCoeff = kDC_BlendCoeff;
1023 paint.fDstBlendCoeff = kZero_BlendCoeff;
1024 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
1025 // solid: dst = src + dst - src * dst
1026 // = (1 - dst) * src + 1 * dst
1027 paint.fSrcBlendCoeff = kIDC_BlendCoeff;
1028 paint.fDstBlendCoeff = kOne_BlendCoeff;
1029 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
1030 // outer: dst = dst * (1 - src)
1031 // = 0 * src + (1 - src) * dst
1032 paint.fSrcBlendCoeff = kZero_BlendCoeff;
1033 paint.fDstBlendCoeff = kISC_BlendCoeff;
1034 }
1035 context->drawRect(paint, srcRect);
1036 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001037 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +00001038 context->setClip(oldClip);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001039
1040 if (grp->hasTextureOrMask()) {
1041 GrMatrix inverse;
1042 if (!matrix.invert(&inverse)) {
1043 return false;
1044 }
1045 grp->preConcatActiveSamplerMatrices(inverse);
1046 }
1047
1048 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
1049 // we assume the last mask index is available for use
1050 GrAssert(NULL == grp->getMask(MASK_IDX));
1051 grp->setMask(MASK_IDX, srcTexture);
1052 grp->getMaskSampler(MASK_IDX)->setClampNoFilter();
1053
1054 GrMatrix m;
1055 m.setTranslate(-finalRect.fLeft, -finalRect.fTop);
1056 m.postIDiv(srcTexture->width(), srcTexture->height());
1057 grp->getMaskSampler(MASK_IDX)->setMatrix(m);
1058 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001059 return true;
1060}
1061
reed@google.com69302852011-02-16 18:08:07 +00001062static bool drawWithMaskFilter(GrContext* context, const SkPath& path,
1063 SkMaskFilter* filter, const SkMatrix& matrix,
1064 const SkRegion& clip, SkBounder* bounder,
1065 GrPaint* grp) {
1066 SkMask srcM, dstM;
1067
1068 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
1069 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
1070 return false;
1071 }
1072
1073 SkAutoMaskImage autoSrc(&srcM, false);
1074
1075 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
1076 return false;
1077 }
1078 // this will free-up dstM when we're done (allocated in filterMask())
1079 SkAutoMaskImage autoDst(&dstM, false);
1080
1081 if (clip.quickReject(dstM.fBounds)) {
1082 return false;
1083 }
1084 if (bounder && !bounder->doIRect(dstM.fBounds)) {
1085 return false;
1086 }
1087
1088 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
1089 // the current clip (and identity matrix) and grpaint settings
1090
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001091 // used to compute inverse view, if necessary
1092 GrMatrix ivm = context->getMatrix();
1093
reed@google.com0c219b62011-02-16 21:31:18 +00001094 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +00001095
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001096 const GrTextureDesc desc = {
1097 kNone_GrTextureFlags,
1098 kNone_GrAALevel,
reed@google.com69302852011-02-16 18:08:07 +00001099 dstM.fBounds.width(),
1100 dstM.fBounds.height(),
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001101 kAlpha_8_GrPixelConfig
reed@google.com69302852011-02-16 18:08:07 +00001102 };
1103
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001104 GrAutoScratchTexture ast(context, desc);
1105 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001106
reed@google.com69302852011-02-16 18:08:07 +00001107 if (NULL == texture) {
1108 return false;
1109 }
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001110 texture->uploadTextureData(0, 0, desc.fWidth, desc.fHeight,
1111 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +00001112
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001113 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
1114 grp->preConcatActiveSamplerMatrices(ivm);
1115 }
1116
1117 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
1118 // we assume the last mask index is available for use
1119 GrAssert(NULL == grp->getMask(MASK_IDX));
1120 grp->setMask(MASK_IDX, texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001121 grp->getMaskSampler(MASK_IDX)->setClampNoFilter();
reed@google.com69302852011-02-16 18:08:07 +00001122
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001123 GrRect d;
1124 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +00001125 GrIntToScalar(dstM.fBounds.fTop),
1126 GrIntToScalar(dstM.fBounds.fRight),
1127 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001128
1129 GrMatrix m;
1130 m.setTranslate(-dstM.fBounds.fLeft, -dstM.fBounds.fTop);
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001131 m.postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001132 grp->getMaskSampler(MASK_IDX)->setMatrix(m);
1133
1134 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +00001135 return true;
1136}
reed@google.com69302852011-02-16 18:08:07 +00001137
reed@google.com0c219b62011-02-16 21:31:18 +00001138void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
reed@google.comac10a2d2010-12-22 21:39:39 +00001139 const SkPaint& paint, const SkMatrix* prePathMatrix,
1140 bool pathIsMutable) {
1141 CHECK_SHOULD_DRAW(draw);
1142
bsalomon@google.com5782d712011-01-21 21:03:59 +00001143 GrPaint grPaint;
1144 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +00001145 if (!this->skPaint2GrPaintShader(paint,
1146 &act,
1147 *draw.fMatrix,
1148 &grPaint,
1149 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001150 return;
1151 }
1152
reed@google.com0c219b62011-02-16 21:31:18 +00001153 // BEGIN lift from SkDraw::drawPath()
1154
1155 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1156 bool doFill = true;
1157 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001158
1159 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001160 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001161
reed@google.come3445642011-02-16 23:20:39 +00001162 if (!pathIsMutable) {
1163 result = &tmpPath;
1164 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001165 }
reed@google.come3445642011-02-16 23:20:39 +00001166 // should I push prePathMatrix on our MV stack temporarily, instead
1167 // of applying it here? See SkDraw.cpp
1168 pathPtr->transform(*prePathMatrix, result);
1169 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001170 }
reed@google.com0c219b62011-02-16 21:31:18 +00001171 // at this point we're done with prePathMatrix
1172 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001173
bsalomon@google.com04de7822011-03-25 18:04:43 +00001174 // This "if" is not part of the SkDraw::drawPath() lift.
1175 // When we get a 1.0 wide stroke we hairline stroke it instead of creating
1176 // a new stroked-path. This is motivated by canvas2D sites that draw
1177 // lines as 1.0 wide stroked paths. We can consider doing an alpha-modulated-
1178 // hairline for width < 1.0 when AA is enabled.
1179 static const int gMatrixMask = ~(SkMatrix::kIdentity_Mask |
1180 SkMatrix::kTranslate_Mask);
1181 if (!paint.getPathEffect() &&
1182 SkPaint::kStroke_Style == paint.getStyle() &&
1183 !(draw.fMatrix->getType() & gMatrixMask) &&
1184 SK_Scalar1 == paint.getStrokeWidth()) {
1185 doFill = false;
1186 }
1187
1188 if (doFill && (paint.getPathEffect() ||
1189 paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.com0c219b62011-02-16 21:31:18 +00001190 doFill = paint.getFillPath(*pathPtr, &tmpPath);
1191 pathPtr = &tmpPath;
1192 }
1193
1194 // END lift from SkDraw::drawPath()
1195
reed@google.com69302852011-02-16 18:08:07 +00001196 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001197 // avoid possibly allocating a new path in transform if we can
1198 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1199
1200 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001201 pathPtr->transform(*draw.fMatrix, devPathPtr);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001202 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
1203 *draw.fMatrix, *draw.fClip, draw.fBounder,
1204 &grPaint)) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001205 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
1206 *draw.fMatrix, *draw.fClip, draw.fBounder,
1207 &grPaint);
1208 }
reed@google.com69302852011-02-16 18:08:07 +00001209 return;
1210 }
reed@google.com69302852011-02-16 18:08:07 +00001211
bsalomon@google.comffca4002011-02-22 20:34:01 +00001212 GrPathFill fill = kHairLine_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001213
reed@google.com0c219b62011-02-16 21:31:18 +00001214 if (doFill) {
1215 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001216 case SkPath::kWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001217 fill = kWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001218 break;
1219 case SkPath::kEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001220 fill = kEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001221 break;
1222 case SkPath::kInverseWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001223 fill = kInverseWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001224 break;
1225 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001226 fill = kInverseEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001227 break;
1228 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001229 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001230 return;
1231 }
1232 }
1233
reed@google.com07f3ee12011-05-16 17:21:57 +00001234 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001235}
1236
reed@google.comac10a2d2010-12-22 21:39:39 +00001237void SkGpuDevice::drawBitmap(const SkDraw& draw,
1238 const SkBitmap& bitmap,
1239 const SkIRect* srcRectPtr,
1240 const SkMatrix& m,
1241 const SkPaint& paint) {
1242 CHECK_SHOULD_DRAW(draw);
1243
1244 SkIRect srcRect;
1245 if (NULL == srcRectPtr) {
1246 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1247 } else {
1248 srcRect = *srcRectPtr;
1249 }
1250
junov@google.comd935cfb2011-06-27 20:48:23 +00001251 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001252 // Convert the bitmap to a shader so that the rect can be drawn
1253 // through drawRect, which supports mask filters.
1254 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001255 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001256 if (srcRectPtr) {
1257 if (!bitmap.extractSubset(&tmp, srcRect)) {
1258 return; // extraction failed
1259 }
1260 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001261 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001262 }
1263 SkPaint paintWithTexture(paint);
1264 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1265 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001266 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001267 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001268
junov@google.com1d329782011-07-28 20:10:09 +00001269 // Transform 'm' needs to be concatenated to the draw matrix,
1270 // rather than transforming the primitive directly, so that 'm' will
1271 // also affect the behavior of the mask filter.
1272 SkMatrix drawMatrix;
1273 drawMatrix.setConcat(*draw.fMatrix, m);
1274 SkDraw transformedDraw(draw);
1275 transformedDraw.fMatrix = &drawMatrix;
1276
1277 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1278
junov@google.comd935cfb2011-06-27 20:48:23 +00001279 return;
1280 }
1281
bsalomon@google.com5782d712011-01-21 21:03:59 +00001282 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001283 if (!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001284 return;
1285 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001286 GrSamplerState* sampler = grPaint.getTextureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001287 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001288 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001289 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001290 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001291 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001292
bsalomon@google.com91958362011-06-13 17:58:13 +00001293 const int maxTextureSize = fContext->getMaxTextureSize();
1294 if (bitmap.getTexture() || (bitmap.width() <= maxTextureSize &&
1295 bitmap.height() <= maxTextureSize)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001296 // take the fast case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001297 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001298 return;
1299 }
1300
1301 // undo the translate done by SkCanvas
1302 int DX = SkMax32(0, srcRect.fLeft);
1303 int DY = SkMax32(0, srcRect.fTop);
1304 // compute clip bounds in local coordinates
1305 SkIRect clipRect;
1306 {
1307 SkRect r;
1308 r.set(draw.fClip->getBounds());
1309 SkMatrix matrix, inverse;
1310 matrix.setConcat(*draw.fMatrix, m);
1311 if (!matrix.invert(&inverse)) {
1312 return;
1313 }
1314 inverse.mapRect(&r);
1315 r.roundOut(&clipRect);
1316 // apply the canvas' translate to our local clip
1317 clipRect.offset(DX, DY);
1318 }
1319
bsalomon@google.com91958362011-06-13 17:58:13 +00001320 int nx = bitmap.width() / maxTextureSize;
1321 int ny = bitmap.height() / maxTextureSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001322 for (int x = 0; x <= nx; x++) {
1323 for (int y = 0; y <= ny; y++) {
1324 SkIRect tileR;
bsalomon@google.com91958362011-06-13 17:58:13 +00001325 tileR.set(x * maxTextureSize, y * maxTextureSize,
1326 (x + 1) * maxTextureSize, (y + 1) * maxTextureSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001327 if (!SkIRect::Intersects(tileR, clipRect)) {
1328 continue;
1329 }
1330
1331 SkIRect srcR = tileR;
1332 if (!srcR.intersect(srcRect)) {
1333 continue;
1334 }
1335
1336 SkBitmap tmpB;
1337 if (bitmap.extractSubset(&tmpB, tileR)) {
1338 // now offset it to make it "local" to our tmp bitmap
1339 srcR.offset(-tileR.fLeft, -tileR.fTop);
1340
1341 SkMatrix tmpM(m);
1342 {
1343 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1344 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1345 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1346 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001347 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001348 }
1349 }
1350 }
1351}
1352
1353/*
1354 * This is called by drawBitmap(), which has to handle images that may be too
1355 * large to be represented by a single texture.
1356 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001357 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1358 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001359 */
1360void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1361 const SkBitmap& bitmap,
1362 const SkIRect& srcRect,
1363 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001364 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001365 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1366 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001367
reed@google.com9c49bc32011-07-07 13:42:37 +00001368 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001369 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001370 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001371 return;
1372 }
1373
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001374 GrSamplerState* sampler = grPaint->getTextureSampler(kBitmapTextureIdx);
1375
1376 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1377 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
1378 sampler->setSampleMode(GrSamplerState::kNormal_SampleMode);
1379 sampler->setMatrix(GrMatrix::I());
reed@google.comac10a2d2010-12-22 21:39:39 +00001380
1381 GrTexture* texture;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001382 SkAutoCachedTexture act(this, bitmap, *sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001383 if (NULL == texture) {
1384 return;
1385 }
1386
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001387 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001388
reed@google.com20efde72011-05-09 17:00:02 +00001389 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1390 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001391 GrRect paintRect;
junov@google.com6acc9b32011-05-16 18:32:07 +00001392 paintRect.setLTRB(GrFixedToScalar((srcRect.fLeft << 16) / bitmap.width()),
1393 GrFixedToScalar((srcRect.fTop << 16) / bitmap.height()),
1394 GrFixedToScalar((srcRect.fRight << 16) / bitmap.width()),
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001395 GrFixedToScalar((srcRect.fBottom << 16) / bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001396
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001397 if (GrSamplerState::kNearest_Filter != sampler->getFilter() &&
junov@google.com6acc9b32011-05-16 18:32:07 +00001398 (srcRect.width() < bitmap.width() ||
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001399 srcRect.height() < bitmap.height())) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001400 // If drawing a subrect of the bitmap and filtering is enabled,
1401 // use a constrained texture domain to avoid color bleeding
1402 GrScalar left, top, right, bottom;
1403 if (srcRect.width() > 1) {
1404 GrScalar border = GR_ScalarHalf / bitmap.width();
1405 left = paintRect.left() + border;
1406 right = paintRect.right() - border;
1407 } else {
1408 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1409 }
1410 if (srcRect.height() > 1) {
1411 GrScalar border = GR_ScalarHalf / bitmap.height();
1412 top = paintRect.top() + border;
1413 bottom = paintRect.bottom() - border;
1414 } else {
1415 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1416 }
1417 GrRect textureDomain;
1418 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001419 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001420 }
1421
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001422 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001423}
1424
1425void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1426 int left, int top, const SkPaint& paint) {
1427 CHECK_SHOULD_DRAW(draw);
1428
1429 SkAutoLockPixels alp(bitmap);
1430 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1431 return;
1432 }
1433
bsalomon@google.com5782d712011-01-21 21:03:59 +00001434 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001435 if(!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001436 return;
1437 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001438
bsalomon@google.com5782d712011-01-21 21:03:59 +00001439 GrAutoMatrix avm(fContext, GrMatrix::I());
1440
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001441 GrSamplerState* sampler = grPaint.getTextureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001442
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001443 GrTexture* texture;
1444 sampler->setClampNoFilter();
1445 SkAutoCachedTexture act(this, bitmap, *sampler, &texture);
1446
1447 grPaint.setTexture(kBitmapTextureIdx, texture);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001448
bsalomon@google.com5782d712011-01-21 21:03:59 +00001449 fContext->drawRectToRect(grPaint,
reed@google.com20efde72011-05-09 17:00:02 +00001450 GrRect::MakeXYWH(GrIntToScalar(left),
1451 GrIntToScalar(top),
1452 GrIntToScalar(bitmap.width()),
1453 GrIntToScalar(bitmap.height())),
1454 GrRect::MakeWH(GR_Scalar1, GR_Scalar1));
reed@google.comac10a2d2010-12-22 21:39:39 +00001455}
1456
1457void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* dev,
1458 int x, int y, const SkPaint& paint) {
1459 CHECK_SHOULD_DRAW(draw);
1460
bsalomon@google.com5782d712011-01-21 21:03:59 +00001461 GrPaint grPaint;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001462 if (!((SkGpuDevice*)dev)->bindDeviceAsTexture(&grPaint) ||
Scroggod757df22011-05-16 13:11:16 +00001463 !this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001464 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001465 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001466
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001467 GrTexture* devTex = grPaint.getTexture(0);
1468 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001469
1470 const SkBitmap& bm = dev->accessBitmap(false);
1471 int w = bm.width();
1472 int h = bm.height();
1473
1474 GrAutoMatrix avm(fContext, GrMatrix::I());
1475
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001476 grPaint.getTextureSampler(kBitmapTextureIdx)->setClampNoFilter();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001477
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001478 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1479 GrIntToScalar(y),
1480 GrIntToScalar(w),
1481 GrIntToScalar(h));
1482 // The device being drawn may not fill up its texture (saveLayer uses
1483 // the approximate ).
1484 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1485 GR_Scalar1 * h / devTex->height());
1486
1487 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001488}
1489
1490///////////////////////////////////////////////////////////////////////////////
1491
1492// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001493static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1494 kTriangles_PrimitiveType,
1495 kTriangleStrip_PrimitiveType,
1496 kTriangleFan_PrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001497};
1498
1499void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1500 int vertexCount, const SkPoint vertices[],
1501 const SkPoint texs[], const SkColor colors[],
1502 SkXfermode* xmode,
1503 const uint16_t indices[], int indexCount,
1504 const SkPaint& paint) {
1505 CHECK_SHOULD_DRAW(draw);
1506
bsalomon@google.com5782d712011-01-21 21:03:59 +00001507 GrPaint grPaint;
1508 SkAutoCachedTexture act;
1509 // we ignore the shader if texs is null.
1510 if (NULL == texs) {
Scroggod757df22011-05-16 13:11:16 +00001511 if (!this->skPaint2GrPaintNoShader(paint,
1512 false,
1513 &grPaint,
1514 NULL == colors)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001515 return;
1516 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001517 } else {
reed@google.com1a2e8d22011-01-21 22:08:29 +00001518 if (!this->skPaint2GrPaintShader(paint, &act,
1519 *draw.fMatrix,
Scroggod757df22011-05-16 13:11:16 +00001520 &grPaint,
1521 NULL == colors)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001522 return;
1523 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001524 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001525
1526 if (NULL != xmode && NULL != texs && NULL != colors) {
1527 SkXfermode::Mode mode;
1528 if (!SkXfermode::IsMode(xmode, &mode) ||
1529 SkXfermode::kMultiply_Mode != mode) {
1530 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1531#if 0
1532 return
1533#endif
1534 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001535 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001536
1537#if SK_SCALAR_IS_GR_SCALAR
1538 // even if GrColor and SkColor byte offsets match we need
1539 // to perform pre-multiply.
1540 if (NULL == colors) {
1541 fContext->drawVertices(grPaint,
1542 gVertexMode2PrimitiveType[vmode],
1543 vertexCount,
1544 (GrPoint*) vertices,
1545 (GrPoint*) texs,
1546 NULL,
1547 indices,
1548 indexCount);
1549 } else
1550#endif
1551 {
1552 SkTexCoordSource texSrc(texs);
1553 SkColorSource colSrc(colors);
1554 SkIndexSource idxSrc(indices, indexCount);
1555
1556 fContext->drawCustomVertices(grPaint,
1557 gVertexMode2PrimitiveType[vmode],
1558 SkPositionSource(vertices, vertexCount),
1559 (NULL == texs) ? NULL : &texSrc,
1560 (NULL == colors) ? NULL : &colSrc,
1561 (NULL == indices) ? NULL : &idxSrc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001562 }
1563}
1564
1565///////////////////////////////////////////////////////////////////////////////
1566
1567static void GlyphCacheAuxProc(void* data) {
1568 delete (GrFontScaler*)data;
1569}
1570
1571static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1572 void* auxData;
1573 GrFontScaler* scaler = NULL;
1574 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1575 scaler = (GrFontScaler*)auxData;
1576 }
1577 if (NULL == scaler) {
1578 scaler = new SkGrFontScaler(cache);
1579 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1580 }
1581 return scaler;
1582}
1583
1584static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1585 SkFixed fx, SkFixed fy,
1586 const SkGlyph& glyph) {
1587 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1588
1589 GrSkDrawProcs* procs = (GrSkDrawProcs*)state.fDraw->fProcs;
1590
1591 if (NULL == procs->fFontScaler) {
1592 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1593 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001594
1595 /*
reed@google.com3b139f52011-06-07 17:56:25 +00001596 * What should we do with fy? (assuming horizontal/latin text)
reed@google.com39ce0ac2011-04-08 15:42:19 +00001597 *
reed@google.com3b139f52011-06-07 17:56:25 +00001598 * The raster code calls SkFixedFloorToFixed on it, as it does with fx.
1599 * It calls that rather than round, because our caller has already added
1600 * SK_FixedHalf, so that calling floor gives us the rounded integer.
1601 *
1602 * Test code between raster and gpu (they should draw the same)
1603 *
1604 * canvas->drawText("Hamburgefons", 12, 0, 16.5f, paint);
1605 *
1606 * Perhaps we should only perform this integralization if there is no
1607 * fExtMatrix...
reed@google.com39ce0ac2011-04-08 15:42:19 +00001608 */
reed@google.com3b139f52011-06-07 17:56:25 +00001609 fy = SkFixedFloorToFixed(fy);
1610
reed@google.comac10a2d2010-12-22 21:39:39 +00001611 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), fx, 0),
reed@google.com3b139f52011-06-07 17:56:25 +00001612 SkFixedFloorToFixed(fx), fy,
reed@google.comac10a2d2010-12-22 21:39:39 +00001613 procs->fFontScaler);
1614}
1615
bsalomon@google.com5782d712011-01-21 21:03:59 +00001616SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001617
1618 // deferred allocation
1619 if (NULL == fDrawProcs) {
1620 fDrawProcs = new GrSkDrawProcs;
1621 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1622 fDrawProcs->fContext = fContext;
1623 }
1624
1625 // init our (and GL's) state
1626 fDrawProcs->fTextContext = context;
1627 fDrawProcs->fFontScaler = NULL;
1628 return fDrawProcs;
1629}
1630
1631void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1632 size_t byteLength, SkScalar x, SkScalar y,
1633 const SkPaint& paint) {
1634 CHECK_SHOULD_DRAW(draw);
1635
1636 if (draw.fMatrix->getType() & SkMatrix::kPerspective_Mask) {
1637 // this guy will just call our drawPath()
1638 draw.drawText((const char*)text, byteLength, x, y, paint);
1639 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001640 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001641
1642 GrPaint grPaint;
1643 SkAutoCachedTexture act;
1644
Scroggod757df22011-05-16 13:11:16 +00001645 if (!this->skPaint2GrPaintShader(paint,
1646 &act,
1647 *draw.fMatrix,
1648 &grPaint,
1649 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001650 return;
1651 }
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001652 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001653 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001654 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1655 }
1656}
1657
1658void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1659 size_t byteLength, const SkScalar pos[],
1660 SkScalar constY, int scalarsPerPos,
1661 const SkPaint& paint) {
1662 CHECK_SHOULD_DRAW(draw);
1663
1664 if (draw.fMatrix->getType() & SkMatrix::kPerspective_Mask) {
1665 // this guy will just call our drawPath()
1666 draw.drawPosText((const char*)text, byteLength, pos, constY,
1667 scalarsPerPos, paint);
1668 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001669 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001670
1671 GrPaint grPaint;
1672 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +00001673 if (!this->skPaint2GrPaintShader(paint,
1674 &act,
1675 *draw.fMatrix,
1676 &grPaint,
1677 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001678 return;
1679 }
1680
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001681 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001682 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001683 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1684 scalarsPerPos, paint);
1685 }
1686}
1687
1688void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1689 size_t len, const SkPath& path,
1690 const SkMatrix* m, const SkPaint& paint) {
1691 CHECK_SHOULD_DRAW(draw);
1692
1693 SkASSERT(draw.fDevice == this);
1694 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1695}
1696
1697///////////////////////////////////////////////////////////////////////////////
1698
reed@google.comf67e4cf2011-03-15 20:56:58 +00001699bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1700 if (!paint.isLCDRenderText()) {
1701 // we're cool with the paint as is
1702 return false;
1703 }
1704
1705 if (paint.getShader() ||
1706 paint.getXfermode() || // unless its srcover
1707 paint.getMaskFilter() ||
1708 paint.getRasterizer() ||
1709 paint.getColorFilter() ||
1710 paint.getPathEffect() ||
1711 paint.isFakeBoldText() ||
1712 paint.getStyle() != SkPaint::kFill_Style) {
1713 // turn off lcd
1714 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1715 flags->fHinting = paint.getHinting();
1716 return true;
1717 }
1718 // we're cool with the paint as is
1719 return false;
1720}
1721
1722///////////////////////////////////////////////////////////////////////////////
1723
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001724SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(const SkBitmap& bitmap,
1725 const GrSamplerState& sampler,
1726 TexType type) {
1727 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001728 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001729
bsalomon@google.come97f0852011-06-17 13:10:25 +00001730 if (kBitmap_TexType != type) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001731 const GrTextureDesc desc = {
1732 kRenderTarget_GrTextureFlagBit,
1733 kNone_GrAALevel,
1734 bitmap.width(),
1735 bitmap.height(),
1736 SkGr::Bitmap2PixelConfig(bitmap)
1737 };
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001738 GrContext::ScratchTexMatch match;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001739 if (kSaveLayerDeviceRenderTarget_TexType == type) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001740 // we know layers will only be drawn through drawDevice.
1741 // drawDevice has been made to work with content embedded in a
1742 // larger texture so its okay to use the approximate version.
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001743 match = GrContext::kApprox_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001744 } else {
bsalomon@google.come97f0852011-06-17 13:10:25 +00001745 SkASSERT(kDeviceRenderTarget_TexType == type);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001746 match = GrContext::kExact_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001747 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001748 entry = ctx->lockScratchTexture(desc, match);
reed@google.comac10a2d2010-12-22 21:39:39 +00001749 } else {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001750 if (!bitmap.isVolatile()) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001751 GrContext::TextureKey key = bitmap.getGenerationID();
1752 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
junov@google.com4ee7ae52011-06-30 17:30:49 +00001753
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001754 entry = ctx->findAndLockTexture(key, bitmap.width(),
1755 bitmap.height(), sampler);
1756 if (NULL == entry.texture()) {
1757 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
junov@google.com4ee7ae52011-06-30 17:30:49 +00001758 bitmap);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001759 }
junov@google.com4ee7ae52011-06-30 17:30:49 +00001760 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001761 entry = sk_gr_create_bitmap_texture(ctx, gUNCACHED_KEY, sampler, bitmap);
junov@google.com4ee7ae52011-06-30 17:30:49 +00001762 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001763 if (NULL == entry.texture()) {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001764 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1765 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001766 }
1767 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001768 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001769}
1770
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001771void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1772 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001773}
1774
bsalomon@google.come97f0852011-06-17 13:10:25 +00001775SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1776 int width, int height,
1777 bool isOpaque,
1778 Usage usage) {
1779 return SkNEW_ARGS(SkGpuDevice,(this->context(), config,
1780 width, height, usage));
1781}
1782