blob: 056c5d817953e7bda4555c73089cbe103862a568 [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: {
tomhudson@google.com83e5eb82012-06-04 19:58:30 +0000543 sampler->setCustomStage(new GrRadialGradient())->unref();
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000544 } break;
545 case SkShader::kSweep_BitmapType: {
tomhudson@google.com83e5eb82012-06-04 19:58:30 +0000546 sampler->setCustomStage(new GrSweepGradient())->unref();
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000547 } break;
548 case SkShader::kTwoPointRadial_BitmapType: {
tomhudson@google.com83e5eb82012-06-04 19:58:30 +0000549 sampler->setCustomStage(new
550 GrRadial2Gradient(twoPointParams[0],
551 twoPointParams[1],
552 twoPointParams[2] < 0))->unref();
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000553 } break;
554 default:
555 break;
556 }
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000557 if (skPaint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000558 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000559 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000560 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000561 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000562 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
563 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000564
bsalomon@google.com84405e02012-03-05 19:57:21 +0000565 GrTexture* texture = act->set(dev, bitmap, sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000566 if (NULL == texture) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000567 SkDebugf("Couldn't convert bitmap to texture.\n");
568 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000569 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000570 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000571
572 // since our texture coords will be in local space, we wack the texture
573 // matrix to map them back into 0...1 before we load it
574 SkMatrix localM;
575 if (shader->getLocalMatrix(&localM)) {
576 SkMatrix inverse;
577 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000578 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000579 }
580 }
581 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000582 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
583 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000584 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000585 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000586 GrScalar s = SkFloatToScalar(1.f / bitmap.width());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000587 matrix->postScale(s, s);
reed@google.comac10a2d2010-12-22 21:39:39 +0000588 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000589
590 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000591}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000592}
reed@google.comac10a2d2010-12-22 21:39:39 +0000593
594///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000595
bsalomon@google.com398109c2011-04-14 18:40:27 +0000596void SkGpuDevice::clear(SkColor color) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000597 fContext->setRenderTarget(fRenderTarget);
bsalomon@google.com31a58402011-04-27 21:00:02 +0000598 fContext->clear(NULL, color);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000599}
600
reed@google.comac10a2d2010-12-22 21:39:39 +0000601void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
602 CHECK_SHOULD_DRAW(draw);
603
bsalomon@google.com5782d712011-01-21 21:03:59 +0000604 GrPaint grPaint;
605 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000606 if (!skPaint2GrPaintShader(this,
607 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000608 true,
609 &act,
610 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000611 return;
612 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000613
614 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000615}
616
617// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000618static const GrPrimitiveType gPointMode2PrimtiveType[] = {
619 kPoints_PrimitiveType,
620 kLines_PrimitiveType,
621 kLineStrip_PrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000622};
623
624void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000625 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000626 CHECK_SHOULD_DRAW(draw);
627
628 SkScalar width = paint.getStrokeWidth();
629 if (width < 0) {
630 return;
631 }
632
633 // we only handle hairlines here, else we let the SkDraw call our drawPath()
634 if (width > 0) {
635 draw.drawPoints(mode, count, pts, paint, true);
636 return;
637 }
638
bsalomon@google.com5782d712011-01-21 21:03:59 +0000639 GrPaint grPaint;
640 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000641 if (!skPaint2GrPaintShader(this,
642 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000643 true,
644 &act,
645 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000646 return;
647 }
648
bsalomon@google.com5782d712011-01-21 21:03:59 +0000649 fContext->drawVertices(grPaint,
650 gPointMode2PrimtiveType[mode],
651 count,
652 (GrPoint*)pts,
653 NULL,
654 NULL,
655 NULL,
656 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000657}
658
reed@google.comc9aa5872011-04-05 21:05:37 +0000659///////////////////////////////////////////////////////////////////////////////
660
reed@google.comac10a2d2010-12-22 21:39:39 +0000661void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
662 const SkPaint& paint) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000663 CHECK_SHOULD_DRAW(draw);
664
bungeman@google.com79bd8772011-07-18 15:34:08 +0000665 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000666 SkScalar width = paint.getStrokeWidth();
667
668 /*
669 We have special code for hairline strokes, miter-strokes, and fills.
670 Anything else we just call our path code.
671 */
672 bool usePath = doStroke && width > 0 &&
673 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000674 // another two reasons we might need to call drawPath...
675 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000676 usePath = true;
677 }
reed@google.com67db6642011-05-26 11:46:35 +0000678 // until we aa rotated rects...
679 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
680 usePath = true;
681 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000682 // small miter limit means right angles show bevel...
683 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
684 paint.getStrokeMiter() < SK_ScalarSqrt2)
685 {
686 usePath = true;
687 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000688 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000689 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
690 usePath = true;
691 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000692
693 if (usePath) {
694 SkPath path;
695 path.addRect(rect);
696 this->drawPath(draw, path, paint, NULL, true);
697 return;
698 }
699
700 GrPaint grPaint;
701 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000702 if (!skPaint2GrPaintShader(this,
703 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000704 true,
705 &act,
706 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000707 return;
708 }
reed@google.com20efde72011-05-09 17:00:02 +0000709 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000710}
711
reed@google.com69302852011-02-16 18:08:07 +0000712#include "SkMaskFilter.h"
713#include "SkBounder.h"
714
bsalomon@google.com85003222012-03-28 14:44:37 +0000715///////////////////////////////////////////////////////////////////////////////
716
717// helpers for applying mask filters
718namespace {
719
720GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000721 switch (fillType) {
722 case SkPath::kWinding_FillType:
723 return kWinding_PathFill;
724 case SkPath::kEvenOdd_FillType:
725 return kEvenOdd_PathFill;
726 case SkPath::kInverseWinding_FillType:
727 return kInverseWinding_PathFill;
728 case SkPath::kInverseEvenOdd_FillType:
729 return kInverseEvenOdd_PathFill;
730 default:
731 SkDebugf("Unsupported path fill type\n");
732 return kHairLine_PathFill;
733 }
734}
735
bsalomon@google.com85003222012-03-28 14:44:37 +0000736// We prefer to blur small rect with small radius via CPU.
737#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
738#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
739inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
740 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
741 rect.height() <= MIN_GPU_BLUR_SIZE &&
742 radius <= MIN_GPU_BLUR_RADIUS) {
743 return true;
744 }
745 return false;
746}
747
748bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
749 SkMaskFilter* filter, const SkMatrix& matrix,
750 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000751 GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000752#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000753 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000754#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000755 SkMaskFilter::BlurInfo info;
756 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000757 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000758 return false;
759 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000760 SkScalar radius = info.fIgnoreTransform ? info.fRadius
761 : matrix.mapRadius(info.fRadius);
762 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000763 if (radius <= 0) {
764 return false;
765 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000766
767 SkRect srcRect = path.getBounds();
768 if (shouldDrawBlurWithCPU(srcRect, radius)) {
769 return false;
770 }
771
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000772 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000773 float sigma3 = sigma * 3.0f;
774
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000775 SkRect clipRect;
776 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000777
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000778 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000779 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
780 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000781 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000782 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000783 SkIRect finalIRect;
784 finalRect.roundOut(&finalIRect);
785 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000786 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000787 }
788 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000789 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000790 }
791 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000792 srcRect.offset(offset);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000793 GrTextureDesc desc = {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000794 kRenderTarget_GrTextureFlagBit,
bungeman@google.comf8aa18c2012-03-19 21:04:52 +0000795 SkScalarCeilToInt(srcRect.width()),
796 SkScalarCeilToInt(srcRect.height()),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000797 // We actually only need A8, but it often isn't supported as a
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000798 // render target so default to RGBA_8888
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000799 kRGBA_8888_PM_GrPixelConfig,
bsalomon@google.comb9014f42012-03-30 14:22:41 +0000800 0 // samples
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000801 };
802
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000803 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
804 desc.fConfig = kAlpha_8_GrPixelConfig;
805 }
806
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000807 GrAutoScratchTexture pathEntry(context, desc);
808 GrTexture* pathTexture = pathEntry.texture();
809 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000810 return false;
811 }
812 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000813 // Once this code moves into GrContext, this should be changed to use
814 // an AutoClipRestore.
815 GrClip oldClip = context->getClip();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000816 context->setRenderTarget(pathTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000817 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000818 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000819 GrPaint tempPaint;
820 tempPaint.reset();
821
822 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000823 tempPaint.fAntiAlias = grp->fAntiAlias;
824 if (tempPaint.fAntiAlias) {
825 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
826 // blend coeff of zero requires dual source blending support in order
827 // to properly blend partially covered pixels. This means the AA
828 // code path may not be taken. So we use a dst blend coeff of ISA. We
829 // could special case AA draws to a dst surface with known alpha=0 to
830 // use a zero dst coeff when dual source blending isn't available.
831 tempPaint.fSrcBlendCoeff = kOne_BlendCoeff;
832 tempPaint.fDstBlendCoeff = kISC_BlendCoeff;
833 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000834 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000835 context->drawPath(tempPaint, path, pathFillType, &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000836
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000837 GrAutoScratchTexture temp1, temp2;
838 // If we're doing a normal blur, we can clobber the pathTexture in the
839 // gaussianBlur. Otherwise, we need to save it for later compositing.
840 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000841 GrTexture* blurTexture = context->gaussianBlur(pathTexture,
842 &temp1,
843 isNormalBlur ? NULL : &temp2,
844 srcRect, sigma, sigma);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000845
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000846 if (!isNormalBlur) {
847 GrPaint paint;
848 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000849 paint.textureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000850 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
851 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000852 // Blend pathTexture over blurTexture.
853 context->setRenderTarget(blurTexture->asRenderTarget());
854 paint.setTexture(0, pathTexture);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000855 if (SkMaskFilter::kInner_BlurType == blurType) {
856 // inner: dst = dst * src
857 paint.fSrcBlendCoeff = kDC_BlendCoeff;
858 paint.fDstBlendCoeff = kZero_BlendCoeff;
859 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
860 // solid: dst = src + dst - src * dst
861 // = (1 - dst) * src + 1 * dst
862 paint.fSrcBlendCoeff = kIDC_BlendCoeff;
863 paint.fDstBlendCoeff = kOne_BlendCoeff;
864 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
865 // outer: dst = dst * (1 - src)
866 // = 0 * src + (1 - src) * dst
867 paint.fSrcBlendCoeff = kZero_BlendCoeff;
868 paint.fDstBlendCoeff = kISC_BlendCoeff;
869 }
870 context->drawRect(paint, srcRect);
871 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000872 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000873 context->setClip(oldClip);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000874
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000875 if (grp->hasTextureOrMask()) {
876 GrMatrix inverse;
877 if (!matrix.invert(&inverse)) {
878 return false;
879 }
880 grp->preConcatActiveSamplerMatrices(inverse);
881 }
882
883 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
884 // we assume the last mask index is available for use
885 GrAssert(NULL == grp->getMask(MASK_IDX));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000886 grp->setMask(MASK_IDX, blurTexture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000887 grp->maskSampler(MASK_IDX)->reset();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000888
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000889 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
890 -finalRect.fTop);
891 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
892 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000893 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000894 return true;
895}
896
bsalomon@google.com85003222012-03-28 14:44:37 +0000897bool drawWithMaskFilter(GrContext* context, const SkPath& path,
898 SkMaskFilter* filter, const SkMatrix& matrix,
899 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000900 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000901 SkMask srcM, dstM;
902
903 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000904 SkMask::kComputeBoundsAndRenderImage_CreateMode,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000905 style)) {
reed@google.com69302852011-02-16 18:08:07 +0000906 return false;
907 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000908 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000909
910 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
911 return false;
912 }
913 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000914 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000915
916 if (clip.quickReject(dstM.fBounds)) {
917 return false;
918 }
919 if (bounder && !bounder->doIRect(dstM.fBounds)) {
920 return false;
921 }
922
923 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
924 // the current clip (and identity matrix) and grpaint settings
925
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000926 // used to compute inverse view, if necessary
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000927 GrMatrix ivm = matrix;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000928
reed@google.com0c219b62011-02-16 21:31:18 +0000929 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +0000930
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000931 const GrTextureDesc desc = {
932 kNone_GrTextureFlags,
reed@google.com69302852011-02-16 18:08:07 +0000933 dstM.fBounds.width(),
934 dstM.fBounds.height(),
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000935 kAlpha_8_GrPixelConfig,
bsalomon@google.comb9014f42012-03-30 14:22:41 +0000936 0, // samples
reed@google.com69302852011-02-16 18:08:07 +0000937 };
938
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000939 GrAutoScratchTexture ast(context, desc);
940 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000941
reed@google.com69302852011-02-16 18:08:07 +0000942 if (NULL == texture) {
943 return false;
944 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000945 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000946 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000947
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000948 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
949 grp->preConcatActiveSamplerMatrices(ivm);
950 }
951
952 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
953 // we assume the last mask index is available for use
954 GrAssert(NULL == grp->getMask(MASK_IDX));
955 grp->setMask(MASK_IDX, texture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000956 grp->maskSampler(MASK_IDX)->reset();
reed@google.com69302852011-02-16 18:08:07 +0000957
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000958 GrRect d;
959 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +0000960 GrIntToScalar(dstM.fBounds.fTop),
961 GrIntToScalar(dstM.fBounds.fRight),
962 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000963
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000964 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
965 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
966 -dstM.fBounds.fTop*SK_Scalar1);
967 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000968 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000969 return true;
970}
reed@google.com69302852011-02-16 18:08:07 +0000971
bsalomon@google.com85003222012-03-28 14:44:37 +0000972}
973
974///////////////////////////////////////////////////////////////////////////////
975
reed@google.com0c219b62011-02-16 21:31:18 +0000976void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000977 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000978 bool pathIsMutable) {
979 CHECK_SHOULD_DRAW(draw);
980
reed@google.comfe626382011-09-21 13:50:35 +0000981 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000982
bsalomon@google.com5782d712011-01-21 21:03:59 +0000983 GrPaint grPaint;
984 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000985 if (!skPaint2GrPaintShader(this,
986 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000987 true,
988 &act,
989 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000990 return;
991 }
992
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000993 // can we cheat, and threat a thin stroke as a hairline w/ coverage
994 // if we can, we draw lots faster (raster device does this same test)
995 SkScalar hairlineCoverage;
996 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
997 doFill = false;
998 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
999 grPaint.fCoverage);
1000 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001001
reed@google.comfe626382011-09-21 13:50:35 +00001002 // If we have a prematrix, apply it to the path, optimizing for the case
1003 // where the original path can in fact be modified in place (even though
1004 // its parameter type is const).
1005 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1006 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001007
1008 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001009 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001010
reed@google.come3445642011-02-16 23:20:39 +00001011 if (!pathIsMutable) {
1012 result = &tmpPath;
1013 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001014 }
reed@google.come3445642011-02-16 23:20:39 +00001015 // should I push prePathMatrix on our MV stack temporarily, instead
1016 // of applying it here? See SkDraw.cpp
1017 pathPtr->transform(*prePathMatrix, result);
1018 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001019 }
reed@google.com0c219b62011-02-16 21:31:18 +00001020 // at this point we're done with prePathMatrix
1021 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001022
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001023 if (paint.getPathEffect() ||
1024 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001025 // it is safe to use tmpPath here, even if we already used it for the
1026 // prepathmatrix, since getFillPath can take the same object for its
1027 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001028 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001029 pathPtr = &tmpPath;
1030 }
1031
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001032 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001033 // avoid possibly allocating a new path in transform if we can
1034 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1035
1036 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001037 pathPtr->transform(*draw.fMatrix, devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001038 GrPathFill pathFillType = doFill ?
1039 skToGrFillType(devPathPtr->getFillType()) : kHairLine_PathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001040 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001041 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001042 &grPaint, pathFillType)) {
1043 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
1044 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001045 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001046 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001047 &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001048 }
reed@google.com69302852011-02-16 18:08:07 +00001049 return;
1050 }
reed@google.com69302852011-02-16 18:08:07 +00001051
bsalomon@google.comffca4002011-02-22 20:34:01 +00001052 GrPathFill fill = kHairLine_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001053
reed@google.com0c219b62011-02-16 21:31:18 +00001054 if (doFill) {
1055 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001056 case SkPath::kWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001057 fill = kWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001058 break;
1059 case SkPath::kEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001060 fill = kEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001061 break;
1062 case SkPath::kInverseWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001063 fill = kInverseWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001064 break;
1065 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001066 fill = kInverseEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001067 break;
1068 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001069 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001070 return;
1071 }
1072 }
1073
reed@google.com07f3ee12011-05-16 17:21:57 +00001074 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001075}
1076
bsalomon@google.comfb309512011-11-30 14:13:48 +00001077namespace {
1078
1079inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1080 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1081 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1082 return tilesX * tilesY;
1083}
1084
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001085inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001086 const SkIRect* srcRectPtr,
1087 int maxTextureSize) {
1088 static const int kSmallTileSize = 1 << 10;
1089 if (maxTextureSize <= kSmallTileSize) {
1090 return maxTextureSize;
1091 }
1092
1093 size_t maxTexTotalTileSize;
1094 size_t smallTotalTileSize;
1095
1096 if (NULL == srcRectPtr) {
1097 int w = bitmap.width();
1098 int h = bitmap.height();
1099 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1100 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1101 } else {
1102 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1103 srcRectPtr->fTop,
1104 srcRectPtr->fRight,
1105 srcRectPtr->fBottom,
1106 maxTextureSize);
1107 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1108 srcRectPtr->fTop,
1109 srcRectPtr->fRight,
1110 srcRectPtr->fBottom,
1111 kSmallTileSize);
1112 }
1113 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1114 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1115
1116 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1117 return kSmallTileSize;
1118 } else {
1119 return maxTextureSize;
1120 }
1121}
1122}
1123
1124bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1125 const GrSamplerState& sampler,
1126 const SkIRect* srcRectPtr,
1127 int* tileSize) const {
1128 SkASSERT(NULL != tileSize);
1129
1130 // if bitmap is explictly texture backed then just use the texture
1131 if (NULL != bitmap.getTexture()) {
1132 return false;
1133 }
1134 // if it's larger than the max texture size, then we have no choice but
1135 // tiling
1136 const int maxTextureSize = fContext->getMaxTextureSize();
1137 if (bitmap.width() > maxTextureSize ||
1138 bitmap.height() > maxTextureSize) {
1139 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1140 return true;
1141 }
1142 // if we are going to have to draw the whole thing, then don't tile
1143 if (NULL == srcRectPtr) {
1144 return false;
1145 }
1146 // if the entire texture is already in our cache then no reason to tile it
1147 if (this->isBitmapInTextureCache(bitmap, sampler)) {
1148 return false;
1149 }
1150
1151 // At this point we know we could do the draw by uploading the entire bitmap
1152 // as a texture. However, if the texture would be large compared to the
1153 // cache size and we don't require most of it for this draw then tile to
1154 // reduce the amount of upload and cache spill.
1155
1156 // assumption here is that sw bitmap size is a good proxy for its size as
1157 // a texture
1158 size_t bmpSize = bitmap.getSize();
1159 size_t cacheSize;
1160 fContext->getTextureCacheLimits(NULL, &cacheSize);
1161 if (bmpSize < cacheSize / 2) {
1162 return false;
1163 }
1164
1165 SkFixed fracUsed =
1166 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1167 (srcRectPtr->height() << 16) / bitmap.height());
1168 if (fracUsed <= SK_FixedHalf) {
1169 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1170 return true;
1171 } else {
1172 return false;
1173 }
1174}
1175
reed@google.comac10a2d2010-12-22 21:39:39 +00001176void SkGpuDevice::drawBitmap(const SkDraw& draw,
1177 const SkBitmap& bitmap,
1178 const SkIRect* srcRectPtr,
1179 const SkMatrix& m,
1180 const SkPaint& paint) {
1181 CHECK_SHOULD_DRAW(draw);
1182
1183 SkIRect srcRect;
1184 if (NULL == srcRectPtr) {
1185 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1186 } else {
1187 srcRect = *srcRectPtr;
1188 }
1189
junov@google.comd935cfb2011-06-27 20:48:23 +00001190 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001191 // Convert the bitmap to a shader so that the rect can be drawn
1192 // through drawRect, which supports mask filters.
1193 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001194 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001195 if (srcRectPtr) {
1196 if (!bitmap.extractSubset(&tmp, srcRect)) {
1197 return; // extraction failed
1198 }
1199 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001200 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001201 }
1202 SkPaint paintWithTexture(paint);
1203 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1204 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001205 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001206 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001207
junov@google.com1d329782011-07-28 20:10:09 +00001208 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001209 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001210 // also affect the behavior of the mask filter.
1211 SkMatrix drawMatrix;
1212 drawMatrix.setConcat(*draw.fMatrix, m);
1213 SkDraw transformedDraw(draw);
1214 transformedDraw.fMatrix = &drawMatrix;
1215
1216 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1217
junov@google.comd935cfb2011-06-27 20:48:23 +00001218 return;
1219 }
1220
bsalomon@google.com5782d712011-01-21 21:03:59 +00001221 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001222 if (!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001223 return;
1224 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001225 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001226 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001227 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001228 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001229 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001230 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001231
bsalomon@google.comfb309512011-11-30 14:13:48 +00001232 int tileSize;
1233 if (!this->shouldTileBitmap(bitmap, *sampler, srcRectPtr, &tileSize)) {
1234 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001235 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001236 return;
1237 }
1238
1239 // undo the translate done by SkCanvas
1240 int DX = SkMax32(0, srcRect.fLeft);
1241 int DY = SkMax32(0, srcRect.fTop);
1242 // compute clip bounds in local coordinates
1243 SkIRect clipRect;
1244 {
1245 SkRect r;
1246 r.set(draw.fClip->getBounds());
1247 SkMatrix matrix, inverse;
1248 matrix.setConcat(*draw.fMatrix, m);
1249 if (!matrix.invert(&inverse)) {
1250 return;
1251 }
1252 inverse.mapRect(&r);
1253 r.roundOut(&clipRect);
1254 // apply the canvas' translate to our local clip
1255 clipRect.offset(DX, DY);
1256 }
1257
bsalomon@google.comfb309512011-11-30 14:13:48 +00001258 int nx = bitmap.width() / tileSize;
1259 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001260 for (int x = 0; x <= nx; x++) {
1261 for (int y = 0; y <= ny; y++) {
1262 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001263 tileR.set(x * tileSize, y * tileSize,
1264 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001265 if (!SkIRect::Intersects(tileR, clipRect)) {
1266 continue;
1267 }
1268
1269 SkIRect srcR = tileR;
1270 if (!srcR.intersect(srcRect)) {
1271 continue;
1272 }
1273
1274 SkBitmap tmpB;
1275 if (bitmap.extractSubset(&tmpB, tileR)) {
1276 // now offset it to make it "local" to our tmp bitmap
1277 srcR.offset(-tileR.fLeft, -tileR.fTop);
1278
1279 SkMatrix tmpM(m);
1280 {
1281 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1282 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1283 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1284 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001285 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001286 }
1287 }
1288 }
1289}
1290
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001291namespace {
1292
1293bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1294 // detect pixel disalignment
1295 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1296 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1297 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1298 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1299 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1300 COLOR_BLEED_TOLERANCE &&
1301 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1302 COLOR_BLEED_TOLERANCE) {
1303 return true;
1304 }
1305 return false;
1306}
1307
1308bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1309 const SkMatrix& m) {
1310 // Only gets called if hasAlignedSamples returned false.
1311 // So we can assume that sampling is axis aligned but not texel aligned.
1312 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
1313 SkRect innerSrcRect(srcRect), innerTransformedRect,
1314 outerTransformedRect(transformedRect);
1315 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1316 m.mapRect(&innerTransformedRect, innerSrcRect);
1317
1318 // The gap between outerTransformedRect and innerTransformedRect
1319 // represents the projection of the source border area, which is
1320 // problematic for color bleeding. We must check whether any
1321 // destination pixels sample the border area.
1322 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1323 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1324 SkIRect outer, inner;
1325 outerTransformedRect.round(&outer);
1326 innerTransformedRect.round(&inner);
1327 // If the inner and outer rects round to the same result, it means the
1328 // border does not overlap any pixel centers. Yay!
1329 return inner != outer;
1330}
1331
1332} // unnamed namespace
1333
reed@google.comac10a2d2010-12-22 21:39:39 +00001334/*
1335 * This is called by drawBitmap(), which has to handle images that may be too
1336 * large to be represented by a single texture.
1337 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001338 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1339 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001340 */
1341void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1342 const SkBitmap& bitmap,
1343 const SkIRect& srcRect,
1344 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001345 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001346 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1347 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001348
reed@google.com9c49bc32011-07-07 13:42:37 +00001349 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001350 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001351 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001352 return;
1353 }
1354
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001355 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001356
1357 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1358 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001359 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001360
1361 GrTexture* texture;
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001362 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001363 if (NULL == texture) {
1364 return;
1365 }
1366
bsalomon@google.com452943d2011-10-31 17:37:14 +00001367 grPaint->setTexture(kBitmapTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001368
reed@google.com20efde72011-05-09 17:00:02 +00001369 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1370 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001371 GrRect paintRect;
bsalomon@google.com91832162012-03-08 19:53:02 +00001372 float wInv = 1.f / bitmap.width();
1373 float hInv = 1.f / bitmap.height();
1374 paintRect.setLTRB(SkFloatToScalar(srcRect.fLeft * wInv),
1375 SkFloatToScalar(srcRect.fTop * hInv),
1376 SkFloatToScalar(srcRect.fRight * wInv),
1377 SkFloatToScalar(srcRect.fBottom * hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001378
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001379 bool needsTextureDomain = false;
1380 if (GrSamplerState::kBilinear_Filter == sampler->getFilter())
1381 {
1382 // Need texture domain if drawing a sub rect.
1383 needsTextureDomain = srcRect.width() < bitmap.width() ||
1384 srcRect.height() < bitmap.height();
1385 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1386 // sampling is axis-aligned
1387 GrRect floatSrcRect, transformedRect;
1388 floatSrcRect.set(srcRect);
1389 SkMatrix srcToDeviceMatrix(m);
1390 srcToDeviceMatrix.postConcat(*draw.fMatrix);
1391 srcToDeviceMatrix.mapRect(&transformedRect, floatSrcRect);
1392
1393 if (hasAlignedSamples(floatSrcRect, transformedRect)) {
1394 // Samples are texel-aligned, so filtering is futile
1395 sampler->setFilter(GrSamplerState::kNearest_Filter);
1396 needsTextureDomain = false;
1397 } else {
1398 needsTextureDomain = needsTextureDomain &&
1399 mayColorBleed(floatSrcRect, transformedRect, m);
1400 }
1401 }
1402 }
1403
1404 GrRect textureDomain = GrRect::MakeEmpty();
1405
1406 if (needsTextureDomain) {
1407 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001408 GrScalar left, top, right, bottom;
1409 if (srcRect.width() > 1) {
1410 GrScalar border = GR_ScalarHalf / bitmap.width();
1411 left = paintRect.left() + border;
1412 right = paintRect.right() - border;
1413 } else {
1414 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1415 }
1416 if (srcRect.height() > 1) {
1417 GrScalar border = GR_ScalarHalf / bitmap.height();
1418 top = paintRect.top() + border;
1419 bottom = paintRect.bottom() - border;
1420 } else {
1421 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1422 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001423 textureDomain.setLTRB(left, top, right, bottom);
junov@google.com6acc9b32011-05-16 18:32:07 +00001424 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001425 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001426
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001427 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001428}
1429
reed@google.com8926b162012-03-23 15:36:36 +00001430static GrTexture* filter_texture(GrContext* context, GrTexture* texture,
1431 SkImageFilter* filter, const GrRect& rect) {
1432 GrAssert(filter);
1433
1434 SkSize blurSize;
1435 SkISize radius;
1436
1437 const GrTextureDesc desc = {
1438 kRenderTarget_GrTextureFlagBit,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001439 SkScalarCeilToInt(rect.width()),
1440 SkScalarCeilToInt(rect.height()),
reed@google.com8926b162012-03-23 15:36:36 +00001441 kRGBA_8888_PM_GrPixelConfig,
bsalomon@google.comb9014f42012-03-30 14:22:41 +00001442 0 // samples
reed@google.com8926b162012-03-23 15:36:36 +00001443 };
1444
1445 if (filter->asABlur(&blurSize)) {
1446 GrAutoScratchTexture temp1, temp2;
1447 texture = context->gaussianBlur(texture, &temp1, &temp2, rect,
1448 blurSize.width(),
1449 blurSize.height());
1450 texture->ref();
1451 } else if (filter->asADilate(&radius)) {
1452 GrAutoScratchTexture temp1(context, desc), temp2(context, desc);
1453 texture = context->applyMorphology(texture, rect,
1454 temp1.texture(), temp2.texture(),
bsalomon@google.comb505a122012-05-31 18:40:36 +00001455 GrContext::kDilate_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001456 radius);
1457 texture->ref();
1458 } else if (filter->asAnErode(&radius)) {
1459 GrAutoScratchTexture temp1(context, desc), temp2(context, desc);
1460 texture = context->applyMorphology(texture, rect,
1461 temp1.texture(), temp2.texture(),
bsalomon@google.comb505a122012-05-31 18:40:36 +00001462 GrContext::kErode_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001463 radius);
1464 texture->ref();
1465 }
1466 return texture;
1467}
1468
reed@google.comac10a2d2010-12-22 21:39:39 +00001469void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1470 int left, int top, const SkPaint& paint) {
1471 CHECK_SHOULD_DRAW(draw);
1472
reed@google.com8926b162012-03-23 15:36:36 +00001473 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001474 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1475 return;
1476 }
1477
reed@google.com76dd2772012-01-05 21:15:07 +00001478 int w = bitmap.width();
1479 int h = bitmap.height();
1480
bsalomon@google.com5782d712011-01-21 21:03:59 +00001481 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001482 if(!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001483 return;
1484 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001485
bsalomon@google.com5782d712011-01-21 21:03:59 +00001486 GrAutoMatrix avm(fContext, GrMatrix::I());
1487
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001488 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001489
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001490 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001491 sampler->reset();
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001492 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001493 grPaint.setTexture(kBitmapTextureIdx, texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001494
reed@google.com8926b162012-03-23 15:36:36 +00001495 SkImageFilter* filter = paint.getImageFilter();
1496 if (NULL != filter) {
1497 GrTexture* filteredTexture = filter_texture(fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001498 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001499 if (filteredTexture) {
1500 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1501 texture = filteredTexture;
1502 filteredTexture->unref();
1503 }
reed@google.com76dd2772012-01-05 21:15:07 +00001504 }
reed@google.com8926b162012-03-23 15:36:36 +00001505
bsalomon@google.com5782d712011-01-21 21:03:59 +00001506 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001507 GrRect::MakeXYWH(GrIntToScalar(left),
1508 GrIntToScalar(top),
1509 GrIntToScalar(w),
1510 GrIntToScalar(h)),
1511 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1512 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001513}
1514
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001515void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
reed@google.comac10a2d2010-12-22 21:39:39 +00001516 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001517 // clear of the source device must occur before CHECK_SHOULD_DRAW
1518 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1519 if (dev->fNeedClear) {
1520 // TODO: could check here whether we really need to draw at all
1521 dev->clear(0x0);
1522 }
1523
reed@google.comac10a2d2010-12-22 21:39:39 +00001524 CHECK_SHOULD_DRAW(draw);
1525
bsalomon@google.com5782d712011-01-21 21:03:59 +00001526 GrPaint grPaint;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001527 if (!dev->bindDeviceAsTexture(&grPaint) ||
bsalomon@google.com84405e02012-03-05 19:57:21 +00001528 !skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001529 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001530 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001531
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001532 GrTexture* devTex = grPaint.getTexture(0);
1533 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001534
reed@google.com8926b162012-03-23 15:36:36 +00001535 SkImageFilter* filter = paint.getImageFilter();
1536 if (NULL != filter) {
robertphillips@google.com8637a362012-04-10 18:32:35 +00001537 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
1538 SkIntToScalar(devTex->height()));
reed@google.com8926b162012-03-23 15:36:36 +00001539 GrTexture* filteredTexture = filter_texture(fContext, devTex, filter,
1540 rect);
1541 if (filteredTexture) {
1542 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1543 devTex = filteredTexture;
1544 filteredTexture->unref();
1545 }
1546 }
1547
bsalomon@google.com5782d712011-01-21 21:03:59 +00001548 const SkBitmap& bm = dev->accessBitmap(false);
1549 int w = bm.width();
1550 int h = bm.height();
1551
1552 GrAutoMatrix avm(fContext, GrMatrix::I());
1553
bsalomon@google.com97912912011-12-06 16:30:36 +00001554 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001555
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001556 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1557 GrIntToScalar(y),
1558 GrIntToScalar(w),
1559 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001560
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001561 // The device being drawn may not fill up its texture (saveLayer uses
1562 // the approximate ).
1563 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1564 GR_Scalar1 * h / devTex->height());
1565
1566 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001567}
1568
reed@google.com8926b162012-03-23 15:36:36 +00001569bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
reed@google.com76dd2772012-01-05 21:15:07 +00001570 SkSize size;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001571 SkISize radius;
1572 if (!filter->asABlur(&size) && !filter->asADilate(&radius) && !filter->asAnErode(&radius)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001573 return false;
1574 }
reed@google.com8926b162012-03-23 15:36:36 +00001575 return true;
1576}
1577
1578bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1579 const SkMatrix& ctm,
1580 SkBitmap* result, SkIPoint* offset) {
1581 // want explicitly our impl, so guard against a subclass of us overriding it
1582 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001583 return false;
1584 }
reed@google.com8926b162012-03-23 15:36:36 +00001585
1586 SkAutoLockPixels alp(src, !src.getTexture());
1587 if (!src.getTexture() && !src.readyToDraw()) {
1588 return false;
1589 }
1590
1591 GrPaint paint;
1592 paint.reset();
1593
1594 GrSamplerState* sampler = paint.textureSampler(kBitmapTextureIdx);
1595
1596 GrTexture* texture;
1597 SkAutoCachedTexture act(this, src, sampler, &texture);
1598
1599 result->setConfig(src.config(), src.width(), src.height());
robertphillips@google.com8637a362012-04-10 18:32:35 +00001600 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
1601 SkIntToScalar(src.height()));
reed@google.com8926b162012-03-23 15:36:36 +00001602 GrTexture* resultTexture = filter_texture(fContext, texture, filter, rect);
1603 if (resultTexture) {
1604 result->setPixelRef(new SkGrTexturePixelRef(resultTexture))->unref();
1605 resultTexture->unref();
1606 }
reed@google.com76dd2772012-01-05 21:15:07 +00001607 return true;
1608}
1609
reed@google.comac10a2d2010-12-22 21:39:39 +00001610///////////////////////////////////////////////////////////////////////////////
1611
1612// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001613static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1614 kTriangles_PrimitiveType,
1615 kTriangleStrip_PrimitiveType,
1616 kTriangleFan_PrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001617};
1618
1619void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1620 int vertexCount, const SkPoint vertices[],
1621 const SkPoint texs[], const SkColor colors[],
1622 SkXfermode* xmode,
1623 const uint16_t indices[], int indexCount,
1624 const SkPaint& paint) {
1625 CHECK_SHOULD_DRAW(draw);
1626
bsalomon@google.com5782d712011-01-21 21:03:59 +00001627 GrPaint grPaint;
1628 SkAutoCachedTexture act;
1629 // we ignore the shader if texs is null.
1630 if (NULL == texs) {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001631 if (!skPaint2GrPaintNoShader(paint,
1632 false,
1633 NULL == colors,
1634 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001635 return;
1636 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001637 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001638 if (!skPaint2GrPaintShader(this,
1639 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001640 NULL == colors,
1641 &act,
1642 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001643 return;
1644 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001645 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001646
1647 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001648 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001649 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1650#if 0
1651 return
1652#endif
1653 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001654 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001655
bsalomon@google.com498776a2011-08-16 19:20:44 +00001656 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1657 if (NULL != colors) {
1658 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001659 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001660 for (int i = 0; i < vertexCount; ++i) {
1661 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1662 }
1663 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001664 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001665 fContext->drawVertices(grPaint,
1666 gVertexMode2PrimitiveType[vmode],
1667 vertexCount,
1668 (GrPoint*) vertices,
1669 (GrPoint*) texs,
1670 colors,
1671 indices,
1672 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001673}
1674
1675///////////////////////////////////////////////////////////////////////////////
1676
1677static void GlyphCacheAuxProc(void* data) {
1678 delete (GrFontScaler*)data;
1679}
1680
1681static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1682 void* auxData;
1683 GrFontScaler* scaler = NULL;
1684 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1685 scaler = (GrFontScaler*)auxData;
1686 }
1687 if (NULL == scaler) {
1688 scaler = new SkGrFontScaler(cache);
1689 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1690 }
1691 return scaler;
1692}
1693
1694static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1695 SkFixed fx, SkFixed fy,
1696 const SkGlyph& glyph) {
1697 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1698
bungeman@google.com15865a72012-01-11 16:28:04 +00001699 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001700
1701 if (NULL == procs->fFontScaler) {
1702 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1703 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001704
bungeman@google.com15865a72012-01-11 16:28:04 +00001705 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1706 glyph.getSubXFixed(),
1707 glyph.getSubYFixed()),
1708 SkFixedFloorToFixed(fx),
1709 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001710 procs->fFontScaler);
1711}
1712
bsalomon@google.com5782d712011-01-21 21:03:59 +00001713SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001714
1715 // deferred allocation
1716 if (NULL == fDrawProcs) {
1717 fDrawProcs = new GrSkDrawProcs;
1718 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1719 fDrawProcs->fContext = fContext;
1720 }
1721
1722 // init our (and GL's) state
1723 fDrawProcs->fTextContext = context;
1724 fDrawProcs->fFontScaler = NULL;
1725 return fDrawProcs;
1726}
1727
1728void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1729 size_t byteLength, SkScalar x, SkScalar y,
1730 const SkPaint& paint) {
1731 CHECK_SHOULD_DRAW(draw);
1732
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001733 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001734 // this guy will just call our drawPath()
1735 draw.drawText((const char*)text, byteLength, x, y, paint);
1736 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001737 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001738
1739 GrPaint grPaint;
1740 SkAutoCachedTexture act;
1741
bsalomon@google.com84405e02012-03-05 19:57:21 +00001742 if (!skPaint2GrPaintShader(this,
1743 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001744 true,
1745 &act,
1746 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001747 return;
1748 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001749 GrTextContext::AutoFinish txtCtxAF(this->getTextContext(), fContext,
1750 grPaint, draw.fExtMatrix);
1751 myDraw.fProcs = this->initDrawForText(txtCtxAF.getTextContext());
reed@google.comac10a2d2010-12-22 21:39:39 +00001752 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1753 }
1754}
1755
1756void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1757 size_t byteLength, const SkScalar pos[],
1758 SkScalar constY, int scalarsPerPos,
1759 const SkPaint& paint) {
1760 CHECK_SHOULD_DRAW(draw);
1761
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001762 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001763 // this guy will just call our drawPath()
1764 draw.drawPosText((const char*)text, byteLength, pos, constY,
1765 scalarsPerPos, paint);
1766 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001767 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001768
1769 GrPaint grPaint;
1770 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001771 if (!skPaint2GrPaintShader(this,
1772 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001773 true,
1774 &act,
1775 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001776 return;
1777 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001778 GrTextContext::AutoFinish txtCtxAF(this->getTextContext(), fContext,
1779 grPaint, draw.fExtMatrix);
1780 myDraw.fProcs = this->initDrawForText(txtCtxAF.getTextContext());
reed@google.comac10a2d2010-12-22 21:39:39 +00001781 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1782 scalarsPerPos, paint);
1783 }
1784}
1785
1786void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1787 size_t len, const SkPath& path,
1788 const SkMatrix* m, const SkPaint& paint) {
1789 CHECK_SHOULD_DRAW(draw);
1790
1791 SkASSERT(draw.fDevice == this);
1792 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1793}
1794
1795///////////////////////////////////////////////////////////////////////////////
1796
reed@google.comf67e4cf2011-03-15 20:56:58 +00001797bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1798 if (!paint.isLCDRenderText()) {
1799 // we're cool with the paint as is
1800 return false;
1801 }
1802
1803 if (paint.getShader() ||
1804 paint.getXfermode() || // unless its srcover
1805 paint.getMaskFilter() ||
1806 paint.getRasterizer() ||
1807 paint.getColorFilter() ||
1808 paint.getPathEffect() ||
1809 paint.isFakeBoldText() ||
1810 paint.getStyle() != SkPaint::kFill_Style) {
1811 // turn off lcd
1812 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1813 flags->fHinting = paint.getHinting();
1814 return true;
1815 }
1816 // we're cool with the paint as is
1817 return false;
1818}
1819
reed@google.com75d939b2011-12-07 15:07:23 +00001820void SkGpuDevice::flush() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001821 DO_DEFERRED_CLEAR;
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001822 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001823}
1824
reed@google.comf67e4cf2011-03-15 20:56:58 +00001825///////////////////////////////////////////////////////////////////////////////
1826
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001827SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(
1828 const SkBitmap& bitmap,
1829 const GrSamplerState* sampler) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001830 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001831 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001832
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001833 if (!bitmap.isVolatile()) {
1834 GrContext::TextureKey key = bitmap.getGenerationID();
1835 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001836
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001837 entry = ctx->findAndLockTexture(key, bitmap.width(),
1838 bitmap.height(), sampler);
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001839 if (NULL == entry.texture()) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001840 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
1841 bitmap);
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001842 }
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001843 } else {
1844 entry = sk_gr_create_bitmap_texture(ctx, gUNCACHED_KEY,
1845 sampler, bitmap);
1846 }
1847 if (NULL == entry.texture()) {
1848 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1849 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001850 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001851 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001852}
1853
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001854void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1855 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001856}
1857
bsalomon@google.comfb309512011-11-30 14:13:48 +00001858bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
1859 const GrSamplerState& sampler) const {
1860 GrContext::TextureKey key = bitmap.getGenerationID();
1861 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
1862 return this->context()->isTextureInCache(key, bitmap.width(),
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001863 bitmap.height(), &sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001864
1865}
1866
1867
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001868SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1869 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001870 bool isOpaque,
1871 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001872 GrTextureDesc desc;
1873 desc.fConfig = fRenderTarget->config();
1874 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1875 desc.fWidth = width;
1876 desc.fHeight = height;
1877 desc.fSampleCnt = fRenderTarget->numSamples();
1878
1879 GrContext::TextureCacheEntry cacheEntry;
1880 GrTexture* texture;
1881 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001882 // Skia's convention is to only clear a device if it is non-opaque.
1883 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001884
1885#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1886 // layers are never draw in repeat modes, so we can request an approx
1887 // match and ignore any padding.
1888 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1889 GrContext::kApprox_ScratchTexMatch :
1890 GrContext::kExact_ScratchTexMatch;
1891 cacheEntry = fContext->lockScratchTexture(desc, matchType);
1892 texture = cacheEntry.texture();
1893#else
1894 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1895 texture = tunref.get();
1896#endif
1897 if (texture) {
1898 return SkNEW_ARGS(SkGpuDevice,(fContext,
1899 texture,
1900 cacheEntry,
1901 needClear));
1902 } else {
1903 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1904 width, height);
1905 return NULL;
1906 }
1907}
1908
1909SkGpuDevice::SkGpuDevice(GrContext* context,
1910 GrTexture* texture,
1911 TexCache cacheEntry,
1912 bool needClear)
1913 : SkDevice(make_bitmap(context, texture->asRenderTarget())) {
1914 GrAssert(texture && texture->asRenderTarget());
1915 GrAssert(NULL == cacheEntry.texture() || texture == cacheEntry.texture());
1916 this->initFromRenderTarget(context, texture->asRenderTarget());
1917 fCache = cacheEntry;
1918 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001919}
1920
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001921GrTextContext* SkGpuDevice::getTextContext() {
1922 if (NULL == fTextContext) {
1923 fTextContext = new GrDefaultTextContext();
1924 }
1925 return fTextContext;
1926}