blob: 6543139c836ed02c0ddcf8e70caae14b8cab739a [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.org907fbd52013-12-09 17:03:02 +000011#include "effects/GrTextureDomain.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000012#include "effects/GrSimpleTextureEffect.h"
13
14#include "GrContext.h"
15#include "GrBitmapTextContext.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000016#include "GrDistanceFieldTextContext.h"
robertphillips@google.come930a072014-04-03 00:34:27 +000017#include "GrLayerCache.h"
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +000018#include "GrPictureUtils.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000019
20#include "SkGrTexturePixelRef.h"
21
commit-bot@chromium.org82139702014-03-10 22:53:20 +000022#include "SkBounder.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000023#include "SkDeviceImageFilterProxy.h"
24#include "SkDrawProcs.h"
25#include "SkGlyphCache.h"
26#include "SkImageFilter.h"
commit-bot@chromium.org82139702014-03-10 22:53:20 +000027#include "SkMaskFilter.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000028#include "SkPathEffect.h"
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +000029#include "SkPicture.h"
robertphillips@google.combeb1af22014-05-07 21:31:09 +000030#include "SkPicturePlayback.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000031#include "SkRRect.h"
32#include "SkStroke.h"
reed@google.com76f10a32014-02-05 15:32:21 +000033#include "SkSurface.h"
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +000034#include "SkTLazy.h"
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000035#include "SkUtils.h"
36#include "SkErrorInternals.h"
37
38#define CACHE_COMPATIBLE_DEVICE_TEXTURES 1
39
40#if 0
41 extern bool (*gShouldDrawProc)();
42 #define CHECK_SHOULD_DRAW(draw, forceI) \
43 do { \
44 if (gShouldDrawProc && !gShouldDrawProc()) return; \
45 this->prepareDraw(draw, forceI); \
46 } while (0)
47#else
48 #define CHECK_SHOULD_DRAW(draw, forceI) this->prepareDraw(draw, forceI)
49#endif
50
51// This constant represents the screen alignment criterion in texels for
52// requiring texture domain clamping to prevent color bleeding when drawing
53// a sub region of a larger source image.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000054#define COLOR_BLEED_TOLERANCE 0.001f
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +000055
56#define DO_DEFERRED_CLEAR() \
57 do { \
58 if (fNeedClear) { \
59 this->clear(SK_ColorTRANSPARENT); \
60 } \
61 } while (false) \
62
63///////////////////////////////////////////////////////////////////////////////
64
65#define CHECK_FOR_ANNOTATION(paint) \
66 do { if (paint.getAnnotation()) { return; } } while (0)
67
68///////////////////////////////////////////////////////////////////////////////
69
70
71class SkGpuDevice::SkAutoCachedTexture : public ::SkNoncopyable {
72public:
73 SkAutoCachedTexture()
74 : fDevice(NULL)
75 , fTexture(NULL) {
76 }
77
78 SkAutoCachedTexture(SkGpuDevice* device,
79 const SkBitmap& bitmap,
80 const GrTextureParams* params,
81 GrTexture** texture)
82 : fDevice(NULL)
83 , fTexture(NULL) {
84 SkASSERT(NULL != texture);
85 *texture = this->set(device, bitmap, params);
86 }
87
88 ~SkAutoCachedTexture() {
89 if (NULL != fTexture) {
90 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
91 }
92 }
93
94 GrTexture* set(SkGpuDevice* device,
95 const SkBitmap& bitmap,
96 const GrTextureParams* params) {
97 if (NULL != fTexture) {
98 GrUnlockAndUnrefCachedBitmapTexture(fTexture);
99 fTexture = NULL;
100 }
101 fDevice = device;
102 GrTexture* result = (GrTexture*)bitmap.getTexture();
103 if (NULL == result) {
104 // Cannot return the native texture so look it up in our cache
105 fTexture = GrLockAndRefCachedBitmapTexture(device->context(), bitmap, params);
106 result = fTexture;
107 }
108 return result;
109 }
110
111private:
112 SkGpuDevice* fDevice;
113 GrTexture* fTexture;
114};
115
116///////////////////////////////////////////////////////////////////////////////
117
118struct GrSkDrawProcs : public SkDrawProcs {
119public:
120 GrContext* fContext;
121 GrTextContext* fTextContext;
122 GrFontScaler* fFontScaler; // cached in the skia glyphcache
123};
124
125///////////////////////////////////////////////////////////////////////////////
126
127static SkBitmap::Config grConfig2skConfig(GrPixelConfig config, bool* isOpaque) {
128 switch (config) {
129 case kAlpha_8_GrPixelConfig:
130 *isOpaque = false;
131 return SkBitmap::kA8_Config;
132 case kRGB_565_GrPixelConfig:
133 *isOpaque = true;
134 return SkBitmap::kRGB_565_Config;
135 case kRGBA_4444_GrPixelConfig:
136 *isOpaque = false;
137 return SkBitmap::kARGB_4444_Config;
138 case kSkia8888_GrPixelConfig:
139 // we don't currently have a way of knowing whether
140 // a 8888 is opaque based on the config.
141 *isOpaque = false;
142 return SkBitmap::kARGB_8888_Config;
143 default:
144 *isOpaque = false;
145 return SkBitmap::kNo_Config;
146 }
147}
148
149/*
150 * GrRenderTarget does not know its opaqueness, only its config, so we have
151 * to make conservative guesses when we return an "equivalent" bitmap.
152 */
153static SkBitmap make_bitmap(GrContext* context, GrRenderTarget* renderTarget) {
154 bool isOpaque;
155 SkBitmap::Config config = grConfig2skConfig(renderTarget->config(), &isOpaque);
156
157 SkBitmap bitmap;
158 bitmap.setConfig(config, renderTarget->width(), renderTarget->height(), 0,
159 isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
160 return bitmap;
161}
162
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000163SkGpuDevice* SkGpuDevice::Create(GrSurface* surface, unsigned flags) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000164 SkASSERT(NULL != surface);
165 if (NULL == surface->asRenderTarget() || NULL == surface->getContext()) {
166 return NULL;
167 }
168 if (surface->asTexture()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000169 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asTexture(), flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000170 } else {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000171 return SkNEW_ARGS(SkGpuDevice, (surface->getContext(), surface->asRenderTarget(), flags));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000172 }
173}
174
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000175SkGpuDevice::SkGpuDevice(GrContext* context, GrTexture* texture, unsigned flags)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000176 : SkBitmapDevice(make_bitmap(context, texture->asRenderTarget())) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000177 this->initFromRenderTarget(context, texture->asRenderTarget(), flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000178}
179
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000180SkGpuDevice::SkGpuDevice(GrContext* context, GrRenderTarget* renderTarget, unsigned flags)
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000181 : SkBitmapDevice(make_bitmap(context, renderTarget)) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000182 this->initFromRenderTarget(context, renderTarget, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000183}
184
185void SkGpuDevice::initFromRenderTarget(GrContext* context,
186 GrRenderTarget* renderTarget,
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000187 unsigned flags) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000188 fDrawProcs = NULL;
189
190 fContext = context;
191 fContext->ref();
192
commit-bot@chromium.org6fcd1ef2014-05-02 12:39:41 +0000193 bool useDFFonts = !!(flags & kDFFonts_Flag);
194 fMainTextContext = SkNEW_ARGS(GrDistanceFieldTextContext, (fContext, fLeakyProperties,
195 useDFFonts));
commit-bot@chromium.org47841822014-03-27 14:19:17 +0000196 fFallbackTextContext = SkNEW_ARGS(GrBitmapTextContext, (fContext, fLeakyProperties));
197
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000198 fRenderTarget = NULL;
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +0000199 fNeedClear = flags & kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000200
201 SkASSERT(NULL != renderTarget);
202 fRenderTarget = renderTarget;
203 fRenderTarget->ref();
204
205 // Hold onto to the texture in the pixel ref (if there is one) because the texture holds a ref
206 // on the RT but not vice-versa.
207 // TODO: Remove this trickery once we figure out how to make SkGrPixelRef do this without
208 // busting chrome (for a currently unknown reason).
209 GrSurface* surface = fRenderTarget->asTexture();
210 if (NULL == surface) {
211 surface = fRenderTarget;
212 }
reed@google.combf790232013-12-13 19:45:58 +0000213
214 SkImageInfo info;
215 surface->asImageInfo(&info);
bungeman@google.coma95a0662014-03-19 23:26:50 +0000216 SkPixelRef* pr = SkNEW_ARGS(SkGrPixelRef, (info, surface, SkToBool(flags & kCached_Flag)));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000217
reed@google.com672588b2014-01-08 15:42:01 +0000218 this->setPixelRef(pr)->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000219}
220
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000221SkGpuDevice* SkGpuDevice::Create(GrContext* context, const SkImageInfo& origInfo,
222 int sampleCount) {
223 if (kUnknown_SkColorType == origInfo.colorType() ||
224 origInfo.width() < 0 || origInfo.height() < 0) {
225 return NULL;
226 }
227
228 SkImageInfo info = origInfo;
229 // TODO: perhas we can loosen this check now that colortype is more detailed
230 // e.g. can we support both RGBA and BGRA here?
231 if (kRGB_565_SkColorType == info.colorType()) {
232 info.fAlphaType = kOpaque_SkAlphaType; // force this setting
233 } else {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000234 info.fColorType = kN32_SkColorType;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000235 if (kOpaque_SkAlphaType != info.alphaType()) {
236 info.fAlphaType = kPremul_SkAlphaType; // force this setting
237 }
238 }
239
240 GrTextureDesc desc;
241 desc.fFlags = kRenderTarget_GrTextureFlagBit;
242 desc.fWidth = info.width();
243 desc.fHeight = info.height();
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000244 desc.fConfig = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000245 desc.fSampleCnt = sampleCount;
246
247 SkAutoTUnref<GrTexture> texture(context->createUncachedTexture(desc, NULL, 0));
248 if (!texture.get()) {
249 return NULL;
250 }
skia.committer@gmail.com969588f2014-02-16 03:01:56 +0000251
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000252 return SkNEW_ARGS(SkGpuDevice, (context, texture.get()));
253}
254
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000255SkGpuDevice::~SkGpuDevice() {
256 if (fDrawProcs) {
257 delete fDrawProcs;
258 }
skia.committer@gmail.comd2ac07b2014-01-25 07:01:49 +0000259
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +0000260 delete fMainTextContext;
261 delete fFallbackTextContext;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000262
263 // The GrContext takes a ref on the target. We don't want to cause the render
264 // target to be unnecessarily kept alive.
265 if (fContext->getRenderTarget() == fRenderTarget) {
266 fContext->setRenderTarget(NULL);
267 }
268
269 if (fContext->getClip() == &fClipData) {
270 fContext->setClip(NULL);
271 }
272
273 SkSafeUnref(fRenderTarget);
274 fContext->unref();
275}
276
277///////////////////////////////////////////////////////////////////////////////
278
279void SkGpuDevice::makeRenderTargetCurrent() {
280 DO_DEFERRED_CLEAR();
281 fContext->setRenderTarget(fRenderTarget);
282}
283
284///////////////////////////////////////////////////////////////////////////////
285
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000286bool SkGpuDevice::onReadPixels(const SkImageInfo& dstInfo, void* dstPixels, size_t dstRowBytes,
287 int x, int y) {
288 DO_DEFERRED_CLEAR();
289
290 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000291 GrPixelConfig config = SkImageInfo2GrPixelConfig(dstInfo);
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000292 if (kUnknown_GrPixelConfig == config) {
293 return false;
294 }
295
296 uint32_t flags = 0;
297 if (kUnpremul_SkAlphaType == dstInfo.alphaType()) {
298 flags = GrContext::kUnpremul_PixelOpsFlag;
299 }
300 return fContext->readRenderTargetPixels(fRenderTarget, x, y, dstInfo.width(), dstInfo.height(),
301 config, dstPixels, dstRowBytes, flags);
302}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000303
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000304bool SkGpuDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
305 int x, int y) {
306 // TODO: teach fRenderTarget to take ImageInfo directly to specify the src pixels
commit-bot@chromium.org3adcc342014-04-23 19:18:09 +0000307 GrPixelConfig config = SkImageInfo2GrPixelConfig(info);
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000308 if (kUnknown_GrPixelConfig == config) {
309 return false;
310 }
311 uint32_t flags = 0;
312 if (kUnpremul_SkAlphaType == info.alphaType()) {
313 flags = GrContext::kUnpremul_PixelOpsFlag;
314 }
315 fRenderTarget->writePixels(x, y, info.width(), info.height(), config, pixels, rowBytes, flags);
316
317 // need to bump our genID for compatibility with clients that "know" we have a bitmap
318 this->onAccessBitmap().notifyPixelsChanged();
319
320 return true;
321}
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000322
senorblanco@chromium.orgb7b7eb32014-03-19 18:24:04 +0000323const SkBitmap& SkGpuDevice::onAccessBitmap() {
324 DO_DEFERRED_CLEAR();
325 return INHERITED::onAccessBitmap();
326}
327
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000328void SkGpuDevice::onAttachToCanvas(SkCanvas* canvas) {
329 INHERITED::onAttachToCanvas(canvas);
330
331 // Canvas promises that this ptr is valid until onDetachFromCanvas is called
332 fClipData.fClipStack = canvas->getClipStack();
333}
334
335void SkGpuDevice::onDetachFromCanvas() {
336 INHERITED::onDetachFromCanvas();
337 fClipData.fClipStack = NULL;
338}
339
340// call this every draw call, to ensure that the context reflects our state,
341// and not the state from some other canvas/device
342void SkGpuDevice::prepareDraw(const SkDraw& draw, bool forceIdentity) {
343 SkASSERT(NULL != fClipData.fClipStack);
344
345 fContext->setRenderTarget(fRenderTarget);
346
347 SkASSERT(draw.fClipStack && draw.fClipStack == fClipData.fClipStack);
348
349 if (forceIdentity) {
350 fContext->setIdentityMatrix();
351 } else {
352 fContext->setMatrix(*draw.fMatrix);
353 }
354 fClipData.fOrigin = this->getOrigin();
355
356 fContext->setClip(&fClipData);
357
358 DO_DEFERRED_CLEAR();
359}
360
361GrRenderTarget* SkGpuDevice::accessRenderTarget() {
362 DO_DEFERRED_CLEAR();
363 return fRenderTarget;
364}
365
366///////////////////////////////////////////////////////////////////////////////
367
368SK_COMPILE_ASSERT(SkShader::kNone_BitmapType == 0, shader_type_mismatch);
369SK_COMPILE_ASSERT(SkShader::kDefault_BitmapType == 1, shader_type_mismatch);
370SK_COMPILE_ASSERT(SkShader::kRadial_BitmapType == 2, shader_type_mismatch);
371SK_COMPILE_ASSERT(SkShader::kSweep_BitmapType == 3, shader_type_mismatch);
372SK_COMPILE_ASSERT(SkShader::kTwoPointRadial_BitmapType == 4,
373 shader_type_mismatch);
374SK_COMPILE_ASSERT(SkShader::kTwoPointConical_BitmapType == 5,
375 shader_type_mismatch);
376SK_COMPILE_ASSERT(SkShader::kLinear_BitmapType == 6, shader_type_mismatch);
377SK_COMPILE_ASSERT(SkShader::kLast_BitmapType == 6, shader_type_mismatch);
378
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000379///////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000380
381SkBitmap::Config SkGpuDevice::config() const {
382 if (NULL == fRenderTarget) {
383 return SkBitmap::kNo_Config;
384 }
385
386 bool isOpaque;
387 return grConfig2skConfig(fRenderTarget->config(), &isOpaque);
388}
389
390void SkGpuDevice::clear(SkColor color) {
391 SkIRect rect = SkIRect::MakeWH(this->width(), this->height());
392 fContext->clear(&rect, SkColor2GrColor(color), true, fRenderTarget);
393 fNeedClear = false;
394}
395
396void SkGpuDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) {
397 CHECK_SHOULD_DRAW(draw, false);
398
399 GrPaint grPaint;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000400 SkPaint2GrPaintShader(this, paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000401
402 fContext->drawPaint(grPaint);
403}
404
405// must be in SkCanvas::PointMode order
406static const GrPrimitiveType gPointMode2PrimtiveType[] = {
407 kPoints_GrPrimitiveType,
408 kLines_GrPrimitiveType,
409 kLineStrip_GrPrimitiveType
410};
411
412void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode,
413 size_t count, const SkPoint pts[], const SkPaint& paint) {
414 CHECK_FOR_ANNOTATION(paint);
415 CHECK_SHOULD_DRAW(draw, false);
416
417 SkScalar width = paint.getStrokeWidth();
418 if (width < 0) {
419 return;
420 }
421
422 // we only handle hairlines and paints without path effects or mask filters,
423 // else we let the SkDraw call our drawPath()
424 if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) {
425 draw.drawPoints(mode, count, pts, paint, true);
426 return;
427 }
428
429 GrPaint grPaint;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000430 SkPaint2GrPaintShader(this, paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000431
432 fContext->drawVertices(grPaint,
433 gPointMode2PrimtiveType[mode],
robertphillips@google.coma4662862013-11-21 14:24:16 +0000434 SkToS32(count),
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000435 (SkPoint*)pts,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000436 NULL,
437 NULL,
438 NULL,
439 0);
440}
441
442///////////////////////////////////////////////////////////////////////////////
443
444void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect,
445 const SkPaint& paint) {
446 CHECK_FOR_ANNOTATION(paint);
447 CHECK_SHOULD_DRAW(draw, false);
448
449 bool doStroke = paint.getStyle() != SkPaint::kFill_Style;
450 SkScalar width = paint.getStrokeWidth();
451
452 /*
453 We have special code for hairline strokes, miter-strokes, bevel-stroke
454 and fills. Anything else we just call our path code.
455 */
456 bool usePath = doStroke && width > 0 &&
457 (paint.getStrokeJoin() == SkPaint::kRound_Join ||
458 (paint.getStrokeJoin() == SkPaint::kBevel_Join && rect.isEmpty()));
459 // another two reasons we might need to call drawPath...
460 if (paint.getMaskFilter() || paint.getPathEffect()) {
461 usePath = true;
462 }
463 if (!usePath && paint.isAntiAlias() && !fContext->getMatrix().rectStaysRect()) {
464#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
465 if (doStroke) {
466#endif
467 usePath = true;
468#if defined(SHADER_AA_FILL_RECT) || !defined(IGNORE_ROT_AA_RECT_OPT)
469 } else {
470 usePath = !fContext->getMatrix().preservesRightAngles();
471 }
472#endif
473 }
474 // until we can both stroke and fill rectangles
475 if (paint.getStyle() == SkPaint::kStrokeAndFill_Style) {
476 usePath = true;
477 }
478
479 if (usePath) {
480 SkPath path;
481 path.addRect(rect);
482 this->drawPath(draw, path, paint, NULL, true);
483 return;
484 }
485
486 GrPaint grPaint;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000487 SkPaint2GrPaintShader(this, paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000488
489 if (!doStroke) {
490 fContext->drawRect(grPaint, rect);
491 } else {
492 SkStrokeRec stroke(paint);
493 fContext->drawRect(grPaint, rect, &stroke);
494 }
495}
496
497///////////////////////////////////////////////////////////////////////////////
498
499void SkGpuDevice::drawRRect(const SkDraw& draw, const SkRRect& rect,
500 const SkPaint& paint) {
501 CHECK_FOR_ANNOTATION(paint);
502 CHECK_SHOULD_DRAW(draw, false);
503
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000504 GrPaint grPaint;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000505 SkPaint2GrPaintShader(this, paint, true, &grPaint);
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000506
507 SkStrokeRec stroke(paint);
508 if (paint.getMaskFilter()) {
509 // try to hit the fast path for drawing filtered round rects
510
511 SkRRect devRRect;
512 if (rect.transform(fContext->getMatrix(), &devRRect)) {
513 if (devRRect.allCornersCircular()) {
514 SkRect maskRect;
515 if (paint.getMaskFilter()->canFilterMaskGPU(devRRect.rect(),
516 draw.fClip->getBounds(),
517 fContext->getMatrix(),
518 &maskRect)) {
519 SkIRect finalIRect;
520 maskRect.roundOut(&finalIRect);
521 if (draw.fClip->quickReject(finalIRect)) {
522 // clipped out
523 return;
524 }
525 if (NULL != draw.fBounder && !draw.fBounder->doIRect(finalIRect)) {
526 // nothing to draw
527 return;
528 }
529 if (paint.getMaskFilter()->directFilterRRectMaskGPU(fContext, &grPaint,
530 stroke, devRRect)) {
531 return;
532 }
533 }
534
535 }
536 }
537
538 }
539
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000540 if (paint.getMaskFilter() || paint.getPathEffect()) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000541 SkPath path;
542 path.addRRect(rect);
543 this->drawPath(draw, path, paint, NULL, true);
544 return;
545 }
546
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000547 fContext->drawRRect(grPaint, rect, stroke);
548}
549
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000550void SkGpuDevice::drawDRRect(const SkDraw& draw, const SkRRect& outer,
551 const SkRRect& inner, const SkPaint& paint) {
552 SkStrokeRec stroke(paint);
553 if (stroke.isFillStyle()) {
554
555 CHECK_FOR_ANNOTATION(paint);
556 CHECK_SHOULD_DRAW(draw, false);
557
558 GrPaint grPaint;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000559 SkPaint2GrPaintShader(this, paint, true, &grPaint);
commit-bot@chromium.org0a09d712014-04-09 21:26:11 +0000560
561 if (NULL == paint.getMaskFilter() && NULL == paint.getPathEffect()) {
562 fContext->drawDRRect(grPaint, outer, inner);
563 return;
564 }
565 }
566
567 SkPath path;
568 path.addRRect(outer);
569 path.addRRect(inner);
570 path.setFillType(SkPath::kEvenOdd_FillType);
571
572 this->drawPath(draw, path, paint, NULL, true);
573}
574
575
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000576/////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000577
578void SkGpuDevice::drawOval(const SkDraw& draw, const SkRect& oval,
579 const SkPaint& paint) {
580 CHECK_FOR_ANNOTATION(paint);
581 CHECK_SHOULD_DRAW(draw, false);
582
583 bool usePath = false;
584 // some basic reasons we might need to call drawPath...
585 if (paint.getMaskFilter() || paint.getPathEffect()) {
586 usePath = true;
587 }
588
589 if (usePath) {
590 SkPath path;
591 path.addOval(oval);
592 this->drawPath(draw, path, paint, NULL, true);
593 return;
594 }
595
596 GrPaint grPaint;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000597 SkPaint2GrPaintShader(this, paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000598 SkStrokeRec stroke(paint);
599
600 fContext->drawOval(grPaint, oval, stroke);
601}
602
603#include "SkMaskFilter.h"
604#include "SkBounder.h"
605
606///////////////////////////////////////////////////////////////////////////////
607
608// helpers for applying mask filters
609namespace {
610
611// Draw a mask using the supplied paint. Since the coverage/geometry
612// is already burnt into the mask this boils down to a rect draw.
613// Return true if the mask was successfully drawn.
614bool draw_mask(GrContext* context, const SkRect& maskRect,
615 GrPaint* grp, GrTexture* mask) {
616 GrContext::AutoMatrix am;
617 if (!am.setIdentity(context, grp)) {
618 return false;
619 }
620
621 SkMatrix matrix;
622 matrix.setTranslate(-maskRect.fLeft, -maskRect.fTop);
623 matrix.postIDiv(mask->width(), mask->height());
624
625 grp->addCoverageEffect(GrSimpleTextureEffect::Create(mask, matrix))->unref();
626 context->drawRect(*grp, maskRect);
627 return true;
628}
629
630bool draw_with_mask_filter(GrContext* context, const SkPath& devPath,
631 SkMaskFilter* filter, const SkRegion& clip, SkBounder* bounder,
632 GrPaint* grp, SkPaint::Style style) {
633 SkMask srcM, dstM;
634
635 if (!SkDraw::DrawToMask(devPath, &clip.getBounds(), filter, &context->getMatrix(), &srcM,
636 SkMask::kComputeBoundsAndRenderImage_CreateMode, style)) {
637 return false;
638 }
639 SkAutoMaskFreeImage autoSrc(srcM.fImage);
640
641 if (!filter->filterMask(&dstM, srcM, context->getMatrix(), NULL)) {
642 return false;
643 }
644 // this will free-up dstM when we're done (allocated in filterMask())
645 SkAutoMaskFreeImage autoDst(dstM.fImage);
646
647 if (clip.quickReject(dstM.fBounds)) {
648 return false;
649 }
650 if (bounder && !bounder->doIRect(dstM.fBounds)) {
651 return false;
652 }
653
654 // we now have a device-aligned 8bit mask in dstM, ready to be drawn using
655 // the current clip (and identity matrix) and GrPaint settings
656 GrTextureDesc desc;
657 desc.fWidth = dstM.fBounds.width();
658 desc.fHeight = dstM.fBounds.height();
659 desc.fConfig = kAlpha_8_GrPixelConfig;
660
661 GrAutoScratchTexture ast(context, desc);
662 GrTexture* texture = ast.texture();
663
664 if (NULL == texture) {
665 return false;
666 }
667 texture->writePixels(0, 0, desc.fWidth, desc.fHeight, desc.fConfig,
668 dstM.fImage, dstM.fRowBytes);
669
670 SkRect maskRect = SkRect::Make(dstM.fBounds);
671
672 return draw_mask(context, maskRect, grp, texture);
673}
674
675// Create a mask of 'devPath' and place the result in 'mask'. Return true on
676// success; false otherwise.
677bool create_mask_GPU(GrContext* context,
678 const SkRect& maskRect,
679 const SkPath& devPath,
680 const SkStrokeRec& stroke,
681 bool doAA,
682 GrAutoScratchTexture* mask) {
683 GrTextureDesc desc;
684 desc.fFlags = kRenderTarget_GrTextureFlagBit;
685 desc.fWidth = SkScalarCeilToInt(maskRect.width());
686 desc.fHeight = SkScalarCeilToInt(maskRect.height());
687 // We actually only need A8, but it often isn't supported as a
688 // render target so default to RGBA_8888
689 desc.fConfig = kRGBA_8888_GrPixelConfig;
690 if (context->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
691 desc.fConfig = kAlpha_8_GrPixelConfig;
692 }
693
694 mask->set(context, desc);
695 if (NULL == mask->texture()) {
696 return false;
697 }
698
699 GrTexture* maskTexture = mask->texture();
700 SkRect clipRect = SkRect::MakeWH(maskRect.width(), maskRect.height());
701
702 GrContext::AutoRenderTarget art(context, maskTexture->asRenderTarget());
703 GrContext::AutoClip ac(context, clipRect);
704
705 context->clear(NULL, 0x0, true);
706
707 GrPaint tempPaint;
708 if (doAA) {
709 tempPaint.setAntiAlias(true);
710 // AA uses the "coverage" stages on GrDrawTarget. Coverage with a dst
711 // blend coeff of zero requires dual source blending support in order
712 // to properly blend partially covered pixels. This means the AA
713 // code path may not be taken. So we use a dst blend coeff of ISA. We
714 // could special case AA draws to a dst surface with known alpha=0 to
715 // use a zero dst coeff when dual source blending isn't available.
716 tempPaint.setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff);
717 }
718
719 GrContext::AutoMatrix am;
720
721 // Draw the mask into maskTexture with the path's top-left at the origin using tempPaint.
722 SkMatrix translate;
723 translate.setTranslate(-maskRect.fLeft, -maskRect.fTop);
724 am.set(context, translate);
725 context->drawPath(tempPaint, devPath, stroke);
726 return true;
727}
728
729SkBitmap wrap_texture(GrTexture* texture) {
reed@google.combf790232013-12-13 19:45:58 +0000730 SkImageInfo info;
731 texture->asImageInfo(&info);
732
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000733 SkBitmap result;
reed@google.combf790232013-12-13 19:45:58 +0000734 result.setConfig(info);
735 result.setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000736 return result;
737}
738
739};
740
741void SkGpuDevice::drawPath(const SkDraw& draw, const SkPath& origSrcPath,
742 const SkPaint& paint, const SkMatrix* prePathMatrix,
743 bool pathIsMutable) {
744 CHECK_FOR_ANNOTATION(paint);
745 CHECK_SHOULD_DRAW(draw, false);
746
747 GrPaint grPaint;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +0000748 SkPaint2GrPaintShader(this, paint, true, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000749
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000750 // If we have a prematrix, apply it to the path, optimizing for the case
751 // where the original path can in fact be modified in place (even though
752 // its parameter type is const).
753 SkPath* pathPtr = const_cast<SkPath*>(&origSrcPath);
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000754 SkTLazy<SkPath> tmpPath;
755 SkTLazy<SkPath> effectPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000756
757 if (prePathMatrix) {
758 SkPath* result = pathPtr;
759
760 if (!pathIsMutable) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000761 result = tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000762 pathIsMutable = true;
763 }
764 // should I push prePathMatrix on our MV stack temporarily, instead
765 // of applying it here? See SkDraw.cpp
766 pathPtr->transform(*prePathMatrix, result);
767 pathPtr = result;
768 }
769 // at this point we're done with prePathMatrix
770 SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;)
771
772 SkStrokeRec stroke(paint);
773 SkPathEffect* pathEffect = paint.getPathEffect();
774 const SkRect* cullRect = NULL; // TODO: what is our bounds?
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000775 if (pathEffect && pathEffect->filterPath(effectPath.init(), *pathPtr, &stroke,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000776 cullRect)) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000777 pathPtr = effectPath.get();
778 pathIsMutable = true;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000779 }
780
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000781 if (paint.getMaskFilter()) {
782 if (!stroke.isHairlineStyle()) {
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000783 SkPath* strokedPath = pathIsMutable ? pathPtr : tmpPath.init();
784 if (stroke.applyToPath(strokedPath, *pathPtr)) {
785 pathPtr = strokedPath;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000786 pathIsMutable = true;
787 stroke.setFillStyle();
788 }
789 }
790
791 // avoid possibly allocating a new path in transform if we can
commit-bot@chromium.orgf0c41e22014-01-14 18:42:34 +0000792 SkPath* devPathPtr = pathIsMutable ? pathPtr : tmpPath.init();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000793
794 // transform the path into device space
795 pathPtr->transform(fContext->getMatrix(), devPathPtr);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000796
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000797 SkRect maskRect;
798 if (paint.getMaskFilter()->canFilterMaskGPU(devPathPtr->getBounds(),
799 draw.fClip->getBounds(),
800 fContext->getMatrix(),
801 &maskRect)) {
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000802 // The context's matrix may change while creating the mask, so save the CTM here to
803 // pass to filterMaskGPU.
804 const SkMatrix ctm = fContext->getMatrix();
805
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000806 SkIRect finalIRect;
807 maskRect.roundOut(&finalIRect);
808 if (draw.fClip->quickReject(finalIRect)) {
809 // clipped out
810 return;
811 }
812 if (NULL != draw.fBounder && !draw.fBounder->doIRect(finalIRect)) {
813 // nothing to draw
814 return;
815 }
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +0000816
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000817 if (paint.getMaskFilter()->directFilterMaskGPU(fContext, &grPaint,
commit-bot@chromium.org82139702014-03-10 22:53:20 +0000818 stroke, *devPathPtr)) {
commit-bot@chromium.orgcf34bc02014-01-30 15:34:43 +0000819 // the mask filter was able to draw itself directly, so there's nothing
820 // left to do.
821 return;
822 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000823
824 GrAutoScratchTexture mask;
825
826 if (create_mask_GPU(fContext, maskRect, *devPathPtr, stroke,
827 grPaint.isAntiAlias(), &mask)) {
828 GrTexture* filtered;
829
commit-bot@chromium.org41bf9302014-01-08 22:25:53 +0000830 if (paint.getMaskFilter()->filterMaskGPU(mask.texture(),
commit-bot@chromium.org439ff1b2014-01-13 16:39:39 +0000831 ctm, maskRect, &filtered, true)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000832 // filterMaskGPU gives us ownership of a ref to the result
833 SkAutoTUnref<GrTexture> atu(filtered);
834
835 // If the scratch texture that we used as the filter src also holds the filter
836 // result then we must detach so that this texture isn't recycled for a later
837 // draw.
838 if (filtered == mask.texture()) {
839 mask.detach();
840 filtered->unref(); // detach transfers GrAutoScratchTexture's ref to us.
841 }
842
843 if (draw_mask(fContext, maskRect, &grPaint, filtered)) {
844 // This path is completely drawn
845 return;
846 }
847 }
848 }
849 }
850
851 // draw the mask on the CPU - this is a fallthrough path in case the
852 // GPU path fails
853 SkPaint::Style style = stroke.isHairlineStyle() ? SkPaint::kStroke_Style :
854 SkPaint::kFill_Style;
855 draw_with_mask_filter(fContext, *devPathPtr, paint.getMaskFilter(),
856 *draw.fClip, draw.fBounder, &grPaint, style);
857 return;
858 }
859
860 fContext->drawPath(grPaint, *pathPtr, stroke);
861}
862
863static const int kBmpSmallTileSize = 1 << 10;
864
865static inline int get_tile_count(const SkIRect& srcRect, int tileSize) {
866 int tilesX = (srcRect.fRight / tileSize) - (srcRect.fLeft / tileSize) + 1;
867 int tilesY = (srcRect.fBottom / tileSize) - (srcRect.fTop / tileSize) + 1;
868 return tilesX * tilesY;
869}
870
871static int determine_tile_size(const SkBitmap& bitmap, const SkIRect& src, int maxTileSize) {
872 if (maxTileSize <= kBmpSmallTileSize) {
873 return maxTileSize;
874 }
875
876 size_t maxTileTotalTileSize = get_tile_count(src, maxTileSize);
877 size_t smallTotalTileSize = get_tile_count(src, kBmpSmallTileSize);
878
879 maxTileTotalTileSize *= maxTileSize * maxTileSize;
880 smallTotalTileSize *= kBmpSmallTileSize * kBmpSmallTileSize;
881
882 if (maxTileTotalTileSize > 2 * smallTotalTileSize) {
883 return kBmpSmallTileSize;
884 } else {
885 return maxTileSize;
886 }
887}
888
889// Given a bitmap, an optional src rect, and a context with a clip and matrix determine what
890// pixels from the bitmap are necessary.
891static void determine_clipped_src_rect(const GrContext* context,
892 const SkBitmap& bitmap,
893 const SkRect* srcRectPtr,
894 SkIRect* clippedSrcIRect) {
895 const GrClipData* clip = context->getClip();
896 clip->getConservativeBounds(context->getRenderTarget(), clippedSrcIRect, NULL);
897 SkMatrix inv;
898 if (!context->getMatrix().invert(&inv)) {
899 clippedSrcIRect->setEmpty();
900 return;
901 }
902 SkRect clippedSrcRect = SkRect::Make(*clippedSrcIRect);
903 inv.mapRect(&clippedSrcRect);
904 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000905 // we've setup src space 0,0 to map to the top left of the src rect.
906 clippedSrcRect.offset(srcRectPtr->fLeft, srcRectPtr->fTop);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000907 if (!clippedSrcRect.intersect(*srcRectPtr)) {
908 clippedSrcIRect->setEmpty();
909 return;
910 }
911 }
912 clippedSrcRect.roundOut(clippedSrcIRect);
913 SkIRect bmpBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
914 if (!clippedSrcIRect->intersect(bmpBounds)) {
915 clippedSrcIRect->setEmpty();
916 }
917}
918
919bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
920 const GrTextureParams& params,
921 const SkRect* srcRectPtr,
922 int maxTileSize,
923 int* tileSize,
924 SkIRect* clippedSrcRect) const {
925 // if bitmap is explictly texture backed then just use the texture
926 if (NULL != bitmap.getTexture()) {
927 return false;
928 }
929
930 // if it's larger than the max tile size, then we have no choice but tiling.
931 if (bitmap.width() > maxTileSize || bitmap.height() > maxTileSize) {
932 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
933 *tileSize = determine_tile_size(bitmap, *clippedSrcRect, maxTileSize);
934 return true;
935 }
936
937 if (bitmap.width() * bitmap.height() < 4 * kBmpSmallTileSize * kBmpSmallTileSize) {
938 return false;
939 }
940
941 // if the entire texture is already in our cache then no reason to tile it
942 if (GrIsBitmapInCache(fContext, bitmap, &params)) {
943 return false;
944 }
945
946 // At this point we know we could do the draw by uploading the entire bitmap
947 // as a texture. However, if the texture would be large compared to the
948 // cache size and we don't require most of it for this draw then tile to
949 // reduce the amount of upload and cache spill.
950
951 // assumption here is that sw bitmap size is a good proxy for its size as
952 // a texture
953 size_t bmpSize = bitmap.getSize();
954 size_t cacheSize;
commit-bot@chromium.org95c20032014-05-09 14:29:32 +0000955 fContext->getResourceCacheLimits(NULL, &cacheSize);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000956 if (bmpSize < cacheSize / 2) {
957 return false;
958 }
959
960 // Figure out how much of the src we will need based on the src rect and clipping.
961 determine_clipped_src_rect(fContext, bitmap, srcRectPtr, clippedSrcRect);
962 *tileSize = kBmpSmallTileSize; // already know whole bitmap fits in one max sized tile.
963 size_t usedTileBytes = get_tile_count(*clippedSrcRect, kBmpSmallTileSize) *
964 kBmpSmallTileSize * kBmpSmallTileSize;
965
966 return usedTileBytes < 2 * bmpSize;
967}
968
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000969void SkGpuDevice::drawBitmap(const SkDraw& origDraw,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000970 const SkBitmap& bitmap,
971 const SkMatrix& m,
972 const SkPaint& paint) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +0000973 SkMatrix concat;
974 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
975 if (!m.isIdentity()) {
976 concat.setConcat(*draw->fMatrix, m);
977 draw.writable()->fMatrix = &concat;
978 }
979 this->drawBitmapCommon(*draw, bitmap, NULL, NULL, paint, SkCanvas::kNone_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000980}
981
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000982// This method outsets 'iRect' by 'outset' all around and then clamps its extents to
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000983// 'clamp'. 'offset' is adjusted to remain positioned over the top-left corner
984// of 'iRect' for all possible outsets/clamps.
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000985static inline void clamped_outset_with_offset(SkIRect* iRect,
986 int outset,
987 SkPoint* offset,
988 const SkIRect& clamp) {
989 iRect->outset(outset, outset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000990
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000991 int leftClampDelta = clamp.fLeft - iRect->fLeft;
992 if (leftClampDelta > 0) {
993 offset->fX -= outset - leftClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000994 iRect->fLeft = clamp.fLeft;
995 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000996 offset->fX -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +0000997 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +0000998
999 int topClampDelta = clamp.fTop - iRect->fTop;
1000 if (topClampDelta > 0) {
1001 offset->fY -= outset - topClampDelta;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001002 iRect->fTop = clamp.fTop;
1003 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001004 offset->fY -= outset;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001005 }
1006
1007 if (iRect->fRight > clamp.fRight) {
1008 iRect->fRight = clamp.fRight;
1009 }
1010 if (iRect->fBottom > clamp.fBottom) {
1011 iRect->fBottom = clamp.fBottom;
1012 }
1013}
1014
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001015static bool has_aligned_samples(const SkRect& srcRect,
1016 const SkRect& transformedRect) {
1017 // detect pixel disalignment
1018 if (SkScalarAbs(SkScalarRoundToScalar(transformedRect.left()) -
1019 transformedRect.left()) < COLOR_BLEED_TOLERANCE &&
1020 SkScalarAbs(SkScalarRoundToScalar(transformedRect.top()) -
1021 transformedRect.top()) < COLOR_BLEED_TOLERANCE &&
1022 SkScalarAbs(transformedRect.width() - srcRect.width()) <
1023 COLOR_BLEED_TOLERANCE &&
1024 SkScalarAbs(transformedRect.height() - srcRect.height()) <
1025 COLOR_BLEED_TOLERANCE) {
1026 return true;
1027 }
1028 return false;
1029}
1030
1031static bool may_color_bleed(const SkRect& srcRect,
1032 const SkRect& transformedRect,
1033 const SkMatrix& m) {
1034 // Only gets called if has_aligned_samples returned false.
1035 // So we can assume that sampling is axis aligned but not texel aligned.
1036 SkASSERT(!has_aligned_samples(srcRect, transformedRect));
1037 SkRect innerSrcRect(srcRect), innerTransformedRect,
1038 outerTransformedRect(transformedRect);
1039 innerSrcRect.inset(SK_ScalarHalf, SK_ScalarHalf);
1040 m.mapRect(&innerTransformedRect, innerSrcRect);
1041
1042 // The gap between outerTransformedRect and innerTransformedRect
1043 // represents the projection of the source border area, which is
1044 // problematic for color bleeding. We must check whether any
1045 // destination pixels sample the border area.
1046 outerTransformedRect.inset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1047 innerTransformedRect.outset(COLOR_BLEED_TOLERANCE, COLOR_BLEED_TOLERANCE);
1048 SkIRect outer, inner;
1049 outerTransformedRect.round(&outer);
1050 innerTransformedRect.round(&inner);
1051 // If the inner and outer rects round to the same result, it means the
1052 // border does not overlap any pixel centers. Yay!
1053 return inner != outer;
1054}
1055
1056static bool needs_texture_domain(const SkBitmap& bitmap,
1057 const SkRect& srcRect,
1058 GrTextureParams &params,
1059 const SkMatrix& contextMatrix,
1060 bool bicubic) {
1061 bool needsTextureDomain = false;
1062
1063 if (bicubic || params.filterMode() != GrTextureParams::kNone_FilterMode) {
1064 // Need texture domain if drawing a sub rect
1065 needsTextureDomain = srcRect.width() < bitmap.width() ||
1066 srcRect.height() < bitmap.height();
1067 if (!bicubic && needsTextureDomain && contextMatrix.rectStaysRect()) {
1068 // sampling is axis-aligned
1069 SkRect transformedRect;
1070 contextMatrix.mapRect(&transformedRect, srcRect);
1071
1072 if (has_aligned_samples(srcRect, transformedRect)) {
1073 params.setFilterMode(GrTextureParams::kNone_FilterMode);
1074 needsTextureDomain = false;
1075 } else {
1076 needsTextureDomain = may_color_bleed(srcRect, transformedRect, contextMatrix);
1077 }
1078 }
1079 }
1080 return needsTextureDomain;
1081}
1082
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001083void SkGpuDevice::drawBitmapCommon(const SkDraw& draw,
1084 const SkBitmap& bitmap,
1085 const SkRect* srcRectPtr,
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001086 const SkSize* dstSizePtr,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001087 const SkPaint& paint,
1088 SkCanvas::DrawBitmapRectFlags flags) {
1089 CHECK_SHOULD_DRAW(draw, false);
1090
1091 SkRect srcRect;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001092 SkSize dstSize;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001093 // If there is no src rect, or the src rect contains the entire bitmap then we're effectively
1094 // in the (easier) bleed case, so update flags.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001095 if (NULL == srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001096 SkScalar w = SkIntToScalar(bitmap.width());
1097 SkScalar h = SkIntToScalar(bitmap.height());
1098 dstSize.fWidth = w;
1099 dstSize.fHeight = h;
1100 srcRect.set(0, 0, w, h);
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001101 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001102 } else {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001103 SkASSERT(NULL != dstSizePtr);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001104 srcRect = *srcRectPtr;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001105 dstSize = *dstSizePtr;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001106 if (srcRect.fLeft <= 0 && srcRect.fTop <= 0 &&
1107 srcRect.fRight >= bitmap.width() && srcRect.fBottom >= bitmap.height()) {
1108 flags = (SkCanvas::DrawBitmapRectFlags) (flags | SkCanvas::kBleed_DrawBitmapRectFlag);
1109 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001110 }
1111
1112 if (paint.getMaskFilter()){
1113 // Convert the bitmap to a shader so that the rect can be drawn
1114 // through drawRect, which supports mask filters.
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001115 SkBitmap tmp; // subset of bitmap, if necessary
1116 const SkBitmap* bitmapPtr = &bitmap;
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001117 SkMatrix localM;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001118 if (NULL != srcRectPtr) {
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001119 localM.setTranslate(-srcRectPtr->fLeft, -srcRectPtr->fTop);
1120 localM.postScale(dstSize.fWidth / srcRectPtr->width(),
1121 dstSize.fHeight / srcRectPtr->height());
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001122 // In bleed mode we position and trim the bitmap based on the src rect which is
1123 // already accounted for in 'm' and 'srcRect'. In clamp mode we need to chop out
1124 // the desired portion of the bitmap and then update 'm' and 'srcRect' to
1125 // compensate.
1126 if (!(SkCanvas::kBleed_DrawBitmapRectFlag & flags)) {
1127 SkIRect iSrc;
1128 srcRect.roundOut(&iSrc);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001129
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001130 SkPoint offset = SkPoint::Make(SkIntToScalar(iSrc.fLeft),
1131 SkIntToScalar(iSrc.fTop));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001132
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001133 if (!bitmap.extractSubset(&tmp, iSrc)) {
1134 return; // extraction failed
1135 }
1136 bitmapPtr = &tmp;
1137 srcRect.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001138
commit-bot@chromium.orgd6ca4ac2013-11-22 20:34:59 +00001139 // The source rect has changed so update the matrix
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001140 localM.preTranslate(offset.fX, offset.fY);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001141 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001142 } else {
1143 localM.reset();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001144 }
1145
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001146 SkPaint paintWithShader(paint);
1147 paintWithShader.setShader(SkShader::CreateBitmapShader(*bitmapPtr,
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +00001148 SkShader::kClamp_TileMode, SkShader::kClamp_TileMode, &localM))->unref();
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001149 SkRect dstRect = {0, 0, dstSize.fWidth, dstSize.fHeight};
1150 this->drawRect(draw, dstRect, paintWithShader);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001151
1152 return;
1153 }
1154
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001155 // If there is no mask filter than it is OK to handle the src rect -> dst rect scaling using
1156 // the view matrix rather than a local matrix.
1157 SkMatrix m;
1158 m.setScale(dstSize.fWidth / srcRect.width(),
1159 dstSize.fHeight / srcRect.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001160 fContext->concatMatrix(m);
1161
1162 GrTextureParams params;
1163 SkPaint::FilterLevel paintFilterLevel = paint.getFilterLevel();
1164 GrTextureParams::FilterMode textureFilterMode;
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001165
1166 int tileFilterPad;
1167 bool doBicubic = false;
1168
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001169 switch(paintFilterLevel) {
1170 case SkPaint::kNone_FilterLevel:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001171 tileFilterPad = 0;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001172 textureFilterMode = GrTextureParams::kNone_FilterMode;
1173 break;
1174 case SkPaint::kLow_FilterLevel:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001175 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001176 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1177 break;
1178 case SkPaint::kMedium_FilterLevel:
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001179 tileFilterPad = 1;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001180 if (fContext->getMatrix().getMinStretch() < SK_Scalar1) {
1181 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1182 } else {
1183 // Don't trigger MIP level generation unnecessarily.
1184 textureFilterMode = GrTextureParams::kBilerp_FilterMode;
1185 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001186 break;
commit-bot@chromium.org79b7eee2013-12-16 21:02:29 +00001187 case SkPaint::kHigh_FilterLevel:
commit-bot@chromium.orgcea9abb2013-12-09 19:15:37 +00001188 // Minification can look bad with the bicubic effect.
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001189 if (fContext->getMatrix().getMinStretch() >= SK_Scalar1) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001190 // We will install an effect that does the filtering in the shader.
1191 textureFilterMode = GrTextureParams::kNone_FilterMode;
1192 tileFilterPad = GrBicubicEffect::kFilterTexelPad;
1193 doBicubic = true;
1194 } else {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001195 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1196 tileFilterPad = 1;
1197 }
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001198 break;
1199 default:
1200 SkErrorInternals::SetError( kInvalidPaint_SkError,
1201 "Sorry, I don't understand the filtering "
1202 "mode you asked for. Falling back to "
1203 "MIPMaps.");
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001204 tileFilterPad = 1;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001205 textureFilterMode = GrTextureParams::kMipMap_FilterMode;
1206 break;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001207 }
1208
1209 params.setFilterMode(textureFilterMode);
1210
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001211 int maxTileSize = fContext->getMaxTextureSize() - 2 * tileFilterPad;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001212 int tileSize;
1213
1214 SkIRect clippedSrcRect;
1215 if (this->shouldTileBitmap(bitmap, params, srcRectPtr, maxTileSize, &tileSize,
1216 &clippedSrcRect)) {
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001217 this->drawTiledBitmap(bitmap, srcRect, clippedSrcRect, params, paint, flags, tileSize,
1218 doBicubic);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001219 } else {
1220 // take the simple case
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001221 bool needsTextureDomain = needs_texture_domain(bitmap,
1222 srcRect,
1223 params,
1224 fContext->getMatrix(),
1225 doBicubic);
1226 this->internalDrawBitmap(bitmap,
1227 srcRect,
1228 params,
1229 paint,
1230 flags,
1231 doBicubic,
1232 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001233 }
1234}
1235
1236// Break 'bitmap' into several tiles to draw it since it has already
1237// been determined to be too large to fit in VRAM
1238void SkGpuDevice::drawTiledBitmap(const SkBitmap& bitmap,
1239 const SkRect& srcRect,
1240 const SkIRect& clippedSrcIRect,
1241 const GrTextureParams& params,
1242 const SkPaint& paint,
1243 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001244 int tileSize,
1245 bool bicubic) {
commit-bot@chromium.org9d5e3f12014-05-01 21:23:19 +00001246 // The following pixel lock is technically redundant, but it is desirable
1247 // to lock outside of the tile loop to prevent redecoding the whole image
1248 // at each tile in cases where 'bitmap' holds an SkDiscardablePixelRef that
1249 // is larger than the limit of the discardable memory pool.
1250 SkAutoLockPixels alp(bitmap);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001251 SkRect clippedSrcRect = SkRect::Make(clippedSrcIRect);
1252
1253 int nx = bitmap.width() / tileSize;
1254 int ny = bitmap.height() / tileSize;
1255 for (int x = 0; x <= nx; x++) {
1256 for (int y = 0; y <= ny; y++) {
1257 SkRect tileR;
1258 tileR.set(SkIntToScalar(x * tileSize),
1259 SkIntToScalar(y * tileSize),
1260 SkIntToScalar((x + 1) * tileSize),
1261 SkIntToScalar((y + 1) * tileSize));
1262
1263 if (!SkRect::Intersects(tileR, clippedSrcRect)) {
1264 continue;
1265 }
1266
1267 if (!tileR.intersect(srcRect)) {
1268 continue;
1269 }
1270
1271 SkBitmap tmpB;
1272 SkIRect iTileR;
1273 tileR.roundOut(&iTileR);
1274 SkPoint offset = SkPoint::Make(SkIntToScalar(iTileR.fLeft),
1275 SkIntToScalar(iTileR.fTop));
1276
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001277 // Adjust the context matrix to draw at the right x,y in device space
1278 SkMatrix tmpM;
1279 GrContext::AutoMatrix am;
1280 tmpM.setTranslate(offset.fX - srcRect.fLeft, offset.fY - srcRect.fTop);
1281 am.setPreConcat(fContext, tmpM);
1282
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001283 if (SkPaint::kNone_FilterLevel != paint.getFilterLevel() || bicubic) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001284 SkIRect iClampRect;
1285
1286 if (SkCanvas::kBleed_DrawBitmapRectFlag & flags) {
1287 // In bleed mode we want to always expand the tile on all edges
1288 // but stay within the bitmap bounds
1289 iClampRect = SkIRect::MakeWH(bitmap.width(), bitmap.height());
1290 } else {
1291 // In texture-domain/clamp mode we only want to expand the
1292 // tile on edges interior to "srcRect" (i.e., we want to
1293 // not bleed across the original clamped edges)
1294 srcRect.roundOut(&iClampRect);
1295 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001296 int outset = bicubic ? GrBicubicEffect::kFilterTexelPad : 1;
1297 clamped_outset_with_offset(&iTileR, outset, &offset, iClampRect);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001298 }
1299
1300 if (bitmap.extractSubset(&tmpB, iTileR)) {
1301 // now offset it to make it "local" to our tmp bitmap
1302 tileR.offset(-offset.fX, -offset.fY);
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001303 GrTextureParams paramsTemp = params;
1304 bool needsTextureDomain = needs_texture_domain(bitmap,
1305 srcRect,
1306 paramsTemp,
1307 fContext->getMatrix(),
1308 bicubic);
1309 this->internalDrawBitmap(tmpB,
1310 tileR,
1311 paramsTemp,
1312 paint,
1313 flags,
1314 bicubic,
1315 needsTextureDomain);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001316 }
1317 }
1318 }
1319}
1320
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001321
1322/*
1323 * This is called by drawBitmap(), which has to handle images that may be too
1324 * large to be represented by a single texture.
1325 *
1326 * internalDrawBitmap assumes that the specified bitmap will fit in a texture
1327 * and that non-texture portion of the GrPaint has already been setup.
1328 */
1329void SkGpuDevice::internalDrawBitmap(const SkBitmap& bitmap,
1330 const SkRect& srcRect,
1331 const GrTextureParams& params,
1332 const SkPaint& paint,
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001333 SkCanvas::DrawBitmapRectFlags flags,
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001334 bool bicubic,
1335 bool needsTextureDomain) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001336 SkASSERT(bitmap.width() <= fContext->getMaxTextureSize() &&
1337 bitmap.height() <= fContext->getMaxTextureSize());
1338
1339 GrTexture* texture;
1340 SkAutoCachedTexture act(this, bitmap, &params, &texture);
1341 if (NULL == texture) {
1342 return;
1343 }
1344
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001345 SkRect dstRect = {0, 0, srcRect.width(), srcRect.height() };
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001346 SkRect paintRect;
1347 SkScalar wInv = SkScalarInvert(SkIntToScalar(texture->width()));
1348 SkScalar hInv = SkScalarInvert(SkIntToScalar(texture->height()));
1349 paintRect.setLTRB(SkScalarMul(srcRect.fLeft, wInv),
1350 SkScalarMul(srcRect.fTop, hInv),
1351 SkScalarMul(srcRect.fRight, wInv),
1352 SkScalarMul(srcRect.fBottom, hInv));
1353
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001354 SkRect textureDomain = SkRect::MakeEmpty();
1355 SkAutoTUnref<GrEffectRef> effect;
commit-bot@chromium.orga17773f2014-05-09 13:53:38 +00001356 if (needsTextureDomain && !(flags & SkCanvas::kBleed_DrawBitmapRectFlag)) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001357 // Use a constrained texture domain to avoid color bleeding
1358 SkScalar left, top, right, bottom;
1359 if (srcRect.width() > SK_Scalar1) {
1360 SkScalar border = SK_ScalarHalf / texture->width();
1361 left = paintRect.left() + border;
1362 right = paintRect.right() - border;
1363 } else {
1364 left = right = SkScalarHalf(paintRect.left() + paintRect.right());
1365 }
1366 if (srcRect.height() > SK_Scalar1) {
1367 SkScalar border = SK_ScalarHalf / texture->height();
1368 top = paintRect.top() + border;
1369 bottom = paintRect.bottom() - border;
1370 } else {
1371 top = bottom = SkScalarHalf(paintRect.top() + paintRect.bottom());
1372 }
1373 textureDomain.setLTRB(left, top, right, bottom);
commit-bot@chromium.org7d7f3142013-12-16 15:18:11 +00001374 if (bicubic) {
1375 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), textureDomain));
1376 } else {
1377 effect.reset(GrTextureDomainEffect::Create(texture,
1378 SkMatrix::I(),
1379 textureDomain,
1380 GrTextureDomain::kClamp_Mode,
1381 params.filterMode()));
1382 }
commit-bot@chromium.orgdec61502013-12-02 22:22:35 +00001383 } else if (bicubic) {
commit-bot@chromium.orgbc91fd72013-12-10 12:53:39 +00001384 SkASSERT(GrTextureParams::kNone_FilterMode == params.filterMode());
1385 SkShader::TileMode tileModes[2] = { params.getTileModeX(), params.getTileModeY() };
1386 effect.reset(GrBicubicEffect::Create(texture, SkMatrix::I(), tileModes));
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001387 } else {
1388 effect.reset(GrSimpleTextureEffect::Create(texture, SkMatrix::I(), params));
1389 }
1390
1391 // Construct a GrPaint by setting the bitmap texture as the first effect and then configuring
1392 // the rest from the SkPaint.
1393 GrPaint grPaint;
1394 grPaint.addColorEffect(effect);
1395 bool alphaOnly = !(SkBitmap::kA8_Config == bitmap.config());
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +00001396 SkPaint2GrPaintNoShader(this, paint, alphaOnly, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001397
1398 fContext->drawRectToRect(grPaint, dstRect, paintRect, NULL);
1399}
1400
1401static bool filter_texture(SkBaseDevice* device, GrContext* context,
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001402 GrTexture* texture, const SkImageFilter* filter,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001403 int w, int h, const SkImageFilter::Context& ctx,
1404 SkBitmap* result, SkIPoint* offset) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001405 SkASSERT(filter);
1406 SkDeviceImageFilterProxy proxy(device);
1407
1408 if (filter->canFilterImageGPU()) {
1409 // Save the render target and set it to NULL, so we don't accidentally draw to it in the
1410 // filter. Also set the clip wide open and the matrix to identity.
1411 GrContext::AutoWideOpenIdentityDraw awo(context, NULL);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001412 return filter->filterImageGPU(&proxy, wrap_texture(texture), ctx, result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001413 } else {
1414 return false;
1415 }
1416}
1417
1418void SkGpuDevice::drawSprite(const SkDraw& draw, const SkBitmap& bitmap,
1419 int left, int top, const SkPaint& paint) {
1420 // drawSprite is defined to be in device coords.
1421 CHECK_SHOULD_DRAW(draw, true);
1422
1423 SkAutoLockPixels alp(bitmap, !bitmap.getTexture());
1424 if (!bitmap.getTexture() && !bitmap.readyToDraw()) {
1425 return;
1426 }
1427
1428 int w = bitmap.width();
1429 int h = bitmap.height();
1430
1431 GrTexture* texture;
1432 // draw sprite uses the default texture params
1433 SkAutoCachedTexture act(this, bitmap, NULL, &texture);
1434
1435 SkImageFilter* filter = paint.getImageFilter();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001436 // This bitmap will own the filtered result as a texture.
1437 SkBitmap filteredBitmap;
1438
1439 if (NULL != filter) {
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001440 SkIPoint offset = SkIPoint::Make(0, 0);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001441 SkMatrix matrix(*draw.fMatrix);
1442 matrix.postTranslate(SkIntToScalar(-left), SkIntToScalar(-top));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001443 SkIRect clipBounds = SkIRect::MakeWH(bitmap.width(), bitmap.height());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001444 SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
1445 SkAutoUnref aur(cache);
1446 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001447 if (filter_texture(this, fContext, texture, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001448 &offset)) {
1449 texture = (GrTexture*) filteredBitmap.getTexture();
1450 w = filteredBitmap.width();
1451 h = filteredBitmap.height();
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001452 left += offset.x();
1453 top += offset.y();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001454 } else {
1455 return;
1456 }
1457 }
1458
1459 GrPaint grPaint;
1460 grPaint.addColorTextureEffect(texture, SkMatrix::I());
1461
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +00001462 SkPaint2GrPaintNoShader(this, paint, true, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001463
1464 fContext->drawRectToRect(grPaint,
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001465 SkRect::MakeXYWH(SkIntToScalar(left),
1466 SkIntToScalar(top),
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001467 SkIntToScalar(w),
1468 SkIntToScalar(h)),
1469 SkRect::MakeXYWH(0,
1470 0,
1471 SK_Scalar1 * w / texture->width(),
1472 SK_Scalar1 * h / texture->height()));
1473}
1474
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001475void SkGpuDevice::drawBitmapRect(const SkDraw& origDraw, const SkBitmap& bitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001476 const SkRect* src, const SkRect& dst,
1477 const SkPaint& paint,
1478 SkCanvas::DrawBitmapRectFlags flags) {
1479 SkMatrix matrix;
1480 SkRect bitmapBounds, tmpSrc;
1481
1482 bitmapBounds.set(0, 0,
1483 SkIntToScalar(bitmap.width()),
1484 SkIntToScalar(bitmap.height()));
1485
1486 // Compute matrix from the two rectangles
1487 if (NULL != src) {
1488 tmpSrc = *src;
1489 } else {
1490 tmpSrc = bitmapBounds;
1491 }
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001492
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001493 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
1494
1495 // clip the tmpSrc to the bounds of the bitmap. No check needed if src==null.
1496 if (NULL != src) {
1497 if (!bitmapBounds.contains(tmpSrc)) {
1498 if (!tmpSrc.intersect(bitmapBounds)) {
1499 return; // nothing to draw
1500 }
1501 }
1502 }
1503
commit-bot@chromium.orga7d89c82014-01-13 14:47:00 +00001504 SkRect tmpDst;
1505 matrix.mapRect(&tmpDst, tmpSrc);
1506
1507 SkTCopyOnFirstWrite<SkDraw> draw(origDraw);
1508 if (0 != tmpDst.fLeft || 0 != tmpDst.fTop) {
1509 // Translate so that tempDst's top left is at the origin.
1510 matrix = *origDraw.fMatrix;
1511 matrix.preTranslate(tmpDst.fLeft, tmpDst.fTop);
1512 draw.writable()->fMatrix = &matrix;
1513 }
1514 SkSize dstSize;
1515 dstSize.fWidth = tmpDst.width();
1516 dstSize.fHeight = tmpDst.height();
1517
1518 this->drawBitmapCommon(*draw, bitmap, &tmpSrc, &dstSize, paint, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001519}
1520
1521void SkGpuDevice::drawDevice(const SkDraw& draw, SkBaseDevice* device,
1522 int x, int y, const SkPaint& paint) {
1523 // clear of the source device must occur before CHECK_SHOULD_DRAW
1524 SkGpuDevice* dev = static_cast<SkGpuDevice*>(device);
1525 if (dev->fNeedClear) {
1526 // TODO: could check here whether we really need to draw at all
1527 dev->clear(0x0);
1528 }
1529
1530 // drawDevice is defined to be in device coords.
1531 CHECK_SHOULD_DRAW(draw, true);
1532
1533 GrRenderTarget* devRT = dev->accessRenderTarget();
1534 GrTexture* devTex;
1535 if (NULL == (devTex = devRT->asTexture())) {
1536 return;
1537 }
1538
1539 const SkBitmap& bm = dev->accessBitmap(false);
1540 int w = bm.width();
1541 int h = bm.height();
1542
1543 SkImageFilter* filter = paint.getImageFilter();
1544 // This bitmap will own the filtered result as a texture.
1545 SkBitmap filteredBitmap;
1546
1547 if (NULL != filter) {
1548 SkIPoint offset = SkIPoint::Make(0, 0);
1549 SkMatrix matrix(*draw.fMatrix);
1550 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001551 SkIRect clipBounds = SkIRect::MakeWH(devTex->width(), devTex->height());
commit-bot@chromium.orgf7efa502014-04-11 18:57:00 +00001552 SkImageFilter::Cache* cache = SkImageFilter::Cache::Create();
1553 SkAutoUnref aur(cache);
1554 SkImageFilter::Context ctx(matrix, clipBounds, cache);
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001555 if (filter_texture(this, fContext, devTex, filter, w, h, ctx, &filteredBitmap,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001556 &offset)) {
1557 devTex = filteredBitmap.getTexture();
1558 w = filteredBitmap.width();
1559 h = filteredBitmap.height();
1560 x += offset.fX;
1561 y += offset.fY;
1562 } else {
1563 return;
1564 }
1565 }
1566
1567 GrPaint grPaint;
1568 grPaint.addColorTextureEffect(devTex, SkMatrix::I());
1569
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +00001570 SkPaint2GrPaintNoShader(this, paint, true, false, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001571
1572 SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(x),
1573 SkIntToScalar(y),
1574 SkIntToScalar(w),
1575 SkIntToScalar(h));
1576
1577 // The device being drawn may not fill up its texture (e.g. saveLayer uses approximate
1578 // scratch texture).
1579 SkRect srcRect = SkRect::MakeWH(SK_Scalar1 * w / devTex->width(),
1580 SK_Scalar1 * h / devTex->height());
1581
1582 fContext->drawRectToRect(grPaint, dstRect, srcRect);
1583}
1584
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001585bool SkGpuDevice::canHandleImageFilter(const SkImageFilter* filter) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001586 return filter->canFilterImageGPU();
1587}
1588
commit-bot@chromium.orgae761f72014-02-05 22:32:02 +00001589bool SkGpuDevice::filterImage(const SkImageFilter* filter, const SkBitmap& src,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001590 const SkImageFilter::Context& ctx,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001591 SkBitmap* result, SkIPoint* offset) {
1592 // want explicitly our impl, so guard against a subclass of us overriding it
1593 if (!this->SkGpuDevice::canHandleImageFilter(filter)) {
1594 return false;
1595 }
1596
1597 SkAutoLockPixels alp(src, !src.getTexture());
1598 if (!src.getTexture() && !src.readyToDraw()) {
1599 return false;
1600 }
1601
1602 GrTexture* texture;
1603 // We assume here that the filter will not attempt to tile the src. Otherwise, this cache lookup
1604 // must be pushed upstack.
1605 SkAutoCachedTexture act(this, src, NULL, &texture);
1606
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +00001607 return filter_texture(this, fContext, texture, filter, src.width(), src.height(), ctx,
1608 result, offset);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001609}
1610
1611///////////////////////////////////////////////////////////////////////////////
1612
1613// must be in SkCanvas::VertexMode order
1614static const GrPrimitiveType gVertexMode2PrimitiveType[] = {
1615 kTriangles_GrPrimitiveType,
1616 kTriangleStrip_GrPrimitiveType,
1617 kTriangleFan_GrPrimitiveType,
1618};
1619
1620void SkGpuDevice::drawVertices(const SkDraw& draw, SkCanvas::VertexMode vmode,
1621 int vertexCount, const SkPoint vertices[],
1622 const SkPoint texs[], const SkColor colors[],
1623 SkXfermode* xmode,
1624 const uint16_t indices[], int indexCount,
1625 const SkPaint& paint) {
1626 CHECK_SHOULD_DRAW(draw, false);
1627
1628 GrPaint grPaint;
1629 // we ignore the shader if texs is null.
1630 if (NULL == texs) {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +00001631 SkPaint2GrPaintNoShader(this, paint, false, NULL == colors, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001632 } else {
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +00001633 SkPaint2GrPaintShader(this, paint, NULL == colors, &grPaint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001634 }
1635
1636 if (NULL != xmode && NULL != texs && NULL != colors) {
1637 if (!SkXfermode::IsMode(xmode, SkXfermode::kModulate_Mode)) {
1638 SkDebugf("Unsupported vertex-color/texture xfer mode.\n");
1639#if 0
1640 return
1641#endif
1642 }
1643 }
1644
1645 SkAutoSTMalloc<128, GrColor> convertedColors(0);
1646 if (NULL != colors) {
1647 // need to convert byte order and from non-PM to PM
1648 convertedColors.reset(vertexCount);
1649 for (int i = 0; i < vertexCount; ++i) {
1650 convertedColors[i] = SkColor2GrColor(colors[i]);
1651 }
1652 colors = convertedColors.get();
1653 }
1654 fContext->drawVertices(grPaint,
1655 gVertexMode2PrimitiveType[vmode],
1656 vertexCount,
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001657 vertices,
1658 texs,
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001659 colors,
1660 indices,
1661 indexCount);
1662}
1663
1664///////////////////////////////////////////////////////////////////////////////
1665
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001666void SkGpuDevice::drawText(const SkDraw& draw, const void* text,
1667 size_t byteLength, SkScalar x, SkScalar y,
1668 const SkPaint& paint) {
1669 CHECK_SHOULD_DRAW(draw, false);
1670
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001671 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001672 GrPaint grPaint;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +00001673 SkPaint2GrPaintShader(this, paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001674
1675 SkDEBUGCODE(this->validate();)
1676
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001677 fMainTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
1678 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001679 GrPaint grPaint;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +00001680 SkPaint2GrPaintShader(this, paint, true, &grPaint);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001681
1682 SkDEBUGCODE(this->validate();)
1683
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001684 fFallbackTextContext->drawText(grPaint, paint, (const char *)text, byteLength, x, y);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001685 } else {
1686 // this guy will just call our drawPath()
1687 draw.drawText_asPaths((const char*)text, byteLength, x, y, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001688 }
1689}
1690
1691void SkGpuDevice::drawPosText(const SkDraw& draw, const void* text,
1692 size_t byteLength, const SkScalar pos[],
1693 SkScalar constY, int scalarsPerPos,
1694 const SkPaint& paint) {
1695 CHECK_SHOULD_DRAW(draw, false);
1696
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001697 if (fMainTextContext->canDraw(paint)) {
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001698 GrPaint grPaint;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +00001699 SkPaint2GrPaintShader(this, paint, true, &grPaint);
commit-bot@chromium.org8128d8c2013-12-19 16:12:25 +00001700
1701 SkDEBUGCODE(this->validate();)
1702
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001703 fMainTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001704 constY, scalarsPerPos);
1705 } else if (fFallbackTextContext && fFallbackTextContext->canDraw(paint)) {
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001706 GrPaint grPaint;
commit-bot@chromium.org8dcff642014-05-15 20:32:48 +00001707 SkPaint2GrPaintShader(this, paint, true, &grPaint);
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001708
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001709 SkDEBUGCODE(this->validate();)
skia.committer@gmail.com4c18e9f2014-01-31 03:01:59 +00001710
1711 fFallbackTextContext->drawPosText(grPaint, paint, (const char *)text, byteLength, pos,
commit-bot@chromium.orgcbbc4812014-01-30 22:05:47 +00001712 constY, scalarsPerPos);
commit-bot@chromium.org9f94b912014-01-30 15:22:54 +00001713 } else {
1714 draw.drawPosText_asPaths((const char*)text, byteLength, pos, constY,
1715 scalarsPerPos, paint);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001716 }
1717}
1718
1719void SkGpuDevice::drawTextOnPath(const SkDraw& draw, const void* text,
1720 size_t len, const SkPath& path,
1721 const SkMatrix* m, const SkPaint& paint) {
1722 CHECK_SHOULD_DRAW(draw, false);
1723
1724 SkASSERT(draw.fDevice == this);
1725 draw.drawTextOnPath((const char*)text, len, path, m, paint);
1726}
1727
1728///////////////////////////////////////////////////////////////////////////////
1729
1730bool SkGpuDevice::filterTextFlags(const SkPaint& paint, TextFlags* flags) {
1731 if (!paint.isLCDRenderText()) {
1732 // we're cool with the paint as is
1733 return false;
1734 }
1735
1736 if (paint.getShader() ||
1737 paint.getXfermode() || // unless its srcover
1738 paint.getMaskFilter() ||
1739 paint.getRasterizer() ||
1740 paint.getColorFilter() ||
1741 paint.getPathEffect() ||
1742 paint.isFakeBoldText() ||
1743 paint.getStyle() != SkPaint::kFill_Style) {
1744 // turn off lcd
1745 flags->fFlags = paint.getFlags() & ~SkPaint::kLCDRenderText_Flag;
1746 flags->fHinting = paint.getHinting();
1747 return true;
1748 }
1749 // we're cool with the paint as is
1750 return false;
1751}
1752
1753void SkGpuDevice::flush() {
1754 DO_DEFERRED_CLEAR();
1755 fContext->resolveRenderTarget(fRenderTarget);
1756}
1757
1758///////////////////////////////////////////////////////////////////////////////
1759
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001760SkBaseDevice* SkGpuDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001761 GrTextureDesc desc;
1762 desc.fConfig = fRenderTarget->config();
1763 desc.fFlags = kRenderTarget_GrTextureFlagBit;
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001764 desc.fWidth = info.width();
1765 desc.fHeight = info.height();
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001766 desc.fSampleCnt = fRenderTarget->numSamples();
1767
1768 SkAutoTUnref<GrTexture> texture;
1769 // Skia's convention is to only clear a device if it is non-opaque.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001770 unsigned flags = info.isOpaque() ? 0 : kNeedClear_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001771
1772#if CACHE_COMPATIBLE_DEVICE_TEXTURES
1773 // layers are never draw in repeat modes, so we can request an approx
1774 // match and ignore any padding.
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001775 flags |= kCached_Flag;
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001776 const GrContext::ScratchTexMatch match = (kSaveLayer_Usage == usage) ?
1777 GrContext::kApprox_ScratchTexMatch :
1778 GrContext::kExact_ScratchTexMatch;
1779 texture.reset(fContext->lockAndRefScratchTexture(desc, match));
1780#else
1781 texture.reset(fContext->createUncachedTexture(desc, NULL, 0));
1782#endif
1783 if (NULL != texture.get()) {
commit-bot@chromium.orgd8a57af2014-03-19 21:19:16 +00001784 return SkGpuDevice::Create(texture, flags);
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001785 } else {
commit-bot@chromium.org15a14052014-02-16 00:59:25 +00001786 GrPrintf("---- failed to create compatible device texture [%d %d]\n",
1787 info.width(), info.height());
skia.committer@gmail.com11a253b2013-11-12 07:02:05 +00001788 return NULL;
1789 }
1790}
1791
reed@google.com76f10a32014-02-05 15:32:21 +00001792SkSurface* SkGpuDevice::newSurface(const SkImageInfo& info) {
1793 return SkSurface::NewRenderTarget(fContext, info, fRenderTarget->numSamples());
1794}
1795
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001796void SkGpuDevice::EXPERIMENTAL_optimize(SkPicture* picture) {
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001797 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001798
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001799 const SkPicture::AccelData* existing = picture->EXPERIMENTAL_getAccelData(key);
1800 if (NULL != existing) {
1801 return;
1802 }
1803
commit-bot@chromium.org8fd93822014-05-06 13:43:22 +00001804 SkAutoTUnref<GPUAccelData> data(SkNEW_ARGS(GPUAccelData, (key)));
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001805
1806 picture->EXPERIMENTAL_addAccelData(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001807
1808 GatherGPUInfo(picture, data);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001809}
1810
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001811static void wrap_texture(GrTexture* texture, int width, int height, SkBitmap* result) {
1812 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
1813 result->setConfig(info);
1814 result->setPixelRef(SkNEW_ARGS(SkGrPixelRef, (info, texture)))->unref();
1815}
1816
commit-bot@chromium.orgc8733292014-04-11 15:54:14 +00001817void SkGpuDevice::EXPERIMENTAL_purge(SkPicture* picture) {
1818
1819}
1820
1821bool SkGpuDevice::EXPERIMENTAL_drawPicture(SkCanvas* canvas, SkPicture* picture) {
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001822
commit-bot@chromium.org0205aba2014-05-06 12:02:22 +00001823 SkPicture::AccelData::Key key = GPUAccelData::ComputeAccelDataKey();
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001824
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001825 const SkPicture::AccelData* data = picture->EXPERIMENTAL_getAccelData(key);
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001826 if (NULL == data) {
1827 return false;
1828 }
1829
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001830 const GPUAccelData *gpuData = static_cast<const GPUAccelData*>(data);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001831
commit-bot@chromium.org8ec8bab2014-05-14 13:11:48 +00001832 if (0 == gpuData->numSaveLayers()) {
1833 return false;
1834 }
1835
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001836 SkAutoTArray<bool> pullForward(gpuData->numSaveLayers());
1837 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1838 pullForward[i] = false;
1839 }
1840
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001841 SkRect clipBounds;
1842 if (!canvas->getClipBounds(&clipBounds)) {
1843 return true;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001844 }
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001845 SkIRect query;
1846 clipBounds.roundOut(&query);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001847
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001848 const SkPicture::OperationList& ops = picture->EXPERIMENTAL_getActiveOps(query);
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001849
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001850 // This code pre-renders the entire layer since it will be cached and potentially
1851 // reused with different clips (e.g., in different tiles). Because of this the
1852 // clip will not be limiting the size of the pre-rendered layer. kSaveLayerMaxSize
1853 // is used to limit which clips are pre-rendered.
1854 static const int kSaveLayerMaxSize = 256;
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001855
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001856 if (ops.valid()) {
1857 // In this case the picture has been generated with a BBH so we use
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001858 // the BBH to limit the pre-rendering to just the layers needed to cover
1859 // the region being drawn
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001860 for (int i = 0; i < ops.numOps(); ++i) {
1861 uint32_t offset = ops.offset(i);
1862
1863 // For now we're saving all the layers in the GPUAccelData so they
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001864 // can be nested. Additionally, the nested layers appear before
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001865 // their parent in the list.
1866 for (int j = 0 ; j < gpuData->numSaveLayers(); ++j) {
1867 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1868
1869 if (pullForward[j]) {
1870 continue; // already pulling forward
1871 }
1872
1873 if (offset < info.fSaveLayerOpID || offset > info.fRestoreOpID) {
1874 continue; // the op isn't in this range
1875 }
1876
1877 // TODO: once this code is more stable unsuitable layers can
1878 // just be omitted during the optimization stage
1879 if (!info.fValid ||
1880 kSaveLayerMaxSize < info.fSize.fWidth ||
1881 kSaveLayerMaxSize < info.fSize.fHeight ||
1882 info.fIsNested) {
1883 continue; // this layer is unsuitable
1884 }
1885
1886 pullForward[j] = true;
1887 }
1888 }
1889 } else {
1890 // In this case there is no BBH associated with the picture. Pre-render
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +00001891 // all the layers that intersect the drawn region
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001892 for (int j = 0; j < gpuData->numSaveLayers(); ++j) {
1893 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(j);
1894
commit-bot@chromium.orgf97d65d2014-05-08 23:24:05 +00001895 SkIRect layerRect = SkIRect::MakeXYWH(info.fOffset.fX,
1896 info.fOffset.fY,
1897 info.fSize.fWidth,
1898 info.fSize.fHeight);
1899
1900 if (!SkIRect::Intersects(query, layerRect)) {
1901 continue;
1902 }
1903
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001904 // TODO: once this code is more stable unsuitable layers can
1905 // just be omitted during the optimization stage
1906 if (!info.fValid ||
1907 kSaveLayerMaxSize < info.fSize.fWidth ||
1908 kSaveLayerMaxSize < info.fSize.fHeight ||
1909 info.fIsNested) {
1910 continue;
1911 }
1912
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001913 pullForward[j] = true;
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001914 }
1915 }
1916
1917 SkPicturePlayback::PlaybackReplacements replacements;
1918
1919 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1920 if (pullForward[i]) {
1921 GrCachedLayer* layer = fContext->getLayerCache()->findLayerOrCreate(picture, i);
1922
1923 const GPUAccelData::SaveLayerInfo& info = gpuData->saveLayerInfo(i);
1924
1925 if (NULL != picture->fPlayback) {
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001926 SkPicturePlayback::PlaybackReplacements::ReplacementInfo* layerInfo =
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001927 replacements.push();
1928 layerInfo->fStart = info.fSaveLayerOpID;
1929 layerInfo->fStop = info.fRestoreOpID;
1930 layerInfo->fPos = info.fOffset;
1931
1932 GrTextureDesc desc;
1933 desc.fFlags = kRenderTarget_GrTextureFlagBit;
1934 desc.fWidth = info.fSize.fWidth;
1935 desc.fHeight = info.fSize.fHeight;
1936 desc.fConfig = kSkia8888_GrPixelConfig;
1937 // TODO: need to deal with sample count
1938
1939 bool bNeedsRendering = true;
1940
1941 // This just uses scratch textures and doesn't cache the texture.
1942 // This can yield a lot of re-rendering
1943 if (NULL == layer->getTexture()) {
skia.committer@gmail.comb2c82c92014-05-08 03:05:29 +00001944 layer->setTexture(fContext->lockAndRefScratchTexture(desc,
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001945 GrContext::kApprox_ScratchTexMatch));
1946 if (NULL == layer->getTexture()) {
1947 continue;
1948 }
1949 } else {
1950 bNeedsRendering = false;
1951 }
1952
1953 layerInfo->fBM = SkNEW(SkBitmap);
1954 wrap_texture(layer->getTexture(), desc.fWidth, desc.fHeight, layerInfo->fBM);
1955
1956 SkASSERT(info.fPaint);
1957 layerInfo->fPaint = info.fPaint;
1958
1959 if (bNeedsRendering) {
1960 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTargetDirect(
1961 layer->getTexture()->asRenderTarget()));
1962
1963 SkCanvas* canvas = surface->getCanvas();
1964
1965 canvas->setMatrix(info.fCTM);
1966 canvas->clear(SK_ColorTRANSPARENT);
1967
1968 picture->fPlayback->setDrawLimits(info.fSaveLayerOpID, info.fRestoreOpID);
1969 picture->fPlayback->draw(*canvas, NULL);
1970 picture->fPlayback->setDrawLimits(0, 0);
1971 canvas->flush();
1972 }
commit-bot@chromium.org8ddc26b2014-03-31 17:55:12 +00001973 }
1974 }
1975 }
1976
robertphillips@google.combeb1af22014-05-07 21:31:09 +00001977 // Playback using new layers
1978 picture->fPlayback->setReplacements(&replacements);
1979 picture->fPlayback->draw(*canvas, NULL);
1980 picture->fPlayback->setReplacements(NULL);
1981
1982 for (int i = 0; i < gpuData->numSaveLayers(); ++i) {
1983 GrCachedLayer* layer = fContext->getLayerCache()->findLayerOrCreate(picture, i);
1984
1985 if (NULL != layer->getTexture()) {
1986 fContext->unlockScratchTexture(layer->getTexture());
1987 layer->setTexture(NULL);
1988 }
1989 }
1990
1991 return true;
commit-bot@chromium.org145d1c02014-03-16 19:46:36 +00001992}