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 Paint & paint)185 void SkiaCanvas::restoreUnclippedLayer(int restoreCount, const Paint& 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
replaceClipRect_deprecated(float left,float top,float right,float bottom)399 bool SkiaCanvas::replaceClipRect_deprecated(float left, float top, float right, float bottom) {
400 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
401
402 // Emulated clip rects are not recorded for partial saves, since
403 // partial saves have been removed from the public API.
404 SkAndroidFrameworkUtils::ResetClip(mCanvas);
405 mCanvas->clipRect(rect, SkClipOp::kIntersect);
406 return !mCanvas->isClipEmpty();
407 }
408
replaceClipPath_deprecated(const SkPath * path)409 bool SkiaCanvas::replaceClipPath_deprecated(const SkPath* path) {
410 SkAndroidFrameworkUtils::ResetClip(mCanvas);
411 mCanvas->clipPath(*path, SkClipOp::kIntersect, true);
412 return !mCanvas->isClipEmpty();
413 }
414
415 // ----------------------------------------------------------------------------
416 // Canvas state operations: Filters
417 // ----------------------------------------------------------------------------
418
getPaintFilter()419 PaintFilter* SkiaCanvas::getPaintFilter() {
420 return mPaintFilter.get();
421 }
422
setPaintFilter(sk_sp<PaintFilter> paintFilter)423 void SkiaCanvas::setPaintFilter(sk_sp<PaintFilter> paintFilter) {
424 mPaintFilter = std::move(paintFilter);
425 }
426
427 // ----------------------------------------------------------------------------
428 // Canvas state operations: Capture
429 // ----------------------------------------------------------------------------
430
captureCanvasState() const431 SkCanvasState* SkiaCanvas::captureCanvasState() const {
432 SkCanvas* canvas = mCanvas;
433 if (mCanvasOwned) {
434 // Important to use the underlying SkCanvas, not the wrapper.
435 canvas = mCanvasOwned.get();
436 }
437
438 // Workarounds for http://crbug.com/271096: SW draw only supports
439 // translate & scale transforms, and a simple rectangular clip.
440 // (This also avoids significant wasted time in calling
441 // SkCanvasStateUtils::CaptureCanvasState when the clip is complex).
442 if (!canvas->isClipRect() || (canvas->getTotalMatrix().getType() &
443 ~(SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask))) {
444 return nullptr;
445 }
446
447 return SkCanvasStateUtils::CaptureCanvasState(canvas);
448 }
449
450 // ----------------------------------------------------------------------------
451 // Canvas draw operations
452 // ----------------------------------------------------------------------------
453
drawColor(int color,SkBlendMode mode)454 void SkiaCanvas::drawColor(int color, SkBlendMode mode) {
455 mCanvas->drawColor(color, mode);
456 }
457
onFilterPaint(Paint & paint)458 void SkiaCanvas::onFilterPaint(Paint& paint) {
459 if (mPaintFilter) {
460 mPaintFilter->filterFullPaint(&paint);
461 }
462 }
463
drawPaint(const Paint & paint)464 void SkiaCanvas::drawPaint(const Paint& paint) {
465 mCanvas->drawPaint(filterPaint(paint));
466 }
467
468 // ----------------------------------------------------------------------------
469 // Canvas draw operations: Geometry
470 // ----------------------------------------------------------------------------
471
drawPoints(const float * points,int count,const Paint & paint,SkCanvas::PointMode mode)472 void SkiaCanvas::drawPoints(const float* points, int count, const Paint& paint,
473 SkCanvas::PointMode mode) {
474 if (CC_UNLIKELY(count < 2 || paint.nothingToDraw())) return;
475 // convert the floats into SkPoints
476 count >>= 1; // now it is the number of points
477 std::unique_ptr<SkPoint[]> pts(new SkPoint[count]);
478 for (int i = 0; i < count; i++) {
479 pts[i].set(points[0], points[1]);
480 points += 2;
481 }
482
483 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawPoints(mode, count, pts.get(), p); });
484 }
485
drawPoint(float x,float y,const Paint & paint)486 void SkiaCanvas::drawPoint(float x, float y, const Paint& paint) {
487 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawPoint(x, y, p); });
488 }
489
drawPoints(const float * points,int count,const Paint & paint)490 void SkiaCanvas::drawPoints(const float* points, int count, const Paint& paint) {
491 this->drawPoints(points, count, paint, SkCanvas::kPoints_PointMode);
492 }
493
drawLine(float startX,float startY,float stopX,float stopY,const Paint & paint)494 void SkiaCanvas::drawLine(float startX, float startY, float stopX, float stopY,
495 const Paint& paint) {
496 applyLooper(&paint,
497 [&](const SkPaint& p) { mCanvas->drawLine(startX, startY, stopX, stopY, p); });
498 }
499
drawLines(const float * points,int count,const Paint & paint)500 void SkiaCanvas::drawLines(const float* points, int count, const Paint& paint) {
501 if (CC_UNLIKELY(count < 4 || paint.nothingToDraw())) return;
502 this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode);
503 }
504
drawRect(float left,float top,float right,float bottom,const Paint & paint)505 void SkiaCanvas::drawRect(float left, float top, float right, float bottom, const Paint& paint) {
506 if (CC_UNLIKELY(paint.nothingToDraw())) return;
507 applyLooper(&paint, [&](const SkPaint& p) {
508 mCanvas->drawRect({left, top, right, bottom}, p);
509 });
510 }
511
drawRegion(const SkRegion & region,const Paint & paint)512 void SkiaCanvas::drawRegion(const SkRegion& region, const Paint& paint) {
513 if (CC_UNLIKELY(paint.nothingToDraw())) return;
514 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawRegion(region, p); });
515 }
516
drawRoundRect(float left,float top,float right,float bottom,float rx,float ry,const Paint & paint)517 void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom, float rx, float ry,
518 const Paint& paint) {
519 if (CC_UNLIKELY(paint.nothingToDraw())) return;
520 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
521 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawRoundRect(rect, rx, ry, p); });
522 }
523
drawDoubleRoundRect(const SkRRect & outer,const SkRRect & inner,const Paint & paint)524 void SkiaCanvas::drawDoubleRoundRect(const SkRRect& outer, const SkRRect& inner,
525 const Paint& paint) {
526 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawDRRect(outer, inner, p); });
527 }
528
drawCircle(float x,float y,float radius,const Paint & paint)529 void SkiaCanvas::drawCircle(float x, float y, float radius, const Paint& paint) {
530 if (CC_UNLIKELY(radius <= 0 || paint.nothingToDraw())) return;
531 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawCircle(x, y, radius, p); });
532 }
533
drawOval(float left,float top,float right,float bottom,const Paint & paint)534 void SkiaCanvas::drawOval(float left, float top, float right, float bottom, const Paint& paint) {
535 if (CC_UNLIKELY(paint.nothingToDraw())) return;
536 SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
537 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawOval(oval, p); });
538 }
539
drawArc(float left,float top,float right,float bottom,float startAngle,float sweepAngle,bool useCenter,const Paint & paint)540 void SkiaCanvas::drawArc(float left, float top, float right, float bottom, float startAngle,
541 float sweepAngle, bool useCenter, const Paint& paint) {
542 if (CC_UNLIKELY(paint.nothingToDraw())) return;
543 SkRect arc = SkRect::MakeLTRB(left, top, right, bottom);
544 applyLooper(&paint, [&](const SkPaint& p) {
545 if (fabs(sweepAngle) >= 360.0f) {
546 mCanvas->drawOval(arc, p);
547 } else {
548 mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, p);
549 }
550 });
551 }
552
drawPath(const SkPath & path,const Paint & paint)553 void SkiaCanvas::drawPath(const SkPath& path, const Paint& paint) {
554 if (CC_UNLIKELY(paint.nothingToDraw())) return;
555 if (CC_UNLIKELY(path.isEmpty() && (!path.isInverseFillType()))) {
556 return;
557 }
558 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawPath(path, p); });
559 }
560
drawVertices(const SkVertices * vertices,SkBlendMode mode,const Paint & paint)561 void SkiaCanvas::drawVertices(const SkVertices* vertices, SkBlendMode mode, const Paint& paint) {
562 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawVertices(vertices, mode, p); });
563 }
564
565 // ----------------------------------------------------------------------------
566 // Canvas draw operations: Bitmaps
567 // ----------------------------------------------------------------------------
568
drawBitmap(Bitmap & bitmap,float left,float top,const Paint * paint)569 void SkiaCanvas::drawBitmap(Bitmap& bitmap, float left, float top, const Paint* paint) {
570 auto image = bitmap.makeImage();
571 applyLooper(paint, [&](const Paint& p) {
572 mCanvas->drawImage(image, left, top, p.sampling(), &p);
573 });
574 }
575
drawBitmap(Bitmap & bitmap,const SkMatrix & matrix,const Paint * paint)576 void SkiaCanvas::drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const Paint* paint) {
577 auto image = bitmap.makeImage();
578 SkAutoCanvasRestore acr(mCanvas, true);
579 mCanvas->concat(matrix);
580 applyLooper(paint, [&](const Paint& p) {
581 mCanvas->drawImage(image, 0, 0, p.sampling(), &p);
582 });
583 }
584
drawBitmap(Bitmap & bitmap,float srcLeft,float srcTop,float srcRight,float srcBottom,float dstLeft,float dstTop,float dstRight,float dstBottom,const Paint * paint)585 void SkiaCanvas::drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight,
586 float srcBottom, float dstLeft, float dstTop, float dstRight,
587 float dstBottom, const Paint* paint) {
588 auto image = bitmap.makeImage();
589 SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
590 SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
591
592 applyLooper(paint, [&](const Paint& p) {
593 mCanvas->drawImageRect(image, srcRect, dstRect, p.sampling(), &p,
594 SkCanvas::kFast_SrcRectConstraint);
595 });
596 }
597
drawBitmapMesh(Bitmap & bitmap,int meshWidth,int meshHeight,const float * vertices,const int * colors,const Paint * paint)598 void SkiaCanvas::drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight,
599 const float* vertices, const int* colors, const Paint* paint) {
600 const int ptCount = (meshWidth + 1) * (meshHeight + 1);
601 const int indexCount = meshWidth * meshHeight * 6;
602 uint32_t flags = SkVertices::kHasTexCoords_BuilderFlag;
603 if (colors) {
604 flags |= SkVertices::kHasColors_BuilderFlag;
605 }
606 SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, ptCount, indexCount, flags);
607 memcpy(builder.positions(), vertices, ptCount * sizeof(SkPoint));
608 if (colors) {
609 memcpy(builder.colors(), colors, ptCount * sizeof(SkColor));
610 }
611 SkPoint* texs = builder.texCoords();
612 uint16_t* indices = builder.indices();
613
614 // cons up texture coordinates and indices
615 {
616 const SkScalar w = SkIntToScalar(bitmap.width());
617 const SkScalar h = SkIntToScalar(bitmap.height());
618 const SkScalar dx = w / meshWidth;
619 const SkScalar dy = h / meshHeight;
620
621 SkPoint* texsPtr = texs;
622 SkScalar y = 0;
623 for (int i = 0; i <= meshHeight; i++) {
624 if (i == meshHeight) {
625 y = h; // to ensure numerically we hit h exactly
626 }
627 SkScalar x = 0;
628 for (int j = 0; j < meshWidth; j++) {
629 texsPtr->set(x, y);
630 texsPtr += 1;
631 x += dx;
632 }
633 texsPtr->set(w, y);
634 texsPtr += 1;
635 y += dy;
636 }
637 SkASSERT(texsPtr - texs == ptCount);
638 }
639
640 // cons up indices
641 {
642 uint16_t* indexPtr = indices;
643 int index = 0;
644 for (int i = 0; i < meshHeight; i++) {
645 for (int j = 0; j < meshWidth; j++) {
646 // lower-left triangle
647 *indexPtr++ = index;
648 *indexPtr++ = index + meshWidth + 1;
649 *indexPtr++ = index + meshWidth + 2;
650 // upper-right triangle
651 *indexPtr++ = index;
652 *indexPtr++ = index + meshWidth + 2;
653 *indexPtr++ = index + 1;
654 // bump to the next cell
655 index += 1;
656 }
657 // bump to the next row
658 index += 1;
659 }
660 SkASSERT(indexPtr - indices == indexCount);
661 }
662
663 // double-check that we have legal indices
664 #ifdef SK_DEBUG
665 {
666 for (int i = 0; i < indexCount; i++) {
667 SkASSERT((unsigned)indices[i] < (unsigned)ptCount);
668 }
669 }
670 #endif
671
672 auto image = bitmap.makeImage();
673
674 // cons-up a shader for the bitmap
675 Paint pnt;
676 if (paint) {
677 pnt = *paint;
678 }
679 SkSamplingOptions sampling = pnt.sampling();
680 pnt.setShader(image->makeShader(sampling));
681
682 auto v = builder.detach();
683 applyLooper(&pnt, [&](const Paint& p) {
684 SkPaint copy(p);
685 auto s = p.sampling();
686 if (s != sampling) {
687 // applyLooper changed the quality?
688 copy.setShader(image->makeShader(s));
689 }
690 mCanvas->drawVertices(v, SkBlendMode::kModulate, copy);
691 });
692 }
693
drawNinePatch(Bitmap & bitmap,const Res_png_9patch & chunk,float dstLeft,float dstTop,float dstRight,float dstBottom,const Paint * paint)694 void SkiaCanvas::drawNinePatch(Bitmap& bitmap, const Res_png_9patch& chunk, float dstLeft,
695 float dstTop, float dstRight, float dstBottom,
696 const Paint* paint) {
697 SkCanvas::Lattice lattice;
698 NinePatchUtils::SetLatticeDivs(&lattice, chunk, bitmap.width(), bitmap.height());
699
700 lattice.fRectTypes = nullptr;
701 lattice.fColors = nullptr;
702 int numFlags = 0;
703 if (chunk.numColors > 0 && chunk.numColors == NinePatchUtils::NumDistinctRects(lattice)) {
704 // We can expect the framework to give us a color for every distinct rect.
705 // Skia requires a flag for every rect.
706 numFlags = (lattice.fXCount + 1) * (lattice.fYCount + 1);
707 }
708
709 SkAutoSTMalloc<25, SkCanvas::Lattice::RectType> flags(numFlags);
710 SkAutoSTMalloc<25, SkColor> colors(numFlags);
711 if (numFlags > 0) {
712 NinePatchUtils::SetLatticeFlags(&lattice, flags.get(), numFlags, chunk, colors.get());
713 }
714
715 lattice.fBounds = nullptr;
716 SkRect dst = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
717 auto image = bitmap.makeImage();
718 applyLooper(paint, [&](const Paint& p) {
719 mCanvas->drawImageLattice(image.get(), lattice, dst, p.filterMode(), &p);
720 });
721 }
722
drawAnimatedImage(AnimatedImageDrawable * imgDrawable)723 double SkiaCanvas::drawAnimatedImage(AnimatedImageDrawable* imgDrawable) {
724 return imgDrawable->drawStaging(mCanvas);
725 }
726
drawVectorDrawable(VectorDrawableRoot * vectorDrawable)727 void SkiaCanvas::drawVectorDrawable(VectorDrawableRoot* vectorDrawable) {
728 vectorDrawable->drawStaging(this);
729 }
730
731 // ----------------------------------------------------------------------------
732 // Canvas draw operations: Text
733 // ----------------------------------------------------------------------------
734
drawGlyphs(ReadGlyphFunc glyphFunc,int count,const Paint & paint,float x,float y,float totalAdvance)735 void SkiaCanvas::drawGlyphs(ReadGlyphFunc glyphFunc, int count, const Paint& paint, float x,
736 float y, float totalAdvance) {
737 if (count <= 0 || paint.nothingToDraw()) return;
738 Paint paintCopy(paint);
739 if (mPaintFilter) {
740 mPaintFilter->filterFullPaint(&paintCopy);
741 }
742 const SkFont& font = paintCopy.getSkFont();
743 // Stroke with a hairline is drawn on HW with a fill style for compatibility with Android O and
744 // older.
745 if (!mCanvasOwned && sApiLevel <= 27 && paintCopy.getStrokeWidth() <= 0 &&
746 paintCopy.getStyle() == SkPaint::kStroke_Style) {
747 paintCopy.setStyle(SkPaint::kFill_Style);
748 }
749
750 SkTextBlobBuilder builder;
751 const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(font, count);
752 glyphFunc(buffer.glyphs, buffer.pos);
753
754 sk_sp<SkTextBlob> textBlob(builder.make());
755
756 applyLooper(&paintCopy, [&](const SkPaint& p) { mCanvas->drawTextBlob(textBlob, 0, 0, p); });
757 drawTextDecorations(x, y, totalAdvance, paintCopy);
758 }
759
drawLayoutOnPath(const minikin::Layout & layout,float hOffset,float vOffset,const Paint & paint,const SkPath & path,size_t start,size_t end)760 void SkiaCanvas::drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
761 const Paint& paint, const SkPath& path, size_t start,
762 size_t end) {
763 Paint paintCopy(paint);
764 if (mPaintFilter) {
765 mPaintFilter->filterFullPaint(&paintCopy);
766 }
767 const SkFont& font = paintCopy.getSkFont();
768
769 const int N = end - start;
770 SkTextBlobBuilder builder;
771 auto rec = builder.allocRunRSXform(font, N);
772 SkRSXform* xform = (SkRSXform*)rec.pos;
773 uint16_t* glyphs = rec.glyphs;
774 SkPathMeasure meas(path, false);
775
776 for (size_t i = start; i < end; i++) {
777 glyphs[i - start] = layout.getGlyphId(i);
778 float halfWidth = layout.getCharAdvance(i) * 0.5f;
779 float x = hOffset + layout.getX(i) + halfWidth;
780 float y = vOffset + layout.getY(i);
781
782 SkPoint pos;
783 SkVector tan;
784 if (!meas.getPosTan(x, &pos, &tan)) {
785 pos.set(x, y);
786 tan.set(1, 0);
787 }
788 xform[i - start].fSCos = tan.x();
789 xform[i - start].fSSin = tan.y();
790 xform[i - start].fTx = pos.x() - tan.y() * y - halfWidth * tan.x();
791 xform[i - start].fTy = pos.y() + tan.x() * y - halfWidth * tan.y();
792 }
793
794 sk_sp<SkTextBlob> textBlob(builder.make());
795
796 applyLooper(&paintCopy, [&](const SkPaint& p) { mCanvas->drawTextBlob(textBlob, 0, 0, p); });
797 }
798
799 // ----------------------------------------------------------------------------
800 // Canvas draw operations: Animations
801 // ----------------------------------------------------------------------------
802
drawRoundRect(uirenderer::CanvasPropertyPrimitive * left,uirenderer::CanvasPropertyPrimitive * top,uirenderer::CanvasPropertyPrimitive * right,uirenderer::CanvasPropertyPrimitive * bottom,uirenderer::CanvasPropertyPrimitive * rx,uirenderer::CanvasPropertyPrimitive * ry,uirenderer::CanvasPropertyPaint * paint)803 void SkiaCanvas::drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
804 uirenderer::CanvasPropertyPrimitive* top,
805 uirenderer::CanvasPropertyPrimitive* right,
806 uirenderer::CanvasPropertyPrimitive* bottom,
807 uirenderer::CanvasPropertyPrimitive* rx,
808 uirenderer::CanvasPropertyPrimitive* ry,
809 uirenderer::CanvasPropertyPaint* paint) {
810 sk_sp<uirenderer::skiapipeline::AnimatedRoundRect> drawable(
811 new uirenderer::skiapipeline::AnimatedRoundRect(left, top, right, bottom, rx, ry,
812 paint));
813 mCanvas->drawDrawable(drawable.get());
814 }
815
drawCircle(uirenderer::CanvasPropertyPrimitive * x,uirenderer::CanvasPropertyPrimitive * y,uirenderer::CanvasPropertyPrimitive * radius,uirenderer::CanvasPropertyPaint * paint)816 void SkiaCanvas::drawCircle(uirenderer::CanvasPropertyPrimitive* x,
817 uirenderer::CanvasPropertyPrimitive* y,
818 uirenderer::CanvasPropertyPrimitive* radius,
819 uirenderer::CanvasPropertyPaint* paint) {
820 sk_sp<uirenderer::skiapipeline::AnimatedCircle> drawable(
821 new uirenderer::skiapipeline::AnimatedCircle(x, y, radius, paint));
822 mCanvas->drawDrawable(drawable.get());
823 }
824
drawRipple(const uirenderer::skiapipeline::RippleDrawableParams & params)825 void SkiaCanvas::drawRipple(const uirenderer::skiapipeline::RippleDrawableParams& params) {
826 uirenderer::skiapipeline::AnimatedRippleDrawable::draw(mCanvas, params);
827 }
828
drawPicture(const SkPicture & picture)829 void SkiaCanvas::drawPicture(const SkPicture& picture) {
830 // TODO: Change to mCanvas->drawPicture()? SkCanvas::drawPicture seems to be
831 // where the logic is for playback vs. ref picture. Using picture.playback here
832 // to stay behavior-identical for now, but should revisit this at some point.
833 picture.playback(mCanvas);
834 }
835
836 // ----------------------------------------------------------------------------
837 // Canvas draw operations: View System
838 // ----------------------------------------------------------------------------
839
drawLayer(uirenderer::DeferredLayerUpdater * layerUpdater)840 void SkiaCanvas::drawLayer(uirenderer::DeferredLayerUpdater* layerUpdater) {
841 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw Layers");
842 }
843
drawRenderNode(uirenderer::RenderNode * renderNode)844 void SkiaCanvas::drawRenderNode(uirenderer::RenderNode* renderNode) {
845 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw RenderNodes");
846 }
847
848 } // namespace android
849