blob: 80e3f378ed1c91b011dcbc2bfacc5a5e9fa04852 [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
robertphillips@google.com607fe072012-07-24 13:54:00 +0000364#ifdef SK_DEBUG
365static void check_bounds(const SkClipStack& clipStack,
366 const SkRegion& clipRegion,
367 const SkIPoint& origin,
368 int renderTargetWidth,
369 int renderTargetHeight) {
370
371 SkIRect bound;
372 SkClipStack::BoundsType boundType;
373 SkRect temp;
374
375 bound.setLTRB(0, 0, renderTargetWidth, renderTargetHeight);
376
377 clipStack.getBounds(&temp, &boundType);
378 if (SkClipStack::kNormal_BoundsType == boundType) {
379 SkIRect temp2;
380
381 temp.roundOut(&temp2);
382
383 temp2.offset(-origin.fX, -origin.fY);
384
385 if (!bound.intersect(temp2)) {
386 bound.setEmpty();
387 }
388 }
389
390// GrAssert(bound.contains(clipRegion.getBounds()));
391}
392#endif
393
reed@google.comac10a2d2010-12-22 21:39:39 +0000394///////////////////////////////////////////////////////////////////////////////
395
396static void convert_matrixclip(GrContext* context, const SkMatrix& matrix,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000397 const SkClipStack& clipStack,
reed@google.com6f8f2922011-03-04 22:27:10 +0000398 const SkRegion& clipRegion,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000399 const SkIPoint& origin,
400 int renderTargetWidth, int renderTargetHeight) {
bsalomon@google.comcc4dac32011-05-10 13:52:42 +0000401 context->setMatrix(matrix);
reed@google.comac10a2d2010-12-22 21:39:39 +0000402
403 SkGrClipIterator iter;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000404 iter.reset(clipStack);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000405
406#ifdef SK_DEBUG
407 check_bounds(clipStack, clipRegion, origin,
408 renderTargetWidth, renderTargetHeight);
409#endif
410
411 SkRect bounds;
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000412 bool isIntersectionOfRects = false;
robertphillips@google.com607fe072012-07-24 13:54:00 +0000413 clipStack.getConservativeBounds(-origin.fX,
414 -origin.fY,
415 renderTargetWidth,
416 renderTargetHeight,
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000417 &bounds,
418 &isIntersectionOfRects);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000419
reed@google.com6f8f2922011-03-04 22:27:10 +0000420 GrClip grc(&iter, GrIntToScalar(-origin.x()), GrIntToScalar(-origin.y()),
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000421 bounds);
robertphillips@google.com4c2a2f72012-07-24 22:07:50 +0000422
423 GrAssert(grc.isRect() == isIntersectionOfRects);
424
bsalomon@google.comd302f142011-03-03 13:54:13 +0000425 context->setClip(grc);
reed@google.comac10a2d2010-12-22 21:39:39 +0000426}
427
428// call this ever each draw call, to ensure that the context reflects our state,
429// and not the state from some other canvas/device
430void SkGpuDevice::prepareRenderTarget(const SkDraw& draw) {
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000431 GrAssert(NULL != fClipStack);
432
reed@google.comac10a2d2010-12-22 21:39:39 +0000433 if (fNeedPrepareRenderTarget ||
bsalomon@google.com5782d712011-01-21 21:03:59 +0000434 fContext->getRenderTarget() != fRenderTarget) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000435
436 fContext->setRenderTarget(fRenderTarget);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000437 SkASSERT(draw.fClipStack && draw.fClipStack == fClipStack);
robertphillips@google.com607fe072012-07-24 13:54:00 +0000438
bsalomon@google.comd302f142011-03-03 13:54:13 +0000439 convert_matrixclip(fContext, *draw.fMatrix,
robertphillips@google.com607fe072012-07-24 13:54:00 +0000440 *fClipStack, *draw.fClip, this->getOrigin(),
441 fRenderTarget->width(), fRenderTarget->height());
reed@google.comac10a2d2010-12-22 21:39:39 +0000442 fNeedPrepareRenderTarget = false;
443 }
444}
445
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000446void SkGpuDevice::setMatrixClip(const SkMatrix& matrix, const SkRegion& clip,
447 const SkClipStack& clipStack) {
448 this->INHERITED::setMatrixClip(matrix, clip, clipStack);
449 // We don't need to set them now because the context may not reflect this device.
bsalomon@google.coma7bf6e22011-04-11 19:20:46 +0000450 fNeedPrepareRenderTarget = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000451}
452
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000453void SkGpuDevice::gainFocus(const SkMatrix& matrix, const SkRegion& clip) {
454
455 GrAssert(NULL != fClipStack);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000456
reed@google.comac10a2d2010-12-22 21:39:39 +0000457 fContext->setRenderTarget(fRenderTarget);
458
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000459 this->INHERITED::gainFocus(matrix, clip);
reed@google.comac10a2d2010-12-22 21:39:39 +0000460
robertphillips@google.com607fe072012-07-24 13:54:00 +0000461 convert_matrixclip(fContext, matrix, *fClipStack, clip, this->getOrigin(),
462 fRenderTarget->width(), fRenderTarget->height());
reed@google.comac10a2d2010-12-22 21:39:39 +0000463
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000464 DO_DEFERRED_CLEAR;
reed@google.comac10a2d2010-12-22 21:39:39 +0000465}
466
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000467SkGpuRenderTarget* SkGpuDevice::accessRenderTarget() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +0000468 DO_DEFERRED_CLEAR;
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000469 return (SkGpuRenderTarget*)fRenderTarget;
reed@google.com75d939b2011-12-07 15:07:23 +0000470}
471
bsalomon@google.comc6cf7232011-02-17 16:43:10 +0000472bool SkGpuDevice::bindDeviceAsTexture(GrPaint* paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000473 if (NULL != fTexture) {
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000474 // FIXME: cannot use GrSingleTextureEffect here: fails
475 // assert in line 1617: null != devTex; generalizing GrPaint::getTexture()
476 // to grab textures off of GrCustomStages breaks gms in various ways -
477 // particularly since table color filter requires multiple textures
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +0000478 paint->setTexture(kBitmapTextureIdx, fTexture);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000479 //paint->textureSampler(kBitmapTextureIdx)->setCustomStage(
480 //SkNEW_ARGS(GrSingleTextureEffect, (fTexture)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000481 return true;
482 }
483 return false;
484}
485
486///////////////////////////////////////////////////////////////////////////////
487
vandebo@chromium.orgd3ae7792011-02-24 00:21:06 +0000488SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
489SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
490SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
491SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
492SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
493 shader_type_mismatch);
rileya@google.com3e332582012-07-03 13:43:35 +0000494SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
495 shader_type_mismatch);
rileya@google.com22e57f92012-07-19 15:16:19 +0000496SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
497SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
reed@google.comac10a2d2010-12-22 21:39:39 +0000498
bsalomon@google.com84405e02012-03-05 19:57:21 +0000499namespace {
500
501// converts a SkPaint to a GrPaint, ignoring the skPaint's shader
502// justAlpha indicates that skPaint's alpha should be used rather than the color
503// Callers may subsequently modify the GrPaint. Setting constantColor indicates
504// that the final paint will draw the same color at every pixel. This allows
505// an optimization where the the color filter can be applied to the skPaint's
twiz@google.com58071162012-07-18 21:41:50 +0000506// color once while converting to GrPaint and then ignored.
507inline bool skPaint2GrPaintNoShader(SkGpuDevice* dev,
508 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000509 bool justAlpha,
510 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000511 SkGpuDevice::SkAutoCachedTexture* act,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000512 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000513
514 grPaint->fDither = skPaint.isDither();
515 grPaint->fAntiAlias = skPaint.isAntiAlias();
bsalomon@google.comdd1be602012-01-18 20:34:00 +0000516 grPaint->fCoverage = 0xFF;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000517
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000518 SkXfermode::Coeff sm = SkXfermode::kOne_Coeff;
519 SkXfermode::Coeff dm = SkXfermode::kISA_Coeff;
bsalomon@google.com5782d712011-01-21 21:03:59 +0000520
521 SkXfermode* mode = skPaint.getXfermode();
522 if (mode) {
523 if (!mode->asCoeff(&sm, &dm)) {
bsalomon@google.com979432b2011-11-05 21:38:22 +0000524 //SkDEBUGCODE(SkDebugf("Unsupported xfer mode.\n");)
bsalomon@google.com5782d712011-01-21 21:03:59 +0000525#if 0
526 return false;
527#endif
528 }
529 }
bsalomon@google.com88939ae2011-12-14 15:58:11 +0000530 grPaint->fSrcBlendCoeff = sk_blend_to_grblend(sm);
531 grPaint->fDstBlendCoeff = sk_blend_to_grblend(dm);
532
bsalomon@google.com5782d712011-01-21 21:03:59 +0000533 if (justAlpha) {
534 uint8_t alpha = skPaint.getAlpha();
535 grPaint->fColor = GrColorPackRGBA(alpha, alpha, alpha, alpha);
Scroggod757df22011-05-16 13:11:16 +0000536 // justAlpha is currently set to true only if there is a texture,
537 // so constantColor should not also be true.
538 GrAssert(!constantColor);
bsalomon@google.com5782d712011-01-21 21:03:59 +0000539 } else {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000540 grPaint->fColor = SkColor2GrColor(skPaint.getColor());
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000541 GrAssert(NULL == grPaint->getTexture(kShaderTextureIdx));
bsalomon@google.com5782d712011-01-21 21:03:59 +0000542 }
Scroggo97c88c22011-05-11 14:05:25 +0000543 SkColorFilter* colorFilter = skPaint.getColorFilter();
544 SkColor color;
545 SkXfermode::Mode filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000546 SkScalar matrix[20];
twiz@google.com58071162012-07-18 21:41:50 +0000547 SkBitmap colorTransformTable;
Scroggo97c88c22011-05-11 14:05:25 +0000548 if (colorFilter != NULL && colorFilter->asColorMode(&color, &filterMode)) {
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000549 grPaint->fColorMatrixEnabled = false;
Scroggod757df22011-05-16 13:11:16 +0000550 if (!constantColor) {
rileya@google.com24f3ad12012-07-18 21:47:40 +0000551 grPaint->fColorFilterColor = SkColor2GrColor(color);
Scroggod757df22011-05-16 13:11:16 +0000552 grPaint->fColorFilterXfermode = filterMode;
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000553 } else {
554 SkColor filtered = colorFilter->filterColor(skPaint.getColor());
rileya@google.com24f3ad12012-07-18 21:47:40 +0000555 grPaint->fColor = SkColor2GrColor(filtered);
senorblanco@chromium.orgb3c20fa2012-01-03 21:20:19 +0000556 grPaint->resetColorFilter();
Scroggod757df22011-05-16 13:11:16 +0000557 }
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000558 } else if (colorFilter != NULL && colorFilter->asColorMatrix(matrix)) {
559 grPaint->fColorMatrixEnabled = true;
560 memcpy(grPaint->fColorMatrix, matrix, sizeof(matrix));
561 grPaint->fColorFilterXfermode = SkXfermode::kDst_Mode;
twiz@google.com58071162012-07-18 21:41:50 +0000562 } else if (colorFilter != NULL && colorFilter->asComponentTable(
563 &colorTransformTable)) {
564 grPaint->resetColorFilter();
565
566 GrSamplerState* colorSampler = grPaint->textureSampler(kColorFilterTextureIdx);
567 GrTexture* texture = act->set(dev, colorTransformTable, colorSampler);
568
bsalomon@google.com25f3e9b2012-07-20 14:23:09 +0000569 colorSampler->reset(GrSamplerState::kClamp_WrapMode, GrSamplerState::kNearest_Filter);
bsalomon@google.comcbd0ad92012-07-20 15:09:31 +0000570 colorSampler->setCustomStage(SkNEW_ARGS(GrColorTableEffect, (texture)))->unref();
senorblanco@chromium.org50bdad82012-01-03 20:51:57 +0000571 } else {
572 grPaint->resetColorFilter();
Scroggo97c88c22011-05-11 14:05:25 +0000573 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000574 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000575}
576
bsalomon@google.com84405e02012-03-05 19:57:21 +0000577// This function is similar to skPaint2GrPaintNoShader but also converts
578// skPaint's shader to a GrTexture/GrSamplerState if possible. The texture to
579// be used is set on grPaint and returned in param act. constantColor has the
580// same meaning as in skPaint2GrPaintNoShader.
581inline bool skPaint2GrPaintShader(SkGpuDevice* dev,
582 const SkPaint& skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000583 bool constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000584 SkGpuDevice::SkAutoCachedTexture textures[GrPaint::kMaxTextures],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000585 GrPaint* grPaint) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000586 SkShader* shader = skPaint.getShader();
reed@google.comac10a2d2010-12-22 21:39:39 +0000587 if (NULL == shader) {
twiz@google.com58071162012-07-18 21:41:50 +0000588 return skPaint2GrPaintNoShader(dev,
589 skPaint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000590 false,
591 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000592 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000593 grPaint);
twiz@google.com58071162012-07-18 21:41:50 +0000594 } else if (!skPaint2GrPaintNoShader(dev, skPaint, true, false,
595 &textures[kColorFilterTextureIdx], grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +0000596 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000597 }
598
rileya@google.com91f319c2012-07-25 17:18:31 +0000599 GrSamplerState* sampler = grPaint->textureSampler(kShaderTextureIdx);
600 GrCustomStage* stage = shader->asNewCustomStage(dev->context(), sampler);
601
602 if (NULL != stage) {
603 sampler->setCustomStage(stage)->unref();
604 SkMatrix localM;
605 if (shader->getLocalMatrix(&localM)) {
606 SkMatrix inverse;
607 if (localM.invert(&inverse)) {
608 sampler->matrix()->preConcat(inverse);
609 }
610 }
611 return true;
612 }
613
reed@google.comac10a2d2010-12-22 21:39:39 +0000614 SkBitmap bitmap;
rileya@google.com91f319c2012-07-25 17:18:31 +0000615 SkMatrix* matrix = sampler->matrix();
reed@google.comac10a2d2010-12-22 21:39:39 +0000616 SkShader::TileMode tileModes[2];
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000617 SkShader::BitmapType bmptype = shader->asABitmap(&bitmap, matrix,
rileya@google.com91f319c2012-07-25 17:18:31 +0000618 tileModes);
reed@google.comac10a2d2010-12-22 21:39:39 +0000619
tomhudson@google.com898e7b52012-06-01 20:42:15 +0000620 if (SkShader::kNone_BitmapType == bmptype) {
reed@google.com2be9e8b2011-07-06 21:18:09 +0000621 SkShader::GradientInfo info;
622 SkColor color;
623
624 info.fColors = &color;
625 info.fColorOffsets = NULL;
626 info.fColorCount = 1;
627 if (SkShader::kColor_GradientType == shader->asAGradient(&info)) {
628 SkPaint copy(skPaint);
629 copy.setShader(NULL);
bsalomon@google.comcd9cfd72011-07-08 16:55:04 +0000630 // modulate the paint alpha by the shader's solid color alpha
631 U8CPU newA = SkMulDiv255Round(SkColorGetA(color), copy.getAlpha());
632 copy.setColor(SkColorSetA(color, newA));
twiz@google.com58071162012-07-18 21:41:50 +0000633 return skPaint2GrPaintNoShader(dev,
634 copy,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000635 false,
636 constantColor,
twiz@google.com58071162012-07-18 21:41:50 +0000637 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +0000638 grPaint);
reed@google.com2be9e8b2011-07-06 21:18:09 +0000639 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000640 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000641 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000642
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000643 // Must set wrap and filter on the sampler before requesting a texture.
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000644 sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
645 sampler->setWrapY(sk_tile_mode_to_grwrap(tileModes[1]));
rileya@google.com91f319c2012-07-25 17:18:31 +0000646 GrSamplerState::Filter filter = GrSamplerState::kNearest_Filter;
647 if (skPaint.isFilterBitmap()) {
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000648 filter = GrSamplerState::kBilinear_Filter;
649 }
650 sampler->setFilter(filter);
twiz@google.com58071162012-07-18 21:41:50 +0000651 GrTexture* texture = textures[kShaderTextureIdx].set(dev, bitmap, sampler);
bsalomon@google.com8f4fdc92012-07-24 17:59:01 +0000652
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000653 if (NULL == texture) {
654 SkDebugf("Couldn't convert bitmap to texture.\n");
655 return false;
656 }
tomhudson@google.comd0c1a062012-07-12 17:23:52 +0000657
rileya@google.com91f319c2012-07-25 17:18:31 +0000658 sampler->setCustomStage(SkNEW_ARGS(GrSingleTextureEffect, (texture)))->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000659
reed@google.comac10a2d2010-12-22 21:39:39 +0000660 // since our texture coords will be in local space, we wack the texture
661 // matrix to map them back into 0...1 before we load it
662 SkMatrix localM;
663 if (shader->getLocalMatrix(&localM)) {
664 SkMatrix inverse;
665 if (localM.invert(&inverse)) {
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000666 matrix->preConcat(inverse);
reed@google.comac10a2d2010-12-22 21:39:39 +0000667 }
668 }
669 if (SkShader::kDefault_BitmapType == bmptype) {
bsalomon@google.com91832162012-03-08 19:53:02 +0000670 GrScalar sx = SkFloatToScalar(1.f / bitmap.width());
671 GrScalar sy = SkFloatToScalar(1.f / bitmap.height());
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000672 matrix->postScale(sx, sy);
reed@google.comac10a2d2010-12-22 21:39:39 +0000673 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000674
675 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000676}
bsalomon@google.com84405e02012-03-05 19:57:21 +0000677}
reed@google.comac10a2d2010-12-22 21:39:39 +0000678
679///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com398109c2011-04-14 18:40:27 +0000680void SkGpuDevice::clear(SkColor color) {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000681 fContext->clear(NULL, color, fRenderTarget);
bsalomon@google.com398109c2011-04-14 18:40:27 +0000682}
683
reed@google.comac10a2d2010-12-22 21:39:39 +0000684void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
685 CHECK_SHOULD_DRAW(draw);
686
bsalomon@google.com5782d712011-01-21 21:03:59 +0000687 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000688 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000689 if (!skPaint2GrPaintShader(this,
690 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000691 true,
twiz@google.com58071162012-07-18 21:41:50 +0000692 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000693 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000694 return;
695 }
bsalomon@google.com5782d712011-01-21 21:03:59 +0000696
697 fContext->drawPaint(grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +0000698}
699
700// must be in SkCanvas::PointMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +0000701static const GrPrimitiveType gPointMode2PrimtiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +0000702 kPoints_GrPrimitiveType,
703 kLines_GrPrimitiveType,
704 kLineStrip_GrPrimitiveType
reed@google.comac10a2d2010-12-22 21:39:39 +0000705};
706
707void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
bsalomon@google.com5782d712011-01-21 21:03:59 +0000708 size_t count, const SkPoint pts[], const SkPaint& paint) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000709 CHECK_SHOULD_DRAW(draw);
710
711 SkScalar width = paint.getStrokeWidth();
712 if (width < 0) {
713 return;
714 }
715
bsalomon@google.comb702c0f2012-06-18 12:52:56 +0000716 // we only handle hairlines and paints without path effects or mask filters,
717 // else we let the SkDraw call our drawPath()
718 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000719 draw.drawPoints(mode, count, pts, paint, true);
720 return;
721 }
722
bsalomon@google.com5782d712011-01-21 21:03:59 +0000723 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000724 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000725 if (!skPaint2GrPaintShader(this,
726 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000727 true,
twiz@google.com58071162012-07-18 21:41:50 +0000728 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000729 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000730 return;
731 }
732
bsalomon@google.com5782d712011-01-21 21:03:59 +0000733 fContext->drawVertices(grPaint,
734 gPointMode2PrimtiveType[mode],
735 count,
736 (GrPoint*)pts,
737 NULL,
738 NULL,
739 NULL,
740 0);
reed@google.comac10a2d2010-12-22 21:39:39 +0000741}
742
reed@google.comc9aa5872011-04-05 21:05:37 +0000743///////////////////////////////////////////////////////////////////////////////
744
reed@google.comac10a2d2010-12-22 21:39:39 +0000745void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
746 const SkPaint& paint) {
reed@google.comb0a34d82012-07-11 19:57:55 +0000747 CHECK_FOR_NODRAW_ANNOTATION(paint);
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000748 CHECK_SHOULD_DRAW(draw);
749
bungeman@google.com79bd8772011-07-18 15:34:08 +0000750 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000751 SkScalar width = paint.getStrokeWidth();
752
753 /*
754 We have special code for hairline strokes, miter-strokes, and fills.
755 Anything else we just call our path code.
756 */
757 bool usePath = doStroke && width > 0 &&
758 paint.getStrokeJoin() != SkPaint::kMiter_Join;
bsalomon@google.com22f42b72012-03-26 14:36:55 +0000759 // another two reasons we might need to call drawPath...
760 if (paint.getMaskFilter() || paint.getPathEffect()) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000761 usePath = true;
762 }
reed@google.com67db6642011-05-26 11:46:35 +0000763 // until we aa rotated rects...
764 if (!usePath && paint.isAntiAlias() && !draw.fMatrix->rectStaysRect()) {
765 usePath = true;
766 }
bungeman@google.com633722e2011-08-09 18:32:51 +0000767 // small miter limit means right angles show bevel...
768 if (SkPaint::kMiter_Join == paint.getStrokeJoin() &&
769 paint.getStrokeMiter() < SK_ScalarSqrt2)
770 {
771 usePath = true;
772 }
bungeman@google.com79bd8772011-07-18 15:34:08 +0000773 // until we can both stroke and fill rectangles
bungeman@google.com79bd8772011-07-18 15:34:08 +0000774 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
775 usePath = true;
776 }
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000777
778 if (usePath) {
779 SkPath path;
780 path.addRect(rect);
781 this->drawPath(draw, path, paint, NULL, true);
782 return;
783 }
784
785 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +0000786 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +0000787 if (!skPaint2GrPaintShader(this,
788 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000789 true,
twiz@google.com58071162012-07-18 21:41:50 +0000790 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +0000791 &grPaint)) {
bsalomon@google.comfea37b52011-04-25 15:51:06 +0000792 return;
793 }
reed@google.com20efde72011-05-09 17:00:02 +0000794 fContext->drawRect(grPaint, rect, doStroke ? width : -1);
reed@google.comac10a2d2010-12-22 21:39:39 +0000795}
796
reed@google.com69302852011-02-16 18:08:07 +0000797#include "SkMaskFilter.h"
798#include "SkBounder.h"
799
bsalomon@google.com85003222012-03-28 14:44:37 +0000800///////////////////////////////////////////////////////////////////////////////
801
802// helpers for applying mask filters
803namespace {
804
805GrPathFill skToGrFillType(SkPath::FillType fillType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000806 switch (fillType) {
807 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000808 return kWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000809 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000810 return kEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000811 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000812 return kInverseWinding_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000813 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +0000814 return kInverseEvenOdd_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000815 default:
816 SkDebugf("Unsupported path fill type\n");
bsalomon@google.com47059542012-06-06 20:51:20 +0000817 return kHairLine_GrPathFill;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000818 }
819}
820
bsalomon@google.com85003222012-03-28 14:44:37 +0000821// We prefer to blur small rect with small radius via CPU.
822#define MIN_GPU_BLUR_SIZE SkIntToScalar(64)
823#define MIN_GPU_BLUR_RADIUS SkIntToScalar(32)
824inline bool shouldDrawBlurWithCPU(const SkRect& rect, SkScalar radius) {
825 if (rect.width() <= MIN_GPU_BLUR_SIZE &&
826 rect.height() <= MIN_GPU_BLUR_SIZE &&
827 radius <= MIN_GPU_BLUR_RADIUS) {
828 return true;
829 }
830 return false;
831}
832
833bool drawWithGPUMaskFilter(GrContext* context, const SkPath& path,
834 SkMaskFilter* filter, const SkMatrix& matrix,
835 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000836 GrPaint* grp, GrPathFill pathFillType) {
senorblanco@chromium.orga479fc72011-07-19 16:40:58 +0000837#ifdef SK_DISABLE_GPU_BLUR
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000838 return false;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000839#endif
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000840 SkMaskFilter::BlurInfo info;
841 SkMaskFilter::BlurType blurType = filter->asABlur(&info);
bsalomon@google.comdafde9e2012-01-11 18:45:39 +0000842 if (SkMaskFilter::kNone_BlurType == blurType) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000843 return false;
844 }
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000845 SkScalar radius = info.fIgnoreTransform ? info.fRadius
846 : matrix.mapRadius(info.fRadius);
847 radius = SkMinScalar(radius, MAX_BLUR_RADIUS);
senorblanco@chromium.org68c4d122011-08-01 21:20:31 +0000848 if (radius <= 0) {
849 return false;
850 }
bsalomon@google.com85003222012-03-28 14:44:37 +0000851
852 SkRect srcRect = path.getBounds();
853 if (shouldDrawBlurWithCPU(srcRect, radius)) {
854 return false;
855 }
856
senorblanco@chromium.orge36ddf02011-07-15 14:28:16 +0000857 float sigma = SkScalarToFloat(radius) * BLUR_SIGMA_SCALE;
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000858 float sigma3 = sigma * 3.0f;
859
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000860 SkRect clipRect;
861 clipRect.set(clip.getBounds());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000862
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000863 // Outset srcRect and clipRect by 3 * sigma, to compute affected blur area.
robertphillips@google.com5af56062012-04-27 15:39:52 +0000864 srcRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
865 clipRect.inset(SkFloatToScalar(-sigma3), SkFloatToScalar(-sigma3));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000866 srcRect.intersect(clipRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000867 SkRect finalRect = srcRect;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000868 SkIRect finalIRect;
869 finalRect.roundOut(&finalIRect);
870 if (clip.quickReject(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000871 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000872 }
873 if (bounder && !bounder->doIRect(finalIRect)) {
senorblanco@chromium.orgaadd9f82011-07-12 19:44:51 +0000874 return true;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000875 }
876 GrPoint offset = GrPoint::Make(-srcRect.fLeft, -srcRect.fTop);
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000877 srcRect.offset(offset);
robertphillips@google.com75b3c962012-06-07 12:08:45 +0000878 GrTextureDesc desc;
879 desc.fFlags = kRenderTarget_GrTextureFlagBit;
880 desc.fWidth = SkScalarCeilToInt(srcRect.width());
881 desc.fHeight = SkScalarCeilToInt(srcRect.height());
882 // We actually only need A8, but it often isn't supported as a
883 // render target so default to RGBA_8888
884 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000885
robertphillips@google.com99a5ac02012-04-10 19:26:38 +0000886 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig)) {
887 desc.fConfig = kAlpha_8_GrPixelConfig;
888 }
889
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000890 GrAutoScratchTexture pathEntry(context, desc);
891 GrTexture* pathTexture = pathEntry.texture();
892 if (NULL == pathTexture) {
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000893 return false;
894 }
895 GrRenderTarget* oldRenderTarget = context->getRenderTarget();
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000896 // Once this code moves into GrContext, this should be changed to use
897 // an AutoClipRestore.
898 GrClip oldClip = context->getClip();
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000899 context->setRenderTarget(pathTexture->asRenderTarget());
robertphillips@google.com3e11c0b2012-07-11 18:20:35 +0000900
901 GrClip newClip(srcRect);
902 context->setClip(newClip);
903
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000904 context->clear(NULL, 0);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000905 GrPaint tempPaint;
906 tempPaint.reset();
907
robertphillips@google.comfea85ac2012-07-11 18:53:23 +0000908 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000909 tempPaint.fAntiAlias = grp->fAntiAlias;
910 if (tempPaint.fAntiAlias) {
911 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
912 // blend coeff of zero requires dual source blending support in order
913 // to properly blend partially covered pixels. This means the AA
914 // code path may not be taken. So we use a dst blend coeff of ISA. We
915 // could special case AA draws to a dst surface with known alpha=0 to
916 // use a zero dst coeff when dual source blending isn't available.
bsalomon@google.com47059542012-06-06 20:51:20 +0000917 tempPaint.fSrcBlendCoeff = kOne_GrBlendCoeff;
918 tempPaint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000919 }
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000920 // Draw hard shadow to pathTexture with path topleft at origin 0,0.
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000921 context->drawPath(tempPaint, path, pathFillType, &offset);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000922
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000923 // If we're doing a normal blur, we can clobber the pathTexture in the
924 // gaussianBlur. Otherwise, we need to save it for later compositing.
925 bool isNormalBlur = blurType == SkMaskFilter::kNormal_BlurType;
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +0000926 SkAutoTUnref<GrTexture> blurTexture(context->gaussianBlur(
927 pathTexture, isNormalBlur, srcRect, sigma, sigma));
bsalomon@google.com50398bf2011-07-26 20:45:30 +0000928
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000929 if (!isNormalBlur) {
930 GrPaint paint;
931 paint.reset();
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000932 paint.textureSampler(0)->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000933 paint.textureSampler(0)->matrix()->setIDiv(pathTexture->width(),
934 pathTexture->height());
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000935 // Blend pathTexture over blurTexture.
936 context->setRenderTarget(blurTexture->asRenderTarget());
tomhudson@google.com1e8f0162012-07-20 16:25:18 +0000937 paint.textureSampler(0)->setCustomStage(SkNEW_ARGS
938 (GrSingleTextureEffect, (pathTexture)))->unref();
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000939 if (SkMaskFilter::kInner_BlurType == blurType) {
940 // inner: dst = dst * src
bsalomon@google.com47059542012-06-06 20:51:20 +0000941 paint.fSrcBlendCoeff = kDC_GrBlendCoeff;
942 paint.fDstBlendCoeff = kZero_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000943 } else if (SkMaskFilter::kSolid_BlurType == blurType) {
944 // solid: dst = src + dst - src * dst
945 // = (1 - dst) * src + 1 * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000946 paint.fSrcBlendCoeff = kIDC_GrBlendCoeff;
947 paint.fDstBlendCoeff = kOne_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000948 } else if (SkMaskFilter::kOuter_BlurType == blurType) {
949 // outer: dst = dst * (1 - src)
950 // = 0 * src + (1 - src) * dst
bsalomon@google.com47059542012-06-06 20:51:20 +0000951 paint.fSrcBlendCoeff = kZero_GrBlendCoeff;
952 paint.fDstBlendCoeff = kISC_GrBlendCoeff;
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +0000953 }
954 context->drawRect(paint, srcRect);
955 }
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000956 context->setRenderTarget(oldRenderTarget);
senorblanco@chromium.org42dd0f92011-07-14 15:29:57 +0000957 context->setClip(oldClip);
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000958
bsalomon@google.come3d32162012-07-20 13:37:06 +0000959 if (!grp->preConcatSamplerMatricesWithInverse(matrix)) {
960 return false;
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000961 }
962
963 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
964 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +0000965 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
senorblanco@chromium.org60014ca2011-11-09 16:05:58 +0000966 grp->setMask(MASK_IDX, blurTexture);
bsalomon@google.com97912912011-12-06 16:30:36 +0000967 grp->maskSampler(MASK_IDX)->reset();
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000968
bsalomon@google.comaa814fe2011-12-12 18:45:07 +0000969 grp->maskSampler(MASK_IDX)->matrix()->setTranslate(-finalRect.fLeft,
970 -finalRect.fTop);
971 grp->maskSampler(MASK_IDX)->matrix()->postIDiv(blurTexture->width(),
972 blurTexture->height());
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000973 context->drawRect(*grp, finalRect);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +0000974 return true;
975}
976
bsalomon@google.com85003222012-03-28 14:44:37 +0000977bool drawWithMaskFilter(GrContext* context, const SkPath& path,
978 SkMaskFilter* filter, const SkMatrix& matrix,
979 const SkRegion& clip, SkBounder* bounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000980 GrPaint* grp, SkPaint::Style style) {
reed@google.com69302852011-02-16 18:08:07 +0000981 SkMask srcM, dstM;
982
983 if (!SkDraw::DrawToMask(path, &clip.getBounds(), filter, &matrix, &srcM,
junov@chromium.org2ac4ef52012-04-04 15:16:51 +0000984 SkMask::kComputeBoundsAndRenderImage_CreateMode,
junov@chromium.orgaad7e272012-04-04 21:01:08 +0000985 style)) {
reed@google.com69302852011-02-16 18:08:07 +0000986 return false;
987 }
bungeman@google.com02f55842011-10-04 21:25:00 +0000988 SkAutoMaskFreeImage autoSrc(srcM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000989
990 if (!filter->filterMask(&dstM, srcM, matrix, NULL)) {
991 return false;
992 }
993 // this will free-up dstM when we're done (allocated in filterMask())
bungeman@google.com02f55842011-10-04 21:25:00 +0000994 SkAutoMaskFreeImage autoDst(dstM.fImage);
reed@google.com69302852011-02-16 18:08:07 +0000995
996 if (clip.quickReject(dstM.fBounds)) {
997 return false;
998 }
999 if (bounder && !bounder->doIRect(dstM.fBounds)) {
1000 return false;
1001 }
1002
1003 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
1004 // the current clip (and identity matrix) and grpaint settings
1005
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001006 GrContext::AutoMatrix avm(context, GrMatrix::I());
reed@google.com69302852011-02-16 18:08:07 +00001007
bsalomon@google.come3d32162012-07-20 13:37:06 +00001008 if (!grp->preConcatSamplerMatricesWithInverse(matrix)) {
1009 return false;
1010 }
1011
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001012 GrTextureDesc desc;
1013 desc.fWidth = dstM.fBounds.width();
1014 desc.fHeight = dstM.fBounds.height();
1015 desc.fConfig = kAlpha_8_GrPixelConfig;
reed@google.com69302852011-02-16 18:08:07 +00001016
bsalomon@google.com50398bf2011-07-26 20:45:30 +00001017 GrAutoScratchTexture ast(context, desc);
1018 GrTexture* texture = ast.texture();
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001019
reed@google.com69302852011-02-16 18:08:07 +00001020 if (NULL == texture) {
1021 return false;
1022 }
bsalomon@google.com6f379512011-11-16 20:36:03 +00001023 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
bsalomon@google.comeb2aa1d2011-07-14 15:45:19 +00001024 dstM.fImage, dstM.fRowBytes);
reed@google.com69302852011-02-16 18:08:07 +00001025
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001026 static const int MASK_IDX = GrPaint::kMaxMasks - 1;
1027 // we assume the last mask index is available for use
tomhudson@google.comf13f5882012-06-25 17:27:28 +00001028 GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001029 grp->setMask(MASK_IDX, texture);
bsalomon@google.com97912912011-12-06 16:30:36 +00001030 grp->maskSampler(MASK_IDX)->reset();
reed@google.com69302852011-02-16 18:08:07 +00001031
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001032 GrRect d;
1033 d.setLTRB(GrIntToScalar(dstM.fBounds.fLeft),
reed@google.com0c219b62011-02-16 21:31:18 +00001034 GrIntToScalar(dstM.fBounds.fTop),
1035 GrIntToScalar(dstM.fBounds.fRight),
1036 GrIntToScalar(dstM.fBounds.fBottom));
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001037
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001038 GrMatrix* m = grp->maskSampler(MASK_IDX)->matrix();
1039 m->setTranslate(-dstM.fBounds.fLeft*SK_Scalar1,
1040 -dstM.fBounds.fTop*SK_Scalar1);
1041 m->postIDiv(texture->width(), texture->height());
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001042 context->drawRect(*grp, d);
reed@google.com69302852011-02-16 18:08:07 +00001043 return true;
1044}
reed@google.com69302852011-02-16 18:08:07 +00001045
bsalomon@google.com85003222012-03-28 14:44:37 +00001046}
1047
1048///////////////////////////////////////////////////////////////////////////////
1049
reed@google.com0c219b62011-02-16 21:31:18 +00001050void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001051 const SkPaint& paint, const SkMatrix* prePathMatrix,
reed@google.comac10a2d2010-12-22 21:39:39 +00001052 bool pathIsMutable) {
reed@google.comb0a34d82012-07-11 19:57:55 +00001053 CHECK_FOR_NODRAW_ANNOTATION(paint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001054 CHECK_SHOULD_DRAW(draw);
1055
reed@google.comfe626382011-09-21 13:50:35 +00001056 bool doFill = true;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001057
bsalomon@google.com5782d712011-01-21 21:03:59 +00001058 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001059 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001060 if (!skPaint2GrPaintShader(this,
1061 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001062 true,
twiz@google.com58071162012-07-18 21:41:50 +00001063 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001064 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001065 return;
1066 }
1067
bsalomon@google.com8c0a0d32012-03-05 16:01:18 +00001068 // can we cheat, and threat a thin stroke as a hairline w/ coverage
1069 // if we can, we draw lots faster (raster device does this same test)
1070 SkScalar hairlineCoverage;
1071 if (SkDrawTreatAsHairline(paint, *draw.fMatrix, &hairlineCoverage)) {
1072 doFill = false;
1073 grPaint.fCoverage = SkScalarRoundToInt(hairlineCoverage *
1074 grPaint.fCoverage);
1075 }
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001076
reed@google.comfe626382011-09-21 13:50:35 +00001077 // If we have a prematrix, apply it to the path, optimizing for the case
1078 // where the original path can in fact be modified in place (even though
1079 // its parameter type is const).
1080 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
1081 SkPath tmpPath;
reed@google.comac10a2d2010-12-22 21:39:39 +00001082
1083 if (prePathMatrix) {
reed@google.come3445642011-02-16 23:20:39 +00001084 SkPath* result = pathPtr;
reed@google.com0c219b62011-02-16 21:31:18 +00001085
reed@google.come3445642011-02-16 23:20:39 +00001086 if (!pathIsMutable) {
1087 result = &tmpPath;
1088 pathIsMutable = true;
reed@google.comac10a2d2010-12-22 21:39:39 +00001089 }
reed@google.come3445642011-02-16 23:20:39 +00001090 // should I push prePathMatrix on our MV stack temporarily, instead
1091 // of applying it here? See SkDraw.cpp
1092 pathPtr->transform(*prePathMatrix, result);
1093 pathPtr = result;
reed@google.comac10a2d2010-12-22 21:39:39 +00001094 }
reed@google.com0c219b62011-02-16 21:31:18 +00001095 // at this point we're done with prePathMatrix
1096 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
reed@google.comac10a2d2010-12-22 21:39:39 +00001097
bsalomon@google.com8b58c4d2012-02-13 14:49:09 +00001098 if (paint.getPathEffect() ||
1099 (doFill && paint.getStyle() != SkPaint::kFill_Style)) {
reed@google.comfe626382011-09-21 13:50:35 +00001100 // it is safe to use tmpPath here, even if we already used it for the
1101 // prepathmatrix, since getFillPath can take the same object for its
1102 // input and output safely.
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001103 doFill = paint.getFillPath(*pathPtr, &tmpPath);
reed@google.com0c219b62011-02-16 21:31:18 +00001104 pathPtr = &tmpPath;
1105 }
1106
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001107 if (paint.getMaskFilter()) {
reed@google.com0c219b62011-02-16 21:31:18 +00001108 // avoid possibly allocating a new path in transform if we can
1109 SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath;
1110
1111 // transform the path into device space
reed@google.come3445642011-02-16 23:20:39 +00001112 pathPtr->transform(*draw.fMatrix, devPathPtr);
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001113 GrPathFill pathFillType = doFill ?
bsalomon@google.com47059542012-06-06 20:51:20 +00001114 skToGrFillType(devPathPtr->getFillType()) : kHairLine_GrPathFill;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001115 if (!drawWithGPUMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.orgb08ea1b2011-07-12 16:54:59 +00001116 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001117 &grPaint, pathFillType)) {
1118 SkPaint::Style style = doFill ? SkPaint::kFill_Style :
1119 SkPaint::kStroke_Style;
bsalomon@google.comdd1be602012-01-18 20:34:00 +00001120 drawWithMaskFilter(fContext, *devPathPtr, paint.getMaskFilter(),
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001121 *draw.fMatrix, *draw.fClip, draw.fBounder,
junov@chromium.orgaad7e272012-04-04 21:01:08 +00001122 &grPaint, style);
senorblanco@chromium.org027de5f2011-07-08 18:03:33 +00001123 }
reed@google.com69302852011-02-16 18:08:07 +00001124 return;
1125 }
reed@google.com69302852011-02-16 18:08:07 +00001126
bsalomon@google.com47059542012-06-06 20:51:20 +00001127 GrPathFill fill = kHairLine_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001128
reed@google.com0c219b62011-02-16 21:31:18 +00001129 if (doFill) {
1130 switch (pathPtr->getFillType()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001131 case SkPath::kWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001132 fill = kWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001133 break;
1134 case SkPath::kEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001135 fill = kEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001136 break;
1137 case SkPath::kInverseWinding_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001138 fill = kInverseWinding_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001139 break;
1140 case SkPath::kInverseEvenOdd_FillType:
bsalomon@google.com47059542012-06-06 20:51:20 +00001141 fill = kInverseEvenOdd_GrPathFill;
reed@google.comac10a2d2010-12-22 21:39:39 +00001142 break;
1143 default:
bsalomon@google.com5782d712011-01-21 21:03:59 +00001144 SkDebugf("Unsupported path fill type\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001145 return;
1146 }
1147 }
1148
reed@google.com07f3ee12011-05-16 17:21:57 +00001149 fContext->drawPath(grPaint, *pathPtr, fill);
reed@google.comac10a2d2010-12-22 21:39:39 +00001150}
1151
bsalomon@google.comfb309512011-11-30 14:13:48 +00001152namespace {
1153
1154inline int get_tile_count(int l, int t, int r, int b, int tileSize) {
1155 int tilesX = (r / tileSize) - (l / tileSize) + 1;
1156 int tilesY = (b / tileSize) - (t / tileSize) + 1;
1157 return tilesX * tilesY;
1158}
1159
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001160inline int determine_tile_size(const SkBitmap& bitmap,
bsalomon@google.comfb309512011-11-30 14:13:48 +00001161 const SkIRect* srcRectPtr,
1162 int maxTextureSize) {
1163 static const int kSmallTileSize = 1 << 10;
1164 if (maxTextureSize <= kSmallTileSize) {
1165 return maxTextureSize;
1166 }
1167
1168 size_t maxTexTotalTileSize;
1169 size_t smallTotalTileSize;
1170
1171 if (NULL == srcRectPtr) {
1172 int w = bitmap.width();
1173 int h = bitmap.height();
1174 maxTexTotalTileSize = get_tile_count(0, 0, w, h, maxTextureSize);
1175 smallTotalTileSize = get_tile_count(0, 0, w, h, kSmallTileSize);
1176 } else {
1177 maxTexTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1178 srcRectPtr->fTop,
1179 srcRectPtr->fRight,
1180 srcRectPtr->fBottom,
1181 maxTextureSize);
1182 smallTotalTileSize = get_tile_count(srcRectPtr->fLeft,
1183 srcRectPtr->fTop,
1184 srcRectPtr->fRight,
1185 srcRectPtr->fBottom,
1186 kSmallTileSize);
1187 }
1188 maxTexTotalTileSize *= maxTextureSize * maxTextureSize;
1189 smallTotalTileSize *= kSmallTileSize * kSmallTileSize;
1190
1191 if (maxTexTotalTileSize > 2 * smallTotalTileSize) {
1192 return kSmallTileSize;
1193 } else {
1194 return maxTextureSize;
1195 }
1196}
1197}
1198
1199bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
1200 const GrSamplerState& sampler,
1201 const SkIRect* srcRectPtr,
1202 int* tileSize) const {
1203 SkASSERT(NULL != tileSize);
1204
1205 // if bitmap is explictly texture backed then just use the texture
1206 if (NULL != bitmap.getTexture()) {
1207 return false;
1208 }
1209 // if it's larger than the max texture size, then we have no choice but
1210 // tiling
1211 const int maxTextureSize = fContext->getMaxTextureSize();
1212 if (bitmap.width() > maxTextureSize ||
1213 bitmap.height() > maxTextureSize) {
1214 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1215 return true;
1216 }
1217 // if we are going to have to draw the whole thing, then don't tile
1218 if (NULL == srcRectPtr) {
1219 return false;
1220 }
1221 // if the entire texture is already in our cache then no reason to tile it
1222 if (this->isBitmapInTextureCache(bitmap, sampler)) {
1223 return false;
1224 }
1225
1226 // At this point we know we could do the draw by uploading the entire bitmap
1227 // as a texture. However, if the texture would be large compared to the
1228 // cache size and we don't require most of it for this draw then tile to
1229 // reduce the amount of upload and cache spill.
1230
1231 // assumption here is that sw bitmap size is a good proxy for its size as
1232 // a texture
1233 size_t bmpSize = bitmap.getSize();
bsalomon@google.com07fc0d12012-06-22 15:15:59 +00001234 size_t cacheSize;
1235 fContext->getTextureCacheLimits(NULL, &cacheSize);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001236 if (bmpSize < cacheSize / 2) {
1237 return false;
1238 }
1239
1240 SkFixed fracUsed =
1241 SkFixedMul((srcRectPtr->width() << 16) / bitmap.width(),
1242 (srcRectPtr->height() << 16) / bitmap.height());
1243 if (fracUsed <= SK_FixedHalf) {
1244 *tileSize = determine_tile_size(bitmap, srcRectPtr, maxTextureSize);
1245 return true;
1246 } else {
1247 return false;
1248 }
1249}
1250
reed@google.comac10a2d2010-12-22 21:39:39 +00001251void SkGpuDevice::drawBitmap(const SkDraw& draw,
1252 const SkBitmap& bitmap,
1253 const SkIRect* srcRectPtr,
1254 const SkMatrix& m,
1255 const SkPaint& paint) {
1256 CHECK_SHOULD_DRAW(draw);
1257
1258 SkIRect srcRect;
1259 if (NULL == srcRectPtr) {
1260 srcRect.set(0, 0, bitmap.width(), bitmap.height());
1261 } else {
1262 srcRect = *srcRectPtr;
1263 }
1264
junov@google.comd935cfb2011-06-27 20:48:23 +00001265 if (paint.getMaskFilter()){
junov@google.com1d329782011-07-28 20:10:09 +00001266 // Convert the bitmap to a shader so that the rect can be drawn
1267 // through drawRect, which supports mask filters.
1268 SkBitmap tmp; // subset of bitmap, if necessary
junov@google.comd935cfb2011-06-27 20:48:23 +00001269 const SkBitmap* bitmapPtr = &bitmap;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001270 if (srcRectPtr) {
1271 if (!bitmap.extractSubset(&tmp, srcRect)) {
1272 return; // extraction failed
1273 }
1274 bitmapPtr = &tmp;
junov@google.com1d329782011-07-28 20:10:09 +00001275 srcRect.set(0,0, srcRect.width(), srcRect.height());
junov@google.comd935cfb2011-06-27 20:48:23 +00001276 }
1277 SkPaint paintWithTexture(paint);
1278 paintWithTexture.setShader(SkShader::CreateBitmapShader( *bitmapPtr,
1279 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode))->unref();
junov@google.comd935cfb2011-06-27 20:48:23 +00001280 SkRect ScalarRect;
epoger@google.com9ef2d832011-07-01 21:12:20 +00001281 ScalarRect.set(srcRect);
junov@google.comd935cfb2011-06-27 20:48:23 +00001282
junov@google.com1d329782011-07-28 20:10:09 +00001283 // Transform 'm' needs to be concatenated to the draw matrix,
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001284 // rather than transforming the primitive directly, so that 'm' will
junov@google.com1d329782011-07-28 20:10:09 +00001285 // also affect the behavior of the mask filter.
1286 SkMatrix drawMatrix;
1287 drawMatrix.setConcat(*draw.fMatrix, m);
1288 SkDraw transformedDraw(draw);
1289 transformedDraw.fMatrix = &drawMatrix;
1290
1291 this->drawRect(transformedDraw, ScalarRect, paintWithTexture);
1292
junov@google.comd935cfb2011-06-27 20:48:23 +00001293 return;
1294 }
1295
bsalomon@google.com5782d712011-01-21 21:03:59 +00001296 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001297 SkAutoCachedTexture colorLutTexture;
1298 if (!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001299 return;
1300 }
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001301 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001302 if (paint.isFilterBitmap()) {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001303 sampler->setFilter(GrSamplerState::kBilinear_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001304 } else {
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001305 sampler->setFilter(GrSamplerState::kNearest_Filter);
bsalomon@google.com6aef1fb2011-05-05 12:33:22 +00001306 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001307
bsalomon@google.comfb309512011-11-30 14:13:48 +00001308 int tileSize;
1309 if (!this->shouldTileBitmap(bitmap, *sampler, srcRectPtr, &tileSize)) {
1310 // take the simple case
bsalomon@google.com5782d712011-01-21 21:03:59 +00001311 this->internalDrawBitmap(draw, bitmap, srcRect, m, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001312 return;
1313 }
1314
1315 // undo the translate done by SkCanvas
1316 int DX = SkMax32(0, srcRect.fLeft);
1317 int DY = SkMax32(0, srcRect.fTop);
1318 // compute clip bounds in local coordinates
1319 SkIRect clipRect;
1320 {
1321 SkRect r;
1322 r.set(draw.fClip->getBounds());
1323 SkMatrix matrix, inverse;
1324 matrix.setConcat(*draw.fMatrix, m);
1325 if (!matrix.invert(&inverse)) {
1326 return;
1327 }
1328 inverse.mapRect(&r);
1329 r.roundOut(&clipRect);
1330 // apply the canvas' translate to our local clip
1331 clipRect.offset(DX, DY);
1332 }
1333
bsalomon@google.comfb309512011-11-30 14:13:48 +00001334 int nx = bitmap.width() / tileSize;
1335 int ny = bitmap.height() / tileSize;
reed@google.comac10a2d2010-12-22 21:39:39 +00001336 for (int x = 0; x <= nx; x++) {
1337 for (int y = 0; y <= ny; y++) {
1338 SkIRect tileR;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001339 tileR.set(x * tileSize, y * tileSize,
1340 (x + 1) * tileSize, (y + 1) * tileSize);
reed@google.comac10a2d2010-12-22 21:39:39 +00001341 if (!SkIRect::Intersects(tileR, clipRect)) {
1342 continue;
1343 }
1344
1345 SkIRect srcR = tileR;
1346 if (!srcR.intersect(srcRect)) {
1347 continue;
1348 }
1349
1350 SkBitmap tmpB;
1351 if (bitmap.extractSubset(&tmpB, tileR)) {
1352 // now offset it to make it "local" to our tmp bitmap
1353 srcR.offset(-tileR.fLeft, -tileR.fTop);
1354
1355 SkMatrix tmpM(m);
1356 {
1357 int dx = tileR.fLeft - DX + SkMax32(0, srcR.fLeft);
1358 int dy = tileR.fTop - DY + SkMax32(0, srcR.fTop);
1359 tmpM.preTranslate(SkIntToScalar(dx), SkIntToScalar(dy));
1360 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001361 this->internalDrawBitmap(draw, tmpB, srcR, tmpM, &grPaint);
reed@google.comac10a2d2010-12-22 21:39:39 +00001362 }
1363 }
1364 }
1365}
1366
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001367namespace {
1368
1369bool hasAlignedSamples(const SkRect& srcRect, const SkRect& transformedRect) {
1370 // detect pixel disalignment
1371 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1372 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1373 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1374 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1375 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1376 COLOR_BLEED_TOLERANCE &&
1377 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1378 COLOR_BLEED_TOLERANCE) {
1379 return true;
1380 }
1381 return false;
1382}
1383
1384bool mayColorBleed(const SkRect& srcRect, const SkRect& transformedRect,
1385 const SkMatrix& m) {
1386 // Only gets called if hasAlignedSamples returned false.
1387 // So we can assume that sampling is axis aligned but not texel aligned.
1388 GrAssert(!hasAlignedSamples(srcRect, transformedRect));
1389 SkRect innerSrcRect(srcRect), innerTransformedRect,
1390 outerTransformedRect(transformedRect);
1391 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1392 m.mapRect(&innerTransformedRect, innerSrcRect);
1393
1394 // The gap between outerTransformedRect and innerTransformedRect
1395 // represents the projection of the source border area, which is
1396 // problematic for color bleeding. We must check whether any
1397 // destination pixels sample the border area.
1398 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1399 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1400 SkIRect outer, inner;
1401 outerTransformedRect.round(&outer);
1402 innerTransformedRect.round(&inner);
1403 // If the inner and outer rects round to the same result, it means the
1404 // border does not overlap any pixel centers. Yay!
1405 return inner != outer;
1406}
1407
1408} // unnamed namespace
1409
reed@google.comac10a2d2010-12-22 21:39:39 +00001410/*
1411 * This is called by drawBitmap(), which has to handle images that may be too
1412 * large to be represented by a single texture.
1413 *
bsalomon@google.com5782d712011-01-21 21:03:59 +00001414 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1415 * and that non-texture portion of the GrPaint has already been setup.
reed@google.comac10a2d2010-12-22 21:39:39 +00001416 */
1417void SkGpuDevice::internalDrawBitmap(const SkDraw& draw,
1418 const SkBitmap& bitmap,
1419 const SkIRect& srcRect,
1420 const SkMatrix& m,
bsalomon@google.com5782d712011-01-21 21:03:59 +00001421 GrPaint* grPaint) {
bsalomon@google.com91958362011-06-13 17:58:13 +00001422 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1423 bitmap.height() <= fContext->getMaxTextureSize());
reed@google.comac10a2d2010-12-22 21:39:39 +00001424
reed@google.com9c49bc32011-07-07 13:42:37 +00001425 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001426 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
reed@google.com9c49bc32011-07-07 13:42:37 +00001427 SkDebugf("nothing to draw\n");
reed@google.comac10a2d2010-12-22 21:39:39 +00001428 return;
1429 }
1430
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001431 GrSamplerState* sampler = grPaint->textureSampler(kBitmapTextureIdx);
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001432
1433 sampler->setWrapX(GrSamplerState::kClamp_WrapMode);
1434 sampler->setWrapY(GrSamplerState::kClamp_WrapMode);
bsalomon@google.comaa814fe2011-12-12 18:45:07 +00001435 sampler->matrix()->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +00001436
1437 GrTexture* texture;
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001438 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
reed@google.comac10a2d2010-12-22 21:39:39 +00001439 if (NULL == texture) {
1440 return;
1441 }
1442
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001443 grPaint->textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1444 (GrSingleTextureEffect, (texture)))->unref();
reed@google.com46799cd2011-02-22 20:56:26 +00001445
reed@google.com20efde72011-05-09 17:00:02 +00001446 GrRect dstRect = SkRect::MakeWH(GrIntToScalar(srcRect.width()),
1447 GrIntToScalar(srcRect.height()));
bsalomon@google.comc6cf7232011-02-17 16:43:10 +00001448 GrRect paintRect;
bsalomon@google.com91832162012-03-08 19:53:02 +00001449 float wInv = 1.f / bitmap.width();
1450 float hInv = 1.f / bitmap.height();
1451 paintRect.setLTRB(SkFloatToScalar(srcRect.fLeft * wInv),
1452 SkFloatToScalar(srcRect.fTop * hInv),
1453 SkFloatToScalar(srcRect.fRight * wInv),
1454 SkFloatToScalar(srcRect.fBottom * hInv));
reed@google.comac10a2d2010-12-22 21:39:39 +00001455
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001456 bool needsTextureDomain = false;
1457 if (GrSamplerState::kBilinear_Filter == sampler->getFilter())
1458 {
1459 // Need texture domain if drawing a sub rect.
1460 needsTextureDomain = srcRect.width() < bitmap.width() ||
1461 srcRect.height() < bitmap.height();
1462 if (m.rectStaysRect() && draw.fMatrix->rectStaysRect()) {
1463 // sampling is axis-aligned
1464 GrRect floatSrcRect, transformedRect;
1465 floatSrcRect.set(srcRect);
1466 SkMatrix srcToDeviceMatrix(m);
1467 srcToDeviceMatrix.postConcat(*draw.fMatrix);
1468 srcToDeviceMatrix.mapRect(&transformedRect, floatSrcRect);
1469
1470 if (hasAlignedSamples(floatSrcRect, transformedRect)) {
1471 // Samples are texel-aligned, so filtering is futile
1472 sampler->setFilter(GrSamplerState::kNearest_Filter);
1473 needsTextureDomain = false;
1474 } else {
1475 needsTextureDomain = needsTextureDomain &&
1476 mayColorBleed(floatSrcRect, transformedRect, m);
1477 }
1478 }
1479 }
1480
1481 GrRect textureDomain = GrRect::MakeEmpty();
1482
1483 if (needsTextureDomain) {
1484 // Use a constrained texture domain to avoid color bleeding
junov@google.com6acc9b32011-05-16 18:32:07 +00001485 GrScalar left, top, right, bottom;
1486 if (srcRect.width() > 1) {
1487 GrScalar border = GR_ScalarHalf / bitmap.width();
1488 left = paintRect.left() + border;
1489 right = paintRect.right() - border;
1490 } else {
1491 left = right = GrScalarHalf(paintRect.left() + paintRect.right());
1492 }
1493 if (srcRect.height() > 1) {
1494 GrScalar border = GR_ScalarHalf / bitmap.height();
1495 top = paintRect.top() + border;
1496 bottom = paintRect.bottom() - border;
1497 } else {
1498 top = bottom = GrScalarHalf(paintRect.top() + paintRect.bottom());
1499 }
junov@chromium.orgf32a9b62012-03-16 20:54:17 +00001500 textureDomain.setLTRB(left, top, right, bottom);
tomhudson@google.com2f68e762012-07-17 18:43:21 +00001501 sampler->setCustomStage(SkNEW_ARGS(GrTextureDomainEffect,
1502 (texture,
1503 textureDomain)))->unref();
junov@google.com6acc9b32011-05-16 18:32:07 +00001504 }
1505
bsalomon@google.comcc4dac32011-05-10 13:52:42 +00001506 fContext->drawRectToRect(*grPaint, dstRect, paintRect, &m);
reed@google.comac10a2d2010-12-22 21:39:39 +00001507}
1508
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001509namespace {
1510
1511void apply_custom_stage(GrContext* context,
1512 GrTexture* srcTexture,
1513 GrTexture* dstTexture,
1514 const GrRect& rect,
1515 GrCustomStage* stage) {
1516 SkASSERT(srcTexture && srcTexture->getContext() == context);
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001517 GrContext::AutoMatrix avm(context, GrMatrix::I());
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001518 GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget());
robertphillips@google.com56c79b12012-07-11 20:57:46 +00001519 GrContext::AutoClip acs(context, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001520
1521 GrMatrix sampleM;
1522 sampleM.setIDiv(srcTexture->width(), srcTexture->height());
1523 GrPaint paint;
1524 paint.reset();
1525 paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
1526 paint.textureSampler(0)->reset(sampleM);
1527 paint.textureSampler(0)->setCustomStage(stage);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001528 context->drawRect(paint, rect);
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001529}
1530
1531};
1532
reed@google.com8926b162012-03-23 15:36:36 +00001533static GrTexture* filter_texture(GrContext* context, GrTexture* texture,
1534 SkImageFilter* filter, const GrRect& rect) {
1535 GrAssert(filter);
1536
1537 SkSize blurSize;
1538 SkISize radius;
1539
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001540 GrTextureDesc desc;
1541 desc.fFlags = kRenderTarget_GrTextureFlagBit,
1542 desc.fWidth = SkScalarCeilToInt(rect.width());
1543 desc.fHeight = SkScalarCeilToInt(rect.height());
1544 desc.fConfig = kRGBA_8888_PM_GrPixelConfig;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001545 GrCustomStage* stage;
reed@google.com8926b162012-03-23 15:36:36 +00001546
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001547 if (filter->asNewCustomStage(&stage, texture)) {
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001548 GrAutoScratchTexture dst(context, desc);
1549 apply_custom_stage(context, texture, dst.texture(), rect, stage);
1550 texture = dst.detach();
1551 stage->unref();
1552 } else if (filter->asABlur(&blurSize)) {
senorblanco@chromium.org1e95d712012-07-18 19:52:53 +00001553 texture = context->gaussianBlur(texture, false, rect,
reed@google.com8926b162012-03-23 15:36:36 +00001554 blurSize.width(),
1555 blurSize.height());
reed@google.com8926b162012-03-23 15:36:36 +00001556 } else if (filter->asADilate(&radius)) {
reed@google.com8926b162012-03-23 15:36:36 +00001557 texture = context->applyMorphology(texture, rect,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001558 GrContext::kDilate_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001559 radius);
reed@google.com8926b162012-03-23 15:36:36 +00001560 } else if (filter->asAnErode(&radius)) {
reed@google.com8926b162012-03-23 15:36:36 +00001561 texture = context->applyMorphology(texture, rect,
bsalomon@google.comb505a122012-05-31 18:40:36 +00001562 GrContext::kErode_MorphologyType,
reed@google.com8926b162012-03-23 15:36:36 +00001563 radius);
reed@google.com8926b162012-03-23 15:36:36 +00001564 }
1565 return texture;
1566}
1567
reed@google.comac10a2d2010-12-22 21:39:39 +00001568void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1569 int left, int top, const SkPaint& paint) {
1570 CHECK_SHOULD_DRAW(draw);
1571
reed@google.com8926b162012-03-23 15:36:36 +00001572 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
reed@google.comac10a2d2010-12-22 21:39:39 +00001573 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1574 return;
1575 }
1576
reed@google.com76dd2772012-01-05 21:15:07 +00001577 int w = bitmap.width();
1578 int h = bitmap.height();
1579
bsalomon@google.com5782d712011-01-21 21:03:59 +00001580 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001581 SkAutoCachedTexture colorLutTexture;
1582 if(!skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001583 return;
1584 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001585
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001586 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.com5782d712011-01-21 21:03:59 +00001587
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +00001588 GrSamplerState* sampler = grPaint.textureSampler(kBitmapTextureIdx);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001589
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001590 GrTexture* texture;
bsalomon@google.com97912912011-12-06 16:30:36 +00001591 sampler->reset();
bsalomon@google.com1fadb202011-12-12 16:10:08 +00001592 SkAutoCachedTexture act(this, bitmap, sampler, &texture);
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001593 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1594 (GrSingleTextureEffect, (texture)))->unref();
bsalomon@google.com26c2d0a2011-05-17 20:15:30 +00001595
reed@google.com8926b162012-03-23 15:36:36 +00001596 SkImageFilter* filter = paint.getImageFilter();
1597 if (NULL != filter) {
1598 GrTexture* filteredTexture = filter_texture(fContext, texture, filter,
robertphillips@google.com8637a362012-04-10 18:32:35 +00001599 GrRect::MakeWH(SkIntToScalar(w), SkIntToScalar(h)));
reed@google.com8926b162012-03-23 15:36:36 +00001600 if (filteredTexture) {
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001601 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1602 (GrSingleTextureEffect, (filteredTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001603 texture = filteredTexture;
1604 filteredTexture->unref();
1605 }
reed@google.com76dd2772012-01-05 21:15:07 +00001606 }
reed@google.com8926b162012-03-23 15:36:36 +00001607
bsalomon@google.com5782d712011-01-21 21:03:59 +00001608 fContext->drawRectToRect(grPaint,
reed@google.com76dd2772012-01-05 21:15:07 +00001609 GrRect::MakeXYWH(GrIntToScalar(left),
1610 GrIntToScalar(top),
1611 GrIntToScalar(w),
1612 GrIntToScalar(h)),
1613 GrRect::MakeWH(GR_Scalar1 * w / texture->width(),
1614 GR_Scalar1 * h / texture->height()));
reed@google.comac10a2d2010-12-22 21:39:39 +00001615}
1616
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001617void SkGpuDevice::drawDevice(const SkDraw& draw, SkDevice* device,
reed@google.comac10a2d2010-12-22 21:39:39 +00001618 int x, int y, const SkPaint& paint) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001619 // clear of the source device must occur before CHECK_SHOULD_DRAW
1620 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1621 if (dev->fNeedClear) {
1622 // TODO: could check here whether we really need to draw at all
1623 dev->clear(0x0);
1624 }
1625
reed@google.comac10a2d2010-12-22 21:39:39 +00001626 CHECK_SHOULD_DRAW(draw);
1627
bsalomon@google.com5782d712011-01-21 21:03:59 +00001628 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001629 SkAutoCachedTexture colorLutTexture;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001630 if (!dev->bindDeviceAsTexture(&grPaint) ||
twiz@google.com58071162012-07-18 21:41:50 +00001631 !skPaint2GrPaintNoShader(this, paint, true, false, &colorLutTexture, &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001632 return;
reed@google.comac10a2d2010-12-22 21:39:39 +00001633 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001634
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001635 GrTexture* devTex = grPaint.getTexture(0);
1636 SkASSERT(NULL != devTex);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001637
reed@google.com8926b162012-03-23 15:36:36 +00001638 SkImageFilter* filter = paint.getImageFilter();
tomhudson@google.com2683a412012-07-20 19:15:06 +00001639 grPaint.textureSampler(kBitmapTextureIdx)->reset();
reed@google.com8926b162012-03-23 15:36:36 +00001640 if (NULL != filter) {
robertphillips@google.com8637a362012-04-10 18:32:35 +00001641 GrRect rect = GrRect::MakeWH(SkIntToScalar(devTex->width()),
1642 SkIntToScalar(devTex->height()));
reed@google.com8926b162012-03-23 15:36:36 +00001643 GrTexture* filteredTexture = filter_texture(fContext, devTex, filter,
1644 rect);
1645 if (filteredTexture) {
tomhudson@google.com1e8f0162012-07-20 16:25:18 +00001646 grPaint.textureSampler(kBitmapTextureIdx)->setCustomStage(SkNEW_ARGS
1647 (GrSingleTextureEffect, (filteredTexture)))->unref();
tomhudson@google.com2683a412012-07-20 19:15:06 +00001648 grPaint.setTexture(kBitmapTextureIdx, NULL);
reed@google.com8926b162012-03-23 15:36:36 +00001649 devTex = filteredTexture;
1650 filteredTexture->unref();
1651 }
1652 }
1653
bsalomon@google.com5782d712011-01-21 21:03:59 +00001654 const SkBitmap& bm = dev->accessBitmap(false);
1655 int w = bm.width();
1656 int h = bm.height();
1657
robertphillips@google.comfea85ac2012-07-11 18:53:23 +00001658 GrContext::AutoMatrix avm(fContext, GrMatrix::I());
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001659 GrRect dstRect = GrRect::MakeXYWH(GrIntToScalar(x),
1660 GrIntToScalar(y),
1661 GrIntToScalar(w),
1662 GrIntToScalar(h));
reed@google.com76dd2772012-01-05 21:15:07 +00001663
bsalomon@google.comb5b31682011-06-16 18:05:35 +00001664 // The device being drawn may not fill up its texture (saveLayer uses
1665 // the approximate ).
1666 GrRect srcRect = GrRect::MakeWH(GR_Scalar1 * w / devTex->width(),
1667 GR_Scalar1 * h / devTex->height());
1668
1669 fContext->drawRectToRect(grPaint, dstRect, srcRect);
reed@google.comac10a2d2010-12-22 21:39:39 +00001670}
1671
reed@google.com8926b162012-03-23 15:36:36 +00001672bool SkGpuDevice::canHandleImageFilter(SkImageFilter* filter) {
reed@google.com76dd2772012-01-05 21:15:07 +00001673 SkSize size;
senorblanco@chromium.org05054f12012-03-02 21:05:45 +00001674 SkISize radius;
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001675
tomhudson@google.comd0c1a062012-07-12 17:23:52 +00001676 if (!filter->asNewCustomStage(NULL, NULL) &&
senorblanco@chromium.org894790d2012-07-11 16:01:22 +00001677 !filter->asABlur(&size) &&
1678 !filter->asADilate(&radius) &&
1679 !filter->asAnErode(&radius)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001680 return false;
1681 }
reed@google.com8926b162012-03-23 15:36:36 +00001682 return true;
1683}
1684
1685bool SkGpuDevice::filterImage(SkImageFilter* filter, const SkBitmap& src,
1686 const SkMatrix& ctm,
1687 SkBitmap* result, SkIPoint* offset) {
1688 // want explicitly our impl, so guard against a subclass of us overriding it
1689 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
reed@google.com76dd2772012-01-05 21:15:07 +00001690 return false;
1691 }
reed@google.com8926b162012-03-23 15:36:36 +00001692
1693 SkAutoLockPixels alp(src, !src.getTexture());
1694 if (!src.getTexture() && !src.readyToDraw()) {
1695 return false;
1696 }
1697
1698 GrPaint paint;
1699 paint.reset();
1700
1701 GrSamplerState* sampler = paint.textureSampler(kBitmapTextureIdx);
1702
1703 GrTexture* texture;
1704 SkAutoCachedTexture act(this, src, sampler, &texture);
1705
1706 result->setConfig(src.config(), src.width(), src.height());
robertphillips@google.com8637a362012-04-10 18:32:35 +00001707 GrRect rect = GrRect::MakeWH(SkIntToScalar(src.width()),
1708 SkIntToScalar(src.height()));
reed@google.com8926b162012-03-23 15:36:36 +00001709 GrTexture* resultTexture = filter_texture(fContext, texture, filter, rect);
1710 if (resultTexture) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001711 result->setPixelRef(SkNEW_ARGS(SkGrTexturePixelRef,
1712 (resultTexture)))->unref();
reed@google.com8926b162012-03-23 15:36:36 +00001713 resultTexture->unref();
1714 }
reed@google.com76dd2772012-01-05 21:15:07 +00001715 return true;
1716}
1717
reed@google.comac10a2d2010-12-22 21:39:39 +00001718///////////////////////////////////////////////////////////////////////////////
1719
1720// must be in SkCanvas::VertexMode order
bsalomon@google.comffca4002011-02-22 20:34:01 +00001721static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
bsalomon@google.com47059542012-06-06 20:51:20 +00001722 kTriangles_GrPrimitiveType,
1723 kTriangleStrip_GrPrimitiveType,
1724 kTriangleFan_GrPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +00001725};
1726
1727void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1728 int vertexCount, const SkPoint vertices[],
1729 const SkPoint texs[], const SkColor colors[],
1730 SkXfermode* xmode,
1731 const uint16_t indices[], int indexCount,
1732 const SkPaint& paint) {
1733 CHECK_SHOULD_DRAW(draw);
1734
bsalomon@google.com5782d712011-01-21 21:03:59 +00001735 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001736 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com5782d712011-01-21 21:03:59 +00001737 // we ignore the shader if texs is null.
1738 if (NULL == texs) {
twiz@google.com58071162012-07-18 21:41:50 +00001739 if (!skPaint2GrPaintNoShader(this,
1740 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001741 false,
1742 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001743 &textures[kColorFilterTextureIdx],
bsalomon@google.com84405e02012-03-05 19:57:21 +00001744 &grPaint)) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001745 return;
1746 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001747 } else {
bsalomon@google.com84405e02012-03-05 19:57:21 +00001748 if (!skPaint2GrPaintShader(this,
1749 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001750 NULL == colors,
twiz@google.com58071162012-07-18 21:41:50 +00001751 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001752 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001753 return;
1754 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001755 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001756
1757 if (NULL != xmode && NULL != texs && NULL != colors) {
mike@reedtribe.orgbe2aa2a2011-11-17 02:32:04 +00001758 if (!SkXfermode::IsMode(xmode, SkXfermode::kMultiply_Mode)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001759 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1760#if 0
1761 return
1762#endif
1763 }
reed@google.comac10a2d2010-12-22 21:39:39 +00001764 }
bsalomon@google.com5782d712011-01-21 21:03:59 +00001765
bsalomon@google.com498776a2011-08-16 19:20:44 +00001766 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1767 if (NULL != colors) {
1768 // need to convert byte order and from non-PM to PM
bsalomon@google.com7d4679a2011-09-02 22:06:24 +00001769 convertedColors.reset(vertexCount);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001770 for (int i = 0; i < vertexCount; ++i) {
rileya@google.com24f3ad12012-07-18 21:47:40 +00001771 convertedColors[i] = SkColor2GrColor(colors[i]);
bsalomon@google.com498776a2011-08-16 19:20:44 +00001772 }
1773 colors = convertedColors.get();
reed@google.comac10a2d2010-12-22 21:39:39 +00001774 }
bsalomon@google.com498776a2011-08-16 19:20:44 +00001775 fContext->drawVertices(grPaint,
1776 gVertexMode2PrimitiveType[vmode],
1777 vertexCount,
1778 (GrPoint*) vertices,
1779 (GrPoint*) texs,
1780 colors,
1781 indices,
1782 indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +00001783}
1784
1785///////////////////////////////////////////////////////////////////////////////
1786
1787static void GlyphCacheAuxProc(void* data) {
reed@google.com26344cf2012-06-27 18:23:01 +00001788 GrFontScaler* scaler = (GrFontScaler*)data;
1789 SkSafeUnref(scaler);
reed@google.comac10a2d2010-12-22 21:39:39 +00001790}
1791
1792static GrFontScaler* get_gr_font_scaler(SkGlyphCache* cache) {
1793 void* auxData;
1794 GrFontScaler* scaler = NULL;
1795 if (cache->getAuxProcData(GlyphCacheAuxProc, &auxData)) {
1796 scaler = (GrFontScaler*)auxData;
1797 }
1798 if (NULL == scaler) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001799 scaler = SkNEW_ARGS(SkGrFontScaler, (cache));
reed@google.comac10a2d2010-12-22 21:39:39 +00001800 cache->setAuxProc(GlyphCacheAuxProc, scaler);
1801 }
1802 return scaler;
1803}
1804
1805static void SkGPU_Draw1Glyph(const SkDraw1Glyph& state,
1806 SkFixed fx, SkFixed fy,
1807 const SkGlyph& glyph) {
1808 SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0);
1809
bungeman@google.com15865a72012-01-11 16:28:04 +00001810 GrSkDrawProcs* procs = static_cast<GrSkDrawProcs*>(state.fDraw->fProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001811
1812 if (NULL == procs->fFontScaler) {
1813 procs->fFontScaler = get_gr_font_scaler(state.fCache);
1814 }
reed@google.com39ce0ac2011-04-08 15:42:19 +00001815
bungeman@google.com15865a72012-01-11 16:28:04 +00001816 procs->fTextContext->drawPackedGlyph(GrGlyph::Pack(glyph.getGlyphID(),
1817 glyph.getSubXFixed(),
1818 glyph.getSubYFixed()),
1819 SkFixedFloorToFixed(fx),
1820 SkFixedFloorToFixed(fy),
reed@google.comac10a2d2010-12-22 21:39:39 +00001821 procs->fFontScaler);
1822}
1823
bsalomon@google.com5782d712011-01-21 21:03:59 +00001824SkDrawProcs* SkGpuDevice::initDrawForText(GrTextContext* context) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001825
1826 // deferred allocation
1827 if (NULL == fDrawProcs) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +00001828 fDrawProcs = SkNEW(GrSkDrawProcs);
reed@google.comac10a2d2010-12-22 21:39:39 +00001829 fDrawProcs->fD1GProc = SkGPU_Draw1Glyph;
1830 fDrawProcs->fContext = fContext;
1831 }
1832
1833 // init our (and GL's) state
1834 fDrawProcs->fTextContext = context;
1835 fDrawProcs->fFontScaler = NULL;
1836 return fDrawProcs;
1837}
1838
1839void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1840 size_t byteLength, SkScalar x, SkScalar y,
1841 const SkPaint& paint) {
1842 CHECK_SHOULD_DRAW(draw);
1843
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001844 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001845 // this guy will just call our drawPath()
1846 draw.drawText((const char*)text, byteLength, x, y, paint);
1847 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001848 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001849
1850 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001851 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001852 if (!skPaint2GrPaintShader(this,
1853 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001854 true,
twiz@google.com58071162012-07-18 21:41:50 +00001855 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001856 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001857 return;
1858 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001859 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1860 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001861 this->INHERITED::drawText(myDraw, text, byteLength, x, y, paint);
1862 }
1863}
1864
1865void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1866 size_t byteLength, const SkScalar pos[],
1867 SkScalar constY, int scalarsPerPos,
1868 const SkPaint& paint) {
1869 CHECK_SHOULD_DRAW(draw);
1870
tomhudson@google.comdd5f7442011-08-30 15:13:55 +00001871 if (draw.fMatrix->hasPerspective()) {
reed@google.comac10a2d2010-12-22 21:39:39 +00001872 // this guy will just call our drawPath()
1873 draw.drawPosText((const char*)text, byteLength, pos, constY,
1874 scalarsPerPos, paint);
1875 } else {
reed@google.comac10a2d2010-12-22 21:39:39 +00001876 SkDraw myDraw(draw);
bsalomon@google.com5782d712011-01-21 21:03:59 +00001877
1878 GrPaint grPaint;
twiz@google.com58071162012-07-18 21:41:50 +00001879 SkAutoCachedTexture textures[GrPaint::kMaxTextures];
bsalomon@google.com84405e02012-03-05 19:57:21 +00001880 if (!skPaint2GrPaintShader(this,
1881 paint,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001882 true,
twiz@google.com58071162012-07-18 21:41:50 +00001883 textures,
bsalomon@google.com84405e02012-03-05 19:57:21 +00001884 &grPaint)) {
bsalomon@google.com5782d712011-01-21 21:03:59 +00001885 return;
1886 }
tomhudson@google.com375ff852012-06-29 18:37:57 +00001887 GrTextContext context(fContext, grPaint, draw.fExtMatrix);
1888 myDraw.fProcs = this->initDrawForText(&context);
reed@google.comac10a2d2010-12-22 21:39:39 +00001889 this->INHERITED::drawPosText(myDraw, text, byteLength, pos, constY,
1890 scalarsPerPos, paint);
1891 }
1892}
1893
1894void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1895 size_t len, const SkPath& path,
1896 const SkMatrix* m, const SkPaint& paint) {
1897 CHECK_SHOULD_DRAW(draw);
1898
1899 SkASSERT(draw.fDevice == this);
1900 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1901}
1902
1903///////////////////////////////////////////////////////////////////////////////
1904
reed@google.comf67e4cf2011-03-15 20:56:58 +00001905bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1906 if (!paint.isLCDRenderText()) {
1907 // we're cool with the paint as is
1908 return false;
1909 }
1910
1911 if (paint.getShader() ||
1912 paint.getXfermode() || // unless its srcover
1913 paint.getMaskFilter() ||
1914 paint.getRasterizer() ||
1915 paint.getColorFilter() ||
1916 paint.getPathEffect() ||
1917 paint.isFakeBoldText() ||
1918 paint.getStyle() != SkPaint::kFill_Style) {
1919 // turn off lcd
1920 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1921 flags->fHinting = paint.getHinting();
1922 return true;
1923 }
1924 // we're cool with the paint as is
1925 return false;
1926}
1927
reed@google.com75d939b2011-12-07 15:07:23 +00001928void SkGpuDevice::flush() {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001929 DO_DEFERRED_CLEAR;
bsalomon@google.com75f9f252012-01-31 13:35:56 +00001930 fContext->resolveRenderTarget(fRenderTarget);
reed@google.com75d939b2011-12-07 15:07:23 +00001931}
1932
reed@google.comf67e4cf2011-03-15 20:56:58 +00001933///////////////////////////////////////////////////////////////////////////////
1934
bsalomon@google.comfb309512011-11-30 14:13:48 +00001935bool SkGpuDevice::isBitmapInTextureCache(const SkBitmap& bitmap,
1936 const GrSamplerState& sampler) const {
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001937 uint64_t key = bitmap.getGenerationID();
bsalomon@google.comfb309512011-11-30 14:13:48 +00001938 key |= ((uint64_t) bitmap.pixelRefOffset()) << 32;
bsalomon@google.comfb309512011-11-30 14:13:48 +00001939
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001940 GrTextureDesc desc;
1941 desc.fWidth = bitmap.width();
1942 desc.fHeight = bitmap.height();
rileya@google.com24f3ad12012-07-18 21:47:40 +00001943 desc.fConfig = SkBitmapConfig2GrPixelConfig(bitmap.config());
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001944 desc.fClientCacheID = key;
robertphillips@google.coma1e57952012-06-04 20:05:28 +00001945
robertphillips@google.com75b3c962012-06-07 12:08:45 +00001946 return this->context()->isTextureInCache(desc, &sampler);
bsalomon@google.comfb309512011-11-30 14:13:48 +00001947}
1948
1949
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001950SkDevice* SkGpuDevice::onCreateCompatibleDevice(SkBitmap::Config config,
1951 int width, int height,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001952 bool isOpaque,
1953 Usage usage) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001954 GrTextureDesc desc;
1955 desc.fConfig = fRenderTarget->config();
1956 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1957 desc.fWidth = width;
1958 desc.fHeight = height;
1959 desc.fSampleCnt = fRenderTarget->numSamples();
1960
1961 GrContext::TextureCacheEntry cacheEntry;
1962 GrTexture* texture;
1963 SkAutoTUnref<GrTexture> tunref;
bsalomon@google.com1b3ac8b2012-04-09 21:40:54 +00001964 // Skia's convention is to only clear a device if it is non-opaque.
1965 bool needClear = !isOpaque;
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001966
1967#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1968 // layers are never draw in repeat modes, so we can request an approx
1969 // match and ignore any padding.
1970 GrContext::ScratchTexMatch matchType = (kSaveLayer_Usage == usage) ?
1971 GrContext::kApprox_ScratchTexMatch :
1972 GrContext::kExact_ScratchTexMatch;
1973 cacheEntry = fContext->lockScratchTexture(desc, matchType);
1974 texture = cacheEntry.texture();
1975#else
1976 tunref.reset(fContext->createUncachedTexture(desc, NULL, 0));
1977 texture = tunref.get();
1978#endif
1979 if (texture) {
1980 return SkNEW_ARGS(SkGpuDevice,(fContext,
1981 texture,
1982 cacheEntry,
1983 needClear));
1984 } else {
1985 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1986 width, height);
1987 return NULL;
1988 }
1989}
1990
1991SkGpuDevice::SkGpuDevice(GrContext* context,
1992 GrTexture* texture,
1993 TexCache cacheEntry,
1994 bool needClear)
robertphillips@google.com40a1ae42012-07-13 15:36:15 +00001995 : SkDevice(make_bitmap(context, texture->asRenderTarget()))
1996 , fClipStack(NULL) {
bsalomon@google.com06cd7322012-03-30 18:45:35 +00001997 GrAssert(texture && texture->asRenderTarget());
1998 GrAssert(NULL == cacheEntry.texture() || texture == cacheEntry.texture());
1999 this->initFromRenderTarget(context, texture->asRenderTarget());
2000 fCache = cacheEntry;
2001 fNeedClear = needClear;
bsalomon@google.come97f0852011-06-17 13:10:25 +00002002}
2003