blob: 7d9b4d90613373d9d1b7c91a2cd583ccfb8be8e9 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
tomhudson@google.com898e7b52012-06-01 20:42:15 +00009#include "SkGpuDevice.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000010
tomhudson@google.com898e7b52012-06-01 20:42:15 +000011#include "effects/GrGradientEffects.h"
epoger@google.comec3ed6a2011-07-28 14:26:00 +000012
reed@google.comac10a2d2010-12-22 21:39:39 +000013#include "GrContext.h"
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000014#include "GrDefaultTextContext.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000015#include "GrTextContext.h"
16
reed@google.comac10a2d2010-12-22 21:39:39 +000017#include "SkGrTexturePixelRef.h"
18
Scroggo97c88c22011-05-11 14:05:25 +000019#include "SkColorFilter.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000020#include "SkDrawProcs.h"
21#include "SkGlyphCache.h"
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +000022#include "SkImageFilter.h"
reed@google.comfe626382011-09-21 13:50:35 +000023#include "SkTLazy.h"
reed@google.comc9aa5872011-04-05 21:05:37 +000024#include "SkUtils.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000025
bsalomon@google.com06cd7322012-03-30 18:45:35 +000026#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
reed@google.comac10a2d2010-12-22 21:39:39 +000027
28#if 0
29 extern bool (*gShouldDrawProc)();
30 #define CHECK_SHOULD_DRAW(draw) \
31 do { \
32 if (gShouldDrawProc && !gShouldDrawProc()) return; \
33 this->prepareRenderTarget(draw); \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000034 GrAssert(!fNeedClear) \
reed@google.comac10a2d2010-12-22 21:39:39 +000035 } while (0)
36#else
bsalomon@google.com06cd7322012-03-30 18:45:35 +000037 #define CHECK_SHOULD_DRAW(draw) this->prepareRenderTarget(draw); \
38 GrAssert(!fNeedClear)
reed@google.comac10a2d2010-12-22 21:39:39 +000039#endif
40
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000041// we use the same texture slot on GrPaint for bitmaps and shaders
42// (since drawBitmap, drawSprite, and drawDevice ignore skia's shader)
43enum {
44 kBitmapTextureIdx = 0,
45 kShaderTextureIdx = 0
46};
47
reed@google.comcde92112011-07-06 20:00:52 +000048
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +000049#define MAX_BLUR_SIGMA 4.0f
50// FIXME: This value comes from from SkBlurMaskFilter.cpp.
51// Should probably be put in a common header someplace.
52#define MAX_BLUR_RADIUS SkIntToScalar(128)
53// This constant approximates the scaling done in the software path's
54// "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
55// IMHO, it actually should be 1: we blur "less" than we should do
56// according to the CSS and canvas specs, simply because Safari does the same.
57// Firefox used to do the same too, until 4.0 where they fixed it. So at some
58// point we should probably get rid of these scaling constants and rebaseline
59// all the blur tests.
60#define BLUR_SIGMA_SCALE 0.6f
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000061// This constant represents the screen alignment criterion in texels for
62// requiring texture domain clamping to prevent color bleeding when drawing
63// a sub region of a larger source image.
64#define COLOR_BLEED_TOLERANCE SkFloatToScalar(0.001f)
bsalomon@google.com06cd7322012-03-30 18:45:35 +000065
66#define DO_DEFERRED_CLEAR \
67 do { \
68 if (fNeedClear) { \
bsalomon@google.com730ca3b2012-04-03 13:25:12 +000069 this->clear(0x0); \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000070 fNeedClear = false; \
71 } \
72 } while (false) \
73
reed@google.comac10a2d2010-12-22 21:39:39 +000074///////////////////////////////////////////////////////////////////////////////
75
bsalomon@google.com84405e02012-03-05 19:57:21 +000076class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
77public:
78 SkAutoCachedTexture() { }
79 SkAutoCachedTexture(SkGpuDevice* device,
80 const SkBitmap& bitmap,
81 const GrSamplerState* sampler,
82 GrTexture** texture) {
83 GrAssert(texture);
84 *texture = this->set(device, bitmap, sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +000085 }
reed@google.comac10a2d2010-12-22 21:39:39 +000086
bsalomon@google.com84405e02012-03-05 19:57:21 +000087 ~SkAutoCachedTexture() {
88 if (fTex.texture()) {
89 fDevice->unlockCachedTexture(fTex);
90 }
reed@google.comac10a2d2010-12-22 21:39:39 +000091 }
bsalomon@google.com84405e02012-03-05 19:57:21 +000092
93 GrTexture* set(SkGpuDevice* device,
94 const SkBitmap& bitmap,
95 const GrSamplerState* sampler) {
96 if (fTex.texture()) {
97 fDevice->unlockCachedTexture(fTex);
98 }
99 fDevice = device;
100 GrTexture* texture = (GrTexture*)bitmap.getTexture();
101 if (texture) {
102 // return the native texture
103 fTex.reset();
104 } else {
105 // look it up in our cache
106 fTex = device->lockCachedTexture(bitmap, sampler);
107 texture = fTex.texture();
108 }
109 return texture;
110 }
111
112private:
113 SkGpuDevice* fDevice;
114 GrContext::TextureCacheEntry fTex;
115};
reed@google.comac10a2d2010-12-22 21:39:39 +0000116
117///////////////////////////////////////////////////////////////////////////////
118
119bool gDoTraceDraw;
120
121struct GrSkDrawProcs : public SkDrawProcs {
122public:
123 GrContext* fContext;
124 GrTextContext* fTextContext;
125 GrFontScaler* fFontScaler; // cached in the skia glyphcache
126};
127
128///////////////////////////////////////////////////////////////////////////////
129
reed@google.comaf951c92011-06-16 19:10:39 +0000130static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
131 switch (config) {
132 case kAlpha_8_GrPixelConfig:
133 *isOpaque = false;
134 return SkBitmap::kA8_Config;
135 case kRGB_565_GrPixelConfig:
136 *isOpaque = true;
137 return SkBitmap::kRGB_565_Config;
138 case kRGBA_4444_GrPixelConfig:
139 *isOpaque = false;
140 return SkBitmap::kARGB_4444_Config;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000141 case kSkia8888_PM_GrPixelConfig:
142 // we don't currently have a way of knowing whether
143 // a 8888 is opaque based on the config.
144 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000145 return SkBitmap::kARGB_8888_Config;
146 default:
147 *isOpaque = false;
148 return SkBitmap::kNo_Config;
149 }
150}
reed@google.comac10a2d2010-12-22 21:39:39 +0000151
reed@google.comaf951c92011-06-16 19:10:39 +0000152static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000153 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000154
155 bool isOpaque;
156 SkBitmap bitmap;
157 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
158 renderTarget->width(), renderTarget->height());
159 bitmap.setIsOpaque(isOpaque);
160 return bitmap;
161}
162
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000163SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
164: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
165 this->initFromRenderTarget(context, texture->asRenderTarget());
166}
167
reed@google.comaf951c92011-06-16 19:10:39 +0000168SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
169: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000170 this->initFromRenderTarget(context, renderTarget);
171}
172
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000173void SkGpuDevice::initFromRenderTarget(GrContext* context,
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000174 GrRenderTarget* renderTarget) {
reed@google.comaf951c92011-06-16 19:10:39 +0000175 fNeedPrepareRenderTarget = false;
176 fDrawProcs = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000177
reed@google.comaf951c92011-06-16 19:10:39 +0000178 fContext = context;
179 fContext->ref();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000180
reed@google.comaf951c92011-06-16 19:10:39 +0000181 fTexture = NULL;
182 fRenderTarget = NULL;
183 fNeedClear = false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000184
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000185 GrAssert(NULL != renderTarget);
186 fRenderTarget = renderTarget;
187 fRenderTarget->ref();
188 // if this RT is also a texture, hold a ref on it
189 fTexture = fRenderTarget->asTexture();
190 SkSafeRef(fTexture);
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000191
192 // Create a pixel ref for the underlying SkBitmap. We prefer a texture pixel
193 // ref to a render target pixel reft. The pixel ref may get ref'ed outside
194 // the device via accessBitmap. This external ref may outlive the device.
195 // Since textures own their render targets (but not vice-versa) we
196 // are ensuring that both objects will live as long as the pixel ref.
197 SkPixelRef* pr;
198 if (fTexture) {
199 pr = new SkGrTexturePixelRef(fTexture);
200 } else {
201 pr = new SkGrRenderTargetPixelRef(fRenderTarget);
202 }
reed@google.comaf951c92011-06-16 19:10:39 +0000203 this->setPixelRef(pr, 0)->unref();
bsalomon@google.comf4a9c822012-03-16 14:02:46 +0000204
205 fTextContext = NULL;
reed@google.comaf951c92011-06-16 19:10:39 +0000206}
207
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000208SkGpuDevice::SkGpuDevice(GrContext* context,
209 SkBitmap::Config config,
210 int width,
211 int height)
212 : SkDevice(config, width, height, false /*isOpaque*/) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000213 fNeedPrepareRenderTarget = false;
214 fDrawProcs = NULL;
215
reed@google.com7b201d22011-01-11 18:59:23 +0000216 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000217 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000218
reed@google.comac10a2d2010-12-22 21:39:39 +0000219 fTexture = NULL;
220 fRenderTarget = NULL;
221 fNeedClear = false;
222
reed@google.comaf951c92011-06-16 19:10:39 +0000223 if (config != SkBitmap::kRGB_565_Config) {
224 config = SkBitmap::kARGB_8888_Config;
225 }
226 SkBitmap bm;
227 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000228
reed@google.comaf951c92011-06-16 19:10:39 +0000229 const GrTextureDesc desc = {
230 kRenderTarget_GrTextureFlagBit,
reed@google.comaf951c92011-06-16 19:10:39 +0000231 width,
232 height,
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000233 SkGr::BitmapConfig2PixelConfig(bm.config()),
bsalomon@google.comb9014f42012-03-30 14:22:41 +0000234 0 // samples
reed@google.comaf951c92011-06-16 19:10:39 +0000235 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000236
reed@google.comaf951c92011-06-16 19:10:39 +0000237 fTexture = fContext->createUncachedTexture(desc, NULL, 0);
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000238
reed@google.comaf951c92011-06-16 19:10:39 +0000239 if (NULL != fTexture) {
240 fRenderTarget = fTexture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000241 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000242
reed@google.comaf951c92011-06-16 19:10:39 +0000243 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000244
reed@google.comaf951c92011-06-16 19:10:39 +0000245 // wrap the bitmap with a pixelref to expose our texture
246 SkGrTexturePixelRef* pr = new SkGrTexturePixelRef(fTexture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000247 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000248 } else {
249 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
250 width, height);
251 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000252 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +0000253
254 fTextContext = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000255}
256
257SkGpuDevice::~SkGpuDevice() {
258 if (fDrawProcs) {
259 delete fDrawProcs;
260 }
261
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000262 SkSafeUnref(fTexture);
263 SkSafeUnref(fRenderTarget);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000264 if (fCache.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000265 GrAssert(NULL != fTexture);
266 GrAssert(fRenderTarget == fTexture->asRenderTarget());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000267 fContext->unlockTexture(fCache);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000268 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000269 fContext->unref();
bsalomon@google.comf4a9c822012-03-16 14:02:46 +0000270
271 if (NULL != fTextContext) {
272 fTextContext->unref();
273 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000274}
275
reed@google.comac10a2d2010-12-22 21:39:39 +0000276///////////////////////////////////////////////////////////////////////////////
277
278void SkGpuDevice::makeRenderTargetCurrent() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000279 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000280 fContext->setRenderTarget(fRenderTarget);
281 fContext->flush(true);
282 fNeedPrepareRenderTarget = true;
283}
284
285///////////////////////////////////////////////////////////////////////////////
286
bsalomon@google.comc4364992011-11-07 15:54:49 +0000287namespace {
288GrPixelConfig config8888_to_gr_config(SkCanvas::Config8888 config8888) {
289 switch (config8888) {
290 case SkCanvas::kNative_Premul_Config8888:
291 return kSkia8888_PM_GrPixelConfig;
292 case SkCanvas::kNative_Unpremul_Config8888:
293 return kSkia8888_UPM_GrPixelConfig;
294 case SkCanvas::kBGRA_Premul_Config8888:
295 return kBGRA_8888_PM_GrPixelConfig;
296 case SkCanvas::kBGRA_Unpremul_Config8888:
297 return kBGRA_8888_UPM_GrPixelConfig;
298 case SkCanvas::kRGBA_Premul_Config8888:
299 return kRGBA_8888_PM_GrPixelConfig;
300 case SkCanvas::kRGBA_Unpremul_Config8888:
301 return kRGBA_8888_UPM_GrPixelConfig;
302 default:
303 GrCrash("Unexpected Config8888.");
304 return kSkia8888_PM_GrPixelConfig;
305 }
306}
307}
308
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000309bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
310 int x, int y,
311 SkCanvas::Config8888 config8888) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000312 DO_DEFERRED_CLEAR;
bsalomon@google.com910267d2011-11-02 20:06:25 +0000313 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
314 SkASSERT(!bitmap.isNull());
315 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000316
bsalomon@google.com910267d2011-11-02 20:06:25 +0000317 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000318 GrPixelConfig config;
319 config = config8888_to_gr_config(config8888);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000320 return fContext->readRenderTargetPixels(fRenderTarget,
321 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000322 bitmap.width(),
323 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000324 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000325 bitmap.getPixels(),
326 bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000327}
328
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000329void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
330 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000331 SkAutoLockPixels alp(bitmap);
332 if (!bitmap.readyToDraw()) {
333 return;
334 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000335
336 GrPixelConfig config;
337 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
338 config = config8888_to_gr_config(config8888);
339 } else {
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000340 config= SkGr::BitmapConfig2PixelConfig(bitmap.config());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000341 }
342
bsalomon@google.com6f379512011-11-16 20:36:03 +0000343 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
344 config, bitmap.getPixels(), bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000345}
346
347///////////////////////////////////////////////////////////////////////////////
348
349static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000350 const SkClipStack& clipStack,
reed@google.com6f8f2922011-03-04 22:27:10 +0000351 const SkRegion& clipRegion,
352 const SkIPoint& origin) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000353 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000354
355 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000356 iter.reset(clipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000357 const SkIRect& skBounds = clipRegion.getBounds();
358 GrRect bounds;
359 bounds.setLTRB(GrIntToScalar(skBounds.fLeft),
360 GrIntToScalar(skBounds.fTop),
361 GrIntToScalar(skBounds.fRight),
362 GrIntToScalar(skBounds.fBottom));
reed@google.com6f8f2922011-03-04 22:27:10 +0000363 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
364 &bounds);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000365 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000366}
367
368// call this ever each draw call, to ensure that the context reflects our state,
369// and not the state from some other canvas/device
370void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
371 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000372 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000373
374 fContext->setRenderTarget(fRenderTarget);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000375 SkASSERT(draw.fClipStack);
376 convert_matrixclip(fContext, *draw.fMatrix,
reed@google.com6f8f2922011-03-04 22:27:10 +0000377 *draw.fClipStack, *draw.fClip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000378 fNeedPrepareRenderTarget = false;
379 }
380}
381
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000382void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
383 const SkClipStack& clipStack) {
384 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
385 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000386 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000387}
388
389void SkGpuDevice::gainFocus(SkCanvas* canvas, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000390 const SkRegion& clip, const SkClipStack& clipStack) {
391
reed@google.comac10a2d2010-12-22 21:39:39 +0000392 fContext->setRenderTarget(fRenderTarget);
393
bsalomon@google.comd302f142011-03-03 13:54:13 +0000394 this->INHERITED::gainFocus(canvas, matrix, clip, clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000395
reed@google.com6f8f2922011-03-04 22:27:10 +0000396 convert_matrixclip(fContext, matrix, clipStack, clip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000397
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000398 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000399}
400
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000401SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000402 DO_DEFERRED_CLEAR;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000403 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000404}
405
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000406bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000407 if (NULL != fTexture) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000408 paint->setTexture(kBitmapTextureIdx, fTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000409 return true;
410 }
411 return false;
412}
413
414///////////////////////////////////////////////////////////////////////////////
415
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000416SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
417SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
418SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
419SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
420SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
421 shader_type_mismatch);
422SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 4, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000423
bsalomon@google.com84405e02012-03-05 19:57:21 +0000424namespace {
425
426// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
427// justAlpha indicates that skPaint's alpha should be used rather than the color
428// Callers may subsequently modify the GrPaint. Setting constantColor indicates
429// that the final paint will draw the same color at every pixel. This allows
430// an optimization where the the color filter can be applied to the skPaint's
431// color once while converting to GrPain and then ignored.
432inline bool skPaint2GrPaintNoShader(const SkPaint& skPaint,
433 bool justAlpha,
434 bool constantColor,
435 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000436
437 grPaint->fDither = skPaint.isDither();
438 grPaint->fAntiAlias = skPaint.isAntiAlias();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000439 grPaint->fCoverage = 0xFF;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000440
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000441 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
442 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000443
444 SkXfermode* mode = skPaint.getXfermode();
445 if (mode) {
446 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000447 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000448#if 0
449 return false;
450#endif
451 }
452 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000453 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
454 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
455
bsalomon@google.com5782d712011-01-21 21:03:59 +0000456 if (justAlpha) {
457 uint8_t alpha = skPaint.getAlpha();
458 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000459 // justAlpha is currently set to true only if there is a texture,
460 // so constantColor should not also be true.
461 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000462 } else {
463 grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000464 grPaint->setTexture(kShaderTextureIdx, NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000465 }
Scroggo97c88c22011-05-11 14:05:25 +0000466 SkColorFilter* colorFilter = skPaint.getColorFilter();
467 SkColor color;
468 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000469 SkScalar matrix[20];
Scroggo97c88c22011-05-11 14:05:25 +0000470 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000471 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000472 if (!constantColor) {
473 grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color);
474 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000475 } else {
476 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
477 grPaint->fColor = SkGr::SkColor2GrColor(filtered);
senorblanco@chromium.orgb3c20fa2012-01-03 21:20:19 +0000478 grPaint->resetColorFilter();
Scroggod757df22011-05-16 13:11:16 +0000479 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000480 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
481 grPaint->fColorMatrixEnabled = true;
482 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
483 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
484 } else {
485 grPaint->resetColorFilter();
Scroggo97c88c22011-05-11 14:05:25 +0000486 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000487 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000488}
489
bsalomon@google.com84405e02012-03-05 19:57:21 +0000490// This function is similar to skPaint2GrPaintNoShader but also converts
491// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
492// be used is set on grPaint and returned in param act. constantColor has the
493// same meaning as in skPaint2GrPaintNoShader.
494inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
495 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000496 bool constantColor,
497 SkGpuDevice::SkAutoCachedTexture* act,
498 GrPaint* grPaint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000499
bsalomon@google.com5782d712011-01-21 21:03:59 +0000500 SkASSERT(NULL != act);
reed@google.comac10a2d2010-12-22 21:39:39 +0000501
bsalomon@google.com5782d712011-01-21 21:03:59 +0000502 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000503 if (NULL == shader) {
bsalomon@google.com84405e02012-03-05 19:57:21 +0000504 return skPaint2GrPaintNoShader(skPaint,
505 false,
506 constantColor,
507 grPaint);
508 } else if (!skPaint2GrPaintNoShader(skPaint, true, false, grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000509 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000510 }
511
reed@google.comac10a2d2010-12-22 21:39:39 +0000512 SkBitmap bitmap;
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000513 SkMatrix* matrix = grPaint->textureSampler(kShaderTextureIdx)->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000514 SkShader::TileMode tileModes[2];
515 SkScalar twoPointParams[3];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000516 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000517 tileModes, twoPointParams);
518
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000519 if (SkShader::kNone_BitmapType == bmptype) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000520 SkShader::GradientInfo info;
521 SkColor color;
522
523 info.fColors = &color;
524 info.fColorOffsets = NULL;
525 info.fColorCount = 1;
526 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
527 SkPaint copy(skPaint);
528 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000529 // modulate the paint alpha by the shader's solid color alpha
530 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
531 copy.setColor(SkColorSetA(color, newA));
bsalomon@google.com84405e02012-03-05 19:57:21 +0000532 return skPaint2GrPaintNoShader(copy,
533 false,
534 constantColor,
535 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000536 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000537 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000538 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000539 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000540 switch (bmptype) {
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000541 case SkShader::kRadial_BitmapType:
542 sampler->setCustomStage(new GrRadialGradient())->unref();
543 sampler->setFilter(GrSamplerState::kBilinear_Filter);
544 break;
545 case SkShader::kSweep_BitmapType:
546 sampler->setCustomStage(new GrSweepGradient())->unref();
547 sampler->setFilter(GrSamplerState::kBilinear_Filter);
548 break;
549 case SkShader::kTwoPointRadial_BitmapType:
550 sampler->setCustomStage(new
551 GrRadial2Gradient(twoPointParams[0],
552 twoPointParams[1],
553 twoPointParams[2] < 0))->unref();
554 sampler->setFilter(GrSamplerState::kBilinear_Filter);
555 break;
556 default:
557 if (skPaint.isFilterBitmap()) {
558 sampler->setFilter(GrSamplerState::kBilinear_Filter);
559 } else {
560 sampler->setFilter(GrSamplerState::kNearest_Filter);
561 }
562 break;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000563 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000564 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
565 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000566
bsalomon@google.com84405e02012-03-05 19:57:21 +0000567 GrTexture* texture = act->set(dev, bitmap, sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000568 if (NULL == texture) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000569 SkDebugf("Couldn't convert bitmap to texture.\n");
570 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000571 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000572 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000573
574 // since our texture coords will be in local space, we wack the texture
575 // matrix to map them back into 0...1 before we load it
576 SkMatrix localM;
577 if (shader->getLocalMatrix(&localM)) {
578 SkMatrix inverse;
579 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000580 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000581 }
582 }
583 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000584 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
585 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000586 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000587 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000588 GrScalar s = SkFloatToScalar(1.f / bitmap.width());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000589 matrix->postScale(s, s);
reed@google.comac10a2d2010-12-22 21:39:39 +0000590 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000591
592 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000593}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000594}
reed@google.comac10a2d2010-12-22 21:39:39 +0000595
596///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com5782d712011-01-21 21:03:59 +0000597
bsalomon@google.com398109c2011-04-14 18:40:27 +0000598void SkGpuDevice::clear(SkColor color) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000599 fContext->setRenderTarget(fRenderTarget);
bsalomon@google.com31a58402011-04-27 21:00:02 +0000600 fContext->clear(NULL, color);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000601}
602
reed@google.comac10a2d2010-12-22 21:39:39 +0000603void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
604 CHECK_SHOULD_DRAW(draw);
605
bsalomon@google.com5782d712011-01-21 21:03:59 +0000606 GrPaint grPaint;
607 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000608 if (!skPaint2GrPaintShader(this,
609 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000610 true,
611 &act,
612 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000613 return;
614 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000615
616 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000617}
618
619// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000620static const GrPrimitiveType gPointMode2PrimtiveType[] = {
621 kPoints_PrimitiveType,
622 kLines_PrimitiveType,
623 kLineStrip_PrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000624};
625
626void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000627 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000628 CHECK_SHOULD_DRAW(draw);
629
630 SkScalar width = paint.getStrokeWidth();
631 if (width < 0) {
632 return;
633 }
634
635 // we only handle hairlines here, else we let the SkDraw call our drawPath()
636 if (width > 0) {
637 draw.drawPoints(mode, count, pts, paint, true);
638 return;
639 }
640
bsalomon@google.com5782d712011-01-21 21:03:59 +0000641 GrPaint grPaint;
642 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000643 if (!skPaint2GrPaintShader(this,
644 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000645 true,
646 &act,
647 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000648 return;
649 }
650
bsalomon@google.com5782d712011-01-21 21:03:59 +0000651 fContext->drawVertices(grPaint,
652 gPointMode2PrimtiveType[mode],
653 count,
654 (GrPoint*)pts,
655 NULL,
656 NULL,
657 NULL,
658 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000659}
660
reed@google.comc9aa5872011-04-05 21:05:37 +0000661///////////////////////////////////////////////////////////////////////////////
662
reed@google.comac10a2d2010-12-22 21:39:39 +0000663void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
664 const SkPaint& paint) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000665 CHECK_SHOULD_DRAW(draw);
666
bungeman@google.com79bd8772011-07-18 15:34:08 +0000667 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000668 SkScalar width = paint.getStrokeWidth();
669
670 /*
671 We have special code for hairline strokes, miter-strokes, and fills.
672 Anything else we just call our path code.
673 */
674 bool usePath = doStroke && width > 0 &&
675 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000676 // another two reasons we might need to call drawPath...
677 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000678 usePath = true;
679 }
reed@google.com67db6642011-05-26 11:46:35 +0000680 // until we aa rotated rects...
681 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
682 usePath = true;
683 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000684 // small miter limit means right angles show bevel...
685 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
686 paint.getStrokeMiter() < SK_ScalarSqrt2)
687 {
688 usePath = true;
689 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000690 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000691 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
692 usePath = true;
693 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000694
695 if (usePath) {
696 SkPath path;
697 path.addRect(rect);
698 this->drawPath(draw, path, paint, NULL, true);
699 return;
700 }
701
702 GrPaint grPaint;
703 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000704 if (!skPaint2GrPaintShader(this,
705 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000706 true,
707 &act,
708 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000709 return;
710 }
reed@google.com20efde72011-05-09 17:00:02 +0000711 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000712}
713
reed@google.com69302852011-02-16 18:08:07 +0000714#include "SkMaskFilter.h"
715#include "SkBounder.h"
716
bsalomon@google.com85003222012-03-28 14:44:37 +0000717///////////////////////////////////////////////////////////////////////////////
718
719// helpers for applying mask filters
720namespace {
721
722GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000723 switch (fillType) {
724 case SkPath::kWinding_FillType:
725 return kWinding_PathFill;
726 case SkPath::kEvenOdd_FillType:
727 return kEvenOdd_PathFill;
728 case SkPath::kInverseWinding_FillType:
729 return kInverseWinding_PathFill;
730 case SkPath::kInverseEvenOdd_FillType:
731 return kInverseEvenOdd_PathFill;
732 default:
733 SkDebugf("Unsupported path fill type\n");
734 return kHairLine_PathFill;
735 }
736}
737
bsalomon@google.com85003222012-03-28 14:44:37 +0000738// We prefer to blur small rect with small radius via CPU.
739#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
740#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
741inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
742 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
743 rect.height() <= MIN_GPU_BLUR_SIZE &&
744 radius <= MIN_GPU_BLUR_RADIUS) {
745 return true;
746 }
747 return false;
748}
749
750bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
751 SkMaskFilter* filter, const SkMatrix& matrix,
752 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000753 GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000754#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000755 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000756#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000757 SkMaskFilter::BlurInfo info;
758 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000759 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000760 return false;
761 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000762 SkScalar radius = info.fIgnoreTransform ? info.fRadius
763 : matrix.mapRadius(info.fRadius);
764 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000765 if (radius <= 0) {
766 return false;
767 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000768
769 SkRect srcRect = path.getBounds();
770 if (shouldDrawBlurWithCPU(srcRect, radius)) {
771 return false;
772 }
773
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000774 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000775 float sigma3 = sigma * 3.0f;
776
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000777 SkRect clipRect;
778 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000779
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000780 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000781 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
782 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000783 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000784 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000785 SkIRect finalIRect;
786 finalRect.roundOut(&finalIRect);
787 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000788 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000789 }
790 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000791 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000792 }
793 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000794 srcRect.offset(offset);
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000795 GrTextureDesc desc = {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000796 kRenderTarget_GrTextureFlagBit,
bungeman@google.comf8aa18c2012-03-19 21:04:52 +0000797 SkScalarCeilToInt(srcRect.width()),
798 SkScalarCeilToInt(srcRect.height()),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000799 // We actually only need A8, but it often isn't supported as a
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000800 // render target so default to RGBA_8888
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000801 kRGBA_8888_PM_GrPixelConfig,
bsalomon@google.comb9014f42012-03-30 14:22:41 +0000802 0 // samples
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000803 };
804
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000805 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
806 desc.fConfig = kAlpha_8_GrPixelConfig;
807 }
808
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000809 GrAutoScratchTexture pathEntry(context, desc);
810 GrTexture* pathTexture = pathEntry.texture();
811 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000812 return false;
813 }
814 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000815 // Once this code moves into GrContext, this should be changed to use
816 // an AutoClipRestore.
817 GrClip oldClip = context->getClip();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000818 context->setRenderTarget(pathTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000819 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000820 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000821 GrPaint tempPaint;
822 tempPaint.reset();
823
824 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000825 tempPaint.fAntiAlias = grp->fAntiAlias;
826 if (tempPaint.fAntiAlias) {
827 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
828 // blend coeff of zero requires dual source blending support in order
829 // to properly blend partially covered pixels. This means the AA
830 // code path may not be taken. So we use a dst blend coeff of ISA. We
831 // could special case AA draws to a dst surface with known alpha=0 to
832 // use a zero dst coeff when dual source blending isn't available.
833 tempPaint.fSrcBlendCoeff = kOne_BlendCoeff;
834 tempPaint.fDstBlendCoeff = kISC_BlendCoeff;
835 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000836 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000837 context->drawPath(tempPaint, path, pathFillType, &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000838
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000839 GrAutoScratchTexture temp1, temp2;
840 // If we're doing a normal blur, we can clobber the pathTexture in the
841 // gaussianBlur. Otherwise, we need to save it for later compositing.
842 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000843 GrTexture* blurTexture = context->gaussianBlur(pathTexture,
844 &temp1,
845 isNormalBlur ? NULL : &temp2,
846 srcRect, sigma, sigma);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000847
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000848 if (!isNormalBlur) {
849 GrPaint paint;
850 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000851 paint.textureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000852 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
853 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000854 // Blend pathTexture over blurTexture.
855 context->setRenderTarget(blurTexture->asRenderTarget());
856 paint.setTexture(0, pathTexture);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000857 if (SkMaskFilter::kInner_BlurType == blurType) {
858 // inner: dst = dst * src
859 paint.fSrcBlendCoeff = kDC_BlendCoeff;
860 paint.fDstBlendCoeff = kZero_BlendCoeff;
861 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
862 // solid: dst = src + dst - src * dst
863 // = (1 - dst) * src + 1 * dst
864 paint.fSrcBlendCoeff = kIDC_BlendCoeff;
865 paint.fDstBlendCoeff = kOne_BlendCoeff;
866 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
867 // outer: dst = dst * (1 - src)
868 // = 0 * src + (1 - src) * dst
869 paint.fSrcBlendCoeff = kZero_BlendCoeff;
870 paint.fDstBlendCoeff = kISC_BlendCoeff;
871 }
872 context->drawRect(paint, srcRect);
873 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000874 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000875 context->setClip(oldClip);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000876
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000877 if (grp->hasTextureOrMask()) {
878 GrMatrix inverse;
879 if (!matrix.invert(&inverse)) {
880 return false;
881 }
882 grp->preConcatActiveSamplerMatrices(inverse);
883 }
884
885 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
886 // we assume the last mask index is available for use
887 GrAssert(NULL == grp->getMask(MASK_IDX));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000888 grp->setMask(MASK_IDX, blurTexture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000889 grp->maskSampler(MASK_IDX)->reset();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000890
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000891 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
892 -finalRect.fTop);
893 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
894 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000895 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000896 return true;
897}
898
bsalomon@google.com85003222012-03-28 14:44:37 +0000899bool drawWithMaskFilter(GrContext* context, const SkPath& path,
900 SkMaskFilter* filter, const SkMatrix& matrix,
901 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000902 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000903 SkMask srcM, dstM;
904
905 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000906 SkMask::kComputeBoundsAndRenderImage_CreateMode,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000907 style)) {
reed@google.com69302852011-02-16 18:08:07 +0000908 return false;
909 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000910 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000911
912 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
913 return false;
914 }
915 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000916 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000917
918 if (clip.quickReject(dstM.fBounds)) {
919 return false;
920 }
921 if (bounder && !bounder->doIRect(dstM.fBounds)) {
922 return false;
923 }
924
925 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
926 // the current clip (and identity matrix) and grpaint settings
927
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000928 // used to compute inverse view, if necessary
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000929 GrMatrix ivm = matrix;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000930
reed@google.com0c219b62011-02-16 21:31:18 +0000931 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +0000932
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000933 const GrTextureDesc desc = {
934 kNone_GrTextureFlags,
reed@google.com69302852011-02-16 18:08:07 +0000935 dstM.fBounds.width(),
936 dstM.fBounds.height(),
bsalomon@google.com78d6cf92012-01-30 18:09:31 +0000937 kAlpha_8_GrPixelConfig,
bsalomon@google.comb9014f42012-03-30 14:22:41 +0000938 0, // samples
reed@google.com69302852011-02-16 18:08:07 +0000939 };
940
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000941 GrAutoScratchTexture ast(context, desc);
942 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000943
reed@google.com69302852011-02-16 18:08:07 +0000944 if (NULL == texture) {
945 return false;
946 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000947 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000948 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000949
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000950 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
951 grp->preConcatActiveSamplerMatrices(ivm);
952 }
953
954 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
955 // we assume the last mask index is available for use
956 GrAssert(NULL == grp->getMask(MASK_IDX));
957 grp->setMask(MASK_IDX, texture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000958 grp->maskSampler(MASK_IDX)->reset();
reed@google.com69302852011-02-16 18:08:07 +0000959
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000960 GrRect d;
961 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +0000962 GrIntToScalar(dstM.fBounds.fTop),
963 GrIntToScalar(dstM.fBounds.fRight),
964 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000965
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000966 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
967 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
968 -dstM.fBounds.fTop*SK_Scalar1);
969 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000970 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000971 return true;
972}
reed@google.com69302852011-02-16 18:08:07 +0000973
bsalomon@google.com85003222012-03-28 14:44:37 +0000974}
975
976///////////////////////////////////////////////////////////////////////////////
977
reed@google.com0c219b62011-02-16 21:31:18 +0000978void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000979 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000980 bool pathIsMutable) {
981 CHECK_SHOULD_DRAW(draw);
982
reed@google.comfe626382011-09-21 13:50:35 +0000983 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000984
bsalomon@google.com5782d712011-01-21 21:03:59 +0000985 GrPaint grPaint;
986 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000987 if (!skPaint2GrPaintShader(this,
988 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000989 true,
990 &act,
991 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000992 return;
993 }
994
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000995 // can we cheat, and threat a thin stroke as a hairline w/ coverage
996 // if we can, we draw lots faster (raster device does this same test)
997 SkScalar hairlineCoverage;
998 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
999 doFill = false;
1000 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
1001 grPaint.fCoverage);
1002 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001003
reed@google.comfe626382011-09-21 13:50:35 +00001004 // If we have a prematrix, apply it to the path, optimizing for the case
1005 // where the original path can in fact be modified in place (even though
1006 // its parameter type is const).
1007 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1008 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001009
1010 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001011 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001012
reed@google.come3445642011-02-16 23:20:39 +00001013 if (!pathIsMutable) {
1014 result = &tmpPath;
1015 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001016 }
reed@google.come3445642011-02-16 23:20:39 +00001017 // should I push prePathMatrix on our MV stack temporarily, instead
1018 // of applying it here? See SkDraw.cpp
1019 pathPtr->transform(*prePathMatrix, result);
1020 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001021 }
reed@google.com0c219b62011-02-16 21:31:18 +00001022 // at this point we're done with prePathMatrix
1023 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001024
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001025 if (paint.getPathEffect() ||
1026 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001027 // it is safe to use tmpPath here, even if we already used it for the
1028 // prepathmatrix, since getFillPath can take the same object for its
1029 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001030 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001031 pathPtr = &tmpPath;
1032 }
1033
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001034 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001035 // avoid possibly allocating a new path in transform if we can
1036 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1037
1038 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001039 pathPtr->transform(*draw.fMatrix, devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001040 GrPathFill pathFillType = doFill ?
1041 skToGrFillType(devPathPtr->getFillType()) : kHairLine_PathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001042 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001043 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001044 &grPaint, pathFillType)) {
1045 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
1046 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001047 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001048 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001049 &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001050 }
reed@google.com69302852011-02-16 18:08:07 +00001051 return;
1052 }
reed@google.com69302852011-02-16 18:08:07 +00001053
bsalomon@google.comffca4002011-02-22 20:34:01 +00001054 GrPathFill fill = kHairLine_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001055
reed@google.com0c219b62011-02-16 21:31:18 +00001056 if (doFill) {
1057 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001058 case SkPath::kWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001059 fill = kWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001060 break;
1061 case SkPath::kEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001062 fill = kEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001063 break;
1064 case SkPath::kInverseWinding_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001065 fill = kInverseWinding_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001066 break;
1067 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.comffca4002011-02-22 20:34:01 +00001068 fill = kInverseEvenOdd_PathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001069 break;
1070 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001071 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001072 return;
1073 }
1074 }
1075
reed@google.com07f3ee12011-05-16 17:21:57 +00001076 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001077}
1078
bsalomon@google.comfb309512011-11-30 14:13:48 +00001079namespace {
1080
1081inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1082 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1083 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1084 return tilesX * tilesY;
1085}
1086
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001087inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001088 const SkIRect* srcRectPtr,
1089 int maxTextureSize) {
1090 static const int kSmallTileSize = 1 << 10;
1091 if (maxTextureSize <= kSmallTileSize) {
1092 return maxTextureSize;
1093 }
1094
1095 size_t maxTexTotalTileSize;
1096 size_t smallTotalTileSize;
1097
1098 if (NULL == srcRectPtr) {
1099 int w = bitmap.width();
1100 int h = bitmap.height();
1101 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1102 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1103 } else {
1104 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1105 srcRectPtr->fTop,
1106 srcRectPtr->fRight,
1107 srcRectPtr->fBottom,
1108 maxTextureSize);
1109 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1110 srcRectPtr->fTop,
1111 srcRectPtr->fRight,
1112 srcRectPtr->fBottom,
1113 kSmallTileSize);
1114 }
1115 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1116 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1117
1118 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1119 return kSmallTileSize;
1120 } else {
1121 return maxTextureSize;
1122 }
1123}
1124}
1125
1126bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1127 const GrSamplerState& sampler,
1128 const SkIRect* srcRectPtr,
1129 int* tileSize) const {
1130 SkASSERT(NULL != tileSize);
1131
1132 // if bitmap is explictly texture backed then just use the texture
1133 if (NULL != bitmap.getTexture()) {
1134 return false;
1135 }
1136 // if it's larger than the max texture size, then we have no choice but
1137 // tiling
1138 const int maxTextureSize = fContext->getMaxTextureSize();
1139 if (bitmap.width() > maxTextureSize ||
1140 bitmap.height() > maxTextureSize) {
1141 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1142 return true;
1143 }
1144 // if we are going to have to draw the whole thing, then don't tile
1145 if (NULL == srcRectPtr) {
1146 return false;
1147 }
1148 // if the entire texture is already in our cache then no reason to tile it
1149 if (this->isBitmapInTextureCache(bitmap, sampler)) {
1150 return false;
1151 }
1152
1153 // At this point we know we could do the draw by uploading the entire bitmap
1154 // as a texture. However, if the texture would be large compared to the
1155 // cache size and we don't require most of it for this draw then tile to
1156 // reduce the amount of upload and cache spill.
1157
1158 // assumption here is that sw bitmap size is a good proxy for its size as
1159 // a texture
1160 size_t bmpSize = bitmap.getSize();
1161 size_t cacheSize;
1162 fContext->getTextureCacheLimits(NULL, &cacheSize);
1163 if (bmpSize < cacheSize / 2) {
1164 return false;
1165 }
1166
1167 SkFixed fracUsed =
1168 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1169 (srcRectPtr->height() << 16) / bitmap.height());
1170 if (fracUsed <= SK_FixedHalf) {
1171 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1172 return true;
1173 } else {
1174 return false;
1175 }
1176}
1177
reed@google.comac10a2d2010-12-22 21:39:39 +00001178void SkGpuDevice::drawBitmap(const SkDraw& draw,
1179 const SkBitmap& bitmap,
1180 const SkIRect* srcRectPtr,
1181 const SkMatrix& m,
1182 const SkPaint& paint) {
1183 CHECK_SHOULD_DRAW(draw);
1184
1185 SkIRect srcRect;
1186 if (NULL == srcRectPtr) {
1187 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1188 } else {
1189 srcRect = *srcRectPtr;
1190 }
1191
junov@google.comd935cfb2011-06-27 20:48:23 +00001192 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001193 // Convert the bitmap to a shader so that the rect can be drawn
1194 // through drawRect, which supports mask filters.
1195 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001196 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001197 if (srcRectPtr) {
1198 if (!bitmap.extractSubset(&tmp, srcRect)) {
1199 return; // extraction failed
1200 }
1201 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001202 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001203 }
1204 SkPaint paintWithTexture(paint);
1205 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1206 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001207 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001208 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001209
junov@google.com1d329782011-07-28 20:10:09 +00001210 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001211 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001212 // also affect the behavior of the mask filter.
1213 SkMatrix drawMatrix;
1214 drawMatrix.setConcat(*draw.fMatrix, m);
1215 SkDraw transformedDraw(draw);
1216 transformedDraw.fMatrix = &drawMatrix;
1217
1218 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1219
junov@google.comd935cfb2011-06-27 20:48:23 +00001220 return;
1221 }
1222
bsalomon@google.com5782d712011-01-21 21:03:59 +00001223 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001224 if (!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001225 return;
1226 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001227 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001228 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001229 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001230 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001231 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001232 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001233
bsalomon@google.comfb309512011-11-30 14:13:48 +00001234 int tileSize;
1235 if (!this->shouldTileBitmap(bitmap, *sampler, srcRectPtr, &tileSize)) {
1236 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001237 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001238 return;
1239 }
1240
1241 // undo the translate done by SkCanvas
1242 int DX = SkMax32(0, srcRect.fLeft);
1243 int DY = SkMax32(0, srcRect.fTop);
1244 // compute clip bounds in local coordinates
1245 SkIRect clipRect;
1246 {
1247 SkRect r;
1248 r.set(draw.fClip->getBounds());
1249 SkMatrix matrix, inverse;
1250 matrix.setConcat(*draw.fMatrix, m);
1251 if (!matrix.invert(&inverse)) {
1252 return;
1253 }
1254 inverse.mapRect(&r);
1255 r.roundOut(&clipRect);
1256 // apply the canvas' translate to our local clip
1257 clipRect.offset(DX, DY);
1258 }
1259
bsalomon@google.comfb309512011-11-30 14:13:48 +00001260 int nx = bitmap.width() / tileSize;
1261 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001262 for (int x = 0; x <= nx; x++) {
1263 for (int y = 0; y <= ny; y++) {
1264 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001265 tileR.set(x * tileSize, y * tileSize,
1266 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001267 if (!SkIRect::Intersects(tileR, clipRect)) {
1268 continue;
1269 }
1270
1271 SkIRect srcR = tileR;
1272 if (!srcR.intersect(srcRect)) {
1273 continue;
1274 }
1275
1276 SkBitmap tmpB;
1277 if (bitmap.extractSubset(&tmpB, tileR)) {
1278 // now offset it to make it "local" to our tmp bitmap
1279 srcR.offset(-tileR.fLeft, -tileR.fTop);
1280
1281 SkMatrix tmpM(m);
1282 {
1283 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1284 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1285 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1286 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001287 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001288 }
1289 }
1290 }
1291}
1292
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001293namespace {
1294
1295bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1296 // detect pixel disalignment
1297 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1298 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1299 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1300 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1301 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1302 COLOR_BLEED_TOLERANCE &&
1303 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1304 COLOR_BLEED_TOLERANCE) {
1305 return true;
1306 }
1307 return false;
1308}
1309
1310bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1311 const SkMatrix& m) {
1312 // Only gets called if hasAlignedSamples returned false.
1313 // So we can assume that sampling is axis aligned but not texel aligned.
1314 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
1315 SkRect innerSrcRect(srcRect), innerTransformedRect,
1316 outerTransformedRect(transformedRect);
1317 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1318 m.mapRect(&innerTransformedRect, innerSrcRect);
1319
1320 // The gap between outerTransformedRect and innerTransformedRect
1321 // represents the projection of the source border area, which is
1322 // problematic for color bleeding. We must check whether any
1323 // destination pixels sample the border area.
1324 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1325 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1326 SkIRect outer, inner;
1327 outerTransformedRect.round(&outer);
1328 innerTransformedRect.round(&inner);
1329 // If the inner and outer rects round to the same result, it means the
1330 // border does not overlap any pixel centers. Yay!
1331 return inner != outer;
1332}
1333
1334} // unnamed namespace
1335
reed@google.comac10a2d2010-12-22 21:39:39 +00001336/*
1337 * This is called by drawBitmap(), which has to handle images that may be too
1338 * large to be represented by a single texture.
1339 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001340 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1341 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001342 */
1343void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1344 const SkBitmap& bitmap,
1345 const SkIRect& srcRect,
1346 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001347 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001348 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1349 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001350
reed@google.com9c49bc32011-07-07 13:42:37 +00001351 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001352 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001353 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001354 return;
1355 }
1356
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001357 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001358
1359 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1360 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001361 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001362
1363 GrTexture* texture;
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001364 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001365 if (NULL == texture) {
1366 return;
1367 }
1368
bsalomon@google.com452943d2011-10-31 17:37:14 +00001369 grPaint->setTexture(kBitmapTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001370
reed@google.com20efde72011-05-09 17:00:02 +00001371 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1372 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001373 GrRect paintRect;
bsalomon@google.com91832162012-03-08 19:53:02 +00001374 float wInv = 1.f / bitmap.width();
1375 float hInv = 1.f / bitmap.height();
1376 paintRect.setLTRB(SkFloatToScalar(srcRect.fLeft * wInv),
1377 SkFloatToScalar(srcRect.fTop * hInv),
1378 SkFloatToScalar(srcRect.fRight * wInv),
1379 SkFloatToScalar(srcRect.fBottom * hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001380
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001381 bool needsTextureDomain = false;
1382 if (GrSamplerState::kBilinear_Filter == sampler->getFilter())
1383 {
1384 // Need texture domain if drawing a sub rect.
1385 needsTextureDomain = srcRect.width() < bitmap.width() ||
1386 srcRect.height() < bitmap.height();
1387 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1388 // sampling is axis-aligned
1389 GrRect floatSrcRect, transformedRect;
1390 floatSrcRect.set(srcRect);
1391 SkMatrix srcToDeviceMatrix(m);
1392 srcToDeviceMatrix.postConcat(*draw.fMatrix);
1393 srcToDeviceMatrix.mapRect(&transformedRect, floatSrcRect);
1394
1395 if (hasAlignedSamples(floatSrcRect, transformedRect)) {
1396 // Samples are texel-aligned, so filtering is futile
1397 sampler->setFilter(GrSamplerState::kNearest_Filter);
1398 needsTextureDomain = false;
1399 } else {
1400 needsTextureDomain = needsTextureDomain &&
1401 mayColorBleed(floatSrcRect, transformedRect, m);
1402 }
1403 }
1404 }
1405
1406 GrRect textureDomain = GrRect::MakeEmpty();
1407
1408 if (needsTextureDomain) {
1409 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001410 GrScalar left, top, right, bottom;
1411 if (srcRect.width() > 1) {
1412 GrScalar border = GR_ScalarHalf / bitmap.width();
1413 left = paintRect.left() + border;
1414 right = paintRect.right() - border;
1415 } else {
1416 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1417 }
1418 if (srcRect.height() > 1) {
1419 GrScalar border = GR_ScalarHalf / bitmap.height();
1420 top = paintRect.top() + border;
1421 bottom = paintRect.bottom() - border;
1422 } else {
1423 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1424 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001425 textureDomain.setLTRB(left, top, right, bottom);
junov@google.com6acc9b32011-05-16 18:32:07 +00001426 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001427 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001428
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001429 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001430}
1431
reed@google.com8926b162012-03-23 15:36:36 +00001432static GrTexture* filter_texture(GrContext* context, GrTexture* texture,
1433 SkImageFilter* filter, const GrRect& rect) {
1434 GrAssert(filter);
1435
1436 SkSize blurSize;
1437 SkISize radius;
1438
1439 const GrTextureDesc desc = {
1440 kRenderTarget_GrTextureFlagBit,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001441 SkScalarCeilToInt(rect.width()),
1442 SkScalarCeilToInt(rect.height()),
reed@google.com8926b162012-03-23 15:36:36 +00001443 kRGBA_8888_PM_GrPixelConfig,
bsalomon@google.comb9014f42012-03-30 14:22:41 +00001444 0 // samples
reed@google.com8926b162012-03-23 15:36:36 +00001445 };
1446
1447 if (filter->asABlur(&blurSize)) {
1448 GrAutoScratchTexture temp1, temp2;
1449 texture = context->gaussianBlur(texture, &temp1, &temp2, rect,
1450 blurSize.width(),
1451 blurSize.height());
1452 texture->ref();
1453 } else if (filter->asADilate(&radius)) {
1454 GrAutoScratchTexture temp1(context, desc), temp2(context, desc);
1455 texture = context->applyMorphology(texture, rect,
1456 temp1.texture(), temp2.texture(),
bsalomon@google.comb505a122012-05-31 18:40:36 +00001457 GrContext::kDilate_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001458 radius);
1459 texture->ref();
1460 } else if (filter->asAnErode(&radius)) {
1461 GrAutoScratchTexture temp1(context, desc), temp2(context, desc);
1462 texture = context->applyMorphology(texture, rect,
1463 temp1.texture(), temp2.texture(),
bsalomon@google.comb505a122012-05-31 18:40:36 +00001464 GrContext::kErode_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001465 radius);
1466 texture->ref();
1467 }
1468 return texture;
1469}
1470
reed@google.comac10a2d2010-12-22 21:39:39 +00001471void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1472 int left, int top, const SkPaint& paint) {
1473 CHECK_SHOULD_DRAW(draw);
1474
reed@google.com8926b162012-03-23 15:36:36 +00001475 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001476 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1477 return;
1478 }
1479
reed@google.com76dd2772012-01-05 21:15:07 +00001480 int w = bitmap.width();
1481 int h = bitmap.height();
1482
bsalomon@google.com5782d712011-01-21 21:03:59 +00001483 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001484 if(!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001485 return;
1486 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001487
bsalomon@google.com5782d712011-01-21 21:03:59 +00001488 GrAutoMatrix avm(fContext, GrMatrix::I());
1489
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001490 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001491
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001492 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001493 sampler->reset();
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001494 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001495 grPaint.setTexture(kBitmapTextureIdx, texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001496
reed@google.com8926b162012-03-23 15:36:36 +00001497 SkImageFilter* filter = paint.getImageFilter();
1498 if (NULL != filter) {
1499 GrTexture* filteredTexture = filter_texture(fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001500 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001501 if (filteredTexture) {
1502 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1503 texture = filteredTexture;
1504 filteredTexture->unref();
1505 }
reed@google.com76dd2772012-01-05 21:15:07 +00001506 }
reed@google.com8926b162012-03-23 15:36:36 +00001507
bsalomon@google.com5782d712011-01-21 21:03:59 +00001508 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001509 GrRect::MakeXYWH(GrIntToScalar(left),
1510 GrIntToScalar(top),
1511 GrIntToScalar(w),
1512 GrIntToScalar(h)),
1513 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1514 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001515}
1516
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001517void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
reed@google.comac10a2d2010-12-22 21:39:39 +00001518 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001519 // clear of the source device must occur before CHECK_SHOULD_DRAW
1520 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1521 if (dev->fNeedClear) {
1522 // TODO: could check here whether we really need to draw at all
1523 dev->clear(0x0);
1524 }
1525
reed@google.comac10a2d2010-12-22 21:39:39 +00001526 CHECK_SHOULD_DRAW(draw);
1527
bsalomon@google.com5782d712011-01-21 21:03:59 +00001528 GrPaint grPaint;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001529 if (!dev->bindDeviceAsTexture(&grPaint) ||
bsalomon@google.com84405e02012-03-05 19:57:21 +00001530 !skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001531 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001532 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001533
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001534 GrTexture* devTex = grPaint.getTexture(0);
1535 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001536
reed@google.com8926b162012-03-23 15:36:36 +00001537 SkImageFilter* filter = paint.getImageFilter();
1538 if (NULL != filter) {
robertphillips@google.com8637a362012-04-10 18:32:35 +00001539 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
1540 SkIntToScalar(devTex->height()));
reed@google.com8926b162012-03-23 15:36:36 +00001541 GrTexture* filteredTexture = filter_texture(fContext, devTex, filter,
1542 rect);
1543 if (filteredTexture) {
1544 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1545 devTex = filteredTexture;
1546 filteredTexture->unref();
1547 }
1548 }
1549
bsalomon@google.com5782d712011-01-21 21:03:59 +00001550 const SkBitmap& bm = dev->accessBitmap(false);
1551 int w = bm.width();
1552 int h = bm.height();
1553
1554 GrAutoMatrix avm(fContext, GrMatrix::I());
1555
bsalomon@google.com97912912011-12-06 16:30:36 +00001556 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001557
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001558 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1559 GrIntToScalar(y),
1560 GrIntToScalar(w),
1561 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001562
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001563 // The device being drawn may not fill up its texture (saveLayer uses
1564 // the approximate ).
1565 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1566 GR_Scalar1 * h / devTex->height());
1567
1568 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001569}
1570
reed@google.com8926b162012-03-23 15:36:36 +00001571bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
reed@google.com76dd2772012-01-05 21:15:07 +00001572 SkSize size;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001573 SkISize radius;
1574 if (!filter->asABlur(&size) && !filter->asADilate(&radius) && !filter->asAnErode(&radius)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001575 return false;
1576 }
reed@google.com8926b162012-03-23 15:36:36 +00001577 return true;
1578}
1579
1580bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1581 const SkMatrix& ctm,
1582 SkBitmap* result, SkIPoint* offset) {
1583 // want explicitly our impl, so guard against a subclass of us overriding it
1584 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001585 return false;
1586 }
reed@google.com8926b162012-03-23 15:36:36 +00001587
1588 SkAutoLockPixels alp(src, !src.getTexture());
1589 if (!src.getTexture() && !src.readyToDraw()) {
1590 return false;
1591 }
1592
1593 GrPaint paint;
1594 paint.reset();
1595
1596 GrSamplerState* sampler = paint.textureSampler(kBitmapTextureIdx);
1597
1598 GrTexture* texture;
1599 SkAutoCachedTexture act(this, src, sampler, &texture);
1600
1601 result->setConfig(src.config(), src.width(), src.height());
robertphillips@google.com8637a362012-04-10 18:32:35 +00001602 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
1603 SkIntToScalar(src.height()));
reed@google.com8926b162012-03-23 15:36:36 +00001604 GrTexture* resultTexture = filter_texture(fContext, texture, filter, rect);
1605 if (resultTexture) {
1606 result->setPixelRef(new SkGrTexturePixelRef(resultTexture))->unref();
1607 resultTexture->unref();
1608 }
reed@google.com76dd2772012-01-05 21:15:07 +00001609 return true;
1610}
1611
reed@google.comac10a2d2010-12-22 21:39:39 +00001612///////////////////////////////////////////////////////////////////////////////
1613
1614// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001615static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1616 kTriangles_PrimitiveType,
1617 kTriangleStrip_PrimitiveType,
1618 kTriangleFan_PrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001619};
1620
1621void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1622 int vertexCount, const SkPoint vertices[],
1623 const SkPoint texs[], const SkColor colors[],
1624 SkXfermode* xmode,
1625 const uint16_t indices[], int indexCount,
1626 const SkPaint& paint) {
1627 CHECK_SHOULD_DRAW(draw);
1628
bsalomon@google.com5782d712011-01-21 21:03:59 +00001629 GrPaint grPaint;
1630 SkAutoCachedTexture act;
1631 // we ignore the shader if texs is null.
1632 if (NULL == texs) {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001633 if (!skPaint2GrPaintNoShader(paint,
1634 false,
1635 NULL == colors,
1636 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001637 return;
1638 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001639 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001640 if (!skPaint2GrPaintShader(this,
1641 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001642 NULL == colors,
1643 &act,
1644 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001645 return;
1646 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001647 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001648
1649 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001650 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001651 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1652#if 0
1653 return
1654#endif
1655 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001656 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001657
bsalomon@google.com498776a2011-08-16 19:20:44 +00001658 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1659 if (NULL != colors) {
1660 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001661 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001662 for (int i = 0; i < vertexCount; ++i) {
1663 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1664 }
1665 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001666 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001667 fContext->drawVertices(grPaint,
1668 gVertexMode2PrimitiveType[vmode],
1669 vertexCount,
1670 (GrPoint*) vertices,
1671 (GrPoint*) texs,
1672 colors,
1673 indices,
1674 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001675}
1676
1677///////////////////////////////////////////////////////////////////////////////
1678
1679static void GlyphCacheAuxProc(void* data) {
1680 delete (GrFontScaler*)data;
1681}
1682
1683static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1684 void* auxData;
1685 GrFontScaler* scaler = NULL;
1686 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1687 scaler = (GrFontScaler*)auxData;
1688 }
1689 if (NULL == scaler) {
1690 scaler = new SkGrFontScaler(cache);
1691 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1692 }
1693 return scaler;
1694}
1695
1696static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1697 SkFixed fx, SkFixed fy,
1698 const SkGlyph& glyph) {
1699 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1700
bungeman@google.com15865a72012-01-11 16:28:04 +00001701 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001702
1703 if (NULL == procs->fFontScaler) {
1704 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1705 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001706
bungeman@google.com15865a72012-01-11 16:28:04 +00001707 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1708 glyph.getSubXFixed(),
1709 glyph.getSubYFixed()),
1710 SkFixedFloorToFixed(fx),
1711 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001712 procs->fFontScaler);
1713}
1714
bsalomon@google.com5782d712011-01-21 21:03:59 +00001715SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001716
1717 // deferred allocation
1718 if (NULL == fDrawProcs) {
1719 fDrawProcs = new GrSkDrawProcs;
1720 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1721 fDrawProcs->fContext = fContext;
1722 }
1723
1724 // init our (and GL's) state
1725 fDrawProcs->fTextContext = context;
1726 fDrawProcs->fFontScaler = NULL;
1727 return fDrawProcs;
1728}
1729
1730void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1731 size_t byteLength, SkScalar x, SkScalar y,
1732 const SkPaint& paint) {
1733 CHECK_SHOULD_DRAW(draw);
1734
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001735 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001736 // this guy will just call our drawPath()
1737 draw.drawText((const char*)text, byteLength, x, y, paint);
1738 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001739 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001740
1741 GrPaint grPaint;
1742 SkAutoCachedTexture act;
1743
bsalomon@google.com84405e02012-03-05 19:57:21 +00001744 if (!skPaint2GrPaintShader(this,
1745 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001746 true,
1747 &act,
1748 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001749 return;
1750 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001751 GrTextContext::AutoFinish txtCtxAF(this->getTextContext(), fContext,
1752 grPaint, draw.fExtMatrix);
1753 myDraw.fProcs = this->initDrawForText(txtCtxAF.getTextContext());
reed@google.comac10a2d2010-12-22 21:39:39 +00001754 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1755 }
1756}
1757
1758void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1759 size_t byteLength, const SkScalar pos[],
1760 SkScalar constY, int scalarsPerPos,
1761 const SkPaint& paint) {
1762 CHECK_SHOULD_DRAW(draw);
1763
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001764 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001765 // this guy will just call our drawPath()
1766 draw.drawPosText((const char*)text, byteLength, pos, constY,
1767 scalarsPerPos, paint);
1768 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001769 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001770
1771 GrPaint grPaint;
1772 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001773 if (!skPaint2GrPaintShader(this,
1774 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001775 true,
1776 &act,
1777 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001778 return;
1779 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001780 GrTextContext::AutoFinish txtCtxAF(this->getTextContext(), fContext,
1781 grPaint, draw.fExtMatrix);
1782 myDraw.fProcs = this->initDrawForText(txtCtxAF.getTextContext());
reed@google.comac10a2d2010-12-22 21:39:39 +00001783 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1784 scalarsPerPos, paint);
1785 }
1786}
1787
1788void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1789 size_t len, const SkPath& path,
1790 const SkMatrix* m, const SkPaint& paint) {
1791 CHECK_SHOULD_DRAW(draw);
1792
1793 SkASSERT(draw.fDevice == this);
1794 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1795}
1796
1797///////////////////////////////////////////////////////////////////////////////
1798
reed@google.comf67e4cf2011-03-15 20:56:58 +00001799bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1800 if (!paint.isLCDRenderText()) {
1801 // we're cool with the paint as is
1802 return false;
1803 }
1804
1805 if (paint.getShader() ||
1806 paint.getXfermode() || // unless its srcover
1807 paint.getMaskFilter() ||
1808 paint.getRasterizer() ||
1809 paint.getColorFilter() ||
1810 paint.getPathEffect() ||
1811 paint.isFakeBoldText() ||
1812 paint.getStyle() != SkPaint::kFill_Style) {
1813 // turn off lcd
1814 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1815 flags->fHinting = paint.getHinting();
1816 return true;
1817 }
1818 // we're cool with the paint as is
1819 return false;
1820}
1821
reed@google.com75d939b2011-12-07 15:07:23 +00001822void SkGpuDevice::flush() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001823 DO_DEFERRED_CLEAR;
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001824 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001825}
1826
reed@google.comf67e4cf2011-03-15 20:56:58 +00001827///////////////////////////////////////////////////////////////////////////////
1828
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001829SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(
1830 const SkBitmap& bitmap,
1831 const GrSamplerState* sampler) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001832 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001833 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001834
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001835 if (!bitmap.isVolatile()) {
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001836 GrTexture::TextureKey key = bitmap.getGenerationID();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001837 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001838
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001839 GrTextureDesc desc = {
1840 kNone_GrTextureFlags,
1841 bitmap.width(),
1842 bitmap.height(),
1843 SkGr::BitmapConfig2PixelConfig(bitmap.config()),
1844 0 // samples
1845 };
1846
1847 entry = ctx->findAndLockTexture(key, desc, sampler);
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001848 if (NULL == entry.texture()) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001849 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
1850 bitmap);
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001851 }
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001852 } else {
1853 entry = sk_gr_create_bitmap_texture(ctx, gUNCACHED_KEY,
1854 sampler, bitmap);
1855 }
1856 if (NULL == entry.texture()) {
1857 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1858 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001859 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001860 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001861}
1862
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001863void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1864 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001865}
1866
bsalomon@google.comfb309512011-11-30 14:13:48 +00001867bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
1868 const GrSamplerState& sampler) const {
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001869 GrTexture::TextureKey key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001870 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001871
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001872 GrTextureDesc desc = {
1873 kNone_GrTextureFlags,
1874 bitmap.width(),
1875 bitmap.height(),
1876 SkGr::BitmapConfig2PixelConfig(bitmap.config()),
1877 0 // samples
1878 };
1879
1880 return this->context()->isTextureInCache(key, desc, &sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001881}
1882
1883
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001884SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1885 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001886 bool isOpaque,
1887 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001888 GrTextureDesc desc;
1889 desc.fConfig = fRenderTarget->config();
1890 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1891 desc.fWidth = width;
1892 desc.fHeight = height;
1893 desc.fSampleCnt = fRenderTarget->numSamples();
1894
1895 GrContext::TextureCacheEntry cacheEntry;
1896 GrTexture* texture;
1897 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001898 // Skia's convention is to only clear a device if it is non-opaque.
1899 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001900
1901#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1902 // layers are never draw in repeat modes, so we can request an approx
1903 // match and ignore any padding.
1904 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1905 GrContext::kApprox_ScratchTexMatch :
1906 GrContext::kExact_ScratchTexMatch;
1907 cacheEntry = fContext->lockScratchTexture(desc, matchType);
1908 texture = cacheEntry.texture();
1909#else
1910 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1911 texture = tunref.get();
1912#endif
1913 if (texture) {
1914 return SkNEW_ARGS(SkGpuDevice,(fContext,
1915 texture,
1916 cacheEntry,
1917 needClear));
1918 } else {
1919 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1920 width, height);
1921 return NULL;
1922 }
1923}
1924
1925SkGpuDevice::SkGpuDevice(GrContext* context,
1926 GrTexture* texture,
1927 TexCache cacheEntry,
1928 bool needClear)
1929 : SkDevice(make_bitmap(context, texture->asRenderTarget())) {
1930 GrAssert(texture && texture->asRenderTarget());
1931 GrAssert(NULL == cacheEntry.texture() || texture == cacheEntry.texture());
1932 this->initFromRenderTarget(context, texture->asRenderTarget());
1933 fCache = cacheEntry;
1934 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001935}
1936
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001937GrTextContext* SkGpuDevice::getTextContext() {
1938 if (NULL == fTextContext) {
1939 fTextContext = new GrDefaultTextContext();
1940 }
1941 return fTextContext;
1942}