blob: 0f4dea085931d6d85304fc4e9cd671c8de82ccc6 [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
bsalomon@google.com84405e02012-03-05 19:57:21 +000059class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
60public:
61 SkAutoCachedTexture() { }
62 SkAutoCachedTexture(SkGpuDevice* device,
63 const SkBitmap& bitmap,
64 const GrSamplerState* sampler,
65 GrTexture** texture) {
66 GrAssert(texture);
67 *texture = this->set(device, bitmap, sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +000068 }
reed@google.comac10a2d2010-12-22 21:39:39 +000069
bsalomon@google.com84405e02012-03-05 19:57:21 +000070 ~SkAutoCachedTexture() {
71 if (fTex.texture()) {
72 fDevice->unlockCachedTexture(fTex);
73 }
reed@google.comac10a2d2010-12-22 21:39:39 +000074 }
bsalomon@google.com84405e02012-03-05 19:57:21 +000075
76 GrTexture* set(SkGpuDevice* device,
77 const SkBitmap& bitmap,
78 const GrSamplerState* sampler) {
79 if (fTex.texture()) {
80 fDevice->unlockCachedTexture(fTex);
81 }
82 fDevice = device;
83 GrTexture* texture = (GrTexture*)bitmap.getTexture();
84 if (texture) {
85 // return the native texture
86 fTex.reset();
87 } else {
88 // look it up in our cache
89 fTex = device->lockCachedTexture(bitmap, sampler);
90 texture = fTex.texture();
91 }
92 return texture;
93 }
94
95private:
96 SkGpuDevice* fDevice;
97 GrContext::TextureCacheEntry fTex;
98};
reed@google.comac10a2d2010-12-22 21:39:39 +000099
100///////////////////////////////////////////////////////////////////////////////
101
102bool gDoTraceDraw;
103
104struct GrSkDrawProcs : public SkDrawProcs {
105public:
106 GrContext* fContext;
107 GrTextContext* fTextContext;
108 GrFontScaler* fFontScaler; // cached in the skia glyphcache
109};
110
111///////////////////////////////////////////////////////////////////////////////
112
reed@google.comaf951c92011-06-16 19:10:39 +0000113static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
114 switch (config) {
115 case kAlpha_8_GrPixelConfig:
116 *isOpaque = false;
117 return SkBitmap::kA8_Config;
118 case kRGB_565_GrPixelConfig:
119 *isOpaque = true;
120 return SkBitmap::kRGB_565_Config;
121 case kRGBA_4444_GrPixelConfig:
122 *isOpaque = false;
123 return SkBitmap::kARGB_4444_Config;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000124 case kSkia8888_PM_GrPixelConfig:
125 // we don't currently have a way of knowing whether
126 // a 8888 is opaque based on the config.
127 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000128 return SkBitmap::kARGB_8888_Config;
129 default:
130 *isOpaque = false;
131 return SkBitmap::kNo_Config;
132 }
133}
reed@google.comac10a2d2010-12-22 21:39:39 +0000134
reed@google.comaf951c92011-06-16 19:10:39 +0000135static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000136 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000137
138 bool isOpaque;
139 SkBitmap bitmap;
140 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
141 renderTarget->width(), renderTarget->height());
142 bitmap.setIsOpaque(isOpaque);
143 return bitmap;
144}
145
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000146SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
147: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
148 this->initFromRenderTarget(context, texture->asRenderTarget());
149}
150
reed@google.comaf951c92011-06-16 19:10:39 +0000151SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
152: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000153 this->initFromRenderTarget(context, renderTarget);
154}
155
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000156void SkGpuDevice::initFromRenderTarget(GrContext* context,
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000157 GrRenderTarget* renderTarget) {
reed@google.comaf951c92011-06-16 19:10:39 +0000158 fNeedPrepareRenderTarget = false;
159 fDrawProcs = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000160
reed@google.comaf951c92011-06-16 19:10:39 +0000161 fContext = context;
162 fContext->ref();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000163
reed@google.comaf951c92011-06-16 19:10:39 +0000164 fTexture = NULL;
165 fRenderTarget = NULL;
166 fNeedClear = false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000167
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000168 GrAssert(NULL != renderTarget);
169 fRenderTarget = renderTarget;
170 fRenderTarget->ref();
171 // if this RT is also a texture, hold a ref on it
172 fTexture = fRenderTarget->asTexture();
173 SkSafeRef(fTexture);
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000174
175 // Create a pixel ref for the underlying SkBitmap. We prefer a texture pixel
176 // ref to a render target pixel reft. The pixel ref may get ref'ed outside
177 // the device via accessBitmap. This external ref may outlive the device.
178 // Since textures own their render targets (but not vice-versa) we
179 // are ensuring that both objects will live as long as the pixel ref.
180 SkPixelRef* pr;
181 if (fTexture) {
182 pr = new SkGrTexturePixelRef(fTexture);
183 } else {
184 pr = new SkGrRenderTargetPixelRef(fRenderTarget);
185 }
reed@google.comaf951c92011-06-16 19:10:39 +0000186 this->setPixelRef(pr, 0)->unref();
187}
188
189SkGpuDevice::SkGpuDevice(GrContext* context, SkBitmap::Config config, int width,
bsalomon@google.come97f0852011-06-17 13:10:25 +0000190 int height, Usage usage)
reed@google.comaf951c92011-06-16 19:10:39 +0000191: SkDevice(config, width, height, false /*isOpaque*/) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000192 fNeedPrepareRenderTarget = false;
193 fDrawProcs = NULL;
194
reed@google.com7b201d22011-01-11 18:59:23 +0000195 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000196 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000197
reed@google.comac10a2d2010-12-22 21:39:39 +0000198 fTexture = NULL;
199 fRenderTarget = NULL;
200 fNeedClear = false;
201
reed@google.comaf951c92011-06-16 19:10:39 +0000202 if (config != SkBitmap::kRGB_565_Config) {
203 config = SkBitmap::kARGB_8888_Config;
204 }
205 SkBitmap bm;
206 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000207
208#if CACHE_LAYER_TEXTURES
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000209 TexType type = (kSaveLayer_Usage == usage) ?
bsalomon@google.come97f0852011-06-17 13:10:25 +0000210 kSaveLayerDeviceRenderTarget_TexType :
211 kDeviceRenderTarget_TexType;
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000212 fCache = this->lockCachedTexture(bm, NULL, type);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000213 fTexture = fCache.texture();
214 if (fTexture) {
reed@google.comaf951c92011-06-16 19:10:39 +0000215 SkASSERT(NULL != fTexture->asRenderTarget());
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000216 // hold a ref directly on fTexture (even though fCache has one) to match
217 // other constructor paths. Simplifies cleanup.
218 fTexture->ref();
reed@google.comaf951c92011-06-16 19:10:39 +0000219 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000220#else
reed@google.comaf951c92011-06-16 19:10:39 +0000221 const GrTextureDesc desc = {
222 kRenderTarget_GrTextureFlagBit,
reed@google.comaf951c92011-06-16 19:10:39 +0000223 width,
224 height,
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000225 SkGr::Bitmap2PixelConfig(bm),
226 {0} // samples
reed@google.comaf951c92011-06-16 19:10:39 +0000227 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000228
reed@google.comaf951c92011-06-16 19:10:39 +0000229 fTexture = fContext->createUncachedTexture(desc, NULL, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000230#endif
reed@google.comaf951c92011-06-16 19:10:39 +0000231 if (NULL != fTexture) {
232 fRenderTarget = fTexture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000233 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000234
reed@google.comaf951c92011-06-16 19:10:39 +0000235 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000236
reed@google.comaf951c92011-06-16 19:10:39 +0000237 // we defer the actual clear until our gainFocus()
238 fNeedClear = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000239
reed@google.comaf951c92011-06-16 19:10:39 +0000240 // wrap the bitmap with a pixelref to expose our texture
241 SkGrTexturePixelRef* pr = new SkGrTexturePixelRef(fTexture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000242 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000243 } else {
244 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
245 width, height);
246 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000247 }
248}
249
250SkGpuDevice::~SkGpuDevice() {
251 if (fDrawProcs) {
252 delete fDrawProcs;
253 }
254
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000255 SkSafeUnref(fTexture);
256 SkSafeUnref(fRenderTarget);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000257 if (fCache.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000258 GrAssert(NULL != fTexture);
259 GrAssert(fRenderTarget == fTexture->asRenderTarget());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000260 fContext->unlockTexture(fCache);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000261 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000262 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000263}
264
reed@google.comac10a2d2010-12-22 21:39:39 +0000265///////////////////////////////////////////////////////////////////////////////
266
267void SkGpuDevice::makeRenderTargetCurrent() {
268 fContext->setRenderTarget(fRenderTarget);
269 fContext->flush(true);
270 fNeedPrepareRenderTarget = true;
271}
272
273///////////////////////////////////////////////////////////////////////////////
274
bsalomon@google.comc4364992011-11-07 15:54:49 +0000275namespace {
276GrPixelConfig config8888_to_gr_config(SkCanvas::Config8888 config8888) {
277 switch (config8888) {
278 case SkCanvas::kNative_Premul_Config8888:
279 return kSkia8888_PM_GrPixelConfig;
280 case SkCanvas::kNative_Unpremul_Config8888:
281 return kSkia8888_UPM_GrPixelConfig;
282 case SkCanvas::kBGRA_Premul_Config8888:
283 return kBGRA_8888_PM_GrPixelConfig;
284 case SkCanvas::kBGRA_Unpremul_Config8888:
285 return kBGRA_8888_UPM_GrPixelConfig;
286 case SkCanvas::kRGBA_Premul_Config8888:
287 return kRGBA_8888_PM_GrPixelConfig;
288 case SkCanvas::kRGBA_Unpremul_Config8888:
289 return kRGBA_8888_UPM_GrPixelConfig;
290 default:
291 GrCrash("Unexpected Config8888.");
292 return kSkia8888_PM_GrPixelConfig;
293 }
294}
295}
296
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000297bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
298 int x, int y,
299 SkCanvas::Config8888 config8888) {
bsalomon@google.com910267d2011-11-02 20:06:25 +0000300 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
301 SkASSERT(!bitmap.isNull());
302 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000303
bsalomon@google.com910267d2011-11-02 20:06:25 +0000304 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000305 GrPixelConfig config;
306 config = config8888_to_gr_config(config8888);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000307 return fContext->readRenderTargetPixels(fRenderTarget,
308 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000309 bitmap.width(),
310 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000311 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000312 bitmap.getPixels(),
313 bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000314}
315
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000316void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
317 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000318 SkAutoLockPixels alp(bitmap);
319 if (!bitmap.readyToDraw()) {
320 return;
321 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000322
323 GrPixelConfig config;
324 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
325 config = config8888_to_gr_config(config8888);
326 } else {
327 config= SkGr::BitmapConfig2PixelConfig(bitmap.config(),
328 bitmap.isOpaque());
329 }
330
bsalomon@google.com6f379512011-11-16 20:36:03 +0000331 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
332 config, bitmap.getPixels(), bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000333}
334
335///////////////////////////////////////////////////////////////////////////////
336
337static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000338 const SkClipStack& clipStack,
reed@google.com6f8f2922011-03-04 22:27:10 +0000339 const SkRegion& clipRegion,
340 const SkIPoint& origin) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000341 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000342
343 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000344 iter.reset(clipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000345 const SkIRect& skBounds = clipRegion.getBounds();
346 GrRect bounds;
347 bounds.setLTRB(GrIntToScalar(skBounds.fLeft),
348 GrIntToScalar(skBounds.fTop),
349 GrIntToScalar(skBounds.fRight),
350 GrIntToScalar(skBounds.fBottom));
reed@google.com6f8f2922011-03-04 22:27:10 +0000351 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
352 &bounds);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000353 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000354}
355
356// call this ever each draw call, to ensure that the context reflects our state,
357// and not the state from some other canvas/device
358void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
359 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000360 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000361
362 fContext->setRenderTarget(fRenderTarget);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000363 SkASSERT(draw.fClipStack);
364 convert_matrixclip(fContext, *draw.fMatrix,
reed@google.com6f8f2922011-03-04 22:27:10 +0000365 *draw.fClipStack, *draw.fClip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000366 fNeedPrepareRenderTarget = false;
367 }
368}
369
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000370void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
371 const SkClipStack& clipStack) {
372 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
373 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000374 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000375}
376
377void SkGpuDevice::gainFocus(SkCanvas* canvas, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000378 const SkRegion& clip, const SkClipStack& clipStack) {
379
reed@google.comac10a2d2010-12-22 21:39:39 +0000380 fContext->setRenderTarget(fRenderTarget);
381
bsalomon@google.comd302f142011-03-03 13:54:13 +0000382 this->INHERITED::gainFocus(canvas, matrix, clip, clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000383
reed@google.com6f8f2922011-03-04 22:27:10 +0000384 convert_matrixclip(fContext, matrix, clipStack, clip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000385
386 if (fNeedClear) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000387 fContext->clear(NULL, 0x0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000388 fNeedClear = false;
389 }
390}
391
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000392SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
393 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000394}
395
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000396bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000397 if (NULL != fTexture) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000398 paint->setTexture(kBitmapTextureIdx, fTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000399 return true;
400 }
401 return false;
402}
403
404///////////////////////////////////////////////////////////////////////////////
405
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000406SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
407SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
408SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
409SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
410SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
411 shader_type_mismatch);
412SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 4, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000413
bsalomon@google.com5782d712011-01-21 21:03:59 +0000414static const GrSamplerState::SampleMode sk_bmp_type_to_sample_mode[] = {
415 (GrSamplerState::SampleMode) -1, // kNone_BitmapType
416 GrSamplerState::kNormal_SampleMode, // kDefault_BitmapType
417 GrSamplerState::kRadial_SampleMode, // kRadial_BitmapType
418 GrSamplerState::kSweep_SampleMode, // kSweep_BitmapType
419 GrSamplerState::kRadial2_SampleMode, // kTwoPointRadial_BitmapType
420};
421
bsalomon@google.com84405e02012-03-05 19:57:21 +0000422namespace {
423
424// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
425// justAlpha indicates that skPaint's alpha should be used rather than the color
426// Callers may subsequently modify the GrPaint. Setting constantColor indicates
427// that the final paint will draw the same color at every pixel. This allows
428// an optimization where the the color filter can be applied to the skPaint's
429// color once while converting to GrPain and then ignored.
430inline bool skPaint2GrPaintNoShader(const SkPaint& skPaint,
431 bool justAlpha,
432 bool constantColor,
433 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000434
435 grPaint->fDither = skPaint.isDither();
436 grPaint->fAntiAlias = skPaint.isAntiAlias();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000437 grPaint->fCoverage = 0xFF;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000438
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000439 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
440 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000441
442 SkXfermode* mode = skPaint.getXfermode();
443 if (mode) {
444 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000445 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000446#if 0
447 return false;
448#endif
449 }
450 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000451 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
452 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
453
bsalomon@google.com5782d712011-01-21 21:03:59 +0000454 if (justAlpha) {
455 uint8_t alpha = skPaint.getAlpha();
456 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000457 // justAlpha is currently set to true only if there is a texture,
458 // so constantColor should not also be true.
459 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000460 } else {
461 grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000462 grPaint->setTexture(kShaderTextureIdx, NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000463 }
Scroggo97c88c22011-05-11 14:05:25 +0000464 SkColorFilter* colorFilter = skPaint.getColorFilter();
465 SkColor color;
466 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000467 SkScalar matrix[20];
Scroggo97c88c22011-05-11 14:05:25 +0000468 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000469 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000470 if (!constantColor) {
471 grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color);
472 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000473 } else {
474 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
475 grPaint->fColor = SkGr::SkColor2GrColor(filtered);
senorblanco@chromium.orgb3c20fa2012-01-03 21:20:19 +0000476 grPaint->resetColorFilter();
Scroggod757df22011-05-16 13:11:16 +0000477 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000478 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
479 grPaint->fColorMatrixEnabled = true;
480 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
481 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
482 } else {
483 grPaint->resetColorFilter();
Scroggo97c88c22011-05-11 14:05:25 +0000484 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000485 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000486}
487
bsalomon@google.com84405e02012-03-05 19:57:21 +0000488// This function is similar to skPaint2GrPaintNoShader but also converts
489// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
490// be used is set on grPaint and returned in param act. constantColor has the
491// same meaning as in skPaint2GrPaintNoShader.
492inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
493 const SkPaint& skPaint,
494 const SkMatrix& ctm,
495 bool constantColor,
496 SkGpuDevice::SkAutoCachedTexture* act,
497 GrPaint* grPaint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000498
bsalomon@google.com5782d712011-01-21 21:03:59 +0000499 SkASSERT(NULL != act);
reed@google.comac10a2d2010-12-22 21:39:39 +0000500
bsalomon@google.com5782d712011-01-21 21:03:59 +0000501 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000502 if (NULL == shader) {
bsalomon@google.com84405e02012-03-05 19:57:21 +0000503 return skPaint2GrPaintNoShader(skPaint,
504 false,
505 constantColor,
506 grPaint);
507 } else if (!skPaint2GrPaintNoShader(skPaint, true, false, grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000508 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000509 }
510
reed@google.comac10a2d2010-12-22 21:39:39 +0000511 SkBitmap bitmap;
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000512 SkMatrix* matrix = grPaint->textureSampler(kShaderTextureIdx)->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000513 SkShader::TileMode tileModes[2];
514 SkScalar twoPointParams[3];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000515 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000516 tileModes, twoPointParams);
517
bsalomon@google.com5782d712011-01-21 21:03:59 +0000518 GrSamplerState::SampleMode sampleMode = sk_bmp_type_to_sample_mode[bmptype];
519 if (-1 == sampleMode) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000520 SkShader::GradientInfo info;
521 SkColor color;
522
523 info.fColors = &color;
524 info.fColorOffsets = NULL;
525 info.fColorCount = 1;
526 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
527 SkPaint copy(skPaint);
528 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000529 // modulate the paint alpha by the shader's solid color alpha
530 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
531 copy.setColor(SkColorSetA(color, newA));
bsalomon@google.com84405e02012-03-05 19:57:21 +0000532 return skPaint2GrPaintNoShader(copy,
533 false,
534 constantColor,
535 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000536 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000537 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000538 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000539 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000540 sampler->setSampleMode(sampleMode);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000541 if (skPaint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000542 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000543 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000544 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000545 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000546 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
547 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000548 if (GrSamplerState::kRadial2_SampleMode == sampleMode) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000549 sampler->setRadial2Params(twoPointParams[0],
550 twoPointParams[1],
551 twoPointParams[2] < 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000552 }
553
bsalomon@google.com84405e02012-03-05 19:57:21 +0000554 GrTexture* texture = act->set(dev, bitmap, sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000555 if (NULL == texture) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000556 SkDebugf("Couldn't convert bitmap to texture.\n");
557 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000558 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000559 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000560
561 // since our texture coords will be in local space, we wack the texture
562 // matrix to map them back into 0...1 before we load it
563 SkMatrix localM;
564 if (shader->getLocalMatrix(&localM)) {
565 SkMatrix inverse;
566 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000567 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000568 }
569 }
570 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000571 GrScalar sx = GrFixedToScalar(GR_Fixed1 / bitmap.width());
572 GrScalar sy = GrFixedToScalar(GR_Fixed1 / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000573 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000574 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000575 GrScalar s = GrFixedToScalar(GR_Fixed1 / bitmap.width());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000576 matrix->postScale(s, s);
reed@google.comac10a2d2010-12-22 21:39:39 +0000577 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000578
579 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000580}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000581}
reed@google.comac10a2d2010-12-22 21:39:39 +0000582
583///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000584
bsalomon@google.com398109c2011-04-14 18:40:27 +0000585void SkGpuDevice::clear(SkColor color) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000586 fContext->clear(NULL, color);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000587}
588
reed@google.comac10a2d2010-12-22 21:39:39 +0000589void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
590 CHECK_SHOULD_DRAW(draw);
591
bsalomon@google.com5782d712011-01-21 21:03:59 +0000592 GrPaint grPaint;
593 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000594 if (!skPaint2GrPaintShader(this,
595 paint,
596 *draw.fMatrix,
597 true,
598 &act,
599 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000600 return;
601 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000602
603 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000604}
605
606// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000607static const GrPrimitiveType gPointMode2PrimtiveType[] = {
608 kPoints_PrimitiveType,
609 kLines_PrimitiveType,
610 kLineStrip_PrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000611};
612
613void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000614 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000615 CHECK_SHOULD_DRAW(draw);
616
617 SkScalar width = paint.getStrokeWidth();
618 if (width < 0) {
619 return;
620 }
621
622 // we only handle hairlines here, else we let the SkDraw call our drawPath()
623 if (width > 0) {
624 draw.drawPoints(mode, count, pts, paint, true);
625 return;
626 }
627
bsalomon@google.com5782d712011-01-21 21:03:59 +0000628 GrPaint grPaint;
629 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000630 if (!skPaint2GrPaintShader(this,
631 paint,
632 *draw.fMatrix,
633 true,
634 &act,
635 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000636 return;
637 }
638
bsalomon@google.com5782d712011-01-21 21:03:59 +0000639 fContext->drawVertices(grPaint,
640 gPointMode2PrimtiveType[mode],
641 count,
642 (GrPoint*)pts,
643 NULL,
644 NULL,
645 NULL,
646 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000647}
648
reed@google.comc9aa5872011-04-05 21:05:37 +0000649///////////////////////////////////////////////////////////////////////////////
650
reed@google.comac10a2d2010-12-22 21:39:39 +0000651void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
652 const SkPaint& paint) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000653 CHECK_SHOULD_DRAW(draw);
654
bungeman@google.com79bd8772011-07-18 15:34:08 +0000655 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000656 SkScalar width = paint.getStrokeWidth();
657
658 /*
659 We have special code for hairline strokes, miter-strokes, and fills.
660 Anything else we just call our path code.
661 */
662 bool usePath = doStroke && width > 0 &&
663 paint.getStrokeJoin() != SkPaint::kMiter_Join;
664 // another reason we might need to call drawPath...
665 if (paint.getMaskFilter()) {
666 usePath = true;
667 }
reed@google.com67db6642011-05-26 11:46:35 +0000668 // until we aa rotated rects...
669 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
670 usePath = true;
671 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000672 // small miter limit means right angles show bevel...
673 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
674 paint.getStrokeMiter() < SK_ScalarSqrt2)
675 {
676 usePath = true;
677 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000678 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000679 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
680 usePath = true;
681 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000682
683 if (usePath) {
684 SkPath path;
685 path.addRect(rect);
686 this->drawPath(draw, path, paint, NULL, true);
687 return;
688 }
689
690 GrPaint grPaint;
691 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000692 if (!skPaint2GrPaintShader(this,
693 paint,
694 *draw.fMatrix,
695 true,
696 &act,
697 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000698 return;
699 }
reed@google.com20efde72011-05-09 17:00:02 +0000700 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000701}
702
reed@google.com69302852011-02-16 18:08:07 +0000703#include "SkMaskFilter.h"
704#include "SkBounder.h"
705
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000706static GrPathFill skToGrFillType(SkPath::FillType fillType) {
707 switch (fillType) {
708 case SkPath::kWinding_FillType:
709 return kWinding_PathFill;
710 case SkPath::kEvenOdd_FillType:
711 return kEvenOdd_PathFill;
712 case SkPath::kInverseWinding_FillType:
713 return kInverseWinding_PathFill;
714 case SkPath::kInverseEvenOdd_FillType:
715 return kInverseEvenOdd_PathFill;
716 default:
717 SkDebugf("Unsupported path fill type\n");
718 return kHairLine_PathFill;
719 }
720}
721
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000722static GrTexture* applyMorphology(GrContext* context, GrTexture* texture,
723 const GrRect& srcRect,
724 GrTexture* temp1, GrTexture* temp2,
725 GrSamplerState::Filter filter,
726 SkISize radius) {
727 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
728 GrAutoMatrix avm(context, GrMatrix::I());
729 GrClip oldClip = context->getClip();
730 context->setClip(GrRect::MakeWH(texture->width(), texture->height()));
731 if (radius.fWidth > 0) {
732 context->setRenderTarget(temp1->asRenderTarget());
733 context->applyMorphology(texture, srcRect, radius.fWidth, filter,
734 GrSamplerState::kX_FilterDirection);
735 SkIRect clearRect = SkIRect::MakeXYWH(
736 srcRect.fLeft, srcRect.fBottom,
737 srcRect.width(), radius.fHeight);
738 context->clear(&clearRect, 0x0);
739 texture = temp1;
740 }
741 if (radius.fHeight > 0) {
742 context->setRenderTarget(temp2->asRenderTarget());
743 context->applyMorphology(texture, srcRect, radius.fHeight, filter,
744 GrSamplerState::kY_FilterDirection);
745 texture = temp2;
746 }
747 context->setRenderTarget(oldRenderTarget);
748 context->setClip(oldClip);
749 return texture;
750}
751
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000752static void buildKernel(float sigma, float* kernel, int kernelWidth) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000753 int halfWidth = (kernelWidth - 1) / 2;
754 float sum = 0.0f;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000755 float denom = 1.0f / (2.0f * sigma * sigma);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000756 for (int i = 0; i < kernelWidth; ++i) {
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000757 float x = static_cast<float>(i - halfWidth);
758 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
759 // is dropped here, since we renormalize the kernel below.
760 kernel[i] = sk_float_exp(- x * x * denom);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000761 sum += kernel[i];
762 }
763 // Normalize the kernel
764 float scale = 1.0f / sum;
765 for (int i = 0; i < kernelWidth; ++i)
766 kernel[i] *= scale;
767}
768
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000769static void scaleRect(SkRect* rect, float xScale, float yScale) {
770 rect->fLeft *= xScale;
771 rect->fTop *= yScale;
772 rect->fRight *= xScale;
773 rect->fBottom *= yScale;
774}
775
776static float adjustSigma(float sigma, int *scaleFactor, int *halfWidth,
777 int *kernelWidth) {
778 *scaleFactor = 1;
779 while (sigma > MAX_BLUR_SIGMA) {
780 *scaleFactor *= 2;
781 sigma *= 0.5f;
782 }
783 *halfWidth = static_cast<int>(ceilf(sigma * 3.0f));
784 *kernelWidth = *halfWidth * 2 + 1;
785 return sigma;
786}
787
788// Apply a Gaussian blur to srcTexture by sigmaX and sigmaY, within the given
789// rect.
790// temp1 and temp2 are used for allocation of intermediate textures.
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000791// If temp2 is non-NULL, srcTexture will be untouched, and the return
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000792// value will be either temp1 or temp2.
793// If temp2 is NULL, srcTexture will be overwritten with intermediate
794// results, and the return value will either be temp1 or srcTexture.
795static GrTexture* gaussianBlur(GrContext* context, GrTexture* srcTexture,
796 GrAutoScratchTexture* temp1,
797 GrAutoScratchTexture* temp2,
798 const SkRect& rect,
799 float sigmaX, float sigmaY) {
800
801 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
802 GrClip oldClip = context->getClip();
803 GrTexture* origTexture = srcTexture;
804 GrAutoMatrix avm(context, GrMatrix::I());
805 SkIRect clearRect;
806 int scaleFactorX, halfWidthX, kernelWidthX;
807 int scaleFactorY, halfWidthY, kernelWidthY;
808 sigmaX = adjustSigma(sigmaX, &scaleFactorX, &halfWidthX, &kernelWidthX);
809 sigmaY = adjustSigma(sigmaY, &scaleFactorY, &halfWidthY, &kernelWidthY);
810
811 SkRect srcRect(rect);
812 scaleRect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
813 srcRect.roundOut();
814 scaleRect(&srcRect, scaleFactorX, scaleFactorY);
815 context->setClip(srcRect);
816
817 const GrTextureDesc desc = {
818 kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit,
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000819 srcRect.width(),
820 srcRect.height(),
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000821 kRGBA_8888_GrPixelConfig,
822 {0} // samples
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000823 };
824
825 temp1->set(context, desc);
826 if (temp2) temp2->set(context, desc);
827
828 GrTexture* dstTexture = temp1->texture();
829 GrPaint paint;
830 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000831 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000832
833 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000834 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
835 srcTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000836 context->setRenderTarget(dstTexture->asRenderTarget());
837 SkRect dstRect(srcRect);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000838 scaleRect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000839 i < scaleFactorY ? 0.5f : 1.0f);
840 paint.setTexture(0, srcTexture);
841 context->drawRectToRect(paint, dstRect, srcRect);
842 srcRect = dstRect;
843 SkTSwap(srcTexture, dstTexture);
844 // If temp2 is non-NULL, don't render back to origTexture
845 if (temp2 && dstTexture == origTexture) dstTexture = temp2->texture();
846 }
847
848 if (sigmaX > 0.0f) {
849 SkAutoTMalloc<float> kernelStorageX(kernelWidthX);
850 float* kernelX = kernelStorageX.get();
851 buildKernel(sigmaX, kernelX, kernelWidthX);
852
853 if (scaleFactorX > 1) {
854 // Clear out a halfWidth to the right of the srcRect to prevent the
855 // X convolution from reading garbage.
856 clearRect = SkIRect::MakeXYWH(
857 srcRect.fRight, srcRect.fTop, halfWidthX, srcRect.height());
858 context->clear(&clearRect, 0x0);
859 }
860
861 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000862 context->convolve(srcTexture, srcRect, kernelX, kernelWidthX,
863 GrSamplerState::kX_FilterDirection);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000864 SkTSwap(srcTexture, dstTexture);
865 if (temp2 && dstTexture == origTexture) dstTexture = temp2->texture();
866 }
867
868 if (sigmaY > 0.0f) {
869 SkAutoTMalloc<float> kernelStorageY(kernelWidthY);
870 float* kernelY = kernelStorageY.get();
871 buildKernel(sigmaY, kernelY, kernelWidthY);
872
873 if (scaleFactorY > 1 || sigmaX > 0.0f) {
874 // Clear out a halfWidth below the srcRect to prevent the Y
875 // convolution from reading garbage.
876 clearRect = SkIRect::MakeXYWH(
877 srcRect.fLeft, srcRect.fBottom, srcRect.width(), halfWidthY);
878 context->clear(&clearRect, 0x0);
879 }
880
881 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000882 context->convolve(srcTexture, srcRect, kernelY, kernelWidthY,
883 GrSamplerState::kY_FilterDirection);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000884 SkTSwap(srcTexture, dstTexture);
885 if (temp2 && dstTexture == origTexture) dstTexture = temp2->texture();
886 }
887
888 if (scaleFactorX > 1 || scaleFactorY > 1) {
889 // Clear one pixel to the right and below, to accommodate bilinear
890 // upsampling.
891 clearRect = SkIRect::MakeXYWH(
892 srcRect.fLeft, srcRect.fBottom, srcRect.width() + 1, 1);
893 context->clear(&clearRect, 0x0);
894 clearRect = SkIRect::MakeXYWH(
895 srcRect.fRight, srcRect.fTop, 1, srcRect.height());
896 context->clear(&clearRect, 0x0);
897 // FIXME: This should be mitchell, not bilinear.
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000898 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000899 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
900 srcTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000901 context->setRenderTarget(dstTexture->asRenderTarget());
902 paint.setTexture(0, srcTexture);
903 SkRect dstRect(srcRect);
904 scaleRect(&dstRect, scaleFactorX, scaleFactorY);
905 context->drawRectToRect(paint, dstRect, srcRect);
906 srcRect = dstRect;
907 SkTSwap(srcTexture, dstTexture);
908 }
909 context->setRenderTarget(oldRenderTarget);
910 context->setClip(oldClip);
911 return srcTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000912}
913
914static bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
915 SkMaskFilter* filter, const SkMatrix& matrix,
916 const SkRegion& clip, SkBounder* bounder,
917 GrPaint* grp) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000918#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000919 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000920#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000921 SkMaskFilter::BlurInfo info;
922 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000923 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000924 return false;
925 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000926 SkScalar radius = info.fIgnoreTransform ? info.fRadius
927 : matrix.mapRadius(info.fRadius);
928 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000929 if (radius <= 0) {
930 return false;
931 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000932 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000933 float sigma3 = sigma * 3.0f;
934
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000935 SkRect srcRect = path.getBounds();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000936 SkRect clipRect;
937 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000938
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000939 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
940 srcRect.inset(-sigma3, -sigma3);
941 clipRect.inset(-sigma3, -sigma3);
942 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000943 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000944 SkIRect finalIRect;
945 finalRect.roundOut(&finalIRect);
946 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000947 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000948 }
949 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000950 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000951 }
952 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000953 srcRect.offset(offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000954 const GrTextureDesc desc = {
955 kRenderTarget_GrTextureFlagBit,
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000956 srcRect.width(),
957 srcRect.height(),
958 // We actually only need A8, but it often isn't supported as a
959 // render target
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000960 kRGBA_8888_PM_GrPixelConfig,
961 {0} // samples
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000962 };
963
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000964 GrAutoScratchTexture pathEntry(context, desc);
965 GrTexture* pathTexture = pathEntry.texture();
966 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000967 return false;
968 }
969 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000970 // Once this code moves into GrContext, this should be changed to use
971 // an AutoClipRestore.
972 GrClip oldClip = context->getClip();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000973 context->setRenderTarget(pathTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000974 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000975 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000976 GrPaint tempPaint;
977 tempPaint.reset();
978
979 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000980 tempPaint.fAntiAlias = grp->fAntiAlias;
981 if (tempPaint.fAntiAlias) {
982 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
983 // blend coeff of zero requires dual source blending support in order
984 // to properly blend partially covered pixels. This means the AA
985 // code path may not be taken. So we use a dst blend coeff of ISA. We
986 // could special case AA draws to a dst surface with known alpha=0 to
987 // use a zero dst coeff when dual source blending isn't available.
988 tempPaint.fSrcBlendCoeff = kOne_BlendCoeff;
989 tempPaint.fDstBlendCoeff = kISC_BlendCoeff;
990 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000991 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000992 context->drawPath(tempPaint, path, skToGrFillType(path.getFillType()), &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000993
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000994 GrAutoScratchTexture temp1, temp2;
995 // If we're doing a normal blur, we can clobber the pathTexture in the
996 // gaussianBlur. Otherwise, we need to save it for later compositing.
997 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
998 GrTexture* blurTexture = gaussianBlur(context, pathTexture,
999 &temp1, isNormalBlur ? NULL : &temp2,
1000 srcRect, sigma, sigma);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001001
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +00001002 if (!isNormalBlur) {
1003 GrPaint paint;
1004 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001005 paint.textureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001006 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
1007 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +00001008 // Blend pathTexture over blurTexture.
1009 context->setRenderTarget(blurTexture->asRenderTarget());
1010 paint.setTexture(0, pathTexture);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001011 if (SkMaskFilter::kInner_BlurType == blurType) {
1012 // inner: dst = dst * src
1013 paint.fSrcBlendCoeff = kDC_BlendCoeff;
1014 paint.fDstBlendCoeff = kZero_BlendCoeff;
1015 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
1016 // solid: dst = src + dst - src * dst
1017 // = (1 - dst) * src + 1 * dst
1018 paint.fSrcBlendCoeff = kIDC_BlendCoeff;
1019 paint.fDstBlendCoeff = kOne_BlendCoeff;
1020 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
1021 // outer: dst = dst * (1 - src)
1022 // = 0 * src + (1 - src) * dst
1023 paint.fSrcBlendCoeff = kZero_BlendCoeff;
1024 paint.fDstBlendCoeff = kISC_BlendCoeff;
1025 }
1026 context->drawRect(paint, srcRect);
1027 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001028 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +00001029 context->setClip(oldClip);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001030
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001031 if (grp->hasTextureOrMask()) {
1032 GrMatrix inverse;
1033 if (!matrix.invert(&inverse)) {
1034 return false;
1035 }
1036 grp->preConcatActiveSamplerMatrices(inverse);
1037 }
1038
1039 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
1040 // we assume the last mask index is available for use
1041 GrAssert(NULL == grp->getMask(MASK_IDX));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +00001042 grp->setMask(MASK_IDX, blurTexture);
bsalomon@google.com97912912011-12-06 16:30:36 +00001043 grp->maskSampler(MASK_IDX)->reset();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001044
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001045 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
1046 -finalRect.fTop);
1047 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
1048 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001049 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001050 return true;
1051}
1052
reed@google.com69302852011-02-16 18:08:07 +00001053static bool drawWithMaskFilter(GrContext* context, const SkPath& path,
1054 SkMaskFilter* filter, const SkMatrix& matrix,
1055 const SkRegion& clip, SkBounder* bounder,
1056 GrPaint* grp) {
1057 SkMask srcM, dstM;
1058
1059 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
1060 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
1061 return false;
1062 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001063 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +00001064
1065 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
1066 return false;
1067 }
1068 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +00001069 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +00001070
1071 if (clip.quickReject(dstM.fBounds)) {
1072 return false;
1073 }
1074 if (bounder && !bounder->doIRect(dstM.fBounds)) {
1075 return false;
1076 }
1077
1078 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
1079 // the current clip (and identity matrix) and grpaint settings
1080
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001081 // used to compute inverse view, if necessary
1082 GrMatrix ivm = context->getMatrix();
1083
reed@google.com0c219b62011-02-16 21:31:18 +00001084 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +00001085
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001086 const GrTextureDesc desc = {
1087 kNone_GrTextureFlags,
reed@google.com69302852011-02-16 18:08:07 +00001088 dstM.fBounds.width(),
1089 dstM.fBounds.height(),
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001090 kAlpha_8_GrPixelConfig,
1091 {0}, // samples
reed@google.com69302852011-02-16 18:08:07 +00001092 };
1093
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001094 GrAutoScratchTexture ast(context, desc);
1095 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001096
reed@google.com69302852011-02-16 18:08:07 +00001097 if (NULL == texture) {
1098 return false;
1099 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001100 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001101 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +00001102
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001103 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
1104 grp->preConcatActiveSamplerMatrices(ivm);
1105 }
1106
1107 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
1108 // we assume the last mask index is available for use
1109 GrAssert(NULL == grp->getMask(MASK_IDX));
1110 grp->setMask(MASK_IDX, texture);
bsalomon@google.com97912912011-12-06 16:30:36 +00001111 grp->maskSampler(MASK_IDX)->reset();
reed@google.com69302852011-02-16 18:08:07 +00001112
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001113 GrRect d;
1114 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +00001115 GrIntToScalar(dstM.fBounds.fTop),
1116 GrIntToScalar(dstM.fBounds.fRight),
1117 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001118
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001119 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
1120 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
1121 -dstM.fBounds.fTop*SK_Scalar1);
1122 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001123 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +00001124 return true;
1125}
reed@google.com69302852011-02-16 18:08:07 +00001126
reed@google.com0c219b62011-02-16 21:31:18 +00001127void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001128 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +00001129 bool pathIsMutable) {
1130 CHECK_SHOULD_DRAW(draw);
1131
reed@google.comfe626382011-09-21 13:50:35 +00001132 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001133
bsalomon@google.com5782d712011-01-21 21:03:59 +00001134 GrPaint grPaint;
1135 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001136 if (!skPaint2GrPaintShader(this,
1137 paint,
1138 *draw.fMatrix,
1139 true,
1140 &act,
1141 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001142 return;
1143 }
1144
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +00001145 // can we cheat, and threat a thin stroke as a hairline w/ coverage
1146 // if we can, we draw lots faster (raster device does this same test)
1147 SkScalar hairlineCoverage;
1148 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
1149 doFill = false;
1150 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
1151 grPaint.fCoverage);
1152 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001153
reed@google.comfe626382011-09-21 13:50:35 +00001154 // If we have a prematrix, apply it to the path, optimizing for the case
1155 // where the original path can in fact be modified in place (even though
1156 // its parameter type is const).
1157 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1158 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001159
1160 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001161 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001162
reed@google.come3445642011-02-16 23:20:39 +00001163 if (!pathIsMutable) {
1164 result = &tmpPath;
1165 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001166 }
reed@google.come3445642011-02-16 23:20:39 +00001167 // should I push prePathMatrix on our MV stack temporarily, instead
1168 // of applying it here? See SkDraw.cpp
1169 pathPtr->transform(*prePathMatrix, result);
1170 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001171 }
reed@google.com0c219b62011-02-16 21:31:18 +00001172 // at this point we're done with prePathMatrix
1173 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001174
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001175 if (paint.getPathEffect() ||
1176 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001177 // it is safe to use tmpPath here, even if we already used it for the
1178 // prepathmatrix, since getFillPath can take the same object for its
1179 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001180 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001181 pathPtr = &tmpPath;
1182 }
1183
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001184 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001185 // avoid possibly allocating a new path in transform if we can
1186 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1187
1188 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001189 pathPtr->transform(*draw.fMatrix, devPathPtr);
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001190 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001191 *draw.fMatrix, *draw.fClip, draw.fBounder,
1192 &grPaint)) {
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001193 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001194 *draw.fMatrix, *draw.fClip, draw.fBounder,
1195 &grPaint);
1196 }
reed@google.com69302852011-02-16 18:08:07 +00001197 return;
1198 }
reed@google.com69302852011-02-16 18:08:07 +00001199
bsalomon@google.comffca4002011-02-22 20:34:01 +00001200 GrPathFill fill = kHairLine_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001201
reed@google.com0c219b62011-02-16 21:31:18 +00001202 if (doFill) {
1203 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001204 case SkPath::kWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001205 fill = kWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001206 break;
1207 case SkPath::kEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001208 fill = kEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001209 break;
1210 case SkPath::kInverseWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001211 fill = kInverseWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001212 break;
1213 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001214 fill = kInverseEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001215 break;
1216 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001217 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001218 return;
1219 }
1220 }
1221
reed@google.com07f3ee12011-05-16 17:21:57 +00001222 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001223}
1224
bsalomon@google.comfb309512011-11-30 14:13:48 +00001225namespace {
1226
1227inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1228 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1229 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1230 return tilesX * tilesY;
1231}
1232
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001233inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001234 const SkIRect* srcRectPtr,
1235 int maxTextureSize) {
1236 static const int kSmallTileSize = 1 << 10;
1237 if (maxTextureSize <= kSmallTileSize) {
1238 return maxTextureSize;
1239 }
1240
1241 size_t maxTexTotalTileSize;
1242 size_t smallTotalTileSize;
1243
1244 if (NULL == srcRectPtr) {
1245 int w = bitmap.width();
1246 int h = bitmap.height();
1247 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1248 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1249 } else {
1250 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1251 srcRectPtr->fTop,
1252 srcRectPtr->fRight,
1253 srcRectPtr->fBottom,
1254 maxTextureSize);
1255 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1256 srcRectPtr->fTop,
1257 srcRectPtr->fRight,
1258 srcRectPtr->fBottom,
1259 kSmallTileSize);
1260 }
1261 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1262 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1263
1264 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1265 return kSmallTileSize;
1266 } else {
1267 return maxTextureSize;
1268 }
1269}
1270}
1271
1272bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1273 const GrSamplerState& sampler,
1274 const SkIRect* srcRectPtr,
1275 int* tileSize) const {
1276 SkASSERT(NULL != tileSize);
1277
1278 // if bitmap is explictly texture backed then just use the texture
1279 if (NULL != bitmap.getTexture()) {
1280 return false;
1281 }
1282 // if it's larger than the max texture size, then we have no choice but
1283 // tiling
1284 const int maxTextureSize = fContext->getMaxTextureSize();
1285 if (bitmap.width() > maxTextureSize ||
1286 bitmap.height() > maxTextureSize) {
1287 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1288 return true;
1289 }
1290 // if we are going to have to draw the whole thing, then don't tile
1291 if (NULL == srcRectPtr) {
1292 return false;
1293 }
1294 // if the entire texture is already in our cache then no reason to tile it
1295 if (this->isBitmapInTextureCache(bitmap, sampler)) {
1296 return false;
1297 }
1298
1299 // At this point we know we could do the draw by uploading the entire bitmap
1300 // as a texture. However, if the texture would be large compared to the
1301 // cache size and we don't require most of it for this draw then tile to
1302 // reduce the amount of upload and cache spill.
1303
1304 // assumption here is that sw bitmap size is a good proxy for its size as
1305 // a texture
1306 size_t bmpSize = bitmap.getSize();
1307 size_t cacheSize;
1308 fContext->getTextureCacheLimits(NULL, &cacheSize);
1309 if (bmpSize < cacheSize / 2) {
1310 return false;
1311 }
1312
1313 SkFixed fracUsed =
1314 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1315 (srcRectPtr->height() << 16) / bitmap.height());
1316 if (fracUsed <= SK_FixedHalf) {
1317 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1318 return true;
1319 } else {
1320 return false;
1321 }
1322}
1323
reed@google.comac10a2d2010-12-22 21:39:39 +00001324void SkGpuDevice::drawBitmap(const SkDraw& draw,
1325 const SkBitmap& bitmap,
1326 const SkIRect* srcRectPtr,
1327 const SkMatrix& m,
1328 const SkPaint& paint) {
1329 CHECK_SHOULD_DRAW(draw);
1330
1331 SkIRect srcRect;
1332 if (NULL == srcRectPtr) {
1333 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1334 } else {
1335 srcRect = *srcRectPtr;
1336 }
1337
junov@google.comd935cfb2011-06-27 20:48:23 +00001338 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001339 // Convert the bitmap to a shader so that the rect can be drawn
1340 // through drawRect, which supports mask filters.
1341 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001342 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001343 if (srcRectPtr) {
1344 if (!bitmap.extractSubset(&tmp, srcRect)) {
1345 return; // extraction failed
1346 }
1347 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001348 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001349 }
1350 SkPaint paintWithTexture(paint);
1351 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1352 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001353 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001354 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001355
junov@google.com1d329782011-07-28 20:10:09 +00001356 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001357 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001358 // also affect the behavior of the mask filter.
1359 SkMatrix drawMatrix;
1360 drawMatrix.setConcat(*draw.fMatrix, m);
1361 SkDraw transformedDraw(draw);
1362 transformedDraw.fMatrix = &drawMatrix;
1363
1364 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1365
junov@google.comd935cfb2011-06-27 20:48:23 +00001366 return;
1367 }
1368
bsalomon@google.com5782d712011-01-21 21:03:59 +00001369 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001370 if (!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001371 return;
1372 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001373 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001374 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001375 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001376 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001377 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001378 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001379
bsalomon@google.comfb309512011-11-30 14:13:48 +00001380 int tileSize;
1381 if (!this->shouldTileBitmap(bitmap, *sampler, srcRectPtr, &tileSize)) {
1382 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001383 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001384 return;
1385 }
1386
1387 // undo the translate done by SkCanvas
1388 int DX = SkMax32(0, srcRect.fLeft);
1389 int DY = SkMax32(0, srcRect.fTop);
1390 // compute clip bounds in local coordinates
1391 SkIRect clipRect;
1392 {
1393 SkRect r;
1394 r.set(draw.fClip->getBounds());
1395 SkMatrix matrix, inverse;
1396 matrix.setConcat(*draw.fMatrix, m);
1397 if (!matrix.invert(&inverse)) {
1398 return;
1399 }
1400 inverse.mapRect(&r);
1401 r.roundOut(&clipRect);
1402 // apply the canvas' translate to our local clip
1403 clipRect.offset(DX, DY);
1404 }
1405
bsalomon@google.comfb309512011-11-30 14:13:48 +00001406 int nx = bitmap.width() / tileSize;
1407 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001408 for (int x = 0; x <= nx; x++) {
1409 for (int y = 0; y <= ny; y++) {
1410 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001411 tileR.set(x * tileSize, y * tileSize,
1412 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001413 if (!SkIRect::Intersects(tileR, clipRect)) {
1414 continue;
1415 }
1416
1417 SkIRect srcR = tileR;
1418 if (!srcR.intersect(srcRect)) {
1419 continue;
1420 }
1421
1422 SkBitmap tmpB;
1423 if (bitmap.extractSubset(&tmpB, tileR)) {
1424 // now offset it to make it "local" to our tmp bitmap
1425 srcR.offset(-tileR.fLeft, -tileR.fTop);
1426
1427 SkMatrix tmpM(m);
1428 {
1429 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1430 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1431 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1432 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001433 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001434 }
1435 }
1436 }
1437}
1438
1439/*
1440 * This is called by drawBitmap(), which has to handle images that may be too
1441 * large to be represented by a single texture.
1442 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001443 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1444 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001445 */
1446void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1447 const SkBitmap& bitmap,
1448 const SkIRect& srcRect,
1449 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001450 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001451 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1452 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001453
reed@google.com9c49bc32011-07-07 13:42:37 +00001454 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001455 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001456 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001457 return;
1458 }
1459
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001460 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001461
1462 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1463 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
1464 sampler->setSampleMode(GrSamplerState::kNormal_SampleMode);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001465 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001466
1467 GrTexture* texture;
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001468 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001469 if (NULL == texture) {
1470 return;
1471 }
1472
bsalomon@google.com452943d2011-10-31 17:37:14 +00001473 grPaint->setTexture(kBitmapTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001474
reed@google.com20efde72011-05-09 17:00:02 +00001475 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1476 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001477 GrRect paintRect;
junov@google.com6acc9b32011-05-16 18:32:07 +00001478 paintRect.setLTRB(GrFixedToScalar((srcRect.fLeft << 16) / bitmap.width()),
1479 GrFixedToScalar((srcRect.fTop << 16) / bitmap.height()),
1480 GrFixedToScalar((srcRect.fRight << 16) / bitmap.width()),
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001481 GrFixedToScalar((srcRect.fBottom << 16) / bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001482
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001483 if (GrSamplerState::kNearest_Filter != sampler->getFilter() &&
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001484 (srcRect.width() < bitmap.width() ||
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001485 srcRect.height() < bitmap.height())) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001486 // If drawing a subrect of the bitmap and filtering is enabled,
1487 // use a constrained texture domain to avoid color bleeding
1488 GrScalar left, top, right, bottom;
1489 if (srcRect.width() > 1) {
1490 GrScalar border = GR_ScalarHalf / bitmap.width();
1491 left = paintRect.left() + border;
1492 right = paintRect.right() - border;
1493 } else {
1494 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1495 }
1496 if (srcRect.height() > 1) {
1497 GrScalar border = GR_ScalarHalf / bitmap.height();
1498 top = paintRect.top() + border;
1499 bottom = paintRect.bottom() - border;
1500 } else {
1501 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1502 }
1503 GrRect textureDomain;
1504 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001505 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001506 }
1507
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001508 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001509}
1510
1511void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1512 int left, int top, const SkPaint& paint) {
1513 CHECK_SHOULD_DRAW(draw);
1514
1515 SkAutoLockPixels alp(bitmap);
1516 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1517 return;
1518 }
1519
reed@google.com76dd2772012-01-05 21:15:07 +00001520 int w = bitmap.width();
1521 int h = bitmap.height();
1522
bsalomon@google.com5782d712011-01-21 21:03:59 +00001523 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001524 if(!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001525 return;
1526 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001527
bsalomon@google.com5782d712011-01-21 21:03:59 +00001528 GrAutoMatrix avm(fContext, GrMatrix::I());
1529
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001530 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001531
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001532 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001533 sampler->reset();
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001534 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001535
reed@google.com76dd2772012-01-05 21:15:07 +00001536 SkImageFilter* imageFilter = paint.getImageFilter();
1537 SkSize blurSize;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001538 SkISize radius;
reed@google.com76dd2772012-01-05 21:15:07 +00001539 if (NULL != imageFilter && imageFilter->asABlur(&blurSize)) {
1540 GrAutoScratchTexture temp1, temp2;
1541 GrTexture* blurTexture = gaussianBlur(fContext,
1542 texture, &temp1, &temp2,
1543 GrRect::MakeWH(w, h),
1544 blurSize.width(),
1545 blurSize.height());
1546 texture = blurTexture;
1547 grPaint.setTexture(kBitmapTextureIdx, texture);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001548 } else if (NULL != imageFilter && imageFilter->asADilate(&radius)) {
1549 const GrTextureDesc desc = {
1550 kRenderTarget_GrTextureFlagBit,
1551 w,
1552 h,
1553 kRGBA_8888_PM_GrPixelConfig,
1554 {0} // samples
1555 };
1556 GrAutoScratchTexture temp1(fContext, desc), temp2(fContext, desc);
1557 texture = applyMorphology(fContext, texture, GrRect::MakeWH(w, h),
1558 temp1.texture(), temp2.texture(),
1559 GrSamplerState::kDilate_Filter, radius);
1560 grPaint.setTexture(kBitmapTextureIdx, texture);
1561 } else if (NULL != imageFilter && imageFilter->asAnErode(&radius)) {
1562 const GrTextureDesc desc = {
1563 kRenderTarget_GrTextureFlagBit,
1564 w,
1565 h,
1566 kRGBA_8888_PM_GrPixelConfig,
1567 {0} // samples
1568 };
1569 GrAutoScratchTexture temp1(fContext, desc), temp2(fContext, desc);
1570 texture = applyMorphology(fContext, texture, GrRect::MakeWH(w, h),
1571 temp1.texture(), temp2.texture(),
1572 GrSamplerState::kErode_Filter, radius);
1573 grPaint.setTexture(kBitmapTextureIdx, texture);
reed@google.com76dd2772012-01-05 21:15:07 +00001574 } else {
1575 grPaint.setTexture(kBitmapTextureIdx, texture);
1576 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001577
bsalomon@google.com5782d712011-01-21 21:03:59 +00001578 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001579 GrRect::MakeXYWH(GrIntToScalar(left),
1580 GrIntToScalar(top),
1581 GrIntToScalar(w),
1582 GrIntToScalar(h)),
1583 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1584 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001585}
1586
1587void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* dev,
1588 int x, int y, const SkPaint& paint) {
1589 CHECK_SHOULD_DRAW(draw);
1590
bsalomon@google.com5782d712011-01-21 21:03:59 +00001591 GrPaint grPaint;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001592 if (!((SkGpuDevice*)dev)->bindDeviceAsTexture(&grPaint) ||
bsalomon@google.com84405e02012-03-05 19:57:21 +00001593 !skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001594 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001595 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001596
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001597 GrTexture* devTex = grPaint.getTexture(0);
1598 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001599
1600 const SkBitmap& bm = dev->accessBitmap(false);
1601 int w = bm.width();
1602 int h = bm.height();
1603
1604 GrAutoMatrix avm(fContext, GrMatrix::I());
1605
bsalomon@google.com97912912011-12-06 16:30:36 +00001606 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001607
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001608 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1609 GrIntToScalar(y),
1610 GrIntToScalar(w),
1611 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001612
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001613 // The device being drawn may not fill up its texture (saveLayer uses
1614 // the approximate ).
1615 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1616 GR_Scalar1 * h / devTex->height());
1617
1618 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001619}
1620
reed@google.com76dd2772012-01-05 21:15:07 +00001621bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1622 const SkMatrix& ctm,
1623 SkBitmap* result, SkIPoint* offset) {
1624 SkSize size;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001625 SkISize radius;
1626 if (!filter->asABlur(&size) && !filter->asADilate(&radius) && !filter->asAnErode(&radius)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001627 return false;
1628 }
1629 SkDevice* dev = this->createCompatibleDevice(SkBitmap::kARGB_8888_Config,
1630 src.width(),
1631 src.height(),
1632 false);
1633 if (NULL == dev) {
1634 return false;
1635 }
1636 SkAutoUnref aur(dev);
1637 SkCanvas canvas(dev);
1638 SkPaint paint;
1639 paint.setImageFilter(filter);
1640 canvas.drawSprite(src, 0, 0, &paint);
1641 *result = dev->accessBitmap(false);
1642 return true;
1643}
1644
reed@google.comac10a2d2010-12-22 21:39:39 +00001645///////////////////////////////////////////////////////////////////////////////
1646
1647// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001648static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1649 kTriangles_PrimitiveType,
1650 kTriangleStrip_PrimitiveType,
1651 kTriangleFan_PrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001652};
1653
1654void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1655 int vertexCount, const SkPoint vertices[],
1656 const SkPoint texs[], const SkColor colors[],
1657 SkXfermode* xmode,
1658 const uint16_t indices[], int indexCount,
1659 const SkPaint& paint) {
1660 CHECK_SHOULD_DRAW(draw);
1661
bsalomon@google.com5782d712011-01-21 21:03:59 +00001662 GrPaint grPaint;
1663 SkAutoCachedTexture act;
1664 // we ignore the shader if texs is null.
1665 if (NULL == texs) {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001666 if (!skPaint2GrPaintNoShader(paint,
1667 false,
1668 NULL == colors,
1669 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001670 return;
1671 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001672 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001673 if (!skPaint2GrPaintShader(this,
1674 paint,
1675 *draw.fMatrix,
1676 NULL == colors,
1677 &act,
1678 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001679 return;
1680 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001681 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001682
1683 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001684 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001685 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1686#if 0
1687 return
1688#endif
1689 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001690 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001691
bsalomon@google.com498776a2011-08-16 19:20:44 +00001692 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1693 if (NULL != colors) {
1694 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001695 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001696 for (int i = 0; i < vertexCount; ++i) {
1697 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1698 }
1699 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001700 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001701 fContext->drawVertices(grPaint,
1702 gVertexMode2PrimitiveType[vmode],
1703 vertexCount,
1704 (GrPoint*) vertices,
1705 (GrPoint*) texs,
1706 colors,
1707 indices,
1708 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001709}
1710
1711///////////////////////////////////////////////////////////////////////////////
1712
1713static void GlyphCacheAuxProc(void* data) {
1714 delete (GrFontScaler*)data;
1715}
1716
1717static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1718 void* auxData;
1719 GrFontScaler* scaler = NULL;
1720 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1721 scaler = (GrFontScaler*)auxData;
1722 }
1723 if (NULL == scaler) {
1724 scaler = new SkGrFontScaler(cache);
1725 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1726 }
1727 return scaler;
1728}
1729
1730static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1731 SkFixed fx, SkFixed fy,
1732 const SkGlyph& glyph) {
1733 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1734
bungeman@google.com15865a72012-01-11 16:28:04 +00001735 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001736
1737 if (NULL == procs->fFontScaler) {
1738 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1739 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001740
bungeman@google.com15865a72012-01-11 16:28:04 +00001741 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1742 glyph.getSubXFixed(),
1743 glyph.getSubYFixed()),
1744 SkFixedFloorToFixed(fx),
1745 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001746 procs->fFontScaler);
1747}
1748
bsalomon@google.com5782d712011-01-21 21:03:59 +00001749SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001750
1751 // deferred allocation
1752 if (NULL == fDrawProcs) {
1753 fDrawProcs = new GrSkDrawProcs;
1754 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1755 fDrawProcs->fContext = fContext;
1756 }
1757
1758 // init our (and GL's) state
1759 fDrawProcs->fTextContext = context;
1760 fDrawProcs->fFontScaler = NULL;
1761 return fDrawProcs;
1762}
1763
1764void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1765 size_t byteLength, SkScalar x, SkScalar y,
1766 const SkPaint& paint) {
1767 CHECK_SHOULD_DRAW(draw);
1768
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001769 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001770 // this guy will just call our drawPath()
1771 draw.drawText((const char*)text, byteLength, x, y, paint);
1772 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001773 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001774
1775 GrPaint grPaint;
1776 SkAutoCachedTexture act;
1777
bsalomon@google.com84405e02012-03-05 19:57:21 +00001778 if (!skPaint2GrPaintShader(this,
1779 paint,
1780 *draw.fMatrix,
1781 true,
1782 &act,
1783 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001784 return;
1785 }
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001786 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001787 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001788 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1789 }
1790}
1791
1792void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1793 size_t byteLength, const SkScalar pos[],
1794 SkScalar constY, int scalarsPerPos,
1795 const SkPaint& paint) {
1796 CHECK_SHOULD_DRAW(draw);
1797
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001798 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001799 // this guy will just call our drawPath()
1800 draw.drawPosText((const char*)text, byteLength, pos, constY,
1801 scalarsPerPos, paint);
1802 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001803 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001804
1805 GrPaint grPaint;
1806 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001807 if (!skPaint2GrPaintShader(this,
1808 paint,
1809 *draw.fMatrix,
1810 true,
1811 &act,
1812 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001813 return;
1814 }
1815
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001816 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001817 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001818 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1819 scalarsPerPos, paint);
1820 }
1821}
1822
1823void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1824 size_t len, const SkPath& path,
1825 const SkMatrix* m, const SkPaint& paint) {
1826 CHECK_SHOULD_DRAW(draw);
1827
1828 SkASSERT(draw.fDevice == this);
1829 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1830}
1831
1832///////////////////////////////////////////////////////////////////////////////
1833
reed@google.comf67e4cf2011-03-15 20:56:58 +00001834bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1835 if (!paint.isLCDRenderText()) {
1836 // we're cool with the paint as is
1837 return false;
1838 }
1839
1840 if (paint.getShader() ||
1841 paint.getXfermode() || // unless its srcover
1842 paint.getMaskFilter() ||
1843 paint.getRasterizer() ||
1844 paint.getColorFilter() ||
1845 paint.getPathEffect() ||
1846 paint.isFakeBoldText() ||
1847 paint.getStyle() != SkPaint::kFill_Style) {
1848 // turn off lcd
1849 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1850 flags->fHinting = paint.getHinting();
1851 return true;
1852 }
1853 // we're cool with the paint as is
1854 return false;
1855}
1856
reed@google.com75d939b2011-12-07 15:07:23 +00001857void SkGpuDevice::flush() {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001858 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001859}
1860
reed@google.comf67e4cf2011-03-15 20:56:58 +00001861///////////////////////////////////////////////////////////////////////////////
1862
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001863SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(const SkBitmap& bitmap,
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001864 const GrSamplerState* sampler,
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001865 TexType type) {
1866 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001867 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001868
bsalomon@google.come97f0852011-06-17 13:10:25 +00001869 if (kBitmap_TexType != type) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001870 const GrTextureDesc desc = {
1871 kRenderTarget_GrTextureFlagBit,
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001872 bitmap.width(),
1873 bitmap.height(),
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001874 SkGr::Bitmap2PixelConfig(bitmap),
1875 {0} // samples
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001876 };
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001877 GrContext::ScratchTexMatch match;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001878 if (kSaveLayerDeviceRenderTarget_TexType == type) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001879 // we know layers will only be drawn through drawDevice.
1880 // drawDevice has been made to work with content embedded in a
1881 // larger texture so its okay to use the approximate version.
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001882 match = GrContext::kApprox_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001883 } else {
bsalomon@google.come97f0852011-06-17 13:10:25 +00001884 SkASSERT(kDeviceRenderTarget_TexType == type);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001885 match = GrContext::kExact_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001886 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001887 entry = ctx->lockScratchTexture(desc, match);
reed@google.comac10a2d2010-12-22 21:39:39 +00001888 } else {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001889 if (!bitmap.isVolatile()) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001890 GrContext::TextureKey key = bitmap.getGenerationID();
1891 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001892
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001893 entry = ctx->findAndLockTexture(key, bitmap.width(),
1894 bitmap.height(), sampler);
1895 if (NULL == entry.texture()) {
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001896 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
junov@google.com4ee7ae52011-06-30 17:30:49 +00001897 bitmap);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001898 }
junov@google.com4ee7ae52011-06-30 17:30:49 +00001899 } else {
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001900 entry = sk_gr_create_bitmap_texture(ctx, gUNCACHED_KEY,
1901 sampler, bitmap);
junov@google.com4ee7ae52011-06-30 17:30:49 +00001902 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001903 if (NULL == entry.texture()) {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001904 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1905 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001906 }
1907 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001908 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001909}
1910
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001911void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1912 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001913}
1914
bsalomon@google.comfb309512011-11-30 14:13:48 +00001915bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
1916 const GrSamplerState& sampler) const {
1917 GrContext::TextureKey key = bitmap.getGenerationID();
1918 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
1919 return this->context()->isTextureInCache(key, bitmap.width(),
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001920 bitmap.height(), &sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001921
1922}
1923
1924
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001925SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1926 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001927 bool isOpaque,
1928 Usage usage) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001929 return SkNEW_ARGS(SkGpuDevice,(this->context(), config,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001930 width, height, usage));
1931}
1932