blob: 7230516316dcec81e08608dea95097f310d5ea40 [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 +0000302// This can be removed when temporary code in writePixels is removed
303#include "SkConfig8888.h"
304
305void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
306 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000307 SkAutoLockPixels alp(bitmap);
308 if (!bitmap.readyToDraw()) {
309 return;
310 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000311
312 GrPixelConfig config;
313 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
314 config = config8888_to_gr_config(config8888);
315 } else {
316 config= SkGr::BitmapConfig2PixelConfig(bitmap.config(),
317 bitmap.isOpaque());
318 }
319
320 // Temporary until we add support for drawing from an unpremul config in
321 // GrContext
322 const SkBitmap* src = &bitmap;
323 SkBitmap tmp;
324 if (GrPixelConfigIsUnpremultiplied(config)) {
325 config = kSkia8888_PM_GrPixelConfig;
326 tmp.setConfig(SkBitmap::kARGB_8888_Config,
327 bitmap.width(), bitmap.height());
328 if (!tmp.allocPixels()) {
329 return;
330 }
331 SkAutoLockPixels alp(bitmap);
332 uint32_t* pixels = reinterpret_cast<uint32_t*>(bitmap.getPixels());
333 SkCopyConfig8888ToBitmap(tmp, pixels, bitmap.rowBytes(), config8888);
334 src = &tmp;
335 }
336
reed@google.comac10a2d2010-12-22 21:39:39 +0000337 fContext->setRenderTarget(fRenderTarget);
338 // we aren't setting the clip or matrix, so mark as dirty
339 // we don't need to set them for this call and don't have them anyway
340 fNeedPrepareRenderTarget = true;
341
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000342 fContext->writePixels(x, y, src->width(), src->height(),
343 config, src->getPixels(), src->rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000344}
345
346///////////////////////////////////////////////////////////////////////////////
347
348static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000349 const SkClipStack& clipStack,
reed@google.com6f8f2922011-03-04 22:27:10 +0000350 const SkRegion& clipRegion,
351 const SkIPoint& origin) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000352 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000353
354 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000355 iter.reset(clipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000356 const SkIRect& skBounds = clipRegion.getBounds();
357 GrRect bounds;
358 bounds.setLTRB(GrIntToScalar(skBounds.fLeft),
359 GrIntToScalar(skBounds.fTop),
360 GrIntToScalar(skBounds.fRight),
361 GrIntToScalar(skBounds.fBottom));
reed@google.com6f8f2922011-03-04 22:27:10 +0000362 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
363 &bounds);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000364 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000365}
366
367// call this ever each draw call, to ensure that the context reflects our state,
368// and not the state from some other canvas/device
369void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
370 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000371 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000372
373 fContext->setRenderTarget(fRenderTarget);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000374 SkASSERT(draw.fClipStack);
375 convert_matrixclip(fContext, *draw.fMatrix,
reed@google.com6f8f2922011-03-04 22:27:10 +0000376 *draw.fClipStack, *draw.fClip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000377 fNeedPrepareRenderTarget = false;
378 }
379}
380
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000381void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
382 const SkClipStack& clipStack) {
383 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
384 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000385 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000386}
387
388void SkGpuDevice::gainFocus(SkCanvas* canvas, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000389 const SkRegion& clip, const SkClipStack& clipStack) {
390
reed@google.comac10a2d2010-12-22 21:39:39 +0000391 fContext->setRenderTarget(fRenderTarget);
392
bsalomon@google.comd302f142011-03-03 13:54:13 +0000393 this->INHERITED::gainFocus(canvas, matrix, clip, clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000394
reed@google.com6f8f2922011-03-04 22:27:10 +0000395 convert_matrixclip(fContext, matrix, clipStack, clip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000396
397 if (fNeedClear) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000398 fContext->clear(NULL, 0x0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000399 fNeedClear = false;
400 }
401}
402
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000403bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000404 if (NULL != fTexture) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000405 paint->setTexture(kBitmapTextureIdx, fTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000406 return true;
407 }
408 return false;
409}
410
411///////////////////////////////////////////////////////////////////////////////
412
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000413SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
414SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
415SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
416SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
417SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
418 shader_type_mismatch);
419SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 4, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000420
bsalomon@google.com5782d712011-01-21 21:03:59 +0000421static const GrSamplerState::SampleMode sk_bmp_type_to_sample_mode[] = {
422 (GrSamplerState::SampleMode) -1, // kNone_BitmapType
423 GrSamplerState::kNormal_SampleMode, // kDefault_BitmapType
424 GrSamplerState::kRadial_SampleMode, // kRadial_BitmapType
425 GrSamplerState::kSweep_SampleMode, // kSweep_BitmapType
426 GrSamplerState::kRadial2_SampleMode, // kTwoPointRadial_BitmapType
427};
428
429bool SkGpuDevice::skPaint2GrPaintNoShader(const SkPaint& skPaint,
430 bool justAlpha,
Scroggod757df22011-05-16 13:11:16 +0000431 GrPaint* grPaint,
432 bool constantColor) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000433
434 grPaint->fDither = skPaint.isDither();
435 grPaint->fAntiAlias = skPaint.isAntiAlias();
436
437 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
438 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
439
440 SkXfermode* mode = skPaint.getXfermode();
441 if (mode) {
442 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000443 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000444#if 0
445 return false;
446#endif
447 }
448 }
449 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
450 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
451
452 if (justAlpha) {
453 uint8_t alpha = skPaint.getAlpha();
454 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000455 // justAlpha is currently set to true only if there is a texture,
456 // so constantColor should not also be true.
457 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000458 } else {
459 grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000460 grPaint->setTexture(kShaderTextureIdx, NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000461 }
Scroggo97c88c22011-05-11 14:05:25 +0000462 SkColorFilter* colorFilter = skPaint.getColorFilter();
463 SkColor color;
464 SkXfermode::Mode filterMode;
465 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
Scroggod757df22011-05-16 13:11:16 +0000466 if (!constantColor) {
467 grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color);
468 grPaint->fColorFilterXfermode = filterMode;
469 return true;
470 }
471 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
472 grPaint->fColor = SkGr::SkColor2GrColor(filtered);
Scroggo97c88c22011-05-11 14:05:25 +0000473 }
Scroggod757df22011-05-16 13:11:16 +0000474 grPaint->resetColorFilter();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000475 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000476}
477
bsalomon@google.com5782d712011-01-21 21:03:59 +0000478bool SkGpuDevice::skPaint2GrPaintShader(const SkPaint& skPaint,
479 SkAutoCachedTexture* act,
480 const SkMatrix& ctm,
Scroggod757df22011-05-16 13:11:16 +0000481 GrPaint* grPaint,
482 bool constantColor) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000483
bsalomon@google.com5782d712011-01-21 21:03:59 +0000484 SkASSERT(NULL != act);
reed@google.comac10a2d2010-12-22 21:39:39 +0000485
bsalomon@google.com5782d712011-01-21 21:03:59 +0000486 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000487 if (NULL == shader) {
Scroggod757df22011-05-16 13:11:16 +0000488 return this->skPaint2GrPaintNoShader(skPaint,
489 false,
490 grPaint,
491 constantColor);
492 } else if (!this->skPaint2GrPaintNoShader(skPaint, true, grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000493 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000494 }
495
reed@google.comac10a2d2010-12-22 21:39:39 +0000496 SkBitmap bitmap;
497 SkMatrix matrix;
498 SkShader::TileMode tileModes[2];
499 SkScalar twoPointParams[3];
500 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, &matrix,
501 tileModes, twoPointParams);
502
bsalomon@google.com5782d712011-01-21 21:03:59 +0000503 GrSamplerState::SampleMode sampleMode = sk_bmp_type_to_sample_mode[bmptype];
504 if (-1 == sampleMode) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000505 SkShader::GradientInfo info;
506 SkColor color;
507
508 info.fColors = &color;
509 info.fColorOffsets = NULL;
510 info.fColorCount = 1;
511 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
512 SkPaint copy(skPaint);
513 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000514 // modulate the paint alpha by the shader's solid color alpha
515 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
516 copy.setColor(SkColorSetA(color, newA));
reed@google.com2be9e8b2011-07-06 21:18:09 +0000517 return this->skPaint2GrPaintNoShader(copy,
518 false,
519 grPaint,
520 constantColor);
521 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000522 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000523 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000524 GrSamplerState* sampler = grPaint->getTextureSampler(kShaderTextureIdx);
525 sampler->setSampleMode(sampleMode);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000526 if (skPaint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000527 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000528 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000529 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000530 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000531 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
532 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000533 if (GrSamplerState::kRadial2_SampleMode == sampleMode) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000534 sampler->setRadial2Params(twoPointParams[0],
535 twoPointParams[1],
536 twoPointParams[2] < 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000537 }
538
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000539 GrTexture* texture = act->set(this, bitmap, *sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000540 if (NULL == texture) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000541 SkDebugf("Couldn't convert bitmap to texture.\n");
542 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000543 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000544 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000545
546 // since our texture coords will be in local space, we wack the texture
547 // matrix to map them back into 0...1 before we load it
548 SkMatrix localM;
549 if (shader->getLocalMatrix(&localM)) {
550 SkMatrix inverse;
551 if (localM.invert(&inverse)) {
552 matrix.preConcat(inverse);
553 }
554 }
555 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000556 GrScalar sx = GrFixedToScalar(GR_Fixed1 / bitmap.width());
557 GrScalar sy = GrFixedToScalar(GR_Fixed1 / bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +0000558 matrix.postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000559 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000560 GrScalar s = GrFixedToScalar(GR_Fixed1 / bitmap.width());
reed@google.comac10a2d2010-12-22 21:39:39 +0000561 matrix.postScale(s, s);
562 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000563 sampler->setMatrix(matrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000564
565 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000566}
567
568///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000569
bsalomon@google.com398109c2011-04-14 18:40:27 +0000570void SkGpuDevice::clear(SkColor color) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000571 fContext->clear(NULL, color);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000572}
573
reed@google.comac10a2d2010-12-22 21:39:39 +0000574void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
575 CHECK_SHOULD_DRAW(draw);
576
bsalomon@google.com5782d712011-01-21 21:03:59 +0000577 GrPaint grPaint;
578 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000579 if (!this->skPaint2GrPaintShader(paint,
580 &act,
581 *draw.fMatrix,
582 &grPaint,
583 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000584 return;
585 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000586
587 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000588}
589
590// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000591static const GrPrimitiveType gPointMode2PrimtiveType[] = {
592 kPoints_PrimitiveType,
593 kLines_PrimitiveType,
594 kLineStrip_PrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000595};
596
597void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000598 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000599 CHECK_SHOULD_DRAW(draw);
600
601 SkScalar width = paint.getStrokeWidth();
602 if (width < 0) {
603 return;
604 }
605
606 // we only handle hairlines here, else we let the SkDraw call our drawPath()
607 if (width > 0) {
608 draw.drawPoints(mode, count, pts, paint, true);
609 return;
610 }
611
bsalomon@google.com5782d712011-01-21 21:03:59 +0000612 GrPaint grPaint;
613 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000614 if (!this->skPaint2GrPaintShader(paint,
615 &act,
616 *draw.fMatrix,
617 &grPaint,
618 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000619 return;
620 }
621
bsalomon@google.com5782d712011-01-21 21:03:59 +0000622 fContext->drawVertices(grPaint,
623 gPointMode2PrimtiveType[mode],
624 count,
625 (GrPoint*)pts,
626 NULL,
627 NULL,
628 NULL,
629 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000630}
631
reed@google.comc9aa5872011-04-05 21:05:37 +0000632///////////////////////////////////////////////////////////////////////////////
633
reed@google.comac10a2d2010-12-22 21:39:39 +0000634void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
635 const SkPaint& paint) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000636 CHECK_SHOULD_DRAW(draw);
637
bungeman@google.com79bd8772011-07-18 15:34:08 +0000638 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000639 SkScalar width = paint.getStrokeWidth();
640
641 /*
642 We have special code for hairline strokes, miter-strokes, and fills.
643 Anything else we just call our path code.
644 */
645 bool usePath = doStroke && width > 0 &&
646 paint.getStrokeJoin() != SkPaint::kMiter_Join;
647 // another reason we might need to call drawPath...
648 if (paint.getMaskFilter()) {
649 usePath = true;
650 }
reed@google.com67db6642011-05-26 11:46:35 +0000651 // until we aa rotated rects...
652 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
653 usePath = true;
654 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000655 // small miter limit means right angles show bevel...
656 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
657 paint.getStrokeMiter() < SK_ScalarSqrt2)
658 {
659 usePath = true;
660 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000661 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000662 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
663 usePath = true;
664 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000665
666 if (usePath) {
667 SkPath path;
668 path.addRect(rect);
669 this->drawPath(draw, path, paint, NULL, true);
670 return;
671 }
672
673 GrPaint grPaint;
674 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000675 if (!this->skPaint2GrPaintShader(paint,
676 &act,
677 *draw.fMatrix,
678 &grPaint,
679 true)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000680 return;
681 }
reed@google.com20efde72011-05-09 17:00:02 +0000682 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000683}
684
reed@google.com69302852011-02-16 18:08:07 +0000685#include "SkMaskFilter.h"
686#include "SkBounder.h"
687
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000688static GrPathFill skToGrFillType(SkPath::FillType fillType) {
689 switch (fillType) {
690 case SkPath::kWinding_FillType:
691 return kWinding_PathFill;
692 case SkPath::kEvenOdd_FillType:
693 return kEvenOdd_PathFill;
694 case SkPath::kInverseWinding_FillType:
695 return kInverseWinding_PathFill;
696 case SkPath::kInverseEvenOdd_FillType:
697 return kInverseEvenOdd_PathFill;
698 default:
699 SkDebugf("Unsupported path fill type\n");
700 return kHairLine_PathFill;
701 }
702}
703
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000704static void buildKernel(float sigma, float* kernel, int kernelWidth) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000705 int halfWidth = (kernelWidth - 1) / 2;
706 float sum = 0.0f;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000707 float denom = 1.0f / (2.0f * sigma * sigma);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000708 for (int i = 0; i < kernelWidth; ++i) {
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000709 float x = static_cast<float>(i - halfWidth);
710 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
711 // is dropped here, since we renormalize the kernel below.
712 kernel[i] = sk_float_exp(- x * x * denom);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000713 sum += kernel[i];
714 }
715 // Normalize the kernel
716 float scale = 1.0f / sum;
717 for (int i = 0; i < kernelWidth; ++i)
718 kernel[i] *= scale;
719}
720
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000721static void scaleRect(SkRect* rect, float xScale, float yScale) {
722 rect->fLeft *= xScale;
723 rect->fTop *= yScale;
724 rect->fRight *= xScale;
725 rect->fBottom *= yScale;
726}
727
728static float adjustSigma(float sigma, int *scaleFactor, int *halfWidth,
729 int *kernelWidth) {
730 *scaleFactor = 1;
731 while (sigma > MAX_BLUR_SIGMA) {
732 *scaleFactor *= 2;
733 sigma *= 0.5f;
734 }
735 *halfWidth = static_cast<int>(ceilf(sigma * 3.0f));
736 *kernelWidth = *halfWidth * 2 + 1;
737 return sigma;
738}
739
740// Apply a Gaussian blur to srcTexture by sigmaX and sigmaY, within the given
741// rect.
742// temp1 and temp2 are used for allocation of intermediate textures.
743// If temp2 is non-NULL, srcTexture will be untouched, and the return
744// value will be either temp1 or temp2.
745// If temp2 is NULL, srcTexture will be overwritten with intermediate
746// results, and the return value will either be temp1 or srcTexture.
747static GrTexture* gaussianBlur(GrContext* context, GrTexture* srcTexture,
748 GrAutoScratchTexture* temp1,
749 GrAutoScratchTexture* temp2,
750 const SkRect& rect,
751 float sigmaX, float sigmaY) {
752
753 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
754 GrClip oldClip = context->getClip();
755 GrTexture* origTexture = srcTexture;
756 GrAutoMatrix avm(context, GrMatrix::I());
757 SkIRect clearRect;
758 int scaleFactorX, halfWidthX, kernelWidthX;
759 int scaleFactorY, halfWidthY, kernelWidthY;
760 sigmaX = adjustSigma(sigmaX, &scaleFactorX, &halfWidthX, &kernelWidthX);
761 sigmaY = adjustSigma(sigmaY, &scaleFactorY, &halfWidthY, &kernelWidthY);
762
763 SkRect srcRect(rect);
764 scaleRect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
765 srcRect.roundOut();
766 scaleRect(&srcRect, scaleFactorX, scaleFactorY);
767 context->setClip(srcRect);
768
769 const GrTextureDesc desc = {
770 kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit,
771 kNone_GrAALevel,
772 srcRect.width(),
773 srcRect.height(),
tomhudson@google.comf74ad8c2011-11-09 22:15:08 +0000774 { kRGBA_8888_GrPixelConfig }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000775 };
776
777 temp1->set(context, desc);
778 if (temp2) temp2->set(context, desc);
779
780 GrTexture* dstTexture = temp1->texture();
781 GrPaint paint;
782 paint.reset();
783 paint.getTextureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
784
785 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
786 GrMatrix sampleM;
787 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
788 paint.getTextureSampler(0)->setMatrix(sampleM);
789 context->setRenderTarget(dstTexture->asRenderTarget());
790 SkRect dstRect(srcRect);
791 scaleRect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
792 i < scaleFactorY ? 0.5f : 1.0f);
793 paint.setTexture(0, srcTexture);
794 context->drawRectToRect(paint, dstRect, srcRect);
795 srcRect = dstRect;
796 SkTSwap(srcTexture, dstTexture);
797 // If temp2 is non-NULL, don't render back to origTexture
798 if (temp2 && dstTexture == origTexture) dstTexture = temp2->texture();
799 }
800
801 if (sigmaX > 0.0f) {
802 SkAutoTMalloc<float> kernelStorageX(kernelWidthX);
803 float* kernelX = kernelStorageX.get();
804 buildKernel(sigmaX, kernelX, kernelWidthX);
805
806 if (scaleFactorX > 1) {
807 // Clear out a halfWidth to the right of the srcRect to prevent the
808 // X convolution from reading garbage.
809 clearRect = SkIRect::MakeXYWH(
810 srcRect.fRight, srcRect.fTop, halfWidthX, srcRect.height());
811 context->clear(&clearRect, 0x0);
812 }
813
814 context->setRenderTarget(dstTexture->asRenderTarget());
815 context->convolveInX(srcTexture, srcRect, kernelX, kernelWidthX);
816 SkTSwap(srcTexture, dstTexture);
817 if (temp2 && dstTexture == origTexture) dstTexture = temp2->texture();
818 }
819
820 if (sigmaY > 0.0f) {
821 SkAutoTMalloc<float> kernelStorageY(kernelWidthY);
822 float* kernelY = kernelStorageY.get();
823 buildKernel(sigmaY, kernelY, kernelWidthY);
824
825 if (scaleFactorY > 1 || sigmaX > 0.0f) {
826 // Clear out a halfWidth below the srcRect to prevent the Y
827 // convolution from reading garbage.
828 clearRect = SkIRect::MakeXYWH(
829 srcRect.fLeft, srcRect.fBottom, srcRect.width(), halfWidthY);
830 context->clear(&clearRect, 0x0);
831 }
832
833 context->setRenderTarget(dstTexture->asRenderTarget());
834 context->convolveInY(srcTexture, srcRect, kernelY, kernelWidthY);
835 SkTSwap(srcTexture, dstTexture);
836 if (temp2 && dstTexture == origTexture) dstTexture = temp2->texture();
837 }
838
839 if (scaleFactorX > 1 || scaleFactorY > 1) {
840 // Clear one pixel to the right and below, to accommodate bilinear
841 // upsampling.
842 clearRect = SkIRect::MakeXYWH(
843 srcRect.fLeft, srcRect.fBottom, srcRect.width() + 1, 1);
844 context->clear(&clearRect, 0x0);
845 clearRect = SkIRect::MakeXYWH(
846 srcRect.fRight, srcRect.fTop, 1, srcRect.height());
847 context->clear(&clearRect, 0x0);
848 // FIXME: This should be mitchell, not bilinear.
849 paint.getTextureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
850 GrMatrix sampleM;
851 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
852 paint.getTextureSampler(0)->setMatrix(sampleM);
853 context->setRenderTarget(dstTexture->asRenderTarget());
854 paint.setTexture(0, srcTexture);
855 SkRect dstRect(srcRect);
856 scaleRect(&dstRect, scaleFactorX, scaleFactorY);
857 context->drawRectToRect(paint, dstRect, srcRect);
858 srcRect = dstRect;
859 SkTSwap(srcTexture, dstTexture);
860 }
861 context->setRenderTarget(oldRenderTarget);
862 context->setClip(oldClip);
863 return srcTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000864}
865
866static bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
867 SkMaskFilter* filter, const SkMatrix& matrix,
868 const SkRegion& clip, SkBounder* bounder,
869 GrPaint* grp) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000870#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000871 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000872#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000873 SkMaskFilter::BlurInfo info;
874 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.com1f221a72011-08-23 20:54:07 +0000875 if (SkMaskFilter::kNone_BlurType == blurType ||
876 !context->supportsShaders()) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000877 return false;
878 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000879 SkScalar radius = info.fIgnoreTransform ? info.fRadius
880 : matrix.mapRadius(info.fRadius);
881 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000882 if (radius <= 0) {
883 return false;
884 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000885 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000886 float sigma3 = sigma * 3.0f;
887
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000888 SkRect srcRect = path.getBounds();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000889 SkRect clipRect;
890 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000891
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000892 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
893 srcRect.inset(-sigma3, -sigma3);
894 clipRect.inset(-sigma3, -sigma3);
895 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000896 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000897 SkIRect finalIRect;
898 finalRect.roundOut(&finalIRect);
899 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000900 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000901 }
902 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000903 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000904 }
905 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000906 srcRect.offset(offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000907 const GrTextureDesc desc = {
908 kRenderTarget_GrTextureFlagBit,
909 kNone_GrAALevel,
910 srcRect.width(),
911 srcRect.height(),
912 // We actually only need A8, but it often isn't supported as a
913 // render target
tomhudson@google.comf74ad8c2011-11-09 22:15:08 +0000914 { kRGBA_8888_PM_GrPixelConfig }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000915 };
916
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000917 GrAutoScratchTexture pathEntry(context, desc);
918 GrTexture* pathTexture = pathEntry.texture();
919 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000920 return false;
921 }
922 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000923 // Once this code moves into GrContext, this should be changed to use
924 // an AutoClipRestore.
925 GrClip oldClip = context->getClip();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000926 context->setRenderTarget(pathTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000927 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000928 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000929 GrPaint tempPaint;
930 tempPaint.reset();
931
932 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000933 tempPaint.fAntiAlias = grp->fAntiAlias;
934 if (tempPaint.fAntiAlias) {
935 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
936 // blend coeff of zero requires dual source blending support in order
937 // to properly blend partially covered pixels. This means the AA
938 // code path may not be taken. So we use a dst blend coeff of ISA. We
939 // could special case AA draws to a dst surface with known alpha=0 to
940 // use a zero dst coeff when dual source blending isn't available.
941 tempPaint.fSrcBlendCoeff = kOne_BlendCoeff;
942 tempPaint.fDstBlendCoeff = kISC_BlendCoeff;
943 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000944 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000945 context->drawPath(tempPaint, path, skToGrFillType(path.getFillType()), &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000946
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000947 GrAutoScratchTexture temp1, temp2;
948 // If we're doing a normal blur, we can clobber the pathTexture in the
949 // gaussianBlur. Otherwise, we need to save it for later compositing.
950 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
951 GrTexture* blurTexture = gaussianBlur(context, pathTexture,
952 &temp1, isNormalBlur ? NULL : &temp2,
953 srcRect, sigma, sigma);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000954
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000955 if (!isNormalBlur) {
956 GrPaint paint;
957 paint.reset();
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000958 paint.getTextureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000959 GrMatrix sampleM;
960 sampleM.setIDiv(pathTexture->width(), pathTexture->height());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000961 paint.getTextureSampler(0)->setMatrix(sampleM);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000962 // Blend pathTexture over blurTexture.
963 context->setRenderTarget(blurTexture->asRenderTarget());
964 paint.setTexture(0, pathTexture);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000965 if (SkMaskFilter::kInner_BlurType == blurType) {
966 // inner: dst = dst * src
967 paint.fSrcBlendCoeff = kDC_BlendCoeff;
968 paint.fDstBlendCoeff = kZero_BlendCoeff;
969 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
970 // solid: dst = src + dst - src * dst
971 // = (1 - dst) * src + 1 * dst
972 paint.fSrcBlendCoeff = kIDC_BlendCoeff;
973 paint.fDstBlendCoeff = kOne_BlendCoeff;
974 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
975 // outer: dst = dst * (1 - src)
976 // = 0 * src + (1 - src) * dst
977 paint.fSrcBlendCoeff = kZero_BlendCoeff;
978 paint.fDstBlendCoeff = kISC_BlendCoeff;
979 }
980 context->drawRect(paint, srcRect);
981 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000982 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000983 context->setClip(oldClip);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000984
985 if (grp->hasTextureOrMask()) {
986 GrMatrix inverse;
987 if (!matrix.invert(&inverse)) {
988 return false;
989 }
990 grp->preConcatActiveSamplerMatrices(inverse);
991 }
992
993 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
994 // we assume the last mask index is available for use
995 GrAssert(NULL == grp->getMask(MASK_IDX));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000996 grp->setMask(MASK_IDX, blurTexture);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000997 grp->getMaskSampler(MASK_IDX)->setClampNoFilter();
998
999 GrMatrix m;
1000 m.setTranslate(-finalRect.fLeft, -finalRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +00001001 m.postIDiv(blurTexture->width(), blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001002 grp->getMaskSampler(MASK_IDX)->setMatrix(m);
1003 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001004 return true;
1005}
1006
reed@google.com69302852011-02-16 18:08:07 +00001007static bool drawWithMaskFilter(GrContext* context, const SkPath& path,
1008 SkMaskFilter* filter, const SkMatrix& matrix,
1009 const SkRegion& clip, SkBounder* bounder,
1010 GrPaint* grp) {
1011 SkMask srcM, dstM;
1012
1013 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
1014 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
1015 return false;
1016 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001017 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +00001018
1019 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
1020 return false;
1021 }
1022 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +00001023 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +00001024
1025 if (clip.quickReject(dstM.fBounds)) {
1026 return false;
1027 }
1028 if (bounder && !bounder->doIRect(dstM.fBounds)) {
1029 return false;
1030 }
1031
1032 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
1033 // the current clip (and identity matrix) and grpaint settings
1034
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001035 // used to compute inverse view, if necessary
1036 GrMatrix ivm = context->getMatrix();
1037
reed@google.com0c219b62011-02-16 21:31:18 +00001038 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +00001039
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001040 const GrTextureDesc desc = {
1041 kNone_GrTextureFlags,
1042 kNone_GrAALevel,
reed@google.com69302852011-02-16 18:08:07 +00001043 dstM.fBounds.width(),
1044 dstM.fBounds.height(),
tomhudson@google.comf74ad8c2011-11-09 22:15:08 +00001045 { kAlpha_8_GrPixelConfig }
reed@google.com69302852011-02-16 18:08:07 +00001046 };
1047
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001048 GrAutoScratchTexture ast(context, desc);
1049 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001050
reed@google.com69302852011-02-16 18:08:07 +00001051 if (NULL == texture) {
1052 return false;
1053 }
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001054 texture->uploadTextureData(0, 0, desc.fWidth, desc.fHeight,
1055 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +00001056
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001057 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
1058 grp->preConcatActiveSamplerMatrices(ivm);
1059 }
1060
1061 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
1062 // we assume the last mask index is available for use
1063 GrAssert(NULL == grp->getMask(MASK_IDX));
1064 grp->setMask(MASK_IDX, texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001065 grp->getMaskSampler(MASK_IDX)->setClampNoFilter();
reed@google.com69302852011-02-16 18:08:07 +00001066
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001067 GrRect d;
1068 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +00001069 GrIntToScalar(dstM.fBounds.fTop),
1070 GrIntToScalar(dstM.fBounds.fRight),
1071 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001072
1073 GrMatrix m;
bsalomon@google.com9d12f5c2011-09-29 18:08:18 +00001074 m.setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
1075 -dstM.fBounds.fTop*SK_Scalar1);
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001076 m.postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001077 grp->getMaskSampler(MASK_IDX)->setMatrix(m);
1078
1079 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +00001080 return true;
1081}
reed@google.com69302852011-02-16 18:08:07 +00001082
reed@google.com0c219b62011-02-16 21:31:18 +00001083void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
reed@google.comfe626382011-09-21 13:50:35 +00001084 const SkPaint& origPaint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +00001085 bool pathIsMutable) {
1086 CHECK_SHOULD_DRAW(draw);
1087
reed@google.comfe626382011-09-21 13:50:35 +00001088 bool doFill = true;
1089 SkTLazy<SkPaint> lazyPaint;
1090 const SkPaint* paint = &origPaint;
1091
1092 // can we cheat, and threat a thin stroke as a hairline (w/ modulated alpha)
1093 // if we can, we draw lots faster (raster device does this same test)
1094 {
1095 SkAlpha newAlpha;
1096 if (SkDrawTreatAsHairline(*paint, *draw.fMatrix, &newAlpha)) {
1097 lazyPaint.set(*paint);
1098 lazyPaint.get()->setAlpha(newAlpha);
1099 lazyPaint.get()->setStrokeWidth(0);
1100 paint = lazyPaint.get();
1101 doFill = false;
1102 }
1103 }
1104 // must reference paint from here down, and not origPaint
1105 // since we may have change the paint (using lazyPaint for storage)
1106
bsalomon@google.com5782d712011-01-21 21:03:59 +00001107 GrPaint grPaint;
1108 SkAutoCachedTexture act;
reed@google.comfe626382011-09-21 13:50:35 +00001109 if (!this->skPaint2GrPaintShader(*paint,
Scroggod757df22011-05-16 13:11:16 +00001110 &act,
1111 *draw.fMatrix,
1112 &grPaint,
1113 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001114 return;
1115 }
1116
reed@google.comfe626382011-09-21 13:50:35 +00001117 // If we have a prematrix, apply it to the path, optimizing for the case
1118 // where the original path can in fact be modified in place (even though
1119 // its parameter type is const).
1120 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1121 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001122
1123 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001124 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001125
reed@google.come3445642011-02-16 23:20:39 +00001126 if (!pathIsMutable) {
1127 result = &tmpPath;
1128 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001129 }
reed@google.come3445642011-02-16 23:20:39 +00001130 // should I push prePathMatrix on our MV stack temporarily, instead
1131 // of applying it here? See SkDraw.cpp
1132 pathPtr->transform(*prePathMatrix, result);
1133 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001134 }
reed@google.com0c219b62011-02-16 21:31:18 +00001135 // at this point we're done with prePathMatrix
1136 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001137
reed@google.comfe626382011-09-21 13:50:35 +00001138 if (doFill && (paint->getPathEffect() ||
1139 paint->getStyle() != SkPaint::kFill_Style)) {
1140 // it is safe to use tmpPath here, even if we already used it for the
1141 // prepathmatrix, since getFillPath can take the same object for its
1142 // input and output safely.
1143 doFill = paint->getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001144 pathPtr = &tmpPath;
1145 }
1146
reed@google.comfe626382011-09-21 13:50:35 +00001147 if (paint->getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001148 // avoid possibly allocating a new path in transform if we can
1149 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1150
1151 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001152 pathPtr->transform(*draw.fMatrix, devPathPtr);
reed@google.comfe626382011-09-21 13:50:35 +00001153 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint->getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001154 *draw.fMatrix, *draw.fClip, draw.fBounder,
1155 &grPaint)) {
reed@google.comfe626382011-09-21 13:50:35 +00001156 drawWithMaskFilter(fContext, *devPathPtr, paint->getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001157 *draw.fMatrix, *draw.fClip, draw.fBounder,
1158 &grPaint);
1159 }
reed@google.com69302852011-02-16 18:08:07 +00001160 return;
1161 }
reed@google.com69302852011-02-16 18:08:07 +00001162
bsalomon@google.comffca4002011-02-22 20:34:01 +00001163 GrPathFill fill = kHairLine_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001164
reed@google.com0c219b62011-02-16 21:31:18 +00001165 if (doFill) {
1166 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001167 case SkPath::kWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001168 fill = kWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001169 break;
1170 case SkPath::kEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001171 fill = kEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001172 break;
1173 case SkPath::kInverseWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001174 fill = kInverseWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001175 break;
1176 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001177 fill = kInverseEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001178 break;
1179 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001180 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001181 return;
1182 }
1183 }
1184
reed@google.com07f3ee12011-05-16 17:21:57 +00001185 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001186}
1187
reed@google.comac10a2d2010-12-22 21:39:39 +00001188void SkGpuDevice::drawBitmap(const SkDraw& draw,
1189 const SkBitmap& bitmap,
1190 const SkIRect* srcRectPtr,
1191 const SkMatrix& m,
1192 const SkPaint& paint) {
1193 CHECK_SHOULD_DRAW(draw);
1194
1195 SkIRect srcRect;
1196 if (NULL == srcRectPtr) {
1197 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1198 } else {
1199 srcRect = *srcRectPtr;
1200 }
1201
junov@google.comd935cfb2011-06-27 20:48:23 +00001202 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001203 // Convert the bitmap to a shader so that the rect can be drawn
1204 // through drawRect, which supports mask filters.
1205 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001206 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001207 if (srcRectPtr) {
1208 if (!bitmap.extractSubset(&tmp, srcRect)) {
1209 return; // extraction failed
1210 }
1211 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001212 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001213 }
1214 SkPaint paintWithTexture(paint);
1215 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1216 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001217 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001218 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001219
junov@google.com1d329782011-07-28 20:10:09 +00001220 // Transform 'm' needs to be concatenated to the draw matrix,
1221 // rather than transforming the primitive directly, so that 'm' will
1222 // also affect the behavior of the mask filter.
1223 SkMatrix drawMatrix;
1224 drawMatrix.setConcat(*draw.fMatrix, m);
1225 SkDraw transformedDraw(draw);
1226 transformedDraw.fMatrix = &drawMatrix;
1227
1228 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1229
junov@google.comd935cfb2011-06-27 20:48:23 +00001230 return;
1231 }
1232
bsalomon@google.com5782d712011-01-21 21:03:59 +00001233 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001234 if (!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001235 return;
1236 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001237 GrSamplerState* sampler = grPaint.getTextureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001238 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001239 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001240 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001241 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001242 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001243
bsalomon@google.com91958362011-06-13 17:58:13 +00001244 const int maxTextureSize = fContext->getMaxTextureSize();
1245 if (bitmap.getTexture() || (bitmap.width() <= maxTextureSize &&
1246 bitmap.height() <= maxTextureSize)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001247 // take the fast case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001248 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001249 return;
1250 }
1251
1252 // undo the translate done by SkCanvas
1253 int DX = SkMax32(0, srcRect.fLeft);
1254 int DY = SkMax32(0, srcRect.fTop);
1255 // compute clip bounds in local coordinates
1256 SkIRect clipRect;
1257 {
1258 SkRect r;
1259 r.set(draw.fClip->getBounds());
1260 SkMatrix matrix, inverse;
1261 matrix.setConcat(*draw.fMatrix, m);
1262 if (!matrix.invert(&inverse)) {
1263 return;
1264 }
1265 inverse.mapRect(&r);
1266 r.roundOut(&clipRect);
1267 // apply the canvas' translate to our local clip
1268 clipRect.offset(DX, DY);
1269 }
1270
bsalomon@google.com91958362011-06-13 17:58:13 +00001271 int nx = bitmap.width() / maxTextureSize;
1272 int ny = bitmap.height() / maxTextureSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001273 for (int x = 0; x <= nx; x++) {
1274 for (int y = 0; y <= ny; y++) {
1275 SkIRect tileR;
bsalomon@google.com91958362011-06-13 17:58:13 +00001276 tileR.set(x * maxTextureSize, y * maxTextureSize,
1277 (x + 1) * maxTextureSize, (y + 1) * maxTextureSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001278 if (!SkIRect::Intersects(tileR, clipRect)) {
1279 continue;
1280 }
1281
1282 SkIRect srcR = tileR;
1283 if (!srcR.intersect(srcRect)) {
1284 continue;
1285 }
1286
1287 SkBitmap tmpB;
1288 if (bitmap.extractSubset(&tmpB, tileR)) {
1289 // now offset it to make it "local" to our tmp bitmap
1290 srcR.offset(-tileR.fLeft, -tileR.fTop);
1291
1292 SkMatrix tmpM(m);
1293 {
1294 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1295 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1296 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1297 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001298 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001299 }
1300 }
1301 }
1302}
1303
1304/*
1305 * This is called by drawBitmap(), which has to handle images that may be too
1306 * large to be represented by a single texture.
1307 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001308 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1309 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001310 */
1311void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1312 const SkBitmap& bitmap,
1313 const SkIRect& srcRect,
1314 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001315 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001316 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1317 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001318
reed@google.com9c49bc32011-07-07 13:42:37 +00001319 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001320 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001321 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001322 return;
1323 }
1324
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001325 GrSamplerState* sampler = grPaint->getTextureSampler(kBitmapTextureIdx);
1326
1327 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1328 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
1329 sampler->setSampleMode(GrSamplerState::kNormal_SampleMode);
1330 sampler->setMatrix(GrMatrix::I());
reed@google.comac10a2d2010-12-22 21:39:39 +00001331
1332 GrTexture* texture;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001333 SkAutoCachedTexture act(this, bitmap, *sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001334 if (NULL == texture) {
1335 return;
1336 }
1337
bsalomon@google.com452943d2011-10-31 17:37:14 +00001338 grPaint->setTexture(kBitmapTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001339
reed@google.com20efde72011-05-09 17:00:02 +00001340 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1341 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001342 GrRect paintRect;
junov@google.com6acc9b32011-05-16 18:32:07 +00001343 paintRect.setLTRB(GrFixedToScalar((srcRect.fLeft << 16) / bitmap.width()),
1344 GrFixedToScalar((srcRect.fTop << 16) / bitmap.height()),
1345 GrFixedToScalar((srcRect.fRight << 16) / bitmap.width()),
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001346 GrFixedToScalar((srcRect.fBottom << 16) / bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001347
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001348 if (GrSamplerState::kNearest_Filter != sampler->getFilter() &&
junov@google.com6acc9b32011-05-16 18:32:07 +00001349 (srcRect.width() < bitmap.width() ||
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001350 srcRect.height() < bitmap.height())) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001351 // If drawing a subrect of the bitmap and filtering is enabled,
1352 // use a constrained texture domain to avoid color bleeding
1353 GrScalar left, top, right, bottom;
1354 if (srcRect.width() > 1) {
1355 GrScalar border = GR_ScalarHalf / bitmap.width();
1356 left = paintRect.left() + border;
1357 right = paintRect.right() - border;
1358 } else {
1359 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1360 }
1361 if (srcRect.height() > 1) {
1362 GrScalar border = GR_ScalarHalf / bitmap.height();
1363 top = paintRect.top() + border;
1364 bottom = paintRect.bottom() - border;
1365 } else {
1366 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1367 }
1368 GrRect textureDomain;
1369 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001370 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001371 }
1372
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001373 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001374}
1375
1376void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1377 int left, int top, const SkPaint& paint) {
1378 CHECK_SHOULD_DRAW(draw);
1379
1380 SkAutoLockPixels alp(bitmap);
1381 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1382 return;
1383 }
1384
bsalomon@google.com5782d712011-01-21 21:03:59 +00001385 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001386 if(!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001387 return;
1388 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001389
bsalomon@google.com5782d712011-01-21 21:03:59 +00001390 GrAutoMatrix avm(fContext, GrMatrix::I());
1391
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001392 GrSamplerState* sampler = grPaint.getTextureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001393
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001394 GrTexture* texture;
1395 sampler->setClampNoFilter();
1396 SkAutoCachedTexture act(this, bitmap, *sampler, &texture);
1397
1398 grPaint.setTexture(kBitmapTextureIdx, texture);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001399
bsalomon@google.com5782d712011-01-21 21:03:59 +00001400 fContext->drawRectToRect(grPaint,
reed@google.com20efde72011-05-09 17:00:02 +00001401 GrRect::MakeXYWH(GrIntToScalar(left),
1402 GrIntToScalar(top),
1403 GrIntToScalar(bitmap.width()),
1404 GrIntToScalar(bitmap.height())),
1405 GrRect::MakeWH(GR_Scalar1, GR_Scalar1));
reed@google.comac10a2d2010-12-22 21:39:39 +00001406}
1407
1408void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* dev,
1409 int x, int y, const SkPaint& paint) {
1410 CHECK_SHOULD_DRAW(draw);
1411
bsalomon@google.com5782d712011-01-21 21:03:59 +00001412 GrPaint grPaint;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001413 if (!((SkGpuDevice*)dev)->bindDeviceAsTexture(&grPaint) ||
Scroggod757df22011-05-16 13:11:16 +00001414 !this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001415 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001416 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001417
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001418 GrTexture* devTex = grPaint.getTexture(0);
1419 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001420
1421 const SkBitmap& bm = dev->accessBitmap(false);
1422 int w = bm.width();
1423 int h = bm.height();
1424
1425 GrAutoMatrix avm(fContext, GrMatrix::I());
1426
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001427 grPaint.getTextureSampler(kBitmapTextureIdx)->setClampNoFilter();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001428
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001429 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1430 GrIntToScalar(y),
1431 GrIntToScalar(w),
1432 GrIntToScalar(h));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +00001433 SkImageFilter* imageFilter = paint.getImageFilter();
1434 SkSize size;
1435 if (NULL != imageFilter && imageFilter->asABlur(&size)) {
1436 GrAutoScratchTexture temp1, temp2;
1437 GrTexture* blurTexture = gaussianBlur(fContext,
1438 devTex, &temp1, &temp2,
1439 GrRect::MakeWH(w, h),
1440 size.width(),
1441 size.height());
1442 grPaint.setTexture(kBitmapTextureIdx, blurTexture);
1443 devTex = blurTexture;
1444 }
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001445 // The device being drawn may not fill up its texture (saveLayer uses
1446 // the approximate ).
1447 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1448 GR_Scalar1 * h / devTex->height());
1449
1450 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001451}
1452
1453///////////////////////////////////////////////////////////////////////////////
1454
1455// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001456static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1457 kTriangles_PrimitiveType,
1458 kTriangleStrip_PrimitiveType,
1459 kTriangleFan_PrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001460};
1461
1462void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1463 int vertexCount, const SkPoint vertices[],
1464 const SkPoint texs[], const SkColor colors[],
1465 SkXfermode* xmode,
1466 const uint16_t indices[], int indexCount,
1467 const SkPaint& paint) {
1468 CHECK_SHOULD_DRAW(draw);
1469
bsalomon@google.com5782d712011-01-21 21:03:59 +00001470 GrPaint grPaint;
1471 SkAutoCachedTexture act;
1472 // we ignore the shader if texs is null.
1473 if (NULL == texs) {
Scroggod757df22011-05-16 13:11:16 +00001474 if (!this->skPaint2GrPaintNoShader(paint,
1475 false,
1476 &grPaint,
1477 NULL == colors)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001478 return;
1479 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001480 } else {
reed@google.com1a2e8d22011-01-21 22:08:29 +00001481 if (!this->skPaint2GrPaintShader(paint, &act,
1482 *draw.fMatrix,
Scroggod757df22011-05-16 13:11:16 +00001483 &grPaint,
1484 NULL == colors)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001485 return;
1486 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001487 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001488
1489 if (NULL != xmode && NULL != texs && NULL != colors) {
1490 SkXfermode::Mode mode;
1491 if (!SkXfermode::IsMode(xmode, &mode) ||
1492 SkXfermode::kMultiply_Mode != mode) {
1493 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1494#if 0
1495 return
1496#endif
1497 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001498 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001499
bsalomon@google.com498776a2011-08-16 19:20:44 +00001500 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1501 if (NULL != colors) {
1502 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001503 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001504 for (int i = 0; i < vertexCount; ++i) {
1505 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1506 }
1507 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001508 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001509 fContext->drawVertices(grPaint,
1510 gVertexMode2PrimitiveType[vmode],
1511 vertexCount,
1512 (GrPoint*) vertices,
1513 (GrPoint*) texs,
1514 colors,
1515 indices,
1516 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001517}
1518
1519///////////////////////////////////////////////////////////////////////////////
1520
1521static void GlyphCacheAuxProc(void* data) {
1522 delete (GrFontScaler*)data;
1523}
1524
1525static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1526 void* auxData;
1527 GrFontScaler* scaler = NULL;
1528 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1529 scaler = (GrFontScaler*)auxData;
1530 }
1531 if (NULL == scaler) {
1532 scaler = new SkGrFontScaler(cache);
1533 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1534 }
1535 return scaler;
1536}
1537
1538static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1539 SkFixed fx, SkFixed fy,
1540 const SkGlyph& glyph) {
1541 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1542
1543 GrSkDrawProcs* procs = (GrSkDrawProcs*)state.fDraw->fProcs;
1544
1545 if (NULL == procs->fFontScaler) {
1546 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1547 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001548
1549 /*
reed@google.com3b139f52011-06-07 17:56:25 +00001550 * What should we do with fy? (assuming horizontal/latin text)
reed@google.com39ce0ac2011-04-08 15:42:19 +00001551 *
reed@google.com3b139f52011-06-07 17:56:25 +00001552 * The raster code calls SkFixedFloorToFixed on it, as it does with fx.
1553 * It calls that rather than round, because our caller has already added
1554 * SK_FixedHalf, so that calling floor gives us the rounded integer.
1555 *
1556 * Test code between raster and gpu (they should draw the same)
1557 *
1558 * canvas->drawText("Hamburgefons", 12, 0, 16.5f, paint);
1559 *
1560 * Perhaps we should only perform this integralization if there is no
1561 * fExtMatrix...
reed@google.com39ce0ac2011-04-08 15:42:19 +00001562 */
reed@google.com3b139f52011-06-07 17:56:25 +00001563 fy = SkFixedFloorToFixed(fy);
1564
reed@google.comac10a2d2010-12-22 21:39:39 +00001565 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(), fx, 0),
reed@google.com3b139f52011-06-07 17:56:25 +00001566 SkFixedFloorToFixed(fx), fy,
reed@google.comac10a2d2010-12-22 21:39:39 +00001567 procs->fFontScaler);
1568}
1569
bsalomon@google.com5782d712011-01-21 21:03:59 +00001570SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001571
1572 // deferred allocation
1573 if (NULL == fDrawProcs) {
1574 fDrawProcs = new GrSkDrawProcs;
1575 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1576 fDrawProcs->fContext = fContext;
1577 }
1578
1579 // init our (and GL's) state
1580 fDrawProcs->fTextContext = context;
1581 fDrawProcs->fFontScaler = NULL;
1582 return fDrawProcs;
1583}
1584
1585void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1586 size_t byteLength, SkScalar x, SkScalar y,
1587 const SkPaint& paint) {
1588 CHECK_SHOULD_DRAW(draw);
1589
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001590 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001591 // this guy will just call our drawPath()
1592 draw.drawText((const char*)text, byteLength, x, y, paint);
1593 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001594 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001595
1596 GrPaint grPaint;
1597 SkAutoCachedTexture act;
1598
Scroggod757df22011-05-16 13:11:16 +00001599 if (!this->skPaint2GrPaintShader(paint,
1600 &act,
1601 *draw.fMatrix,
1602 &grPaint,
1603 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001604 return;
1605 }
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001606 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001607 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001608 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1609 }
1610}
1611
1612void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1613 size_t byteLength, const SkScalar pos[],
1614 SkScalar constY, int scalarsPerPos,
1615 const SkPaint& paint) {
1616 CHECK_SHOULD_DRAW(draw);
1617
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001618 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001619 // this guy will just call our drawPath()
1620 draw.drawPosText((const char*)text, byteLength, pos, constY,
1621 scalarsPerPos, paint);
1622 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001623 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001624
1625 GrPaint grPaint;
1626 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +00001627 if (!this->skPaint2GrPaintShader(paint,
1628 &act,
1629 *draw.fMatrix,
1630 &grPaint,
1631 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001632 return;
1633 }
1634
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001635 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001636 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001637 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1638 scalarsPerPos, paint);
1639 }
1640}
1641
1642void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1643 size_t len, const SkPath& path,
1644 const SkMatrix* m, const SkPaint& paint) {
1645 CHECK_SHOULD_DRAW(draw);
1646
1647 SkASSERT(draw.fDevice == this);
1648 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1649}
1650
1651///////////////////////////////////////////////////////////////////////////////
1652
reed@google.comf67e4cf2011-03-15 20:56:58 +00001653bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1654 if (!paint.isLCDRenderText()) {
1655 // we're cool with the paint as is
1656 return false;
1657 }
1658
1659 if (paint.getShader() ||
1660 paint.getXfermode() || // unless its srcover
1661 paint.getMaskFilter() ||
1662 paint.getRasterizer() ||
1663 paint.getColorFilter() ||
1664 paint.getPathEffect() ||
1665 paint.isFakeBoldText() ||
1666 paint.getStyle() != SkPaint::kFill_Style) {
1667 // turn off lcd
1668 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1669 flags->fHinting = paint.getHinting();
1670 return true;
1671 }
1672 // we're cool with the paint as is
1673 return false;
1674}
1675
1676///////////////////////////////////////////////////////////////////////////////
1677
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001678SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(const SkBitmap& bitmap,
1679 const GrSamplerState& sampler,
1680 TexType type) {
1681 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001682 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001683
bsalomon@google.come97f0852011-06-17 13:10:25 +00001684 if (kBitmap_TexType != type) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001685 const GrTextureDesc desc = {
1686 kRenderTarget_GrTextureFlagBit,
1687 kNone_GrAALevel,
1688 bitmap.width(),
1689 bitmap.height(),
tomhudson@google.comf74ad8c2011-11-09 22:15:08 +00001690 { SkGr::Bitmap2PixelConfig(bitmap) }
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001691 };
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001692 GrContext::ScratchTexMatch match;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001693 if (kSaveLayerDeviceRenderTarget_TexType == type) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001694 // we know layers will only be drawn through drawDevice.
1695 // drawDevice has been made to work with content embedded in a
1696 // larger texture so its okay to use the approximate version.
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001697 match = GrContext::kApprox_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001698 } else {
bsalomon@google.come97f0852011-06-17 13:10:25 +00001699 SkASSERT(kDeviceRenderTarget_TexType == type);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001700 match = GrContext::kExact_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001701 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001702 entry = ctx->lockScratchTexture(desc, match);
reed@google.comac10a2d2010-12-22 21:39:39 +00001703 } else {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001704 if (!bitmap.isVolatile()) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001705 GrContext::TextureKey key = bitmap.getGenerationID();
1706 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
junov@google.com4ee7ae52011-06-30 17:30:49 +00001707
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001708 entry = ctx->findAndLockTexture(key, bitmap.width(),
1709 bitmap.height(), sampler);
1710 if (NULL == entry.texture()) {
1711 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
junov@google.com4ee7ae52011-06-30 17:30:49 +00001712 bitmap);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001713 }
junov@google.com4ee7ae52011-06-30 17:30:49 +00001714 } else {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001715 entry = sk_gr_create_bitmap_texture(ctx, gUNCACHED_KEY, sampler, bitmap);
junov@google.com4ee7ae52011-06-30 17:30:49 +00001716 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001717 if (NULL == entry.texture()) {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001718 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1719 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001720 }
1721 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001722 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001723}
1724
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001725void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1726 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001727}
1728
bsalomon@google.come97f0852011-06-17 13:10:25 +00001729SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1730 int width, int height,
1731 bool isOpaque,
1732 Usage usage) {
1733 return SkNEW_ARGS(SkGpuDevice,(this->context(), config,
1734 width, height, usage));
1735}
1736