blob: 46b06d80bebcb5da7e74b4c1e931f9f3f9f0bad7 [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
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000229 GrTextureDesc desc;
230 desc.fFlags = kRenderTarget_GrTextureFlagBit;
231 desc.fWidth = width;
232 desc.fHeight = height;
233 desc.fConfig = SkGr::BitmapConfig2PixelConfig(bm.config());
reed@google.comac10a2d2010-12-22 21:39:39 +0000234
reed@google.comaf951c92011-06-16 19:10:39 +0000235 fTexture = fContext->createUncachedTexture(desc, NULL, 0);
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000236
reed@google.comaf951c92011-06-16 19:10:39 +0000237 if (NULL != fTexture) {
238 fRenderTarget = fTexture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000239 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000240
reed@google.comaf951c92011-06-16 19:10:39 +0000241 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000242
reed@google.comaf951c92011-06-16 19:10:39 +0000243 // wrap the bitmap with a pixelref to expose our texture
244 SkGrTexturePixelRef* pr = new SkGrTexturePixelRef(fTexture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000245 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000246 } else {
247 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
248 width, height);
249 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000250 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +0000251
252 fTextContext = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000253}
254
255SkGpuDevice::~SkGpuDevice() {
256 if (fDrawProcs) {
257 delete fDrawProcs;
258 }
259
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000260 // The SkGpuDevice gives the context the render target (e.g., in gainFocus)
261 // This call gives the context a chance to relinquish it
262 fContext->setRenderTarget(NULL);
263
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000264 SkSafeUnref(fTexture);
265 SkSafeUnref(fRenderTarget);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000266 if (fCache.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000267 GrAssert(NULL != fTexture);
268 GrAssert(fRenderTarget == fTexture->asRenderTarget());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000269 fContext->unlockTexture(fCache);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000270 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000271 fContext->unref();
bsalomon@google.comf4a9c822012-03-16 14:02:46 +0000272
273 if (NULL != fTextContext) {
274 fTextContext->unref();
275 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000276}
277
reed@google.comac10a2d2010-12-22 21:39:39 +0000278///////////////////////////////////////////////////////////////////////////////
279
280void SkGpuDevice::makeRenderTargetCurrent() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000281 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000282 fContext->setRenderTarget(fRenderTarget);
283 fContext->flush(true);
284 fNeedPrepareRenderTarget = true;
285}
286
287///////////////////////////////////////////////////////////////////////////////
288
bsalomon@google.comc4364992011-11-07 15:54:49 +0000289namespace {
290GrPixelConfig config8888_to_gr_config(SkCanvas::Config8888 config8888) {
291 switch (config8888) {
292 case SkCanvas::kNative_Premul_Config8888:
293 return kSkia8888_PM_GrPixelConfig;
294 case SkCanvas::kNative_Unpremul_Config8888:
295 return kSkia8888_UPM_GrPixelConfig;
296 case SkCanvas::kBGRA_Premul_Config8888:
297 return kBGRA_8888_PM_GrPixelConfig;
298 case SkCanvas::kBGRA_Unpremul_Config8888:
299 return kBGRA_8888_UPM_GrPixelConfig;
300 case SkCanvas::kRGBA_Premul_Config8888:
301 return kRGBA_8888_PM_GrPixelConfig;
302 case SkCanvas::kRGBA_Unpremul_Config8888:
303 return kRGBA_8888_UPM_GrPixelConfig;
304 default:
305 GrCrash("Unexpected Config8888.");
306 return kSkia8888_PM_GrPixelConfig;
307 }
308}
309}
310
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000311bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
312 int x, int y,
313 SkCanvas::Config8888 config8888) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000314 DO_DEFERRED_CLEAR;
bsalomon@google.com910267d2011-11-02 20:06:25 +0000315 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
316 SkASSERT(!bitmap.isNull());
317 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000318
bsalomon@google.com910267d2011-11-02 20:06:25 +0000319 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000320 GrPixelConfig config;
321 config = config8888_to_gr_config(config8888);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000322 return fContext->readRenderTargetPixels(fRenderTarget,
323 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000324 bitmap.width(),
325 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000326 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000327 bitmap.getPixels(),
328 bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000329}
330
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000331void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
332 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000333 SkAutoLockPixels alp(bitmap);
334 if (!bitmap.readyToDraw()) {
335 return;
336 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000337
338 GrPixelConfig config;
339 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
340 config = config8888_to_gr_config(config8888);
341 } else {
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000342 config= SkGr::BitmapConfig2PixelConfig(bitmap.config());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000343 }
344
bsalomon@google.com6f379512011-11-16 20:36:03 +0000345 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
346 config, bitmap.getPixels(), bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000347}
348
349///////////////////////////////////////////////////////////////////////////////
350
351static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000352 const SkClipStack& clipStack,
reed@google.com6f8f2922011-03-04 22:27:10 +0000353 const SkRegion& clipRegion,
354 const SkIPoint& origin) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000355 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000356
357 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000358 iter.reset(clipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000359 const SkIRect& skBounds = clipRegion.getBounds();
360 GrRect bounds;
361 bounds.setLTRB(GrIntToScalar(skBounds.fLeft),
362 GrIntToScalar(skBounds.fTop),
363 GrIntToScalar(skBounds.fRight),
364 GrIntToScalar(skBounds.fBottom));
reed@google.com6f8f2922011-03-04 22:27:10 +0000365 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
366 &bounds);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000367 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000368}
369
370// call this ever each draw call, to ensure that the context reflects our state,
371// and not the state from some other canvas/device
372void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
373 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000374 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000375
376 fContext->setRenderTarget(fRenderTarget);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000377 SkASSERT(draw.fClipStack);
378 convert_matrixclip(fContext, *draw.fMatrix,
reed@google.com6f8f2922011-03-04 22:27:10 +0000379 *draw.fClipStack, *draw.fClip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000380 fNeedPrepareRenderTarget = false;
381 }
382}
383
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000384void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
385 const SkClipStack& clipStack) {
386 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
387 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000388 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000389}
390
391void SkGpuDevice::gainFocus(SkCanvas* canvas, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000392 const SkRegion& clip, const SkClipStack& clipStack) {
393
reed@google.comac10a2d2010-12-22 21:39:39 +0000394 fContext->setRenderTarget(fRenderTarget);
395
bsalomon@google.comd302f142011-03-03 13:54:13 +0000396 this->INHERITED::gainFocus(canvas, matrix, clip, clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000397
reed@google.com6f8f2922011-03-04 22:27:10 +0000398 convert_matrixclip(fContext, matrix, clipStack, clip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000399
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000400 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000401}
402
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000403SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000404 DO_DEFERRED_CLEAR;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000405 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000406}
407
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000408bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000409 if (NULL != fTexture) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000410 paint->setTexture(kBitmapTextureIdx, fTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000411 return true;
412 }
413 return false;
414}
415
416///////////////////////////////////////////////////////////////////////////////
417
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000418SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
419SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
420SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
421SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
422SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
423 shader_type_mismatch);
424SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 4, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000425
bsalomon@google.com84405e02012-03-05 19:57:21 +0000426namespace {
427
428// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
429// justAlpha indicates that skPaint's alpha should be used rather than the color
430// Callers may subsequently modify the GrPaint. Setting constantColor indicates
431// that the final paint will draw the same color at every pixel. This allows
432// an optimization where the the color filter can be applied to the skPaint's
433// color once while converting to GrPain and then ignored.
434inline bool skPaint2GrPaintNoShader(const SkPaint& skPaint,
435 bool justAlpha,
436 bool constantColor,
437 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000438
439 grPaint->fDither = skPaint.isDither();
440 grPaint->fAntiAlias = skPaint.isAntiAlias();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000441 grPaint->fCoverage = 0xFF;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000442
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000443 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
444 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000445
446 SkXfermode* mode = skPaint.getXfermode();
447 if (mode) {
448 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000449 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000450#if 0
451 return false;
452#endif
453 }
454 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000455 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
456 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
457
bsalomon@google.com5782d712011-01-21 21:03:59 +0000458 if (justAlpha) {
459 uint8_t alpha = skPaint.getAlpha();
460 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000461 // justAlpha is currently set to true only if there is a texture,
462 // so constantColor should not also be true.
463 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000464 } else {
465 grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000466 grPaint->setTexture(kShaderTextureIdx, NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000467 }
Scroggo97c88c22011-05-11 14:05:25 +0000468 SkColorFilter* colorFilter = skPaint.getColorFilter();
469 SkColor color;
470 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000471 SkScalar matrix[20];
Scroggo97c88c22011-05-11 14:05:25 +0000472 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000473 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000474 if (!constantColor) {
475 grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color);
476 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000477 } else {
478 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
479 grPaint->fColor = SkGr::SkColor2GrColor(filtered);
senorblanco@chromium.orgb3c20fa2012-01-03 21:20:19 +0000480 grPaint->resetColorFilter();
Scroggod757df22011-05-16 13:11:16 +0000481 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000482 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
483 grPaint->fColorMatrixEnabled = true;
484 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
485 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
486 } else {
487 grPaint->resetColorFilter();
Scroggo97c88c22011-05-11 14:05:25 +0000488 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000489 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000490}
491
bsalomon@google.com84405e02012-03-05 19:57:21 +0000492// This function is similar to skPaint2GrPaintNoShader but also converts
493// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
494// be used is set on grPaint and returned in param act. constantColor has the
495// same meaning as in skPaint2GrPaintNoShader.
496inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
497 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000498 bool constantColor,
499 SkGpuDevice::SkAutoCachedTexture* act,
500 GrPaint* grPaint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000501
bsalomon@google.com5782d712011-01-21 21:03:59 +0000502 SkASSERT(NULL != act);
reed@google.comac10a2d2010-12-22 21:39:39 +0000503
bsalomon@google.com5782d712011-01-21 21:03:59 +0000504 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000505 if (NULL == shader) {
bsalomon@google.com84405e02012-03-05 19:57:21 +0000506 return skPaint2GrPaintNoShader(skPaint,
507 false,
508 constantColor,
509 grPaint);
510 } else if (!skPaint2GrPaintNoShader(skPaint, true, false, grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000511 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000512 }
513
reed@google.comac10a2d2010-12-22 21:39:39 +0000514 SkBitmap bitmap;
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000515 SkMatrix* matrix = grPaint->textureSampler(kShaderTextureIdx)->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000516 SkShader::TileMode tileModes[2];
517 SkScalar twoPointParams[3];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000518 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000519 tileModes, twoPointParams);
520
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000521 if (SkShader::kNone_BitmapType == bmptype) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000522 SkShader::GradientInfo info;
523 SkColor color;
524
525 info.fColors = &color;
526 info.fColorOffsets = NULL;
527 info.fColorCount = 1;
528 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
529 SkPaint copy(skPaint);
530 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000531 // modulate the paint alpha by the shader's solid color alpha
532 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
533 copy.setColor(SkColorSetA(color, newA));
bsalomon@google.com84405e02012-03-05 19:57:21 +0000534 return skPaint2GrPaintNoShader(copy,
535 false,
536 constantColor,
537 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000538 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000539 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000540 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000541 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000542 switch (bmptype) {
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000543 case SkShader::kRadial_BitmapType:
544 sampler->setCustomStage(new GrRadialGradient())->unref();
545 sampler->setFilter(GrSamplerState::kBilinear_Filter);
546 break;
547 case SkShader::kSweep_BitmapType:
548 sampler->setCustomStage(new GrSweepGradient())->unref();
549 sampler->setFilter(GrSamplerState::kBilinear_Filter);
550 break;
551 case SkShader::kTwoPointRadial_BitmapType:
552 sampler->setCustomStage(new
553 GrRadial2Gradient(twoPointParams[0],
554 twoPointParams[1],
555 twoPointParams[2] < 0))->unref();
556 sampler->setFilter(GrSamplerState::kBilinear_Filter);
557 break;
558 default:
559 if (skPaint.isFilterBitmap()) {
560 sampler->setFilter(GrSamplerState::kBilinear_Filter);
561 } else {
562 sampler->setFilter(GrSamplerState::kNearest_Filter);
563 }
564 break;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000565 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000566 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
567 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000568
bsalomon@google.com84405e02012-03-05 19:57:21 +0000569 GrTexture* texture = act->set(dev, bitmap, sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000570 if (NULL == texture) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000571 SkDebugf("Couldn't convert bitmap to texture.\n");
572 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000573 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000574 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000575
576 // since our texture coords will be in local space, we wack the texture
577 // matrix to map them back into 0...1 before we load it
578 SkMatrix localM;
579 if (shader->getLocalMatrix(&localM)) {
580 SkMatrix inverse;
581 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000582 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000583 }
584 }
585 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000586 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
587 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000588 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000589 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000590 GrScalar s = SkFloatToScalar(1.f / bitmap.width());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000591 matrix->postScale(s, s);
reed@google.comac10a2d2010-12-22 21:39:39 +0000592 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000593
594 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000595}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000596}
reed@google.comac10a2d2010-12-22 21:39:39 +0000597
598///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000599void SkGpuDevice::clear(SkColor color) {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000600 fContext->clear(NULL, color, fRenderTarget);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000601}
602
reed@google.comac10a2d2010-12-22 21:39:39 +0000603void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
604 CHECK_SHOULD_DRAW(draw);
605
bsalomon@google.com5782d712011-01-21 21:03:59 +0000606 GrPaint grPaint;
607 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000608 if (!skPaint2GrPaintShader(this,
609 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000610 true,
611 &act,
612 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000613 return;
614 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000615
616 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000617}
618
619// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000620static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000621 kPoints_GrPrimitiveType,
622 kLines_GrPrimitiveType,
623 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000624};
625
626void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000627 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000628 CHECK_SHOULD_DRAW(draw);
629
630 SkScalar width = paint.getStrokeWidth();
631 if (width < 0) {
632 return;
633 }
634
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000635 // we only handle hairlines and paints without path effects or mask filters,
636 // else we let the SkDraw call our drawPath()
637 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000638 draw.drawPoints(mode, count, pts, paint, true);
639 return;
640 }
641
bsalomon@google.com5782d712011-01-21 21:03:59 +0000642 GrPaint grPaint;
643 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000644 if (!skPaint2GrPaintShader(this,
645 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000646 true,
647 &act,
648 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000649 return;
650 }
651
bsalomon@google.com5782d712011-01-21 21:03:59 +0000652 fContext->drawVertices(grPaint,
653 gPointMode2PrimtiveType[mode],
654 count,
655 (GrPoint*)pts,
656 NULL,
657 NULL,
658 NULL,
659 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000660}
661
reed@google.comc9aa5872011-04-05 21:05:37 +0000662///////////////////////////////////////////////////////////////////////////////
663
reed@google.comac10a2d2010-12-22 21:39:39 +0000664void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
665 const SkPaint& paint) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000666 CHECK_SHOULD_DRAW(draw);
667
bungeman@google.com79bd8772011-07-18 15:34:08 +0000668 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000669 SkScalar width = paint.getStrokeWidth();
670
671 /*
672 We have special code for hairline strokes, miter-strokes, and fills.
673 Anything else we just call our path code.
674 */
675 bool usePath = doStroke && width > 0 &&
676 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000677 // another two reasons we might need to call drawPath...
678 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000679 usePath = true;
680 }
reed@google.com67db6642011-05-26 11:46:35 +0000681 // until we aa rotated rects...
682 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
683 usePath = true;
684 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000685 // small miter limit means right angles show bevel...
686 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
687 paint.getStrokeMiter() < SK_ScalarSqrt2)
688 {
689 usePath = true;
690 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000691 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000692 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
693 usePath = true;
694 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000695
696 if (usePath) {
697 SkPath path;
698 path.addRect(rect);
699 this->drawPath(draw, path, paint, NULL, true);
700 return;
701 }
702
703 GrPaint grPaint;
704 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000705 if (!skPaint2GrPaintShader(this,
706 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000707 true,
708 &act,
709 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000710 return;
711 }
reed@google.com20efde72011-05-09 17:00:02 +0000712 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000713}
714
reed@google.com69302852011-02-16 18:08:07 +0000715#include "SkMaskFilter.h"
716#include "SkBounder.h"
717
bsalomon@google.com85003222012-03-28 14:44:37 +0000718///////////////////////////////////////////////////////////////////////////////
719
720// helpers for applying mask filters
721namespace {
722
723GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000724 switch (fillType) {
725 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000726 return kWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000727 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000728 return kEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000729 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000730 return kInverseWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000731 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000732 return kInverseEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000733 default:
734 SkDebugf("Unsupported path fill type\n");
bsalomon@google.com47059542012-06-06 20:51:20 +0000735 return kHairLine_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000736 }
737}
738
bsalomon@google.com85003222012-03-28 14:44:37 +0000739// We prefer to blur small rect with small radius via CPU.
740#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
741#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
742inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
743 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
744 rect.height() <= MIN_GPU_BLUR_SIZE &&
745 radius <= MIN_GPU_BLUR_RADIUS) {
746 return true;
747 }
748 return false;
749}
750
751bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
752 SkMaskFilter* filter, const SkMatrix& matrix,
753 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000754 GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000755#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000756 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000757#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000758 SkMaskFilter::BlurInfo info;
759 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000760 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000761 return false;
762 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000763 SkScalar radius = info.fIgnoreTransform ? info.fRadius
764 : matrix.mapRadius(info.fRadius);
765 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000766 if (radius <= 0) {
767 return false;
768 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000769
770 SkRect srcRect = path.getBounds();
771 if (shouldDrawBlurWithCPU(srcRect, radius)) {
772 return false;
773 }
774
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000775 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000776 float sigma3 = sigma * 3.0f;
777
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000778 SkRect clipRect;
779 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000780
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000781 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000782 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
783 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000784 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000785 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000786 SkIRect finalIRect;
787 finalRect.roundOut(&finalIRect);
788 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000789 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000790 }
791 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000792 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000793 }
794 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000795 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000796 GrTextureDesc desc;
797 desc.fFlags = kRenderTarget_GrTextureFlagBit;
798 desc.fWidth = SkScalarCeilToInt(srcRect.width());
799 desc.fHeight = SkScalarCeilToInt(srcRect.height());
800 // We actually only need A8, but it often isn't supported as a
801 // render target so default to RGBA_8888
802 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000803
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000804 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
805 desc.fConfig = kAlpha_8_GrPixelConfig;
806 }
807
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000808 GrAutoScratchTexture pathEntry(context, desc);
809 GrTexture* pathTexture = pathEntry.texture();
810 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000811 return false;
812 }
813 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000814 // Once this code moves into GrContext, this should be changed to use
815 // an AutoClipRestore.
816 GrClip oldClip = context->getClip();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000817 context->setRenderTarget(pathTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000818 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000819 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000820 GrPaint tempPaint;
821 tempPaint.reset();
822
823 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000824 tempPaint.fAntiAlias = grp->fAntiAlias;
825 if (tempPaint.fAntiAlias) {
826 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
827 // blend coeff of zero requires dual source blending support in order
828 // to properly blend partially covered pixels. This means the AA
829 // code path may not be taken. So we use a dst blend coeff of ISA. We
830 // could special case AA draws to a dst surface with known alpha=0 to
831 // use a zero dst coeff when dual source blending isn't available.
bsalomon@google.com47059542012-06-06 20:51:20 +0000832 tempPaint.fSrcBlendCoeff = kOne_GrBlendCoeff;
833 tempPaint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000834 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000835 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000836 context->drawPath(tempPaint, path, pathFillType, &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000837
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000838 GrAutoScratchTexture temp1, temp2;
839 // If we're doing a normal blur, we can clobber the pathTexture in the
840 // gaussianBlur. Otherwise, we need to save it for later compositing.
841 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000842 GrTexture* blurTexture = context->gaussianBlur(pathTexture,
843 &temp1,
844 isNormalBlur ? NULL : &temp2,
845 srcRect, sigma, sigma);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000846
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000847 if (!isNormalBlur) {
848 GrPaint paint;
849 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000850 paint.textureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000851 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
852 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000853 // Blend pathTexture over blurTexture.
854 context->setRenderTarget(blurTexture->asRenderTarget());
855 paint.setTexture(0, pathTexture);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000856 if (SkMaskFilter::kInner_BlurType == blurType) {
857 // inner: dst = dst * src
bsalomon@google.com47059542012-06-06 20:51:20 +0000858 paint.fSrcBlendCoeff = kDC_GrBlendCoeff;
859 paint.fDstBlendCoeff = kZero_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000860 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
861 // solid: dst = src + dst - src * dst
862 // = (1 - dst) * src + 1 * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000863 paint.fSrcBlendCoeff = kIDC_GrBlendCoeff;
864 paint.fDstBlendCoeff = kOne_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000865 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
866 // outer: dst = dst * (1 - src)
867 // = 0 * src + (1 - src) * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000868 paint.fSrcBlendCoeff = kZero_GrBlendCoeff;
869 paint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000870 }
871 context->drawRect(paint, srcRect);
872 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000873 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000874 context->setClip(oldClip);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000875
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000876 if (grp->hasTextureOrMask()) {
877 GrMatrix inverse;
878 if (!matrix.invert(&inverse)) {
879 return false;
880 }
881 grp->preConcatActiveSamplerMatrices(inverse);
882 }
883
884 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
885 // we assume the last mask index is available for use
886 GrAssert(NULL == grp->getMask(MASK_IDX));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000887 grp->setMask(MASK_IDX, blurTexture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000888 grp->maskSampler(MASK_IDX)->reset();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000889
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000890 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
891 -finalRect.fTop);
892 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
893 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000894 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000895 return true;
896}
897
bsalomon@google.com85003222012-03-28 14:44:37 +0000898bool drawWithMaskFilter(GrContext* context, const SkPath& path,
899 SkMaskFilter* filter, const SkMatrix& matrix,
900 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000901 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000902 SkMask srcM, dstM;
903
904 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000905 SkMask::kComputeBoundsAndRenderImage_CreateMode,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000906 style)) {
reed@google.com69302852011-02-16 18:08:07 +0000907 return false;
908 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000909 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000910
911 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
912 return false;
913 }
914 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000915 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000916
917 if (clip.quickReject(dstM.fBounds)) {
918 return false;
919 }
920 if (bounder && !bounder->doIRect(dstM.fBounds)) {
921 return false;
922 }
923
924 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
925 // the current clip (and identity matrix) and grpaint settings
926
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000927 // used to compute inverse view, if necessary
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000928 GrMatrix ivm = matrix;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000929
reed@google.com0c219b62011-02-16 21:31:18 +0000930 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +0000931
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000932 GrTextureDesc desc;
933 desc.fWidth = dstM.fBounds.width();
934 desc.fHeight = dstM.fBounds.height();
935 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +0000936
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000937 GrAutoScratchTexture ast(context, desc);
938 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000939
reed@google.com69302852011-02-16 18:08:07 +0000940 if (NULL == texture) {
941 return false;
942 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000943 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000944 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000945
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000946 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
947 grp->preConcatActiveSamplerMatrices(ivm);
948 }
949
950 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
951 // we assume the last mask index is available for use
952 GrAssert(NULL == grp->getMask(MASK_IDX));
953 grp->setMask(MASK_IDX, texture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000954 grp->maskSampler(MASK_IDX)->reset();
reed@google.com69302852011-02-16 18:08:07 +0000955
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000956 GrRect d;
957 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +0000958 GrIntToScalar(dstM.fBounds.fTop),
959 GrIntToScalar(dstM.fBounds.fRight),
960 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000961
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000962 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
963 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
964 -dstM.fBounds.fTop*SK_Scalar1);
965 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000966 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000967 return true;
968}
reed@google.com69302852011-02-16 18:08:07 +0000969
bsalomon@google.com85003222012-03-28 14:44:37 +0000970}
971
972///////////////////////////////////////////////////////////////////////////////
973
reed@google.com0c219b62011-02-16 21:31:18 +0000974void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000975 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000976 bool pathIsMutable) {
977 CHECK_SHOULD_DRAW(draw);
978
reed@google.comfe626382011-09-21 13:50:35 +0000979 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000980
bsalomon@google.com5782d712011-01-21 21:03:59 +0000981 GrPaint grPaint;
982 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000983 if (!skPaint2GrPaintShader(this,
984 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000985 true,
986 &act,
987 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000988 return;
989 }
990
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000991 // can we cheat, and threat a thin stroke as a hairline w/ coverage
992 // if we can, we draw lots faster (raster device does this same test)
993 SkScalar hairlineCoverage;
994 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
995 doFill = false;
996 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
997 grPaint.fCoverage);
998 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000999
reed@google.comfe626382011-09-21 13:50:35 +00001000 // If we have a prematrix, apply it to the path, optimizing for the case
1001 // where the original path can in fact be modified in place (even though
1002 // its parameter type is const).
1003 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1004 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001005
1006 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001007 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001008
reed@google.come3445642011-02-16 23:20:39 +00001009 if (!pathIsMutable) {
1010 result = &tmpPath;
1011 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001012 }
reed@google.come3445642011-02-16 23:20:39 +00001013 // should I push prePathMatrix on our MV stack temporarily, instead
1014 // of applying it here? See SkDraw.cpp
1015 pathPtr->transform(*prePathMatrix, result);
1016 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001017 }
reed@google.com0c219b62011-02-16 21:31:18 +00001018 // at this point we're done with prePathMatrix
1019 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001020
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001021 if (paint.getPathEffect() ||
1022 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001023 // it is safe to use tmpPath here, even if we already used it for the
1024 // prepathmatrix, since getFillPath can take the same object for its
1025 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001026 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001027 pathPtr = &tmpPath;
1028 }
1029
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001030 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001031 // avoid possibly allocating a new path in transform if we can
1032 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1033
1034 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001035 pathPtr->transform(*draw.fMatrix, devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001036 GrPathFill pathFillType = doFill ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001037 skToGrFillType(devPathPtr->getFillType()) : kHairLine_GrPathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001038 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001039 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001040 &grPaint, pathFillType)) {
1041 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
1042 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001043 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001044 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001045 &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001046 }
reed@google.com69302852011-02-16 18:08:07 +00001047 return;
1048 }
reed@google.com69302852011-02-16 18:08:07 +00001049
bsalomon@google.com47059542012-06-06 20:51:20 +00001050 GrPathFill fill = kHairLine_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001051
reed@google.com0c219b62011-02-16 21:31:18 +00001052 if (doFill) {
1053 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001054 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001055 fill = kWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001056 break;
1057 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001058 fill = kEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001059 break;
1060 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001061 fill = kInverseWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001062 break;
1063 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001064 fill = kInverseEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001065 break;
1066 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001067 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001068 return;
1069 }
1070 }
1071
reed@google.com07f3ee12011-05-16 17:21:57 +00001072 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001073}
1074
bsalomon@google.comfb309512011-11-30 14:13:48 +00001075namespace {
1076
1077inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1078 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1079 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1080 return tilesX * tilesY;
1081}
1082
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001083inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001084 const SkIRect* srcRectPtr,
1085 int maxTextureSize) {
1086 static const int kSmallTileSize = 1 << 10;
1087 if (maxTextureSize <= kSmallTileSize) {
1088 return maxTextureSize;
1089 }
1090
1091 size_t maxTexTotalTileSize;
1092 size_t smallTotalTileSize;
1093
1094 if (NULL == srcRectPtr) {
1095 int w = bitmap.width();
1096 int h = bitmap.height();
1097 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1098 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1099 } else {
1100 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1101 srcRectPtr->fTop,
1102 srcRectPtr->fRight,
1103 srcRectPtr->fBottom,
1104 maxTextureSize);
1105 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1106 srcRectPtr->fTop,
1107 srcRectPtr->fRight,
1108 srcRectPtr->fBottom,
1109 kSmallTileSize);
1110 }
1111 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1112 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1113
1114 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1115 return kSmallTileSize;
1116 } else {
1117 return maxTextureSize;
1118 }
1119}
1120}
1121
1122bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1123 const GrSamplerState& sampler,
1124 const SkIRect* srcRectPtr,
1125 int* tileSize) const {
1126 SkASSERT(NULL != tileSize);
1127
1128 // if bitmap is explictly texture backed then just use the texture
1129 if (NULL != bitmap.getTexture()) {
1130 return false;
1131 }
1132 // if it's larger than the max texture size, then we have no choice but
1133 // tiling
1134 const int maxTextureSize = fContext->getMaxTextureSize();
1135 if (bitmap.width() > maxTextureSize ||
1136 bitmap.height() > maxTextureSize) {
1137 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1138 return true;
1139 }
1140 // if we are going to have to draw the whole thing, then don't tile
1141 if (NULL == srcRectPtr) {
1142 return false;
1143 }
1144 // if the entire texture is already in our cache then no reason to tile it
1145 if (this->isBitmapInTextureCache(bitmap, sampler)) {
1146 return false;
1147 }
1148
1149 // At this point we know we could do the draw by uploading the entire bitmap
1150 // as a texture. However, if the texture would be large compared to the
1151 // cache size and we don't require most of it for this draw then tile to
1152 // reduce the amount of upload and cache spill.
1153
1154 // assumption here is that sw bitmap size is a good proxy for its size as
1155 // a texture
1156 size_t bmpSize = bitmap.getSize();
bsalomon@google.com8f7e1da2012-06-21 19:48:32 +00001157 size_t cacheSize = fContext->getTextureCacheBudget();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001158 if (bmpSize < cacheSize / 2) {
1159 return false;
1160 }
1161
1162 SkFixed fracUsed =
1163 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1164 (srcRectPtr->height() << 16) / bitmap.height());
1165 if (fracUsed <= SK_FixedHalf) {
1166 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1167 return true;
1168 } else {
1169 return false;
1170 }
1171}
1172
reed@google.comac10a2d2010-12-22 21:39:39 +00001173void SkGpuDevice::drawBitmap(const SkDraw& draw,
1174 const SkBitmap& bitmap,
1175 const SkIRect* srcRectPtr,
1176 const SkMatrix& m,
1177 const SkPaint& paint) {
1178 CHECK_SHOULD_DRAW(draw);
1179
1180 SkIRect srcRect;
1181 if (NULL == srcRectPtr) {
1182 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1183 } else {
1184 srcRect = *srcRectPtr;
1185 }
1186
junov@google.comd935cfb2011-06-27 20:48:23 +00001187 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001188 // Convert the bitmap to a shader so that the rect can be drawn
1189 // through drawRect, which supports mask filters.
1190 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001191 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001192 if (srcRectPtr) {
1193 if (!bitmap.extractSubset(&tmp, srcRect)) {
1194 return; // extraction failed
1195 }
1196 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001197 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001198 }
1199 SkPaint paintWithTexture(paint);
1200 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1201 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001202 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001203 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001204
junov@google.com1d329782011-07-28 20:10:09 +00001205 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001206 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001207 // also affect the behavior of the mask filter.
1208 SkMatrix drawMatrix;
1209 drawMatrix.setConcat(*draw.fMatrix, m);
1210 SkDraw transformedDraw(draw);
1211 transformedDraw.fMatrix = &drawMatrix;
1212
1213 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1214
junov@google.comd935cfb2011-06-27 20:48:23 +00001215 return;
1216 }
1217
bsalomon@google.com5782d712011-01-21 21:03:59 +00001218 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001219 if (!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001220 return;
1221 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001222 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001223 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001224 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001225 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001226 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001227 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001228
bsalomon@google.comfb309512011-11-30 14:13:48 +00001229 int tileSize;
1230 if (!this->shouldTileBitmap(bitmap, *sampler, srcRectPtr, &tileSize)) {
1231 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001232 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001233 return;
1234 }
1235
1236 // undo the translate done by SkCanvas
1237 int DX = SkMax32(0, srcRect.fLeft);
1238 int DY = SkMax32(0, srcRect.fTop);
1239 // compute clip bounds in local coordinates
1240 SkIRect clipRect;
1241 {
1242 SkRect r;
1243 r.set(draw.fClip->getBounds());
1244 SkMatrix matrix, inverse;
1245 matrix.setConcat(*draw.fMatrix, m);
1246 if (!matrix.invert(&inverse)) {
1247 return;
1248 }
1249 inverse.mapRect(&r);
1250 r.roundOut(&clipRect);
1251 // apply the canvas' translate to our local clip
1252 clipRect.offset(DX, DY);
1253 }
1254
bsalomon@google.comfb309512011-11-30 14:13:48 +00001255 int nx = bitmap.width() / tileSize;
1256 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001257 for (int x = 0; x <= nx; x++) {
1258 for (int y = 0; y <= ny; y++) {
1259 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001260 tileR.set(x * tileSize, y * tileSize,
1261 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001262 if (!SkIRect::Intersects(tileR, clipRect)) {
1263 continue;
1264 }
1265
1266 SkIRect srcR = tileR;
1267 if (!srcR.intersect(srcRect)) {
1268 continue;
1269 }
1270
1271 SkBitmap tmpB;
1272 if (bitmap.extractSubset(&tmpB, tileR)) {
1273 // now offset it to make it "local" to our tmp bitmap
1274 srcR.offset(-tileR.fLeft, -tileR.fTop);
1275
1276 SkMatrix tmpM(m);
1277 {
1278 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1279 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1280 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1281 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001282 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001283 }
1284 }
1285 }
1286}
1287
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001288namespace {
1289
1290bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1291 // detect pixel disalignment
1292 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1293 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1294 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1295 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1296 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1297 COLOR_BLEED_TOLERANCE &&
1298 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1299 COLOR_BLEED_TOLERANCE) {
1300 return true;
1301 }
1302 return false;
1303}
1304
1305bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1306 const SkMatrix& m) {
1307 // Only gets called if hasAlignedSamples returned false.
1308 // So we can assume that sampling is axis aligned but not texel aligned.
1309 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
1310 SkRect innerSrcRect(srcRect), innerTransformedRect,
1311 outerTransformedRect(transformedRect);
1312 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1313 m.mapRect(&innerTransformedRect, innerSrcRect);
1314
1315 // The gap between outerTransformedRect and innerTransformedRect
1316 // represents the projection of the source border area, which is
1317 // problematic for color bleeding. We must check whether any
1318 // destination pixels sample the border area.
1319 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1320 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1321 SkIRect outer, inner;
1322 outerTransformedRect.round(&outer);
1323 innerTransformedRect.round(&inner);
1324 // If the inner and outer rects round to the same result, it means the
1325 // border does not overlap any pixel centers. Yay!
1326 return inner != outer;
1327}
1328
1329} // unnamed namespace
1330
reed@google.comac10a2d2010-12-22 21:39:39 +00001331/*
1332 * This is called by drawBitmap(), which has to handle images that may be too
1333 * large to be represented by a single texture.
1334 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001335 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1336 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001337 */
1338void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1339 const SkBitmap& bitmap,
1340 const SkIRect& srcRect,
1341 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001342 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001343 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1344 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001345
reed@google.com9c49bc32011-07-07 13:42:37 +00001346 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001347 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001348 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001349 return;
1350 }
1351
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001352 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001353
1354 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1355 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001356 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001357
1358 GrTexture* texture;
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001359 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001360 if (NULL == texture) {
1361 return;
1362 }
1363
bsalomon@google.com452943d2011-10-31 17:37:14 +00001364 grPaint->setTexture(kBitmapTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001365
reed@google.com20efde72011-05-09 17:00:02 +00001366 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1367 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001368 GrRect paintRect;
bsalomon@google.com91832162012-03-08 19:53:02 +00001369 float wInv = 1.f / bitmap.width();
1370 float hInv = 1.f / bitmap.height();
1371 paintRect.setLTRB(SkFloatToScalar(srcRect.fLeft * wInv),
1372 SkFloatToScalar(srcRect.fTop * hInv),
1373 SkFloatToScalar(srcRect.fRight * wInv),
1374 SkFloatToScalar(srcRect.fBottom * hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001375
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001376 bool needsTextureDomain = false;
1377 if (GrSamplerState::kBilinear_Filter == sampler->getFilter())
1378 {
1379 // Need texture domain if drawing a sub rect.
1380 needsTextureDomain = srcRect.width() < bitmap.width() ||
1381 srcRect.height() < bitmap.height();
1382 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1383 // sampling is axis-aligned
1384 GrRect floatSrcRect, transformedRect;
1385 floatSrcRect.set(srcRect);
1386 SkMatrix srcToDeviceMatrix(m);
1387 srcToDeviceMatrix.postConcat(*draw.fMatrix);
1388 srcToDeviceMatrix.mapRect(&transformedRect, floatSrcRect);
1389
1390 if (hasAlignedSamples(floatSrcRect, transformedRect)) {
1391 // Samples are texel-aligned, so filtering is futile
1392 sampler->setFilter(GrSamplerState::kNearest_Filter);
1393 needsTextureDomain = false;
1394 } else {
1395 needsTextureDomain = needsTextureDomain &&
1396 mayColorBleed(floatSrcRect, transformedRect, m);
1397 }
1398 }
1399 }
1400
1401 GrRect textureDomain = GrRect::MakeEmpty();
1402
1403 if (needsTextureDomain) {
1404 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001405 GrScalar left, top, right, bottom;
1406 if (srcRect.width() > 1) {
1407 GrScalar border = GR_ScalarHalf / bitmap.width();
1408 left = paintRect.left() + border;
1409 right = paintRect.right() - border;
1410 } else {
1411 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1412 }
1413 if (srcRect.height() > 1) {
1414 GrScalar border = GR_ScalarHalf / bitmap.height();
1415 top = paintRect.top() + border;
1416 bottom = paintRect.bottom() - border;
1417 } else {
1418 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1419 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001420 textureDomain.setLTRB(left, top, right, bottom);
junov@google.com6acc9b32011-05-16 18:32:07 +00001421 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001422 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001423
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001424 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001425}
1426
reed@google.com8926b162012-03-23 15:36:36 +00001427static GrTexture* filter_texture(GrContext* context, GrTexture* texture,
1428 SkImageFilter* filter, const GrRect& rect) {
1429 GrAssert(filter);
1430
1431 SkSize blurSize;
1432 SkISize radius;
1433
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001434 GrTextureDesc desc;
1435 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1436 desc.fWidth = SkScalarCeilToInt(rect.width());
1437 desc.fHeight = SkScalarCeilToInt(rect.height());
1438 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
reed@google.com8926b162012-03-23 15:36:36 +00001439
1440 if (filter->asABlur(&blurSize)) {
1441 GrAutoScratchTexture temp1, temp2;
1442 texture = context->gaussianBlur(texture, &temp1, &temp2, rect,
1443 blurSize.width(),
1444 blurSize.height());
1445 texture->ref();
1446 } else if (filter->asADilate(&radius)) {
1447 GrAutoScratchTexture temp1(context, desc), temp2(context, desc);
1448 texture = context->applyMorphology(texture, rect,
1449 temp1.texture(), temp2.texture(),
bsalomon@google.comb505a122012-05-31 18:40:36 +00001450 GrContext::kDilate_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001451 radius);
1452 texture->ref();
1453 } else if (filter->asAnErode(&radius)) {
1454 GrAutoScratchTexture temp1(context, desc), temp2(context, desc);
1455 texture = context->applyMorphology(texture, rect,
1456 temp1.texture(), temp2.texture(),
bsalomon@google.comb505a122012-05-31 18:40:36 +00001457 GrContext::kErode_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001458 radius);
1459 texture->ref();
1460 }
1461 return texture;
1462}
1463
reed@google.comac10a2d2010-12-22 21:39:39 +00001464void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1465 int left, int top, const SkPaint& paint) {
1466 CHECK_SHOULD_DRAW(draw);
1467
reed@google.com8926b162012-03-23 15:36:36 +00001468 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001469 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1470 return;
1471 }
1472
reed@google.com76dd2772012-01-05 21:15:07 +00001473 int w = bitmap.width();
1474 int h = bitmap.height();
1475
bsalomon@google.com5782d712011-01-21 21:03:59 +00001476 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001477 if(!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001478 return;
1479 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001480
bsalomon@google.com5782d712011-01-21 21:03:59 +00001481 GrAutoMatrix avm(fContext, GrMatrix::I());
1482
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001483 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001484
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001485 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001486 sampler->reset();
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001487 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001488 grPaint.setTexture(kBitmapTextureIdx, texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001489
reed@google.com8926b162012-03-23 15:36:36 +00001490 SkImageFilter* filter = paint.getImageFilter();
1491 if (NULL != filter) {
1492 GrTexture* filteredTexture = filter_texture(fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001493 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001494 if (filteredTexture) {
1495 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1496 texture = filteredTexture;
1497 filteredTexture->unref();
1498 }
reed@google.com76dd2772012-01-05 21:15:07 +00001499 }
reed@google.com8926b162012-03-23 15:36:36 +00001500
bsalomon@google.com5782d712011-01-21 21:03:59 +00001501 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001502 GrRect::MakeXYWH(GrIntToScalar(left),
1503 GrIntToScalar(top),
1504 GrIntToScalar(w),
1505 GrIntToScalar(h)),
1506 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1507 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001508}
1509
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001510void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
reed@google.comac10a2d2010-12-22 21:39:39 +00001511 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001512 // clear of the source device must occur before CHECK_SHOULD_DRAW
1513 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1514 if (dev->fNeedClear) {
1515 // TODO: could check here whether we really need to draw at all
1516 dev->clear(0x0);
1517 }
1518
reed@google.comac10a2d2010-12-22 21:39:39 +00001519 CHECK_SHOULD_DRAW(draw);
1520
bsalomon@google.com5782d712011-01-21 21:03:59 +00001521 GrPaint grPaint;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001522 if (!dev->bindDeviceAsTexture(&grPaint) ||
bsalomon@google.com84405e02012-03-05 19:57:21 +00001523 !skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001524 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001525 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001526
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001527 GrTexture* devTex = grPaint.getTexture(0);
1528 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001529
reed@google.com8926b162012-03-23 15:36:36 +00001530 SkImageFilter* filter = paint.getImageFilter();
1531 if (NULL != filter) {
robertphillips@google.com8637a362012-04-10 18:32:35 +00001532 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
1533 SkIntToScalar(devTex->height()));
reed@google.com8926b162012-03-23 15:36:36 +00001534 GrTexture* filteredTexture = filter_texture(fContext, devTex, filter,
1535 rect);
1536 if (filteredTexture) {
1537 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1538 devTex = filteredTexture;
1539 filteredTexture->unref();
1540 }
1541 }
1542
bsalomon@google.com5782d712011-01-21 21:03:59 +00001543 const SkBitmap& bm = dev->accessBitmap(false);
1544 int w = bm.width();
1545 int h = bm.height();
1546
1547 GrAutoMatrix avm(fContext, GrMatrix::I());
1548
bsalomon@google.com97912912011-12-06 16:30:36 +00001549 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001550
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001551 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1552 GrIntToScalar(y),
1553 GrIntToScalar(w),
1554 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001555
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001556 // The device being drawn may not fill up its texture (saveLayer uses
1557 // the approximate ).
1558 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1559 GR_Scalar1 * h / devTex->height());
1560
1561 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001562}
1563
reed@google.com8926b162012-03-23 15:36:36 +00001564bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
reed@google.com76dd2772012-01-05 21:15:07 +00001565 SkSize size;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001566 SkISize radius;
1567 if (!filter->asABlur(&size) && !filter->asADilate(&radius) && !filter->asAnErode(&radius)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001568 return false;
1569 }
reed@google.com8926b162012-03-23 15:36:36 +00001570 return true;
1571}
1572
1573bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1574 const SkMatrix& ctm,
1575 SkBitmap* result, SkIPoint* offset) {
1576 // want explicitly our impl, so guard against a subclass of us overriding it
1577 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001578 return false;
1579 }
reed@google.com8926b162012-03-23 15:36:36 +00001580
1581 SkAutoLockPixels alp(src, !src.getTexture());
1582 if (!src.getTexture() && !src.readyToDraw()) {
1583 return false;
1584 }
1585
1586 GrPaint paint;
1587 paint.reset();
1588
1589 GrSamplerState* sampler = paint.textureSampler(kBitmapTextureIdx);
1590
1591 GrTexture* texture;
1592 SkAutoCachedTexture act(this, src, sampler, &texture);
1593
1594 result->setConfig(src.config(), src.width(), src.height());
robertphillips@google.com8637a362012-04-10 18:32:35 +00001595 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
1596 SkIntToScalar(src.height()));
reed@google.com8926b162012-03-23 15:36:36 +00001597 GrTexture* resultTexture = filter_texture(fContext, texture, filter, rect);
1598 if (resultTexture) {
1599 result->setPixelRef(new SkGrTexturePixelRef(resultTexture))->unref();
1600 resultTexture->unref();
1601 }
reed@google.com76dd2772012-01-05 21:15:07 +00001602 return true;
1603}
1604
reed@google.comac10a2d2010-12-22 21:39:39 +00001605///////////////////////////////////////////////////////////////////////////////
1606
1607// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001608static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001609 kTriangles_GrPrimitiveType,
1610 kTriangleStrip_GrPrimitiveType,
1611 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001612};
1613
1614void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1615 int vertexCount, const SkPoint vertices[],
1616 const SkPoint texs[], const SkColor colors[],
1617 SkXfermode* xmode,
1618 const uint16_t indices[], int indexCount,
1619 const SkPaint& paint) {
1620 CHECK_SHOULD_DRAW(draw);
1621
bsalomon@google.com5782d712011-01-21 21:03:59 +00001622 GrPaint grPaint;
1623 SkAutoCachedTexture act;
1624 // we ignore the shader if texs is null.
1625 if (NULL == texs) {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001626 if (!skPaint2GrPaintNoShader(paint,
1627 false,
1628 NULL == colors,
1629 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001630 return;
1631 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001632 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001633 if (!skPaint2GrPaintShader(this,
1634 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001635 NULL == colors,
1636 &act,
1637 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001638 return;
1639 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001640 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001641
1642 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001643 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001644 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1645#if 0
1646 return
1647#endif
1648 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001649 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001650
bsalomon@google.com498776a2011-08-16 19:20:44 +00001651 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1652 if (NULL != colors) {
1653 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001654 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001655 for (int i = 0; i < vertexCount; ++i) {
1656 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1657 }
1658 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001659 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001660 fContext->drawVertices(grPaint,
1661 gVertexMode2PrimitiveType[vmode],
1662 vertexCount,
1663 (GrPoint*) vertices,
1664 (GrPoint*) texs,
1665 colors,
1666 indices,
1667 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001668}
1669
1670///////////////////////////////////////////////////////////////////////////////
1671
1672static void GlyphCacheAuxProc(void* data) {
1673 delete (GrFontScaler*)data;
1674}
1675
1676static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1677 void* auxData;
1678 GrFontScaler* scaler = NULL;
1679 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1680 scaler = (GrFontScaler*)auxData;
1681 }
1682 if (NULL == scaler) {
1683 scaler = new SkGrFontScaler(cache);
1684 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1685 }
1686 return scaler;
1687}
1688
1689static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1690 SkFixed fx, SkFixed fy,
1691 const SkGlyph& glyph) {
1692 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1693
bungeman@google.com15865a72012-01-11 16:28:04 +00001694 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001695
1696 if (NULL == procs->fFontScaler) {
1697 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1698 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001699
bungeman@google.com15865a72012-01-11 16:28:04 +00001700 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1701 glyph.getSubXFixed(),
1702 glyph.getSubYFixed()),
1703 SkFixedFloorToFixed(fx),
1704 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001705 procs->fFontScaler);
1706}
1707
bsalomon@google.com5782d712011-01-21 21:03:59 +00001708SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001709
1710 // deferred allocation
1711 if (NULL == fDrawProcs) {
1712 fDrawProcs = new GrSkDrawProcs;
1713 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1714 fDrawProcs->fContext = fContext;
1715 }
1716
1717 // init our (and GL's) state
1718 fDrawProcs->fTextContext = context;
1719 fDrawProcs->fFontScaler = NULL;
1720 return fDrawProcs;
1721}
1722
1723void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1724 size_t byteLength, SkScalar x, SkScalar y,
1725 const SkPaint& paint) {
1726 CHECK_SHOULD_DRAW(draw);
1727
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001728 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001729 // this guy will just call our drawPath()
1730 draw.drawText((const char*)text, byteLength, x, y, paint);
1731 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001732 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001733
1734 GrPaint grPaint;
1735 SkAutoCachedTexture act;
1736
bsalomon@google.com84405e02012-03-05 19:57:21 +00001737 if (!skPaint2GrPaintShader(this,
1738 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001739 true,
1740 &act,
1741 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001742 return;
1743 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001744 GrTextContext::AutoFinish txtCtxAF(this->getTextContext(), fContext,
1745 grPaint, draw.fExtMatrix);
1746 myDraw.fProcs = this->initDrawForText(txtCtxAF.getTextContext());
reed@google.comac10a2d2010-12-22 21:39:39 +00001747 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1748 }
1749}
1750
1751void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1752 size_t byteLength, const SkScalar pos[],
1753 SkScalar constY, int scalarsPerPos,
1754 const SkPaint& paint) {
1755 CHECK_SHOULD_DRAW(draw);
1756
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001757 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001758 // this guy will just call our drawPath()
1759 draw.drawPosText((const char*)text, byteLength, pos, constY,
1760 scalarsPerPos, paint);
1761 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001762 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001763
1764 GrPaint grPaint;
1765 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001766 if (!skPaint2GrPaintShader(this,
1767 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001768 true,
1769 &act,
1770 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001771 return;
1772 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001773 GrTextContext::AutoFinish txtCtxAF(this->getTextContext(), fContext,
1774 grPaint, draw.fExtMatrix);
1775 myDraw.fProcs = this->initDrawForText(txtCtxAF.getTextContext());
reed@google.comac10a2d2010-12-22 21:39:39 +00001776 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1777 scalarsPerPos, paint);
1778 }
1779}
1780
1781void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1782 size_t len, const SkPath& path,
1783 const SkMatrix* m, const SkPaint& paint) {
1784 CHECK_SHOULD_DRAW(draw);
1785
1786 SkASSERT(draw.fDevice == this);
1787 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1788}
1789
1790///////////////////////////////////////////////////////////////////////////////
1791
reed@google.comf67e4cf2011-03-15 20:56:58 +00001792bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1793 if (!paint.isLCDRenderText()) {
1794 // we're cool with the paint as is
1795 return false;
1796 }
1797
1798 if (paint.getShader() ||
1799 paint.getXfermode() || // unless its srcover
1800 paint.getMaskFilter() ||
1801 paint.getRasterizer() ||
1802 paint.getColorFilter() ||
1803 paint.getPathEffect() ||
1804 paint.isFakeBoldText() ||
1805 paint.getStyle() != SkPaint::kFill_Style) {
1806 // turn off lcd
1807 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1808 flags->fHinting = paint.getHinting();
1809 return true;
1810 }
1811 // we're cool with the paint as is
1812 return false;
1813}
1814
reed@google.com75d939b2011-12-07 15:07:23 +00001815void SkGpuDevice::flush() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001816 DO_DEFERRED_CLEAR;
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001817 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001818}
1819
reed@google.comf67e4cf2011-03-15 20:56:58 +00001820///////////////////////////////////////////////////////////////////////////////
1821
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001822SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(
1823 const SkBitmap& bitmap,
1824 const GrSamplerState* sampler) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001825 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001826 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001827
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001828 if (!bitmap.isVolatile()) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001829 uint64_t key = bitmap.getGenerationID();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001830 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001831
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001832 GrTextureDesc desc;
1833 desc.fWidth = bitmap.width();
1834 desc.fHeight = bitmap.height();
1835 desc.fConfig = SkGr::BitmapConfig2PixelConfig(bitmap.config());
robertphillips@google.com56f22442012-06-08 14:21:26 +00001836 desc.fClientCacheID = key;
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001837
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001838 entry = ctx->findAndLockTexture(desc, 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 {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001844 entry = sk_gr_create_bitmap_texture(ctx, kUncached_CacheID,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001845 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 {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001860 uint64_t key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001861 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001862
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001863 GrTextureDesc desc;
1864 desc.fWidth = bitmap.width();
1865 desc.fHeight = bitmap.height();
1866 desc.fConfig = SkGr::BitmapConfig2PixelConfig(bitmap.config());
1867 desc.fClientCacheID = key;
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001868
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001869 return this->context()->isTextureInCache(desc, &sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001870}
1871
1872
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001873SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1874 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001875 bool isOpaque,
1876 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001877 GrTextureDesc desc;
1878 desc.fConfig = fRenderTarget->config();
1879 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1880 desc.fWidth = width;
1881 desc.fHeight = height;
1882 desc.fSampleCnt = fRenderTarget->numSamples();
1883
1884 GrContext::TextureCacheEntry cacheEntry;
1885 GrTexture* texture;
1886 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001887 // Skia's convention is to only clear a device if it is non-opaque.
1888 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001889
1890#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1891 // layers are never draw in repeat modes, so we can request an approx
1892 // match and ignore any padding.
1893 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1894 GrContext::kApprox_ScratchTexMatch :
1895 GrContext::kExact_ScratchTexMatch;
1896 cacheEntry = fContext->lockScratchTexture(desc, matchType);
1897 texture = cacheEntry.texture();
1898#else
1899 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1900 texture = tunref.get();
1901#endif
1902 if (texture) {
1903 return SkNEW_ARGS(SkGpuDevice,(fContext,
1904 texture,
1905 cacheEntry,
1906 needClear));
1907 } else {
1908 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1909 width, height);
1910 return NULL;
1911 }
1912}
1913
1914SkGpuDevice::SkGpuDevice(GrContext* context,
1915 GrTexture* texture,
1916 TexCache cacheEntry,
1917 bool needClear)
1918 : SkDevice(make_bitmap(context, texture->asRenderTarget())) {
1919 GrAssert(texture && texture->asRenderTarget());
1920 GrAssert(NULL == cacheEntry.texture() || texture == cacheEntry.texture());
1921 this->initFromRenderTarget(context, texture->asRenderTarget());
1922 fCache = cacheEntry;
1923 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001924}
1925
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001926GrTextContext* SkGpuDevice::getTextContext() {
1927 if (NULL == fTextContext) {
1928 fTextContext = new GrDefaultTextContext();
1929 }
1930 return fTextContext;
1931}