blob: c3754dcada50a82694fcdcdf3832d3e9c7d595d6 [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) {
133 if (SkGpuDevice::Current3DApiRenderTarget() == renderTarget) {
134 renderTarget = context->createRenderTargetFrom3DApiState();
135 }
136 GrTexture* texture = renderTarget->asTexture();
137 GrPixelConfig config = texture ? texture->config() : kRGBA_8888_GrPixelConfig;
138
139 bool isOpaque;
140 SkBitmap bitmap;
141 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
142 renderTarget->width(), renderTarget->height());
143 bitmap.setIsOpaque(isOpaque);
144 return bitmap;
145}
146
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000147SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
148: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
149 this->initFromRenderTarget(context, texture->asRenderTarget());
150}
151
reed@google.comaf951c92011-06-16 19:10:39 +0000152SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
153: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000154 this->initFromRenderTarget(context, renderTarget);
155}
156
157void SkGpuDevice::initFromRenderTarget(GrContext* context,
158 GrRenderTarget* renderTarget) {
reed@google.comaf951c92011-06-16 19:10:39 +0000159 fNeedPrepareRenderTarget = false;
160 fDrawProcs = NULL;
161
162 fContext = context;
163 fContext->ref();
164
reed@google.comaf951c92011-06-16 19:10:39 +0000165 fTexture = NULL;
166 fRenderTarget = NULL;
167 fNeedClear = false;
168
169 if (Current3DApiRenderTarget() == renderTarget) {
170 fRenderTarget = fContext->createRenderTargetFrom3DApiState();
171 } else {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000172 GrAssert(NULL != renderTarget);
reed@google.comaf951c92011-06-16 19:10:39 +0000173 fRenderTarget = renderTarget;
174 fRenderTarget->ref();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000175 // if this RT is also a texture, hold a ref on it
176 fTexture = fRenderTarget->asTexture();
177 SkSafeRef(fTexture);
reed@google.comaf951c92011-06-16 19:10:39 +0000178 }
179
180 SkGrRenderTargetPixelRef* pr = new SkGrRenderTargetPixelRef(fRenderTarget);
181 this->setPixelRef(pr, 0)->unref();
182}
183
184SkGpuDevice::SkGpuDevice(GrContext* context, SkBitmap::Config config, int width,
bsalomon@google.come97f0852011-06-17 13:10:25 +0000185 int height, Usage usage)
reed@google.comaf951c92011-06-16 19:10:39 +0000186: SkDevice(config, width, height, false /*isOpaque*/) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000187 fNeedPrepareRenderTarget = false;
188 fDrawProcs = NULL;
189
reed@google.com7b201d22011-01-11 18:59:23 +0000190 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000191 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000192
reed@google.comac10a2d2010-12-22 21:39:39 +0000193 fTexture = NULL;
194 fRenderTarget = NULL;
195 fNeedClear = false;
196
reed@google.comaf951c92011-06-16 19:10:39 +0000197 if (config != SkBitmap::kRGB_565_Config) {
198 config = SkBitmap::kARGB_8888_Config;
199 }
200 SkBitmap bm;
201 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000202
203#if CACHE_LAYER_TEXTURES
bsalomon@google.come97f0852011-06-17 13:10:25 +0000204 TexType type = (kSaveLayer_Usage == usage) ?
205 kSaveLayerDeviceRenderTarget_TexType :
206 kDeviceRenderTarget_TexType;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000207 fCache = this->lockCachedTexture(bm, GrSamplerState::ClampNoFilter(), type);
208 fTexture = fCache.texture();
209 if (fTexture) {
reed@google.comaf951c92011-06-16 19:10:39 +0000210 SkASSERT(NULL != fTexture->asRenderTarget());
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000211 // hold a ref directly on fTexture (even though fCache has one) to match
212 // other constructor paths. Simplifies cleanup.
213 fTexture->ref();
reed@google.comaf951c92011-06-16 19:10:39 +0000214 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000215#else
reed@google.comaf951c92011-06-16 19:10:39 +0000216 const GrTextureDesc desc = {
217 kRenderTarget_GrTextureFlagBit,
218 kNone_GrAALevel,
219 width,
220 height,
221 SkGr::Bitmap2PixelConfig(bm)
222 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000223
reed@google.comaf951c92011-06-16 19:10:39 +0000224 fTexture = fContext->createUncachedTexture(desc, NULL, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000225#endif
reed@google.comaf951c92011-06-16 19:10:39 +0000226 if (NULL != fTexture) {
227 fRenderTarget = fTexture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000228 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000229
reed@google.comaf951c92011-06-16 19:10:39 +0000230 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000231
reed@google.comaf951c92011-06-16 19:10:39 +0000232 // we defer the actual clear until our gainFocus()
233 fNeedClear = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000234
reed@google.comaf951c92011-06-16 19:10:39 +0000235 // wrap the bitmap with a pixelref to expose our texture
236 SkGrTexturePixelRef* pr = new SkGrTexturePixelRef(fTexture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000237 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000238 } else {
239 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
240 width, height);
241 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000242 }
243}
244
245SkGpuDevice::~SkGpuDevice() {
246 if (fDrawProcs) {
247 delete fDrawProcs;
248 }
249
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000250 SkSafeUnref(fTexture);
251 SkSafeUnref(fRenderTarget);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000252 if (fCache.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000253 GrAssert(NULL != fTexture);
254 GrAssert(fRenderTarget == fTexture->asRenderTarget());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000255 fContext->unlockTexture(fCache);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000256 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000257 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000258}
259
reed@google.comac10a2d2010-12-22 21:39:39 +0000260///////////////////////////////////////////////////////////////////////////////
261
262void SkGpuDevice::makeRenderTargetCurrent() {
263 fContext->setRenderTarget(fRenderTarget);
264 fContext->flush(true);
265 fNeedPrepareRenderTarget = true;
266}
267
268///////////////////////////////////////////////////////////////////////////////
269
270bool SkGpuDevice::readPixels(const SkIRect& srcRect, SkBitmap* bitmap) {
271 SkIRect bounds;
272 bounds.set(0, 0, this->width(), this->height());
273 if (!bounds.intersect(srcRect)) {
274 return false;
275 }
276
277 const int w = bounds.width();
278 const int h = bounds.height();
279 SkBitmap tmp;
280 // note we explicitly specify our rowBytes to be snug (no gap between rows)
281 tmp.setConfig(SkBitmap::kARGB_8888_Config, w, h, w * 4);
282 if (!tmp.allocPixels()) {
283 return false;
284 }
285
Scroggo813c33c2011-04-07 20:56:21 +0000286 tmp.lockPixels();
reed@google.comac10a2d2010-12-22 21:39:39 +0000287
Scroggoeb176032011-04-07 21:11:49 +0000288 bool read = fContext->readRenderTargetPixels(fRenderTarget,
289 bounds.fLeft, bounds.fTop,
290 bounds.width(), bounds.height(),
291 kRGBA_8888_GrPixelConfig,
292 tmp.getPixels());
Scroggo813c33c2011-04-07 20:56:21 +0000293 tmp.unlockPixels();
294 if (!read) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000295 return false;
296 }
297
298 tmp.swap(*bitmap);
299 return true;
300}
301
302void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y) {
303 SkAutoLockPixels alp(bitmap);
304 if (!bitmap.readyToDraw()) {
305 return;
306 }
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000307 GrPixelConfig config = SkGr::BitmapConfig2PixelConfig(bitmap.config(),
308 bitmap.isOpaque());
reed@google.comac10a2d2010-12-22 21:39:39 +0000309 fContext->setRenderTarget(fRenderTarget);
310 // we aren't setting the clip or matrix, so mark as dirty
311 // we don't need to set them for this call and don't have them anyway
312 fNeedPrepareRenderTarget = true;
313
314 fContext->writePixels(x, y, bitmap.width(), bitmap.height(),
315 config, bitmap.getPixels(), bitmap.rowBytes());
316}
317
318///////////////////////////////////////////////////////////////////////////////
319
320static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000321 const SkClipStack& clipStack,
reed@google.com6f8f2922011-03-04 22:27:10 +0000322 const SkRegion& clipRegion,
323 const SkIPoint& origin) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000324 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000325
326 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000327 iter.reset(clipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000328 const SkIRect& skBounds = clipRegion.getBounds();
329 GrRect bounds;
330 bounds.setLTRB(GrIntToScalar(skBounds.fLeft),
331 GrIntToScalar(skBounds.fTop),
332 GrIntToScalar(skBounds.fRight),
333 GrIntToScalar(skBounds.fBottom));
reed@google.com6f8f2922011-03-04 22:27:10 +0000334 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
335 &bounds);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000336 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000337}
338
339// call this ever each draw call, to ensure that the context reflects our state,
340// and not the state from some other canvas/device
341void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
342 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000343 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000344
345 fContext->setRenderTarget(fRenderTarget);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000346 SkASSERT(draw.fClipStack);
347 convert_matrixclip(fContext, *draw.fMatrix,
reed@google.com6f8f2922011-03-04 22:27:10 +0000348 *draw.fClipStack, *draw.fClip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000349 fNeedPrepareRenderTarget = false;
350 }
351}
352
reed@google.com46799cd2011-02-22 20:56:26 +0000353void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
354 const SkClipStack& clipStack) {
355 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000356 // We don't need to set them now because the context may not reflect this device.
357 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000358}
359
360void SkGpuDevice::gainFocus(SkCanvas* canvas, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000361 const SkRegion& clip, const SkClipStack& clipStack) {
362
reed@google.comac10a2d2010-12-22 21:39:39 +0000363 fContext->setRenderTarget(fRenderTarget);
364
bsalomon@google.comd302f142011-03-03 13:54:13 +0000365 this->INHERITED::gainFocus(canvas, matrix, clip, clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000366
reed@google.com6f8f2922011-03-04 22:27:10 +0000367 convert_matrixclip(fContext, matrix, clipStack, clip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000368
369 if (fNeedClear) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000370 fContext->clear(NULL, 0x0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000371 fNeedClear = false;
372 }
373}
374
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000375bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000376 if (NULL != fTexture) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000377 paint->setTexture(kBitmapTextureIdx, fTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000378 return true;
379 }
380 return false;
381}
382
383///////////////////////////////////////////////////////////////////////////////
384
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000385SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
386SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
387SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
388SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
389SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
390 shader_type_mismatch);
391SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 4, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000392
bsalomon@google.com5782d712011-01-21 21:03:59 +0000393static const GrSamplerState::SampleMode sk_bmp_type_to_sample_mode[] = {
394 (GrSamplerState::SampleMode) -1, // kNone_BitmapType
395 GrSamplerState::kNormal_SampleMode, // kDefault_BitmapType
396 GrSamplerState::kRadial_SampleMode, // kRadial_BitmapType
397 GrSamplerState::kSweep_SampleMode, // kSweep_BitmapType
398 GrSamplerState::kRadial2_SampleMode, // kTwoPointRadial_BitmapType
399};
400
401bool SkGpuDevice::skPaint2GrPaintNoShader(const SkPaint& skPaint,
402 bool justAlpha,
Scroggod757df22011-05-16 13:11:16 +0000403 GrPaint* grPaint,
404 bool constantColor) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000405
406 grPaint->fDither = skPaint.isDither();
407 grPaint->fAntiAlias = skPaint.isAntiAlias();
408
409 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
410 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
411
412 SkXfermode* mode = skPaint.getXfermode();
413 if (mode) {
414 if (!mode->asCoeff(&sm, &dm)) {
reed@google.com1a2e8d22011-01-21 22:08:29 +0000415 SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000416#if 0
417 return false;
418#endif
419 }
420 }
421 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
422 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
423
424 if (justAlpha) {
425 uint8_t alpha = skPaint.getAlpha();
426 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000427 // justAlpha is currently set to true only if there is a texture,
428 // so constantColor should not also be true.
429 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000430 } else {
431 grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000432 grPaint->setTexture(kShaderTextureIdx, NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000433 }
Scroggo97c88c22011-05-11 14:05:25 +0000434 SkColorFilter* colorFilter = skPaint.getColorFilter();
435 SkColor color;
436 SkXfermode::Mode filterMode;
437 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
Scroggod757df22011-05-16 13:11:16 +0000438 if (!constantColor) {
439 grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color);
440 grPaint->fColorFilterXfermode = filterMode;
441 return true;
442 }
443 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
444 grPaint->fColor = SkGr::SkColor2GrColor(filtered);
Scroggo97c88c22011-05-11 14:05:25 +0000445 }
Scroggod757df22011-05-16 13:11:16 +0000446 grPaint->resetColorFilter();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000447 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000448}
449
bsalomon@google.com5782d712011-01-21 21:03:59 +0000450bool SkGpuDevice::skPaint2GrPaintShader(const SkPaint& skPaint,
451 SkAutoCachedTexture* act,
452 const SkMatrix& ctm,
Scroggod757df22011-05-16 13:11:16 +0000453 GrPaint* grPaint,
454 bool constantColor) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000455
bsalomon@google.com5782d712011-01-21 21:03:59 +0000456 SkASSERT(NULL != act);
reed@google.comac10a2d2010-12-22 21:39:39 +0000457
bsalomon@google.com5782d712011-01-21 21:03:59 +0000458 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000459 if (NULL == shader) {
Scroggod757df22011-05-16 13:11:16 +0000460 return this->skPaint2GrPaintNoShader(skPaint,
461 false,
462 grPaint,
463 constantColor);
464 } else if (!this->skPaint2GrPaintNoShader(skPaint, true, grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000465 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000466 }
467
reed@google.comac10a2d2010-12-22 21:39:39 +0000468 SkBitmap bitmap;
469 SkMatrix matrix;
470 SkShader::TileMode tileModes[2];
471 SkScalar twoPointParams[3];
472 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, &matrix,
473 tileModes, twoPointParams);
474
bsalomon@google.com5782d712011-01-21 21:03:59 +0000475 GrSamplerState::SampleMode sampleMode = sk_bmp_type_to_sample_mode[bmptype];
476 if (-1 == sampleMode) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000477 SkShader::GradientInfo info;
478 SkColor color;
479
480 info.fColors = &color;
481 info.fColorOffsets = NULL;
482 info.fColorCount = 1;
483 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
484 SkPaint copy(skPaint);
485 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000486 // modulate the paint alpha by the shader's solid color alpha
487 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
488 copy.setColor(SkColorSetA(color, newA));
reed@google.com2be9e8b2011-07-06 21:18:09 +0000489 return this->skPaint2GrPaintNoShader(copy,
490 false,
491 grPaint,
492 constantColor);
493 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000494 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000495 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000496 GrSamplerState* sampler = grPaint->getTextureSampler(kShaderTextureIdx);
497 sampler->setSampleMode(sampleMode);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000498 if (skPaint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000499 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000500 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000501 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000502 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000503 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
504 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000505 if (GrSamplerState::kRadial2_SampleMode == sampleMode) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000506 sampler->setRadial2Params(twoPointParams[0],
507 twoPointParams[1],
508 twoPointParams[2] < 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000509 }
510
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000511 GrTexture* texture = act->set(this, bitmap, *sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000512 if (NULL == texture) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000513 SkDebugf("Couldn't convert bitmap to texture.\n");
514 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000515 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000516 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000517
518 // since our texture coords will be in local space, we wack the texture
519 // matrix to map them back into 0...1 before we load it
520 SkMatrix localM;
521 if (shader->getLocalMatrix(&localM)) {
522 SkMatrix inverse;
523 if (localM.invert(&inverse)) {
524 matrix.preConcat(inverse);
525 }
526 }
527 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000528 GrScalar sx = GrFixedToScalar(GR_Fixed1 / bitmap.width());
529 GrScalar sy = GrFixedToScalar(GR_Fixed1 / bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +0000530 matrix.postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000531 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000532 GrScalar s = GrFixedToScalar(GR_Fixed1 / bitmap.width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000533 matrix.postScale(s, s);
534 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000535 sampler->setMatrix(matrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000536
537 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000538}
539
540///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000541
542class SkPositionSource {
543public:
544 SkPositionSource(const SkPoint* points, int count)
545 : fPoints(points), fCount(count) {}
546
547 int count() const { return fCount; }
548
549 void writeValue(int i, GrPoint* dstPosition) const {
550 SkASSERT(i < fCount);
551 dstPosition->fX = SkScalarToGrScalar(fPoints[i].fX);
552 dstPosition->fY = SkScalarToGrScalar(fPoints[i].fY);
553 }
554private:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000555 const SkPoint* fPoints;
bsalomon@google.com19628322011-02-03 21:30:17 +0000556 int fCount;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000557};
558
559class SkTexCoordSource {
560public:
561 SkTexCoordSource(const SkPoint* coords)
562 : fCoords(coords) {}
563
564 void writeValue(int i, GrPoint* dstCoord) const {
565 dstCoord->fX = SkScalarToGrScalar(fCoords[i].fX);
566 dstCoord->fY = SkScalarToGrScalar(fCoords[i].fY);
567 }
568private:
569 const SkPoint* fCoords;
570};
571
572class SkColorSource {
573public:
574 SkColorSource(const SkColor* colors) : fColors(colors) {}
575
576 void writeValue(int i, GrColor* dstColor) const {
577 *dstColor = SkGr::SkColor2GrColor(fColors[i]);
578 }
579private:
580 const SkColor* fColors;
581};
582
583class SkIndexSource {
584public:
585 SkIndexSource(const uint16_t* indices, int count)
586 : fIndices(indices), fCount(count) {
587 }
588
589 int count() const { return fCount; }
590
591 void writeValue(int i, uint16_t* dstIndex) const {
592 *dstIndex = fIndices[i];
593 }
594
595private:
bsalomon@google.com5782d712011-01-21 21:03:59 +0000596 const uint16_t* fIndices;
bsalomon@google.com19628322011-02-03 21:30:17 +0000597 int fCount;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000598};
599
600///////////////////////////////////////////////////////////////////////////////
601
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000602#if 0 // not currently being used so don't compile,
603
bsalomon@google.com5782d712011-01-21 21:03:59 +0000604// can be used for positions or texture coordinates
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000605
bsalomon@google.com5782d712011-01-21 21:03:59 +0000606class SkRectFanSource {
607public:
608 SkRectFanSource(const SkRect& rect) : fRect(rect) {}
609
610 int count() const { return 4; }
611
612 void writeValue(int i, GrPoint* dstPoint) const {
613 SkASSERT(i < 4);
614 dstPoint->fX = SkScalarToGrScalar((i % 3) ? fRect.fRight :
615 fRect.fLeft);
616 dstPoint->fY = SkScalarToGrScalar((i < 2) ? fRect.fTop :
617 fRect.fBottom);
618 }
619private:
620 const SkRect& fRect;
621};
622
623class SkIRectFanSource {
624public:
625 SkIRectFanSource(const SkIRect& rect) : fRect(rect) {}
626
627 int count() const { return 4; }
628
629 void writeValue(int i, GrPoint* dstPoint) const {
630 SkASSERT(i < 4);
631 dstPoint->fX = (i % 3) ? GrIntToScalar(fRect.fRight) :
632 GrIntToScalar(fRect.fLeft);
633 dstPoint->fY = (i < 2) ? GrIntToScalar(fRect.fTop) :
634 GrIntToScalar(fRect.fBottom);
635 }
636private:
637 const SkIRect& fRect;
638};
639
640class SkMatRectFanSource {
641public:
642 SkMatRectFanSource(const SkRect& rect, const SkMatrix& matrix)
643 : fRect(rect), fMatrix(matrix) {}
644
645 int count() const { return 4; }
646
647 void writeValue(int i, GrPoint* dstPoint) const {
648 SkASSERT(i < 4);
649
650#if SK_SCALAR_IS_GR_SCALAR
651 fMatrix.mapXY((i % 3) ? fRect.fRight : fRect.fLeft,
652 (i < 2) ? fRect.fTop : fRect.fBottom,
653 (SkPoint*)dstPoint);
654#else
655 SkPoint dst;
656 fMatrix.mapXY((i % 3) ? fRect.fRight : fRect.fLeft,
657 (i < 2) ? fRect.fTop : fRect.fBottom,
658 &dst);
659 dstPoint->fX = SkScalarToGrScalar(dst.fX);
660 dstPoint->fY = SkScalarToGrScalar(dst.fY);
661#endif
662 }
663private:
664 const SkRect& fRect;
665 const SkMatrix& fMatrix;
666};
667
bsalomon@google.com6f7fbc92011-02-01 19:12:40 +0000668#endif
669
reed@google.comac10a2d2010-12-22 21:39:39 +0000670///////////////////////////////////////////////////////////////////////////////
671
bsalomon@google.com398109c2011-04-14 18:40:27 +0000672void SkGpuDevice::clear(SkColor color) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000673 fContext->clear(NULL, color);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000674}
675
reed@google.comac10a2d2010-12-22 21:39:39 +0000676void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
677 CHECK_SHOULD_DRAW(draw);
678
bsalomon@google.com5782d712011-01-21 21:03:59 +0000679 GrPaint grPaint;
680 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000681 if (!this->skPaint2GrPaintShader(paint,
682 &act,
683 *draw.fMatrix,
684 &grPaint,
685 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000686 return;
687 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000688
689 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000690}
691
692// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000693static const GrPrimitiveType gPointMode2PrimtiveType[] = {
694 kPoints_PrimitiveType,
695 kLines_PrimitiveType,
696 kLineStrip_PrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000697};
698
699void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000700 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000701 CHECK_SHOULD_DRAW(draw);
702
703 SkScalar width = paint.getStrokeWidth();
704 if (width < 0) {
705 return;
706 }
707
708 // we only handle hairlines here, else we let the SkDraw call our drawPath()
709 if (width > 0) {
710 draw.drawPoints(mode, count, pts, paint, true);
711 return;
712 }
713
bsalomon@google.com5782d712011-01-21 21:03:59 +0000714 GrPaint grPaint;
715 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000716 if (!this->skPaint2GrPaintShader(paint,
717 &act,
718 *draw.fMatrix,
719 &grPaint,
720 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000721 return;
722 }
723
reed@google.comac10a2d2010-12-22 21:39:39 +0000724#if SK_SCALAR_IS_GR_SCALAR
bsalomon@google.com5782d712011-01-21 21:03:59 +0000725 fContext->drawVertices(grPaint,
726 gPointMode2PrimtiveType[mode],
727 count,
728 (GrPoint*)pts,
729 NULL,
730 NULL,
731 NULL,
732 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000733#else
bsalomon@google.com5782d712011-01-21 21:03:59 +0000734 fContext->drawCustomVertices(grPaint,
735 gPointMode2PrimtiveType[mode],
736 SkPositionSource(pts, count));
reed@google.comac10a2d2010-12-22 21:39:39 +0000737#endif
reed@google.comac10a2d2010-12-22 21:39:39 +0000738}
739
reed@google.comc9aa5872011-04-05 21:05:37 +0000740///////////////////////////////////////////////////////////////////////////////
741
reed@google.comac10a2d2010-12-22 21:39:39 +0000742void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
743 const SkPaint& paint) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000744 CHECK_SHOULD_DRAW(draw);
745
bungeman@google.com79bd8772011-07-18 15:34:08 +0000746 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000747 SkScalar width = paint.getStrokeWidth();
748
749 /*
750 We have special code for hairline strokes, miter-strokes, and fills.
751 Anything else we just call our path code.
752 */
753 bool usePath = doStroke && width > 0 &&
754 paint.getStrokeJoin() != SkPaint::kMiter_Join;
755 // another reason we might need to call drawPath...
756 if (paint.getMaskFilter()) {
757 usePath = true;
758 }
reed@google.com67db6642011-05-26 11:46:35 +0000759 // until we aa rotated rects...
760 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
761 usePath = true;
762 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000763 // until we can both stroke and fill rectangles
764 // with large enough miter limit...
765 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
766 usePath = true;
767 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000768
769 if (usePath) {
770 SkPath path;
771 path.addRect(rect);
772 this->drawPath(draw, path, paint, NULL, true);
773 return;
774 }
775
776 GrPaint grPaint;
777 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000778 if (!this->skPaint2GrPaintShader(paint,
779 &act,
780 *draw.fMatrix,
781 &grPaint,
782 true)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000783 return;
784 }
reed@google.com20efde72011-05-09 17:00:02 +0000785 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000786}
787
reed@google.com69302852011-02-16 18:08:07 +0000788#include "SkMaskFilter.h"
789#include "SkBounder.h"
790
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000791static GrPathFill skToGrFillType(SkPath::FillType fillType) {
792 switch (fillType) {
793 case SkPath::kWinding_FillType:
794 return kWinding_PathFill;
795 case SkPath::kEvenOdd_FillType:
796 return kEvenOdd_PathFill;
797 case SkPath::kInverseWinding_FillType:
798 return kInverseWinding_PathFill;
799 case SkPath::kInverseEvenOdd_FillType:
800 return kInverseEvenOdd_PathFill;
801 default:
802 SkDebugf("Unsupported path fill type\n");
803 return kHairLine_PathFill;
804 }
805}
806
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000807static void buildKernel(float sigma, float* kernel, int kernelWidth) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000808 int halfWidth = (kernelWidth - 1) / 2;
809 float sum = 0.0f;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000810 float denom = 1.0f / (2.0f * sigma * sigma);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000811 for (int i = 0; i < kernelWidth; ++i) {
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000812 float x = static_cast<float>(i - halfWidth);
813 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
814 // is dropped here, since we renormalize the kernel below.
815 kernel[i] = sk_float_exp(- x * x * denom);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000816 sum += kernel[i];
817 }
818 // Normalize the kernel
819 float scale = 1.0f / sum;
820 for (int i = 0; i < kernelWidth; ++i)
821 kernel[i] *= scale;
822}
823
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000824static void scaleRect(SkRect* rect, float scale) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000825 rect->fLeft *= scale;
826 rect->fTop *= scale;
827 rect->fRight *= scale;
828 rect->fBottom *= scale;
829}
830
831static bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
832 SkMaskFilter* filter, const SkMatrix& matrix,
833 const SkRegion& clip, SkBounder* bounder,
834 GrPaint* grp) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000835#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000836 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000837#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000838 SkMaskFilter::BlurInfo info;
839 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000840 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000841 return false;
842 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000843 SkScalar radius = info.fIgnoreTransform ? info.fRadius
844 : matrix.mapRadius(info.fRadius);
845 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000846 if (radius <= 0) {
847 return false;
848 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000849 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000850 SkRect srcRect = path.getBounds();
851
852 int scaleFactor = 1;
853
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000854 while (sigma > MAX_BLUR_SIGMA) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000855 scaleFactor *= 2;
856 sigma *= 0.5f;
857 }
senorblanco@chromium.org4a947d22011-07-18 21:48:35 +0000858 int halfWidth = static_cast<int>(ceilf(sigma * 3.0f));
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000859 int kernelWidth = halfWidth * 2 + 1;
860
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000861 float invScale = 1.0f / scaleFactor;
862 scaleRect(&srcRect, invScale);
863 srcRect.roundOut();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000864 srcRect.inset(-halfWidth, -halfWidth);
865
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000866 SkRect clipBounds;
867 clipBounds.set(clip.getBounds());
868 scaleRect(&clipBounds, invScale);
869 clipBounds.roundOut();
870 clipBounds.inset(-halfWidth, -halfWidth);
871
872 srcRect.intersect(clipBounds);
873
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000874 scaleRect(&srcRect, scaleFactor);
875 SkRect finalRect = srcRect;
876
877 SkIRect finalIRect;
878 finalRect.roundOut(&finalIRect);
879 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000880 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000881 }
882 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000883 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000884 }
885 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
886 srcRect.offset(-srcRect.fLeft, -srcRect.fTop);
887 const GrTextureDesc desc = {
888 kRenderTarget_GrTextureFlagBit,
889 kNone_GrAALevel,
890 srcRect.width(),
891 srcRect.height(),
892 // We actually only need A8, but it often isn't supported as a
893 // render target
894 kRGBA_8888_GrPixelConfig
895 };
896
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000897 GrAutoScratchTexture srcEntry(context, desc);
898 GrAutoScratchTexture dstEntry(context, desc);
899 if (NULL == srcEntry.texture() || NULL == dstEntry.texture()) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000900 return false;
901 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000902 GrTexture* srcTexture = srcEntry.texture();
903 GrTexture* dstTexture = dstEntry.texture();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000904 if (NULL == srcTexture || NULL == dstTexture) {
905 return false;
906 }
907 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000908 // Once this code moves into GrContext, this should be changed to use
909 // an AutoClipRestore.
910 GrClip oldClip = context->getClip();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000911 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000912 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000913 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000914 GrPaint tempPaint;
915 tempPaint.reset();
916
917 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000918 tempPaint.fAntiAlias = grp->fAntiAlias;
919 if (tempPaint.fAntiAlias) {
920 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
921 // blend coeff of zero requires dual source blending support in order
922 // to properly blend partially covered pixels. This means the AA
923 // code path may not be taken. So we use a dst blend coeff of ISA. We
924 // could special case AA draws to a dst surface with known alpha=0 to
925 // use a zero dst coeff when dual source blending isn't available.
926 tempPaint.fSrcBlendCoeff = kOne_BlendCoeff;
927 tempPaint.fDstBlendCoeff = kISC_BlendCoeff;
928 }
929 // Draw hard shadow to dstTexture with path topleft at origin 0,0.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000930 context->drawPath(tempPaint, path, skToGrFillType(path.getFillType()), &offset);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000931 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000932
933 GrMatrix sampleM;
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000934 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000935 GrPaint paint;
936 paint.reset();
937 paint.getTextureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
938 paint.getTextureSampler(0)->setMatrix(sampleM);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000939 GrAutoScratchTexture origEntry;
940
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000941 if (blurType != SkMaskFilter::kNormal_BlurType) {
942 // Stash away a copy of the unblurred image.
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000943 origEntry.set(context, desc);
944 if (NULL == origEntry.texture()) {
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000945 return false;
946 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000947 context->setRenderTarget(origEntry.texture()->asRenderTarget());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000948 paint.setTexture(0, srcTexture);
949 context->drawRect(paint, srcRect);
950 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000951 for (int i = 1; i < scaleFactor; i *= 2) {
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000952 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
953 paint.getTextureSampler(0)->setMatrix(sampleM);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000954 context->setRenderTarget(dstTexture->asRenderTarget());
955 SkRect dstRect(srcRect);
956 scaleRect(&dstRect, 0.5f);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000957 paint.setTexture(0, srcTexture);
958 context->drawRectToRect(paint, dstRect, srcRect);
959 srcRect = dstRect;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000960 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000961 }
962
963 SkAutoTMalloc<float> kernelStorage(kernelWidth);
964 float* kernel = kernelStorage.get();
965 buildKernel(sigma, kernel, kernelWidth);
966
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000967 // Clear out a halfWidth to the right of the srcRect to prevent the
968 // X convolution from reading garbage.
969 SkIRect clearRect = SkIRect::MakeXYWH(
970 srcRect.fRight, srcRect.fTop, halfWidth, srcRect.height());
971 context->clear(&clearRect, 0x0);
972
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000973 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000974 context->convolveInX(srcTexture, srcRect, kernel, kernelWidth);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000975 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000976
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000977 // Clear out a halfWidth below the srcRect to prevent the Y
978 // convolution from reading garbage.
979 clearRect = SkIRect::MakeXYWH(
980 srcRect.fLeft, srcRect.fBottom, srcRect.width(), halfWidth);
981 context->clear(&clearRect, 0x0);
982
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000983 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000984 context->convolveInY(srcTexture, srcRect, kernel, kernelWidth);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000985 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000986
senorblanco@chromium.org2ce9a042011-07-22 15:31:14 +0000987 // Clear one pixel to the right and below, to accommodate bilinear
988 // upsampling.
989 clearRect = SkIRect::MakeXYWH(
990 srcRect.fLeft, srcRect.fBottom, srcRect.width() + 1, 1);
991 context->clear(&clearRect, 0x0);
992 clearRect = SkIRect::MakeXYWH(
993 srcRect.fRight, srcRect.fTop, 1, srcRect.height());
994 context->clear(&clearRect, 0x0);
995
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000996 if (scaleFactor > 1) {
997 // FIXME: This should be mitchell, not bilinear.
998 paint.getTextureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +0000999 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001000 paint.getTextureSampler(0)->setMatrix(sampleM);
1001 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001002 paint.setTexture(0, srcTexture);
1003 SkRect dstRect(srcRect);
1004 scaleRect(&dstRect, scaleFactor);
1005 context->drawRectToRect(paint, dstRect, srcRect);
1006 srcRect = dstRect;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001007 SkTSwap(srcTexture, dstTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001008 }
1009
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001010 if (blurType != SkMaskFilter::kNormal_BlurType) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001011 GrTexture* origTexture = origEntry.texture();
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001012 paint.getTextureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
senorblanco@chromium.org422b67d2011-07-19 21:22:13 +00001013 sampleM.setIDiv(origTexture->width(), origTexture->height());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001014 paint.getTextureSampler(0)->setMatrix(sampleM);
1015 // Blend origTexture over srcTexture.
1016 context->setRenderTarget(srcTexture->asRenderTarget());
1017 paint.setTexture(0, origTexture);
1018 if (SkMaskFilter::kInner_BlurType == blurType) {
1019 // inner: dst = dst * src
1020 paint.fSrcBlendCoeff = kDC_BlendCoeff;
1021 paint.fDstBlendCoeff = kZero_BlendCoeff;
1022 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
1023 // solid: dst = src + dst - src * dst
1024 // = (1 - dst) * src + 1 * dst
1025 paint.fSrcBlendCoeff = kIDC_BlendCoeff;
1026 paint.fDstBlendCoeff = kOne_BlendCoeff;
1027 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
1028 // outer: dst = dst * (1 - src)
1029 // = 0 * src + (1 - src) * dst
1030 paint.fSrcBlendCoeff = kZero_BlendCoeff;
1031 paint.fDstBlendCoeff = kISC_BlendCoeff;
1032 }
1033 context->drawRect(paint, srcRect);
1034 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001035 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +00001036 context->setClip(oldClip);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001037
1038 if (grp->hasTextureOrMask()) {
1039 GrMatrix inverse;
1040 if (!matrix.invert(&inverse)) {
1041 return false;
1042 }
1043 grp->preConcatActiveSamplerMatrices(inverse);
1044 }
1045
1046 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
1047 // we assume the last mask index is available for use
1048 GrAssert(NULL == grp->getMask(MASK_IDX));
1049 grp->setMask(MASK_IDX, srcTexture);
1050 grp->getMaskSampler(MASK_IDX)->setClampNoFilter();
1051
1052 GrMatrix m;
1053 m.setTranslate(-finalRect.fLeft, -finalRect.fTop);
1054 m.postIDiv(srcTexture->width(), srcTexture->height());
1055 grp->getMaskSampler(MASK_IDX)->setMatrix(m);
1056 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001057 return true;
1058}
1059
reed@google.com69302852011-02-16 18:08:07 +00001060static bool drawWithMaskFilter(GrContext* context, const SkPath& path,
1061 SkMaskFilter* filter, const SkMatrix& matrix,
1062 const SkRegion& clip, SkBounder* bounder,
1063 GrPaint* grp) {
1064 SkMask srcM, dstM;
1065
1066 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
1067 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
1068 return false;
1069 }
1070
1071 SkAutoMaskImage autoSrc(&srcM, false);
1072
1073 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
1074 return false;
1075 }
1076 // this will free-up dstM when we're done (allocated in filterMask())
1077 SkAutoMaskImage autoDst(&dstM, false);
1078
1079 if (clip.quickReject(dstM.fBounds)) {
1080 return false;
1081 }
1082 if (bounder && !bounder->doIRect(dstM.fBounds)) {
1083 return false;
1084 }
1085
1086 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
1087 // the current clip (and identity matrix) and grpaint settings
1088
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001089 // used to compute inverse view, if necessary
1090 GrMatrix ivm = context->getMatrix();
1091
reed@google.com0c219b62011-02-16 21:31:18 +00001092 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +00001093
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001094 const GrTextureDesc desc = {
1095 kNone_GrTextureFlags,
1096 kNone_GrAALevel,
reed@google.com69302852011-02-16 18:08:07 +00001097 dstM.fBounds.width(),
1098 dstM.fBounds.height(),
bsalomon@google.com669fdc42011-04-05 17:08:27 +00001099 kAlpha_8_GrPixelConfig
reed@google.com69302852011-02-16 18:08:07 +00001100 };
1101
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001102 GrAutoScratchTexture ast(context, desc);
1103 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001104
reed@google.com69302852011-02-16 18:08:07 +00001105 if (NULL == texture) {
1106 return false;
1107 }
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001108 texture->uploadTextureData(0, 0, desc.fWidth, desc.fHeight,
1109 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +00001110
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001111 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
1112 grp->preConcatActiveSamplerMatrices(ivm);
1113 }
1114
1115 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
1116 // we assume the last mask index is available for use
1117 GrAssert(NULL == grp->getMask(MASK_IDX));
1118 grp->setMask(MASK_IDX, texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001119 grp->getMaskSampler(MASK_IDX)->setClampNoFilter();
reed@google.com69302852011-02-16 18:08:07 +00001120
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001121 GrRect d;
1122 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +00001123 GrIntToScalar(dstM.fBounds.fTop),
1124 GrIntToScalar(dstM.fBounds.fRight),
1125 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001126
1127 GrMatrix m;
1128 m.setTranslate(-dstM.fBounds.fLeft, -dstM.fBounds.fTop);
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001129 m.postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001130 grp->getMaskSampler(MASK_IDX)->setMatrix(m);
1131
1132 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +00001133 return true;
1134}
reed@google.com69302852011-02-16 18:08:07 +00001135
reed@google.com0c219b62011-02-16 21:31:18 +00001136void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
reed@google.comac10a2d2010-12-22 21:39:39 +00001137 const SkPaint& paint, const SkMatrix* prePathMatrix,
1138 bool pathIsMutable) {
1139 CHECK_SHOULD_DRAW(draw);
1140
bsalomon@google.com5782d712011-01-21 21:03:59 +00001141 GrPaint grPaint;
1142 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +00001143 if (!this->skPaint2GrPaintShader(paint,
1144 &act,
1145 *draw.fMatrix,
1146 &grPaint,
1147 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001148 return;
1149 }
1150
reed@google.com0c219b62011-02-16 21:31:18 +00001151 // BEGIN lift from SkDraw::drawPath()
1152
1153 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1154 bool doFill = true;
1155 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001156
1157 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001158 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001159
reed@google.come3445642011-02-16 23:20:39 +00001160 if (!pathIsMutable) {
1161 result = &tmpPath;
1162 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001163 }
reed@google.come3445642011-02-16 23:20:39 +00001164 // should I push prePathMatrix on our MV stack temporarily, instead
1165 // of applying it here? See SkDraw.cpp
1166 pathPtr->transform(*prePathMatrix, result);
1167 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001168 }
reed@google.com0c219b62011-02-16 21:31:18 +00001169 // at this point we're done with prePathMatrix
1170 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001171
bsalomon@google.com04de7822011-03-25 18:04:43 +00001172 // This "if" is not part of the SkDraw::drawPath() lift.
1173 // When we get a 1.0 wide stroke we hairline stroke it instead of creating
1174 // a new stroked-path. This is motivated by canvas2D sites that draw
1175 // lines as 1.0 wide stroked paths. We can consider doing an alpha-modulated-
1176 // hairline for width < 1.0 when AA is enabled.
1177 static const int gMatrixMask = ~(SkMatrix::kIdentity_Mask |
1178 SkMatrix::kTranslate_Mask);
1179 if (!paint.getPathEffect() &&
1180 SkPaint::kStroke_Style == paint.getStyle() &&
1181 !(draw.fMatrix->getType() & gMatrixMask) &&
1182 SK_Scalar1 == paint.getStrokeWidth()) {
1183 doFill = false;
1184 }
1185
1186 if (doFill && (paint.getPathEffect() ||
1187 paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.com0c219b62011-02-16 21:31:18 +00001188 doFill = paint.getFillPath(*pathPtr, &tmpPath);
1189 pathPtr = &tmpPath;
1190 }
1191
1192 // END lift from SkDraw::drawPath()
1193
reed@google.com69302852011-02-16 18:08:07 +00001194 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001195 // avoid possibly allocating a new path in transform if we can
1196 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1197
1198 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001199 pathPtr->transform(*draw.fMatrix, devPathPtr);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001200 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
1201 *draw.fMatrix, *draw.fClip, draw.fBounder,
1202 &grPaint)) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001203 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
1204 *draw.fMatrix, *draw.fClip, draw.fBounder,
1205 &grPaint);
1206 }
reed@google.com69302852011-02-16 18:08:07 +00001207 return;
1208 }
reed@google.com69302852011-02-16 18:08:07 +00001209
bsalomon@google.comffca4002011-02-22 20:34:01 +00001210 GrPathFill fill = kHairLine_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001211
reed@google.com0c219b62011-02-16 21:31:18 +00001212 if (doFill) {
1213 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001214 case SkPath::kWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001215 fill = kWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001216 break;
1217 case SkPath::kEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001218 fill = kEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001219 break;
1220 case SkPath::kInverseWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001221 fill = kInverseWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001222 break;
1223 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001224 fill = kInverseEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001225 break;
1226 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001227 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001228 return;
1229 }
1230 }
1231
reed@google.com07f3ee12011-05-16 17:21:57 +00001232 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001233}
1234
reed@google.comac10a2d2010-12-22 21:39:39 +00001235void SkGpuDevice::drawBitmap(const SkDraw& draw,
1236 const SkBitmap& bitmap,
1237 const SkIRect* srcRectPtr,
1238 const SkMatrix& m,
1239 const SkPaint& paint) {
1240 CHECK_SHOULD_DRAW(draw);
1241
1242 SkIRect srcRect;
1243 if (NULL == srcRectPtr) {
1244 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1245 } else {
1246 srcRect = *srcRectPtr;
1247 }
1248
junov@google.comd935cfb2011-06-27 20:48:23 +00001249 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001250 // Convert the bitmap to a shader so that the rect can be drawn
1251 // through drawRect, which supports mask filters.
1252 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001253 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001254 if (srcRectPtr) {
1255 if (!bitmap.extractSubset(&tmp, srcRect)) {
1256 return; // extraction failed
1257 }
1258 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001259 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001260 }
1261 SkPaint paintWithTexture(paint);
1262 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1263 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001264 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001265 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001266
junov@google.com1d329782011-07-28 20:10:09 +00001267 // Transform 'm' needs to be concatenated to the draw matrix,
1268 // rather than transforming the primitive directly, so that 'm' will
1269 // also affect the behavior of the mask filter.
1270 SkMatrix drawMatrix;
1271 drawMatrix.setConcat(*draw.fMatrix, m);
1272 SkDraw transformedDraw(draw);
1273 transformedDraw.fMatrix = &drawMatrix;
1274
1275 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1276
junov@google.comd935cfb2011-06-27 20:48:23 +00001277 return;
1278 }
1279
bsalomon@google.com5782d712011-01-21 21:03:59 +00001280 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001281 if (!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001282 return;
1283 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001284 GrSamplerState* sampler = grPaint.getTextureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001285 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001286 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001287 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001288 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001289 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001290
bsalomon@google.com91958362011-06-13 17:58:13 +00001291 const int maxTextureSize = fContext->getMaxTextureSize();
1292 if (bitmap.getTexture() || (bitmap.width() <= maxTextureSize &&
1293 bitmap.height() <= maxTextureSize)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001294 // take the fast case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001295 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001296 return;
1297 }
1298
1299 // undo the translate done by SkCanvas
1300 int DX = SkMax32(0, srcRect.fLeft);
1301 int DY = SkMax32(0, srcRect.fTop);
1302 // compute clip bounds in local coordinates
1303 SkIRect clipRect;
1304 {
1305 SkRect r;
1306 r.set(draw.fClip->getBounds());
1307 SkMatrix matrix, inverse;
1308 matrix.setConcat(*draw.fMatrix, m);
1309 if (!matrix.invert(&inverse)) {
1310 return;
1311 }
1312 inverse.mapRect(&r);
1313 r.roundOut(&clipRect);
1314 // apply the canvas' translate to our local clip
1315 clipRect.offset(DX, DY);
1316 }
1317
bsalomon@google.com91958362011-06-13 17:58:13 +00001318 int nx = bitmap.width() / maxTextureSize;
1319 int ny = bitmap.height() / maxTextureSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001320 for (int x = 0; x <= nx; x++) {
1321 for (int y = 0; y <= ny; y++) {
1322 SkIRect tileR;
bsalomon@google.com91958362011-06-13 17:58:13 +00001323 tileR.set(x * maxTextureSize, y * maxTextureSize,
1324 (x + 1) * maxTextureSize, (y + 1) * maxTextureSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001325 if (!SkIRect::Intersects(tileR, clipRect)) {
1326 continue;
1327 }
1328
1329 SkIRect srcR = tileR;
1330 if (!srcR.intersect(srcRect)) {
1331 continue;
1332 }
1333
1334 SkBitmap tmpB;
1335 if (bitmap.extractSubset(&tmpB, tileR)) {
1336 // now offset it to make it "local" to our tmp bitmap
1337 srcR.offset(-tileR.fLeft, -tileR.fTop);
1338
1339 SkMatrix tmpM(m);
1340 {
1341 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1342 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1343 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1344 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001345 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001346 }
1347 }
1348 }
1349}
1350
1351/*
1352 * This is called by drawBitmap(), which has to handle images that may be too
1353 * large to be represented by a single texture.
1354 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001355 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1356 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001357 */
1358void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1359 const SkBitmap& bitmap,
1360 const SkIRect& srcRect,
1361 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001362 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001363 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1364 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001365
reed@google.com9c49bc32011-07-07 13:42:37 +00001366 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001367 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001368 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001369 return;
1370 }
1371
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001372 GrSamplerState* sampler = grPaint->getTextureSampler(kBitmapTextureIdx);
1373
1374 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1375 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
1376 sampler->setSampleMode(GrSamplerState::kNormal_SampleMode);
1377 sampler->setMatrix(GrMatrix::I());
reed@google.comac10a2d2010-12-22 21:39:39 +00001378
1379 GrTexture* texture;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001380 SkAutoCachedTexture act(this, bitmap, *sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001381 if (NULL == texture) {
1382 return;
1383 }
1384
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001385 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001386
reed@google.com20efde72011-05-09 17:00:02 +00001387 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1388 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001389 GrRect paintRect;
junov@google.com6acc9b32011-05-16 18:32:07 +00001390 paintRect.setLTRB(GrFixedToScalar((srcRect.fLeft << 16) / bitmap.width()),
1391 GrFixedToScalar((srcRect.fTop << 16) / bitmap.height()),
1392 GrFixedToScalar((srcRect.fRight << 16) / bitmap.width()),
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001393 GrFixedToScalar((srcRect.fBottom << 16) / bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001394
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001395 if (GrSamplerState::kNearest_Filter != sampler->getFilter() &&
junov@google.com6acc9b32011-05-16 18:32:07 +00001396 (srcRect.width() < bitmap.width() ||
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001397 srcRect.height() < bitmap.height())) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001398 // If drawing a subrect of the bitmap and filtering is enabled,
1399 // use a constrained texture domain to avoid color bleeding
1400 GrScalar left, top, right, bottom;
1401 if (srcRect.width() > 1) {
1402 GrScalar border = GR_ScalarHalf / bitmap.width();
1403 left = paintRect.left() + border;
1404 right = paintRect.right() - border;
1405 } else {
1406 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1407 }
1408 if (srcRect.height() > 1) {
1409 GrScalar border = GR_ScalarHalf / bitmap.height();
1410 top = paintRect.top() + border;
1411 bottom = paintRect.bottom() - border;
1412 } else {
1413 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1414 }
1415 GrRect textureDomain;
1416 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001417 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001418 }
1419
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001420 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001421}
1422
1423void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1424 int left, int top, const SkPaint& paint) {
1425 CHECK_SHOULD_DRAW(draw);
1426
1427 SkAutoLockPixels alp(bitmap);
1428 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1429 return;
1430 }
1431
bsalomon@google.com5782d712011-01-21 21:03:59 +00001432 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001433 if(!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001434 return;
1435 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001436
bsalomon@google.com5782d712011-01-21 21:03:59 +00001437 GrAutoMatrix avm(fContext, GrMatrix::I());
1438
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001439 GrSamplerState* sampler = grPaint.getTextureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001440
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001441 GrTexture* texture;
1442 sampler->setClampNoFilter();
1443 SkAutoCachedTexture act(this, bitmap, *sampler, &texture);
1444
1445 grPaint.setTexture(kBitmapTextureIdx, texture);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001446
bsalomon@google.com5782d712011-01-21 21:03:59 +00001447 fContext->drawRectToRect(grPaint,
reed@google.com20efde72011-05-09 17:00:02 +00001448 GrRect::MakeXYWH(GrIntToScalar(left),
1449 GrIntToScalar(top),
1450 GrIntToScalar(bitmap.width()),
1451 GrIntToScalar(bitmap.height())),
1452 GrRect::MakeWH(GR_Scalar1, GR_Scalar1));
reed@google.comac10a2d2010-12-22 21:39:39 +00001453}
1454
1455void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* dev,
1456 int x, int y, const SkPaint& paint) {
1457 CHECK_SHOULD_DRAW(draw);
1458
bsalomon@google.com5782d712011-01-21 21:03:59 +00001459 GrPaint grPaint;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001460 if (!((SkGpuDevice*)dev)->bindDeviceAsTexture(&grPaint) ||
Scroggod757df22011-05-16 13:11:16 +00001461 !this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001462 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001463 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001464
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001465 GrTexture* devTex = grPaint.getTexture(0);
1466 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001467
1468 const SkBitmap& bm = dev->accessBitmap(false);
1469 int w = bm.width();
1470 int h = bm.height();
1471
1472 GrAutoMatrix avm(fContext, GrMatrix::I());
1473
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001474 grPaint.getTextureSampler(kBitmapTextureIdx)->setClampNoFilter();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001475
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001476 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1477 GrIntToScalar(y),
1478 GrIntToScalar(w),
1479 GrIntToScalar(h));
1480 // The device being drawn may not fill up its texture (saveLayer uses
1481 // the approximate ).
1482 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1483 GR_Scalar1 * h / devTex->height());
1484
1485 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001486}
1487
1488///////////////////////////////////////////////////////////////////////////////
1489
1490// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001491static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1492 kTriangles_PrimitiveType,
1493 kTriangleStrip_PrimitiveType,
1494 kTriangleFan_PrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001495};
1496
1497void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1498 int vertexCount, const SkPoint vertices[],
1499 const SkPoint texs[], const SkColor colors[],
1500 SkXfermode* xmode,
1501 const uint16_t indices[], int indexCount,
1502 const SkPaint& paint) {
1503 CHECK_SHOULD_DRAW(draw);
1504
bsalomon@google.com5782d712011-01-21 21:03:59 +00001505 GrPaint grPaint;
1506 SkAutoCachedTexture act;
1507 // we ignore the shader if texs is null.
1508 if (NULL == texs) {
Scroggod757df22011-05-16 13:11:16 +00001509 if (!this->skPaint2GrPaintNoShader(paint,
1510 false,
1511 &grPaint,
1512 NULL == colors)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001513 return;
1514 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001515 } else {
reed@google.com1a2e8d22011-01-21 22:08:29 +00001516 if (!this->skPaint2GrPaintShader(paint, &act,
1517 *draw.fMatrix,
Scroggod757df22011-05-16 13:11:16 +00001518 &grPaint,
1519 NULL == colors)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001520 return;
1521 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001522 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001523
1524 if (NULL != xmode && NULL != texs && NULL != colors) {
1525 SkXfermode::Mode mode;
1526 if (!SkXfermode::IsMode(xmode, &mode) ||
1527 SkXfermode::kMultiply_Mode != mode) {
1528 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1529#if 0
1530 return
1531#endif
1532 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001533 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001534
1535#if SK_SCALAR_IS_GR_SCALAR
1536 // even if GrColor and SkColor byte offsets match we need
1537 // to perform pre-multiply.
1538 if (NULL == colors) {
1539 fContext->drawVertices(grPaint,
1540 gVertexMode2PrimitiveType[vmode],
1541 vertexCount,
1542 (GrPoint*) vertices,
1543 (GrPoint*) texs,
1544 NULL,
1545 indices,
1546 indexCount);
1547 } else
1548#endif
1549 {
1550 SkTexCoordSource texSrc(texs);
1551 SkColorSource colSrc(colors);
1552 SkIndexSource idxSrc(indices, indexCount);
1553
1554 fContext->drawCustomVertices(grPaint,
1555 gVertexMode2PrimitiveType[vmode],
1556 SkPositionSource(vertices, vertexCount),
1557 (NULL == texs) ? NULL : &texSrc,
1558 (NULL == colors) ? NULL : &colSrc,
1559 (NULL == indices) ? NULL : &idxSrc);
reed@google.comac10a2d2010-12-22 21:39:39 +00001560 }
1561}
1562
1563///////////////////////////////////////////////////////////////////////////////
1564
1565static void GlyphCacheAuxProc(void* data) {
1566 delete (GrFontScaler*)data;
1567}
1568
1569static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1570 void* auxData;
1571 GrFontScaler* scaler = NULL;
1572 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1573 scaler = (GrFontScaler*)auxData;
1574 }
1575 if (NULL == scaler) {
1576 scaler = new SkGrFontScaler(cache);
1577 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1578 }
1579 return scaler;
1580}
1581
1582static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1583 SkFixed fx, SkFixed fy,
1584 const SkGlyph& glyph) {
1585 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1586
1587 GrSkDrawProcs* procs = (GrSkDrawProcs*)state.fDraw->fProcs;
1588
1589 if (NULL == procs->fFontScaler) {
1590 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1591 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001592
1593 /*
reed@google.com3b139f52011-06-07 17:56:25 +00001594 * What should we do with fy? (assuming horizontal/latin text)
reed@google.com39ce0ac2011-04-08 15:42:19 +00001595 *
reed@google.com3b139f52011-06-07 17:56:25 +00001596 * The raster code calls SkFixedFloorToFixed on it, as it does with fx.
1597 * It calls that rather than round, because our caller has already added
1598 * SK_FixedHalf, so that calling floor gives us the rounded integer.
1599 *
1600 * Test code between raster and gpu (they should draw the same)
1601 *
1602 * canvas->drawText("Hamburgefons", 12, 0, 16.5f, paint);
1603 *
1604 * Perhaps we should only perform this integralization if there is no
1605 * fExtMatrix...
reed@google.com39ce0ac2011-04-08 15:42:19 +00001606 */
reed@google.com3b139f52011-06-07 17:56:25 +00001607 fy = SkFixedFloorToFixed(fy);
1608
reed@google.comac10a2d2010-12-22 21:39:39 +00001609 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), fx, 0),
reed@google.com3b139f52011-06-07 17:56:25 +00001610 SkFixedFloorToFixed(fx), fy,
reed@google.comac10a2d2010-12-22 21:39:39 +00001611 procs->fFontScaler);
1612}
1613
bsalomon@google.com5782d712011-01-21 21:03:59 +00001614SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001615
1616 // deferred allocation
1617 if (NULL == fDrawProcs) {
1618 fDrawProcs = new GrSkDrawProcs;
1619 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1620 fDrawProcs->fContext = fContext;
1621 }
1622
1623 // init our (and GL's) state
1624 fDrawProcs->fTextContext = context;
1625 fDrawProcs->fFontScaler = NULL;
1626 return fDrawProcs;
1627}
1628
1629void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1630 size_t byteLength, SkScalar x, SkScalar y,
1631 const SkPaint& paint) {
1632 CHECK_SHOULD_DRAW(draw);
1633
1634 if (draw.fMatrix->getType() & SkMatrix::kPerspective_Mask) {
1635 // this guy will just call our drawPath()
1636 draw.drawText((const char*)text, byteLength, x, y, paint);
1637 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001638 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001639
1640 GrPaint grPaint;
1641 SkAutoCachedTexture act;
1642
Scroggod757df22011-05-16 13:11:16 +00001643 if (!this->skPaint2GrPaintShader(paint,
1644 &act,
1645 *draw.fMatrix,
1646 &grPaint,
1647 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001648 return;
1649 }
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001650 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001651 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001652 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1653 }
1654}
1655
1656void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1657 size_t byteLength, const SkScalar pos[],
1658 SkScalar constY, int scalarsPerPos,
1659 const SkPaint& paint) {
1660 CHECK_SHOULD_DRAW(draw);
1661
1662 if (draw.fMatrix->getType() & SkMatrix::kPerspective_Mask) {
1663 // this guy will just call our drawPath()
1664 draw.drawPosText((const char*)text, byteLength, pos, constY,
1665 scalarsPerPos, paint);
1666 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001667 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001668
1669 GrPaint grPaint;
1670 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +00001671 if (!this->skPaint2GrPaintShader(paint,
1672 &act,
1673 *draw.fMatrix,
1674 &grPaint,
1675 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001676 return;
1677 }
1678
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001679 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001680 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001681 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1682 scalarsPerPos, paint);
1683 }
1684}
1685
1686void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1687 size_t len, const SkPath& path,
1688 const SkMatrix* m, const SkPaint& paint) {
1689 CHECK_SHOULD_DRAW(draw);
1690
1691 SkASSERT(draw.fDevice == this);
1692 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1693}
1694
1695///////////////////////////////////////////////////////////////////////////////
1696
reed@google.comf67e4cf2011-03-15 20:56:58 +00001697bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1698 if (!paint.isLCDRenderText()) {
1699 // we're cool with the paint as is
1700 return false;
1701 }
1702
1703 if (paint.getShader() ||
1704 paint.getXfermode() || // unless its srcover
1705 paint.getMaskFilter() ||
1706 paint.getRasterizer() ||
1707 paint.getColorFilter() ||
1708 paint.getPathEffect() ||
1709 paint.isFakeBoldText() ||
1710 paint.getStyle() != SkPaint::kFill_Style) {
1711 // turn off lcd
1712 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1713 flags->fHinting = paint.getHinting();
1714 return true;
1715 }
1716 // we're cool with the paint as is
1717 return false;
1718}
1719
1720///////////////////////////////////////////////////////////////////////////////
1721
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001722SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(const SkBitmap& bitmap,
1723 const GrSamplerState& sampler,
1724 TexType type) {
1725 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001726 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001727
bsalomon@google.come97f0852011-06-17 13:10:25 +00001728 if (kBitmap_TexType != type) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001729 const GrTextureDesc desc = {
1730 kRenderTarget_GrTextureFlagBit,
1731 kNone_GrAALevel,
1732 bitmap.width(),
1733 bitmap.height(),
1734 SkGr::Bitmap2PixelConfig(bitmap)
1735 };
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001736 GrContext::ScratchTexMatch match;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001737 if (kSaveLayerDeviceRenderTarget_TexType == type) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001738 // we know layers will only be drawn through drawDevice.
1739 // drawDevice has been made to work with content embedded in a
1740 // larger texture so its okay to use the approximate version.
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001741 match = GrContext::kApprox_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001742 } else {
bsalomon@google.come97f0852011-06-17 13:10:25 +00001743 SkASSERT(kDeviceRenderTarget_TexType == type);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001744 match = GrContext::kExact_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001745 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001746 entry = ctx->lockScratchTexture(desc, match);
reed@google.comac10a2d2010-12-22 21:39:39 +00001747 } else {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001748 if (!bitmap.isVolatile()) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001749 GrContext::TextureKey key = bitmap.getGenerationID();
1750 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
junov@google.com4ee7ae52011-06-30 17:30:49 +00001751
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001752 entry = ctx->findAndLockTexture(key, bitmap.width(),
1753 bitmap.height(), sampler);
1754 if (NULL == entry.texture()) {
1755 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
junov@google.com4ee7ae52011-06-30 17:30:49 +00001756 bitmap);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001757 }
junov@google.com4ee7ae52011-06-30 17:30:49 +00001758 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001759 entry = sk_gr_create_bitmap_texture(ctx, gUNCACHED_KEY, sampler, bitmap);
junov@google.com4ee7ae52011-06-30 17:30:49 +00001760 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001761 if (NULL == entry.texture()) {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001762 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1763 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001764 }
1765 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001766 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001767}
1768
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001769void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1770 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001771}
1772
bsalomon@google.come97f0852011-06-17 13:10:25 +00001773SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1774 int width, int height,
1775 bool isOpaque,
1776 Usage usage) {
1777 return SkNEW_ARGS(SkGpuDevice,(this->context(), config,
1778 width, height, usage));
1779}
1780