blob: 837ff8c245474b3bef7d376b26fb6fdeab994557 [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"
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +000024#include "SkRRect.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000025#include "SkStroke.h"
reed@google.comc9aa5872011-04-05 21:05:37 +000026#include "SkUtils.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000027
bsalomon@google.com06cd7322012-03-30 18:45:35 +000028#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
reed@google.comac10a2d2010-12-22 21:39:39 +000029
30#if 0
31 extern bool (*gShouldDrawProc)();
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +000032 #define CHECK_SHOULD_DRAW(draw, forceI) \
reed@google.comac10a2d2010-12-22 21:39:39 +000033 do { \
34 if (gShouldDrawProc && !gShouldDrawProc()) return; \
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +000035 this->prepareDraw(draw, forceI); \
reed@google.comac10a2d2010-12-22 21:39:39 +000036 } while (0)
37#else
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +000038 #define CHECK_SHOULD_DRAW(draw, forceI) this->prepareDraw(draw, forceI)
reed@google.comac10a2d2010-12-22 21:39:39 +000039#endif
40
bsalomon@google.com73818dc2013-03-28 13:23:29 +000041// we use the same effect slot on GrPaint for bitmaps and shaders (since drawBitmap, drawSprite,
42// and drawDevice ignore SkShader)
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000043enum {
bsalomon@google.com73818dc2013-03-28 13:23:29 +000044 kShaderEffectIdx = 0,
45 kBitmapEffectIdx = 0,
46 kColorFilterEffectIdx = 1,
47 kXfermodeEffectIdx = 2,
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000048};
49
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +000050#define MAX_BLUR_SIGMA 4.0f
51// FIXME: This value comes from from SkBlurMaskFilter.cpp.
52// Should probably be put in a common header someplace.
53#define MAX_BLUR_RADIUS SkIntToScalar(128)
54// This constant approximates the scaling done in the software path's
55// "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
56// IMHO, it actually should be 1: we blur "less" than we should do
57// according to the CSS and canvas specs, simply because Safari does the same.
58// Firefox used to do the same too, until 4.0 where they fixed it. So at some
59// point we should probably get rid of these scaling constants and rebaseline
60// all the blur tests.
61#define BLUR_SIGMA_SCALE 0.6f
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000062// This constant represents the screen alignment criterion in texels for
rmistry@google.comd6176b02012-08-23 18:14:13 +000063// requiring texture domain clamping to prevent color bleeding when drawing
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000064// a sub region of a larger source image.
65#define COLOR_BLEED_TOLERANCE SkFloatToScalar(0.001f)
bsalomon@google.com06cd7322012-03-30 18:45:35 +000066
junov@google.comdbfac8a2012-12-06 21:47:40 +000067#define DO_DEFERRED_CLEAR() \
68 do { \
69 if (fNeedClear) { \
70 this->clear(SK_ColorTRANSPARENT); \
71 } \
72 } while (false) \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000073
reed@google.comac10a2d2010-12-22 21:39:39 +000074///////////////////////////////////////////////////////////////////////////////
75
reed@google.comb0a34d82012-07-11 19:57:55 +000076#define CHECK_FOR_NODRAW_ANNOTATION(paint) \
77 do { if (paint.isNoDrawAnnotation()) { return; } } while (0)
78
79///////////////////////////////////////////////////////////////////////////////
80
81
bsalomon@google.com84405e02012-03-05 19:57:21 +000082class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
83public:
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000084 SkAutoCachedTexture()
85 : fDevice(NULL)
86 , fTexture(NULL) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000087 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000088
bsalomon@google.com84405e02012-03-05 19:57:21 +000089 SkAutoCachedTexture(SkGpuDevice* device,
90 const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +000091 const GrTextureParams* params,
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000092 GrTexture** texture)
93 : fDevice(NULL)
94 , fTexture(NULL) {
95 GrAssert(NULL != texture);
bsalomon@google.comb8670992012-07-25 21:27:09 +000096 *texture = this->set(device, bitmap, params);
reed@google.comac10a2d2010-12-22 21:39:39 +000097 }
reed@google.comac10a2d2010-12-22 21:39:39 +000098
bsalomon@google.com84405e02012-03-05 19:57:21 +000099 ~SkAutoCachedTexture() {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000100 if (NULL != fTexture) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000101 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
bsalomon@google.com84405e02012-03-05 19:57:21 +0000102 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000103 }
bsalomon@google.com84405e02012-03-05 19:57:21 +0000104
105 GrTexture* set(SkGpuDevice* device,
106 const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000107 const GrTextureParams* params) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000108 if (NULL != fTexture) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000109 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000110 fTexture = NULL;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000111 }
112 fDevice = device;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000113 GrTexture* result = (GrTexture*)bitmap.getTexture();
114 if (NULL == result) {
115 // Cannot return the native texture so look it up in our cache
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000116 fTexture = GrLockAndRefCachedBitmapTexture(device->context(), bitmap, params);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000117 result = fTexture;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000118 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000119 return result;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000120 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000121
bsalomon@google.com84405e02012-03-05 19:57:21 +0000122private:
123 SkGpuDevice* fDevice;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000124 GrTexture* fTexture;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000125};
reed@google.comac10a2d2010-12-22 21:39:39 +0000126
127///////////////////////////////////////////////////////////////////////////////
128
reed@google.comac10a2d2010-12-22 21:39:39 +0000129struct GrSkDrawProcs : public SkDrawProcs {
130public:
131 GrContext* fContext;
132 GrTextContext* fTextContext;
133 GrFontScaler* fFontScaler; // cached in the skia glyphcache
134};
135
136///////////////////////////////////////////////////////////////////////////////
137
reed@google.comaf951c92011-06-16 19:10:39 +0000138static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
139 switch (config) {
140 case kAlpha_8_GrPixelConfig:
141 *isOpaque = false;
142 return SkBitmap::kA8_Config;
143 case kRGB_565_GrPixelConfig:
144 *isOpaque = true;
145 return SkBitmap::kRGB_565_Config;
146 case kRGBA_4444_GrPixelConfig:
147 *isOpaque = false;
148 return SkBitmap::kARGB_4444_Config;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000149 case kSkia8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +0000150 // we don't currently have a way of knowing whether
151 // a 8888 is opaque based on the config.
152 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000153 return SkBitmap::kARGB_8888_Config;
154 default:
155 *isOpaque = false;
156 return SkBitmap::kNo_Config;
157 }
158}
reed@google.comac10a2d2010-12-22 21:39:39 +0000159
reed@google.comaf951c92011-06-16 19:10:39 +0000160static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000161 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000162
163 bool isOpaque;
164 SkBitmap bitmap;
165 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
166 renderTarget->width(), renderTarget->height());
167 bitmap.setIsOpaque(isOpaque);
168 return bitmap;
169}
170
bsalomon@google.com123ac1d2013-03-28 19:18:12 +0000171SkGpuDevice* SkGpuDevice::Create(GrSurface* surface) {
172 GrAssert(NULL != surface);
173 if (NULL == surface->asRenderTarget() || NULL == surface->getContext()) {
174 return NULL;
175 }
176 if (surface->asTexture()) {
177 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asTexture()));
178 } else {
179 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asRenderTarget()));
180 }
181}
182
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000183SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000184: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
bsalomon@google.com8090e652012-08-28 15:07:11 +0000185 this->initFromRenderTarget(context, texture->asRenderTarget(), false);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000186}
187
reed@google.comaf951c92011-06-16 19:10:39 +0000188SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000189: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.com8090e652012-08-28 15:07:11 +0000190 this->initFromRenderTarget(context, renderTarget, false);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000191}
192
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000193void SkGpuDevice::initFromRenderTarget(GrContext* context,
bsalomon@google.com8090e652012-08-28 15:07:11 +0000194 GrRenderTarget* renderTarget,
195 bool cached) {
reed@google.comaf951c92011-06-16 19:10:39 +0000196 fDrawProcs = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000197
reed@google.comaf951c92011-06-16 19:10:39 +0000198 fContext = context;
199 fContext->ref();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000200
reed@google.comaf951c92011-06-16 19:10:39 +0000201 fRenderTarget = NULL;
202 fNeedClear = false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000203
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000204 GrAssert(NULL != renderTarget);
205 fRenderTarget = renderTarget;
206 fRenderTarget->ref();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000207
bsalomon@google.coma2921122012-08-28 12:34:17 +0000208 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
209 // on the RT but not vice-versa.
210 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
211 // busting chrome (for a currently unknown reason).
212 GrSurface* surface = fRenderTarget->asTexture();
213 if (NULL == surface) {
214 surface = fRenderTarget;
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000215 }
bsalomon@google.com8090e652012-08-28 15:07:11 +0000216 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (surface, cached));
bsalomon@google.coma2921122012-08-28 12:34:17 +0000217
reed@google.comaf951c92011-06-16 19:10:39 +0000218 this->setPixelRef(pr, 0)->unref();
219}
220
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000221SkGpuDevice::SkGpuDevice(GrContext* context,
222 SkBitmap::Config config,
223 int width,
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000224 int height,
225 int sampleCount)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000226 : SkDevice(config, width, height, false /*isOpaque*/) {
227
reed@google.comac10a2d2010-12-22 21:39:39 +0000228 fDrawProcs = NULL;
229
reed@google.com7b201d22011-01-11 18:59:23 +0000230 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000231 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000232
reed@google.comac10a2d2010-12-22 21:39:39 +0000233 fRenderTarget = NULL;
234 fNeedClear = false;
235
reed@google.comaf951c92011-06-16 19:10:39 +0000236 if (config != SkBitmap::kRGB_565_Config) {
237 config = SkBitmap::kARGB_8888_Config;
238 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000239
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000240 GrTextureDesc desc;
241 desc.fFlags = kRenderTarget_GrTextureFlagBit;
242 desc.fWidth = width;
243 desc.fHeight = height;
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000244 desc.fConfig = SkBitmapConfig2GrPixelConfig(config);
245 desc.fSampleCnt = sampleCount;
reed@google.comac10a2d2010-12-22 21:39:39 +0000246
bsalomon@google.coma2921122012-08-28 12:34:17 +0000247 SkAutoTUnref<GrTexture> texture(fContext->createUncachedTexture(desc, NULL, 0));
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000248
bsalomon@google.coma2921122012-08-28 12:34:17 +0000249 if (NULL != texture) {
250 fRenderTarget = texture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000251 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000252
reed@google.comaf951c92011-06-16 19:10:39 +0000253 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000254
reed@google.comaf951c92011-06-16 19:10:39 +0000255 // wrap the bitmap with a pixelref to expose our texture
bsalomon@google.coma2921122012-08-28 12:34:17 +0000256 SkGrPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (texture));
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000257 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000258 } else {
259 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
260 width, height);
261 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000262 }
263}
264
265SkGpuDevice::~SkGpuDevice() {
266 if (fDrawProcs) {
267 delete fDrawProcs;
268 }
269
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000270 // The GrContext takes a ref on the target. We don't want to cause the render
271 // target to be unnecessarily kept alive.
272 if (fContext->getRenderTarget() == fRenderTarget) {
273 fContext->setRenderTarget(NULL);
274 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000275
robertphillips@google.com055f9082012-10-24 13:24:11 +0000276 if (fContext->getClip() == &fClipData) {
277 fContext->setClip(NULL);
278 }
279
bsalomon@google.coma2921122012-08-28 12:34:17 +0000280 SkSafeUnref(fRenderTarget);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000281 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000282}
283
reed@google.comac10a2d2010-12-22 21:39:39 +0000284///////////////////////////////////////////////////////////////////////////////
285
286void SkGpuDevice::makeRenderTargetCurrent() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000287 DO_DEFERRED_CLEAR();
reed@google.comac10a2d2010-12-22 21:39:39 +0000288 fContext->setRenderTarget(fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000289}
290
291///////////////////////////////////////////////////////////////////////////////
292
bsalomon@google.comc4364992011-11-07 15:54:49 +0000293namespace {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000294GrPixelConfig config8888_to_grconfig_and_flags(SkCanvas::Config8888 config8888, uint32_t* flags) {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000295 switch (config8888) {
296 case SkCanvas::kNative_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000297 *flags = 0;
298 return kSkia8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000299 case SkCanvas::kNative_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000300 *flags = GrContext::kUnpremul_PixelOpsFlag;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000301 return kSkia8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000302 case SkCanvas::kBGRA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000303 *flags = 0;
304 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000305 case SkCanvas::kBGRA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000306 *flags = GrContext::kUnpremul_PixelOpsFlag;
307 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000308 case SkCanvas::kRGBA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000309 *flags = 0;
310 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000311 case SkCanvas::kRGBA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000312 *flags = GrContext::kUnpremul_PixelOpsFlag;
313 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000314 default:
315 GrCrash("Unexpected Config8888.");
bsalomon@google.comccaa0022012-09-25 19:55:07 +0000316 *flags = 0; // suppress warning
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000317 return kSkia8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000318 }
319}
320}
321
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000322bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
323 int x, int y,
324 SkCanvas::Config8888 config8888) {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000325 DO_DEFERRED_CLEAR();
bsalomon@google.com910267d2011-11-02 20:06:25 +0000326 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
327 SkASSERT(!bitmap.isNull());
328 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000329
bsalomon@google.com910267d2011-11-02 20:06:25 +0000330 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000331 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000332 uint32_t flags;
333 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000334 return fContext->readRenderTargetPixels(fRenderTarget,
335 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000336 bitmap.width(),
337 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000338 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000339 bitmap.getPixels(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000340 bitmap.rowBytes(),
341 flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000342}
343
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000344void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
345 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000346 SkAutoLockPixels alp(bitmap);
347 if (!bitmap.readyToDraw()) {
348 return;
349 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000350
351 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000352 uint32_t flags;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000353 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000354 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000355 } else {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000356 flags = 0;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000357 config= SkBitmapConfig2GrPixelConfig(bitmap.config());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000358 }
359
bsalomon@google.com6f379512011-11-16 20:36:03 +0000360 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000361 config, bitmap.getPixels(), bitmap.rowBytes(), flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000362}
363
robertphillips@google.com46f93502012-08-07 15:38:08 +0000364namespace {
humper@google.com05af1af2013-01-07 16:47:43 +0000365void purgeClipCB(int genID, void* ) {
robertphillips@google.com46f93502012-08-07 15:38:08 +0000366
367 if (SkClipStack::kInvalidGenID == genID ||
368 SkClipStack::kEmptyGenID == genID ||
369 SkClipStack::kWideOpenGenID == genID) {
370 // none of these cases will have a cached clip mask
371 return;
372 }
373
374}
375};
376
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000377void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
378 INHERITED::onAttachToCanvas(canvas);
379
380 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000381 fClipData.fClipStack = canvas->getClipStack();
robertphillips@google.com46f93502012-08-07 15:38:08 +0000382
383 fClipData.fClipStack->addPurgeClipCallback(purgeClipCB, fContext);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000384}
385
386void SkGpuDevice::onDetachFromCanvas() {
387 INHERITED::onDetachFromCanvas();
388
robertphillips@google.com46f93502012-08-07 15:38:08 +0000389 // TODO: iterate through the clip stack and clean up any cached clip masks
390 fClipData.fClipStack->removePurgeClipCallback(purgeClipCB, fContext);
391
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000392 fClipData.fClipStack = NULL;
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000393}
394
robertphillips@google.com607fe072012-07-24 13:54:00 +0000395#ifdef SK_DEBUG
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000396static void check_bounds(const GrClipData& clipData,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000397 const SkRegion& clipRegion,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000398 int renderTargetWidth,
399 int renderTargetHeight) {
400
robertphillips@google.com7b112892012-07-31 15:18:21 +0000401 SkIRect devBound;
402
403 devBound.setLTRB(0, 0, renderTargetWidth, renderTargetHeight);
404
robertphillips@google.com607fe072012-07-24 13:54:00 +0000405 SkClipStack::BoundsType boundType;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000406 SkRect canvTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000407
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000408 clipData.fClipStack->getBounds(&canvTemp, &boundType);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000409 if (SkClipStack::kNormal_BoundsType == boundType) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000410 SkIRect devTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000411
robertphillips@google.com7b112892012-07-31 15:18:21 +0000412 canvTemp.roundOut(&devTemp);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000413
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000414 devTemp.offset(-clipData.fOrigin.fX, -clipData.fOrigin.fY);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000415
robertphillips@google.com7b112892012-07-31 15:18:21 +0000416 if (!devBound.intersect(devTemp)) {
417 devBound.setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000418 }
419 }
420
robertphillips@google.com768fee82012-08-02 12:42:43 +0000421 GrAssert(devBound.contains(clipRegion.getBounds()));
robertphillips@google.com607fe072012-07-24 13:54:00 +0000422}
423#endif
424
reed@google.comac10a2d2010-12-22 21:39:39 +0000425///////////////////////////////////////////////////////////////////////////////
426
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000427// call this every draw call, to ensure that the context reflects our state,
reed@google.comac10a2d2010-12-22 21:39:39 +0000428// and not the state from some other canvas/device
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000429void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000430 GrAssert(NULL != fClipData.fClipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000431
reed@google.comac10a2d2010-12-22 21:39:39 +0000432 fContext->setRenderTarget(fRenderTarget);
433
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000434 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000435
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000436 if (forceIdentity) {
437 fContext->setIdentityMatrix();
438 } else {
439 fContext->setMatrix(*draw.fMatrix);
440 }
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000441 fClipData.fOrigin = this->getOrigin();
reed@google.comac10a2d2010-12-22 21:39:39 +0000442
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000443#ifdef SK_DEBUG
444 check_bounds(fClipData, *draw.fClip, fRenderTarget->width(), fRenderTarget->height());
445#endif
446
447 fContext->setClip(&fClipData);
448
449 DO_DEFERRED_CLEAR();
reed@google.comac10a2d2010-12-22 21:39:39 +0000450}
451
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000452SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000453 DO_DEFERRED_CLEAR();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000454 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000455}
456
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000457bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
bsalomon@google.coma2921122012-08-28 12:34:17 +0000458 GrTexture* texture = fRenderTarget->asTexture();
459 if (NULL != texture) {
bsalomon@google.com73818dc2013-03-28 13:23:29 +0000460 paint->colorStage(kBitmapEffectIdx)->setEffect(
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000461 GrSimpleTextureEffect::Create(texture, SkMatrix::I()))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000462 return true;
463 }
464 return false;
465}
466
467///////////////////////////////////////////////////////////////////////////////
468
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000469SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
470SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
471SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
472SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
473SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
474 shader_type_mismatch);
rileya@google.com3e332582012-07-03 13:43:35 +0000475SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
476 shader_type_mismatch);
rileya@google.com22e57f92012-07-19 15:16:19 +0000477SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
478SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000479
bsalomon@google.com84405e02012-03-05 19:57:21 +0000480namespace {
481
482// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
483// justAlpha indicates that skPaint's alpha should be used rather than the color
484// Callers may subsequently modify the GrPaint. Setting constantColor indicates
485// that the final paint will draw the same color at every pixel. This allows
486// an optimization where the the color filter can be applied to the skPaint's
twiz@google.com58071162012-07-18 21:41:50 +0000487// color once while converting to GrPaint and then ignored.
488inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
489 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000490 bool justAlpha,
491 bool constantColor,
492 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000493
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000494 grPaint->setDither(skPaint.isDither());
495 grPaint->setAntiAlias(skPaint.isAntiAlias());
bsalomon@google.com5782d712011-01-21 21:03:59 +0000496
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000497 SkXfermode::Coeff sm;
498 SkXfermode::Coeff dm;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000499
500 SkXfermode* mode = skPaint.getXfermode();
bsalomon@google.comdb446252013-03-27 18:46:16 +0000501 GrEffectRef* xferEffect = NULL;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000502 if (SkXfermode::AsNewEffectOrCoeff(mode, dev->context(), &xferEffect, &sm, &dm)) {
503 if (NULL != xferEffect) {
504 grPaint->colorStage(kXfermodeEffectIdx)->setEffect(xferEffect)->unref();
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000505 sm = SkXfermode::kOne_Coeff;
bsalomon@google.com5920ac22013-04-19 13:14:45 +0000506 dm = SkXfermode::kZero_Coeff;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000507 }
bsalomon@google.comf51c0132013-03-27 18:31:15 +0000508 } else {
509 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000510#if 0
bsalomon@google.comf51c0132013-03-27 18:31:15 +0000511 return false;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000512#else
513 // Fall back to src-over
514 sm = SkXfermode::kOne_Coeff;
515 dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000516#endif
bsalomon@google.com5782d712011-01-21 21:03:59 +0000517 }
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000518 grPaint->setBlendFunc(sk_blend_to_grblend(sm), sk_blend_to_grblend(dm));
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000519
bsalomon@google.com5782d712011-01-21 21:03:59 +0000520 if (justAlpha) {
521 uint8_t alpha = skPaint.getAlpha();
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000522 grPaint->setColor(GrColorPackRGBA(alpha, alpha, alpha, alpha));
Scroggod757df22011-05-16 13:11:16 +0000523 // justAlpha is currently set to true only if there is a texture,
524 // so constantColor should not also be true.
525 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000526 } else {
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000527 grPaint->setColor(SkColor2GrColor(skPaint.getColor()));
bsalomon@google.com73818dc2013-03-28 13:23:29 +0000528 GrAssert(!grPaint->isColorStageEnabled(kShaderEffectIdx));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000529 }
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000530
Scroggo97c88c22011-05-11 14:05:25 +0000531 SkColorFilter* colorFilter = skPaint.getColorFilter();
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000532 if (NULL != colorFilter) {
533 // if the source color is a constant then apply the filter here once rather than per pixel
534 // in a shader.
535 if (constantColor) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000536 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000537 grPaint->setColor(SkColor2GrColor(filtered));
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000538 } else {
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000539 SkAutoTUnref<GrEffectRef> effect(colorFilter->asNewEffect(dev->context()));
bsalomon@google.com021fc732012-10-25 12:47:42 +0000540 if (NULL != effect.get()) {
bsalomon@google.com73818dc2013-03-28 13:23:29 +0000541 grPaint->colorStage(kColorFilterEffectIdx)->setEffect(effect);
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000542 } else {
bsalomon@google.com8ea78d82012-10-24 20:11:30 +0000543 // TODO: rewrite this using asNewEffect()
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000544 SkColor color;
545 SkXfermode::Mode filterMode;
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000546 if (colorFilter->asColorMode(&color, &filterMode)) {
547 grPaint->setXfermodeColorFilter(filterMode, SkColor2GrColor(color));
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000548 }
549 }
Scroggod757df22011-05-16 13:11:16 +0000550 }
Scroggo97c88c22011-05-11 14:05:25 +0000551 }
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000552
bsalomon@google.com5782d712011-01-21 21:03:59 +0000553 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000554}
555
bsalomon@google.com84405e02012-03-05 19:57:21 +0000556// This function is similar to skPaint2GrPaintNoShader but also converts
bsalomon@google.com08283af2012-10-26 13:01:20 +0000557// skPaint's shader to a GrTexture/GrEffectStage if possible. The texture to
bsalomon@google.com84405e02012-03-05 19:57:21 +0000558// be used is set on grPaint and returned in param act. constantColor has the
559// same meaning as in skPaint2GrPaintNoShader.
560inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
561 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000562 bool constantColor,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000563 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000564 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000565 if (NULL == shader) {
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000566 return skPaint2GrPaintNoShader(dev, skPaint, false, constantColor, grPaint);
567 } else if (!skPaint2GrPaintNoShader(dev, skPaint, true, false, grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000568 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000569 }
570
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000571 SkAutoTUnref<GrEffectRef> effect(shader->asNewEffect(dev->context(), skPaint));
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000572 if (NULL != effect.get()) {
bsalomon@google.com73818dc2013-03-28 13:23:29 +0000573 grPaint->colorStage(kShaderEffectIdx)->setEffect(effect);
rileya@google.com91f319c2012-07-25 17:18:31 +0000574 return true;
575 }
576
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000577 // We still don't have SkColorShader::asNewEffect() implemented.
578 SkShader::GradientInfo info;
579 SkColor color;
reed@google.comac10a2d2010-12-22 21:39:39 +0000580
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000581 info.fColors = &color;
582 info.fColorOffsets = NULL;
583 info.fColorCount = 1;
584 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
585 SkPaint copy(skPaint);
586 copy.setShader(NULL);
587 // modulate the paint alpha by the shader's solid color alpha
588 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
589 copy.setColor(SkColorSetA(color, newA));
590 return skPaint2GrPaintNoShader(dev, copy, false, constantColor, grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000591 }
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000592 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000593}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000594}
reed@google.comac10a2d2010-12-22 21:39:39 +0000595
596///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000597void SkGpuDevice::clear(SkColor color) {
bsalomon@google.com32cf29e2013-01-25 15:03:18 +0000598 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
599 fContext->clear(&rect, SkColor2GrColor(color), fRenderTarget);
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000600 fNeedClear = false;
bsalomon@google.com398109c2011-04-14 18:40:27 +0000601}
602
reed@google.comac10a2d2010-12-22 21:39:39 +0000603void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000604 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000605
bsalomon@google.com5782d712011-01-21 21:03:59 +0000606 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000607 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000608 return;
609 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000610
611 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000612}
613
614// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000615static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000616 kPoints_GrPrimitiveType,
617 kLines_GrPrimitiveType,
618 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000619};
620
621void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000622 size_t count, const SkPoint pts[], const SkPaint& paint) {
epoger@google.comb58772f2013-03-08 09:09:10 +0000623 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000624 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000625
626 SkScalar width = paint.getStrokeWidth();
627 if (width < 0) {
628 return;
629 }
630
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000631 // we only handle hairlines and paints without path effects or mask filters,
632 // else we let the SkDraw call our drawPath()
633 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000634 draw.drawPoints(mode, count, pts, paint, true);
635 return;
636 }
637
bsalomon@google.com5782d712011-01-21 21:03:59 +0000638 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000639 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000640 return;
641 }
642
bsalomon@google.com5782d712011-01-21 21:03:59 +0000643 fContext->drawVertices(grPaint,
644 gPointMode2PrimtiveType[mode],
645 count,
646 (GrPoint*)pts,
647 NULL,
648 NULL,
649 NULL,
650 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000651}
652
reed@google.comc9aa5872011-04-05 21:05:37 +0000653///////////////////////////////////////////////////////////////////////////////
654
reed@google.comac10a2d2010-12-22 21:39:39 +0000655void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000656 const SkPaint& paint) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000657 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000658 CHECK_SHOULD_DRAW(draw, false);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000659
bungeman@google.com79bd8772011-07-18 15:34:08 +0000660 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000661 SkScalar width = paint.getStrokeWidth();
662
663 /*
664 We have special code for hairline strokes, miter-strokes, and fills.
665 Anything else we just call our path code.
666 */
667 bool usePath = doStroke && width > 0 &&
668 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000669 // another two reasons we might need to call drawPath...
670 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000671 usePath = true;
672 }
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000673 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000674#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000675 if (doStroke) {
676#endif
677 usePath = true;
robertphillips@google.com4b140b52013-05-02 17:13:13 +0000678#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
robertphillips@google.comdf3695e2013-04-09 14:01:44 +0000679 } else {
680 usePath = !fContext->getMatrix().preservesRightAngles();
681 }
682#endif
reed@google.com67db6642011-05-26 11:46:35 +0000683 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000684 // small miter limit means right angles show bevel...
685 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
686 paint.getStrokeMiter() < SK_ScalarSqrt2)
687 {
688 usePath = true;
689 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000690 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000691 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
692 usePath = true;
693 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000694
695 if (usePath) {
696 SkPath path;
697 path.addRect(rect);
698 this->drawPath(draw, path, paint, NULL, true);
699 return;
700 }
701
702 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000703 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000704 return;
705 }
reed@google.com20efde72011-05-09 17:00:02 +0000706 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000707}
708
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000709///////////////////////////////////////////////////////////////////////////////
710
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000711void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
712 const SkPaint& paint) {
713 CHECK_FOR_NODRAW_ANNOTATION(paint);
714 CHECK_SHOULD_DRAW(draw, false);
715
commit-bot@chromium.org37d883d2013-05-02 13:11:22 +0000716 bool usePath = !rect.isSimple();
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +0000717 // another two reasons we might need to call drawPath...
718 if (paint.getMaskFilter() || paint.getPathEffect()) {
719 usePath = true;
720 }
721 // until we can rotate rrects...
722 if (!usePath && !fContext->getMatrix().rectStaysRect()) {
723 usePath = true;
724 }
725
726 if (usePath) {
727 SkPath path;
728 path.addRRect(rect);
729 this->drawPath(draw, path, paint, NULL, true);
730 return;
731 }
732
733 GrPaint grPaint;
734 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
735 return;
736 }
737
738 SkStrokeRec stroke(paint);
739 fContext->drawRRect(grPaint, rect, stroke);
740}
741
742///////////////////////////////////////////////////////////////////////////////
743
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000744void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
745 const SkPaint& paint) {
746 CHECK_FOR_NODRAW_ANNOTATION(paint);
747 CHECK_SHOULD_DRAW(draw, false);
748
749 bool usePath = false;
750 // some basic reasons we might need to call drawPath...
751 if (paint.getMaskFilter() || paint.getPathEffect()) {
752 usePath = true;
753 }
754
755 if (usePath) {
756 SkPath path;
757 path.addOval(oval);
758 this->drawPath(draw, path, paint, NULL, true);
759 return;
760 }
761
762 GrPaint grPaint;
763 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
764 return;
765 }
766 SkStrokeRec stroke(paint);
767
768 fContext->drawOval(grPaint, oval, stroke);
769}
770
reed@google.com69302852011-02-16 18:08:07 +0000771#include "SkMaskFilter.h"
772#include "SkBounder.h"
773
bsalomon@google.com85003222012-03-28 14:44:37 +0000774///////////////////////////////////////////////////////////////////////////////
775
776// helpers for applying mask filters
777namespace {
778
bsalomon@google.com85003222012-03-28 14:44:37 +0000779// We prefer to blur small rect with small radius via CPU.
780#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
781#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
782inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
783 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
784 rect.height() <= MIN_GPU_BLUR_SIZE &&
785 radius <= MIN_GPU_BLUR_RADIUS) {
786 return true;
787 }
788 return false;
789}
790
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000791bool drawWithGPUMaskFilter(GrContext* context, const SkPath& devPath, const SkStrokeRec& stroke,
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000792 SkMaskFilter* filter, const SkRegion& clip,
sugoi@google.com12b4e272012-12-06 20:13:11 +0000793 SkBounder* bounder, GrPaint* grp) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000794 SkMaskFilter::BlurInfo info;
795 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000796 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000797 return false;
798 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000799 SkScalar radius = info.fIgnoreTransform ? info.fRadius
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000800 : context->getMatrix().mapRadius(info.fRadius);
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000801 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000802 if (radius <= 0) {
803 return false;
804 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000805
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000806 SkRect srcRect = devPath.getBounds();
bsalomon@google.com85003222012-03-28 14:44:37 +0000807 if (shouldDrawBlurWithCPU(srcRect, radius)) {
808 return false;
809 }
810
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000811 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000812 float sigma3 = sigma * 3.0f;
813
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000814 SkRect clipRect;
815 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000816
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000817 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000818 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
819 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000820 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000821 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000822 SkIRect finalIRect;
823 finalRect.roundOut(&finalIRect);
824 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000825 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000826 }
827 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000828 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000829 }
830 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000831 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000832 GrTextureDesc desc;
833 desc.fFlags = kRenderTarget_GrTextureFlagBit;
834 desc.fWidth = SkScalarCeilToInt(srcRect.width());
835 desc.fHeight = SkScalarCeilToInt(srcRect.height());
836 // We actually only need A8, but it often isn't supported as a
837 // render target so default to RGBA_8888
bsalomon@google.com0342a852012-08-20 19:22:38 +0000838 desc.fConfig = kRGBA_8888_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000839
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000840 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
841 desc.fConfig = kAlpha_8_GrPixelConfig;
842 }
843
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000844 GrAutoScratchTexture pathEntry(context, desc);
845 GrTexture* pathTexture = pathEntry.texture();
846 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000847 return false;
848 }
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000849
robertphillips@google.comccb39502012-10-01 18:25:13 +0000850 SkAutoTUnref<GrTexture> blurTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000851
robertphillips@google.comccb39502012-10-01 18:25:13 +0000852 {
853 GrContext::AutoRenderTarget art(context, pathTexture->asRenderTarget());
854 GrContext::AutoClip ac(context, srcRect);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000855
robertphillips@google.comccb39502012-10-01 18:25:13 +0000856 context->clear(NULL, 0);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000857
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000858 GrPaint tempPaint;
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000859 if (grp->isAntiAlias()) {
860 tempPaint.setAntiAlias(true);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000861 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
862 // blend coeff of zero requires dual source blending support in order
863 // to properly blend partially covered pixels. This means the AA
864 // code path may not be taken. So we use a dst blend coeff of ISA. We
865 // could special case AA draws to a dst surface with known alpha=0 to
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000866 // use a zero dst coeff when dual source blending isn't available.f
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000867 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000868 }
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000869
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000870 GrContext::AutoMatrix am;
871
872 // Draw hard shadow to pathTexture with path top-left at origin using tempPaint.
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000873 SkMatrix translate;
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000874 translate.setTranslate(offset.fX, offset.fY);
875 am.set(context, translate);
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000876 context->drawPath(tempPaint, devPath, stroke);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000877
878 // If we're doing a normal blur, we can clobber the pathTexture in the
879 // gaussianBlur. Otherwise, we need to save it for later compositing.
880 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
skia.committer@gmail.comdc3a4e52012-10-02 02:01:24 +0000881 blurTexture.reset(context->gaussianBlur(pathTexture, isNormalBlur,
robertphillips@google.comccb39502012-10-01 18:25:13 +0000882 srcRect, sigma, sigma));
robertphillips@google.com7d126752012-10-19 12:56:26 +0000883 if (NULL == blurTexture) {
884 return false;
885 }
robertphillips@google.comccb39502012-10-01 18:25:13 +0000886
887 if (!isNormalBlur) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000888 context->setIdentityMatrix();
robertphillips@google.comccb39502012-10-01 18:25:13 +0000889 GrPaint paint;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000890 SkMatrix matrix;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000891 matrix.setIDiv(pathTexture->width(), pathTexture->height());
robertphillips@google.comccb39502012-10-01 18:25:13 +0000892 // Blend pathTexture over blurTexture.
893 context->setRenderTarget(blurTexture->asRenderTarget());
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000894 paint.colorStage(0)->setEffect(
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000895 GrSimpleTextureEffect::Create(pathTexture, matrix))->unref();
robertphillips@google.comccb39502012-10-01 18:25:13 +0000896 if (SkMaskFilter::kInner_BlurType == blurType) {
897 // inner: dst = dst * src
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000898 paint.setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000899 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
900 // solid: dst = src + dst - src * dst
901 // = (1 - dst) * src + 1 * dst
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000902 paint.setBlendFunc(kIDC_GrBlendCoeff, kOne_GrBlendCoeff);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000903 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
904 // outer: dst = dst * (1 - src)
905 // = 0 * src + (1 - src) * dst
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000906 paint.setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000907 }
908 context->drawRect(paint, srcRect);
909 }
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000910 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000911
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000912 GrContext::AutoMatrix am;
913 if (!am.setIdentity(context, grp)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000914 return false;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000915 }
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +0000916
bsalomon@google.com88becf42012-10-05 14:54:42 +0000917 static const int MASK_IDX = GrPaint::kMaxCoverageStages - 1;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000918 // we assume the last mask index is available for use
bsalomon@google.com88becf42012-10-05 14:54:42 +0000919 GrAssert(!grp->isCoverageStageEnabled(MASK_IDX));
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000920
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000921 SkMatrix matrix;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000922 matrix.setTranslate(-finalRect.fLeft, -finalRect.fTop);
923 matrix.postIDiv(blurTexture->width(), blurTexture->height());
924
bsalomon@google.com08283af2012-10-26 13:01:20 +0000925 grp->coverageStage(MASK_IDX)->reset();
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000926 grp->coverageStage(MASK_IDX)->setEffect(
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000927 GrSimpleTextureEffect::Create(blurTexture, matrix))->unref();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000928 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000929 return true;
930}
931
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000932bool drawWithMaskFilter(GrContext* context, const SkPath& devPath,
933 SkMaskFilter* filter, const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000934 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000935 SkMask srcM, dstM;
936
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000937 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
938 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
reed@google.com69302852011-02-16 18:08:07 +0000939 return false;
940 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000941 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000942
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000943 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
reed@google.com69302852011-02-16 18:08:07 +0000944 return false;
945 }
946 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000947 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000948
949 if (clip.quickReject(dstM.fBounds)) {
950 return false;
951 }
952 if (bounder && !bounder->doIRect(dstM.fBounds)) {
953 return false;
954 }
955
956 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000957 // the current clip (and identity matrix) and GrPaint settings
958 GrContext::AutoMatrix am;
959 am.setIdentity(context, grp);
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000960
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000961 GrTextureDesc desc;
962 desc.fWidth = dstM.fBounds.width();
963 desc.fHeight = dstM.fBounds.height();
964 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +0000965
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000966 GrAutoScratchTexture ast(context, desc);
967 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000968
reed@google.com69302852011-02-16 18:08:07 +0000969 if (NULL == texture) {
970 return false;
971 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000972 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000973 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000974
bsalomon@google.com88becf42012-10-05 14:54:42 +0000975 static const int MASK_IDX = GrPaint::kMaxCoverageStages - 1;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000976 // we assume the last mask index is available for use
bsalomon@google.com88becf42012-10-05 14:54:42 +0000977 GrAssert(!grp->isCoverageStageEnabled(MASK_IDX));
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000978
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000979 SkMatrix m;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000980 m.setTranslate(-dstM.fBounds.fLeft*SK_Scalar1, -dstM.fBounds.fTop*SK_Scalar1);
981 m.postIDiv(texture->width(), texture->height());
982
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000983 grp->coverageStage(MASK_IDX)->setEffect(GrSimpleTextureEffect::Create(texture, m))->unref();
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000984 GrRect d;
bsalomon@google.com81712882012-11-01 17:12:34 +0000985 d.setLTRB(SkIntToScalar(dstM.fBounds.fLeft),
986 SkIntToScalar(dstM.fBounds.fTop),
987 SkIntToScalar(dstM.fBounds.fRight),
988 SkIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000989
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000990 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000991 return true;
992}
reed@google.com69302852011-02-16 18:08:07 +0000993
bsalomon@google.com85003222012-03-28 14:44:37 +0000994}
995
996///////////////////////////////////////////////////////////////////////////////
997
reed@google.com0c219b62011-02-16 21:31:18 +0000998void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000999 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +00001000 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +00001001 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001002 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001003
bsalomon@google.com5782d712011-01-21 21:03:59 +00001004 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001005 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001006 return;
1007 }
1008
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001009 // can we cheat, and treat a thin stroke as a hairline w/ coverage
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +00001010 // if we can, we draw lots faster (raster device does this same test)
1011 SkScalar hairlineCoverage;
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001012 bool doHairLine = SkDrawTreatAsHairline(paint, fContext->getMatrix(), &hairlineCoverage);
1013 if (doHairLine) {
bsalomon@google.comc7448ce2012-10-05 19:04:13 +00001014 grPaint.setCoverage(SkScalarRoundToInt(hairlineCoverage * grPaint.getCoverage()));
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +00001015 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001016
reed@google.comfe626382011-09-21 13:50:35 +00001017 // If we have a prematrix, apply it to the path, optimizing for the case
1018 // where the original path can in fact be modified in place (even though
1019 // its parameter type is const).
1020 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001021 SkPath tmpPath, effectPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001022
1023 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001024 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001025
reed@google.come3445642011-02-16 23:20:39 +00001026 if (!pathIsMutable) {
1027 result = &tmpPath;
1028 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001029 }
reed@google.come3445642011-02-16 23:20:39 +00001030 // should I push prePathMatrix on our MV stack temporarily, instead
1031 // of applying it here? See SkDraw.cpp
1032 pathPtr->transform(*prePathMatrix, result);
1033 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001034 }
reed@google.com0c219b62011-02-16 21:31:18 +00001035 // at this point we're done with prePathMatrix
1036 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001037
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001038 SkStrokeRec stroke(paint);
1039 SkPathEffect* pathEffect = paint.getPathEffect();
reed@google.com4bbdeac2013-01-24 21:03:11 +00001040 const SkRect* cullRect = NULL; // TODO: what is our bounds?
1041 if (pathEffect && pathEffect->filterPath(&effectPath, *pathPtr, &stroke,
1042 cullRect)) {
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001043 pathPtr = &effectPath;
1044 }
1045
1046 if (!pathEffect && doHairLine) {
1047 stroke.setHairlineStyle();
reed@google.com0c219b62011-02-16 21:31:18 +00001048 }
1049
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001050 if (paint.getMaskFilter()) {
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001051 if (!stroke.isHairlineStyle()) {
1052 if (stroke.applyToPath(&tmpPath, *pathPtr)) {
1053 pathPtr = &tmpPath;
1054 stroke.setFillStyle();
1055 }
1056 }
1057
reed@google.com0c219b62011-02-16 21:31:18 +00001058 // avoid possibly allocating a new path in transform if we can
1059 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1060
1061 // transform the path into device space
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001062 pathPtr->transform(fContext->getMatrix(), devPathPtr);
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001063 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, stroke, paint.getMaskFilter(),
sugoi@google.com12b4e272012-12-06 20:13:11 +00001064 *draw.fClip, draw.fBounder, &grPaint)) {
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001065 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
1066 SkPaint::kFill_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001067 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001068 *draw.fClip, draw.fBounder, &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001069 }
reed@google.com69302852011-02-16 18:08:07 +00001070 return;
1071 }
reed@google.com69302852011-02-16 18:08:07 +00001072
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001073 fContext->drawPath(grPaint, *pathPtr, stroke);
reed@google.comac10a2d2010-12-22 21:39:39 +00001074}
1075
bsalomon@google.comfb309512011-11-30 14:13:48 +00001076namespace {
1077
1078inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1079 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1080 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1081 return tilesX * tilesY;
1082}
1083
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001084inline int determine_tile_size(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001085 const SkRect& src,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001086 int maxTextureSize) {
1087 static const int kSmallTileSize = 1 << 10;
1088 if (maxTextureSize <= kSmallTileSize) {
1089 return maxTextureSize;
1090 }
1091
1092 size_t maxTexTotalTileSize;
1093 size_t smallTotalTileSize;
1094
robertphillips@google.combac6b052012-09-28 18:06:49 +00001095 SkIRect iSrc;
1096 src.roundOut(&iSrc);
1097
1098 maxTexTotalTileSize = get_tile_count(iSrc.fLeft,
1099 iSrc.fTop,
1100 iSrc.fRight,
1101 iSrc.fBottom,
1102 maxTextureSize);
1103 smallTotalTileSize = get_tile_count(iSrc.fLeft,
1104 iSrc.fTop,
1105 iSrc.fRight,
1106 iSrc.fBottom,
1107 kSmallTileSize);
1108
bsalomon@google.comfb309512011-11-30 14:13:48 +00001109 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1110 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1111
1112 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1113 return kSmallTileSize;
1114 } else {
1115 return maxTextureSize;
1116 }
1117}
1118}
1119
1120bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001121 const GrTextureParams& params,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001122 const SkRect* srcRectPtr) const {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001123 // if bitmap is explictly texture backed then just use the texture
1124 if (NULL != bitmap.getTexture()) {
1125 return false;
1126 }
1127 // if it's larger than the max texture size, then we have no choice but
1128 // tiling
1129 const int maxTextureSize = fContext->getMaxTextureSize();
1130 if (bitmap.width() > maxTextureSize ||
1131 bitmap.height() > maxTextureSize) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001132 return true;
1133 }
1134 // if we are going to have to draw the whole thing, then don't tile
1135 if (NULL == srcRectPtr) {
1136 return false;
1137 }
1138 // if the entire texture is already in our cache then no reason to tile it
bsalomon@google.com0797c2c2012-12-20 15:13:01 +00001139 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001140 return false;
1141 }
1142
1143 // At this point we know we could do the draw by uploading the entire bitmap
1144 // as a texture. However, if the texture would be large compared to the
1145 // cache size and we don't require most of it for this draw then tile to
1146 // reduce the amount of upload and cache spill.
1147
1148 // assumption here is that sw bitmap size is a good proxy for its size as
1149 // a texture
1150 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001151 size_t cacheSize;
1152 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001153 if (bmpSize < cacheSize / 2) {
1154 return false;
1155 }
1156
robertphillips@google.combac6b052012-09-28 18:06:49 +00001157 SkScalar fracUsed = SkScalarMul(srcRectPtr->width() / bitmap.width(),
1158 srcRectPtr->height() / bitmap.height());
1159 if (fracUsed <= SK_ScalarHalf) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001160 return true;
1161 } else {
1162 return false;
1163 }
1164}
1165
reed@google.comac10a2d2010-12-22 21:39:39 +00001166void SkGpuDevice::drawBitmap(const SkDraw& draw,
1167 const SkBitmap& bitmap,
1168 const SkIRect* srcRectPtr,
1169 const SkMatrix& m,
1170 const SkPaint& paint) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001171
1172 SkRect tmp;
1173 SkRect* tmpPtr = NULL;
1174
1175 // convert from SkIRect to SkRect
1176 if (NULL != srcRectPtr) {
1177 tmp.set(*srcRectPtr);
1178 tmpPtr = &tmp;
1179 }
1180
1181 // We cannot call drawBitmapRect here since 'm' could be anything
1182 this->drawBitmapCommon(draw, bitmap, tmpPtr, m, paint);
1183}
1184
1185void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1186 const SkBitmap& bitmap,
1187 const SkRect* srcRectPtr,
1188 const SkMatrix& m,
1189 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001190 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001191
robertphillips@google.combac6b052012-09-28 18:06:49 +00001192 SkRect srcRect;
reed@google.comac10a2d2010-12-22 21:39:39 +00001193 if (NULL == srcRectPtr) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001194 srcRect.set(0, 0, SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001195 } else {
1196 srcRect = *srcRectPtr;
1197 }
1198
junov@google.comd935cfb2011-06-27 20:48:23 +00001199 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001200 // Convert the bitmap to a shader so that the rect can be drawn
1201 // through drawRect, which supports mask filters.
robertphillips@google.combac6b052012-09-28 18:06:49 +00001202 SkMatrix newM(m);
junov@google.com1d329782011-07-28 20:10:09 +00001203 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001204 const SkBitmap* bitmapPtr = &bitmap;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001205 if (NULL != srcRectPtr) {
1206 SkIRect iSrc;
1207 srcRect.roundOut(&iSrc);
1208 if (!bitmap.extractSubset(&tmp, iSrc)) {
epoger@google.com9ef2d832011-07-01 21:12:20 +00001209 return; // extraction failed
1210 }
1211 bitmapPtr = &tmp;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001212 srcRect.offset(SkIntToScalar(-iSrc.fLeft), SkIntToScalar(-iSrc.fTop));
1213 // The source rect has changed so update the matrix
1214 newM.preTranslate(SkIntToScalar(iSrc.fLeft), SkIntToScalar(iSrc.fTop));
junov@google.comd935cfb2011-06-27 20:48:23 +00001215 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001216
junov@google.comd935cfb2011-06-27 20:48:23 +00001217 SkPaint paintWithTexture(paint);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001218 paintWithTexture.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
junov@google.comd935cfb2011-06-27 20:48:23 +00001219 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001220
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001221 // Transform 'newM' needs to be concatenated to the current matrix,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001222 // rather than transforming the primitive directly, so that 'newM' will
junov@google.com1d329782011-07-28 20:10:09 +00001223 // also affect the behavior of the mask filter.
1224 SkMatrix drawMatrix;
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001225 drawMatrix.setConcat(fContext->getMatrix(), newM);
junov@google.com1d329782011-07-28 20:10:09 +00001226 SkDraw transformedDraw(draw);
1227 transformedDraw.fMatrix = &drawMatrix;
1228
robertphillips@google.combac6b052012-09-28 18:06:49 +00001229 this->drawRect(transformedDraw, srcRect, paintWithTexture);
junov@google.com1d329782011-07-28 20:10:09 +00001230
junov@google.comd935cfb2011-06-27 20:48:23 +00001231 return;
1232 }
1233
bsalomon@google.com5782d712011-01-21 21:03:59 +00001234 GrPaint grPaint;
skia.committer@gmail.com4e73aa12013-01-09 02:01:30 +00001235
humper@google.com9aaf36d2013-01-08 16:08:01 +00001236 bool alphaOnly = !(SkBitmap::kA8_Config == bitmap.config());
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001237 if (!skPaint2GrPaintNoShader(this, paint, alphaOnly, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001238 return;
1239 }
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001240 GrTextureParams params;
1241 params.setBilerp(paint.isFilterBitmap());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001242
robertphillips@google.combac6b052012-09-28 18:06:49 +00001243 if (!this->shouldTileBitmap(bitmap, params, srcRectPtr)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001244 // take the simple case
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001245 this->internalDrawBitmap(bitmap, srcRect, m, params, &grPaint);
robertphillips@google.combac6b052012-09-28 18:06:49 +00001246 } else {
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001247 this->drawTiledBitmap(bitmap, srcRect, m, params, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001248 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001249}
1250
1251// Break 'bitmap' into several tiles to draw it since it has already
1252// been determined to be too large to fit in VRAM
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001253void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001254 const SkRect& srcRect,
1255 const SkMatrix& m,
1256 const GrTextureParams& params,
1257 GrPaint* grPaint) {
1258 const int maxTextureSize = fContext->getMaxTextureSize();
1259
1260 int tileSize = determine_tile_size(bitmap, srcRect, maxTextureSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001261
reed@google.comac10a2d2010-12-22 21:39:39 +00001262 // compute clip bounds in local coordinates
robertphillips@google.combac6b052012-09-28 18:06:49 +00001263 SkRect clipRect;
reed@google.comac10a2d2010-12-22 21:39:39 +00001264 {
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001265 const GrRenderTarget* rt = fContext->getRenderTarget();
1266 clipRect.setWH(SkIntToScalar(rt->width()), SkIntToScalar(rt->height()));
1267 if (!fContext->getClip()->fClipStack->intersectRectWithClip(&clipRect)) {
1268 return;
1269 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001270 SkMatrix matrix, inverse;
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001271 matrix.setConcat(fContext->getMatrix(), m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001272 if (!matrix.invert(&inverse)) {
1273 return;
1274 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001275 inverse.mapRect(&clipRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001276 }
1277
bsalomon@google.comfb309512011-11-30 14:13:48 +00001278 int nx = bitmap.width() / tileSize;
1279 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001280 for (int x = 0; x <= nx; x++) {
1281 for (int y = 0; y <= ny; y++) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001282 SkRect tileR;
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001283 tileR.set(SkIntToScalar(x * tileSize),
robertphillips@google.combac6b052012-09-28 18:06:49 +00001284 SkIntToScalar(y * tileSize),
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001285 SkIntToScalar((x + 1) * tileSize),
robertphillips@google.combac6b052012-09-28 18:06:49 +00001286 SkIntToScalar((y + 1) * tileSize));
1287
1288 if (!SkRect::Intersects(tileR, clipRect)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001289 continue;
1290 }
1291
robertphillips@google.combac6b052012-09-28 18:06:49 +00001292 if (!tileR.intersect(srcRect)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001293 continue;
1294 }
1295
1296 SkBitmap tmpB;
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001297 SkIRect iTileR;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001298 tileR.roundOut(&iTileR);
1299 if (bitmap.extractSubset(&tmpB, iTileR)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001300 // now offset it to make it "local" to our tmp bitmap
robertphillips@google.combac6b052012-09-28 18:06:49 +00001301 tileR.offset(SkIntToScalar(-iTileR.fLeft), SkIntToScalar(-iTileR.fTop));
reed@google.comac10a2d2010-12-22 21:39:39 +00001302 SkMatrix tmpM(m);
skia.committer@gmail.comdc3a4e52012-10-02 02:01:24 +00001303 tmpM.preTranslate(SkIntToScalar(iTileR.fLeft),
robertphillips@google.comffad46b2012-10-01 14:32:51 +00001304 SkIntToScalar(iTileR.fTop));
1305
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001306 this->internalDrawBitmap(tmpB, tileR, tmpM, params, grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001307 }
1308 }
1309 }
1310}
1311
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001312namespace {
1313
1314bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1315 // detect pixel disalignment
1316 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1317 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
rmistry@google.comd6176b02012-08-23 18:14:13 +00001318 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001319 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1320 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1321 COLOR_BLEED_TOLERANCE &&
1322 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1323 COLOR_BLEED_TOLERANCE) {
1324 return true;
1325 }
1326 return false;
1327}
1328
1329bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1330 const SkMatrix& m) {
1331 // Only gets called if hasAlignedSamples returned false.
1332 // So we can assume that sampling is axis aligned but not texel aligned.
1333 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001334 SkRect innerSrcRect(srcRect), innerTransformedRect,
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001335 outerTransformedRect(transformedRect);
1336 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1337 m.mapRect(&innerTransformedRect, innerSrcRect);
1338
1339 // The gap between outerTransformedRect and innerTransformedRect
1340 // represents the projection of the source border area, which is
1341 // problematic for color bleeding. We must check whether any
1342 // destination pixels sample the border area.
1343 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1344 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1345 SkIRect outer, inner;
1346 outerTransformedRect.round(&outer);
1347 innerTransformedRect.round(&inner);
1348 // If the inner and outer rects round to the same result, it means the
1349 // border does not overlap any pixel centers. Yay!
1350 return inner != outer;
1351}
1352
1353} // unnamed namespace
1354
reed@google.comac10a2d2010-12-22 21:39:39 +00001355/*
1356 * This is called by drawBitmap(), which has to handle images that may be too
1357 * large to be represented by a single texture.
1358 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001359 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1360 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001361 */
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001362void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001363 const SkRect& srcRect,
reed@google.comac10a2d2010-12-22 21:39:39 +00001364 const SkMatrix& m,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001365 const GrTextureParams& params,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001366 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001367 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1368 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001369
reed@google.com9c49bc32011-07-07 13:42:37 +00001370 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001371 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001372 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001373 return;
1374 }
1375
reed@google.comac10a2d2010-12-22 21:39:39 +00001376 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001377 SkAutoCachedTexture act(this, bitmap, &params, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001378 if (NULL == texture) {
1379 return;
1380 }
1381
robertphillips@google.combac6b052012-09-28 18:06:49 +00001382 GrRect dstRect(srcRect);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001383 GrRect paintRect;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001384 SkScalar wInv = SkScalarInvert(SkIntToScalar(bitmap.width()));
1385 SkScalar hInv = SkScalarInvert(SkIntToScalar(bitmap.height()));
1386 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1387 SkScalarMul(srcRect.fTop, hInv),
1388 SkScalarMul(srcRect.fRight, wInv),
1389 SkScalarMul(srcRect.fBottom, hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001390
rmistry@google.comd6176b02012-08-23 18:14:13 +00001391 bool needsTextureDomain = false;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001392 if (params.isBilerp()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001393 // Need texture domain if drawing a sub rect.
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001394 needsTextureDomain = srcRect.width() < bitmap.width() ||
robertphillips@google.combac6b052012-09-28 18:06:49 +00001395 srcRect.height() < bitmap.height();
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001396 if (m.rectStaysRect() && fContext->getMatrix().rectStaysRect()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001397 // sampling is axis-aligned
robertphillips@google.combac6b052012-09-28 18:06:49 +00001398 GrRect transformedRect;
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001399 SkMatrix srcToDeviceMatrix(m);
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001400 srcToDeviceMatrix.postConcat(fContext->getMatrix());
robertphillips@google.combac6b052012-09-28 18:06:49 +00001401 srcToDeviceMatrix.mapRect(&transformedRect, srcRect);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001402
robertphillips@google.combac6b052012-09-28 18:06:49 +00001403 if (hasAlignedSamples(srcRect, transformedRect)) {
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001404 // We could also turn off filtering here (but we already did a cache lookup with
1405 // params).
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001406 needsTextureDomain = false;
1407 } else {
1408 needsTextureDomain = needsTextureDomain &&
robertphillips@google.combac6b052012-09-28 18:06:49 +00001409 mayColorBleed(srcRect, transformedRect, m);
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001410 }
1411 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001412 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001413
1414 GrRect textureDomain = GrRect::MakeEmpty();
bsalomon@google.com0ac6af42013-01-16 15:16:18 +00001415 SkAutoTUnref<GrEffectRef> effect;
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001416 if (needsTextureDomain) {
1417 // Use a constrained texture domain to avoid color bleeding
bsalomon@google.com81712882012-11-01 17:12:34 +00001418 SkScalar left, top, right, bottom;
1419 if (srcRect.width() > SK_Scalar1) {
1420 SkScalar border = SK_ScalarHalf / bitmap.width();
junov@google.com6acc9b32011-05-16 18:32:07 +00001421 left = paintRect.left() + border;
1422 right = paintRect.right() - border;
1423 } else {
bsalomon@google.com81712882012-11-01 17:12:34 +00001424 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
junov@google.com6acc9b32011-05-16 18:32:07 +00001425 }
bsalomon@google.com81712882012-11-01 17:12:34 +00001426 if (srcRect.height() > SK_Scalar1) {
1427 SkScalar border = SK_ScalarHalf / bitmap.height();
junov@google.com6acc9b32011-05-16 18:32:07 +00001428 top = paintRect.top() + border;
1429 bottom = paintRect.bottom() - border;
1430 } else {
bsalomon@google.com81712882012-11-01 17:12:34 +00001431 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
junov@google.com6acc9b32011-05-16 18:32:07 +00001432 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001433 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +00001434 effect.reset(GrTextureDomainEffect::Create(texture,
1435 SkMatrix::I(),
1436 textureDomain,
1437 GrTextureDomainEffect::kClamp_WrapMode,
1438 params.isBilerp()));
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001439 } else {
bsalomon@google.com68b58c92013-01-17 16:50:08 +00001440 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
junov@google.com6acc9b32011-05-16 18:32:07 +00001441 }
bsalomon@google.com73818dc2013-03-28 13:23:29 +00001442 grPaint->colorStage(kBitmapEffectIdx)->setEffect(effect);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001443 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001444}
1445
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001446static SkBitmap wrap_texture(GrTexture* texture) {
1447 SkBitmap result;
1448 bool dummy;
1449 SkBitmap::Config config = grConfig2skConfig(texture->config(), &dummy);
1450 result.setConfig(config, texture->width(), texture->height());
1451 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (texture)))->unref();
1452 return result;
1453}
1454
1455static bool filter_texture(SkDevice* device, GrContext* context,
1456 GrTexture* texture, SkImageFilter* filter,
1457 int w, int h, SkBitmap* result) {
reed@google.com8926b162012-03-23 15:36:36 +00001458 GrAssert(filter);
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001459 SkDeviceImageFilterProxy proxy(device);
reed@google.com8926b162012-03-23 15:36:36 +00001460
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001461 if (filter->canFilterImageGPU()) {
senorblanco@chromium.org985fa792012-10-24 15:14:26 +00001462 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1463 // filter. Also set the clip wide open and the matrix to identity.
1464 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001465 return filter->filterImageGPU(&proxy, wrap_texture(texture), result);
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001466 } else {
1467 return false;
reed@google.com8926b162012-03-23 15:36:36 +00001468 }
reed@google.com8926b162012-03-23 15:36:36 +00001469}
1470
reed@google.comac10a2d2010-12-22 21:39:39 +00001471void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001472 int left, int top, const SkPaint& paint) {
1473 // drawSprite is defined to be in device coords.
1474 CHECK_SHOULD_DRAW(draw, true);
reed@google.comac10a2d2010-12-22 21:39:39 +00001475
reed@google.com8926b162012-03-23 15:36:36 +00001476 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001477 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1478 return;
1479 }
1480
reed@google.com76dd2772012-01-05 21:15:07 +00001481 int w = bitmap.width();
1482 int h = bitmap.height();
1483
bsalomon@google.com5782d712011-01-21 21:03:59 +00001484 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001485 if(!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001486 return;
1487 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001488
bsalomon@google.com73818dc2013-03-28 13:23:29 +00001489 GrEffectStage* stage = grPaint.colorStage(kBitmapEffectIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001490
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001491 GrTexture* texture;
bsalomon@google.com08283af2012-10-26 13:01:20 +00001492 stage->reset();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001493 // draw sprite uses the default texture params
1494 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
bsalomon@google.com73818dc2013-03-28 13:23:29 +00001495 grPaint.colorStage(kBitmapEffectIdx)->setEffect(
bsalomon@google.com68b58c92013-01-17 16:50:08 +00001496 GrSimpleTextureEffect::Create(texture, SkMatrix::I()))->unref();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001497
reed@google.com8926b162012-03-23 15:36:36 +00001498 SkImageFilter* filter = paint.getImageFilter();
1499 if (NULL != filter) {
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001500 SkBitmap filterBitmap;
1501 if (filter_texture(this, fContext, texture, filter, w, h, &filterBitmap)) {
bsalomon@google.com73818dc2013-03-28 13:23:29 +00001502 grPaint.colorStage(kBitmapEffectIdx)->setEffect(
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001503 GrSimpleTextureEffect::Create((GrTexture*) filterBitmap.getTexture(), SkMatrix::I()))->unref();
1504 texture = (GrTexture*) filterBitmap.getTexture();
1505 w = filterBitmap.width();
1506 h = filterBitmap.height();
reed@google.com8926b162012-03-23 15:36:36 +00001507 }
reed@google.com76dd2772012-01-05 21:15:07 +00001508 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001509
bsalomon@google.com5782d712011-01-21 21:03:59 +00001510 fContext->drawRectToRect(grPaint,
bsalomon@google.com81712882012-11-01 17:12:34 +00001511 GrRect::MakeXYWH(SkIntToScalar(left),
1512 SkIntToScalar(top),
1513 SkIntToScalar(w),
1514 SkIntToScalar(h)),
1515 GrRect::MakeWH(SK_Scalar1 * w / texture->width(),
1516 SK_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001517}
1518
reed@google.com33535f32012-09-25 15:37:50 +00001519void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1520 const SkRect* src, const SkRect& dst,
1521 const SkPaint& paint) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001522 SkMatrix matrix;
1523 SkRect bitmapBounds, tmpSrc;
1524
1525 bitmapBounds.set(0, 0,
1526 SkIntToScalar(bitmap.width()),
1527 SkIntToScalar(bitmap.height()));
1528
reed@google.com33535f32012-09-25 15:37:50 +00001529 // Compute matrix from the two rectangles
robertphillips@google.combac6b052012-09-28 18:06:49 +00001530 if (NULL != src) {
1531 tmpSrc = *src;
1532 } else {
1533 tmpSrc = bitmapBounds;
1534 }
1535 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1536
1537 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1538 if (NULL != src) {
1539 if (!bitmapBounds.contains(tmpSrc)) {
1540 if (!tmpSrc.intersect(bitmapBounds)) {
1541 return; // nothing to draw
reed@google.com33535f32012-09-25 15:37:50 +00001542 }
reed@google.com33535f32012-09-25 15:37:50 +00001543 }
reed@google.com33535f32012-09-25 15:37:50 +00001544 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +00001545
robertphillips@google.combac6b052012-09-28 18:06:49 +00001546 this->drawBitmapCommon(draw, bitmap, &tmpSrc, matrix, paint);
reed@google.com33535f32012-09-25 15:37:50 +00001547}
1548
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001549void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001550 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001551 // clear of the source device must occur before CHECK_SHOULD_DRAW
1552 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1553 if (dev->fNeedClear) {
1554 // TODO: could check here whether we really need to draw at all
1555 dev->clear(0x0);
1556 }
1557
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001558 // drawDevice is defined to be in device coords.
1559 CHECK_SHOULD_DRAW(draw, true);
reed@google.comac10a2d2010-12-22 21:39:39 +00001560
bsalomon@google.com5782d712011-01-21 21:03:59 +00001561 GrPaint grPaint;
bsalomon@google.com73818dc2013-03-28 13:23:29 +00001562 grPaint.colorStage(kBitmapEffectIdx)->reset();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001563 if (!dev->bindDeviceAsTexture(&grPaint) ||
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001564 !skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001565 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001566 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001567
bsalomon@google.com73818dc2013-03-28 13:23:29 +00001568 GrTexture* devTex = (*grPaint.getColorStage(kBitmapEffectIdx).getEffect())->texture(0);
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001569 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001570
senorblanco@chromium.org514b9222013-01-18 21:53:12 +00001571 const SkBitmap& bm = dev->accessBitmap(false);
1572 int w = bm.width();
1573 int h = bm.height();
1574
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001575 SkImageFilter* filter = paint.getImageFilter();
1576 if (NULL != filter) {
1577 SkBitmap filterBitmap;
1578 if (filter_texture(this, fContext, devTex, filter, w, h, &filterBitmap)) {
bsalomon@google.com73818dc2013-03-28 13:23:29 +00001579 grPaint.colorStage(kBitmapEffectIdx)->setEffect(
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001580 GrSimpleTextureEffect::Create((GrTexture*) filterBitmap.getTexture(), SkMatrix::I()))->unref();
1581 devTex = (GrTexture*) filterBitmap.getTexture();
1582 w = filterBitmap.width();
1583 h = filterBitmap.height();
1584 }
1585 }
1586
bsalomon@google.com81712882012-11-01 17:12:34 +00001587 GrRect dstRect = GrRect::MakeXYWH(SkIntToScalar(x),
1588 SkIntToScalar(y),
1589 SkIntToScalar(w),
1590 SkIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001591
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001592 // The device being drawn may not fill up its texture (saveLayer uses
1593 // the approximate ).
bsalomon@google.com81712882012-11-01 17:12:34 +00001594 GrRect srcRect = GrRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1595 SK_Scalar1 * h / devTex->height());
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001596
1597 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001598}
1599
reed@google.com8926b162012-03-23 15:36:36 +00001600bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
senorblanco@chromium.orgd043cce2013-04-08 19:43:22 +00001601 return filter->canFilterImageGPU();
reed@google.com8926b162012-03-23 15:36:36 +00001602}
1603
1604bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1605 const SkMatrix& ctm,
1606 SkBitmap* result, SkIPoint* offset) {
1607 // want explicitly our impl, so guard against a subclass of us overriding it
1608 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001609 return false;
1610 }
reed@google.com8926b162012-03-23 15:36:36 +00001611
1612 SkAutoLockPixels alp(src, !src.getTexture());
1613 if (!src.getTexture() && !src.readyToDraw()) {
1614 return false;
1615 }
1616
1617 GrPaint paint;
reed@google.com8926b162012-03-23 15:36:36 +00001618
reed@google.com8926b162012-03-23 15:36:36 +00001619 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001620 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1621 // must be pushed upstack.
1622 SkAutoCachedTexture act(this, src, NULL, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001623
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001624 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), result);
reed@google.com76dd2772012-01-05 21:15:07 +00001625}
1626
reed@google.comac10a2d2010-12-22 21:39:39 +00001627///////////////////////////////////////////////////////////////////////////////
1628
1629// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001630static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001631 kTriangles_GrPrimitiveType,
1632 kTriangleStrip_GrPrimitiveType,
1633 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001634};
1635
1636void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1637 int vertexCount, const SkPoint vertices[],
1638 const SkPoint texs[], const SkColor colors[],
1639 SkXfermode* xmode,
1640 const uint16_t indices[], int indexCount,
1641 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001642 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001643
bsalomon@google.com5782d712011-01-21 21:03:59 +00001644 GrPaint grPaint;
bsalomon@google.com5782d712011-01-21 21:03:59 +00001645 // we ignore the shader if texs is null.
1646 if (NULL == texs) {
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001647 if (!skPaint2GrPaintNoShader(this, paint, false, NULL == colors, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001648 return;
1649 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001650 } else {
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001651 if (!skPaint2GrPaintShader(this, paint, NULL == colors, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001652 return;
1653 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001654 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001655
1656 if (NULL != xmode && NULL != texs && NULL != colors) {
reed@google.com8d3cd7a2013-01-30 21:36:11 +00001657 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001658 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1659#if 0
1660 return
1661#endif
1662 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001663 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001664
bsalomon@google.com498776a2011-08-16 19:20:44 +00001665 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1666 if (NULL != colors) {
1667 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001668 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001669 for (int i = 0; i < vertexCount; ++i) {
rileya@google.com24f3ad12012-07-18 21:47:40 +00001670 convertedColors[i] = SkColor2GrColor(colors[i]);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001671 }
1672 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001673 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001674 fContext->drawVertices(grPaint,
1675 gVertexMode2PrimitiveType[vmode],
1676 vertexCount,
1677 (GrPoint*) vertices,
1678 (GrPoint*) texs,
1679 colors,
1680 indices,
1681 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001682}
1683
1684///////////////////////////////////////////////////////////////////////////////
1685
1686static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001687 GrFontScaler* scaler = (GrFontScaler*)data;
1688 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001689}
1690
1691static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1692 void* auxData;
1693 GrFontScaler* scaler = NULL;
1694 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1695 scaler = (GrFontScaler*)auxData;
1696 }
1697 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001698 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001699 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1700 }
1701 return scaler;
1702}
1703
1704static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1705 SkFixed fx, SkFixed fy,
1706 const SkGlyph& glyph) {
1707 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1708
bungeman@google.com15865a72012-01-11 16:28:04 +00001709 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001710
1711 if (NULL == procs->fFontScaler) {
1712 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1713 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001714
bungeman@google.com15865a72012-01-11 16:28:04 +00001715 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1716 glyph.getSubXFixed(),
1717 glyph.getSubYFixed()),
1718 SkFixedFloorToFixed(fx),
1719 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001720 procs->fFontScaler);
1721}
1722
bsalomon@google.com5782d712011-01-21 21:03:59 +00001723SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001724
1725 // deferred allocation
1726 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001727 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001728 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1729 fDrawProcs->fContext = fContext;
1730 }
1731
1732 // init our (and GL's) state
1733 fDrawProcs->fTextContext = context;
1734 fDrawProcs->fFontScaler = NULL;
1735 return fDrawProcs;
1736}
1737
1738void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1739 size_t byteLength, SkScalar x, SkScalar y,
1740 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001741 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001742
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001743 if (fContext->getMatrix().hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001744 // this guy will just call our drawPath()
1745 draw.drawText((const char*)text, byteLength, x, y, paint);
1746 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001747 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001748
1749 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001750 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001751 return;
1752 }
bsalomon@google.com0e354aa2012-10-08 20:44:25 +00001753 GrTextContext context(fContext, grPaint);
tomhudson@google.com375ff852012-06-29 18:37:57 +00001754 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001755 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1756 }
1757}
1758
1759void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1760 size_t byteLength, const SkScalar pos[],
1761 SkScalar constY, int scalarsPerPos,
1762 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001763 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001764
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001765 if (fContext->getMatrix().hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001766 // this guy will just call our drawPath()
1767 draw.drawPosText((const char*)text, byteLength, pos, constY,
1768 scalarsPerPos, paint);
1769 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001770 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001771
1772 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001773 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001774 return;
1775 }
bsalomon@google.com0e354aa2012-10-08 20:44:25 +00001776 GrTextContext context(fContext, grPaint);
tomhudson@google.com375ff852012-06-29 18:37:57 +00001777 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001778 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1779 scalarsPerPos, paint);
1780 }
1781}
1782
1783void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1784 size_t len, const SkPath& path,
1785 const SkMatrix* m, const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001786 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001787
1788 SkASSERT(draw.fDevice == this);
1789 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1790}
1791
1792///////////////////////////////////////////////////////////////////////////////
1793
reed@google.comf67e4cf2011-03-15 20:56:58 +00001794bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1795 if (!paint.isLCDRenderText()) {
1796 // we're cool with the paint as is
1797 return false;
1798 }
1799
1800 if (paint.getShader() ||
1801 paint.getXfermode() || // unless its srcover
1802 paint.getMaskFilter() ||
1803 paint.getRasterizer() ||
1804 paint.getColorFilter() ||
1805 paint.getPathEffect() ||
1806 paint.isFakeBoldText() ||
1807 paint.getStyle() != SkPaint::kFill_Style) {
1808 // turn off lcd
1809 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1810 flags->fHinting = paint.getHinting();
1811 return true;
1812 }
1813 // we're cool with the paint as is
1814 return false;
1815}
1816
reed@google.com75d939b2011-12-07 15:07:23 +00001817void SkGpuDevice::flush() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +00001818 DO_DEFERRED_CLEAR();
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001819 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001820}
1821
reed@google.comf67e4cf2011-03-15 20:56:58 +00001822///////////////////////////////////////////////////////////////////////////////
1823
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001824SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1825 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001826 bool isOpaque,
1827 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001828 GrTextureDesc desc;
1829 desc.fConfig = fRenderTarget->config();
1830 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1831 desc.fWidth = width;
1832 desc.fHeight = height;
1833 desc.fSampleCnt = fRenderTarget->numSamples();
1834
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001835 SkAutoTUnref<GrTexture> texture;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001836 // Skia's convention is to only clear a device if it is non-opaque.
1837 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001838
1839#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1840 // layers are never draw in repeat modes, so we can request an approx
1841 // match and ignore any padding.
bsalomon@google.com0797c2c2012-12-20 15:13:01 +00001842 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1843 GrContext::kApprox_ScratchTexMatch :
1844 GrContext::kExact_ScratchTexMatch;
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001845 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001846#else
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001847 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001848#endif
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001849 if (NULL != texture.get()) {
1850 return SkNEW_ARGS(SkGpuDevice,(fContext, texture, needClear));
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001851 } else {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001852 GrPrintf("---- failed to create compatible device texture [%d %d]\n", width, height);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001853 return NULL;
1854 }
1855}
1856
1857SkGpuDevice::SkGpuDevice(GrContext* context,
1858 GrTexture* texture,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001859 bool needClear)
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001860 : SkDevice(make_bitmap(context, texture->asRenderTarget())) {
1861
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001862 GrAssert(texture && texture->asRenderTarget());
bsalomon@google.com8090e652012-08-28 15:07:11 +00001863 // This constructor is called from onCreateCompatibleDevice. It has locked the RT in the texture
1864 // cache. We pass true for the third argument so that it will get unlocked.
1865 this->initFromRenderTarget(context, texture->asRenderTarget(), true);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001866 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001867}