blob: 008ea3aaa2e7160292707468db33a139e1c10b88 [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
344void SkiaCanvas::rotate(float degrees) {
345 mCanvas->rotate(degrees);
346}
347
348void SkiaCanvas::scale(float sx, float sy) {
349 mCanvas->scale(sx, sy);
350}
351
352void SkiaCanvas::skew(float sx, float sy) {
353 mCanvas->skew(sx, sy);
354}
355
356void SkiaCanvas::translate(float dx, float dy) {
357 mCanvas->translate(dx, dy);
358}
359
360// ----------------------------------------------------------------------------
361// Canvas state operations: Clips
362// ----------------------------------------------------------------------------
363
364// This function is a mirror of SkCanvas::getClipBounds except that it does
365// not outset the edge of the clip to account for anti-aliasing. There is
366// a skia bug to investigate pushing this logic into back into skia.
367// (see https://code.google.com/p/skia/issues/detail?id=1303)
368bool SkiaCanvas::getClipBounds(SkRect* outRect) const {
369 SkIRect ibounds;
Mike Reed5e438982017-01-25 08:23:25 -0500370 if (!mCanvas->getDeviceClipBounds(&ibounds)) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400371 return false;
372 }
373
374 SkMatrix inverse;
375 // if we can't invert the CTM, we can't return local clip bounds
376 if (!mCanvas->getTotalMatrix().invert(&inverse)) {
377 if (outRect) {
378 outRect->setEmpty();
379 }
380 return false;
381 }
382
383 if (NULL != outRect) {
384 SkRect r = SkRect::Make(ibounds);
385 inverse.mapRect(outRect, r);
386 }
387 return true;
388}
389
390bool SkiaCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
391 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
392 return mCanvas->quickReject(bounds);
393}
394
395bool SkiaCanvas::quickRejectPath(const SkPath& path) const {
396 return mCanvas->quickReject(path);
397}
398
Mike Reed6e49c9f2016-12-02 15:36:59 -0500399bool SkiaCanvas::clipRect(float left, float top, float right, float bottom, SkClipOp op) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400400 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Stan Ilievf50806a2016-10-24 10:40:39 -0400401 this->recordClip(rect, op);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400402 mCanvas->clipRect(rect, op);
Chris Craik5ec6a282015-06-23 15:42:12 -0700403 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400404}
405
Mike Reed6e49c9f2016-12-02 15:36:59 -0500406bool SkiaCanvas::clipPath(const SkPath* path, SkClipOp op) {
Derek Sollenbergerf7d98f42017-04-17 11:27:36 -0400407 this->recordClip(*path, op);
Nader Jawade431e312019-12-04 14:13:18 -0800408 mCanvas->clipPath(*path, op, true);
Chris Craik5ec6a282015-06-23 15:42:12 -0700409 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400410}
411
Michael Ludwig70cf50c22021-07-21 17:02:39 +0000412bool SkiaCanvas::replaceClipRect_deprecated(float left, float top, float right, float bottom) {
413 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
414
415 // Emulated clip rects are not recorded for partial saves, since
416 // partial saves have been removed from the public API.
417 SkAndroidFrameworkUtils::ResetClip(mCanvas);
418 mCanvas->clipRect(rect, SkClipOp::kIntersect);
419 return !mCanvas->isClipEmpty();
420}
421
422bool SkiaCanvas::replaceClipPath_deprecated(const SkPath* path) {
423 SkAndroidFrameworkUtils::ResetClip(mCanvas);
424 mCanvas->clipPath(*path, SkClipOp::kIntersect, true);
425 return !mCanvas->isClipEmpty();
426}
427
Derek Sollenberger8872b382014-06-23 14:13:53 -0400428// ----------------------------------------------------------------------------
429// Canvas state operations: Filters
430// ----------------------------------------------------------------------------
431
Ben Wagner0ed10be2018-06-28 17:08:16 -0400432PaintFilter* SkiaCanvas::getPaintFilter() {
433 return mPaintFilter.get();
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400434}
435
Ben Wagner0ed10be2018-06-28 17:08:16 -0400436void SkiaCanvas::setPaintFilter(sk_sp<PaintFilter> paintFilter) {
437 mPaintFilter = std::move(paintFilter);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400438}
439
440// ----------------------------------------------------------------------------
Matt Sarettd0814db2017-04-13 09:33:18 -0400441// Canvas state operations: Capture
442// ----------------------------------------------------------------------------
443
444SkCanvasState* SkiaCanvas::captureCanvasState() const {
445 SkCanvas* canvas = mCanvas;
446 if (mCanvasOwned) {
447 // Important to use the underlying SkCanvas, not the wrapper.
448 canvas = mCanvasOwned.get();
449 }
450
451 // Workarounds for http://crbug.com/271096: SW draw only supports
452 // translate & scale transforms, and a simple rectangular clip.
453 // (This also avoids significant wasted time in calling
454 // SkCanvasStateUtils::CaptureCanvasState when the clip is complex).
John Reck1bcacfd2017-11-03 10:12:19 -0700455 if (!canvas->isClipRect() || (canvas->getTotalMatrix().getType() &
456 ~(SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask))) {
457 return nullptr;
Matt Sarettd0814db2017-04-13 09:33:18 -0400458 }
459
460 return SkCanvasStateUtils::CaptureCanvasState(canvas);
461}
462
463// ----------------------------------------------------------------------------
Derek Sollenberger8872b382014-06-23 14:13:53 -0400464// Canvas draw operations
465// ----------------------------------------------------------------------------
466
Mike Reed260ab722016-10-07 15:59:20 -0400467void SkiaCanvas::drawColor(int color, SkBlendMode mode) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400468 mCanvas->drawColor(color, mode);
469}
470
Mike Reeda6cb58a2021-07-10 13:31:34 -0400471void SkiaCanvas::onFilterPaint(Paint& paint) {
Ben Wagner0ed10be2018-06-28 17:08:16 -0400472 if (mPaintFilter) {
Mike Reeda6cb58a2021-07-10 13:31:34 -0400473 mPaintFilter->filterFullPaint(&paint);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400474 }
Ben Wagner0ed10be2018-06-28 17:08:16 -0400475}
476
Mike Reeda6cb58a2021-07-10 13:31:34 -0400477void SkiaCanvas::drawPaint(const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500478 mCanvas->drawPaint(filterPaint(paint));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400479}
480
481// ----------------------------------------------------------------------------
482// Canvas draw operations: Geometry
483// ----------------------------------------------------------------------------
484
Mike Reedc2dbc032019-07-25 12:28:29 -0400485void SkiaCanvas::drawPoints(const float* points, int count, const Paint& paint,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400486 SkCanvas::PointMode mode) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500487 if (CC_UNLIKELY(count < 2 || paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400488 // convert the floats into SkPoints
John Reck1bcacfd2017-11-03 10:12:19 -0700489 count >>= 1; // now it is the number of points
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400490 std::unique_ptr<SkPoint[]> pts(new SkPoint[count]);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400491 for (int i = 0; i < count; i++) {
492 pts[i].set(points[0], points[1]);
493 points += 2;
494 }
Mike Reedc2dbc032019-07-25 12:28:29 -0400495
Mike Reedbb4cb482021-02-22 14:21:20 -0500496 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawPoints(mode, count, pts.get(), p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400497}
498
Mike Reedc2dbc032019-07-25 12:28:29 -0400499void SkiaCanvas::drawPoint(float x, float y, const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500500 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawPoint(x, y, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400501}
502
Mike Reedc2dbc032019-07-25 12:28:29 -0400503void SkiaCanvas::drawPoints(const float* points, int count, const Paint& paint) {
504 this->drawPoints(points, count, paint, SkCanvas::kPoints_PointMode);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400505}
506
507void SkiaCanvas::drawLine(float startX, float startY, float stopX, float stopY,
Mike Reedc2dbc032019-07-25 12:28:29 -0400508 const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500509 applyLooper(&paint,
510 [&](const SkPaint& p) { mCanvas->drawLine(startX, startY, stopX, stopY, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400511}
512
Mike Reedc2dbc032019-07-25 12:28:29 -0400513void SkiaCanvas::drawLines(const float* points, int count, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500514 if (CC_UNLIKELY(count < 4 || paint.nothingToDraw())) return;
Mike Reedc2dbc032019-07-25 12:28:29 -0400515 this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400516}
517
Mike Reedc2dbc032019-07-25 12:28:29 -0400518void SkiaCanvas::drawRect(float left, float top, float right, float bottom, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500519 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Mike Reedbb4cb482021-02-22 14:21:20 -0500520 applyLooper(&paint, [&](const SkPaint& p) {
Mike Reedc2dbc032019-07-25 12:28:29 -0400521 mCanvas->drawRect({left, top, right, bottom}, p);
522 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400523}
524
Mike Reedc2dbc032019-07-25 12:28:29 -0400525void SkiaCanvas::drawRegion(const SkRegion& region, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500526 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Mike Reedbb4cb482021-02-22 14:21:20 -0500527 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawRegion(region, p); });
Derek Sollenberger94394b32015-07-10 09:58:41 -0400528}
529
John Reck1bcacfd2017-11-03 10:12:19 -0700530void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom, float rx, float ry,
Mike Reedc2dbc032019-07-25 12:28:29 -0400531 const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500532 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400533 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Mike Reedbb4cb482021-02-22 14:21:20 -0500534 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawRoundRect(rect, rx, ry, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400535}
536
Nader Jawadadfe1d92018-09-27 12:27:36 -0700537void SkiaCanvas::drawDoubleRoundRect(const SkRRect& outer, const SkRRect& inner,
Mike Reedc2dbc032019-07-25 12:28:29 -0400538 const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500539 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawDRRect(outer, inner, p); });
Nader Jawadadfe1d92018-09-27 12:27:36 -0700540}
541
Mike Reedc2dbc032019-07-25 12:28:29 -0400542void SkiaCanvas::drawCircle(float x, float y, float radius, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500543 if (CC_UNLIKELY(radius <= 0 || paint.nothingToDraw())) return;
Mike Reedbb4cb482021-02-22 14:21:20 -0500544 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawCircle(x, y, radius, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400545}
546
Mike Reedc2dbc032019-07-25 12:28:29 -0400547void SkiaCanvas::drawOval(float left, float top, float right, float bottom, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500548 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400549 SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
Mike Reedbb4cb482021-02-22 14:21:20 -0500550 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawOval(oval, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400551}
552
John Reck1bcacfd2017-11-03 10:12:19 -0700553void SkiaCanvas::drawArc(float left, float top, float right, float bottom, float startAngle,
Mike Reedc2dbc032019-07-25 12:28:29 -0400554 float sweepAngle, bool useCenter, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500555 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400556 SkRect arc = SkRect::MakeLTRB(left, top, right, bottom);
Mike Reedbb4cb482021-02-22 14:21:20 -0500557 applyLooper(&paint, [&](const SkPaint& p) {
Mike Reedc2dbc032019-07-25 12:28:29 -0400558 if (fabs(sweepAngle) >= 360.0f) {
559 mCanvas->drawOval(arc, p);
560 } else {
561 mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, p);
562 }
563 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400564}
565
Mike Reedc2dbc032019-07-25 12:28:29 -0400566void SkiaCanvas::drawPath(const SkPath& path, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500567 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Stan Iliev6dcfdec2017-08-15 16:42:05 -0400568 if (CC_UNLIKELY(path.isEmpty() && (!path.isInverseFillType()))) {
569 return;
570 }
Mike Reedbb4cb482021-02-22 14:21:20 -0500571 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawPath(path, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400572}
573
Mike Reedc2dbc032019-07-25 12:28:29 -0400574void SkiaCanvas::drawVertices(const SkVertices* vertices, SkBlendMode mode, const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500575 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawVertices(vertices, mode, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400576}
577
Nader Jawad5f0a8002023-02-21 17:00:51 -0800578void SkiaCanvas::drawMesh(const Mesh& mesh, sk_sp<SkBlender> blender, const Paint& paint) {
579 GrDirectContext* context = nullptr;
580 auto recordingContext = mCanvas->recordingContext();
581 if (recordingContext) {
582 context = recordingContext->asDirectContext();
583 }
584 mesh.updateSkMesh(context);
585 mCanvas->drawMesh(mesh.getSkMesh(), blender, paint);
Angel Aguayo90c46ee2022-11-08 23:42:09 +0000586}
587
Derek Sollenberger8872b382014-06-23 14:13:53 -0400588// ----------------------------------------------------------------------------
589// Canvas draw operations: Bitmaps
590// ----------------------------------------------------------------------------
591
John Reck859af0d2023-11-15 12:40:39 -0500592bool SkiaCanvas::useGainmapShader(Bitmap& bitmap) {
593 // If the bitmap doesn't have a gainmap, don't use the gainmap shader
594 if (!bitmap.hasGainmap()) return false;
595
596 // If we don't have an owned canvas, then we're either hardware accelerated or drawing
597 // to a picture - use the gainmap shader out of caution. Ideally a picture canvas would
598 // use a drawable here instead to defer making that decision until the last possible
599 // moment
600 if (!mCanvasOwned) return true;
601
602 auto info = mCanvasOwned->imageInfo();
603
604 // If it's an unknown colortype then it's not a bitmap-backed canvas
605 if (info.colorType() == SkColorType::kUnknown_SkColorType) return true;
606
607 skcms_TransferFunction tfn;
608 info.colorSpace()->transferFn(&tfn);
609
610 auto transferType = skcms_TransferFunction_getType(&tfn);
611 switch (transferType) {
612 case skcms_TFType_HLGish:
613 case skcms_TFType_HLGinvish:
614 case skcms_TFType_PQish:
615 return true;
616 case skcms_TFType_Invalid:
617 case skcms_TFType_sRGBish:
618 return false;
619 }
620}
621
Mike Reedc2dbc032019-07-25 12:28:29 -0400622void SkiaCanvas::drawBitmap(Bitmap& bitmap, float left, float top, const Paint* paint) {
623 auto image = bitmap.makeImage();
John Reckb2a4b932023-03-29 17:01:25 -0400624
John Reck859af0d2023-11-15 12:40:39 -0500625 if (useGainmapShader(bitmap)) {
John Reckb2a4b932023-03-29 17:01:25 -0400626 Paint gainmapPaint = paint ? *paint : Paint();
627 sk_sp<SkShader> gainmapShader = uirenderer::MakeGainmapShader(
628 image, bitmap.gainmap()->bitmap->makeImage(), bitmap.gainmap()->info,
629 SkTileMode::kClamp, SkTileMode::kClamp, gainmapPaint.sampling());
630 gainmapPaint.setShader(gainmapShader);
631 return drawRect(left, top, left + bitmap.width(), top + bitmap.height(), gainmapPaint);
632 }
633
Mike Reeda6cb58a2021-07-10 13:31:34 -0400634 applyLooper(paint, [&](const Paint& p) {
635 mCanvas->drawImage(image, left, top, p.sampling(), &p);
Mike Reedc2dbc032019-07-25 12:28:29 -0400636 });
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400637}
638
Mike Reedc2dbc032019-07-25 12:28:29 -0400639void SkiaCanvas::drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const Paint* paint) {
Mike Reed6acfe162016-11-18 17:21:09 -0500640 SkAutoCanvasRestore acr(mCanvas, true);
Mike Reed70ffbf92014-12-08 17:03:30 -0500641 mCanvas->concat(matrix);
John Reckb2a4b932023-03-29 17:01:25 -0400642 drawBitmap(bitmap, 0, 0, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400643}
644
John Reck1bcacfd2017-11-03 10:12:19 -0700645void SkiaCanvas::drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight,
646 float srcBottom, float dstLeft, float dstTop, float dstRight,
Mike Reedc2dbc032019-07-25 12:28:29 -0400647 float dstBottom, const Paint* paint) {
648 auto image = bitmap.makeImage();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400649 SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
650 SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400651
John Reck859af0d2023-11-15 12:40:39 -0500652 if (useGainmapShader(bitmap)) {
John Reckb2a4b932023-03-29 17:01:25 -0400653 Paint gainmapPaint = paint ? *paint : Paint();
654 sk_sp<SkShader> gainmapShader = uirenderer::MakeGainmapShader(
655 image, bitmap.gainmap()->bitmap->makeImage(), bitmap.gainmap()->info,
656 SkTileMode::kClamp, SkTileMode::kClamp, gainmapPaint.sampling());
657 gainmapShader = gainmapShader->makeWithLocalMatrix(SkMatrix::RectToRect(srcRect, dstRect));
658 gainmapPaint.setShader(gainmapShader);
659 return drawRect(dstLeft, dstTop, dstRight, dstBottom, gainmapPaint);
660 }
661
Mike Reeda6cb58a2021-07-10 13:31:34 -0400662 applyLooper(paint, [&](const Paint& p) {
663 mCanvas->drawImageRect(image, srcRect, dstRect, p.sampling(), &p,
Mike Reed7994a312021-01-28 18:06:26 -0500664 SkCanvas::kFast_SrcRectConstraint);
Mike Reedc2dbc032019-07-25 12:28:29 -0400665 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400666}
667
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400668void SkiaCanvas::drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight,
Mike Reedc2dbc032019-07-25 12:28:29 -0400669 const float* vertices, const int* colors, const Paint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400670 const int ptCount = (meshWidth + 1) * (meshHeight + 1);
671 const int indexCount = meshWidth * meshHeight * 6;
Mike Reed871cd2d2017-03-17 10:15:52 -0400672 uint32_t flags = SkVertices::kHasTexCoords_BuilderFlag;
673 if (colors) {
674 flags |= SkVertices::kHasColors_BuilderFlag;
675 }
Mike Reed826deef2017-04-04 15:32:04 -0400676 SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, ptCount, indexCount, flags);
Mike Reed871cd2d2017-03-17 10:15:52 -0400677 memcpy(builder.positions(), vertices, ptCount * sizeof(SkPoint));
678 if (colors) {
679 memcpy(builder.colors(), colors, ptCount * sizeof(SkColor));
680 }
681 SkPoint* texs = builder.texCoords();
682 uint16_t* indices = builder.indices();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400683
684 // cons up texture coordinates and indices
685 {
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400686 const SkScalar w = SkIntToScalar(bitmap.width());
687 const SkScalar h = SkIntToScalar(bitmap.height());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400688 const SkScalar dx = w / meshWidth;
689 const SkScalar dy = h / meshHeight;
690
691 SkPoint* texsPtr = texs;
692 SkScalar y = 0;
693 for (int i = 0; i <= meshHeight; i++) {
694 if (i == meshHeight) {
695 y = h; // to ensure numerically we hit h exactly
696 }
697 SkScalar x = 0;
698 for (int j = 0; j < meshWidth; j++) {
699 texsPtr->set(x, y);
700 texsPtr += 1;
701 x += dx;
702 }
703 texsPtr->set(w, y);
704 texsPtr += 1;
705 y += dy;
706 }
Kevin Lubicka22c1302023-01-18 14:16:44 +0000707 LOG_FATAL_IF((texsPtr - texs) != ptCount);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400708 }
709
710 // cons up indices
711 {
712 uint16_t* indexPtr = indices;
713 int index = 0;
714 for (int i = 0; i < meshHeight; i++) {
715 for (int j = 0; j < meshWidth; j++) {
716 // lower-left triangle
717 *indexPtr++ = index;
718 *indexPtr++ = index + meshWidth + 1;
719 *indexPtr++ = index + meshWidth + 2;
720 // upper-right triangle
721 *indexPtr++ = index;
722 *indexPtr++ = index + meshWidth + 2;
723 *indexPtr++ = index + 1;
724 // bump to the next cell
725 index += 1;
726 }
727 // bump to the next row
728 index += 1;
729 }
Kevin Lubicka22c1302023-01-18 14:16:44 +0000730 LOG_FATAL_IF((indexPtr - indices) != indexCount);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400731 }
732
John Reck1bcacfd2017-11-03 10:12:19 -0700733// double-check that we have legal indices
Kevin Lubicka22c1302023-01-18 14:16:44 +0000734#if !defined(NDEBUG)
Derek Sollenberger8872b382014-06-23 14:13:53 -0400735 {
736 for (int i = 0; i < indexCount; i++) {
Kevin Lubicka22c1302023-01-18 14:16:44 +0000737 LOG_FATAL_IF((unsigned)indices[i] >= (unsigned)ptCount);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400738 }
739 }
740#endif
741
Mike Reed7994a312021-01-28 18:06:26 -0500742 auto image = bitmap.makeImage();
743
Derek Sollenberger8872b382014-06-23 14:13:53 -0400744 // cons-up a shader for the bitmap
Mike Reedc2dbc032019-07-25 12:28:29 -0400745 Paint pnt;
746 if (paint) {
747 pnt = *paint;
748 }
Mike Reeda6cb58a2021-07-10 13:31:34 -0400749 SkSamplingOptions sampling = pnt.sampling();
Mike Reed7994a312021-01-28 18:06:26 -0500750 pnt.setShader(image->makeShader(sampling));
751
Mike Reedc2dbc032019-07-25 12:28:29 -0400752 auto v = builder.detach();
Mike Reeda6cb58a2021-07-10 13:31:34 -0400753 applyLooper(&pnt, [&](const Paint& p) {
Mike Reed7994a312021-01-28 18:06:26 -0500754 SkPaint copy(p);
Mike Reeda6cb58a2021-07-10 13:31:34 -0400755 auto s = p.sampling();
Mike Reed7994a312021-01-28 18:06:26 -0500756 if (s != sampling) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500757 // applyLooper changed the quality?
Mike Reed7994a312021-01-28 18:06:26 -0500758 copy.setShader(image->makeShader(s));
759 }
760 mCanvas->drawVertices(v, SkBlendMode::kModulate, copy);
Mike Reedc2dbc032019-07-25 12:28:29 -0400761 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400762}
763
John Reck1bcacfd2017-11-03 10:12:19 -0700764void SkiaCanvas::drawNinePatch(Bitmap& bitmap, const Res_png_9patch& chunk, float dstLeft,
765 float dstTop, float dstRight, float dstBottom,
Mike Reedc2dbc032019-07-25 12:28:29 -0400766 const Paint* paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400767 SkCanvas::Lattice lattice;
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400768 NinePatchUtils::SetLatticeDivs(&lattice, chunk, bitmap.width(), bitmap.height());
Stan Ilievf50806a2016-10-24 10:40:39 -0400769
Stan Ilieve12d7312017-12-04 14:48:27 -0500770 lattice.fRectTypes = nullptr;
771 lattice.fColors = nullptr;
Stan Ilievf50806a2016-10-24 10:40:39 -0400772 int numFlags = 0;
Stan Iliev021693b2016-10-17 16:26:15 -0400773 if (chunk.numColors > 0 && chunk.numColors == NinePatchUtils::NumDistinctRects(lattice)) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400774 // We can expect the framework to give us a color for every distinct rect.
775 // Skia requires a flag for every rect.
776 numFlags = (lattice.fXCount + 1) * (lattice.fYCount + 1);
777 }
778
Kevin Lubicka22c1302023-01-18 14:16:44 +0000779 // Most times, we do not have very many flags/colors, so the stack allocated part of
780 // FatVector will save us a heap allocation.
781 FatVector<SkCanvas::Lattice::RectType, 25> flags(numFlags);
782 FatVector<SkColor, 25> colors(numFlags);
Stan Ilievf50806a2016-10-24 10:40:39 -0400783 if (numFlags > 0) {
Kevin Lubicka22c1302023-01-18 14:16:44 +0000784 NinePatchUtils::SetLatticeFlags(&lattice, flags.data(), numFlags, chunk, colors.data());
Stan Ilievf50806a2016-10-24 10:40:39 -0400785 }
786
787 lattice.fBounds = nullptr;
788 SkRect dst = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Mike Reedc2dbc032019-07-25 12:28:29 -0400789 auto image = bitmap.makeImage();
Mike Reeda6cb58a2021-07-10 13:31:34 -0400790 applyLooper(paint, [&](const Paint& p) {
791 mCanvas->drawImageLattice(image.get(), lattice, dst, p.filterMode(), &p);
Mike Reedc2dbc032019-07-25 12:28:29 -0400792 });
Derek Sollenbergeredca3202015-07-10 13:56:39 -0400793}
794
Derek Sollenberger2d142132018-01-22 10:25:26 -0500795double SkiaCanvas::drawAnimatedImage(AnimatedImageDrawable* imgDrawable) {
796 return imgDrawable->drawStaging(mCanvas);
Leon Scroggins III671cce22018-01-14 16:52:17 -0500797}
798
Doris Liu766431a2016-02-04 22:17:11 +0000799void SkiaCanvas::drawVectorDrawable(VectorDrawableRoot* vectorDrawable) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800800 vectorDrawable->drawStaging(this);
Doris Liu766431a2016-02-04 22:17:11 +0000801}
802
Derek Sollenberger8872b382014-06-23 14:13:53 -0400803// ----------------------------------------------------------------------------
804// Canvas draw operations: Text
805// ----------------------------------------------------------------------------
806
Mike Reed2dfd55d2019-01-08 16:19:03 -0500807void SkiaCanvas::drawGlyphs(ReadGlyphFunc glyphFunc, int count, const Paint& paint, float x,
Seigo Nonaka63af7ba2020-09-21 23:07:43 -0700808 float y, float totalAdvance) {
Stan Iliev0b58d992017-03-30 18:22:27 -0400809 if (count <= 0 || paint.nothingToDraw()) return;
Mike Reedf6d86ac2019-01-18 14:13:23 -0500810 Paint paintCopy(paint);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400811 if (mPaintFilter) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500812 mPaintFilter->filterFullPaint(&paintCopy);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400813 }
Mike Reedf6d86ac2019-01-18 14:13:23 -0500814 const SkFont& font = paintCopy.getSkFont();
Stan Iliev7717e222018-02-05 18:04:11 -0500815 // Stroke with a hairline is drawn on HW with a fill style for compatibility with Android O and
816 // older.
John Recke170fb62018-05-07 08:12:07 -0700817 if (!mCanvasOwned && sApiLevel <= 27 && paintCopy.getStrokeWidth() <= 0 &&
818 paintCopy.getStyle() == SkPaint::kStroke_Style) {
Stan Iliev7717e222018-02-05 18:04:11 -0500819 paintCopy.setStyle(SkPaint::kFill_Style);
820 }
Stan Ilievf50806a2016-10-24 10:40:39 -0400821
Stan Ilievf50806a2016-10-24 10:40:39 -0400822 SkTextBlobBuilder builder;
Mike Reed2e204fc2019-01-28 13:31:36 -0500823 const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(font, count);
Stan Iliev0b58d992017-03-30 18:22:27 -0400824 glyphFunc(buffer.glyphs, buffer.pos);
Stan Ilievf50806a2016-10-24 10:40:39 -0400825
826 sk_sp<SkTextBlob> textBlob(builder.make());
Nathaniel Nifong52d37772020-01-10 16:12:41 -0500827
Mike Reedbb4cb482021-02-22 14:21:20 -0500828 applyLooper(&paintCopy, [&](const SkPaint& p) { mCanvas->drawTextBlob(textBlob, 0, 0, p); });
Seigo Nonakacd348c62023-09-12 16:05:44 +0900829 if (!text_feature::fix_double_underline()) {
830 drawTextDecorations(x, y, totalAdvance, paintCopy);
831 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400832}
833
Yuqian Liafc221492016-07-18 13:07:42 -0400834void SkiaCanvas::drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
Mike Reed2dfd55d2019-01-08 16:19:03 -0500835 const Paint& paint, const SkPath& path, size_t start,
John Reck1bcacfd2017-11-03 10:12:19 -0700836 size_t end) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500837 Paint paintCopy(paint);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400838 if (mPaintFilter) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500839 mPaintFilter->filterFullPaint(&paintCopy);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400840 }
Mike Reedf6d86ac2019-01-18 14:13:23 -0500841 const SkFont& font = paintCopy.getSkFont();
Derek Sollenberger415a74b2018-03-14 15:03:13 -0400842
Yuqian Liafc221492016-07-18 13:07:42 -0400843 const int N = end - start;
Mike Reed4a4b1be2019-01-01 15:43:06 -0500844 SkTextBlobBuilder builder;
845 auto rec = builder.allocRunRSXform(font, N);
846 SkRSXform* xform = (SkRSXform*)rec.pos;
847 uint16_t* glyphs = rec.glyphs;
Yuqian Liafc221492016-07-18 13:07:42 -0400848 SkPathMeasure meas(path, false);
849
850 for (size_t i = start; i < end; i++) {
851 glyphs[i - start] = layout.getGlyphId(i);
Stan Ilievc6c96dd2018-01-10 16:06:04 -0500852 float halfWidth = layout.getCharAdvance(i) * 0.5f;
853 float x = hOffset + layout.getX(i) + halfWidth;
Yuqian Liafc221492016-07-18 13:07:42 -0400854 float y = vOffset + layout.getY(i);
855
856 SkPoint pos;
857 SkVector tan;
858 if (!meas.getPosTan(x, &pos, &tan)) {
859 pos.set(x, y);
860 tan.set(1, 0);
861 }
862 xform[i - start].fSCos = tan.x();
863 xform[i - start].fSSin = tan.y();
Stan Ilievc6c96dd2018-01-10 16:06:04 -0500864 xform[i - start].fTx = pos.x() - tan.y() * y - halfWidth * tan.x();
865 xform[i - start].fTy = pos.y() + tan.x() * y - halfWidth * tan.y();
Yuqian Liafc221492016-07-18 13:07:42 -0400866 }
Leon Scroggins III950f2aa2020-04-29 13:46:00 -0400867
868 sk_sp<SkTextBlob> textBlob(builder.make());
869
Mike Reedbb4cb482021-02-22 14:21:20 -0500870 applyLooper(&paintCopy, [&](const SkPaint& p) { mCanvas->drawTextBlob(textBlob, 0, 0, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400871}
872
Derek Sollenberger6f485562015-07-30 10:00:39 -0400873// ----------------------------------------------------------------------------
874// Canvas draw operations: Animations
875// ----------------------------------------------------------------------------
876
Derek Sollenberger6f485562015-07-30 10:00:39 -0400877void SkiaCanvas::drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
John Reck1bcacfd2017-11-03 10:12:19 -0700878 uirenderer::CanvasPropertyPrimitive* top,
879 uirenderer::CanvasPropertyPrimitive* right,
880 uirenderer::CanvasPropertyPrimitive* bottom,
881 uirenderer::CanvasPropertyPrimitive* rx,
882 uirenderer::CanvasPropertyPrimitive* ry,
883 uirenderer::CanvasPropertyPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -0400884 sk_sp<uirenderer::skiapipeline::AnimatedRoundRect> drawable(
John Reck1bcacfd2017-11-03 10:12:19 -0700885 new uirenderer::skiapipeline::AnimatedRoundRect(left, top, right, bottom, rx, ry,
886 paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400887 mCanvas->drawDrawable(drawable.get());
888}
889
John Reck1bcacfd2017-11-03 10:12:19 -0700890void SkiaCanvas::drawCircle(uirenderer::CanvasPropertyPrimitive* x,
891 uirenderer::CanvasPropertyPrimitive* y,
892 uirenderer::CanvasPropertyPrimitive* radius,
893 uirenderer::CanvasPropertyPaint* paint) {
894 sk_sp<uirenderer::skiapipeline::AnimatedCircle> drawable(
895 new uirenderer::skiapipeline::AnimatedCircle(x, y, radius, paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400896 mCanvas->drawDrawable(drawable.get());
897}
898
Lucas Dupin00af5272021-04-29 20:30:01 -0700899void SkiaCanvas::drawRipple(const uirenderer::skiapipeline::RippleDrawableParams& params) {
900 uirenderer::skiapipeline::AnimatedRippleDrawable::draw(mCanvas, params);
Derek Sollenbergerdf301aa2020-12-17 11:06:03 -0500901}
902
John Reck894e85a2019-07-15 16:16:44 -0700903void SkiaCanvas::drawPicture(const SkPicture& picture) {
904 // TODO: Change to mCanvas->drawPicture()? SkCanvas::drawPicture seems to be
905 // where the logic is for playback vs. ref picture. Using picture.playback here
906 // to stay behavior-identical for now, but should revisit this at some point.
907 picture.playback(mCanvas);
908}
909
Derek Sollenberger6f485562015-07-30 10:00:39 -0400910// ----------------------------------------------------------------------------
911// Canvas draw operations: View System
912// ----------------------------------------------------------------------------
913
Stan Ilievf50806a2016-10-24 10:40:39 -0400914void SkiaCanvas::drawLayer(uirenderer::DeferredLayerUpdater* layerUpdater) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400915 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw Layers");
916}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400917
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400918void SkiaCanvas::drawRenderNode(uirenderer::RenderNode* renderNode) {
919 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw RenderNodes");
920}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400921
John Reck1bcacfd2017-11-03 10:12:19 -0700922} // namespace android