blob: b751c46c4a35778496058fcce8f49fbd39b3cbac [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);
bsalomon32d0b3b2014-08-29 07:50:23 -0700137 if (NULL == surface->asRenderTarget() || surface->wasDestroyed()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000138 return NULL;
139 }
bsalomon32d0b3b2014-08-29 07:50:23 -0700140 return SkNEW_ARGS(SkGpuDevice, (surface, flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000141}
142
bsalomon32d0b3b2014-08-29 07:50:23 -0700143SkGpuDevice::SkGpuDevice(GrSurface* surface, unsigned flags) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000144
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000145 fDrawProcs = NULL;
146
bsalomon32d0b3b2014-08-29 07:50:23 -0700147 fContext = SkRef(surface->getContext());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000148
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000149 fNeedClear = flags & kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000150
bsalomon32d0b3b2014-08-29 07:50:23 -0700151 fRenderTarget = SkRef(surface->asRenderTarget());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000152
153 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
154 // on the RT but not vice-versa.
155 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
156 // busting chrome (for a currently unknown reason).
bsalomon32d0b3b2014-08-29 07:50:23 -0700157 surface = fRenderTarget->asTexture();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000158 if (NULL == surface) {
159 surface = fRenderTarget;
160 }
reed@google.combf790232013-12-13 19:45:58 +0000161
reed6c225732014-06-09 19:52:07 -0700162 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef,
163 (surface->info(), surface, SkToBool(flags & kCached_Flag)));
reed89443ab2014-06-27 11:34:19 -0700164 fLegacyBitmap.setInfo(surface->info());
165 fLegacyBitmap.setPixelRef(pr)->unref();
kkinnunenc6cb56f2014-06-24 00:12:27 -0700166
167 bool useDFFonts = !!(flags & kDFFonts_Flag);
168 fMainTextContext = fContext->createTextContext(fRenderTarget, fLeakyProperties, useDFFonts);
169 fFallbackTextContext = SkNEW_ARGS(GrBitmapTextContext, (fContext, fLeakyProperties));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000170}
171
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000172SkGpuDevice* SkGpuDevice::Create(GrContext* context, const SkImageInfo& origInfo,
173 int sampleCount) {
174 if (kUnknown_SkColorType == origInfo.colorType() ||
175 origInfo.width() < 0 || origInfo.height() < 0) {
176 return NULL;
177 }
178
179 SkImageInfo info = origInfo;
180 // TODO: perhas we can loosen this check now that colortype is more detailed
181 // e.g. can we support both RGBA and BGRA here?
182 if (kRGB_565_SkColorType == info.colorType()) {
183 info.fAlphaType = kOpaque_SkAlphaType; // force this setting
184 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000185 info.fColorType = kN32_SkColorType;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000186 if (kOpaque_SkAlphaType != info.alphaType()) {
187 info.fAlphaType = kPremul_SkAlphaType; // force this setting
188 }
189 }
190
191 GrTextureDesc desc;
192 desc.fFlags = kRenderTarget_GrTextureFlagBit;
193 desc.fWidth = info.width();
194 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000195 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000196 desc.fSampleCnt = sampleCount;
197
198 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
199 if (!texture.get()) {
200 return NULL;
201 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000202
bsalomon32d0b3b2014-08-29 07:50:23 -0700203 return SkNEW_ARGS(SkGpuDevice, (texture.get()));
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000204}
205
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000206SkGpuDevice::~SkGpuDevice() {
207 if (fDrawProcs) {
208 delete fDrawProcs;
209 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000210
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000211 delete fMainTextContext;
212 delete fFallbackTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000213
214 // The GrContext takes a ref on the target. We don't want to cause the render
215 // target to be unnecessarily kept alive.
216 if (fContext->getRenderTarget() == fRenderTarget) {
217 fContext->setRenderTarget(NULL);
218 }
219
220 if (fContext->getClip() == &fClipData) {
221 fContext->setClip(NULL);
222 }
223
bsalomon32d0b3b2014-08-29 07:50:23 -0700224 fRenderTarget->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000225 fContext->unref();
226}
227
228///////////////////////////////////////////////////////////////////////////////
229
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000230bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
231 int x, int y) {
232 DO_DEFERRED_CLEAR();
233
234 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000235 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000236 if (kUnknown_GrPixelConfig == config) {
237 return false;
238 }
239
240 uint32_t flags = 0;
241 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
242 flags = GrContext::kUnpremul_PixelOpsFlag;
243 }
244 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
245 config, dstPixels, dstRowBytes, flags);
246}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000247
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000248bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
249 int x, int y) {
250 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000251 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000252 if (kUnknown_GrPixelConfig == config) {
253 return false;
254 }
255 uint32_t flags = 0;
256 if (kUnpremul_SkAlphaType == info.alphaType()) {
257 flags = GrContext::kUnpremul_PixelOpsFlag;
258 }
259 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
260
261 // need to bump our genID for compatibility with clients that "know" we have a bitmap
reed89443ab2014-06-27 11:34:19 -0700262 fLegacyBitmap.notifyPixelsChanged();
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000263
264 return true;
265}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000266
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000267const SkBitmap& SkGpuDevice::onAccessBitmap() {
268 DO_DEFERRED_CLEAR();
reed89443ab2014-06-27 11:34:19 -0700269 return fLegacyBitmap;
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000270}
271
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000272void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
273 INHERITED::onAttachToCanvas(canvas);
274
275 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
276 fClipData.fClipStack = canvas->getClipStack();
277}
278
279void SkGpuDevice::onDetachFromCanvas() {
280 INHERITED::onDetachFromCanvas();
281 fClipData.fClipStack = NULL;
282}
283
284// call this every draw call, to ensure that the context reflects our state,
285// and not the state from some other canvas/device
286void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
287 SkASSERT(NULL != fClipData.fClipStack);
288
289 fContext->setRenderTarget(fRenderTarget);
290
291 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
292
293 if (forceIdentity) {
294 fContext->setIdentityMatrix();
295 } else {
296 fContext->setMatrix(*draw.fMatrix);
297 }
298 fClipData.fOrigin = this->getOrigin();
299
300 fContext->setClip(&fClipData);
301
302 DO_DEFERRED_CLEAR();
303}
304
305GrRenderTarget* SkGpuDevice::accessRenderTarget() {
306 DO_DEFERRED_CLEAR();
307 return fRenderTarget;
308}
309
310///////////////////////////////////////////////////////////////////////////////
311
312SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
313SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
314SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
315SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
316SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
317 shader_type_mismatch);
318SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
319 shader_type_mismatch);
320SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
321SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
322
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000323///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000324
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000325void SkGpuDevice::clear(SkColor color) {
egdanield78a1682014-07-09 10:41:26 -0700326 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::clear", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000327 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
328 fContext->clear(&rect, SkColor2GrColor(color), true, fRenderTarget);
329 fNeedClear = false;
330}
331
332void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
333 CHECK_SHOULD_DRAW(draw, false);
egdanield78a1682014-07-09 10:41:26 -0700334 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPaint", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000335
336 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000337 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000338
339 fContext->drawPaint(grPaint);
340}
341
342// must be in SkCanvas::PointMode order
343static const GrPrimitiveType gPointMode2PrimtiveType[] = {
344 kPoints_GrPrimitiveType,
345 kLines_GrPrimitiveType,
346 kLineStrip_GrPrimitiveType
347};
348
349void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
350 size_t count, const SkPoint pts[], const SkPaint& paint) {
351 CHECK_FOR_ANNOTATION(paint);
352 CHECK_SHOULD_DRAW(draw, false);
353
354 SkScalar width = paint.getStrokeWidth();
355 if (width < 0) {
356 return;
357 }
358
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000359 if (paint.getPathEffect() && 2 == count && SkCanvas::kLines_PointMode == mode) {
egdaniele61c4112014-06-12 10:24:21 -0700360 GrStrokeInfo strokeInfo(paint, SkPaint::kStroke_Style);
361 GrPaint grPaint;
362 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
363 SkPath path;
364 path.moveTo(pts[0]);
365 path.lineTo(pts[1]);
366 fContext->drawPath(grPaint, path, strokeInfo);
367 return;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000368 }
369
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000370 // we only handle hairlines and paints without path effects or mask filters,
371 // else we let the SkDraw call our drawPath()
372 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
373 draw.drawPoints(mode, count, pts, paint, true);
374 return;
375 }
376
377 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000378 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000379
380 fContext->drawVertices(grPaint,
381 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000382 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000383 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000384 NULL,
385 NULL,
386 NULL,
387 0);
388}
389
390///////////////////////////////////////////////////////////////////////////////
391
392void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
393 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -0700394 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRect", fContext);
395
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000396 CHECK_FOR_ANNOTATION(paint);
397 CHECK_SHOULD_DRAW(draw, false);
398
399 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
400 SkScalar width = paint.getStrokeWidth();
401
402 /*
403 We have special code for hairline strokes, miter-strokes, bevel-stroke
404 and fills. Anything else we just call our path code.
405 */
406 bool usePath = doStroke && width > 0 &&
407 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
408 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
409 // another two reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700410
411 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000412 usePath = true;
413 }
egdanield58a0ba2014-06-11 10:30:05 -0700414
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000415 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
416#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
417 if (doStroke) {
418#endif
419 usePath = true;
420#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
421 } else {
422 usePath = !fContext->getMatrix().preservesRightAngles();
423 }
424#endif
425 }
426 // until we can both stroke and fill rectangles
427 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
428 usePath = true;
429 }
430
egdanield58a0ba2014-06-11 10:30:05 -0700431 GrStrokeInfo strokeInfo(paint);
432
433 const SkPathEffect* pe = paint.getPathEffect();
434 if (!usePath && NULL != pe && !strokeInfo.isDashed()) {
435 usePath = true;
436 }
437
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000438 if (usePath) {
439 SkPath path;
440 path.addRect(rect);
441 this->drawPath(draw, path, paint, NULL, true);
442 return;
443 }
444
445 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000446 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400447
egdanield58a0ba2014-06-11 10:30:05 -0700448 fContext->drawRect(grPaint, rect, &strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000449}
450
451///////////////////////////////////////////////////////////////////////////////
452
453void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
454 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700455 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawRRect", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000456 CHECK_FOR_ANNOTATION(paint);
457 CHECK_SHOULD_DRAW(draw, false);
458
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000459 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000460 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
Mike Klein744fb732014-06-23 15:13:26 -0400461
egdanield58a0ba2014-06-11 10:30:05 -0700462 GrStrokeInfo strokeInfo(paint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000463 if (paint.getMaskFilter()) {
464 // try to hit the fast path for drawing filtered round rects
465
466 SkRRect devRRect;
467 if (rect.transform(fContext->getMatrix(), &devRRect)) {
468 if (devRRect.allCornersCircular()) {
469 SkRect maskRect;
470 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
471 draw.fClip->getBounds(),
472 fContext->getMatrix(),
473 &maskRect)) {
474 SkIRect finalIRect;
475 maskRect.roundOut(&finalIRect);
476 if (draw.fClip->quickReject(finalIRect)) {
477 // clipped out
478 return;
479 }
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000480 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, &grPaint,
egdanield58a0ba2014-06-11 10:30:05 -0700481 strokeInfo.getStrokeRec(),
482 devRRect)) {
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000483 return;
484 }
485 }
486
487 }
488 }
489
490 }
491
egdanield58a0ba2014-06-11 10:30:05 -0700492 bool usePath = false;
493
494 if (paint.getMaskFilter()) {
495 usePath = true;
496 } else {
497 const SkPathEffect* pe = paint.getPathEffect();
498 if (NULL != pe && !strokeInfo.isDashed()) {
499 usePath = true;
500 }
501 }
502
503
504 if (usePath) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000505 SkPath path;
506 path.addRRect(rect);
507 this->drawPath(draw, path, paint, NULL, true);
508 return;
509 }
Mike Klein744fb732014-06-23 15:13:26 -0400510
egdanield58a0ba2014-06-11 10:30:05 -0700511 fContext->drawRRect(grPaint, rect, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000512}
513
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000514void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
515 const SkRRect& inner, const SkPaint& paint) {
516 SkStrokeRec stroke(paint);
517 if (stroke.isFillStyle()) {
518
519 CHECK_FOR_ANNOTATION(paint);
520 CHECK_SHOULD_DRAW(draw, false);
521
522 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000523 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000524
525 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
526 fContext->drawDRRect(grPaint, outer, inner);
527 return;
528 }
529 }
530
531 SkPath path;
532 path.addRRect(outer);
533 path.addRRect(inner);
534 path.setFillType(SkPath::kEvenOdd_FillType);
535
536 this->drawPath(draw, path, paint, NULL, true);
537}
538
539
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000540/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000541
542void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
543 const SkPaint& paint) {
egdanield78a1682014-07-09 10:41:26 -0700544 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawOval", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000545 CHECK_FOR_ANNOTATION(paint);
546 CHECK_SHOULD_DRAW(draw, false);
547
egdanield58a0ba2014-06-11 10:30:05 -0700548 GrStrokeInfo strokeInfo(paint);
549
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000550 bool usePath = false;
551 // some basic reasons we might need to call drawPath...
egdanield58a0ba2014-06-11 10:30:05 -0700552 if (paint.getMaskFilter()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000553 usePath = true;
egdanield58a0ba2014-06-11 10:30:05 -0700554 } else {
555 const SkPathEffect* pe = paint.getPathEffect();
556 if (NULL != pe && !strokeInfo.isDashed()) {
557 usePath = true;
558 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000559 }
560
561 if (usePath) {
562 SkPath path;
563 path.addOval(oval);
564 this->drawPath(draw, path, paint, NULL, true);
565 return;
566 }
567
568 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000569 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000570
egdanield58a0ba2014-06-11 10:30:05 -0700571 fContext->drawOval(grPaint, oval, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000572}
573
574#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000575
576///////////////////////////////////////////////////////////////////////////////
577
578// helpers for applying mask filters
579namespace {
580
581// Draw a mask using the supplied paint. Since the coverage/geometry
582// is already burnt into the mask this boils down to a rect draw.
583// Return true if the mask was successfully drawn.
584bool draw_mask(GrContext* context, const SkRect& maskRect,
585 GrPaint* grp, GrTexture* mask) {
586 GrContext::AutoMatrix am;
587 if (!am.setIdentity(context, grp)) {
588 return false;
589 }
590
591 SkMatrix matrix;
592 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
593 matrix.postIDiv(mask->width(), mask->height());
594
595 grp->addCoverageEffect(GrSimpleTextureEffect::Create(mask, matrix))->unref();
596 context->drawRect(*grp, maskRect);
597 return true;
598}
599
600bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
reed868074b2014-06-03 10:53:59 -0700601 SkMaskFilter* filter, const SkRegion& clip,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000602 GrPaint* grp, SkPaint::Style style) {
603 SkMask srcM, dstM;
604
605 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
606 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
607 return false;
608 }
609 SkAutoMaskFreeImage autoSrc(srcM.fImage);
610
611 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
612 return false;
613 }
614 // this will free-up dstM when we're done (allocated in filterMask())
615 SkAutoMaskFreeImage autoDst(dstM.fImage);
616
617 if (clip.quickReject(dstM.fBounds)) {
618 return false;
619 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000620
621 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
622 // the current clip (and identity matrix) and GrPaint settings
623 GrTextureDesc desc;
624 desc.fWidth = dstM.fBounds.width();
625 desc.fHeight = dstM.fBounds.height();
626 desc.fConfig = kAlpha_8_GrPixelConfig;
627
628 GrAutoScratchTexture ast(context, desc);
629 GrTexture* texture = ast.texture();
630
631 if (NULL == texture) {
632 return false;
633 }
634 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
635 dstM.fImage, dstM.fRowBytes);
636
637 SkRect maskRect = SkRect::Make(dstM.fBounds);
638
639 return draw_mask(context, maskRect, grp, texture);
640}
641
642// Create a mask of 'devPath' and place the result in 'mask'. Return true on
643// success; false otherwise.
644bool create_mask_GPU(GrContext* context,
645 const SkRect& maskRect,
646 const SkPath& devPath,
egdanield58a0ba2014-06-11 10:30:05 -0700647 const GrStrokeInfo& strokeInfo,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000648 bool doAA,
649 GrAutoScratchTexture* mask) {
650 GrTextureDesc desc;
651 desc.fFlags = kRenderTarget_GrTextureFlagBit;
652 desc.fWidth = SkScalarCeilToInt(maskRect.width());
653 desc.fHeight = SkScalarCeilToInt(maskRect.height());
654 // We actually only need A8, but it often isn't supported as a
655 // render target so default to RGBA_8888
656 desc.fConfig = kRGBA_8888_GrPixelConfig;
657 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
658 desc.fConfig = kAlpha_8_GrPixelConfig;
659 }
660
661 mask->set(context, desc);
662 if (NULL == mask->texture()) {
663 return false;
664 }
665
666 GrTexture* maskTexture = mask->texture();
667 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
668
669 GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget());
670 GrContext::AutoClip ac(context, clipRect);
671
672 context->clear(NULL, 0x0, true);
673
674 GrPaint tempPaint;
675 if (doAA) {
676 tempPaint.setAntiAlias(true);
677 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
678 // blend coeff of zero requires dual source blending support in order
679 // to properly blend partially covered pixels. This means the AA
680 // code path may not be taken. So we use a dst blend coeff of ISA. We
681 // could special case AA draws to a dst surface with known alpha=0 to
682 // use a zero dst coeff when dual source blending isn't available.
683 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
684 }
685
686 GrContext::AutoMatrix am;
687
688 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
689 SkMatrix translate;
690 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
691 am.set(context, translate);
egdanield58a0ba2014-06-11 10:30:05 -0700692 context->drawPath(tempPaint, devPath, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000693 return true;
694}
695
696SkBitmap wrap_texture(GrTexture* texture) {
697 SkBitmap result;
reed6c225732014-06-09 19:52:07 -0700698 result.setInfo(texture->info());
699 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (result.info(), texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000700 return result;
701}
702
703};
704
705void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
706 const SkPaint& paint, const SkMatrix* prePathMatrix,
707 bool pathIsMutable) {
708 CHECK_FOR_ANNOTATION(paint);
709 CHECK_SHOULD_DRAW(draw, false);
egdanield78a1682014-07-09 10:41:26 -0700710 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPath", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000711
712 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +0000713 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000714
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000715 // If we have a prematrix, apply it to the path, optimizing for the case
716 // where the original path can in fact be modified in place (even though
717 // its parameter type is const).
718 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000719 SkTLazy<SkPath> tmpPath;
720 SkTLazy<SkPath> effectPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000721
722 if (prePathMatrix) {
723 SkPath* result = pathPtr;
724
725 if (!pathIsMutable) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000726 result = tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000727 pathIsMutable = true;
728 }
729 // should I push prePathMatrix on our MV stack temporarily, instead
730 // of applying it here? See SkDraw.cpp
731 pathPtr->transform(*prePathMatrix, result);
732 pathPtr = result;
733 }
734 // at this point we're done with prePathMatrix
735 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
736
egdanield58a0ba2014-06-11 10:30:05 -0700737 GrStrokeInfo strokeInfo(paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000738 SkPathEffect* pathEffect = paint.getPathEffect();
739 const SkRect* cullRect = NULL; // TODO: what is our bounds?
egdanield58a0ba2014-06-11 10:30:05 -0700740 SkStrokeRec* strokePtr = strokeInfo.getStrokeRecPtr();
741 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, strokePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000742 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000743 pathPtr = effectPath.get();
744 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700745 strokeInfo.removeDash();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000746 }
747
egdanield58a0ba2014-06-11 10:30:05 -0700748 const SkStrokeRec& stroke = strokeInfo.getStrokeRec();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000749 if (paint.getMaskFilter()) {
750 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000751 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
752 if (stroke.applyToPath(strokedPath, *pathPtr)) {
753 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000754 pathIsMutable = true;
egdanield58a0ba2014-06-11 10:30:05 -0700755 strokeInfo.setFillStyle();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000756 }
757 }
758
759 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000760 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000761
762 // transform the path into device space
763 pathPtr->transform(fContext->getMatrix(), devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000764
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000765 SkRect maskRect;
766 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
767 draw.fClip->getBounds(),
768 fContext->getMatrix(),
769 &maskRect)) {
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000770 // The context's matrix may change while creating the mask, so save the CTM here to
771 // pass to filterMaskGPU.
772 const SkMatrix ctm = fContext->getMatrix();
773
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000774 SkIRect finalIRect;
775 maskRect.roundOut(&finalIRect);
776 if (draw.fClip->quickReject(finalIRect)) {
777 // clipped out
778 return;
779 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000780
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000781 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint,
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000782 stroke, *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000783 // the mask filter was able to draw itself directly, so there's nothing
784 // left to do.
785 return;
786 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000787
788 GrAutoScratchTexture mask;
789
egdanield58a0ba2014-06-11 10:30:05 -0700790 if (create_mask_GPU(fContext, maskRect, *devPathPtr, strokeInfo,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000791 grPaint.isAntiAlias(), &mask)) {
792 GrTexture* filtered;
793
commit-bot@chromium.org41bf9302014-01-08 22:25:53 +0000794 if (paint.getMaskFilter()->filterMaskGPU(mask.texture(),
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000795 ctm, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000796 // filterMaskGPU gives us ownership of a ref to the result
797 SkAutoTUnref<GrTexture> atu(filtered);
798
799 // If the scratch texture that we used as the filter src also holds the filter
800 // result then we must detach so that this texture isn't recycled for a later
801 // draw.
802 if (filtered == mask.texture()) {
803 mask.detach();
804 filtered->unref(); // detach transfers GrAutoScratchTexture's ref to us.
805 }
806
807 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
808 // This path is completely drawn
809 return;
810 }
811 }
812 }
813 }
814
815 // draw the mask on the CPU - this is a fallthrough path in case the
816 // GPU path fails
817 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
818 SkPaint::kFill_Style;
egdanield58a0ba2014-06-11 10:30:05 -0700819 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(),
820 *draw.fClip, &grPaint, style);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000821 return;
822 }
823
egdanield58a0ba2014-06-11 10:30:05 -0700824 fContext->drawPath(grPaint, *pathPtr, strokeInfo);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000825}
826
827static const int kBmpSmallTileSize = 1 << 10;
828
829static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
830 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
831 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
832 return tilesX * tilesY;
833}
834
835static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
836 if (maxTileSize <= kBmpSmallTileSize) {
837 return maxTileSize;
838 }
839
840 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
841 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
842
843 maxTileTotalTileSize *= maxTileSize * maxTileSize;
844 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
845
846 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
847 return kBmpSmallTileSize;
848 } else {
849 return maxTileSize;
850 }
851}
852
853// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
854// pixels from the bitmap are necessary.
855static void determine_clipped_src_rect(const GrContext* context,
856 const SkBitmap& bitmap,
857 const SkRect* srcRectPtr,
858 SkIRect* clippedSrcIRect) {
859 const GrClipData* clip = context->getClip();
860 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
861 SkMatrix inv;
862 if (!context->getMatrix().invert(&inv)) {
863 clippedSrcIRect->setEmpty();
864 return;
865 }
866 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
867 inv.mapRect(&clippedSrcRect);
868 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000869 // we've setup src space 0,0 to map to the top left of the src rect.
870 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000871 if (!clippedSrcRect.intersect(*srcRectPtr)) {
872 clippedSrcIRect->setEmpty();
873 return;
874 }
875 }
876 clippedSrcRect.roundOut(clippedSrcIRect);
877 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
878 if (!clippedSrcIRect->intersect(bmpBounds)) {
879 clippedSrcIRect->setEmpty();
880 }
881}
882
883bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
884 const GrTextureParams& params,
885 const SkRect* srcRectPtr,
886 int maxTileSize,
887 int* tileSize,
888 SkIRect* clippedSrcRect) const {
889 // if bitmap is explictly texture backed then just use the texture
890 if (NULL != bitmap.getTexture()) {
891 return false;
892 }
893
894 // if it's larger than the max tile size, then we have no choice but tiling.
895 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
896 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
897 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
898 return true;
899 }
900
901 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
902 return false;
903 }
904
905 // if the entire texture is already in our cache then no reason to tile it
906 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
907 return false;
908 }
909
910 // At this point we know we could do the draw by uploading the entire bitmap
911 // as a texture. However, if the texture would be large compared to the
912 // cache size and we don't require most of it for this draw then tile to
913 // reduce the amount of upload and cache spill.
914
915 // assumption here is that sw bitmap size is a good proxy for its size as
916 // a texture
917 size_t bmpSize = bitmap.getSize();
918 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000919 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000920 if (bmpSize < cacheSize / 2) {
921 return false;
922 }
923
924 // Figure out how much of the src we will need based on the src rect and clipping.
925 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
926 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
927 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
928 kBmpSmallTileSize * kBmpSmallTileSize;
929
930 return usedTileBytes < 2 * bmpSize;
931}
932
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000933void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000934 const SkBitmap& bitmap,
935 const SkMatrix& m,
936 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000937 SkMatrix concat;
938 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
939 if (!m.isIdentity()) {
940 concat.setConcat(*draw->fMatrix, m);
941 draw.writable()->fMatrix = &concat;
942 }
943 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000944}
945
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000946// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000947// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
948// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000949static inline void clamped_outset_with_offset(SkIRect* iRect,
950 int outset,
951 SkPoint* offset,
952 const SkIRect& clamp) {
953 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000954
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000955 int leftClampDelta = clamp.fLeft - iRect->fLeft;
956 if (leftClampDelta > 0) {
957 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000958 iRect->fLeft = clamp.fLeft;
959 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000960 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000961 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000962
963 int topClampDelta = clamp.fTop - iRect->fTop;
964 if (topClampDelta > 0) {
965 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000966 iRect->fTop = clamp.fTop;
967 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000968 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000969 }
970
971 if (iRect->fRight > clamp.fRight) {
972 iRect->fRight = clamp.fRight;
973 }
974 if (iRect->fBottom > clamp.fBottom) {
975 iRect->fBottom = clamp.fBottom;
976 }
977}
978
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +0000979static bool has_aligned_samples(const SkRect& srcRect,
980 const SkRect& transformedRect) {
981 // detect pixel disalignment
982 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
983 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
984 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
985 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
986 SkScalarAbs(transformedRect.width() - srcRect.width()) <
987 COLOR_BLEED_TOLERANCE &&
988 SkScalarAbs(transformedRect.height() - srcRect.height()) <
989 COLOR_BLEED_TOLERANCE) {
990 return true;
991 }
992 return false;
993}
994
995static bool may_color_bleed(const SkRect& srcRect,
996 const SkRect& transformedRect,
997 const SkMatrix& m) {
998 // Only gets called if has_aligned_samples returned false.
999 // So we can assume that sampling is axis aligned but not texel aligned.
1000 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
1001 SkRect innerSrcRect(srcRect), innerTransformedRect,
1002 outerTransformedRect(transformedRect);
1003 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1004 m.mapRect(&innerTransformedRect, innerSrcRect);
1005
1006 // The gap between outerTransformedRect and innerTransformedRect
1007 // represents the projection of the source border area, which is
1008 // problematic for color bleeding. We must check whether any
1009 // destination pixels sample the border area.
1010 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1011 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1012 SkIRect outer, inner;
1013 outerTransformedRect.round(&outer);
1014 innerTransformedRect.round(&inner);
1015 // If the inner and outer rects round to the same result, it means the
1016 // border does not overlap any pixel centers. Yay!
1017 return inner != outer;
1018}
1019
1020static bool needs_texture_domain(const SkBitmap& bitmap,
1021 const SkRect& srcRect,
1022 GrTextureParams &params,
1023 const SkMatrix& contextMatrix,
1024 bool bicubic) {
1025 bool needsTextureDomain = false;
1026
1027 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
1028 // Need texture domain if drawing a sub rect
1029 needsTextureDomain = srcRect.width() < bitmap.width() ||
1030 srcRect.height() < bitmap.height();
1031 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
1032 // sampling is axis-aligned
1033 SkRect transformedRect;
1034 contextMatrix.mapRect(&transformedRect, srcRect);
1035
1036 if (has_aligned_samples(srcRect, transformedRect)) {
1037 params.setFilterMode(GrTextureParams::kNone_FilterMode);
1038 needsTextureDomain = false;
1039 } else {
1040 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
1041 }
1042 }
1043 }
1044 return needsTextureDomain;
1045}
1046
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001047void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1048 const SkBitmap& bitmap,
1049 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001050 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001051 const SkPaint& paint,
1052 SkCanvas::DrawBitmapRectFlags flags) {
1053 CHECK_SHOULD_DRAW(draw, false);
1054
1055 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001056 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001057 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1058 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001059 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001060 SkScalar w = SkIntToScalar(bitmap.width());
1061 SkScalar h = SkIntToScalar(bitmap.height());
1062 dstSize.fWidth = w;
1063 dstSize.fHeight = h;
1064 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001065 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001066 } else {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001067 SkASSERT(NULL != dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001068 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001069 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001070 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1071 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1072 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1073 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001074 }
1075
1076 if (paint.getMaskFilter()){
1077 // Convert the bitmap to a shader so that the rect can be drawn
1078 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001079 SkBitmap tmp; // subset of bitmap, if necessary
1080 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001081 SkMatrix localM;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001082 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001083 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1084 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1085 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001086 // In bleed mode we position and trim the bitmap based on the src rect which is
1087 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1088 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1089 // compensate.
1090 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1091 SkIRect iSrc;
1092 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001093
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001094 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1095 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001096
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001097 if (!bitmap.extractSubset(&tmp, iSrc)) {
1098 return; // extraction failed
1099 }
1100 bitmapPtr = &tmp;
1101 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001102
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001103 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001104 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001105 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001106 } else {
1107 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001108 }
1109
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001110 SkPaint paintWithShader(paint);
1111 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001112 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001113 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1114 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001115
1116 return;
1117 }
1118
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001119 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1120 // the view matrix rather than a local matrix.
1121 SkMatrix m;
1122 m.setScale(dstSize.fWidth / srcRect.width(),
1123 dstSize.fHeight / srcRect.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001124 fContext->concatMatrix(m);
1125
1126 GrTextureParams params;
1127 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1128 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001129
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001130 bool doBicubic = false;
1131
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001132 switch(paintFilterLevel) {
1133 case SkPaint::kNone_FilterLevel:
1134 textureFilterMode = GrTextureParams::kNone_FilterMode;
1135 break;
1136 case SkPaint::kLow_FilterLevel:
1137 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1138 break;
1139 case SkPaint::kMedium_FilterLevel:
commit-bot@chromium.org18786512014-05-20 14:53:45 +00001140 if (fContext->getMatrix().getMinScale() < SK_Scalar1) {
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001141 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1142 } else {
1143 // Don't trigger MIP level generation unnecessarily.
1144 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1145 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001146 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001147 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001148 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001149 doBicubic =
1150 GrBicubicEffect::ShouldUseBicubic(fContext->getMatrix(), &textureFilterMode);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001151 break;
1152 default:
1153 SkErrorInternals::SetError( kInvalidPaint_SkError,
1154 "Sorry, I don't understand the filtering "
1155 "mode you asked for. Falling back to "
1156 "MIPMaps.");
1157 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1158 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001159 }
1160
commit-bot@chromium.org9927bd32014-05-20 17:51:13 +00001161 int tileFilterPad;
1162 if (doBicubic) {
1163 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1164 } else if (GrTextureParams::kNone_FilterMode == textureFilterMode) {
1165 tileFilterPad = 0;
1166 } else {
1167 tileFilterPad = 1;
1168 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001169 params.setFilterMode(textureFilterMode);
1170
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001171 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001172 int tileSize;
1173
1174 SkIRect clippedSrcRect;
1175 if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize,
1176 &clippedSrcRect)) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001177 this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize,
1178 doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001179 } else {
1180 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001181 bool needsTextureDomain = needs_texture_domain(bitmap,
1182 srcRect,
1183 params,
1184 fContext->getMatrix(),
1185 doBicubic);
1186 this->internalDrawBitmap(bitmap,
1187 srcRect,
1188 params,
1189 paint,
1190 flags,
1191 doBicubic,
1192 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001193 }
1194}
1195
1196// Break 'bitmap' into several tiles to draw it since it has already
1197// been determined to be too large to fit in VRAM
1198void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
1199 const SkRect& srcRect,
1200 const SkIRect& clippedSrcIRect,
1201 const GrTextureParams& params,
1202 const SkPaint& paint,
1203 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001204 int tileSize,
1205 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001206 // The following pixel lock is technically redundant, but it is desirable
1207 // to lock outside of the tile loop to prevent redecoding the whole image
1208 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1209 // is larger than the limit of the discardable memory pool.
1210 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001211 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1212
1213 int nx = bitmap.width() / tileSize;
1214 int ny = bitmap.height() / tileSize;
1215 for (int x = 0; x <= nx; x++) {
1216 for (int y = 0; y <= ny; y++) {
1217 SkRect tileR;
1218 tileR.set(SkIntToScalar(x * tileSize),
1219 SkIntToScalar(y * tileSize),
1220 SkIntToScalar((x + 1) * tileSize),
1221 SkIntToScalar((y + 1) * tileSize));
1222
1223 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1224 continue;
1225 }
1226
1227 if (!tileR.intersect(srcRect)) {
1228 continue;
1229 }
1230
1231 SkBitmap tmpB;
1232 SkIRect iTileR;
1233 tileR.roundOut(&iTileR);
1234 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1235 SkIntToScalar(iTileR.fTop));
1236
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001237 // Adjust the context matrix to draw at the right x,y in device space
1238 SkMatrix tmpM;
1239 GrContext::AutoMatrix am;
1240 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
1241 am.setPreConcat(fContext, tmpM);
1242
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001243 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001244 SkIRect iClampRect;
1245
1246 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1247 // In bleed mode we want to always expand the tile on all edges
1248 // but stay within the bitmap bounds
1249 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1250 } else {
1251 // In texture-domain/clamp mode we only want to expand the
1252 // tile on edges interior to "srcRect" (i.e., we want to
1253 // not bleed across the original clamped edges)
1254 srcRect.roundOut(&iClampRect);
1255 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001256 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1257 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001258 }
1259
1260 if (bitmap.extractSubset(&tmpB, iTileR)) {
1261 // now offset it to make it "local" to our tmp bitmap
1262 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001263 GrTextureParams paramsTemp = params;
1264 bool needsTextureDomain = needs_texture_domain(bitmap,
1265 srcRect,
1266 paramsTemp,
1267 fContext->getMatrix(),
1268 bicubic);
1269 this->internalDrawBitmap(tmpB,
1270 tileR,
1271 paramsTemp,
1272 paint,
1273 flags,
1274 bicubic,
1275 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001276 }
1277 }
1278 }
1279}
1280
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001281
1282/*
1283 * This is called by drawBitmap(), which has to handle images that may be too
1284 * large to be represented by a single texture.
1285 *
1286 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1287 * and that non-texture portion of the GrPaint has already been setup.
1288 */
1289void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
1290 const SkRect& srcRect,
1291 const GrTextureParams& params,
1292 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001293 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001294 bool bicubic,
1295 bool needsTextureDomain) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001296 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1297 bitmap.height() <= fContext->getMaxTextureSize());
1298
1299 GrTexture* texture;
1300 SkAutoCachedTexture act(this, bitmap, &params, &texture);
1301 if (NULL == texture) {
1302 return;
1303 }
1304
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001305 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001306 SkRect paintRect;
1307 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1308 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1309 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1310 SkScalarMul(srcRect.fTop, hInv),
1311 SkScalarMul(srcRect.fRight, wInv),
1312 SkScalarMul(srcRect.fBottom, hInv));
1313
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001314 SkRect textureDomain = SkRect::MakeEmpty();
bsalomon83d081a2014-07-08 09:56:10 -07001315 SkAutoTUnref<GrEffect> effect;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001316 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001317 // Use a constrained texture domain to avoid color bleeding
1318 SkScalar left, top, right, bottom;
1319 if (srcRect.width() > SK_Scalar1) {
1320 SkScalar border = SK_ScalarHalf / texture->width();
1321 left = paintRect.left() + border;
1322 right = paintRect.right() - border;
1323 } else {
1324 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1325 }
1326 if (srcRect.height() > SK_Scalar1) {
1327 SkScalar border = SK_ScalarHalf / texture->height();
1328 top = paintRect.top() + border;
1329 bottom = paintRect.bottom() - border;
1330 } else {
1331 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1332 }
1333 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001334 if (bicubic) {
1335 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
1336 } else {
1337 effect.reset(GrTextureDomainEffect::Create(texture,
1338 SkMatrix::I(),
1339 textureDomain,
1340 GrTextureDomain::kClamp_Mode,
1341 params.filterMode()));
1342 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001343 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001344 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1345 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
1346 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001347 } else {
1348 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
1349 }
1350
1351 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1352 // the rest from the SkPaint.
1353 GrPaint grPaint;
1354 grPaint.addColorEffect(effect);
reed0689d7b2014-06-14 05:30:20 -07001355 bool alphaOnly = !(kAlpha_8_SkColorType == bitmap.colorType());
bsalomon83d081a2014-07-08 09:56:10 -07001356 GrColor paintColor = (alphaOnly) ? SkColor2GrColorJustAlpha(paint.getColor()) :
1357 SkColor2GrColor(paint.getColor());
1358 SkPaint2GrPaintNoShader(this->context(), paint, paintColor, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001359
bsalomon01c8da12014-08-04 09:21:30 -07001360 fContext->drawRectToRect(grPaint, dstRect, paintRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001361}
1362
1363static bool filter_texture(SkBaseDevice* device, GrContext* context,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001364 GrTexture* texture, const SkImageFilter* filter,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001365 int w, int h, const SkImageFilter::Context& ctx,
1366 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001367 SkASSERT(filter);
1368 SkDeviceImageFilterProxy proxy(device);
1369
1370 if (filter->canFilterImageGPU()) {
1371 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1372 // filter. Also set the clip wide open and the matrix to identity.
1373 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001374 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001375 } else {
1376 return false;
1377 }
1378}
1379
1380void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1381 int left, int top, const SkPaint& paint) {
1382 // drawSprite is defined to be in device coords.
1383 CHECK_SHOULD_DRAW(draw, true);
1384
1385 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1386 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1387 return;
1388 }
1389
1390 int w = bitmap.width();
1391 int h = bitmap.height();
1392
1393 GrTexture* texture;
1394 // draw sprite uses the default texture params
1395 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
1396
1397 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001398 // This bitmap will own the filtered result as a texture.
1399 SkBitmap filteredBitmap;
1400
1401 if (NULL != filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001402 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001403 SkMatrix matrix(*draw.fMatrix);
1404 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001405 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
senorblancobe129b22014-08-08 07:14:35 -07001406 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
senorblanco55b6d8b2014-07-30 11:26:46 -07001407 // This cache is transient, and is freed (along with all its contained
1408 // textures) when it goes out of scope.
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001409 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001410 if (filter_texture(this, fContext, texture, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001411 &offset)) {
1412 texture = (GrTexture*) filteredBitmap.getTexture();
1413 w = filteredBitmap.width();
1414 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001415 left += offset.x();
1416 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001417 } else {
1418 return;
1419 }
1420 }
1421
1422 GrPaint grPaint;
1423 grPaint.addColorTextureEffect(texture, SkMatrix::I());
1424
dandov9de5b512014-06-10 14:38:28 -07001425 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1426 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001427
1428 fContext->drawRectToRect(grPaint,
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001429 SkRect::MakeXYWH(SkIntToScalar(left),
1430 SkIntToScalar(top),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001431 SkIntToScalar(w),
1432 SkIntToScalar(h)),
1433 SkRect::MakeXYWH(0,
1434 0,
1435 SK_Scalar1 * w / texture->width(),
1436 SK_Scalar1 * h / texture->height()));
1437}
1438
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001439void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001440 const SkRect* src, const SkRect& dst,
1441 const SkPaint& paint,
1442 SkCanvas::DrawBitmapRectFlags flags) {
1443 SkMatrix matrix;
1444 SkRect bitmapBounds, tmpSrc;
1445
1446 bitmapBounds.set(0, 0,
1447 SkIntToScalar(bitmap.width()),
1448 SkIntToScalar(bitmap.height()));
1449
1450 // Compute matrix from the two rectangles
1451 if (NULL != src) {
1452 tmpSrc = *src;
1453 } else {
1454 tmpSrc = bitmapBounds;
1455 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001456
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001457 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1458
1459 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1460 if (NULL != src) {
1461 if (!bitmapBounds.contains(tmpSrc)) {
1462 if (!tmpSrc.intersect(bitmapBounds)) {
1463 return; // nothing to draw
1464 }
1465 }
1466 }
1467
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001468 SkRect tmpDst;
1469 matrix.mapRect(&tmpDst, tmpSrc);
1470
1471 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1472 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1473 // Translate so that tempDst's top left is at the origin.
1474 matrix = *origDraw.fMatrix;
1475 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1476 draw.writable()->fMatrix = &matrix;
1477 }
1478 SkSize dstSize;
1479 dstSize.fWidth = tmpDst.width();
1480 dstSize.fHeight = tmpDst.height();
1481
1482 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001483}
1484
1485void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1486 int x, int y, const SkPaint& paint) {
1487 // clear of the source device must occur before CHECK_SHOULD_DRAW
egdanield78a1682014-07-09 10:41:26 -07001488 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawDevice", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001489 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1490 if (dev->fNeedClear) {
1491 // TODO: could check here whether we really need to draw at all
1492 dev->clear(0x0);
1493 }
1494
1495 // drawDevice is defined to be in device coords.
1496 CHECK_SHOULD_DRAW(draw, true);
1497
1498 GrRenderTarget* devRT = dev->accessRenderTarget();
1499 GrTexture* devTex;
1500 if (NULL == (devTex = devRT->asTexture())) {
1501 return;
1502 }
1503
1504 const SkBitmap& bm = dev->accessBitmap(false);
1505 int w = bm.width();
1506 int h = bm.height();
1507
1508 SkImageFilter* filter = paint.getImageFilter();
1509 // This bitmap will own the filtered result as a texture.
1510 SkBitmap filteredBitmap;
1511
1512 if (NULL != filter) {
1513 SkIPoint offset = SkIPoint::Make(0, 0);
1514 SkMatrix matrix(*draw.fMatrix);
1515 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001516 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
senorblanco55b6d8b2014-07-30 11:26:46 -07001517 // This cache is transient, and is freed (along with all its contained
1518 // textures) when it goes out of scope.
senorblancobe129b22014-08-08 07:14:35 -07001519 SkAutoTUnref<SkImageFilter::Cache> cache(getImageFilterCache());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001520 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001521 if (filter_texture(this, fContext, devTex, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001522 &offset)) {
1523 devTex = filteredBitmap.getTexture();
1524 w = filteredBitmap.width();
1525 h = filteredBitmap.height();
1526 x += offset.fX;
1527 y += offset.fY;
1528 } else {
1529 return;
1530 }
1531 }
1532
1533 GrPaint grPaint;
1534 grPaint.addColorTextureEffect(devTex, SkMatrix::I());
1535
dandov9de5b512014-06-10 14:38:28 -07001536 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColorJustAlpha(paint.getColor()),
1537 false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001538
1539 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1540 SkIntToScalar(y),
1541 SkIntToScalar(w),
1542 SkIntToScalar(h));
1543
1544 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1545 // scratch texture).
1546 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1547 SK_Scalar1 * h / devTex->height());
1548
1549 fContext->drawRectToRect(grPaint, dstRect, srcRect);
1550}
1551
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001552bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001553 return filter->canFilterImageGPU();
1554}
1555
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001556bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001557 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001558 SkBitmap* result, SkIPoint* offset) {
1559 // want explicitly our impl, so guard against a subclass of us overriding it
1560 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1561 return false;
1562 }
1563
1564 SkAutoLockPixels alp(src, !src.getTexture());
1565 if (!src.getTexture() && !src.readyToDraw()) {
1566 return false;
1567 }
1568
1569 GrTexture* texture;
1570 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1571 // must be pushed upstack.
1572 SkAutoCachedTexture act(this, src, NULL, &texture);
1573
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001574 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctx,
1575 result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001576}
1577
1578///////////////////////////////////////////////////////////////////////////////
1579
1580// must be in SkCanvas::VertexMode order
1581static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1582 kTriangles_GrPrimitiveType,
1583 kTriangleStrip_GrPrimitiveType,
1584 kTriangleFan_GrPrimitiveType,
1585};
1586
1587void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1588 int vertexCount, const SkPoint vertices[],
1589 const SkPoint texs[], const SkColor colors[],
1590 SkXfermode* xmode,
1591 const uint16_t indices[], int indexCount,
1592 const SkPaint& paint) {
1593 CHECK_SHOULD_DRAW(draw, false);
1594
egdanield78a1682014-07-09 10:41:26 -07001595 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawVertices", fContext);
mtklein533eb782014-08-27 10:39:42 -07001596
dandov32a311b2014-07-15 19:46:26 -07001597 const uint16_t* outIndices;
1598 SkAutoTDeleteArray<uint16_t> outAlloc(NULL);
1599 GrPrimitiveType primType;
1600 GrPaint grPaint;
bsalomona098dd42014-08-06 11:01:44 -07001601
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001602 // If both textures and vertex-colors are NULL, strokes hairlines with the paint's color.
1603 if ((NULL == texs || NULL == paint.getShader()) && NULL == colors) {
mtklein533eb782014-08-27 10:39:42 -07001604
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001605 texs = NULL;
mtklein533eb782014-08-27 10:39:42 -07001606
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001607 SkPaint copy(paint);
1608 copy.setStyle(SkPaint::kStroke_Style);
1609 copy.setStrokeWidth(0);
mtklein533eb782014-08-27 10:39:42 -07001610
dandov32a311b2014-07-15 19:46:26 -07001611 // we ignore the shader if texs is null.
1612 SkPaint2GrPaintNoShader(this->context(), copy, SkColor2GrColor(copy.getColor()),
1613 NULL == colors, &grPaint);
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001614
dandov32a311b2014-07-15 19:46:26 -07001615 primType = kLines_GrPrimitiveType;
1616 int triangleCount = 0;
bsalomona098dd42014-08-06 11:01:44 -07001617 int n = (NULL == indices) ? vertexCount : indexCount;
dandov32a311b2014-07-15 19:46:26 -07001618 switch (vmode) {
1619 case SkCanvas::kTriangles_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001620 triangleCount = n / 3;
dandov32a311b2014-07-15 19:46:26 -07001621 break;
1622 case SkCanvas::kTriangleStrip_VertexMode:
1623 case SkCanvas::kTriangleFan_VertexMode:
bsalomona098dd42014-08-06 11:01:44 -07001624 triangleCount = n - 2;
dandov32a311b2014-07-15 19:46:26 -07001625 break;
1626 }
mtklein533eb782014-08-27 10:39:42 -07001627
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001628 VertState state(vertexCount, indices, indexCount);
1629 VertState::Proc vertProc = state.chooseProc(vmode);
mtklein533eb782014-08-27 10:39:42 -07001630
dandov32a311b2014-07-15 19:46:26 -07001631 //number of indices for lines per triangle with kLines
1632 indexCount = triangleCount * 6;
mtklein533eb782014-08-27 10:39:42 -07001633
dandov32a311b2014-07-15 19:46:26 -07001634 outAlloc.reset(SkNEW_ARRAY(uint16_t, indexCount));
1635 outIndices = outAlloc.get();
1636 uint16_t* auxIndices = outAlloc.get();
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001637 int i = 0;
1638 while (vertProc(&state)) {
dandov32a311b2014-07-15 19:46:26 -07001639 auxIndices[i] = state.f0;
1640 auxIndices[i + 1] = state.f1;
1641 auxIndices[i + 2] = state.f1;
1642 auxIndices[i + 3] = state.f2;
1643 auxIndices[i + 4] = state.f2;
1644 auxIndices[i + 5] = state.f0;
commit-bot@chromium.org559a8832014-05-30 10:08:22 +00001645 i += 6;
1646 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001647 } else {
dandov32a311b2014-07-15 19:46:26 -07001648 outIndices = indices;
1649 primType = gVertexMode2PrimitiveType[vmode];
mtklein533eb782014-08-27 10:39:42 -07001650
dandov32a311b2014-07-15 19:46:26 -07001651 if (NULL == texs || NULL == paint.getShader()) {
1652 SkPaint2GrPaintNoShader(this->context(), paint, SkColor2GrColor(paint.getColor()),
1653 NULL == colors, &grPaint);
1654 } else {
1655 SkPaint2GrPaintShader(this->context(), paint, NULL == colors, &grPaint);
1656 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001657 }
1658
mtklein2583b622014-06-04 08:20:41 -07001659#if 0
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001660 if (NULL != xmode && NULL != texs && NULL != colors) {
1661 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1662 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
mtklein2583b622014-06-04 08:20:41 -07001663 return;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001664 }
1665 }
mtklein2583b622014-06-04 08:20:41 -07001666#endif
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001667
1668 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1669 if (NULL != colors) {
1670 // need to convert byte order and from non-PM to PM
1671 convertedColors.reset(vertexCount);
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001672 SkColor color;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001673 for (int i = 0; i < vertexCount; ++i) {
commit-bot@chromium.orgc93e6812014-05-23 08:09:26 +00001674 color = colors[i];
1675 if (paint.getAlpha() != 255) {
1676 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paint.getAlpha()));
1677 }
1678 convertedColors[i] = SkColor2GrColor(color);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001679 }
1680 colors = convertedColors.get();
1681 }
1682 fContext->drawVertices(grPaint,
dandov32a311b2014-07-15 19:46:26 -07001683 primType,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001684 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001685 vertices,
1686 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001687 colors,
dandov32a311b2014-07-15 19:46:26 -07001688 outIndices,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001689 indexCount);
1690}
1691
1692///////////////////////////////////////////////////////////////////////////////
1693
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001694void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1695 size_t byteLength, SkScalar x, SkScalar y,
1696 const SkPaint& paint) {
1697 CHECK_SHOULD_DRAW(draw, false);
egdanield78a1682014-07-09 10:41:26 -07001698 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001699
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001700 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001701 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001702 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001703
1704 SkDEBUGCODE(this->validate();)
1705
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001706 fMainTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
1707 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001708 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001709 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001710
1711 SkDEBUGCODE(this->validate();)
1712
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001713 fFallbackTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001714 } else {
1715 // this guy will just call our drawPath()
1716 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001717 }
1718}
1719
1720void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1721 size_t byteLength, const SkScalar pos[],
1722 SkScalar constY, int scalarsPerPos,
1723 const SkPaint& paint) {
egdanielbbcb38d2014-06-19 10:19:29 -07001724 GR_CREATE_TRACE_MARKER_CONTEXT("SkGpuDevice::drawPosText", fContext);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001725 CHECK_SHOULD_DRAW(draw, false);
1726
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001727 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001728 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001729 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001730
1731 SkDEBUGCODE(this->validate();)
1732
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001733 fMainTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001734 constY, scalarsPerPos);
1735 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001736 GrPaint grPaint;
commit-bot@chromium.org3595f882014-05-19 19:35:57 +00001737 SkPaint2GrPaintShader(this->context(), paint, true, &grPaint);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001738
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001739 SkDEBUGCODE(this->validate();)
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001740
1741 fFallbackTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001742 constY, scalarsPerPos);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001743 } else {
1744 draw.drawPosText_asPaths((const char*)text, byteLength, pos, constY,
1745 scalarsPerPos, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001746 }
1747}
1748
1749void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1750 size_t len, const SkPath& path,
1751 const SkMatrix* m, const SkPaint& paint) {
1752 CHECK_SHOULD_DRAW(draw, false);
1753
1754 SkASSERT(draw.fDevice == this);
1755 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1756}
1757
1758///////////////////////////////////////////////////////////////////////////////
1759
1760bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1761 if (!paint.isLCDRenderText()) {
1762 // we're cool with the paint as is
1763 return false;
1764 }
1765
1766 if (paint.getShader() ||
1767 paint.getXfermode() || // unless its srcover
1768 paint.getMaskFilter() ||
1769 paint.getRasterizer() ||
1770 paint.getColorFilter() ||
1771 paint.getPathEffect() ||
1772 paint.isFakeBoldText() ||
1773 paint.getStyle() != SkPaint::kFill_Style) {
1774 // turn off lcd
1775 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1776 flags->fHinting = paint.getHinting();
1777 return true;
1778 }
1779 // we're cool with the paint as is
1780 return false;
1781}
1782
1783void SkGpuDevice::flush() {
1784 DO_DEFERRED_CLEAR();
1785 fContext->resolveRenderTarget(fRenderTarget);
1786}
1787
1788///////////////////////////////////////////////////////////////////////////////
1789
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001790SkBaseDevice* SkGpuDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001791 GrTextureDesc desc;
1792 desc.fConfig = fRenderTarget->config();
1793 desc.fFlags = kRenderTarget_GrTextureFlagBit;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001794 desc.fWidth = info.width();
1795 desc.fHeight = info.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001796 desc.fSampleCnt = fRenderTarget->numSamples();
1797
1798 SkAutoTUnref<GrTexture> texture;
1799 // Skia's convention is to only clear a device if it is non-opaque.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001800 unsigned flags = info.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001801
1802#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1803 // layers are never draw in repeat modes, so we can request an approx
1804 // match and ignore any padding.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001805 flags |= kCached_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001806 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1807 GrContext::kApprox_ScratchTexMatch :
1808 GrContext::kExact_ScratchTexMatch;
1809 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
1810#else
1811 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1812#endif
1813 if (NULL != texture.get()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001814 return SkGpuDevice::Create(texture, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001815 } else {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001816 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1817 info.width(), info.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001818 return NULL;
1819 }
1820}
1821
reed@google.com76f10a32014-02-05 15:32:21 +00001822SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info) {
1823 return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples());
1824}
1825
robertphillips9b14f262014-06-04 05:40:44 -07001826void SkGpuDevice::EXPERIMENTAL_optimize(const SkPicture* picture) {
robertphillipsd771f6b2014-07-22 10:18:06 -07001827 fContext->getLayerCache()->processDeletedPictures();
1828
robertphillipsc019ec42014-08-12 05:35:58 -07001829 if (NULL != picture->fData.get() && !picture->fData->suitableForLayerOptimization()) {
1830 return;
1831 }
1832
robertphillips6617d502014-08-18 09:39:34 -07001833 SkPicture::AccelData::Key key = GrAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001834
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001835 const SkPicture::AccelData* existing = picture->EXPERIMENTAL_getAccelData(key);
1836 if (NULL != existing) {
1837 return;
1838 }
1839
robertphillipsd6283302014-08-27 11:53:28 -07001840 GPUOptimize(picture);
robertphillipsd771f6b2014-07-22 10:18:06 -07001841
1842 fContext->getLayerCache()->trackPicture(picture);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001843}
1844
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001845static void wrap_texture(GrTexture* texture, int width, int height, SkBitmap* result) {
1846 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
commit-bot@chromium.orga3264e52014-05-30 13:26:10 +00001847 result->setInfo(info);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001848 result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
1849}
1850
robertphillips64bf7672014-08-21 13:07:35 -07001851// Return true if any layers are suitable for hoisting
1852bool SkGpuDevice::FindLayersToHoist(const GrAccelData *gpuData,
1853 const SkPicture::OperationList* ops,
mtklein533eb782014-08-27 10:39:42 -07001854 const SkRect& query,
robertphillips64bf7672014-08-21 13:07:35 -07001855 bool* pullForward) {
1856 bool anyHoisted = false;
1857
1858 // Layer hoisting pre-renders the entire layer since it will be cached and potentially
1859 // reused with different clips (e.g., in different tiles). Because of this the
1860 // clip will not be limiting the size of the pre-rendered layer. kSaveLayerMaxSize
1861 // is used to limit which clips are pre-rendered.
1862 static const int kSaveLayerMaxSize = 256;
1863
1864 if (NULL != ops) {
1865 // In this case the picture has been generated with a BBH so we use
1866 // the BBH to limit the pre-rendering to just the layers needed to cover
1867 // the region being drawn
1868 for (int i = 0; i < ops->numOps(); ++i) {
1869 uint32_t offset = ops->offset(i);
1870
1871 // For now we're saving all the layers in the GrAccelData so they
1872 // can be nested. Additionally, the nested layers appear before
1873 // their parent in the list.
1874 for (int j = 0; j < gpuData->numSaveLayers(); ++j) {
1875 const GrAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1876
1877 if (pullForward[j]) {
1878 continue; // already pulling forward
1879 }
1880
1881 if (offset < info.fSaveLayerOpID || offset > info.fRestoreOpID) {
1882 continue; // the op isn't in this range
1883 }
1884
1885 // TODO: once this code is more stable unsuitable layers can
1886 // just be omitted during the optimization stage
1887 if (!info.fValid ||
1888 kSaveLayerMaxSize < info.fSize.fWidth ||
1889 kSaveLayerMaxSize < info.fSize.fHeight ||
1890 info.fIsNested) {
1891 continue; // this layer is unsuitable
1892 }
1893
1894 pullForward[j] = true;
1895 anyHoisted = true;
1896 }
1897 }
1898 } else {
1899 // In this case there is no BBH associated with the picture. Pre-render
1900 // all the layers that intersect the drawn region
1901 for (int j = 0; j < gpuData->numSaveLayers(); ++j) {
1902 const GrAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1903
mtklein533eb782014-08-27 10:39:42 -07001904 SkRect layerRect = SkRect::MakeXYWH(SkIntToScalar(info.fOffset.fX),
1905 SkIntToScalar(info.fOffset.fY),
1906 SkIntToScalar(info.fSize.fWidth),
1907 SkIntToScalar(info.fSize.fHeight));
robertphillips64bf7672014-08-21 13:07:35 -07001908
mtklein533eb782014-08-27 10:39:42 -07001909 if (!SkRect::Intersects(query, layerRect)) {
robertphillips64bf7672014-08-21 13:07:35 -07001910 continue;
1911 }
1912
1913 // TODO: once this code is more stable unsuitable layers can
1914 // just be omitted during the optimization stage
1915 if (!info.fValid ||
1916 kSaveLayerMaxSize < info.fSize.fWidth ||
1917 kSaveLayerMaxSize < info.fSize.fHeight ||
1918 info.fIsNested) {
1919 continue;
1920 }
1921
1922 pullForward[j] = true;
1923 anyHoisted = true;
1924 }
1925 }
1926
1927 return anyHoisted;
1928}
1929
reedd5fa1a42014-08-09 11:08:05 -07001930bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* mainCanvas, const SkPicture* picture,
1931 const SkMatrix* matrix, const SkPaint* paint) {
1932 // todo: should handle these natively
1933 if (matrix || paint) {
1934 return false;
1935 }
1936
robertphillipsd771f6b2014-07-22 10:18:06 -07001937 fContext->getLayerCache()->processDeletedPictures();
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001938
robertphillips6617d502014-08-18 09:39:34 -07001939 SkPicture::AccelData::Key key = GrAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001940
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001941 const SkPicture::AccelData* data = picture->EXPERIMENTAL_getAccelData(key);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001942 if (NULL == data) {
1943 return false;
1944 }
1945
robertphillips6617d502014-08-18 09:39:34 -07001946 const GrAccelData *gpuData = static_cast<const GrAccelData*>(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001947
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001948 if (0 == gpuData->numSaveLayers()) {
1949 return false;
1950 }
1951
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001952 SkAutoTArray<bool> pullForward(gpuData->numSaveLayers());
1953 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1954 pullForward[i] = false;
1955 }
1956
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001957 SkRect clipBounds;
robertphillips0c423322014-07-31 11:02:38 -07001958 if (!mainCanvas->getClipBounds(&clipBounds)) {
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001959 return true;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001960 }
1961
mtklein533eb782014-08-27 10:39:42 -07001962 SkAutoTDelete<const SkPicture::OperationList> ops(
1963 picture->EXPERIMENTAL_getActiveOps(clipBounds));
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001964
mtklein533eb782014-08-27 10:39:42 -07001965 if (!FindLayersToHoist(gpuData, ops.get(), clipBounds, pullForward.get())) {
robertphillips64bf7672014-08-21 13:07:35 -07001966 return false;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001967 }
1968
robertphillipsc26d9912014-07-10 07:21:27 -07001969 SkPictureReplacementPlayback::PlaybackReplacements replacements;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001970
robertphillips0c423322014-07-31 11:02:38 -07001971 SkTDArray<GrCachedLayer*> atlased, nonAtlased;
1972 atlased.setReserve(gpuData->numSaveLayers());
1973
robertphillips4ec84da2014-06-24 13:10:43 -07001974 // Generate the layer and/or ensure it is locked
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001975 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1976 if (pullForward[i]) {
robertphillips6617d502014-08-18 09:39:34 -07001977 const GrAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(i);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001978
robertphillipsd6283302014-08-27 11:53:28 -07001979 GrCachedLayer* layer = fContext->getLayerCache()->findLayerOrCreate(picture->uniqueID(),
1980 info.fSaveLayerOpID,
1981 info.fRestoreOpID,
1982 info.fOriginXform);
robertphillips0c423322014-07-31 11:02:38 -07001983
robertphillipsc26d9912014-07-10 07:21:27 -07001984 SkPictureReplacementPlayback::PlaybackReplacements::ReplacementInfo* layerInfo =
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001985 replacements.push();
robertphillips4ec84da2014-06-24 13:10:43 -07001986 layerInfo->fStart = info.fSaveLayerOpID;
1987 layerInfo->fStop = info.fRestoreOpID;
1988 layerInfo->fPos = info.fOffset;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001989
robertphillips4ec84da2014-06-24 13:10:43 -07001990 GrTextureDesc desc;
1991 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1992 desc.fWidth = info.fSize.fWidth;
1993 desc.fHeight = info.fSize.fHeight;
1994 desc.fConfig = kSkia8888_GrPixelConfig;
1995 // TODO: need to deal with sample count
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001996
robertphillips6f294af2014-08-18 08:50:03 -07001997 bool needsRendering = fContext->getLayerCache()->lock(layer, desc,
1998 info.fHasNestedLayers || info.fIsNested);
robertphillips952841b2014-06-30 08:26:50 -07001999 if (NULL == layer->texture()) {
robertphillips4ec84da2014-06-24 13:10:43 -07002000 continue;
2001 }
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002002
robertphillips4ec84da2014-06-24 13:10:43 -07002003 layerInfo->fBM = SkNEW(SkBitmap); // fBM is allocated so ReplacementInfo can be POD
mtklein533eb782014-08-27 10:39:42 -07002004 wrap_texture(layer->texture(),
robertphillips21048b52014-07-15 19:46:35 -07002005 !layer->isAtlased() ? desc.fWidth : layer->texture()->width(),
2006 !layer->isAtlased() ? desc.fHeight : layer->texture()->height(),
robertphillips952841b2014-06-30 08:26:50 -07002007 layerInfo->fBM);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002008
robertphillips4ec84da2014-06-24 13:10:43 -07002009 SkASSERT(info.fPaint);
2010 layerInfo->fPaint = info.fPaint;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002011
robertphillips21048b52014-07-15 19:46:35 -07002012 layerInfo->fSrcRect = SkIRect::MakeXYWH(layer->rect().fLeft,
2013 layer->rect().fTop,
2014 layer->rect().width(),
2015 layer->rect().height());
2016
robertphillips4ec84da2014-06-24 13:10:43 -07002017 if (needsRendering) {
robertphillips21048b52014-07-15 19:46:35 -07002018 if (layer->isAtlased()) {
robertphillips0c423322014-07-31 11:02:38 -07002019 *atlased.append() = layer;
robertphillips952841b2014-06-30 08:26:50 -07002020 } else {
robertphillips0c423322014-07-31 11:02:38 -07002021 *nonAtlased.append() = layer;
robertphillips952841b2014-06-30 08:26:50 -07002022 }
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00002023 }
2024 }
2025 }
2026
robertphillips64bf7672014-08-21 13:07:35 -07002027 this->drawLayers(picture, atlased, nonAtlased);
2028
2029 // Render the entire picture using new layers
2030 SkPictureReplacementPlayback playback(picture, &replacements, ops.get());
2031
2032 playback.draw(mainCanvas, NULL);
2033
2034 this->unlockLayers(picture);
2035
2036 return true;
2037}
2038
2039void SkGpuDevice::drawLayers(const SkPicture* picture,
mtklein533eb782014-08-27 10:39:42 -07002040 const SkTDArray<GrCachedLayer*>& atlased,
robertphillips64bf7672014-08-21 13:07:35 -07002041 const SkTDArray<GrCachedLayer*>& nonAtlased) {
robertphillips0c423322014-07-31 11:02:38 -07002042 // Render the atlased layers that require it
2043 if (atlased.count() > 0) {
2044 // All the atlased layers are rendered into the same GrTexture
2045 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect(
robertphillips64bf7672014-08-21 13:07:35 -07002046 atlased[0]->texture()->asRenderTarget(),
2047 SkSurface::kStandard_TextRenderMode,
2048 SkSurface::kDontClear_RenderTargetFlag));
robertphillips0c423322014-07-31 11:02:38 -07002049
2050 SkCanvas* atlasCanvas = surface->getCanvas();
2051
2052 SkPaint paint;
2053 paint.setColor(SK_ColorTRANSPARENT);
2054 paint.setXfermode(SkXfermode::Create(SkXfermode::kSrc_Mode))->unref();
2055
2056 for (int i = 0; i < atlased.count(); ++i) {
2057 GrCachedLayer* layer = atlased[i];
2058
2059 atlasCanvas->save();
2060
2061 // Add a rect clip to make sure the rendering doesn't
2062 // extend beyond the boundaries of the atlased sub-rect
2063 SkRect bound = SkRect::MakeXYWH(SkIntToScalar(layer->rect().fLeft),
2064 SkIntToScalar(layer->rect().fTop),
2065 SkIntToScalar(layer->rect().width()),
2066 SkIntToScalar(layer->rect().height()));
2067 atlasCanvas->clipRect(bound);
2068
2069 // Since 'clear' doesn't respect the clip we need to draw a rect
2070 // TODO: ensure none of the atlased layers contain a clear call!
2071 atlasCanvas->drawRect(bound, paint);
2072
2073 // info.fCTM maps the layer's top/left to the origin.
2074 // Since this layer is atlased, the top/left corner needs
2075 // to be offset to the correct location in the backing texture.
2076 atlasCanvas->translate(bound.fLeft, bound.fTop);
2077 atlasCanvas->concat(layer->ctm());
2078
2079 SkPictureRangePlayback rangePlayback(picture,
2080 layer->start(),
2081 layer->stop());
2082 rangePlayback.draw(atlasCanvas, NULL);
2083
2084 atlasCanvas->restore();
2085 }
2086
2087 atlasCanvas->flush();
2088 }
2089
2090 // Render the non-atlased layers that require it
2091 for (int i = 0; i < nonAtlased.count(); ++i) {
2092 GrCachedLayer* layer = nonAtlased[i];
2093
2094 // Each non-atlased layer has its own GrTexture
2095 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect(
robertphillips64bf7672014-08-21 13:07:35 -07002096 layer->texture()->asRenderTarget(),
2097 SkSurface::kStandard_TextRenderMode,
2098 SkSurface::kDontClear_RenderTargetFlag));
robertphillips0c423322014-07-31 11:02:38 -07002099
2100 SkCanvas* layerCanvas = surface->getCanvas();
2101
2102 // Add a rect clip to make sure the rendering doesn't
2103 // extend beyond the boundaries of the atlased sub-rect
2104 SkRect bound = SkRect::MakeXYWH(SkIntToScalar(layer->rect().fLeft),
2105 SkIntToScalar(layer->rect().fTop),
2106 SkIntToScalar(layer->rect().width()),
2107 SkIntToScalar(layer->rect().height()));
2108
2109 layerCanvas->clipRect(bound); // TODO: still useful?
2110
2111 layerCanvas->clear(SK_ColorTRANSPARENT);
2112
2113 layerCanvas->concat(layer->ctm());
2114
2115 SkPictureRangePlayback rangePlayback(picture,
2116 layer->start(),
2117 layer->stop());
2118 rangePlayback.draw(layerCanvas, NULL);
2119
2120 layerCanvas->flush();
2121 }
robertphillips64bf7672014-08-21 13:07:35 -07002122}
robertphillips0c423322014-07-31 11:02:38 -07002123
robertphillips64bf7672014-08-21 13:07:35 -07002124void SkGpuDevice::unlockLayers(const SkPicture* picture) {
2125 SkPicture::AccelData::Key key = GrAccelData::ComputeAccelDataKey();
robertphillipsce4dd3d2014-07-07 13:46:35 -07002126
robertphillips64bf7672014-08-21 13:07:35 -07002127 const SkPicture::AccelData* data = picture->EXPERIMENTAL_getAccelData(key);
2128 SkASSERT(NULL != data);
2129
2130 const GrAccelData *gpuData = static_cast<const GrAccelData*>(data);
2131 SkASSERT(0 != gpuData->numSaveLayers());
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002132
robertphillips4ec84da2014-06-24 13:10:43 -07002133 // unlock the layers
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002134 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
robertphillips6617d502014-08-18 09:39:34 -07002135 const GrAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(i);
robertphillips0c423322014-07-31 11:02:38 -07002136
robertphillips64bf7672014-08-21 13:07:35 -07002137 GrCachedLayer* layer = fContext->getLayerCache()->findLayer(picture->uniqueID(),
robertphillips0c423322014-07-31 11:02:38 -07002138 info.fSaveLayerOpID,
2139 info.fRestoreOpID,
robertphillipsd6283302014-08-27 11:53:28 -07002140 info.fOriginXform);
robertphillips4ec84da2014-06-24 13:10:43 -07002141 fContext->getLayerCache()->unlock(layer);
robertphillips@google.combeb1af22014-05-07 21:31:09 +00002142 }
2143
robertphillips6f294af2014-08-18 08:50:03 -07002144#if DISABLE_CACHING
mtklein533eb782014-08-27 10:39:42 -07002145 // This code completely clears out the atlas. It is required when
robertphillips6f294af2014-08-18 08:50:03 -07002146 // caching is disabled so the atlas doesn't fill up and force more
2147 // free floating layers
2148 fContext->getLayerCache()->purge(picture->uniqueID());
2149
2150 fContext->getLayerCache()->purgeAll();
2151#endif
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00002152}
senorblanco55b6d8b2014-07-30 11:26:46 -07002153
senorblancobe129b22014-08-08 07:14:35 -07002154SkImageFilter::Cache* SkGpuDevice::getImageFilterCache() {
senorblanco55b6d8b2014-07-30 11:26:46 -07002155 // We always return a transient cache, so it is freed after each
2156 // filter traversal.
senorblancobe129b22014-08-08 07:14:35 -07002157 return SkImageFilter::Cache::Create(kDefaultImageFilterCacheSize);
senorblanco55b6d8b2014-07-30 11:26:46 -07002158}