blob: 5d04995d0755811e9986c08fbe5ec8c84e86eacd [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
bsalomon@google.com85003222012-03-28 14:44:37 +0000744// We prefer to blur small rect with small radius via CPU.
745#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
746#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
747inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
748 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
749 rect.height() <= MIN_GPU_BLUR_SIZE &&
750 radius <= MIN_GPU_BLUR_RADIUS) {
751 return true;
752 }
753 return false;
754}
755
sugoi@google.com12b4e272012-12-06 20:13:11 +0000756bool drawWithGPUMaskFilter(GrContext* context, const SkPath& devPath, bool doHairLine,
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000757 SkMaskFilter* filter, const SkRegion& clip,
sugoi@google.com12b4e272012-12-06 20:13:11 +0000758 SkBounder* bounder, GrPaint* grp) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000759 SkMaskFilter::BlurInfo info;
760 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000761 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000762 return false;
763 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000764 SkScalar radius = info.fIgnoreTransform ? info.fRadius
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000765 : context->getMatrix().mapRadius(info.fRadius);
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000766 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000767 if (radius <= 0) {
768 return false;
769 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000770
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000771 SkRect srcRect = devPath.getBounds();
bsalomon@google.com85003222012-03-28 14:44:37 +0000772 if (shouldDrawBlurWithCPU(srcRect, radius)) {
773 return false;
774 }
775
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000776 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000777 float sigma3 = sigma * 3.0f;
778
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000779 SkRect clipRect;
780 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000781
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000782 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000783 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
784 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000785 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000786 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000787 SkIRect finalIRect;
788 finalRect.roundOut(&finalIRect);
789 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000790 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000791 }
792 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000793 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000794 }
795 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000796 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000797 GrTextureDesc desc;
798 desc.fFlags = kRenderTarget_GrTextureFlagBit;
799 desc.fWidth = SkScalarCeilToInt(srcRect.width());
800 desc.fHeight = SkScalarCeilToInt(srcRect.height());
801 // We actually only need A8, but it often isn't supported as a
802 // render target so default to RGBA_8888
bsalomon@google.com0342a852012-08-20 19:22:38 +0000803 desc.fConfig = kRGBA_8888_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000804
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000805 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
806 desc.fConfig = kAlpha_8_GrPixelConfig;
807 }
808
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000809 GrAutoScratchTexture pathEntry(context, desc);
810 GrTexture* pathTexture = pathEntry.texture();
811 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000812 return false;
813 }
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000814
robertphillips@google.comccb39502012-10-01 18:25:13 +0000815 SkAutoTUnref<GrTexture> blurTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000816
robertphillips@google.comccb39502012-10-01 18:25:13 +0000817 {
818 GrContext::AutoRenderTarget art(context, pathTexture->asRenderTarget());
819 GrContext::AutoClip ac(context, srcRect);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000820
robertphillips@google.comccb39502012-10-01 18:25:13 +0000821 context->clear(NULL, 0);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000822
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000823 GrPaint tempPaint;
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000824 if (grp->isAntiAlias()) {
825 tempPaint.setAntiAlias(true);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000826 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
827 // blend coeff of zero requires dual source blending support in order
828 // to properly blend partially covered pixels. This means the AA
829 // code path may not be taken. So we use a dst blend coeff of ISA. We
830 // could special case AA draws to a dst surface with known alpha=0 to
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000831 // use a zero dst coeff when dual source blending isn't available.f
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000832 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000833 }
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000834
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000835 GrContext::AutoMatrix am;
836
837 // Draw hard shadow to pathTexture with path top-left at origin using tempPaint.
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000838 SkMatrix translate;
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000839 translate.setTranslate(offset.fX, offset.fY);
840 am.set(context, translate);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000841 context->drawPath(tempPaint, devPath, doHairLine);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000842
843 // If we're doing a normal blur, we can clobber the pathTexture in the
844 // gaussianBlur. Otherwise, we need to save it for later compositing.
845 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
skia.committer@gmail.comdc3a4e52012-10-02 02:01:24 +0000846 blurTexture.reset(context->gaussianBlur(pathTexture, isNormalBlur,
robertphillips@google.comccb39502012-10-01 18:25:13 +0000847 srcRect, sigma, sigma));
robertphillips@google.com7d126752012-10-19 12:56:26 +0000848 if (NULL == blurTexture) {
849 return false;
850 }
robertphillips@google.comccb39502012-10-01 18:25:13 +0000851
852 if (!isNormalBlur) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000853 context->setIdentityMatrix();
robertphillips@google.comccb39502012-10-01 18:25:13 +0000854 GrPaint paint;
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000855 SkMatrix matrix;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000856 matrix.setIDiv(pathTexture->width(), pathTexture->height());
robertphillips@google.comccb39502012-10-01 18:25:13 +0000857 // Blend pathTexture over blurTexture.
858 context->setRenderTarget(blurTexture->asRenderTarget());
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000859 paint.colorStage(0)->setEffect(SkNEW_ARGS(GrSingleTextureEffect, (pathTexture, matrix)))->unref();
robertphillips@google.comccb39502012-10-01 18:25:13 +0000860 if (SkMaskFilter::kInner_BlurType == blurType) {
861 // inner: dst = dst * src
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000862 paint.setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000863 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
864 // solid: dst = src + dst - src * dst
865 // = (1 - dst) * src + 1 * dst
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000866 paint.setBlendFunc(kIDC_GrBlendCoeff, kOne_GrBlendCoeff);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000867 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
868 // outer: dst = dst * (1 - src)
869 // = 0 * src + (1 - src) * dst
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000870 paint.setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000871 }
872 context->drawRect(paint, srcRect);
873 }
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000874 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000875
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000876 GrContext::AutoMatrix am;
877 if (!am.setIdentity(context, grp)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000878 return false;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000879 }
skia.committer@gmail.comf57c01b2012-10-13 02:01:56 +0000880
bsalomon@google.com88becf42012-10-05 14:54:42 +0000881 static const int MASK_IDX = GrPaint::kMaxCoverageStages - 1;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000882 // we assume the last mask index is available for use
bsalomon@google.com88becf42012-10-05 14:54:42 +0000883 GrAssert(!grp->isCoverageStageEnabled(MASK_IDX));
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000884
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000885 SkMatrix matrix;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000886 matrix.setTranslate(-finalRect.fLeft, -finalRect.fTop);
887 matrix.postIDiv(blurTexture->width(), blurTexture->height());
888
bsalomon@google.com08283af2012-10-26 13:01:20 +0000889 grp->coverageStage(MASK_IDX)->reset();
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000890 grp->coverageStage(MASK_IDX)->setEffect(SkNEW_ARGS(GrSingleTextureEffect, (blurTexture, matrix)))->unref();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000891 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000892 return true;
893}
894
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000895bool drawWithMaskFilter(GrContext* context, const SkPath& devPath,
896 SkMaskFilter* filter, const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000897 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000898 SkMask srcM, dstM;
899
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000900 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
901 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
reed@google.com69302852011-02-16 18:08:07 +0000902 return false;
903 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000904 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000905
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000906 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
reed@google.com69302852011-02-16 18:08:07 +0000907 return false;
908 }
909 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000910 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000911
912 if (clip.quickReject(dstM.fBounds)) {
913 return false;
914 }
915 if (bounder && !bounder->doIRect(dstM.fBounds)) {
916 return false;
917 }
918
919 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000920 // the current clip (and identity matrix) and GrPaint settings
921 GrContext::AutoMatrix am;
922 am.setIdentity(context, grp);
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000923
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000924 GrTextureDesc desc;
925 desc.fWidth = dstM.fBounds.width();
926 desc.fHeight = dstM.fBounds.height();
927 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +0000928
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000929 GrAutoScratchTexture ast(context, desc);
930 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000931
reed@google.com69302852011-02-16 18:08:07 +0000932 if (NULL == texture) {
933 return false;
934 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000935 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000936 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000937
bsalomon@google.com88becf42012-10-05 14:54:42 +0000938 static const int MASK_IDX = GrPaint::kMaxCoverageStages - 1;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000939 // we assume the last mask index is available for use
bsalomon@google.com88becf42012-10-05 14:54:42 +0000940 GrAssert(!grp->isCoverageStageEnabled(MASK_IDX));
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000941
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000942 SkMatrix m;
bsalomon@google.comdfdb7e52012-10-16 15:19:45 +0000943 m.setTranslate(-dstM.fBounds.fLeft*SK_Scalar1, -dstM.fBounds.fTop*SK_Scalar1);
944 m.postIDiv(texture->width(), texture->height());
945
bsalomon@google.comd8b5fac2012-11-01 17:02:46 +0000946 grp->coverageStage(MASK_IDX)->setEffect(SkNEW_ARGS(GrSingleTextureEffect, (texture, m)))->unref();
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000947 GrRect d;
bsalomon@google.com81712882012-11-01 17:12:34 +0000948 d.setLTRB(SkIntToScalar(dstM.fBounds.fLeft),
949 SkIntToScalar(dstM.fBounds.fTop),
950 SkIntToScalar(dstM.fBounds.fRight),
951 SkIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000952
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000953 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +0000954 return true;
955}
reed@google.com69302852011-02-16 18:08:07 +0000956
bsalomon@google.com85003222012-03-28 14:44:37 +0000957}
958
959///////////////////////////////////////////////////////////////////////////////
960
reed@google.com0c219b62011-02-16 21:31:18 +0000961void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000962 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000963 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000964 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +0000965 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000966
sugoi@google.com12b4e272012-12-06 20:13:11 +0000967 bool doHairLine = false;
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000968
bsalomon@google.com5782d712011-01-21 21:03:59 +0000969 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +0000970 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000971 if (!skPaint2GrPaintShader(this,
972 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000973 true,
twiz@google.com58071162012-07-18 21:41:50 +0000974 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000975 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000976 return;
977 }
978
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000979 // can we cheat, and threat a thin stroke as a hairline w/ coverage
980 // if we can, we draw lots faster (raster device does this same test)
981 SkScalar hairlineCoverage;
bsalomon@google.com3ab43d52012-10-11 19:39:09 +0000982 if (SkDrawTreatAsHairline(paint, fContext->getMatrix(), &hairlineCoverage)) {
sugoi@google.com12b4e272012-12-06 20:13:11 +0000983 doHairLine = true;
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000984 grPaint.setCoverage(SkScalarRoundToInt(hairlineCoverage * grPaint.getCoverage()));
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +0000985 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000986
reed@google.comfe626382011-09-21 13:50:35 +0000987 // If we have a prematrix, apply it to the path, optimizing for the case
988 // where the original path can in fact be modified in place (even though
989 // its parameter type is const).
990 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
991 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +0000992
993 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +0000994 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +0000995
reed@google.come3445642011-02-16 23:20:39 +0000996 if (!pathIsMutable) {
997 result = &tmpPath;
998 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000999 }
reed@google.come3445642011-02-16 23:20:39 +00001000 // should I push prePathMatrix on our MV stack temporarily, instead
1001 // of applying it here? See SkDraw.cpp
1002 pathPtr->transform(*prePathMatrix, result);
1003 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001004 }
reed@google.com0c219b62011-02-16 21:31:18 +00001005 // at this point we're done with prePathMatrix
1006 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001007
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001008 if (paint.getPathEffect() ||
sugoi@google.com12b4e272012-12-06 20:13:11 +00001009 (!doHairLine && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001010 // it is safe to use tmpPath here, even if we already used it for the
1011 // prepathmatrix, since getFillPath can take the same object for its
1012 // input and output safely.
sugoi@google.com12b4e272012-12-06 20:13:11 +00001013 doHairLine = !paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001014 pathPtr = &tmpPath;
1015 }
1016
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001017 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001018 // avoid possibly allocating a new path in transform if we can
1019 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1020
1021 // transform the path into device space
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001022 pathPtr->transform(fContext->getMatrix(), devPathPtr);
sugoi@google.com12b4e272012-12-06 20:13:11 +00001023 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, doHairLine, paint.getMaskFilter(),
1024 *draw.fClip, draw.fBounder, &grPaint)) {
1025 SkPaint::Style style = doHairLine ? SkPaint::kStroke_Style :
1026 SkPaint::kFill_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001027 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001028 *draw.fClip, draw.fBounder, &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001029 }
reed@google.com69302852011-02-16 18:08:07 +00001030 return;
1031 }
reed@google.com69302852011-02-16 18:08:07 +00001032
sugoi@google.com12b4e272012-12-06 20:13:11 +00001033 fContext->drawPath(grPaint, *pathPtr, doHairLine);
reed@google.comac10a2d2010-12-22 21:39:39 +00001034}
1035
bsalomon@google.comfb309512011-11-30 14:13:48 +00001036namespace {
1037
1038inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1039 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1040 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1041 return tilesX * tilesY;
1042}
1043
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001044inline int determine_tile_size(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001045 const SkRect& src,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001046 int maxTextureSize) {
1047 static const int kSmallTileSize = 1 << 10;
1048 if (maxTextureSize <= kSmallTileSize) {
1049 return maxTextureSize;
1050 }
1051
1052 size_t maxTexTotalTileSize;
1053 size_t smallTotalTileSize;
1054
robertphillips@google.combac6b052012-09-28 18:06:49 +00001055 SkIRect iSrc;
1056 src.roundOut(&iSrc);
1057
1058 maxTexTotalTileSize = get_tile_count(iSrc.fLeft,
1059 iSrc.fTop,
1060 iSrc.fRight,
1061 iSrc.fBottom,
1062 maxTextureSize);
1063 smallTotalTileSize = get_tile_count(iSrc.fLeft,
1064 iSrc.fTop,
1065 iSrc.fRight,
1066 iSrc.fBottom,
1067 kSmallTileSize);
1068
bsalomon@google.comfb309512011-11-30 14:13:48 +00001069 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1070 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1071
1072 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1073 return kSmallTileSize;
1074 } else {
1075 return maxTextureSize;
1076 }
1077}
1078}
1079
1080bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001081 const GrTextureParams& params,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001082 const SkRect* srcRectPtr) const {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001083 // if bitmap is explictly texture backed then just use the texture
1084 if (NULL != bitmap.getTexture()) {
1085 return false;
1086 }
1087 // if it's larger than the max texture size, then we have no choice but
1088 // tiling
1089 const int maxTextureSize = fContext->getMaxTextureSize();
1090 if (bitmap.width() > maxTextureSize ||
1091 bitmap.height() > maxTextureSize) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001092 return true;
1093 }
1094 // if we are going to have to draw the whole thing, then don't tile
1095 if (NULL == srcRectPtr) {
1096 return false;
1097 }
1098 // if the entire texture is already in our cache then no reason to tile it
bsalomon@google.comb8670992012-07-25 21:27:09 +00001099 if (this->isBitmapInTextureCache(bitmap, params)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001100 return false;
1101 }
1102
1103 // At this point we know we could do the draw by uploading the entire bitmap
1104 // as a texture. However, if the texture would be large compared to the
1105 // cache size and we don't require most of it for this draw then tile to
1106 // reduce the amount of upload and cache spill.
1107
1108 // assumption here is that sw bitmap size is a good proxy for its size as
1109 // a texture
1110 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001111 size_t cacheSize;
1112 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001113 if (bmpSize < cacheSize / 2) {
1114 return false;
1115 }
1116
robertphillips@google.combac6b052012-09-28 18:06:49 +00001117 SkScalar fracUsed = SkScalarMul(srcRectPtr->width() / bitmap.width(),
1118 srcRectPtr->height() / bitmap.height());
1119 if (fracUsed <= SK_ScalarHalf) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001120 return true;
1121 } else {
1122 return false;
1123 }
1124}
1125
reed@google.comac10a2d2010-12-22 21:39:39 +00001126void SkGpuDevice::drawBitmap(const SkDraw& draw,
1127 const SkBitmap& bitmap,
1128 const SkIRect* srcRectPtr,
1129 const SkMatrix& m,
1130 const SkPaint& paint) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001131
1132 SkRect tmp;
1133 SkRect* tmpPtr = NULL;
1134
1135 // convert from SkIRect to SkRect
1136 if (NULL != srcRectPtr) {
1137 tmp.set(*srcRectPtr);
1138 tmpPtr = &tmp;
1139 }
1140
1141 // We cannot call drawBitmapRect here since 'm' could be anything
1142 this->drawBitmapCommon(draw, bitmap, tmpPtr, m, paint);
1143}
1144
1145void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1146 const SkBitmap& bitmap,
1147 const SkRect* srcRectPtr,
1148 const SkMatrix& m,
1149 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001150 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001151
robertphillips@google.combac6b052012-09-28 18:06:49 +00001152 SkRect srcRect;
reed@google.comac10a2d2010-12-22 21:39:39 +00001153 if (NULL == srcRectPtr) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001154 srcRect.set(0, 0, SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001155 } else {
1156 srcRect = *srcRectPtr;
1157 }
1158
junov@google.comd935cfb2011-06-27 20:48:23 +00001159 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001160 // Convert the bitmap to a shader so that the rect can be drawn
1161 // through drawRect, which supports mask filters.
robertphillips@google.combac6b052012-09-28 18:06:49 +00001162 SkMatrix newM(m);
junov@google.com1d329782011-07-28 20:10:09 +00001163 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001164 const SkBitmap* bitmapPtr = &bitmap;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001165 if (NULL != srcRectPtr) {
1166 SkIRect iSrc;
1167 srcRect.roundOut(&iSrc);
1168 if (!bitmap.extractSubset(&tmp, iSrc)) {
epoger@google.com9ef2d832011-07-01 21:12:20 +00001169 return; // extraction failed
1170 }
1171 bitmapPtr = &tmp;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001172 srcRect.offset(SkIntToScalar(-iSrc.fLeft), SkIntToScalar(-iSrc.fTop));
1173 // The source rect has changed so update the matrix
1174 newM.preTranslate(SkIntToScalar(iSrc.fLeft), SkIntToScalar(iSrc.fTop));
junov@google.comd935cfb2011-06-27 20:48:23 +00001175 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001176
junov@google.comd935cfb2011-06-27 20:48:23 +00001177 SkPaint paintWithTexture(paint);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001178 paintWithTexture.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
junov@google.comd935cfb2011-06-27 20:48:23 +00001179 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001180
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001181 // Transform 'newM' needs to be concatenated to the current matrix,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001182 // rather than transforming the primitive directly, so that 'newM' will
junov@google.com1d329782011-07-28 20:10:09 +00001183 // also affect the behavior of the mask filter.
1184 SkMatrix drawMatrix;
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001185 drawMatrix.setConcat(fContext->getMatrix(), newM);
junov@google.com1d329782011-07-28 20:10:09 +00001186 SkDraw transformedDraw(draw);
1187 transformedDraw.fMatrix = &drawMatrix;
1188
robertphillips@google.combac6b052012-09-28 18:06:49 +00001189 this->drawRect(transformedDraw, srcRect, paintWithTexture);
junov@google.com1d329782011-07-28 20:10:09 +00001190
junov@google.comd935cfb2011-06-27 20:48:23 +00001191 return;
1192 }
1193
bsalomon@google.com5782d712011-01-21 21:03:59 +00001194 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001195 SkAutoCachedTexture colorLutTexture;
1196 if (!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001197 return;
1198 }
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001199 GrTextureParams params;
1200 params.setBilerp(paint.isFilterBitmap());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001201
robertphillips@google.combac6b052012-09-28 18:06:49 +00001202 if (!this->shouldTileBitmap(bitmap, params, srcRectPtr)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001203 // take the simple case
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001204 this->internalDrawBitmap(bitmap, srcRect, m, params, &grPaint);
robertphillips@google.combac6b052012-09-28 18:06:49 +00001205 } else {
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001206 this->drawTiledBitmap(bitmap, srcRect, m, params, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001207 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001208}
1209
1210// Break 'bitmap' into several tiles to draw it since it has already
1211// been determined to be too large to fit in VRAM
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001212void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001213 const SkRect& srcRect,
1214 const SkMatrix& m,
1215 const GrTextureParams& params,
1216 GrPaint* grPaint) {
1217 const int maxTextureSize = fContext->getMaxTextureSize();
1218
1219 int tileSize = determine_tile_size(bitmap, srcRect, maxTextureSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001220
reed@google.comac10a2d2010-12-22 21:39:39 +00001221 // compute clip bounds in local coordinates
robertphillips@google.combac6b052012-09-28 18:06:49 +00001222 SkRect clipRect;
reed@google.comac10a2d2010-12-22 21:39:39 +00001223 {
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001224 const GrRenderTarget* rt = fContext->getRenderTarget();
1225 clipRect.setWH(SkIntToScalar(rt->width()), SkIntToScalar(rt->height()));
1226 if (!fContext->getClip()->fClipStack->intersectRectWithClip(&clipRect)) {
1227 return;
1228 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001229 SkMatrix matrix, inverse;
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001230 matrix.setConcat(fContext->getMatrix(), m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001231 if (!matrix.invert(&inverse)) {
1232 return;
1233 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001234 inverse.mapRect(&clipRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001235 }
1236
bsalomon@google.comfb309512011-11-30 14:13:48 +00001237 int nx = bitmap.width() / tileSize;
1238 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001239 for (int x = 0; x <= nx; x++) {
1240 for (int y = 0; y <= ny; y++) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001241 SkRect tileR;
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001242 tileR.set(SkIntToScalar(x * tileSize),
robertphillips@google.combac6b052012-09-28 18:06:49 +00001243 SkIntToScalar(y * tileSize),
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001244 SkIntToScalar((x + 1) * tileSize),
robertphillips@google.combac6b052012-09-28 18:06:49 +00001245 SkIntToScalar((y + 1) * tileSize));
1246
1247 if (!SkRect::Intersects(tileR, clipRect)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001248 continue;
1249 }
1250
robertphillips@google.combac6b052012-09-28 18:06:49 +00001251 if (!tileR.intersect(srcRect)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001252 continue;
1253 }
1254
1255 SkBitmap tmpB;
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001256 SkIRect iTileR;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001257 tileR.roundOut(&iTileR);
1258 if (bitmap.extractSubset(&tmpB, iTileR)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001259 // now offset it to make it "local" to our tmp bitmap
robertphillips@google.combac6b052012-09-28 18:06:49 +00001260 tileR.offset(SkIntToScalar(-iTileR.fLeft), SkIntToScalar(-iTileR.fTop));
reed@google.comac10a2d2010-12-22 21:39:39 +00001261 SkMatrix tmpM(m);
skia.committer@gmail.comdc3a4e52012-10-02 02:01:24 +00001262 tmpM.preTranslate(SkIntToScalar(iTileR.fLeft),
robertphillips@google.comffad46b2012-10-01 14:32:51 +00001263 SkIntToScalar(iTileR.fTop));
1264
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001265 this->internalDrawBitmap(tmpB, tileR, tmpM, params, grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001266 }
1267 }
1268 }
1269}
1270
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001271namespace {
1272
1273bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1274 // detect pixel disalignment
1275 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1276 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
rmistry@google.comd6176b02012-08-23 18:14:13 +00001277 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001278 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1279 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1280 COLOR_BLEED_TOLERANCE &&
1281 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1282 COLOR_BLEED_TOLERANCE) {
1283 return true;
1284 }
1285 return false;
1286}
1287
1288bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1289 const SkMatrix& m) {
1290 // Only gets called if hasAlignedSamples returned false.
1291 // So we can assume that sampling is axis aligned but not texel aligned.
1292 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001293 SkRect innerSrcRect(srcRect), innerTransformedRect,
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001294 outerTransformedRect(transformedRect);
1295 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1296 m.mapRect(&innerTransformedRect, innerSrcRect);
1297
1298 // The gap between outerTransformedRect and innerTransformedRect
1299 // represents the projection of the source border area, which is
1300 // problematic for color bleeding. We must check whether any
1301 // destination pixels sample the border area.
1302 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1303 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1304 SkIRect outer, inner;
1305 outerTransformedRect.round(&outer);
1306 innerTransformedRect.round(&inner);
1307 // If the inner and outer rects round to the same result, it means the
1308 // border does not overlap any pixel centers. Yay!
1309 return inner != outer;
1310}
1311
1312} // unnamed namespace
1313
reed@google.comac10a2d2010-12-22 21:39:39 +00001314/*
1315 * This is called by drawBitmap(), which has to handle images that may be too
1316 * large to be represented by a single texture.
1317 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001318 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1319 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001320 */
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001321void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001322 const SkRect& srcRect,
reed@google.comac10a2d2010-12-22 21:39:39 +00001323 const SkMatrix& m,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001324 const GrTextureParams& params,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001325 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001326 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1327 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001328
reed@google.com9c49bc32011-07-07 13:42:37 +00001329 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001330 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001331 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001332 return;
1333 }
1334
bsalomon@google.com08283af2012-10-26 13:01:20 +00001335 GrEffectStage* stage = grPaint->colorStage(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001336
reed@google.comac10a2d2010-12-22 21:39:39 +00001337 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001338 SkAutoCachedTexture act(this, bitmap, &params, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001339 if (NULL == texture) {
1340 return;
1341 }
1342
robertphillips@google.combac6b052012-09-28 18:06:49 +00001343 GrRect dstRect(srcRect);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001344 GrRect paintRect;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001345 SkScalar wInv = SkScalarInvert(SkIntToScalar(bitmap.width()));
1346 SkScalar hInv = SkScalarInvert(SkIntToScalar(bitmap.height()));
1347 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1348 SkScalarMul(srcRect.fTop, hInv),
1349 SkScalarMul(srcRect.fRight, wInv),
1350 SkScalarMul(srcRect.fBottom, hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001351
rmistry@google.comd6176b02012-08-23 18:14:13 +00001352 bool needsTextureDomain = false;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001353 if (params.isBilerp()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001354 // Need texture domain if drawing a sub rect.
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001355 needsTextureDomain = srcRect.width() < bitmap.width() ||
robertphillips@google.combac6b052012-09-28 18:06:49 +00001356 srcRect.height() < bitmap.height();
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001357 if (m.rectStaysRect() && fContext->getMatrix().rectStaysRect()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001358 // sampling is axis-aligned
robertphillips@google.combac6b052012-09-28 18:06:49 +00001359 GrRect transformedRect;
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001360 SkMatrix srcToDeviceMatrix(m);
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001361 srcToDeviceMatrix.postConcat(fContext->getMatrix());
robertphillips@google.combac6b052012-09-28 18:06:49 +00001362 srcToDeviceMatrix.mapRect(&transformedRect, srcRect);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001363
robertphillips@google.combac6b052012-09-28 18:06:49 +00001364 if (hasAlignedSamples(srcRect, transformedRect)) {
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001365 // We could also turn off filtering here (but we already did a cache lookup with
1366 // params).
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001367 needsTextureDomain = false;
1368 } else {
1369 needsTextureDomain = needsTextureDomain &&
robertphillips@google.combac6b052012-09-28 18:06:49 +00001370 mayColorBleed(srcRect, transformedRect, m);
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001371 }
1372 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001373 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001374
1375 GrRect textureDomain = GrRect::MakeEmpty();
bsalomon@google.com021fc732012-10-25 12:47:42 +00001376 SkAutoTUnref<GrEffect> effect;
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001377 if (needsTextureDomain) {
1378 // Use a constrained texture domain to avoid color bleeding
bsalomon@google.com81712882012-11-01 17:12:34 +00001379 SkScalar left, top, right, bottom;
1380 if (srcRect.width() > SK_Scalar1) {
1381 SkScalar border = SK_ScalarHalf / bitmap.width();
junov@google.com6acc9b32011-05-16 18:32:07 +00001382 left = paintRect.left() + border;
1383 right = paintRect.right() - border;
1384 } else {
bsalomon@google.com81712882012-11-01 17:12:34 +00001385 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
junov@google.com6acc9b32011-05-16 18:32:07 +00001386 }
bsalomon@google.com81712882012-11-01 17:12:34 +00001387 if (srcRect.height() > SK_Scalar1) {
1388 SkScalar border = SK_ScalarHalf / bitmap.height();
junov@google.com6acc9b32011-05-16 18:32:07 +00001389 top = paintRect.top() + border;
1390 bottom = paintRect.bottom() - border;
1391 } else {
bsalomon@google.com81712882012-11-01 17:12:34 +00001392 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
junov@google.com6acc9b32011-05-16 18:32:07 +00001393 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001394 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +00001395 effect.reset(GrTextureDomainEffect::Create(texture,
1396 SkMatrix::I(),
1397 textureDomain,
1398 GrTextureDomainEffect::kClamp_WrapMode,
1399 params.isBilerp()));
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001400 } else {
bsalomon@google.com021fc732012-10-25 12:47:42 +00001401 effect.reset(SkNEW_ARGS(GrSingleTextureEffect, (texture, params)));
junov@google.com6acc9b32011-05-16 18:32:07 +00001402 }
bsalomon@google.com08283af2012-10-26 13:01:20 +00001403 grPaint->colorStage(kBitmapTextureIdx)->setEffect(effect);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001404 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001405}
1406
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001407namespace {
1408
bsalomon@google.comf271cc72012-10-24 19:35:13 +00001409void apply_effect(GrContext* context,
1410 GrTexture* srcTexture,
1411 GrTexture* dstTexture,
1412 const GrRect& rect,
1413 GrEffect* effect) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001414 SkASSERT(srcTexture && srcTexture->getContext() == context);
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001415 GrContext::AutoMatrix am;
1416 am.setIdentity(context);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001417 GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget());
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001418 GrContext::AutoClip acs(context, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001419
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001420 GrPaint paint;
bsalomon@google.comdbe49f72012-11-05 16:36:02 +00001421 paint.colorStage(0)->setEffect(effect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001422 context->drawRect(paint, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001423}
1424
1425};
1426
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001427static GrTexture* filter_texture(SkDevice* device, GrContext* context,
1428 GrTexture* texture, SkImageFilter* filter,
1429 const GrRect& rect) {
reed@google.com8926b162012-03-23 15:36:36 +00001430 GrAssert(filter);
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001431 SkDeviceImageFilterProxy proxy(device);
reed@google.com8926b162012-03-23 15:36:36 +00001432
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001433 GrTextureDesc desc;
1434 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1435 desc.fWidth = SkScalarCeilToInt(rect.width());
1436 desc.fHeight = SkScalarCeilToInt(rect.height());
bsalomon@google.com0342a852012-08-20 19:22:38 +00001437 desc.fConfig = kRGBA_8888_GrPixelConfig;
bsalomon@google.com021fc732012-10-25 12:47:42 +00001438 GrEffect* effect;
reed@google.com8926b162012-03-23 15:36:36 +00001439
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001440 if (filter->canFilterImageGPU()) {
senorblanco@chromium.org985fa792012-10-24 15:14:26 +00001441 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1442 // filter. Also set the clip wide open and the matrix to identity.
1443 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001444 texture = filter->onFilterImageGPU(&proxy, texture, rect);
bsalomon@google.com021fc732012-10-25 12:47:42 +00001445 } else if (filter->asNewEffect(&effect, texture)) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001446 GrAutoScratchTexture dst(context, desc);
bsalomon@google.com021fc732012-10-25 12:47:42 +00001447 apply_effect(context, texture, dst.texture(), rect, effect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001448 texture = dst.detach();
bsalomon@google.com021fc732012-10-25 12:47:42 +00001449 effect->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001450 }
1451 return texture;
1452}
1453
reed@google.comac10a2d2010-12-22 21:39:39 +00001454void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001455 int left, int top, const SkPaint& paint) {
1456 // drawSprite is defined to be in device coords.
1457 CHECK_SHOULD_DRAW(draw, true);
reed@google.comac10a2d2010-12-22 21:39:39 +00001458
reed@google.com8926b162012-03-23 15:36:36 +00001459 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001460 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1461 return;
1462 }
1463
reed@google.com76dd2772012-01-05 21:15:07 +00001464 int w = bitmap.width();
1465 int h = bitmap.height();
1466
bsalomon@google.com5782d712011-01-21 21:03:59 +00001467 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001468 SkAutoCachedTexture colorLutTexture;
1469 if(!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001470 return;
1471 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001472
bsalomon@google.com08283af2012-10-26 13:01:20 +00001473 GrEffectStage* stage = grPaint.colorStage(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001474
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001475 GrTexture* texture;
bsalomon@google.com08283af2012-10-26 13:01:20 +00001476 stage->reset();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001477 // draw sprite uses the default texture params
1478 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
bsalomon@google.com08283af2012-10-26 13:01:20 +00001479 grPaint.colorStage(kBitmapTextureIdx)->setEffect(SkNEW_ARGS
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001480 (GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001481
reed@google.com8926b162012-03-23 15:36:36 +00001482 SkImageFilter* filter = paint.getImageFilter();
1483 if (NULL != filter) {
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001484 GrTexture* filteredTexture = filter_texture(this, fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001485 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001486 if (filteredTexture) {
bsalomon@google.com08283af2012-10-26 13:01:20 +00001487 grPaint.colorStage(kBitmapTextureIdx)->setEffect(SkNEW_ARGS
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001488 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001489 texture = filteredTexture;
1490 filteredTexture->unref();
1491 }
reed@google.com76dd2772012-01-05 21:15:07 +00001492 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001493
bsalomon@google.com5782d712011-01-21 21:03:59 +00001494 fContext->drawRectToRect(grPaint,
bsalomon@google.com81712882012-11-01 17:12:34 +00001495 GrRect::MakeXYWH(SkIntToScalar(left),
1496 SkIntToScalar(top),
1497 SkIntToScalar(w),
1498 SkIntToScalar(h)),
1499 GrRect::MakeWH(SK_Scalar1 * w / texture->width(),
1500 SK_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001501}
1502
reed@google.com33535f32012-09-25 15:37:50 +00001503void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1504 const SkRect* src, const SkRect& dst,
1505 const SkPaint& paint) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001506 SkMatrix matrix;
1507 SkRect bitmapBounds, tmpSrc;
1508
1509 bitmapBounds.set(0, 0,
1510 SkIntToScalar(bitmap.width()),
1511 SkIntToScalar(bitmap.height()));
1512
reed@google.com33535f32012-09-25 15:37:50 +00001513 // Compute matrix from the two rectangles
robertphillips@google.combac6b052012-09-28 18:06:49 +00001514 if (NULL != src) {
1515 tmpSrc = *src;
1516 } else {
1517 tmpSrc = bitmapBounds;
1518 }
1519 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1520
1521 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1522 if (NULL != src) {
1523 if (!bitmapBounds.contains(tmpSrc)) {
1524 if (!tmpSrc.intersect(bitmapBounds)) {
1525 return; // nothing to draw
reed@google.com33535f32012-09-25 15:37:50 +00001526 }
reed@google.com33535f32012-09-25 15:37:50 +00001527 }
reed@google.com33535f32012-09-25 15:37:50 +00001528 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +00001529
robertphillips@google.combac6b052012-09-28 18:06:49 +00001530 this->drawBitmapCommon(draw, bitmap, &tmpSrc, matrix, paint);
reed@google.com33535f32012-09-25 15:37:50 +00001531}
1532
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001533void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001534 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001535 // clear of the source device must occur before CHECK_SHOULD_DRAW
1536 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1537 if (dev->fNeedClear) {
1538 // TODO: could check here whether we really need to draw at all
1539 dev->clear(0x0);
1540 }
1541
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001542 // drawDevice is defined to be in device coords.
1543 CHECK_SHOULD_DRAW(draw, true);
reed@google.comac10a2d2010-12-22 21:39:39 +00001544
bsalomon@google.com5782d712011-01-21 21:03:59 +00001545 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001546 SkAutoCachedTexture colorLutTexture;
bsalomon@google.com08283af2012-10-26 13:01:20 +00001547 grPaint.colorStage(kBitmapTextureIdx)->reset();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001548 if (!dev->bindDeviceAsTexture(&grPaint) ||
twiz@google.com58071162012-07-18 21:41:50 +00001549 !skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001550 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001551 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001552
bsalomon@google.com08283af2012-10-26 13:01:20 +00001553 GrTexture* devTex = grPaint.getColorStage(kBitmapTextureIdx).getEffect()->texture(0);
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001554 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001555
reed@google.com8926b162012-03-23 15:36:36 +00001556 SkImageFilter* filter = paint.getImageFilter();
1557 if (NULL != filter) {
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001558 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001559 SkIntToScalar(devTex->height()));
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001560 GrTexture* filteredTexture = filter_texture(this, fContext, devTex, filter, rect);
reed@google.com8926b162012-03-23 15:36:36 +00001561 if (filteredTexture) {
bsalomon@google.com08283af2012-10-26 13:01:20 +00001562 grPaint.colorStage(kBitmapTextureIdx)->setEffect(SkNEW_ARGS
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001563 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001564 devTex = filteredTexture;
1565 filteredTexture->unref();
1566 }
1567 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001568
bsalomon@google.com5782d712011-01-21 21:03:59 +00001569 const SkBitmap& bm = dev->accessBitmap(false);
1570 int w = bm.width();
1571 int h = bm.height();
1572
bsalomon@google.com81712882012-11-01 17:12:34 +00001573 GrRect dstRect = GrRect::MakeXYWH(SkIntToScalar(x),
1574 SkIntToScalar(y),
1575 SkIntToScalar(w),
1576 SkIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001577
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001578 // The device being drawn may not fill up its texture (saveLayer uses
1579 // the approximate ).
bsalomon@google.com81712882012-11-01 17:12:34 +00001580 GrRect srcRect = GrRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1581 SK_Scalar1 * h / devTex->height());
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001582
1583 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001584}
1585
reed@google.com8926b162012-03-23 15:36:36 +00001586bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
bsalomon@google.com8ea78d82012-10-24 20:11:30 +00001587 if (!filter->asNewEffect(NULL, NULL) &&
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001588 !filter->canFilterImageGPU()) {
reed@google.com76dd2772012-01-05 21:15:07 +00001589 return false;
1590 }
reed@google.com8926b162012-03-23 15:36:36 +00001591 return true;
1592}
1593
1594bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1595 const SkMatrix& ctm,
1596 SkBitmap* result, SkIPoint* offset) {
1597 // want explicitly our impl, so guard against a subclass of us overriding it
1598 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001599 return false;
1600 }
reed@google.com8926b162012-03-23 15:36:36 +00001601
1602 SkAutoLockPixels alp(src, !src.getTexture());
1603 if (!src.getTexture() && !src.readyToDraw()) {
1604 return false;
1605 }
1606
1607 GrPaint paint;
reed@google.com8926b162012-03-23 15:36:36 +00001608
reed@google.com8926b162012-03-23 15:36:36 +00001609 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001610 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1611 // must be pushed upstack.
1612 SkAutoCachedTexture act(this, src, NULL, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001613
1614 result->setConfig(src.config(), src.width(), src.height());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001615 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001616 SkIntToScalar(src.height()));
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001617 GrTexture* resultTexture = filter_texture(this, fContext, texture, filter, rect);
reed@google.com8926b162012-03-23 15:36:36 +00001618 if (resultTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001619 result->setPixelRef(SkNEW_ARGS(SkGrTexturePixelRef,
1620 (resultTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001621 resultTexture->unref();
1622 }
reed@google.com76dd2772012-01-05 21:15:07 +00001623 return true;
1624}
1625
reed@google.comac10a2d2010-12-22 21:39:39 +00001626///////////////////////////////////////////////////////////////////////////////
1627
1628// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001629static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001630 kTriangles_GrPrimitiveType,
1631 kTriangleStrip_GrPrimitiveType,
1632 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001633};
1634
1635void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1636 int vertexCount, const SkPoint vertices[],
1637 const SkPoint texs[], const SkColor colors[],
1638 SkXfermode* xmode,
1639 const uint16_t indices[], int indexCount,
1640 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001641 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001642
bsalomon@google.com5782d712011-01-21 21:03:59 +00001643 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +00001644 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com5782d712011-01-21 21:03:59 +00001645 // we ignore the shader if texs is null.
1646 if (NULL == texs) {
twiz@google.com58071162012-07-18 21:41:50 +00001647 if (!skPaint2GrPaintNoShader(this,
1648 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001649 false,
1650 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001651 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +00001652 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001653 return;
1654 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001655 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001656 if (!skPaint2GrPaintShader(this,
1657 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001658 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001659 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001660 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001661 return;
1662 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001663 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001664
1665 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001666 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001667 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1668#if 0
1669 return
1670#endif
1671 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001672 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001673
bsalomon@google.com498776a2011-08-16 19:20:44 +00001674 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1675 if (NULL != colors) {
1676 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001677 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001678 for (int i = 0; i < vertexCount; ++i) {
rileya@google.com24f3ad12012-07-18 21:47:40 +00001679 convertedColors[i] = SkColor2GrColor(colors[i]);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001680 }
1681 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001682 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001683 fContext->drawVertices(grPaint,
1684 gVertexMode2PrimitiveType[vmode],
1685 vertexCount,
1686 (GrPoint*) vertices,
1687 (GrPoint*) texs,
1688 colors,
1689 indices,
1690 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001691}
1692
1693///////////////////////////////////////////////////////////////////////////////
1694
1695static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001696 GrFontScaler* scaler = (GrFontScaler*)data;
1697 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001698}
1699
1700static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1701 void* auxData;
1702 GrFontScaler* scaler = NULL;
1703 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1704 scaler = (GrFontScaler*)auxData;
1705 }
1706 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001707 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001708 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1709 }
1710 return scaler;
1711}
1712
1713static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1714 SkFixed fx, SkFixed fy,
1715 const SkGlyph& glyph) {
1716 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1717
bungeman@google.com15865a72012-01-11 16:28:04 +00001718 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001719
1720 if (NULL == procs->fFontScaler) {
1721 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1722 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001723
bungeman@google.com15865a72012-01-11 16:28:04 +00001724 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1725 glyph.getSubXFixed(),
1726 glyph.getSubYFixed()),
1727 SkFixedFloorToFixed(fx),
1728 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001729 procs->fFontScaler);
1730}
1731
bsalomon@google.com5782d712011-01-21 21:03:59 +00001732SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001733
1734 // deferred allocation
1735 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001736 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001737 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1738 fDrawProcs->fContext = fContext;
1739 }
1740
1741 // init our (and GL's) state
1742 fDrawProcs->fTextContext = context;
1743 fDrawProcs->fFontScaler = NULL;
1744 return fDrawProcs;
1745}
1746
1747void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1748 size_t byteLength, SkScalar x, SkScalar y,
1749 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001750 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001751
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001752 if (fContext->getMatrix().hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001753 // this guy will just call our drawPath()
1754 draw.drawText((const char*)text, byteLength, x, y, paint);
1755 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001756 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001757
1758 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +00001759 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001760 if (!skPaint2GrPaintShader(this,
1761 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001762 true,
twiz@google.com58071162012-07-18 21:41:50 +00001763 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001764 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001765 return;
1766 }
bsalomon@google.com0e354aa2012-10-08 20:44:25 +00001767 GrTextContext context(fContext, grPaint);
tomhudson@google.com375ff852012-06-29 18:37:57 +00001768 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001769 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1770 }
1771}
1772
1773void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1774 size_t byteLength, const SkScalar pos[],
1775 SkScalar constY, int scalarsPerPos,
1776 const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001777 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001778
bsalomon@google.com3ab43d52012-10-11 19:39:09 +00001779 if (fContext->getMatrix().hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001780 // this guy will just call our drawPath()
1781 draw.drawPosText((const char*)text, byteLength, pos, constY,
1782 scalarsPerPos, paint);
1783 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001784 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001785
1786 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +00001787 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001788 if (!skPaint2GrPaintShader(this,
1789 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001790 true,
twiz@google.com58071162012-07-18 21:41:50 +00001791 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001792 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001793 return;
1794 }
bsalomon@google.com0e354aa2012-10-08 20:44:25 +00001795 GrTextContext context(fContext, grPaint);
tomhudson@google.com375ff852012-06-29 18:37:57 +00001796 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001797 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1798 scalarsPerPos, paint);
1799 }
1800}
1801
1802void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1803 size_t len, const SkPath& path,
1804 const SkMatrix* m, const SkPaint& paint) {
bsalomon@google.com3cbaa2d2012-10-12 14:51:52 +00001805 CHECK_SHOULD_DRAW(draw, false);
reed@google.comac10a2d2010-12-22 21:39:39 +00001806
1807 SkASSERT(draw.fDevice == this);
1808 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1809}
1810
1811///////////////////////////////////////////////////////////////////////////////
1812
reed@google.comf67e4cf2011-03-15 20:56:58 +00001813bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1814 if (!paint.isLCDRenderText()) {
1815 // we're cool with the paint as is
1816 return false;
1817 }
1818
1819 if (paint.getShader() ||
1820 paint.getXfermode() || // unless its srcover
1821 paint.getMaskFilter() ||
1822 paint.getRasterizer() ||
1823 paint.getColorFilter() ||
1824 paint.getPathEffect() ||
1825 paint.isFakeBoldText() ||
1826 paint.getStyle() != SkPaint::kFill_Style) {
1827 // turn off lcd
1828 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1829 flags->fHinting = paint.getHinting();
1830 return true;
1831 }
1832 // we're cool with the paint as is
1833 return false;
1834}
1835
reed@google.com75d939b2011-12-07 15:07:23 +00001836void SkGpuDevice::flush() {
bsalomon@google.coma6926b12012-10-10 15:25:50 +00001837 DO_DEFERRED_CLEAR();
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001838 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001839}
1840
reed@google.comf67e4cf2011-03-15 20:56:58 +00001841///////////////////////////////////////////////////////////////////////////////
1842
bsalomon@google.comfb309512011-11-30 14:13:48 +00001843bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001844 const GrTextureParams& params) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001845 uint64_t key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001846 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001847
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001848 GrTextureDesc desc;
1849 desc.fWidth = bitmap.width();
1850 desc.fHeight = bitmap.height();
rileya@google.com24f3ad12012-07-18 21:47:40 +00001851 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config());
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001852
robertphillips@google.com9c2ea842012-08-13 17:47:59 +00001853 GrCacheData cacheData(key);
1854
1855 return this->context()->isTextureInCache(desc, cacheData, &params);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001856}
1857
1858
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001859SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1860 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001861 bool isOpaque,
1862 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001863 GrTextureDesc desc;
1864 desc.fConfig = fRenderTarget->config();
1865 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1866 desc.fWidth = width;
1867 desc.fHeight = height;
1868 desc.fSampleCnt = fRenderTarget->numSamples();
1869
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001870 GrTexture* texture;
1871 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001872 // Skia's convention is to only clear a device if it is non-opaque.
1873 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001874
1875#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1876 // layers are never draw in repeat modes, so we can request an approx
1877 // match and ignore any padding.
1878 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1879 GrContext::kApprox_ScratchTexMatch :
1880 GrContext::kExact_ScratchTexMatch;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001881 texture = fContext->lockScratchTexture(desc, matchType);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001882#else
1883 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1884 texture = tunref.get();
1885#endif
1886 if (texture) {
1887 return SkNEW_ARGS(SkGpuDevice,(fContext,
1888 texture,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001889 needClear));
1890 } else {
1891 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1892 width, height);
1893 return NULL;
1894 }
1895}
1896
1897SkGpuDevice::SkGpuDevice(GrContext* context,
1898 GrTexture* texture,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001899 bool needClear)
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001900 : SkDevice(make_bitmap(context, texture->asRenderTarget())) {
1901
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001902 GrAssert(texture && texture->asRenderTarget());
bsalomon@google.com8090e652012-08-28 15:07:11 +00001903 // This constructor is called from onCreateCompatibleDevice. It has locked the RT in the texture
1904 // cache. We pass true for the third argument so that it will get unlocked.
1905 this->initFromRenderTarget(context, texture->asRenderTarget(), true);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001906 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001907}