blob: 5506d1ab9988e9682e5dc2373259c4582ecb1d05 [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"
reed@google.comc9aa5872011-04-05 21:05:37 +000022#include "SkUtils.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000023
bsalomon@google.com06cd7322012-03-30 18:45:35 +000024#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
reed@google.comac10a2d2010-12-22 21:39:39 +000025
26#if 0
27 extern bool (*gShouldDrawProc)();
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +000028 #define CHECK_SHOULD_DRAW(draw, forceI) \
reed@google.comac10a2d2010-12-22 21:39:39 +000029 do { \
30 if (gShouldDrawProc && !gShouldDrawProc()) return; \
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +000031 this->prepareDraw(draw, forceI); \
reed@google.comac10a2d2010-12-22 21:39:39 +000032 } while (0)
33#else
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +000034 #define CHECK_SHOULD_DRAW(draw, forceI) this->prepareDraw(draw, forceI)
reed@google.comac10a2d2010-12-22 21:39:39 +000035#endif
36
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000037// we use the same texture slot on GrPaint for bitmaps and shaders
38// (since drawBitmap, drawSprite, and drawDevice ignore skia's shader)
39enum {
40 kBitmapTextureIdx = 0,
twiz@google.com58071162012-07-18 21:41:50 +000041 kShaderTextureIdx = 0,
42 kColorFilterTextureIdx = 1
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000043};
44
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +000045#define MAX_BLUR_SIGMA 4.0f
46// FIXME: This value comes from from SkBlurMaskFilter.cpp.
47// Should probably be put in a common header someplace.
48#define MAX_BLUR_RADIUS SkIntToScalar(128)
49// This constant approximates the scaling done in the software path's
50// "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
51// IMHO, it actually should be 1: we blur "less" than we should do
52// according to the CSS and canvas specs, simply because Safari does the same.
53// Firefox used to do the same too, until 4.0 where they fixed it. So at some
54// point we should probably get rid of these scaling constants and rebaseline
55// all the blur tests.
56#define BLUR_SIGMA_SCALE 0.6f
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000057// This constant represents the screen alignment criterion in texels for
rmistry@google.comd6176b02012-08-23 18:14:13 +000058// requiring texture domain clamping to prevent color bleeding when drawing
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000059// a sub region of a larger source image.
60#define COLOR_BLEED_TOLERANCE SkFloatToScalar(0.001f)
bsalomon@google.com06cd7322012-03-30 18:45:35 +000061
bsalomon@google.coma6926b12012-10-10 15:25:50 +000062#define DO_DEFERRED_CLEAR() \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000063 do { \
64 if (fNeedClear) { \
bsalomon@google.com730ca3b2012-04-03 13:25:12 +000065 this->clear(0x0); \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000066 } \
67 } while (false) \
68
reed@google.comac10a2d2010-12-22 21:39:39 +000069///////////////////////////////////////////////////////////////////////////////
70
reed@google.comb0a34d82012-07-11 19:57:55 +000071#define CHECK_FOR_NODRAW_ANNOTATION(paint) \
72 do { if (paint.isNoDrawAnnotation()) { return; } } while (0)
73
74///////////////////////////////////////////////////////////////////////////////
75
76
bsalomon@google.com84405e02012-03-05 19:57:21 +000077class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
78public:
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000079 SkAutoCachedTexture()
80 : fDevice(NULL)
81 , fTexture(NULL) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000082 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000083
bsalomon@google.com84405e02012-03-05 19:57:21 +000084 SkAutoCachedTexture(SkGpuDevice* device,
85 const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +000086 const GrTextureParams* params,
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000087 GrTexture** texture)
88 : fDevice(NULL)
89 , fTexture(NULL) {
90 GrAssert(NULL != texture);
bsalomon@google.comb8670992012-07-25 21:27:09 +000091 *texture = this->set(device, bitmap, params);
reed@google.comac10a2d2010-12-22 21:39:39 +000092 }
reed@google.comac10a2d2010-12-22 21:39:39 +000093
bsalomon@google.com84405e02012-03-05 19:57:21 +000094 ~SkAutoCachedTexture() {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000095 if (NULL != fTexture) {
96 GrUnlockCachedBitmapTexture(fTexture);
bsalomon@google.com84405e02012-03-05 19:57:21 +000097 }
reed@google.comac10a2d2010-12-22 21:39:39 +000098 }
bsalomon@google.com84405e02012-03-05 19:57:21 +000099
100 GrTexture* set(SkGpuDevice* device,
101 const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000102 const GrTextureParams* params) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000103 if (NULL != fTexture) {
104 GrUnlockCachedBitmapTexture(fTexture);
105 fTexture = NULL;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000106 }
107 fDevice = device;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000108 GrTexture* result = (GrTexture*)bitmap.getTexture();
109 if (NULL == result) {
110 // Cannot return the native texture so look it up in our cache
111 fTexture = GrLockCachedBitmapTexture(device->context(), bitmap, params);
112 result = fTexture;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000113 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000114 return result;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000115 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000116
bsalomon@google.com84405e02012-03-05 19:57:21 +0000117private:
118 SkGpuDevice* fDevice;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000119 GrTexture* fTexture;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000120};
reed@google.comac10a2d2010-12-22 21:39:39 +0000121
122///////////////////////////////////////////////////////////////////////////////
123
reed@google.comac10a2d2010-12-22 21:39:39 +0000124struct GrSkDrawProcs : public SkDrawProcs {
125public:
126 GrContext* fContext;
127 GrTextContext* fTextContext;
128 GrFontScaler* fFontScaler; // cached in the skia glyphcache
129};
130
131///////////////////////////////////////////////////////////////////////////////
132
reed@google.comaf951c92011-06-16 19:10:39 +0000133static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
134 switch (config) {
135 case kAlpha_8_GrPixelConfig:
136 *isOpaque = false;
137 return SkBitmap::kA8_Config;
138 case kRGB_565_GrPixelConfig:
139 *isOpaque = true;
140 return SkBitmap::kRGB_565_Config;
141 case kRGBA_4444_GrPixelConfig:
142 *isOpaque = false;
143 return SkBitmap::kARGB_4444_Config;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000144 case kSkia8888_PM_GrPixelConfig:
145 // we don't currently have a way of knowing whether
146 // a 8888 is opaque based on the config.
147 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000148 return SkBitmap::kARGB_8888_Config;
149 default:
150 *isOpaque = false;
151 return SkBitmap::kNo_Config;
152 }
153}
reed@google.comac10a2d2010-12-22 21:39:39 +0000154
reed@google.comaf951c92011-06-16 19:10:39 +0000155static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000156 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000157
158 bool isOpaque;
159 SkBitmap bitmap;
160 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
161 renderTarget->width(), renderTarget->height());
162 bitmap.setIsOpaque(isOpaque);
163 return bitmap;
164}
165
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000166SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000167: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
bsalomon@google.com8090e652012-08-28 15:07:11 +0000168 this->initFromRenderTarget(context, texture->asRenderTarget(), false);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000169}
170
reed@google.comaf951c92011-06-16 19:10:39 +0000171SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000172: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.com8090e652012-08-28 15:07:11 +0000173 this->initFromRenderTarget(context, renderTarget, false);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000174}
175
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000176void SkGpuDevice::initFromRenderTarget(GrContext* context,
bsalomon@google.com8090e652012-08-28 15:07:11 +0000177 GrRenderTarget* renderTarget,
178 bool cached) {
reed@google.comaf951c92011-06-16 19:10:39 +0000179 fDrawProcs = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000180
reed@google.comaf951c92011-06-16 19:10:39 +0000181 fContext = context;
182 fContext->ref();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000183
reed@google.comaf951c92011-06-16 19:10:39 +0000184 fRenderTarget = NULL;
185 fNeedClear = false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000186
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000187 GrAssert(NULL != renderTarget);
188 fRenderTarget = renderTarget;
189 fRenderTarget->ref();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000190
bsalomon@google.coma2921122012-08-28 12:34:17 +0000191 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
192 // on the RT but not vice-versa.
193 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
194 // busting chrome (for a currently unknown reason).
195 GrSurface* surface = fRenderTarget->asTexture();
196 if (NULL == surface) {
197 surface = fRenderTarget;
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000198 }
bsalomon@google.com8090e652012-08-28 15:07:11 +0000199 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (surface, cached));
bsalomon@google.coma2921122012-08-28 12:34:17 +0000200
reed@google.comaf951c92011-06-16 19:10:39 +0000201 this->setPixelRef(pr, 0)->unref();
202}
203
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000204SkGpuDevice::SkGpuDevice(GrContext* context,
205 SkBitmap::Config config,
206 int width,
207 int height)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000208 : SkDevice(config, width, height, false /*isOpaque*/) {
209
reed@google.comac10a2d2010-12-22 21:39:39 +0000210 fDrawProcs = NULL;
211
reed@google.com7b201d22011-01-11 18:59:23 +0000212 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000213 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000214
reed@google.comac10a2d2010-12-22 21:39:39 +0000215 fRenderTarget = NULL;
216 fNeedClear = false;
217
reed@google.comaf951c92011-06-16 19:10:39 +0000218 if (config != SkBitmap::kRGB_565_Config) {
219 config = SkBitmap::kARGB_8888_Config;
220 }
221 SkBitmap bm;
222 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000223
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000224 GrTextureDesc desc;
225 desc.fFlags = kRenderTarget_GrTextureFlagBit;
226 desc.fWidth = width;
227 desc.fHeight = height;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000228 desc.fConfig = SkBitmapConfig2GrPixelConfig(bm.config());
reed@google.comac10a2d2010-12-22 21:39:39 +0000229
bsalomon@google.coma2921122012-08-28 12:34:17 +0000230 SkAutoTUnref<GrTexture> texture(fContext->createUncachedTexture(desc, NULL, 0));
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000231
bsalomon@google.coma2921122012-08-28 12:34:17 +0000232 if (NULL != texture) {
233 fRenderTarget = texture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000234 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000235
reed@google.comaf951c92011-06-16 19:10:39 +0000236 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000237
reed@google.comaf951c92011-06-16 19:10:39 +0000238 // wrap the bitmap with a pixelref to expose our texture
bsalomon@google.coma2921122012-08-28 12:34:17 +0000239 SkGrPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (texture));
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000240 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000241 } else {
242 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
243 width, height);
244 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000245 }
246}
247
248SkGpuDevice::~SkGpuDevice() {
249 if (fDrawProcs) {
250 delete fDrawProcs;
251 }
252
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000253 // The GrContext takes a ref on the target. We don't want to cause the render
254 // target to be unnecessarily kept alive.
255 if (fContext->getRenderTarget() == fRenderTarget) {
256 fContext->setRenderTarget(NULL);
257 }
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000258
bsalomon@google.coma2921122012-08-28 12:34:17 +0000259 SkSafeUnref(fRenderTarget);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000260 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000261}
262
reed@google.comac10a2d2010-12-22 21:39:39 +0000263///////////////////////////////////////////////////////////////////////////////
264
265void SkGpuDevice::makeRenderTargetCurrent() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000266 DO_DEFERRED_CLEAR();
reed@google.comac10a2d2010-12-22 21:39:39 +0000267 fContext->setRenderTarget(fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000268}
269
270///////////////////////////////////////////////////////////////////////////////
271
bsalomon@google.comc4364992011-11-07 15:54:49 +0000272namespace {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000273GrPixelConfig config8888_to_grconfig_and_flags(SkCanvas::Config8888 config8888, uint32_t* flags) {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000274 switch (config8888) {
275 case SkCanvas::kNative_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000276 *flags = 0;
277 return kSkia8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000278 case SkCanvas::kNative_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000279 *flags = GrContext::kUnpremul_PixelOpsFlag;
280 return kSkia8888_PM_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000281 case SkCanvas::kBGRA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000282 *flags = 0;
283 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000284 case SkCanvas::kBGRA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000285 *flags = GrContext::kUnpremul_PixelOpsFlag;
286 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000287 case SkCanvas::kRGBA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000288 *flags = 0;
289 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000290 case SkCanvas::kRGBA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000291 *flags = GrContext::kUnpremul_PixelOpsFlag;
292 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000293 default:
294 GrCrash("Unexpected Config8888.");
bsalomon@google.comccaa0022012-09-25 19:55:07 +0000295 *flags = 0; // suppress warning
bsalomon@google.comc4364992011-11-07 15:54:49 +0000296 return kSkia8888_PM_GrPixelConfig;
297 }
298}
299}
300
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000301bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
302 int x, int y,
303 SkCanvas::Config8888 config8888) {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000304 DO_DEFERRED_CLEAR();
bsalomon@google.com910267d2011-11-02 20:06:25 +0000305 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
306 SkASSERT(!bitmap.isNull());
307 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000308
bsalomon@google.com910267d2011-11-02 20:06:25 +0000309 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000310 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000311 uint32_t flags;
312 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000313 return fContext->readRenderTargetPixels(fRenderTarget,
314 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000315 bitmap.width(),
316 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000317 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000318 bitmap.getPixels(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000319 bitmap.rowBytes(),
320 flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000321}
322
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000323void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
324 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000325 SkAutoLockPixels alp(bitmap);
326 if (!bitmap.readyToDraw()) {
327 return;
328 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000329
330 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000331 uint32_t flags;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000332 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000333 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000334 } else {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000335 flags = 0;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000336 config= SkBitmapConfig2GrPixelConfig(bitmap.config());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000337 }
338
bsalomon@google.com6f379512011-11-16 20:36:03 +0000339 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000340 config, bitmap.getPixels(), bitmap.rowBytes(), flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000341}
342
robertphillips@google.com46f93502012-08-07 15:38:08 +0000343namespace {
344void purgeClipCB(int genID, void* data) {
345 GrContext* context = (GrContext*) data;
346
347 if (SkClipStack::kInvalidGenID == genID ||
348 SkClipStack::kEmptyGenID == genID ||
349 SkClipStack::kWideOpenGenID == genID) {
350 // none of these cases will have a cached clip mask
351 return;
352 }
353
354}
355};
356
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000357void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
358 INHERITED::onAttachToCanvas(canvas);
359
360 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000361 fClipData.fClipStack = canvas->getClipStack();
robertphillips@google.com46f93502012-08-07 15:38:08 +0000362
363 fClipData.fClipStack->addPurgeClipCallback(purgeClipCB, fContext);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000364}
365
366void SkGpuDevice::onDetachFromCanvas() {
367 INHERITED::onDetachFromCanvas();
368
robertphillips@google.com46f93502012-08-07 15:38:08 +0000369 // TODO: iterate through the clip stack and clean up any cached clip masks
370 fClipData.fClipStack->removePurgeClipCallback(purgeClipCB, fContext);
371
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000372 fClipData.fClipStack = NULL;
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000373}
374
robertphillips@google.com607fe072012-07-24 13:54:00 +0000375#ifdef SK_DEBUG
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000376static void check_bounds(const GrClipData& clipData,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000377 const SkRegion& clipRegion,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000378 int renderTargetWidth,
379 int renderTargetHeight) {
380
robertphillips@google.com7b112892012-07-31 15:18:21 +0000381 SkIRect devBound;
382
383 devBound.setLTRB(0, 0, renderTargetWidth, renderTargetHeight);
384
robertphillips@google.com607fe072012-07-24 13:54:00 +0000385 SkClipStack::BoundsType boundType;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000386 SkRect canvTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000387
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000388 clipData.fClipStack->getBounds(&canvTemp, &boundType);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000389 if (SkClipStack::kNormal_BoundsType == boundType) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000390 SkIRect devTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000391
robertphillips@google.com7b112892012-07-31 15:18:21 +0000392 canvTemp.roundOut(&devTemp);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000393
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000394 devTemp.offset(-clipData.fOrigin.fX, -clipData.fOrigin.fY);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000395
robertphillips@google.com7b112892012-07-31 15:18:21 +0000396 if (!devBound.intersect(devTemp)) {
397 devBound.setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000398 }
399 }
400
robertphillips@google.com768fee82012-08-02 12:42:43 +0000401 GrAssert(devBound.contains(clipRegion.getBounds()));
robertphillips@google.com607fe072012-07-24 13:54:00 +0000402}
403#endif
404
reed@google.comac10a2d2010-12-22 21:39:39 +0000405///////////////////////////////////////////////////////////////////////////////
406
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000407// call this every draw call, to ensure that the context reflects our state,
reed@google.comac10a2d2010-12-22 21:39:39 +0000408// and not the state from some other canvas/device
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000409void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000410 GrAssert(NULL != fClipData.fClipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000411
reed@google.comac10a2d2010-12-22 21:39:39 +0000412 fContext->setRenderTarget(fRenderTarget);
413
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000414 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000415
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000416 if (forceIdentity) {
417 fContext->setIdentityMatrix();
418 } else {
419 fContext->setMatrix(*draw.fMatrix);
420 }
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000421 fClipData.fOrigin = this->getOrigin();
reed@google.comac10a2d2010-12-22 21:39:39 +0000422
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000423#ifdef SK_DEBUG
424 check_bounds(fClipData, *draw.fClip, fRenderTarget->width(), fRenderTarget->height());
425#endif
426
427 fContext->setClip(&fClipData);
428
429 DO_DEFERRED_CLEAR();
reed@google.comac10a2d2010-12-22 21:39:39 +0000430}
431
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000432SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000433 DO_DEFERRED_CLEAR();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000434 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000435}
436
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000437bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
bsalomon@google.coma2921122012-08-28 12:34:17 +0000438 GrTexture* texture = fRenderTarget->asTexture();
439 if (NULL != texture) {
bsalomon@google.com88becf42012-10-05 14:54:42 +0000440 paint->colorSampler(kBitmapTextureIdx)->setCustomStage(
bsalomon@google.coma2921122012-08-28 12:34:17 +0000441 SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000442 return true;
443 }
444 return false;
445}
446
447///////////////////////////////////////////////////////////////////////////////
448
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000449SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
450SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
451SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
452SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
453SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
454 shader_type_mismatch);
rileya@google.com3e332582012-07-03 13:43:35 +0000455SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
456 shader_type_mismatch);
rileya@google.com22e57f92012-07-19 15:16:19 +0000457SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
458SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000459
bsalomon@google.com84405e02012-03-05 19:57:21 +0000460namespace {
461
462// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
463// justAlpha indicates that skPaint's alpha should be used rather than the color
464// Callers may subsequently modify the GrPaint. Setting constantColor indicates
465// that the final paint will draw the same color at every pixel. This allows
466// an optimization where the the color filter can be applied to the skPaint's
twiz@google.com58071162012-07-18 21:41:50 +0000467// color once while converting to GrPaint and then ignored.
468inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
469 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000470 bool justAlpha,
471 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000472 SkGpuDevice::SkAutoCachedTexture* act,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000473 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000474
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000475 grPaint->setDither(skPaint.isDither());
476 grPaint->setAntiAlias(skPaint.isAntiAlias());
bsalomon@google.com5782d712011-01-21 21:03:59 +0000477
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000478 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
479 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000480
481 SkXfermode* mode = skPaint.getXfermode();
482 if (mode) {
483 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000484 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000485#if 0
486 return false;
487#endif
488 }
489 }
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000490 grPaint->setBlendFunc(sk_blend_to_grblend(sm), sk_blend_to_grblend(dm));
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000491
bsalomon@google.com5782d712011-01-21 21:03:59 +0000492 if (justAlpha) {
493 uint8_t alpha = skPaint.getAlpha();
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000494 grPaint->setColor(GrColorPackRGBA(alpha, alpha, alpha, alpha));
Scroggod757df22011-05-16 13:11:16 +0000495 // justAlpha is currently set to true only if there is a texture,
496 // so constantColor should not also be true.
497 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000498 } else {
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000499 grPaint->setColor(SkColor2GrColor(skPaint.getColor()));
bsalomon@google.com88becf42012-10-05 14:54:42 +0000500 GrAssert(!grPaint->isColorStageEnabled(kShaderTextureIdx));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000501 }
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000502
Scroggo97c88c22011-05-11 14:05:25 +0000503 SkColorFilter* colorFilter = skPaint.getColorFilter();
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000504 if (NULL != colorFilter) {
505 // if the source color is a constant then apply the filter here once rather than per pixel
506 // in a shader.
507 if (constantColor) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000508 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000509 grPaint->setColor(SkColor2GrColor(filtered));
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000510 } else {
511 SkAutoTUnref<GrCustomStage> stage(colorFilter->asNewCustomStage(dev->context()));
512 if (NULL != stage.get()) {
513 grPaint->colorSampler(kColorFilterTextureIdx)->setCustomStage(stage);
514 } else {
bsalomon@google.comb2ad1012012-10-17 15:00:32 +0000515 // TODO: rewrite this using asNewCustomStage()
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000516 SkColor color;
517 SkXfermode::Mode filterMode;
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000518 if (colorFilter->asColorMode(&color, &filterMode)) {
519 grPaint->setXfermodeColorFilter(filterMode, SkColor2GrColor(color));
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000520 }
521 }
Scroggod757df22011-05-16 13:11:16 +0000522 }
Scroggo97c88c22011-05-11 14:05:25 +0000523 }
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000524
bsalomon@google.com5782d712011-01-21 21:03:59 +0000525 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000526}
527
bsalomon@google.com84405e02012-03-05 19:57:21 +0000528// This function is similar to skPaint2GrPaintNoShader but also converts
529// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
530// be used is set on grPaint and returned in param act. constantColor has the
531// same meaning as in skPaint2GrPaintNoShader.
532inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
533 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000534 bool constantColor,
bsalomon@google.com88becf42012-10-05 14:54:42 +0000535 SkGpuDevice::SkAutoCachedTexture textures[GrPaint::kMaxColorStages],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000536 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000537 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000538 if (NULL == shader) {
twiz@google.com58071162012-07-18 21:41:50 +0000539 return skPaint2GrPaintNoShader(dev,
540 skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000541 false,
542 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000543 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000544 grPaint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000545 } else if (!skPaint2GrPaintNoShader(dev, skPaint, true, false,
twiz@google.com58071162012-07-18 21:41:50 +0000546 &textures[kColorFilterTextureIdx], grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000547 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000548 }
549
bsalomon@google.com88becf42012-10-05 14:54:42 +0000550 GrSamplerState* sampler = grPaint->colorSampler(kShaderTextureIdx);
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000551 if (shader->asNewCustomStage(dev->context(), sampler)) {
rileya@google.com91f319c2012-07-25 17:18:31 +0000552 return true;
553 }
554
reed@google.comac10a2d2010-12-22 21:39:39 +0000555 SkBitmap bitmap;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000556 SkMatrix matrix;
reed@google.comac10a2d2010-12-22 21:39:39 +0000557 SkShader::TileMode tileModes[2];
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000558 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, &matrix, tileModes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000559
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000560 if (SkShader::kNone_BitmapType == bmptype) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000561 SkShader::GradientInfo info;
562 SkColor color;
563
564 info.fColors = &color;
565 info.fColorOffsets = NULL;
566 info.fColorCount = 1;
567 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
568 SkPaint copy(skPaint);
569 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000570 // modulate the paint alpha by the shader's solid color alpha
571 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
572 copy.setColor(SkColorSetA(color, newA));
twiz@google.com58071162012-07-18 21:41:50 +0000573 return skPaint2GrPaintNoShader(dev,
574 copy,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000575 false,
576 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000577 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000578 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000579 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000580 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000581 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000582
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000583 // Must set wrap and filter on the sampler before requesting a texture.
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000584 GrTextureParams params(tileModes, skPaint.isFilterBitmap());
585 GrTexture* texture = textures[kShaderTextureIdx].set(dev, bitmap, &params);
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000586
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000587 if (NULL == texture) {
588 SkDebugf("Couldn't convert bitmap to texture.\n");
589 return false;
590 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000591
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000592 // since our texture coords will be in local space, we whack the texture
reed@google.comac10a2d2010-12-22 21:39:39 +0000593 // matrix to map them back into 0...1 before we load it
594 SkMatrix localM;
595 if (shader->getLocalMatrix(&localM)) {
596 SkMatrix inverse;
597 if (localM.invert(&inverse)) {
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000598 matrix.preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000599 }
600 }
601 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000602 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
603 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000604 matrix.postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000605 }
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000606 sampler->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect, (texture, params)), matrix)->unref();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000607
608 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000609}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000610}
reed@google.comac10a2d2010-12-22 21:39:39 +0000611
612///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000613void SkGpuDevice::clear(SkColor color) {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000614 fContext->clear(NULL, color, fRenderTarget);
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000615 fNeedClear = false;
bsalomon@google.com398109c2011-04-14 18:40:27 +0000616}
617
reed@google.comac10a2d2010-12-22 21:39:39 +0000618void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000619 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000620
bsalomon@google.com5782d712011-01-21 21:03:59 +0000621 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +0000622 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000623 if (!skPaint2GrPaintShader(this,
624 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000625 true,
twiz@google.com58071162012-07-18 21:41:50 +0000626 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000627 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000628 return;
629 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000630
631 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000632}
633
634// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000635static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000636 kPoints_GrPrimitiveType,
637 kLines_GrPrimitiveType,
638 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000639};
640
641void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000642 size_t count, const SkPoint pts[], const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000643 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000644
645 SkScalar width = paint.getStrokeWidth();
646 if (width < 0) {
647 return;
648 }
649
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000650 // we only handle hairlines and paints without path effects or mask filters,
651 // else we let the SkDraw call our drawPath()
652 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000653 draw.drawPoints(mode, count, pts, paint, true);
654 return;
655 }
656
bsalomon@google.com5782d712011-01-21 21:03:59 +0000657 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +0000658 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000659 if (!skPaint2GrPaintShader(this,
660 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000661 true,
twiz@google.com58071162012-07-18 21:41:50 +0000662 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000663 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000664 return;
665 }
666
bsalomon@google.com5782d712011-01-21 21:03:59 +0000667 fContext->drawVertices(grPaint,
668 gPointMode2PrimtiveType[mode],
669 count,
670 (GrPoint*)pts,
671 NULL,
672 NULL,
673 NULL,
674 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000675}
676
reed@google.comc9aa5872011-04-05 21:05:37 +0000677///////////////////////////////////////////////////////////////////////////////
678
reed@google.comac10a2d2010-12-22 21:39:39 +0000679void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000680 const SkPaint& paint) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000681 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000682 CHECK_SHOULD_DRAW(draw, false);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000683
bungeman@google.com79bd8772011-07-18 15:34:08 +0000684 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000685 SkScalar width = paint.getStrokeWidth();
686
687 /*
688 We have special code for hairline strokes, miter-strokes, and fills.
689 Anything else we just call our path code.
690 */
691 bool usePath = doStroke && width > 0 &&
692 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000693 // another two reasons we might need to call drawPath...
694 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000695 usePath = true;
696 }
reed@google.com67db6642011-05-26 11:46:35 +0000697 // until we aa rotated rects...
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000698 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
reed@google.com67db6642011-05-26 11:46:35 +0000699 usePath = true;
700 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000701 // small miter limit means right angles show bevel...
702 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
703 paint.getStrokeMiter() < SK_ScalarSqrt2)
704 {
705 usePath = true;
706 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000707 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000708 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
709 usePath = true;
710 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000711
712 if (usePath) {
713 SkPath path;
714 path.addRect(rect);
715 this->drawPath(draw, path, paint, NULL, true);
716 return;
717 }
718
719 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +0000720 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000721 if (!skPaint2GrPaintShader(this,
722 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000723 true,
twiz@google.com58071162012-07-18 21:41:50 +0000724 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000725 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000726 return;
727 }
reed@google.com20efde72011-05-09 17:00:02 +0000728 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000729}
730
reed@google.com69302852011-02-16 18:08:07 +0000731#include "SkMaskFilter.h"
732#include "SkBounder.h"
733
bsalomon@google.com85003222012-03-28 14:44:37 +0000734///////////////////////////////////////////////////////////////////////////////
735
736// helpers for applying mask filters
737namespace {
738
739GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000740 switch (fillType) {
741 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000742 return kWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000743 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000744 return kEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000745 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000746 return kInverseWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000747 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000748 return kInverseEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000749 default:
750 SkDebugf("Unsupported path fill type\n");
bsalomon@google.com47059542012-06-06 20:51:20 +0000751 return kHairLine_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000752 }
753}
754
bsalomon@google.com85003222012-03-28 14:44:37 +0000755// We prefer to blur small rect with small radius via CPU.
756#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
757#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
758inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
759 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
760 rect.height() <= MIN_GPU_BLUR_SIZE &&
761 radius <= MIN_GPU_BLUR_RADIUS) {
762 return true;
763 }
764 return false;
765}
766
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000767bool drawWithGPUMaskFilter(GrContext* context, const SkPath& devPath,
768 SkMaskFilter* filter, const SkRegion& clip,
769 SkBounder* bounder, GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000770#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000771 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000772#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000773 SkMaskFilter::BlurInfo info;
774 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000775 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000776 return false;
777 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000778 SkScalar radius = info.fIgnoreTransform ? info.fRadius
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000779 : context->getMatrix().mapRadius(info.fRadius);
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000780 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000781 if (radius <= 0) {
782 return false;
783 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000784
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000785 SkRect srcRect = devPath.getBounds();
bsalomon@google.com85003222012-03-28 14:44:37 +0000786 if (shouldDrawBlurWithCPU(srcRect, radius)) {
787 return false;
788 }
789
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000790 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000791 float sigma3 = sigma * 3.0f;
792
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000793 SkRect clipRect;
794 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000795
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000796 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000797 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
798 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000799 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000800 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000801 SkIRect finalIRect;
802 finalRect.roundOut(&finalIRect);
803 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000804 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000805 }
806 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000807 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000808 }
809 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000810 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000811 GrTextureDesc desc;
812 desc.fFlags = kRenderTarget_GrTextureFlagBit;
813 desc.fWidth = SkScalarCeilToInt(srcRect.width());
814 desc.fHeight = SkScalarCeilToInt(srcRect.height());
815 // We actually only need A8, but it often isn't supported as a
816 // render target so default to RGBA_8888
bsalomon@google.com0342a852012-08-20 19:22:38 +0000817 desc.fConfig = kRGBA_8888_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000818
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000819 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
820 desc.fConfig = kAlpha_8_GrPixelConfig;
821 }
822
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000823 GrAutoScratchTexture pathEntry(context, desc);
824 GrTexture* pathTexture = pathEntry.texture();
825 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000826 return false;
827 }
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000828
robertphillips@google.comccb39502012-10-01 18:25:13 +0000829 SkAutoTUnref<GrTexture> blurTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000830
robertphillips@google.comccb39502012-10-01 18:25:13 +0000831 {
832 GrContext::AutoRenderTarget art(context, pathTexture->asRenderTarget());
833 GrContext::AutoClip ac(context, srcRect);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000834
robertphillips@google.comccb39502012-10-01 18:25:13 +0000835 context->clear(NULL, 0);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000836
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000837 GrPaint tempPaint;
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000838 if (grp->isAntiAlias()) {
839 tempPaint.setAntiAlias(true);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000840 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
841 // blend coeff of zero requires dual source blending support in order
842 // to properly blend partially covered pixels. This means the AA
843 // code path may not be taken. So we use a dst blend coeff of ISA. We
844 // could special case AA draws to a dst surface with known alpha=0 to
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000845 // use a zero dst coeff when dual source blending isn't available.f
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000846 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000847 }
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000848
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000849 GrContext::AutoMatrix am;
850
851 // Draw hard shadow to pathTexture with path top-left at origin using tempPaint.
852 GrMatrix translate;
853 translate.setTranslate(offset.fX, offset.fY);
854 am.set(context, translate);
855 context->drawPath(tempPaint, devPath, pathFillType);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000856
857 // If we're doing a normal blur, we can clobber the pathTexture in the
858 // gaussianBlur. Otherwise, we need to save it for later compositing.
859 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
skia.committer@gmail.comdc3a4e52012-10-02 02:01:24 +0000860 blurTexture.reset(context->gaussianBlur(pathTexture, isNormalBlur,
robertphillips@google.comccb39502012-10-01 18:25:13 +0000861 srcRect, sigma, sigma));
862
863 if (!isNormalBlur) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000864 context->setIdentityMatrix();
robertphillips@google.comccb39502012-10-01 18:25:13 +0000865 GrPaint paint;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000866 GrMatrix matrix;
867 matrix.setIDiv(pathTexture->width(), pathTexture->height());
robertphillips@google.comccb39502012-10-01 18:25:13 +0000868 // Blend pathTexture over blurTexture.
869 context->setRenderTarget(blurTexture->asRenderTarget());
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000870 paint.colorSampler(0)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect, (pathTexture)), matrix)->unref();
robertphillips@google.comccb39502012-10-01 18:25:13 +0000871 if (SkMaskFilter::kInner_BlurType == blurType) {
872 // inner: dst = dst * src
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000873 paint.setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000874 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
875 // solid: dst = src + dst - src * dst
876 // = (1 - dst) * src + 1 * dst
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000877 paint.setBlendFunc(kIDC_GrBlendCoeff, kOne_GrBlendCoeff);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000878 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
879 // outer: dst = dst * (1 - src)
880 // = 0 * src + (1 - src) * dst
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000881 paint.setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000882 }
883 context->drawRect(paint, srcRect);
884 }
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000885 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000886
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000887 GrContext::AutoMatrix am;
888 if (!am.setIdentity(context, grp)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000889 return false;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000890 }
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +0000891
bsalomon@google.com88becf42012-10-05 14:54:42 +0000892 static const int MASK_IDX = GrPaint::kMaxCoverageStages - 1;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000893 // we assume the last mask index is available for use
bsalomon@google.com88becf42012-10-05 14:54:42 +0000894 GrAssert(!grp->isCoverageStageEnabled(MASK_IDX));
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000895
896 GrMatrix matrix;
897 matrix.setTranslate(-finalRect.fLeft, -finalRect.fTop);
898 matrix.postIDiv(blurTexture->width(), blurTexture->height());
899
bsalomon@google.com88becf42012-10-05 14:54:42 +0000900 grp->coverageSampler(MASK_IDX)->reset();
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000901 grp->coverageSampler(MASK_IDX)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect, (blurTexture)), matrix)->unref();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000902 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000903 return true;
904}
905
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000906bool drawWithMaskFilter(GrContext* context, const SkPath& devPath,
907 SkMaskFilter* filter, const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000908 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000909 SkMask srcM, dstM;
910
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000911 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
912 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
reed@google.com69302852011-02-16 18:08:07 +0000913 return false;
914 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000915 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000916
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000917 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
reed@google.com69302852011-02-16 18:08:07 +0000918 return false;
919 }
920 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000921 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000922
923 if (clip.quickReject(dstM.fBounds)) {
924 return false;
925 }
926 if (bounder && !bounder->doIRect(dstM.fBounds)) {
927 return false;
928 }
929
930 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000931 // the current clip (and identity matrix) and GrPaint settings
932 GrContext::AutoMatrix am;
933 am.setIdentity(context, grp);
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000934
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000935 GrTextureDesc desc;
936 desc.fWidth = dstM.fBounds.width();
937 desc.fHeight = dstM.fBounds.height();
938 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +0000939
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000940 GrAutoScratchTexture ast(context, desc);
941 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000942
reed@google.com69302852011-02-16 18:08:07 +0000943 if (NULL == texture) {
944 return false;
945 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000946 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000947 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000948
bsalomon@google.com88becf42012-10-05 14:54:42 +0000949 static const int MASK_IDX = GrPaint::kMaxCoverageStages - 1;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000950 // we assume the last mask index is available for use
bsalomon@google.com88becf42012-10-05 14:54:42 +0000951 GrAssert(!grp->isCoverageStageEnabled(MASK_IDX));
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000952
953 GrMatrix m;
954 m.setTranslate(-dstM.fBounds.fLeft*SK_Scalar1, -dstM.fBounds.fTop*SK_Scalar1);
955 m.postIDiv(texture->width(), texture->height());
956
957 grp->coverageSampler(MASK_IDX)->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect, (texture)), m)->unref();
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000958 GrRect d;
959 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +0000960 GrIntToScalar(dstM.fBounds.fTop),
961 GrIntToScalar(dstM.fBounds.fRight),
962 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000963
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000964 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000965 return true;
966}
reed@google.com69302852011-02-16 18:08:07 +0000967
bsalomon@google.com85003222012-03-28 14:44:37 +0000968}
969
970///////////////////////////////////////////////////////////////////////////////
971
reed@google.com0c219b62011-02-16 21:31:18 +0000972void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000973 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000974 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000975 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000976 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000977
reed@google.comfe626382011-09-21 13:50:35 +0000978 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000979
bsalomon@google.com5782d712011-01-21 21:03:59 +0000980 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +0000981 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000982 if (!skPaint2GrPaintShader(this,
983 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000984 true,
twiz@google.com58071162012-07-18 21:41:50 +0000985 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000986 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000987 return;
988 }
989
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000990 // can we cheat, and threat a thin stroke as a hairline w/ coverage
991 // if we can, we draw lots faster (raster device does this same test)
992 SkScalar hairlineCoverage;
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000993 if (SkDrawTreatAsHairline(paint, fContext->getMatrix(), &hairlineCoverage)) {
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000994 doFill = false;
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000995 grPaint.setCoverage(SkScalarRoundToInt(hairlineCoverage * grPaint.getCoverage()));
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000996 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000997
reed@google.comfe626382011-09-21 13:50:35 +0000998 // If we have a prematrix, apply it to the path, optimizing for the case
999 // where the original path can in fact be modified in place (even though
1000 // its parameter type is const).
1001 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1002 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001003
1004 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001005 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001006
reed@google.come3445642011-02-16 23:20:39 +00001007 if (!pathIsMutable) {
1008 result = &tmpPath;
1009 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001010 }
reed@google.come3445642011-02-16 23:20:39 +00001011 // should I push prePathMatrix on our MV stack temporarily, instead
1012 // of applying it here? See SkDraw.cpp
1013 pathPtr->transform(*prePathMatrix, result);
1014 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001015 }
reed@google.com0c219b62011-02-16 21:31:18 +00001016 // at this point we're done with prePathMatrix
1017 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001018
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001019 if (paint.getPathEffect() ||
1020 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001021 // it is safe to use tmpPath here, even if we already used it for the
1022 // prepathmatrix, since getFillPath can take the same object for its
1023 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001024 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001025 pathPtr = &tmpPath;
1026 }
1027
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001028 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001029 // avoid possibly allocating a new path in transform if we can
1030 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1031
1032 // transform the path into device space
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001033 pathPtr->transform(fContext->getMatrix(), devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001034 GrPathFill pathFillType = doFill ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001035 skToGrFillType(devPathPtr->getFillType()) : kHairLine_GrPathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001036 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001037 *draw.fClip, draw.fBounder, &grPaint, pathFillType)) {
rmistry@google.comd6176b02012-08-23 18:14:13 +00001038 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001039 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001040 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001041 *draw.fClip, draw.fBounder, &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001042 }
reed@google.com69302852011-02-16 18:08:07 +00001043 return;
1044 }
reed@google.com69302852011-02-16 18:08:07 +00001045
bsalomon@google.com47059542012-06-06 20:51:20 +00001046 GrPathFill fill = kHairLine_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001047
reed@google.com0c219b62011-02-16 21:31:18 +00001048 if (doFill) {
1049 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001050 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001051 fill = kWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001052 break;
1053 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001054 fill = kEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001055 break;
1056 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001057 fill = kInverseWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001058 break;
1059 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001060 fill = kInverseEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001061 break;
1062 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001063 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001064 return;
1065 }
1066 }
1067
reed@google.com07f3ee12011-05-16 17:21:57 +00001068 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001069}
1070
bsalomon@google.comfb309512011-11-30 14:13:48 +00001071namespace {
1072
1073inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1074 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1075 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1076 return tilesX * tilesY;
1077}
1078
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001079inline int determine_tile_size(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001080 const SkRect& src,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001081 int maxTextureSize) {
1082 static const int kSmallTileSize = 1 << 10;
1083 if (maxTextureSize <= kSmallTileSize) {
1084 return maxTextureSize;
1085 }
1086
1087 size_t maxTexTotalTileSize;
1088 size_t smallTotalTileSize;
1089
robertphillips@google.combac6b052012-09-28 18:06:49 +00001090 SkIRect iSrc;
1091 src.roundOut(&iSrc);
1092
1093 maxTexTotalTileSize = get_tile_count(iSrc.fLeft,
1094 iSrc.fTop,
1095 iSrc.fRight,
1096 iSrc.fBottom,
1097 maxTextureSize);
1098 smallTotalTileSize = get_tile_count(iSrc.fLeft,
1099 iSrc.fTop,
1100 iSrc.fRight,
1101 iSrc.fBottom,
1102 kSmallTileSize);
1103
bsalomon@google.comfb309512011-11-30 14:13:48 +00001104 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1105 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1106
1107 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1108 return kSmallTileSize;
1109 } else {
1110 return maxTextureSize;
1111 }
1112}
1113}
1114
1115bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001116 const GrTextureParams& params,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001117 const SkRect* srcRectPtr) const {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001118 // if bitmap is explictly texture backed then just use the texture
1119 if (NULL != bitmap.getTexture()) {
1120 return false;
1121 }
1122 // if it's larger than the max texture size, then we have no choice but
1123 // tiling
1124 const int maxTextureSize = fContext->getMaxTextureSize();
1125 if (bitmap.width() > maxTextureSize ||
1126 bitmap.height() > maxTextureSize) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001127 return true;
1128 }
1129 // if we are going to have to draw the whole thing, then don't tile
1130 if (NULL == srcRectPtr) {
1131 return false;
1132 }
1133 // if the entire texture is already in our cache then no reason to tile it
bsalomon@google.comb8670992012-07-25 21:27:09 +00001134 if (this->isBitmapInTextureCache(bitmap, params)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001135 return false;
1136 }
1137
1138 // At this point we know we could do the draw by uploading the entire bitmap
1139 // as a texture. However, if the texture would be large compared to the
1140 // cache size and we don't require most of it for this draw then tile to
1141 // reduce the amount of upload and cache spill.
1142
1143 // assumption here is that sw bitmap size is a good proxy for its size as
1144 // a texture
1145 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001146 size_t cacheSize;
1147 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001148 if (bmpSize < cacheSize / 2) {
1149 return false;
1150 }
1151
robertphillips@google.combac6b052012-09-28 18:06:49 +00001152 SkScalar fracUsed = SkScalarMul(srcRectPtr->width() / bitmap.width(),
1153 srcRectPtr->height() / bitmap.height());
1154 if (fracUsed <= SK_ScalarHalf) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001155 return true;
1156 } else {
1157 return false;
1158 }
1159}
1160
reed@google.comac10a2d2010-12-22 21:39:39 +00001161void SkGpuDevice::drawBitmap(const SkDraw& draw,
1162 const SkBitmap& bitmap,
1163 const SkIRect* srcRectPtr,
1164 const SkMatrix& m,
1165 const SkPaint& paint) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001166
1167 SkRect tmp;
1168 SkRect* tmpPtr = NULL;
1169
1170 // convert from SkIRect to SkRect
1171 if (NULL != srcRectPtr) {
1172 tmp.set(*srcRectPtr);
1173 tmpPtr = &tmp;
1174 }
1175
1176 // We cannot call drawBitmapRect here since 'm' could be anything
1177 this->drawBitmapCommon(draw, bitmap, tmpPtr, m, paint);
1178}
1179
1180void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1181 const SkBitmap& bitmap,
1182 const SkRect* srcRectPtr,
1183 const SkMatrix& m,
1184 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001185 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001186
robertphillips@google.combac6b052012-09-28 18:06:49 +00001187 SkRect srcRect;
reed@google.comac10a2d2010-12-22 21:39:39 +00001188 if (NULL == srcRectPtr) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001189 srcRect.set(0, 0, SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001190 } else {
1191 srcRect = *srcRectPtr;
1192 }
1193
junov@google.comd935cfb2011-06-27 20:48:23 +00001194 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001195 // Convert the bitmap to a shader so that the rect can be drawn
1196 // through drawRect, which supports mask filters.
robertphillips@google.combac6b052012-09-28 18:06:49 +00001197 SkMatrix newM(m);
junov@google.com1d329782011-07-28 20:10:09 +00001198 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001199 const SkBitmap* bitmapPtr = &bitmap;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001200 if (NULL != srcRectPtr) {
1201 SkIRect iSrc;
1202 srcRect.roundOut(&iSrc);
1203 if (!bitmap.extractSubset(&tmp, iSrc)) {
epoger@google.com9ef2d832011-07-01 21:12:20 +00001204 return; // extraction failed
1205 }
1206 bitmapPtr = &tmp;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001207 srcRect.offset(SkIntToScalar(-iSrc.fLeft), SkIntToScalar(-iSrc.fTop));
1208 // The source rect has changed so update the matrix
1209 newM.preTranslate(SkIntToScalar(iSrc.fLeft), SkIntToScalar(iSrc.fTop));
junov@google.comd935cfb2011-06-27 20:48:23 +00001210 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001211
junov@google.comd935cfb2011-06-27 20:48:23 +00001212 SkPaint paintWithTexture(paint);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001213 paintWithTexture.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
junov@google.comd935cfb2011-06-27 20:48:23 +00001214 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001215
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001216 // Transform 'newM' needs to be concatenated to the current matrix,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001217 // rather than transforming the primitive directly, so that 'newM' will
junov@google.com1d329782011-07-28 20:10:09 +00001218 // also affect the behavior of the mask filter.
1219 SkMatrix drawMatrix;
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001220 drawMatrix.setConcat(fContext->getMatrix(), newM);
junov@google.com1d329782011-07-28 20:10:09 +00001221 SkDraw transformedDraw(draw);
1222 transformedDraw.fMatrix = &drawMatrix;
1223
robertphillips@google.combac6b052012-09-28 18:06:49 +00001224 this->drawRect(transformedDraw, srcRect, paintWithTexture);
junov@google.com1d329782011-07-28 20:10:09 +00001225
junov@google.comd935cfb2011-06-27 20:48:23 +00001226 return;
1227 }
1228
bsalomon@google.com5782d712011-01-21 21:03:59 +00001229 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001230 SkAutoCachedTexture colorLutTexture;
1231 if (!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001232 return;
1233 }
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001234 GrTextureParams params;
1235 params.setBilerp(paint.isFilterBitmap());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001236
robertphillips@google.combac6b052012-09-28 18:06:49 +00001237 if (!this->shouldTileBitmap(bitmap, params, srcRectPtr)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001238 // take the simple case
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001239 this->internalDrawBitmap(bitmap, srcRect, m, params, &grPaint);
robertphillips@google.combac6b052012-09-28 18:06:49 +00001240 } else {
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001241 this->drawTiledBitmap(bitmap, srcRect, m, params, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001242 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001243}
1244
1245// Break 'bitmap' into several tiles to draw it since it has already
1246// been determined to be too large to fit in VRAM
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001247void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001248 const SkRect& srcRect,
1249 const SkMatrix& m,
1250 const GrTextureParams& params,
1251 GrPaint* grPaint) {
1252 const int maxTextureSize = fContext->getMaxTextureSize();
1253
1254 int tileSize = determine_tile_size(bitmap, srcRect, maxTextureSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001255
reed@google.comac10a2d2010-12-22 21:39:39 +00001256 // compute clip bounds in local coordinates
robertphillips@google.combac6b052012-09-28 18:06:49 +00001257 SkRect clipRect;
reed@google.comac10a2d2010-12-22 21:39:39 +00001258 {
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001259 const GrRenderTarget* rt = fContext->getRenderTarget();
1260 clipRect.setWH(SkIntToScalar(rt->width()), SkIntToScalar(rt->height()));
1261 if (!fContext->getClip()->fClipStack->intersectRectWithClip(&clipRect)) {
1262 return;
1263 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001264 SkMatrix matrix, inverse;
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001265 matrix.setConcat(fContext->getMatrix(), m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001266 if (!matrix.invert(&inverse)) {
1267 return;
1268 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001269 inverse.mapRect(&clipRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001270 }
1271
bsalomon@google.comfb309512011-11-30 14:13:48 +00001272 int nx = bitmap.width() / tileSize;
1273 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001274 for (int x = 0; x <= nx; x++) {
1275 for (int y = 0; y <= ny; y++) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001276 SkRect tileR;
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001277 tileR.set(SkIntToScalar(x * tileSize),
robertphillips@google.combac6b052012-09-28 18:06:49 +00001278 SkIntToScalar(y * tileSize),
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001279 SkIntToScalar((x + 1) * tileSize),
robertphillips@google.combac6b052012-09-28 18:06:49 +00001280 SkIntToScalar((y + 1) * tileSize));
1281
1282 if (!SkRect::Intersects(tileR, clipRect)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001283 continue;
1284 }
1285
robertphillips@google.combac6b052012-09-28 18:06:49 +00001286 if (!tileR.intersect(srcRect)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001287 continue;
1288 }
1289
1290 SkBitmap tmpB;
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001291 SkIRect iTileR;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001292 tileR.roundOut(&iTileR);
1293 if (bitmap.extractSubset(&tmpB, iTileR)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001294 // now offset it to make it "local" to our tmp bitmap
robertphillips@google.combac6b052012-09-28 18:06:49 +00001295 tileR.offset(SkIntToScalar(-iTileR.fLeft), SkIntToScalar(-iTileR.fTop));
reed@google.comac10a2d2010-12-22 21:39:39 +00001296 SkMatrix tmpM(m);
skia.committer@gmail.comdc3a4e52012-10-02 02:01:24 +00001297 tmpM.preTranslate(SkIntToScalar(iTileR.fLeft),
robertphillips@google.comffad46b2012-10-01 14:32:51 +00001298 SkIntToScalar(iTileR.fTop));
1299
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001300 this->internalDrawBitmap(tmpB, tileR, tmpM, params, grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001301 }
1302 }
1303 }
1304}
1305
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001306namespace {
1307
1308bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1309 // detect pixel disalignment
1310 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1311 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
rmistry@google.comd6176b02012-08-23 18:14:13 +00001312 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001313 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1314 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1315 COLOR_BLEED_TOLERANCE &&
1316 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1317 COLOR_BLEED_TOLERANCE) {
1318 return true;
1319 }
1320 return false;
1321}
1322
1323bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1324 const SkMatrix& m) {
1325 // Only gets called if hasAlignedSamples returned false.
1326 // So we can assume that sampling is axis aligned but not texel aligned.
1327 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001328 SkRect innerSrcRect(srcRect), innerTransformedRect,
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001329 outerTransformedRect(transformedRect);
1330 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1331 m.mapRect(&innerTransformedRect, innerSrcRect);
1332
1333 // The gap between outerTransformedRect and innerTransformedRect
1334 // represents the projection of the source border area, which is
1335 // problematic for color bleeding. We must check whether any
1336 // destination pixels sample the border area.
1337 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1338 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1339 SkIRect outer, inner;
1340 outerTransformedRect.round(&outer);
1341 innerTransformedRect.round(&inner);
1342 // If the inner and outer rects round to the same result, it means the
1343 // border does not overlap any pixel centers. Yay!
1344 return inner != outer;
1345}
1346
1347} // unnamed namespace
1348
reed@google.comac10a2d2010-12-22 21:39:39 +00001349/*
1350 * This is called by drawBitmap(), which has to handle images that may be too
1351 * large to be represented by a single texture.
1352 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001353 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1354 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001355 */
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001356void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001357 const SkRect& srcRect,
reed@google.comac10a2d2010-12-22 21:39:39 +00001358 const SkMatrix& m,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001359 const GrTextureParams& params,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001360 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001361 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1362 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001363
reed@google.com9c49bc32011-07-07 13:42:37 +00001364 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001365 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001366 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001367 return;
1368 }
1369
bsalomon@google.com88becf42012-10-05 14:54:42 +00001370 GrSamplerState* sampler = grPaint->colorSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001371
reed@google.comac10a2d2010-12-22 21:39:39 +00001372 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001373 SkAutoCachedTexture act(this, bitmap, &params, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001374 if (NULL == texture) {
1375 return;
1376 }
1377
robertphillips@google.combac6b052012-09-28 18:06:49 +00001378 GrRect dstRect(srcRect);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001379 GrRect paintRect;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001380 SkScalar wInv = SkScalarInvert(SkIntToScalar(bitmap.width()));
1381 SkScalar hInv = SkScalarInvert(SkIntToScalar(bitmap.height()));
1382 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1383 SkScalarMul(srcRect.fTop, hInv),
1384 SkScalarMul(srcRect.fRight, wInv),
1385 SkScalarMul(srcRect.fBottom, hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001386
rmistry@google.comd6176b02012-08-23 18:14:13 +00001387 bool needsTextureDomain = false;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001388 if (params.isBilerp()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001389 // Need texture domain if drawing a sub rect.
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001390 needsTextureDomain = srcRect.width() < bitmap.width() ||
robertphillips@google.combac6b052012-09-28 18:06:49 +00001391 srcRect.height() < bitmap.height();
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001392 if (m.rectStaysRect() && fContext->getMatrix().rectStaysRect()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001393 // sampling is axis-aligned
robertphillips@google.combac6b052012-09-28 18:06:49 +00001394 GrRect transformedRect;
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001395 SkMatrix srcToDeviceMatrix(m);
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001396 srcToDeviceMatrix.postConcat(fContext->getMatrix());
robertphillips@google.combac6b052012-09-28 18:06:49 +00001397 srcToDeviceMatrix.mapRect(&transformedRect, srcRect);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001398
robertphillips@google.combac6b052012-09-28 18:06:49 +00001399 if (hasAlignedSamples(srcRect, transformedRect)) {
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001400 // We could also turn off filtering here (but we already did a cache lookup with
1401 // params).
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001402 needsTextureDomain = false;
1403 } else {
1404 needsTextureDomain = needsTextureDomain &&
robertphillips@google.combac6b052012-09-28 18:06:49 +00001405 mayColorBleed(srcRect, transformedRect, m);
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001406 }
1407 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001408 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001409
1410 GrRect textureDomain = GrRect::MakeEmpty();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001411 SkAutoTUnref<GrCustomStage> stage;
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001412 if (needsTextureDomain) {
1413 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001414 GrScalar left, top, right, bottom;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001415 if (srcRect.width() > GR_Scalar1) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001416 GrScalar border = GR_ScalarHalf / bitmap.width();
1417 left = paintRect.left() + border;
1418 right = paintRect.right() - border;
1419 } else {
1420 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1421 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001422 if (srcRect.height() > GR_Scalar1) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001423 GrScalar border = GR_ScalarHalf / bitmap.height();
1424 top = paintRect.top() + border;
1425 bottom = paintRect.bottom() - border;
1426 } else {
1427 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1428 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001429 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001430 stage.reset(SkNEW_ARGS(GrTextureDomainEffect, (texture, textureDomain, params)));
1431 } else {
1432 stage.reset(SkNEW_ARGS(GrSingleTextureEffect, (texture, params)));
junov@google.com6acc9b32011-05-16 18:32:07 +00001433 }
bsalomon@google.com88becf42012-10-05 14:54:42 +00001434 grPaint->colorSampler(kBitmapTextureIdx)->setCustomStage(stage);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001435 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001436}
1437
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001438namespace {
1439
1440void apply_custom_stage(GrContext* context,
1441 GrTexture* srcTexture,
1442 GrTexture* dstTexture,
1443 const GrRect& rect,
1444 GrCustomStage* stage) {
1445 SkASSERT(srcTexture && srcTexture->getContext() == context);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001446 GrContext::AutoMatrix am;
1447 am.setIdentity(context);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001448 GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget());
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001449 GrContext::AutoClip acs(context, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001450
1451 GrMatrix sampleM;
1452 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
1453 GrPaint paint;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +00001454 paint.colorSampler(0)->setCustomStage(stage, sampleM);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001455 context->drawRect(paint, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001456}
1457
1458};
1459
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001460static GrTexture* filter_texture(SkDevice* device, GrContext* context,
1461 GrTexture* texture, SkImageFilter* filter,
1462 const GrRect& rect) {
reed@google.com8926b162012-03-23 15:36:36 +00001463 GrAssert(filter);
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001464 SkDeviceImageFilterProxy proxy(device);
reed@google.com8926b162012-03-23 15:36:36 +00001465
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001466 GrTextureDesc desc;
1467 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1468 desc.fWidth = SkScalarCeilToInt(rect.width());
1469 desc.fHeight = SkScalarCeilToInt(rect.height());
bsalomon@google.com0342a852012-08-20 19:22:38 +00001470 desc.fConfig = kRGBA_8888_GrPixelConfig;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001471 GrCustomStage* stage;
reed@google.com8926b162012-03-23 15:36:36 +00001472
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001473 if (filter->canFilterImageGPU()) {
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001474 texture = filter->onFilterImageGPU(&proxy, texture, rect);
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001475 } else if (filter->asNewCustomStage(&stage, texture)) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001476 GrAutoScratchTexture dst(context, desc);
1477 apply_custom_stage(context, texture, dst.texture(), rect, stage);
1478 texture = dst.detach();
1479 stage->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001480 }
1481 return texture;
1482}
1483
reed@google.comac10a2d2010-12-22 21:39:39 +00001484void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001485 int left, int top, const SkPaint& paint) {
1486 // drawSprite is defined to be in device coords.
1487 CHECK_SHOULD_DRAW(draw, true);
reed@google.comac10a2d2010-12-22 21:39:39 +00001488
reed@google.com8926b162012-03-23 15:36:36 +00001489 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001490 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1491 return;
1492 }
1493
reed@google.com76dd2772012-01-05 21:15:07 +00001494 int w = bitmap.width();
1495 int h = bitmap.height();
1496
bsalomon@google.com5782d712011-01-21 21:03:59 +00001497 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001498 SkAutoCachedTexture colorLutTexture;
1499 if(!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001500 return;
1501 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001502
bsalomon@google.com88becf42012-10-05 14:54:42 +00001503 GrSamplerState* sampler = grPaint.colorSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001504
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001505 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001506 sampler->reset();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001507 // draw sprite uses the default texture params
1508 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
bsalomon@google.com88becf42012-10-05 14:54:42 +00001509 grPaint.colorSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001510 (GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001511
reed@google.com8926b162012-03-23 15:36:36 +00001512 SkImageFilter* filter = paint.getImageFilter();
1513 if (NULL != filter) {
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001514 GrTexture* filteredTexture = filter_texture(this, fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001515 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001516 if (filteredTexture) {
bsalomon@google.com88becf42012-10-05 14:54:42 +00001517 grPaint.colorSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001518 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001519 texture = filteredTexture;
1520 filteredTexture->unref();
1521 }
reed@google.com76dd2772012-01-05 21:15:07 +00001522 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001523
bsalomon@google.com5782d712011-01-21 21:03:59 +00001524 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001525 GrRect::MakeXYWH(GrIntToScalar(left),
1526 GrIntToScalar(top),
1527 GrIntToScalar(w),
1528 GrIntToScalar(h)),
1529 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1530 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001531}
1532
reed@google.com33535f32012-09-25 15:37:50 +00001533void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1534 const SkRect* src, const SkRect& dst,
1535 const SkPaint& paint) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001536 SkMatrix matrix;
1537 SkRect bitmapBounds, tmpSrc;
1538
1539 bitmapBounds.set(0, 0,
1540 SkIntToScalar(bitmap.width()),
1541 SkIntToScalar(bitmap.height()));
1542
reed@google.com33535f32012-09-25 15:37:50 +00001543 // Compute matrix from the two rectangles
robertphillips@google.combac6b052012-09-28 18:06:49 +00001544 if (NULL != src) {
1545 tmpSrc = *src;
1546 } else {
1547 tmpSrc = bitmapBounds;
1548 }
1549 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1550
1551 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1552 if (NULL != src) {
1553 if (!bitmapBounds.contains(tmpSrc)) {
1554 if (!tmpSrc.intersect(bitmapBounds)) {
1555 return; // nothing to draw
reed@google.com33535f32012-09-25 15:37:50 +00001556 }
reed@google.com33535f32012-09-25 15:37:50 +00001557 }
reed@google.com33535f32012-09-25 15:37:50 +00001558 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +00001559
robertphillips@google.combac6b052012-09-28 18:06:49 +00001560 this->drawBitmapCommon(draw, bitmap, &tmpSrc, matrix, paint);
reed@google.com33535f32012-09-25 15:37:50 +00001561}
1562
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001563void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001564 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001565 // clear of the source device must occur before CHECK_SHOULD_DRAW
1566 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1567 if (dev->fNeedClear) {
1568 // TODO: could check here whether we really need to draw at all
1569 dev->clear(0x0);
1570 }
1571
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001572 // drawDevice is defined to be in device coords.
1573 CHECK_SHOULD_DRAW(draw, true);
reed@google.comac10a2d2010-12-22 21:39:39 +00001574
bsalomon@google.com5782d712011-01-21 21:03:59 +00001575 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001576 SkAutoCachedTexture colorLutTexture;
bsalomon@google.com88becf42012-10-05 14:54:42 +00001577 grPaint.colorSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001578 if (!dev->bindDeviceAsTexture(&grPaint) ||
twiz@google.com58071162012-07-18 21:41:50 +00001579 !skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001580 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001581 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001582
bsalomon@google.com88becf42012-10-05 14:54:42 +00001583 GrTexture* devTex = grPaint.getColorSampler(kBitmapTextureIdx).getCustomStage()->texture(0);
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001584 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001585
reed@google.com8926b162012-03-23 15:36:36 +00001586 SkImageFilter* filter = paint.getImageFilter();
1587 if (NULL != filter) {
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001588 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001589 SkIntToScalar(devTex->height()));
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001590 GrTexture* filteredTexture = filter_texture(this, fContext, devTex, filter, rect);
reed@google.com8926b162012-03-23 15:36:36 +00001591 if (filteredTexture) {
bsalomon@google.com88becf42012-10-05 14:54:42 +00001592 grPaint.colorSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001593 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001594 devTex = filteredTexture;
1595 filteredTexture->unref();
1596 }
1597 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001598
bsalomon@google.com5782d712011-01-21 21:03:59 +00001599 const SkBitmap& bm = dev->accessBitmap(false);
1600 int w = bm.width();
1601 int h = bm.height();
1602
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001603 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1604 GrIntToScalar(y),
1605 GrIntToScalar(w),
1606 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001607
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001608 // The device being drawn may not fill up its texture (saveLayer uses
1609 // the approximate ).
1610 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1611 GR_Scalar1 * h / devTex->height());
1612
1613 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001614}
1615
reed@google.com8926b162012-03-23 15:36:36 +00001616bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001617 if (!filter->asNewCustomStage(NULL, NULL) &&
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001618 !filter->canFilterImageGPU()) {
reed@google.com76dd2772012-01-05 21:15:07 +00001619 return false;
1620 }
reed@google.com8926b162012-03-23 15:36:36 +00001621 return true;
1622}
1623
1624bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1625 const SkMatrix& ctm,
1626 SkBitmap* result, SkIPoint* offset) {
1627 // want explicitly our impl, so guard against a subclass of us overriding it
1628 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001629 return false;
1630 }
reed@google.com8926b162012-03-23 15:36:36 +00001631
1632 SkAutoLockPixels alp(src, !src.getTexture());
1633 if (!src.getTexture() && !src.readyToDraw()) {
1634 return false;
1635 }
1636
1637 GrPaint paint;
reed@google.com8926b162012-03-23 15:36:36 +00001638
reed@google.com8926b162012-03-23 15:36:36 +00001639 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001640 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1641 // must be pushed upstack.
1642 SkAutoCachedTexture act(this, src, NULL, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001643
1644 result->setConfig(src.config(), src.width(), src.height());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001645 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001646 SkIntToScalar(src.height()));
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001647 GrTexture* resultTexture = filter_texture(this, fContext, texture, filter, rect);
reed@google.com8926b162012-03-23 15:36:36 +00001648 if (resultTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001649 result->setPixelRef(SkNEW_ARGS(SkGrTexturePixelRef,
1650 (resultTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001651 resultTexture->unref();
1652 }
reed@google.com76dd2772012-01-05 21:15:07 +00001653 return true;
1654}
1655
reed@google.comac10a2d2010-12-22 21:39:39 +00001656///////////////////////////////////////////////////////////////////////////////
1657
1658// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001659static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001660 kTriangles_GrPrimitiveType,
1661 kTriangleStrip_GrPrimitiveType,
1662 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001663};
1664
1665void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1666 int vertexCount, const SkPoint vertices[],
1667 const SkPoint texs[], const SkColor colors[],
1668 SkXfermode* xmode,
1669 const uint16_t indices[], int indexCount,
1670 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001671 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001672
bsalomon@google.com5782d712011-01-21 21:03:59 +00001673 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +00001674 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com5782d712011-01-21 21:03:59 +00001675 // we ignore the shader if texs is null.
1676 if (NULL == texs) {
twiz@google.com58071162012-07-18 21:41:50 +00001677 if (!skPaint2GrPaintNoShader(this,
1678 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001679 false,
1680 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001681 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +00001682 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001683 return;
1684 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001685 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001686 if (!skPaint2GrPaintShader(this,
1687 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001688 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001689 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001690 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001691 return;
1692 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001693 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001694
1695 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001696 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001697 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1698#if 0
1699 return
1700#endif
1701 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001702 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001703
bsalomon@google.com498776a2011-08-16 19:20:44 +00001704 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1705 if (NULL != colors) {
1706 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001707 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001708 for (int i = 0; i < vertexCount; ++i) {
rileya@google.com24f3ad12012-07-18 21:47:40 +00001709 convertedColors[i] = SkColor2GrColor(colors[i]);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001710 }
1711 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001712 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001713 fContext->drawVertices(grPaint,
1714 gVertexMode2PrimitiveType[vmode],
1715 vertexCount,
1716 (GrPoint*) vertices,
1717 (GrPoint*) texs,
1718 colors,
1719 indices,
1720 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001721}
1722
1723///////////////////////////////////////////////////////////////////////////////
1724
1725static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001726 GrFontScaler* scaler = (GrFontScaler*)data;
1727 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001728}
1729
1730static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1731 void* auxData;
1732 GrFontScaler* scaler = NULL;
1733 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1734 scaler = (GrFontScaler*)auxData;
1735 }
1736 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001737 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001738 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1739 }
1740 return scaler;
1741}
1742
1743static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1744 SkFixed fx, SkFixed fy,
1745 const SkGlyph& glyph) {
1746 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1747
bungeman@google.com15865a72012-01-11 16:28:04 +00001748 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001749
1750 if (NULL == procs->fFontScaler) {
1751 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1752 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001753
bungeman@google.com15865a72012-01-11 16:28:04 +00001754 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1755 glyph.getSubXFixed(),
1756 glyph.getSubYFixed()),
1757 SkFixedFloorToFixed(fx),
1758 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001759 procs->fFontScaler);
1760}
1761
bsalomon@google.com5782d712011-01-21 21:03:59 +00001762SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001763
1764 // deferred allocation
1765 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001766 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001767 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1768 fDrawProcs->fContext = fContext;
1769 }
1770
1771 // init our (and GL's) state
1772 fDrawProcs->fTextContext = context;
1773 fDrawProcs->fFontScaler = NULL;
1774 return fDrawProcs;
1775}
1776
1777void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1778 size_t byteLength, SkScalar x, SkScalar y,
1779 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001780 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001781
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001782 if (fContext->getMatrix().hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001783 // this guy will just call our drawPath()
1784 draw.drawText((const char*)text, byteLength, x, y, paint);
1785 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001786 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001787
1788 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +00001789 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001790 if (!skPaint2GrPaintShader(this,
1791 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001792 true,
twiz@google.com58071162012-07-18 21:41:50 +00001793 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001794 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001795 return;
1796 }
bsalomon@google.com0e354aa2012-10-08 20:44:25 +00001797 GrTextContext context(fContext, grPaint);
tomhudson@google.com375ff852012-06-29 18:37:57 +00001798 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001799 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1800 }
1801}
1802
1803void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1804 size_t byteLength, const SkScalar pos[],
1805 SkScalar constY, int scalarsPerPos,
1806 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001807 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001808
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001809 if (fContext->getMatrix().hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001810 // this guy will just call our drawPath()
1811 draw.drawPosText((const char*)text, byteLength, pos, constY,
1812 scalarsPerPos, paint);
1813 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001814 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001815
1816 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +00001817 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001818 if (!skPaint2GrPaintShader(this,
1819 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001820 true,
twiz@google.com58071162012-07-18 21:41:50 +00001821 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001822 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001823 return;
1824 }
bsalomon@google.com0e354aa2012-10-08 20:44:25 +00001825 GrTextContext context(fContext, grPaint);
tomhudson@google.com375ff852012-06-29 18:37:57 +00001826 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001827 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1828 scalarsPerPos, paint);
1829 }
1830}
1831
1832void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1833 size_t len, const SkPath& path,
1834 const SkMatrix* m, const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001835 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001836
1837 SkASSERT(draw.fDevice == this);
1838 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1839}
1840
1841///////////////////////////////////////////////////////////////////////////////
1842
reed@google.comf67e4cf2011-03-15 20:56:58 +00001843bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1844 if (!paint.isLCDRenderText()) {
1845 // we're cool with the paint as is
1846 return false;
1847 }
1848
1849 if (paint.getShader() ||
1850 paint.getXfermode() || // unless its srcover
1851 paint.getMaskFilter() ||
1852 paint.getRasterizer() ||
1853 paint.getColorFilter() ||
1854 paint.getPathEffect() ||
1855 paint.isFakeBoldText() ||
1856 paint.getStyle() != SkPaint::kFill_Style) {
1857 // turn off lcd
1858 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1859 flags->fHinting = paint.getHinting();
1860 return true;
1861 }
1862 // we're cool with the paint as is
1863 return false;
1864}
1865
reed@google.com75d939b2011-12-07 15:07:23 +00001866void SkGpuDevice::flush() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +00001867 DO_DEFERRED_CLEAR();
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001868 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001869}
1870
reed@google.comf67e4cf2011-03-15 20:56:58 +00001871///////////////////////////////////////////////////////////////////////////////
1872
bsalomon@google.comfb309512011-11-30 14:13:48 +00001873bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001874 const GrTextureParams& params) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001875 uint64_t key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001876 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001877
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001878 GrTextureDesc desc;
1879 desc.fWidth = bitmap.width();
1880 desc.fHeight = bitmap.height();
rileya@google.com24f3ad12012-07-18 21:47:40 +00001881 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config());
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001882
robertphillips@google.com9c2ea842012-08-13 17:47:59 +00001883 GrCacheData cacheData(key);
1884
1885 return this->context()->isTextureInCache(desc, cacheData, &params);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001886}
1887
1888
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001889SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1890 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001891 bool isOpaque,
1892 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001893 GrTextureDesc desc;
1894 desc.fConfig = fRenderTarget->config();
1895 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1896 desc.fWidth = width;
1897 desc.fHeight = height;
1898 desc.fSampleCnt = fRenderTarget->numSamples();
1899
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001900 GrTexture* texture;
1901 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001902 // Skia's convention is to only clear a device if it is non-opaque.
1903 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001904
1905#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1906 // layers are never draw in repeat modes, so we can request an approx
1907 // match and ignore any padding.
1908 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1909 GrContext::kApprox_ScratchTexMatch :
1910 GrContext::kExact_ScratchTexMatch;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001911 texture = fContext->lockScratchTexture(desc, matchType);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001912#else
1913 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1914 texture = tunref.get();
1915#endif
1916 if (texture) {
1917 return SkNEW_ARGS(SkGpuDevice,(fContext,
1918 texture,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001919 needClear));
1920 } else {
1921 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1922 width, height);
1923 return NULL;
1924 }
1925}
1926
1927SkGpuDevice::SkGpuDevice(GrContext* context,
1928 GrTexture* texture,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001929 bool needClear)
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001930 : SkDevice(make_bitmap(context, texture->asRenderTarget())) {
1931
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001932 GrAssert(texture && texture->asRenderTarget());
bsalomon@google.com8090e652012-08-28 15:07:11 +00001933 // This constructor is called from onCreateCompatibleDevice. It has locked the RT in the texture
1934 // cache. We pass true for the third argument so that it will get unlocked.
1935 this->initFromRenderTarget(context, texture->asRenderTarget(), true);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001936 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001937}