blob: 3e5bba0ce20814156978a87b0f1abbf1b832f420 [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
374 SkIRect bound;
375 SkClipStack::BoundsType boundType;
376 SkRect temp;
377
378 bound.setLTRB(0, 0, renderTargetWidth, renderTargetHeight);
379
380 clipStack.getBounds(&temp, &boundType);
381 if (SkClipStack::kNormal_BoundsType == boundType) {
382 SkIRect temp2;
383
384 temp.roundOut(&temp2);
385
386 temp2.offset(-origin.fX, -origin.fY);
387
388 if (!bound.intersect(temp2)) {
389 bound.setEmpty();
390 }
391 }
392
393// GrAssert(bound.contains(clipRegion.getBounds()));
394}
395#endif
396
reed@google.comac10a2d2010-12-22 21:39:39 +0000397///////////////////////////////////////////////////////////////////////////////
398
399static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000400 const SkClipStack& clipStack,
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000401 GrClipData& clipData,
reed@google.com6f8f2922011-03-04 22:27:10 +0000402 const SkRegion& clipRegion,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000403 const SkIPoint& origin,
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000404 int renderTargetWidth, int renderTargetHeight,
405 GrClip* result) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000406 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000407
408 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000409 iter.reset(clipStack);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000410
411#ifdef SK_DEBUG
412 check_bounds(clipStack, clipRegion, origin,
413 renderTargetWidth, renderTargetHeight);
414#endif
415
416 SkRect bounds;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000417 bool isIntersectionOfRects = false;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000418 clipStack.getConservativeBounds(-origin.fX,
419 -origin.fY,
420 renderTargetWidth,
421 renderTargetHeight,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000422 &bounds,
423 &isIntersectionOfRects);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000424
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000425 result->setFromIterator(&iter,
426 GrIntToScalar(-origin.x()),
427 GrIntToScalar(-origin.y()),
428 bounds);
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000429
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000430 GrAssert(result->isRect() == isIntersectionOfRects);
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000431
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000432 clipData.fClipStack = result;
433 clipData.fOrigin = origin;
434 context->setClip(&clipData);
reed@google.comac10a2d2010-12-22 21:39:39 +0000435}
436
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000437// call this every draw call, to ensure that the context reflects our state,
reed@google.comac10a2d2010-12-22 21:39:39 +0000438// and not the state from some other canvas/device
439void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000440 GrAssert(NULL != fClipStack);
441
reed@google.comac10a2d2010-12-22 21:39:39 +0000442 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000443 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000444
445 fContext->setRenderTarget(fRenderTarget);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000446 SkASSERT(draw.fClipStack && draw.fClipStack == fClipStack);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000447
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000448 convert_matrixclip(fContext, *draw.fMatrix,
449 *fClipStack, fClipData, *draw.fClip, this->getOrigin(),
450 fRenderTarget->width(), fRenderTarget->height(),
451 &fGrClip);
reed@google.comac10a2d2010-12-22 21:39:39 +0000452 fNeedPrepareRenderTarget = false;
453 }
454}
455
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000456void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
457 const SkClipStack& clipStack) {
458 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
459 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000460 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000461}
462
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000463void SkGpuDevice::gainFocus(const SkMatrix& matrix, const SkRegion& clip) {
464
465 GrAssert(NULL != fClipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000466
reed@google.comac10a2d2010-12-22 21:39:39 +0000467 fContext->setRenderTarget(fRenderTarget);
468
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000469 this->INHERITED::gainFocus(matrix, clip);
reed@google.comac10a2d2010-12-22 21:39:39 +0000470
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000471 convert_matrixclip(fContext, matrix, *fClipStack, fClipData, clip, this->getOrigin(),
472 fRenderTarget->width(), fRenderTarget->height(), &fGrClip);
reed@google.comac10a2d2010-12-22 21:39:39 +0000473
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000474 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000475}
476
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000477SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000478 DO_DEFERRED_CLEAR;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000479 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000480}
481
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000482bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000483 if (NULL != fTexture) {
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000484 paint->textureSampler(kBitmapTextureIdx)->setCustomStage(
485 SkNEW_ARGS(GrSingleTextureEffect, (fTexture)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000486 return true;
487 }
488 return false;
489}
490
491///////////////////////////////////////////////////////////////////////////////
492
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000493SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
494SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
495SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
496SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
497SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
498 shader_type_mismatch);
rileya@google.com3e332582012-07-03 13:43:35 +0000499SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
500 shader_type_mismatch);
rileya@google.com22e57f92012-07-19 15:16:19 +0000501SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
502SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000503
bsalomon@google.com84405e02012-03-05 19:57:21 +0000504namespace {
505
506// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
507// justAlpha indicates that skPaint's alpha should be used rather than the color
508// Callers may subsequently modify the GrPaint. Setting constantColor indicates
509// that the final paint will draw the same color at every pixel. This allows
510// an optimization where the the color filter can be applied to the skPaint's
twiz@google.com58071162012-07-18 21:41:50 +0000511// color once while converting to GrPaint and then ignored.
512inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
513 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000514 bool justAlpha,
515 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000516 SkGpuDevice::SkAutoCachedTexture* act,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000517 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000518
519 grPaint->fDither = skPaint.isDither();
520 grPaint->fAntiAlias = skPaint.isAntiAlias();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000521 grPaint->fCoverage = 0xFF;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000522
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000523 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
524 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000525
526 SkXfermode* mode = skPaint.getXfermode();
527 if (mode) {
528 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000529 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000530#if 0
531 return false;
532#endif
533 }
534 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000535 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
536 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
537
bsalomon@google.com5782d712011-01-21 21:03:59 +0000538 if (justAlpha) {
539 uint8_t alpha = skPaint.getAlpha();
540 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000541 // justAlpha is currently set to true only if there is a texture,
542 // so constantColor should not also be true.
543 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000544 } else {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000545 grPaint->fColor = SkColor2GrColor(skPaint.getColor());
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000546 GrAssert(!grPaint->isTextureStageEnabled(kShaderTextureIdx));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000547 }
Scroggo97c88c22011-05-11 14:05:25 +0000548 SkColorFilter* colorFilter = skPaint.getColorFilter();
549 SkColor color;
550 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000551 SkScalar matrix[20];
twiz@google.com58071162012-07-18 21:41:50 +0000552 SkBitmap colorTransformTable;
Scroggo97c88c22011-05-11 14:05:25 +0000553 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000554 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000555 if (!constantColor) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000556 grPaint->fColorFilterColor = SkColor2GrColor(color);
Scroggod757df22011-05-16 13:11:16 +0000557 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000558 } else {
559 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
rileya@google.com24f3ad12012-07-18 21:47:40 +0000560 grPaint->fColor = SkColor2GrColor(filtered);
senorblanco@chromium.orgb3c20fa2012-01-03 21:20:19 +0000561 grPaint->resetColorFilter();
Scroggod757df22011-05-16 13:11:16 +0000562 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000563 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
564 grPaint->fColorMatrixEnabled = true;
565 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
566 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
twiz@google.com58071162012-07-18 21:41:50 +0000567 } else if (colorFilter != NULL && colorFilter->asComponentTable(
568 &colorTransformTable)) {
569 grPaint->resetColorFilter();
570
571 GrSamplerState* colorSampler = grPaint->textureSampler(kColorFilterTextureIdx);
bsalomon@google.comb8670992012-07-25 21:27:09 +0000572 GrTexture* texture = act->set(dev, colorTransformTable, colorSampler->textureParams());
twiz@google.com58071162012-07-18 21:41:50 +0000573
bsalomon@google.comb8670992012-07-25 21:27:09 +0000574 colorSampler->reset();
bsalomon@google.comcbd0ad92012-07-20 15:09:31 +0000575 colorSampler->setCustomStage(SkNEW_ARGS(GrColorTableEffect, (texture)))->unref();
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000576 } else {
577 grPaint->resetColorFilter();
Scroggo97c88c22011-05-11 14:05:25 +0000578 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000579 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000580}
581
bsalomon@google.com84405e02012-03-05 19:57:21 +0000582// This function is similar to skPaint2GrPaintNoShader but also converts
583// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
584// be used is set on grPaint and returned in param act. constantColor has the
585// same meaning as in skPaint2GrPaintNoShader.
586inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
587 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000588 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000589 SkGpuDevice::SkAutoCachedTexture textures[GrPaint::kMaxTextures],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000590 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000591 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000592 if (NULL == shader) {
twiz@google.com58071162012-07-18 21:41:50 +0000593 return skPaint2GrPaintNoShader(dev,
594 skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000595 false,
596 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000597 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000598 grPaint);
twiz@google.com58071162012-07-18 21:41:50 +0000599 } else if (!skPaint2GrPaintNoShader(dev, skPaint, true, false,
600 &textures[kColorFilterTextureIdx], grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000601 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000602 }
603
rileya@google.com91f319c2012-07-25 17:18:31 +0000604 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
605 GrCustomStage* stage = shader->asNewCustomStage(dev->context(), sampler);
606
607 if (NULL != stage) {
608 sampler->setCustomStage(stage)->unref();
609 SkMatrix localM;
610 if (shader->getLocalMatrix(&localM)) {
611 SkMatrix inverse;
612 if (localM.invert(&inverse)) {
613 sampler->matrix()->preConcat(inverse);
614 }
615 }
616 return true;
617 }
618
reed@google.comac10a2d2010-12-22 21:39:39 +0000619 SkBitmap bitmap;
rileya@google.com91f319c2012-07-25 17:18:31 +0000620 SkMatrix* matrix = sampler->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000621 SkShader::TileMode tileModes[2];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000622 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
rileya@google.com91f319c2012-07-25 17:18:31 +0000623 tileModes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000624
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000625 if (SkShader::kNone_BitmapType == bmptype) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000626 SkShader::GradientInfo info;
627 SkColor color;
628
629 info.fColors = &color;
630 info.fColorOffsets = NULL;
631 info.fColorCount = 1;
632 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
633 SkPaint copy(skPaint);
634 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000635 // modulate the paint alpha by the shader's solid color alpha
636 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
637 copy.setColor(SkColorSetA(color, newA));
twiz@google.com58071162012-07-18 21:41:50 +0000638 return skPaint2GrPaintNoShader(dev,
639 copy,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000640 false,
641 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000642 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000643 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000644 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000645 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000646 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000647
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000648 // Must set wrap and filter on the sampler before requesting a texture.
bsalomon@google.comb8670992012-07-25 21:27:09 +0000649 sampler->textureParams()->reset(tileModes, skPaint.isFilterBitmap());
650 GrTexture* texture = textures[kShaderTextureIdx].set(dev, bitmap, sampler->textureParams());
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000651
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000652 if (NULL == texture) {
653 SkDebugf("Couldn't convert bitmap to texture.\n");
654 return false;
655 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000656
rileya@google.com91f319c2012-07-25 17:18:31 +0000657 sampler->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000658
reed@google.comac10a2d2010-12-22 21:39:39 +0000659 // since our texture coords will be in local space, we wack the texture
660 // matrix to map them back into 0...1 before we load it
661 SkMatrix localM;
662 if (shader->getLocalMatrix(&localM)) {
663 SkMatrix inverse;
664 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000665 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000666 }
667 }
668 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000669 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
670 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000671 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000672 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000673
674 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000675}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000676}
reed@google.comac10a2d2010-12-22 21:39:39 +0000677
678///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000679void SkGpuDevice::clear(SkColor color) {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000680 fContext->clear(NULL, color, fRenderTarget);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000681}
682
reed@google.comac10a2d2010-12-22 21:39:39 +0000683void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
684 CHECK_SHOULD_DRAW(draw);
685
bsalomon@google.com5782d712011-01-21 21:03:59 +0000686 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000687 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000688 if (!skPaint2GrPaintShader(this,
689 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000690 true,
twiz@google.com58071162012-07-18 21:41:50 +0000691 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000692 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000693 return;
694 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000695
696 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000697}
698
699// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000700static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000701 kPoints_GrPrimitiveType,
702 kLines_GrPrimitiveType,
703 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000704};
705
706void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000707 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000708 CHECK_SHOULD_DRAW(draw);
709
710 SkScalar width = paint.getStrokeWidth();
711 if (width < 0) {
712 return;
713 }
714
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000715 // we only handle hairlines and paints without path effects or mask filters,
716 // else we let the SkDraw call our drawPath()
717 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000718 draw.drawPoints(mode, count, pts, paint, true);
719 return;
720 }
721
bsalomon@google.com5782d712011-01-21 21:03:59 +0000722 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000723 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000724 if (!skPaint2GrPaintShader(this,
725 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000726 true,
twiz@google.com58071162012-07-18 21:41:50 +0000727 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000728 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000729 return;
730 }
731
bsalomon@google.com5782d712011-01-21 21:03:59 +0000732 fContext->drawVertices(grPaint,
733 gPointMode2PrimtiveType[mode],
734 count,
735 (GrPoint*)pts,
736 NULL,
737 NULL,
738 NULL,
739 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000740}
741
reed@google.comc9aa5872011-04-05 21:05:37 +0000742///////////////////////////////////////////////////////////////////////////////
743
reed@google.comac10a2d2010-12-22 21:39:39 +0000744void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
745 const SkPaint& paint) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000746 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000747 CHECK_SHOULD_DRAW(draw);
748
bungeman@google.com79bd8772011-07-18 15:34:08 +0000749 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000750 SkScalar width = paint.getStrokeWidth();
751
752 /*
753 We have special code for hairline strokes, miter-strokes, and fills.
754 Anything else we just call our path code.
755 */
756 bool usePath = doStroke && width > 0 &&
757 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000758 // another two reasons we might need to call drawPath...
759 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000760 usePath = true;
761 }
reed@google.com67db6642011-05-26 11:46:35 +0000762 // until we aa rotated rects...
763 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
764 usePath = true;
765 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000766 // small miter limit means right angles show bevel...
767 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
768 paint.getStrokeMiter() < SK_ScalarSqrt2)
769 {
770 usePath = true;
771 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000772 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000773 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
774 usePath = true;
775 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000776
777 if (usePath) {
778 SkPath path;
779 path.addRect(rect);
780 this->drawPath(draw, path, paint, NULL, true);
781 return;
782 }
783
784 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000785 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000786 if (!skPaint2GrPaintShader(this,
787 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000788 true,
twiz@google.com58071162012-07-18 21:41:50 +0000789 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000790 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000791 return;
792 }
reed@google.com20efde72011-05-09 17:00:02 +0000793 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000794}
795
reed@google.com69302852011-02-16 18:08:07 +0000796#include "SkMaskFilter.h"
797#include "SkBounder.h"
798
bsalomon@google.com85003222012-03-28 14:44:37 +0000799///////////////////////////////////////////////////////////////////////////////
800
801// helpers for applying mask filters
802namespace {
803
804GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000805 switch (fillType) {
806 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000807 return kWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000808 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000809 return kEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000810 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000811 return kInverseWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000812 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000813 return kInverseEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000814 default:
815 SkDebugf("Unsupported path fill type\n");
bsalomon@google.com47059542012-06-06 20:51:20 +0000816 return kHairLine_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000817 }
818}
819
bsalomon@google.com85003222012-03-28 14:44:37 +0000820// We prefer to blur small rect with small radius via CPU.
821#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
822#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
823inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
824 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
825 rect.height() <= MIN_GPU_BLUR_SIZE &&
826 radius <= MIN_GPU_BLUR_RADIUS) {
827 return true;
828 }
829 return false;
830}
831
832bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
833 SkMaskFilter* filter, const SkMatrix& matrix,
834 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000835 GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000836#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000837 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000838#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000839 SkMaskFilter::BlurInfo info;
840 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000841 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000842 return false;
843 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000844 SkScalar radius = info.fIgnoreTransform ? info.fRadius
845 : matrix.mapRadius(info.fRadius);
846 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000847 if (radius <= 0) {
848 return false;
849 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000850
851 SkRect srcRect = path.getBounds();
852 if (shouldDrawBlurWithCPU(srcRect, radius)) {
853 return false;
854 }
855
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000856 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000857 float sigma3 = sigma * 3.0f;
858
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000859 SkRect clipRect;
860 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000861
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000862 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000863 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
864 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000865 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000866 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000867 SkIRect finalIRect;
868 finalRect.roundOut(&finalIRect);
869 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000870 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000871 }
872 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000873 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000874 }
875 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000876 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000877 GrTextureDesc desc;
878 desc.fFlags = kRenderTarget_GrTextureFlagBit;
879 desc.fWidth = SkScalarCeilToInt(srcRect.width());
880 desc.fHeight = SkScalarCeilToInt(srcRect.height());
881 // We actually only need A8, but it often isn't supported as a
882 // render target so default to RGBA_8888
883 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000884
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000885 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
886 desc.fConfig = kAlpha_8_GrPixelConfig;
887 }
888
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000889 GrAutoScratchTexture pathEntry(context, desc);
890 GrTexture* pathTexture = pathEntry.texture();
891 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000892 return false;
893 }
894 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000895 // Once this code moves into GrContext, this should be changed to use
896 // an AutoClipRestore.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000897 const GrClipData* oldClipData = context->getClip();
898
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000899 context->setRenderTarget(pathTexture->asRenderTarget());
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000900
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000901 GrClip newClipStack(srcRect);
902 GrClipData newClipData;
903 newClipData.fClipStack = &newClipStack;
904 context->setClip(&newClipData);
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000905
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000906 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000907 GrPaint tempPaint;
908 tempPaint.reset();
909
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000910 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000911 tempPaint.fAntiAlias = grp->fAntiAlias;
912 if (tempPaint.fAntiAlias) {
913 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
914 // blend coeff of zero requires dual source blending support in order
915 // to properly blend partially covered pixels. This means the AA
916 // code path may not be taken. So we use a dst blend coeff of ISA. We
917 // could special case AA draws to a dst surface with known alpha=0 to
918 // use a zero dst coeff when dual source blending isn't available.
bsalomon@google.com47059542012-06-06 20:51:20 +0000919 tempPaint.fSrcBlendCoeff = kOne_GrBlendCoeff;
920 tempPaint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000921 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000922 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000923 context->drawPath(tempPaint, path, pathFillType, &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000924
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000925 // If we're doing a normal blur, we can clobber the pathTexture in the
926 // gaussianBlur. Otherwise, we need to save it for later compositing.
927 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +0000928 SkAutoTUnref<GrTexture> blurTexture(context->gaussianBlur(
929 pathTexture, isNormalBlur, srcRect, sigma, sigma));
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000930
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000931 if (!isNormalBlur) {
932 GrPaint paint;
933 paint.reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +0000934 paint.textureSampler(0)->textureParams()->setClampNoFilter();
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000935 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
936 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000937 // Blend pathTexture over blurTexture.
938 context->setRenderTarget(blurTexture->asRenderTarget());
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000939 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS
940 (GrSingleTextureEffect, (pathTexture)))->unref();
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000941 if (SkMaskFilter::kInner_BlurType == blurType) {
942 // inner: dst = dst * src
bsalomon@google.com47059542012-06-06 20:51:20 +0000943 paint.fSrcBlendCoeff = kDC_GrBlendCoeff;
944 paint.fDstBlendCoeff = kZero_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000945 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
946 // solid: dst = src + dst - src * dst
947 // = (1 - dst) * src + 1 * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000948 paint.fSrcBlendCoeff = kIDC_GrBlendCoeff;
949 paint.fDstBlendCoeff = kOne_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000950 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
951 // outer: dst = dst * (1 - src)
952 // = 0 * src + (1 - src) * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000953 paint.fSrcBlendCoeff = kZero_GrBlendCoeff;
954 paint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000955 }
956 context->drawRect(paint, srcRect);
957 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000958 context->setRenderTarget(oldRenderTarget);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000959 context->setClip(oldClipData);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000960
bsalomon@google.come3d32162012-07-20 13:37:06 +0000961 if (!grp->preConcatSamplerMatricesWithInverse(matrix)) {
962 return false;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000963 }
964
965 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
966 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000967 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
bsalomon@google.com97912912011-12-06 16:30:36 +0000968 grp->maskSampler(MASK_IDX)->reset();
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000969 grp->maskSampler(MASK_IDX)->setCustomStage(
970 SkNEW_ARGS(GrSingleTextureEffect, (blurTexture)))->unref();
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000971 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
972 -finalRect.fTop);
973 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
974 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000975 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000976 return true;
977}
978
bsalomon@google.com85003222012-03-28 14:44:37 +0000979bool drawWithMaskFilter(GrContext* context, const SkPath& path,
980 SkMaskFilter* filter, const SkMatrix& matrix,
981 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000982 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000983 SkMask srcM, dstM;
984
985 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000986 SkMask::kComputeBoundsAndRenderImage_CreateMode,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000987 style)) {
reed@google.com69302852011-02-16 18:08:07 +0000988 return false;
989 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000990 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000991
992 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
993 return false;
994 }
995 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000996 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000997
998 if (clip.quickReject(dstM.fBounds)) {
999 return false;
1000 }
1001 if (bounder && !bounder->doIRect(dstM.fBounds)) {
1002 return false;
1003 }
1004
1005 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
1006 // the current clip (and identity matrix) and grpaint settings
1007
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001008 GrContext::AutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +00001009
bsalomon@google.come3d32162012-07-20 13:37:06 +00001010 if (!grp->preConcatSamplerMatricesWithInverse(matrix)) {
1011 return false;
1012 }
1013
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001014 GrTextureDesc desc;
1015 desc.fWidth = dstM.fBounds.width();
1016 desc.fHeight = dstM.fBounds.height();
1017 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +00001018
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001019 GrAutoScratchTexture ast(context, desc);
1020 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001021
reed@google.com69302852011-02-16 18:08:07 +00001022 if (NULL == texture) {
1023 return false;
1024 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001025 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001026 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +00001027
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001028 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
1029 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001030 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
bsalomon@google.com97912912011-12-06 16:30:36 +00001031 grp->maskSampler(MASK_IDX)->reset();
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001032 grp->maskSampler(MASK_IDX)->setCustomStage(
1033 SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001034 GrRect d;
1035 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +00001036 GrIntToScalar(dstM.fBounds.fTop),
1037 GrIntToScalar(dstM.fBounds.fRight),
1038 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001039
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001040 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
1041 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
1042 -dstM.fBounds.fTop*SK_Scalar1);
1043 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001044 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +00001045 return true;
1046}
reed@google.com69302852011-02-16 18:08:07 +00001047
bsalomon@google.com85003222012-03-28 14:44:37 +00001048}
1049
1050///////////////////////////////////////////////////////////////////////////////
1051
reed@google.com0c219b62011-02-16 21:31:18 +00001052void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001053 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +00001054 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +00001055 CHECK_FOR_NODRAW_ANNOTATION(paint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001056 CHECK_SHOULD_DRAW(draw);
1057
reed@google.comfe626382011-09-21 13:50:35 +00001058 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001059
bsalomon@google.com5782d712011-01-21 21:03:59 +00001060 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001061 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001062 if (!skPaint2GrPaintShader(this,
1063 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001064 true,
twiz@google.com58071162012-07-18 21:41:50 +00001065 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001066 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001067 return;
1068 }
1069
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +00001070 // can we cheat, and threat a thin stroke as a hairline w/ coverage
1071 // if we can, we draw lots faster (raster device does this same test)
1072 SkScalar hairlineCoverage;
1073 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
1074 doFill = false;
1075 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
1076 grPaint.fCoverage);
1077 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001078
reed@google.comfe626382011-09-21 13:50:35 +00001079 // If we have a prematrix, apply it to the path, optimizing for the case
1080 // where the original path can in fact be modified in place (even though
1081 // its parameter type is const).
1082 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1083 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001084
1085 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001086 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001087
reed@google.come3445642011-02-16 23:20:39 +00001088 if (!pathIsMutable) {
1089 result = &tmpPath;
1090 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001091 }
reed@google.come3445642011-02-16 23:20:39 +00001092 // should I push prePathMatrix on our MV stack temporarily, instead
1093 // of applying it here? See SkDraw.cpp
1094 pathPtr->transform(*prePathMatrix, result);
1095 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001096 }
reed@google.com0c219b62011-02-16 21:31:18 +00001097 // at this point we're done with prePathMatrix
1098 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001099
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001100 if (paint.getPathEffect() ||
1101 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001102 // it is safe to use tmpPath here, even if we already used it for the
1103 // prepathmatrix, since getFillPath can take the same object for its
1104 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001105 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001106 pathPtr = &tmpPath;
1107 }
1108
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001109 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001110 // avoid possibly allocating a new path in transform if we can
1111 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1112
1113 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001114 pathPtr->transform(*draw.fMatrix, devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001115 GrPathFill pathFillType = doFill ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001116 skToGrFillType(devPathPtr->getFillType()) : kHairLine_GrPathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001117 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001118 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001119 &grPaint, pathFillType)) {
1120 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
1121 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001122 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001123 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001124 &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001125 }
reed@google.com69302852011-02-16 18:08:07 +00001126 return;
1127 }
reed@google.com69302852011-02-16 18:08:07 +00001128
bsalomon@google.com47059542012-06-06 20:51:20 +00001129 GrPathFill fill = kHairLine_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001130
reed@google.com0c219b62011-02-16 21:31:18 +00001131 if (doFill) {
1132 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001133 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001134 fill = kWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001135 break;
1136 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001137 fill = kEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001138 break;
1139 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001140 fill = kInverseWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001141 break;
1142 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001143 fill = kInverseEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001144 break;
1145 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001146 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001147 return;
1148 }
1149 }
1150
reed@google.com07f3ee12011-05-16 17:21:57 +00001151 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001152}
1153
bsalomon@google.comfb309512011-11-30 14:13:48 +00001154namespace {
1155
1156inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1157 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1158 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1159 return tilesX * tilesY;
1160}
1161
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001162inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001163 const SkIRect* srcRectPtr,
1164 int maxTextureSize) {
1165 static const int kSmallTileSize = 1 << 10;
1166 if (maxTextureSize <= kSmallTileSize) {
1167 return maxTextureSize;
1168 }
1169
1170 size_t maxTexTotalTileSize;
1171 size_t smallTotalTileSize;
1172
1173 if (NULL == srcRectPtr) {
1174 int w = bitmap.width();
1175 int h = bitmap.height();
1176 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1177 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1178 } else {
1179 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1180 srcRectPtr->fTop,
1181 srcRectPtr->fRight,
1182 srcRectPtr->fBottom,
1183 maxTextureSize);
1184 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1185 srcRectPtr->fTop,
1186 srcRectPtr->fRight,
1187 srcRectPtr->fBottom,
1188 kSmallTileSize);
1189 }
1190 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1191 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1192
1193 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1194 return kSmallTileSize;
1195 } else {
1196 return maxTextureSize;
1197 }
1198}
1199}
1200
1201bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001202 const GrTextureParams& params,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001203 const SkIRect* srcRectPtr,
1204 int* tileSize) const {
1205 SkASSERT(NULL != tileSize);
1206
1207 // if bitmap is explictly texture backed then just use the texture
1208 if (NULL != bitmap.getTexture()) {
1209 return false;
1210 }
1211 // if it's larger than the max texture size, then we have no choice but
1212 // tiling
1213 const int maxTextureSize = fContext->getMaxTextureSize();
1214 if (bitmap.width() > maxTextureSize ||
1215 bitmap.height() > maxTextureSize) {
1216 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1217 return true;
1218 }
1219 // if we are going to have to draw the whole thing, then don't tile
1220 if (NULL == srcRectPtr) {
1221 return false;
1222 }
1223 // if the entire texture is already in our cache then no reason to tile it
bsalomon@google.comb8670992012-07-25 21:27:09 +00001224 if (this->isBitmapInTextureCache(bitmap, params)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001225 return false;
1226 }
1227
1228 // At this point we know we could do the draw by uploading the entire bitmap
1229 // as a texture. However, if the texture would be large compared to the
1230 // cache size and we don't require most of it for this draw then tile to
1231 // reduce the amount of upload and cache spill.
1232
1233 // assumption here is that sw bitmap size is a good proxy for its size as
1234 // a texture
1235 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001236 size_t cacheSize;
1237 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001238 if (bmpSize < cacheSize / 2) {
1239 return false;
1240 }
1241
1242 SkFixed fracUsed =
1243 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1244 (srcRectPtr->height() << 16) / bitmap.height());
1245 if (fracUsed <= SK_FixedHalf) {
1246 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1247 return true;
1248 } else {
1249 return false;
1250 }
1251}
1252
reed@google.comac10a2d2010-12-22 21:39:39 +00001253void SkGpuDevice::drawBitmap(const SkDraw& draw,
1254 const SkBitmap& bitmap,
1255 const SkIRect* srcRectPtr,
1256 const SkMatrix& m,
1257 const SkPaint& paint) {
1258 CHECK_SHOULD_DRAW(draw);
1259
1260 SkIRect srcRect;
1261 if (NULL == srcRectPtr) {
1262 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1263 } else {
1264 srcRect = *srcRectPtr;
1265 }
1266
junov@google.comd935cfb2011-06-27 20:48:23 +00001267 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001268 // Convert the bitmap to a shader so that the rect can be drawn
1269 // through drawRect, which supports mask filters.
1270 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001271 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001272 if (srcRectPtr) {
1273 if (!bitmap.extractSubset(&tmp, srcRect)) {
1274 return; // extraction failed
1275 }
1276 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001277 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001278 }
1279 SkPaint paintWithTexture(paint);
1280 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1281 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001282 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001283 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001284
junov@google.com1d329782011-07-28 20:10:09 +00001285 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001286 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001287 // also affect the behavior of the mask filter.
1288 SkMatrix drawMatrix;
1289 drawMatrix.setConcat(*draw.fMatrix, m);
1290 SkDraw transformedDraw(draw);
1291 transformedDraw.fMatrix = &drawMatrix;
1292
1293 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1294
junov@google.comd935cfb2011-06-27 20:48:23 +00001295 return;
1296 }
1297
bsalomon@google.com5782d712011-01-21 21:03:59 +00001298 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001299 SkAutoCachedTexture colorLutTexture;
1300 if (!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001301 return;
1302 }
bsalomon@google.comb8670992012-07-25 21:27:09 +00001303 GrTextureParams* params = grPaint.textureSampler(kBitmapTextureIdx)->textureParams();
1304 params->setBilerp(paint.isFilterBitmap());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001305
bsalomon@google.comfb309512011-11-30 14:13:48 +00001306 int tileSize;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001307 if (!this->shouldTileBitmap(bitmap, *params, srcRectPtr, &tileSize)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001308 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001309 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001310 return;
1311 }
1312
1313 // undo the translate done by SkCanvas
1314 int DX = SkMax32(0, srcRect.fLeft);
1315 int DY = SkMax32(0, srcRect.fTop);
1316 // compute clip bounds in local coordinates
1317 SkIRect clipRect;
1318 {
1319 SkRect r;
1320 r.set(draw.fClip->getBounds());
1321 SkMatrix matrix, inverse;
1322 matrix.setConcat(*draw.fMatrix, m);
1323 if (!matrix.invert(&inverse)) {
1324 return;
1325 }
1326 inverse.mapRect(&r);
1327 r.roundOut(&clipRect);
1328 // apply the canvas' translate to our local clip
1329 clipRect.offset(DX, DY);
1330 }
1331
bsalomon@google.comfb309512011-11-30 14:13:48 +00001332 int nx = bitmap.width() / tileSize;
1333 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001334 for (int x = 0; x <= nx; x++) {
1335 for (int y = 0; y <= ny; y++) {
1336 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001337 tileR.set(x * tileSize, y * tileSize,
1338 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001339 if (!SkIRect::Intersects(tileR, clipRect)) {
1340 continue;
1341 }
1342
1343 SkIRect srcR = tileR;
1344 if (!srcR.intersect(srcRect)) {
1345 continue;
1346 }
1347
1348 SkBitmap tmpB;
1349 if (bitmap.extractSubset(&tmpB, tileR)) {
1350 // now offset it to make it "local" to our tmp bitmap
1351 srcR.offset(-tileR.fLeft, -tileR.fTop);
1352
1353 SkMatrix tmpM(m);
1354 {
1355 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1356 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1357 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1358 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001359 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001360 }
1361 }
1362 }
1363}
1364
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001365namespace {
1366
1367bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1368 // detect pixel disalignment
1369 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1370 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1371 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1372 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1373 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1374 COLOR_BLEED_TOLERANCE &&
1375 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1376 COLOR_BLEED_TOLERANCE) {
1377 return true;
1378 }
1379 return false;
1380}
1381
1382bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1383 const SkMatrix& m) {
1384 // Only gets called if hasAlignedSamples returned false.
1385 // So we can assume that sampling is axis aligned but not texel aligned.
1386 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
1387 SkRect innerSrcRect(srcRect), innerTransformedRect,
1388 outerTransformedRect(transformedRect);
1389 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1390 m.mapRect(&innerTransformedRect, innerSrcRect);
1391
1392 // The gap between outerTransformedRect and innerTransformedRect
1393 // represents the projection of the source border area, which is
1394 // problematic for color bleeding. We must check whether any
1395 // destination pixels sample the border area.
1396 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1397 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1398 SkIRect outer, inner;
1399 outerTransformedRect.round(&outer);
1400 innerTransformedRect.round(&inner);
1401 // If the inner and outer rects round to the same result, it means the
1402 // border does not overlap any pixel centers. Yay!
1403 return inner != outer;
1404}
1405
1406} // unnamed namespace
1407
reed@google.comac10a2d2010-12-22 21:39:39 +00001408/*
1409 * This is called by drawBitmap(), which has to handle images that may be too
1410 * large to be represented by a single texture.
1411 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001412 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1413 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001414 */
1415void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1416 const SkBitmap& bitmap,
1417 const SkIRect& srcRect,
1418 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001419 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001420 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1421 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001422
reed@google.com9c49bc32011-07-07 13:42:37 +00001423 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001424 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001425 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001426 return;
1427 }
1428
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001429 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001430
bsalomon@google.comb8670992012-07-25 21:27:09 +00001431 sampler->textureParams()->setClamp();
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001432 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001433
1434 GrTexture* texture;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001435 SkAutoCachedTexture act(this, bitmap, sampler->textureParams(), &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001436 if (NULL == texture) {
1437 return;
1438 }
1439
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001440 grPaint->textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1441 (GrSingleTextureEffect, (texture)))->unref();
reed@google.com46799cd2011-02-22 20:56:26 +00001442
reed@google.com20efde72011-05-09 17:00:02 +00001443 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1444 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001445 GrRect paintRect;
bsalomon@google.com91832162012-03-08 19:53:02 +00001446 float wInv = 1.f / bitmap.width();
1447 float hInv = 1.f / bitmap.height();
1448 paintRect.setLTRB(SkFloatToScalar(srcRect.fLeft * wInv),
1449 SkFloatToScalar(srcRect.fTop * hInv),
1450 SkFloatToScalar(srcRect.fRight * wInv),
1451 SkFloatToScalar(srcRect.fBottom * hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001452
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001453 bool needsTextureDomain = false;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001454 if (sampler->textureParams()->isBilerp()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001455 // Need texture domain if drawing a sub rect.
bsalomon@google.comb8670992012-07-25 21:27:09 +00001456 needsTextureDomain = srcRect.width() < bitmap.width() || srcRect.height() < bitmap.height();
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001457 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1458 // sampling is axis-aligned
1459 GrRect floatSrcRect, transformedRect;
1460 floatSrcRect.set(srcRect);
1461 SkMatrix srcToDeviceMatrix(m);
1462 srcToDeviceMatrix.postConcat(*draw.fMatrix);
1463 srcToDeviceMatrix.mapRect(&transformedRect, floatSrcRect);
1464
1465 if (hasAlignedSamples(floatSrcRect, transformedRect)) {
1466 // Samples are texel-aligned, so filtering is futile
bsalomon@google.comb8670992012-07-25 21:27:09 +00001467 sampler->textureParams()->setBilerp(false);
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001468 needsTextureDomain = false;
1469 } else {
1470 needsTextureDomain = needsTextureDomain &&
1471 mayColorBleed(floatSrcRect, transformedRect, m);
1472 }
1473 }
1474 }
1475
1476 GrRect textureDomain = GrRect::MakeEmpty();
1477
1478 if (needsTextureDomain) {
1479 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001480 GrScalar left, top, right, bottom;
1481 if (srcRect.width() > 1) {
1482 GrScalar border = GR_ScalarHalf / bitmap.width();
1483 left = paintRect.left() + border;
1484 right = paintRect.right() - border;
1485 } else {
1486 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1487 }
1488 if (srcRect.height() > 1) {
1489 GrScalar border = GR_ScalarHalf / bitmap.height();
1490 top = paintRect.top() + border;
1491 bottom = paintRect.bottom() - border;
1492 } else {
1493 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1494 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001495 textureDomain.setLTRB(left, top, right, bottom);
tomhudson@google.com2f68e762012-07-17 18:43:21 +00001496 sampler->setCustomStage(SkNEW_ARGS(GrTextureDomainEffect,
1497 (texture,
1498 textureDomain)))->unref();
junov@google.com6acc9b32011-05-16 18:32:07 +00001499 }
1500
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001501 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001502}
1503
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001504namespace {
1505
1506void apply_custom_stage(GrContext* context,
1507 GrTexture* srcTexture,
1508 GrTexture* dstTexture,
1509 const GrRect& rect,
1510 GrCustomStage* stage) {
1511 SkASSERT(srcTexture && srcTexture->getContext() == context);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001512 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001513 GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget());
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001514 GrContext::AutoClip acs(context, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001515
1516 GrMatrix sampleM;
1517 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
1518 GrPaint paint;
1519 paint.reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +00001520 paint.textureSampler(0)->textureParams()->setBilerp(true);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001521 paint.textureSampler(0)->reset(sampleM);
1522 paint.textureSampler(0)->setCustomStage(stage);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001523 context->drawRect(paint, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001524}
1525
1526};
1527
reed@google.com8926b162012-03-23 15:36:36 +00001528static GrTexture* filter_texture(GrContext* context, GrTexture* texture,
1529 SkImageFilter* filter, const GrRect& rect) {
1530 GrAssert(filter);
1531
1532 SkSize blurSize;
1533 SkISize radius;
1534
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001535 GrTextureDesc desc;
1536 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1537 desc.fWidth = SkScalarCeilToInt(rect.width());
1538 desc.fHeight = SkScalarCeilToInt(rect.height());
1539 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001540 GrCustomStage* stage;
reed@google.com8926b162012-03-23 15:36:36 +00001541
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001542 if (filter->asNewCustomStage(&stage, texture)) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001543 GrAutoScratchTexture dst(context, desc);
1544 apply_custom_stage(context, texture, dst.texture(), rect, stage);
1545 texture = dst.detach();
1546 stage->unref();
1547 } else if (filter->asABlur(&blurSize)) {
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001548 texture = context->gaussianBlur(texture, false, rect,
reed@google.com8926b162012-03-23 15:36:36 +00001549 blurSize.width(),
1550 blurSize.height());
reed@google.com8926b162012-03-23 15:36:36 +00001551 } else if (filter->asADilate(&radius)) {
reed@google.com8926b162012-03-23 15:36:36 +00001552 texture = context->applyMorphology(texture, rect,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001553 GrContext::kDilate_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001554 radius);
reed@google.com8926b162012-03-23 15:36:36 +00001555 } else if (filter->asAnErode(&radius)) {
reed@google.com8926b162012-03-23 15:36:36 +00001556 texture = context->applyMorphology(texture, rect,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001557 GrContext::kErode_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001558 radius);
reed@google.com8926b162012-03-23 15:36:36 +00001559 }
1560 return texture;
1561}
1562
reed@google.comac10a2d2010-12-22 21:39:39 +00001563void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1564 int left, int top, const SkPaint& paint) {
1565 CHECK_SHOULD_DRAW(draw);
1566
reed@google.com8926b162012-03-23 15:36:36 +00001567 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001568 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1569 return;
1570 }
1571
reed@google.com76dd2772012-01-05 21:15:07 +00001572 int w = bitmap.width();
1573 int h = bitmap.height();
1574
bsalomon@google.com5782d712011-01-21 21:03:59 +00001575 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001576 SkAutoCachedTexture colorLutTexture;
1577 if(!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001578 return;
1579 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001580
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001581 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001582
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001583 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001584
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001585 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001586 sampler->reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +00001587 SkAutoCachedTexture act(this, bitmap, sampler->textureParams(), &texture);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001588 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1589 (GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001590
reed@google.com8926b162012-03-23 15:36:36 +00001591 SkImageFilter* filter = paint.getImageFilter();
1592 if (NULL != filter) {
1593 GrTexture* filteredTexture = filter_texture(fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001594 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001595 if (filteredTexture) {
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001596 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1597 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001598 texture = filteredTexture;
1599 filteredTexture->unref();
1600 }
reed@google.com76dd2772012-01-05 21:15:07 +00001601 }
reed@google.com8926b162012-03-23 15:36:36 +00001602
bsalomon@google.com5782d712011-01-21 21:03:59 +00001603 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001604 GrRect::MakeXYWH(GrIntToScalar(left),
1605 GrIntToScalar(top),
1606 GrIntToScalar(w),
1607 GrIntToScalar(h)),
1608 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1609 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001610}
1611
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001612void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
reed@google.comac10a2d2010-12-22 21:39:39 +00001613 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001614 // clear of the source device must occur before CHECK_SHOULD_DRAW
1615 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1616 if (dev->fNeedClear) {
1617 // TODO: could check here whether we really need to draw at all
1618 dev->clear(0x0);
1619 }
1620
reed@google.comac10a2d2010-12-22 21:39:39 +00001621 CHECK_SHOULD_DRAW(draw);
1622
bsalomon@google.com5782d712011-01-21 21:03:59 +00001623 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001624 SkAutoCachedTexture colorLutTexture;
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001625 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001626 if (!dev->bindDeviceAsTexture(&grPaint) ||
twiz@google.com58071162012-07-18 21:41:50 +00001627 !skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001628 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001629 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001630
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001631 GrTexture* devTex = grPaint.getTextureSampler(kBitmapTextureIdx).getCustomStage()->texture(0);
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001632 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001633
reed@google.com8926b162012-03-23 15:36:36 +00001634 SkImageFilter* filter = paint.getImageFilter();
1635 if (NULL != filter) {
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001636 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001637 SkIntToScalar(devTex->height()));
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001638 GrTexture* filteredTexture = filter_texture(fContext, devTex, filter, rect);
reed@google.com8926b162012-03-23 15:36:36 +00001639 if (filteredTexture) {
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001640 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1641 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001642 devTex = filteredTexture;
1643 filteredTexture->unref();
1644 }
1645 }
1646
bsalomon@google.com5782d712011-01-21 21:03:59 +00001647 const SkBitmap& bm = dev->accessBitmap(false);
1648 int w = bm.width();
1649 int h = bm.height();
1650
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001651 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001652 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1653 GrIntToScalar(y),
1654 GrIntToScalar(w),
1655 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001656
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001657 // The device being drawn may not fill up its texture (saveLayer uses
1658 // the approximate ).
1659 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1660 GR_Scalar1 * h / devTex->height());
1661
1662 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001663}
1664
reed@google.com8926b162012-03-23 15:36:36 +00001665bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
reed@google.com76dd2772012-01-05 21:15:07 +00001666 SkSize size;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001667 SkISize radius;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001668
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001669 if (!filter->asNewCustomStage(NULL, NULL) &&
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001670 !filter->asABlur(&size) &&
1671 !filter->asADilate(&radius) &&
1672 !filter->asAnErode(&radius)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001673 return false;
1674 }
reed@google.com8926b162012-03-23 15:36:36 +00001675 return true;
1676}
1677
1678bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1679 const SkMatrix& ctm,
1680 SkBitmap* result, SkIPoint* offset) {
1681 // want explicitly our impl, so guard against a subclass of us overriding it
1682 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001683 return false;
1684 }
reed@google.com8926b162012-03-23 15:36:36 +00001685
1686 SkAutoLockPixels alp(src, !src.getTexture());
1687 if (!src.getTexture() && !src.readyToDraw()) {
1688 return false;
1689 }
1690
1691 GrPaint paint;
1692 paint.reset();
1693
1694 GrSamplerState* sampler = paint.textureSampler(kBitmapTextureIdx);
1695
1696 GrTexture* texture;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001697 SkAutoCachedTexture act(this, src, sampler->textureParams(), &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001698
1699 result->setConfig(src.config(), src.width(), src.height());
robertphillips@google.com8637a362012-04-10 18:32:35 +00001700 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
1701 SkIntToScalar(src.height()));
reed@google.com8926b162012-03-23 15:36:36 +00001702 GrTexture* resultTexture = filter_texture(fContext, texture, filter, rect);
1703 if (resultTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001704 result->setPixelRef(SkNEW_ARGS(SkGrTexturePixelRef,
1705 (resultTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001706 resultTexture->unref();
1707 }
reed@google.com76dd2772012-01-05 21:15:07 +00001708 return true;
1709}
1710
reed@google.comac10a2d2010-12-22 21:39:39 +00001711///////////////////////////////////////////////////////////////////////////////
1712
1713// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001714static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001715 kTriangles_GrPrimitiveType,
1716 kTriangleStrip_GrPrimitiveType,
1717 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001718};
1719
1720void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1721 int vertexCount, const SkPoint vertices[],
1722 const SkPoint texs[], const SkColor colors[],
1723 SkXfermode* xmode,
1724 const uint16_t indices[], int indexCount,
1725 const SkPaint& paint) {
1726 CHECK_SHOULD_DRAW(draw);
1727
bsalomon@google.com5782d712011-01-21 21:03:59 +00001728 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001729 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com5782d712011-01-21 21:03:59 +00001730 // we ignore the shader if texs is null.
1731 if (NULL == texs) {
twiz@google.com58071162012-07-18 21:41:50 +00001732 if (!skPaint2GrPaintNoShader(this,
1733 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001734 false,
1735 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001736 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +00001737 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001738 return;
1739 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001740 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001741 if (!skPaint2GrPaintShader(this,
1742 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001743 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001744 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001745 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001746 return;
1747 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001748 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001749
1750 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001751 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001752 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1753#if 0
1754 return
1755#endif
1756 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001757 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001758
bsalomon@google.com498776a2011-08-16 19:20:44 +00001759 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1760 if (NULL != colors) {
1761 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001762 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001763 for (int i = 0; i < vertexCount; ++i) {
rileya@google.com24f3ad12012-07-18 21:47:40 +00001764 convertedColors[i] = SkColor2GrColor(colors[i]);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001765 }
1766 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001767 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001768 fContext->drawVertices(grPaint,
1769 gVertexMode2PrimitiveType[vmode],
1770 vertexCount,
1771 (GrPoint*) vertices,
1772 (GrPoint*) texs,
1773 colors,
1774 indices,
1775 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001776}
1777
1778///////////////////////////////////////////////////////////////////////////////
1779
1780static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001781 GrFontScaler* scaler = (GrFontScaler*)data;
1782 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001783}
1784
1785static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1786 void* auxData;
1787 GrFontScaler* scaler = NULL;
1788 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1789 scaler = (GrFontScaler*)auxData;
1790 }
1791 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001792 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001793 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1794 }
1795 return scaler;
1796}
1797
1798static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1799 SkFixed fx, SkFixed fy,
1800 const SkGlyph& glyph) {
1801 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1802
bungeman@google.com15865a72012-01-11 16:28:04 +00001803 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001804
1805 if (NULL == procs->fFontScaler) {
1806 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1807 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001808
bungeman@google.com15865a72012-01-11 16:28:04 +00001809 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1810 glyph.getSubXFixed(),
1811 glyph.getSubYFixed()),
1812 SkFixedFloorToFixed(fx),
1813 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001814 procs->fFontScaler);
1815}
1816
bsalomon@google.com5782d712011-01-21 21:03:59 +00001817SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001818
1819 // deferred allocation
1820 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001821 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001822 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1823 fDrawProcs->fContext = fContext;
1824 }
1825
1826 // init our (and GL's) state
1827 fDrawProcs->fTextContext = context;
1828 fDrawProcs->fFontScaler = NULL;
1829 return fDrawProcs;
1830}
1831
1832void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1833 size_t byteLength, SkScalar x, SkScalar y,
1834 const SkPaint& paint) {
1835 CHECK_SHOULD_DRAW(draw);
1836
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001837 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001838 // this guy will just call our drawPath()
1839 draw.drawText((const char*)text, byteLength, x, y, paint);
1840 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001841 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001842
1843 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001844 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001845 if (!skPaint2GrPaintShader(this,
1846 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001847 true,
twiz@google.com58071162012-07-18 21:41:50 +00001848 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001849 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001850 return;
1851 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001852 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1853 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001854 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1855 }
1856}
1857
1858void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1859 size_t byteLength, const SkScalar pos[],
1860 SkScalar constY, int scalarsPerPos,
1861 const SkPaint& paint) {
1862 CHECK_SHOULD_DRAW(draw);
1863
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001864 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001865 // this guy will just call our drawPath()
1866 draw.drawPosText((const char*)text, byteLength, pos, constY,
1867 scalarsPerPos, paint);
1868 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001869 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001870
1871 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001872 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001873 if (!skPaint2GrPaintShader(this,
1874 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001875 true,
twiz@google.com58071162012-07-18 21:41:50 +00001876 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001877 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001878 return;
1879 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001880 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1881 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001882 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1883 scalarsPerPos, paint);
1884 }
1885}
1886
1887void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1888 size_t len, const SkPath& path,
1889 const SkMatrix* m, const SkPaint& paint) {
1890 CHECK_SHOULD_DRAW(draw);
1891
1892 SkASSERT(draw.fDevice == this);
1893 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1894}
1895
1896///////////////////////////////////////////////////////////////////////////////
1897
reed@google.comf67e4cf2011-03-15 20:56:58 +00001898bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1899 if (!paint.isLCDRenderText()) {
1900 // we're cool with the paint as is
1901 return false;
1902 }
1903
1904 if (paint.getShader() ||
1905 paint.getXfermode() || // unless its srcover
1906 paint.getMaskFilter() ||
1907 paint.getRasterizer() ||
1908 paint.getColorFilter() ||
1909 paint.getPathEffect() ||
1910 paint.isFakeBoldText() ||
1911 paint.getStyle() != SkPaint::kFill_Style) {
1912 // turn off lcd
1913 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1914 flags->fHinting = paint.getHinting();
1915 return true;
1916 }
1917 // we're cool with the paint as is
1918 return false;
1919}
1920
reed@google.com75d939b2011-12-07 15:07:23 +00001921void SkGpuDevice::flush() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001922 DO_DEFERRED_CLEAR;
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001923 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001924}
1925
reed@google.comf67e4cf2011-03-15 20:56:58 +00001926///////////////////////////////////////////////////////////////////////////////
1927
bsalomon@google.comfb309512011-11-30 14:13:48 +00001928bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001929 const GrTextureParams& params) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001930 uint64_t key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001931 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001932
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001933 GrTextureDesc desc;
1934 desc.fWidth = bitmap.width();
1935 desc.fHeight = bitmap.height();
rileya@google.com24f3ad12012-07-18 21:47:40 +00001936 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config());
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001937 desc.fClientCacheID = key;
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001938
bsalomon@google.comb8670992012-07-25 21:27:09 +00001939 return this->context()->isTextureInCache(desc, &params);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001940}
1941
1942
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001943SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1944 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001945 bool isOpaque,
1946 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001947 GrTextureDesc desc;
1948 desc.fConfig = fRenderTarget->config();
1949 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1950 desc.fWidth = width;
1951 desc.fHeight = height;
1952 desc.fSampleCnt = fRenderTarget->numSamples();
1953
1954 GrContext::TextureCacheEntry cacheEntry;
1955 GrTexture* texture;
1956 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001957 // Skia's convention is to only clear a device if it is non-opaque.
1958 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001959
1960#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1961 // layers are never draw in repeat modes, so we can request an approx
1962 // match and ignore any padding.
1963 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1964 GrContext::kApprox_ScratchTexMatch :
1965 GrContext::kExact_ScratchTexMatch;
1966 cacheEntry = fContext->lockScratchTexture(desc, matchType);
1967 texture = cacheEntry.texture();
1968#else
1969 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1970 texture = tunref.get();
1971#endif
1972 if (texture) {
1973 return SkNEW_ARGS(SkGpuDevice,(fContext,
1974 texture,
1975 cacheEntry,
1976 needClear));
1977 } else {
1978 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1979 width, height);
1980 return NULL;
1981 }
1982}
1983
1984SkGpuDevice::SkGpuDevice(GrContext* context,
1985 GrTexture* texture,
1986 TexCache cacheEntry,
1987 bool needClear)
robertphillips@google.com40a1ae42012-07-13 15:36:15 +00001988 : SkDevice(make_bitmap(context, texture->asRenderTarget()))
1989 , fClipStack(NULL) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001990 GrAssert(texture && texture->asRenderTarget());
1991 GrAssert(NULL == cacheEntry.texture() || texture == cacheEntry.texture());
1992 this->initFromRenderTarget(context, texture->asRenderTarget());
1993 fCache = cacheEntry;
1994 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001995}
1996