blob: de9d9737df5b6d407fa7c74502cc23100feb1797 [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
tomhudson@google.com898e7b52012-06-01 20:42:15 +00009#include "SkGpuDevice.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000010
tomhudson@google.com898e7b52012-06-01 20:42:15 +000011#include "effects/GrGradientEffects.h"
epoger@google.comec3ed6a2011-07-28 14:26:00 +000012
reed@google.comac10a2d2010-12-22 21:39:39 +000013#include "GrContext.h"
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000014#include "GrDefaultTextContext.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000015#include "GrTextContext.h"
16
reed@google.comac10a2d2010-12-22 21:39:39 +000017#include "SkGrTexturePixelRef.h"
18
Scroggo97c88c22011-05-11 14:05:25 +000019#include "SkColorFilter.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000020#include "SkDrawProcs.h"
21#include "SkGlyphCache.h"
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +000022#include "SkImageFilter.h"
reed@google.comfe626382011-09-21 13:50:35 +000023#include "SkTLazy.h"
reed@google.comc9aa5872011-04-05 21:05:37 +000024#include "SkUtils.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000025
bsalomon@google.com06cd7322012-03-30 18:45:35 +000026#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
reed@google.comac10a2d2010-12-22 21:39:39 +000027
28#if 0
29 extern bool (*gShouldDrawProc)();
30 #define CHECK_SHOULD_DRAW(draw) \
31 do { \
32 if (gShouldDrawProc && !gShouldDrawProc()) return; \
33 this->prepareRenderTarget(draw); \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000034 GrAssert(!fNeedClear) \
reed@google.comac10a2d2010-12-22 21:39:39 +000035 } while (0)
36#else
bsalomon@google.com06cd7322012-03-30 18:45:35 +000037 #define CHECK_SHOULD_DRAW(draw) this->prepareRenderTarget(draw); \
38 GrAssert(!fNeedClear)
reed@google.comac10a2d2010-12-22 21:39:39 +000039#endif
40
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000041// we use the same texture slot on GrPaint for bitmaps and shaders
42// (since drawBitmap, drawSprite, and drawDevice ignore skia's shader)
43enum {
44 kBitmapTextureIdx = 0,
45 kShaderTextureIdx = 0
46};
47
reed@google.comcde92112011-07-06 20:00:52 +000048
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +000049#define MAX_BLUR_SIGMA 4.0f
50// FIXME: This value comes from from SkBlurMaskFilter.cpp.
51// Should probably be put in a common header someplace.
52#define MAX_BLUR_RADIUS SkIntToScalar(128)
53// This constant approximates the scaling done in the software path's
54// "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
55// IMHO, it actually should be 1: we blur "less" than we should do
56// according to the CSS and canvas specs, simply because Safari does the same.
57// Firefox used to do the same too, until 4.0 where they fixed it. So at some
58// point we should probably get rid of these scaling constants and rebaseline
59// all the blur tests.
60#define BLUR_SIGMA_SCALE 0.6f
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000061// This constant represents the screen alignment criterion in texels for
62// requiring texture domain clamping to prevent color bleeding when drawing
63// a sub region of a larger source image.
64#define COLOR_BLEED_TOLERANCE SkFloatToScalar(0.001f)
bsalomon@google.com06cd7322012-03-30 18:45:35 +000065
66#define DO_DEFERRED_CLEAR \
67 do { \
68 if (fNeedClear) { \
bsalomon@google.com730ca3b2012-04-03 13:25:12 +000069 this->clear(0x0); \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000070 fNeedClear = false; \
71 } \
72 } while (false) \
73
reed@google.comac10a2d2010-12-22 21:39:39 +000074///////////////////////////////////////////////////////////////////////////////
75
bsalomon@google.com84405e02012-03-05 19:57:21 +000076class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
77public:
78 SkAutoCachedTexture() { }
79 SkAutoCachedTexture(SkGpuDevice* device,
80 const SkBitmap& bitmap,
81 const GrSamplerState* sampler,
82 GrTexture** texture) {
83 GrAssert(texture);
84 *texture = this->set(device, bitmap, sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +000085 }
reed@google.comac10a2d2010-12-22 21:39:39 +000086
bsalomon@google.com84405e02012-03-05 19:57:21 +000087 ~SkAutoCachedTexture() {
88 if (fTex.texture()) {
89 fDevice->unlockCachedTexture(fTex);
90 }
reed@google.comac10a2d2010-12-22 21:39:39 +000091 }
bsalomon@google.com84405e02012-03-05 19:57:21 +000092
93 GrTexture* set(SkGpuDevice* device,
94 const SkBitmap& bitmap,
95 const GrSamplerState* sampler) {
96 if (fTex.texture()) {
97 fDevice->unlockCachedTexture(fTex);
98 }
99 fDevice = device;
100 GrTexture* texture = (GrTexture*)bitmap.getTexture();
101 if (texture) {
102 // return the native texture
103 fTex.reset();
104 } else {
105 // look it up in our cache
106 fTex = device->lockCachedTexture(bitmap, sampler);
107 texture = fTex.texture();
108 }
109 return texture;
110 }
111
112private:
113 SkGpuDevice* fDevice;
114 GrContext::TextureCacheEntry fTex;
115};
reed@google.comac10a2d2010-12-22 21:39:39 +0000116
117///////////////////////////////////////////////////////////////////////////////
118
119bool gDoTraceDraw;
120
121struct GrSkDrawProcs : public SkDrawProcs {
122public:
123 GrContext* fContext;
124 GrTextContext* fTextContext;
125 GrFontScaler* fFontScaler; // cached in the skia glyphcache
126};
127
128///////////////////////////////////////////////////////////////////////////////
129
reed@google.comaf951c92011-06-16 19:10:39 +0000130static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
131 switch (config) {
132 case kAlpha_8_GrPixelConfig:
133 *isOpaque = false;
134 return SkBitmap::kA8_Config;
135 case kRGB_565_GrPixelConfig:
136 *isOpaque = true;
137 return SkBitmap::kRGB_565_Config;
138 case kRGBA_4444_GrPixelConfig:
139 *isOpaque = false;
140 return SkBitmap::kARGB_4444_Config;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000141 case kSkia8888_PM_GrPixelConfig:
142 // we don't currently have a way of knowing whether
143 // a 8888 is opaque based on the config.
144 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000145 return SkBitmap::kARGB_8888_Config;
146 default:
147 *isOpaque = false;
148 return SkBitmap::kNo_Config;
149 }
150}
reed@google.comac10a2d2010-12-22 21:39:39 +0000151
reed@google.comaf951c92011-06-16 19:10:39 +0000152static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000153 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000154
155 bool isOpaque;
156 SkBitmap bitmap;
157 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
158 renderTarget->width(), renderTarget->height());
159 bitmap.setIsOpaque(isOpaque);
160 return bitmap;
161}
162
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000163SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
164: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
165 this->initFromRenderTarget(context, texture->asRenderTarget());
166}
167
reed@google.comaf951c92011-06-16 19:10:39 +0000168SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
169: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000170 this->initFromRenderTarget(context, renderTarget);
171}
172
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000173void SkGpuDevice::initFromRenderTarget(GrContext* context,
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000174 GrRenderTarget* renderTarget) {
reed@google.comaf951c92011-06-16 19:10:39 +0000175 fNeedPrepareRenderTarget = false;
176 fDrawProcs = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000177
reed@google.comaf951c92011-06-16 19:10:39 +0000178 fContext = context;
179 fContext->ref();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000180
reed@google.comaf951c92011-06-16 19:10:39 +0000181 fTexture = NULL;
182 fRenderTarget = NULL;
183 fNeedClear = false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000184
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000185 GrAssert(NULL != renderTarget);
186 fRenderTarget = renderTarget;
187 fRenderTarget->ref();
188 // if this RT is also a texture, hold a ref on it
189 fTexture = fRenderTarget->asTexture();
190 SkSafeRef(fTexture);
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000191
192 // Create a pixel ref for the underlying SkBitmap. We prefer a texture pixel
193 // ref to a render target pixel reft. The pixel ref may get ref'ed outside
194 // the device via accessBitmap. This external ref may outlive the device.
195 // Since textures own their render targets (but not vice-versa) we
196 // are ensuring that both objects will live as long as the pixel ref.
197 SkPixelRef* pr;
198 if (fTexture) {
199 pr = new SkGrTexturePixelRef(fTexture);
200 } else {
201 pr = new SkGrRenderTargetPixelRef(fRenderTarget);
202 }
reed@google.comaf951c92011-06-16 19:10:39 +0000203 this->setPixelRef(pr, 0)->unref();
bsalomon@google.comf4a9c822012-03-16 14:02:46 +0000204
205 fTextContext = NULL;
reed@google.comaf951c92011-06-16 19:10:39 +0000206}
207
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000208SkGpuDevice::SkGpuDevice(GrContext* context,
209 SkBitmap::Config config,
210 int width,
211 int height)
212 : SkDevice(config, width, height, false /*isOpaque*/) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000213 fNeedPrepareRenderTarget = false;
214 fDrawProcs = NULL;
215
reed@google.com7b201d22011-01-11 18:59:23 +0000216 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000217 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000218
reed@google.comac10a2d2010-12-22 21:39:39 +0000219 fTexture = NULL;
220 fRenderTarget = NULL;
221 fNeedClear = false;
222
reed@google.comaf951c92011-06-16 19:10:39 +0000223 if (config != SkBitmap::kRGB_565_Config) {
224 config = SkBitmap::kARGB_8888_Config;
225 }
226 SkBitmap bm;
227 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000228
reed@google.comaf951c92011-06-16 19:10:39 +0000229 const GrTextureDesc desc = {
230 kRenderTarget_GrTextureFlagBit,
reed@google.comaf951c92011-06-16 19:10:39 +0000231 width,
232 height,
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000233 SkGr::Bitmap2PixelConfig(bm),
bsalomon@google.comb9014f42012-03-30 14:22:41 +0000234 0 // samples
reed@google.comaf951c92011-06-16 19:10:39 +0000235 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000236
reed@google.comaf951c92011-06-16 19:10:39 +0000237 fTexture = fContext->createUncachedTexture(desc, NULL, 0);
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000238
reed@google.comaf951c92011-06-16 19:10:39 +0000239 if (NULL != fTexture) {
240 fRenderTarget = fTexture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000241 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000242
reed@google.comaf951c92011-06-16 19:10:39 +0000243 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000244
reed@google.comaf951c92011-06-16 19:10:39 +0000245 // wrap the bitmap with a pixelref to expose our texture
246 SkGrTexturePixelRef* pr = new SkGrTexturePixelRef(fTexture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000247 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000248 } else {
249 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
250 width, height);
251 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000252 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +0000253
254 fTextContext = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000255}
256
257SkGpuDevice::~SkGpuDevice() {
258 if (fDrawProcs) {
259 delete fDrawProcs;
260 }
261
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000262 SkSafeUnref(fTexture);
263 SkSafeUnref(fRenderTarget);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000264 if (fCache.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000265 GrAssert(NULL != fTexture);
266 GrAssert(fRenderTarget == fTexture->asRenderTarget());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000267 fContext->unlockTexture(fCache);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000268 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000269 fContext->unref();
bsalomon@google.comf4a9c822012-03-16 14:02:46 +0000270
271 if (NULL != fTextContext) {
272 fTextContext->unref();
273 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000274}
275
reed@google.comac10a2d2010-12-22 21:39:39 +0000276///////////////////////////////////////////////////////////////////////////////
277
278void SkGpuDevice::makeRenderTargetCurrent() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000279 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000280 fContext->setRenderTarget(fRenderTarget);
281 fContext->flush(true);
282 fNeedPrepareRenderTarget = true;
283}
284
285///////////////////////////////////////////////////////////////////////////////
286
bsalomon@google.comc4364992011-11-07 15:54:49 +0000287namespace {
288GrPixelConfig config8888_to_gr_config(SkCanvas::Config8888 config8888) {
289 switch (config8888) {
290 case SkCanvas::kNative_Premul_Config8888:
291 return kSkia8888_PM_GrPixelConfig;
292 case SkCanvas::kNative_Unpremul_Config8888:
293 return kSkia8888_UPM_GrPixelConfig;
294 case SkCanvas::kBGRA_Premul_Config8888:
295 return kBGRA_8888_PM_GrPixelConfig;
296 case SkCanvas::kBGRA_Unpremul_Config8888:
297 return kBGRA_8888_UPM_GrPixelConfig;
298 case SkCanvas::kRGBA_Premul_Config8888:
299 return kRGBA_8888_PM_GrPixelConfig;
300 case SkCanvas::kRGBA_Unpremul_Config8888:
301 return kRGBA_8888_UPM_GrPixelConfig;
302 default:
303 GrCrash("Unexpected Config8888.");
304 return kSkia8888_PM_GrPixelConfig;
305 }
306}
307}
308
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000309bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
310 int x, int y,
311 SkCanvas::Config8888 config8888) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000312 DO_DEFERRED_CLEAR;
bsalomon@google.com910267d2011-11-02 20:06:25 +0000313 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
314 SkASSERT(!bitmap.isNull());
315 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000316
bsalomon@google.com910267d2011-11-02 20:06:25 +0000317 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000318 GrPixelConfig config;
319 config = config8888_to_gr_config(config8888);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000320 return fContext->readRenderTargetPixels(fRenderTarget,
321 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000322 bitmap.width(),
323 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000324 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000325 bitmap.getPixels(),
326 bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000327}
328
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000329void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
330 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000331 SkAutoLockPixels alp(bitmap);
332 if (!bitmap.readyToDraw()) {
333 return;
334 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000335
336 GrPixelConfig config;
337 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
338 config = config8888_to_gr_config(config8888);
339 } else {
340 config= SkGr::BitmapConfig2PixelConfig(bitmap.config(),
341 bitmap.isOpaque());
342 }
343
bsalomon@google.com6f379512011-11-16 20:36:03 +0000344 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
345 config, bitmap.getPixels(), bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000346}
347
348///////////////////////////////////////////////////////////////////////////////
349
350static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000351 const SkClipStack& clipStack,
reed@google.com6f8f2922011-03-04 22:27:10 +0000352 const SkRegion& clipRegion,
353 const SkIPoint& origin) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000354 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000355
356 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000357 iter.reset(clipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000358 const SkIRect& skBounds = clipRegion.getBounds();
359 GrRect bounds;
360 bounds.setLTRB(GrIntToScalar(skBounds.fLeft),
361 GrIntToScalar(skBounds.fTop),
362 GrIntToScalar(skBounds.fRight),
363 GrIntToScalar(skBounds.fBottom));
reed@google.com6f8f2922011-03-04 22:27:10 +0000364 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
365 &bounds);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000366 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000367}
368
369// call this ever each draw call, to ensure that the context reflects our state,
370// and not the state from some other canvas/device
371void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
372 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000373 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000374
375 fContext->setRenderTarget(fRenderTarget);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000376 SkASSERT(draw.fClipStack);
377 convert_matrixclip(fContext, *draw.fMatrix,
reed@google.com6f8f2922011-03-04 22:27:10 +0000378 *draw.fClipStack, *draw.fClip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000379 fNeedPrepareRenderTarget = false;
380 }
381}
382
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000383void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
384 const SkClipStack& clipStack) {
385 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
386 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000387 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000388}
389
390void SkGpuDevice::gainFocus(SkCanvas* canvas, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000391 const SkRegion& clip, const SkClipStack& clipStack) {
392
reed@google.comac10a2d2010-12-22 21:39:39 +0000393 fContext->setRenderTarget(fRenderTarget);
394
bsalomon@google.comd302f142011-03-03 13:54:13 +0000395 this->INHERITED::gainFocus(canvas, matrix, clip, clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000396
reed@google.com6f8f2922011-03-04 22:27:10 +0000397 convert_matrixclip(fContext, matrix, clipStack, clip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000398
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000399 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000400}
401
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000402SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000403 DO_DEFERRED_CLEAR;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000404 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000405}
406
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000407bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000408 if (NULL != fTexture) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000409 paint->setTexture(kBitmapTextureIdx, fTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000410 return true;
411 }
412 return false;
413}
414
415///////////////////////////////////////////////////////////////////////////////
416
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000417SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
418SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
419SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
420SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
421SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
422 shader_type_mismatch);
423SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 4, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000424
bsalomon@google.com84405e02012-03-05 19:57:21 +0000425namespace {
426
427// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
428// justAlpha indicates that skPaint's alpha should be used rather than the color
429// Callers may subsequently modify the GrPaint. Setting constantColor indicates
430// that the final paint will draw the same color at every pixel. This allows
431// an optimization where the the color filter can be applied to the skPaint's
432// color once while converting to GrPain and then ignored.
433inline bool skPaint2GrPaintNoShader(const SkPaint& skPaint,
434 bool justAlpha,
435 bool constantColor,
436 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000437
438 grPaint->fDither = skPaint.isDither();
439 grPaint->fAntiAlias = skPaint.isAntiAlias();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000440 grPaint->fCoverage = 0xFF;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000441
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000442 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
443 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000444
445 SkXfermode* mode = skPaint.getXfermode();
446 if (mode) {
447 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000448 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000449#if 0
450 return false;
451#endif
452 }
453 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000454 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
455 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
456
bsalomon@google.com5782d712011-01-21 21:03:59 +0000457 if (justAlpha) {
458 uint8_t alpha = skPaint.getAlpha();
459 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000460 // justAlpha is currently set to true only if there is a texture,
461 // so constantColor should not also be true.
462 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000463 } else {
464 grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000465 grPaint->setTexture(kShaderTextureIdx, NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000466 }
Scroggo97c88c22011-05-11 14:05:25 +0000467 SkColorFilter* colorFilter = skPaint.getColorFilter();
468 SkColor color;
469 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000470 SkScalar matrix[20];
Scroggo97c88c22011-05-11 14:05:25 +0000471 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000472 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000473 if (!constantColor) {
474 grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color);
475 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000476 } else {
477 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
478 grPaint->fColor = SkGr::SkColor2GrColor(filtered);
senorblanco@chromium.orgb3c20fa2012-01-03 21:20:19 +0000479 grPaint->resetColorFilter();
Scroggod757df22011-05-16 13:11:16 +0000480 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000481 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
482 grPaint->fColorMatrixEnabled = true;
483 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
484 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
485 } else {
486 grPaint->resetColorFilter();
Scroggo97c88c22011-05-11 14:05:25 +0000487 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000488 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000489}
490
bsalomon@google.com84405e02012-03-05 19:57:21 +0000491// This function is similar to skPaint2GrPaintNoShader but also converts
492// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
493// be used is set on grPaint and returned in param act. constantColor has the
494// same meaning as in skPaint2GrPaintNoShader.
495inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
496 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000497 bool constantColor,
498 SkGpuDevice::SkAutoCachedTexture* act,
499 GrPaint* grPaint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000500
bsalomon@google.com5782d712011-01-21 21:03:59 +0000501 SkASSERT(NULL != act);
reed@google.comac10a2d2010-12-22 21:39:39 +0000502
bsalomon@google.com5782d712011-01-21 21:03:59 +0000503 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000504 if (NULL == shader) {
bsalomon@google.com84405e02012-03-05 19:57:21 +0000505 return skPaint2GrPaintNoShader(skPaint,
506 false,
507 constantColor,
508 grPaint);
509 } else if (!skPaint2GrPaintNoShader(skPaint, true, false, grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000510 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000511 }
512
reed@google.comac10a2d2010-12-22 21:39:39 +0000513 SkBitmap bitmap;
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000514 SkMatrix* matrix = grPaint->textureSampler(kShaderTextureIdx)->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000515 SkShader::TileMode tileModes[2];
516 SkScalar twoPointParams[3];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000517 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000518 tileModes, twoPointParams);
519
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000520 if (SkShader::kNone_BitmapType == bmptype) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000521 SkShader::GradientInfo info;
522 SkColor color;
523
524 info.fColors = &color;
525 info.fColorOffsets = NULL;
526 info.fColorCount = 1;
527 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
528 SkPaint copy(skPaint);
529 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000530 // modulate the paint alpha by the shader's solid color alpha
531 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
532 copy.setColor(SkColorSetA(color, newA));
bsalomon@google.com84405e02012-03-05 19:57:21 +0000533 return skPaint2GrPaintNoShader(copy,
534 false,
535 constantColor,
536 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000537 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000538 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000539 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000540 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000541 switch (bmptype) {
542 case SkShader::kRadial_BitmapType: {
543 sampler->setCustomStage(new GrRadialGradient());
544 } break;
545 case SkShader::kSweep_BitmapType: {
546 sampler->setCustomStage(new GrSweepGradient());
547 } break;
548 case SkShader::kTwoPointRadial_BitmapType: {
549 sampler->setCustomStage(new GrRadial2Gradient(twoPointParams[0],
550 twoPointParams[1],
551 twoPointParams[2] < 0));
552 } break;
553 default:
554 break;
555 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000556 if (skPaint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000557 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000558 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000559 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000560 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000561 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
562 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000563
bsalomon@google.com84405e02012-03-05 19:57:21 +0000564 GrTexture* texture = act->set(dev, bitmap, sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000565 if (NULL == texture) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000566 SkDebugf("Couldn't convert bitmap to texture.\n");
567 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000568 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000569 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000570
571 // since our texture coords will be in local space, we wack the texture
572 // matrix to map them back into 0...1 before we load it
573 SkMatrix localM;
574 if (shader->getLocalMatrix(&localM)) {
575 SkMatrix inverse;
576 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000577 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000578 }
579 }
580 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000581 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
582 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000583 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000584 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000585 GrScalar s = SkFloatToScalar(1.f / bitmap.width());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000586 matrix->postScale(s, s);
reed@google.comac10a2d2010-12-22 21:39:39 +0000587 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000588
589 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000590}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000591}
reed@google.comac10a2d2010-12-22 21:39:39 +0000592
593///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000594
bsalomon@google.com398109c2011-04-14 18:40:27 +0000595void SkGpuDevice::clear(SkColor color) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000596 fContext->setRenderTarget(fRenderTarget);
bsalomon@google.com31a58402011-04-27 21:00:02 +0000597 fContext->clear(NULL, color);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000598}
599
reed@google.comac10a2d2010-12-22 21:39:39 +0000600void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
601 CHECK_SHOULD_DRAW(draw);
602
bsalomon@google.com5782d712011-01-21 21:03:59 +0000603 GrPaint grPaint;
604 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000605 if (!skPaint2GrPaintShader(this,
606 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000607 true,
608 &act,
609 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000610 return;
611 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000612
613 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000614}
615
616// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000617static const GrPrimitiveType gPointMode2PrimtiveType[] = {
618 kPoints_PrimitiveType,
619 kLines_PrimitiveType,
620 kLineStrip_PrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000621};
622
623void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000624 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000625 CHECK_SHOULD_DRAW(draw);
626
627 SkScalar width = paint.getStrokeWidth();
628 if (width < 0) {
629 return;
630 }
631
632 // we only handle hairlines here, else we let the SkDraw call our drawPath()
633 if (width > 0) {
634 draw.drawPoints(mode, count, pts, paint, true);
635 return;
636 }
637
bsalomon@google.com5782d712011-01-21 21:03:59 +0000638 GrPaint grPaint;
639 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000640 if (!skPaint2GrPaintShader(this,
641 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000642 true,
643 &act,
644 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000645 return;
646 }
647
bsalomon@google.com5782d712011-01-21 21:03:59 +0000648 fContext->drawVertices(grPaint,
649 gPointMode2PrimtiveType[mode],
650 count,
651 (GrPoint*)pts,
652 NULL,
653 NULL,
654 NULL,
655 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000656}
657
reed@google.comc9aa5872011-04-05 21:05:37 +0000658///////////////////////////////////////////////////////////////////////////////
659
reed@google.comac10a2d2010-12-22 21:39:39 +0000660void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
661 const SkPaint& paint) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000662 CHECK_SHOULD_DRAW(draw);
663
bungeman@google.com79bd8772011-07-18 15:34:08 +0000664 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000665 SkScalar width = paint.getStrokeWidth();
666
667 /*
668 We have special code for hairline strokes, miter-strokes, and fills.
669 Anything else we just call our path code.
670 */
671 bool usePath = doStroke && width > 0 &&
672 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000673 // another two reasons we might need to call drawPath...
674 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000675 usePath = true;
676 }
reed@google.com67db6642011-05-26 11:46:35 +0000677 // until we aa rotated rects...
678 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
679 usePath = true;
680 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000681 // small miter limit means right angles show bevel...
682 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
683 paint.getStrokeMiter() < SK_ScalarSqrt2)
684 {
685 usePath = true;
686 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000687 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000688 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
689 usePath = true;
690 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000691
692 if (usePath) {
693 SkPath path;
694 path.addRect(rect);
695 this->drawPath(draw, path, paint, NULL, true);
696 return;
697 }
698
699 GrPaint grPaint;
700 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000701 if (!skPaint2GrPaintShader(this,
702 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000703 true,
704 &act,
705 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000706 return;
707 }
reed@google.com20efde72011-05-09 17:00:02 +0000708 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000709}
710
reed@google.com69302852011-02-16 18:08:07 +0000711#include "SkMaskFilter.h"
712#include "SkBounder.h"
713
bsalomon@google.com85003222012-03-28 14:44:37 +0000714///////////////////////////////////////////////////////////////////////////////
715
716// helpers for applying mask filters
717namespace {
718
719GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000720 switch (fillType) {
721 case SkPath::kWinding_FillType:
722 return kWinding_PathFill;
723 case SkPath::kEvenOdd_FillType:
724 return kEvenOdd_PathFill;
725 case SkPath::kInverseWinding_FillType:
726 return kInverseWinding_PathFill;
727 case SkPath::kInverseEvenOdd_FillType:
728 return kInverseEvenOdd_PathFill;
729 default:
730 SkDebugf("Unsupported path fill type\n");
731 return kHairLine_PathFill;
732 }
733}
734
bsalomon@google.com85003222012-03-28 14:44:37 +0000735// We prefer to blur small rect with small radius via CPU.
736#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
737#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
738inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
739 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
740 rect.height() <= MIN_GPU_BLUR_SIZE &&
741 radius <= MIN_GPU_BLUR_RADIUS) {
742 return true;
743 }
744 return false;
745}
746
747bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
748 SkMaskFilter* filter, const SkMatrix& matrix,
749 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000750 GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000751#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000752 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000753#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000754 SkMaskFilter::BlurInfo info;
755 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000756 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000757 return false;
758 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000759 SkScalar radius = info.fIgnoreTransform ? info.fRadius
760 : matrix.mapRadius(info.fRadius);
761 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000762 if (radius <= 0) {
763 return false;
764 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000765
766 SkRect srcRect = path.getBounds();
767 if (shouldDrawBlurWithCPU(srcRect, radius)) {
768 return false;
769 }
770
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000771 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000772 float sigma3 = sigma * 3.0f;
773
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000774 SkRect clipRect;
775 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000776
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000777 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000778 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
779 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000780 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000781 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000782 SkIRect finalIRect;
783 finalRect.roundOut(&finalIRect);
784 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000785 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000786 }
787 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000788 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000789 }
790 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000791 srcRect.offset(offset);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000792 GrTextureDesc desc = {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000793 kRenderTarget_GrTextureFlagBit,
bungeman@google.comf8aa18c2012-03-19 21:04:52 +0000794 SkScalarCeilToInt(srcRect.width()),
795 SkScalarCeilToInt(srcRect.height()),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000796 // We actually only need A8, but it often isn't supported as a
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000797 // render target so default to RGBA_8888
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000798 kRGBA_8888_PM_GrPixelConfig,
bsalomon@google.comb9014f42012-03-30 14:22:41 +0000799 0 // samples
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000800 };
801
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000802 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
803 desc.fConfig = kAlpha_8_GrPixelConfig;
804 }
805
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000806 GrAutoScratchTexture pathEntry(context, desc);
807 GrTexture* pathTexture = pathEntry.texture();
808 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000809 return false;
810 }
811 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000812 // Once this code moves into GrContext, this should be changed to use
813 // an AutoClipRestore.
814 GrClip oldClip = context->getClip();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000815 context->setRenderTarget(pathTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000816 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000817 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000818 GrPaint tempPaint;
819 tempPaint.reset();
820
821 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000822 tempPaint.fAntiAlias = grp->fAntiAlias;
823 if (tempPaint.fAntiAlias) {
824 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
825 // blend coeff of zero requires dual source blending support in order
826 // to properly blend partially covered pixels. This means the AA
827 // code path may not be taken. So we use a dst blend coeff of ISA. We
828 // could special case AA draws to a dst surface with known alpha=0 to
829 // use a zero dst coeff when dual source blending isn't available.
830 tempPaint.fSrcBlendCoeff = kOne_BlendCoeff;
831 tempPaint.fDstBlendCoeff = kISC_BlendCoeff;
832 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000833 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000834 context->drawPath(tempPaint, path, pathFillType, &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000835
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000836 GrAutoScratchTexture temp1, temp2;
837 // If we're doing a normal blur, we can clobber the pathTexture in the
838 // gaussianBlur. Otherwise, we need to save it for later compositing.
839 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000840 GrTexture* blurTexture = context->gaussianBlur(pathTexture,
841 &temp1,
842 isNormalBlur ? NULL : &temp2,
843 srcRect, sigma, sigma);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000844
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000845 if (!isNormalBlur) {
846 GrPaint paint;
847 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000848 paint.textureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000849 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
850 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000851 // Blend pathTexture over blurTexture.
852 context->setRenderTarget(blurTexture->asRenderTarget());
853 paint.setTexture(0, pathTexture);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000854 if (SkMaskFilter::kInner_BlurType == blurType) {
855 // inner: dst = dst * src
856 paint.fSrcBlendCoeff = kDC_BlendCoeff;
857 paint.fDstBlendCoeff = kZero_BlendCoeff;
858 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
859 // solid: dst = src + dst - src * dst
860 // = (1 - dst) * src + 1 * dst
861 paint.fSrcBlendCoeff = kIDC_BlendCoeff;
862 paint.fDstBlendCoeff = kOne_BlendCoeff;
863 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
864 // outer: dst = dst * (1 - src)
865 // = 0 * src + (1 - src) * dst
866 paint.fSrcBlendCoeff = kZero_BlendCoeff;
867 paint.fDstBlendCoeff = kISC_BlendCoeff;
868 }
869 context->drawRect(paint, srcRect);
870 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000871 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000872 context->setClip(oldClip);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000873
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000874 if (grp->hasTextureOrMask()) {
875 GrMatrix inverse;
876 if (!matrix.invert(&inverse)) {
877 return false;
878 }
879 grp->preConcatActiveSamplerMatrices(inverse);
880 }
881
882 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
883 // we assume the last mask index is available for use
884 GrAssert(NULL == grp->getMask(MASK_IDX));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000885 grp->setMask(MASK_IDX, blurTexture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000886 grp->maskSampler(MASK_IDX)->reset();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000887
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000888 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
889 -finalRect.fTop);
890 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
891 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000892 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000893 return true;
894}
895
bsalomon@google.com85003222012-03-28 14:44:37 +0000896bool drawWithMaskFilter(GrContext* context, const SkPath& path,
897 SkMaskFilter* filter, const SkMatrix& matrix,
898 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000899 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000900 SkMask srcM, dstM;
901
902 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000903 SkMask::kComputeBoundsAndRenderImage_CreateMode,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000904 style)) {
reed@google.com69302852011-02-16 18:08:07 +0000905 return false;
906 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000907 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000908
909 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
910 return false;
911 }
912 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000913 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000914
915 if (clip.quickReject(dstM.fBounds)) {
916 return false;
917 }
918 if (bounder && !bounder->doIRect(dstM.fBounds)) {
919 return false;
920 }
921
922 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
923 // the current clip (and identity matrix) and grpaint settings
924
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000925 // used to compute inverse view, if necessary
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000926 GrMatrix ivm = matrix;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000927
reed@google.com0c219b62011-02-16 21:31:18 +0000928 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +0000929
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000930 const GrTextureDesc desc = {
931 kNone_GrTextureFlags,
reed@google.com69302852011-02-16 18:08:07 +0000932 dstM.fBounds.width(),
933 dstM.fBounds.height(),
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000934 kAlpha_8_GrPixelConfig,
bsalomon@google.comb9014f42012-03-30 14:22:41 +0000935 0, // samples
reed@google.com69302852011-02-16 18:08:07 +0000936 };
937
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000938 GrAutoScratchTexture ast(context, desc);
939 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000940
reed@google.com69302852011-02-16 18:08:07 +0000941 if (NULL == texture) {
942 return false;
943 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000944 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000945 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000946
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000947 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
948 grp->preConcatActiveSamplerMatrices(ivm);
949 }
950
951 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
952 // we assume the last mask index is available for use
953 GrAssert(NULL == grp->getMask(MASK_IDX));
954 grp->setMask(MASK_IDX, texture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000955 grp->maskSampler(MASK_IDX)->reset();
reed@google.com69302852011-02-16 18:08:07 +0000956
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000957 GrRect d;
958 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +0000959 GrIntToScalar(dstM.fBounds.fTop),
960 GrIntToScalar(dstM.fBounds.fRight),
961 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000962
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000963 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
964 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
965 -dstM.fBounds.fTop*SK_Scalar1);
966 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000967 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000968 return true;
969}
reed@google.com69302852011-02-16 18:08:07 +0000970
bsalomon@google.com85003222012-03-28 14:44:37 +0000971}
972
973///////////////////////////////////////////////////////////////////////////////
974
reed@google.com0c219b62011-02-16 21:31:18 +0000975void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000976 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000977 bool pathIsMutable) {
978 CHECK_SHOULD_DRAW(draw);
979
reed@google.comfe626382011-09-21 13:50:35 +0000980 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000981
bsalomon@google.com5782d712011-01-21 21:03:59 +0000982 GrPaint grPaint;
983 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000984 if (!skPaint2GrPaintShader(this,
985 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000986 true,
987 &act,
988 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000989 return;
990 }
991
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000992 // can we cheat, and threat a thin stroke as a hairline w/ coverage
993 // if we can, we draw lots faster (raster device does this same test)
994 SkScalar hairlineCoverage;
995 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
996 doFill = false;
997 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
998 grPaint.fCoverage);
999 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001000
reed@google.comfe626382011-09-21 13:50:35 +00001001 // If we have a prematrix, apply it to the path, optimizing for the case
1002 // where the original path can in fact be modified in place (even though
1003 // its parameter type is const).
1004 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1005 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001006
1007 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001008 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001009
reed@google.come3445642011-02-16 23:20:39 +00001010 if (!pathIsMutable) {
1011 result = &tmpPath;
1012 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001013 }
reed@google.come3445642011-02-16 23:20:39 +00001014 // should I push prePathMatrix on our MV stack temporarily, instead
1015 // of applying it here? See SkDraw.cpp
1016 pathPtr->transform(*prePathMatrix, result);
1017 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001018 }
reed@google.com0c219b62011-02-16 21:31:18 +00001019 // at this point we're done with prePathMatrix
1020 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001021
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001022 if (paint.getPathEffect() ||
1023 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001024 // it is safe to use tmpPath here, even if we already used it for the
1025 // prepathmatrix, since getFillPath can take the same object for its
1026 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001027 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001028 pathPtr = &tmpPath;
1029 }
1030
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001031 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001032 // avoid possibly allocating a new path in transform if we can
1033 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1034
1035 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001036 pathPtr->transform(*draw.fMatrix, devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001037 GrPathFill pathFillType = doFill ?
1038 skToGrFillType(devPathPtr->getFillType()) : kHairLine_PathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001039 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001040 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001041 &grPaint, pathFillType)) {
1042 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
1043 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001044 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001045 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001046 &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001047 }
reed@google.com69302852011-02-16 18:08:07 +00001048 return;
1049 }
reed@google.com69302852011-02-16 18:08:07 +00001050
bsalomon@google.comffca4002011-02-22 20:34:01 +00001051 GrPathFill fill = kHairLine_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001052
reed@google.com0c219b62011-02-16 21:31:18 +00001053 if (doFill) {
1054 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001055 case SkPath::kWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001056 fill = kWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001057 break;
1058 case SkPath::kEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001059 fill = kEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001060 break;
1061 case SkPath::kInverseWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001062 fill = kInverseWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001063 break;
1064 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001065 fill = kInverseEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001066 break;
1067 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001068 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001069 return;
1070 }
1071 }
1072
reed@google.com07f3ee12011-05-16 17:21:57 +00001073 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001074}
1075
bsalomon@google.comfb309512011-11-30 14:13:48 +00001076namespace {
1077
1078inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1079 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1080 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1081 return tilesX * tilesY;
1082}
1083
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001084inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001085 const SkIRect* srcRectPtr,
1086 int maxTextureSize) {
1087 static const int kSmallTileSize = 1 << 10;
1088 if (maxTextureSize <= kSmallTileSize) {
1089 return maxTextureSize;
1090 }
1091
1092 size_t maxTexTotalTileSize;
1093 size_t smallTotalTileSize;
1094
1095 if (NULL == srcRectPtr) {
1096 int w = bitmap.width();
1097 int h = bitmap.height();
1098 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1099 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1100 } else {
1101 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1102 srcRectPtr->fTop,
1103 srcRectPtr->fRight,
1104 srcRectPtr->fBottom,
1105 maxTextureSize);
1106 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1107 srcRectPtr->fTop,
1108 srcRectPtr->fRight,
1109 srcRectPtr->fBottom,
1110 kSmallTileSize);
1111 }
1112 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1113 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1114
1115 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1116 return kSmallTileSize;
1117 } else {
1118 return maxTextureSize;
1119 }
1120}
1121}
1122
1123bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1124 const GrSamplerState& sampler,
1125 const SkIRect* srcRectPtr,
1126 int* tileSize) const {
1127 SkASSERT(NULL != tileSize);
1128
1129 // if bitmap is explictly texture backed then just use the texture
1130 if (NULL != bitmap.getTexture()) {
1131 return false;
1132 }
1133 // if it's larger than the max texture size, then we have no choice but
1134 // tiling
1135 const int maxTextureSize = fContext->getMaxTextureSize();
1136 if (bitmap.width() > maxTextureSize ||
1137 bitmap.height() > maxTextureSize) {
1138 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1139 return true;
1140 }
1141 // if we are going to have to draw the whole thing, then don't tile
1142 if (NULL == srcRectPtr) {
1143 return false;
1144 }
1145 // if the entire texture is already in our cache then no reason to tile it
1146 if (this->isBitmapInTextureCache(bitmap, sampler)) {
1147 return false;
1148 }
1149
1150 // At this point we know we could do the draw by uploading the entire bitmap
1151 // as a texture. However, if the texture would be large compared to the
1152 // cache size and we don't require most of it for this draw then tile to
1153 // reduce the amount of upload and cache spill.
1154
1155 // assumption here is that sw bitmap size is a good proxy for its size as
1156 // a texture
1157 size_t bmpSize = bitmap.getSize();
1158 size_t cacheSize;
1159 fContext->getTextureCacheLimits(NULL, &cacheSize);
1160 if (bmpSize < cacheSize / 2) {
1161 return false;
1162 }
1163
1164 SkFixed fracUsed =
1165 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1166 (srcRectPtr->height() << 16) / bitmap.height());
1167 if (fracUsed <= SK_FixedHalf) {
1168 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1169 return true;
1170 } else {
1171 return false;
1172 }
1173}
1174
reed@google.comac10a2d2010-12-22 21:39:39 +00001175void SkGpuDevice::drawBitmap(const SkDraw& draw,
1176 const SkBitmap& bitmap,
1177 const SkIRect* srcRectPtr,
1178 const SkMatrix& m,
1179 const SkPaint& paint) {
1180 CHECK_SHOULD_DRAW(draw);
1181
1182 SkIRect srcRect;
1183 if (NULL == srcRectPtr) {
1184 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1185 } else {
1186 srcRect = *srcRectPtr;
1187 }
1188
junov@google.comd935cfb2011-06-27 20:48:23 +00001189 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001190 // Convert the bitmap to a shader so that the rect can be drawn
1191 // through drawRect, which supports mask filters.
1192 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001193 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001194 if (srcRectPtr) {
1195 if (!bitmap.extractSubset(&tmp, srcRect)) {
1196 return; // extraction failed
1197 }
1198 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001199 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001200 }
1201 SkPaint paintWithTexture(paint);
1202 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1203 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001204 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001205 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001206
junov@google.com1d329782011-07-28 20:10:09 +00001207 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001208 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001209 // also affect the behavior of the mask filter.
1210 SkMatrix drawMatrix;
1211 drawMatrix.setConcat(*draw.fMatrix, m);
1212 SkDraw transformedDraw(draw);
1213 transformedDraw.fMatrix = &drawMatrix;
1214
1215 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1216
junov@google.comd935cfb2011-06-27 20:48:23 +00001217 return;
1218 }
1219
bsalomon@google.com5782d712011-01-21 21:03:59 +00001220 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001221 if (!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001222 return;
1223 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001224 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001225 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001226 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001227 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001228 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001229 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001230
bsalomon@google.comfb309512011-11-30 14:13:48 +00001231 int tileSize;
1232 if (!this->shouldTileBitmap(bitmap, *sampler, srcRectPtr, &tileSize)) {
1233 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001234 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001235 return;
1236 }
1237
1238 // undo the translate done by SkCanvas
1239 int DX = SkMax32(0, srcRect.fLeft);
1240 int DY = SkMax32(0, srcRect.fTop);
1241 // compute clip bounds in local coordinates
1242 SkIRect clipRect;
1243 {
1244 SkRect r;
1245 r.set(draw.fClip->getBounds());
1246 SkMatrix matrix, inverse;
1247 matrix.setConcat(*draw.fMatrix, m);
1248 if (!matrix.invert(&inverse)) {
1249 return;
1250 }
1251 inverse.mapRect(&r);
1252 r.roundOut(&clipRect);
1253 // apply the canvas' translate to our local clip
1254 clipRect.offset(DX, DY);
1255 }
1256
bsalomon@google.comfb309512011-11-30 14:13:48 +00001257 int nx = bitmap.width() / tileSize;
1258 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001259 for (int x = 0; x <= nx; x++) {
1260 for (int y = 0; y <= ny; y++) {
1261 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001262 tileR.set(x * tileSize, y * tileSize,
1263 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001264 if (!SkIRect::Intersects(tileR, clipRect)) {
1265 continue;
1266 }
1267
1268 SkIRect srcR = tileR;
1269 if (!srcR.intersect(srcRect)) {
1270 continue;
1271 }
1272
1273 SkBitmap tmpB;
1274 if (bitmap.extractSubset(&tmpB, tileR)) {
1275 // now offset it to make it "local" to our tmp bitmap
1276 srcR.offset(-tileR.fLeft, -tileR.fTop);
1277
1278 SkMatrix tmpM(m);
1279 {
1280 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1281 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1282 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1283 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001284 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001285 }
1286 }
1287 }
1288}
1289
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001290namespace {
1291
1292bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1293 // detect pixel disalignment
1294 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1295 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1296 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1297 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1298 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1299 COLOR_BLEED_TOLERANCE &&
1300 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1301 COLOR_BLEED_TOLERANCE) {
1302 return true;
1303 }
1304 return false;
1305}
1306
1307bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1308 const SkMatrix& m) {
1309 // Only gets called if hasAlignedSamples returned false.
1310 // So we can assume that sampling is axis aligned but not texel aligned.
1311 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
1312 SkRect innerSrcRect(srcRect), innerTransformedRect,
1313 outerTransformedRect(transformedRect);
1314 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1315 m.mapRect(&innerTransformedRect, innerSrcRect);
1316
1317 // The gap between outerTransformedRect and innerTransformedRect
1318 // represents the projection of the source border area, which is
1319 // problematic for color bleeding. We must check whether any
1320 // destination pixels sample the border area.
1321 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1322 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1323 SkIRect outer, inner;
1324 outerTransformedRect.round(&outer);
1325 innerTransformedRect.round(&inner);
1326 // If the inner and outer rects round to the same result, it means the
1327 // border does not overlap any pixel centers. Yay!
1328 return inner != outer;
1329}
1330
1331} // unnamed namespace
1332
reed@google.comac10a2d2010-12-22 21:39:39 +00001333/*
1334 * This is called by drawBitmap(), which has to handle images that may be too
1335 * large to be represented by a single texture.
1336 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001337 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1338 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001339 */
1340void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1341 const SkBitmap& bitmap,
1342 const SkIRect& srcRect,
1343 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001344 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001345 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1346 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001347
reed@google.com9c49bc32011-07-07 13:42:37 +00001348 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001349 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001350 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001351 return;
1352 }
1353
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001354 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001355
1356 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1357 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001358 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001359
1360 GrTexture* texture;
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001361 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001362 if (NULL == texture) {
1363 return;
1364 }
1365
bsalomon@google.com452943d2011-10-31 17:37:14 +00001366 grPaint->setTexture(kBitmapTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001367
reed@google.com20efde72011-05-09 17:00:02 +00001368 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1369 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001370 GrRect paintRect;
bsalomon@google.com91832162012-03-08 19:53:02 +00001371 float wInv = 1.f / bitmap.width();
1372 float hInv = 1.f / bitmap.height();
1373 paintRect.setLTRB(SkFloatToScalar(srcRect.fLeft * wInv),
1374 SkFloatToScalar(srcRect.fTop * hInv),
1375 SkFloatToScalar(srcRect.fRight * wInv),
1376 SkFloatToScalar(srcRect.fBottom * hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001377
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001378 bool needsTextureDomain = false;
1379 if (GrSamplerState::kBilinear_Filter == sampler->getFilter())
1380 {
1381 // Need texture domain if drawing a sub rect.
1382 needsTextureDomain = srcRect.width() < bitmap.width() ||
1383 srcRect.height() < bitmap.height();
1384 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1385 // sampling is axis-aligned
1386 GrRect floatSrcRect, transformedRect;
1387 floatSrcRect.set(srcRect);
1388 SkMatrix srcToDeviceMatrix(m);
1389 srcToDeviceMatrix.postConcat(*draw.fMatrix);
1390 srcToDeviceMatrix.mapRect(&transformedRect, floatSrcRect);
1391
1392 if (hasAlignedSamples(floatSrcRect, transformedRect)) {
1393 // Samples are texel-aligned, so filtering is futile
1394 sampler->setFilter(GrSamplerState::kNearest_Filter);
1395 needsTextureDomain = false;
1396 } else {
1397 needsTextureDomain = needsTextureDomain &&
1398 mayColorBleed(floatSrcRect, transformedRect, m);
1399 }
1400 }
1401 }
1402
1403 GrRect textureDomain = GrRect::MakeEmpty();
1404
1405 if (needsTextureDomain) {
1406 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001407 GrScalar left, top, right, bottom;
1408 if (srcRect.width() > 1) {
1409 GrScalar border = GR_ScalarHalf / bitmap.width();
1410 left = paintRect.left() + border;
1411 right = paintRect.right() - border;
1412 } else {
1413 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1414 }
1415 if (srcRect.height() > 1) {
1416 GrScalar border = GR_ScalarHalf / bitmap.height();
1417 top = paintRect.top() + border;
1418 bottom = paintRect.bottom() - border;
1419 } else {
1420 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1421 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001422 textureDomain.setLTRB(left, top, right, bottom);
junov@google.com6acc9b32011-05-16 18:32:07 +00001423 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001424 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001425
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001426 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001427}
1428
reed@google.com8926b162012-03-23 15:36:36 +00001429static GrTexture* filter_texture(GrContext* context, GrTexture* texture,
1430 SkImageFilter* filter, const GrRect& rect) {
1431 GrAssert(filter);
1432
1433 SkSize blurSize;
1434 SkISize radius;
1435
1436 const GrTextureDesc desc = {
1437 kRenderTarget_GrTextureFlagBit,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001438 SkScalarCeilToInt(rect.width()),
1439 SkScalarCeilToInt(rect.height()),
reed@google.com8926b162012-03-23 15:36:36 +00001440 kRGBA_8888_PM_GrPixelConfig,
bsalomon@google.comb9014f42012-03-30 14:22:41 +00001441 0 // samples
reed@google.com8926b162012-03-23 15:36:36 +00001442 };
1443
1444 if (filter->asABlur(&blurSize)) {
1445 GrAutoScratchTexture temp1, temp2;
1446 texture = context->gaussianBlur(texture, &temp1, &temp2, rect,
1447 blurSize.width(),
1448 blurSize.height());
1449 texture->ref();
1450 } else if (filter->asADilate(&radius)) {
1451 GrAutoScratchTexture temp1(context, desc), temp2(context, desc);
1452 texture = context->applyMorphology(texture, rect,
1453 temp1.texture(), temp2.texture(),
bsalomon@google.comb505a122012-05-31 18:40:36 +00001454 GrContext::kDilate_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001455 radius);
1456 texture->ref();
1457 } else if (filter->asAnErode(&radius)) {
1458 GrAutoScratchTexture temp1(context, desc), temp2(context, desc);
1459 texture = context->applyMorphology(texture, rect,
1460 temp1.texture(), temp2.texture(),
bsalomon@google.comb505a122012-05-31 18:40:36 +00001461 GrContext::kErode_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001462 radius);
1463 texture->ref();
1464 }
1465 return texture;
1466}
1467
reed@google.comac10a2d2010-12-22 21:39:39 +00001468void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1469 int left, int top, const SkPaint& paint) {
1470 CHECK_SHOULD_DRAW(draw);
1471
reed@google.com8926b162012-03-23 15:36:36 +00001472 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001473 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1474 return;
1475 }
1476
reed@google.com76dd2772012-01-05 21:15:07 +00001477 int w = bitmap.width();
1478 int h = bitmap.height();
1479
bsalomon@google.com5782d712011-01-21 21:03:59 +00001480 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001481 if(!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001482 return;
1483 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001484
bsalomon@google.com5782d712011-01-21 21:03:59 +00001485 GrAutoMatrix avm(fContext, GrMatrix::I());
1486
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001487 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001488
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001489 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001490 sampler->reset();
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001491 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001492 grPaint.setTexture(kBitmapTextureIdx, texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001493
reed@google.com8926b162012-03-23 15:36:36 +00001494 SkImageFilter* filter = paint.getImageFilter();
1495 if (NULL != filter) {
1496 GrTexture* filteredTexture = filter_texture(fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001497 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001498 if (filteredTexture) {
1499 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1500 texture = filteredTexture;
1501 filteredTexture->unref();
1502 }
reed@google.com76dd2772012-01-05 21:15:07 +00001503 }
reed@google.com8926b162012-03-23 15:36:36 +00001504
bsalomon@google.com5782d712011-01-21 21:03:59 +00001505 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001506 GrRect::MakeXYWH(GrIntToScalar(left),
1507 GrIntToScalar(top),
1508 GrIntToScalar(w),
1509 GrIntToScalar(h)),
1510 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1511 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001512}
1513
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001514void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
reed@google.comac10a2d2010-12-22 21:39:39 +00001515 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001516 // clear of the source device must occur before CHECK_SHOULD_DRAW
1517 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1518 if (dev->fNeedClear) {
1519 // TODO: could check here whether we really need to draw at all
1520 dev->clear(0x0);
1521 }
1522
reed@google.comac10a2d2010-12-22 21:39:39 +00001523 CHECK_SHOULD_DRAW(draw);
1524
bsalomon@google.com5782d712011-01-21 21:03:59 +00001525 GrPaint grPaint;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001526 if (!dev->bindDeviceAsTexture(&grPaint) ||
bsalomon@google.com84405e02012-03-05 19:57:21 +00001527 !skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001528 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001529 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001530
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001531 GrTexture* devTex = grPaint.getTexture(0);
1532 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001533
reed@google.com8926b162012-03-23 15:36:36 +00001534 SkImageFilter* filter = paint.getImageFilter();
1535 if (NULL != filter) {
robertphillips@google.com8637a362012-04-10 18:32:35 +00001536 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
1537 SkIntToScalar(devTex->height()));
reed@google.com8926b162012-03-23 15:36:36 +00001538 GrTexture* filteredTexture = filter_texture(fContext, devTex, filter,
1539 rect);
1540 if (filteredTexture) {
1541 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1542 devTex = filteredTexture;
1543 filteredTexture->unref();
1544 }
1545 }
1546
bsalomon@google.com5782d712011-01-21 21:03:59 +00001547 const SkBitmap& bm = dev->accessBitmap(false);
1548 int w = bm.width();
1549 int h = bm.height();
1550
1551 GrAutoMatrix avm(fContext, GrMatrix::I());
1552
bsalomon@google.com97912912011-12-06 16:30:36 +00001553 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001554
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001555 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1556 GrIntToScalar(y),
1557 GrIntToScalar(w),
1558 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001559
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001560 // The device being drawn may not fill up its texture (saveLayer uses
1561 // the approximate ).
1562 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1563 GR_Scalar1 * h / devTex->height());
1564
1565 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001566}
1567
reed@google.com8926b162012-03-23 15:36:36 +00001568bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
reed@google.com76dd2772012-01-05 21:15:07 +00001569 SkSize size;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001570 SkISize radius;
1571 if (!filter->asABlur(&size) && !filter->asADilate(&radius) && !filter->asAnErode(&radius)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001572 return false;
1573 }
reed@google.com8926b162012-03-23 15:36:36 +00001574 return true;
1575}
1576
1577bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1578 const SkMatrix& ctm,
1579 SkBitmap* result, SkIPoint* offset) {
1580 // want explicitly our impl, so guard against a subclass of us overriding it
1581 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001582 return false;
1583 }
reed@google.com8926b162012-03-23 15:36:36 +00001584
1585 SkAutoLockPixels alp(src, !src.getTexture());
1586 if (!src.getTexture() && !src.readyToDraw()) {
1587 return false;
1588 }
1589
1590 GrPaint paint;
1591 paint.reset();
1592
1593 GrSamplerState* sampler = paint.textureSampler(kBitmapTextureIdx);
1594
1595 GrTexture* texture;
1596 SkAutoCachedTexture act(this, src, sampler, &texture);
1597
1598 result->setConfig(src.config(), src.width(), src.height());
robertphillips@google.com8637a362012-04-10 18:32:35 +00001599 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
1600 SkIntToScalar(src.height()));
reed@google.com8926b162012-03-23 15:36:36 +00001601 GrTexture* resultTexture = filter_texture(fContext, texture, filter, rect);
1602 if (resultTexture) {
1603 result->setPixelRef(new SkGrTexturePixelRef(resultTexture))->unref();
1604 resultTexture->unref();
1605 }
reed@google.com76dd2772012-01-05 21:15:07 +00001606 return true;
1607}
1608
reed@google.comac10a2d2010-12-22 21:39:39 +00001609///////////////////////////////////////////////////////////////////////////////
1610
1611// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001612static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1613 kTriangles_PrimitiveType,
1614 kTriangleStrip_PrimitiveType,
1615 kTriangleFan_PrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001616};
1617
1618void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1619 int vertexCount, const SkPoint vertices[],
1620 const SkPoint texs[], const SkColor colors[],
1621 SkXfermode* xmode,
1622 const uint16_t indices[], int indexCount,
1623 const SkPaint& paint) {
1624 CHECK_SHOULD_DRAW(draw);
1625
bsalomon@google.com5782d712011-01-21 21:03:59 +00001626 GrPaint grPaint;
1627 SkAutoCachedTexture act;
1628 // we ignore the shader if texs is null.
1629 if (NULL == texs) {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001630 if (!skPaint2GrPaintNoShader(paint,
1631 false,
1632 NULL == colors,
1633 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001634 return;
1635 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001636 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001637 if (!skPaint2GrPaintShader(this,
1638 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001639 NULL == colors,
1640 &act,
1641 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001642 return;
1643 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001644 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001645
1646 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001647 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001648 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1649#if 0
1650 return
1651#endif
1652 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001653 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001654
bsalomon@google.com498776a2011-08-16 19:20:44 +00001655 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1656 if (NULL != colors) {
1657 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001658 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001659 for (int i = 0; i < vertexCount; ++i) {
1660 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1661 }
1662 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001663 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001664 fContext->drawVertices(grPaint,
1665 gVertexMode2PrimitiveType[vmode],
1666 vertexCount,
1667 (GrPoint*) vertices,
1668 (GrPoint*) texs,
1669 colors,
1670 indices,
1671 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001672}
1673
1674///////////////////////////////////////////////////////////////////////////////
1675
1676static void GlyphCacheAuxProc(void* data) {
1677 delete (GrFontScaler*)data;
1678}
1679
1680static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1681 void* auxData;
1682 GrFontScaler* scaler = NULL;
1683 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1684 scaler = (GrFontScaler*)auxData;
1685 }
1686 if (NULL == scaler) {
1687 scaler = new SkGrFontScaler(cache);
1688 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1689 }
1690 return scaler;
1691}
1692
1693static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1694 SkFixed fx, SkFixed fy,
1695 const SkGlyph& glyph) {
1696 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1697
bungeman@google.com15865a72012-01-11 16:28:04 +00001698 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001699
1700 if (NULL == procs->fFontScaler) {
1701 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1702 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001703
bungeman@google.com15865a72012-01-11 16:28:04 +00001704 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1705 glyph.getSubXFixed(),
1706 glyph.getSubYFixed()),
1707 SkFixedFloorToFixed(fx),
1708 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001709 procs->fFontScaler);
1710}
1711
bsalomon@google.com5782d712011-01-21 21:03:59 +00001712SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001713
1714 // deferred allocation
1715 if (NULL == fDrawProcs) {
1716 fDrawProcs = new GrSkDrawProcs;
1717 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1718 fDrawProcs->fContext = fContext;
1719 }
1720
1721 // init our (and GL's) state
1722 fDrawProcs->fTextContext = context;
1723 fDrawProcs->fFontScaler = NULL;
1724 return fDrawProcs;
1725}
1726
1727void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1728 size_t byteLength, SkScalar x, SkScalar y,
1729 const SkPaint& paint) {
1730 CHECK_SHOULD_DRAW(draw);
1731
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001732 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001733 // this guy will just call our drawPath()
1734 draw.drawText((const char*)text, byteLength, x, y, paint);
1735 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001736 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001737
1738 GrPaint grPaint;
1739 SkAutoCachedTexture act;
1740
bsalomon@google.com84405e02012-03-05 19:57:21 +00001741 if (!skPaint2GrPaintShader(this,
1742 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001743 true,
1744 &act,
1745 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001746 return;
1747 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001748 GrTextContext::AutoFinish txtCtxAF(this->getTextContext(), fContext,
1749 grPaint, draw.fExtMatrix);
1750 myDraw.fProcs = this->initDrawForText(txtCtxAF.getTextContext());
reed@google.comac10a2d2010-12-22 21:39:39 +00001751 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1752 }
1753}
1754
1755void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1756 size_t byteLength, const SkScalar pos[],
1757 SkScalar constY, int scalarsPerPos,
1758 const SkPaint& paint) {
1759 CHECK_SHOULD_DRAW(draw);
1760
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001761 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001762 // this guy will just call our drawPath()
1763 draw.drawPosText((const char*)text, byteLength, pos, constY,
1764 scalarsPerPos, paint);
1765 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001766 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001767
1768 GrPaint grPaint;
1769 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001770 if (!skPaint2GrPaintShader(this,
1771 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001772 true,
1773 &act,
1774 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001775 return;
1776 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001777 GrTextContext::AutoFinish txtCtxAF(this->getTextContext(), fContext,
1778 grPaint, draw.fExtMatrix);
1779 myDraw.fProcs = this->initDrawForText(txtCtxAF.getTextContext());
reed@google.comac10a2d2010-12-22 21:39:39 +00001780 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1781 scalarsPerPos, paint);
1782 }
1783}
1784
1785void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1786 size_t len, const SkPath& path,
1787 const SkMatrix* m, const SkPaint& paint) {
1788 CHECK_SHOULD_DRAW(draw);
1789
1790 SkASSERT(draw.fDevice == this);
1791 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1792}
1793
1794///////////////////////////////////////////////////////////////////////////////
1795
reed@google.comf67e4cf2011-03-15 20:56:58 +00001796bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1797 if (!paint.isLCDRenderText()) {
1798 // we're cool with the paint as is
1799 return false;
1800 }
1801
1802 if (paint.getShader() ||
1803 paint.getXfermode() || // unless its srcover
1804 paint.getMaskFilter() ||
1805 paint.getRasterizer() ||
1806 paint.getColorFilter() ||
1807 paint.getPathEffect() ||
1808 paint.isFakeBoldText() ||
1809 paint.getStyle() != SkPaint::kFill_Style) {
1810 // turn off lcd
1811 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1812 flags->fHinting = paint.getHinting();
1813 return true;
1814 }
1815 // we're cool with the paint as is
1816 return false;
1817}
1818
reed@google.com75d939b2011-12-07 15:07:23 +00001819void SkGpuDevice::flush() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001820 DO_DEFERRED_CLEAR;
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001821 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001822}
1823
reed@google.comf67e4cf2011-03-15 20:56:58 +00001824///////////////////////////////////////////////////////////////////////////////
1825
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001826SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(
1827 const SkBitmap& bitmap,
1828 const GrSamplerState* sampler) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001829 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001830 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001831
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001832 if (!bitmap.isVolatile()) {
1833 GrContext::TextureKey key = bitmap.getGenerationID();
1834 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001835
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001836 entry = ctx->findAndLockTexture(key, bitmap.width(),
1837 bitmap.height(), sampler);
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001838 if (NULL == entry.texture()) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001839 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
1840 bitmap);
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001841 }
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001842 } else {
1843 entry = sk_gr_create_bitmap_texture(ctx, gUNCACHED_KEY,
1844 sampler, bitmap);
1845 }
1846 if (NULL == entry.texture()) {
1847 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1848 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001849 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001850 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001851}
1852
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001853void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1854 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001855}
1856
bsalomon@google.comfb309512011-11-30 14:13:48 +00001857bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
1858 const GrSamplerState& sampler) const {
1859 GrContext::TextureKey key = bitmap.getGenerationID();
1860 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
1861 return this->context()->isTextureInCache(key, bitmap.width(),
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001862 bitmap.height(), &sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001863
1864}
1865
1866
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001867SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1868 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001869 bool isOpaque,
1870 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001871 GrTextureDesc desc;
1872 desc.fConfig = fRenderTarget->config();
1873 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1874 desc.fWidth = width;
1875 desc.fHeight = height;
1876 desc.fSampleCnt = fRenderTarget->numSamples();
1877
1878 GrContext::TextureCacheEntry cacheEntry;
1879 GrTexture* texture;
1880 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001881 // Skia's convention is to only clear a device if it is non-opaque.
1882 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001883
1884#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1885 // layers are never draw in repeat modes, so we can request an approx
1886 // match and ignore any padding.
1887 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1888 GrContext::kApprox_ScratchTexMatch :
1889 GrContext::kExact_ScratchTexMatch;
1890 cacheEntry = fContext->lockScratchTexture(desc, matchType);
1891 texture = cacheEntry.texture();
1892#else
1893 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1894 texture = tunref.get();
1895#endif
1896 if (texture) {
1897 return SkNEW_ARGS(SkGpuDevice,(fContext,
1898 texture,
1899 cacheEntry,
1900 needClear));
1901 } else {
1902 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1903 width, height);
1904 return NULL;
1905 }
1906}
1907
1908SkGpuDevice::SkGpuDevice(GrContext* context,
1909 GrTexture* texture,
1910 TexCache cacheEntry,
1911 bool needClear)
1912 : SkDevice(make_bitmap(context, texture->asRenderTarget())) {
1913 GrAssert(texture && texture->asRenderTarget());
1914 GrAssert(NULL == cacheEntry.texture() || texture == cacheEntry.texture());
1915 this->initFromRenderTarget(context, texture->asRenderTarget());
1916 fCache = cacheEntry;
1917 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001918}
1919
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001920GrTextContext* SkGpuDevice::getTextContext() {
1921 if (NULL == fTextContext) {
1922 fTextContext = new GrDefaultTextContext();
1923 }
1924 return fTextContext;
1925}