blob: a09b57ff10a545b95f7af137e6c8d92d8f8e5fbb [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
tomhudson@google.com898e7b52012-06-01 20:42:15 +00008#include "SkGpuDevice.h"
reed@google.comac10a2d2010-12-22 21:39:39 +00009
twiz@google.com58071162012-07-18 21:41:50 +000010#include "effects/GrColorTableEffect.h"
tomhudson@google.com2f68e762012-07-17 18:43:21 +000011#include "effects/GrTextureDomainEffect.h"
epoger@google.comec3ed6a2011-07-28 14:26:00 +000012
reed@google.comac10a2d2010-12-22 21:39:39 +000013#include "GrContext.h"
14#include "GrTextContext.h"
15
robertphillips@google.come9c04692012-06-29 00:30:13 +000016#include "SkGrTexturePixelRef.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000017
Scroggo97c88c22011-05-11 14:05:25 +000018#include "SkColorFilter.h"
senorblanco@chromium.org9c397442012-09-27 21:57:45 +000019#include "SkDeviceImageFilterProxy.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000020#include "SkDrawProcs.h"
21#include "SkGlyphCache.h"
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +000022#include "SkImageFilter.h"
reed@google.comc9aa5872011-04-05 21:05:37 +000023#include "SkUtils.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000024
bsalomon@google.com06cd7322012-03-30 18:45:35 +000025#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
reed@google.comac10a2d2010-12-22 21:39:39 +000026
27#if 0
28 extern bool (*gShouldDrawProc)();
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +000029 #define CHECK_SHOULD_DRAW(draw, forceI) \
reed@google.comac10a2d2010-12-22 21:39:39 +000030 do { \
31 if (gShouldDrawProc && !gShouldDrawProc()) return; \
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +000032 this->prepareDraw(draw, forceI); \
reed@google.comac10a2d2010-12-22 21:39:39 +000033 } while (0)
34#else
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +000035 #define CHECK_SHOULD_DRAW(draw, forceI) this->prepareDraw(draw, forceI)
reed@google.comac10a2d2010-12-22 21:39:39 +000036#endif
37
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000038// we use the same texture slot on GrPaint for bitmaps and shaders
39// (since drawBitmap, drawSprite, and drawDevice ignore skia's shader)
40enum {
41 kBitmapTextureIdx = 0,
twiz@google.com58071162012-07-18 21:41:50 +000042 kShaderTextureIdx = 0,
43 kColorFilterTextureIdx = 1
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000044};
45
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +000046#define MAX_BLUR_SIGMA 4.0f
47// FIXME: This value comes from from SkBlurMaskFilter.cpp.
48// Should probably be put in a common header someplace.
49#define MAX_BLUR_RADIUS SkIntToScalar(128)
50// This constant approximates the scaling done in the software path's
51// "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
52// IMHO, it actually should be 1: we blur "less" than we should do
53// according to the CSS and canvas specs, simply because Safari does the same.
54// Firefox used to do the same too, until 4.0 where they fixed it. So at some
55// point we should probably get rid of these scaling constants and rebaseline
56// all the blur tests.
57#define BLUR_SIGMA_SCALE 0.6f
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000058// This constant represents the screen alignment criterion in texels for
rmistry@google.comd6176b02012-08-23 18:14:13 +000059// requiring texture domain clamping to prevent color bleeding when drawing
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000060// a sub region of a larger source image.
61#define COLOR_BLEED_TOLERANCE SkFloatToScalar(0.001f)
bsalomon@google.com06cd7322012-03-30 18:45:35 +000062
bsalomon@google.coma6926b12012-10-10 15:25:50 +000063#define DO_DEFERRED_CLEAR() \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000064 do { \
65 if (fNeedClear) { \
bsalomon@google.com730ca3b2012-04-03 13:25:12 +000066 this->clear(0x0); \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000067 } \
68 } while (false) \
69
reed@google.comac10a2d2010-12-22 21:39:39 +000070///////////////////////////////////////////////////////////////////////////////
71
reed@google.comb0a34d82012-07-11 19:57:55 +000072#define CHECK_FOR_NODRAW_ANNOTATION(paint) \
73 do { if (paint.isNoDrawAnnotation()) { return; } } while (0)
74
75///////////////////////////////////////////////////////////////////////////////
76
77
bsalomon@google.com84405e02012-03-05 19:57:21 +000078class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
79public:
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000080 SkAutoCachedTexture()
81 : fDevice(NULL)
82 , fTexture(NULL) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000083 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000084
bsalomon@google.com84405e02012-03-05 19:57:21 +000085 SkAutoCachedTexture(SkGpuDevice* device,
86 const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +000087 const GrTextureParams* params,
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000088 GrTexture** texture)
89 : fDevice(NULL)
90 , fTexture(NULL) {
91 GrAssert(NULL != texture);
bsalomon@google.comb8670992012-07-25 21:27:09 +000092 *texture = this->set(device, bitmap, params);
reed@google.comac10a2d2010-12-22 21:39:39 +000093 }
reed@google.comac10a2d2010-12-22 21:39:39 +000094
bsalomon@google.com84405e02012-03-05 19:57:21 +000095 ~SkAutoCachedTexture() {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000096 if (NULL != fTexture) {
97 GrUnlockCachedBitmapTexture(fTexture);
bsalomon@google.com84405e02012-03-05 19:57:21 +000098 }
reed@google.comac10a2d2010-12-22 21:39:39 +000099 }
bsalomon@google.com84405e02012-03-05 19:57:21 +0000100
101 GrTexture* set(SkGpuDevice* device,
102 const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000103 const GrTextureParams* params) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000104 if (NULL != fTexture) {
105 GrUnlockCachedBitmapTexture(fTexture);
106 fTexture = NULL;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000107 }
108 fDevice = device;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000109 GrTexture* result = (GrTexture*)bitmap.getTexture();
110 if (NULL == result) {
111 // Cannot return the native texture so look it up in our cache
112 fTexture = GrLockCachedBitmapTexture(device->context(), bitmap, params);
113 result = fTexture;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000114 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000115 return result;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000116 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000117
bsalomon@google.com84405e02012-03-05 19:57:21 +0000118private:
119 SkGpuDevice* fDevice;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000120 GrTexture* fTexture;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000121};
reed@google.comac10a2d2010-12-22 21:39:39 +0000122
123///////////////////////////////////////////////////////////////////////////////
124
reed@google.comac10a2d2010-12-22 21:39:39 +0000125struct GrSkDrawProcs : public SkDrawProcs {
126public:
127 GrContext* fContext;
128 GrTextContext* fTextContext;
129 GrFontScaler* fFontScaler; // cached in the skia glyphcache
130};
131
132///////////////////////////////////////////////////////////////////////////////
133
reed@google.comaf951c92011-06-16 19:10:39 +0000134static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
135 switch (config) {
136 case kAlpha_8_GrPixelConfig:
137 *isOpaque = false;
138 return SkBitmap::kA8_Config;
139 case kRGB_565_GrPixelConfig:
140 *isOpaque = true;
141 return SkBitmap::kRGB_565_Config;
142 case kRGBA_4444_GrPixelConfig:
143 *isOpaque = false;
144 return SkBitmap::kARGB_4444_Config;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000145 case kSkia8888_PM_GrPixelConfig:
146 // we don't currently have a way of knowing whether
147 // a 8888 is opaque based on the config.
148 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000149 return SkBitmap::kARGB_8888_Config;
150 default:
151 *isOpaque = false;
152 return SkBitmap::kNo_Config;
153 }
154}
reed@google.comac10a2d2010-12-22 21:39:39 +0000155
reed@google.comaf951c92011-06-16 19:10:39 +0000156static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000157 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000158
159 bool isOpaque;
160 SkBitmap bitmap;
161 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
162 renderTarget->width(), renderTarget->height());
163 bitmap.setIsOpaque(isOpaque);
164 return bitmap;
165}
166
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000167SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000168: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
bsalomon@google.com8090e652012-08-28 15:07:11 +0000169 this->initFromRenderTarget(context, texture->asRenderTarget(), false);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000170}
171
reed@google.comaf951c92011-06-16 19:10:39 +0000172SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000173: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.com8090e652012-08-28 15:07:11 +0000174 this->initFromRenderTarget(context, renderTarget, false);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000175}
176
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000177void SkGpuDevice::initFromRenderTarget(GrContext* context,
bsalomon@google.com8090e652012-08-28 15:07:11 +0000178 GrRenderTarget* renderTarget,
179 bool cached) {
reed@google.comaf951c92011-06-16 19:10:39 +0000180 fDrawProcs = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000181
reed@google.comaf951c92011-06-16 19:10:39 +0000182 fContext = context;
183 fContext->ref();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000184
reed@google.comaf951c92011-06-16 19:10:39 +0000185 fRenderTarget = NULL;
186 fNeedClear = false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000187
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000188 GrAssert(NULL != renderTarget);
189 fRenderTarget = renderTarget;
190 fRenderTarget->ref();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000191
bsalomon@google.coma2921122012-08-28 12:34:17 +0000192 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
193 // on the RT but not vice-versa.
194 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
195 // busting chrome (for a currently unknown reason).
196 GrSurface* surface = fRenderTarget->asTexture();
197 if (NULL == surface) {
198 surface = fRenderTarget;
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000199 }
bsalomon@google.com8090e652012-08-28 15:07:11 +0000200 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (surface, cached));
bsalomon@google.coma2921122012-08-28 12:34:17 +0000201
reed@google.comaf951c92011-06-16 19:10:39 +0000202 this->setPixelRef(pr, 0)->unref();
203}
204
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000205SkGpuDevice::SkGpuDevice(GrContext* context,
206 SkBitmap::Config config,
207 int width,
208 int height)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000209 : SkDevice(config, width, height, false /*isOpaque*/) {
210
reed@google.comac10a2d2010-12-22 21:39:39 +0000211 fDrawProcs = NULL;
212
reed@google.com7b201d22011-01-11 18:59:23 +0000213 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000214 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000215
reed@google.comac10a2d2010-12-22 21:39:39 +0000216 fRenderTarget = NULL;
217 fNeedClear = false;
218
reed@google.comaf951c92011-06-16 19:10:39 +0000219 if (config != SkBitmap::kRGB_565_Config) {
220 config = SkBitmap::kARGB_8888_Config;
221 }
222 SkBitmap bm;
223 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000224
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000225 GrTextureDesc desc;
226 desc.fFlags = kRenderTarget_GrTextureFlagBit;
227 desc.fWidth = width;
228 desc.fHeight = height;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000229 desc.fConfig = SkBitmapConfig2GrPixelConfig(bm.config());
reed@google.comac10a2d2010-12-22 21:39:39 +0000230
bsalomon@google.coma2921122012-08-28 12:34:17 +0000231 SkAutoTUnref<GrTexture> texture(fContext->createUncachedTexture(desc, NULL, 0));
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000232
bsalomon@google.coma2921122012-08-28 12:34:17 +0000233 if (NULL != texture) {
234 fRenderTarget = texture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000235 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000236
reed@google.comaf951c92011-06-16 19:10:39 +0000237 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000238
reed@google.comaf951c92011-06-16 19:10:39 +0000239 // wrap the bitmap with a pixelref to expose our texture
bsalomon@google.coma2921122012-08-28 12:34:17 +0000240 SkGrPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (texture));
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000241 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000242 } else {
243 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
244 width, height);
245 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000246 }
247}
248
249SkGpuDevice::~SkGpuDevice() {
250 if (fDrawProcs) {
251 delete fDrawProcs;
252 }
253
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000254 // The GrContext takes a ref on the target. We don't want to cause the render
255 // target to be unnecessarily kept alive.
256 if (fContext->getRenderTarget() == fRenderTarget) {
257 fContext->setRenderTarget(NULL);
258 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000259
bsalomon@google.coma2921122012-08-28 12:34:17 +0000260 SkSafeUnref(fRenderTarget);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000261 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000262}
263
reed@google.comac10a2d2010-12-22 21:39:39 +0000264///////////////////////////////////////////////////////////////////////////////
265
266void SkGpuDevice::makeRenderTargetCurrent() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000267 DO_DEFERRED_CLEAR();
reed@google.comac10a2d2010-12-22 21:39:39 +0000268 fContext->setRenderTarget(fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000269}
270
271///////////////////////////////////////////////////////////////////////////////
272
bsalomon@google.comc4364992011-11-07 15:54:49 +0000273namespace {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000274GrPixelConfig config8888_to_grconfig_and_flags(SkCanvas::Config8888 config8888, uint32_t* flags) {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000275 switch (config8888) {
276 case SkCanvas::kNative_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000277 *flags = 0;
278 return kSkia8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000279 case SkCanvas::kNative_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000280 *flags = GrContext::kUnpremul_PixelOpsFlag;
281 return kSkia8888_PM_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000282 case SkCanvas::kBGRA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000283 *flags = 0;
284 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000285 case SkCanvas::kBGRA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000286 *flags = GrContext::kUnpremul_PixelOpsFlag;
287 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000288 case SkCanvas::kRGBA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000289 *flags = 0;
290 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000291 case SkCanvas::kRGBA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000292 *flags = GrContext::kUnpremul_PixelOpsFlag;
293 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000294 default:
295 GrCrash("Unexpected Config8888.");
bsalomon@google.comccaa0022012-09-25 19:55:07 +0000296 *flags = 0; // suppress warning
bsalomon@google.comc4364992011-11-07 15:54:49 +0000297 return kSkia8888_PM_GrPixelConfig;
298 }
299}
300}
301
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000302bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
303 int x, int y,
304 SkCanvas::Config8888 config8888) {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000305 DO_DEFERRED_CLEAR();
bsalomon@google.com910267d2011-11-02 20:06:25 +0000306 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
307 SkASSERT(!bitmap.isNull());
308 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000309
bsalomon@google.com910267d2011-11-02 20:06:25 +0000310 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000311 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000312 uint32_t flags;
313 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000314 return fContext->readRenderTargetPixels(fRenderTarget,
315 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000316 bitmap.width(),
317 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000318 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000319 bitmap.getPixels(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000320 bitmap.rowBytes(),
321 flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000322}
323
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000324void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
325 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000326 SkAutoLockPixels alp(bitmap);
327 if (!bitmap.readyToDraw()) {
328 return;
329 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000330
331 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000332 uint32_t flags;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000333 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000334 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000335 } else {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000336 flags = 0;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000337 config= SkBitmapConfig2GrPixelConfig(bitmap.config());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000338 }
339
bsalomon@google.com6f379512011-11-16 20:36:03 +0000340 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000341 config, bitmap.getPixels(), bitmap.rowBytes(), flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000342}
343
robertphillips@google.com46f93502012-08-07 15:38:08 +0000344namespace {
345void purgeClipCB(int genID, void* data) {
346 GrContext* context = (GrContext*) data;
347
348 if (SkClipStack::kInvalidGenID == genID ||
349 SkClipStack::kEmptyGenID == genID ||
350 SkClipStack::kWideOpenGenID == genID) {
351 // none of these cases will have a cached clip mask
352 return;
353 }
354
355}
356};
357
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000358void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
359 INHERITED::onAttachToCanvas(canvas);
360
361 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000362 fClipData.fClipStack = canvas->getClipStack();
robertphillips@google.com46f93502012-08-07 15:38:08 +0000363
364 fClipData.fClipStack->addPurgeClipCallback(purgeClipCB, fContext);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000365}
366
367void SkGpuDevice::onDetachFromCanvas() {
368 INHERITED::onDetachFromCanvas();
369
robertphillips@google.com46f93502012-08-07 15:38:08 +0000370 // TODO: iterate through the clip stack and clean up any cached clip masks
371 fClipData.fClipStack->removePurgeClipCallback(purgeClipCB, fContext);
372
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000373 fClipData.fClipStack = NULL;
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000374}
375
robertphillips@google.com607fe072012-07-24 13:54:00 +0000376#ifdef SK_DEBUG
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000377static void check_bounds(const GrClipData& clipData,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000378 const SkRegion& clipRegion,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000379 int renderTargetWidth,
380 int renderTargetHeight) {
381
robertphillips@google.com7b112892012-07-31 15:18:21 +0000382 SkIRect devBound;
383
384 devBound.setLTRB(0, 0, renderTargetWidth, renderTargetHeight);
385
robertphillips@google.com607fe072012-07-24 13:54:00 +0000386 SkClipStack::BoundsType boundType;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000387 SkRect canvTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000388
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000389 clipData.fClipStack->getBounds(&canvTemp, &boundType);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000390 if (SkClipStack::kNormal_BoundsType == boundType) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000391 SkIRect devTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000392
robertphillips@google.com7b112892012-07-31 15:18:21 +0000393 canvTemp.roundOut(&devTemp);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000394
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000395 devTemp.offset(-clipData.fOrigin.fX, -clipData.fOrigin.fY);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000396
robertphillips@google.com7b112892012-07-31 15:18:21 +0000397 if (!devBound.intersect(devTemp)) {
398 devBound.setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000399 }
400 }
401
robertphillips@google.com768fee82012-08-02 12:42:43 +0000402 GrAssert(devBound.contains(clipRegion.getBounds()));
robertphillips@google.com607fe072012-07-24 13:54:00 +0000403}
404#endif
405
reed@google.comac10a2d2010-12-22 21:39:39 +0000406///////////////////////////////////////////////////////////////////////////////
407
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000408// call this every draw call, to ensure that the context reflects our state,
reed@google.comac10a2d2010-12-22 21:39:39 +0000409// and not the state from some other canvas/device
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000410void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000411 GrAssert(NULL != fClipData.fClipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000412
reed@google.comac10a2d2010-12-22 21:39:39 +0000413 fContext->setRenderTarget(fRenderTarget);
414
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000415 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000416
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000417 if (forceIdentity) {
418 fContext->setIdentityMatrix();
419 } else {
420 fContext->setMatrix(*draw.fMatrix);
421 }
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000422 fClipData.fOrigin = this->getOrigin();
reed@google.comac10a2d2010-12-22 21:39:39 +0000423
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000424#ifdef SK_DEBUG
425 check_bounds(fClipData, *draw.fClip, fRenderTarget->width(), fRenderTarget->height());
426#endif
427
428 fContext->setClip(&fClipData);
429
430 DO_DEFERRED_CLEAR();
reed@google.comac10a2d2010-12-22 21:39:39 +0000431}
432
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000433SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000434 DO_DEFERRED_CLEAR();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000435 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000436}
437
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000438bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
bsalomon@google.coma2921122012-08-28 12:34:17 +0000439 GrTexture* texture = fRenderTarget->asTexture();
440 if (NULL != texture) {
bsalomon@google.com88becf42012-10-05 14:54:42 +0000441 paint->colorSampler(kBitmapTextureIdx)->setCustomStage(
bsalomon@google.coma2921122012-08-28 12:34:17 +0000442 SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000443 return true;
444 }
445 return false;
446}
447
448///////////////////////////////////////////////////////////////////////////////
449
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000450SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
451SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
452SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
453SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
454SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
455 shader_type_mismatch);
rileya@google.com3e332582012-07-03 13:43:35 +0000456SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
457 shader_type_mismatch);
rileya@google.com22e57f92012-07-19 15:16:19 +0000458SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
459SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000460
bsalomon@google.com84405e02012-03-05 19:57:21 +0000461namespace {
462
463// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
464// justAlpha indicates that skPaint's alpha should be used rather than the color
465// Callers may subsequently modify the GrPaint. Setting constantColor indicates
466// that the final paint will draw the same color at every pixel. This allows
467// an optimization where the the color filter can be applied to the skPaint's
twiz@google.com58071162012-07-18 21:41:50 +0000468// color once while converting to GrPaint and then ignored.
469inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
470 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000471 bool justAlpha,
472 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000473 SkGpuDevice::SkAutoCachedTexture* act,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000474 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000475
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000476 grPaint->setDither(skPaint.isDither());
477 grPaint->setAntiAlias(skPaint.isAntiAlias());
bsalomon@google.com5782d712011-01-21 21:03:59 +0000478
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000479 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
480 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000481
482 SkXfermode* mode = skPaint.getXfermode();
483 if (mode) {
484 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000485 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000486#if 0
487 return false;
488#endif
489 }
490 }
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000491 grPaint->setBlendFunc(sk_blend_to_grblend(sm), sk_blend_to_grblend(dm));
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000492
bsalomon@google.com5782d712011-01-21 21:03:59 +0000493 if (justAlpha) {
494 uint8_t alpha = skPaint.getAlpha();
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000495 grPaint->setColor(GrColorPackRGBA(alpha, alpha, alpha, alpha));
Scroggod757df22011-05-16 13:11:16 +0000496 // justAlpha is currently set to true only if there is a texture,
497 // so constantColor should not also be true.
498 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000499 } else {
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000500 grPaint->setColor(SkColor2GrColor(skPaint.getColor()));
bsalomon@google.com88becf42012-10-05 14:54:42 +0000501 GrAssert(!grPaint->isColorStageEnabled(kShaderTextureIdx));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000502 }
Scroggo97c88c22011-05-11 14:05:25 +0000503 SkColorFilter* colorFilter = skPaint.getColorFilter();
504 SkColor color;
505 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000506 SkScalar matrix[20];
twiz@google.com58071162012-07-18 21:41:50 +0000507 SkBitmap colorTransformTable;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000508 // TODO: SkColorFilter::asCustomStage()
Scroggo97c88c22011-05-11 14:05:25 +0000509 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
Scroggod757df22011-05-16 13:11:16 +0000510 if (!constantColor) {
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000511 grPaint->setXfermodeColorFilter(filterMode, SkColor2GrColor(color));
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000512 } else {
513 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000514 grPaint->setColor(SkColor2GrColor(filtered));
Scroggod757df22011-05-16 13:11:16 +0000515 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000516 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000517 grPaint->setColorMatrix(matrix);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000518 } else if (colorFilter != NULL && colorFilter->asComponentTable(&colorTransformTable)) {
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000519 // pass NULL because the color table effect doesn't use tiling or filtering.
520 GrTexture* texture = act->set(dev, colorTransformTable, NULL);
bsalomon@google.com88becf42012-10-05 14:54:42 +0000521 GrSamplerState* colorSampler = grPaint->colorSampler(kColorFilterTextureIdx);
bsalomon@google.comb8670992012-07-25 21:27:09 +0000522 colorSampler->reset();
bsalomon@google.comcbd0ad92012-07-20 15:09:31 +0000523 colorSampler->setCustomStage(SkNEW_ARGS(GrColorTableEffect, (texture)))->unref();
Scroggo97c88c22011-05-11 14:05:25 +0000524 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000525 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000526}
527
bsalomon@google.com84405e02012-03-05 19:57:21 +0000528// This function is similar to skPaint2GrPaintNoShader but also converts
529// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
530// be used is set on grPaint and returned in param act. constantColor has the
531// same meaning as in skPaint2GrPaintNoShader.
532inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
533 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000534 bool constantColor,
bsalomon@google.com88becf42012-10-05 14:54:42 +0000535 SkGpuDevice::SkAutoCachedTexture textures[GrPaint::kMaxColorStages],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000536 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000537 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000538 if (NULL == shader) {
twiz@google.com58071162012-07-18 21:41:50 +0000539 return skPaint2GrPaintNoShader(dev,
540 skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000541 false,
542 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000543 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000544 grPaint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000545 } else if (!skPaint2GrPaintNoShader(dev, skPaint, true, false,
twiz@google.com58071162012-07-18 21:41:50 +0000546 &textures[kColorFilterTextureIdx], grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000547 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000548 }
549
bsalomon@google.com88becf42012-10-05 14:54:42 +0000550 GrSamplerState* sampler = grPaint->colorSampler(kShaderTextureIdx);
rileya@google.com91f319c2012-07-25 17:18:31 +0000551 GrCustomStage* stage = shader->asNewCustomStage(dev->context(), sampler);
552
553 if (NULL != stage) {
554 sampler->setCustomStage(stage)->unref();
555 SkMatrix localM;
556 if (shader->getLocalMatrix(&localM)) {
557 SkMatrix inverse;
558 if (localM.invert(&inverse)) {
559 sampler->matrix()->preConcat(inverse);
560 }
561 }
562 return true;
563 }
564
reed@google.comac10a2d2010-12-22 21:39:39 +0000565 SkBitmap bitmap;
rileya@google.com91f319c2012-07-25 17:18:31 +0000566 SkMatrix* matrix = sampler->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000567 SkShader::TileMode tileModes[2];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000568 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
rileya@google.com91f319c2012-07-25 17:18:31 +0000569 tileModes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000570
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000571 if (SkShader::kNone_BitmapType == bmptype) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000572 SkShader::GradientInfo info;
573 SkColor color;
574
575 info.fColors = &color;
576 info.fColorOffsets = NULL;
577 info.fColorCount = 1;
578 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
579 SkPaint copy(skPaint);
580 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000581 // modulate the paint alpha by the shader's solid color alpha
582 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
583 copy.setColor(SkColorSetA(color, newA));
twiz@google.com58071162012-07-18 21:41:50 +0000584 return skPaint2GrPaintNoShader(dev,
585 copy,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000586 false,
587 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000588 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000589 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000590 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000591 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000592 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000593
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000594 // Must set wrap and filter on the sampler before requesting a texture.
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000595 GrTextureParams params(tileModes, skPaint.isFilterBitmap());
596 GrTexture* texture = textures[kShaderTextureIdx].set(dev, bitmap, &params);
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000597
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000598 if (NULL == texture) {
599 SkDebugf("Couldn't convert bitmap to texture.\n");
600 return false;
601 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000602
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000603 sampler->reset();
604 sampler->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect, (texture, params)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000605
reed@google.comac10a2d2010-12-22 21:39:39 +0000606 // since our texture coords will be in local space, we wack the texture
607 // matrix to map them back into 0...1 before we load it
608 SkMatrix localM;
609 if (shader->getLocalMatrix(&localM)) {
610 SkMatrix inverse;
611 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000612 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000613 }
614 }
615 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000616 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
617 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000618 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000619 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000620
621 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000622}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000623}
reed@google.comac10a2d2010-12-22 21:39:39 +0000624
625///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000626void SkGpuDevice::clear(SkColor color) {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000627 fContext->clear(NULL, color, fRenderTarget);
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000628 fNeedClear = false;
bsalomon@google.com398109c2011-04-14 18:40:27 +0000629}
630
reed@google.comac10a2d2010-12-22 21:39:39 +0000631void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000632 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000633
bsalomon@google.com5782d712011-01-21 21:03:59 +0000634 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +0000635 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000636 if (!skPaint2GrPaintShader(this,
637 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000638 true,
twiz@google.com58071162012-07-18 21:41:50 +0000639 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000640 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000641 return;
642 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000643
644 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000645}
646
647// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000648static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000649 kPoints_GrPrimitiveType,
650 kLines_GrPrimitiveType,
651 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000652};
653
654void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000655 size_t count, const SkPoint pts[], const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000656 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000657
658 SkScalar width = paint.getStrokeWidth();
659 if (width < 0) {
660 return;
661 }
662
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000663 // we only handle hairlines and paints without path effects or mask filters,
664 // else we let the SkDraw call our drawPath()
665 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000666 draw.drawPoints(mode, count, pts, paint, true);
667 return;
668 }
669
bsalomon@google.com5782d712011-01-21 21:03:59 +0000670 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +0000671 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000672 if (!skPaint2GrPaintShader(this,
673 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000674 true,
twiz@google.com58071162012-07-18 21:41:50 +0000675 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000676 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000677 return;
678 }
679
bsalomon@google.com5782d712011-01-21 21:03:59 +0000680 fContext->drawVertices(grPaint,
681 gPointMode2PrimtiveType[mode],
682 count,
683 (GrPoint*)pts,
684 NULL,
685 NULL,
686 NULL,
687 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000688}
689
reed@google.comc9aa5872011-04-05 21:05:37 +0000690///////////////////////////////////////////////////////////////////////////////
691
reed@google.comac10a2d2010-12-22 21:39:39 +0000692void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000693 const SkPaint& paint) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000694 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000695 CHECK_SHOULD_DRAW(draw, false);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000696
bungeman@google.com79bd8772011-07-18 15:34:08 +0000697 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000698 SkScalar width = paint.getStrokeWidth();
699
700 /*
701 We have special code for hairline strokes, miter-strokes, and fills.
702 Anything else we just call our path code.
703 */
704 bool usePath = doStroke && width > 0 &&
705 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000706 // another two reasons we might need to call drawPath...
707 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000708 usePath = true;
709 }
reed@google.com67db6642011-05-26 11:46:35 +0000710 // until we aa rotated rects...
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000711 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
reed@google.com67db6642011-05-26 11:46:35 +0000712 usePath = true;
713 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000714 // small miter limit means right angles show bevel...
715 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
716 paint.getStrokeMiter() < SK_ScalarSqrt2)
717 {
718 usePath = true;
719 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000720 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000721 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
722 usePath = true;
723 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000724
725 if (usePath) {
726 SkPath path;
727 path.addRect(rect);
728 this->drawPath(draw, path, paint, NULL, true);
729 return;
730 }
731
732 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +0000733 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000734 if (!skPaint2GrPaintShader(this,
735 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000736 true,
twiz@google.com58071162012-07-18 21:41:50 +0000737 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000738 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000739 return;
740 }
reed@google.com20efde72011-05-09 17:00:02 +0000741 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000742}
743
reed@google.com69302852011-02-16 18:08:07 +0000744#include "SkMaskFilter.h"
745#include "SkBounder.h"
746
bsalomon@google.com85003222012-03-28 14:44:37 +0000747///////////////////////////////////////////////////////////////////////////////
748
749// helpers for applying mask filters
750namespace {
751
752GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000753 switch (fillType) {
754 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000755 return kWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000756 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000757 return kEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000758 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000759 return kInverseWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000760 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000761 return kInverseEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000762 default:
763 SkDebugf("Unsupported path fill type\n");
bsalomon@google.com47059542012-06-06 20:51:20 +0000764 return kHairLine_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000765 }
766}
767
bsalomon@google.com85003222012-03-28 14:44:37 +0000768// We prefer to blur small rect with small radius via CPU.
769#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
770#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
771inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
772 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
773 rect.height() <= MIN_GPU_BLUR_SIZE &&
774 radius <= MIN_GPU_BLUR_RADIUS) {
775 return true;
776 }
777 return false;
778}
779
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000780bool drawWithGPUMaskFilter(GrContext* context, const SkPath& devPath,
781 SkMaskFilter* filter, const SkRegion& clip,
782 SkBounder* bounder, GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000783#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000784 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000785#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000786 SkMaskFilter::BlurInfo info;
787 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000788 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000789 return false;
790 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000791 SkScalar radius = info.fIgnoreTransform ? info.fRadius
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000792 : context->getMatrix().mapRadius(info.fRadius);
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000793 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000794 if (radius <= 0) {
795 return false;
796 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000797
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000798 SkRect srcRect = devPath.getBounds();
bsalomon@google.com85003222012-03-28 14:44:37 +0000799 if (shouldDrawBlurWithCPU(srcRect, radius)) {
800 return false;
801 }
802
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000803 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000804 float sigma3 = sigma * 3.0f;
805
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000806 SkRect clipRect;
807 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000808
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000809 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000810 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
811 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000812 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000813 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000814 SkIRect finalIRect;
815 finalRect.roundOut(&finalIRect);
816 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000817 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000818 }
819 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000820 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000821 }
822 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000823 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000824 GrTextureDesc desc;
825 desc.fFlags = kRenderTarget_GrTextureFlagBit;
826 desc.fWidth = SkScalarCeilToInt(srcRect.width());
827 desc.fHeight = SkScalarCeilToInt(srcRect.height());
828 // We actually only need A8, but it often isn't supported as a
829 // render target so default to RGBA_8888
bsalomon@google.com0342a852012-08-20 19:22:38 +0000830 desc.fConfig = kRGBA_8888_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000831
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000832 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
833 desc.fConfig = kAlpha_8_GrPixelConfig;
834 }
835
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000836 GrAutoScratchTexture pathEntry(context, desc);
837 GrTexture* pathTexture = pathEntry.texture();
838 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000839 return false;
840 }
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000841
robertphillips@google.comccb39502012-10-01 18:25:13 +0000842 SkAutoTUnref<GrTexture> blurTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000843
robertphillips@google.comccb39502012-10-01 18:25:13 +0000844 {
845 GrContext::AutoRenderTarget art(context, pathTexture->asRenderTarget());
846 GrContext::AutoClip ac(context, srcRect);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000847
robertphillips@google.comccb39502012-10-01 18:25:13 +0000848 context->clear(NULL, 0);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000849
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000850 GrPaint tempPaint;
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000851 if (grp->isAntiAlias()) {
852 tempPaint.setAntiAlias(true);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000853 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
854 // blend coeff of zero requires dual source blending support in order
855 // to properly blend partially covered pixels. This means the AA
856 // code path may not be taken. So we use a dst blend coeff of ISA. We
857 // could special case AA draws to a dst surface with known alpha=0 to
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000858 // use a zero dst coeff when dual source blending isn't available.f
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000859 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000860 }
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000861
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000862 GrContext::AutoMatrix am;
863
864 // Draw hard shadow to pathTexture with path top-left at origin using tempPaint.
865 GrMatrix translate;
866 translate.setTranslate(offset.fX, offset.fY);
867 am.set(context, translate);
868 context->drawPath(tempPaint, devPath, pathFillType);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000869
870 // If we're doing a normal blur, we can clobber the pathTexture in the
871 // gaussianBlur. Otherwise, we need to save it for later compositing.
872 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
skia.committer@gmail.comdc3a4e52012-10-02 02:01:24 +0000873 blurTexture.reset(context->gaussianBlur(pathTexture, isNormalBlur,
robertphillips@google.comccb39502012-10-01 18:25:13 +0000874 srcRect, sigma, sigma));
875
876 if (!isNormalBlur) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000877 context->setIdentityMatrix();
robertphillips@google.comccb39502012-10-01 18:25:13 +0000878 GrPaint paint;
879 paint.reset();
bsalomon@google.com88becf42012-10-05 14:54:42 +0000880 paint.colorSampler(0)->matrix()->setIDiv(pathTexture->width(),
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000881 pathTexture->height());
robertphillips@google.comccb39502012-10-01 18:25:13 +0000882 // Blend pathTexture over blurTexture.
883 context->setRenderTarget(blurTexture->asRenderTarget());
bsalomon@google.com88becf42012-10-05 14:54:42 +0000884 paint.colorSampler(0)->setCustomStage(SkNEW_ARGS
robertphillips@google.comccb39502012-10-01 18:25:13 +0000885 (GrSingleTextureEffect, (pathTexture)))->unref();
886 if (SkMaskFilter::kInner_BlurType == blurType) {
887 // inner: dst = dst * src
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000888 paint.setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000889 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
890 // solid: dst = src + dst - src * dst
891 // = (1 - dst) * src + 1 * dst
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000892 paint.setBlendFunc(kIDC_GrBlendCoeff, kOne_GrBlendCoeff);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000893 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
894 // outer: dst = dst * (1 - src)
895 // = 0 * src + (1 - src) * dst
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000896 paint.setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000897 }
898 context->drawRect(paint, srcRect);
899 }
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000900 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000901
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000902 GrContext::AutoMatrix am;
903 if (!am.setIdentity(context, grp)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000904 return false;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000905 }
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +0000906
bsalomon@google.com88becf42012-10-05 14:54:42 +0000907 static const int MASK_IDX = GrPaint::kMaxCoverageStages - 1;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000908 // we assume the last mask index is available for use
bsalomon@google.com88becf42012-10-05 14:54:42 +0000909 GrAssert(!grp->isCoverageStageEnabled(MASK_IDX));
910 grp->coverageSampler(MASK_IDX)->reset();
911 grp->coverageSampler(MASK_IDX)->setCustomStage(
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000912 SkNEW_ARGS(GrSingleTextureEffect, (blurTexture)))->unref();
bsalomon@google.com88becf42012-10-05 14:54:42 +0000913 grp->coverageSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000914 -finalRect.fTop);
bsalomon@google.com88becf42012-10-05 14:54:42 +0000915 grp->coverageSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000916 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000917 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000918 return true;
919}
920
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000921bool drawWithMaskFilter(GrContext* context, const SkPath& devPath,
922 SkMaskFilter* filter, const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000923 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000924 SkMask srcM, dstM;
925
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000926 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
927 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
reed@google.com69302852011-02-16 18:08:07 +0000928 return false;
929 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000930 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000931
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000932 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
reed@google.com69302852011-02-16 18:08:07 +0000933 return false;
934 }
935 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000936 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000937
938 if (clip.quickReject(dstM.fBounds)) {
939 return false;
940 }
941 if (bounder && !bounder->doIRect(dstM.fBounds)) {
942 return false;
943 }
944
945 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000946 // the current clip (and identity matrix) and GrPaint settings
947 GrContext::AutoMatrix am;
948 am.setIdentity(context, grp);
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000949
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000950 GrTextureDesc desc;
951 desc.fWidth = dstM.fBounds.width();
952 desc.fHeight = dstM.fBounds.height();
953 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +0000954
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000955 GrAutoScratchTexture ast(context, desc);
956 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000957
reed@google.com69302852011-02-16 18:08:07 +0000958 if (NULL == texture) {
959 return false;
960 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000961 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000962 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000963
bsalomon@google.com88becf42012-10-05 14:54:42 +0000964 static const int MASK_IDX = GrPaint::kMaxCoverageStages - 1;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000965 // we assume the last mask index is available for use
bsalomon@google.com88becf42012-10-05 14:54:42 +0000966 GrAssert(!grp->isCoverageStageEnabled(MASK_IDX));
967 grp->coverageSampler(MASK_IDX)->reset();
968 grp->coverageSampler(MASK_IDX)->setCustomStage(
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000969 SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000970 GrRect d;
971 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +0000972 GrIntToScalar(dstM.fBounds.fTop),
973 GrIntToScalar(dstM.fBounds.fRight),
974 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000975
bsalomon@google.com88becf42012-10-05 14:54:42 +0000976 GrMatrix* m = grp->coverageSampler(MASK_IDX)->matrix();
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000977 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
978 -dstM.fBounds.fTop*SK_Scalar1);
979 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000980 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000981 return true;
982}
reed@google.com69302852011-02-16 18:08:07 +0000983
bsalomon@google.com85003222012-03-28 14:44:37 +0000984}
985
986///////////////////////////////////////////////////////////////////////////////
987
reed@google.com0c219b62011-02-16 21:31:18 +0000988void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000989 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000990 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000991 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000992 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000993
reed@google.comfe626382011-09-21 13:50:35 +0000994 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000995
bsalomon@google.com5782d712011-01-21 21:03:59 +0000996 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +0000997 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000998 if (!skPaint2GrPaintShader(this,
999 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001000 true,
twiz@google.com58071162012-07-18 21:41:50 +00001001 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001002 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001003 return;
1004 }
1005
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +00001006 // can we cheat, and threat a thin stroke as a hairline w/ coverage
1007 // if we can, we draw lots faster (raster device does this same test)
1008 SkScalar hairlineCoverage;
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001009 if (SkDrawTreatAsHairline(paint, fContext->getMatrix(), &hairlineCoverage)) {
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +00001010 doFill = false;
bsalomon@google.comc7448ce2012-10-05 19:04:13 +00001011 grPaint.setCoverage(SkScalarRoundToInt(hairlineCoverage * grPaint.getCoverage()));
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +00001012 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001013
reed@google.comfe626382011-09-21 13:50:35 +00001014 // If we have a prematrix, apply it to the path, optimizing for the case
1015 // where the original path can in fact be modified in place (even though
1016 // its parameter type is const).
1017 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1018 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001019
1020 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001021 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001022
reed@google.come3445642011-02-16 23:20:39 +00001023 if (!pathIsMutable) {
1024 result = &tmpPath;
1025 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001026 }
reed@google.come3445642011-02-16 23:20:39 +00001027 // should I push prePathMatrix on our MV stack temporarily, instead
1028 // of applying it here? See SkDraw.cpp
1029 pathPtr->transform(*prePathMatrix, result);
1030 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001031 }
reed@google.com0c219b62011-02-16 21:31:18 +00001032 // at this point we're done with prePathMatrix
1033 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001034
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001035 if (paint.getPathEffect() ||
1036 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001037 // it is safe to use tmpPath here, even if we already used it for the
1038 // prepathmatrix, since getFillPath can take the same object for its
1039 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001040 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001041 pathPtr = &tmpPath;
1042 }
1043
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001044 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001045 // avoid possibly allocating a new path in transform if we can
1046 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1047
1048 // transform the path into device space
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001049 pathPtr->transform(fContext->getMatrix(), devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001050 GrPathFill pathFillType = doFill ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001051 skToGrFillType(devPathPtr->getFillType()) : kHairLine_GrPathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001052 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001053 *draw.fClip, draw.fBounder, &grPaint, pathFillType)) {
rmistry@google.comd6176b02012-08-23 18:14:13 +00001054 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001055 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001056 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001057 *draw.fClip, draw.fBounder, &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001058 }
reed@google.com69302852011-02-16 18:08:07 +00001059 return;
1060 }
reed@google.com69302852011-02-16 18:08:07 +00001061
bsalomon@google.com47059542012-06-06 20:51:20 +00001062 GrPathFill fill = kHairLine_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001063
reed@google.com0c219b62011-02-16 21:31:18 +00001064 if (doFill) {
1065 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001066 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001067 fill = kWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001068 break;
1069 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001070 fill = kEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001071 break;
1072 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001073 fill = kInverseWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001074 break;
1075 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001076 fill = kInverseEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001077 break;
1078 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001079 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001080 return;
1081 }
1082 }
1083
reed@google.com07f3ee12011-05-16 17:21:57 +00001084 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001085}
1086
bsalomon@google.comfb309512011-11-30 14:13:48 +00001087namespace {
1088
1089inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1090 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1091 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1092 return tilesX * tilesY;
1093}
1094
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001095inline int determine_tile_size(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001096 const SkRect& src,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001097 int maxTextureSize) {
1098 static const int kSmallTileSize = 1 << 10;
1099 if (maxTextureSize <= kSmallTileSize) {
1100 return maxTextureSize;
1101 }
1102
1103 size_t maxTexTotalTileSize;
1104 size_t smallTotalTileSize;
1105
robertphillips@google.combac6b052012-09-28 18:06:49 +00001106 SkIRect iSrc;
1107 src.roundOut(&iSrc);
1108
1109 maxTexTotalTileSize = get_tile_count(iSrc.fLeft,
1110 iSrc.fTop,
1111 iSrc.fRight,
1112 iSrc.fBottom,
1113 maxTextureSize);
1114 smallTotalTileSize = get_tile_count(iSrc.fLeft,
1115 iSrc.fTop,
1116 iSrc.fRight,
1117 iSrc.fBottom,
1118 kSmallTileSize);
1119
bsalomon@google.comfb309512011-11-30 14:13:48 +00001120 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1121 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1122
1123 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1124 return kSmallTileSize;
1125 } else {
1126 return maxTextureSize;
1127 }
1128}
1129}
1130
1131bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001132 const GrTextureParams& params,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001133 const SkRect* srcRectPtr) const {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001134 // if bitmap is explictly texture backed then just use the texture
1135 if (NULL != bitmap.getTexture()) {
1136 return false;
1137 }
1138 // if it's larger than the max texture size, then we have no choice but
1139 // tiling
1140 const int maxTextureSize = fContext->getMaxTextureSize();
1141 if (bitmap.width() > maxTextureSize ||
1142 bitmap.height() > maxTextureSize) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001143 return true;
1144 }
1145 // if we are going to have to draw the whole thing, then don't tile
1146 if (NULL == srcRectPtr) {
1147 return false;
1148 }
1149 // if the entire texture is already in our cache then no reason to tile it
bsalomon@google.comb8670992012-07-25 21:27:09 +00001150 if (this->isBitmapInTextureCache(bitmap, params)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001151 return false;
1152 }
1153
1154 // At this point we know we could do the draw by uploading the entire bitmap
1155 // as a texture. However, if the texture would be large compared to the
1156 // cache size and we don't require most of it for this draw then tile to
1157 // reduce the amount of upload and cache spill.
1158
1159 // assumption here is that sw bitmap size is a good proxy for its size as
1160 // a texture
1161 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001162 size_t cacheSize;
1163 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001164 if (bmpSize < cacheSize / 2) {
1165 return false;
1166 }
1167
robertphillips@google.combac6b052012-09-28 18:06:49 +00001168 SkScalar fracUsed = SkScalarMul(srcRectPtr->width() / bitmap.width(),
1169 srcRectPtr->height() / bitmap.height());
1170 if (fracUsed <= SK_ScalarHalf) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001171 return true;
1172 } else {
1173 return false;
1174 }
1175}
1176
reed@google.comac10a2d2010-12-22 21:39:39 +00001177void SkGpuDevice::drawBitmap(const SkDraw& draw,
1178 const SkBitmap& bitmap,
1179 const SkIRect* srcRectPtr,
1180 const SkMatrix& m,
1181 const SkPaint& paint) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001182
1183 SkRect tmp;
1184 SkRect* tmpPtr = NULL;
1185
1186 // convert from SkIRect to SkRect
1187 if (NULL != srcRectPtr) {
1188 tmp.set(*srcRectPtr);
1189 tmpPtr = &tmp;
1190 }
1191
1192 // We cannot call drawBitmapRect here since 'm' could be anything
1193 this->drawBitmapCommon(draw, bitmap, tmpPtr, m, paint);
1194}
1195
1196void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1197 const SkBitmap& bitmap,
1198 const SkRect* srcRectPtr,
1199 const SkMatrix& m,
1200 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001201 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001202
robertphillips@google.combac6b052012-09-28 18:06:49 +00001203 SkRect srcRect;
reed@google.comac10a2d2010-12-22 21:39:39 +00001204 if (NULL == srcRectPtr) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001205 srcRect.set(0, 0, SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001206 } else {
1207 srcRect = *srcRectPtr;
1208 }
1209
junov@google.comd935cfb2011-06-27 20:48:23 +00001210 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001211 // Convert the bitmap to a shader so that the rect can be drawn
1212 // through drawRect, which supports mask filters.
robertphillips@google.combac6b052012-09-28 18:06:49 +00001213 SkMatrix newM(m);
junov@google.com1d329782011-07-28 20:10:09 +00001214 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001215 const SkBitmap* bitmapPtr = &bitmap;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001216 if (NULL != srcRectPtr) {
1217 SkIRect iSrc;
1218 srcRect.roundOut(&iSrc);
1219 if (!bitmap.extractSubset(&tmp, iSrc)) {
epoger@google.com9ef2d832011-07-01 21:12:20 +00001220 return; // extraction failed
1221 }
1222 bitmapPtr = &tmp;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001223 srcRect.offset(SkIntToScalar(-iSrc.fLeft), SkIntToScalar(-iSrc.fTop));
1224 // The source rect has changed so update the matrix
1225 newM.preTranslate(SkIntToScalar(iSrc.fLeft), SkIntToScalar(iSrc.fTop));
junov@google.comd935cfb2011-06-27 20:48:23 +00001226 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001227
junov@google.comd935cfb2011-06-27 20:48:23 +00001228 SkPaint paintWithTexture(paint);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001229 paintWithTexture.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
junov@google.comd935cfb2011-06-27 20:48:23 +00001230 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001231
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001232 // Transform 'newM' needs to be concatenated to the current matrix,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001233 // rather than transforming the primitive directly, so that 'newM' will
junov@google.com1d329782011-07-28 20:10:09 +00001234 // also affect the behavior of the mask filter.
1235 SkMatrix drawMatrix;
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001236 drawMatrix.setConcat(fContext->getMatrix(), newM);
junov@google.com1d329782011-07-28 20:10:09 +00001237 SkDraw transformedDraw(draw);
1238 transformedDraw.fMatrix = &drawMatrix;
1239
robertphillips@google.combac6b052012-09-28 18:06:49 +00001240 this->drawRect(transformedDraw, srcRect, paintWithTexture);
junov@google.com1d329782011-07-28 20:10:09 +00001241
junov@google.comd935cfb2011-06-27 20:48:23 +00001242 return;
1243 }
1244
bsalomon@google.com5782d712011-01-21 21:03:59 +00001245 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001246 SkAutoCachedTexture colorLutTexture;
1247 if (!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001248 return;
1249 }
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001250 GrTextureParams params;
1251 params.setBilerp(paint.isFilterBitmap());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001252
robertphillips@google.combac6b052012-09-28 18:06:49 +00001253 if (!this->shouldTileBitmap(bitmap, params, srcRectPtr)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001254 // take the simple case
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001255 this->internalDrawBitmap(bitmap, srcRect, m, params, &grPaint);
robertphillips@google.combac6b052012-09-28 18:06:49 +00001256 } else {
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001257 this->drawTiledBitmap(bitmap, srcRect, m, params, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001258 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001259}
1260
1261// Break 'bitmap' into several tiles to draw it since it has already
1262// been determined to be too large to fit in VRAM
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001263void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001264 const SkRect& srcRect,
1265 const SkMatrix& m,
1266 const GrTextureParams& params,
1267 GrPaint* grPaint) {
1268 const int maxTextureSize = fContext->getMaxTextureSize();
1269
1270 int tileSize = determine_tile_size(bitmap, srcRect, maxTextureSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001271
reed@google.comac10a2d2010-12-22 21:39:39 +00001272 // compute clip bounds in local coordinates
robertphillips@google.combac6b052012-09-28 18:06:49 +00001273 SkRect clipRect;
reed@google.comac10a2d2010-12-22 21:39:39 +00001274 {
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001275 const GrRenderTarget* rt = fContext->getRenderTarget();
1276 clipRect.setWH(SkIntToScalar(rt->width()), SkIntToScalar(rt->height()));
1277 if (!fContext->getClip()->fClipStack->intersectRectWithClip(&clipRect)) {
1278 return;
1279 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001280 SkMatrix matrix, inverse;
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001281 matrix.setConcat(fContext->getMatrix(), m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001282 if (!matrix.invert(&inverse)) {
1283 return;
1284 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001285 inverse.mapRect(&clipRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001286 }
1287
bsalomon@google.comfb309512011-11-30 14:13:48 +00001288 int nx = bitmap.width() / tileSize;
1289 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001290 for (int x = 0; x <= nx; x++) {
1291 for (int y = 0; y <= ny; y++) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001292 SkRect tileR;
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001293 tileR.set(SkIntToScalar(x * tileSize),
robertphillips@google.combac6b052012-09-28 18:06:49 +00001294 SkIntToScalar(y * tileSize),
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001295 SkIntToScalar((x + 1) * tileSize),
robertphillips@google.combac6b052012-09-28 18:06:49 +00001296 SkIntToScalar((y + 1) * tileSize));
1297
1298 if (!SkRect::Intersects(tileR, clipRect)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001299 continue;
1300 }
1301
robertphillips@google.combac6b052012-09-28 18:06:49 +00001302 if (!tileR.intersect(srcRect)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001303 continue;
1304 }
1305
1306 SkBitmap tmpB;
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001307 SkIRect iTileR;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001308 tileR.roundOut(&iTileR);
1309 if (bitmap.extractSubset(&tmpB, iTileR)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001310 // now offset it to make it "local" to our tmp bitmap
robertphillips@google.combac6b052012-09-28 18:06:49 +00001311 tileR.offset(SkIntToScalar(-iTileR.fLeft), SkIntToScalar(-iTileR.fTop));
reed@google.comac10a2d2010-12-22 21:39:39 +00001312 SkMatrix tmpM(m);
skia.committer@gmail.comdc3a4e52012-10-02 02:01:24 +00001313 tmpM.preTranslate(SkIntToScalar(iTileR.fLeft),
robertphillips@google.comffad46b2012-10-01 14:32:51 +00001314 SkIntToScalar(iTileR.fTop));
1315
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001316 this->internalDrawBitmap(tmpB, tileR, tmpM, params, grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001317 }
1318 }
1319 }
1320}
1321
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001322namespace {
1323
1324bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1325 // detect pixel disalignment
1326 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1327 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
rmistry@google.comd6176b02012-08-23 18:14:13 +00001328 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001329 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1330 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1331 COLOR_BLEED_TOLERANCE &&
1332 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1333 COLOR_BLEED_TOLERANCE) {
1334 return true;
1335 }
1336 return false;
1337}
1338
1339bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1340 const SkMatrix& m) {
1341 // Only gets called if hasAlignedSamples returned false.
1342 // So we can assume that sampling is axis aligned but not texel aligned.
1343 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001344 SkRect innerSrcRect(srcRect), innerTransformedRect,
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001345 outerTransformedRect(transformedRect);
1346 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1347 m.mapRect(&innerTransformedRect, innerSrcRect);
1348
1349 // The gap between outerTransformedRect and innerTransformedRect
1350 // represents the projection of the source border area, which is
1351 // problematic for color bleeding. We must check whether any
1352 // destination pixels sample the border area.
1353 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1354 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1355 SkIRect outer, inner;
1356 outerTransformedRect.round(&outer);
1357 innerTransformedRect.round(&inner);
1358 // If the inner and outer rects round to the same result, it means the
1359 // border does not overlap any pixel centers. Yay!
1360 return inner != outer;
1361}
1362
1363} // unnamed namespace
1364
reed@google.comac10a2d2010-12-22 21:39:39 +00001365/*
1366 * This is called by drawBitmap(), which has to handle images that may be too
1367 * large to be represented by a single texture.
1368 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001369 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1370 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001371 */
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001372void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001373 const SkRect& srcRect,
reed@google.comac10a2d2010-12-22 21:39:39 +00001374 const SkMatrix& m,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001375 const GrTextureParams& params,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001376 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001377 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1378 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001379
reed@google.com9c49bc32011-07-07 13:42:37 +00001380 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001381 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001382 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001383 return;
1384 }
1385
bsalomon@google.com88becf42012-10-05 14:54:42 +00001386 GrSamplerState* sampler = grPaint->colorSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001387
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001388 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001389
1390 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001391 SkAutoCachedTexture act(this, bitmap, &params, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001392 if (NULL == texture) {
1393 return;
1394 }
1395
robertphillips@google.combac6b052012-09-28 18:06:49 +00001396 GrRect dstRect(srcRect);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001397 GrRect paintRect;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001398 SkScalar wInv = SkScalarInvert(SkIntToScalar(bitmap.width()));
1399 SkScalar hInv = SkScalarInvert(SkIntToScalar(bitmap.height()));
1400 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1401 SkScalarMul(srcRect.fTop, hInv),
1402 SkScalarMul(srcRect.fRight, wInv),
1403 SkScalarMul(srcRect.fBottom, hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001404
rmistry@google.comd6176b02012-08-23 18:14:13 +00001405 bool needsTextureDomain = false;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001406 if (params.isBilerp()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001407 // Need texture domain if drawing a sub rect.
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001408 needsTextureDomain = srcRect.width() < bitmap.width() ||
robertphillips@google.combac6b052012-09-28 18:06:49 +00001409 srcRect.height() < bitmap.height();
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001410 if (m.rectStaysRect() && fContext->getMatrix().rectStaysRect()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001411 // sampling is axis-aligned
robertphillips@google.combac6b052012-09-28 18:06:49 +00001412 GrRect transformedRect;
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001413 SkMatrix srcToDeviceMatrix(m);
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001414 srcToDeviceMatrix.postConcat(fContext->getMatrix());
robertphillips@google.combac6b052012-09-28 18:06:49 +00001415 srcToDeviceMatrix.mapRect(&transformedRect, srcRect);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001416
robertphillips@google.combac6b052012-09-28 18:06:49 +00001417 if (hasAlignedSamples(srcRect, transformedRect)) {
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001418 // We could also turn off filtering here (but we already did a cache lookup with
1419 // params).
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001420 needsTextureDomain = false;
1421 } else {
1422 needsTextureDomain = needsTextureDomain &&
robertphillips@google.combac6b052012-09-28 18:06:49 +00001423 mayColorBleed(srcRect, transformedRect, m);
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001424 }
1425 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001426 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001427
1428 GrRect textureDomain = GrRect::MakeEmpty();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001429 SkAutoTUnref<GrCustomStage> stage;
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001430 if (needsTextureDomain) {
1431 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001432 GrScalar left, top, right, bottom;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001433 if (srcRect.width() > GR_Scalar1) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001434 GrScalar border = GR_ScalarHalf / bitmap.width();
1435 left = paintRect.left() + border;
1436 right = paintRect.right() - border;
1437 } else {
1438 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1439 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001440 if (srcRect.height() > GR_Scalar1) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001441 GrScalar border = GR_ScalarHalf / bitmap.height();
1442 top = paintRect.top() + border;
1443 bottom = paintRect.bottom() - border;
1444 } else {
1445 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1446 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001447 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001448 stage.reset(SkNEW_ARGS(GrTextureDomainEffect, (texture, textureDomain, params)));
1449 } else {
1450 stage.reset(SkNEW_ARGS(GrSingleTextureEffect, (texture, params)));
junov@google.com6acc9b32011-05-16 18:32:07 +00001451 }
bsalomon@google.com88becf42012-10-05 14:54:42 +00001452 grPaint->colorSampler(kBitmapTextureIdx)->setCustomStage(stage);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001453 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001454}
1455
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001456namespace {
1457
1458void apply_custom_stage(GrContext* context,
1459 GrTexture* srcTexture,
1460 GrTexture* dstTexture,
1461 const GrRect& rect,
1462 GrCustomStage* stage) {
1463 SkASSERT(srcTexture && srcTexture->getContext() == context);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001464 GrContext::AutoMatrix am;
1465 am.setIdentity(context);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001466 GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget());
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001467 GrContext::AutoClip acs(context, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001468
1469 GrMatrix sampleM;
1470 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
1471 GrPaint paint;
bsalomon@google.com88becf42012-10-05 14:54:42 +00001472 paint.colorSampler(0)->reset(sampleM);
1473 paint.colorSampler(0)->setCustomStage(stage);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001474 context->drawRect(paint, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001475}
1476
1477};
1478
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001479static GrTexture* filter_texture(SkDevice* device, GrContext* context,
1480 GrTexture* texture, SkImageFilter* filter,
1481 const GrRect& rect) {
reed@google.com8926b162012-03-23 15:36:36 +00001482 GrAssert(filter);
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001483 SkDeviceImageFilterProxy proxy(device);
reed@google.com8926b162012-03-23 15:36:36 +00001484
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001485 GrTextureDesc desc;
1486 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1487 desc.fWidth = SkScalarCeilToInt(rect.width());
1488 desc.fHeight = SkScalarCeilToInt(rect.height());
bsalomon@google.com0342a852012-08-20 19:22:38 +00001489 desc.fConfig = kRGBA_8888_GrPixelConfig;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001490 GrCustomStage* stage;
reed@google.com8926b162012-03-23 15:36:36 +00001491
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001492 if (filter->canFilterImageGPU()) {
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001493 texture = filter->onFilterImageGPU(&proxy, texture, rect);
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001494 } else if (filter->asNewCustomStage(&stage, texture)) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001495 GrAutoScratchTexture dst(context, desc);
1496 apply_custom_stage(context, texture, dst.texture(), rect, stage);
1497 texture = dst.detach();
1498 stage->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001499 }
1500 return texture;
1501}
1502
reed@google.comac10a2d2010-12-22 21:39:39 +00001503void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001504 int left, int top, const SkPaint& paint) {
1505 // drawSprite is defined to be in device coords.
1506 CHECK_SHOULD_DRAW(draw, true);
reed@google.comac10a2d2010-12-22 21:39:39 +00001507
reed@google.com8926b162012-03-23 15:36:36 +00001508 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001509 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1510 return;
1511 }
1512
reed@google.com76dd2772012-01-05 21:15:07 +00001513 int w = bitmap.width();
1514 int h = bitmap.height();
1515
bsalomon@google.com5782d712011-01-21 21:03:59 +00001516 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001517 SkAutoCachedTexture colorLutTexture;
1518 if(!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001519 return;
1520 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001521
bsalomon@google.com88becf42012-10-05 14:54:42 +00001522 GrSamplerState* sampler = grPaint.colorSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001523
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001524 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001525 sampler->reset();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001526 // draw sprite uses the default texture params
1527 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
bsalomon@google.com88becf42012-10-05 14:54:42 +00001528 grPaint.colorSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001529 (GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001530
reed@google.com8926b162012-03-23 15:36:36 +00001531 SkImageFilter* filter = paint.getImageFilter();
1532 if (NULL != filter) {
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001533 GrTexture* filteredTexture = filter_texture(this, fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001534 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001535 if (filteredTexture) {
bsalomon@google.com88becf42012-10-05 14:54:42 +00001536 grPaint.colorSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001537 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001538 texture = filteredTexture;
1539 filteredTexture->unref();
1540 }
reed@google.com76dd2772012-01-05 21:15:07 +00001541 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001542
bsalomon@google.com5782d712011-01-21 21:03:59 +00001543 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001544 GrRect::MakeXYWH(GrIntToScalar(left),
1545 GrIntToScalar(top),
1546 GrIntToScalar(w),
1547 GrIntToScalar(h)),
1548 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1549 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001550}
1551
reed@google.com33535f32012-09-25 15:37:50 +00001552void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1553 const SkRect* src, const SkRect& dst,
1554 const SkPaint& paint) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001555 SkMatrix matrix;
1556 SkRect bitmapBounds, tmpSrc;
1557
1558 bitmapBounds.set(0, 0,
1559 SkIntToScalar(bitmap.width()),
1560 SkIntToScalar(bitmap.height()));
1561
reed@google.com33535f32012-09-25 15:37:50 +00001562 // Compute matrix from the two rectangles
robertphillips@google.combac6b052012-09-28 18:06:49 +00001563 if (NULL != src) {
1564 tmpSrc = *src;
1565 } else {
1566 tmpSrc = bitmapBounds;
1567 }
1568 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1569
1570 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1571 if (NULL != src) {
1572 if (!bitmapBounds.contains(tmpSrc)) {
1573 if (!tmpSrc.intersect(bitmapBounds)) {
1574 return; // nothing to draw
reed@google.com33535f32012-09-25 15:37:50 +00001575 }
reed@google.com33535f32012-09-25 15:37:50 +00001576 }
reed@google.com33535f32012-09-25 15:37:50 +00001577 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +00001578
robertphillips@google.combac6b052012-09-28 18:06:49 +00001579 this->drawBitmapCommon(draw, bitmap, &tmpSrc, matrix, paint);
reed@google.com33535f32012-09-25 15:37:50 +00001580}
1581
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001582void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001583 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001584 // clear of the source device must occur before CHECK_SHOULD_DRAW
1585 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1586 if (dev->fNeedClear) {
1587 // TODO: could check here whether we really need to draw at all
1588 dev->clear(0x0);
1589 }
1590
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001591 // drawDevice is defined to be in device coords.
1592 CHECK_SHOULD_DRAW(draw, true);
reed@google.comac10a2d2010-12-22 21:39:39 +00001593
bsalomon@google.com5782d712011-01-21 21:03:59 +00001594 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001595 SkAutoCachedTexture colorLutTexture;
bsalomon@google.com88becf42012-10-05 14:54:42 +00001596 grPaint.colorSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001597 if (!dev->bindDeviceAsTexture(&grPaint) ||
twiz@google.com58071162012-07-18 21:41:50 +00001598 !skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001599 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001600 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001601
bsalomon@google.com88becf42012-10-05 14:54:42 +00001602 GrTexture* devTex = grPaint.getColorSampler(kBitmapTextureIdx).getCustomStage()->texture(0);
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001603 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001604
reed@google.com8926b162012-03-23 15:36:36 +00001605 SkImageFilter* filter = paint.getImageFilter();
1606 if (NULL != filter) {
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001607 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001608 SkIntToScalar(devTex->height()));
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001609 GrTexture* filteredTexture = filter_texture(this, fContext, devTex, filter, rect);
reed@google.com8926b162012-03-23 15:36:36 +00001610 if (filteredTexture) {
bsalomon@google.com88becf42012-10-05 14:54:42 +00001611 grPaint.colorSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001612 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001613 devTex = filteredTexture;
1614 filteredTexture->unref();
1615 }
1616 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001617
bsalomon@google.com5782d712011-01-21 21:03:59 +00001618 const SkBitmap& bm = dev->accessBitmap(false);
1619 int w = bm.width();
1620 int h = bm.height();
1621
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001622 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1623 GrIntToScalar(y),
1624 GrIntToScalar(w),
1625 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001626
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001627 // The device being drawn may not fill up its texture (saveLayer uses
1628 // the approximate ).
1629 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1630 GR_Scalar1 * h / devTex->height());
1631
1632 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001633}
1634
reed@google.com8926b162012-03-23 15:36:36 +00001635bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001636 if (!filter->asNewCustomStage(NULL, NULL) &&
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001637 !filter->canFilterImageGPU()) {
reed@google.com76dd2772012-01-05 21:15:07 +00001638 return false;
1639 }
reed@google.com8926b162012-03-23 15:36:36 +00001640 return true;
1641}
1642
1643bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1644 const SkMatrix& ctm,
1645 SkBitmap* result, SkIPoint* offset) {
1646 // want explicitly our impl, so guard against a subclass of us overriding it
1647 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001648 return false;
1649 }
reed@google.com8926b162012-03-23 15:36:36 +00001650
1651 SkAutoLockPixels alp(src, !src.getTexture());
1652 if (!src.getTexture() && !src.readyToDraw()) {
1653 return false;
1654 }
1655
1656 GrPaint paint;
reed@google.com8926b162012-03-23 15:36:36 +00001657
reed@google.com8926b162012-03-23 15:36:36 +00001658 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001659 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1660 // must be pushed upstack.
1661 SkAutoCachedTexture act(this, src, NULL, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001662
1663 result->setConfig(src.config(), src.width(), src.height());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001664 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001665 SkIntToScalar(src.height()));
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001666 GrTexture* resultTexture = filter_texture(this, fContext, texture, filter, rect);
reed@google.com8926b162012-03-23 15:36:36 +00001667 if (resultTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001668 result->setPixelRef(SkNEW_ARGS(SkGrTexturePixelRef,
1669 (resultTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001670 resultTexture->unref();
1671 }
reed@google.com76dd2772012-01-05 21:15:07 +00001672 return true;
1673}
1674
reed@google.comac10a2d2010-12-22 21:39:39 +00001675///////////////////////////////////////////////////////////////////////////////
1676
1677// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001678static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001679 kTriangles_GrPrimitiveType,
1680 kTriangleStrip_GrPrimitiveType,
1681 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001682};
1683
1684void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1685 int vertexCount, const SkPoint vertices[],
1686 const SkPoint texs[], const SkColor colors[],
1687 SkXfermode* xmode,
1688 const uint16_t indices[], int indexCount,
1689 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001690 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001691
bsalomon@google.com5782d712011-01-21 21:03:59 +00001692 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +00001693 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com5782d712011-01-21 21:03:59 +00001694 // we ignore the shader if texs is null.
1695 if (NULL == texs) {
twiz@google.com58071162012-07-18 21:41:50 +00001696 if (!skPaint2GrPaintNoShader(this,
1697 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001698 false,
1699 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001700 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +00001701 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001702 return;
1703 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001704 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001705 if (!skPaint2GrPaintShader(this,
1706 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001707 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001708 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001709 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001710 return;
1711 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001712 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001713
1714 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001715 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001716 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1717#if 0
1718 return
1719#endif
1720 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001721 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001722
bsalomon@google.com498776a2011-08-16 19:20:44 +00001723 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1724 if (NULL != colors) {
1725 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001726 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001727 for (int i = 0; i < vertexCount; ++i) {
rileya@google.com24f3ad12012-07-18 21:47:40 +00001728 convertedColors[i] = SkColor2GrColor(colors[i]);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001729 }
1730 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001731 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001732 fContext->drawVertices(grPaint,
1733 gVertexMode2PrimitiveType[vmode],
1734 vertexCount,
1735 (GrPoint*) vertices,
1736 (GrPoint*) texs,
1737 colors,
1738 indices,
1739 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001740}
1741
1742///////////////////////////////////////////////////////////////////////////////
1743
1744static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001745 GrFontScaler* scaler = (GrFontScaler*)data;
1746 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001747}
1748
1749static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1750 void* auxData;
1751 GrFontScaler* scaler = NULL;
1752 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1753 scaler = (GrFontScaler*)auxData;
1754 }
1755 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001756 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001757 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1758 }
1759 return scaler;
1760}
1761
1762static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1763 SkFixed fx, SkFixed fy,
1764 const SkGlyph& glyph) {
1765 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1766
bungeman@google.com15865a72012-01-11 16:28:04 +00001767 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001768
1769 if (NULL == procs->fFontScaler) {
1770 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1771 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001772
bungeman@google.com15865a72012-01-11 16:28:04 +00001773 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1774 glyph.getSubXFixed(),
1775 glyph.getSubYFixed()),
1776 SkFixedFloorToFixed(fx),
1777 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001778 procs->fFontScaler);
1779}
1780
bsalomon@google.com5782d712011-01-21 21:03:59 +00001781SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001782
1783 // deferred allocation
1784 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001785 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001786 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1787 fDrawProcs->fContext = fContext;
1788 }
1789
1790 // init our (and GL's) state
1791 fDrawProcs->fTextContext = context;
1792 fDrawProcs->fFontScaler = NULL;
1793 return fDrawProcs;
1794}
1795
1796void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1797 size_t byteLength, SkScalar x, SkScalar y,
1798 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001799 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001800
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001801 if (fContext->getMatrix().hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001802 // this guy will just call our drawPath()
1803 draw.drawText((const char*)text, byteLength, x, y, paint);
1804 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001805 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001806
1807 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +00001808 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001809 if (!skPaint2GrPaintShader(this,
1810 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001811 true,
twiz@google.com58071162012-07-18 21:41:50 +00001812 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001813 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001814 return;
1815 }
bsalomon@google.com0e354aa2012-10-08 20:44:25 +00001816 GrTextContext context(fContext, grPaint);
tomhudson@google.com375ff852012-06-29 18:37:57 +00001817 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001818 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1819 }
1820}
1821
1822void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1823 size_t byteLength, const SkScalar pos[],
1824 SkScalar constY, int scalarsPerPos,
1825 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001826 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001827
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001828 if (fContext->getMatrix().hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001829 // this guy will just call our drawPath()
1830 draw.drawPosText((const char*)text, byteLength, pos, constY,
1831 scalarsPerPos, paint);
1832 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001833 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001834
1835 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +00001836 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001837 if (!skPaint2GrPaintShader(this,
1838 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001839 true,
twiz@google.com58071162012-07-18 21:41:50 +00001840 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001841 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001842 return;
1843 }
bsalomon@google.com0e354aa2012-10-08 20:44:25 +00001844 GrTextContext context(fContext, grPaint);
tomhudson@google.com375ff852012-06-29 18:37:57 +00001845 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001846 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1847 scalarsPerPos, paint);
1848 }
1849}
1850
1851void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1852 size_t len, const SkPath& path,
1853 const SkMatrix* m, const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001854 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001855
1856 SkASSERT(draw.fDevice == this);
1857 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1858}
1859
1860///////////////////////////////////////////////////////////////////////////////
1861
reed@google.comf67e4cf2011-03-15 20:56:58 +00001862bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1863 if (!paint.isLCDRenderText()) {
1864 // we're cool with the paint as is
1865 return false;
1866 }
1867
1868 if (paint.getShader() ||
1869 paint.getXfermode() || // unless its srcover
1870 paint.getMaskFilter() ||
1871 paint.getRasterizer() ||
1872 paint.getColorFilter() ||
1873 paint.getPathEffect() ||
1874 paint.isFakeBoldText() ||
1875 paint.getStyle() != SkPaint::kFill_Style) {
1876 // turn off lcd
1877 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1878 flags->fHinting = paint.getHinting();
1879 return true;
1880 }
1881 // we're cool with the paint as is
1882 return false;
1883}
1884
reed@google.com75d939b2011-12-07 15:07:23 +00001885void SkGpuDevice::flush() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +00001886 DO_DEFERRED_CLEAR();
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001887 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001888}
1889
reed@google.comf67e4cf2011-03-15 20:56:58 +00001890///////////////////////////////////////////////////////////////////////////////
1891
bsalomon@google.comfb309512011-11-30 14:13:48 +00001892bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001893 const GrTextureParams& params) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001894 uint64_t key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001895 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001896
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001897 GrTextureDesc desc;
1898 desc.fWidth = bitmap.width();
1899 desc.fHeight = bitmap.height();
rileya@google.com24f3ad12012-07-18 21:47:40 +00001900 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config());
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001901
robertphillips@google.com9c2ea842012-08-13 17:47:59 +00001902 GrCacheData cacheData(key);
1903
1904 return this->context()->isTextureInCache(desc, cacheData, &params);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001905}
1906
1907
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001908SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1909 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001910 bool isOpaque,
1911 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001912 GrTextureDesc desc;
1913 desc.fConfig = fRenderTarget->config();
1914 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1915 desc.fWidth = width;
1916 desc.fHeight = height;
1917 desc.fSampleCnt = fRenderTarget->numSamples();
1918
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001919 GrTexture* texture;
1920 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001921 // Skia's convention is to only clear a device if it is non-opaque.
1922 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001923
1924#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1925 // layers are never draw in repeat modes, so we can request an approx
1926 // match and ignore any padding.
1927 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1928 GrContext::kApprox_ScratchTexMatch :
1929 GrContext::kExact_ScratchTexMatch;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001930 texture = fContext->lockScratchTexture(desc, matchType);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001931#else
1932 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1933 texture = tunref.get();
1934#endif
1935 if (texture) {
1936 return SkNEW_ARGS(SkGpuDevice,(fContext,
1937 texture,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001938 needClear));
1939 } else {
1940 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1941 width, height);
1942 return NULL;
1943 }
1944}
1945
1946SkGpuDevice::SkGpuDevice(GrContext* context,
1947 GrTexture* texture,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001948 bool needClear)
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001949 : SkDevice(make_bitmap(context, texture->asRenderTarget())) {
1950
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001951 GrAssert(texture && texture->asRenderTarget());
bsalomon@google.com8090e652012-08-28 15:07:11 +00001952 // This constructor is called from onCreateCompatibleDevice. It has locked the RT in the texture
1953 // cache. We pass true for the third argument so that it will get unlocked.
1954 this->initFromRenderTarget(context, texture->asRenderTarget(), true);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001955 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001956}