blob: 91184b70fff0e4864b360c6950e4626106f89ced [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"
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +000020#include "SkImageFilter.h"
reed@google.comfe626382011-09-21 13:50:35 +000021#include "SkTLazy.h"
reed@google.comc9aa5872011-04-05 21:05:37 +000022#include "SkUtils.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000023
24#define CACHE_LAYER_TEXTURES 1
25
26#if 0
27 extern bool (*gShouldDrawProc)();
28 #define CHECK_SHOULD_DRAW(draw) \
29 do { \
30 if (gShouldDrawProc && !gShouldDrawProc()) return; \
31 this->prepareRenderTarget(draw); \
32 } while (0)
33#else
34 #define CHECK_SHOULD_DRAW(draw) this->prepareRenderTarget(draw)
35#endif
36
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000037// we use the same texture slot on GrPaint for bitmaps and shaders
38// (since drawBitmap, drawSprite, and drawDevice ignore skia's shader)
39enum {
40 kBitmapTextureIdx = 0,
41 kShaderTextureIdx = 0
42};
43
reed@google.comcde92112011-07-06 20:00:52 +000044
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +000045#define MAX_BLUR_SIGMA 4.0f
46// FIXME: This value comes from from SkBlurMaskFilter.cpp.
47// Should probably be put in a common header someplace.
48#define MAX_BLUR_RADIUS SkIntToScalar(128)
49// This constant approximates the scaling done in the software path's
50// "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
51// IMHO, it actually should be 1: we blur "less" than we should do
52// according to the CSS and canvas specs, simply because Safari does the same.
53// Firefox used to do the same too, until 4.0 where they fixed it. So at some
54// point we should probably get rid of these scaling constants and rebaseline
55// all the blur tests.
56#define BLUR_SIGMA_SCALE 0.6f
reed@google.comac10a2d2010-12-22 21:39:39 +000057///////////////////////////////////////////////////////////////////////////////
58
59SkGpuDevice::SkAutoCachedTexture::
60 SkAutoCachedTexture(SkGpuDevice* device,
61 const SkBitmap& bitmap,
62 const GrSamplerState& sampler,
63 GrTexture** texture) {
64 GrAssert(texture);
reed@google.comac10a2d2010-12-22 21:39:39 +000065 *texture = this->set(device, bitmap, sampler);
66}
67
68SkGpuDevice::SkAutoCachedTexture::SkAutoCachedTexture() {
reed@google.comac10a2d2010-12-22 21:39:39 +000069}
70
71GrTexture* SkGpuDevice::SkAutoCachedTexture::set(SkGpuDevice* device,
72 const SkBitmap& bitmap,
73 const GrSamplerState& sampler) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000074 if (fTex.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +000075 fDevice->unlockCachedTexture(fTex);
76 }
77 fDevice = device;
78 GrTexture* texture = (GrTexture*)bitmap.getTexture();
79 if (texture) {
80 // return the native texture
bsalomon@google.com50398bf2011-07-26 20:45:30 +000081 fTex.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +000082 } else {
83 // look it up in our cache
bsalomon@google.com50398bf2011-07-26 20:45:30 +000084 fTex = device->lockCachedTexture(bitmap, sampler);
85 texture = fTex.texture();
reed@google.comac10a2d2010-12-22 21:39:39 +000086 }
87 return texture;
88}
89
90SkGpuDevice::SkAutoCachedTexture::~SkAutoCachedTexture() {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000091 if (fTex.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +000092 fDevice->unlockCachedTexture(fTex);
93 }
94}
95
96///////////////////////////////////////////////////////////////////////////////
97
98bool gDoTraceDraw;
99
100struct GrSkDrawProcs : public SkDrawProcs {
101public:
102 GrContext* fContext;
103 GrTextContext* fTextContext;
104 GrFontScaler* fFontScaler; // cached in the skia glyphcache
105};
106
107///////////////////////////////////////////////////////////////////////////////
108
reed@google.comaf951c92011-06-16 19:10:39 +0000109static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
110 switch (config) {
111 case kAlpha_8_GrPixelConfig:
112 *isOpaque = false;
113 return SkBitmap::kA8_Config;
114 case kRGB_565_GrPixelConfig:
115 *isOpaque = true;
116 return SkBitmap::kRGB_565_Config;
117 case kRGBA_4444_GrPixelConfig:
118 *isOpaque = false;
119 return SkBitmap::kARGB_4444_Config;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000120 case kSkia8888_PM_GrPixelConfig:
121 // we don't currently have a way of knowing whether
122 // a 8888 is opaque based on the config.
123 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000124 return SkBitmap::kARGB_8888_Config;
125 default:
126 *isOpaque = false;
127 return SkBitmap::kNo_Config;
128 }
129}
reed@google.comac10a2d2010-12-22 21:39:39 +0000130
reed@google.comaf951c92011-06-16 19:10:39 +0000131static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000132 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000133
134 bool isOpaque;
135 SkBitmap bitmap;
136 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
137 renderTarget->width(), renderTarget->height());
138 bitmap.setIsOpaque(isOpaque);
139 return bitmap;
140}
141
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000142SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
143: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
144 this->initFromRenderTarget(context, texture->asRenderTarget());
145}
146
reed@google.comaf951c92011-06-16 19:10:39 +0000147SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
148: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000149 this->initFromRenderTarget(context, renderTarget);
150}
151
152void SkGpuDevice::initFromRenderTarget(GrContext* context,
153 GrRenderTarget* renderTarget) {
reed@google.comaf951c92011-06-16 19:10:39 +0000154 fNeedPrepareRenderTarget = false;
155 fDrawProcs = NULL;
156
157 fContext = context;
158 fContext->ref();
159
reed@google.comaf951c92011-06-16 19:10:39 +0000160 fTexture = NULL;
161 fRenderTarget = NULL;
162 fNeedClear = false;
163
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000164 GrAssert(NULL != renderTarget);
165 fRenderTarget = renderTarget;
166 fRenderTarget->ref();
167 // if this RT is also a texture, hold a ref on it
168 fTexture = fRenderTarget->asTexture();
169 SkSafeRef(fTexture);
reed@google.comaf951c92011-06-16 19:10:39 +0000170
171 SkGrRenderTargetPixelRef* pr = new SkGrRenderTargetPixelRef(fRenderTarget);
172 this->setPixelRef(pr, 0)->unref();
173}
174
175SkGpuDevice::SkGpuDevice(GrContext* context, SkBitmap::Config config, int width,
bsalomon@google.come97f0852011-06-17 13:10:25 +0000176 int height, Usage usage)
reed@google.comaf951c92011-06-16 19:10:39 +0000177: SkDevice(config, width, height, false /*isOpaque*/) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000178 fNeedPrepareRenderTarget = false;
179 fDrawProcs = NULL;
180
reed@google.com7b201d22011-01-11 18:59:23 +0000181 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000182 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000183
reed@google.comac10a2d2010-12-22 21:39:39 +0000184 fTexture = NULL;
185 fRenderTarget = NULL;
186 fNeedClear = false;
187
reed@google.comaf951c92011-06-16 19:10:39 +0000188 if (config != SkBitmap::kRGB_565_Config) {
189 config = SkBitmap::kARGB_8888_Config;
190 }
191 SkBitmap bm;
192 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000193
194#if CACHE_LAYER_TEXTURES
bsalomon@google.come97f0852011-06-17 13:10:25 +0000195 TexType type = (kSaveLayer_Usage == usage) ?
196 kSaveLayerDeviceRenderTarget_TexType :
197 kDeviceRenderTarget_TexType;
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000198 fCache = this->lockCachedTexture(bm, GrSamplerState::ClampNoFilter(), type);
199 fTexture = fCache.texture();
200 if (fTexture) {
reed@google.comaf951c92011-06-16 19:10:39 +0000201 SkASSERT(NULL != fTexture->asRenderTarget());
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000202 // hold a ref directly on fTexture (even though fCache has one) to match
203 // other constructor paths. Simplifies cleanup.
204 fTexture->ref();
reed@google.comaf951c92011-06-16 19:10:39 +0000205 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000206#else
reed@google.comaf951c92011-06-16 19:10:39 +0000207 const GrTextureDesc desc = {
208 kRenderTarget_GrTextureFlagBit,
209 kNone_GrAALevel,
210 width,
211 height,
212 SkGr::Bitmap2PixelConfig(bm)
213 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000214
reed@google.comaf951c92011-06-16 19:10:39 +0000215 fTexture = fContext->createUncachedTexture(desc, NULL, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000216#endif
reed@google.comaf951c92011-06-16 19:10:39 +0000217 if (NULL != fTexture) {
218 fRenderTarget = fTexture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000219 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000220
reed@google.comaf951c92011-06-16 19:10:39 +0000221 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000222
reed@google.comaf951c92011-06-16 19:10:39 +0000223 // we defer the actual clear until our gainFocus()
224 fNeedClear = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000225
reed@google.comaf951c92011-06-16 19:10:39 +0000226 // wrap the bitmap with a pixelref to expose our texture
227 SkGrTexturePixelRef* pr = new SkGrTexturePixelRef(fTexture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000228 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000229 } else {
230 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
231 width, height);
232 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000233 }
234}
235
236SkGpuDevice::~SkGpuDevice() {
237 if (fDrawProcs) {
238 delete fDrawProcs;
239 }
240
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000241 SkSafeUnref(fTexture);
242 SkSafeUnref(fRenderTarget);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000243 if (fCache.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000244 GrAssert(NULL != fTexture);
245 GrAssert(fRenderTarget == fTexture->asRenderTarget());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000246 fContext->unlockTexture(fCache);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000247 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000248 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000249}
250
reed@google.comac10a2d2010-12-22 21:39:39 +0000251///////////////////////////////////////////////////////////////////////////////
252
253void SkGpuDevice::makeRenderTargetCurrent() {
254 fContext->setRenderTarget(fRenderTarget);
255 fContext->flush(true);
256 fNeedPrepareRenderTarget = true;
257}
258
259///////////////////////////////////////////////////////////////////////////////
260
bsalomon@google.comc4364992011-11-07 15:54:49 +0000261namespace {
262GrPixelConfig config8888_to_gr_config(SkCanvas::Config8888 config8888) {
263 switch (config8888) {
264 case SkCanvas::kNative_Premul_Config8888:
265 return kSkia8888_PM_GrPixelConfig;
266 case SkCanvas::kNative_Unpremul_Config8888:
267 return kSkia8888_UPM_GrPixelConfig;
268 case SkCanvas::kBGRA_Premul_Config8888:
269 return kBGRA_8888_PM_GrPixelConfig;
270 case SkCanvas::kBGRA_Unpremul_Config8888:
271 return kBGRA_8888_UPM_GrPixelConfig;
272 case SkCanvas::kRGBA_Premul_Config8888:
273 return kRGBA_8888_PM_GrPixelConfig;
274 case SkCanvas::kRGBA_Unpremul_Config8888:
275 return kRGBA_8888_UPM_GrPixelConfig;
276 default:
277 GrCrash("Unexpected Config8888.");
278 return kSkia8888_PM_GrPixelConfig;
279 }
280}
281}
282
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000283bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
284 int x, int y,
285 SkCanvas::Config8888 config8888) {
bsalomon@google.com910267d2011-11-02 20:06:25 +0000286 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
287 SkASSERT(!bitmap.isNull());
288 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000289
bsalomon@google.com910267d2011-11-02 20:06:25 +0000290 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000291 GrPixelConfig config;
292 config = config8888_to_gr_config(config8888);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000293 return fContext->readRenderTargetPixels(fRenderTarget,
294 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000295 bitmap.width(),
296 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000297 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000298 bitmap.getPixels(),
299 bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000300}
301
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000302void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
303 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000304 SkAutoLockPixels alp(bitmap);
305 if (!bitmap.readyToDraw()) {
306 return;
307 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000308
309 GrPixelConfig config;
310 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
311 config = config8888_to_gr_config(config8888);
312 } else {
313 config= SkGr::BitmapConfig2PixelConfig(bitmap.config(),
314 bitmap.isOpaque());
315 }
316
reed@google.comac10a2d2010-12-22 21:39:39 +0000317 fContext->setRenderTarget(fRenderTarget);
318 // we aren't setting the clip or matrix, so mark as dirty
319 // we don't need to set them for this call and don't have them anyway
320 fNeedPrepareRenderTarget = true;
321
bsalomon@google.com74b98712011-11-11 19:46:16 +0000322 fContext->writePixels(x, y, bitmap.width(), bitmap.height(),
323 config, bitmap.getPixels(), bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000324}
325
326///////////////////////////////////////////////////////////////////////////////
327
328static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000329 const SkClipStack& clipStack,
reed@google.com6f8f2922011-03-04 22:27:10 +0000330 const SkRegion& clipRegion,
331 const SkIPoint& origin) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000332 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000333
334 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000335 iter.reset(clipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000336 const SkIRect& skBounds = clipRegion.getBounds();
337 GrRect bounds;
338 bounds.setLTRB(GrIntToScalar(skBounds.fLeft),
339 GrIntToScalar(skBounds.fTop),
340 GrIntToScalar(skBounds.fRight),
341 GrIntToScalar(skBounds.fBottom));
reed@google.com6f8f2922011-03-04 22:27:10 +0000342 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
343 &bounds);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000344 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000345}
346
347// call this ever each draw call, to ensure that the context reflects our state,
348// and not the state from some other canvas/device
349void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
350 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000351 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000352
353 fContext->setRenderTarget(fRenderTarget);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000354 SkASSERT(draw.fClipStack);
355 convert_matrixclip(fContext, *draw.fMatrix,
reed@google.com6f8f2922011-03-04 22:27:10 +0000356 *draw.fClipStack, *draw.fClip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000357 fNeedPrepareRenderTarget = false;
358 }
359}
360
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000361void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
362 const SkClipStack& clipStack) {
363 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
364 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000365 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000366}
367
368void SkGpuDevice::gainFocus(SkCanvas* canvas, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000369 const SkRegion& clip, const SkClipStack& clipStack) {
370
reed@google.comac10a2d2010-12-22 21:39:39 +0000371 fContext->setRenderTarget(fRenderTarget);
372
bsalomon@google.comd302f142011-03-03 13:54:13 +0000373 this->INHERITED::gainFocus(canvas, matrix, clip, clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000374
reed@google.com6f8f2922011-03-04 22:27:10 +0000375 convert_matrixclip(fContext, matrix, clipStack, clip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000376
377 if (fNeedClear) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000378 fContext->clear(NULL, 0x0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000379 fNeedClear = false;
380 }
381}
382
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000383bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000384 if (NULL != fTexture) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000385 paint->setTexture(kBitmapTextureIdx, fTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000386 return true;
387 }
388 return false;
389}
390
391///////////////////////////////////////////////////////////////////////////////
392
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000393SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
394SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
395SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
396SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
397SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
398 shader_type_mismatch);
399SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 4, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000400
bsalomon@google.com5782d712011-01-21 21:03:59 +0000401static const GrSamplerState::SampleMode sk_bmp_type_to_sample_mode[] = {
402 (GrSamplerState::SampleMode) -1, // kNone_BitmapType
403 GrSamplerState::kNormal_SampleMode, // kDefault_BitmapType
404 GrSamplerState::kRadial_SampleMode, // kRadial_BitmapType
405 GrSamplerState::kSweep_SampleMode, // kSweep_BitmapType
406 GrSamplerState::kRadial2_SampleMode, // kTwoPointRadial_BitmapType
407};
408
409bool SkGpuDevice::skPaint2GrPaintNoShader(const SkPaint& skPaint,
410 bool justAlpha,
Scroggod757df22011-05-16 13:11:16 +0000411 GrPaint* grPaint,
412 bool constantColor) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000413
414 grPaint->fDither = skPaint.isDither();
415 grPaint->fAntiAlias = skPaint.isAntiAlias();
416
417 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
418 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
419
420 SkXfermode* mode = skPaint.getXfermode();
421 if (mode) {
422 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000423 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000424#if 0
425 return false;
426#endif
427 }
428 }
429 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
430 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
431
432 if (justAlpha) {
433 uint8_t alpha = skPaint.getAlpha();
434 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000435 // justAlpha is currently set to true only if there is a texture,
436 // so constantColor should not also be true.
437 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000438 } else {
439 grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000440 grPaint->setTexture(kShaderTextureIdx, NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000441 }
Scroggo97c88c22011-05-11 14:05:25 +0000442 SkColorFilter* colorFilter = skPaint.getColorFilter();
443 SkColor color;
444 SkXfermode::Mode filterMode;
445 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
Scroggod757df22011-05-16 13:11:16 +0000446 if (!constantColor) {
447 grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color);
448 grPaint->fColorFilterXfermode = filterMode;
449 return true;
450 }
451 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
452 grPaint->fColor = SkGr::SkColor2GrColor(filtered);
Scroggo97c88c22011-05-11 14:05:25 +0000453 }
Scroggod757df22011-05-16 13:11:16 +0000454 grPaint->resetColorFilter();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000455 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000456}
457
bsalomon@google.com5782d712011-01-21 21:03:59 +0000458bool SkGpuDevice::skPaint2GrPaintShader(const SkPaint& skPaint,
459 SkAutoCachedTexture* act,
460 const SkMatrix& ctm,
Scroggod757df22011-05-16 13:11:16 +0000461 GrPaint* grPaint,
462 bool constantColor) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000463
bsalomon@google.com5782d712011-01-21 21:03:59 +0000464 SkASSERT(NULL != act);
reed@google.comac10a2d2010-12-22 21:39:39 +0000465
bsalomon@google.com5782d712011-01-21 21:03:59 +0000466 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000467 if (NULL == shader) {
Scroggod757df22011-05-16 13:11:16 +0000468 return this->skPaint2GrPaintNoShader(skPaint,
469 false,
470 grPaint,
471 constantColor);
472 } else if (!this->skPaint2GrPaintNoShader(skPaint, true, grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000473 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000474 }
475
reed@google.comac10a2d2010-12-22 21:39:39 +0000476 SkBitmap bitmap;
477 SkMatrix matrix;
478 SkShader::TileMode tileModes[2];
479 SkScalar twoPointParams[3];
480 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, &matrix,
481 tileModes, twoPointParams);
482
bsalomon@google.com5782d712011-01-21 21:03:59 +0000483 GrSamplerState::SampleMode sampleMode = sk_bmp_type_to_sample_mode[bmptype];
484 if (-1 == sampleMode) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000485 SkShader::GradientInfo info;
486 SkColor color;
487
488 info.fColors = &color;
489 info.fColorOffsets = NULL;
490 info.fColorCount = 1;
491 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
492 SkPaint copy(skPaint);
493 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000494 // modulate the paint alpha by the shader's solid color alpha
495 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
496 copy.setColor(SkColorSetA(color, newA));
reed@google.com2be9e8b2011-07-06 21:18:09 +0000497 return this->skPaint2GrPaintNoShader(copy,
498 false,
499 grPaint,
500 constantColor);
501 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000502 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000503 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000504 GrSamplerState* sampler = grPaint->getTextureSampler(kShaderTextureIdx);
505 sampler->setSampleMode(sampleMode);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000506 if (skPaint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000507 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000508 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000509 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000510 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000511 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
512 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000513 if (GrSamplerState::kRadial2_SampleMode == sampleMode) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000514 sampler->setRadial2Params(twoPointParams[0],
515 twoPointParams[1],
516 twoPointParams[2] < 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000517 }
518
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000519 GrTexture* texture = act->set(this, bitmap, *sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000520 if (NULL == texture) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000521 SkDebugf("Couldn't convert bitmap to texture.\n");
522 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000523 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000524 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000525
526 // since our texture coords will be in local space, we wack the texture
527 // matrix to map them back into 0...1 before we load it
528 SkMatrix localM;
529 if (shader->getLocalMatrix(&localM)) {
530 SkMatrix inverse;
531 if (localM.invert(&inverse)) {
532 matrix.preConcat(inverse);
533 }
534 }
535 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000536 GrScalar sx = GrFixedToScalar(GR_Fixed1 / bitmap.width());
537 GrScalar sy = GrFixedToScalar(GR_Fixed1 / bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +0000538 matrix.postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000539 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000540 GrScalar s = GrFixedToScalar(GR_Fixed1 / bitmap.width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000541 matrix.postScale(s, s);
542 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000543 sampler->setMatrix(matrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000544
545 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000546}
547
548///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000549
bsalomon@google.com398109c2011-04-14 18:40:27 +0000550void SkGpuDevice::clear(SkColor color) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000551 fContext->clear(NULL, color);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000552}
553
reed@google.comac10a2d2010-12-22 21:39:39 +0000554void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
555 CHECK_SHOULD_DRAW(draw);
556
bsalomon@google.com5782d712011-01-21 21:03:59 +0000557 GrPaint grPaint;
558 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000559 if (!this->skPaint2GrPaintShader(paint,
560 &act,
561 *draw.fMatrix,
562 &grPaint,
563 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000564 return;
565 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000566
567 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000568}
569
570// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000571static const GrPrimitiveType gPointMode2PrimtiveType[] = {
572 kPoints_PrimitiveType,
573 kLines_PrimitiveType,
574 kLineStrip_PrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000575};
576
577void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000578 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000579 CHECK_SHOULD_DRAW(draw);
580
581 SkScalar width = paint.getStrokeWidth();
582 if (width < 0) {
583 return;
584 }
585
586 // we only handle hairlines here, else we let the SkDraw call our drawPath()
587 if (width > 0) {
588 draw.drawPoints(mode, count, pts, paint, true);
589 return;
590 }
591
bsalomon@google.com5782d712011-01-21 21:03:59 +0000592 GrPaint grPaint;
593 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000594 if (!this->skPaint2GrPaintShader(paint,
595 &act,
596 *draw.fMatrix,
597 &grPaint,
598 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000599 return;
600 }
601
bsalomon@google.com5782d712011-01-21 21:03:59 +0000602 fContext->drawVertices(grPaint,
603 gPointMode2PrimtiveType[mode],
604 count,
605 (GrPoint*)pts,
606 NULL,
607 NULL,
608 NULL,
609 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000610}
611
reed@google.comc9aa5872011-04-05 21:05:37 +0000612///////////////////////////////////////////////////////////////////////////////
613
reed@google.comac10a2d2010-12-22 21:39:39 +0000614void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
615 const SkPaint& paint) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000616 CHECK_SHOULD_DRAW(draw);
617
bungeman@google.com79bd8772011-07-18 15:34:08 +0000618 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000619 SkScalar width = paint.getStrokeWidth();
620
621 /*
622 We have special code for hairline strokes, miter-strokes, and fills.
623 Anything else we just call our path code.
624 */
625 bool usePath = doStroke && width > 0 &&
626 paint.getStrokeJoin() != SkPaint::kMiter_Join;
627 // another reason we might need to call drawPath...
628 if (paint.getMaskFilter()) {
629 usePath = true;
630 }
reed@google.com67db6642011-05-26 11:46:35 +0000631 // until we aa rotated rects...
632 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
633 usePath = true;
634 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000635 // small miter limit means right angles show bevel...
636 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
637 paint.getStrokeMiter() < SK_ScalarSqrt2)
638 {
639 usePath = true;
640 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000641 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000642 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
643 usePath = true;
644 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000645
646 if (usePath) {
647 SkPath path;
648 path.addRect(rect);
649 this->drawPath(draw, path, paint, NULL, true);
650 return;
651 }
652
653 GrPaint grPaint;
654 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000655 if (!this->skPaint2GrPaintShader(paint,
656 &act,
657 *draw.fMatrix,
658 &grPaint,
659 true)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000660 return;
661 }
reed@google.com20efde72011-05-09 17:00:02 +0000662 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000663}
664
reed@google.com69302852011-02-16 18:08:07 +0000665#include "SkMaskFilter.h"
666#include "SkBounder.h"
667
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000668static GrPathFill skToGrFillType(SkPath::FillType fillType) {
669 switch (fillType) {
670 case SkPath::kWinding_FillType:
671 return kWinding_PathFill;
672 case SkPath::kEvenOdd_FillType:
673 return kEvenOdd_PathFill;
674 case SkPath::kInverseWinding_FillType:
675 return kInverseWinding_PathFill;
676 case SkPath::kInverseEvenOdd_FillType:
677 return kInverseEvenOdd_PathFill;
678 default:
679 SkDebugf("Unsupported path fill type\n");
680 return kHairLine_PathFill;
681 }
682}
683
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000684static void buildKernel(float sigma, float* kernel, int kernelWidth) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000685 int halfWidth = (kernelWidth - 1) / 2;
686 float sum = 0.0f;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000687 float denom = 1.0f / (2.0f * sigma * sigma);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000688 for (int i = 0; i < kernelWidth; ++i) {
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000689 float x = static_cast<float>(i - halfWidth);
690 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
691 // is dropped here, since we renormalize the kernel below.
692 kernel[i] = sk_float_exp(- x * x * denom);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000693 sum += kernel[i];
694 }
695 // Normalize the kernel
696 float scale = 1.0f / sum;
697 for (int i = 0; i < kernelWidth; ++i)
698 kernel[i] *= scale;
699}
700
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000701static void scaleRect(SkRect* rect, float xScale, float yScale) {
702 rect->fLeft *= xScale;
703 rect->fTop *= yScale;
704 rect->fRight *= xScale;
705 rect->fBottom *= yScale;
706}
707
708static float adjustSigma(float sigma, int *scaleFactor, int *halfWidth,
709 int *kernelWidth) {
710 *scaleFactor = 1;
711 while (sigma > MAX_BLUR_SIGMA) {
712 *scaleFactor *= 2;
713 sigma *= 0.5f;
714 }
715 *halfWidth = static_cast<int>(ceilf(sigma * 3.0f));
716 *kernelWidth = *halfWidth * 2 + 1;
717 return sigma;
718}
719
720// Apply a Gaussian blur to srcTexture by sigmaX and sigmaY, within the given
721// rect.
722// temp1 and temp2 are used for allocation of intermediate textures.
723// If temp2 is non-NULL, srcTexture will be untouched, and the return
724// value will be either temp1 or temp2.
725// If temp2 is NULL, srcTexture will be overwritten with intermediate
726// results, and the return value will either be temp1 or srcTexture.
727static GrTexture* gaussianBlur(GrContext* context, GrTexture* srcTexture,
728 GrAutoScratchTexture* temp1,
729 GrAutoScratchTexture* temp2,
730 const SkRect& rect,
731 float sigmaX, float sigmaY) {
732
733 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
734 GrClip oldClip = context->getClip();
735 GrTexture* origTexture = srcTexture;
736 GrAutoMatrix avm(context, GrMatrix::I());
737 SkIRect clearRect;
738 int scaleFactorX, halfWidthX, kernelWidthX;
739 int scaleFactorY, halfWidthY, kernelWidthY;
740 sigmaX = adjustSigma(sigmaX, &scaleFactorX, &halfWidthX, &kernelWidthX);
741 sigmaY = adjustSigma(sigmaY, &scaleFactorY, &halfWidthY, &kernelWidthY);
742
743 SkRect srcRect(rect);
744 scaleRect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
745 srcRect.roundOut();
746 scaleRect(&srcRect, scaleFactorX, scaleFactorY);
747 context->setClip(srcRect);
748
749 const GrTextureDesc desc = {
750 kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit,
751 kNone_GrAALevel,
752 srcRect.width(),
753 srcRect.height(),
tomhudson@google.comf74ad8c2011-11-09 22:15:08 +0000754 { kRGBA_8888_GrPixelConfig }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000755 };
756
757 temp1->set(context, desc);
758 if (temp2) temp2->set(context, desc);
759
760 GrTexture* dstTexture = temp1->texture();
761 GrPaint paint;
762 paint.reset();
763 paint.getTextureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
764
765 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
766 GrMatrix sampleM;
767 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
768 paint.getTextureSampler(0)->setMatrix(sampleM);
769 context->setRenderTarget(dstTexture->asRenderTarget());
770 SkRect dstRect(srcRect);
771 scaleRect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
772 i < scaleFactorY ? 0.5f : 1.0f);
773 paint.setTexture(0, srcTexture);
774 context->drawRectToRect(paint, dstRect, srcRect);
775 srcRect = dstRect;
776 SkTSwap(srcTexture, dstTexture);
777 // If temp2 is non-NULL, don't render back to origTexture
778 if (temp2 && dstTexture == origTexture) dstTexture = temp2->texture();
779 }
780
781 if (sigmaX > 0.0f) {
782 SkAutoTMalloc<float> kernelStorageX(kernelWidthX);
783 float* kernelX = kernelStorageX.get();
784 buildKernel(sigmaX, kernelX, kernelWidthX);
785
786 if (scaleFactorX > 1) {
787 // Clear out a halfWidth to the right of the srcRect to prevent the
788 // X convolution from reading garbage.
789 clearRect = SkIRect::MakeXYWH(
790 srcRect.fRight, srcRect.fTop, halfWidthX, srcRect.height());
791 context->clear(&clearRect, 0x0);
792 }
793
794 context->setRenderTarget(dstTexture->asRenderTarget());
795 context->convolveInX(srcTexture, srcRect, kernelX, kernelWidthX);
796 SkTSwap(srcTexture, dstTexture);
797 if (temp2 && dstTexture == origTexture) dstTexture = temp2->texture();
798 }
799
800 if (sigmaY > 0.0f) {
801 SkAutoTMalloc<float> kernelStorageY(kernelWidthY);
802 float* kernelY = kernelStorageY.get();
803 buildKernel(sigmaY, kernelY, kernelWidthY);
804
805 if (scaleFactorY > 1 || sigmaX > 0.0f) {
806 // Clear out a halfWidth below the srcRect to prevent the Y
807 // convolution from reading garbage.
808 clearRect = SkIRect::MakeXYWH(
809 srcRect.fLeft, srcRect.fBottom, srcRect.width(), halfWidthY);
810 context->clear(&clearRect, 0x0);
811 }
812
813 context->setRenderTarget(dstTexture->asRenderTarget());
814 context->convolveInY(srcTexture, srcRect, kernelY, kernelWidthY);
815 SkTSwap(srcTexture, dstTexture);
816 if (temp2 && dstTexture == origTexture) dstTexture = temp2->texture();
817 }
818
819 if (scaleFactorX > 1 || scaleFactorY > 1) {
820 // Clear one pixel to the right and below, to accommodate bilinear
821 // upsampling.
822 clearRect = SkIRect::MakeXYWH(
823 srcRect.fLeft, srcRect.fBottom, srcRect.width() + 1, 1);
824 context->clear(&clearRect, 0x0);
825 clearRect = SkIRect::MakeXYWH(
826 srcRect.fRight, srcRect.fTop, 1, srcRect.height());
827 context->clear(&clearRect, 0x0);
828 // FIXME: This should be mitchell, not bilinear.
829 paint.getTextureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
830 GrMatrix sampleM;
831 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
832 paint.getTextureSampler(0)->setMatrix(sampleM);
833 context->setRenderTarget(dstTexture->asRenderTarget());
834 paint.setTexture(0, srcTexture);
835 SkRect dstRect(srcRect);
836 scaleRect(&dstRect, scaleFactorX, scaleFactorY);
837 context->drawRectToRect(paint, dstRect, srcRect);
838 srcRect = dstRect;
839 SkTSwap(srcTexture, dstTexture);
840 }
841 context->setRenderTarget(oldRenderTarget);
842 context->setClip(oldClip);
843 return srcTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000844}
845
846static bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
847 SkMaskFilter* filter, const SkMatrix& matrix,
848 const SkRegion& clip, SkBounder* bounder,
849 GrPaint* grp) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000850#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000851 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000852#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000853 SkMaskFilter::BlurInfo info;
854 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000855 if (SkMaskFilter::kNone_BlurType == blurType ||
856 !context->supportsShaders()) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000857 return false;
858 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000859 SkScalar radius = info.fIgnoreTransform ? info.fRadius
860 : matrix.mapRadius(info.fRadius);
861 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000862 if (radius <= 0) {
863 return false;
864 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000865 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000866 float sigma3 = sigma * 3.0f;
867
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000868 SkRect srcRect = path.getBounds();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000869 SkRect clipRect;
870 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000871
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000872 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
873 srcRect.inset(-sigma3, -sigma3);
874 clipRect.inset(-sigma3, -sigma3);
875 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000876 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000877 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);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000886 srcRect.offset(offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000887 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
tomhudson@google.comf74ad8c2011-11-09 22:15:08 +0000894 { kRGBA_8888_PM_GrPixelConfig }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000895 };
896
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000897 GrAutoScratchTexture pathEntry(context, desc);
898 GrTexture* pathTexture = pathEntry.texture();
899 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000900 return false;
901 }
902 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000903 // Once this code moves into GrContext, this should be changed to use
904 // an AutoClipRestore.
905 GrClip oldClip = context->getClip();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000906 context->setRenderTarget(pathTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000907 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000908 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000909 GrPaint tempPaint;
910 tempPaint.reset();
911
912 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000913 tempPaint.fAntiAlias = grp->fAntiAlias;
914 if (tempPaint.fAntiAlias) {
915 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
916 // blend coeff of zero requires dual source blending support in order
917 // to properly blend partially covered pixels. This means the AA
918 // code path may not be taken. So we use a dst blend coeff of ISA. We
919 // could special case AA draws to a dst surface with known alpha=0 to
920 // use a zero dst coeff when dual source blending isn't available.
921 tempPaint.fSrcBlendCoeff = kOne_BlendCoeff;
922 tempPaint.fDstBlendCoeff = kISC_BlendCoeff;
923 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000924 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000925 context->drawPath(tempPaint, path, skToGrFillType(path.getFillType()), &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000926
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000927 GrAutoScratchTexture temp1, temp2;
928 // If we're doing a normal blur, we can clobber the pathTexture in the
929 // gaussianBlur. Otherwise, we need to save it for later compositing.
930 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
931 GrTexture* blurTexture = gaussianBlur(context, pathTexture,
932 &temp1, isNormalBlur ? NULL : &temp2,
933 srcRect, sigma, sigma);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000934
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000935 if (!isNormalBlur) {
936 GrPaint paint;
937 paint.reset();
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000938 paint.getTextureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000939 GrMatrix sampleM;
940 sampleM.setIDiv(pathTexture->width(), pathTexture->height());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000941 paint.getTextureSampler(0)->setMatrix(sampleM);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000942 // Blend pathTexture over blurTexture.
943 context->setRenderTarget(blurTexture->asRenderTarget());
944 paint.setTexture(0, pathTexture);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000945 if (SkMaskFilter::kInner_BlurType == blurType) {
946 // inner: dst = dst * src
947 paint.fSrcBlendCoeff = kDC_BlendCoeff;
948 paint.fDstBlendCoeff = kZero_BlendCoeff;
949 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
950 // solid: dst = src + dst - src * dst
951 // = (1 - dst) * src + 1 * dst
952 paint.fSrcBlendCoeff = kIDC_BlendCoeff;
953 paint.fDstBlendCoeff = kOne_BlendCoeff;
954 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
955 // outer: dst = dst * (1 - src)
956 // = 0 * src + (1 - src) * dst
957 paint.fSrcBlendCoeff = kZero_BlendCoeff;
958 paint.fDstBlendCoeff = kISC_BlendCoeff;
959 }
960 context->drawRect(paint, srcRect);
961 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000962 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000963 context->setClip(oldClip);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000964
965 if (grp->hasTextureOrMask()) {
966 GrMatrix inverse;
967 if (!matrix.invert(&inverse)) {
968 return false;
969 }
970 grp->preConcatActiveSamplerMatrices(inverse);
971 }
972
973 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
974 // we assume the last mask index is available for use
975 GrAssert(NULL == grp->getMask(MASK_IDX));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000976 grp->setMask(MASK_IDX, blurTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000977 grp->getMaskSampler(MASK_IDX)->setClampNoFilter();
978
979 GrMatrix m;
980 m.setTranslate(-finalRect.fLeft, -finalRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000981 m.postIDiv(blurTexture->width(), blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000982 grp->getMaskSampler(MASK_IDX)->setMatrix(m);
983 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000984 return true;
985}
986
reed@google.com69302852011-02-16 18:08:07 +0000987static bool drawWithMaskFilter(GrContext* context, const SkPath& path,
988 SkMaskFilter* filter, const SkMatrix& matrix,
989 const SkRegion& clip, SkBounder* bounder,
990 GrPaint* grp) {
991 SkMask srcM, dstM;
992
993 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
994 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
995 return false;
996 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000997 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000998
999 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
1000 return false;
1001 }
1002 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +00001003 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +00001004
1005 if (clip.quickReject(dstM.fBounds)) {
1006 return false;
1007 }
1008 if (bounder && !bounder->doIRect(dstM.fBounds)) {
1009 return false;
1010 }
1011
1012 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
1013 // the current clip (and identity matrix) and grpaint settings
1014
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001015 // used to compute inverse view, if necessary
1016 GrMatrix ivm = context->getMatrix();
1017
reed@google.com0c219b62011-02-16 21:31:18 +00001018 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +00001019
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001020 const GrTextureDesc desc = {
1021 kNone_GrTextureFlags,
1022 kNone_GrAALevel,
reed@google.com69302852011-02-16 18:08:07 +00001023 dstM.fBounds.width(),
1024 dstM.fBounds.height(),
tomhudson@google.comf74ad8c2011-11-09 22:15:08 +00001025 { kAlpha_8_GrPixelConfig }
reed@google.com69302852011-02-16 18:08:07 +00001026 };
1027
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001028 GrAutoScratchTexture ast(context, desc);
1029 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001030
reed@google.com69302852011-02-16 18:08:07 +00001031 if (NULL == texture) {
1032 return false;
1033 }
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001034 texture->uploadTextureData(0, 0, desc.fWidth, desc.fHeight,
1035 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +00001036
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001037 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
1038 grp->preConcatActiveSamplerMatrices(ivm);
1039 }
1040
1041 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
1042 // we assume the last mask index is available for use
1043 GrAssert(NULL == grp->getMask(MASK_IDX));
1044 grp->setMask(MASK_IDX, texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001045 grp->getMaskSampler(MASK_IDX)->setClampNoFilter();
reed@google.com69302852011-02-16 18:08:07 +00001046
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001047 GrRect d;
1048 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +00001049 GrIntToScalar(dstM.fBounds.fTop),
1050 GrIntToScalar(dstM.fBounds.fRight),
1051 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001052
1053 GrMatrix m;
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +00001054 m.setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
1055 -dstM.fBounds.fTop*SK_Scalar1);
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001056 m.postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001057 grp->getMaskSampler(MASK_IDX)->setMatrix(m);
1058
1059 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +00001060 return true;
1061}
reed@google.com69302852011-02-16 18:08:07 +00001062
reed@google.com0c219b62011-02-16 21:31:18 +00001063void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
reed@google.comfe626382011-09-21 13:50:35 +00001064 const SkPaint& origPaint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +00001065 bool pathIsMutable) {
1066 CHECK_SHOULD_DRAW(draw);
1067
reed@google.comfe626382011-09-21 13:50:35 +00001068 bool doFill = true;
1069 SkTLazy<SkPaint> lazyPaint;
1070 const SkPaint* paint = &origPaint;
1071
1072 // can we cheat, and threat a thin stroke as a hairline (w/ modulated alpha)
1073 // if we can, we draw lots faster (raster device does this same test)
1074 {
1075 SkAlpha newAlpha;
1076 if (SkDrawTreatAsHairline(*paint, *draw.fMatrix, &newAlpha)) {
1077 lazyPaint.set(*paint);
1078 lazyPaint.get()->setAlpha(newAlpha);
1079 lazyPaint.get()->setStrokeWidth(0);
1080 paint = lazyPaint.get();
1081 doFill = false;
1082 }
1083 }
1084 // must reference paint from here down, and not origPaint
1085 // since we may have change the paint (using lazyPaint for storage)
1086
bsalomon@google.com5782d712011-01-21 21:03:59 +00001087 GrPaint grPaint;
1088 SkAutoCachedTexture act;
reed@google.comfe626382011-09-21 13:50:35 +00001089 if (!this->skPaint2GrPaintShader(*paint,
Scroggod757df22011-05-16 13:11:16 +00001090 &act,
1091 *draw.fMatrix,
1092 &grPaint,
1093 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001094 return;
1095 }
1096
reed@google.comfe626382011-09-21 13:50:35 +00001097 // If we have a prematrix, apply it to the path, optimizing for the case
1098 // where the original path can in fact be modified in place (even though
1099 // its parameter type is const).
1100 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1101 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001102
1103 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001104 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001105
reed@google.come3445642011-02-16 23:20:39 +00001106 if (!pathIsMutable) {
1107 result = &tmpPath;
1108 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001109 }
reed@google.come3445642011-02-16 23:20:39 +00001110 // should I push prePathMatrix on our MV stack temporarily, instead
1111 // of applying it here? See SkDraw.cpp
1112 pathPtr->transform(*prePathMatrix, result);
1113 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001114 }
reed@google.com0c219b62011-02-16 21:31:18 +00001115 // at this point we're done with prePathMatrix
1116 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001117
reed@google.comfe626382011-09-21 13:50:35 +00001118 if (doFill && (paint->getPathEffect() ||
1119 paint->getStyle() != SkPaint::kFill_Style)) {
1120 // it is safe to use tmpPath here, even if we already used it for the
1121 // prepathmatrix, since getFillPath can take the same object for its
1122 // input and output safely.
1123 doFill = paint->getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001124 pathPtr = &tmpPath;
1125 }
1126
reed@google.comfe626382011-09-21 13:50:35 +00001127 if (paint->getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001128 // avoid possibly allocating a new path in transform if we can
1129 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1130
1131 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001132 pathPtr->transform(*draw.fMatrix, devPathPtr);
reed@google.comfe626382011-09-21 13:50:35 +00001133 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint->getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001134 *draw.fMatrix, *draw.fClip, draw.fBounder,
1135 &grPaint)) {
reed@google.comfe626382011-09-21 13:50:35 +00001136 drawWithMaskFilter(fContext, *devPathPtr, paint->getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001137 *draw.fMatrix, *draw.fClip, draw.fBounder,
1138 &grPaint);
1139 }
reed@google.com69302852011-02-16 18:08:07 +00001140 return;
1141 }
reed@google.com69302852011-02-16 18:08:07 +00001142
bsalomon@google.comffca4002011-02-22 20:34:01 +00001143 GrPathFill fill = kHairLine_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001144
reed@google.com0c219b62011-02-16 21:31:18 +00001145 if (doFill) {
1146 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001147 case SkPath::kWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001148 fill = kWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001149 break;
1150 case SkPath::kEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001151 fill = kEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001152 break;
1153 case SkPath::kInverseWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001154 fill = kInverseWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001155 break;
1156 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001157 fill = kInverseEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001158 break;
1159 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001160 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001161 return;
1162 }
1163 }
1164
reed@google.com07f3ee12011-05-16 17:21:57 +00001165 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001166}
1167
reed@google.comac10a2d2010-12-22 21:39:39 +00001168void SkGpuDevice::drawBitmap(const SkDraw& draw,
1169 const SkBitmap& bitmap,
1170 const SkIRect* srcRectPtr,
1171 const SkMatrix& m,
1172 const SkPaint& paint) {
1173 CHECK_SHOULD_DRAW(draw);
1174
1175 SkIRect srcRect;
1176 if (NULL == srcRectPtr) {
1177 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1178 } else {
1179 srcRect = *srcRectPtr;
1180 }
1181
junov@google.comd935cfb2011-06-27 20:48:23 +00001182 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001183 // Convert the bitmap to a shader so that the rect can be drawn
1184 // through drawRect, which supports mask filters.
1185 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001186 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001187 if (srcRectPtr) {
1188 if (!bitmap.extractSubset(&tmp, srcRect)) {
1189 return; // extraction failed
1190 }
1191 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001192 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001193 }
1194 SkPaint paintWithTexture(paint);
1195 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1196 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001197 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001198 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001199
junov@google.com1d329782011-07-28 20:10:09 +00001200 // Transform 'm' needs to be concatenated to the draw matrix,
1201 // rather than transforming the primitive directly, so that 'm' will
1202 // also affect the behavior of the mask filter.
1203 SkMatrix drawMatrix;
1204 drawMatrix.setConcat(*draw.fMatrix, m);
1205 SkDraw transformedDraw(draw);
1206 transformedDraw.fMatrix = &drawMatrix;
1207
1208 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1209
junov@google.comd935cfb2011-06-27 20:48:23 +00001210 return;
1211 }
1212
bsalomon@google.com5782d712011-01-21 21:03:59 +00001213 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001214 if (!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001215 return;
1216 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001217 GrSamplerState* sampler = grPaint.getTextureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001218 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001219 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001220 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001221 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001222 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001223
bsalomon@google.com91958362011-06-13 17:58:13 +00001224 const int maxTextureSize = fContext->getMaxTextureSize();
1225 if (bitmap.getTexture() || (bitmap.width() <= maxTextureSize &&
1226 bitmap.height() <= maxTextureSize)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001227 // take the fast case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001228 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001229 return;
1230 }
1231
1232 // undo the translate done by SkCanvas
1233 int DX = SkMax32(0, srcRect.fLeft);
1234 int DY = SkMax32(0, srcRect.fTop);
1235 // compute clip bounds in local coordinates
1236 SkIRect clipRect;
1237 {
1238 SkRect r;
1239 r.set(draw.fClip->getBounds());
1240 SkMatrix matrix, inverse;
1241 matrix.setConcat(*draw.fMatrix, m);
1242 if (!matrix.invert(&inverse)) {
1243 return;
1244 }
1245 inverse.mapRect(&r);
1246 r.roundOut(&clipRect);
1247 // apply the canvas' translate to our local clip
1248 clipRect.offset(DX, DY);
1249 }
1250
bsalomon@google.com91958362011-06-13 17:58:13 +00001251 int nx = bitmap.width() / maxTextureSize;
1252 int ny = bitmap.height() / maxTextureSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001253 for (int x = 0; x <= nx; x++) {
1254 for (int y = 0; y <= ny; y++) {
1255 SkIRect tileR;
bsalomon@google.com91958362011-06-13 17:58:13 +00001256 tileR.set(x * maxTextureSize, y * maxTextureSize,
1257 (x + 1) * maxTextureSize, (y + 1) * maxTextureSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001258 if (!SkIRect::Intersects(tileR, clipRect)) {
1259 continue;
1260 }
1261
1262 SkIRect srcR = tileR;
1263 if (!srcR.intersect(srcRect)) {
1264 continue;
1265 }
1266
1267 SkBitmap tmpB;
1268 if (bitmap.extractSubset(&tmpB, tileR)) {
1269 // now offset it to make it "local" to our tmp bitmap
1270 srcR.offset(-tileR.fLeft, -tileR.fTop);
1271
1272 SkMatrix tmpM(m);
1273 {
1274 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1275 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1276 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1277 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001278 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001279 }
1280 }
1281 }
1282}
1283
1284/*
1285 * This is called by drawBitmap(), which has to handle images that may be too
1286 * large to be represented by a single texture.
1287 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001288 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1289 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001290 */
1291void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1292 const SkBitmap& bitmap,
1293 const SkIRect& srcRect,
1294 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001295 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001296 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1297 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001298
reed@google.com9c49bc32011-07-07 13:42:37 +00001299 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001300 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001301 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001302 return;
1303 }
1304
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001305 GrSamplerState* sampler = grPaint->getTextureSampler(kBitmapTextureIdx);
1306
1307 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1308 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
1309 sampler->setSampleMode(GrSamplerState::kNormal_SampleMode);
1310 sampler->setMatrix(GrMatrix::I());
reed@google.comac10a2d2010-12-22 21:39:39 +00001311
1312 GrTexture* texture;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001313 SkAutoCachedTexture act(this, bitmap, *sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001314 if (NULL == texture) {
1315 return;
1316 }
1317
bsalomon@google.com452943d2011-10-31 17:37:14 +00001318 grPaint->setTexture(kBitmapTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001319
reed@google.com20efde72011-05-09 17:00:02 +00001320 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1321 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001322 GrRect paintRect;
junov@google.com6acc9b32011-05-16 18:32:07 +00001323 paintRect.setLTRB(GrFixedToScalar((srcRect.fLeft << 16) / bitmap.width()),
1324 GrFixedToScalar((srcRect.fTop << 16) / bitmap.height()),
1325 GrFixedToScalar((srcRect.fRight << 16) / bitmap.width()),
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001326 GrFixedToScalar((srcRect.fBottom << 16) / bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001327
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001328 if (GrSamplerState::kNearest_Filter != sampler->getFilter() &&
junov@google.com6acc9b32011-05-16 18:32:07 +00001329 (srcRect.width() < bitmap.width() ||
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001330 srcRect.height() < bitmap.height())) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001331 // If drawing a subrect of the bitmap and filtering is enabled,
1332 // use a constrained texture domain to avoid color bleeding
1333 GrScalar left, top, right, bottom;
1334 if (srcRect.width() > 1) {
1335 GrScalar border = GR_ScalarHalf / bitmap.width();
1336 left = paintRect.left() + border;
1337 right = paintRect.right() - border;
1338 } else {
1339 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1340 }
1341 if (srcRect.height() > 1) {
1342 GrScalar border = GR_ScalarHalf / bitmap.height();
1343 top = paintRect.top() + border;
1344 bottom = paintRect.bottom() - border;
1345 } else {
1346 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1347 }
1348 GrRect textureDomain;
1349 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001350 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001351 }
1352
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001353 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001354}
1355
1356void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1357 int left, int top, const SkPaint& paint) {
1358 CHECK_SHOULD_DRAW(draw);
1359
1360 SkAutoLockPixels alp(bitmap);
1361 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1362 return;
1363 }
1364
bsalomon@google.com5782d712011-01-21 21:03:59 +00001365 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001366 if(!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001367 return;
1368 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001369
bsalomon@google.com5782d712011-01-21 21:03:59 +00001370 GrAutoMatrix avm(fContext, GrMatrix::I());
1371
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001372 GrSamplerState* sampler = grPaint.getTextureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001373
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001374 GrTexture* texture;
1375 sampler->setClampNoFilter();
1376 SkAutoCachedTexture act(this, bitmap, *sampler, &texture);
1377
1378 grPaint.setTexture(kBitmapTextureIdx, texture);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001379
bsalomon@google.com5782d712011-01-21 21:03:59 +00001380 fContext->drawRectToRect(grPaint,
reed@google.com20efde72011-05-09 17:00:02 +00001381 GrRect::MakeXYWH(GrIntToScalar(left),
1382 GrIntToScalar(top),
1383 GrIntToScalar(bitmap.width()),
1384 GrIntToScalar(bitmap.height())),
1385 GrRect::MakeWH(GR_Scalar1, GR_Scalar1));
reed@google.comac10a2d2010-12-22 21:39:39 +00001386}
1387
1388void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* dev,
1389 int x, int y, const SkPaint& paint) {
1390 CHECK_SHOULD_DRAW(draw);
1391
bsalomon@google.com5782d712011-01-21 21:03:59 +00001392 GrPaint grPaint;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001393 if (!((SkGpuDevice*)dev)->bindDeviceAsTexture(&grPaint) ||
Scroggod757df22011-05-16 13:11:16 +00001394 !this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001395 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001396 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001397
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001398 GrTexture* devTex = grPaint.getTexture(0);
1399 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001400
1401 const SkBitmap& bm = dev->accessBitmap(false);
1402 int w = bm.width();
1403 int h = bm.height();
1404
1405 GrAutoMatrix avm(fContext, GrMatrix::I());
1406
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001407 grPaint.getTextureSampler(kBitmapTextureIdx)->setClampNoFilter();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001408
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001409 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1410 GrIntToScalar(y),
1411 GrIntToScalar(w),
1412 GrIntToScalar(h));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +00001413 SkImageFilter* imageFilter = paint.getImageFilter();
1414 SkSize size;
1415 if (NULL != imageFilter && imageFilter->asABlur(&size)) {
1416 GrAutoScratchTexture temp1, temp2;
1417 GrTexture* blurTexture = gaussianBlur(fContext,
1418 devTex, &temp1, &temp2,
1419 GrRect::MakeWH(w, h),
1420 size.width(),
1421 size.height());
1422 grPaint.setTexture(kBitmapTextureIdx, blurTexture);
1423 devTex = blurTexture;
1424 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001425 // The device being drawn may not fill up its texture (saveLayer uses
1426 // the approximate ).
1427 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1428 GR_Scalar1 * h / devTex->height());
1429
1430 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001431}
1432
1433///////////////////////////////////////////////////////////////////////////////
1434
1435// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001436static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1437 kTriangles_PrimitiveType,
1438 kTriangleStrip_PrimitiveType,
1439 kTriangleFan_PrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001440};
1441
1442void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1443 int vertexCount, const SkPoint vertices[],
1444 const SkPoint texs[], const SkColor colors[],
1445 SkXfermode* xmode,
1446 const uint16_t indices[], int indexCount,
1447 const SkPaint& paint) {
1448 CHECK_SHOULD_DRAW(draw);
1449
bsalomon@google.com5782d712011-01-21 21:03:59 +00001450 GrPaint grPaint;
1451 SkAutoCachedTexture act;
1452 // we ignore the shader if texs is null.
1453 if (NULL == texs) {
Scroggod757df22011-05-16 13:11:16 +00001454 if (!this->skPaint2GrPaintNoShader(paint,
1455 false,
1456 &grPaint,
1457 NULL == colors)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001458 return;
1459 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001460 } else {
reed@google.com1a2e8d22011-01-21 22:08:29 +00001461 if (!this->skPaint2GrPaintShader(paint, &act,
1462 *draw.fMatrix,
Scroggod757df22011-05-16 13:11:16 +00001463 &grPaint,
1464 NULL == colors)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001465 return;
1466 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001467 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001468
1469 if (NULL != xmode && NULL != texs && NULL != colors) {
1470 SkXfermode::Mode mode;
1471 if (!SkXfermode::IsMode(xmode, &mode) ||
1472 SkXfermode::kMultiply_Mode != mode) {
1473 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1474#if 0
1475 return
1476#endif
1477 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001478 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001479
bsalomon@google.com498776a2011-08-16 19:20:44 +00001480 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1481 if (NULL != colors) {
1482 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001483 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001484 for (int i = 0; i < vertexCount; ++i) {
1485 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1486 }
1487 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001488 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001489 fContext->drawVertices(grPaint,
1490 gVertexMode2PrimitiveType[vmode],
1491 vertexCount,
1492 (GrPoint*) vertices,
1493 (GrPoint*) texs,
1494 colors,
1495 indices,
1496 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001497}
1498
1499///////////////////////////////////////////////////////////////////////////////
1500
1501static void GlyphCacheAuxProc(void* data) {
1502 delete (GrFontScaler*)data;
1503}
1504
1505static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1506 void* auxData;
1507 GrFontScaler* scaler = NULL;
1508 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1509 scaler = (GrFontScaler*)auxData;
1510 }
1511 if (NULL == scaler) {
1512 scaler = new SkGrFontScaler(cache);
1513 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1514 }
1515 return scaler;
1516}
1517
1518static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1519 SkFixed fx, SkFixed fy,
1520 const SkGlyph& glyph) {
1521 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1522
1523 GrSkDrawProcs* procs = (GrSkDrawProcs*)state.fDraw->fProcs;
1524
1525 if (NULL == procs->fFontScaler) {
1526 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1527 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001528
1529 /*
reed@google.com3b139f52011-06-07 17:56:25 +00001530 * What should we do with fy? (assuming horizontal/latin text)
reed@google.com39ce0ac2011-04-08 15:42:19 +00001531 *
reed@google.com3b139f52011-06-07 17:56:25 +00001532 * The raster code calls SkFixedFloorToFixed on it, as it does with fx.
1533 * It calls that rather than round, because our caller has already added
1534 * SK_FixedHalf, so that calling floor gives us the rounded integer.
1535 *
1536 * Test code between raster and gpu (they should draw the same)
1537 *
1538 * canvas->drawText("Hamburgefons", 12, 0, 16.5f, paint);
1539 *
1540 * Perhaps we should only perform this integralization if there is no
1541 * fExtMatrix...
reed@google.com39ce0ac2011-04-08 15:42:19 +00001542 */
reed@google.com3b139f52011-06-07 17:56:25 +00001543 fy = SkFixedFloorToFixed(fy);
1544
reed@google.comac10a2d2010-12-22 21:39:39 +00001545 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), fx, 0),
reed@google.com3b139f52011-06-07 17:56:25 +00001546 SkFixedFloorToFixed(fx), fy,
reed@google.comac10a2d2010-12-22 21:39:39 +00001547 procs->fFontScaler);
1548}
1549
bsalomon@google.com5782d712011-01-21 21:03:59 +00001550SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001551
1552 // deferred allocation
1553 if (NULL == fDrawProcs) {
1554 fDrawProcs = new GrSkDrawProcs;
1555 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1556 fDrawProcs->fContext = fContext;
1557 }
1558
1559 // init our (and GL's) state
1560 fDrawProcs->fTextContext = context;
1561 fDrawProcs->fFontScaler = NULL;
1562 return fDrawProcs;
1563}
1564
1565void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1566 size_t byteLength, SkScalar x, SkScalar y,
1567 const SkPaint& paint) {
1568 CHECK_SHOULD_DRAW(draw);
1569
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001570 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001571 // this guy will just call our drawPath()
1572 draw.drawText((const char*)text, byteLength, x, y, paint);
1573 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001574 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001575
1576 GrPaint grPaint;
1577 SkAutoCachedTexture act;
1578
Scroggod757df22011-05-16 13:11:16 +00001579 if (!this->skPaint2GrPaintShader(paint,
1580 &act,
1581 *draw.fMatrix,
1582 &grPaint,
1583 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001584 return;
1585 }
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001586 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001587 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001588 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1589 }
1590}
1591
1592void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1593 size_t byteLength, const SkScalar pos[],
1594 SkScalar constY, int scalarsPerPos,
1595 const SkPaint& paint) {
1596 CHECK_SHOULD_DRAW(draw);
1597
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001598 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001599 // this guy will just call our drawPath()
1600 draw.drawPosText((const char*)text, byteLength, pos, constY,
1601 scalarsPerPos, paint);
1602 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001603 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001604
1605 GrPaint grPaint;
1606 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +00001607 if (!this->skPaint2GrPaintShader(paint,
1608 &act,
1609 *draw.fMatrix,
1610 &grPaint,
1611 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001612 return;
1613 }
1614
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001615 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001616 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001617 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1618 scalarsPerPos, paint);
1619 }
1620}
1621
1622void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1623 size_t len, const SkPath& path,
1624 const SkMatrix* m, const SkPaint& paint) {
1625 CHECK_SHOULD_DRAW(draw);
1626
1627 SkASSERT(draw.fDevice == this);
1628 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1629}
1630
1631///////////////////////////////////////////////////////////////////////////////
1632
reed@google.comf67e4cf2011-03-15 20:56:58 +00001633bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1634 if (!paint.isLCDRenderText()) {
1635 // we're cool with the paint as is
1636 return false;
1637 }
1638
1639 if (paint.getShader() ||
1640 paint.getXfermode() || // unless its srcover
1641 paint.getMaskFilter() ||
1642 paint.getRasterizer() ||
1643 paint.getColorFilter() ||
1644 paint.getPathEffect() ||
1645 paint.isFakeBoldText() ||
1646 paint.getStyle() != SkPaint::kFill_Style) {
1647 // turn off lcd
1648 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1649 flags->fHinting = paint.getHinting();
1650 return true;
1651 }
1652 // we're cool with the paint as is
1653 return false;
1654}
1655
1656///////////////////////////////////////////////////////////////////////////////
1657
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001658SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(const SkBitmap& bitmap,
1659 const GrSamplerState& sampler,
1660 TexType type) {
1661 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001662 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001663
bsalomon@google.come97f0852011-06-17 13:10:25 +00001664 if (kBitmap_TexType != type) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001665 const GrTextureDesc desc = {
1666 kRenderTarget_GrTextureFlagBit,
1667 kNone_GrAALevel,
1668 bitmap.width(),
1669 bitmap.height(),
tomhudson@google.comf74ad8c2011-11-09 22:15:08 +00001670 { SkGr::Bitmap2PixelConfig(bitmap) }
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001671 };
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001672 GrContext::ScratchTexMatch match;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001673 if (kSaveLayerDeviceRenderTarget_TexType == type) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001674 // we know layers will only be drawn through drawDevice.
1675 // drawDevice has been made to work with content embedded in a
1676 // larger texture so its okay to use the approximate version.
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001677 match = GrContext::kApprox_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001678 } else {
bsalomon@google.come97f0852011-06-17 13:10:25 +00001679 SkASSERT(kDeviceRenderTarget_TexType == type);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001680 match = GrContext::kExact_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001681 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001682 entry = ctx->lockScratchTexture(desc, match);
reed@google.comac10a2d2010-12-22 21:39:39 +00001683 } else {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001684 if (!bitmap.isVolatile()) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001685 GrContext::TextureKey key = bitmap.getGenerationID();
1686 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
junov@google.com4ee7ae52011-06-30 17:30:49 +00001687
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001688 entry = ctx->findAndLockTexture(key, bitmap.width(),
1689 bitmap.height(), sampler);
1690 if (NULL == entry.texture()) {
1691 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
junov@google.com4ee7ae52011-06-30 17:30:49 +00001692 bitmap);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001693 }
junov@google.com4ee7ae52011-06-30 17:30:49 +00001694 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001695 entry = sk_gr_create_bitmap_texture(ctx, gUNCACHED_KEY, sampler, bitmap);
junov@google.com4ee7ae52011-06-30 17:30:49 +00001696 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001697 if (NULL == entry.texture()) {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001698 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1699 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001700 }
1701 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001702 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001703}
1704
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001705void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1706 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001707}
1708
bsalomon@google.come97f0852011-06-17 13:10:25 +00001709SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1710 int width, int height,
1711 bool isOpaque,
1712 Usage usage) {
1713 return SkNEW_ARGS(SkGpuDevice,(this->context(), config,
1714 width, height, usage));
1715}
1716