blob: ec8f26c5fb57894e3fe80a7f4ec3eb60880a8d10 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
tomhudson@google.com898e7b52012-06-01 20:42:15 +00008#include "SkGpuDevice.h"
reed@google.comac10a2d2010-12-22 21:39:39 +00009
twiz@google.com58071162012-07-18 21:41:50 +000010#include "effects/GrColorTableEffect.h"
tomhudson@google.com2f68e762012-07-17 18:43:21 +000011#include "effects/GrTextureDomainEffect.h"
epoger@google.comec3ed6a2011-07-28 14:26:00 +000012
reed@google.comac10a2d2010-12-22 21:39:39 +000013#include "GrContext.h"
14#include "GrTextContext.h"
15
robertphillips@google.come9c04692012-06-29 00:30:13 +000016#include "SkGrTexturePixelRef.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000017
Scroggo97c88c22011-05-11 14:05:25 +000018#include "SkColorFilter.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000019#include "SkDrawProcs.h"
20#include "SkGlyphCache.h"
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +000021#include "SkImageFilter.h"
reed@google.comfe626382011-09-21 13:50:35 +000022#include "SkTLazy.h"
reed@google.comc9aa5872011-04-05 21:05:37 +000023#include "SkUtils.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000024
bsalomon@google.com06cd7322012-03-30 18:45:35 +000025#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
reed@google.comac10a2d2010-12-22 21:39:39 +000026
27#if 0
28 extern bool (*gShouldDrawProc)();
29 #define CHECK_SHOULD_DRAW(draw) \
30 do { \
31 if (gShouldDrawProc && !gShouldDrawProc()) return; \
32 this->prepareRenderTarget(draw); \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000033 GrAssert(!fNeedClear) \
reed@google.comac10a2d2010-12-22 21:39:39 +000034 } while (0)
35#else
bsalomon@google.com06cd7322012-03-30 18:45:35 +000036 #define CHECK_SHOULD_DRAW(draw) this->prepareRenderTarget(draw); \
37 GrAssert(!fNeedClear)
reed@google.comac10a2d2010-12-22 21:39:39 +000038#endif
39
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000040// we use the same texture slot on GrPaint for bitmaps and shaders
41// (since drawBitmap, drawSprite, and drawDevice ignore skia's shader)
42enum {
43 kBitmapTextureIdx = 0,
twiz@google.com58071162012-07-18 21:41:50 +000044 kShaderTextureIdx = 0,
45 kColorFilterTextureIdx = 1
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000046};
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
reed@google.comb0a34d82012-07-11 19:57:55 +000076#define CHECK_FOR_NODRAW_ANNOTATION(paint) \
77 do { if (paint.isNoDrawAnnotation()) { return; } } while (0)
78
79///////////////////////////////////////////////////////////////////////////////
80
81
bsalomon@google.com84405e02012-03-05 19:57:21 +000082class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
83public:
84 SkAutoCachedTexture() { }
85 SkAutoCachedTexture(SkGpuDevice* device,
86 const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +000087 const GrTextureParams* params,
bsalomon@google.com84405e02012-03-05 19:57:21 +000088 GrTexture** texture) {
89 GrAssert(texture);
bsalomon@google.comb8670992012-07-25 21:27:09 +000090 *texture = this->set(device, bitmap, params);
reed@google.comac10a2d2010-12-22 21:39:39 +000091 }
reed@google.comac10a2d2010-12-22 21:39:39 +000092
bsalomon@google.com84405e02012-03-05 19:57:21 +000093 ~SkAutoCachedTexture() {
94 if (fTex.texture()) {
rileya@google.com24f3ad12012-07-18 21:47:40 +000095 GrUnlockCachedBitmapTexture(fDevice->context(), fTex);
bsalomon@google.com84405e02012-03-05 19:57:21 +000096 }
reed@google.comac10a2d2010-12-22 21:39:39 +000097 }
bsalomon@google.com84405e02012-03-05 19:57:21 +000098
99 GrTexture* set(SkGpuDevice* device,
100 const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000101 const GrTextureParams* params) {
bsalomon@google.com84405e02012-03-05 19:57:21 +0000102 if (fTex.texture()) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000103 GrUnlockCachedBitmapTexture(fDevice->context(), fTex);
bsalomon@google.com84405e02012-03-05 19:57:21 +0000104 }
105 fDevice = device;
106 GrTexture* texture = (GrTexture*)bitmap.getTexture();
107 if (texture) {
108 // return the native texture
109 fTex.reset();
110 } else {
111 // look it up in our cache
bsalomon@google.comb8670992012-07-25 21:27:09 +0000112 fTex = GrLockCachedBitmapTexture(device->context(), bitmap, params);
bsalomon@google.com84405e02012-03-05 19:57:21 +0000113 texture = fTex.texture();
114 }
115 return texture;
116 }
117
118private:
119 SkGpuDevice* fDevice;
120 GrContext::TextureCacheEntry fTex;
121};
reed@google.comac10a2d2010-12-22 21:39:39 +0000122
123///////////////////////////////////////////////////////////////////////////////
124
125bool gDoTraceDraw;
126
127struct GrSkDrawProcs : public SkDrawProcs {
128public:
129 GrContext* fContext;
130 GrTextContext* fTextContext;
131 GrFontScaler* fFontScaler; // cached in the skia glyphcache
132};
133
134///////////////////////////////////////////////////////////////////////////////
135
reed@google.comaf951c92011-06-16 19:10:39 +0000136static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
137 switch (config) {
138 case kAlpha_8_GrPixelConfig:
139 *isOpaque = false;
140 return SkBitmap::kA8_Config;
141 case kRGB_565_GrPixelConfig:
142 *isOpaque = true;
143 return SkBitmap::kRGB_565_Config;
144 case kRGBA_4444_GrPixelConfig:
145 *isOpaque = false;
146 return SkBitmap::kARGB_4444_Config;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000147 case kSkia8888_PM_GrPixelConfig:
148 // we don't currently have a way of knowing whether
149 // a 8888 is opaque based on the config.
150 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000151 return SkBitmap::kARGB_8888_Config;
152 default:
153 *isOpaque = false;
154 return SkBitmap::kNo_Config;
155 }
156}
reed@google.comac10a2d2010-12-22 21:39:39 +0000157
reed@google.comaf951c92011-06-16 19:10:39 +0000158static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000159 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000160
161 bool isOpaque;
162 SkBitmap bitmap;
163 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
164 renderTarget->width(), renderTarget->height());
165 bitmap.setIsOpaque(isOpaque);
166 return bitmap;
167}
168
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000169SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000170: SkDevice(make_bitmap(context, texture->asRenderTarget()))
171, fClipStack(NULL) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000172 this->initFromRenderTarget(context, texture->asRenderTarget());
173}
174
reed@google.comaf951c92011-06-16 19:10:39 +0000175SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000176: SkDevice(make_bitmap(context, renderTarget))
177, fClipStack(NULL) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000178 this->initFromRenderTarget(context, renderTarget);
179}
180
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000181void SkGpuDevice::initFromRenderTarget(GrContext* context,
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000182 GrRenderTarget* renderTarget) {
reed@google.comaf951c92011-06-16 19:10:39 +0000183 fNeedPrepareRenderTarget = false;
184 fDrawProcs = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000185
reed@google.comaf951c92011-06-16 19:10:39 +0000186 fContext = context;
187 fContext->ref();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000188
reed@google.comaf951c92011-06-16 19:10:39 +0000189 fTexture = NULL;
190 fRenderTarget = NULL;
191 fNeedClear = false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000192
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000193 GrAssert(NULL != renderTarget);
194 fRenderTarget = renderTarget;
195 fRenderTarget->ref();
196 // if this RT is also a texture, hold a ref on it
197 fTexture = fRenderTarget->asTexture();
198 SkSafeRef(fTexture);
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000199
200 // Create a pixel ref for the underlying SkBitmap. We prefer a texture pixel
201 // ref to a render target pixel reft. The pixel ref may get ref'ed outside
202 // the device via accessBitmap. This external ref may outlive the device.
203 // Since textures own their render targets (but not vice-versa) we
204 // are ensuring that both objects will live as long as the pixel ref.
205 SkPixelRef* pr;
206 if (fTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000207 pr = SkNEW_ARGS(SkGrTexturePixelRef, (fTexture));
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000208 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000209 pr = SkNEW_ARGS(SkGrRenderTargetPixelRef, (fRenderTarget));
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000210 }
reed@google.comaf951c92011-06-16 19:10:39 +0000211 this->setPixelRef(pr, 0)->unref();
212}
213
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000214SkGpuDevice::SkGpuDevice(GrContext* context,
215 SkBitmap::Config config,
216 int width,
217 int height)
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000218 : SkDevice(config, width, height, false /*isOpaque*/)
219 , fClipStack(NULL) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000220 fNeedPrepareRenderTarget = false;
221 fDrawProcs = NULL;
222
reed@google.com7b201d22011-01-11 18:59:23 +0000223 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000224 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000225
reed@google.comac10a2d2010-12-22 21:39:39 +0000226 fTexture = NULL;
227 fRenderTarget = NULL;
228 fNeedClear = false;
229
reed@google.comaf951c92011-06-16 19:10:39 +0000230 if (config != SkBitmap::kRGB_565_Config) {
231 config = SkBitmap::kARGB_8888_Config;
232 }
233 SkBitmap bm;
234 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000235
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000236 GrTextureDesc desc;
237 desc.fFlags = kRenderTarget_GrTextureFlagBit;
238 desc.fWidth = width;
239 desc.fHeight = height;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000240 desc.fConfig = SkBitmapConfig2GrPixelConfig(bm.config());
reed@google.comac10a2d2010-12-22 21:39:39 +0000241
reed@google.comaf951c92011-06-16 19:10:39 +0000242 fTexture = fContext->createUncachedTexture(desc, NULL, 0);
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000243
reed@google.comaf951c92011-06-16 19:10:39 +0000244 if (NULL != fTexture) {
245 fRenderTarget = fTexture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000246 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000247
reed@google.comaf951c92011-06-16 19:10:39 +0000248 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000249
reed@google.comaf951c92011-06-16 19:10:39 +0000250 // wrap the bitmap with a pixelref to expose our texture
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000251 SkGrTexturePixelRef* pr = SkNEW_ARGS(SkGrTexturePixelRef, (fTexture));
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000252 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000253 } else {
254 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
255 width, height);
256 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000257 }
258}
259
260SkGpuDevice::~SkGpuDevice() {
261 if (fDrawProcs) {
262 delete fDrawProcs;
263 }
264
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000265 // The SkGpuDevice gives the context the render target (e.g., in gainFocus)
266 // This call gives the context a chance to relinquish it
267 fContext->setRenderTarget(NULL);
268
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000269 SkSafeUnref(fTexture);
270 SkSafeUnref(fRenderTarget);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000271 if (fCache.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000272 GrAssert(NULL != fTexture);
273 GrAssert(fRenderTarget == fTexture->asRenderTarget());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000274 fContext->unlockTexture(fCache);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000275 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000276 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000277}
278
reed@google.comac10a2d2010-12-22 21:39:39 +0000279///////////////////////////////////////////////////////////////////////////////
280
281void SkGpuDevice::makeRenderTargetCurrent() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000282 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000283 fContext->setRenderTarget(fRenderTarget);
284 fContext->flush(true);
285 fNeedPrepareRenderTarget = true;
286}
287
288///////////////////////////////////////////////////////////////////////////////
289
bsalomon@google.comc4364992011-11-07 15:54:49 +0000290namespace {
291GrPixelConfig config8888_to_gr_config(SkCanvas::Config8888 config8888) {
292 switch (config8888) {
293 case SkCanvas::kNative_Premul_Config8888:
294 return kSkia8888_PM_GrPixelConfig;
295 case SkCanvas::kNative_Unpremul_Config8888:
296 return kSkia8888_UPM_GrPixelConfig;
297 case SkCanvas::kBGRA_Premul_Config8888:
298 return kBGRA_8888_PM_GrPixelConfig;
299 case SkCanvas::kBGRA_Unpremul_Config8888:
300 return kBGRA_8888_UPM_GrPixelConfig;
301 case SkCanvas::kRGBA_Premul_Config8888:
302 return kRGBA_8888_PM_GrPixelConfig;
303 case SkCanvas::kRGBA_Unpremul_Config8888:
304 return kRGBA_8888_UPM_GrPixelConfig;
305 default:
306 GrCrash("Unexpected Config8888.");
307 return kSkia8888_PM_GrPixelConfig;
308 }
309}
310}
311
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000312bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
313 int x, int y,
314 SkCanvas::Config8888 config8888) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000315 DO_DEFERRED_CLEAR;
bsalomon@google.com910267d2011-11-02 20:06:25 +0000316 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
317 SkASSERT(!bitmap.isNull());
318 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000319
bsalomon@google.com910267d2011-11-02 20:06:25 +0000320 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000321 GrPixelConfig config;
322 config = config8888_to_gr_config(config8888);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000323 return fContext->readRenderTargetPixels(fRenderTarget,
324 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000325 bitmap.width(),
326 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000327 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000328 bitmap.getPixels(),
329 bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000330}
331
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000332void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
333 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000334 SkAutoLockPixels alp(bitmap);
335 if (!bitmap.readyToDraw()) {
336 return;
337 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000338
339 GrPixelConfig config;
340 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
341 config = config8888_to_gr_config(config8888);
342 } else {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000343 config= SkBitmapConfig2GrPixelConfig(bitmap.config());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000344 }
345
bsalomon@google.com6f379512011-11-16 20:36:03 +0000346 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
347 config, bitmap.getPixels(), bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000348}
349
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000350void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
351 INHERITED::onAttachToCanvas(canvas);
352
353 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
354 fClipStack = canvas->getClipStack();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000355
356 fClipData.fClipStack = NULL;
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000357}
358
359void SkGpuDevice::onDetachFromCanvas() {
360 INHERITED::onDetachFromCanvas();
361
362 fClipStack = NULL;
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000363
364 fClipData.fClipStack = NULL;
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000365}
366
robertphillips@google.com607fe072012-07-24 13:54:00 +0000367#ifdef SK_DEBUG
368static void check_bounds(const SkClipStack& clipStack,
369 const SkRegion& clipRegion,
370 const SkIPoint& origin,
371 int renderTargetWidth,
372 int renderTargetHeight) {
373
robertphillips@google.com7b112892012-07-31 15:18:21 +0000374 SkIRect devBound;
375
376 devBound.setLTRB(0, 0, renderTargetWidth, renderTargetHeight);
377
robertphillips@google.com607fe072012-07-24 13:54:00 +0000378 SkClipStack::BoundsType boundType;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000379 SkRect canvTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000380
robertphillips@google.com7b112892012-07-31 15:18:21 +0000381 clipStack.getBounds(&canvTemp, &boundType);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000382 if (SkClipStack::kNormal_BoundsType == boundType) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000383 SkIRect devTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000384
robertphillips@google.com7b112892012-07-31 15:18:21 +0000385 canvTemp.roundOut(&devTemp);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000386
robertphillips@google.com7b112892012-07-31 15:18:21 +0000387 devTemp.offset(-origin.fX, -origin.fY);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000388
robertphillips@google.com7b112892012-07-31 15:18:21 +0000389 if (!devBound.intersect(devTemp)) {
390 devBound.setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000391 }
392 }
393
robertphillips@google.com7b112892012-07-31 15:18:21 +0000394// GrAssert(devBound.contains(clipRegion.getBounds()));
robertphillips@google.com607fe072012-07-24 13:54:00 +0000395}
396#endif
397
reed@google.comac10a2d2010-12-22 21:39:39 +0000398///////////////////////////////////////////////////////////////////////////////
399
400static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000401 const SkClipStack& clipStack,
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000402 GrClipData& clipData,
reed@google.com6f8f2922011-03-04 22:27:10 +0000403 const SkRegion& clipRegion,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000404 const SkIPoint& origin,
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000405 int renderTargetWidth, int renderTargetHeight,
406 GrClip* result) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000407 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000408
409 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000410 iter.reset(clipStack);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000411
412#ifdef SK_DEBUG
413 check_bounds(clipStack, clipRegion, origin,
414 renderTargetWidth, renderTargetHeight);
415#endif
416
robertphillips@google.com7b112892012-07-31 15:18:21 +0000417 SkRect devClipBounds;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000418 bool isIntersectionOfRects = false;
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000419 clipStack.getConservativeBounds(0, 0,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000420 renderTargetWidth,
421 renderTargetHeight,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000422 &devClipBounds,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000423 &isIntersectionOfRects);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000424
robertphillips@google.com7b112892012-07-31 15:18:21 +0000425 result->setFromIterator(&iter, devClipBounds);
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000426
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000427 GrAssert(result->isRect() == isIntersectionOfRects);
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000428
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000429 clipData.fClipStack = result;
430 clipData.fOrigin = origin;
431 context->setClip(&clipData);
reed@google.comac10a2d2010-12-22 21:39:39 +0000432}
433
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000434// call this every draw call, to ensure that the context reflects our state,
reed@google.comac10a2d2010-12-22 21:39:39 +0000435// and not the state from some other canvas/device
436void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000437 GrAssert(NULL != fClipStack);
438
reed@google.comac10a2d2010-12-22 21:39:39 +0000439 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000440 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000441
442 fContext->setRenderTarget(fRenderTarget);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000443 SkASSERT(draw.fClipStack && draw.fClipStack == fClipStack);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000444
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000445 convert_matrixclip(fContext, *draw.fMatrix,
446 *fClipStack, fClipData, *draw.fClip, this->getOrigin(),
447 fRenderTarget->width(), fRenderTarget->height(),
448 &fGrClip);
reed@google.comac10a2d2010-12-22 21:39:39 +0000449 fNeedPrepareRenderTarget = false;
450 }
451}
452
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000453void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
454 const SkClipStack& clipStack) {
455 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
456 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000457 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000458}
459
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000460void SkGpuDevice::gainFocus(const SkMatrix& matrix, const SkRegion& clip) {
461
462 GrAssert(NULL != fClipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000463
reed@google.comac10a2d2010-12-22 21:39:39 +0000464 fContext->setRenderTarget(fRenderTarget);
465
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000466 this->INHERITED::gainFocus(matrix, clip);
reed@google.comac10a2d2010-12-22 21:39:39 +0000467
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000468 convert_matrixclip(fContext, matrix, *fClipStack, fClipData, clip, this->getOrigin(),
469 fRenderTarget->width(), fRenderTarget->height(), &fGrClip);
reed@google.comac10a2d2010-12-22 21:39:39 +0000470
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000471 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000472}
473
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000474SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000475 DO_DEFERRED_CLEAR;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000476 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000477}
478
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000479bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000480 if (NULL != fTexture) {
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000481 paint->textureSampler(kBitmapTextureIdx)->setCustomStage(
482 SkNEW_ARGS(GrSingleTextureEffect, (fTexture)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000483 return true;
484 }
485 return false;
486}
487
488///////////////////////////////////////////////////////////////////////////////
489
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000490SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
491SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
492SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
493SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
494SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
495 shader_type_mismatch);
rileya@google.com3e332582012-07-03 13:43:35 +0000496SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
497 shader_type_mismatch);
rileya@google.com22e57f92012-07-19 15:16:19 +0000498SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
499SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000500
bsalomon@google.com84405e02012-03-05 19:57:21 +0000501namespace {
502
503// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
504// justAlpha indicates that skPaint's alpha should be used rather than the color
505// Callers may subsequently modify the GrPaint. Setting constantColor indicates
506// that the final paint will draw the same color at every pixel. This allows
507// an optimization where the the color filter can be applied to the skPaint's
twiz@google.com58071162012-07-18 21:41:50 +0000508// color once while converting to GrPaint and then ignored.
509inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
510 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000511 bool justAlpha,
512 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000513 SkGpuDevice::SkAutoCachedTexture* act,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000514 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000515
516 grPaint->fDither = skPaint.isDither();
517 grPaint->fAntiAlias = skPaint.isAntiAlias();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000518 grPaint->fCoverage = 0xFF;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000519
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000520 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
521 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000522
523 SkXfermode* mode = skPaint.getXfermode();
524 if (mode) {
525 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000526 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000527#if 0
528 return false;
529#endif
530 }
531 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000532 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
533 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
534
bsalomon@google.com5782d712011-01-21 21:03:59 +0000535 if (justAlpha) {
536 uint8_t alpha = skPaint.getAlpha();
537 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000538 // justAlpha is currently set to true only if there is a texture,
539 // so constantColor should not also be true.
540 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000541 } else {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000542 grPaint->fColor = SkColor2GrColor(skPaint.getColor());
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000543 GrAssert(!grPaint->isTextureStageEnabled(kShaderTextureIdx));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000544 }
Scroggo97c88c22011-05-11 14:05:25 +0000545 SkColorFilter* colorFilter = skPaint.getColorFilter();
546 SkColor color;
547 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000548 SkScalar matrix[20];
twiz@google.com58071162012-07-18 21:41:50 +0000549 SkBitmap colorTransformTable;
Scroggo97c88c22011-05-11 14:05:25 +0000550 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000551 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000552 if (!constantColor) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000553 grPaint->fColorFilterColor = SkColor2GrColor(color);
Scroggod757df22011-05-16 13:11:16 +0000554 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000555 } else {
556 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
rileya@google.com24f3ad12012-07-18 21:47:40 +0000557 grPaint->fColor = SkColor2GrColor(filtered);
senorblanco@chromium.orgb3c20fa2012-01-03 21:20:19 +0000558 grPaint->resetColorFilter();
Scroggod757df22011-05-16 13:11:16 +0000559 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000560 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
561 grPaint->fColorMatrixEnabled = true;
562 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
563 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
twiz@google.com58071162012-07-18 21:41:50 +0000564 } else if (colorFilter != NULL && colorFilter->asComponentTable(
565 &colorTransformTable)) {
566 grPaint->resetColorFilter();
567
568 GrSamplerState* colorSampler = grPaint->textureSampler(kColorFilterTextureIdx);
bsalomon@google.comb8670992012-07-25 21:27:09 +0000569 GrTexture* texture = act->set(dev, colorTransformTable, colorSampler->textureParams());
twiz@google.com58071162012-07-18 21:41:50 +0000570
bsalomon@google.comb8670992012-07-25 21:27:09 +0000571 colorSampler->reset();
bsalomon@google.comcbd0ad92012-07-20 15:09:31 +0000572 colorSampler->setCustomStage(SkNEW_ARGS(GrColorTableEffect, (texture)))->unref();
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000573 } else {
574 grPaint->resetColorFilter();
Scroggo97c88c22011-05-11 14:05:25 +0000575 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000576 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000577}
578
bsalomon@google.com84405e02012-03-05 19:57:21 +0000579// This function is similar to skPaint2GrPaintNoShader but also converts
580// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
581// be used is set on grPaint and returned in param act. constantColor has the
582// same meaning as in skPaint2GrPaintNoShader.
583inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
584 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000585 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000586 SkGpuDevice::SkAutoCachedTexture textures[GrPaint::kMaxTextures],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000587 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000588 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000589 if (NULL == shader) {
twiz@google.com58071162012-07-18 21:41:50 +0000590 return skPaint2GrPaintNoShader(dev,
591 skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000592 false,
593 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000594 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000595 grPaint);
twiz@google.com58071162012-07-18 21:41:50 +0000596 } else if (!skPaint2GrPaintNoShader(dev, skPaint, true, false,
597 &textures[kColorFilterTextureIdx], grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000598 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000599 }
600
rileya@google.com91f319c2012-07-25 17:18:31 +0000601 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
602 GrCustomStage* stage = shader->asNewCustomStage(dev->context(), sampler);
603
604 if (NULL != stage) {
605 sampler->setCustomStage(stage)->unref();
606 SkMatrix localM;
607 if (shader->getLocalMatrix(&localM)) {
608 SkMatrix inverse;
609 if (localM.invert(&inverse)) {
610 sampler->matrix()->preConcat(inverse);
611 }
612 }
613 return true;
614 }
615
reed@google.comac10a2d2010-12-22 21:39:39 +0000616 SkBitmap bitmap;
rileya@google.com91f319c2012-07-25 17:18:31 +0000617 SkMatrix* matrix = sampler->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000618 SkShader::TileMode tileModes[2];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000619 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
rileya@google.com91f319c2012-07-25 17:18:31 +0000620 tileModes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000621
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000622 if (SkShader::kNone_BitmapType == bmptype) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000623 SkShader::GradientInfo info;
624 SkColor color;
625
626 info.fColors = &color;
627 info.fColorOffsets = NULL;
628 info.fColorCount = 1;
629 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
630 SkPaint copy(skPaint);
631 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000632 // modulate the paint alpha by the shader's solid color alpha
633 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
634 copy.setColor(SkColorSetA(color, newA));
twiz@google.com58071162012-07-18 21:41:50 +0000635 return skPaint2GrPaintNoShader(dev,
636 copy,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000637 false,
638 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000639 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000640 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000641 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000642 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000643 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000644
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000645 // Must set wrap and filter on the sampler before requesting a texture.
bsalomon@google.comb8670992012-07-25 21:27:09 +0000646 sampler->textureParams()->reset(tileModes, skPaint.isFilterBitmap());
647 GrTexture* texture = textures[kShaderTextureIdx].set(dev, bitmap, sampler->textureParams());
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000648
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000649 if (NULL == texture) {
650 SkDebugf("Couldn't convert bitmap to texture.\n");
651 return false;
652 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000653
rileya@google.com91f319c2012-07-25 17:18:31 +0000654 sampler->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000655
reed@google.comac10a2d2010-12-22 21:39:39 +0000656 // since our texture coords will be in local space, we wack the texture
657 // matrix to map them back into 0...1 before we load it
658 SkMatrix localM;
659 if (shader->getLocalMatrix(&localM)) {
660 SkMatrix inverse;
661 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000662 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000663 }
664 }
665 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000666 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
667 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000668 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000669 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000670
671 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000672}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000673}
reed@google.comac10a2d2010-12-22 21:39:39 +0000674
675///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000676void SkGpuDevice::clear(SkColor color) {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000677 fContext->clear(NULL, color, fRenderTarget);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000678}
679
reed@google.comac10a2d2010-12-22 21:39:39 +0000680void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
681 CHECK_SHOULD_DRAW(draw);
682
bsalomon@google.com5782d712011-01-21 21:03:59 +0000683 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000684 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000685 if (!skPaint2GrPaintShader(this,
686 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000687 true,
twiz@google.com58071162012-07-18 21:41:50 +0000688 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000689 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000690 return;
691 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000692
693 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000694}
695
696// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000697static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000698 kPoints_GrPrimitiveType,
699 kLines_GrPrimitiveType,
700 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000701};
702
703void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000704 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000705 CHECK_SHOULD_DRAW(draw);
706
707 SkScalar width = paint.getStrokeWidth();
708 if (width < 0) {
709 return;
710 }
711
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000712 // we only handle hairlines and paints without path effects or mask filters,
713 // else we let the SkDraw call our drawPath()
714 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000715 draw.drawPoints(mode, count, pts, paint, true);
716 return;
717 }
718
bsalomon@google.com5782d712011-01-21 21:03:59 +0000719 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000720 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000721 if (!skPaint2GrPaintShader(this,
722 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000723 true,
twiz@google.com58071162012-07-18 21:41:50 +0000724 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000725 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000726 return;
727 }
728
bsalomon@google.com5782d712011-01-21 21:03:59 +0000729 fContext->drawVertices(grPaint,
730 gPointMode2PrimtiveType[mode],
731 count,
732 (GrPoint*)pts,
733 NULL,
734 NULL,
735 NULL,
736 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000737}
738
reed@google.comc9aa5872011-04-05 21:05:37 +0000739///////////////////////////////////////////////////////////////////////////////
740
reed@google.comac10a2d2010-12-22 21:39:39 +0000741void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
742 const SkPaint& paint) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000743 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000744 CHECK_SHOULD_DRAW(draw);
745
bungeman@google.com79bd8772011-07-18 15:34:08 +0000746 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000747 SkScalar width = paint.getStrokeWidth();
748
749 /*
750 We have special code for hairline strokes, miter-strokes, and fills.
751 Anything else we just call our path code.
752 */
753 bool usePath = doStroke && width > 0 &&
754 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000755 // another two reasons we might need to call drawPath...
756 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000757 usePath = true;
758 }
reed@google.com67db6642011-05-26 11:46:35 +0000759 // until we aa rotated rects...
760 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
761 usePath = true;
762 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000763 // small miter limit means right angles show bevel...
764 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
765 paint.getStrokeMiter() < SK_ScalarSqrt2)
766 {
767 usePath = true;
768 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000769 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000770 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
771 usePath = true;
772 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000773
774 if (usePath) {
775 SkPath path;
776 path.addRect(rect);
777 this->drawPath(draw, path, paint, NULL, true);
778 return;
779 }
780
781 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000782 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000783 if (!skPaint2GrPaintShader(this,
784 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000785 true,
twiz@google.com58071162012-07-18 21:41:50 +0000786 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000787 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000788 return;
789 }
reed@google.com20efde72011-05-09 17:00:02 +0000790 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000791}
792
reed@google.com69302852011-02-16 18:08:07 +0000793#include "SkMaskFilter.h"
794#include "SkBounder.h"
795
bsalomon@google.com85003222012-03-28 14:44:37 +0000796///////////////////////////////////////////////////////////////////////////////
797
798// helpers for applying mask filters
799namespace {
800
801GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000802 switch (fillType) {
803 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000804 return kWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000805 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000806 return kEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000807 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000808 return kInverseWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000809 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000810 return kInverseEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000811 default:
812 SkDebugf("Unsupported path fill type\n");
bsalomon@google.com47059542012-06-06 20:51:20 +0000813 return kHairLine_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000814 }
815}
816
bsalomon@google.com85003222012-03-28 14:44:37 +0000817// We prefer to blur small rect with small radius via CPU.
818#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
819#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
820inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
821 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
822 rect.height() <= MIN_GPU_BLUR_SIZE &&
823 radius <= MIN_GPU_BLUR_RADIUS) {
824 return true;
825 }
826 return false;
827}
828
829bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
830 SkMaskFilter* filter, const SkMatrix& matrix,
831 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000832 GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000833#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000834 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000835#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000836 SkMaskFilter::BlurInfo info;
837 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000838 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000839 return false;
840 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000841 SkScalar radius = info.fIgnoreTransform ? info.fRadius
842 : matrix.mapRadius(info.fRadius);
843 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000844 if (radius <= 0) {
845 return false;
846 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000847
848 SkRect srcRect = path.getBounds();
849 if (shouldDrawBlurWithCPU(srcRect, radius)) {
850 return false;
851 }
852
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000853 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000854 float sigma3 = sigma * 3.0f;
855
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000856 SkRect clipRect;
857 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000858
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000859 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000860 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
861 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000862 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000863 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000864 SkIRect finalIRect;
865 finalRect.roundOut(&finalIRect);
866 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000867 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000868 }
869 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000870 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000871 }
872 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000873 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000874 GrTextureDesc desc;
875 desc.fFlags = kRenderTarget_GrTextureFlagBit;
876 desc.fWidth = SkScalarCeilToInt(srcRect.width());
877 desc.fHeight = SkScalarCeilToInt(srcRect.height());
878 // We actually only need A8, but it often isn't supported as a
879 // render target so default to RGBA_8888
880 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000881
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000882 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
883 desc.fConfig = kAlpha_8_GrPixelConfig;
884 }
885
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000886 GrAutoScratchTexture pathEntry(context, desc);
887 GrTexture* pathTexture = pathEntry.texture();
888 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000889 return false;
890 }
891 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000892 // Once this code moves into GrContext, this should be changed to use
893 // an AutoClipRestore.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000894 const GrClipData* oldClipData = context->getClip();
895
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000896 context->setRenderTarget(pathTexture->asRenderTarget());
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000897
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000898 GrClip newClipStack(srcRect);
899 GrClipData newClipData;
900 newClipData.fClipStack = &newClipStack;
901 context->setClip(&newClipData);
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000902
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000903 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000904 GrPaint tempPaint;
905 tempPaint.reset();
906
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000907 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000908 tempPaint.fAntiAlias = grp->fAntiAlias;
909 if (tempPaint.fAntiAlias) {
910 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
911 // blend coeff of zero requires dual source blending support in order
912 // to properly blend partially covered pixels. This means the AA
913 // code path may not be taken. So we use a dst blend coeff of ISA. We
914 // could special case AA draws to a dst surface with known alpha=0 to
915 // use a zero dst coeff when dual source blending isn't available.
bsalomon@google.com47059542012-06-06 20:51:20 +0000916 tempPaint.fSrcBlendCoeff = kOne_GrBlendCoeff;
917 tempPaint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000918 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000919 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000920 context->drawPath(tempPaint, path, pathFillType, &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000921
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000922 // If we're doing a normal blur, we can clobber the pathTexture in the
923 // gaussianBlur. Otherwise, we need to save it for later compositing.
924 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +0000925 SkAutoTUnref<GrTexture> blurTexture(context->gaussianBlur(
926 pathTexture, isNormalBlur, srcRect, sigma, sigma));
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000927
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000928 if (!isNormalBlur) {
929 GrPaint paint;
930 paint.reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +0000931 paint.textureSampler(0)->textureParams()->setClampNoFilter();
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000932 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
933 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000934 // Blend pathTexture over blurTexture.
935 context->setRenderTarget(blurTexture->asRenderTarget());
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000936 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS
937 (GrSingleTextureEffect, (pathTexture)))->unref();
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000938 if (SkMaskFilter::kInner_BlurType == blurType) {
939 // inner: dst = dst * src
bsalomon@google.com47059542012-06-06 20:51:20 +0000940 paint.fSrcBlendCoeff = kDC_GrBlendCoeff;
941 paint.fDstBlendCoeff = kZero_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000942 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
943 // solid: dst = src + dst - src * dst
944 // = (1 - dst) * src + 1 * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000945 paint.fSrcBlendCoeff = kIDC_GrBlendCoeff;
946 paint.fDstBlendCoeff = kOne_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000947 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
948 // outer: dst = dst * (1 - src)
949 // = 0 * src + (1 - src) * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000950 paint.fSrcBlendCoeff = kZero_GrBlendCoeff;
951 paint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000952 }
953 context->drawRect(paint, srcRect);
954 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000955 context->setRenderTarget(oldRenderTarget);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000956 context->setClip(oldClipData);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000957
bsalomon@google.come3d32162012-07-20 13:37:06 +0000958 if (!grp->preConcatSamplerMatricesWithInverse(matrix)) {
959 return false;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000960 }
961
962 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
963 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000964 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
bsalomon@google.com97912912011-12-06 16:30:36 +0000965 grp->maskSampler(MASK_IDX)->reset();
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000966 grp->maskSampler(MASK_IDX)->setCustomStage(
967 SkNEW_ARGS(GrSingleTextureEffect, (blurTexture)))->unref();
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000968 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
969 -finalRect.fTop);
970 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
971 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000972 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000973 return true;
974}
975
bsalomon@google.com85003222012-03-28 14:44:37 +0000976bool drawWithMaskFilter(GrContext* context, const SkPath& path,
977 SkMaskFilter* filter, const SkMatrix& matrix,
978 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000979 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000980 SkMask srcM, dstM;
981
982 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000983 SkMask::kComputeBoundsAndRenderImage_CreateMode,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000984 style)) {
reed@google.com69302852011-02-16 18:08:07 +0000985 return false;
986 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000987 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000988
989 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
990 return false;
991 }
992 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000993 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000994
995 if (clip.quickReject(dstM.fBounds)) {
996 return false;
997 }
998 if (bounder && !bounder->doIRect(dstM.fBounds)) {
999 return false;
1000 }
1001
1002 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
1003 // the current clip (and identity matrix) and grpaint settings
1004
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001005 GrContext::AutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +00001006
bsalomon@google.come3d32162012-07-20 13:37:06 +00001007 if (!grp->preConcatSamplerMatricesWithInverse(matrix)) {
1008 return false;
1009 }
1010
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001011 GrTextureDesc desc;
1012 desc.fWidth = dstM.fBounds.width();
1013 desc.fHeight = dstM.fBounds.height();
1014 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +00001015
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001016 GrAutoScratchTexture ast(context, desc);
1017 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001018
reed@google.com69302852011-02-16 18:08:07 +00001019 if (NULL == texture) {
1020 return false;
1021 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001022 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001023 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +00001024
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001025 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
1026 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001027 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
bsalomon@google.com97912912011-12-06 16:30:36 +00001028 grp->maskSampler(MASK_IDX)->reset();
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001029 grp->maskSampler(MASK_IDX)->setCustomStage(
1030 SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001031 GrRect d;
1032 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +00001033 GrIntToScalar(dstM.fBounds.fTop),
1034 GrIntToScalar(dstM.fBounds.fRight),
1035 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001036
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001037 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
1038 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
1039 -dstM.fBounds.fTop*SK_Scalar1);
1040 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001041 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +00001042 return true;
1043}
reed@google.com69302852011-02-16 18:08:07 +00001044
bsalomon@google.com85003222012-03-28 14:44:37 +00001045}
1046
1047///////////////////////////////////////////////////////////////////////////////
1048
reed@google.com0c219b62011-02-16 21:31:18 +00001049void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001050 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +00001051 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +00001052 CHECK_FOR_NODRAW_ANNOTATION(paint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001053 CHECK_SHOULD_DRAW(draw);
1054
reed@google.comfe626382011-09-21 13:50:35 +00001055 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001056
bsalomon@google.com5782d712011-01-21 21:03:59 +00001057 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001058 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001059 if (!skPaint2GrPaintShader(this,
1060 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001061 true,
twiz@google.com58071162012-07-18 21:41:50 +00001062 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001063 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001064 return;
1065 }
1066
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +00001067 // can we cheat, and threat a thin stroke as a hairline w/ coverage
1068 // if we can, we draw lots faster (raster device does this same test)
1069 SkScalar hairlineCoverage;
1070 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
1071 doFill = false;
1072 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
1073 grPaint.fCoverage);
1074 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001075
reed@google.comfe626382011-09-21 13:50:35 +00001076 // If we have a prematrix, apply it to the path, optimizing for the case
1077 // where the original path can in fact be modified in place (even though
1078 // its parameter type is const).
1079 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1080 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001081
1082 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001083 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001084
reed@google.come3445642011-02-16 23:20:39 +00001085 if (!pathIsMutable) {
1086 result = &tmpPath;
1087 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001088 }
reed@google.come3445642011-02-16 23:20:39 +00001089 // should I push prePathMatrix on our MV stack temporarily, instead
1090 // of applying it here? See SkDraw.cpp
1091 pathPtr->transform(*prePathMatrix, result);
1092 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001093 }
reed@google.com0c219b62011-02-16 21:31:18 +00001094 // at this point we're done with prePathMatrix
1095 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001096
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001097 if (paint.getPathEffect() ||
1098 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001099 // it is safe to use tmpPath here, even if we already used it for the
1100 // prepathmatrix, since getFillPath can take the same object for its
1101 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001102 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001103 pathPtr = &tmpPath;
1104 }
1105
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001106 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001107 // avoid possibly allocating a new path in transform if we can
1108 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1109
1110 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001111 pathPtr->transform(*draw.fMatrix, devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001112 GrPathFill pathFillType = doFill ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001113 skToGrFillType(devPathPtr->getFillType()) : kHairLine_GrPathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001114 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001115 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001116 &grPaint, pathFillType)) {
1117 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
1118 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001119 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001120 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001121 &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001122 }
reed@google.com69302852011-02-16 18:08:07 +00001123 return;
1124 }
reed@google.com69302852011-02-16 18:08:07 +00001125
bsalomon@google.com47059542012-06-06 20:51:20 +00001126 GrPathFill fill = kHairLine_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001127
reed@google.com0c219b62011-02-16 21:31:18 +00001128 if (doFill) {
1129 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001130 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001131 fill = kWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001132 break;
1133 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001134 fill = kEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001135 break;
1136 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001137 fill = kInverseWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001138 break;
1139 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001140 fill = kInverseEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001141 break;
1142 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001143 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001144 return;
1145 }
1146 }
1147
reed@google.com07f3ee12011-05-16 17:21:57 +00001148 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001149}
1150
bsalomon@google.comfb309512011-11-30 14:13:48 +00001151namespace {
1152
1153inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1154 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1155 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1156 return tilesX * tilesY;
1157}
1158
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001159inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001160 const SkIRect* srcRectPtr,
1161 int maxTextureSize) {
1162 static const int kSmallTileSize = 1 << 10;
1163 if (maxTextureSize <= kSmallTileSize) {
1164 return maxTextureSize;
1165 }
1166
1167 size_t maxTexTotalTileSize;
1168 size_t smallTotalTileSize;
1169
1170 if (NULL == srcRectPtr) {
1171 int w = bitmap.width();
1172 int h = bitmap.height();
1173 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1174 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1175 } else {
1176 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1177 srcRectPtr->fTop,
1178 srcRectPtr->fRight,
1179 srcRectPtr->fBottom,
1180 maxTextureSize);
1181 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1182 srcRectPtr->fTop,
1183 srcRectPtr->fRight,
1184 srcRectPtr->fBottom,
1185 kSmallTileSize);
1186 }
1187 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1188 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1189
1190 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1191 return kSmallTileSize;
1192 } else {
1193 return maxTextureSize;
1194 }
1195}
1196}
1197
1198bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001199 const GrTextureParams& params,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001200 const SkIRect* srcRectPtr,
1201 int* tileSize) const {
1202 SkASSERT(NULL != tileSize);
1203
1204 // if bitmap is explictly texture backed then just use the texture
1205 if (NULL != bitmap.getTexture()) {
1206 return false;
1207 }
1208 // if it's larger than the max texture size, then we have no choice but
1209 // tiling
1210 const int maxTextureSize = fContext->getMaxTextureSize();
1211 if (bitmap.width() > maxTextureSize ||
1212 bitmap.height() > maxTextureSize) {
1213 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1214 return true;
1215 }
1216 // if we are going to have to draw the whole thing, then don't tile
1217 if (NULL == srcRectPtr) {
1218 return false;
1219 }
1220 // if the entire texture is already in our cache then no reason to tile it
bsalomon@google.comb8670992012-07-25 21:27:09 +00001221 if (this->isBitmapInTextureCache(bitmap, params)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001222 return false;
1223 }
1224
1225 // At this point we know we could do the draw by uploading the entire bitmap
1226 // as a texture. However, if the texture would be large compared to the
1227 // cache size and we don't require most of it for this draw then tile to
1228 // reduce the amount of upload and cache spill.
1229
1230 // assumption here is that sw bitmap size is a good proxy for its size as
1231 // a texture
1232 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001233 size_t cacheSize;
1234 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001235 if (bmpSize < cacheSize / 2) {
1236 return false;
1237 }
1238
1239 SkFixed fracUsed =
1240 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1241 (srcRectPtr->height() << 16) / bitmap.height());
1242 if (fracUsed <= SK_FixedHalf) {
1243 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1244 return true;
1245 } else {
1246 return false;
1247 }
1248}
1249
reed@google.comac10a2d2010-12-22 21:39:39 +00001250void SkGpuDevice::drawBitmap(const SkDraw& draw,
1251 const SkBitmap& bitmap,
1252 const SkIRect* srcRectPtr,
1253 const SkMatrix& m,
1254 const SkPaint& paint) {
1255 CHECK_SHOULD_DRAW(draw);
1256
1257 SkIRect srcRect;
1258 if (NULL == srcRectPtr) {
1259 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1260 } else {
1261 srcRect = *srcRectPtr;
1262 }
1263
junov@google.comd935cfb2011-06-27 20:48:23 +00001264 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001265 // Convert the bitmap to a shader so that the rect can be drawn
1266 // through drawRect, which supports mask filters.
1267 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001268 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001269 if (srcRectPtr) {
1270 if (!bitmap.extractSubset(&tmp, srcRect)) {
1271 return; // extraction failed
1272 }
1273 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001274 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001275 }
1276 SkPaint paintWithTexture(paint);
1277 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1278 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001279 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001280 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001281
junov@google.com1d329782011-07-28 20:10:09 +00001282 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001283 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001284 // also affect the behavior of the mask filter.
1285 SkMatrix drawMatrix;
1286 drawMatrix.setConcat(*draw.fMatrix, m);
1287 SkDraw transformedDraw(draw);
1288 transformedDraw.fMatrix = &drawMatrix;
1289
1290 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1291
junov@google.comd935cfb2011-06-27 20:48:23 +00001292 return;
1293 }
1294
bsalomon@google.com5782d712011-01-21 21:03:59 +00001295 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001296 SkAutoCachedTexture colorLutTexture;
1297 if (!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001298 return;
1299 }
bsalomon@google.comb8670992012-07-25 21:27:09 +00001300 GrTextureParams* params = grPaint.textureSampler(kBitmapTextureIdx)->textureParams();
1301 params->setBilerp(paint.isFilterBitmap());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001302
bsalomon@google.comfb309512011-11-30 14:13:48 +00001303 int tileSize;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001304 if (!this->shouldTileBitmap(bitmap, *params, srcRectPtr, &tileSize)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001305 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001306 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001307 return;
1308 }
1309
1310 // undo the translate done by SkCanvas
1311 int DX = SkMax32(0, srcRect.fLeft);
1312 int DY = SkMax32(0, srcRect.fTop);
1313 // compute clip bounds in local coordinates
1314 SkIRect clipRect;
1315 {
1316 SkRect r;
1317 r.set(draw.fClip->getBounds());
1318 SkMatrix matrix, inverse;
1319 matrix.setConcat(*draw.fMatrix, m);
1320 if (!matrix.invert(&inverse)) {
1321 return;
1322 }
1323 inverse.mapRect(&r);
1324 r.roundOut(&clipRect);
1325 // apply the canvas' translate to our local clip
1326 clipRect.offset(DX, DY);
1327 }
1328
bsalomon@google.comfb309512011-11-30 14:13:48 +00001329 int nx = bitmap.width() / tileSize;
1330 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001331 for (int x = 0; x <= nx; x++) {
1332 for (int y = 0; y <= ny; y++) {
1333 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001334 tileR.set(x * tileSize, y * tileSize,
1335 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001336 if (!SkIRect::Intersects(tileR, clipRect)) {
1337 continue;
1338 }
1339
1340 SkIRect srcR = tileR;
1341 if (!srcR.intersect(srcRect)) {
1342 continue;
1343 }
1344
1345 SkBitmap tmpB;
1346 if (bitmap.extractSubset(&tmpB, tileR)) {
1347 // now offset it to make it "local" to our tmp bitmap
1348 srcR.offset(-tileR.fLeft, -tileR.fTop);
1349
1350 SkMatrix tmpM(m);
1351 {
1352 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1353 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1354 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1355 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001356 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001357 }
1358 }
1359 }
1360}
1361
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001362namespace {
1363
1364bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1365 // detect pixel disalignment
1366 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1367 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1368 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1369 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1370 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1371 COLOR_BLEED_TOLERANCE &&
1372 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1373 COLOR_BLEED_TOLERANCE) {
1374 return true;
1375 }
1376 return false;
1377}
1378
1379bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1380 const SkMatrix& m) {
1381 // Only gets called if hasAlignedSamples returned false.
1382 // So we can assume that sampling is axis aligned but not texel aligned.
1383 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
1384 SkRect innerSrcRect(srcRect), innerTransformedRect,
1385 outerTransformedRect(transformedRect);
1386 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1387 m.mapRect(&innerTransformedRect, innerSrcRect);
1388
1389 // The gap between outerTransformedRect and innerTransformedRect
1390 // represents the projection of the source border area, which is
1391 // problematic for color bleeding. We must check whether any
1392 // destination pixels sample the border area.
1393 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1394 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1395 SkIRect outer, inner;
1396 outerTransformedRect.round(&outer);
1397 innerTransformedRect.round(&inner);
1398 // If the inner and outer rects round to the same result, it means the
1399 // border does not overlap any pixel centers. Yay!
1400 return inner != outer;
1401}
1402
1403} // unnamed namespace
1404
reed@google.comac10a2d2010-12-22 21:39:39 +00001405/*
1406 * This is called by drawBitmap(), which has to handle images that may be too
1407 * large to be represented by a single texture.
1408 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001409 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1410 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001411 */
1412void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1413 const SkBitmap& bitmap,
1414 const SkIRect& srcRect,
1415 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001416 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001417 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1418 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001419
reed@google.com9c49bc32011-07-07 13:42:37 +00001420 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001421 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001422 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001423 return;
1424 }
1425
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001426 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001427
bsalomon@google.comb8670992012-07-25 21:27:09 +00001428 sampler->textureParams()->setClamp();
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001429 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001430
1431 GrTexture* texture;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001432 SkAutoCachedTexture act(this, bitmap, sampler->textureParams(), &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001433 if (NULL == texture) {
1434 return;
1435 }
1436
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001437 grPaint->textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1438 (GrSingleTextureEffect, (texture)))->unref();
reed@google.com46799cd2011-02-22 20:56:26 +00001439
reed@google.com20efde72011-05-09 17:00:02 +00001440 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1441 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001442 GrRect paintRect;
bsalomon@google.com91832162012-03-08 19:53:02 +00001443 float wInv = 1.f / bitmap.width();
1444 float hInv = 1.f / bitmap.height();
1445 paintRect.setLTRB(SkFloatToScalar(srcRect.fLeft * wInv),
1446 SkFloatToScalar(srcRect.fTop * hInv),
1447 SkFloatToScalar(srcRect.fRight * wInv),
1448 SkFloatToScalar(srcRect.fBottom * hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001449
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001450 bool needsTextureDomain = false;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001451 if (sampler->textureParams()->isBilerp()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001452 // Need texture domain if drawing a sub rect.
bsalomon@google.comb8670992012-07-25 21:27:09 +00001453 needsTextureDomain = srcRect.width() < bitmap.width() || srcRect.height() < bitmap.height();
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001454 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1455 // sampling is axis-aligned
1456 GrRect floatSrcRect, transformedRect;
1457 floatSrcRect.set(srcRect);
1458 SkMatrix srcToDeviceMatrix(m);
1459 srcToDeviceMatrix.postConcat(*draw.fMatrix);
1460 srcToDeviceMatrix.mapRect(&transformedRect, floatSrcRect);
1461
1462 if (hasAlignedSamples(floatSrcRect, transformedRect)) {
1463 // Samples are texel-aligned, so filtering is futile
bsalomon@google.comb8670992012-07-25 21:27:09 +00001464 sampler->textureParams()->setBilerp(false);
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001465 needsTextureDomain = false;
1466 } else {
1467 needsTextureDomain = needsTextureDomain &&
1468 mayColorBleed(floatSrcRect, transformedRect, m);
1469 }
1470 }
1471 }
1472
1473 GrRect textureDomain = GrRect::MakeEmpty();
1474
1475 if (needsTextureDomain) {
1476 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001477 GrScalar left, top, right, bottom;
1478 if (srcRect.width() > 1) {
1479 GrScalar border = GR_ScalarHalf / bitmap.width();
1480 left = paintRect.left() + border;
1481 right = paintRect.right() - border;
1482 } else {
1483 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1484 }
1485 if (srcRect.height() > 1) {
1486 GrScalar border = GR_ScalarHalf / bitmap.height();
1487 top = paintRect.top() + border;
1488 bottom = paintRect.bottom() - border;
1489 } else {
1490 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1491 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001492 textureDomain.setLTRB(left, top, right, bottom);
tomhudson@google.com2f68e762012-07-17 18:43:21 +00001493 sampler->setCustomStage(SkNEW_ARGS(GrTextureDomainEffect,
1494 (texture,
1495 textureDomain)))->unref();
junov@google.com6acc9b32011-05-16 18:32:07 +00001496 }
1497
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001498 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001499}
1500
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001501namespace {
1502
1503void apply_custom_stage(GrContext* context,
1504 GrTexture* srcTexture,
1505 GrTexture* dstTexture,
1506 const GrRect& rect,
1507 GrCustomStage* stage) {
1508 SkASSERT(srcTexture && srcTexture->getContext() == context);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001509 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001510 GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget());
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001511 GrContext::AutoClip acs(context, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001512
1513 GrMatrix sampleM;
1514 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
1515 GrPaint paint;
1516 paint.reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +00001517 paint.textureSampler(0)->textureParams()->setBilerp(true);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001518 paint.textureSampler(0)->reset(sampleM);
1519 paint.textureSampler(0)->setCustomStage(stage);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001520 context->drawRect(paint, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001521}
1522
1523};
1524
reed@google.com8926b162012-03-23 15:36:36 +00001525static GrTexture* filter_texture(GrContext* context, GrTexture* texture,
1526 SkImageFilter* filter, const GrRect& rect) {
1527 GrAssert(filter);
1528
1529 SkSize blurSize;
1530 SkISize radius;
1531
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001532 GrTextureDesc desc;
1533 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1534 desc.fWidth = SkScalarCeilToInt(rect.width());
1535 desc.fHeight = SkScalarCeilToInt(rect.height());
1536 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001537 GrCustomStage* stage;
reed@google.com8926b162012-03-23 15:36:36 +00001538
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001539 if (filter->asNewCustomStage(&stage, texture)) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001540 GrAutoScratchTexture dst(context, desc);
1541 apply_custom_stage(context, texture, dst.texture(), rect, stage);
1542 texture = dst.detach();
1543 stage->unref();
1544 } else if (filter->asABlur(&blurSize)) {
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001545 texture = context->gaussianBlur(texture, false, rect,
reed@google.com8926b162012-03-23 15:36:36 +00001546 blurSize.width(),
1547 blurSize.height());
reed@google.com8926b162012-03-23 15:36:36 +00001548 } else if (filter->asADilate(&radius)) {
reed@google.com8926b162012-03-23 15:36:36 +00001549 texture = context->applyMorphology(texture, rect,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001550 GrContext::kDilate_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001551 radius);
reed@google.com8926b162012-03-23 15:36:36 +00001552 } else if (filter->asAnErode(&radius)) {
reed@google.com8926b162012-03-23 15:36:36 +00001553 texture = context->applyMorphology(texture, rect,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001554 GrContext::kErode_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001555 radius);
reed@google.com8926b162012-03-23 15:36:36 +00001556 }
1557 return texture;
1558}
1559
reed@google.comac10a2d2010-12-22 21:39:39 +00001560void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1561 int left, int top, const SkPaint& paint) {
1562 CHECK_SHOULD_DRAW(draw);
1563
reed@google.com8926b162012-03-23 15:36:36 +00001564 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001565 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1566 return;
1567 }
1568
reed@google.com76dd2772012-01-05 21:15:07 +00001569 int w = bitmap.width();
1570 int h = bitmap.height();
1571
bsalomon@google.com5782d712011-01-21 21:03:59 +00001572 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001573 SkAutoCachedTexture colorLutTexture;
1574 if(!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001575 return;
1576 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001577
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001578 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001579
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001580 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001581
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001582 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001583 sampler->reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +00001584 SkAutoCachedTexture act(this, bitmap, sampler->textureParams(), &texture);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001585 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1586 (GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001587
reed@google.com8926b162012-03-23 15:36:36 +00001588 SkImageFilter* filter = paint.getImageFilter();
1589 if (NULL != filter) {
1590 GrTexture* filteredTexture = filter_texture(fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001591 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001592 if (filteredTexture) {
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001593 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1594 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001595 texture = filteredTexture;
1596 filteredTexture->unref();
1597 }
reed@google.com76dd2772012-01-05 21:15:07 +00001598 }
reed@google.com8926b162012-03-23 15:36:36 +00001599
bsalomon@google.com5782d712011-01-21 21:03:59 +00001600 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001601 GrRect::MakeXYWH(GrIntToScalar(left),
1602 GrIntToScalar(top),
1603 GrIntToScalar(w),
1604 GrIntToScalar(h)),
1605 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1606 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001607}
1608
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001609void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
reed@google.comac10a2d2010-12-22 21:39:39 +00001610 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001611 // clear of the source device must occur before CHECK_SHOULD_DRAW
1612 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1613 if (dev->fNeedClear) {
1614 // TODO: could check here whether we really need to draw at all
1615 dev->clear(0x0);
1616 }
1617
reed@google.comac10a2d2010-12-22 21:39:39 +00001618 CHECK_SHOULD_DRAW(draw);
1619
bsalomon@google.com5782d712011-01-21 21:03:59 +00001620 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001621 SkAutoCachedTexture colorLutTexture;
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001622 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001623 if (!dev->bindDeviceAsTexture(&grPaint) ||
twiz@google.com58071162012-07-18 21:41:50 +00001624 !skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001625 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001626 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001627
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001628 GrTexture* devTex = grPaint.getTextureSampler(kBitmapTextureIdx).getCustomStage()->texture(0);
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001629 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001630
reed@google.com8926b162012-03-23 15:36:36 +00001631 SkImageFilter* filter = paint.getImageFilter();
1632 if (NULL != filter) {
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001633 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001634 SkIntToScalar(devTex->height()));
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001635 GrTexture* filteredTexture = filter_texture(fContext, devTex, filter, rect);
reed@google.com8926b162012-03-23 15:36:36 +00001636 if (filteredTexture) {
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001637 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1638 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001639 devTex = filteredTexture;
1640 filteredTexture->unref();
1641 }
1642 }
1643
bsalomon@google.com5782d712011-01-21 21:03:59 +00001644 const SkBitmap& bm = dev->accessBitmap(false);
1645 int w = bm.width();
1646 int h = bm.height();
1647
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001648 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001649 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1650 GrIntToScalar(y),
1651 GrIntToScalar(w),
1652 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001653
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001654 // The device being drawn may not fill up its texture (saveLayer uses
1655 // the approximate ).
1656 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1657 GR_Scalar1 * h / devTex->height());
1658
1659 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001660}
1661
reed@google.com8926b162012-03-23 15:36:36 +00001662bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
reed@google.com76dd2772012-01-05 21:15:07 +00001663 SkSize size;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001664 SkISize radius;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001665
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001666 if (!filter->asNewCustomStage(NULL, NULL) &&
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001667 !filter->asABlur(&size) &&
1668 !filter->asADilate(&radius) &&
1669 !filter->asAnErode(&radius)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001670 return false;
1671 }
reed@google.com8926b162012-03-23 15:36:36 +00001672 return true;
1673}
1674
1675bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1676 const SkMatrix& ctm,
1677 SkBitmap* result, SkIPoint* offset) {
1678 // want explicitly our impl, so guard against a subclass of us overriding it
1679 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001680 return false;
1681 }
reed@google.com8926b162012-03-23 15:36:36 +00001682
1683 SkAutoLockPixels alp(src, !src.getTexture());
1684 if (!src.getTexture() && !src.readyToDraw()) {
1685 return false;
1686 }
1687
1688 GrPaint paint;
1689 paint.reset();
1690
1691 GrSamplerState* sampler = paint.textureSampler(kBitmapTextureIdx);
1692
1693 GrTexture* texture;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001694 SkAutoCachedTexture act(this, src, sampler->textureParams(), &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001695
1696 result->setConfig(src.config(), src.width(), src.height());
robertphillips@google.com8637a362012-04-10 18:32:35 +00001697 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
1698 SkIntToScalar(src.height()));
reed@google.com8926b162012-03-23 15:36:36 +00001699 GrTexture* resultTexture = filter_texture(fContext, texture, filter, rect);
1700 if (resultTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001701 result->setPixelRef(SkNEW_ARGS(SkGrTexturePixelRef,
1702 (resultTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001703 resultTexture->unref();
1704 }
reed@google.com76dd2772012-01-05 21:15:07 +00001705 return true;
1706}
1707
reed@google.comac10a2d2010-12-22 21:39:39 +00001708///////////////////////////////////////////////////////////////////////////////
1709
1710// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001711static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001712 kTriangles_GrPrimitiveType,
1713 kTriangleStrip_GrPrimitiveType,
1714 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001715};
1716
1717void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1718 int vertexCount, const SkPoint vertices[],
1719 const SkPoint texs[], const SkColor colors[],
1720 SkXfermode* xmode,
1721 const uint16_t indices[], int indexCount,
1722 const SkPaint& paint) {
1723 CHECK_SHOULD_DRAW(draw);
1724
bsalomon@google.com5782d712011-01-21 21:03:59 +00001725 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001726 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com5782d712011-01-21 21:03:59 +00001727 // we ignore the shader if texs is null.
1728 if (NULL == texs) {
twiz@google.com58071162012-07-18 21:41:50 +00001729 if (!skPaint2GrPaintNoShader(this,
1730 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001731 false,
1732 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001733 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +00001734 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001735 return;
1736 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001737 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001738 if (!skPaint2GrPaintShader(this,
1739 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001740 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001741 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001742 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001743 return;
1744 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001745 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001746
1747 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001748 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001749 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1750#if 0
1751 return
1752#endif
1753 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001754 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001755
bsalomon@google.com498776a2011-08-16 19:20:44 +00001756 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1757 if (NULL != colors) {
1758 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001759 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001760 for (int i = 0; i < vertexCount; ++i) {
rileya@google.com24f3ad12012-07-18 21:47:40 +00001761 convertedColors[i] = SkColor2GrColor(colors[i]);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001762 }
1763 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001764 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001765 fContext->drawVertices(grPaint,
1766 gVertexMode2PrimitiveType[vmode],
1767 vertexCount,
1768 (GrPoint*) vertices,
1769 (GrPoint*) texs,
1770 colors,
1771 indices,
1772 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001773}
1774
1775///////////////////////////////////////////////////////////////////////////////
1776
1777static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001778 GrFontScaler* scaler = (GrFontScaler*)data;
1779 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001780}
1781
1782static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1783 void* auxData;
1784 GrFontScaler* scaler = NULL;
1785 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1786 scaler = (GrFontScaler*)auxData;
1787 }
1788 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001789 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001790 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1791 }
1792 return scaler;
1793}
1794
1795static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1796 SkFixed fx, SkFixed fy,
1797 const SkGlyph& glyph) {
1798 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1799
bungeman@google.com15865a72012-01-11 16:28:04 +00001800 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001801
1802 if (NULL == procs->fFontScaler) {
1803 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1804 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001805
bungeman@google.com15865a72012-01-11 16:28:04 +00001806 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1807 glyph.getSubXFixed(),
1808 glyph.getSubYFixed()),
1809 SkFixedFloorToFixed(fx),
1810 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001811 procs->fFontScaler);
1812}
1813
bsalomon@google.com5782d712011-01-21 21:03:59 +00001814SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001815
1816 // deferred allocation
1817 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001818 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001819 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1820 fDrawProcs->fContext = fContext;
1821 }
1822
1823 // init our (and GL's) state
1824 fDrawProcs->fTextContext = context;
1825 fDrawProcs->fFontScaler = NULL;
1826 return fDrawProcs;
1827}
1828
1829void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1830 size_t byteLength, SkScalar x, SkScalar y,
1831 const SkPaint& paint) {
1832 CHECK_SHOULD_DRAW(draw);
1833
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001834 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001835 // this guy will just call our drawPath()
1836 draw.drawText((const char*)text, byteLength, x, y, paint);
1837 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001838 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001839
1840 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001841 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001842 if (!skPaint2GrPaintShader(this,
1843 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001844 true,
twiz@google.com58071162012-07-18 21:41:50 +00001845 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001846 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001847 return;
1848 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001849 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1850 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001851 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1852 }
1853}
1854
1855void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1856 size_t byteLength, const SkScalar pos[],
1857 SkScalar constY, int scalarsPerPos,
1858 const SkPaint& paint) {
1859 CHECK_SHOULD_DRAW(draw);
1860
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001861 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001862 // this guy will just call our drawPath()
1863 draw.drawPosText((const char*)text, byteLength, pos, constY,
1864 scalarsPerPos, paint);
1865 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001866 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001867
1868 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001869 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001870 if (!skPaint2GrPaintShader(this,
1871 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001872 true,
twiz@google.com58071162012-07-18 21:41:50 +00001873 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001874 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001875 return;
1876 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001877 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1878 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001879 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1880 scalarsPerPos, paint);
1881 }
1882}
1883
1884void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1885 size_t len, const SkPath& path,
1886 const SkMatrix* m, const SkPaint& paint) {
1887 CHECK_SHOULD_DRAW(draw);
1888
1889 SkASSERT(draw.fDevice == this);
1890 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1891}
1892
1893///////////////////////////////////////////////////////////////////////////////
1894
reed@google.comf67e4cf2011-03-15 20:56:58 +00001895bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1896 if (!paint.isLCDRenderText()) {
1897 // we're cool with the paint as is
1898 return false;
1899 }
1900
1901 if (paint.getShader() ||
1902 paint.getXfermode() || // unless its srcover
1903 paint.getMaskFilter() ||
1904 paint.getRasterizer() ||
1905 paint.getColorFilter() ||
1906 paint.getPathEffect() ||
1907 paint.isFakeBoldText() ||
1908 paint.getStyle() != SkPaint::kFill_Style) {
1909 // turn off lcd
1910 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1911 flags->fHinting = paint.getHinting();
1912 return true;
1913 }
1914 // we're cool with the paint as is
1915 return false;
1916}
1917
reed@google.com75d939b2011-12-07 15:07:23 +00001918void SkGpuDevice::flush() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001919 DO_DEFERRED_CLEAR;
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001920 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001921}
1922
reed@google.comf67e4cf2011-03-15 20:56:58 +00001923///////////////////////////////////////////////////////////////////////////////
1924
bsalomon@google.comfb309512011-11-30 14:13:48 +00001925bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001926 const GrTextureParams& params) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001927 uint64_t key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001928 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001929
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001930 GrTextureDesc desc;
1931 desc.fWidth = bitmap.width();
1932 desc.fHeight = bitmap.height();
rileya@google.com24f3ad12012-07-18 21:47:40 +00001933 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config());
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001934 desc.fClientCacheID = key;
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001935
bsalomon@google.comb8670992012-07-25 21:27:09 +00001936 return this->context()->isTextureInCache(desc, &params);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001937}
1938
1939
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001940SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1941 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001942 bool isOpaque,
1943 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001944 GrTextureDesc desc;
1945 desc.fConfig = fRenderTarget->config();
1946 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1947 desc.fWidth = width;
1948 desc.fHeight = height;
1949 desc.fSampleCnt = fRenderTarget->numSamples();
1950
1951 GrContext::TextureCacheEntry cacheEntry;
1952 GrTexture* texture;
1953 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001954 // Skia's convention is to only clear a device if it is non-opaque.
1955 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001956
1957#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1958 // layers are never draw in repeat modes, so we can request an approx
1959 // match and ignore any padding.
1960 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1961 GrContext::kApprox_ScratchTexMatch :
1962 GrContext::kExact_ScratchTexMatch;
1963 cacheEntry = fContext->lockScratchTexture(desc, matchType);
1964 texture = cacheEntry.texture();
1965#else
1966 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1967 texture = tunref.get();
1968#endif
1969 if (texture) {
1970 return SkNEW_ARGS(SkGpuDevice,(fContext,
1971 texture,
1972 cacheEntry,
1973 needClear));
1974 } else {
1975 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1976 width, height);
1977 return NULL;
1978 }
1979}
1980
1981SkGpuDevice::SkGpuDevice(GrContext* context,
1982 GrTexture* texture,
1983 TexCache cacheEntry,
1984 bool needClear)
robertphillips@google.com40a1ae42012-07-13 15:36:15 +00001985 : SkDevice(make_bitmap(context, texture->asRenderTarget()))
1986 , fClipStack(NULL) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001987 GrAssert(texture && texture->asRenderTarget());
1988 GrAssert(NULL == cacheEntry.texture() || texture == cacheEntry.texture());
1989 this->initFromRenderTarget(context, texture->asRenderTarget());
1990 fCache = cacheEntry;
1991 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001992}
1993