blob: b62e5be39a2287a7d7e39402a2df0edfaba9f5de [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
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000260 SkSafeUnref(fTexture);
261 SkSafeUnref(fRenderTarget);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000262 if (fCache.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000263 GrAssert(NULL != fTexture);
264 GrAssert(fRenderTarget == fTexture->asRenderTarget());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000265 fContext->unlockTexture(fCache);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000266 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000267 fContext->unref();
bsalomon@google.comf4a9c822012-03-16 14:02:46 +0000268
269 if (NULL != fTextContext) {
270 fTextContext->unref();
271 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000272}
273
reed@google.comac10a2d2010-12-22 21:39:39 +0000274///////////////////////////////////////////////////////////////////////////////
275
276void SkGpuDevice::makeRenderTargetCurrent() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000277 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000278 fContext->setRenderTarget(fRenderTarget);
279 fContext->flush(true);
280 fNeedPrepareRenderTarget = true;
281}
282
283///////////////////////////////////////////////////////////////////////////////
284
bsalomon@google.comc4364992011-11-07 15:54:49 +0000285namespace {
286GrPixelConfig config8888_to_gr_config(SkCanvas::Config8888 config8888) {
287 switch (config8888) {
288 case SkCanvas::kNative_Premul_Config8888:
289 return kSkia8888_PM_GrPixelConfig;
290 case SkCanvas::kNative_Unpremul_Config8888:
291 return kSkia8888_UPM_GrPixelConfig;
292 case SkCanvas::kBGRA_Premul_Config8888:
293 return kBGRA_8888_PM_GrPixelConfig;
294 case SkCanvas::kBGRA_Unpremul_Config8888:
295 return kBGRA_8888_UPM_GrPixelConfig;
296 case SkCanvas::kRGBA_Premul_Config8888:
297 return kRGBA_8888_PM_GrPixelConfig;
298 case SkCanvas::kRGBA_Unpremul_Config8888:
299 return kRGBA_8888_UPM_GrPixelConfig;
300 default:
301 GrCrash("Unexpected Config8888.");
302 return kSkia8888_PM_GrPixelConfig;
303 }
304}
305}
306
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000307bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
308 int x, int y,
309 SkCanvas::Config8888 config8888) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000310 DO_DEFERRED_CLEAR;
bsalomon@google.com910267d2011-11-02 20:06:25 +0000311 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
312 SkASSERT(!bitmap.isNull());
313 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000314
bsalomon@google.com910267d2011-11-02 20:06:25 +0000315 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000316 GrPixelConfig config;
317 config = config8888_to_gr_config(config8888);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000318 return fContext->readRenderTargetPixels(fRenderTarget,
319 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000320 bitmap.width(),
321 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000322 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000323 bitmap.getPixels(),
324 bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000325}
326
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000327void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
328 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000329 SkAutoLockPixels alp(bitmap);
330 if (!bitmap.readyToDraw()) {
331 return;
332 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000333
334 GrPixelConfig config;
335 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
336 config = config8888_to_gr_config(config8888);
337 } else {
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000338 config= SkGr::BitmapConfig2PixelConfig(bitmap.config());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000339 }
340
bsalomon@google.com6f379512011-11-16 20:36:03 +0000341 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
342 config, bitmap.getPixels(), bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000343}
344
345///////////////////////////////////////////////////////////////////////////////
346
347static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000348 const SkClipStack& clipStack,
reed@google.com6f8f2922011-03-04 22:27:10 +0000349 const SkRegion& clipRegion,
350 const SkIPoint& origin) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000351 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000352
353 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000354 iter.reset(clipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000355 const SkIRect& skBounds = clipRegion.getBounds();
356 GrRect bounds;
357 bounds.setLTRB(GrIntToScalar(skBounds.fLeft),
358 GrIntToScalar(skBounds.fTop),
359 GrIntToScalar(skBounds.fRight),
360 GrIntToScalar(skBounds.fBottom));
reed@google.com6f8f2922011-03-04 22:27:10 +0000361 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
362 &bounds);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000363 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000364}
365
366// call this ever each draw call, to ensure that the context reflects our state,
367// and not the state from some other canvas/device
368void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
369 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000370 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000371
372 fContext->setRenderTarget(fRenderTarget);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000373 SkASSERT(draw.fClipStack);
374 convert_matrixclip(fContext, *draw.fMatrix,
reed@google.com6f8f2922011-03-04 22:27:10 +0000375 *draw.fClipStack, *draw.fClip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000376 fNeedPrepareRenderTarget = false;
377 }
378}
379
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000380void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
381 const SkClipStack& clipStack) {
382 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
383 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000384 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000385}
386
387void SkGpuDevice::gainFocus(SkCanvas* canvas, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000388 const SkRegion& clip, const SkClipStack& clipStack) {
389
reed@google.comac10a2d2010-12-22 21:39:39 +0000390 fContext->setRenderTarget(fRenderTarget);
391
bsalomon@google.comd302f142011-03-03 13:54:13 +0000392 this->INHERITED::gainFocus(canvas, matrix, clip, clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000393
reed@google.com6f8f2922011-03-04 22:27:10 +0000394 convert_matrixclip(fContext, matrix, clipStack, clip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000395
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000396 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000397}
398
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000399SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000400 DO_DEFERRED_CLEAR;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000401 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000402}
403
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000404bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000405 if (NULL != fTexture) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000406 paint->setTexture(kBitmapTextureIdx, fTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000407 return true;
408 }
409 return false;
410}
411
412///////////////////////////////////////////////////////////////////////////////
413
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000414SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
415SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
416SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
417SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
418SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
419 shader_type_mismatch);
420SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 4, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000421
bsalomon@google.com84405e02012-03-05 19:57:21 +0000422namespace {
423
424// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
425// justAlpha indicates that skPaint's alpha should be used rather than the color
426// Callers may subsequently modify the GrPaint. Setting constantColor indicates
427// that the final paint will draw the same color at every pixel. This allows
428// an optimization where the the color filter can be applied to the skPaint's
429// color once while converting to GrPain and then ignored.
430inline bool skPaint2GrPaintNoShader(const SkPaint& skPaint,
431 bool justAlpha,
432 bool constantColor,
433 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000434
435 grPaint->fDither = skPaint.isDither();
436 grPaint->fAntiAlias = skPaint.isAntiAlias();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000437 grPaint->fCoverage = 0xFF;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000438
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000439 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
440 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000441
442 SkXfermode* mode = skPaint.getXfermode();
443 if (mode) {
444 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000445 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000446#if 0
447 return false;
448#endif
449 }
450 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000451 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
452 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
453
bsalomon@google.com5782d712011-01-21 21:03:59 +0000454 if (justAlpha) {
455 uint8_t alpha = skPaint.getAlpha();
456 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000457 // justAlpha is currently set to true only if there is a texture,
458 // so constantColor should not also be true.
459 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000460 } else {
461 grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000462 grPaint->setTexture(kShaderTextureIdx, NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000463 }
Scroggo97c88c22011-05-11 14:05:25 +0000464 SkColorFilter* colorFilter = skPaint.getColorFilter();
465 SkColor color;
466 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000467 SkScalar matrix[20];
Scroggo97c88c22011-05-11 14:05:25 +0000468 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000469 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000470 if (!constantColor) {
471 grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color);
472 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000473 } else {
474 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
475 grPaint->fColor = SkGr::SkColor2GrColor(filtered);
senorblanco@chromium.orgb3c20fa2012-01-03 21:20:19 +0000476 grPaint->resetColorFilter();
Scroggod757df22011-05-16 13:11:16 +0000477 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000478 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
479 grPaint->fColorMatrixEnabled = true;
480 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
481 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
482 } else {
483 grPaint->resetColorFilter();
Scroggo97c88c22011-05-11 14:05:25 +0000484 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000485 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000486}
487
bsalomon@google.com84405e02012-03-05 19:57:21 +0000488// This function is similar to skPaint2GrPaintNoShader but also converts
489// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
490// be used is set on grPaint and returned in param act. constantColor has the
491// same meaning as in skPaint2GrPaintNoShader.
492inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
493 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000494 bool constantColor,
495 SkGpuDevice::SkAutoCachedTexture* act,
496 GrPaint* grPaint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000497
bsalomon@google.com5782d712011-01-21 21:03:59 +0000498 SkASSERT(NULL != act);
reed@google.comac10a2d2010-12-22 21:39:39 +0000499
bsalomon@google.com5782d712011-01-21 21:03:59 +0000500 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000501 if (NULL == shader) {
bsalomon@google.com84405e02012-03-05 19:57:21 +0000502 return skPaint2GrPaintNoShader(skPaint,
503 false,
504 constantColor,
505 grPaint);
506 } else if (!skPaint2GrPaintNoShader(skPaint, true, false, grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000507 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000508 }
509
reed@google.comac10a2d2010-12-22 21:39:39 +0000510 SkBitmap bitmap;
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000511 SkMatrix* matrix = grPaint->textureSampler(kShaderTextureIdx)->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000512 SkShader::TileMode tileModes[2];
513 SkScalar twoPointParams[3];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000514 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000515 tileModes, twoPointParams);
516
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000517 if (SkShader::kNone_BitmapType == bmptype) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000518 SkShader::GradientInfo info;
519 SkColor color;
520
521 info.fColors = &color;
522 info.fColorOffsets = NULL;
523 info.fColorCount = 1;
524 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
525 SkPaint copy(skPaint);
526 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000527 // modulate the paint alpha by the shader's solid color alpha
528 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
529 copy.setColor(SkColorSetA(color, newA));
bsalomon@google.com84405e02012-03-05 19:57:21 +0000530 return skPaint2GrPaintNoShader(copy,
531 false,
532 constantColor,
533 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000534 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000535 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000536 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000537 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000538 switch (bmptype) {
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000539 case SkShader::kRadial_BitmapType:
540 sampler->setCustomStage(new GrRadialGradient())->unref();
541 sampler->setFilter(GrSamplerState::kBilinear_Filter);
542 break;
543 case SkShader::kSweep_BitmapType:
544 sampler->setCustomStage(new GrSweepGradient())->unref();
545 sampler->setFilter(GrSamplerState::kBilinear_Filter);
546 break;
547 case SkShader::kTwoPointRadial_BitmapType:
548 sampler->setCustomStage(new
549 GrRadial2Gradient(twoPointParams[0],
550 twoPointParams[1],
551 twoPointParams[2] < 0))->unref();
552 sampler->setFilter(GrSamplerState::kBilinear_Filter);
553 break;
554 default:
555 if (skPaint.isFilterBitmap()) {
556 sampler->setFilter(GrSamplerState::kBilinear_Filter);
557 } else {
558 sampler->setFilter(GrSamplerState::kNearest_Filter);
559 }
560 break;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000561 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000562 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
563 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000564
bsalomon@google.com84405e02012-03-05 19:57:21 +0000565 GrTexture* texture = act->set(dev, bitmap, sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000566 if (NULL == texture) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000567 SkDebugf("Couldn't convert bitmap to texture.\n");
568 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000569 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000570 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000571
572 // since our texture coords will be in local space, we wack the texture
573 // matrix to map them back into 0...1 before we load it
574 SkMatrix localM;
575 if (shader->getLocalMatrix(&localM)) {
576 SkMatrix inverse;
577 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000578 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000579 }
580 }
581 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000582 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
583 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000584 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000585 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000586 GrScalar s = SkFloatToScalar(1.f / bitmap.width());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000587 matrix->postScale(s, s);
reed@google.comac10a2d2010-12-22 21:39:39 +0000588 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000589
590 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000591}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000592}
reed@google.comac10a2d2010-12-22 21:39:39 +0000593
594///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000595
bsalomon@google.com398109c2011-04-14 18:40:27 +0000596void SkGpuDevice::clear(SkColor color) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000597 fContext->setRenderTarget(fRenderTarget);
bsalomon@google.com31a58402011-04-27 21:00:02 +0000598 fContext->clear(NULL, color);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000599}
600
reed@google.comac10a2d2010-12-22 21:39:39 +0000601void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
602 CHECK_SHOULD_DRAW(draw);
603
bsalomon@google.com5782d712011-01-21 21:03:59 +0000604 GrPaint grPaint;
605 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000606 if (!skPaint2GrPaintShader(this,
607 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000608 true,
609 &act,
610 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000611 return;
612 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000613
614 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000615}
616
617// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000618static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000619 kPoints_GrPrimitiveType,
620 kLines_GrPrimitiveType,
621 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000622};
623
624void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000625 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000626 CHECK_SHOULD_DRAW(draw);
627
628 SkScalar width = paint.getStrokeWidth();
629 if (width < 0) {
630 return;
631 }
632
633 // we only handle hairlines here, else we let the SkDraw call our drawPath()
634 if (width > 0) {
635 draw.drawPoints(mode, count, pts, paint, true);
636 return;
637 }
638
bsalomon@google.com5782d712011-01-21 21:03:59 +0000639 GrPaint grPaint;
640 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000641 if (!skPaint2GrPaintShader(this,
642 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000643 true,
644 &act,
645 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000646 return;
647 }
648
bsalomon@google.com5782d712011-01-21 21:03:59 +0000649 fContext->drawVertices(grPaint,
650 gPointMode2PrimtiveType[mode],
651 count,
652 (GrPoint*)pts,
653 NULL,
654 NULL,
655 NULL,
656 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000657}
658
reed@google.comc9aa5872011-04-05 21:05:37 +0000659///////////////////////////////////////////////////////////////////////////////
660
reed@google.comac10a2d2010-12-22 21:39:39 +0000661void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
662 const SkPaint& paint) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000663 CHECK_SHOULD_DRAW(draw);
664
bungeman@google.com79bd8772011-07-18 15:34:08 +0000665 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000666 SkScalar width = paint.getStrokeWidth();
667
668 /*
669 We have special code for hairline strokes, miter-strokes, and fills.
670 Anything else we just call our path code.
671 */
672 bool usePath = doStroke && width > 0 &&
673 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000674 // another two reasons we might need to call drawPath...
675 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000676 usePath = true;
677 }
reed@google.com67db6642011-05-26 11:46:35 +0000678 // until we aa rotated rects...
679 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
680 usePath = true;
681 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000682 // small miter limit means right angles show bevel...
683 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
684 paint.getStrokeMiter() < SK_ScalarSqrt2)
685 {
686 usePath = true;
687 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000688 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000689 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
690 usePath = true;
691 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000692
693 if (usePath) {
694 SkPath path;
695 path.addRect(rect);
696 this->drawPath(draw, path, paint, NULL, true);
697 return;
698 }
699
700 GrPaint grPaint;
701 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000702 if (!skPaint2GrPaintShader(this,
703 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000704 true,
705 &act,
706 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000707 return;
708 }
reed@google.com20efde72011-05-09 17:00:02 +0000709 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000710}
711
reed@google.com69302852011-02-16 18:08:07 +0000712#include "SkMaskFilter.h"
713#include "SkBounder.h"
714
bsalomon@google.com85003222012-03-28 14:44:37 +0000715///////////////////////////////////////////////////////////////////////////////
716
717// helpers for applying mask filters
718namespace {
719
720GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000721 switch (fillType) {
722 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000723 return kWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000724 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000725 return kEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000726 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000727 return kInverseWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000728 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000729 return kInverseEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000730 default:
731 SkDebugf("Unsupported path fill type\n");
bsalomon@google.com47059542012-06-06 20:51:20 +0000732 return kHairLine_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000733 }
734}
735
bsalomon@google.com85003222012-03-28 14:44:37 +0000736// We prefer to blur small rect with small radius via CPU.
737#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
738#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
739inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
740 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
741 rect.height() <= MIN_GPU_BLUR_SIZE &&
742 radius <= MIN_GPU_BLUR_RADIUS) {
743 return true;
744 }
745 return false;
746}
747
748bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
749 SkMaskFilter* filter, const SkMatrix& matrix,
750 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000751 GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000752#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000753 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000754#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000755 SkMaskFilter::BlurInfo info;
756 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000757 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000758 return false;
759 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000760 SkScalar radius = info.fIgnoreTransform ? info.fRadius
761 : matrix.mapRadius(info.fRadius);
762 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000763 if (radius <= 0) {
764 return false;
765 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000766
767 SkRect srcRect = path.getBounds();
768 if (shouldDrawBlurWithCPU(srcRect, radius)) {
769 return false;
770 }
771
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000772 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000773 float sigma3 = sigma * 3.0f;
774
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000775 SkRect clipRect;
776 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000777
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000778 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000779 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
780 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000781 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000782 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000783 SkIRect finalIRect;
784 finalRect.roundOut(&finalIRect);
785 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000786 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000787 }
788 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000789 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000790 }
791 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000792 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000793 GrTextureDesc desc;
794 desc.fFlags = kRenderTarget_GrTextureFlagBit;
795 desc.fWidth = SkScalarCeilToInt(srcRect.width());
796 desc.fHeight = SkScalarCeilToInt(srcRect.height());
797 // We actually only need A8, but it often isn't supported as a
798 // render target so default to RGBA_8888
799 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000800
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000801 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
802 desc.fConfig = kAlpha_8_GrPixelConfig;
803 }
804
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000805 GrAutoScratchTexture pathEntry(context, desc);
806 GrTexture* pathTexture = pathEntry.texture();
807 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000808 return false;
809 }
810 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000811 // Once this code moves into GrContext, this should be changed to use
812 // an AutoClipRestore.
813 GrClip oldClip = context->getClip();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000814 context->setRenderTarget(pathTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000815 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000816 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000817 GrPaint tempPaint;
818 tempPaint.reset();
819
820 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000821 tempPaint.fAntiAlias = grp->fAntiAlias;
822 if (tempPaint.fAntiAlias) {
823 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
824 // blend coeff of zero requires dual source blending support in order
825 // to properly blend partially covered pixels. This means the AA
826 // code path may not be taken. So we use a dst blend coeff of ISA. We
827 // could special case AA draws to a dst surface with known alpha=0 to
828 // use a zero dst coeff when dual source blending isn't available.
bsalomon@google.com47059542012-06-06 20:51:20 +0000829 tempPaint.fSrcBlendCoeff = kOne_GrBlendCoeff;
830 tempPaint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000831 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000832 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000833 context->drawPath(tempPaint, path, pathFillType, &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000834
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000835 GrAutoScratchTexture temp1, temp2;
836 // If we're doing a normal blur, we can clobber the pathTexture in the
837 // gaussianBlur. Otherwise, we need to save it for later compositing.
838 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000839 GrTexture* blurTexture = context->gaussianBlur(pathTexture,
840 &temp1,
841 isNormalBlur ? NULL : &temp2,
842 srcRect, sigma, sigma);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000843
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000844 if (!isNormalBlur) {
845 GrPaint paint;
846 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000847 paint.textureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000848 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
849 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000850 // Blend pathTexture over blurTexture.
851 context->setRenderTarget(blurTexture->asRenderTarget());
852 paint.setTexture(0, pathTexture);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000853 if (SkMaskFilter::kInner_BlurType == blurType) {
854 // inner: dst = dst * src
bsalomon@google.com47059542012-06-06 20:51:20 +0000855 paint.fSrcBlendCoeff = kDC_GrBlendCoeff;
856 paint.fDstBlendCoeff = kZero_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000857 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
858 // solid: dst = src + dst - src * dst
859 // = (1 - dst) * src + 1 * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000860 paint.fSrcBlendCoeff = kIDC_GrBlendCoeff;
861 paint.fDstBlendCoeff = kOne_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000862 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
863 // outer: dst = dst * (1 - src)
864 // = 0 * src + (1 - src) * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000865 paint.fSrcBlendCoeff = kZero_GrBlendCoeff;
866 paint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000867 }
868 context->drawRect(paint, srcRect);
869 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000870 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000871 context->setClip(oldClip);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000872
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000873 if (grp->hasTextureOrMask()) {
874 GrMatrix inverse;
875 if (!matrix.invert(&inverse)) {
876 return false;
877 }
878 grp->preConcatActiveSamplerMatrices(inverse);
879 }
880
881 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
882 // we assume the last mask index is available for use
883 GrAssert(NULL == grp->getMask(MASK_IDX));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000884 grp->setMask(MASK_IDX, blurTexture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000885 grp->maskSampler(MASK_IDX)->reset();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000886
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000887 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
888 -finalRect.fTop);
889 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
890 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000891 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000892 return true;
893}
894
bsalomon@google.com85003222012-03-28 14:44:37 +0000895bool drawWithMaskFilter(GrContext* context, const SkPath& path,
896 SkMaskFilter* filter, const SkMatrix& matrix,
897 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000898 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000899 SkMask srcM, dstM;
900
901 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000902 SkMask::kComputeBoundsAndRenderImage_CreateMode,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000903 style)) {
reed@google.com69302852011-02-16 18:08:07 +0000904 return false;
905 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000906 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000907
908 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
909 return false;
910 }
911 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000912 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000913
914 if (clip.quickReject(dstM.fBounds)) {
915 return false;
916 }
917 if (bounder && !bounder->doIRect(dstM.fBounds)) {
918 return false;
919 }
920
921 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
922 // the current clip (and identity matrix) and grpaint settings
923
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000924 // used to compute inverse view, if necessary
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000925 GrMatrix ivm = matrix;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000926
reed@google.com0c219b62011-02-16 21:31:18 +0000927 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +0000928
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000929 GrTextureDesc desc;
930 desc.fWidth = dstM.fBounds.width();
931 desc.fHeight = dstM.fBounds.height();
932 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +0000933
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000934 GrAutoScratchTexture ast(context, desc);
935 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000936
reed@google.com69302852011-02-16 18:08:07 +0000937 if (NULL == texture) {
938 return false;
939 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000940 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000941 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000942
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000943 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
944 grp->preConcatActiveSamplerMatrices(ivm);
945 }
946
947 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
948 // we assume the last mask index is available for use
949 GrAssert(NULL == grp->getMask(MASK_IDX));
950 grp->setMask(MASK_IDX, texture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000951 grp->maskSampler(MASK_IDX)->reset();
reed@google.com69302852011-02-16 18:08:07 +0000952
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000953 GrRect d;
954 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +0000955 GrIntToScalar(dstM.fBounds.fTop),
956 GrIntToScalar(dstM.fBounds.fRight),
957 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000958
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000959 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
960 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
961 -dstM.fBounds.fTop*SK_Scalar1);
962 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000963 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000964 return true;
965}
reed@google.com69302852011-02-16 18:08:07 +0000966
bsalomon@google.com85003222012-03-28 14:44:37 +0000967}
968
969///////////////////////////////////////////////////////////////////////////////
970
reed@google.com0c219b62011-02-16 21:31:18 +0000971void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000972 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000973 bool pathIsMutable) {
974 CHECK_SHOULD_DRAW(draw);
975
reed@google.comfe626382011-09-21 13:50:35 +0000976 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000977
bsalomon@google.com5782d712011-01-21 21:03:59 +0000978 GrPaint grPaint;
979 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000980 if (!skPaint2GrPaintShader(this,
981 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000982 true,
983 &act,
984 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000985 return;
986 }
987
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000988 // can we cheat, and threat a thin stroke as a hairline w/ coverage
989 // if we can, we draw lots faster (raster device does this same test)
990 SkScalar hairlineCoverage;
991 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
992 doFill = false;
993 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
994 grPaint.fCoverage);
995 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000996
reed@google.comfe626382011-09-21 13:50:35 +0000997 // If we have a prematrix, apply it to the path, optimizing for the case
998 // where the original path can in fact be modified in place (even though
999 // its parameter type is const).
1000 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1001 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001002
1003 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001004 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001005
reed@google.come3445642011-02-16 23:20:39 +00001006 if (!pathIsMutable) {
1007 result = &tmpPath;
1008 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001009 }
reed@google.come3445642011-02-16 23:20:39 +00001010 // should I push prePathMatrix on our MV stack temporarily, instead
1011 // of applying it here? See SkDraw.cpp
1012 pathPtr->transform(*prePathMatrix, result);
1013 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001014 }
reed@google.com0c219b62011-02-16 21:31:18 +00001015 // at this point we're done with prePathMatrix
1016 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001017
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001018 if (paint.getPathEffect() ||
1019 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001020 // it is safe to use tmpPath here, even if we already used it for the
1021 // prepathmatrix, since getFillPath can take the same object for its
1022 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001023 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001024 pathPtr = &tmpPath;
1025 }
1026
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001027 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001028 // avoid possibly allocating a new path in transform if we can
1029 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1030
1031 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001032 pathPtr->transform(*draw.fMatrix, devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001033 GrPathFill pathFillType = doFill ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001034 skToGrFillType(devPathPtr->getFillType()) : kHairLine_GrPathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001035 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001036 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001037 &grPaint, pathFillType)) {
1038 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
1039 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001040 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001041 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001042 &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001043 }
reed@google.com69302852011-02-16 18:08:07 +00001044 return;
1045 }
reed@google.com69302852011-02-16 18:08:07 +00001046
bsalomon@google.com47059542012-06-06 20:51:20 +00001047 GrPathFill fill = kHairLine_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001048
reed@google.com0c219b62011-02-16 21:31:18 +00001049 if (doFill) {
1050 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001051 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001052 fill = kWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001053 break;
1054 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001055 fill = kEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001056 break;
1057 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001058 fill = kInverseWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001059 break;
1060 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001061 fill = kInverseEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001062 break;
1063 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001064 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001065 return;
1066 }
1067 }
1068
reed@google.com07f3ee12011-05-16 17:21:57 +00001069 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001070}
1071
bsalomon@google.comfb309512011-11-30 14:13:48 +00001072namespace {
1073
1074inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1075 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1076 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1077 return tilesX * tilesY;
1078}
1079
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001080inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001081 const SkIRect* srcRectPtr,
1082 int maxTextureSize) {
1083 static const int kSmallTileSize = 1 << 10;
1084 if (maxTextureSize <= kSmallTileSize) {
1085 return maxTextureSize;
1086 }
1087
1088 size_t maxTexTotalTileSize;
1089 size_t smallTotalTileSize;
1090
1091 if (NULL == srcRectPtr) {
1092 int w = bitmap.width();
1093 int h = bitmap.height();
1094 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1095 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1096 } else {
1097 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1098 srcRectPtr->fTop,
1099 srcRectPtr->fRight,
1100 srcRectPtr->fBottom,
1101 maxTextureSize);
1102 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1103 srcRectPtr->fTop,
1104 srcRectPtr->fRight,
1105 srcRectPtr->fBottom,
1106 kSmallTileSize);
1107 }
1108 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1109 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1110
1111 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1112 return kSmallTileSize;
1113 } else {
1114 return maxTextureSize;
1115 }
1116}
1117}
1118
1119bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1120 const GrSamplerState& sampler,
1121 const SkIRect* srcRectPtr,
1122 int* tileSize) const {
1123 SkASSERT(NULL != tileSize);
1124
1125 // if bitmap is explictly texture backed then just use the texture
1126 if (NULL != bitmap.getTexture()) {
1127 return false;
1128 }
1129 // if it's larger than the max texture size, then we have no choice but
1130 // tiling
1131 const int maxTextureSize = fContext->getMaxTextureSize();
1132 if (bitmap.width() > maxTextureSize ||
1133 bitmap.height() > maxTextureSize) {
1134 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1135 return true;
1136 }
1137 // if we are going to have to draw the whole thing, then don't tile
1138 if (NULL == srcRectPtr) {
1139 return false;
1140 }
1141 // if the entire texture is already in our cache then no reason to tile it
1142 if (this->isBitmapInTextureCache(bitmap, sampler)) {
1143 return false;
1144 }
1145
1146 // At this point we know we could do the draw by uploading the entire bitmap
1147 // as a texture. However, if the texture would be large compared to the
1148 // cache size and we don't require most of it for this draw then tile to
1149 // reduce the amount of upload and cache spill.
1150
1151 // assumption here is that sw bitmap size is a good proxy for its size as
1152 // a texture
1153 size_t bmpSize = bitmap.getSize();
1154 size_t cacheSize;
1155 fContext->getTextureCacheLimits(NULL, &cacheSize);
1156 if (bmpSize < cacheSize / 2) {
1157 return false;
1158 }
1159
1160 SkFixed fracUsed =
1161 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1162 (srcRectPtr->height() << 16) / bitmap.height());
1163 if (fracUsed <= SK_FixedHalf) {
1164 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1165 return true;
1166 } else {
1167 return false;
1168 }
1169}
1170
reed@google.comac10a2d2010-12-22 21:39:39 +00001171void SkGpuDevice::drawBitmap(const SkDraw& draw,
1172 const SkBitmap& bitmap,
1173 const SkIRect* srcRectPtr,
1174 const SkMatrix& m,
1175 const SkPaint& paint) {
1176 CHECK_SHOULD_DRAW(draw);
1177
1178 SkIRect srcRect;
1179 if (NULL == srcRectPtr) {
1180 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1181 } else {
1182 srcRect = *srcRectPtr;
1183 }
1184
junov@google.comd935cfb2011-06-27 20:48:23 +00001185 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001186 // Convert the bitmap to a shader so that the rect can be drawn
1187 // through drawRect, which supports mask filters.
1188 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001189 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001190 if (srcRectPtr) {
1191 if (!bitmap.extractSubset(&tmp, srcRect)) {
1192 return; // extraction failed
1193 }
1194 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001195 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001196 }
1197 SkPaint paintWithTexture(paint);
1198 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1199 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001200 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001201 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001202
junov@google.com1d329782011-07-28 20:10:09 +00001203 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001204 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001205 // also affect the behavior of the mask filter.
1206 SkMatrix drawMatrix;
1207 drawMatrix.setConcat(*draw.fMatrix, m);
1208 SkDraw transformedDraw(draw);
1209 transformedDraw.fMatrix = &drawMatrix;
1210
1211 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1212
junov@google.comd935cfb2011-06-27 20:48:23 +00001213 return;
1214 }
1215
bsalomon@google.com5782d712011-01-21 21:03:59 +00001216 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001217 if (!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001218 return;
1219 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001220 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001221 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001222 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001223 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001224 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001225 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001226
bsalomon@google.comfb309512011-11-30 14:13:48 +00001227 int tileSize;
1228 if (!this->shouldTileBitmap(bitmap, *sampler, srcRectPtr, &tileSize)) {
1229 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001230 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001231 return;
1232 }
1233
1234 // undo the translate done by SkCanvas
1235 int DX = SkMax32(0, srcRect.fLeft);
1236 int DY = SkMax32(0, srcRect.fTop);
1237 // compute clip bounds in local coordinates
1238 SkIRect clipRect;
1239 {
1240 SkRect r;
1241 r.set(draw.fClip->getBounds());
1242 SkMatrix matrix, inverse;
1243 matrix.setConcat(*draw.fMatrix, m);
1244 if (!matrix.invert(&inverse)) {
1245 return;
1246 }
1247 inverse.mapRect(&r);
1248 r.roundOut(&clipRect);
1249 // apply the canvas' translate to our local clip
1250 clipRect.offset(DX, DY);
1251 }
1252
bsalomon@google.comfb309512011-11-30 14:13:48 +00001253 int nx = bitmap.width() / tileSize;
1254 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001255 for (int x = 0; x <= nx; x++) {
1256 for (int y = 0; y <= ny; y++) {
1257 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001258 tileR.set(x * tileSize, y * tileSize,
1259 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001260 if (!SkIRect::Intersects(tileR, clipRect)) {
1261 continue;
1262 }
1263
1264 SkIRect srcR = tileR;
1265 if (!srcR.intersect(srcRect)) {
1266 continue;
1267 }
1268
1269 SkBitmap tmpB;
1270 if (bitmap.extractSubset(&tmpB, tileR)) {
1271 // now offset it to make it "local" to our tmp bitmap
1272 srcR.offset(-tileR.fLeft, -tileR.fTop);
1273
1274 SkMatrix tmpM(m);
1275 {
1276 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1277 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1278 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1279 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001280 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001281 }
1282 }
1283 }
1284}
1285
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001286namespace {
1287
1288bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1289 // detect pixel disalignment
1290 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1291 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1292 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1293 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1294 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1295 COLOR_BLEED_TOLERANCE &&
1296 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1297 COLOR_BLEED_TOLERANCE) {
1298 return true;
1299 }
1300 return false;
1301}
1302
1303bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1304 const SkMatrix& m) {
1305 // Only gets called if hasAlignedSamples returned false.
1306 // So we can assume that sampling is axis aligned but not texel aligned.
1307 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
1308 SkRect innerSrcRect(srcRect), innerTransformedRect,
1309 outerTransformedRect(transformedRect);
1310 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1311 m.mapRect(&innerTransformedRect, innerSrcRect);
1312
1313 // The gap between outerTransformedRect and innerTransformedRect
1314 // represents the projection of the source border area, which is
1315 // problematic for color bleeding. We must check whether any
1316 // destination pixels sample the border area.
1317 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1318 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1319 SkIRect outer, inner;
1320 outerTransformedRect.round(&outer);
1321 innerTransformedRect.round(&inner);
1322 // If the inner and outer rects round to the same result, it means the
1323 // border does not overlap any pixel centers. Yay!
1324 return inner != outer;
1325}
1326
1327} // unnamed namespace
1328
reed@google.comac10a2d2010-12-22 21:39:39 +00001329/*
1330 * This is called by drawBitmap(), which has to handle images that may be too
1331 * large to be represented by a single texture.
1332 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001333 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1334 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001335 */
1336void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1337 const SkBitmap& bitmap,
1338 const SkIRect& srcRect,
1339 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001340 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001341 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1342 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001343
reed@google.com9c49bc32011-07-07 13:42:37 +00001344 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001345 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001346 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001347 return;
1348 }
1349
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001350 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001351
1352 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1353 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001354 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001355
1356 GrTexture* texture;
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001357 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001358 if (NULL == texture) {
1359 return;
1360 }
1361
bsalomon@google.com452943d2011-10-31 17:37:14 +00001362 grPaint->setTexture(kBitmapTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001363
reed@google.com20efde72011-05-09 17:00:02 +00001364 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1365 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001366 GrRect paintRect;
bsalomon@google.com91832162012-03-08 19:53:02 +00001367 float wInv = 1.f / bitmap.width();
1368 float hInv = 1.f / bitmap.height();
1369 paintRect.setLTRB(SkFloatToScalar(srcRect.fLeft * wInv),
1370 SkFloatToScalar(srcRect.fTop * hInv),
1371 SkFloatToScalar(srcRect.fRight * wInv),
1372 SkFloatToScalar(srcRect.fBottom * hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001373
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001374 bool needsTextureDomain = false;
1375 if (GrSamplerState::kBilinear_Filter == sampler->getFilter())
1376 {
1377 // Need texture domain if drawing a sub rect.
1378 needsTextureDomain = srcRect.width() < bitmap.width() ||
1379 srcRect.height() < bitmap.height();
1380 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1381 // sampling is axis-aligned
1382 GrRect floatSrcRect, transformedRect;
1383 floatSrcRect.set(srcRect);
1384 SkMatrix srcToDeviceMatrix(m);
1385 srcToDeviceMatrix.postConcat(*draw.fMatrix);
1386 srcToDeviceMatrix.mapRect(&transformedRect, floatSrcRect);
1387
1388 if (hasAlignedSamples(floatSrcRect, transformedRect)) {
1389 // Samples are texel-aligned, so filtering is futile
1390 sampler->setFilter(GrSamplerState::kNearest_Filter);
1391 needsTextureDomain = false;
1392 } else {
1393 needsTextureDomain = needsTextureDomain &&
1394 mayColorBleed(floatSrcRect, transformedRect, m);
1395 }
1396 }
1397 }
1398
1399 GrRect textureDomain = GrRect::MakeEmpty();
1400
1401 if (needsTextureDomain) {
1402 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001403 GrScalar left, top, right, bottom;
1404 if (srcRect.width() > 1) {
1405 GrScalar border = GR_ScalarHalf / bitmap.width();
1406 left = paintRect.left() + border;
1407 right = paintRect.right() - border;
1408 } else {
1409 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1410 }
1411 if (srcRect.height() > 1) {
1412 GrScalar border = GR_ScalarHalf / bitmap.height();
1413 top = paintRect.top() + border;
1414 bottom = paintRect.bottom() - border;
1415 } else {
1416 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1417 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001418 textureDomain.setLTRB(left, top, right, bottom);
junov@google.com6acc9b32011-05-16 18:32:07 +00001419 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001420 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001421
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001422 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001423}
1424
reed@google.com8926b162012-03-23 15:36:36 +00001425static GrTexture* filter_texture(GrContext* context, GrTexture* texture,
1426 SkImageFilter* filter, const GrRect& rect) {
1427 GrAssert(filter);
1428
1429 SkSize blurSize;
1430 SkISize radius;
1431
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001432 GrTextureDesc desc;
1433 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1434 desc.fWidth = SkScalarCeilToInt(rect.width());
1435 desc.fHeight = SkScalarCeilToInt(rect.height());
1436 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
reed@google.com8926b162012-03-23 15:36:36 +00001437
1438 if (filter->asABlur(&blurSize)) {
1439 GrAutoScratchTexture temp1, temp2;
1440 texture = context->gaussianBlur(texture, &temp1, &temp2, rect,
1441 blurSize.width(),
1442 blurSize.height());
1443 texture->ref();
1444 } else if (filter->asADilate(&radius)) {
1445 GrAutoScratchTexture temp1(context, desc), temp2(context, desc);
1446 texture = context->applyMorphology(texture, rect,
1447 temp1.texture(), temp2.texture(),
bsalomon@google.comb505a122012-05-31 18:40:36 +00001448 GrContext::kDilate_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001449 radius);
1450 texture->ref();
1451 } else if (filter->asAnErode(&radius)) {
1452 GrAutoScratchTexture temp1(context, desc), temp2(context, desc);
1453 texture = context->applyMorphology(texture, rect,
1454 temp1.texture(), temp2.texture(),
bsalomon@google.comb505a122012-05-31 18:40:36 +00001455 GrContext::kErode_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001456 radius);
1457 texture->ref();
1458 }
1459 return texture;
1460}
1461
reed@google.comac10a2d2010-12-22 21:39:39 +00001462void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1463 int left, int top, const SkPaint& paint) {
1464 CHECK_SHOULD_DRAW(draw);
1465
reed@google.com8926b162012-03-23 15:36:36 +00001466 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001467 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1468 return;
1469 }
1470
reed@google.com76dd2772012-01-05 21:15:07 +00001471 int w = bitmap.width();
1472 int h = bitmap.height();
1473
bsalomon@google.com5782d712011-01-21 21:03:59 +00001474 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001475 if(!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001476 return;
1477 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001478
bsalomon@google.com5782d712011-01-21 21:03:59 +00001479 GrAutoMatrix avm(fContext, GrMatrix::I());
1480
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001481 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001482
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001483 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001484 sampler->reset();
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001485 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001486 grPaint.setTexture(kBitmapTextureIdx, texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001487
reed@google.com8926b162012-03-23 15:36:36 +00001488 SkImageFilter* filter = paint.getImageFilter();
1489 if (NULL != filter) {
1490 GrTexture* filteredTexture = filter_texture(fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001491 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001492 if (filteredTexture) {
1493 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1494 texture = filteredTexture;
1495 filteredTexture->unref();
1496 }
reed@google.com76dd2772012-01-05 21:15:07 +00001497 }
reed@google.com8926b162012-03-23 15:36:36 +00001498
bsalomon@google.com5782d712011-01-21 21:03:59 +00001499 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001500 GrRect::MakeXYWH(GrIntToScalar(left),
1501 GrIntToScalar(top),
1502 GrIntToScalar(w),
1503 GrIntToScalar(h)),
1504 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1505 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001506}
1507
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001508void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
reed@google.comac10a2d2010-12-22 21:39:39 +00001509 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001510 // clear of the source device must occur before CHECK_SHOULD_DRAW
1511 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1512 if (dev->fNeedClear) {
1513 // TODO: could check here whether we really need to draw at all
1514 dev->clear(0x0);
1515 }
1516
reed@google.comac10a2d2010-12-22 21:39:39 +00001517 CHECK_SHOULD_DRAW(draw);
1518
bsalomon@google.com5782d712011-01-21 21:03:59 +00001519 GrPaint grPaint;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001520 if (!dev->bindDeviceAsTexture(&grPaint) ||
bsalomon@google.com84405e02012-03-05 19:57:21 +00001521 !skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001522 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001523 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001524
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001525 GrTexture* devTex = grPaint.getTexture(0);
1526 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001527
reed@google.com8926b162012-03-23 15:36:36 +00001528 SkImageFilter* filter = paint.getImageFilter();
1529 if (NULL != filter) {
robertphillips@google.com8637a362012-04-10 18:32:35 +00001530 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
1531 SkIntToScalar(devTex->height()));
reed@google.com8926b162012-03-23 15:36:36 +00001532 GrTexture* filteredTexture = filter_texture(fContext, devTex, filter,
1533 rect);
1534 if (filteredTexture) {
1535 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1536 devTex = filteredTexture;
1537 filteredTexture->unref();
1538 }
1539 }
1540
bsalomon@google.com5782d712011-01-21 21:03:59 +00001541 const SkBitmap& bm = dev->accessBitmap(false);
1542 int w = bm.width();
1543 int h = bm.height();
1544
1545 GrAutoMatrix avm(fContext, GrMatrix::I());
1546
bsalomon@google.com97912912011-12-06 16:30:36 +00001547 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001548
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001549 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1550 GrIntToScalar(y),
1551 GrIntToScalar(w),
1552 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001553
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001554 // The device being drawn may not fill up its texture (saveLayer uses
1555 // the approximate ).
1556 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1557 GR_Scalar1 * h / devTex->height());
1558
1559 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001560}
1561
reed@google.com8926b162012-03-23 15:36:36 +00001562bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
reed@google.com76dd2772012-01-05 21:15:07 +00001563 SkSize size;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001564 SkISize radius;
1565 if (!filter->asABlur(&size) && !filter->asADilate(&radius) && !filter->asAnErode(&radius)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001566 return false;
1567 }
reed@google.com8926b162012-03-23 15:36:36 +00001568 return true;
1569}
1570
1571bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1572 const SkMatrix& ctm,
1573 SkBitmap* result, SkIPoint* offset) {
1574 // want explicitly our impl, so guard against a subclass of us overriding it
1575 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001576 return false;
1577 }
reed@google.com8926b162012-03-23 15:36:36 +00001578
1579 SkAutoLockPixels alp(src, !src.getTexture());
1580 if (!src.getTexture() && !src.readyToDraw()) {
1581 return false;
1582 }
1583
1584 GrPaint paint;
1585 paint.reset();
1586
1587 GrSamplerState* sampler = paint.textureSampler(kBitmapTextureIdx);
1588
1589 GrTexture* texture;
1590 SkAutoCachedTexture act(this, src, sampler, &texture);
1591
1592 result->setConfig(src.config(), src.width(), src.height());
robertphillips@google.com8637a362012-04-10 18:32:35 +00001593 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
1594 SkIntToScalar(src.height()));
reed@google.com8926b162012-03-23 15:36:36 +00001595 GrTexture* resultTexture = filter_texture(fContext, texture, filter, rect);
1596 if (resultTexture) {
1597 result->setPixelRef(new SkGrTexturePixelRef(resultTexture))->unref();
1598 resultTexture->unref();
1599 }
reed@google.com76dd2772012-01-05 21:15:07 +00001600 return true;
1601}
1602
reed@google.comac10a2d2010-12-22 21:39:39 +00001603///////////////////////////////////////////////////////////////////////////////
1604
1605// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001606static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001607 kTriangles_GrPrimitiveType,
1608 kTriangleStrip_GrPrimitiveType,
1609 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001610};
1611
1612void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1613 int vertexCount, const SkPoint vertices[],
1614 const SkPoint texs[], const SkColor colors[],
1615 SkXfermode* xmode,
1616 const uint16_t indices[], int indexCount,
1617 const SkPaint& paint) {
1618 CHECK_SHOULD_DRAW(draw);
1619
bsalomon@google.com5782d712011-01-21 21:03:59 +00001620 GrPaint grPaint;
1621 SkAutoCachedTexture act;
1622 // we ignore the shader if texs is null.
1623 if (NULL == texs) {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001624 if (!skPaint2GrPaintNoShader(paint,
1625 false,
1626 NULL == colors,
1627 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001628 return;
1629 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001630 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001631 if (!skPaint2GrPaintShader(this,
1632 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001633 NULL == colors,
1634 &act,
1635 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001636 return;
1637 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001638 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001639
1640 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001641 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001642 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1643#if 0
1644 return
1645#endif
1646 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001647 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001648
bsalomon@google.com498776a2011-08-16 19:20:44 +00001649 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1650 if (NULL != colors) {
1651 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001652 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001653 for (int i = 0; i < vertexCount; ++i) {
1654 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1655 }
1656 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001657 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001658 fContext->drawVertices(grPaint,
1659 gVertexMode2PrimitiveType[vmode],
1660 vertexCount,
1661 (GrPoint*) vertices,
1662 (GrPoint*) texs,
1663 colors,
1664 indices,
1665 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001666}
1667
1668///////////////////////////////////////////////////////////////////////////////
1669
1670static void GlyphCacheAuxProc(void* data) {
1671 delete (GrFontScaler*)data;
1672}
1673
1674static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1675 void* auxData;
1676 GrFontScaler* scaler = NULL;
1677 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1678 scaler = (GrFontScaler*)auxData;
1679 }
1680 if (NULL == scaler) {
1681 scaler = new SkGrFontScaler(cache);
1682 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1683 }
1684 return scaler;
1685}
1686
1687static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1688 SkFixed fx, SkFixed fy,
1689 const SkGlyph& glyph) {
1690 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1691
bungeman@google.com15865a72012-01-11 16:28:04 +00001692 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001693
1694 if (NULL == procs->fFontScaler) {
1695 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1696 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001697
bungeman@google.com15865a72012-01-11 16:28:04 +00001698 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1699 glyph.getSubXFixed(),
1700 glyph.getSubYFixed()),
1701 SkFixedFloorToFixed(fx),
1702 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001703 procs->fFontScaler);
1704}
1705
bsalomon@google.com5782d712011-01-21 21:03:59 +00001706SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001707
1708 // deferred allocation
1709 if (NULL == fDrawProcs) {
1710 fDrawProcs = new GrSkDrawProcs;
1711 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1712 fDrawProcs->fContext = fContext;
1713 }
1714
1715 // init our (and GL's) state
1716 fDrawProcs->fTextContext = context;
1717 fDrawProcs->fFontScaler = NULL;
1718 return fDrawProcs;
1719}
1720
1721void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1722 size_t byteLength, SkScalar x, SkScalar y,
1723 const SkPaint& paint) {
1724 CHECK_SHOULD_DRAW(draw);
1725
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001726 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001727 // this guy will just call our drawPath()
1728 draw.drawText((const char*)text, byteLength, x, y, paint);
1729 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001730 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001731
1732 GrPaint grPaint;
1733 SkAutoCachedTexture act;
1734
bsalomon@google.com84405e02012-03-05 19:57:21 +00001735 if (!skPaint2GrPaintShader(this,
1736 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001737 true,
1738 &act,
1739 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001740 return;
1741 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001742 GrTextContext::AutoFinish txtCtxAF(this->getTextContext(), fContext,
1743 grPaint, draw.fExtMatrix);
1744 myDraw.fProcs = this->initDrawForText(txtCtxAF.getTextContext());
reed@google.comac10a2d2010-12-22 21:39:39 +00001745 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1746 }
1747}
1748
1749void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1750 size_t byteLength, const SkScalar pos[],
1751 SkScalar constY, int scalarsPerPos,
1752 const SkPaint& paint) {
1753 CHECK_SHOULD_DRAW(draw);
1754
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001755 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001756 // this guy will just call our drawPath()
1757 draw.drawPosText((const char*)text, byteLength, pos, constY,
1758 scalarsPerPos, paint);
1759 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001760 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001761
1762 GrPaint grPaint;
1763 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001764 if (!skPaint2GrPaintShader(this,
1765 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001766 true,
1767 &act,
1768 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001769 return;
1770 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001771 GrTextContext::AutoFinish txtCtxAF(this->getTextContext(), fContext,
1772 grPaint, draw.fExtMatrix);
1773 myDraw.fProcs = this->initDrawForText(txtCtxAF.getTextContext());
reed@google.comac10a2d2010-12-22 21:39:39 +00001774 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1775 scalarsPerPos, paint);
1776 }
1777}
1778
1779void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1780 size_t len, const SkPath& path,
1781 const SkMatrix* m, const SkPaint& paint) {
1782 CHECK_SHOULD_DRAW(draw);
1783
1784 SkASSERT(draw.fDevice == this);
1785 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1786}
1787
1788///////////////////////////////////////////////////////////////////////////////
1789
reed@google.comf67e4cf2011-03-15 20:56:58 +00001790bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1791 if (!paint.isLCDRenderText()) {
1792 // we're cool with the paint as is
1793 return false;
1794 }
1795
1796 if (paint.getShader() ||
1797 paint.getXfermode() || // unless its srcover
1798 paint.getMaskFilter() ||
1799 paint.getRasterizer() ||
1800 paint.getColorFilter() ||
1801 paint.getPathEffect() ||
1802 paint.isFakeBoldText() ||
1803 paint.getStyle() != SkPaint::kFill_Style) {
1804 // turn off lcd
1805 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1806 flags->fHinting = paint.getHinting();
1807 return true;
1808 }
1809 // we're cool with the paint as is
1810 return false;
1811}
1812
reed@google.com75d939b2011-12-07 15:07:23 +00001813void SkGpuDevice::flush() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001814 DO_DEFERRED_CLEAR;
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001815 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001816}
1817
reed@google.comf67e4cf2011-03-15 20:56:58 +00001818///////////////////////////////////////////////////////////////////////////////
1819
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001820SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(
1821 const SkBitmap& bitmap,
1822 const GrSamplerState* sampler) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001823 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001824 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001825
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001826 if (!bitmap.isVolatile()) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001827 uint64_t key = bitmap.getGenerationID();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001828 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001829
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001830 GrTextureDesc desc;
1831 desc.fWidth = bitmap.width();
1832 desc.fHeight = bitmap.height();
1833 desc.fConfig = SkGr::BitmapConfig2PixelConfig(bitmap.config());
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001834
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001835 entry = ctx->findAndLockTexture(desc, sampler);
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001836 if (NULL == entry.texture()) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001837 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
1838 bitmap);
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001839 }
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001840 } else {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001841 entry = sk_gr_create_bitmap_texture(ctx, kUncached_CacheID,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001842 sampler, bitmap);
1843 }
1844 if (NULL == entry.texture()) {
1845 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1846 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001847 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001848 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001849}
1850
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001851void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1852 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001853}
1854
bsalomon@google.comfb309512011-11-30 14:13:48 +00001855bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
1856 const GrSamplerState& sampler) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001857 uint64_t key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001858 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001859
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001860 GrTextureDesc desc;
1861 desc.fWidth = bitmap.width();
1862 desc.fHeight = bitmap.height();
1863 desc.fConfig = SkGr::BitmapConfig2PixelConfig(bitmap.config());
1864 desc.fClientCacheID = key;
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001865
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001866 return this->context()->isTextureInCache(desc, &sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001867}
1868
1869
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001870SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1871 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001872 bool isOpaque,
1873 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001874 GrTextureDesc desc;
1875 desc.fConfig = fRenderTarget->config();
1876 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1877 desc.fWidth = width;
1878 desc.fHeight = height;
1879 desc.fSampleCnt = fRenderTarget->numSamples();
1880
1881 GrContext::TextureCacheEntry cacheEntry;
1882 GrTexture* texture;
1883 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001884 // Skia's convention is to only clear a device if it is non-opaque.
1885 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001886
1887#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1888 // layers are never draw in repeat modes, so we can request an approx
1889 // match and ignore any padding.
1890 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1891 GrContext::kApprox_ScratchTexMatch :
1892 GrContext::kExact_ScratchTexMatch;
1893 cacheEntry = fContext->lockScratchTexture(desc, matchType);
1894 texture = cacheEntry.texture();
1895#else
1896 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1897 texture = tunref.get();
1898#endif
1899 if (texture) {
1900 return SkNEW_ARGS(SkGpuDevice,(fContext,
1901 texture,
1902 cacheEntry,
1903 needClear));
1904 } else {
1905 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1906 width, height);
1907 return NULL;
1908 }
1909}
1910
1911SkGpuDevice::SkGpuDevice(GrContext* context,
1912 GrTexture* texture,
1913 TexCache cacheEntry,
1914 bool needClear)
1915 : SkDevice(make_bitmap(context, texture->asRenderTarget())) {
1916 GrAssert(texture && texture->asRenderTarget());
1917 GrAssert(NULL == cacheEntry.texture() || texture == cacheEntry.texture());
1918 this->initFromRenderTarget(context, texture->asRenderTarget());
1919 fCache = cacheEntry;
1920 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001921}
1922
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001923GrTextContext* SkGpuDevice::getTextContext() {
1924 if (NULL == fTextContext) {
1925 fTextContext = new GrDefaultTextContext();
1926 }
1927 return fTextContext;
1928}