• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "SkiaCanvas.h"
18 
19 #include "CanvasProperty.h"
20 #include "NinePatchUtils.h"
21 #include "VectorDrawable.h"
22 #include "hwui/Bitmap.h"
23 #include "hwui/MinikinUtils.h"
24 #include "hwui/PaintFilter.h"
25 #include "pipeline/skia/AnimatedDrawables.h"
26 #include "pipeline/skia/HolePunch.h"
27 
28 #include <SkAndroidFrameworkUtils.h>
29 #include <SkAnimatedImage.h>
30 #include <SkCanvasPriv.h>
31 #include <SkCanvasStateUtils.h>
32 #include <SkColorFilter.h>
33 #include <SkDeque.h>
34 #include <SkDrawable.h>
35 #include <SkFont.h>
36 #include <SkGraphics.h>
37 #include <SkImage.h>
38 #include <SkImagePriv.h>
39 #include <SkPicture.h>
40 #include <SkRSXform.h>
41 #include <SkShader.h>
42 #include <SkTemplates.h>
43 #include <SkTextBlob.h>
44 #include <SkVertices.h>
45 
46 #include <memory>
47 #include <optional>
48 #include <utility>
49 
50 namespace android {
51 
52 using uirenderer::PaintUtils;
53 
create_canvas(const SkBitmap & bitmap)54 Canvas* Canvas::create_canvas(const SkBitmap& bitmap) {
55     return new SkiaCanvas(bitmap);
56 }
57 
create_canvas(SkCanvas * skiaCanvas)58 Canvas* Canvas::create_canvas(SkCanvas* skiaCanvas) {
59     return new SkiaCanvas(skiaCanvas);
60 }
61 
SkiaCanvas()62 SkiaCanvas::SkiaCanvas() {}
63 
SkiaCanvas(SkCanvas * canvas)64 SkiaCanvas::SkiaCanvas(SkCanvas* canvas) : mCanvas(canvas) {}
65 
SkiaCanvas(const SkBitmap & bitmap)66 SkiaCanvas::SkiaCanvas(const SkBitmap& bitmap) {
67     mCanvasOwned = std::unique_ptr<SkCanvas>(new SkCanvas(bitmap));
68     mCanvas = mCanvasOwned.get();
69 }
70 
~SkiaCanvas()71 SkiaCanvas::~SkiaCanvas() {}
72 
reset(SkCanvas * skiaCanvas)73 void SkiaCanvas::reset(SkCanvas* skiaCanvas) {
74     if (mCanvas != skiaCanvas) {
75         mCanvas = skiaCanvas;
76         mCanvasOwned.reset();
77     }
78     mSaveStack.reset(nullptr);
79 }
80 
81 // ----------------------------------------------------------------------------
82 // Canvas state operations: Replace Bitmap
83 // ----------------------------------------------------------------------------
84 
setBitmap(const SkBitmap & bitmap)85 void SkiaCanvas::setBitmap(const SkBitmap& bitmap) {
86     // deletes the previously owned canvas (if any)
87     mCanvasOwned.reset(new SkCanvas(bitmap));
88     mCanvas = mCanvasOwned.get();
89 
90     // clean up the old save stack
91     mSaveStack.reset(nullptr);
92 }
93 
94 // ----------------------------------------------------------------------------
95 // Canvas state operations
96 // ----------------------------------------------------------------------------
97 
isOpaque()98 bool SkiaCanvas::isOpaque() {
99     return mCanvas->imageInfo().isOpaque();
100 }
101 
width()102 int SkiaCanvas::width() {
103     return mCanvas->imageInfo().width();
104 }
105 
height()106 int SkiaCanvas::height() {
107     return mCanvas->imageInfo().height();
108 }
109 
110 // ----------------------------------------------------------------------------
111 // Canvas state operations: Save (layer)
112 // ----------------------------------------------------------------------------
113 
getSaveCount() const114 int SkiaCanvas::getSaveCount() const {
115     return mCanvas->getSaveCount();
116 }
117 
save(SaveFlags::Flags flags)118 int SkiaCanvas::save(SaveFlags::Flags flags) {
119     int count = mCanvas->save();
120     recordPartialSave(flags);
121     return count;
122 }
123 
124 // The SkiaCanvas::restore operation layers on the capability to preserve
125 // either (or both) the matrix and/or clip state after a SkCanvas::restore
126 // operation. It does this by explicitly saving off the clip & matrix state
127 // when requested and playing it back after the SkCanvas::restore.
restore()128 void SkiaCanvas::restore() {
129     const auto* rec = this->currentSaveRec();
130     if (!rec) {
131         // Fast path - no record for this frame.
132         mCanvas->restore();
133         return;
134     }
135 
136     bool preserveMatrix = !(rec->saveFlags & SaveFlags::Matrix);
137     bool preserveClip = !(rec->saveFlags & SaveFlags::Clip);
138 
139     SkMatrix savedMatrix;
140     if (preserveMatrix) {
141         savedMatrix = mCanvas->getTotalMatrix();
142     }
143 
144     const size_t clipIndex = rec->clipIndex;
145 
146     mCanvas->restore();
147     mSaveStack->pop_back();
148 
149     if (preserveMatrix) {
150         mCanvas->setMatrix(savedMatrix);
151     }
152 
153     if (preserveClip) {
154         this->applyPersistentClips(clipIndex);
155     }
156 }
157 
restoreToCount(int restoreCount)158 void SkiaCanvas::restoreToCount(int restoreCount) {
159     while (mCanvas->getSaveCount() > restoreCount) {
160         this->restore();
161     }
162 }
163 
saveLayer(float left,float top,float right,float bottom,const SkPaint * paint)164 int SkiaCanvas::saveLayer(float left, float top, float right, float bottom, const SkPaint* paint) {
165     const SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
166     const SkCanvas::SaveLayerRec rec(&bounds, paint);
167 
168     return mCanvas->saveLayer(rec);
169 }
170 
saveLayerAlpha(float left,float top,float right,float bottom,int alpha)171 int SkiaCanvas::saveLayerAlpha(float left, float top, float right, float bottom, int alpha) {
172     if (static_cast<unsigned>(alpha) < 0xFF) {
173         SkPaint alphaPaint;
174         alphaPaint.setAlpha(alpha);
175         return this->saveLayer(left, top, right, bottom, &alphaPaint);
176     }
177     return this->saveLayer(left, top, right, bottom, nullptr);
178 }
179 
saveUnclippedLayer(int left,int top,int right,int bottom)180 int SkiaCanvas::saveUnclippedLayer(int left, int top, int right, int bottom) {
181     SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
182     return SkAndroidFrameworkUtils::SaveBehind(mCanvas, &bounds);
183 }
184 
restoreUnclippedLayer(int restoreCount,const SkPaint & paint)185 void SkiaCanvas::restoreUnclippedLayer(int restoreCount, const SkPaint& paint) {
186 
187     while (mCanvas->getSaveCount() > restoreCount + 1) {
188         this->restore();
189     }
190 
191     if (mCanvas->getSaveCount() == restoreCount + 1) {
192         SkCanvasPriv::DrawBehind(mCanvas, filterPaint(paint));
193         this->restore();
194     }
195 }
196 
197 class SkiaCanvas::Clip {
198 public:
Clip(const SkRect & rect,SkClipOp op,const SkMatrix & m)199     Clip(const SkRect& rect, SkClipOp op, const SkMatrix& m)
200             : mType(Type::Rect), mOp(op), mMatrix(m), mRRect(SkRRect::MakeRect(rect)) {}
Clip(const SkRRect & rrect,SkClipOp op,const SkMatrix & m)201     Clip(const SkRRect& rrect, SkClipOp op, const SkMatrix& m)
202             : mType(Type::RRect), mOp(op), mMatrix(m), mRRect(rrect) {}
Clip(const SkPath & path,SkClipOp op,const SkMatrix & m)203     Clip(const SkPath& path, SkClipOp op, const SkMatrix& m)
204             : mType(Type::Path), mOp(op), mMatrix(m), mPath(std::in_place, path) {}
205 
apply(SkCanvas * canvas) const206     void apply(SkCanvas* canvas) const {
207         canvas->setMatrix(mMatrix);
208         switch (mType) {
209             case Type::Rect:
210                 // Don't anti-alias rectangular clips
211                 canvas->clipRect(mRRect.rect(), mOp, false);
212                 break;
213             case Type::RRect:
214                 // Ensure rounded rectangular clips are anti-aliased
215                 canvas->clipRRect(mRRect, mOp, true);
216                 break;
217             case Type::Path:
218                 // Ensure path clips are anti-aliased
219                 canvas->clipPath(mPath.value(), mOp, true);
220                 break;
221         }
222     }
223 
224 private:
225     enum class Type {
226         Rect,
227         RRect,
228         Path,
229     };
230 
231     Type mType;
232     SkClipOp mOp;
233     SkMatrix mMatrix;
234 
235     // These are logically a union (tracked separately due to non-POD path).
236     std::optional<SkPath> mPath;
237     SkRRect mRRect;
238 };
239 
currentSaveRec() const240 const SkiaCanvas::SaveRec* SkiaCanvas::currentSaveRec() const {
241     const SaveRec* rec = mSaveStack ? static_cast<const SaveRec*>(mSaveStack->back()) : nullptr;
242     int currentSaveCount = mCanvas->getSaveCount();
243     SkASSERT(!rec || currentSaveCount >= rec->saveCount);
244 
245     return (rec && rec->saveCount == currentSaveCount) ? rec : nullptr;
246 }
247 
punchHole(const SkRRect & rect)248 void SkiaCanvas::punchHole(const SkRRect& rect) {
249     SkPaint paint = SkPaint();
250     paint.setColor(0);
251     paint.setBlendMode(SkBlendMode::kClear);
252     mCanvas->drawRRect(rect, paint);
253 }
254 
255 // ----------------------------------------------------------------------------
256 // functions to emulate legacy SaveFlags (i.e. independent matrix/clip flags)
257 // ----------------------------------------------------------------------------
258 
recordPartialSave(SaveFlags::Flags flags)259 void SkiaCanvas::recordPartialSave(SaveFlags::Flags flags) {
260     // A partial save is a save operation which doesn't capture the full canvas state.
261     // (either SaveFlags::Matrix or SaveFlags::Clip is missing).
262 
263     // Mask-out non canvas state bits.
264     flags &= SaveFlags::MatrixClip;
265 
266     if (flags == SaveFlags::MatrixClip) {
267         // not a partial save.
268         return;
269     }
270 
271     if (!mSaveStack) {
272         mSaveStack.reset(new SkDeque(sizeof(struct SaveRec), 8));
273     }
274 
275     SaveRec* rec = static_cast<SaveRec*>(mSaveStack->push_back());
276     rec->saveCount = mCanvas->getSaveCount();
277     rec->saveFlags = flags;
278     rec->clipIndex = mClipStack.size();
279 }
280 
281 template <typename T>
recordClip(const T & clip,SkClipOp op)282 void SkiaCanvas::recordClip(const T& clip, SkClipOp op) {
283     // Only need tracking when in a partial save frame which
284     // doesn't restore the clip.
285     const SaveRec* rec = this->currentSaveRec();
286     if (rec && !(rec->saveFlags & SaveFlags::Clip)) {
287         mClipStack.emplace_back(clip, op, mCanvas->getTotalMatrix());
288     }
289 }
290 
291 // Applies and optionally removes all clips >= index.
applyPersistentClips(size_t clipStartIndex)292 void SkiaCanvas::applyPersistentClips(size_t clipStartIndex) {
293     SkASSERT(clipStartIndex <= mClipStack.size());
294     const auto begin = mClipStack.cbegin() + clipStartIndex;
295     const auto end = mClipStack.cend();
296 
297     // Clip application mutates the CTM.
298     const SkMatrix saveMatrix = mCanvas->getTotalMatrix();
299 
300     for (auto clip = begin; clip != end; ++clip) {
301         clip->apply(mCanvas);
302     }
303 
304     mCanvas->setMatrix(saveMatrix);
305 
306     // If the current/post-restore save rec is also persisting clips, we
307     // leave them on the stack to be reapplied part of the next restore().
308     // Otherwise we're done and just pop them.
309     const auto* rec = this->currentSaveRec();
310     if (!rec || (rec->saveFlags & SaveFlags::Clip)) {
311         mClipStack.erase(begin, end);
312     }
313 }
314 
315 // ----------------------------------------------------------------------------
316 // Canvas state operations: Matrix
317 // ----------------------------------------------------------------------------
318 
getMatrix(SkMatrix * outMatrix) const319 void SkiaCanvas::getMatrix(SkMatrix* outMatrix) const {
320     *outMatrix = mCanvas->getTotalMatrix();
321 }
322 
setMatrix(const SkMatrix & matrix)323 void SkiaCanvas::setMatrix(const SkMatrix& matrix) {
324     mCanvas->setMatrix(matrix);
325 }
326 
concat(const SkMatrix & matrix)327 void SkiaCanvas::concat(const SkMatrix& matrix) {
328     mCanvas->concat(matrix);
329 }
330 
rotate(float degrees)331 void SkiaCanvas::rotate(float degrees) {
332     mCanvas->rotate(degrees);
333 }
334 
scale(float sx,float sy)335 void SkiaCanvas::scale(float sx, float sy) {
336     mCanvas->scale(sx, sy);
337 }
338 
skew(float sx,float sy)339 void SkiaCanvas::skew(float sx, float sy) {
340     mCanvas->skew(sx, sy);
341 }
342 
translate(float dx,float dy)343 void SkiaCanvas::translate(float dx, float dy) {
344     mCanvas->translate(dx, dy);
345 }
346 
347 // ----------------------------------------------------------------------------
348 // Canvas state operations: Clips
349 // ----------------------------------------------------------------------------
350 
351 // This function is a mirror of SkCanvas::getClipBounds except that it does
352 // not outset the edge of the clip to account for anti-aliasing. There is
353 // a skia bug to investigate pushing this logic into back into skia.
354 // (see https://code.google.com/p/skia/issues/detail?id=1303)
getClipBounds(SkRect * outRect) const355 bool SkiaCanvas::getClipBounds(SkRect* outRect) const {
356     SkIRect ibounds;
357     if (!mCanvas->getDeviceClipBounds(&ibounds)) {
358         return false;
359     }
360 
361     SkMatrix inverse;
362     // if we can't invert the CTM, we can't return local clip bounds
363     if (!mCanvas->getTotalMatrix().invert(&inverse)) {
364         if (outRect) {
365             outRect->setEmpty();
366         }
367         return false;
368     }
369 
370     if (NULL != outRect) {
371         SkRect r = SkRect::Make(ibounds);
372         inverse.mapRect(outRect, r);
373     }
374     return true;
375 }
376 
quickRejectRect(float left,float top,float right,float bottom) const377 bool SkiaCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
378     SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
379     return mCanvas->quickReject(bounds);
380 }
381 
quickRejectPath(const SkPath & path) const382 bool SkiaCanvas::quickRejectPath(const SkPath& path) const {
383     return mCanvas->quickReject(path);
384 }
385 
clipRect(float left,float top,float right,float bottom,SkClipOp op)386 bool SkiaCanvas::clipRect(float left, float top, float right, float bottom, SkClipOp op) {
387     SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
388     this->recordClip(rect, op);
389     mCanvas->clipRect(rect, op);
390     return !mCanvas->isClipEmpty();
391 }
392 
clipPath(const SkPath * path,SkClipOp op)393 bool SkiaCanvas::clipPath(const SkPath* path, SkClipOp op) {
394     this->recordClip(*path, op);
395     mCanvas->clipPath(*path, op, true);
396     return !mCanvas->isClipEmpty();
397 }
398 
399 // ----------------------------------------------------------------------------
400 // Canvas state operations: Filters
401 // ----------------------------------------------------------------------------
402 
getPaintFilter()403 PaintFilter* SkiaCanvas::getPaintFilter() {
404     return mPaintFilter.get();
405 }
406 
setPaintFilter(sk_sp<PaintFilter> paintFilter)407 void SkiaCanvas::setPaintFilter(sk_sp<PaintFilter> paintFilter) {
408     mPaintFilter = std::move(paintFilter);
409 }
410 
411 // ----------------------------------------------------------------------------
412 // Canvas state operations: Capture
413 // ----------------------------------------------------------------------------
414 
captureCanvasState() const415 SkCanvasState* SkiaCanvas::captureCanvasState() const {
416     SkCanvas* canvas = mCanvas;
417     if (mCanvasOwned) {
418         // Important to use the underlying SkCanvas, not the wrapper.
419         canvas = mCanvasOwned.get();
420     }
421 
422     // Workarounds for http://crbug.com/271096: SW draw only supports
423     // translate & scale transforms, and a simple rectangular clip.
424     // (This also avoids significant wasted time in calling
425     // SkCanvasStateUtils::CaptureCanvasState when the clip is complex).
426     if (!canvas->isClipRect() || (canvas->getTotalMatrix().getType() &
427                                   ~(SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask))) {
428         return nullptr;
429     }
430 
431     return SkCanvasStateUtils::CaptureCanvasState(canvas);
432 }
433 
434 // ----------------------------------------------------------------------------
435 // Canvas draw operations
436 // ----------------------------------------------------------------------------
437 
drawColor(int color,SkBlendMode mode)438 void SkiaCanvas::drawColor(int color, SkBlendMode mode) {
439     mCanvas->drawColor(color, mode);
440 }
441 
onFilterPaint(SkPaint & paint)442 void SkiaCanvas::onFilterPaint(SkPaint& paint) {
443     if (mPaintFilter) {
444         mPaintFilter->filter(&paint);
445     }
446 }
447 
drawPaint(const SkPaint & paint)448 void SkiaCanvas::drawPaint(const SkPaint& paint) {
449     mCanvas->drawPaint(filterPaint(paint));
450 }
451 
452 // ----------------------------------------------------------------------------
453 // Canvas draw operations: Geometry
454 // ----------------------------------------------------------------------------
455 
drawPoints(const float * points,int count,const Paint & paint,SkCanvas::PointMode mode)456 void SkiaCanvas::drawPoints(const float* points, int count, const Paint& paint,
457                             SkCanvas::PointMode mode) {
458     if (CC_UNLIKELY(count < 2 || paint.nothingToDraw())) return;
459     // convert the floats into SkPoints
460     count >>= 1;  // now it is the number of points
461     std::unique_ptr<SkPoint[]> pts(new SkPoint[count]);
462     for (int i = 0; i < count; i++) {
463         pts[i].set(points[0], points[1]);
464         points += 2;
465     }
466 
467     applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawPoints(mode, count, pts.get(), p); });
468 }
469 
drawPoint(float x,float y,const Paint & paint)470 void SkiaCanvas::drawPoint(float x, float y, const Paint& paint) {
471     applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawPoint(x, y, p); });
472 }
473 
drawPoints(const float * points,int count,const Paint & paint)474 void SkiaCanvas::drawPoints(const float* points, int count, const Paint& paint) {
475     this->drawPoints(points, count, paint, SkCanvas::kPoints_PointMode);
476 }
477 
drawLine(float startX,float startY,float stopX,float stopY,const Paint & paint)478 void SkiaCanvas::drawLine(float startX, float startY, float stopX, float stopY,
479                           const Paint& paint) {
480     applyLooper(&paint,
481                 [&](const SkPaint& p) { mCanvas->drawLine(startX, startY, stopX, stopY, p); });
482 }
483 
drawLines(const float * points,int count,const Paint & paint)484 void SkiaCanvas::drawLines(const float* points, int count, const Paint& paint) {
485     if (CC_UNLIKELY(count < 4 || paint.nothingToDraw())) return;
486     this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode);
487 }
488 
drawRect(float left,float top,float right,float bottom,const Paint & paint)489 void SkiaCanvas::drawRect(float left, float top, float right, float bottom, const Paint& paint) {
490     if (CC_UNLIKELY(paint.nothingToDraw())) return;
491     applyLooper(&paint, [&](const SkPaint& p) {
492         mCanvas->drawRect({left, top, right, bottom}, p);
493     });
494 }
495 
drawRegion(const SkRegion & region,const Paint & paint)496 void SkiaCanvas::drawRegion(const SkRegion& region, const Paint& paint) {
497     if (CC_UNLIKELY(paint.nothingToDraw())) return;
498     applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawRegion(region, p); });
499 }
500 
drawRoundRect(float left,float top,float right,float bottom,float rx,float ry,const Paint & paint)501 void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom, float rx, float ry,
502                                const Paint& paint) {
503     if (CC_UNLIKELY(paint.nothingToDraw())) return;
504     SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
505     applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawRoundRect(rect, rx, ry, p); });
506 }
507 
drawDoubleRoundRect(const SkRRect & outer,const SkRRect & inner,const Paint & paint)508 void SkiaCanvas::drawDoubleRoundRect(const SkRRect& outer, const SkRRect& inner,
509                                 const Paint& paint) {
510     applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawDRRect(outer, inner, p); });
511 }
512 
drawCircle(float x,float y,float radius,const Paint & paint)513 void SkiaCanvas::drawCircle(float x, float y, float radius, const Paint& paint) {
514     if (CC_UNLIKELY(radius <= 0 || paint.nothingToDraw())) return;
515     applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawCircle(x, y, radius, p); });
516 }
517 
drawOval(float left,float top,float right,float bottom,const Paint & paint)518 void SkiaCanvas::drawOval(float left, float top, float right, float bottom, const Paint& paint) {
519     if (CC_UNLIKELY(paint.nothingToDraw())) return;
520     SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
521     applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawOval(oval, p); });
522 }
523 
drawArc(float left,float top,float right,float bottom,float startAngle,float sweepAngle,bool useCenter,const Paint & paint)524 void SkiaCanvas::drawArc(float left, float top, float right, float bottom, float startAngle,
525                          float sweepAngle, bool useCenter, const Paint& paint) {
526     if (CC_UNLIKELY(paint.nothingToDraw())) return;
527     SkRect arc = SkRect::MakeLTRB(left, top, right, bottom);
528     applyLooper(&paint, [&](const SkPaint& p) {
529         if (fabs(sweepAngle) >= 360.0f) {
530             mCanvas->drawOval(arc, p);
531         } else {
532             mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, p);
533         }
534     });
535 }
536 
drawPath(const SkPath & path,const Paint & paint)537 void SkiaCanvas::drawPath(const SkPath& path, const Paint& paint) {
538     if (CC_UNLIKELY(paint.nothingToDraw())) return;
539     if (CC_UNLIKELY(path.isEmpty() && (!path.isInverseFillType()))) {
540         return;
541     }
542     applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawPath(path, p); });
543 }
544 
drawVertices(const SkVertices * vertices,SkBlendMode mode,const Paint & paint)545 void SkiaCanvas::drawVertices(const SkVertices* vertices, SkBlendMode mode, const Paint& paint) {
546     applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawVertices(vertices, mode, p); });
547 }
548 
549 // ----------------------------------------------------------------------------
550 // Canvas draw operations: Bitmaps
551 // ----------------------------------------------------------------------------
552 
drawBitmap(Bitmap & bitmap,float left,float top,const Paint * paint)553 void SkiaCanvas::drawBitmap(Bitmap& bitmap, float left, float top, const Paint* paint) {
554     auto image = bitmap.makeImage();
555     applyLooper(paint, [&](const SkPaint& p) {
556         auto sampling = SkSamplingOptions(p.getFilterQuality());
557         mCanvas->drawImage(image, left, top, sampling, &p);
558     });
559 }
560 
drawBitmap(Bitmap & bitmap,const SkMatrix & matrix,const Paint * paint)561 void SkiaCanvas::drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const Paint* paint) {
562     auto image = bitmap.makeImage();
563     SkAutoCanvasRestore acr(mCanvas, true);
564     mCanvas->concat(matrix);
565     applyLooper(paint, [&](const SkPaint& p) {
566         auto sampling = SkSamplingOptions(p.getFilterQuality());
567         mCanvas->drawImage(image, 0, 0, sampling, &p);
568     });
569 }
570 
drawBitmap(Bitmap & bitmap,float srcLeft,float srcTop,float srcRight,float srcBottom,float dstLeft,float dstTop,float dstRight,float dstBottom,const Paint * paint)571 void SkiaCanvas::drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight,
572                             float srcBottom, float dstLeft, float dstTop, float dstRight,
573                             float dstBottom, const Paint* paint) {
574     auto image = bitmap.makeImage();
575     SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
576     SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
577 
578     applyLooper(paint, [&](const SkPaint& p) {
579         auto sampling = SkSamplingOptions(p.getFilterQuality());
580         mCanvas->drawImageRect(image, srcRect, dstRect, sampling, &p,
581                                SkCanvas::kFast_SrcRectConstraint);
582     });
583 }
584 
paintToFilter(const Paint * paint)585 static SkFilterMode paintToFilter(const Paint* paint) {
586     return paint && paint->isFilterBitmap() ? SkFilterMode::kLinear
587                                             : SkFilterMode::kNearest;
588 }
589 
drawBitmapMesh(Bitmap & bitmap,int meshWidth,int meshHeight,const float * vertices,const int * colors,const Paint * paint)590 void SkiaCanvas::drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight,
591                                 const float* vertices, const int* colors, const Paint* paint) {
592     const int ptCount = (meshWidth + 1) * (meshHeight + 1);
593     const int indexCount = meshWidth * meshHeight * 6;
594     uint32_t flags = SkVertices::kHasTexCoords_BuilderFlag;
595     if (colors) {
596         flags |= SkVertices::kHasColors_BuilderFlag;
597     }
598     SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, ptCount, indexCount, flags);
599     memcpy(builder.positions(), vertices, ptCount * sizeof(SkPoint));
600     if (colors) {
601         memcpy(builder.colors(), colors, ptCount * sizeof(SkColor));
602     }
603     SkPoint* texs = builder.texCoords();
604     uint16_t* indices = builder.indices();
605 
606     // cons up texture coordinates and indices
607     {
608         const SkScalar w = SkIntToScalar(bitmap.width());
609         const SkScalar h = SkIntToScalar(bitmap.height());
610         const SkScalar dx = w / meshWidth;
611         const SkScalar dy = h / meshHeight;
612 
613         SkPoint* texsPtr = texs;
614         SkScalar y = 0;
615         for (int i = 0; i <= meshHeight; i++) {
616             if (i == meshHeight) {
617                 y = h;  // to ensure numerically we hit h exactly
618             }
619             SkScalar x = 0;
620             for (int j = 0; j < meshWidth; j++) {
621                 texsPtr->set(x, y);
622                 texsPtr += 1;
623                 x += dx;
624             }
625             texsPtr->set(w, y);
626             texsPtr += 1;
627             y += dy;
628         }
629         SkASSERT(texsPtr - texs == ptCount);
630     }
631 
632     // cons up indices
633     {
634         uint16_t* indexPtr = indices;
635         int index = 0;
636         for (int i = 0; i < meshHeight; i++) {
637             for (int j = 0; j < meshWidth; j++) {
638                 // lower-left triangle
639                 *indexPtr++ = index;
640                 *indexPtr++ = index + meshWidth + 1;
641                 *indexPtr++ = index + meshWidth + 2;
642                 // upper-right triangle
643                 *indexPtr++ = index;
644                 *indexPtr++ = index + meshWidth + 2;
645                 *indexPtr++ = index + 1;
646                 // bump to the next cell
647                 index += 1;
648             }
649             // bump to the next row
650             index += 1;
651         }
652         SkASSERT(indexPtr - indices == indexCount);
653     }
654 
655 // double-check that we have legal indices
656 #ifdef SK_DEBUG
657     {
658         for (int i = 0; i < indexCount; i++) {
659             SkASSERT((unsigned)indices[i] < (unsigned)ptCount);
660         }
661     }
662 #endif
663 
664     auto image = bitmap.makeImage();
665 
666     // cons-up a shader for the bitmap
667     Paint pnt;
668     if (paint) {
669         pnt = *paint;
670     }
671     SkSamplingOptions sampling(paintToFilter(&pnt));
672     pnt.setShader(image->makeShader(sampling));
673 
674     auto v = builder.detach();
675     applyLooper(&pnt, [&](const SkPaint& p) {
676         SkPaint copy(p);
677         auto s = SkSamplingOptions(p.getFilterQuality());
678         if (s != sampling) {
679             // applyLooper changed the quality?
680             copy.setShader(image->makeShader(s));
681         }
682         mCanvas->drawVertices(v, SkBlendMode::kModulate, copy);
683     });
684 }
685 
drawNinePatch(Bitmap & bitmap,const Res_png_9patch & chunk,float dstLeft,float dstTop,float dstRight,float dstBottom,const Paint * paint)686 void SkiaCanvas::drawNinePatch(Bitmap& bitmap, const Res_png_9patch& chunk, float dstLeft,
687                                float dstTop, float dstRight, float dstBottom,
688                                const Paint* paint) {
689     SkCanvas::Lattice lattice;
690     NinePatchUtils::SetLatticeDivs(&lattice, chunk, bitmap.width(), bitmap.height());
691 
692     lattice.fRectTypes = nullptr;
693     lattice.fColors = nullptr;
694     int numFlags = 0;
695     if (chunk.numColors > 0 && chunk.numColors == NinePatchUtils::NumDistinctRects(lattice)) {
696         // We can expect the framework to give us a color for every distinct rect.
697         // Skia requires a flag for every rect.
698         numFlags = (lattice.fXCount + 1) * (lattice.fYCount + 1);
699     }
700 
701     SkAutoSTMalloc<25, SkCanvas::Lattice::RectType> flags(numFlags);
702     SkAutoSTMalloc<25, SkColor> colors(numFlags);
703     if (numFlags > 0) {
704         NinePatchUtils::SetLatticeFlags(&lattice, flags.get(), numFlags, chunk, colors.get());
705     }
706 
707     lattice.fBounds = nullptr;
708     SkRect dst = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
709     auto image = bitmap.makeImage();
710     applyLooper(paint, [&](const SkPaint& p) {
711         auto filter = SkSamplingOptions(p.getFilterQuality()).filter;
712         mCanvas->drawImageLattice(image.get(), lattice, dst, filter, &p);
713     });
714 }
715 
drawAnimatedImage(AnimatedImageDrawable * imgDrawable)716 double SkiaCanvas::drawAnimatedImage(AnimatedImageDrawable* imgDrawable) {
717     return imgDrawable->drawStaging(mCanvas);
718 }
719 
drawVectorDrawable(VectorDrawableRoot * vectorDrawable)720 void SkiaCanvas::drawVectorDrawable(VectorDrawableRoot* vectorDrawable) {
721     vectorDrawable->drawStaging(this);
722 }
723 
724 // ----------------------------------------------------------------------------
725 // Canvas draw operations: Text
726 // ----------------------------------------------------------------------------
727 
drawGlyphs(ReadGlyphFunc glyphFunc,int count,const Paint & paint,float x,float y,float totalAdvance)728 void SkiaCanvas::drawGlyphs(ReadGlyphFunc glyphFunc, int count, const Paint& paint, float x,
729                             float y, float totalAdvance) {
730     if (count <= 0 || paint.nothingToDraw()) return;
731     Paint paintCopy(paint);
732     if (mPaintFilter) {
733         mPaintFilter->filterFullPaint(&paintCopy);
734     }
735     const SkFont& font = paintCopy.getSkFont();
736     // Stroke with a hairline is drawn on HW with a fill style for compatibility with Android O and
737     // older.
738     if (!mCanvasOwned && sApiLevel <= 27 && paintCopy.getStrokeWidth() <= 0 &&
739         paintCopy.getStyle() == SkPaint::kStroke_Style) {
740         paintCopy.setStyle(SkPaint::kFill_Style);
741     }
742 
743     SkTextBlobBuilder builder;
744     const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(font, count);
745     glyphFunc(buffer.glyphs, buffer.pos);
746 
747     sk_sp<SkTextBlob> textBlob(builder.make());
748 
749     applyLooper(&paintCopy, [&](const SkPaint& p) { mCanvas->drawTextBlob(textBlob, 0, 0, p); });
750     drawTextDecorations(x, y, totalAdvance, paintCopy);
751 }
752 
drawLayoutOnPath(const minikin::Layout & layout,float hOffset,float vOffset,const Paint & paint,const SkPath & path,size_t start,size_t end)753 void SkiaCanvas::drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
754                                   const Paint& paint, const SkPath& path, size_t start,
755                                   size_t end) {
756     Paint paintCopy(paint);
757     if (mPaintFilter) {
758         mPaintFilter->filterFullPaint(&paintCopy);
759     }
760     const SkFont& font = paintCopy.getSkFont();
761 
762     const int N = end - start;
763     SkTextBlobBuilder builder;
764     auto rec = builder.allocRunRSXform(font, N);
765     SkRSXform* xform = (SkRSXform*)rec.pos;
766     uint16_t* glyphs = rec.glyphs;
767     SkPathMeasure meas(path, false);
768 
769     for (size_t i = start; i < end; i++) {
770         glyphs[i - start] = layout.getGlyphId(i);
771         float halfWidth = layout.getCharAdvance(i) * 0.5f;
772         float x = hOffset + layout.getX(i) + halfWidth;
773         float y = vOffset + layout.getY(i);
774 
775         SkPoint pos;
776         SkVector tan;
777         if (!meas.getPosTan(x, &pos, &tan)) {
778             pos.set(x, y);
779             tan.set(1, 0);
780         }
781         xform[i - start].fSCos = tan.x();
782         xform[i - start].fSSin = tan.y();
783         xform[i - start].fTx = pos.x() - tan.y() * y - halfWidth * tan.x();
784         xform[i - start].fTy = pos.y() + tan.x() * y - halfWidth * tan.y();
785     }
786 
787     sk_sp<SkTextBlob> textBlob(builder.make());
788 
789     applyLooper(&paintCopy, [&](const SkPaint& p) { mCanvas->drawTextBlob(textBlob, 0, 0, p); });
790 }
791 
792 // ----------------------------------------------------------------------------
793 // Canvas draw operations: Animations
794 // ----------------------------------------------------------------------------
795 
drawRoundRect(uirenderer::CanvasPropertyPrimitive * left,uirenderer::CanvasPropertyPrimitive * top,uirenderer::CanvasPropertyPrimitive * right,uirenderer::CanvasPropertyPrimitive * bottom,uirenderer::CanvasPropertyPrimitive * rx,uirenderer::CanvasPropertyPrimitive * ry,uirenderer::CanvasPropertyPaint * paint)796 void SkiaCanvas::drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
797                                uirenderer::CanvasPropertyPrimitive* top,
798                                uirenderer::CanvasPropertyPrimitive* right,
799                                uirenderer::CanvasPropertyPrimitive* bottom,
800                                uirenderer::CanvasPropertyPrimitive* rx,
801                                uirenderer::CanvasPropertyPrimitive* ry,
802                                uirenderer::CanvasPropertyPaint* paint) {
803     sk_sp<uirenderer::skiapipeline::AnimatedRoundRect> drawable(
804             new uirenderer::skiapipeline::AnimatedRoundRect(left, top, right, bottom, rx, ry,
805                                                             paint));
806     mCanvas->drawDrawable(drawable.get());
807 }
808 
drawCircle(uirenderer::CanvasPropertyPrimitive * x,uirenderer::CanvasPropertyPrimitive * y,uirenderer::CanvasPropertyPrimitive * radius,uirenderer::CanvasPropertyPaint * paint)809 void SkiaCanvas::drawCircle(uirenderer::CanvasPropertyPrimitive* x,
810                             uirenderer::CanvasPropertyPrimitive* y,
811                             uirenderer::CanvasPropertyPrimitive* radius,
812                             uirenderer::CanvasPropertyPaint* paint) {
813     sk_sp<uirenderer::skiapipeline::AnimatedCircle> drawable(
814             new uirenderer::skiapipeline::AnimatedCircle(x, y, radius, paint));
815     mCanvas->drawDrawable(drawable.get());
816 }
817 
drawRipple(const uirenderer::skiapipeline::RippleDrawableParams & params)818 void SkiaCanvas::drawRipple(const uirenderer::skiapipeline::RippleDrawableParams& params) {
819     uirenderer::skiapipeline::AnimatedRippleDrawable::draw(mCanvas, params);
820 }
821 
drawPicture(const SkPicture & picture)822 void SkiaCanvas::drawPicture(const SkPicture& picture) {
823     // TODO: Change to mCanvas->drawPicture()? SkCanvas::drawPicture seems to be
824     // where the logic is for playback vs. ref picture. Using picture.playback here
825     // to stay behavior-identical for now, but should revisit this at some point.
826     picture.playback(mCanvas);
827 }
828 
829 // ----------------------------------------------------------------------------
830 // Canvas draw operations: View System
831 // ----------------------------------------------------------------------------
832 
drawLayer(uirenderer::DeferredLayerUpdater * layerUpdater)833 void SkiaCanvas::drawLayer(uirenderer::DeferredLayerUpdater* layerUpdater) {
834     LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw Layers");
835 }
836 
drawRenderNode(uirenderer::RenderNode * renderNode)837 void SkiaCanvas::drawRenderNode(uirenderer::RenderNode* renderNode) {
838     LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw RenderNodes");
839 }
840 
841 }  // namespace android
842