blob: 3ce70d15f486e873371165470028eb58963d38f5 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
tomhudson@google.com898e7b52012-06-01 20:42:15 +00009#include "SkGpuDevice.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000010
tomhudson@google.com898e7b52012-06-01 20:42:15 +000011#include "effects/GrGradientEffects.h"
epoger@google.comec3ed6a2011-07-28 14:26:00 +000012
reed@google.comac10a2d2010-12-22 21:39:39 +000013#include "GrContext.h"
bsalomon@google.comf4a9c822012-03-16 14:02:46 +000014#include "GrDefaultTextContext.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000015#include "GrTextContext.h"
16
reed@google.comac10a2d2010-12-22 21:39:39 +000017#include "SkGrTexturePixelRef.h"
18
Scroggo97c88c22011-05-11 14:05:25 +000019#include "SkColorFilter.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000020#include "SkDrawProcs.h"
21#include "SkGlyphCache.h"
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +000022#include "SkImageFilter.h"
reed@google.comfe626382011-09-21 13:50:35 +000023#include "SkTLazy.h"
reed@google.comc9aa5872011-04-05 21:05:37 +000024#include "SkUtils.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000025
bsalomon@google.com06cd7322012-03-30 18:45:35 +000026#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
reed@google.comac10a2d2010-12-22 21:39:39 +000027
28#if 0
29 extern bool (*gShouldDrawProc)();
30 #define CHECK_SHOULD_DRAW(draw) \
31 do { \
32 if (gShouldDrawProc && !gShouldDrawProc()) return; \
33 this->prepareRenderTarget(draw); \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000034 GrAssert(!fNeedClear) \
reed@google.comac10a2d2010-12-22 21:39:39 +000035 } while (0)
36#else
bsalomon@google.com06cd7322012-03-30 18:45:35 +000037 #define CHECK_SHOULD_DRAW(draw) this->prepareRenderTarget(draw); \
38 GrAssert(!fNeedClear)
reed@google.comac10a2d2010-12-22 21:39:39 +000039#endif
40
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000041// we use the same texture slot on GrPaint for bitmaps and shaders
42// (since drawBitmap, drawSprite, and drawDevice ignore skia's shader)
43enum {
44 kBitmapTextureIdx = 0,
45 kShaderTextureIdx = 0
46};
47
reed@google.comcde92112011-07-06 20:00:52 +000048
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +000049#define MAX_BLUR_SIGMA 4.0f
50// FIXME: This value comes from from SkBlurMaskFilter.cpp.
51// Should probably be put in a common header someplace.
52#define MAX_BLUR_RADIUS SkIntToScalar(128)
53// This constant approximates the scaling done in the software path's
54// "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
55// IMHO, it actually should be 1: we blur "less" than we should do
56// according to the CSS and canvas specs, simply because Safari does the same.
57// Firefox used to do the same too, until 4.0 where they fixed it. So at some
58// point we should probably get rid of these scaling constants and rebaseline
59// all the blur tests.
60#define BLUR_SIGMA_SCALE 0.6f
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000061// This constant represents the screen alignment criterion in texels for
62// requiring texture domain clamping to prevent color bleeding when drawing
63// a sub region of a larger source image.
64#define COLOR_BLEED_TOLERANCE SkFloatToScalar(0.001f)
bsalomon@google.com06cd7322012-03-30 18:45:35 +000065
66#define DO_DEFERRED_CLEAR \
67 do { \
68 if (fNeedClear) { \
bsalomon@google.com730ca3b2012-04-03 13:25:12 +000069 this->clear(0x0); \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000070 fNeedClear = false; \
71 } \
72 } while (false) \
73
reed@google.comac10a2d2010-12-22 21:39:39 +000074///////////////////////////////////////////////////////////////////////////////
75
bsalomon@google.com84405e02012-03-05 19:57:21 +000076class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
77public:
78 SkAutoCachedTexture() { }
79 SkAutoCachedTexture(SkGpuDevice* device,
80 const SkBitmap& bitmap,
81 const GrSamplerState* sampler,
82 GrTexture** texture) {
83 GrAssert(texture);
84 *texture = this->set(device, bitmap, sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +000085 }
reed@google.comac10a2d2010-12-22 21:39:39 +000086
bsalomon@google.com84405e02012-03-05 19:57:21 +000087 ~SkAutoCachedTexture() {
88 if (fTex.texture()) {
89 fDevice->unlockCachedTexture(fTex);
90 }
reed@google.comac10a2d2010-12-22 21:39:39 +000091 }
bsalomon@google.com84405e02012-03-05 19:57:21 +000092
93 GrTexture* set(SkGpuDevice* device,
94 const SkBitmap& bitmap,
95 const GrSamplerState* sampler) {
96 if (fTex.texture()) {
97 fDevice->unlockCachedTexture(fTex);
98 }
99 fDevice = device;
100 GrTexture* texture = (GrTexture*)bitmap.getTexture();
101 if (texture) {
102 // return the native texture
103 fTex.reset();
104 } else {
105 // look it up in our cache
106 fTex = device->lockCachedTexture(bitmap, sampler);
107 texture = fTex.texture();
108 }
109 return texture;
110 }
111
112private:
113 SkGpuDevice* fDevice;
114 GrContext::TextureCacheEntry fTex;
115};
reed@google.comac10a2d2010-12-22 21:39:39 +0000116
117///////////////////////////////////////////////////////////////////////////////
118
119bool gDoTraceDraw;
120
121struct GrSkDrawProcs : public SkDrawProcs {
122public:
123 GrContext* fContext;
124 GrTextContext* fTextContext;
125 GrFontScaler* fFontScaler; // cached in the skia glyphcache
126};
127
128///////////////////////////////////////////////////////////////////////////////
129
reed@google.comaf951c92011-06-16 19:10:39 +0000130static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
131 switch (config) {
132 case kAlpha_8_GrPixelConfig:
133 *isOpaque = false;
134 return SkBitmap::kA8_Config;
135 case kRGB_565_GrPixelConfig:
136 *isOpaque = true;
137 return SkBitmap::kRGB_565_Config;
138 case kRGBA_4444_GrPixelConfig:
139 *isOpaque = false;
140 return SkBitmap::kARGB_4444_Config;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000141 case kSkia8888_PM_GrPixelConfig:
142 // we don't currently have a way of knowing whether
143 // a 8888 is opaque based on the config.
144 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000145 return SkBitmap::kARGB_8888_Config;
146 default:
147 *isOpaque = false;
148 return SkBitmap::kNo_Config;
149 }
150}
reed@google.comac10a2d2010-12-22 21:39:39 +0000151
reed@google.comaf951c92011-06-16 19:10:39 +0000152static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000153 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000154
155 bool isOpaque;
156 SkBitmap bitmap;
157 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
158 renderTarget->width(), renderTarget->height());
159 bitmap.setIsOpaque(isOpaque);
160 return bitmap;
161}
162
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000163SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
164: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
165 this->initFromRenderTarget(context, texture->asRenderTarget());
166}
167
reed@google.comaf951c92011-06-16 19:10:39 +0000168SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
169: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000170 this->initFromRenderTarget(context, renderTarget);
171}
172
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000173void SkGpuDevice::initFromRenderTarget(GrContext* context,
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000174 GrRenderTarget* renderTarget) {
reed@google.comaf951c92011-06-16 19:10:39 +0000175 fNeedPrepareRenderTarget = false;
176 fDrawProcs = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000177
reed@google.comaf951c92011-06-16 19:10:39 +0000178 fContext = context;
179 fContext->ref();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000180
reed@google.comaf951c92011-06-16 19:10:39 +0000181 fTexture = NULL;
182 fRenderTarget = NULL;
183 fNeedClear = false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000184
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000185 GrAssert(NULL != renderTarget);
186 fRenderTarget = renderTarget;
187 fRenderTarget->ref();
188 // if this RT is also a texture, hold a ref on it
189 fTexture = fRenderTarget->asTexture();
190 SkSafeRef(fTexture);
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000191
192 // Create a pixel ref for the underlying SkBitmap. We prefer a texture pixel
193 // ref to a render target pixel reft. The pixel ref may get ref'ed outside
194 // the device via accessBitmap. This external ref may outlive the device.
195 // Since textures own their render targets (but not vice-versa) we
196 // are ensuring that both objects will live as long as the pixel ref.
197 SkPixelRef* pr;
198 if (fTexture) {
199 pr = new SkGrTexturePixelRef(fTexture);
200 } else {
201 pr = new SkGrRenderTargetPixelRef(fRenderTarget);
202 }
reed@google.comaf951c92011-06-16 19:10:39 +0000203 this->setPixelRef(pr, 0)->unref();
bsalomon@google.comf4a9c822012-03-16 14:02:46 +0000204
205 fTextContext = NULL;
reed@google.comaf951c92011-06-16 19:10:39 +0000206}
207
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000208SkGpuDevice::SkGpuDevice(GrContext* context,
209 SkBitmap::Config config,
210 int width,
211 int height)
212 : SkDevice(config, width, height, false /*isOpaque*/) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000213 fNeedPrepareRenderTarget = false;
214 fDrawProcs = NULL;
215
reed@google.com7b201d22011-01-11 18:59:23 +0000216 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000217 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000218
reed@google.comac10a2d2010-12-22 21:39:39 +0000219 fTexture = NULL;
220 fRenderTarget = NULL;
221 fNeedClear = false;
222
reed@google.comaf951c92011-06-16 19:10:39 +0000223 if (config != SkBitmap::kRGB_565_Config) {
224 config = SkBitmap::kARGB_8888_Config;
225 }
226 SkBitmap bm;
227 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000228
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000229 GrTextureDesc desc;
230 desc.fFlags = kRenderTarget_GrTextureFlagBit;
231 desc.fWidth = width;
232 desc.fHeight = height;
233 desc.fConfig = SkGr::BitmapConfig2PixelConfig(bm.config());
reed@google.comac10a2d2010-12-22 21:39:39 +0000234
reed@google.comaf951c92011-06-16 19:10:39 +0000235 fTexture = fContext->createUncachedTexture(desc, NULL, 0);
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000236
reed@google.comaf951c92011-06-16 19:10:39 +0000237 if (NULL != fTexture) {
238 fRenderTarget = fTexture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000239 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000240
reed@google.comaf951c92011-06-16 19:10:39 +0000241 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000242
reed@google.comaf951c92011-06-16 19:10:39 +0000243 // wrap the bitmap with a pixelref to expose our texture
244 SkGrTexturePixelRef* pr = new SkGrTexturePixelRef(fTexture);
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000245 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000246 } else {
247 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
248 width, height);
249 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000250 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +0000251
252 fTextContext = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000253}
254
255SkGpuDevice::~SkGpuDevice() {
256 if (fDrawProcs) {
257 delete fDrawProcs;
258 }
259
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000260 SkSafeUnref(fTexture);
261 SkSafeUnref(fRenderTarget);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000262 if (fCache.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000263 GrAssert(NULL != fTexture);
264 GrAssert(fRenderTarget == fTexture->asRenderTarget());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000265 fContext->unlockTexture(fCache);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000266 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000267 fContext->unref();
bsalomon@google.comf4a9c822012-03-16 14:02:46 +0000268
269 if (NULL != fTextContext) {
270 fTextContext->unref();
271 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000272}
273
reed@google.comac10a2d2010-12-22 21:39:39 +0000274///////////////////////////////////////////////////////////////////////////////
275
276void SkGpuDevice::makeRenderTargetCurrent() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000277 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000278 fContext->setRenderTarget(fRenderTarget);
279 fContext->flush(true);
280 fNeedPrepareRenderTarget = true;
281}
282
283///////////////////////////////////////////////////////////////////////////////
284
bsalomon@google.comc4364992011-11-07 15:54:49 +0000285namespace {
286GrPixelConfig config8888_to_gr_config(SkCanvas::Config8888 config8888) {
287 switch (config8888) {
288 case SkCanvas::kNative_Premul_Config8888:
289 return kSkia8888_PM_GrPixelConfig;
290 case SkCanvas::kNative_Unpremul_Config8888:
291 return kSkia8888_UPM_GrPixelConfig;
292 case SkCanvas::kBGRA_Premul_Config8888:
293 return kBGRA_8888_PM_GrPixelConfig;
294 case SkCanvas::kBGRA_Unpremul_Config8888:
295 return kBGRA_8888_UPM_GrPixelConfig;
296 case SkCanvas::kRGBA_Premul_Config8888:
297 return kRGBA_8888_PM_GrPixelConfig;
298 case SkCanvas::kRGBA_Unpremul_Config8888:
299 return kRGBA_8888_UPM_GrPixelConfig;
300 default:
301 GrCrash("Unexpected Config8888.");
302 return kSkia8888_PM_GrPixelConfig;
303 }
304}
305}
306
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000307bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
308 int x, int y,
309 SkCanvas::Config8888 config8888) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000310 DO_DEFERRED_CLEAR;
bsalomon@google.com910267d2011-11-02 20:06:25 +0000311 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
312 SkASSERT(!bitmap.isNull());
313 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000314
bsalomon@google.com910267d2011-11-02 20:06:25 +0000315 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000316 GrPixelConfig config;
317 config = config8888_to_gr_config(config8888);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000318 return fContext->readRenderTargetPixels(fRenderTarget,
319 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000320 bitmap.width(),
321 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000322 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000323 bitmap.getPixels(),
324 bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000325}
326
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000327void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
328 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000329 SkAutoLockPixels alp(bitmap);
330 if (!bitmap.readyToDraw()) {
331 return;
332 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000333
334 GrPixelConfig config;
335 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
336 config = config8888_to_gr_config(config8888);
337 } else {
robertphillips@google.coma1e57952012-06-04 20:05:28 +0000338 config= SkGr::BitmapConfig2PixelConfig(bitmap.config());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000339 }
340
bsalomon@google.com6f379512011-11-16 20:36:03 +0000341 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
342 config, bitmap.getPixels(), bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000343}
344
345///////////////////////////////////////////////////////////////////////////////
346
347static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000348 const SkClipStack& clipStack,
reed@google.com6f8f2922011-03-04 22:27:10 +0000349 const SkRegion& clipRegion,
350 const SkIPoint& origin) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000351 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000352
353 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000354 iter.reset(clipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000355 const SkIRect& skBounds = clipRegion.getBounds();
356 GrRect bounds;
357 bounds.setLTRB(GrIntToScalar(skBounds.fLeft),
358 GrIntToScalar(skBounds.fTop),
359 GrIntToScalar(skBounds.fRight),
360 GrIntToScalar(skBounds.fBottom));
reed@google.com6f8f2922011-03-04 22:27:10 +0000361 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
362 &bounds);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000363 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000364}
365
366// call this ever each draw call, to ensure that the context reflects our state,
367// and not the state from some other canvas/device
368void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
369 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000370 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000371
372 fContext->setRenderTarget(fRenderTarget);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000373 SkASSERT(draw.fClipStack);
374 convert_matrixclip(fContext, *draw.fMatrix,
reed@google.com6f8f2922011-03-04 22:27:10 +0000375 *draw.fClipStack, *draw.fClip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000376 fNeedPrepareRenderTarget = false;
377 }
378}
379
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000380void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
381 const SkClipStack& clipStack) {
382 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
383 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000384 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000385}
386
387void SkGpuDevice::gainFocus(SkCanvas* canvas, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000388 const SkRegion& clip, const SkClipStack& clipStack) {
389
reed@google.comac10a2d2010-12-22 21:39:39 +0000390 fContext->setRenderTarget(fRenderTarget);
391
bsalomon@google.comd302f142011-03-03 13:54:13 +0000392 this->INHERITED::gainFocus(canvas, matrix, clip, clipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000393
reed@google.com6f8f2922011-03-04 22:27:10 +0000394 convert_matrixclip(fContext, matrix, clipStack, clip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000395
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000396 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000397}
398
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000399SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000400 DO_DEFERRED_CLEAR;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000401 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000402}
403
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000404bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000405 if (NULL != fTexture) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000406 paint->setTexture(kBitmapTextureIdx, fTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000407 return true;
408 }
409 return false;
410}
411
412///////////////////////////////////////////////////////////////////////////////
413
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000414SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
415SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
416SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
417SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
418SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
419 shader_type_mismatch);
420SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 4, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000421
bsalomon@google.com84405e02012-03-05 19:57:21 +0000422namespace {
423
424// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
425// justAlpha indicates that skPaint's alpha should be used rather than the color
426// Callers may subsequently modify the GrPaint. Setting constantColor indicates
427// that the final paint will draw the same color at every pixel. This allows
428// an optimization where the the color filter can be applied to the skPaint's
429// color once while converting to GrPain and then ignored.
430inline bool skPaint2GrPaintNoShader(const SkPaint& skPaint,
431 bool justAlpha,
432 bool constantColor,
433 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000434
435 grPaint->fDither = skPaint.isDither();
436 grPaint->fAntiAlias = skPaint.isAntiAlias();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000437 grPaint->fCoverage = 0xFF;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000438
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000439 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
440 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000441
442 SkXfermode* mode = skPaint.getXfermode();
443 if (mode) {
444 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000445 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000446#if 0
447 return false;
448#endif
449 }
450 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000451 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
452 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
453
bsalomon@google.com5782d712011-01-21 21:03:59 +0000454 if (justAlpha) {
455 uint8_t alpha = skPaint.getAlpha();
456 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000457 // justAlpha is currently set to true only if there is a texture,
458 // so constantColor should not also be true.
459 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000460 } else {
461 grPaint->fColor = SkGr::SkColor2GrColor(skPaint.getColor());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000462 grPaint->setTexture(kShaderTextureIdx, NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000463 }
Scroggo97c88c22011-05-11 14:05:25 +0000464 SkColorFilter* colorFilter = skPaint.getColorFilter();
465 SkColor color;
466 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000467 SkScalar matrix[20];
Scroggo97c88c22011-05-11 14:05:25 +0000468 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000469 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000470 if (!constantColor) {
471 grPaint->fColorFilterColor = SkGr::SkColor2GrColor(color);
472 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000473 } else {
474 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
475 grPaint->fColor = SkGr::SkColor2GrColor(filtered);
senorblanco@chromium.orgb3c20fa2012-01-03 21:20:19 +0000476 grPaint->resetColorFilter();
Scroggod757df22011-05-16 13:11:16 +0000477 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000478 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
479 grPaint->fColorMatrixEnabled = true;
480 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
481 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
482 } else {
483 grPaint->resetColorFilter();
Scroggo97c88c22011-05-11 14:05:25 +0000484 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000485 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000486}
487
bsalomon@google.com84405e02012-03-05 19:57:21 +0000488// This function is similar to skPaint2GrPaintNoShader but also converts
489// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
490// be used is set on grPaint and returned in param act. constantColor has the
491// same meaning as in skPaint2GrPaintNoShader.
492inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
493 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000494 bool constantColor,
495 SkGpuDevice::SkAutoCachedTexture* act,
496 GrPaint* grPaint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000497
bsalomon@google.com5782d712011-01-21 21:03:59 +0000498 SkASSERT(NULL != act);
reed@google.comac10a2d2010-12-22 21:39:39 +0000499
bsalomon@google.com5782d712011-01-21 21:03:59 +0000500 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000501 if (NULL == shader) {
bsalomon@google.com84405e02012-03-05 19:57:21 +0000502 return skPaint2GrPaintNoShader(skPaint,
503 false,
504 constantColor,
505 grPaint);
506 } else if (!skPaint2GrPaintNoShader(skPaint, true, false, grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000507 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000508 }
509
reed@google.comac10a2d2010-12-22 21:39:39 +0000510 SkBitmap bitmap;
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000511 SkMatrix* matrix = grPaint->textureSampler(kShaderTextureIdx)->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000512 SkShader::TileMode tileModes[2];
513 SkScalar twoPointParams[3];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000514 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000515 tileModes, twoPointParams);
516
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000517 if (SkShader::kNone_BitmapType == bmptype) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000518 SkShader::GradientInfo info;
519 SkColor color;
520
521 info.fColors = &color;
522 info.fColorOffsets = NULL;
523 info.fColorCount = 1;
524 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
525 SkPaint copy(skPaint);
526 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000527 // modulate the paint alpha by the shader's solid color alpha
528 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
529 copy.setColor(SkColorSetA(color, newA));
bsalomon@google.com84405e02012-03-05 19:57:21 +0000530 return skPaint2GrPaintNoShader(copy,
531 false,
532 constantColor,
533 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000534 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000535 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000536 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000537 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000538 switch (bmptype) {
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000539 case SkShader::kRadial_BitmapType:
540 sampler->setCustomStage(new GrRadialGradient())->unref();
541 sampler->setFilter(GrSamplerState::kBilinear_Filter);
542 break;
543 case SkShader::kSweep_BitmapType:
544 sampler->setCustomStage(new GrSweepGradient())->unref();
545 sampler->setFilter(GrSamplerState::kBilinear_Filter);
546 break;
547 case SkShader::kTwoPointRadial_BitmapType:
548 sampler->setCustomStage(new
549 GrRadial2Gradient(twoPointParams[0],
550 twoPointParams[1],
551 twoPointParams[2] < 0))->unref();
552 sampler->setFilter(GrSamplerState::kBilinear_Filter);
553 break;
554 default:
555 if (skPaint.isFilterBitmap()) {
556 sampler->setFilter(GrSamplerState::kBilinear_Filter);
557 } else {
558 sampler->setFilter(GrSamplerState::kNearest_Filter);
559 }
560 break;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000561 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000562 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
563 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000564
bsalomon@google.com84405e02012-03-05 19:57:21 +0000565 GrTexture* texture = act->set(dev, bitmap, sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +0000566 if (NULL == texture) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000567 SkDebugf("Couldn't convert bitmap to texture.\n");
568 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000569 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000570 grPaint->setTexture(kShaderTextureIdx, texture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000571
572 // since our texture coords will be in local space, we wack the texture
573 // matrix to map them back into 0...1 before we load it
574 SkMatrix localM;
575 if (shader->getLocalMatrix(&localM)) {
576 SkMatrix inverse;
577 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000578 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000579 }
580 }
581 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000582 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
583 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000584 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000585 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000586 GrScalar s = SkFloatToScalar(1.f / bitmap.width());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000587 matrix->postScale(s, s);
reed@google.comac10a2d2010-12-22 21:39:39 +0000588 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000589
590 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000591}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000592}
reed@google.comac10a2d2010-12-22 21:39:39 +0000593
594///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000595void SkGpuDevice::clear(SkColor color) {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000596 fContext->clear(NULL, color, fRenderTarget);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000597}
598
reed@google.comac10a2d2010-12-22 21:39:39 +0000599void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
600 CHECK_SHOULD_DRAW(draw);
601
bsalomon@google.com5782d712011-01-21 21:03:59 +0000602 GrPaint grPaint;
603 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000604 if (!skPaint2GrPaintShader(this,
605 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000606 true,
607 &act,
608 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000609 return;
610 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000611
612 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000613}
614
615// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000616static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000617 kPoints_GrPrimitiveType,
618 kLines_GrPrimitiveType,
619 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000620};
621
622void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000623 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000624 CHECK_SHOULD_DRAW(draw);
625
626 SkScalar width = paint.getStrokeWidth();
627 if (width < 0) {
628 return;
629 }
630
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000631 // we only handle hairlines and paints without path effects or mask filters,
632 // else we let the SkDraw call our drawPath()
633 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000634 draw.drawPoints(mode, count, pts, paint, true);
635 return;
636 }
637
bsalomon@google.com5782d712011-01-21 21:03:59 +0000638 GrPaint grPaint;
639 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000640 if (!skPaint2GrPaintShader(this,
641 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000642 true,
643 &act,
644 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000645 return;
646 }
647
bsalomon@google.com5782d712011-01-21 21:03:59 +0000648 fContext->drawVertices(grPaint,
649 gPointMode2PrimtiveType[mode],
650 count,
651 (GrPoint*)pts,
652 NULL,
653 NULL,
654 NULL,
655 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000656}
657
reed@google.comc9aa5872011-04-05 21:05:37 +0000658///////////////////////////////////////////////////////////////////////////////
659
reed@google.comac10a2d2010-12-22 21:39:39 +0000660void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
661 const SkPaint& paint) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000662 CHECK_SHOULD_DRAW(draw);
663
bungeman@google.com79bd8772011-07-18 15:34:08 +0000664 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000665 SkScalar width = paint.getStrokeWidth();
666
667 /*
668 We have special code for hairline strokes, miter-strokes, and fills.
669 Anything else we just call our path code.
670 */
671 bool usePath = doStroke && width > 0 &&
672 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000673 // another two reasons we might need to call drawPath...
674 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000675 usePath = true;
676 }
reed@google.com67db6642011-05-26 11:46:35 +0000677 // until we aa rotated rects...
678 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
679 usePath = true;
680 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000681 // small miter limit means right angles show bevel...
682 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
683 paint.getStrokeMiter() < SK_ScalarSqrt2)
684 {
685 usePath = true;
686 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000687 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000688 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
689 usePath = true;
690 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000691
692 if (usePath) {
693 SkPath path;
694 path.addRect(rect);
695 this->drawPath(draw, path, paint, NULL, true);
696 return;
697 }
698
699 GrPaint grPaint;
700 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000701 if (!skPaint2GrPaintShader(this,
702 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000703 true,
704 &act,
705 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000706 return;
707 }
reed@google.com20efde72011-05-09 17:00:02 +0000708 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000709}
710
reed@google.com69302852011-02-16 18:08:07 +0000711#include "SkMaskFilter.h"
712#include "SkBounder.h"
713
bsalomon@google.com85003222012-03-28 14:44:37 +0000714///////////////////////////////////////////////////////////////////////////////
715
716// helpers for applying mask filters
717namespace {
718
719GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000720 switch (fillType) {
721 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000722 return kWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000723 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000724 return kEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000725 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000726 return kInverseWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000727 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000728 return kInverseEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000729 default:
730 SkDebugf("Unsupported path fill type\n");
bsalomon@google.com47059542012-06-06 20:51:20 +0000731 return kHairLine_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000732 }
733}
734
bsalomon@google.com85003222012-03-28 14:44:37 +0000735// We prefer to blur small rect with small radius via CPU.
736#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
737#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
738inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
739 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
740 rect.height() <= MIN_GPU_BLUR_SIZE &&
741 radius <= MIN_GPU_BLUR_RADIUS) {
742 return true;
743 }
744 return false;
745}
746
747bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
748 SkMaskFilter* filter, const SkMatrix& matrix,
749 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000750 GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000751#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000752 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000753#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000754 SkMaskFilter::BlurInfo info;
755 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000756 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000757 return false;
758 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000759 SkScalar radius = info.fIgnoreTransform ? info.fRadius
760 : matrix.mapRadius(info.fRadius);
761 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000762 if (radius <= 0) {
763 return false;
764 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000765
766 SkRect srcRect = path.getBounds();
767 if (shouldDrawBlurWithCPU(srcRect, radius)) {
768 return false;
769 }
770
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000771 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000772 float sigma3 = sigma * 3.0f;
773
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000774 SkRect clipRect;
775 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000776
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000777 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000778 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
779 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000780 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000781 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000782 SkIRect finalIRect;
783 finalRect.roundOut(&finalIRect);
784 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000785 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000786 }
787 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000788 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000789 }
790 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000791 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000792 GrTextureDesc desc;
793 desc.fFlags = kRenderTarget_GrTextureFlagBit;
794 desc.fWidth = SkScalarCeilToInt(srcRect.width());
795 desc.fHeight = SkScalarCeilToInt(srcRect.height());
796 // We actually only need A8, but it often isn't supported as a
797 // render target so default to RGBA_8888
798 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000799
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000800 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
801 desc.fConfig = kAlpha_8_GrPixelConfig;
802 }
803
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000804 GrAutoScratchTexture pathEntry(context, desc);
805 GrTexture* pathTexture = pathEntry.texture();
806 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000807 return false;
808 }
809 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000810 // Once this code moves into GrContext, this should be changed to use
811 // an AutoClipRestore.
812 GrClip oldClip = context->getClip();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000813 context->setRenderTarget(pathTexture->asRenderTarget());
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000814 context->setClip(srcRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000815 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000816 GrPaint tempPaint;
817 tempPaint.reset();
818
819 GrAutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000820 tempPaint.fAntiAlias = grp->fAntiAlias;
821 if (tempPaint.fAntiAlias) {
822 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
823 // blend coeff of zero requires dual source blending support in order
824 // to properly blend partially covered pixels. This means the AA
825 // code path may not be taken. So we use a dst blend coeff of ISA. We
826 // could special case AA draws to a dst surface with known alpha=0 to
827 // use a zero dst coeff when dual source blending isn't available.
bsalomon@google.com47059542012-06-06 20:51:20 +0000828 tempPaint.fSrcBlendCoeff = kOne_GrBlendCoeff;
829 tempPaint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000830 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000831 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000832 context->drawPath(tempPaint, path, pathFillType, &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000833
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000834 GrAutoScratchTexture temp1, temp2;
835 // If we're doing a normal blur, we can clobber the pathTexture in the
836 // gaussianBlur. Otherwise, we need to save it for later compositing.
837 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org3b4dd902012-03-05 20:41:22 +0000838 GrTexture* blurTexture = context->gaussianBlur(pathTexture,
839 &temp1,
840 isNormalBlur ? NULL : &temp2,
841 srcRect, sigma, sigma);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000842
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000843 if (!isNormalBlur) {
844 GrPaint paint;
845 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000846 paint.textureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000847 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
848 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000849 // Blend pathTexture over blurTexture.
850 context->setRenderTarget(blurTexture->asRenderTarget());
851 paint.setTexture(0, pathTexture);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000852 if (SkMaskFilter::kInner_BlurType == blurType) {
853 // inner: dst = dst * src
bsalomon@google.com47059542012-06-06 20:51:20 +0000854 paint.fSrcBlendCoeff = kDC_GrBlendCoeff;
855 paint.fDstBlendCoeff = kZero_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000856 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
857 // solid: dst = src + dst - src * dst
858 // = (1 - dst) * src + 1 * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000859 paint.fSrcBlendCoeff = kIDC_GrBlendCoeff;
860 paint.fDstBlendCoeff = kOne_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000861 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
862 // outer: dst = dst * (1 - src)
863 // = 0 * src + (1 - src) * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000864 paint.fSrcBlendCoeff = kZero_GrBlendCoeff;
865 paint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000866 }
867 context->drawRect(paint, srcRect);
868 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000869 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000870 context->setClip(oldClip);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000871
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000872 if (grp->hasTextureOrMask()) {
873 GrMatrix inverse;
874 if (!matrix.invert(&inverse)) {
875 return false;
876 }
877 grp->preConcatActiveSamplerMatrices(inverse);
878 }
879
880 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
881 // we assume the last mask index is available for use
882 GrAssert(NULL == grp->getMask(MASK_IDX));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000883 grp->setMask(MASK_IDX, blurTexture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000884 grp->maskSampler(MASK_IDX)->reset();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000885
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000886 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
887 -finalRect.fTop);
888 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
889 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000890 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000891 return true;
892}
893
bsalomon@google.com85003222012-03-28 14:44:37 +0000894bool drawWithMaskFilter(GrContext* context, const SkPath& path,
895 SkMaskFilter* filter, const SkMatrix& matrix,
896 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000897 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000898 SkMask srcM, dstM;
899
900 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000901 SkMask::kComputeBoundsAndRenderImage_CreateMode,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000902 style)) {
reed@google.com69302852011-02-16 18:08:07 +0000903 return false;
904 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000905 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000906
907 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
908 return false;
909 }
910 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000911 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000912
913 if (clip.quickReject(dstM.fBounds)) {
914 return false;
915 }
916 if (bounder && !bounder->doIRect(dstM.fBounds)) {
917 return false;
918 }
919
920 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
921 // the current clip (and identity matrix) and grpaint settings
922
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000923 // used to compute inverse view, if necessary
bsalomon@google.com7d30a212012-04-25 15:52:27 +0000924 GrMatrix ivm = matrix;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000925
reed@google.com0c219b62011-02-16 21:31:18 +0000926 GrAutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +0000927
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000928 GrTextureDesc desc;
929 desc.fWidth = dstM.fBounds.width();
930 desc.fHeight = dstM.fBounds.height();
931 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +0000932
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000933 GrAutoScratchTexture ast(context, desc);
934 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000935
reed@google.com69302852011-02-16 18:08:07 +0000936 if (NULL == texture) {
937 return false;
938 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000939 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000940 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000941
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000942 if (grp->hasTextureOrMask() && ivm.invert(&ivm)) {
943 grp->preConcatActiveSamplerMatrices(ivm);
944 }
945
946 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
947 // we assume the last mask index is available for use
948 GrAssert(NULL == grp->getMask(MASK_IDX));
949 grp->setMask(MASK_IDX, texture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000950 grp->maskSampler(MASK_IDX)->reset();
reed@google.com69302852011-02-16 18:08:07 +0000951
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000952 GrRect d;
953 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +0000954 GrIntToScalar(dstM.fBounds.fTop),
955 GrIntToScalar(dstM.fBounds.fRight),
956 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000957
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000958 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
959 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
960 -dstM.fBounds.fTop*SK_Scalar1);
961 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000962 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000963 return true;
964}
reed@google.com69302852011-02-16 18:08:07 +0000965
bsalomon@google.com85003222012-03-28 14:44:37 +0000966}
967
968///////////////////////////////////////////////////////////////////////////////
969
reed@google.com0c219b62011-02-16 21:31:18 +0000970void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000971 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000972 bool pathIsMutable) {
973 CHECK_SHOULD_DRAW(draw);
974
reed@google.comfe626382011-09-21 13:50:35 +0000975 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000976
bsalomon@google.com5782d712011-01-21 21:03:59 +0000977 GrPaint grPaint;
978 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000979 if (!skPaint2GrPaintShader(this,
980 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000981 true,
982 &act,
983 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000984 return;
985 }
986
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000987 // can we cheat, and threat a thin stroke as a hairline w/ coverage
988 // if we can, we draw lots faster (raster device does this same test)
989 SkScalar hairlineCoverage;
990 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
991 doFill = false;
992 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
993 grPaint.fCoverage);
994 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000995
reed@google.comfe626382011-09-21 13:50:35 +0000996 // If we have a prematrix, apply it to the path, optimizing for the case
997 // where the original path can in fact be modified in place (even though
998 // its parameter type is const).
999 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1000 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001001
1002 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001003 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001004
reed@google.come3445642011-02-16 23:20:39 +00001005 if (!pathIsMutable) {
1006 result = &tmpPath;
1007 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001008 }
reed@google.come3445642011-02-16 23:20:39 +00001009 // should I push prePathMatrix on our MV stack temporarily, instead
1010 // of applying it here? See SkDraw.cpp
1011 pathPtr->transform(*prePathMatrix, result);
1012 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001013 }
reed@google.com0c219b62011-02-16 21:31:18 +00001014 // at this point we're done with prePathMatrix
1015 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001016
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001017 if (paint.getPathEffect() ||
1018 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001019 // it is safe to use tmpPath here, even if we already used it for the
1020 // prepathmatrix, since getFillPath can take the same object for its
1021 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001022 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001023 pathPtr = &tmpPath;
1024 }
1025
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001026 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001027 // avoid possibly allocating a new path in transform if we can
1028 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1029
1030 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001031 pathPtr->transform(*draw.fMatrix, devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001032 GrPathFill pathFillType = doFill ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001033 skToGrFillType(devPathPtr->getFillType()) : kHairLine_GrPathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001034 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001035 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001036 &grPaint, pathFillType)) {
1037 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
1038 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001039 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001040 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001041 &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001042 }
reed@google.com69302852011-02-16 18:08:07 +00001043 return;
1044 }
reed@google.com69302852011-02-16 18:08:07 +00001045
bsalomon@google.com47059542012-06-06 20:51:20 +00001046 GrPathFill fill = kHairLine_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001047
reed@google.com0c219b62011-02-16 21:31:18 +00001048 if (doFill) {
1049 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001050 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001051 fill = kWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001052 break;
1053 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001054 fill = kEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001055 break;
1056 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001057 fill = kInverseWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001058 break;
1059 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001060 fill = kInverseEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001061 break;
1062 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001063 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001064 return;
1065 }
1066 }
1067
reed@google.com07f3ee12011-05-16 17:21:57 +00001068 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001069}
1070
bsalomon@google.comfb309512011-11-30 14:13:48 +00001071namespace {
1072
1073inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1074 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1075 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1076 return tilesX * tilesY;
1077}
1078
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001079inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001080 const SkIRect* srcRectPtr,
1081 int maxTextureSize) {
1082 static const int kSmallTileSize = 1 << 10;
1083 if (maxTextureSize <= kSmallTileSize) {
1084 return maxTextureSize;
1085 }
1086
1087 size_t maxTexTotalTileSize;
1088 size_t smallTotalTileSize;
1089
1090 if (NULL == srcRectPtr) {
1091 int w = bitmap.width();
1092 int h = bitmap.height();
1093 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1094 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1095 } else {
1096 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1097 srcRectPtr->fTop,
1098 srcRectPtr->fRight,
1099 srcRectPtr->fBottom,
1100 maxTextureSize);
1101 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1102 srcRectPtr->fTop,
1103 srcRectPtr->fRight,
1104 srcRectPtr->fBottom,
1105 kSmallTileSize);
1106 }
1107 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1108 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1109
1110 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1111 return kSmallTileSize;
1112 } else {
1113 return maxTextureSize;
1114 }
1115}
1116}
1117
1118bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1119 const GrSamplerState& sampler,
1120 const SkIRect* srcRectPtr,
1121 int* tileSize) const {
1122 SkASSERT(NULL != tileSize);
1123
1124 // if bitmap is explictly texture backed then just use the texture
1125 if (NULL != bitmap.getTexture()) {
1126 return false;
1127 }
1128 // if it's larger than the max texture size, then we have no choice but
1129 // tiling
1130 const int maxTextureSize = fContext->getMaxTextureSize();
1131 if (bitmap.width() > maxTextureSize ||
1132 bitmap.height() > maxTextureSize) {
1133 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1134 return true;
1135 }
1136 // if we are going to have to draw the whole thing, then don't tile
1137 if (NULL == srcRectPtr) {
1138 return false;
1139 }
1140 // if the entire texture is already in our cache then no reason to tile it
1141 if (this->isBitmapInTextureCache(bitmap, sampler)) {
1142 return false;
1143 }
1144
1145 // At this point we know we could do the draw by uploading the entire bitmap
1146 // as a texture. However, if the texture would be large compared to the
1147 // cache size and we don't require most of it for this draw then tile to
1148 // reduce the amount of upload and cache spill.
1149
1150 // assumption here is that sw bitmap size is a good proxy for its size as
1151 // a texture
1152 size_t bmpSize = bitmap.getSize();
bsalomon@google.com8f7e1da2012-06-21 19:48:32 +00001153 size_t cacheSize = fContext->getTextureCacheBudget();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001154 if (bmpSize < cacheSize / 2) {
1155 return false;
1156 }
1157
1158 SkFixed fracUsed =
1159 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1160 (srcRectPtr->height() << 16) / bitmap.height());
1161 if (fracUsed <= SK_FixedHalf) {
1162 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1163 return true;
1164 } else {
1165 return false;
1166 }
1167}
1168
reed@google.comac10a2d2010-12-22 21:39:39 +00001169void SkGpuDevice::drawBitmap(const SkDraw& draw,
1170 const SkBitmap& bitmap,
1171 const SkIRect* srcRectPtr,
1172 const SkMatrix& m,
1173 const SkPaint& paint) {
1174 CHECK_SHOULD_DRAW(draw);
1175
1176 SkIRect srcRect;
1177 if (NULL == srcRectPtr) {
1178 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1179 } else {
1180 srcRect = *srcRectPtr;
1181 }
1182
junov@google.comd935cfb2011-06-27 20:48:23 +00001183 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001184 // Convert the bitmap to a shader so that the rect can be drawn
1185 // through drawRect, which supports mask filters.
1186 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001187 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001188 if (srcRectPtr) {
1189 if (!bitmap.extractSubset(&tmp, srcRect)) {
1190 return; // extraction failed
1191 }
1192 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001193 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001194 }
1195 SkPaint paintWithTexture(paint);
1196 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1197 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001198 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001199 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001200
junov@google.com1d329782011-07-28 20:10:09 +00001201 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001202 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001203 // also affect the behavior of the mask filter.
1204 SkMatrix drawMatrix;
1205 drawMatrix.setConcat(*draw.fMatrix, m);
1206 SkDraw transformedDraw(draw);
1207 transformedDraw.fMatrix = &drawMatrix;
1208
1209 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1210
junov@google.comd935cfb2011-06-27 20:48:23 +00001211 return;
1212 }
1213
bsalomon@google.com5782d712011-01-21 21:03:59 +00001214 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001215 if (!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001216 return;
1217 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001218 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001219 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001220 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001221 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001222 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001223 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001224
bsalomon@google.comfb309512011-11-30 14:13:48 +00001225 int tileSize;
1226 if (!this->shouldTileBitmap(bitmap, *sampler, srcRectPtr, &tileSize)) {
1227 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001228 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001229 return;
1230 }
1231
1232 // undo the translate done by SkCanvas
1233 int DX = SkMax32(0, srcRect.fLeft);
1234 int DY = SkMax32(0, srcRect.fTop);
1235 // compute clip bounds in local coordinates
1236 SkIRect clipRect;
1237 {
1238 SkRect r;
1239 r.set(draw.fClip->getBounds());
1240 SkMatrix matrix, inverse;
1241 matrix.setConcat(*draw.fMatrix, m);
1242 if (!matrix.invert(&inverse)) {
1243 return;
1244 }
1245 inverse.mapRect(&r);
1246 r.roundOut(&clipRect);
1247 // apply the canvas' translate to our local clip
1248 clipRect.offset(DX, DY);
1249 }
1250
bsalomon@google.comfb309512011-11-30 14:13:48 +00001251 int nx = bitmap.width() / tileSize;
1252 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001253 for (int x = 0; x <= nx; x++) {
1254 for (int y = 0; y <= ny; y++) {
1255 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001256 tileR.set(x * tileSize, y * tileSize,
1257 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001258 if (!SkIRect::Intersects(tileR, clipRect)) {
1259 continue;
1260 }
1261
1262 SkIRect srcR = tileR;
1263 if (!srcR.intersect(srcRect)) {
1264 continue;
1265 }
1266
1267 SkBitmap tmpB;
1268 if (bitmap.extractSubset(&tmpB, tileR)) {
1269 // now offset it to make it "local" to our tmp bitmap
1270 srcR.offset(-tileR.fLeft, -tileR.fTop);
1271
1272 SkMatrix tmpM(m);
1273 {
1274 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1275 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1276 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1277 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001278 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001279 }
1280 }
1281 }
1282}
1283
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001284namespace {
1285
1286bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1287 // detect pixel disalignment
1288 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1289 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1290 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1291 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1292 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1293 COLOR_BLEED_TOLERANCE &&
1294 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1295 COLOR_BLEED_TOLERANCE) {
1296 return true;
1297 }
1298 return false;
1299}
1300
1301bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1302 const SkMatrix& m) {
1303 // Only gets called if hasAlignedSamples returned false.
1304 // So we can assume that sampling is axis aligned but not texel aligned.
1305 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
1306 SkRect innerSrcRect(srcRect), innerTransformedRect,
1307 outerTransformedRect(transformedRect);
1308 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1309 m.mapRect(&innerTransformedRect, innerSrcRect);
1310
1311 // The gap between outerTransformedRect and innerTransformedRect
1312 // represents the projection of the source border area, which is
1313 // problematic for color bleeding. We must check whether any
1314 // destination pixels sample the border area.
1315 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1316 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1317 SkIRect outer, inner;
1318 outerTransformedRect.round(&outer);
1319 innerTransformedRect.round(&inner);
1320 // If the inner and outer rects round to the same result, it means the
1321 // border does not overlap any pixel centers. Yay!
1322 return inner != outer;
1323}
1324
1325} // unnamed namespace
1326
reed@google.comac10a2d2010-12-22 21:39:39 +00001327/*
1328 * This is called by drawBitmap(), which has to handle images that may be too
1329 * large to be represented by a single texture.
1330 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001331 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1332 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001333 */
1334void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1335 const SkBitmap& bitmap,
1336 const SkIRect& srcRect,
1337 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001338 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001339 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1340 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001341
reed@google.com9c49bc32011-07-07 13:42:37 +00001342 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001343 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001344 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001345 return;
1346 }
1347
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001348 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001349
1350 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1351 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001352 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001353
1354 GrTexture* texture;
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001355 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001356 if (NULL == texture) {
1357 return;
1358 }
1359
bsalomon@google.com452943d2011-10-31 17:37:14 +00001360 grPaint->setTexture(kBitmapTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001361
reed@google.com20efde72011-05-09 17:00:02 +00001362 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1363 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001364 GrRect paintRect;
bsalomon@google.com91832162012-03-08 19:53:02 +00001365 float wInv = 1.f / bitmap.width();
1366 float hInv = 1.f / bitmap.height();
1367 paintRect.setLTRB(SkFloatToScalar(srcRect.fLeft * wInv),
1368 SkFloatToScalar(srcRect.fTop * hInv),
1369 SkFloatToScalar(srcRect.fRight * wInv),
1370 SkFloatToScalar(srcRect.fBottom * hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001371
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001372 bool needsTextureDomain = false;
1373 if (GrSamplerState::kBilinear_Filter == sampler->getFilter())
1374 {
1375 // Need texture domain if drawing a sub rect.
1376 needsTextureDomain = srcRect.width() < bitmap.width() ||
1377 srcRect.height() < bitmap.height();
1378 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1379 // sampling is axis-aligned
1380 GrRect floatSrcRect, transformedRect;
1381 floatSrcRect.set(srcRect);
1382 SkMatrix srcToDeviceMatrix(m);
1383 srcToDeviceMatrix.postConcat(*draw.fMatrix);
1384 srcToDeviceMatrix.mapRect(&transformedRect, floatSrcRect);
1385
1386 if (hasAlignedSamples(floatSrcRect, transformedRect)) {
1387 // Samples are texel-aligned, so filtering is futile
1388 sampler->setFilter(GrSamplerState::kNearest_Filter);
1389 needsTextureDomain = false;
1390 } else {
1391 needsTextureDomain = needsTextureDomain &&
1392 mayColorBleed(floatSrcRect, transformedRect, m);
1393 }
1394 }
1395 }
1396
1397 GrRect textureDomain = GrRect::MakeEmpty();
1398
1399 if (needsTextureDomain) {
1400 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001401 GrScalar left, top, right, bottom;
1402 if (srcRect.width() > 1) {
1403 GrScalar border = GR_ScalarHalf / bitmap.width();
1404 left = paintRect.left() + border;
1405 right = paintRect.right() - border;
1406 } else {
1407 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1408 }
1409 if (srcRect.height() > 1) {
1410 GrScalar border = GR_ScalarHalf / bitmap.height();
1411 top = paintRect.top() + border;
1412 bottom = paintRect.bottom() - border;
1413 } else {
1414 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1415 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001416 textureDomain.setLTRB(left, top, right, bottom);
junov@google.com6acc9b32011-05-16 18:32:07 +00001417 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001418 sampler->setTextureDomain(textureDomain);
junov@google.com6acc9b32011-05-16 18:32:07 +00001419
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001420 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001421}
1422
reed@google.com8926b162012-03-23 15:36:36 +00001423static GrTexture* filter_texture(GrContext* context, GrTexture* texture,
1424 SkImageFilter* filter, const GrRect& rect) {
1425 GrAssert(filter);
1426
1427 SkSize blurSize;
1428 SkISize radius;
1429
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001430 GrTextureDesc desc;
1431 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1432 desc.fWidth = SkScalarCeilToInt(rect.width());
1433 desc.fHeight = SkScalarCeilToInt(rect.height());
1434 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
reed@google.com8926b162012-03-23 15:36:36 +00001435
1436 if (filter->asABlur(&blurSize)) {
1437 GrAutoScratchTexture temp1, temp2;
1438 texture = context->gaussianBlur(texture, &temp1, &temp2, rect,
1439 blurSize.width(),
1440 blurSize.height());
1441 texture->ref();
1442 } else if (filter->asADilate(&radius)) {
1443 GrAutoScratchTexture temp1(context, desc), temp2(context, desc);
1444 texture = context->applyMorphology(texture, rect,
1445 temp1.texture(), temp2.texture(),
bsalomon@google.comb505a122012-05-31 18:40:36 +00001446 GrContext::kDilate_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001447 radius);
1448 texture->ref();
1449 } else if (filter->asAnErode(&radius)) {
1450 GrAutoScratchTexture temp1(context, desc), temp2(context, desc);
1451 texture = context->applyMorphology(texture, rect,
1452 temp1.texture(), temp2.texture(),
bsalomon@google.comb505a122012-05-31 18:40:36 +00001453 GrContext::kErode_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001454 radius);
1455 texture->ref();
1456 }
1457 return texture;
1458}
1459
reed@google.comac10a2d2010-12-22 21:39:39 +00001460void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1461 int left, int top, const SkPaint& paint) {
1462 CHECK_SHOULD_DRAW(draw);
1463
reed@google.com8926b162012-03-23 15:36:36 +00001464 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001465 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1466 return;
1467 }
1468
reed@google.com76dd2772012-01-05 21:15:07 +00001469 int w = bitmap.width();
1470 int h = bitmap.height();
1471
bsalomon@google.com5782d712011-01-21 21:03:59 +00001472 GrPaint grPaint;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001473 if(!skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001474 return;
1475 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001476
bsalomon@google.com5782d712011-01-21 21:03:59 +00001477 GrAutoMatrix avm(fContext, GrMatrix::I());
1478
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001479 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001480
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001481 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001482 sampler->reset();
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001483 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001484 grPaint.setTexture(kBitmapTextureIdx, texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001485
reed@google.com8926b162012-03-23 15:36:36 +00001486 SkImageFilter* filter = paint.getImageFilter();
1487 if (NULL != filter) {
1488 GrTexture* filteredTexture = filter_texture(fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001489 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001490 if (filteredTexture) {
1491 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1492 texture = filteredTexture;
1493 filteredTexture->unref();
1494 }
reed@google.com76dd2772012-01-05 21:15:07 +00001495 }
reed@google.com8926b162012-03-23 15:36:36 +00001496
bsalomon@google.com5782d712011-01-21 21:03:59 +00001497 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001498 GrRect::MakeXYWH(GrIntToScalar(left),
1499 GrIntToScalar(top),
1500 GrIntToScalar(w),
1501 GrIntToScalar(h)),
1502 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1503 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001504}
1505
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001506void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
reed@google.comac10a2d2010-12-22 21:39:39 +00001507 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001508 // clear of the source device must occur before CHECK_SHOULD_DRAW
1509 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1510 if (dev->fNeedClear) {
1511 // TODO: could check here whether we really need to draw at all
1512 dev->clear(0x0);
1513 }
1514
reed@google.comac10a2d2010-12-22 21:39:39 +00001515 CHECK_SHOULD_DRAW(draw);
1516
bsalomon@google.com5782d712011-01-21 21:03:59 +00001517 GrPaint grPaint;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001518 if (!dev->bindDeviceAsTexture(&grPaint) ||
bsalomon@google.com84405e02012-03-05 19:57:21 +00001519 !skPaint2GrPaintNoShader(paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001520 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001521 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001522
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001523 GrTexture* devTex = grPaint.getTexture(0);
1524 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001525
reed@google.com8926b162012-03-23 15:36:36 +00001526 SkImageFilter* filter = paint.getImageFilter();
1527 if (NULL != filter) {
robertphillips@google.com8637a362012-04-10 18:32:35 +00001528 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
1529 SkIntToScalar(devTex->height()));
reed@google.com8926b162012-03-23 15:36:36 +00001530 GrTexture* filteredTexture = filter_texture(fContext, devTex, filter,
1531 rect);
1532 if (filteredTexture) {
1533 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1534 devTex = filteredTexture;
1535 filteredTexture->unref();
1536 }
1537 }
1538
bsalomon@google.com5782d712011-01-21 21:03:59 +00001539 const SkBitmap& bm = dev->accessBitmap(false);
1540 int w = bm.width();
1541 int h = bm.height();
1542
1543 GrAutoMatrix avm(fContext, GrMatrix::I());
1544
bsalomon@google.com97912912011-12-06 16:30:36 +00001545 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001546
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001547 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1548 GrIntToScalar(y),
1549 GrIntToScalar(w),
1550 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001551
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001552 // The device being drawn may not fill up its texture (saveLayer uses
1553 // the approximate ).
1554 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1555 GR_Scalar1 * h / devTex->height());
1556
1557 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001558}
1559
reed@google.com8926b162012-03-23 15:36:36 +00001560bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
reed@google.com76dd2772012-01-05 21:15:07 +00001561 SkSize size;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001562 SkISize radius;
1563 if (!filter->asABlur(&size) && !filter->asADilate(&radius) && !filter->asAnErode(&radius)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001564 return false;
1565 }
reed@google.com8926b162012-03-23 15:36:36 +00001566 return true;
1567}
1568
1569bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1570 const SkMatrix& ctm,
1571 SkBitmap* result, SkIPoint* offset) {
1572 // want explicitly our impl, so guard against a subclass of us overriding it
1573 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001574 return false;
1575 }
reed@google.com8926b162012-03-23 15:36:36 +00001576
1577 SkAutoLockPixels alp(src, !src.getTexture());
1578 if (!src.getTexture() && !src.readyToDraw()) {
1579 return false;
1580 }
1581
1582 GrPaint paint;
1583 paint.reset();
1584
1585 GrSamplerState* sampler = paint.textureSampler(kBitmapTextureIdx);
1586
1587 GrTexture* texture;
1588 SkAutoCachedTexture act(this, src, sampler, &texture);
1589
1590 result->setConfig(src.config(), src.width(), src.height());
robertphillips@google.com8637a362012-04-10 18:32:35 +00001591 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
1592 SkIntToScalar(src.height()));
reed@google.com8926b162012-03-23 15:36:36 +00001593 GrTexture* resultTexture = filter_texture(fContext, texture, filter, rect);
1594 if (resultTexture) {
1595 result->setPixelRef(new SkGrTexturePixelRef(resultTexture))->unref();
1596 resultTexture->unref();
1597 }
reed@google.com76dd2772012-01-05 21:15:07 +00001598 return true;
1599}
1600
reed@google.comac10a2d2010-12-22 21:39:39 +00001601///////////////////////////////////////////////////////////////////////////////
1602
1603// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001604static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001605 kTriangles_GrPrimitiveType,
1606 kTriangleStrip_GrPrimitiveType,
1607 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001608};
1609
1610void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1611 int vertexCount, const SkPoint vertices[],
1612 const SkPoint texs[], const SkColor colors[],
1613 SkXfermode* xmode,
1614 const uint16_t indices[], int indexCount,
1615 const SkPaint& paint) {
1616 CHECK_SHOULD_DRAW(draw);
1617
bsalomon@google.com5782d712011-01-21 21:03:59 +00001618 GrPaint grPaint;
1619 SkAutoCachedTexture act;
1620 // we ignore the shader if texs is null.
1621 if (NULL == texs) {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001622 if (!skPaint2GrPaintNoShader(paint,
1623 false,
1624 NULL == colors,
1625 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001626 return;
1627 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001628 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001629 if (!skPaint2GrPaintShader(this,
1630 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001631 NULL == colors,
1632 &act,
1633 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001634 return;
1635 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001636 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001637
1638 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001639 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001640 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1641#if 0
1642 return
1643#endif
1644 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001645 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001646
bsalomon@google.com498776a2011-08-16 19:20:44 +00001647 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1648 if (NULL != colors) {
1649 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001650 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001651 for (int i = 0; i < vertexCount; ++i) {
1652 convertedColors[i] = SkGr::SkColor2GrColor(colors[i]);
1653 }
1654 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001655 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001656 fContext->drawVertices(grPaint,
1657 gVertexMode2PrimitiveType[vmode],
1658 vertexCount,
1659 (GrPoint*) vertices,
1660 (GrPoint*) texs,
1661 colors,
1662 indices,
1663 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001664}
1665
1666///////////////////////////////////////////////////////////////////////////////
1667
1668static void GlyphCacheAuxProc(void* data) {
1669 delete (GrFontScaler*)data;
1670}
1671
1672static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1673 void* auxData;
1674 GrFontScaler* scaler = NULL;
1675 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1676 scaler = (GrFontScaler*)auxData;
1677 }
1678 if (NULL == scaler) {
1679 scaler = new SkGrFontScaler(cache);
1680 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1681 }
1682 return scaler;
1683}
1684
1685static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1686 SkFixed fx, SkFixed fy,
1687 const SkGlyph& glyph) {
1688 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1689
bungeman@google.com15865a72012-01-11 16:28:04 +00001690 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001691
1692 if (NULL == procs->fFontScaler) {
1693 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1694 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001695
bungeman@google.com15865a72012-01-11 16:28:04 +00001696 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1697 glyph.getSubXFixed(),
1698 glyph.getSubYFixed()),
1699 SkFixedFloorToFixed(fx),
1700 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001701 procs->fFontScaler);
1702}
1703
bsalomon@google.com5782d712011-01-21 21:03:59 +00001704SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001705
1706 // deferred allocation
1707 if (NULL == fDrawProcs) {
1708 fDrawProcs = new GrSkDrawProcs;
1709 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1710 fDrawProcs->fContext = fContext;
1711 }
1712
1713 // init our (and GL's) state
1714 fDrawProcs->fTextContext = context;
1715 fDrawProcs->fFontScaler = NULL;
1716 return fDrawProcs;
1717}
1718
1719void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1720 size_t byteLength, SkScalar x, SkScalar y,
1721 const SkPaint& paint) {
1722 CHECK_SHOULD_DRAW(draw);
1723
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001724 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001725 // this guy will just call our drawPath()
1726 draw.drawText((const char*)text, byteLength, x, y, paint);
1727 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001728 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001729
1730 GrPaint grPaint;
1731 SkAutoCachedTexture act;
1732
bsalomon@google.com84405e02012-03-05 19:57:21 +00001733 if (!skPaint2GrPaintShader(this,
1734 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001735 true,
1736 &act,
1737 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001738 return;
1739 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001740 GrTextContext::AutoFinish txtCtxAF(this->getTextContext(), fContext,
1741 grPaint, draw.fExtMatrix);
1742 myDraw.fProcs = this->initDrawForText(txtCtxAF.getTextContext());
reed@google.comac10a2d2010-12-22 21:39:39 +00001743 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1744 }
1745}
1746
1747void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1748 size_t byteLength, const SkScalar pos[],
1749 SkScalar constY, int scalarsPerPos,
1750 const SkPaint& paint) {
1751 CHECK_SHOULD_DRAW(draw);
1752
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001753 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001754 // this guy will just call our drawPath()
1755 draw.drawPosText((const char*)text, byteLength, pos, constY,
1756 scalarsPerPos, paint);
1757 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001758 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001759
1760 GrPaint grPaint;
1761 SkAutoCachedTexture act;
bsalomon@google.com84405e02012-03-05 19:57:21 +00001762 if (!skPaint2GrPaintShader(this,
1763 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001764 true,
1765 &act,
1766 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001767 return;
1768 }
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001769 GrTextContext::AutoFinish txtCtxAF(this->getTextContext(), fContext,
1770 grPaint, draw.fExtMatrix);
1771 myDraw.fProcs = this->initDrawForText(txtCtxAF.getTextContext());
reed@google.comac10a2d2010-12-22 21:39:39 +00001772 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1773 scalarsPerPos, paint);
1774 }
1775}
1776
1777void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1778 size_t len, const SkPath& path,
1779 const SkMatrix* m, const SkPaint& paint) {
1780 CHECK_SHOULD_DRAW(draw);
1781
1782 SkASSERT(draw.fDevice == this);
1783 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1784}
1785
1786///////////////////////////////////////////////////////////////////////////////
1787
reed@google.comf67e4cf2011-03-15 20:56:58 +00001788bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1789 if (!paint.isLCDRenderText()) {
1790 // we're cool with the paint as is
1791 return false;
1792 }
1793
1794 if (paint.getShader() ||
1795 paint.getXfermode() || // unless its srcover
1796 paint.getMaskFilter() ||
1797 paint.getRasterizer() ||
1798 paint.getColorFilter() ||
1799 paint.getPathEffect() ||
1800 paint.isFakeBoldText() ||
1801 paint.getStyle() != SkPaint::kFill_Style) {
1802 // turn off lcd
1803 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1804 flags->fHinting = paint.getHinting();
1805 return true;
1806 }
1807 // we're cool with the paint as is
1808 return false;
1809}
1810
reed@google.com75d939b2011-12-07 15:07:23 +00001811void SkGpuDevice::flush() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001812 DO_DEFERRED_CLEAR;
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001813 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001814}
1815
reed@google.comf67e4cf2011-03-15 20:56:58 +00001816///////////////////////////////////////////////////////////////////////////////
1817
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001818SkGpuDevice::TexCache SkGpuDevice::lockCachedTexture(
1819 const SkBitmap& bitmap,
1820 const GrSamplerState* sampler) {
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001821 GrContext::TextureCacheEntry entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001822 GrContext* ctx = this->context();
bsalomon@google.comfea37b52011-04-25 15:51:06 +00001823
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001824 if (!bitmap.isVolatile()) {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001825 uint64_t key = bitmap.getGenerationID();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001826 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001827
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001828 GrTextureDesc desc;
1829 desc.fWidth = bitmap.width();
1830 desc.fHeight = bitmap.height();
1831 desc.fConfig = SkGr::BitmapConfig2PixelConfig(bitmap.config());
robertphillips@google.com56f22442012-06-08 14:21:26 +00001832 desc.fClientCacheID = key;
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001833
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001834 entry = ctx->findAndLockTexture(desc, sampler);
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001835 if (NULL == entry.texture()) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001836 entry = sk_gr_create_bitmap_texture(ctx, key, sampler,
1837 bitmap);
bsalomon@google.com18bbb8b2012-03-30 18:29:01 +00001838 }
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001839 } else {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001840 entry = sk_gr_create_bitmap_texture(ctx, kUncached_CacheID,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001841 sampler, bitmap);
1842 }
1843 if (NULL == entry.texture()) {
1844 GrPrintf("---- failed to create texture for cache [%d %d]\n",
1845 bitmap.width(), bitmap.height());
reed@google.comac10a2d2010-12-22 21:39:39 +00001846 }
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001847 return entry;
reed@google.comac10a2d2010-12-22 21:39:39 +00001848}
1849
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001850void SkGpuDevice::unlockCachedTexture(TexCache cache) {
1851 this->context()->unlockTexture(cache);
reed@google.comac10a2d2010-12-22 21:39:39 +00001852}
1853
bsalomon@google.comfb309512011-11-30 14:13:48 +00001854bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
1855 const GrSamplerState& sampler) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001856 uint64_t key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001857 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001858
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001859 GrTextureDesc desc;
1860 desc.fWidth = bitmap.width();
1861 desc.fHeight = bitmap.height();
1862 desc.fConfig = SkGr::BitmapConfig2PixelConfig(bitmap.config());
1863 desc.fClientCacheID = key;
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001864
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001865 return this->context()->isTextureInCache(desc, &sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001866}
1867
1868
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001869SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1870 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001871 bool isOpaque,
1872 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001873 GrTextureDesc desc;
1874 desc.fConfig = fRenderTarget->config();
1875 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1876 desc.fWidth = width;
1877 desc.fHeight = height;
1878 desc.fSampleCnt = fRenderTarget->numSamples();
1879
1880 GrContext::TextureCacheEntry cacheEntry;
1881 GrTexture* texture;
1882 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001883 // Skia's convention is to only clear a device if it is non-opaque.
1884 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001885
1886#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1887 // layers are never draw in repeat modes, so we can request an approx
1888 // match and ignore any padding.
1889 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1890 GrContext::kApprox_ScratchTexMatch :
1891 GrContext::kExact_ScratchTexMatch;
1892 cacheEntry = fContext->lockScratchTexture(desc, matchType);
1893 texture = cacheEntry.texture();
1894#else
1895 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1896 texture = tunref.get();
1897#endif
1898 if (texture) {
1899 return SkNEW_ARGS(SkGpuDevice,(fContext,
1900 texture,
1901 cacheEntry,
1902 needClear));
1903 } else {
1904 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1905 width, height);
1906 return NULL;
1907 }
1908}
1909
1910SkGpuDevice::SkGpuDevice(GrContext* context,
1911 GrTexture* texture,
1912 TexCache cacheEntry,
1913 bool needClear)
1914 : SkDevice(make_bitmap(context, texture->asRenderTarget())) {
1915 GrAssert(texture && texture->asRenderTarget());
1916 GrAssert(NULL == cacheEntry.texture() || texture == cacheEntry.texture());
1917 this->initFromRenderTarget(context, texture->asRenderTarget());
1918 fCache = cacheEntry;
1919 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001920}
1921
bsalomon@google.comf4a9c822012-03-16 14:02:46 +00001922GrTextContext* SkGpuDevice::getTextContext() {
1923 if (NULL == fTextContext) {
1924 fTextContext = new GrDefaultTextContext();
1925 }
1926 return fTextContext;
1927}