blob: 8394c3cd4175818d87b1faff1b158316e64fee41 [file] [log] [blame]
Derek Sollenberger8872b382014-06-23 14:13:53 -04001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Derek Sollenbergerc1908132016-07-15 10:28:16 -040017#include "SkiaCanvas.h"
Derek Sollenberger8872b382014-06-23 14:13:53 -040018
Derek Sollenberger24fc9012018-12-07 14:12:12 -050019#include <SkAndroidFrameworkUtils.h>
Leon Scroggins III671cce22018-01-14 16:52:17 -050020#include <SkAnimatedImage.h>
Kevin Lubick856848e2022-02-24 16:24:09 +000021#include <SkBitmap.h>
Kevin Lubicka22c1302023-01-18 14:16:44 +000022#include <SkBlendMode.h>
23#include <SkCanvas.h>
Derek Sollenbergerac33a482019-04-22 16:28:09 -040024#include <SkCanvasPriv.h>
Matt Sarettd0814db2017-04-13 09:33:18 -040025#include <SkCanvasStateUtils.h>
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -040026#include <SkColorFilter.h>
John Reck1bcacfd2017-11-03 10:12:19 -070027#include <SkDrawable.h>
Mike Reed1c79eab2018-11-21 11:01:57 -050028#include <SkFont.h>
John Reck849911a2015-01-20 07:51:14 -080029#include <SkGraphics.h>
Derek Sollenberger6f485562015-07-30 10:00:39 -040030#include <SkImage.h>
Matt Sarett62feb3a2016-09-20 10:34:11 -040031#include <SkImagePriv.h>
Kevin Lubick856848e2022-02-24 16:24:09 +000032#include <SkMatrix.h>
33#include <SkPaint.h>
Leon Scroggins III671cce22018-01-14 16:52:17 -050034#include <SkPicture.h>
Kevin Lubick856848e2022-02-24 16:24:09 +000035#include <SkRRect.h>
Kevin Lubickb3731212023-01-05 19:52:09 +000036#include <SkRSXform.h>
Kevin Lubick856848e2022-02-24 16:24:09 +000037#include <SkRect.h>
38#include <SkRefCnt.h>
John Reck849911a2015-01-20 07:51:14 -080039#include <SkShader.h>
Stan Ilievf50806a2016-10-24 10:40:39 -040040#include <SkTextBlob.h>
Brian Osmane3b9a122020-04-01 12:24:19 -040041#include <SkVertices.h>
Nader Jawad5f0a8002023-02-21 17:00:51 -080042#include <log/log.h>
43#include <ui/FatVector.h>
Derek Sollenberger8872b382014-06-23 14:13:53 -040044
Ben Wagner60126ef2015-08-07 12:13:48 -040045#include <memory>
Ben Wagner0ed10be2018-06-28 17:08:16 -040046#include <optional>
47#include <utility>
Ben Wagner60126ef2015-08-07 12:13:48 -040048
Kevin Lubickb3731212023-01-05 19:52:09 +000049#include "CanvasProperty.h"
Nader Jawad5f0a8002023-02-21 17:00:51 -080050#include "Mesh.h"
Kevin Lubickb3731212023-01-05 19:52:09 +000051#include "NinePatchUtils.h"
Kevin Lubickb3731212023-01-05 19:52:09 +000052#include "VectorDrawable.h"
John Reckb2a4b932023-03-29 17:01:25 -040053#include "effects/GainmapRenderer.h"
Kevin Lubickb3731212023-01-05 19:52:09 +000054#include "hwui/Bitmap.h"
55#include "hwui/MinikinUtils.h"
56#include "hwui/PaintFilter.h"
57#include "pipeline/skia/AnimatedDrawables.h"
58#include "pipeline/skia/HolePunch.h"
59
Derek Sollenberger8872b382014-06-23 14:13:53 -040060namespace android {
61
Stan Ilievf50806a2016-10-24 10:40:39 -040062using uirenderer::PaintUtils;
63
Ryan Prichard39971552022-08-26 20:53:33 -070064class SkiaCanvas::Clip {
65public:
66 Clip(const SkRect& rect, SkClipOp op, const SkMatrix& m)
67 : mType(Type::Rect), mOp(op), mMatrix(m), mRRect(SkRRect::MakeRect(rect)) {}
68 Clip(const SkRRect& rrect, SkClipOp op, const SkMatrix& m)
69 : mType(Type::RRect), mOp(op), mMatrix(m), mRRect(rrect) {}
70 Clip(const SkPath& path, SkClipOp op, const SkMatrix& m)
71 : mType(Type::Path), mOp(op), mMatrix(m), mPath(std::in_place, path) {}
72
73 void apply(SkCanvas* canvas) const {
74 canvas->setMatrix(mMatrix);
75 switch (mType) {
76 case Type::Rect:
77 // Don't anti-alias rectangular clips
78 canvas->clipRect(mRRect.rect(), mOp, false);
79 break;
80 case Type::RRect:
81 // Ensure rounded rectangular clips are anti-aliased
82 canvas->clipRRect(mRRect, mOp, true);
83 break;
84 case Type::Path:
85 // Ensure path clips are anti-aliased
86 canvas->clipPath(mPath.value(), mOp, true);
87 break;
88 }
89 }
90
91private:
92 enum class Type {
93 Rect,
94 RRect,
95 Path,
96 };
97
98 Type mType;
99 SkClipOp mOp;
100 SkMatrix mMatrix;
101
102 // These are logically a union (tracked separately due to non-POD path).
103 std::optional<SkPath> mPath;
104 SkRRect mRRect;
105};
106
John Reckc1b33d62015-04-22 09:04:45 -0700107Canvas* Canvas::create_canvas(const SkBitmap& bitmap) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400108 return new SkiaCanvas(bitmap);
109}
110
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400111Canvas* Canvas::create_canvas(SkCanvas* skiaCanvas) {
112 return new SkiaCanvas(skiaCanvas);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400113}
114
Stan Ilievf50806a2016-10-24 10:40:39 -0400115SkiaCanvas::SkiaCanvas() {}
116
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400117SkiaCanvas::SkiaCanvas(SkCanvas* canvas) : mCanvas(canvas) {}
Stan Ilievf50806a2016-10-24 10:40:39 -0400118
John Reckc1b33d62015-04-22 09:04:45 -0700119SkiaCanvas::SkiaCanvas(const SkBitmap& bitmap) {
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400120 mCanvasOwned = std::unique_ptr<SkCanvas>(new SkCanvas(bitmap));
121 mCanvas = mCanvasOwned.get();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400122}
123
Stan Iliev021693b2016-10-17 16:26:15 -0400124SkiaCanvas::~SkiaCanvas() {}
125
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400126void SkiaCanvas::reset(SkCanvas* skiaCanvas) {
Mike Reed6acfe162016-11-18 17:21:09 -0500127 if (mCanvas != skiaCanvas) {
128 mCanvas = skiaCanvas;
129 mCanvasOwned.reset();
130 }
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400131 mSaveStack.reset(nullptr);
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400132}
133
Derek Sollenberger8872b382014-06-23 14:13:53 -0400134// ----------------------------------------------------------------------------
135// Canvas state operations: Replace Bitmap
136// ----------------------------------------------------------------------------
137
John Reckc1b33d62015-04-22 09:04:45 -0700138void SkiaCanvas::setBitmap(const SkBitmap& bitmap) {
Tony Mantler4f641d12017-03-14 22:36:14 +0000139 // deletes the previously owned canvas (if any)
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400140 mCanvasOwned.reset(new SkCanvas(bitmap));
141 mCanvas = mCanvasOwned.get();
Tony Mantler4f641d12017-03-14 22:36:14 +0000142
Derek Sollenberger8872b382014-06-23 14:13:53 -0400143 // clean up the old save stack
Stan Ilievf50806a2016-10-24 10:40:39 -0400144 mSaveStack.reset(nullptr);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400145}
146
147// ----------------------------------------------------------------------------
148// Canvas state operations
149// ----------------------------------------------------------------------------
150
151bool SkiaCanvas::isOpaque() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400152 return mCanvas->imageInfo().isOpaque();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400153}
154
155int SkiaCanvas::width() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400156 return mCanvas->imageInfo().width();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400157}
158
159int SkiaCanvas::height() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400160 return mCanvas->imageInfo().height();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400161}
162
163// ----------------------------------------------------------------------------
164// Canvas state operations: Save (layer)
165// ----------------------------------------------------------------------------
166
167int SkiaCanvas::getSaveCount() const {
168 return mCanvas->getSaveCount();
169}
170
Florin Malitaeecff562015-12-21 10:43:01 -0500171int SkiaCanvas::save(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400172 int count = mCanvas->save();
173 recordPartialSave(flags);
174 return count;
175}
176
Florin Malita5e271402015-11-04 14:36:02 -0500177// The SkiaCanvas::restore operation layers on the capability to preserve
178// either (or both) the matrix and/or clip state after a SkCanvas::restore
179// operation. It does this by explicitly saving off the clip & matrix state
180// when requested and playing it back after the SkCanvas::restore.
Derek Sollenberger8872b382014-06-23 14:13:53 -0400181void SkiaCanvas::restore() {
Kevin Lubickb3731212023-01-05 19:52:09 +0000182 const SaveRec* rec = this->currentSaveRec();
Stan Ilievf50806a2016-10-24 10:40:39 -0400183 if (!rec) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400184 // Fast path - no record for this frame.
185 mCanvas->restore();
186 return;
187 }
188
Florin Malitaeecff562015-12-21 10:43:01 -0500189 bool preserveMatrix = !(rec->saveFlags & SaveFlags::Matrix);
John Reck1bcacfd2017-11-03 10:12:19 -0700190 bool preserveClip = !(rec->saveFlags & SaveFlags::Clip);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400191
192 SkMatrix savedMatrix;
193 if (preserveMatrix) {
194 savedMatrix = mCanvas->getTotalMatrix();
195 }
196
Stan Ilievf50806a2016-10-24 10:40:39 -0400197 const size_t clipIndex = rec->clipIndex;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400198
199 mCanvas->restore();
Stan Ilievf50806a2016-10-24 10:40:39 -0400200 mSaveStack->pop_back();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400201
202 if (preserveMatrix) {
203 mCanvas->setMatrix(savedMatrix);
204 }
205
Stan Ilievf50806a2016-10-24 10:40:39 -0400206 if (preserveClip) {
207 this->applyPersistentClips(clipIndex);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400208 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400209}
210
211void SkiaCanvas::restoreToCount(int restoreCount) {
212 while (mCanvas->getSaveCount() > restoreCount) {
213 this->restore();
214 }
215}
216
John Recka00eef212020-11-16 12:45:55 -0500217int SkiaCanvas::saveLayer(float left, float top, float right, float bottom, const SkPaint* paint) {
Florin Malitaeecff562015-12-21 10:43:01 -0500218 const SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
John Recka00eef212020-11-16 12:45:55 -0500219 const SkCanvas::SaveLayerRec rec(&bounds, paint);
Florin Malitaeecff562015-12-21 10:43:01 -0500220
Stan Iliev68885e32016-12-14 11:18:34 -0500221 return mCanvas->saveLayer(rec);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400222}
223
John Recka00eef212020-11-16 12:45:55 -0500224int SkiaCanvas::saveLayerAlpha(float left, float top, float right, float bottom, int alpha) {
Florin Malitaeecff562015-12-21 10:43:01 -0500225 if (static_cast<unsigned>(alpha) < 0xFF) {
Yuqian Lifd92ee42016-04-27 17:03:38 -0400226 SkPaint alphaPaint;
227 alphaPaint.setAlpha(alpha);
John Recka00eef212020-11-16 12:45:55 -0500228 return this->saveLayer(left, top, right, bottom, &alphaPaint);
Florin Malitaeecff562015-12-21 10:43:01 -0500229 }
John Recka00eef212020-11-16 12:45:55 -0500230 return this->saveLayer(left, top, right, bottom, nullptr);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400231}
232
Derek Sollenberger24fc9012018-12-07 14:12:12 -0500233int SkiaCanvas::saveUnclippedLayer(int left, int top, int right, int bottom) {
234 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
235 return SkAndroidFrameworkUtils::SaveBehind(mCanvas, &bounds);
236}
237
Mike Reeda6cb58a2021-07-10 13:31:34 -0400238void SkiaCanvas::restoreUnclippedLayer(int restoreCount, const Paint& paint) {
Derek Sollenbergerac33a482019-04-22 16:28:09 -0400239
240 while (mCanvas->getSaveCount() > restoreCount + 1) {
241 this->restore();
242 }
243
244 if (mCanvas->getSaveCount() == restoreCount + 1) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500245 SkCanvasPriv::DrawBehind(mCanvas, filterPaint(paint));
Derek Sollenbergerac33a482019-04-22 16:28:09 -0400246 this->restore();
247 }
248}
249
Stan Ilievf50806a2016-10-24 10:40:39 -0400250const SkiaCanvas::SaveRec* SkiaCanvas::currentSaveRec() const {
Kevin Lubickb3731212023-01-05 19:52:09 +0000251 const SaveRec* rec = (mSaveStack && !mSaveStack->empty())
252 ? static_cast<const SaveRec*>(&mSaveStack->back())
253 : nullptr;
Stan Ilievf50806a2016-10-24 10:40:39 -0400254 int currentSaveCount = mCanvas->getSaveCount();
Kevin Lubicka22c1302023-01-18 14:16:44 +0000255 LOG_FATAL_IF(!(!rec || currentSaveCount >= rec->saveCount));
Stan Ilievf50806a2016-10-24 10:40:39 -0400256
257 return (rec && rec->saveCount == currentSaveCount) ? rec : nullptr;
258}
259
Alec Mouri655a5e42022-09-12 17:49:17 +0000260void SkiaCanvas::punchHole(const SkRRect& rect, float alpha) {
Nader Jawad2dc632a2021-03-29 18:51:29 -0700261 SkPaint paint = SkPaint();
Alec Mouri655a5e42022-09-12 17:49:17 +0000262 paint.setColor(SkColors::kBlack);
263 paint.setAlphaf(alpha);
264 paint.setBlendMode(SkBlendMode::kDstOut);
Nader Jawad2dc632a2021-03-29 18:51:29 -0700265 mCanvas->drawRRect(rect, paint);
266}
267
Derek Sollenberger8872b382014-06-23 14:13:53 -0400268// ----------------------------------------------------------------------------
269// functions to emulate legacy SaveFlags (i.e. independent matrix/clip flags)
270// ----------------------------------------------------------------------------
271
Florin Malitaeecff562015-12-21 10:43:01 -0500272void SkiaCanvas::recordPartialSave(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400273 // A partial save is a save operation which doesn't capture the full canvas state.
Florin Malitaeecff562015-12-21 10:43:01 -0500274 // (either SaveFlags::Matrix or SaveFlags::Clip is missing).
Derek Sollenberger8872b382014-06-23 14:13:53 -0400275
276 // Mask-out non canvas state bits.
Florin Malitaeecff562015-12-21 10:43:01 -0500277 flags &= SaveFlags::MatrixClip;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400278
Florin Malitaeecff562015-12-21 10:43:01 -0500279 if (flags == SaveFlags::MatrixClip) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400280 // not a partial save.
281 return;
282 }
283
Stan Ilievf50806a2016-10-24 10:40:39 -0400284 if (!mSaveStack) {
Kevin Lubickb3731212023-01-05 19:52:09 +0000285 mSaveStack.reset(new std::deque<SaveRec>());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400286 }
287
Kevin Lubickb3731212023-01-05 19:52:09 +0000288 mSaveStack->emplace_back(mCanvas->getSaveCount(), // saveCount
289 flags, // saveFlags
290 mClipStack.size()); // clipIndex
Derek Sollenberger8872b382014-06-23 14:13:53 -0400291}
292
Stan Ilievf50806a2016-10-24 10:40:39 -0400293template <typename T>
Mike Reed6e49c9f2016-12-02 15:36:59 -0500294void SkiaCanvas::recordClip(const T& clip, SkClipOp op) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400295 // Only need tracking when in a partial save frame which
296 // doesn't restore the clip.
297 const SaveRec* rec = this->currentSaveRec();
298 if (rec && !(rec->saveFlags & SaveFlags::Clip)) {
299 mClipStack.emplace_back(clip, op, mCanvas->getTotalMatrix());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400300 }
301}
302
Stan Ilievf50806a2016-10-24 10:40:39 -0400303// Applies and optionally removes all clips >= index.
304void SkiaCanvas::applyPersistentClips(size_t clipStartIndex) {
Kevin Lubicka22c1302023-01-18 14:16:44 +0000305 LOG_FATAL_IF(clipStartIndex > mClipStack.size());
Stan Ilievf50806a2016-10-24 10:40:39 -0400306 const auto begin = mClipStack.cbegin() + clipStartIndex;
307 const auto end = mClipStack.cend();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400308
Stan Ilievf50806a2016-10-24 10:40:39 -0400309 // Clip application mutates the CTM.
310 const SkMatrix saveMatrix = mCanvas->getTotalMatrix();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400311
Stan Ilievf50806a2016-10-24 10:40:39 -0400312 for (auto clip = begin; clip != end; ++clip) {
Mike Reed6acfe162016-11-18 17:21:09 -0500313 clip->apply(mCanvas);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400314 }
315
Stan Ilievf50806a2016-10-24 10:40:39 -0400316 mCanvas->setMatrix(saveMatrix);
317
318 // If the current/post-restore save rec is also persisting clips, we
319 // leave them on the stack to be reapplied part of the next restore().
320 // Otherwise we're done and just pop them.
Kevin Lubickb3731212023-01-05 19:52:09 +0000321 const SaveRec* rec = this->currentSaveRec();
Stan Ilievf50806a2016-10-24 10:40:39 -0400322 if (!rec || (rec->saveFlags & SaveFlags::Clip)) {
323 mClipStack.erase(begin, end);
324 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400325}
326
327// ----------------------------------------------------------------------------
328// Canvas state operations: Matrix
329// ----------------------------------------------------------------------------
330
331void SkiaCanvas::getMatrix(SkMatrix* outMatrix) const {
332 *outMatrix = mCanvas->getTotalMatrix();
333}
334
335void SkiaCanvas::setMatrix(const SkMatrix& matrix) {
336 mCanvas->setMatrix(matrix);
337}
338
339void SkiaCanvas::concat(const SkMatrix& matrix) {
340 mCanvas->concat(matrix);
341}
342
343void SkiaCanvas::rotate(float degrees) {
344 mCanvas->rotate(degrees);
345}
346
347void SkiaCanvas::scale(float sx, float sy) {
348 mCanvas->scale(sx, sy);
349}
350
351void SkiaCanvas::skew(float sx, float sy) {
352 mCanvas->skew(sx, sy);
353}
354
355void SkiaCanvas::translate(float dx, float dy) {
356 mCanvas->translate(dx, dy);
357}
358
359// ----------------------------------------------------------------------------
360// Canvas state operations: Clips
361// ----------------------------------------------------------------------------
362
363// This function is a mirror of SkCanvas::getClipBounds except that it does
364// not outset the edge of the clip to account for anti-aliasing. There is
365// a skia bug to investigate pushing this logic into back into skia.
366// (see https://code.google.com/p/skia/issues/detail?id=1303)
367bool SkiaCanvas::getClipBounds(SkRect* outRect) const {
368 SkIRect ibounds;
Mike Reed5e438982017-01-25 08:23:25 -0500369 if (!mCanvas->getDeviceClipBounds(&ibounds)) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400370 return false;
371 }
372
373 SkMatrix inverse;
374 // if we can't invert the CTM, we can't return local clip bounds
375 if (!mCanvas->getTotalMatrix().invert(&inverse)) {
376 if (outRect) {
377 outRect->setEmpty();
378 }
379 return false;
380 }
381
382 if (NULL != outRect) {
383 SkRect r = SkRect::Make(ibounds);
384 inverse.mapRect(outRect, r);
385 }
386 return true;
387}
388
389bool SkiaCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
390 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
391 return mCanvas->quickReject(bounds);
392}
393
394bool SkiaCanvas::quickRejectPath(const SkPath& path) const {
395 return mCanvas->quickReject(path);
396}
397
Mike Reed6e49c9f2016-12-02 15:36:59 -0500398bool SkiaCanvas::clipRect(float left, float top, float right, float bottom, SkClipOp op) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400399 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Stan Ilievf50806a2016-10-24 10:40:39 -0400400 this->recordClip(rect, op);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400401 mCanvas->clipRect(rect, op);
Chris Craik5ec6a282015-06-23 15:42:12 -0700402 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400403}
404
Mike Reed6e49c9f2016-12-02 15:36:59 -0500405bool SkiaCanvas::clipPath(const SkPath* path, SkClipOp op) {
Derek Sollenbergerf7d98f42017-04-17 11:27:36 -0400406 this->recordClip(*path, op);
Nader Jawade431e312019-12-04 14:13:18 -0800407 mCanvas->clipPath(*path, op, true);
Chris Craik5ec6a282015-06-23 15:42:12 -0700408 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400409}
410
Michael Ludwig70cf50c22021-07-21 17:02:39 +0000411bool SkiaCanvas::replaceClipRect_deprecated(float left, float top, float right, float bottom) {
412 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
413
414 // Emulated clip rects are not recorded for partial saves, since
415 // partial saves have been removed from the public API.
416 SkAndroidFrameworkUtils::ResetClip(mCanvas);
417 mCanvas->clipRect(rect, SkClipOp::kIntersect);
418 return !mCanvas->isClipEmpty();
419}
420
421bool SkiaCanvas::replaceClipPath_deprecated(const SkPath* path) {
422 SkAndroidFrameworkUtils::ResetClip(mCanvas);
423 mCanvas->clipPath(*path, SkClipOp::kIntersect, true);
424 return !mCanvas->isClipEmpty();
425}
426
Derek Sollenberger8872b382014-06-23 14:13:53 -0400427// ----------------------------------------------------------------------------
428// Canvas state operations: Filters
429// ----------------------------------------------------------------------------
430
Ben Wagner0ed10be2018-06-28 17:08:16 -0400431PaintFilter* SkiaCanvas::getPaintFilter() {
432 return mPaintFilter.get();
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400433}
434
Ben Wagner0ed10be2018-06-28 17:08:16 -0400435void SkiaCanvas::setPaintFilter(sk_sp<PaintFilter> paintFilter) {
436 mPaintFilter = std::move(paintFilter);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400437}
438
439// ----------------------------------------------------------------------------
Matt Sarettd0814db2017-04-13 09:33:18 -0400440// Canvas state operations: Capture
441// ----------------------------------------------------------------------------
442
443SkCanvasState* SkiaCanvas::captureCanvasState() const {
444 SkCanvas* canvas = mCanvas;
445 if (mCanvasOwned) {
446 // Important to use the underlying SkCanvas, not the wrapper.
447 canvas = mCanvasOwned.get();
448 }
449
450 // Workarounds for http://crbug.com/271096: SW draw only supports
451 // translate & scale transforms, and a simple rectangular clip.
452 // (This also avoids significant wasted time in calling
453 // SkCanvasStateUtils::CaptureCanvasState when the clip is complex).
John Reck1bcacfd2017-11-03 10:12:19 -0700454 if (!canvas->isClipRect() || (canvas->getTotalMatrix().getType() &
455 ~(SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask))) {
456 return nullptr;
Matt Sarettd0814db2017-04-13 09:33:18 -0400457 }
458
459 return SkCanvasStateUtils::CaptureCanvasState(canvas);
460}
461
462// ----------------------------------------------------------------------------
Derek Sollenberger8872b382014-06-23 14:13:53 -0400463// Canvas draw operations
464// ----------------------------------------------------------------------------
465
Mike Reed260ab722016-10-07 15:59:20 -0400466void SkiaCanvas::drawColor(int color, SkBlendMode mode) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400467 mCanvas->drawColor(color, mode);
468}
469
Mike Reeda6cb58a2021-07-10 13:31:34 -0400470void SkiaCanvas::onFilterPaint(Paint& paint) {
Ben Wagner0ed10be2018-06-28 17:08:16 -0400471 if (mPaintFilter) {
Mike Reeda6cb58a2021-07-10 13:31:34 -0400472 mPaintFilter->filterFullPaint(&paint);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400473 }
Ben Wagner0ed10be2018-06-28 17:08:16 -0400474}
475
Mike Reeda6cb58a2021-07-10 13:31:34 -0400476void SkiaCanvas::drawPaint(const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500477 mCanvas->drawPaint(filterPaint(paint));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400478}
479
480// ----------------------------------------------------------------------------
481// Canvas draw operations: Geometry
482// ----------------------------------------------------------------------------
483
Mike Reedc2dbc032019-07-25 12:28:29 -0400484void SkiaCanvas::drawPoints(const float* points, int count, const Paint& paint,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400485 SkCanvas::PointMode mode) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500486 if (CC_UNLIKELY(count < 2 || paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400487 // convert the floats into SkPoints
John Reck1bcacfd2017-11-03 10:12:19 -0700488 count >>= 1; // now it is the number of points
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400489 std::unique_ptr<SkPoint[]> pts(new SkPoint[count]);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400490 for (int i = 0; i < count; i++) {
491 pts[i].set(points[0], points[1]);
492 points += 2;
493 }
Mike Reedc2dbc032019-07-25 12:28:29 -0400494
Mike Reedbb4cb482021-02-22 14:21:20 -0500495 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawPoints(mode, count, pts.get(), p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400496}
497
Mike Reedc2dbc032019-07-25 12:28:29 -0400498void SkiaCanvas::drawPoint(float x, float y, const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500499 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawPoint(x, y, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400500}
501
Mike Reedc2dbc032019-07-25 12:28:29 -0400502void SkiaCanvas::drawPoints(const float* points, int count, const Paint& paint) {
503 this->drawPoints(points, count, paint, SkCanvas::kPoints_PointMode);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400504}
505
506void SkiaCanvas::drawLine(float startX, float startY, float stopX, float stopY,
Mike Reedc2dbc032019-07-25 12:28:29 -0400507 const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500508 applyLooper(&paint,
509 [&](const SkPaint& p) { mCanvas->drawLine(startX, startY, stopX, stopY, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400510}
511
Mike Reedc2dbc032019-07-25 12:28:29 -0400512void SkiaCanvas::drawLines(const float* points, int count, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500513 if (CC_UNLIKELY(count < 4 || paint.nothingToDraw())) return;
Mike Reedc2dbc032019-07-25 12:28:29 -0400514 this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400515}
516
Mike Reedc2dbc032019-07-25 12:28:29 -0400517void SkiaCanvas::drawRect(float left, float top, float right, float bottom, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500518 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Mike Reedbb4cb482021-02-22 14:21:20 -0500519 applyLooper(&paint, [&](const SkPaint& p) {
Mike Reedc2dbc032019-07-25 12:28:29 -0400520 mCanvas->drawRect({left, top, right, bottom}, p);
521 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400522}
523
Mike Reedc2dbc032019-07-25 12:28:29 -0400524void SkiaCanvas::drawRegion(const SkRegion& region, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500525 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Mike Reedbb4cb482021-02-22 14:21:20 -0500526 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawRegion(region, p); });
Derek Sollenberger94394b32015-07-10 09:58:41 -0400527}
528
John Reck1bcacfd2017-11-03 10:12:19 -0700529void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom, float rx, float ry,
Mike Reedc2dbc032019-07-25 12:28:29 -0400530 const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500531 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400532 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Mike Reedbb4cb482021-02-22 14:21:20 -0500533 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawRoundRect(rect, rx, ry, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400534}
535
Nader Jawadadfe1d92018-09-27 12:27:36 -0700536void SkiaCanvas::drawDoubleRoundRect(const SkRRect& outer, const SkRRect& inner,
Mike Reedc2dbc032019-07-25 12:28:29 -0400537 const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500538 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawDRRect(outer, inner, p); });
Nader Jawadadfe1d92018-09-27 12:27:36 -0700539}
540
Mike Reedc2dbc032019-07-25 12:28:29 -0400541void SkiaCanvas::drawCircle(float x, float y, float radius, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500542 if (CC_UNLIKELY(radius <= 0 || paint.nothingToDraw())) return;
Mike Reedbb4cb482021-02-22 14:21:20 -0500543 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawCircle(x, y, radius, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400544}
545
Mike Reedc2dbc032019-07-25 12:28:29 -0400546void SkiaCanvas::drawOval(float left, float top, float right, float bottom, 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 oval = SkRect::MakeLTRB(left, top, right, bottom);
Mike Reedbb4cb482021-02-22 14:21:20 -0500549 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawOval(oval, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400550}
551
John Reck1bcacfd2017-11-03 10:12:19 -0700552void SkiaCanvas::drawArc(float left, float top, float right, float bottom, float startAngle,
Mike Reedc2dbc032019-07-25 12:28:29 -0400553 float sweepAngle, bool useCenter, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500554 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400555 SkRect arc = SkRect::MakeLTRB(left, top, right, bottom);
Mike Reedbb4cb482021-02-22 14:21:20 -0500556 applyLooper(&paint, [&](const SkPaint& p) {
Mike Reedc2dbc032019-07-25 12:28:29 -0400557 if (fabs(sweepAngle) >= 360.0f) {
558 mCanvas->drawOval(arc, p);
559 } else {
560 mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, p);
561 }
562 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400563}
564
Mike Reedc2dbc032019-07-25 12:28:29 -0400565void SkiaCanvas::drawPath(const SkPath& path, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500566 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Stan Iliev6dcfdec2017-08-15 16:42:05 -0400567 if (CC_UNLIKELY(path.isEmpty() && (!path.isInverseFillType()))) {
568 return;
569 }
Mike Reedbb4cb482021-02-22 14:21:20 -0500570 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawPath(path, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400571}
572
Mike Reedc2dbc032019-07-25 12:28:29 -0400573void SkiaCanvas::drawVertices(const SkVertices* vertices, SkBlendMode mode, const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500574 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawVertices(vertices, mode, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400575}
576
Nader Jawad5f0a8002023-02-21 17:00:51 -0800577void SkiaCanvas::drawMesh(const Mesh& mesh, sk_sp<SkBlender> blender, const Paint& paint) {
578 GrDirectContext* context = nullptr;
579 auto recordingContext = mCanvas->recordingContext();
580 if (recordingContext) {
581 context = recordingContext->asDirectContext();
582 }
583 mesh.updateSkMesh(context);
584 mCanvas->drawMesh(mesh.getSkMesh(), blender, paint);
Angel Aguayo90c46ee2022-11-08 23:42:09 +0000585}
586
Derek Sollenberger8872b382014-06-23 14:13:53 -0400587// ----------------------------------------------------------------------------
588// Canvas draw operations: Bitmaps
589// ----------------------------------------------------------------------------
590
Mike Reedc2dbc032019-07-25 12:28:29 -0400591void SkiaCanvas::drawBitmap(Bitmap& bitmap, float left, float top, const Paint* paint) {
592 auto image = bitmap.makeImage();
John Reckb2a4b932023-03-29 17:01:25 -0400593
594 if (bitmap.hasGainmap()) {
595 Paint gainmapPaint = paint ? *paint : Paint();
596 sk_sp<SkShader> gainmapShader = uirenderer::MakeGainmapShader(
597 image, bitmap.gainmap()->bitmap->makeImage(), bitmap.gainmap()->info,
598 SkTileMode::kClamp, SkTileMode::kClamp, gainmapPaint.sampling());
599 gainmapPaint.setShader(gainmapShader);
600 return drawRect(left, top, left + bitmap.width(), top + bitmap.height(), gainmapPaint);
601 }
602
Mike Reeda6cb58a2021-07-10 13:31:34 -0400603 applyLooper(paint, [&](const Paint& p) {
604 mCanvas->drawImage(image, left, top, p.sampling(), &p);
Mike Reedc2dbc032019-07-25 12:28:29 -0400605 });
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400606}
607
Mike Reedc2dbc032019-07-25 12:28:29 -0400608void SkiaCanvas::drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const Paint* paint) {
Mike Reed6acfe162016-11-18 17:21:09 -0500609 SkAutoCanvasRestore acr(mCanvas, true);
Mike Reed70ffbf92014-12-08 17:03:30 -0500610 mCanvas->concat(matrix);
John Reckb2a4b932023-03-29 17:01:25 -0400611 drawBitmap(bitmap, 0, 0, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400612}
613
John Reck1bcacfd2017-11-03 10:12:19 -0700614void SkiaCanvas::drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight,
615 float srcBottom, float dstLeft, float dstTop, float dstRight,
Mike Reedc2dbc032019-07-25 12:28:29 -0400616 float dstBottom, const Paint* paint) {
617 auto image = bitmap.makeImage();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400618 SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
619 SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400620
John Reckb2a4b932023-03-29 17:01:25 -0400621 if (bitmap.hasGainmap()) {
622 Paint gainmapPaint = paint ? *paint : Paint();
623 sk_sp<SkShader> gainmapShader = uirenderer::MakeGainmapShader(
624 image, bitmap.gainmap()->bitmap->makeImage(), bitmap.gainmap()->info,
625 SkTileMode::kClamp, SkTileMode::kClamp, gainmapPaint.sampling());
626 gainmapShader = gainmapShader->makeWithLocalMatrix(SkMatrix::RectToRect(srcRect, dstRect));
627 gainmapPaint.setShader(gainmapShader);
628 return drawRect(dstLeft, dstTop, dstRight, dstBottom, gainmapPaint);
629 }
630
Mike Reeda6cb58a2021-07-10 13:31:34 -0400631 applyLooper(paint, [&](const Paint& p) {
632 mCanvas->drawImageRect(image, srcRect, dstRect, p.sampling(), &p,
Mike Reed7994a312021-01-28 18:06:26 -0500633 SkCanvas::kFast_SrcRectConstraint);
Mike Reedc2dbc032019-07-25 12:28:29 -0400634 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400635}
636
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400637void SkiaCanvas::drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight,
Mike Reedc2dbc032019-07-25 12:28:29 -0400638 const float* vertices, const int* colors, const Paint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400639 const int ptCount = (meshWidth + 1) * (meshHeight + 1);
640 const int indexCount = meshWidth * meshHeight * 6;
Mike Reed871cd2d2017-03-17 10:15:52 -0400641 uint32_t flags = SkVertices::kHasTexCoords_BuilderFlag;
642 if (colors) {
643 flags |= SkVertices::kHasColors_BuilderFlag;
644 }
Mike Reed826deef2017-04-04 15:32:04 -0400645 SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, ptCount, indexCount, flags);
Mike Reed871cd2d2017-03-17 10:15:52 -0400646 memcpy(builder.positions(), vertices, ptCount * sizeof(SkPoint));
647 if (colors) {
648 memcpy(builder.colors(), colors, ptCount * sizeof(SkColor));
649 }
650 SkPoint* texs = builder.texCoords();
651 uint16_t* indices = builder.indices();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400652
653 // cons up texture coordinates and indices
654 {
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400655 const SkScalar w = SkIntToScalar(bitmap.width());
656 const SkScalar h = SkIntToScalar(bitmap.height());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400657 const SkScalar dx = w / meshWidth;
658 const SkScalar dy = h / meshHeight;
659
660 SkPoint* texsPtr = texs;
661 SkScalar y = 0;
662 for (int i = 0; i <= meshHeight; i++) {
663 if (i == meshHeight) {
664 y = h; // to ensure numerically we hit h exactly
665 }
666 SkScalar x = 0;
667 for (int j = 0; j < meshWidth; j++) {
668 texsPtr->set(x, y);
669 texsPtr += 1;
670 x += dx;
671 }
672 texsPtr->set(w, y);
673 texsPtr += 1;
674 y += dy;
675 }
Kevin Lubicka22c1302023-01-18 14:16:44 +0000676 LOG_FATAL_IF((texsPtr - texs) != ptCount);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400677 }
678
679 // cons up indices
680 {
681 uint16_t* indexPtr = indices;
682 int index = 0;
683 for (int i = 0; i < meshHeight; i++) {
684 for (int j = 0; j < meshWidth; j++) {
685 // lower-left triangle
686 *indexPtr++ = index;
687 *indexPtr++ = index + meshWidth + 1;
688 *indexPtr++ = index + meshWidth + 2;
689 // upper-right triangle
690 *indexPtr++ = index;
691 *indexPtr++ = index + meshWidth + 2;
692 *indexPtr++ = index + 1;
693 // bump to the next cell
694 index += 1;
695 }
696 // bump to the next row
697 index += 1;
698 }
Kevin Lubicka22c1302023-01-18 14:16:44 +0000699 LOG_FATAL_IF((indexPtr - indices) != indexCount);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400700 }
701
John Reck1bcacfd2017-11-03 10:12:19 -0700702// double-check that we have legal indices
Kevin Lubicka22c1302023-01-18 14:16:44 +0000703#if !defined(NDEBUG)
Derek Sollenberger8872b382014-06-23 14:13:53 -0400704 {
705 for (int i = 0; i < indexCount; i++) {
Kevin Lubicka22c1302023-01-18 14:16:44 +0000706 LOG_FATAL_IF((unsigned)indices[i] >= (unsigned)ptCount);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400707 }
708 }
709#endif
710
Mike Reed7994a312021-01-28 18:06:26 -0500711 auto image = bitmap.makeImage();
712
Derek Sollenberger8872b382014-06-23 14:13:53 -0400713 // cons-up a shader for the bitmap
Mike Reedc2dbc032019-07-25 12:28:29 -0400714 Paint pnt;
715 if (paint) {
716 pnt = *paint;
717 }
Mike Reeda6cb58a2021-07-10 13:31:34 -0400718 SkSamplingOptions sampling = pnt.sampling();
Mike Reed7994a312021-01-28 18:06:26 -0500719 pnt.setShader(image->makeShader(sampling));
720
Mike Reedc2dbc032019-07-25 12:28:29 -0400721 auto v = builder.detach();
Mike Reeda6cb58a2021-07-10 13:31:34 -0400722 applyLooper(&pnt, [&](const Paint& p) {
Mike Reed7994a312021-01-28 18:06:26 -0500723 SkPaint copy(p);
Mike Reeda6cb58a2021-07-10 13:31:34 -0400724 auto s = p.sampling();
Mike Reed7994a312021-01-28 18:06:26 -0500725 if (s != sampling) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500726 // applyLooper changed the quality?
Mike Reed7994a312021-01-28 18:06:26 -0500727 copy.setShader(image->makeShader(s));
728 }
729 mCanvas->drawVertices(v, SkBlendMode::kModulate, copy);
Mike Reedc2dbc032019-07-25 12:28:29 -0400730 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400731}
732
John Reck1bcacfd2017-11-03 10:12:19 -0700733void SkiaCanvas::drawNinePatch(Bitmap& bitmap, const Res_png_9patch& chunk, float dstLeft,
734 float dstTop, float dstRight, float dstBottom,
Mike Reedc2dbc032019-07-25 12:28:29 -0400735 const Paint* paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400736 SkCanvas::Lattice lattice;
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400737 NinePatchUtils::SetLatticeDivs(&lattice, chunk, bitmap.width(), bitmap.height());
Stan Ilievf50806a2016-10-24 10:40:39 -0400738
Stan Ilieve12d7312017-12-04 14:48:27 -0500739 lattice.fRectTypes = nullptr;
740 lattice.fColors = nullptr;
Stan Ilievf50806a2016-10-24 10:40:39 -0400741 int numFlags = 0;
Stan Iliev021693b2016-10-17 16:26:15 -0400742 if (chunk.numColors > 0 && chunk.numColors == NinePatchUtils::NumDistinctRects(lattice)) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400743 // We can expect the framework to give us a color for every distinct rect.
744 // Skia requires a flag for every rect.
745 numFlags = (lattice.fXCount + 1) * (lattice.fYCount + 1);
746 }
747
Kevin Lubicka22c1302023-01-18 14:16:44 +0000748 // Most times, we do not have very many flags/colors, so the stack allocated part of
749 // FatVector will save us a heap allocation.
750 FatVector<SkCanvas::Lattice::RectType, 25> flags(numFlags);
751 FatVector<SkColor, 25> colors(numFlags);
Stan Ilievf50806a2016-10-24 10:40:39 -0400752 if (numFlags > 0) {
Kevin Lubicka22c1302023-01-18 14:16:44 +0000753 NinePatchUtils::SetLatticeFlags(&lattice, flags.data(), numFlags, chunk, colors.data());
Stan Ilievf50806a2016-10-24 10:40:39 -0400754 }
755
756 lattice.fBounds = nullptr;
757 SkRect dst = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Mike Reedc2dbc032019-07-25 12:28:29 -0400758 auto image = bitmap.makeImage();
Mike Reeda6cb58a2021-07-10 13:31:34 -0400759 applyLooper(paint, [&](const Paint& p) {
760 mCanvas->drawImageLattice(image.get(), lattice, dst, p.filterMode(), &p);
Mike Reedc2dbc032019-07-25 12:28:29 -0400761 });
Derek Sollenbergeredca3202015-07-10 13:56:39 -0400762}
763
Derek Sollenberger2d142132018-01-22 10:25:26 -0500764double SkiaCanvas::drawAnimatedImage(AnimatedImageDrawable* imgDrawable) {
765 return imgDrawable->drawStaging(mCanvas);
Leon Scroggins III671cce22018-01-14 16:52:17 -0500766}
767
Doris Liu766431a2016-02-04 22:17:11 +0000768void SkiaCanvas::drawVectorDrawable(VectorDrawableRoot* vectorDrawable) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800769 vectorDrawable->drawStaging(this);
Doris Liu766431a2016-02-04 22:17:11 +0000770}
771
Derek Sollenberger8872b382014-06-23 14:13:53 -0400772// ----------------------------------------------------------------------------
773// Canvas draw operations: Text
774// ----------------------------------------------------------------------------
775
Mike Reed2dfd55d2019-01-08 16:19:03 -0500776void SkiaCanvas::drawGlyphs(ReadGlyphFunc glyphFunc, int count, const Paint& paint, float x,
Seigo Nonaka63af7ba2020-09-21 23:07:43 -0700777 float y, float totalAdvance) {
Stan Iliev0b58d992017-03-30 18:22:27 -0400778 if (count <= 0 || paint.nothingToDraw()) return;
Mike Reedf6d86ac2019-01-18 14:13:23 -0500779 Paint paintCopy(paint);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400780 if (mPaintFilter) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500781 mPaintFilter->filterFullPaint(&paintCopy);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400782 }
Mike Reedf6d86ac2019-01-18 14:13:23 -0500783 const SkFont& font = paintCopy.getSkFont();
Stan Iliev7717e222018-02-05 18:04:11 -0500784 // Stroke with a hairline is drawn on HW with a fill style for compatibility with Android O and
785 // older.
John Recke170fb62018-05-07 08:12:07 -0700786 if (!mCanvasOwned && sApiLevel <= 27 && paintCopy.getStrokeWidth() <= 0 &&
787 paintCopy.getStyle() == SkPaint::kStroke_Style) {
Stan Iliev7717e222018-02-05 18:04:11 -0500788 paintCopy.setStyle(SkPaint::kFill_Style);
789 }
Stan Ilievf50806a2016-10-24 10:40:39 -0400790
Stan Ilievf50806a2016-10-24 10:40:39 -0400791 SkTextBlobBuilder builder;
Mike Reed2e204fc2019-01-28 13:31:36 -0500792 const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(font, count);
Stan Iliev0b58d992017-03-30 18:22:27 -0400793 glyphFunc(buffer.glyphs, buffer.pos);
Stan Ilievf50806a2016-10-24 10:40:39 -0400794
795 sk_sp<SkTextBlob> textBlob(builder.make());
Nathaniel Nifong52d37772020-01-10 16:12:41 -0500796
Mike Reedbb4cb482021-02-22 14:21:20 -0500797 applyLooper(&paintCopy, [&](const SkPaint& p) { mCanvas->drawTextBlob(textBlob, 0, 0, p); });
Stan Ilievf50806a2016-10-24 10:40:39 -0400798 drawTextDecorations(x, y, totalAdvance, paintCopy);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400799}
800
Yuqian Liafc221492016-07-18 13:07:42 -0400801void SkiaCanvas::drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
Mike Reed2dfd55d2019-01-08 16:19:03 -0500802 const Paint& paint, const SkPath& path, size_t start,
John Reck1bcacfd2017-11-03 10:12:19 -0700803 size_t end) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500804 Paint paintCopy(paint);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400805 if (mPaintFilter) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500806 mPaintFilter->filterFullPaint(&paintCopy);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400807 }
Mike Reedf6d86ac2019-01-18 14:13:23 -0500808 const SkFont& font = paintCopy.getSkFont();
Derek Sollenberger415a74b2018-03-14 15:03:13 -0400809
Yuqian Liafc221492016-07-18 13:07:42 -0400810 const int N = end - start;
Mike Reed4a4b1be2019-01-01 15:43:06 -0500811 SkTextBlobBuilder builder;
812 auto rec = builder.allocRunRSXform(font, N);
813 SkRSXform* xform = (SkRSXform*)rec.pos;
814 uint16_t* glyphs = rec.glyphs;
Yuqian Liafc221492016-07-18 13:07:42 -0400815 SkPathMeasure meas(path, false);
816
817 for (size_t i = start; i < end; i++) {
818 glyphs[i - start] = layout.getGlyphId(i);
Stan Ilievc6c96dd2018-01-10 16:06:04 -0500819 float halfWidth = layout.getCharAdvance(i) * 0.5f;
820 float x = hOffset + layout.getX(i) + halfWidth;
Yuqian Liafc221492016-07-18 13:07:42 -0400821 float y = vOffset + layout.getY(i);
822
823 SkPoint pos;
824 SkVector tan;
825 if (!meas.getPosTan(x, &pos, &tan)) {
826 pos.set(x, y);
827 tan.set(1, 0);
828 }
829 xform[i - start].fSCos = tan.x();
830 xform[i - start].fSSin = tan.y();
Stan Ilievc6c96dd2018-01-10 16:06:04 -0500831 xform[i - start].fTx = pos.x() - tan.y() * y - halfWidth * tan.x();
832 xform[i - start].fTy = pos.y() + tan.x() * y - halfWidth * tan.y();
Yuqian Liafc221492016-07-18 13:07:42 -0400833 }
Leon Scroggins III950f2aa2020-04-29 13:46:00 -0400834
835 sk_sp<SkTextBlob> textBlob(builder.make());
836
Mike Reedbb4cb482021-02-22 14:21:20 -0500837 applyLooper(&paintCopy, [&](const SkPaint& p) { mCanvas->drawTextBlob(textBlob, 0, 0, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400838}
839
Derek Sollenberger6f485562015-07-30 10:00:39 -0400840// ----------------------------------------------------------------------------
841// Canvas draw operations: Animations
842// ----------------------------------------------------------------------------
843
Derek Sollenberger6f485562015-07-30 10:00:39 -0400844void SkiaCanvas::drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
John Reck1bcacfd2017-11-03 10:12:19 -0700845 uirenderer::CanvasPropertyPrimitive* top,
846 uirenderer::CanvasPropertyPrimitive* right,
847 uirenderer::CanvasPropertyPrimitive* bottom,
848 uirenderer::CanvasPropertyPrimitive* rx,
849 uirenderer::CanvasPropertyPrimitive* ry,
850 uirenderer::CanvasPropertyPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -0400851 sk_sp<uirenderer::skiapipeline::AnimatedRoundRect> drawable(
John Reck1bcacfd2017-11-03 10:12:19 -0700852 new uirenderer::skiapipeline::AnimatedRoundRect(left, top, right, bottom, rx, ry,
853 paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400854 mCanvas->drawDrawable(drawable.get());
855}
856
John Reck1bcacfd2017-11-03 10:12:19 -0700857void SkiaCanvas::drawCircle(uirenderer::CanvasPropertyPrimitive* x,
858 uirenderer::CanvasPropertyPrimitive* y,
859 uirenderer::CanvasPropertyPrimitive* radius,
860 uirenderer::CanvasPropertyPaint* paint) {
861 sk_sp<uirenderer::skiapipeline::AnimatedCircle> drawable(
862 new uirenderer::skiapipeline::AnimatedCircle(x, y, radius, paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400863 mCanvas->drawDrawable(drawable.get());
864}
865
Lucas Dupin00af5272021-04-29 20:30:01 -0700866void SkiaCanvas::drawRipple(const uirenderer::skiapipeline::RippleDrawableParams& params) {
867 uirenderer::skiapipeline::AnimatedRippleDrawable::draw(mCanvas, params);
Derek Sollenbergerdf301aa2020-12-17 11:06:03 -0500868}
869
John Reck894e85a2019-07-15 16:16:44 -0700870void SkiaCanvas::drawPicture(const SkPicture& picture) {
871 // TODO: Change to mCanvas->drawPicture()? SkCanvas::drawPicture seems to be
872 // where the logic is for playback vs. ref picture. Using picture.playback here
873 // to stay behavior-identical for now, but should revisit this at some point.
874 picture.playback(mCanvas);
875}
876
Derek Sollenberger6f485562015-07-30 10:00:39 -0400877// ----------------------------------------------------------------------------
878// Canvas draw operations: View System
879// ----------------------------------------------------------------------------
880
Stan Ilievf50806a2016-10-24 10:40:39 -0400881void SkiaCanvas::drawLayer(uirenderer::DeferredLayerUpdater* layerUpdater) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400882 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw Layers");
883}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400884
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400885void SkiaCanvas::drawRenderNode(uirenderer::RenderNode* renderNode) {
886 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw RenderNodes");
887}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400888
John Reck1bcacfd2017-11-03 10:12:19 -0700889} // namespace android