blob: 7a1276982d0a66a8be477fdb0e505774d4398726 [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>
Nader Jawad5f0a8002023-02-21 17:00:51 -080042#include <log/log.h>
43#include <ui/FatVector.h>
Derek Sollenberger8872b382014-06-23 14:13:53 -040044
Ben Wagner60126ef2015-08-07 12:13:48 -040045#include <memory>
Ben Wagner0ed10be2018-06-28 17:08:16 -040046#include <optional>
47#include <utility>
Ben Wagner60126ef2015-08-07 12:13:48 -040048
Kevin Lubickb3731212023-01-05 19:52:09 +000049#include "CanvasProperty.h"
Nader Jawad5f0a8002023-02-21 17:00:51 -080050#include "Mesh.h"
Kevin Lubickb3731212023-01-05 19:52:09 +000051#include "NinePatchUtils.h"
Kevin Lubickb3731212023-01-05 19:52:09 +000052#include "VectorDrawable.h"
53#include "hwui/Bitmap.h"
54#include "hwui/MinikinUtils.h"
55#include "hwui/PaintFilter.h"
56#include "pipeline/skia/AnimatedDrawables.h"
57#include "pipeline/skia/HolePunch.h"
58
Derek Sollenberger8872b382014-06-23 14:13:53 -040059namespace android {
60
Stan Ilievf50806a2016-10-24 10:40:39 -040061using uirenderer::PaintUtils;
62
Ryan Prichard39971552022-08-26 20:53:33 -070063class SkiaCanvas::Clip {
64public:
65 Clip(const SkRect& rect, SkClipOp op, const SkMatrix& m)
66 : mType(Type::Rect), mOp(op), mMatrix(m), mRRect(SkRRect::MakeRect(rect)) {}
67 Clip(const SkRRect& rrect, SkClipOp op, const SkMatrix& m)
68 : mType(Type::RRect), mOp(op), mMatrix(m), mRRect(rrect) {}
69 Clip(const SkPath& path, SkClipOp op, const SkMatrix& m)
70 : mType(Type::Path), mOp(op), mMatrix(m), mPath(std::in_place, path) {}
71
72 void apply(SkCanvas* canvas) const {
73 canvas->setMatrix(mMatrix);
74 switch (mType) {
75 case Type::Rect:
76 // Don't anti-alias rectangular clips
77 canvas->clipRect(mRRect.rect(), mOp, false);
78 break;
79 case Type::RRect:
80 // Ensure rounded rectangular clips are anti-aliased
81 canvas->clipRRect(mRRect, mOp, true);
82 break;
83 case Type::Path:
84 // Ensure path clips are anti-aliased
85 canvas->clipPath(mPath.value(), mOp, true);
86 break;
87 }
88 }
89
90private:
91 enum class Type {
92 Rect,
93 RRect,
94 Path,
95 };
96
97 Type mType;
98 SkClipOp mOp;
99 SkMatrix mMatrix;
100
101 // These are logically a union (tracked separately due to non-POD path).
102 std::optional<SkPath> mPath;
103 SkRRect mRRect;
104};
105
John Reckc1b33d62015-04-22 09:04:45 -0700106Canvas* Canvas::create_canvas(const SkBitmap& bitmap) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400107 return new SkiaCanvas(bitmap);
108}
109
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400110Canvas* Canvas::create_canvas(SkCanvas* skiaCanvas) {
111 return new SkiaCanvas(skiaCanvas);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400112}
113
Stan Ilievf50806a2016-10-24 10:40:39 -0400114SkiaCanvas::SkiaCanvas() {}
115
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400116SkiaCanvas::SkiaCanvas(SkCanvas* canvas) : mCanvas(canvas) {}
Stan Ilievf50806a2016-10-24 10:40:39 -0400117
John Reckc1b33d62015-04-22 09:04:45 -0700118SkiaCanvas::SkiaCanvas(const SkBitmap& bitmap) {
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400119 mCanvasOwned = std::unique_ptr<SkCanvas>(new SkCanvas(bitmap));
120 mCanvas = mCanvasOwned.get();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400121}
122
Stan Iliev021693b2016-10-17 16:26:15 -0400123SkiaCanvas::~SkiaCanvas() {}
124
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400125void SkiaCanvas::reset(SkCanvas* skiaCanvas) {
Mike Reed6acfe162016-11-18 17:21:09 -0500126 if (mCanvas != skiaCanvas) {
127 mCanvas = skiaCanvas;
128 mCanvasOwned.reset();
129 }
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400130 mSaveStack.reset(nullptr);
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400131}
132
Derek Sollenberger8872b382014-06-23 14:13:53 -0400133// ----------------------------------------------------------------------------
134// Canvas state operations: Replace Bitmap
135// ----------------------------------------------------------------------------
136
John Reckc1b33d62015-04-22 09:04:45 -0700137void SkiaCanvas::setBitmap(const SkBitmap& bitmap) {
Tony Mantler4f641d12017-03-14 22:36:14 +0000138 // deletes the previously owned canvas (if any)
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400139 mCanvasOwned.reset(new SkCanvas(bitmap));
140 mCanvas = mCanvasOwned.get();
Tony Mantler4f641d12017-03-14 22:36:14 +0000141
Derek Sollenberger8872b382014-06-23 14:13:53 -0400142 // clean up the old save stack
Stan Ilievf50806a2016-10-24 10:40:39 -0400143 mSaveStack.reset(nullptr);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400144}
145
146// ----------------------------------------------------------------------------
147// Canvas state operations
148// ----------------------------------------------------------------------------
149
150bool SkiaCanvas::isOpaque() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400151 return mCanvas->imageInfo().isOpaque();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400152}
153
154int SkiaCanvas::width() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400155 return mCanvas->imageInfo().width();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400156}
157
158int SkiaCanvas::height() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400159 return mCanvas->imageInfo().height();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400160}
161
162// ----------------------------------------------------------------------------
163// Canvas state operations: Save (layer)
164// ----------------------------------------------------------------------------
165
166int SkiaCanvas::getSaveCount() const {
167 return mCanvas->getSaveCount();
168}
169
Florin Malitaeecff562015-12-21 10:43:01 -0500170int SkiaCanvas::save(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400171 int count = mCanvas->save();
172 recordPartialSave(flags);
173 return count;
174}
175
Florin Malita5e271402015-11-04 14:36:02 -0500176// The SkiaCanvas::restore operation layers on the capability to preserve
177// either (or both) the matrix and/or clip state after a SkCanvas::restore
178// operation. It does this by explicitly saving off the clip & matrix state
179// when requested and playing it back after the SkCanvas::restore.
Derek Sollenberger8872b382014-06-23 14:13:53 -0400180void SkiaCanvas::restore() {
Kevin Lubickb3731212023-01-05 19:52:09 +0000181 const SaveRec* rec = this->currentSaveRec();
Stan Ilievf50806a2016-10-24 10:40:39 -0400182 if (!rec) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400183 // Fast path - no record for this frame.
184 mCanvas->restore();
185 return;
186 }
187
Florin Malitaeecff562015-12-21 10:43:01 -0500188 bool preserveMatrix = !(rec->saveFlags & SaveFlags::Matrix);
John Reck1bcacfd2017-11-03 10:12:19 -0700189 bool preserveClip = !(rec->saveFlags & SaveFlags::Clip);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400190
191 SkMatrix savedMatrix;
192 if (preserveMatrix) {
193 savedMatrix = mCanvas->getTotalMatrix();
194 }
195
Stan Ilievf50806a2016-10-24 10:40:39 -0400196 const size_t clipIndex = rec->clipIndex;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400197
198 mCanvas->restore();
Stan Ilievf50806a2016-10-24 10:40:39 -0400199 mSaveStack->pop_back();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400200
201 if (preserveMatrix) {
202 mCanvas->setMatrix(savedMatrix);
203 }
204
Stan Ilievf50806a2016-10-24 10:40:39 -0400205 if (preserveClip) {
206 this->applyPersistentClips(clipIndex);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400207 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400208}
209
210void SkiaCanvas::restoreToCount(int restoreCount) {
211 while (mCanvas->getSaveCount() > restoreCount) {
212 this->restore();
213 }
214}
215
John Recka00eef212020-11-16 12:45:55 -0500216int SkiaCanvas::saveLayer(float left, float top, float right, float bottom, const SkPaint* paint) {
Florin Malitaeecff562015-12-21 10:43:01 -0500217 const SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
John Recka00eef212020-11-16 12:45:55 -0500218 const SkCanvas::SaveLayerRec rec(&bounds, paint);
Florin Malitaeecff562015-12-21 10:43:01 -0500219
Stan Iliev68885e32016-12-14 11:18:34 -0500220 return mCanvas->saveLayer(rec);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400221}
222
John Recka00eef212020-11-16 12:45:55 -0500223int SkiaCanvas::saveLayerAlpha(float left, float top, float right, float bottom, int alpha) {
Florin Malitaeecff562015-12-21 10:43:01 -0500224 if (static_cast<unsigned>(alpha) < 0xFF) {
Yuqian Lifd92ee42016-04-27 17:03:38 -0400225 SkPaint alphaPaint;
226 alphaPaint.setAlpha(alpha);
John Recka00eef212020-11-16 12:45:55 -0500227 return this->saveLayer(left, top, right, bottom, &alphaPaint);
Florin Malitaeecff562015-12-21 10:43:01 -0500228 }
John Recka00eef212020-11-16 12:45:55 -0500229 return this->saveLayer(left, top, right, bottom, nullptr);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400230}
231
Derek Sollenberger24fc9012018-12-07 14:12:12 -0500232int SkiaCanvas::saveUnclippedLayer(int left, int top, int right, int bottom) {
233 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
234 return SkAndroidFrameworkUtils::SaveBehind(mCanvas, &bounds);
235}
236
Mike Reeda6cb58a2021-07-10 13:31:34 -0400237void SkiaCanvas::restoreUnclippedLayer(int restoreCount, const Paint& paint) {
Derek Sollenbergerac33a482019-04-22 16:28:09 -0400238
239 while (mCanvas->getSaveCount() > restoreCount + 1) {
240 this->restore();
241 }
242
243 if (mCanvas->getSaveCount() == restoreCount + 1) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500244 SkCanvasPriv::DrawBehind(mCanvas, filterPaint(paint));
Derek Sollenbergerac33a482019-04-22 16:28:09 -0400245 this->restore();
246 }
247}
248
Stan Ilievf50806a2016-10-24 10:40:39 -0400249const SkiaCanvas::SaveRec* SkiaCanvas::currentSaveRec() const {
Kevin Lubickb3731212023-01-05 19:52:09 +0000250 const SaveRec* rec = (mSaveStack && !mSaveStack->empty())
251 ? static_cast<const SaveRec*>(&mSaveStack->back())
252 : nullptr;
Stan Ilievf50806a2016-10-24 10:40:39 -0400253 int currentSaveCount = mCanvas->getSaveCount();
Kevin Lubicka22c1302023-01-18 14:16:44 +0000254 LOG_FATAL_IF(!(!rec || currentSaveCount >= rec->saveCount));
Stan Ilievf50806a2016-10-24 10:40:39 -0400255
256 return (rec && rec->saveCount == currentSaveCount) ? rec : nullptr;
257}
258
Alec Mouri655a5e42022-09-12 17:49:17 +0000259void SkiaCanvas::punchHole(const SkRRect& rect, float alpha) {
Nader Jawad2dc632a2021-03-29 18:51:29 -0700260 SkPaint paint = SkPaint();
Alec Mouri655a5e42022-09-12 17:49:17 +0000261 paint.setColor(SkColors::kBlack);
262 paint.setAlphaf(alpha);
263 paint.setBlendMode(SkBlendMode::kDstOut);
Nader Jawad2dc632a2021-03-29 18:51:29 -0700264 mCanvas->drawRRect(rect, paint);
265}
266
Derek Sollenberger8872b382014-06-23 14:13:53 -0400267// ----------------------------------------------------------------------------
268// functions to emulate legacy SaveFlags (i.e. independent matrix/clip flags)
269// ----------------------------------------------------------------------------
270
Florin Malitaeecff562015-12-21 10:43:01 -0500271void SkiaCanvas::recordPartialSave(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400272 // A partial save is a save operation which doesn't capture the full canvas state.
Florin Malitaeecff562015-12-21 10:43:01 -0500273 // (either SaveFlags::Matrix or SaveFlags::Clip is missing).
Derek Sollenberger8872b382014-06-23 14:13:53 -0400274
275 // Mask-out non canvas state bits.
Florin Malitaeecff562015-12-21 10:43:01 -0500276 flags &= SaveFlags::MatrixClip;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400277
Florin Malitaeecff562015-12-21 10:43:01 -0500278 if (flags == SaveFlags::MatrixClip) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400279 // not a partial save.
280 return;
281 }
282
Stan Ilievf50806a2016-10-24 10:40:39 -0400283 if (!mSaveStack) {
Kevin Lubickb3731212023-01-05 19:52:09 +0000284 mSaveStack.reset(new std::deque<SaveRec>());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400285 }
286
Kevin Lubickb3731212023-01-05 19:52:09 +0000287 mSaveStack->emplace_back(mCanvas->getSaveCount(), // saveCount
288 flags, // saveFlags
289 mClipStack.size()); // clipIndex
Derek Sollenberger8872b382014-06-23 14:13:53 -0400290}
291
Stan Ilievf50806a2016-10-24 10:40:39 -0400292template <typename T>
Mike Reed6e49c9f2016-12-02 15:36:59 -0500293void SkiaCanvas::recordClip(const T& clip, SkClipOp op) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400294 // Only need tracking when in a partial save frame which
295 // doesn't restore the clip.
296 const SaveRec* rec = this->currentSaveRec();
297 if (rec && !(rec->saveFlags & SaveFlags::Clip)) {
298 mClipStack.emplace_back(clip, op, mCanvas->getTotalMatrix());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400299 }
300}
301
Stan Ilievf50806a2016-10-24 10:40:39 -0400302// Applies and optionally removes all clips >= index.
303void SkiaCanvas::applyPersistentClips(size_t clipStartIndex) {
Kevin Lubicka22c1302023-01-18 14:16:44 +0000304 LOG_FATAL_IF(clipStartIndex > mClipStack.size());
Stan Ilievf50806a2016-10-24 10:40:39 -0400305 const auto begin = mClipStack.cbegin() + clipStartIndex;
306 const auto end = mClipStack.cend();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400307
Stan Ilievf50806a2016-10-24 10:40:39 -0400308 // Clip application mutates the CTM.
309 const SkMatrix saveMatrix = mCanvas->getTotalMatrix();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400310
Stan Ilievf50806a2016-10-24 10:40:39 -0400311 for (auto clip = begin; clip != end; ++clip) {
Mike Reed6acfe162016-11-18 17:21:09 -0500312 clip->apply(mCanvas);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400313 }
314
Stan Ilievf50806a2016-10-24 10:40:39 -0400315 mCanvas->setMatrix(saveMatrix);
316
317 // If the current/post-restore save rec is also persisting clips, we
318 // leave them on the stack to be reapplied part of the next restore().
319 // Otherwise we're done and just pop them.
Kevin Lubickb3731212023-01-05 19:52:09 +0000320 const SaveRec* rec = this->currentSaveRec();
Stan Ilievf50806a2016-10-24 10:40:39 -0400321 if (!rec || (rec->saveFlags & SaveFlags::Clip)) {
322 mClipStack.erase(begin, end);
323 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400324}
325
326// ----------------------------------------------------------------------------
327// Canvas state operations: Matrix
328// ----------------------------------------------------------------------------
329
330void SkiaCanvas::getMatrix(SkMatrix* outMatrix) const {
331 *outMatrix = mCanvas->getTotalMatrix();
332}
333
334void SkiaCanvas::setMatrix(const SkMatrix& matrix) {
335 mCanvas->setMatrix(matrix);
336}
337
338void SkiaCanvas::concat(const SkMatrix& matrix) {
339 mCanvas->concat(matrix);
340}
341
342void SkiaCanvas::rotate(float degrees) {
343 mCanvas->rotate(degrees);
344}
345
346void SkiaCanvas::scale(float sx, float sy) {
347 mCanvas->scale(sx, sy);
348}
349
350void SkiaCanvas::skew(float sx, float sy) {
351 mCanvas->skew(sx, sy);
352}
353
354void SkiaCanvas::translate(float dx, float dy) {
355 mCanvas->translate(dx, dy);
356}
357
358// ----------------------------------------------------------------------------
359// Canvas state operations: Clips
360// ----------------------------------------------------------------------------
361
362// This function is a mirror of SkCanvas::getClipBounds except that it does
363// not outset the edge of the clip to account for anti-aliasing. There is
364// a skia bug to investigate pushing this logic into back into skia.
365// (see https://code.google.com/p/skia/issues/detail?id=1303)
366bool SkiaCanvas::getClipBounds(SkRect* outRect) const {
367 SkIRect ibounds;
Mike Reed5e438982017-01-25 08:23:25 -0500368 if (!mCanvas->getDeviceClipBounds(&ibounds)) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400369 return false;
370 }
371
372 SkMatrix inverse;
373 // if we can't invert the CTM, we can't return local clip bounds
374 if (!mCanvas->getTotalMatrix().invert(&inverse)) {
375 if (outRect) {
376 outRect->setEmpty();
377 }
378 return false;
379 }
380
381 if (NULL != outRect) {
382 SkRect r = SkRect::Make(ibounds);
383 inverse.mapRect(outRect, r);
384 }
385 return true;
386}
387
388bool SkiaCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
389 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
390 return mCanvas->quickReject(bounds);
391}
392
393bool SkiaCanvas::quickRejectPath(const SkPath& path) const {
394 return mCanvas->quickReject(path);
395}
396
Mike Reed6e49c9f2016-12-02 15:36:59 -0500397bool SkiaCanvas::clipRect(float left, float top, float right, float bottom, SkClipOp op) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400398 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Stan Ilievf50806a2016-10-24 10:40:39 -0400399 this->recordClip(rect, op);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400400 mCanvas->clipRect(rect, op);
Chris Craik5ec6a282015-06-23 15:42:12 -0700401 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400402}
403
Mike Reed6e49c9f2016-12-02 15:36:59 -0500404bool SkiaCanvas::clipPath(const SkPath* path, SkClipOp op) {
Derek Sollenbergerf7d98f42017-04-17 11:27:36 -0400405 this->recordClip(*path, op);
Nader Jawade431e312019-12-04 14:13:18 -0800406 mCanvas->clipPath(*path, op, true);
Chris Craik5ec6a282015-06-23 15:42:12 -0700407 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400408}
409
Michael Ludwig70cf50c22021-07-21 17:02:39 +0000410bool SkiaCanvas::replaceClipRect_deprecated(float left, float top, float right, float bottom) {
411 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
412
413 // Emulated clip rects are not recorded for partial saves, since
414 // partial saves have been removed from the public API.
415 SkAndroidFrameworkUtils::ResetClip(mCanvas);
416 mCanvas->clipRect(rect, SkClipOp::kIntersect);
417 return !mCanvas->isClipEmpty();
418}
419
420bool SkiaCanvas::replaceClipPath_deprecated(const SkPath* path) {
421 SkAndroidFrameworkUtils::ResetClip(mCanvas);
422 mCanvas->clipPath(*path, SkClipOp::kIntersect, true);
423 return !mCanvas->isClipEmpty();
424}
425
Derek Sollenberger8872b382014-06-23 14:13:53 -0400426// ----------------------------------------------------------------------------
427// Canvas state operations: Filters
428// ----------------------------------------------------------------------------
429
Ben Wagner0ed10be2018-06-28 17:08:16 -0400430PaintFilter* SkiaCanvas::getPaintFilter() {
431 return mPaintFilter.get();
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400432}
433
Ben Wagner0ed10be2018-06-28 17:08:16 -0400434void SkiaCanvas::setPaintFilter(sk_sp<PaintFilter> paintFilter) {
435 mPaintFilter = std::move(paintFilter);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400436}
437
438// ----------------------------------------------------------------------------
Matt Sarettd0814db2017-04-13 09:33:18 -0400439// Canvas state operations: Capture
440// ----------------------------------------------------------------------------
441
442SkCanvasState* SkiaCanvas::captureCanvasState() const {
443 SkCanvas* canvas = mCanvas;
444 if (mCanvasOwned) {
445 // Important to use the underlying SkCanvas, not the wrapper.
446 canvas = mCanvasOwned.get();
447 }
448
449 // Workarounds for http://crbug.com/271096: SW draw only supports
450 // translate & scale transforms, and a simple rectangular clip.
451 // (This also avoids significant wasted time in calling
452 // SkCanvasStateUtils::CaptureCanvasState when the clip is complex).
John Reck1bcacfd2017-11-03 10:12:19 -0700453 if (!canvas->isClipRect() || (canvas->getTotalMatrix().getType() &
454 ~(SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask))) {
455 return nullptr;
Matt Sarettd0814db2017-04-13 09:33:18 -0400456 }
457
458 return SkCanvasStateUtils::CaptureCanvasState(canvas);
459}
460
461// ----------------------------------------------------------------------------
Derek Sollenberger8872b382014-06-23 14:13:53 -0400462// Canvas draw operations
463// ----------------------------------------------------------------------------
464
Mike Reed260ab722016-10-07 15:59:20 -0400465void SkiaCanvas::drawColor(int color, SkBlendMode mode) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400466 mCanvas->drawColor(color, mode);
467}
468
Mike Reeda6cb58a2021-07-10 13:31:34 -0400469void SkiaCanvas::onFilterPaint(Paint& paint) {
Ben Wagner0ed10be2018-06-28 17:08:16 -0400470 if (mPaintFilter) {
Mike Reeda6cb58a2021-07-10 13:31:34 -0400471 mPaintFilter->filterFullPaint(&paint);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400472 }
Ben Wagner0ed10be2018-06-28 17:08:16 -0400473}
474
Mike Reeda6cb58a2021-07-10 13:31:34 -0400475void SkiaCanvas::drawPaint(const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500476 mCanvas->drawPaint(filterPaint(paint));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400477}
478
479// ----------------------------------------------------------------------------
480// Canvas draw operations: Geometry
481// ----------------------------------------------------------------------------
482
Mike Reedc2dbc032019-07-25 12:28:29 -0400483void SkiaCanvas::drawPoints(const float* points, int count, const Paint& paint,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400484 SkCanvas::PointMode mode) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500485 if (CC_UNLIKELY(count < 2 || paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400486 // convert the floats into SkPoints
John Reck1bcacfd2017-11-03 10:12:19 -0700487 count >>= 1; // now it is the number of points
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400488 std::unique_ptr<SkPoint[]> pts(new SkPoint[count]);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400489 for (int i = 0; i < count; i++) {
490 pts[i].set(points[0], points[1]);
491 points += 2;
492 }
Mike Reedc2dbc032019-07-25 12:28:29 -0400493
Mike Reedbb4cb482021-02-22 14:21:20 -0500494 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawPoints(mode, count, pts.get(), p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400495}
496
Mike Reedc2dbc032019-07-25 12:28:29 -0400497void SkiaCanvas::drawPoint(float x, float y, const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500498 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawPoint(x, y, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400499}
500
Mike Reedc2dbc032019-07-25 12:28:29 -0400501void SkiaCanvas::drawPoints(const float* points, int count, const Paint& paint) {
502 this->drawPoints(points, count, paint, SkCanvas::kPoints_PointMode);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400503}
504
505void SkiaCanvas::drawLine(float startX, float startY, float stopX, float stopY,
Mike Reedc2dbc032019-07-25 12:28:29 -0400506 const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500507 applyLooper(&paint,
508 [&](const SkPaint& p) { mCanvas->drawLine(startX, startY, stopX, stopY, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400509}
510
Mike Reedc2dbc032019-07-25 12:28:29 -0400511void SkiaCanvas::drawLines(const float* points, int count, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500512 if (CC_UNLIKELY(count < 4 || paint.nothingToDraw())) return;
Mike Reedc2dbc032019-07-25 12:28:29 -0400513 this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400514}
515
Mike Reedc2dbc032019-07-25 12:28:29 -0400516void SkiaCanvas::drawRect(float left, float top, float right, float bottom, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500517 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Mike Reedbb4cb482021-02-22 14:21:20 -0500518 applyLooper(&paint, [&](const SkPaint& p) {
Mike Reedc2dbc032019-07-25 12:28:29 -0400519 mCanvas->drawRect({left, top, right, bottom}, p);
520 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400521}
522
Mike Reedc2dbc032019-07-25 12:28:29 -0400523void SkiaCanvas::drawRegion(const SkRegion& region, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500524 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Mike Reedbb4cb482021-02-22 14:21:20 -0500525 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawRegion(region, p); });
Derek Sollenberger94394b32015-07-10 09:58:41 -0400526}
527
John Reck1bcacfd2017-11-03 10:12:19 -0700528void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom, float rx, float ry,
Mike Reedc2dbc032019-07-25 12:28:29 -0400529 const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500530 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400531 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Mike Reedbb4cb482021-02-22 14:21:20 -0500532 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawRoundRect(rect, rx, ry, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400533}
534
Nader Jawadadfe1d92018-09-27 12:27:36 -0700535void SkiaCanvas::drawDoubleRoundRect(const SkRRect& outer, const SkRRect& inner,
Mike Reedc2dbc032019-07-25 12:28:29 -0400536 const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500537 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawDRRect(outer, inner, p); });
Nader Jawadadfe1d92018-09-27 12:27:36 -0700538}
539
Mike Reedc2dbc032019-07-25 12:28:29 -0400540void SkiaCanvas::drawCircle(float x, float y, float radius, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500541 if (CC_UNLIKELY(radius <= 0 || paint.nothingToDraw())) return;
Mike Reedbb4cb482021-02-22 14:21:20 -0500542 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawCircle(x, y, radius, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400543}
544
Mike Reedc2dbc032019-07-25 12:28:29 -0400545void SkiaCanvas::drawOval(float left, float top, float right, float bottom, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500546 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400547 SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
Mike Reedbb4cb482021-02-22 14:21:20 -0500548 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawOval(oval, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400549}
550
John Reck1bcacfd2017-11-03 10:12:19 -0700551void SkiaCanvas::drawArc(float left, float top, float right, float bottom, float startAngle,
Mike Reedc2dbc032019-07-25 12:28:29 -0400552 float sweepAngle, bool useCenter, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500553 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400554 SkRect arc = SkRect::MakeLTRB(left, top, right, bottom);
Mike Reedbb4cb482021-02-22 14:21:20 -0500555 applyLooper(&paint, [&](const SkPaint& p) {
Mike Reedc2dbc032019-07-25 12:28:29 -0400556 if (fabs(sweepAngle) >= 360.0f) {
557 mCanvas->drawOval(arc, p);
558 } else {
559 mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, p);
560 }
561 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400562}
563
Mike Reedc2dbc032019-07-25 12:28:29 -0400564void SkiaCanvas::drawPath(const SkPath& path, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500565 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Stan Iliev6dcfdec2017-08-15 16:42:05 -0400566 if (CC_UNLIKELY(path.isEmpty() && (!path.isInverseFillType()))) {
567 return;
568 }
Mike Reedbb4cb482021-02-22 14:21:20 -0500569 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawPath(path, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400570}
571
Mike Reedc2dbc032019-07-25 12:28:29 -0400572void SkiaCanvas::drawVertices(const SkVertices* vertices, SkBlendMode mode, const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500573 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawVertices(vertices, mode, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400574}
575
Nader Jawad5f0a8002023-02-21 17:00:51 -0800576void SkiaCanvas::drawMesh(const Mesh& mesh, sk_sp<SkBlender> blender, const Paint& paint) {
577 GrDirectContext* context = nullptr;
578 auto recordingContext = mCanvas->recordingContext();
579 if (recordingContext) {
580 context = recordingContext->asDirectContext();
581 }
582 mesh.updateSkMesh(context);
583 mCanvas->drawMesh(mesh.getSkMesh(), blender, paint);
Angel Aguayo90c46ee2022-11-08 23:42:09 +0000584}
585
Derek Sollenberger8872b382014-06-23 14:13:53 -0400586// ----------------------------------------------------------------------------
587// Canvas draw operations: Bitmaps
588// ----------------------------------------------------------------------------
589
Mike Reedc2dbc032019-07-25 12:28:29 -0400590void SkiaCanvas::drawBitmap(Bitmap& bitmap, float left, float top, const Paint* paint) {
591 auto image = bitmap.makeImage();
Mike Reeda6cb58a2021-07-10 13:31:34 -0400592 applyLooper(paint, [&](const Paint& p) {
593 mCanvas->drawImage(image, left, top, p.sampling(), &p);
Mike Reedc2dbc032019-07-25 12:28:29 -0400594 });
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400595}
596
Mike Reedc2dbc032019-07-25 12:28:29 -0400597void SkiaCanvas::drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const Paint* paint) {
598 auto image = bitmap.makeImage();
Mike Reed6acfe162016-11-18 17:21:09 -0500599 SkAutoCanvasRestore acr(mCanvas, true);
Mike Reed70ffbf92014-12-08 17:03:30 -0500600 mCanvas->concat(matrix);
Mike Reeda6cb58a2021-07-10 13:31:34 -0400601 applyLooper(paint, [&](const Paint& p) {
602 mCanvas->drawImage(image, 0, 0, p.sampling(), &p);
Mike Reedc2dbc032019-07-25 12:28:29 -0400603 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400604}
605
John Reck1bcacfd2017-11-03 10:12:19 -0700606void SkiaCanvas::drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight,
607 float srcBottom, float dstLeft, float dstTop, float dstRight,
Mike Reedc2dbc032019-07-25 12:28:29 -0400608 float dstBottom, const Paint* paint) {
609 auto image = bitmap.makeImage();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400610 SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
611 SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400612
Mike Reeda6cb58a2021-07-10 13:31:34 -0400613 applyLooper(paint, [&](const Paint& p) {
614 mCanvas->drawImageRect(image, srcRect, dstRect, p.sampling(), &p,
Mike Reed7994a312021-01-28 18:06:26 -0500615 SkCanvas::kFast_SrcRectConstraint);
Mike Reedc2dbc032019-07-25 12:28:29 -0400616 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400617}
618
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400619void SkiaCanvas::drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight,
Mike Reedc2dbc032019-07-25 12:28:29 -0400620 const float* vertices, const int* colors, const Paint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400621 const int ptCount = (meshWidth + 1) * (meshHeight + 1);
622 const int indexCount = meshWidth * meshHeight * 6;
Mike Reed871cd2d2017-03-17 10:15:52 -0400623 uint32_t flags = SkVertices::kHasTexCoords_BuilderFlag;
624 if (colors) {
625 flags |= SkVertices::kHasColors_BuilderFlag;
626 }
Mike Reed826deef2017-04-04 15:32:04 -0400627 SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, ptCount, indexCount, flags);
Mike Reed871cd2d2017-03-17 10:15:52 -0400628 memcpy(builder.positions(), vertices, ptCount * sizeof(SkPoint));
629 if (colors) {
630 memcpy(builder.colors(), colors, ptCount * sizeof(SkColor));
631 }
632 SkPoint* texs = builder.texCoords();
633 uint16_t* indices = builder.indices();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400634
635 // cons up texture coordinates and indices
636 {
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400637 const SkScalar w = SkIntToScalar(bitmap.width());
638 const SkScalar h = SkIntToScalar(bitmap.height());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400639 const SkScalar dx = w / meshWidth;
640 const SkScalar dy = h / meshHeight;
641
642 SkPoint* texsPtr = texs;
643 SkScalar y = 0;
644 for (int i = 0; i <= meshHeight; i++) {
645 if (i == meshHeight) {
646 y = h; // to ensure numerically we hit h exactly
647 }
648 SkScalar x = 0;
649 for (int j = 0; j < meshWidth; j++) {
650 texsPtr->set(x, y);
651 texsPtr += 1;
652 x += dx;
653 }
654 texsPtr->set(w, y);
655 texsPtr += 1;
656 y += dy;
657 }
Kevin Lubicka22c1302023-01-18 14:16:44 +0000658 LOG_FATAL_IF((texsPtr - texs) != ptCount);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400659 }
660
661 // cons up indices
662 {
663 uint16_t* indexPtr = indices;
664 int index = 0;
665 for (int i = 0; i < meshHeight; i++) {
666 for (int j = 0; j < meshWidth; j++) {
667 // lower-left triangle
668 *indexPtr++ = index;
669 *indexPtr++ = index + meshWidth + 1;
670 *indexPtr++ = index + meshWidth + 2;
671 // upper-right triangle
672 *indexPtr++ = index;
673 *indexPtr++ = index + meshWidth + 2;
674 *indexPtr++ = index + 1;
675 // bump to the next cell
676 index += 1;
677 }
678 // bump to the next row
679 index += 1;
680 }
Kevin Lubicka22c1302023-01-18 14:16:44 +0000681 LOG_FATAL_IF((indexPtr - indices) != indexCount);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400682 }
683
John Reck1bcacfd2017-11-03 10:12:19 -0700684// double-check that we have legal indices
Kevin Lubicka22c1302023-01-18 14:16:44 +0000685#if !defined(NDEBUG)
Derek Sollenberger8872b382014-06-23 14:13:53 -0400686 {
687 for (int i = 0; i < indexCount; i++) {
Kevin Lubicka22c1302023-01-18 14:16:44 +0000688 LOG_FATAL_IF((unsigned)indices[i] >= (unsigned)ptCount);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400689 }
690 }
691#endif
692
Mike Reed7994a312021-01-28 18:06:26 -0500693 auto image = bitmap.makeImage();
694
Derek Sollenberger8872b382014-06-23 14:13:53 -0400695 // cons-up a shader for the bitmap
Mike Reedc2dbc032019-07-25 12:28:29 -0400696 Paint pnt;
697 if (paint) {
698 pnt = *paint;
699 }
Mike Reeda6cb58a2021-07-10 13:31:34 -0400700 SkSamplingOptions sampling = pnt.sampling();
Mike Reed7994a312021-01-28 18:06:26 -0500701 pnt.setShader(image->makeShader(sampling));
702
Mike Reedc2dbc032019-07-25 12:28:29 -0400703 auto v = builder.detach();
Mike Reeda6cb58a2021-07-10 13:31:34 -0400704 applyLooper(&pnt, [&](const Paint& p) {
Mike Reed7994a312021-01-28 18:06:26 -0500705 SkPaint copy(p);
Mike Reeda6cb58a2021-07-10 13:31:34 -0400706 auto s = p.sampling();
Mike Reed7994a312021-01-28 18:06:26 -0500707 if (s != sampling) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500708 // applyLooper changed the quality?
Mike Reed7994a312021-01-28 18:06:26 -0500709 copy.setShader(image->makeShader(s));
710 }
711 mCanvas->drawVertices(v, SkBlendMode::kModulate, copy);
Mike Reedc2dbc032019-07-25 12:28:29 -0400712 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400713}
714
John Reck1bcacfd2017-11-03 10:12:19 -0700715void SkiaCanvas::drawNinePatch(Bitmap& bitmap, const Res_png_9patch& chunk, float dstLeft,
716 float dstTop, float dstRight, float dstBottom,
Mike Reedc2dbc032019-07-25 12:28:29 -0400717 const Paint* paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400718 SkCanvas::Lattice lattice;
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400719 NinePatchUtils::SetLatticeDivs(&lattice, chunk, bitmap.width(), bitmap.height());
Stan Ilievf50806a2016-10-24 10:40:39 -0400720
Stan Ilieve12d7312017-12-04 14:48:27 -0500721 lattice.fRectTypes = nullptr;
722 lattice.fColors = nullptr;
Stan Ilievf50806a2016-10-24 10:40:39 -0400723 int numFlags = 0;
Stan Iliev021693b2016-10-17 16:26:15 -0400724 if (chunk.numColors > 0 && chunk.numColors == NinePatchUtils::NumDistinctRects(lattice)) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400725 // We can expect the framework to give us a color for every distinct rect.
726 // Skia requires a flag for every rect.
727 numFlags = (lattice.fXCount + 1) * (lattice.fYCount + 1);
728 }
729
Kevin Lubicka22c1302023-01-18 14:16:44 +0000730 // Most times, we do not have very many flags/colors, so the stack allocated part of
731 // FatVector will save us a heap allocation.
732 FatVector<SkCanvas::Lattice::RectType, 25> flags(numFlags);
733 FatVector<SkColor, 25> colors(numFlags);
Stan Ilievf50806a2016-10-24 10:40:39 -0400734 if (numFlags > 0) {
Kevin Lubicka22c1302023-01-18 14:16:44 +0000735 NinePatchUtils::SetLatticeFlags(&lattice, flags.data(), numFlags, chunk, colors.data());
Stan Ilievf50806a2016-10-24 10:40:39 -0400736 }
737
738 lattice.fBounds = nullptr;
739 SkRect dst = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Mike Reedc2dbc032019-07-25 12:28:29 -0400740 auto image = bitmap.makeImage();
Mike Reeda6cb58a2021-07-10 13:31:34 -0400741 applyLooper(paint, [&](const Paint& p) {
742 mCanvas->drawImageLattice(image.get(), lattice, dst, p.filterMode(), &p);
Mike Reedc2dbc032019-07-25 12:28:29 -0400743 });
Derek Sollenbergeredca3202015-07-10 13:56:39 -0400744}
745
Derek Sollenberger2d142132018-01-22 10:25:26 -0500746double SkiaCanvas::drawAnimatedImage(AnimatedImageDrawable* imgDrawable) {
747 return imgDrawable->drawStaging(mCanvas);
Leon Scroggins III671cce22018-01-14 16:52:17 -0500748}
749
Doris Liu766431a2016-02-04 22:17:11 +0000750void SkiaCanvas::drawVectorDrawable(VectorDrawableRoot* vectorDrawable) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800751 vectorDrawable->drawStaging(this);
Doris Liu766431a2016-02-04 22:17:11 +0000752}
753
Derek Sollenberger8872b382014-06-23 14:13:53 -0400754// ----------------------------------------------------------------------------
755// Canvas draw operations: Text
756// ----------------------------------------------------------------------------
757
Mike Reed2dfd55d2019-01-08 16:19:03 -0500758void SkiaCanvas::drawGlyphs(ReadGlyphFunc glyphFunc, int count, const Paint& paint, float x,
Seigo Nonaka63af7ba2020-09-21 23:07:43 -0700759 float y, float totalAdvance) {
Stan Iliev0b58d992017-03-30 18:22:27 -0400760 if (count <= 0 || paint.nothingToDraw()) return;
Mike Reedf6d86ac2019-01-18 14:13:23 -0500761 Paint paintCopy(paint);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400762 if (mPaintFilter) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500763 mPaintFilter->filterFullPaint(&paintCopy);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400764 }
Mike Reedf6d86ac2019-01-18 14:13:23 -0500765 const SkFont& font = paintCopy.getSkFont();
Stan Iliev7717e222018-02-05 18:04:11 -0500766 // Stroke with a hairline is drawn on HW with a fill style for compatibility with Android O and
767 // older.
John Recke170fb62018-05-07 08:12:07 -0700768 if (!mCanvasOwned && sApiLevel <= 27 && paintCopy.getStrokeWidth() <= 0 &&
769 paintCopy.getStyle() == SkPaint::kStroke_Style) {
Stan Iliev7717e222018-02-05 18:04:11 -0500770 paintCopy.setStyle(SkPaint::kFill_Style);
771 }
Stan Ilievf50806a2016-10-24 10:40:39 -0400772
Stan Ilievf50806a2016-10-24 10:40:39 -0400773 SkTextBlobBuilder builder;
Mike Reed2e204fc2019-01-28 13:31:36 -0500774 const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(font, count);
Stan Iliev0b58d992017-03-30 18:22:27 -0400775 glyphFunc(buffer.glyphs, buffer.pos);
Stan Ilievf50806a2016-10-24 10:40:39 -0400776
777 sk_sp<SkTextBlob> textBlob(builder.make());
Nathaniel Nifong52d37772020-01-10 16:12:41 -0500778
Mike Reedbb4cb482021-02-22 14:21:20 -0500779 applyLooper(&paintCopy, [&](const SkPaint& p) { mCanvas->drawTextBlob(textBlob, 0, 0, p); });
Stan Ilievf50806a2016-10-24 10:40:39 -0400780 drawTextDecorations(x, y, totalAdvance, paintCopy);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400781}
782
Yuqian Liafc221492016-07-18 13:07:42 -0400783void SkiaCanvas::drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
Mike Reed2dfd55d2019-01-08 16:19:03 -0500784 const Paint& paint, const SkPath& path, size_t start,
John Reck1bcacfd2017-11-03 10:12:19 -0700785 size_t end) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500786 Paint paintCopy(paint);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400787 if (mPaintFilter) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500788 mPaintFilter->filterFullPaint(&paintCopy);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400789 }
Mike Reedf6d86ac2019-01-18 14:13:23 -0500790 const SkFont& font = paintCopy.getSkFont();
Derek Sollenberger415a74b2018-03-14 15:03:13 -0400791
Yuqian Liafc221492016-07-18 13:07:42 -0400792 const int N = end - start;
Mike Reed4a4b1be2019-01-01 15:43:06 -0500793 SkTextBlobBuilder builder;
794 auto rec = builder.allocRunRSXform(font, N);
795 SkRSXform* xform = (SkRSXform*)rec.pos;
796 uint16_t* glyphs = rec.glyphs;
Yuqian Liafc221492016-07-18 13:07:42 -0400797 SkPathMeasure meas(path, false);
798
799 for (size_t i = start; i < end; i++) {
800 glyphs[i - start] = layout.getGlyphId(i);
Stan Ilievc6c96dd2018-01-10 16:06:04 -0500801 float halfWidth = layout.getCharAdvance(i) * 0.5f;
802 float x = hOffset + layout.getX(i) + halfWidth;
Yuqian Liafc221492016-07-18 13:07:42 -0400803 float y = vOffset + layout.getY(i);
804
805 SkPoint pos;
806 SkVector tan;
807 if (!meas.getPosTan(x, &pos, &tan)) {
808 pos.set(x, y);
809 tan.set(1, 0);
810 }
811 xform[i - start].fSCos = tan.x();
812 xform[i - start].fSSin = tan.y();
Stan Ilievc6c96dd2018-01-10 16:06:04 -0500813 xform[i - start].fTx = pos.x() - tan.y() * y - halfWidth * tan.x();
814 xform[i - start].fTy = pos.y() + tan.x() * y - halfWidth * tan.y();
Yuqian Liafc221492016-07-18 13:07:42 -0400815 }
Leon Scroggins III950f2aa2020-04-29 13:46:00 -0400816
817 sk_sp<SkTextBlob> textBlob(builder.make());
818
Mike Reedbb4cb482021-02-22 14:21:20 -0500819 applyLooper(&paintCopy, [&](const SkPaint& p) { mCanvas->drawTextBlob(textBlob, 0, 0, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400820}
821
Derek Sollenberger6f485562015-07-30 10:00:39 -0400822// ----------------------------------------------------------------------------
823// Canvas draw operations: Animations
824// ----------------------------------------------------------------------------
825
Derek Sollenberger6f485562015-07-30 10:00:39 -0400826void SkiaCanvas::drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
John Reck1bcacfd2017-11-03 10:12:19 -0700827 uirenderer::CanvasPropertyPrimitive* top,
828 uirenderer::CanvasPropertyPrimitive* right,
829 uirenderer::CanvasPropertyPrimitive* bottom,
830 uirenderer::CanvasPropertyPrimitive* rx,
831 uirenderer::CanvasPropertyPrimitive* ry,
832 uirenderer::CanvasPropertyPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -0400833 sk_sp<uirenderer::skiapipeline::AnimatedRoundRect> drawable(
John Reck1bcacfd2017-11-03 10:12:19 -0700834 new uirenderer::skiapipeline::AnimatedRoundRect(left, top, right, bottom, rx, ry,
835 paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400836 mCanvas->drawDrawable(drawable.get());
837}
838
John Reck1bcacfd2017-11-03 10:12:19 -0700839void SkiaCanvas::drawCircle(uirenderer::CanvasPropertyPrimitive* x,
840 uirenderer::CanvasPropertyPrimitive* y,
841 uirenderer::CanvasPropertyPrimitive* radius,
842 uirenderer::CanvasPropertyPaint* paint) {
843 sk_sp<uirenderer::skiapipeline::AnimatedCircle> drawable(
844 new uirenderer::skiapipeline::AnimatedCircle(x, y, radius, paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400845 mCanvas->drawDrawable(drawable.get());
846}
847
Lucas Dupin00af5272021-04-29 20:30:01 -0700848void SkiaCanvas::drawRipple(const uirenderer::skiapipeline::RippleDrawableParams& params) {
849 uirenderer::skiapipeline::AnimatedRippleDrawable::draw(mCanvas, params);
Derek Sollenbergerdf301aa2020-12-17 11:06:03 -0500850}
851
John Reck894e85a2019-07-15 16:16:44 -0700852void SkiaCanvas::drawPicture(const SkPicture& picture) {
853 // TODO: Change to mCanvas->drawPicture()? SkCanvas::drawPicture seems to be
854 // where the logic is for playback vs. ref picture. Using picture.playback here
855 // to stay behavior-identical for now, but should revisit this at some point.
856 picture.playback(mCanvas);
857}
858
Derek Sollenberger6f485562015-07-30 10:00:39 -0400859// ----------------------------------------------------------------------------
860// Canvas draw operations: View System
861// ----------------------------------------------------------------------------
862
Stan Ilievf50806a2016-10-24 10:40:39 -0400863void SkiaCanvas::drawLayer(uirenderer::DeferredLayerUpdater* layerUpdater) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400864 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw Layers");
865}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400866
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400867void SkiaCanvas::drawRenderNode(uirenderer::RenderNode* renderNode) {
868 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw RenderNodes");
869}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400870
John Reck1bcacfd2017-11-03 10:12:19 -0700871} // namespace android