blob: d6e37833fb32f9e1865dc79127248803fbc77659 [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
tomhudson@google.com898e7b52012-06-01 20:42:15 +000010#include "effects/GrGradientEffects.h"
twiz@google.com58071162012-07-18 21:41:50 +000011#include "effects/GrColorTableEffect.h"
tomhudson@google.com2f68e762012-07-17 18:43:21 +000012#include "effects/GrTextureDomainEffect.h"
epoger@google.comec3ed6a2011-07-28 14:26:00 +000013
reed@google.comac10a2d2010-12-22 21:39:39 +000014#include "GrContext.h"
15#include "GrTextContext.h"
16
robertphillips@google.come9c04692012-06-29 00:30:13 +000017#include "SkGrTexturePixelRef.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000018
Scroggo97c88c22011-05-11 14:05:25 +000019#include "SkColorFilter.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
63// requiring texture domain clamping to prevent color bleeding when drawing
64// 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:
85 SkAutoCachedTexture() { }
86 SkAutoCachedTexture(SkGpuDevice* device,
87 const SkBitmap& bitmap,
88 const GrSamplerState* sampler,
89 GrTexture** texture) {
90 GrAssert(texture);
91 *texture = this->set(device, bitmap, sampler);
reed@google.comac10a2d2010-12-22 21:39:39 +000092 }
reed@google.comac10a2d2010-12-22 21:39:39 +000093
bsalomon@google.com84405e02012-03-05 19:57:21 +000094 ~SkAutoCachedTexture() {
95 if (fTex.texture()) {
rileya@google.com24f3ad12012-07-18 21:47:40 +000096 GrUnlockCachedBitmapTexture(fDevice->context(), fTex);
bsalomon@google.com84405e02012-03-05 19:57:21 +000097 }
reed@google.comac10a2d2010-12-22 21:39:39 +000098 }
bsalomon@google.com84405e02012-03-05 19:57:21 +000099
100 GrTexture* set(SkGpuDevice* device,
101 const SkBitmap& bitmap,
102 const GrSamplerState* sampler) {
103 if (fTex.texture()) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000104 GrUnlockCachedBitmapTexture(fDevice->context(), fTex);
bsalomon@google.com84405e02012-03-05 19:57:21 +0000105 }
106 fDevice = device;
107 GrTexture* texture = (GrTexture*)bitmap.getTexture();
108 if (texture) {
109 // return the native texture
110 fTex.reset();
111 } else {
112 // look it up in our cache
rileya@google.com24f3ad12012-07-18 21:47:40 +0000113 fTex = GrLockCachedBitmapTexture(device->context(), bitmap, sampler);
bsalomon@google.com84405e02012-03-05 19:57:21 +0000114 texture = fTex.texture();
115 }
116 return texture;
117 }
118
119private:
120 SkGpuDevice* fDevice;
121 GrContext::TextureCacheEntry fTex;
122};
reed@google.comac10a2d2010-12-22 21:39:39 +0000123
124///////////////////////////////////////////////////////////////////////////////
125
126bool gDoTraceDraw;
127
128struct GrSkDrawProcs : public SkDrawProcs {
129public:
130 GrContext* fContext;
131 GrTextContext* fTextContext;
132 GrFontScaler* fFontScaler; // cached in the skia glyphcache
133};
134
135///////////////////////////////////////////////////////////////////////////////
136
reed@google.comaf951c92011-06-16 19:10:39 +0000137static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
138 switch (config) {
139 case kAlpha_8_GrPixelConfig:
140 *isOpaque = false;
141 return SkBitmap::kA8_Config;
142 case kRGB_565_GrPixelConfig:
143 *isOpaque = true;
144 return SkBitmap::kRGB_565_Config;
145 case kRGBA_4444_GrPixelConfig:
146 *isOpaque = false;
147 return SkBitmap::kARGB_4444_Config;
bsalomon@google.comc4364992011-11-07 15:54:49 +0000148 case kSkia8888_PM_GrPixelConfig:
149 // we don't currently have a way of knowing whether
150 // a 8888 is opaque based on the config.
151 *isOpaque = false;
reed@google.comaf951c92011-06-16 19:10:39 +0000152 return SkBitmap::kARGB_8888_Config;
153 default:
154 *isOpaque = false;
155 return SkBitmap::kNo_Config;
156 }
157}
reed@google.comac10a2d2010-12-22 21:39:39 +0000158
reed@google.comaf951c92011-06-16 19:10:39 +0000159static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000160 GrPixelConfig config = renderTarget->config();
reed@google.comaf951c92011-06-16 19:10:39 +0000161
162 bool isOpaque;
163 SkBitmap bitmap;
164 bitmap.setConfig(grConfig2skConfig(config, &isOpaque),
165 renderTarget->width(), renderTarget->height());
166 bitmap.setIsOpaque(isOpaque);
167 return bitmap;
168}
169
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000170SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture)
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000171: SkDevice(make_bitmap(context, texture->asRenderTarget()))
172, fClipStack(NULL) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000173 this->initFromRenderTarget(context, texture->asRenderTarget());
174}
175
reed@google.comaf951c92011-06-16 19:10:39 +0000176SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget)
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000177: SkDevice(make_bitmap(context, renderTarget))
178, fClipStack(NULL) {
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000179 this->initFromRenderTarget(context, renderTarget);
180}
181
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000182void SkGpuDevice::initFromRenderTarget(GrContext* context,
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000183 GrRenderTarget* renderTarget) {
reed@google.comaf951c92011-06-16 19:10:39 +0000184 fNeedPrepareRenderTarget = false;
185 fDrawProcs = NULL;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000186
reed@google.comaf951c92011-06-16 19:10:39 +0000187 fContext = context;
188 fContext->ref();
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000189
reed@google.comaf951c92011-06-16 19:10:39 +0000190 fTexture = NULL;
191 fRenderTarget = NULL;
192 fNeedClear = false;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000193
bsalomon@google.com971d0c82011-08-19 17:22:05 +0000194 GrAssert(NULL != renderTarget);
195 fRenderTarget = renderTarget;
196 fRenderTarget->ref();
197 // if this RT is also a texture, hold a ref on it
198 fTexture = fRenderTarget->asTexture();
199 SkSafeRef(fTexture);
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000200
201 // Create a pixel ref for the underlying SkBitmap. We prefer a texture pixel
202 // ref to a render target pixel reft. The pixel ref may get ref'ed outside
203 // the device via accessBitmap. This external ref may outlive the device.
204 // Since textures own their render targets (but not vice-versa) we
205 // are ensuring that both objects will live as long as the pixel ref.
206 SkPixelRef* pr;
207 if (fTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000208 pr = SkNEW_ARGS(SkGrTexturePixelRef, (fTexture));
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000209 } else {
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000210 pr = SkNEW_ARGS(SkGrRenderTargetPixelRef, (fRenderTarget));
bsalomon@google.comd9ce1252012-01-24 02:31:42 +0000211 }
reed@google.comaf951c92011-06-16 19:10:39 +0000212 this->setPixelRef(pr, 0)->unref();
213}
214
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000215SkGpuDevice::SkGpuDevice(GrContext* context,
216 SkBitmap::Config config,
217 int width,
218 int height)
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000219 : SkDevice(config, width, height, false /*isOpaque*/)
220 , fClipStack(NULL) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000221 fNeedPrepareRenderTarget = false;
222 fDrawProcs = NULL;
223
reed@google.com7b201d22011-01-11 18:59:23 +0000224 fContext = context;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000225 fContext->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000226
reed@google.comac10a2d2010-12-22 21:39:39 +0000227 fTexture = NULL;
228 fRenderTarget = NULL;
229 fNeedClear = false;
230
reed@google.comaf951c92011-06-16 19:10:39 +0000231 if (config != SkBitmap::kRGB_565_Config) {
232 config = SkBitmap::kARGB_8888_Config;
233 }
234 SkBitmap bm;
235 bm.setConfig(config, width, height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000236
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000237 GrTextureDesc desc;
238 desc.fFlags = kRenderTarget_GrTextureFlagBit;
239 desc.fWidth = width;
240 desc.fHeight = height;
rileya@google.com24f3ad12012-07-18 21:47:40 +0000241 desc.fConfig = SkBitmapConfig2GrPixelConfig(bm.config());
reed@google.comac10a2d2010-12-22 21:39:39 +0000242
reed@google.comaf951c92011-06-16 19:10:39 +0000243 fTexture = fContext->createUncachedTexture(desc, NULL, 0);
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000244
reed@google.comaf951c92011-06-16 19:10:39 +0000245 if (NULL != fTexture) {
246 fRenderTarget = fTexture->asRenderTarget();
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000247 fRenderTarget->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000248
reed@google.comaf951c92011-06-16 19:10:39 +0000249 GrAssert(NULL != fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000250
reed@google.comaf951c92011-06-16 19:10:39 +0000251 // wrap the bitmap with a pixelref to expose our texture
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000252 SkGrTexturePixelRef* pr = SkNEW_ARGS(SkGrTexturePixelRef, (fTexture));
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000253 this->setPixelRef(pr, 0)->unref();
reed@google.comaf951c92011-06-16 19:10:39 +0000254 } else {
255 GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
256 width, height);
257 GrAssert(false);
reed@google.comac10a2d2010-12-22 21:39:39 +0000258 }
259}
260
261SkGpuDevice::~SkGpuDevice() {
262 if (fDrawProcs) {
263 delete fDrawProcs;
264 }
265
robertphillips@google.com9ec07532012-06-22 12:01:30 +0000266 // The SkGpuDevice gives the context the render target (e.g., in gainFocus)
267 // This call gives the context a chance to relinquish it
268 fContext->setRenderTarget(NULL);
269
bsalomon@google.comf9046fe2011-06-17 15:10:21 +0000270 SkSafeUnref(fTexture);
271 SkSafeUnref(fRenderTarget);
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000272 if (fCache.texture()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000273 GrAssert(NULL != fTexture);
274 GrAssert(fRenderTarget == fTexture->asRenderTarget());
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000275 fContext->unlockTexture(fCache);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000276 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000277 fContext->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000278}
279
reed@google.comac10a2d2010-12-22 21:39:39 +0000280///////////////////////////////////////////////////////////////////////////////
281
282void SkGpuDevice::makeRenderTargetCurrent() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000283 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000284 fContext->setRenderTarget(fRenderTarget);
285 fContext->flush(true);
286 fNeedPrepareRenderTarget = true;
287}
288
289///////////////////////////////////////////////////////////////////////////////
290
bsalomon@google.comc4364992011-11-07 15:54:49 +0000291namespace {
292GrPixelConfig config8888_to_gr_config(SkCanvas::Config8888 config8888) {
293 switch (config8888) {
294 case SkCanvas::kNative_Premul_Config8888:
295 return kSkia8888_PM_GrPixelConfig;
296 case SkCanvas::kNative_Unpremul_Config8888:
297 return kSkia8888_UPM_GrPixelConfig;
298 case SkCanvas::kBGRA_Premul_Config8888:
299 return kBGRA_8888_PM_GrPixelConfig;
300 case SkCanvas::kBGRA_Unpremul_Config8888:
301 return kBGRA_8888_UPM_GrPixelConfig;
302 case SkCanvas::kRGBA_Premul_Config8888:
303 return kRGBA_8888_PM_GrPixelConfig;
304 case SkCanvas::kRGBA_Unpremul_Config8888:
305 return kRGBA_8888_UPM_GrPixelConfig;
306 default:
307 GrCrash("Unexpected Config8888.");
308 return kSkia8888_PM_GrPixelConfig;
309 }
310}
311}
312
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000313bool SkGpuDevice::onReadPixels(const SkBitmap& bitmap,
314 int x, int y,
315 SkCanvas::Config8888 config8888) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000316 DO_DEFERRED_CLEAR;
bsalomon@google.com910267d2011-11-02 20:06:25 +0000317 SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
318 SkASSERT(!bitmap.isNull());
319 SkASSERT(SkIRect::MakeWH(this->width(), this->height()).contains(SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height())));
reed@google.comac10a2d2010-12-22 21:39:39 +0000320
bsalomon@google.com910267d2011-11-02 20:06:25 +0000321 SkAutoLockPixels alp(bitmap);
bsalomon@google.comc4364992011-11-07 15:54:49 +0000322 GrPixelConfig config;
323 config = config8888_to_gr_config(config8888);
bsalomon@google.comc6980972011-11-02 19:57:21 +0000324 return fContext->readRenderTargetPixels(fRenderTarget,
325 x, y,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000326 bitmap.width(),
327 bitmap.height(),
bsalomon@google.comc4364992011-11-07 15:54:49 +0000328 config,
bsalomon@google.com910267d2011-11-02 20:06:25 +0000329 bitmap.getPixels(),
330 bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000331}
332
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000333void SkGpuDevice::writePixels(const SkBitmap& bitmap, int x, int y,
334 SkCanvas::Config8888 config8888) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000335 SkAutoLockPixels alp(bitmap);
336 if (!bitmap.readyToDraw()) {
337 return;
338 }
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000339
340 GrPixelConfig config;
341 if (SkBitmap::kARGB_8888_Config == bitmap.config()) {
342 config = config8888_to_gr_config(config8888);
343 } else {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000344 config= SkBitmapConfig2GrPixelConfig(bitmap.config());
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000345 }
346
bsalomon@google.com6f379512011-11-16 20:36:03 +0000347 fRenderTarget->writePixels(x, y, bitmap.width(), bitmap.height(),
348 config, bitmap.getPixels(), bitmap.rowBytes());
reed@google.comac10a2d2010-12-22 21:39:39 +0000349}
350
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000351void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
352 INHERITED::onAttachToCanvas(canvas);
353
354 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
355 fClipStack = canvas->getClipStack();
356}
357
358void SkGpuDevice::onDetachFromCanvas() {
359 INHERITED::onDetachFromCanvas();
360
361 fClipStack = NULL;
362}
363
reed@google.comac10a2d2010-12-22 21:39:39 +0000364///////////////////////////////////////////////////////////////////////////////
365
366static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000367 const SkClipStack& clipStack,
reed@google.com6f8f2922011-03-04 22:27:10 +0000368 const SkRegion& clipRegion,
369 const SkIPoint& origin) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000370 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000371
372 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000373 iter.reset(clipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000374 const SkIRect& skBounds = clipRegion.getBounds();
375 GrRect bounds;
376 bounds.setLTRB(GrIntToScalar(skBounds.fLeft),
377 GrIntToScalar(skBounds.fTop),
378 GrIntToScalar(skBounds.fRight),
379 GrIntToScalar(skBounds.fBottom));
reed@google.com6f8f2922011-03-04 22:27:10 +0000380 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000381 bounds);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000382 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000383}
384
385// call this ever each draw call, to ensure that the context reflects our state,
386// and not the state from some other canvas/device
387void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000388 GrAssert(NULL != fClipStack);
389
reed@google.comac10a2d2010-12-22 21:39:39 +0000390 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000391 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000392
393 fContext->setRenderTarget(fRenderTarget);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000394 SkASSERT(draw.fClipStack && draw.fClipStack == fClipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000395 convert_matrixclip(fContext, *draw.fMatrix,
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000396 *fClipStack, *draw.fClip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000397 fNeedPrepareRenderTarget = false;
398 }
399}
400
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000401void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
402 const SkClipStack& clipStack) {
403 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
404 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000405 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000406}
407
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000408void SkGpuDevice::gainFocus(const SkMatrix& matrix, const SkRegion& clip) {
409
410 GrAssert(NULL != fClipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000411
reed@google.comac10a2d2010-12-22 21:39:39 +0000412 fContext->setRenderTarget(fRenderTarget);
413
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000414 this->INHERITED::gainFocus(matrix, clip);
reed@google.comac10a2d2010-12-22 21:39:39 +0000415
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000416 convert_matrixclip(fContext, matrix, *fClipStack, clip, this->getOrigin());
reed@google.comac10a2d2010-12-22 21:39:39 +0000417
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000418 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000419}
420
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000421SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000422 DO_DEFERRED_CLEAR;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000423 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000424}
425
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000426bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000427 if (NULL != fTexture) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000428 paint->setTexture(kBitmapTextureIdx, fTexture);
reed@google.comac10a2d2010-12-22 21:39:39 +0000429 return true;
430 }
431 return false;
432}
433
434///////////////////////////////////////////////////////////////////////////////
435
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000436SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
437SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
438SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
439SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
440SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
441 shader_type_mismatch);
rileya@google.com3e332582012-07-03 13:43:35 +0000442SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
443 shader_type_mismatch);
rileya@google.com22e57f92012-07-19 15:16:19 +0000444SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
445SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000446
bsalomon@google.com84405e02012-03-05 19:57:21 +0000447namespace {
448
449// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
450// justAlpha indicates that skPaint's alpha should be used rather than the color
451// Callers may subsequently modify the GrPaint. Setting constantColor indicates
452// that the final paint will draw the same color at every pixel. This allows
453// an optimization where the the color filter can be applied to the skPaint's
twiz@google.com58071162012-07-18 21:41:50 +0000454// color once while converting to GrPaint and then ignored.
455inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
456 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000457 bool justAlpha,
458 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000459 SkGpuDevice::SkAutoCachedTexture* act,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000460 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000461
462 grPaint->fDither = skPaint.isDither();
463 grPaint->fAntiAlias = skPaint.isAntiAlias();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000464 grPaint->fCoverage = 0xFF;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000465
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000466 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
467 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000468
469 SkXfermode* mode = skPaint.getXfermode();
470 if (mode) {
471 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000472 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000473#if 0
474 return false;
475#endif
476 }
477 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000478 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
479 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
480
bsalomon@google.com5782d712011-01-21 21:03:59 +0000481 if (justAlpha) {
482 uint8_t alpha = skPaint.getAlpha();
483 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000484 // justAlpha is currently set to true only if there is a texture,
485 // so constantColor should not also be true.
486 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000487 } else {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000488 grPaint->fColor = SkColor2GrColor(skPaint.getColor());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000489 grPaint->setTexture(kShaderTextureIdx, NULL);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000490 }
Scroggo97c88c22011-05-11 14:05:25 +0000491 SkColorFilter* colorFilter = skPaint.getColorFilter();
492 SkColor color;
493 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000494 SkScalar matrix[20];
twiz@google.com58071162012-07-18 21:41:50 +0000495 SkBitmap colorTransformTable;
Scroggo97c88c22011-05-11 14:05:25 +0000496 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000497 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000498 if (!constantColor) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000499 grPaint->fColorFilterColor = SkColor2GrColor(color);
Scroggod757df22011-05-16 13:11:16 +0000500 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000501 } else {
502 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
rileya@google.com24f3ad12012-07-18 21:47:40 +0000503 grPaint->fColor = SkColor2GrColor(filtered);
senorblanco@chromium.orgb3c20fa2012-01-03 21:20:19 +0000504 grPaint->resetColorFilter();
Scroggod757df22011-05-16 13:11:16 +0000505 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000506 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
507 grPaint->fColorMatrixEnabled = true;
508 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
509 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
twiz@google.com58071162012-07-18 21:41:50 +0000510 } else if (colorFilter != NULL && colorFilter->asComponentTable(
511 &colorTransformTable)) {
512 grPaint->resetColorFilter();
513
514 GrSamplerState* colorSampler = grPaint->textureSampler(kColorFilterTextureIdx);
515 GrTexture* texture = act->set(dev, colorTransformTable, colorSampler);
516
517 colorSampler->setCustomStage(SkNEW_ARGS(GrColorTableEffect, (texture)))->unref();
bsalomon@google.com25f3e9b2012-07-20 14:23:09 +0000518 // reset() sets the sampler's matrix to I. The custom stage doesn't require the matrix.
519 // The shader generator will omit the matrix if it is I. Once the matrix is moved from the
520 // sampler state to the custom stage this will no longer be necessary.
521 colorSampler->reset(GrSamplerState::kClamp_WrapMode, GrSamplerState::kNearest_Filter);
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000522 } else {
523 grPaint->resetColorFilter();
Scroggo97c88c22011-05-11 14:05:25 +0000524 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000525 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000526}
527
bsalomon@google.com84405e02012-03-05 19:57:21 +0000528// This function is similar to skPaint2GrPaintNoShader but also converts
529// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
530// be used is set on grPaint and returned in param act. constantColor has the
531// same meaning as in skPaint2GrPaintNoShader.
532inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
533 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000534 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000535 SkGpuDevice::SkAutoCachedTexture textures[GrPaint::kMaxTextures],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000536 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000537 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000538 if (NULL == shader) {
twiz@google.com58071162012-07-18 21:41:50 +0000539 return skPaint2GrPaintNoShader(dev,
540 skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000541 false,
542 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000543 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000544 grPaint);
twiz@google.com58071162012-07-18 21:41:50 +0000545 } else if (!skPaint2GrPaintNoShader(dev, skPaint, true, false,
546 &textures[kColorFilterTextureIdx], grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000547 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000548 }
549
reed@google.comac10a2d2010-12-22 21:39:39 +0000550 SkBitmap bitmap;
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000551 SkMatrix* matrix = grPaint->textureSampler(kShaderTextureIdx)->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000552 SkShader::TileMode tileModes[2];
553 SkScalar twoPointParams[3];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000554 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
reed@google.comac10a2d2010-12-22 21:39:39 +0000555 tileModes, twoPointParams);
556
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000557 if (SkShader::kNone_BitmapType == bmptype) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000558 SkShader::GradientInfo info;
559 SkColor color;
560
561 info.fColors = &color;
562 info.fColorOffsets = NULL;
563 info.fColorCount = 1;
564 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
565 SkPaint copy(skPaint);
566 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000567 // modulate the paint alpha by the shader's solid color alpha
568 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
569 copy.setColor(SkColorSetA(color, newA));
twiz@google.com58071162012-07-18 21:41:50 +0000570 return skPaint2GrPaintNoShader(dev,
571 copy,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000572 false,
573 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000574 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000575 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000576 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000577 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000578 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000579
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000580 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
twiz@google.com58071162012-07-18 21:41:50 +0000581 GrTexture* texture = textures[kShaderTextureIdx].set(dev, bitmap, sampler);
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000582 if (NULL == texture) {
583 SkDebugf("Couldn't convert bitmap to texture.\n");
584 return false;
585 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000586
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000587 switch (bmptype) {
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000588 case SkShader::kRadial_BitmapType:
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000589 sampler->setCustomStage(SkNEW_ARGS(GrRadialGradient, (texture)))->unref();
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000590 sampler->setFilter(GrSamplerState::kBilinear_Filter);
591 break;
592 case SkShader::kSweep_BitmapType:
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000593 sampler->setCustomStage(SkNEW_ARGS(GrSweepGradient, (texture)))->unref();
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000594 sampler->setFilter(GrSamplerState::kBilinear_Filter);
595 break;
596 case SkShader::kTwoPointRadial_BitmapType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000597 sampler->setCustomStage(SkNEW_ARGS(GrRadial2Gradient,
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000598 (texture,
599 twoPointParams[0],
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000600 twoPointParams[1],
601 twoPointParams[2] < 0)))->unref();
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000602 sampler->setFilter(GrSamplerState::kBilinear_Filter);
603 break;
rileya@google.com3e332582012-07-03 13:43:35 +0000604 case SkShader::kTwoPointConical_BitmapType:
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000605 sampler->setCustomStage(SkNEW_ARGS(GrConical2Gradient,
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000606 (texture,
607 twoPointParams[0],
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000608 twoPointParams[1],
609 twoPointParams[2])))->unref();
rileya@google.com3e332582012-07-03 13:43:35 +0000610 sampler->setFilter(GrSamplerState::kBilinear_Filter);
611 break;
rileya@google.com22e57f92012-07-19 15:16:19 +0000612 case SkShader::kLinear_BitmapType:
613 sampler->setCustomStage(SkNEW_ARGS(GrLinearGradient, (texture)))->unref();
rileya@google.com5ce42ca2012-07-19 19:42:10 +0000614 sampler->setFilter(GrSamplerState::kBilinear_Filter);
rileya@google.com22e57f92012-07-19 15:16:19 +0000615 break;
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000616 default:
617 if (skPaint.isFilterBitmap()) {
618 sampler->setFilter(GrSamplerState::kBilinear_Filter);
619 } else {
620 sampler->setFilter(GrSamplerState::kNearest_Filter);
621 }
tomhudson@google.come742bf02012-07-13 19:54:19 +0000622 // TODO - once we have a trivial GrCustomStage for texture drawing,
623 // create that here & get rid of the paint's texture
624 grPaint->setTexture(kShaderTextureIdx, texture);
bsalomon@google.com3a5dab42012-06-04 20:21:28 +0000625 break;
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +0000626 }
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000627 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
628 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
reed@google.comac10a2d2010-12-22 21:39:39 +0000629
reed@google.comac10a2d2010-12-22 21:39:39 +0000630 // since our texture coords will be in local space, we wack the texture
631 // matrix to map them back into 0...1 before we load it
632 SkMatrix localM;
633 if (shader->getLocalMatrix(&localM)) {
634 SkMatrix inverse;
635 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000636 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000637 }
638 }
639 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000640 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
641 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000642 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000643 } else if (SkShader::kRadial_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000644 GrScalar s = SkFloatToScalar(1.f / bitmap.width());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000645 matrix->postScale(s, s);
reed@google.comac10a2d2010-12-22 21:39:39 +0000646 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000647
648 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000649}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000650}
reed@google.comac10a2d2010-12-22 21:39:39 +0000651
652///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000653void SkGpuDevice::clear(SkColor color) {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000654 fContext->clear(NULL, color, fRenderTarget);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000655}
656
reed@google.comac10a2d2010-12-22 21:39:39 +0000657void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
658 CHECK_SHOULD_DRAW(draw);
659
bsalomon@google.com5782d712011-01-21 21:03:59 +0000660 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000661 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000662 if (!skPaint2GrPaintShader(this,
663 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000664 true,
twiz@google.com58071162012-07-18 21:41:50 +0000665 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000666 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000667 return;
668 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000669
670 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000671}
672
673// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000674static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000675 kPoints_GrPrimitiveType,
676 kLines_GrPrimitiveType,
677 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000678};
679
680void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000681 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000682 CHECK_SHOULD_DRAW(draw);
683
684 SkScalar width = paint.getStrokeWidth();
685 if (width < 0) {
686 return;
687 }
688
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000689 // we only handle hairlines and paints without path effects or mask filters,
690 // else we let the SkDraw call our drawPath()
691 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000692 draw.drawPoints(mode, count, pts, paint, true);
693 return;
694 }
695
bsalomon@google.com5782d712011-01-21 21:03:59 +0000696 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000697 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000698 if (!skPaint2GrPaintShader(this,
699 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000700 true,
twiz@google.com58071162012-07-18 21:41:50 +0000701 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000702 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000703 return;
704 }
705
bsalomon@google.com5782d712011-01-21 21:03:59 +0000706 fContext->drawVertices(grPaint,
707 gPointMode2PrimtiveType[mode],
708 count,
709 (GrPoint*)pts,
710 NULL,
711 NULL,
712 NULL,
713 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000714}
715
reed@google.comc9aa5872011-04-05 21:05:37 +0000716///////////////////////////////////////////////////////////////////////////////
717
reed@google.comac10a2d2010-12-22 21:39:39 +0000718void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
719 const SkPaint& paint) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000720 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000721 CHECK_SHOULD_DRAW(draw);
722
bungeman@google.com79bd8772011-07-18 15:34:08 +0000723 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000724 SkScalar width = paint.getStrokeWidth();
725
726 /*
727 We have special code for hairline strokes, miter-strokes, and fills.
728 Anything else we just call our path code.
729 */
730 bool usePath = doStroke && width > 0 &&
731 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000732 // another two reasons we might need to call drawPath...
733 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000734 usePath = true;
735 }
reed@google.com67db6642011-05-26 11:46:35 +0000736 // until we aa rotated rects...
737 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
738 usePath = true;
739 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000740 // small miter limit means right angles show bevel...
741 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
742 paint.getStrokeMiter() < SK_ScalarSqrt2)
743 {
744 usePath = true;
745 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000746 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000747 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
748 usePath = true;
749 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000750
751 if (usePath) {
752 SkPath path;
753 path.addRect(rect);
754 this->drawPath(draw, path, paint, NULL, true);
755 return;
756 }
757
758 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000759 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000760 if (!skPaint2GrPaintShader(this,
761 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000762 true,
twiz@google.com58071162012-07-18 21:41:50 +0000763 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000764 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000765 return;
766 }
reed@google.com20efde72011-05-09 17:00:02 +0000767 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000768}
769
reed@google.com69302852011-02-16 18:08:07 +0000770#include "SkMaskFilter.h"
771#include "SkBounder.h"
772
bsalomon@google.com85003222012-03-28 14:44:37 +0000773///////////////////////////////////////////////////////////////////////////////
774
775// helpers for applying mask filters
776namespace {
777
778GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000779 switch (fillType) {
780 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000781 return kWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000782 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000783 return kEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000784 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000785 return kInverseWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000786 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000787 return kInverseEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000788 default:
789 SkDebugf("Unsupported path fill type\n");
bsalomon@google.com47059542012-06-06 20:51:20 +0000790 return kHairLine_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000791 }
792}
793
bsalomon@google.com85003222012-03-28 14:44:37 +0000794// We prefer to blur small rect with small radius via CPU.
795#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
796#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
797inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
798 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
799 rect.height() <= MIN_GPU_BLUR_SIZE &&
800 radius <= MIN_GPU_BLUR_RADIUS) {
801 return true;
802 }
803 return false;
804}
805
806bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
807 SkMaskFilter* filter, const SkMatrix& matrix,
808 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000809 GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000810#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000811 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000812#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000813 SkMaskFilter::BlurInfo info;
814 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000815 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000816 return false;
817 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000818 SkScalar radius = info.fIgnoreTransform ? info.fRadius
819 : matrix.mapRadius(info.fRadius);
820 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000821 if (radius <= 0) {
822 return false;
823 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000824
825 SkRect srcRect = path.getBounds();
826 if (shouldDrawBlurWithCPU(srcRect, radius)) {
827 return false;
828 }
829
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000830 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000831 float sigma3 = sigma * 3.0f;
832
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000833 SkRect clipRect;
834 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000835
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000836 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000837 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
838 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000839 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000840 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000841 SkIRect finalIRect;
842 finalRect.roundOut(&finalIRect);
843 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000844 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000845 }
846 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000847 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000848 }
849 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000850 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000851 GrTextureDesc desc;
852 desc.fFlags = kRenderTarget_GrTextureFlagBit;
853 desc.fWidth = SkScalarCeilToInt(srcRect.width());
854 desc.fHeight = SkScalarCeilToInt(srcRect.height());
855 // We actually only need A8, but it often isn't supported as a
856 // render target so default to RGBA_8888
857 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000858
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000859 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
860 desc.fConfig = kAlpha_8_GrPixelConfig;
861 }
862
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000863 GrAutoScratchTexture pathEntry(context, desc);
864 GrTexture* pathTexture = pathEntry.texture();
865 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000866 return false;
867 }
868 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000869 // Once this code moves into GrContext, this should be changed to use
870 // an AutoClipRestore.
871 GrClip oldClip = context->getClip();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000872 context->setRenderTarget(pathTexture->asRenderTarget());
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000873
874 GrClip newClip(srcRect);
875 context->setClip(newClip);
876
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000877 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000878 GrPaint tempPaint;
879 tempPaint.reset();
880
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000881 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000882 tempPaint.fAntiAlias = grp->fAntiAlias;
883 if (tempPaint.fAntiAlias) {
884 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
885 // blend coeff of zero requires dual source blending support in order
886 // to properly blend partially covered pixels. This means the AA
887 // code path may not be taken. So we use a dst blend coeff of ISA. We
888 // could special case AA draws to a dst surface with known alpha=0 to
889 // use a zero dst coeff when dual source blending isn't available.
bsalomon@google.com47059542012-06-06 20:51:20 +0000890 tempPaint.fSrcBlendCoeff = kOne_GrBlendCoeff;
891 tempPaint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000892 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000893 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000894 context->drawPath(tempPaint, path, pathFillType, &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000895
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000896 // If we're doing a normal blur, we can clobber the pathTexture in the
897 // gaussianBlur. Otherwise, we need to save it for later compositing.
898 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +0000899 SkAutoTUnref<GrTexture> blurTexture(context->gaussianBlur(
900 pathTexture, isNormalBlur, srcRect, sigma, sigma));
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000901
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000902 if (!isNormalBlur) {
903 GrPaint paint;
904 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000905 paint.textureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000906 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
907 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000908 // Blend pathTexture over blurTexture.
909 context->setRenderTarget(blurTexture->asRenderTarget());
910 paint.setTexture(0, pathTexture);
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000911 if (SkMaskFilter::kInner_BlurType == blurType) {
912 // inner: dst = dst * src
bsalomon@google.com47059542012-06-06 20:51:20 +0000913 paint.fSrcBlendCoeff = kDC_GrBlendCoeff;
914 paint.fDstBlendCoeff = kZero_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000915 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
916 // solid: dst = src + dst - src * dst
917 // = (1 - dst) * src + 1 * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000918 paint.fSrcBlendCoeff = kIDC_GrBlendCoeff;
919 paint.fDstBlendCoeff = kOne_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000920 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
921 // outer: dst = dst * (1 - src)
922 // = 0 * src + (1 - src) * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000923 paint.fSrcBlendCoeff = kZero_GrBlendCoeff;
924 paint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000925 }
926 context->drawRect(paint, srcRect);
927 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000928 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000929 context->setClip(oldClip);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000930
bsalomon@google.come3d32162012-07-20 13:37:06 +0000931 if (!grp->preConcatSamplerMatricesWithInverse(matrix)) {
932 return false;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000933 }
934
935 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
936 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000937 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000938 grp->setMask(MASK_IDX, blurTexture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000939 grp->maskSampler(MASK_IDX)->reset();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000940
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000941 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
942 -finalRect.fTop);
943 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
944 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000945 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000946 return true;
947}
948
bsalomon@google.com85003222012-03-28 14:44:37 +0000949bool drawWithMaskFilter(GrContext* context, const SkPath& path,
950 SkMaskFilter* filter, const SkMatrix& matrix,
951 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000952 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000953 SkMask srcM, dstM;
954
955 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000956 SkMask::kComputeBoundsAndRenderImage_CreateMode,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000957 style)) {
reed@google.com69302852011-02-16 18:08:07 +0000958 return false;
959 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000960 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000961
962 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
963 return false;
964 }
965 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000966 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000967
968 if (clip.quickReject(dstM.fBounds)) {
969 return false;
970 }
971 if (bounder && !bounder->doIRect(dstM.fBounds)) {
972 return false;
973 }
974
975 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
976 // the current clip (and identity matrix) and grpaint settings
977
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000978 GrContext::AutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +0000979
bsalomon@google.come3d32162012-07-20 13:37:06 +0000980 if (!grp->preConcatSamplerMatricesWithInverse(matrix)) {
981 return false;
982 }
983
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000984 GrTextureDesc desc;
985 desc.fWidth = dstM.fBounds.width();
986 desc.fHeight = dstM.fBounds.height();
987 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +0000988
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000989 GrAutoScratchTexture ast(context, desc);
990 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000991
reed@google.com69302852011-02-16 18:08:07 +0000992 if (NULL == texture) {
993 return false;
994 }
bsalomon@google.com6f379512011-11-16 20:36:03 +0000995 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +0000996 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +0000997
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000998 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
999 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001000 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001001 grp->setMask(MASK_IDX, texture);
bsalomon@google.com97912912011-12-06 16:30:36 +00001002 grp->maskSampler(MASK_IDX)->reset();
reed@google.com69302852011-02-16 18:08:07 +00001003
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001004 GrRect d;
1005 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +00001006 GrIntToScalar(dstM.fBounds.fTop),
1007 GrIntToScalar(dstM.fBounds.fRight),
1008 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001009
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001010 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
1011 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
1012 -dstM.fBounds.fTop*SK_Scalar1);
1013 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001014 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +00001015 return true;
1016}
reed@google.com69302852011-02-16 18:08:07 +00001017
bsalomon@google.com85003222012-03-28 14:44:37 +00001018}
1019
1020///////////////////////////////////////////////////////////////////////////////
1021
reed@google.com0c219b62011-02-16 21:31:18 +00001022void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001023 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +00001024 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +00001025 CHECK_FOR_NODRAW_ANNOTATION(paint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001026 CHECK_SHOULD_DRAW(draw);
1027
reed@google.comfe626382011-09-21 13:50:35 +00001028 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001029
bsalomon@google.com5782d712011-01-21 21:03:59 +00001030 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001031 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001032 if (!skPaint2GrPaintShader(this,
1033 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001034 true,
twiz@google.com58071162012-07-18 21:41:50 +00001035 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001036 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001037 return;
1038 }
1039
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +00001040 // can we cheat, and threat a thin stroke as a hairline w/ coverage
1041 // if we can, we draw lots faster (raster device does this same test)
1042 SkScalar hairlineCoverage;
1043 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
1044 doFill = false;
1045 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
1046 grPaint.fCoverage);
1047 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001048
reed@google.comfe626382011-09-21 13:50:35 +00001049 // If we have a prematrix, apply it to the path, optimizing for the case
1050 // where the original path can in fact be modified in place (even though
1051 // its parameter type is const).
1052 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1053 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001054
1055 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001056 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001057
reed@google.come3445642011-02-16 23:20:39 +00001058 if (!pathIsMutable) {
1059 result = &tmpPath;
1060 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001061 }
reed@google.come3445642011-02-16 23:20:39 +00001062 // should I push prePathMatrix on our MV stack temporarily, instead
1063 // of applying it here? See SkDraw.cpp
1064 pathPtr->transform(*prePathMatrix, result);
1065 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001066 }
reed@google.com0c219b62011-02-16 21:31:18 +00001067 // at this point we're done with prePathMatrix
1068 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001069
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001070 if (paint.getPathEffect() ||
1071 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001072 // it is safe to use tmpPath here, even if we already used it for the
1073 // prepathmatrix, since getFillPath can take the same object for its
1074 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001075 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001076 pathPtr = &tmpPath;
1077 }
1078
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001079 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001080 // avoid possibly allocating a new path in transform if we can
1081 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1082
1083 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001084 pathPtr->transform(*draw.fMatrix, devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001085 GrPathFill pathFillType = doFill ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001086 skToGrFillType(devPathPtr->getFillType()) : kHairLine_GrPathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001087 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001088 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001089 &grPaint, pathFillType)) {
1090 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
1091 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001092 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001093 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001094 &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001095 }
reed@google.com69302852011-02-16 18:08:07 +00001096 return;
1097 }
reed@google.com69302852011-02-16 18:08:07 +00001098
bsalomon@google.com47059542012-06-06 20:51:20 +00001099 GrPathFill fill = kHairLine_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001100
reed@google.com0c219b62011-02-16 21:31:18 +00001101 if (doFill) {
1102 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001103 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001104 fill = kWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001105 break;
1106 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001107 fill = kEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001108 break;
1109 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001110 fill = kInverseWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001111 break;
1112 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001113 fill = kInverseEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001114 break;
1115 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001116 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001117 return;
1118 }
1119 }
1120
reed@google.com07f3ee12011-05-16 17:21:57 +00001121 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001122}
1123
bsalomon@google.comfb309512011-11-30 14:13:48 +00001124namespace {
1125
1126inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1127 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1128 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1129 return tilesX * tilesY;
1130}
1131
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001132inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001133 const SkIRect* srcRectPtr,
1134 int maxTextureSize) {
1135 static const int kSmallTileSize = 1 << 10;
1136 if (maxTextureSize <= kSmallTileSize) {
1137 return maxTextureSize;
1138 }
1139
1140 size_t maxTexTotalTileSize;
1141 size_t smallTotalTileSize;
1142
1143 if (NULL == srcRectPtr) {
1144 int w = bitmap.width();
1145 int h = bitmap.height();
1146 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1147 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1148 } else {
1149 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1150 srcRectPtr->fTop,
1151 srcRectPtr->fRight,
1152 srcRectPtr->fBottom,
1153 maxTextureSize);
1154 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1155 srcRectPtr->fTop,
1156 srcRectPtr->fRight,
1157 srcRectPtr->fBottom,
1158 kSmallTileSize);
1159 }
1160 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1161 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1162
1163 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1164 return kSmallTileSize;
1165 } else {
1166 return maxTextureSize;
1167 }
1168}
1169}
1170
1171bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1172 const GrSamplerState& sampler,
1173 const SkIRect* srcRectPtr,
1174 int* tileSize) const {
1175 SkASSERT(NULL != tileSize);
1176
1177 // if bitmap is explictly texture backed then just use the texture
1178 if (NULL != bitmap.getTexture()) {
1179 return false;
1180 }
1181 // if it's larger than the max texture size, then we have no choice but
1182 // tiling
1183 const int maxTextureSize = fContext->getMaxTextureSize();
1184 if (bitmap.width() > maxTextureSize ||
1185 bitmap.height() > maxTextureSize) {
1186 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1187 return true;
1188 }
1189 // if we are going to have to draw the whole thing, then don't tile
1190 if (NULL == srcRectPtr) {
1191 return false;
1192 }
1193 // if the entire texture is already in our cache then no reason to tile it
1194 if (this->isBitmapInTextureCache(bitmap, sampler)) {
1195 return false;
1196 }
1197
1198 // At this point we know we could do the draw by uploading the entire bitmap
1199 // as a texture. However, if the texture would be large compared to the
1200 // cache size and we don't require most of it for this draw then tile to
1201 // reduce the amount of upload and cache spill.
1202
1203 // assumption here is that sw bitmap size is a good proxy for its size as
1204 // a texture
1205 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001206 size_t cacheSize;
1207 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001208 if (bmpSize < cacheSize / 2) {
1209 return false;
1210 }
1211
1212 SkFixed fracUsed =
1213 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1214 (srcRectPtr->height() << 16) / bitmap.height());
1215 if (fracUsed <= SK_FixedHalf) {
1216 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1217 return true;
1218 } else {
1219 return false;
1220 }
1221}
1222
reed@google.comac10a2d2010-12-22 21:39:39 +00001223void SkGpuDevice::drawBitmap(const SkDraw& draw,
1224 const SkBitmap& bitmap,
1225 const SkIRect* srcRectPtr,
1226 const SkMatrix& m,
1227 const SkPaint& paint) {
1228 CHECK_SHOULD_DRAW(draw);
1229
1230 SkIRect srcRect;
1231 if (NULL == srcRectPtr) {
1232 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1233 } else {
1234 srcRect = *srcRectPtr;
1235 }
1236
junov@google.comd935cfb2011-06-27 20:48:23 +00001237 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001238 // Convert the bitmap to a shader so that the rect can be drawn
1239 // through drawRect, which supports mask filters.
1240 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001241 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001242 if (srcRectPtr) {
1243 if (!bitmap.extractSubset(&tmp, srcRect)) {
1244 return; // extraction failed
1245 }
1246 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001247 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001248 }
1249 SkPaint paintWithTexture(paint);
1250 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1251 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001252 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001253 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001254
junov@google.com1d329782011-07-28 20:10:09 +00001255 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001256 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001257 // also affect the behavior of the mask filter.
1258 SkMatrix drawMatrix;
1259 drawMatrix.setConcat(*draw.fMatrix, m);
1260 SkDraw transformedDraw(draw);
1261 transformedDraw.fMatrix = &drawMatrix;
1262
1263 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1264
junov@google.comd935cfb2011-06-27 20:48:23 +00001265 return;
1266 }
1267
bsalomon@google.com5782d712011-01-21 21:03:59 +00001268 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001269 SkAutoCachedTexture colorLutTexture;
1270 if (!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001271 return;
1272 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001273 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001274 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001275 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001276 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001277 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001278 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001279
bsalomon@google.comfb309512011-11-30 14:13:48 +00001280 int tileSize;
1281 if (!this->shouldTileBitmap(bitmap, *sampler, srcRectPtr, &tileSize)) {
1282 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001283 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001284 return;
1285 }
1286
1287 // undo the translate done by SkCanvas
1288 int DX = SkMax32(0, srcRect.fLeft);
1289 int DY = SkMax32(0, srcRect.fTop);
1290 // compute clip bounds in local coordinates
1291 SkIRect clipRect;
1292 {
1293 SkRect r;
1294 r.set(draw.fClip->getBounds());
1295 SkMatrix matrix, inverse;
1296 matrix.setConcat(*draw.fMatrix, m);
1297 if (!matrix.invert(&inverse)) {
1298 return;
1299 }
1300 inverse.mapRect(&r);
1301 r.roundOut(&clipRect);
1302 // apply the canvas' translate to our local clip
1303 clipRect.offset(DX, DY);
1304 }
1305
bsalomon@google.comfb309512011-11-30 14:13:48 +00001306 int nx = bitmap.width() / tileSize;
1307 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001308 for (int x = 0; x <= nx; x++) {
1309 for (int y = 0; y <= ny; y++) {
1310 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001311 tileR.set(x * tileSize, y * tileSize,
1312 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001313 if (!SkIRect::Intersects(tileR, clipRect)) {
1314 continue;
1315 }
1316
1317 SkIRect srcR = tileR;
1318 if (!srcR.intersect(srcRect)) {
1319 continue;
1320 }
1321
1322 SkBitmap tmpB;
1323 if (bitmap.extractSubset(&tmpB, tileR)) {
1324 // now offset it to make it "local" to our tmp bitmap
1325 srcR.offset(-tileR.fLeft, -tileR.fTop);
1326
1327 SkMatrix tmpM(m);
1328 {
1329 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1330 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1331 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1332 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001333 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001334 }
1335 }
1336 }
1337}
1338
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001339namespace {
1340
1341bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1342 // detect pixel disalignment
1343 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1344 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1345 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1346 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1347 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1348 COLOR_BLEED_TOLERANCE &&
1349 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1350 COLOR_BLEED_TOLERANCE) {
1351 return true;
1352 }
1353 return false;
1354}
1355
1356bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1357 const SkMatrix& m) {
1358 // Only gets called if hasAlignedSamples returned false.
1359 // So we can assume that sampling is axis aligned but not texel aligned.
1360 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
1361 SkRect innerSrcRect(srcRect), innerTransformedRect,
1362 outerTransformedRect(transformedRect);
1363 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1364 m.mapRect(&innerTransformedRect, innerSrcRect);
1365
1366 // The gap between outerTransformedRect and innerTransformedRect
1367 // represents the projection of the source border area, which is
1368 // problematic for color bleeding. We must check whether any
1369 // destination pixels sample the border area.
1370 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1371 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1372 SkIRect outer, inner;
1373 outerTransformedRect.round(&outer);
1374 innerTransformedRect.round(&inner);
1375 // If the inner and outer rects round to the same result, it means the
1376 // border does not overlap any pixel centers. Yay!
1377 return inner != outer;
1378}
1379
1380} // unnamed namespace
1381
reed@google.comac10a2d2010-12-22 21:39:39 +00001382/*
1383 * This is called by drawBitmap(), which has to handle images that may be too
1384 * large to be represented by a single texture.
1385 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001386 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1387 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001388 */
1389void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1390 const SkBitmap& bitmap,
1391 const SkIRect& srcRect,
1392 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001393 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001394 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1395 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001396
reed@google.com9c49bc32011-07-07 13:42:37 +00001397 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001398 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001399 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001400 return;
1401 }
1402
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001403 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001404
1405 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1406 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001407 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001408
1409 GrTexture* texture;
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001410 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001411 if (NULL == texture) {
1412 return;
1413 }
1414
bsalomon@google.com452943d2011-10-31 17:37:14 +00001415 grPaint->setTexture(kBitmapTextureIdx, texture);
reed@google.com46799cd2011-02-22 20:56:26 +00001416
reed@google.com20efde72011-05-09 17:00:02 +00001417 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1418 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001419 GrRect paintRect;
bsalomon@google.com91832162012-03-08 19:53:02 +00001420 float wInv = 1.f / bitmap.width();
1421 float hInv = 1.f / bitmap.height();
1422 paintRect.setLTRB(SkFloatToScalar(srcRect.fLeft * wInv),
1423 SkFloatToScalar(srcRect.fTop * hInv),
1424 SkFloatToScalar(srcRect.fRight * wInv),
1425 SkFloatToScalar(srcRect.fBottom * hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001426
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001427 bool needsTextureDomain = false;
1428 if (GrSamplerState::kBilinear_Filter == sampler->getFilter())
1429 {
1430 // Need texture domain if drawing a sub rect.
1431 needsTextureDomain = srcRect.width() < bitmap.width() ||
1432 srcRect.height() < bitmap.height();
1433 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1434 // sampling is axis-aligned
1435 GrRect floatSrcRect, transformedRect;
1436 floatSrcRect.set(srcRect);
1437 SkMatrix srcToDeviceMatrix(m);
1438 srcToDeviceMatrix.postConcat(*draw.fMatrix);
1439 srcToDeviceMatrix.mapRect(&transformedRect, floatSrcRect);
1440
1441 if (hasAlignedSamples(floatSrcRect, transformedRect)) {
1442 // Samples are texel-aligned, so filtering is futile
1443 sampler->setFilter(GrSamplerState::kNearest_Filter);
1444 needsTextureDomain = false;
1445 } else {
1446 needsTextureDomain = needsTextureDomain &&
1447 mayColorBleed(floatSrcRect, transformedRect, m);
1448 }
1449 }
1450 }
1451
1452 GrRect textureDomain = GrRect::MakeEmpty();
1453
1454 if (needsTextureDomain) {
1455 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001456 GrScalar left, top, right, bottom;
1457 if (srcRect.width() > 1) {
1458 GrScalar border = GR_ScalarHalf / bitmap.width();
1459 left = paintRect.left() + border;
1460 right = paintRect.right() - border;
1461 } else {
1462 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1463 }
1464 if (srcRect.height() > 1) {
1465 GrScalar border = GR_ScalarHalf / bitmap.height();
1466 top = paintRect.top() + border;
1467 bottom = paintRect.bottom() - border;
1468 } else {
1469 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1470 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001471 textureDomain.setLTRB(left, top, right, bottom);
tomhudson@google.com2f68e762012-07-17 18:43:21 +00001472 sampler->setCustomStage(SkNEW_ARGS(GrTextureDomainEffect,
1473 (texture,
1474 textureDomain)))->unref();
junov@google.com6acc9b32011-05-16 18:32:07 +00001475 }
1476
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001477 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001478}
1479
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001480namespace {
1481
1482void apply_custom_stage(GrContext* context,
1483 GrTexture* srcTexture,
1484 GrTexture* dstTexture,
1485 const GrRect& rect,
1486 GrCustomStage* stage) {
1487 SkASSERT(srcTexture && srcTexture->getContext() == context);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001488 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001489 GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget());
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001490 GrContext::AutoClip acs(context, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001491
1492 GrMatrix sampleM;
1493 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
1494 GrPaint paint;
1495 paint.reset();
1496 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
1497 paint.textureSampler(0)->reset(sampleM);
1498 paint.textureSampler(0)->setCustomStage(stage);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001499 context->drawRect(paint, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001500}
1501
1502};
1503
reed@google.com8926b162012-03-23 15:36:36 +00001504static GrTexture* filter_texture(GrContext* context, GrTexture* texture,
1505 SkImageFilter* filter, const GrRect& rect) {
1506 GrAssert(filter);
1507
1508 SkSize blurSize;
1509 SkISize radius;
1510
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001511 GrTextureDesc desc;
1512 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1513 desc.fWidth = SkScalarCeilToInt(rect.width());
1514 desc.fHeight = SkScalarCeilToInt(rect.height());
1515 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001516 GrCustomStage* stage;
reed@google.com8926b162012-03-23 15:36:36 +00001517
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001518 if (filter->asNewCustomStage(&stage, texture)) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001519 GrAutoScratchTexture dst(context, desc);
1520 apply_custom_stage(context, texture, dst.texture(), rect, stage);
1521 texture = dst.detach();
1522 stage->unref();
1523 } else if (filter->asABlur(&blurSize)) {
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001524 texture = context->gaussianBlur(texture, false, rect,
reed@google.com8926b162012-03-23 15:36:36 +00001525 blurSize.width(),
1526 blurSize.height());
reed@google.com8926b162012-03-23 15:36:36 +00001527 } else if (filter->asADilate(&radius)) {
reed@google.com8926b162012-03-23 15:36:36 +00001528 texture = context->applyMorphology(texture, rect,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001529 GrContext::kDilate_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001530 radius);
reed@google.com8926b162012-03-23 15:36:36 +00001531 } else if (filter->asAnErode(&radius)) {
reed@google.com8926b162012-03-23 15:36:36 +00001532 texture = context->applyMorphology(texture, rect,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001533 GrContext::kErode_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001534 radius);
reed@google.com8926b162012-03-23 15:36:36 +00001535 }
1536 return texture;
1537}
1538
reed@google.comac10a2d2010-12-22 21:39:39 +00001539void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1540 int left, int top, const SkPaint& paint) {
1541 CHECK_SHOULD_DRAW(draw);
1542
reed@google.com8926b162012-03-23 15:36:36 +00001543 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001544 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1545 return;
1546 }
1547
reed@google.com76dd2772012-01-05 21:15:07 +00001548 int w = bitmap.width();
1549 int h = bitmap.height();
1550
bsalomon@google.com5782d712011-01-21 21:03:59 +00001551 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001552 SkAutoCachedTexture colorLutTexture;
1553 if(!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001554 return;
1555 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001556
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001557 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001558
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001559 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001560
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001561 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001562 sampler->reset();
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001563 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.com8926b162012-03-23 15:36:36 +00001564 grPaint.setTexture(kBitmapTextureIdx, texture);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001565
reed@google.com8926b162012-03-23 15:36:36 +00001566 SkImageFilter* filter = paint.getImageFilter();
1567 if (NULL != filter) {
1568 GrTexture* filteredTexture = filter_texture(fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001569 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001570 if (filteredTexture) {
1571 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1572 texture = filteredTexture;
1573 filteredTexture->unref();
1574 }
reed@google.com76dd2772012-01-05 21:15:07 +00001575 }
reed@google.com8926b162012-03-23 15:36:36 +00001576
bsalomon@google.com5782d712011-01-21 21:03:59 +00001577 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001578 GrRect::MakeXYWH(GrIntToScalar(left),
1579 GrIntToScalar(top),
1580 GrIntToScalar(w),
1581 GrIntToScalar(h)),
1582 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1583 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001584}
1585
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001586void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
reed@google.comac10a2d2010-12-22 21:39:39 +00001587 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001588 // clear of the source device must occur before CHECK_SHOULD_DRAW
1589 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1590 if (dev->fNeedClear) {
1591 // TODO: could check here whether we really need to draw at all
1592 dev->clear(0x0);
1593 }
1594
reed@google.comac10a2d2010-12-22 21:39:39 +00001595 CHECK_SHOULD_DRAW(draw);
1596
bsalomon@google.com5782d712011-01-21 21:03:59 +00001597 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001598 SkAutoCachedTexture colorLutTexture;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001599 if (!dev->bindDeviceAsTexture(&grPaint) ||
twiz@google.com58071162012-07-18 21:41:50 +00001600 !skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001601 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001602 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001603
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001604 GrTexture* devTex = grPaint.getTexture(0);
1605 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001606
reed@google.com8926b162012-03-23 15:36:36 +00001607 SkImageFilter* filter = paint.getImageFilter();
1608 if (NULL != filter) {
robertphillips@google.com8637a362012-04-10 18:32:35 +00001609 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
1610 SkIntToScalar(devTex->height()));
reed@google.com8926b162012-03-23 15:36:36 +00001611 GrTexture* filteredTexture = filter_texture(fContext, devTex, filter,
1612 rect);
1613 if (filteredTexture) {
1614 grPaint.setTexture(kBitmapTextureIdx, filteredTexture);
1615 devTex = filteredTexture;
1616 filteredTexture->unref();
1617 }
1618 }
1619
bsalomon@google.com5782d712011-01-21 21:03:59 +00001620 const SkBitmap& bm = dev->accessBitmap(false);
1621 int w = bm.width();
1622 int h = bm.height();
1623
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001624 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001625
bsalomon@google.com97912912011-12-06 16:30:36 +00001626 grPaint.textureSampler(kBitmapTextureIdx)->reset();
bsalomon@google.com5782d712011-01-21 21:03:59 +00001627
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001628 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1629 GrIntToScalar(y),
1630 GrIntToScalar(w),
1631 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001632
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001633 // The device being drawn may not fill up its texture (saveLayer uses
1634 // the approximate ).
1635 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1636 GR_Scalar1 * h / devTex->height());
1637
1638 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001639}
1640
reed@google.com8926b162012-03-23 15:36:36 +00001641bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
reed@google.com76dd2772012-01-05 21:15:07 +00001642 SkSize size;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001643 SkISize radius;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001644
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001645 if (!filter->asNewCustomStage(NULL, NULL) &&
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001646 !filter->asABlur(&size) &&
1647 !filter->asADilate(&radius) &&
1648 !filter->asAnErode(&radius)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001649 return false;
1650 }
reed@google.com8926b162012-03-23 15:36:36 +00001651 return true;
1652}
1653
1654bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1655 const SkMatrix& ctm,
1656 SkBitmap* result, SkIPoint* offset) {
1657 // want explicitly our impl, so guard against a subclass of us overriding it
1658 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001659 return false;
1660 }
reed@google.com8926b162012-03-23 15:36:36 +00001661
1662 SkAutoLockPixels alp(src, !src.getTexture());
1663 if (!src.getTexture() && !src.readyToDraw()) {
1664 return false;
1665 }
1666
1667 GrPaint paint;
1668 paint.reset();
1669
1670 GrSamplerState* sampler = paint.textureSampler(kBitmapTextureIdx);
1671
1672 GrTexture* texture;
1673 SkAutoCachedTexture act(this, src, sampler, &texture);
1674
1675 result->setConfig(src.config(), src.width(), src.height());
robertphillips@google.com8637a362012-04-10 18:32:35 +00001676 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
1677 SkIntToScalar(src.height()));
reed@google.com8926b162012-03-23 15:36:36 +00001678 GrTexture* resultTexture = filter_texture(fContext, texture, filter, rect);
1679 if (resultTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001680 result->setPixelRef(SkNEW_ARGS(SkGrTexturePixelRef,
1681 (resultTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001682 resultTexture->unref();
1683 }
reed@google.com76dd2772012-01-05 21:15:07 +00001684 return true;
1685}
1686
reed@google.comac10a2d2010-12-22 21:39:39 +00001687///////////////////////////////////////////////////////////////////////////////
1688
1689// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001690static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001691 kTriangles_GrPrimitiveType,
1692 kTriangleStrip_GrPrimitiveType,
1693 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001694};
1695
1696void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1697 int vertexCount, const SkPoint vertices[],
1698 const SkPoint texs[], const SkColor colors[],
1699 SkXfermode* xmode,
1700 const uint16_t indices[], int indexCount,
1701 const SkPaint& paint) {
1702 CHECK_SHOULD_DRAW(draw);
1703
bsalomon@google.com5782d712011-01-21 21:03:59 +00001704 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001705 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com5782d712011-01-21 21:03:59 +00001706 // we ignore the shader if texs is null.
1707 if (NULL == texs) {
twiz@google.com58071162012-07-18 21:41:50 +00001708 if (!skPaint2GrPaintNoShader(this,
1709 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001710 false,
1711 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001712 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +00001713 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001714 return;
1715 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001716 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001717 if (!skPaint2GrPaintShader(this,
1718 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001719 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001720 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001721 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001722 return;
1723 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001724 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001725
1726 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001727 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001728 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1729#if 0
1730 return
1731#endif
1732 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001733 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001734
bsalomon@google.com498776a2011-08-16 19:20:44 +00001735 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1736 if (NULL != colors) {
1737 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001738 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001739 for (int i = 0; i < vertexCount; ++i) {
rileya@google.com24f3ad12012-07-18 21:47:40 +00001740 convertedColors[i] = SkColor2GrColor(colors[i]);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001741 }
1742 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001743 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001744 fContext->drawVertices(grPaint,
1745 gVertexMode2PrimitiveType[vmode],
1746 vertexCount,
1747 (GrPoint*) vertices,
1748 (GrPoint*) texs,
1749 colors,
1750 indices,
1751 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001752}
1753
1754///////////////////////////////////////////////////////////////////////////////
1755
1756static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001757 GrFontScaler* scaler = (GrFontScaler*)data;
1758 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001759}
1760
1761static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1762 void* auxData;
1763 GrFontScaler* scaler = NULL;
1764 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1765 scaler = (GrFontScaler*)auxData;
1766 }
1767 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001768 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001769 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1770 }
1771 return scaler;
1772}
1773
1774static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1775 SkFixed fx, SkFixed fy,
1776 const SkGlyph& glyph) {
1777 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1778
bungeman@google.com15865a72012-01-11 16:28:04 +00001779 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001780
1781 if (NULL == procs->fFontScaler) {
1782 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1783 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001784
bungeman@google.com15865a72012-01-11 16:28:04 +00001785 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1786 glyph.getSubXFixed(),
1787 glyph.getSubYFixed()),
1788 SkFixedFloorToFixed(fx),
1789 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001790 procs->fFontScaler);
1791}
1792
bsalomon@google.com5782d712011-01-21 21:03:59 +00001793SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001794
1795 // deferred allocation
1796 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001797 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001798 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1799 fDrawProcs->fContext = fContext;
1800 }
1801
1802 // init our (and GL's) state
1803 fDrawProcs->fTextContext = context;
1804 fDrawProcs->fFontScaler = NULL;
1805 return fDrawProcs;
1806}
1807
1808void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1809 size_t byteLength, SkScalar x, SkScalar y,
1810 const SkPaint& paint) {
1811 CHECK_SHOULD_DRAW(draw);
1812
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001813 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001814 // this guy will just call our drawPath()
1815 draw.drawText((const char*)text, byteLength, x, y, paint);
1816 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001817 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001818
1819 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001820 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001821 if (!skPaint2GrPaintShader(this,
1822 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001823 true,
twiz@google.com58071162012-07-18 21:41:50 +00001824 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001825 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001826 return;
1827 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001828 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1829 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001830 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1831 }
1832}
1833
1834void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1835 size_t byteLength, const SkScalar pos[],
1836 SkScalar constY, int scalarsPerPos,
1837 const SkPaint& paint) {
1838 CHECK_SHOULD_DRAW(draw);
1839
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001840 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001841 // this guy will just call our drawPath()
1842 draw.drawPosText((const char*)text, byteLength, pos, constY,
1843 scalarsPerPos, paint);
1844 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001845 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001846
1847 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001848 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001849 if (!skPaint2GrPaintShader(this,
1850 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001851 true,
twiz@google.com58071162012-07-18 21:41:50 +00001852 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001853 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001854 return;
1855 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001856 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1857 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001858 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1859 scalarsPerPos, paint);
1860 }
1861}
1862
1863void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1864 size_t len, const SkPath& path,
1865 const SkMatrix* m, const SkPaint& paint) {
1866 CHECK_SHOULD_DRAW(draw);
1867
1868 SkASSERT(draw.fDevice == this);
1869 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1870}
1871
1872///////////////////////////////////////////////////////////////////////////////
1873
reed@google.comf67e4cf2011-03-15 20:56:58 +00001874bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1875 if (!paint.isLCDRenderText()) {
1876 // we're cool with the paint as is
1877 return false;
1878 }
1879
1880 if (paint.getShader() ||
1881 paint.getXfermode() || // unless its srcover
1882 paint.getMaskFilter() ||
1883 paint.getRasterizer() ||
1884 paint.getColorFilter() ||
1885 paint.getPathEffect() ||
1886 paint.isFakeBoldText() ||
1887 paint.getStyle() != SkPaint::kFill_Style) {
1888 // turn off lcd
1889 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1890 flags->fHinting = paint.getHinting();
1891 return true;
1892 }
1893 // we're cool with the paint as is
1894 return false;
1895}
1896
reed@google.com75d939b2011-12-07 15:07:23 +00001897void SkGpuDevice::flush() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001898 DO_DEFERRED_CLEAR;
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001899 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001900}
1901
reed@google.comf67e4cf2011-03-15 20:56:58 +00001902///////////////////////////////////////////////////////////////////////////////
1903
bsalomon@google.comfb309512011-11-30 14:13:48 +00001904bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
1905 const GrSamplerState& sampler) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001906 uint64_t key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001907 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001908
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001909 GrTextureDesc desc;
1910 desc.fWidth = bitmap.width();
1911 desc.fHeight = bitmap.height();
rileya@google.com24f3ad12012-07-18 21:47:40 +00001912 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config());
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001913 desc.fClientCacheID = key;
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001914
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001915 return this->context()->isTextureInCache(desc, &sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001916}
1917
1918
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001919SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1920 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001921 bool isOpaque,
1922 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001923 GrTextureDesc desc;
1924 desc.fConfig = fRenderTarget->config();
1925 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1926 desc.fWidth = width;
1927 desc.fHeight = height;
1928 desc.fSampleCnt = fRenderTarget->numSamples();
1929
1930 GrContext::TextureCacheEntry cacheEntry;
1931 GrTexture* texture;
1932 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001933 // Skia's convention is to only clear a device if it is non-opaque.
1934 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001935
1936#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1937 // layers are never draw in repeat modes, so we can request an approx
1938 // match and ignore any padding.
1939 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1940 GrContext::kApprox_ScratchTexMatch :
1941 GrContext::kExact_ScratchTexMatch;
1942 cacheEntry = fContext->lockScratchTexture(desc, matchType);
1943 texture = cacheEntry.texture();
1944#else
1945 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1946 texture = tunref.get();
1947#endif
1948 if (texture) {
1949 return SkNEW_ARGS(SkGpuDevice,(fContext,
1950 texture,
1951 cacheEntry,
1952 needClear));
1953 } else {
1954 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1955 width, height);
1956 return NULL;
1957 }
1958}
1959
1960SkGpuDevice::SkGpuDevice(GrContext* context,
1961 GrTexture* texture,
1962 TexCache cacheEntry,
1963 bool needClear)
robertphillips@google.com40a1ae42012-07-13 15:36:15 +00001964 : SkDevice(make_bitmap(context, texture->asRenderTarget()))
1965 , fClipStack(NULL) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001966 GrAssert(texture && texture->asRenderTarget());
1967 GrAssert(NULL == cacheEntry.texture() || texture == cacheEntry.texture());
1968 this->initFromRenderTarget(context, texture->asRenderTarget());
1969 fCache = cacheEntry;
1970 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001971}
1972