blob: aca3ba6d76ccec49ad5319a1a7f76a1de45a0794 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
tomhudson@google.com898e7b52012-06-01 20:42:15 +00008#include "SkGpuDevice.h"
reed@google.comac10a2d2010-12-22 21:39:39 +00009
tomhudson@google.com2f68e762012-07-17 18:43:21 +000010#include "effects/GrTextureDomainEffect.h"
bsalomon@google.com68b58c92013-01-17 16:50:08 +000011#include "effects/GrSimpleTextureEffect.h"
epoger@google.comec3ed6a2011-07-28 14:26:00 +000012
reed@google.comac10a2d2010-12-22 21:39:39 +000013#include "GrContext.h"
14#include "GrTextContext.h"
15
robertphillips@google.come9c04692012-06-29 00:30:13 +000016#include "SkGrTexturePixelRef.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000017
Scroggo97c88c22011-05-11 14:05:25 +000018#include "SkColorFilter.h"
senorblanco@chromium.org9c397442012-09-27 21:57:45 +000019#include "SkDeviceImageFilterProxy.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000020#include "SkDrawProcs.h"
21#include "SkGlyphCache.h"
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +000022#include "SkImageFilter.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000023#include "SkPathEffect.h"
24#include "SkStroke.h"
reed@google.comc9aa5872011-04-05 21:05:37 +000025#include "SkUtils.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000026
bsalomon@google.com06cd7322012-03-30 18:45:35 +000027#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
reed@google.comac10a2d2010-12-22 21:39:39 +000028
29#if 0
30 extern bool (*gShouldDrawProc)();
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +000031 #define CHECK_SHOULD_DRAW(draw, forceI) \
reed@google.comac10a2d2010-12-22 21:39:39 +000032 do { \
33 if (gShouldDrawProc && !gShouldDrawProc()) return; \
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +000034 this->prepareDraw(draw, forceI); \
reed@google.comac10a2d2010-12-22 21:39:39 +000035 } while (0)
36#else
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +000037 #define CHECK_SHOULD_DRAW(draw, forceI) this->prepareDraw(draw, forceI)
reed@google.comac10a2d2010-12-22 21:39:39 +000038#endif
39
bsalomon@google.com73818dc2013-03-28 13:23:29 +000040// we use the same effect slot on GrPaint for bitmaps and shaders (since drawBitmap, drawSprite,
41// and drawDevice ignore SkShader)
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000042enum {
bsalomon@google.com73818dc2013-03-28 13:23:29 +000043 kShaderEffectIdx = 0,
44 kBitmapEffectIdx = 0,
45 kColorFilterEffectIdx = 1,
46 kXfermodeEffectIdx = 2,
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000047};
48
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +000049#define MAX_BLUR_SIGMA 4.0f
50// FIXME: This value comes from from SkBlurMaskFilter.cpp.
51// Should probably be put in a common header someplace.
52#define MAX_BLUR_RADIUS SkIntToScalar(128)
53// This constant approximates the scaling done in the software path's
54// "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
55// IMHO, it actually should be 1: we blur "less" than we should do
56// according to the CSS and canvas specs, simply because Safari does the same.
57// Firefox used to do the same too, until 4.0 where they fixed it. So at some
58// point we should probably get rid of these scaling constants and rebaseline
59// all the blur tests.
60#define BLUR_SIGMA_SCALE 0.6f
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000061// This constant represents the screen alignment criterion in texels for
rmistry@google.comd6176b02012-08-23 18:14:13 +000062// requiring texture domain clamping to prevent color bleeding when drawing
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000063// a sub region of a larger source image.
64#define COLOR_BLEED_TOLERANCE SkFloatToScalar(0.001f)
bsalomon@google.com06cd7322012-03-30 18:45:35 +000065
junov@google.comdbfac8a2012-12-06 21:47:40 +000066#define DO_DEFERRED_CLEAR() \
67 do { \
68 if (fNeedClear) { \
69 this->clear(SK_ColorTRANSPARENT); \
70 } \
71 } while (false) \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000072
reed@google.comac10a2d2010-12-22 21:39:39 +000073///////////////////////////////////////////////////////////////////////////////
74
reed@google.comb0a34d82012-07-11 19:57:55 +000075#define CHECK_FOR_NODRAW_ANNOTATION(paint) \
76 do { if (paint.isNoDrawAnnotation()) { return; } } while (0)
77
78///////////////////////////////////////////////////////////////////////////////
79
80
bsalomon@google.com84405e02012-03-05 19:57:21 +000081class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
82public:
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000083 SkAutoCachedTexture()
84 : fDevice(NULL)
85 , fTexture(NULL) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000086 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000087
bsalomon@google.com84405e02012-03-05 19:57:21 +000088 SkAutoCachedTexture(SkGpuDevice* device,
89 const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +000090 const GrTextureParams* params,
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000091 GrTexture** texture)
92 : fDevice(NULL)
93 , fTexture(NULL) {
94 GrAssert(NULL != texture);
bsalomon@google.comb8670992012-07-25 21:27:09 +000095 *texture = this->set(device, bitmap, params);
reed@google.comac10a2d2010-12-22 21:39:39 +000096 }
reed@google.comac10a2d2010-12-22 21:39:39 +000097
bsalomon@google.com84405e02012-03-05 19:57:21 +000098 ~SkAutoCachedTexture() {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000099 if (NULL != fTexture) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000100 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
bsalomon@google.com84405e02012-03-05 19:57:21 +0000101 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000102 }
bsalomon@google.com84405e02012-03-05 19:57:21 +0000103
104 GrTexture* set(SkGpuDevice* device,
105 const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000106 const GrTextureParams* params) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000107 if (NULL != fTexture) {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000108 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000109 fTexture = NULL;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000110 }
111 fDevice = device;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000112 GrTexture* result = (GrTexture*)bitmap.getTexture();
113 if (NULL == result) {
114 // Cannot return the native texture so look it up in our cache
bsalomon@google.com95ed55a2013-01-24 14:46:47 +0000115 fTexture = GrLockAndRefCachedBitmapTexture(device->context(), bitmap, params);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000116 result = fTexture;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000117 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000118 return result;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000119 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000120
bsalomon@google.com84405e02012-03-05 19:57:21 +0000121private:
122 SkGpuDevice* fDevice;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000123 GrTexture* fTexture;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000124};
reed@google.comac10a2d2010-12-22 21:39:39 +0000125
126///////////////////////////////////////////////////////////////////////////////
127
reed@google.comac10a2d2010-12-22 21:39:39 +0000128struct GrSkDrawProcs : public SkDrawProcs {
129public:
130 GrContext* fContext;
131 GrTextContext* fTextContext;
132 GrFontScaler* fFontScaler; // cached in the skia glyphcache
133};
134
135///////////////////////////////////////////////////////////////////////////////
136
reed@google.comaf951c92011-06-16 19:10:39 +0000137static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
138 switch (config) {
139 case kAlpha_8_GrPixelConfig:
140 *isOpaque = false;
141 return SkBitmap::kA8_Config;
142 case kRGB_565_GrPixelConfig:
143 *isOpaque = true;
144 return SkBitmap::kRGB_565_Config;
145 case kRGBA_4444_GrPixelConfig:
146 *isOpaque = false;
147 return SkBitmap::kARGB_4444_Config;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000148 case kSkia8888_GrPixelConfig:
bsalomon@google.comc4364992011-11-07 15:54:49 +0000149 // we don't currently have a way of knowing whether
150 // a 8888 is opaque based on the config.
151 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000152 return SkBitmap::kARGB_8888_Config;
153 default:
154 *isOpaque = false;
155 return SkBitmap::kNo_Config;
156 }
157}
reed@google.comac10a2d2010-12-22 21:39:39 +0000158
reed@google.comaf951c92011-06-16 19:10:39 +0000159static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000160 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000161
162 bool isOpaque;
163 SkBitmap bitmap;
164 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
165 renderTarget->width(), renderTarget->height());
166 bitmap.setIsOpaque(isOpaque);
167 return bitmap;
168}
169
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000170SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000171: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
bsalomon@google.com8090e652012-08-28 15:07:11 +0000172 this->initFromRenderTarget(context, texture->asRenderTarget(), false);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000173}
174
reed@google.comaf951c92011-06-16 19:10:39 +0000175SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000176: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.com8090e652012-08-28 15:07:11 +0000177 this->initFromRenderTarget(context, renderTarget, false);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000178}
179
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000180void SkGpuDevice::initFromRenderTarget(GrContext* context,
bsalomon@google.com8090e652012-08-28 15:07:11 +0000181 GrRenderTarget* renderTarget,
182 bool cached) {
reed@google.comaf951c92011-06-16 19:10:39 +0000183 fDrawProcs = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000184
reed@google.comaf951c92011-06-16 19:10:39 +0000185 fContext = context;
186 fContext->ref();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000187
reed@google.comaf951c92011-06-16 19:10:39 +0000188 fRenderTarget = NULL;
189 fNeedClear = false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000190
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000191 GrAssert(NULL != renderTarget);
192 fRenderTarget = renderTarget;
193 fRenderTarget->ref();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000194
bsalomon@google.coma2921122012-08-28 12:34:17 +0000195 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
196 // on the RT but not vice-versa.
197 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
198 // busting chrome (for a currently unknown reason).
199 GrSurface* surface = fRenderTarget->asTexture();
200 if (NULL == surface) {
201 surface = fRenderTarget;
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000202 }
bsalomon@google.com8090e652012-08-28 15:07:11 +0000203 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (surface, cached));
bsalomon@google.coma2921122012-08-28 12:34:17 +0000204
reed@google.comaf951c92011-06-16 19:10:39 +0000205 this->setPixelRef(pr, 0)->unref();
206}
207
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000208SkGpuDevice::SkGpuDevice(GrContext* context,
209 SkBitmap::Config config,
210 int width,
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000211 int height,
212 int sampleCount)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000213 : SkDevice(config, width, height, false /*isOpaque*/) {
214
reed@google.comac10a2d2010-12-22 21:39:39 +0000215 fDrawProcs = NULL;
216
reed@google.com7b201d22011-01-11 18:59:23 +0000217 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000218 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000219
reed@google.comac10a2d2010-12-22 21:39:39 +0000220 fRenderTarget = NULL;
221 fNeedClear = false;
222
reed@google.comaf951c92011-06-16 19:10:39 +0000223 if (config != SkBitmap::kRGB_565_Config) {
224 config = SkBitmap::kARGB_8888_Config;
225 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000226
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000227 GrTextureDesc desc;
228 desc.fFlags = kRenderTarget_GrTextureFlagBit;
229 desc.fWidth = width;
230 desc.fHeight = height;
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000231 desc.fConfig = SkBitmapConfig2GrPixelConfig(config);
232 desc.fSampleCnt = sampleCount;
reed@google.comac10a2d2010-12-22 21:39:39 +0000233
bsalomon@google.coma2921122012-08-28 12:34:17 +0000234 SkAutoTUnref<GrTexture> texture(fContext->createUncachedTexture(desc, NULL, 0));
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000235
bsalomon@google.coma2921122012-08-28 12:34:17 +0000236 if (NULL != texture) {
237 fRenderTarget = texture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000238 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000239
reed@google.comaf951c92011-06-16 19:10:39 +0000240 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000241
reed@google.comaf951c92011-06-16 19:10:39 +0000242 // wrap the bitmap with a pixelref to expose our texture
bsalomon@google.coma2921122012-08-28 12:34:17 +0000243 SkGrPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (texture));
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000244 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000245 } else {
246 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
247 width, height);
248 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000249 }
250}
251
252SkGpuDevice::~SkGpuDevice() {
253 if (fDrawProcs) {
254 delete fDrawProcs;
255 }
256
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000257 // The GrContext takes a ref on the target. We don't want to cause the render
258 // target to be unnecessarily kept alive.
259 if (fContext->getRenderTarget() == fRenderTarget) {
260 fContext->setRenderTarget(NULL);
261 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000262
robertphillips@google.com055f9082012-10-24 13:24:11 +0000263 if (fContext->getClip() == &fClipData) {
264 fContext->setClip(NULL);
265 }
266
bsalomon@google.coma2921122012-08-28 12:34:17 +0000267 SkSafeUnref(fRenderTarget);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000268 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000269}
270
reed@google.comac10a2d2010-12-22 21:39:39 +0000271///////////////////////////////////////////////////////////////////////////////
272
273void SkGpuDevice::makeRenderTargetCurrent() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000274 DO_DEFERRED_CLEAR();
reed@google.comac10a2d2010-12-22 21:39:39 +0000275 fContext->setRenderTarget(fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000276}
277
278///////////////////////////////////////////////////////////////////////////////
279
bsalomon@google.comc4364992011-11-07 15:54:49 +0000280namespace {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000281GrPixelConfig config8888_to_grconfig_and_flags(SkCanvas::Config8888 config8888, uint32_t* flags) {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000282 switch (config8888) {
283 case SkCanvas::kNative_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000284 *flags = 0;
285 return kSkia8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000286 case SkCanvas::kNative_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000287 *flags = GrContext::kUnpremul_PixelOpsFlag;
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000288 return kSkia8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000289 case SkCanvas::kBGRA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000290 *flags = 0;
291 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000292 case SkCanvas::kBGRA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000293 *flags = GrContext::kUnpremul_PixelOpsFlag;
294 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000295 case SkCanvas::kRGBA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000296 *flags = 0;
297 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000298 case SkCanvas::kRGBA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000299 *flags = GrContext::kUnpremul_PixelOpsFlag;
300 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000301 default:
302 GrCrash("Unexpected Config8888.");
bsalomon@google.comccaa0022012-09-25 19:55:07 +0000303 *flags = 0; // suppress warning
bsalomon@google.comfec0bc32013-02-07 14:43:04 +0000304 return kSkia8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000305 }
306}
307}
308
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000309bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
310 int x, int y,
311 SkCanvas::Config8888 config8888) {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000312 DO_DEFERRED_CLEAR();
bsalomon@google.com910267d2011-11-02 20:06:25 +0000313 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
314 SkASSERT(!bitmap.isNull());
315 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000316
bsalomon@google.com910267d2011-11-02 20:06:25 +0000317 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000318 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000319 uint32_t flags;
320 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000321 return fContext->readRenderTargetPixels(fRenderTarget,
322 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000323 bitmap.width(),
324 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000325 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000326 bitmap.getPixels(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000327 bitmap.rowBytes(),
328 flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000329}
330
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000331void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
332 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000333 SkAutoLockPixels alp(bitmap);
334 if (!bitmap.readyToDraw()) {
335 return;
336 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000337
338 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000339 uint32_t flags;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000340 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000341 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000342 } else {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000343 flags = 0;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000344 config= SkBitmapConfig2GrPixelConfig(bitmap.config());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000345 }
346
bsalomon@google.com6f379512011-11-16 20:36:03 +0000347 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000348 config, bitmap.getPixels(), bitmap.rowBytes(), flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000349}
350
robertphillips@google.com46f93502012-08-07 15:38:08 +0000351namespace {
humper@google.com05af1af2013-01-07 16:47:43 +0000352void purgeClipCB(int genID, void* ) {
robertphillips@google.com46f93502012-08-07 15:38:08 +0000353
354 if (SkClipStack::kInvalidGenID == genID ||
355 SkClipStack::kEmptyGenID == genID ||
356 SkClipStack::kWideOpenGenID == genID) {
357 // none of these cases will have a cached clip mask
358 return;
359 }
360
361}
362};
363
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000364void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
365 INHERITED::onAttachToCanvas(canvas);
366
367 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000368 fClipData.fClipStack = canvas->getClipStack();
robertphillips@google.com46f93502012-08-07 15:38:08 +0000369
370 fClipData.fClipStack->addPurgeClipCallback(purgeClipCB, fContext);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000371}
372
373void SkGpuDevice::onDetachFromCanvas() {
374 INHERITED::onDetachFromCanvas();
375
robertphillips@google.com46f93502012-08-07 15:38:08 +0000376 // TODO: iterate through the clip stack and clean up any cached clip masks
377 fClipData.fClipStack->removePurgeClipCallback(purgeClipCB, fContext);
378
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000379 fClipData.fClipStack = NULL;
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000380}
381
robertphillips@google.com607fe072012-07-24 13:54:00 +0000382#ifdef SK_DEBUG
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000383static void check_bounds(const GrClipData& clipData,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000384 const SkRegion& clipRegion,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000385 int renderTargetWidth,
386 int renderTargetHeight) {
387
robertphillips@google.com7b112892012-07-31 15:18:21 +0000388 SkIRect devBound;
389
390 devBound.setLTRB(0, 0, renderTargetWidth, renderTargetHeight);
391
robertphillips@google.com607fe072012-07-24 13:54:00 +0000392 SkClipStack::BoundsType boundType;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000393 SkRect canvTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000394
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000395 clipData.fClipStack->getBounds(&canvTemp, &boundType);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000396 if (SkClipStack::kNormal_BoundsType == boundType) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000397 SkIRect devTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000398
robertphillips@google.com7b112892012-07-31 15:18:21 +0000399 canvTemp.roundOut(&devTemp);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000400
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000401 devTemp.offset(-clipData.fOrigin.fX, -clipData.fOrigin.fY);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000402
robertphillips@google.com7b112892012-07-31 15:18:21 +0000403 if (!devBound.intersect(devTemp)) {
404 devBound.setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000405 }
406 }
407
robertphillips@google.com768fee82012-08-02 12:42:43 +0000408 GrAssert(devBound.contains(clipRegion.getBounds()));
robertphillips@google.com607fe072012-07-24 13:54:00 +0000409}
410#endif
411
reed@google.comac10a2d2010-12-22 21:39:39 +0000412///////////////////////////////////////////////////////////////////////////////
413
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000414// call this every draw call, to ensure that the context reflects our state,
reed@google.comac10a2d2010-12-22 21:39:39 +0000415// and not the state from some other canvas/device
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000416void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000417 GrAssert(NULL != fClipData.fClipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000418
reed@google.comac10a2d2010-12-22 21:39:39 +0000419 fContext->setRenderTarget(fRenderTarget);
420
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000421 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000422
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000423 if (forceIdentity) {
424 fContext->setIdentityMatrix();
425 } else {
426 fContext->setMatrix(*draw.fMatrix);
427 }
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000428 fClipData.fOrigin = this->getOrigin();
reed@google.comac10a2d2010-12-22 21:39:39 +0000429
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000430#ifdef SK_DEBUG
431 check_bounds(fClipData, *draw.fClip, fRenderTarget->width(), fRenderTarget->height());
432#endif
433
434 fContext->setClip(&fClipData);
435
436 DO_DEFERRED_CLEAR();
reed@google.comac10a2d2010-12-22 21:39:39 +0000437}
438
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000439SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000440 DO_DEFERRED_CLEAR();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000441 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000442}
443
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000444bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
bsalomon@google.coma2921122012-08-28 12:34:17 +0000445 GrTexture* texture = fRenderTarget->asTexture();
446 if (NULL != texture) {
bsalomon@google.com73818dc2013-03-28 13:23:29 +0000447 paint->colorStage(kBitmapEffectIdx)->setEffect(
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000448 GrSimpleTextureEffect::Create(texture, SkMatrix::I()))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000449 return true;
450 }
451 return false;
452}
453
454///////////////////////////////////////////////////////////////////////////////
455
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000456SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
457SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
458SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
459SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
460SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
461 shader_type_mismatch);
rileya@google.com3e332582012-07-03 13:43:35 +0000462SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
463 shader_type_mismatch);
rileya@google.com22e57f92012-07-19 15:16:19 +0000464SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
465SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000466
bsalomon@google.com84405e02012-03-05 19:57:21 +0000467namespace {
468
469// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
470// justAlpha indicates that skPaint's alpha should be used rather than the color
471// Callers may subsequently modify the GrPaint. Setting constantColor indicates
472// that the final paint will draw the same color at every pixel. This allows
473// an optimization where the the color filter can be applied to the skPaint's
twiz@google.com58071162012-07-18 21:41:50 +0000474// color once while converting to GrPaint and then ignored.
475inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
476 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000477 bool justAlpha,
478 bool constantColor,
479 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000480
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000481 grPaint->setDither(skPaint.isDither());
482 grPaint->setAntiAlias(skPaint.isAntiAlias());
bsalomon@google.com5782d712011-01-21 21:03:59 +0000483
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000484 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
485 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000486
487 SkXfermode* mode = skPaint.getXfermode();
bsalomon@google.comdb446252013-03-27 18:46:16 +0000488 GrEffectRef* xferEffect = NULL;
bsalomon@google.comf51c0132013-03-27 18:31:15 +0000489 if (SkXfermode::AsNewEffect(mode, dev->context(), &xferEffect, &sm, &dm)) {
bsalomon@google.com73818dc2013-03-28 13:23:29 +0000490 SkSafeUnref(grPaint->colorStage(kXfermodeEffectIdx)->setEffect(xferEffect));
bsalomon@google.comf51c0132013-03-27 18:31:15 +0000491 } else {
492 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000493#if 0
bsalomon@google.comf51c0132013-03-27 18:31:15 +0000494 return false;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000495#endif
bsalomon@google.com5782d712011-01-21 21:03:59 +0000496 }
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000497 grPaint->setBlendFunc(sk_blend_to_grblend(sm), sk_blend_to_grblend(dm));
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000498
bsalomon@google.com5782d712011-01-21 21:03:59 +0000499 if (justAlpha) {
500 uint8_t alpha = skPaint.getAlpha();
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000501 grPaint->setColor(GrColorPackRGBA(alpha, alpha, alpha, alpha));
Scroggod757df22011-05-16 13:11:16 +0000502 // justAlpha is currently set to true only if there is a texture,
503 // so constantColor should not also be true.
504 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000505 } else {
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000506 grPaint->setColor(SkColor2GrColor(skPaint.getColor()));
bsalomon@google.com73818dc2013-03-28 13:23:29 +0000507 GrAssert(!grPaint->isColorStageEnabled(kShaderEffectIdx));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000508 }
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000509
Scroggo97c88c22011-05-11 14:05:25 +0000510 SkColorFilter* colorFilter = skPaint.getColorFilter();
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000511 if (NULL != colorFilter) {
512 // if the source color is a constant then apply the filter here once rather than per pixel
513 // in a shader.
514 if (constantColor) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000515 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000516 grPaint->setColor(SkColor2GrColor(filtered));
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000517 } else {
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000518 SkAutoTUnref<GrEffectRef> effect(colorFilter->asNewEffect(dev->context()));
bsalomon@google.com021fc732012-10-25 12:47:42 +0000519 if (NULL != effect.get()) {
bsalomon@google.com73818dc2013-03-28 13:23:29 +0000520 grPaint->colorStage(kColorFilterEffectIdx)->setEffect(effect);
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000521 } else {
bsalomon@google.com8ea78d82012-10-24 20:11:30 +0000522 // TODO: rewrite this using asNewEffect()
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000523 SkColor color;
524 SkXfermode::Mode filterMode;
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000525 if (colorFilter->asColorMode(&color, &filterMode)) {
526 grPaint->setXfermodeColorFilter(filterMode, SkColor2GrColor(color));
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000527 }
528 }
Scroggod757df22011-05-16 13:11:16 +0000529 }
Scroggo97c88c22011-05-11 14:05:25 +0000530 }
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000531
bsalomon@google.com5782d712011-01-21 21:03:59 +0000532 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000533}
534
bsalomon@google.com84405e02012-03-05 19:57:21 +0000535// This function is similar to skPaint2GrPaintNoShader but also converts
bsalomon@google.com08283af2012-10-26 13:01:20 +0000536// skPaint's shader to a GrTexture/GrEffectStage if possible. The texture to
bsalomon@google.com84405e02012-03-05 19:57:21 +0000537// be used is set on grPaint and returned in param act. constantColor has the
538// same meaning as in skPaint2GrPaintNoShader.
539inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
540 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000541 bool constantColor,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000542 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000543 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000544 if (NULL == shader) {
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000545 return skPaint2GrPaintNoShader(dev, skPaint, false, constantColor, grPaint);
546 } else if (!skPaint2GrPaintNoShader(dev, skPaint, true, false, grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000547 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000548 }
549
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000550 SkAutoTUnref<GrEffectRef> effect(shader->asNewEffect(dev->context(), skPaint));
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000551 if (NULL != effect.get()) {
bsalomon@google.com73818dc2013-03-28 13:23:29 +0000552 grPaint->colorStage(kShaderEffectIdx)->setEffect(effect);
rileya@google.com91f319c2012-07-25 17:18:31 +0000553 return true;
554 }
555
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000556 // We still don't have SkColorShader::asNewEffect() implemented.
557 SkShader::GradientInfo info;
558 SkColor color;
reed@google.comac10a2d2010-12-22 21:39:39 +0000559
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000560 info.fColors = &color;
561 info.fColorOffsets = NULL;
562 info.fColorCount = 1;
563 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
564 SkPaint copy(skPaint);
565 copy.setShader(NULL);
566 // modulate the paint alpha by the shader's solid color alpha
567 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
568 copy.setColor(SkColorSetA(color, newA));
569 return skPaint2GrPaintNoShader(dev, copy, false, constantColor, grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000570 }
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000571 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000572}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000573}
reed@google.comac10a2d2010-12-22 21:39:39 +0000574
575///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000576void SkGpuDevice::clear(SkColor color) {
bsalomon@google.com32cf29e2013-01-25 15:03:18 +0000577 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
578 fContext->clear(&rect, SkColor2GrColor(color), fRenderTarget);
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000579 fNeedClear = false;
bsalomon@google.com398109c2011-04-14 18:40:27 +0000580}
581
reed@google.comac10a2d2010-12-22 21:39:39 +0000582void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000583 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000584
bsalomon@google.com5782d712011-01-21 21:03:59 +0000585 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000586 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000587 return;
588 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000589
590 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000591}
592
593// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000594static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000595 kPoints_GrPrimitiveType,
596 kLines_GrPrimitiveType,
597 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000598};
599
600void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000601 size_t count, const SkPoint pts[], const SkPaint& paint) {
epoger@google.comb58772f2013-03-08 09:09:10 +0000602 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000603 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000604
605 SkScalar width = paint.getStrokeWidth();
606 if (width < 0) {
607 return;
608 }
609
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000610 // we only handle hairlines and paints without path effects or mask filters,
611 // else we let the SkDraw call our drawPath()
612 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000613 draw.drawPoints(mode, count, pts, paint, true);
614 return;
615 }
616
bsalomon@google.com5782d712011-01-21 21:03:59 +0000617 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000618 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000619 return;
620 }
621
bsalomon@google.com5782d712011-01-21 21:03:59 +0000622 fContext->drawVertices(grPaint,
623 gPointMode2PrimtiveType[mode],
624 count,
625 (GrPoint*)pts,
626 NULL,
627 NULL,
628 NULL,
629 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000630}
631
reed@google.comc9aa5872011-04-05 21:05:37 +0000632///////////////////////////////////////////////////////////////////////////////
633
reed@google.comac10a2d2010-12-22 21:39:39 +0000634void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000635 const SkPaint& paint) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000636 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000637 CHECK_SHOULD_DRAW(draw, false);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000638
bungeman@google.com79bd8772011-07-18 15:34:08 +0000639 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000640 SkScalar width = paint.getStrokeWidth();
641
642 /*
643 We have special code for hairline strokes, miter-strokes, and fills.
644 Anything else we just call our path code.
645 */
646 bool usePath = doStroke && width > 0 &&
647 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000648 // another two reasons we might need to call drawPath...
649 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000650 usePath = true;
651 }
reed@google.com67db6642011-05-26 11:46:35 +0000652 // until we aa rotated rects...
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000653 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
reed@google.com67db6642011-05-26 11:46:35 +0000654 usePath = true;
655 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000656 // small miter limit means right angles show bevel...
657 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
658 paint.getStrokeMiter() < SK_ScalarSqrt2)
659 {
660 usePath = true;
661 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000662 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000663 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
664 usePath = true;
665 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000666
667 if (usePath) {
668 SkPath path;
669 path.addRect(rect);
670 this->drawPath(draw, path, paint, NULL, true);
671 return;
672 }
673
674 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000675 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000676 return;
677 }
reed@google.com20efde72011-05-09 17:00:02 +0000678 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000679}
680
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000681///////////////////////////////////////////////////////////////////////////////
682
683void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
684 const SkPaint& paint) {
685 CHECK_FOR_NODRAW_ANNOTATION(paint);
686 CHECK_SHOULD_DRAW(draw, false);
687
688 bool usePath = false;
689 // some basic reasons we might need to call drawPath...
690 if (paint.getMaskFilter() || paint.getPathEffect()) {
691 usePath = true;
692 }
693
694 if (usePath) {
695 SkPath path;
696 path.addOval(oval);
697 this->drawPath(draw, path, paint, NULL, true);
698 return;
699 }
700
701 GrPaint grPaint;
702 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
703 return;
704 }
705 SkStrokeRec stroke(paint);
706
707 fContext->drawOval(grPaint, oval, stroke);
708}
709
reed@google.com69302852011-02-16 18:08:07 +0000710#include "SkMaskFilter.h"
711#include "SkBounder.h"
712
bsalomon@google.com85003222012-03-28 14:44:37 +0000713///////////////////////////////////////////////////////////////////////////////
714
715// helpers for applying mask filters
716namespace {
717
bsalomon@google.com85003222012-03-28 14:44:37 +0000718// We prefer to blur small rect with small radius via CPU.
719#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
720#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
721inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
722 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
723 rect.height() <= MIN_GPU_BLUR_SIZE &&
724 radius <= MIN_GPU_BLUR_RADIUS) {
725 return true;
726 }
727 return false;
728}
729
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000730bool drawWithGPUMaskFilter(GrContext* context, const SkPath& devPath, const SkStrokeRec& stroke,
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000731 SkMaskFilter* filter, const SkRegion& clip,
sugoi@google.com12b4e272012-12-06 20:13:11 +0000732 SkBounder* bounder, GrPaint* grp) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000733 SkMaskFilter::BlurInfo info;
734 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000735 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000736 return false;
737 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000738 SkScalar radius = info.fIgnoreTransform ? info.fRadius
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000739 : context->getMatrix().mapRadius(info.fRadius);
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000740 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000741 if (radius <= 0) {
742 return false;
743 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000744
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000745 SkRect srcRect = devPath.getBounds();
bsalomon@google.com85003222012-03-28 14:44:37 +0000746 if (shouldDrawBlurWithCPU(srcRect, radius)) {
747 return false;
748 }
749
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000750 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000751 float sigma3 = sigma * 3.0f;
752
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000753 SkRect clipRect;
754 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000755
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000756 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000757 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
758 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000759 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000760 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000761 SkIRect finalIRect;
762 finalRect.roundOut(&finalIRect);
763 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000764 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000765 }
766 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000767 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000768 }
769 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000770 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000771 GrTextureDesc desc;
772 desc.fFlags = kRenderTarget_GrTextureFlagBit;
773 desc.fWidth = SkScalarCeilToInt(srcRect.width());
774 desc.fHeight = SkScalarCeilToInt(srcRect.height());
775 // We actually only need A8, but it often isn't supported as a
776 // render target so default to RGBA_8888
bsalomon@google.com0342a852012-08-20 19:22:38 +0000777 desc.fConfig = kRGBA_8888_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000778
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000779 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
780 desc.fConfig = kAlpha_8_GrPixelConfig;
781 }
782
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000783 GrAutoScratchTexture pathEntry(context, desc);
784 GrTexture* pathTexture = pathEntry.texture();
785 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000786 return false;
787 }
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000788
robertphillips@google.comccb39502012-10-01 18:25:13 +0000789 SkAutoTUnref<GrTexture> blurTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000790
robertphillips@google.comccb39502012-10-01 18:25:13 +0000791 {
792 GrContext::AutoRenderTarget art(context, pathTexture->asRenderTarget());
793 GrContext::AutoClip ac(context, srcRect);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000794
robertphillips@google.comccb39502012-10-01 18:25:13 +0000795 context->clear(NULL, 0);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000796
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000797 GrPaint tempPaint;
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000798 if (grp->isAntiAlias()) {
799 tempPaint.setAntiAlias(true);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000800 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
801 // blend coeff of zero requires dual source blending support in order
802 // to properly blend partially covered pixels. This means the AA
803 // code path may not be taken. So we use a dst blend coeff of ISA. We
804 // could special case AA draws to a dst surface with known alpha=0 to
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000805 // use a zero dst coeff when dual source blending isn't available.f
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000806 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000807 }
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000808
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000809 GrContext::AutoMatrix am;
810
811 // Draw hard shadow to pathTexture with path top-left at origin using tempPaint.
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000812 SkMatrix translate;
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000813 translate.setTranslate(offset.fX, offset.fY);
814 am.set(context, translate);
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000815 context->drawPath(tempPaint, devPath, stroke);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000816
817 // If we're doing a normal blur, we can clobber the pathTexture in the
818 // gaussianBlur. Otherwise, we need to save it for later compositing.
819 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
skia.committer@gmail.comdc3a4e52012-10-02 02:01:24 +0000820 blurTexture.reset(context->gaussianBlur(pathTexture, isNormalBlur,
robertphillips@google.comccb39502012-10-01 18:25:13 +0000821 srcRect, sigma, sigma));
robertphillips@google.com7d126752012-10-19 12:56:26 +0000822 if (NULL == blurTexture) {
823 return false;
824 }
robertphillips@google.comccb39502012-10-01 18:25:13 +0000825
826 if (!isNormalBlur) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000827 context->setIdentityMatrix();
robertphillips@google.comccb39502012-10-01 18:25:13 +0000828 GrPaint paint;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000829 SkMatrix matrix;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000830 matrix.setIDiv(pathTexture->width(), pathTexture->height());
robertphillips@google.comccb39502012-10-01 18:25:13 +0000831 // Blend pathTexture over blurTexture.
832 context->setRenderTarget(blurTexture->asRenderTarget());
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000833 paint.colorStage(0)->setEffect(
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000834 GrSimpleTextureEffect::Create(pathTexture, matrix))->unref();
robertphillips@google.comccb39502012-10-01 18:25:13 +0000835 if (SkMaskFilter::kInner_BlurType == blurType) {
836 // inner: dst = dst * src
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000837 paint.setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000838 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
839 // solid: dst = src + dst - src * dst
840 // = (1 - dst) * src + 1 * dst
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000841 paint.setBlendFunc(kIDC_GrBlendCoeff, kOne_GrBlendCoeff);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000842 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
843 // outer: dst = dst * (1 - src)
844 // = 0 * src + (1 - src) * dst
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000845 paint.setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000846 }
847 context->drawRect(paint, srcRect);
848 }
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000849 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000850
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000851 GrContext::AutoMatrix am;
852 if (!am.setIdentity(context, grp)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000853 return false;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000854 }
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +0000855
bsalomon@google.com88becf42012-10-05 14:54:42 +0000856 static const int MASK_IDX = GrPaint::kMaxCoverageStages - 1;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000857 // we assume the last mask index is available for use
bsalomon@google.com88becf42012-10-05 14:54:42 +0000858 GrAssert(!grp->isCoverageStageEnabled(MASK_IDX));
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000859
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000860 SkMatrix matrix;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000861 matrix.setTranslate(-finalRect.fLeft, -finalRect.fTop);
862 matrix.postIDiv(blurTexture->width(), blurTexture->height());
863
bsalomon@google.com08283af2012-10-26 13:01:20 +0000864 grp->coverageStage(MASK_IDX)->reset();
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000865 grp->coverageStage(MASK_IDX)->setEffect(
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000866 GrSimpleTextureEffect::Create(blurTexture, matrix))->unref();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000867 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000868 return true;
869}
870
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000871bool drawWithMaskFilter(GrContext* context, const SkPath& devPath,
872 SkMaskFilter* filter, const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000873 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000874 SkMask srcM, dstM;
875
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000876 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
877 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
reed@google.com69302852011-02-16 18:08:07 +0000878 return false;
879 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000880 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000881
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000882 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
reed@google.com69302852011-02-16 18:08:07 +0000883 return false;
884 }
885 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000886 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000887
888 if (clip.quickReject(dstM.fBounds)) {
889 return false;
890 }
891 if (bounder && !bounder->doIRect(dstM.fBounds)) {
892 return false;
893 }
894
895 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000896 // the current clip (and identity matrix) and GrPaint settings
897 GrContext::AutoMatrix am;
898 am.setIdentity(context, grp);
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000899
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000900 GrTextureDesc desc;
901 desc.fWidth = dstM.fBounds.width();
902 desc.fHeight = dstM.fBounds.height();
903 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +0000904
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000905 GrAutoScratchTexture ast(context, desc);
906 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000907
reed@google.com69302852011-02-16 18:08:07 +0000908 if (NULL == texture) {
909 return false;
910 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000911 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000912 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000913
bsalomon@google.com88becf42012-10-05 14:54:42 +0000914 static const int MASK_IDX = GrPaint::kMaxCoverageStages - 1;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000915 // we assume the last mask index is available for use
bsalomon@google.com88becf42012-10-05 14:54:42 +0000916 GrAssert(!grp->isCoverageStageEnabled(MASK_IDX));
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000917
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000918 SkMatrix m;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000919 m.setTranslate(-dstM.fBounds.fLeft*SK_Scalar1, -dstM.fBounds.fTop*SK_Scalar1);
920 m.postIDiv(texture->width(), texture->height());
921
bsalomon@google.com68b58c92013-01-17 16:50:08 +0000922 grp->coverageStage(MASK_IDX)->setEffect(GrSimpleTextureEffect::Create(texture, m))->unref();
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000923 GrRect d;
bsalomon@google.com81712882012-11-01 17:12:34 +0000924 d.setLTRB(SkIntToScalar(dstM.fBounds.fLeft),
925 SkIntToScalar(dstM.fBounds.fTop),
926 SkIntToScalar(dstM.fBounds.fRight),
927 SkIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000928
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000929 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000930 return true;
931}
reed@google.com69302852011-02-16 18:08:07 +0000932
bsalomon@google.com85003222012-03-28 14:44:37 +0000933}
934
935///////////////////////////////////////////////////////////////////////////////
936
reed@google.com0c219b62011-02-16 21:31:18 +0000937void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000938 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000939 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000940 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000941 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000942
bsalomon@google.com5782d712011-01-21 21:03:59 +0000943 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000944 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000945 return;
946 }
947
jvanverth@google.com46d3d392013-01-22 13:34:01 +0000948 // can we cheat, and treat a thin stroke as a hairline w/ coverage
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000949 // if we can, we draw lots faster (raster device does this same test)
950 SkScalar hairlineCoverage;
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000951 bool doHairLine = SkDrawTreatAsHairline(paint, fContext->getMatrix(), &hairlineCoverage);
952 if (doHairLine) {
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000953 grPaint.setCoverage(SkScalarRoundToInt(hairlineCoverage * grPaint.getCoverage()));
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000954 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000955
reed@google.comfe626382011-09-21 13:50:35 +0000956 // If we have a prematrix, apply it to the path, optimizing for the case
957 // where the original path can in fact be modified in place (even though
958 // its parameter type is const).
959 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000960 SkPath tmpPath, effectPath;
reed@google.comac10a2d2010-12-22 21:39:39 +0000961
962 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +0000963 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +0000964
reed@google.come3445642011-02-16 23:20:39 +0000965 if (!pathIsMutable) {
966 result = &tmpPath;
967 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000968 }
reed@google.come3445642011-02-16 23:20:39 +0000969 // should I push prePathMatrix on our MV stack temporarily, instead
970 // of applying it here? See SkDraw.cpp
971 pathPtr->transform(*prePathMatrix, result);
972 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +0000973 }
reed@google.com0c219b62011-02-16 21:31:18 +0000974 // at this point we're done with prePathMatrix
975 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +0000976
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000977 SkStrokeRec stroke(paint);
978 SkPathEffect* pathEffect = paint.getPathEffect();
reed@google.com4bbdeac2013-01-24 21:03:11 +0000979 const SkRect* cullRect = NULL; // TODO: what is our bounds?
980 if (pathEffect && pathEffect->filterPath(&effectPath, *pathPtr, &stroke,
981 cullRect)) {
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000982 pathPtr = &effectPath;
983 }
984
985 if (!pathEffect && doHairLine) {
986 stroke.setHairlineStyle();
reed@google.com0c219b62011-02-16 21:31:18 +0000987 }
988
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000989 if (paint.getMaskFilter()) {
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000990 if (!stroke.isHairlineStyle()) {
991 if (stroke.applyToPath(&tmpPath, *pathPtr)) {
992 pathPtr = &tmpPath;
993 stroke.setFillStyle();
994 }
995 }
996
reed@google.com0c219b62011-02-16 21:31:18 +0000997 // avoid possibly allocating a new path in transform if we can
998 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
999
1000 // transform the path into device space
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001001 pathPtr->transform(fContext->getMatrix(), devPathPtr);
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001002 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, stroke, paint.getMaskFilter(),
sugoi@google.com12b4e272012-12-06 20:13:11 +00001003 *draw.fClip, draw.fBounder, &grPaint)) {
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001004 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
1005 SkPaint::kFill_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001006 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001007 *draw.fClip, draw.fBounder, &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001008 }
reed@google.com69302852011-02-16 18:08:07 +00001009 return;
1010 }
reed@google.com69302852011-02-16 18:08:07 +00001011
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001012 fContext->drawPath(grPaint, *pathPtr, stroke);
reed@google.comac10a2d2010-12-22 21:39:39 +00001013}
1014
bsalomon@google.comfb309512011-11-30 14:13:48 +00001015namespace {
1016
1017inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1018 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1019 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1020 return tilesX * tilesY;
1021}
1022
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001023inline int determine_tile_size(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001024 const SkRect& src,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001025 int maxTextureSize) {
1026 static const int kSmallTileSize = 1 << 10;
1027 if (maxTextureSize <= kSmallTileSize) {
1028 return maxTextureSize;
1029 }
1030
1031 size_t maxTexTotalTileSize;
1032 size_t smallTotalTileSize;
1033
robertphillips@google.combac6b052012-09-28 18:06:49 +00001034 SkIRect iSrc;
1035 src.roundOut(&iSrc);
1036
1037 maxTexTotalTileSize = get_tile_count(iSrc.fLeft,
1038 iSrc.fTop,
1039 iSrc.fRight,
1040 iSrc.fBottom,
1041 maxTextureSize);
1042 smallTotalTileSize = get_tile_count(iSrc.fLeft,
1043 iSrc.fTop,
1044 iSrc.fRight,
1045 iSrc.fBottom,
1046 kSmallTileSize);
1047
bsalomon@google.comfb309512011-11-30 14:13:48 +00001048 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1049 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1050
1051 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1052 return kSmallTileSize;
1053 } else {
1054 return maxTextureSize;
1055 }
1056}
1057}
1058
1059bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001060 const GrTextureParams& params,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001061 const SkRect* srcRectPtr) const {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001062 // if bitmap is explictly texture backed then just use the texture
1063 if (NULL != bitmap.getTexture()) {
1064 return false;
1065 }
1066 // if it's larger than the max texture size, then we have no choice but
1067 // tiling
1068 const int maxTextureSize = fContext->getMaxTextureSize();
1069 if (bitmap.width() > maxTextureSize ||
1070 bitmap.height() > maxTextureSize) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001071 return true;
1072 }
1073 // if we are going to have to draw the whole thing, then don't tile
1074 if (NULL == srcRectPtr) {
1075 return false;
1076 }
1077 // if the entire texture is already in our cache then no reason to tile it
bsalomon@google.com0797c2c2012-12-20 15:13:01 +00001078 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001079 return false;
1080 }
1081
1082 // At this point we know we could do the draw by uploading the entire bitmap
1083 // as a texture. However, if the texture would be large compared to the
1084 // cache size and we don't require most of it for this draw then tile to
1085 // reduce the amount of upload and cache spill.
1086
1087 // assumption here is that sw bitmap size is a good proxy for its size as
1088 // a texture
1089 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001090 size_t cacheSize;
1091 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001092 if (bmpSize < cacheSize / 2) {
1093 return false;
1094 }
1095
robertphillips@google.combac6b052012-09-28 18:06:49 +00001096 SkScalar fracUsed = SkScalarMul(srcRectPtr->width() / bitmap.width(),
1097 srcRectPtr->height() / bitmap.height());
1098 if (fracUsed <= SK_ScalarHalf) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001099 return true;
1100 } else {
1101 return false;
1102 }
1103}
1104
reed@google.comac10a2d2010-12-22 21:39:39 +00001105void SkGpuDevice::drawBitmap(const SkDraw& draw,
1106 const SkBitmap& bitmap,
1107 const SkIRect* srcRectPtr,
1108 const SkMatrix& m,
1109 const SkPaint& paint) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001110
1111 SkRect tmp;
1112 SkRect* tmpPtr = NULL;
1113
1114 // convert from SkIRect to SkRect
1115 if (NULL != srcRectPtr) {
1116 tmp.set(*srcRectPtr);
1117 tmpPtr = &tmp;
1118 }
1119
1120 // We cannot call drawBitmapRect here since 'm' could be anything
1121 this->drawBitmapCommon(draw, bitmap, tmpPtr, m, paint);
1122}
1123
1124void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1125 const SkBitmap& bitmap,
1126 const SkRect* srcRectPtr,
1127 const SkMatrix& m,
1128 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001129 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001130
robertphillips@google.combac6b052012-09-28 18:06:49 +00001131 SkRect srcRect;
reed@google.comac10a2d2010-12-22 21:39:39 +00001132 if (NULL == srcRectPtr) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001133 srcRect.set(0, 0, SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001134 } else {
1135 srcRect = *srcRectPtr;
1136 }
1137
junov@google.comd935cfb2011-06-27 20:48:23 +00001138 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001139 // Convert the bitmap to a shader so that the rect can be drawn
1140 // through drawRect, which supports mask filters.
robertphillips@google.combac6b052012-09-28 18:06:49 +00001141 SkMatrix newM(m);
junov@google.com1d329782011-07-28 20:10:09 +00001142 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001143 const SkBitmap* bitmapPtr = &bitmap;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001144 if (NULL != srcRectPtr) {
1145 SkIRect iSrc;
1146 srcRect.roundOut(&iSrc);
1147 if (!bitmap.extractSubset(&tmp, iSrc)) {
epoger@google.com9ef2d832011-07-01 21:12:20 +00001148 return; // extraction failed
1149 }
1150 bitmapPtr = &tmp;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001151 srcRect.offset(SkIntToScalar(-iSrc.fLeft), SkIntToScalar(-iSrc.fTop));
1152 // The source rect has changed so update the matrix
1153 newM.preTranslate(SkIntToScalar(iSrc.fLeft), SkIntToScalar(iSrc.fTop));
junov@google.comd935cfb2011-06-27 20:48:23 +00001154 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001155
junov@google.comd935cfb2011-06-27 20:48:23 +00001156 SkPaint paintWithTexture(paint);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001157 paintWithTexture.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
junov@google.comd935cfb2011-06-27 20:48:23 +00001158 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001159
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001160 // Transform 'newM' needs to be concatenated to the current matrix,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001161 // rather than transforming the primitive directly, so that 'newM' will
junov@google.com1d329782011-07-28 20:10:09 +00001162 // also affect the behavior of the mask filter.
1163 SkMatrix drawMatrix;
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001164 drawMatrix.setConcat(fContext->getMatrix(), newM);
junov@google.com1d329782011-07-28 20:10:09 +00001165 SkDraw transformedDraw(draw);
1166 transformedDraw.fMatrix = &drawMatrix;
1167
robertphillips@google.combac6b052012-09-28 18:06:49 +00001168 this->drawRect(transformedDraw, srcRect, paintWithTexture);
junov@google.com1d329782011-07-28 20:10:09 +00001169
junov@google.comd935cfb2011-06-27 20:48:23 +00001170 return;
1171 }
1172
bsalomon@google.com5782d712011-01-21 21:03:59 +00001173 GrPaint grPaint;
skia.committer@gmail.com4e73aa12013-01-09 02:01:30 +00001174
humper@google.com9aaf36d2013-01-08 16:08:01 +00001175 bool alphaOnly = !(SkBitmap::kA8_Config == bitmap.config());
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001176 if (!skPaint2GrPaintNoShader(this, paint, alphaOnly, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001177 return;
1178 }
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001179 GrTextureParams params;
1180 params.setBilerp(paint.isFilterBitmap());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001181
robertphillips@google.combac6b052012-09-28 18:06:49 +00001182 if (!this->shouldTileBitmap(bitmap, params, srcRectPtr)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001183 // take the simple case
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001184 this->internalDrawBitmap(bitmap, srcRect, m, params, &grPaint);
robertphillips@google.combac6b052012-09-28 18:06:49 +00001185 } else {
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001186 this->drawTiledBitmap(bitmap, srcRect, m, params, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001187 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001188}
1189
1190// Break 'bitmap' into several tiles to draw it since it has already
1191// been determined to be too large to fit in VRAM
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001192void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001193 const SkRect& srcRect,
1194 const SkMatrix& m,
1195 const GrTextureParams& params,
1196 GrPaint* grPaint) {
1197 const int maxTextureSize = fContext->getMaxTextureSize();
1198
1199 int tileSize = determine_tile_size(bitmap, srcRect, maxTextureSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001200
reed@google.comac10a2d2010-12-22 21:39:39 +00001201 // compute clip bounds in local coordinates
robertphillips@google.combac6b052012-09-28 18:06:49 +00001202 SkRect clipRect;
reed@google.comac10a2d2010-12-22 21:39:39 +00001203 {
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001204 const GrRenderTarget* rt = fContext->getRenderTarget();
1205 clipRect.setWH(SkIntToScalar(rt->width()), SkIntToScalar(rt->height()));
1206 if (!fContext->getClip()->fClipStack->intersectRectWithClip(&clipRect)) {
1207 return;
1208 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001209 SkMatrix matrix, inverse;
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001210 matrix.setConcat(fContext->getMatrix(), m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001211 if (!matrix.invert(&inverse)) {
1212 return;
1213 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001214 inverse.mapRect(&clipRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001215 }
1216
bsalomon@google.comfb309512011-11-30 14:13:48 +00001217 int nx = bitmap.width() / tileSize;
1218 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001219 for (int x = 0; x <= nx; x++) {
1220 for (int y = 0; y <= ny; y++) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001221 SkRect tileR;
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001222 tileR.set(SkIntToScalar(x * tileSize),
robertphillips@google.combac6b052012-09-28 18:06:49 +00001223 SkIntToScalar(y * tileSize),
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001224 SkIntToScalar((x + 1) * tileSize),
robertphillips@google.combac6b052012-09-28 18:06:49 +00001225 SkIntToScalar((y + 1) * tileSize));
1226
1227 if (!SkRect::Intersects(tileR, clipRect)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001228 continue;
1229 }
1230
robertphillips@google.combac6b052012-09-28 18:06:49 +00001231 if (!tileR.intersect(srcRect)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001232 continue;
1233 }
1234
1235 SkBitmap tmpB;
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001236 SkIRect iTileR;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001237 tileR.roundOut(&iTileR);
1238 if (bitmap.extractSubset(&tmpB, iTileR)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001239 // now offset it to make it "local" to our tmp bitmap
robertphillips@google.combac6b052012-09-28 18:06:49 +00001240 tileR.offset(SkIntToScalar(-iTileR.fLeft), SkIntToScalar(-iTileR.fTop));
reed@google.comac10a2d2010-12-22 21:39:39 +00001241 SkMatrix tmpM(m);
skia.committer@gmail.comdc3a4e52012-10-02 02:01:24 +00001242 tmpM.preTranslate(SkIntToScalar(iTileR.fLeft),
robertphillips@google.comffad46b2012-10-01 14:32:51 +00001243 SkIntToScalar(iTileR.fTop));
1244
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001245 this->internalDrawBitmap(tmpB, tileR, tmpM, params, grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001246 }
1247 }
1248 }
1249}
1250
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001251namespace {
1252
1253bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1254 // detect pixel disalignment
1255 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1256 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
rmistry@google.comd6176b02012-08-23 18:14:13 +00001257 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001258 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1259 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1260 COLOR_BLEED_TOLERANCE &&
1261 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1262 COLOR_BLEED_TOLERANCE) {
1263 return true;
1264 }
1265 return false;
1266}
1267
1268bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1269 const SkMatrix& m) {
1270 // Only gets called if hasAlignedSamples returned false.
1271 // So we can assume that sampling is axis aligned but not texel aligned.
1272 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001273 SkRect innerSrcRect(srcRect), innerTransformedRect,
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001274 outerTransformedRect(transformedRect);
1275 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1276 m.mapRect(&innerTransformedRect, innerSrcRect);
1277
1278 // The gap between outerTransformedRect and innerTransformedRect
1279 // represents the projection of the source border area, which is
1280 // problematic for color bleeding. We must check whether any
1281 // destination pixels sample the border area.
1282 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1283 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1284 SkIRect outer, inner;
1285 outerTransformedRect.round(&outer);
1286 innerTransformedRect.round(&inner);
1287 // If the inner and outer rects round to the same result, it means the
1288 // border does not overlap any pixel centers. Yay!
1289 return inner != outer;
1290}
1291
1292} // unnamed namespace
1293
reed@google.comac10a2d2010-12-22 21:39:39 +00001294/*
1295 * This is called by drawBitmap(), which has to handle images that may be too
1296 * large to be represented by a single texture.
1297 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001298 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1299 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001300 */
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001301void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001302 const SkRect& srcRect,
reed@google.comac10a2d2010-12-22 21:39:39 +00001303 const SkMatrix& m,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001304 const GrTextureParams& params,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001305 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001306 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1307 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001308
reed@google.com9c49bc32011-07-07 13:42:37 +00001309 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001310 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001311 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001312 return;
1313 }
1314
reed@google.comac10a2d2010-12-22 21:39:39 +00001315 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001316 SkAutoCachedTexture act(this, bitmap, &params, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001317 if (NULL == texture) {
1318 return;
1319 }
1320
robertphillips@google.combac6b052012-09-28 18:06:49 +00001321 GrRect dstRect(srcRect);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001322 GrRect paintRect;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001323 SkScalar wInv = SkScalarInvert(SkIntToScalar(bitmap.width()));
1324 SkScalar hInv = SkScalarInvert(SkIntToScalar(bitmap.height()));
1325 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1326 SkScalarMul(srcRect.fTop, hInv),
1327 SkScalarMul(srcRect.fRight, wInv),
1328 SkScalarMul(srcRect.fBottom, hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001329
rmistry@google.comd6176b02012-08-23 18:14:13 +00001330 bool needsTextureDomain = false;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001331 if (params.isBilerp()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001332 // Need texture domain if drawing a sub rect.
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001333 needsTextureDomain = srcRect.width() < bitmap.width() ||
robertphillips@google.combac6b052012-09-28 18:06:49 +00001334 srcRect.height() < bitmap.height();
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001335 if (m.rectStaysRect() && fContext->getMatrix().rectStaysRect()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001336 // sampling is axis-aligned
robertphillips@google.combac6b052012-09-28 18:06:49 +00001337 GrRect transformedRect;
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001338 SkMatrix srcToDeviceMatrix(m);
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001339 srcToDeviceMatrix.postConcat(fContext->getMatrix());
robertphillips@google.combac6b052012-09-28 18:06:49 +00001340 srcToDeviceMatrix.mapRect(&transformedRect, srcRect);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001341
robertphillips@google.combac6b052012-09-28 18:06:49 +00001342 if (hasAlignedSamples(srcRect, transformedRect)) {
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001343 // We could also turn off filtering here (but we already did a cache lookup with
1344 // params).
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001345 needsTextureDomain = false;
1346 } else {
1347 needsTextureDomain = needsTextureDomain &&
robertphillips@google.combac6b052012-09-28 18:06:49 +00001348 mayColorBleed(srcRect, transformedRect, m);
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001349 }
1350 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001351 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001352
1353 GrRect textureDomain = GrRect::MakeEmpty();
bsalomon@google.com0ac6af42013-01-16 15:16:18 +00001354 SkAutoTUnref<GrEffectRef> effect;
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001355 if (needsTextureDomain) {
1356 // Use a constrained texture domain to avoid color bleeding
bsalomon@google.com81712882012-11-01 17:12:34 +00001357 SkScalar left, top, right, bottom;
1358 if (srcRect.width() > SK_Scalar1) {
1359 SkScalar border = SK_ScalarHalf / bitmap.width();
junov@google.com6acc9b32011-05-16 18:32:07 +00001360 left = paintRect.left() + border;
1361 right = paintRect.right() - border;
1362 } else {
bsalomon@google.com81712882012-11-01 17:12:34 +00001363 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
junov@google.com6acc9b32011-05-16 18:32:07 +00001364 }
bsalomon@google.com81712882012-11-01 17:12:34 +00001365 if (srcRect.height() > SK_Scalar1) {
1366 SkScalar border = SK_ScalarHalf / bitmap.height();
junov@google.com6acc9b32011-05-16 18:32:07 +00001367 top = paintRect.top() + border;
1368 bottom = paintRect.bottom() - border;
1369 } else {
bsalomon@google.com81712882012-11-01 17:12:34 +00001370 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
junov@google.com6acc9b32011-05-16 18:32:07 +00001371 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001372 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +00001373 effect.reset(GrTextureDomainEffect::Create(texture,
1374 SkMatrix::I(),
1375 textureDomain,
1376 GrTextureDomainEffect::kClamp_WrapMode,
1377 params.isBilerp()));
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001378 } else {
bsalomon@google.com68b58c92013-01-17 16:50:08 +00001379 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
junov@google.com6acc9b32011-05-16 18:32:07 +00001380 }
bsalomon@google.com73818dc2013-03-28 13:23:29 +00001381 grPaint->colorStage(kBitmapEffectIdx)->setEffect(effect);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001382 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001383}
1384
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001385namespace {
1386
bsalomon@google.comf271cc72012-10-24 19:35:13 +00001387void apply_effect(GrContext* context,
1388 GrTexture* srcTexture,
1389 GrTexture* dstTexture,
1390 const GrRect& rect,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +00001391 GrEffectRef* effect) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001392 SkASSERT(srcTexture && srcTexture->getContext() == context);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001393 GrContext::AutoMatrix am;
1394 am.setIdentity(context);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001395 GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget());
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001396 GrContext::AutoClip acs(context, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001397
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001398 GrPaint paint;
bsalomon@google.comdbe49f72012-11-05 16:36:02 +00001399 paint.colorStage(0)->setEffect(effect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001400 context->drawRect(paint, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001401}
1402
1403};
1404
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001405static SkBitmap wrap_texture(GrTexture* texture) {
1406 SkBitmap result;
1407 bool dummy;
1408 SkBitmap::Config config = grConfig2skConfig(texture->config(), &dummy);
1409 result.setConfig(config, texture->width(), texture->height());
1410 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (texture)))->unref();
1411 return result;
1412}
1413
1414static bool filter_texture(SkDevice* device, GrContext* context,
1415 GrTexture* texture, SkImageFilter* filter,
1416 int w, int h, SkBitmap* result) {
reed@google.com8926b162012-03-23 15:36:36 +00001417 GrAssert(filter);
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001418 SkDeviceImageFilterProxy proxy(device);
reed@google.com8926b162012-03-23 15:36:36 +00001419
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001420 GrTextureDesc desc;
1421 desc.fFlags = kRenderTarget_GrTextureFlagBit,
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001422 desc.fWidth = w;
1423 desc.fHeight = h;
bsalomon@google.com0342a852012-08-20 19:22:38 +00001424 desc.fConfig = kRGBA_8888_GrPixelConfig;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +00001425 GrEffectRef* effect;
reed@google.com8926b162012-03-23 15:36:36 +00001426
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001427 if (filter->canFilterImageGPU()) {
senorblanco@chromium.org985fa792012-10-24 15:14:26 +00001428 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1429 // filter. Also set the clip wide open and the matrix to identity.
1430 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001431 return filter->filterImageGPU(&proxy, wrap_texture(texture), result);
bsalomon@google.com021fc732012-10-25 12:47:42 +00001432 } else if (filter->asNewEffect(&effect, texture)) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001433 GrAutoScratchTexture dst(context, desc);
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001434 SkRect r = SkRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h));
1435 apply_effect(context, texture, dst.texture(), r, effect);
1436 SkAutoTUnref<GrTexture> resultTex(dst.detach());
bsalomon@google.com021fc732012-10-25 12:47:42 +00001437 effect->unref();
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001438 *result = wrap_texture(resultTex.get());
1439 return true;
1440 } else {
1441 return false;
reed@google.com8926b162012-03-23 15:36:36 +00001442 }
reed@google.com8926b162012-03-23 15:36:36 +00001443}
1444
reed@google.comac10a2d2010-12-22 21:39:39 +00001445void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001446 int left, int top, const SkPaint& paint) {
1447 // drawSprite is defined to be in device coords.
1448 CHECK_SHOULD_DRAW(draw, true);
reed@google.comac10a2d2010-12-22 21:39:39 +00001449
reed@google.com8926b162012-03-23 15:36:36 +00001450 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001451 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1452 return;
1453 }
1454
reed@google.com76dd2772012-01-05 21:15:07 +00001455 int w = bitmap.width();
1456 int h = bitmap.height();
1457
bsalomon@google.com5782d712011-01-21 21:03:59 +00001458 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001459 if(!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001460 return;
1461 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001462
bsalomon@google.com73818dc2013-03-28 13:23:29 +00001463 GrEffectStage* stage = grPaint.colorStage(kBitmapEffectIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001464
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001465 GrTexture* texture;
bsalomon@google.com08283af2012-10-26 13:01:20 +00001466 stage->reset();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001467 // draw sprite uses the default texture params
1468 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
bsalomon@google.com73818dc2013-03-28 13:23:29 +00001469 grPaint.colorStage(kBitmapEffectIdx)->setEffect(
bsalomon@google.com68b58c92013-01-17 16:50:08 +00001470 GrSimpleTextureEffect::Create(texture, SkMatrix::I()))->unref();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001471
reed@google.com8926b162012-03-23 15:36:36 +00001472 SkImageFilter* filter = paint.getImageFilter();
1473 if (NULL != filter) {
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001474 SkBitmap filterBitmap;
1475 if (filter_texture(this, fContext, texture, filter, w, h, &filterBitmap)) {
bsalomon@google.com73818dc2013-03-28 13:23:29 +00001476 grPaint.colorStage(kBitmapEffectIdx)->setEffect(
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001477 GrSimpleTextureEffect::Create((GrTexture*) filterBitmap.getTexture(), SkMatrix::I()))->unref();
1478 texture = (GrTexture*) filterBitmap.getTexture();
1479 w = filterBitmap.width();
1480 h = filterBitmap.height();
reed@google.com8926b162012-03-23 15:36:36 +00001481 }
reed@google.com76dd2772012-01-05 21:15:07 +00001482 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001483
bsalomon@google.com5782d712011-01-21 21:03:59 +00001484 fContext->drawRectToRect(grPaint,
bsalomon@google.com81712882012-11-01 17:12:34 +00001485 GrRect::MakeXYWH(SkIntToScalar(left),
1486 SkIntToScalar(top),
1487 SkIntToScalar(w),
1488 SkIntToScalar(h)),
1489 GrRect::MakeWH(SK_Scalar1 * w / texture->width(),
1490 SK_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001491}
1492
reed@google.com33535f32012-09-25 15:37:50 +00001493void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1494 const SkRect* src, const SkRect& dst,
1495 const SkPaint& paint) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001496 SkMatrix matrix;
1497 SkRect bitmapBounds, tmpSrc;
1498
1499 bitmapBounds.set(0, 0,
1500 SkIntToScalar(bitmap.width()),
1501 SkIntToScalar(bitmap.height()));
1502
reed@google.com33535f32012-09-25 15:37:50 +00001503 // Compute matrix from the two rectangles
robertphillips@google.combac6b052012-09-28 18:06:49 +00001504 if (NULL != src) {
1505 tmpSrc = *src;
1506 } else {
1507 tmpSrc = bitmapBounds;
1508 }
1509 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1510
1511 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1512 if (NULL != src) {
1513 if (!bitmapBounds.contains(tmpSrc)) {
1514 if (!tmpSrc.intersect(bitmapBounds)) {
1515 return; // nothing to draw
reed@google.com33535f32012-09-25 15:37:50 +00001516 }
reed@google.com33535f32012-09-25 15:37:50 +00001517 }
reed@google.com33535f32012-09-25 15:37:50 +00001518 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +00001519
robertphillips@google.combac6b052012-09-28 18:06:49 +00001520 this->drawBitmapCommon(draw, bitmap, &tmpSrc, matrix, paint);
reed@google.com33535f32012-09-25 15:37:50 +00001521}
1522
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001523void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001524 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001525 // clear of the source device must occur before CHECK_SHOULD_DRAW
1526 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1527 if (dev->fNeedClear) {
1528 // TODO: could check here whether we really need to draw at all
1529 dev->clear(0x0);
1530 }
1531
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001532 // drawDevice is defined to be in device coords.
1533 CHECK_SHOULD_DRAW(draw, true);
reed@google.comac10a2d2010-12-22 21:39:39 +00001534
bsalomon@google.com5782d712011-01-21 21:03:59 +00001535 GrPaint grPaint;
bsalomon@google.com73818dc2013-03-28 13:23:29 +00001536 grPaint.colorStage(kBitmapEffectIdx)->reset();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001537 if (!dev->bindDeviceAsTexture(&grPaint) ||
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001538 !skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001539 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001540 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001541
bsalomon@google.com73818dc2013-03-28 13:23:29 +00001542 GrTexture* devTex = (*grPaint.getColorStage(kBitmapEffectIdx).getEffect())->texture(0);
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001543 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001544
senorblanco@chromium.org514b9222013-01-18 21:53:12 +00001545 const SkBitmap& bm = dev->accessBitmap(false);
1546 int w = bm.width();
1547 int h = bm.height();
1548
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001549 SkImageFilter* filter = paint.getImageFilter();
1550 if (NULL != filter) {
1551 SkBitmap filterBitmap;
1552 if (filter_texture(this, fContext, devTex, filter, w, h, &filterBitmap)) {
bsalomon@google.com73818dc2013-03-28 13:23:29 +00001553 grPaint.colorStage(kBitmapEffectIdx)->setEffect(
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001554 GrSimpleTextureEffect::Create((GrTexture*) filterBitmap.getTexture(), SkMatrix::I()))->unref();
1555 devTex = (GrTexture*) filterBitmap.getTexture();
1556 w = filterBitmap.width();
1557 h = filterBitmap.height();
1558 }
1559 }
1560
bsalomon@google.com81712882012-11-01 17:12:34 +00001561 GrRect dstRect = GrRect::MakeXYWH(SkIntToScalar(x),
1562 SkIntToScalar(y),
1563 SkIntToScalar(w),
1564 SkIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001565
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001566 // The device being drawn may not fill up its texture (saveLayer uses
1567 // the approximate ).
bsalomon@google.com81712882012-11-01 17:12:34 +00001568 GrRect srcRect = GrRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1569 SK_Scalar1 * h / devTex->height());
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001570
1571 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001572}
1573
reed@google.com8926b162012-03-23 15:36:36 +00001574bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
bsalomon@google.com8ea78d82012-10-24 20:11:30 +00001575 if (!filter->asNewEffect(NULL, NULL) &&
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001576 !filter->canFilterImageGPU()) {
reed@google.com76dd2772012-01-05 21:15:07 +00001577 return false;
1578 }
reed@google.com8926b162012-03-23 15:36:36 +00001579 return true;
1580}
1581
1582bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1583 const SkMatrix& ctm,
1584 SkBitmap* result, SkIPoint* offset) {
1585 // want explicitly our impl, so guard against a subclass of us overriding it
1586 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001587 return false;
1588 }
reed@google.com8926b162012-03-23 15:36:36 +00001589
1590 SkAutoLockPixels alp(src, !src.getTexture());
1591 if (!src.getTexture() && !src.readyToDraw()) {
1592 return false;
1593 }
1594
1595 GrPaint paint;
reed@google.com8926b162012-03-23 15:36:36 +00001596
reed@google.com8926b162012-03-23 15:36:36 +00001597 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001598 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1599 // must be pushed upstack.
1600 SkAutoCachedTexture act(this, src, NULL, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001601
senorblanco@chromium.orgc2594f42013-01-30 19:08:47 +00001602 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), result);
reed@google.com76dd2772012-01-05 21:15:07 +00001603}
1604
reed@google.comac10a2d2010-12-22 21:39:39 +00001605///////////////////////////////////////////////////////////////////////////////
1606
1607// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001608static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001609 kTriangles_GrPrimitiveType,
1610 kTriangleStrip_GrPrimitiveType,
1611 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001612};
1613
1614void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1615 int vertexCount, const SkPoint vertices[],
1616 const SkPoint texs[], const SkColor colors[],
1617 SkXfermode* xmode,
1618 const uint16_t indices[], int indexCount,
1619 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001620 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001621
bsalomon@google.com5782d712011-01-21 21:03:59 +00001622 GrPaint grPaint;
bsalomon@google.com5782d712011-01-21 21:03:59 +00001623 // we ignore the shader if texs is null.
1624 if (NULL == texs) {
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001625 if (!skPaint2GrPaintNoShader(this, paint, false, NULL == colors, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001626 return;
1627 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001628 } else {
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001629 if (!skPaint2GrPaintShader(this, paint, NULL == colors, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001630 return;
1631 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001632 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001633
1634 if (NULL != xmode && NULL != texs && NULL != colors) {
reed@google.com8d3cd7a2013-01-30 21:36:11 +00001635 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001636 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1637#if 0
1638 return
1639#endif
1640 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001641 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001642
bsalomon@google.com498776a2011-08-16 19:20:44 +00001643 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1644 if (NULL != colors) {
1645 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001646 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001647 for (int i = 0; i < vertexCount; ++i) {
rileya@google.com24f3ad12012-07-18 21:47:40 +00001648 convertedColors[i] = SkColor2GrColor(colors[i]);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001649 }
1650 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001651 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001652 fContext->drawVertices(grPaint,
1653 gVertexMode2PrimitiveType[vmode],
1654 vertexCount,
1655 (GrPoint*) vertices,
1656 (GrPoint*) texs,
1657 colors,
1658 indices,
1659 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001660}
1661
1662///////////////////////////////////////////////////////////////////////////////
1663
1664static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001665 GrFontScaler* scaler = (GrFontScaler*)data;
1666 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001667}
1668
1669static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1670 void* auxData;
1671 GrFontScaler* scaler = NULL;
1672 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1673 scaler = (GrFontScaler*)auxData;
1674 }
1675 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001676 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001677 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1678 }
1679 return scaler;
1680}
1681
1682static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1683 SkFixed fx, SkFixed fy,
1684 const SkGlyph& glyph) {
1685 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1686
bungeman@google.com15865a72012-01-11 16:28:04 +00001687 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001688
1689 if (NULL == procs->fFontScaler) {
1690 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1691 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001692
bungeman@google.com15865a72012-01-11 16:28:04 +00001693 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1694 glyph.getSubXFixed(),
1695 glyph.getSubYFixed()),
1696 SkFixedFloorToFixed(fx),
1697 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001698 procs->fFontScaler);
1699}
1700
bsalomon@google.com5782d712011-01-21 21:03:59 +00001701SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001702
1703 // deferred allocation
1704 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001705 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001706 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1707 fDrawProcs->fContext = fContext;
1708 }
1709
1710 // init our (and GL's) state
1711 fDrawProcs->fTextContext = context;
1712 fDrawProcs->fFontScaler = NULL;
1713 return fDrawProcs;
1714}
1715
1716void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1717 size_t byteLength, SkScalar x, SkScalar y,
1718 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001719 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001720
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001721 if (fContext->getMatrix().hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001722 // this guy will just call our drawPath()
1723 draw.drawText((const char*)text, byteLength, x, y, paint);
1724 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001725 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001726
1727 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001728 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001729 return;
1730 }
bsalomon@google.com0e354aa2012-10-08 20:44:25 +00001731 GrTextContext context(fContext, grPaint);
tomhudson@google.com375ff852012-06-29 18:37:57 +00001732 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001733 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1734 }
1735}
1736
1737void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1738 size_t byteLength, const SkScalar pos[],
1739 SkScalar constY, int scalarsPerPos,
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.drawPosText((const char*)text, byteLength, pos, constY,
1746 scalarsPerPos, paint);
1747 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001748 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001749
1750 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001751 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001752 return;
1753 }
bsalomon@google.com0e354aa2012-10-08 20:44:25 +00001754 GrTextContext context(fContext, grPaint);
tomhudson@google.com375ff852012-06-29 18:37:57 +00001755 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001756 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1757 scalarsPerPos, paint);
1758 }
1759}
1760
1761void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1762 size_t len, const SkPath& path,
1763 const SkMatrix* m, const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001764 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001765
1766 SkASSERT(draw.fDevice == this);
1767 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1768}
1769
1770///////////////////////////////////////////////////////////////////////////////
1771
reed@google.comf67e4cf2011-03-15 20:56:58 +00001772bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1773 if (!paint.isLCDRenderText()) {
1774 // we're cool with the paint as is
1775 return false;
1776 }
1777
1778 if (paint.getShader() ||
1779 paint.getXfermode() || // unless its srcover
1780 paint.getMaskFilter() ||
1781 paint.getRasterizer() ||
1782 paint.getColorFilter() ||
1783 paint.getPathEffect() ||
1784 paint.isFakeBoldText() ||
1785 paint.getStyle() != SkPaint::kFill_Style) {
1786 // turn off lcd
1787 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1788 flags->fHinting = paint.getHinting();
1789 return true;
1790 }
1791 // we're cool with the paint as is
1792 return false;
1793}
1794
reed@google.com75d939b2011-12-07 15:07:23 +00001795void SkGpuDevice::flush() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +00001796 DO_DEFERRED_CLEAR();
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001797 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001798}
1799
reed@google.comf67e4cf2011-03-15 20:56:58 +00001800///////////////////////////////////////////////////////////////////////////////
1801
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001802SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1803 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001804 bool isOpaque,
1805 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001806 GrTextureDesc desc;
1807 desc.fConfig = fRenderTarget->config();
1808 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1809 desc.fWidth = width;
1810 desc.fHeight = height;
1811 desc.fSampleCnt = fRenderTarget->numSamples();
1812
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001813 SkAutoTUnref<GrTexture> texture;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001814 // Skia's convention is to only clear a device if it is non-opaque.
1815 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001816
1817#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1818 // layers are never draw in repeat modes, so we can request an approx
1819 // match and ignore any padding.
bsalomon@google.com0797c2c2012-12-20 15:13:01 +00001820 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1821 GrContext::kApprox_ScratchTexMatch :
1822 GrContext::kExact_ScratchTexMatch;
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001823 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001824#else
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001825 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001826#endif
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001827 if (NULL != texture.get()) {
1828 return SkNEW_ARGS(SkGpuDevice,(fContext, texture, needClear));
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001829 } else {
bsalomon@google.com95ed55a2013-01-24 14:46:47 +00001830 GrPrintf("---- failed to create compatible device texture [%d %d]\n", width, height);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001831 return NULL;
1832 }
1833}
1834
1835SkGpuDevice::SkGpuDevice(GrContext* context,
1836 GrTexture* texture,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001837 bool needClear)
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001838 : SkDevice(make_bitmap(context, texture->asRenderTarget())) {
1839
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001840 GrAssert(texture && texture->asRenderTarget());
bsalomon@google.com8090e652012-08-28 15:07:11 +00001841 // This constructor is called from onCreateCompatibleDevice. It has locked the RT in the texture
1842 // cache. We pass true for the third argument so that it will get unlocked.
1843 this->initFromRenderTarget(context, texture->asRenderTarget(), true);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001844 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001845}