blob: 49d6562ceaef92945d1ba0c9dc2a37ec4e38efe9 [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.com73818dc2013-03-28 13:23:29 +000040// we use the same effect slot on GrPaint for bitmaps and shaders (since drawBitmap, drawSprite,
41// and drawDevice ignore SkShader)
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000042enum {
bsalomon@google.com73818dc2013-03-28 13:23:29 +000043 kShaderEffectIdx = 0,
44 kBitmapEffectIdx = 0,
45 kColorFilterEffectIdx = 1,
46 kXfermodeEffectIdx = 2,
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000047};
48
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +000049#define MAX_BLUR_SIGMA 4.0f
50// FIXME: This value comes from from SkBlurMaskFilter.cpp.
51// Should probably be put in a common header someplace.
52#define MAX_BLUR_RADIUS SkIntToScalar(128)
53// This constant approximates the scaling done in the software path's
54// "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
55// IMHO, it actually should be 1: we blur "less" than we should do
56// according to the CSS and canvas specs, simply because Safari does the same.
57// Firefox used to do the same too, until 4.0 where they fixed it. So at some
58// point we should probably get rid of these scaling constants and rebaseline
59// all the blur tests.
60#define BLUR_SIGMA_SCALE 0.6f
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000061// This constant represents the screen alignment criterion in texels for
rmistry@google.comd6176b02012-08-23 18:14:13 +000062// requiring texture domain clamping to prevent color bleeding when drawing
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000063// a sub region of a larger source image.
64#define COLOR_BLEED_TOLERANCE SkFloatToScalar(0.001f)
bsalomon@google.com06cd7322012-03-30 18:45:35 +000065
junov@google.comdbfac8a2012-12-06 21:47:40 +000066#define DO_DEFERRED_CLEAR() \
67 do { \
68 if (fNeedClear) { \
69 this->clear(SK_ColorTRANSPARENT); \
70 } \
71 } while (false) \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000072
reed@google.comac10a2d2010-12-22 21:39:39 +000073///////////////////////////////////////////////////////////////////////////////
74
reed@google.comb0a34d82012-07-11 19:57:55 +000075#define CHECK_FOR_NODRAW_ANNOTATION(paint) \
76 do { if (paint.isNoDrawAnnotation()) { return; } } while (0)
77
78///////////////////////////////////////////////////////////////////////////////
79
80
bsalomon@google.com84405e02012-03-05 19:57:21 +000081class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
82public:
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000083 SkAutoCachedTexture()
84 : fDevice(NULL)
85 , fTexture(NULL) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000086 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000087
bsalomon@google.com84405e02012-03-05 19:57:21 +000088 SkAutoCachedTexture(SkGpuDevice* device,
89 const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +000090 const GrTextureParams* params,
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000091 GrTexture** texture)
92 : fDevice(NULL)
93 , fTexture(NULL) {
94 GrAssert(NULL != texture);
bsalomon@google.comb8670992012-07-25 21:27:09 +000095 *texture = this->set(device, bitmap, params);
reed@google.comac10a2d2010-12-22 21:39:39 +000096 }
reed@google.comac10a2d2010-12-22 21:39:39 +000097
bsalomon@google.com84405e02012-03-05 19:57:21 +000098 ~SkAutoCachedTexture() {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000099 if (NULL != fTexture) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000100 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
bsalomon@google.com84405e02012-03-05 19:57:21 +0000101 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000102 }
bsalomon@google.com84405e02012-03-05 19:57:21 +0000103
104 GrTexture* set(SkGpuDevice* device,
105 const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000106 const GrTextureParams* params) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000107 if (NULL != fTexture) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000108 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000109 fTexture = NULL;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000110 }
111 fDevice = device;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000112 GrTexture* result = (GrTexture*)bitmap.getTexture();
113 if (NULL == result) {
114 // Cannot return the native texture so look it up in our cache
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000115 fTexture = GrLockAndRefCachedBitmapTexture(device->context(), bitmap, params);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000116 result = fTexture;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000117 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000118 return result;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000119 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000120
bsalomon@google.com84405e02012-03-05 19:57:21 +0000121private:
122 SkGpuDevice* fDevice;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000123 GrTexture* fTexture;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000124};
reed@google.comac10a2d2010-12-22 21:39:39 +0000125
126///////////////////////////////////////////////////////////////////////////////
127
reed@google.comac10a2d2010-12-22 21:39:39 +0000128struct GrSkDrawProcs : public SkDrawProcs {
129public:
130 GrContext* fContext;
131 GrTextContext* fTextContext;
132 GrFontScaler* fFontScaler; // cached in the skia glyphcache
133};
134
135///////////////////////////////////////////////////////////////////////////////
136
reed@google.comaf951c92011-06-16 19:10:39 +0000137static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
138 switch (config) {
139 case kAlpha_8_GrPixelConfig:
140 *isOpaque = false;
141 return SkBitmap::kA8_Config;
142 case kRGB_565_GrPixelConfig:
143 *isOpaque = true;
144 return SkBitmap::kRGB_565_Config;
145 case kRGBA_4444_GrPixelConfig:
146 *isOpaque = false;
147 return SkBitmap::kARGB_4444_Config;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000148 case kSkia8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +0000149 // we don't currently have a way of knowing whether
150 // a 8888 is opaque based on the config.
151 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000152 return SkBitmap::kARGB_8888_Config;
153 default:
154 *isOpaque = false;
155 return SkBitmap::kNo_Config;
156 }
157}
reed@google.comac10a2d2010-12-22 21:39:39 +0000158
reed@google.comaf951c92011-06-16 19:10:39 +0000159static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000160 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000161
162 bool isOpaque;
163 SkBitmap bitmap;
164 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
165 renderTarget->width(), renderTarget->height());
166 bitmap.setIsOpaque(isOpaque);
167 return bitmap;
168}
169
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000170SkGpuDevice* SkGpuDevice::Create(GrSurface* surface) {
171 GrAssert(NULL != surface);
172 if (NULL == surface->asRenderTarget() || NULL == surface->getContext()) {
173 return NULL;
174 }
175 if (surface->asTexture()) {
176 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asTexture()));
177 } else {
178 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asRenderTarget()));
179 }
180}
181
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000182SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000183: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
bsalomon@google.com8090e652012-08-28 15:07:11 +0000184 this->initFromRenderTarget(context, texture->asRenderTarget(), false);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000185}
186
reed@google.comaf951c92011-06-16 19:10:39 +0000187SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000188: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.com8090e652012-08-28 15:07:11 +0000189 this->initFromRenderTarget(context, renderTarget, false);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000190}
191
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000192void SkGpuDevice::initFromRenderTarget(GrContext* context,
bsalomon@google.com8090e652012-08-28 15:07:11 +0000193 GrRenderTarget* renderTarget,
194 bool cached) {
reed@google.comaf951c92011-06-16 19:10:39 +0000195 fDrawProcs = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000196
reed@google.comaf951c92011-06-16 19:10:39 +0000197 fContext = context;
198 fContext->ref();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000199
reed@google.comaf951c92011-06-16 19:10:39 +0000200 fRenderTarget = NULL;
201 fNeedClear = false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000202
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000203 GrAssert(NULL != renderTarget);
204 fRenderTarget = renderTarget;
205 fRenderTarget->ref();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000206
bsalomon@google.coma2921122012-08-28 12:34:17 +0000207 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
208 // on the RT but not vice-versa.
209 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
210 // busting chrome (for a currently unknown reason).
211 GrSurface* surface = fRenderTarget->asTexture();
212 if (NULL == surface) {
213 surface = fRenderTarget;
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000214 }
bsalomon@google.com8090e652012-08-28 15:07:11 +0000215 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (surface, cached));
bsalomon@google.coma2921122012-08-28 12:34:17 +0000216
reed@google.comaf951c92011-06-16 19:10:39 +0000217 this->setPixelRef(pr, 0)->unref();
218}
219
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000220SkGpuDevice::SkGpuDevice(GrContext* context,
221 SkBitmap::Config config,
222 int width,
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000223 int height,
224 int sampleCount)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000225 : SkDevice(config, width, height, false /*isOpaque*/) {
226
reed@google.comac10a2d2010-12-22 21:39:39 +0000227 fDrawProcs = NULL;
228
reed@google.com7b201d22011-01-11 18:59:23 +0000229 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000230 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000231
reed@google.comac10a2d2010-12-22 21:39:39 +0000232 fRenderTarget = NULL;
233 fNeedClear = false;
234
reed@google.comaf951c92011-06-16 19:10:39 +0000235 if (config != SkBitmap::kRGB_565_Config) {
236 config = SkBitmap::kARGB_8888_Config;
237 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000238
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000239 GrTextureDesc desc;
240 desc.fFlags = kRenderTarget_GrTextureFlagBit;
241 desc.fWidth = width;
242 desc.fHeight = height;
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000243 desc.fConfig = SkBitmapConfig2GrPixelConfig(config);
244 desc.fSampleCnt = sampleCount;
reed@google.comac10a2d2010-12-22 21:39:39 +0000245
bsalomon@google.coma2921122012-08-28 12:34:17 +0000246 SkAutoTUnref<GrTexture> texture(fContext->createUncachedTexture(desc, NULL, 0));
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000247
bsalomon@google.coma2921122012-08-28 12:34:17 +0000248 if (NULL != texture) {
249 fRenderTarget = texture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000250 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000251
reed@google.comaf951c92011-06-16 19:10:39 +0000252 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000253
reed@google.comaf951c92011-06-16 19:10:39 +0000254 // wrap the bitmap with a pixelref to expose our texture
bsalomon@google.coma2921122012-08-28 12:34:17 +0000255 SkGrPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (texture));
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000256 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000257 } else {
258 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
259 width, height);
260 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000261 }
262}
263
264SkGpuDevice::~SkGpuDevice() {
265 if (fDrawProcs) {
266 delete fDrawProcs;
267 }
268
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000269 // The GrContext takes a ref on the target. We don't want to cause the render
270 // target to be unnecessarily kept alive.
271 if (fContext->getRenderTarget() == fRenderTarget) {
272 fContext->setRenderTarget(NULL);
273 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000274
robertphillips@google.com055f9082012-10-24 13:24:11 +0000275 if (fContext->getClip() == &fClipData) {
276 fContext->setClip(NULL);
277 }
278
bsalomon@google.coma2921122012-08-28 12:34:17 +0000279 SkSafeUnref(fRenderTarget);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000280 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000281}
282
reed@google.comac10a2d2010-12-22 21:39:39 +0000283///////////////////////////////////////////////////////////////////////////////
284
285void SkGpuDevice::makeRenderTargetCurrent() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000286 DO_DEFERRED_CLEAR();
reed@google.comac10a2d2010-12-22 21:39:39 +0000287 fContext->setRenderTarget(fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000288}
289
290///////////////////////////////////////////////////////////////////////////////
291
bsalomon@google.comc4364992011-11-07 15:54:49 +0000292namespace {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000293GrPixelConfig config8888_to_grconfig_and_flags(SkCanvas::Config8888 config8888, uint32_t* flags) {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000294 switch (config8888) {
295 case SkCanvas::kNative_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000296 *flags = 0;
297 return kSkia8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000298 case SkCanvas::kNative_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000299 *flags = GrContext::kUnpremul_PixelOpsFlag;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000300 return kSkia8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000301 case SkCanvas::kBGRA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000302 *flags = 0;
303 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000304 case SkCanvas::kBGRA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000305 *flags = GrContext::kUnpremul_PixelOpsFlag;
306 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000307 case SkCanvas::kRGBA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000308 *flags = 0;
309 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000310 case SkCanvas::kRGBA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000311 *flags = GrContext::kUnpremul_PixelOpsFlag;
312 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000313 default:
314 GrCrash("Unexpected Config8888.");
bsalomon@google.comccaa0022012-09-25 19:55:07 +0000315 *flags = 0; // suppress warning
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000316 return kSkia8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000317 }
318}
319}
320
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000321bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
322 int x, int y,
323 SkCanvas::Config8888 config8888) {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000324 DO_DEFERRED_CLEAR();
bsalomon@google.com910267d2011-11-02 20:06:25 +0000325 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
326 SkASSERT(!bitmap.isNull());
327 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000328
bsalomon@google.com910267d2011-11-02 20:06:25 +0000329 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000330 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000331 uint32_t flags;
332 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000333 return fContext->readRenderTargetPixels(fRenderTarget,
334 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000335 bitmap.width(),
336 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000337 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000338 bitmap.getPixels(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000339 bitmap.rowBytes(),
340 flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000341}
342
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000343void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
344 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000345 SkAutoLockPixels alp(bitmap);
346 if (!bitmap.readyToDraw()) {
347 return;
348 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000349
350 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000351 uint32_t flags;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000352 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000353 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000354 } else {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000355 flags = 0;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000356 config= SkBitmapConfig2GrPixelConfig(bitmap.config());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000357 }
358
bsalomon@google.com6f379512011-11-16 20:36:03 +0000359 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000360 config, bitmap.getPixels(), bitmap.rowBytes(), flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000361}
362
robertphillips@google.com46f93502012-08-07 15:38:08 +0000363namespace {
humper@google.com05af1af2013-01-07 16:47:43 +0000364void purgeClipCB(int genID, void* ) {
robertphillips@google.com46f93502012-08-07 15:38:08 +0000365
366 if (SkClipStack::kInvalidGenID == genID ||
367 SkClipStack::kEmptyGenID == genID ||
368 SkClipStack::kWideOpenGenID == genID) {
369 // none of these cases will have a cached clip mask
370 return;
371 }
372
373}
374};
375
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000376void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
377 INHERITED::onAttachToCanvas(canvas);
378
379 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000380 fClipData.fClipStack = canvas->getClipStack();
robertphillips@google.com46f93502012-08-07 15:38:08 +0000381
382 fClipData.fClipStack->addPurgeClipCallback(purgeClipCB, fContext);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000383}
384
385void SkGpuDevice::onDetachFromCanvas() {
386 INHERITED::onDetachFromCanvas();
387
robertphillips@google.com46f93502012-08-07 15:38:08 +0000388 // TODO: iterate through the clip stack and clean up any cached clip masks
389 fClipData.fClipStack->removePurgeClipCallback(purgeClipCB, fContext);
390
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000391 fClipData.fClipStack = NULL;
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000392}
393
robertphillips@google.com607fe072012-07-24 13:54:00 +0000394#ifdef SK_DEBUG
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000395static void check_bounds(const GrClipData& clipData,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000396 const SkRegion& clipRegion,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000397 int renderTargetWidth,
398 int renderTargetHeight) {
399
robertphillips@google.com7b112892012-07-31 15:18:21 +0000400 SkIRect devBound;
401
402 devBound.setLTRB(0, 0, renderTargetWidth, renderTargetHeight);
403
robertphillips@google.com607fe072012-07-24 13:54:00 +0000404 SkClipStack::BoundsType boundType;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000405 SkRect canvTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000406
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000407 clipData.fClipStack->getBounds(&canvTemp, &boundType);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000408 if (SkClipStack::kNormal_BoundsType == boundType) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000409 SkIRect devTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000410
robertphillips@google.com7b112892012-07-31 15:18:21 +0000411 canvTemp.roundOut(&devTemp);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000412
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000413 devTemp.offset(-clipData.fOrigin.fX, -clipData.fOrigin.fY);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000414
robertphillips@google.com7b112892012-07-31 15:18:21 +0000415 if (!devBound.intersect(devTemp)) {
416 devBound.setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000417 }
418 }
419
robertphillips@google.com768fee82012-08-02 12:42:43 +0000420 GrAssert(devBound.contains(clipRegion.getBounds()));
robertphillips@google.com607fe072012-07-24 13:54:00 +0000421}
422#endif
423
reed@google.comac10a2d2010-12-22 21:39:39 +0000424///////////////////////////////////////////////////////////////////////////////
425
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000426// call this every draw call, to ensure that the context reflects our state,
reed@google.comac10a2d2010-12-22 21:39:39 +0000427// and not the state from some other canvas/device
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000428void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000429 GrAssert(NULL != fClipData.fClipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000430
reed@google.comac10a2d2010-12-22 21:39:39 +0000431 fContext->setRenderTarget(fRenderTarget);
432
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000433 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000434
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000435 if (forceIdentity) {
436 fContext->setIdentityMatrix();
437 } else {
438 fContext->setMatrix(*draw.fMatrix);
439 }
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000440 fClipData.fOrigin = this->getOrigin();
reed@google.comac10a2d2010-12-22 21:39:39 +0000441
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000442#ifdef SK_DEBUG
443 check_bounds(fClipData, *draw.fClip, fRenderTarget->width(), fRenderTarget->height());
444#endif
445
446 fContext->setClip(&fClipData);
447
448 DO_DEFERRED_CLEAR();
reed@google.comac10a2d2010-12-22 21:39:39 +0000449}
450
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000451SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000452 DO_DEFERRED_CLEAR();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000453 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000454}
455
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000456bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
bsalomon@google.coma2921122012-08-28 12:34:17 +0000457 GrTexture* texture = fRenderTarget->asTexture();
458 if (NULL != texture) {
bsalomon@google.com73818dc2013-03-28 13:23:29 +0000459 paint->colorStage(kBitmapEffectIdx)->setEffect(
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000460 GrSimpleTextureEffect::Create(texture, SkMatrix::I()))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000461 return true;
462 }
463 return false;
464}
465
466///////////////////////////////////////////////////////////////////////////////
467
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000468SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
469SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
470SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
471SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
472SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
473 shader_type_mismatch);
rileya@google.com3e332582012-07-03 13:43:35 +0000474SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
475 shader_type_mismatch);
rileya@google.com22e57f92012-07-19 15:16:19 +0000476SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
477SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000478
bsalomon@google.com84405e02012-03-05 19:57:21 +0000479namespace {
480
481// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
482// justAlpha indicates that skPaint's alpha should be used rather than the color
483// Callers may subsequently modify the GrPaint. Setting constantColor indicates
484// that the final paint will draw the same color at every pixel. This allows
485// an optimization where the the color filter can be applied to the skPaint's
twiz@google.com58071162012-07-18 21:41:50 +0000486// color once while converting to GrPaint and then ignored.
487inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
488 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000489 bool justAlpha,
490 bool constantColor,
491 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000492
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000493 grPaint->setDither(skPaint.isDither());
494 grPaint->setAntiAlias(skPaint.isAntiAlias());
bsalomon@google.com5782d712011-01-21 21:03:59 +0000495
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000496 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
497 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000498
499 SkXfermode* mode = skPaint.getXfermode();
bsalomon@google.comdb446252013-03-27 18:46:16 +0000500 GrEffectRef* xferEffect = NULL;
bsalomon@google.comf51c0132013-03-27 18:31:15 +0000501 if (SkXfermode::AsNewEffect(mode, dev->context(), &xferEffect, &sm, &dm)) {
bsalomon@google.com73818dc2013-03-28 13:23:29 +0000502 SkSafeUnref(grPaint->colorStage(kXfermodeEffectIdx)->setEffect(xferEffect));
bsalomon@google.comf51c0132013-03-27 18:31:15 +0000503 } else {
504 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000505#if 0
bsalomon@google.comf51c0132013-03-27 18:31:15 +0000506 return false;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000507#endif
bsalomon@google.com5782d712011-01-21 21:03:59 +0000508 }
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000509 grPaint->setBlendFunc(sk_blend_to_grblend(sm), sk_blend_to_grblend(dm));
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000510
bsalomon@google.com5782d712011-01-21 21:03:59 +0000511 if (justAlpha) {
512 uint8_t alpha = skPaint.getAlpha();
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000513 grPaint->setColor(GrColorPackRGBA(alpha, alpha, alpha, alpha));
Scroggod757df22011-05-16 13:11:16 +0000514 // justAlpha is currently set to true only if there is a texture,
515 // so constantColor should not also be true.
516 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000517 } else {
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000518 grPaint->setColor(SkColor2GrColor(skPaint.getColor()));
bsalomon@google.com73818dc2013-03-28 13:23:29 +0000519 GrAssert(!grPaint->isColorStageEnabled(kShaderEffectIdx));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000520 }
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000521
Scroggo97c88c22011-05-11 14:05:25 +0000522 SkColorFilter* colorFilter = skPaint.getColorFilter();
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000523 if (NULL != colorFilter) {
524 // if the source color is a constant then apply the filter here once rather than per pixel
525 // in a shader.
526 if (constantColor) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000527 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000528 grPaint->setColor(SkColor2GrColor(filtered));
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000529 } else {
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000530 SkAutoTUnref<GrEffectRef> effect(colorFilter->asNewEffect(dev->context()));
bsalomon@google.com021fc732012-10-25 12:47:42 +0000531 if (NULL != effect.get()) {
bsalomon@google.com73818dc2013-03-28 13:23:29 +0000532 grPaint->colorStage(kColorFilterEffectIdx)->setEffect(effect);
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000533 } else {
bsalomon@google.com8ea78d82012-10-24 20:11:30 +0000534 // TODO: rewrite this using asNewEffect()
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000535 SkColor color;
536 SkXfermode::Mode filterMode;
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000537 if (colorFilter->asColorMode(&color, &filterMode)) {
538 grPaint->setXfermodeColorFilter(filterMode, SkColor2GrColor(color));
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000539 }
540 }
Scroggod757df22011-05-16 13:11:16 +0000541 }
Scroggo97c88c22011-05-11 14:05:25 +0000542 }
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000543
bsalomon@google.com5782d712011-01-21 21:03:59 +0000544 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000545}
546
bsalomon@google.com84405e02012-03-05 19:57:21 +0000547// This function is similar to skPaint2GrPaintNoShader but also converts
bsalomon@google.com08283af2012-10-26 13:01:20 +0000548// skPaint's shader to a GrTexture/GrEffectStage if possible. The texture to
bsalomon@google.com84405e02012-03-05 19:57:21 +0000549// be used is set on grPaint and returned in param act. constantColor has the
550// same meaning as in skPaint2GrPaintNoShader.
551inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
552 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000553 bool constantColor,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000554 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000555 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000556 if (NULL == shader) {
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000557 return skPaint2GrPaintNoShader(dev, skPaint, false, constantColor, grPaint);
558 } else if (!skPaint2GrPaintNoShader(dev, skPaint, true, false, grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000559 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000560 }
561
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000562 SkAutoTUnref<GrEffectRef> effect(shader->asNewEffect(dev->context(), skPaint));
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000563 if (NULL != effect.get()) {
bsalomon@google.com73818dc2013-03-28 13:23:29 +0000564 grPaint->colorStage(kShaderEffectIdx)->setEffect(effect);
rileya@google.com91f319c2012-07-25 17:18:31 +0000565 return true;
566 }
567
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000568 // We still don't have SkColorShader::asNewEffect() implemented.
569 SkShader::GradientInfo info;
570 SkColor color;
reed@google.comac10a2d2010-12-22 21:39:39 +0000571
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000572 info.fColors = &color;
573 info.fColorOffsets = NULL;
574 info.fColorCount = 1;
575 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
576 SkPaint copy(skPaint);
577 copy.setShader(NULL);
578 // modulate the paint alpha by the shader's solid color alpha
579 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
580 copy.setColor(SkColorSetA(color, newA));
581 return skPaint2GrPaintNoShader(dev, copy, false, constantColor, grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000582 }
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000583 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000584}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000585}
reed@google.comac10a2d2010-12-22 21:39:39 +0000586
587///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000588void SkGpuDevice::clear(SkColor color) {
bsalomon@google.com32cf29e2013-01-25 15:03:18 +0000589 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
590 fContext->clear(&rect, SkColor2GrColor(color), fRenderTarget);
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000591 fNeedClear = false;
bsalomon@google.com398109c2011-04-14 18:40:27 +0000592}
593
reed@google.comac10a2d2010-12-22 21:39:39 +0000594void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000595 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000596
bsalomon@google.com5782d712011-01-21 21:03:59 +0000597 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000598 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000599 return;
600 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000601
602 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000603}
604
605// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000606static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000607 kPoints_GrPrimitiveType,
608 kLines_GrPrimitiveType,
609 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000610};
611
612void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000613 size_t count, const SkPoint pts[], const SkPaint& paint) {
epoger@google.comb58772f2013-03-08 09:09:10 +0000614 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000615 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000616
617 SkScalar width = paint.getStrokeWidth();
618 if (width < 0) {
619 return;
620 }
621
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000622 // we only handle hairlines and paints without path effects or mask filters,
623 // else we let the SkDraw call our drawPath()
624 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000625 draw.drawPoints(mode, count, pts, paint, true);
626 return;
627 }
628
bsalomon@google.com5782d712011-01-21 21:03:59 +0000629 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000630 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000631 return;
632 }
633
bsalomon@google.com5782d712011-01-21 21:03:59 +0000634 fContext->drawVertices(grPaint,
635 gPointMode2PrimtiveType[mode],
636 count,
637 (GrPoint*)pts,
638 NULL,
639 NULL,
640 NULL,
641 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000642}
643
reed@google.comc9aa5872011-04-05 21:05:37 +0000644///////////////////////////////////////////////////////////////////////////////
645
reed@google.comac10a2d2010-12-22 21:39:39 +0000646void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000647 const SkPaint& paint) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000648 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000649 CHECK_SHOULD_DRAW(draw, false);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000650
bungeman@google.com79bd8772011-07-18 15:34:08 +0000651 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000652 SkScalar width = paint.getStrokeWidth();
653
654 /*
655 We have special code for hairline strokes, miter-strokes, and fills.
656 Anything else we just call our path code.
657 */
658 bool usePath = doStroke && width > 0 &&
659 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000660 // another two reasons we might need to call drawPath...
661 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000662 usePath = true;
663 }
reed@google.com67db6642011-05-26 11:46:35 +0000664 // until we aa rotated rects...
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000665 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
reed@google.com67db6642011-05-26 11:46:35 +0000666 usePath = true;
667 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000668 // small miter limit means right angles show bevel...
669 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
670 paint.getStrokeMiter() < SK_ScalarSqrt2)
671 {
672 usePath = true;
673 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000674 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000675 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
676 usePath = true;
677 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000678
679 if (usePath) {
680 SkPath path;
681 path.addRect(rect);
682 this->drawPath(draw, path, paint, NULL, true);
683 return;
684 }
685
686 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000687 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000688 return;
689 }
reed@google.com20efde72011-05-09 17:00:02 +0000690 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000691}
692
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000693///////////////////////////////////////////////////////////////////////////////
694
695void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
696 const SkPaint& paint) {
697 CHECK_FOR_NODRAW_ANNOTATION(paint);
698 CHECK_SHOULD_DRAW(draw, false);
699
700 bool usePath = false;
701 // some basic reasons we might need to call drawPath...
702 if (paint.getMaskFilter() || paint.getPathEffect()) {
703 usePath = true;
704 }
705
706 if (usePath) {
707 SkPath path;
708 path.addOval(oval);
709 this->drawPath(draw, path, paint, NULL, true);
710 return;
711 }
712
713 GrPaint grPaint;
714 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
715 return;
716 }
717 SkStrokeRec stroke(paint);
718
719 fContext->drawOval(grPaint, oval, stroke);
720}
721
reed@google.com69302852011-02-16 18:08:07 +0000722#include "SkMaskFilter.h"
723#include "SkBounder.h"
724
bsalomon@google.com85003222012-03-28 14:44:37 +0000725///////////////////////////////////////////////////////////////////////////////
726
727// helpers for applying mask filters
728namespace {
729
bsalomon@google.com85003222012-03-28 14:44:37 +0000730// We prefer to blur small rect with small radius via CPU.
731#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
732#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
733inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
734 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
735 rect.height() <= MIN_GPU_BLUR_SIZE &&
736 radius <= MIN_GPU_BLUR_RADIUS) {
737 return true;
738 }
739 return false;
740}
741
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000742bool drawWithGPUMaskFilter(GrContext* context, const SkPath& devPath, const SkStrokeRec& stroke,
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000743 SkMaskFilter* filter, const SkRegion& clip,
sugoi@google.com12b4e272012-12-06 20:13:11 +0000744 SkBounder* bounder, GrPaint* grp) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000745 SkMaskFilter::BlurInfo info;
746 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000747 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000748 return false;
749 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000750 SkScalar radius = info.fIgnoreTransform ? info.fRadius
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000751 : context->getMatrix().mapRadius(info.fRadius);
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000752 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000753 if (radius <= 0) {
754 return false;
755 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000756
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000757 SkRect srcRect = devPath.getBounds();
bsalomon@google.com85003222012-03-28 14:44:37 +0000758 if (shouldDrawBlurWithCPU(srcRect, radius)) {
759 return false;
760 }
761
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000762 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000763 float sigma3 = sigma * 3.0f;
764
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000765 SkRect clipRect;
766 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000767
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000768 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000769 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
770 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000771 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000772 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000773 SkIRect finalIRect;
774 finalRect.roundOut(&finalIRect);
775 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000776 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000777 }
778 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000779 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000780 }
781 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000782 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000783 GrTextureDesc desc;
784 desc.fFlags = kRenderTarget_GrTextureFlagBit;
785 desc.fWidth = SkScalarCeilToInt(srcRect.width());
786 desc.fHeight = SkScalarCeilToInt(srcRect.height());
787 // We actually only need A8, but it often isn't supported as a
788 // render target so default to RGBA_8888
bsalomon@google.com0342a852012-08-20 19:22:38 +0000789 desc.fConfig = kRGBA_8888_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000790
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000791 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
792 desc.fConfig = kAlpha_8_GrPixelConfig;
793 }
794
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000795 GrAutoScratchTexture pathEntry(context, desc);
796 GrTexture* pathTexture = pathEntry.texture();
797 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000798 return false;
799 }
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000800
robertphillips@google.comccb39502012-10-01 18:25:13 +0000801 SkAutoTUnref<GrTexture> blurTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000802
robertphillips@google.comccb39502012-10-01 18:25:13 +0000803 {
804 GrContext::AutoRenderTarget art(context, pathTexture->asRenderTarget());
805 GrContext::AutoClip ac(context, srcRect);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000806
robertphillips@google.comccb39502012-10-01 18:25:13 +0000807 context->clear(NULL, 0);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000808
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000809 GrPaint tempPaint;
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000810 if (grp->isAntiAlias()) {
811 tempPaint.setAntiAlias(true);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000812 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
813 // blend coeff of zero requires dual source blending support in order
814 // to properly blend partially covered pixels. This means the AA
815 // code path may not be taken. So we use a dst blend coeff of ISA. We
816 // could special case AA draws to a dst surface with known alpha=0 to
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000817 // use a zero dst coeff when dual source blending isn't available.f
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000818 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000819 }
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000820
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000821 GrContext::AutoMatrix am;
822
823 // Draw hard shadow to pathTexture with path top-left at origin using tempPaint.
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000824 SkMatrix translate;
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000825 translate.setTranslate(offset.fX, offset.fY);
826 am.set(context, translate);
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000827 context->drawPath(tempPaint, devPath, stroke);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000828
829 // If we're doing a normal blur, we can clobber the pathTexture in the
830 // gaussianBlur. Otherwise, we need to save it for later compositing.
831 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
skia.committer@gmail.comdc3a4e52012-10-02 02:01:24 +0000832 blurTexture.reset(context->gaussianBlur(pathTexture, isNormalBlur,
robertphillips@google.comccb39502012-10-01 18:25:13 +0000833 srcRect, sigma, sigma));
robertphillips@google.com7d126752012-10-19 12:56:26 +0000834 if (NULL == blurTexture) {
835 return false;
836 }
robertphillips@google.comccb39502012-10-01 18:25:13 +0000837
838 if (!isNormalBlur) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000839 context->setIdentityMatrix();
robertphillips@google.comccb39502012-10-01 18:25:13 +0000840 GrPaint paint;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000841 SkMatrix matrix;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000842 matrix.setIDiv(pathTexture->width(), pathTexture->height());
robertphillips@google.comccb39502012-10-01 18:25:13 +0000843 // Blend pathTexture over blurTexture.
844 context->setRenderTarget(blurTexture->asRenderTarget());
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000845 paint.colorStage(0)->setEffect(
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000846 GrSimpleTextureEffect::Create(pathTexture, matrix))->unref();
robertphillips@google.comccb39502012-10-01 18:25:13 +0000847 if (SkMaskFilter::kInner_BlurType == blurType) {
848 // inner: dst = dst * src
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000849 paint.setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000850 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
851 // solid: dst = src + dst - src * dst
852 // = (1 - dst) * src + 1 * dst
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000853 paint.setBlendFunc(kIDC_GrBlendCoeff, kOne_GrBlendCoeff);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000854 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
855 // outer: dst = dst * (1 - src)
856 // = 0 * src + (1 - src) * dst
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000857 paint.setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000858 }
859 context->drawRect(paint, srcRect);
860 }
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000861 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000862
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000863 GrContext::AutoMatrix am;
864 if (!am.setIdentity(context, grp)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000865 return false;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000866 }
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +0000867
bsalomon@google.com88becf42012-10-05 14:54:42 +0000868 static const int MASK_IDX = GrPaint::kMaxCoverageStages - 1;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000869 // we assume the last mask index is available for use
bsalomon@google.com88becf42012-10-05 14:54:42 +0000870 GrAssert(!grp->isCoverageStageEnabled(MASK_IDX));
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000871
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000872 SkMatrix matrix;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000873 matrix.setTranslate(-finalRect.fLeft, -finalRect.fTop);
874 matrix.postIDiv(blurTexture->width(), blurTexture->height());
875
bsalomon@google.com08283af2012-10-26 13:01:20 +0000876 grp->coverageStage(MASK_IDX)->reset();
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000877 grp->coverageStage(MASK_IDX)->setEffect(
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000878 GrSimpleTextureEffect::Create(blurTexture, matrix))->unref();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000879 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000880 return true;
881}
882
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000883bool drawWithMaskFilter(GrContext* context, const SkPath& devPath,
884 SkMaskFilter* filter, const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000885 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000886 SkMask srcM, dstM;
887
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000888 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
889 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
reed@google.com69302852011-02-16 18:08:07 +0000890 return false;
891 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000892 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000893
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000894 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
reed@google.com69302852011-02-16 18:08:07 +0000895 return false;
896 }
897 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000898 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000899
900 if (clip.quickReject(dstM.fBounds)) {
901 return false;
902 }
903 if (bounder && !bounder->doIRect(dstM.fBounds)) {
904 return false;
905 }
906
907 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000908 // the current clip (and identity matrix) and GrPaint settings
909 GrContext::AutoMatrix am;
910 am.setIdentity(context, grp);
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000911
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000912 GrTextureDesc desc;
913 desc.fWidth = dstM.fBounds.width();
914 desc.fHeight = dstM.fBounds.height();
915 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +0000916
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000917 GrAutoScratchTexture ast(context, desc);
918 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000919
reed@google.com69302852011-02-16 18:08:07 +0000920 if (NULL == texture) {
921 return false;
922 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000923 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000924 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000925
bsalomon@google.com88becf42012-10-05 14:54:42 +0000926 static const int MASK_IDX = GrPaint::kMaxCoverageStages - 1;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000927 // we assume the last mask index is available for use
bsalomon@google.com88becf42012-10-05 14:54:42 +0000928 GrAssert(!grp->isCoverageStageEnabled(MASK_IDX));
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000929
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000930 SkMatrix m;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000931 m.setTranslate(-dstM.fBounds.fLeft*SK_Scalar1, -dstM.fBounds.fTop*SK_Scalar1);
932 m.postIDiv(texture->width(), texture->height());
933
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000934 grp->coverageStage(MASK_IDX)->setEffect(GrSimpleTextureEffect::Create(texture, m))->unref();
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000935 GrRect d;
bsalomon@google.com81712882012-11-01 17:12:34 +0000936 d.setLTRB(SkIntToScalar(dstM.fBounds.fLeft),
937 SkIntToScalar(dstM.fBounds.fTop),
938 SkIntToScalar(dstM.fBounds.fRight),
939 SkIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000940
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000941 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000942 return true;
943}
reed@google.com69302852011-02-16 18:08:07 +0000944
bsalomon@google.com85003222012-03-28 14:44:37 +0000945}
946
947///////////////////////////////////////////////////////////////////////////////
948
reed@google.com0c219b62011-02-16 21:31:18 +0000949void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000950 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000951 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000952 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000953 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000954
bsalomon@google.com5782d712011-01-21 21:03:59 +0000955 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000956 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000957 return;
958 }
959
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000960 // can we cheat, and treat a thin stroke as a hairline w/ coverage
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000961 // if we can, we draw lots faster (raster device does this same test)
962 SkScalar hairlineCoverage;
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000963 bool doHairLine = SkDrawTreatAsHairline(paint, fContext->getMatrix(), &hairlineCoverage);
964 if (doHairLine) {
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000965 grPaint.setCoverage(SkScalarRoundToInt(hairlineCoverage * grPaint.getCoverage()));
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000966 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000967
reed@google.comfe626382011-09-21 13:50:35 +0000968 // If we have a prematrix, apply it to the path, optimizing for the case
969 // where the original path can in fact be modified in place (even though
970 // its parameter type is const).
971 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000972 SkPath tmpPath, effectPath;
reed@google.comac10a2d2010-12-22 21:39:39 +0000973
974 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +0000975 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +0000976
reed@google.come3445642011-02-16 23:20:39 +0000977 if (!pathIsMutable) {
978 result = &tmpPath;
979 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000980 }
reed@google.come3445642011-02-16 23:20:39 +0000981 // should I push prePathMatrix on our MV stack temporarily, instead
982 // of applying it here? See SkDraw.cpp
983 pathPtr->transform(*prePathMatrix, result);
984 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +0000985 }
reed@google.com0c219b62011-02-16 21:31:18 +0000986 // at this point we're done with prePathMatrix
987 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +0000988
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000989 SkStrokeRec stroke(paint);
990 SkPathEffect* pathEffect = paint.getPathEffect();
reed@google.com4bbdeac2013-01-24 21:03:11 +0000991 const SkRect* cullRect = NULL; // TODO: what is our bounds?
992 if (pathEffect && pathEffect->filterPath(&effectPath, *pathPtr, &stroke,
993 cullRect)) {
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000994 pathPtr = &effectPath;
995 }
996
997 if (!pathEffect && doHairLine) {
998 stroke.setHairlineStyle();
reed@google.com0c219b62011-02-16 21:31:18 +0000999 }
1000
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001001 if (paint.getMaskFilter()) {
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001002 if (!stroke.isHairlineStyle()) {
1003 if (stroke.applyToPath(&tmpPath, *pathPtr)) {
1004 pathPtr = &tmpPath;
1005 stroke.setFillStyle();
1006 }
1007 }
1008
reed@google.com0c219b62011-02-16 21:31:18 +00001009 // avoid possibly allocating a new path in transform if we can
1010 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1011
1012 // transform the path into device space
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001013 pathPtr->transform(fContext->getMatrix(), devPathPtr);
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001014 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, stroke, paint.getMaskFilter(),
sugoi@google.com12b4e272012-12-06 20:13:11 +00001015 *draw.fClip, draw.fBounder, &grPaint)) {
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001016 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
1017 SkPaint::kFill_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001018 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001019 *draw.fClip, draw.fBounder, &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001020 }
reed@google.com69302852011-02-16 18:08:07 +00001021 return;
1022 }
reed@google.com69302852011-02-16 18:08:07 +00001023
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001024 fContext->drawPath(grPaint, *pathPtr, stroke);
reed@google.comac10a2d2010-12-22 21:39:39 +00001025}
1026
bsalomon@google.comfb309512011-11-30 14:13:48 +00001027namespace {
1028
1029inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1030 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1031 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1032 return tilesX * tilesY;
1033}
1034
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001035inline int determine_tile_size(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001036 const SkRect& src,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001037 int maxTextureSize) {
1038 static const int kSmallTileSize = 1 << 10;
1039 if (maxTextureSize <= kSmallTileSize) {
1040 return maxTextureSize;
1041 }
1042
1043 size_t maxTexTotalTileSize;
1044 size_t smallTotalTileSize;
1045
robertphillips@google.combac6b052012-09-28 18:06:49 +00001046 SkIRect iSrc;
1047 src.roundOut(&iSrc);
1048
1049 maxTexTotalTileSize = get_tile_count(iSrc.fLeft,
1050 iSrc.fTop,
1051 iSrc.fRight,
1052 iSrc.fBottom,
1053 maxTextureSize);
1054 smallTotalTileSize = get_tile_count(iSrc.fLeft,
1055 iSrc.fTop,
1056 iSrc.fRight,
1057 iSrc.fBottom,
1058 kSmallTileSize);
1059
bsalomon@google.comfb309512011-11-30 14:13:48 +00001060 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1061 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1062
1063 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1064 return kSmallTileSize;
1065 } else {
1066 return maxTextureSize;
1067 }
1068}
1069}
1070
1071bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001072 const GrTextureParams& params,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001073 const SkRect* srcRectPtr) const {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001074 // if bitmap is explictly texture backed then just use the texture
1075 if (NULL != bitmap.getTexture()) {
1076 return false;
1077 }
1078 // if it's larger than the max texture size, then we have no choice but
1079 // tiling
1080 const int maxTextureSize = fContext->getMaxTextureSize();
1081 if (bitmap.width() > maxTextureSize ||
1082 bitmap.height() > maxTextureSize) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001083 return true;
1084 }
1085 // if we are going to have to draw the whole thing, then don't tile
1086 if (NULL == srcRectPtr) {
1087 return false;
1088 }
1089 // if the entire texture is already in our cache then no reason to tile it
bsalomon@google.com0797c2c2012-12-20 15:13:01 +00001090 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001091 return false;
1092 }
1093
1094 // At this point we know we could do the draw by uploading the entire bitmap
1095 // as a texture. However, if the texture would be large compared to the
1096 // cache size and we don't require most of it for this draw then tile to
1097 // reduce the amount of upload and cache spill.
1098
1099 // assumption here is that sw bitmap size is a good proxy for its size as
1100 // a texture
1101 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001102 size_t cacheSize;
1103 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001104 if (bmpSize < cacheSize / 2) {
1105 return false;
1106 }
1107
robertphillips@google.combac6b052012-09-28 18:06:49 +00001108 SkScalar fracUsed = SkScalarMul(srcRectPtr->width() / bitmap.width(),
1109 srcRectPtr->height() / bitmap.height());
1110 if (fracUsed <= SK_ScalarHalf) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001111 return true;
1112 } else {
1113 return false;
1114 }
1115}
1116
reed@google.comac10a2d2010-12-22 21:39:39 +00001117void SkGpuDevice::drawBitmap(const SkDraw& draw,
1118 const SkBitmap& bitmap,
1119 const SkIRect* srcRectPtr,
1120 const SkMatrix& m,
1121 const SkPaint& paint) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001122
1123 SkRect tmp;
1124 SkRect* tmpPtr = NULL;
1125
1126 // convert from SkIRect to SkRect
1127 if (NULL != srcRectPtr) {
1128 tmp.set(*srcRectPtr);
1129 tmpPtr = &tmp;
1130 }
1131
1132 // We cannot call drawBitmapRect here since 'm' could be anything
1133 this->drawBitmapCommon(draw, bitmap, tmpPtr, m, paint);
1134}
1135
1136void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1137 const SkBitmap& bitmap,
1138 const SkRect* srcRectPtr,
1139 const SkMatrix& m,
1140 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001141 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001142
robertphillips@google.combac6b052012-09-28 18:06:49 +00001143 SkRect srcRect;
reed@google.comac10a2d2010-12-22 21:39:39 +00001144 if (NULL == srcRectPtr) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001145 srcRect.set(0, 0, SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001146 } else {
1147 srcRect = *srcRectPtr;
1148 }
1149
junov@google.comd935cfb2011-06-27 20:48:23 +00001150 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001151 // Convert the bitmap to a shader so that the rect can be drawn
1152 // through drawRect, which supports mask filters.
robertphillips@google.combac6b052012-09-28 18:06:49 +00001153 SkMatrix newM(m);
junov@google.com1d329782011-07-28 20:10:09 +00001154 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001155 const SkBitmap* bitmapPtr = &bitmap;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001156 if (NULL != srcRectPtr) {
1157 SkIRect iSrc;
1158 srcRect.roundOut(&iSrc);
1159 if (!bitmap.extractSubset(&tmp, iSrc)) {
epoger@google.com9ef2d832011-07-01 21:12:20 +00001160 return; // extraction failed
1161 }
1162 bitmapPtr = &tmp;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001163 srcRect.offset(SkIntToScalar(-iSrc.fLeft), SkIntToScalar(-iSrc.fTop));
1164 // The source rect has changed so update the matrix
1165 newM.preTranslate(SkIntToScalar(iSrc.fLeft), SkIntToScalar(iSrc.fTop));
junov@google.comd935cfb2011-06-27 20:48:23 +00001166 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001167
junov@google.comd935cfb2011-06-27 20:48:23 +00001168 SkPaint paintWithTexture(paint);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001169 paintWithTexture.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
junov@google.comd935cfb2011-06-27 20:48:23 +00001170 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001171
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001172 // Transform 'newM' needs to be concatenated to the current matrix,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001173 // rather than transforming the primitive directly, so that 'newM' will
junov@google.com1d329782011-07-28 20:10:09 +00001174 // also affect the behavior of the mask filter.
1175 SkMatrix drawMatrix;
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001176 drawMatrix.setConcat(fContext->getMatrix(), newM);
junov@google.com1d329782011-07-28 20:10:09 +00001177 SkDraw transformedDraw(draw);
1178 transformedDraw.fMatrix = &drawMatrix;
1179
robertphillips@google.combac6b052012-09-28 18:06:49 +00001180 this->drawRect(transformedDraw, srcRect, paintWithTexture);
junov@google.com1d329782011-07-28 20:10:09 +00001181
junov@google.comd935cfb2011-06-27 20:48:23 +00001182 return;
1183 }
1184
bsalomon@google.com5782d712011-01-21 21:03:59 +00001185 GrPaint grPaint;
skia.committer@gmail.com4e73aa12013-01-09 02:01:30 +00001186
humper@google.com9aaf36d2013-01-08 16:08:01 +00001187 bool alphaOnly = !(SkBitmap::kA8_Config == bitmap.config());
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001188 if (!skPaint2GrPaintNoShader(this, paint, alphaOnly, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001189 return;
1190 }
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001191 GrTextureParams params;
1192 params.setBilerp(paint.isFilterBitmap());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001193
robertphillips@google.combac6b052012-09-28 18:06:49 +00001194 if (!this->shouldTileBitmap(bitmap, params, srcRectPtr)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001195 // take the simple case
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001196 this->internalDrawBitmap(bitmap, srcRect, m, params, &grPaint);
robertphillips@google.combac6b052012-09-28 18:06:49 +00001197 } else {
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001198 this->drawTiledBitmap(bitmap, srcRect, m, params, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001199 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001200}
1201
1202// Break 'bitmap' into several tiles to draw it since it has already
1203// been determined to be too large to fit in VRAM
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001204void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001205 const SkRect& srcRect,
1206 const SkMatrix& m,
1207 const GrTextureParams& params,
1208 GrPaint* grPaint) {
1209 const int maxTextureSize = fContext->getMaxTextureSize();
1210
1211 int tileSize = determine_tile_size(bitmap, srcRect, maxTextureSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001212
reed@google.comac10a2d2010-12-22 21:39:39 +00001213 // compute clip bounds in local coordinates
robertphillips@google.combac6b052012-09-28 18:06:49 +00001214 SkRect clipRect;
reed@google.comac10a2d2010-12-22 21:39:39 +00001215 {
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001216 const GrRenderTarget* rt = fContext->getRenderTarget();
1217 clipRect.setWH(SkIntToScalar(rt->width()), SkIntToScalar(rt->height()));
1218 if (!fContext->getClip()->fClipStack->intersectRectWithClip(&clipRect)) {
1219 return;
1220 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001221 SkMatrix matrix, inverse;
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001222 matrix.setConcat(fContext->getMatrix(), m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001223 if (!matrix.invert(&inverse)) {
1224 return;
1225 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001226 inverse.mapRect(&clipRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001227 }
1228
bsalomon@google.comfb309512011-11-30 14:13:48 +00001229 int nx = bitmap.width() / tileSize;
1230 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001231 for (int x = 0; x <= nx; x++) {
1232 for (int y = 0; y <= ny; y++) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001233 SkRect tileR;
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001234 tileR.set(SkIntToScalar(x * tileSize),
robertphillips@google.combac6b052012-09-28 18:06:49 +00001235 SkIntToScalar(y * tileSize),
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001236 SkIntToScalar((x + 1) * tileSize),
robertphillips@google.combac6b052012-09-28 18:06:49 +00001237 SkIntToScalar((y + 1) * tileSize));
1238
1239 if (!SkRect::Intersects(tileR, clipRect)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001240 continue;
1241 }
1242
robertphillips@google.combac6b052012-09-28 18:06:49 +00001243 if (!tileR.intersect(srcRect)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001244 continue;
1245 }
1246
1247 SkBitmap tmpB;
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001248 SkIRect iTileR;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001249 tileR.roundOut(&iTileR);
1250 if (bitmap.extractSubset(&tmpB, iTileR)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001251 // now offset it to make it "local" to our tmp bitmap
robertphillips@google.combac6b052012-09-28 18:06:49 +00001252 tileR.offset(SkIntToScalar(-iTileR.fLeft), SkIntToScalar(-iTileR.fTop));
reed@google.comac10a2d2010-12-22 21:39:39 +00001253 SkMatrix tmpM(m);
skia.committer@gmail.comdc3a4e52012-10-02 02:01:24 +00001254 tmpM.preTranslate(SkIntToScalar(iTileR.fLeft),
robertphillips@google.comffad46b2012-10-01 14:32:51 +00001255 SkIntToScalar(iTileR.fTop));
1256
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001257 this->internalDrawBitmap(tmpB, tileR, tmpM, params, grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001258 }
1259 }
1260 }
1261}
1262
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001263namespace {
1264
1265bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1266 // detect pixel disalignment
1267 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1268 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
rmistry@google.comd6176b02012-08-23 18:14:13 +00001269 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001270 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1271 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1272 COLOR_BLEED_TOLERANCE &&
1273 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1274 COLOR_BLEED_TOLERANCE) {
1275 return true;
1276 }
1277 return false;
1278}
1279
1280bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1281 const SkMatrix& m) {
1282 // Only gets called if hasAlignedSamples returned false.
1283 // So we can assume that sampling is axis aligned but not texel aligned.
1284 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001285 SkRect innerSrcRect(srcRect), innerTransformedRect,
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001286 outerTransformedRect(transformedRect);
1287 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1288 m.mapRect(&innerTransformedRect, innerSrcRect);
1289
1290 // The gap between outerTransformedRect and innerTransformedRect
1291 // represents the projection of the source border area, which is
1292 // problematic for color bleeding. We must check whether any
1293 // destination pixels sample the border area.
1294 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1295 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1296 SkIRect outer, inner;
1297 outerTransformedRect.round(&outer);
1298 innerTransformedRect.round(&inner);
1299 // If the inner and outer rects round to the same result, it means the
1300 // border does not overlap any pixel centers. Yay!
1301 return inner != outer;
1302}
1303
1304} // unnamed namespace
1305
reed@google.comac10a2d2010-12-22 21:39:39 +00001306/*
1307 * This is called by drawBitmap(), which has to handle images that may be too
1308 * large to be represented by a single texture.
1309 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001310 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1311 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001312 */
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001313void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001314 const SkRect& srcRect,
reed@google.comac10a2d2010-12-22 21:39:39 +00001315 const SkMatrix& m,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001316 const GrTextureParams& params,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001317 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001318 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1319 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001320
reed@google.com9c49bc32011-07-07 13:42:37 +00001321 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001322 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001323 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001324 return;
1325 }
1326
reed@google.comac10a2d2010-12-22 21:39:39 +00001327 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001328 SkAutoCachedTexture act(this, bitmap, &params, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001329 if (NULL == texture) {
1330 return;
1331 }
1332
robertphillips@google.combac6b052012-09-28 18:06:49 +00001333 GrRect dstRect(srcRect);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001334 GrRect paintRect;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001335 SkScalar wInv = SkScalarInvert(SkIntToScalar(bitmap.width()));
1336 SkScalar hInv = SkScalarInvert(SkIntToScalar(bitmap.height()));
1337 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1338 SkScalarMul(srcRect.fTop, hInv),
1339 SkScalarMul(srcRect.fRight, wInv),
1340 SkScalarMul(srcRect.fBottom, hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001341
rmistry@google.comd6176b02012-08-23 18:14:13 +00001342 bool needsTextureDomain = false;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001343 if (params.isBilerp()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001344 // Need texture domain if drawing a sub rect.
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001345 needsTextureDomain = srcRect.width() < bitmap.width() ||
robertphillips@google.combac6b052012-09-28 18:06:49 +00001346 srcRect.height() < bitmap.height();
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001347 if (m.rectStaysRect() && fContext->getMatrix().rectStaysRect()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001348 // sampling is axis-aligned
robertphillips@google.combac6b052012-09-28 18:06:49 +00001349 GrRect transformedRect;
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001350 SkMatrix srcToDeviceMatrix(m);
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001351 srcToDeviceMatrix.postConcat(fContext->getMatrix());
robertphillips@google.combac6b052012-09-28 18:06:49 +00001352 srcToDeviceMatrix.mapRect(&transformedRect, srcRect);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001353
robertphillips@google.combac6b052012-09-28 18:06:49 +00001354 if (hasAlignedSamples(srcRect, transformedRect)) {
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001355 // We could also turn off filtering here (but we already did a cache lookup with
1356 // params).
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001357 needsTextureDomain = false;
1358 } else {
1359 needsTextureDomain = needsTextureDomain &&
robertphillips@google.combac6b052012-09-28 18:06:49 +00001360 mayColorBleed(srcRect, transformedRect, m);
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001361 }
1362 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001363 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001364
1365 GrRect textureDomain = GrRect::MakeEmpty();
bsalomon@google.com0ac6af42013-01-16 15:16:18 +00001366 SkAutoTUnref<GrEffectRef> effect;
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001367 if (needsTextureDomain) {
1368 // Use a constrained texture domain to avoid color bleeding
bsalomon@google.com81712882012-11-01 17:12:34 +00001369 SkScalar left, top, right, bottom;
1370 if (srcRect.width() > SK_Scalar1) {
1371 SkScalar border = SK_ScalarHalf / bitmap.width();
junov@google.com6acc9b32011-05-16 18:32:07 +00001372 left = paintRect.left() + border;
1373 right = paintRect.right() - border;
1374 } else {
bsalomon@google.com81712882012-11-01 17:12:34 +00001375 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
junov@google.com6acc9b32011-05-16 18:32:07 +00001376 }
bsalomon@google.com81712882012-11-01 17:12:34 +00001377 if (srcRect.height() > SK_Scalar1) {
1378 SkScalar border = SK_ScalarHalf / bitmap.height();
junov@google.com6acc9b32011-05-16 18:32:07 +00001379 top = paintRect.top() + border;
1380 bottom = paintRect.bottom() - border;
1381 } else {
bsalomon@google.com81712882012-11-01 17:12:34 +00001382 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
junov@google.com6acc9b32011-05-16 18:32:07 +00001383 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001384 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +00001385 effect.reset(GrTextureDomainEffect::Create(texture,
1386 SkMatrix::I(),
1387 textureDomain,
1388 GrTextureDomainEffect::kClamp_WrapMode,
1389 params.isBilerp()));
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001390 } else {
bsalomon@google.com68b58c92013-01-17 16:50:08 +00001391 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
junov@google.com6acc9b32011-05-16 18:32:07 +00001392 }
bsalomon@google.com73818dc2013-03-28 13:23:29 +00001393 grPaint->colorStage(kBitmapEffectIdx)->setEffect(effect);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001394 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001395}
1396
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001397namespace {
1398
bsalomon@google.comf271cc72012-10-24 19:35:13 +00001399void apply_effect(GrContext* context,
1400 GrTexture* srcTexture,
1401 GrTexture* dstTexture,
1402 const GrRect& rect,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +00001403 GrEffectRef* effect) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001404 SkASSERT(srcTexture && srcTexture->getContext() == context);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001405 GrContext::AutoMatrix am;
1406 am.setIdentity(context);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001407 GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget());
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001408 GrContext::AutoClip acs(context, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001409
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001410 GrPaint paint;
bsalomon@google.comdbe49f72012-11-05 16:36:02 +00001411 paint.colorStage(0)->setEffect(effect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001412 context->drawRect(paint, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001413}
1414
1415};
1416
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001417static SkBitmap wrap_texture(GrTexture* texture) {
1418 SkBitmap result;
1419 bool dummy;
1420 SkBitmap::Config config = grConfig2skConfig(texture->config(), &dummy);
1421 result.setConfig(config, texture->width(), texture->height());
1422 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (texture)))->unref();
1423 return result;
1424}
1425
1426static bool filter_texture(SkDevice* device, GrContext* context,
1427 GrTexture* texture, SkImageFilter* filter,
1428 int w, int h, SkBitmap* result) {
reed@google.com8926b162012-03-23 15:36:36 +00001429 GrAssert(filter);
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001430 SkDeviceImageFilterProxy proxy(device);
reed@google.com8926b162012-03-23 15:36:36 +00001431
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001432 GrTextureDesc desc;
1433 desc.fFlags = kRenderTarget_GrTextureFlagBit,
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001434 desc.fWidth = w;
1435 desc.fHeight = h;
bsalomon@google.com0342a852012-08-20 19:22:38 +00001436 desc.fConfig = kRGBA_8888_GrPixelConfig;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +00001437 GrEffectRef* effect;
reed@google.com8926b162012-03-23 15:36:36 +00001438
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001439 if (filter->canFilterImageGPU()) {
senorblanco@chromium.org985fa792012-10-24 15:14:26 +00001440 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1441 // filter. Also set the clip wide open and the matrix to identity.
1442 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001443 return filter->filterImageGPU(&proxy, wrap_texture(texture), result);
bsalomon@google.com021fc732012-10-25 12:47:42 +00001444 } else if (filter->asNewEffect(&effect, texture)) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001445 GrAutoScratchTexture dst(context, desc);
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001446 SkRect r = SkRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h));
1447 apply_effect(context, texture, dst.texture(), r, effect);
1448 SkAutoTUnref<GrTexture> resultTex(dst.detach());
bsalomon@google.com021fc732012-10-25 12:47:42 +00001449 effect->unref();
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001450 *result = wrap_texture(resultTex.get());
1451 return true;
1452 } else {
1453 return false;
reed@google.com8926b162012-03-23 15:36:36 +00001454 }
reed@google.com8926b162012-03-23 15:36:36 +00001455}
1456
reed@google.comac10a2d2010-12-22 21:39:39 +00001457void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001458 int left, int top, const SkPaint& paint) {
1459 // drawSprite is defined to be in device coords.
1460 CHECK_SHOULD_DRAW(draw, true);
reed@google.comac10a2d2010-12-22 21:39:39 +00001461
reed@google.com8926b162012-03-23 15:36:36 +00001462 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001463 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1464 return;
1465 }
1466
reed@google.com76dd2772012-01-05 21:15:07 +00001467 int w = bitmap.width();
1468 int h = bitmap.height();
1469
bsalomon@google.com5782d712011-01-21 21:03:59 +00001470 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001471 if(!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001472 return;
1473 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001474
bsalomon@google.com73818dc2013-03-28 13:23:29 +00001475 GrEffectStage* stage = grPaint.colorStage(kBitmapEffectIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001476
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001477 GrTexture* texture;
bsalomon@google.com08283af2012-10-26 13:01:20 +00001478 stage->reset();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001479 // draw sprite uses the default texture params
1480 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
bsalomon@google.com73818dc2013-03-28 13:23:29 +00001481 grPaint.colorStage(kBitmapEffectIdx)->setEffect(
bsalomon@google.com68b58c92013-01-17 16:50:08 +00001482 GrSimpleTextureEffect::Create(texture, SkMatrix::I()))->unref();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001483
reed@google.com8926b162012-03-23 15:36:36 +00001484 SkImageFilter* filter = paint.getImageFilter();
1485 if (NULL != filter) {
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001486 SkBitmap filterBitmap;
1487 if (filter_texture(this, fContext, texture, filter, w, h, &filterBitmap)) {
bsalomon@google.com73818dc2013-03-28 13:23:29 +00001488 grPaint.colorStage(kBitmapEffectIdx)->setEffect(
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001489 GrSimpleTextureEffect::Create((GrTexture*) filterBitmap.getTexture(), SkMatrix::I()))->unref();
1490 texture = (GrTexture*) filterBitmap.getTexture();
1491 w = filterBitmap.width();
1492 h = filterBitmap.height();
reed@google.com8926b162012-03-23 15:36:36 +00001493 }
reed@google.com76dd2772012-01-05 21:15:07 +00001494 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001495
bsalomon@google.com5782d712011-01-21 21:03:59 +00001496 fContext->drawRectToRect(grPaint,
bsalomon@google.com81712882012-11-01 17:12:34 +00001497 GrRect::MakeXYWH(SkIntToScalar(left),
1498 SkIntToScalar(top),
1499 SkIntToScalar(w),
1500 SkIntToScalar(h)),
1501 GrRect::MakeWH(SK_Scalar1 * w / texture->width(),
1502 SK_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001503}
1504
reed@google.com33535f32012-09-25 15:37:50 +00001505void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1506 const SkRect* src, const SkRect& dst,
1507 const SkPaint& paint) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001508 SkMatrix matrix;
1509 SkRect bitmapBounds, tmpSrc;
1510
1511 bitmapBounds.set(0, 0,
1512 SkIntToScalar(bitmap.width()),
1513 SkIntToScalar(bitmap.height()));
1514
reed@google.com33535f32012-09-25 15:37:50 +00001515 // Compute matrix from the two rectangles
robertphillips@google.combac6b052012-09-28 18:06:49 +00001516 if (NULL != src) {
1517 tmpSrc = *src;
1518 } else {
1519 tmpSrc = bitmapBounds;
1520 }
1521 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1522
1523 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1524 if (NULL != src) {
1525 if (!bitmapBounds.contains(tmpSrc)) {
1526 if (!tmpSrc.intersect(bitmapBounds)) {
1527 return; // nothing to draw
reed@google.com33535f32012-09-25 15:37:50 +00001528 }
reed@google.com33535f32012-09-25 15:37:50 +00001529 }
reed@google.com33535f32012-09-25 15:37:50 +00001530 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +00001531
robertphillips@google.combac6b052012-09-28 18:06:49 +00001532 this->drawBitmapCommon(draw, bitmap, &tmpSrc, matrix, paint);
reed@google.com33535f32012-09-25 15:37:50 +00001533}
1534
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001535void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001536 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001537 // clear of the source device must occur before CHECK_SHOULD_DRAW
1538 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1539 if (dev->fNeedClear) {
1540 // TODO: could check here whether we really need to draw at all
1541 dev->clear(0x0);
1542 }
1543
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001544 // drawDevice is defined to be in device coords.
1545 CHECK_SHOULD_DRAW(draw, true);
reed@google.comac10a2d2010-12-22 21:39:39 +00001546
bsalomon@google.com5782d712011-01-21 21:03:59 +00001547 GrPaint grPaint;
bsalomon@google.com73818dc2013-03-28 13:23:29 +00001548 grPaint.colorStage(kBitmapEffectIdx)->reset();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001549 if (!dev->bindDeviceAsTexture(&grPaint) ||
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001550 !skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001551 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001552 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001553
bsalomon@google.com73818dc2013-03-28 13:23:29 +00001554 GrTexture* devTex = (*grPaint.getColorStage(kBitmapEffectIdx).getEffect())->texture(0);
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001555 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001556
senorblanco@chromium.org514b9222013-01-18 21:53:12 +00001557 const SkBitmap& bm = dev->accessBitmap(false);
1558 int w = bm.width();
1559 int h = bm.height();
1560
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001561 SkImageFilter* filter = paint.getImageFilter();
1562 if (NULL != filter) {
1563 SkBitmap filterBitmap;
1564 if (filter_texture(this, fContext, devTex, filter, w, h, &filterBitmap)) {
bsalomon@google.com73818dc2013-03-28 13:23:29 +00001565 grPaint.colorStage(kBitmapEffectIdx)->setEffect(
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001566 GrSimpleTextureEffect::Create((GrTexture*) filterBitmap.getTexture(), SkMatrix::I()))->unref();
1567 devTex = (GrTexture*) filterBitmap.getTexture();
1568 w = filterBitmap.width();
1569 h = filterBitmap.height();
1570 }
1571 }
1572
bsalomon@google.com81712882012-11-01 17:12:34 +00001573 GrRect dstRect = GrRect::MakeXYWH(SkIntToScalar(x),
1574 SkIntToScalar(y),
1575 SkIntToScalar(w),
1576 SkIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001577
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001578 // The device being drawn may not fill up its texture (saveLayer uses
1579 // the approximate ).
bsalomon@google.com81712882012-11-01 17:12:34 +00001580 GrRect srcRect = GrRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1581 SK_Scalar1 * h / devTex->height());
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001582
1583 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001584}
1585
reed@google.com8926b162012-03-23 15:36:36 +00001586bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
bsalomon@google.com8ea78d82012-10-24 20:11:30 +00001587 if (!filter->asNewEffect(NULL, NULL) &&
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001588 !filter->canFilterImageGPU()) {
reed@google.com76dd2772012-01-05 21:15:07 +00001589 return false;
1590 }
reed@google.com8926b162012-03-23 15:36:36 +00001591 return true;
1592}
1593
1594bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1595 const SkMatrix& ctm,
1596 SkBitmap* result, SkIPoint* offset) {
1597 // want explicitly our impl, so guard against a subclass of us overriding it
1598 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001599 return false;
1600 }
reed@google.com8926b162012-03-23 15:36:36 +00001601
1602 SkAutoLockPixels alp(src, !src.getTexture());
1603 if (!src.getTexture() && !src.readyToDraw()) {
1604 return false;
1605 }
1606
1607 GrPaint paint;
reed@google.com8926b162012-03-23 15:36:36 +00001608
reed@google.com8926b162012-03-23 15:36:36 +00001609 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001610 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1611 // must be pushed upstack.
1612 SkAutoCachedTexture act(this, src, NULL, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001613
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001614 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), result);
reed@google.com76dd2772012-01-05 21:15:07 +00001615}
1616
reed@google.comac10a2d2010-12-22 21:39:39 +00001617///////////////////////////////////////////////////////////////////////////////
1618
1619// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001620static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001621 kTriangles_GrPrimitiveType,
1622 kTriangleStrip_GrPrimitiveType,
1623 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001624};
1625
1626void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1627 int vertexCount, const SkPoint vertices[],
1628 const SkPoint texs[], const SkColor colors[],
1629 SkXfermode* xmode,
1630 const uint16_t indices[], int indexCount,
1631 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001632 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001633
bsalomon@google.com5782d712011-01-21 21:03:59 +00001634 GrPaint grPaint;
bsalomon@google.com5782d712011-01-21 21:03:59 +00001635 // we ignore the shader if texs is null.
1636 if (NULL == texs) {
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001637 if (!skPaint2GrPaintNoShader(this, paint, false, NULL == colors, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001638 return;
1639 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001640 } else {
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001641 if (!skPaint2GrPaintShader(this, paint, NULL == colors, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001642 return;
1643 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001644 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001645
1646 if (NULL != xmode && NULL != texs && NULL != colors) {
reed@google.com8d3cd7a2013-01-30 21:36:11 +00001647 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001648 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1649#if 0
1650 return
1651#endif
1652 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001653 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001654
bsalomon@google.com498776a2011-08-16 19:20:44 +00001655 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1656 if (NULL != colors) {
1657 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001658 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001659 for (int i = 0; i < vertexCount; ++i) {
rileya@google.com24f3ad12012-07-18 21:47:40 +00001660 convertedColors[i] = SkColor2GrColor(colors[i]);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001661 }
1662 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001663 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001664 fContext->drawVertices(grPaint,
1665 gVertexMode2PrimitiveType[vmode],
1666 vertexCount,
1667 (GrPoint*) vertices,
1668 (GrPoint*) texs,
1669 colors,
1670 indices,
1671 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001672}
1673
1674///////////////////////////////////////////////////////////////////////////////
1675
1676static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001677 GrFontScaler* scaler = (GrFontScaler*)data;
1678 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001679}
1680
1681static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1682 void* auxData;
1683 GrFontScaler* scaler = NULL;
1684 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1685 scaler = (GrFontScaler*)auxData;
1686 }
1687 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001688 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001689 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1690 }
1691 return scaler;
1692}
1693
1694static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1695 SkFixed fx, SkFixed fy,
1696 const SkGlyph& glyph) {
1697 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1698
bungeman@google.com15865a72012-01-11 16:28:04 +00001699 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001700
1701 if (NULL == procs->fFontScaler) {
1702 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1703 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001704
bungeman@google.com15865a72012-01-11 16:28:04 +00001705 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1706 glyph.getSubXFixed(),
1707 glyph.getSubYFixed()),
1708 SkFixedFloorToFixed(fx),
1709 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001710 procs->fFontScaler);
1711}
1712
bsalomon@google.com5782d712011-01-21 21:03:59 +00001713SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001714
1715 // deferred allocation
1716 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001717 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001718 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1719 fDrawProcs->fContext = fContext;
1720 }
1721
1722 // init our (and GL's) state
1723 fDrawProcs->fTextContext = context;
1724 fDrawProcs->fFontScaler = NULL;
1725 return fDrawProcs;
1726}
1727
1728void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1729 size_t byteLength, SkScalar x, SkScalar y,
1730 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001731 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001732
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001733 if (fContext->getMatrix().hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001734 // this guy will just call our drawPath()
1735 draw.drawText((const char*)text, byteLength, x, y, paint);
1736 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001737 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001738
1739 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001740 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001741 return;
1742 }
bsalomon@google.com0e354aa2012-10-08 20:44:25 +00001743 GrTextContext context(fContext, grPaint);
tomhudson@google.com375ff852012-06-29 18:37:57 +00001744 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001745 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1746 }
1747}
1748
1749void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1750 size_t byteLength, const SkScalar pos[],
1751 SkScalar constY, int scalarsPerPos,
1752 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001753 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001754
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001755 if (fContext->getMatrix().hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001756 // this guy will just call our drawPath()
1757 draw.drawPosText((const char*)text, byteLength, pos, constY,
1758 scalarsPerPos, paint);
1759 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001760 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001761
1762 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001763 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001764 return;
1765 }
bsalomon@google.com0e354aa2012-10-08 20:44:25 +00001766 GrTextContext context(fContext, grPaint);
tomhudson@google.com375ff852012-06-29 18:37:57 +00001767 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001768 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1769 scalarsPerPos, paint);
1770 }
1771}
1772
1773void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1774 size_t len, const SkPath& path,
1775 const SkMatrix* m, const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001776 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001777
1778 SkASSERT(draw.fDevice == this);
1779 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1780}
1781
1782///////////////////////////////////////////////////////////////////////////////
1783
reed@google.comf67e4cf2011-03-15 20:56:58 +00001784bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1785 if (!paint.isLCDRenderText()) {
1786 // we're cool with the paint as is
1787 return false;
1788 }
1789
1790 if (paint.getShader() ||
1791 paint.getXfermode() || // unless its srcover
1792 paint.getMaskFilter() ||
1793 paint.getRasterizer() ||
1794 paint.getColorFilter() ||
1795 paint.getPathEffect() ||
1796 paint.isFakeBoldText() ||
1797 paint.getStyle() != SkPaint::kFill_Style) {
1798 // turn off lcd
1799 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1800 flags->fHinting = paint.getHinting();
1801 return true;
1802 }
1803 // we're cool with the paint as is
1804 return false;
1805}
1806
reed@google.com75d939b2011-12-07 15:07:23 +00001807void SkGpuDevice::flush() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +00001808 DO_DEFERRED_CLEAR();
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001809 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001810}
1811
reed@google.comf67e4cf2011-03-15 20:56:58 +00001812///////////////////////////////////////////////////////////////////////////////
1813
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001814SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1815 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001816 bool isOpaque,
1817 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001818 GrTextureDesc desc;
1819 desc.fConfig = fRenderTarget->config();
1820 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1821 desc.fWidth = width;
1822 desc.fHeight = height;
1823 desc.fSampleCnt = fRenderTarget->numSamples();
1824
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001825 SkAutoTUnref<GrTexture> texture;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001826 // Skia's convention is to only clear a device if it is non-opaque.
1827 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001828
1829#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1830 // layers are never draw in repeat modes, so we can request an approx
1831 // match and ignore any padding.
bsalomon@google.com0797c2c2012-12-20 15:13:01 +00001832 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1833 GrContext::kApprox_ScratchTexMatch :
1834 GrContext::kExact_ScratchTexMatch;
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001835 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001836#else
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001837 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001838#endif
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001839 if (NULL != texture.get()) {
1840 return SkNEW_ARGS(SkGpuDevice,(fContext, texture, needClear));
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001841 } else {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001842 GrPrintf("---- failed to create compatible device texture [%d %d]\n", width, height);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001843 return NULL;
1844 }
1845}
1846
1847SkGpuDevice::SkGpuDevice(GrContext* context,
1848 GrTexture* texture,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001849 bool needClear)
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001850 : SkDevice(make_bitmap(context, texture->asRenderTarget())) {
1851
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001852 GrAssert(texture && texture->asRenderTarget());
bsalomon@google.com8090e652012-08-28 15:07:11 +00001853 // This constructor is called from onCreateCompatibleDevice. It has locked the RT in the texture
1854 // cache. We pass true for the third argument so that it will get unlocked.
1855 this->initFromRenderTarget(context, texture->asRenderTarget(), true);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001856 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001857}