blob: 77a08eccb960fdcb9937cc12b73b614f930ac77d [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
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000633 // we only handle hairlines and paints without path effects or mask filters,
634 // else we let the SkDraw call our drawPath()
635 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000636 draw.drawPoints(mode, count, pts, paint, true);
637 return;
638 }
639
bsalomon@google.com5782d712011-01-21 21:03:59 +0000640 GrPaint grPaint;
641 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000642 if (!skPaint2GrPaintShader(this,
643 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000644 true,
645 &act,
646 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000647 return;
648 }
649
bsalomon@google.com5782d712011-01-21 21:03:59 +0000650 fContext->drawVertices(grPaint,
651 gPointMode2PrimtiveType[mode],
652 count,
653 (GrPoint*)pts,
654 NULL,
655 NULL,
656 NULL,
657 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000658}
659
reed@google.comc9aa5872011-04-05 21:05:37 +0000660///////////////////////////////////////////////////////////////////////////////
661
reed@google.comac10a2d2010-12-22 21:39:39 +0000662void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
663 const SkPaint& paint) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000664 CHECK_SHOULD_DRAW(draw);
665
bungeman@google.com79bd8772011-07-18 15:34:08 +0000666 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000667 SkScalar width = paint.getStrokeWidth();
668
669 /*
670 We have special code for hairline strokes, miter-strokes, and fills.
671 Anything else we just call our path code.
672 */
673 bool usePath = doStroke && width > 0 &&
674 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000675 // another two reasons we might need to call drawPath...
676 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000677 usePath = true;
678 }
reed@google.com67db6642011-05-26 11:46:35 +0000679 // until we aa rotated rects...
680 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
681 usePath = true;
682 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000683 // small miter limit means right angles show bevel...
684 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
685 paint.getStrokeMiter() < SK_ScalarSqrt2)
686 {
687 usePath = true;
688 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000689 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000690 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
691 usePath = true;
692 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000693
694 if (usePath) {
695 SkPath path;
696 path.addRect(rect);
697 this->drawPath(draw, path, paint, NULL, true);
698 return;
699 }
700
701 GrPaint grPaint;
702 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000703 if (!skPaint2GrPaintShader(this,
704 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000705 true,
706 &act,
707 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000708 return;
709 }
reed@google.com20efde72011-05-09 17:00:02 +0000710 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000711}
712
reed@google.com69302852011-02-16 18:08:07 +0000713#include "SkMaskFilter.h"
714#include "SkBounder.h"
715
bsalomon@google.com85003222012-03-28 14:44:37 +0000716///////////////////////////////////////////////////////////////////////////////
717
718// helpers for applying mask filters
719namespace {
720
721GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000722 switch (fillType) {
723 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000724 return kWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000725 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000726 return kEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000727 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000728 return kInverseWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000729 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000730 return kInverseEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000731 default:
732 SkDebugf("Unsupported path fill type\n");
bsalomon@google.com47059542012-06-06 20:51:20 +0000733 return kHairLine_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000734 }
735}
736
bsalomon@google.com85003222012-03-28 14:44:37 +0000737// We prefer to blur small rect with small radius via CPU.
738#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
739#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
740inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
741 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
742 rect.height() <= MIN_GPU_BLUR_SIZE &&
743 radius <= MIN_GPU_BLUR_RADIUS) {
744 return true;
745 }
746 return false;
747}
748
749bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
750 SkMaskFilter* filter, const SkMatrix& matrix,
751 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000752 GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000753#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000754 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000755#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000756 SkMaskFilter::BlurInfo info;
757 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000758 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000759 return false;
760 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000761 SkScalar radius = info.fIgnoreTransform ? info.fRadius
762 : matrix.mapRadius(info.fRadius);
763 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000764 if (radius <= 0) {
765 return false;
766 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000767
768 SkRect srcRect = path.getBounds();
769 if (shouldDrawBlurWithCPU(srcRect, radius)) {
770 return false;
771 }
772
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000773 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000774 float sigma3 = sigma * 3.0f;
775
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000776 SkRect clipRect;
777 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000778
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000779 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000780 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
781 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000782 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000783 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000784 SkIRect finalIRect;
785 finalRect.roundOut(&finalIRect);
786 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000787 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000788 }
789 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000790 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000791 }
792 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000793 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000794 GrTextureDesc desc;
795 desc.fFlags = kRenderTarget_GrTextureFlagBit;
796 desc.fWidth = SkScalarCeilToInt(srcRect.width());
797 desc.fHeight = SkScalarCeilToInt(srcRect.height());
798 // We actually only need A8, but it often isn't supported as a
799 // render target so default to RGBA_8888
800 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000801
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000802 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
803 desc.fConfig = kAlpha_8_GrPixelConfig;
804 }
805
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000806 GrAutoScratchTexture pathEntry(context, desc);
807 GrTexture* pathTexture = pathEntry.texture();
808 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000809 return false;
810 }
811 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000812 // Once this code moves into GrContext, this should be changed to use
813 // an AutoClipRestore.
814 GrClip oldClip = context->getClip();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000815 context->setRenderTarget(pathTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000816 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000817 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000818 GrPaint tempPaint;
819 tempPaint.reset();
820
821 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000822 tempPaint.fAntiAlias = grp->fAntiAlias;
823 if (tempPaint.fAntiAlias) {
824 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
825 // blend coeff of zero requires dual source blending support in order
826 // to properly blend partially covered pixels. This means the AA
827 // code path may not be taken. So we use a dst blend coeff of ISA. We
828 // could special case AA draws to a dst surface with known alpha=0 to
829 // use a zero dst coeff when dual source blending isn't available.
bsalomon@google.com47059542012-06-06 20:51:20 +0000830 tempPaint.fSrcBlendCoeff = kOne_GrBlendCoeff;
831 tempPaint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000832 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000833 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000834 context->drawPath(tempPaint, path, pathFillType, &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000835
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000836 GrAutoScratchTexture temp1, temp2;
837 // If we're doing a normal blur, we can clobber the pathTexture in the
838 // gaussianBlur. Otherwise, we need to save it for later compositing.
839 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000840 GrTexture* blurTexture = context->gaussianBlur(pathTexture,
841 &temp1,
842 isNormalBlur ? NULL : &temp2,
843 srcRect, sigma, sigma);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000844
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000845 if (!isNormalBlur) {
846 GrPaint paint;
847 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000848 paint.textureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000849 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
850 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000851 // Blend pathTexture over blurTexture.
852 context->setRenderTarget(blurTexture->asRenderTarget());
853 paint.setTexture(0, pathTexture);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000854 if (SkMaskFilter::kInner_BlurType == blurType) {
855 // inner: dst = dst * src
bsalomon@google.com47059542012-06-06 20:51:20 +0000856 paint.fSrcBlendCoeff = kDC_GrBlendCoeff;
857 paint.fDstBlendCoeff = kZero_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000858 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
859 // solid: dst = src + dst - src * dst
860 // = (1 - dst) * src + 1 * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000861 paint.fSrcBlendCoeff = kIDC_GrBlendCoeff;
862 paint.fDstBlendCoeff = kOne_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000863 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
864 // outer: dst = dst * (1 - src)
865 // = 0 * src + (1 - src) * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000866 paint.fSrcBlendCoeff = kZero_GrBlendCoeff;
867 paint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000868 }
869 context->drawRect(paint, srcRect);
870 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000871 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000872 context->setClip(oldClip);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000873
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000874 if (grp->hasTextureOrMask()) {
875 GrMatrix inverse;
876 if (!matrix.invert(&inverse)) {
877 return false;
878 }
879 grp->preConcatActiveSamplerMatrices(inverse);
880 }
881
882 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
883 // we assume the last mask index is available for use
884 GrAssert(NULL == grp->getMask(MASK_IDX));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000885 grp->setMask(MASK_IDX, blurTexture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000886 grp->maskSampler(MASK_IDX)->reset();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000887
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000888 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
889 -finalRect.fTop);
890 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
891 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000892 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000893 return true;
894}
895
bsalomon@google.com85003222012-03-28 14:44:37 +0000896bool drawWithMaskFilter(GrContext* context, const SkPath& path,
897 SkMaskFilter* filter, const SkMatrix& matrix,
898 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000899 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000900 SkMask srcM, dstM;
901
902 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000903 SkMask::kComputeBoundsAndRenderImage_CreateMode,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000904 style)) {
reed@google.com69302852011-02-16 18:08:07 +0000905 return false;
906 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000907 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000908
909 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
910 return false;
911 }
912 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000913 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000914
915 if (clip.quickReject(dstM.fBounds)) {
916 return false;
917 }
918 if (bounder && !bounder->doIRect(dstM.fBounds)) {
919 return false;
920 }
921
922 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
923 // the current clip (and identity matrix) and grpaint settings
924
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000925 // used to compute inverse view, if necessary
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000926 GrMatrix ivm = matrix;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000927
reed@google.com0c219b62011-02-16 21:31:18 +0000928 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +0000929
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000930 GrTextureDesc desc;
931 desc.fWidth = dstM.fBounds.width();
932 desc.fHeight = dstM.fBounds.height();
933 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +0000934
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000935 GrAutoScratchTexture ast(context, desc);
936 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000937
reed@google.com69302852011-02-16 18:08:07 +0000938 if (NULL == texture) {
939 return false;
940 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000941 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000942 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000943
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000944 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
945 grp->preConcatActiveSamplerMatrices(ivm);
946 }
947
948 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
949 // we assume the last mask index is available for use
950 GrAssert(NULL == grp->getMask(MASK_IDX));
951 grp->setMask(MASK_IDX, texture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000952 grp->maskSampler(MASK_IDX)->reset();
reed@google.com69302852011-02-16 18:08:07 +0000953
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000954 GrRect d;
955 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +0000956 GrIntToScalar(dstM.fBounds.fTop),
957 GrIntToScalar(dstM.fBounds.fRight),
958 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000959
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000960 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
961 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
962 -dstM.fBounds.fTop*SK_Scalar1);
963 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000964 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000965 return true;
966}
reed@google.com69302852011-02-16 18:08:07 +0000967
bsalomon@google.com85003222012-03-28 14:44:37 +0000968}
969
970///////////////////////////////////////////////////////////////////////////////
971
reed@google.com0c219b62011-02-16 21:31:18 +0000972void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000973 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000974 bool pathIsMutable) {
975 CHECK_SHOULD_DRAW(draw);
976
reed@google.comfe626382011-09-21 13:50:35 +0000977 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000978
bsalomon@google.com5782d712011-01-21 21:03:59 +0000979 GrPaint grPaint;
980 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000981 if (!skPaint2GrPaintShader(this,
982 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000983 true,
984 &act,
985 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000986 return;
987 }
988
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000989 // can we cheat, and threat a thin stroke as a hairline w/ coverage
990 // if we can, we draw lots faster (raster device does this same test)
991 SkScalar hairlineCoverage;
992 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
993 doFill = false;
994 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
995 grPaint.fCoverage);
996 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000997
reed@google.comfe626382011-09-21 13:50:35 +0000998 // If we have a prematrix, apply it to the path, optimizing for the case
999 // where the original path can in fact be modified in place (even though
1000 // its parameter type is const).
1001 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1002 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001003
1004 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001005 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001006
reed@google.come3445642011-02-16 23:20:39 +00001007 if (!pathIsMutable) {
1008 result = &tmpPath;
1009 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001010 }
reed@google.come3445642011-02-16 23:20:39 +00001011 // should I push prePathMatrix on our MV stack temporarily, instead
1012 // of applying it here? See SkDraw.cpp
1013 pathPtr->transform(*prePathMatrix, result);
1014 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001015 }
reed@google.com0c219b62011-02-16 21:31:18 +00001016 // at this point we're done with prePathMatrix
1017 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001018
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001019 if (paint.getPathEffect() ||
1020 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001021 // it is safe to use tmpPath here, even if we already used it for the
1022 // prepathmatrix, since getFillPath can take the same object for its
1023 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001024 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001025 pathPtr = &tmpPath;
1026 }
1027
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001028 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001029 // avoid possibly allocating a new path in transform if we can
1030 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1031
1032 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001033 pathPtr->transform(*draw.fMatrix, devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001034 GrPathFill pathFillType = doFill ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001035 skToGrFillType(devPathPtr->getFillType()) : kHairLine_GrPathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001036 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001037 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001038 &grPaint, pathFillType)) {
1039 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
1040 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001041 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001042 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001043 &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001044 }
reed@google.com69302852011-02-16 18:08:07 +00001045 return;
1046 }
reed@google.com69302852011-02-16 18:08:07 +00001047
bsalomon@google.com47059542012-06-06 20:51:20 +00001048 GrPathFill fill = kHairLine_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001049
reed@google.com0c219b62011-02-16 21:31:18 +00001050 if (doFill) {
1051 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001052 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001053 fill = kWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001054 break;
1055 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001056 fill = kEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001057 break;
1058 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001059 fill = kInverseWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001060 break;
1061 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001062 fill = kInverseEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001063 break;
1064 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001065 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001066 return;
1067 }
1068 }
1069
reed@google.com07f3ee12011-05-16 17:21:57 +00001070 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001071}
1072
bsalomon@google.comfb309512011-11-30 14:13:48 +00001073namespace {
1074
1075inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1076 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1077 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1078 return tilesX * tilesY;
1079}
1080
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001081inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001082 const SkIRect* srcRectPtr,
1083 int maxTextureSize) {
1084 static const int kSmallTileSize = 1 << 10;
1085 if (maxTextureSize <= kSmallTileSize) {
1086 return maxTextureSize;
1087 }
1088
1089 size_t maxTexTotalTileSize;
1090 size_t smallTotalTileSize;
1091
1092 if (NULL == srcRectPtr) {
1093 int w = bitmap.width();
1094 int h = bitmap.height();
1095 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1096 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1097 } else {
1098 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1099 srcRectPtr->fTop,
1100 srcRectPtr->fRight,
1101 srcRectPtr->fBottom,
1102 maxTextureSize);
1103 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1104 srcRectPtr->fTop,
1105 srcRectPtr->fRight,
1106 srcRectPtr->fBottom,
1107 kSmallTileSize);
1108 }
1109 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1110 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1111
1112 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1113 return kSmallTileSize;
1114 } else {
1115 return maxTextureSize;
1116 }
1117}
1118}
1119
1120bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1121 const GrSamplerState& sampler,
1122 const SkIRect* srcRectPtr,
1123 int* tileSize) const {
1124 SkASSERT(NULL != tileSize);
1125
1126 // if bitmap is explictly texture backed then just use the texture
1127 if (NULL != bitmap.getTexture()) {
1128 return false;
1129 }
1130 // if it's larger than the max texture size, then we have no choice but
1131 // tiling
1132 const int maxTextureSize = fContext->getMaxTextureSize();
1133 if (bitmap.width() > maxTextureSize ||
1134 bitmap.height() > maxTextureSize) {
1135 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1136 return true;
1137 }
1138 // if we are going to have to draw the whole thing, then don't tile
1139 if (NULL == srcRectPtr) {
1140 return false;
1141 }
1142 // if the entire texture is already in our cache then no reason to tile it
1143 if (this->isBitmapInTextureCache(bitmap, sampler)) {
1144 return false;
1145 }
1146
1147 // At this point we know we could do the draw by uploading the entire bitmap
1148 // as a texture. However, if the texture would be large compared to the
1149 // cache size and we don't require most of it for this draw then tile to
1150 // reduce the amount of upload and cache spill.
1151
1152 // assumption here is that sw bitmap size is a good proxy for its size as
1153 // a texture
1154 size_t bmpSize = bitmap.getSize();
1155 size_t cacheSize;
1156 fContext->getTextureCacheLimits(NULL, &cacheSize);
1157 if (bmpSize < cacheSize / 2) {
1158 return false;
1159 }
1160
1161 SkFixed fracUsed =
1162 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1163 (srcRectPtr->height() << 16) / bitmap.height());
1164 if (fracUsed <= SK_FixedHalf) {
1165 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1166 return true;
1167 } else {
1168 return false;
1169 }
1170}
1171
reed@google.comac10a2d2010-12-22 21:39:39 +00001172void SkGpuDevice::drawBitmap(const SkDraw& draw,
1173 const SkBitmap& bitmap,
1174 const SkIRect* srcRectPtr,
1175 const SkMatrix& m,
1176 const SkPaint& paint) {
1177 CHECK_SHOULD_DRAW(draw);
1178
1179 SkIRect srcRect;
1180 if (NULL == srcRectPtr) {
1181 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1182 } else {
1183 srcRect = *srcRectPtr;
1184 }
1185
junov@google.comd935cfb2011-06-27 20:48:23 +00001186 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001187 // Convert the bitmap to a shader so that the rect can be drawn
1188 // through drawRect, which supports mask filters.
1189 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001190 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001191 if (srcRectPtr) {
1192 if (!bitmap.extractSubset(&tmp, srcRect)) {
1193 return; // extraction failed
1194 }
1195 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001196 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001197 }
1198 SkPaint paintWithTexture(paint);
1199 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1200 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001201 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001202 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001203
junov@google.com1d329782011-07-28 20:10:09 +00001204 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001205 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001206 // also affect the behavior of the mask filter.
1207 SkMatrix drawMatrix;
1208 drawMatrix.setConcat(*draw.fMatrix, m);
1209 SkDraw transformedDraw(draw);
1210 transformedDraw.fMatrix = &drawMatrix;
1211
1212 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1213
junov@google.comd935cfb2011-06-27 20:48:23 +00001214 return;
1215 }
1216
bsalomon@google.com5782d712011-01-21 21:03:59 +00001217 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001218 if (!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001219 return;
1220 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001221 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001222 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001223 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001224 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001225 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001226 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001227
bsalomon@google.comfb309512011-11-30 14:13:48 +00001228 int tileSize;
1229 if (!this->shouldTileBitmap(bitmap, *sampler, srcRectPtr, &tileSize)) {
1230 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001231 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001232 return;
1233 }
1234
1235 // undo the translate done by SkCanvas
1236 int DX = SkMax32(0, srcRect.fLeft);
1237 int DY = SkMax32(0, srcRect.fTop);
1238 // compute clip bounds in local coordinates
1239 SkIRect clipRect;
1240 {
1241 SkRect r;
1242 r.set(draw.fClip->getBounds());
1243 SkMatrix matrix, inverse;
1244 matrix.setConcat(*draw.fMatrix, m);
1245 if (!matrix.invert(&inverse)) {
1246 return;
1247 }
1248 inverse.mapRect(&r);
1249 r.roundOut(&clipRect);
1250 // apply the canvas' translate to our local clip
1251 clipRect.offset(DX, DY);
1252 }
1253
bsalomon@google.comfb309512011-11-30 14:13:48 +00001254 int nx = bitmap.width() / tileSize;
1255 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001256 for (int x = 0; x <= nx; x++) {
1257 for (int y = 0; y <= ny; y++) {
1258 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001259 tileR.set(x * tileSize, y * tileSize,
1260 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001261 if (!SkIRect::Intersects(tileR, clipRect)) {
1262 continue;
1263 }
1264
1265 SkIRect srcR = tileR;
1266 if (!srcR.intersect(srcRect)) {
1267 continue;
1268 }
1269
1270 SkBitmap tmpB;
1271 if (bitmap.extractSubset(&tmpB, tileR)) {
1272 // now offset it to make it "local" to our tmp bitmap
1273 srcR.offset(-tileR.fLeft, -tileR.fTop);
1274
1275 SkMatrix tmpM(m);
1276 {
1277 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1278 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1279 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1280 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001281 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001282 }
1283 }
1284 }
1285}
1286
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001287namespace {
1288
1289bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1290 // detect pixel disalignment
1291 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1292 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1293 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1294 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1295 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1296 COLOR_BLEED_TOLERANCE &&
1297 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1298 COLOR_BLEED_TOLERANCE) {
1299 return true;
1300 }
1301 return false;
1302}
1303
1304bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1305 const SkMatrix& m) {
1306 // Only gets called if hasAlignedSamples returned false.
1307 // So we can assume that sampling is axis aligned but not texel aligned.
1308 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
1309 SkRect innerSrcRect(srcRect), innerTransformedRect,
1310 outerTransformedRect(transformedRect);
1311 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1312 m.mapRect(&innerTransformedRect, innerSrcRect);
1313
1314 // The gap between outerTransformedRect and innerTransformedRect
1315 // represents the projection of the source border area, which is
1316 // problematic for color bleeding. We must check whether any
1317 // destination pixels sample the border area.
1318 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1319 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1320 SkIRect outer, inner;
1321 outerTransformedRect.round(&outer);
1322 innerTransformedRect.round(&inner);
1323 // If the inner and outer rects round to the same result, it means the
1324 // border does not overlap any pixel centers. Yay!
1325 return inner != outer;
1326}
1327
1328} // unnamed namespace
1329
reed@google.comac10a2d2010-12-22 21:39:39 +00001330/*
1331 * This is called by drawBitmap(), which has to handle images that may be too
1332 * large to be represented by a single texture.
1333 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001334 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1335 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001336 */
1337void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1338 const SkBitmap& bitmap,
1339 const SkIRect& srcRect,
1340 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001341 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001342 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1343 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001344
reed@google.com9c49bc32011-07-07 13:42:37 +00001345 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001346 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001347 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001348 return;
1349 }
1350
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001351 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001352
1353 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1354 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001355 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001356
1357 GrTexture* texture;
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001358 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001359 if (NULL == texture) {
1360 return;
1361 }
1362
bsalomon@google.com452943d2011-10-31 17:37:14 +00001363 grPaint->setTexture(kBitmapTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001364
reed@google.com20efde72011-05-09 17:00:02 +00001365 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1366 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001367 GrRect paintRect;
bsalomon@google.com91832162012-03-08 19:53:02 +00001368 float wInv = 1.f / bitmap.width();
1369 float hInv = 1.f / bitmap.height();
1370 paintRect.setLTRB(SkFloatToScalar(srcRect.fLeft * wInv),
1371 SkFloatToScalar(srcRect.fTop * hInv),
1372 SkFloatToScalar(srcRect.fRight * wInv),
1373 SkFloatToScalar(srcRect.fBottom * hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001374
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001375 bool needsTextureDomain = false;
1376 if (GrSamplerState::kBilinear_Filter == sampler->getFilter())
1377 {
1378 // Need texture domain if drawing a sub rect.
1379 needsTextureDomain = srcRect.width() < bitmap.width() ||
1380 srcRect.height() < bitmap.height();
1381 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1382 // sampling is axis-aligned
1383 GrRect floatSrcRect, transformedRect;
1384 floatSrcRect.set(srcRect);
1385 SkMatrix srcToDeviceMatrix(m);
1386 srcToDeviceMatrix.postConcat(*draw.fMatrix);
1387 srcToDeviceMatrix.mapRect(&transformedRect, floatSrcRect);
1388
1389 if (hasAlignedSamples(floatSrcRect, transformedRect)) {
1390 // Samples are texel-aligned, so filtering is futile
1391 sampler->setFilter(GrSamplerState::kNearest_Filter);
1392 needsTextureDomain = false;
1393 } else {
1394 needsTextureDomain = needsTextureDomain &&
1395 mayColorBleed(floatSrcRect, transformedRect, m);
1396 }
1397 }
1398 }
1399
1400 GrRect textureDomain = GrRect::MakeEmpty();
1401
1402 if (needsTextureDomain) {
1403 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001404 GrScalar left, top, right, bottom;
1405 if (srcRect.width() > 1) {
1406 GrScalar border = GR_ScalarHalf / bitmap.width();
1407 left = paintRect.left() + border;
1408 right = paintRect.right() - border;
1409 } else {
1410 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1411 }
1412 if (srcRect.height() > 1) {
1413 GrScalar border = GR_ScalarHalf / bitmap.height();
1414 top = paintRect.top() + border;
1415 bottom = paintRect.bottom() - border;
1416 } else {
1417 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1418 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001419 textureDomain.setLTRB(left, top, right, bottom);
junov@google.com6acc9b32011-05-16 18:32:07 +00001420 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001421 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001422
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001423 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001424}
1425
reed@google.com8926b162012-03-23 15:36:36 +00001426static GrTexture* filter_texture(GrContext* context, GrTexture* texture,
1427 SkImageFilter* filter, const GrRect& rect) {
1428 GrAssert(filter);
1429
1430 SkSize blurSize;
1431 SkISize radius;
1432
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001433 GrTextureDesc desc;
1434 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1435 desc.fWidth = SkScalarCeilToInt(rect.width());
1436 desc.fHeight = SkScalarCeilToInt(rect.height());
1437 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
reed@google.com8926b162012-03-23 15:36:36 +00001438
1439 if (filter->asABlur(&blurSize)) {
1440 GrAutoScratchTexture temp1, temp2;
1441 texture = context->gaussianBlur(texture, &temp1, &temp2, rect,
1442 blurSize.width(),
1443 blurSize.height());
1444 texture->ref();
1445 } else if (filter->asADilate(&radius)) {
1446 GrAutoScratchTexture temp1(context, desc), temp2(context, desc);
1447 texture = context->applyMorphology(texture, rect,
1448 temp1.texture(), temp2.texture(),
bsalomon@google.comb505a122012-05-31 18:40:36 +00001449 GrContext::kDilate_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001450 radius);
1451 texture->ref();
1452 } else if (filter->asAnErode(&radius)) {
1453 GrAutoScratchTexture temp1(context, desc), temp2(context, desc);
1454 texture = context->applyMorphology(texture, rect,
1455 temp1.texture(), temp2.texture(),
bsalomon@google.comb505a122012-05-31 18:40:36 +00001456 GrContext::kErode_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001457 radius);
1458 texture->ref();
1459 }
1460 return texture;
1461}
1462
reed@google.comac10a2d2010-12-22 21:39:39 +00001463void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1464 int left, int top, const SkPaint& paint) {
1465 CHECK_SHOULD_DRAW(draw);
1466
reed@google.com8926b162012-03-23 15:36:36 +00001467 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001468 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1469 return;
1470 }
1471
reed@google.com76dd2772012-01-05 21:15:07 +00001472 int w = bitmap.width();
1473 int h = bitmap.height();
1474
bsalomon@google.com5782d712011-01-21 21:03:59 +00001475 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001476 if(!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001477 return;
1478 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001479
bsalomon@google.com5782d712011-01-21 21:03:59 +00001480 GrAutoMatrix avm(fContext, GrMatrix::I());
1481
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001482 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001483
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001484 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001485 sampler->reset();
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001486 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001487 grPaint.setTexture(kBitmapTextureIdx, texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001488
reed@google.com8926b162012-03-23 15:36:36 +00001489 SkImageFilter* filter = paint.getImageFilter();
1490 if (NULL != filter) {
1491 GrTexture* filteredTexture = filter_texture(fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001492 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001493 if (filteredTexture) {
1494 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1495 texture = filteredTexture;
1496 filteredTexture->unref();
1497 }
reed@google.com76dd2772012-01-05 21:15:07 +00001498 }
reed@google.com8926b162012-03-23 15:36:36 +00001499
bsalomon@google.com5782d712011-01-21 21:03:59 +00001500 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001501 GrRect::MakeXYWH(GrIntToScalar(left),
1502 GrIntToScalar(top),
1503 GrIntToScalar(w),
1504 GrIntToScalar(h)),
1505 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1506 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001507}
1508
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001509void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
reed@google.comac10a2d2010-12-22 21:39:39 +00001510 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001511 // clear of the source device must occur before CHECK_SHOULD_DRAW
1512 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1513 if (dev->fNeedClear) {
1514 // TODO: could check here whether we really need to draw at all
1515 dev->clear(0x0);
1516 }
1517
reed@google.comac10a2d2010-12-22 21:39:39 +00001518 CHECK_SHOULD_DRAW(draw);
1519
bsalomon@google.com5782d712011-01-21 21:03:59 +00001520 GrPaint grPaint;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001521 if (!dev->bindDeviceAsTexture(&grPaint) ||
bsalomon@google.com84405e02012-03-05 19:57:21 +00001522 !skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001523 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001524 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001525
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001526 GrTexture* devTex = grPaint.getTexture(0);
1527 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001528
reed@google.com8926b162012-03-23 15:36:36 +00001529 SkImageFilter* filter = paint.getImageFilter();
1530 if (NULL != filter) {
robertphillips@google.com8637a362012-04-10 18:32:35 +00001531 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
1532 SkIntToScalar(devTex->height()));
reed@google.com8926b162012-03-23 15:36:36 +00001533 GrTexture* filteredTexture = filter_texture(fContext, devTex, filter,
1534 rect);
1535 if (filteredTexture) {
1536 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1537 devTex = filteredTexture;
1538 filteredTexture->unref();
1539 }
1540 }
1541
bsalomon@google.com5782d712011-01-21 21:03:59 +00001542 const SkBitmap& bm = dev->accessBitmap(false);
1543 int w = bm.width();
1544 int h = bm.height();
1545
1546 GrAutoMatrix avm(fContext, GrMatrix::I());
1547
bsalomon@google.com97912912011-12-06 16:30:36 +00001548 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001549
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001550 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1551 GrIntToScalar(y),
1552 GrIntToScalar(w),
1553 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001554
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001555 // The device being drawn may not fill up its texture (saveLayer uses
1556 // the approximate ).
1557 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1558 GR_Scalar1 * h / devTex->height());
1559
1560 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001561}
1562
reed@google.com8926b162012-03-23 15:36:36 +00001563bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
reed@google.com76dd2772012-01-05 21:15:07 +00001564 SkSize size;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001565 SkISize radius;
1566 if (!filter->asABlur(&size) && !filter->asADilate(&radius) && !filter->asAnErode(&radius)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001567 return false;
1568 }
reed@google.com8926b162012-03-23 15:36:36 +00001569 return true;
1570}
1571
1572bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1573 const SkMatrix& ctm,
1574 SkBitmap* result, SkIPoint* offset) {
1575 // want explicitly our impl, so guard against a subclass of us overriding it
1576 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001577 return false;
1578 }
reed@google.com8926b162012-03-23 15:36:36 +00001579
1580 SkAutoLockPixels alp(src, !src.getTexture());
1581 if (!src.getTexture() && !src.readyToDraw()) {
1582 return false;
1583 }
1584
1585 GrPaint paint;
1586 paint.reset();
1587
1588 GrSamplerState* sampler = paint.textureSampler(kBitmapTextureIdx);
1589
1590 GrTexture* texture;
1591 SkAutoCachedTexture act(this, src, sampler, &texture);
1592
1593 result->setConfig(src.config(), src.width(), src.height());
robertphillips@google.com8637a362012-04-10 18:32:35 +00001594 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
1595 SkIntToScalar(src.height()));
reed@google.com8926b162012-03-23 15:36:36 +00001596 GrTexture* resultTexture = filter_texture(fContext, texture, filter, rect);
1597 if (resultTexture) {
1598 result->setPixelRef(new SkGrTexturePixelRef(resultTexture))->unref();
1599 resultTexture->unref();
1600 }
reed@google.com76dd2772012-01-05 21:15:07 +00001601 return true;
1602}
1603
reed@google.comac10a2d2010-12-22 21:39:39 +00001604///////////////////////////////////////////////////////////////////////////////
1605
1606// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001607static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001608 kTriangles_GrPrimitiveType,
1609 kTriangleStrip_GrPrimitiveType,
1610 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001611};
1612
1613void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1614 int vertexCount, const SkPoint vertices[],
1615 const SkPoint texs[], const SkColor colors[],
1616 SkXfermode* xmode,
1617 const uint16_t indices[], int indexCount,
1618 const SkPaint& paint) {
1619 CHECK_SHOULD_DRAW(draw);
1620
bsalomon@google.com5782d712011-01-21 21:03:59 +00001621 GrPaint grPaint;
1622 SkAutoCachedTexture act;
1623 // we ignore the shader if texs is null.
1624 if (NULL == texs) {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001625 if (!skPaint2GrPaintNoShader(paint,
1626 false,
1627 NULL == colors,
1628 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001629 return;
1630 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001631 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001632 if (!skPaint2GrPaintShader(this,
1633 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001634 NULL == colors,
1635 &act,
1636 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001637 return;
1638 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001639 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001640
1641 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001642 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001643 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1644#if 0
1645 return
1646#endif
1647 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001648 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001649
bsalomon@google.com498776a2011-08-16 19:20:44 +00001650 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1651 if (NULL != colors) {
1652 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001653 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001654 for (int i = 0; i < vertexCount; ++i) {
1655 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1656 }
1657 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001658 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001659 fContext->drawVertices(grPaint,
1660 gVertexMode2PrimitiveType[vmode],
1661 vertexCount,
1662 (GrPoint*) vertices,
1663 (GrPoint*) texs,
1664 colors,
1665 indices,
1666 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001667}
1668
1669///////////////////////////////////////////////////////////////////////////////
1670
1671static void GlyphCacheAuxProc(void* data) {
1672 delete (GrFontScaler*)data;
1673}
1674
1675static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1676 void* auxData;
1677 GrFontScaler* scaler = NULL;
1678 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1679 scaler = (GrFontScaler*)auxData;
1680 }
1681 if (NULL == scaler) {
1682 scaler = new SkGrFontScaler(cache);
1683 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1684 }
1685 return scaler;
1686}
1687
1688static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1689 SkFixed fx, SkFixed fy,
1690 const SkGlyph& glyph) {
1691 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1692
bungeman@google.com15865a72012-01-11 16:28:04 +00001693 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001694
1695 if (NULL == procs->fFontScaler) {
1696 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1697 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001698
bungeman@google.com15865a72012-01-11 16:28:04 +00001699 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1700 glyph.getSubXFixed(),
1701 glyph.getSubYFixed()),
1702 SkFixedFloorToFixed(fx),
1703 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001704 procs->fFontScaler);
1705}
1706
bsalomon@google.com5782d712011-01-21 21:03:59 +00001707SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001708
1709 // deferred allocation
1710 if (NULL == fDrawProcs) {
1711 fDrawProcs = new GrSkDrawProcs;
1712 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1713 fDrawProcs->fContext = fContext;
1714 }
1715
1716 // init our (and GL's) state
1717 fDrawProcs->fTextContext = context;
1718 fDrawProcs->fFontScaler = NULL;
1719 return fDrawProcs;
1720}
1721
1722void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1723 size_t byteLength, SkScalar x, SkScalar y,
1724 const SkPaint& paint) {
1725 CHECK_SHOULD_DRAW(draw);
1726
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001727 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001728 // this guy will just call our drawPath()
1729 draw.drawText((const char*)text, byteLength, x, y, paint);
1730 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001731 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001732
1733 GrPaint grPaint;
1734 SkAutoCachedTexture act;
1735
bsalomon@google.com84405e02012-03-05 19:57:21 +00001736 if (!skPaint2GrPaintShader(this,
1737 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001738 true,
1739 &act,
1740 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001741 return;
1742 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001743 GrTextContext::AutoFinish txtCtxAF(this->getTextContext(), fContext,
1744 grPaint, draw.fExtMatrix);
1745 myDraw.fProcs = this->initDrawForText(txtCtxAF.getTextContext());
reed@google.comac10a2d2010-12-22 21:39:39 +00001746 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1747 }
1748}
1749
1750void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1751 size_t byteLength, const SkScalar pos[],
1752 SkScalar constY, int scalarsPerPos,
1753 const SkPaint& paint) {
1754 CHECK_SHOULD_DRAW(draw);
1755
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001756 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001757 // this guy will just call our drawPath()
1758 draw.drawPosText((const char*)text, byteLength, pos, constY,
1759 scalarsPerPos, paint);
1760 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001761 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001762
1763 GrPaint grPaint;
1764 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001765 if (!skPaint2GrPaintShader(this,
1766 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001767 true,
1768 &act,
1769 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001770 return;
1771 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001772 GrTextContext::AutoFinish txtCtxAF(this->getTextContext(), fContext,
1773 grPaint, draw.fExtMatrix);
1774 myDraw.fProcs = this->initDrawForText(txtCtxAF.getTextContext());
reed@google.comac10a2d2010-12-22 21:39:39 +00001775 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1776 scalarsPerPos, paint);
1777 }
1778}
1779
1780void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1781 size_t len, const SkPath& path,
1782 const SkMatrix* m, const SkPaint& paint) {
1783 CHECK_SHOULD_DRAW(draw);
1784
1785 SkASSERT(draw.fDevice == this);
1786 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1787}
1788
1789///////////////////////////////////////////////////////////////////////////////
1790
reed@google.comf67e4cf2011-03-15 20:56:58 +00001791bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1792 if (!paint.isLCDRenderText()) {
1793 // we're cool with the paint as is
1794 return false;
1795 }
1796
1797 if (paint.getShader() ||
1798 paint.getXfermode() || // unless its srcover
1799 paint.getMaskFilter() ||
1800 paint.getRasterizer() ||
1801 paint.getColorFilter() ||
1802 paint.getPathEffect() ||
1803 paint.isFakeBoldText() ||
1804 paint.getStyle() != SkPaint::kFill_Style) {
1805 // turn off lcd
1806 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1807 flags->fHinting = paint.getHinting();
1808 return true;
1809 }
1810 // we're cool with the paint as is
1811 return false;
1812}
1813
reed@google.com75d939b2011-12-07 15:07:23 +00001814void SkGpuDevice::flush() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001815 DO_DEFERRED_CLEAR;
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001816 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001817}
1818
reed@google.comf67e4cf2011-03-15 20:56:58 +00001819///////////////////////////////////////////////////////////////////////////////
1820
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001821SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(
1822 const SkBitmap& bitmap,
1823 const GrSamplerState* sampler) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001824 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001825 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001826
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001827 if (!bitmap.isVolatile()) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001828 uint64_t key = bitmap.getGenerationID();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001829 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001830
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001831 GrTextureDesc desc;
1832 desc.fWidth = bitmap.width();
1833 desc.fHeight = bitmap.height();
1834 desc.fConfig = SkGr::BitmapConfig2PixelConfig(bitmap.config());
robertphillips@google.com56f22442012-06-08 14:21:26 +00001835 desc.fClientCacheID = key;
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001836
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001837 entry = ctx->findAndLockTexture(desc, sampler);
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001838 if (NULL == entry.texture()) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001839 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
1840 bitmap);
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001841 }
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001842 } else {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001843 entry = sk_gr_create_bitmap_texture(ctx, kUncached_CacheID,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001844 sampler, bitmap);
1845 }
1846 if (NULL == entry.texture()) {
1847 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1848 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001849 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001850 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001851}
1852
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001853void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1854 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001855}
1856
bsalomon@google.comfb309512011-11-30 14:13:48 +00001857bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
1858 const GrSamplerState& sampler) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001859 uint64_t key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001860 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001861
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001862 GrTextureDesc desc;
1863 desc.fWidth = bitmap.width();
1864 desc.fHeight = bitmap.height();
1865 desc.fConfig = SkGr::BitmapConfig2PixelConfig(bitmap.config());
1866 desc.fClientCacheID = key;
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001867
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001868 return this->context()->isTextureInCache(desc, &sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001869}
1870
1871
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001872SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1873 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001874 bool isOpaque,
1875 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001876 GrTextureDesc desc;
1877 desc.fConfig = fRenderTarget->config();
1878 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1879 desc.fWidth = width;
1880 desc.fHeight = height;
1881 desc.fSampleCnt = fRenderTarget->numSamples();
1882
1883 GrContext::TextureCacheEntry cacheEntry;
1884 GrTexture* texture;
1885 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001886 // Skia's convention is to only clear a device if it is non-opaque.
1887 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001888
1889#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1890 // layers are never draw in repeat modes, so we can request an approx
1891 // match and ignore any padding.
1892 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1893 GrContext::kApprox_ScratchTexMatch :
1894 GrContext::kExact_ScratchTexMatch;
1895 cacheEntry = fContext->lockScratchTexture(desc, matchType);
1896 texture = cacheEntry.texture();
1897#else
1898 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1899 texture = tunref.get();
1900#endif
1901 if (texture) {
1902 return SkNEW_ARGS(SkGpuDevice,(fContext,
1903 texture,
1904 cacheEntry,
1905 needClear));
1906 } else {
1907 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1908 width, height);
1909 return NULL;
1910 }
1911}
1912
1913SkGpuDevice::SkGpuDevice(GrContext* context,
1914 GrTexture* texture,
1915 TexCache cacheEntry,
1916 bool needClear)
1917 : SkDevice(make_bitmap(context, texture->asRenderTarget())) {
1918 GrAssert(texture && texture->asRenderTarget());
1919 GrAssert(NULL == cacheEntry.texture() || texture == cacheEntry.texture());
1920 this->initFromRenderTarget(context, texture->asRenderTarget());
1921 fCache = cacheEntry;
1922 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001923}
1924
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001925GrTextContext* SkGpuDevice::getTextContext() {
1926 if (NULL == fTextContext) {
1927 fTextContext = new GrDefaultTextContext();
1928 }
1929 return fTextContext;
1930}