blob: b0b5ed04537e762e137067cfc071d58ab73c5c5d [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"
senorblanco@chromium.org9c397442012-09-27 21:57:45 +000019#include "SkDeviceImageFilterProxy.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000020#include "SkDrawProcs.h"
21#include "SkGlyphCache.h"
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +000022#include "SkImageFilter.h"
reed@google.comfe626382011-09-21 13:50:35 +000023#include "SkTLazy.h"
reed@google.comc9aa5872011-04-05 21:05:37 +000024#include "SkUtils.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000025
bsalomon@google.com06cd7322012-03-30 18:45:35 +000026#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
reed@google.comac10a2d2010-12-22 21:39:39 +000027
28#if 0
29 extern bool (*gShouldDrawProc)();
30 #define CHECK_SHOULD_DRAW(draw) \
31 do { \
32 if (gShouldDrawProc && !gShouldDrawProc()) return; \
33 this->prepareRenderTarget(draw); \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000034 GrAssert(!fNeedClear) \
reed@google.comac10a2d2010-12-22 21:39:39 +000035 } while (0)
36#else
bsalomon@google.com06cd7322012-03-30 18:45:35 +000037 #define CHECK_SHOULD_DRAW(draw) this->prepareRenderTarget(draw); \
38 GrAssert(!fNeedClear)
reed@google.comac10a2d2010-12-22 21:39:39 +000039#endif
40
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000041// we use the same texture slot on GrPaint for bitmaps and shaders
42// (since drawBitmap, drawSprite, and drawDevice ignore skia's shader)
43enum {
44 kBitmapTextureIdx = 0,
twiz@google.com58071162012-07-18 21:41:50 +000045 kShaderTextureIdx = 0,
46 kColorFilterTextureIdx = 1
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +000047};
48
reed@google.comcde92112011-07-06 20:00:52 +000049
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +000050#define MAX_BLUR_SIGMA 4.0f
51// FIXME: This value comes from from SkBlurMaskFilter.cpp.
52// Should probably be put in a common header someplace.
53#define MAX_BLUR_RADIUS SkIntToScalar(128)
54// This constant approximates the scaling done in the software path's
55// "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
56// IMHO, it actually should be 1: we blur "less" than we should do
57// according to the CSS and canvas specs, simply because Safari does the same.
58// Firefox used to do the same too, until 4.0 where they fixed it. So at some
59// point we should probably get rid of these scaling constants and rebaseline
60// all the blur tests.
61#define BLUR_SIGMA_SCALE 0.6f
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000062// This constant represents the screen alignment criterion in texels for
rmistry@google.comd6176b02012-08-23 18:14:13 +000063// requiring texture domain clamping to prevent color bleeding when drawing
junov@chromium.orgf32a9b62012-03-16 20:54:17 +000064// a sub region of a larger source image.
65#define COLOR_BLEED_TOLERANCE SkFloatToScalar(0.001f)
bsalomon@google.com06cd7322012-03-30 18:45:35 +000066
67#define DO_DEFERRED_CLEAR \
68 do { \
69 if (fNeedClear) { \
bsalomon@google.com730ca3b2012-04-03 13:25:12 +000070 this->clear(0x0); \
bsalomon@google.com06cd7322012-03-30 18:45:35 +000071 fNeedClear = false; \
72 } \
73 } while (false) \
74
reed@google.comac10a2d2010-12-22 21:39:39 +000075///////////////////////////////////////////////////////////////////////////////
76
reed@google.comb0a34d82012-07-11 19:57:55 +000077#define CHECK_FOR_NODRAW_ANNOTATION(paint) \
78 do { if (paint.isNoDrawAnnotation()) { return; } } while (0)
79
80///////////////////////////////////////////////////////////////////////////////
81
82
bsalomon@google.com84405e02012-03-05 19:57:21 +000083class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
84public:
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000085 SkAutoCachedTexture()
86 : fDevice(NULL)
87 , fTexture(NULL) {
rmistry@google.comd6176b02012-08-23 18:14:13 +000088 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000089
bsalomon@google.com84405e02012-03-05 19:57:21 +000090 SkAutoCachedTexture(SkGpuDevice* device,
91 const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +000092 const GrTextureParams* params,
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +000093 GrTexture** texture)
94 : fDevice(NULL)
95 , fTexture(NULL) {
96 GrAssert(NULL != texture);
bsalomon@google.comb8670992012-07-25 21:27:09 +000097 *texture = this->set(device, bitmap, params);
reed@google.comac10a2d2010-12-22 21:39:39 +000098 }
reed@google.comac10a2d2010-12-22 21:39:39 +000099
bsalomon@google.com84405e02012-03-05 19:57:21 +0000100 ~SkAutoCachedTexture() {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000101 if (NULL != fTexture) {
102 GrUnlockCachedBitmapTexture(fTexture);
bsalomon@google.com84405e02012-03-05 19:57:21 +0000103 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000104 }
bsalomon@google.com84405e02012-03-05 19:57:21 +0000105
106 GrTexture* set(SkGpuDevice* device,
107 const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +0000108 const GrTextureParams* params) {
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000109 if (NULL != fTexture) {
110 GrUnlockCachedBitmapTexture(fTexture);
111 fTexture = NULL;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000112 }
113 fDevice = device;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000114 GrTexture* result = (GrTexture*)bitmap.getTexture();
115 if (NULL == result) {
116 // Cannot return the native texture so look it up in our cache
117 fTexture = GrLockCachedBitmapTexture(device->context(), bitmap, params);
118 result = fTexture;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000119 }
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000120 return result;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000121 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000122
bsalomon@google.com84405e02012-03-05 19:57:21 +0000123private:
124 SkGpuDevice* fDevice;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +0000125 GrTexture* fTexture;
bsalomon@google.com84405e02012-03-05 19:57:21 +0000126};
reed@google.comac10a2d2010-12-22 21:39:39 +0000127
128///////////////////////////////////////////////////////////////////////////////
129
reed@google.comac10a2d2010-12-22 21:39:39 +0000130struct GrSkDrawProcs : public SkDrawProcs {
131public:
132 GrContext* fContext;
133 GrTextContext* fTextContext;
134 GrFontScaler* fFontScaler; // cached in the skia glyphcache
135};
136
137///////////////////////////////////////////////////////////////////////////////
138
reed@google.comaf951c92011-06-16 19:10:39 +0000139static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
140 switch (config) {
141 case kAlpha_8_GrPixelConfig:
142 *isOpaque = false;
143 return SkBitmap::kA8_Config;
144 case kRGB_565_GrPixelConfig:
145 *isOpaque = true;
146 return SkBitmap::kRGB_565_Config;
147 case kRGBA_4444_GrPixelConfig:
148 *isOpaque = false;
149 return SkBitmap::kARGB_4444_Config;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000150 case kSkia8888_PM_GrPixelConfig:
151 // we don't currently have a way of knowing whether
152 // a 8888 is opaque based on the config.
153 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000154 return SkBitmap::kARGB_8888_Config;
155 default:
156 *isOpaque = false;
157 return SkBitmap::kNo_Config;
158 }
159}
reed@google.comac10a2d2010-12-22 21:39:39 +0000160
reed@google.comaf951c92011-06-16 19:10:39 +0000161static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000162 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000163
164 bool isOpaque;
165 SkBitmap bitmap;
166 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
167 renderTarget->width(), renderTarget->height());
168 bitmap.setIsOpaque(isOpaque);
169 return bitmap;
170}
171
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000172SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000173: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
bsalomon@google.com8090e652012-08-28 15:07:11 +0000174 this->initFromRenderTarget(context, texture->asRenderTarget(), false);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000175}
176
reed@google.comaf951c92011-06-16 19:10:39 +0000177SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000178: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.com8090e652012-08-28 15:07:11 +0000179 this->initFromRenderTarget(context, renderTarget, false);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000180}
181
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000182void SkGpuDevice::initFromRenderTarget(GrContext* context,
bsalomon@google.com8090e652012-08-28 15:07:11 +0000183 GrRenderTarget* renderTarget,
184 bool cached) {
reed@google.comaf951c92011-06-16 19:10:39 +0000185 fNeedPrepareRenderTarget = false;
186 fDrawProcs = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000187
reed@google.comaf951c92011-06-16 19:10:39 +0000188 fContext = context;
189 fContext->ref();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000190
reed@google.comaf951c92011-06-16 19:10:39 +0000191 fRenderTarget = NULL;
192 fNeedClear = false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000193
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000194 GrAssert(NULL != renderTarget);
195 fRenderTarget = renderTarget;
196 fRenderTarget->ref();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000197
bsalomon@google.coma2921122012-08-28 12:34:17 +0000198 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
199 // on the RT but not vice-versa.
200 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
201 // busting chrome (for a currently unknown reason).
202 GrSurface* surface = fRenderTarget->asTexture();
203 if (NULL == surface) {
204 surface = fRenderTarget;
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000205 }
bsalomon@google.com8090e652012-08-28 15:07:11 +0000206 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (surface, cached));
bsalomon@google.coma2921122012-08-28 12:34:17 +0000207
reed@google.comaf951c92011-06-16 19:10:39 +0000208 this->setPixelRef(pr, 0)->unref();
209}
210
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000211SkGpuDevice::SkGpuDevice(GrContext* context,
212 SkBitmap::Config config,
213 int width,
214 int height)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000215 : SkDevice(config, width, height, false /*isOpaque*/) {
216
reed@google.comac10a2d2010-12-22 21:39:39 +0000217 fNeedPrepareRenderTarget = false;
218 fDrawProcs = NULL;
219
reed@google.com7b201d22011-01-11 18:59:23 +0000220 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000221 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000222
reed@google.comac10a2d2010-12-22 21:39:39 +0000223 fRenderTarget = NULL;
224 fNeedClear = false;
225
reed@google.comaf951c92011-06-16 19:10:39 +0000226 if (config != SkBitmap::kRGB_565_Config) {
227 config = SkBitmap::kARGB_8888_Config;
228 }
229 SkBitmap bm;
230 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000231
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000232 GrTextureDesc desc;
233 desc.fFlags = kRenderTarget_GrTextureFlagBit;
234 desc.fWidth = width;
235 desc.fHeight = height;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000236 desc.fConfig = SkBitmapConfig2GrPixelConfig(bm.config());
reed@google.comac10a2d2010-12-22 21:39:39 +0000237
bsalomon@google.coma2921122012-08-28 12:34:17 +0000238 SkAutoTUnref<GrTexture> texture(fContext->createUncachedTexture(desc, NULL, 0));
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000239
bsalomon@google.coma2921122012-08-28 12:34:17 +0000240 if (NULL != texture) {
241 fRenderTarget = texture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000242 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000243
reed@google.comaf951c92011-06-16 19:10:39 +0000244 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000245
reed@google.comaf951c92011-06-16 19:10:39 +0000246 // wrap the bitmap with a pixelref to expose our texture
bsalomon@google.coma2921122012-08-28 12:34:17 +0000247 SkGrPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (texture));
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000248 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000249 } else {
250 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
251 width, height);
252 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000253 }
254}
255
256SkGpuDevice::~SkGpuDevice() {
257 if (fDrawProcs) {
258 delete fDrawProcs;
259 }
260
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000261 // The SkGpuDevice gives the context the render target (e.g., in gainFocus)
rmistry@google.comd6176b02012-08-23 18:14:13 +0000262 // This call gives the context a chance to relinquish it
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000263 fContext->setRenderTarget(NULL);
264
bsalomon@google.coma2921122012-08-28 12:34:17 +0000265 SkSafeUnref(fRenderTarget);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000266 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000267}
268
reed@google.comac10a2d2010-12-22 21:39:39 +0000269///////////////////////////////////////////////////////////////////////////////
270
271void SkGpuDevice::makeRenderTargetCurrent() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000272 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000273 fContext->setRenderTarget(fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000274 fNeedPrepareRenderTarget = true;
275}
276
277///////////////////////////////////////////////////////////////////////////////
278
bsalomon@google.comc4364992011-11-07 15:54:49 +0000279namespace {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000280GrPixelConfig config8888_to_grconfig_and_flags(SkCanvas::Config8888 config8888, uint32_t* flags) {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000281 switch (config8888) {
282 case SkCanvas::kNative_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000283 *flags = 0;
284 return kSkia8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000285 case SkCanvas::kNative_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000286 *flags = GrContext::kUnpremul_PixelOpsFlag;
287 return kSkia8888_PM_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000288 case SkCanvas::kBGRA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000289 *flags = 0;
290 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000291 case SkCanvas::kBGRA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000292 *flags = GrContext::kUnpremul_PixelOpsFlag;
293 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000294 case SkCanvas::kRGBA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000295 *flags = 0;
296 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000297 case SkCanvas::kRGBA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000298 *flags = GrContext::kUnpremul_PixelOpsFlag;
299 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000300 default:
301 GrCrash("Unexpected Config8888.");
bsalomon@google.comccaa0022012-09-25 19:55:07 +0000302 *flags = 0; // suppress warning
bsalomon@google.comc4364992011-11-07 15:54:49 +0000303 return kSkia8888_PM_GrPixelConfig;
304 }
305}
306}
307
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000308bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
309 int x, int y,
310 SkCanvas::Config8888 config8888) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000311 DO_DEFERRED_CLEAR;
bsalomon@google.com910267d2011-11-02 20:06:25 +0000312 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
313 SkASSERT(!bitmap.isNull());
314 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000315
bsalomon@google.com910267d2011-11-02 20:06:25 +0000316 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000317 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000318 uint32_t flags;
319 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000320 return fContext->readRenderTargetPixels(fRenderTarget,
321 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000322 bitmap.width(),
323 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000324 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000325 bitmap.getPixels(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000326 bitmap.rowBytes(),
327 flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000328}
329
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000330void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
331 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000332 SkAutoLockPixels alp(bitmap);
333 if (!bitmap.readyToDraw()) {
334 return;
335 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000336
337 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000338 uint32_t flags;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000339 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000340 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000341 } else {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000342 flags = 0;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000343 config= SkBitmapConfig2GrPixelConfig(bitmap.config());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000344 }
345
bsalomon@google.com6f379512011-11-16 20:36:03 +0000346 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000347 config, bitmap.getPixels(), bitmap.rowBytes(), flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000348}
349
robertphillips@google.com46f93502012-08-07 15:38:08 +0000350namespace {
351void purgeClipCB(int genID, void* data) {
352 GrContext* context = (GrContext*) data;
353
354 if (SkClipStack::kInvalidGenID == genID ||
355 SkClipStack::kEmptyGenID == genID ||
356 SkClipStack::kWideOpenGenID == genID) {
357 // none of these cases will have a cached clip mask
358 return;
359 }
360
361}
362};
363
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000364void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
365 INHERITED::onAttachToCanvas(canvas);
366
367 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000368 fClipData.fClipStack = canvas->getClipStack();
robertphillips@google.com46f93502012-08-07 15:38:08 +0000369
370 fClipData.fClipStack->addPurgeClipCallback(purgeClipCB, fContext);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000371}
372
373void SkGpuDevice::onDetachFromCanvas() {
374 INHERITED::onDetachFromCanvas();
375
robertphillips@google.com46f93502012-08-07 15:38:08 +0000376 // TODO: iterate through the clip stack and clean up any cached clip masks
377 fClipData.fClipStack->removePurgeClipCallback(purgeClipCB, fContext);
378
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000379 fClipData.fClipStack = NULL;
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000380}
381
robertphillips@google.com607fe072012-07-24 13:54:00 +0000382#ifdef SK_DEBUG
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000383static void check_bounds(const GrClipData& clipData,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000384 const SkRegion& clipRegion,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000385 int renderTargetWidth,
386 int renderTargetHeight) {
387
robertphillips@google.com7b112892012-07-31 15:18:21 +0000388 SkIRect devBound;
389
390 devBound.setLTRB(0, 0, renderTargetWidth, renderTargetHeight);
391
robertphillips@google.com607fe072012-07-24 13:54:00 +0000392 SkClipStack::BoundsType boundType;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000393 SkRect canvTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000394
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000395 clipData.fClipStack->getBounds(&canvTemp, &boundType);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000396 if (SkClipStack::kNormal_BoundsType == boundType) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000397 SkIRect devTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000398
robertphillips@google.com7b112892012-07-31 15:18:21 +0000399 canvTemp.roundOut(&devTemp);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000400
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000401 devTemp.offset(-clipData.fOrigin.fX, -clipData.fOrigin.fY);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000402
robertphillips@google.com7b112892012-07-31 15:18:21 +0000403 if (!devBound.intersect(devTemp)) {
404 devBound.setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000405 }
406 }
407
robertphillips@google.com768fee82012-08-02 12:42:43 +0000408 GrAssert(devBound.contains(clipRegion.getBounds()));
robertphillips@google.com607fe072012-07-24 13:54:00 +0000409}
410#endif
411
reed@google.comac10a2d2010-12-22 21:39:39 +0000412///////////////////////////////////////////////////////////////////////////////
413
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000414static void set_matrix_and_clip(GrContext* context, const SkMatrix& matrix,
415 GrClipData& clipData,
416 const SkRegion& clipRegion,
417 const SkIPoint& origin,
418 int renderTargetWidth, int renderTargetHeight) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000419 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000420
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000421 clipData.fOrigin = origin;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000422
423#ifdef SK_DEBUG
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000424 check_bounds(clipData, clipRegion,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000425 renderTargetWidth, renderTargetHeight);
426#endif
427
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000428 context->setClip(&clipData);
reed@google.comac10a2d2010-12-22 21:39:39 +0000429}
430
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000431// call this every draw call, to ensure that the context reflects our state,
reed@google.comac10a2d2010-12-22 21:39:39 +0000432// and not the state from some other canvas/device
433void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000434 GrAssert(NULL != fClipData.fClipStack);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000435
reed@google.comac10a2d2010-12-22 21:39:39 +0000436 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000437 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000438
439 fContext->setRenderTarget(fRenderTarget);
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000440 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000441
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000442 set_matrix_and_clip(fContext, *draw.fMatrix,
443 fClipData, *draw.fClip, this->getOrigin(),
444 fRenderTarget->width(), fRenderTarget->height());
reed@google.comac10a2d2010-12-22 21:39:39 +0000445 fNeedPrepareRenderTarget = false;
446 }
447}
448
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000449void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
450 const SkClipStack& clipStack) {
451 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
452 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000453 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000454}
455
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000456void SkGpuDevice::gainFocus(const SkMatrix& matrix, const SkRegion& clip) {
457
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000458 GrAssert(NULL != fClipData.fClipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000459
reed@google.comac10a2d2010-12-22 21:39:39 +0000460 fContext->setRenderTarget(fRenderTarget);
461
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000462 this->INHERITED::gainFocus(matrix, clip);
reed@google.comac10a2d2010-12-22 21:39:39 +0000463
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000464 set_matrix_and_clip(fContext, matrix, fClipData, clip, this->getOrigin(),
465 fRenderTarget->width(), fRenderTarget->height());
reed@google.comac10a2d2010-12-22 21:39:39 +0000466
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000467 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000468}
469
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000470SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000471 DO_DEFERRED_CLEAR;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000472 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000473}
474
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000475bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
bsalomon@google.coma2921122012-08-28 12:34:17 +0000476 GrTexture* texture = fRenderTarget->asTexture();
477 if (NULL != texture) {
bsalomon@google.com88becf42012-10-05 14:54:42 +0000478 paint->colorSampler(kBitmapTextureIdx)->setCustomStage(
bsalomon@google.coma2921122012-08-28 12:34:17 +0000479 SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000480 return true;
481 }
482 return false;
483}
484
485///////////////////////////////////////////////////////////////////////////////
486
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000487SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
488SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
489SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
490SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
491SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
492 shader_type_mismatch);
rileya@google.com3e332582012-07-03 13:43:35 +0000493SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
494 shader_type_mismatch);
rileya@google.com22e57f92012-07-19 15:16:19 +0000495SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
496SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000497
bsalomon@google.com84405e02012-03-05 19:57:21 +0000498namespace {
499
500// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
501// justAlpha indicates that skPaint's alpha should be used rather than the color
502// Callers may subsequently modify the GrPaint. Setting constantColor indicates
503// that the final paint will draw the same color at every pixel. This allows
504// an optimization where the the color filter can be applied to the skPaint's
twiz@google.com58071162012-07-18 21:41:50 +0000505// color once while converting to GrPaint and then ignored.
506inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
507 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000508 bool justAlpha,
509 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000510 SkGpuDevice::SkAutoCachedTexture* act,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000511 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000512
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000513 grPaint->setDither(skPaint.isDither());
514 grPaint->setAntiAlias(skPaint.isAntiAlias());
bsalomon@google.com5782d712011-01-21 21:03:59 +0000515
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000516 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
517 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000518
519 SkXfermode* mode = skPaint.getXfermode();
520 if (mode) {
521 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000522 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000523#if 0
524 return false;
525#endif
526 }
527 }
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000528 grPaint->setBlendFunc(sk_blend_to_grblend(sm), sk_blend_to_grblend(dm));
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000529
bsalomon@google.com5782d712011-01-21 21:03:59 +0000530 if (justAlpha) {
531 uint8_t alpha = skPaint.getAlpha();
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000532 grPaint->setColor(GrColorPackRGBA(alpha, alpha, alpha, alpha));
Scroggod757df22011-05-16 13:11:16 +0000533 // justAlpha is currently set to true only if there is a texture,
534 // so constantColor should not also be true.
535 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000536 } else {
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000537 grPaint->setColor(SkColor2GrColor(skPaint.getColor()));
bsalomon@google.com88becf42012-10-05 14:54:42 +0000538 GrAssert(!grPaint->isColorStageEnabled(kShaderTextureIdx));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000539 }
Scroggo97c88c22011-05-11 14:05:25 +0000540 SkColorFilter* colorFilter = skPaint.getColorFilter();
541 SkColor color;
542 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000543 SkScalar matrix[20];
twiz@google.com58071162012-07-18 21:41:50 +0000544 SkBitmap colorTransformTable;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000545 // TODO: SkColorFilter::asCustomStage()
Scroggo97c88c22011-05-11 14:05:25 +0000546 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
Scroggod757df22011-05-16 13:11:16 +0000547 if (!constantColor) {
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000548 grPaint->setXfermodeColorFilter(filterMode, SkColor2GrColor(color));
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000549 } else {
550 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000551 grPaint->setColor(SkColor2GrColor(filtered));
Scroggod757df22011-05-16 13:11:16 +0000552 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000553 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000554 grPaint->setColorMatrix(matrix);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000555 } else if (colorFilter != NULL && colorFilter->asComponentTable(&colorTransformTable)) {
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000556 // pass NULL because the color table effect doesn't use tiling or filtering.
557 GrTexture* texture = act->set(dev, colorTransformTable, NULL);
bsalomon@google.com88becf42012-10-05 14:54:42 +0000558 GrSamplerState* colorSampler = grPaint->colorSampler(kColorFilterTextureIdx);
bsalomon@google.comb8670992012-07-25 21:27:09 +0000559 colorSampler->reset();
bsalomon@google.comcbd0ad92012-07-20 15:09:31 +0000560 colorSampler->setCustomStage(SkNEW_ARGS(GrColorTableEffect, (texture)))->unref();
Scroggo97c88c22011-05-11 14:05:25 +0000561 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000562 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000563}
564
bsalomon@google.com84405e02012-03-05 19:57:21 +0000565// This function is similar to skPaint2GrPaintNoShader but also converts
566// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
567// be used is set on grPaint and returned in param act. constantColor has the
568// same meaning as in skPaint2GrPaintNoShader.
569inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
570 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000571 bool constantColor,
bsalomon@google.com88becf42012-10-05 14:54:42 +0000572 SkGpuDevice::SkAutoCachedTexture textures[GrPaint::kMaxColorStages],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000573 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000574 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000575 if (NULL == shader) {
twiz@google.com58071162012-07-18 21:41:50 +0000576 return skPaint2GrPaintNoShader(dev,
577 skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000578 false,
579 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000580 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000581 grPaint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000582 } else if (!skPaint2GrPaintNoShader(dev, skPaint, true, false,
twiz@google.com58071162012-07-18 21:41:50 +0000583 &textures[kColorFilterTextureIdx], grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000584 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000585 }
586
bsalomon@google.com88becf42012-10-05 14:54:42 +0000587 GrSamplerState* sampler = grPaint->colorSampler(kShaderTextureIdx);
rileya@google.com91f319c2012-07-25 17:18:31 +0000588 GrCustomStage* stage = shader->asNewCustomStage(dev->context(), sampler);
589
590 if (NULL != stage) {
591 sampler->setCustomStage(stage)->unref();
592 SkMatrix localM;
593 if (shader->getLocalMatrix(&localM)) {
594 SkMatrix inverse;
595 if (localM.invert(&inverse)) {
596 sampler->matrix()->preConcat(inverse);
597 }
598 }
599 return true;
600 }
601
reed@google.comac10a2d2010-12-22 21:39:39 +0000602 SkBitmap bitmap;
rileya@google.com91f319c2012-07-25 17:18:31 +0000603 SkMatrix* matrix = sampler->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000604 SkShader::TileMode tileModes[2];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000605 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
rileya@google.com91f319c2012-07-25 17:18:31 +0000606 tileModes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000607
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000608 if (SkShader::kNone_BitmapType == bmptype) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000609 SkShader::GradientInfo info;
610 SkColor color;
611
612 info.fColors = &color;
613 info.fColorOffsets = NULL;
614 info.fColorCount = 1;
615 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
616 SkPaint copy(skPaint);
617 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000618 // modulate the paint alpha by the shader's solid color alpha
619 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
620 copy.setColor(SkColorSetA(color, newA));
twiz@google.com58071162012-07-18 21:41:50 +0000621 return skPaint2GrPaintNoShader(dev,
622 copy,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000623 false,
624 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000625 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000626 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000627 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000628 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000629 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000630
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000631 // Must set wrap and filter on the sampler before requesting a texture.
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000632 GrTextureParams params(tileModes, skPaint.isFilterBitmap());
633 GrTexture* texture = textures[kShaderTextureIdx].set(dev, bitmap, &params);
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000634
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000635 if (NULL == texture) {
636 SkDebugf("Couldn't convert bitmap to texture.\n");
637 return false;
638 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000639
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000640 sampler->reset();
641 sampler->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect, (texture, params)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000642
reed@google.comac10a2d2010-12-22 21:39:39 +0000643 // since our texture coords will be in local space, we wack the texture
644 // matrix to map them back into 0...1 before we load it
645 SkMatrix localM;
646 if (shader->getLocalMatrix(&localM)) {
647 SkMatrix inverse;
648 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000649 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000650 }
651 }
652 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000653 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
654 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000655 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000656 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000657
658 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000659}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000660}
reed@google.comac10a2d2010-12-22 21:39:39 +0000661
662///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000663void SkGpuDevice::clear(SkColor color) {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000664 fContext->clear(NULL, color, fRenderTarget);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000665}
666
reed@google.comac10a2d2010-12-22 21:39:39 +0000667void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
668 CHECK_SHOULD_DRAW(draw);
669
bsalomon@google.com5782d712011-01-21 21:03:59 +0000670 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +0000671 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000672 if (!skPaint2GrPaintShader(this,
673 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000674 true,
twiz@google.com58071162012-07-18 21:41:50 +0000675 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000676 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000677 return;
678 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000679
680 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000681}
682
683// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000684static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000685 kPoints_GrPrimitiveType,
686 kLines_GrPrimitiveType,
687 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000688};
689
690void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000691 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000692 CHECK_SHOULD_DRAW(draw);
693
694 SkScalar width = paint.getStrokeWidth();
695 if (width < 0) {
696 return;
697 }
698
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000699 // we only handle hairlines and paints without path effects or mask filters,
700 // else we let the SkDraw call our drawPath()
701 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000702 draw.drawPoints(mode, count, pts, paint, true);
703 return;
704 }
705
bsalomon@google.com5782d712011-01-21 21:03:59 +0000706 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +0000707 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000708 if (!skPaint2GrPaintShader(this,
709 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000710 true,
twiz@google.com58071162012-07-18 21:41:50 +0000711 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000712 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000713 return;
714 }
715
bsalomon@google.com5782d712011-01-21 21:03:59 +0000716 fContext->drawVertices(grPaint,
717 gPointMode2PrimtiveType[mode],
718 count,
719 (GrPoint*)pts,
720 NULL,
721 NULL,
722 NULL,
723 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000724}
725
reed@google.comc9aa5872011-04-05 21:05:37 +0000726///////////////////////////////////////////////////////////////////////////////
727
reed@google.comac10a2d2010-12-22 21:39:39 +0000728void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
729 const SkPaint& paint) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000730 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000731 CHECK_SHOULD_DRAW(draw);
732
bungeman@google.com79bd8772011-07-18 15:34:08 +0000733 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000734 SkScalar width = paint.getStrokeWidth();
735
736 /*
737 We have special code for hairline strokes, miter-strokes, and fills.
738 Anything else we just call our path code.
739 */
740 bool usePath = doStroke && width > 0 &&
741 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000742 // another two reasons we might need to call drawPath...
743 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000744 usePath = true;
745 }
reed@google.com67db6642011-05-26 11:46:35 +0000746 // until we aa rotated rects...
747 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
748 usePath = true;
749 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000750 // small miter limit means right angles show bevel...
751 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
752 paint.getStrokeMiter() < SK_ScalarSqrt2)
753 {
754 usePath = true;
755 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000756 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000757 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
758 usePath = true;
759 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000760
761 if (usePath) {
762 SkPath path;
763 path.addRect(rect);
764 this->drawPath(draw, path, paint, NULL, true);
765 return;
766 }
767
768 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +0000769 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000770 if (!skPaint2GrPaintShader(this,
771 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000772 true,
twiz@google.com58071162012-07-18 21:41:50 +0000773 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000774 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000775 return;
776 }
reed@google.com20efde72011-05-09 17:00:02 +0000777 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000778}
779
reed@google.com69302852011-02-16 18:08:07 +0000780#include "SkMaskFilter.h"
781#include "SkBounder.h"
782
bsalomon@google.com85003222012-03-28 14:44:37 +0000783///////////////////////////////////////////////////////////////////////////////
784
785// helpers for applying mask filters
786namespace {
787
788GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000789 switch (fillType) {
790 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000791 return kWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000792 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000793 return kEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000794 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000795 return kInverseWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000796 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000797 return kInverseEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000798 default:
799 SkDebugf("Unsupported path fill type\n");
bsalomon@google.com47059542012-06-06 20:51:20 +0000800 return kHairLine_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000801 }
802}
803
bsalomon@google.com85003222012-03-28 14:44:37 +0000804// We prefer to blur small rect with small radius via CPU.
805#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
806#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
807inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
808 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
809 rect.height() <= MIN_GPU_BLUR_SIZE &&
810 radius <= MIN_GPU_BLUR_RADIUS) {
811 return true;
812 }
813 return false;
814}
815
816bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
817 SkMaskFilter* filter, const SkMatrix& matrix,
818 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000819 GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000820#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000821 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000822#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000823 SkMaskFilter::BlurInfo info;
824 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000825 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000826 return false;
827 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000828 SkScalar radius = info.fIgnoreTransform ? info.fRadius
829 : matrix.mapRadius(info.fRadius);
830 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000831 if (radius <= 0) {
832 return false;
833 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000834
835 SkRect srcRect = path.getBounds();
836 if (shouldDrawBlurWithCPU(srcRect, radius)) {
837 return false;
838 }
839
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000840 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000841 float sigma3 = sigma * 3.0f;
842
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000843 SkRect clipRect;
844 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000845
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000846 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000847 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
848 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000849 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000850 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000851 SkIRect finalIRect;
852 finalRect.roundOut(&finalIRect);
853 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000854 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000855 }
856 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000857 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000858 }
859 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000860 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000861 GrTextureDesc desc;
862 desc.fFlags = kRenderTarget_GrTextureFlagBit;
863 desc.fWidth = SkScalarCeilToInt(srcRect.width());
864 desc.fHeight = SkScalarCeilToInt(srcRect.height());
865 // We actually only need A8, but it often isn't supported as a
866 // render target so default to RGBA_8888
bsalomon@google.com0342a852012-08-20 19:22:38 +0000867 desc.fConfig = kRGBA_8888_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000868
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000869 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
870 desc.fConfig = kAlpha_8_GrPixelConfig;
871 }
872
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000873 GrAutoScratchTexture pathEntry(context, desc);
874 GrTexture* pathTexture = pathEntry.texture();
875 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000876 return false;
877 }
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000878
robertphillips@google.comccb39502012-10-01 18:25:13 +0000879 SkAutoTUnref<GrTexture> blurTexture;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000880
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000881 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000882
robertphillips@google.comccb39502012-10-01 18:25:13 +0000883 {
884 GrContext::AutoRenderTarget art(context, pathTexture->asRenderTarget());
885 GrContext::AutoClip ac(context, srcRect);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000886
robertphillips@google.comccb39502012-10-01 18:25:13 +0000887 context->clear(NULL, 0);
888 GrPaint tempPaint;
robertphillips@google.comccb39502012-10-01 18:25:13 +0000889
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000890 if (grp->isAntiAlias()) {
891 tempPaint.setAntiAlias(true);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000892 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
893 // blend coeff of zero requires dual source blending support in order
894 // to properly blend partially covered pixels. This means the AA
895 // code path may not be taken. So we use a dst blend coeff of ISA. We
896 // could special case AA draws to a dst surface with known alpha=0 to
897 // use a zero dst coeff when dual source blending isn't available.
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000898 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000899 }
robertphillips@google.comccb39502012-10-01 18:25:13 +0000900 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
901 context->drawPath(tempPaint, path, pathFillType, &offset);
902
903 // If we're doing a normal blur, we can clobber the pathTexture in the
904 // gaussianBlur. Otherwise, we need to save it for later compositing.
905 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
skia.committer@gmail.comdc3a4e52012-10-02 02:01:24 +0000906 blurTexture.reset(context->gaussianBlur(pathTexture, isNormalBlur,
robertphillips@google.comccb39502012-10-01 18:25:13 +0000907 srcRect, sigma, sigma));
908
909 if (!isNormalBlur) {
910 GrPaint paint;
911 paint.reset();
bsalomon@google.com88becf42012-10-05 14:54:42 +0000912 paint.colorSampler(0)->matrix()->setIDiv(pathTexture->width(),
robertphillips@google.comccb39502012-10-01 18:25:13 +0000913 pathTexture->height());
914 // Blend pathTexture over blurTexture.
915 context->setRenderTarget(blurTexture->asRenderTarget());
bsalomon@google.com88becf42012-10-05 14:54:42 +0000916 paint.colorSampler(0)->setCustomStage(SkNEW_ARGS
robertphillips@google.comccb39502012-10-01 18:25:13 +0000917 (GrSingleTextureEffect, (pathTexture)))->unref();
918 if (SkMaskFilter::kInner_BlurType == blurType) {
919 // inner: dst = dst * src
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000920 paint.setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000921 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
922 // solid: dst = src + dst - src * dst
923 // = (1 - dst) * src + 1 * dst
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000924 paint.setBlendFunc(kIDC_GrBlendCoeff, kOne_GrBlendCoeff);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000925 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
926 // outer: dst = dst * (1 - src)
927 // = 0 * src + (1 - src) * dst
bsalomon@google.comc7448ce2012-10-05 19:04:13 +0000928 paint.setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff);
robertphillips@google.comccb39502012-10-01 18:25:13 +0000929 }
930 context->drawRect(paint, srcRect);
931 }
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000932 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000933
bsalomon@google.come3d32162012-07-20 13:37:06 +0000934 if (!grp->preConcatSamplerMatricesWithInverse(matrix)) {
935 return false;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000936 }
937
bsalomon@google.com88becf42012-10-05 14:54:42 +0000938 static const int MASK_IDX = GrPaint::kMaxCoverageStages - 1;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000939 // we assume the last mask index is available for use
bsalomon@google.com88becf42012-10-05 14:54:42 +0000940 GrAssert(!grp->isCoverageStageEnabled(MASK_IDX));
941 grp->coverageSampler(MASK_IDX)->reset();
942 grp->coverageSampler(MASK_IDX)->setCustomStage(
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000943 SkNEW_ARGS(GrSingleTextureEffect, (blurTexture)))->unref();
bsalomon@google.com88becf42012-10-05 14:54:42 +0000944 grp->coverageSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000945 -finalRect.fTop);
bsalomon@google.com88becf42012-10-05 14:54:42 +0000946 grp->coverageSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000947 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000948 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000949 return true;
950}
951
bsalomon@google.com85003222012-03-28 14:44:37 +0000952bool drawWithMaskFilter(GrContext* context, const SkPath& path,
953 SkMaskFilter* filter, const SkMatrix& matrix,
954 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000955 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000956 SkMask srcM, dstM;
957
958 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000959 SkMask::kComputeBoundsAndRenderImage_CreateMode,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000960 style)) {
reed@google.com69302852011-02-16 18:08:07 +0000961 return false;
962 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000963 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000964
965 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
966 return false;
967 }
968 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000969 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000970
971 if (clip.quickReject(dstM.fBounds)) {
972 return false;
973 }
974 if (bounder && !bounder->doIRect(dstM.fBounds)) {
975 return false;
976 }
977
978 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
979 // the current clip (and identity matrix) and grpaint settings
980
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000981 GrContext::AutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +0000982
bsalomon@google.come3d32162012-07-20 13:37:06 +0000983 if (!grp->preConcatSamplerMatricesWithInverse(matrix)) {
984 return false;
985 }
986
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000987 GrTextureDesc desc;
988 desc.fWidth = dstM.fBounds.width();
989 desc.fHeight = dstM.fBounds.height();
990 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +0000991
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000992 GrAutoScratchTexture ast(context, desc);
993 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000994
reed@google.com69302852011-02-16 18:08:07 +0000995 if (NULL == texture) {
996 return false;
997 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000998 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000999 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +00001000
bsalomon@google.com88becf42012-10-05 14:54:42 +00001001 static const int MASK_IDX = GrPaint::kMaxCoverageStages - 1;
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001002 // we assume the last mask index is available for use
bsalomon@google.com88becf42012-10-05 14:54:42 +00001003 GrAssert(!grp->isCoverageStageEnabled(MASK_IDX));
1004 grp->coverageSampler(MASK_IDX)->reset();
1005 grp->coverageSampler(MASK_IDX)->setCustomStage(
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001006 SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001007 GrRect d;
1008 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +00001009 GrIntToScalar(dstM.fBounds.fTop),
1010 GrIntToScalar(dstM.fBounds.fRight),
1011 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001012
bsalomon@google.com88becf42012-10-05 14:54:42 +00001013 GrMatrix* m = grp->coverageSampler(MASK_IDX)->matrix();
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001014 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
1015 -dstM.fBounds.fTop*SK_Scalar1);
1016 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001017 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +00001018 return true;
1019}
reed@google.com69302852011-02-16 18:08:07 +00001020
bsalomon@google.com85003222012-03-28 14:44:37 +00001021}
1022
1023///////////////////////////////////////////////////////////////////////////////
1024
reed@google.com0c219b62011-02-16 21:31:18 +00001025void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001026 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +00001027 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +00001028 CHECK_FOR_NODRAW_ANNOTATION(paint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001029 CHECK_SHOULD_DRAW(draw);
1030
reed@google.comfe626382011-09-21 13:50:35 +00001031 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001032
bsalomon@google.com5782d712011-01-21 21:03:59 +00001033 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +00001034 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001035 if (!skPaint2GrPaintShader(this,
1036 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001037 true,
twiz@google.com58071162012-07-18 21:41:50 +00001038 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001039 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001040 return;
1041 }
1042
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +00001043 // can we cheat, and threat a thin stroke as a hairline w/ coverage
1044 // if we can, we draw lots faster (raster device does this same test)
1045 SkScalar hairlineCoverage;
1046 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
1047 doFill = false;
bsalomon@google.comc7448ce2012-10-05 19:04:13 +00001048 grPaint.setCoverage(SkScalarRoundToInt(hairlineCoverage * grPaint.getCoverage()));
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +00001049 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001050
reed@google.comfe626382011-09-21 13:50:35 +00001051 // If we have a prematrix, apply it to the path, optimizing for the case
1052 // where the original path can in fact be modified in place (even though
1053 // its parameter type is const).
1054 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1055 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001056
1057 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001058 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001059
reed@google.come3445642011-02-16 23:20:39 +00001060 if (!pathIsMutable) {
1061 result = &tmpPath;
1062 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001063 }
reed@google.come3445642011-02-16 23:20:39 +00001064 // should I push prePathMatrix on our MV stack temporarily, instead
1065 // of applying it here? See SkDraw.cpp
1066 pathPtr->transform(*prePathMatrix, result);
1067 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001068 }
reed@google.com0c219b62011-02-16 21:31:18 +00001069 // at this point we're done with prePathMatrix
1070 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001071
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001072 if (paint.getPathEffect() ||
1073 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001074 // it is safe to use tmpPath here, even if we already used it for the
1075 // prepathmatrix, since getFillPath can take the same object for its
1076 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001077 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001078 pathPtr = &tmpPath;
1079 }
1080
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001081 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001082 // avoid possibly allocating a new path in transform if we can
1083 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1084
1085 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001086 pathPtr->transform(*draw.fMatrix, devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001087 GrPathFill pathFillType = doFill ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001088 skToGrFillType(devPathPtr->getFillType()) : kHairLine_GrPathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001089 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001090 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001091 &grPaint, pathFillType)) {
rmistry@google.comd6176b02012-08-23 18:14:13 +00001092 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001093 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001094 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001095 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001096 &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001097 }
reed@google.com69302852011-02-16 18:08:07 +00001098 return;
1099 }
reed@google.com69302852011-02-16 18:08:07 +00001100
bsalomon@google.com47059542012-06-06 20:51:20 +00001101 GrPathFill fill = kHairLine_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001102
reed@google.com0c219b62011-02-16 21:31:18 +00001103 if (doFill) {
1104 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001105 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001106 fill = kWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001107 break;
1108 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001109 fill = kEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001110 break;
1111 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001112 fill = kInverseWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001113 break;
1114 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001115 fill = kInverseEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001116 break;
1117 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001118 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001119 return;
1120 }
1121 }
1122
reed@google.com07f3ee12011-05-16 17:21:57 +00001123 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001124}
1125
bsalomon@google.comfb309512011-11-30 14:13:48 +00001126namespace {
1127
1128inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1129 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1130 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1131 return tilesX * tilesY;
1132}
1133
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001134inline int determine_tile_size(const SkBitmap& bitmap,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001135 const SkRect& src,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001136 int maxTextureSize) {
1137 static const int kSmallTileSize = 1 << 10;
1138 if (maxTextureSize <= kSmallTileSize) {
1139 return maxTextureSize;
1140 }
1141
1142 size_t maxTexTotalTileSize;
1143 size_t smallTotalTileSize;
1144
robertphillips@google.combac6b052012-09-28 18:06:49 +00001145 SkIRect iSrc;
1146 src.roundOut(&iSrc);
1147
1148 maxTexTotalTileSize = get_tile_count(iSrc.fLeft,
1149 iSrc.fTop,
1150 iSrc.fRight,
1151 iSrc.fBottom,
1152 maxTextureSize);
1153 smallTotalTileSize = get_tile_count(iSrc.fLeft,
1154 iSrc.fTop,
1155 iSrc.fRight,
1156 iSrc.fBottom,
1157 kSmallTileSize);
1158
bsalomon@google.comfb309512011-11-30 14:13:48 +00001159 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1160 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1161
1162 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1163 return kSmallTileSize;
1164 } else {
1165 return maxTextureSize;
1166 }
1167}
1168}
1169
1170bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001171 const GrTextureParams& params,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001172 const SkRect* srcRectPtr) const {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001173 // if bitmap is explictly texture backed then just use the texture
1174 if (NULL != bitmap.getTexture()) {
1175 return false;
1176 }
1177 // if it's larger than the max texture size, then we have no choice but
1178 // tiling
1179 const int maxTextureSize = fContext->getMaxTextureSize();
1180 if (bitmap.width() > maxTextureSize ||
1181 bitmap.height() > maxTextureSize) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001182 return true;
1183 }
1184 // if we are going to have to draw the whole thing, then don't tile
1185 if (NULL == srcRectPtr) {
1186 return false;
1187 }
1188 // if the entire texture is already in our cache then no reason to tile it
bsalomon@google.comb8670992012-07-25 21:27:09 +00001189 if (this->isBitmapInTextureCache(bitmap, params)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001190 return false;
1191 }
1192
1193 // At this point we know we could do the draw by uploading the entire bitmap
1194 // as a texture. However, if the texture would be large compared to the
1195 // cache size and we don't require most of it for this draw then tile to
1196 // reduce the amount of upload and cache spill.
1197
1198 // assumption here is that sw bitmap size is a good proxy for its size as
1199 // a texture
1200 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001201 size_t cacheSize;
1202 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001203 if (bmpSize < cacheSize / 2) {
1204 return false;
1205 }
1206
robertphillips@google.combac6b052012-09-28 18:06:49 +00001207 SkScalar fracUsed = SkScalarMul(srcRectPtr->width() / bitmap.width(),
1208 srcRectPtr->height() / bitmap.height());
1209 if (fracUsed <= SK_ScalarHalf) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001210 return true;
1211 } else {
1212 return false;
1213 }
1214}
1215
reed@google.comac10a2d2010-12-22 21:39:39 +00001216void SkGpuDevice::drawBitmap(const SkDraw& draw,
1217 const SkBitmap& bitmap,
1218 const SkIRect* srcRectPtr,
1219 const SkMatrix& m,
1220 const SkPaint& paint) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001221
1222 SkRect tmp;
1223 SkRect* tmpPtr = NULL;
1224
1225 // convert from SkIRect to SkRect
1226 if (NULL != srcRectPtr) {
1227 tmp.set(*srcRectPtr);
1228 tmpPtr = &tmp;
1229 }
1230
1231 // We cannot call drawBitmapRect here since 'm' could be anything
1232 this->drawBitmapCommon(draw, bitmap, tmpPtr, m, paint);
1233}
1234
1235void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1236 const SkBitmap& bitmap,
1237 const SkRect* srcRectPtr,
1238 const SkMatrix& m,
1239 const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001240 CHECK_SHOULD_DRAW(draw);
1241
robertphillips@google.combac6b052012-09-28 18:06:49 +00001242 SkRect srcRect;
reed@google.comac10a2d2010-12-22 21:39:39 +00001243 if (NULL == srcRectPtr) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001244 srcRect.set(0, 0, SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001245 } else {
1246 srcRect = *srcRectPtr;
1247 }
1248
junov@google.comd935cfb2011-06-27 20:48:23 +00001249 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001250 // Convert the bitmap to a shader so that the rect can be drawn
1251 // through drawRect, which supports mask filters.
robertphillips@google.combac6b052012-09-28 18:06:49 +00001252 SkMatrix newM(m);
junov@google.com1d329782011-07-28 20:10:09 +00001253 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001254 const SkBitmap* bitmapPtr = &bitmap;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001255 if (NULL != srcRectPtr) {
1256 SkIRect iSrc;
1257 srcRect.roundOut(&iSrc);
1258 if (!bitmap.extractSubset(&tmp, iSrc)) {
epoger@google.com9ef2d832011-07-01 21:12:20 +00001259 return; // extraction failed
1260 }
1261 bitmapPtr = &tmp;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001262 srcRect.offset(SkIntToScalar(-iSrc.fLeft), SkIntToScalar(-iSrc.fTop));
1263 // The source rect has changed so update the matrix
1264 newM.preTranslate(SkIntToScalar(iSrc.fLeft), SkIntToScalar(iSrc.fTop));
junov@google.comd935cfb2011-06-27 20:48:23 +00001265 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001266
junov@google.comd935cfb2011-06-27 20:48:23 +00001267 SkPaint paintWithTexture(paint);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001268 paintWithTexture.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
junov@google.comd935cfb2011-06-27 20:48:23 +00001269 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001270
robertphillips@google.combac6b052012-09-28 18:06:49 +00001271 // Transform 'newM' needs to be concatenated to the draw matrix,
1272 // rather than transforming the primitive directly, so that 'newM' will
junov@google.com1d329782011-07-28 20:10:09 +00001273 // also affect the behavior of the mask filter.
1274 SkMatrix drawMatrix;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001275 drawMatrix.setConcat(*draw.fMatrix, newM);
junov@google.com1d329782011-07-28 20:10:09 +00001276 SkDraw transformedDraw(draw);
1277 transformedDraw.fMatrix = &drawMatrix;
1278
robertphillips@google.combac6b052012-09-28 18:06:49 +00001279 this->drawRect(transformedDraw, srcRect, paintWithTexture);
junov@google.com1d329782011-07-28 20:10:09 +00001280
junov@google.comd935cfb2011-06-27 20:48:23 +00001281 return;
1282 }
1283
bsalomon@google.com5782d712011-01-21 21:03:59 +00001284 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001285 SkAutoCachedTexture colorLutTexture;
1286 if (!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001287 return;
1288 }
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001289 GrTextureParams params;
1290 params.setBilerp(paint.isFilterBitmap());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001291
robertphillips@google.combac6b052012-09-28 18:06:49 +00001292 if (!this->shouldTileBitmap(bitmap, params, srcRectPtr)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001293 // take the simple case
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001294 this->internalDrawBitmap(draw, bitmap, srcRect, m, params, &grPaint);
robertphillips@google.combac6b052012-09-28 18:06:49 +00001295 } else {
1296 this->drawTiledBitmap(draw, bitmap, srcRect, m, params, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001297 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001298}
1299
1300// Break 'bitmap' into several tiles to draw it since it has already
1301// been determined to be too large to fit in VRAM
1302void SkGpuDevice::drawTiledBitmap(const SkDraw& draw,
1303 const SkBitmap& bitmap,
1304 const SkRect& srcRect,
1305 const SkMatrix& m,
1306 const GrTextureParams& params,
1307 GrPaint* grPaint) {
1308 const int maxTextureSize = fContext->getMaxTextureSize();
1309
1310 int tileSize = determine_tile_size(bitmap, srcRect, maxTextureSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001311
reed@google.comac10a2d2010-12-22 21:39:39 +00001312 // compute clip bounds in local coordinates
robertphillips@google.combac6b052012-09-28 18:06:49 +00001313 SkRect clipRect;
reed@google.comac10a2d2010-12-22 21:39:39 +00001314 {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001315 clipRect.set(draw.fClip->getBounds());
reed@google.comac10a2d2010-12-22 21:39:39 +00001316 SkMatrix matrix, inverse;
1317 matrix.setConcat(*draw.fMatrix, m);
1318 if (!matrix.invert(&inverse)) {
1319 return;
1320 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001321 inverse.mapRect(&clipRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001322 }
1323
bsalomon@google.comfb309512011-11-30 14:13:48 +00001324 int nx = bitmap.width() / tileSize;
1325 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001326 for (int x = 0; x <= nx; x++) {
1327 for (int y = 0; y <= ny; y++) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001328 SkRect tileR;
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001329 tileR.set(SkIntToScalar(x * tileSize),
robertphillips@google.combac6b052012-09-28 18:06:49 +00001330 SkIntToScalar(y * tileSize),
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001331 SkIntToScalar((x + 1) * tileSize),
robertphillips@google.combac6b052012-09-28 18:06:49 +00001332 SkIntToScalar((y + 1) * tileSize));
1333
1334 if (!SkRect::Intersects(tileR, clipRect)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001335 continue;
1336 }
1337
robertphillips@google.combac6b052012-09-28 18:06:49 +00001338 if (!tileR.intersect(srcRect)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001339 continue;
1340 }
1341
1342 SkBitmap tmpB;
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001343 SkIRect iTileR;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001344 tileR.roundOut(&iTileR);
1345 if (bitmap.extractSubset(&tmpB, iTileR)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001346 // now offset it to make it "local" to our tmp bitmap
robertphillips@google.combac6b052012-09-28 18:06:49 +00001347 tileR.offset(SkIntToScalar(-iTileR.fLeft), SkIntToScalar(-iTileR.fTop));
reed@google.comac10a2d2010-12-22 21:39:39 +00001348 SkMatrix tmpM(m);
skia.committer@gmail.comdc3a4e52012-10-02 02:01:24 +00001349 tmpM.preTranslate(SkIntToScalar(iTileR.fLeft),
robertphillips@google.comffad46b2012-10-01 14:32:51 +00001350 SkIntToScalar(iTileR.fTop));
1351
robertphillips@google.combac6b052012-09-28 18:06:49 +00001352 this->internalDrawBitmap(draw, tmpB, tileR, 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,
robertphillips@google.combac6b052012-09-28 18:06:49 +00001410 const SkRect& srcRect,
reed@google.comac10a2d2010-12-22 21:39:39 +00001411 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.com88becf42012-10-05 14:54:42 +00001423 GrSamplerState* sampler = grPaint->colorSampler(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
robertphillips@google.combac6b052012-09-28 18:06:49 +00001433 GrRect dstRect(srcRect);
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001434 GrRect paintRect;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001435 SkScalar wInv = SkScalarInvert(SkIntToScalar(bitmap.width()));
1436 SkScalar hInv = SkScalarInvert(SkIntToScalar(bitmap.height()));
1437 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1438 SkScalarMul(srcRect.fTop, hInv),
1439 SkScalarMul(srcRect.fRight, wInv),
1440 SkScalarMul(srcRect.fBottom, hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001441
rmistry@google.comd6176b02012-08-23 18:14:13 +00001442 bool needsTextureDomain = false;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001443 if (params.isBilerp()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001444 // Need texture domain if drawing a sub rect.
skia.committer@gmail.com22b460c2012-09-29 02:01:02 +00001445 needsTextureDomain = srcRect.width() < bitmap.width() ||
robertphillips@google.combac6b052012-09-28 18:06:49 +00001446 srcRect.height() < bitmap.height();
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001447 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1448 // sampling is axis-aligned
robertphillips@google.combac6b052012-09-28 18:06:49 +00001449 GrRect transformedRect;
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001450 SkMatrix srcToDeviceMatrix(m);
1451 srcToDeviceMatrix.postConcat(*draw.fMatrix);
robertphillips@google.combac6b052012-09-28 18:06:49 +00001452 srcToDeviceMatrix.mapRect(&transformedRect, srcRect);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001453
robertphillips@google.combac6b052012-09-28 18:06:49 +00001454 if (hasAlignedSamples(srcRect, transformedRect)) {
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001455 // We could also turn off filtering here (but we already did a cache lookup with
1456 // params).
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001457 needsTextureDomain = false;
1458 } else {
1459 needsTextureDomain = needsTextureDomain &&
robertphillips@google.combac6b052012-09-28 18:06:49 +00001460 mayColorBleed(srcRect, transformedRect, m);
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001461 }
1462 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001463 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001464
1465 GrRect textureDomain = GrRect::MakeEmpty();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001466 SkAutoTUnref<GrCustomStage> stage;
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001467 if (needsTextureDomain) {
1468 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001469 GrScalar left, top, right, bottom;
robertphillips@google.combac6b052012-09-28 18:06:49 +00001470 if (srcRect.width() > GR_Scalar1) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001471 GrScalar border = GR_ScalarHalf / bitmap.width();
1472 left = paintRect.left() + border;
1473 right = paintRect.right() - border;
1474 } else {
1475 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1476 }
robertphillips@google.combac6b052012-09-28 18:06:49 +00001477 if (srcRect.height() > GR_Scalar1) {
junov@google.com6acc9b32011-05-16 18:32:07 +00001478 GrScalar border = GR_ScalarHalf / bitmap.height();
1479 top = paintRect.top() + border;
1480 bottom = paintRect.bottom() - border;
1481 } else {
1482 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1483 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001484 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001485 stage.reset(SkNEW_ARGS(GrTextureDomainEffect, (texture, textureDomain, params)));
1486 } else {
1487 stage.reset(SkNEW_ARGS(GrSingleTextureEffect, (texture, params)));
junov@google.com6acc9b32011-05-16 18:32:07 +00001488 }
bsalomon@google.com88becf42012-10-05 14:54:42 +00001489 grPaint->colorSampler(kBitmapTextureIdx)->setCustomStage(stage);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001490 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001491}
1492
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001493namespace {
1494
1495void apply_custom_stage(GrContext* context,
1496 GrTexture* srcTexture,
1497 GrTexture* dstTexture,
1498 const GrRect& rect,
1499 GrCustomStage* stage) {
1500 SkASSERT(srcTexture && srcTexture->getContext() == context);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001501 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001502 GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget());
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001503 GrContext::AutoClip acs(context, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001504
1505 GrMatrix sampleM;
1506 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
1507 GrPaint paint;
bsalomon@google.com88becf42012-10-05 14:54:42 +00001508 paint.colorSampler(0)->reset(sampleM);
1509 paint.colorSampler(0)->setCustomStage(stage);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001510 context->drawRect(paint, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001511}
1512
1513};
1514
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001515static GrTexture* filter_texture(SkDevice* device, GrContext* context,
1516 GrTexture* texture, SkImageFilter* filter,
1517 const GrRect& rect) {
reed@google.com8926b162012-03-23 15:36:36 +00001518 GrAssert(filter);
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001519 SkDeviceImageFilterProxy proxy(device);
reed@google.com8926b162012-03-23 15:36:36 +00001520
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()) {
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001529 texture = filter->onFilterImageGPU(&proxy, texture, rect);
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001530 } 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.com88becf42012-10-05 14:54:42 +00001559 GrSamplerState* sampler = grPaint.colorSampler(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);
bsalomon@google.com88becf42012-10-05 14:54:42 +00001565 grPaint.colorSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001566 (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) {
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001570 GrTexture* filteredTexture = filter_texture(this, 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) {
bsalomon@google.com88becf42012-10-05 14:54:42 +00001573 grPaint.colorSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001574 (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) {
robertphillips@google.combac6b052012-09-28 18:06:49 +00001592 SkMatrix matrix;
1593 SkRect bitmapBounds, tmpSrc;
1594
1595 bitmapBounds.set(0, 0,
1596 SkIntToScalar(bitmap.width()),
1597 SkIntToScalar(bitmap.height()));
1598
reed@google.com33535f32012-09-25 15:37:50 +00001599 // Compute matrix from the two rectangles
robertphillips@google.combac6b052012-09-28 18:06:49 +00001600 if (NULL != src) {
1601 tmpSrc = *src;
1602 } else {
1603 tmpSrc = bitmapBounds;
1604 }
1605 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1606
1607 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1608 if (NULL != src) {
1609 if (!bitmapBounds.contains(tmpSrc)) {
1610 if (!tmpSrc.intersect(bitmapBounds)) {
1611 return; // nothing to draw
reed@google.com33535f32012-09-25 15:37:50 +00001612 }
reed@google.com33535f32012-09-25 15:37:50 +00001613 }
reed@google.com33535f32012-09-25 15:37:50 +00001614 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +00001615
robertphillips@google.combac6b052012-09-28 18:06:49 +00001616 this->drawBitmapCommon(draw, bitmap, &tmpSrc, matrix, paint);
reed@google.com33535f32012-09-25 15:37:50 +00001617}
1618
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001619void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
reed@google.comac10a2d2010-12-22 21:39:39 +00001620 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001621 // clear of the source device must occur before CHECK_SHOULD_DRAW
1622 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1623 if (dev->fNeedClear) {
1624 // TODO: could check here whether we really need to draw at all
1625 dev->clear(0x0);
1626 }
1627
reed@google.comac10a2d2010-12-22 21:39:39 +00001628 CHECK_SHOULD_DRAW(draw);
1629
bsalomon@google.com5782d712011-01-21 21:03:59 +00001630 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001631 SkAutoCachedTexture colorLutTexture;
bsalomon@google.com88becf42012-10-05 14:54:42 +00001632 grPaint.colorSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001633 if (!dev->bindDeviceAsTexture(&grPaint) ||
twiz@google.com58071162012-07-18 21:41:50 +00001634 !skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001635 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001636 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001637
bsalomon@google.com88becf42012-10-05 14:54:42 +00001638 GrTexture* devTex = grPaint.getColorSampler(kBitmapTextureIdx).getCustomStage()->texture(0);
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001639 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001640
reed@google.com8926b162012-03-23 15:36:36 +00001641 SkImageFilter* filter = paint.getImageFilter();
1642 if (NULL != filter) {
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001643 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001644 SkIntToScalar(devTex->height()));
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001645 GrTexture* filteredTexture = filter_texture(this, fContext, devTex, filter, rect);
reed@google.com8926b162012-03-23 15:36:36 +00001646 if (filteredTexture) {
bsalomon@google.com88becf42012-10-05 14:54:42 +00001647 grPaint.colorSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001648 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001649 devTex = filteredTexture;
1650 filteredTexture->unref();
1651 }
1652 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001653
bsalomon@google.com5782d712011-01-21 21:03:59 +00001654 const SkBitmap& bm = dev->accessBitmap(false);
1655 int w = bm.width();
1656 int h = bm.height();
1657
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001658 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001659 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1660 GrIntToScalar(y),
1661 GrIntToScalar(w),
1662 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001663
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001664 // The device being drawn may not fill up its texture (saveLayer uses
1665 // the approximate ).
1666 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1667 GR_Scalar1 * h / devTex->height());
1668
1669 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001670}
1671
reed@google.com8926b162012-03-23 15:36:36 +00001672bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001673 if (!filter->asNewCustomStage(NULL, NULL) &&
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001674 !filter->canFilterImageGPU()) {
reed@google.com76dd2772012-01-05 21:15:07 +00001675 return false;
1676 }
reed@google.com8926b162012-03-23 15:36:36 +00001677 return true;
1678}
1679
1680bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1681 const SkMatrix& ctm,
1682 SkBitmap* result, SkIPoint* offset) {
1683 // want explicitly our impl, so guard against a subclass of us overriding it
1684 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001685 return false;
1686 }
reed@google.com8926b162012-03-23 15:36:36 +00001687
1688 SkAutoLockPixels alp(src, !src.getTexture());
1689 if (!src.getTexture() && !src.readyToDraw()) {
1690 return false;
1691 }
1692
1693 GrPaint paint;
reed@google.com8926b162012-03-23 15:36:36 +00001694
reed@google.com8926b162012-03-23 15:36:36 +00001695 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001696 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1697 // must be pushed upstack.
1698 SkAutoCachedTexture act(this, src, NULL, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001699
1700 result->setConfig(src.config(), src.width(), src.height());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001701 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001702 SkIntToScalar(src.height()));
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001703 GrTexture* resultTexture = filter_texture(this, fContext, texture, filter, rect);
reed@google.com8926b162012-03-23 15:36:36 +00001704 if (resultTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001705 result->setPixelRef(SkNEW_ARGS(SkGrTexturePixelRef,
1706 (resultTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001707 resultTexture->unref();
1708 }
reed@google.com76dd2772012-01-05 21:15:07 +00001709 return true;
1710}
1711
reed@google.comac10a2d2010-12-22 21:39:39 +00001712///////////////////////////////////////////////////////////////////////////////
1713
1714// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001715static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001716 kTriangles_GrPrimitiveType,
1717 kTriangleStrip_GrPrimitiveType,
1718 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001719};
1720
1721void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1722 int vertexCount, const SkPoint vertices[],
1723 const SkPoint texs[], const SkColor colors[],
1724 SkXfermode* xmode,
1725 const uint16_t indices[], int indexCount,
1726 const SkPaint& paint) {
1727 CHECK_SHOULD_DRAW(draw);
1728
bsalomon@google.com5782d712011-01-21 21:03:59 +00001729 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +00001730 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com5782d712011-01-21 21:03:59 +00001731 // we ignore the shader if texs is null.
1732 if (NULL == texs) {
twiz@google.com58071162012-07-18 21:41:50 +00001733 if (!skPaint2GrPaintNoShader(this,
1734 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001735 false,
1736 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001737 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +00001738 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001739 return;
1740 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001741 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001742 if (!skPaint2GrPaintShader(this,
1743 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001744 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001745 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001746 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001747 return;
1748 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001749 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001750
1751 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001752 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001753 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1754#if 0
1755 return
1756#endif
1757 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001758 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001759
bsalomon@google.com498776a2011-08-16 19:20:44 +00001760 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1761 if (NULL != colors) {
1762 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001763 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001764 for (int i = 0; i < vertexCount; ++i) {
rileya@google.com24f3ad12012-07-18 21:47:40 +00001765 convertedColors[i] = SkColor2GrColor(colors[i]);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001766 }
1767 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001768 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001769 fContext->drawVertices(grPaint,
1770 gVertexMode2PrimitiveType[vmode],
1771 vertexCount,
1772 (GrPoint*) vertices,
1773 (GrPoint*) texs,
1774 colors,
1775 indices,
1776 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001777}
1778
1779///////////////////////////////////////////////////////////////////////////////
1780
1781static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001782 GrFontScaler* scaler = (GrFontScaler*)data;
1783 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001784}
1785
1786static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1787 void* auxData;
1788 GrFontScaler* scaler = NULL;
1789 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1790 scaler = (GrFontScaler*)auxData;
1791 }
1792 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001793 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001794 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1795 }
1796 return scaler;
1797}
1798
1799static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1800 SkFixed fx, SkFixed fy,
1801 const SkGlyph& glyph) {
1802 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1803
bungeman@google.com15865a72012-01-11 16:28:04 +00001804 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001805
1806 if (NULL == procs->fFontScaler) {
1807 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1808 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001809
bungeman@google.com15865a72012-01-11 16:28:04 +00001810 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1811 glyph.getSubXFixed(),
1812 glyph.getSubYFixed()),
1813 SkFixedFloorToFixed(fx),
1814 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001815 procs->fFontScaler);
1816}
1817
bsalomon@google.com5782d712011-01-21 21:03:59 +00001818SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001819
1820 // deferred allocation
1821 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001822 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001823 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1824 fDrawProcs->fContext = fContext;
1825 }
1826
1827 // init our (and GL's) state
1828 fDrawProcs->fTextContext = context;
1829 fDrawProcs->fFontScaler = NULL;
1830 return fDrawProcs;
1831}
1832
1833void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1834 size_t byteLength, SkScalar x, SkScalar y,
1835 const SkPaint& paint) {
1836 CHECK_SHOULD_DRAW(draw);
1837
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001838 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001839 // this guy will just call our drawPath()
1840 draw.drawText((const char*)text, byteLength, x, y, paint);
1841 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001842 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001843
1844 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +00001845 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001846 if (!skPaint2GrPaintShader(this,
1847 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001848 true,
twiz@google.com58071162012-07-18 21:41:50 +00001849 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001850 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001851 return;
1852 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001853 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1854 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001855 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1856 }
1857}
1858
1859void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1860 size_t byteLength, const SkScalar pos[],
1861 SkScalar constY, int scalarsPerPos,
1862 const SkPaint& paint) {
1863 CHECK_SHOULD_DRAW(draw);
1864
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001865 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001866 // this guy will just call our drawPath()
1867 draw.drawPosText((const char*)text, byteLength, pos, constY,
1868 scalarsPerPos, paint);
1869 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001870 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001871
1872 GrPaint grPaint;
bsalomon@google.com88becf42012-10-05 14:54:42 +00001873 SkAutoCachedTexture textures[GrPaint::kMaxColorStages];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001874 if (!skPaint2GrPaintShader(this,
1875 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001876 true,
twiz@google.com58071162012-07-18 21:41:50 +00001877 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001878 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001879 return;
1880 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001881 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1882 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001883 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1884 scalarsPerPos, paint);
1885 }
1886}
1887
1888void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1889 size_t len, const SkPath& path,
1890 const SkMatrix* m, const SkPaint& paint) {
1891 CHECK_SHOULD_DRAW(draw);
1892
1893 SkASSERT(draw.fDevice == this);
1894 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1895}
1896
1897///////////////////////////////////////////////////////////////////////////////
1898
reed@google.comf67e4cf2011-03-15 20:56:58 +00001899bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1900 if (!paint.isLCDRenderText()) {
1901 // we're cool with the paint as is
1902 return false;
1903 }
1904
1905 if (paint.getShader() ||
1906 paint.getXfermode() || // unless its srcover
1907 paint.getMaskFilter() ||
1908 paint.getRasterizer() ||
1909 paint.getColorFilter() ||
1910 paint.getPathEffect() ||
1911 paint.isFakeBoldText() ||
1912 paint.getStyle() != SkPaint::kFill_Style) {
1913 // turn off lcd
1914 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1915 flags->fHinting = paint.getHinting();
1916 return true;
1917 }
1918 // we're cool with the paint as is
1919 return false;
1920}
1921
reed@google.com75d939b2011-12-07 15:07:23 +00001922void SkGpuDevice::flush() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001923 DO_DEFERRED_CLEAR;
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001924 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001925}
1926
reed@google.comf67e4cf2011-03-15 20:56:58 +00001927///////////////////////////////////////////////////////////////////////////////
1928
bsalomon@google.comfb309512011-11-30 14:13:48 +00001929bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001930 const GrTextureParams& params) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001931 uint64_t key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001932 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001933
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001934 GrTextureDesc desc;
1935 desc.fWidth = bitmap.width();
1936 desc.fHeight = bitmap.height();
rileya@google.com24f3ad12012-07-18 21:47:40 +00001937 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config());
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001938
robertphillips@google.com9c2ea842012-08-13 17:47:59 +00001939 GrCacheData cacheData(key);
1940
1941 return this->context()->isTextureInCache(desc, cacheData, &params);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001942}
1943
1944
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001945SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1946 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001947 bool isOpaque,
1948 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001949 GrTextureDesc desc;
1950 desc.fConfig = fRenderTarget->config();
1951 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1952 desc.fWidth = width;
1953 desc.fHeight = height;
1954 desc.fSampleCnt = fRenderTarget->numSamples();
1955
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001956 GrTexture* texture;
1957 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001958 // Skia's convention is to only clear a device if it is non-opaque.
1959 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001960
1961#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1962 // layers are never draw in repeat modes, so we can request an approx
1963 // match and ignore any padding.
1964 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1965 GrContext::kApprox_ScratchTexMatch :
1966 GrContext::kExact_ScratchTexMatch;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001967 texture = fContext->lockScratchTexture(desc, matchType);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001968#else
1969 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1970 texture = tunref.get();
1971#endif
1972 if (texture) {
1973 return SkNEW_ARGS(SkGpuDevice,(fContext,
1974 texture,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001975 needClear));
1976 } else {
1977 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1978 width, height);
1979 return NULL;
1980 }
1981}
1982
1983SkGpuDevice::SkGpuDevice(GrContext* context,
1984 GrTexture* texture,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001985 bool needClear)
robertphillips@google.com641f8b12012-07-31 19:15:58 +00001986 : SkDevice(make_bitmap(context, texture->asRenderTarget())) {
1987
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001988 GrAssert(texture && texture->asRenderTarget());
bsalomon@google.com8090e652012-08-28 15:07:11 +00001989 // This constructor is called from onCreateCompatibleDevice. It has locked the RT in the texture
1990 // cache. We pass true for the third argument so that it will get unlocked.
1991 this->initFromRenderTarget(context, texture->asRenderTarget(), true);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001992 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001993}