1 /*
2 * Copyright 2013 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 "include/core/SkImageFilter.h"
9 #include "include/core/SkMatrix.h"
10 #include "include/core/SkPaint.h"
11 #include "include/core/SkPath.h"
12 #include "include/core/SkPixmap.h"
13 #include "include/core/SkRasterHandleAllocator.h"
14 #include "include/core/SkShader.h"
15 #include "include/core/SkSurface.h"
16 #include "include/core/SkVertices.h"
17 #include "src/core/SkBitmapDevice.h"
18 #include "src/core/SkDraw.h"
19 #include "src/core/SkGlyphRun.h"
20 #include "src/core/SkImageFilterCache.h"
21 #include "src/core/SkImageFilter_Base.h"
22 #include "src/core/SkMakeUnique.h"
23 #include "src/core/SkRasterClip.h"
24 #include "src/core/SkSpecialImage.h"
25 #include "src/core/SkStrikeCache.h"
26 #include "src/core/SkTLazy.h"
27
28 struct Bounder {
29 SkRect fBounds;
30 bool fHasBounds;
31
BounderBounder32 Bounder(const SkRect& r, const SkPaint& paint) {
33 if ((fHasBounds = paint.canComputeFastBounds())) {
34 fBounds = paint.computeFastBounds(r, &fBounds);
35 }
36 }
37
hasBoundsBounder38 bool hasBounds() const { return fHasBounds; }
boundsBounder39 const SkRect* bounds() const { return fHasBounds ? &fBounds : nullptr; }
operator const SkRect*Bounder40 operator const SkRect* () const { return this->bounds(); }
41 };
42
43 class SkDrawTiler {
44 enum {
45 // 8K is 1 too big, since 8K << supersample == 32768 which is too big for SkFixed
46 kMaxDim = 8192 - 1
47 };
48
49 SkBitmapDevice* fDevice;
50 SkPixmap fRootPixmap;
51 SkIRect fSrcBounds;
52
53 // Used for tiling and non-tiling
54 SkDraw fDraw;
55
56 // fCurr... are only used if fNeedTiling
57 SkMatrix fTileMatrix;
58 SkRasterClip fTileRC;
59 SkIPoint fOrigin;
60
61 bool fDone, fNeedsTiling;
62
63 public:
NeedsTiling(SkBitmapDevice * dev)64 static bool NeedsTiling(SkBitmapDevice* dev) {
65 return dev->width() > kMaxDim || dev->height() > kMaxDim;
66 }
67
SkDrawTiler(SkBitmapDevice * dev,const SkRect * bounds)68 SkDrawTiler(SkBitmapDevice* dev, const SkRect* bounds) : fDevice(dev) {
69 fDone = false;
70
71 // we need fDst to be set, and if we're actually drawing, to dirty the genID
72 if (!dev->accessPixels(&fRootPixmap)) {
73 // NoDrawDevice uses us (why?) so we have to catch this case w/ no pixels
74 fRootPixmap.reset(dev->imageInfo(), nullptr, 0);
75 }
76
77 // do a quick check, so we don't even have to process "bounds" if there is no need
78 const SkIRect clipR = dev->fRCStack.rc().getBounds();
79 fNeedsTiling = clipR.right() > kMaxDim || clipR.bottom() > kMaxDim;
80 if (fNeedsTiling) {
81 if (bounds) {
82 // Make sure we round first, and then intersect. We can't rely on promoting the
83 // clipR to floats (and then intersecting with devBounds) since promoting
84 // int --> float can make the float larger than the int.
85 // rounding(out) first runs the risk of clamping if the float is larger an intmax
86 // but our roundOut() is saturating, which is fine for this use case
87 //
88 // e.g. the older version of this code did this:
89 // devBounds = mapRect(bounds);
90 // if (devBounds.intersect(SkRect::Make(clipR))) {
91 // fSrcBounds = devBounds.roundOut();
92 // The problem being that the promotion of clipR to SkRect was unreliable
93 //
94 fSrcBounds = dev->ctm().mapRect(*bounds).roundOut();
95 if (fSrcBounds.intersect(clipR)) {
96 // Check again, now that we have computed srcbounds.
97 fNeedsTiling = fSrcBounds.right() > kMaxDim || fSrcBounds.bottom() > kMaxDim;
98 } else {
99 fNeedsTiling = false;
100 fDone = true;
101 }
102 } else {
103 fSrcBounds = clipR;
104 }
105 }
106
107 if (fNeedsTiling) {
108 // fDraw.fDst is reset each time in setupTileDraw()
109 fDraw.fMatrix = &fTileMatrix;
110 fDraw.fRC = &fTileRC;
111 // we'll step/increase it before using it
112 fOrigin.set(fSrcBounds.fLeft - kMaxDim, fSrcBounds.fTop);
113 } else {
114 // don't reference fSrcBounds, as it may not have been set
115 fDraw.fDst = fRootPixmap;
116 fDraw.fMatrix = &dev->ctm();
117 fDraw.fRC = &dev->fRCStack.rc();
118 fOrigin.set(0, 0);
119
120 fDraw.fCoverage = dev->accessCoverage();
121 }
122 }
123
needsTiling() const124 bool needsTiling() const { return fNeedsTiling; }
125
next()126 const SkDraw* next() {
127 if (fDone) {
128 return nullptr;
129 }
130 if (fNeedsTiling) {
131 do {
132 this->stepAndSetupTileDraw(); // might set the clip to empty and fDone to true
133 } while (!fDone && fTileRC.isEmpty());
134 // if we exit the loop and we're still empty, we're (past) done
135 if (fTileRC.isEmpty()) {
136 SkASSERT(fDone);
137 return nullptr;
138 }
139 SkASSERT(!fTileRC.isEmpty());
140 } else {
141 fDone = true; // only draw untiled once
142 }
143 return &fDraw;
144 }
145
146 private:
stepAndSetupTileDraw()147 void stepAndSetupTileDraw() {
148 SkASSERT(!fDone);
149 SkASSERT(fNeedsTiling);
150
151 // We do fRootPixmap.width() - kMaxDim instead of fOrigin.fX + kMaxDim to avoid overflow.
152 if (fOrigin.fX >= fSrcBounds.fRight - kMaxDim) { // too far
153 fOrigin.fX = fSrcBounds.fLeft;
154 fOrigin.fY += kMaxDim;
155 } else {
156 fOrigin.fX += kMaxDim;
157 }
158 // fDone = next origin will be invalid.
159 fDone = fOrigin.fX >= fSrcBounds.fRight - kMaxDim &&
160 fOrigin.fY >= fSrcBounds.fBottom - kMaxDim;
161
162 SkIRect bounds = SkIRect::MakeXYWH(fOrigin.x(), fOrigin.y(), kMaxDim, kMaxDim);
163 SkASSERT(!bounds.isEmpty());
164 bool success = fRootPixmap.extractSubset(&fDraw.fDst, bounds);
165 SkASSERT_RELEASE(success);
166 // now don't use bounds, since fDst has the clipped dimensions.
167
168 fTileMatrix = fDevice->ctm();
169 fTileMatrix.postTranslate(SkIntToScalar(-fOrigin.x()), SkIntToScalar(-fOrigin.y()));
170 fDevice->fRCStack.rc().translate(-fOrigin.x(), -fOrigin.y(), &fTileRC);
171 fTileRC.op(SkIRect::MakeWH(fDraw.fDst.width(), fDraw.fDst.height()),
172 SkRegion::kIntersect_Op);
173 }
174 };
175
176 // Passing a bounds allows the tiler to only visit the dst-tiles that might intersect the
177 // drawing. If null is passed, the tiler has to visit everywhere. The bounds is expected to be
178 // in local coordinates, as the tiler itself will transform that into device coordinates.
179 //
180 #define LOOP_TILER(code, boundsPtr) \
181 SkDrawTiler priv_tiler(this, boundsPtr); \
182 while (const SkDraw* priv_draw = priv_tiler.next()) { \
183 priv_draw->code; \
184 }
185
186 // Helper to create an SkDraw from a device
187 class SkBitmapDevice::BDDraw : public SkDraw {
188 public:
BDDraw(SkBitmapDevice * dev)189 BDDraw(SkBitmapDevice* dev) {
190 // we need fDst to be set, and if we're actually drawing, to dirty the genID
191 if (!dev->accessPixels(&fDst)) {
192 // NoDrawDevice uses us (why?) so we have to catch this case w/ no pixels
193 fDst.reset(dev->imageInfo(), nullptr, 0);
194 }
195 fMatrix = &dev->ctm();
196 fRC = &dev->fRCStack.rc();
197 fCoverage = dev->accessCoverage();
198 }
199 };
200
valid_for_bitmap_device(const SkImageInfo & info,SkAlphaType * newAlphaType)201 static bool valid_for_bitmap_device(const SkImageInfo& info,
202 SkAlphaType* newAlphaType) {
203 if (info.width() < 0 || info.height() < 0 || kUnknown_SkColorType == info.colorType()) {
204 return false;
205 }
206
207 if (newAlphaType) {
208 *newAlphaType = SkColorTypeIsAlwaysOpaque(info.colorType()) ? kOpaque_SkAlphaType
209 : info.alphaType();
210 }
211
212 return true;
213 }
214
SkBitmapDevice(const SkBitmap & bitmap)215 SkBitmapDevice::SkBitmapDevice(const SkBitmap& bitmap)
216 : INHERITED(bitmap.info(), SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType))
217 , fBitmap(bitmap)
218 , fRCStack(bitmap.width(), bitmap.height())
219 , fGlyphPainter(this->surfaceProps(),
220 bitmap.colorType(),
221 bitmap.colorSpace(),
222 SkStrikeCache::GlobalStrikeCache()) {
223 SkASSERT(valid_for_bitmap_device(bitmap.info(), nullptr));
224 }
225
Create(const SkImageInfo & info)226 SkBitmapDevice* SkBitmapDevice::Create(const SkImageInfo& info) {
227 return Create(info, SkSurfaceProps(SkSurfaceProps::kLegacyFontHost_InitType));
228 }
229
SkBitmapDevice(const SkBitmap & bitmap,const SkSurfaceProps & surfaceProps,SkRasterHandleAllocator::Handle hndl,const SkBitmap * coverage)230 SkBitmapDevice::SkBitmapDevice(const SkBitmap& bitmap, const SkSurfaceProps& surfaceProps,
231 SkRasterHandleAllocator::Handle hndl, const SkBitmap* coverage)
232 : INHERITED(bitmap.info(), surfaceProps)
233 , fBitmap(bitmap)
234 , fRasterHandle(hndl)
235 , fRCStack(bitmap.width(), bitmap.height())
236 , fGlyphPainter(this->surfaceProps(),
237 bitmap.colorType(),
238 bitmap.colorSpace(),
239 SkStrikeCache::GlobalStrikeCache()) {
240 SkASSERT(valid_for_bitmap_device(bitmap.info(), nullptr));
241
242 if (coverage) {
243 SkASSERT(coverage->width() == bitmap.width());
244 SkASSERT(coverage->height() == bitmap.height());
245 fCoverage = skstd::make_unique<SkBitmap>(*coverage);
246 }
247 }
248
Create(const SkImageInfo & origInfo,const SkSurfaceProps & surfaceProps,bool trackCoverage,SkRasterHandleAllocator * allocator)249 SkBitmapDevice* SkBitmapDevice::Create(const SkImageInfo& origInfo,
250 const SkSurfaceProps& surfaceProps,
251 bool trackCoverage,
252 SkRasterHandleAllocator* allocator) {
253 SkAlphaType newAT = origInfo.alphaType();
254 if (!valid_for_bitmap_device(origInfo, &newAT)) {
255 return nullptr;
256 }
257
258 SkRasterHandleAllocator::Handle hndl = nullptr;
259 const SkImageInfo info = origInfo.makeAlphaType(newAT);
260 SkBitmap bitmap;
261
262 if (kUnknown_SkColorType == info.colorType()) {
263 if (!bitmap.setInfo(info)) {
264 return nullptr;
265 }
266 } else if (allocator) {
267 hndl = allocator->allocBitmap(info, &bitmap);
268 if (!hndl) {
269 return nullptr;
270 }
271 } else if (info.isOpaque()) {
272 // If this bitmap is opaque, we don't have any sensible default color,
273 // so we just return uninitialized pixels.
274 if (!bitmap.tryAllocPixels(info)) {
275 return nullptr;
276 }
277 } else {
278 // This bitmap has transparency, so we'll zero the pixels (to transparent).
279 // We use the flag as a faster alloc-then-eraseColor(SK_ColorTRANSPARENT).
280 if (!bitmap.tryAllocPixelsFlags(info, SkBitmap::kZeroPixels_AllocFlag)) {
281 return nullptr;
282 }
283 }
284
285 SkBitmap coverage;
286 if (trackCoverage) {
287 SkImageInfo ci = SkImageInfo::Make(info.width(), info.height(), kAlpha_8_SkColorType,
288 kPremul_SkAlphaType);
289 if (!coverage.tryAllocPixelsFlags(ci, SkBitmap::kZeroPixels_AllocFlag)) {
290 return nullptr;
291 }
292 }
293
294 return new SkBitmapDevice(bitmap, surfaceProps, hndl, trackCoverage ? &coverage : nullptr);
295 }
296
replaceBitmapBackendForRasterSurface(const SkBitmap & bm)297 void SkBitmapDevice::replaceBitmapBackendForRasterSurface(const SkBitmap& bm) {
298 SkASSERT(bm.width() == fBitmap.width());
299 SkASSERT(bm.height() == fBitmap.height());
300 fBitmap = bm; // intent is to use bm's pixelRef (and rowbytes/config)
301 this->privateResize(fBitmap.info().width(), fBitmap.info().height());
302 }
303
onCreateDevice(const CreateInfo & cinfo,const SkPaint *)304 SkBaseDevice* SkBitmapDevice::onCreateDevice(const CreateInfo& cinfo, const SkPaint*) {
305 const SkSurfaceProps surfaceProps(this->surfaceProps().flags(), cinfo.fPixelGeometry);
306 return SkBitmapDevice::Create(cinfo.fInfo, surfaceProps, cinfo.fTrackCoverage,
307 cinfo.fAllocator);
308 }
309
onAccessPixels(SkPixmap * pmap)310 bool SkBitmapDevice::onAccessPixels(SkPixmap* pmap) {
311 if (this->onPeekPixels(pmap)) {
312 fBitmap.notifyPixelsChanged();
313 return true;
314 }
315 return false;
316 }
317
onPeekPixels(SkPixmap * pmap)318 bool SkBitmapDevice::onPeekPixels(SkPixmap* pmap) {
319 const SkImageInfo info = fBitmap.info();
320 if (fBitmap.getPixels() && (kUnknown_SkColorType != info.colorType())) {
321 pmap->reset(fBitmap.info(), fBitmap.getPixels(), fBitmap.rowBytes());
322 return true;
323 }
324 return false;
325 }
326
onWritePixels(const SkPixmap & pm,int x,int y)327 bool SkBitmapDevice::onWritePixels(const SkPixmap& pm, int x, int y) {
328 // since we don't stop creating un-pixeled devices yet, check for no pixels here
329 if (nullptr == fBitmap.getPixels()) {
330 return false;
331 }
332
333 if (fBitmap.writePixels(pm, x, y)) {
334 fBitmap.notifyPixelsChanged();
335 return true;
336 }
337 return false;
338 }
339
onReadPixels(const SkPixmap & pm,int x,int y)340 bool SkBitmapDevice::onReadPixels(const SkPixmap& pm, int x, int y) {
341 return fBitmap.readPixels(pm, x, y);
342 }
343
344 ///////////////////////////////////////////////////////////////////////////////
345
drawPaint(const SkPaint & paint)346 void SkBitmapDevice::drawPaint(const SkPaint& paint) {
347 BDDraw(this).drawPaint(paint);
348 }
349
drawPoints(SkCanvas::PointMode mode,size_t count,const SkPoint pts[],const SkPaint & paint)350 void SkBitmapDevice::drawPoints(SkCanvas::PointMode mode, size_t count,
351 const SkPoint pts[], const SkPaint& paint) {
352 LOOP_TILER( drawPoints(mode, count, pts, paint, nullptr), nullptr)
353 }
354
drawRect(const SkRect & r,const SkPaint & paint)355 void SkBitmapDevice::drawRect(const SkRect& r, const SkPaint& paint) {
356 LOOP_TILER( drawRect(r, paint), Bounder(r, paint))
357 }
358
drawOval(const SkRect & oval,const SkPaint & paint)359 void SkBitmapDevice::drawOval(const SkRect& oval, const SkPaint& paint) {
360 SkPath path;
361 path.addOval(oval);
362 // call the VIRTUAL version, so any subclasses who do handle drawPath aren't
363 // required to override drawOval.
364 this->drawPath(path, paint, true);
365 }
366
drawRRect(const SkRRect & rrect,const SkPaint & paint)367 void SkBitmapDevice::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
368 #ifdef SK_IGNORE_BLURRED_RRECT_OPT
369 SkPath path;
370
371 path.addRRect(rrect);
372 // call the VIRTUAL version, so any subclasses who do handle drawPath aren't
373 // required to override drawRRect.
374 this->drawPath(path, paint, true);
375 #else
376 LOOP_TILER( drawRRect(rrect, paint), Bounder(rrect.getBounds(), paint))
377 #endif
378 }
379
drawPath(const SkPath & path,const SkPaint & paint,bool pathIsMutable)380 void SkBitmapDevice::drawPath(const SkPath& path,
381 const SkPaint& paint,
382 bool pathIsMutable) {
383 const SkRect* bounds = nullptr;
384 if (SkDrawTiler::NeedsTiling(this) && !path.isInverseFillType()) {
385 bounds = &path.getBounds();
386 }
387 SkDrawTiler tiler(this, bounds ? Bounder(*bounds, paint).bounds() : nullptr);
388 if (tiler.needsTiling()) {
389 pathIsMutable = false;
390 }
391 while (const SkDraw* draw = tiler.next()) {
392 draw->drawPath(path, paint, nullptr, pathIsMutable);
393 }
394 }
395
drawBitmap(const SkBitmap & bitmap,const SkMatrix & matrix,const SkRect * dstOrNull,const SkPaint & paint)396 void SkBitmapDevice::drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix,
397 const SkRect* dstOrNull, const SkPaint& paint) {
398 const SkRect* bounds = dstOrNull;
399 SkRect storage;
400 if (!bounds && SkDrawTiler::NeedsTiling(this)) {
401 matrix.mapRect(&storage, SkRect::MakeIWH(bitmap.width(), bitmap.height()));
402 Bounder b(storage, paint);
403 if (b.hasBounds()) {
404 storage = *b.bounds();
405 bounds = &storage;
406 }
407 }
408 LOOP_TILER(drawBitmap(bitmap, matrix, dstOrNull, paint), bounds)
409 }
410
CanApplyDstMatrixAsCTM(const SkMatrix & m,const SkPaint & paint)411 static inline bool CanApplyDstMatrixAsCTM(const SkMatrix& m, const SkPaint& paint) {
412 if (!paint.getMaskFilter()) {
413 return true;
414 }
415
416 // Some mask filters parameters (sigma) depend on the CTM/scale.
417 return m.getType() <= SkMatrix::kTranslate_Mask;
418 }
419
drawBitmapRect(const SkBitmap & bitmap,const SkRect * src,const SkRect & dst,const SkPaint & paint,SkCanvas::SrcRectConstraint constraint)420 void SkBitmapDevice::drawBitmapRect(const SkBitmap& bitmap,
421 const SkRect* src, const SkRect& dst,
422 const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) {
423 SkASSERT(dst.isFinite());
424 SkASSERT(dst.isSorted());
425
426 SkMatrix matrix;
427 SkRect bitmapBounds, tmpSrc, tmpDst;
428 SkBitmap tmpBitmap;
429
430 bitmapBounds.isetWH(bitmap.width(), bitmap.height());
431
432 // Compute matrix from the two rectangles
433 if (src) {
434 tmpSrc = *src;
435 } else {
436 tmpSrc = bitmapBounds;
437 }
438 matrix.setRectToRect(tmpSrc, dst, SkMatrix::kFill_ScaleToFit);
439
440 LogDrawScaleFactor(this->ctm(), matrix, paint.getFilterQuality());
441
442 const SkRect* dstPtr = &dst;
443 const SkBitmap* bitmapPtr = &bitmap;
444
445 // clip the tmpSrc to the bounds of the bitmap, and recompute dstRect if
446 // needed (if the src was clipped). No check needed if src==null.
447 if (src) {
448 if (!bitmapBounds.contains(*src)) {
449 if (!tmpSrc.intersect(bitmapBounds)) {
450 return; // nothing to draw
451 }
452 // recompute dst, based on the smaller tmpSrc
453 matrix.mapRect(&tmpDst, tmpSrc);
454 if (!tmpDst.isFinite()) {
455 return;
456 }
457 dstPtr = &tmpDst;
458 }
459 }
460
461 if (src && !src->contains(bitmapBounds) &&
462 SkCanvas::kFast_SrcRectConstraint == constraint &&
463 paint.getFilterQuality() != kNone_SkFilterQuality) {
464 // src is smaller than the bounds of the bitmap, and we are filtering, so we don't know
465 // how much more of the bitmap we need, so we can't use extractSubset or drawBitmap,
466 // but we must use a shader w/ dst bounds (which can access all of the bitmap needed).
467 goto USE_SHADER;
468 }
469
470 if (src) {
471 // since we may need to clamp to the borders of the src rect within
472 // the bitmap, we extract a subset.
473 const SkIRect srcIR = tmpSrc.roundOut();
474 if (!bitmap.extractSubset(&tmpBitmap, srcIR)) {
475 return;
476 }
477 bitmapPtr = &tmpBitmap;
478
479 // Since we did an extract, we need to adjust the matrix accordingly
480 SkScalar dx = 0, dy = 0;
481 if (srcIR.fLeft > 0) {
482 dx = SkIntToScalar(srcIR.fLeft);
483 }
484 if (srcIR.fTop > 0) {
485 dy = SkIntToScalar(srcIR.fTop);
486 }
487 if (dx || dy) {
488 matrix.preTranslate(dx, dy);
489 }
490
491 #ifdef SK_DRAWBITMAPRECT_FAST_OFFSET
492 SkRect extractedBitmapBounds = SkRect::MakeXYWH(dx, dy,
493 SkIntToScalar(bitmapPtr->width()),
494 SkIntToScalar(bitmapPtr->height()));
495 #else
496 SkRect extractedBitmapBounds;
497 extractedBitmapBounds.isetWH(bitmapPtr->width(), bitmapPtr->height());
498 #endif
499 if (extractedBitmapBounds == tmpSrc) {
500 // no fractional part in src, we can just call drawBitmap
501 goto USE_DRAWBITMAP;
502 }
503 } else {
504 USE_DRAWBITMAP:
505 // We can go faster by just calling drawBitmap, which will concat the
506 // matrix with the CTM, and try to call drawSprite if it can. If not,
507 // it will make a shader and call drawRect, as we do below.
508 if (CanApplyDstMatrixAsCTM(matrix, paint)) {
509 this->drawBitmap(*bitmapPtr, matrix, dstPtr, paint);
510 return;
511 }
512 }
513
514 USE_SHADER:
515
516 // TODO(herb): Move this over to SkArenaAlloc when arena alloc has a facility to return sk_sps.
517 // Since the shader need only live for our stack-frame, pass in a custom allocator. This
518 // can save malloc calls, and signals to SkMakeBitmapShader to not try to copy the bitmap
519 // if its mutable, since that precaution is not needed (give the short lifetime of the shader).
520
521 // construct a shader, so we can call drawRect with the dst
522 auto s = SkMakeBitmapShaderForPaint(paint, *bitmapPtr, SkTileMode::kClamp,
523 SkTileMode::kClamp, &matrix, kNever_SkCopyPixelsMode);
524 if (!s) {
525 return;
526 }
527
528 SkPaint paintWithShader(paint);
529 paintWithShader.setStyle(SkPaint::kFill_Style);
530 paintWithShader.setShader(std::move(s));
531
532 // Call ourself, in case the subclass wanted to share this setup code
533 // but handle the drawRect code themselves.
534 this->drawRect(*dstPtr, paintWithShader);
535 }
536
drawSprite(const SkBitmap & bitmap,int x,int y,const SkPaint & paint)537 void SkBitmapDevice::drawSprite(const SkBitmap& bitmap, int x, int y, const SkPaint& paint) {
538 BDDraw(this).drawSprite(bitmap, x, y, paint);
539 }
540
drawGlyphRunList(const SkGlyphRunList & glyphRunList)541 void SkBitmapDevice::drawGlyphRunList(const SkGlyphRunList& glyphRunList) {
542 LOOP_TILER( drawGlyphRunList(glyphRunList, &fGlyphPainter), nullptr )
543 }
544
drawVertices(const SkVertices * vertices,const SkVertices::Bone bones[],int boneCount,SkBlendMode bmode,const SkPaint & paint)545 void SkBitmapDevice::drawVertices(const SkVertices* vertices, const SkVertices::Bone bones[],
546 int boneCount, SkBlendMode bmode, const SkPaint& paint) {
547 BDDraw(this).drawVertices(vertices->mode(), vertices->vertexCount(), vertices->positions(),
548 vertices->texCoords(), vertices->colors(), vertices->boneIndices(),
549 vertices->boneWeights(), bmode, vertices->indices(),
550 vertices->indexCount(), paint, bones, boneCount);
551 }
552
drawDevice(SkBaseDevice * device,int x,int y,const SkPaint & origPaint)553 void SkBitmapDevice::drawDevice(SkBaseDevice* device, int x, int y, const SkPaint& origPaint) {
554 SkASSERT(!origPaint.getImageFilter());
555
556 // todo: can we unify with similar adjustment in SkGpuDevice?
557 SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
558 if (paint->getMaskFilter()) {
559 paint.writable()->setMaskFilter(paint->getMaskFilter()->makeWithMatrix(this->ctm()));
560 }
561
562 // hack to test coverage
563 SkBitmapDevice* src = static_cast<SkBitmapDevice*>(device);
564 if (src->fCoverage) {
565 SkDraw draw;
566 draw.fDst = fBitmap.pixmap();
567 draw.fMatrix = &SkMatrix::I();
568 draw.fRC = &fRCStack.rc();
569 SkPaint paint(origPaint);
570 paint.setShader(src->fBitmap.makeShader());
571 draw.drawBitmap(*src->fCoverage.get(),
572 SkMatrix::MakeTrans(SkIntToScalar(x),SkIntToScalar(y)), nullptr, paint);
573 } else {
574 this->drawSprite(src->fBitmap, x, y, *paint);
575 }
576 }
577
drawAtlas(const SkImage * atlas,const SkRSXform xform[],const SkRect tex[],const SkColor colors[],int count,SkBlendMode mode,const SkPaint & paint)578 void SkBitmapDevice::drawAtlas(const SkImage* atlas, const SkRSXform xform[],
579 const SkRect tex[], const SkColor colors[], int count,
580 SkBlendMode mode, const SkPaint& paint) {
581 // set this to true for performance comparisons with the old drawVertices way
582 if (false) {
583 this->INHERITED::drawAtlas(atlas, xform, tex, colors, count, mode, paint);
584 return;
585 }
586 BDDraw(this).drawAtlas(atlas, xform, tex, colors, count, mode, paint);
587 }
588
589 ///////////////////////////////////////////////////////////////////////////////
590
591 namespace {
592
593 class SkAutoDeviceClipRestore {
594 public:
SkAutoDeviceClipRestore(SkBaseDevice * device,const SkIRect & clip)595 SkAutoDeviceClipRestore(SkBaseDevice* device, const SkIRect& clip)
596 : fDevice(device)
597 , fPrevCTM(device->ctm()) {
598 fDevice->save();
599 fDevice->setCTM(SkMatrix::I());
600 fDevice->clipRect(SkRect::Make(clip), SkClipOp::kIntersect, false);
601 fDevice->setCTM(fPrevCTM);
602 }
603
~SkAutoDeviceClipRestore()604 ~SkAutoDeviceClipRestore() {
605 fDevice->restore(fPrevCTM);
606 }
607
608 private:
609 SkBaseDevice* fDevice;
610 const SkMatrix fPrevCTM;
611 };
612
613 } // anonymous ns
614
drawSpecial(SkSpecialImage * src,int x,int y,const SkPaint & origPaint,SkImage * clipImage,const SkMatrix & clipMatrix)615 void SkBitmapDevice::drawSpecial(SkSpecialImage* src, int x, int y, const SkPaint& origPaint,
616 SkImage* clipImage, const SkMatrix& clipMatrix) {
617 SkASSERT(!src->isTextureBacked());
618
619 sk_sp<SkSpecialImage> filteredImage;
620 SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
621
622 if (SkImageFilter* filter = paint->getImageFilter()) {
623 SkIPoint offset = SkIPoint::Make(0, 0);
624 const SkMatrix matrix = SkMatrix::Concat(
625 SkMatrix::MakeTrans(SkIntToScalar(-x), SkIntToScalar(-y)), this->ctm());
626 const SkIRect clipBounds = fRCStack.rc().getBounds().makeOffset(-x, -y);
627 sk_sp<SkImageFilterCache> cache(this->getImageFilterCache());
628 SkImageFilter_Base::Context ctx(matrix, clipBounds, cache.get(), fBitmap.colorType(),
629 fBitmap.colorSpace(), src);
630
631 filteredImage = as_IFB(filter)->filterImage(ctx, &offset);
632 if (!filteredImage) {
633 return;
634 }
635
636 src = filteredImage.get();
637 paint.writable()->setImageFilter(nullptr);
638 x += offset.x();
639 y += offset.y();
640 }
641
642 if (paint->getMaskFilter()) {
643 paint.writable()->setMaskFilter(paint->getMaskFilter()->makeWithMatrix(this->ctm()));
644 }
645
646 if (!clipImage) {
647 SkBitmap resultBM;
648 if (src->getROPixels(&resultBM)) {
649 this->drawSprite(resultBM, x, y, *paint);
650 }
651 return;
652 }
653
654 // Clip image case.
655 sk_sp<SkImage> srcImage(src->asImage());
656 if (!srcImage) {
657 return;
658 }
659
660 const SkMatrix totalMatrix = SkMatrix::Concat(this->ctm(), clipMatrix);
661 SkRect clipBounds;
662 totalMatrix.mapRect(&clipBounds, SkRect::Make(clipImage->bounds()));
663 const SkIRect srcBounds = srcImage->bounds().makeOffset(x, y);
664
665 SkIRect maskBounds = fRCStack.rc().getBounds();
666 if (!maskBounds.intersect(clipBounds.roundOut()) || !maskBounds.intersect(srcBounds)) {
667 return;
668 }
669
670 sk_sp<SkImage> mask;
671 SkMatrix maskMatrix, shaderMatrix;
672 SkTLazy<SkAutoDeviceClipRestore> autoClipRestore;
673
674 SkMatrix totalInverse;
675 if (clipImage->isAlphaOnly() && totalMatrix.invert(&totalInverse)) {
676 // If the mask is already in A8 format, we can draw it directly
677 // (while compensating in the shader matrix).
678 mask = sk_ref_sp(clipImage);
679 maskMatrix = totalMatrix;
680 shaderMatrix = SkMatrix::Concat(totalInverse, SkMatrix::MakeTrans(x, y));
681
682 // If the mask is not fully contained within the src layer, we must clip.
683 if (!srcBounds.contains(clipBounds)) {
684 autoClipRestore.init(this, srcBounds);
685 }
686
687 maskBounds.offsetTo(0, 0);
688 } else {
689 // Otherwise, we convert the mask to A8 explicitly.
690 sk_sp<SkSurface> surf = SkSurface::MakeRaster(SkImageInfo::MakeA8(maskBounds.width(),
691 maskBounds.height()));
692 SkCanvas* canvas = surf->getCanvas();
693 canvas->translate(-maskBounds.x(), -maskBounds.y());
694 canvas->concat(totalMatrix);
695 canvas->drawImage(clipImage, 0, 0);
696
697 mask = surf->makeImageSnapshot();
698 maskMatrix = SkMatrix::I();
699 shaderMatrix = SkMatrix::MakeTrans(x - maskBounds.x(), y - maskBounds.y());
700 }
701
702 SkAutoDeviceCTMRestore adctmr(this, maskMatrix);
703 paint.writable()->setShader(srcImage->makeShader(&shaderMatrix));
704 this->drawImageRect(mask.get(), nullptr,
705 SkRect::MakeXYWH(maskBounds.x(), maskBounds.y(),
706 mask->width(), mask->height()),
707 *paint, SkCanvas::kFast_SrcRectConstraint);
708 }
709
makeSpecial(const SkBitmap & bitmap)710 sk_sp<SkSpecialImage> SkBitmapDevice::makeSpecial(const SkBitmap& bitmap) {
711 return SkSpecialImage::MakeFromRaster(bitmap.bounds(), bitmap);
712 }
713
makeSpecial(const SkImage * image)714 sk_sp<SkSpecialImage> SkBitmapDevice::makeSpecial(const SkImage* image) {
715 return SkSpecialImage::MakeFromImage(nullptr, SkIRect::MakeWH(image->width(), image->height()),
716 image->makeNonTextureImage());
717 }
718
snapSpecial()719 sk_sp<SkSpecialImage> SkBitmapDevice::snapSpecial() {
720 return this->makeSpecial(fBitmap);
721 }
722
snapBackImage(const SkIRect & bounds)723 sk_sp<SkSpecialImage> SkBitmapDevice::snapBackImage(const SkIRect& bounds) {
724 return SkSpecialImage::CopyFromRaster(bounds, fBitmap, &this->surfaceProps());
725 }
726
727 ///////////////////////////////////////////////////////////////////////////////
728
makeSurface(const SkImageInfo & info,const SkSurfaceProps & props)729 sk_sp<SkSurface> SkBitmapDevice::makeSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
730 return SkSurface::MakeRaster(info, &props);
731 }
732
getImageFilterCache()733 SkImageFilterCache* SkBitmapDevice::getImageFilterCache() {
734 SkImageFilterCache* cache = SkImageFilterCache::Get();
735 cache->ref();
736 return cache;
737 }
738
739 ///////////////////////////////////////////////////////////////////////////////////////////////////
740
onSave()741 void SkBitmapDevice::onSave() {
742 fRCStack.save();
743 }
744
onRestore()745 void SkBitmapDevice::onRestore() {
746 fRCStack.restore();
747 }
748
onClipRect(const SkRect & rect,SkClipOp op,bool aa)749 void SkBitmapDevice::onClipRect(const SkRect& rect, SkClipOp op, bool aa) {
750 fRCStack.clipRect(this->ctm(), rect, op, aa);
751 }
752
onClipRRect(const SkRRect & rrect,SkClipOp op,bool aa)753 void SkBitmapDevice::onClipRRect(const SkRRect& rrect, SkClipOp op, bool aa) {
754 fRCStack.clipRRect(this->ctm(), rrect, op, aa);
755 }
756
onClipPath(const SkPath & path,SkClipOp op,bool aa)757 void SkBitmapDevice::onClipPath(const SkPath& path, SkClipOp op, bool aa) {
758 fRCStack.clipPath(this->ctm(), path, op, aa);
759 }
760
onClipRegion(const SkRegion & rgn,SkClipOp op)761 void SkBitmapDevice::onClipRegion(const SkRegion& rgn, SkClipOp op) {
762 SkIPoint origin = this->getOrigin();
763 SkRegion tmp;
764 const SkRegion* ptr = &rgn;
765 if (origin.fX | origin.fY) {
766 // translate from "global/canvas" coordinates to relative to this device
767 rgn.translate(-origin.fX, -origin.fY, &tmp);
768 ptr = &tmp;
769 }
770 fRCStack.clipRegion(*ptr, op);
771 }
772
onSetDeviceClipRestriction(SkIRect * mutableClipRestriction)773 void SkBitmapDevice::onSetDeviceClipRestriction(SkIRect* mutableClipRestriction) {
774 fRCStack.setDeviceClipRestriction(mutableClipRestriction);
775 if (!mutableClipRestriction->isEmpty()) {
776 SkRegion rgn(*mutableClipRestriction);
777 fRCStack.clipRegion(rgn, SkClipOp::kIntersect);
778 }
779 }
780
onClipIsAA() const781 bool SkBitmapDevice::onClipIsAA() const {
782 const SkRasterClip& rc = fRCStack.rc();
783 return !rc.isEmpty() && rc.isAA();
784 }
785
onAsRgnClip(SkRegion * rgn) const786 void SkBitmapDevice::onAsRgnClip(SkRegion* rgn) const {
787 const SkRasterClip& rc = fRCStack.rc();
788 if (rc.isAA()) {
789 rgn->setRect(rc.getBounds());
790 } else {
791 *rgn = rc.bwRgn();
792 }
793 }
794
validateDevBounds(const SkIRect & drawClipBounds)795 void SkBitmapDevice::validateDevBounds(const SkIRect& drawClipBounds) {
796 #ifdef SK_DEBUG
797 const SkIRect& stackBounds = fRCStack.rc().getBounds();
798 SkASSERT(drawClipBounds == stackBounds);
799 #endif
800 }
801
onGetClipType() const802 SkBaseDevice::ClipType SkBitmapDevice::onGetClipType() const {
803 const SkRasterClip& rc = fRCStack.rc();
804 if (rc.isEmpty()) {
805 return ClipType::kEmpty;
806 } else if (rc.isRect()) {
807 return ClipType::kRect;
808 } else {
809 return ClipType::kComplex;
810 }
811 }
812