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