blob: f7e5152482fbe41a246d94d643e8bdef82e18ef1 [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);
reed@google.comac10a2d2010-12-22 21:39:39 +0000287 fNeedPrepareRenderTarget = true;
288}
289
290///////////////////////////////////////////////////////////////////////////////
291
bsalomon@google.comc4364992011-11-07 15:54:49 +0000292namespace {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000293GrPixelConfig config8888_to_grconfig_and_flags(SkCanvas::Config8888 config8888, uint32_t* flags) {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000294 switch (config8888) {
295 case SkCanvas::kNative_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000296 *flags = 0;
297 return kSkia8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000298 case SkCanvas::kNative_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000299 *flags = GrContext::kUnpremul_PixelOpsFlag;
300 return kSkia8888_PM_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000301 case SkCanvas::kBGRA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000302 *flags = 0;
303 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000304 case SkCanvas::kBGRA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000305 *flags = GrContext::kUnpremul_PixelOpsFlag;
306 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000307 case SkCanvas::kRGBA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000308 *flags = 0;
309 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000310 case SkCanvas::kRGBA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000311 *flags = GrContext::kUnpremul_PixelOpsFlag;
312 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000313 default:
314 GrCrash("Unexpected Config8888.");
315 return kSkia8888_PM_GrPixelConfig;
316 }
317}
318}
319
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000320bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
321 int x, int y,
322 SkCanvas::Config8888 config8888) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000323 DO_DEFERRED_CLEAR;
bsalomon@google.com910267d2011-11-02 20:06:25 +0000324 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
325 SkASSERT(!bitmap.isNull());
326 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000327
bsalomon@google.com910267d2011-11-02 20:06:25 +0000328 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000329 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000330 uint32_t flags;
331 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000332 return fContext->readRenderTargetPixels(fRenderTarget,
333 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000334 bitmap.width(),
335 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000336 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000337 bitmap.getPixels(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000338 bitmap.rowBytes(),
339 flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000340}
341
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000342void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
343 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000344 SkAutoLockPixels alp(bitmap);
345 if (!bitmap.readyToDraw()) {
346 return;
347 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000348
349 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000350 uint32_t flags;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000351 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000352 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000353 } else {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000354 flags = 0;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000355 config= SkBitmapConfig2GrPixelConfig(bitmap.config());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000356 }
357
bsalomon@google.com6f379512011-11-16 20:36:03 +0000358 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000359 config, bitmap.getPixels(), bitmap.rowBytes(), flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000360}
361
robertphillips@google.com46f93502012-08-07 15:38:08 +0000362namespace {
363void purgeClipCB(int genID, void* data) {
364 GrContext* context = (GrContext*) data;
365
366 if (SkClipStack::kInvalidGenID == genID ||
367 SkClipStack::kEmptyGenID == genID ||
368 SkClipStack::kWideOpenGenID == genID) {
369 // none of these cases will have a cached clip mask
370 return;
371 }
372
373}
374};
375
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000376void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
377 INHERITED::onAttachToCanvas(canvas);
378
379 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000380 fClipData.fClipStack = canvas->getClipStack();
robertphillips@google.com46f93502012-08-07 15:38:08 +0000381
382 fClipData.fClipStack->addPurgeClipCallback(purgeClipCB, fContext);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000383}
384
385void SkGpuDevice::onDetachFromCanvas() {
386 INHERITED::onDetachFromCanvas();
387
robertphillips@google.com46f93502012-08-07 15:38:08 +0000388 // TODO: iterate through the clip stack and clean up any cached clip masks
389 fClipData.fClipStack->removePurgeClipCallback(purgeClipCB, fContext);
390
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000391 fClipData.fClipStack = NULL;
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000392}
393
robertphillips@google.com607fe072012-07-24 13:54:00 +0000394#ifdef SK_DEBUG
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000395static void check_bounds(const GrClipData& clipData,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000396 const SkRegion& clipRegion,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000397 int renderTargetWidth,
398 int renderTargetHeight) {
399
robertphillips@google.com7b112892012-07-31 15:18:21 +0000400 SkIRect devBound;
401
402 devBound.setLTRB(0, 0, renderTargetWidth, renderTargetHeight);
403
robertphillips@google.com607fe072012-07-24 13:54:00 +0000404 SkClipStack::BoundsType boundType;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000405 SkRect canvTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000406
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000407 clipData.fClipStack->getBounds(&canvTemp, &boundType);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000408 if (SkClipStack::kNormal_BoundsType == boundType) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000409 SkIRect devTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000410
robertphillips@google.com7b112892012-07-31 15:18:21 +0000411 canvTemp.roundOut(&devTemp);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000412
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000413 devTemp.offset(-clipData.fOrigin.fX, -clipData.fOrigin.fY);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000414
robertphillips@google.com7b112892012-07-31 15:18:21 +0000415 if (!devBound.intersect(devTemp)) {
416 devBound.setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000417 }
418 }
419
robertphillips@google.com768fee82012-08-02 12:42:43 +0000420 GrAssert(devBound.contains(clipRegion.getBounds()));
robertphillips@google.com607fe072012-07-24 13:54:00 +0000421}
422#endif
423
reed@google.comac10a2d2010-12-22 21:39:39 +0000424///////////////////////////////////////////////////////////////////////////////
425
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000426static void set_matrix_and_clip(GrContext* context, const SkMatrix& matrix,
427 GrClipData& clipData,
428 const SkRegion& clipRegion,
429 const SkIPoint& origin,
430 int renderTargetWidth, int renderTargetHeight) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000431 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000432
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000433 clipData.fOrigin = origin;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000434
435#ifdef SK_DEBUG
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000436 check_bounds(clipData, clipRegion,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000437 renderTargetWidth, renderTargetHeight);
438#endif
439
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000440 context->setClip(&clipData);
reed@google.comac10a2d2010-12-22 21:39:39 +0000441}
442
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000443// call this every draw call, to ensure that the context reflects our state,
reed@google.comac10a2d2010-12-22 21:39:39 +0000444// and not the state from some other canvas/device
445void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000446 GrAssert(NULL != fClipData.fClipStack);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000447
reed@google.comac10a2d2010-12-22 21:39:39 +0000448 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000449 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000450
451 fContext->setRenderTarget(fRenderTarget);
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000452 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000453
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000454 set_matrix_and_clip(fContext, *draw.fMatrix,
455 fClipData, *draw.fClip, this->getOrigin(),
456 fRenderTarget->width(), fRenderTarget->height());
reed@google.comac10a2d2010-12-22 21:39:39 +0000457 fNeedPrepareRenderTarget = false;
458 }
459}
460
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000461void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
462 const SkClipStack& clipStack) {
463 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
464 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000465 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000466}
467
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000468void SkGpuDevice::gainFocus(const SkMatrix& matrix, const SkRegion& clip) {
469
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000470 GrAssert(NULL != fClipData.fClipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000471
reed@google.comac10a2d2010-12-22 21:39:39 +0000472 fContext->setRenderTarget(fRenderTarget);
473
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000474 this->INHERITED::gainFocus(matrix, clip);
reed@google.comac10a2d2010-12-22 21:39:39 +0000475
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000476 set_matrix_and_clip(fContext, matrix, fClipData, clip, this->getOrigin(),
477 fRenderTarget->width(), fRenderTarget->height());
reed@google.comac10a2d2010-12-22 21:39:39 +0000478
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000479 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000480}
481
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000482SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000483 DO_DEFERRED_CLEAR;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000484 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000485}
486
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000487bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000488 if (NULL != fTexture) {
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000489 paint->textureSampler(kBitmapTextureIdx)->setCustomStage(
490 SkNEW_ARGS(GrSingleTextureEffect, (fTexture)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000491 return true;
492 }
493 return false;
494}
495
496///////////////////////////////////////////////////////////////////////////////
497
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000498SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
499SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
500SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
501SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
502SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
503 shader_type_mismatch);
rileya@google.com3e332582012-07-03 13:43:35 +0000504SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
505 shader_type_mismatch);
rileya@google.com22e57f92012-07-19 15:16:19 +0000506SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
507SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000508
bsalomon@google.com84405e02012-03-05 19:57:21 +0000509namespace {
510
511// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
512// justAlpha indicates that skPaint's alpha should be used rather than the color
513// Callers may subsequently modify the GrPaint. Setting constantColor indicates
514// that the final paint will draw the same color at every pixel. This allows
515// an optimization where the the color filter can be applied to the skPaint's
twiz@google.com58071162012-07-18 21:41:50 +0000516// color once while converting to GrPaint and then ignored.
517inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
518 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000519 bool justAlpha,
520 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000521 SkGpuDevice::SkAutoCachedTexture* act,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000522 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000523
524 grPaint->fDither = skPaint.isDither();
525 grPaint->fAntiAlias = skPaint.isAntiAlias();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000526 grPaint->fCoverage = 0xFF;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000527
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000528 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
529 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000530
531 SkXfermode* mode = skPaint.getXfermode();
532 if (mode) {
533 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000534 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000535#if 0
536 return false;
537#endif
538 }
539 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000540 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
541 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
542
bsalomon@google.com5782d712011-01-21 21:03:59 +0000543 if (justAlpha) {
544 uint8_t alpha = skPaint.getAlpha();
545 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000546 // justAlpha is currently set to true only if there is a texture,
547 // so constantColor should not also be true.
548 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000549 } else {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000550 grPaint->fColor = SkColor2GrColor(skPaint.getColor());
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000551 GrAssert(!grPaint->isTextureStageEnabled(kShaderTextureIdx));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000552 }
Scroggo97c88c22011-05-11 14:05:25 +0000553 SkColorFilter* colorFilter = skPaint.getColorFilter();
554 SkColor color;
555 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000556 SkScalar matrix[20];
twiz@google.com58071162012-07-18 21:41:50 +0000557 SkBitmap colorTransformTable;
bsalomon@google.com0d944822012-08-16 15:06:57 +0000558 grPaint->resetColorFilter();
Scroggo97c88c22011-05-11 14:05:25 +0000559 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000560 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000561 if (!constantColor) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000562 grPaint->fColorFilterColor = SkColor2GrColor(color);
Scroggod757df22011-05-16 13:11:16 +0000563 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000564 } else {
565 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
rileya@google.com24f3ad12012-07-18 21:47:40 +0000566 grPaint->fColor = SkColor2GrColor(filtered);
Scroggod757df22011-05-16 13:11:16 +0000567 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000568 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
569 grPaint->fColorMatrixEnabled = true;
570 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
571 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
twiz@google.com58071162012-07-18 21:41:50 +0000572 } else if (colorFilter != NULL && colorFilter->asComponentTable(
573 &colorTransformTable)) {
574 grPaint->resetColorFilter();
575
576 GrSamplerState* colorSampler = grPaint->textureSampler(kColorFilterTextureIdx);
bsalomon@google.comb8670992012-07-25 21:27:09 +0000577 GrTexture* texture = act->set(dev, colorTransformTable, colorSampler->textureParams());
twiz@google.com58071162012-07-18 21:41:50 +0000578
bsalomon@google.comb8670992012-07-25 21:27:09 +0000579 colorSampler->reset();
bsalomon@google.comcbd0ad92012-07-20 15:09:31 +0000580 colorSampler->setCustomStage(SkNEW_ARGS(GrColorTableEffect, (texture)))->unref();
Scroggo97c88c22011-05-11 14:05:25 +0000581 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000582 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000583}
584
bsalomon@google.com84405e02012-03-05 19:57:21 +0000585// This function is similar to skPaint2GrPaintNoShader but also converts
586// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
587// be used is set on grPaint and returned in param act. constantColor has the
588// same meaning as in skPaint2GrPaintNoShader.
589inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
590 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000591 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000592 SkGpuDevice::SkAutoCachedTexture textures[GrPaint::kMaxTextures],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000593 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000594 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000595 if (NULL == shader) {
twiz@google.com58071162012-07-18 21:41:50 +0000596 return skPaint2GrPaintNoShader(dev,
597 skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000598 false,
599 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000600 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000601 grPaint);
twiz@google.com58071162012-07-18 21:41:50 +0000602 } else if (!skPaint2GrPaintNoShader(dev, skPaint, true, false,
603 &textures[kColorFilterTextureIdx], grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000604 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000605 }
606
rileya@google.com91f319c2012-07-25 17:18:31 +0000607 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
608 GrCustomStage* stage = shader->asNewCustomStage(dev->context(), sampler);
609
610 if (NULL != stage) {
611 sampler->setCustomStage(stage)->unref();
612 SkMatrix localM;
613 if (shader->getLocalMatrix(&localM)) {
614 SkMatrix inverse;
615 if (localM.invert(&inverse)) {
616 sampler->matrix()->preConcat(inverse);
617 }
618 }
619 return true;
620 }
621
reed@google.comac10a2d2010-12-22 21:39:39 +0000622 SkBitmap bitmap;
rileya@google.com91f319c2012-07-25 17:18:31 +0000623 SkMatrix* matrix = sampler->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000624 SkShader::TileMode tileModes[2];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000625 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
rileya@google.com91f319c2012-07-25 17:18:31 +0000626 tileModes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000627
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000628 if (SkShader::kNone_BitmapType == bmptype) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000629 SkShader::GradientInfo info;
630 SkColor color;
631
632 info.fColors = &color;
633 info.fColorOffsets = NULL;
634 info.fColorCount = 1;
635 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
636 SkPaint copy(skPaint);
637 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000638 // modulate the paint alpha by the shader's solid color alpha
639 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
640 copy.setColor(SkColorSetA(color, newA));
twiz@google.com58071162012-07-18 21:41:50 +0000641 return skPaint2GrPaintNoShader(dev,
642 copy,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000643 false,
644 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000645 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000646 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000647 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000648 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000649 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000650
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000651 // Must set wrap and filter on the sampler before requesting a texture.
bsalomon@google.comb8670992012-07-25 21:27:09 +0000652 sampler->textureParams()->reset(tileModes, skPaint.isFilterBitmap());
653 GrTexture* texture = textures[kShaderTextureIdx].set(dev, bitmap, sampler->textureParams());
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000654
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000655 if (NULL == texture) {
656 SkDebugf("Couldn't convert bitmap to texture.\n");
657 return false;
658 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000659
rileya@google.com91f319c2012-07-25 17:18:31 +0000660 sampler->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000661
reed@google.comac10a2d2010-12-22 21:39:39 +0000662 // since our texture coords will be in local space, we wack the texture
663 // matrix to map them back into 0...1 before we load it
664 SkMatrix localM;
665 if (shader->getLocalMatrix(&localM)) {
666 SkMatrix inverse;
667 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000668 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000669 }
670 }
671 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000672 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
673 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000674 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000675 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000676
677 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000678}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000679}
reed@google.comac10a2d2010-12-22 21:39:39 +0000680
681///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000682void SkGpuDevice::clear(SkColor color) {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000683 fContext->clear(NULL, color, fRenderTarget);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000684}
685
reed@google.comac10a2d2010-12-22 21:39:39 +0000686void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
687 CHECK_SHOULD_DRAW(draw);
688
bsalomon@google.com5782d712011-01-21 21:03:59 +0000689 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000690 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000691 if (!skPaint2GrPaintShader(this,
692 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000693 true,
twiz@google.com58071162012-07-18 21:41:50 +0000694 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000695 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000696 return;
697 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000698
699 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000700}
701
702// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000703static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000704 kPoints_GrPrimitiveType,
705 kLines_GrPrimitiveType,
706 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000707};
708
709void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000710 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000711 CHECK_SHOULD_DRAW(draw);
712
713 SkScalar width = paint.getStrokeWidth();
714 if (width < 0) {
715 return;
716 }
717
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000718 // we only handle hairlines and paints without path effects or mask filters,
719 // else we let the SkDraw call our drawPath()
720 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000721 draw.drawPoints(mode, count, pts, paint, true);
722 return;
723 }
724
bsalomon@google.com5782d712011-01-21 21:03:59 +0000725 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000726 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000727 if (!skPaint2GrPaintShader(this,
728 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000729 true,
twiz@google.com58071162012-07-18 21:41:50 +0000730 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000731 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000732 return;
733 }
734
bsalomon@google.com5782d712011-01-21 21:03:59 +0000735 fContext->drawVertices(grPaint,
736 gPointMode2PrimtiveType[mode],
737 count,
738 (GrPoint*)pts,
739 NULL,
740 NULL,
741 NULL,
742 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000743}
744
reed@google.comc9aa5872011-04-05 21:05:37 +0000745///////////////////////////////////////////////////////////////////////////////
746
reed@google.comac10a2d2010-12-22 21:39:39 +0000747void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
748 const SkPaint& paint) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000749 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000750 CHECK_SHOULD_DRAW(draw);
751
bungeman@google.com79bd8772011-07-18 15:34:08 +0000752 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000753 SkScalar width = paint.getStrokeWidth();
754
755 /*
756 We have special code for hairline strokes, miter-strokes, and fills.
757 Anything else we just call our path code.
758 */
759 bool usePath = doStroke && width > 0 &&
760 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000761 // another two reasons we might need to call drawPath...
762 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000763 usePath = true;
764 }
reed@google.com67db6642011-05-26 11:46:35 +0000765 // until we aa rotated rects...
766 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
767 usePath = true;
768 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000769 // small miter limit means right angles show bevel...
770 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
771 paint.getStrokeMiter() < SK_ScalarSqrt2)
772 {
773 usePath = true;
774 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000775 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000776 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
777 usePath = true;
778 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000779
780 if (usePath) {
781 SkPath path;
782 path.addRect(rect);
783 this->drawPath(draw, path, paint, NULL, true);
784 return;
785 }
786
787 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000788 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000789 if (!skPaint2GrPaintShader(this,
790 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000791 true,
twiz@google.com58071162012-07-18 21:41:50 +0000792 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000793 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000794 return;
795 }
reed@google.com20efde72011-05-09 17:00:02 +0000796 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000797}
798
reed@google.com69302852011-02-16 18:08:07 +0000799#include "SkMaskFilter.h"
800#include "SkBounder.h"
801
bsalomon@google.com85003222012-03-28 14:44:37 +0000802///////////////////////////////////////////////////////////////////////////////
803
804// helpers for applying mask filters
805namespace {
806
807GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000808 switch (fillType) {
809 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000810 return kWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000811 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000812 return kEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000813 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000814 return kInverseWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000815 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000816 return kInverseEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000817 default:
818 SkDebugf("Unsupported path fill type\n");
bsalomon@google.com47059542012-06-06 20:51:20 +0000819 return kHairLine_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000820 }
821}
822
bsalomon@google.com85003222012-03-28 14:44:37 +0000823// We prefer to blur small rect with small radius via CPU.
824#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
825#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
826inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
827 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
828 rect.height() <= MIN_GPU_BLUR_SIZE &&
829 radius <= MIN_GPU_BLUR_RADIUS) {
830 return true;
831 }
832 return false;
833}
834
835bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
836 SkMaskFilter* filter, const SkMatrix& matrix,
837 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000838 GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000839#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000840 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000841#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000842 SkMaskFilter::BlurInfo info;
843 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000844 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000845 return false;
846 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000847 SkScalar radius = info.fIgnoreTransform ? info.fRadius
848 : matrix.mapRadius(info.fRadius);
849 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000850 if (radius <= 0) {
851 return false;
852 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000853
854 SkRect srcRect = path.getBounds();
855 if (shouldDrawBlurWithCPU(srcRect, radius)) {
856 return false;
857 }
858
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000859 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000860 float sigma3 = sigma * 3.0f;
861
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000862 SkRect clipRect;
863 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000864
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000865 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000866 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
867 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000868 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000869 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000870 SkIRect finalIRect;
871 finalRect.roundOut(&finalIRect);
872 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000873 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000874 }
875 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000876 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000877 }
878 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000879 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000880 GrTextureDesc desc;
881 desc.fFlags = kRenderTarget_GrTextureFlagBit;
882 desc.fWidth = SkScalarCeilToInt(srcRect.width());
883 desc.fHeight = SkScalarCeilToInt(srcRect.height());
884 // We actually only need A8, but it often isn't supported as a
885 // render target so default to RGBA_8888
bsalomon@google.com0342a852012-08-20 19:22:38 +0000886 desc.fConfig = kRGBA_8888_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000887
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000888 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
889 desc.fConfig = kAlpha_8_GrPixelConfig;
890 }
891
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000892 GrAutoScratchTexture pathEntry(context, desc);
893 GrTexture* pathTexture = pathEntry.texture();
894 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000895 return false;
896 }
897 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000898 // Once this code moves into GrContext, this should be changed to use
899 // an AutoClipRestore.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000900 const GrClipData* oldClipData = context->getClip();
901
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000902 context->setRenderTarget(pathTexture->asRenderTarget());
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000903
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000904 SkClipStack newClipStack(srcRect);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000905 GrClipData newClipData;
906 newClipData.fClipStack = &newClipStack;
907 context->setClip(&newClipData);
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000908
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000909 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000910 GrPaint tempPaint;
911 tempPaint.reset();
912
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000913 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000914 tempPaint.fAntiAlias = grp->fAntiAlias;
915 if (tempPaint.fAntiAlias) {
916 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
917 // blend coeff of zero requires dual source blending support in order
918 // to properly blend partially covered pixels. This means the AA
919 // code path may not be taken. So we use a dst blend coeff of ISA. We
920 // could special case AA draws to a dst surface with known alpha=0 to
921 // use a zero dst coeff when dual source blending isn't available.
bsalomon@google.com47059542012-06-06 20:51:20 +0000922 tempPaint.fSrcBlendCoeff = kOne_GrBlendCoeff;
923 tempPaint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000924 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000925 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000926 context->drawPath(tempPaint, path, pathFillType, &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000927
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000928 // If we're doing a normal blur, we can clobber the pathTexture in the
929 // gaussianBlur. Otherwise, we need to save it for later compositing.
930 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +0000931 SkAutoTUnref<GrTexture> blurTexture(context->gaussianBlur(
932 pathTexture, isNormalBlur, srcRect, sigma, sigma));
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000933
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000934 if (!isNormalBlur) {
935 GrPaint paint;
936 paint.reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +0000937 paint.textureSampler(0)->textureParams()->setClampNoFilter();
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000938 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
939 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000940 // Blend pathTexture over blurTexture.
941 context->setRenderTarget(blurTexture->asRenderTarget());
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000942 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS
943 (GrSingleTextureEffect, (pathTexture)))->unref();
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000944 if (SkMaskFilter::kInner_BlurType == blurType) {
945 // inner: dst = dst * src
bsalomon@google.com47059542012-06-06 20:51:20 +0000946 paint.fSrcBlendCoeff = kDC_GrBlendCoeff;
947 paint.fDstBlendCoeff = kZero_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000948 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
949 // solid: dst = src + dst - src * dst
950 // = (1 - dst) * src + 1 * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000951 paint.fSrcBlendCoeff = kIDC_GrBlendCoeff;
952 paint.fDstBlendCoeff = kOne_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000953 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
954 // outer: dst = dst * (1 - src)
955 // = 0 * src + (1 - src) * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000956 paint.fSrcBlendCoeff = kZero_GrBlendCoeff;
957 paint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000958 }
959 context->drawRect(paint, srcRect);
960 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000961 context->setRenderTarget(oldRenderTarget);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000962 context->setClip(oldClipData);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000963
bsalomon@google.come3d32162012-07-20 13:37:06 +0000964 if (!grp->preConcatSamplerMatricesWithInverse(matrix)) {
965 return false;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000966 }
967
968 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
969 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000970 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
bsalomon@google.com97912912011-12-06 16:30:36 +0000971 grp->maskSampler(MASK_IDX)->reset();
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000972 grp->maskSampler(MASK_IDX)->setCustomStage(
973 SkNEW_ARGS(GrSingleTextureEffect, (blurTexture)))->unref();
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000974 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
975 -finalRect.fTop);
976 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
977 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000978 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000979 return true;
980}
981
bsalomon@google.com85003222012-03-28 14:44:37 +0000982bool drawWithMaskFilter(GrContext* context, const SkPath& path,
983 SkMaskFilter* filter, const SkMatrix& matrix,
984 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000985 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000986 SkMask srcM, dstM;
987
988 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000989 SkMask::kComputeBoundsAndRenderImage_CreateMode,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000990 style)) {
reed@google.com69302852011-02-16 18:08:07 +0000991 return false;
992 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000993 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000994
995 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
996 return false;
997 }
998 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000999 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +00001000
1001 if (clip.quickReject(dstM.fBounds)) {
1002 return false;
1003 }
1004 if (bounder && !bounder->doIRect(dstM.fBounds)) {
1005 return false;
1006 }
1007
1008 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
1009 // the current clip (and identity matrix) and grpaint settings
1010
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001011 GrContext::AutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +00001012
bsalomon@google.come3d32162012-07-20 13:37:06 +00001013 if (!grp->preConcatSamplerMatricesWithInverse(matrix)) {
1014 return false;
1015 }
1016
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001017 GrTextureDesc desc;
1018 desc.fWidth = dstM.fBounds.width();
1019 desc.fHeight = dstM.fBounds.height();
1020 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +00001021
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001022 GrAutoScratchTexture ast(context, desc);
1023 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001024
reed@google.com69302852011-02-16 18:08:07 +00001025 if (NULL == texture) {
1026 return false;
1027 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001028 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001029 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +00001030
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001031 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
1032 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001033 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
bsalomon@google.com97912912011-12-06 16:30:36 +00001034 grp->maskSampler(MASK_IDX)->reset();
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001035 grp->maskSampler(MASK_IDX)->setCustomStage(
1036 SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001037 GrRect d;
1038 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +00001039 GrIntToScalar(dstM.fBounds.fTop),
1040 GrIntToScalar(dstM.fBounds.fRight),
1041 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001042
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001043 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
1044 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
1045 -dstM.fBounds.fTop*SK_Scalar1);
1046 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001047 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +00001048 return true;
1049}
reed@google.com69302852011-02-16 18:08:07 +00001050
bsalomon@google.com85003222012-03-28 14:44:37 +00001051}
1052
1053///////////////////////////////////////////////////////////////////////////////
1054
reed@google.com0c219b62011-02-16 21:31:18 +00001055void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001056 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +00001057 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +00001058 CHECK_FOR_NODRAW_ANNOTATION(paint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001059 CHECK_SHOULD_DRAW(draw);
1060
reed@google.comfe626382011-09-21 13:50:35 +00001061 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001062
bsalomon@google.com5782d712011-01-21 21:03:59 +00001063 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001064 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001065 if (!skPaint2GrPaintShader(this,
1066 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001067 true,
twiz@google.com58071162012-07-18 21:41:50 +00001068 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001069 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001070 return;
1071 }
1072
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +00001073 // can we cheat, and threat a thin stroke as a hairline w/ coverage
1074 // if we can, we draw lots faster (raster device does this same test)
1075 SkScalar hairlineCoverage;
1076 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
1077 doFill = false;
1078 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
1079 grPaint.fCoverage);
1080 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001081
reed@google.comfe626382011-09-21 13:50:35 +00001082 // If we have a prematrix, apply it to the path, optimizing for the case
1083 // where the original path can in fact be modified in place (even though
1084 // its parameter type is const).
1085 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1086 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001087
1088 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001089 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001090
reed@google.come3445642011-02-16 23:20:39 +00001091 if (!pathIsMutable) {
1092 result = &tmpPath;
1093 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001094 }
reed@google.come3445642011-02-16 23:20:39 +00001095 // should I push prePathMatrix on our MV stack temporarily, instead
1096 // of applying it here? See SkDraw.cpp
1097 pathPtr->transform(*prePathMatrix, result);
1098 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001099 }
reed@google.com0c219b62011-02-16 21:31:18 +00001100 // at this point we're done with prePathMatrix
1101 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001102
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001103 if (paint.getPathEffect() ||
1104 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001105 // it is safe to use tmpPath here, even if we already used it for the
1106 // prepathmatrix, since getFillPath can take the same object for its
1107 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001108 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001109 pathPtr = &tmpPath;
1110 }
1111
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001112 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001113 // avoid possibly allocating a new path in transform if we can
1114 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1115
1116 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001117 pathPtr->transform(*draw.fMatrix, devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001118 GrPathFill pathFillType = doFill ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001119 skToGrFillType(devPathPtr->getFillType()) : kHairLine_GrPathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001120 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001121 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001122 &grPaint, pathFillType)) {
1123 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
1124 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001125 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001126 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001127 &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001128 }
reed@google.com69302852011-02-16 18:08:07 +00001129 return;
1130 }
reed@google.com69302852011-02-16 18:08:07 +00001131
bsalomon@google.com47059542012-06-06 20:51:20 +00001132 GrPathFill fill = kHairLine_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001133
reed@google.com0c219b62011-02-16 21:31:18 +00001134 if (doFill) {
1135 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001136 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001137 fill = kWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001138 break;
1139 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001140 fill = kEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001141 break;
1142 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001143 fill = kInverseWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001144 break;
1145 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001146 fill = kInverseEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001147 break;
1148 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001149 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001150 return;
1151 }
1152 }
1153
reed@google.com07f3ee12011-05-16 17:21:57 +00001154 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001155}
1156
bsalomon@google.comfb309512011-11-30 14:13:48 +00001157namespace {
1158
1159inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1160 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1161 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1162 return tilesX * tilesY;
1163}
1164
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001165inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001166 const SkIRect* srcRectPtr,
1167 int maxTextureSize) {
1168 static const int kSmallTileSize = 1 << 10;
1169 if (maxTextureSize <= kSmallTileSize) {
1170 return maxTextureSize;
1171 }
1172
1173 size_t maxTexTotalTileSize;
1174 size_t smallTotalTileSize;
1175
1176 if (NULL == srcRectPtr) {
1177 int w = bitmap.width();
1178 int h = bitmap.height();
1179 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1180 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1181 } else {
1182 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1183 srcRectPtr->fTop,
1184 srcRectPtr->fRight,
1185 srcRectPtr->fBottom,
1186 maxTextureSize);
1187 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1188 srcRectPtr->fTop,
1189 srcRectPtr->fRight,
1190 srcRectPtr->fBottom,
1191 kSmallTileSize);
1192 }
1193 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1194 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1195
1196 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1197 return kSmallTileSize;
1198 } else {
1199 return maxTextureSize;
1200 }
1201}
1202}
1203
1204bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001205 const GrTextureParams& params,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001206 const SkIRect* srcRectPtr,
1207 int* tileSize) const {
1208 SkASSERT(NULL != tileSize);
1209
1210 // if bitmap is explictly texture backed then just use the texture
1211 if (NULL != bitmap.getTexture()) {
1212 return false;
1213 }
1214 // if it's larger than the max texture size, then we have no choice but
1215 // tiling
1216 const int maxTextureSize = fContext->getMaxTextureSize();
1217 if (bitmap.width() > maxTextureSize ||
1218 bitmap.height() > maxTextureSize) {
1219 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1220 return true;
1221 }
1222 // if we are going to have to draw the whole thing, then don't tile
1223 if (NULL == srcRectPtr) {
1224 return false;
1225 }
1226 // if the entire texture is already in our cache then no reason to tile it
bsalomon@google.comb8670992012-07-25 21:27:09 +00001227 if (this->isBitmapInTextureCache(bitmap, params)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001228 return false;
1229 }
1230
1231 // At this point we know we could do the draw by uploading the entire bitmap
1232 // as a texture. However, if the texture would be large compared to the
1233 // cache size and we don't require most of it for this draw then tile to
1234 // reduce the amount of upload and cache spill.
1235
1236 // assumption here is that sw bitmap size is a good proxy for its size as
1237 // a texture
1238 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001239 size_t cacheSize;
1240 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001241 if (bmpSize < cacheSize / 2) {
1242 return false;
1243 }
1244
1245 SkFixed fracUsed =
1246 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1247 (srcRectPtr->height() << 16) / bitmap.height());
1248 if (fracUsed <= SK_FixedHalf) {
1249 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1250 return true;
1251 } else {
1252 return false;
1253 }
1254}
1255
reed@google.comac10a2d2010-12-22 21:39:39 +00001256void SkGpuDevice::drawBitmap(const SkDraw& draw,
1257 const SkBitmap& bitmap,
1258 const SkIRect* srcRectPtr,
1259 const SkMatrix& m,
1260 const SkPaint& paint) {
1261 CHECK_SHOULD_DRAW(draw);
1262
1263 SkIRect srcRect;
1264 if (NULL == srcRectPtr) {
1265 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1266 } else {
1267 srcRect = *srcRectPtr;
1268 }
1269
junov@google.comd935cfb2011-06-27 20:48:23 +00001270 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001271 // Convert the bitmap to a shader so that the rect can be drawn
1272 // through drawRect, which supports mask filters.
1273 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001274 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001275 if (srcRectPtr) {
1276 if (!bitmap.extractSubset(&tmp, srcRect)) {
1277 return; // extraction failed
1278 }
1279 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001280 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001281 }
1282 SkPaint paintWithTexture(paint);
1283 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1284 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001285 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001286 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001287
junov@google.com1d329782011-07-28 20:10:09 +00001288 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001289 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001290 // also affect the behavior of the mask filter.
1291 SkMatrix drawMatrix;
1292 drawMatrix.setConcat(*draw.fMatrix, m);
1293 SkDraw transformedDraw(draw);
1294 transformedDraw.fMatrix = &drawMatrix;
1295
1296 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1297
junov@google.comd935cfb2011-06-27 20:48:23 +00001298 return;
1299 }
1300
bsalomon@google.com5782d712011-01-21 21:03:59 +00001301 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001302 SkAutoCachedTexture colorLutTexture;
1303 if (!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001304 return;
1305 }
bsalomon@google.comb8670992012-07-25 21:27:09 +00001306 GrTextureParams* params = grPaint.textureSampler(kBitmapTextureIdx)->textureParams();
1307 params->setBilerp(paint.isFilterBitmap());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001308
bsalomon@google.comfb309512011-11-30 14:13:48 +00001309 int tileSize;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001310 if (!this->shouldTileBitmap(bitmap, *params, srcRectPtr, &tileSize)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001311 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001312 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001313 return;
1314 }
1315
1316 // undo the translate done by SkCanvas
1317 int DX = SkMax32(0, srcRect.fLeft);
1318 int DY = SkMax32(0, srcRect.fTop);
1319 // compute clip bounds in local coordinates
1320 SkIRect clipRect;
1321 {
1322 SkRect r;
1323 r.set(draw.fClip->getBounds());
1324 SkMatrix matrix, inverse;
1325 matrix.setConcat(*draw.fMatrix, m);
1326 if (!matrix.invert(&inverse)) {
1327 return;
1328 }
1329 inverse.mapRect(&r);
1330 r.roundOut(&clipRect);
1331 // apply the canvas' translate to our local clip
1332 clipRect.offset(DX, DY);
1333 }
1334
bsalomon@google.comfb309512011-11-30 14:13:48 +00001335 int nx = bitmap.width() / tileSize;
1336 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001337 for (int x = 0; x <= nx; x++) {
1338 for (int y = 0; y <= ny; y++) {
1339 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001340 tileR.set(x * tileSize, y * tileSize,
1341 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001342 if (!SkIRect::Intersects(tileR, clipRect)) {
1343 continue;
1344 }
1345
1346 SkIRect srcR = tileR;
1347 if (!srcR.intersect(srcRect)) {
1348 continue;
1349 }
1350
1351 SkBitmap tmpB;
1352 if (bitmap.extractSubset(&tmpB, tileR)) {
1353 // now offset it to make it "local" to our tmp bitmap
1354 srcR.offset(-tileR.fLeft, -tileR.fTop);
1355
1356 SkMatrix tmpM(m);
1357 {
1358 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1359 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1360 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1361 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001362 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001363 }
1364 }
1365 }
1366}
1367
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001368namespace {
1369
1370bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1371 // detect pixel disalignment
1372 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1373 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1374 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1375 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1376 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1377 COLOR_BLEED_TOLERANCE &&
1378 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1379 COLOR_BLEED_TOLERANCE) {
1380 return true;
1381 }
1382 return false;
1383}
1384
1385bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1386 const SkMatrix& m) {
1387 // Only gets called if hasAlignedSamples returned false.
1388 // So we can assume that sampling is axis aligned but not texel aligned.
1389 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
1390 SkRect innerSrcRect(srcRect), innerTransformedRect,
1391 outerTransformedRect(transformedRect);
1392 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1393 m.mapRect(&innerTransformedRect, innerSrcRect);
1394
1395 // The gap between outerTransformedRect and innerTransformedRect
1396 // represents the projection of the source border area, which is
1397 // problematic for color bleeding. We must check whether any
1398 // destination pixels sample the border area.
1399 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1400 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1401 SkIRect outer, inner;
1402 outerTransformedRect.round(&outer);
1403 innerTransformedRect.round(&inner);
1404 // If the inner and outer rects round to the same result, it means the
1405 // border does not overlap any pixel centers. Yay!
1406 return inner != outer;
1407}
1408
1409} // unnamed namespace
1410
reed@google.comac10a2d2010-12-22 21:39:39 +00001411/*
1412 * This is called by drawBitmap(), which has to handle images that may be too
1413 * large to be represented by a single texture.
1414 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001415 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1416 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001417 */
1418void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1419 const SkBitmap& bitmap,
1420 const SkIRect& srcRect,
1421 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001422 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001423 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1424 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001425
reed@google.com9c49bc32011-07-07 13:42:37 +00001426 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001427 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001428 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001429 return;
1430 }
1431
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001432 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001433
bsalomon@google.comb8670992012-07-25 21:27:09 +00001434 sampler->textureParams()->setClamp();
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001435 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001436
1437 GrTexture* texture;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001438 SkAutoCachedTexture act(this, bitmap, sampler->textureParams(), &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001439 if (NULL == texture) {
1440 return;
1441 }
1442
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001443 grPaint->textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1444 (GrSingleTextureEffect, (texture)))->unref();
reed@google.com46799cd2011-02-22 20:56:26 +00001445
reed@google.com20efde72011-05-09 17:00:02 +00001446 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1447 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001448 GrRect paintRect;
bsalomon@google.com91832162012-03-08 19:53:02 +00001449 float wInv = 1.f / bitmap.width();
1450 float hInv = 1.f / bitmap.height();
1451 paintRect.setLTRB(SkFloatToScalar(srcRect.fLeft * wInv),
1452 SkFloatToScalar(srcRect.fTop * hInv),
1453 SkFloatToScalar(srcRect.fRight * wInv),
1454 SkFloatToScalar(srcRect.fBottom * hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001455
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001456 bool needsTextureDomain = false;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001457 if (sampler->textureParams()->isBilerp()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001458 // Need texture domain if drawing a sub rect.
bsalomon@google.comb8670992012-07-25 21:27:09 +00001459 needsTextureDomain = srcRect.width() < bitmap.width() || srcRect.height() < bitmap.height();
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001460 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1461 // sampling is axis-aligned
1462 GrRect floatSrcRect, transformedRect;
1463 floatSrcRect.set(srcRect);
1464 SkMatrix srcToDeviceMatrix(m);
1465 srcToDeviceMatrix.postConcat(*draw.fMatrix);
1466 srcToDeviceMatrix.mapRect(&transformedRect, floatSrcRect);
1467
1468 if (hasAlignedSamples(floatSrcRect, transformedRect)) {
1469 // Samples are texel-aligned, so filtering is futile
bsalomon@google.comb8670992012-07-25 21:27:09 +00001470 sampler->textureParams()->setBilerp(false);
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001471 needsTextureDomain = false;
1472 } else {
1473 needsTextureDomain = needsTextureDomain &&
1474 mayColorBleed(floatSrcRect, transformedRect, m);
1475 }
1476 }
1477 }
1478
1479 GrRect textureDomain = GrRect::MakeEmpty();
1480
1481 if (needsTextureDomain) {
1482 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001483 GrScalar left, top, right, bottom;
1484 if (srcRect.width() > 1) {
1485 GrScalar border = GR_ScalarHalf / bitmap.width();
1486 left = paintRect.left() + border;
1487 right = paintRect.right() - border;
1488 } else {
1489 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1490 }
1491 if (srcRect.height() > 1) {
1492 GrScalar border = GR_ScalarHalf / bitmap.height();
1493 top = paintRect.top() + border;
1494 bottom = paintRect.bottom() - border;
1495 } else {
1496 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1497 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001498 textureDomain.setLTRB(left, top, right, bottom);
tomhudson@google.com2f68e762012-07-17 18:43:21 +00001499 sampler->setCustomStage(SkNEW_ARGS(GrTextureDomainEffect,
1500 (texture,
1501 textureDomain)))->unref();
junov@google.com6acc9b32011-05-16 18:32:07 +00001502 }
1503
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001504 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001505}
1506
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001507namespace {
1508
1509void apply_custom_stage(GrContext* context,
1510 GrTexture* srcTexture,
1511 GrTexture* dstTexture,
1512 const GrRect& rect,
1513 GrCustomStage* stage) {
1514 SkASSERT(srcTexture && srcTexture->getContext() == context);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001515 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001516 GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget());
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001517 GrContext::AutoClip acs(context, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001518
1519 GrMatrix sampleM;
1520 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
1521 GrPaint paint;
1522 paint.reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +00001523 paint.textureSampler(0)->textureParams()->setBilerp(true);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001524 paint.textureSampler(0)->reset(sampleM);
1525 paint.textureSampler(0)->setCustomStage(stage);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001526 context->drawRect(paint, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001527}
1528
1529};
1530
reed@google.com8926b162012-03-23 15:36:36 +00001531static GrTexture* filter_texture(GrContext* context, GrTexture* texture,
1532 SkImageFilter* filter, const GrRect& rect) {
1533 GrAssert(filter);
1534
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001535 GrTextureDesc desc;
1536 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1537 desc.fWidth = SkScalarCeilToInt(rect.width());
1538 desc.fHeight = SkScalarCeilToInt(rect.height());
bsalomon@google.com0342a852012-08-20 19:22:38 +00001539 desc.fConfig = kRGBA_8888_GrPixelConfig;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001540 GrCustomStage* stage;
reed@google.com8926b162012-03-23 15:36:36 +00001541
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001542 if (filter->canFilterImageGPU()) {
1543 texture = filter->onFilterImageGPU(texture, rect);
1544 } else if (filter->asNewCustomStage(&stage, texture)) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001545 GrAutoScratchTexture dst(context, desc);
1546 apply_custom_stage(context, texture, dst.texture(), rect, stage);
1547 texture = dst.detach();
1548 stage->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001549 }
1550 return texture;
1551}
1552
reed@google.comac10a2d2010-12-22 21:39:39 +00001553void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1554 int left, int top, const SkPaint& paint) {
1555 CHECK_SHOULD_DRAW(draw);
1556
reed@google.com8926b162012-03-23 15:36:36 +00001557 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001558 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1559 return;
1560 }
1561
reed@google.com76dd2772012-01-05 21:15:07 +00001562 int w = bitmap.width();
1563 int h = bitmap.height();
1564
bsalomon@google.com5782d712011-01-21 21:03:59 +00001565 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001566 SkAutoCachedTexture colorLutTexture;
1567 if(!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001568 return;
1569 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001570
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001571 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001572
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001573 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001574
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001575 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001576 sampler->reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +00001577 SkAutoCachedTexture act(this, bitmap, sampler->textureParams(), &texture);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001578 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1579 (GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001580
reed@google.com8926b162012-03-23 15:36:36 +00001581 SkImageFilter* filter = paint.getImageFilter();
1582 if (NULL != filter) {
1583 GrTexture* filteredTexture = filter_texture(fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001584 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001585 if (filteredTexture) {
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001586 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1587 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001588 texture = filteredTexture;
1589 filteredTexture->unref();
1590 }
reed@google.com76dd2772012-01-05 21:15:07 +00001591 }
reed@google.com8926b162012-03-23 15:36:36 +00001592
bsalomon@google.com5782d712011-01-21 21:03:59 +00001593 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001594 GrRect::MakeXYWH(GrIntToScalar(left),
1595 GrIntToScalar(top),
1596 GrIntToScalar(w),
1597 GrIntToScalar(h)),
1598 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1599 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001600}
1601
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001602void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
reed@google.comac10a2d2010-12-22 21:39:39 +00001603 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001604 // clear of the source device must occur before CHECK_SHOULD_DRAW
1605 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1606 if (dev->fNeedClear) {
1607 // TODO: could check here whether we really need to draw at all
1608 dev->clear(0x0);
1609 }
1610
reed@google.comac10a2d2010-12-22 21:39:39 +00001611 CHECK_SHOULD_DRAW(draw);
1612
bsalomon@google.com5782d712011-01-21 21:03:59 +00001613 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001614 SkAutoCachedTexture colorLutTexture;
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001615 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001616 if (!dev->bindDeviceAsTexture(&grPaint) ||
twiz@google.com58071162012-07-18 21:41:50 +00001617 !skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001618 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001619 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001620
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001621 GrTexture* devTex = grPaint.getTextureSampler(kBitmapTextureIdx).getCustomStage()->texture(0);
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001622 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001623
reed@google.com8926b162012-03-23 15:36:36 +00001624 SkImageFilter* filter = paint.getImageFilter();
1625 if (NULL != filter) {
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001626 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001627 SkIntToScalar(devTex->height()));
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001628 GrTexture* filteredTexture = filter_texture(fContext, devTex, filter, rect);
reed@google.com8926b162012-03-23 15:36:36 +00001629 if (filteredTexture) {
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001630 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1631 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001632 devTex = filteredTexture;
1633 filteredTexture->unref();
1634 }
1635 }
1636
bsalomon@google.com5782d712011-01-21 21:03:59 +00001637 const SkBitmap& bm = dev->accessBitmap(false);
1638 int w = bm.width();
1639 int h = bm.height();
1640
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001641 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001642 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1643 GrIntToScalar(y),
1644 GrIntToScalar(w),
1645 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001646
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001647 // The device being drawn may not fill up its texture (saveLayer uses
1648 // the approximate ).
1649 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1650 GR_Scalar1 * h / devTex->height());
1651
1652 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001653}
1654
reed@google.com8926b162012-03-23 15:36:36 +00001655bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001656 if (!filter->asNewCustomStage(NULL, NULL) &&
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001657 !filter->canFilterImageGPU()) {
reed@google.com76dd2772012-01-05 21:15:07 +00001658 return false;
1659 }
reed@google.com8926b162012-03-23 15:36:36 +00001660 return true;
1661}
1662
1663bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1664 const SkMatrix& ctm,
1665 SkBitmap* result, SkIPoint* offset) {
1666 // want explicitly our impl, so guard against a subclass of us overriding it
1667 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001668 return false;
1669 }
reed@google.com8926b162012-03-23 15:36:36 +00001670
1671 SkAutoLockPixels alp(src, !src.getTexture());
1672 if (!src.getTexture() && !src.readyToDraw()) {
1673 return false;
1674 }
1675
1676 GrPaint paint;
1677 paint.reset();
1678
1679 GrSamplerState* sampler = paint.textureSampler(kBitmapTextureIdx);
1680
1681 GrTexture* texture;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001682 SkAutoCachedTexture act(this, src, sampler->textureParams(), &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001683
1684 result->setConfig(src.config(), src.width(), src.height());
robertphillips@google.com8637a362012-04-10 18:32:35 +00001685 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
1686 SkIntToScalar(src.height()));
reed@google.com8926b162012-03-23 15:36:36 +00001687 GrTexture* resultTexture = filter_texture(fContext, texture, filter, rect);
1688 if (resultTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001689 result->setPixelRef(SkNEW_ARGS(SkGrTexturePixelRef,
1690 (resultTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001691 resultTexture->unref();
1692 }
reed@google.com76dd2772012-01-05 21:15:07 +00001693 return true;
1694}
1695
reed@google.comac10a2d2010-12-22 21:39:39 +00001696///////////////////////////////////////////////////////////////////////////////
1697
1698// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001699static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001700 kTriangles_GrPrimitiveType,
1701 kTriangleStrip_GrPrimitiveType,
1702 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001703};
1704
1705void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1706 int vertexCount, const SkPoint vertices[],
1707 const SkPoint texs[], const SkColor colors[],
1708 SkXfermode* xmode,
1709 const uint16_t indices[], int indexCount,
1710 const SkPaint& paint) {
1711 CHECK_SHOULD_DRAW(draw);
1712
bsalomon@google.com5782d712011-01-21 21:03:59 +00001713 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001714 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com5782d712011-01-21 21:03:59 +00001715 // we ignore the shader if texs is null.
1716 if (NULL == texs) {
twiz@google.com58071162012-07-18 21:41:50 +00001717 if (!skPaint2GrPaintNoShader(this,
1718 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001719 false,
1720 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001721 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +00001722 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001723 return;
1724 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001725 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001726 if (!skPaint2GrPaintShader(this,
1727 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001728 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001729 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001730 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001731 return;
1732 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001733 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001734
1735 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001736 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001737 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1738#if 0
1739 return
1740#endif
1741 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001742 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001743
bsalomon@google.com498776a2011-08-16 19:20:44 +00001744 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1745 if (NULL != colors) {
1746 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001747 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001748 for (int i = 0; i < vertexCount; ++i) {
rileya@google.com24f3ad12012-07-18 21:47:40 +00001749 convertedColors[i] = SkColor2GrColor(colors[i]);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001750 }
1751 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001752 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001753 fContext->drawVertices(grPaint,
1754 gVertexMode2PrimitiveType[vmode],
1755 vertexCount,
1756 (GrPoint*) vertices,
1757 (GrPoint*) texs,
1758 colors,
1759 indices,
1760 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001761}
1762
1763///////////////////////////////////////////////////////////////////////////////
1764
1765static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001766 GrFontScaler* scaler = (GrFontScaler*)data;
1767 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001768}
1769
1770static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1771 void* auxData;
1772 GrFontScaler* scaler = NULL;
1773 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1774 scaler = (GrFontScaler*)auxData;
1775 }
1776 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001777 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001778 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1779 }
1780 return scaler;
1781}
1782
1783static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1784 SkFixed fx, SkFixed fy,
1785 const SkGlyph& glyph) {
1786 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1787
bungeman@google.com15865a72012-01-11 16:28:04 +00001788 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001789
1790 if (NULL == procs->fFontScaler) {
1791 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1792 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001793
bungeman@google.com15865a72012-01-11 16:28:04 +00001794 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1795 glyph.getSubXFixed(),
1796 glyph.getSubYFixed()),
1797 SkFixedFloorToFixed(fx),
1798 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001799 procs->fFontScaler);
1800}
1801
bsalomon@google.com5782d712011-01-21 21:03:59 +00001802SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001803
1804 // deferred allocation
1805 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001806 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001807 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1808 fDrawProcs->fContext = fContext;
1809 }
1810
1811 // init our (and GL's) state
1812 fDrawProcs->fTextContext = context;
1813 fDrawProcs->fFontScaler = NULL;
1814 return fDrawProcs;
1815}
1816
1817void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1818 size_t byteLength, SkScalar x, SkScalar y,
1819 const SkPaint& paint) {
1820 CHECK_SHOULD_DRAW(draw);
1821
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001822 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001823 // this guy will just call our drawPath()
1824 draw.drawText((const char*)text, byteLength, x, y, paint);
1825 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001826 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001827
1828 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001829 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001830 if (!skPaint2GrPaintShader(this,
1831 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001832 true,
twiz@google.com58071162012-07-18 21:41:50 +00001833 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001834 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001835 return;
1836 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001837 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1838 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001839 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1840 }
1841}
1842
1843void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1844 size_t byteLength, const SkScalar pos[],
1845 SkScalar constY, int scalarsPerPos,
1846 const SkPaint& paint) {
1847 CHECK_SHOULD_DRAW(draw);
1848
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001849 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001850 // this guy will just call our drawPath()
1851 draw.drawPosText((const char*)text, byteLength, pos, constY,
1852 scalarsPerPos, paint);
1853 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001854 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001855
1856 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001857 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001858 if (!skPaint2GrPaintShader(this,
1859 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001860 true,
twiz@google.com58071162012-07-18 21:41:50 +00001861 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001862 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001863 return;
1864 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001865 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1866 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001867 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1868 scalarsPerPos, paint);
1869 }
1870}
1871
1872void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1873 size_t len, const SkPath& path,
1874 const SkMatrix* m, const SkPaint& paint) {
1875 CHECK_SHOULD_DRAW(draw);
1876
1877 SkASSERT(draw.fDevice == this);
1878 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1879}
1880
1881///////////////////////////////////////////////////////////////////////////////
1882
reed@google.comf67e4cf2011-03-15 20:56:58 +00001883bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1884 if (!paint.isLCDRenderText()) {
1885 // we're cool with the paint as is
1886 return false;
1887 }
1888
1889 if (paint.getShader() ||
1890 paint.getXfermode() || // unless its srcover
1891 paint.getMaskFilter() ||
1892 paint.getRasterizer() ||
1893 paint.getColorFilter() ||
1894 paint.getPathEffect() ||
1895 paint.isFakeBoldText() ||
1896 paint.getStyle() != SkPaint::kFill_Style) {
1897 // turn off lcd
1898 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1899 flags->fHinting = paint.getHinting();
1900 return true;
1901 }
1902 // we're cool with the paint as is
1903 return false;
1904}
1905
reed@google.com75d939b2011-12-07 15:07:23 +00001906void SkGpuDevice::flush() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001907 DO_DEFERRED_CLEAR;
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001908 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001909}
1910
reed@google.comf67e4cf2011-03-15 20:56:58 +00001911///////////////////////////////////////////////////////////////////////////////
1912
bsalomon@google.comfb309512011-11-30 14:13:48 +00001913bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001914 const GrTextureParams& params) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001915 uint64_t key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001916 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001917
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001918 GrTextureDesc desc;
1919 desc.fWidth = bitmap.width();
1920 desc.fHeight = bitmap.height();
rileya@google.com24f3ad12012-07-18 21:47:40 +00001921 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config());
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001922
robertphillips@google.com9c2ea842012-08-13 17:47:59 +00001923 GrCacheData cacheData(key);
1924
1925 return this->context()->isTextureInCache(desc, cacheData, &params);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001926}
1927
1928
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001929SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1930 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001931 bool isOpaque,
1932 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001933 GrTextureDesc desc;
1934 desc.fConfig = fRenderTarget->config();
1935 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1936 desc.fWidth = width;
1937 desc.fHeight = height;
1938 desc.fSampleCnt = fRenderTarget->numSamples();
1939
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001940 GrTexture* texture;
1941 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001942 // Skia's convention is to only clear a device if it is non-opaque.
1943 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001944
1945#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1946 // layers are never draw in repeat modes, so we can request an approx
1947 // match and ignore any padding.
1948 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1949 GrContext::kApprox_ScratchTexMatch :
1950 GrContext::kExact_ScratchTexMatch;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001951 texture = fContext->lockScratchTexture(desc, matchType);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001952#else
1953 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1954 texture = tunref.get();
1955#endif
1956 if (texture) {
1957 return SkNEW_ARGS(SkGpuDevice,(fContext,
1958 texture,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001959 needClear));
1960 } else {
1961 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1962 width, height);
1963 return NULL;
1964 }
1965}
1966
1967SkGpuDevice::SkGpuDevice(GrContext* context,
1968 GrTexture* texture,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001969 bool needClear)
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001970 : SkDevice(make_bitmap(context, texture->asRenderTarget())) {
1971
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001972 GrAssert(texture && texture->asRenderTarget());
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001973 this->initFromRenderTarget(context, texture->asRenderTarget());
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001974 fCached = true;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001975 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001976}