blob: 1e0c359e5f8b8e3540d3cc7bfaeb0947b64728b4 [file] [log] [blame]
Derek Sollenberger8872b382014-06-23 14:13:53 -04001/*
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
Derek Sollenbergerc1908132016-07-15 10:28:16 -040017#include "SkiaCanvas.h"
Derek Sollenberger8872b382014-06-23 14:13:53 -040018
Derek Sollenberger24fc9012018-12-07 14:12:12 -050019#include <SkAndroidFrameworkUtils.h>
Leon Scroggins III671cce22018-01-14 16:52:17 -050020#include <SkAnimatedImage.h>
Kevin Lubick856848e2022-02-24 16:24:09 +000021#include <SkBitmap.h>
Kevin Lubicka22c1302023-01-18 14:16:44 +000022#include <SkBlendMode.h>
23#include <SkCanvas.h>
Derek Sollenbergerac33a482019-04-22 16:28:09 -040024#include <SkCanvasPriv.h>
Matt Sarettd0814db2017-04-13 09:33:18 -040025#include <SkCanvasStateUtils.h>
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -040026#include <SkColorFilter.h>
John Reck1bcacfd2017-11-03 10:12:19 -070027#include <SkDrawable.h>
Mike Reed1c79eab2018-11-21 11:01:57 -050028#include <SkFont.h>
John Reck849911a2015-01-20 07:51:14 -080029#include <SkGraphics.h>
Derek Sollenberger6f485562015-07-30 10:00:39 -040030#include <SkImage.h>
Matt Sarett62feb3a2016-09-20 10:34:11 -040031#include <SkImagePriv.h>
Kevin Lubick856848e2022-02-24 16:24:09 +000032#include <SkMatrix.h>
33#include <SkPaint.h>
Leon Scroggins III671cce22018-01-14 16:52:17 -050034#include <SkPicture.h>
Kevin Lubick856848e2022-02-24 16:24:09 +000035#include <SkRRect.h>
Kevin Lubickb3731212023-01-05 19:52:09 +000036#include <SkRSXform.h>
Kevin Lubick856848e2022-02-24 16:24:09 +000037#include <SkRect.h>
38#include <SkRefCnt.h>
John Reck849911a2015-01-20 07:51:14 -080039#include <SkShader.h>
Stan Ilievf50806a2016-10-24 10:40:39 -040040#include <SkTextBlob.h>
Brian Osmane3b9a122020-04-01 12:24:19 -040041#include <SkVertices.h>
Derek Sollenberger8872b382014-06-23 14:13:53 -040042
Ben Wagner60126ef2015-08-07 12:13:48 -040043#include <memory>
Ben Wagner0ed10be2018-06-28 17:08:16 -040044#include <optional>
45#include <utility>
Ben Wagner60126ef2015-08-07 12:13:48 -040046
Kevin Lubickb3731212023-01-05 19:52:09 +000047#include "CanvasProperty.h"
48#include "NinePatchUtils.h"
Kevin Lubickb3731212023-01-05 19:52:09 +000049#include "VectorDrawable.h"
50#include "hwui/Bitmap.h"
51#include "hwui/MinikinUtils.h"
52#include "hwui/PaintFilter.h"
Kevin Lubicka22c1302023-01-18 14:16:44 +000053#include <log/log.h>
Kevin Lubickb3731212023-01-05 19:52:09 +000054#include "pipeline/skia/AnimatedDrawables.h"
55#include "pipeline/skia/HolePunch.h"
Kevin Lubicka22c1302023-01-18 14:16:44 +000056#include <ui/FatVector.h>
Kevin Lubickb3731212023-01-05 19:52:09 +000057
Derek Sollenberger8872b382014-06-23 14:13:53 -040058namespace android {
59
Stan Ilievf50806a2016-10-24 10:40:39 -040060using uirenderer::PaintUtils;
61
Ryan Prichard39971552022-08-26 20:53:33 -070062class SkiaCanvas::Clip {
63public:
64 Clip(const SkRect& rect, SkClipOp op, const SkMatrix& m)
65 : mType(Type::Rect), mOp(op), mMatrix(m), mRRect(SkRRect::MakeRect(rect)) {}
66 Clip(const SkRRect& rrect, SkClipOp op, const SkMatrix& m)
67 : mType(Type::RRect), mOp(op), mMatrix(m), mRRect(rrect) {}
68 Clip(const SkPath& path, SkClipOp op, const SkMatrix& m)
69 : mType(Type::Path), mOp(op), mMatrix(m), mPath(std::in_place, path) {}
70
71 void apply(SkCanvas* canvas) const {
72 canvas->setMatrix(mMatrix);
73 switch (mType) {
74 case Type::Rect:
75 // Don't anti-alias rectangular clips
76 canvas->clipRect(mRRect.rect(), mOp, false);
77 break;
78 case Type::RRect:
79 // Ensure rounded rectangular clips are anti-aliased
80 canvas->clipRRect(mRRect, mOp, true);
81 break;
82 case Type::Path:
83 // Ensure path clips are anti-aliased
84 canvas->clipPath(mPath.value(), mOp, true);
85 break;
86 }
87 }
88
89private:
90 enum class Type {
91 Rect,
92 RRect,
93 Path,
94 };
95
96 Type mType;
97 SkClipOp mOp;
98 SkMatrix mMatrix;
99
100 // These are logically a union (tracked separately due to non-POD path).
101 std::optional<SkPath> mPath;
102 SkRRect mRRect;
103};
104
John Reckc1b33d62015-04-22 09:04:45 -0700105Canvas* Canvas::create_canvas(const SkBitmap& bitmap) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400106 return new SkiaCanvas(bitmap);
107}
108
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400109Canvas* Canvas::create_canvas(SkCanvas* skiaCanvas) {
110 return new SkiaCanvas(skiaCanvas);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400111}
112
Stan Ilievf50806a2016-10-24 10:40:39 -0400113SkiaCanvas::SkiaCanvas() {}
114
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400115SkiaCanvas::SkiaCanvas(SkCanvas* canvas) : mCanvas(canvas) {}
Stan Ilievf50806a2016-10-24 10:40:39 -0400116
John Reckc1b33d62015-04-22 09:04:45 -0700117SkiaCanvas::SkiaCanvas(const SkBitmap& bitmap) {
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400118 mCanvasOwned = std::unique_ptr<SkCanvas>(new SkCanvas(bitmap));
119 mCanvas = mCanvasOwned.get();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400120}
121
Stan Iliev021693b2016-10-17 16:26:15 -0400122SkiaCanvas::~SkiaCanvas() {}
123
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400124void SkiaCanvas::reset(SkCanvas* skiaCanvas) {
Mike Reed6acfe162016-11-18 17:21:09 -0500125 if (mCanvas != skiaCanvas) {
126 mCanvas = skiaCanvas;
127 mCanvasOwned.reset();
128 }
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400129 mSaveStack.reset(nullptr);
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400130}
131
Derek Sollenberger8872b382014-06-23 14:13:53 -0400132// ----------------------------------------------------------------------------
133// Canvas state operations: Replace Bitmap
134// ----------------------------------------------------------------------------
135
John Reckc1b33d62015-04-22 09:04:45 -0700136void SkiaCanvas::setBitmap(const SkBitmap& bitmap) {
Tony Mantler4f641d12017-03-14 22:36:14 +0000137 // deletes the previously owned canvas (if any)
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400138 mCanvasOwned.reset(new SkCanvas(bitmap));
139 mCanvas = mCanvasOwned.get();
Tony Mantler4f641d12017-03-14 22:36:14 +0000140
Derek Sollenberger8872b382014-06-23 14:13:53 -0400141 // clean up the old save stack
Stan Ilievf50806a2016-10-24 10:40:39 -0400142 mSaveStack.reset(nullptr);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400143}
144
145// ----------------------------------------------------------------------------
146// Canvas state operations
147// ----------------------------------------------------------------------------
148
149bool SkiaCanvas::isOpaque() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400150 return mCanvas->imageInfo().isOpaque();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400151}
152
153int SkiaCanvas::width() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400154 return mCanvas->imageInfo().width();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400155}
156
157int SkiaCanvas::height() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400158 return mCanvas->imageInfo().height();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400159}
160
161// ----------------------------------------------------------------------------
162// Canvas state operations: Save (layer)
163// ----------------------------------------------------------------------------
164
165int SkiaCanvas::getSaveCount() const {
166 return mCanvas->getSaveCount();
167}
168
Florin Malitaeecff562015-12-21 10:43:01 -0500169int SkiaCanvas::save(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400170 int count = mCanvas->save();
171 recordPartialSave(flags);
172 return count;
173}
174
Florin Malita5e271402015-11-04 14:36:02 -0500175// The SkiaCanvas::restore operation layers on the capability to preserve
176// either (or both) the matrix and/or clip state after a SkCanvas::restore
177// operation. It does this by explicitly saving off the clip & matrix state
178// when requested and playing it back after the SkCanvas::restore.
Derek Sollenberger8872b382014-06-23 14:13:53 -0400179void SkiaCanvas::restore() {
Kevin Lubickb3731212023-01-05 19:52:09 +0000180 const SaveRec* rec = this->currentSaveRec();
Stan Ilievf50806a2016-10-24 10:40:39 -0400181 if (!rec) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400182 // Fast path - no record for this frame.
183 mCanvas->restore();
184 return;
185 }
186
Florin Malitaeecff562015-12-21 10:43:01 -0500187 bool preserveMatrix = !(rec->saveFlags & SaveFlags::Matrix);
John Reck1bcacfd2017-11-03 10:12:19 -0700188 bool preserveClip = !(rec->saveFlags & SaveFlags::Clip);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400189
190 SkMatrix savedMatrix;
191 if (preserveMatrix) {
192 savedMatrix = mCanvas->getTotalMatrix();
193 }
194
Stan Ilievf50806a2016-10-24 10:40:39 -0400195 const size_t clipIndex = rec->clipIndex;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400196
197 mCanvas->restore();
Stan Ilievf50806a2016-10-24 10:40:39 -0400198 mSaveStack->pop_back();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400199
200 if (preserveMatrix) {
201 mCanvas->setMatrix(savedMatrix);
202 }
203
Stan Ilievf50806a2016-10-24 10:40:39 -0400204 if (preserveClip) {
205 this->applyPersistentClips(clipIndex);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400206 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400207}
208
209void SkiaCanvas::restoreToCount(int restoreCount) {
210 while (mCanvas->getSaveCount() > restoreCount) {
211 this->restore();
212 }
213}
214
John Recka00eef212020-11-16 12:45:55 -0500215int SkiaCanvas::saveLayer(float left, float top, float right, float bottom, const SkPaint* paint) {
Florin Malitaeecff562015-12-21 10:43:01 -0500216 const SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
John Recka00eef212020-11-16 12:45:55 -0500217 const SkCanvas::SaveLayerRec rec(&bounds, paint);
Florin Malitaeecff562015-12-21 10:43:01 -0500218
Stan Iliev68885e32016-12-14 11:18:34 -0500219 return mCanvas->saveLayer(rec);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400220}
221
John Recka00eef212020-11-16 12:45:55 -0500222int SkiaCanvas::saveLayerAlpha(float left, float top, float right, float bottom, int alpha) {
Florin Malitaeecff562015-12-21 10:43:01 -0500223 if (static_cast<unsigned>(alpha) < 0xFF) {
Yuqian Lifd92ee42016-04-27 17:03:38 -0400224 SkPaint alphaPaint;
225 alphaPaint.setAlpha(alpha);
John Recka00eef212020-11-16 12:45:55 -0500226 return this->saveLayer(left, top, right, bottom, &alphaPaint);
Florin Malitaeecff562015-12-21 10:43:01 -0500227 }
John Recka00eef212020-11-16 12:45:55 -0500228 return this->saveLayer(left, top, right, bottom, nullptr);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400229}
230
Derek Sollenberger24fc9012018-12-07 14:12:12 -0500231int SkiaCanvas::saveUnclippedLayer(int left, int top, int right, int bottom) {
232 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
233 return SkAndroidFrameworkUtils::SaveBehind(mCanvas, &bounds);
234}
235
Mike Reeda6cb58a2021-07-10 13:31:34 -0400236void SkiaCanvas::restoreUnclippedLayer(int restoreCount, const Paint& paint) {
Derek Sollenbergerac33a482019-04-22 16:28:09 -0400237
238 while (mCanvas->getSaveCount() > restoreCount + 1) {
239 this->restore();
240 }
241
242 if (mCanvas->getSaveCount() == restoreCount + 1) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500243 SkCanvasPriv::DrawBehind(mCanvas, filterPaint(paint));
Derek Sollenbergerac33a482019-04-22 16:28:09 -0400244 this->restore();
245 }
246}
247
Stan Ilievf50806a2016-10-24 10:40:39 -0400248const SkiaCanvas::SaveRec* SkiaCanvas::currentSaveRec() const {
Kevin Lubickb3731212023-01-05 19:52:09 +0000249 const SaveRec* rec = (mSaveStack && !mSaveStack->empty())
250 ? static_cast<const SaveRec*>(&mSaveStack->back())
251 : nullptr;
Stan Ilievf50806a2016-10-24 10:40:39 -0400252 int currentSaveCount = mCanvas->getSaveCount();
Kevin Lubicka22c1302023-01-18 14:16:44 +0000253 LOG_FATAL_IF(!(!rec || currentSaveCount >= rec->saveCount));
Stan Ilievf50806a2016-10-24 10:40:39 -0400254
255 return (rec && rec->saveCount == currentSaveCount) ? rec : nullptr;
256}
257
Alec Mouri655a5e42022-09-12 17:49:17 +0000258void SkiaCanvas::punchHole(const SkRRect& rect, float alpha) {
Nader Jawad2dc632a2021-03-29 18:51:29 -0700259 SkPaint paint = SkPaint();
Alec Mouri655a5e42022-09-12 17:49:17 +0000260 paint.setColor(SkColors::kBlack);
261 paint.setAlphaf(alpha);
262 paint.setBlendMode(SkBlendMode::kDstOut);
Nader Jawad2dc632a2021-03-29 18:51:29 -0700263 mCanvas->drawRRect(rect, paint);
264}
265
Derek Sollenberger8872b382014-06-23 14:13:53 -0400266// ----------------------------------------------------------------------------
267// functions to emulate legacy SaveFlags (i.e. independent matrix/clip flags)
268// ----------------------------------------------------------------------------
269
Florin Malitaeecff562015-12-21 10:43:01 -0500270void SkiaCanvas::recordPartialSave(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400271 // A partial save is a save operation which doesn't capture the full canvas state.
Florin Malitaeecff562015-12-21 10:43:01 -0500272 // (either SaveFlags::Matrix or SaveFlags::Clip is missing).
Derek Sollenberger8872b382014-06-23 14:13:53 -0400273
274 // Mask-out non canvas state bits.
Florin Malitaeecff562015-12-21 10:43:01 -0500275 flags &= SaveFlags::MatrixClip;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400276
Florin Malitaeecff562015-12-21 10:43:01 -0500277 if (flags == SaveFlags::MatrixClip) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400278 // not a partial save.
279 return;
280 }
281
Stan Ilievf50806a2016-10-24 10:40:39 -0400282 if (!mSaveStack) {
Kevin Lubickb3731212023-01-05 19:52:09 +0000283 mSaveStack.reset(new std::deque<SaveRec>());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400284 }
285
Kevin Lubickb3731212023-01-05 19:52:09 +0000286 mSaveStack->emplace_back(mCanvas->getSaveCount(), // saveCount
287 flags, // saveFlags
288 mClipStack.size()); // clipIndex
Derek Sollenberger8872b382014-06-23 14:13:53 -0400289}
290
Stan Ilievf50806a2016-10-24 10:40:39 -0400291template <typename T>
Mike Reed6e49c9f2016-12-02 15:36:59 -0500292void SkiaCanvas::recordClip(const T& clip, SkClipOp op) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400293 // Only need tracking when in a partial save frame which
294 // doesn't restore the clip.
295 const SaveRec* rec = this->currentSaveRec();
296 if (rec && !(rec->saveFlags & SaveFlags::Clip)) {
297 mClipStack.emplace_back(clip, op, mCanvas->getTotalMatrix());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400298 }
299}
300
Stan Ilievf50806a2016-10-24 10:40:39 -0400301// Applies and optionally removes all clips >= index.
302void SkiaCanvas::applyPersistentClips(size_t clipStartIndex) {
Kevin Lubicka22c1302023-01-18 14:16:44 +0000303 LOG_FATAL_IF(clipStartIndex > mClipStack.size());
Stan Ilievf50806a2016-10-24 10:40:39 -0400304 const auto begin = mClipStack.cbegin() + clipStartIndex;
305 const auto end = mClipStack.cend();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400306
Stan Ilievf50806a2016-10-24 10:40:39 -0400307 // Clip application mutates the CTM.
308 const SkMatrix saveMatrix = mCanvas->getTotalMatrix();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400309
Stan Ilievf50806a2016-10-24 10:40:39 -0400310 for (auto clip = begin; clip != end; ++clip) {
Mike Reed6acfe162016-11-18 17:21:09 -0500311 clip->apply(mCanvas);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400312 }
313
Stan Ilievf50806a2016-10-24 10:40:39 -0400314 mCanvas->setMatrix(saveMatrix);
315
316 // If the current/post-restore save rec is also persisting clips, we
317 // leave them on the stack to be reapplied part of the next restore().
318 // Otherwise we're done and just pop them.
Kevin Lubickb3731212023-01-05 19:52:09 +0000319 const SaveRec* rec = this->currentSaveRec();
Stan Ilievf50806a2016-10-24 10:40:39 -0400320 if (!rec || (rec->saveFlags & SaveFlags::Clip)) {
321 mClipStack.erase(begin, end);
322 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400323}
324
325// ----------------------------------------------------------------------------
326// Canvas state operations: Matrix
327// ----------------------------------------------------------------------------
328
329void SkiaCanvas::getMatrix(SkMatrix* outMatrix) const {
330 *outMatrix = mCanvas->getTotalMatrix();
331}
332
333void SkiaCanvas::setMatrix(const SkMatrix& matrix) {
334 mCanvas->setMatrix(matrix);
335}
336
337void SkiaCanvas::concat(const SkMatrix& matrix) {
338 mCanvas->concat(matrix);
339}
340
341void SkiaCanvas::rotate(float degrees) {
342 mCanvas->rotate(degrees);
343}
344
345void SkiaCanvas::scale(float sx, float sy) {
346 mCanvas->scale(sx, sy);
347}
348
349void SkiaCanvas::skew(float sx, float sy) {
350 mCanvas->skew(sx, sy);
351}
352
353void SkiaCanvas::translate(float dx, float dy) {
354 mCanvas->translate(dx, dy);
355}
356
357// ----------------------------------------------------------------------------
358// Canvas state operations: Clips
359// ----------------------------------------------------------------------------
360
361// This function is a mirror of SkCanvas::getClipBounds except that it does
362// not outset the edge of the clip to account for anti-aliasing. There is
363// a skia bug to investigate pushing this logic into back into skia.
364// (see https://code.google.com/p/skia/issues/detail?id=1303)
365bool SkiaCanvas::getClipBounds(SkRect* outRect) const {
366 SkIRect ibounds;
Mike Reed5e438982017-01-25 08:23:25 -0500367 if (!mCanvas->getDeviceClipBounds(&ibounds)) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400368 return false;
369 }
370
371 SkMatrix inverse;
372 // if we can't invert the CTM, we can't return local clip bounds
373 if (!mCanvas->getTotalMatrix().invert(&inverse)) {
374 if (outRect) {
375 outRect->setEmpty();
376 }
377 return false;
378 }
379
380 if (NULL != outRect) {
381 SkRect r = SkRect::Make(ibounds);
382 inverse.mapRect(outRect, r);
383 }
384 return true;
385}
386
387bool SkiaCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
388 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
389 return mCanvas->quickReject(bounds);
390}
391
392bool SkiaCanvas::quickRejectPath(const SkPath& path) const {
393 return mCanvas->quickReject(path);
394}
395
Mike Reed6e49c9f2016-12-02 15:36:59 -0500396bool SkiaCanvas::clipRect(float left, float top, float right, float bottom, SkClipOp op) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400397 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Stan Ilievf50806a2016-10-24 10:40:39 -0400398 this->recordClip(rect, op);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400399 mCanvas->clipRect(rect, op);
Chris Craik5ec6a282015-06-23 15:42:12 -0700400 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400401}
402
Mike Reed6e49c9f2016-12-02 15:36:59 -0500403bool SkiaCanvas::clipPath(const SkPath* path, SkClipOp op) {
Derek Sollenbergerf7d98f42017-04-17 11:27:36 -0400404 this->recordClip(*path, op);
Nader Jawade431e312019-12-04 14:13:18 -0800405 mCanvas->clipPath(*path, op, true);
Chris Craik5ec6a282015-06-23 15:42:12 -0700406 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400407}
408
Michael Ludwig70cf50c22021-07-21 17:02:39 +0000409bool SkiaCanvas::replaceClipRect_deprecated(float left, float top, float right, float bottom) {
410 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
411
412 // Emulated clip rects are not recorded for partial saves, since
413 // partial saves have been removed from the public API.
414 SkAndroidFrameworkUtils::ResetClip(mCanvas);
415 mCanvas->clipRect(rect, SkClipOp::kIntersect);
416 return !mCanvas->isClipEmpty();
417}
418
419bool SkiaCanvas::replaceClipPath_deprecated(const SkPath* path) {
420 SkAndroidFrameworkUtils::ResetClip(mCanvas);
421 mCanvas->clipPath(*path, SkClipOp::kIntersect, true);
422 return !mCanvas->isClipEmpty();
423}
424
Derek Sollenberger8872b382014-06-23 14:13:53 -0400425// ----------------------------------------------------------------------------
426// Canvas state operations: Filters
427// ----------------------------------------------------------------------------
428
Ben Wagner0ed10be2018-06-28 17:08:16 -0400429PaintFilter* SkiaCanvas::getPaintFilter() {
430 return mPaintFilter.get();
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400431}
432
Ben Wagner0ed10be2018-06-28 17:08:16 -0400433void SkiaCanvas::setPaintFilter(sk_sp<PaintFilter> paintFilter) {
434 mPaintFilter = std::move(paintFilter);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400435}
436
437// ----------------------------------------------------------------------------
Matt Sarettd0814db2017-04-13 09:33:18 -0400438// Canvas state operations: Capture
439// ----------------------------------------------------------------------------
440
441SkCanvasState* SkiaCanvas::captureCanvasState() const {
442 SkCanvas* canvas = mCanvas;
443 if (mCanvasOwned) {
444 // Important to use the underlying SkCanvas, not the wrapper.
445 canvas = mCanvasOwned.get();
446 }
447
448 // Workarounds for http://crbug.com/271096: SW draw only supports
449 // translate & scale transforms, and a simple rectangular clip.
450 // (This also avoids significant wasted time in calling
451 // SkCanvasStateUtils::CaptureCanvasState when the clip is complex).
John Reck1bcacfd2017-11-03 10:12:19 -0700452 if (!canvas->isClipRect() || (canvas->getTotalMatrix().getType() &
453 ~(SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask))) {
454 return nullptr;
Matt Sarettd0814db2017-04-13 09:33:18 -0400455 }
456
457 return SkCanvasStateUtils::CaptureCanvasState(canvas);
458}
459
460// ----------------------------------------------------------------------------
Derek Sollenberger8872b382014-06-23 14:13:53 -0400461// Canvas draw operations
462// ----------------------------------------------------------------------------
463
Mike Reed260ab722016-10-07 15:59:20 -0400464void SkiaCanvas::drawColor(int color, SkBlendMode mode) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400465 mCanvas->drawColor(color, mode);
466}
467
Mike Reeda6cb58a2021-07-10 13:31:34 -0400468void SkiaCanvas::onFilterPaint(Paint& paint) {
Ben Wagner0ed10be2018-06-28 17:08:16 -0400469 if (mPaintFilter) {
Mike Reeda6cb58a2021-07-10 13:31:34 -0400470 mPaintFilter->filterFullPaint(&paint);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400471 }
Ben Wagner0ed10be2018-06-28 17:08:16 -0400472}
473
Mike Reeda6cb58a2021-07-10 13:31:34 -0400474void SkiaCanvas::drawPaint(const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500475 mCanvas->drawPaint(filterPaint(paint));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400476}
477
478// ----------------------------------------------------------------------------
479// Canvas draw operations: Geometry
480// ----------------------------------------------------------------------------
481
Mike Reedc2dbc032019-07-25 12:28:29 -0400482void SkiaCanvas::drawPoints(const float* points, int count, const Paint& paint,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400483 SkCanvas::PointMode mode) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500484 if (CC_UNLIKELY(count < 2 || paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400485 // convert the floats into SkPoints
John Reck1bcacfd2017-11-03 10:12:19 -0700486 count >>= 1; // now it is the number of points
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400487 std::unique_ptr<SkPoint[]> pts(new SkPoint[count]);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400488 for (int i = 0; i < count; i++) {
489 pts[i].set(points[0], points[1]);
490 points += 2;
491 }
Mike Reedc2dbc032019-07-25 12:28:29 -0400492
Mike Reedbb4cb482021-02-22 14:21:20 -0500493 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawPoints(mode, count, pts.get(), p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400494}
495
Mike Reedc2dbc032019-07-25 12:28:29 -0400496void SkiaCanvas::drawPoint(float x, float y, const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500497 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawPoint(x, y, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400498}
499
Mike Reedc2dbc032019-07-25 12:28:29 -0400500void SkiaCanvas::drawPoints(const float* points, int count, const Paint& paint) {
501 this->drawPoints(points, count, paint, SkCanvas::kPoints_PointMode);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400502}
503
504void SkiaCanvas::drawLine(float startX, float startY, float stopX, float stopY,
Mike Reedc2dbc032019-07-25 12:28:29 -0400505 const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500506 applyLooper(&paint,
507 [&](const SkPaint& p) { mCanvas->drawLine(startX, startY, stopX, stopY, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400508}
509
Mike Reedc2dbc032019-07-25 12:28:29 -0400510void SkiaCanvas::drawLines(const float* points, int count, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500511 if (CC_UNLIKELY(count < 4 || paint.nothingToDraw())) return;
Mike Reedc2dbc032019-07-25 12:28:29 -0400512 this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400513}
514
Mike Reedc2dbc032019-07-25 12:28:29 -0400515void SkiaCanvas::drawRect(float left, float top, float right, float bottom, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500516 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Mike Reedbb4cb482021-02-22 14:21:20 -0500517 applyLooper(&paint, [&](const SkPaint& p) {
Mike Reedc2dbc032019-07-25 12:28:29 -0400518 mCanvas->drawRect({left, top, right, bottom}, p);
519 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400520}
521
Mike Reedc2dbc032019-07-25 12:28:29 -0400522void SkiaCanvas::drawRegion(const SkRegion& region, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500523 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Mike Reedbb4cb482021-02-22 14:21:20 -0500524 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawRegion(region, p); });
Derek Sollenberger94394b32015-07-10 09:58:41 -0400525}
526
John Reck1bcacfd2017-11-03 10:12:19 -0700527void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom, float rx, float ry,
Mike Reedc2dbc032019-07-25 12:28:29 -0400528 const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500529 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400530 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Mike Reedbb4cb482021-02-22 14:21:20 -0500531 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawRoundRect(rect, rx, ry, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400532}
533
Nader Jawadadfe1d92018-09-27 12:27:36 -0700534void SkiaCanvas::drawDoubleRoundRect(const SkRRect& outer, const SkRRect& inner,
Mike Reedc2dbc032019-07-25 12:28:29 -0400535 const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500536 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawDRRect(outer, inner, p); });
Nader Jawadadfe1d92018-09-27 12:27:36 -0700537}
538
Mike Reedc2dbc032019-07-25 12:28:29 -0400539void SkiaCanvas::drawCircle(float x, float y, float radius, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500540 if (CC_UNLIKELY(radius <= 0 || paint.nothingToDraw())) return;
Mike Reedbb4cb482021-02-22 14:21:20 -0500541 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawCircle(x, y, radius, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400542}
543
Mike Reedc2dbc032019-07-25 12:28:29 -0400544void SkiaCanvas::drawOval(float left, float top, float right, float bottom, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500545 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400546 SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
Mike Reedbb4cb482021-02-22 14:21:20 -0500547 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawOval(oval, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400548}
549
John Reck1bcacfd2017-11-03 10:12:19 -0700550void SkiaCanvas::drawArc(float left, float top, float right, float bottom, float startAngle,
Mike Reedc2dbc032019-07-25 12:28:29 -0400551 float sweepAngle, bool useCenter, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500552 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400553 SkRect arc = SkRect::MakeLTRB(left, top, right, bottom);
Mike Reedbb4cb482021-02-22 14:21:20 -0500554 applyLooper(&paint, [&](const SkPaint& p) {
Mike Reedc2dbc032019-07-25 12:28:29 -0400555 if (fabs(sweepAngle) >= 360.0f) {
556 mCanvas->drawOval(arc, p);
557 } else {
558 mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, p);
559 }
560 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400561}
562
Mike Reedc2dbc032019-07-25 12:28:29 -0400563void SkiaCanvas::drawPath(const SkPath& path, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500564 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Stan Iliev6dcfdec2017-08-15 16:42:05 -0400565 if (CC_UNLIKELY(path.isEmpty() && (!path.isInverseFillType()))) {
566 return;
567 }
Mike Reedbb4cb482021-02-22 14:21:20 -0500568 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawPath(path, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400569}
570
Mike Reedc2dbc032019-07-25 12:28:29 -0400571void SkiaCanvas::drawVertices(const SkVertices* vertices, SkBlendMode mode, const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500572 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawVertices(vertices, mode, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400573}
574
Angel Aguayo90c46ee2022-11-08 23:42:09 +0000575void SkiaCanvas::drawMesh(const SkMesh& mesh, sk_sp<SkBlender> blender, const SkPaint& paint) {
576 mCanvas->drawMesh(mesh, blender, paint);
577}
578
Derek Sollenberger8872b382014-06-23 14:13:53 -0400579// ----------------------------------------------------------------------------
580// Canvas draw operations: Bitmaps
581// ----------------------------------------------------------------------------
582
Mike Reedc2dbc032019-07-25 12:28:29 -0400583void SkiaCanvas::drawBitmap(Bitmap& bitmap, float left, float top, const Paint* paint) {
584 auto image = bitmap.makeImage();
Mike Reeda6cb58a2021-07-10 13:31:34 -0400585 applyLooper(paint, [&](const Paint& p) {
586 mCanvas->drawImage(image, left, top, p.sampling(), &p);
Mike Reedc2dbc032019-07-25 12:28:29 -0400587 });
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400588}
589
Mike Reedc2dbc032019-07-25 12:28:29 -0400590void SkiaCanvas::drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const Paint* paint) {
591 auto image = bitmap.makeImage();
Mike Reed6acfe162016-11-18 17:21:09 -0500592 SkAutoCanvasRestore acr(mCanvas, true);
Mike Reed70ffbf92014-12-08 17:03:30 -0500593 mCanvas->concat(matrix);
Mike Reeda6cb58a2021-07-10 13:31:34 -0400594 applyLooper(paint, [&](const Paint& p) {
595 mCanvas->drawImage(image, 0, 0, p.sampling(), &p);
Mike Reedc2dbc032019-07-25 12:28:29 -0400596 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400597}
598
John Reck1bcacfd2017-11-03 10:12:19 -0700599void SkiaCanvas::drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight,
600 float srcBottom, float dstLeft, float dstTop, float dstRight,
Mike Reedc2dbc032019-07-25 12:28:29 -0400601 float dstBottom, const Paint* paint) {
602 auto image = bitmap.makeImage();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400603 SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
604 SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400605
Mike Reeda6cb58a2021-07-10 13:31:34 -0400606 applyLooper(paint, [&](const Paint& p) {
607 mCanvas->drawImageRect(image, srcRect, dstRect, p.sampling(), &p,
Mike Reed7994a312021-01-28 18:06:26 -0500608 SkCanvas::kFast_SrcRectConstraint);
Mike Reedc2dbc032019-07-25 12:28:29 -0400609 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400610}
611
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400612void SkiaCanvas::drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight,
Mike Reedc2dbc032019-07-25 12:28:29 -0400613 const float* vertices, const int* colors, const Paint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400614 const int ptCount = (meshWidth + 1) * (meshHeight + 1);
615 const int indexCount = meshWidth * meshHeight * 6;
Mike Reed871cd2d2017-03-17 10:15:52 -0400616 uint32_t flags = SkVertices::kHasTexCoords_BuilderFlag;
617 if (colors) {
618 flags |= SkVertices::kHasColors_BuilderFlag;
619 }
Mike Reed826deef2017-04-04 15:32:04 -0400620 SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, ptCount, indexCount, flags);
Mike Reed871cd2d2017-03-17 10:15:52 -0400621 memcpy(builder.positions(), vertices, ptCount * sizeof(SkPoint));
622 if (colors) {
623 memcpy(builder.colors(), colors, ptCount * sizeof(SkColor));
624 }
625 SkPoint* texs = builder.texCoords();
626 uint16_t* indices = builder.indices();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400627
628 // cons up texture coordinates and indices
629 {
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400630 const SkScalar w = SkIntToScalar(bitmap.width());
631 const SkScalar h = SkIntToScalar(bitmap.height());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400632 const SkScalar dx = w / meshWidth;
633 const SkScalar dy = h / meshHeight;
634
635 SkPoint* texsPtr = texs;
636 SkScalar y = 0;
637 for (int i = 0; i <= meshHeight; i++) {
638 if (i == meshHeight) {
639 y = h; // to ensure numerically we hit h exactly
640 }
641 SkScalar x = 0;
642 for (int j = 0; j < meshWidth; j++) {
643 texsPtr->set(x, y);
644 texsPtr += 1;
645 x += dx;
646 }
647 texsPtr->set(w, y);
648 texsPtr += 1;
649 y += dy;
650 }
Kevin Lubicka22c1302023-01-18 14:16:44 +0000651 LOG_FATAL_IF((texsPtr - texs) != ptCount);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400652 }
653
654 // cons up indices
655 {
656 uint16_t* indexPtr = indices;
657 int index = 0;
658 for (int i = 0; i < meshHeight; i++) {
659 for (int j = 0; j < meshWidth; j++) {
660 // lower-left triangle
661 *indexPtr++ = index;
662 *indexPtr++ = index + meshWidth + 1;
663 *indexPtr++ = index + meshWidth + 2;
664 // upper-right triangle
665 *indexPtr++ = index;
666 *indexPtr++ = index + meshWidth + 2;
667 *indexPtr++ = index + 1;
668 // bump to the next cell
669 index += 1;
670 }
671 // bump to the next row
672 index += 1;
673 }
Kevin Lubicka22c1302023-01-18 14:16:44 +0000674 LOG_FATAL_IF((indexPtr - indices) != indexCount);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400675 }
676
John Reck1bcacfd2017-11-03 10:12:19 -0700677// double-check that we have legal indices
Kevin Lubicka22c1302023-01-18 14:16:44 +0000678#if !defined(NDEBUG)
Derek Sollenberger8872b382014-06-23 14:13:53 -0400679 {
680 for (int i = 0; i < indexCount; i++) {
Kevin Lubicka22c1302023-01-18 14:16:44 +0000681 LOG_FATAL_IF((unsigned)indices[i] >= (unsigned)ptCount);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400682 }
683 }
684#endif
685
Mike Reed7994a312021-01-28 18:06:26 -0500686 auto image = bitmap.makeImage();
687
Derek Sollenberger8872b382014-06-23 14:13:53 -0400688 // cons-up a shader for the bitmap
Mike Reedc2dbc032019-07-25 12:28:29 -0400689 Paint pnt;
690 if (paint) {
691 pnt = *paint;
692 }
Mike Reeda6cb58a2021-07-10 13:31:34 -0400693 SkSamplingOptions sampling = pnt.sampling();
Mike Reed7994a312021-01-28 18:06:26 -0500694 pnt.setShader(image->makeShader(sampling));
695
Mike Reedc2dbc032019-07-25 12:28:29 -0400696 auto v = builder.detach();
Mike Reeda6cb58a2021-07-10 13:31:34 -0400697 applyLooper(&pnt, [&](const Paint& p) {
Mike Reed7994a312021-01-28 18:06:26 -0500698 SkPaint copy(p);
Mike Reeda6cb58a2021-07-10 13:31:34 -0400699 auto s = p.sampling();
Mike Reed7994a312021-01-28 18:06:26 -0500700 if (s != sampling) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500701 // applyLooper changed the quality?
Mike Reed7994a312021-01-28 18:06:26 -0500702 copy.setShader(image->makeShader(s));
703 }
704 mCanvas->drawVertices(v, SkBlendMode::kModulate, copy);
Mike Reedc2dbc032019-07-25 12:28:29 -0400705 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400706}
707
John Reck1bcacfd2017-11-03 10:12:19 -0700708void SkiaCanvas::drawNinePatch(Bitmap& bitmap, const Res_png_9patch& chunk, float dstLeft,
709 float dstTop, float dstRight, float dstBottom,
Mike Reedc2dbc032019-07-25 12:28:29 -0400710 const Paint* paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400711 SkCanvas::Lattice lattice;
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400712 NinePatchUtils::SetLatticeDivs(&lattice, chunk, bitmap.width(), bitmap.height());
Stan Ilievf50806a2016-10-24 10:40:39 -0400713
Stan Ilieve12d7312017-12-04 14:48:27 -0500714 lattice.fRectTypes = nullptr;
715 lattice.fColors = nullptr;
Stan Ilievf50806a2016-10-24 10:40:39 -0400716 int numFlags = 0;
Stan Iliev021693b2016-10-17 16:26:15 -0400717 if (chunk.numColors > 0 && chunk.numColors == NinePatchUtils::NumDistinctRects(lattice)) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400718 // We can expect the framework to give us a color for every distinct rect.
719 // Skia requires a flag for every rect.
720 numFlags = (lattice.fXCount + 1) * (lattice.fYCount + 1);
721 }
722
Kevin Lubicka22c1302023-01-18 14:16:44 +0000723 // Most times, we do not have very many flags/colors, so the stack allocated part of
724 // FatVector will save us a heap allocation.
725 FatVector<SkCanvas::Lattice::RectType, 25> flags(numFlags);
726 FatVector<SkColor, 25> colors(numFlags);
Stan Ilievf50806a2016-10-24 10:40:39 -0400727 if (numFlags > 0) {
Kevin Lubicka22c1302023-01-18 14:16:44 +0000728 NinePatchUtils::SetLatticeFlags(&lattice, flags.data(), numFlags, chunk, colors.data());
Stan Ilievf50806a2016-10-24 10:40:39 -0400729 }
730
731 lattice.fBounds = nullptr;
732 SkRect dst = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Mike Reedc2dbc032019-07-25 12:28:29 -0400733 auto image = bitmap.makeImage();
Mike Reeda6cb58a2021-07-10 13:31:34 -0400734 applyLooper(paint, [&](const Paint& p) {
735 mCanvas->drawImageLattice(image.get(), lattice, dst, p.filterMode(), &p);
Mike Reedc2dbc032019-07-25 12:28:29 -0400736 });
Derek Sollenbergeredca3202015-07-10 13:56:39 -0400737}
738
Derek Sollenberger2d142132018-01-22 10:25:26 -0500739double SkiaCanvas::drawAnimatedImage(AnimatedImageDrawable* imgDrawable) {
740 return imgDrawable->drawStaging(mCanvas);
Leon Scroggins III671cce22018-01-14 16:52:17 -0500741}
742
Jorge Betancourt2e58b5c2022-09-01 09:57:09 -0400743void SkiaCanvas::drawLottie(LottieDrawable* lottieDrawable) {
744 lottieDrawable->drawStaging(mCanvas);
745}
746
Doris Liu766431a2016-02-04 22:17:11 +0000747void SkiaCanvas::drawVectorDrawable(VectorDrawableRoot* vectorDrawable) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800748 vectorDrawable->drawStaging(this);
Doris Liu766431a2016-02-04 22:17:11 +0000749}
750
Derek Sollenberger8872b382014-06-23 14:13:53 -0400751// ----------------------------------------------------------------------------
752// Canvas draw operations: Text
753// ----------------------------------------------------------------------------
754
Mike Reed2dfd55d2019-01-08 16:19:03 -0500755void SkiaCanvas::drawGlyphs(ReadGlyphFunc glyphFunc, int count, const Paint& paint, float x,
Seigo Nonaka63af7ba2020-09-21 23:07:43 -0700756 float y, float totalAdvance) {
Stan Iliev0b58d992017-03-30 18:22:27 -0400757 if (count <= 0 || paint.nothingToDraw()) return;
Mike Reedf6d86ac2019-01-18 14:13:23 -0500758 Paint paintCopy(paint);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400759 if (mPaintFilter) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500760 mPaintFilter->filterFullPaint(&paintCopy);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400761 }
Mike Reedf6d86ac2019-01-18 14:13:23 -0500762 const SkFont& font = paintCopy.getSkFont();
Stan Iliev7717e222018-02-05 18:04:11 -0500763 // Stroke with a hairline is drawn on HW with a fill style for compatibility with Android O and
764 // older.
John Recke170fb62018-05-07 08:12:07 -0700765 if (!mCanvasOwned && sApiLevel <= 27 && paintCopy.getStrokeWidth() <= 0 &&
766 paintCopy.getStyle() == SkPaint::kStroke_Style) {
Stan Iliev7717e222018-02-05 18:04:11 -0500767 paintCopy.setStyle(SkPaint::kFill_Style);
768 }
Stan Ilievf50806a2016-10-24 10:40:39 -0400769
Stan Ilievf50806a2016-10-24 10:40:39 -0400770 SkTextBlobBuilder builder;
Mike Reed2e204fc2019-01-28 13:31:36 -0500771 const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(font, count);
Stan Iliev0b58d992017-03-30 18:22:27 -0400772 glyphFunc(buffer.glyphs, buffer.pos);
Stan Ilievf50806a2016-10-24 10:40:39 -0400773
774 sk_sp<SkTextBlob> textBlob(builder.make());
Nathaniel Nifong52d37772020-01-10 16:12:41 -0500775
Mike Reedbb4cb482021-02-22 14:21:20 -0500776 applyLooper(&paintCopy, [&](const SkPaint& p) { mCanvas->drawTextBlob(textBlob, 0, 0, p); });
Stan Ilievf50806a2016-10-24 10:40:39 -0400777 drawTextDecorations(x, y, totalAdvance, paintCopy);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400778}
779
Yuqian Liafc221492016-07-18 13:07:42 -0400780void SkiaCanvas::drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
Mike Reed2dfd55d2019-01-08 16:19:03 -0500781 const Paint& paint, const SkPath& path, size_t start,
John Reck1bcacfd2017-11-03 10:12:19 -0700782 size_t end) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500783 Paint paintCopy(paint);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400784 if (mPaintFilter) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500785 mPaintFilter->filterFullPaint(&paintCopy);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400786 }
Mike Reedf6d86ac2019-01-18 14:13:23 -0500787 const SkFont& font = paintCopy.getSkFont();
Derek Sollenberger415a74b2018-03-14 15:03:13 -0400788
Yuqian Liafc221492016-07-18 13:07:42 -0400789 const int N = end - start;
Mike Reed4a4b1be2019-01-01 15:43:06 -0500790 SkTextBlobBuilder builder;
791 auto rec = builder.allocRunRSXform(font, N);
792 SkRSXform* xform = (SkRSXform*)rec.pos;
793 uint16_t* glyphs = rec.glyphs;
Yuqian Liafc221492016-07-18 13:07:42 -0400794 SkPathMeasure meas(path, false);
795
796 for (size_t i = start; i < end; i++) {
797 glyphs[i - start] = layout.getGlyphId(i);
Stan Ilievc6c96dd2018-01-10 16:06:04 -0500798 float halfWidth = layout.getCharAdvance(i) * 0.5f;
799 float x = hOffset + layout.getX(i) + halfWidth;
Yuqian Liafc221492016-07-18 13:07:42 -0400800 float y = vOffset + layout.getY(i);
801
802 SkPoint pos;
803 SkVector tan;
804 if (!meas.getPosTan(x, &pos, &tan)) {
805 pos.set(x, y);
806 tan.set(1, 0);
807 }
808 xform[i - start].fSCos = tan.x();
809 xform[i - start].fSSin = tan.y();
Stan Ilievc6c96dd2018-01-10 16:06:04 -0500810 xform[i - start].fTx = pos.x() - tan.y() * y - halfWidth * tan.x();
811 xform[i - start].fTy = pos.y() + tan.x() * y - halfWidth * tan.y();
Yuqian Liafc221492016-07-18 13:07:42 -0400812 }
Leon Scroggins III950f2aa2020-04-29 13:46:00 -0400813
814 sk_sp<SkTextBlob> textBlob(builder.make());
815
Mike Reedbb4cb482021-02-22 14:21:20 -0500816 applyLooper(&paintCopy, [&](const SkPaint& p) { mCanvas->drawTextBlob(textBlob, 0, 0, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400817}
818
Derek Sollenberger6f485562015-07-30 10:00:39 -0400819// ----------------------------------------------------------------------------
820// Canvas draw operations: Animations
821// ----------------------------------------------------------------------------
822
Derek Sollenberger6f485562015-07-30 10:00:39 -0400823void SkiaCanvas::drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
John Reck1bcacfd2017-11-03 10:12:19 -0700824 uirenderer::CanvasPropertyPrimitive* top,
825 uirenderer::CanvasPropertyPrimitive* right,
826 uirenderer::CanvasPropertyPrimitive* bottom,
827 uirenderer::CanvasPropertyPrimitive* rx,
828 uirenderer::CanvasPropertyPrimitive* ry,
829 uirenderer::CanvasPropertyPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -0400830 sk_sp<uirenderer::skiapipeline::AnimatedRoundRect> drawable(
John Reck1bcacfd2017-11-03 10:12:19 -0700831 new uirenderer::skiapipeline::AnimatedRoundRect(left, top, right, bottom, rx, ry,
832 paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400833 mCanvas->drawDrawable(drawable.get());
834}
835
John Reck1bcacfd2017-11-03 10:12:19 -0700836void SkiaCanvas::drawCircle(uirenderer::CanvasPropertyPrimitive* x,
837 uirenderer::CanvasPropertyPrimitive* y,
838 uirenderer::CanvasPropertyPrimitive* radius,
839 uirenderer::CanvasPropertyPaint* paint) {
840 sk_sp<uirenderer::skiapipeline::AnimatedCircle> drawable(
841 new uirenderer::skiapipeline::AnimatedCircle(x, y, radius, paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400842 mCanvas->drawDrawable(drawable.get());
843}
844
Lucas Dupin00af5272021-04-29 20:30:01 -0700845void SkiaCanvas::drawRipple(const uirenderer::skiapipeline::RippleDrawableParams& params) {
846 uirenderer::skiapipeline::AnimatedRippleDrawable::draw(mCanvas, params);
Derek Sollenbergerdf301aa2020-12-17 11:06:03 -0500847}
848
John Reck894e85a2019-07-15 16:16:44 -0700849void SkiaCanvas::drawPicture(const SkPicture& picture) {
850 // TODO: Change to mCanvas->drawPicture()? SkCanvas::drawPicture seems to be
851 // where the logic is for playback vs. ref picture. Using picture.playback here
852 // to stay behavior-identical for now, but should revisit this at some point.
853 picture.playback(mCanvas);
854}
855
Derek Sollenberger6f485562015-07-30 10:00:39 -0400856// ----------------------------------------------------------------------------
857// Canvas draw operations: View System
858// ----------------------------------------------------------------------------
859
Stan Ilievf50806a2016-10-24 10:40:39 -0400860void SkiaCanvas::drawLayer(uirenderer::DeferredLayerUpdater* layerUpdater) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400861 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw Layers");
862}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400863
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400864void SkiaCanvas::drawRenderNode(uirenderer::RenderNode* renderNode) {
865 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw RenderNodes");
866}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400867
John Reck1bcacfd2017-11-03 10:12:19 -0700868} // namespace android