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