blob: 59a3b6173993eed5cd6dffa0cd3407646634b85d [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
130bool gDoTraceDraw;
131
132struct GrSkDrawProcs : public SkDrawProcs {
133public:
134 GrContext* fContext;
135 GrTextContext* fTextContext;
136 GrFontScaler* fFontScaler; // cached in the skia glyphcache
137};
138
139///////////////////////////////////////////////////////////////////////////////
140
reed@google.comaf951c92011-06-16 19:10:39 +0000141static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
142 switch (config) {
143 case kAlpha_8_GrPixelConfig:
144 *isOpaque = false;
145 return SkBitmap::kA8_Config;
146 case kRGB_565_GrPixelConfig:
147 *isOpaque = true;
148 return SkBitmap::kRGB_565_Config;
149 case kRGBA_4444_GrPixelConfig:
150 *isOpaque = false;
151 return SkBitmap::kARGB_4444_Config;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000152 case kSkia8888_PM_GrPixelConfig:
153 // we don't currently have a way of knowing whether
154 // a 8888 is opaque based on the config.
155 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000156 return SkBitmap::kARGB_8888_Config;
157 default:
158 *isOpaque = false;
159 return SkBitmap::kNo_Config;
160 }
161}
reed@google.comac10a2d2010-12-22 21:39:39 +0000162
reed@google.comaf951c92011-06-16 19:10:39 +0000163static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000164 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000165
166 bool isOpaque;
167 SkBitmap bitmap;
168 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
169 renderTarget->width(), renderTarget->height());
170 bitmap.setIsOpaque(isOpaque);
171 return bitmap;
172}
173
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000174SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000175: SkDevice(make_bitmap(context, texture->asRenderTarget())) {
bsalomon@google.com8090e652012-08-28 15:07:11 +0000176 this->initFromRenderTarget(context, texture->asRenderTarget(), false);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000177}
178
reed@google.comaf951c92011-06-16 19:10:39 +0000179SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000180: SkDevice(make_bitmap(context, renderTarget)) {
bsalomon@google.com8090e652012-08-28 15:07:11 +0000181 this->initFromRenderTarget(context, renderTarget, false);
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000182}
183
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000184void SkGpuDevice::initFromRenderTarget(GrContext* context,
bsalomon@google.com8090e652012-08-28 15:07:11 +0000185 GrRenderTarget* renderTarget,
186 bool cached) {
reed@google.comaf951c92011-06-16 19:10:39 +0000187 fNeedPrepareRenderTarget = false;
188 fDrawProcs = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000189
reed@google.comaf951c92011-06-16 19:10:39 +0000190 fContext = context;
191 fContext->ref();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000192
reed@google.comaf951c92011-06-16 19:10:39 +0000193 fRenderTarget = NULL;
194 fNeedClear = false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000195
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000196 GrAssert(NULL != renderTarget);
197 fRenderTarget = renderTarget;
198 fRenderTarget->ref();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000199
bsalomon@google.coma2921122012-08-28 12:34:17 +0000200 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
201 // on the RT but not vice-versa.
202 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
203 // busting chrome (for a currently unknown reason).
204 GrSurface* surface = fRenderTarget->asTexture();
205 if (NULL == surface) {
206 surface = fRenderTarget;
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000207 }
bsalomon@google.com8090e652012-08-28 15:07:11 +0000208 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (surface, cached));
bsalomon@google.coma2921122012-08-28 12:34:17 +0000209
reed@google.comaf951c92011-06-16 19:10:39 +0000210 this->setPixelRef(pr, 0)->unref();
211}
212
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000213SkGpuDevice::SkGpuDevice(GrContext* context,
214 SkBitmap::Config config,
215 int width,
216 int height)
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000217 : SkDevice(config, width, height, false /*isOpaque*/) {
218
reed@google.comac10a2d2010-12-22 21:39:39 +0000219 fNeedPrepareRenderTarget = false;
220 fDrawProcs = NULL;
221
reed@google.com7b201d22011-01-11 18:59:23 +0000222 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000223 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000224
reed@google.comac10a2d2010-12-22 21:39:39 +0000225 fRenderTarget = NULL;
226 fNeedClear = false;
227
reed@google.comaf951c92011-06-16 19:10:39 +0000228 if (config != SkBitmap::kRGB_565_Config) {
229 config = SkBitmap::kARGB_8888_Config;
230 }
231 SkBitmap bm;
232 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000233
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000234 GrTextureDesc desc;
235 desc.fFlags = kRenderTarget_GrTextureFlagBit;
236 desc.fWidth = width;
237 desc.fHeight = height;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000238 desc.fConfig = SkBitmapConfig2GrPixelConfig(bm.config());
reed@google.comac10a2d2010-12-22 21:39:39 +0000239
bsalomon@google.coma2921122012-08-28 12:34:17 +0000240 SkAutoTUnref<GrTexture> texture(fContext->createUncachedTexture(desc, NULL, 0));
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000241
bsalomon@google.coma2921122012-08-28 12:34:17 +0000242 if (NULL != texture) {
243 fRenderTarget = texture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000244 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000245
reed@google.comaf951c92011-06-16 19:10:39 +0000246 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000247
reed@google.comaf951c92011-06-16 19:10:39 +0000248 // wrap the bitmap with a pixelref to expose our texture
bsalomon@google.coma2921122012-08-28 12:34:17 +0000249 SkGrPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (texture));
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000250 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000251 } else {
252 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
253 width, height);
254 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000255 }
256}
257
258SkGpuDevice::~SkGpuDevice() {
259 if (fDrawProcs) {
260 delete fDrawProcs;
261 }
262
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000263 // The SkGpuDevice gives the context the render target (e.g., in gainFocus)
rmistry@google.comd6176b02012-08-23 18:14:13 +0000264 // This call gives the context a chance to relinquish it
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000265 fContext->setRenderTarget(NULL);
266
bsalomon@google.coma2921122012-08-28 12:34:17 +0000267 SkSafeUnref(fRenderTarget);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000268 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000269}
270
reed@google.comac10a2d2010-12-22 21:39:39 +0000271///////////////////////////////////////////////////////////////////////////////
272
273void SkGpuDevice::makeRenderTargetCurrent() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000274 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000275 fContext->setRenderTarget(fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000276 fNeedPrepareRenderTarget = true;
277}
278
279///////////////////////////////////////////////////////////////////////////////
280
bsalomon@google.comc4364992011-11-07 15:54:49 +0000281namespace {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000282GrPixelConfig config8888_to_grconfig_and_flags(SkCanvas::Config8888 config8888, uint32_t* flags) {
bsalomon@google.comc4364992011-11-07 15:54:49 +0000283 switch (config8888) {
284 case SkCanvas::kNative_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000285 *flags = 0;
286 return kSkia8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000287 case SkCanvas::kNative_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000288 *flags = GrContext::kUnpremul_PixelOpsFlag;
289 return kSkia8888_PM_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000290 case SkCanvas::kBGRA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000291 *flags = 0;
292 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000293 case SkCanvas::kBGRA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000294 *flags = GrContext::kUnpremul_PixelOpsFlag;
295 return kBGRA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000296 case SkCanvas::kRGBA_Premul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000297 *flags = 0;
298 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000299 case SkCanvas::kRGBA_Unpremul_Config8888:
bsalomon@google.com0342a852012-08-20 19:22:38 +0000300 *flags = GrContext::kUnpremul_PixelOpsFlag;
301 return kRGBA_8888_GrPixelConfig;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000302 default:
303 GrCrash("Unexpected Config8888.");
bsalomon@google.comccaa0022012-09-25 19:55:07 +0000304 *flags = 0; // suppress warning
bsalomon@google.comc4364992011-11-07 15:54:49 +0000305 return kSkia8888_PM_GrPixelConfig;
306 }
307}
308}
309
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000310bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
311 int x, int y,
312 SkCanvas::Config8888 config8888) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000313 DO_DEFERRED_CLEAR;
bsalomon@google.com910267d2011-11-02 20:06:25 +0000314 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
315 SkASSERT(!bitmap.isNull());
316 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000317
bsalomon@google.com910267d2011-11-02 20:06:25 +0000318 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000319 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000320 uint32_t flags;
321 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000322 return fContext->readRenderTargetPixels(fRenderTarget,
323 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000324 bitmap.width(),
325 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000326 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000327 bitmap.getPixels(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000328 bitmap.rowBytes(),
329 flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000330}
331
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000332void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
333 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000334 SkAutoLockPixels alp(bitmap);
335 if (!bitmap.readyToDraw()) {
336 return;
337 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000338
339 GrPixelConfig config;
bsalomon@google.com0342a852012-08-20 19:22:38 +0000340 uint32_t flags;
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000341 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000342 config = config8888_to_grconfig_and_flags(config8888, &flags);
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000343 } else {
bsalomon@google.com0342a852012-08-20 19:22:38 +0000344 flags = 0;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000345 config= SkBitmapConfig2GrPixelConfig(bitmap.config());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000346 }
347
bsalomon@google.com6f379512011-11-16 20:36:03 +0000348 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
bsalomon@google.com0342a852012-08-20 19:22:38 +0000349 config, bitmap.getPixels(), bitmap.rowBytes(), flags);
reed@google.comac10a2d2010-12-22 21:39:39 +0000350}
351
robertphillips@google.com46f93502012-08-07 15:38:08 +0000352namespace {
353void purgeClipCB(int genID, void* data) {
354 GrContext* context = (GrContext*) data;
355
356 if (SkClipStack::kInvalidGenID == genID ||
357 SkClipStack::kEmptyGenID == genID ||
358 SkClipStack::kWideOpenGenID == genID) {
359 // none of these cases will have a cached clip mask
360 return;
361 }
362
363}
364};
365
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000366void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
367 INHERITED::onAttachToCanvas(canvas);
368
369 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000370 fClipData.fClipStack = canvas->getClipStack();
robertphillips@google.com46f93502012-08-07 15:38:08 +0000371
372 fClipData.fClipStack->addPurgeClipCallback(purgeClipCB, fContext);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000373}
374
375void SkGpuDevice::onDetachFromCanvas() {
376 INHERITED::onDetachFromCanvas();
377
robertphillips@google.com46f93502012-08-07 15:38:08 +0000378 // TODO: iterate through the clip stack and clean up any cached clip masks
379 fClipData.fClipStack->removePurgeClipCallback(purgeClipCB, fContext);
380
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000381 fClipData.fClipStack = NULL;
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000382}
383
robertphillips@google.com607fe072012-07-24 13:54:00 +0000384#ifdef SK_DEBUG
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000385static void check_bounds(const GrClipData& clipData,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000386 const SkRegion& clipRegion,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000387 int renderTargetWidth,
388 int renderTargetHeight) {
389
robertphillips@google.com7b112892012-07-31 15:18:21 +0000390 SkIRect devBound;
391
392 devBound.setLTRB(0, 0, renderTargetWidth, renderTargetHeight);
393
robertphillips@google.com607fe072012-07-24 13:54:00 +0000394 SkClipStack::BoundsType boundType;
robertphillips@google.com7b112892012-07-31 15:18:21 +0000395 SkRect canvTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000396
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000397 clipData.fClipStack->getBounds(&canvTemp, &boundType);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000398 if (SkClipStack::kNormal_BoundsType == boundType) {
robertphillips@google.com7b112892012-07-31 15:18:21 +0000399 SkIRect devTemp;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000400
robertphillips@google.com7b112892012-07-31 15:18:21 +0000401 canvTemp.roundOut(&devTemp);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000402
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000403 devTemp.offset(-clipData.fOrigin.fX, -clipData.fOrigin.fY);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000404
robertphillips@google.com7b112892012-07-31 15:18:21 +0000405 if (!devBound.intersect(devTemp)) {
406 devBound.setEmpty();
robertphillips@google.com607fe072012-07-24 13:54:00 +0000407 }
408 }
409
robertphillips@google.com768fee82012-08-02 12:42:43 +0000410 GrAssert(devBound.contains(clipRegion.getBounds()));
robertphillips@google.com607fe072012-07-24 13:54:00 +0000411}
412#endif
413
reed@google.comac10a2d2010-12-22 21:39:39 +0000414///////////////////////////////////////////////////////////////////////////////
415
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000416static void set_matrix_and_clip(GrContext* context, const SkMatrix& matrix,
417 GrClipData& clipData,
418 const SkRegion& clipRegion,
419 const SkIPoint& origin,
420 int renderTargetWidth, int renderTargetHeight) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000421 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000422
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000423 clipData.fOrigin = origin;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000424
425#ifdef SK_DEBUG
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000426 check_bounds(clipData, clipRegion,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000427 renderTargetWidth, renderTargetHeight);
428#endif
429
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000430 context->setClip(&clipData);
reed@google.comac10a2d2010-12-22 21:39:39 +0000431}
432
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000433// call this every draw call, to ensure that the context reflects our state,
reed@google.comac10a2d2010-12-22 21:39:39 +0000434// and not the state from some other canvas/device
435void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000436 GrAssert(NULL != fClipData.fClipStack);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000437
reed@google.comac10a2d2010-12-22 21:39:39 +0000438 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000439 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000440
441 fContext->setRenderTarget(fRenderTarget);
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000442 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000443
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000444 set_matrix_and_clip(fContext, *draw.fMatrix,
445 fClipData, *draw.fClip, this->getOrigin(),
446 fRenderTarget->width(), fRenderTarget->height());
reed@google.comac10a2d2010-12-22 21:39:39 +0000447 fNeedPrepareRenderTarget = false;
448 }
449}
450
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000451void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
452 const SkClipStack& clipStack) {
453 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
454 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000455 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000456}
457
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000458void SkGpuDevice::gainFocus(const SkMatrix& matrix, const SkRegion& clip) {
459
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000460 GrAssert(NULL != fClipData.fClipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000461
reed@google.comac10a2d2010-12-22 21:39:39 +0000462 fContext->setRenderTarget(fRenderTarget);
463
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000464 this->INHERITED::gainFocus(matrix, clip);
reed@google.comac10a2d2010-12-22 21:39:39 +0000465
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000466 set_matrix_and_clip(fContext, matrix, fClipData, clip, this->getOrigin(),
467 fRenderTarget->width(), fRenderTarget->height());
reed@google.comac10a2d2010-12-22 21:39:39 +0000468
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000469 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000470}
471
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000472SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000473 DO_DEFERRED_CLEAR;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000474 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000475}
476
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000477bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
bsalomon@google.coma2921122012-08-28 12:34:17 +0000478 GrTexture* texture = fRenderTarget->asTexture();
479 if (NULL != texture) {
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000480 paint->textureSampler(kBitmapTextureIdx)->setCustomStage(
bsalomon@google.coma2921122012-08-28 12:34:17 +0000481 SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000482 return true;
483 }
484 return false;
485}
486
487///////////////////////////////////////////////////////////////////////////////
488
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000489SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
490SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
491SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
492SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
493SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
494 shader_type_mismatch);
rileya@google.com3e332582012-07-03 13:43:35 +0000495SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
496 shader_type_mismatch);
rileya@google.com22e57f92012-07-19 15:16:19 +0000497SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
498SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000499
bsalomon@google.com84405e02012-03-05 19:57:21 +0000500namespace {
501
502// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
503// justAlpha indicates that skPaint's alpha should be used rather than the color
504// Callers may subsequently modify the GrPaint. Setting constantColor indicates
505// that the final paint will draw the same color at every pixel. This allows
506// an optimization where the the color filter can be applied to the skPaint's
twiz@google.com58071162012-07-18 21:41:50 +0000507// color once while converting to GrPaint and then ignored.
508inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
509 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000510 bool justAlpha,
511 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000512 SkGpuDevice::SkAutoCachedTexture* act,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000513 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000514
515 grPaint->fDither = skPaint.isDither();
516 grPaint->fAntiAlias = skPaint.isAntiAlias();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000517 grPaint->fCoverage = 0xFF;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000518
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000519 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
520 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000521
522 SkXfermode* mode = skPaint.getXfermode();
523 if (mode) {
524 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000525 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000526#if 0
527 return false;
528#endif
529 }
530 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000531 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
532 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
533
bsalomon@google.com5782d712011-01-21 21:03:59 +0000534 if (justAlpha) {
535 uint8_t alpha = skPaint.getAlpha();
536 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000537 // justAlpha is currently set to true only if there is a texture,
538 // so constantColor should not also be true.
539 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000540 } else {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000541 grPaint->fColor = SkColor2GrColor(skPaint.getColor());
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000542 GrAssert(!grPaint->isTextureStageEnabled(kShaderTextureIdx));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000543 }
Scroggo97c88c22011-05-11 14:05:25 +0000544 SkColorFilter* colorFilter = skPaint.getColorFilter();
545 SkColor color;
546 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000547 SkScalar matrix[20];
twiz@google.com58071162012-07-18 21:41:50 +0000548 SkBitmap colorTransformTable;
bsalomon@google.com0d944822012-08-16 15:06:57 +0000549 grPaint->resetColorFilter();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000550 // TODO: SkColorFilter::asCustomStage()
Scroggo97c88c22011-05-11 14:05:25 +0000551 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000552 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000553 if (!constantColor) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000554 grPaint->fColorFilterColor = SkColor2GrColor(color);
Scroggod757df22011-05-16 13:11:16 +0000555 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000556 } else {
557 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
rileya@google.com24f3ad12012-07-18 21:47:40 +0000558 grPaint->fColor = SkColor2GrColor(filtered);
Scroggod757df22011-05-16 13:11:16 +0000559 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000560 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
561 grPaint->fColorMatrixEnabled = true;
562 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
563 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000564 } else if (colorFilter != NULL && colorFilter->asComponentTable(&colorTransformTable)) {
twiz@google.com58071162012-07-18 21:41:50 +0000565 grPaint->resetColorFilter();
566
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000567 // pass NULL because the color table effect doesn't use tiling or filtering.
568 GrTexture* texture = act->set(dev, colorTransformTable, NULL);
twiz@google.com58071162012-07-18 21:41:50 +0000569 GrSamplerState* colorSampler = grPaint->textureSampler(kColorFilterTextureIdx);
bsalomon@google.comb8670992012-07-25 21:27:09 +0000570 colorSampler->reset();
bsalomon@google.comcbd0ad92012-07-20 15:09:31 +0000571 colorSampler->setCustomStage(SkNEW_ARGS(GrColorTableEffect, (texture)))->unref();
Scroggo97c88c22011-05-11 14:05:25 +0000572 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000573 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000574}
575
bsalomon@google.com84405e02012-03-05 19:57:21 +0000576// This function is similar to skPaint2GrPaintNoShader but also converts
577// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
578// be used is set on grPaint and returned in param act. constantColor has the
579// same meaning as in skPaint2GrPaintNoShader.
580inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
581 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000582 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000583 SkGpuDevice::SkAutoCachedTexture textures[GrPaint::kMaxTextures],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000584 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000585 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000586 if (NULL == shader) {
twiz@google.com58071162012-07-18 21:41:50 +0000587 return skPaint2GrPaintNoShader(dev,
588 skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000589 false,
590 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000591 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000592 grPaint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000593 } else if (!skPaint2GrPaintNoShader(dev, skPaint, true, false,
twiz@google.com58071162012-07-18 21:41:50 +0000594 &textures[kColorFilterTextureIdx], grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000595 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000596 }
597
rileya@google.com91f319c2012-07-25 17:18:31 +0000598 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
599 GrCustomStage* stage = shader->asNewCustomStage(dev->context(), sampler);
600
601 if (NULL != stage) {
602 sampler->setCustomStage(stage)->unref();
603 SkMatrix localM;
604 if (shader->getLocalMatrix(&localM)) {
605 SkMatrix inverse;
606 if (localM.invert(&inverse)) {
607 sampler->matrix()->preConcat(inverse);
608 }
609 }
610 return true;
611 }
612
reed@google.comac10a2d2010-12-22 21:39:39 +0000613 SkBitmap bitmap;
rileya@google.com91f319c2012-07-25 17:18:31 +0000614 SkMatrix* matrix = sampler->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000615 SkShader::TileMode tileModes[2];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000616 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
rileya@google.com91f319c2012-07-25 17:18:31 +0000617 tileModes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000618
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000619 if (SkShader::kNone_BitmapType == bmptype) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000620 SkShader::GradientInfo info;
621 SkColor color;
622
623 info.fColors = &color;
624 info.fColorOffsets = NULL;
625 info.fColorCount = 1;
626 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
627 SkPaint copy(skPaint);
628 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000629 // modulate the paint alpha by the shader's solid color alpha
630 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
631 copy.setColor(SkColorSetA(color, newA));
twiz@google.com58071162012-07-18 21:41:50 +0000632 return skPaint2GrPaintNoShader(dev,
633 copy,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000634 false,
635 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000636 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000637 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000638 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000639 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000640 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000641
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000642 // Must set wrap and filter on the sampler before requesting a texture.
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000643 GrTextureParams params(tileModes, skPaint.isFilterBitmap());
644 GrTexture* texture = textures[kShaderTextureIdx].set(dev, bitmap, &params);
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000645
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000646 if (NULL == texture) {
647 SkDebugf("Couldn't convert bitmap to texture.\n");
648 return false;
649 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000650
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +0000651 sampler->reset();
652 sampler->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect, (texture, params)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000653
reed@google.comac10a2d2010-12-22 21:39:39 +0000654 // since our texture coords will be in local space, we wack the texture
655 // matrix to map them back into 0...1 before we load it
656 SkMatrix localM;
657 if (shader->getLocalMatrix(&localM)) {
658 SkMatrix inverse;
659 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000660 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000661 }
662 }
663 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000664 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
665 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000666 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000667 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000668
669 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000670}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000671}
reed@google.comac10a2d2010-12-22 21:39:39 +0000672
673///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000674void SkGpuDevice::clear(SkColor color) {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000675 fContext->clear(NULL, color, fRenderTarget);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000676}
677
reed@google.comac10a2d2010-12-22 21:39:39 +0000678void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
679 CHECK_SHOULD_DRAW(draw);
680
bsalomon@google.com5782d712011-01-21 21:03:59 +0000681 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000682 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000683 if (!skPaint2GrPaintShader(this,
684 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000685 true,
twiz@google.com58071162012-07-18 21:41:50 +0000686 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000687 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000688 return;
689 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000690
691 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000692}
693
694// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000695static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000696 kPoints_GrPrimitiveType,
697 kLines_GrPrimitiveType,
698 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000699};
700
701void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000702 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000703 CHECK_SHOULD_DRAW(draw);
704
705 SkScalar width = paint.getStrokeWidth();
706 if (width < 0) {
707 return;
708 }
709
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000710 // we only handle hairlines and paints without path effects or mask filters,
711 // else we let the SkDraw call our drawPath()
712 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000713 draw.drawPoints(mode, count, pts, paint, true);
714 return;
715 }
716
bsalomon@google.com5782d712011-01-21 21:03:59 +0000717 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000718 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000719 if (!skPaint2GrPaintShader(this,
720 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000721 true,
twiz@google.com58071162012-07-18 21:41:50 +0000722 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000723 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000724 return;
725 }
726
bsalomon@google.com5782d712011-01-21 21:03:59 +0000727 fContext->drawVertices(grPaint,
728 gPointMode2PrimtiveType[mode],
729 count,
730 (GrPoint*)pts,
731 NULL,
732 NULL,
733 NULL,
734 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000735}
736
reed@google.comc9aa5872011-04-05 21:05:37 +0000737///////////////////////////////////////////////////////////////////////////////
738
reed@google.comac10a2d2010-12-22 21:39:39 +0000739void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
740 const SkPaint& paint) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000741 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000742 CHECK_SHOULD_DRAW(draw);
743
bungeman@google.com79bd8772011-07-18 15:34:08 +0000744 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000745 SkScalar width = paint.getStrokeWidth();
746
747 /*
748 We have special code for hairline strokes, miter-strokes, and fills.
749 Anything else we just call our path code.
750 */
751 bool usePath = doStroke && width > 0 &&
752 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000753 // another two reasons we might need to call drawPath...
754 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000755 usePath = true;
756 }
reed@google.com67db6642011-05-26 11:46:35 +0000757 // until we aa rotated rects...
758 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
759 usePath = true;
760 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000761 // small miter limit means right angles show bevel...
762 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
763 paint.getStrokeMiter() < SK_ScalarSqrt2)
764 {
765 usePath = true;
766 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000767 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000768 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
769 usePath = true;
770 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000771
772 if (usePath) {
773 SkPath path;
774 path.addRect(rect);
775 this->drawPath(draw, path, paint, NULL, true);
776 return;
777 }
778
779 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000780 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000781 if (!skPaint2GrPaintShader(this,
782 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000783 true,
twiz@google.com58071162012-07-18 21:41:50 +0000784 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000785 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000786 return;
787 }
reed@google.com20efde72011-05-09 17:00:02 +0000788 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000789}
790
reed@google.com69302852011-02-16 18:08:07 +0000791#include "SkMaskFilter.h"
792#include "SkBounder.h"
793
bsalomon@google.com85003222012-03-28 14:44:37 +0000794///////////////////////////////////////////////////////////////////////////////
795
796// helpers for applying mask filters
797namespace {
798
799GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000800 switch (fillType) {
801 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000802 return kWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000803 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000804 return kEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000805 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000806 return kInverseWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000807 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000808 return kInverseEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000809 default:
810 SkDebugf("Unsupported path fill type\n");
bsalomon@google.com47059542012-06-06 20:51:20 +0000811 return kHairLine_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000812 }
813}
814
bsalomon@google.com85003222012-03-28 14:44:37 +0000815// We prefer to blur small rect with small radius via CPU.
816#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
817#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
818inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
819 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
820 rect.height() <= MIN_GPU_BLUR_SIZE &&
821 radius <= MIN_GPU_BLUR_RADIUS) {
822 return true;
823 }
824 return false;
825}
826
827bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
828 SkMaskFilter* filter, const SkMatrix& matrix,
829 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000830 GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000831#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000832 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000833#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000834 SkMaskFilter::BlurInfo info;
835 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000836 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000837 return false;
838 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000839 SkScalar radius = info.fIgnoreTransform ? info.fRadius
840 : matrix.mapRadius(info.fRadius);
841 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000842 if (radius <= 0) {
843 return false;
844 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000845
846 SkRect srcRect = path.getBounds();
847 if (shouldDrawBlurWithCPU(srcRect, radius)) {
848 return false;
849 }
850
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000851 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000852 float sigma3 = sigma * 3.0f;
853
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000854 SkRect clipRect;
855 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000856
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000857 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000858 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
859 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000860 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000861 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000862 SkIRect finalIRect;
863 finalRect.roundOut(&finalIRect);
864 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000865 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000866 }
867 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000868 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000869 }
870 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000871 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000872 GrTextureDesc desc;
873 desc.fFlags = kRenderTarget_GrTextureFlagBit;
874 desc.fWidth = SkScalarCeilToInt(srcRect.width());
875 desc.fHeight = SkScalarCeilToInt(srcRect.height());
876 // We actually only need A8, but it often isn't supported as a
877 // render target so default to RGBA_8888
bsalomon@google.com0342a852012-08-20 19:22:38 +0000878 desc.fConfig = kRGBA_8888_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000879
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000880 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
881 desc.fConfig = kAlpha_8_GrPixelConfig;
882 }
883
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000884 GrAutoScratchTexture pathEntry(context, desc);
885 GrTexture* pathTexture = pathEntry.texture();
886 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000887 return false;
888 }
889 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000890 // Once this code moves into GrContext, this should be changed to use
891 // an AutoClipRestore.
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000892 const GrClipData* oldClipData = context->getClip();
893
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000894 context->setRenderTarget(pathTexture->asRenderTarget());
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000895
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000896 SkClipStack newClipStack(srcRect);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000897 GrClipData newClipData;
898 newClipData.fClipStack = &newClipStack;
899 context->setClip(&newClipData);
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000900
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000901 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000902 GrPaint tempPaint;
903 tempPaint.reset();
904
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000905 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000906 tempPaint.fAntiAlias = grp->fAntiAlias;
907 if (tempPaint.fAntiAlias) {
908 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
909 // blend coeff of zero requires dual source blending support in order
910 // to properly blend partially covered pixels. This means the AA
911 // code path may not be taken. So we use a dst blend coeff of ISA. We
912 // could special case AA draws to a dst surface with known alpha=0 to
913 // use a zero dst coeff when dual source blending isn't available.
bsalomon@google.com47059542012-06-06 20:51:20 +0000914 tempPaint.fSrcBlendCoeff = kOne_GrBlendCoeff;
915 tempPaint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000916 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000917 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000918 context->drawPath(tempPaint, path, pathFillType, &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000919
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000920 // If we're doing a normal blur, we can clobber the pathTexture in the
921 // gaussianBlur. Otherwise, we need to save it for later compositing.
922 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +0000923 SkAutoTUnref<GrTexture> blurTexture(context->gaussianBlur(
924 pathTexture, isNormalBlur, srcRect, sigma, sigma));
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000925
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000926 if (!isNormalBlur) {
927 GrPaint paint;
928 paint.reset();
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000929 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
930 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000931 // Blend pathTexture over blurTexture.
932 context->setRenderTarget(blurTexture->asRenderTarget());
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000933 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS
934 (GrSingleTextureEffect, (pathTexture)))->unref();
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000935 if (SkMaskFilter::kInner_BlurType == blurType) {
936 // inner: dst = dst * src
bsalomon@google.com47059542012-06-06 20:51:20 +0000937 paint.fSrcBlendCoeff = kDC_GrBlendCoeff;
938 paint.fDstBlendCoeff = kZero_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000939 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
940 // solid: dst = src + dst - src * dst
941 // = (1 - dst) * src + 1 * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000942 paint.fSrcBlendCoeff = kIDC_GrBlendCoeff;
943 paint.fDstBlendCoeff = kOne_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000944 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
945 // outer: dst = dst * (1 - src)
946 // = 0 * src + (1 - src) * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000947 paint.fSrcBlendCoeff = kZero_GrBlendCoeff;
948 paint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000949 }
950 context->drawRect(paint, srcRect);
951 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000952 context->setRenderTarget(oldRenderTarget);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000953 context->setClip(oldClipData);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000954
bsalomon@google.come3d32162012-07-20 13:37:06 +0000955 if (!grp->preConcatSamplerMatricesWithInverse(matrix)) {
956 return false;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000957 }
958
959 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
960 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000961 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
bsalomon@google.com97912912011-12-06 16:30:36 +0000962 grp->maskSampler(MASK_IDX)->reset();
bsalomon@google.com1c31f632012-07-26 19:39:06 +0000963 grp->maskSampler(MASK_IDX)->setCustomStage(
964 SkNEW_ARGS(GrSingleTextureEffect, (blurTexture)))->unref();
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000965 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
966 -finalRect.fTop);
967 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
968 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000969 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000970 return true;
971}
972
bsalomon@google.com85003222012-03-28 14:44:37 +0000973bool drawWithMaskFilter(GrContext* context, const SkPath& path,
974 SkMaskFilter* filter, const SkMatrix& matrix,
975 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000976 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000977 SkMask srcM, dstM;
978
979 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000980 SkMask::kComputeBoundsAndRenderImage_CreateMode,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000981 style)) {
reed@google.com69302852011-02-16 18:08:07 +0000982 return false;
983 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000984 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000985
986 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
987 return false;
988 }
989 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000990 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000991
992 if (clip.quickReject(dstM.fBounds)) {
993 return false;
994 }
995 if (bounder && !bounder->doIRect(dstM.fBounds)) {
996 return false;
997 }
998
999 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
1000 // the current clip (and identity matrix) and grpaint settings
1001
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001002 GrContext::AutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +00001003
bsalomon@google.come3d32162012-07-20 13:37:06 +00001004 if (!grp->preConcatSamplerMatricesWithInverse(matrix)) {
1005 return false;
1006 }
1007
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001008 GrTextureDesc desc;
1009 desc.fWidth = dstM.fBounds.width();
1010 desc.fHeight = dstM.fBounds.height();
1011 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +00001012
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001013 GrAutoScratchTexture ast(context, desc);
1014 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001015
reed@google.com69302852011-02-16 18:08:07 +00001016 if (NULL == texture) {
1017 return false;
1018 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001019 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001020 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +00001021
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001022 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
1023 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001024 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
bsalomon@google.com97912912011-12-06 16:30:36 +00001025 grp->maskSampler(MASK_IDX)->reset();
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001026 grp->maskSampler(MASK_IDX)->setCustomStage(
1027 SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001028 GrRect d;
1029 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +00001030 GrIntToScalar(dstM.fBounds.fTop),
1031 GrIntToScalar(dstM.fBounds.fRight),
1032 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001033
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001034 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
1035 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
1036 -dstM.fBounds.fTop*SK_Scalar1);
1037 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001038 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +00001039 return true;
1040}
reed@google.com69302852011-02-16 18:08:07 +00001041
bsalomon@google.com85003222012-03-28 14:44:37 +00001042}
1043
1044///////////////////////////////////////////////////////////////////////////////
1045
reed@google.com0c219b62011-02-16 21:31:18 +00001046void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001047 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +00001048 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +00001049 CHECK_FOR_NODRAW_ANNOTATION(paint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001050 CHECK_SHOULD_DRAW(draw);
1051
reed@google.comfe626382011-09-21 13:50:35 +00001052 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001053
bsalomon@google.com5782d712011-01-21 21:03:59 +00001054 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001055 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001056 if (!skPaint2GrPaintShader(this,
1057 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001058 true,
twiz@google.com58071162012-07-18 21:41:50 +00001059 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001060 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001061 return;
1062 }
1063
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +00001064 // can we cheat, and threat a thin stroke as a hairline w/ coverage
1065 // if we can, we draw lots faster (raster device does this same test)
1066 SkScalar hairlineCoverage;
1067 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
1068 doFill = false;
1069 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
1070 grPaint.fCoverage);
1071 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001072
reed@google.comfe626382011-09-21 13:50:35 +00001073 // If we have a prematrix, apply it to the path, optimizing for the case
1074 // where the original path can in fact be modified in place (even though
1075 // its parameter type is const).
1076 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1077 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001078
1079 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001080 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001081
reed@google.come3445642011-02-16 23:20:39 +00001082 if (!pathIsMutable) {
1083 result = &tmpPath;
1084 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001085 }
reed@google.come3445642011-02-16 23:20:39 +00001086 // should I push prePathMatrix on our MV stack temporarily, instead
1087 // of applying it here? See SkDraw.cpp
1088 pathPtr->transform(*prePathMatrix, result);
1089 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001090 }
reed@google.com0c219b62011-02-16 21:31:18 +00001091 // at this point we're done with prePathMatrix
1092 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001093
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001094 if (paint.getPathEffect() ||
1095 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001096 // it is safe to use tmpPath here, even if we already used it for the
1097 // prepathmatrix, since getFillPath can take the same object for its
1098 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001099 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001100 pathPtr = &tmpPath;
1101 }
1102
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001103 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001104 // avoid possibly allocating a new path in transform if we can
1105 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1106
1107 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001108 pathPtr->transform(*draw.fMatrix, devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001109 GrPathFill pathFillType = doFill ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001110 skToGrFillType(devPathPtr->getFillType()) : kHairLine_GrPathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001111 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001112 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001113 &grPaint, pathFillType)) {
rmistry@google.comd6176b02012-08-23 18:14:13 +00001114 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001115 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001116 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001117 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001118 &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001119 }
reed@google.com69302852011-02-16 18:08:07 +00001120 return;
1121 }
reed@google.com69302852011-02-16 18:08:07 +00001122
bsalomon@google.com47059542012-06-06 20:51:20 +00001123 GrPathFill fill = kHairLine_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001124
reed@google.com0c219b62011-02-16 21:31:18 +00001125 if (doFill) {
1126 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001127 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001128 fill = kWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001129 break;
1130 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001131 fill = kEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001132 break;
1133 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001134 fill = kInverseWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001135 break;
1136 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001137 fill = kInverseEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001138 break;
1139 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001140 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001141 return;
1142 }
1143 }
1144
reed@google.com07f3ee12011-05-16 17:21:57 +00001145 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001146}
1147
bsalomon@google.comfb309512011-11-30 14:13:48 +00001148namespace {
1149
1150inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1151 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1152 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1153 return tilesX * tilesY;
1154}
1155
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001156inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001157 const SkIRect* srcRectPtr,
1158 int maxTextureSize) {
1159 static const int kSmallTileSize = 1 << 10;
1160 if (maxTextureSize <= kSmallTileSize) {
1161 return maxTextureSize;
1162 }
1163
1164 size_t maxTexTotalTileSize;
1165 size_t smallTotalTileSize;
1166
1167 if (NULL == srcRectPtr) {
1168 int w = bitmap.width();
1169 int h = bitmap.height();
1170 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1171 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1172 } else {
1173 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1174 srcRectPtr->fTop,
1175 srcRectPtr->fRight,
1176 srcRectPtr->fBottom,
1177 maxTextureSize);
1178 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1179 srcRectPtr->fTop,
1180 srcRectPtr->fRight,
1181 srcRectPtr->fBottom,
1182 kSmallTileSize);
1183 }
1184 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1185 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1186
1187 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1188 return kSmallTileSize;
1189 } else {
1190 return maxTextureSize;
1191 }
1192}
1193}
1194
1195bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001196 const GrTextureParams& params,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001197 const SkIRect* srcRectPtr,
1198 int* tileSize) const {
1199 SkASSERT(NULL != tileSize);
1200
1201 // if bitmap is explictly texture backed then just use the texture
1202 if (NULL != bitmap.getTexture()) {
1203 return false;
1204 }
1205 // if it's larger than the max texture size, then we have no choice but
1206 // tiling
1207 const int maxTextureSize = fContext->getMaxTextureSize();
1208 if (bitmap.width() > maxTextureSize ||
1209 bitmap.height() > maxTextureSize) {
1210 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1211 return true;
1212 }
1213 // if we are going to have to draw the whole thing, then don't tile
1214 if (NULL == srcRectPtr) {
1215 return false;
1216 }
1217 // if the entire texture is already in our cache then no reason to tile it
bsalomon@google.comb8670992012-07-25 21:27:09 +00001218 if (this->isBitmapInTextureCache(bitmap, params)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001219 return false;
1220 }
1221
1222 // At this point we know we could do the draw by uploading the entire bitmap
1223 // as a texture. However, if the texture would be large compared to the
1224 // cache size and we don't require most of it for this draw then tile to
1225 // reduce the amount of upload and cache spill.
1226
1227 // assumption here is that sw bitmap size is a good proxy for its size as
1228 // a texture
1229 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001230 size_t cacheSize;
1231 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001232 if (bmpSize < cacheSize / 2) {
1233 return false;
1234 }
1235
1236 SkFixed fracUsed =
1237 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1238 (srcRectPtr->height() << 16) / bitmap.height());
1239 if (fracUsed <= SK_FixedHalf) {
1240 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1241 return true;
1242 } else {
1243 return false;
1244 }
1245}
1246
reed@google.comac10a2d2010-12-22 21:39:39 +00001247void SkGpuDevice::drawBitmap(const SkDraw& draw,
1248 const SkBitmap& bitmap,
1249 const SkIRect* srcRectPtr,
1250 const SkMatrix& m,
1251 const SkPaint& paint) {
1252 CHECK_SHOULD_DRAW(draw);
1253
1254 SkIRect srcRect;
1255 if (NULL == srcRectPtr) {
1256 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1257 } else {
1258 srcRect = *srcRectPtr;
1259 }
1260
junov@google.comd935cfb2011-06-27 20:48:23 +00001261 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001262 // Convert the bitmap to a shader so that the rect can be drawn
1263 // through drawRect, which supports mask filters.
1264 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001265 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001266 if (srcRectPtr) {
1267 if (!bitmap.extractSubset(&tmp, srcRect)) {
1268 return; // extraction failed
1269 }
1270 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001271 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001272 }
1273 SkPaint paintWithTexture(paint);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001274 paintWithTexture.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
junov@google.comd935cfb2011-06-27 20:48:23 +00001275 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001276 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001277 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001278
junov@google.com1d329782011-07-28 20:10:09 +00001279 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001280 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001281 // also affect the behavior of the mask filter.
1282 SkMatrix drawMatrix;
1283 drawMatrix.setConcat(*draw.fMatrix, m);
1284 SkDraw transformedDraw(draw);
1285 transformedDraw.fMatrix = &drawMatrix;
1286
1287 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1288
junov@google.comd935cfb2011-06-27 20:48:23 +00001289 return;
1290 }
1291
bsalomon@google.com5782d712011-01-21 21:03:59 +00001292 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001293 SkAutoCachedTexture colorLutTexture;
1294 if (!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001295 return;
1296 }
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001297 GrTextureParams params;
1298 params.setBilerp(paint.isFilterBitmap());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001299
bsalomon@google.comfb309512011-11-30 14:13:48 +00001300 int tileSize;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001301 if (!this->shouldTileBitmap(bitmap, params, srcRectPtr, &tileSize)) {
bsalomon@google.comfb309512011-11-30 14:13:48 +00001302 // take the simple case
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001303 this->internalDrawBitmap(draw, bitmap, srcRect, m, params, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001304 return;
1305 }
1306
1307 // undo the translate done by SkCanvas
1308 int DX = SkMax32(0, srcRect.fLeft);
1309 int DY = SkMax32(0, srcRect.fTop);
1310 // compute clip bounds in local coordinates
1311 SkIRect clipRect;
1312 {
1313 SkRect r;
1314 r.set(draw.fClip->getBounds());
1315 SkMatrix matrix, inverse;
1316 matrix.setConcat(*draw.fMatrix, m);
1317 if (!matrix.invert(&inverse)) {
1318 return;
1319 }
1320 inverse.mapRect(&r);
1321 r.roundOut(&clipRect);
1322 // apply the canvas' translate to our local clip
1323 clipRect.offset(DX, DY);
1324 }
1325
bsalomon@google.comfb309512011-11-30 14:13:48 +00001326 int nx = bitmap.width() / tileSize;
1327 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001328 for (int x = 0; x <= nx; x++) {
1329 for (int y = 0; y <= ny; y++) {
1330 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001331 tileR.set(x * tileSize, y * tileSize,
1332 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001333 if (!SkIRect::Intersects(tileR, clipRect)) {
1334 continue;
1335 }
1336
1337 SkIRect srcR = tileR;
1338 if (!srcR.intersect(srcRect)) {
1339 continue;
1340 }
1341
1342 SkBitmap tmpB;
1343 if (bitmap.extractSubset(&tmpB, tileR)) {
1344 // now offset it to make it "local" to our tmp bitmap
1345 srcR.offset(-tileR.fLeft, -tileR.fTop);
1346
1347 SkMatrix tmpM(m);
1348 {
1349 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1350 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1351 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1352 }
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001353 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, params, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001354 }
1355 }
1356 }
1357}
1358
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001359namespace {
1360
1361bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1362 // detect pixel disalignment
1363 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1364 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
rmistry@google.comd6176b02012-08-23 18:14:13 +00001365 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001366 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1367 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1368 COLOR_BLEED_TOLERANCE &&
1369 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1370 COLOR_BLEED_TOLERANCE) {
1371 return true;
1372 }
1373 return false;
1374}
1375
1376bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1377 const SkMatrix& m) {
1378 // Only gets called if hasAlignedSamples returned false.
1379 // So we can assume that sampling is axis aligned but not texel aligned.
1380 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
rmistry@google.comd6176b02012-08-23 18:14:13 +00001381 SkRect innerSrcRect(srcRect), innerTransformedRect,
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001382 outerTransformedRect(transformedRect);
1383 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1384 m.mapRect(&innerTransformedRect, innerSrcRect);
1385
1386 // The gap between outerTransformedRect and innerTransformedRect
1387 // represents the projection of the source border area, which is
1388 // problematic for color bleeding. We must check whether any
1389 // destination pixels sample the border area.
1390 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1391 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1392 SkIRect outer, inner;
1393 outerTransformedRect.round(&outer);
1394 innerTransformedRect.round(&inner);
1395 // If the inner and outer rects round to the same result, it means the
1396 // border does not overlap any pixel centers. Yay!
1397 return inner != outer;
1398}
1399
1400} // unnamed namespace
1401
reed@google.comac10a2d2010-12-22 21:39:39 +00001402/*
1403 * This is called by drawBitmap(), which has to handle images that may be too
1404 * large to be represented by a single texture.
1405 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001406 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1407 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001408 */
1409void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1410 const SkBitmap& bitmap,
1411 const SkIRect& srcRect,
1412 const SkMatrix& m,
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001413 const GrTextureParams& params,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001414 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001415 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1416 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001417
reed@google.com9c49bc32011-07-07 13:42:37 +00001418 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001419 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001420 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001421 return;
1422 }
1423
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001424 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001425
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001426 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001427
1428 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001429 SkAutoCachedTexture act(this, bitmap, &params, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001430 if (NULL == texture) {
1431 return;
1432 }
1433
reed@google.com20efde72011-05-09 17:00:02 +00001434 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1435 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001436 GrRect paintRect;
bsalomon@google.com91832162012-03-08 19:53:02 +00001437 float wInv = 1.f / bitmap.width();
1438 float hInv = 1.f / bitmap.height();
1439 paintRect.setLTRB(SkFloatToScalar(srcRect.fLeft * wInv),
1440 SkFloatToScalar(srcRect.fTop * hInv),
1441 SkFloatToScalar(srcRect.fRight * wInv),
1442 SkFloatToScalar(srcRect.fBottom * hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001443
rmistry@google.comd6176b02012-08-23 18:14:13 +00001444 bool needsTextureDomain = false;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001445 if (params.isBilerp()) {
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001446 // Need texture domain if drawing a sub rect.
bsalomon@google.comb8670992012-07-25 21:27:09 +00001447 needsTextureDomain = srcRect.width() < bitmap.width() || srcRect.height() < bitmap.height();
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001448 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1449 // sampling is axis-aligned
1450 GrRect floatSrcRect, transformedRect;
1451 floatSrcRect.set(srcRect);
1452 SkMatrix srcToDeviceMatrix(m);
1453 srcToDeviceMatrix.postConcat(*draw.fMatrix);
1454 srcToDeviceMatrix.mapRect(&transformedRect, floatSrcRect);
rmistry@google.comd6176b02012-08-23 18:14:13 +00001455
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001456 if (hasAlignedSamples(floatSrcRect, transformedRect)) {
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001457 // We could also turn off filtering here (but we already did a cache lookup with
1458 // params).
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001459 needsTextureDomain = false;
1460 } else {
1461 needsTextureDomain = needsTextureDomain &&
1462 mayColorBleed(floatSrcRect, transformedRect, m);
1463 }
1464 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001465 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001466
1467 GrRect textureDomain = GrRect::MakeEmpty();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001468 SkAutoTUnref<GrCustomStage> stage;
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001469 if (needsTextureDomain) {
1470 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001471 GrScalar left, top, right, bottom;
1472 if (srcRect.width() > 1) {
1473 GrScalar border = GR_ScalarHalf / bitmap.width();
1474 left = paintRect.left() + border;
1475 right = paintRect.right() - border;
1476 } else {
1477 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1478 }
1479 if (srcRect.height() > 1) {
1480 GrScalar border = GR_ScalarHalf / bitmap.height();
1481 top = paintRect.top() + border;
1482 bottom = paintRect.bottom() - border;
1483 } else {
1484 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1485 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001486 textureDomain.setLTRB(left, top, right, bottom);
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001487 stage.reset(SkNEW_ARGS(GrTextureDomainEffect, (texture, textureDomain, params)));
1488 } else {
1489 stage.reset(SkNEW_ARGS(GrSingleTextureEffect, (texture, params)));
junov@google.com6acc9b32011-05-16 18:32:07 +00001490 }
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001491 grPaint->textureSampler(kBitmapTextureIdx)->setCustomStage(stage);
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001492 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001493}
1494
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001495namespace {
1496
1497void apply_custom_stage(GrContext* context,
1498 GrTexture* srcTexture,
1499 GrTexture* dstTexture,
1500 const GrRect& rect,
1501 GrCustomStage* stage) {
1502 SkASSERT(srcTexture && srcTexture->getContext() == context);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001503 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001504 GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget());
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001505 GrContext::AutoClip acs(context, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001506
1507 GrMatrix sampleM;
1508 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
1509 GrPaint paint;
1510 paint.reset();
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001511 paint.textureSampler(0)->reset(sampleM);
1512 paint.textureSampler(0)->setCustomStage(stage);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001513 context->drawRect(paint, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001514}
1515
1516};
1517
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001518static GrTexture* filter_texture(SkDevice* device, GrContext* context,
1519 GrTexture* texture, SkImageFilter* filter,
1520 const GrRect& rect) {
reed@google.com8926b162012-03-23 15:36:36 +00001521 GrAssert(filter);
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001522 SkDeviceImageFilterProxy proxy(device);
reed@google.com8926b162012-03-23 15:36:36 +00001523
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001524 GrTextureDesc desc;
1525 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1526 desc.fWidth = SkScalarCeilToInt(rect.width());
1527 desc.fHeight = SkScalarCeilToInt(rect.height());
bsalomon@google.com0342a852012-08-20 19:22:38 +00001528 desc.fConfig = kRGBA_8888_GrPixelConfig;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001529 GrCustomStage* stage;
reed@google.com8926b162012-03-23 15:36:36 +00001530
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001531 if (filter->canFilterImageGPU()) {
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001532 texture = filter->onFilterImageGPU(&proxy, texture, rect);
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001533 } else if (filter->asNewCustomStage(&stage, texture)) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001534 GrAutoScratchTexture dst(context, desc);
1535 apply_custom_stage(context, texture, dst.texture(), rect, stage);
1536 texture = dst.detach();
1537 stage->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001538 }
1539 return texture;
1540}
1541
reed@google.comac10a2d2010-12-22 21:39:39 +00001542void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1543 int left, int top, const SkPaint& paint) {
1544 CHECK_SHOULD_DRAW(draw);
1545
reed@google.com8926b162012-03-23 15:36:36 +00001546 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001547 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1548 return;
1549 }
1550
reed@google.com76dd2772012-01-05 21:15:07 +00001551 int w = bitmap.width();
1552 int h = bitmap.height();
1553
bsalomon@google.com5782d712011-01-21 21:03:59 +00001554 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001555 SkAutoCachedTexture colorLutTexture;
1556 if(!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001557 return;
1558 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001559
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001560 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001561
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001562 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001563
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001564 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001565 sampler->reset();
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001566 // draw sprite uses the default texture params
1567 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001568 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1569 (GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001570
reed@google.com8926b162012-03-23 15:36:36 +00001571 SkImageFilter* filter = paint.getImageFilter();
1572 if (NULL != filter) {
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001573 GrTexture* filteredTexture = filter_texture(this, fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001574 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001575 if (filteredTexture) {
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001576 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1577 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001578 texture = filteredTexture;
1579 filteredTexture->unref();
1580 }
reed@google.com76dd2772012-01-05 21:15:07 +00001581 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001582
bsalomon@google.com5782d712011-01-21 21:03:59 +00001583 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001584 GrRect::MakeXYWH(GrIntToScalar(left),
1585 GrIntToScalar(top),
1586 GrIntToScalar(w),
1587 GrIntToScalar(h)),
1588 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1589 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001590}
1591
reed@google.com33535f32012-09-25 15:37:50 +00001592void SkGpuDevice::drawBitmapRect(const SkDraw& draw, const SkBitmap& bitmap,
1593 const SkRect* src, const SkRect& dst,
1594 const SkPaint& paint) {
1595 SkMatrix matrix;
1596 // Compute matrix from the two rectangles
1597 {
1598 SkRect tmpSrc;
1599 if (src) {
1600 tmpSrc = *src;
1601 // if the extract process clipped off the top or left of the
1602 // original, we adjust for that here to get the position right.
1603 if (tmpSrc.fLeft > 0) {
1604 tmpSrc.fRight -= tmpSrc.fLeft;
1605 tmpSrc.fLeft = 0;
1606 }
1607 if (tmpSrc.fTop > 0) {
1608 tmpSrc.fBottom -= tmpSrc.fTop;
1609 tmpSrc.fTop = 0;
1610 }
1611 } else {
1612 tmpSrc.set(0, 0, SkIntToScalar(bitmap.width()),
1613 SkIntToScalar(bitmap.height()));
1614 }
1615 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1616 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +00001617
reed@google.com33535f32012-09-25 15:37:50 +00001618 // ensure that src is "valid" before we pass it to our internal routines
1619 // and to SkDevice. i.e. sure it is contained inside the original bitmap.
1620 SkIRect isrcStorage;
1621 SkIRect* isrcPtr = NULL;
1622 if (src) {
1623 src->roundOut(&isrcStorage);
1624 if (!isrcStorage.intersect(0, 0, bitmap.width(), bitmap.height())) {
1625 return;
1626 }
1627 isrcPtr = &isrcStorage;
1628 }
1629
1630 this->drawBitmap(draw, bitmap, isrcPtr, matrix, paint);
1631}
1632
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001633void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
reed@google.comac10a2d2010-12-22 21:39:39 +00001634 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001635 // clear of the source device must occur before CHECK_SHOULD_DRAW
1636 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1637 if (dev->fNeedClear) {
1638 // TODO: could check here whether we really need to draw at all
1639 dev->clear(0x0);
1640 }
1641
reed@google.comac10a2d2010-12-22 21:39:39 +00001642 CHECK_SHOULD_DRAW(draw);
1643
bsalomon@google.com5782d712011-01-21 21:03:59 +00001644 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001645 SkAutoCachedTexture colorLutTexture;
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001646 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001647 if (!dev->bindDeviceAsTexture(&grPaint) ||
twiz@google.com58071162012-07-18 21:41:50 +00001648 !skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001649 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001650 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001651
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001652 GrTexture* devTex = grPaint.getTextureSampler(kBitmapTextureIdx).getCustomStage()->texture(0);
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001653 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001654
reed@google.com8926b162012-03-23 15:36:36 +00001655 SkImageFilter* filter = paint.getImageFilter();
1656 if (NULL != filter) {
bsalomon@google.com1c31f632012-07-26 19:39:06 +00001657 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001658 SkIntToScalar(devTex->height()));
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001659 GrTexture* filteredTexture = filter_texture(this, fContext, devTex, filter, rect);
reed@google.com8926b162012-03-23 15:36:36 +00001660 if (filteredTexture) {
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001661 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1662 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001663 devTex = filteredTexture;
1664 filteredTexture->unref();
1665 }
1666 }
rmistry@google.comd6176b02012-08-23 18:14:13 +00001667
bsalomon@google.com5782d712011-01-21 21:03:59 +00001668 const SkBitmap& bm = dev->accessBitmap(false);
1669 int w = bm.width();
1670 int h = bm.height();
1671
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001672 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001673 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1674 GrIntToScalar(y),
1675 GrIntToScalar(w),
1676 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001677
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001678 // The device being drawn may not fill up its texture (saveLayer uses
1679 // the approximate ).
1680 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1681 GR_Scalar1 * h / devTex->height());
1682
1683 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001684}
1685
reed@google.com8926b162012-03-23 15:36:36 +00001686bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001687 if (!filter->asNewCustomStage(NULL, NULL) &&
senorblanco@chromium.org302cffb2012-08-01 20:16:34 +00001688 !filter->canFilterImageGPU()) {
reed@google.com76dd2772012-01-05 21:15:07 +00001689 return false;
1690 }
reed@google.com8926b162012-03-23 15:36:36 +00001691 return true;
1692}
1693
1694bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1695 const SkMatrix& ctm,
1696 SkBitmap* result, SkIPoint* offset) {
1697 // want explicitly our impl, so guard against a subclass of us overriding it
1698 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001699 return false;
1700 }
reed@google.com8926b162012-03-23 15:36:36 +00001701
1702 SkAutoLockPixels alp(src, !src.getTexture());
1703 if (!src.getTexture() && !src.readyToDraw()) {
1704 return false;
1705 }
1706
1707 GrPaint paint;
1708 paint.reset();
1709
reed@google.com8926b162012-03-23 15:36:36 +00001710 GrTexture* texture;
bsalomon@google.com1ce49fc2012-09-18 14:14:49 +00001711 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1712 // must be pushed upstack.
1713 SkAutoCachedTexture act(this, src, NULL, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001714
1715 result->setConfig(src.config(), src.width(), src.height());
rmistry@google.comd6176b02012-08-23 18:14:13 +00001716 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
robertphillips@google.com8637a362012-04-10 18:32:35 +00001717 SkIntToScalar(src.height()));
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001718 GrTexture* resultTexture = filter_texture(this, fContext, texture, filter, rect);
reed@google.com8926b162012-03-23 15:36:36 +00001719 if (resultTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001720 result->setPixelRef(SkNEW_ARGS(SkGrTexturePixelRef,
1721 (resultTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001722 resultTexture->unref();
1723 }
reed@google.com76dd2772012-01-05 21:15:07 +00001724 return true;
1725}
1726
reed@google.comac10a2d2010-12-22 21:39:39 +00001727///////////////////////////////////////////////////////////////////////////////
1728
1729// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001730static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001731 kTriangles_GrPrimitiveType,
1732 kTriangleStrip_GrPrimitiveType,
1733 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001734};
1735
1736void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1737 int vertexCount, const SkPoint vertices[],
1738 const SkPoint texs[], const SkColor colors[],
1739 SkXfermode* xmode,
1740 const uint16_t indices[], int indexCount,
1741 const SkPaint& paint) {
1742 CHECK_SHOULD_DRAW(draw);
1743
bsalomon@google.com5782d712011-01-21 21:03:59 +00001744 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001745 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com5782d712011-01-21 21:03:59 +00001746 // we ignore the shader if texs is null.
1747 if (NULL == texs) {
twiz@google.com58071162012-07-18 21:41:50 +00001748 if (!skPaint2GrPaintNoShader(this,
1749 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001750 false,
1751 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001752 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +00001753 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001754 return;
1755 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001756 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001757 if (!skPaint2GrPaintShader(this,
1758 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001759 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001760 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001761 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001762 return;
1763 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001764 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001765
1766 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001767 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001768 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1769#if 0
1770 return
1771#endif
1772 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001773 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001774
bsalomon@google.com498776a2011-08-16 19:20:44 +00001775 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1776 if (NULL != colors) {
1777 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001778 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001779 for (int i = 0; i < vertexCount; ++i) {
rileya@google.com24f3ad12012-07-18 21:47:40 +00001780 convertedColors[i] = SkColor2GrColor(colors[i]);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001781 }
1782 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001783 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001784 fContext->drawVertices(grPaint,
1785 gVertexMode2PrimitiveType[vmode],
1786 vertexCount,
1787 (GrPoint*) vertices,
1788 (GrPoint*) texs,
1789 colors,
1790 indices,
1791 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001792}
1793
1794///////////////////////////////////////////////////////////////////////////////
1795
1796static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001797 GrFontScaler* scaler = (GrFontScaler*)data;
1798 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001799}
1800
1801static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1802 void* auxData;
1803 GrFontScaler* scaler = NULL;
1804 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1805 scaler = (GrFontScaler*)auxData;
1806 }
1807 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001808 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001809 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1810 }
1811 return scaler;
1812}
1813
1814static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1815 SkFixed fx, SkFixed fy,
1816 const SkGlyph& glyph) {
1817 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1818
bungeman@google.com15865a72012-01-11 16:28:04 +00001819 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001820
1821 if (NULL == procs->fFontScaler) {
1822 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1823 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001824
bungeman@google.com15865a72012-01-11 16:28:04 +00001825 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1826 glyph.getSubXFixed(),
1827 glyph.getSubYFixed()),
1828 SkFixedFloorToFixed(fx),
1829 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001830 procs->fFontScaler);
1831}
1832
bsalomon@google.com5782d712011-01-21 21:03:59 +00001833SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001834
1835 // deferred allocation
1836 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001837 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001838 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1839 fDrawProcs->fContext = fContext;
1840 }
1841
1842 // init our (and GL's) state
1843 fDrawProcs->fTextContext = context;
1844 fDrawProcs->fFontScaler = NULL;
1845 return fDrawProcs;
1846}
1847
1848void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1849 size_t byteLength, SkScalar x, SkScalar y,
1850 const SkPaint& paint) {
1851 CHECK_SHOULD_DRAW(draw);
1852
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001853 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001854 // this guy will just call our drawPath()
1855 draw.drawText((const char*)text, byteLength, x, y, paint);
1856 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001857 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001858
1859 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001860 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001861 if (!skPaint2GrPaintShader(this,
1862 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001863 true,
twiz@google.com58071162012-07-18 21:41:50 +00001864 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001865 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001866 return;
1867 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001868 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1869 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001870 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1871 }
1872}
1873
1874void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1875 size_t byteLength, const SkScalar pos[],
1876 SkScalar constY, int scalarsPerPos,
1877 const SkPaint& paint) {
1878 CHECK_SHOULD_DRAW(draw);
1879
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001880 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001881 // this guy will just call our drawPath()
1882 draw.drawPosText((const char*)text, byteLength, pos, constY,
1883 scalarsPerPos, paint);
1884 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001885 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001886
1887 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001888 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001889 if (!skPaint2GrPaintShader(this,
1890 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001891 true,
twiz@google.com58071162012-07-18 21:41:50 +00001892 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001893 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001894 return;
1895 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001896 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1897 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001898 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1899 scalarsPerPos, paint);
1900 }
1901}
1902
1903void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1904 size_t len, const SkPath& path,
1905 const SkMatrix* m, const SkPaint& paint) {
1906 CHECK_SHOULD_DRAW(draw);
1907
1908 SkASSERT(draw.fDevice == this);
1909 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1910}
1911
1912///////////////////////////////////////////////////////////////////////////////
1913
reed@google.comf67e4cf2011-03-15 20:56:58 +00001914bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1915 if (!paint.isLCDRenderText()) {
1916 // we're cool with the paint as is
1917 return false;
1918 }
1919
1920 if (paint.getShader() ||
1921 paint.getXfermode() || // unless its srcover
1922 paint.getMaskFilter() ||
1923 paint.getRasterizer() ||
1924 paint.getColorFilter() ||
1925 paint.getPathEffect() ||
1926 paint.isFakeBoldText() ||
1927 paint.getStyle() != SkPaint::kFill_Style) {
1928 // turn off lcd
1929 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1930 flags->fHinting = paint.getHinting();
1931 return true;
1932 }
1933 // we're cool with the paint as is
1934 return false;
1935}
1936
reed@google.com75d939b2011-12-07 15:07:23 +00001937void SkGpuDevice::flush() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001938 DO_DEFERRED_CLEAR;
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001939 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001940}
1941
reed@google.comf67e4cf2011-03-15 20:56:58 +00001942///////////////////////////////////////////////////////////////////////////////
1943
bsalomon@google.comfb309512011-11-30 14:13:48 +00001944bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
bsalomon@google.comb8670992012-07-25 21:27:09 +00001945 const GrTextureParams& params) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001946 uint64_t key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001947 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001948
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001949 GrTextureDesc desc;
1950 desc.fWidth = bitmap.width();
1951 desc.fHeight = bitmap.height();
rileya@google.com24f3ad12012-07-18 21:47:40 +00001952 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config());
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001953
robertphillips@google.com9c2ea842012-08-13 17:47:59 +00001954 GrCacheData cacheData(key);
1955
1956 return this->context()->isTextureInCache(desc, cacheData, &params);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001957}
1958
1959
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001960SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1961 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001962 bool isOpaque,
1963 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001964 GrTextureDesc desc;
1965 desc.fConfig = fRenderTarget->config();
1966 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1967 desc.fWidth = width;
1968 desc.fHeight = height;
1969 desc.fSampleCnt = fRenderTarget->numSamples();
1970
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001971 GrTexture* texture;
1972 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001973 // Skia's convention is to only clear a device if it is non-opaque.
1974 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001975
1976#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1977 // layers are never draw in repeat modes, so we can request an approx
1978 // match and ignore any padding.
1979 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1980 GrContext::kApprox_ScratchTexMatch :
1981 GrContext::kExact_ScratchTexMatch;
robertphillips@google.com1f47f4f2012-08-16 14:49:16 +00001982 texture = fContext->lockScratchTexture(desc, matchType);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001983#else
1984 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1985 texture = tunref.get();
1986#endif
1987 if (texture) {
1988 return SkNEW_ARGS(SkGpuDevice,(fContext,
1989 texture,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001990 needClear));
1991 } else {
1992 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1993 width, height);
1994 return NULL;
1995 }
1996}
1997
1998SkGpuDevice::SkGpuDevice(GrContext* context,
1999 GrTexture* texture,
bsalomon@google.com06cd7322012-03-30 18:45:35 +00002000 bool needClear)
robertphillips@google.com641f8b12012-07-31 19:15:58 +00002001 : SkDevice(make_bitmap(context, texture->asRenderTarget())) {
2002
bsalomon@google.com06cd7322012-03-30 18:45:35 +00002003 GrAssert(texture && texture->asRenderTarget());
bsalomon@google.com8090e652012-08-28 15:07:11 +00002004 // This constructor is called from onCreateCompatibleDevice. It has locked the RT in the texture
2005 // cache. We pass true for the third argument so that it will get unlocked.
2006 this->initFromRenderTarget(context, texture->asRenderTarget(), true);
bsalomon@google.com06cd7322012-03-30 18:45:35 +00002007 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00002008}