blob: b168e57e6381088ec5b2eda508904c5446fec136 [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.comfe626382011-09-21 13:50:35 +000023#include "SkTLazy.h"
reed@google.comc9aa5872011-04-05 21:05:37 +000024#include "SkUtils.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000025
bsalomon@google.com06cd7322012-03-30 18:45:35 +000026#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
reed@google.comac10a2d2010-12-22 21:39:39 +000027
28#if 0
29 extern bool (*gShouldDrawProc)();
30 #define CHECK_SHOULD_DRAW(draw) \
31 do { \
32 if (gShouldDrawProc && !gShouldDrawProc()) return; \
bsalomon@google.coma6926b12012-10-10 15:25:50 +000033 this->prepareDraw(draw); \
reed@google.comac10a2d2010-12-22 21:39:39 +000034 } while (0)
35#else
bsalomon@google.coma6926b12012-10-10 15:25:50 +000036 #define CHECK_SHOULD_DRAW(draw) this->prepareDraw(draw)
reed@google.comac10a2d2010-12-22 21:39:39 +000037#endif
38
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000039// we use the same texture slot on GrPaint for bitmaps and shaders
40// (since drawBitmap, drawSprite, and drawDevice ignore skia's shader)
41enum {
42 kBitmapTextureIdx = 0,
twiz@google.com58071162012-07-18 21:41:50 +000043 kShaderTextureIdx = 0,
44 kColorFilterTextureIdx = 1
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000045};
46
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +000047#define MAX_BLUR_SIGMA 4.0f
48// FIXME: This value comes from from SkBlurMaskFilter.cpp.
49// Should probably be put in a common header someplace.
50#define MAX_BLUR_RADIUS SkIntToScalar(128)
51// This constant approximates the scaling done in the software path's
52// "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
53// IMHO, it actually should be 1: we blur "less" than we should do
54// according to the CSS and canvas specs, simply because Safari does the same.
55// Firefox used to do the same too, until 4.0 where they fixed it. So at some
56// point we should probably get rid of these scaling constants and rebaseline
57// all the blur tests.
58#define BLUR_SIGMA_SCALE 0.6f
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000059// This constant represents the screen alignment criterion in texels for
rmistry@google.comd6176b02012-08-23 18:14:13 +000060// requiring texture domain clamping to prevent color bleeding when drawing
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000061// a sub region of a larger source image.
62#define COLOR_BLEED_TOLERANCE SkFloatToScalar(0.001f)
bsalomon@google.com06cd7322012-03-30 18:45:35 +000063
bsalomon@google.coma6926b12012-10-10 15:25:50 +000064#define DO_DEFERRED_CLEAR() \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000065 do { \
66 if (fNeedClear) { \
bsalomon@google.com730ca3b2012-04-03 13:25:12 +000067 this->clear(0x0); \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000068 } \
69 } while (false) \
70
reed@google.comac10a2d2010-12-22 21:39:39 +000071///////////////////////////////////////////////////////////////////////////////
72
reed@google.comb0a34d82012-07-11 19:57:55 +000073#define CHECK_FOR_NODRAW_ANNOTATION(paint) \
74 do { if (paint.isNoDrawAnnotation()) { return; } } while (0)
75
76///////////////////////////////////////////////////////////////////////////////
77
78
bsalomon@google.com84405e02012-03-05 19:57:21 +000079class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
80public:
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000081 SkAutoCachedTexture()
82 : fDevice(NULL)
83 , fTexture(NULL) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000084 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000085
bsalomon@google.com84405e02012-03-05 19:57:21 +000086 SkAutoCachedTexture(SkGpuDevice* device,
87 const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +000088 const GrTextureParams* params,
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000089 GrTexture** texture)
90 : fDevice(NULL)
91 , fTexture(NULL) {
92 GrAssert(NULL != texture);
bsalomon@google.comb8670992012-07-25 21:27:09 +000093 *texture = this->set(device, bitmap, params);
reed@google.comac10a2d2010-12-22 21:39:39 +000094 }
reed@google.comac10a2d2010-12-22 21:39:39 +000095
bsalomon@google.com84405e02012-03-05 19:57:21 +000096 ~SkAutoCachedTexture() {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000097 if (NULL != fTexture) {
98 GrUnlockCachedBitmapTexture(fTexture);
bsalomon@google.com84405e02012-03-05 19:57:21 +000099 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000100 }
bsalomon@google.com84405e02012-03-05 19:57:21 +0000101
102 GrTexture* set(SkGpuDevice* device,
103 const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000104 const GrTextureParams* params) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000105 if (NULL != fTexture) {
106 GrUnlockCachedBitmapTexture(fTexture);
107 fTexture = NULL;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000108 }
109 fDevice = device;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000110 GrTexture* result = (GrTexture*)bitmap.getTexture();
111 if (NULL == result) {
112 // Cannot return the native texture so look it up in our cache
113 fTexture = GrLockCachedBitmapTexture(device->context(), bitmap, params);
114 result = fTexture;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000115 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000116 return result;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000117 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000118
bsalomon@google.com84405e02012-03-05 19:57:21 +0000119private:
120 SkGpuDevice* fDevice;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000121 GrTexture* fTexture;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000122};
reed@google.comac10a2d2010-12-22 21:39:39 +0000123
124///////////////////////////////////////////////////////////////////////////////
125
reed@google.comac10a2d2010-12-22 21:39:39 +0000126struct GrSkDrawProcs : public SkDrawProcs {
127public:
128 GrContext* fContext;
129 GrTextContext* fTextContext;
130 GrFontScaler* fFontScaler; // cached in the skia glyphcache
131};
132
133///////////////////////////////////////////////////////////////////////////////
134
reed@google.comaf951c92011-06-16 19:10:39 +0000135static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
136 switch (config) {
137 case kAlpha_8_GrPixelConfig:
138 *isOpaque = false;
139 return SkBitmap::kA8_Config;
140 case kRGB_565_GrPixelConfig:
141 *isOpaque = true;
142 return SkBitmap::kRGB_565_Config;
143 case kRGBA_4444_GrPixelConfig:
144 *isOpaque = false;
145 return SkBitmap::kARGB_4444_Config;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000146 case kSkia8888_PM_GrPixelConfig:
147 // we don't currently have a way of knowing whether
148 // a 8888 is opaque based on the config.
149 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000150 return SkBitmap::kARGB_8888_Config;
151 default:
152 *isOpaque = false;
153 return SkBitmap::kNo_Config;
154 }
155}
reed@google.comac10a2d2010-12-22 21:39:39 +0000156
reed@google.comaf951c92011-06-16 19:10:39 +0000157static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000158 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000159
160 bool isOpaque;
161 SkBitmap bitmap;
162 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
163 renderTarget->width(), renderTarget->height());
164 bitmap.setIsOpaque(isOpaque);
165 return bitmap;
166}
167
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000168SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000169: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
bsalomon@google.com8090e652012-08-28 15:07:11 +0000170 this->initFromRenderTarget(context, texture->asRenderTarget(), false);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000171}
172
reed@google.comaf951c92011-06-16 19:10:39 +0000173SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000174: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.com8090e652012-08-28 15:07:11 +0000175 this->initFromRenderTarget(context, renderTarget, false);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000176}
177
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000178void SkGpuDevice::initFromRenderTarget(GrContext* context,
bsalomon@google.com8090e652012-08-28 15:07:11 +0000179 GrRenderTarget* renderTarget,
180 bool cached) {
reed@google.comaf951c92011-06-16 19:10:39 +0000181 fDrawProcs = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000182
reed@google.comaf951c92011-06-16 19:10:39 +0000183 fContext = context;
184 fContext->ref();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000185
reed@google.comaf951c92011-06-16 19:10:39 +0000186 fRenderTarget = NULL;
187 fNeedClear = false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000188
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000189 GrAssert(NULL != renderTarget);
190 fRenderTarget = renderTarget;
191 fRenderTarget->ref();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000192
bsalomon@google.coma2921122012-08-28 12:34:17 +0000193 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
194 // on the RT but not vice-versa.
195 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
196 // busting chrome (for a currently unknown reason).
197 GrSurface* surface = fRenderTarget->asTexture();
198 if (NULL == surface) {
199 surface = fRenderTarget;
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000200 }
bsalomon@google.com8090e652012-08-28 15:07:11 +0000201 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (surface, cached));
bsalomon@google.coma2921122012-08-28 12:34:17 +0000202
reed@google.comaf951c92011-06-16 19:10:39 +0000203 this->setPixelRef(pr, 0)->unref();
204}
205
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000206SkGpuDevice::SkGpuDevice(GrContext* context,
207 SkBitmap::Config config,
208 int width,
209 int height)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000210 : SkDevice(config, width, height, false /*isOpaque*/) {
211
reed@google.comac10a2d2010-12-22 21:39:39 +0000212 fDrawProcs = NULL;
213
reed@google.com7b201d22011-01-11 18:59:23 +0000214 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000215 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000216
reed@google.comac10a2d2010-12-22 21:39:39 +0000217 fRenderTarget = NULL;
218 fNeedClear = false;
219
reed@google.comaf951c92011-06-16 19:10:39 +0000220 if (config != SkBitmap::kRGB_565_Config) {
221 config = SkBitmap::kARGB_8888_Config;
222 }
223 SkBitmap bm;
224 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000225
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000226 GrTextureDesc desc;
227 desc.fFlags = kRenderTarget_GrTextureFlagBit;
228 desc.fWidth = width;
229 desc.fHeight = height;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000230 desc.fConfig = SkBitmapConfig2GrPixelConfig(bm.config());
reed@google.comac10a2d2010-12-22 21:39:39 +0000231
bsalomon@google.coma2921122012-08-28 12:34:17 +0000232 SkAutoTUnref<GrTexture> texture(fContext->createUncachedTexture(desc, NULL, 0));
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000233
bsalomon@google.coma2921122012-08-28 12:34:17 +0000234 if (NULL != texture) {
235 fRenderTarget = texture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000236 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000237
reed@google.comaf951c92011-06-16 19:10:39 +0000238 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000239
reed@google.comaf951c92011-06-16 19:10:39 +0000240 // wrap the bitmap with a pixelref to expose our texture
bsalomon@google.coma2921122012-08-28 12:34:17 +0000241 SkGrPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (texture));
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000242 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000243 } else {
244 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
245 width, height);
246 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000247 }
248}
249
250SkGpuDevice::~SkGpuDevice() {
251 if (fDrawProcs) {
252 delete fDrawProcs;
253 }
254
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000255 // The GrContext takes a ref on the target. We don't want to cause the render
256 // target to be unnecessarily kept alive.
257 if (fContext->getRenderTarget() == fRenderTarget) {
258 fContext->setRenderTarget(NULL);
259 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000260
bsalomon@google.coma2921122012-08-28 12:34:17 +0000261 SkSafeUnref(fRenderTarget);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000262 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000263}
264
reed@google.comac10a2d2010-12-22 21:39:39 +0000265///////////////////////////////////////////////////////////////////////////////
266
267void SkGpuDevice::makeRenderTargetCurrent() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000268 DO_DEFERRED_CLEAR();
reed@google.comac10a2d2010-12-22 21:39:39 +0000269 fContext->setRenderTarget(fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000270}
271
272///////////////////////////////////////////////////////////////////////////////
273
bsalomon@google.comc4364992011-11-07 15:54:49 +0000274namespace {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000275GrPixelConfig config8888_to_grconfig_and_flags(SkCanvas::Config8888 config8888, uint32_t* flags) {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000276 switch (config8888) {
277 case SkCanvas::kNative_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000278 *flags = 0;
279 return kSkia8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000280 case SkCanvas::kNative_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000281 *flags = GrContext::kUnpremul_PixelOpsFlag;
282 return kSkia8888_PM_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000283 case SkCanvas::kBGRA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000284 *flags = 0;
285 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000286 case SkCanvas::kBGRA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000287 *flags = GrContext::kUnpremul_PixelOpsFlag;
288 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000289 case SkCanvas::kRGBA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000290 *flags = 0;
291 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000292 case SkCanvas::kRGBA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000293 *flags = GrContext::kUnpremul_PixelOpsFlag;
294 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000295 default:
296 GrCrash("Unexpected Config8888.");
bsalomon@google.comccaa0022012-09-25 19:55:07 +0000297 *flags = 0; // suppress warning
bsalomon@google.comc4364992011-11-07 15:54:49 +0000298 return kSkia8888_PM_GrPixelConfig;
299 }
300}
301}
302
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000303bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
304 int x, int y,
305 SkCanvas::Config8888 config8888) {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000306 DO_DEFERRED_CLEAR();
bsalomon@google.com910267d2011-11-02 20:06:25 +0000307 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
308 SkASSERT(!bitmap.isNull());
309 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000310
bsalomon@google.com910267d2011-11-02 20:06:25 +0000311 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000312 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000313 uint32_t flags;
314 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000315 return fContext->readRenderTargetPixels(fRenderTarget,
316 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000317 bitmap.width(),
318 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000319 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000320 bitmap.getPixels(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000321 bitmap.rowBytes(),
322 flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000323}
324
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000325void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
326 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000327 SkAutoLockPixels alp(bitmap);
328 if (!bitmap.readyToDraw()) {
329 return;
330 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000331
332 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000333 uint32_t flags;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000334 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000335 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000336 } else {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000337 flags = 0;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000338 config= SkBitmapConfig2GrPixelConfig(bitmap.config());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000339 }
340
bsalomon@google.com6f379512011-11-16 20:36:03 +0000341 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000342 config, bitmap.getPixels(), bitmap.rowBytes(), flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000343}
344
robertphillips@google.com46f93502012-08-07 15:38:08 +0000345namespace {
346void purgeClipCB(int genID, void* data) {
347 GrContext* context = (GrContext*) data;
348
349 if (SkClipStack::kInvalidGenID == genID ||
350 SkClipStack::kEmptyGenID == genID ||
351 SkClipStack::kWideOpenGenID == genID) {
352 // none of these cases will have a cached clip mask
353 return;
354 }
355
356}
357};
358
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000359void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
360 INHERITED::onAttachToCanvas(canvas);
361
362 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000363 fClipData.fClipStack = canvas->getClipStack();
robertphillips@google.com46f93502012-08-07 15:38:08 +0000364
365 fClipData.fClipStack->addPurgeClipCallback(purgeClipCB, fContext);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000366}
367
368void SkGpuDevice::onDetachFromCanvas() {
369 INHERITED::onDetachFromCanvas();
370
robertphillips@google.com46f93502012-08-07 15:38:08 +0000371 // TODO: iterate through the clip stack and clean up any cached clip masks
372 fClipData.fClipStack->removePurgeClipCallback(purgeClipCB, fContext);
373
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000374 fClipData.fClipStack = NULL;
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000375}
376
robertphillips@google.com607fe072012-07-24 13:54:00 +0000377#ifdef SK_DEBUG
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000378static void check_bounds(const GrClipData& clipData,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000379 const SkRegion& clipRegion,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000380 int renderTargetWidth,
381 int renderTargetHeight) {
382
robertphillips@google.com7b112892012-07-31 15:18:21 +0000383 SkIRect devBound;
384
385 devBound.setLTRB(0, 0, renderTargetWidth, renderTargetHeight);
386
robertphillips@google.com607fe072012-07-24 13:54:00 +0000387 SkClipStack::BoundsType boundType;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000388 SkRect canvTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000389
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000390 clipData.fClipStack->getBounds(&canvTemp, &boundType);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000391 if (SkClipStack::kNormal_BoundsType == boundType) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000392 SkIRect devTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000393
robertphillips@google.com7b112892012-07-31 15:18:21 +0000394 canvTemp.roundOut(&devTemp);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000395
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000396 devTemp.offset(-clipData.fOrigin.fX, -clipData.fOrigin.fY);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000397
robertphillips@google.com7b112892012-07-31 15:18:21 +0000398 if (!devBound.intersect(devTemp)) {
399 devBound.setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000400 }
401 }
402
robertphillips@google.com768fee82012-08-02 12:42:43 +0000403 GrAssert(devBound.contains(clipRegion.getBounds()));
robertphillips@google.com607fe072012-07-24 13:54:00 +0000404}
405#endif
406
reed@google.comac10a2d2010-12-22 21:39:39 +0000407///////////////////////////////////////////////////////////////////////////////
408
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000409// call this every draw call, to ensure that the context reflects our state,
reed@google.comac10a2d2010-12-22 21:39:39 +0000410// and not the state from some other canvas/device
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000411void SkGpuDevice::prepareDraw(const SkDraw& draw) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000412 GrAssert(NULL != fClipData.fClipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000413
reed@google.comac10a2d2010-12-22 21:39:39 +0000414 fContext->setRenderTarget(fRenderTarget);
415
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000416 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000417
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000418 fContext->setMatrix(*draw.fMatrix);
419 fClipData.fOrigin = this->getOrigin();
reed@google.comac10a2d2010-12-22 21:39:39 +0000420
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000421#ifdef SK_DEBUG
422 check_bounds(fClipData, *draw.fClip, fRenderTarget->width(), fRenderTarget->height());
423#endif
424
425 fContext->setClip(&fClipData);
426
427 DO_DEFERRED_CLEAR();
reed@google.comac10a2d2010-12-22 21:39:39 +0000428}
429
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000430SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000431 DO_DEFERRED_CLEAR();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000432 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000433}
434
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000435bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
bsalomon@google.coma2921122012-08-28 12:34:17 +0000436 GrTexture* texture = fRenderTarget->asTexture();
437 if (NULL != texture) {
bsalomon@google.com88becf42012-10-05 14:54:42 +0000438 paint->colorSampler(kBitmapTextureIdx)->setCustomStage(
bsalomon@google.coma2921122012-08-28 12:34:17 +0000439 SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000440 return true;
441 }
442 return false;
443}
444
445///////////////////////////////////////////////////////////////////////////////
446
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000447SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
448SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
449SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
450SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
451SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
452 shader_type_mismatch);
rileya@google.com3e332582012-07-03 13:43:35 +0000453SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
454 shader_type_mismatch);
rileya@google.com22e57f92012-07-19 15:16:19 +0000455SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
456SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000457
bsalomon@google.com84405e02012-03-05 19:57:21 +0000458namespace {
459
460// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
461// justAlpha indicates that skPaint's alpha should be used rather than the color
462// Callers may subsequently modify the GrPaint. Setting constantColor indicates
463// that the final paint will draw the same color at every pixel. This allows
464// an optimization where the the color filter can be applied to the skPaint's
twiz@google.com58071162012-07-18 21:41:50 +0000465// color once while converting to GrPaint and then ignored.
466inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
467 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000468 bool justAlpha,
469 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000470 SkGpuDevice::SkAutoCachedTexture* act,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000471 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000472
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000473 grPaint->setDither(skPaint.isDither());
474 grPaint->setAntiAlias(skPaint.isAntiAlias());
bsalomon@google.com5782d712011-01-21 21:03:59 +0000475
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000476 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
477 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000478
479 SkXfermode* mode = skPaint.getXfermode();
480 if (mode) {
481 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000482 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000483#if 0
484 return false;
485#endif
486 }
487 }
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000488 grPaint->setBlendFunc(sk_blend_to_grblend(sm), sk_blend_to_grblend(dm));
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000489
bsalomon@google.com5782d712011-01-21 21:03:59 +0000490 if (justAlpha) {
491 uint8_t alpha = skPaint.getAlpha();
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000492 grPaint->setColor(GrColorPackRGBA(alpha, alpha, alpha, alpha));
Scroggod757df22011-05-16 13:11:16 +0000493 // justAlpha is currently set to true only if there is a texture,
494 // so constantColor should not also be true.
495 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000496 } else {
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000497 grPaint->setColor(SkColor2GrColor(skPaint.getColor()));
bsalomon@google.com88becf42012-10-05 14:54:42 +0000498 GrAssert(!grPaint->isColorStageEnabled(kShaderTextureIdx));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000499 }
Scroggo97c88c22011-05-11 14:05:25 +0000500 SkColorFilter* colorFilter = skPaint.getColorFilter();
501 SkColor color;
502 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000503 SkScalar matrix[20];
twiz@google.com58071162012-07-18 21:41:50 +0000504 SkBitmap colorTransformTable;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000505 // TODO: SkColorFilter::asCustomStage()
Scroggo97c88c22011-05-11 14:05:25 +0000506 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
Scroggod757df22011-05-16 13:11:16 +0000507 if (!constantColor) {
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000508 grPaint->setXfermodeColorFilter(filterMode, SkColor2GrColor(color));
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000509 } else {
510 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000511 grPaint->setColor(SkColor2GrColor(filtered));
Scroggod757df22011-05-16 13:11:16 +0000512 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000513 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000514 grPaint->setColorMatrix(matrix);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000515 } else if (colorFilter != NULL && colorFilter->asComponentTable(&colorTransformTable)) {
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000516 // pass NULL because the color table effect doesn't use tiling or filtering.
517 GrTexture* texture = act->set(dev, colorTransformTable, NULL);
bsalomon@google.com88becf42012-10-05 14:54:42 +0000518 GrSamplerState* colorSampler = grPaint->colorSampler(kColorFilterTextureIdx);
bsalomon@google.comb8670992012-07-25 21:27:09 +0000519 colorSampler->reset();
bsalomon@google.comcbd0ad92012-07-20 15:09:31 +0000520 colorSampler->setCustomStage(SkNEW_ARGS(GrColorTableEffect, (texture)))->unref();
Scroggo97c88c22011-05-11 14:05:25 +0000521 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000522 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000523}
524
bsalomon@google.com84405e02012-03-05 19:57:21 +0000525// This function is similar to skPaint2GrPaintNoShader but also converts
526// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
527// be used is set on grPaint and returned in param act. constantColor has the
528// same meaning as in skPaint2GrPaintNoShader.
529inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
530 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000531 bool constantColor,
bsalomon@google.com88becf42012-10-05 14:54:42 +0000532 SkGpuDevice::SkAutoCachedTexture textures[GrPaint::kMaxColorStages],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000533 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000534 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000535 if (NULL == shader) {
twiz@google.com58071162012-07-18 21:41:50 +0000536 return skPaint2GrPaintNoShader(dev,
537 skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000538 false,
539 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000540 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000541 grPaint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000542 } else if (!skPaint2GrPaintNoShader(dev, skPaint, true, false,
twiz@google.com58071162012-07-18 21:41:50 +0000543 &textures[kColorFilterTextureIdx], grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000544 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000545 }
546
bsalomon@google.com88becf42012-10-05 14:54:42 +0000547 GrSamplerState* sampler = grPaint->colorSampler(kShaderTextureIdx);
rileya@google.com91f319c2012-07-25 17:18:31 +0000548 GrCustomStage* stage = shader->asNewCustomStage(dev->context(), sampler);
549
550 if (NULL != stage) {
551 sampler->setCustomStage(stage)->unref();
552 SkMatrix localM;
553 if (shader->getLocalMatrix(&localM)) {
554 SkMatrix inverse;
555 if (localM.invert(&inverse)) {
556 sampler->matrix()->preConcat(inverse);
557 }
558 }
559 return true;
560 }
561
reed@google.comac10a2d2010-12-22 21:39:39 +0000562 SkBitmap bitmap;
rileya@google.com91f319c2012-07-25 17:18:31 +0000563 SkMatrix* matrix = sampler->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000564 SkShader::TileMode tileModes[2];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000565 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
rileya@google.com91f319c2012-07-25 17:18:31 +0000566 tileModes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000567
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000568 if (SkShader::kNone_BitmapType == bmptype) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000569 SkShader::GradientInfo info;
570 SkColor color;
571
572 info.fColors = &color;
573 info.fColorOffsets = NULL;
574 info.fColorCount = 1;
575 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
576 SkPaint copy(skPaint);
577 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000578 // modulate the paint alpha by the shader's solid color alpha
579 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
580 copy.setColor(SkColorSetA(color, newA));
twiz@google.com58071162012-07-18 21:41:50 +0000581 return skPaint2GrPaintNoShader(dev,
582 copy,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000583 false,
584 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000585 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000586 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000587 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000588 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000589 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000590
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000591 // Must set wrap and filter on the sampler before requesting a texture.
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000592 GrTextureParams params(tileModes, skPaint.isFilterBitmap());
593 GrTexture* texture = textures[kShaderTextureIdx].set(dev, bitmap, &params);
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000594
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000595 if (NULL == texture) {
596 SkDebugf("Couldn't convert bitmap to texture.\n");
597 return false;
598 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000599
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000600 sampler->reset();
601 sampler->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect, (texture, params)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000602
reed@google.comac10a2d2010-12-22 21:39:39 +0000603 // since our texture coords will be in local space, we wack the texture
604 // matrix to map them back into 0...1 before we load it
605 SkMatrix localM;
606 if (shader->getLocalMatrix(&localM)) {
607 SkMatrix inverse;
608 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000609 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000610 }
611 }
612 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000613 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
614 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000615 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000616 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000617
618 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000619}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000620}
reed@google.comac10a2d2010-12-22 21:39:39 +0000621
622///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000623void SkGpuDevice::clear(SkColor color) {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000624 fContext->clear(NULL, color, fRenderTarget);
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000625 fNeedClear = false;
bsalomon@google.com398109c2011-04-14 18:40:27 +0000626}
627
reed@google.comac10a2d2010-12-22 21:39:39 +0000628void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
629 CHECK_SHOULD_DRAW(draw);
630
bsalomon@google.com5782d712011-01-21 21:03:59 +0000631 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +0000632 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000633 if (!skPaint2GrPaintShader(this,
634 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000635 true,
twiz@google.com58071162012-07-18 21:41:50 +0000636 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000637 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000638 return;
639 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000640
641 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000642}
643
644// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000645static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000646 kPoints_GrPrimitiveType,
647 kLines_GrPrimitiveType,
648 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000649};
650
651void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000652 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000653 CHECK_SHOULD_DRAW(draw);
654
655 SkScalar width = paint.getStrokeWidth();
656 if (width < 0) {
657 return;
658 }
659
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000660 // we only handle hairlines and paints without path effects or mask filters,
661 // else we let the SkDraw call our drawPath()
662 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000663 draw.drawPoints(mode, count, pts, paint, true);
664 return;
665 }
666
bsalomon@google.com5782d712011-01-21 21:03:59 +0000667 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +0000668 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000669 if (!skPaint2GrPaintShader(this,
670 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000671 true,
twiz@google.com58071162012-07-18 21:41:50 +0000672 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000673 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000674 return;
675 }
676
bsalomon@google.com5782d712011-01-21 21:03:59 +0000677 fContext->drawVertices(grPaint,
678 gPointMode2PrimtiveType[mode],
679 count,
680 (GrPoint*)pts,
681 NULL,
682 NULL,
683 NULL,
684 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000685}
686
reed@google.comc9aa5872011-04-05 21:05:37 +0000687///////////////////////////////////////////////////////////////////////////////
688
reed@google.comac10a2d2010-12-22 21:39:39 +0000689void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
690 const SkPaint& paint) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000691 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000692 CHECK_SHOULD_DRAW(draw);
693
bungeman@google.com79bd8772011-07-18 15:34:08 +0000694 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000695 SkScalar width = paint.getStrokeWidth();
696
697 /*
698 We have special code for hairline strokes, miter-strokes, and fills.
699 Anything else we just call our path code.
700 */
701 bool usePath = doStroke && width > 0 &&
702 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000703 // another two reasons we might need to call drawPath...
704 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000705 usePath = true;
706 }
reed@google.com67db6642011-05-26 11:46:35 +0000707 // until we aa rotated rects...
708 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
709 usePath = true;
710 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000711 // small miter limit means right angles show bevel...
712 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
713 paint.getStrokeMiter() < SK_ScalarSqrt2)
714 {
715 usePath = true;
716 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000717 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000718 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
719 usePath = true;
720 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000721
722 if (usePath) {
723 SkPath path;
724 path.addRect(rect);
725 this->drawPath(draw, path, paint, NULL, true);
726 return;
727 }
728
729 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +0000730 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000731 if (!skPaint2GrPaintShader(this,
732 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000733 true,
twiz@google.com58071162012-07-18 21:41:50 +0000734 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000735 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000736 return;
737 }
reed@google.com20efde72011-05-09 17:00:02 +0000738 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000739}
740
reed@google.com69302852011-02-16 18:08:07 +0000741#include "SkMaskFilter.h"
742#include "SkBounder.h"
743
bsalomon@google.com85003222012-03-28 14:44:37 +0000744///////////////////////////////////////////////////////////////////////////////
745
746// helpers for applying mask filters
747namespace {
748
749GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000750 switch (fillType) {
751 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000752 return kWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000753 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000754 return kEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000755 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000756 return kInverseWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000757 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000758 return kInverseEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000759 default:
760 SkDebugf("Unsupported path fill type\n");
bsalomon@google.com47059542012-06-06 20:51:20 +0000761 return kHairLine_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000762 }
763}
764
bsalomon@google.com85003222012-03-28 14:44:37 +0000765// We prefer to blur small rect with small radius via CPU.
766#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
767#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
768inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
769 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
770 rect.height() <= MIN_GPU_BLUR_SIZE &&
771 radius <= MIN_GPU_BLUR_RADIUS) {
772 return true;
773 }
774 return false;
775}
776
777bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
778 SkMaskFilter* filter, const SkMatrix& matrix,
779 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000780 GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000781#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000782 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000783#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000784 SkMaskFilter::BlurInfo info;
785 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000786 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000787 return false;
788 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000789 SkScalar radius = info.fIgnoreTransform ? info.fRadius
790 : matrix.mapRadius(info.fRadius);
791 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000792 if (radius <= 0) {
793 return false;
794 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000795
796 SkRect srcRect = path.getBounds();
797 if (shouldDrawBlurWithCPU(srcRect, radius)) {
798 return false;
799 }
800
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000801 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000802 float sigma3 = sigma * 3.0f;
803
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000804 SkRect clipRect;
805 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000806
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000807 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000808 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
809 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000810 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000811 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000812 SkIRect finalIRect;
813 finalRect.roundOut(&finalIRect);
814 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000815 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000816 }
817 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000818 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000819 }
820 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000821 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000822 GrTextureDesc desc;
823 desc.fFlags = kRenderTarget_GrTextureFlagBit;
824 desc.fWidth = SkScalarCeilToInt(srcRect.width());
825 desc.fHeight = SkScalarCeilToInt(srcRect.height());
826 // We actually only need A8, but it often isn't supported as a
827 // render target so default to RGBA_8888
bsalomon@google.com0342a852012-08-20 19:22:38 +0000828 desc.fConfig = kRGBA_8888_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000829
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000830 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
831 desc.fConfig = kAlpha_8_GrPixelConfig;
832 }
833
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000834 GrAutoScratchTexture pathEntry(context, desc);
835 GrTexture* pathTexture = pathEntry.texture();
836 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000837 return false;
838 }
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000839
robertphillips@google.comccb39502012-10-01 18:25:13 +0000840 SkAutoTUnref<GrTexture> blurTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000841
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000842 // We pass kPreserve here. We will replace the current matrix below.
843 GrContext::AutoMatrix avm(context, GrContext::AutoMatrix::kPreserve_InitialMatrix);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000844
robertphillips@google.comccb39502012-10-01 18:25:13 +0000845 {
846 GrContext::AutoRenderTarget art(context, pathTexture->asRenderTarget());
847 GrContext::AutoClip ac(context, srcRect);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000848
robertphillips@google.comccb39502012-10-01 18:25:13 +0000849 context->clear(NULL, 0);
850 GrPaint tempPaint;
robertphillips@google.comccb39502012-10-01 18:25:13 +0000851
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000852 if (grp->isAntiAlias()) {
853 tempPaint.setAntiAlias(true);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000854 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
855 // blend coeff of zero requires dual source blending support in order
856 // to properly blend partially covered pixels. This means the AA
857 // code path may not be taken. So we use a dst blend coeff of ISA. We
858 // could special case AA draws to a dst surface with known alpha=0 to
859 // use a zero dst coeff when dual source blending isn't available.
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000860 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000861 }
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000862 // Draw hard shadow to pathTexture with path top-left at origin 0,0.
863 GrMatrix translate;
864 translate.setTranslate(offset.fX, offset.fY);
865 context->setMatrix(translate);
866 context->drawPath(tempPaint, path, pathFillType);
867
868 // switch to device coord drawing when going back to the main RT.
869 context->setIdentityMatrix();
robertphillips@google.comccb39502012-10-01 18:25:13 +0000870
871 // If we're doing a normal blur, we can clobber the pathTexture in the
872 // gaussianBlur. Otherwise, we need to save it for later compositing.
873 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
skia.committer@gmail.comdc3a4e52012-10-02 02:01:24 +0000874 blurTexture.reset(context->gaussianBlur(pathTexture, isNormalBlur,
robertphillips@google.comccb39502012-10-01 18:25:13 +0000875 srcRect, sigma, sigma));
876
877 if (!isNormalBlur) {
878 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.come3d32162012-07-20 13:37:06 +0000902 if (!grp->preConcatSamplerMatricesWithInverse(matrix)) {
903 return false;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000904 }
905
bsalomon@google.com88becf42012-10-05 14:54:42 +0000906 static const int MASK_IDX = GrPaint::kMaxCoverageStages - 1;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000907 // we assume the last mask index is available for use
bsalomon@google.com88becf42012-10-05 14:54:42 +0000908 GrAssert(!grp->isCoverageStageEnabled(MASK_IDX));
909 grp->coverageSampler(MASK_IDX)->reset();
910 grp->coverageSampler(MASK_IDX)->setCustomStage(
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000911 SkNEW_ARGS(GrSingleTextureEffect, (blurTexture)))->unref();
bsalomon@google.com88becf42012-10-05 14:54:42 +0000912 grp->coverageSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000913 -finalRect.fTop);
bsalomon@google.com88becf42012-10-05 14:54:42 +0000914 grp->coverageSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000915 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000916 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000917 return true;
918}
919
bsalomon@google.com85003222012-03-28 14:44:37 +0000920bool drawWithMaskFilter(GrContext* context, const SkPath& path,
921 SkMaskFilter* filter, const SkMatrix& matrix,
922 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
926 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000927 SkMask::kComputeBoundsAndRenderImage_CreateMode,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000928 style)) {
reed@google.com69302852011-02-16 18:08:07 +0000929 return false;
930 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000931 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000932
933 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
934 return false;
935 }
936 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000937 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000938
939 if (clip.quickReject(dstM.fBounds)) {
940 return false;
941 }
942 if (bounder && !bounder->doIRect(dstM.fBounds)) {
943 return false;
944 }
945
946 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
947 // the current clip (and identity matrix) and grpaint settings
948
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000949 GrContext::AutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +0000950
bsalomon@google.come3d32162012-07-20 13:37:06 +0000951 if (!grp->preConcatSamplerMatricesWithInverse(matrix)) {
952 return false;
953 }
954
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000955 GrTextureDesc desc;
956 desc.fWidth = dstM.fBounds.width();
957 desc.fHeight = dstM.fBounds.height();
958 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +0000959
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000960 GrAutoScratchTexture ast(context, desc);
961 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000962
reed@google.com69302852011-02-16 18:08:07 +0000963 if (NULL == texture) {
964 return false;
965 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000966 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000967 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000968
bsalomon@google.com88becf42012-10-05 14:54:42 +0000969 static const int MASK_IDX = GrPaint::kMaxCoverageStages - 1;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000970 // we assume the last mask index is available for use
bsalomon@google.com88becf42012-10-05 14:54:42 +0000971 GrAssert(!grp->isCoverageStageEnabled(MASK_IDX));
972 grp->coverageSampler(MASK_IDX)->reset();
973 grp->coverageSampler(MASK_IDX)->setCustomStage(
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000974 SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000975 GrRect d;
976 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +0000977 GrIntToScalar(dstM.fBounds.fTop),
978 GrIntToScalar(dstM.fBounds.fRight),
979 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000980
bsalomon@google.com88becf42012-10-05 14:54:42 +0000981 GrMatrix* m = grp->coverageSampler(MASK_IDX)->matrix();
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000982 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
983 -dstM.fBounds.fTop*SK_Scalar1);
984 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000985 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000986 return true;
987}
reed@google.com69302852011-02-16 18:08:07 +0000988
bsalomon@google.com85003222012-03-28 14:44:37 +0000989}
990
991///////////////////////////////////////////////////////////////////////////////
992
reed@google.com0c219b62011-02-16 21:31:18 +0000993void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000994 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000995 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000996 CHECK_FOR_NODRAW_ANNOTATION(paint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000997 CHECK_SHOULD_DRAW(draw);
998
reed@google.comfe626382011-09-21 13:50:35 +0000999 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001000
bsalomon@google.com5782d712011-01-21 21:03:59 +00001001 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +00001002 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001003 if (!skPaint2GrPaintShader(this,
1004 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001005 true,
twiz@google.com58071162012-07-18 21:41:50 +00001006 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001007 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001008 return;
1009 }
1010
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +00001011 // can we cheat, and threat a thin stroke as a hairline w/ coverage
1012 // if we can, we draw lots faster (raster device does this same test)
1013 SkScalar hairlineCoverage;
1014 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
1015 doFill = false;
bsalomon@google.comc7448ce2012-10-05 19:04:13 +00001016 grPaint.setCoverage(SkScalarRoundToInt(hairlineCoverage * grPaint.getCoverage()));
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +00001017 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001018
reed@google.comfe626382011-09-21 13:50:35 +00001019 // If we have a prematrix, apply it to the path, optimizing for the case
1020 // where the original path can in fact be modified in place (even though
1021 // its parameter type is const).
1022 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1023 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001024
1025 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001026 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001027
reed@google.come3445642011-02-16 23:20:39 +00001028 if (!pathIsMutable) {
1029 result = &tmpPath;
1030 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001031 }
reed@google.come3445642011-02-16 23:20:39 +00001032 // should I push prePathMatrix on our MV stack temporarily, instead
1033 // of applying it here? See SkDraw.cpp
1034 pathPtr->transform(*prePathMatrix, result);
1035 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001036 }
reed@google.com0c219b62011-02-16 21:31:18 +00001037 // at this point we're done with prePathMatrix
1038 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001039
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001040 if (paint.getPathEffect() ||
1041 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001042 // it is safe to use tmpPath here, even if we already used it for the
1043 // prepathmatrix, since getFillPath can take the same object for its
1044 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001045 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001046 pathPtr = &tmpPath;
1047 }
1048
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001049 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001050 // avoid possibly allocating a new path in transform if we can
1051 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1052
1053 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001054 pathPtr->transform(*draw.fMatrix, devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001055 GrPathFill pathFillType = doFill ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001056 skToGrFillType(devPathPtr->getFillType()) : kHairLine_GrPathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001057 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001058 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001059 &grPaint, pathFillType)) {
rmistry@google.comd6176b02012-08-23 18:14:13 +00001060 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001061 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001062 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001063 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001064 &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001065 }
reed@google.com69302852011-02-16 18:08:07 +00001066 return;
1067 }
reed@google.com69302852011-02-16 18:08:07 +00001068
bsalomon@google.com47059542012-06-06 20:51:20 +00001069 GrPathFill fill = kHairLine_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001070
reed@google.com0c219b62011-02-16 21:31:18 +00001071 if (doFill) {
1072 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001073 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001074 fill = kWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001075 break;
1076 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001077 fill = kEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001078 break;
1079 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001080 fill = kInverseWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001081 break;
1082 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001083 fill = kInverseEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001084 break;
1085 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001086 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001087 return;
1088 }
1089 }
1090
reed@google.com07f3ee12011-05-16 17:21:57 +00001091 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001092}
1093
bsalomon@google.comfb309512011-11-30 14:13:48 +00001094namespace {
1095
1096inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1097 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1098 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1099 return tilesX * tilesY;
1100}
1101
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001102inline int determine_tile_size(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001103 const SkRect& src,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001104 int maxTextureSize) {
1105 static const int kSmallTileSize = 1 << 10;
1106 if (maxTextureSize <= kSmallTileSize) {
1107 return maxTextureSize;
1108 }
1109
1110 size_t maxTexTotalTileSize;
1111 size_t smallTotalTileSize;
1112
robertphillips@google.combac6b052012-09-28 18:06:49 +00001113 SkIRect iSrc;
1114 src.roundOut(&iSrc);
1115
1116 maxTexTotalTileSize = get_tile_count(iSrc.fLeft,
1117 iSrc.fTop,
1118 iSrc.fRight,
1119 iSrc.fBottom,
1120 maxTextureSize);
1121 smallTotalTileSize = get_tile_count(iSrc.fLeft,
1122 iSrc.fTop,
1123 iSrc.fRight,
1124 iSrc.fBottom,
1125 kSmallTileSize);
1126
bsalomon@google.comfb309512011-11-30 14:13:48 +00001127 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1128 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1129
1130 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1131 return kSmallTileSize;
1132 } else {
1133 return maxTextureSize;
1134 }
1135}
1136}
1137
1138bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001139 const GrTextureParams& params,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001140 const SkRect* srcRectPtr) const {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001141 // if bitmap is explictly texture backed then just use the texture
1142 if (NULL != bitmap.getTexture()) {
1143 return false;
1144 }
1145 // if it's larger than the max texture size, then we have no choice but
1146 // tiling
1147 const int maxTextureSize = fContext->getMaxTextureSize();
1148 if (bitmap.width() > maxTextureSize ||
1149 bitmap.height() > maxTextureSize) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001150 return true;
1151 }
1152 // if we are going to have to draw the whole thing, then don't tile
1153 if (NULL == srcRectPtr) {
1154 return false;
1155 }
1156 // if the entire texture is already in our cache then no reason to tile it
bsalomon@google.comb8670992012-07-25 21:27:09 +00001157 if (this->isBitmapInTextureCache(bitmap, params)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001158 return false;
1159 }
1160
1161 // At this point we know we could do the draw by uploading the entire bitmap
1162 // as a texture. However, if the texture would be large compared to the
1163 // cache size and we don't require most of it for this draw then tile to
1164 // reduce the amount of upload and cache spill.
1165
1166 // assumption here is that sw bitmap size is a good proxy for its size as
1167 // a texture
1168 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001169 size_t cacheSize;
1170 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001171 if (bmpSize < cacheSize / 2) {
1172 return false;
1173 }
1174
robertphillips@google.combac6b052012-09-28 18:06:49 +00001175 SkScalar fracUsed = SkScalarMul(srcRectPtr->width() / bitmap.width(),
1176 srcRectPtr->height() / bitmap.height());
1177 if (fracUsed <= SK_ScalarHalf) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001178 return true;
1179 } else {
1180 return false;
1181 }
1182}
1183
reed@google.comac10a2d2010-12-22 21:39:39 +00001184void SkGpuDevice::drawBitmap(const SkDraw& draw,
1185 const SkBitmap& bitmap,
1186 const SkIRect* srcRectPtr,
1187 const SkMatrix& m,
1188 const SkPaint& paint) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001189
1190 SkRect tmp;
1191 SkRect* tmpPtr = NULL;
1192
1193 // convert from SkIRect to SkRect
1194 if (NULL != srcRectPtr) {
1195 tmp.set(*srcRectPtr);
1196 tmpPtr = &tmp;
1197 }
1198
1199 // We cannot call drawBitmapRect here since 'm' could be anything
1200 this->drawBitmapCommon(draw, bitmap, tmpPtr, m, paint);
1201}
1202
1203void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1204 const SkBitmap& bitmap,
1205 const SkRect* srcRectPtr,
1206 const SkMatrix& m,
1207 const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001208 CHECK_SHOULD_DRAW(draw);
1209
robertphillips@google.combac6b052012-09-28 18:06:49 +00001210 SkRect srcRect;
reed@google.comac10a2d2010-12-22 21:39:39 +00001211 if (NULL == srcRectPtr) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001212 srcRect.set(0, 0, SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001213 } else {
1214 srcRect = *srcRectPtr;
1215 }
1216
junov@google.comd935cfb2011-06-27 20:48:23 +00001217 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001218 // Convert the bitmap to a shader so that the rect can be drawn
1219 // through drawRect, which supports mask filters.
robertphillips@google.combac6b052012-09-28 18:06:49 +00001220 SkMatrix newM(m);
junov@google.com1d329782011-07-28 20:10:09 +00001221 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001222 const SkBitmap* bitmapPtr = &bitmap;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001223 if (NULL != srcRectPtr) {
1224 SkIRect iSrc;
1225 srcRect.roundOut(&iSrc);
1226 if (!bitmap.extractSubset(&tmp, iSrc)) {
epoger@google.com9ef2d832011-07-01 21:12:20 +00001227 return; // extraction failed
1228 }
1229 bitmapPtr = &tmp;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001230 srcRect.offset(SkIntToScalar(-iSrc.fLeft), SkIntToScalar(-iSrc.fTop));
1231 // The source rect has changed so update the matrix
1232 newM.preTranslate(SkIntToScalar(iSrc.fLeft), SkIntToScalar(iSrc.fTop));
junov@google.comd935cfb2011-06-27 20:48:23 +00001233 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001234
junov@google.comd935cfb2011-06-27 20:48:23 +00001235 SkPaint paintWithTexture(paint);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001236 paintWithTexture.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
junov@google.comd935cfb2011-06-27 20:48:23 +00001237 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001238
robertphillips@google.combac6b052012-09-28 18:06:49 +00001239 // Transform 'newM' needs to be concatenated to the draw matrix,
1240 // rather than transforming the primitive directly, so that 'newM' will
junov@google.com1d329782011-07-28 20:10:09 +00001241 // also affect the behavior of the mask filter.
1242 SkMatrix drawMatrix;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001243 drawMatrix.setConcat(*draw.fMatrix, newM);
junov@google.com1d329782011-07-28 20:10:09 +00001244 SkDraw transformedDraw(draw);
1245 transformedDraw.fMatrix = &drawMatrix;
1246
robertphillips@google.combac6b052012-09-28 18:06:49 +00001247 this->drawRect(transformedDraw, srcRect, paintWithTexture);
junov@google.com1d329782011-07-28 20:10:09 +00001248
junov@google.comd935cfb2011-06-27 20:48:23 +00001249 return;
1250 }
1251
bsalomon@google.com5782d712011-01-21 21:03:59 +00001252 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001253 SkAutoCachedTexture colorLutTexture;
1254 if (!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001255 return;
1256 }
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001257 GrTextureParams params;
1258 params.setBilerp(paint.isFilterBitmap());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001259
robertphillips@google.combac6b052012-09-28 18:06:49 +00001260 if (!this->shouldTileBitmap(bitmap, params, srcRectPtr)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001261 // take the simple case
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001262 this->internalDrawBitmap(draw, bitmap, srcRect, m, params, &grPaint);
robertphillips@google.combac6b052012-09-28 18:06:49 +00001263 } else {
1264 this->drawTiledBitmap(draw, bitmap, srcRect, m, params, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001265 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001266}
1267
1268// Break 'bitmap' into several tiles to draw it since it has already
1269// been determined to be too large to fit in VRAM
1270void SkGpuDevice::drawTiledBitmap(const SkDraw& draw,
1271 const SkBitmap& bitmap,
1272 const SkRect& srcRect,
1273 const SkMatrix& m,
1274 const GrTextureParams& params,
1275 GrPaint* grPaint) {
1276 const int maxTextureSize = fContext->getMaxTextureSize();
1277
1278 int tileSize = determine_tile_size(bitmap, srcRect, maxTextureSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001279
reed@google.comac10a2d2010-12-22 21:39:39 +00001280 // compute clip bounds in local coordinates
robertphillips@google.combac6b052012-09-28 18:06:49 +00001281 SkRect clipRect;
reed@google.comac10a2d2010-12-22 21:39:39 +00001282 {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001283 clipRect.set(draw.fClip->getBounds());
reed@google.comac10a2d2010-12-22 21:39:39 +00001284 SkMatrix matrix, inverse;
1285 matrix.setConcat(*draw.fMatrix, m);
1286 if (!matrix.invert(&inverse)) {
1287 return;
1288 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001289 inverse.mapRect(&clipRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001290 }
1291
bsalomon@google.comfb309512011-11-30 14:13:48 +00001292 int nx = bitmap.width() / tileSize;
1293 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001294 for (int x = 0; x <= nx; x++) {
1295 for (int y = 0; y <= ny; y++) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001296 SkRect tileR;
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001297 tileR.set(SkIntToScalar(x * tileSize),
robertphillips@google.combac6b052012-09-28 18:06:49 +00001298 SkIntToScalar(y * tileSize),
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001299 SkIntToScalar((x + 1) * tileSize),
robertphillips@google.combac6b052012-09-28 18:06:49 +00001300 SkIntToScalar((y + 1) * tileSize));
1301
1302 if (!SkRect::Intersects(tileR, clipRect)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001303 continue;
1304 }
1305
robertphillips@google.combac6b052012-09-28 18:06:49 +00001306 if (!tileR.intersect(srcRect)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001307 continue;
1308 }
1309
1310 SkBitmap tmpB;
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001311 SkIRect iTileR;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001312 tileR.roundOut(&iTileR);
1313 if (bitmap.extractSubset(&tmpB, iTileR)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001314 // now offset it to make it "local" to our tmp bitmap
robertphillips@google.combac6b052012-09-28 18:06:49 +00001315 tileR.offset(SkIntToScalar(-iTileR.fLeft), SkIntToScalar(-iTileR.fTop));
reed@google.comac10a2d2010-12-22 21:39:39 +00001316 SkMatrix tmpM(m);
skia.committer@gmail.comdc3a4e52012-10-02 02:01:24 +00001317 tmpM.preTranslate(SkIntToScalar(iTileR.fLeft),
robertphillips@google.comffad46b2012-10-01 14:32:51 +00001318 SkIntToScalar(iTileR.fTop));
1319
robertphillips@google.combac6b052012-09-28 18:06:49 +00001320 this->internalDrawBitmap(draw, tmpB, tileR, tmpM, params, grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001321 }
1322 }
1323 }
1324}
1325
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001326namespace {
1327
1328bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1329 // detect pixel disalignment
1330 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1331 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
rmistry@google.comd6176b02012-08-23 18:14:13 +00001332 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001333 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1334 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1335 COLOR_BLEED_TOLERANCE &&
1336 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1337 COLOR_BLEED_TOLERANCE) {
1338 return true;
1339 }
1340 return false;
1341}
1342
1343bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1344 const SkMatrix& m) {
1345 // Only gets called if hasAlignedSamples returned false.
1346 // So we can assume that sampling is axis aligned but not texel aligned.
1347 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001348 SkRect innerSrcRect(srcRect), innerTransformedRect,
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001349 outerTransformedRect(transformedRect);
1350 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1351 m.mapRect(&innerTransformedRect, innerSrcRect);
1352
1353 // The gap between outerTransformedRect and innerTransformedRect
1354 // represents the projection of the source border area, which is
1355 // problematic for color bleeding. We must check whether any
1356 // destination pixels sample the border area.
1357 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1358 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1359 SkIRect outer, inner;
1360 outerTransformedRect.round(&outer);
1361 innerTransformedRect.round(&inner);
1362 // If the inner and outer rects round to the same result, it means the
1363 // border does not overlap any pixel centers. Yay!
1364 return inner != outer;
1365}
1366
1367} // unnamed namespace
1368
reed@google.comac10a2d2010-12-22 21:39:39 +00001369/*
1370 * This is called by drawBitmap(), which has to handle images that may be too
1371 * large to be represented by a single texture.
1372 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001373 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1374 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001375 */
1376void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1377 const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001378 const SkRect& srcRect,
reed@google.comac10a2d2010-12-22 21:39:39 +00001379 const SkMatrix& m,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001380 const GrTextureParams& params,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001381 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001382 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1383 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001384
reed@google.com9c49bc32011-07-07 13:42:37 +00001385 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001386 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001387 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001388 return;
1389 }
1390
bsalomon@google.com88becf42012-10-05 14:54:42 +00001391 GrSamplerState* sampler = grPaint->colorSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001392
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001393 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001394
1395 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001396 SkAutoCachedTexture act(this, bitmap, &params, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001397 if (NULL == texture) {
1398 return;
1399 }
1400
robertphillips@google.combac6b052012-09-28 18:06:49 +00001401 GrRect dstRect(srcRect);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001402 GrRect paintRect;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001403 SkScalar wInv = SkScalarInvert(SkIntToScalar(bitmap.width()));
1404 SkScalar hInv = SkScalarInvert(SkIntToScalar(bitmap.height()));
1405 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1406 SkScalarMul(srcRect.fTop, hInv),
1407 SkScalarMul(srcRect.fRight, wInv),
1408 SkScalarMul(srcRect.fBottom, hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001409
rmistry@google.comd6176b02012-08-23 18:14:13 +00001410 bool needsTextureDomain = false;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001411 if (params.isBilerp()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001412 // Need texture domain if drawing a sub rect.
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001413 needsTextureDomain = srcRect.width() < bitmap.width() ||
robertphillips@google.combac6b052012-09-28 18:06:49 +00001414 srcRect.height() < bitmap.height();
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001415 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1416 // sampling is axis-aligned
robertphillips@google.combac6b052012-09-28 18:06:49 +00001417 GrRect transformedRect;
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001418 SkMatrix srcToDeviceMatrix(m);
1419 srcToDeviceMatrix.postConcat(*draw.fMatrix);
robertphillips@google.combac6b052012-09-28 18:06:49 +00001420 srcToDeviceMatrix.mapRect(&transformedRect, srcRect);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001421
robertphillips@google.combac6b052012-09-28 18:06:49 +00001422 if (hasAlignedSamples(srcRect, transformedRect)) {
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001423 // We could also turn off filtering here (but we already did a cache lookup with
1424 // params).
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001425 needsTextureDomain = false;
1426 } else {
1427 needsTextureDomain = needsTextureDomain &&
robertphillips@google.combac6b052012-09-28 18:06:49 +00001428 mayColorBleed(srcRect, transformedRect, m);
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001429 }
1430 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001431 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001432
1433 GrRect textureDomain = GrRect::MakeEmpty();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001434 SkAutoTUnref<GrCustomStage> stage;
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001435 if (needsTextureDomain) {
1436 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001437 GrScalar left, top, right, bottom;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001438 if (srcRect.width() > GR_Scalar1) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001439 GrScalar border = GR_ScalarHalf / bitmap.width();
1440 left = paintRect.left() + border;
1441 right = paintRect.right() - border;
1442 } else {
1443 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1444 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001445 if (srcRect.height() > GR_Scalar1) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001446 GrScalar border = GR_ScalarHalf / bitmap.height();
1447 top = paintRect.top() + border;
1448 bottom = paintRect.bottom() - border;
1449 } else {
1450 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1451 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001452 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001453 stage.reset(SkNEW_ARGS(GrTextureDomainEffect, (texture, textureDomain, params)));
1454 } else {
1455 stage.reset(SkNEW_ARGS(GrSingleTextureEffect, (texture, params)));
junov@google.com6acc9b32011-05-16 18:32:07 +00001456 }
bsalomon@google.com88becf42012-10-05 14:54:42 +00001457 grPaint->colorSampler(kBitmapTextureIdx)->setCustomStage(stage);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001458 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001459}
1460
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001461namespace {
1462
1463void apply_custom_stage(GrContext* context,
1464 GrTexture* srcTexture,
1465 GrTexture* dstTexture,
1466 const GrRect& rect,
1467 GrCustomStage* stage) {
1468 SkASSERT(srcTexture && srcTexture->getContext() == context);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001469 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001470 GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget());
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001471 GrContext::AutoClip acs(context, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001472
1473 GrMatrix sampleM;
1474 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
1475 GrPaint paint;
bsalomon@google.com88becf42012-10-05 14:54:42 +00001476 paint.colorSampler(0)->reset(sampleM);
1477 paint.colorSampler(0)->setCustomStage(stage);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001478 context->drawRect(paint, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001479}
1480
1481};
1482
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001483static GrTexture* filter_texture(SkDevice* device, GrContext* context,
1484 GrTexture* texture, SkImageFilter* filter,
1485 const GrRect& rect) {
reed@google.com8926b162012-03-23 15:36:36 +00001486 GrAssert(filter);
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001487 SkDeviceImageFilterProxy proxy(device);
reed@google.com8926b162012-03-23 15:36:36 +00001488
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001489 GrTextureDesc desc;
1490 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1491 desc.fWidth = SkScalarCeilToInt(rect.width());
1492 desc.fHeight = SkScalarCeilToInt(rect.height());
bsalomon@google.com0342a852012-08-20 19:22:38 +00001493 desc.fConfig = kRGBA_8888_GrPixelConfig;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001494 GrCustomStage* stage;
reed@google.com8926b162012-03-23 15:36:36 +00001495
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001496 if (filter->canFilterImageGPU()) {
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001497 texture = filter->onFilterImageGPU(&proxy, texture, rect);
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001498 } else if (filter->asNewCustomStage(&stage, texture)) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001499 GrAutoScratchTexture dst(context, desc);
1500 apply_custom_stage(context, texture, dst.texture(), rect, stage);
1501 texture = dst.detach();
1502 stage->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001503 }
1504 return texture;
1505}
1506
reed@google.comac10a2d2010-12-22 21:39:39 +00001507void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1508 int left, int top, const SkPaint& paint) {
1509 CHECK_SHOULD_DRAW(draw);
1510
reed@google.com8926b162012-03-23 15:36:36 +00001511 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001512 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1513 return;
1514 }
1515
reed@google.com76dd2772012-01-05 21:15:07 +00001516 int w = bitmap.width();
1517 int h = bitmap.height();
1518
bsalomon@google.com5782d712011-01-21 21:03:59 +00001519 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001520 SkAutoCachedTexture colorLutTexture;
1521 if(!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001522 return;
1523 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001524
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001525 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001526
bsalomon@google.com88becf42012-10-05 14:54:42 +00001527 GrSamplerState* sampler = grPaint.colorSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001528
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001529 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001530 sampler->reset();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001531 // draw sprite uses the default texture params
1532 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
bsalomon@google.com88becf42012-10-05 14:54:42 +00001533 grPaint.colorSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001534 (GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001535
reed@google.com8926b162012-03-23 15:36:36 +00001536 SkImageFilter* filter = paint.getImageFilter();
1537 if (NULL != filter) {
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001538 GrTexture* filteredTexture = filter_texture(this, fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001539 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001540 if (filteredTexture) {
bsalomon@google.com88becf42012-10-05 14:54:42 +00001541 grPaint.colorSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001542 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001543 texture = filteredTexture;
1544 filteredTexture->unref();
1545 }
reed@google.com76dd2772012-01-05 21:15:07 +00001546 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001547
bsalomon@google.com5782d712011-01-21 21:03:59 +00001548 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001549 GrRect::MakeXYWH(GrIntToScalar(left),
1550 GrIntToScalar(top),
1551 GrIntToScalar(w),
1552 GrIntToScalar(h)),
1553 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1554 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001555}
1556
reed@google.com33535f32012-09-25 15:37:50 +00001557void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1558 const SkRect* src, const SkRect& dst,
1559 const SkPaint& paint) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001560 SkMatrix matrix;
1561 SkRect bitmapBounds, tmpSrc;
1562
1563 bitmapBounds.set(0, 0,
1564 SkIntToScalar(bitmap.width()),
1565 SkIntToScalar(bitmap.height()));
1566
reed@google.com33535f32012-09-25 15:37:50 +00001567 // Compute matrix from the two rectangles
robertphillips@google.combac6b052012-09-28 18:06:49 +00001568 if (NULL != src) {
1569 tmpSrc = *src;
1570 } else {
1571 tmpSrc = bitmapBounds;
1572 }
1573 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1574
1575 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1576 if (NULL != src) {
1577 if (!bitmapBounds.contains(tmpSrc)) {
1578 if (!tmpSrc.intersect(bitmapBounds)) {
1579 return; // nothing to draw
reed@google.com33535f32012-09-25 15:37:50 +00001580 }
reed@google.com33535f32012-09-25 15:37:50 +00001581 }
reed@google.com33535f32012-09-25 15:37:50 +00001582 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +00001583
robertphillips@google.combac6b052012-09-28 18:06:49 +00001584 this->drawBitmapCommon(draw, bitmap, &tmpSrc, matrix, paint);
reed@google.com33535f32012-09-25 15:37:50 +00001585}
1586
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001587void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
reed@google.comac10a2d2010-12-22 21:39:39 +00001588 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001589 // clear of the source device must occur before CHECK_SHOULD_DRAW
1590 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1591 if (dev->fNeedClear) {
1592 // TODO: could check here whether we really need to draw at all
1593 dev->clear(0x0);
1594 }
1595
reed@google.comac10a2d2010-12-22 21:39:39 +00001596 CHECK_SHOULD_DRAW(draw);
1597
bsalomon@google.com5782d712011-01-21 21:03:59 +00001598 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001599 SkAutoCachedTexture colorLutTexture;
bsalomon@google.com88becf42012-10-05 14:54:42 +00001600 grPaint.colorSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001601 if (!dev->bindDeviceAsTexture(&grPaint) ||
twiz@google.com58071162012-07-18 21:41:50 +00001602 !skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001603 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001604 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001605
bsalomon@google.com88becf42012-10-05 14:54:42 +00001606 GrTexture* devTex = grPaint.getColorSampler(kBitmapTextureIdx).getCustomStage()->texture(0);
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001607 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001608
reed@google.com8926b162012-03-23 15:36:36 +00001609 SkImageFilter* filter = paint.getImageFilter();
1610 if (NULL != filter) {
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001611 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001612 SkIntToScalar(devTex->height()));
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001613 GrTexture* filteredTexture = filter_texture(this, fContext, devTex, filter, rect);
reed@google.com8926b162012-03-23 15:36:36 +00001614 if (filteredTexture) {
bsalomon@google.com88becf42012-10-05 14:54:42 +00001615 grPaint.colorSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001616 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001617 devTex = filteredTexture;
1618 filteredTexture->unref();
1619 }
1620 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001621
bsalomon@google.com5782d712011-01-21 21:03:59 +00001622 const SkBitmap& bm = dev->accessBitmap(false);
1623 int w = bm.width();
1624 int h = bm.height();
1625
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001626 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001627 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1628 GrIntToScalar(y),
1629 GrIntToScalar(w),
1630 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001631
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001632 // The device being drawn may not fill up its texture (saveLayer uses
1633 // the approximate ).
1634 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1635 GR_Scalar1 * h / devTex->height());
1636
1637 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001638}
1639
reed@google.com8926b162012-03-23 15:36:36 +00001640bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001641 if (!filter->asNewCustomStage(NULL, NULL) &&
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001642 !filter->canFilterImageGPU()) {
reed@google.com76dd2772012-01-05 21:15:07 +00001643 return false;
1644 }
reed@google.com8926b162012-03-23 15:36:36 +00001645 return true;
1646}
1647
1648bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1649 const SkMatrix& ctm,
1650 SkBitmap* result, SkIPoint* offset) {
1651 // want explicitly our impl, so guard against a subclass of us overriding it
1652 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001653 return false;
1654 }
reed@google.com8926b162012-03-23 15:36:36 +00001655
1656 SkAutoLockPixels alp(src, !src.getTexture());
1657 if (!src.getTexture() && !src.readyToDraw()) {
1658 return false;
1659 }
1660
1661 GrPaint paint;
reed@google.com8926b162012-03-23 15:36:36 +00001662
reed@google.com8926b162012-03-23 15:36:36 +00001663 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001664 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1665 // must be pushed upstack.
1666 SkAutoCachedTexture act(this, src, NULL, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001667
1668 result->setConfig(src.config(), src.width(), src.height());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001669 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001670 SkIntToScalar(src.height()));
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001671 GrTexture* resultTexture = filter_texture(this, fContext, texture, filter, rect);
reed@google.com8926b162012-03-23 15:36:36 +00001672 if (resultTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001673 result->setPixelRef(SkNEW_ARGS(SkGrTexturePixelRef,
1674 (resultTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001675 resultTexture->unref();
1676 }
reed@google.com76dd2772012-01-05 21:15:07 +00001677 return true;
1678}
1679
reed@google.comac10a2d2010-12-22 21:39:39 +00001680///////////////////////////////////////////////////////////////////////////////
1681
1682// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001683static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001684 kTriangles_GrPrimitiveType,
1685 kTriangleStrip_GrPrimitiveType,
1686 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001687};
1688
1689void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1690 int vertexCount, const SkPoint vertices[],
1691 const SkPoint texs[], const SkColor colors[],
1692 SkXfermode* xmode,
1693 const uint16_t indices[], int indexCount,
1694 const SkPaint& paint) {
1695 CHECK_SHOULD_DRAW(draw);
1696
bsalomon@google.com5782d712011-01-21 21:03:59 +00001697 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +00001698 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com5782d712011-01-21 21:03:59 +00001699 // we ignore the shader if texs is null.
1700 if (NULL == texs) {
twiz@google.com58071162012-07-18 21:41:50 +00001701 if (!skPaint2GrPaintNoShader(this,
1702 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001703 false,
1704 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001705 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +00001706 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001707 return;
1708 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001709 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001710 if (!skPaint2GrPaintShader(this,
1711 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001712 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001713 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001714 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001715 return;
1716 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001717 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001718
1719 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001720 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001721 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1722#if 0
1723 return
1724#endif
1725 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001726 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001727
bsalomon@google.com498776a2011-08-16 19:20:44 +00001728 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1729 if (NULL != colors) {
1730 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001731 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001732 for (int i = 0; i < vertexCount; ++i) {
rileya@google.com24f3ad12012-07-18 21:47:40 +00001733 convertedColors[i] = SkColor2GrColor(colors[i]);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001734 }
1735 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001736 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001737 fContext->drawVertices(grPaint,
1738 gVertexMode2PrimitiveType[vmode],
1739 vertexCount,
1740 (GrPoint*) vertices,
1741 (GrPoint*) texs,
1742 colors,
1743 indices,
1744 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001745}
1746
1747///////////////////////////////////////////////////////////////////////////////
1748
1749static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001750 GrFontScaler* scaler = (GrFontScaler*)data;
1751 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001752}
1753
1754static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1755 void* auxData;
1756 GrFontScaler* scaler = NULL;
1757 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1758 scaler = (GrFontScaler*)auxData;
1759 }
1760 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001761 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001762 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1763 }
1764 return scaler;
1765}
1766
1767static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1768 SkFixed fx, SkFixed fy,
1769 const SkGlyph& glyph) {
1770 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1771
bungeman@google.com15865a72012-01-11 16:28:04 +00001772 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001773
1774 if (NULL == procs->fFontScaler) {
1775 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1776 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001777
bungeman@google.com15865a72012-01-11 16:28:04 +00001778 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1779 glyph.getSubXFixed(),
1780 glyph.getSubYFixed()),
1781 SkFixedFloorToFixed(fx),
1782 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001783 procs->fFontScaler);
1784}
1785
bsalomon@google.com5782d712011-01-21 21:03:59 +00001786SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001787
1788 // deferred allocation
1789 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001790 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001791 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1792 fDrawProcs->fContext = fContext;
1793 }
1794
1795 // init our (and GL's) state
1796 fDrawProcs->fTextContext = context;
1797 fDrawProcs->fFontScaler = NULL;
1798 return fDrawProcs;
1799}
1800
1801void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1802 size_t byteLength, SkScalar x, SkScalar y,
1803 const SkPaint& paint) {
1804 CHECK_SHOULD_DRAW(draw);
1805
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001806 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001807 // this guy will just call our drawPath()
1808 draw.drawText((const char*)text, byteLength, x, y, paint);
1809 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001810 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001811
1812 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +00001813 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001814 if (!skPaint2GrPaintShader(this,
1815 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001816 true,
twiz@google.com58071162012-07-18 21:41:50 +00001817 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001818 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001819 return;
1820 }
bsalomon@google.com0e354aa2012-10-08 20:44:25 +00001821 GrTextContext context(fContext, grPaint);
tomhudson@google.com375ff852012-06-29 18:37:57 +00001822 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001823 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1824 }
1825}
1826
1827void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1828 size_t byteLength, const SkScalar pos[],
1829 SkScalar constY, int scalarsPerPos,
1830 const SkPaint& paint) {
1831 CHECK_SHOULD_DRAW(draw);
1832
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001833 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001834 // this guy will just call our drawPath()
1835 draw.drawPosText((const char*)text, byteLength, pos, constY,
1836 scalarsPerPos, paint);
1837 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001838 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001839
1840 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +00001841 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001842 if (!skPaint2GrPaintShader(this,
1843 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001844 true,
twiz@google.com58071162012-07-18 21:41:50 +00001845 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001846 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001847 return;
1848 }
bsalomon@google.com0e354aa2012-10-08 20:44:25 +00001849 GrTextContext context(fContext, grPaint);
tomhudson@google.com375ff852012-06-29 18:37:57 +00001850 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001851 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1852 scalarsPerPos, paint);
1853 }
1854}
1855
1856void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1857 size_t len, const SkPath& path,
1858 const SkMatrix* m, const SkPaint& paint) {
1859 CHECK_SHOULD_DRAW(draw);
1860
1861 SkASSERT(draw.fDevice == this);
1862 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1863}
1864
1865///////////////////////////////////////////////////////////////////////////////
1866
reed@google.comf67e4cf2011-03-15 20:56:58 +00001867bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1868 if (!paint.isLCDRenderText()) {
1869 // we're cool with the paint as is
1870 return false;
1871 }
1872
1873 if (paint.getShader() ||
1874 paint.getXfermode() || // unless its srcover
1875 paint.getMaskFilter() ||
1876 paint.getRasterizer() ||
1877 paint.getColorFilter() ||
1878 paint.getPathEffect() ||
1879 paint.isFakeBoldText() ||
1880 paint.getStyle() != SkPaint::kFill_Style) {
1881 // turn off lcd
1882 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1883 flags->fHinting = paint.getHinting();
1884 return true;
1885 }
1886 // we're cool with the paint as is
1887 return false;
1888}
1889
reed@google.com75d939b2011-12-07 15:07:23 +00001890void SkGpuDevice::flush() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +00001891 DO_DEFERRED_CLEAR();
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001892 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001893}
1894
reed@google.comf67e4cf2011-03-15 20:56:58 +00001895///////////////////////////////////////////////////////////////////////////////
1896
bsalomon@google.comfb309512011-11-30 14:13:48 +00001897bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001898 const GrTextureParams& params) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001899 uint64_t key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001900 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001901
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001902 GrTextureDesc desc;
1903 desc.fWidth = bitmap.width();
1904 desc.fHeight = bitmap.height();
rileya@google.com24f3ad12012-07-18 21:47:40 +00001905 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config());
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001906
robertphillips@google.com9c2ea842012-08-13 17:47:59 +00001907 GrCacheData cacheData(key);
1908
1909 return this->context()->isTextureInCache(desc, cacheData, &params);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001910}
1911
1912
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001913SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1914 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001915 bool isOpaque,
1916 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001917 GrTextureDesc desc;
1918 desc.fConfig = fRenderTarget->config();
1919 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1920 desc.fWidth = width;
1921 desc.fHeight = height;
1922 desc.fSampleCnt = fRenderTarget->numSamples();
1923
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001924 GrTexture* texture;
1925 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001926 // Skia's convention is to only clear a device if it is non-opaque.
1927 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001928
1929#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1930 // layers are never draw in repeat modes, so we can request an approx
1931 // match and ignore any padding.
1932 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1933 GrContext::kApprox_ScratchTexMatch :
1934 GrContext::kExact_ScratchTexMatch;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001935 texture = fContext->lockScratchTexture(desc, matchType);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001936#else
1937 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1938 texture = tunref.get();
1939#endif
1940 if (texture) {
1941 return SkNEW_ARGS(SkGpuDevice,(fContext,
1942 texture,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001943 needClear));
1944 } else {
1945 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1946 width, height);
1947 return NULL;
1948 }
1949}
1950
1951SkGpuDevice::SkGpuDevice(GrContext* context,
1952 GrTexture* texture,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001953 bool needClear)
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001954 : SkDevice(make_bitmap(context, texture->asRenderTarget())) {
1955
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001956 GrAssert(texture && texture->asRenderTarget());
bsalomon@google.com8090e652012-08-28 15:07:11 +00001957 // This constructor is called from onCreateCompatibleDevice. It has locked the RT in the texture
1958 // cache. We pass true for the third argument so that it will get unlocked.
1959 this->initFromRenderTarget(context, texture->asRenderTarget(), true);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001960 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001961}