blob: f205569fa024daa04c0c1892d1d9973fdd79484e [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"
epoger@google.comec3ed6a2011-07-28 14:26:00 +000011
reed@google.comac10a2d2010-12-22 21:39:39 +000012#include "GrContext.h"
13#include "GrTextContext.h"
14
robertphillips@google.come9c04692012-06-29 00:30:13 +000015#include "SkGrTexturePixelRef.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000016
Scroggo97c88c22011-05-11 14:05:25 +000017#include "SkColorFilter.h"
senorblanco@chromium.org9c397442012-09-27 21:57:45 +000018#include "SkDeviceImageFilterProxy.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000019#include "SkDrawProcs.h"
20#include "SkGlyphCache.h"
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +000021#include "SkImageFilter.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000022#include "SkPathEffect.h"
23#include "SkStroke.h"
reed@google.comc9aa5872011-04-05 21:05:37 +000024#include "SkUtils.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000025
bsalomon@google.com06cd7322012-03-30 18:45:35 +000026#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
reed@google.comac10a2d2010-12-22 21:39:39 +000027
28#if 0
29 extern bool (*gShouldDrawProc)();
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +000030 #define CHECK_SHOULD_DRAW(draw, forceI) \
reed@google.comac10a2d2010-12-22 21:39:39 +000031 do { \
32 if (gShouldDrawProc && !gShouldDrawProc()) return; \
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +000033 this->prepareDraw(draw, forceI); \
reed@google.comac10a2d2010-12-22 21:39:39 +000034 } while (0)
35#else
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +000036 #define CHECK_SHOULD_DRAW(draw, forceI) this->prepareDraw(draw, forceI)
reed@google.comac10a2d2010-12-22 21:39:39 +000037#endif
38
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000039// we use the same texture slot on GrPaint for bitmaps and shaders
40// (since drawBitmap, drawSprite, and drawDevice ignore skia's shader)
41enum {
42 kBitmapTextureIdx = 0,
twiz@google.com58071162012-07-18 21:41:50 +000043 kShaderTextureIdx = 0,
44 kColorFilterTextureIdx = 1
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000045};
46
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +000047#define MAX_BLUR_SIGMA 4.0f
48// FIXME: This value comes from from SkBlurMaskFilter.cpp.
49// Should probably be put in a common header someplace.
50#define MAX_BLUR_RADIUS SkIntToScalar(128)
51// This constant approximates the scaling done in the software path's
52// "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
53// IMHO, it actually should be 1: we blur "less" than we should do
54// according to the CSS and canvas specs, simply because Safari does the same.
55// Firefox used to do the same too, until 4.0 where they fixed it. So at some
56// point we should probably get rid of these scaling constants and rebaseline
57// all the blur tests.
58#define BLUR_SIGMA_SCALE 0.6f
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000059// This constant represents the screen alignment criterion in texels for
rmistry@google.comd6176b02012-08-23 18:14:13 +000060// requiring texture domain clamping to prevent color bleeding when drawing
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000061// a sub region of a larger source image.
62#define COLOR_BLEED_TOLERANCE SkFloatToScalar(0.001f)
bsalomon@google.com06cd7322012-03-30 18:45:35 +000063
junov@google.comdbfac8a2012-12-06 21:47:40 +000064#define DO_DEFERRED_CLEAR() \
65 do { \
66 if (fNeedClear) { \
67 this->clear(SK_ColorTRANSPARENT); \
68 } \
69 } while (false) \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000070
reed@google.comac10a2d2010-12-22 21:39:39 +000071///////////////////////////////////////////////////////////////////////////////
72
reed@google.comb0a34d82012-07-11 19:57:55 +000073#define CHECK_FOR_NODRAW_ANNOTATION(paint) \
74 do { if (paint.isNoDrawAnnotation()) { return; } } while (0)
75
76///////////////////////////////////////////////////////////////////////////////
77
78
bsalomon@google.com84405e02012-03-05 19:57:21 +000079class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
80public:
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000081 SkAutoCachedTexture()
82 : fDevice(NULL)
83 , fTexture(NULL) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000084 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000085
bsalomon@google.com84405e02012-03-05 19:57:21 +000086 SkAutoCachedTexture(SkGpuDevice* device,
87 const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +000088 const GrTextureParams* params,
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000089 GrTexture** texture)
90 : fDevice(NULL)
91 , fTexture(NULL) {
92 GrAssert(NULL != texture);
bsalomon@google.comb8670992012-07-25 21:27:09 +000093 *texture = this->set(device, bitmap, params);
reed@google.comac10a2d2010-12-22 21:39:39 +000094 }
reed@google.comac10a2d2010-12-22 21:39:39 +000095
bsalomon@google.com84405e02012-03-05 19:57:21 +000096 ~SkAutoCachedTexture() {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000097 if (NULL != fTexture) {
98 GrUnlockCachedBitmapTexture(fTexture);
bsalomon@google.com84405e02012-03-05 19:57:21 +000099 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000100 }
bsalomon@google.com84405e02012-03-05 19:57:21 +0000101
102 GrTexture* set(SkGpuDevice* device,
103 const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000104 const GrTextureParams* params) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000105 if (NULL != fTexture) {
106 GrUnlockCachedBitmapTexture(fTexture);
107 fTexture = NULL;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000108 }
109 fDevice = device;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000110 GrTexture* result = (GrTexture*)bitmap.getTexture();
111 if (NULL == result) {
112 // Cannot return the native texture so look it up in our cache
113 fTexture = GrLockCachedBitmapTexture(device->context(), bitmap, params);
114 result = fTexture;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000115 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000116 return result;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000117 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000118
bsalomon@google.com84405e02012-03-05 19:57:21 +0000119private:
120 SkGpuDevice* fDevice;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000121 GrTexture* fTexture;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000122};
reed@google.comac10a2d2010-12-22 21:39:39 +0000123
124///////////////////////////////////////////////////////////////////////////////
125
reed@google.comac10a2d2010-12-22 21:39:39 +0000126struct GrSkDrawProcs : public SkDrawProcs {
127public:
128 GrContext* fContext;
129 GrTextContext* fTextContext;
130 GrFontScaler* fFontScaler; // cached in the skia glyphcache
131};
132
133///////////////////////////////////////////////////////////////////////////////
134
reed@google.comaf951c92011-06-16 19:10:39 +0000135static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
136 switch (config) {
137 case kAlpha_8_GrPixelConfig:
138 *isOpaque = false;
139 return SkBitmap::kA8_Config;
140 case kRGB_565_GrPixelConfig:
141 *isOpaque = true;
142 return SkBitmap::kRGB_565_Config;
143 case kRGBA_4444_GrPixelConfig:
144 *isOpaque = false;
145 return SkBitmap::kARGB_4444_Config;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000146 case kSkia8888_PM_GrPixelConfig:
147 // we don't currently have a way of knowing whether
148 // a 8888 is opaque based on the config.
149 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000150 return SkBitmap::kARGB_8888_Config;
151 default:
152 *isOpaque = false;
153 return SkBitmap::kNo_Config;
154 }
155}
reed@google.comac10a2d2010-12-22 21:39:39 +0000156
reed@google.comaf951c92011-06-16 19:10:39 +0000157static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000158 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000159
160 bool isOpaque;
161 SkBitmap bitmap;
162 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
163 renderTarget->width(), renderTarget->height());
164 bitmap.setIsOpaque(isOpaque);
165 return bitmap;
166}
167
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000168SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000169: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
bsalomon@google.com8090e652012-08-28 15:07:11 +0000170 this->initFromRenderTarget(context, texture->asRenderTarget(), false);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000171}
172
reed@google.comaf951c92011-06-16 19:10:39 +0000173SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000174: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.com8090e652012-08-28 15:07:11 +0000175 this->initFromRenderTarget(context, renderTarget, false);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000176}
177
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000178void SkGpuDevice::initFromRenderTarget(GrContext* context,
bsalomon@google.com8090e652012-08-28 15:07:11 +0000179 GrRenderTarget* renderTarget,
180 bool cached) {
reed@google.comaf951c92011-06-16 19:10:39 +0000181 fDrawProcs = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000182
reed@google.comaf951c92011-06-16 19:10:39 +0000183 fContext = context;
184 fContext->ref();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000185
reed@google.comaf951c92011-06-16 19:10:39 +0000186 fRenderTarget = NULL;
187 fNeedClear = false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000188
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000189 GrAssert(NULL != renderTarget);
190 fRenderTarget = renderTarget;
191 fRenderTarget->ref();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000192
bsalomon@google.coma2921122012-08-28 12:34:17 +0000193 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
194 // on the RT but not vice-versa.
195 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
196 // busting chrome (for a currently unknown reason).
197 GrSurface* surface = fRenderTarget->asTexture();
198 if (NULL == surface) {
199 surface = fRenderTarget;
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000200 }
bsalomon@google.com8090e652012-08-28 15:07:11 +0000201 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (surface, cached));
bsalomon@google.coma2921122012-08-28 12:34:17 +0000202
reed@google.comaf951c92011-06-16 19:10:39 +0000203 this->setPixelRef(pr, 0)->unref();
204}
205
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000206SkGpuDevice::SkGpuDevice(GrContext* context,
207 SkBitmap::Config config,
208 int width,
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000209 int height,
210 int sampleCount)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000211 : SkDevice(config, width, height, false /*isOpaque*/) {
212
reed@google.comac10a2d2010-12-22 21:39:39 +0000213 fDrawProcs = NULL;
214
reed@google.com7b201d22011-01-11 18:59:23 +0000215 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000216 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000217
reed@google.comac10a2d2010-12-22 21:39:39 +0000218 fRenderTarget = NULL;
219 fNeedClear = false;
220
reed@google.comaf951c92011-06-16 19:10:39 +0000221 if (config != SkBitmap::kRGB_565_Config) {
222 config = SkBitmap::kARGB_8888_Config;
223 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000224
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000225 GrTextureDesc desc;
226 desc.fFlags = kRenderTarget_GrTextureFlagBit;
227 desc.fWidth = width;
228 desc.fHeight = height;
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000229 desc.fConfig = SkBitmapConfig2GrPixelConfig(config);
230 desc.fSampleCnt = sampleCount;
reed@google.comac10a2d2010-12-22 21:39:39 +0000231
bsalomon@google.coma2921122012-08-28 12:34:17 +0000232 SkAutoTUnref<GrTexture> texture(fContext->createUncachedTexture(desc, NULL, 0));
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000233
bsalomon@google.coma2921122012-08-28 12:34:17 +0000234 if (NULL != texture) {
235 fRenderTarget = texture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000236 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000237
reed@google.comaf951c92011-06-16 19:10:39 +0000238 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000239
reed@google.comaf951c92011-06-16 19:10:39 +0000240 // wrap the bitmap with a pixelref to expose our texture
bsalomon@google.coma2921122012-08-28 12:34:17 +0000241 SkGrPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (texture));
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000242 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000243 } else {
244 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
245 width, height);
246 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000247 }
248}
249
250SkGpuDevice::~SkGpuDevice() {
251 if (fDrawProcs) {
252 delete fDrawProcs;
253 }
254
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000255 // The GrContext takes a ref on the target. We don't want to cause the render
256 // target to be unnecessarily kept alive.
257 if (fContext->getRenderTarget() == fRenderTarget) {
258 fContext->setRenderTarget(NULL);
259 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000260
robertphillips@google.com055f9082012-10-24 13:24:11 +0000261 if (fContext->getClip() == &fClipData) {
262 fContext->setClip(NULL);
263 }
264
bsalomon@google.coma2921122012-08-28 12:34:17 +0000265 SkSafeUnref(fRenderTarget);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000266 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000267}
268
reed@google.comac10a2d2010-12-22 21:39:39 +0000269///////////////////////////////////////////////////////////////////////////////
270
271void SkGpuDevice::makeRenderTargetCurrent() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000272 DO_DEFERRED_CLEAR();
reed@google.comac10a2d2010-12-22 21:39:39 +0000273 fContext->setRenderTarget(fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000274}
275
276///////////////////////////////////////////////////////////////////////////////
277
bsalomon@google.comc4364992011-11-07 15:54:49 +0000278namespace {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000279GrPixelConfig config8888_to_grconfig_and_flags(SkCanvas::Config8888 config8888, uint32_t* flags) {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000280 switch (config8888) {
281 case SkCanvas::kNative_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000282 *flags = 0;
283 return kSkia8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000284 case SkCanvas::kNative_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000285 *flags = GrContext::kUnpremul_PixelOpsFlag;
286 return kSkia8888_PM_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000287 case SkCanvas::kBGRA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000288 *flags = 0;
289 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000290 case SkCanvas::kBGRA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000291 *flags = GrContext::kUnpremul_PixelOpsFlag;
292 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000293 case SkCanvas::kRGBA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000294 *flags = 0;
295 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000296 case SkCanvas::kRGBA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000297 *flags = GrContext::kUnpremul_PixelOpsFlag;
298 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000299 default:
300 GrCrash("Unexpected Config8888.");
bsalomon@google.comccaa0022012-09-25 19:55:07 +0000301 *flags = 0; // suppress warning
bsalomon@google.comc4364992011-11-07 15:54:49 +0000302 return kSkia8888_PM_GrPixelConfig;
303 }
304}
305}
306
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000307bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
308 int x, int y,
309 SkCanvas::Config8888 config8888) {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000310 DO_DEFERRED_CLEAR();
bsalomon@google.com910267d2011-11-02 20:06:25 +0000311 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
312 SkASSERT(!bitmap.isNull());
313 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000314
bsalomon@google.com910267d2011-11-02 20:06:25 +0000315 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000316 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000317 uint32_t flags;
318 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000319 return fContext->readRenderTargetPixels(fRenderTarget,
320 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000321 bitmap.width(),
322 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000323 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000324 bitmap.getPixels(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000325 bitmap.rowBytes(),
326 flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000327}
328
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000329void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
330 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000331 SkAutoLockPixels alp(bitmap);
332 if (!bitmap.readyToDraw()) {
333 return;
334 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000335
336 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000337 uint32_t flags;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000338 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000339 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000340 } else {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000341 flags = 0;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000342 config= SkBitmapConfig2GrPixelConfig(bitmap.config());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000343 }
344
bsalomon@google.com6f379512011-11-16 20:36:03 +0000345 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000346 config, bitmap.getPixels(), bitmap.rowBytes(), flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000347}
348
robertphillips@google.com46f93502012-08-07 15:38:08 +0000349namespace {
humper@google.com05af1af2013-01-07 16:47:43 +0000350void purgeClipCB(int genID, void* ) {
robertphillips@google.com46f93502012-08-07 15:38:08 +0000351
352 if (SkClipStack::kInvalidGenID == genID ||
353 SkClipStack::kEmptyGenID == genID ||
354 SkClipStack::kWideOpenGenID == genID) {
355 // none of these cases will have a cached clip mask
356 return;
357 }
358
359}
360};
361
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000362void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
363 INHERITED::onAttachToCanvas(canvas);
364
365 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000366 fClipData.fClipStack = canvas->getClipStack();
robertphillips@google.com46f93502012-08-07 15:38:08 +0000367
368 fClipData.fClipStack->addPurgeClipCallback(purgeClipCB, fContext);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000369}
370
371void SkGpuDevice::onDetachFromCanvas() {
372 INHERITED::onDetachFromCanvas();
373
robertphillips@google.com46f93502012-08-07 15:38:08 +0000374 // TODO: iterate through the clip stack and clean up any cached clip masks
375 fClipData.fClipStack->removePurgeClipCallback(purgeClipCB, fContext);
376
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000377 fClipData.fClipStack = NULL;
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000378}
379
robertphillips@google.com607fe072012-07-24 13:54:00 +0000380#ifdef SK_DEBUG
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000381static void check_bounds(const GrClipData& clipData,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000382 const SkRegion& clipRegion,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000383 int renderTargetWidth,
384 int renderTargetHeight) {
385
robertphillips@google.com7b112892012-07-31 15:18:21 +0000386 SkIRect devBound;
387
388 devBound.setLTRB(0, 0, renderTargetWidth, renderTargetHeight);
389
robertphillips@google.com607fe072012-07-24 13:54:00 +0000390 SkClipStack::BoundsType boundType;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000391 SkRect canvTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000392
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000393 clipData.fClipStack->getBounds(&canvTemp, &boundType);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000394 if (SkClipStack::kNormal_BoundsType == boundType) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000395 SkIRect devTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000396
robertphillips@google.com7b112892012-07-31 15:18:21 +0000397 canvTemp.roundOut(&devTemp);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000398
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000399 devTemp.offset(-clipData.fOrigin.fX, -clipData.fOrigin.fY);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000400
robertphillips@google.com7b112892012-07-31 15:18:21 +0000401 if (!devBound.intersect(devTemp)) {
402 devBound.setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000403 }
404 }
405
robertphillips@google.com768fee82012-08-02 12:42:43 +0000406 GrAssert(devBound.contains(clipRegion.getBounds()));
robertphillips@google.com607fe072012-07-24 13:54:00 +0000407}
408#endif
409
reed@google.comac10a2d2010-12-22 21:39:39 +0000410///////////////////////////////////////////////////////////////////////////////
411
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000412// call this every draw call, to ensure that the context reflects our state,
reed@google.comac10a2d2010-12-22 21:39:39 +0000413// and not the state from some other canvas/device
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000414void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000415 GrAssert(NULL != fClipData.fClipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000416
reed@google.comac10a2d2010-12-22 21:39:39 +0000417 fContext->setRenderTarget(fRenderTarget);
418
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000419 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000420
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000421 if (forceIdentity) {
422 fContext->setIdentityMatrix();
423 } else {
424 fContext->setMatrix(*draw.fMatrix);
425 }
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000426 fClipData.fOrigin = this->getOrigin();
reed@google.comac10a2d2010-12-22 21:39:39 +0000427
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000428#ifdef SK_DEBUG
429 check_bounds(fClipData, *draw.fClip, fRenderTarget->width(), fRenderTarget->height());
430#endif
431
432 fContext->setClip(&fClipData);
433
434 DO_DEFERRED_CLEAR();
reed@google.comac10a2d2010-12-22 21:39:39 +0000435}
436
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000437SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000438 DO_DEFERRED_CLEAR();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000439 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000440}
441
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000442bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
bsalomon@google.coma2921122012-08-28 12:34:17 +0000443 GrTexture* texture = fRenderTarget->asTexture();
444 if (NULL != texture) {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000445 paint->colorStage(kBitmapTextureIdx)->setEffect(
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000446 GrSingleTextureEffect::Create(texture, SkMatrix::I()))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000447 return true;
448 }
449 return false;
450}
451
452///////////////////////////////////////////////////////////////////////////////
453
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000454SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
455SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
456SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
457SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
458SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
459 shader_type_mismatch);
rileya@google.com3e332582012-07-03 13:43:35 +0000460SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
461 shader_type_mismatch);
rileya@google.com22e57f92012-07-19 15:16:19 +0000462SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
463SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000464
bsalomon@google.com84405e02012-03-05 19:57:21 +0000465namespace {
466
467// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
468// justAlpha indicates that skPaint's alpha should be used rather than the color
469// Callers may subsequently modify the GrPaint. Setting constantColor indicates
470// that the final paint will draw the same color at every pixel. This allows
471// an optimization where the the color filter can be applied to the skPaint's
twiz@google.com58071162012-07-18 21:41:50 +0000472// color once while converting to GrPaint and then ignored.
473inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
474 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000475 bool justAlpha,
476 bool constantColor,
477 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000478
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000479 grPaint->setDither(skPaint.isDither());
480 grPaint->setAntiAlias(skPaint.isAntiAlias());
bsalomon@google.com5782d712011-01-21 21:03:59 +0000481
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000482 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
483 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000484
485 SkXfermode* mode = skPaint.getXfermode();
486 if (mode) {
487 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000488 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000489#if 0
490 return false;
491#endif
492 }
493 }
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000494 grPaint->setBlendFunc(sk_blend_to_grblend(sm), sk_blend_to_grblend(dm));
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000495
bsalomon@google.com5782d712011-01-21 21:03:59 +0000496 if (justAlpha) {
497 uint8_t alpha = skPaint.getAlpha();
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000498 grPaint->setColor(GrColorPackRGBA(alpha, alpha, alpha, alpha));
Scroggod757df22011-05-16 13:11:16 +0000499 // justAlpha is currently set to true only if there is a texture,
500 // so constantColor should not also be true.
501 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000502 } else {
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000503 grPaint->setColor(SkColor2GrColor(skPaint.getColor()));
bsalomon@google.com88becf42012-10-05 14:54:42 +0000504 GrAssert(!grPaint->isColorStageEnabled(kShaderTextureIdx));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000505 }
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000506
Scroggo97c88c22011-05-11 14:05:25 +0000507 SkColorFilter* colorFilter = skPaint.getColorFilter();
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000508 if (NULL != colorFilter) {
509 // if the source color is a constant then apply the filter here once rather than per pixel
510 // in a shader.
511 if (constantColor) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000512 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000513 grPaint->setColor(SkColor2GrColor(filtered));
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000514 } else {
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000515 SkAutoTUnref<GrEffectRef> effect(colorFilter->asNewEffect(dev->context()));
bsalomon@google.com021fc732012-10-25 12:47:42 +0000516 if (NULL != effect.get()) {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000517 grPaint->colorStage(kColorFilterTextureIdx)->setEffect(effect);
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000518 } else {
bsalomon@google.com8ea78d82012-10-24 20:11:30 +0000519 // TODO: rewrite this using asNewEffect()
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000520 SkColor color;
521 SkXfermode::Mode filterMode;
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000522 if (colorFilter->asColorMode(&color, &filterMode)) {
523 grPaint->setXfermodeColorFilter(filterMode, SkColor2GrColor(color));
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000524 }
525 }
Scroggod757df22011-05-16 13:11:16 +0000526 }
Scroggo97c88c22011-05-11 14:05:25 +0000527 }
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000528
bsalomon@google.com5782d712011-01-21 21:03:59 +0000529 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000530}
531
bsalomon@google.com84405e02012-03-05 19:57:21 +0000532// This function is similar to skPaint2GrPaintNoShader but also converts
bsalomon@google.com08283af2012-10-26 13:01:20 +0000533// skPaint's shader to a GrTexture/GrEffectStage if possible. The texture to
bsalomon@google.com84405e02012-03-05 19:57:21 +0000534// be used is set on grPaint and returned in param act. constantColor has the
535// same meaning as in skPaint2GrPaintNoShader.
536inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
537 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000538 bool constantColor,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000539 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000540 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000541 if (NULL == shader) {
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000542 return skPaint2GrPaintNoShader(dev, skPaint, false, constantColor, grPaint);
543 } else if (!skPaint2GrPaintNoShader(dev, skPaint, true, false, grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000544 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000545 }
546
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000547 SkAutoTUnref<GrEffectRef> effect(shader->asNewEffect(dev->context(), skPaint));
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000548 if (NULL != effect.get()) {
549 grPaint->colorStage(kShaderTextureIdx)->setEffect(effect);
rileya@google.com91f319c2012-07-25 17:18:31 +0000550 return true;
551 }
552
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000553 // We still don't have SkColorShader::asNewEffect() implemented.
554 SkShader::GradientInfo info;
555 SkColor color;
reed@google.comac10a2d2010-12-22 21:39:39 +0000556
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000557 info.fColors = &color;
558 info.fColorOffsets = NULL;
559 info.fColorCount = 1;
560 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
561 SkPaint copy(skPaint);
562 copy.setShader(NULL);
563 // modulate the paint alpha by the shader's solid color alpha
564 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
565 copy.setColor(SkColorSetA(color, newA));
566 return skPaint2GrPaintNoShader(dev, copy, false, constantColor, grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000567 }
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000568 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000569}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000570}
reed@google.comac10a2d2010-12-22 21:39:39 +0000571
572///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000573void SkGpuDevice::clear(SkColor color) {
robertphillips@google.comea5d8af2012-11-02 17:38:28 +0000574 fContext->clear(NULL, SkColor2GrColor(color), fRenderTarget);
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000575 fNeedClear = false;
bsalomon@google.com398109c2011-04-14 18:40:27 +0000576}
577
reed@google.comac10a2d2010-12-22 21:39:39 +0000578void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000579 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000580
bsalomon@google.com5782d712011-01-21 21:03:59 +0000581 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000582 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000583 return;
584 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000585
586 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000587}
588
589// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000590static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000591 kPoints_GrPrimitiveType,
592 kLines_GrPrimitiveType,
593 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000594};
595
596void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000597 size_t count, const SkPoint pts[], const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000598 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000599
600 SkScalar width = paint.getStrokeWidth();
601 if (width < 0) {
602 return;
603 }
604
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000605 // we only handle hairlines and paints without path effects or mask filters,
606 // else we let the SkDraw call our drawPath()
607 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000608 draw.drawPoints(mode, count, pts, paint, true);
609 return;
610 }
611
bsalomon@google.com5782d712011-01-21 21:03:59 +0000612 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000613 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000614 return;
615 }
616
bsalomon@google.com5782d712011-01-21 21:03:59 +0000617 fContext->drawVertices(grPaint,
618 gPointMode2PrimtiveType[mode],
619 count,
620 (GrPoint*)pts,
621 NULL,
622 NULL,
623 NULL,
624 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000625}
626
reed@google.comc9aa5872011-04-05 21:05:37 +0000627///////////////////////////////////////////////////////////////////////////////
628
reed@google.comac10a2d2010-12-22 21:39:39 +0000629void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000630 const SkPaint& paint) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000631 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000632 CHECK_SHOULD_DRAW(draw, false);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000633
bungeman@google.com79bd8772011-07-18 15:34:08 +0000634 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000635 SkScalar width = paint.getStrokeWidth();
636
637 /*
638 We have special code for hairline strokes, miter-strokes, and fills.
639 Anything else we just call our path code.
640 */
641 bool usePath = doStroke && width > 0 &&
642 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000643 // another two reasons we might need to call drawPath...
644 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000645 usePath = true;
646 }
reed@google.com67db6642011-05-26 11:46:35 +0000647 // until we aa rotated rects...
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000648 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
reed@google.com67db6642011-05-26 11:46:35 +0000649 usePath = true;
650 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000651 // small miter limit means right angles show bevel...
652 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
653 paint.getStrokeMiter() < SK_ScalarSqrt2)
654 {
655 usePath = true;
656 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000657 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000658 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
659 usePath = true;
660 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000661
662 if (usePath) {
663 SkPath path;
664 path.addRect(rect);
665 this->drawPath(draw, path, paint, NULL, true);
666 return;
667 }
668
669 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000670 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000671 return;
672 }
reed@google.com20efde72011-05-09 17:00:02 +0000673 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000674}
675
reed@google.com69302852011-02-16 18:08:07 +0000676#include "SkMaskFilter.h"
677#include "SkBounder.h"
678
bsalomon@google.com85003222012-03-28 14:44:37 +0000679///////////////////////////////////////////////////////////////////////////////
680
681// helpers for applying mask filters
682namespace {
683
bsalomon@google.com85003222012-03-28 14:44:37 +0000684// We prefer to blur small rect with small radius via CPU.
685#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
686#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
687inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
688 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
689 rect.height() <= MIN_GPU_BLUR_SIZE &&
690 radius <= MIN_GPU_BLUR_RADIUS) {
691 return true;
692 }
693 return false;
694}
695
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000696bool drawWithGPUMaskFilter(GrContext* context, const SkPath& devPath, const SkStrokeRec& stroke,
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000697 SkMaskFilter* filter, const SkRegion& clip,
sugoi@google.com12b4e272012-12-06 20:13:11 +0000698 SkBounder* bounder, GrPaint* grp) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000699 SkMaskFilter::BlurInfo info;
700 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000701 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000702 return false;
703 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000704 SkScalar radius = info.fIgnoreTransform ? info.fRadius
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000705 : context->getMatrix().mapRadius(info.fRadius);
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000706 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000707 if (radius <= 0) {
708 return false;
709 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000710
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000711 SkRect srcRect = devPath.getBounds();
bsalomon@google.com85003222012-03-28 14:44:37 +0000712 if (shouldDrawBlurWithCPU(srcRect, radius)) {
713 return false;
714 }
715
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000716 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000717 float sigma3 = sigma * 3.0f;
718
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000719 SkRect clipRect;
720 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000721
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000722 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000723 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
724 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000725 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000726 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000727 SkIRect finalIRect;
728 finalRect.roundOut(&finalIRect);
729 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000730 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000731 }
732 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000733 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000734 }
735 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000736 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000737 GrTextureDesc desc;
738 desc.fFlags = kRenderTarget_GrTextureFlagBit;
739 desc.fWidth = SkScalarCeilToInt(srcRect.width());
740 desc.fHeight = SkScalarCeilToInt(srcRect.height());
741 // We actually only need A8, but it often isn't supported as a
742 // render target so default to RGBA_8888
bsalomon@google.com0342a852012-08-20 19:22:38 +0000743 desc.fConfig = kRGBA_8888_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000744
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000745 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
746 desc.fConfig = kAlpha_8_GrPixelConfig;
747 }
748
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000749 GrAutoScratchTexture pathEntry(context, desc);
750 GrTexture* pathTexture = pathEntry.texture();
751 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000752 return false;
753 }
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000754
robertphillips@google.comccb39502012-10-01 18:25:13 +0000755 SkAutoTUnref<GrTexture> blurTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000756
robertphillips@google.comccb39502012-10-01 18:25:13 +0000757 {
758 GrContext::AutoRenderTarget art(context, pathTexture->asRenderTarget());
759 GrContext::AutoClip ac(context, srcRect);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000760
robertphillips@google.comccb39502012-10-01 18:25:13 +0000761 context->clear(NULL, 0);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000762
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000763 GrPaint tempPaint;
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000764 if (grp->isAntiAlias()) {
765 tempPaint.setAntiAlias(true);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000766 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
767 // blend coeff of zero requires dual source blending support in order
768 // to properly blend partially covered pixels. This means the AA
769 // code path may not be taken. So we use a dst blend coeff of ISA. We
770 // could special case AA draws to a dst surface with known alpha=0 to
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000771 // use a zero dst coeff when dual source blending isn't available.f
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000772 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000773 }
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000774
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000775 GrContext::AutoMatrix am;
776
777 // Draw hard shadow to pathTexture with path top-left at origin using tempPaint.
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000778 SkMatrix translate;
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000779 translate.setTranslate(offset.fX, offset.fY);
780 am.set(context, translate);
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000781 context->drawPath(tempPaint, devPath, stroke);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000782
783 // If we're doing a normal blur, we can clobber the pathTexture in the
784 // gaussianBlur. Otherwise, we need to save it for later compositing.
785 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
skia.committer@gmail.comdc3a4e52012-10-02 02:01:24 +0000786 blurTexture.reset(context->gaussianBlur(pathTexture, isNormalBlur,
robertphillips@google.comccb39502012-10-01 18:25:13 +0000787 srcRect, sigma, sigma));
robertphillips@google.com7d126752012-10-19 12:56:26 +0000788 if (NULL == blurTexture) {
789 return false;
790 }
robertphillips@google.comccb39502012-10-01 18:25:13 +0000791
792 if (!isNormalBlur) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000793 context->setIdentityMatrix();
robertphillips@google.comccb39502012-10-01 18:25:13 +0000794 GrPaint paint;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000795 SkMatrix matrix;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000796 matrix.setIDiv(pathTexture->width(), pathTexture->height());
robertphillips@google.comccb39502012-10-01 18:25:13 +0000797 // Blend pathTexture over blurTexture.
798 context->setRenderTarget(blurTexture->asRenderTarget());
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000799 paint.colorStage(0)->setEffect(
800 GrSingleTextureEffect::Create(pathTexture, matrix))->unref();
robertphillips@google.comccb39502012-10-01 18:25:13 +0000801 if (SkMaskFilter::kInner_BlurType == blurType) {
802 // inner: dst = dst * src
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000803 paint.setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000804 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
805 // solid: dst = src + dst - src * dst
806 // = (1 - dst) * src + 1 * dst
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000807 paint.setBlendFunc(kIDC_GrBlendCoeff, kOne_GrBlendCoeff);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000808 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
809 // outer: dst = dst * (1 - src)
810 // = 0 * src + (1 - src) * dst
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000811 paint.setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000812 }
813 context->drawRect(paint, srcRect);
814 }
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000815 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000816
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000817 GrContext::AutoMatrix am;
818 if (!am.setIdentity(context, grp)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000819 return false;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000820 }
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +0000821
bsalomon@google.com88becf42012-10-05 14:54:42 +0000822 static const int MASK_IDX = GrPaint::kMaxCoverageStages - 1;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000823 // we assume the last mask index is available for use
bsalomon@google.com88becf42012-10-05 14:54:42 +0000824 GrAssert(!grp->isCoverageStageEnabled(MASK_IDX));
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000825
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000826 SkMatrix matrix;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000827 matrix.setTranslate(-finalRect.fLeft, -finalRect.fTop);
828 matrix.postIDiv(blurTexture->width(), blurTexture->height());
829
bsalomon@google.com08283af2012-10-26 13:01:20 +0000830 grp->coverageStage(MASK_IDX)->reset();
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000831 grp->coverageStage(MASK_IDX)->setEffect(
832 GrSingleTextureEffect::Create(blurTexture, matrix))->unref();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000833 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000834 return true;
835}
836
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000837bool drawWithMaskFilter(GrContext* context, const SkPath& devPath,
838 SkMaskFilter* filter, const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000839 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000840 SkMask srcM, dstM;
841
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000842 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
843 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
reed@google.com69302852011-02-16 18:08:07 +0000844 return false;
845 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000846 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000847
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000848 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
reed@google.com69302852011-02-16 18:08:07 +0000849 return false;
850 }
851 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000852 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000853
854 if (clip.quickReject(dstM.fBounds)) {
855 return false;
856 }
857 if (bounder && !bounder->doIRect(dstM.fBounds)) {
858 return false;
859 }
860
861 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000862 // the current clip (and identity matrix) and GrPaint settings
863 GrContext::AutoMatrix am;
864 am.setIdentity(context, grp);
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000865
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000866 GrTextureDesc desc;
867 desc.fWidth = dstM.fBounds.width();
868 desc.fHeight = dstM.fBounds.height();
869 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +0000870
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000871 GrAutoScratchTexture ast(context, desc);
872 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000873
reed@google.com69302852011-02-16 18:08:07 +0000874 if (NULL == texture) {
875 return false;
876 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000877 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000878 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000879
bsalomon@google.com88becf42012-10-05 14:54:42 +0000880 static const int MASK_IDX = GrPaint::kMaxCoverageStages - 1;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000881 // we assume the last mask index is available for use
bsalomon@google.com88becf42012-10-05 14:54:42 +0000882 GrAssert(!grp->isCoverageStageEnabled(MASK_IDX));
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000883
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000884 SkMatrix m;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000885 m.setTranslate(-dstM.fBounds.fLeft*SK_Scalar1, -dstM.fBounds.fTop*SK_Scalar1);
886 m.postIDiv(texture->width(), texture->height());
887
bsalomon@google.com0ac6af42013-01-16 15:16:18 +0000888 grp->coverageStage(MASK_IDX)->setEffect(GrSingleTextureEffect::Create(texture, m))->unref();
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000889 GrRect d;
bsalomon@google.com81712882012-11-01 17:12:34 +0000890 d.setLTRB(SkIntToScalar(dstM.fBounds.fLeft),
891 SkIntToScalar(dstM.fBounds.fTop),
892 SkIntToScalar(dstM.fBounds.fRight),
893 SkIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000894
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000895 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000896 return true;
897}
reed@google.com69302852011-02-16 18:08:07 +0000898
bsalomon@google.com85003222012-03-28 14:44:37 +0000899}
900
901///////////////////////////////////////////////////////////////////////////////
902
reed@google.com0c219b62011-02-16 21:31:18 +0000903void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000904 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000905 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000906 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000907 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000908
bsalomon@google.com5782d712011-01-21 21:03:59 +0000909 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +0000910 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000911 return;
912 }
913
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000914 // can we cheat, and threat a thin stroke as a hairline w/ coverage
915 // if we can, we draw lots faster (raster device does this same test)
916 SkScalar hairlineCoverage;
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000917 bool doHairLine = SkDrawTreatAsHairline(paint, fContext->getMatrix(), &hairlineCoverage);
918 if (doHairLine) {
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000919 grPaint.setCoverage(SkScalarRoundToInt(hairlineCoverage * grPaint.getCoverage()));
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000920 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000921
reed@google.comfe626382011-09-21 13:50:35 +0000922 // If we have a prematrix, apply it to the path, optimizing for the case
923 // where the original path can in fact be modified in place (even though
924 // its parameter type is const).
925 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000926 SkPath tmpPath, effectPath;
reed@google.comac10a2d2010-12-22 21:39:39 +0000927
928 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +0000929 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +0000930
reed@google.come3445642011-02-16 23:20:39 +0000931 if (!pathIsMutable) {
932 result = &tmpPath;
933 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000934 }
reed@google.come3445642011-02-16 23:20:39 +0000935 // should I push prePathMatrix on our MV stack temporarily, instead
936 // of applying it here? See SkDraw.cpp
937 pathPtr->transform(*prePathMatrix, result);
938 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +0000939 }
reed@google.com0c219b62011-02-16 21:31:18 +0000940 // at this point we're done with prePathMatrix
941 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +0000942
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000943 SkStrokeRec stroke(paint);
944 SkPathEffect* pathEffect = paint.getPathEffect();
945 if (pathEffect && pathEffect->filterPath(&effectPath, *pathPtr, &stroke)) {
946 pathPtr = &effectPath;
947 }
948
949 if (!pathEffect && doHairLine) {
950 stroke.setHairlineStyle();
reed@google.com0c219b62011-02-16 21:31:18 +0000951 }
952
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000953 if (paint.getMaskFilter()) {
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000954 if (!stroke.isHairlineStyle()) {
955 if (stroke.applyToPath(&tmpPath, *pathPtr)) {
956 pathPtr = &tmpPath;
957 stroke.setFillStyle();
958 }
959 }
960
reed@google.com0c219b62011-02-16 21:31:18 +0000961 // avoid possibly allocating a new path in transform if we can
962 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
963
964 // transform the path into device space
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000965 pathPtr->transform(fContext->getMatrix(), devPathPtr);
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000966 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, stroke, paint.getMaskFilter(),
sugoi@google.com12b4e272012-12-06 20:13:11 +0000967 *draw.fClip, draw.fBounder, &grPaint)) {
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000968 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
969 SkPaint::kFill_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000970 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000971 *draw.fClip, draw.fBounder, &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000972 }
reed@google.com69302852011-02-16 18:08:07 +0000973 return;
974 }
reed@google.com69302852011-02-16 18:08:07 +0000975
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000976 fContext->drawPath(grPaint, *pathPtr, stroke);
reed@google.comac10a2d2010-12-22 21:39:39 +0000977}
978
bsalomon@google.comfb309512011-11-30 14:13:48 +0000979namespace {
980
981inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
982 int tilesX = (r / tileSize) - (l / tileSize) + 1;
983 int tilesY = (b / tileSize) - (t / tileSize) + 1;
984 return tilesX * tilesY;
985}
986
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000987inline int determine_tile_size(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +0000988 const SkRect& src,
bsalomon@google.comfb309512011-11-30 14:13:48 +0000989 int maxTextureSize) {
990 static const int kSmallTileSize = 1 << 10;
991 if (maxTextureSize <= kSmallTileSize) {
992 return maxTextureSize;
993 }
994
995 size_t maxTexTotalTileSize;
996 size_t smallTotalTileSize;
997
robertphillips@google.combac6b052012-09-28 18:06:49 +0000998 SkIRect iSrc;
999 src.roundOut(&iSrc);
1000
1001 maxTexTotalTileSize = get_tile_count(iSrc.fLeft,
1002 iSrc.fTop,
1003 iSrc.fRight,
1004 iSrc.fBottom,
1005 maxTextureSize);
1006 smallTotalTileSize = get_tile_count(iSrc.fLeft,
1007 iSrc.fTop,
1008 iSrc.fRight,
1009 iSrc.fBottom,
1010 kSmallTileSize);
1011
bsalomon@google.comfb309512011-11-30 14:13:48 +00001012 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1013 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1014
1015 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1016 return kSmallTileSize;
1017 } else {
1018 return maxTextureSize;
1019 }
1020}
1021}
1022
1023bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001024 const GrTextureParams& params,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001025 const SkRect* srcRectPtr) const {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001026 // if bitmap is explictly texture backed then just use the texture
1027 if (NULL != bitmap.getTexture()) {
1028 return false;
1029 }
1030 // if it's larger than the max texture size, then we have no choice but
1031 // tiling
1032 const int maxTextureSize = fContext->getMaxTextureSize();
1033 if (bitmap.width() > maxTextureSize ||
1034 bitmap.height() > maxTextureSize) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001035 return true;
1036 }
1037 // if we are going to have to draw the whole thing, then don't tile
1038 if (NULL == srcRectPtr) {
1039 return false;
1040 }
1041 // if the entire texture is already in our cache then no reason to tile it
bsalomon@google.com0797c2c2012-12-20 15:13:01 +00001042 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001043 return false;
1044 }
1045
1046 // At this point we know we could do the draw by uploading the entire bitmap
1047 // as a texture. However, if the texture would be large compared to the
1048 // cache size and we don't require most of it for this draw then tile to
1049 // reduce the amount of upload and cache spill.
1050
1051 // assumption here is that sw bitmap size is a good proxy for its size as
1052 // a texture
1053 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001054 size_t cacheSize;
1055 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001056 if (bmpSize < cacheSize / 2) {
1057 return false;
1058 }
1059
robertphillips@google.combac6b052012-09-28 18:06:49 +00001060 SkScalar fracUsed = SkScalarMul(srcRectPtr->width() / bitmap.width(),
1061 srcRectPtr->height() / bitmap.height());
1062 if (fracUsed <= SK_ScalarHalf) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001063 return true;
1064 } else {
1065 return false;
1066 }
1067}
1068
reed@google.comac10a2d2010-12-22 21:39:39 +00001069void SkGpuDevice::drawBitmap(const SkDraw& draw,
1070 const SkBitmap& bitmap,
1071 const SkIRect* srcRectPtr,
1072 const SkMatrix& m,
1073 const SkPaint& paint) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001074
1075 SkRect tmp;
1076 SkRect* tmpPtr = NULL;
1077
1078 // convert from SkIRect to SkRect
1079 if (NULL != srcRectPtr) {
1080 tmp.set(*srcRectPtr);
1081 tmpPtr = &tmp;
1082 }
1083
1084 // We cannot call drawBitmapRect here since 'm' could be anything
1085 this->drawBitmapCommon(draw, bitmap, tmpPtr, m, paint);
1086}
1087
1088void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1089 const SkBitmap& bitmap,
1090 const SkRect* srcRectPtr,
1091 const SkMatrix& m,
1092 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001093 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001094
robertphillips@google.combac6b052012-09-28 18:06:49 +00001095 SkRect srcRect;
reed@google.comac10a2d2010-12-22 21:39:39 +00001096 if (NULL == srcRectPtr) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001097 srcRect.set(0, 0, SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001098 } else {
1099 srcRect = *srcRectPtr;
1100 }
1101
junov@google.comd935cfb2011-06-27 20:48:23 +00001102 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001103 // Convert the bitmap to a shader so that the rect can be drawn
1104 // through drawRect, which supports mask filters.
robertphillips@google.combac6b052012-09-28 18:06:49 +00001105 SkMatrix newM(m);
junov@google.com1d329782011-07-28 20:10:09 +00001106 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001107 const SkBitmap* bitmapPtr = &bitmap;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001108 if (NULL != srcRectPtr) {
1109 SkIRect iSrc;
1110 srcRect.roundOut(&iSrc);
1111 if (!bitmap.extractSubset(&tmp, iSrc)) {
epoger@google.com9ef2d832011-07-01 21:12:20 +00001112 return; // extraction failed
1113 }
1114 bitmapPtr = &tmp;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001115 srcRect.offset(SkIntToScalar(-iSrc.fLeft), SkIntToScalar(-iSrc.fTop));
1116 // The source rect has changed so update the matrix
1117 newM.preTranslate(SkIntToScalar(iSrc.fLeft), SkIntToScalar(iSrc.fTop));
junov@google.comd935cfb2011-06-27 20:48:23 +00001118 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001119
junov@google.comd935cfb2011-06-27 20:48:23 +00001120 SkPaint paintWithTexture(paint);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001121 paintWithTexture.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
junov@google.comd935cfb2011-06-27 20:48:23 +00001122 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001123
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001124 // Transform 'newM' needs to be concatenated to the current matrix,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001125 // rather than transforming the primitive directly, so that 'newM' will
junov@google.com1d329782011-07-28 20:10:09 +00001126 // also affect the behavior of the mask filter.
1127 SkMatrix drawMatrix;
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001128 drawMatrix.setConcat(fContext->getMatrix(), newM);
junov@google.com1d329782011-07-28 20:10:09 +00001129 SkDraw transformedDraw(draw);
1130 transformedDraw.fMatrix = &drawMatrix;
1131
robertphillips@google.combac6b052012-09-28 18:06:49 +00001132 this->drawRect(transformedDraw, srcRect, paintWithTexture);
junov@google.com1d329782011-07-28 20:10:09 +00001133
junov@google.comd935cfb2011-06-27 20:48:23 +00001134 return;
1135 }
1136
bsalomon@google.com5782d712011-01-21 21:03:59 +00001137 GrPaint grPaint;
skia.committer@gmail.com4e73aa12013-01-09 02:01:30 +00001138
humper@google.com9aaf36d2013-01-08 16:08:01 +00001139 bool alphaOnly = !(SkBitmap::kA8_Config == bitmap.config());
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001140 if (!skPaint2GrPaintNoShader(this, paint, alphaOnly, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001141 return;
1142 }
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001143 GrTextureParams params;
1144 params.setBilerp(paint.isFilterBitmap());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001145
robertphillips@google.combac6b052012-09-28 18:06:49 +00001146 if (!this->shouldTileBitmap(bitmap, params, srcRectPtr)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001147 // take the simple case
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001148 this->internalDrawBitmap(bitmap, srcRect, m, params, &grPaint);
robertphillips@google.combac6b052012-09-28 18:06:49 +00001149 } else {
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001150 this->drawTiledBitmap(bitmap, srcRect, m, params, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001151 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001152}
1153
1154// Break 'bitmap' into several tiles to draw it since it has already
1155// been determined to be too large to fit in VRAM
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001156void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001157 const SkRect& srcRect,
1158 const SkMatrix& m,
1159 const GrTextureParams& params,
1160 GrPaint* grPaint) {
1161 const int maxTextureSize = fContext->getMaxTextureSize();
1162
1163 int tileSize = determine_tile_size(bitmap, srcRect, maxTextureSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001164
reed@google.comac10a2d2010-12-22 21:39:39 +00001165 // compute clip bounds in local coordinates
robertphillips@google.combac6b052012-09-28 18:06:49 +00001166 SkRect clipRect;
reed@google.comac10a2d2010-12-22 21:39:39 +00001167 {
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001168 const GrRenderTarget* rt = fContext->getRenderTarget();
1169 clipRect.setWH(SkIntToScalar(rt->width()), SkIntToScalar(rt->height()));
1170 if (!fContext->getClip()->fClipStack->intersectRectWithClip(&clipRect)) {
1171 return;
1172 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001173 SkMatrix matrix, inverse;
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001174 matrix.setConcat(fContext->getMatrix(), m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001175 if (!matrix.invert(&inverse)) {
1176 return;
1177 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001178 inverse.mapRect(&clipRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001179 }
1180
bsalomon@google.comfb309512011-11-30 14:13:48 +00001181 int nx = bitmap.width() / tileSize;
1182 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001183 for (int x = 0; x <= nx; x++) {
1184 for (int y = 0; y <= ny; y++) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001185 SkRect tileR;
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001186 tileR.set(SkIntToScalar(x * tileSize),
robertphillips@google.combac6b052012-09-28 18:06:49 +00001187 SkIntToScalar(y * tileSize),
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001188 SkIntToScalar((x + 1) * tileSize),
robertphillips@google.combac6b052012-09-28 18:06:49 +00001189 SkIntToScalar((y + 1) * tileSize));
1190
1191 if (!SkRect::Intersects(tileR, clipRect)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001192 continue;
1193 }
1194
robertphillips@google.combac6b052012-09-28 18:06:49 +00001195 if (!tileR.intersect(srcRect)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001196 continue;
1197 }
1198
1199 SkBitmap tmpB;
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001200 SkIRect iTileR;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001201 tileR.roundOut(&iTileR);
1202 if (bitmap.extractSubset(&tmpB, iTileR)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001203 // now offset it to make it "local" to our tmp bitmap
robertphillips@google.combac6b052012-09-28 18:06:49 +00001204 tileR.offset(SkIntToScalar(-iTileR.fLeft), SkIntToScalar(-iTileR.fTop));
reed@google.comac10a2d2010-12-22 21:39:39 +00001205 SkMatrix tmpM(m);
skia.committer@gmail.comdc3a4e52012-10-02 02:01:24 +00001206 tmpM.preTranslate(SkIntToScalar(iTileR.fLeft),
robertphillips@google.comffad46b2012-10-01 14:32:51 +00001207 SkIntToScalar(iTileR.fTop));
1208
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001209 this->internalDrawBitmap(tmpB, tileR, tmpM, params, grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001210 }
1211 }
1212 }
1213}
1214
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001215namespace {
1216
1217bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1218 // detect pixel disalignment
1219 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1220 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
rmistry@google.comd6176b02012-08-23 18:14:13 +00001221 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001222 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1223 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1224 COLOR_BLEED_TOLERANCE &&
1225 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1226 COLOR_BLEED_TOLERANCE) {
1227 return true;
1228 }
1229 return false;
1230}
1231
1232bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1233 const SkMatrix& m) {
1234 // Only gets called if hasAlignedSamples returned false.
1235 // So we can assume that sampling is axis aligned but not texel aligned.
1236 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001237 SkRect innerSrcRect(srcRect), innerTransformedRect,
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001238 outerTransformedRect(transformedRect);
1239 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1240 m.mapRect(&innerTransformedRect, innerSrcRect);
1241
1242 // The gap between outerTransformedRect and innerTransformedRect
1243 // represents the projection of the source border area, which is
1244 // problematic for color bleeding. We must check whether any
1245 // destination pixels sample the border area.
1246 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1247 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1248 SkIRect outer, inner;
1249 outerTransformedRect.round(&outer);
1250 innerTransformedRect.round(&inner);
1251 // If the inner and outer rects round to the same result, it means the
1252 // border does not overlap any pixel centers. Yay!
1253 return inner != outer;
1254}
1255
1256} // unnamed namespace
1257
reed@google.comac10a2d2010-12-22 21:39:39 +00001258/*
1259 * This is called by drawBitmap(), which has to handle images that may be too
1260 * large to be represented by a single texture.
1261 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001262 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1263 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001264 */
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001265void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001266 const SkRect& srcRect,
reed@google.comac10a2d2010-12-22 21:39:39 +00001267 const SkMatrix& m,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001268 const GrTextureParams& params,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001269 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001270 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1271 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001272
reed@google.com9c49bc32011-07-07 13:42:37 +00001273 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001274 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001275 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001276 return;
1277 }
1278
reed@google.comac10a2d2010-12-22 21:39:39 +00001279 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001280 SkAutoCachedTexture act(this, bitmap, &params, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001281 if (NULL == texture) {
1282 return;
1283 }
1284
robertphillips@google.combac6b052012-09-28 18:06:49 +00001285 GrRect dstRect(srcRect);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001286 GrRect paintRect;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001287 SkScalar wInv = SkScalarInvert(SkIntToScalar(bitmap.width()));
1288 SkScalar hInv = SkScalarInvert(SkIntToScalar(bitmap.height()));
1289 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1290 SkScalarMul(srcRect.fTop, hInv),
1291 SkScalarMul(srcRect.fRight, wInv),
1292 SkScalarMul(srcRect.fBottom, hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001293
rmistry@google.comd6176b02012-08-23 18:14:13 +00001294 bool needsTextureDomain = false;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001295 if (params.isBilerp()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001296 // Need texture domain if drawing a sub rect.
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001297 needsTextureDomain = srcRect.width() < bitmap.width() ||
robertphillips@google.combac6b052012-09-28 18:06:49 +00001298 srcRect.height() < bitmap.height();
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001299 if (m.rectStaysRect() && fContext->getMatrix().rectStaysRect()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001300 // sampling is axis-aligned
robertphillips@google.combac6b052012-09-28 18:06:49 +00001301 GrRect transformedRect;
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001302 SkMatrix srcToDeviceMatrix(m);
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001303 srcToDeviceMatrix.postConcat(fContext->getMatrix());
robertphillips@google.combac6b052012-09-28 18:06:49 +00001304 srcToDeviceMatrix.mapRect(&transformedRect, srcRect);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001305
robertphillips@google.combac6b052012-09-28 18:06:49 +00001306 if (hasAlignedSamples(srcRect, transformedRect)) {
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001307 // We could also turn off filtering here (but we already did a cache lookup with
1308 // params).
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001309 needsTextureDomain = false;
1310 } else {
1311 needsTextureDomain = needsTextureDomain &&
robertphillips@google.combac6b052012-09-28 18:06:49 +00001312 mayColorBleed(srcRect, transformedRect, m);
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001313 }
1314 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001315 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001316
1317 GrRect textureDomain = GrRect::MakeEmpty();
bsalomon@google.com0ac6af42013-01-16 15:16:18 +00001318 SkAutoTUnref<GrEffectRef> effect;
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001319 if (needsTextureDomain) {
1320 // Use a constrained texture domain to avoid color bleeding
bsalomon@google.com81712882012-11-01 17:12:34 +00001321 SkScalar left, top, right, bottom;
1322 if (srcRect.width() > SK_Scalar1) {
1323 SkScalar border = SK_ScalarHalf / bitmap.width();
junov@google.com6acc9b32011-05-16 18:32:07 +00001324 left = paintRect.left() + border;
1325 right = paintRect.right() - border;
1326 } else {
bsalomon@google.com81712882012-11-01 17:12:34 +00001327 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
junov@google.com6acc9b32011-05-16 18:32:07 +00001328 }
bsalomon@google.com81712882012-11-01 17:12:34 +00001329 if (srcRect.height() > SK_Scalar1) {
1330 SkScalar border = SK_ScalarHalf / bitmap.height();
junov@google.com6acc9b32011-05-16 18:32:07 +00001331 top = paintRect.top() + border;
1332 bottom = paintRect.bottom() - border;
1333 } else {
bsalomon@google.com81712882012-11-01 17:12:34 +00001334 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
junov@google.com6acc9b32011-05-16 18:32:07 +00001335 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001336 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +00001337 effect.reset(GrTextureDomainEffect::Create(texture,
1338 SkMatrix::I(),
1339 textureDomain,
1340 GrTextureDomainEffect::kClamp_WrapMode,
1341 params.isBilerp()));
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001342 } else {
bsalomon@google.com0ac6af42013-01-16 15:16:18 +00001343 effect.reset(GrSingleTextureEffect::Create(texture, SkMatrix::I(), params));
junov@google.com6acc9b32011-05-16 18:32:07 +00001344 }
bsalomon@google.com08283af2012-10-26 13:01:20 +00001345 grPaint->colorStage(kBitmapTextureIdx)->setEffect(effect);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001346 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001347}
1348
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001349namespace {
1350
bsalomon@google.comf271cc72012-10-24 19:35:13 +00001351void apply_effect(GrContext* context,
1352 GrTexture* srcTexture,
1353 GrTexture* dstTexture,
1354 const GrRect& rect,
bsalomon@google.com0ac6af42013-01-16 15:16:18 +00001355 GrEffectRef* effect) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001356 SkASSERT(srcTexture && srcTexture->getContext() == context);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001357 GrContext::AutoMatrix am;
1358 am.setIdentity(context);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001359 GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget());
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001360 GrContext::AutoClip acs(context, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001361
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001362 GrPaint paint;
bsalomon@google.comdbe49f72012-11-05 16:36:02 +00001363 paint.colorStage(0)->setEffect(effect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001364 context->drawRect(paint, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001365}
1366
1367};
1368
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001369static GrTexture* filter_texture(SkDevice* device, GrContext* context,
1370 GrTexture* texture, SkImageFilter* filter,
1371 const GrRect& rect) {
reed@google.com8926b162012-03-23 15:36:36 +00001372 GrAssert(filter);
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001373 SkDeviceImageFilterProxy proxy(device);
reed@google.com8926b162012-03-23 15:36:36 +00001374
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001375 GrTextureDesc desc;
1376 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1377 desc.fWidth = SkScalarCeilToInt(rect.width());
1378 desc.fHeight = SkScalarCeilToInt(rect.height());
bsalomon@google.com0342a852012-08-20 19:22:38 +00001379 desc.fConfig = kRGBA_8888_GrPixelConfig;
bsalomon@google.com0ac6af42013-01-16 15:16:18 +00001380 GrEffectRef* effect;
reed@google.com8926b162012-03-23 15:36:36 +00001381
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001382 if (filter->canFilterImageGPU()) {
senorblanco@chromium.org985fa792012-10-24 15:14:26 +00001383 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1384 // filter. Also set the clip wide open and the matrix to identity.
1385 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org171f5c72013-01-10 19:55:18 +00001386 texture = filter->filterImageGPU(&proxy, texture, rect);
bsalomon@google.com021fc732012-10-25 12:47:42 +00001387 } else if (filter->asNewEffect(&effect, texture)) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001388 GrAutoScratchTexture dst(context, desc);
bsalomon@google.com021fc732012-10-25 12:47:42 +00001389 apply_effect(context, texture, dst.texture(), rect, effect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001390 texture = dst.detach();
bsalomon@google.com021fc732012-10-25 12:47:42 +00001391 effect->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001392 }
1393 return texture;
1394}
1395
reed@google.comac10a2d2010-12-22 21:39:39 +00001396void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001397 int left, int top, const SkPaint& paint) {
1398 // drawSprite is defined to be in device coords.
1399 CHECK_SHOULD_DRAW(draw, true);
reed@google.comac10a2d2010-12-22 21:39:39 +00001400
reed@google.com8926b162012-03-23 15:36:36 +00001401 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001402 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1403 return;
1404 }
1405
reed@google.com76dd2772012-01-05 21:15:07 +00001406 int w = bitmap.width();
1407 int h = bitmap.height();
1408
bsalomon@google.com5782d712011-01-21 21:03:59 +00001409 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001410 if(!skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001411 return;
1412 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001413
bsalomon@google.com08283af2012-10-26 13:01:20 +00001414 GrEffectStage* stage = grPaint.colorStage(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001415
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001416 GrTexture* texture;
bsalomon@google.com08283af2012-10-26 13:01:20 +00001417 stage->reset();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001418 // draw sprite uses the default texture params
1419 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
bsalomon@google.com0ac6af42013-01-16 15:16:18 +00001420 grPaint.colorStage(kBitmapTextureIdx)->setEffect(
1421 GrSingleTextureEffect::Create(texture, SkMatrix::I()))->unref();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001422
reed@google.com8926b162012-03-23 15:36:36 +00001423 SkImageFilter* filter = paint.getImageFilter();
1424 if (NULL != filter) {
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001425 GrTexture* filteredTexture = filter_texture(this, fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001426 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001427 if (filteredTexture) {
bsalomon@google.com0ac6af42013-01-16 15:16:18 +00001428 grPaint.colorStage(kBitmapTextureIdx)->setEffect(
1429 GrSingleTextureEffect::Create(filteredTexture, SkMatrix::I()))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001430 texture = filteredTexture;
1431 filteredTexture->unref();
1432 }
reed@google.com76dd2772012-01-05 21:15:07 +00001433 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001434
bsalomon@google.com5782d712011-01-21 21:03:59 +00001435 fContext->drawRectToRect(grPaint,
bsalomon@google.com81712882012-11-01 17:12:34 +00001436 GrRect::MakeXYWH(SkIntToScalar(left),
1437 SkIntToScalar(top),
1438 SkIntToScalar(w),
1439 SkIntToScalar(h)),
1440 GrRect::MakeWH(SK_Scalar1 * w / texture->width(),
1441 SK_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001442}
1443
reed@google.com33535f32012-09-25 15:37:50 +00001444void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1445 const SkRect* src, const SkRect& dst,
1446 const SkPaint& paint) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001447 SkMatrix matrix;
1448 SkRect bitmapBounds, tmpSrc;
1449
1450 bitmapBounds.set(0, 0,
1451 SkIntToScalar(bitmap.width()),
1452 SkIntToScalar(bitmap.height()));
1453
reed@google.com33535f32012-09-25 15:37:50 +00001454 // Compute matrix from the two rectangles
robertphillips@google.combac6b052012-09-28 18:06:49 +00001455 if (NULL != src) {
1456 tmpSrc = *src;
1457 } else {
1458 tmpSrc = bitmapBounds;
1459 }
1460 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1461
1462 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1463 if (NULL != src) {
1464 if (!bitmapBounds.contains(tmpSrc)) {
1465 if (!tmpSrc.intersect(bitmapBounds)) {
1466 return; // nothing to draw
reed@google.com33535f32012-09-25 15:37:50 +00001467 }
reed@google.com33535f32012-09-25 15:37:50 +00001468 }
reed@google.com33535f32012-09-25 15:37:50 +00001469 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +00001470
robertphillips@google.combac6b052012-09-28 18:06:49 +00001471 this->drawBitmapCommon(draw, bitmap, &tmpSrc, matrix, paint);
reed@google.com33535f32012-09-25 15:37:50 +00001472}
1473
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001474void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001475 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001476 // clear of the source device must occur before CHECK_SHOULD_DRAW
1477 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1478 if (dev->fNeedClear) {
1479 // TODO: could check here whether we really need to draw at all
1480 dev->clear(0x0);
1481 }
1482
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001483 // drawDevice is defined to be in device coords.
1484 CHECK_SHOULD_DRAW(draw, true);
reed@google.comac10a2d2010-12-22 21:39:39 +00001485
bsalomon@google.com5782d712011-01-21 21:03:59 +00001486 GrPaint grPaint;
bsalomon@google.com08283af2012-10-26 13:01:20 +00001487 grPaint.colorStage(kBitmapTextureIdx)->reset();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001488 if (!dev->bindDeviceAsTexture(&grPaint) ||
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001489 !skPaint2GrPaintNoShader(this, paint, true, false, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001490 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001491 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001492
bsalomon@google.com08283af2012-10-26 13:01:20 +00001493 GrTexture* devTex = grPaint.getColorStage(kBitmapTextureIdx).getEffect()->texture(0);
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001494 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001495
reed@google.com8926b162012-03-23 15:36:36 +00001496 SkImageFilter* filter = paint.getImageFilter();
1497 if (NULL != filter) {
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001498 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001499 SkIntToScalar(devTex->height()));
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001500 GrTexture* filteredTexture = filter_texture(this, fContext, devTex, filter, rect);
reed@google.com8926b162012-03-23 15:36:36 +00001501 if (filteredTexture) {
bsalomon@google.com0ac6af42013-01-16 15:16:18 +00001502 grPaint.colorStage(kBitmapTextureIdx)->setEffect(
1503 GrSingleTextureEffect::Create(filteredTexture, SkMatrix::I()))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001504 devTex = filteredTexture;
1505 filteredTexture->unref();
1506 }
1507 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001508
bsalomon@google.com5782d712011-01-21 21:03:59 +00001509 const SkBitmap& bm = dev->accessBitmap(false);
1510 int w = bm.width();
1511 int h = bm.height();
1512
bsalomon@google.com81712882012-11-01 17:12:34 +00001513 GrRect dstRect = GrRect::MakeXYWH(SkIntToScalar(x),
1514 SkIntToScalar(y),
1515 SkIntToScalar(w),
1516 SkIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001517
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001518 // The device being drawn may not fill up its texture (saveLayer uses
1519 // the approximate ).
bsalomon@google.com81712882012-11-01 17:12:34 +00001520 GrRect srcRect = GrRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1521 SK_Scalar1 * h / devTex->height());
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001522
1523 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001524}
1525
reed@google.com8926b162012-03-23 15:36:36 +00001526bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
bsalomon@google.com8ea78d82012-10-24 20:11:30 +00001527 if (!filter->asNewEffect(NULL, NULL) &&
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001528 !filter->canFilterImageGPU()) {
reed@google.com76dd2772012-01-05 21:15:07 +00001529 return false;
1530 }
reed@google.com8926b162012-03-23 15:36:36 +00001531 return true;
1532}
1533
1534bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1535 const SkMatrix& ctm,
1536 SkBitmap* result, SkIPoint* offset) {
1537 // want explicitly our impl, so guard against a subclass of us overriding it
1538 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001539 return false;
1540 }
reed@google.com8926b162012-03-23 15:36:36 +00001541
1542 SkAutoLockPixels alp(src, !src.getTexture());
1543 if (!src.getTexture() && !src.readyToDraw()) {
1544 return false;
1545 }
1546
1547 GrPaint paint;
reed@google.com8926b162012-03-23 15:36:36 +00001548
reed@google.com8926b162012-03-23 15:36:36 +00001549 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001550 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1551 // must be pushed upstack.
1552 SkAutoCachedTexture act(this, src, NULL, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001553
1554 result->setConfig(src.config(), src.width(), src.height());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001555 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001556 SkIntToScalar(src.height()));
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001557 GrTexture* resultTexture = filter_texture(this, fContext, texture, filter, rect);
reed@google.com8926b162012-03-23 15:36:36 +00001558 if (resultTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001559 result->setPixelRef(SkNEW_ARGS(SkGrTexturePixelRef,
1560 (resultTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001561 resultTexture->unref();
1562 }
reed@google.com76dd2772012-01-05 21:15:07 +00001563 return true;
1564}
1565
reed@google.comac10a2d2010-12-22 21:39:39 +00001566///////////////////////////////////////////////////////////////////////////////
1567
1568// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001569static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001570 kTriangles_GrPrimitiveType,
1571 kTriangleStrip_GrPrimitiveType,
1572 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001573};
1574
1575void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1576 int vertexCount, const SkPoint vertices[],
1577 const SkPoint texs[], const SkColor colors[],
1578 SkXfermode* xmode,
1579 const uint16_t indices[], int indexCount,
1580 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001581 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001582
bsalomon@google.com5782d712011-01-21 21:03:59 +00001583 GrPaint grPaint;
bsalomon@google.com5782d712011-01-21 21:03:59 +00001584 // we ignore the shader if texs is null.
1585 if (NULL == texs) {
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001586 if (!skPaint2GrPaintNoShader(this, paint, false, NULL == colors, &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001587 return;
1588 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001589 } else {
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001590 if (!skPaint2GrPaintShader(this, paint, NULL == colors, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001591 return;
1592 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001593 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001594
1595 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001596 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001597 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1598#if 0
1599 return
1600#endif
1601 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001602 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001603
bsalomon@google.com498776a2011-08-16 19:20:44 +00001604 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1605 if (NULL != colors) {
1606 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001607 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001608 for (int i = 0; i < vertexCount; ++i) {
rileya@google.com24f3ad12012-07-18 21:47:40 +00001609 convertedColors[i] = SkColor2GrColor(colors[i]);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001610 }
1611 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001612 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001613 fContext->drawVertices(grPaint,
1614 gVertexMode2PrimitiveType[vmode],
1615 vertexCount,
1616 (GrPoint*) vertices,
1617 (GrPoint*) texs,
1618 colors,
1619 indices,
1620 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001621}
1622
1623///////////////////////////////////////////////////////////////////////////////
1624
1625static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001626 GrFontScaler* scaler = (GrFontScaler*)data;
1627 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001628}
1629
1630static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1631 void* auxData;
1632 GrFontScaler* scaler = NULL;
1633 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1634 scaler = (GrFontScaler*)auxData;
1635 }
1636 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001637 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001638 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1639 }
1640 return scaler;
1641}
1642
1643static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1644 SkFixed fx, SkFixed fy,
1645 const SkGlyph& glyph) {
1646 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1647
bungeman@google.com15865a72012-01-11 16:28:04 +00001648 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001649
1650 if (NULL == procs->fFontScaler) {
1651 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1652 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001653
bungeman@google.com15865a72012-01-11 16:28:04 +00001654 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1655 glyph.getSubXFixed(),
1656 glyph.getSubYFixed()),
1657 SkFixedFloorToFixed(fx),
1658 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001659 procs->fFontScaler);
1660}
1661
bsalomon@google.com5782d712011-01-21 21:03:59 +00001662SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001663
1664 // deferred allocation
1665 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001666 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001667 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1668 fDrawProcs->fContext = fContext;
1669 }
1670
1671 // init our (and GL's) state
1672 fDrawProcs->fTextContext = context;
1673 fDrawProcs->fFontScaler = NULL;
1674 return fDrawProcs;
1675}
1676
1677void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1678 size_t byteLength, SkScalar x, SkScalar y,
1679 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001680 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001681
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001682 if (fContext->getMatrix().hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001683 // this guy will just call our drawPath()
1684 draw.drawText((const char*)text, byteLength, x, y, paint);
1685 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001686 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001687
1688 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001689 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001690 return;
1691 }
bsalomon@google.com0e354aa2012-10-08 20:44:25 +00001692 GrTextContext context(fContext, grPaint);
tomhudson@google.com375ff852012-06-29 18:37:57 +00001693 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001694 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1695 }
1696}
1697
1698void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1699 size_t byteLength, const SkScalar pos[],
1700 SkScalar constY, int scalarsPerPos,
1701 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001702 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001703
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001704 if (fContext->getMatrix().hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001705 // this guy will just call our drawPath()
1706 draw.drawPosText((const char*)text, byteLength, pos, constY,
1707 scalarsPerPos, paint);
1708 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001709 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001710
1711 GrPaint grPaint;
bsalomon@google.come197cbf2013-01-14 16:46:26 +00001712 if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001713 return;
1714 }
bsalomon@google.com0e354aa2012-10-08 20:44:25 +00001715 GrTextContext context(fContext, grPaint);
tomhudson@google.com375ff852012-06-29 18:37:57 +00001716 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001717 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1718 scalarsPerPos, paint);
1719 }
1720}
1721
1722void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1723 size_t len, const SkPath& path,
1724 const SkMatrix* m, const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001725 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001726
1727 SkASSERT(draw.fDevice == this);
1728 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1729}
1730
1731///////////////////////////////////////////////////////////////////////////////
1732
reed@google.comf67e4cf2011-03-15 20:56:58 +00001733bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1734 if (!paint.isLCDRenderText()) {
1735 // we're cool with the paint as is
1736 return false;
1737 }
1738
1739 if (paint.getShader() ||
1740 paint.getXfermode() || // unless its srcover
1741 paint.getMaskFilter() ||
1742 paint.getRasterizer() ||
1743 paint.getColorFilter() ||
1744 paint.getPathEffect() ||
1745 paint.isFakeBoldText() ||
1746 paint.getStyle() != SkPaint::kFill_Style) {
1747 // turn off lcd
1748 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1749 flags->fHinting = paint.getHinting();
1750 return true;
1751 }
1752 // we're cool with the paint as is
1753 return false;
1754}
1755
reed@google.com75d939b2011-12-07 15:07:23 +00001756void SkGpuDevice::flush() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +00001757 DO_DEFERRED_CLEAR();
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001758 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001759}
1760
reed@google.comf67e4cf2011-03-15 20:56:58 +00001761///////////////////////////////////////////////////////////////////////////////
1762
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001763SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1764 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001765 bool isOpaque,
1766 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001767 GrTextureDesc desc;
1768 desc.fConfig = fRenderTarget->config();
1769 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1770 desc.fWidth = width;
1771 desc.fHeight = height;
1772 desc.fSampleCnt = fRenderTarget->numSamples();
1773
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001774 GrTexture* texture;
1775 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001776 // Skia's convention is to only clear a device if it is non-opaque.
1777 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001778
1779#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1780 // layers are never draw in repeat modes, so we can request an approx
1781 // match and ignore any padding.
bsalomon@google.com0797c2c2012-12-20 15:13:01 +00001782 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1783 GrContext::kApprox_ScratchTexMatch :
1784 GrContext::kExact_ScratchTexMatch;
1785 texture = fContext->lockScratchTexture(desc, match);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001786#else
1787 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1788 texture = tunref.get();
1789#endif
1790 if (texture) {
1791 return SkNEW_ARGS(SkGpuDevice,(fContext,
1792 texture,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001793 needClear));
1794 } else {
1795 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1796 width, height);
1797 return NULL;
1798 }
1799}
1800
1801SkGpuDevice::SkGpuDevice(GrContext* context,
1802 GrTexture* texture,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001803 bool needClear)
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001804 : SkDevice(make_bitmap(context, texture->asRenderTarget())) {
1805
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001806 GrAssert(texture && texture->asRenderTarget());
bsalomon@google.com8090e652012-08-28 15:07:11 +00001807 // This constructor is called from onCreateCompatibleDevice. It has locked the RT in the texture
1808 // cache. We pass true for the third argument so that it will get unlocked.
1809 this->initFromRenderTarget(context, texture->asRenderTarget(), true);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001810 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001811}