blob: 8d43f704e186725c0dbec268c65e734b7b91f8af [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
rmistry@google.comd6176b02012-08-23 18:14:13 +000062// requiring texture domain clamping to prevent color bleeding when drawing
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000063// 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) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000087 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000088
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 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000121
bsalomon@google.com84405e02012-03-05 19:57:21 +0000122private:
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 fRenderTarget = NULL;
193 fNeedClear = false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000194
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000195 GrAssert(NULL != renderTarget);
196 fRenderTarget = renderTarget;
197 fRenderTarget->ref();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000198
bsalomon@google.coma2921122012-08-28 12:34:17 +0000199 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
200 // on the RT but not vice-versa.
201 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
202 // busting chrome (for a currently unknown reason).
203 GrSurface* surface = fRenderTarget->asTexture();
204 if (NULL == surface) {
205 surface = fRenderTarget;
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000206 }
bsalomon@google.coma2921122012-08-28 12:34:17 +0000207 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (surface));
208
reed@google.comaf951c92011-06-16 19:10:39 +0000209 this->setPixelRef(pr, 0)->unref();
210}
211
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000212SkGpuDevice::SkGpuDevice(GrContext* context,
213 SkBitmap::Config config,
214 int width,
215 int height)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000216 : SkDevice(config, width, height, false /*isOpaque*/) {
217
reed@google.comac10a2d2010-12-22 21:39:39 +0000218 fNeedPrepareRenderTarget = false;
219 fDrawProcs = NULL;
220
reed@google.com7b201d22011-01-11 18:59:23 +0000221 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000222 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000223
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000224 fCached = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000225 fRenderTarget = NULL;
226 fNeedClear = false;
227
reed@google.comaf951c92011-06-16 19:10:39 +0000228 if (config != SkBitmap::kRGB_565_Config) {
229 config = SkBitmap::kARGB_8888_Config;
230 }
231 SkBitmap bm;
232 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000233
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000234 GrTextureDesc desc;
235 desc.fFlags = kRenderTarget_GrTextureFlagBit;
236 desc.fWidth = width;
237 desc.fHeight = height;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000238 desc.fConfig = SkBitmapConfig2GrPixelConfig(bm.config());
reed@google.comac10a2d2010-12-22 21:39:39 +0000239
bsalomon@google.coma2921122012-08-28 12:34:17 +0000240 SkAutoTUnref<GrTexture> texture(fContext->createUncachedTexture(desc, NULL, 0));
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000241
bsalomon@google.coma2921122012-08-28 12:34:17 +0000242 if (NULL != texture) {
243 fRenderTarget = texture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000244 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000245
reed@google.comaf951c92011-06-16 19:10:39 +0000246 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000247
reed@google.comaf951c92011-06-16 19:10:39 +0000248 // wrap the bitmap with a pixelref to expose our texture
bsalomon@google.coma2921122012-08-28 12:34:17 +0000249 SkGrPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (texture));
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000250 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000251 } else {
252 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
253 width, height);
254 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000255 }
256}
257
258SkGpuDevice::~SkGpuDevice() {
259 if (fDrawProcs) {
260 delete fDrawProcs;
261 }
262
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000263 // The SkGpuDevice gives the context the render target (e.g., in gainFocus)
rmistry@google.comd6176b02012-08-23 18:14:13 +0000264 // This call gives the context a chance to relinquish it
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000265 fContext->setRenderTarget(NULL);
266
bsalomon@google.coma2921122012-08-28 12:34:17 +0000267 GrTexture* texture = fRenderTarget->asTexture();
268 if (NULL != texture && fCached) {
269 fContext->unlockTexture(texture);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000270 }
bsalomon@google.coma2921122012-08-28 12:34:17 +0000271 SkSafeUnref(fRenderTarget);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000272 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000273}
274
reed@google.comac10a2d2010-12-22 21:39:39 +0000275///////////////////////////////////////////////////////////////////////////////
276
277void SkGpuDevice::makeRenderTargetCurrent() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000278 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000279 fContext->setRenderTarget(fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000280 fNeedPrepareRenderTarget = true;
281}
282
283///////////////////////////////////////////////////////////////////////////////
284
bsalomon@google.comc4364992011-11-07 15:54:49 +0000285namespace {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000286GrPixelConfig config8888_to_grconfig_and_flags(SkCanvas::Config8888 config8888, uint32_t* flags) {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000287 switch (config8888) {
288 case SkCanvas::kNative_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000289 *flags = 0;
290 return kSkia8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000291 case SkCanvas::kNative_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000292 *flags = GrContext::kUnpremul_PixelOpsFlag;
293 return kSkia8888_PM_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000294 case SkCanvas::kBGRA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000295 *flags = 0;
296 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000297 case SkCanvas::kBGRA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000298 *flags = GrContext::kUnpremul_PixelOpsFlag;
299 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000300 case SkCanvas::kRGBA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000301 *flags = 0;
302 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000303 case SkCanvas::kRGBA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000304 *flags = GrContext::kUnpremul_PixelOpsFlag;
305 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000306 default:
307 GrCrash("Unexpected Config8888.");
308 return kSkia8888_PM_GrPixelConfig;
309 }
310}
311}
312
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000313bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
314 int x, int y,
315 SkCanvas::Config8888 config8888) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000316 DO_DEFERRED_CLEAR;
bsalomon@google.com910267d2011-11-02 20:06:25 +0000317 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
318 SkASSERT(!bitmap.isNull());
319 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000320
bsalomon@google.com910267d2011-11-02 20:06:25 +0000321 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000322 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000323 uint32_t flags;
324 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000325 return fContext->readRenderTargetPixels(fRenderTarget,
326 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000327 bitmap.width(),
328 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000329 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000330 bitmap.getPixels(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000331 bitmap.rowBytes(),
332 flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000333}
334
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000335void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
336 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000337 SkAutoLockPixels alp(bitmap);
338 if (!bitmap.readyToDraw()) {
339 return;
340 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000341
342 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000343 uint32_t flags;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000344 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000345 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000346 } else {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000347 flags = 0;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000348 config= SkBitmapConfig2GrPixelConfig(bitmap.config());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000349 }
350
bsalomon@google.com6f379512011-11-16 20:36:03 +0000351 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000352 config, bitmap.getPixels(), bitmap.rowBytes(), flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000353}
354
robertphillips@google.com46f93502012-08-07 15:38:08 +0000355namespace {
356void purgeClipCB(int genID, void* data) {
357 GrContext* context = (GrContext*) data;
358
359 if (SkClipStack::kInvalidGenID == genID ||
360 SkClipStack::kEmptyGenID == genID ||
361 SkClipStack::kWideOpenGenID == genID) {
362 // none of these cases will have a cached clip mask
363 return;
364 }
365
366}
367};
368
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000369void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
370 INHERITED::onAttachToCanvas(canvas);
371
372 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000373 fClipData.fClipStack = canvas->getClipStack();
robertphillips@google.com46f93502012-08-07 15:38:08 +0000374
375 fClipData.fClipStack->addPurgeClipCallback(purgeClipCB, fContext);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000376}
377
378void SkGpuDevice::onDetachFromCanvas() {
379 INHERITED::onDetachFromCanvas();
380
robertphillips@google.com46f93502012-08-07 15:38:08 +0000381 // TODO: iterate through the clip stack and clean up any cached clip masks
382 fClipData.fClipStack->removePurgeClipCallback(purgeClipCB, fContext);
383
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000384 fClipData.fClipStack = NULL;
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000385}
386
robertphillips@google.com607fe072012-07-24 13:54:00 +0000387#ifdef SK_DEBUG
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000388static void check_bounds(const GrClipData& clipData,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000389 const SkRegion& clipRegion,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000390 int renderTargetWidth,
391 int renderTargetHeight) {
392
robertphillips@google.com7b112892012-07-31 15:18:21 +0000393 SkIRect devBound;
394
395 devBound.setLTRB(0, 0, renderTargetWidth, renderTargetHeight);
396
robertphillips@google.com607fe072012-07-24 13:54:00 +0000397 SkClipStack::BoundsType boundType;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000398 SkRect canvTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000399
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000400 clipData.fClipStack->getBounds(&canvTemp, &boundType);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000401 if (SkClipStack::kNormal_BoundsType == boundType) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000402 SkIRect devTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000403
robertphillips@google.com7b112892012-07-31 15:18:21 +0000404 canvTemp.roundOut(&devTemp);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000405
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000406 devTemp.offset(-clipData.fOrigin.fX, -clipData.fOrigin.fY);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000407
robertphillips@google.com7b112892012-07-31 15:18:21 +0000408 if (!devBound.intersect(devTemp)) {
409 devBound.setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000410 }
411 }
412
robertphillips@google.com768fee82012-08-02 12:42:43 +0000413 GrAssert(devBound.contains(clipRegion.getBounds()));
robertphillips@google.com607fe072012-07-24 13:54:00 +0000414}
415#endif
416
reed@google.comac10a2d2010-12-22 21:39:39 +0000417///////////////////////////////////////////////////////////////////////////////
418
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000419static void set_matrix_and_clip(GrContext* context, const SkMatrix& matrix,
420 GrClipData& clipData,
421 const SkRegion& clipRegion,
422 const SkIPoint& origin,
423 int renderTargetWidth, int renderTargetHeight) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000424 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000425
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000426 clipData.fOrigin = origin;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000427
428#ifdef SK_DEBUG
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000429 check_bounds(clipData, clipRegion,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000430 renderTargetWidth, renderTargetHeight);
431#endif
432
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000433 context->setClip(&clipData);
reed@google.comac10a2d2010-12-22 21:39:39 +0000434}
435
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000436// call this every draw call, to ensure that the context reflects our state,
reed@google.comac10a2d2010-12-22 21:39:39 +0000437// and not the state from some other canvas/device
438void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000439 GrAssert(NULL != fClipData.fClipStack);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000440
reed@google.comac10a2d2010-12-22 21:39:39 +0000441 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000442 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000443
444 fContext->setRenderTarget(fRenderTarget);
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000445 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000446
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000447 set_matrix_and_clip(fContext, *draw.fMatrix,
448 fClipData, *draw.fClip, this->getOrigin(),
449 fRenderTarget->width(), fRenderTarget->height());
reed@google.comac10a2d2010-12-22 21:39:39 +0000450 fNeedPrepareRenderTarget = false;
451 }
452}
453
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000454void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
455 const SkClipStack& clipStack) {
456 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
457 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000458 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000459}
460
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000461void SkGpuDevice::gainFocus(const SkMatrix& matrix, const SkRegion& clip) {
462
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000463 GrAssert(NULL != fClipData.fClipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000464
reed@google.comac10a2d2010-12-22 21:39:39 +0000465 fContext->setRenderTarget(fRenderTarget);
466
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000467 this->INHERITED::gainFocus(matrix, clip);
reed@google.comac10a2d2010-12-22 21:39:39 +0000468
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000469 set_matrix_and_clip(fContext, matrix, fClipData, clip, this->getOrigin(),
470 fRenderTarget->width(), fRenderTarget->height());
reed@google.comac10a2d2010-12-22 21:39:39 +0000471
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000472 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000473}
474
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000475SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000476 DO_DEFERRED_CLEAR;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000477 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000478}
479
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000480bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
bsalomon@google.coma2921122012-08-28 12:34:17 +0000481 GrTexture* texture = fRenderTarget->asTexture();
482 if (NULL != texture) {
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000483 paint->textureSampler(kBitmapTextureIdx)->setCustomStage(
bsalomon@google.coma2921122012-08-28 12:34:17 +0000484 SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000485 return true;
486 }
487 return false;
488}
489
490///////////////////////////////////////////////////////////////////////////////
491
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000492SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
493SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
494SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
495SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
496SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
497 shader_type_mismatch);
rileya@google.com3e332582012-07-03 13:43:35 +0000498SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
499 shader_type_mismatch);
rileya@google.com22e57f92012-07-19 15:16:19 +0000500SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
501SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000502
bsalomon@google.com84405e02012-03-05 19:57:21 +0000503namespace {
504
505// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
506// justAlpha indicates that skPaint's alpha should be used rather than the color
507// Callers may subsequently modify the GrPaint. Setting constantColor indicates
508// that the final paint will draw the same color at every pixel. This allows
509// an optimization where the the color filter can be applied to the skPaint's
twiz@google.com58071162012-07-18 21:41:50 +0000510// color once while converting to GrPaint and then ignored.
511inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
512 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000513 bool justAlpha,
514 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000515 SkGpuDevice::SkAutoCachedTexture* act,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000516 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000517
518 grPaint->fDither = skPaint.isDither();
519 grPaint->fAntiAlias = skPaint.isAntiAlias();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000520 grPaint->fCoverage = 0xFF;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000521
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000522 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
523 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000524
525 SkXfermode* mode = skPaint.getXfermode();
526 if (mode) {
527 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000528 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000529#if 0
530 return false;
531#endif
532 }
533 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000534 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
535 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
536
bsalomon@google.com5782d712011-01-21 21:03:59 +0000537 if (justAlpha) {
538 uint8_t alpha = skPaint.getAlpha();
539 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000540 // justAlpha is currently set to true only if there is a texture,
541 // so constantColor should not also be true.
542 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000543 } else {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000544 grPaint->fColor = SkColor2GrColor(skPaint.getColor());
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000545 GrAssert(!grPaint->isTextureStageEnabled(kShaderTextureIdx));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000546 }
Scroggo97c88c22011-05-11 14:05:25 +0000547 SkColorFilter* colorFilter = skPaint.getColorFilter();
548 SkColor color;
549 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000550 SkScalar matrix[20];
twiz@google.com58071162012-07-18 21:41:50 +0000551 SkBitmap colorTransformTable;
bsalomon@google.com0d944822012-08-16 15:06:57 +0000552 grPaint->resetColorFilter();
Scroggo97c88c22011-05-11 14:05:25 +0000553 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000554 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000555 if (!constantColor) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000556 grPaint->fColorFilterColor = SkColor2GrColor(color);
Scroggod757df22011-05-16 13:11:16 +0000557 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000558 } else {
559 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
rileya@google.com24f3ad12012-07-18 21:47:40 +0000560 grPaint->fColor = SkColor2GrColor(filtered);
Scroggod757df22011-05-16 13:11:16 +0000561 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000562 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
563 grPaint->fColorMatrixEnabled = true;
564 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
565 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
twiz@google.com58071162012-07-18 21:41:50 +0000566 } else if (colorFilter != NULL && colorFilter->asComponentTable(
567 &colorTransformTable)) {
568 grPaint->resetColorFilter();
569
570 GrSamplerState* colorSampler = grPaint->textureSampler(kColorFilterTextureIdx);
bsalomon@google.comb8670992012-07-25 21:27:09 +0000571 GrTexture* texture = act->set(dev, colorTransformTable, colorSampler->textureParams());
twiz@google.com58071162012-07-18 21:41:50 +0000572
bsalomon@google.comb8670992012-07-25 21:27:09 +0000573 colorSampler->reset();
bsalomon@google.comcbd0ad92012-07-20 15:09:31 +0000574 colorSampler->setCustomStage(SkNEW_ARGS(GrColorTableEffect, (texture)))->unref();
Scroggo97c88c22011-05-11 14:05:25 +0000575 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000576 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000577}
578
bsalomon@google.com84405e02012-03-05 19:57:21 +0000579// This function is similar to skPaint2GrPaintNoShader but also converts
580// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
581// be used is set on grPaint and returned in param act. constantColor has the
582// same meaning as in skPaint2GrPaintNoShader.
583inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
584 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000585 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000586 SkGpuDevice::SkAutoCachedTexture textures[GrPaint::kMaxTextures],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000587 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000588 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000589 if (NULL == shader) {
twiz@google.com58071162012-07-18 21:41:50 +0000590 return skPaint2GrPaintNoShader(dev,
591 skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000592 false,
593 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000594 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000595 grPaint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000596 } else if (!skPaint2GrPaintNoShader(dev, skPaint, true, false,
twiz@google.com58071162012-07-18 21:41:50 +0000597 &textures[kColorFilterTextureIdx], grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000598 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000599 }
600
rileya@google.com91f319c2012-07-25 17:18:31 +0000601 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
602 GrCustomStage* stage = shader->asNewCustomStage(dev->context(), sampler);
603
604 if (NULL != stage) {
605 sampler->setCustomStage(stage)->unref();
606 SkMatrix localM;
607 if (shader->getLocalMatrix(&localM)) {
608 SkMatrix inverse;
609 if (localM.invert(&inverse)) {
610 sampler->matrix()->preConcat(inverse);
611 }
612 }
613 return true;
614 }
615
reed@google.comac10a2d2010-12-22 21:39:39 +0000616 SkBitmap bitmap;
rileya@google.com91f319c2012-07-25 17:18:31 +0000617 SkMatrix* matrix = sampler->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000618 SkShader::TileMode tileModes[2];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000619 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
rileya@google.com91f319c2012-07-25 17:18:31 +0000620 tileModes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000621
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000622 if (SkShader::kNone_BitmapType == bmptype) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000623 SkShader::GradientInfo info;
624 SkColor color;
625
626 info.fColors = &color;
627 info.fColorOffsets = NULL;
628 info.fColorCount = 1;
629 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
630 SkPaint copy(skPaint);
631 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000632 // modulate the paint alpha by the shader's solid color alpha
633 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
634 copy.setColor(SkColorSetA(color, newA));
twiz@google.com58071162012-07-18 21:41:50 +0000635 return skPaint2GrPaintNoShader(dev,
636 copy,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000637 false,
638 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000639 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000640 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000641 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000642 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000643 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000644
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000645 // Must set wrap and filter on the sampler before requesting a texture.
bsalomon@google.comb8670992012-07-25 21:27:09 +0000646 sampler->textureParams()->reset(tileModes, skPaint.isFilterBitmap());
647 GrTexture* texture = textures[kShaderTextureIdx].set(dev, bitmap, sampler->textureParams());
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000648
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000649 if (NULL == texture) {
650 SkDebugf("Couldn't convert bitmap to texture.\n");
651 return false;
652 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000653
rileya@google.com91f319c2012-07-25 17:18:31 +0000654 sampler->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000655
reed@google.comac10a2d2010-12-22 21:39:39 +0000656 // since our texture coords will be in local space, we wack the texture
657 // matrix to map them back into 0...1 before we load it
658 SkMatrix localM;
659 if (shader->getLocalMatrix(&localM)) {
660 SkMatrix inverse;
661 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000662 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000663 }
664 }
665 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000666 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
667 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000668 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000669 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000670
671 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000672}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000673}
reed@google.comac10a2d2010-12-22 21:39:39 +0000674
675///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000676void SkGpuDevice::clear(SkColor color) {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000677 fContext->clear(NULL, color, fRenderTarget);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000678}
679
reed@google.comac10a2d2010-12-22 21:39:39 +0000680void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
681 CHECK_SHOULD_DRAW(draw);
682
bsalomon@google.com5782d712011-01-21 21:03:59 +0000683 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000684 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000685 if (!skPaint2GrPaintShader(this,
686 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000687 true,
twiz@google.com58071162012-07-18 21:41:50 +0000688 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000689 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000690 return;
691 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000692
693 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000694}
695
696// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000697static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000698 kPoints_GrPrimitiveType,
699 kLines_GrPrimitiveType,
700 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000701};
702
703void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000704 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000705 CHECK_SHOULD_DRAW(draw);
706
707 SkScalar width = paint.getStrokeWidth();
708 if (width < 0) {
709 return;
710 }
711
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000712 // we only handle hairlines and paints without path effects or mask filters,
713 // else we let the SkDraw call our drawPath()
714 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000715 draw.drawPoints(mode, count, pts, paint, true);
716 return;
717 }
718
bsalomon@google.com5782d712011-01-21 21:03:59 +0000719 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000720 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000721 if (!skPaint2GrPaintShader(this,
722 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000723 true,
twiz@google.com58071162012-07-18 21:41:50 +0000724 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000725 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000726 return;
727 }
728
bsalomon@google.com5782d712011-01-21 21:03:59 +0000729 fContext->drawVertices(grPaint,
730 gPointMode2PrimtiveType[mode],
731 count,
732 (GrPoint*)pts,
733 NULL,
734 NULL,
735 NULL,
736 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000737}
738
reed@google.comc9aa5872011-04-05 21:05:37 +0000739///////////////////////////////////////////////////////////////////////////////
740
reed@google.comac10a2d2010-12-22 21:39:39 +0000741void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
742 const SkPaint& paint) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000743 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000744 CHECK_SHOULD_DRAW(draw);
745
bungeman@google.com79bd8772011-07-18 15:34:08 +0000746 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000747 SkScalar width = paint.getStrokeWidth();
748
749 /*
750 We have special code for hairline strokes, miter-strokes, and fills.
751 Anything else we just call our path code.
752 */
753 bool usePath = doStroke && width > 0 &&
754 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000755 // another two reasons we might need to call drawPath...
756 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000757 usePath = true;
758 }
reed@google.com67db6642011-05-26 11:46:35 +0000759 // until we aa rotated rects...
760 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
761 usePath = true;
762 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000763 // small miter limit means right angles show bevel...
764 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
765 paint.getStrokeMiter() < SK_ScalarSqrt2)
766 {
767 usePath = true;
768 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000769 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000770 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
771 usePath = true;
772 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000773
774 if (usePath) {
775 SkPath path;
776 path.addRect(rect);
777 this->drawPath(draw, path, paint, NULL, true);
778 return;
779 }
780
781 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000782 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000783 if (!skPaint2GrPaintShader(this,
784 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000785 true,
twiz@google.com58071162012-07-18 21:41:50 +0000786 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000787 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000788 return;
789 }
reed@google.com20efde72011-05-09 17:00:02 +0000790 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000791}
792
reed@google.com69302852011-02-16 18:08:07 +0000793#include "SkMaskFilter.h"
794#include "SkBounder.h"
795
bsalomon@google.com85003222012-03-28 14:44:37 +0000796///////////////////////////////////////////////////////////////////////////////
797
798// helpers for applying mask filters
799namespace {
800
801GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000802 switch (fillType) {
803 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000804 return kWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000805 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000806 return kEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000807 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000808 return kInverseWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000809 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000810 return kInverseEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000811 default:
812 SkDebugf("Unsupported path fill type\n");
bsalomon@google.com47059542012-06-06 20:51:20 +0000813 return kHairLine_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000814 }
815}
816
bsalomon@google.com85003222012-03-28 14:44:37 +0000817// We prefer to blur small rect with small radius via CPU.
818#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
819#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
820inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
821 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
822 rect.height() <= MIN_GPU_BLUR_SIZE &&
823 radius <= MIN_GPU_BLUR_RADIUS) {
824 return true;
825 }
826 return false;
827}
828
829bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
830 SkMaskFilter* filter, const SkMatrix& matrix,
831 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000832 GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000833#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000834 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000835#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000836 SkMaskFilter::BlurInfo info;
837 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000838 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000839 return false;
840 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000841 SkScalar radius = info.fIgnoreTransform ? info.fRadius
842 : matrix.mapRadius(info.fRadius);
843 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000844 if (radius <= 0) {
845 return false;
846 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000847
848 SkRect srcRect = path.getBounds();
849 if (shouldDrawBlurWithCPU(srcRect, radius)) {
850 return false;
851 }
852
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000853 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000854 float sigma3 = sigma * 3.0f;
855
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000856 SkRect clipRect;
857 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000858
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000859 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000860 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
861 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000862 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000863 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000864 SkIRect finalIRect;
865 finalRect.roundOut(&finalIRect);
866 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000867 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000868 }
869 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000870 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000871 }
872 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000873 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000874 GrTextureDesc desc;
875 desc.fFlags = kRenderTarget_GrTextureFlagBit;
876 desc.fWidth = SkScalarCeilToInt(srcRect.width());
877 desc.fHeight = SkScalarCeilToInt(srcRect.height());
878 // We actually only need A8, but it often isn't supported as a
879 // render target so default to RGBA_8888
bsalomon@google.com0342a852012-08-20 19:22:38 +0000880 desc.fConfig = kRGBA_8888_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000881
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000882 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
883 desc.fConfig = kAlpha_8_GrPixelConfig;
884 }
885
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000886 GrAutoScratchTexture pathEntry(context, desc);
887 GrTexture* pathTexture = pathEntry.texture();
888 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000889 return false;
890 }
891 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000892 // Once this code moves into GrContext, this should be changed to use
893 // an AutoClipRestore.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000894 const GrClipData* oldClipData = context->getClip();
895
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000896 context->setRenderTarget(pathTexture->asRenderTarget());
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000897
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000898 SkClipStack newClipStack(srcRect);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000899 GrClipData newClipData;
900 newClipData.fClipStack = &newClipStack;
901 context->setClip(&newClipData);
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000902
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000903 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000904 GrPaint tempPaint;
905 tempPaint.reset();
906
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000907 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000908 tempPaint.fAntiAlias = grp->fAntiAlias;
909 if (tempPaint.fAntiAlias) {
910 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
911 // blend coeff of zero requires dual source blending support in order
912 // to properly blend partially covered pixels. This means the AA
913 // code path may not be taken. So we use a dst blend coeff of ISA. We
914 // could special case AA draws to a dst surface with known alpha=0 to
915 // use a zero dst coeff when dual source blending isn't available.
bsalomon@google.com47059542012-06-06 20:51:20 +0000916 tempPaint.fSrcBlendCoeff = kOne_GrBlendCoeff;
917 tempPaint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000918 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000919 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000920 context->drawPath(tempPaint, path, pathFillType, &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000921
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000922 // If we're doing a normal blur, we can clobber the pathTexture in the
923 // gaussianBlur. Otherwise, we need to save it for later compositing.
924 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +0000925 SkAutoTUnref<GrTexture> blurTexture(context->gaussianBlur(
926 pathTexture, isNormalBlur, srcRect, sigma, sigma));
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000927
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000928 if (!isNormalBlur) {
929 GrPaint paint;
930 paint.reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +0000931 paint.textureSampler(0)->textureParams()->setClampNoFilter();
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000932 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
933 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000934 // Blend pathTexture over blurTexture.
935 context->setRenderTarget(blurTexture->asRenderTarget());
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000936 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS
937 (GrSingleTextureEffect, (pathTexture)))->unref();
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000938 if (SkMaskFilter::kInner_BlurType == blurType) {
939 // inner: dst = dst * src
bsalomon@google.com47059542012-06-06 20:51:20 +0000940 paint.fSrcBlendCoeff = kDC_GrBlendCoeff;
941 paint.fDstBlendCoeff = kZero_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000942 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
943 // solid: dst = src + dst - src * dst
944 // = (1 - dst) * src + 1 * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000945 paint.fSrcBlendCoeff = kIDC_GrBlendCoeff;
946 paint.fDstBlendCoeff = kOne_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000947 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
948 // outer: dst = dst * (1 - src)
949 // = 0 * src + (1 - src) * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000950 paint.fSrcBlendCoeff = kZero_GrBlendCoeff;
951 paint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000952 }
953 context->drawRect(paint, srcRect);
954 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000955 context->setRenderTarget(oldRenderTarget);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000956 context->setClip(oldClipData);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000957
bsalomon@google.come3d32162012-07-20 13:37:06 +0000958 if (!grp->preConcatSamplerMatricesWithInverse(matrix)) {
959 return false;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000960 }
961
962 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
963 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000964 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
bsalomon@google.com97912912011-12-06 16:30:36 +0000965 grp->maskSampler(MASK_IDX)->reset();
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000966 grp->maskSampler(MASK_IDX)->setCustomStage(
967 SkNEW_ARGS(GrSingleTextureEffect, (blurTexture)))->unref();
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000968 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
969 -finalRect.fTop);
970 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
971 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000972 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000973 return true;
974}
975
bsalomon@google.com85003222012-03-28 14:44:37 +0000976bool drawWithMaskFilter(GrContext* context, const SkPath& path,
977 SkMaskFilter* filter, const SkMatrix& matrix,
978 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000979 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000980 SkMask srcM, dstM;
981
982 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000983 SkMask::kComputeBoundsAndRenderImage_CreateMode,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000984 style)) {
reed@google.com69302852011-02-16 18:08:07 +0000985 return false;
986 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000987 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000988
989 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
990 return false;
991 }
992 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000993 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000994
995 if (clip.quickReject(dstM.fBounds)) {
996 return false;
997 }
998 if (bounder && !bounder->doIRect(dstM.fBounds)) {
999 return false;
1000 }
1001
1002 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
1003 // the current clip (and identity matrix) and grpaint settings
1004
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001005 GrContext::AutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +00001006
bsalomon@google.come3d32162012-07-20 13:37:06 +00001007 if (!grp->preConcatSamplerMatricesWithInverse(matrix)) {
1008 return false;
1009 }
1010
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001011 GrTextureDesc desc;
1012 desc.fWidth = dstM.fBounds.width();
1013 desc.fHeight = dstM.fBounds.height();
1014 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +00001015
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001016 GrAutoScratchTexture ast(context, desc);
1017 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001018
reed@google.com69302852011-02-16 18:08:07 +00001019 if (NULL == texture) {
1020 return false;
1021 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001022 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001023 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +00001024
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001025 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
1026 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001027 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
bsalomon@google.com97912912011-12-06 16:30:36 +00001028 grp->maskSampler(MASK_IDX)->reset();
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001029 grp->maskSampler(MASK_IDX)->setCustomStage(
1030 SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001031 GrRect d;
1032 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +00001033 GrIntToScalar(dstM.fBounds.fTop),
1034 GrIntToScalar(dstM.fBounds.fRight),
1035 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001036
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001037 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
1038 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
1039 -dstM.fBounds.fTop*SK_Scalar1);
1040 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001041 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +00001042 return true;
1043}
reed@google.com69302852011-02-16 18:08:07 +00001044
bsalomon@google.com85003222012-03-28 14:44:37 +00001045}
1046
1047///////////////////////////////////////////////////////////////////////////////
1048
reed@google.com0c219b62011-02-16 21:31:18 +00001049void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001050 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +00001051 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +00001052 CHECK_FOR_NODRAW_ANNOTATION(paint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001053 CHECK_SHOULD_DRAW(draw);
1054
reed@google.comfe626382011-09-21 13:50:35 +00001055 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001056
bsalomon@google.com5782d712011-01-21 21:03:59 +00001057 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001058 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001059 if (!skPaint2GrPaintShader(this,
1060 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001061 true,
twiz@google.com58071162012-07-18 21:41:50 +00001062 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001063 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001064 return;
1065 }
1066
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +00001067 // can we cheat, and threat a thin stroke as a hairline w/ coverage
1068 // if we can, we draw lots faster (raster device does this same test)
1069 SkScalar hairlineCoverage;
1070 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
1071 doFill = false;
1072 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
1073 grPaint.fCoverage);
1074 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001075
reed@google.comfe626382011-09-21 13:50:35 +00001076 // If we have a prematrix, apply it to the path, optimizing for the case
1077 // where the original path can in fact be modified in place (even though
1078 // its parameter type is const).
1079 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1080 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001081
1082 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001083 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001084
reed@google.come3445642011-02-16 23:20:39 +00001085 if (!pathIsMutable) {
1086 result = &tmpPath;
1087 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001088 }
reed@google.come3445642011-02-16 23:20:39 +00001089 // should I push prePathMatrix on our MV stack temporarily, instead
1090 // of applying it here? See SkDraw.cpp
1091 pathPtr->transform(*prePathMatrix, result);
1092 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001093 }
reed@google.com0c219b62011-02-16 21:31:18 +00001094 // at this point we're done with prePathMatrix
1095 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001096
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001097 if (paint.getPathEffect() ||
1098 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001099 // it is safe to use tmpPath here, even if we already used it for the
1100 // prepathmatrix, since getFillPath can take the same object for its
1101 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001102 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001103 pathPtr = &tmpPath;
1104 }
1105
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001106 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001107 // avoid possibly allocating a new path in transform if we can
1108 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1109
1110 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001111 pathPtr->transform(*draw.fMatrix, devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001112 GrPathFill pathFillType = doFill ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001113 skToGrFillType(devPathPtr->getFillType()) : kHairLine_GrPathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001114 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001115 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001116 &grPaint, pathFillType)) {
rmistry@google.comd6176b02012-08-23 18:14:13 +00001117 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001118 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001119 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001120 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001121 &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001122 }
reed@google.com69302852011-02-16 18:08:07 +00001123 return;
1124 }
reed@google.com69302852011-02-16 18:08:07 +00001125
bsalomon@google.com47059542012-06-06 20:51:20 +00001126 GrPathFill fill = kHairLine_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001127
reed@google.com0c219b62011-02-16 21:31:18 +00001128 if (doFill) {
1129 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001130 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001131 fill = kWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001132 break;
1133 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001134 fill = kEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001135 break;
1136 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001137 fill = kInverseWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001138 break;
1139 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001140 fill = kInverseEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001141 break;
1142 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001143 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001144 return;
1145 }
1146 }
1147
reed@google.com07f3ee12011-05-16 17:21:57 +00001148 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001149}
1150
bsalomon@google.comfb309512011-11-30 14:13:48 +00001151namespace {
1152
1153inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1154 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1155 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1156 return tilesX * tilesY;
1157}
1158
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001159inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001160 const SkIRect* srcRectPtr,
1161 int maxTextureSize) {
1162 static const int kSmallTileSize = 1 << 10;
1163 if (maxTextureSize <= kSmallTileSize) {
1164 return maxTextureSize;
1165 }
1166
1167 size_t maxTexTotalTileSize;
1168 size_t smallTotalTileSize;
1169
1170 if (NULL == srcRectPtr) {
1171 int w = bitmap.width();
1172 int h = bitmap.height();
1173 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1174 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1175 } else {
1176 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1177 srcRectPtr->fTop,
1178 srcRectPtr->fRight,
1179 srcRectPtr->fBottom,
1180 maxTextureSize);
1181 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1182 srcRectPtr->fTop,
1183 srcRectPtr->fRight,
1184 srcRectPtr->fBottom,
1185 kSmallTileSize);
1186 }
1187 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1188 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1189
1190 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1191 return kSmallTileSize;
1192 } else {
1193 return maxTextureSize;
1194 }
1195}
1196}
1197
1198bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001199 const GrTextureParams& params,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001200 const SkIRect* srcRectPtr,
1201 int* tileSize) const {
1202 SkASSERT(NULL != tileSize);
1203
1204 // if bitmap is explictly texture backed then just use the texture
1205 if (NULL != bitmap.getTexture()) {
1206 return false;
1207 }
1208 // if it's larger than the max texture size, then we have no choice but
1209 // tiling
1210 const int maxTextureSize = fContext->getMaxTextureSize();
1211 if (bitmap.width() > maxTextureSize ||
1212 bitmap.height() > maxTextureSize) {
1213 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1214 return true;
1215 }
1216 // if we are going to have to draw the whole thing, then don't tile
1217 if (NULL == srcRectPtr) {
1218 return false;
1219 }
1220 // if the entire texture is already in our cache then no reason to tile it
bsalomon@google.comb8670992012-07-25 21:27:09 +00001221 if (this->isBitmapInTextureCache(bitmap, params)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001222 return false;
1223 }
1224
1225 // At this point we know we could do the draw by uploading the entire bitmap
1226 // as a texture. However, if the texture would be large compared to the
1227 // cache size and we don't require most of it for this draw then tile to
1228 // reduce the amount of upload and cache spill.
1229
1230 // assumption here is that sw bitmap size is a good proxy for its size as
1231 // a texture
1232 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001233 size_t cacheSize;
1234 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001235 if (bmpSize < cacheSize / 2) {
1236 return false;
1237 }
1238
1239 SkFixed fracUsed =
1240 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1241 (srcRectPtr->height() << 16) / bitmap.height());
1242 if (fracUsed <= SK_FixedHalf) {
1243 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1244 return true;
1245 } else {
1246 return false;
1247 }
1248}
1249
reed@google.comac10a2d2010-12-22 21:39:39 +00001250void SkGpuDevice::drawBitmap(const SkDraw& draw,
1251 const SkBitmap& bitmap,
1252 const SkIRect* srcRectPtr,
1253 const SkMatrix& m,
1254 const SkPaint& paint) {
1255 CHECK_SHOULD_DRAW(draw);
1256
1257 SkIRect srcRect;
1258 if (NULL == srcRectPtr) {
1259 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1260 } else {
1261 srcRect = *srcRectPtr;
1262 }
1263
junov@google.comd935cfb2011-06-27 20:48:23 +00001264 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001265 // Convert the bitmap to a shader so that the rect can be drawn
1266 // through drawRect, which supports mask filters.
1267 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001268 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001269 if (srcRectPtr) {
1270 if (!bitmap.extractSubset(&tmp, srcRect)) {
1271 return; // extraction failed
1272 }
1273 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001274 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001275 }
1276 SkPaint paintWithTexture(paint);
1277 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1278 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001279 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001280 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001281
junov@google.com1d329782011-07-28 20:10:09 +00001282 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001283 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001284 // also affect the behavior of the mask filter.
1285 SkMatrix drawMatrix;
1286 drawMatrix.setConcat(*draw.fMatrix, m);
1287 SkDraw transformedDraw(draw);
1288 transformedDraw.fMatrix = &drawMatrix;
1289
1290 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1291
junov@google.comd935cfb2011-06-27 20:48:23 +00001292 return;
1293 }
1294
bsalomon@google.com5782d712011-01-21 21:03:59 +00001295 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001296 SkAutoCachedTexture colorLutTexture;
1297 if (!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001298 return;
1299 }
bsalomon@google.comb8670992012-07-25 21:27:09 +00001300 GrTextureParams* params = grPaint.textureSampler(kBitmapTextureIdx)->textureParams();
1301 params->setBilerp(paint.isFilterBitmap());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001302
bsalomon@google.comfb309512011-11-30 14:13:48 +00001303 int tileSize;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001304 if (!this->shouldTileBitmap(bitmap, *params, srcRectPtr, &tileSize)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001305 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001306 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001307 return;
1308 }
1309
1310 // undo the translate done by SkCanvas
1311 int DX = SkMax32(0, srcRect.fLeft);
1312 int DY = SkMax32(0, srcRect.fTop);
1313 // compute clip bounds in local coordinates
1314 SkIRect clipRect;
1315 {
1316 SkRect r;
1317 r.set(draw.fClip->getBounds());
1318 SkMatrix matrix, inverse;
1319 matrix.setConcat(*draw.fMatrix, m);
1320 if (!matrix.invert(&inverse)) {
1321 return;
1322 }
1323 inverse.mapRect(&r);
1324 r.roundOut(&clipRect);
1325 // apply the canvas' translate to our local clip
1326 clipRect.offset(DX, DY);
1327 }
1328
bsalomon@google.comfb309512011-11-30 14:13:48 +00001329 int nx = bitmap.width() / tileSize;
1330 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001331 for (int x = 0; x <= nx; x++) {
1332 for (int y = 0; y <= ny; y++) {
1333 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001334 tileR.set(x * tileSize, y * tileSize,
1335 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001336 if (!SkIRect::Intersects(tileR, clipRect)) {
1337 continue;
1338 }
1339
1340 SkIRect srcR = tileR;
1341 if (!srcR.intersect(srcRect)) {
1342 continue;
1343 }
1344
1345 SkBitmap tmpB;
1346 if (bitmap.extractSubset(&tmpB, tileR)) {
1347 // now offset it to make it "local" to our tmp bitmap
1348 srcR.offset(-tileR.fLeft, -tileR.fTop);
1349
1350 SkMatrix tmpM(m);
1351 {
1352 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1353 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1354 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1355 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001356 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001357 }
1358 }
1359 }
1360}
1361
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001362namespace {
1363
1364bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1365 // detect pixel disalignment
1366 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1367 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
rmistry@google.comd6176b02012-08-23 18:14:13 +00001368 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001369 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1370 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1371 COLOR_BLEED_TOLERANCE &&
1372 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1373 COLOR_BLEED_TOLERANCE) {
1374 return true;
1375 }
1376 return false;
1377}
1378
1379bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1380 const SkMatrix& m) {
1381 // Only gets called if hasAlignedSamples returned false.
1382 // So we can assume that sampling is axis aligned but not texel aligned.
1383 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001384 SkRect innerSrcRect(srcRect), innerTransformedRect,
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001385 outerTransformedRect(transformedRect);
1386 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1387 m.mapRect(&innerTransformedRect, innerSrcRect);
1388
1389 // The gap between outerTransformedRect and innerTransformedRect
1390 // represents the projection of the source border area, which is
1391 // problematic for color bleeding. We must check whether any
1392 // destination pixels sample the border area.
1393 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1394 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1395 SkIRect outer, inner;
1396 outerTransformedRect.round(&outer);
1397 innerTransformedRect.round(&inner);
1398 // If the inner and outer rects round to the same result, it means the
1399 // border does not overlap any pixel centers. Yay!
1400 return inner != outer;
1401}
1402
1403} // unnamed namespace
1404
reed@google.comac10a2d2010-12-22 21:39:39 +00001405/*
1406 * This is called by drawBitmap(), which has to handle images that may be too
1407 * large to be represented by a single texture.
1408 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001409 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1410 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001411 */
1412void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1413 const SkBitmap& bitmap,
1414 const SkIRect& srcRect,
1415 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001416 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001417 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1418 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001419
reed@google.com9c49bc32011-07-07 13:42:37 +00001420 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001421 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001422 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001423 return;
1424 }
1425
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001426 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001427
bsalomon@google.comb8670992012-07-25 21:27:09 +00001428 sampler->textureParams()->setClamp();
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001429 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001430
1431 GrTexture* texture;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001432 SkAutoCachedTexture act(this, bitmap, sampler->textureParams(), &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001433 if (NULL == texture) {
1434 return;
1435 }
1436
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001437 grPaint->textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1438 (GrSingleTextureEffect, (texture)))->unref();
reed@google.com46799cd2011-02-22 20:56:26 +00001439
reed@google.com20efde72011-05-09 17:00:02 +00001440 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1441 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001442 GrRect paintRect;
bsalomon@google.com91832162012-03-08 19:53:02 +00001443 float wInv = 1.f / bitmap.width();
1444 float hInv = 1.f / bitmap.height();
1445 paintRect.setLTRB(SkFloatToScalar(srcRect.fLeft * wInv),
1446 SkFloatToScalar(srcRect.fTop * hInv),
1447 SkFloatToScalar(srcRect.fRight * wInv),
1448 SkFloatToScalar(srcRect.fBottom * hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001449
rmistry@google.comd6176b02012-08-23 18:14:13 +00001450 bool needsTextureDomain = false;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001451 if (sampler->textureParams()->isBilerp()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001452 // Need texture domain if drawing a sub rect.
bsalomon@google.comb8670992012-07-25 21:27:09 +00001453 needsTextureDomain = srcRect.width() < bitmap.width() || srcRect.height() < bitmap.height();
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001454 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1455 // sampling is axis-aligned
1456 GrRect floatSrcRect, transformedRect;
1457 floatSrcRect.set(srcRect);
1458 SkMatrix srcToDeviceMatrix(m);
1459 srcToDeviceMatrix.postConcat(*draw.fMatrix);
1460 srcToDeviceMatrix.mapRect(&transformedRect, floatSrcRect);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001461
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001462 if (hasAlignedSamples(floatSrcRect, transformedRect)) {
1463 // Samples are texel-aligned, so filtering is futile
bsalomon@google.comb8670992012-07-25 21:27:09 +00001464 sampler->textureParams()->setBilerp(false);
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001465 needsTextureDomain = false;
1466 } else {
1467 needsTextureDomain = needsTextureDomain &&
1468 mayColorBleed(floatSrcRect, transformedRect, m);
1469 }
1470 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001471 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001472
1473 GrRect textureDomain = GrRect::MakeEmpty();
1474
1475 if (needsTextureDomain) {
1476 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001477 GrScalar left, top, right, bottom;
1478 if (srcRect.width() > 1) {
1479 GrScalar border = GR_ScalarHalf / bitmap.width();
1480 left = paintRect.left() + border;
1481 right = paintRect.right() - border;
1482 } else {
1483 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1484 }
1485 if (srcRect.height() > 1) {
1486 GrScalar border = GR_ScalarHalf / bitmap.height();
1487 top = paintRect.top() + border;
1488 bottom = paintRect.bottom() - border;
1489 } else {
1490 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1491 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001492 textureDomain.setLTRB(left, top, right, bottom);
tomhudson@google.com2f68e762012-07-17 18:43:21 +00001493 sampler->setCustomStage(SkNEW_ARGS(GrTextureDomainEffect,
1494 (texture,
1495 textureDomain)))->unref();
junov@google.com6acc9b32011-05-16 18:32:07 +00001496 }
1497
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001498 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001499}
1500
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001501namespace {
1502
1503void apply_custom_stage(GrContext* context,
1504 GrTexture* srcTexture,
1505 GrTexture* dstTexture,
1506 const GrRect& rect,
1507 GrCustomStage* stage) {
1508 SkASSERT(srcTexture && srcTexture->getContext() == context);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001509 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001510 GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget());
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001511 GrContext::AutoClip acs(context, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001512
1513 GrMatrix sampleM;
1514 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
1515 GrPaint paint;
1516 paint.reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +00001517 paint.textureSampler(0)->textureParams()->setBilerp(true);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001518 paint.textureSampler(0)->reset(sampleM);
1519 paint.textureSampler(0)->setCustomStage(stage);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001520 context->drawRect(paint, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001521}
1522
1523};
1524
reed@google.com8926b162012-03-23 15:36:36 +00001525static GrTexture* filter_texture(GrContext* context, GrTexture* texture,
1526 SkImageFilter* filter, const GrRect& rect) {
1527 GrAssert(filter);
1528
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001529 GrTextureDesc desc;
1530 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1531 desc.fWidth = SkScalarCeilToInt(rect.width());
1532 desc.fHeight = SkScalarCeilToInt(rect.height());
bsalomon@google.com0342a852012-08-20 19:22:38 +00001533 desc.fConfig = kRGBA_8888_GrPixelConfig;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001534 GrCustomStage* stage;
reed@google.com8926b162012-03-23 15:36:36 +00001535
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001536 if (filter->canFilterImageGPU()) {
1537 texture = filter->onFilterImageGPU(texture, rect);
1538 } else if (filter->asNewCustomStage(&stage, texture)) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001539 GrAutoScratchTexture dst(context, desc);
1540 apply_custom_stage(context, texture, dst.texture(), rect, stage);
1541 texture = dst.detach();
1542 stage->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001543 }
1544 return texture;
1545}
1546
reed@google.comac10a2d2010-12-22 21:39:39 +00001547void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1548 int left, int top, const SkPaint& paint) {
1549 CHECK_SHOULD_DRAW(draw);
1550
reed@google.com8926b162012-03-23 15:36:36 +00001551 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001552 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1553 return;
1554 }
1555
reed@google.com76dd2772012-01-05 21:15:07 +00001556 int w = bitmap.width();
1557 int h = bitmap.height();
1558
bsalomon@google.com5782d712011-01-21 21:03:59 +00001559 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001560 SkAutoCachedTexture colorLutTexture;
1561 if(!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001562 return;
1563 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001564
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001565 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001566
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001567 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001568
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001569 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001570 sampler->reset();
bsalomon@google.comb8670992012-07-25 21:27:09 +00001571 SkAutoCachedTexture act(this, bitmap, sampler->textureParams(), &texture);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001572 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1573 (GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001574
reed@google.com8926b162012-03-23 15:36:36 +00001575 SkImageFilter* filter = paint.getImageFilter();
1576 if (NULL != filter) {
1577 GrTexture* filteredTexture = filter_texture(fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001578 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001579 if (filteredTexture) {
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001580 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1581 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001582 texture = filteredTexture;
1583 filteredTexture->unref();
1584 }
reed@google.com76dd2772012-01-05 21:15:07 +00001585 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001586
bsalomon@google.com5782d712011-01-21 21:03:59 +00001587 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001588 GrRect::MakeXYWH(GrIntToScalar(left),
1589 GrIntToScalar(top),
1590 GrIntToScalar(w),
1591 GrIntToScalar(h)),
1592 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1593 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001594}
1595
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001596void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
reed@google.comac10a2d2010-12-22 21:39:39 +00001597 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001598 // clear of the source device must occur before CHECK_SHOULD_DRAW
1599 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1600 if (dev->fNeedClear) {
1601 // TODO: could check here whether we really need to draw at all
1602 dev->clear(0x0);
1603 }
1604
reed@google.comac10a2d2010-12-22 21:39:39 +00001605 CHECK_SHOULD_DRAW(draw);
1606
bsalomon@google.com5782d712011-01-21 21:03:59 +00001607 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001608 SkAutoCachedTexture colorLutTexture;
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001609 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001610 if (!dev->bindDeviceAsTexture(&grPaint) ||
twiz@google.com58071162012-07-18 21:41:50 +00001611 !skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001612 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001613 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001614
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001615 GrTexture* devTex = grPaint.getTextureSampler(kBitmapTextureIdx).getCustomStage()->texture(0);
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001616 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001617
reed@google.com8926b162012-03-23 15:36:36 +00001618 SkImageFilter* filter = paint.getImageFilter();
1619 if (NULL != filter) {
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001620 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001621 SkIntToScalar(devTex->height()));
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001622 GrTexture* filteredTexture = filter_texture(fContext, devTex, filter, rect);
reed@google.com8926b162012-03-23 15:36:36 +00001623 if (filteredTexture) {
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001624 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1625 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001626 devTex = filteredTexture;
1627 filteredTexture->unref();
1628 }
1629 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001630
bsalomon@google.com5782d712011-01-21 21:03:59 +00001631 const SkBitmap& bm = dev->accessBitmap(false);
1632 int w = bm.width();
1633 int h = bm.height();
1634
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001635 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001636 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1637 GrIntToScalar(y),
1638 GrIntToScalar(w),
1639 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001640
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001641 // The device being drawn may not fill up its texture (saveLayer uses
1642 // the approximate ).
1643 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1644 GR_Scalar1 * h / devTex->height());
1645
1646 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001647}
1648
reed@google.com8926b162012-03-23 15:36:36 +00001649bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001650 if (!filter->asNewCustomStage(NULL, NULL) &&
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001651 !filter->canFilterImageGPU()) {
reed@google.com76dd2772012-01-05 21:15:07 +00001652 return false;
1653 }
reed@google.com8926b162012-03-23 15:36:36 +00001654 return true;
1655}
1656
1657bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1658 const SkMatrix& ctm,
1659 SkBitmap* result, SkIPoint* offset) {
1660 // want explicitly our impl, so guard against a subclass of us overriding it
1661 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001662 return false;
1663 }
reed@google.com8926b162012-03-23 15:36:36 +00001664
1665 SkAutoLockPixels alp(src, !src.getTexture());
1666 if (!src.getTexture() && !src.readyToDraw()) {
1667 return false;
1668 }
1669
1670 GrPaint paint;
1671 paint.reset();
1672
1673 GrSamplerState* sampler = paint.textureSampler(kBitmapTextureIdx);
1674
1675 GrTexture* texture;
bsalomon@google.comb8670992012-07-25 21:27:09 +00001676 SkAutoCachedTexture act(this, src, sampler->textureParams(), &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001677
1678 result->setConfig(src.config(), src.width(), src.height());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001679 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001680 SkIntToScalar(src.height()));
reed@google.com8926b162012-03-23 15:36:36 +00001681 GrTexture* resultTexture = filter_texture(fContext, texture, filter, rect);
1682 if (resultTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001683 result->setPixelRef(SkNEW_ARGS(SkGrTexturePixelRef,
1684 (resultTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001685 resultTexture->unref();
1686 }
reed@google.com76dd2772012-01-05 21:15:07 +00001687 return true;
1688}
1689
reed@google.comac10a2d2010-12-22 21:39:39 +00001690///////////////////////////////////////////////////////////////////////////////
1691
1692// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001693static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001694 kTriangles_GrPrimitiveType,
1695 kTriangleStrip_GrPrimitiveType,
1696 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001697};
1698
1699void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1700 int vertexCount, const SkPoint vertices[],
1701 const SkPoint texs[], const SkColor colors[],
1702 SkXfermode* xmode,
1703 const uint16_t indices[], int indexCount,
1704 const SkPaint& paint) {
1705 CHECK_SHOULD_DRAW(draw);
1706
bsalomon@google.com5782d712011-01-21 21:03:59 +00001707 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001708 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com5782d712011-01-21 21:03:59 +00001709 // we ignore the shader if texs is null.
1710 if (NULL == texs) {
twiz@google.com58071162012-07-18 21:41:50 +00001711 if (!skPaint2GrPaintNoShader(this,
1712 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001713 false,
1714 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001715 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +00001716 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001717 return;
1718 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001719 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001720 if (!skPaint2GrPaintShader(this,
1721 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001722 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001723 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001724 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001725 return;
1726 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001727 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001728
1729 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001730 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001731 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1732#if 0
1733 return
1734#endif
1735 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001736 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001737
bsalomon@google.com498776a2011-08-16 19:20:44 +00001738 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1739 if (NULL != colors) {
1740 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001741 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001742 for (int i = 0; i < vertexCount; ++i) {
rileya@google.com24f3ad12012-07-18 21:47:40 +00001743 convertedColors[i] = SkColor2GrColor(colors[i]);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001744 }
1745 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001746 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001747 fContext->drawVertices(grPaint,
1748 gVertexMode2PrimitiveType[vmode],
1749 vertexCount,
1750 (GrPoint*) vertices,
1751 (GrPoint*) texs,
1752 colors,
1753 indices,
1754 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001755}
1756
1757///////////////////////////////////////////////////////////////////////////////
1758
1759static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001760 GrFontScaler* scaler = (GrFontScaler*)data;
1761 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001762}
1763
1764static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1765 void* auxData;
1766 GrFontScaler* scaler = NULL;
1767 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1768 scaler = (GrFontScaler*)auxData;
1769 }
1770 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001771 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001772 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1773 }
1774 return scaler;
1775}
1776
1777static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1778 SkFixed fx, SkFixed fy,
1779 const SkGlyph& glyph) {
1780 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1781
bungeman@google.com15865a72012-01-11 16:28:04 +00001782 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001783
1784 if (NULL == procs->fFontScaler) {
1785 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1786 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001787
bungeman@google.com15865a72012-01-11 16:28:04 +00001788 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1789 glyph.getSubXFixed(),
1790 glyph.getSubYFixed()),
1791 SkFixedFloorToFixed(fx),
1792 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001793 procs->fFontScaler);
1794}
1795
bsalomon@google.com5782d712011-01-21 21:03:59 +00001796SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001797
1798 // deferred allocation
1799 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001800 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001801 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1802 fDrawProcs->fContext = fContext;
1803 }
1804
1805 // init our (and GL's) state
1806 fDrawProcs->fTextContext = context;
1807 fDrawProcs->fFontScaler = NULL;
1808 return fDrawProcs;
1809}
1810
1811void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1812 size_t byteLength, SkScalar x, SkScalar y,
1813 const SkPaint& paint) {
1814 CHECK_SHOULD_DRAW(draw);
1815
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001816 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001817 // this guy will just call our drawPath()
1818 draw.drawText((const char*)text, byteLength, x, y, paint);
1819 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001820 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001821
1822 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001823 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001824 if (!skPaint2GrPaintShader(this,
1825 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001826 true,
twiz@google.com58071162012-07-18 21:41:50 +00001827 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001828 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001829 return;
1830 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001831 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1832 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001833 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1834 }
1835}
1836
1837void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1838 size_t byteLength, const SkScalar pos[],
1839 SkScalar constY, int scalarsPerPos,
1840 const SkPaint& paint) {
1841 CHECK_SHOULD_DRAW(draw);
1842
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001843 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001844 // this guy will just call our drawPath()
1845 draw.drawPosText((const char*)text, byteLength, pos, constY,
1846 scalarsPerPos, paint);
1847 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001848 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001849
1850 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001851 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001852 if (!skPaint2GrPaintShader(this,
1853 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001854 true,
twiz@google.com58071162012-07-18 21:41:50 +00001855 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001856 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001857 return;
1858 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001859 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1860 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001861 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1862 scalarsPerPos, paint);
1863 }
1864}
1865
1866void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1867 size_t len, const SkPath& path,
1868 const SkMatrix* m, const SkPaint& paint) {
1869 CHECK_SHOULD_DRAW(draw);
1870
1871 SkASSERT(draw.fDevice == this);
1872 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1873}
1874
1875///////////////////////////////////////////////////////////////////////////////
1876
reed@google.comf67e4cf2011-03-15 20:56:58 +00001877bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1878 if (!paint.isLCDRenderText()) {
1879 // we're cool with the paint as is
1880 return false;
1881 }
1882
1883 if (paint.getShader() ||
1884 paint.getXfermode() || // unless its srcover
1885 paint.getMaskFilter() ||
1886 paint.getRasterizer() ||
1887 paint.getColorFilter() ||
1888 paint.getPathEffect() ||
1889 paint.isFakeBoldText() ||
1890 paint.getStyle() != SkPaint::kFill_Style) {
1891 // turn off lcd
1892 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1893 flags->fHinting = paint.getHinting();
1894 return true;
1895 }
1896 // we're cool with the paint as is
1897 return false;
1898}
1899
reed@google.com75d939b2011-12-07 15:07:23 +00001900void SkGpuDevice::flush() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001901 DO_DEFERRED_CLEAR;
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001902 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001903}
1904
reed@google.comf67e4cf2011-03-15 20:56:58 +00001905///////////////////////////////////////////////////////////////////////////////
1906
bsalomon@google.comfb309512011-11-30 14:13:48 +00001907bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001908 const GrTextureParams& params) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001909 uint64_t key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001910 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001911
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001912 GrTextureDesc desc;
1913 desc.fWidth = bitmap.width();
1914 desc.fHeight = bitmap.height();
rileya@google.com24f3ad12012-07-18 21:47:40 +00001915 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config());
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001916
robertphillips@google.com9c2ea842012-08-13 17:47:59 +00001917 GrCacheData cacheData(key);
1918
1919 return this->context()->isTextureInCache(desc, cacheData, &params);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001920}
1921
1922
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001923SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1924 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001925 bool isOpaque,
1926 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001927 GrTextureDesc desc;
1928 desc.fConfig = fRenderTarget->config();
1929 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1930 desc.fWidth = width;
1931 desc.fHeight = height;
1932 desc.fSampleCnt = fRenderTarget->numSamples();
1933
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001934 GrTexture* texture;
1935 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001936 // Skia's convention is to only clear a device if it is non-opaque.
1937 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001938
1939#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1940 // layers are never draw in repeat modes, so we can request an approx
1941 // match and ignore any padding.
1942 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1943 GrContext::kApprox_ScratchTexMatch :
1944 GrContext::kExact_ScratchTexMatch;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001945 texture = fContext->lockScratchTexture(desc, matchType);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001946#else
1947 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1948 texture = tunref.get();
1949#endif
1950 if (texture) {
1951 return SkNEW_ARGS(SkGpuDevice,(fContext,
1952 texture,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001953 needClear));
1954 } else {
1955 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1956 width, height);
1957 return NULL;
1958 }
1959}
1960
1961SkGpuDevice::SkGpuDevice(GrContext* context,
1962 GrTexture* texture,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001963 bool needClear)
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001964 : SkDevice(make_bitmap(context, texture->asRenderTarget())) {
1965
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001966 GrAssert(texture && texture->asRenderTarget());
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001967 this->initFromRenderTarget(context, texture->asRenderTarget());
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001968 fCached = true;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001969 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001970}