blob: dcfb57b0309c7d2fa848ea6b2165eca7cf35770b [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.comf8d904a2012-07-31 12:18:16 +0000418 clipStack.getConservativeBounds(0, 0,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000419 renderTargetWidth,
420 renderTargetHeight,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000421 &bounds,
422 &isIntersectionOfRects);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000423
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000424 result->setFromIterator(&iter, bounds);
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000425
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000426 GrAssert(result->isRect() == isIntersectionOfRects);
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000427
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000428 clipData.fClipStack = result;
429 clipData.fOrigin = origin;
430 context->setClip(&clipData);
reed@google.comac10a2d2010-12-22 21:39:39 +0000431}
432
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000433// call this every draw call, to ensure that the context reflects our state,
reed@google.comac10a2d2010-12-22 21:39:39 +0000434// and not the state from some other canvas/device
435void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000436 GrAssert(NULL != fClipStack);
437
reed@google.comac10a2d2010-12-22 21:39:39 +0000438 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000439 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000440
441 fContext->setRenderTarget(fRenderTarget);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000442 SkASSERT(draw.fClipStack && draw.fClipStack == fClipStack);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000443
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000444 convert_matrixclip(fContext, *draw.fMatrix,
445 *fClipStack, fClipData, *draw.fClip, this->getOrigin(),
446 fRenderTarget->width(), fRenderTarget->height(),
447 &fGrClip);
reed@google.comac10a2d2010-12-22 21:39:39 +0000448 fNeedPrepareRenderTarget = false;
449 }
450}
451
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000452void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
453 const SkClipStack& clipStack) {
454 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
455 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000456 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000457}
458
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000459void SkGpuDevice::gainFocus(const SkMatrix& matrix, const SkRegion& clip) {
460
461 GrAssert(NULL != fClipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000462
reed@google.comac10a2d2010-12-22 21:39:39 +0000463 fContext->setRenderTarget(fRenderTarget);
464
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000465 this->INHERITED::gainFocus(matrix, clip);
reed@google.comac10a2d2010-12-22 21:39:39 +0000466
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000467 convert_matrixclip(fContext, matrix, *fClipStack, fClipData, clip, this->getOrigin(),
468 fRenderTarget->width(), fRenderTarget->height(), &fGrClip);
reed@google.comac10a2d2010-12-22 21:39:39 +0000469
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000470 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000471}
472
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000473SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000474 DO_DEFERRED_CLEAR;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000475 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000476}
477
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000478bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000479 if (NULL != fTexture) {
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000480 paint->textureSampler(kBitmapTextureIdx)->setCustomStage(
481 SkNEW_ARGS(GrSingleTextureEffect, (fTexture)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000482 return true;
483 }
484 return false;
485}
486
487///////////////////////////////////////////////////////////////////////////////
488
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000489SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
490SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
491SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
492SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
493SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
494 shader_type_mismatch);
rileya@google.com3e332582012-07-03 13:43:35 +0000495SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
496 shader_type_mismatch);
rileya@google.com22e57f92012-07-19 15:16:19 +0000497SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
498SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000499
bsalomon@google.com84405e02012-03-05 19:57:21 +0000500namespace {
501
502// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
503// justAlpha indicates that skPaint's alpha should be used rather than the color
504// Callers may subsequently modify the GrPaint. Setting constantColor indicates
505// that the final paint will draw the same color at every pixel. This allows
506// an optimization where the the color filter can be applied to the skPaint's
twiz@google.com58071162012-07-18 21:41:50 +0000507// color once while converting to GrPaint and then ignored.
508inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
509 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000510 bool justAlpha,
511 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000512 SkGpuDevice::SkAutoCachedTexture* act,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000513 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000514
515 grPaint->fDither = skPaint.isDither();
516 grPaint->fAntiAlias = skPaint.isAntiAlias();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000517 grPaint->fCoverage = 0xFF;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000518
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000519 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
520 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000521
522 SkXfermode* mode = skPaint.getXfermode();
523 if (mode) {
524 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000525 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000526#if 0
527 return false;
528#endif
529 }
530 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000531 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
532 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
533
bsalomon@google.com5782d712011-01-21 21:03:59 +0000534 if (justAlpha) {
535 uint8_t alpha = skPaint.getAlpha();
536 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000537 // justAlpha is currently set to true only if there is a texture,
538 // so constantColor should not also be true.
539 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000540 } else {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000541 grPaint->fColor = SkColor2GrColor(skPaint.getColor());
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000542 GrAssert(!grPaint->isTextureStageEnabled(kShaderTextureIdx));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000543 }
Scroggo97c88c22011-05-11 14:05:25 +0000544 SkColorFilter* colorFilter = skPaint.getColorFilter();
545 SkColor color;
546 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000547 SkScalar matrix[20];
twiz@google.com58071162012-07-18 21:41:50 +0000548 SkBitmap colorTransformTable;
Scroggo97c88c22011-05-11 14:05:25 +0000549 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000550 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000551 if (!constantColor) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000552 grPaint->fColorFilterColor = SkColor2GrColor(color);
Scroggod757df22011-05-16 13:11:16 +0000553 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000554 } else {
555 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
rileya@google.com24f3ad12012-07-18 21:47:40 +0000556 grPaint->fColor = SkColor2GrColor(filtered);
senorblanco@chromium.orgb3c20fa2012-01-03 21:20:19 +0000557 grPaint->resetColorFilter();
Scroggod757df22011-05-16 13:11:16 +0000558 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000559 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
560 grPaint->fColorMatrixEnabled = true;
561 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
562 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
twiz@google.com58071162012-07-18 21:41:50 +0000563 } else if (colorFilter != NULL && colorFilter->asComponentTable(
564 &colorTransformTable)) {
565 grPaint->resetColorFilter();
566
567 GrSamplerState* colorSampler = grPaint->textureSampler(kColorFilterTextureIdx);
bsalomon@google.comb8670992012-07-25 21:27:09 +0000568 GrTexture* texture = act->set(dev, colorTransformTable, colorSampler->textureParams());
twiz@google.com58071162012-07-18 21:41:50 +0000569
bsalomon@google.comb8670992012-07-25 21:27:09 +0000570 colorSampler->reset();
bsalomon@google.comcbd0ad92012-07-20 15:09:31 +0000571 colorSampler->setCustomStage(SkNEW_ARGS(GrColorTableEffect, (texture)))->unref();
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000572 } else {
573 grPaint->resetColorFilter();
Scroggo97c88c22011-05-11 14:05:25 +0000574 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000575 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000576}
577
bsalomon@google.com84405e02012-03-05 19:57:21 +0000578// This function is similar to skPaint2GrPaintNoShader but also converts
579// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
580// be used is set on grPaint and returned in param act. constantColor has the
581// same meaning as in skPaint2GrPaintNoShader.
582inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
583 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000584 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000585 SkGpuDevice::SkAutoCachedTexture textures[GrPaint::kMaxTextures],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000586 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000587 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000588 if (NULL == shader) {
twiz@google.com58071162012-07-18 21:41:50 +0000589 return skPaint2GrPaintNoShader(dev,
590 skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000591 false,
592 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000593 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000594 grPaint);
twiz@google.com58071162012-07-18 21:41:50 +0000595 } else if (!skPaint2GrPaintNoShader(dev, skPaint, true, false,
596 &textures[kColorFilterTextureIdx], grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000597 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000598 }
599
rileya@google.com91f319c2012-07-25 17:18:31 +0000600 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
601 GrCustomStage* stage = shader->asNewCustomStage(dev->context(), sampler);
602
603 if (NULL != stage) {
604 sampler->setCustomStage(stage)->unref();
605 SkMatrix localM;
606 if (shader->getLocalMatrix(&localM)) {
607 SkMatrix inverse;
608 if (localM.invert(&inverse)) {
609 sampler->matrix()->preConcat(inverse);
610 }
611 }
612 return true;
613 }
614
reed@google.comac10a2d2010-12-22 21:39:39 +0000615 SkBitmap bitmap;
rileya@google.com91f319c2012-07-25 17:18:31 +0000616 SkMatrix* matrix = sampler->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000617 SkShader::TileMode tileModes[2];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000618 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
rileya@google.com91f319c2012-07-25 17:18:31 +0000619 tileModes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000620
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000621 if (SkShader::kNone_BitmapType == bmptype) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000622 SkShader::GradientInfo info;
623 SkColor color;
624
625 info.fColors = &color;
626 info.fColorOffsets = NULL;
627 info.fColorCount = 1;
628 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
629 SkPaint copy(skPaint);
630 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000631 // modulate the paint alpha by the shader's solid color alpha
632 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
633 copy.setColor(SkColorSetA(color, newA));
twiz@google.com58071162012-07-18 21:41:50 +0000634 return skPaint2GrPaintNoShader(dev,
635 copy,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000636 false,
637 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000638 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000639 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000640 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000641 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000642 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000643
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000644 // Must set wrap and filter on the sampler before requesting a texture.
bsalomon@google.comb8670992012-07-25 21:27:09 +0000645 sampler->textureParams()->reset(tileModes, skPaint.isFilterBitmap());
646 GrTexture* texture = textures[kShaderTextureIdx].set(dev, bitmap, sampler->textureParams());
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000647
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000648 if (NULL == texture) {
649 SkDebugf("Couldn't convert bitmap to texture.\n");
650 return false;
651 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000652
rileya@google.com91f319c2012-07-25 17:18:31 +0000653 sampler->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000654
reed@google.comac10a2d2010-12-22 21:39:39 +0000655 // since our texture coords will be in local space, we wack the texture
656 // matrix to map them back into 0...1 before we load it
657 SkMatrix localM;
658 if (shader->getLocalMatrix(&localM)) {
659 SkMatrix inverse;
660 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000661 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000662 }
663 }
664 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000665 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
666 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000667 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000668 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000669
670 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000671}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000672}
reed@google.comac10a2d2010-12-22 21:39:39 +0000673
674///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000675void SkGpuDevice::clear(SkColor color) {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000676 fContext->clear(NULL, color, fRenderTarget);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000677}
678
reed@google.comac10a2d2010-12-22 21:39:39 +0000679void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
680 CHECK_SHOULD_DRAW(draw);
681
bsalomon@google.com5782d712011-01-21 21:03:59 +0000682 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000683 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000684 if (!skPaint2GrPaintShader(this,
685 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000686 true,
twiz@google.com58071162012-07-18 21:41:50 +0000687 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000688 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000689 return;
690 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000691
692 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000693}
694
695// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000696static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000697 kPoints_GrPrimitiveType,
698 kLines_GrPrimitiveType,
699 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000700};
701
702void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000703 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000704 CHECK_SHOULD_DRAW(draw);
705
706 SkScalar width = paint.getStrokeWidth();
707 if (width < 0) {
708 return;
709 }
710
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000711 // we only handle hairlines and paints without path effects or mask filters,
712 // else we let the SkDraw call our drawPath()
713 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000714 draw.drawPoints(mode, count, pts, paint, true);
715 return;
716 }
717
bsalomon@google.com5782d712011-01-21 21:03:59 +0000718 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000719 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000720 if (!skPaint2GrPaintShader(this,
721 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000722 true,
twiz@google.com58071162012-07-18 21:41:50 +0000723 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000724 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000725 return;
726 }
727
bsalomon@google.com5782d712011-01-21 21:03:59 +0000728 fContext->drawVertices(grPaint,
729 gPointMode2PrimtiveType[mode],
730 count,
731 (GrPoint*)pts,
732 NULL,
733 NULL,
734 NULL,
735 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000736}
737
reed@google.comc9aa5872011-04-05 21:05:37 +0000738///////////////////////////////////////////////////////////////////////////////
739
reed@google.comac10a2d2010-12-22 21:39:39 +0000740void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
741 const SkPaint& paint) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000742 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000743 CHECK_SHOULD_DRAW(draw);
744
bungeman@google.com79bd8772011-07-18 15:34:08 +0000745 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000746 SkScalar width = paint.getStrokeWidth();
747
748 /*
749 We have special code for hairline strokes, miter-strokes, and fills.
750 Anything else we just call our path code.
751 */
752 bool usePath = doStroke && width > 0 &&
753 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000754 // another two reasons we might need to call drawPath...
755 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000756 usePath = true;
757 }
reed@google.com67db6642011-05-26 11:46:35 +0000758 // until we aa rotated rects...
759 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
760 usePath = true;
761 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000762 // small miter limit means right angles show bevel...
763 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
764 paint.getStrokeMiter() < SK_ScalarSqrt2)
765 {
766 usePath = true;
767 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000768 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000769 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
770 usePath = true;
771 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000772
773 if (usePath) {
774 SkPath path;
775 path.addRect(rect);
776 this->drawPath(draw, path, paint, NULL, true);
777 return;
778 }
779
780 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000781 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000782 if (!skPaint2GrPaintShader(this,
783 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000784 true,
twiz@google.com58071162012-07-18 21:41:50 +0000785 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000786 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000787 return;
788 }
reed@google.com20efde72011-05-09 17:00:02 +0000789 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000790}
791
reed@google.com69302852011-02-16 18:08:07 +0000792#include "SkMaskFilter.h"
793#include "SkBounder.h"
794
bsalomon@google.com85003222012-03-28 14:44:37 +0000795///////////////////////////////////////////////////////////////////////////////
796
797// helpers for applying mask filters
798namespace {
799
800GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000801 switch (fillType) {
802 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000803 return kWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000804 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000805 return kEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000806 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000807 return kInverseWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000808 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000809 return kInverseEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000810 default:
811 SkDebugf("Unsupported path fill type\n");
bsalomon@google.com47059542012-06-06 20:51:20 +0000812 return kHairLine_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000813 }
814}
815
bsalomon@google.com85003222012-03-28 14:44:37 +0000816// We prefer to blur small rect with small radius via CPU.
817#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
818#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
819inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
820 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
821 rect.height() <= MIN_GPU_BLUR_SIZE &&
822 radius <= MIN_GPU_BLUR_RADIUS) {
823 return true;
824 }
825 return false;
826}
827
828bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
829 SkMaskFilter* filter, const SkMatrix& matrix,
830 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000831 GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000832#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000833 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000834#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000835 SkMaskFilter::BlurInfo info;
836 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000837 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000838 return false;
839 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000840 SkScalar radius = info.fIgnoreTransform ? info.fRadius
841 : matrix.mapRadius(info.fRadius);
842 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000843 if (radius <= 0) {
844 return false;
845 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000846
847 SkRect srcRect = path.getBounds();
848 if (shouldDrawBlurWithCPU(srcRect, radius)) {
849 return false;
850 }
851
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000852 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000853 float sigma3 = sigma * 3.0f;
854
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000855 SkRect clipRect;
856 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000857
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000858 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000859 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
860 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000861 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000862 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000863 SkIRect finalIRect;
864 finalRect.roundOut(&finalIRect);
865 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000866 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000867 }
868 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000869 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000870 }
871 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000872 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000873 GrTextureDesc desc;
874 desc.fFlags = kRenderTarget_GrTextureFlagBit;
875 desc.fWidth = SkScalarCeilToInt(srcRect.width());
876 desc.fHeight = SkScalarCeilToInt(srcRect.height());
877 // We actually only need A8, but it often isn't supported as a
878 // render target so default to RGBA_8888
879 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000880
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000881 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
882 desc.fConfig = kAlpha_8_GrPixelConfig;
883 }
884
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000885 GrAutoScratchTexture pathEntry(context, desc);
886 GrTexture* pathTexture = pathEntry.texture();
887 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000888 return false;
889 }
890 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000891 // Once this code moves into GrContext, this should be changed to use
892 // an AutoClipRestore.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000893 const GrClipData* oldClipData = context->getClip();
894
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000895 context->setRenderTarget(pathTexture->asRenderTarget());
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000896
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000897 GrClip newClipStack(srcRect);
898 GrClipData newClipData;
899 newClipData.fClipStack = &newClipStack;
900 context->setClip(&newClipData);
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000901
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000902 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000903 GrPaint tempPaint;
904 tempPaint.reset();
905
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000906 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000907 tempPaint.fAntiAlias = grp->fAntiAlias;
908 if (tempPaint.fAntiAlias) {
909 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
910 // blend coeff of zero requires dual source blending support in order
911 // to properly blend partially covered pixels. This means the AA
912 // code path may not be taken. So we use a dst blend coeff of ISA. We
913 // could special case AA draws to a dst surface with known alpha=0 to
914 // use a zero dst coeff when dual source blending isn't available.
bsalomon@google.com47059542012-06-06 20:51:20 +0000915 tempPaint.fSrcBlendCoeff = kOne_GrBlendCoeff;
916 tempPaint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000917 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000918 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000919 context->drawPath(tempPaint, path, pathFillType, &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000920
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000921 // If we're doing a normal blur, we can clobber the pathTexture in the
922 // gaussianBlur. Otherwise, we need to save it for later compositing.
923 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +0000924 SkAutoTUnref<GrTexture> blurTexture(context->gaussianBlur(
925 pathTexture, isNormalBlur, srcRect, sigma, sigma));
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000926
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000927 if (!isNormalBlur) {
928 GrPaint paint;
929 paint.reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +0000930 paint.textureSampler(0)->textureParams()->setClampNoFilter();
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000931 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
932 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000933 // Blend pathTexture over blurTexture.
934 context->setRenderTarget(blurTexture->asRenderTarget());
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000935 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS
936 (GrSingleTextureEffect, (pathTexture)))->unref();
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000937 if (SkMaskFilter::kInner_BlurType == blurType) {
938 // inner: dst = dst * src
bsalomon@google.com47059542012-06-06 20:51:20 +0000939 paint.fSrcBlendCoeff = kDC_GrBlendCoeff;
940 paint.fDstBlendCoeff = kZero_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000941 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
942 // solid: dst = src + dst - src * dst
943 // = (1 - dst) * src + 1 * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000944 paint.fSrcBlendCoeff = kIDC_GrBlendCoeff;
945 paint.fDstBlendCoeff = kOne_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000946 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
947 // outer: dst = dst * (1 - src)
948 // = 0 * src + (1 - src) * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000949 paint.fSrcBlendCoeff = kZero_GrBlendCoeff;
950 paint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000951 }
952 context->drawRect(paint, srcRect);
953 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000954 context->setRenderTarget(oldRenderTarget);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000955 context->setClip(oldClipData);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000956
bsalomon@google.come3d32162012-07-20 13:37:06 +0000957 if (!grp->preConcatSamplerMatricesWithInverse(matrix)) {
958 return false;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000959 }
960
961 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
962 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000963 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
bsalomon@google.com97912912011-12-06 16:30:36 +0000964 grp->maskSampler(MASK_IDX)->reset();
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000965 grp->maskSampler(MASK_IDX)->setCustomStage(
966 SkNEW_ARGS(GrSingleTextureEffect, (blurTexture)))->unref();
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000967 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
968 -finalRect.fTop);
969 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
970 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000971 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000972 return true;
973}
974
bsalomon@google.com85003222012-03-28 14:44:37 +0000975bool drawWithMaskFilter(GrContext* context, const SkPath& path,
976 SkMaskFilter* filter, const SkMatrix& matrix,
977 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000978 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000979 SkMask srcM, dstM;
980
981 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000982 SkMask::kComputeBoundsAndRenderImage_CreateMode,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000983 style)) {
reed@google.com69302852011-02-16 18:08:07 +0000984 return false;
985 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000986 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000987
988 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
989 return false;
990 }
991 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000992 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000993
994 if (clip.quickReject(dstM.fBounds)) {
995 return false;
996 }
997 if (bounder && !bounder->doIRect(dstM.fBounds)) {
998 return false;
999 }
1000
1001 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
1002 // the current clip (and identity matrix) and grpaint settings
1003
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001004 GrContext::AutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +00001005
bsalomon@google.come3d32162012-07-20 13:37:06 +00001006 if (!grp->preConcatSamplerMatricesWithInverse(matrix)) {
1007 return false;
1008 }
1009
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001010 GrTextureDesc desc;
1011 desc.fWidth = dstM.fBounds.width();
1012 desc.fHeight = dstM.fBounds.height();
1013 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +00001014
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001015 GrAutoScratchTexture ast(context, desc);
1016 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001017
reed@google.com69302852011-02-16 18:08:07 +00001018 if (NULL == texture) {
1019 return false;
1020 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001021 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001022 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +00001023
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001024 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
1025 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001026 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
bsalomon@google.com97912912011-12-06 16:30:36 +00001027 grp->maskSampler(MASK_IDX)->reset();
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001028 grp->maskSampler(MASK_IDX)->setCustomStage(
1029 SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001030 GrRect d;
1031 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +00001032 GrIntToScalar(dstM.fBounds.fTop),
1033 GrIntToScalar(dstM.fBounds.fRight),
1034 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001035
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001036 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
1037 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
1038 -dstM.fBounds.fTop*SK_Scalar1);
1039 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001040 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +00001041 return true;
1042}
reed@google.com69302852011-02-16 18:08:07 +00001043
bsalomon@google.com85003222012-03-28 14:44:37 +00001044}
1045
1046///////////////////////////////////////////////////////////////////////////////
1047
reed@google.com0c219b62011-02-16 21:31:18 +00001048void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001049 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +00001050 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +00001051 CHECK_FOR_NODRAW_ANNOTATION(paint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001052 CHECK_SHOULD_DRAW(draw);
1053
reed@google.comfe626382011-09-21 13:50:35 +00001054 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001055
bsalomon@google.com5782d712011-01-21 21:03:59 +00001056 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001057 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001058 if (!skPaint2GrPaintShader(this,
1059 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001060 true,
twiz@google.com58071162012-07-18 21:41:50 +00001061 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001062 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001063 return;
1064 }
1065
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +00001066 // can we cheat, and threat a thin stroke as a hairline w/ coverage
1067 // if we can, we draw lots faster (raster device does this same test)
1068 SkScalar hairlineCoverage;
1069 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
1070 doFill = false;
1071 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
1072 grPaint.fCoverage);
1073 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001074
reed@google.comfe626382011-09-21 13:50:35 +00001075 // If we have a prematrix, apply it to the path, optimizing for the case
1076 // where the original path can in fact be modified in place (even though
1077 // its parameter type is const).
1078 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1079 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001080
1081 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001082 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001083
reed@google.come3445642011-02-16 23:20:39 +00001084 if (!pathIsMutable) {
1085 result = &tmpPath;
1086 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001087 }
reed@google.come3445642011-02-16 23:20:39 +00001088 // should I push prePathMatrix on our MV stack temporarily, instead
1089 // of applying it here? See SkDraw.cpp
1090 pathPtr->transform(*prePathMatrix, result);
1091 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001092 }
reed@google.com0c219b62011-02-16 21:31:18 +00001093 // at this point we're done with prePathMatrix
1094 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001095
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001096 if (paint.getPathEffect() ||
1097 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001098 // it is safe to use tmpPath here, even if we already used it for the
1099 // prepathmatrix, since getFillPath can take the same object for its
1100 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001101 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001102 pathPtr = &tmpPath;
1103 }
1104
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001105 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001106 // avoid possibly allocating a new path in transform if we can
1107 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1108
1109 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001110 pathPtr->transform(*draw.fMatrix, devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001111 GrPathFill pathFillType = doFill ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001112 skToGrFillType(devPathPtr->getFillType()) : kHairLine_GrPathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001113 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001114 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001115 &grPaint, pathFillType)) {
1116 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
1117 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001118 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001119 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001120 &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001121 }
reed@google.com69302852011-02-16 18:08:07 +00001122 return;
1123 }
reed@google.com69302852011-02-16 18:08:07 +00001124
bsalomon@google.com47059542012-06-06 20:51:20 +00001125 GrPathFill fill = kHairLine_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001126
reed@google.com0c219b62011-02-16 21:31:18 +00001127 if (doFill) {
1128 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001129 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001130 fill = kWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001131 break;
1132 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001133 fill = kEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001134 break;
1135 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001136 fill = kInverseWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001137 break;
1138 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001139 fill = kInverseEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001140 break;
1141 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001142 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001143 return;
1144 }
1145 }
1146
reed@google.com07f3ee12011-05-16 17:21:57 +00001147 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001148}
1149
bsalomon@google.comfb309512011-11-30 14:13:48 +00001150namespace {
1151
1152inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1153 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1154 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1155 return tilesX * tilesY;
1156}
1157
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001158inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001159 const SkIRect* srcRectPtr,
1160 int maxTextureSize) {
1161 static const int kSmallTileSize = 1 << 10;
1162 if (maxTextureSize <= kSmallTileSize) {
1163 return maxTextureSize;
1164 }
1165
1166 size_t maxTexTotalTileSize;
1167 size_t smallTotalTileSize;
1168
1169 if (NULL == srcRectPtr) {
1170 int w = bitmap.width();
1171 int h = bitmap.height();
1172 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1173 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1174 } else {
1175 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1176 srcRectPtr->fTop,
1177 srcRectPtr->fRight,
1178 srcRectPtr->fBottom,
1179 maxTextureSize);
1180 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1181 srcRectPtr->fTop,
1182 srcRectPtr->fRight,
1183 srcRectPtr->fBottom,
1184 kSmallTileSize);
1185 }
1186 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1187 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1188
1189 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1190 return kSmallTileSize;
1191 } else {
1192 return maxTextureSize;
1193 }
1194}
1195}
1196
1197bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001198 const GrTextureParams& params,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001199 const SkIRect* srcRectPtr,
1200 int* tileSize) const {
1201 SkASSERT(NULL != tileSize);
1202
1203 // if bitmap is explictly texture backed then just use the texture
1204 if (NULL != bitmap.getTexture()) {
1205 return false;
1206 }
1207 // if it's larger than the max texture size, then we have no choice but
1208 // tiling
1209 const int maxTextureSize = fContext->getMaxTextureSize();
1210 if (bitmap.width() > maxTextureSize ||
1211 bitmap.height() > maxTextureSize) {
1212 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1213 return true;
1214 }
1215 // if we are going to have to draw the whole thing, then don't tile
1216 if (NULL == srcRectPtr) {
1217 return false;
1218 }
1219 // if the entire texture is already in our cache then no reason to tile it
bsalomon@google.comb8670992012-07-25 21:27:09 +00001220 if (this->isBitmapInTextureCache(bitmap, params)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001221 return false;
1222 }
1223
1224 // At this point we know we could do the draw by uploading the entire bitmap
1225 // as a texture. However, if the texture would be large compared to the
1226 // cache size and we don't require most of it for this draw then tile to
1227 // reduce the amount of upload and cache spill.
1228
1229 // assumption here is that sw bitmap size is a good proxy for its size as
1230 // a texture
1231 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001232 size_t cacheSize;
1233 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001234 if (bmpSize < cacheSize / 2) {
1235 return false;
1236 }
1237
1238 SkFixed fracUsed =
1239 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1240 (srcRectPtr->height() << 16) / bitmap.height());
1241 if (fracUsed <= SK_FixedHalf) {
1242 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1243 return true;
1244 } else {
1245 return false;
1246 }
1247}
1248
reed@google.comac10a2d2010-12-22 21:39:39 +00001249void SkGpuDevice::drawBitmap(const SkDraw& draw,
1250 const SkBitmap& bitmap,
1251 const SkIRect* srcRectPtr,
1252 const SkMatrix& m,
1253 const SkPaint& paint) {
1254 CHECK_SHOULD_DRAW(draw);
1255
1256 SkIRect srcRect;
1257 if (NULL == srcRectPtr) {
1258 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1259 } else {
1260 srcRect = *srcRectPtr;
1261 }
1262
junov@google.comd935cfb2011-06-27 20:48:23 +00001263 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001264 // Convert the bitmap to a shader so that the rect can be drawn
1265 // through drawRect, which supports mask filters.
1266 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001267 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001268 if (srcRectPtr) {
1269 if (!bitmap.extractSubset(&tmp, srcRect)) {
1270 return; // extraction failed
1271 }
1272 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001273 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001274 }
1275 SkPaint paintWithTexture(paint);
1276 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1277 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001278 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001279 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001280
junov@google.com1d329782011-07-28 20:10:09 +00001281 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001282 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001283 // also affect the behavior of the mask filter.
1284 SkMatrix drawMatrix;
1285 drawMatrix.setConcat(*draw.fMatrix, m);
1286 SkDraw transformedDraw(draw);
1287 transformedDraw.fMatrix = &drawMatrix;
1288
1289 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1290
junov@google.comd935cfb2011-06-27 20:48:23 +00001291 return;
1292 }
1293
bsalomon@google.com5782d712011-01-21 21:03:59 +00001294 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001295 SkAutoCachedTexture colorLutTexture;
1296 if (!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001297 return;
1298 }
bsalomon@google.comb8670992012-07-25 21:27:09 +00001299 GrTextureParams* params = grPaint.textureSampler(kBitmapTextureIdx)->textureParams();
1300 params->setBilerp(paint.isFilterBitmap());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001301
bsalomon@google.comfb309512011-11-30 14:13:48 +00001302 int tileSize;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001303 if (!this->shouldTileBitmap(bitmap, *params, srcRectPtr, &tileSize)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001304 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001305 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001306 return;
1307 }
1308
1309 // undo the translate done by SkCanvas
1310 int DX = SkMax32(0, srcRect.fLeft);
1311 int DY = SkMax32(0, srcRect.fTop);
1312 // compute clip bounds in local coordinates
1313 SkIRect clipRect;
1314 {
1315 SkRect r;
1316 r.set(draw.fClip->getBounds());
1317 SkMatrix matrix, inverse;
1318 matrix.setConcat(*draw.fMatrix, m);
1319 if (!matrix.invert(&inverse)) {
1320 return;
1321 }
1322 inverse.mapRect(&r);
1323 r.roundOut(&clipRect);
1324 // apply the canvas' translate to our local clip
1325 clipRect.offset(DX, DY);
1326 }
1327
bsalomon@google.comfb309512011-11-30 14:13:48 +00001328 int nx = bitmap.width() / tileSize;
1329 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001330 for (int x = 0; x <= nx; x++) {
1331 for (int y = 0; y <= ny; y++) {
1332 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001333 tileR.set(x * tileSize, y * tileSize,
1334 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001335 if (!SkIRect::Intersects(tileR, clipRect)) {
1336 continue;
1337 }
1338
1339 SkIRect srcR = tileR;
1340 if (!srcR.intersect(srcRect)) {
1341 continue;
1342 }
1343
1344 SkBitmap tmpB;
1345 if (bitmap.extractSubset(&tmpB, tileR)) {
1346 // now offset it to make it "local" to our tmp bitmap
1347 srcR.offset(-tileR.fLeft, -tileR.fTop);
1348
1349 SkMatrix tmpM(m);
1350 {
1351 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1352 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1353 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1354 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001355 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001356 }
1357 }
1358 }
1359}
1360
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001361namespace {
1362
1363bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1364 // detect pixel disalignment
1365 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1366 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1367 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1368 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1369 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1370 COLOR_BLEED_TOLERANCE &&
1371 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1372 COLOR_BLEED_TOLERANCE) {
1373 return true;
1374 }
1375 return false;
1376}
1377
1378bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1379 const SkMatrix& m) {
1380 // Only gets called if hasAlignedSamples returned false.
1381 // So we can assume that sampling is axis aligned but not texel aligned.
1382 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
1383 SkRect innerSrcRect(srcRect), innerTransformedRect,
1384 outerTransformedRect(transformedRect);
1385 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1386 m.mapRect(&innerTransformedRect, innerSrcRect);
1387
1388 // The gap between outerTransformedRect and innerTransformedRect
1389 // represents the projection of the source border area, which is
1390 // problematic for color bleeding. We must check whether any
1391 // destination pixels sample the border area.
1392 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1393 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1394 SkIRect outer, inner;
1395 outerTransformedRect.round(&outer);
1396 innerTransformedRect.round(&inner);
1397 // If the inner and outer rects round to the same result, it means the
1398 // border does not overlap any pixel centers. Yay!
1399 return inner != outer;
1400}
1401
1402} // unnamed namespace
1403
reed@google.comac10a2d2010-12-22 21:39:39 +00001404/*
1405 * This is called by drawBitmap(), which has to handle images that may be too
1406 * large to be represented by a single texture.
1407 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001408 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1409 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001410 */
1411void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1412 const SkBitmap& bitmap,
1413 const SkIRect& srcRect,
1414 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001415 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001416 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1417 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001418
reed@google.com9c49bc32011-07-07 13:42:37 +00001419 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001420 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001421 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001422 return;
1423 }
1424
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001425 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001426
bsalomon@google.comb8670992012-07-25 21:27:09 +00001427 sampler->textureParams()->setClamp();
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001428 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001429
1430 GrTexture* texture;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001431 SkAutoCachedTexture act(this, bitmap, sampler->textureParams(), &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001432 if (NULL == texture) {
1433 return;
1434 }
1435
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001436 grPaint->textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1437 (GrSingleTextureEffect, (texture)))->unref();
reed@google.com46799cd2011-02-22 20:56:26 +00001438
reed@google.com20efde72011-05-09 17:00:02 +00001439 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1440 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001441 GrRect paintRect;
bsalomon@google.com91832162012-03-08 19:53:02 +00001442 float wInv = 1.f / bitmap.width();
1443 float hInv = 1.f / bitmap.height();
1444 paintRect.setLTRB(SkFloatToScalar(srcRect.fLeft * wInv),
1445 SkFloatToScalar(srcRect.fTop * hInv),
1446 SkFloatToScalar(srcRect.fRight * wInv),
1447 SkFloatToScalar(srcRect.fBottom * hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001448
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001449 bool needsTextureDomain = false;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001450 if (sampler->textureParams()->isBilerp()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001451 // Need texture domain if drawing a sub rect.
bsalomon@google.comb8670992012-07-25 21:27:09 +00001452 needsTextureDomain = srcRect.width() < bitmap.width() || srcRect.height() < bitmap.height();
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001453 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1454 // sampling is axis-aligned
1455 GrRect floatSrcRect, transformedRect;
1456 floatSrcRect.set(srcRect);
1457 SkMatrix srcToDeviceMatrix(m);
1458 srcToDeviceMatrix.postConcat(*draw.fMatrix);
1459 srcToDeviceMatrix.mapRect(&transformedRect, floatSrcRect);
1460
1461 if (hasAlignedSamples(floatSrcRect, transformedRect)) {
1462 // Samples are texel-aligned, so filtering is futile
bsalomon@google.comb8670992012-07-25 21:27:09 +00001463 sampler->textureParams()->setBilerp(false);
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001464 needsTextureDomain = false;
1465 } else {
1466 needsTextureDomain = needsTextureDomain &&
1467 mayColorBleed(floatSrcRect, transformedRect, m);
1468 }
1469 }
1470 }
1471
1472 GrRect textureDomain = GrRect::MakeEmpty();
1473
1474 if (needsTextureDomain) {
1475 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001476 GrScalar left, top, right, bottom;
1477 if (srcRect.width() > 1) {
1478 GrScalar border = GR_ScalarHalf / bitmap.width();
1479 left = paintRect.left() + border;
1480 right = paintRect.right() - border;
1481 } else {
1482 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1483 }
1484 if (srcRect.height() > 1) {
1485 GrScalar border = GR_ScalarHalf / bitmap.height();
1486 top = paintRect.top() + border;
1487 bottom = paintRect.bottom() - border;
1488 } else {
1489 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1490 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001491 textureDomain.setLTRB(left, top, right, bottom);
tomhudson@google.com2f68e762012-07-17 18:43:21 +00001492 sampler->setCustomStage(SkNEW_ARGS(GrTextureDomainEffect,
1493 (texture,
1494 textureDomain)))->unref();
junov@google.com6acc9b32011-05-16 18:32:07 +00001495 }
1496
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001497 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001498}
1499
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001500namespace {
1501
1502void apply_custom_stage(GrContext* context,
1503 GrTexture* srcTexture,
1504 GrTexture* dstTexture,
1505 const GrRect& rect,
1506 GrCustomStage* stage) {
1507 SkASSERT(srcTexture && srcTexture->getContext() == context);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001508 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001509 GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget());
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001510 GrContext::AutoClip acs(context, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001511
1512 GrMatrix sampleM;
1513 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
1514 GrPaint paint;
1515 paint.reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +00001516 paint.textureSampler(0)->textureParams()->setBilerp(true);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001517 paint.textureSampler(0)->reset(sampleM);
1518 paint.textureSampler(0)->setCustomStage(stage);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001519 context->drawRect(paint, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001520}
1521
1522};
1523
reed@google.com8926b162012-03-23 15:36:36 +00001524static GrTexture* filter_texture(GrContext* context, GrTexture* texture,
1525 SkImageFilter* filter, const GrRect& rect) {
1526 GrAssert(filter);
1527
1528 SkSize blurSize;
1529 SkISize radius;
1530
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001531 GrTextureDesc desc;
1532 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1533 desc.fWidth = SkScalarCeilToInt(rect.width());
1534 desc.fHeight = SkScalarCeilToInt(rect.height());
1535 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001536 GrCustomStage* stage;
reed@google.com8926b162012-03-23 15:36:36 +00001537
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001538 if (filter->asNewCustomStage(&stage, texture)) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001539 GrAutoScratchTexture dst(context, desc);
1540 apply_custom_stage(context, texture, dst.texture(), rect, stage);
1541 texture = dst.detach();
1542 stage->unref();
1543 } else if (filter->asABlur(&blurSize)) {
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001544 texture = context->gaussianBlur(texture, false, rect,
reed@google.com8926b162012-03-23 15:36:36 +00001545 blurSize.width(),
1546 blurSize.height());
reed@google.com8926b162012-03-23 15:36:36 +00001547 } else if (filter->asADilate(&radius)) {
reed@google.com8926b162012-03-23 15:36:36 +00001548 texture = context->applyMorphology(texture, rect,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001549 GrContext::kDilate_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001550 radius);
reed@google.com8926b162012-03-23 15:36:36 +00001551 } else if (filter->asAnErode(&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::kErode_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001554 radius);
reed@google.com8926b162012-03-23 15:36:36 +00001555 }
1556 return texture;
1557}
1558
reed@google.comac10a2d2010-12-22 21:39:39 +00001559void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1560 int left, int top, const SkPaint& paint) {
1561 CHECK_SHOULD_DRAW(draw);
1562
reed@google.com8926b162012-03-23 15:36:36 +00001563 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001564 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1565 return;
1566 }
1567
reed@google.com76dd2772012-01-05 21:15:07 +00001568 int w = bitmap.width();
1569 int h = bitmap.height();
1570
bsalomon@google.com5782d712011-01-21 21:03:59 +00001571 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001572 SkAutoCachedTexture colorLutTexture;
1573 if(!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001574 return;
1575 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001576
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001577 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001578
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001579 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001580
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001581 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001582 sampler->reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +00001583 SkAutoCachedTexture act(this, bitmap, sampler->textureParams(), &texture);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001584 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1585 (GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001586
reed@google.com8926b162012-03-23 15:36:36 +00001587 SkImageFilter* filter = paint.getImageFilter();
1588 if (NULL != filter) {
1589 GrTexture* filteredTexture = filter_texture(fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001590 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001591 if (filteredTexture) {
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001592 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1593 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001594 texture = filteredTexture;
1595 filteredTexture->unref();
1596 }
reed@google.com76dd2772012-01-05 21:15:07 +00001597 }
reed@google.com8926b162012-03-23 15:36:36 +00001598
bsalomon@google.com5782d712011-01-21 21:03:59 +00001599 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001600 GrRect::MakeXYWH(GrIntToScalar(left),
1601 GrIntToScalar(top),
1602 GrIntToScalar(w),
1603 GrIntToScalar(h)),
1604 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1605 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001606}
1607
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001608void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
reed@google.comac10a2d2010-12-22 21:39:39 +00001609 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001610 // clear of the source device must occur before CHECK_SHOULD_DRAW
1611 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1612 if (dev->fNeedClear) {
1613 // TODO: could check here whether we really need to draw at all
1614 dev->clear(0x0);
1615 }
1616
reed@google.comac10a2d2010-12-22 21:39:39 +00001617 CHECK_SHOULD_DRAW(draw);
1618
bsalomon@google.com5782d712011-01-21 21:03:59 +00001619 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001620 SkAutoCachedTexture colorLutTexture;
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001621 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001622 if (!dev->bindDeviceAsTexture(&grPaint) ||
twiz@google.com58071162012-07-18 21:41:50 +00001623 !skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001624 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001625 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001626
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001627 GrTexture* devTex = grPaint.getTextureSampler(kBitmapTextureIdx).getCustomStage()->texture(0);
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001628 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001629
reed@google.com8926b162012-03-23 15:36:36 +00001630 SkImageFilter* filter = paint.getImageFilter();
1631 if (NULL != filter) {
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001632 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001633 SkIntToScalar(devTex->height()));
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001634 GrTexture* filteredTexture = filter_texture(fContext, devTex, filter, rect);
reed@google.com8926b162012-03-23 15:36:36 +00001635 if (filteredTexture) {
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001636 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1637 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001638 devTex = filteredTexture;
1639 filteredTexture->unref();
1640 }
1641 }
1642
bsalomon@google.com5782d712011-01-21 21:03:59 +00001643 const SkBitmap& bm = dev->accessBitmap(false);
1644 int w = bm.width();
1645 int h = bm.height();
1646
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001647 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001648 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1649 GrIntToScalar(y),
1650 GrIntToScalar(w),
1651 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001652
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001653 // The device being drawn may not fill up its texture (saveLayer uses
1654 // the approximate ).
1655 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1656 GR_Scalar1 * h / devTex->height());
1657
1658 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001659}
1660
reed@google.com8926b162012-03-23 15:36:36 +00001661bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
reed@google.com76dd2772012-01-05 21:15:07 +00001662 SkSize size;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001663 SkISize radius;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001664
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001665 if (!filter->asNewCustomStage(NULL, NULL) &&
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001666 !filter->asABlur(&size) &&
1667 !filter->asADilate(&radius) &&
1668 !filter->asAnErode(&radius)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001669 return false;
1670 }
reed@google.com8926b162012-03-23 15:36:36 +00001671 return true;
1672}
1673
1674bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1675 const SkMatrix& ctm,
1676 SkBitmap* result, SkIPoint* offset) {
1677 // want explicitly our impl, so guard against a subclass of us overriding it
1678 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001679 return false;
1680 }
reed@google.com8926b162012-03-23 15:36:36 +00001681
1682 SkAutoLockPixels alp(src, !src.getTexture());
1683 if (!src.getTexture() && !src.readyToDraw()) {
1684 return false;
1685 }
1686
1687 GrPaint paint;
1688 paint.reset();
1689
1690 GrSamplerState* sampler = paint.textureSampler(kBitmapTextureIdx);
1691
1692 GrTexture* texture;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001693 SkAutoCachedTexture act(this, src, sampler->textureParams(), &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001694
1695 result->setConfig(src.config(), src.width(), src.height());
robertphillips@google.com8637a362012-04-10 18:32:35 +00001696 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
1697 SkIntToScalar(src.height()));
reed@google.com8926b162012-03-23 15:36:36 +00001698 GrTexture* resultTexture = filter_texture(fContext, texture, filter, rect);
1699 if (resultTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001700 result->setPixelRef(SkNEW_ARGS(SkGrTexturePixelRef,
1701 (resultTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001702 resultTexture->unref();
1703 }
reed@google.com76dd2772012-01-05 21:15:07 +00001704 return true;
1705}
1706
reed@google.comac10a2d2010-12-22 21:39:39 +00001707///////////////////////////////////////////////////////////////////////////////
1708
1709// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001710static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001711 kTriangles_GrPrimitiveType,
1712 kTriangleStrip_GrPrimitiveType,
1713 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001714};
1715
1716void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1717 int vertexCount, const SkPoint vertices[],
1718 const SkPoint texs[], const SkColor colors[],
1719 SkXfermode* xmode,
1720 const uint16_t indices[], int indexCount,
1721 const SkPaint& paint) {
1722 CHECK_SHOULD_DRAW(draw);
1723
bsalomon@google.com5782d712011-01-21 21:03:59 +00001724 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001725 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com5782d712011-01-21 21:03:59 +00001726 // we ignore the shader if texs is null.
1727 if (NULL == texs) {
twiz@google.com58071162012-07-18 21:41:50 +00001728 if (!skPaint2GrPaintNoShader(this,
1729 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001730 false,
1731 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001732 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +00001733 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001734 return;
1735 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001736 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001737 if (!skPaint2GrPaintShader(this,
1738 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001739 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001740 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001741 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001742 return;
1743 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001744 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001745
1746 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001747 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001748 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1749#if 0
1750 return
1751#endif
1752 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001753 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001754
bsalomon@google.com498776a2011-08-16 19:20:44 +00001755 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1756 if (NULL != colors) {
1757 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001758 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001759 for (int i = 0; i < vertexCount; ++i) {
rileya@google.com24f3ad12012-07-18 21:47:40 +00001760 convertedColors[i] = SkColor2GrColor(colors[i]);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001761 }
1762 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001763 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001764 fContext->drawVertices(grPaint,
1765 gVertexMode2PrimitiveType[vmode],
1766 vertexCount,
1767 (GrPoint*) vertices,
1768 (GrPoint*) texs,
1769 colors,
1770 indices,
1771 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001772}
1773
1774///////////////////////////////////////////////////////////////////////////////
1775
1776static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001777 GrFontScaler* scaler = (GrFontScaler*)data;
1778 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001779}
1780
1781static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1782 void* auxData;
1783 GrFontScaler* scaler = NULL;
1784 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1785 scaler = (GrFontScaler*)auxData;
1786 }
1787 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001788 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001789 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1790 }
1791 return scaler;
1792}
1793
1794static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1795 SkFixed fx, SkFixed fy,
1796 const SkGlyph& glyph) {
1797 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1798
bungeman@google.com15865a72012-01-11 16:28:04 +00001799 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001800
1801 if (NULL == procs->fFontScaler) {
1802 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1803 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001804
bungeman@google.com15865a72012-01-11 16:28:04 +00001805 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1806 glyph.getSubXFixed(),
1807 glyph.getSubYFixed()),
1808 SkFixedFloorToFixed(fx),
1809 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001810 procs->fFontScaler);
1811}
1812
bsalomon@google.com5782d712011-01-21 21:03:59 +00001813SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001814
1815 // deferred allocation
1816 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001817 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001818 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1819 fDrawProcs->fContext = fContext;
1820 }
1821
1822 // init our (and GL's) state
1823 fDrawProcs->fTextContext = context;
1824 fDrawProcs->fFontScaler = NULL;
1825 return fDrawProcs;
1826}
1827
1828void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1829 size_t byteLength, SkScalar x, SkScalar y,
1830 const SkPaint& paint) {
1831 CHECK_SHOULD_DRAW(draw);
1832
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001833 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001834 // this guy will just call our drawPath()
1835 draw.drawText((const char*)text, byteLength, x, y, paint);
1836 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001837 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001838
1839 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001840 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001841 if (!skPaint2GrPaintShader(this,
1842 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001843 true,
twiz@google.com58071162012-07-18 21:41:50 +00001844 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001845 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001846 return;
1847 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001848 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1849 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001850 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1851 }
1852}
1853
1854void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1855 size_t byteLength, const SkScalar pos[],
1856 SkScalar constY, int scalarsPerPos,
1857 const SkPaint& paint) {
1858 CHECK_SHOULD_DRAW(draw);
1859
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001860 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001861 // this guy will just call our drawPath()
1862 draw.drawPosText((const char*)text, byteLength, pos, constY,
1863 scalarsPerPos, paint);
1864 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001865 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001866
1867 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001868 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001869 if (!skPaint2GrPaintShader(this,
1870 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001871 true,
twiz@google.com58071162012-07-18 21:41:50 +00001872 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001873 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001874 return;
1875 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001876 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1877 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001878 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1879 scalarsPerPos, paint);
1880 }
1881}
1882
1883void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1884 size_t len, const SkPath& path,
1885 const SkMatrix* m, const SkPaint& paint) {
1886 CHECK_SHOULD_DRAW(draw);
1887
1888 SkASSERT(draw.fDevice == this);
1889 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1890}
1891
1892///////////////////////////////////////////////////////////////////////////////
1893
reed@google.comf67e4cf2011-03-15 20:56:58 +00001894bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1895 if (!paint.isLCDRenderText()) {
1896 // we're cool with the paint as is
1897 return false;
1898 }
1899
1900 if (paint.getShader() ||
1901 paint.getXfermode() || // unless its srcover
1902 paint.getMaskFilter() ||
1903 paint.getRasterizer() ||
1904 paint.getColorFilter() ||
1905 paint.getPathEffect() ||
1906 paint.isFakeBoldText() ||
1907 paint.getStyle() != SkPaint::kFill_Style) {
1908 // turn off lcd
1909 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1910 flags->fHinting = paint.getHinting();
1911 return true;
1912 }
1913 // we're cool with the paint as is
1914 return false;
1915}
1916
reed@google.com75d939b2011-12-07 15:07:23 +00001917void SkGpuDevice::flush() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001918 DO_DEFERRED_CLEAR;
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001919 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001920}
1921
reed@google.comf67e4cf2011-03-15 20:56:58 +00001922///////////////////////////////////////////////////////////////////////////////
1923
bsalomon@google.comfb309512011-11-30 14:13:48 +00001924bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001925 const GrTextureParams& params) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001926 uint64_t key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001927 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001928
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001929 GrTextureDesc desc;
1930 desc.fWidth = bitmap.width();
1931 desc.fHeight = bitmap.height();
rileya@google.com24f3ad12012-07-18 21:47:40 +00001932 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config());
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001933 desc.fClientCacheID = key;
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001934
bsalomon@google.comb8670992012-07-25 21:27:09 +00001935 return this->context()->isTextureInCache(desc, &params);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001936}
1937
1938
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001939SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1940 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001941 bool isOpaque,
1942 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001943 GrTextureDesc desc;
1944 desc.fConfig = fRenderTarget->config();
1945 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1946 desc.fWidth = width;
1947 desc.fHeight = height;
1948 desc.fSampleCnt = fRenderTarget->numSamples();
1949
1950 GrContext::TextureCacheEntry cacheEntry;
1951 GrTexture* texture;
1952 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001953 // Skia's convention is to only clear a device if it is non-opaque.
1954 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001955
1956#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1957 // layers are never draw in repeat modes, so we can request an approx
1958 // match and ignore any padding.
1959 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1960 GrContext::kApprox_ScratchTexMatch :
1961 GrContext::kExact_ScratchTexMatch;
1962 cacheEntry = fContext->lockScratchTexture(desc, matchType);
1963 texture = cacheEntry.texture();
1964#else
1965 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1966 texture = tunref.get();
1967#endif
1968 if (texture) {
1969 return SkNEW_ARGS(SkGpuDevice,(fContext,
1970 texture,
1971 cacheEntry,
1972 needClear));
1973 } else {
1974 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1975 width, height);
1976 return NULL;
1977 }
1978}
1979
1980SkGpuDevice::SkGpuDevice(GrContext* context,
1981 GrTexture* texture,
1982 TexCache cacheEntry,
1983 bool needClear)
robertphillips@google.com40a1ae42012-07-13 15:36:15 +00001984 : SkDevice(make_bitmap(context, texture->asRenderTarget()))
1985 , fClipStack(NULL) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001986 GrAssert(texture && texture->asRenderTarget());
1987 GrAssert(NULL == cacheEntry.texture() || texture == cacheEntry.texture());
1988 this->initFromRenderTarget(context, texture->asRenderTarget());
1989 fCache = cacheEntry;
1990 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001991}
1992