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