blob: 14b8d8d0aa1282eacfa25fa1f5ebdf918d9d24ed [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"
Seigo Nonakacd348c62023-09-12 16:05:44 +090050#include "FeatureFlags.h"
Nader Jawad5f0a8002023-02-21 17:00:51 -080051#include "Mesh.h"
Kevin Lubickb3731212023-01-05 19:52:09 +000052#include "NinePatchUtils.h"
Kevin Lubickb3731212023-01-05 19:52:09 +000053#include "VectorDrawable.h"
John Reckb2a4b932023-03-29 17:01:25 -040054#include "effects/GainmapRenderer.h"
Kevin Lubickb3731212023-01-05 19:52:09 +000055#include "hwui/Bitmap.h"
56#include "hwui/MinikinUtils.h"
57#include "hwui/PaintFilter.h"
58#include "pipeline/skia/AnimatedDrawables.h"
59#include "pipeline/skia/HolePunch.h"
60
Derek Sollenberger8872b382014-06-23 14:13:53 -040061namespace android {
62
Stan Ilievf50806a2016-10-24 10:40:39 -040063using uirenderer::PaintUtils;
64
Ryan Prichard39971552022-08-26 20:53:33 -070065class SkiaCanvas::Clip {
66public:
67 Clip(const SkRect& rect, SkClipOp op, const SkMatrix& m)
68 : mType(Type::Rect), mOp(op), mMatrix(m), mRRect(SkRRect::MakeRect(rect)) {}
69 Clip(const SkRRect& rrect, SkClipOp op, const SkMatrix& m)
70 : mType(Type::RRect), mOp(op), mMatrix(m), mRRect(rrect) {}
71 Clip(const SkPath& path, SkClipOp op, const SkMatrix& m)
72 : mType(Type::Path), mOp(op), mMatrix(m), mPath(std::in_place, path) {}
73
74 void apply(SkCanvas* canvas) const {
75 canvas->setMatrix(mMatrix);
76 switch (mType) {
77 case Type::Rect:
78 // Don't anti-alias rectangular clips
79 canvas->clipRect(mRRect.rect(), mOp, false);
80 break;
81 case Type::RRect:
82 // Ensure rounded rectangular clips are anti-aliased
83 canvas->clipRRect(mRRect, mOp, true);
84 break;
85 case Type::Path:
86 // Ensure path clips are anti-aliased
87 canvas->clipPath(mPath.value(), mOp, true);
88 break;
89 }
90 }
91
92private:
93 enum class Type {
94 Rect,
95 RRect,
96 Path,
97 };
98
99 Type mType;
100 SkClipOp mOp;
101 SkMatrix mMatrix;
102
103 // These are logically a union (tracked separately due to non-POD path).
104 std::optional<SkPath> mPath;
105 SkRRect mRRect;
106};
107
John Reckc1b33d62015-04-22 09:04:45 -0700108Canvas* Canvas::create_canvas(const SkBitmap& bitmap) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400109 return new SkiaCanvas(bitmap);
110}
111
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400112Canvas* Canvas::create_canvas(SkCanvas* skiaCanvas) {
113 return new SkiaCanvas(skiaCanvas);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400114}
115
Stan Ilievf50806a2016-10-24 10:40:39 -0400116SkiaCanvas::SkiaCanvas() {}
117
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400118SkiaCanvas::SkiaCanvas(SkCanvas* canvas) : mCanvas(canvas) {}
Stan Ilievf50806a2016-10-24 10:40:39 -0400119
John Reckc1b33d62015-04-22 09:04:45 -0700120SkiaCanvas::SkiaCanvas(const SkBitmap& bitmap) {
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400121 mCanvasOwned = std::unique_ptr<SkCanvas>(new SkCanvas(bitmap));
122 mCanvas = mCanvasOwned.get();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400123}
124
Stan Iliev021693b2016-10-17 16:26:15 -0400125SkiaCanvas::~SkiaCanvas() {}
126
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400127void SkiaCanvas::reset(SkCanvas* skiaCanvas) {
Mike Reed6acfe162016-11-18 17:21:09 -0500128 if (mCanvas != skiaCanvas) {
129 mCanvas = skiaCanvas;
130 mCanvasOwned.reset();
131 }
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400132 mSaveStack.reset(nullptr);
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400133}
134
Derek Sollenberger8872b382014-06-23 14:13:53 -0400135// ----------------------------------------------------------------------------
136// Canvas state operations: Replace Bitmap
137// ----------------------------------------------------------------------------
138
John Reckc1b33d62015-04-22 09:04:45 -0700139void SkiaCanvas::setBitmap(const SkBitmap& bitmap) {
Tony Mantler4f641d12017-03-14 22:36:14 +0000140 // deletes the previously owned canvas (if any)
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400141 mCanvasOwned.reset(new SkCanvas(bitmap));
142 mCanvas = mCanvasOwned.get();
Tony Mantler4f641d12017-03-14 22:36:14 +0000143
Derek Sollenberger8872b382014-06-23 14:13:53 -0400144 // clean up the old save stack
Stan Ilievf50806a2016-10-24 10:40:39 -0400145 mSaveStack.reset(nullptr);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400146}
147
148// ----------------------------------------------------------------------------
149// Canvas state operations
150// ----------------------------------------------------------------------------
151
152bool SkiaCanvas::isOpaque() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400153 return mCanvas->imageInfo().isOpaque();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400154}
155
156int SkiaCanvas::width() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400157 return mCanvas->imageInfo().width();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400158}
159
160int SkiaCanvas::height() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400161 return mCanvas->imageInfo().height();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400162}
163
164// ----------------------------------------------------------------------------
165// Canvas state operations: Save (layer)
166// ----------------------------------------------------------------------------
167
168int SkiaCanvas::getSaveCount() const {
169 return mCanvas->getSaveCount();
170}
171
Florin Malitaeecff562015-12-21 10:43:01 -0500172int SkiaCanvas::save(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400173 int count = mCanvas->save();
174 recordPartialSave(flags);
175 return count;
176}
177
Florin Malita5e271402015-11-04 14:36:02 -0500178// The SkiaCanvas::restore operation layers on the capability to preserve
179// either (or both) the matrix and/or clip state after a SkCanvas::restore
180// operation. It does this by explicitly saving off the clip & matrix state
181// when requested and playing it back after the SkCanvas::restore.
Derek Sollenberger8872b382014-06-23 14:13:53 -0400182void SkiaCanvas::restore() {
Kevin Lubickb3731212023-01-05 19:52:09 +0000183 const SaveRec* rec = this->currentSaveRec();
Stan Ilievf50806a2016-10-24 10:40:39 -0400184 if (!rec) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400185 // Fast path - no record for this frame.
186 mCanvas->restore();
187 return;
188 }
189
Florin Malitaeecff562015-12-21 10:43:01 -0500190 bool preserveMatrix = !(rec->saveFlags & SaveFlags::Matrix);
John Reck1bcacfd2017-11-03 10:12:19 -0700191 bool preserveClip = !(rec->saveFlags & SaveFlags::Clip);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400192
193 SkMatrix savedMatrix;
194 if (preserveMatrix) {
195 savedMatrix = mCanvas->getTotalMatrix();
196 }
197
Stan Ilievf50806a2016-10-24 10:40:39 -0400198 const size_t clipIndex = rec->clipIndex;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400199
200 mCanvas->restore();
Stan Ilievf50806a2016-10-24 10:40:39 -0400201 mSaveStack->pop_back();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400202
203 if (preserveMatrix) {
204 mCanvas->setMatrix(savedMatrix);
205 }
206
Stan Ilievf50806a2016-10-24 10:40:39 -0400207 if (preserveClip) {
208 this->applyPersistentClips(clipIndex);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400209 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400210}
211
212void SkiaCanvas::restoreToCount(int restoreCount) {
213 while (mCanvas->getSaveCount() > restoreCount) {
214 this->restore();
215 }
216}
217
John Recka00eef212020-11-16 12:45:55 -0500218int SkiaCanvas::saveLayer(float left, float top, float right, float bottom, const SkPaint* paint) {
Florin Malitaeecff562015-12-21 10:43:01 -0500219 const SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
John Recka00eef212020-11-16 12:45:55 -0500220 const SkCanvas::SaveLayerRec rec(&bounds, paint);
Florin Malitaeecff562015-12-21 10:43:01 -0500221
Stan Iliev68885e32016-12-14 11:18:34 -0500222 return mCanvas->saveLayer(rec);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400223}
224
John Recka00eef212020-11-16 12:45:55 -0500225int SkiaCanvas::saveLayerAlpha(float left, float top, float right, float bottom, int alpha) {
Florin Malitaeecff562015-12-21 10:43:01 -0500226 if (static_cast<unsigned>(alpha) < 0xFF) {
Yuqian Lifd92ee42016-04-27 17:03:38 -0400227 SkPaint alphaPaint;
228 alphaPaint.setAlpha(alpha);
John Recka00eef212020-11-16 12:45:55 -0500229 return this->saveLayer(left, top, right, bottom, &alphaPaint);
Florin Malitaeecff562015-12-21 10:43:01 -0500230 }
John Recka00eef212020-11-16 12:45:55 -0500231 return this->saveLayer(left, top, right, bottom, nullptr);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400232}
233
Derek Sollenberger24fc9012018-12-07 14:12:12 -0500234int SkiaCanvas::saveUnclippedLayer(int left, int top, int right, int bottom) {
235 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
236 return SkAndroidFrameworkUtils::SaveBehind(mCanvas, &bounds);
237}
238
Mike Reeda6cb58a2021-07-10 13:31:34 -0400239void SkiaCanvas::restoreUnclippedLayer(int restoreCount, const Paint& paint) {
Derek Sollenbergerac33a482019-04-22 16:28:09 -0400240
241 while (mCanvas->getSaveCount() > restoreCount + 1) {
242 this->restore();
243 }
244
245 if (mCanvas->getSaveCount() == restoreCount + 1) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500246 SkCanvasPriv::DrawBehind(mCanvas, filterPaint(paint));
Derek Sollenbergerac33a482019-04-22 16:28:09 -0400247 this->restore();
248 }
249}
250
Stan Ilievf50806a2016-10-24 10:40:39 -0400251const SkiaCanvas::SaveRec* SkiaCanvas::currentSaveRec() const {
Kevin Lubickb3731212023-01-05 19:52:09 +0000252 const SaveRec* rec = (mSaveStack && !mSaveStack->empty())
253 ? static_cast<const SaveRec*>(&mSaveStack->back())
254 : nullptr;
Stan Ilievf50806a2016-10-24 10:40:39 -0400255 int currentSaveCount = mCanvas->getSaveCount();
Kevin Lubicka22c1302023-01-18 14:16:44 +0000256 LOG_FATAL_IF(!(!rec || currentSaveCount >= rec->saveCount));
Stan Ilievf50806a2016-10-24 10:40:39 -0400257
258 return (rec && rec->saveCount == currentSaveCount) ? rec : nullptr;
259}
260
Alec Mouri655a5e42022-09-12 17:49:17 +0000261void SkiaCanvas::punchHole(const SkRRect& rect, float alpha) {
Nader Jawad2dc632a2021-03-29 18:51:29 -0700262 SkPaint paint = SkPaint();
Alec Mouri655a5e42022-09-12 17:49:17 +0000263 paint.setColor(SkColors::kBlack);
264 paint.setAlphaf(alpha);
265 paint.setBlendMode(SkBlendMode::kDstOut);
Nader Jawad2dc632a2021-03-29 18:51:29 -0700266 mCanvas->drawRRect(rect, paint);
267}
268
Derek Sollenberger8872b382014-06-23 14:13:53 -0400269// ----------------------------------------------------------------------------
270// functions to emulate legacy SaveFlags (i.e. independent matrix/clip flags)
271// ----------------------------------------------------------------------------
272
Florin Malitaeecff562015-12-21 10:43:01 -0500273void SkiaCanvas::recordPartialSave(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400274 // A partial save is a save operation which doesn't capture the full canvas state.
Florin Malitaeecff562015-12-21 10:43:01 -0500275 // (either SaveFlags::Matrix or SaveFlags::Clip is missing).
Derek Sollenberger8872b382014-06-23 14:13:53 -0400276
277 // Mask-out non canvas state bits.
Florin Malitaeecff562015-12-21 10:43:01 -0500278 flags &= SaveFlags::MatrixClip;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400279
Florin Malitaeecff562015-12-21 10:43:01 -0500280 if (flags == SaveFlags::MatrixClip) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400281 // not a partial save.
282 return;
283 }
284
Stan Ilievf50806a2016-10-24 10:40:39 -0400285 if (!mSaveStack) {
Kevin Lubickb3731212023-01-05 19:52:09 +0000286 mSaveStack.reset(new std::deque<SaveRec>());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400287 }
288
Kevin Lubickb3731212023-01-05 19:52:09 +0000289 mSaveStack->emplace_back(mCanvas->getSaveCount(), // saveCount
290 flags, // saveFlags
291 mClipStack.size()); // clipIndex
Derek Sollenberger8872b382014-06-23 14:13:53 -0400292}
293
Stan Ilievf50806a2016-10-24 10:40:39 -0400294template <typename T>
Mike Reed6e49c9f2016-12-02 15:36:59 -0500295void SkiaCanvas::recordClip(const T& clip, SkClipOp op) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400296 // Only need tracking when in a partial save frame which
297 // doesn't restore the clip.
298 const SaveRec* rec = this->currentSaveRec();
299 if (rec && !(rec->saveFlags & SaveFlags::Clip)) {
300 mClipStack.emplace_back(clip, op, mCanvas->getTotalMatrix());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400301 }
302}
303
Stan Ilievf50806a2016-10-24 10:40:39 -0400304// Applies and optionally removes all clips >= index.
305void SkiaCanvas::applyPersistentClips(size_t clipStartIndex) {
Kevin Lubicka22c1302023-01-18 14:16:44 +0000306 LOG_FATAL_IF(clipStartIndex > mClipStack.size());
Stan Ilievf50806a2016-10-24 10:40:39 -0400307 const auto begin = mClipStack.cbegin() + clipStartIndex;
308 const auto end = mClipStack.cend();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400309
Stan Ilievf50806a2016-10-24 10:40:39 -0400310 // Clip application mutates the CTM.
311 const SkMatrix saveMatrix = mCanvas->getTotalMatrix();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400312
Stan Ilievf50806a2016-10-24 10:40:39 -0400313 for (auto clip = begin; clip != end; ++clip) {
Mike Reed6acfe162016-11-18 17:21:09 -0500314 clip->apply(mCanvas);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400315 }
316
Stan Ilievf50806a2016-10-24 10:40:39 -0400317 mCanvas->setMatrix(saveMatrix);
318
319 // If the current/post-restore save rec is also persisting clips, we
320 // leave them on the stack to be reapplied part of the next restore().
321 // Otherwise we're done and just pop them.
Kevin Lubickb3731212023-01-05 19:52:09 +0000322 const SaveRec* rec = this->currentSaveRec();
Stan Ilievf50806a2016-10-24 10:40:39 -0400323 if (!rec || (rec->saveFlags & SaveFlags::Clip)) {
324 mClipStack.erase(begin, end);
325 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400326}
327
328// ----------------------------------------------------------------------------
329// Canvas state operations: Matrix
330// ----------------------------------------------------------------------------
331
332void SkiaCanvas::getMatrix(SkMatrix* outMatrix) const {
333 *outMatrix = mCanvas->getTotalMatrix();
334}
335
336void SkiaCanvas::setMatrix(const SkMatrix& matrix) {
337 mCanvas->setMatrix(matrix);
338}
339
340void SkiaCanvas::concat(const SkMatrix& matrix) {
341 mCanvas->concat(matrix);
342}
343
Jorge Betancourtc9806fadcc2023-12-04 15:30:18 -0500344void SkiaCanvas::concat(const SkM44& matrix) {
345 mCanvas->concat(matrix);
346}
347
Derek Sollenberger8872b382014-06-23 14:13:53 -0400348void SkiaCanvas::rotate(float degrees) {
349 mCanvas->rotate(degrees);
350}
351
352void SkiaCanvas::scale(float sx, float sy) {
353 mCanvas->scale(sx, sy);
354}
355
356void SkiaCanvas::skew(float sx, float sy) {
357 mCanvas->skew(sx, sy);
358}
359
360void SkiaCanvas::translate(float dx, float dy) {
361 mCanvas->translate(dx, dy);
362}
363
364// ----------------------------------------------------------------------------
365// Canvas state operations: Clips
366// ----------------------------------------------------------------------------
367
368// This function is a mirror of SkCanvas::getClipBounds except that it does
369// not outset the edge of the clip to account for anti-aliasing. There is
370// a skia bug to investigate pushing this logic into back into skia.
371// (see https://code.google.com/p/skia/issues/detail?id=1303)
372bool SkiaCanvas::getClipBounds(SkRect* outRect) const {
373 SkIRect ibounds;
Mike Reed5e438982017-01-25 08:23:25 -0500374 if (!mCanvas->getDeviceClipBounds(&ibounds)) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400375 return false;
376 }
377
378 SkMatrix inverse;
379 // if we can't invert the CTM, we can't return local clip bounds
380 if (!mCanvas->getTotalMatrix().invert(&inverse)) {
381 if (outRect) {
382 outRect->setEmpty();
383 }
384 return false;
385 }
386
387 if (NULL != outRect) {
388 SkRect r = SkRect::Make(ibounds);
389 inverse.mapRect(outRect, r);
390 }
391 return true;
392}
393
394bool SkiaCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
395 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
396 return mCanvas->quickReject(bounds);
397}
398
399bool SkiaCanvas::quickRejectPath(const SkPath& path) const {
400 return mCanvas->quickReject(path);
401}
402
Mike Reed6e49c9f2016-12-02 15:36:59 -0500403bool SkiaCanvas::clipRect(float left, float top, float right, float bottom, SkClipOp op) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400404 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Stan Ilievf50806a2016-10-24 10:40:39 -0400405 this->recordClip(rect, op);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400406 mCanvas->clipRect(rect, op);
Chris Craik5ec6a282015-06-23 15:42:12 -0700407 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400408}
409
Mike Reed6e49c9f2016-12-02 15:36:59 -0500410bool SkiaCanvas::clipPath(const SkPath* path, SkClipOp op) {
Derek Sollenbergerf7d98f42017-04-17 11:27:36 -0400411 this->recordClip(*path, op);
Nader Jawade431e312019-12-04 14:13:18 -0800412 mCanvas->clipPath(*path, op, true);
Chris Craik5ec6a282015-06-23 15:42:12 -0700413 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400414}
415
Michael Ludwig70cf50c22021-07-21 17:02:39 +0000416bool SkiaCanvas::replaceClipRect_deprecated(float left, float top, float right, float bottom) {
417 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
418
419 // Emulated clip rects are not recorded for partial saves, since
420 // partial saves have been removed from the public API.
421 SkAndroidFrameworkUtils::ResetClip(mCanvas);
422 mCanvas->clipRect(rect, SkClipOp::kIntersect);
423 return !mCanvas->isClipEmpty();
424}
425
426bool SkiaCanvas::replaceClipPath_deprecated(const SkPath* path) {
427 SkAndroidFrameworkUtils::ResetClip(mCanvas);
428 mCanvas->clipPath(*path, SkClipOp::kIntersect, true);
429 return !mCanvas->isClipEmpty();
430}
431
Derek Sollenberger8872b382014-06-23 14:13:53 -0400432// ----------------------------------------------------------------------------
433// Canvas state operations: Filters
434// ----------------------------------------------------------------------------
435
Ben Wagner0ed10be2018-06-28 17:08:16 -0400436PaintFilter* SkiaCanvas::getPaintFilter() {
437 return mPaintFilter.get();
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400438}
439
Ben Wagner0ed10be2018-06-28 17:08:16 -0400440void SkiaCanvas::setPaintFilter(sk_sp<PaintFilter> paintFilter) {
441 mPaintFilter = std::move(paintFilter);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400442}
443
444// ----------------------------------------------------------------------------
Matt Sarettd0814db2017-04-13 09:33:18 -0400445// Canvas state operations: Capture
446// ----------------------------------------------------------------------------
447
448SkCanvasState* SkiaCanvas::captureCanvasState() const {
449 SkCanvas* canvas = mCanvas;
450 if (mCanvasOwned) {
451 // Important to use the underlying SkCanvas, not the wrapper.
452 canvas = mCanvasOwned.get();
453 }
454
455 // Workarounds for http://crbug.com/271096: SW draw only supports
456 // translate & scale transforms, and a simple rectangular clip.
457 // (This also avoids significant wasted time in calling
458 // SkCanvasStateUtils::CaptureCanvasState when the clip is complex).
John Reck1bcacfd2017-11-03 10:12:19 -0700459 if (!canvas->isClipRect() || (canvas->getTotalMatrix().getType() &
460 ~(SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask))) {
461 return nullptr;
Matt Sarettd0814db2017-04-13 09:33:18 -0400462 }
463
464 return SkCanvasStateUtils::CaptureCanvasState(canvas);
465}
466
467// ----------------------------------------------------------------------------
Derek Sollenberger8872b382014-06-23 14:13:53 -0400468// Canvas draw operations
469// ----------------------------------------------------------------------------
470
Mike Reed260ab722016-10-07 15:59:20 -0400471void SkiaCanvas::drawColor(int color, SkBlendMode mode) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400472 mCanvas->drawColor(color, mode);
473}
474
Mike Reeda6cb58a2021-07-10 13:31:34 -0400475void SkiaCanvas::onFilterPaint(Paint& paint) {
Ben Wagner0ed10be2018-06-28 17:08:16 -0400476 if (mPaintFilter) {
Mike Reeda6cb58a2021-07-10 13:31:34 -0400477 mPaintFilter->filterFullPaint(&paint);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400478 }
Ben Wagner0ed10be2018-06-28 17:08:16 -0400479}
480
Mike Reeda6cb58a2021-07-10 13:31:34 -0400481void SkiaCanvas::drawPaint(const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500482 mCanvas->drawPaint(filterPaint(paint));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400483}
484
485// ----------------------------------------------------------------------------
486// Canvas draw operations: Geometry
487// ----------------------------------------------------------------------------
488
Mike Reedc2dbc032019-07-25 12:28:29 -0400489void SkiaCanvas::drawPoints(const float* points, int count, const Paint& paint,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400490 SkCanvas::PointMode mode) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500491 if (CC_UNLIKELY(count < 2 || paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400492 // convert the floats into SkPoints
John Reck1bcacfd2017-11-03 10:12:19 -0700493 count >>= 1; // now it is the number of points
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400494 std::unique_ptr<SkPoint[]> pts(new SkPoint[count]);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400495 for (int i = 0; i < count; i++) {
496 pts[i].set(points[0], points[1]);
497 points += 2;
498 }
Mike Reedc2dbc032019-07-25 12:28:29 -0400499
Mike Reedbb4cb482021-02-22 14:21:20 -0500500 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawPoints(mode, count, pts.get(), p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400501}
502
Mike Reedc2dbc032019-07-25 12:28:29 -0400503void SkiaCanvas::drawPoint(float x, float y, const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500504 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawPoint(x, y, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400505}
506
Mike Reedc2dbc032019-07-25 12:28:29 -0400507void SkiaCanvas::drawPoints(const float* points, int count, const Paint& paint) {
508 this->drawPoints(points, count, paint, SkCanvas::kPoints_PointMode);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400509}
510
511void SkiaCanvas::drawLine(float startX, float startY, float stopX, float stopY,
Mike Reedc2dbc032019-07-25 12:28:29 -0400512 const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500513 applyLooper(&paint,
514 [&](const SkPaint& p) { mCanvas->drawLine(startX, startY, stopX, stopY, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400515}
516
Mike Reedc2dbc032019-07-25 12:28:29 -0400517void SkiaCanvas::drawLines(const float* points, int count, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500518 if (CC_UNLIKELY(count < 4 || paint.nothingToDraw())) return;
Mike Reedc2dbc032019-07-25 12:28:29 -0400519 this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400520}
521
Mike Reedc2dbc032019-07-25 12:28:29 -0400522void SkiaCanvas::drawRect(float left, float top, float right, float bottom, 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) {
Mike Reedc2dbc032019-07-25 12:28:29 -0400525 mCanvas->drawRect({left, top, right, bottom}, p);
526 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400527}
528
Mike Reedc2dbc032019-07-25 12:28:29 -0400529void SkiaCanvas::drawRegion(const SkRegion& region, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500530 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Mike Reedbb4cb482021-02-22 14:21:20 -0500531 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawRegion(region, p); });
Derek Sollenberger94394b32015-07-10 09:58:41 -0400532}
533
John Reck1bcacfd2017-11-03 10:12:19 -0700534void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom, float rx, float ry,
Mike Reedc2dbc032019-07-25 12:28:29 -0400535 const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500536 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400537 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Mike Reedbb4cb482021-02-22 14:21:20 -0500538 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawRoundRect(rect, rx, ry, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400539}
540
Nader Jawadadfe1d92018-09-27 12:27:36 -0700541void SkiaCanvas::drawDoubleRoundRect(const SkRRect& outer, const SkRRect& inner,
Mike Reedc2dbc032019-07-25 12:28:29 -0400542 const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500543 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawDRRect(outer, inner, p); });
Nader Jawadadfe1d92018-09-27 12:27:36 -0700544}
545
Mike Reedc2dbc032019-07-25 12:28:29 -0400546void SkiaCanvas::drawCircle(float x, float y, float radius, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500547 if (CC_UNLIKELY(radius <= 0 || paint.nothingToDraw())) return;
Mike Reedbb4cb482021-02-22 14:21:20 -0500548 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawCircle(x, y, radius, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400549}
550
Mike Reedc2dbc032019-07-25 12:28:29 -0400551void SkiaCanvas::drawOval(float left, float top, float right, float bottom, 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 oval = SkRect::MakeLTRB(left, top, right, bottom);
Mike Reedbb4cb482021-02-22 14:21:20 -0500554 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawOval(oval, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400555}
556
John Reck1bcacfd2017-11-03 10:12:19 -0700557void SkiaCanvas::drawArc(float left, float top, float right, float bottom, float startAngle,
Mike Reedc2dbc032019-07-25 12:28:29 -0400558 float sweepAngle, bool useCenter, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500559 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400560 SkRect arc = SkRect::MakeLTRB(left, top, right, bottom);
Mike Reedbb4cb482021-02-22 14:21:20 -0500561 applyLooper(&paint, [&](const SkPaint& p) {
Mike Reedc2dbc032019-07-25 12:28:29 -0400562 if (fabs(sweepAngle) >= 360.0f) {
563 mCanvas->drawOval(arc, p);
564 } else {
565 mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, p);
566 }
567 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400568}
569
Mike Reedc2dbc032019-07-25 12:28:29 -0400570void SkiaCanvas::drawPath(const SkPath& path, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500571 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Stan Iliev6dcfdec2017-08-15 16:42:05 -0400572 if (CC_UNLIKELY(path.isEmpty() && (!path.isInverseFillType()))) {
573 return;
574 }
Mike Reedbb4cb482021-02-22 14:21:20 -0500575 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawPath(path, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400576}
577
Mike Reedc2dbc032019-07-25 12:28:29 -0400578void SkiaCanvas::drawVertices(const SkVertices* vertices, SkBlendMode mode, const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500579 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawVertices(vertices, mode, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400580}
581
Nader Jawad5f0a8002023-02-21 17:00:51 -0800582void SkiaCanvas::drawMesh(const Mesh& mesh, sk_sp<SkBlender> blender, const Paint& paint) {
583 GrDirectContext* context = nullptr;
584 auto recordingContext = mCanvas->recordingContext();
585 if (recordingContext) {
586 context = recordingContext->asDirectContext();
587 }
588 mesh.updateSkMesh(context);
589 mCanvas->drawMesh(mesh.getSkMesh(), blender, paint);
Angel Aguayo90c46ee2022-11-08 23:42:09 +0000590}
591
Derek Sollenberger8872b382014-06-23 14:13:53 -0400592// ----------------------------------------------------------------------------
593// Canvas draw operations: Bitmaps
594// ----------------------------------------------------------------------------
595
John Reck859af0d2023-11-15 12:40:39 -0500596bool SkiaCanvas::useGainmapShader(Bitmap& bitmap) {
597 // If the bitmap doesn't have a gainmap, don't use the gainmap shader
598 if (!bitmap.hasGainmap()) return false;
599
600 // If we don't have an owned canvas, then we're either hardware accelerated or drawing
601 // to a picture - use the gainmap shader out of caution. Ideally a picture canvas would
602 // use a drawable here instead to defer making that decision until the last possible
603 // moment
604 if (!mCanvasOwned) return true;
605
606 auto info = mCanvasOwned->imageInfo();
607
608 // If it's an unknown colortype then it's not a bitmap-backed canvas
609 if (info.colorType() == SkColorType::kUnknown_SkColorType) return true;
610
611 skcms_TransferFunction tfn;
612 info.colorSpace()->transferFn(&tfn);
613
614 auto transferType = skcms_TransferFunction_getType(&tfn);
615 switch (transferType) {
616 case skcms_TFType_HLGish:
617 case skcms_TFType_HLGinvish:
618 case skcms_TFType_PQish:
619 return true;
620 case skcms_TFType_Invalid:
621 case skcms_TFType_sRGBish:
622 return false;
623 }
624}
625
Mike Reedc2dbc032019-07-25 12:28:29 -0400626void SkiaCanvas::drawBitmap(Bitmap& bitmap, float left, float top, const Paint* paint) {
627 auto image = bitmap.makeImage();
John Reckb2a4b932023-03-29 17:01:25 -0400628
John Reck859af0d2023-11-15 12:40:39 -0500629 if (useGainmapShader(bitmap)) {
John Reckb2a4b932023-03-29 17:01:25 -0400630 Paint gainmapPaint = paint ? *paint : Paint();
631 sk_sp<SkShader> gainmapShader = uirenderer::MakeGainmapShader(
632 image, bitmap.gainmap()->bitmap->makeImage(), bitmap.gainmap()->info,
633 SkTileMode::kClamp, SkTileMode::kClamp, gainmapPaint.sampling());
634 gainmapPaint.setShader(gainmapShader);
635 return drawRect(left, top, left + bitmap.width(), top + bitmap.height(), gainmapPaint);
636 }
637
Mike Reeda6cb58a2021-07-10 13:31:34 -0400638 applyLooper(paint, [&](const Paint& p) {
639 mCanvas->drawImage(image, left, top, p.sampling(), &p);
Mike Reedc2dbc032019-07-25 12:28:29 -0400640 });
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400641}
642
Mike Reedc2dbc032019-07-25 12:28:29 -0400643void SkiaCanvas::drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const Paint* paint) {
Mike Reed6acfe162016-11-18 17:21:09 -0500644 SkAutoCanvasRestore acr(mCanvas, true);
Mike Reed70ffbf92014-12-08 17:03:30 -0500645 mCanvas->concat(matrix);
John Reckb2a4b932023-03-29 17:01:25 -0400646 drawBitmap(bitmap, 0, 0, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400647}
648
John Reck1bcacfd2017-11-03 10:12:19 -0700649void SkiaCanvas::drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight,
650 float srcBottom, float dstLeft, float dstTop, float dstRight,
Mike Reedc2dbc032019-07-25 12:28:29 -0400651 float dstBottom, const Paint* paint) {
652 auto image = bitmap.makeImage();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400653 SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
654 SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400655
John Reck859af0d2023-11-15 12:40:39 -0500656 if (useGainmapShader(bitmap)) {
John Reckb2a4b932023-03-29 17:01:25 -0400657 Paint gainmapPaint = paint ? *paint : Paint();
658 sk_sp<SkShader> gainmapShader = uirenderer::MakeGainmapShader(
659 image, bitmap.gainmap()->bitmap->makeImage(), bitmap.gainmap()->info,
660 SkTileMode::kClamp, SkTileMode::kClamp, gainmapPaint.sampling());
661 gainmapShader = gainmapShader->makeWithLocalMatrix(SkMatrix::RectToRect(srcRect, dstRect));
662 gainmapPaint.setShader(gainmapShader);
663 return drawRect(dstLeft, dstTop, dstRight, dstBottom, gainmapPaint);
664 }
665
Mike Reeda6cb58a2021-07-10 13:31:34 -0400666 applyLooper(paint, [&](const Paint& p) {
667 mCanvas->drawImageRect(image, srcRect, dstRect, p.sampling(), &p,
Mike Reed7994a312021-01-28 18:06:26 -0500668 SkCanvas::kFast_SrcRectConstraint);
Mike Reedc2dbc032019-07-25 12:28:29 -0400669 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400670}
671
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400672void SkiaCanvas::drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight,
Mike Reedc2dbc032019-07-25 12:28:29 -0400673 const float* vertices, const int* colors, const Paint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400674 const int ptCount = (meshWidth + 1) * (meshHeight + 1);
675 const int indexCount = meshWidth * meshHeight * 6;
Mike Reed871cd2d2017-03-17 10:15:52 -0400676 uint32_t flags = SkVertices::kHasTexCoords_BuilderFlag;
677 if (colors) {
678 flags |= SkVertices::kHasColors_BuilderFlag;
679 }
Mike Reed826deef2017-04-04 15:32:04 -0400680 SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, ptCount, indexCount, flags);
Mike Reed871cd2d2017-03-17 10:15:52 -0400681 memcpy(builder.positions(), vertices, ptCount * sizeof(SkPoint));
682 if (colors) {
683 memcpy(builder.colors(), colors, ptCount * sizeof(SkColor));
684 }
685 SkPoint* texs = builder.texCoords();
686 uint16_t* indices = builder.indices();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400687
688 // cons up texture coordinates and indices
689 {
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400690 const SkScalar w = SkIntToScalar(bitmap.width());
691 const SkScalar h = SkIntToScalar(bitmap.height());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400692 const SkScalar dx = w / meshWidth;
693 const SkScalar dy = h / meshHeight;
694
695 SkPoint* texsPtr = texs;
696 SkScalar y = 0;
697 for (int i = 0; i <= meshHeight; i++) {
698 if (i == meshHeight) {
699 y = h; // to ensure numerically we hit h exactly
700 }
701 SkScalar x = 0;
702 for (int j = 0; j < meshWidth; j++) {
703 texsPtr->set(x, y);
704 texsPtr += 1;
705 x += dx;
706 }
707 texsPtr->set(w, y);
708 texsPtr += 1;
709 y += dy;
710 }
Kevin Lubicka22c1302023-01-18 14:16:44 +0000711 LOG_FATAL_IF((texsPtr - texs) != ptCount);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400712 }
713
714 // cons up indices
715 {
716 uint16_t* indexPtr = indices;
717 int index = 0;
718 for (int i = 0; i < meshHeight; i++) {
719 for (int j = 0; j < meshWidth; j++) {
720 // lower-left triangle
721 *indexPtr++ = index;
722 *indexPtr++ = index + meshWidth + 1;
723 *indexPtr++ = index + meshWidth + 2;
724 // upper-right triangle
725 *indexPtr++ = index;
726 *indexPtr++ = index + meshWidth + 2;
727 *indexPtr++ = index + 1;
728 // bump to the next cell
729 index += 1;
730 }
731 // bump to the next row
732 index += 1;
733 }
Kevin Lubicka22c1302023-01-18 14:16:44 +0000734 LOG_FATAL_IF((indexPtr - indices) != indexCount);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400735 }
736
John Reck1bcacfd2017-11-03 10:12:19 -0700737// double-check that we have legal indices
Kevin Lubicka22c1302023-01-18 14:16:44 +0000738#if !defined(NDEBUG)
Derek Sollenberger8872b382014-06-23 14:13:53 -0400739 {
740 for (int i = 0; i < indexCount; i++) {
Kevin Lubicka22c1302023-01-18 14:16:44 +0000741 LOG_FATAL_IF((unsigned)indices[i] >= (unsigned)ptCount);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400742 }
743 }
744#endif
745
Mike Reed7994a312021-01-28 18:06:26 -0500746 auto image = bitmap.makeImage();
747
Derek Sollenberger8872b382014-06-23 14:13:53 -0400748 // cons-up a shader for the bitmap
Mike Reedc2dbc032019-07-25 12:28:29 -0400749 Paint pnt;
750 if (paint) {
751 pnt = *paint;
752 }
Mike Reeda6cb58a2021-07-10 13:31:34 -0400753 SkSamplingOptions sampling = pnt.sampling();
Mike Reed7994a312021-01-28 18:06:26 -0500754 pnt.setShader(image->makeShader(sampling));
755
Mike Reedc2dbc032019-07-25 12:28:29 -0400756 auto v = builder.detach();
Mike Reeda6cb58a2021-07-10 13:31:34 -0400757 applyLooper(&pnt, [&](const Paint& p) {
Mike Reed7994a312021-01-28 18:06:26 -0500758 SkPaint copy(p);
Mike Reeda6cb58a2021-07-10 13:31:34 -0400759 auto s = p.sampling();
Mike Reed7994a312021-01-28 18:06:26 -0500760 if (s != sampling) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500761 // applyLooper changed the quality?
Mike Reed7994a312021-01-28 18:06:26 -0500762 copy.setShader(image->makeShader(s));
763 }
764 mCanvas->drawVertices(v, SkBlendMode::kModulate, copy);
Mike Reedc2dbc032019-07-25 12:28:29 -0400765 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400766}
767
John Reck1bcacfd2017-11-03 10:12:19 -0700768void SkiaCanvas::drawNinePatch(Bitmap& bitmap, const Res_png_9patch& chunk, float dstLeft,
769 float dstTop, float dstRight, float dstBottom,
Mike Reedc2dbc032019-07-25 12:28:29 -0400770 const Paint* paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400771 SkCanvas::Lattice lattice;
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400772 NinePatchUtils::SetLatticeDivs(&lattice, chunk, bitmap.width(), bitmap.height());
Stan Ilievf50806a2016-10-24 10:40:39 -0400773
Stan Ilieve12d7312017-12-04 14:48:27 -0500774 lattice.fRectTypes = nullptr;
775 lattice.fColors = nullptr;
Stan Ilievf50806a2016-10-24 10:40:39 -0400776 int numFlags = 0;
Stan Iliev021693b2016-10-17 16:26:15 -0400777 if (chunk.numColors > 0 && chunk.numColors == NinePatchUtils::NumDistinctRects(lattice)) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400778 // We can expect the framework to give us a color for every distinct rect.
779 // Skia requires a flag for every rect.
780 numFlags = (lattice.fXCount + 1) * (lattice.fYCount + 1);
781 }
782
Kevin Lubicka22c1302023-01-18 14:16:44 +0000783 // Most times, we do not have very many flags/colors, so the stack allocated part of
784 // FatVector will save us a heap allocation.
785 FatVector<SkCanvas::Lattice::RectType, 25> flags(numFlags);
786 FatVector<SkColor, 25> colors(numFlags);
Stan Ilievf50806a2016-10-24 10:40:39 -0400787 if (numFlags > 0) {
Kevin Lubicka22c1302023-01-18 14:16:44 +0000788 NinePatchUtils::SetLatticeFlags(&lattice, flags.data(), numFlags, chunk, colors.data());
Stan Ilievf50806a2016-10-24 10:40:39 -0400789 }
790
791 lattice.fBounds = nullptr;
792 SkRect dst = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Mike Reedc2dbc032019-07-25 12:28:29 -0400793 auto image = bitmap.makeImage();
Mike Reeda6cb58a2021-07-10 13:31:34 -0400794 applyLooper(paint, [&](const Paint& p) {
795 mCanvas->drawImageLattice(image.get(), lattice, dst, p.filterMode(), &p);
Mike Reedc2dbc032019-07-25 12:28:29 -0400796 });
Derek Sollenbergeredca3202015-07-10 13:56:39 -0400797}
798
Derek Sollenberger2d142132018-01-22 10:25:26 -0500799double SkiaCanvas::drawAnimatedImage(AnimatedImageDrawable* imgDrawable) {
800 return imgDrawable->drawStaging(mCanvas);
Leon Scroggins III671cce22018-01-14 16:52:17 -0500801}
802
Doris Liu766431a2016-02-04 22:17:11 +0000803void SkiaCanvas::drawVectorDrawable(VectorDrawableRoot* vectorDrawable) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800804 vectorDrawable->drawStaging(this);
Doris Liu766431a2016-02-04 22:17:11 +0000805}
806
Derek Sollenberger8872b382014-06-23 14:13:53 -0400807// ----------------------------------------------------------------------------
808// Canvas draw operations: Text
809// ----------------------------------------------------------------------------
810
Mike Reed2dfd55d2019-01-08 16:19:03 -0500811void SkiaCanvas::drawGlyphs(ReadGlyphFunc glyphFunc, int count, const Paint& paint, float x,
Seigo Nonaka63af7ba2020-09-21 23:07:43 -0700812 float y, float totalAdvance) {
Stan Iliev0b58d992017-03-30 18:22:27 -0400813 if (count <= 0 || paint.nothingToDraw()) return;
Mike Reedf6d86ac2019-01-18 14:13:23 -0500814 Paint paintCopy(paint);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400815 if (mPaintFilter) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500816 mPaintFilter->filterFullPaint(&paintCopy);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400817 }
Mike Reedf6d86ac2019-01-18 14:13:23 -0500818 const SkFont& font = paintCopy.getSkFont();
Stan Iliev7717e222018-02-05 18:04:11 -0500819 // Stroke with a hairline is drawn on HW with a fill style for compatibility with Android O and
820 // older.
John Recke170fb62018-05-07 08:12:07 -0700821 if (!mCanvasOwned && sApiLevel <= 27 && paintCopy.getStrokeWidth() <= 0 &&
822 paintCopy.getStyle() == SkPaint::kStroke_Style) {
Stan Iliev7717e222018-02-05 18:04:11 -0500823 paintCopy.setStyle(SkPaint::kFill_Style);
824 }
Stan Ilievf50806a2016-10-24 10:40:39 -0400825
Stan Ilievf50806a2016-10-24 10:40:39 -0400826 SkTextBlobBuilder builder;
Mike Reed2e204fc2019-01-28 13:31:36 -0500827 const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(font, count);
Stan Iliev0b58d992017-03-30 18:22:27 -0400828 glyphFunc(buffer.glyphs, buffer.pos);
Stan Ilievf50806a2016-10-24 10:40:39 -0400829
830 sk_sp<SkTextBlob> textBlob(builder.make());
Nathaniel Nifong52d37772020-01-10 16:12:41 -0500831
Mike Reedbb4cb482021-02-22 14:21:20 -0500832 applyLooper(&paintCopy, [&](const SkPaint& p) { mCanvas->drawTextBlob(textBlob, 0, 0, p); });
Seigo Nonakacd348c62023-09-12 16:05:44 +0900833 if (!text_feature::fix_double_underline()) {
834 drawTextDecorations(x, y, totalAdvance, paintCopy);
835 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400836}
837
Yuqian Liafc221492016-07-18 13:07:42 -0400838void SkiaCanvas::drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
Mike Reed2dfd55d2019-01-08 16:19:03 -0500839 const Paint& paint, const SkPath& path, size_t start,
John Reck1bcacfd2017-11-03 10:12:19 -0700840 size_t end) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500841 Paint paintCopy(paint);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400842 if (mPaintFilter) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500843 mPaintFilter->filterFullPaint(&paintCopy);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400844 }
Mike Reedf6d86ac2019-01-18 14:13:23 -0500845 const SkFont& font = paintCopy.getSkFont();
Derek Sollenberger415a74b2018-03-14 15:03:13 -0400846
Yuqian Liafc221492016-07-18 13:07:42 -0400847 const int N = end - start;
Mike Reed4a4b1be2019-01-01 15:43:06 -0500848 SkTextBlobBuilder builder;
849 auto rec = builder.allocRunRSXform(font, N);
850 SkRSXform* xform = (SkRSXform*)rec.pos;
851 uint16_t* glyphs = rec.glyphs;
Yuqian Liafc221492016-07-18 13:07:42 -0400852 SkPathMeasure meas(path, false);
853
854 for (size_t i = start; i < end; i++) {
855 glyphs[i - start] = layout.getGlyphId(i);
Stan Ilievc6c96dd2018-01-10 16:06:04 -0500856 float halfWidth = layout.getCharAdvance(i) * 0.5f;
857 float x = hOffset + layout.getX(i) + halfWidth;
Yuqian Liafc221492016-07-18 13:07:42 -0400858 float y = vOffset + layout.getY(i);
859
860 SkPoint pos;
861 SkVector tan;
862 if (!meas.getPosTan(x, &pos, &tan)) {
863 pos.set(x, y);
864 tan.set(1, 0);
865 }
866 xform[i - start].fSCos = tan.x();
867 xform[i - start].fSSin = tan.y();
Stan Ilievc6c96dd2018-01-10 16:06:04 -0500868 xform[i - start].fTx = pos.x() - tan.y() * y - halfWidth * tan.x();
869 xform[i - start].fTy = pos.y() + tan.x() * y - halfWidth * tan.y();
Yuqian Liafc221492016-07-18 13:07:42 -0400870 }
Leon Scroggins III950f2aa2020-04-29 13:46:00 -0400871
872 sk_sp<SkTextBlob> textBlob(builder.make());
873
Mike Reedbb4cb482021-02-22 14:21:20 -0500874 applyLooper(&paintCopy, [&](const SkPaint& p) { mCanvas->drawTextBlob(textBlob, 0, 0, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400875}
876
Derek Sollenberger6f485562015-07-30 10:00:39 -0400877// ----------------------------------------------------------------------------
878// Canvas draw operations: Animations
879// ----------------------------------------------------------------------------
880
Derek Sollenberger6f485562015-07-30 10:00:39 -0400881void SkiaCanvas::drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
John Reck1bcacfd2017-11-03 10:12:19 -0700882 uirenderer::CanvasPropertyPrimitive* top,
883 uirenderer::CanvasPropertyPrimitive* right,
884 uirenderer::CanvasPropertyPrimitive* bottom,
885 uirenderer::CanvasPropertyPrimitive* rx,
886 uirenderer::CanvasPropertyPrimitive* ry,
887 uirenderer::CanvasPropertyPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -0400888 sk_sp<uirenderer::skiapipeline::AnimatedRoundRect> drawable(
John Reck1bcacfd2017-11-03 10:12:19 -0700889 new uirenderer::skiapipeline::AnimatedRoundRect(left, top, right, bottom, rx, ry,
890 paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400891 mCanvas->drawDrawable(drawable.get());
892}
893
John Reck1bcacfd2017-11-03 10:12:19 -0700894void SkiaCanvas::drawCircle(uirenderer::CanvasPropertyPrimitive* x,
895 uirenderer::CanvasPropertyPrimitive* y,
896 uirenderer::CanvasPropertyPrimitive* radius,
897 uirenderer::CanvasPropertyPaint* paint) {
898 sk_sp<uirenderer::skiapipeline::AnimatedCircle> drawable(
899 new uirenderer::skiapipeline::AnimatedCircle(x, y, radius, paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400900 mCanvas->drawDrawable(drawable.get());
901}
902
Lucas Dupin00af5272021-04-29 20:30:01 -0700903void SkiaCanvas::drawRipple(const uirenderer::skiapipeline::RippleDrawableParams& params) {
904 uirenderer::skiapipeline::AnimatedRippleDrawable::draw(mCanvas, params);
Derek Sollenbergerdf301aa2020-12-17 11:06:03 -0500905}
906
John Reck894e85a2019-07-15 16:16:44 -0700907void SkiaCanvas::drawPicture(const SkPicture& picture) {
908 // TODO: Change to mCanvas->drawPicture()? SkCanvas::drawPicture seems to be
909 // where the logic is for playback vs. ref picture. Using picture.playback here
910 // to stay behavior-identical for now, but should revisit this at some point.
911 picture.playback(mCanvas);
912}
913
Derek Sollenberger6f485562015-07-30 10:00:39 -0400914// ----------------------------------------------------------------------------
915// Canvas draw operations: View System
916// ----------------------------------------------------------------------------
917
Stan Ilievf50806a2016-10-24 10:40:39 -0400918void SkiaCanvas::drawLayer(uirenderer::DeferredLayerUpdater* layerUpdater) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400919 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw Layers");
920}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400921
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400922void SkiaCanvas::drawRenderNode(uirenderer::RenderNode* renderNode) {
923 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw RenderNodes");
924}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400925
John Reck1bcacfd2017-11-03 10:12:19 -0700926} // namespace android