blob: 48ae845e3bb36afcc8f5d520c34d0e4bae717d1d [file] [log] [blame]
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001/*
2 * 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.
6 */
7
8#include "SkGpuDevice.h"
9
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +000010#include "effects/GrBicubicEffect.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000011#include "effects/GrDashingEffect.h"
commit-bot@chromium.org907fbd52013-12-09 17:03:02 +000012#include "effects/GrTextureDomain.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000013#include "effects/GrSimpleTextureEffect.h"
14
15#include "GrContext.h"
16#include "GrBitmapTextContext.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000017#include "GrDistanceFieldTextContext.h"
robertphillips@google.come930a072014-04-03 00:34:27 +000018#include "GrLayerCache.h"
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +000019#include "GrPictureUtils.h"
egdanield58a0ba2014-06-11 10:30:05 -070020#include "GrStrokeInfo.h"
egdanielbbcb38d2014-06-19 10:19:29 -070021#include "GrTracing.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000022
23#include "SkGrTexturePixelRef.h"
24
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000025#include "SkDeviceImageFilterProxy.h"
26#include "SkDrawProcs.h"
27#include "SkGlyphCache.h"
28#include "SkImageFilter.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000029#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000030#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000031#include "SkPicture.h"
robertphillipsdb539902014-07-01 08:47:04 -070032#include "SkPictureData.h"
robertphillips1ad00e42014-07-08 08:28:08 -070033#include "SkPictureRangePlayback.h"
robertphillipsc26d9912014-07-10 07:21:27 -070034#include "SkPictureReplacementPlayback.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000035#include "SkRRect.h"
36#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000037#include "SkSurface.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000038#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000039#include "SkUtils.h"
commit-bot@chromium.org559a8832014-05-30 10:08:22 +000040#include "SkVertState.h"
robertphillips320c9232014-07-29 06:07:19 -070041#include "SkXfermode.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000042#include "SkErrorInternals.h"
43
senorblanco55b6d8b2014-07-30 11:26:46 -070044enum { kDefaultImageFilterCacheSize = 32 * 1024 * 1024 };
45
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000046#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
47
48#if 0
49 extern bool (*gShouldDrawProc)();
50 #define CHECK_SHOULD_DRAW(draw, forceI) \
51 do { \
52 if (gShouldDrawProc && !gShouldDrawProc()) return; \
53 this->prepareDraw(draw, forceI); \
54 } while (0)
55#else
56 #define CHECK_SHOULD_DRAW(draw, forceI) this->prepareDraw(draw, forceI)
57#endif
58
59// This constant represents the screen alignment criterion in texels for
60// requiring texture domain clamping to prevent color bleeding when drawing
61// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000062#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000063
64#define DO_DEFERRED_CLEAR() \
65 do { \
66 if (fNeedClear) { \
67 this->clear(SK_ColorTRANSPARENT); \
68 } \
69 } while (false) \
70
71///////////////////////////////////////////////////////////////////////////////
72
73#define CHECK_FOR_ANNOTATION(paint) \
74 do { if (paint.getAnnotation()) { return; } } while (0)
75
76///////////////////////////////////////////////////////////////////////////////
77
78
79class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
80public:
81 SkAutoCachedTexture()
82 : fDevice(NULL)
83 , fTexture(NULL) {
84 }
85
86 SkAutoCachedTexture(SkGpuDevice* device,
87 const SkBitmap& bitmap,
88 const GrTextureParams* params,
89 GrTexture** texture)
90 : fDevice(NULL)
91 , fTexture(NULL) {
92 SkASSERT(NULL != texture);
93 *texture = this->set(device, bitmap, params);
94 }
95
96 ~SkAutoCachedTexture() {
97 if (NULL != fTexture) {
98 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
99 }
100 }
101
102 GrTexture* set(SkGpuDevice* device,
103 const SkBitmap& bitmap,
104 const GrTextureParams* params) {
105 if (NULL != fTexture) {
106 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
107 fTexture = NULL;
108 }
109 fDevice = device;
110 GrTexture* result = (GrTexture*)bitmap.getTexture();
111 if (NULL == result) {
112 // Cannot return the native texture so look it up in our cache
113 fTexture = GrLockAndRefCachedBitmapTexture(device->context(), bitmap, params);
114 result = fTexture;
115 }
116 return result;
117 }
118
119private:
120 SkGpuDevice* fDevice;
121 GrTexture* fTexture;
122};
123
124///////////////////////////////////////////////////////////////////////////////
125
126struct GrSkDrawProcs : public SkDrawProcs {
127public:
128 GrContext* fContext;
129 GrTextContext* fTextContext;
130 GrFontScaler* fFontScaler; // cached in the skia glyphcache
131};
132
133///////////////////////////////////////////////////////////////////////////////
134
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000135SkGpuDevice* SkGpuDevice::Create(GrSurface* surface, unsigned flags) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000136 SkASSERT(NULL != surface);
137 if (NULL == surface->asRenderTarget() || NULL == surface->getContext()) {
138 return NULL;
139 }
140 if (surface->asTexture()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000141 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asTexture(), flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000142 } else {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000143 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asRenderTarget(), flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000144 }
145}
146
reed89443ab2014-06-27 11:34:19 -0700147SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture, unsigned flags) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000148 this->initFromRenderTarget(context, texture->asRenderTarget(), flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000149}
150
reed89443ab2014-06-27 11:34:19 -0700151SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget, unsigned flags) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000152 this->initFromRenderTarget(context, renderTarget, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000153}
154
155void SkGpuDevice::initFromRenderTarget(GrContext* context,
156 GrRenderTarget* renderTarget,
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000157 unsigned flags) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000158 fDrawProcs = NULL;
159
160 fContext = context;
161 fContext->ref();
162
163 fRenderTarget = NULL;
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000164 fNeedClear = flags & kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000165
166 SkASSERT(NULL != renderTarget);
167 fRenderTarget = renderTarget;
168 fRenderTarget->ref();
169
170 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
171 // on the RT but not vice-versa.
172 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
173 // busting chrome (for a currently unknown reason).
174 GrSurface* surface = fRenderTarget->asTexture();
175 if (NULL == surface) {
176 surface = fRenderTarget;
177 }
reed@google.combf790232013-12-13 19:45:58 +0000178
reed6c225732014-06-09 19:52:07 -0700179 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef,
180 (surface->info(), surface, SkToBool(flags & kCached_Flag)));
reed89443ab2014-06-27 11:34:19 -0700181 fLegacyBitmap.setInfo(surface->info());
182 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700183
184 bool useDFFonts = !!(flags & kDFFonts_Flag);
185 fMainTextContext = fContext->createTextContext(fRenderTarget, fLeakyProperties, useDFFonts);
186 fFallbackTextContext = SkNEW_ARGS(GrBitmapTextContext, (fContext, fLeakyProperties));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000187}
188
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000189SkGpuDevice* SkGpuDevice::Create(GrContext* context, const SkImageInfo& origInfo,
190 int sampleCount) {
191 if (kUnknown_SkColorType == origInfo.colorType() ||
192 origInfo.width() < 0 || origInfo.height() < 0) {
193 return NULL;
194 }
195
196 SkImageInfo info = origInfo;
197 // TODO: perhas we can loosen this check now that colortype is more detailed
198 // e.g. can we support both RGBA and BGRA here?
199 if (kRGB_565_SkColorType == info.colorType()) {
200 info.fAlphaType = kOpaque_SkAlphaType; // force this setting
201 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000202 info.fColorType = kN32_SkColorType;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000203 if (kOpaque_SkAlphaType != info.alphaType()) {
204 info.fAlphaType = kPremul_SkAlphaType; // force this setting
205 }
206 }
207
208 GrTextureDesc desc;
209 desc.fFlags = kRenderTarget_GrTextureFlagBit;
210 desc.fWidth = info.width();
211 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000212 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000213 desc.fSampleCnt = sampleCount;
214
215 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
216 if (!texture.get()) {
217 return NULL;
218 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000219
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000220 return SkNEW_ARGS(SkGpuDevice, (context, texture.get()));
221}
222
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000223SkGpuDevice::~SkGpuDevice() {
224 if (fDrawProcs) {
225 delete fDrawProcs;
226 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000227
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000228 delete fMainTextContext;
229 delete fFallbackTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000230
231 // The GrContext takes a ref on the target. We don't want to cause the render
232 // target to be unnecessarily kept alive.
233 if (fContext->getRenderTarget() == fRenderTarget) {
234 fContext->setRenderTarget(NULL);
235 }
236
237 if (fContext->getClip() == &fClipData) {
238 fContext->setClip(NULL);
239 }
240
241 SkSafeUnref(fRenderTarget);
242 fContext->unref();
243}
244
245///////////////////////////////////////////////////////////////////////////////
246
247void SkGpuDevice::makeRenderTargetCurrent() {
248 DO_DEFERRED_CLEAR();
249 fContext->setRenderTarget(fRenderTarget);
250}
251
252///////////////////////////////////////////////////////////////////////////////
253
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000254bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
255 int x, int y) {
256 DO_DEFERRED_CLEAR();
257
258 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000259 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000260 if (kUnknown_GrPixelConfig == config) {
261 return false;
262 }
263
264 uint32_t flags = 0;
265 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
266 flags = GrContext::kUnpremul_PixelOpsFlag;
267 }
268 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
269 config, dstPixels, dstRowBytes, flags);
270}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000271
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000272bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
273 int x, int y) {
274 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000275 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000276 if (kUnknown_GrPixelConfig == config) {
277 return false;
278 }
279 uint32_t flags = 0;
280 if (kUnpremul_SkAlphaType == info.alphaType()) {
281 flags = GrContext::kUnpremul_PixelOpsFlag;
282 }
283 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
284
285 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700286 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000287
288 return true;
289}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000290
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000291const SkBitmap& SkGpuDevice::onAccessBitmap() {
292 DO_DEFERRED_CLEAR();
reed89443ab2014-06-27 11:34:19 -0700293 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000294}
295
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000296void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
297 INHERITED::onAttachToCanvas(canvas);
298
299 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
300 fClipData.fClipStack = canvas->getClipStack();
301}
302
303void SkGpuDevice::onDetachFromCanvas() {
304 INHERITED::onDetachFromCanvas();
305 fClipData.fClipStack = NULL;
306}
307
308// call this every draw call, to ensure that the context reflects our state,
309// and not the state from some other canvas/device
310void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
311 SkASSERT(NULL != fClipData.fClipStack);
312
313 fContext->setRenderTarget(fRenderTarget);
314
315 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
316
317 if (forceIdentity) {
318 fContext->setIdentityMatrix();
319 } else {
320 fContext->setMatrix(*draw.fMatrix);
321 }
322 fClipData.fOrigin = this->getOrigin();
323
324 fContext->setClip(&fClipData);
325
326 DO_DEFERRED_CLEAR();
327}
328
329GrRenderTarget* SkGpuDevice::accessRenderTarget() {
330 DO_DEFERRED_CLEAR();
331 return fRenderTarget;
332}
333
334///////////////////////////////////////////////////////////////////////////////
335
336SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
337SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
338SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
339SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
340SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
341 shader_type_mismatch);
342SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
343 shader_type_mismatch);
344SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
345SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
346
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000347///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000348
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000349void SkGpuDevice::clear(SkColor color) {
egdanield78a1682014-07-09 10:41:26 -0700350 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::clear", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000351 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
352 fContext->clear(&rect, SkColor2GrColor(color), true, fRenderTarget);
353 fNeedClear = false;
354}
355
356void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
357 CHECK_SHOULD_DRAW(draw, false);
egdanield78a1682014-07-09 10:41:26 -0700358 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000359
360 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000361 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000362
363 fContext->drawPaint(grPaint);
364}
365
366// must be in SkCanvas::PointMode order
367static const GrPrimitiveType gPointMode2PrimtiveType[] = {
368 kPoints_GrPrimitiveType,
369 kLines_GrPrimitiveType,
370 kLineStrip_GrPrimitiveType
371};
372
373void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
374 size_t count, const SkPoint pts[], const SkPaint& paint) {
375 CHECK_FOR_ANNOTATION(paint);
376 CHECK_SHOULD_DRAW(draw, false);
377
378 SkScalar width = paint.getStrokeWidth();
379 if (width < 0) {
380 return;
381 }
382
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000383 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700384 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
385 GrPaint grPaint;
386 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
387 SkPath path;
388 path.moveTo(pts[0]);
389 path.lineTo(pts[1]);
390 fContext->drawPath(grPaint, path, strokeInfo);
391 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000392 }
393
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000394 // we only handle hairlines and paints without path effects or mask filters,
395 // else we let the SkDraw call our drawPath()
396 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
397 draw.drawPoints(mode, count, pts, paint, true);
398 return;
399 }
400
401 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000402 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000403
404 fContext->drawVertices(grPaint,
405 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000406 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000407 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000408 NULL,
409 NULL,
410 NULL,
411 0);
412}
413
414///////////////////////////////////////////////////////////////////////////////
415
416void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
417 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -0700418 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext);
419
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000420 CHECK_FOR_ANNOTATION(paint);
421 CHECK_SHOULD_DRAW(draw, false);
422
423 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
424 SkScalar width = paint.getStrokeWidth();
425
426 /*
427 We have special code for hairline strokes, miter-strokes, bevel-stroke
428 and fills. Anything else we just call our path code.
429 */
430 bool usePath = doStroke && width > 0 &&
431 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
432 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
433 // another two reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700434
435 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000436 usePath = true;
437 }
egdanield58a0ba2014-06-11 10:30:05 -0700438
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000439 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
440#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
441 if (doStroke) {
442#endif
443 usePath = true;
444#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
445 } else {
446 usePath = !fContext->getMatrix().preservesRightAngles();
447 }
448#endif
449 }
450 // until we can both stroke and fill rectangles
451 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
452 usePath = true;
453 }
454
egdanield58a0ba2014-06-11 10:30:05 -0700455 GrStrokeInfo strokeInfo(paint);
456
457 const SkPathEffect* pe = paint.getPathEffect();
458 if (!usePath && NULL != pe && !strokeInfo.isDashed()) {
459 usePath = true;
460 }
461
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000462 if (usePath) {
463 SkPath path;
464 path.addRect(rect);
465 this->drawPath(draw, path, paint, NULL, true);
466 return;
467 }
468
469 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000470 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400471
egdanield58a0ba2014-06-11 10:30:05 -0700472 fContext->drawRect(grPaint, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000473}
474
475///////////////////////////////////////////////////////////////////////////////
476
477void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
478 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700479 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000480 CHECK_FOR_ANNOTATION(paint);
481 CHECK_SHOULD_DRAW(draw, false);
482
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000483 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000484 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400485
egdanield58a0ba2014-06-11 10:30:05 -0700486 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000487 if (paint.getMaskFilter()) {
488 // try to hit the fast path for drawing filtered round rects
489
490 SkRRect devRRect;
491 if (rect.transform(fContext->getMatrix(), &devRRect)) {
492 if (devRRect.allCornersCircular()) {
493 SkRect maskRect;
494 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
495 draw.fClip->getBounds(),
496 fContext->getMatrix(),
497 &maskRect)) {
498 SkIRect finalIRect;
499 maskRect.roundOut(&finalIRect);
500 if (draw.fClip->quickReject(finalIRect)) {
501 // clipped out
502 return;
503 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000504 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, &grPaint,
egdanield58a0ba2014-06-11 10:30:05 -0700505 strokeInfo.getStrokeRec(),
506 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000507 return;
508 }
509 }
510
511 }
512 }
513
514 }
515
egdanield58a0ba2014-06-11 10:30:05 -0700516 bool usePath = false;
517
518 if (paint.getMaskFilter()) {
519 usePath = true;
520 } else {
521 const SkPathEffect* pe = paint.getPathEffect();
522 if (NULL != pe && !strokeInfo.isDashed()) {
523 usePath = true;
524 }
525 }
526
527
528 if (usePath) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000529 SkPath path;
530 path.addRRect(rect);
531 this->drawPath(draw, path, paint, NULL, true);
532 return;
533 }
Mike Klein744fb732014-06-23 15:13:26 -0400534
egdanield58a0ba2014-06-11 10:30:05 -0700535 fContext->drawRRect(grPaint, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000536}
537
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000538void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
539 const SkRRect& inner, const SkPaint& paint) {
540 SkStrokeRec stroke(paint);
541 if (stroke.isFillStyle()) {
542
543 CHECK_FOR_ANNOTATION(paint);
544 CHECK_SHOULD_DRAW(draw, false);
545
546 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000547 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000548
549 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
550 fContext->drawDRRect(grPaint, outer, inner);
551 return;
552 }
553 }
554
555 SkPath path;
556 path.addRRect(outer);
557 path.addRRect(inner);
558 path.setFillType(SkPath::kEvenOdd_FillType);
559
560 this->drawPath(draw, path, paint, NULL, true);
561}
562
563
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000564/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000565
566void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
567 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700568 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawOval", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000569 CHECK_FOR_ANNOTATION(paint);
570 CHECK_SHOULD_DRAW(draw, false);
571
egdanield58a0ba2014-06-11 10:30:05 -0700572 GrStrokeInfo strokeInfo(paint);
573
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000574 bool usePath = false;
575 // some basic reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700576 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000577 usePath = true;
egdanield58a0ba2014-06-11 10:30:05 -0700578 } else {
579 const SkPathEffect* pe = paint.getPathEffect();
580 if (NULL != pe && !strokeInfo.isDashed()) {
581 usePath = true;
582 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000583 }
584
585 if (usePath) {
586 SkPath path;
587 path.addOval(oval);
588 this->drawPath(draw, path, paint, NULL, true);
589 return;
590 }
591
592 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000593 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000594
egdanield58a0ba2014-06-11 10:30:05 -0700595 fContext->drawOval(grPaint, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000596}
597
598#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000599
600///////////////////////////////////////////////////////////////////////////////
601
602// helpers for applying mask filters
603namespace {
604
605// Draw a mask using the supplied paint. Since the coverage/geometry
606// is already burnt into the mask this boils down to a rect draw.
607// Return true if the mask was successfully drawn.
608bool draw_mask(GrContext* context, const SkRect& maskRect,
609 GrPaint* grp, GrTexture* mask) {
610 GrContext::AutoMatrix am;
611 if (!am.setIdentity(context, grp)) {
612 return false;
613 }
614
615 SkMatrix matrix;
616 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
617 matrix.postIDiv(mask->width(), mask->height());
618
619 grp->addCoverageEffect(GrSimpleTextureEffect::Create(mask, matrix))->unref();
620 context->drawRect(*grp, maskRect);
621 return true;
622}
623
624bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
reed868074b2014-06-03 10:53:59 -0700625 SkMaskFilter* filter, const SkRegion& clip,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000626 GrPaint* grp, SkPaint::Style style) {
627 SkMask srcM, dstM;
628
629 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
630 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
631 return false;
632 }
633 SkAutoMaskFreeImage autoSrc(srcM.fImage);
634
635 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
636 return false;
637 }
638 // this will free-up dstM when we're done (allocated in filterMask())
639 SkAutoMaskFreeImage autoDst(dstM.fImage);
640
641 if (clip.quickReject(dstM.fBounds)) {
642 return false;
643 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000644
645 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
646 // the current clip (and identity matrix) and GrPaint settings
647 GrTextureDesc desc;
648 desc.fWidth = dstM.fBounds.width();
649 desc.fHeight = dstM.fBounds.height();
650 desc.fConfig = kAlpha_8_GrPixelConfig;
651
652 GrAutoScratchTexture ast(context, desc);
653 GrTexture* texture = ast.texture();
654
655 if (NULL == texture) {
656 return false;
657 }
658 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
659 dstM.fImage, dstM.fRowBytes);
660
661 SkRect maskRect = SkRect::Make(dstM.fBounds);
662
663 return draw_mask(context, maskRect, grp, texture);
664}
665
666// Create a mask of 'devPath' and place the result in 'mask'. Return true on
667// success; false otherwise.
668bool create_mask_GPU(GrContext* context,
669 const SkRect& maskRect,
670 const SkPath& devPath,
egdanield58a0ba2014-06-11 10:30:05 -0700671 const GrStrokeInfo& strokeInfo,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000672 bool doAA,
673 GrAutoScratchTexture* mask) {
674 GrTextureDesc desc;
675 desc.fFlags = kRenderTarget_GrTextureFlagBit;
676 desc.fWidth = SkScalarCeilToInt(maskRect.width());
677 desc.fHeight = SkScalarCeilToInt(maskRect.height());
678 // We actually only need A8, but it often isn't supported as a
679 // render target so default to RGBA_8888
680 desc.fConfig = kRGBA_8888_GrPixelConfig;
681 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
682 desc.fConfig = kAlpha_8_GrPixelConfig;
683 }
684
685 mask->set(context, desc);
686 if (NULL == mask->texture()) {
687 return false;
688 }
689
690 GrTexture* maskTexture = mask->texture();
691 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
692
693 GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget());
694 GrContext::AutoClip ac(context, clipRect);
695
696 context->clear(NULL, 0x0, true);
697
698 GrPaint tempPaint;
699 if (doAA) {
700 tempPaint.setAntiAlias(true);
701 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
702 // blend coeff of zero requires dual source blending support in order
703 // to properly blend partially covered pixels. This means the AA
704 // code path may not be taken. So we use a dst blend coeff of ISA. We
705 // could special case AA draws to a dst surface with known alpha=0 to
706 // use a zero dst coeff when dual source blending isn't available.
707 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
708 }
709
710 GrContext::AutoMatrix am;
711
712 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
713 SkMatrix translate;
714 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
715 am.set(context, translate);
egdanield58a0ba2014-06-11 10:30:05 -0700716 context->drawPath(tempPaint, devPath, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000717 return true;
718}
719
720SkBitmap wrap_texture(GrTexture* texture) {
721 SkBitmap result;
reed6c225732014-06-09 19:52:07 -0700722 result.setInfo(texture->info());
723 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000724 return result;
725}
726
727};
728
729void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
730 const SkPaint& paint, const SkMatrix* prePathMatrix,
731 bool pathIsMutable) {
732 CHECK_FOR_ANNOTATION(paint);
733 CHECK_SHOULD_DRAW(draw, false);
egdanield78a1682014-07-09 10:41:26 -0700734 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000735
736 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000737 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000738
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000739 // If we have a prematrix, apply it to the path, optimizing for the case
740 // where the original path can in fact be modified in place (even though
741 // its parameter type is const).
742 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000743 SkTLazy<SkPath> tmpPath;
744 SkTLazy<SkPath> effectPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000745
746 if (prePathMatrix) {
747 SkPath* result = pathPtr;
748
749 if (!pathIsMutable) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000750 result = tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000751 pathIsMutable = true;
752 }
753 // should I push prePathMatrix on our MV stack temporarily, instead
754 // of applying it here? See SkDraw.cpp
755 pathPtr->transform(*prePathMatrix, result);
756 pathPtr = result;
757 }
758 // at this point we're done with prePathMatrix
759 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
760
egdanield58a0ba2014-06-11 10:30:05 -0700761 GrStrokeInfo strokeInfo(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000762 SkPathEffect* pathEffect = paint.getPathEffect();
763 const SkRect* cullRect = NULL; // TODO: what is our bounds?
egdanield58a0ba2014-06-11 10:30:05 -0700764 SkStrokeRec* strokePtr = strokeInfo.getStrokeRecPtr();
765 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, strokePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000766 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000767 pathPtr = effectPath.get();
768 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700769 strokeInfo.removeDash();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000770 }
771
egdanield58a0ba2014-06-11 10:30:05 -0700772 const SkStrokeRec& stroke = strokeInfo.getStrokeRec();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000773 if (paint.getMaskFilter()) {
774 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000775 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
776 if (stroke.applyToPath(strokedPath, *pathPtr)) {
777 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000778 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700779 strokeInfo.setFillStyle();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000780 }
781 }
782
783 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000784 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000785
786 // transform the path into device space
787 pathPtr->transform(fContext->getMatrix(), devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000788
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000789 SkRect maskRect;
790 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
791 draw.fClip->getBounds(),
792 fContext->getMatrix(),
793 &maskRect)) {
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000794 // The context's matrix may change while creating the mask, so save the CTM here to
795 // pass to filterMaskGPU.
796 const SkMatrix ctm = fContext->getMatrix();
797
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000798 SkIRect finalIRect;
799 maskRect.roundOut(&finalIRect);
800 if (draw.fClip->quickReject(finalIRect)) {
801 // clipped out
802 return;
803 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000804
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000805 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint,
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000806 stroke, *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000807 // the mask filter was able to draw itself directly, so there's nothing
808 // left to do.
809 return;
810 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000811
812 GrAutoScratchTexture mask;
813
egdanield58a0ba2014-06-11 10:30:05 -0700814 if (create_mask_GPU(fContext, maskRect, *devPathPtr, strokeInfo,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000815 grPaint.isAntiAlias(), &mask)) {
816 GrTexture* filtered;
817
commit-bot@chromium.org41bf9302014-01-08 22:25:53 +0000818 if (paint.getMaskFilter()->filterMaskGPU(mask.texture(),
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000819 ctm, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000820 // filterMaskGPU gives us ownership of a ref to the result
821 SkAutoTUnref<GrTexture> atu(filtered);
822
823 // If the scratch texture that we used as the filter src also holds the filter
824 // result then we must detach so that this texture isn't recycled for a later
825 // draw.
826 if (filtered == mask.texture()) {
827 mask.detach();
828 filtered->unref(); // detach transfers GrAutoScratchTexture's ref to us.
829 }
830
831 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
832 // This path is completely drawn
833 return;
834 }
835 }
836 }
837 }
838
839 // draw the mask on the CPU - this is a fallthrough path in case the
840 // GPU path fails
841 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
842 SkPaint::kFill_Style;
egdanield58a0ba2014-06-11 10:30:05 -0700843 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(),
844 *draw.fClip, &grPaint, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000845 return;
846 }
847
egdanield58a0ba2014-06-11 10:30:05 -0700848 fContext->drawPath(grPaint, *pathPtr, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000849}
850
851static const int kBmpSmallTileSize = 1 << 10;
852
853static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
854 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
855 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
856 return tilesX * tilesY;
857}
858
859static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
860 if (maxTileSize <= kBmpSmallTileSize) {
861 return maxTileSize;
862 }
863
864 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
865 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
866
867 maxTileTotalTileSize *= maxTileSize * maxTileSize;
868 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
869
870 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
871 return kBmpSmallTileSize;
872 } else {
873 return maxTileSize;
874 }
875}
876
877// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
878// pixels from the bitmap are necessary.
879static void determine_clipped_src_rect(const GrContext* context,
880 const SkBitmap& bitmap,
881 const SkRect* srcRectPtr,
882 SkIRect* clippedSrcIRect) {
883 const GrClipData* clip = context->getClip();
884 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
885 SkMatrix inv;
886 if (!context->getMatrix().invert(&inv)) {
887 clippedSrcIRect->setEmpty();
888 return;
889 }
890 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
891 inv.mapRect(&clippedSrcRect);
892 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000893 // we've setup src space 0,0 to map to the top left of the src rect.
894 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000895 if (!clippedSrcRect.intersect(*srcRectPtr)) {
896 clippedSrcIRect->setEmpty();
897 return;
898 }
899 }
900 clippedSrcRect.roundOut(clippedSrcIRect);
901 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
902 if (!clippedSrcIRect->intersect(bmpBounds)) {
903 clippedSrcIRect->setEmpty();
904 }
905}
906
907bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
908 const GrTextureParams& params,
909 const SkRect* srcRectPtr,
910 int maxTileSize,
911 int* tileSize,
912 SkIRect* clippedSrcRect) const {
913 // if bitmap is explictly texture backed then just use the texture
914 if (NULL != bitmap.getTexture()) {
915 return false;
916 }
917
918 // if it's larger than the max tile size, then we have no choice but tiling.
919 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
920 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
921 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
922 return true;
923 }
924
925 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
926 return false;
927 }
928
929 // if the entire texture is already in our cache then no reason to tile it
930 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
931 return false;
932 }
933
934 // At this point we know we could do the draw by uploading the entire bitmap
935 // as a texture. However, if the texture would be large compared to the
936 // cache size and we don't require most of it for this draw then tile to
937 // reduce the amount of upload and cache spill.
938
939 // assumption here is that sw bitmap size is a good proxy for its size as
940 // a texture
941 size_t bmpSize = bitmap.getSize();
942 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000943 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000944 if (bmpSize < cacheSize / 2) {
945 return false;
946 }
947
948 // Figure out how much of the src we will need based on the src rect and clipping.
949 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
950 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
951 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
952 kBmpSmallTileSize * kBmpSmallTileSize;
953
954 return usedTileBytes < 2 * bmpSize;
955}
956
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000957void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000958 const SkBitmap& bitmap,
959 const SkMatrix& m,
960 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000961 SkMatrix concat;
962 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
963 if (!m.isIdentity()) {
964 concat.setConcat(*draw->fMatrix, m);
965 draw.writable()->fMatrix = &concat;
966 }
967 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000968}
969
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000970// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000971// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
972// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000973static inline void clamped_outset_with_offset(SkIRect* iRect,
974 int outset,
975 SkPoint* offset,
976 const SkIRect& clamp) {
977 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000978
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000979 int leftClampDelta = clamp.fLeft - iRect->fLeft;
980 if (leftClampDelta > 0) {
981 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000982 iRect->fLeft = clamp.fLeft;
983 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000984 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000985 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000986
987 int topClampDelta = clamp.fTop - iRect->fTop;
988 if (topClampDelta > 0) {
989 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000990 iRect->fTop = clamp.fTop;
991 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000992 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000993 }
994
995 if (iRect->fRight > clamp.fRight) {
996 iRect->fRight = clamp.fRight;
997 }
998 if (iRect->fBottom > clamp.fBottom) {
999 iRect->fBottom = clamp.fBottom;
1000 }
1001}
1002
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001003static bool has_aligned_samples(const SkRect& srcRect,
1004 const SkRect& transformedRect) {
1005 // detect pixel disalignment
1006 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1007 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1008 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1009 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1010 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1011 COLOR_BLEED_TOLERANCE &&
1012 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1013 COLOR_BLEED_TOLERANCE) {
1014 return true;
1015 }
1016 return false;
1017}
1018
1019static bool may_color_bleed(const SkRect& srcRect,
1020 const SkRect& transformedRect,
1021 const SkMatrix& m) {
1022 // Only gets called if has_aligned_samples returned false.
1023 // So we can assume that sampling is axis aligned but not texel aligned.
1024 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
1025 SkRect innerSrcRect(srcRect), innerTransformedRect,
1026 outerTransformedRect(transformedRect);
1027 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1028 m.mapRect(&innerTransformedRect, innerSrcRect);
1029
1030 // The gap between outerTransformedRect and innerTransformedRect
1031 // represents the projection of the source border area, which is
1032 // problematic for color bleeding. We must check whether any
1033 // destination pixels sample the border area.
1034 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1035 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1036 SkIRect outer, inner;
1037 outerTransformedRect.round(&outer);
1038 innerTransformedRect.round(&inner);
1039 // If the inner and outer rects round to the same result, it means the
1040 // border does not overlap any pixel centers. Yay!
1041 return inner != outer;
1042}
1043
1044static bool needs_texture_domain(const SkBitmap& bitmap,
1045 const SkRect& srcRect,
1046 GrTextureParams &params,
1047 const SkMatrix& contextMatrix,
1048 bool bicubic) {
1049 bool needsTextureDomain = false;
1050
1051 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
1052 // Need texture domain if drawing a sub rect
1053 needsTextureDomain = srcRect.width() < bitmap.width() ||
1054 srcRect.height() < bitmap.height();
1055 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
1056 // sampling is axis-aligned
1057 SkRect transformedRect;
1058 contextMatrix.mapRect(&transformedRect, srcRect);
1059
1060 if (has_aligned_samples(srcRect, transformedRect)) {
1061 params.setFilterMode(GrTextureParams::kNone_FilterMode);
1062 needsTextureDomain = false;
1063 } else {
1064 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
1065 }
1066 }
1067 }
1068 return needsTextureDomain;
1069}
1070
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001071void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1072 const SkBitmap& bitmap,
1073 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001074 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001075 const SkPaint& paint,
1076 SkCanvas::DrawBitmapRectFlags flags) {
1077 CHECK_SHOULD_DRAW(draw, false);
1078
1079 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001080 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001081 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1082 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001083 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001084 SkScalar w = SkIntToScalar(bitmap.width());
1085 SkScalar h = SkIntToScalar(bitmap.height());
1086 dstSize.fWidth = w;
1087 dstSize.fHeight = h;
1088 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001089 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001090 } else {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001091 SkASSERT(NULL != dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001092 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001093 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001094 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1095 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1096 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1097 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001098 }
1099
1100 if (paint.getMaskFilter()){
1101 // Convert the bitmap to a shader so that the rect can be drawn
1102 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001103 SkBitmap tmp; // subset of bitmap, if necessary
1104 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001105 SkMatrix localM;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001106 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001107 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1108 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1109 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001110 // In bleed mode we position and trim the bitmap based on the src rect which is
1111 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1112 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1113 // compensate.
1114 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1115 SkIRect iSrc;
1116 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001117
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001118 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1119 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001120
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001121 if (!bitmap.extractSubset(&tmp, iSrc)) {
1122 return; // extraction failed
1123 }
1124 bitmapPtr = &tmp;
1125 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001126
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001127 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001128 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001129 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001130 } else {
1131 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001132 }
1133
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001134 SkPaint paintWithShader(paint);
1135 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001136 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001137 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1138 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001139
1140 return;
1141 }
1142
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001143 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1144 // the view matrix rather than a local matrix.
1145 SkMatrix m;
1146 m.setScale(dstSize.fWidth / srcRect.width(),
1147 dstSize.fHeight / srcRect.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001148 fContext->concatMatrix(m);
1149
1150 GrTextureParams params;
1151 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1152 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001153
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001154 bool doBicubic = false;
1155
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001156 switch(paintFilterLevel) {
1157 case SkPaint::kNone_FilterLevel:
1158 textureFilterMode = GrTextureParams::kNone_FilterMode;
1159 break;
1160 case SkPaint::kLow_FilterLevel:
1161 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1162 break;
1163 case SkPaint::kMedium_FilterLevel:
commit-bot@chromium.org18786512014-05-20 14:53:45 +00001164 if (fContext->getMatrix().getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001165 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1166 } else {
1167 // Don't trigger MIP level generation unnecessarily.
1168 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1169 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001170 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001171 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001172 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001173 doBicubic =
1174 GrBicubicEffect::ShouldUseBicubic(fContext->getMatrix(), &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001175 break;
1176 default:
1177 SkErrorInternals::SetError( kInvalidPaint_SkError,
1178 "Sorry, I don't understand the filtering "
1179 "mode you asked for. Falling back to "
1180 "MIPMaps.");
1181 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1182 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001183 }
1184
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001185 int tileFilterPad;
1186 if (doBicubic) {
1187 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1188 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1189 tileFilterPad = 0;
1190 } else {
1191 tileFilterPad = 1;
1192 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001193 params.setFilterMode(textureFilterMode);
1194
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001195 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001196 int tileSize;
1197
1198 SkIRect clippedSrcRect;
1199 if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize,
1200 &clippedSrcRect)) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001201 this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize,
1202 doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001203 } else {
1204 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001205 bool needsTextureDomain = needs_texture_domain(bitmap,
1206 srcRect,
1207 params,
1208 fContext->getMatrix(),
1209 doBicubic);
1210 this->internalDrawBitmap(bitmap,
1211 srcRect,
1212 params,
1213 paint,
1214 flags,
1215 doBicubic,
1216 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001217 }
1218}
1219
1220// Break 'bitmap' into several tiles to draw it since it has already
1221// been determined to be too large to fit in VRAM
1222void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
1223 const SkRect& srcRect,
1224 const SkIRect& clippedSrcIRect,
1225 const GrTextureParams& params,
1226 const SkPaint& paint,
1227 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001228 int tileSize,
1229 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001230 // The following pixel lock is technically redundant, but it is desirable
1231 // to lock outside of the tile loop to prevent redecoding the whole image
1232 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1233 // is larger than the limit of the discardable memory pool.
1234 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001235 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1236
1237 int nx = bitmap.width() / tileSize;
1238 int ny = bitmap.height() / tileSize;
1239 for (int x = 0; x <= nx; x++) {
1240 for (int y = 0; y <= ny; y++) {
1241 SkRect tileR;
1242 tileR.set(SkIntToScalar(x * tileSize),
1243 SkIntToScalar(y * tileSize),
1244 SkIntToScalar((x + 1) * tileSize),
1245 SkIntToScalar((y + 1) * tileSize));
1246
1247 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1248 continue;
1249 }
1250
1251 if (!tileR.intersect(srcRect)) {
1252 continue;
1253 }
1254
1255 SkBitmap tmpB;
1256 SkIRect iTileR;
1257 tileR.roundOut(&iTileR);
1258 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1259 SkIntToScalar(iTileR.fTop));
1260
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001261 // Adjust the context matrix to draw at the right x,y in device space
1262 SkMatrix tmpM;
1263 GrContext::AutoMatrix am;
1264 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
1265 am.setPreConcat(fContext, tmpM);
1266
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001267 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001268 SkIRect iClampRect;
1269
1270 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1271 // In bleed mode we want to always expand the tile on all edges
1272 // but stay within the bitmap bounds
1273 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1274 } else {
1275 // In texture-domain/clamp mode we only want to expand the
1276 // tile on edges interior to "srcRect" (i.e., we want to
1277 // not bleed across the original clamped edges)
1278 srcRect.roundOut(&iClampRect);
1279 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001280 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1281 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001282 }
1283
1284 if (bitmap.extractSubset(&tmpB, iTileR)) {
1285 // now offset it to make it "local" to our tmp bitmap
1286 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001287 GrTextureParams paramsTemp = params;
1288 bool needsTextureDomain = needs_texture_domain(bitmap,
1289 srcRect,
1290 paramsTemp,
1291 fContext->getMatrix(),
1292 bicubic);
1293 this->internalDrawBitmap(tmpB,
1294 tileR,
1295 paramsTemp,
1296 paint,
1297 flags,
1298 bicubic,
1299 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001300 }
1301 }
1302 }
1303}
1304
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001305
1306/*
1307 * This is called by drawBitmap(), which has to handle images that may be too
1308 * large to be represented by a single texture.
1309 *
1310 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1311 * and that non-texture portion of the GrPaint has already been setup.
1312 */
1313void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
1314 const SkRect& srcRect,
1315 const GrTextureParams& params,
1316 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001317 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001318 bool bicubic,
1319 bool needsTextureDomain) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001320 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1321 bitmap.height() <= fContext->getMaxTextureSize());
1322
1323 GrTexture* texture;
1324 SkAutoCachedTexture act(this, bitmap, &params, &texture);
1325 if (NULL == texture) {
1326 return;
1327 }
1328
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001329 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001330 SkRect paintRect;
1331 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1332 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1333 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1334 SkScalarMul(srcRect.fTop, hInv),
1335 SkScalarMul(srcRect.fRight, wInv),
1336 SkScalarMul(srcRect.fBottom, hInv));
1337
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001338 SkRect textureDomain = SkRect::MakeEmpty();
bsalomon83d081a2014-07-08 09:56:10 -07001339 SkAutoTUnref<GrEffect> effect;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001340 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001341 // Use a constrained texture domain to avoid color bleeding
1342 SkScalar left, top, right, bottom;
1343 if (srcRect.width() > SK_Scalar1) {
1344 SkScalar border = SK_ScalarHalf / texture->width();
1345 left = paintRect.left() + border;
1346 right = paintRect.right() - border;
1347 } else {
1348 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1349 }
1350 if (srcRect.height() > SK_Scalar1) {
1351 SkScalar border = SK_ScalarHalf / texture->height();
1352 top = paintRect.top() + border;
1353 bottom = paintRect.bottom() - border;
1354 } else {
1355 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1356 }
1357 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001358 if (bicubic) {
1359 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
1360 } else {
1361 effect.reset(GrTextureDomainEffect::Create(texture,
1362 SkMatrix::I(),
1363 textureDomain,
1364 GrTextureDomain::kClamp_Mode,
1365 params.filterMode()));
1366 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001367 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001368 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1369 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
1370 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001371 } else {
1372 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
1373 }
1374
1375 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1376 // the rest from the SkPaint.
1377 GrPaint grPaint;
1378 grPaint.addColorEffect(effect);
reed0689d7b2014-06-14 05:30:20 -07001379 bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType());
bsalomon83d081a2014-07-08 09:56:10 -07001380 GrColor paintColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1381 SkColor2GrColor(paint.getColor());
1382 SkPaint2GrPaintNoShader(this->context(), paint, paintColor, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001383
bsalomon01c8da12014-08-04 09:21:30 -07001384 fContext->drawRectToRect(grPaint, dstRect, paintRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001385}
1386
1387static bool filter_texture(SkBaseDevice* device, GrContext* context,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001388 GrTexture* texture, const SkImageFilter* filter,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001389 int w, int h, const SkImageFilter::Context& ctx,
1390 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001391 SkASSERT(filter);
1392 SkDeviceImageFilterProxy proxy(device);
1393
1394 if (filter->canFilterImageGPU()) {
1395 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1396 // filter. Also set the clip wide open and the matrix to identity.
1397 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001398 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001399 } else {
1400 return false;
1401 }
1402}
1403
1404void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1405 int left, int top, const SkPaint& paint) {
1406 // drawSprite is defined to be in device coords.
1407 CHECK_SHOULD_DRAW(draw, true);
1408
1409 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1410 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1411 return;
1412 }
1413
1414 int w = bitmap.width();
1415 int h = bitmap.height();
1416
1417 GrTexture* texture;
1418 // draw sprite uses the default texture params
1419 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
1420
1421 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001422 // This bitmap will own the filtered result as a texture.
1423 SkBitmap filteredBitmap;
1424
1425 if (NULL != filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001426 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001427 SkMatrix matrix(*draw.fMatrix);
1428 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001429 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
senorblanco55b6d8b2014-07-30 11:26:46 -07001430 SkAutoTUnref<SkImageFilter::UniqueIDCache> cache(getImageFilterCache());
1431 // This cache is transient, and is freed (along with all its contained
1432 // textures) when it goes out of scope.
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001433 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001434 if (filter_texture(this, fContext, texture, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001435 &offset)) {
1436 texture = (GrTexture*) filteredBitmap.getTexture();
1437 w = filteredBitmap.width();
1438 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001439 left += offset.x();
1440 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001441 } else {
1442 return;
1443 }
1444 }
1445
1446 GrPaint grPaint;
1447 grPaint.addColorTextureEffect(texture, SkMatrix::I());
1448
dandov9de5b512014-06-10 14:38:28 -07001449 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1450 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001451
1452 fContext->drawRectToRect(grPaint,
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001453 SkRect::MakeXYWH(SkIntToScalar(left),
1454 SkIntToScalar(top),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001455 SkIntToScalar(w),
1456 SkIntToScalar(h)),
1457 SkRect::MakeXYWH(0,
1458 0,
1459 SK_Scalar1 * w / texture->width(),
1460 SK_Scalar1 * h / texture->height()));
1461}
1462
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001463void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001464 const SkRect* src, const SkRect& dst,
1465 const SkPaint& paint,
1466 SkCanvas::DrawBitmapRectFlags flags) {
1467 SkMatrix matrix;
1468 SkRect bitmapBounds, tmpSrc;
1469
1470 bitmapBounds.set(0, 0,
1471 SkIntToScalar(bitmap.width()),
1472 SkIntToScalar(bitmap.height()));
1473
1474 // Compute matrix from the two rectangles
1475 if (NULL != src) {
1476 tmpSrc = *src;
1477 } else {
1478 tmpSrc = bitmapBounds;
1479 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001480
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001481 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1482
1483 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1484 if (NULL != src) {
1485 if (!bitmapBounds.contains(tmpSrc)) {
1486 if (!tmpSrc.intersect(bitmapBounds)) {
1487 return; // nothing to draw
1488 }
1489 }
1490 }
1491
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001492 SkRect tmpDst;
1493 matrix.mapRect(&tmpDst, tmpSrc);
1494
1495 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1496 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1497 // Translate so that tempDst's top left is at the origin.
1498 matrix = *origDraw.fMatrix;
1499 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1500 draw.writable()->fMatrix = &matrix;
1501 }
1502 SkSize dstSize;
1503 dstSize.fWidth = tmpDst.width();
1504 dstSize.fHeight = tmpDst.height();
1505
1506 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001507}
1508
1509void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1510 int x, int y, const SkPaint& paint) {
1511 // clear of the source device must occur before CHECK_SHOULD_DRAW
egdanield78a1682014-07-09 10:41:26 -07001512 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001513 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1514 if (dev->fNeedClear) {
1515 // TODO: could check here whether we really need to draw at all
1516 dev->clear(0x0);
1517 }
1518
1519 // drawDevice is defined to be in device coords.
1520 CHECK_SHOULD_DRAW(draw, true);
1521
1522 GrRenderTarget* devRT = dev->accessRenderTarget();
1523 GrTexture* devTex;
1524 if (NULL == (devTex = devRT->asTexture())) {
1525 return;
1526 }
1527
1528 const SkBitmap& bm = dev->accessBitmap(false);
1529 int w = bm.width();
1530 int h = bm.height();
1531
1532 SkImageFilter* filter = paint.getImageFilter();
1533 // This bitmap will own the filtered result as a texture.
1534 SkBitmap filteredBitmap;
1535
1536 if (NULL != filter) {
1537 SkIPoint offset = SkIPoint::Make(0, 0);
1538 SkMatrix matrix(*draw.fMatrix);
1539 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001540 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
senorblanco55b6d8b2014-07-30 11:26:46 -07001541 // This cache is transient, and is freed (along with all its contained
1542 // textures) when it goes out of scope.
1543 SkAutoTUnref<SkImageFilter::UniqueIDCache> cache(getImageFilterCache());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001544 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001545 if (filter_texture(this, fContext, devTex, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001546 &offset)) {
1547 devTex = filteredBitmap.getTexture();
1548 w = filteredBitmap.width();
1549 h = filteredBitmap.height();
1550 x += offset.fX;
1551 y += offset.fY;
1552 } else {
1553 return;
1554 }
1555 }
1556
1557 GrPaint grPaint;
1558 grPaint.addColorTextureEffect(devTex, SkMatrix::I());
1559
dandov9de5b512014-06-10 14:38:28 -07001560 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1561 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001562
1563 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1564 SkIntToScalar(y),
1565 SkIntToScalar(w),
1566 SkIntToScalar(h));
1567
1568 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1569 // scratch texture).
1570 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1571 SK_Scalar1 * h / devTex->height());
1572
1573 fContext->drawRectToRect(grPaint, dstRect, srcRect);
1574}
1575
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001576bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001577 return filter->canFilterImageGPU();
1578}
1579
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001580bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001581 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001582 SkBitmap* result, SkIPoint* offset) {
1583 // want explicitly our impl, so guard against a subclass of us overriding it
1584 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1585 return false;
1586 }
1587
1588 SkAutoLockPixels alp(src, !src.getTexture());
1589 if (!src.getTexture() && !src.readyToDraw()) {
1590 return false;
1591 }
1592
1593 GrTexture* texture;
1594 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1595 // must be pushed upstack.
1596 SkAutoCachedTexture act(this, src, NULL, &texture);
1597
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001598 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctx,
1599 result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001600}
1601
1602///////////////////////////////////////////////////////////////////////////////
1603
1604// must be in SkCanvas::VertexMode order
1605static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1606 kTriangles_GrPrimitiveType,
1607 kTriangleStrip_GrPrimitiveType,
1608 kTriangleFan_GrPrimitiveType,
1609};
1610
1611void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1612 int vertexCount, const SkPoint vertices[],
1613 const SkPoint texs[], const SkColor colors[],
1614 SkXfermode* xmode,
1615 const uint16_t indices[], int indexCount,
1616 const SkPaint& paint) {
1617 CHECK_SHOULD_DRAW(draw, false);
1618
egdanield78a1682014-07-09 10:41:26 -07001619 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawVertices", fContext);
dandov32a311b2014-07-15 19:46:26 -07001620
1621 const uint16_t* outIndices;
1622 SkAutoTDeleteArray<uint16_t> outAlloc(NULL);
1623 GrPrimitiveType primType;
1624 GrPaint grPaint;
bsalomona098dd42014-08-06 11:01:44 -07001625
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001626 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1627 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
dandov32a311b2014-07-15 19:46:26 -07001628
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001629 texs = NULL;
dandov32a311b2014-07-15 19:46:26 -07001630
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001631 SkPaint copy(paint);
1632 copy.setStyle(SkPaint::kStroke_Style);
1633 copy.setStrokeWidth(0);
dandov32a311b2014-07-15 19:46:26 -07001634
1635 // we ignore the shader if texs is null.
1636 SkPaint2GrPaintNoShader(this->context(), copy, SkColor2GrColor(copy.getColor()),
1637 NULL == colors, &grPaint);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001638
dandov32a311b2014-07-15 19:46:26 -07001639 primType = kLines_GrPrimitiveType;
1640 int triangleCount = 0;
bsalomona098dd42014-08-06 11:01:44 -07001641 int n = (NULL == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001642 switch (vmode) {
1643 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001644 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001645 break;
1646 case SkCanvas::kTriangleStrip_VertexMode:
1647 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001648 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001649 break;
1650 }
1651
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001652 VertState state(vertexCount, indices, indexCount);
1653 VertState::Proc vertProc = state.chooseProc(vmode);
dandov32a311b2014-07-15 19:46:26 -07001654
1655 //number of indices for lines per triangle with kLines
1656 indexCount = triangleCount * 6;
1657
1658 outAlloc.reset(SkNEW_ARRAY(uint16_t, indexCount));
1659 outIndices = outAlloc.get();
1660 uint16_t* auxIndices = outAlloc.get();
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001661 int i = 0;
1662 while (vertProc(&state)) {
dandov32a311b2014-07-15 19:46:26 -07001663 auxIndices[i] = state.f0;
1664 auxIndices[i + 1] = state.f1;
1665 auxIndices[i + 2] = state.f1;
1666 auxIndices[i + 3] = state.f2;
1667 auxIndices[i + 4] = state.f2;
1668 auxIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001669 i += 6;
1670 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001671 } else {
dandov32a311b2014-07-15 19:46:26 -07001672 outIndices = indices;
1673 primType = gVertexMode2PrimitiveType[vmode];
1674
1675 if (NULL == texs || NULL == paint.getShader()) {
1676 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColor(paint.getColor()),
1677 NULL == colors, &grPaint);
1678 } else {
1679 SkPaint2GrPaintShader(this->context(), paint, NULL == colors, &grPaint);
1680 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001681 }
1682
mtklein2583b622014-06-04 08:20:41 -07001683#if 0
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001684 if (NULL != xmode && NULL != texs && NULL != colors) {
1685 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1686 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001687 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001688 }
1689 }
mtklein2583b622014-06-04 08:20:41 -07001690#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001691
1692 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1693 if (NULL != colors) {
1694 // need to convert byte order and from non-PM to PM
1695 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001696 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001697 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001698 color = colors[i];
1699 if (paint.getAlpha() != 255) {
1700 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1701 }
1702 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001703 }
1704 colors = convertedColors.get();
1705 }
1706 fContext->drawVertices(grPaint,
dandov32a311b2014-07-15 19:46:26 -07001707 primType,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001708 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001709 vertices,
1710 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001711 colors,
dandov32a311b2014-07-15 19:46:26 -07001712 outIndices,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001713 indexCount);
1714}
1715
1716///////////////////////////////////////////////////////////////////////////////
1717
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001718void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1719 size_t byteLength, SkScalar x, SkScalar y,
1720 const SkPaint& paint) {
1721 CHECK_SHOULD_DRAW(draw, false);
egdanield78a1682014-07-09 10:41:26 -07001722 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001723
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001724 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001725 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001726 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001727
1728 SkDEBUGCODE(this->validate();)
1729
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001730 fMainTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
1731 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001732 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001733 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001734
1735 SkDEBUGCODE(this->validate();)
1736
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001737 fFallbackTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001738 } else {
1739 // this guy will just call our drawPath()
1740 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001741 }
1742}
1743
1744void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1745 size_t byteLength, const SkScalar pos[],
1746 SkScalar constY, int scalarsPerPos,
1747 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -07001748 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001749 CHECK_SHOULD_DRAW(draw, false);
1750
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001751 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001752 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001753 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001754
1755 SkDEBUGCODE(this->validate();)
1756
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001757 fMainTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001758 constY, scalarsPerPos);
1759 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001760 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001761 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001762
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001763 SkDEBUGCODE(this->validate();)
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001764
1765 fFallbackTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001766 constY, scalarsPerPos);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001767 } else {
1768 draw.drawPosText_asPaths((const char*)text, byteLength, pos, constY,
1769 scalarsPerPos, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001770 }
1771}
1772
1773void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1774 size_t len, const SkPath& path,
1775 const SkMatrix* m, const SkPaint& paint) {
1776 CHECK_SHOULD_DRAW(draw, false);
1777
1778 SkASSERT(draw.fDevice == this);
1779 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1780}
1781
1782///////////////////////////////////////////////////////////////////////////////
1783
1784bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1785 if (!paint.isLCDRenderText()) {
1786 // we're cool with the paint as is
1787 return false;
1788 }
1789
1790 if (paint.getShader() ||
1791 paint.getXfermode() || // unless its srcover
1792 paint.getMaskFilter() ||
1793 paint.getRasterizer() ||
1794 paint.getColorFilter() ||
1795 paint.getPathEffect() ||
1796 paint.isFakeBoldText() ||
1797 paint.getStyle() != SkPaint::kFill_Style) {
1798 // turn off lcd
1799 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1800 flags->fHinting = paint.getHinting();
1801 return true;
1802 }
1803 // we're cool with the paint as is
1804 return false;
1805}
1806
1807void SkGpuDevice::flush() {
1808 DO_DEFERRED_CLEAR();
1809 fContext->resolveRenderTarget(fRenderTarget);
1810}
1811
1812///////////////////////////////////////////////////////////////////////////////
1813
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001814SkBaseDevice* SkGpuDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001815 GrTextureDesc desc;
1816 desc.fConfig = fRenderTarget->config();
1817 desc.fFlags = kRenderTarget_GrTextureFlagBit;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001818 desc.fWidth = info.width();
1819 desc.fHeight = info.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001820 desc.fSampleCnt = fRenderTarget->numSamples();
1821
1822 SkAutoTUnref<GrTexture> texture;
1823 // Skia's convention is to only clear a device if it is non-opaque.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001824 unsigned flags = info.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001825
1826#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1827 // layers are never draw in repeat modes, so we can request an approx
1828 // match and ignore any padding.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001829 flags |= kCached_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001830 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1831 GrContext::kApprox_ScratchTexMatch :
1832 GrContext::kExact_ScratchTexMatch;
1833 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
1834#else
1835 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1836#endif
1837 if (NULL != texture.get()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001838 return SkGpuDevice::Create(texture, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001839 } else {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001840 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1841 info.width(), info.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001842 return NULL;
1843 }
1844}
1845
reed@google.com76f10a32014-02-05 15:32:21 +00001846SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info) {
1847 return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples());
1848}
1849
robertphillips9b14f262014-06-04 05:40:44 -07001850void SkGpuDevice::EXPERIMENTAL_optimize(const SkPicture* picture) {
robertphillipsd771f6b2014-07-22 10:18:06 -07001851 fContext->getLayerCache()->processDeletedPictures();
1852
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001853 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001854
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001855 const SkPicture::AccelData* existing = picture->EXPERIMENTAL_getAccelData(key);
1856 if (NULL != existing) {
1857 return;
1858 }
1859
commit-bot@chromium.org8fd93822014-05-06 13:43:22 +00001860 SkAutoTUnref<GPUAccelData> data(SkNEW_ARGS(GPUAccelData, (key)));
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001861
1862 picture->EXPERIMENTAL_addAccelData(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001863
1864 GatherGPUInfo(picture, data);
robertphillipsd771f6b2014-07-22 10:18:06 -07001865
1866 fContext->getLayerCache()->trackPicture(picture);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001867}
1868
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001869static void wrap_texture(GrTexture* texture, int width, int height, SkBitmap* result) {
1870 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001871 result->setInfo(info);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001872 result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
1873}
1874
robertphillips0c423322014-07-31 11:02:38 -07001875bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* picture) {
robertphillipsd771f6b2014-07-22 10:18:06 -07001876 fContext->getLayerCache()->processDeletedPictures();
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001877
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001878 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001879
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001880 const SkPicture::AccelData* data = picture->EXPERIMENTAL_getAccelData(key);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001881 if (NULL == data) {
1882 return false;
1883 }
1884
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001885 const GPUAccelData *gpuData = static_cast<const GPUAccelData*>(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001886
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001887 if (0 == gpuData->numSaveLayers()) {
1888 return false;
1889 }
1890
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001891 SkAutoTArray<bool> pullForward(gpuData->numSaveLayers());
1892 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1893 pullForward[i] = false;
1894 }
1895
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001896 SkRect clipBounds;
robertphillips0c423322014-07-31 11:02:38 -07001897 if (!mainCanvas->getClipBounds(&clipBounds)) {
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001898 return true;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001899 }
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001900 SkIRect query;
1901 clipBounds.roundOut(&query);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001902
robertphillipsce4dd3d2014-07-07 13:46:35 -07001903 SkAutoTDelete<const SkPicture::OperationList> ops(picture->EXPERIMENTAL_getActiveOps(query));
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001904
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001905 // This code pre-renders the entire layer since it will be cached and potentially
1906 // reused with different clips (e.g., in different tiles). Because of this the
1907 // clip will not be limiting the size of the pre-rendered layer. kSaveLayerMaxSize
1908 // is used to limit which clips are pre-rendered.
1909 static const int kSaveLayerMaxSize = 256;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001910
robertphillipsce4dd3d2014-07-07 13:46:35 -07001911 if (NULL != ops.get()) {
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001912 // In this case the picture has been generated with a BBH so we use
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001913 // the BBH to limit the pre-rendering to just the layers needed to cover
1914 // the region being drawn
robertphillipsce4dd3d2014-07-07 13:46:35 -07001915 for (int i = 0; i < ops->numOps(); ++i) {
1916 uint32_t offset = ops->offset(i);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001917
1918 // For now we're saving all the layers in the GPUAccelData so they
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001919 // can be nested. Additionally, the nested layers appear before
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001920 // their parent in the list.
1921 for (int j = 0 ; j < gpuData->numSaveLayers(); ++j) {
1922 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1923
1924 if (pullForward[j]) {
1925 continue; // already pulling forward
1926 }
1927
1928 if (offset < info.fSaveLayerOpID || offset > info.fRestoreOpID) {
1929 continue; // the op isn't in this range
1930 }
1931
1932 // TODO: once this code is more stable unsuitable layers can
1933 // just be omitted during the optimization stage
1934 if (!info.fValid ||
1935 kSaveLayerMaxSize < info.fSize.fWidth ||
1936 kSaveLayerMaxSize < info.fSize.fHeight ||
1937 info.fIsNested) {
1938 continue; // this layer is unsuitable
1939 }
1940
1941 pullForward[j] = true;
1942 }
1943 }
1944 } else {
1945 // In this case there is no BBH associated with the picture. Pre-render
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +00001946 // all the layers that intersect the drawn region
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001947 for (int j = 0; j < gpuData->numSaveLayers(); ++j) {
1948 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1949
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +00001950 SkIRect layerRect = SkIRect::MakeXYWH(info.fOffset.fX,
1951 info.fOffset.fY,
1952 info.fSize.fWidth,
1953 info.fSize.fHeight);
1954
1955 if (!SkIRect::Intersects(query, layerRect)) {
1956 continue;
1957 }
1958
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001959 // TODO: once this code is more stable unsuitable layers can
1960 // just be omitted during the optimization stage
1961 if (!info.fValid ||
1962 kSaveLayerMaxSize < info.fSize.fWidth ||
1963 kSaveLayerMaxSize < info.fSize.fHeight ||
1964 info.fIsNested) {
1965 continue;
1966 }
1967
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001968 pullForward[j] = true;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001969 }
1970 }
1971
robertphillipsc26d9912014-07-10 07:21:27 -07001972 SkPictureReplacementPlayback::PlaybackReplacements replacements;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001973
robertphillips0c423322014-07-31 11:02:38 -07001974 SkTDArray<GrCachedLayer*> atlased, nonAtlased;
1975 atlased.setReserve(gpuData->numSaveLayers());
1976
robertphillips4ec84da2014-06-24 13:10:43 -07001977 // Generate the layer and/or ensure it is locked
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001978 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1979 if (pullForward[i]) {
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001980 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(i);
1981
robertphillips0c423322014-07-31 11:02:38 -07001982 GrCachedLayer* layer = fContext->getLayerCache()->findLayerOrCreate(picture,
1983 info.fSaveLayerOpID,
1984 info.fRestoreOpID,
1985 info.fCTM);
1986
robertphillipsc26d9912014-07-10 07:21:27 -07001987 SkPictureReplacementPlayback::PlaybackReplacements::ReplacementInfo* layerInfo =
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001988 replacements.push();
robertphillips4ec84da2014-06-24 13:10:43 -07001989 layerInfo->fStart = info.fSaveLayerOpID;
1990 layerInfo->fStop = info.fRestoreOpID;
1991 layerInfo->fPos = info.fOffset;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001992
robertphillips4ec84da2014-06-24 13:10:43 -07001993 GrTextureDesc desc;
1994 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1995 desc.fWidth = info.fSize.fWidth;
1996 desc.fHeight = info.fSize.fHeight;
1997 desc.fConfig = kSkia8888_GrPixelConfig;
1998 // TODO: need to deal with sample count
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001999
robertphillips4ec84da2014-06-24 13:10:43 -07002000 bool needsRendering = !fContext->getLayerCache()->lock(layer, desc);
robertphillips952841b2014-06-30 08:26:50 -07002001 if (NULL == layer->texture()) {
robertphillips4ec84da2014-06-24 13:10:43 -07002002 continue;
2003 }
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002004
robertphillips4ec84da2014-06-24 13:10:43 -07002005 layerInfo->fBM = SkNEW(SkBitmap); // fBM is allocated so ReplacementInfo can be POD
robertphillips952841b2014-06-30 08:26:50 -07002006 wrap_texture(layer->texture(),
robertphillips21048b52014-07-15 19:46:35 -07002007 !layer->isAtlased() ? desc.fWidth : layer->texture()->width(),
2008 !layer->isAtlased() ? desc.fHeight : layer->texture()->height(),
robertphillips952841b2014-06-30 08:26:50 -07002009 layerInfo->fBM);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002010
robertphillips4ec84da2014-06-24 13:10:43 -07002011 SkASSERT(info.fPaint);
2012 layerInfo->fPaint = info.fPaint;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002013
robertphillips21048b52014-07-15 19:46:35 -07002014 layerInfo->fSrcRect = SkIRect::MakeXYWH(layer->rect().fLeft,
2015 layer->rect().fTop,
2016 layer->rect().width(),
2017 layer->rect().height());
2018
robertphillips4ec84da2014-06-24 13:10:43 -07002019 if (needsRendering) {
robertphillips21048b52014-07-15 19:46:35 -07002020 if (layer->isAtlased()) {
robertphillips0c423322014-07-31 11:02:38 -07002021 *atlased.append() = layer;
robertphillips952841b2014-06-30 08:26:50 -07002022 } else {
robertphillips0c423322014-07-31 11:02:38 -07002023 *nonAtlased.append() = layer;
robertphillips952841b2014-06-30 08:26:50 -07002024 }
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00002025 }
2026 }
2027 }
2028
robertphillips0c423322014-07-31 11:02:38 -07002029 // Render the atlased layers that require it
2030 if (atlased.count() > 0) {
2031 // All the atlased layers are rendered into the same GrTexture
2032 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect(
2033 atlased[0]->texture()->asRenderTarget(),
2034 SkSurface::kStandard_TextRenderMode,
2035 SkSurface::kDontClear_RenderTargetFlag));
2036
2037 SkCanvas* atlasCanvas = surface->getCanvas();
2038
2039 SkPaint paint;
2040 paint.setColor(SK_ColorTRANSPARENT);
2041 paint.setXfermode(SkXfermode::Create(SkXfermode::kSrc_Mode))->unref();
2042
2043 for (int i = 0; i < atlased.count(); ++i) {
2044 GrCachedLayer* layer = atlased[i];
2045
2046 atlasCanvas->save();
2047
2048 // Add a rect clip to make sure the rendering doesn't
2049 // extend beyond the boundaries of the atlased sub-rect
2050 SkRect bound = SkRect::MakeXYWH(SkIntToScalar(layer->rect().fLeft),
2051 SkIntToScalar(layer->rect().fTop),
2052 SkIntToScalar(layer->rect().width()),
2053 SkIntToScalar(layer->rect().height()));
2054 atlasCanvas->clipRect(bound);
2055
2056 // Since 'clear' doesn't respect the clip we need to draw a rect
2057 // TODO: ensure none of the atlased layers contain a clear call!
2058 atlasCanvas->drawRect(bound, paint);
2059
2060 // info.fCTM maps the layer's top/left to the origin.
2061 // Since this layer is atlased, the top/left corner needs
2062 // to be offset to the correct location in the backing texture.
2063 atlasCanvas->translate(bound.fLeft, bound.fTop);
2064 atlasCanvas->concat(layer->ctm());
2065
2066 SkPictureRangePlayback rangePlayback(picture,
2067 layer->start(),
2068 layer->stop());
2069 rangePlayback.draw(atlasCanvas, NULL);
2070
2071 atlasCanvas->restore();
2072 }
2073
2074 atlasCanvas->flush();
2075 }
2076
2077 // Render the non-atlased layers that require it
2078 for (int i = 0; i < nonAtlased.count(); ++i) {
2079 GrCachedLayer* layer = nonAtlased[i];
2080
2081 // Each non-atlased layer has its own GrTexture
2082 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect(
2083 layer->texture()->asRenderTarget(),
2084 SkSurface::kStandard_TextRenderMode,
2085 SkSurface::kDontClear_RenderTargetFlag));
2086
2087 SkCanvas* layerCanvas = surface->getCanvas();
2088
2089 // Add a rect clip to make sure the rendering doesn't
2090 // extend beyond the boundaries of the atlased sub-rect
2091 SkRect bound = SkRect::MakeXYWH(SkIntToScalar(layer->rect().fLeft),
2092 SkIntToScalar(layer->rect().fTop),
2093 SkIntToScalar(layer->rect().width()),
2094 SkIntToScalar(layer->rect().height()));
2095
2096 layerCanvas->clipRect(bound); // TODO: still useful?
2097
2098 layerCanvas->clear(SK_ColorTRANSPARENT);
2099
2100 layerCanvas->concat(layer->ctm());
2101
2102 SkPictureRangePlayback rangePlayback(picture,
2103 layer->start(),
2104 layer->stop());
2105 rangePlayback.draw(layerCanvas, NULL);
2106
2107 layerCanvas->flush();
2108 }
2109
2110 // Render the entire picture using new layers
robertphillipsc26d9912014-07-10 07:21:27 -07002111 SkPictureReplacementPlayback playback(picture, &replacements, ops.get());
robertphillipsce4dd3d2014-07-07 13:46:35 -07002112
robertphillips0c423322014-07-31 11:02:38 -07002113 playback.draw(mainCanvas, NULL);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002114
robertphillips4ec84da2014-06-24 13:10:43 -07002115 // unlock the layers
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002116 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
robertphillips0c423322014-07-31 11:02:38 -07002117 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(i);
2118
2119 GrCachedLayer* layer = fContext->getLayerCache()->findLayer(picture,
2120 info.fSaveLayerOpID,
2121 info.fRestoreOpID,
2122 info.fCTM);
robertphillips4ec84da2014-06-24 13:10:43 -07002123 fContext->getLayerCache()->unlock(layer);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002124 }
2125
2126 return true;
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00002127}
senorblanco55b6d8b2014-07-30 11:26:46 -07002128
2129SkImageFilter::UniqueIDCache* SkGpuDevice::getImageFilterCache() {
2130 // We always return a transient cache, so it is freed after each
2131 // filter traversal.
2132 return SkImageFilter::UniqueIDCache::Create(kDefaultImageFilterCacheSize);
2133}