blob: 0ea34528e2fd356cadc859e9732fda5aed4d3449 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#include "GrContext.h"
12#include "GrTextContext.h"
13
reed@google.comac10a2d2010-12-22 21:39:39 +000014#include "SkGpuDevice.h"
15#include "SkGrTexturePixelRef.h"
16
Scroggo97c88c22011-05-11 14:05:25 +000017#include "SkColorFilter.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000018#include "SkDrawProcs.h"
19#include "SkGlyphCache.h"
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +000020#include "SkImageFilter.h"
reed@google.comfe626382011-09-21 13:50:35 +000021#include "SkTLazy.h"
reed@google.comc9aa5872011-04-05 21:05:37 +000022#include "SkUtils.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000023
24#define CACHE_LAYER_TEXTURES 1
25
26#if 0
27 extern bool (*gShouldDrawProc)();
28 #define CHECK_SHOULD_DRAW(draw) \
29 do { \
30 if (gShouldDrawProc && !gShouldDrawProc()) return; \
31 this->prepareRenderTarget(draw); \
32 } while (0)
33#else
34 #define CHECK_SHOULD_DRAW(draw) this->prepareRenderTarget(draw)
35#endif
36
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000037// we use the same texture slot on GrPaint for bitmaps and shaders
38// (since drawBitmap, drawSprite, and drawDevice ignore skia's shader)
39enum {
40 kBitmapTextureIdx = 0,
41 kShaderTextureIdx = 0
42};
43
reed@google.comcde92112011-07-06 20:00:52 +000044
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +000045#define MAX_BLUR_SIGMA 4.0f
46// FIXME: This value comes from from SkBlurMaskFilter.cpp.
47// Should probably be put in a common header someplace.
48#define MAX_BLUR_RADIUS SkIntToScalar(128)
49// This constant approximates the scaling done in the software path's
50// "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
51// IMHO, it actually should be 1: we blur "less" than we should do
52// according to the CSS and canvas specs, simply because Safari does the same.
53// Firefox used to do the same too, until 4.0 where they fixed it. So at some
54// point we should probably get rid of these scaling constants and rebaseline
55// all the blur tests.
56#define BLUR_SIGMA_SCALE 0.6f
reed@google.comac10a2d2010-12-22 21:39:39 +000057///////////////////////////////////////////////////////////////////////////////
58
59SkGpuDevice::SkAutoCachedTexture::
60 SkAutoCachedTexture(SkGpuDevice* device,
61 const SkBitmap& bitmap,
bsalomon@google.com1fadb202011-12-12 16:10:08 +000062 const GrSamplerState* sampler,
reed@google.comac10a2d2010-12-22 21:39:39 +000063 GrTexture** texture) {
64 GrAssert(texture);
reed@google.comac10a2d2010-12-22 21:39:39 +000065 *texture = this->set(device, bitmap, sampler);
66}
67
68SkGpuDevice::SkAutoCachedTexture::SkAutoCachedTexture() {
reed@google.comac10a2d2010-12-22 21:39:39 +000069}
70
71GrTexture* SkGpuDevice::SkAutoCachedTexture::set(SkGpuDevice* device,
72 const SkBitmap& bitmap,
bsalomon@google.com1fadb202011-12-12 16:10:08 +000073 const GrSamplerState* sampler) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000074 if (fTex.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +000075 fDevice->unlockCachedTexture(fTex);
76 }
77 fDevice = device;
78 GrTexture* texture = (GrTexture*)bitmap.getTexture();
79 if (texture) {
80 // return the native texture
bsalomon@google.com50398bf2011-07-26 20:45:30 +000081 fTex.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +000082 } else {
83 // look it up in our cache
bsalomon@google.com50398bf2011-07-26 20:45:30 +000084 fTex = device->lockCachedTexture(bitmap, sampler);
85 texture = fTex.texture();
reed@google.comac10a2d2010-12-22 21:39:39 +000086 }
87 return texture;
88}
89
90SkGpuDevice::SkAutoCachedTexture::~SkAutoCachedTexture() {
bsalomon@google.com50398bf2011-07-26 20:45:30 +000091 if (fTex.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +000092 fDevice->unlockCachedTexture(fTex);
93 }
94}
95
96///////////////////////////////////////////////////////////////////////////////
97
98bool gDoTraceDraw;
99
100struct GrSkDrawProcs : public SkDrawProcs {
101public:
102 GrContext* fContext;
103 GrTextContext* fTextContext;
104 GrFontScaler* fFontScaler; // cached in the skia glyphcache
105};
106
107///////////////////////////////////////////////////////////////////////////////
108
reed@google.comaf951c92011-06-16 19:10:39 +0000109static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
110 switch (config) {
111 case kAlpha_8_GrPixelConfig:
112 *isOpaque = false;
113 return SkBitmap::kA8_Config;
114 case kRGB_565_GrPixelConfig:
115 *isOpaque = true;
116 return SkBitmap::kRGB_565_Config;
117 case kRGBA_4444_GrPixelConfig:
118 *isOpaque = false;
119 return SkBitmap::kARGB_4444_Config;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000120 case kSkia8888_PM_GrPixelConfig:
121 // we don't currently have a way of knowing whether
122 // a 8888 is opaque based on the config.
123 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000124 return SkBitmap::kARGB_8888_Config;
125 default:
126 *isOpaque = false;
127 return SkBitmap::kNo_Config;
128 }
129}
reed@google.comac10a2d2010-12-22 21:39:39 +0000130
reed@google.comaf951c92011-06-16 19:10:39 +0000131static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000132 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000133
134 bool isOpaque;
135 SkBitmap bitmap;
136 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
137 renderTarget->width(), renderTarget->height());
138 bitmap.setIsOpaque(isOpaque);
139 return bitmap;
140}
141
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000142SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
143: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
144 this->initFromRenderTarget(context, texture->asRenderTarget());
145}
146
reed@google.comaf951c92011-06-16 19:10:39 +0000147SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
148: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000149 this->initFromRenderTarget(context, renderTarget);
150}
151
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000152void SkGpuDevice::initFromRenderTarget(GrContext* context,
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000153 GrRenderTarget* renderTarget) {
reed@google.comaf951c92011-06-16 19:10:39 +0000154 fNeedPrepareRenderTarget = false;
155 fDrawProcs = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000156
reed@google.comaf951c92011-06-16 19:10:39 +0000157 fContext = context;
158 fContext->ref();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000159
reed@google.comaf951c92011-06-16 19:10:39 +0000160 fTexture = NULL;
161 fRenderTarget = NULL;
162 fNeedClear = false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000163
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000164 GrAssert(NULL != renderTarget);
165 fRenderTarget = renderTarget;
166 fRenderTarget->ref();
167 // if this RT is also a texture, hold a ref on it
168 fTexture = fRenderTarget->asTexture();
169 SkSafeRef(fTexture);
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000170
171 // Create a pixel ref for the underlying SkBitmap. We prefer a texture pixel
172 // ref to a render target pixel reft. The pixel ref may get ref'ed outside
173 // the device via accessBitmap. This external ref may outlive the device.
174 // Since textures own their render targets (but not vice-versa) we
175 // are ensuring that both objects will live as long as the pixel ref.
176 SkPixelRef* pr;
177 if (fTexture) {
178 pr = new SkGrTexturePixelRef(fTexture);
179 } else {
180 pr = new SkGrRenderTargetPixelRef(fRenderTarget);
181 }
reed@google.comaf951c92011-06-16 19:10:39 +0000182 this->setPixelRef(pr, 0)->unref();
183}
184
185SkGpuDevice::SkGpuDevice(GrContext* context, SkBitmap::Config config, int width,
bsalomon@google.come97f0852011-06-17 13:10:25 +0000186 int height, Usage usage)
reed@google.comaf951c92011-06-16 19:10:39 +0000187: SkDevice(config, width, height, false /*isOpaque*/) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000188 fNeedPrepareRenderTarget = false;
189 fDrawProcs = NULL;
190
reed@google.com7b201d22011-01-11 18:59:23 +0000191 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000192 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000193
reed@google.comac10a2d2010-12-22 21:39:39 +0000194 fTexture = NULL;
195 fRenderTarget = NULL;
196 fNeedClear = false;
197
reed@google.comaf951c92011-06-16 19:10:39 +0000198 if (config != SkBitmap::kRGB_565_Config) {
199 config = SkBitmap::kARGB_8888_Config;
200 }
201 SkBitmap bm;
202 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000203
204#if CACHE_LAYER_TEXTURES
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000205 TexType type = (kSaveLayer_Usage == usage) ?
bsalomon@google.come97f0852011-06-17 13:10:25 +0000206 kSaveLayerDeviceRenderTarget_TexType :
207 kDeviceRenderTarget_TexType;
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000208 fCache = this->lockCachedTexture(bm, NULL, type);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000209 fTexture = fCache.texture();
210 if (fTexture) {
reed@google.comaf951c92011-06-16 19:10:39 +0000211 SkASSERT(NULL != fTexture->asRenderTarget());
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000212 // hold a ref directly on fTexture (even though fCache has one) to match
213 // other constructor paths. Simplifies cleanup.
214 fTexture->ref();
reed@google.comaf951c92011-06-16 19:10:39 +0000215 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000216#else
reed@google.comaf951c92011-06-16 19:10:39 +0000217 const GrTextureDesc desc = {
218 kRenderTarget_GrTextureFlagBit,
reed@google.comaf951c92011-06-16 19:10:39 +0000219 width,
220 height,
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000221 SkGr::Bitmap2PixelConfig(bm),
222 {0} // samples
reed@google.comaf951c92011-06-16 19:10:39 +0000223 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000224
reed@google.comaf951c92011-06-16 19:10:39 +0000225 fTexture = fContext->createUncachedTexture(desc, NULL, 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000226#endif
reed@google.comaf951c92011-06-16 19:10:39 +0000227 if (NULL != fTexture) {
228 fRenderTarget = fTexture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000229 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000230
reed@google.comaf951c92011-06-16 19:10:39 +0000231 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000232
reed@google.comaf951c92011-06-16 19:10:39 +0000233 // we defer the actual clear until our gainFocus()
234 fNeedClear = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000235
reed@google.comaf951c92011-06-16 19:10:39 +0000236 // wrap the bitmap with a pixelref to expose our texture
237 SkGrTexturePixelRef* pr = new SkGrTexturePixelRef(fTexture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000238 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000239 } else {
240 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
241 width, height);
242 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000243 }
244}
245
246SkGpuDevice::~SkGpuDevice() {
247 if (fDrawProcs) {
248 delete fDrawProcs;
249 }
250
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000251 SkSafeUnref(fTexture);
252 SkSafeUnref(fRenderTarget);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000253 if (fCache.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000254 GrAssert(NULL != fTexture);
255 GrAssert(fRenderTarget == fTexture->asRenderTarget());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000256 fContext->unlockTexture(fCache);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000257 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000258 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000259}
260
reed@google.comac10a2d2010-12-22 21:39:39 +0000261///////////////////////////////////////////////////////////////////////////////
262
263void SkGpuDevice::makeRenderTargetCurrent() {
264 fContext->setRenderTarget(fRenderTarget);
265 fContext->flush(true);
266 fNeedPrepareRenderTarget = true;
267}
268
269///////////////////////////////////////////////////////////////////////////////
270
bsalomon@google.comc4364992011-11-07 15:54:49 +0000271namespace {
272GrPixelConfig config8888_to_gr_config(SkCanvas::Config8888 config8888) {
273 switch (config8888) {
274 case SkCanvas::kNative_Premul_Config8888:
275 return kSkia8888_PM_GrPixelConfig;
276 case SkCanvas::kNative_Unpremul_Config8888:
277 return kSkia8888_UPM_GrPixelConfig;
278 case SkCanvas::kBGRA_Premul_Config8888:
279 return kBGRA_8888_PM_GrPixelConfig;
280 case SkCanvas::kBGRA_Unpremul_Config8888:
281 return kBGRA_8888_UPM_GrPixelConfig;
282 case SkCanvas::kRGBA_Premul_Config8888:
283 return kRGBA_8888_PM_GrPixelConfig;
284 case SkCanvas::kRGBA_Unpremul_Config8888:
285 return kRGBA_8888_UPM_GrPixelConfig;
286 default:
287 GrCrash("Unexpected Config8888.");
288 return kSkia8888_PM_GrPixelConfig;
289 }
290}
291}
292
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000293bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
294 int x, int y,
295 SkCanvas::Config8888 config8888) {
bsalomon@google.com910267d2011-11-02 20:06:25 +0000296 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
297 SkASSERT(!bitmap.isNull());
298 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000299
bsalomon@google.com910267d2011-11-02 20:06:25 +0000300 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000301 GrPixelConfig config;
302 config = config8888_to_gr_config(config8888);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000303 return fContext->readRenderTargetPixels(fRenderTarget,
304 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000305 bitmap.width(),
306 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000307 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000308 bitmap.getPixels(),
309 bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000310}
311
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000312void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
313 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000314 SkAutoLockPixels alp(bitmap);
315 if (!bitmap.readyToDraw()) {
316 return;
317 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000318
319 GrPixelConfig config;
320 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
321 config = config8888_to_gr_config(config8888);
322 } else {
323 config= SkGr::BitmapConfig2PixelConfig(bitmap.config(),
324 bitmap.isOpaque());
325 }
326
bsalomon@google.com6f379512011-11-16 20:36:03 +0000327 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
328 config, bitmap.getPixels(), bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000329}
330
331///////////////////////////////////////////////////////////////////////////////
332
333static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000334 const SkClipStack& clipStack,
reed@google.com6f8f2922011-03-04 22:27:10 +0000335 const SkRegion& clipRegion,
336 const SkIPoint& origin) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000337 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000338
339 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000340 iter.reset(clipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000341 const SkIRect& skBounds = clipRegion.getBounds();
342 GrRect bounds;
343 bounds.setLTRB(GrIntToScalar(skBounds.fLeft),
344 GrIntToScalar(skBounds.fTop),
345 GrIntToScalar(skBounds.fRight),
346 GrIntToScalar(skBounds.fBottom));
reed@google.com6f8f2922011-03-04 22:27:10 +0000347 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
348 &bounds);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000349 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000350}
351
352// call this ever each draw call, to ensure that the context reflects our state,
353// and not the state from some other canvas/device
354void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
355 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000356 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000357
358 fContext->setRenderTarget(fRenderTarget);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000359 SkASSERT(draw.fClipStack);
360 convert_matrixclip(fContext, *draw.fMatrix,
reed@google.com6f8f2922011-03-04 22:27:10 +0000361 *draw.fClipStack, *draw.fClip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000362 fNeedPrepareRenderTarget = false;
363 }
364}
365
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000366void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
367 const SkClipStack& clipStack) {
368 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
369 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000370 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000371}
372
373void SkGpuDevice::gainFocus(SkCanvas* canvas, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000374 const SkRegion& clip, const SkClipStack& clipStack) {
375
reed@google.comac10a2d2010-12-22 21:39:39 +0000376 fContext->setRenderTarget(fRenderTarget);
377
bsalomon@google.comd302f142011-03-03 13:54:13 +0000378 this->INHERITED::gainFocus(canvas, matrix, clip, clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000379
reed@google.com6f8f2922011-03-04 22:27:10 +0000380 convert_matrixclip(fContext, matrix, clipStack, clip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000381
382 if (fNeedClear) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000383 fContext->clear(NULL, 0x0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000384 fNeedClear = false;
385 }
386}
387
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000388SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
389 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000390}
391
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000392bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000393 if (NULL != fTexture) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000394 paint->setTexture(kBitmapTextureIdx, fTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000395 return true;
396 }
397 return false;
398}
399
400///////////////////////////////////////////////////////////////////////////////
401
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000402SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
403SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
404SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
405SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
406SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
407 shader_type_mismatch);
408SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 4, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000409
bsalomon@google.com5782d712011-01-21 21:03:59 +0000410static const GrSamplerState::SampleMode sk_bmp_type_to_sample_mode[] = {
411 (GrSamplerState::SampleMode) -1, // kNone_BitmapType
412 GrSamplerState::kNormal_SampleMode, // kDefault_BitmapType
413 GrSamplerState::kRadial_SampleMode, // kRadial_BitmapType
414 GrSamplerState::kSweep_SampleMode, // kSweep_BitmapType
415 GrSamplerState::kRadial2_SampleMode, // kTwoPointRadial_BitmapType
416};
417
418bool SkGpuDevice::skPaint2GrPaintNoShader(const SkPaint& skPaint,
419 bool justAlpha,
Scroggod757df22011-05-16 13:11:16 +0000420 GrPaint* grPaint,
421 bool constantColor) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000422
423 grPaint->fDither = skPaint.isDither();
424 grPaint->fAntiAlias = skPaint.isAntiAlias();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000425 grPaint->fCoverage = 0xFF;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000426
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000427 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
428 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000429
430 SkXfermode* mode = skPaint.getXfermode();
431 if (mode) {
432 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000433 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000434#if 0
435 return false;
436#endif
437 }
438 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000439 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
440 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
441
bsalomon@google.com5782d712011-01-21 21:03:59 +0000442 if (justAlpha) {
443 uint8_t alpha = skPaint.getAlpha();
444 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000445 // justAlpha is currently set to true only if there is a texture,
446 // so constantColor should not also be true.
447 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000448 } else {
449 grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000450 grPaint->setTexture(kShaderTextureIdx, NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000451 }
Scroggo97c88c22011-05-11 14:05:25 +0000452 SkColorFilter* colorFilter = skPaint.getColorFilter();
453 SkColor color;
454 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000455 SkScalar matrix[20];
Scroggo97c88c22011-05-11 14:05:25 +0000456 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000457 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000458 if (!constantColor) {
459 grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color);
460 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000461 } else {
462 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
463 grPaint->fColor = SkGr::SkColor2GrColor(filtered);
senorblanco@chromium.orgb3c20fa2012-01-03 21:20:19 +0000464 grPaint->resetColorFilter();
Scroggod757df22011-05-16 13:11:16 +0000465 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000466 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
467 grPaint->fColorMatrixEnabled = true;
468 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
469 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
470 } else {
471 grPaint->resetColorFilter();
Scroggo97c88c22011-05-11 14:05:25 +0000472 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000473 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000474}
475
bsalomon@google.com5782d712011-01-21 21:03:59 +0000476bool SkGpuDevice::skPaint2GrPaintShader(const SkPaint& skPaint,
477 SkAutoCachedTexture* act,
478 const SkMatrix& ctm,
Scroggod757df22011-05-16 13:11:16 +0000479 GrPaint* grPaint,
480 bool constantColor) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000481
bsalomon@google.com5782d712011-01-21 21:03:59 +0000482 SkASSERT(NULL != act);
reed@google.comac10a2d2010-12-22 21:39:39 +0000483
bsalomon@google.com5782d712011-01-21 21:03:59 +0000484 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000485 if (NULL == shader) {
Scroggod757df22011-05-16 13:11:16 +0000486 return this->skPaint2GrPaintNoShader(skPaint,
487 false,
488 grPaint,
489 constantColor);
490 } else if (!this->skPaint2GrPaintNoShader(skPaint, true, grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000491 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000492 }
493
reed@google.comac10a2d2010-12-22 21:39:39 +0000494 SkBitmap bitmap;
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000495 SkMatrix* matrix = grPaint->textureSampler(kShaderTextureIdx)->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000496 SkShader::TileMode tileModes[2];
497 SkScalar twoPointParams[3];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000498 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000499 tileModes, twoPointParams);
500
bsalomon@google.com5782d712011-01-21 21:03:59 +0000501 GrSamplerState::SampleMode sampleMode = sk_bmp_type_to_sample_mode[bmptype];
502 if (-1 == sampleMode) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000503 SkShader::GradientInfo info;
504 SkColor color;
505
506 info.fColors = &color;
507 info.fColorOffsets = NULL;
508 info.fColorCount = 1;
509 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
510 SkPaint copy(skPaint);
511 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000512 // modulate the paint alpha by the shader's solid color alpha
513 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
514 copy.setColor(SkColorSetA(color, newA));
reed@google.com2be9e8b2011-07-06 21:18:09 +0000515 return this->skPaint2GrPaintNoShader(copy,
516 false,
517 grPaint,
518 constantColor);
519 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000520 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000521 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000522 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000523 sampler->setSampleMode(sampleMode);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000524 if (skPaint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000525 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000526 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000527 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000528 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000529 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
530 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000531 if (GrSamplerState::kRadial2_SampleMode == sampleMode) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000532 sampler->setRadial2Params(twoPointParams[0],
533 twoPointParams[1],
534 twoPointParams[2] < 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000535 }
536
bsalomon@google.com1fadb202011-12-12 16:10:08 +0000537 GrTexture* texture = act->set(this, bitmap, sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000538 if (NULL == texture) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000539 SkDebugf("Couldn't convert bitmap to texture.\n");
540 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000541 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000542 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000543
544 // since our texture coords will be in local space, we wack the texture
545 // matrix to map them back into 0...1 before we load it
546 SkMatrix localM;
547 if (shader->getLocalMatrix(&localM)) {
548 SkMatrix inverse;
549 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000550 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000551 }
552 }
553 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000554 GrScalar sx = GrFixedToScalar(GR_Fixed1 / bitmap.width());
555 GrScalar sy = GrFixedToScalar(GR_Fixed1 / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000556 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000557 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000558 GrScalar s = GrFixedToScalar(GR_Fixed1 / bitmap.width());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000559 matrix->postScale(s, s);
reed@google.comac10a2d2010-12-22 21:39:39 +0000560 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000561
562 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000563}
564
565///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000566
bsalomon@google.com398109c2011-04-14 18:40:27 +0000567void SkGpuDevice::clear(SkColor color) {
bsalomon@google.com31a58402011-04-27 21:00:02 +0000568 fContext->clear(NULL, color);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000569}
570
reed@google.comac10a2d2010-12-22 21:39:39 +0000571void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
572 CHECK_SHOULD_DRAW(draw);
573
bsalomon@google.com5782d712011-01-21 21:03:59 +0000574 GrPaint grPaint;
575 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000576 if (!this->skPaint2GrPaintShader(paint,
577 &act,
578 *draw.fMatrix,
579 &grPaint,
580 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000581 return;
582 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000583
584 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000585}
586
587// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000588static const GrPrimitiveType gPointMode2PrimtiveType[] = {
589 kPoints_PrimitiveType,
590 kLines_PrimitiveType,
591 kLineStrip_PrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000592};
593
594void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000595 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000596 CHECK_SHOULD_DRAW(draw);
597
598 SkScalar width = paint.getStrokeWidth();
599 if (width < 0) {
600 return;
601 }
602
603 // we only handle hairlines here, else we let the SkDraw call our drawPath()
604 if (width > 0) {
605 draw.drawPoints(mode, count, pts, paint, true);
606 return;
607 }
608
bsalomon@google.com5782d712011-01-21 21:03:59 +0000609 GrPaint grPaint;
610 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000611 if (!this->skPaint2GrPaintShader(paint,
612 &act,
613 *draw.fMatrix,
614 &grPaint,
615 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000616 return;
617 }
618
bsalomon@google.com5782d712011-01-21 21:03:59 +0000619 fContext->drawVertices(grPaint,
620 gPointMode2PrimtiveType[mode],
621 count,
622 (GrPoint*)pts,
623 NULL,
624 NULL,
625 NULL,
626 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000627}
628
reed@google.comc9aa5872011-04-05 21:05:37 +0000629///////////////////////////////////////////////////////////////////////////////
630
reed@google.comac10a2d2010-12-22 21:39:39 +0000631void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
632 const SkPaint& paint) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000633 CHECK_SHOULD_DRAW(draw);
634
bungeman@google.com79bd8772011-07-18 15:34:08 +0000635 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000636 SkScalar width = paint.getStrokeWidth();
637
638 /*
639 We have special code for hairline strokes, miter-strokes, and fills.
640 Anything else we just call our path code.
641 */
642 bool usePath = doStroke && width > 0 &&
643 paint.getStrokeJoin() != SkPaint::kMiter_Join;
644 // another reason we might need to call drawPath...
645 if (paint.getMaskFilter()) {
646 usePath = true;
647 }
reed@google.com67db6642011-05-26 11:46:35 +0000648 // until we aa rotated rects...
649 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
650 usePath = true;
651 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000652 // small miter limit means right angles show bevel...
653 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
654 paint.getStrokeMiter() < SK_ScalarSqrt2)
655 {
656 usePath = true;
657 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000658 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000659 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
660 usePath = true;
661 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000662
663 if (usePath) {
664 SkPath path;
665 path.addRect(rect);
666 this->drawPath(draw, path, paint, NULL, true);
667 return;
668 }
669
670 GrPaint grPaint;
671 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +0000672 if (!this->skPaint2GrPaintShader(paint,
673 &act,
674 *draw.fMatrix,
675 &grPaint,
676 true)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000677 return;
678 }
reed@google.com20efde72011-05-09 17:00:02 +0000679 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000680}
681
reed@google.com69302852011-02-16 18:08:07 +0000682#include "SkMaskFilter.h"
683#include "SkBounder.h"
684
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000685static GrPathFill skToGrFillType(SkPath::FillType fillType) {
686 switch (fillType) {
687 case SkPath::kWinding_FillType:
688 return kWinding_PathFill;
689 case SkPath::kEvenOdd_FillType:
690 return kEvenOdd_PathFill;
691 case SkPath::kInverseWinding_FillType:
692 return kInverseWinding_PathFill;
693 case SkPath::kInverseEvenOdd_FillType:
694 return kInverseEvenOdd_PathFill;
695 default:
696 SkDebugf("Unsupported path fill type\n");
697 return kHairLine_PathFill;
698 }
699}
700
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000701static GrTexture* applyMorphology(GrContext* context, GrTexture* texture,
702 const GrRect& srcRect,
703 GrTexture* temp1, GrTexture* temp2,
704 GrSamplerState::Filter filter,
705 SkISize radius) {
706 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
707 GrAutoMatrix avm(context, GrMatrix::I());
708 GrClip oldClip = context->getClip();
709 context->setClip(GrRect::MakeWH(texture->width(), texture->height()));
710 if (radius.fWidth > 0) {
711 context->setRenderTarget(temp1->asRenderTarget());
712 context->applyMorphology(texture, srcRect, radius.fWidth, filter,
713 GrSamplerState::kX_FilterDirection);
714 SkIRect clearRect = SkIRect::MakeXYWH(
715 srcRect.fLeft, srcRect.fBottom,
716 srcRect.width(), radius.fHeight);
717 context->clear(&clearRect, 0x0);
718 texture = temp1;
719 }
720 if (radius.fHeight > 0) {
721 context->setRenderTarget(temp2->asRenderTarget());
722 context->applyMorphology(texture, srcRect, radius.fHeight, filter,
723 GrSamplerState::kY_FilterDirection);
724 texture = temp2;
725 }
726 context->setRenderTarget(oldRenderTarget);
727 context->setClip(oldClip);
728 return texture;
729}
730
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000731static void buildKernel(float sigma, float* kernel, int kernelWidth) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000732 int halfWidth = (kernelWidth - 1) / 2;
733 float sum = 0.0f;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000734 float denom = 1.0f / (2.0f * sigma * sigma);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000735 for (int i = 0; i < kernelWidth; ++i) {
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000736 float x = static_cast<float>(i - halfWidth);
737 // Note that the constant term (1/(sqrt(2*pi*sigma^2)) of the Gaussian
738 // is dropped here, since we renormalize the kernel below.
739 kernel[i] = sk_float_exp(- x * x * denom);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000740 sum += kernel[i];
741 }
742 // Normalize the kernel
743 float scale = 1.0f / sum;
744 for (int i = 0; i < kernelWidth; ++i)
745 kernel[i] *= scale;
746}
747
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000748static void scaleRect(SkRect* rect, float xScale, float yScale) {
749 rect->fLeft *= xScale;
750 rect->fTop *= yScale;
751 rect->fRight *= xScale;
752 rect->fBottom *= yScale;
753}
754
755static float adjustSigma(float sigma, int *scaleFactor, int *halfWidth,
756 int *kernelWidth) {
757 *scaleFactor = 1;
758 while (sigma > MAX_BLUR_SIGMA) {
759 *scaleFactor *= 2;
760 sigma *= 0.5f;
761 }
762 *halfWidth = static_cast<int>(ceilf(sigma * 3.0f));
763 *kernelWidth = *halfWidth * 2 + 1;
764 return sigma;
765}
766
767// Apply a Gaussian blur to srcTexture by sigmaX and sigmaY, within the given
768// rect.
769// temp1 and temp2 are used for allocation of intermediate textures.
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000770// If temp2 is non-NULL, srcTexture will be untouched, and the return
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000771// value will be either temp1 or temp2.
772// If temp2 is NULL, srcTexture will be overwritten with intermediate
773// results, and the return value will either be temp1 or srcTexture.
774static GrTexture* gaussianBlur(GrContext* context, GrTexture* srcTexture,
775 GrAutoScratchTexture* temp1,
776 GrAutoScratchTexture* temp2,
777 const SkRect& rect,
778 float sigmaX, float sigmaY) {
779
780 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
781 GrClip oldClip = context->getClip();
782 GrTexture* origTexture = srcTexture;
783 GrAutoMatrix avm(context, GrMatrix::I());
784 SkIRect clearRect;
785 int scaleFactorX, halfWidthX, kernelWidthX;
786 int scaleFactorY, halfWidthY, kernelWidthY;
787 sigmaX = adjustSigma(sigmaX, &scaleFactorX, &halfWidthX, &kernelWidthX);
788 sigmaY = adjustSigma(sigmaY, &scaleFactorY, &halfWidthY, &kernelWidthY);
789
790 SkRect srcRect(rect);
791 scaleRect(&srcRect, 1.0f / scaleFactorX, 1.0f / scaleFactorY);
792 srcRect.roundOut();
793 scaleRect(&srcRect, scaleFactorX, scaleFactorY);
794 context->setClip(srcRect);
795
796 const GrTextureDesc desc = {
797 kRenderTarget_GrTextureFlagBit | kNoStencil_GrTextureFlagBit,
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000798 srcRect.width(),
799 srcRect.height(),
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000800 kRGBA_8888_GrPixelConfig,
801 {0} // samples
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000802 };
803
804 temp1->set(context, desc);
805 if (temp2) temp2->set(context, desc);
806
807 GrTexture* dstTexture = temp1->texture();
808 GrPaint paint;
809 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000810 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000811
812 for (int i = 1; i < scaleFactorX || i < scaleFactorY; i *= 2) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000813 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
814 srcTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000815 context->setRenderTarget(dstTexture->asRenderTarget());
816 SkRect dstRect(srcRect);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000817 scaleRect(&dstRect, i < scaleFactorX ? 0.5f : 1.0f,
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000818 i < scaleFactorY ? 0.5f : 1.0f);
819 paint.setTexture(0, srcTexture);
820 context->drawRectToRect(paint, dstRect, srcRect);
821 srcRect = dstRect;
822 SkTSwap(srcTexture, dstTexture);
823 // If temp2 is non-NULL, don't render back to origTexture
824 if (temp2 && dstTexture == origTexture) dstTexture = temp2->texture();
825 }
826
827 if (sigmaX > 0.0f) {
828 SkAutoTMalloc<float> kernelStorageX(kernelWidthX);
829 float* kernelX = kernelStorageX.get();
830 buildKernel(sigmaX, kernelX, kernelWidthX);
831
832 if (scaleFactorX > 1) {
833 // Clear out a halfWidth to the right of the srcRect to prevent the
834 // X convolution from reading garbage.
835 clearRect = SkIRect::MakeXYWH(
836 srcRect.fRight, srcRect.fTop, halfWidthX, srcRect.height());
837 context->clear(&clearRect, 0x0);
838 }
839
840 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000841 context->convolve(srcTexture, srcRect, kernelX, kernelWidthX,
842 GrSamplerState::kX_FilterDirection);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000843 SkTSwap(srcTexture, dstTexture);
844 if (temp2 && dstTexture == origTexture) dstTexture = temp2->texture();
845 }
846
847 if (sigmaY > 0.0f) {
848 SkAutoTMalloc<float> kernelStorageY(kernelWidthY);
849 float* kernelY = kernelStorageY.get();
850 buildKernel(sigmaY, kernelY, kernelWidthY);
851
852 if (scaleFactorY > 1 || sigmaX > 0.0f) {
853 // Clear out a halfWidth below the srcRect to prevent the Y
854 // convolution from reading garbage.
855 clearRect = SkIRect::MakeXYWH(
856 srcRect.fLeft, srcRect.fBottom, srcRect.width(), halfWidthY);
857 context->clear(&clearRect, 0x0);
858 }
859
860 context->setRenderTarget(dstTexture->asRenderTarget());
senorblanco@chromium.org05054f12012-03-02 21:05:45 +0000861 context->convolve(srcTexture, srcRect, kernelY, kernelWidthY,
862 GrSamplerState::kY_FilterDirection);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000863 SkTSwap(srcTexture, dstTexture);
864 if (temp2 && dstTexture == origTexture) dstTexture = temp2->texture();
865 }
866
867 if (scaleFactorX > 1 || scaleFactorY > 1) {
868 // Clear one pixel to the right and below, to accommodate bilinear
869 // upsampling.
870 clearRect = SkIRect::MakeXYWH(
871 srcRect.fLeft, srcRect.fBottom, srcRect.width() + 1, 1);
872 context->clear(&clearRect, 0x0);
873 clearRect = SkIRect::MakeXYWH(
874 srcRect.fRight, srcRect.fTop, 1, srcRect.height());
875 context->clear(&clearRect, 0x0);
876 // FIXME: This should be mitchell, not bilinear.
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000877 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000878 paint.textureSampler(0)->matrix()->setIDiv(srcTexture->width(),
879 srcTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000880 context->setRenderTarget(dstTexture->asRenderTarget());
881 paint.setTexture(0, srcTexture);
882 SkRect dstRect(srcRect);
883 scaleRect(&dstRect, scaleFactorX, scaleFactorY);
884 context->drawRectToRect(paint, dstRect, srcRect);
885 srcRect = dstRect;
886 SkTSwap(srcTexture, dstTexture);
887 }
888 context->setRenderTarget(oldRenderTarget);
889 context->setClip(oldClip);
890 return srcTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000891}
892
893static bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
894 SkMaskFilter* filter, const SkMatrix& matrix,
895 const SkRegion& clip, SkBounder* bounder,
896 GrPaint* grp) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000897#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000898 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000899#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000900 SkMaskFilter::BlurInfo info;
901 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000902 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000903 return false;
904 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000905 SkScalar radius = info.fIgnoreTransform ? info.fRadius
906 : matrix.mapRadius(info.fRadius);
907 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000908 if (radius <= 0) {
909 return false;
910 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000911 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000912 float sigma3 = sigma * 3.0f;
913
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000914 SkRect srcRect = path.getBounds();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000915 SkRect clipRect;
916 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000917
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000918 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
919 srcRect.inset(-sigma3, -sigma3);
920 clipRect.inset(-sigma3, -sigma3);
921 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000922 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000923 SkIRect finalIRect;
924 finalRect.roundOut(&finalIRect);
925 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000926 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000927 }
928 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000929 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000930 }
931 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000932 srcRect.offset(offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000933 const GrTextureDesc desc = {
934 kRenderTarget_GrTextureFlagBit,
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000935 srcRect.width(),
936 srcRect.height(),
937 // We actually only need A8, but it often isn't supported as a
938 // render target
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000939 kRGBA_8888_PM_GrPixelConfig,
940 {0} // samples
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000941 };
942
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000943 GrAutoScratchTexture pathEntry(context, desc);
944 GrTexture* pathTexture = pathEntry.texture();
945 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000946 return false;
947 }
948 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000949 // Once this code moves into GrContext, this should be changed to use
950 // an AutoClipRestore.
951 GrClip oldClip = context->getClip();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000952 context->setRenderTarget(pathTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000953 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000954 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000955 GrPaint tempPaint;
956 tempPaint.reset();
957
958 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000959 tempPaint.fAntiAlias = grp->fAntiAlias;
960 if (tempPaint.fAntiAlias) {
961 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
962 // blend coeff of zero requires dual source blending support in order
963 // to properly blend partially covered pixels. This means the AA
964 // code path may not be taken. So we use a dst blend coeff of ISA. We
965 // could special case AA draws to a dst surface with known alpha=0 to
966 // use a zero dst coeff when dual source blending isn't available.
967 tempPaint.fSrcBlendCoeff = kOne_BlendCoeff;
968 tempPaint.fDstBlendCoeff = kISC_BlendCoeff;
969 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000970 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000971 context->drawPath(tempPaint, path, skToGrFillType(path.getFillType()), &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000972
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000973 GrAutoScratchTexture temp1, temp2;
974 // If we're doing a normal blur, we can clobber the pathTexture in the
975 // gaussianBlur. Otherwise, we need to save it for later compositing.
976 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
977 GrTexture* blurTexture = gaussianBlur(context, pathTexture,
978 &temp1, isNormalBlur ? NULL : &temp2,
979 srcRect, sigma, sigma);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000980
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000981 if (!isNormalBlur) {
982 GrPaint paint;
983 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000984 paint.textureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000985 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
986 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000987 // Blend pathTexture over blurTexture.
988 context->setRenderTarget(blurTexture->asRenderTarget());
989 paint.setTexture(0, pathTexture);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000990 if (SkMaskFilter::kInner_BlurType == blurType) {
991 // inner: dst = dst * src
992 paint.fSrcBlendCoeff = kDC_BlendCoeff;
993 paint.fDstBlendCoeff = kZero_BlendCoeff;
994 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
995 // solid: dst = src + dst - src * dst
996 // = (1 - dst) * src + 1 * dst
997 paint.fSrcBlendCoeff = kIDC_BlendCoeff;
998 paint.fDstBlendCoeff = kOne_BlendCoeff;
999 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
1000 // outer: dst = dst * (1 - src)
1001 // = 0 * src + (1 - src) * dst
1002 paint.fSrcBlendCoeff = kZero_BlendCoeff;
1003 paint.fDstBlendCoeff = kISC_BlendCoeff;
1004 }
1005 context->drawRect(paint, srcRect);
1006 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001007 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +00001008 context->setClip(oldClip);
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001009
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001010 if (grp->hasTextureOrMask()) {
1011 GrMatrix inverse;
1012 if (!matrix.invert(&inverse)) {
1013 return false;
1014 }
1015 grp->preConcatActiveSamplerMatrices(inverse);
1016 }
1017
1018 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
1019 // we assume the last mask index is available for use
1020 GrAssert(NULL == grp->getMask(MASK_IDX));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +00001021 grp->setMask(MASK_IDX, blurTexture);
bsalomon@google.com97912912011-12-06 16:30:36 +00001022 grp->maskSampler(MASK_IDX)->reset();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001023
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001024 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
1025 -finalRect.fTop);
1026 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
1027 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001028 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001029 return true;
1030}
1031
reed@google.com69302852011-02-16 18:08:07 +00001032static bool drawWithMaskFilter(GrContext* context, const SkPath& path,
1033 SkMaskFilter* filter, const SkMatrix& matrix,
1034 const SkRegion& clip, SkBounder* bounder,
1035 GrPaint* grp) {
1036 SkMask srcM, dstM;
1037
1038 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
1039 SkMask::kComputeBoundsAndRenderImage_CreateMode)) {
1040 return false;
1041 }
bungeman@google.com02f55842011-10-04 21:25:00 +00001042 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +00001043
1044 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
1045 return false;
1046 }
1047 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +00001048 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +00001049
1050 if (clip.quickReject(dstM.fBounds)) {
1051 return false;
1052 }
1053 if (bounder && !bounder->doIRect(dstM.fBounds)) {
1054 return false;
1055 }
1056
1057 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
1058 // the current clip (and identity matrix) and grpaint settings
1059
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001060 // used to compute inverse view, if necessary
1061 GrMatrix ivm = context->getMatrix();
1062
reed@google.com0c219b62011-02-16 21:31:18 +00001063 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +00001064
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001065 const GrTextureDesc desc = {
1066 kNone_GrTextureFlags,
reed@google.com69302852011-02-16 18:08:07 +00001067 dstM.fBounds.width(),
1068 dstM.fBounds.height(),
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001069 kAlpha_8_GrPixelConfig,
1070 {0}, // samples
reed@google.com69302852011-02-16 18:08:07 +00001071 };
1072
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001073 GrAutoScratchTexture ast(context, desc);
1074 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001075
reed@google.com69302852011-02-16 18:08:07 +00001076 if (NULL == texture) {
1077 return false;
1078 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001079 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001080 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +00001081
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001082 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
1083 grp->preConcatActiveSamplerMatrices(ivm);
1084 }
1085
1086 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
1087 // we assume the last mask index is available for use
1088 GrAssert(NULL == grp->getMask(MASK_IDX));
1089 grp->setMask(MASK_IDX, texture);
bsalomon@google.com97912912011-12-06 16:30:36 +00001090 grp->maskSampler(MASK_IDX)->reset();
reed@google.com69302852011-02-16 18:08:07 +00001091
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001092 GrRect d;
1093 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +00001094 GrIntToScalar(dstM.fBounds.fTop),
1095 GrIntToScalar(dstM.fBounds.fRight),
1096 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001097
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001098 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
1099 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
1100 -dstM.fBounds.fTop*SK_Scalar1);
1101 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001102 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +00001103 return true;
1104}
reed@google.com69302852011-02-16 18:08:07 +00001105
reed@google.com0c219b62011-02-16 21:31:18 +00001106void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001107 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +00001108 bool pathIsMutable) {
1109 CHECK_SHOULD_DRAW(draw);
1110
reed@google.comfe626382011-09-21 13:50:35 +00001111 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001112
bsalomon@google.com5782d712011-01-21 21:03:59 +00001113 GrPaint grPaint;
1114 SkAutoCachedTexture act;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001115 if (!this->skPaint2GrPaintShader(paint,
Scroggod757df22011-05-16 13:11:16 +00001116 &act,
1117 *draw.fMatrix,
1118 &grPaint,
1119 true)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001120 return;
1121 }
1122
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +00001123 // can we cheat, and threat a thin stroke as a hairline w/ coverage
1124 // if we can, we draw lots faster (raster device does this same test)
1125 SkScalar hairlineCoverage;
1126 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
1127 doFill = false;
1128 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
1129 grPaint.fCoverage);
1130 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001131
reed@google.comfe626382011-09-21 13:50:35 +00001132 // If we have a prematrix, apply it to the path, optimizing for the case
1133 // where the original path can in fact be modified in place (even though
1134 // its parameter type is const).
1135 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1136 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001137
1138 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001139 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001140
reed@google.come3445642011-02-16 23:20:39 +00001141 if (!pathIsMutable) {
1142 result = &tmpPath;
1143 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001144 }
reed@google.come3445642011-02-16 23:20:39 +00001145 // should I push prePathMatrix on our MV stack temporarily, instead
1146 // of applying it here? See SkDraw.cpp
1147 pathPtr->transform(*prePathMatrix, result);
1148 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001149 }
reed@google.com0c219b62011-02-16 21:31:18 +00001150 // at this point we're done with prePathMatrix
1151 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001152
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001153 if (paint.getPathEffect() ||
1154 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001155 // it is safe to use tmpPath here, even if we already used it for the
1156 // prepathmatrix, since getFillPath can take the same object for its
1157 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001158 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001159 pathPtr = &tmpPath;
1160 }
1161
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001162 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001163 // avoid possibly allocating a new path in transform if we can
1164 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1165
1166 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001167 pathPtr->transform(*draw.fMatrix, devPathPtr);
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001168 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001169 *draw.fMatrix, *draw.fClip, draw.fBounder,
1170 &grPaint)) {
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001171 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001172 *draw.fMatrix, *draw.fClip, draw.fBounder,
1173 &grPaint);
1174 }
reed@google.com69302852011-02-16 18:08:07 +00001175 return;
1176 }
reed@google.com69302852011-02-16 18:08:07 +00001177
bsalomon@google.comffca4002011-02-22 20:34:01 +00001178 GrPathFill fill = kHairLine_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001179
reed@google.com0c219b62011-02-16 21:31:18 +00001180 if (doFill) {
1181 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001182 case SkPath::kWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001183 fill = kWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001184 break;
1185 case SkPath::kEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001186 fill = kEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001187 break;
1188 case SkPath::kInverseWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001189 fill = kInverseWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001190 break;
1191 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001192 fill = kInverseEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001193 break;
1194 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001195 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001196 return;
1197 }
1198 }
1199
reed@google.com07f3ee12011-05-16 17:21:57 +00001200 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001201}
1202
bsalomon@google.comfb309512011-11-30 14:13:48 +00001203namespace {
1204
1205inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1206 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1207 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1208 return tilesX * tilesY;
1209}
1210
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001211inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001212 const SkIRect* srcRectPtr,
1213 int maxTextureSize) {
1214 static const int kSmallTileSize = 1 << 10;
1215 if (maxTextureSize <= kSmallTileSize) {
1216 return maxTextureSize;
1217 }
1218
1219 size_t maxTexTotalTileSize;
1220 size_t smallTotalTileSize;
1221
1222 if (NULL == srcRectPtr) {
1223 int w = bitmap.width();
1224 int h = bitmap.height();
1225 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1226 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1227 } else {
1228 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1229 srcRectPtr->fTop,
1230 srcRectPtr->fRight,
1231 srcRectPtr->fBottom,
1232 maxTextureSize);
1233 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1234 srcRectPtr->fTop,
1235 srcRectPtr->fRight,
1236 srcRectPtr->fBottom,
1237 kSmallTileSize);
1238 }
1239 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1240 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1241
1242 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1243 return kSmallTileSize;
1244 } else {
1245 return maxTextureSize;
1246 }
1247}
1248}
1249
1250bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1251 const GrSamplerState& sampler,
1252 const SkIRect* srcRectPtr,
1253 int* tileSize) const {
1254 SkASSERT(NULL != tileSize);
1255
1256 // if bitmap is explictly texture backed then just use the texture
1257 if (NULL != bitmap.getTexture()) {
1258 return false;
1259 }
1260 // if it's larger than the max texture size, then we have no choice but
1261 // tiling
1262 const int maxTextureSize = fContext->getMaxTextureSize();
1263 if (bitmap.width() > maxTextureSize ||
1264 bitmap.height() > maxTextureSize) {
1265 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1266 return true;
1267 }
1268 // if we are going to have to draw the whole thing, then don't tile
1269 if (NULL == srcRectPtr) {
1270 return false;
1271 }
1272 // if the entire texture is already in our cache then no reason to tile it
1273 if (this->isBitmapInTextureCache(bitmap, sampler)) {
1274 return false;
1275 }
1276
1277 // At this point we know we could do the draw by uploading the entire bitmap
1278 // as a texture. However, if the texture would be large compared to the
1279 // cache size and we don't require most of it for this draw then tile to
1280 // reduce the amount of upload and cache spill.
1281
1282 // assumption here is that sw bitmap size is a good proxy for its size as
1283 // a texture
1284 size_t bmpSize = bitmap.getSize();
1285 size_t cacheSize;
1286 fContext->getTextureCacheLimits(NULL, &cacheSize);
1287 if (bmpSize < cacheSize / 2) {
1288 return false;
1289 }
1290
1291 SkFixed fracUsed =
1292 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1293 (srcRectPtr->height() << 16) / bitmap.height());
1294 if (fracUsed <= SK_FixedHalf) {
1295 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1296 return true;
1297 } else {
1298 return false;
1299 }
1300}
1301
reed@google.comac10a2d2010-12-22 21:39:39 +00001302void SkGpuDevice::drawBitmap(const SkDraw& draw,
1303 const SkBitmap& bitmap,
1304 const SkIRect* srcRectPtr,
1305 const SkMatrix& m,
1306 const SkPaint& paint) {
1307 CHECK_SHOULD_DRAW(draw);
1308
1309 SkIRect srcRect;
1310 if (NULL == srcRectPtr) {
1311 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1312 } else {
1313 srcRect = *srcRectPtr;
1314 }
1315
junov@google.comd935cfb2011-06-27 20:48:23 +00001316 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001317 // Convert the bitmap to a shader so that the rect can be drawn
1318 // through drawRect, which supports mask filters.
1319 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001320 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001321 if (srcRectPtr) {
1322 if (!bitmap.extractSubset(&tmp, srcRect)) {
1323 return; // extraction failed
1324 }
1325 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001326 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001327 }
1328 SkPaint paintWithTexture(paint);
1329 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1330 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001331 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001332 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001333
junov@google.com1d329782011-07-28 20:10:09 +00001334 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001335 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001336 // also affect the behavior of the mask filter.
1337 SkMatrix drawMatrix;
1338 drawMatrix.setConcat(*draw.fMatrix, m);
1339 SkDraw transformedDraw(draw);
1340 transformedDraw.fMatrix = &drawMatrix;
1341
1342 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1343
junov@google.comd935cfb2011-06-27 20:48:23 +00001344 return;
1345 }
1346
bsalomon@google.com5782d712011-01-21 21:03:59 +00001347 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001348 if (!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001349 return;
1350 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001351 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001352 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001353 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001354 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001355 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001356 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001357
bsalomon@google.comfb309512011-11-30 14:13:48 +00001358 int tileSize;
1359 if (!this->shouldTileBitmap(bitmap, *sampler, srcRectPtr, &tileSize)) {
1360 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001361 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001362 return;
1363 }
1364
1365 // undo the translate done by SkCanvas
1366 int DX = SkMax32(0, srcRect.fLeft);
1367 int DY = SkMax32(0, srcRect.fTop);
1368 // compute clip bounds in local coordinates
1369 SkIRect clipRect;
1370 {
1371 SkRect r;
1372 r.set(draw.fClip->getBounds());
1373 SkMatrix matrix, inverse;
1374 matrix.setConcat(*draw.fMatrix, m);
1375 if (!matrix.invert(&inverse)) {
1376 return;
1377 }
1378 inverse.mapRect(&r);
1379 r.roundOut(&clipRect);
1380 // apply the canvas' translate to our local clip
1381 clipRect.offset(DX, DY);
1382 }
1383
bsalomon@google.comfb309512011-11-30 14:13:48 +00001384 int nx = bitmap.width() / tileSize;
1385 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001386 for (int x = 0; x <= nx; x++) {
1387 for (int y = 0; y <= ny; y++) {
1388 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001389 tileR.set(x * tileSize, y * tileSize,
1390 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001391 if (!SkIRect::Intersects(tileR, clipRect)) {
1392 continue;
1393 }
1394
1395 SkIRect srcR = tileR;
1396 if (!srcR.intersect(srcRect)) {
1397 continue;
1398 }
1399
1400 SkBitmap tmpB;
1401 if (bitmap.extractSubset(&tmpB, tileR)) {
1402 // now offset it to make it "local" to our tmp bitmap
1403 srcR.offset(-tileR.fLeft, -tileR.fTop);
1404
1405 SkMatrix tmpM(m);
1406 {
1407 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1408 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1409 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1410 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001411 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001412 }
1413 }
1414 }
1415}
1416
1417/*
1418 * This is called by drawBitmap(), which has to handle images that may be too
1419 * large to be represented by a single texture.
1420 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001421 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1422 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001423 */
1424void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1425 const SkBitmap& bitmap,
1426 const SkIRect& srcRect,
1427 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001428 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001429 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1430 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001431
reed@google.com9c49bc32011-07-07 13:42:37 +00001432 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001433 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001434 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001435 return;
1436 }
1437
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001438 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001439
1440 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1441 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
1442 sampler->setSampleMode(GrSamplerState::kNormal_SampleMode);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001443 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001444
1445 GrTexture* texture;
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001446 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001447 if (NULL == texture) {
1448 return;
1449 }
1450
bsalomon@google.com452943d2011-10-31 17:37:14 +00001451 grPaint->setTexture(kBitmapTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001452
reed@google.com20efde72011-05-09 17:00:02 +00001453 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1454 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001455 GrRect paintRect;
junov@google.com6acc9b32011-05-16 18:32:07 +00001456 paintRect.setLTRB(GrFixedToScalar((srcRect.fLeft << 16) / bitmap.width()),
1457 GrFixedToScalar((srcRect.fTop << 16) / bitmap.height()),
1458 GrFixedToScalar((srcRect.fRight << 16) / bitmap.width()),
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001459 GrFixedToScalar((srcRect.fBottom << 16) / bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001460
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001461 if (GrSamplerState::kNearest_Filter != sampler->getFilter() &&
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001462 (srcRect.width() < bitmap.width() ||
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001463 srcRect.height() < bitmap.height())) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001464 // If drawing a subrect of the bitmap and filtering is enabled,
1465 // use a constrained texture domain to avoid color bleeding
1466 GrScalar left, top, right, bottom;
1467 if (srcRect.width() > 1) {
1468 GrScalar border = GR_ScalarHalf / bitmap.width();
1469 left = paintRect.left() + border;
1470 right = paintRect.right() - border;
1471 } else {
1472 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1473 }
1474 if (srcRect.height() > 1) {
1475 GrScalar border = GR_ScalarHalf / bitmap.height();
1476 top = paintRect.top() + border;
1477 bottom = paintRect.bottom() - border;
1478 } else {
1479 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1480 }
1481 GrRect textureDomain;
1482 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001483 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001484 }
1485
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001486 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001487}
1488
1489void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1490 int left, int top, const SkPaint& paint) {
1491 CHECK_SHOULD_DRAW(draw);
1492
1493 SkAutoLockPixels alp(bitmap);
1494 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1495 return;
1496 }
1497
reed@google.com76dd2772012-01-05 21:15:07 +00001498 int w = bitmap.width();
1499 int h = bitmap.height();
1500
bsalomon@google.com5782d712011-01-21 21:03:59 +00001501 GrPaint grPaint;
Scroggod757df22011-05-16 13:11:16 +00001502 if(!this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001503 return;
1504 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001505
bsalomon@google.com5782d712011-01-21 21:03:59 +00001506 GrAutoMatrix avm(fContext, GrMatrix::I());
1507
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001508 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001509
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001510 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001511 sampler->reset();
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001512 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001513
reed@google.com76dd2772012-01-05 21:15:07 +00001514 SkImageFilter* imageFilter = paint.getImageFilter();
1515 SkSize blurSize;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001516 SkISize radius;
reed@google.com76dd2772012-01-05 21:15:07 +00001517 if (NULL != imageFilter && imageFilter->asABlur(&blurSize)) {
1518 GrAutoScratchTexture temp1, temp2;
1519 GrTexture* blurTexture = gaussianBlur(fContext,
1520 texture, &temp1, &temp2,
1521 GrRect::MakeWH(w, h),
1522 blurSize.width(),
1523 blurSize.height());
1524 texture = blurTexture;
1525 grPaint.setTexture(kBitmapTextureIdx, texture);
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001526 } else if (NULL != imageFilter && imageFilter->asADilate(&radius)) {
1527 const GrTextureDesc desc = {
1528 kRenderTarget_GrTextureFlagBit,
1529 w,
1530 h,
1531 kRGBA_8888_PM_GrPixelConfig,
1532 {0} // samples
1533 };
1534 GrAutoScratchTexture temp1(fContext, desc), temp2(fContext, desc);
1535 texture = applyMorphology(fContext, texture, GrRect::MakeWH(w, h),
1536 temp1.texture(), temp2.texture(),
1537 GrSamplerState::kDilate_Filter, radius);
1538 grPaint.setTexture(kBitmapTextureIdx, texture);
1539 } else if (NULL != imageFilter && imageFilter->asAnErode(&radius)) {
1540 const GrTextureDesc desc = {
1541 kRenderTarget_GrTextureFlagBit,
1542 w,
1543 h,
1544 kRGBA_8888_PM_GrPixelConfig,
1545 {0} // samples
1546 };
1547 GrAutoScratchTexture temp1(fContext, desc), temp2(fContext, desc);
1548 texture = applyMorphology(fContext, texture, GrRect::MakeWH(w, h),
1549 temp1.texture(), temp2.texture(),
1550 GrSamplerState::kErode_Filter, radius);
1551 grPaint.setTexture(kBitmapTextureIdx, texture);
reed@google.com76dd2772012-01-05 21:15:07 +00001552 } else {
1553 grPaint.setTexture(kBitmapTextureIdx, texture);
1554 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001555
bsalomon@google.com5782d712011-01-21 21:03:59 +00001556 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001557 GrRect::MakeXYWH(GrIntToScalar(left),
1558 GrIntToScalar(top),
1559 GrIntToScalar(w),
1560 GrIntToScalar(h)),
1561 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1562 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001563}
1564
1565void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* dev,
1566 int x, int y, const SkPaint& paint) {
1567 CHECK_SHOULD_DRAW(draw);
1568
bsalomon@google.com5782d712011-01-21 21:03:59 +00001569 GrPaint grPaint;
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001570 if (!((SkGpuDevice*)dev)->bindDeviceAsTexture(&grPaint) ||
Scroggod757df22011-05-16 13:11:16 +00001571 !this->skPaint2GrPaintNoShader(paint, true, &grPaint, false)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001572 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001573 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001574
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001575 GrTexture* devTex = grPaint.getTexture(0);
1576 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001577
1578 const SkBitmap& bm = dev->accessBitmap(false);
1579 int w = bm.width();
1580 int h = bm.height();
1581
1582 GrAutoMatrix avm(fContext, GrMatrix::I());
1583
bsalomon@google.com97912912011-12-06 16:30:36 +00001584 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001585
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001586 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1587 GrIntToScalar(y),
1588 GrIntToScalar(w),
1589 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001590
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001591 // The device being drawn may not fill up its texture (saveLayer uses
1592 // the approximate ).
1593 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1594 GR_Scalar1 * h / devTex->height());
1595
1596 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001597}
1598
reed@google.com76dd2772012-01-05 21:15:07 +00001599bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1600 const SkMatrix& ctm,
1601 SkBitmap* result, SkIPoint* offset) {
1602 SkSize size;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001603 SkISize radius;
1604 if (!filter->asABlur(&size) && !filter->asADilate(&radius) && !filter->asAnErode(&radius)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001605 return false;
1606 }
1607 SkDevice* dev = this->createCompatibleDevice(SkBitmap::kARGB_8888_Config,
1608 src.width(),
1609 src.height(),
1610 false);
1611 if (NULL == dev) {
1612 return false;
1613 }
1614 SkAutoUnref aur(dev);
1615 SkCanvas canvas(dev);
1616 SkPaint paint;
1617 paint.setImageFilter(filter);
1618 canvas.drawSprite(src, 0, 0, &paint);
1619 *result = dev->accessBitmap(false);
1620 return true;
1621}
1622
reed@google.comac10a2d2010-12-22 21:39:39 +00001623///////////////////////////////////////////////////////////////////////////////
1624
1625// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001626static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1627 kTriangles_PrimitiveType,
1628 kTriangleStrip_PrimitiveType,
1629 kTriangleFan_PrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001630};
1631
1632void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1633 int vertexCount, const SkPoint vertices[],
1634 const SkPoint texs[], const SkColor colors[],
1635 SkXfermode* xmode,
1636 const uint16_t indices[], int indexCount,
1637 const SkPaint& paint) {
1638 CHECK_SHOULD_DRAW(draw);
1639
bsalomon@google.com5782d712011-01-21 21:03:59 +00001640 GrPaint grPaint;
1641 SkAutoCachedTexture act;
1642 // we ignore the shader if texs is null.
1643 if (NULL == texs) {
Scroggod757df22011-05-16 13:11:16 +00001644 if (!this->skPaint2GrPaintNoShader(paint,
1645 false,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001646 &grPaint,
Scroggod757df22011-05-16 13:11:16 +00001647 NULL == colors)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001648 return;
1649 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001650 } else {
reed@google.com1a2e8d22011-01-21 22:08:29 +00001651 if (!this->skPaint2GrPaintShader(paint, &act,
1652 *draw.fMatrix,
Scroggod757df22011-05-16 13:11:16 +00001653 &grPaint,
1654 NULL == colors)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001655 return;
1656 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001657 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001658
1659 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001660 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001661 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1662#if 0
1663 return
1664#endif
1665 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001666 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001667
bsalomon@google.com498776a2011-08-16 19:20:44 +00001668 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1669 if (NULL != colors) {
1670 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001671 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001672 for (int i = 0; i < vertexCount; ++i) {
1673 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1674 }
1675 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001676 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001677 fContext->drawVertices(grPaint,
1678 gVertexMode2PrimitiveType[vmode],
1679 vertexCount,
1680 (GrPoint*) vertices,
1681 (GrPoint*) texs,
1682 colors,
1683 indices,
1684 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001685}
1686
1687///////////////////////////////////////////////////////////////////////////////
1688
1689static void GlyphCacheAuxProc(void* data) {
1690 delete (GrFontScaler*)data;
1691}
1692
1693static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1694 void* auxData;
1695 GrFontScaler* scaler = NULL;
1696 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1697 scaler = (GrFontScaler*)auxData;
1698 }
1699 if (NULL == scaler) {
1700 scaler = new SkGrFontScaler(cache);
1701 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1702 }
1703 return scaler;
1704}
1705
1706static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1707 SkFixed fx, SkFixed fy,
1708 const SkGlyph& glyph) {
1709 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1710
bungeman@google.com15865a72012-01-11 16:28:04 +00001711 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001712
1713 if (NULL == procs->fFontScaler) {
1714 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1715 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001716
bungeman@google.com15865a72012-01-11 16:28:04 +00001717 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1718 glyph.getSubXFixed(),
1719 glyph.getSubYFixed()),
1720 SkFixedFloorToFixed(fx),
1721 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001722 procs->fFontScaler);
1723}
1724
bsalomon@google.com5782d712011-01-21 21:03:59 +00001725SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001726
1727 // deferred allocation
1728 if (NULL == fDrawProcs) {
1729 fDrawProcs = new GrSkDrawProcs;
1730 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1731 fDrawProcs->fContext = fContext;
1732 }
1733
1734 // init our (and GL's) state
1735 fDrawProcs->fTextContext = context;
1736 fDrawProcs->fFontScaler = NULL;
1737 return fDrawProcs;
1738}
1739
1740void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1741 size_t byteLength, SkScalar x, SkScalar y,
1742 const SkPaint& paint) {
1743 CHECK_SHOULD_DRAW(draw);
1744
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001745 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001746 // this guy will just call our drawPath()
1747 draw.drawText((const char*)text, byteLength, x, y, paint);
1748 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001749 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001750
1751 GrPaint grPaint;
1752 SkAutoCachedTexture act;
1753
Scroggod757df22011-05-16 13:11:16 +00001754 if (!this->skPaint2GrPaintShader(paint,
1755 &act,
1756 *draw.fMatrix,
1757 &grPaint,
1758 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001759 return;
1760 }
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001761 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001762 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001763 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1764 }
1765}
1766
1767void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1768 size_t byteLength, const SkScalar pos[],
1769 SkScalar constY, int scalarsPerPos,
1770 const SkPaint& paint) {
1771 CHECK_SHOULD_DRAW(draw);
1772
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001773 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001774 // this guy will just call our drawPath()
1775 draw.drawPosText((const char*)text, byteLength, pos, constY,
1776 scalarsPerPos, paint);
1777 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001778 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001779
1780 GrPaint grPaint;
1781 SkAutoCachedTexture act;
Scroggod757df22011-05-16 13:11:16 +00001782 if (!this->skPaint2GrPaintShader(paint,
1783 &act,
1784 *draw.fMatrix,
1785 &grPaint,
1786 true)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001787 return;
1788 }
1789
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001790 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001791 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001792 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1793 scalarsPerPos, paint);
1794 }
1795}
1796
1797void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1798 size_t len, const SkPath& path,
1799 const SkMatrix* m, const SkPaint& paint) {
1800 CHECK_SHOULD_DRAW(draw);
1801
1802 SkASSERT(draw.fDevice == this);
1803 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1804}
1805
1806///////////////////////////////////////////////////////////////////////////////
1807
reed@google.comf67e4cf2011-03-15 20:56:58 +00001808bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1809 if (!paint.isLCDRenderText()) {
1810 // we're cool with the paint as is
1811 return false;
1812 }
1813
1814 if (paint.getShader() ||
1815 paint.getXfermode() || // unless its srcover
1816 paint.getMaskFilter() ||
1817 paint.getRasterizer() ||
1818 paint.getColorFilter() ||
1819 paint.getPathEffect() ||
1820 paint.isFakeBoldText() ||
1821 paint.getStyle() != SkPaint::kFill_Style) {
1822 // turn off lcd
1823 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1824 flags->fHinting = paint.getHinting();
1825 return true;
1826 }
1827 // we're cool with the paint as is
1828 return false;
1829}
1830
reed@google.com75d939b2011-12-07 15:07:23 +00001831void SkGpuDevice::flush() {
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001832 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001833}
1834
reed@google.comf67e4cf2011-03-15 20:56:58 +00001835///////////////////////////////////////////////////////////////////////////////
1836
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001837SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(const SkBitmap& bitmap,
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001838 const GrSamplerState* sampler,
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001839 TexType type) {
1840 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001841 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001842
bsalomon@google.come97f0852011-06-17 13:10:25 +00001843 if (kBitmap_TexType != type) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001844 const GrTextureDesc desc = {
1845 kRenderTarget_GrTextureFlagBit,
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001846 bitmap.width(),
1847 bitmap.height(),
bsalomon@google.com78d6cf92012-01-30 18:09:31 +00001848 SkGr::Bitmap2PixelConfig(bitmap),
1849 {0} // samples
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001850 };
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001851 GrContext::ScratchTexMatch match;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001852 if (kSaveLayerDeviceRenderTarget_TexType == type) {
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001853 // we know layers will only be drawn through drawDevice.
1854 // drawDevice has been made to work with content embedded in a
1855 // larger texture so its okay to use the approximate version.
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001856 match = GrContext::kApprox_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001857 } else {
bsalomon@google.come97f0852011-06-17 13:10:25 +00001858 SkASSERT(kDeviceRenderTarget_TexType == type);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001859 match = GrContext::kExact_ScratchTexMatch;
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001860 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001861 entry = ctx->lockScratchTexture(desc, match);
reed@google.comac10a2d2010-12-22 21:39:39 +00001862 } else {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001863 if (!bitmap.isVolatile()) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001864 GrContext::TextureKey key = bitmap.getGenerationID();
1865 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001866
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001867 entry = ctx->findAndLockTexture(key, bitmap.width(),
1868 bitmap.height(), sampler);
1869 if (NULL == entry.texture()) {
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001870 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
junov@google.com4ee7ae52011-06-30 17:30:49 +00001871 bitmap);
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001872 }
junov@google.com4ee7ae52011-06-30 17:30:49 +00001873 } else {
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001874 entry = sk_gr_create_bitmap_texture(ctx, gUNCACHED_KEY,
1875 sampler, bitmap);
junov@google.com4ee7ae52011-06-30 17:30:49 +00001876 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001877 if (NULL == entry.texture()) {
junov@google.com4ee7ae52011-06-30 17:30:49 +00001878 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1879 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001880 }
1881 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001882 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001883}
1884
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001885void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1886 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001887}
1888
bsalomon@google.comfb309512011-11-30 14:13:48 +00001889bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
1890 const GrSamplerState& sampler) const {
1891 GrContext::TextureKey key = bitmap.getGenerationID();
1892 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
1893 return this->context()->isTextureInCache(key, bitmap.width(),
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001894 bitmap.height(), &sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001895
1896}
1897
1898
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001899SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1900 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001901 bool isOpaque,
1902 Usage usage) {
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001903 return SkNEW_ARGS(SkGpuDevice,(this->context(), config,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001904 width, height, usage));
1905}
1906