blob: 322748e2d2c116a28697a6e13266982fbf230996 [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.com8090e652012-08-28 15:07:11 +0000175 this->initFromRenderTarget(context, texture->asRenderTarget(), false);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000176}
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.com8090e652012-08-28 15:07:11 +0000180 this->initFromRenderTarget(context, renderTarget, false);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000181}
182
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000183void SkGpuDevice::initFromRenderTarget(GrContext* context,
bsalomon@google.com8090e652012-08-28 15:07:11 +0000184 GrRenderTarget* renderTarget,
185 bool cached) {
reed@google.comaf951c92011-06-16 19:10:39 +0000186 fNeedPrepareRenderTarget = false;
187 fDrawProcs = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000188
reed@google.comaf951c92011-06-16 19:10:39 +0000189 fContext = context;
190 fContext->ref();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000191
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.com8090e652012-08-28 15:07:11 +0000207 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (surface, cached));
bsalomon@google.coma2921122012-08-28 12:34:17 +0000208
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
reed@google.comac10a2d2010-12-22 21:39:39 +0000224 fRenderTarget = NULL;
225 fNeedClear = false;
226
reed@google.comaf951c92011-06-16 19:10:39 +0000227 if (config != SkBitmap::kRGB_565_Config) {
228 config = SkBitmap::kARGB_8888_Config;
229 }
230 SkBitmap bm;
231 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000232
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000233 GrTextureDesc desc;
234 desc.fFlags = kRenderTarget_GrTextureFlagBit;
235 desc.fWidth = width;
236 desc.fHeight = height;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000237 desc.fConfig = SkBitmapConfig2GrPixelConfig(bm.config());
reed@google.comac10a2d2010-12-22 21:39:39 +0000238
bsalomon@google.coma2921122012-08-28 12:34:17 +0000239 SkAutoTUnref<GrTexture> texture(fContext->createUncachedTexture(desc, NULL, 0));
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000240
bsalomon@google.coma2921122012-08-28 12:34:17 +0000241 if (NULL != texture) {
242 fRenderTarget = texture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000243 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000244
reed@google.comaf951c92011-06-16 19:10:39 +0000245 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000246
reed@google.comaf951c92011-06-16 19:10:39 +0000247 // wrap the bitmap with a pixelref to expose our texture
bsalomon@google.coma2921122012-08-28 12:34:17 +0000248 SkGrPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (texture));
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000249 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000250 } else {
251 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
252 width, height);
253 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000254 }
255}
256
257SkGpuDevice::~SkGpuDevice() {
258 if (fDrawProcs) {
259 delete fDrawProcs;
260 }
261
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000262 // The SkGpuDevice gives the context the render target (e.g., in gainFocus)
rmistry@google.comd6176b02012-08-23 18:14:13 +0000263 // This call gives the context a chance to relinquish it
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000264 fContext->setRenderTarget(NULL);
265
bsalomon@google.coma2921122012-08-28 12:34:17 +0000266 SkSafeUnref(fRenderTarget);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000267 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000268}
269
reed@google.comac10a2d2010-12-22 21:39:39 +0000270///////////////////////////////////////////////////////////////////////////////
271
272void SkGpuDevice::makeRenderTargetCurrent() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000273 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000274 fContext->setRenderTarget(fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000275 fNeedPrepareRenderTarget = true;
276}
277
278///////////////////////////////////////////////////////////////////////////////
279
bsalomon@google.comc4364992011-11-07 15:54:49 +0000280namespace {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000281GrPixelConfig config8888_to_grconfig_and_flags(SkCanvas::Config8888 config8888, uint32_t* flags) {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000282 switch (config8888) {
283 case SkCanvas::kNative_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000284 *flags = 0;
285 return kSkia8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000286 case SkCanvas::kNative_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000287 *flags = GrContext::kUnpremul_PixelOpsFlag;
288 return kSkia8888_PM_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000289 case SkCanvas::kBGRA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000290 *flags = 0;
291 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000292 case SkCanvas::kBGRA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000293 *flags = GrContext::kUnpremul_PixelOpsFlag;
294 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000295 case SkCanvas::kRGBA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000296 *flags = 0;
297 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000298 case SkCanvas::kRGBA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000299 *flags = GrContext::kUnpremul_PixelOpsFlag;
300 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000301 default:
302 GrCrash("Unexpected Config8888.");
bsalomon@google.comccaa0022012-09-25 19:55:07 +0000303 *flags = 0; // suppress warning
bsalomon@google.comc4364992011-11-07 15:54:49 +0000304 return kSkia8888_PM_GrPixelConfig;
305 }
306}
307}
308
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000309bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
310 int x, int y,
311 SkCanvas::Config8888 config8888) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000312 DO_DEFERRED_CLEAR;
bsalomon@google.com910267d2011-11-02 20:06:25 +0000313 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
314 SkASSERT(!bitmap.isNull());
315 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000316
bsalomon@google.com910267d2011-11-02 20:06:25 +0000317 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000318 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000319 uint32_t flags;
320 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000321 return fContext->readRenderTargetPixels(fRenderTarget,
322 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000323 bitmap.width(),
324 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000325 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000326 bitmap.getPixels(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000327 bitmap.rowBytes(),
328 flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000329}
330
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000331void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
332 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000333 SkAutoLockPixels alp(bitmap);
334 if (!bitmap.readyToDraw()) {
335 return;
336 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000337
338 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000339 uint32_t flags;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000340 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000341 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000342 } else {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000343 flags = 0;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000344 config= SkBitmapConfig2GrPixelConfig(bitmap.config());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000345 }
346
bsalomon@google.com6f379512011-11-16 20:36:03 +0000347 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000348 config, bitmap.getPixels(), bitmap.rowBytes(), flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000349}
350
robertphillips@google.com46f93502012-08-07 15:38:08 +0000351namespace {
352void purgeClipCB(int genID, void* data) {
353 GrContext* context = (GrContext*) data;
354
355 if (SkClipStack::kInvalidGenID == genID ||
356 SkClipStack::kEmptyGenID == genID ||
357 SkClipStack::kWideOpenGenID == genID) {
358 // none of these cases will have a cached clip mask
359 return;
360 }
361
362}
363};
364
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000365void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
366 INHERITED::onAttachToCanvas(canvas);
367
368 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000369 fClipData.fClipStack = canvas->getClipStack();
robertphillips@google.com46f93502012-08-07 15:38:08 +0000370
371 fClipData.fClipStack->addPurgeClipCallback(purgeClipCB, fContext);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000372}
373
374void SkGpuDevice::onDetachFromCanvas() {
375 INHERITED::onDetachFromCanvas();
376
robertphillips@google.com46f93502012-08-07 15:38:08 +0000377 // TODO: iterate through the clip stack and clean up any cached clip masks
378 fClipData.fClipStack->removePurgeClipCallback(purgeClipCB, fContext);
379
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000380 fClipData.fClipStack = NULL;
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000381}
382
robertphillips@google.com607fe072012-07-24 13:54:00 +0000383#ifdef SK_DEBUG
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000384static void check_bounds(const GrClipData& clipData,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000385 const SkRegion& clipRegion,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000386 int renderTargetWidth,
387 int renderTargetHeight) {
388
robertphillips@google.com7b112892012-07-31 15:18:21 +0000389 SkIRect devBound;
390
391 devBound.setLTRB(0, 0, renderTargetWidth, renderTargetHeight);
392
robertphillips@google.com607fe072012-07-24 13:54:00 +0000393 SkClipStack::BoundsType boundType;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000394 SkRect canvTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000395
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000396 clipData.fClipStack->getBounds(&canvTemp, &boundType);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000397 if (SkClipStack::kNormal_BoundsType == boundType) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000398 SkIRect devTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000399
robertphillips@google.com7b112892012-07-31 15:18:21 +0000400 canvTemp.roundOut(&devTemp);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000401
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000402 devTemp.offset(-clipData.fOrigin.fX, -clipData.fOrigin.fY);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000403
robertphillips@google.com7b112892012-07-31 15:18:21 +0000404 if (!devBound.intersect(devTemp)) {
405 devBound.setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000406 }
407 }
408
robertphillips@google.com768fee82012-08-02 12:42:43 +0000409 GrAssert(devBound.contains(clipRegion.getBounds()));
robertphillips@google.com607fe072012-07-24 13:54:00 +0000410}
411#endif
412
reed@google.comac10a2d2010-12-22 21:39:39 +0000413///////////////////////////////////////////////////////////////////////////////
414
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000415static void set_matrix_and_clip(GrContext* context, const SkMatrix& matrix,
416 GrClipData& clipData,
417 const SkRegion& clipRegion,
418 const SkIPoint& origin,
419 int renderTargetWidth, int renderTargetHeight) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000420 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000421
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000422 clipData.fOrigin = origin;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000423
424#ifdef SK_DEBUG
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000425 check_bounds(clipData, clipRegion,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000426 renderTargetWidth, renderTargetHeight);
427#endif
428
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000429 context->setClip(&clipData);
reed@google.comac10a2d2010-12-22 21:39:39 +0000430}
431
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000432// call this every draw call, to ensure that the context reflects our state,
reed@google.comac10a2d2010-12-22 21:39:39 +0000433// and not the state from some other canvas/device
434void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000435 GrAssert(NULL != fClipData.fClipStack);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000436
reed@google.comac10a2d2010-12-22 21:39:39 +0000437 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000438 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000439
440 fContext->setRenderTarget(fRenderTarget);
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000441 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000442
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000443 set_matrix_and_clip(fContext, *draw.fMatrix,
444 fClipData, *draw.fClip, this->getOrigin(),
445 fRenderTarget->width(), fRenderTarget->height());
reed@google.comac10a2d2010-12-22 21:39:39 +0000446 fNeedPrepareRenderTarget = false;
447 }
448}
449
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000450void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
451 const SkClipStack& clipStack) {
452 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
453 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000454 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000455}
456
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000457void SkGpuDevice::gainFocus(const SkMatrix& matrix, const SkRegion& clip) {
458
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000459 GrAssert(NULL != fClipData.fClipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000460
reed@google.comac10a2d2010-12-22 21:39:39 +0000461 fContext->setRenderTarget(fRenderTarget);
462
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000463 this->INHERITED::gainFocus(matrix, clip);
reed@google.comac10a2d2010-12-22 21:39:39 +0000464
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000465 set_matrix_and_clip(fContext, matrix, fClipData, clip, this->getOrigin(),
466 fRenderTarget->width(), fRenderTarget->height());
reed@google.comac10a2d2010-12-22 21:39:39 +0000467
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000468 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000469}
470
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000471SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000472 DO_DEFERRED_CLEAR;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000473 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000474}
475
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000476bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
bsalomon@google.coma2921122012-08-28 12:34:17 +0000477 GrTexture* texture = fRenderTarget->asTexture();
478 if (NULL != texture) {
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000479 paint->textureSampler(kBitmapTextureIdx)->setCustomStage(
bsalomon@google.coma2921122012-08-28 12:34:17 +0000480 SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000481 return true;
482 }
483 return false;
484}
485
486///////////////////////////////////////////////////////////////////////////////
487
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000488SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
489SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
490SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
491SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
492SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
493 shader_type_mismatch);
rileya@google.com3e332582012-07-03 13:43:35 +0000494SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
495 shader_type_mismatch);
rileya@google.com22e57f92012-07-19 15:16:19 +0000496SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
497SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000498
bsalomon@google.com84405e02012-03-05 19:57:21 +0000499namespace {
500
501// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
502// justAlpha indicates that skPaint's alpha should be used rather than the color
503// Callers may subsequently modify the GrPaint. Setting constantColor indicates
504// that the final paint will draw the same color at every pixel. This allows
505// an optimization where the the color filter can be applied to the skPaint's
twiz@google.com58071162012-07-18 21:41:50 +0000506// color once while converting to GrPaint and then ignored.
507inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
508 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000509 bool justAlpha,
510 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000511 SkGpuDevice::SkAutoCachedTexture* act,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000512 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000513
514 grPaint->fDither = skPaint.isDither();
515 grPaint->fAntiAlias = skPaint.isAntiAlias();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000516 grPaint->fCoverage = 0xFF;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000517
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000518 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
519 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000520
521 SkXfermode* mode = skPaint.getXfermode();
522 if (mode) {
523 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000524 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000525#if 0
526 return false;
527#endif
528 }
529 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000530 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
531 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
532
bsalomon@google.com5782d712011-01-21 21:03:59 +0000533 if (justAlpha) {
534 uint8_t alpha = skPaint.getAlpha();
535 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000536 // justAlpha is currently set to true only if there is a texture,
537 // so constantColor should not also be true.
538 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000539 } else {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000540 grPaint->fColor = SkColor2GrColor(skPaint.getColor());
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000541 GrAssert(!grPaint->isTextureStageEnabled(kShaderTextureIdx));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000542 }
Scroggo97c88c22011-05-11 14:05:25 +0000543 SkColorFilter* colorFilter = skPaint.getColorFilter();
544 SkColor color;
545 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000546 SkScalar matrix[20];
twiz@google.com58071162012-07-18 21:41:50 +0000547 SkBitmap colorTransformTable;
bsalomon@google.com0d944822012-08-16 15:06:57 +0000548 grPaint->resetColorFilter();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000549 // TODO: SkColorFilter::asCustomStage()
Scroggo97c88c22011-05-11 14:05:25 +0000550 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000551 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000552 if (!constantColor) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000553 grPaint->fColorFilterColor = SkColor2GrColor(color);
Scroggod757df22011-05-16 13:11:16 +0000554 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000555 } else {
556 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
rileya@google.com24f3ad12012-07-18 21:47:40 +0000557 grPaint->fColor = SkColor2GrColor(filtered);
Scroggod757df22011-05-16 13:11:16 +0000558 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000559 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
560 grPaint->fColorMatrixEnabled = true;
561 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
562 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000563 } else if (colorFilter != NULL && colorFilter->asComponentTable(&colorTransformTable)) {
twiz@google.com58071162012-07-18 21:41:50 +0000564 grPaint->resetColorFilter();
565
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000566 // pass NULL because the color table effect doesn't use tiling or filtering.
567 GrTexture* texture = act->set(dev, colorTransformTable, NULL);
twiz@google.com58071162012-07-18 21:41:50 +0000568 GrSamplerState* colorSampler = grPaint->textureSampler(kColorFilterTextureIdx);
bsalomon@google.comb8670992012-07-25 21:27:09 +0000569 colorSampler->reset();
bsalomon@google.comcbd0ad92012-07-20 15:09:31 +0000570 colorSampler->setCustomStage(SkNEW_ARGS(GrColorTableEffect, (texture)))->unref();
Scroggo97c88c22011-05-11 14:05:25 +0000571 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000572 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000573}
574
bsalomon@google.com84405e02012-03-05 19:57:21 +0000575// This function is similar to skPaint2GrPaintNoShader but also converts
576// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
577// be used is set on grPaint and returned in param act. constantColor has the
578// same meaning as in skPaint2GrPaintNoShader.
579inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
580 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000581 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000582 SkGpuDevice::SkAutoCachedTexture textures[GrPaint::kMaxTextures],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000583 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000584 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000585 if (NULL == shader) {
twiz@google.com58071162012-07-18 21:41:50 +0000586 return skPaint2GrPaintNoShader(dev,
587 skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000588 false,
589 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000590 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000591 grPaint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000592 } else if (!skPaint2GrPaintNoShader(dev, skPaint, true, false,
twiz@google.com58071162012-07-18 21:41:50 +0000593 &textures[kColorFilterTextureIdx], grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000594 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000595 }
596
rileya@google.com91f319c2012-07-25 17:18:31 +0000597 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
598 GrCustomStage* stage = shader->asNewCustomStage(dev->context(), sampler);
599
600 if (NULL != stage) {
601 sampler->setCustomStage(stage)->unref();
602 SkMatrix localM;
603 if (shader->getLocalMatrix(&localM)) {
604 SkMatrix inverse;
605 if (localM.invert(&inverse)) {
606 sampler->matrix()->preConcat(inverse);
607 }
608 }
609 return true;
610 }
611
reed@google.comac10a2d2010-12-22 21:39:39 +0000612 SkBitmap bitmap;
rileya@google.com91f319c2012-07-25 17:18:31 +0000613 SkMatrix* matrix = sampler->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000614 SkShader::TileMode tileModes[2];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000615 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
rileya@google.com91f319c2012-07-25 17:18:31 +0000616 tileModes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000617
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000618 if (SkShader::kNone_BitmapType == bmptype) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000619 SkShader::GradientInfo info;
620 SkColor color;
621
622 info.fColors = &color;
623 info.fColorOffsets = NULL;
624 info.fColorCount = 1;
625 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
626 SkPaint copy(skPaint);
627 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000628 // modulate the paint alpha by the shader's solid color alpha
629 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
630 copy.setColor(SkColorSetA(color, newA));
twiz@google.com58071162012-07-18 21:41:50 +0000631 return skPaint2GrPaintNoShader(dev,
632 copy,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000633 false,
634 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000635 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000636 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000637 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000638 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000639 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000640
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000641 // Must set wrap and filter on the sampler before requesting a texture.
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000642 GrTextureParams params(tileModes, skPaint.isFilterBitmap());
643 GrTexture* texture = textures[kShaderTextureIdx].set(dev, bitmap, &params);
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000644
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000645 if (NULL == texture) {
646 SkDebugf("Couldn't convert bitmap to texture.\n");
647 return false;
648 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000649
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000650 sampler->reset();
651 sampler->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect, (texture, params)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000652
reed@google.comac10a2d2010-12-22 21:39:39 +0000653 // since our texture coords will be in local space, we wack the texture
654 // matrix to map them back into 0...1 before we load it
655 SkMatrix localM;
656 if (shader->getLocalMatrix(&localM)) {
657 SkMatrix inverse;
658 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000659 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000660 }
661 }
662 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000663 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
664 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000665 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000666 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000667
668 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000669}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000670}
reed@google.comac10a2d2010-12-22 21:39:39 +0000671
672///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000673void SkGpuDevice::clear(SkColor color) {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000674 fContext->clear(NULL, color, fRenderTarget);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000675}
676
reed@google.comac10a2d2010-12-22 21:39:39 +0000677void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
678 CHECK_SHOULD_DRAW(draw);
679
bsalomon@google.com5782d712011-01-21 21:03:59 +0000680 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000681 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000682 if (!skPaint2GrPaintShader(this,
683 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000684 true,
twiz@google.com58071162012-07-18 21:41:50 +0000685 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000686 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000687 return;
688 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000689
690 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000691}
692
693// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000694static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000695 kPoints_GrPrimitiveType,
696 kLines_GrPrimitiveType,
697 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000698};
699
700void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000701 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000702 CHECK_SHOULD_DRAW(draw);
703
704 SkScalar width = paint.getStrokeWidth();
705 if (width < 0) {
706 return;
707 }
708
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000709 // we only handle hairlines and paints without path effects or mask filters,
710 // else we let the SkDraw call our drawPath()
711 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000712 draw.drawPoints(mode, count, pts, paint, true);
713 return;
714 }
715
bsalomon@google.com5782d712011-01-21 21:03:59 +0000716 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000717 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000718 if (!skPaint2GrPaintShader(this,
719 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000720 true,
twiz@google.com58071162012-07-18 21:41:50 +0000721 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000722 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000723 return;
724 }
725
bsalomon@google.com5782d712011-01-21 21:03:59 +0000726 fContext->drawVertices(grPaint,
727 gPointMode2PrimtiveType[mode],
728 count,
729 (GrPoint*)pts,
730 NULL,
731 NULL,
732 NULL,
733 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000734}
735
reed@google.comc9aa5872011-04-05 21:05:37 +0000736///////////////////////////////////////////////////////////////////////////////
737
reed@google.comac10a2d2010-12-22 21:39:39 +0000738void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
739 const SkPaint& paint) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000740 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000741 CHECK_SHOULD_DRAW(draw);
742
bungeman@google.com79bd8772011-07-18 15:34:08 +0000743 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000744 SkScalar width = paint.getStrokeWidth();
745
746 /*
747 We have special code for hairline strokes, miter-strokes, and fills.
748 Anything else we just call our path code.
749 */
750 bool usePath = doStroke && width > 0 &&
751 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000752 // another two reasons we might need to call drawPath...
753 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000754 usePath = true;
755 }
reed@google.com67db6642011-05-26 11:46:35 +0000756 // until we aa rotated rects...
757 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
758 usePath = true;
759 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000760 // small miter limit means right angles show bevel...
761 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
762 paint.getStrokeMiter() < SK_ScalarSqrt2)
763 {
764 usePath = true;
765 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000766 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000767 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
768 usePath = true;
769 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000770
771 if (usePath) {
772 SkPath path;
773 path.addRect(rect);
774 this->drawPath(draw, path, paint, NULL, true);
775 return;
776 }
777
778 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000779 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000780 if (!skPaint2GrPaintShader(this,
781 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000782 true,
twiz@google.com58071162012-07-18 21:41:50 +0000783 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000784 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000785 return;
786 }
reed@google.com20efde72011-05-09 17:00:02 +0000787 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000788}
789
reed@google.com69302852011-02-16 18:08:07 +0000790#include "SkMaskFilter.h"
791#include "SkBounder.h"
792
bsalomon@google.com85003222012-03-28 14:44:37 +0000793///////////////////////////////////////////////////////////////////////////////
794
795// helpers for applying mask filters
796namespace {
797
798GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000799 switch (fillType) {
800 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000801 return kWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000802 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000803 return kEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000804 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000805 return kInverseWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000806 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000807 return kInverseEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000808 default:
809 SkDebugf("Unsupported path fill type\n");
bsalomon@google.com47059542012-06-06 20:51:20 +0000810 return kHairLine_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000811 }
812}
813
bsalomon@google.com85003222012-03-28 14:44:37 +0000814// We prefer to blur small rect with small radius via CPU.
815#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
816#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
817inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
818 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
819 rect.height() <= MIN_GPU_BLUR_SIZE &&
820 radius <= MIN_GPU_BLUR_RADIUS) {
821 return true;
822 }
823 return false;
824}
825
826bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
827 SkMaskFilter* filter, const SkMatrix& matrix,
828 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000829 GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000830#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000831 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000832#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000833 SkMaskFilter::BlurInfo info;
834 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000835 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000836 return false;
837 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000838 SkScalar radius = info.fIgnoreTransform ? info.fRadius
839 : matrix.mapRadius(info.fRadius);
840 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000841 if (radius <= 0) {
842 return false;
843 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000844
845 SkRect srcRect = path.getBounds();
846 if (shouldDrawBlurWithCPU(srcRect, radius)) {
847 return false;
848 }
849
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000850 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000851 float sigma3 = sigma * 3.0f;
852
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000853 SkRect clipRect;
854 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000855
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000856 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000857 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
858 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000859 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000860 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000861 SkIRect finalIRect;
862 finalRect.roundOut(&finalIRect);
863 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000864 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000865 }
866 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000867 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000868 }
869 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000870 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000871 GrTextureDesc desc;
872 desc.fFlags = kRenderTarget_GrTextureFlagBit;
873 desc.fWidth = SkScalarCeilToInt(srcRect.width());
874 desc.fHeight = SkScalarCeilToInt(srcRect.height());
875 // We actually only need A8, but it often isn't supported as a
876 // render target so default to RGBA_8888
bsalomon@google.com0342a852012-08-20 19:22:38 +0000877 desc.fConfig = kRGBA_8888_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000878
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000879 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
880 desc.fConfig = kAlpha_8_GrPixelConfig;
881 }
882
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000883 GrAutoScratchTexture pathEntry(context, desc);
884 GrTexture* pathTexture = pathEntry.texture();
885 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000886 return false;
887 }
888 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000889 // Once this code moves into GrContext, this should be changed to use
890 // an AutoClipRestore.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000891 const GrClipData* oldClipData = context->getClip();
892
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000893 context->setRenderTarget(pathTexture->asRenderTarget());
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000894
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000895 SkClipStack newClipStack(srcRect);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000896 GrClipData newClipData;
897 newClipData.fClipStack = &newClipStack;
898 context->setClip(&newClipData);
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000899
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000900 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000901 GrPaint tempPaint;
902 tempPaint.reset();
903
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000904 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000905 tempPaint.fAntiAlias = grp->fAntiAlias;
906 if (tempPaint.fAntiAlias) {
907 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
908 // blend coeff of zero requires dual source blending support in order
909 // to properly blend partially covered pixels. This means the AA
910 // code path may not be taken. So we use a dst blend coeff of ISA. We
911 // could special case AA draws to a dst surface with known alpha=0 to
912 // use a zero dst coeff when dual source blending isn't available.
bsalomon@google.com47059542012-06-06 20:51:20 +0000913 tempPaint.fSrcBlendCoeff = kOne_GrBlendCoeff;
914 tempPaint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000915 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000916 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000917 context->drawPath(tempPaint, path, pathFillType, &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000918
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000919 // If we're doing a normal blur, we can clobber the pathTexture in the
920 // gaussianBlur. Otherwise, we need to save it for later compositing.
921 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +0000922 SkAutoTUnref<GrTexture> blurTexture(context->gaussianBlur(
923 pathTexture, isNormalBlur, srcRect, sigma, sigma));
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000924
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000925 if (!isNormalBlur) {
926 GrPaint paint;
927 paint.reset();
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000928 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
929 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000930 // Blend pathTexture over blurTexture.
931 context->setRenderTarget(blurTexture->asRenderTarget());
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000932 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS
933 (GrSingleTextureEffect, (pathTexture)))->unref();
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000934 if (SkMaskFilter::kInner_BlurType == blurType) {
935 // inner: dst = dst * src
bsalomon@google.com47059542012-06-06 20:51:20 +0000936 paint.fSrcBlendCoeff = kDC_GrBlendCoeff;
937 paint.fDstBlendCoeff = kZero_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000938 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
939 // solid: dst = src + dst - src * dst
940 // = (1 - dst) * src + 1 * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000941 paint.fSrcBlendCoeff = kIDC_GrBlendCoeff;
942 paint.fDstBlendCoeff = kOne_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000943 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
944 // outer: dst = dst * (1 - src)
945 // = 0 * src + (1 - src) * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000946 paint.fSrcBlendCoeff = kZero_GrBlendCoeff;
947 paint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000948 }
949 context->drawRect(paint, srcRect);
950 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000951 context->setRenderTarget(oldRenderTarget);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000952 context->setClip(oldClipData);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000953
bsalomon@google.come3d32162012-07-20 13:37:06 +0000954 if (!grp->preConcatSamplerMatricesWithInverse(matrix)) {
955 return false;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000956 }
957
958 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
959 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000960 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
bsalomon@google.com97912912011-12-06 16:30:36 +0000961 grp->maskSampler(MASK_IDX)->reset();
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000962 grp->maskSampler(MASK_IDX)->setCustomStage(
963 SkNEW_ARGS(GrSingleTextureEffect, (blurTexture)))->unref();
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000964 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
965 -finalRect.fTop);
966 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
967 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000968 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000969 return true;
970}
971
bsalomon@google.com85003222012-03-28 14:44:37 +0000972bool drawWithMaskFilter(GrContext* context, const SkPath& path,
973 SkMaskFilter* filter, const SkMatrix& matrix,
974 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000975 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000976 SkMask srcM, dstM;
977
978 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000979 SkMask::kComputeBoundsAndRenderImage_CreateMode,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000980 style)) {
reed@google.com69302852011-02-16 18:08:07 +0000981 return false;
982 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000983 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000984
985 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
986 return false;
987 }
988 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000989 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000990
991 if (clip.quickReject(dstM.fBounds)) {
992 return false;
993 }
994 if (bounder && !bounder->doIRect(dstM.fBounds)) {
995 return false;
996 }
997
998 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
999 // the current clip (and identity matrix) and grpaint settings
1000
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001001 GrContext::AutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +00001002
bsalomon@google.come3d32162012-07-20 13:37:06 +00001003 if (!grp->preConcatSamplerMatricesWithInverse(matrix)) {
1004 return false;
1005 }
1006
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001007 GrTextureDesc desc;
1008 desc.fWidth = dstM.fBounds.width();
1009 desc.fHeight = dstM.fBounds.height();
1010 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +00001011
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001012 GrAutoScratchTexture ast(context, desc);
1013 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001014
reed@google.com69302852011-02-16 18:08:07 +00001015 if (NULL == texture) {
1016 return false;
1017 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001018 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001019 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +00001020
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001021 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
1022 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001023 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
bsalomon@google.com97912912011-12-06 16:30:36 +00001024 grp->maskSampler(MASK_IDX)->reset();
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001025 grp->maskSampler(MASK_IDX)->setCustomStage(
1026 SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001027 GrRect d;
1028 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +00001029 GrIntToScalar(dstM.fBounds.fTop),
1030 GrIntToScalar(dstM.fBounds.fRight),
1031 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001032
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001033 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
1034 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
1035 -dstM.fBounds.fTop*SK_Scalar1);
1036 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001037 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +00001038 return true;
1039}
reed@google.com69302852011-02-16 18:08:07 +00001040
bsalomon@google.com85003222012-03-28 14:44:37 +00001041}
1042
1043///////////////////////////////////////////////////////////////////////////////
1044
reed@google.com0c219b62011-02-16 21:31:18 +00001045void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001046 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +00001047 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +00001048 CHECK_FOR_NODRAW_ANNOTATION(paint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001049 CHECK_SHOULD_DRAW(draw);
1050
reed@google.comfe626382011-09-21 13:50:35 +00001051 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001052
bsalomon@google.com5782d712011-01-21 21:03:59 +00001053 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001054 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001055 if (!skPaint2GrPaintShader(this,
1056 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001057 true,
twiz@google.com58071162012-07-18 21:41:50 +00001058 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001059 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001060 return;
1061 }
1062
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +00001063 // can we cheat, and threat a thin stroke as a hairline w/ coverage
1064 // if we can, we draw lots faster (raster device does this same test)
1065 SkScalar hairlineCoverage;
1066 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
1067 doFill = false;
1068 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
1069 grPaint.fCoverage);
1070 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001071
reed@google.comfe626382011-09-21 13:50:35 +00001072 // If we have a prematrix, apply it to the path, optimizing for the case
1073 // where the original path can in fact be modified in place (even though
1074 // its parameter type is const).
1075 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1076 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001077
1078 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001079 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001080
reed@google.come3445642011-02-16 23:20:39 +00001081 if (!pathIsMutable) {
1082 result = &tmpPath;
1083 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001084 }
reed@google.come3445642011-02-16 23:20:39 +00001085 // should I push prePathMatrix on our MV stack temporarily, instead
1086 // of applying it here? See SkDraw.cpp
1087 pathPtr->transform(*prePathMatrix, result);
1088 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001089 }
reed@google.com0c219b62011-02-16 21:31:18 +00001090 // at this point we're done with prePathMatrix
1091 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001092
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001093 if (paint.getPathEffect() ||
1094 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001095 // it is safe to use tmpPath here, even if we already used it for the
1096 // prepathmatrix, since getFillPath can take the same object for its
1097 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001098 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001099 pathPtr = &tmpPath;
1100 }
1101
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001102 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001103 // avoid possibly allocating a new path in transform if we can
1104 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1105
1106 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001107 pathPtr->transform(*draw.fMatrix, devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001108 GrPathFill pathFillType = doFill ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001109 skToGrFillType(devPathPtr->getFillType()) : kHairLine_GrPathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001110 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001111 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001112 &grPaint, pathFillType)) {
rmistry@google.comd6176b02012-08-23 18:14:13 +00001113 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001114 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001115 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001116 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001117 &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001118 }
reed@google.com69302852011-02-16 18:08:07 +00001119 return;
1120 }
reed@google.com69302852011-02-16 18:08:07 +00001121
bsalomon@google.com47059542012-06-06 20:51:20 +00001122 GrPathFill fill = kHairLine_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001123
reed@google.com0c219b62011-02-16 21:31:18 +00001124 if (doFill) {
1125 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001126 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001127 fill = kWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001128 break;
1129 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001130 fill = kEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001131 break;
1132 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001133 fill = kInverseWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001134 break;
1135 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001136 fill = kInverseEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001137 break;
1138 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001139 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001140 return;
1141 }
1142 }
1143
reed@google.com07f3ee12011-05-16 17:21:57 +00001144 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001145}
1146
bsalomon@google.comfb309512011-11-30 14:13:48 +00001147namespace {
1148
1149inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1150 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1151 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1152 return tilesX * tilesY;
1153}
1154
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001155inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001156 const SkIRect* srcRectPtr,
1157 int maxTextureSize) {
1158 static const int kSmallTileSize = 1 << 10;
1159 if (maxTextureSize <= kSmallTileSize) {
1160 return maxTextureSize;
1161 }
1162
1163 size_t maxTexTotalTileSize;
1164 size_t smallTotalTileSize;
1165
1166 if (NULL == srcRectPtr) {
1167 int w = bitmap.width();
1168 int h = bitmap.height();
1169 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1170 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1171 } else {
1172 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1173 srcRectPtr->fTop,
1174 srcRectPtr->fRight,
1175 srcRectPtr->fBottom,
1176 maxTextureSize);
1177 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1178 srcRectPtr->fTop,
1179 srcRectPtr->fRight,
1180 srcRectPtr->fBottom,
1181 kSmallTileSize);
1182 }
1183 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1184 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1185
1186 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1187 return kSmallTileSize;
1188 } else {
1189 return maxTextureSize;
1190 }
1191}
1192}
1193
1194bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001195 const GrTextureParams& params,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001196 const SkIRect* srcRectPtr,
1197 int* tileSize) const {
1198 SkASSERT(NULL != tileSize);
1199
1200 // if bitmap is explictly texture backed then just use the texture
1201 if (NULL != bitmap.getTexture()) {
1202 return false;
1203 }
1204 // if it's larger than the max texture size, then we have no choice but
1205 // tiling
1206 const int maxTextureSize = fContext->getMaxTextureSize();
1207 if (bitmap.width() > maxTextureSize ||
1208 bitmap.height() > maxTextureSize) {
1209 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1210 return true;
1211 }
1212 // if we are going to have to draw the whole thing, then don't tile
1213 if (NULL == srcRectPtr) {
1214 return false;
1215 }
1216 // if the entire texture is already in our cache then no reason to tile it
bsalomon@google.comb8670992012-07-25 21:27:09 +00001217 if (this->isBitmapInTextureCache(bitmap, params)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001218 return false;
1219 }
1220
1221 // At this point we know we could do the draw by uploading the entire bitmap
1222 // as a texture. However, if the texture would be large compared to the
1223 // cache size and we don't require most of it for this draw then tile to
1224 // reduce the amount of upload and cache spill.
1225
1226 // assumption here is that sw bitmap size is a good proxy for its size as
1227 // a texture
1228 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001229 size_t cacheSize;
1230 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001231 if (bmpSize < cacheSize / 2) {
1232 return false;
1233 }
1234
1235 SkFixed fracUsed =
1236 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1237 (srcRectPtr->height() << 16) / bitmap.height());
1238 if (fracUsed <= SK_FixedHalf) {
1239 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1240 return true;
1241 } else {
1242 return false;
1243 }
1244}
1245
reed@google.comac10a2d2010-12-22 21:39:39 +00001246void SkGpuDevice::drawBitmap(const SkDraw& draw,
1247 const SkBitmap& bitmap,
1248 const SkIRect* srcRectPtr,
1249 const SkMatrix& m,
1250 const SkPaint& paint) {
1251 CHECK_SHOULD_DRAW(draw);
1252
1253 SkIRect srcRect;
1254 if (NULL == srcRectPtr) {
1255 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1256 } else {
1257 srcRect = *srcRectPtr;
1258 }
1259
junov@google.comd935cfb2011-06-27 20:48:23 +00001260 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001261 // Convert the bitmap to a shader so that the rect can be drawn
1262 // through drawRect, which supports mask filters.
1263 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001264 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001265 if (srcRectPtr) {
1266 if (!bitmap.extractSubset(&tmp, srcRect)) {
1267 return; // extraction failed
1268 }
1269 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001270 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001271 }
1272 SkPaint paintWithTexture(paint);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001273 paintWithTexture.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
junov@google.comd935cfb2011-06-27 20:48:23 +00001274 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001275 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001276 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001277
junov@google.com1d329782011-07-28 20:10:09 +00001278 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001279 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001280 // also affect the behavior of the mask filter.
1281 SkMatrix drawMatrix;
1282 drawMatrix.setConcat(*draw.fMatrix, m);
1283 SkDraw transformedDraw(draw);
1284 transformedDraw.fMatrix = &drawMatrix;
1285
1286 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1287
junov@google.comd935cfb2011-06-27 20:48:23 +00001288 return;
1289 }
1290
bsalomon@google.com5782d712011-01-21 21:03:59 +00001291 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001292 SkAutoCachedTexture colorLutTexture;
1293 if (!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001294 return;
1295 }
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001296 GrTextureParams params;
1297 params.setBilerp(paint.isFilterBitmap());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001298
bsalomon@google.comfb309512011-11-30 14:13:48 +00001299 int tileSize;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001300 if (!this->shouldTileBitmap(bitmap, params, srcRectPtr, &tileSize)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001301 // take the simple case
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001302 this->internalDrawBitmap(draw, bitmap, srcRect, m, params, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001303 return;
1304 }
1305
1306 // undo the translate done by SkCanvas
1307 int DX = SkMax32(0, srcRect.fLeft);
1308 int DY = SkMax32(0, srcRect.fTop);
1309 // compute clip bounds in local coordinates
1310 SkIRect clipRect;
1311 {
1312 SkRect r;
1313 r.set(draw.fClip->getBounds());
1314 SkMatrix matrix, inverse;
1315 matrix.setConcat(*draw.fMatrix, m);
1316 if (!matrix.invert(&inverse)) {
1317 return;
1318 }
1319 inverse.mapRect(&r);
1320 r.roundOut(&clipRect);
1321 // apply the canvas' translate to our local clip
1322 clipRect.offset(DX, DY);
1323 }
1324
bsalomon@google.comfb309512011-11-30 14:13:48 +00001325 int nx = bitmap.width() / tileSize;
1326 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001327 for (int x = 0; x <= nx; x++) {
1328 for (int y = 0; y <= ny; y++) {
1329 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001330 tileR.set(x * tileSize, y * tileSize,
1331 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001332 if (!SkIRect::Intersects(tileR, clipRect)) {
1333 continue;
1334 }
1335
1336 SkIRect srcR = tileR;
1337 if (!srcR.intersect(srcRect)) {
1338 continue;
1339 }
1340
1341 SkBitmap tmpB;
1342 if (bitmap.extractSubset(&tmpB, tileR)) {
1343 // now offset it to make it "local" to our tmp bitmap
1344 srcR.offset(-tileR.fLeft, -tileR.fTop);
1345
1346 SkMatrix tmpM(m);
1347 {
1348 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1349 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1350 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1351 }
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001352 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, params, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001353 }
1354 }
1355 }
1356}
1357
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001358namespace {
1359
1360bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1361 // detect pixel disalignment
1362 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1363 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
rmistry@google.comd6176b02012-08-23 18:14:13 +00001364 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001365 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1366 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1367 COLOR_BLEED_TOLERANCE &&
1368 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1369 COLOR_BLEED_TOLERANCE) {
1370 return true;
1371 }
1372 return false;
1373}
1374
1375bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1376 const SkMatrix& m) {
1377 // Only gets called if hasAlignedSamples returned false.
1378 // So we can assume that sampling is axis aligned but not texel aligned.
1379 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001380 SkRect innerSrcRect(srcRect), innerTransformedRect,
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001381 outerTransformedRect(transformedRect);
1382 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1383 m.mapRect(&innerTransformedRect, innerSrcRect);
1384
1385 // The gap between outerTransformedRect and innerTransformedRect
1386 // represents the projection of the source border area, which is
1387 // problematic for color bleeding. We must check whether any
1388 // destination pixels sample the border area.
1389 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1390 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1391 SkIRect outer, inner;
1392 outerTransformedRect.round(&outer);
1393 innerTransformedRect.round(&inner);
1394 // If the inner and outer rects round to the same result, it means the
1395 // border does not overlap any pixel centers. Yay!
1396 return inner != outer;
1397}
1398
1399} // unnamed namespace
1400
reed@google.comac10a2d2010-12-22 21:39:39 +00001401/*
1402 * This is called by drawBitmap(), which has to handle images that may be too
1403 * large to be represented by a single texture.
1404 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001405 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1406 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001407 */
1408void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1409 const SkBitmap& bitmap,
1410 const SkIRect& srcRect,
1411 const SkMatrix& m,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001412 const GrTextureParams& params,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001413 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001414 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1415 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001416
reed@google.com9c49bc32011-07-07 13:42:37 +00001417 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001418 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001419 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001420 return;
1421 }
1422
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001423 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001424
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001425 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001426
1427 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001428 SkAutoCachedTexture act(this, bitmap, &params, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001429 if (NULL == texture) {
1430 return;
1431 }
1432
reed@google.com20efde72011-05-09 17:00:02 +00001433 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1434 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001435 GrRect paintRect;
bsalomon@google.com91832162012-03-08 19:53:02 +00001436 float wInv = 1.f / bitmap.width();
1437 float hInv = 1.f / bitmap.height();
1438 paintRect.setLTRB(SkFloatToScalar(srcRect.fLeft * wInv),
1439 SkFloatToScalar(srcRect.fTop * hInv),
1440 SkFloatToScalar(srcRect.fRight * wInv),
1441 SkFloatToScalar(srcRect.fBottom * hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001442
rmistry@google.comd6176b02012-08-23 18:14:13 +00001443 bool needsTextureDomain = false;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001444 if (params.isBilerp()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001445 // Need texture domain if drawing a sub rect.
bsalomon@google.comb8670992012-07-25 21:27:09 +00001446 needsTextureDomain = srcRect.width() < bitmap.width() || srcRect.height() < bitmap.height();
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001447 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1448 // sampling is axis-aligned
1449 GrRect floatSrcRect, transformedRect;
1450 floatSrcRect.set(srcRect);
1451 SkMatrix srcToDeviceMatrix(m);
1452 srcToDeviceMatrix.postConcat(*draw.fMatrix);
1453 srcToDeviceMatrix.mapRect(&transformedRect, floatSrcRect);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001454
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001455 if (hasAlignedSamples(floatSrcRect, transformedRect)) {
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001456 // We could also turn off filtering here (but we already did a cache lookup with
1457 // params).
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001458 needsTextureDomain = false;
1459 } else {
1460 needsTextureDomain = needsTextureDomain &&
1461 mayColorBleed(floatSrcRect, transformedRect, m);
1462 }
1463 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001464 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001465
1466 GrRect textureDomain = GrRect::MakeEmpty();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001467 SkAutoTUnref<GrCustomStage> stage;
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001468 if (needsTextureDomain) {
1469 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001470 GrScalar left, top, right, bottom;
1471 if (srcRect.width() > 1) {
1472 GrScalar border = GR_ScalarHalf / bitmap.width();
1473 left = paintRect.left() + border;
1474 right = paintRect.right() - border;
1475 } else {
1476 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1477 }
1478 if (srcRect.height() > 1) {
1479 GrScalar border = GR_ScalarHalf / bitmap.height();
1480 top = paintRect.top() + border;
1481 bottom = paintRect.bottom() - border;
1482 } else {
1483 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1484 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001485 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001486 stage.reset(SkNEW_ARGS(GrTextureDomainEffect, (texture, textureDomain, params)));
1487 } else {
1488 stage.reset(SkNEW_ARGS(GrSingleTextureEffect, (texture, params)));
junov@google.com6acc9b32011-05-16 18:32:07 +00001489 }
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001490 grPaint->textureSampler(kBitmapTextureIdx)->setCustomStage(stage);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001491 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001492}
1493
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001494namespace {
1495
1496void apply_custom_stage(GrContext* context,
1497 GrTexture* srcTexture,
1498 GrTexture* dstTexture,
1499 const GrRect& rect,
1500 GrCustomStage* stage) {
1501 SkASSERT(srcTexture && srcTexture->getContext() == context);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001502 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001503 GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget());
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001504 GrContext::AutoClip acs(context, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001505
1506 GrMatrix sampleM;
1507 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
1508 GrPaint paint;
1509 paint.reset();
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001510 paint.textureSampler(0)->reset(sampleM);
1511 paint.textureSampler(0)->setCustomStage(stage);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001512 context->drawRect(paint, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001513}
1514
1515};
1516
reed@google.com8926b162012-03-23 15:36:36 +00001517static GrTexture* filter_texture(GrContext* context, GrTexture* texture,
1518 SkImageFilter* filter, const GrRect& rect) {
1519 GrAssert(filter);
1520
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001521 GrTextureDesc desc;
1522 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1523 desc.fWidth = SkScalarCeilToInt(rect.width());
1524 desc.fHeight = SkScalarCeilToInt(rect.height());
bsalomon@google.com0342a852012-08-20 19:22:38 +00001525 desc.fConfig = kRGBA_8888_GrPixelConfig;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001526 GrCustomStage* stage;
reed@google.com8926b162012-03-23 15:36:36 +00001527
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001528 if (filter->canFilterImageGPU()) {
1529 texture = filter->onFilterImageGPU(texture, rect);
1530 } else if (filter->asNewCustomStage(&stage, texture)) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001531 GrAutoScratchTexture dst(context, desc);
1532 apply_custom_stage(context, texture, dst.texture(), rect, stage);
1533 texture = dst.detach();
1534 stage->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001535 }
1536 return texture;
1537}
1538
reed@google.comac10a2d2010-12-22 21:39:39 +00001539void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1540 int left, int top, const SkPaint& paint) {
1541 CHECK_SHOULD_DRAW(draw);
1542
reed@google.com8926b162012-03-23 15:36:36 +00001543 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001544 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1545 return;
1546 }
1547
reed@google.com76dd2772012-01-05 21:15:07 +00001548 int w = bitmap.width();
1549 int h = bitmap.height();
1550
bsalomon@google.com5782d712011-01-21 21:03:59 +00001551 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001552 SkAutoCachedTexture colorLutTexture;
1553 if(!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001554 return;
1555 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001556
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001557 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001558
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001559 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001560
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001561 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001562 sampler->reset();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001563 // draw sprite uses the default texture params
1564 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001565 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1566 (GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001567
reed@google.com8926b162012-03-23 15:36:36 +00001568 SkImageFilter* filter = paint.getImageFilter();
1569 if (NULL != filter) {
1570 GrTexture* filteredTexture = filter_texture(fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001571 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001572 if (filteredTexture) {
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001573 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1574 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001575 texture = filteredTexture;
1576 filteredTexture->unref();
1577 }
reed@google.com76dd2772012-01-05 21:15:07 +00001578 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001579
bsalomon@google.com5782d712011-01-21 21:03:59 +00001580 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001581 GrRect::MakeXYWH(GrIntToScalar(left),
1582 GrIntToScalar(top),
1583 GrIntToScalar(w),
1584 GrIntToScalar(h)),
1585 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1586 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001587}
1588
reed@google.com33535f32012-09-25 15:37:50 +00001589void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1590 const SkRect* src, const SkRect& dst,
1591 const SkPaint& paint) {
1592 SkMatrix matrix;
1593 // Compute matrix from the two rectangles
1594 {
1595 SkRect tmpSrc;
1596 if (src) {
1597 tmpSrc = *src;
1598 // if the extract process clipped off the top or left of the
1599 // original, we adjust for that here to get the position right.
1600 if (tmpSrc.fLeft > 0) {
1601 tmpSrc.fRight -= tmpSrc.fLeft;
1602 tmpSrc.fLeft = 0;
1603 }
1604 if (tmpSrc.fTop > 0) {
1605 tmpSrc.fBottom -= tmpSrc.fTop;
1606 tmpSrc.fTop = 0;
1607 }
1608 } else {
1609 tmpSrc.set(0, 0, SkIntToScalar(bitmap.width()),
1610 SkIntToScalar(bitmap.height()));
1611 }
1612 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1613 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +00001614
reed@google.com33535f32012-09-25 15:37:50 +00001615 // ensure that src is "valid" before we pass it to our internal routines
1616 // and to SkDevice. i.e. sure it is contained inside the original bitmap.
1617 SkIRect isrcStorage;
1618 SkIRect* isrcPtr = NULL;
1619 if (src) {
1620 src->roundOut(&isrcStorage);
1621 if (!isrcStorage.intersect(0, 0, bitmap.width(), bitmap.height())) {
1622 return;
1623 }
1624 isrcPtr = &isrcStorage;
1625 }
1626
1627 this->drawBitmap(draw, bitmap, isrcPtr, matrix, paint);
1628}
1629
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001630void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
reed@google.comac10a2d2010-12-22 21:39:39 +00001631 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001632 // clear of the source device must occur before CHECK_SHOULD_DRAW
1633 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1634 if (dev->fNeedClear) {
1635 // TODO: could check here whether we really need to draw at all
1636 dev->clear(0x0);
1637 }
1638
reed@google.comac10a2d2010-12-22 21:39:39 +00001639 CHECK_SHOULD_DRAW(draw);
1640
bsalomon@google.com5782d712011-01-21 21:03:59 +00001641 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001642 SkAutoCachedTexture colorLutTexture;
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001643 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001644 if (!dev->bindDeviceAsTexture(&grPaint) ||
twiz@google.com58071162012-07-18 21:41:50 +00001645 !skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001646 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001647 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001648
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001649 GrTexture* devTex = grPaint.getTextureSampler(kBitmapTextureIdx).getCustomStage()->texture(0);
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001650 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001651
reed@google.com8926b162012-03-23 15:36:36 +00001652 SkImageFilter* filter = paint.getImageFilter();
1653 if (NULL != filter) {
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001654 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001655 SkIntToScalar(devTex->height()));
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001656 GrTexture* filteredTexture = filter_texture(fContext, devTex, filter, rect);
reed@google.com8926b162012-03-23 15:36:36 +00001657 if (filteredTexture) {
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001658 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1659 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001660 devTex = filteredTexture;
1661 filteredTexture->unref();
1662 }
1663 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001664
bsalomon@google.com5782d712011-01-21 21:03:59 +00001665 const SkBitmap& bm = dev->accessBitmap(false);
1666 int w = bm.width();
1667 int h = bm.height();
1668
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001669 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001670 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1671 GrIntToScalar(y),
1672 GrIntToScalar(w),
1673 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001674
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001675 // The device being drawn may not fill up its texture (saveLayer uses
1676 // the approximate ).
1677 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1678 GR_Scalar1 * h / devTex->height());
1679
1680 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001681}
1682
reed@google.com8926b162012-03-23 15:36:36 +00001683bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001684 if (!filter->asNewCustomStage(NULL, NULL) &&
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001685 !filter->canFilterImageGPU()) {
reed@google.com76dd2772012-01-05 21:15:07 +00001686 return false;
1687 }
reed@google.com8926b162012-03-23 15:36:36 +00001688 return true;
1689}
1690
1691bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1692 const SkMatrix& ctm,
1693 SkBitmap* result, SkIPoint* offset) {
1694 // want explicitly our impl, so guard against a subclass of us overriding it
1695 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001696 return false;
1697 }
reed@google.com8926b162012-03-23 15:36:36 +00001698
1699 SkAutoLockPixels alp(src, !src.getTexture());
1700 if (!src.getTexture() && !src.readyToDraw()) {
1701 return false;
1702 }
1703
1704 GrPaint paint;
1705 paint.reset();
1706
reed@google.com8926b162012-03-23 15:36:36 +00001707 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001708 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1709 // must be pushed upstack.
1710 SkAutoCachedTexture act(this, src, NULL, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001711
1712 result->setConfig(src.config(), src.width(), src.height());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001713 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001714 SkIntToScalar(src.height()));
reed@google.com8926b162012-03-23 15:36:36 +00001715 GrTexture* resultTexture = filter_texture(fContext, texture, filter, rect);
1716 if (resultTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001717 result->setPixelRef(SkNEW_ARGS(SkGrTexturePixelRef,
1718 (resultTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001719 resultTexture->unref();
1720 }
reed@google.com76dd2772012-01-05 21:15:07 +00001721 return true;
1722}
1723
reed@google.comac10a2d2010-12-22 21:39:39 +00001724///////////////////////////////////////////////////////////////////////////////
1725
1726// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001727static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001728 kTriangles_GrPrimitiveType,
1729 kTriangleStrip_GrPrimitiveType,
1730 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001731};
1732
1733void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1734 int vertexCount, const SkPoint vertices[],
1735 const SkPoint texs[], const SkColor colors[],
1736 SkXfermode* xmode,
1737 const uint16_t indices[], int indexCount,
1738 const SkPaint& paint) {
1739 CHECK_SHOULD_DRAW(draw);
1740
bsalomon@google.com5782d712011-01-21 21:03:59 +00001741 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001742 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com5782d712011-01-21 21:03:59 +00001743 // we ignore the shader if texs is null.
1744 if (NULL == texs) {
twiz@google.com58071162012-07-18 21:41:50 +00001745 if (!skPaint2GrPaintNoShader(this,
1746 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001747 false,
1748 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001749 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +00001750 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001751 return;
1752 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001753 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001754 if (!skPaint2GrPaintShader(this,
1755 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001756 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001757 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001758 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001759 return;
1760 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001761 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001762
1763 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001764 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001765 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1766#if 0
1767 return
1768#endif
1769 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001770 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001771
bsalomon@google.com498776a2011-08-16 19:20:44 +00001772 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1773 if (NULL != colors) {
1774 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001775 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001776 for (int i = 0; i < vertexCount; ++i) {
rileya@google.com24f3ad12012-07-18 21:47:40 +00001777 convertedColors[i] = SkColor2GrColor(colors[i]);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001778 }
1779 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001780 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001781 fContext->drawVertices(grPaint,
1782 gVertexMode2PrimitiveType[vmode],
1783 vertexCount,
1784 (GrPoint*) vertices,
1785 (GrPoint*) texs,
1786 colors,
1787 indices,
1788 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001789}
1790
1791///////////////////////////////////////////////////////////////////////////////
1792
1793static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001794 GrFontScaler* scaler = (GrFontScaler*)data;
1795 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001796}
1797
1798static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1799 void* auxData;
1800 GrFontScaler* scaler = NULL;
1801 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1802 scaler = (GrFontScaler*)auxData;
1803 }
1804 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001805 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001806 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1807 }
1808 return scaler;
1809}
1810
1811static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1812 SkFixed fx, SkFixed fy,
1813 const SkGlyph& glyph) {
1814 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1815
bungeman@google.com15865a72012-01-11 16:28:04 +00001816 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001817
1818 if (NULL == procs->fFontScaler) {
1819 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1820 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001821
bungeman@google.com15865a72012-01-11 16:28:04 +00001822 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1823 glyph.getSubXFixed(),
1824 glyph.getSubYFixed()),
1825 SkFixedFloorToFixed(fx),
1826 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001827 procs->fFontScaler);
1828}
1829
bsalomon@google.com5782d712011-01-21 21:03:59 +00001830SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001831
1832 // deferred allocation
1833 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001834 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001835 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1836 fDrawProcs->fContext = fContext;
1837 }
1838
1839 // init our (and GL's) state
1840 fDrawProcs->fTextContext = context;
1841 fDrawProcs->fFontScaler = NULL;
1842 return fDrawProcs;
1843}
1844
1845void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1846 size_t byteLength, SkScalar x, SkScalar y,
1847 const SkPaint& paint) {
1848 CHECK_SHOULD_DRAW(draw);
1849
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001850 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001851 // this guy will just call our drawPath()
1852 draw.drawText((const char*)text, byteLength, x, y, paint);
1853 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001854 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001855
1856 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001857 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001858 if (!skPaint2GrPaintShader(this,
1859 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001860 true,
twiz@google.com58071162012-07-18 21:41:50 +00001861 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001862 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001863 return;
1864 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001865 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1866 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001867 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1868 }
1869}
1870
1871void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1872 size_t byteLength, const SkScalar pos[],
1873 SkScalar constY, int scalarsPerPos,
1874 const SkPaint& paint) {
1875 CHECK_SHOULD_DRAW(draw);
1876
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001877 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001878 // this guy will just call our drawPath()
1879 draw.drawPosText((const char*)text, byteLength, pos, constY,
1880 scalarsPerPos, paint);
1881 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001882 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001883
1884 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001885 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001886 if (!skPaint2GrPaintShader(this,
1887 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001888 true,
twiz@google.com58071162012-07-18 21:41:50 +00001889 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001890 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001891 return;
1892 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001893 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1894 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001895 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1896 scalarsPerPos, paint);
1897 }
1898}
1899
1900void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1901 size_t len, const SkPath& path,
1902 const SkMatrix* m, const SkPaint& paint) {
1903 CHECK_SHOULD_DRAW(draw);
1904
1905 SkASSERT(draw.fDevice == this);
1906 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1907}
1908
1909///////////////////////////////////////////////////////////////////////////////
1910
reed@google.comf67e4cf2011-03-15 20:56:58 +00001911bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1912 if (!paint.isLCDRenderText()) {
1913 // we're cool with the paint as is
1914 return false;
1915 }
1916
1917 if (paint.getShader() ||
1918 paint.getXfermode() || // unless its srcover
1919 paint.getMaskFilter() ||
1920 paint.getRasterizer() ||
1921 paint.getColorFilter() ||
1922 paint.getPathEffect() ||
1923 paint.isFakeBoldText() ||
1924 paint.getStyle() != SkPaint::kFill_Style) {
1925 // turn off lcd
1926 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1927 flags->fHinting = paint.getHinting();
1928 return true;
1929 }
1930 // we're cool with the paint as is
1931 return false;
1932}
1933
reed@google.com75d939b2011-12-07 15:07:23 +00001934void SkGpuDevice::flush() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001935 DO_DEFERRED_CLEAR;
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001936 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001937}
1938
reed@google.comf67e4cf2011-03-15 20:56:58 +00001939///////////////////////////////////////////////////////////////////////////////
1940
bsalomon@google.comfb309512011-11-30 14:13:48 +00001941bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001942 const GrTextureParams& params) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001943 uint64_t key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001944 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001945
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001946 GrTextureDesc desc;
1947 desc.fWidth = bitmap.width();
1948 desc.fHeight = bitmap.height();
rileya@google.com24f3ad12012-07-18 21:47:40 +00001949 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config());
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001950
robertphillips@google.com9c2ea842012-08-13 17:47:59 +00001951 GrCacheData cacheData(key);
1952
1953 return this->context()->isTextureInCache(desc, cacheData, &params);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001954}
1955
1956
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001957SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1958 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001959 bool isOpaque,
1960 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001961 GrTextureDesc desc;
1962 desc.fConfig = fRenderTarget->config();
1963 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1964 desc.fWidth = width;
1965 desc.fHeight = height;
1966 desc.fSampleCnt = fRenderTarget->numSamples();
1967
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001968 GrTexture* texture;
1969 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001970 // Skia's convention is to only clear a device if it is non-opaque.
1971 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001972
1973#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1974 // layers are never draw in repeat modes, so we can request an approx
1975 // match and ignore any padding.
1976 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1977 GrContext::kApprox_ScratchTexMatch :
1978 GrContext::kExact_ScratchTexMatch;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001979 texture = fContext->lockScratchTexture(desc, matchType);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001980#else
1981 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1982 texture = tunref.get();
1983#endif
1984 if (texture) {
1985 return SkNEW_ARGS(SkGpuDevice,(fContext,
1986 texture,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001987 needClear));
1988 } else {
1989 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1990 width, height);
1991 return NULL;
1992 }
1993}
1994
1995SkGpuDevice::SkGpuDevice(GrContext* context,
1996 GrTexture* texture,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001997 bool needClear)
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001998 : SkDevice(make_bitmap(context, texture->asRenderTarget())) {
1999
bsalomon@google.com06cd7322012-03-30 18:45:35 +00002000 GrAssert(texture && texture->asRenderTarget());
bsalomon@google.com8090e652012-08-28 15:07:11 +00002001 // This constructor is called from onCreateCompatibleDevice. It has locked the RT in the texture
2002 // cache. We pass true for the third argument so that it will get unlocked.
2003 this->initFromRenderTarget(context, texture->asRenderTarget(), true);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00002004 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00002005}