blob: cedb7d4e8a7e495dd16693e19487e8c8cc11dae7 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
tomhudson@google.com898e7b52012-06-01 20:42:15 +00008#include "SkGpuDevice.h"
reed@google.comac10a2d2010-12-22 21:39:39 +00009
tomhudson@google.com2f68e762012-07-17 18:43:21 +000010#include "effects/GrTextureDomainEffect.h"
bsalomon@google.com68b58c92013-01-17 16:50:08 +000011#include "effects/GrSimpleTextureEffect.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"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000023#include "SkPathEffect.h"
24#include "SkStroke.h"
reed@google.comc9aa5872011-04-05 21:05:37 +000025#include "SkUtils.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000026
bsalomon@google.com06cd7322012-03-30 18:45:35 +000027#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
reed@google.comac10a2d2010-12-22 21:39:39 +000028
29#if 0
30 extern bool (*gShouldDrawProc)();
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +000031 #define CHECK_SHOULD_DRAW(draw, forceI) \
reed@google.comac10a2d2010-12-22 21:39:39 +000032 do { \
33 if (gShouldDrawProc && !gShouldDrawProc()) return; \
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +000034 this->prepareDraw(draw, forceI); \
reed@google.comac10a2d2010-12-22 21:39:39 +000035 } while (0)
36#else
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +000037 #define CHECK_SHOULD_DRAW(draw, forceI) this->prepareDraw(draw, forceI)
reed@google.comac10a2d2010-12-22 21:39:39 +000038#endif
39
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000040// we use the same texture slot on GrPaint for bitmaps and shaders
41// (since drawBitmap, drawSprite, and drawDevice ignore skia's shader)
42enum {
43 kBitmapTextureIdx = 0,
twiz@google.com58071162012-07-18 21:41:50 +000044 kShaderTextureIdx = 0,
45 kColorFilterTextureIdx = 1
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000046};
47
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +000048#define MAX_BLUR_SIGMA 4.0f
49// FIXME: This value comes from from SkBlurMaskFilter.cpp.
50// Should probably be put in a common header someplace.
51#define MAX_BLUR_RADIUS SkIntToScalar(128)
52// This constant approximates the scaling done in the software path's
53// "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
54// IMHO, it actually should be 1: we blur "less" than we should do
55// according to the CSS and canvas specs, simply because Safari does the same.
56// Firefox used to do the same too, until 4.0 where they fixed it. So at some
57// point we should probably get rid of these scaling constants and rebaseline
58// all the blur tests.
59#define BLUR_SIGMA_SCALE 0.6f
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000060// This constant represents the screen alignment criterion in texels for
rmistry@google.comd6176b02012-08-23 18:14:13 +000061// requiring texture domain clamping to prevent color bleeding when drawing
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000062// a sub region of a larger source image.
63#define COLOR_BLEED_TOLERANCE SkFloatToScalar(0.001f)
bsalomon@google.com06cd7322012-03-30 18:45:35 +000064
junov@google.comdbfac8a2012-12-06 21:47:40 +000065#define DO_DEFERRED_CLEAR() \
66 do { \
67 if (fNeedClear) { \
68 this->clear(SK_ColorTRANSPARENT); \
69 } \
70 } while (false) \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000071
reed@google.comac10a2d2010-12-22 21:39:39 +000072///////////////////////////////////////////////////////////////////////////////
73
reed@google.comb0a34d82012-07-11 19:57:55 +000074#define CHECK_FOR_NODRAW_ANNOTATION(paint) \
75 do { if (paint.isNoDrawAnnotation()) { return; } } while (0)
76
77///////////////////////////////////////////////////////////////////////////////
78
79
bsalomon@google.com84405e02012-03-05 19:57:21 +000080class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
81public:
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000082 SkAutoCachedTexture()
83 : fDevice(NULL)
84 , fTexture(NULL) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000085 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000086
bsalomon@google.com84405e02012-03-05 19:57:21 +000087 SkAutoCachedTexture(SkGpuDevice* device,
88 const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +000089 const GrTextureParams* params,
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000090 GrTexture** texture)
91 : fDevice(NULL)
92 , fTexture(NULL) {
93 GrAssert(NULL != texture);
bsalomon@google.comb8670992012-07-25 21:27:09 +000094 *texture = this->set(device, bitmap, params);
reed@google.comac10a2d2010-12-22 21:39:39 +000095 }
reed@google.comac10a2d2010-12-22 21:39:39 +000096
bsalomon@google.com84405e02012-03-05 19:57:21 +000097 ~SkAutoCachedTexture() {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000098 if (NULL != fTexture) {
99 GrUnlockCachedBitmapTexture(fTexture);
bsalomon@google.com84405e02012-03-05 19:57:21 +0000100 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000101 }
bsalomon@google.com84405e02012-03-05 19:57:21 +0000102
103 GrTexture* set(SkGpuDevice* device,
104 const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000105 const GrTextureParams* params) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000106 if (NULL != fTexture) {
107 GrUnlockCachedBitmapTexture(fTexture);
108 fTexture = NULL;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000109 }
110 fDevice = device;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000111 GrTexture* result = (GrTexture*)bitmap.getTexture();
112 if (NULL == result) {
113 // Cannot return the native texture so look it up in our cache
114 fTexture = GrLockCachedBitmapTexture(device->context(), bitmap, params);
115 result = fTexture;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000116 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000117 return result;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000118 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000119
bsalomon@google.com84405e02012-03-05 19:57:21 +0000120private:
121 SkGpuDevice* fDevice;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000122 GrTexture* fTexture;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000123};
reed@google.comac10a2d2010-12-22 21:39:39 +0000124
125///////////////////////////////////////////////////////////////////////////////
126
reed@google.comac10a2d2010-12-22 21:39:39 +0000127struct GrSkDrawProcs : public SkDrawProcs {
128public:
129 GrContext* fContext;
130 GrTextContext* fTextContext;
131 GrFontScaler* fFontScaler; // cached in the skia glyphcache
132};
133
134///////////////////////////////////////////////////////////////////////////////
135
reed@google.comaf951c92011-06-16 19:10:39 +0000136static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
137 switch (config) {
138 case kAlpha_8_GrPixelConfig:
139 *isOpaque = false;
140 return SkBitmap::kA8_Config;
141 case kRGB_565_GrPixelConfig:
142 *isOpaque = true;
143 return SkBitmap::kRGB_565_Config;
144 case kRGBA_4444_GrPixelConfig:
145 *isOpaque = false;
146 return SkBitmap::kARGB_4444_Config;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000147 case kSkia8888_PM_GrPixelConfig:
148 // we don't currently have a way of knowing whether
149 // a 8888 is opaque based on the config.
150 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000151 return SkBitmap::kARGB_8888_Config;
152 default:
153 *isOpaque = false;
154 return SkBitmap::kNo_Config;
155 }
156}
reed@google.comac10a2d2010-12-22 21:39:39 +0000157
reed@google.comaf951c92011-06-16 19:10:39 +0000158static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000159 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000160
161 bool isOpaque;
162 SkBitmap bitmap;
163 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
164 renderTarget->width(), renderTarget->height());
165 bitmap.setIsOpaque(isOpaque);
166 return bitmap;
167}
168
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000169SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000170: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
bsalomon@google.com8090e652012-08-28 15:07:11 +0000171 this->initFromRenderTarget(context, texture->asRenderTarget(), false);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000172}
173
reed@google.comaf951c92011-06-16 19:10:39 +0000174SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000175: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.com8090e652012-08-28 15:07:11 +0000176 this->initFromRenderTarget(context, renderTarget, false);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000177}
178
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000179void SkGpuDevice::initFromRenderTarget(GrContext* context,
bsalomon@google.com8090e652012-08-28 15:07:11 +0000180 GrRenderTarget* renderTarget,
181 bool cached) {
reed@google.comaf951c92011-06-16 19:10:39 +0000182 fDrawProcs = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000183
reed@google.comaf951c92011-06-16 19:10:39 +0000184 fContext = context;
185 fContext->ref();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000186
reed@google.comaf951c92011-06-16 19:10:39 +0000187 fRenderTarget = NULL;
188 fNeedClear = false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000189
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000190 GrAssert(NULL != renderTarget);
191 fRenderTarget = renderTarget;
192 fRenderTarget->ref();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000193
bsalomon@google.coma2921122012-08-28 12:34:17 +0000194 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
195 // on the RT but not vice-versa.
196 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
197 // busting chrome (for a currently unknown reason).
198 GrSurface* surface = fRenderTarget->asTexture();
199 if (NULL == surface) {
200 surface = fRenderTarget;
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000201 }
bsalomon@google.com8090e652012-08-28 15:07:11 +0000202 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (surface, cached));
bsalomon@google.coma2921122012-08-28 12:34:17 +0000203
reed@google.comaf951c92011-06-16 19:10:39 +0000204 this->setPixelRef(pr, 0)->unref();
205}
206
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000207SkGpuDevice::SkGpuDevice(GrContext* context,
208 SkBitmap::Config config,
209 int width,
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000210 int height,
211 int sampleCount)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000212 : SkDevice(config, width, height, false /*isOpaque*/) {
213
reed@google.comac10a2d2010-12-22 21:39:39 +0000214 fDrawProcs = NULL;
215
reed@google.com7b201d22011-01-11 18:59:23 +0000216 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000217 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000218
reed@google.comac10a2d2010-12-22 21:39:39 +0000219 fRenderTarget = NULL;
220 fNeedClear = false;
221
reed@google.comaf951c92011-06-16 19:10:39 +0000222 if (config != SkBitmap::kRGB_565_Config) {
223 config = SkBitmap::kARGB_8888_Config;
224 }
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;
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000230 desc.fConfig = SkBitmapConfig2GrPixelConfig(config);
231 desc.fSampleCnt = sampleCount;
reed@google.comac10a2d2010-12-22 21:39:39 +0000232
bsalomon@google.coma2921122012-08-28 12:34:17 +0000233 SkAutoTUnref<GrTexture> texture(fContext->createUncachedTexture(desc, NULL, 0));
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000234
bsalomon@google.coma2921122012-08-28 12:34:17 +0000235 if (NULL != texture) {
236 fRenderTarget = texture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000237 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000238
reed@google.comaf951c92011-06-16 19:10:39 +0000239 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000240
reed@google.comaf951c92011-06-16 19:10:39 +0000241 // wrap the bitmap with a pixelref to expose our texture
bsalomon@google.coma2921122012-08-28 12:34:17 +0000242 SkGrPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (texture));
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000243 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000244 } else {
245 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
246 width, height);
247 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000248 }
249}
250
251SkGpuDevice::~SkGpuDevice() {
252 if (fDrawProcs) {
253 delete fDrawProcs;
254 }
255
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000256 // The GrContext takes a ref on the target. We don't want to cause the render
257 // target to be unnecessarily kept alive.
258 if (fContext->getRenderTarget() == fRenderTarget) {
259 fContext->setRenderTarget(NULL);
260 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000261
robertphillips@google.com055f9082012-10-24 13:24:11 +0000262 if (fContext->getClip() == &fClipData) {
263 fContext->setClip(NULL);
264 }
265
bsalomon@google.coma2921122012-08-28 12:34:17 +0000266 SkSafeUnref(fRenderTarget);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000267 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000268}
269
reed@google.comac10a2d2010-12-22 21:39:39 +0000270///////////////////////////////////////////////////////////////////////////////
271
272void SkGpuDevice::makeRenderTargetCurrent() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000273 DO_DEFERRED_CLEAR();
reed@google.comac10a2d2010-12-22 21:39:39 +0000274 fContext->setRenderTarget(fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000275}
276
277///////////////////////////////////////////////////////////////////////////////
278
bsalomon@google.comc4364992011-11-07 15:54:49 +0000279namespace {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000280GrPixelConfig config8888_to_grconfig_and_flags(SkCanvas::Config8888 config8888, uint32_t* flags) {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000281 switch (config8888) {
282 case SkCanvas::kNative_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000283 *flags = 0;
284 return kSkia8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000285 case SkCanvas::kNative_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000286 *flags = GrContext::kUnpremul_PixelOpsFlag;
287 return kSkia8888_PM_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000288 case SkCanvas::kBGRA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000289 *flags = 0;
290 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000291 case SkCanvas::kBGRA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000292 *flags = GrContext::kUnpremul_PixelOpsFlag;
293 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000294 case SkCanvas::kRGBA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000295 *flags = 0;
296 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000297 case SkCanvas::kRGBA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000298 *flags = GrContext::kUnpremul_PixelOpsFlag;
299 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000300 default:
301 GrCrash("Unexpected Config8888.");
bsalomon@google.comccaa0022012-09-25 19:55:07 +0000302 *flags = 0; // suppress warning
bsalomon@google.comc4364992011-11-07 15:54:49 +0000303 return kSkia8888_PM_GrPixelConfig;
304 }
305}
306}
307
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000308bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
309 int x, int y,
310 SkCanvas::Config8888 config8888) {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000311 DO_DEFERRED_CLEAR();
bsalomon@google.com910267d2011-11-02 20:06:25 +0000312 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
313 SkASSERT(!bitmap.isNull());
314 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000315
bsalomon@google.com910267d2011-11-02 20:06:25 +0000316 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000317 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000318 uint32_t flags;
319 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000320 return fContext->readRenderTargetPixels(fRenderTarget,
321 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000322 bitmap.width(),
323 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000324 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000325 bitmap.getPixels(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000326 bitmap.rowBytes(),
327 flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000328}
329
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000330void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
331 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000332 SkAutoLockPixels alp(bitmap);
333 if (!bitmap.readyToDraw()) {
334 return;
335 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000336
337 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000338 uint32_t flags;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000339 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000340 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000341 } else {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000342 flags = 0;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000343 config= SkBitmapConfig2GrPixelConfig(bitmap.config());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000344 }
345
bsalomon@google.com6f379512011-11-16 20:36:03 +0000346 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000347 config, bitmap.getPixels(), bitmap.rowBytes(), flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000348}
349
robertphillips@google.com46f93502012-08-07 15:38:08 +0000350namespace {
humper@google.com05af1af2013-01-07 16:47:43 +0000351void purgeClipCB(int genID, void* ) {
robertphillips@google.com46f93502012-08-07 15:38:08 +0000352
353 if (SkClipStack::kInvalidGenID == genID ||
354 SkClipStack::kEmptyGenID == genID ||
355 SkClipStack::kWideOpenGenID == genID) {
356 // none of these cases will have a cached clip mask
357 return;
358 }
359
360}
361};
362
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000363void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
364 INHERITED::onAttachToCanvas(canvas);
365
366 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000367 fClipData.fClipStack = canvas->getClipStack();
robertphillips@google.com46f93502012-08-07 15:38:08 +0000368
369 fClipData.fClipStack->addPurgeClipCallback(purgeClipCB, fContext);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000370}
371
372void SkGpuDevice::onDetachFromCanvas() {
373 INHERITED::onDetachFromCanvas();
374
robertphillips@google.com46f93502012-08-07 15:38:08 +0000375 // TODO: iterate through the clip stack and clean up any cached clip masks
376 fClipData.fClipStack->removePurgeClipCallback(purgeClipCB, fContext);
377
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000378 fClipData.fClipStack = NULL;
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000379}
380
robertphillips@google.com607fe072012-07-24 13:54:00 +0000381#ifdef SK_DEBUG
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000382static void check_bounds(const GrClipData& clipData,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000383 const SkRegion& clipRegion,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000384 int renderTargetWidth,
385 int renderTargetHeight) {
386
robertphillips@google.com7b112892012-07-31 15:18:21 +0000387 SkIRect devBound;
388
389 devBound.setLTRB(0, 0, renderTargetWidth, renderTargetHeight);
390
robertphillips@google.com607fe072012-07-24 13:54:00 +0000391 SkClipStack::BoundsType boundType;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000392 SkRect canvTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000393
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000394 clipData.fClipStack->getBounds(&canvTemp, &boundType);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000395 if (SkClipStack::kNormal_BoundsType == boundType) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000396 SkIRect devTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000397
robertphillips@google.com7b112892012-07-31 15:18:21 +0000398 canvTemp.roundOut(&devTemp);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000399
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000400 devTemp.offset(-clipData.fOrigin.fX, -clipData.fOrigin.fY);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000401
robertphillips@google.com7b112892012-07-31 15:18:21 +0000402 if (!devBound.intersect(devTemp)) {
403 devBound.setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000404 }
405 }
406
robertphillips@google.com768fee82012-08-02 12:42:43 +0000407 GrAssert(devBound.contains(clipRegion.getBounds()));
robertphillips@google.com607fe072012-07-24 13:54:00 +0000408}
409#endif
410
reed@google.comac10a2d2010-12-22 21:39:39 +0000411///////////////////////////////////////////////////////////////////////////////
412
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000413// call this every draw call, to ensure that the context reflects our state,
reed@google.comac10a2d2010-12-22 21:39:39 +0000414// and not the state from some other canvas/device
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000415void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000416 GrAssert(NULL != fClipData.fClipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000417
reed@google.comac10a2d2010-12-22 21:39:39 +0000418 fContext->setRenderTarget(fRenderTarget);
419
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000420 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000421
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000422 if (forceIdentity) {
423 fContext->setIdentityMatrix();
424 } else {
425 fContext->setMatrix(*draw.fMatrix);
426 }
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000427 fClipData.fOrigin = this->getOrigin();
reed@google.comac10a2d2010-12-22 21:39:39 +0000428
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000429#ifdef SK_DEBUG
430 check_bounds(fClipData, *draw.fClip, fRenderTarget->width(), fRenderTarget->height());
431#endif
432
433 fContext->setClip(&fClipData);
434
435 DO_DEFERRED_CLEAR();
reed@google.comac10a2d2010-12-22 21:39:39 +0000436}
437
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000438SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000439 DO_DEFERRED_CLEAR();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000440 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000441}
442
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000443bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
bsalomon@google.coma2921122012-08-28 12:34:17 +0000444 GrTexture* texture = fRenderTarget->asTexture();
445 if (NULL != texture) {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000446 paint->colorStage(kBitmapTextureIdx)->setEffect(
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000447 GrSimpleTextureEffect::Create(texture, SkMatrix::I()))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000448 return true;
449 }
450 return false;
451}
452
453///////////////////////////////////////////////////////////////////////////////
454
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000455SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
456SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
457SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
458SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
459SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
460 shader_type_mismatch);
rileya@google.com3e332582012-07-03 13:43:35 +0000461SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
462 shader_type_mismatch);
rileya@google.com22e57f92012-07-19 15:16:19 +0000463SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
464SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000465
bsalomon@google.com84405e02012-03-05 19:57:21 +0000466namespace {
467
468// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
469// justAlpha indicates that skPaint's alpha should be used rather than the color
470// Callers may subsequently modify the GrPaint. Setting constantColor indicates
471// that the final paint will draw the same color at every pixel. This allows
472// an optimization where the the color filter can be applied to the skPaint's
twiz@google.com58071162012-07-18 21:41:50 +0000473// color once while converting to GrPaint and then ignored.
474inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
475 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000476 bool justAlpha,
477 bool constantColor,
478 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000479
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000480 grPaint->setDither(skPaint.isDither());
481 grPaint->setAntiAlias(skPaint.isAntiAlias());
bsalomon@google.com5782d712011-01-21 21:03:59 +0000482
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000483 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
484 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000485
486 SkXfermode* mode = skPaint.getXfermode();
487 if (mode) {
488 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000489 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000490#if 0
491 return false;
492#endif
493 }
494 }
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000495 grPaint->setBlendFunc(sk_blend_to_grblend(sm), sk_blend_to_grblend(dm));
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000496
bsalomon@google.com5782d712011-01-21 21:03:59 +0000497 if (justAlpha) {
498 uint8_t alpha = skPaint.getAlpha();
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000499 grPaint->setColor(GrColorPackRGBA(alpha, alpha, alpha, alpha));
Scroggod757df22011-05-16 13:11:16 +0000500 // justAlpha is currently set to true only if there is a texture,
501 // so constantColor should not also be true.
502 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000503 } else {
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000504 grPaint->setColor(SkColor2GrColor(skPaint.getColor()));
bsalomon@google.com88becf42012-10-05 14:54:42 +0000505 GrAssert(!grPaint->isColorStageEnabled(kShaderTextureIdx));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000506 }
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000507
Scroggo97c88c22011-05-11 14:05:25 +0000508 SkColorFilter* colorFilter = skPaint.getColorFilter();
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000509 if (NULL != colorFilter) {
510 // if the source color is a constant then apply the filter here once rather than per pixel
511 // in a shader.
512 if (constantColor) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000513 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000514 grPaint->setColor(SkColor2GrColor(filtered));
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000515 } else {
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000516 SkAutoTUnref<GrEffectRef> effect(colorFilter->asNewEffect(dev->context()));
bsalomon@google.com021fc732012-10-25 12:47:42 +0000517 if (NULL != effect.get()) {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000518 grPaint->colorStage(kColorFilterTextureIdx)->setEffect(effect);
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000519 } else {
bsalomon@google.com8ea78d82012-10-24 20:11:30 +0000520 // TODO: rewrite this using asNewEffect()
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000521 SkColor color;
522 SkXfermode::Mode filterMode;
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000523 if (colorFilter->asColorMode(&color, &filterMode)) {
524 grPaint->setXfermodeColorFilter(filterMode, SkColor2GrColor(color));
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000525 }
526 }
Scroggod757df22011-05-16 13:11:16 +0000527 }
Scroggo97c88c22011-05-11 14:05:25 +0000528 }
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000529
bsalomon@google.com5782d712011-01-21 21:03:59 +0000530 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000531}
532
bsalomon@google.com84405e02012-03-05 19:57:21 +0000533// This function is similar to skPaint2GrPaintNoShader but also converts
bsalomon@google.com08283af2012-10-26 13:01:20 +0000534// skPaint's shader to a GrTexture/GrEffectStage if possible. The texture to
bsalomon@google.com84405e02012-03-05 19:57:21 +0000535// be used is set on grPaint and returned in param act. constantColor has the
536// same meaning as in skPaint2GrPaintNoShader.
537inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
538 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000539 bool constantColor,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000540 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000541 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000542 if (NULL == shader) {
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000543 return skPaint2GrPaintNoShader(dev, skPaint, false, constantColor, grPaint);
544 } else if (!skPaint2GrPaintNoShader(dev, skPaint, true, false, grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000545 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000546 }
547
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000548 SkAutoTUnref<GrEffectRef> effect(shader->asNewEffect(dev->context(), skPaint));
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000549 if (NULL != effect.get()) {
550 grPaint->colorStage(kShaderTextureIdx)->setEffect(effect);
rileya@google.com91f319c2012-07-25 17:18:31 +0000551 return true;
552 }
553
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000554 // We still don't have SkColorShader::asNewEffect() implemented.
555 SkShader::GradientInfo info;
556 SkColor color;
reed@google.comac10a2d2010-12-22 21:39:39 +0000557
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000558 info.fColors = &color;
559 info.fColorOffsets = NULL;
560 info.fColorCount = 1;
561 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
562 SkPaint copy(skPaint);
563 copy.setShader(NULL);
564 // modulate the paint alpha by the shader's solid color alpha
565 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
566 copy.setColor(SkColorSetA(color, newA));
567 return skPaint2GrPaintNoShader(dev, copy, false, constantColor, grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000568 }
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000569 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000570}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000571}
reed@google.comac10a2d2010-12-22 21:39:39 +0000572
573///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000574void SkGpuDevice::clear(SkColor color) {
robertphillips@google.comea5d8af2012-11-02 17:38:28 +0000575 fContext->clear(NULL, SkColor2GrColor(color), fRenderTarget);
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000576 fNeedClear = false;
bsalomon@google.com398109c2011-04-14 18:40:27 +0000577}
578
reed@google.comac10a2d2010-12-22 21:39:39 +0000579void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000580 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000581
bsalomon@google.com5782d712011-01-21 21:03:59 +0000582 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000583 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000584 return;
585 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000586
587 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000588}
589
590// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000591static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000592 kPoints_GrPrimitiveType,
593 kLines_GrPrimitiveType,
594 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000595};
596
597void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000598 size_t count, const SkPoint pts[], const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000599 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000600
601 SkScalar width = paint.getStrokeWidth();
602 if (width < 0) {
603 return;
604 }
605
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000606 // we only handle hairlines and paints without path effects or mask filters,
607 // else we let the SkDraw call our drawPath()
608 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000609 draw.drawPoints(mode, count, pts, paint, true);
610 return;
611 }
612
bsalomon@google.com5782d712011-01-21 21:03:59 +0000613 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000614 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000615 return;
616 }
617
bsalomon@google.com5782d712011-01-21 21:03:59 +0000618 fContext->drawVertices(grPaint,
619 gPointMode2PrimtiveType[mode],
620 count,
621 (GrPoint*)pts,
622 NULL,
623 NULL,
624 NULL,
625 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000626}
627
reed@google.comc9aa5872011-04-05 21:05:37 +0000628///////////////////////////////////////////////////////////////////////////////
629
reed@google.comac10a2d2010-12-22 21:39:39 +0000630void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000631 const SkPaint& paint) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000632 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000633 CHECK_SHOULD_DRAW(draw, false);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000634
bungeman@google.com79bd8772011-07-18 15:34:08 +0000635 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000636 SkScalar width = paint.getStrokeWidth();
637
638 /*
639 We have special code for hairline strokes, miter-strokes, and fills.
640 Anything else we just call our path code.
641 */
642 bool usePath = doStroke && width > 0 &&
643 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000644 // another two reasons we might need to call drawPath...
645 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000646 usePath = true;
647 }
reed@google.com67db6642011-05-26 11:46:35 +0000648 // until we aa rotated rects...
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000649 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
reed@google.com67db6642011-05-26 11:46:35 +0000650 usePath = true;
651 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000652 // small miter limit means right angles show bevel...
653 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
654 paint.getStrokeMiter() < SK_ScalarSqrt2)
655 {
656 usePath = true;
657 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000658 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000659 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
660 usePath = true;
661 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000662
663 if (usePath) {
664 SkPath path;
665 path.addRect(rect);
666 this->drawPath(draw, path, paint, NULL, true);
667 return;
668 }
669
670 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000671 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000672 return;
673 }
reed@google.com20efde72011-05-09 17:00:02 +0000674 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000675}
676
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000677///////////////////////////////////////////////////////////////////////////////
678
679void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
680 const SkPaint& paint) {
681 CHECK_FOR_NODRAW_ANNOTATION(paint);
682 CHECK_SHOULD_DRAW(draw, false);
683
684 bool usePath = false;
685 // some basic reasons we might need to call drawPath...
686 if (paint.getMaskFilter() || paint.getPathEffect()) {
687 usePath = true;
688 }
689
690 if (usePath) {
691 SkPath path;
692 path.addOval(oval);
693 this->drawPath(draw, path, paint, NULL, true);
694 return;
695 }
696
697 GrPaint grPaint;
698 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
699 return;
700 }
701 SkStrokeRec stroke(paint);
702
703 fContext->drawOval(grPaint, oval, stroke);
704}
705
reed@google.com69302852011-02-16 18:08:07 +0000706#include "SkMaskFilter.h"
707#include "SkBounder.h"
708
bsalomon@google.com85003222012-03-28 14:44:37 +0000709///////////////////////////////////////////////////////////////////////////////
710
711// helpers for applying mask filters
712namespace {
713
bsalomon@google.com85003222012-03-28 14:44:37 +0000714// We prefer to blur small rect with small radius via CPU.
715#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
716#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
717inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
718 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
719 rect.height() <= MIN_GPU_BLUR_SIZE &&
720 radius <= MIN_GPU_BLUR_RADIUS) {
721 return true;
722 }
723 return false;
724}
725
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000726bool drawWithGPUMaskFilter(GrContext* context, const SkPath& devPath, const SkStrokeRec& stroke,
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000727 SkMaskFilter* filter, const SkRegion& clip,
sugoi@google.com12b4e272012-12-06 20:13:11 +0000728 SkBounder* bounder, GrPaint* grp) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000729 SkMaskFilter::BlurInfo info;
730 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000731 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000732 return false;
733 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000734 SkScalar radius = info.fIgnoreTransform ? info.fRadius
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000735 : context->getMatrix().mapRadius(info.fRadius);
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000736 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000737 if (radius <= 0) {
738 return false;
739 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000740
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000741 SkRect srcRect = devPath.getBounds();
bsalomon@google.com85003222012-03-28 14:44:37 +0000742 if (shouldDrawBlurWithCPU(srcRect, radius)) {
743 return false;
744 }
745
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000746 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000747 float sigma3 = sigma * 3.0f;
748
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000749 SkRect clipRect;
750 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000751
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000752 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000753 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
754 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000755 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000756 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000757 SkIRect finalIRect;
758 finalRect.roundOut(&finalIRect);
759 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000760 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000761 }
762 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000763 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000764 }
765 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000766 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000767 GrTextureDesc desc;
768 desc.fFlags = kRenderTarget_GrTextureFlagBit;
769 desc.fWidth = SkScalarCeilToInt(srcRect.width());
770 desc.fHeight = SkScalarCeilToInt(srcRect.height());
771 // We actually only need A8, but it often isn't supported as a
772 // render target so default to RGBA_8888
bsalomon@google.com0342a852012-08-20 19:22:38 +0000773 desc.fConfig = kRGBA_8888_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000774
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000775 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
776 desc.fConfig = kAlpha_8_GrPixelConfig;
777 }
778
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000779 GrAutoScratchTexture pathEntry(context, desc);
780 GrTexture* pathTexture = pathEntry.texture();
781 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000782 return false;
783 }
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000784
robertphillips@google.comccb39502012-10-01 18:25:13 +0000785 SkAutoTUnref<GrTexture> blurTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000786
robertphillips@google.comccb39502012-10-01 18:25:13 +0000787 {
788 GrContext::AutoRenderTarget art(context, pathTexture->asRenderTarget());
789 GrContext::AutoClip ac(context, srcRect);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000790
robertphillips@google.comccb39502012-10-01 18:25:13 +0000791 context->clear(NULL, 0);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000792
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000793 GrPaint tempPaint;
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000794 if (grp->isAntiAlias()) {
795 tempPaint.setAntiAlias(true);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000796 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
797 // blend coeff of zero requires dual source blending support in order
798 // to properly blend partially covered pixels. This means the AA
799 // code path may not be taken. So we use a dst blend coeff of ISA. We
800 // could special case AA draws to a dst surface with known alpha=0 to
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000801 // use a zero dst coeff when dual source blending isn't available.f
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000802 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000803 }
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000804
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000805 GrContext::AutoMatrix am;
806
807 // Draw hard shadow to pathTexture with path top-left at origin using tempPaint.
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000808 SkMatrix translate;
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000809 translate.setTranslate(offset.fX, offset.fY);
810 am.set(context, translate);
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000811 context->drawPath(tempPaint, devPath, stroke);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000812
813 // If we're doing a normal blur, we can clobber the pathTexture in the
814 // gaussianBlur. Otherwise, we need to save it for later compositing.
815 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
skia.committer@gmail.comdc3a4e52012-10-02 02:01:24 +0000816 blurTexture.reset(context->gaussianBlur(pathTexture, isNormalBlur,
robertphillips@google.comccb39502012-10-01 18:25:13 +0000817 srcRect, sigma, sigma));
robertphillips@google.com7d126752012-10-19 12:56:26 +0000818 if (NULL == blurTexture) {
819 return false;
820 }
robertphillips@google.comccb39502012-10-01 18:25:13 +0000821
822 if (!isNormalBlur) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000823 context->setIdentityMatrix();
robertphillips@google.comccb39502012-10-01 18:25:13 +0000824 GrPaint paint;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000825 SkMatrix matrix;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000826 matrix.setIDiv(pathTexture->width(), pathTexture->height());
robertphillips@google.comccb39502012-10-01 18:25:13 +0000827 // Blend pathTexture over blurTexture.
828 context->setRenderTarget(blurTexture->asRenderTarget());
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000829 paint.colorStage(0)->setEffect(
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000830 GrSimpleTextureEffect::Create(pathTexture, matrix))->unref();
robertphillips@google.comccb39502012-10-01 18:25:13 +0000831 if (SkMaskFilter::kInner_BlurType == blurType) {
832 // inner: dst = dst * src
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000833 paint.setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000834 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
835 // solid: dst = src + dst - src * dst
836 // = (1 - dst) * src + 1 * dst
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000837 paint.setBlendFunc(kIDC_GrBlendCoeff, kOne_GrBlendCoeff);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000838 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
839 // outer: dst = dst * (1 - src)
840 // = 0 * src + (1 - src) * dst
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000841 paint.setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000842 }
843 context->drawRect(paint, srcRect);
844 }
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000845 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000846
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000847 GrContext::AutoMatrix am;
848 if (!am.setIdentity(context, grp)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000849 return false;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000850 }
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +0000851
bsalomon@google.com88becf42012-10-05 14:54:42 +0000852 static const int MASK_IDX = GrPaint::kMaxCoverageStages - 1;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000853 // we assume the last mask index is available for use
bsalomon@google.com88becf42012-10-05 14:54:42 +0000854 GrAssert(!grp->isCoverageStageEnabled(MASK_IDX));
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000855
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000856 SkMatrix matrix;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000857 matrix.setTranslate(-finalRect.fLeft, -finalRect.fTop);
858 matrix.postIDiv(blurTexture->width(), blurTexture->height());
859
bsalomon@google.com08283af2012-10-26 13:01:20 +0000860 grp->coverageStage(MASK_IDX)->reset();
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000861 grp->coverageStage(MASK_IDX)->setEffect(
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000862 GrSimpleTextureEffect::Create(blurTexture, matrix))->unref();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000863 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000864 return true;
865}
866
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000867bool drawWithMaskFilter(GrContext* context, const SkPath& devPath,
868 SkMaskFilter* filter, const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000869 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000870 SkMask srcM, dstM;
871
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000872 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
873 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
reed@google.com69302852011-02-16 18:08:07 +0000874 return false;
875 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000876 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000877
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000878 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
reed@google.com69302852011-02-16 18:08:07 +0000879 return false;
880 }
881 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000882 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000883
884 if (clip.quickReject(dstM.fBounds)) {
885 return false;
886 }
887 if (bounder && !bounder->doIRect(dstM.fBounds)) {
888 return false;
889 }
890
891 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000892 // the current clip (and identity matrix) and GrPaint settings
893 GrContext::AutoMatrix am;
894 am.setIdentity(context, grp);
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000895
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000896 GrTextureDesc desc;
897 desc.fWidth = dstM.fBounds.width();
898 desc.fHeight = dstM.fBounds.height();
899 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +0000900
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000901 GrAutoScratchTexture ast(context, desc);
902 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000903
reed@google.com69302852011-02-16 18:08:07 +0000904 if (NULL == texture) {
905 return false;
906 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000907 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000908 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000909
bsalomon@google.com88becf42012-10-05 14:54:42 +0000910 static const int MASK_IDX = GrPaint::kMaxCoverageStages - 1;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000911 // we assume the last mask index is available for use
bsalomon@google.com88becf42012-10-05 14:54:42 +0000912 GrAssert(!grp->isCoverageStageEnabled(MASK_IDX));
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000913
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000914 SkMatrix m;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000915 m.setTranslate(-dstM.fBounds.fLeft*SK_Scalar1, -dstM.fBounds.fTop*SK_Scalar1);
916 m.postIDiv(texture->width(), texture->height());
917
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000918 grp->coverageStage(MASK_IDX)->setEffect(GrSimpleTextureEffect::Create(texture, m))->unref();
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000919 GrRect d;
bsalomon@google.com81712882012-11-01 17:12:34 +0000920 d.setLTRB(SkIntToScalar(dstM.fBounds.fLeft),
921 SkIntToScalar(dstM.fBounds.fTop),
922 SkIntToScalar(dstM.fBounds.fRight),
923 SkIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000924
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000925 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000926 return true;
927}
reed@google.com69302852011-02-16 18:08:07 +0000928
bsalomon@google.com85003222012-03-28 14:44:37 +0000929}
930
931///////////////////////////////////////////////////////////////////////////////
932
reed@google.com0c219b62011-02-16 21:31:18 +0000933void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000934 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000935 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000936 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000937 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000938
bsalomon@google.com5782d712011-01-21 21:03:59 +0000939 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000940 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000941 return;
942 }
943
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000944 // can we cheat, and treat a thin stroke as a hairline w/ coverage
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000945 // if we can, we draw lots faster (raster device does this same test)
946 SkScalar hairlineCoverage;
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000947 bool doHairLine = SkDrawTreatAsHairline(paint, fContext->getMatrix(), &hairlineCoverage);
948 if (doHairLine) {
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000949 grPaint.setCoverage(SkScalarRoundToInt(hairlineCoverage * grPaint.getCoverage()));
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000950 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000951
reed@google.comfe626382011-09-21 13:50:35 +0000952 // If we have a prematrix, apply it to the path, optimizing for the case
953 // where the original path can in fact be modified in place (even though
954 // its parameter type is const).
955 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000956 SkPath tmpPath, effectPath;
reed@google.comac10a2d2010-12-22 21:39:39 +0000957
958 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +0000959 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +0000960
reed@google.come3445642011-02-16 23:20:39 +0000961 if (!pathIsMutable) {
962 result = &tmpPath;
963 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000964 }
reed@google.come3445642011-02-16 23:20:39 +0000965 // should I push prePathMatrix on our MV stack temporarily, instead
966 // of applying it here? See SkDraw.cpp
967 pathPtr->transform(*prePathMatrix, result);
968 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +0000969 }
reed@google.com0c219b62011-02-16 21:31:18 +0000970 // at this point we're done with prePathMatrix
971 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +0000972
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000973 SkStrokeRec stroke(paint);
974 SkPathEffect* pathEffect = paint.getPathEffect();
975 if (pathEffect && pathEffect->filterPath(&effectPath, *pathPtr, &stroke)) {
976 pathPtr = &effectPath;
977 }
978
979 if (!pathEffect && doHairLine) {
980 stroke.setHairlineStyle();
reed@google.com0c219b62011-02-16 21:31:18 +0000981 }
982
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000983 if (paint.getMaskFilter()) {
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000984 if (!stroke.isHairlineStyle()) {
985 if (stroke.applyToPath(&tmpPath, *pathPtr)) {
986 pathPtr = &tmpPath;
987 stroke.setFillStyle();
988 }
989 }
990
reed@google.com0c219b62011-02-16 21:31:18 +0000991 // avoid possibly allocating a new path in transform if we can
992 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
993
994 // transform the path into device space
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000995 pathPtr->transform(fContext->getMatrix(), devPathPtr);
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000996 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, stroke, paint.getMaskFilter(),
sugoi@google.com12b4e272012-12-06 20:13:11 +0000997 *draw.fClip, draw.fBounder, &grPaint)) {
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000998 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
999 SkPaint::kFill_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001000 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001001 *draw.fClip, draw.fBounder, &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001002 }
reed@google.com69302852011-02-16 18:08:07 +00001003 return;
1004 }
reed@google.com69302852011-02-16 18:08:07 +00001005
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001006 fContext->drawPath(grPaint, *pathPtr, stroke);
reed@google.comac10a2d2010-12-22 21:39:39 +00001007}
1008
bsalomon@google.comfb309512011-11-30 14:13:48 +00001009namespace {
1010
1011inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1012 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1013 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1014 return tilesX * tilesY;
1015}
1016
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001017inline int determine_tile_size(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001018 const SkRect& src,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001019 int maxTextureSize) {
1020 static const int kSmallTileSize = 1 << 10;
1021 if (maxTextureSize <= kSmallTileSize) {
1022 return maxTextureSize;
1023 }
1024
1025 size_t maxTexTotalTileSize;
1026 size_t smallTotalTileSize;
1027
robertphillips@google.combac6b052012-09-28 18:06:49 +00001028 SkIRect iSrc;
1029 src.roundOut(&iSrc);
1030
1031 maxTexTotalTileSize = get_tile_count(iSrc.fLeft,
1032 iSrc.fTop,
1033 iSrc.fRight,
1034 iSrc.fBottom,
1035 maxTextureSize);
1036 smallTotalTileSize = get_tile_count(iSrc.fLeft,
1037 iSrc.fTop,
1038 iSrc.fRight,
1039 iSrc.fBottom,
1040 kSmallTileSize);
1041
bsalomon@google.comfb309512011-11-30 14:13:48 +00001042 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1043 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1044
1045 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1046 return kSmallTileSize;
1047 } else {
1048 return maxTextureSize;
1049 }
1050}
1051}
1052
1053bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001054 const GrTextureParams& params,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001055 const SkRect* srcRectPtr) const {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001056 // if bitmap is explictly texture backed then just use the texture
1057 if (NULL != bitmap.getTexture()) {
1058 return false;
1059 }
1060 // if it's larger than the max texture size, then we have no choice but
1061 // tiling
1062 const int maxTextureSize = fContext->getMaxTextureSize();
1063 if (bitmap.width() > maxTextureSize ||
1064 bitmap.height() > maxTextureSize) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001065 return true;
1066 }
1067 // if we are going to have to draw the whole thing, then don't tile
1068 if (NULL == srcRectPtr) {
1069 return false;
1070 }
1071 // if the entire texture is already in our cache then no reason to tile it
bsalomon@google.com0797c2c2012-12-20 15:13:01 +00001072 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001073 return false;
1074 }
1075
1076 // At this point we know we could do the draw by uploading the entire bitmap
1077 // as a texture. However, if the texture would be large compared to the
1078 // cache size and we don't require most of it for this draw then tile to
1079 // reduce the amount of upload and cache spill.
1080
1081 // assumption here is that sw bitmap size is a good proxy for its size as
1082 // a texture
1083 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001084 size_t cacheSize;
1085 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001086 if (bmpSize < cacheSize / 2) {
1087 return false;
1088 }
1089
robertphillips@google.combac6b052012-09-28 18:06:49 +00001090 SkScalar fracUsed = SkScalarMul(srcRectPtr->width() / bitmap.width(),
1091 srcRectPtr->height() / bitmap.height());
1092 if (fracUsed <= SK_ScalarHalf) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001093 return true;
1094 } else {
1095 return false;
1096 }
1097}
1098
reed@google.comac10a2d2010-12-22 21:39:39 +00001099void SkGpuDevice::drawBitmap(const SkDraw& draw,
1100 const SkBitmap& bitmap,
1101 const SkIRect* srcRectPtr,
1102 const SkMatrix& m,
1103 const SkPaint& paint) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001104
1105 SkRect tmp;
1106 SkRect* tmpPtr = NULL;
1107
1108 // convert from SkIRect to SkRect
1109 if (NULL != srcRectPtr) {
1110 tmp.set(*srcRectPtr);
1111 tmpPtr = &tmp;
1112 }
1113
1114 // We cannot call drawBitmapRect here since 'm' could be anything
1115 this->drawBitmapCommon(draw, bitmap, tmpPtr, m, paint);
1116}
1117
1118void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1119 const SkBitmap& bitmap,
1120 const SkRect* srcRectPtr,
1121 const SkMatrix& m,
1122 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001123 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001124
robertphillips@google.combac6b052012-09-28 18:06:49 +00001125 SkRect srcRect;
reed@google.comac10a2d2010-12-22 21:39:39 +00001126 if (NULL == srcRectPtr) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001127 srcRect.set(0, 0, SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001128 } else {
1129 srcRect = *srcRectPtr;
1130 }
1131
junov@google.comd935cfb2011-06-27 20:48:23 +00001132 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001133 // Convert the bitmap to a shader so that the rect can be drawn
1134 // through drawRect, which supports mask filters.
robertphillips@google.combac6b052012-09-28 18:06:49 +00001135 SkMatrix newM(m);
junov@google.com1d329782011-07-28 20:10:09 +00001136 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001137 const SkBitmap* bitmapPtr = &bitmap;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001138 if (NULL != srcRectPtr) {
1139 SkIRect iSrc;
1140 srcRect.roundOut(&iSrc);
1141 if (!bitmap.extractSubset(&tmp, iSrc)) {
epoger@google.com9ef2d832011-07-01 21:12:20 +00001142 return; // extraction failed
1143 }
1144 bitmapPtr = &tmp;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001145 srcRect.offset(SkIntToScalar(-iSrc.fLeft), SkIntToScalar(-iSrc.fTop));
1146 // The source rect has changed so update the matrix
1147 newM.preTranslate(SkIntToScalar(iSrc.fLeft), SkIntToScalar(iSrc.fTop));
junov@google.comd935cfb2011-06-27 20:48:23 +00001148 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001149
junov@google.comd935cfb2011-06-27 20:48:23 +00001150 SkPaint paintWithTexture(paint);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001151 paintWithTexture.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
junov@google.comd935cfb2011-06-27 20:48:23 +00001152 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001153
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001154 // Transform 'newM' needs to be concatenated to the current matrix,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001155 // rather than transforming the primitive directly, so that 'newM' will
junov@google.com1d329782011-07-28 20:10:09 +00001156 // also affect the behavior of the mask filter.
1157 SkMatrix drawMatrix;
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001158 drawMatrix.setConcat(fContext->getMatrix(), newM);
junov@google.com1d329782011-07-28 20:10:09 +00001159 SkDraw transformedDraw(draw);
1160 transformedDraw.fMatrix = &drawMatrix;
1161
robertphillips@google.combac6b052012-09-28 18:06:49 +00001162 this->drawRect(transformedDraw, srcRect, paintWithTexture);
junov@google.com1d329782011-07-28 20:10:09 +00001163
junov@google.comd935cfb2011-06-27 20:48:23 +00001164 return;
1165 }
1166
bsalomon@google.com5782d712011-01-21 21:03:59 +00001167 GrPaint grPaint;
skia.committer@gmail.com4e73aa12013-01-09 02:01:30 +00001168
humper@google.com9aaf36d2013-01-08 16:08:01 +00001169 bool alphaOnly = !(SkBitmap::kA8_Config == bitmap.config());
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001170 if (!skPaint2GrPaintNoShader(this, paint, alphaOnly, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001171 return;
1172 }
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001173 GrTextureParams params;
1174 params.setBilerp(paint.isFilterBitmap());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001175
robertphillips@google.combac6b052012-09-28 18:06:49 +00001176 if (!this->shouldTileBitmap(bitmap, params, srcRectPtr)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001177 // take the simple case
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001178 this->internalDrawBitmap(bitmap, srcRect, m, params, &grPaint);
robertphillips@google.combac6b052012-09-28 18:06:49 +00001179 } else {
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001180 this->drawTiledBitmap(bitmap, srcRect, m, params, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001181 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001182}
1183
1184// Break 'bitmap' into several tiles to draw it since it has already
1185// been determined to be too large to fit in VRAM
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001186void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001187 const SkRect& srcRect,
1188 const SkMatrix& m,
1189 const GrTextureParams& params,
1190 GrPaint* grPaint) {
1191 const int maxTextureSize = fContext->getMaxTextureSize();
1192
1193 int tileSize = determine_tile_size(bitmap, srcRect, maxTextureSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001194
reed@google.comac10a2d2010-12-22 21:39:39 +00001195 // compute clip bounds in local coordinates
robertphillips@google.combac6b052012-09-28 18:06:49 +00001196 SkRect clipRect;
reed@google.comac10a2d2010-12-22 21:39:39 +00001197 {
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001198 const GrRenderTarget* rt = fContext->getRenderTarget();
1199 clipRect.setWH(SkIntToScalar(rt->width()), SkIntToScalar(rt->height()));
1200 if (!fContext->getClip()->fClipStack->intersectRectWithClip(&clipRect)) {
1201 return;
1202 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001203 SkMatrix matrix, inverse;
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001204 matrix.setConcat(fContext->getMatrix(), m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001205 if (!matrix.invert(&inverse)) {
1206 return;
1207 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001208 inverse.mapRect(&clipRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001209 }
1210
bsalomon@google.comfb309512011-11-30 14:13:48 +00001211 int nx = bitmap.width() / tileSize;
1212 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001213 for (int x = 0; x <= nx; x++) {
1214 for (int y = 0; y <= ny; y++) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001215 SkRect tileR;
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001216 tileR.set(SkIntToScalar(x * tileSize),
robertphillips@google.combac6b052012-09-28 18:06:49 +00001217 SkIntToScalar(y * tileSize),
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001218 SkIntToScalar((x + 1) * tileSize),
robertphillips@google.combac6b052012-09-28 18:06:49 +00001219 SkIntToScalar((y + 1) * tileSize));
1220
1221 if (!SkRect::Intersects(tileR, clipRect)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001222 continue;
1223 }
1224
robertphillips@google.combac6b052012-09-28 18:06:49 +00001225 if (!tileR.intersect(srcRect)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001226 continue;
1227 }
1228
1229 SkBitmap tmpB;
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001230 SkIRect iTileR;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001231 tileR.roundOut(&iTileR);
1232 if (bitmap.extractSubset(&tmpB, iTileR)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001233 // now offset it to make it "local" to our tmp bitmap
robertphillips@google.combac6b052012-09-28 18:06:49 +00001234 tileR.offset(SkIntToScalar(-iTileR.fLeft), SkIntToScalar(-iTileR.fTop));
reed@google.comac10a2d2010-12-22 21:39:39 +00001235 SkMatrix tmpM(m);
skia.committer@gmail.comdc3a4e52012-10-02 02:01:24 +00001236 tmpM.preTranslate(SkIntToScalar(iTileR.fLeft),
robertphillips@google.comffad46b2012-10-01 14:32:51 +00001237 SkIntToScalar(iTileR.fTop));
1238
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001239 this->internalDrawBitmap(tmpB, tileR, tmpM, params, grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001240 }
1241 }
1242 }
1243}
1244
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001245namespace {
1246
1247bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1248 // detect pixel disalignment
1249 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1250 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
rmistry@google.comd6176b02012-08-23 18:14:13 +00001251 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001252 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1253 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1254 COLOR_BLEED_TOLERANCE &&
1255 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1256 COLOR_BLEED_TOLERANCE) {
1257 return true;
1258 }
1259 return false;
1260}
1261
1262bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1263 const SkMatrix& m) {
1264 // Only gets called if hasAlignedSamples returned false.
1265 // So we can assume that sampling is axis aligned but not texel aligned.
1266 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001267 SkRect innerSrcRect(srcRect), innerTransformedRect,
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001268 outerTransformedRect(transformedRect);
1269 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1270 m.mapRect(&innerTransformedRect, innerSrcRect);
1271
1272 // The gap between outerTransformedRect and innerTransformedRect
1273 // represents the projection of the source border area, which is
1274 // problematic for color bleeding. We must check whether any
1275 // destination pixels sample the border area.
1276 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1277 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1278 SkIRect outer, inner;
1279 outerTransformedRect.round(&outer);
1280 innerTransformedRect.round(&inner);
1281 // If the inner and outer rects round to the same result, it means the
1282 // border does not overlap any pixel centers. Yay!
1283 return inner != outer;
1284}
1285
1286} // unnamed namespace
1287
reed@google.comac10a2d2010-12-22 21:39:39 +00001288/*
1289 * This is called by drawBitmap(), which has to handle images that may be too
1290 * large to be represented by a single texture.
1291 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001292 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1293 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001294 */
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001295void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001296 const SkRect& srcRect,
reed@google.comac10a2d2010-12-22 21:39:39 +00001297 const SkMatrix& m,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001298 const GrTextureParams& params,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001299 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001300 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1301 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001302
reed@google.com9c49bc32011-07-07 13:42:37 +00001303 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001304 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001305 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001306 return;
1307 }
1308
reed@google.comac10a2d2010-12-22 21:39:39 +00001309 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001310 SkAutoCachedTexture act(this, bitmap, &params, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001311 if (NULL == texture) {
1312 return;
1313 }
1314
robertphillips@google.combac6b052012-09-28 18:06:49 +00001315 GrRect dstRect(srcRect);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001316 GrRect paintRect;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001317 SkScalar wInv = SkScalarInvert(SkIntToScalar(bitmap.width()));
1318 SkScalar hInv = SkScalarInvert(SkIntToScalar(bitmap.height()));
1319 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1320 SkScalarMul(srcRect.fTop, hInv),
1321 SkScalarMul(srcRect.fRight, wInv),
1322 SkScalarMul(srcRect.fBottom, hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001323
rmistry@google.comd6176b02012-08-23 18:14:13 +00001324 bool needsTextureDomain = false;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001325 if (params.isBilerp()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001326 // Need texture domain if drawing a sub rect.
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001327 needsTextureDomain = srcRect.width() < bitmap.width() ||
robertphillips@google.combac6b052012-09-28 18:06:49 +00001328 srcRect.height() < bitmap.height();
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001329 if (m.rectStaysRect() && fContext->getMatrix().rectStaysRect()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001330 // sampling is axis-aligned
robertphillips@google.combac6b052012-09-28 18:06:49 +00001331 GrRect transformedRect;
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001332 SkMatrix srcToDeviceMatrix(m);
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001333 srcToDeviceMatrix.postConcat(fContext->getMatrix());
robertphillips@google.combac6b052012-09-28 18:06:49 +00001334 srcToDeviceMatrix.mapRect(&transformedRect, srcRect);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001335
robertphillips@google.combac6b052012-09-28 18:06:49 +00001336 if (hasAlignedSamples(srcRect, transformedRect)) {
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001337 // We could also turn off filtering here (but we already did a cache lookup with
1338 // params).
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001339 needsTextureDomain = false;
1340 } else {
1341 needsTextureDomain = needsTextureDomain &&
robertphillips@google.combac6b052012-09-28 18:06:49 +00001342 mayColorBleed(srcRect, transformedRect, m);
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001343 }
1344 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001345 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001346
1347 GrRect textureDomain = GrRect::MakeEmpty();
bsalomon@google.com0ac6af42013-01-16 15:16:18 +00001348 SkAutoTUnref<GrEffectRef> effect;
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001349 if (needsTextureDomain) {
1350 // Use a constrained texture domain to avoid color bleeding
bsalomon@google.com81712882012-11-01 17:12:34 +00001351 SkScalar left, top, right, bottom;
1352 if (srcRect.width() > SK_Scalar1) {
1353 SkScalar border = SK_ScalarHalf / bitmap.width();
junov@google.com6acc9b32011-05-16 18:32:07 +00001354 left = paintRect.left() + border;
1355 right = paintRect.right() - border;
1356 } else {
bsalomon@google.com81712882012-11-01 17:12:34 +00001357 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
junov@google.com6acc9b32011-05-16 18:32:07 +00001358 }
bsalomon@google.com81712882012-11-01 17:12:34 +00001359 if (srcRect.height() > SK_Scalar1) {
1360 SkScalar border = SK_ScalarHalf / bitmap.height();
junov@google.com6acc9b32011-05-16 18:32:07 +00001361 top = paintRect.top() + border;
1362 bottom = paintRect.bottom() - border;
1363 } else {
bsalomon@google.com81712882012-11-01 17:12:34 +00001364 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
junov@google.com6acc9b32011-05-16 18:32:07 +00001365 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001366 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +00001367 effect.reset(GrTextureDomainEffect::Create(texture,
1368 SkMatrix::I(),
1369 textureDomain,
1370 GrTextureDomainEffect::kClamp_WrapMode,
1371 params.isBilerp()));
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001372 } else {
bsalomon@google.com68b58c92013-01-17 16:50:08 +00001373 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
junov@google.com6acc9b32011-05-16 18:32:07 +00001374 }
bsalomon@google.com08283af2012-10-26 13:01:20 +00001375 grPaint->colorStage(kBitmapTextureIdx)->setEffect(effect);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001376 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001377}
1378
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001379namespace {
1380
bsalomon@google.comf271cc72012-10-24 19:35:13 +00001381void apply_effect(GrContext* context,
1382 GrTexture* srcTexture,
1383 GrTexture* dstTexture,
1384 const GrRect& rect,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +00001385 GrEffectRef* effect) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001386 SkASSERT(srcTexture && srcTexture->getContext() == context);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001387 GrContext::AutoMatrix am;
1388 am.setIdentity(context);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001389 GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget());
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001390 GrContext::AutoClip acs(context, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001391
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001392 GrPaint paint;
bsalomon@google.comdbe49f72012-11-05 16:36:02 +00001393 paint.colorStage(0)->setEffect(effect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001394 context->drawRect(paint, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001395}
1396
1397};
1398
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001399static GrTexture* filter_texture(SkDevice* device, GrContext* context,
1400 GrTexture* texture, SkImageFilter* filter,
1401 const GrRect& rect) {
reed@google.com8926b162012-03-23 15:36:36 +00001402 GrAssert(filter);
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001403 SkDeviceImageFilterProxy proxy(device);
reed@google.com8926b162012-03-23 15:36:36 +00001404
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001405 GrTextureDesc desc;
1406 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1407 desc.fWidth = SkScalarCeilToInt(rect.width());
1408 desc.fHeight = SkScalarCeilToInt(rect.height());
bsalomon@google.com0342a852012-08-20 19:22:38 +00001409 desc.fConfig = kRGBA_8888_GrPixelConfig;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +00001410 GrEffectRef* effect;
reed@google.com8926b162012-03-23 15:36:36 +00001411
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001412 if (filter->canFilterImageGPU()) {
senorblanco@chromium.org985fa792012-10-24 15:14:26 +00001413 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1414 // filter. Also set the clip wide open and the matrix to identity.
1415 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org171f5c72013-01-10 19:55:18 +00001416 texture = filter->filterImageGPU(&proxy, texture, rect);
bsalomon@google.com021fc732012-10-25 12:47:42 +00001417 } else if (filter->asNewEffect(&effect, texture)) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001418 GrAutoScratchTexture dst(context, desc);
bsalomon@google.com021fc732012-10-25 12:47:42 +00001419 apply_effect(context, texture, dst.texture(), rect, effect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001420 texture = dst.detach();
bsalomon@google.com021fc732012-10-25 12:47:42 +00001421 effect->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001422 }
1423 return texture;
1424}
1425
reed@google.comac10a2d2010-12-22 21:39:39 +00001426void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001427 int left, int top, const SkPaint& paint) {
1428 // drawSprite is defined to be in device coords.
1429 CHECK_SHOULD_DRAW(draw, true);
reed@google.comac10a2d2010-12-22 21:39:39 +00001430
reed@google.com8926b162012-03-23 15:36:36 +00001431 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001432 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1433 return;
1434 }
1435
reed@google.com76dd2772012-01-05 21:15:07 +00001436 int w = bitmap.width();
1437 int h = bitmap.height();
1438
bsalomon@google.com5782d712011-01-21 21:03:59 +00001439 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001440 if(!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001441 return;
1442 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001443
bsalomon@google.com08283af2012-10-26 13:01:20 +00001444 GrEffectStage* stage = grPaint.colorStage(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001445
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001446 GrTexture* texture;
bsalomon@google.com08283af2012-10-26 13:01:20 +00001447 stage->reset();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001448 // draw sprite uses the default texture params
1449 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +00001450 grPaint.colorStage(kBitmapTextureIdx)->setEffect(
bsalomon@google.com68b58c92013-01-17 16:50:08 +00001451 GrSimpleTextureEffect::Create(texture, SkMatrix::I()))->unref();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001452
reed@google.com8926b162012-03-23 15:36:36 +00001453 SkImageFilter* filter = paint.getImageFilter();
1454 if (NULL != filter) {
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001455 GrTexture* filteredTexture = filter_texture(this, fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001456 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001457 if (filteredTexture) {
bsalomon@google.com0ac6af42013-01-16 15:16:18 +00001458 grPaint.colorStage(kBitmapTextureIdx)->setEffect(
bsalomon@google.com68b58c92013-01-17 16:50:08 +00001459 GrSimpleTextureEffect::Create(filteredTexture, SkMatrix::I()))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001460 texture = filteredTexture;
1461 filteredTexture->unref();
1462 }
reed@google.com76dd2772012-01-05 21:15:07 +00001463 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001464
bsalomon@google.com5782d712011-01-21 21:03:59 +00001465 fContext->drawRectToRect(grPaint,
bsalomon@google.com81712882012-11-01 17:12:34 +00001466 GrRect::MakeXYWH(SkIntToScalar(left),
1467 SkIntToScalar(top),
1468 SkIntToScalar(w),
1469 SkIntToScalar(h)),
1470 GrRect::MakeWH(SK_Scalar1 * w / texture->width(),
1471 SK_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001472}
1473
reed@google.com33535f32012-09-25 15:37:50 +00001474void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1475 const SkRect* src, const SkRect& dst,
1476 const SkPaint& paint) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001477 SkMatrix matrix;
1478 SkRect bitmapBounds, tmpSrc;
1479
1480 bitmapBounds.set(0, 0,
1481 SkIntToScalar(bitmap.width()),
1482 SkIntToScalar(bitmap.height()));
1483
reed@google.com33535f32012-09-25 15:37:50 +00001484 // Compute matrix from the two rectangles
robertphillips@google.combac6b052012-09-28 18:06:49 +00001485 if (NULL != src) {
1486 tmpSrc = *src;
1487 } else {
1488 tmpSrc = bitmapBounds;
1489 }
1490 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1491
1492 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1493 if (NULL != src) {
1494 if (!bitmapBounds.contains(tmpSrc)) {
1495 if (!tmpSrc.intersect(bitmapBounds)) {
1496 return; // nothing to draw
reed@google.com33535f32012-09-25 15:37:50 +00001497 }
reed@google.com33535f32012-09-25 15:37:50 +00001498 }
reed@google.com33535f32012-09-25 15:37:50 +00001499 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +00001500
robertphillips@google.combac6b052012-09-28 18:06:49 +00001501 this->drawBitmapCommon(draw, bitmap, &tmpSrc, matrix, paint);
reed@google.com33535f32012-09-25 15:37:50 +00001502}
1503
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001504void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001505 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001506 // clear of the source device must occur before CHECK_SHOULD_DRAW
1507 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1508 if (dev->fNeedClear) {
1509 // TODO: could check here whether we really need to draw at all
1510 dev->clear(0x0);
1511 }
1512
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001513 // drawDevice is defined to be in device coords.
1514 CHECK_SHOULD_DRAW(draw, true);
reed@google.comac10a2d2010-12-22 21:39:39 +00001515
bsalomon@google.com5782d712011-01-21 21:03:59 +00001516 GrPaint grPaint;
bsalomon@google.com08283af2012-10-26 13:01:20 +00001517 grPaint.colorStage(kBitmapTextureIdx)->reset();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001518 if (!dev->bindDeviceAsTexture(&grPaint) ||
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001519 !skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001520 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001521 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001522
bsalomon@google.com08283af2012-10-26 13:01:20 +00001523 GrTexture* devTex = grPaint.getColorStage(kBitmapTextureIdx).getEffect()->texture(0);
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001524 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001525
reed@google.com8926b162012-03-23 15:36:36 +00001526 SkImageFilter* filter = paint.getImageFilter();
1527 if (NULL != filter) {
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001528 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001529 SkIntToScalar(devTex->height()));
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001530 GrTexture* filteredTexture = filter_texture(this, fContext, devTex, filter, rect);
reed@google.com8926b162012-03-23 15:36:36 +00001531 if (filteredTexture) {
bsalomon@google.com0ac6af42013-01-16 15:16:18 +00001532 grPaint.colorStage(kBitmapTextureIdx)->setEffect(
bsalomon@google.com68b58c92013-01-17 16:50:08 +00001533 GrSimpleTextureEffect::Create(filteredTexture, SkMatrix::I()))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001534 devTex = filteredTexture;
1535 filteredTexture->unref();
1536 }
1537 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001538
senorblanco@chromium.org514b9222013-01-18 21:53:12 +00001539 const SkBitmap& bm = dev->accessBitmap(false);
1540 int w = bm.width();
1541 int h = bm.height();
1542
bsalomon@google.com81712882012-11-01 17:12:34 +00001543 GrRect dstRect = GrRect::MakeXYWH(SkIntToScalar(x),
1544 SkIntToScalar(y),
1545 SkIntToScalar(w),
1546 SkIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001547
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001548 // The device being drawn may not fill up its texture (saveLayer uses
1549 // the approximate ).
bsalomon@google.com81712882012-11-01 17:12:34 +00001550 GrRect srcRect = GrRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1551 SK_Scalar1 * h / devTex->height());
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001552
1553 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001554}
1555
reed@google.com8926b162012-03-23 15:36:36 +00001556bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
bsalomon@google.com8ea78d82012-10-24 20:11:30 +00001557 if (!filter->asNewEffect(NULL, NULL) &&
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001558 !filter->canFilterImageGPU()) {
reed@google.com76dd2772012-01-05 21:15:07 +00001559 return false;
1560 }
reed@google.com8926b162012-03-23 15:36:36 +00001561 return true;
1562}
1563
1564bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1565 const SkMatrix& ctm,
1566 SkBitmap* result, SkIPoint* offset) {
1567 // want explicitly our impl, so guard against a subclass of us overriding it
1568 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001569 return false;
1570 }
reed@google.com8926b162012-03-23 15:36:36 +00001571
1572 SkAutoLockPixels alp(src, !src.getTexture());
1573 if (!src.getTexture() && !src.readyToDraw()) {
1574 return false;
1575 }
1576
1577 GrPaint paint;
reed@google.com8926b162012-03-23 15:36:36 +00001578
reed@google.com8926b162012-03-23 15:36:36 +00001579 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001580 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1581 // must be pushed upstack.
1582 SkAutoCachedTexture act(this, src, NULL, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001583
1584 result->setConfig(src.config(), src.width(), src.height());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001585 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001586 SkIntToScalar(src.height()));
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001587 GrTexture* resultTexture = filter_texture(this, fContext, texture, filter, rect);
reed@google.com8926b162012-03-23 15:36:36 +00001588 if (resultTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001589 result->setPixelRef(SkNEW_ARGS(SkGrTexturePixelRef,
1590 (resultTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001591 resultTexture->unref();
1592 }
reed@google.com76dd2772012-01-05 21:15:07 +00001593 return true;
1594}
1595
reed@google.comac10a2d2010-12-22 21:39:39 +00001596///////////////////////////////////////////////////////////////////////////////
1597
1598// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001599static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001600 kTriangles_GrPrimitiveType,
1601 kTriangleStrip_GrPrimitiveType,
1602 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001603};
1604
1605void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1606 int vertexCount, const SkPoint vertices[],
1607 const SkPoint texs[], const SkColor colors[],
1608 SkXfermode* xmode,
1609 const uint16_t indices[], int indexCount,
1610 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001611 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001612
bsalomon@google.com5782d712011-01-21 21:03:59 +00001613 GrPaint grPaint;
bsalomon@google.com5782d712011-01-21 21:03:59 +00001614 // we ignore the shader if texs is null.
1615 if (NULL == texs) {
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001616 if (!skPaint2GrPaintNoShader(this, paint, false, NULL == colors, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001617 return;
1618 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001619 } else {
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001620 if (!skPaint2GrPaintShader(this, paint, NULL == colors, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001621 return;
1622 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001623 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001624
1625 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001626 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001627 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1628#if 0
1629 return
1630#endif
1631 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001632 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001633
bsalomon@google.com498776a2011-08-16 19:20:44 +00001634 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1635 if (NULL != colors) {
1636 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001637 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001638 for (int i = 0; i < vertexCount; ++i) {
rileya@google.com24f3ad12012-07-18 21:47:40 +00001639 convertedColors[i] = SkColor2GrColor(colors[i]);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001640 }
1641 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001642 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001643 fContext->drawVertices(grPaint,
1644 gVertexMode2PrimitiveType[vmode],
1645 vertexCount,
1646 (GrPoint*) vertices,
1647 (GrPoint*) texs,
1648 colors,
1649 indices,
1650 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001651}
1652
1653///////////////////////////////////////////////////////////////////////////////
1654
1655static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001656 GrFontScaler* scaler = (GrFontScaler*)data;
1657 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001658}
1659
1660static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1661 void* auxData;
1662 GrFontScaler* scaler = NULL;
1663 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1664 scaler = (GrFontScaler*)auxData;
1665 }
1666 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001667 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001668 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1669 }
1670 return scaler;
1671}
1672
1673static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1674 SkFixed fx, SkFixed fy,
1675 const SkGlyph& glyph) {
1676 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1677
bungeman@google.com15865a72012-01-11 16:28:04 +00001678 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001679
1680 if (NULL == procs->fFontScaler) {
1681 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1682 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001683
bungeman@google.com15865a72012-01-11 16:28:04 +00001684 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1685 glyph.getSubXFixed(),
1686 glyph.getSubYFixed()),
1687 SkFixedFloorToFixed(fx),
1688 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001689 procs->fFontScaler);
1690}
1691
bsalomon@google.com5782d712011-01-21 21:03:59 +00001692SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001693
1694 // deferred allocation
1695 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001696 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001697 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1698 fDrawProcs->fContext = fContext;
1699 }
1700
1701 // init our (and GL's) state
1702 fDrawProcs->fTextContext = context;
1703 fDrawProcs->fFontScaler = NULL;
1704 return fDrawProcs;
1705}
1706
1707void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1708 size_t byteLength, SkScalar x, SkScalar y,
1709 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001710 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001711
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001712 if (fContext->getMatrix().hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001713 // this guy will just call our drawPath()
1714 draw.drawText((const char*)text, byteLength, x, y, paint);
1715 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001716 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001717
1718 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001719 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001720 return;
1721 }
bsalomon@google.com0e354aa2012-10-08 20:44:25 +00001722 GrTextContext context(fContext, grPaint);
tomhudson@google.com375ff852012-06-29 18:37:57 +00001723 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001724 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1725 }
1726}
1727
1728void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1729 size_t byteLength, const SkScalar pos[],
1730 SkScalar constY, int scalarsPerPos,
1731 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001732 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001733
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001734 if (fContext->getMatrix().hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001735 // this guy will just call our drawPath()
1736 draw.drawPosText((const char*)text, byteLength, pos, constY,
1737 scalarsPerPos, paint);
1738 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001739 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001740
1741 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001742 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001743 return;
1744 }
bsalomon@google.com0e354aa2012-10-08 20:44:25 +00001745 GrTextContext context(fContext, grPaint);
tomhudson@google.com375ff852012-06-29 18:37:57 +00001746 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001747 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1748 scalarsPerPos, paint);
1749 }
1750}
1751
1752void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1753 size_t len, const SkPath& path,
1754 const SkMatrix* m, const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001755 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001756
1757 SkASSERT(draw.fDevice == this);
1758 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1759}
1760
1761///////////////////////////////////////////////////////////////////////////////
1762
reed@google.comf67e4cf2011-03-15 20:56:58 +00001763bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1764 if (!paint.isLCDRenderText()) {
1765 // we're cool with the paint as is
1766 return false;
1767 }
1768
1769 if (paint.getShader() ||
1770 paint.getXfermode() || // unless its srcover
1771 paint.getMaskFilter() ||
1772 paint.getRasterizer() ||
1773 paint.getColorFilter() ||
1774 paint.getPathEffect() ||
1775 paint.isFakeBoldText() ||
1776 paint.getStyle() != SkPaint::kFill_Style) {
1777 // turn off lcd
1778 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1779 flags->fHinting = paint.getHinting();
1780 return true;
1781 }
1782 // we're cool with the paint as is
1783 return false;
1784}
1785
reed@google.com75d939b2011-12-07 15:07:23 +00001786void SkGpuDevice::flush() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +00001787 DO_DEFERRED_CLEAR();
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001788 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001789}
1790
reed@google.comf67e4cf2011-03-15 20:56:58 +00001791///////////////////////////////////////////////////////////////////////////////
1792
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001793SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1794 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001795 bool isOpaque,
1796 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001797 GrTextureDesc desc;
1798 desc.fConfig = fRenderTarget->config();
1799 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1800 desc.fWidth = width;
1801 desc.fHeight = height;
1802 desc.fSampleCnt = fRenderTarget->numSamples();
1803
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001804 GrTexture* texture;
1805 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001806 // Skia's convention is to only clear a device if it is non-opaque.
1807 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001808
1809#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1810 // layers are never draw in repeat modes, so we can request an approx
1811 // match and ignore any padding.
bsalomon@google.com0797c2c2012-12-20 15:13:01 +00001812 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1813 GrContext::kApprox_ScratchTexMatch :
1814 GrContext::kExact_ScratchTexMatch;
1815 texture = fContext->lockScratchTexture(desc, match);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001816#else
1817 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1818 texture = tunref.get();
1819#endif
1820 if (texture) {
1821 return SkNEW_ARGS(SkGpuDevice,(fContext,
1822 texture,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001823 needClear));
1824 } else {
1825 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1826 width, height);
1827 return NULL;
1828 }
1829}
1830
1831SkGpuDevice::SkGpuDevice(GrContext* context,
1832 GrTexture* texture,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001833 bool needClear)
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001834 : SkDevice(make_bitmap(context, texture->asRenderTarget())) {
1835
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001836 GrAssert(texture && texture->asRenderTarget());
bsalomon@google.com8090e652012-08-28 15:07:11 +00001837 // This constructor is called from onCreateCompatibleDevice. It has locked the RT in the texture
1838 // cache. We pass true for the third argument so that it will get unlocked.
1839 this->initFromRenderTarget(context, texture->asRenderTarget(), true);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001840 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001841}