blob: 14eeace5631c9fe2670086ac27cd6cea6b0672e0 [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,
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000207 int height,
208 int sampleCount)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000209 : SkDevice(config, width, height, false /*isOpaque*/) {
210
reed@google.comac10a2d2010-12-22 21:39:39 +0000211 fDrawProcs = NULL;
212
reed@google.com7b201d22011-01-11 18:59:23 +0000213 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000214 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000215
reed@google.comac10a2d2010-12-22 21:39:39 +0000216 fRenderTarget = NULL;
217 fNeedClear = false;
218
reed@google.comaf951c92011-06-16 19:10:39 +0000219 if (config != SkBitmap::kRGB_565_Config) {
220 config = SkBitmap::kARGB_8888_Config;
221 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000222
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000223 GrTextureDesc desc;
224 desc.fFlags = kRenderTarget_GrTextureFlagBit;
225 desc.fWidth = width;
226 desc.fHeight = height;
robertphillips@google.comd3eb3362012-10-31 13:56:35 +0000227 desc.fConfig = SkBitmapConfig2GrPixelConfig(config);
228 desc.fSampleCnt = sampleCount;
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
robertphillips@google.com055f9082012-10-24 13:24:11 +0000259 if (fContext->getClip() == &fClipData) {
260 fContext->setClip(NULL);
261 }
262
bsalomon@google.coma2921122012-08-28 12:34:17 +0000263 SkSafeUnref(fRenderTarget);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000264 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000265}
266
reed@google.comac10a2d2010-12-22 21:39:39 +0000267///////////////////////////////////////////////////////////////////////////////
268
269void SkGpuDevice::makeRenderTargetCurrent() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000270 DO_DEFERRED_CLEAR();
reed@google.comac10a2d2010-12-22 21:39:39 +0000271 fContext->setRenderTarget(fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000272}
273
274///////////////////////////////////////////////////////////////////////////////
275
bsalomon@google.comc4364992011-11-07 15:54:49 +0000276namespace {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000277GrPixelConfig config8888_to_grconfig_and_flags(SkCanvas::Config8888 config8888, uint32_t* flags) {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000278 switch (config8888) {
279 case SkCanvas::kNative_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000280 *flags = 0;
281 return kSkia8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000282 case SkCanvas::kNative_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000283 *flags = GrContext::kUnpremul_PixelOpsFlag;
284 return kSkia8888_PM_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000285 case SkCanvas::kBGRA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000286 *flags = 0;
287 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000288 case SkCanvas::kBGRA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000289 *flags = GrContext::kUnpremul_PixelOpsFlag;
290 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000291 case SkCanvas::kRGBA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000292 *flags = 0;
293 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000294 case SkCanvas::kRGBA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000295 *flags = GrContext::kUnpremul_PixelOpsFlag;
296 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000297 default:
298 GrCrash("Unexpected Config8888.");
bsalomon@google.comccaa0022012-09-25 19:55:07 +0000299 *flags = 0; // suppress warning
bsalomon@google.comc4364992011-11-07 15:54:49 +0000300 return kSkia8888_PM_GrPixelConfig;
301 }
302}
303}
304
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000305bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
306 int x, int y,
307 SkCanvas::Config8888 config8888) {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000308 DO_DEFERRED_CLEAR();
bsalomon@google.com910267d2011-11-02 20:06:25 +0000309 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
310 SkASSERT(!bitmap.isNull());
311 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000312
bsalomon@google.com910267d2011-11-02 20:06:25 +0000313 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000314 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000315 uint32_t flags;
316 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000317 return fContext->readRenderTargetPixels(fRenderTarget,
318 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000319 bitmap.width(),
320 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000321 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000322 bitmap.getPixels(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000323 bitmap.rowBytes(),
324 flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000325}
326
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000327void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
328 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000329 SkAutoLockPixels alp(bitmap);
330 if (!bitmap.readyToDraw()) {
331 return;
332 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000333
334 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000335 uint32_t flags;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000336 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000337 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000338 } else {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000339 flags = 0;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000340 config= SkBitmapConfig2GrPixelConfig(bitmap.config());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000341 }
342
bsalomon@google.com6f379512011-11-16 20:36:03 +0000343 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000344 config, bitmap.getPixels(), bitmap.rowBytes(), flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000345}
346
robertphillips@google.com46f93502012-08-07 15:38:08 +0000347namespace {
348void purgeClipCB(int genID, void* data) {
349 GrContext* context = (GrContext*) data;
350
351 if (SkClipStack::kInvalidGenID == genID ||
352 SkClipStack::kEmptyGenID == genID ||
353 SkClipStack::kWideOpenGenID == genID) {
354 // none of these cases will have a cached clip mask
355 return;
356 }
357
358}
359};
360
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000361void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
362 INHERITED::onAttachToCanvas(canvas);
363
364 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000365 fClipData.fClipStack = canvas->getClipStack();
robertphillips@google.com46f93502012-08-07 15:38:08 +0000366
367 fClipData.fClipStack->addPurgeClipCallback(purgeClipCB, fContext);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000368}
369
370void SkGpuDevice::onDetachFromCanvas() {
371 INHERITED::onDetachFromCanvas();
372
robertphillips@google.com46f93502012-08-07 15:38:08 +0000373 // TODO: iterate through the clip stack and clean up any cached clip masks
374 fClipData.fClipStack->removePurgeClipCallback(purgeClipCB, fContext);
375
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000376 fClipData.fClipStack = NULL;
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000377}
378
robertphillips@google.com607fe072012-07-24 13:54:00 +0000379#ifdef SK_DEBUG
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000380static void check_bounds(const GrClipData& clipData,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000381 const SkRegion& clipRegion,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000382 int renderTargetWidth,
383 int renderTargetHeight) {
384
robertphillips@google.com7b112892012-07-31 15:18:21 +0000385 SkIRect devBound;
386
387 devBound.setLTRB(0, 0, renderTargetWidth, renderTargetHeight);
388
robertphillips@google.com607fe072012-07-24 13:54:00 +0000389 SkClipStack::BoundsType boundType;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000390 SkRect canvTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000391
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000392 clipData.fClipStack->getBounds(&canvTemp, &boundType);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000393 if (SkClipStack::kNormal_BoundsType == boundType) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000394 SkIRect devTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000395
robertphillips@google.com7b112892012-07-31 15:18:21 +0000396 canvTemp.roundOut(&devTemp);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000397
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000398 devTemp.offset(-clipData.fOrigin.fX, -clipData.fOrigin.fY);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000399
robertphillips@google.com7b112892012-07-31 15:18:21 +0000400 if (!devBound.intersect(devTemp)) {
401 devBound.setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000402 }
403 }
404
robertphillips@google.com768fee82012-08-02 12:42:43 +0000405 GrAssert(devBound.contains(clipRegion.getBounds()));
robertphillips@google.com607fe072012-07-24 13:54:00 +0000406}
407#endif
408
reed@google.comac10a2d2010-12-22 21:39:39 +0000409///////////////////////////////////////////////////////////////////////////////
410
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000411// call this every draw call, to ensure that the context reflects our state,
reed@google.comac10a2d2010-12-22 21:39:39 +0000412// and not the state from some other canvas/device
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000413void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000414 GrAssert(NULL != fClipData.fClipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000415
reed@google.comac10a2d2010-12-22 21:39:39 +0000416 fContext->setRenderTarget(fRenderTarget);
417
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000418 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000419
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000420 if (forceIdentity) {
421 fContext->setIdentityMatrix();
422 } else {
423 fContext->setMatrix(*draw.fMatrix);
424 }
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000425 fClipData.fOrigin = this->getOrigin();
reed@google.comac10a2d2010-12-22 21:39:39 +0000426
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000427#ifdef SK_DEBUG
428 check_bounds(fClipData, *draw.fClip, fRenderTarget->width(), fRenderTarget->height());
429#endif
430
431 fContext->setClip(&fClipData);
432
433 DO_DEFERRED_CLEAR();
reed@google.comac10a2d2010-12-22 21:39:39 +0000434}
435
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000436SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000437 DO_DEFERRED_CLEAR();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000438 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000439}
440
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000441bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
bsalomon@google.coma2921122012-08-28 12:34:17 +0000442 GrTexture* texture = fRenderTarget->asTexture();
443 if (NULL != texture) {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000444 paint->colorStage(kBitmapTextureIdx)->setEffect(
bsalomon@google.coma2921122012-08-28 12:34:17 +0000445 SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000446 return true;
447 }
448 return false;
449}
450
451///////////////////////////////////////////////////////////////////////////////
452
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000453SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
454SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
455SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
456SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
457SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
458 shader_type_mismatch);
rileya@google.com3e332582012-07-03 13:43:35 +0000459SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
460 shader_type_mismatch);
rileya@google.com22e57f92012-07-19 15:16:19 +0000461SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
462SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000463
bsalomon@google.com84405e02012-03-05 19:57:21 +0000464namespace {
465
466// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
467// justAlpha indicates that skPaint's alpha should be used rather than the color
468// Callers may subsequently modify the GrPaint. Setting constantColor indicates
469// that the final paint will draw the same color at every pixel. This allows
470// an optimization where the the color filter can be applied to the skPaint's
twiz@google.com58071162012-07-18 21:41:50 +0000471// color once while converting to GrPaint and then ignored.
472inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
473 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000474 bool justAlpha,
475 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000476 SkGpuDevice::SkAutoCachedTexture* act,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000477 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000478
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000479 grPaint->setDither(skPaint.isDither());
480 grPaint->setAntiAlias(skPaint.isAntiAlias());
bsalomon@google.com5782d712011-01-21 21:03:59 +0000481
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000482 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
483 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000484
485 SkXfermode* mode = skPaint.getXfermode();
486 if (mode) {
487 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000488 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000489#if 0
490 return false;
491#endif
492 }
493 }
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000494 grPaint->setBlendFunc(sk_blend_to_grblend(sm), sk_blend_to_grblend(dm));
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000495
bsalomon@google.com5782d712011-01-21 21:03:59 +0000496 if (justAlpha) {
497 uint8_t alpha = skPaint.getAlpha();
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000498 grPaint->setColor(GrColorPackRGBA(alpha, alpha, alpha, alpha));
Scroggod757df22011-05-16 13:11:16 +0000499 // justAlpha is currently set to true only if there is a texture,
500 // so constantColor should not also be true.
501 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000502 } else {
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000503 grPaint->setColor(SkColor2GrColor(skPaint.getColor()));
bsalomon@google.com88becf42012-10-05 14:54:42 +0000504 GrAssert(!grPaint->isColorStageEnabled(kShaderTextureIdx));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000505 }
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000506
Scroggo97c88c22011-05-11 14:05:25 +0000507 SkColorFilter* colorFilter = skPaint.getColorFilter();
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000508 if (NULL != colorFilter) {
509 // if the source color is a constant then apply the filter here once rather than per pixel
510 // in a shader.
511 if (constantColor) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000512 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000513 grPaint->setColor(SkColor2GrColor(filtered));
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000514 } else {
bsalomon@google.com021fc732012-10-25 12:47:42 +0000515 SkAutoTUnref<GrEffect> effect(colorFilter->asNewEffect(dev->context()));
516 if (NULL != effect.get()) {
bsalomon@google.com08283af2012-10-26 13:01:20 +0000517 grPaint->colorStage(kColorFilterTextureIdx)->setEffect(effect);
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000518 } else {
bsalomon@google.com8ea78d82012-10-24 20:11:30 +0000519 // TODO: rewrite this using asNewEffect()
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000520 SkColor color;
521 SkXfermode::Mode filterMode;
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000522 if (colorFilter->asColorMode(&color, &filterMode)) {
523 grPaint->setXfermodeColorFilter(filterMode, SkColor2GrColor(color));
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000524 }
525 }
Scroggod757df22011-05-16 13:11:16 +0000526 }
Scroggo97c88c22011-05-11 14:05:25 +0000527 }
bsalomon@google.com67e78c92012-10-17 13:36:14 +0000528
bsalomon@google.com5782d712011-01-21 21:03:59 +0000529 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000530}
531
bsalomon@google.com84405e02012-03-05 19:57:21 +0000532// This function is similar to skPaint2GrPaintNoShader but also converts
bsalomon@google.com08283af2012-10-26 13:01:20 +0000533// skPaint's shader to a GrTexture/GrEffectStage if possible. The texture to
bsalomon@google.com84405e02012-03-05 19:57:21 +0000534// be used is set on grPaint and returned in param act. constantColor has the
535// same meaning as in skPaint2GrPaintNoShader.
536inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
537 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000538 bool constantColor,
bsalomon@google.com88becf42012-10-05 14:54:42 +0000539 SkGpuDevice::SkAutoCachedTexture textures[GrPaint::kMaxColorStages],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000540 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000541 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000542 if (NULL == shader) {
twiz@google.com58071162012-07-18 21:41:50 +0000543 return skPaint2GrPaintNoShader(dev,
544 skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000545 false,
546 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000547 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000548 grPaint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000549 } else if (!skPaint2GrPaintNoShader(dev, skPaint, true, false,
twiz@google.com58071162012-07-18 21:41:50 +0000550 &textures[kColorFilterTextureIdx], grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000551 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000552 }
553
bsalomon@google.com08283af2012-10-26 13:01:20 +0000554 GrEffectStage* stage = grPaint->colorStage(kShaderTextureIdx);
555 if (shader->asNewEffect(dev->context(), stage)) {
rileya@google.com91f319c2012-07-25 17:18:31 +0000556 return true;
557 }
558
reed@google.comac10a2d2010-12-22 21:39:39 +0000559 SkBitmap bitmap;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000560 SkMatrix matrix;
reed@google.comac10a2d2010-12-22 21:39:39 +0000561 SkShader::TileMode tileModes[2];
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000562 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, &matrix, tileModes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000563
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000564 if (SkShader::kNone_BitmapType == bmptype) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000565 SkShader::GradientInfo info;
566 SkColor color;
567
568 info.fColors = &color;
569 info.fColorOffsets = NULL;
570 info.fColorCount = 1;
571 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
572 SkPaint copy(skPaint);
573 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000574 // modulate the paint alpha by the shader's solid color alpha
575 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
576 copy.setColor(SkColorSetA(color, newA));
twiz@google.com58071162012-07-18 21:41:50 +0000577 return skPaint2GrPaintNoShader(dev,
578 copy,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000579 false,
580 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000581 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000582 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000583 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000584 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000585 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000586
bsalomon@google.comf94b3a42012-10-31 18:09:01 +0000587 // since our texture coords will be in local space, we whack the texture
588 // matrix to map them back into 0...1 before we load it
589 if (shader->hasLocalMatrix()) {
590 SkMatrix inverse;
591 if (!shader->getLocalMatrix().invert(&inverse)) {
592 return false;
593 }
594 matrix.preConcat(inverse);
595 }
596
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000597 // Must set wrap and filter on the sampler before requesting a texture.
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000598 GrTextureParams params(tileModes, skPaint.isFilterBitmap());
599 GrTexture* texture = textures[kShaderTextureIdx].set(dev, bitmap, &params);
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000600
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000601 if (NULL == texture) {
602 SkDebugf("Couldn't convert bitmap to texture.\n");
603 return false;
604 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000605
reed@google.comac10a2d2010-12-22 21:39:39 +0000606 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com81712882012-11-01 17:12:34 +0000607 SkScalar sx = SkFloatToScalar(1.f / bitmap.width());
608 SkScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000609 matrix.postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000610 }
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000611 stage->setEffect(SkNEW_ARGS(GrSingleTextureEffect, (texture, matrix, params)))->unref();
bsalomon@google.com5782d712011-01-21 21:03:59 +0000612
613 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000614}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000615}
reed@google.comac10a2d2010-12-22 21:39:39 +0000616
617///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000618void SkGpuDevice::clear(SkColor color) {
robertphillips@google.comea5d8af2012-11-02 17:38:28 +0000619 fContext->clear(NULL, SkColor2GrColor(color), fRenderTarget);
bsalomon@google.coma6926b12012-10-10 15:25:50 +0000620 fNeedClear = false;
bsalomon@google.com398109c2011-04-14 18:40:27 +0000621}
622
reed@google.comac10a2d2010-12-22 21:39:39 +0000623void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000624 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000625
bsalomon@google.com5782d712011-01-21 21:03:59 +0000626 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +0000627 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000628 if (!skPaint2GrPaintShader(this,
629 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000630 true,
twiz@google.com58071162012-07-18 21:41:50 +0000631 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000632 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000633 return;
634 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000635
636 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000637}
638
639// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000640static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000641 kPoints_GrPrimitiveType,
642 kLines_GrPrimitiveType,
643 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000644};
645
646void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000647 size_t count, const SkPoint pts[], const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000648 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000649
650 SkScalar width = paint.getStrokeWidth();
651 if (width < 0) {
652 return;
653 }
654
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000655 // we only handle hairlines and paints without path effects or mask filters,
656 // else we let the SkDraw call our drawPath()
657 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000658 draw.drawPoints(mode, count, pts, paint, true);
659 return;
660 }
661
bsalomon@google.com5782d712011-01-21 21:03:59 +0000662 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +0000663 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000664 if (!skPaint2GrPaintShader(this,
665 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000666 true,
twiz@google.com58071162012-07-18 21:41:50 +0000667 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000668 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000669 return;
670 }
671
bsalomon@google.com5782d712011-01-21 21:03:59 +0000672 fContext->drawVertices(grPaint,
673 gPointMode2PrimtiveType[mode],
674 count,
675 (GrPoint*)pts,
676 NULL,
677 NULL,
678 NULL,
679 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000680}
681
reed@google.comc9aa5872011-04-05 21:05:37 +0000682///////////////////////////////////////////////////////////////////////////////
683
reed@google.comac10a2d2010-12-22 21:39:39 +0000684void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000685 const SkPaint& paint) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000686 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000687 CHECK_SHOULD_DRAW(draw, false);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000688
bungeman@google.com79bd8772011-07-18 15:34:08 +0000689 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000690 SkScalar width = paint.getStrokeWidth();
691
692 /*
693 We have special code for hairline strokes, miter-strokes, and fills.
694 Anything else we just call our path code.
695 */
696 bool usePath = doStroke && width > 0 &&
697 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000698 // another two reasons we might need to call drawPath...
699 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000700 usePath = true;
701 }
reed@google.com67db6642011-05-26 11:46:35 +0000702 // until we aa rotated rects...
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000703 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
reed@google.com67db6642011-05-26 11:46:35 +0000704 usePath = true;
705 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000706 // small miter limit means right angles show bevel...
707 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
708 paint.getStrokeMiter() < SK_ScalarSqrt2)
709 {
710 usePath = true;
711 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000712 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000713 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
714 usePath = true;
715 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000716
717 if (usePath) {
718 SkPath path;
719 path.addRect(rect);
720 this->drawPath(draw, path, paint, NULL, true);
721 return;
722 }
723
724 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +0000725 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000726 if (!skPaint2GrPaintShader(this,
727 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000728 true,
twiz@google.com58071162012-07-18 21:41:50 +0000729 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000730 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000731 return;
732 }
reed@google.com20efde72011-05-09 17:00:02 +0000733 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000734}
735
reed@google.com69302852011-02-16 18:08:07 +0000736#include "SkMaskFilter.h"
737#include "SkBounder.h"
738
bsalomon@google.com85003222012-03-28 14:44:37 +0000739///////////////////////////////////////////////////////////////////////////////
740
741// helpers for applying mask filters
742namespace {
743
744GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000745 switch (fillType) {
746 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000747 return kWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000748 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000749 return kEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000750 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000751 return kInverseWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000752 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000753 return kInverseEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000754 default:
755 SkDebugf("Unsupported path fill type\n");
bsalomon@google.com47059542012-06-06 20:51:20 +0000756 return kHairLine_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000757 }
758}
759
bsalomon@google.com85003222012-03-28 14:44:37 +0000760// We prefer to blur small rect with small radius via CPU.
761#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
762#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
763inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
764 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
765 rect.height() <= MIN_GPU_BLUR_SIZE &&
766 radius <= MIN_GPU_BLUR_RADIUS) {
767 return true;
768 }
769 return false;
770}
771
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000772bool drawWithGPUMaskFilter(GrContext* context, const SkPath& devPath,
773 SkMaskFilter* filter, const SkRegion& clip,
774 SkBounder* bounder, GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000775#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000776 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000777#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000778 SkMaskFilter::BlurInfo info;
779 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000780 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000781 return false;
782 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000783 SkScalar radius = info.fIgnoreTransform ? info.fRadius
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000784 : context->getMatrix().mapRadius(info.fRadius);
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000785 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000786 if (radius <= 0) {
787 return false;
788 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000789
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000790 SkRect srcRect = devPath.getBounds();
bsalomon@google.com85003222012-03-28 14:44:37 +0000791 if (shouldDrawBlurWithCPU(srcRect, radius)) {
792 return false;
793 }
794
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000795 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000796 float sigma3 = sigma * 3.0f;
797
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000798 SkRect clipRect;
799 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000800
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000801 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000802 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
803 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000804 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000805 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000806 SkIRect finalIRect;
807 finalRect.roundOut(&finalIRect);
808 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000809 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000810 }
811 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000812 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000813 }
814 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000815 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000816 GrTextureDesc desc;
817 desc.fFlags = kRenderTarget_GrTextureFlagBit;
818 desc.fWidth = SkScalarCeilToInt(srcRect.width());
819 desc.fHeight = SkScalarCeilToInt(srcRect.height());
820 // We actually only need A8, but it often isn't supported as a
821 // render target so default to RGBA_8888
bsalomon@google.com0342a852012-08-20 19:22:38 +0000822 desc.fConfig = kRGBA_8888_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000823
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000824 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
825 desc.fConfig = kAlpha_8_GrPixelConfig;
826 }
827
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000828 GrAutoScratchTexture pathEntry(context, desc);
829 GrTexture* pathTexture = pathEntry.texture();
830 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000831 return false;
832 }
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000833
robertphillips@google.comccb39502012-10-01 18:25:13 +0000834 SkAutoTUnref<GrTexture> blurTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000835
robertphillips@google.comccb39502012-10-01 18:25:13 +0000836 {
837 GrContext::AutoRenderTarget art(context, pathTexture->asRenderTarget());
838 GrContext::AutoClip ac(context, srcRect);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000839
robertphillips@google.comccb39502012-10-01 18:25:13 +0000840 context->clear(NULL, 0);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000841
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000842 GrPaint tempPaint;
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000843 if (grp->isAntiAlias()) {
844 tempPaint.setAntiAlias(true);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000845 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
846 // blend coeff of zero requires dual source blending support in order
847 // to properly blend partially covered pixels. This means the AA
848 // code path may not be taken. So we use a dst blend coeff of ISA. We
849 // could special case AA draws to a dst surface with known alpha=0 to
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000850 // use a zero dst coeff when dual source blending isn't available.f
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000851 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000852 }
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000853
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000854 GrContext::AutoMatrix am;
855
856 // Draw hard shadow to pathTexture with path top-left at origin using tempPaint.
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000857 SkMatrix translate;
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000858 translate.setTranslate(offset.fX, offset.fY);
859 am.set(context, translate);
860 context->drawPath(tempPaint, devPath, pathFillType);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000861
862 // If we're doing a normal blur, we can clobber the pathTexture in the
863 // gaussianBlur. Otherwise, we need to save it for later compositing.
864 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
skia.committer@gmail.comdc3a4e52012-10-02 02:01:24 +0000865 blurTexture.reset(context->gaussianBlur(pathTexture, isNormalBlur,
robertphillips@google.comccb39502012-10-01 18:25:13 +0000866 srcRect, sigma, sigma));
robertphillips@google.com7d126752012-10-19 12:56:26 +0000867 if (NULL == blurTexture) {
868 return false;
869 }
robertphillips@google.comccb39502012-10-01 18:25:13 +0000870
871 if (!isNormalBlur) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000872 context->setIdentityMatrix();
robertphillips@google.comccb39502012-10-01 18:25:13 +0000873 GrPaint paint;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000874 SkMatrix matrix;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000875 matrix.setIDiv(pathTexture->width(), pathTexture->height());
robertphillips@google.comccb39502012-10-01 18:25:13 +0000876 // Blend pathTexture over blurTexture.
877 context->setRenderTarget(blurTexture->asRenderTarget());
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000878 paint.colorStage(0)->setEffect(SkNEW_ARGS(GrSingleTextureEffect, (pathTexture, matrix)))->unref();
robertphillips@google.comccb39502012-10-01 18:25:13 +0000879 if (SkMaskFilter::kInner_BlurType == blurType) {
880 // inner: dst = dst * src
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000881 paint.setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000882 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
883 // solid: dst = src + dst - src * dst
884 // = (1 - dst) * src + 1 * dst
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000885 paint.setBlendFunc(kIDC_GrBlendCoeff, kOne_GrBlendCoeff);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000886 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
887 // outer: dst = dst * (1 - src)
888 // = 0 * src + (1 - src) * dst
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000889 paint.setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000890 }
891 context->drawRect(paint, srcRect);
892 }
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000893 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000894
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000895 GrContext::AutoMatrix am;
896 if (!am.setIdentity(context, grp)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000897 return false;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000898 }
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +0000899
bsalomon@google.com88becf42012-10-05 14:54:42 +0000900 static const int MASK_IDX = GrPaint::kMaxCoverageStages - 1;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000901 // we assume the last mask index is available for use
bsalomon@google.com88becf42012-10-05 14:54:42 +0000902 GrAssert(!grp->isCoverageStageEnabled(MASK_IDX));
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000903
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000904 SkMatrix matrix;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000905 matrix.setTranslate(-finalRect.fLeft, -finalRect.fTop);
906 matrix.postIDiv(blurTexture->width(), blurTexture->height());
907
bsalomon@google.com08283af2012-10-26 13:01:20 +0000908 grp->coverageStage(MASK_IDX)->reset();
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000909 grp->coverageStage(MASK_IDX)->setEffect(SkNEW_ARGS(GrSingleTextureEffect, (blurTexture, matrix)))->unref();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000910 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000911 return true;
912}
913
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000914bool drawWithMaskFilter(GrContext* context, const SkPath& devPath,
915 SkMaskFilter* filter, const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000916 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000917 SkMask srcM, dstM;
918
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000919 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
920 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
reed@google.com69302852011-02-16 18:08:07 +0000921 return false;
922 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000923 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000924
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000925 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
reed@google.com69302852011-02-16 18:08:07 +0000926 return false;
927 }
928 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000929 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000930
931 if (clip.quickReject(dstM.fBounds)) {
932 return false;
933 }
934 if (bounder && !bounder->doIRect(dstM.fBounds)) {
935 return false;
936 }
937
938 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000939 // the current clip (and identity matrix) and GrPaint settings
940 GrContext::AutoMatrix am;
941 am.setIdentity(context, grp);
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000942
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000943 GrTextureDesc desc;
944 desc.fWidth = dstM.fBounds.width();
945 desc.fHeight = dstM.fBounds.height();
946 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +0000947
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000948 GrAutoScratchTexture ast(context, desc);
949 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000950
reed@google.com69302852011-02-16 18:08:07 +0000951 if (NULL == texture) {
952 return false;
953 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000954 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000955 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000956
bsalomon@google.com88becf42012-10-05 14:54:42 +0000957 static const int MASK_IDX = GrPaint::kMaxCoverageStages - 1;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000958 // we assume the last mask index is available for use
bsalomon@google.com88becf42012-10-05 14:54:42 +0000959 GrAssert(!grp->isCoverageStageEnabled(MASK_IDX));
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000960
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000961 SkMatrix m;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000962 m.setTranslate(-dstM.fBounds.fLeft*SK_Scalar1, -dstM.fBounds.fTop*SK_Scalar1);
963 m.postIDiv(texture->width(), texture->height());
964
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000965 grp->coverageStage(MASK_IDX)->setEffect(SkNEW_ARGS(GrSingleTextureEffect, (texture, m)))->unref();
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000966 GrRect d;
bsalomon@google.com81712882012-11-01 17:12:34 +0000967 d.setLTRB(SkIntToScalar(dstM.fBounds.fLeft),
968 SkIntToScalar(dstM.fBounds.fTop),
969 SkIntToScalar(dstM.fBounds.fRight),
970 SkIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000971
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000972 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000973 return true;
974}
reed@google.com69302852011-02-16 18:08:07 +0000975
bsalomon@google.com85003222012-03-28 14:44:37 +0000976}
977
978///////////////////////////////////////////////////////////////////////////////
979
reed@google.com0c219b62011-02-16 21:31:18 +0000980void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000981 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000982 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000983 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000984 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000985
reed@google.comfe626382011-09-21 13:50:35 +0000986 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000987
bsalomon@google.com5782d712011-01-21 21:03:59 +0000988 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +0000989 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000990 if (!skPaint2GrPaintShader(this,
991 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000992 true,
twiz@google.com58071162012-07-18 21:41:50 +0000993 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000994 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000995 return;
996 }
997
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000998 // can we cheat, and threat a thin stroke as a hairline w/ coverage
999 // if we can, we draw lots faster (raster device does this same test)
1000 SkScalar hairlineCoverage;
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001001 if (SkDrawTreatAsHairline(paint, fContext->getMatrix(), &hairlineCoverage)) {
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +00001002 doFill = false;
bsalomon@google.comc7448ce2012-10-05 19:04:13 +00001003 grPaint.setCoverage(SkScalarRoundToInt(hairlineCoverage * grPaint.getCoverage()));
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +00001004 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001005
reed@google.comfe626382011-09-21 13:50:35 +00001006 // If we have a prematrix, apply it to the path, optimizing for the case
1007 // where the original path can in fact be modified in place (even though
1008 // its parameter type is const).
1009 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1010 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001011
1012 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001013 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001014
reed@google.come3445642011-02-16 23:20:39 +00001015 if (!pathIsMutable) {
1016 result = &tmpPath;
1017 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001018 }
reed@google.come3445642011-02-16 23:20:39 +00001019 // should I push prePathMatrix on our MV stack temporarily, instead
1020 // of applying it here? See SkDraw.cpp
1021 pathPtr->transform(*prePathMatrix, result);
1022 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001023 }
reed@google.com0c219b62011-02-16 21:31:18 +00001024 // at this point we're done with prePathMatrix
1025 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001026
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001027 if (paint.getPathEffect() ||
1028 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001029 // it is safe to use tmpPath here, even if we already used it for the
1030 // prepathmatrix, since getFillPath can take the same object for its
1031 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001032 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001033 pathPtr = &tmpPath;
1034 }
1035
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001036 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001037 // avoid possibly allocating a new path in transform if we can
1038 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1039
1040 // transform the path into device space
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001041 pathPtr->transform(fContext->getMatrix(), devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001042 GrPathFill pathFillType = doFill ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001043 skToGrFillType(devPathPtr->getFillType()) : kHairLine_GrPathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001044 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001045 *draw.fClip, draw.fBounder, &grPaint, pathFillType)) {
rmistry@google.comd6176b02012-08-23 18:14:13 +00001046 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001047 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001048 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001049 *draw.fClip, draw.fBounder, &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001050 }
reed@google.com69302852011-02-16 18:08:07 +00001051 return;
1052 }
reed@google.com69302852011-02-16 18:08:07 +00001053
bsalomon@google.com47059542012-06-06 20:51:20 +00001054 GrPathFill fill = kHairLine_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001055
reed@google.com0c219b62011-02-16 21:31:18 +00001056 if (doFill) {
1057 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001058 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001059 fill = kWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001060 break;
1061 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001062 fill = kEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001063 break;
1064 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001065 fill = kInverseWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001066 break;
1067 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001068 fill = kInverseEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001069 break;
1070 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001071 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001072 return;
1073 }
1074 }
1075
reed@google.com07f3ee12011-05-16 17:21:57 +00001076 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001077}
1078
bsalomon@google.comfb309512011-11-30 14:13:48 +00001079namespace {
1080
1081inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1082 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1083 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1084 return tilesX * tilesY;
1085}
1086
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001087inline int determine_tile_size(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001088 const SkRect& src,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001089 int maxTextureSize) {
1090 static const int kSmallTileSize = 1 << 10;
1091 if (maxTextureSize <= kSmallTileSize) {
1092 return maxTextureSize;
1093 }
1094
1095 size_t maxTexTotalTileSize;
1096 size_t smallTotalTileSize;
1097
robertphillips@google.combac6b052012-09-28 18:06:49 +00001098 SkIRect iSrc;
1099 src.roundOut(&iSrc);
1100
1101 maxTexTotalTileSize = get_tile_count(iSrc.fLeft,
1102 iSrc.fTop,
1103 iSrc.fRight,
1104 iSrc.fBottom,
1105 maxTextureSize);
1106 smallTotalTileSize = get_tile_count(iSrc.fLeft,
1107 iSrc.fTop,
1108 iSrc.fRight,
1109 iSrc.fBottom,
1110 kSmallTileSize);
1111
bsalomon@google.comfb309512011-11-30 14:13:48 +00001112 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1113 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1114
1115 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1116 return kSmallTileSize;
1117 } else {
1118 return maxTextureSize;
1119 }
1120}
1121}
1122
1123bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001124 const GrTextureParams& params,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001125 const SkRect* srcRectPtr) const {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001126 // if bitmap is explictly texture backed then just use the texture
1127 if (NULL != bitmap.getTexture()) {
1128 return false;
1129 }
1130 // if it's larger than the max texture size, then we have no choice but
1131 // tiling
1132 const int maxTextureSize = fContext->getMaxTextureSize();
1133 if (bitmap.width() > maxTextureSize ||
1134 bitmap.height() > maxTextureSize) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001135 return true;
1136 }
1137 // if we are going to have to draw the whole thing, then don't tile
1138 if (NULL == srcRectPtr) {
1139 return false;
1140 }
1141 // if the entire texture is already in our cache then no reason to tile it
bsalomon@google.comb8670992012-07-25 21:27:09 +00001142 if (this->isBitmapInTextureCache(bitmap, params)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001143 return false;
1144 }
1145
1146 // At this point we know we could do the draw by uploading the entire bitmap
1147 // as a texture. However, if the texture would be large compared to the
1148 // cache size and we don't require most of it for this draw then tile to
1149 // reduce the amount of upload and cache spill.
1150
1151 // assumption here is that sw bitmap size is a good proxy for its size as
1152 // a texture
1153 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001154 size_t cacheSize;
1155 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001156 if (bmpSize < cacheSize / 2) {
1157 return false;
1158 }
1159
robertphillips@google.combac6b052012-09-28 18:06:49 +00001160 SkScalar fracUsed = SkScalarMul(srcRectPtr->width() / bitmap.width(),
1161 srcRectPtr->height() / bitmap.height());
1162 if (fracUsed <= SK_ScalarHalf) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001163 return true;
1164 } else {
1165 return false;
1166 }
1167}
1168
reed@google.comac10a2d2010-12-22 21:39:39 +00001169void SkGpuDevice::drawBitmap(const SkDraw& draw,
1170 const SkBitmap& bitmap,
1171 const SkIRect* srcRectPtr,
1172 const SkMatrix& m,
1173 const SkPaint& paint) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001174
1175 SkRect tmp;
1176 SkRect* tmpPtr = NULL;
1177
1178 // convert from SkIRect to SkRect
1179 if (NULL != srcRectPtr) {
1180 tmp.set(*srcRectPtr);
1181 tmpPtr = &tmp;
1182 }
1183
1184 // We cannot call drawBitmapRect here since 'm' could be anything
1185 this->drawBitmapCommon(draw, bitmap, tmpPtr, m, paint);
1186}
1187
1188void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1189 const SkBitmap& bitmap,
1190 const SkRect* srcRectPtr,
1191 const SkMatrix& m,
1192 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001193 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001194
robertphillips@google.combac6b052012-09-28 18:06:49 +00001195 SkRect srcRect;
reed@google.comac10a2d2010-12-22 21:39:39 +00001196 if (NULL == srcRectPtr) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001197 srcRect.set(0, 0, SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001198 } else {
1199 srcRect = *srcRectPtr;
1200 }
1201
junov@google.comd935cfb2011-06-27 20:48:23 +00001202 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001203 // Convert the bitmap to a shader so that the rect can be drawn
1204 // through drawRect, which supports mask filters.
robertphillips@google.combac6b052012-09-28 18:06:49 +00001205 SkMatrix newM(m);
junov@google.com1d329782011-07-28 20:10:09 +00001206 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001207 const SkBitmap* bitmapPtr = &bitmap;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001208 if (NULL != srcRectPtr) {
1209 SkIRect iSrc;
1210 srcRect.roundOut(&iSrc);
1211 if (!bitmap.extractSubset(&tmp, iSrc)) {
epoger@google.com9ef2d832011-07-01 21:12:20 +00001212 return; // extraction failed
1213 }
1214 bitmapPtr = &tmp;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001215 srcRect.offset(SkIntToScalar(-iSrc.fLeft), SkIntToScalar(-iSrc.fTop));
1216 // The source rect has changed so update the matrix
1217 newM.preTranslate(SkIntToScalar(iSrc.fLeft), SkIntToScalar(iSrc.fTop));
junov@google.comd935cfb2011-06-27 20:48:23 +00001218 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001219
junov@google.comd935cfb2011-06-27 20:48:23 +00001220 SkPaint paintWithTexture(paint);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001221 paintWithTexture.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
junov@google.comd935cfb2011-06-27 20:48:23 +00001222 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001223
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001224 // Transform 'newM' needs to be concatenated to the current matrix,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001225 // rather than transforming the primitive directly, so that 'newM' will
junov@google.com1d329782011-07-28 20:10:09 +00001226 // also affect the behavior of the mask filter.
1227 SkMatrix drawMatrix;
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001228 drawMatrix.setConcat(fContext->getMatrix(), newM);
junov@google.com1d329782011-07-28 20:10:09 +00001229 SkDraw transformedDraw(draw);
1230 transformedDraw.fMatrix = &drawMatrix;
1231
robertphillips@google.combac6b052012-09-28 18:06:49 +00001232 this->drawRect(transformedDraw, srcRect, paintWithTexture);
junov@google.com1d329782011-07-28 20:10:09 +00001233
junov@google.comd935cfb2011-06-27 20:48:23 +00001234 return;
1235 }
1236
bsalomon@google.com5782d712011-01-21 21:03:59 +00001237 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001238 SkAutoCachedTexture colorLutTexture;
1239 if (!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001240 return;
1241 }
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001242 GrTextureParams params;
1243 params.setBilerp(paint.isFilterBitmap());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001244
robertphillips@google.combac6b052012-09-28 18:06:49 +00001245 if (!this->shouldTileBitmap(bitmap, params, srcRectPtr)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001246 // take the simple case
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001247 this->internalDrawBitmap(bitmap, srcRect, m, params, &grPaint);
robertphillips@google.combac6b052012-09-28 18:06:49 +00001248 } else {
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001249 this->drawTiledBitmap(bitmap, srcRect, m, params, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001250 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001251}
1252
1253// Break 'bitmap' into several tiles to draw it since it has already
1254// been determined to be too large to fit in VRAM
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001255void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001256 const SkRect& srcRect,
1257 const SkMatrix& m,
1258 const GrTextureParams& params,
1259 GrPaint* grPaint) {
1260 const int maxTextureSize = fContext->getMaxTextureSize();
1261
1262 int tileSize = determine_tile_size(bitmap, srcRect, maxTextureSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001263
reed@google.comac10a2d2010-12-22 21:39:39 +00001264 // compute clip bounds in local coordinates
robertphillips@google.combac6b052012-09-28 18:06:49 +00001265 SkRect clipRect;
reed@google.comac10a2d2010-12-22 21:39:39 +00001266 {
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001267 const GrRenderTarget* rt = fContext->getRenderTarget();
1268 clipRect.setWH(SkIntToScalar(rt->width()), SkIntToScalar(rt->height()));
1269 if (!fContext->getClip()->fClipStack->intersectRectWithClip(&clipRect)) {
1270 return;
1271 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001272 SkMatrix matrix, inverse;
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001273 matrix.setConcat(fContext->getMatrix(), m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001274 if (!matrix.invert(&inverse)) {
1275 return;
1276 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001277 inverse.mapRect(&clipRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001278 }
1279
bsalomon@google.comfb309512011-11-30 14:13:48 +00001280 int nx = bitmap.width() / tileSize;
1281 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001282 for (int x = 0; x <= nx; x++) {
1283 for (int y = 0; y <= ny; y++) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001284 SkRect tileR;
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001285 tileR.set(SkIntToScalar(x * tileSize),
robertphillips@google.combac6b052012-09-28 18:06:49 +00001286 SkIntToScalar(y * tileSize),
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001287 SkIntToScalar((x + 1) * tileSize),
robertphillips@google.combac6b052012-09-28 18:06:49 +00001288 SkIntToScalar((y + 1) * tileSize));
1289
1290 if (!SkRect::Intersects(tileR, clipRect)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001291 continue;
1292 }
1293
robertphillips@google.combac6b052012-09-28 18:06:49 +00001294 if (!tileR.intersect(srcRect)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001295 continue;
1296 }
1297
1298 SkBitmap tmpB;
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001299 SkIRect iTileR;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001300 tileR.roundOut(&iTileR);
1301 if (bitmap.extractSubset(&tmpB, iTileR)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001302 // now offset it to make it "local" to our tmp bitmap
robertphillips@google.combac6b052012-09-28 18:06:49 +00001303 tileR.offset(SkIntToScalar(-iTileR.fLeft), SkIntToScalar(-iTileR.fTop));
reed@google.comac10a2d2010-12-22 21:39:39 +00001304 SkMatrix tmpM(m);
skia.committer@gmail.comdc3a4e52012-10-02 02:01:24 +00001305 tmpM.preTranslate(SkIntToScalar(iTileR.fLeft),
robertphillips@google.comffad46b2012-10-01 14:32:51 +00001306 SkIntToScalar(iTileR.fTop));
1307
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001308 this->internalDrawBitmap(tmpB, tileR, tmpM, params, grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001309 }
1310 }
1311 }
1312}
1313
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001314namespace {
1315
1316bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1317 // detect pixel disalignment
1318 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1319 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
rmistry@google.comd6176b02012-08-23 18:14:13 +00001320 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001321 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1322 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1323 COLOR_BLEED_TOLERANCE &&
1324 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1325 COLOR_BLEED_TOLERANCE) {
1326 return true;
1327 }
1328 return false;
1329}
1330
1331bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1332 const SkMatrix& m) {
1333 // Only gets called if hasAlignedSamples returned false.
1334 // So we can assume that sampling is axis aligned but not texel aligned.
1335 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001336 SkRect innerSrcRect(srcRect), innerTransformedRect,
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001337 outerTransformedRect(transformedRect);
1338 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1339 m.mapRect(&innerTransformedRect, innerSrcRect);
1340
1341 // The gap between outerTransformedRect and innerTransformedRect
1342 // represents the projection of the source border area, which is
1343 // problematic for color bleeding. We must check whether any
1344 // destination pixels sample the border area.
1345 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1346 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1347 SkIRect outer, inner;
1348 outerTransformedRect.round(&outer);
1349 innerTransformedRect.round(&inner);
1350 // If the inner and outer rects round to the same result, it means the
1351 // border does not overlap any pixel centers. Yay!
1352 return inner != outer;
1353}
1354
1355} // unnamed namespace
1356
reed@google.comac10a2d2010-12-22 21:39:39 +00001357/*
1358 * This is called by drawBitmap(), which has to handle images that may be too
1359 * large to be represented by a single texture.
1360 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001361 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1362 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001363 */
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001364void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001365 const SkRect& srcRect,
reed@google.comac10a2d2010-12-22 21:39:39 +00001366 const SkMatrix& m,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001367 const GrTextureParams& params,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001368 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001369 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1370 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001371
reed@google.com9c49bc32011-07-07 13:42:37 +00001372 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001373 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001374 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001375 return;
1376 }
1377
bsalomon@google.com08283af2012-10-26 13:01:20 +00001378 GrEffectStage* stage = grPaint->colorStage(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001379
reed@google.comac10a2d2010-12-22 21:39:39 +00001380 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001381 SkAutoCachedTexture act(this, bitmap, &params, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001382 if (NULL == texture) {
1383 return;
1384 }
1385
robertphillips@google.combac6b052012-09-28 18:06:49 +00001386 GrRect dstRect(srcRect);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001387 GrRect paintRect;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001388 SkScalar wInv = SkScalarInvert(SkIntToScalar(bitmap.width()));
1389 SkScalar hInv = SkScalarInvert(SkIntToScalar(bitmap.height()));
1390 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1391 SkScalarMul(srcRect.fTop, hInv),
1392 SkScalarMul(srcRect.fRight, wInv),
1393 SkScalarMul(srcRect.fBottom, hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001394
rmistry@google.comd6176b02012-08-23 18:14:13 +00001395 bool needsTextureDomain = false;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001396 if (params.isBilerp()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001397 // Need texture domain if drawing a sub rect.
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001398 needsTextureDomain = srcRect.width() < bitmap.width() ||
robertphillips@google.combac6b052012-09-28 18:06:49 +00001399 srcRect.height() < bitmap.height();
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001400 if (m.rectStaysRect() && fContext->getMatrix().rectStaysRect()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001401 // sampling is axis-aligned
robertphillips@google.combac6b052012-09-28 18:06:49 +00001402 GrRect transformedRect;
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001403 SkMatrix srcToDeviceMatrix(m);
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001404 srcToDeviceMatrix.postConcat(fContext->getMatrix());
robertphillips@google.combac6b052012-09-28 18:06:49 +00001405 srcToDeviceMatrix.mapRect(&transformedRect, srcRect);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001406
robertphillips@google.combac6b052012-09-28 18:06:49 +00001407 if (hasAlignedSamples(srcRect, transformedRect)) {
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001408 // We could also turn off filtering here (but we already did a cache lookup with
1409 // params).
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001410 needsTextureDomain = false;
1411 } else {
1412 needsTextureDomain = needsTextureDomain &&
robertphillips@google.combac6b052012-09-28 18:06:49 +00001413 mayColorBleed(srcRect, transformedRect, m);
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001414 }
1415 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001416 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001417
1418 GrRect textureDomain = GrRect::MakeEmpty();
bsalomon@google.com021fc732012-10-25 12:47:42 +00001419 SkAutoTUnref<GrEffect> effect;
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001420 if (needsTextureDomain) {
1421 // Use a constrained texture domain to avoid color bleeding
bsalomon@google.com81712882012-11-01 17:12:34 +00001422 SkScalar left, top, right, bottom;
1423 if (srcRect.width() > SK_Scalar1) {
1424 SkScalar border = SK_ScalarHalf / bitmap.width();
junov@google.com6acc9b32011-05-16 18:32:07 +00001425 left = paintRect.left() + border;
1426 right = paintRect.right() - border;
1427 } else {
bsalomon@google.com81712882012-11-01 17:12:34 +00001428 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
junov@google.com6acc9b32011-05-16 18:32:07 +00001429 }
bsalomon@google.com81712882012-11-01 17:12:34 +00001430 if (srcRect.height() > SK_Scalar1) {
1431 SkScalar border = SK_ScalarHalf / bitmap.height();
junov@google.com6acc9b32011-05-16 18:32:07 +00001432 top = paintRect.top() + border;
1433 bottom = paintRect.bottom() - border;
1434 } else {
bsalomon@google.com81712882012-11-01 17:12:34 +00001435 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
junov@google.com6acc9b32011-05-16 18:32:07 +00001436 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001437 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com021fc732012-10-25 12:47:42 +00001438 effect.reset(SkNEW_ARGS(GrTextureDomainEffect, (texture, textureDomain, params)));
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001439 } else {
bsalomon@google.com021fc732012-10-25 12:47:42 +00001440 effect.reset(SkNEW_ARGS(GrSingleTextureEffect, (texture, params)));
junov@google.com6acc9b32011-05-16 18:32:07 +00001441 }
bsalomon@google.com08283af2012-10-26 13:01:20 +00001442 grPaint->colorStage(kBitmapTextureIdx)->setEffect(effect);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001443 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001444}
1445
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001446namespace {
1447
bsalomon@google.comf271cc72012-10-24 19:35:13 +00001448void apply_effect(GrContext* context,
1449 GrTexture* srcTexture,
1450 GrTexture* dstTexture,
1451 const GrRect& rect,
1452 GrEffect* effect) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001453 SkASSERT(srcTexture && srcTexture->getContext() == context);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001454 GrContext::AutoMatrix am;
1455 am.setIdentity(context);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001456 GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget());
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001457 GrContext::AutoClip acs(context, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001458
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001459 GrPaint paint;
bsalomon@google.comdbe49f72012-11-05 16:36:02 +00001460 paint.colorStage(0)->setEffect(effect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001461 context->drawRect(paint, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001462}
1463
1464};
1465
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001466static GrTexture* filter_texture(SkDevice* device, GrContext* context,
1467 GrTexture* texture, SkImageFilter* filter,
1468 const GrRect& rect) {
reed@google.com8926b162012-03-23 15:36:36 +00001469 GrAssert(filter);
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001470 SkDeviceImageFilterProxy proxy(device);
reed@google.com8926b162012-03-23 15:36:36 +00001471
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001472 GrTextureDesc desc;
1473 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1474 desc.fWidth = SkScalarCeilToInt(rect.width());
1475 desc.fHeight = SkScalarCeilToInt(rect.height());
bsalomon@google.com0342a852012-08-20 19:22:38 +00001476 desc.fConfig = kRGBA_8888_GrPixelConfig;
bsalomon@google.com021fc732012-10-25 12:47:42 +00001477 GrEffect* effect;
reed@google.com8926b162012-03-23 15:36:36 +00001478
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001479 if (filter->canFilterImageGPU()) {
senorblanco@chromium.org985fa792012-10-24 15:14:26 +00001480 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1481 // filter. Also set the clip wide open and the matrix to identity.
1482 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001483 texture = filter->onFilterImageGPU(&proxy, texture, rect);
bsalomon@google.com021fc732012-10-25 12:47:42 +00001484 } else if (filter->asNewEffect(&effect, texture)) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001485 GrAutoScratchTexture dst(context, desc);
bsalomon@google.com021fc732012-10-25 12:47:42 +00001486 apply_effect(context, texture, dst.texture(), rect, effect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001487 texture = dst.detach();
bsalomon@google.com021fc732012-10-25 12:47:42 +00001488 effect->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001489 }
1490 return texture;
1491}
1492
reed@google.comac10a2d2010-12-22 21:39:39 +00001493void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001494 int left, int top, const SkPaint& paint) {
1495 // drawSprite is defined to be in device coords.
1496 CHECK_SHOULD_DRAW(draw, true);
reed@google.comac10a2d2010-12-22 21:39:39 +00001497
reed@google.com8926b162012-03-23 15:36:36 +00001498 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001499 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1500 return;
1501 }
1502
reed@google.com76dd2772012-01-05 21:15:07 +00001503 int w = bitmap.width();
1504 int h = bitmap.height();
1505
bsalomon@google.com5782d712011-01-21 21:03:59 +00001506 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001507 SkAutoCachedTexture colorLutTexture;
1508 if(!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001509 return;
1510 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001511
bsalomon@google.com08283af2012-10-26 13:01:20 +00001512 GrEffectStage* stage = grPaint.colorStage(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001513
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001514 GrTexture* texture;
bsalomon@google.com08283af2012-10-26 13:01:20 +00001515 stage->reset();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001516 // draw sprite uses the default texture params
1517 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
bsalomon@google.com08283af2012-10-26 13:01:20 +00001518 grPaint.colorStage(kBitmapTextureIdx)->setEffect(SkNEW_ARGS
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001519 (GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001520
reed@google.com8926b162012-03-23 15:36:36 +00001521 SkImageFilter* filter = paint.getImageFilter();
1522 if (NULL != filter) {
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001523 GrTexture* filteredTexture = filter_texture(this, fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001524 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001525 if (filteredTexture) {
bsalomon@google.com08283af2012-10-26 13:01:20 +00001526 grPaint.colorStage(kBitmapTextureIdx)->setEffect(SkNEW_ARGS
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001527 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001528 texture = filteredTexture;
1529 filteredTexture->unref();
1530 }
reed@google.com76dd2772012-01-05 21:15:07 +00001531 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001532
bsalomon@google.com5782d712011-01-21 21:03:59 +00001533 fContext->drawRectToRect(grPaint,
bsalomon@google.com81712882012-11-01 17:12:34 +00001534 GrRect::MakeXYWH(SkIntToScalar(left),
1535 SkIntToScalar(top),
1536 SkIntToScalar(w),
1537 SkIntToScalar(h)),
1538 GrRect::MakeWH(SK_Scalar1 * w / texture->width(),
1539 SK_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001540}
1541
reed@google.com33535f32012-09-25 15:37:50 +00001542void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1543 const SkRect* src, const SkRect& dst,
1544 const SkPaint& paint) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001545 SkMatrix matrix;
1546 SkRect bitmapBounds, tmpSrc;
1547
1548 bitmapBounds.set(0, 0,
1549 SkIntToScalar(bitmap.width()),
1550 SkIntToScalar(bitmap.height()));
1551
reed@google.com33535f32012-09-25 15:37:50 +00001552 // Compute matrix from the two rectangles
robertphillips@google.combac6b052012-09-28 18:06:49 +00001553 if (NULL != src) {
1554 tmpSrc = *src;
1555 } else {
1556 tmpSrc = bitmapBounds;
1557 }
1558 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1559
1560 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1561 if (NULL != src) {
1562 if (!bitmapBounds.contains(tmpSrc)) {
1563 if (!tmpSrc.intersect(bitmapBounds)) {
1564 return; // nothing to draw
reed@google.com33535f32012-09-25 15:37:50 +00001565 }
reed@google.com33535f32012-09-25 15:37:50 +00001566 }
reed@google.com33535f32012-09-25 15:37:50 +00001567 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +00001568
robertphillips@google.combac6b052012-09-28 18:06:49 +00001569 this->drawBitmapCommon(draw, bitmap, &tmpSrc, matrix, paint);
reed@google.com33535f32012-09-25 15:37:50 +00001570}
1571
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001572void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001573 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001574 // clear of the source device must occur before CHECK_SHOULD_DRAW
1575 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1576 if (dev->fNeedClear) {
1577 // TODO: could check here whether we really need to draw at all
1578 dev->clear(0x0);
1579 }
1580
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001581 // drawDevice is defined to be in device coords.
1582 CHECK_SHOULD_DRAW(draw, true);
reed@google.comac10a2d2010-12-22 21:39:39 +00001583
bsalomon@google.com5782d712011-01-21 21:03:59 +00001584 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001585 SkAutoCachedTexture colorLutTexture;
bsalomon@google.com08283af2012-10-26 13:01:20 +00001586 grPaint.colorStage(kBitmapTextureIdx)->reset();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001587 if (!dev->bindDeviceAsTexture(&grPaint) ||
twiz@google.com58071162012-07-18 21:41:50 +00001588 !skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001589 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001590 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001591
bsalomon@google.com08283af2012-10-26 13:01:20 +00001592 GrTexture* devTex = grPaint.getColorStage(kBitmapTextureIdx).getEffect()->texture(0);
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001593 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001594
reed@google.com8926b162012-03-23 15:36:36 +00001595 SkImageFilter* filter = paint.getImageFilter();
1596 if (NULL != filter) {
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001597 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001598 SkIntToScalar(devTex->height()));
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001599 GrTexture* filteredTexture = filter_texture(this, fContext, devTex, filter, rect);
reed@google.com8926b162012-03-23 15:36:36 +00001600 if (filteredTexture) {
bsalomon@google.com08283af2012-10-26 13:01:20 +00001601 grPaint.colorStage(kBitmapTextureIdx)->setEffect(SkNEW_ARGS
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001602 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001603 devTex = filteredTexture;
1604 filteredTexture->unref();
1605 }
1606 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001607
bsalomon@google.com5782d712011-01-21 21:03:59 +00001608 const SkBitmap& bm = dev->accessBitmap(false);
1609 int w = bm.width();
1610 int h = bm.height();
1611
bsalomon@google.com81712882012-11-01 17:12:34 +00001612 GrRect dstRect = GrRect::MakeXYWH(SkIntToScalar(x),
1613 SkIntToScalar(y),
1614 SkIntToScalar(w),
1615 SkIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001616
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001617 // The device being drawn may not fill up its texture (saveLayer uses
1618 // the approximate ).
bsalomon@google.com81712882012-11-01 17:12:34 +00001619 GrRect srcRect = GrRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1620 SK_Scalar1 * h / devTex->height());
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001621
1622 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001623}
1624
reed@google.com8926b162012-03-23 15:36:36 +00001625bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
bsalomon@google.com8ea78d82012-10-24 20:11:30 +00001626 if (!filter->asNewEffect(NULL, NULL) &&
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001627 !filter->canFilterImageGPU()) {
reed@google.com76dd2772012-01-05 21:15:07 +00001628 return false;
1629 }
reed@google.com8926b162012-03-23 15:36:36 +00001630 return true;
1631}
1632
1633bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1634 const SkMatrix& ctm,
1635 SkBitmap* result, SkIPoint* offset) {
1636 // want explicitly our impl, so guard against a subclass of us overriding it
1637 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001638 return false;
1639 }
reed@google.com8926b162012-03-23 15:36:36 +00001640
1641 SkAutoLockPixels alp(src, !src.getTexture());
1642 if (!src.getTexture() && !src.readyToDraw()) {
1643 return false;
1644 }
1645
1646 GrPaint paint;
reed@google.com8926b162012-03-23 15:36:36 +00001647
reed@google.com8926b162012-03-23 15:36:36 +00001648 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001649 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1650 // must be pushed upstack.
1651 SkAutoCachedTexture act(this, src, NULL, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001652
1653 result->setConfig(src.config(), src.width(), src.height());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001654 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001655 SkIntToScalar(src.height()));
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001656 GrTexture* resultTexture = filter_texture(this, fContext, texture, filter, rect);
reed@google.com8926b162012-03-23 15:36:36 +00001657 if (resultTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001658 result->setPixelRef(SkNEW_ARGS(SkGrTexturePixelRef,
1659 (resultTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001660 resultTexture->unref();
1661 }
reed@google.com76dd2772012-01-05 21:15:07 +00001662 return true;
1663}
1664
reed@google.comac10a2d2010-12-22 21:39:39 +00001665///////////////////////////////////////////////////////////////////////////////
1666
1667// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001668static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001669 kTriangles_GrPrimitiveType,
1670 kTriangleStrip_GrPrimitiveType,
1671 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001672};
1673
1674void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1675 int vertexCount, const SkPoint vertices[],
1676 const SkPoint texs[], const SkColor colors[],
1677 SkXfermode* xmode,
1678 const uint16_t indices[], int indexCount,
1679 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001680 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001681
bsalomon@google.com5782d712011-01-21 21:03:59 +00001682 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +00001683 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com5782d712011-01-21 21:03:59 +00001684 // we ignore the shader if texs is null.
1685 if (NULL == texs) {
twiz@google.com58071162012-07-18 21:41:50 +00001686 if (!skPaint2GrPaintNoShader(this,
1687 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001688 false,
1689 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001690 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +00001691 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001692 return;
1693 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001694 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001695 if (!skPaint2GrPaintShader(this,
1696 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001697 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001698 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001699 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001700 return;
1701 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001702 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001703
1704 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001705 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001706 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1707#if 0
1708 return
1709#endif
1710 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001711 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001712
bsalomon@google.com498776a2011-08-16 19:20:44 +00001713 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1714 if (NULL != colors) {
1715 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001716 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001717 for (int i = 0; i < vertexCount; ++i) {
rileya@google.com24f3ad12012-07-18 21:47:40 +00001718 convertedColors[i] = SkColor2GrColor(colors[i]);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001719 }
1720 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001721 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001722 fContext->drawVertices(grPaint,
1723 gVertexMode2PrimitiveType[vmode],
1724 vertexCount,
1725 (GrPoint*) vertices,
1726 (GrPoint*) texs,
1727 colors,
1728 indices,
1729 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001730}
1731
1732///////////////////////////////////////////////////////////////////////////////
1733
1734static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001735 GrFontScaler* scaler = (GrFontScaler*)data;
1736 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001737}
1738
1739static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1740 void* auxData;
1741 GrFontScaler* scaler = NULL;
1742 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1743 scaler = (GrFontScaler*)auxData;
1744 }
1745 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001746 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001747 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1748 }
1749 return scaler;
1750}
1751
1752static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1753 SkFixed fx, SkFixed fy,
1754 const SkGlyph& glyph) {
1755 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1756
bungeman@google.com15865a72012-01-11 16:28:04 +00001757 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001758
1759 if (NULL == procs->fFontScaler) {
1760 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1761 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001762
bungeman@google.com15865a72012-01-11 16:28:04 +00001763 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1764 glyph.getSubXFixed(),
1765 glyph.getSubYFixed()),
1766 SkFixedFloorToFixed(fx),
1767 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001768 procs->fFontScaler);
1769}
1770
bsalomon@google.com5782d712011-01-21 21:03:59 +00001771SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001772
1773 // deferred allocation
1774 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001775 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001776 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1777 fDrawProcs->fContext = fContext;
1778 }
1779
1780 // init our (and GL's) state
1781 fDrawProcs->fTextContext = context;
1782 fDrawProcs->fFontScaler = NULL;
1783 return fDrawProcs;
1784}
1785
1786void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1787 size_t byteLength, SkScalar x, SkScalar y,
1788 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001789 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001790
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001791 if (fContext->getMatrix().hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001792 // this guy will just call our drawPath()
1793 draw.drawText((const char*)text, byteLength, x, y, paint);
1794 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001795 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001796
1797 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +00001798 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001799 if (!skPaint2GrPaintShader(this,
1800 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001801 true,
twiz@google.com58071162012-07-18 21:41:50 +00001802 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001803 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001804 return;
1805 }
bsalomon@google.com0e354aa2012-10-08 20:44:25 +00001806 GrTextContext context(fContext, grPaint);
tomhudson@google.com375ff852012-06-29 18:37:57 +00001807 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001808 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1809 }
1810}
1811
1812void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1813 size_t byteLength, const SkScalar pos[],
1814 SkScalar constY, int scalarsPerPos,
1815 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001816 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001817
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001818 if (fContext->getMatrix().hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001819 // this guy will just call our drawPath()
1820 draw.drawPosText((const char*)text, byteLength, pos, constY,
1821 scalarsPerPos, paint);
1822 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001823 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001824
1825 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +00001826 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001827 if (!skPaint2GrPaintShader(this,
1828 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001829 true,
twiz@google.com58071162012-07-18 21:41:50 +00001830 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001831 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001832 return;
1833 }
bsalomon@google.com0e354aa2012-10-08 20:44:25 +00001834 GrTextContext context(fContext, grPaint);
tomhudson@google.com375ff852012-06-29 18:37:57 +00001835 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001836 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1837 scalarsPerPos, paint);
1838 }
1839}
1840
1841void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1842 size_t len, const SkPath& path,
1843 const SkMatrix* m, const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001844 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001845
1846 SkASSERT(draw.fDevice == this);
1847 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1848}
1849
1850///////////////////////////////////////////////////////////////////////////////
1851
reed@google.comf67e4cf2011-03-15 20:56:58 +00001852bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1853 if (!paint.isLCDRenderText()) {
1854 // we're cool with the paint as is
1855 return false;
1856 }
1857
1858 if (paint.getShader() ||
1859 paint.getXfermode() || // unless its srcover
1860 paint.getMaskFilter() ||
1861 paint.getRasterizer() ||
1862 paint.getColorFilter() ||
1863 paint.getPathEffect() ||
1864 paint.isFakeBoldText() ||
1865 paint.getStyle() != SkPaint::kFill_Style) {
1866 // turn off lcd
1867 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1868 flags->fHinting = paint.getHinting();
1869 return true;
1870 }
1871 // we're cool with the paint as is
1872 return false;
1873}
1874
reed@google.com75d939b2011-12-07 15:07:23 +00001875void SkGpuDevice::flush() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +00001876 DO_DEFERRED_CLEAR();
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001877 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001878}
1879
reed@google.comf67e4cf2011-03-15 20:56:58 +00001880///////////////////////////////////////////////////////////////////////////////
1881
bsalomon@google.comfb309512011-11-30 14:13:48 +00001882bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001883 const GrTextureParams& params) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001884 uint64_t key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001885 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001886
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001887 GrTextureDesc desc;
1888 desc.fWidth = bitmap.width();
1889 desc.fHeight = bitmap.height();
rileya@google.com24f3ad12012-07-18 21:47:40 +00001890 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config());
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001891
robertphillips@google.com9c2ea842012-08-13 17:47:59 +00001892 GrCacheData cacheData(key);
1893
1894 return this->context()->isTextureInCache(desc, cacheData, &params);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001895}
1896
1897
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001898SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1899 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001900 bool isOpaque,
1901 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001902 GrTextureDesc desc;
1903 desc.fConfig = fRenderTarget->config();
1904 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1905 desc.fWidth = width;
1906 desc.fHeight = height;
1907 desc.fSampleCnt = fRenderTarget->numSamples();
1908
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001909 GrTexture* texture;
1910 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001911 // Skia's convention is to only clear a device if it is non-opaque.
1912 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001913
1914#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1915 // layers are never draw in repeat modes, so we can request an approx
1916 // match and ignore any padding.
1917 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1918 GrContext::kApprox_ScratchTexMatch :
1919 GrContext::kExact_ScratchTexMatch;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001920 texture = fContext->lockScratchTexture(desc, matchType);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001921#else
1922 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1923 texture = tunref.get();
1924#endif
1925 if (texture) {
1926 return SkNEW_ARGS(SkGpuDevice,(fContext,
1927 texture,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001928 needClear));
1929 } else {
1930 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1931 width, height);
1932 return NULL;
1933 }
1934}
1935
1936SkGpuDevice::SkGpuDevice(GrContext* context,
1937 GrTexture* texture,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001938 bool needClear)
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001939 : SkDevice(make_bitmap(context, texture->asRenderTarget())) {
1940
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001941 GrAssert(texture && texture->asRenderTarget());
bsalomon@google.com8090e652012-08-28 15:07:11 +00001942 // This constructor is called from onCreateCompatibleDevice. It has locked the RT in the texture
1943 // cache. We pass true for the third argument so that it will get unlocked.
1944 this->initFromRenderTarget(context, texture->asRenderTarget(), true);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001945 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001946}