blob: 1eb9e49ee49942f306ee570e2eab82cccec5840e [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
twiz@google.com58071162012-07-18 21:41:50 +000010#include "effects/GrColorTableEffect.h"
tomhudson@google.com2f68e762012-07-17 18:43:21 +000011#include "effects/GrTextureDomainEffect.h"
epoger@google.comec3ed6a2011-07-28 14:26:00 +000012
reed@google.comac10a2d2010-12-22 21:39:39 +000013#include "GrContext.h"
14#include "GrTextContext.h"
15
robertphillips@google.come9c04692012-06-29 00:30:13 +000016#include "SkGrTexturePixelRef.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000017
Scroggo97c88c22011-05-11 14:05:25 +000018#include "SkColorFilter.h"
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.comfe626382011-09-21 13:50:35 +000022#include "SkTLazy.h"
reed@google.comc9aa5872011-04-05 21:05:37 +000023#include "SkUtils.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000024
bsalomon@google.com06cd7322012-03-30 18:45:35 +000025#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
reed@google.comac10a2d2010-12-22 21:39:39 +000026
27#if 0
28 extern bool (*gShouldDrawProc)();
29 #define CHECK_SHOULD_DRAW(draw) \
30 do { \
31 if (gShouldDrawProc && !gShouldDrawProc()) return; \
32 this->prepareRenderTarget(draw); \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000033 GrAssert(!fNeedClear) \
reed@google.comac10a2d2010-12-22 21:39:39 +000034 } while (0)
35#else
bsalomon@google.com06cd7322012-03-30 18:45:35 +000036 #define CHECK_SHOULD_DRAW(draw) this->prepareRenderTarget(draw); \
37 GrAssert(!fNeedClear)
reed@google.comac10a2d2010-12-22 21:39:39 +000038#endif
39
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000040// we use the same texture slot on GrPaint for bitmaps and shaders
41// (since drawBitmap, drawSprite, and drawDevice ignore skia's shader)
42enum {
43 kBitmapTextureIdx = 0,
twiz@google.com58071162012-07-18 21:41:50 +000044 kShaderTextureIdx = 0,
45 kColorFilterTextureIdx = 1
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000046};
47
reed@google.comcde92112011-07-06 20:00:52 +000048
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +000049#define MAX_BLUR_SIGMA 4.0f
50// FIXME: This value comes from from SkBlurMaskFilter.cpp.
51// Should probably be put in a common header someplace.
52#define MAX_BLUR_RADIUS SkIntToScalar(128)
53// This constant approximates the scaling done in the software path's
54// "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
55// IMHO, it actually should be 1: we blur "less" than we should do
56// according to the CSS and canvas specs, simply because Safari does the same.
57// Firefox used to do the same too, until 4.0 where they fixed it. So at some
58// point we should probably get rid of these scaling constants and rebaseline
59// all the blur tests.
60#define BLUR_SIGMA_SCALE 0.6f
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000061// This constant represents the screen alignment criterion in texels for
62// requiring texture domain clamping to prevent color bleeding when drawing
63// a sub region of a larger source image.
64#define COLOR_BLEED_TOLERANCE SkFloatToScalar(0.001f)
bsalomon@google.com06cd7322012-03-30 18:45:35 +000065
66#define DO_DEFERRED_CLEAR \
67 do { \
68 if (fNeedClear) { \
bsalomon@google.com730ca3b2012-04-03 13:25:12 +000069 this->clear(0x0); \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000070 fNeedClear = false; \
71 } \
72 } while (false) \
73
reed@google.comac10a2d2010-12-22 21:39:39 +000074///////////////////////////////////////////////////////////////////////////////
75
reed@google.comb0a34d82012-07-11 19:57:55 +000076#define CHECK_FOR_NODRAW_ANNOTATION(paint) \
77 do { if (paint.isNoDrawAnnotation()) { return; } } while (0)
78
79///////////////////////////////////////////////////////////////////////////////
80
81
bsalomon@google.com84405e02012-03-05 19:57:21 +000082class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
83public:
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000084 SkAutoCachedTexture()
85 : fDevice(NULL)
86 , fTexture(NULL) {
87 }
88
bsalomon@google.com84405e02012-03-05 19:57:21 +000089 SkAutoCachedTexture(SkGpuDevice* device,
90 const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +000091 const GrTextureParams* params,
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000092 GrTexture** texture)
93 : fDevice(NULL)
94 , fTexture(NULL) {
95 GrAssert(NULL != texture);
bsalomon@google.comb8670992012-07-25 21:27:09 +000096 *texture = this->set(device, bitmap, params);
reed@google.comac10a2d2010-12-22 21:39:39 +000097 }
reed@google.comac10a2d2010-12-22 21:39:39 +000098
bsalomon@google.com84405e02012-03-05 19:57:21 +000099 ~SkAutoCachedTexture() {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000100 if (NULL != fTexture) {
101 GrUnlockCachedBitmapTexture(fTexture);
bsalomon@google.com84405e02012-03-05 19:57:21 +0000102 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000103 }
bsalomon@google.com84405e02012-03-05 19:57:21 +0000104
105 GrTexture* set(SkGpuDevice* device,
106 const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000107 const GrTextureParams* params) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000108 if (NULL != fTexture) {
109 GrUnlockCachedBitmapTexture(fTexture);
110 fTexture = NULL;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000111 }
112 fDevice = device;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000113 GrTexture* result = (GrTexture*)bitmap.getTexture();
114 if (NULL == result) {
115 // Cannot return the native texture so look it up in our cache
116 fTexture = GrLockCachedBitmapTexture(device->context(), bitmap, params);
117 result = fTexture;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000118 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000119 return result;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000120 }
121
122private:
123 SkGpuDevice* fDevice;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000124 GrTexture* fTexture;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000125};
reed@google.comac10a2d2010-12-22 21:39:39 +0000126
127///////////////////////////////////////////////////////////////////////////////
128
129bool gDoTraceDraw;
130
131struct GrSkDrawProcs : public SkDrawProcs {
132public:
133 GrContext* fContext;
134 GrTextContext* fTextContext;
135 GrFontScaler* fFontScaler; // cached in the skia glyphcache
136};
137
138///////////////////////////////////////////////////////////////////////////////
139
reed@google.comaf951c92011-06-16 19:10:39 +0000140static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
141 switch (config) {
142 case kAlpha_8_GrPixelConfig:
143 *isOpaque = false;
144 return SkBitmap::kA8_Config;
145 case kRGB_565_GrPixelConfig:
146 *isOpaque = true;
147 return SkBitmap::kRGB_565_Config;
148 case kRGBA_4444_GrPixelConfig:
149 *isOpaque = false;
150 return SkBitmap::kARGB_4444_Config;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000151 case kSkia8888_PM_GrPixelConfig:
152 // we don't currently have a way of knowing whether
153 // a 8888 is opaque based on the config.
154 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000155 return SkBitmap::kARGB_8888_Config;
156 default:
157 *isOpaque = false;
158 return SkBitmap::kNo_Config;
159 }
160}
reed@google.comac10a2d2010-12-22 21:39:39 +0000161
reed@google.comaf951c92011-06-16 19:10:39 +0000162static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000163 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000164
165 bool isOpaque;
166 SkBitmap bitmap;
167 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
168 renderTarget->width(), renderTarget->height());
169 bitmap.setIsOpaque(isOpaque);
170 return bitmap;
171}
172
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000173SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000174: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000175 this->initFromRenderTarget(context, texture->asRenderTarget());
176}
177
reed@google.comaf951c92011-06-16 19:10:39 +0000178SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000179: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000180 this->initFromRenderTarget(context, renderTarget);
181}
182
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000183void SkGpuDevice::initFromRenderTarget(GrContext* context,
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000184 GrRenderTarget* renderTarget) {
reed@google.comaf951c92011-06-16 19:10:39 +0000185 fNeedPrepareRenderTarget = false;
186 fDrawProcs = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000187
reed@google.comaf951c92011-06-16 19:10:39 +0000188 fContext = context;
189 fContext->ref();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000190
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000191 fCached = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000192 fTexture = NULL;
193 fRenderTarget = NULL;
194 fNeedClear = false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000195
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000196 GrAssert(NULL != renderTarget);
197 fRenderTarget = renderTarget;
198 fRenderTarget->ref();
199 // if this RT is also a texture, hold a ref on it
200 fTexture = fRenderTarget->asTexture();
201 SkSafeRef(fTexture);
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000202
203 // Create a pixel ref for the underlying SkBitmap. We prefer a texture pixel
204 // ref to a render target pixel reft. The pixel ref may get ref'ed outside
205 // the device via accessBitmap. This external ref may outlive the device.
206 // Since textures own their render targets (but not vice-versa) we
207 // are ensuring that both objects will live as long as the pixel ref.
208 SkPixelRef* pr;
209 if (fTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000210 pr = SkNEW_ARGS(SkGrTexturePixelRef, (fTexture));
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000211 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000212 pr = SkNEW_ARGS(SkGrRenderTargetPixelRef, (fRenderTarget));
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000213 }
reed@google.comaf951c92011-06-16 19:10:39 +0000214 this->setPixelRef(pr, 0)->unref();
215}
216
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000217SkGpuDevice::SkGpuDevice(GrContext* context,
218 SkBitmap::Config config,
219 int width,
220 int height)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000221 : SkDevice(config, width, height, false /*isOpaque*/) {
222
reed@google.comac10a2d2010-12-22 21:39:39 +0000223 fNeedPrepareRenderTarget = false;
224 fDrawProcs = NULL;
225
reed@google.com7b201d22011-01-11 18:59:23 +0000226 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000227 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000228
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000229 fCached = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000230 fTexture = NULL;
231 fRenderTarget = NULL;
232 fNeedClear = false;
233
reed@google.comaf951c92011-06-16 19:10:39 +0000234 if (config != SkBitmap::kRGB_565_Config) {
235 config = SkBitmap::kARGB_8888_Config;
236 }
237 SkBitmap bm;
238 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000239
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000240 GrTextureDesc desc;
241 desc.fFlags = kRenderTarget_GrTextureFlagBit;
242 desc.fWidth = width;
243 desc.fHeight = height;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000244 desc.fConfig = SkBitmapConfig2GrPixelConfig(bm.config());
reed@google.comac10a2d2010-12-22 21:39:39 +0000245
reed@google.comaf951c92011-06-16 19:10:39 +0000246 fTexture = fContext->createUncachedTexture(desc, NULL, 0);
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000247
reed@google.comaf951c92011-06-16 19:10:39 +0000248 if (NULL != fTexture) {
249 fRenderTarget = fTexture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000250 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000251
reed@google.comaf951c92011-06-16 19:10:39 +0000252 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000253
reed@google.comaf951c92011-06-16 19:10:39 +0000254 // wrap the bitmap with a pixelref to expose our texture
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000255 SkGrTexturePixelRef* pr = SkNEW_ARGS(SkGrTexturePixelRef, (fTexture));
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000256 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000257 } else {
258 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
259 width, height);
260 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000261 }
262}
263
264SkGpuDevice::~SkGpuDevice() {
265 if (fDrawProcs) {
266 delete fDrawProcs;
267 }
268
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000269 // The SkGpuDevice gives the context the render target (e.g., in gainFocus)
270 // This call gives the context a chance to relinquish it
271 fContext->setRenderTarget(NULL);
272
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000273 SkSafeUnref(fTexture);
274 SkSafeUnref(fRenderTarget);
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000275 if (NULL != fTexture && fCached) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000276 GrAssert(fRenderTarget == fTexture->asRenderTarget());
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000277 fContext->unlockTexture(fTexture);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000278 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000279 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000280}
281
reed@google.comac10a2d2010-12-22 21:39:39 +0000282///////////////////////////////////////////////////////////////////////////////
283
284void SkGpuDevice::makeRenderTargetCurrent() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000285 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000286 fContext->setRenderTarget(fRenderTarget);
287 fContext->flush(true);
288 fNeedPrepareRenderTarget = true;
289}
290
291///////////////////////////////////////////////////////////////////////////////
292
bsalomon@google.comc4364992011-11-07 15:54:49 +0000293namespace {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000294GrPixelConfig config8888_to_grconfig_and_flags(SkCanvas::Config8888 config8888, uint32_t* flags) {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000295 switch (config8888) {
296 case SkCanvas::kNative_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000297 *flags = 0;
298 return kSkia8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000299 case SkCanvas::kNative_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000300 *flags = GrContext::kUnpremul_PixelOpsFlag;
301 return kSkia8888_PM_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000302 case SkCanvas::kBGRA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000303 *flags = 0;
304 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000305 case SkCanvas::kBGRA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000306 *flags = GrContext::kUnpremul_PixelOpsFlag;
307 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000308 case SkCanvas::kRGBA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000309 *flags = 0;
310 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000311 case SkCanvas::kRGBA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000312 *flags = GrContext::kUnpremul_PixelOpsFlag;
313 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000314 default:
315 GrCrash("Unexpected Config8888.");
316 return kSkia8888_PM_GrPixelConfig;
317 }
318}
319}
320
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000321bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
322 int x, int y,
323 SkCanvas::Config8888 config8888) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000324 DO_DEFERRED_CLEAR;
bsalomon@google.com910267d2011-11-02 20:06:25 +0000325 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
326 SkASSERT(!bitmap.isNull());
327 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000328
bsalomon@google.com910267d2011-11-02 20:06:25 +0000329 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000330 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000331 uint32_t flags;
332 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000333 return fContext->readRenderTargetPixels(fRenderTarget,
334 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000335 bitmap.width(),
336 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000337 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000338 bitmap.getPixels(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000339 bitmap.rowBytes(),
340 flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000341}
342
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000343void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
344 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000345 SkAutoLockPixels alp(bitmap);
346 if (!bitmap.readyToDraw()) {
347 return;
348 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000349
350 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000351 uint32_t flags;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000352 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000353 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000354 } else {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000355 flags = 0;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000356 config= SkBitmapConfig2GrPixelConfig(bitmap.config());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000357 }
358
bsalomon@google.com6f379512011-11-16 20:36:03 +0000359 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000360 config, bitmap.getPixels(), bitmap.rowBytes(), flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000361}
362
robertphillips@google.com46f93502012-08-07 15:38:08 +0000363namespace {
364void purgeClipCB(int genID, void* data) {
365 GrContext* context = (GrContext*) data;
366
367 if (SkClipStack::kInvalidGenID == genID ||
368 SkClipStack::kEmptyGenID == genID ||
369 SkClipStack::kWideOpenGenID == genID) {
370 // none of these cases will have a cached clip mask
371 return;
372 }
373
374}
375};
376
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000377void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
378 INHERITED::onAttachToCanvas(canvas);
379
380 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000381 fClipData.fClipStack = canvas->getClipStack();
robertphillips@google.com46f93502012-08-07 15:38:08 +0000382
383 fClipData.fClipStack->addPurgeClipCallback(purgeClipCB, fContext);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000384}
385
386void SkGpuDevice::onDetachFromCanvas() {
387 INHERITED::onDetachFromCanvas();
388
robertphillips@google.com46f93502012-08-07 15:38:08 +0000389 // TODO: iterate through the clip stack and clean up any cached clip masks
390 fClipData.fClipStack->removePurgeClipCallback(purgeClipCB, fContext);
391
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000392 fClipData.fClipStack = NULL;
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000393}
394
robertphillips@google.com607fe072012-07-24 13:54:00 +0000395#ifdef SK_DEBUG
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000396static void check_bounds(const GrClipData& clipData,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000397 const SkRegion& clipRegion,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000398 int renderTargetWidth,
399 int renderTargetHeight) {
400
robertphillips@google.com7b112892012-07-31 15:18:21 +0000401 SkIRect devBound;
402
403 devBound.setLTRB(0, 0, renderTargetWidth, renderTargetHeight);
404
robertphillips@google.com607fe072012-07-24 13:54:00 +0000405 SkClipStack::BoundsType boundType;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000406 SkRect canvTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000407
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000408 clipData.fClipStack->getBounds(&canvTemp, &boundType);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000409 if (SkClipStack::kNormal_BoundsType == boundType) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000410 SkIRect devTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000411
robertphillips@google.com7b112892012-07-31 15:18:21 +0000412 canvTemp.roundOut(&devTemp);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000413
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000414 devTemp.offset(-clipData.fOrigin.fX, -clipData.fOrigin.fY);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000415
robertphillips@google.com7b112892012-07-31 15:18:21 +0000416 if (!devBound.intersect(devTemp)) {
417 devBound.setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000418 }
419 }
420
robertphillips@google.com768fee82012-08-02 12:42:43 +0000421 GrAssert(devBound.contains(clipRegion.getBounds()));
robertphillips@google.com607fe072012-07-24 13:54:00 +0000422}
423#endif
424
reed@google.comac10a2d2010-12-22 21:39:39 +0000425///////////////////////////////////////////////////////////////////////////////
426
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000427static void set_matrix_and_clip(GrContext* context, const SkMatrix& matrix,
428 GrClipData& clipData,
429 const SkRegion& clipRegion,
430 const SkIPoint& origin,
431 int renderTargetWidth, int renderTargetHeight) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000432 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000433
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000434 clipData.fOrigin = origin;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000435
436#ifdef SK_DEBUG
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000437 check_bounds(clipData, clipRegion,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000438 renderTargetWidth, renderTargetHeight);
439#endif
440
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000441 context->setClip(&clipData);
reed@google.comac10a2d2010-12-22 21:39:39 +0000442}
443
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000444// call this every draw call, to ensure that the context reflects our state,
reed@google.comac10a2d2010-12-22 21:39:39 +0000445// and not the state from some other canvas/device
446void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000447 GrAssert(NULL != fClipData.fClipStack);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000448
reed@google.comac10a2d2010-12-22 21:39:39 +0000449 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000450 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000451
452 fContext->setRenderTarget(fRenderTarget);
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000453 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000454
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000455 set_matrix_and_clip(fContext, *draw.fMatrix,
456 fClipData, *draw.fClip, this->getOrigin(),
457 fRenderTarget->width(), fRenderTarget->height());
reed@google.comac10a2d2010-12-22 21:39:39 +0000458 fNeedPrepareRenderTarget = false;
459 }
460}
461
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000462void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
463 const SkClipStack& clipStack) {
464 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
465 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000466 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000467}
468
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000469void SkGpuDevice::gainFocus(const SkMatrix& matrix, const SkRegion& clip) {
470
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000471 GrAssert(NULL != fClipData.fClipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000472
reed@google.comac10a2d2010-12-22 21:39:39 +0000473 fContext->setRenderTarget(fRenderTarget);
474
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000475 this->INHERITED::gainFocus(matrix, clip);
reed@google.comac10a2d2010-12-22 21:39:39 +0000476
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000477 set_matrix_and_clip(fContext, matrix, fClipData, clip, this->getOrigin(),
478 fRenderTarget->width(), fRenderTarget->height());
reed@google.comac10a2d2010-12-22 21:39:39 +0000479
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000480 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000481}
482
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000483SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000484 DO_DEFERRED_CLEAR;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000485 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000486}
487
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000488bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000489 if (NULL != fTexture) {
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000490 paint->textureSampler(kBitmapTextureIdx)->setCustomStage(
491 SkNEW_ARGS(GrSingleTextureEffect, (fTexture)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000492 return true;
493 }
494 return false;
495}
496
497///////////////////////////////////////////////////////////////////////////////
498
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000499SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
500SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
501SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
502SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
503SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
504 shader_type_mismatch);
rileya@google.com3e332582012-07-03 13:43:35 +0000505SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
506 shader_type_mismatch);
rileya@google.com22e57f92012-07-19 15:16:19 +0000507SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
508SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000509
bsalomon@google.com84405e02012-03-05 19:57:21 +0000510namespace {
511
512// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
513// justAlpha indicates that skPaint's alpha should be used rather than the color
514// Callers may subsequently modify the GrPaint. Setting constantColor indicates
515// that the final paint will draw the same color at every pixel. This allows
516// an optimization where the the color filter can be applied to the skPaint's
twiz@google.com58071162012-07-18 21:41:50 +0000517// color once while converting to GrPaint and then ignored.
518inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
519 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000520 bool justAlpha,
521 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000522 SkGpuDevice::SkAutoCachedTexture* act,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000523 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000524
525 grPaint->fDither = skPaint.isDither();
526 grPaint->fAntiAlias = skPaint.isAntiAlias();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000527 grPaint->fCoverage = 0xFF;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000528
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000529 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
530 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000531
532 SkXfermode* mode = skPaint.getXfermode();
533 if (mode) {
534 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000535 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000536#if 0
537 return false;
538#endif
539 }
540 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000541 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
542 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
543
bsalomon@google.com5782d712011-01-21 21:03:59 +0000544 if (justAlpha) {
545 uint8_t alpha = skPaint.getAlpha();
546 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000547 // justAlpha is currently set to true only if there is a texture,
548 // so constantColor should not also be true.
549 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000550 } else {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000551 grPaint->fColor = SkColor2GrColor(skPaint.getColor());
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000552 GrAssert(!grPaint->isTextureStageEnabled(kShaderTextureIdx));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000553 }
Scroggo97c88c22011-05-11 14:05:25 +0000554 SkColorFilter* colorFilter = skPaint.getColorFilter();
555 SkColor color;
556 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000557 SkScalar matrix[20];
twiz@google.com58071162012-07-18 21:41:50 +0000558 SkBitmap colorTransformTable;
bsalomon@google.com0d944822012-08-16 15:06:57 +0000559 grPaint->resetColorFilter();
Scroggo97c88c22011-05-11 14:05:25 +0000560 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000561 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000562 if (!constantColor) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000563 grPaint->fColorFilterColor = SkColor2GrColor(color);
Scroggod757df22011-05-16 13:11:16 +0000564 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000565 } else {
566 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
rileya@google.com24f3ad12012-07-18 21:47:40 +0000567 grPaint->fColor = SkColor2GrColor(filtered);
Scroggod757df22011-05-16 13:11:16 +0000568 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000569 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
570 grPaint->fColorMatrixEnabled = true;
571 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
572 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
twiz@google.com58071162012-07-18 21:41:50 +0000573 } else if (colorFilter != NULL && colorFilter->asComponentTable(
574 &colorTransformTable)) {
575 grPaint->resetColorFilter();
576
577 GrSamplerState* colorSampler = grPaint->textureSampler(kColorFilterTextureIdx);
bsalomon@google.comb8670992012-07-25 21:27:09 +0000578 GrTexture* texture = act->set(dev, colorTransformTable, colorSampler->textureParams());
twiz@google.com58071162012-07-18 21:41:50 +0000579
bsalomon@google.comb8670992012-07-25 21:27:09 +0000580 colorSampler->reset();
bsalomon@google.comcbd0ad92012-07-20 15:09:31 +0000581 colorSampler->setCustomStage(SkNEW_ARGS(GrColorTableEffect, (texture)))->unref();
Scroggo97c88c22011-05-11 14:05:25 +0000582 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000583 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000584}
585
bsalomon@google.com84405e02012-03-05 19:57:21 +0000586// This function is similar to skPaint2GrPaintNoShader but also converts
587// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
588// be used is set on grPaint and returned in param act. constantColor has the
589// same meaning as in skPaint2GrPaintNoShader.
590inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
591 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000592 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000593 SkGpuDevice::SkAutoCachedTexture textures[GrPaint::kMaxTextures],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000594 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000595 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000596 if (NULL == shader) {
twiz@google.com58071162012-07-18 21:41:50 +0000597 return skPaint2GrPaintNoShader(dev,
598 skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000599 false,
600 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000601 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000602 grPaint);
twiz@google.com58071162012-07-18 21:41:50 +0000603 } else if (!skPaint2GrPaintNoShader(dev, skPaint, true, false,
604 &textures[kColorFilterTextureIdx], grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000605 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000606 }
607
rileya@google.com91f319c2012-07-25 17:18:31 +0000608 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
609 GrCustomStage* stage = shader->asNewCustomStage(dev->context(), sampler);
610
611 if (NULL != stage) {
612 sampler->setCustomStage(stage)->unref();
613 SkMatrix localM;
614 if (shader->getLocalMatrix(&localM)) {
615 SkMatrix inverse;
616 if (localM.invert(&inverse)) {
617 sampler->matrix()->preConcat(inverse);
618 }
619 }
620 return true;
621 }
622
reed@google.comac10a2d2010-12-22 21:39:39 +0000623 SkBitmap bitmap;
rileya@google.com91f319c2012-07-25 17:18:31 +0000624 SkMatrix* matrix = sampler->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000625 SkShader::TileMode tileModes[2];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000626 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
rileya@google.com91f319c2012-07-25 17:18:31 +0000627 tileModes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000628
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000629 if (SkShader::kNone_BitmapType == bmptype) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000630 SkShader::GradientInfo info;
631 SkColor color;
632
633 info.fColors = &color;
634 info.fColorOffsets = NULL;
635 info.fColorCount = 1;
636 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
637 SkPaint copy(skPaint);
638 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000639 // modulate the paint alpha by the shader's solid color alpha
640 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
641 copy.setColor(SkColorSetA(color, newA));
twiz@google.com58071162012-07-18 21:41:50 +0000642 return skPaint2GrPaintNoShader(dev,
643 copy,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000644 false,
645 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000646 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000647 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000648 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000649 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000650 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000651
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000652 // Must set wrap and filter on the sampler before requesting a texture.
bsalomon@google.comb8670992012-07-25 21:27:09 +0000653 sampler->textureParams()->reset(tileModes, skPaint.isFilterBitmap());
654 GrTexture* texture = textures[kShaderTextureIdx].set(dev, bitmap, sampler->textureParams());
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000655
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000656 if (NULL == texture) {
657 SkDebugf("Couldn't convert bitmap to texture.\n");
658 return false;
659 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000660
rileya@google.com91f319c2012-07-25 17:18:31 +0000661 sampler->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000662
reed@google.comac10a2d2010-12-22 21:39:39 +0000663 // since our texture coords will be in local space, we wack the texture
664 // matrix to map them back into 0...1 before we load it
665 SkMatrix localM;
666 if (shader->getLocalMatrix(&localM)) {
667 SkMatrix inverse;
668 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000669 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000670 }
671 }
672 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000673 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
674 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000675 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000676 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000677
678 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000679}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000680}
reed@google.comac10a2d2010-12-22 21:39:39 +0000681
682///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000683void SkGpuDevice::clear(SkColor color) {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000684 fContext->clear(NULL, color, fRenderTarget);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000685}
686
reed@google.comac10a2d2010-12-22 21:39:39 +0000687void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
688 CHECK_SHOULD_DRAW(draw);
689
bsalomon@google.com5782d712011-01-21 21:03:59 +0000690 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000691 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000692 if (!skPaint2GrPaintShader(this,
693 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000694 true,
twiz@google.com58071162012-07-18 21:41:50 +0000695 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000696 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000697 return;
698 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000699
700 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000701}
702
703// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000704static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000705 kPoints_GrPrimitiveType,
706 kLines_GrPrimitiveType,
707 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000708};
709
710void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000711 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000712 CHECK_SHOULD_DRAW(draw);
713
714 SkScalar width = paint.getStrokeWidth();
715 if (width < 0) {
716 return;
717 }
718
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000719 // we only handle hairlines and paints without path effects or mask filters,
720 // else we let the SkDraw call our drawPath()
721 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000722 draw.drawPoints(mode, count, pts, paint, true);
723 return;
724 }
725
bsalomon@google.com5782d712011-01-21 21:03:59 +0000726 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000727 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000728 if (!skPaint2GrPaintShader(this,
729 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000730 true,
twiz@google.com58071162012-07-18 21:41:50 +0000731 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000732 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000733 return;
734 }
735
bsalomon@google.com5782d712011-01-21 21:03:59 +0000736 fContext->drawVertices(grPaint,
737 gPointMode2PrimtiveType[mode],
738 count,
739 (GrPoint*)pts,
740 NULL,
741 NULL,
742 NULL,
743 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000744}
745
reed@google.comc9aa5872011-04-05 21:05:37 +0000746///////////////////////////////////////////////////////////////////////////////
747
reed@google.comac10a2d2010-12-22 21:39:39 +0000748void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
749 const SkPaint& paint) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000750 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000751 CHECK_SHOULD_DRAW(draw);
752
bungeman@google.com79bd8772011-07-18 15:34:08 +0000753 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000754 SkScalar width = paint.getStrokeWidth();
755
756 /*
757 We have special code for hairline strokes, miter-strokes, and fills.
758 Anything else we just call our path code.
759 */
760 bool usePath = doStroke && width > 0 &&
761 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000762 // another two reasons we might need to call drawPath...
763 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000764 usePath = true;
765 }
reed@google.com67db6642011-05-26 11:46:35 +0000766 // until we aa rotated rects...
767 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
768 usePath = true;
769 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000770 // small miter limit means right angles show bevel...
771 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
772 paint.getStrokeMiter() < SK_ScalarSqrt2)
773 {
774 usePath = true;
775 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000776 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000777 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
778 usePath = true;
779 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000780
781 if (usePath) {
782 SkPath path;
783 path.addRect(rect);
784 this->drawPath(draw, path, paint, NULL, true);
785 return;
786 }
787
788 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000789 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000790 if (!skPaint2GrPaintShader(this,
791 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000792 true,
twiz@google.com58071162012-07-18 21:41:50 +0000793 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000794 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000795 return;
796 }
reed@google.com20efde72011-05-09 17:00:02 +0000797 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000798}
799
reed@google.com69302852011-02-16 18:08:07 +0000800#include "SkMaskFilter.h"
801#include "SkBounder.h"
802
bsalomon@google.com85003222012-03-28 14:44:37 +0000803///////////////////////////////////////////////////////////////////////////////
804
805// helpers for applying mask filters
806namespace {
807
808GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000809 switch (fillType) {
810 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000811 return kWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000812 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000813 return kEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000814 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000815 return kInverseWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000816 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000817 return kInverseEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000818 default:
819 SkDebugf("Unsupported path fill type\n");
bsalomon@google.com47059542012-06-06 20:51:20 +0000820 return kHairLine_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000821 }
822}
823
bsalomon@google.com85003222012-03-28 14:44:37 +0000824// We prefer to blur small rect with small radius via CPU.
825#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
826#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
827inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
828 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
829 rect.height() <= MIN_GPU_BLUR_SIZE &&
830 radius <= MIN_GPU_BLUR_RADIUS) {
831 return true;
832 }
833 return false;
834}
835
836bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
837 SkMaskFilter* filter, const SkMatrix& matrix,
838 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000839 GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000840#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000841 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000842#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000843 SkMaskFilter::BlurInfo info;
844 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000845 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000846 return false;
847 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000848 SkScalar radius = info.fIgnoreTransform ? info.fRadius
849 : matrix.mapRadius(info.fRadius);
850 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000851 if (radius <= 0) {
852 return false;
853 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000854
855 SkRect srcRect = path.getBounds();
856 if (shouldDrawBlurWithCPU(srcRect, radius)) {
857 return false;
858 }
859
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000860 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000861 float sigma3 = sigma * 3.0f;
862
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000863 SkRect clipRect;
864 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000865
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000866 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000867 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
868 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000869 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000870 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000871 SkIRect finalIRect;
872 finalRect.roundOut(&finalIRect);
873 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000874 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000875 }
876 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000877 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000878 }
879 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000880 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000881 GrTextureDesc desc;
882 desc.fFlags = kRenderTarget_GrTextureFlagBit;
883 desc.fWidth = SkScalarCeilToInt(srcRect.width());
884 desc.fHeight = SkScalarCeilToInt(srcRect.height());
885 // We actually only need A8, but it often isn't supported as a
886 // render target so default to RGBA_8888
bsalomon@google.com0342a852012-08-20 19:22:38 +0000887 desc.fConfig = kRGBA_8888_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000888
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000889 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
890 desc.fConfig = kAlpha_8_GrPixelConfig;
891 }
892
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000893 GrAutoScratchTexture pathEntry(context, desc);
894 GrTexture* pathTexture = pathEntry.texture();
895 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000896 return false;
897 }
898 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000899 // Once this code moves into GrContext, this should be changed to use
900 // an AutoClipRestore.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000901 const GrClipData* oldClipData = context->getClip();
902
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000903 context->setRenderTarget(pathTexture->asRenderTarget());
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000904
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000905 SkClipStack newClipStack(srcRect);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000906 GrClipData newClipData;
907 newClipData.fClipStack = &newClipStack;
908 context->setClip(&newClipData);
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000909
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000910 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000911 GrPaint tempPaint;
912 tempPaint.reset();
913
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000914 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000915 tempPaint.fAntiAlias = grp->fAntiAlias;
916 if (tempPaint.fAntiAlias) {
917 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
918 // blend coeff of zero requires dual source blending support in order
919 // to properly blend partially covered pixels. This means the AA
920 // code path may not be taken. So we use a dst blend coeff of ISA. We
921 // could special case AA draws to a dst surface with known alpha=0 to
922 // use a zero dst coeff when dual source blending isn't available.
bsalomon@google.com47059542012-06-06 20:51:20 +0000923 tempPaint.fSrcBlendCoeff = kOne_GrBlendCoeff;
924 tempPaint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000925 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000926 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000927 context->drawPath(tempPaint, path, pathFillType, &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000928
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000929 // If we're doing a normal blur, we can clobber the pathTexture in the
930 // gaussianBlur. Otherwise, we need to save it for later compositing.
931 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +0000932 SkAutoTUnref<GrTexture> blurTexture(context->gaussianBlur(
933 pathTexture, isNormalBlur, srcRect, sigma, sigma));
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000934
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000935 if (!isNormalBlur) {
936 GrPaint paint;
937 paint.reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +0000938 paint.textureSampler(0)->textureParams()->setClampNoFilter();
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000939 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
940 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000941 // Blend pathTexture over blurTexture.
942 context->setRenderTarget(blurTexture->asRenderTarget());
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000943 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS
944 (GrSingleTextureEffect, (pathTexture)))->unref();
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000945 if (SkMaskFilter::kInner_BlurType == blurType) {
946 // inner: dst = dst * src
bsalomon@google.com47059542012-06-06 20:51:20 +0000947 paint.fSrcBlendCoeff = kDC_GrBlendCoeff;
948 paint.fDstBlendCoeff = kZero_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000949 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
950 // solid: dst = src + dst - src * dst
951 // = (1 - dst) * src + 1 * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000952 paint.fSrcBlendCoeff = kIDC_GrBlendCoeff;
953 paint.fDstBlendCoeff = kOne_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000954 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
955 // outer: dst = dst * (1 - src)
956 // = 0 * src + (1 - src) * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000957 paint.fSrcBlendCoeff = kZero_GrBlendCoeff;
958 paint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000959 }
960 context->drawRect(paint, srcRect);
961 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000962 context->setRenderTarget(oldRenderTarget);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000963 context->setClip(oldClipData);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000964
bsalomon@google.come3d32162012-07-20 13:37:06 +0000965 if (!grp->preConcatSamplerMatricesWithInverse(matrix)) {
966 return false;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000967 }
968
969 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
970 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000971 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
bsalomon@google.com97912912011-12-06 16:30:36 +0000972 grp->maskSampler(MASK_IDX)->reset();
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000973 grp->maskSampler(MASK_IDX)->setCustomStage(
974 SkNEW_ARGS(GrSingleTextureEffect, (blurTexture)))->unref();
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000975 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
976 -finalRect.fTop);
977 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
978 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000979 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000980 return true;
981}
982
bsalomon@google.com85003222012-03-28 14:44:37 +0000983bool drawWithMaskFilter(GrContext* context, const SkPath& path,
984 SkMaskFilter* filter, const SkMatrix& matrix,
985 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000986 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000987 SkMask srcM, dstM;
988
989 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000990 SkMask::kComputeBoundsAndRenderImage_CreateMode,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000991 style)) {
reed@google.com69302852011-02-16 18:08:07 +0000992 return false;
993 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000994 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000995
996 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
997 return false;
998 }
999 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +00001000 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +00001001
1002 if (clip.quickReject(dstM.fBounds)) {
1003 return false;
1004 }
1005 if (bounder && !bounder->doIRect(dstM.fBounds)) {
1006 return false;
1007 }
1008
1009 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
1010 // the current clip (and identity matrix) and grpaint settings
1011
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001012 GrContext::AutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +00001013
bsalomon@google.come3d32162012-07-20 13:37:06 +00001014 if (!grp->preConcatSamplerMatricesWithInverse(matrix)) {
1015 return false;
1016 }
1017
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001018 GrTextureDesc desc;
1019 desc.fWidth = dstM.fBounds.width();
1020 desc.fHeight = dstM.fBounds.height();
1021 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +00001022
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001023 GrAutoScratchTexture ast(context, desc);
1024 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001025
reed@google.com69302852011-02-16 18:08:07 +00001026 if (NULL == texture) {
1027 return false;
1028 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001029 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001030 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +00001031
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001032 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
1033 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001034 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
bsalomon@google.com97912912011-12-06 16:30:36 +00001035 grp->maskSampler(MASK_IDX)->reset();
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001036 grp->maskSampler(MASK_IDX)->setCustomStage(
1037 SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001038 GrRect d;
1039 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +00001040 GrIntToScalar(dstM.fBounds.fTop),
1041 GrIntToScalar(dstM.fBounds.fRight),
1042 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001043
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001044 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
1045 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
1046 -dstM.fBounds.fTop*SK_Scalar1);
1047 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001048 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +00001049 return true;
1050}
reed@google.com69302852011-02-16 18:08:07 +00001051
bsalomon@google.com85003222012-03-28 14:44:37 +00001052}
1053
1054///////////////////////////////////////////////////////////////////////////////
1055
reed@google.com0c219b62011-02-16 21:31:18 +00001056void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001057 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +00001058 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +00001059 CHECK_FOR_NODRAW_ANNOTATION(paint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001060 CHECK_SHOULD_DRAW(draw);
1061
reed@google.comfe626382011-09-21 13:50:35 +00001062 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001063
bsalomon@google.com5782d712011-01-21 21:03:59 +00001064 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001065 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001066 if (!skPaint2GrPaintShader(this,
1067 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001068 true,
twiz@google.com58071162012-07-18 21:41:50 +00001069 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001070 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001071 return;
1072 }
1073
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +00001074 // can we cheat, and threat a thin stroke as a hairline w/ coverage
1075 // if we can, we draw lots faster (raster device does this same test)
1076 SkScalar hairlineCoverage;
1077 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
1078 doFill = false;
1079 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
1080 grPaint.fCoverage);
1081 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001082
reed@google.comfe626382011-09-21 13:50:35 +00001083 // If we have a prematrix, apply it to the path, optimizing for the case
1084 // where the original path can in fact be modified in place (even though
1085 // its parameter type is const).
1086 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1087 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001088
1089 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001090 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001091
reed@google.come3445642011-02-16 23:20:39 +00001092 if (!pathIsMutable) {
1093 result = &tmpPath;
1094 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001095 }
reed@google.come3445642011-02-16 23:20:39 +00001096 // should I push prePathMatrix on our MV stack temporarily, instead
1097 // of applying it here? See SkDraw.cpp
1098 pathPtr->transform(*prePathMatrix, result);
1099 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001100 }
reed@google.com0c219b62011-02-16 21:31:18 +00001101 // at this point we're done with prePathMatrix
1102 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001103
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001104 if (paint.getPathEffect() ||
1105 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001106 // it is safe to use tmpPath here, even if we already used it for the
1107 // prepathmatrix, since getFillPath can take the same object for its
1108 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001109 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001110 pathPtr = &tmpPath;
1111 }
1112
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001113 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001114 // avoid possibly allocating a new path in transform if we can
1115 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1116
1117 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001118 pathPtr->transform(*draw.fMatrix, devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001119 GrPathFill pathFillType = doFill ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001120 skToGrFillType(devPathPtr->getFillType()) : kHairLine_GrPathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001121 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001122 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001123 &grPaint, pathFillType)) {
1124 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
1125 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001126 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001127 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001128 &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001129 }
reed@google.com69302852011-02-16 18:08:07 +00001130 return;
1131 }
reed@google.com69302852011-02-16 18:08:07 +00001132
bsalomon@google.com47059542012-06-06 20:51:20 +00001133 GrPathFill fill = kHairLine_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001134
reed@google.com0c219b62011-02-16 21:31:18 +00001135 if (doFill) {
1136 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001137 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001138 fill = kWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001139 break;
1140 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001141 fill = kEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001142 break;
1143 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001144 fill = kInverseWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001145 break;
1146 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001147 fill = kInverseEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001148 break;
1149 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001150 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001151 return;
1152 }
1153 }
1154
reed@google.com07f3ee12011-05-16 17:21:57 +00001155 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001156}
1157
bsalomon@google.comfb309512011-11-30 14:13:48 +00001158namespace {
1159
1160inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1161 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1162 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1163 return tilesX * tilesY;
1164}
1165
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001166inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001167 const SkIRect* srcRectPtr,
1168 int maxTextureSize) {
1169 static const int kSmallTileSize = 1 << 10;
1170 if (maxTextureSize <= kSmallTileSize) {
1171 return maxTextureSize;
1172 }
1173
1174 size_t maxTexTotalTileSize;
1175 size_t smallTotalTileSize;
1176
1177 if (NULL == srcRectPtr) {
1178 int w = bitmap.width();
1179 int h = bitmap.height();
1180 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1181 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1182 } else {
1183 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1184 srcRectPtr->fTop,
1185 srcRectPtr->fRight,
1186 srcRectPtr->fBottom,
1187 maxTextureSize);
1188 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1189 srcRectPtr->fTop,
1190 srcRectPtr->fRight,
1191 srcRectPtr->fBottom,
1192 kSmallTileSize);
1193 }
1194 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1195 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1196
1197 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1198 return kSmallTileSize;
1199 } else {
1200 return maxTextureSize;
1201 }
1202}
1203}
1204
1205bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001206 const GrTextureParams& params,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001207 const SkIRect* srcRectPtr,
1208 int* tileSize) const {
1209 SkASSERT(NULL != tileSize);
1210
1211 // if bitmap is explictly texture backed then just use the texture
1212 if (NULL != bitmap.getTexture()) {
1213 return false;
1214 }
1215 // if it's larger than the max texture size, then we have no choice but
1216 // tiling
1217 const int maxTextureSize = fContext->getMaxTextureSize();
1218 if (bitmap.width() > maxTextureSize ||
1219 bitmap.height() > maxTextureSize) {
1220 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1221 return true;
1222 }
1223 // if we are going to have to draw the whole thing, then don't tile
1224 if (NULL == srcRectPtr) {
1225 return false;
1226 }
1227 // if the entire texture is already in our cache then no reason to tile it
bsalomon@google.comb8670992012-07-25 21:27:09 +00001228 if (this->isBitmapInTextureCache(bitmap, params)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001229 return false;
1230 }
1231
1232 // At this point we know we could do the draw by uploading the entire bitmap
1233 // as a texture. However, if the texture would be large compared to the
1234 // cache size and we don't require most of it for this draw then tile to
1235 // reduce the amount of upload and cache spill.
1236
1237 // assumption here is that sw bitmap size is a good proxy for its size as
1238 // a texture
1239 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001240 size_t cacheSize;
1241 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001242 if (bmpSize < cacheSize / 2) {
1243 return false;
1244 }
1245
1246 SkFixed fracUsed =
1247 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1248 (srcRectPtr->height() << 16) / bitmap.height());
1249 if (fracUsed <= SK_FixedHalf) {
1250 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1251 return true;
1252 } else {
1253 return false;
1254 }
1255}
1256
reed@google.comac10a2d2010-12-22 21:39:39 +00001257void SkGpuDevice::drawBitmap(const SkDraw& draw,
1258 const SkBitmap& bitmap,
1259 const SkIRect* srcRectPtr,
1260 const SkMatrix& m,
1261 const SkPaint& paint) {
1262 CHECK_SHOULD_DRAW(draw);
1263
1264 SkIRect srcRect;
1265 if (NULL == srcRectPtr) {
1266 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1267 } else {
1268 srcRect = *srcRectPtr;
1269 }
1270
junov@google.comd935cfb2011-06-27 20:48:23 +00001271 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001272 // Convert the bitmap to a shader so that the rect can be drawn
1273 // through drawRect, which supports mask filters.
1274 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001275 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001276 if (srcRectPtr) {
1277 if (!bitmap.extractSubset(&tmp, srcRect)) {
1278 return; // extraction failed
1279 }
1280 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001281 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001282 }
1283 SkPaint paintWithTexture(paint);
1284 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1285 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001286 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001287 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001288
junov@google.com1d329782011-07-28 20:10:09 +00001289 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001290 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001291 // also affect the behavior of the mask filter.
1292 SkMatrix drawMatrix;
1293 drawMatrix.setConcat(*draw.fMatrix, m);
1294 SkDraw transformedDraw(draw);
1295 transformedDraw.fMatrix = &drawMatrix;
1296
1297 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1298
junov@google.comd935cfb2011-06-27 20:48:23 +00001299 return;
1300 }
1301
bsalomon@google.com5782d712011-01-21 21:03:59 +00001302 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001303 SkAutoCachedTexture colorLutTexture;
1304 if (!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001305 return;
1306 }
bsalomon@google.comb8670992012-07-25 21:27:09 +00001307 GrTextureParams* params = grPaint.textureSampler(kBitmapTextureIdx)->textureParams();
1308 params->setBilerp(paint.isFilterBitmap());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001309
bsalomon@google.comfb309512011-11-30 14:13:48 +00001310 int tileSize;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001311 if (!this->shouldTileBitmap(bitmap, *params, srcRectPtr, &tileSize)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001312 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001313 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001314 return;
1315 }
1316
1317 // undo the translate done by SkCanvas
1318 int DX = SkMax32(0, srcRect.fLeft);
1319 int DY = SkMax32(0, srcRect.fTop);
1320 // compute clip bounds in local coordinates
1321 SkIRect clipRect;
1322 {
1323 SkRect r;
1324 r.set(draw.fClip->getBounds());
1325 SkMatrix matrix, inverse;
1326 matrix.setConcat(*draw.fMatrix, m);
1327 if (!matrix.invert(&inverse)) {
1328 return;
1329 }
1330 inverse.mapRect(&r);
1331 r.roundOut(&clipRect);
1332 // apply the canvas' translate to our local clip
1333 clipRect.offset(DX, DY);
1334 }
1335
bsalomon@google.comfb309512011-11-30 14:13:48 +00001336 int nx = bitmap.width() / tileSize;
1337 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001338 for (int x = 0; x <= nx; x++) {
1339 for (int y = 0; y <= ny; y++) {
1340 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001341 tileR.set(x * tileSize, y * tileSize,
1342 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001343 if (!SkIRect::Intersects(tileR, clipRect)) {
1344 continue;
1345 }
1346
1347 SkIRect srcR = tileR;
1348 if (!srcR.intersect(srcRect)) {
1349 continue;
1350 }
1351
1352 SkBitmap tmpB;
1353 if (bitmap.extractSubset(&tmpB, tileR)) {
1354 // now offset it to make it "local" to our tmp bitmap
1355 srcR.offset(-tileR.fLeft, -tileR.fTop);
1356
1357 SkMatrix tmpM(m);
1358 {
1359 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1360 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1361 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1362 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001363 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001364 }
1365 }
1366 }
1367}
1368
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001369namespace {
1370
1371bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1372 // detect pixel disalignment
1373 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1374 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1375 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1376 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1377 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1378 COLOR_BLEED_TOLERANCE &&
1379 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1380 COLOR_BLEED_TOLERANCE) {
1381 return true;
1382 }
1383 return false;
1384}
1385
1386bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1387 const SkMatrix& m) {
1388 // Only gets called if hasAlignedSamples returned false.
1389 // So we can assume that sampling is axis aligned but not texel aligned.
1390 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
1391 SkRect innerSrcRect(srcRect), innerTransformedRect,
1392 outerTransformedRect(transformedRect);
1393 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1394 m.mapRect(&innerTransformedRect, innerSrcRect);
1395
1396 // The gap between outerTransformedRect and innerTransformedRect
1397 // represents the projection of the source border area, which is
1398 // problematic for color bleeding. We must check whether any
1399 // destination pixels sample the border area.
1400 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1401 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1402 SkIRect outer, inner;
1403 outerTransformedRect.round(&outer);
1404 innerTransformedRect.round(&inner);
1405 // If the inner and outer rects round to the same result, it means the
1406 // border does not overlap any pixel centers. Yay!
1407 return inner != outer;
1408}
1409
1410} // unnamed namespace
1411
reed@google.comac10a2d2010-12-22 21:39:39 +00001412/*
1413 * This is called by drawBitmap(), which has to handle images that may be too
1414 * large to be represented by a single texture.
1415 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001416 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1417 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001418 */
1419void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1420 const SkBitmap& bitmap,
1421 const SkIRect& srcRect,
1422 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001423 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001424 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1425 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001426
reed@google.com9c49bc32011-07-07 13:42:37 +00001427 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001428 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001429 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001430 return;
1431 }
1432
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001433 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001434
bsalomon@google.comb8670992012-07-25 21:27:09 +00001435 sampler->textureParams()->setClamp();
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001436 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001437
1438 GrTexture* texture;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001439 SkAutoCachedTexture act(this, bitmap, sampler->textureParams(), &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001440 if (NULL == texture) {
1441 return;
1442 }
1443
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001444 grPaint->textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1445 (GrSingleTextureEffect, (texture)))->unref();
reed@google.com46799cd2011-02-22 20:56:26 +00001446
reed@google.com20efde72011-05-09 17:00:02 +00001447 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1448 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001449 GrRect paintRect;
bsalomon@google.com91832162012-03-08 19:53:02 +00001450 float wInv = 1.f / bitmap.width();
1451 float hInv = 1.f / bitmap.height();
1452 paintRect.setLTRB(SkFloatToScalar(srcRect.fLeft * wInv),
1453 SkFloatToScalar(srcRect.fTop * hInv),
1454 SkFloatToScalar(srcRect.fRight * wInv),
1455 SkFloatToScalar(srcRect.fBottom * hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001456
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001457 bool needsTextureDomain = false;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001458 if (sampler->textureParams()->isBilerp()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001459 // Need texture domain if drawing a sub rect.
bsalomon@google.comb8670992012-07-25 21:27:09 +00001460 needsTextureDomain = srcRect.width() < bitmap.width() || srcRect.height() < bitmap.height();
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001461 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1462 // sampling is axis-aligned
1463 GrRect floatSrcRect, transformedRect;
1464 floatSrcRect.set(srcRect);
1465 SkMatrix srcToDeviceMatrix(m);
1466 srcToDeviceMatrix.postConcat(*draw.fMatrix);
1467 srcToDeviceMatrix.mapRect(&transformedRect, floatSrcRect);
1468
1469 if (hasAlignedSamples(floatSrcRect, transformedRect)) {
1470 // Samples are texel-aligned, so filtering is futile
bsalomon@google.comb8670992012-07-25 21:27:09 +00001471 sampler->textureParams()->setBilerp(false);
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001472 needsTextureDomain = false;
1473 } else {
1474 needsTextureDomain = needsTextureDomain &&
1475 mayColorBleed(floatSrcRect, transformedRect, m);
1476 }
1477 }
1478 }
1479
1480 GrRect textureDomain = GrRect::MakeEmpty();
1481
1482 if (needsTextureDomain) {
1483 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001484 GrScalar left, top, right, bottom;
1485 if (srcRect.width() > 1) {
1486 GrScalar border = GR_ScalarHalf / bitmap.width();
1487 left = paintRect.left() + border;
1488 right = paintRect.right() - border;
1489 } else {
1490 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1491 }
1492 if (srcRect.height() > 1) {
1493 GrScalar border = GR_ScalarHalf / bitmap.height();
1494 top = paintRect.top() + border;
1495 bottom = paintRect.bottom() - border;
1496 } else {
1497 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1498 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001499 textureDomain.setLTRB(left, top, right, bottom);
tomhudson@google.com2f68e762012-07-17 18:43:21 +00001500 sampler->setCustomStage(SkNEW_ARGS(GrTextureDomainEffect,
1501 (texture,
1502 textureDomain)))->unref();
junov@google.com6acc9b32011-05-16 18:32:07 +00001503 }
1504
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001505 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001506}
1507
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001508namespace {
1509
1510void apply_custom_stage(GrContext* context,
1511 GrTexture* srcTexture,
1512 GrTexture* dstTexture,
1513 const GrRect& rect,
1514 GrCustomStage* stage) {
1515 SkASSERT(srcTexture && srcTexture->getContext() == context);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001516 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001517 GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget());
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001518 GrContext::AutoClip acs(context, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001519
1520 GrMatrix sampleM;
1521 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
1522 GrPaint paint;
1523 paint.reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +00001524 paint.textureSampler(0)->textureParams()->setBilerp(true);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001525 paint.textureSampler(0)->reset(sampleM);
1526 paint.textureSampler(0)->setCustomStage(stage);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001527 context->drawRect(paint, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001528}
1529
1530};
1531
reed@google.com8926b162012-03-23 15:36:36 +00001532static GrTexture* filter_texture(GrContext* context, GrTexture* texture,
1533 SkImageFilter* filter, const GrRect& rect) {
1534 GrAssert(filter);
1535
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001536 GrTextureDesc desc;
1537 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1538 desc.fWidth = SkScalarCeilToInt(rect.width());
1539 desc.fHeight = SkScalarCeilToInt(rect.height());
bsalomon@google.com0342a852012-08-20 19:22:38 +00001540 desc.fConfig = kRGBA_8888_GrPixelConfig;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001541 GrCustomStage* stage;
reed@google.com8926b162012-03-23 15:36:36 +00001542
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001543 if (filter->canFilterImageGPU()) {
1544 texture = filter->onFilterImageGPU(texture, rect);
1545 } else if (filter->asNewCustomStage(&stage, texture)) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001546 GrAutoScratchTexture dst(context, desc);
1547 apply_custom_stage(context, texture, dst.texture(), rect, stage);
1548 texture = dst.detach();
1549 stage->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001550 }
1551 return texture;
1552}
1553
reed@google.comac10a2d2010-12-22 21:39:39 +00001554void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1555 int left, int top, const SkPaint& paint) {
1556 CHECK_SHOULD_DRAW(draw);
1557
reed@google.com8926b162012-03-23 15:36:36 +00001558 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001559 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1560 return;
1561 }
1562
reed@google.com76dd2772012-01-05 21:15:07 +00001563 int w = bitmap.width();
1564 int h = bitmap.height();
1565
bsalomon@google.com5782d712011-01-21 21:03:59 +00001566 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001567 SkAutoCachedTexture colorLutTexture;
1568 if(!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001569 return;
1570 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001571
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001572 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001573
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001574 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001575
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001576 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001577 sampler->reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +00001578 SkAutoCachedTexture act(this, bitmap, sampler->textureParams(), &texture);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001579 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1580 (GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001581
reed@google.com8926b162012-03-23 15:36:36 +00001582 SkImageFilter* filter = paint.getImageFilter();
1583 if (NULL != filter) {
1584 GrTexture* filteredTexture = filter_texture(fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001585 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001586 if (filteredTexture) {
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001587 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1588 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001589 texture = filteredTexture;
1590 filteredTexture->unref();
1591 }
reed@google.com76dd2772012-01-05 21:15:07 +00001592 }
reed@google.com8926b162012-03-23 15:36:36 +00001593
bsalomon@google.com5782d712011-01-21 21:03:59 +00001594 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001595 GrRect::MakeXYWH(GrIntToScalar(left),
1596 GrIntToScalar(top),
1597 GrIntToScalar(w),
1598 GrIntToScalar(h)),
1599 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1600 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001601}
1602
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001603void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
reed@google.comac10a2d2010-12-22 21:39:39 +00001604 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001605 // clear of the source device must occur before CHECK_SHOULD_DRAW
1606 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1607 if (dev->fNeedClear) {
1608 // TODO: could check here whether we really need to draw at all
1609 dev->clear(0x0);
1610 }
1611
reed@google.comac10a2d2010-12-22 21:39:39 +00001612 CHECK_SHOULD_DRAW(draw);
1613
bsalomon@google.com5782d712011-01-21 21:03:59 +00001614 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001615 SkAutoCachedTexture colorLutTexture;
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001616 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001617 if (!dev->bindDeviceAsTexture(&grPaint) ||
twiz@google.com58071162012-07-18 21:41:50 +00001618 !skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001619 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001620 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001621
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001622 GrTexture* devTex = grPaint.getTextureSampler(kBitmapTextureIdx).getCustomStage()->texture(0);
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001623 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001624
reed@google.com8926b162012-03-23 15:36:36 +00001625 SkImageFilter* filter = paint.getImageFilter();
1626 if (NULL != filter) {
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001627 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001628 SkIntToScalar(devTex->height()));
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001629 GrTexture* filteredTexture = filter_texture(fContext, devTex, filter, rect);
reed@google.com8926b162012-03-23 15:36:36 +00001630 if (filteredTexture) {
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001631 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1632 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001633 devTex = filteredTexture;
1634 filteredTexture->unref();
1635 }
1636 }
1637
bsalomon@google.com5782d712011-01-21 21:03:59 +00001638 const SkBitmap& bm = dev->accessBitmap(false);
1639 int w = bm.width();
1640 int h = bm.height();
1641
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001642 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001643 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1644 GrIntToScalar(y),
1645 GrIntToScalar(w),
1646 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001647
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001648 // The device being drawn may not fill up its texture (saveLayer uses
1649 // the approximate ).
1650 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1651 GR_Scalar1 * h / devTex->height());
1652
1653 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001654}
1655
reed@google.com8926b162012-03-23 15:36:36 +00001656bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001657 if (!filter->asNewCustomStage(NULL, NULL) &&
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001658 !filter->canFilterImageGPU()) {
reed@google.com76dd2772012-01-05 21:15:07 +00001659 return false;
1660 }
reed@google.com8926b162012-03-23 15:36:36 +00001661 return true;
1662}
1663
1664bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1665 const SkMatrix& ctm,
1666 SkBitmap* result, SkIPoint* offset) {
1667 // want explicitly our impl, so guard against a subclass of us overriding it
1668 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001669 return false;
1670 }
reed@google.com8926b162012-03-23 15:36:36 +00001671
1672 SkAutoLockPixels alp(src, !src.getTexture());
1673 if (!src.getTexture() && !src.readyToDraw()) {
1674 return false;
1675 }
1676
1677 GrPaint paint;
1678 paint.reset();
1679
1680 GrSamplerState* sampler = paint.textureSampler(kBitmapTextureIdx);
1681
1682 GrTexture* texture;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001683 SkAutoCachedTexture act(this, src, sampler->textureParams(), &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001684
1685 result->setConfig(src.config(), src.width(), src.height());
robertphillips@google.com8637a362012-04-10 18:32:35 +00001686 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
1687 SkIntToScalar(src.height()));
reed@google.com8926b162012-03-23 15:36:36 +00001688 GrTexture* resultTexture = filter_texture(fContext, texture, filter, rect);
1689 if (resultTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001690 result->setPixelRef(SkNEW_ARGS(SkGrTexturePixelRef,
1691 (resultTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001692 resultTexture->unref();
1693 }
reed@google.com76dd2772012-01-05 21:15:07 +00001694 return true;
1695}
1696
reed@google.comac10a2d2010-12-22 21:39:39 +00001697///////////////////////////////////////////////////////////////////////////////
1698
1699// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001700static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001701 kTriangles_GrPrimitiveType,
1702 kTriangleStrip_GrPrimitiveType,
1703 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001704};
1705
1706void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1707 int vertexCount, const SkPoint vertices[],
1708 const SkPoint texs[], const SkColor colors[],
1709 SkXfermode* xmode,
1710 const uint16_t indices[], int indexCount,
1711 const SkPaint& paint) {
1712 CHECK_SHOULD_DRAW(draw);
1713
bsalomon@google.com5782d712011-01-21 21:03:59 +00001714 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001715 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com5782d712011-01-21 21:03:59 +00001716 // we ignore the shader if texs is null.
1717 if (NULL == texs) {
twiz@google.com58071162012-07-18 21:41:50 +00001718 if (!skPaint2GrPaintNoShader(this,
1719 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001720 false,
1721 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001722 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +00001723 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001724 return;
1725 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001726 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001727 if (!skPaint2GrPaintShader(this,
1728 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001729 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001730 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001731 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001732 return;
1733 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001734 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001735
1736 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001737 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001738 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1739#if 0
1740 return
1741#endif
1742 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001743 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001744
bsalomon@google.com498776a2011-08-16 19:20:44 +00001745 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1746 if (NULL != colors) {
1747 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001748 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001749 for (int i = 0; i < vertexCount; ++i) {
rileya@google.com24f3ad12012-07-18 21:47:40 +00001750 convertedColors[i] = SkColor2GrColor(colors[i]);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001751 }
1752 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001753 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001754 fContext->drawVertices(grPaint,
1755 gVertexMode2PrimitiveType[vmode],
1756 vertexCount,
1757 (GrPoint*) vertices,
1758 (GrPoint*) texs,
1759 colors,
1760 indices,
1761 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001762}
1763
1764///////////////////////////////////////////////////////////////////////////////
1765
1766static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001767 GrFontScaler* scaler = (GrFontScaler*)data;
1768 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001769}
1770
1771static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1772 void* auxData;
1773 GrFontScaler* scaler = NULL;
1774 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1775 scaler = (GrFontScaler*)auxData;
1776 }
1777 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001778 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001779 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1780 }
1781 return scaler;
1782}
1783
1784static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1785 SkFixed fx, SkFixed fy,
1786 const SkGlyph& glyph) {
1787 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1788
bungeman@google.com15865a72012-01-11 16:28:04 +00001789 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001790
1791 if (NULL == procs->fFontScaler) {
1792 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1793 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001794
bungeman@google.com15865a72012-01-11 16:28:04 +00001795 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1796 glyph.getSubXFixed(),
1797 glyph.getSubYFixed()),
1798 SkFixedFloorToFixed(fx),
1799 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001800 procs->fFontScaler);
1801}
1802
bsalomon@google.com5782d712011-01-21 21:03:59 +00001803SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001804
1805 // deferred allocation
1806 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001807 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001808 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1809 fDrawProcs->fContext = fContext;
1810 }
1811
1812 // init our (and GL's) state
1813 fDrawProcs->fTextContext = context;
1814 fDrawProcs->fFontScaler = NULL;
1815 return fDrawProcs;
1816}
1817
1818void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1819 size_t byteLength, SkScalar x, SkScalar y,
1820 const SkPaint& paint) {
1821 CHECK_SHOULD_DRAW(draw);
1822
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001823 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001824 // this guy will just call our drawPath()
1825 draw.drawText((const char*)text, byteLength, x, y, paint);
1826 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001827 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001828
1829 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001830 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001831 if (!skPaint2GrPaintShader(this,
1832 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001833 true,
twiz@google.com58071162012-07-18 21:41:50 +00001834 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001835 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001836 return;
1837 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001838 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1839 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001840 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1841 }
1842}
1843
1844void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1845 size_t byteLength, const SkScalar pos[],
1846 SkScalar constY, int scalarsPerPos,
1847 const SkPaint& paint) {
1848 CHECK_SHOULD_DRAW(draw);
1849
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001850 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001851 // this guy will just call our drawPath()
1852 draw.drawPosText((const char*)text, byteLength, pos, constY,
1853 scalarsPerPos, paint);
1854 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001855 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001856
1857 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001858 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001859 if (!skPaint2GrPaintShader(this,
1860 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001861 true,
twiz@google.com58071162012-07-18 21:41:50 +00001862 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001863 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001864 return;
1865 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001866 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1867 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001868 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1869 scalarsPerPos, paint);
1870 }
1871}
1872
1873void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1874 size_t len, const SkPath& path,
1875 const SkMatrix* m, const SkPaint& paint) {
1876 CHECK_SHOULD_DRAW(draw);
1877
1878 SkASSERT(draw.fDevice == this);
1879 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1880}
1881
1882///////////////////////////////////////////////////////////////////////////////
1883
reed@google.comf67e4cf2011-03-15 20:56:58 +00001884bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1885 if (!paint.isLCDRenderText()) {
1886 // we're cool with the paint as is
1887 return false;
1888 }
1889
1890 if (paint.getShader() ||
1891 paint.getXfermode() || // unless its srcover
1892 paint.getMaskFilter() ||
1893 paint.getRasterizer() ||
1894 paint.getColorFilter() ||
1895 paint.getPathEffect() ||
1896 paint.isFakeBoldText() ||
1897 paint.getStyle() != SkPaint::kFill_Style) {
1898 // turn off lcd
1899 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1900 flags->fHinting = paint.getHinting();
1901 return true;
1902 }
1903 // we're cool with the paint as is
1904 return false;
1905}
1906
reed@google.com75d939b2011-12-07 15:07:23 +00001907void SkGpuDevice::flush() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001908 DO_DEFERRED_CLEAR;
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001909 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001910}
1911
reed@google.comf67e4cf2011-03-15 20:56:58 +00001912///////////////////////////////////////////////////////////////////////////////
1913
bsalomon@google.comfb309512011-11-30 14:13:48 +00001914bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001915 const GrTextureParams& params) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001916 uint64_t key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001917 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001918
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001919 GrTextureDesc desc;
1920 desc.fWidth = bitmap.width();
1921 desc.fHeight = bitmap.height();
rileya@google.com24f3ad12012-07-18 21:47:40 +00001922 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config());
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001923
robertphillips@google.com9c2ea842012-08-13 17:47:59 +00001924 GrCacheData cacheData(key);
1925
1926 return this->context()->isTextureInCache(desc, cacheData, &params);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001927}
1928
1929
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001930SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1931 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001932 bool isOpaque,
1933 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001934 GrTextureDesc desc;
1935 desc.fConfig = fRenderTarget->config();
1936 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1937 desc.fWidth = width;
1938 desc.fHeight = height;
1939 desc.fSampleCnt = fRenderTarget->numSamples();
1940
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001941 GrTexture* texture;
1942 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001943 // Skia's convention is to only clear a device if it is non-opaque.
1944 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001945
1946#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1947 // layers are never draw in repeat modes, so we can request an approx
1948 // match and ignore any padding.
1949 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1950 GrContext::kApprox_ScratchTexMatch :
1951 GrContext::kExact_ScratchTexMatch;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001952 texture = fContext->lockScratchTexture(desc, matchType);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001953#else
1954 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1955 texture = tunref.get();
1956#endif
1957 if (texture) {
1958 return SkNEW_ARGS(SkGpuDevice,(fContext,
1959 texture,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001960 needClear));
1961 } else {
1962 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1963 width, height);
1964 return NULL;
1965 }
1966}
1967
1968SkGpuDevice::SkGpuDevice(GrContext* context,
1969 GrTexture* texture,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001970 bool needClear)
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001971 : SkDevice(make_bitmap(context, texture->asRenderTarget())) {
1972
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001973 GrAssert(texture && texture->asRenderTarget());
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001974 this->initFromRenderTarget(context, texture->asRenderTarget());
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001975 fCached = true;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001976 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001977}