blob: 9e825fb350d6816bfa61dd6d7c13291d229f9c3a [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) {}
Jorge Betancourt3700b102024-02-01 17:38:31 +000073 Clip(const sk_sp<SkShader> shader, SkClipOp op, const SkMatrix& m)
74 : mType(Type::Shader), mOp(op), mMatrix(m), mShader(shader) {}
Ryan Prichard39971552022-08-26 20:53:33 -070075
76 void apply(SkCanvas* canvas) const {
77 canvas->setMatrix(mMatrix);
78 switch (mType) {
79 case Type::Rect:
80 // Don't anti-alias rectangular clips
81 canvas->clipRect(mRRect.rect(), mOp, false);
82 break;
83 case Type::RRect:
84 // Ensure rounded rectangular clips are anti-aliased
85 canvas->clipRRect(mRRect, mOp, true);
86 break;
87 case Type::Path:
88 // Ensure path clips are anti-aliased
89 canvas->clipPath(mPath.value(), mOp, true);
90 break;
Jorge Betancourt3700b102024-02-01 17:38:31 +000091 case Type::Shader:
92 canvas->clipShader(mShader, mOp);
Ryan Prichard39971552022-08-26 20:53:33 -070093 }
94 }
95
96private:
97 enum class Type {
98 Rect,
99 RRect,
100 Path,
Jorge Betancourt3700b102024-02-01 17:38:31 +0000101 Shader,
Ryan Prichard39971552022-08-26 20:53:33 -0700102 };
103
104 Type mType;
105 SkClipOp mOp;
106 SkMatrix mMatrix;
107
108 // These are logically a union (tracked separately due to non-POD path).
109 std::optional<SkPath> mPath;
110 SkRRect mRRect;
Jorge Betancourt3700b102024-02-01 17:38:31 +0000111 sk_sp<SkShader> mShader;
Ryan Prichard39971552022-08-26 20:53:33 -0700112};
113
John Reckc1b33d62015-04-22 09:04:45 -0700114Canvas* Canvas::create_canvas(const SkBitmap& bitmap) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400115 return new SkiaCanvas(bitmap);
116}
117
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400118Canvas* Canvas::create_canvas(SkCanvas* skiaCanvas) {
119 return new SkiaCanvas(skiaCanvas);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400120}
121
Stan Ilievf50806a2016-10-24 10:40:39 -0400122SkiaCanvas::SkiaCanvas() {}
123
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400124SkiaCanvas::SkiaCanvas(SkCanvas* canvas) : mCanvas(canvas) {}
Stan Ilievf50806a2016-10-24 10:40:39 -0400125
John Reckc1b33d62015-04-22 09:04:45 -0700126SkiaCanvas::SkiaCanvas(const SkBitmap& bitmap) {
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400127 mCanvasOwned = std::unique_ptr<SkCanvas>(new SkCanvas(bitmap));
128 mCanvas = mCanvasOwned.get();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400129}
130
Stan Iliev021693b2016-10-17 16:26:15 -0400131SkiaCanvas::~SkiaCanvas() {}
132
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400133void SkiaCanvas::reset(SkCanvas* skiaCanvas) {
Mike Reed6acfe162016-11-18 17:21:09 -0500134 if (mCanvas != skiaCanvas) {
135 mCanvas = skiaCanvas;
136 mCanvasOwned.reset();
137 }
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400138 mSaveStack.reset(nullptr);
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400139}
140
Derek Sollenberger8872b382014-06-23 14:13:53 -0400141// ----------------------------------------------------------------------------
142// Canvas state operations: Replace Bitmap
143// ----------------------------------------------------------------------------
144
John Reckc1b33d62015-04-22 09:04:45 -0700145void SkiaCanvas::setBitmap(const SkBitmap& bitmap) {
Tony Mantler4f641d12017-03-14 22:36:14 +0000146 // deletes the previously owned canvas (if any)
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400147 mCanvasOwned.reset(new SkCanvas(bitmap));
148 mCanvas = mCanvasOwned.get();
Tony Mantler4f641d12017-03-14 22:36:14 +0000149
Derek Sollenberger8872b382014-06-23 14:13:53 -0400150 // clean up the old save stack
Stan Ilievf50806a2016-10-24 10:40:39 -0400151 mSaveStack.reset(nullptr);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400152}
153
154// ----------------------------------------------------------------------------
155// Canvas state operations
156// ----------------------------------------------------------------------------
157
158bool SkiaCanvas::isOpaque() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400159 return mCanvas->imageInfo().isOpaque();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400160}
161
162int SkiaCanvas::width() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400163 return mCanvas->imageInfo().width();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400164}
165
166int SkiaCanvas::height() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400167 return mCanvas->imageInfo().height();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400168}
169
170// ----------------------------------------------------------------------------
171// Canvas state operations: Save (layer)
172// ----------------------------------------------------------------------------
173
174int SkiaCanvas::getSaveCount() const {
175 return mCanvas->getSaveCount();
176}
177
Florin Malitaeecff562015-12-21 10:43:01 -0500178int SkiaCanvas::save(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400179 int count = mCanvas->save();
180 recordPartialSave(flags);
181 return count;
182}
183
Florin Malita5e271402015-11-04 14:36:02 -0500184// The SkiaCanvas::restore operation layers on the capability to preserve
185// either (or both) the matrix and/or clip state after a SkCanvas::restore
186// operation. It does this by explicitly saving off the clip & matrix state
187// when requested and playing it back after the SkCanvas::restore.
Derek Sollenberger8872b382014-06-23 14:13:53 -0400188void SkiaCanvas::restore() {
Kevin Lubickb3731212023-01-05 19:52:09 +0000189 const SaveRec* rec = this->currentSaveRec();
Stan Ilievf50806a2016-10-24 10:40:39 -0400190 if (!rec) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400191 // Fast path - no record for this frame.
192 mCanvas->restore();
193 return;
194 }
195
Florin Malitaeecff562015-12-21 10:43:01 -0500196 bool preserveMatrix = !(rec->saveFlags & SaveFlags::Matrix);
John Reck1bcacfd2017-11-03 10:12:19 -0700197 bool preserveClip = !(rec->saveFlags & SaveFlags::Clip);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400198
199 SkMatrix savedMatrix;
200 if (preserveMatrix) {
201 savedMatrix = mCanvas->getTotalMatrix();
202 }
203
Stan Ilievf50806a2016-10-24 10:40:39 -0400204 const size_t clipIndex = rec->clipIndex;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400205
206 mCanvas->restore();
Stan Ilievf50806a2016-10-24 10:40:39 -0400207 mSaveStack->pop_back();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400208
209 if (preserveMatrix) {
210 mCanvas->setMatrix(savedMatrix);
211 }
212
Stan Ilievf50806a2016-10-24 10:40:39 -0400213 if (preserveClip) {
214 this->applyPersistentClips(clipIndex);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400215 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400216}
217
218void SkiaCanvas::restoreToCount(int restoreCount) {
219 while (mCanvas->getSaveCount() > restoreCount) {
220 this->restore();
221 }
222}
223
John Recka00eef212020-11-16 12:45:55 -0500224int SkiaCanvas::saveLayer(float left, float top, float right, float bottom, const SkPaint* paint) {
Florin Malitaeecff562015-12-21 10:43:01 -0500225 const SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
John Recka00eef212020-11-16 12:45:55 -0500226 const SkCanvas::SaveLayerRec rec(&bounds, paint);
Florin Malitaeecff562015-12-21 10:43:01 -0500227
Stan Iliev68885e32016-12-14 11:18:34 -0500228 return mCanvas->saveLayer(rec);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400229}
230
John Recka00eef212020-11-16 12:45:55 -0500231int SkiaCanvas::saveLayerAlpha(float left, float top, float right, float bottom, int alpha) {
Florin Malitaeecff562015-12-21 10:43:01 -0500232 if (static_cast<unsigned>(alpha) < 0xFF) {
Yuqian Lifd92ee42016-04-27 17:03:38 -0400233 SkPaint alphaPaint;
234 alphaPaint.setAlpha(alpha);
John Recka00eef212020-11-16 12:45:55 -0500235 return this->saveLayer(left, top, right, bottom, &alphaPaint);
Florin Malitaeecff562015-12-21 10:43:01 -0500236 }
John Recka00eef212020-11-16 12:45:55 -0500237 return this->saveLayer(left, top, right, bottom, nullptr);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400238}
239
Derek Sollenberger24fc9012018-12-07 14:12:12 -0500240int SkiaCanvas::saveUnclippedLayer(int left, int top, int right, int bottom) {
241 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
242 return SkAndroidFrameworkUtils::SaveBehind(mCanvas, &bounds);
243}
244
Mike Reeda6cb58a2021-07-10 13:31:34 -0400245void SkiaCanvas::restoreUnclippedLayer(int restoreCount, const Paint& paint) {
Derek Sollenbergerac33a482019-04-22 16:28:09 -0400246
247 while (mCanvas->getSaveCount() > restoreCount + 1) {
248 this->restore();
249 }
250
251 if (mCanvas->getSaveCount() == restoreCount + 1) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500252 SkCanvasPriv::DrawBehind(mCanvas, filterPaint(paint));
Derek Sollenbergerac33a482019-04-22 16:28:09 -0400253 this->restore();
254 }
255}
256
Stan Ilievf50806a2016-10-24 10:40:39 -0400257const SkiaCanvas::SaveRec* SkiaCanvas::currentSaveRec() const {
Kevin Lubickb3731212023-01-05 19:52:09 +0000258 const SaveRec* rec = (mSaveStack && !mSaveStack->empty())
259 ? static_cast<const SaveRec*>(&mSaveStack->back())
260 : nullptr;
Stan Ilievf50806a2016-10-24 10:40:39 -0400261 int currentSaveCount = mCanvas->getSaveCount();
Kevin Lubicka22c1302023-01-18 14:16:44 +0000262 LOG_FATAL_IF(!(!rec || currentSaveCount >= rec->saveCount));
Stan Ilievf50806a2016-10-24 10:40:39 -0400263
264 return (rec && rec->saveCount == currentSaveCount) ? rec : nullptr;
265}
266
Alec Mouri655a5e42022-09-12 17:49:17 +0000267void SkiaCanvas::punchHole(const SkRRect& rect, float alpha) {
Nader Jawad2dc632a2021-03-29 18:51:29 -0700268 SkPaint paint = SkPaint();
Alec Mouri655a5e42022-09-12 17:49:17 +0000269 paint.setColor(SkColors::kBlack);
270 paint.setAlphaf(alpha);
271 paint.setBlendMode(SkBlendMode::kDstOut);
Nader Jawad2dc632a2021-03-29 18:51:29 -0700272 mCanvas->drawRRect(rect, paint);
273}
274
Derek Sollenberger8872b382014-06-23 14:13:53 -0400275// ----------------------------------------------------------------------------
276// functions to emulate legacy SaveFlags (i.e. independent matrix/clip flags)
277// ----------------------------------------------------------------------------
278
Florin Malitaeecff562015-12-21 10:43:01 -0500279void SkiaCanvas::recordPartialSave(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400280 // A partial save is a save operation which doesn't capture the full canvas state.
Florin Malitaeecff562015-12-21 10:43:01 -0500281 // (either SaveFlags::Matrix or SaveFlags::Clip is missing).
Derek Sollenberger8872b382014-06-23 14:13:53 -0400282
283 // Mask-out non canvas state bits.
Florin Malitaeecff562015-12-21 10:43:01 -0500284 flags &= SaveFlags::MatrixClip;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400285
Florin Malitaeecff562015-12-21 10:43:01 -0500286 if (flags == SaveFlags::MatrixClip) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400287 // not a partial save.
288 return;
289 }
290
Stan Ilievf50806a2016-10-24 10:40:39 -0400291 if (!mSaveStack) {
Kevin Lubickb3731212023-01-05 19:52:09 +0000292 mSaveStack.reset(new std::deque<SaveRec>());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400293 }
294
Kevin Lubickb3731212023-01-05 19:52:09 +0000295 mSaveStack->emplace_back(mCanvas->getSaveCount(), // saveCount
296 flags, // saveFlags
297 mClipStack.size()); // clipIndex
Derek Sollenberger8872b382014-06-23 14:13:53 -0400298}
299
Stan Ilievf50806a2016-10-24 10:40:39 -0400300template <typename T>
Mike Reed6e49c9f2016-12-02 15:36:59 -0500301void SkiaCanvas::recordClip(const T& clip, SkClipOp op) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400302 // Only need tracking when in a partial save frame which
303 // doesn't restore the clip.
304 const SaveRec* rec = this->currentSaveRec();
305 if (rec && !(rec->saveFlags & SaveFlags::Clip)) {
306 mClipStack.emplace_back(clip, op, mCanvas->getTotalMatrix());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400307 }
308}
309
Stan Ilievf50806a2016-10-24 10:40:39 -0400310// Applies and optionally removes all clips >= index.
311void SkiaCanvas::applyPersistentClips(size_t clipStartIndex) {
Kevin Lubicka22c1302023-01-18 14:16:44 +0000312 LOG_FATAL_IF(clipStartIndex > mClipStack.size());
Stan Ilievf50806a2016-10-24 10:40:39 -0400313 const auto begin = mClipStack.cbegin() + clipStartIndex;
314 const auto end = mClipStack.cend();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400315
Stan Ilievf50806a2016-10-24 10:40:39 -0400316 // Clip application mutates the CTM.
317 const SkMatrix saveMatrix = mCanvas->getTotalMatrix();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400318
Stan Ilievf50806a2016-10-24 10:40:39 -0400319 for (auto clip = begin; clip != end; ++clip) {
Mike Reed6acfe162016-11-18 17:21:09 -0500320 clip->apply(mCanvas);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400321 }
322
Stan Ilievf50806a2016-10-24 10:40:39 -0400323 mCanvas->setMatrix(saveMatrix);
324
325 // If the current/post-restore save rec is also persisting clips, we
326 // leave them on the stack to be reapplied part of the next restore().
327 // Otherwise we're done and just pop them.
Kevin Lubickb3731212023-01-05 19:52:09 +0000328 const SaveRec* rec = this->currentSaveRec();
Stan Ilievf50806a2016-10-24 10:40:39 -0400329 if (!rec || (rec->saveFlags & SaveFlags::Clip)) {
330 mClipStack.erase(begin, end);
331 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400332}
333
334// ----------------------------------------------------------------------------
335// Canvas state operations: Matrix
336// ----------------------------------------------------------------------------
337
338void SkiaCanvas::getMatrix(SkMatrix* outMatrix) const {
339 *outMatrix = mCanvas->getTotalMatrix();
340}
341
342void SkiaCanvas::setMatrix(const SkMatrix& matrix) {
343 mCanvas->setMatrix(matrix);
344}
345
346void SkiaCanvas::concat(const SkMatrix& matrix) {
347 mCanvas->concat(matrix);
348}
349
Jorge Betancourtc9806fadcc2023-12-04 15:30:18 -0500350void SkiaCanvas::concat(const SkM44& matrix) {
351 mCanvas->concat(matrix);
352}
353
Derek Sollenberger8872b382014-06-23 14:13:53 -0400354void SkiaCanvas::rotate(float degrees) {
355 mCanvas->rotate(degrees);
356}
357
358void SkiaCanvas::scale(float sx, float sy) {
359 mCanvas->scale(sx, sy);
360}
361
362void SkiaCanvas::skew(float sx, float sy) {
363 mCanvas->skew(sx, sy);
364}
365
366void SkiaCanvas::translate(float dx, float dy) {
367 mCanvas->translate(dx, dy);
368}
369
370// ----------------------------------------------------------------------------
371// Canvas state operations: Clips
372// ----------------------------------------------------------------------------
373
374// This function is a mirror of SkCanvas::getClipBounds except that it does
375// not outset the edge of the clip to account for anti-aliasing. There is
376// a skia bug to investigate pushing this logic into back into skia.
377// (see https://code.google.com/p/skia/issues/detail?id=1303)
378bool SkiaCanvas::getClipBounds(SkRect* outRect) const {
379 SkIRect ibounds;
Mike Reed5e438982017-01-25 08:23:25 -0500380 if (!mCanvas->getDeviceClipBounds(&ibounds)) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400381 return false;
382 }
383
384 SkMatrix inverse;
385 // if we can't invert the CTM, we can't return local clip bounds
386 if (!mCanvas->getTotalMatrix().invert(&inverse)) {
387 if (outRect) {
388 outRect->setEmpty();
389 }
390 return false;
391 }
392
393 if (NULL != outRect) {
394 SkRect r = SkRect::Make(ibounds);
395 inverse.mapRect(outRect, r);
396 }
397 return true;
398}
399
400bool SkiaCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
401 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
402 return mCanvas->quickReject(bounds);
403}
404
405bool SkiaCanvas::quickRejectPath(const SkPath& path) const {
406 return mCanvas->quickReject(path);
407}
408
Mike Reed6e49c9f2016-12-02 15:36:59 -0500409bool SkiaCanvas::clipRect(float left, float top, float right, float bottom, SkClipOp op) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400410 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Stan Ilievf50806a2016-10-24 10:40:39 -0400411 this->recordClip(rect, op);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400412 mCanvas->clipRect(rect, op);
Chris Craik5ec6a282015-06-23 15:42:12 -0700413 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400414}
415
Mike Reed6e49c9f2016-12-02 15:36:59 -0500416bool SkiaCanvas::clipPath(const SkPath* path, SkClipOp op) {
Derek Sollenbergerf7d98f42017-04-17 11:27:36 -0400417 this->recordClip(*path, op);
Nader Jawade431e312019-12-04 14:13:18 -0800418 mCanvas->clipPath(*path, op, true);
Chris Craik5ec6a282015-06-23 15:42:12 -0700419 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400420}
421
Jorge Betancourt3700b102024-02-01 17:38:31 +0000422void SkiaCanvas::clipShader(sk_sp<SkShader> shader, SkClipOp op) {
423 this->recordClip(shader, op);
424 mCanvas->clipShader(shader, op);
425}
426
Michael Ludwig70cf50c22021-07-21 17:02:39 +0000427bool SkiaCanvas::replaceClipRect_deprecated(float left, float top, float right, float bottom) {
428 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
429
430 // Emulated clip rects are not recorded for partial saves, since
431 // partial saves have been removed from the public API.
432 SkAndroidFrameworkUtils::ResetClip(mCanvas);
433 mCanvas->clipRect(rect, SkClipOp::kIntersect);
434 return !mCanvas->isClipEmpty();
435}
436
437bool SkiaCanvas::replaceClipPath_deprecated(const SkPath* path) {
438 SkAndroidFrameworkUtils::ResetClip(mCanvas);
439 mCanvas->clipPath(*path, SkClipOp::kIntersect, true);
440 return !mCanvas->isClipEmpty();
441}
442
Derek Sollenberger8872b382014-06-23 14:13:53 -0400443// ----------------------------------------------------------------------------
444// Canvas state operations: Filters
445// ----------------------------------------------------------------------------
446
Ben Wagner0ed10be2018-06-28 17:08:16 -0400447PaintFilter* SkiaCanvas::getPaintFilter() {
448 return mPaintFilter.get();
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400449}
450
Ben Wagner0ed10be2018-06-28 17:08:16 -0400451void SkiaCanvas::setPaintFilter(sk_sp<PaintFilter> paintFilter) {
452 mPaintFilter = std::move(paintFilter);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400453}
454
455// ----------------------------------------------------------------------------
Matt Sarettd0814db2017-04-13 09:33:18 -0400456// Canvas state operations: Capture
457// ----------------------------------------------------------------------------
458
459SkCanvasState* SkiaCanvas::captureCanvasState() const {
460 SkCanvas* canvas = mCanvas;
461 if (mCanvasOwned) {
462 // Important to use the underlying SkCanvas, not the wrapper.
463 canvas = mCanvasOwned.get();
464 }
465
466 // Workarounds for http://crbug.com/271096: SW draw only supports
467 // translate & scale transforms, and a simple rectangular clip.
468 // (This also avoids significant wasted time in calling
469 // SkCanvasStateUtils::CaptureCanvasState when the clip is complex).
John Reck1bcacfd2017-11-03 10:12:19 -0700470 if (!canvas->isClipRect() || (canvas->getTotalMatrix().getType() &
471 ~(SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask))) {
472 return nullptr;
Matt Sarettd0814db2017-04-13 09:33:18 -0400473 }
474
475 return SkCanvasStateUtils::CaptureCanvasState(canvas);
476}
477
478// ----------------------------------------------------------------------------
Derek Sollenberger8872b382014-06-23 14:13:53 -0400479// Canvas draw operations
480// ----------------------------------------------------------------------------
481
Mike Reed260ab722016-10-07 15:59:20 -0400482void SkiaCanvas::drawColor(int color, SkBlendMode mode) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400483 mCanvas->drawColor(color, mode);
484}
485
Mike Reeda6cb58a2021-07-10 13:31:34 -0400486void SkiaCanvas::onFilterPaint(Paint& paint) {
Ben Wagner0ed10be2018-06-28 17:08:16 -0400487 if (mPaintFilter) {
Mike Reeda6cb58a2021-07-10 13:31:34 -0400488 mPaintFilter->filterFullPaint(&paint);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400489 }
Ben Wagner0ed10be2018-06-28 17:08:16 -0400490}
491
Mike Reeda6cb58a2021-07-10 13:31:34 -0400492void SkiaCanvas::drawPaint(const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500493 mCanvas->drawPaint(filterPaint(paint));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400494}
495
496// ----------------------------------------------------------------------------
497// Canvas draw operations: Geometry
498// ----------------------------------------------------------------------------
499
Mike Reedc2dbc032019-07-25 12:28:29 -0400500void SkiaCanvas::drawPoints(const float* points, int count, const Paint& paint,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400501 SkCanvas::PointMode mode) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500502 if (CC_UNLIKELY(count < 2 || paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400503 // convert the floats into SkPoints
John Reck1bcacfd2017-11-03 10:12:19 -0700504 count >>= 1; // now it is the number of points
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400505 std::unique_ptr<SkPoint[]> pts(new SkPoint[count]);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400506 for (int i = 0; i < count; i++) {
507 pts[i].set(points[0], points[1]);
508 points += 2;
509 }
Mike Reedc2dbc032019-07-25 12:28:29 -0400510
Mike Reedbb4cb482021-02-22 14:21:20 -0500511 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawPoints(mode, count, pts.get(), p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400512}
513
Mike Reedc2dbc032019-07-25 12:28:29 -0400514void SkiaCanvas::drawPoint(float x, float y, const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500515 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawPoint(x, y, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400516}
517
Mike Reedc2dbc032019-07-25 12:28:29 -0400518void SkiaCanvas::drawPoints(const float* points, int count, const Paint& paint) {
519 this->drawPoints(points, count, paint, SkCanvas::kPoints_PointMode);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400520}
521
522void SkiaCanvas::drawLine(float startX, float startY, float stopX, float stopY,
Mike Reedc2dbc032019-07-25 12:28:29 -0400523 const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500524 applyLooper(&paint,
525 [&](const SkPaint& p) { mCanvas->drawLine(startX, startY, stopX, stopY, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400526}
527
Mike Reedc2dbc032019-07-25 12:28:29 -0400528void SkiaCanvas::drawLines(const float* points, int count, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500529 if (CC_UNLIKELY(count < 4 || paint.nothingToDraw())) return;
Mike Reedc2dbc032019-07-25 12:28:29 -0400530 this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400531}
532
Mike Reedc2dbc032019-07-25 12:28:29 -0400533void SkiaCanvas::drawRect(float left, float top, float right, float bottom, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500534 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Mike Reedbb4cb482021-02-22 14:21:20 -0500535 applyLooper(&paint, [&](const SkPaint& p) {
Mike Reedc2dbc032019-07-25 12:28:29 -0400536 mCanvas->drawRect({left, top, right, bottom}, p);
537 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400538}
539
Mike Reedc2dbc032019-07-25 12:28:29 -0400540void SkiaCanvas::drawRegion(const SkRegion& region, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500541 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Mike Reedbb4cb482021-02-22 14:21:20 -0500542 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawRegion(region, p); });
Derek Sollenberger94394b32015-07-10 09:58:41 -0400543}
544
John Reck1bcacfd2017-11-03 10:12:19 -0700545void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom, float rx, float ry,
Mike Reedc2dbc032019-07-25 12:28:29 -0400546 const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500547 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400548 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Mike Reedbb4cb482021-02-22 14:21:20 -0500549 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawRoundRect(rect, rx, ry, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400550}
551
Nader Jawadadfe1d92018-09-27 12:27:36 -0700552void SkiaCanvas::drawDoubleRoundRect(const SkRRect& outer, const SkRRect& inner,
Mike Reedc2dbc032019-07-25 12:28:29 -0400553 const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500554 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawDRRect(outer, inner, p); });
Nader Jawadadfe1d92018-09-27 12:27:36 -0700555}
556
Mike Reedc2dbc032019-07-25 12:28:29 -0400557void SkiaCanvas::drawCircle(float x, float y, float radius, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500558 if (CC_UNLIKELY(radius <= 0 || paint.nothingToDraw())) return;
Mike Reedbb4cb482021-02-22 14:21:20 -0500559 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawCircle(x, y, radius, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400560}
561
Mike Reedc2dbc032019-07-25 12:28:29 -0400562void SkiaCanvas::drawOval(float left, float top, float right, float bottom, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500563 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400564 SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
Mike Reedbb4cb482021-02-22 14:21:20 -0500565 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawOval(oval, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400566}
567
John Reck1bcacfd2017-11-03 10:12:19 -0700568void SkiaCanvas::drawArc(float left, float top, float right, float bottom, float startAngle,
Mike Reedc2dbc032019-07-25 12:28:29 -0400569 float sweepAngle, bool useCenter, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500570 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400571 SkRect arc = SkRect::MakeLTRB(left, top, right, bottom);
Mike Reedbb4cb482021-02-22 14:21:20 -0500572 applyLooper(&paint, [&](const SkPaint& p) {
Mike Reedc2dbc032019-07-25 12:28:29 -0400573 if (fabs(sweepAngle) >= 360.0f) {
574 mCanvas->drawOval(arc, p);
575 } else {
576 mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, p);
577 }
578 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400579}
580
Mike Reedc2dbc032019-07-25 12:28:29 -0400581void SkiaCanvas::drawPath(const SkPath& path, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500582 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Stan Iliev6dcfdec2017-08-15 16:42:05 -0400583 if (CC_UNLIKELY(path.isEmpty() && (!path.isInverseFillType()))) {
584 return;
585 }
Mike Reedbb4cb482021-02-22 14:21:20 -0500586 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawPath(path, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400587}
588
Mike Reedc2dbc032019-07-25 12:28:29 -0400589void SkiaCanvas::drawVertices(const SkVertices* vertices, SkBlendMode mode, const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500590 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawVertices(vertices, mode, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400591}
592
Nader Jawad5f0a8002023-02-21 17:00:51 -0800593void SkiaCanvas::drawMesh(const Mesh& mesh, sk_sp<SkBlender> blender, const Paint& paint) {
594 GrDirectContext* context = nullptr;
595 auto recordingContext = mCanvas->recordingContext();
596 if (recordingContext) {
597 context = recordingContext->asDirectContext();
598 }
Alex Strelnikovb58afe62024-03-07 20:38:10 +0000599 mesh.refBufferData()->updateBuffers(context);
600 mCanvas->drawMesh(mesh.takeSnapshot().getSkMesh(), blender, paint);
Angel Aguayo90c46ee2022-11-08 23:42:09 +0000601}
602
Derek Sollenberger8872b382014-06-23 14:13:53 -0400603// ----------------------------------------------------------------------------
604// Canvas draw operations: Bitmaps
605// ----------------------------------------------------------------------------
606
John Reck859af0d2023-11-15 12:40:39 -0500607bool SkiaCanvas::useGainmapShader(Bitmap& bitmap) {
608 // If the bitmap doesn't have a gainmap, don't use the gainmap shader
609 if (!bitmap.hasGainmap()) return false;
610
611 // If we don't have an owned canvas, then we're either hardware accelerated or drawing
612 // to a picture - use the gainmap shader out of caution. Ideally a picture canvas would
613 // use a drawable here instead to defer making that decision until the last possible
614 // moment
615 if (!mCanvasOwned) return true;
616
617 auto info = mCanvasOwned->imageInfo();
618
619 // If it's an unknown colortype then it's not a bitmap-backed canvas
620 if (info.colorType() == SkColorType::kUnknown_SkColorType) return true;
621
622 skcms_TransferFunction tfn;
623 info.colorSpace()->transferFn(&tfn);
624
625 auto transferType = skcms_TransferFunction_getType(&tfn);
626 switch (transferType) {
627 case skcms_TFType_HLGish:
628 case skcms_TFType_HLGinvish:
629 case skcms_TFType_PQish:
630 return true;
631 case skcms_TFType_Invalid:
632 case skcms_TFType_sRGBish:
633 return false;
634 }
635}
636
Mike Reedc2dbc032019-07-25 12:28:29 -0400637void SkiaCanvas::drawBitmap(Bitmap& bitmap, float left, float top, const Paint* paint) {
638 auto image = bitmap.makeImage();
John Reckb2a4b932023-03-29 17:01:25 -0400639
John Reck859af0d2023-11-15 12:40:39 -0500640 if (useGainmapShader(bitmap)) {
John Reckb2a4b932023-03-29 17:01:25 -0400641 Paint gainmapPaint = paint ? *paint : Paint();
642 sk_sp<SkShader> gainmapShader = uirenderer::MakeGainmapShader(
643 image, bitmap.gainmap()->bitmap->makeImage(), bitmap.gainmap()->info,
644 SkTileMode::kClamp, SkTileMode::kClamp, gainmapPaint.sampling());
645 gainmapPaint.setShader(gainmapShader);
646 return drawRect(left, top, left + bitmap.width(), top + bitmap.height(), gainmapPaint);
647 }
648
Mike Reeda6cb58a2021-07-10 13:31:34 -0400649 applyLooper(paint, [&](const Paint& p) {
650 mCanvas->drawImage(image, left, top, p.sampling(), &p);
Mike Reedc2dbc032019-07-25 12:28:29 -0400651 });
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400652}
653
Mike Reedc2dbc032019-07-25 12:28:29 -0400654void SkiaCanvas::drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const Paint* paint) {
Mike Reed6acfe162016-11-18 17:21:09 -0500655 SkAutoCanvasRestore acr(mCanvas, true);
Mike Reed70ffbf92014-12-08 17:03:30 -0500656 mCanvas->concat(matrix);
John Reckb2a4b932023-03-29 17:01:25 -0400657 drawBitmap(bitmap, 0, 0, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400658}
659
John Reck1bcacfd2017-11-03 10:12:19 -0700660void SkiaCanvas::drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight,
661 float srcBottom, float dstLeft, float dstTop, float dstRight,
Mike Reedc2dbc032019-07-25 12:28:29 -0400662 float dstBottom, const Paint* paint) {
663 auto image = bitmap.makeImage();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400664 SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
665 SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400666
John Reck859af0d2023-11-15 12:40:39 -0500667 if (useGainmapShader(bitmap)) {
John Reckb2a4b932023-03-29 17:01:25 -0400668 Paint gainmapPaint = paint ? *paint : Paint();
669 sk_sp<SkShader> gainmapShader = uirenderer::MakeGainmapShader(
670 image, bitmap.gainmap()->bitmap->makeImage(), bitmap.gainmap()->info,
671 SkTileMode::kClamp, SkTileMode::kClamp, gainmapPaint.sampling());
672 gainmapShader = gainmapShader->makeWithLocalMatrix(SkMatrix::RectToRect(srcRect, dstRect));
673 gainmapPaint.setShader(gainmapShader);
674 return drawRect(dstLeft, dstTop, dstRight, dstBottom, gainmapPaint);
675 }
676
Mike Reeda6cb58a2021-07-10 13:31:34 -0400677 applyLooper(paint, [&](const Paint& p) {
678 mCanvas->drawImageRect(image, srcRect, dstRect, p.sampling(), &p,
Mike Reed7994a312021-01-28 18:06:26 -0500679 SkCanvas::kFast_SrcRectConstraint);
Mike Reedc2dbc032019-07-25 12:28:29 -0400680 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400681}
682
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400683void SkiaCanvas::drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight,
Mike Reedc2dbc032019-07-25 12:28:29 -0400684 const float* vertices, const int* colors, const Paint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400685 const int ptCount = (meshWidth + 1) * (meshHeight + 1);
686 const int indexCount = meshWidth * meshHeight * 6;
Mike Reed871cd2d2017-03-17 10:15:52 -0400687 uint32_t flags = SkVertices::kHasTexCoords_BuilderFlag;
688 if (colors) {
689 flags |= SkVertices::kHasColors_BuilderFlag;
690 }
Mike Reed826deef2017-04-04 15:32:04 -0400691 SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, ptCount, indexCount, flags);
Mike Reed871cd2d2017-03-17 10:15:52 -0400692 memcpy(builder.positions(), vertices, ptCount * sizeof(SkPoint));
693 if (colors) {
694 memcpy(builder.colors(), colors, ptCount * sizeof(SkColor));
695 }
696 SkPoint* texs = builder.texCoords();
697 uint16_t* indices = builder.indices();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400698
699 // cons up texture coordinates and indices
700 {
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400701 const SkScalar w = SkIntToScalar(bitmap.width());
702 const SkScalar h = SkIntToScalar(bitmap.height());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400703 const SkScalar dx = w / meshWidth;
704 const SkScalar dy = h / meshHeight;
705
706 SkPoint* texsPtr = texs;
707 SkScalar y = 0;
708 for (int i = 0; i <= meshHeight; i++) {
709 if (i == meshHeight) {
710 y = h; // to ensure numerically we hit h exactly
711 }
712 SkScalar x = 0;
713 for (int j = 0; j < meshWidth; j++) {
714 texsPtr->set(x, y);
715 texsPtr += 1;
716 x += dx;
717 }
718 texsPtr->set(w, y);
719 texsPtr += 1;
720 y += dy;
721 }
Kevin Lubicka22c1302023-01-18 14:16:44 +0000722 LOG_FATAL_IF((texsPtr - texs) != ptCount);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400723 }
724
725 // cons up indices
726 {
727 uint16_t* indexPtr = indices;
728 int index = 0;
729 for (int i = 0; i < meshHeight; i++) {
730 for (int j = 0; j < meshWidth; j++) {
731 // lower-left triangle
732 *indexPtr++ = index;
733 *indexPtr++ = index + meshWidth + 1;
734 *indexPtr++ = index + meshWidth + 2;
735 // upper-right triangle
736 *indexPtr++ = index;
737 *indexPtr++ = index + meshWidth + 2;
738 *indexPtr++ = index + 1;
739 // bump to the next cell
740 index += 1;
741 }
742 // bump to the next row
743 index += 1;
744 }
Kevin Lubicka22c1302023-01-18 14:16:44 +0000745 LOG_FATAL_IF((indexPtr - indices) != indexCount);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400746 }
747
John Reck1bcacfd2017-11-03 10:12:19 -0700748// double-check that we have legal indices
Kevin Lubicka22c1302023-01-18 14:16:44 +0000749#if !defined(NDEBUG)
Derek Sollenberger8872b382014-06-23 14:13:53 -0400750 {
751 for (int i = 0; i < indexCount; i++) {
Kevin Lubicka22c1302023-01-18 14:16:44 +0000752 LOG_FATAL_IF((unsigned)indices[i] >= (unsigned)ptCount);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400753 }
754 }
755#endif
756
Mike Reed7994a312021-01-28 18:06:26 -0500757 auto image = bitmap.makeImage();
758
Derek Sollenberger8872b382014-06-23 14:13:53 -0400759 // cons-up a shader for the bitmap
Mike Reedc2dbc032019-07-25 12:28:29 -0400760 Paint pnt;
761 if (paint) {
762 pnt = *paint;
763 }
Mike Reeda6cb58a2021-07-10 13:31:34 -0400764 SkSamplingOptions sampling = pnt.sampling();
Mike Reed7994a312021-01-28 18:06:26 -0500765 pnt.setShader(image->makeShader(sampling));
766
Mike Reedc2dbc032019-07-25 12:28:29 -0400767 auto v = builder.detach();
Mike Reeda6cb58a2021-07-10 13:31:34 -0400768 applyLooper(&pnt, [&](const Paint& p) {
Mike Reed7994a312021-01-28 18:06:26 -0500769 SkPaint copy(p);
Mike Reeda6cb58a2021-07-10 13:31:34 -0400770 auto s = p.sampling();
Mike Reed7994a312021-01-28 18:06:26 -0500771 if (s != sampling) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500772 // applyLooper changed the quality?
Mike Reed7994a312021-01-28 18:06:26 -0500773 copy.setShader(image->makeShader(s));
774 }
775 mCanvas->drawVertices(v, SkBlendMode::kModulate, copy);
Mike Reedc2dbc032019-07-25 12:28:29 -0400776 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400777}
778
John Reck1bcacfd2017-11-03 10:12:19 -0700779void SkiaCanvas::drawNinePatch(Bitmap& bitmap, const Res_png_9patch& chunk, float dstLeft,
780 float dstTop, float dstRight, float dstBottom,
Mike Reedc2dbc032019-07-25 12:28:29 -0400781 const Paint* paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400782 SkCanvas::Lattice lattice;
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400783 NinePatchUtils::SetLatticeDivs(&lattice, chunk, bitmap.width(), bitmap.height());
Stan Ilievf50806a2016-10-24 10:40:39 -0400784
Stan Ilieve12d7312017-12-04 14:48:27 -0500785 lattice.fRectTypes = nullptr;
786 lattice.fColors = nullptr;
Stan Ilievf50806a2016-10-24 10:40:39 -0400787 int numFlags = 0;
Stan Iliev021693b2016-10-17 16:26:15 -0400788 if (chunk.numColors > 0 && chunk.numColors == NinePatchUtils::NumDistinctRects(lattice)) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400789 // We can expect the framework to give us a color for every distinct rect.
790 // Skia requires a flag for every rect.
791 numFlags = (lattice.fXCount + 1) * (lattice.fYCount + 1);
792 }
793
Kevin Lubicka22c1302023-01-18 14:16:44 +0000794 // Most times, we do not have very many flags/colors, so the stack allocated part of
795 // FatVector will save us a heap allocation.
796 FatVector<SkCanvas::Lattice::RectType, 25> flags(numFlags);
797 FatVector<SkColor, 25> colors(numFlags);
Stan Ilievf50806a2016-10-24 10:40:39 -0400798 if (numFlags > 0) {
Kevin Lubicka22c1302023-01-18 14:16:44 +0000799 NinePatchUtils::SetLatticeFlags(&lattice, flags.data(), numFlags, chunk, colors.data());
Stan Ilievf50806a2016-10-24 10:40:39 -0400800 }
801
802 lattice.fBounds = nullptr;
803 SkRect dst = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Mike Reedc2dbc032019-07-25 12:28:29 -0400804 auto image = bitmap.makeImage();
Mike Reeda6cb58a2021-07-10 13:31:34 -0400805 applyLooper(paint, [&](const Paint& p) {
806 mCanvas->drawImageLattice(image.get(), lattice, dst, p.filterMode(), &p);
Mike Reedc2dbc032019-07-25 12:28:29 -0400807 });
Derek Sollenbergeredca3202015-07-10 13:56:39 -0400808}
809
Derek Sollenberger2d142132018-01-22 10:25:26 -0500810double SkiaCanvas::drawAnimatedImage(AnimatedImageDrawable* imgDrawable) {
811 return imgDrawable->drawStaging(mCanvas);
Leon Scroggins III671cce22018-01-14 16:52:17 -0500812}
813
Doris Liu766431a2016-02-04 22:17:11 +0000814void SkiaCanvas::drawVectorDrawable(VectorDrawableRoot* vectorDrawable) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800815 vectorDrawable->drawStaging(this);
Doris Liu766431a2016-02-04 22:17:11 +0000816}
817
Derek Sollenberger8872b382014-06-23 14:13:53 -0400818// ----------------------------------------------------------------------------
819// Canvas draw operations: Text
820// ----------------------------------------------------------------------------
821
Mike Reed2dfd55d2019-01-08 16:19:03 -0500822void SkiaCanvas::drawGlyphs(ReadGlyphFunc glyphFunc, int count, const Paint& paint, float x,
Seigo Nonaka63af7ba2020-09-21 23:07:43 -0700823 float y, float totalAdvance) {
Stan Iliev0b58d992017-03-30 18:22:27 -0400824 if (count <= 0 || paint.nothingToDraw()) return;
Mike Reedf6d86ac2019-01-18 14:13:23 -0500825 Paint paintCopy(paint);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400826 if (mPaintFilter) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500827 mPaintFilter->filterFullPaint(&paintCopy);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400828 }
Mike Reedf6d86ac2019-01-18 14:13:23 -0500829 const SkFont& font = paintCopy.getSkFont();
Stan Iliev7717e222018-02-05 18:04:11 -0500830 // Stroke with a hairline is drawn on HW with a fill style for compatibility with Android O and
831 // older.
John Recke170fb62018-05-07 08:12:07 -0700832 if (!mCanvasOwned && sApiLevel <= 27 && paintCopy.getStrokeWidth() <= 0 &&
833 paintCopy.getStyle() == SkPaint::kStroke_Style) {
Stan Iliev7717e222018-02-05 18:04:11 -0500834 paintCopy.setStyle(SkPaint::kFill_Style);
835 }
Stan Ilievf50806a2016-10-24 10:40:39 -0400836
Stan Ilievf50806a2016-10-24 10:40:39 -0400837 SkTextBlobBuilder builder;
Mike Reed2e204fc2019-01-28 13:31:36 -0500838 const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(font, count);
Stan Iliev0b58d992017-03-30 18:22:27 -0400839 glyphFunc(buffer.glyphs, buffer.pos);
Stan Ilievf50806a2016-10-24 10:40:39 -0400840
841 sk_sp<SkTextBlob> textBlob(builder.make());
Nathaniel Nifong52d37772020-01-10 16:12:41 -0500842
Mike Reedbb4cb482021-02-22 14:21:20 -0500843 applyLooper(&paintCopy, [&](const SkPaint& p) { mCanvas->drawTextBlob(textBlob, 0, 0, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400844}
845
Yuqian Liafc221492016-07-18 13:07:42 -0400846void SkiaCanvas::drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
Mike Reed2dfd55d2019-01-08 16:19:03 -0500847 const Paint& paint, const SkPath& path, size_t start,
John Reck1bcacfd2017-11-03 10:12:19 -0700848 size_t end) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500849 Paint paintCopy(paint);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400850 if (mPaintFilter) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500851 mPaintFilter->filterFullPaint(&paintCopy);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400852 }
Mike Reedf6d86ac2019-01-18 14:13:23 -0500853 const SkFont& font = paintCopy.getSkFont();
Derek Sollenberger415a74b2018-03-14 15:03:13 -0400854
Yuqian Liafc221492016-07-18 13:07:42 -0400855 const int N = end - start;
Mike Reed4a4b1be2019-01-01 15:43:06 -0500856 SkTextBlobBuilder builder;
857 auto rec = builder.allocRunRSXform(font, N);
858 SkRSXform* xform = (SkRSXform*)rec.pos;
859 uint16_t* glyphs = rec.glyphs;
Yuqian Liafc221492016-07-18 13:07:42 -0400860 SkPathMeasure meas(path, false);
861
862 for (size_t i = start; i < end; i++) {
863 glyphs[i - start] = layout.getGlyphId(i);
Stan Ilievc6c96dd2018-01-10 16:06:04 -0500864 float halfWidth = layout.getCharAdvance(i) * 0.5f;
865 float x = hOffset + layout.getX(i) + halfWidth;
Yuqian Liafc221492016-07-18 13:07:42 -0400866 float y = vOffset + layout.getY(i);
867
868 SkPoint pos;
869 SkVector tan;
870 if (!meas.getPosTan(x, &pos, &tan)) {
871 pos.set(x, y);
872 tan.set(1, 0);
873 }
874 xform[i - start].fSCos = tan.x();
875 xform[i - start].fSSin = tan.y();
Stan Ilievc6c96dd2018-01-10 16:06:04 -0500876 xform[i - start].fTx = pos.x() - tan.y() * y - halfWidth * tan.x();
877 xform[i - start].fTy = pos.y() + tan.x() * y - halfWidth * tan.y();
Yuqian Liafc221492016-07-18 13:07:42 -0400878 }
Leon Scroggins III950f2aa2020-04-29 13:46:00 -0400879
880 sk_sp<SkTextBlob> textBlob(builder.make());
881
Mike Reedbb4cb482021-02-22 14:21:20 -0500882 applyLooper(&paintCopy, [&](const SkPaint& p) { mCanvas->drawTextBlob(textBlob, 0, 0, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400883}
884
Derek Sollenberger6f485562015-07-30 10:00:39 -0400885// ----------------------------------------------------------------------------
886// Canvas draw operations: Animations
887// ----------------------------------------------------------------------------
888
Derek Sollenberger6f485562015-07-30 10:00:39 -0400889void SkiaCanvas::drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
John Reck1bcacfd2017-11-03 10:12:19 -0700890 uirenderer::CanvasPropertyPrimitive* top,
891 uirenderer::CanvasPropertyPrimitive* right,
892 uirenderer::CanvasPropertyPrimitive* bottom,
893 uirenderer::CanvasPropertyPrimitive* rx,
894 uirenderer::CanvasPropertyPrimitive* ry,
895 uirenderer::CanvasPropertyPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -0400896 sk_sp<uirenderer::skiapipeline::AnimatedRoundRect> drawable(
John Reck1bcacfd2017-11-03 10:12:19 -0700897 new uirenderer::skiapipeline::AnimatedRoundRect(left, top, right, bottom, rx, ry,
898 paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400899 mCanvas->drawDrawable(drawable.get());
900}
901
John Reck1bcacfd2017-11-03 10:12:19 -0700902void SkiaCanvas::drawCircle(uirenderer::CanvasPropertyPrimitive* x,
903 uirenderer::CanvasPropertyPrimitive* y,
904 uirenderer::CanvasPropertyPrimitive* radius,
905 uirenderer::CanvasPropertyPaint* paint) {
906 sk_sp<uirenderer::skiapipeline::AnimatedCircle> drawable(
907 new uirenderer::skiapipeline::AnimatedCircle(x, y, radius, paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400908 mCanvas->drawDrawable(drawable.get());
909}
910
Lucas Dupin00af5272021-04-29 20:30:01 -0700911void SkiaCanvas::drawRipple(const uirenderer::skiapipeline::RippleDrawableParams& params) {
912 uirenderer::skiapipeline::AnimatedRippleDrawable::draw(mCanvas, params);
Derek Sollenbergerdf301aa2020-12-17 11:06:03 -0500913}
914
John Reck894e85a2019-07-15 16:16:44 -0700915void SkiaCanvas::drawPicture(const SkPicture& picture) {
916 // TODO: Change to mCanvas->drawPicture()? SkCanvas::drawPicture seems to be
917 // where the logic is for playback vs. ref picture. Using picture.playback here
918 // to stay behavior-identical for now, but should revisit this at some point.
919 picture.playback(mCanvas);
920}
921
Derek Sollenberger6f485562015-07-30 10:00:39 -0400922// ----------------------------------------------------------------------------
923// Canvas draw operations: View System
924// ----------------------------------------------------------------------------
925
Stan Ilievf50806a2016-10-24 10:40:39 -0400926void SkiaCanvas::drawLayer(uirenderer::DeferredLayerUpdater* layerUpdater) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400927 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw Layers");
928}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400929
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400930void SkiaCanvas::drawRenderNode(uirenderer::RenderNode* renderNode) {
931 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw RenderNodes");
932}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400933
John Reck1bcacfd2017-11-03 10:12:19 -0700934} // namespace android