blob: b87089ce02230f91c803c73fb6331b688f9b1f90 [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
tomhudson@google.com2f68e762012-07-17 18:43:21 +000010#include "effects/GrTextureDomainEffect.h"
epoger@google.comec3ed6a2011-07-28 14:26:00 +000011
reed@google.comac10a2d2010-12-22 21:39:39 +000012#include "GrContext.h"
13#include "GrTextContext.h"
14
robertphillips@google.come9c04692012-06-29 00:30:13 +000015#include "SkGrTexturePixelRef.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000016
Scroggo97c88c22011-05-11 14:05:25 +000017#include "SkColorFilter.h"
senorblanco@chromium.org9c397442012-09-27 21:57:45 +000018#include "SkDeviceImageFilterProxy.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.comc9aa5872011-04-05 21:05:37 +000022#include "SkUtils.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000023
bsalomon@google.com06cd7322012-03-30 18:45:35 +000024#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
reed@google.comac10a2d2010-12-22 21:39:39 +000025
26#if 0
27 extern bool (*gShouldDrawProc)();
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +000028 #define CHECK_SHOULD_DRAW(draw, forceI) \
reed@google.comac10a2d2010-12-22 21:39:39 +000029 do { \
30 if (gShouldDrawProc && !gShouldDrawProc()) return; \
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +000031 this->prepareDraw(draw, forceI); \
reed@google.comac10a2d2010-12-22 21:39:39 +000032 } while (0)
33#else
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +000034 #define CHECK_SHOULD_DRAW(draw, forceI) this->prepareDraw(draw, forceI)
reed@google.comac10a2d2010-12-22 21:39:39 +000035#endif
36
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000037// we use the same texture slot on GrPaint for bitmaps and shaders
38// (since drawBitmap, drawSprite, and drawDevice ignore skia's shader)
39enum {
40 kBitmapTextureIdx = 0,
twiz@google.com58071162012-07-18 21:41:50 +000041 kShaderTextureIdx = 0,
42 kColorFilterTextureIdx = 1
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000043};
44
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +000045#define MAX_BLUR_SIGMA 4.0f
46// FIXME: This value comes from from SkBlurMaskFilter.cpp.
47// Should probably be put in a common header someplace.
48#define MAX_BLUR_RADIUS SkIntToScalar(128)
49// This constant approximates the scaling done in the software path's
50// "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
51// IMHO, it actually should be 1: we blur "less" than we should do
52// according to the CSS and canvas specs, simply because Safari does the same.
53// Firefox used to do the same too, until 4.0 where they fixed it. So at some
54// point we should probably get rid of these scaling constants and rebaseline
55// all the blur tests.
56#define BLUR_SIGMA_SCALE 0.6f
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000057// This constant represents the screen alignment criterion in texels for
rmistry@google.comd6176b02012-08-23 18:14:13 +000058// requiring texture domain clamping to prevent color bleeding when drawing
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000059// a sub region of a larger source image.
60#define COLOR_BLEED_TOLERANCE SkFloatToScalar(0.001f)
bsalomon@google.com06cd7322012-03-30 18:45:35 +000061
bsalomon@google.coma6926b12012-10-10 15:25:50 +000062#define DO_DEFERRED_CLEAR() \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000063 do { \
64 if (fNeedClear) { \
bsalomon@google.com730ca3b2012-04-03 13:25:12 +000065 this->clear(0x0); \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000066 } \
67 } while (false) \
68
reed@google.comac10a2d2010-12-22 21:39:39 +000069///////////////////////////////////////////////////////////////////////////////
70
reed@google.comb0a34d82012-07-11 19:57:55 +000071#define CHECK_FOR_NODRAW_ANNOTATION(paint) \
72 do { if (paint.isNoDrawAnnotation()) { return; } } while (0)
73
74///////////////////////////////////////////////////////////////////////////////
75
76
bsalomon@google.com84405e02012-03-05 19:57:21 +000077class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
78public:
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000079 SkAutoCachedTexture()
80 : fDevice(NULL)
81 , fTexture(NULL) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000082 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000083
bsalomon@google.com84405e02012-03-05 19:57:21 +000084 SkAutoCachedTexture(SkGpuDevice* device,
85 const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +000086 const GrTextureParams* params,
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000087 GrTexture** texture)
88 : fDevice(NULL)
89 , fTexture(NULL) {
90 GrAssert(NULL != texture);
bsalomon@google.comb8670992012-07-25 21:27:09 +000091 *texture = this->set(device, bitmap, params);
reed@google.comac10a2d2010-12-22 21:39:39 +000092 }
reed@google.comac10a2d2010-12-22 21:39:39 +000093
bsalomon@google.com84405e02012-03-05 19:57:21 +000094 ~SkAutoCachedTexture() {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000095 if (NULL != fTexture) {
96 GrUnlockCachedBitmapTexture(fTexture);
bsalomon@google.com84405e02012-03-05 19:57:21 +000097 }
reed@google.comac10a2d2010-12-22 21:39:39 +000098 }
bsalomon@google.com84405e02012-03-05 19:57:21 +000099
100 GrTexture* set(SkGpuDevice* device,
101 const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000102 const GrTextureParams* params) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000103 if (NULL != fTexture) {
104 GrUnlockCachedBitmapTexture(fTexture);
105 fTexture = NULL;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000106 }
107 fDevice = device;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000108 GrTexture* result = (GrTexture*)bitmap.getTexture();
109 if (NULL == result) {
110 // Cannot return the native texture so look it up in our cache
111 fTexture = GrLockCachedBitmapTexture(device->context(), bitmap, params);
112 result = fTexture;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000113 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000114 return result;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000115 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000116
bsalomon@google.com84405e02012-03-05 19:57:21 +0000117private:
118 SkGpuDevice* fDevice;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000119 GrTexture* fTexture;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000120};
reed@google.comac10a2d2010-12-22 21:39:39 +0000121
122///////////////////////////////////////////////////////////////////////////////
123
reed@google.comac10a2d2010-12-22 21:39:39 +0000124struct GrSkDrawProcs : public SkDrawProcs {
125public:
126 GrContext* fContext;
127 GrTextContext* fTextContext;
128 GrFontScaler* fFontScaler; // cached in the skia glyphcache
129};
130
131///////////////////////////////////////////////////////////////////////////////
132
reed@google.comaf951c92011-06-16 19:10:39 +0000133static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
134 switch (config) {
135 case kAlpha_8_GrPixelConfig:
136 *isOpaque = false;
137 return SkBitmap::kA8_Config;
138 case kRGB_565_GrPixelConfig:
139 *isOpaque = true;
140 return SkBitmap::kRGB_565_Config;
141 case kRGBA_4444_GrPixelConfig:
142 *isOpaque = false;
143 return SkBitmap::kARGB_4444_Config;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000144 case kSkia8888_PM_GrPixelConfig:
145 // we don't currently have a way of knowing whether
146 // a 8888 is opaque based on the config.
147 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000148 return SkBitmap::kARGB_8888_Config;
149 default:
150 *isOpaque = false;
151 return SkBitmap::kNo_Config;
152 }
153}
reed@google.comac10a2d2010-12-22 21:39:39 +0000154
reed@google.comaf951c92011-06-16 19:10:39 +0000155static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000156 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000157
158 bool isOpaque;
159 SkBitmap bitmap;
160 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
161 renderTarget->width(), renderTarget->height());
162 bitmap.setIsOpaque(isOpaque);
163 return bitmap;
164}
165
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000166SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000167: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
bsalomon@google.com8090e652012-08-28 15:07:11 +0000168 this->initFromRenderTarget(context, texture->asRenderTarget(), false);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000169}
170
reed@google.comaf951c92011-06-16 19:10:39 +0000171SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000172: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.com8090e652012-08-28 15:07:11 +0000173 this->initFromRenderTarget(context, renderTarget, false);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000174}
175
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000176void SkGpuDevice::initFromRenderTarget(GrContext* context,
bsalomon@google.com8090e652012-08-28 15:07:11 +0000177 GrRenderTarget* renderTarget,
178 bool cached) {
reed@google.comaf951c92011-06-16 19:10:39 +0000179 fDrawProcs = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000180
reed@google.comaf951c92011-06-16 19:10:39 +0000181 fContext = context;
182 fContext->ref();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000183
reed@google.comaf951c92011-06-16 19:10:39 +0000184 fRenderTarget = NULL;
185 fNeedClear = false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000186
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000187 GrAssert(NULL != renderTarget);
188 fRenderTarget = renderTarget;
189 fRenderTarget->ref();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000190
bsalomon@google.coma2921122012-08-28 12:34:17 +0000191 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
192 // on the RT but not vice-versa.
193 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
194 // busting chrome (for a currently unknown reason).
195 GrSurface* surface = fRenderTarget->asTexture();
196 if (NULL == surface) {
197 surface = fRenderTarget;
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000198 }
bsalomon@google.com8090e652012-08-28 15:07:11 +0000199 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (surface, cached));
bsalomon@google.coma2921122012-08-28 12:34:17 +0000200
reed@google.comaf951c92011-06-16 19:10:39 +0000201 this->setPixelRef(pr, 0)->unref();
202}
203
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000204SkGpuDevice::SkGpuDevice(GrContext* context,
205 SkBitmap::Config config,
206 int width,
207 int height)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000208 : SkDevice(config, width, height, false /*isOpaque*/) {
209
reed@google.comac10a2d2010-12-22 21:39:39 +0000210 fDrawProcs = NULL;
211
reed@google.com7b201d22011-01-11 18:59:23 +0000212 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000213 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000214
reed@google.comac10a2d2010-12-22 21:39:39 +0000215 fRenderTarget = NULL;
216 fNeedClear = false;
217
reed@google.comaf951c92011-06-16 19:10:39 +0000218 if (config != SkBitmap::kRGB_565_Config) {
219 config = SkBitmap::kARGB_8888_Config;
220 }
221 SkBitmap bm;
222 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000223
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000224 GrTextureDesc desc;
225 desc.fFlags = kRenderTarget_GrTextureFlagBit;
226 desc.fWidth = width;
227 desc.fHeight = height;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000228 desc.fConfig = SkBitmapConfig2GrPixelConfig(bm.config());
reed@google.comac10a2d2010-12-22 21:39:39 +0000229
bsalomon@google.coma2921122012-08-28 12:34:17 +0000230 SkAutoTUnref<GrTexture> texture(fContext->createUncachedTexture(desc, NULL, 0));
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000231
bsalomon@google.coma2921122012-08-28 12:34:17 +0000232 if (NULL != texture) {
233 fRenderTarget = texture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000234 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000235
reed@google.comaf951c92011-06-16 19:10:39 +0000236 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000237
reed@google.comaf951c92011-06-16 19:10:39 +0000238 // wrap the bitmap with a pixelref to expose our texture
bsalomon@google.coma2921122012-08-28 12:34:17 +0000239 SkGrPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (texture));
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000240 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000241 } else {
242 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
243 width, height);
244 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000245 }
246}
247
248SkGpuDevice::~SkGpuDevice() {
249 if (fDrawProcs) {
250 delete fDrawProcs;
251 }
252
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000253 // The GrContext takes a ref on the target. We don't want to cause the render
254 // target to be unnecessarily kept alive.
255 if (fContext->getRenderTarget() == fRenderTarget) {
256 fContext->setRenderTarget(NULL);
257 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000258
robertphillips@google.com055f9082012-10-24 13:24:11 +0000259 if (fContext->getClip() == &fClipData) {
260 fContext->setClip(NULL);
261 }
262
bsalomon@google.coma2921122012-08-28 12:34:17 +0000263 SkSafeUnref(fRenderTarget);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000264 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000265}
266
reed@google.comac10a2d2010-12-22 21:39:39 +0000267///////////////////////////////////////////////////////////////////////////////
268
269void SkGpuDevice::makeRenderTargetCurrent() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000270 DO_DEFERRED_CLEAR();
reed@google.comac10a2d2010-12-22 21:39:39 +0000271 fContext->setRenderTarget(fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000272}
273
274///////////////////////////////////////////////////////////////////////////////
275
bsalomon@google.comc4364992011-11-07 15:54:49 +0000276namespace {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000277GrPixelConfig config8888_to_grconfig_and_flags(SkCanvas::Config8888 config8888, uint32_t* flags) {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000278 switch (config8888) {
279 case SkCanvas::kNative_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000280 *flags = 0;
281 return kSkia8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000282 case SkCanvas::kNative_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000283 *flags = GrContext::kUnpremul_PixelOpsFlag;
284 return kSkia8888_PM_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000285 case SkCanvas::kBGRA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000286 *flags = 0;
287 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000288 case SkCanvas::kBGRA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000289 *flags = GrContext::kUnpremul_PixelOpsFlag;
290 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000291 case SkCanvas::kRGBA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000292 *flags = 0;
293 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000294 case SkCanvas::kRGBA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000295 *flags = GrContext::kUnpremul_PixelOpsFlag;
296 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000297 default:
298 GrCrash("Unexpected Config8888.");
bsalomon@google.comccaa0022012-09-25 19:55:07 +0000299 *flags = 0; // suppress warning
bsalomon@google.comc4364992011-11-07 15:54:49 +0000300 return kSkia8888_PM_GrPixelConfig;
301 }
302}
303}
304
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000305bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
306 int x, int y,
307 SkCanvas::Config8888 config8888) {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000308 DO_DEFERRED_CLEAR();
bsalomon@google.com910267d2011-11-02 20:06:25 +0000309 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
310 SkASSERT(!bitmap.isNull());
311 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000312
bsalomon@google.com910267d2011-11-02 20:06:25 +0000313 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000314 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000315 uint32_t flags;
316 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000317 return fContext->readRenderTargetPixels(fRenderTarget,
318 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000319 bitmap.width(),
320 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000321 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000322 bitmap.getPixels(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000323 bitmap.rowBytes(),
324 flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000325}
326
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000327void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
328 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000329 SkAutoLockPixels alp(bitmap);
330 if (!bitmap.readyToDraw()) {
331 return;
332 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000333
334 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000335 uint32_t flags;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000336 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000337 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000338 } else {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000339 flags = 0;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000340 config= SkBitmapConfig2GrPixelConfig(bitmap.config());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000341 }
342
bsalomon@google.com6f379512011-11-16 20:36:03 +0000343 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000344 config, bitmap.getPixels(), bitmap.rowBytes(), flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000345}
346
robertphillips@google.com46f93502012-08-07 15:38:08 +0000347namespace {
348void purgeClipCB(int genID, void* data) {
349 GrContext* context = (GrContext*) data;
350
351 if (SkClipStack::kInvalidGenID == genID ||
352 SkClipStack::kEmptyGenID == genID ||
353 SkClipStack::kWideOpenGenID == genID) {
354 // none of these cases will have a cached clip mask
355 return;
356 }
357
358}
359};
360
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000361void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
362 INHERITED::onAttachToCanvas(canvas);
363
364 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000365 fClipData.fClipStack = canvas->getClipStack();
robertphillips@google.com46f93502012-08-07 15:38:08 +0000366
367 fClipData.fClipStack->addPurgeClipCallback(purgeClipCB, fContext);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000368}
369
370void SkGpuDevice::onDetachFromCanvas() {
371 INHERITED::onDetachFromCanvas();
372
robertphillips@google.com46f93502012-08-07 15:38:08 +0000373 // TODO: iterate through the clip stack and clean up any cached clip masks
374 fClipData.fClipStack->removePurgeClipCallback(purgeClipCB, fContext);
375
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000376 fClipData.fClipStack = NULL;
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000377}
378
robertphillips@google.com607fe072012-07-24 13:54:00 +0000379#ifdef SK_DEBUG
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000380static void check_bounds(const GrClipData& clipData,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000381 const SkRegion& clipRegion,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000382 int renderTargetWidth,
383 int renderTargetHeight) {
384
robertphillips@google.com7b112892012-07-31 15:18:21 +0000385 SkIRect devBound;
386
387 devBound.setLTRB(0, 0, renderTargetWidth, renderTargetHeight);
388
robertphillips@google.com607fe072012-07-24 13:54:00 +0000389 SkClipStack::BoundsType boundType;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000390 SkRect canvTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000391
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000392 clipData.fClipStack->getBounds(&canvTemp, &boundType);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000393 if (SkClipStack::kNormal_BoundsType == boundType) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000394 SkIRect devTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000395
robertphillips@google.com7b112892012-07-31 15:18:21 +0000396 canvTemp.roundOut(&devTemp);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000397
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000398 devTemp.offset(-clipData.fOrigin.fX, -clipData.fOrigin.fY);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000399
robertphillips@google.com7b112892012-07-31 15:18:21 +0000400 if (!devBound.intersect(devTemp)) {
401 devBound.setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000402 }
403 }
404
robertphillips@google.com768fee82012-08-02 12:42:43 +0000405 GrAssert(devBound.contains(clipRegion.getBounds()));
robertphillips@google.com607fe072012-07-24 13:54:00 +0000406}
407#endif
408
reed@google.comac10a2d2010-12-22 21:39:39 +0000409///////////////////////////////////////////////////////////////////////////////
410
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000411// call this every draw call, to ensure that the context reflects our state,
reed@google.comac10a2d2010-12-22 21:39:39 +0000412// and not the state from some other canvas/device
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000413void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000414 GrAssert(NULL != fClipData.fClipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000415
reed@google.comac10a2d2010-12-22 21:39:39 +0000416 fContext->setRenderTarget(fRenderTarget);
417
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000418 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000419
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000420 if (forceIdentity) {
421 fContext->setIdentityMatrix();
422 } else {
423 fContext->setMatrix(*draw.fMatrix);
424 }
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000425 fClipData.fOrigin = this->getOrigin();
reed@google.comac10a2d2010-12-22 21:39:39 +0000426
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000427#ifdef SK_DEBUG
428 check_bounds(fClipData, *draw.fClip, fRenderTarget->width(), fRenderTarget->height());
429#endif
430
431 fContext->setClip(&fClipData);
432
433 DO_DEFERRED_CLEAR();
reed@google.comac10a2d2010-12-22 21:39:39 +0000434}
435
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000436SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000437 DO_DEFERRED_CLEAR();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000438 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000439}
440
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000441bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
bsalomon@google.coma2921122012-08-28 12:34:17 +0000442 GrTexture* texture = fRenderTarget->asTexture();
443 if (NULL != texture) {
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000444 paint->colorSampler(kBitmapTextureIdx)->setEffect(
bsalomon@google.coma2921122012-08-28 12:34:17 +0000445 SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000446 return true;
447 }
448 return false;
449}
450
451///////////////////////////////////////////////////////////////////////////////
452
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000453SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
454SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
455SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
456SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
457SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
458 shader_type_mismatch);
rileya@google.com3e332582012-07-03 13:43:35 +0000459SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
460 shader_type_mismatch);
rileya@google.com22e57f92012-07-19 15:16:19 +0000461SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
462SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000463
bsalomon@google.com84405e02012-03-05 19:57:21 +0000464namespace {
465
466// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
467// justAlpha indicates that skPaint's alpha should be used rather than the color
468// Callers may subsequently modify the GrPaint. Setting constantColor indicates
469// that the final paint will draw the same color at every pixel. This allows
470// an optimization where the the color filter can be applied to the skPaint's
twiz@google.com58071162012-07-18 21:41:50 +0000471// color once while converting to GrPaint and then ignored.
472inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
473 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000474 bool justAlpha,
475 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000476 SkGpuDevice::SkAutoCachedTexture* act,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000477 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000478
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000479 grPaint->setDither(skPaint.isDither());
480 grPaint->setAntiAlias(skPaint.isAntiAlias());
bsalomon@google.com5782d712011-01-21 21:03:59 +0000481
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000482 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
483 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000484
485 SkXfermode* mode = skPaint.getXfermode();
486 if (mode) {
487 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000488 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000489#if 0
490 return false;
491#endif
492 }
493 }
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000494 grPaint->setBlendFunc(sk_blend_to_grblend(sm), sk_blend_to_grblend(dm));
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000495
bsalomon@google.com5782d712011-01-21 21:03:59 +0000496 if (justAlpha) {
497 uint8_t alpha = skPaint.getAlpha();
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000498 grPaint->setColor(GrColorPackRGBA(alpha, alpha, alpha, alpha));
Scroggod757df22011-05-16 13:11:16 +0000499 // justAlpha is currently set to true only if there is a texture,
500 // so constantColor should not also be true.
501 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000502 } else {
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000503 grPaint->setColor(SkColor2GrColor(skPaint.getColor()));
bsalomon@google.com88becf42012-10-05 14:54:42 +0000504 GrAssert(!grPaint->isColorStageEnabled(kShaderTextureIdx));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000505 }
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000506
Scroggo97c88c22011-05-11 14:05:25 +0000507 SkColorFilter* colorFilter = skPaint.getColorFilter();
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000508 if (NULL != colorFilter) {
509 // if the source color is a constant then apply the filter here once rather than per pixel
510 // in a shader.
511 if (constantColor) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000512 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000513 grPaint->setColor(SkColor2GrColor(filtered));
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000514 } else {
bsalomon@google.coma469c282012-10-24 18:28:34 +0000515 SkAutoTUnref<GrEffect> stage(colorFilter->asNewCustomStage(dev->context()));
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000516 if (NULL != stage.get()) {
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000517 grPaint->colorSampler(kColorFilterTextureIdx)->setEffect(stage);
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000518 } else {
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000519 // TODO: rewrite this using asNewCustomStage()
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000520 SkColor color;
521 SkXfermode::Mode filterMode;
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000522 if (colorFilter->asColorMode(&color, &filterMode)) {
523 grPaint->setXfermodeColorFilter(filterMode, SkColor2GrColor(color));
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000524 }
525 }
Scroggod757df22011-05-16 13:11:16 +0000526 }
Scroggo97c88c22011-05-11 14:05:25 +0000527 }
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000528
bsalomon@google.com5782d712011-01-21 21:03:59 +0000529 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000530}
531
bsalomon@google.com84405e02012-03-05 19:57:21 +0000532// This function is similar to skPaint2GrPaintNoShader but also converts
533// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
534// be used is set on grPaint and returned in param act. constantColor has the
535// same meaning as in skPaint2GrPaintNoShader.
536inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
537 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000538 bool constantColor,
bsalomon@google.com88becf42012-10-05 14:54:42 +0000539 SkGpuDevice::SkAutoCachedTexture textures[GrPaint::kMaxColorStages],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000540 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000541 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000542 if (NULL == shader) {
twiz@google.com58071162012-07-18 21:41:50 +0000543 return skPaint2GrPaintNoShader(dev,
544 skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000545 false,
546 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000547 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000548 grPaint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000549 } else if (!skPaint2GrPaintNoShader(dev, skPaint, true, false,
twiz@google.com58071162012-07-18 21:41:50 +0000550 &textures[kColorFilterTextureIdx], grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000551 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000552 }
553
bsalomon@google.com88becf42012-10-05 14:54:42 +0000554 GrSamplerState* sampler = grPaint->colorSampler(kShaderTextureIdx);
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000555 if (shader->asNewCustomStage(dev->context(), sampler)) {
rileya@google.com91f319c2012-07-25 17:18:31 +0000556 return true;
557 }
558
reed@google.comac10a2d2010-12-22 21:39:39 +0000559 SkBitmap bitmap;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000560 SkMatrix matrix;
reed@google.comac10a2d2010-12-22 21:39:39 +0000561 SkShader::TileMode tileModes[2];
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000562 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, &matrix, tileModes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000563
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000564 if (SkShader::kNone_BitmapType == bmptype) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000565 SkShader::GradientInfo info;
566 SkColor color;
567
568 info.fColors = &color;
569 info.fColorOffsets = NULL;
570 info.fColorCount = 1;
571 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
572 SkPaint copy(skPaint);
573 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000574 // modulate the paint alpha by the shader's solid color alpha
575 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
576 copy.setColor(SkColorSetA(color, newA));
twiz@google.com58071162012-07-18 21:41:50 +0000577 return skPaint2GrPaintNoShader(dev,
578 copy,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000579 false,
580 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000581 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000582 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000583 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000584 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000585 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000586
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000587 // Must set wrap and filter on the sampler before requesting a texture.
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000588 GrTextureParams params(tileModes, skPaint.isFilterBitmap());
589 GrTexture* texture = textures[kShaderTextureIdx].set(dev, bitmap, &params);
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000590
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000591 if (NULL == texture) {
592 SkDebugf("Couldn't convert bitmap to texture.\n");
593 return false;
594 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000595
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000596 // since our texture coords will be in local space, we whack the texture
reed@google.comac10a2d2010-12-22 21:39:39 +0000597 // matrix to map them back into 0...1 before we load it
598 SkMatrix localM;
599 if (shader->getLocalMatrix(&localM)) {
600 SkMatrix inverse;
601 if (localM.invert(&inverse)) {
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000602 matrix.preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000603 }
604 }
605 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000606 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
607 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000608 matrix.postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000609 }
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000610 sampler->setEffect(SkNEW_ARGS(GrSingleTextureEffect, (texture, params)), matrix)->unref();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000611
612 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000613}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000614}
reed@google.comac10a2d2010-12-22 21:39:39 +0000615
616///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000617void SkGpuDevice::clear(SkColor color) {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000618 fContext->clear(NULL, color, fRenderTarget);
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000619 fNeedClear = false;
bsalomon@google.com398109c2011-04-14 18:40:27 +0000620}
621
reed@google.comac10a2d2010-12-22 21:39:39 +0000622void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000623 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000624
bsalomon@google.com5782d712011-01-21 21:03:59 +0000625 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +0000626 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000627 if (!skPaint2GrPaintShader(this,
628 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000629 true,
twiz@google.com58071162012-07-18 21:41:50 +0000630 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000631 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000632 return;
633 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000634
635 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000636}
637
638// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000639static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000640 kPoints_GrPrimitiveType,
641 kLines_GrPrimitiveType,
642 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000643};
644
645void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000646 size_t count, const SkPoint pts[], const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000647 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000648
649 SkScalar width = paint.getStrokeWidth();
650 if (width < 0) {
651 return;
652 }
653
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000654 // we only handle hairlines and paints without path effects or mask filters,
655 // else we let the SkDraw call our drawPath()
656 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000657 draw.drawPoints(mode, count, pts, paint, true);
658 return;
659 }
660
bsalomon@google.com5782d712011-01-21 21:03:59 +0000661 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +0000662 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000663 if (!skPaint2GrPaintShader(this,
664 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000665 true,
twiz@google.com58071162012-07-18 21:41:50 +0000666 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000667 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000668 return;
669 }
670
bsalomon@google.com5782d712011-01-21 21:03:59 +0000671 fContext->drawVertices(grPaint,
672 gPointMode2PrimtiveType[mode],
673 count,
674 (GrPoint*)pts,
675 NULL,
676 NULL,
677 NULL,
678 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000679}
680
reed@google.comc9aa5872011-04-05 21:05:37 +0000681///////////////////////////////////////////////////////////////////////////////
682
reed@google.comac10a2d2010-12-22 21:39:39 +0000683void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000684 const SkPaint& paint) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000685 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000686 CHECK_SHOULD_DRAW(draw, false);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000687
bungeman@google.com79bd8772011-07-18 15:34:08 +0000688 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000689 SkScalar width = paint.getStrokeWidth();
690
691 /*
692 We have special code for hairline strokes, miter-strokes, and fills.
693 Anything else we just call our path code.
694 */
695 bool usePath = doStroke && width > 0 &&
696 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000697 // another two reasons we might need to call drawPath...
698 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000699 usePath = true;
700 }
reed@google.com67db6642011-05-26 11:46:35 +0000701 // until we aa rotated rects...
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000702 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
reed@google.com67db6642011-05-26 11:46:35 +0000703 usePath = true;
704 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000705 // small miter limit means right angles show bevel...
706 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
707 paint.getStrokeMiter() < SK_ScalarSqrt2)
708 {
709 usePath = true;
710 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000711 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000712 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
713 usePath = true;
714 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000715
716 if (usePath) {
717 SkPath path;
718 path.addRect(rect);
719 this->drawPath(draw, path, paint, NULL, true);
720 return;
721 }
722
723 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +0000724 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000725 if (!skPaint2GrPaintShader(this,
726 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000727 true,
twiz@google.com58071162012-07-18 21:41:50 +0000728 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000729 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000730 return;
731 }
reed@google.com20efde72011-05-09 17:00:02 +0000732 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000733}
734
reed@google.com69302852011-02-16 18:08:07 +0000735#include "SkMaskFilter.h"
736#include "SkBounder.h"
737
bsalomon@google.com85003222012-03-28 14:44:37 +0000738///////////////////////////////////////////////////////////////////////////////
739
740// helpers for applying mask filters
741namespace {
742
743GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000744 switch (fillType) {
745 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000746 return kWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000747 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000748 return kEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000749 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000750 return kInverseWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000751 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000752 return kInverseEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000753 default:
754 SkDebugf("Unsupported path fill type\n");
bsalomon@google.com47059542012-06-06 20:51:20 +0000755 return kHairLine_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000756 }
757}
758
bsalomon@google.com85003222012-03-28 14:44:37 +0000759// We prefer to blur small rect with small radius via CPU.
760#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
761#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
762inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
763 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
764 rect.height() <= MIN_GPU_BLUR_SIZE &&
765 radius <= MIN_GPU_BLUR_RADIUS) {
766 return true;
767 }
768 return false;
769}
770
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000771bool drawWithGPUMaskFilter(GrContext* context, const SkPath& devPath,
772 SkMaskFilter* filter, const SkRegion& clip,
773 SkBounder* bounder, GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000774#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000775 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000776#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000777 SkMaskFilter::BlurInfo info;
778 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000779 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000780 return false;
781 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000782 SkScalar radius = info.fIgnoreTransform ? info.fRadius
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000783 : context->getMatrix().mapRadius(info.fRadius);
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000784 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000785 if (radius <= 0) {
786 return false;
787 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000788
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000789 SkRect srcRect = devPath.getBounds();
bsalomon@google.com85003222012-03-28 14:44:37 +0000790 if (shouldDrawBlurWithCPU(srcRect, radius)) {
791 return false;
792 }
793
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000794 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000795 float sigma3 = sigma * 3.0f;
796
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000797 SkRect clipRect;
798 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000799
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000800 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000801 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
802 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000803 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000804 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000805 SkIRect finalIRect;
806 finalRect.roundOut(&finalIRect);
807 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000808 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000809 }
810 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000811 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000812 }
813 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000814 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000815 GrTextureDesc desc;
816 desc.fFlags = kRenderTarget_GrTextureFlagBit;
817 desc.fWidth = SkScalarCeilToInt(srcRect.width());
818 desc.fHeight = SkScalarCeilToInt(srcRect.height());
819 // We actually only need A8, but it often isn't supported as a
820 // render target so default to RGBA_8888
bsalomon@google.com0342a852012-08-20 19:22:38 +0000821 desc.fConfig = kRGBA_8888_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000822
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000823 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
824 desc.fConfig = kAlpha_8_GrPixelConfig;
825 }
826
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000827 GrAutoScratchTexture pathEntry(context, desc);
828 GrTexture* pathTexture = pathEntry.texture();
829 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000830 return false;
831 }
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000832
robertphillips@google.comccb39502012-10-01 18:25:13 +0000833 SkAutoTUnref<GrTexture> blurTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000834
robertphillips@google.comccb39502012-10-01 18:25:13 +0000835 {
836 GrContext::AutoRenderTarget art(context, pathTexture->asRenderTarget());
837 GrContext::AutoClip ac(context, srcRect);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000838
robertphillips@google.comccb39502012-10-01 18:25:13 +0000839 context->clear(NULL, 0);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000840
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000841 GrPaint tempPaint;
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000842 if (grp->isAntiAlias()) {
843 tempPaint.setAntiAlias(true);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000844 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
845 // blend coeff of zero requires dual source blending support in order
846 // to properly blend partially covered pixels. This means the AA
847 // code path may not be taken. So we use a dst blend coeff of ISA. We
848 // could special case AA draws to a dst surface with known alpha=0 to
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000849 // use a zero dst coeff when dual source blending isn't available.f
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000850 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000851 }
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000852
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000853 GrContext::AutoMatrix am;
854
855 // Draw hard shadow to pathTexture with path top-left at origin using tempPaint.
856 GrMatrix translate;
857 translate.setTranslate(offset.fX, offset.fY);
858 am.set(context, translate);
859 context->drawPath(tempPaint, devPath, pathFillType);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000860
861 // If we're doing a normal blur, we can clobber the pathTexture in the
862 // gaussianBlur. Otherwise, we need to save it for later compositing.
863 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
skia.committer@gmail.comdc3a4e52012-10-02 02:01:24 +0000864 blurTexture.reset(context->gaussianBlur(pathTexture, isNormalBlur,
robertphillips@google.comccb39502012-10-01 18:25:13 +0000865 srcRect, sigma, sigma));
robertphillips@google.com7d126752012-10-19 12:56:26 +0000866 if (NULL == blurTexture) {
867 return false;
868 }
robertphillips@google.comccb39502012-10-01 18:25:13 +0000869
870 if (!isNormalBlur) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000871 context->setIdentityMatrix();
robertphillips@google.comccb39502012-10-01 18:25:13 +0000872 GrPaint paint;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000873 GrMatrix matrix;
874 matrix.setIDiv(pathTexture->width(), pathTexture->height());
robertphillips@google.comccb39502012-10-01 18:25:13 +0000875 // Blend pathTexture over blurTexture.
876 context->setRenderTarget(blurTexture->asRenderTarget());
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000877 paint.colorSampler(0)->setEffect(SkNEW_ARGS(GrSingleTextureEffect, (pathTexture)), matrix)->unref();
robertphillips@google.comccb39502012-10-01 18:25:13 +0000878 if (SkMaskFilter::kInner_BlurType == blurType) {
879 // inner: dst = dst * src
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000880 paint.setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000881 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
882 // solid: dst = src + dst - src * dst
883 // = (1 - dst) * src + 1 * dst
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000884 paint.setBlendFunc(kIDC_GrBlendCoeff, kOne_GrBlendCoeff);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000885 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
886 // outer: dst = dst * (1 - src)
887 // = 0 * src + (1 - src) * dst
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000888 paint.setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000889 }
890 context->drawRect(paint, srcRect);
891 }
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000892 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000893
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000894 GrContext::AutoMatrix am;
895 if (!am.setIdentity(context, grp)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000896 return false;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000897 }
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +0000898
bsalomon@google.com88becf42012-10-05 14:54:42 +0000899 static const int MASK_IDX = GrPaint::kMaxCoverageStages - 1;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000900 // we assume the last mask index is available for use
bsalomon@google.com88becf42012-10-05 14:54:42 +0000901 GrAssert(!grp->isCoverageStageEnabled(MASK_IDX));
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000902
903 GrMatrix matrix;
904 matrix.setTranslate(-finalRect.fLeft, -finalRect.fTop);
905 matrix.postIDiv(blurTexture->width(), blurTexture->height());
906
bsalomon@google.com88becf42012-10-05 14:54:42 +0000907 grp->coverageSampler(MASK_IDX)->reset();
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000908 grp->coverageSampler(MASK_IDX)->setEffect(SkNEW_ARGS(GrSingleTextureEffect, (blurTexture)), matrix)->unref();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000909 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000910 return true;
911}
912
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000913bool drawWithMaskFilter(GrContext* context, const SkPath& devPath,
914 SkMaskFilter* filter, const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000915 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000916 SkMask srcM, dstM;
917
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000918 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
919 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
reed@google.com69302852011-02-16 18:08:07 +0000920 return false;
921 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000922 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000923
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000924 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
reed@google.com69302852011-02-16 18:08:07 +0000925 return false;
926 }
927 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000928 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000929
930 if (clip.quickReject(dstM.fBounds)) {
931 return false;
932 }
933 if (bounder && !bounder->doIRect(dstM.fBounds)) {
934 return false;
935 }
936
937 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000938 // the current clip (and identity matrix) and GrPaint settings
939 GrContext::AutoMatrix am;
940 am.setIdentity(context, grp);
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000941
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000942 GrTextureDesc desc;
943 desc.fWidth = dstM.fBounds.width();
944 desc.fHeight = dstM.fBounds.height();
945 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +0000946
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000947 GrAutoScratchTexture ast(context, desc);
948 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000949
reed@google.com69302852011-02-16 18:08:07 +0000950 if (NULL == texture) {
951 return false;
952 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000953 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000954 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000955
bsalomon@google.com88becf42012-10-05 14:54:42 +0000956 static const int MASK_IDX = GrPaint::kMaxCoverageStages - 1;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000957 // we assume the last mask index is available for use
bsalomon@google.com88becf42012-10-05 14:54:42 +0000958 GrAssert(!grp->isCoverageStageEnabled(MASK_IDX));
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000959
960 GrMatrix m;
961 m.setTranslate(-dstM.fBounds.fLeft*SK_Scalar1, -dstM.fBounds.fTop*SK_Scalar1);
962 m.postIDiv(texture->width(), texture->height());
963
bsalomon@google.com6f261be2012-10-24 19:07:10 +0000964 grp->coverageSampler(MASK_IDX)->setEffect(SkNEW_ARGS(GrSingleTextureEffect, (texture)), m)->unref();
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000965 GrRect d;
966 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +0000967 GrIntToScalar(dstM.fBounds.fTop),
968 GrIntToScalar(dstM.fBounds.fRight),
969 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000970
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000971 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000972 return true;
973}
reed@google.com69302852011-02-16 18:08:07 +0000974
bsalomon@google.com85003222012-03-28 14:44:37 +0000975}
976
977///////////////////////////////////////////////////////////////////////////////
978
reed@google.com0c219b62011-02-16 21:31:18 +0000979void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000980 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000981 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000982 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000983 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000984
reed@google.comfe626382011-09-21 13:50:35 +0000985 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000986
bsalomon@google.com5782d712011-01-21 21:03:59 +0000987 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +0000988 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000989 if (!skPaint2GrPaintShader(this,
990 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000991 true,
twiz@google.com58071162012-07-18 21:41:50 +0000992 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000993 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000994 return;
995 }
996
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000997 // can we cheat, and threat a thin stroke as a hairline w/ coverage
998 // if we can, we draw lots faster (raster device does this same test)
999 SkScalar hairlineCoverage;
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001000 if (SkDrawTreatAsHairline(paint, fContext->getMatrix(), &hairlineCoverage)) {
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +00001001 doFill = false;
bsalomon@google.comc7448ce2012-10-05 19:04:13 +00001002 grPaint.setCoverage(SkScalarRoundToInt(hairlineCoverage * grPaint.getCoverage()));
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +00001003 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001004
reed@google.comfe626382011-09-21 13:50:35 +00001005 // If we have a prematrix, apply it to the path, optimizing for the case
1006 // where the original path can in fact be modified in place (even though
1007 // its parameter type is const).
1008 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1009 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001010
1011 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001012 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001013
reed@google.come3445642011-02-16 23:20:39 +00001014 if (!pathIsMutable) {
1015 result = &tmpPath;
1016 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001017 }
reed@google.come3445642011-02-16 23:20:39 +00001018 // should I push prePathMatrix on our MV stack temporarily, instead
1019 // of applying it here? See SkDraw.cpp
1020 pathPtr->transform(*prePathMatrix, result);
1021 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001022 }
reed@google.com0c219b62011-02-16 21:31:18 +00001023 // at this point we're done with prePathMatrix
1024 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001025
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001026 if (paint.getPathEffect() ||
1027 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001028 // it is safe to use tmpPath here, even if we already used it for the
1029 // prepathmatrix, since getFillPath can take the same object for its
1030 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001031 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001032 pathPtr = &tmpPath;
1033 }
1034
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001035 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001036 // avoid possibly allocating a new path in transform if we can
1037 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1038
1039 // transform the path into device space
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001040 pathPtr->transform(fContext->getMatrix(), devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001041 GrPathFill pathFillType = doFill ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001042 skToGrFillType(devPathPtr->getFillType()) : kHairLine_GrPathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001043 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001044 *draw.fClip, draw.fBounder, &grPaint, pathFillType)) {
rmistry@google.comd6176b02012-08-23 18:14:13 +00001045 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001046 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001047 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001048 *draw.fClip, draw.fBounder, &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001049 }
reed@google.com69302852011-02-16 18:08:07 +00001050 return;
1051 }
reed@google.com69302852011-02-16 18:08:07 +00001052
bsalomon@google.com47059542012-06-06 20:51:20 +00001053 GrPathFill fill = kHairLine_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001054
reed@google.com0c219b62011-02-16 21:31:18 +00001055 if (doFill) {
1056 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001057 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001058 fill = kWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001059 break;
1060 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001061 fill = kEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001062 break;
1063 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001064 fill = kInverseWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001065 break;
1066 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001067 fill = kInverseEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001068 break;
1069 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001070 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001071 return;
1072 }
1073 }
1074
reed@google.com07f3ee12011-05-16 17:21:57 +00001075 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001076}
1077
bsalomon@google.comfb309512011-11-30 14:13:48 +00001078namespace {
1079
1080inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1081 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1082 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1083 return tilesX * tilesY;
1084}
1085
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001086inline int determine_tile_size(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001087 const SkRect& src,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001088 int maxTextureSize) {
1089 static const int kSmallTileSize = 1 << 10;
1090 if (maxTextureSize <= kSmallTileSize) {
1091 return maxTextureSize;
1092 }
1093
1094 size_t maxTexTotalTileSize;
1095 size_t smallTotalTileSize;
1096
robertphillips@google.combac6b052012-09-28 18:06:49 +00001097 SkIRect iSrc;
1098 src.roundOut(&iSrc);
1099
1100 maxTexTotalTileSize = get_tile_count(iSrc.fLeft,
1101 iSrc.fTop,
1102 iSrc.fRight,
1103 iSrc.fBottom,
1104 maxTextureSize);
1105 smallTotalTileSize = get_tile_count(iSrc.fLeft,
1106 iSrc.fTop,
1107 iSrc.fRight,
1108 iSrc.fBottom,
1109 kSmallTileSize);
1110
bsalomon@google.comfb309512011-11-30 14:13:48 +00001111 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1112 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1113
1114 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1115 return kSmallTileSize;
1116 } else {
1117 return maxTextureSize;
1118 }
1119}
1120}
1121
1122bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001123 const GrTextureParams& params,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001124 const SkRect* srcRectPtr) const {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001125 // if bitmap is explictly texture backed then just use the texture
1126 if (NULL != bitmap.getTexture()) {
1127 return false;
1128 }
1129 // if it's larger than the max texture size, then we have no choice but
1130 // tiling
1131 const int maxTextureSize = fContext->getMaxTextureSize();
1132 if (bitmap.width() > maxTextureSize ||
1133 bitmap.height() > maxTextureSize) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001134 return true;
1135 }
1136 // if we are going to have to draw the whole thing, then don't tile
1137 if (NULL == srcRectPtr) {
1138 return false;
1139 }
1140 // if the entire texture is already in our cache then no reason to tile it
bsalomon@google.comb8670992012-07-25 21:27:09 +00001141 if (this->isBitmapInTextureCache(bitmap, params)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001142 return false;
1143 }
1144
1145 // At this point we know we could do the draw by uploading the entire bitmap
1146 // as a texture. However, if the texture would be large compared to the
1147 // cache size and we don't require most of it for this draw then tile to
1148 // reduce the amount of upload and cache spill.
1149
1150 // assumption here is that sw bitmap size is a good proxy for its size as
1151 // a texture
1152 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001153 size_t cacheSize;
1154 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001155 if (bmpSize < cacheSize / 2) {
1156 return false;
1157 }
1158
robertphillips@google.combac6b052012-09-28 18:06:49 +00001159 SkScalar fracUsed = SkScalarMul(srcRectPtr->width() / bitmap.width(),
1160 srcRectPtr->height() / bitmap.height());
1161 if (fracUsed <= SK_ScalarHalf) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001162 return true;
1163 } else {
1164 return false;
1165 }
1166}
1167
reed@google.comac10a2d2010-12-22 21:39:39 +00001168void SkGpuDevice::drawBitmap(const SkDraw& draw,
1169 const SkBitmap& bitmap,
1170 const SkIRect* srcRectPtr,
1171 const SkMatrix& m,
1172 const SkPaint& paint) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001173
1174 SkRect tmp;
1175 SkRect* tmpPtr = NULL;
1176
1177 // convert from SkIRect to SkRect
1178 if (NULL != srcRectPtr) {
1179 tmp.set(*srcRectPtr);
1180 tmpPtr = &tmp;
1181 }
1182
1183 // We cannot call drawBitmapRect here since 'm' could be anything
1184 this->drawBitmapCommon(draw, bitmap, tmpPtr, m, paint);
1185}
1186
1187void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1188 const SkBitmap& bitmap,
1189 const SkRect* srcRectPtr,
1190 const SkMatrix& m,
1191 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001192 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001193
robertphillips@google.combac6b052012-09-28 18:06:49 +00001194 SkRect srcRect;
reed@google.comac10a2d2010-12-22 21:39:39 +00001195 if (NULL == srcRectPtr) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001196 srcRect.set(0, 0, SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001197 } else {
1198 srcRect = *srcRectPtr;
1199 }
1200
junov@google.comd935cfb2011-06-27 20:48:23 +00001201 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001202 // Convert the bitmap to a shader so that the rect can be drawn
1203 // through drawRect, which supports mask filters.
robertphillips@google.combac6b052012-09-28 18:06:49 +00001204 SkMatrix newM(m);
junov@google.com1d329782011-07-28 20:10:09 +00001205 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001206 const SkBitmap* bitmapPtr = &bitmap;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001207 if (NULL != srcRectPtr) {
1208 SkIRect iSrc;
1209 srcRect.roundOut(&iSrc);
1210 if (!bitmap.extractSubset(&tmp, iSrc)) {
epoger@google.com9ef2d832011-07-01 21:12:20 +00001211 return; // extraction failed
1212 }
1213 bitmapPtr = &tmp;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001214 srcRect.offset(SkIntToScalar(-iSrc.fLeft), SkIntToScalar(-iSrc.fTop));
1215 // The source rect has changed so update the matrix
1216 newM.preTranslate(SkIntToScalar(iSrc.fLeft), SkIntToScalar(iSrc.fTop));
junov@google.comd935cfb2011-06-27 20:48:23 +00001217 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001218
junov@google.comd935cfb2011-06-27 20:48:23 +00001219 SkPaint paintWithTexture(paint);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001220 paintWithTexture.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
junov@google.comd935cfb2011-06-27 20:48:23 +00001221 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001222
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001223 // Transform 'newM' needs to be concatenated to the current matrix,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001224 // rather than transforming the primitive directly, so that 'newM' will
junov@google.com1d329782011-07-28 20:10:09 +00001225 // also affect the behavior of the mask filter.
1226 SkMatrix drawMatrix;
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001227 drawMatrix.setConcat(fContext->getMatrix(), newM);
junov@google.com1d329782011-07-28 20:10:09 +00001228 SkDraw transformedDraw(draw);
1229 transformedDraw.fMatrix = &drawMatrix;
1230
robertphillips@google.combac6b052012-09-28 18:06:49 +00001231 this->drawRect(transformedDraw, srcRect, paintWithTexture);
junov@google.com1d329782011-07-28 20:10:09 +00001232
junov@google.comd935cfb2011-06-27 20:48:23 +00001233 return;
1234 }
1235
bsalomon@google.com5782d712011-01-21 21:03:59 +00001236 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001237 SkAutoCachedTexture colorLutTexture;
1238 if (!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001239 return;
1240 }
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001241 GrTextureParams params;
1242 params.setBilerp(paint.isFilterBitmap());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001243
robertphillips@google.combac6b052012-09-28 18:06:49 +00001244 if (!this->shouldTileBitmap(bitmap, params, srcRectPtr)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001245 // take the simple case
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001246 this->internalDrawBitmap(bitmap, srcRect, m, params, &grPaint);
robertphillips@google.combac6b052012-09-28 18:06:49 +00001247 } else {
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001248 this->drawTiledBitmap(bitmap, srcRect, m, params, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001249 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001250}
1251
1252// Break 'bitmap' into several tiles to draw it since it has already
1253// been determined to be too large to fit in VRAM
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001254void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001255 const SkRect& srcRect,
1256 const SkMatrix& m,
1257 const GrTextureParams& params,
1258 GrPaint* grPaint) {
1259 const int maxTextureSize = fContext->getMaxTextureSize();
1260
1261 int tileSize = determine_tile_size(bitmap, srcRect, maxTextureSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001262
reed@google.comac10a2d2010-12-22 21:39:39 +00001263 // compute clip bounds in local coordinates
robertphillips@google.combac6b052012-09-28 18:06:49 +00001264 SkRect clipRect;
reed@google.comac10a2d2010-12-22 21:39:39 +00001265 {
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001266 const GrRenderTarget* rt = fContext->getRenderTarget();
1267 clipRect.setWH(SkIntToScalar(rt->width()), SkIntToScalar(rt->height()));
1268 if (!fContext->getClip()->fClipStack->intersectRectWithClip(&clipRect)) {
1269 return;
1270 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001271 SkMatrix matrix, inverse;
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001272 matrix.setConcat(fContext->getMatrix(), m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001273 if (!matrix.invert(&inverse)) {
1274 return;
1275 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001276 inverse.mapRect(&clipRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001277 }
1278
bsalomon@google.comfb309512011-11-30 14:13:48 +00001279 int nx = bitmap.width() / tileSize;
1280 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001281 for (int x = 0; x <= nx; x++) {
1282 for (int y = 0; y <= ny; y++) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001283 SkRect tileR;
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001284 tileR.set(SkIntToScalar(x * tileSize),
robertphillips@google.combac6b052012-09-28 18:06:49 +00001285 SkIntToScalar(y * tileSize),
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001286 SkIntToScalar((x + 1) * tileSize),
robertphillips@google.combac6b052012-09-28 18:06:49 +00001287 SkIntToScalar((y + 1) * tileSize));
1288
1289 if (!SkRect::Intersects(tileR, clipRect)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001290 continue;
1291 }
1292
robertphillips@google.combac6b052012-09-28 18:06:49 +00001293 if (!tileR.intersect(srcRect)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001294 continue;
1295 }
1296
1297 SkBitmap tmpB;
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001298 SkIRect iTileR;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001299 tileR.roundOut(&iTileR);
1300 if (bitmap.extractSubset(&tmpB, iTileR)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001301 // now offset it to make it "local" to our tmp bitmap
robertphillips@google.combac6b052012-09-28 18:06:49 +00001302 tileR.offset(SkIntToScalar(-iTileR.fLeft), SkIntToScalar(-iTileR.fTop));
reed@google.comac10a2d2010-12-22 21:39:39 +00001303 SkMatrix tmpM(m);
skia.committer@gmail.comdc3a4e52012-10-02 02:01:24 +00001304 tmpM.preTranslate(SkIntToScalar(iTileR.fLeft),
robertphillips@google.comffad46b2012-10-01 14:32:51 +00001305 SkIntToScalar(iTileR.fTop));
1306
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001307 this->internalDrawBitmap(tmpB, tileR, tmpM, params, grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001308 }
1309 }
1310 }
1311}
1312
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001313namespace {
1314
1315bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1316 // detect pixel disalignment
1317 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1318 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
rmistry@google.comd6176b02012-08-23 18:14:13 +00001319 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001320 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1321 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1322 COLOR_BLEED_TOLERANCE &&
1323 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1324 COLOR_BLEED_TOLERANCE) {
1325 return true;
1326 }
1327 return false;
1328}
1329
1330bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1331 const SkMatrix& m) {
1332 // Only gets called if hasAlignedSamples returned false.
1333 // So we can assume that sampling is axis aligned but not texel aligned.
1334 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001335 SkRect innerSrcRect(srcRect), innerTransformedRect,
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001336 outerTransformedRect(transformedRect);
1337 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1338 m.mapRect(&innerTransformedRect, innerSrcRect);
1339
1340 // The gap between outerTransformedRect and innerTransformedRect
1341 // represents the projection of the source border area, which is
1342 // problematic for color bleeding. We must check whether any
1343 // destination pixels sample the border area.
1344 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1345 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1346 SkIRect outer, inner;
1347 outerTransformedRect.round(&outer);
1348 innerTransformedRect.round(&inner);
1349 // If the inner and outer rects round to the same result, it means the
1350 // border does not overlap any pixel centers. Yay!
1351 return inner != outer;
1352}
1353
1354} // unnamed namespace
1355
reed@google.comac10a2d2010-12-22 21:39:39 +00001356/*
1357 * This is called by drawBitmap(), which has to handle images that may be too
1358 * large to be represented by a single texture.
1359 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001360 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1361 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001362 */
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001363void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001364 const SkRect& srcRect,
reed@google.comac10a2d2010-12-22 21:39:39 +00001365 const SkMatrix& m,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001366 const GrTextureParams& params,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001367 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001368 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1369 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001370
reed@google.com9c49bc32011-07-07 13:42:37 +00001371 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001372 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001373 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001374 return;
1375 }
1376
bsalomon@google.com88becf42012-10-05 14:54:42 +00001377 GrSamplerState* sampler = grPaint->colorSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001378
reed@google.comac10a2d2010-12-22 21:39:39 +00001379 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001380 SkAutoCachedTexture act(this, bitmap, &params, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001381 if (NULL == texture) {
1382 return;
1383 }
1384
robertphillips@google.combac6b052012-09-28 18:06:49 +00001385 GrRect dstRect(srcRect);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001386 GrRect paintRect;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001387 SkScalar wInv = SkScalarInvert(SkIntToScalar(bitmap.width()));
1388 SkScalar hInv = SkScalarInvert(SkIntToScalar(bitmap.height()));
1389 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1390 SkScalarMul(srcRect.fTop, hInv),
1391 SkScalarMul(srcRect.fRight, wInv),
1392 SkScalarMul(srcRect.fBottom, hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001393
rmistry@google.comd6176b02012-08-23 18:14:13 +00001394 bool needsTextureDomain = false;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001395 if (params.isBilerp()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001396 // Need texture domain if drawing a sub rect.
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001397 needsTextureDomain = srcRect.width() < bitmap.width() ||
robertphillips@google.combac6b052012-09-28 18:06:49 +00001398 srcRect.height() < bitmap.height();
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001399 if (m.rectStaysRect() && fContext->getMatrix().rectStaysRect()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001400 // sampling is axis-aligned
robertphillips@google.combac6b052012-09-28 18:06:49 +00001401 GrRect transformedRect;
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001402 SkMatrix srcToDeviceMatrix(m);
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001403 srcToDeviceMatrix.postConcat(fContext->getMatrix());
robertphillips@google.combac6b052012-09-28 18:06:49 +00001404 srcToDeviceMatrix.mapRect(&transformedRect, srcRect);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001405
robertphillips@google.combac6b052012-09-28 18:06:49 +00001406 if (hasAlignedSamples(srcRect, transformedRect)) {
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001407 // We could also turn off filtering here (but we already did a cache lookup with
1408 // params).
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001409 needsTextureDomain = false;
1410 } else {
1411 needsTextureDomain = needsTextureDomain &&
robertphillips@google.combac6b052012-09-28 18:06:49 +00001412 mayColorBleed(srcRect, transformedRect, m);
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001413 }
1414 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001415 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001416
1417 GrRect textureDomain = GrRect::MakeEmpty();
bsalomon@google.coma469c282012-10-24 18:28:34 +00001418 SkAutoTUnref<GrEffect> stage;
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001419 if (needsTextureDomain) {
1420 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001421 GrScalar left, top, right, bottom;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001422 if (srcRect.width() > GR_Scalar1) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001423 GrScalar border = GR_ScalarHalf / bitmap.width();
1424 left = paintRect.left() + border;
1425 right = paintRect.right() - border;
1426 } else {
1427 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1428 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001429 if (srcRect.height() > GR_Scalar1) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001430 GrScalar border = GR_ScalarHalf / bitmap.height();
1431 top = paintRect.top() + border;
1432 bottom = paintRect.bottom() - border;
1433 } else {
1434 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1435 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001436 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001437 stage.reset(SkNEW_ARGS(GrTextureDomainEffect, (texture, textureDomain, params)));
1438 } else {
1439 stage.reset(SkNEW_ARGS(GrSingleTextureEffect, (texture, params)));
junov@google.com6acc9b32011-05-16 18:32:07 +00001440 }
bsalomon@google.com6f261be2012-10-24 19:07:10 +00001441 grPaint->colorSampler(kBitmapTextureIdx)->setEffect(stage);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001442 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001443}
1444
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001445namespace {
1446
1447void apply_custom_stage(GrContext* context,
1448 GrTexture* srcTexture,
1449 GrTexture* dstTexture,
1450 const GrRect& rect,
bsalomon@google.coma469c282012-10-24 18:28:34 +00001451 GrEffect* stage) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001452 SkASSERT(srcTexture && srcTexture->getContext() == context);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001453 GrContext::AutoMatrix am;
1454 am.setIdentity(context);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001455 GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget());
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001456 GrContext::AutoClip acs(context, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001457
1458 GrMatrix sampleM;
1459 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
1460 GrPaint paint;
bsalomon@google.com6f261be2012-10-24 19:07:10 +00001461 paint.colorSampler(0)->setEffect(stage, sampleM);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001462 context->drawRect(paint, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001463}
1464
1465};
1466
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001467static GrTexture* filter_texture(SkDevice* device, GrContext* context,
1468 GrTexture* texture, SkImageFilter* filter,
1469 const GrRect& rect) {
reed@google.com8926b162012-03-23 15:36:36 +00001470 GrAssert(filter);
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001471 SkDeviceImageFilterProxy proxy(device);
reed@google.com8926b162012-03-23 15:36:36 +00001472
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001473 GrTextureDesc desc;
1474 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1475 desc.fWidth = SkScalarCeilToInt(rect.width());
1476 desc.fHeight = SkScalarCeilToInt(rect.height());
bsalomon@google.com0342a852012-08-20 19:22:38 +00001477 desc.fConfig = kRGBA_8888_GrPixelConfig;
bsalomon@google.coma469c282012-10-24 18:28:34 +00001478 GrEffect* stage;
reed@google.com8926b162012-03-23 15:36:36 +00001479
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001480 if (filter->canFilterImageGPU()) {
senorblanco@chromium.org985fa792012-10-24 15:14:26 +00001481 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1482 // filter. Also set the clip wide open and the matrix to identity.
1483 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001484 texture = filter->onFilterImageGPU(&proxy, texture, rect);
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001485 } else if (filter->asNewCustomStage(&stage, texture)) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001486 GrAutoScratchTexture dst(context, desc);
1487 apply_custom_stage(context, texture, dst.texture(), rect, stage);
1488 texture = dst.detach();
1489 stage->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001490 }
1491 return texture;
1492}
1493
reed@google.comac10a2d2010-12-22 21:39:39 +00001494void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001495 int left, int top, const SkPaint& paint) {
1496 // drawSprite is defined to be in device coords.
1497 CHECK_SHOULD_DRAW(draw, true);
reed@google.comac10a2d2010-12-22 21:39:39 +00001498
reed@google.com8926b162012-03-23 15:36:36 +00001499 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001500 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1501 return;
1502 }
1503
reed@google.com76dd2772012-01-05 21:15:07 +00001504 int w = bitmap.width();
1505 int h = bitmap.height();
1506
bsalomon@google.com5782d712011-01-21 21:03:59 +00001507 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001508 SkAutoCachedTexture colorLutTexture;
1509 if(!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001510 return;
1511 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001512
bsalomon@google.com88becf42012-10-05 14:54:42 +00001513 GrSamplerState* sampler = grPaint.colorSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001514
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001515 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001516 sampler->reset();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001517 // draw sprite uses the default texture params
1518 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
bsalomon@google.com6f261be2012-10-24 19:07:10 +00001519 grPaint.colorSampler(kBitmapTextureIdx)->setEffect(SkNEW_ARGS
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001520 (GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001521
reed@google.com8926b162012-03-23 15:36:36 +00001522 SkImageFilter* filter = paint.getImageFilter();
1523 if (NULL != filter) {
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001524 GrTexture* filteredTexture = filter_texture(this, fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001525 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001526 if (filteredTexture) {
bsalomon@google.com6f261be2012-10-24 19:07:10 +00001527 grPaint.colorSampler(kBitmapTextureIdx)->setEffect(SkNEW_ARGS
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001528 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001529 texture = filteredTexture;
1530 filteredTexture->unref();
1531 }
reed@google.com76dd2772012-01-05 21:15:07 +00001532 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001533
bsalomon@google.com5782d712011-01-21 21:03:59 +00001534 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001535 GrRect::MakeXYWH(GrIntToScalar(left),
1536 GrIntToScalar(top),
1537 GrIntToScalar(w),
1538 GrIntToScalar(h)),
1539 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1540 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001541}
1542
reed@google.com33535f32012-09-25 15:37:50 +00001543void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1544 const SkRect* src, const SkRect& dst,
1545 const SkPaint& paint) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001546 SkMatrix matrix;
1547 SkRect bitmapBounds, tmpSrc;
1548
1549 bitmapBounds.set(0, 0,
1550 SkIntToScalar(bitmap.width()),
1551 SkIntToScalar(bitmap.height()));
1552
reed@google.com33535f32012-09-25 15:37:50 +00001553 // Compute matrix from the two rectangles
robertphillips@google.combac6b052012-09-28 18:06:49 +00001554 if (NULL != src) {
1555 tmpSrc = *src;
1556 } else {
1557 tmpSrc = bitmapBounds;
1558 }
1559 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1560
1561 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1562 if (NULL != src) {
1563 if (!bitmapBounds.contains(tmpSrc)) {
1564 if (!tmpSrc.intersect(bitmapBounds)) {
1565 return; // nothing to draw
reed@google.com33535f32012-09-25 15:37:50 +00001566 }
reed@google.com33535f32012-09-25 15:37:50 +00001567 }
reed@google.com33535f32012-09-25 15:37:50 +00001568 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +00001569
robertphillips@google.combac6b052012-09-28 18:06:49 +00001570 this->drawBitmapCommon(draw, bitmap, &tmpSrc, matrix, paint);
reed@google.com33535f32012-09-25 15:37:50 +00001571}
1572
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001573void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001574 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001575 // clear of the source device must occur before CHECK_SHOULD_DRAW
1576 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1577 if (dev->fNeedClear) {
1578 // TODO: could check here whether we really need to draw at all
1579 dev->clear(0x0);
1580 }
1581
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001582 // drawDevice is defined to be in device coords.
1583 CHECK_SHOULD_DRAW(draw, true);
reed@google.comac10a2d2010-12-22 21:39:39 +00001584
bsalomon@google.com5782d712011-01-21 21:03:59 +00001585 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001586 SkAutoCachedTexture colorLutTexture;
bsalomon@google.com88becf42012-10-05 14:54:42 +00001587 grPaint.colorSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001588 if (!dev->bindDeviceAsTexture(&grPaint) ||
twiz@google.com58071162012-07-18 21:41:50 +00001589 !skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001590 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001591 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001592
bsalomon@google.com6f261be2012-10-24 19:07:10 +00001593 GrTexture* devTex = grPaint.getColorSampler(kBitmapTextureIdx).getEffect()->texture(0);
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001594 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001595
reed@google.com8926b162012-03-23 15:36:36 +00001596 SkImageFilter* filter = paint.getImageFilter();
1597 if (NULL != filter) {
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001598 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001599 SkIntToScalar(devTex->height()));
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001600 GrTexture* filteredTexture = filter_texture(this, fContext, devTex, filter, rect);
reed@google.com8926b162012-03-23 15:36:36 +00001601 if (filteredTexture) {
bsalomon@google.com6f261be2012-10-24 19:07:10 +00001602 grPaint.colorSampler(kBitmapTextureIdx)->setEffect(SkNEW_ARGS
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001603 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001604 devTex = filteredTexture;
1605 filteredTexture->unref();
1606 }
1607 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001608
bsalomon@google.com5782d712011-01-21 21:03:59 +00001609 const SkBitmap& bm = dev->accessBitmap(false);
1610 int w = bm.width();
1611 int h = bm.height();
1612
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001613 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1614 GrIntToScalar(y),
1615 GrIntToScalar(w),
1616 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001617
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001618 // The device being drawn may not fill up its texture (saveLayer uses
1619 // the approximate ).
1620 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1621 GR_Scalar1 * h / devTex->height());
1622
1623 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001624}
1625
reed@google.com8926b162012-03-23 15:36:36 +00001626bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001627 if (!filter->asNewCustomStage(NULL, NULL) &&
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001628 !filter->canFilterImageGPU()) {
reed@google.com76dd2772012-01-05 21:15:07 +00001629 return false;
1630 }
reed@google.com8926b162012-03-23 15:36:36 +00001631 return true;
1632}
1633
1634bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1635 const SkMatrix& ctm,
1636 SkBitmap* result, SkIPoint* offset) {
1637 // want explicitly our impl, so guard against a subclass of us overriding it
1638 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001639 return false;
1640 }
reed@google.com8926b162012-03-23 15:36:36 +00001641
1642 SkAutoLockPixels alp(src, !src.getTexture());
1643 if (!src.getTexture() && !src.readyToDraw()) {
1644 return false;
1645 }
1646
1647 GrPaint paint;
reed@google.com8926b162012-03-23 15:36:36 +00001648
reed@google.com8926b162012-03-23 15:36:36 +00001649 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001650 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1651 // must be pushed upstack.
1652 SkAutoCachedTexture act(this, src, NULL, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001653
1654 result->setConfig(src.config(), src.width(), src.height());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001655 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001656 SkIntToScalar(src.height()));
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001657 GrTexture* resultTexture = filter_texture(this, fContext, texture, filter, rect);
reed@google.com8926b162012-03-23 15:36:36 +00001658 if (resultTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001659 result->setPixelRef(SkNEW_ARGS(SkGrTexturePixelRef,
1660 (resultTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001661 resultTexture->unref();
1662 }
reed@google.com76dd2772012-01-05 21:15:07 +00001663 return true;
1664}
1665
reed@google.comac10a2d2010-12-22 21:39:39 +00001666///////////////////////////////////////////////////////////////////////////////
1667
1668// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001669static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001670 kTriangles_GrPrimitiveType,
1671 kTriangleStrip_GrPrimitiveType,
1672 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001673};
1674
1675void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1676 int vertexCount, const SkPoint vertices[],
1677 const SkPoint texs[], const SkColor colors[],
1678 SkXfermode* xmode,
1679 const uint16_t indices[], int indexCount,
1680 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001681 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001682
bsalomon@google.com5782d712011-01-21 21:03:59 +00001683 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +00001684 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com5782d712011-01-21 21:03:59 +00001685 // we ignore the shader if texs is null.
1686 if (NULL == texs) {
twiz@google.com58071162012-07-18 21:41:50 +00001687 if (!skPaint2GrPaintNoShader(this,
1688 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001689 false,
1690 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001691 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +00001692 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001693 return;
1694 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001695 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001696 if (!skPaint2GrPaintShader(this,
1697 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001698 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001699 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001700 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001701 return;
1702 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001703 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001704
1705 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001706 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001707 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1708#if 0
1709 return
1710#endif
1711 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001712 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001713
bsalomon@google.com498776a2011-08-16 19:20:44 +00001714 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1715 if (NULL != colors) {
1716 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001717 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001718 for (int i = 0; i < vertexCount; ++i) {
rileya@google.com24f3ad12012-07-18 21:47:40 +00001719 convertedColors[i] = SkColor2GrColor(colors[i]);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001720 }
1721 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001722 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001723 fContext->drawVertices(grPaint,
1724 gVertexMode2PrimitiveType[vmode],
1725 vertexCount,
1726 (GrPoint*) vertices,
1727 (GrPoint*) texs,
1728 colors,
1729 indices,
1730 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001731}
1732
1733///////////////////////////////////////////////////////////////////////////////
1734
1735static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001736 GrFontScaler* scaler = (GrFontScaler*)data;
1737 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001738}
1739
1740static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1741 void* auxData;
1742 GrFontScaler* scaler = NULL;
1743 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1744 scaler = (GrFontScaler*)auxData;
1745 }
1746 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001747 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001748 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1749 }
1750 return scaler;
1751}
1752
1753static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1754 SkFixed fx, SkFixed fy,
1755 const SkGlyph& glyph) {
1756 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1757
bungeman@google.com15865a72012-01-11 16:28:04 +00001758 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001759
1760 if (NULL == procs->fFontScaler) {
1761 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1762 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001763
bungeman@google.com15865a72012-01-11 16:28:04 +00001764 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1765 glyph.getSubXFixed(),
1766 glyph.getSubYFixed()),
1767 SkFixedFloorToFixed(fx),
1768 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001769 procs->fFontScaler);
1770}
1771
bsalomon@google.com5782d712011-01-21 21:03:59 +00001772SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001773
1774 // deferred allocation
1775 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001776 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001777 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1778 fDrawProcs->fContext = fContext;
1779 }
1780
1781 // init our (and GL's) state
1782 fDrawProcs->fTextContext = context;
1783 fDrawProcs->fFontScaler = NULL;
1784 return fDrawProcs;
1785}
1786
1787void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1788 size_t byteLength, SkScalar x, SkScalar y,
1789 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001790 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001791
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001792 if (fContext->getMatrix().hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001793 // this guy will just call our drawPath()
1794 draw.drawText((const char*)text, byteLength, x, y, paint);
1795 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001796 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001797
1798 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +00001799 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001800 if (!skPaint2GrPaintShader(this,
1801 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001802 true,
twiz@google.com58071162012-07-18 21:41:50 +00001803 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001804 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001805 return;
1806 }
bsalomon@google.com0e354aa2012-10-08 20:44:25 +00001807 GrTextContext context(fContext, grPaint);
tomhudson@google.com375ff852012-06-29 18:37:57 +00001808 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001809 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1810 }
1811}
1812
1813void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1814 size_t byteLength, const SkScalar pos[],
1815 SkScalar constY, int scalarsPerPos,
1816 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001817 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001818
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001819 if (fContext->getMatrix().hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001820 // this guy will just call our drawPath()
1821 draw.drawPosText((const char*)text, byteLength, pos, constY,
1822 scalarsPerPos, paint);
1823 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001824 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001825
1826 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +00001827 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001828 if (!skPaint2GrPaintShader(this,
1829 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001830 true,
twiz@google.com58071162012-07-18 21:41:50 +00001831 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001832 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001833 return;
1834 }
bsalomon@google.com0e354aa2012-10-08 20:44:25 +00001835 GrTextContext context(fContext, grPaint);
tomhudson@google.com375ff852012-06-29 18:37:57 +00001836 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001837 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1838 scalarsPerPos, paint);
1839 }
1840}
1841
1842void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1843 size_t len, const SkPath& path,
1844 const SkMatrix* m, const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001845 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001846
1847 SkASSERT(draw.fDevice == this);
1848 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1849}
1850
1851///////////////////////////////////////////////////////////////////////////////
1852
reed@google.comf67e4cf2011-03-15 20:56:58 +00001853bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1854 if (!paint.isLCDRenderText()) {
1855 // we're cool with the paint as is
1856 return false;
1857 }
1858
1859 if (paint.getShader() ||
1860 paint.getXfermode() || // unless its srcover
1861 paint.getMaskFilter() ||
1862 paint.getRasterizer() ||
1863 paint.getColorFilter() ||
1864 paint.getPathEffect() ||
1865 paint.isFakeBoldText() ||
1866 paint.getStyle() != SkPaint::kFill_Style) {
1867 // turn off lcd
1868 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1869 flags->fHinting = paint.getHinting();
1870 return true;
1871 }
1872 // we're cool with the paint as is
1873 return false;
1874}
1875
reed@google.com75d939b2011-12-07 15:07:23 +00001876void SkGpuDevice::flush() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +00001877 DO_DEFERRED_CLEAR();
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001878 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001879}
1880
reed@google.comf67e4cf2011-03-15 20:56:58 +00001881///////////////////////////////////////////////////////////////////////////////
1882
bsalomon@google.comfb309512011-11-30 14:13:48 +00001883bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001884 const GrTextureParams& params) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001885 uint64_t key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001886 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001887
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001888 GrTextureDesc desc;
1889 desc.fWidth = bitmap.width();
1890 desc.fHeight = bitmap.height();
rileya@google.com24f3ad12012-07-18 21:47:40 +00001891 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config());
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001892
robertphillips@google.com9c2ea842012-08-13 17:47:59 +00001893 GrCacheData cacheData(key);
1894
1895 return this->context()->isTextureInCache(desc, cacheData, &params);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001896}
1897
1898
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001899SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1900 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001901 bool isOpaque,
1902 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001903 GrTextureDesc desc;
1904 desc.fConfig = fRenderTarget->config();
1905 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1906 desc.fWidth = width;
1907 desc.fHeight = height;
1908 desc.fSampleCnt = fRenderTarget->numSamples();
1909
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001910 GrTexture* texture;
1911 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001912 // Skia's convention is to only clear a device if it is non-opaque.
1913 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001914
1915#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1916 // layers are never draw in repeat modes, so we can request an approx
1917 // match and ignore any padding.
1918 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1919 GrContext::kApprox_ScratchTexMatch :
1920 GrContext::kExact_ScratchTexMatch;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001921 texture = fContext->lockScratchTexture(desc, matchType);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001922#else
1923 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1924 texture = tunref.get();
1925#endif
1926 if (texture) {
1927 return SkNEW_ARGS(SkGpuDevice,(fContext,
1928 texture,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001929 needClear));
1930 } else {
1931 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1932 width, height);
1933 return NULL;
1934 }
1935}
1936
1937SkGpuDevice::SkGpuDevice(GrContext* context,
1938 GrTexture* texture,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001939 bool needClear)
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001940 : SkDevice(make_bitmap(context, texture->asRenderTarget())) {
1941
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001942 GrAssert(texture && texture->asRenderTarget());
bsalomon@google.com8090e652012-08-28 15:07:11 +00001943 // This constructor is called from onCreateCompatibleDevice. It has locked the RT in the texture
1944 // cache. We pass true for the third argument so that it will get unlocked.
1945 this->initFromRenderTarget(context, texture->asRenderTarget(), true);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001946 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001947}