blob: d83d78f650aadf45ff62550c30c7906ddb219e25 [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 Sollenbergerc1908132016-07-15 10:28:16 -040019#include "CanvasProperty.h"
Matt Sarett7de73852016-10-25 18:36:39 -040020#include "NinePatchUtils.h"
Alec Mouri655a5e42022-09-12 17:49:17 +000021#include "SkBlendMode.h"
Derek Sollenbergerc1908132016-07-15 10:28:16 -040022#include "VectorDrawable.h"
sergeyvaed7f582016-10-14 16:30:21 -070023#include "hwui/Bitmap.h"
Yuqian Liafc221492016-07-18 13:07:42 -040024#include "hwui/MinikinUtils.h"
Ben Wagner0ed10be2018-06-28 17:08:16 -040025#include "hwui/PaintFilter.h"
Stan Iliev021693b2016-10-17 16:26:15 -040026#include "pipeline/skia/AnimatedDrawables.h"
Nader Jawad2dc632a2021-03-29 18:51:29 -070027#include "pipeline/skia/HolePunch.h"
Derek Sollenbergerc1908132016-07-15 10:28:16 -040028
Derek Sollenberger24fc9012018-12-07 14:12:12 -050029#include <SkAndroidFrameworkUtils.h>
Leon Scroggins III671cce22018-01-14 16:52:17 -050030#include <SkAnimatedImage.h>
Kevin Lubick856848e2022-02-24 16:24:09 +000031#include <SkBitmap.h>
Derek Sollenbergerac33a482019-04-22 16:28:09 -040032#include <SkCanvasPriv.h>
Matt Sarettd0814db2017-04-13 09:33:18 -040033#include <SkCanvasStateUtils.h>
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -040034#include <SkColorFilter.h>
John Reck849911a2015-01-20 07:51:14 -080035#include <SkDeque.h>
John Reck1bcacfd2017-11-03 10:12:19 -070036#include <SkDrawable.h>
Mike Reed1c79eab2018-11-21 11:01:57 -050037#include <SkFont.h>
John Reck849911a2015-01-20 07:51:14 -080038#include <SkGraphics.h>
Derek Sollenberger6f485562015-07-30 10:00:39 -040039#include <SkImage.h>
Matt Sarett62feb3a2016-09-20 10:34:11 -040040#include <SkImagePriv.h>
Kevin Lubick856848e2022-02-24 16:24:09 +000041#include <SkMatrix.h>
42#include <SkPaint.h>
Leon Scroggins III671cce22018-01-14 16:52:17 -050043#include <SkPicture.h>
Yuqian Liafc221492016-07-18 13:07:42 -040044#include <SkRSXform.h>
Kevin Lubick856848e2022-02-24 16:24:09 +000045#include <SkRRect.h>
46#include <SkRect.h>
47#include <SkRefCnt.h>
John Reck849911a2015-01-20 07:51:14 -080048#include <SkShader.h>
John Reck849911a2015-01-20 07:51:14 -080049#include <SkTemplates.h>
Stan Ilievf50806a2016-10-24 10:40:39 -040050#include <SkTextBlob.h>
Brian Osmane3b9a122020-04-01 12:24:19 -040051#include <SkVertices.h>
Derek Sollenberger8872b382014-06-23 14:13:53 -040052
Ben Wagner60126ef2015-08-07 12:13:48 -040053#include <memory>
Ben Wagner0ed10be2018-06-28 17:08:16 -040054#include <optional>
55#include <utility>
Ben Wagner60126ef2015-08-07 12:13:48 -040056
Derek Sollenberger8872b382014-06-23 14:13:53 -040057namespace android {
58
Stan Ilievf50806a2016-10-24 10:40:39 -040059using uirenderer::PaintUtils;
60
Ryan Prichard39971552022-08-26 20:53:33 -070061class SkiaCanvas::Clip {
62public:
63 Clip(const SkRect& rect, SkClipOp op, const SkMatrix& m)
64 : mType(Type::Rect), mOp(op), mMatrix(m), mRRect(SkRRect::MakeRect(rect)) {}
65 Clip(const SkRRect& rrect, SkClipOp op, const SkMatrix& m)
66 : mType(Type::RRect), mOp(op), mMatrix(m), mRRect(rrect) {}
67 Clip(const SkPath& path, SkClipOp op, const SkMatrix& m)
68 : mType(Type::Path), mOp(op), mMatrix(m), mPath(std::in_place, path) {}
69
70 void apply(SkCanvas* canvas) const {
71 canvas->setMatrix(mMatrix);
72 switch (mType) {
73 case Type::Rect:
74 // Don't anti-alias rectangular clips
75 canvas->clipRect(mRRect.rect(), mOp, false);
76 break;
77 case Type::RRect:
78 // Ensure rounded rectangular clips are anti-aliased
79 canvas->clipRRect(mRRect, mOp, true);
80 break;
81 case Type::Path:
82 // Ensure path clips are anti-aliased
83 canvas->clipPath(mPath.value(), mOp, true);
84 break;
85 }
86 }
87
88private:
89 enum class Type {
90 Rect,
91 RRect,
92 Path,
93 };
94
95 Type mType;
96 SkClipOp mOp;
97 SkMatrix mMatrix;
98
99 // These are logically a union (tracked separately due to non-POD path).
100 std::optional<SkPath> mPath;
101 SkRRect mRRect;
102};
103
John Reckc1b33d62015-04-22 09:04:45 -0700104Canvas* Canvas::create_canvas(const SkBitmap& bitmap) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400105 return new SkiaCanvas(bitmap);
106}
107
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400108Canvas* Canvas::create_canvas(SkCanvas* skiaCanvas) {
109 return new SkiaCanvas(skiaCanvas);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400110}
111
Stan Ilievf50806a2016-10-24 10:40:39 -0400112SkiaCanvas::SkiaCanvas() {}
113
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400114SkiaCanvas::SkiaCanvas(SkCanvas* canvas) : mCanvas(canvas) {}
Stan Ilievf50806a2016-10-24 10:40:39 -0400115
John Reckc1b33d62015-04-22 09:04:45 -0700116SkiaCanvas::SkiaCanvas(const SkBitmap& bitmap) {
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400117 mCanvasOwned = std::unique_ptr<SkCanvas>(new SkCanvas(bitmap));
118 mCanvas = mCanvasOwned.get();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400119}
120
Stan Iliev021693b2016-10-17 16:26:15 -0400121SkiaCanvas::~SkiaCanvas() {}
122
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400123void SkiaCanvas::reset(SkCanvas* skiaCanvas) {
Mike Reed6acfe162016-11-18 17:21:09 -0500124 if (mCanvas != skiaCanvas) {
125 mCanvas = skiaCanvas;
126 mCanvasOwned.reset();
127 }
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400128 mSaveStack.reset(nullptr);
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400129}
130
Derek Sollenberger8872b382014-06-23 14:13:53 -0400131// ----------------------------------------------------------------------------
132// Canvas state operations: Replace Bitmap
133// ----------------------------------------------------------------------------
134
John Reckc1b33d62015-04-22 09:04:45 -0700135void SkiaCanvas::setBitmap(const SkBitmap& bitmap) {
Tony Mantler4f641d12017-03-14 22:36:14 +0000136 // deletes the previously owned canvas (if any)
Derek Sollenbergerd01b5912018-10-19 15:55:33 -0400137 mCanvasOwned.reset(new SkCanvas(bitmap));
138 mCanvas = mCanvasOwned.get();
Tony Mantler4f641d12017-03-14 22:36:14 +0000139
Derek Sollenberger8872b382014-06-23 14:13:53 -0400140 // clean up the old save stack
Stan Ilievf50806a2016-10-24 10:40:39 -0400141 mSaveStack.reset(nullptr);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400142}
143
144// ----------------------------------------------------------------------------
145// Canvas state operations
146// ----------------------------------------------------------------------------
147
148bool SkiaCanvas::isOpaque() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400149 return mCanvas->imageInfo().isOpaque();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400150}
151
152int SkiaCanvas::width() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400153 return mCanvas->imageInfo().width();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400154}
155
156int SkiaCanvas::height() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400157 return mCanvas->imageInfo().height();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400158}
159
160// ----------------------------------------------------------------------------
161// Canvas state operations: Save (layer)
162// ----------------------------------------------------------------------------
163
164int SkiaCanvas::getSaveCount() const {
165 return mCanvas->getSaveCount();
166}
167
Florin Malitaeecff562015-12-21 10:43:01 -0500168int SkiaCanvas::save(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400169 int count = mCanvas->save();
170 recordPartialSave(flags);
171 return count;
172}
173
Florin Malita5e271402015-11-04 14:36:02 -0500174// The SkiaCanvas::restore operation layers on the capability to preserve
175// either (or both) the matrix and/or clip state after a SkCanvas::restore
176// operation. It does this by explicitly saving off the clip & matrix state
177// when requested and playing it back after the SkCanvas::restore.
Derek Sollenberger8872b382014-06-23 14:13:53 -0400178void SkiaCanvas::restore() {
Stan Ilievf50806a2016-10-24 10:40:39 -0400179 const auto* rec = this->currentSaveRec();
180 if (!rec) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400181 // Fast path - no record for this frame.
182 mCanvas->restore();
183 return;
184 }
185
Florin Malitaeecff562015-12-21 10:43:01 -0500186 bool preserveMatrix = !(rec->saveFlags & SaveFlags::Matrix);
John Reck1bcacfd2017-11-03 10:12:19 -0700187 bool preserveClip = !(rec->saveFlags & SaveFlags::Clip);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400188
189 SkMatrix savedMatrix;
190 if (preserveMatrix) {
191 savedMatrix = mCanvas->getTotalMatrix();
192 }
193
Stan Ilievf50806a2016-10-24 10:40:39 -0400194 const size_t clipIndex = rec->clipIndex;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400195
196 mCanvas->restore();
Stan Ilievf50806a2016-10-24 10:40:39 -0400197 mSaveStack->pop_back();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400198
199 if (preserveMatrix) {
200 mCanvas->setMatrix(savedMatrix);
201 }
202
Stan Ilievf50806a2016-10-24 10:40:39 -0400203 if (preserveClip) {
204 this->applyPersistentClips(clipIndex);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400205 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400206}
207
208void SkiaCanvas::restoreToCount(int restoreCount) {
209 while (mCanvas->getSaveCount() > restoreCount) {
210 this->restore();
211 }
212}
213
John Recka00eef212020-11-16 12:45:55 -0500214int SkiaCanvas::saveLayer(float left, float top, float right, float bottom, const SkPaint* paint) {
Florin Malitaeecff562015-12-21 10:43:01 -0500215 const SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
John Recka00eef212020-11-16 12:45:55 -0500216 const SkCanvas::SaveLayerRec rec(&bounds, paint);
Florin Malitaeecff562015-12-21 10:43:01 -0500217
Stan Iliev68885e32016-12-14 11:18:34 -0500218 return mCanvas->saveLayer(rec);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400219}
220
John Recka00eef212020-11-16 12:45:55 -0500221int SkiaCanvas::saveLayerAlpha(float left, float top, float right, float bottom, int alpha) {
Florin Malitaeecff562015-12-21 10:43:01 -0500222 if (static_cast<unsigned>(alpha) < 0xFF) {
Yuqian Lifd92ee42016-04-27 17:03:38 -0400223 SkPaint alphaPaint;
224 alphaPaint.setAlpha(alpha);
John Recka00eef212020-11-16 12:45:55 -0500225 return this->saveLayer(left, top, right, bottom, &alphaPaint);
Florin Malitaeecff562015-12-21 10:43:01 -0500226 }
John Recka00eef212020-11-16 12:45:55 -0500227 return this->saveLayer(left, top, right, bottom, nullptr);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400228}
229
Derek Sollenberger24fc9012018-12-07 14:12:12 -0500230int SkiaCanvas::saveUnclippedLayer(int left, int top, int right, int bottom) {
231 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
232 return SkAndroidFrameworkUtils::SaveBehind(mCanvas, &bounds);
233}
234
Mike Reeda6cb58a2021-07-10 13:31:34 -0400235void SkiaCanvas::restoreUnclippedLayer(int restoreCount, const Paint& paint) {
Derek Sollenbergerac33a482019-04-22 16:28:09 -0400236
237 while (mCanvas->getSaveCount() > restoreCount + 1) {
238 this->restore();
239 }
240
241 if (mCanvas->getSaveCount() == restoreCount + 1) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500242 SkCanvasPriv::DrawBehind(mCanvas, filterPaint(paint));
Derek Sollenbergerac33a482019-04-22 16:28:09 -0400243 this->restore();
244 }
245}
246
Stan Ilievf50806a2016-10-24 10:40:39 -0400247const SkiaCanvas::SaveRec* SkiaCanvas::currentSaveRec() const {
John Reck1bcacfd2017-11-03 10:12:19 -0700248 const SaveRec* rec = mSaveStack ? static_cast<const SaveRec*>(mSaveStack->back()) : nullptr;
Stan Ilievf50806a2016-10-24 10:40:39 -0400249 int currentSaveCount = mCanvas->getSaveCount();
250 SkASSERT(!rec || currentSaveCount >= rec->saveCount);
251
252 return (rec && rec->saveCount == currentSaveCount) ? rec : nullptr;
253}
254
Alec Mouri655a5e42022-09-12 17:49:17 +0000255void SkiaCanvas::punchHole(const SkRRect& rect, float alpha) {
Nader Jawad2dc632a2021-03-29 18:51:29 -0700256 SkPaint paint = SkPaint();
Alec Mouri655a5e42022-09-12 17:49:17 +0000257 paint.setColor(SkColors::kBlack);
258 paint.setAlphaf(alpha);
259 paint.setBlendMode(SkBlendMode::kDstOut);
Nader Jawad2dc632a2021-03-29 18:51:29 -0700260 mCanvas->drawRRect(rect, paint);
261}
262
Derek Sollenberger8872b382014-06-23 14:13:53 -0400263// ----------------------------------------------------------------------------
264// functions to emulate legacy SaveFlags (i.e. independent matrix/clip flags)
265// ----------------------------------------------------------------------------
266
Florin Malitaeecff562015-12-21 10:43:01 -0500267void SkiaCanvas::recordPartialSave(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400268 // A partial save is a save operation which doesn't capture the full canvas state.
Florin Malitaeecff562015-12-21 10:43:01 -0500269 // (either SaveFlags::Matrix or SaveFlags::Clip is missing).
Derek Sollenberger8872b382014-06-23 14:13:53 -0400270
271 // Mask-out non canvas state bits.
Florin Malitaeecff562015-12-21 10:43:01 -0500272 flags &= SaveFlags::MatrixClip;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400273
Florin Malitaeecff562015-12-21 10:43:01 -0500274 if (flags == SaveFlags::MatrixClip) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400275 // not a partial save.
276 return;
277 }
278
Stan Ilievf50806a2016-10-24 10:40:39 -0400279 if (!mSaveStack) {
Ben Wagnerd1cbc162015-08-19 12:45:09 -0400280 mSaveStack.reset(new SkDeque(sizeof(struct SaveRec), 8));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400281 }
282
283 SaveRec* rec = static_cast<SaveRec*>(mSaveStack->push_back());
Florin Malita5e271402015-11-04 14:36:02 -0500284 rec->saveCount = mCanvas->getSaveCount();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400285 rec->saveFlags = flags;
Stan Ilievf50806a2016-10-24 10:40:39 -0400286 rec->clipIndex = mClipStack.size();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400287}
288
Stan Ilievf50806a2016-10-24 10:40:39 -0400289template <typename T>
Mike Reed6e49c9f2016-12-02 15:36:59 -0500290void SkiaCanvas::recordClip(const T& clip, SkClipOp op) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400291 // Only need tracking when in a partial save frame which
292 // doesn't restore the clip.
293 const SaveRec* rec = this->currentSaveRec();
294 if (rec && !(rec->saveFlags & SaveFlags::Clip)) {
295 mClipStack.emplace_back(clip, op, mCanvas->getTotalMatrix());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400296 }
297}
298
Stan Ilievf50806a2016-10-24 10:40:39 -0400299// Applies and optionally removes all clips >= index.
300void SkiaCanvas::applyPersistentClips(size_t clipStartIndex) {
301 SkASSERT(clipStartIndex <= mClipStack.size());
302 const auto begin = mClipStack.cbegin() + clipStartIndex;
303 const auto end = mClipStack.cend();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400304
Stan Ilievf50806a2016-10-24 10:40:39 -0400305 // Clip application mutates the CTM.
306 const SkMatrix saveMatrix = mCanvas->getTotalMatrix();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400307
Stan Ilievf50806a2016-10-24 10:40:39 -0400308 for (auto clip = begin; clip != end; ++clip) {
Mike Reed6acfe162016-11-18 17:21:09 -0500309 clip->apply(mCanvas);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400310 }
311
Stan Ilievf50806a2016-10-24 10:40:39 -0400312 mCanvas->setMatrix(saveMatrix);
313
314 // If the current/post-restore save rec is also persisting clips, we
315 // leave them on the stack to be reapplied part of the next restore().
316 // Otherwise we're done and just pop them.
317 const auto* rec = this->currentSaveRec();
318 if (!rec || (rec->saveFlags & SaveFlags::Clip)) {
319 mClipStack.erase(begin, end);
320 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400321}
322
323// ----------------------------------------------------------------------------
324// Canvas state operations: Matrix
325// ----------------------------------------------------------------------------
326
327void SkiaCanvas::getMatrix(SkMatrix* outMatrix) const {
328 *outMatrix = mCanvas->getTotalMatrix();
329}
330
331void SkiaCanvas::setMatrix(const SkMatrix& matrix) {
332 mCanvas->setMatrix(matrix);
333}
334
335void SkiaCanvas::concat(const SkMatrix& matrix) {
336 mCanvas->concat(matrix);
337}
338
339void SkiaCanvas::rotate(float degrees) {
340 mCanvas->rotate(degrees);
341}
342
343void SkiaCanvas::scale(float sx, float sy) {
344 mCanvas->scale(sx, sy);
345}
346
347void SkiaCanvas::skew(float sx, float sy) {
348 mCanvas->skew(sx, sy);
349}
350
351void SkiaCanvas::translate(float dx, float dy) {
352 mCanvas->translate(dx, dy);
353}
354
355// ----------------------------------------------------------------------------
356// Canvas state operations: Clips
357// ----------------------------------------------------------------------------
358
359// This function is a mirror of SkCanvas::getClipBounds except that it does
360// not outset the edge of the clip to account for anti-aliasing. There is
361// a skia bug to investigate pushing this logic into back into skia.
362// (see https://code.google.com/p/skia/issues/detail?id=1303)
363bool SkiaCanvas::getClipBounds(SkRect* outRect) const {
364 SkIRect ibounds;
Mike Reed5e438982017-01-25 08:23:25 -0500365 if (!mCanvas->getDeviceClipBounds(&ibounds)) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400366 return false;
367 }
368
369 SkMatrix inverse;
370 // if we can't invert the CTM, we can't return local clip bounds
371 if (!mCanvas->getTotalMatrix().invert(&inverse)) {
372 if (outRect) {
373 outRect->setEmpty();
374 }
375 return false;
376 }
377
378 if (NULL != outRect) {
379 SkRect r = SkRect::Make(ibounds);
380 inverse.mapRect(outRect, r);
381 }
382 return true;
383}
384
385bool SkiaCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
386 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
387 return mCanvas->quickReject(bounds);
388}
389
390bool SkiaCanvas::quickRejectPath(const SkPath& path) const {
391 return mCanvas->quickReject(path);
392}
393
Mike Reed6e49c9f2016-12-02 15:36:59 -0500394bool SkiaCanvas::clipRect(float left, float top, float right, float bottom, SkClipOp op) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400395 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Stan Ilievf50806a2016-10-24 10:40:39 -0400396 this->recordClip(rect, op);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400397 mCanvas->clipRect(rect, op);
Chris Craik5ec6a282015-06-23 15:42:12 -0700398 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400399}
400
Mike Reed6e49c9f2016-12-02 15:36:59 -0500401bool SkiaCanvas::clipPath(const SkPath* path, SkClipOp op) {
Derek Sollenbergerf7d98f42017-04-17 11:27:36 -0400402 this->recordClip(*path, op);
Nader Jawade431e312019-12-04 14:13:18 -0800403 mCanvas->clipPath(*path, op, true);
Chris Craik5ec6a282015-06-23 15:42:12 -0700404 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400405}
406
Michael Ludwig70cf50c22021-07-21 17:02:39 +0000407bool SkiaCanvas::replaceClipRect_deprecated(float left, float top, float right, float bottom) {
408 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
409
410 // Emulated clip rects are not recorded for partial saves, since
411 // partial saves have been removed from the public API.
412 SkAndroidFrameworkUtils::ResetClip(mCanvas);
413 mCanvas->clipRect(rect, SkClipOp::kIntersect);
414 return !mCanvas->isClipEmpty();
415}
416
417bool SkiaCanvas::replaceClipPath_deprecated(const SkPath* path) {
418 SkAndroidFrameworkUtils::ResetClip(mCanvas);
419 mCanvas->clipPath(*path, SkClipOp::kIntersect, true);
420 return !mCanvas->isClipEmpty();
421}
422
Derek Sollenberger8872b382014-06-23 14:13:53 -0400423// ----------------------------------------------------------------------------
424// Canvas state operations: Filters
425// ----------------------------------------------------------------------------
426
Ben Wagner0ed10be2018-06-28 17:08:16 -0400427PaintFilter* SkiaCanvas::getPaintFilter() {
428 return mPaintFilter.get();
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400429}
430
Ben Wagner0ed10be2018-06-28 17:08:16 -0400431void SkiaCanvas::setPaintFilter(sk_sp<PaintFilter> paintFilter) {
432 mPaintFilter = std::move(paintFilter);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400433}
434
435// ----------------------------------------------------------------------------
Matt Sarettd0814db2017-04-13 09:33:18 -0400436// Canvas state operations: Capture
437// ----------------------------------------------------------------------------
438
439SkCanvasState* SkiaCanvas::captureCanvasState() const {
440 SkCanvas* canvas = mCanvas;
441 if (mCanvasOwned) {
442 // Important to use the underlying SkCanvas, not the wrapper.
443 canvas = mCanvasOwned.get();
444 }
445
446 // Workarounds for http://crbug.com/271096: SW draw only supports
447 // translate & scale transforms, and a simple rectangular clip.
448 // (This also avoids significant wasted time in calling
449 // SkCanvasStateUtils::CaptureCanvasState when the clip is complex).
John Reck1bcacfd2017-11-03 10:12:19 -0700450 if (!canvas->isClipRect() || (canvas->getTotalMatrix().getType() &
451 ~(SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask))) {
452 return nullptr;
Matt Sarettd0814db2017-04-13 09:33:18 -0400453 }
454
455 return SkCanvasStateUtils::CaptureCanvasState(canvas);
456}
457
458// ----------------------------------------------------------------------------
Derek Sollenberger8872b382014-06-23 14:13:53 -0400459// Canvas draw operations
460// ----------------------------------------------------------------------------
461
Mike Reed260ab722016-10-07 15:59:20 -0400462void SkiaCanvas::drawColor(int color, SkBlendMode mode) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400463 mCanvas->drawColor(color, mode);
464}
465
Mike Reeda6cb58a2021-07-10 13:31:34 -0400466void SkiaCanvas::onFilterPaint(Paint& paint) {
Ben Wagner0ed10be2018-06-28 17:08:16 -0400467 if (mPaintFilter) {
Mike Reeda6cb58a2021-07-10 13:31:34 -0400468 mPaintFilter->filterFullPaint(&paint);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400469 }
Ben Wagner0ed10be2018-06-28 17:08:16 -0400470}
471
Mike Reeda6cb58a2021-07-10 13:31:34 -0400472void SkiaCanvas::drawPaint(const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500473 mCanvas->drawPaint(filterPaint(paint));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400474}
475
476// ----------------------------------------------------------------------------
477// Canvas draw operations: Geometry
478// ----------------------------------------------------------------------------
479
Mike Reedc2dbc032019-07-25 12:28:29 -0400480void SkiaCanvas::drawPoints(const float* points, int count, const Paint& paint,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400481 SkCanvas::PointMode mode) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500482 if (CC_UNLIKELY(count < 2 || paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400483 // convert the floats into SkPoints
John Reck1bcacfd2017-11-03 10:12:19 -0700484 count >>= 1; // now it is the number of points
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400485 std::unique_ptr<SkPoint[]> pts(new SkPoint[count]);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400486 for (int i = 0; i < count; i++) {
487 pts[i].set(points[0], points[1]);
488 points += 2;
489 }
Mike Reedc2dbc032019-07-25 12:28:29 -0400490
Mike Reedbb4cb482021-02-22 14:21:20 -0500491 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawPoints(mode, count, pts.get(), p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400492}
493
Mike Reedc2dbc032019-07-25 12:28:29 -0400494void SkiaCanvas::drawPoint(float x, float y, const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500495 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawPoint(x, y, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400496}
497
Mike Reedc2dbc032019-07-25 12:28:29 -0400498void SkiaCanvas::drawPoints(const float* points, int count, const Paint& paint) {
499 this->drawPoints(points, count, paint, SkCanvas::kPoints_PointMode);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400500}
501
502void SkiaCanvas::drawLine(float startX, float startY, float stopX, float stopY,
Mike Reedc2dbc032019-07-25 12:28:29 -0400503 const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500504 applyLooper(&paint,
505 [&](const SkPaint& p) { mCanvas->drawLine(startX, startY, stopX, stopY, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400506}
507
Mike Reedc2dbc032019-07-25 12:28:29 -0400508void SkiaCanvas::drawLines(const float* points, int count, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500509 if (CC_UNLIKELY(count < 4 || paint.nothingToDraw())) return;
Mike Reedc2dbc032019-07-25 12:28:29 -0400510 this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400511}
512
Mike Reedc2dbc032019-07-25 12:28:29 -0400513void SkiaCanvas::drawRect(float left, float top, float right, float bottom, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500514 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Mike Reedbb4cb482021-02-22 14:21:20 -0500515 applyLooper(&paint, [&](const SkPaint& p) {
Mike Reedc2dbc032019-07-25 12:28:29 -0400516 mCanvas->drawRect({left, top, right, bottom}, p);
517 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400518}
519
Mike Reedc2dbc032019-07-25 12:28:29 -0400520void SkiaCanvas::drawRegion(const SkRegion& region, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500521 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Mike Reedbb4cb482021-02-22 14:21:20 -0500522 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawRegion(region, p); });
Derek Sollenberger94394b32015-07-10 09:58:41 -0400523}
524
John Reck1bcacfd2017-11-03 10:12:19 -0700525void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom, float rx, float ry,
Mike Reedc2dbc032019-07-25 12:28:29 -0400526 const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500527 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400528 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Mike Reedbb4cb482021-02-22 14:21:20 -0500529 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawRoundRect(rect, rx, ry, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400530}
531
Nader Jawadadfe1d92018-09-27 12:27:36 -0700532void SkiaCanvas::drawDoubleRoundRect(const SkRRect& outer, const SkRRect& inner,
Mike Reedc2dbc032019-07-25 12:28:29 -0400533 const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500534 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawDRRect(outer, inner, p); });
Nader Jawadadfe1d92018-09-27 12:27:36 -0700535}
536
Mike Reedc2dbc032019-07-25 12:28:29 -0400537void SkiaCanvas::drawCircle(float x, float y, float radius, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500538 if (CC_UNLIKELY(radius <= 0 || paint.nothingToDraw())) return;
Mike Reedbb4cb482021-02-22 14:21:20 -0500539 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawCircle(x, y, radius, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400540}
541
Mike Reedc2dbc032019-07-25 12:28:29 -0400542void SkiaCanvas::drawOval(float left, float top, float right, float bottom, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500543 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400544 SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
Mike Reedbb4cb482021-02-22 14:21:20 -0500545 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawOval(oval, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400546}
547
John Reck1bcacfd2017-11-03 10:12:19 -0700548void SkiaCanvas::drawArc(float left, float top, float right, float bottom, float startAngle,
Mike Reedc2dbc032019-07-25 12:28:29 -0400549 float sweepAngle, bool useCenter, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500550 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400551 SkRect arc = SkRect::MakeLTRB(left, top, right, bottom);
Mike Reedbb4cb482021-02-22 14:21:20 -0500552 applyLooper(&paint, [&](const SkPaint& p) {
Mike Reedc2dbc032019-07-25 12:28:29 -0400553 if (fabs(sweepAngle) >= 360.0f) {
554 mCanvas->drawOval(arc, p);
555 } else {
556 mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, p);
557 }
558 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400559}
560
Mike Reedc2dbc032019-07-25 12:28:29 -0400561void SkiaCanvas::drawPath(const SkPath& path, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500562 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Stan Iliev6dcfdec2017-08-15 16:42:05 -0400563 if (CC_UNLIKELY(path.isEmpty() && (!path.isInverseFillType()))) {
564 return;
565 }
Mike Reedbb4cb482021-02-22 14:21:20 -0500566 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawPath(path, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400567}
568
Mike Reedc2dbc032019-07-25 12:28:29 -0400569void SkiaCanvas::drawVertices(const SkVertices* vertices, SkBlendMode mode, const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500570 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawVertices(vertices, mode, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400571}
572
Angel Aguayo90c46ee2022-11-08 23:42:09 +0000573void SkiaCanvas::drawMesh(const SkMesh& mesh, sk_sp<SkBlender> blender, const SkPaint& paint) {
574 mCanvas->drawMesh(mesh, blender, paint);
575}
576
Derek Sollenberger8872b382014-06-23 14:13:53 -0400577// ----------------------------------------------------------------------------
578// Canvas draw operations: Bitmaps
579// ----------------------------------------------------------------------------
580
Mike Reedc2dbc032019-07-25 12:28:29 -0400581void SkiaCanvas::drawBitmap(Bitmap& bitmap, float left, float top, const Paint* paint) {
582 auto image = bitmap.makeImage();
Mike Reeda6cb58a2021-07-10 13:31:34 -0400583 applyLooper(paint, [&](const Paint& p) {
584 mCanvas->drawImage(image, left, top, p.sampling(), &p);
Mike Reedc2dbc032019-07-25 12:28:29 -0400585 });
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400586}
587
Mike Reedc2dbc032019-07-25 12:28:29 -0400588void SkiaCanvas::drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const Paint* paint) {
589 auto image = bitmap.makeImage();
Mike Reed6acfe162016-11-18 17:21:09 -0500590 SkAutoCanvasRestore acr(mCanvas, true);
Mike Reed70ffbf92014-12-08 17:03:30 -0500591 mCanvas->concat(matrix);
Mike Reeda6cb58a2021-07-10 13:31:34 -0400592 applyLooper(paint, [&](const Paint& p) {
593 mCanvas->drawImage(image, 0, 0, p.sampling(), &p);
Mike Reedc2dbc032019-07-25 12:28:29 -0400594 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400595}
596
John Reck1bcacfd2017-11-03 10:12:19 -0700597void SkiaCanvas::drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight,
598 float srcBottom, float dstLeft, float dstTop, float dstRight,
Mike Reedc2dbc032019-07-25 12:28:29 -0400599 float dstBottom, const Paint* paint) {
600 auto image = bitmap.makeImage();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400601 SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
602 SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400603
Mike Reeda6cb58a2021-07-10 13:31:34 -0400604 applyLooper(paint, [&](const Paint& p) {
605 mCanvas->drawImageRect(image, srcRect, dstRect, p.sampling(), &p,
Mike Reed7994a312021-01-28 18:06:26 -0500606 SkCanvas::kFast_SrcRectConstraint);
Mike Reedc2dbc032019-07-25 12:28:29 -0400607 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400608}
609
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400610void SkiaCanvas::drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight,
Mike Reedc2dbc032019-07-25 12:28:29 -0400611 const float* vertices, const int* colors, const Paint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400612 const int ptCount = (meshWidth + 1) * (meshHeight + 1);
613 const int indexCount = meshWidth * meshHeight * 6;
Mike Reed871cd2d2017-03-17 10:15:52 -0400614 uint32_t flags = SkVertices::kHasTexCoords_BuilderFlag;
615 if (colors) {
616 flags |= SkVertices::kHasColors_BuilderFlag;
617 }
Mike Reed826deef2017-04-04 15:32:04 -0400618 SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, ptCount, indexCount, flags);
Mike Reed871cd2d2017-03-17 10:15:52 -0400619 memcpy(builder.positions(), vertices, ptCount * sizeof(SkPoint));
620 if (colors) {
621 memcpy(builder.colors(), colors, ptCount * sizeof(SkColor));
622 }
623 SkPoint* texs = builder.texCoords();
624 uint16_t* indices = builder.indices();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400625
626 // cons up texture coordinates and indices
627 {
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400628 const SkScalar w = SkIntToScalar(bitmap.width());
629 const SkScalar h = SkIntToScalar(bitmap.height());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400630 const SkScalar dx = w / meshWidth;
631 const SkScalar dy = h / meshHeight;
632
633 SkPoint* texsPtr = texs;
634 SkScalar y = 0;
635 for (int i = 0; i <= meshHeight; i++) {
636 if (i == meshHeight) {
637 y = h; // to ensure numerically we hit h exactly
638 }
639 SkScalar x = 0;
640 for (int j = 0; j < meshWidth; j++) {
641 texsPtr->set(x, y);
642 texsPtr += 1;
643 x += dx;
644 }
645 texsPtr->set(w, y);
646 texsPtr += 1;
647 y += dy;
648 }
649 SkASSERT(texsPtr - texs == ptCount);
650 }
651
652 // cons up indices
653 {
654 uint16_t* indexPtr = indices;
655 int index = 0;
656 for (int i = 0; i < meshHeight; i++) {
657 for (int j = 0; j < meshWidth; j++) {
658 // lower-left triangle
659 *indexPtr++ = index;
660 *indexPtr++ = index + meshWidth + 1;
661 *indexPtr++ = index + meshWidth + 2;
662 // upper-right triangle
663 *indexPtr++ = index;
664 *indexPtr++ = index + meshWidth + 2;
665 *indexPtr++ = index + 1;
666 // bump to the next cell
667 index += 1;
668 }
669 // bump to the next row
670 index += 1;
671 }
672 SkASSERT(indexPtr - indices == indexCount);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400673 }
674
John Reck1bcacfd2017-11-03 10:12:19 -0700675// double-check that we have legal indices
Derek Sollenberger8872b382014-06-23 14:13:53 -0400676#ifdef SK_DEBUG
677 {
678 for (int i = 0; i < indexCount; i++) {
679 SkASSERT((unsigned)indices[i] < (unsigned)ptCount);
680 }
681 }
682#endif
683
Mike Reed7994a312021-01-28 18:06:26 -0500684 auto image = bitmap.makeImage();
685
Derek Sollenberger8872b382014-06-23 14:13:53 -0400686 // cons-up a shader for the bitmap
Mike Reedc2dbc032019-07-25 12:28:29 -0400687 Paint pnt;
688 if (paint) {
689 pnt = *paint;
690 }
Mike Reeda6cb58a2021-07-10 13:31:34 -0400691 SkSamplingOptions sampling = pnt.sampling();
Mike Reed7994a312021-01-28 18:06:26 -0500692 pnt.setShader(image->makeShader(sampling));
693
Mike Reedc2dbc032019-07-25 12:28:29 -0400694 auto v = builder.detach();
Mike Reeda6cb58a2021-07-10 13:31:34 -0400695 applyLooper(&pnt, [&](const Paint& p) {
Mike Reed7994a312021-01-28 18:06:26 -0500696 SkPaint copy(p);
Mike Reeda6cb58a2021-07-10 13:31:34 -0400697 auto s = p.sampling();
Mike Reed7994a312021-01-28 18:06:26 -0500698 if (s != sampling) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500699 // applyLooper changed the quality?
Mike Reed7994a312021-01-28 18:06:26 -0500700 copy.setShader(image->makeShader(s));
701 }
702 mCanvas->drawVertices(v, SkBlendMode::kModulate, copy);
Mike Reedc2dbc032019-07-25 12:28:29 -0400703 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400704}
705
John Reck1bcacfd2017-11-03 10:12:19 -0700706void SkiaCanvas::drawNinePatch(Bitmap& bitmap, const Res_png_9patch& chunk, float dstLeft,
707 float dstTop, float dstRight, float dstBottom,
Mike Reedc2dbc032019-07-25 12:28:29 -0400708 const Paint* paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400709 SkCanvas::Lattice lattice;
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400710 NinePatchUtils::SetLatticeDivs(&lattice, chunk, bitmap.width(), bitmap.height());
Stan Ilievf50806a2016-10-24 10:40:39 -0400711
Stan Ilieve12d7312017-12-04 14:48:27 -0500712 lattice.fRectTypes = nullptr;
713 lattice.fColors = nullptr;
Stan Ilievf50806a2016-10-24 10:40:39 -0400714 int numFlags = 0;
Stan Iliev021693b2016-10-17 16:26:15 -0400715 if (chunk.numColors > 0 && chunk.numColors == NinePatchUtils::NumDistinctRects(lattice)) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400716 // We can expect the framework to give us a color for every distinct rect.
717 // Skia requires a flag for every rect.
718 numFlags = (lattice.fXCount + 1) * (lattice.fYCount + 1);
719 }
720
Stan Ilieve12d7312017-12-04 14:48:27 -0500721 SkAutoSTMalloc<25, SkCanvas::Lattice::RectType> flags(numFlags);
722 SkAutoSTMalloc<25, SkColor> colors(numFlags);
Stan Ilievf50806a2016-10-24 10:40:39 -0400723 if (numFlags > 0) {
Stan Ilieve12d7312017-12-04 14:48:27 -0500724 NinePatchUtils::SetLatticeFlags(&lattice, flags.get(), numFlags, chunk, colors.get());
Stan Ilievf50806a2016-10-24 10:40:39 -0400725 }
726
727 lattice.fBounds = nullptr;
728 SkRect dst = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Mike Reedc2dbc032019-07-25 12:28:29 -0400729 auto image = bitmap.makeImage();
Mike Reeda6cb58a2021-07-10 13:31:34 -0400730 applyLooper(paint, [&](const Paint& p) {
731 mCanvas->drawImageLattice(image.get(), lattice, dst, p.filterMode(), &p);
Mike Reedc2dbc032019-07-25 12:28:29 -0400732 });
Derek Sollenbergeredca3202015-07-10 13:56:39 -0400733}
734
Derek Sollenberger2d142132018-01-22 10:25:26 -0500735double SkiaCanvas::drawAnimatedImage(AnimatedImageDrawable* imgDrawable) {
736 return imgDrawable->drawStaging(mCanvas);
Leon Scroggins III671cce22018-01-14 16:52:17 -0500737}
738
Doris Liu766431a2016-02-04 22:17:11 +0000739void SkiaCanvas::drawVectorDrawable(VectorDrawableRoot* vectorDrawable) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800740 vectorDrawable->drawStaging(this);
Doris Liu766431a2016-02-04 22:17:11 +0000741}
742
Derek Sollenberger8872b382014-06-23 14:13:53 -0400743// ----------------------------------------------------------------------------
744// Canvas draw operations: Text
745// ----------------------------------------------------------------------------
746
Mike Reed2dfd55d2019-01-08 16:19:03 -0500747void SkiaCanvas::drawGlyphs(ReadGlyphFunc glyphFunc, int count, const Paint& paint, float x,
Seigo Nonaka63af7ba2020-09-21 23:07:43 -0700748 float y, float totalAdvance) {
Stan Iliev0b58d992017-03-30 18:22:27 -0400749 if (count <= 0 || paint.nothingToDraw()) return;
Mike Reedf6d86ac2019-01-18 14:13:23 -0500750 Paint paintCopy(paint);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400751 if (mPaintFilter) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500752 mPaintFilter->filterFullPaint(&paintCopy);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400753 }
Mike Reedf6d86ac2019-01-18 14:13:23 -0500754 const SkFont& font = paintCopy.getSkFont();
Stan Iliev7717e222018-02-05 18:04:11 -0500755 // Stroke with a hairline is drawn on HW with a fill style for compatibility with Android O and
756 // older.
John Recke170fb62018-05-07 08:12:07 -0700757 if (!mCanvasOwned && sApiLevel <= 27 && paintCopy.getStrokeWidth() <= 0 &&
758 paintCopy.getStyle() == SkPaint::kStroke_Style) {
Stan Iliev7717e222018-02-05 18:04:11 -0500759 paintCopy.setStyle(SkPaint::kFill_Style);
760 }
Stan Ilievf50806a2016-10-24 10:40:39 -0400761
Stan Ilievf50806a2016-10-24 10:40:39 -0400762 SkTextBlobBuilder builder;
Mike Reed2e204fc2019-01-28 13:31:36 -0500763 const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(font, count);
Stan Iliev0b58d992017-03-30 18:22:27 -0400764 glyphFunc(buffer.glyphs, buffer.pos);
Stan Ilievf50806a2016-10-24 10:40:39 -0400765
766 sk_sp<SkTextBlob> textBlob(builder.make());
Nathaniel Nifong52d37772020-01-10 16:12:41 -0500767
Mike Reedbb4cb482021-02-22 14:21:20 -0500768 applyLooper(&paintCopy, [&](const SkPaint& p) { mCanvas->drawTextBlob(textBlob, 0, 0, p); });
Stan Ilievf50806a2016-10-24 10:40:39 -0400769 drawTextDecorations(x, y, totalAdvance, paintCopy);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400770}
771
Yuqian Liafc221492016-07-18 13:07:42 -0400772void SkiaCanvas::drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
Mike Reed2dfd55d2019-01-08 16:19:03 -0500773 const Paint& paint, const SkPath& path, size_t start,
John Reck1bcacfd2017-11-03 10:12:19 -0700774 size_t end) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500775 Paint paintCopy(paint);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400776 if (mPaintFilter) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500777 mPaintFilter->filterFullPaint(&paintCopy);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400778 }
Mike Reedf6d86ac2019-01-18 14:13:23 -0500779 const SkFont& font = paintCopy.getSkFont();
Derek Sollenberger415a74b2018-03-14 15:03:13 -0400780
Yuqian Liafc221492016-07-18 13:07:42 -0400781 const int N = end - start;
Mike Reed4a4b1be2019-01-01 15:43:06 -0500782 SkTextBlobBuilder builder;
783 auto rec = builder.allocRunRSXform(font, N);
784 SkRSXform* xform = (SkRSXform*)rec.pos;
785 uint16_t* glyphs = rec.glyphs;
Yuqian Liafc221492016-07-18 13:07:42 -0400786 SkPathMeasure meas(path, false);
787
788 for (size_t i = start; i < end; i++) {
789 glyphs[i - start] = layout.getGlyphId(i);
Stan Ilievc6c96dd2018-01-10 16:06:04 -0500790 float halfWidth = layout.getCharAdvance(i) * 0.5f;
791 float x = hOffset + layout.getX(i) + halfWidth;
Yuqian Liafc221492016-07-18 13:07:42 -0400792 float y = vOffset + layout.getY(i);
793
794 SkPoint pos;
795 SkVector tan;
796 if (!meas.getPosTan(x, &pos, &tan)) {
797 pos.set(x, y);
798 tan.set(1, 0);
799 }
800 xform[i - start].fSCos = tan.x();
801 xform[i - start].fSSin = tan.y();
Stan Ilievc6c96dd2018-01-10 16:06:04 -0500802 xform[i - start].fTx = pos.x() - tan.y() * y - halfWidth * tan.x();
803 xform[i - start].fTy = pos.y() + tan.x() * y - halfWidth * tan.y();
Yuqian Liafc221492016-07-18 13:07:42 -0400804 }
Leon Scroggins III950f2aa2020-04-29 13:46:00 -0400805
806 sk_sp<SkTextBlob> textBlob(builder.make());
807
Mike Reedbb4cb482021-02-22 14:21:20 -0500808 applyLooper(&paintCopy, [&](const SkPaint& p) { mCanvas->drawTextBlob(textBlob, 0, 0, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400809}
810
Derek Sollenberger6f485562015-07-30 10:00:39 -0400811// ----------------------------------------------------------------------------
812// Canvas draw operations: Animations
813// ----------------------------------------------------------------------------
814
Derek Sollenberger6f485562015-07-30 10:00:39 -0400815void SkiaCanvas::drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
John Reck1bcacfd2017-11-03 10:12:19 -0700816 uirenderer::CanvasPropertyPrimitive* top,
817 uirenderer::CanvasPropertyPrimitive* right,
818 uirenderer::CanvasPropertyPrimitive* bottom,
819 uirenderer::CanvasPropertyPrimitive* rx,
820 uirenderer::CanvasPropertyPrimitive* ry,
821 uirenderer::CanvasPropertyPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -0400822 sk_sp<uirenderer::skiapipeline::AnimatedRoundRect> drawable(
John Reck1bcacfd2017-11-03 10:12:19 -0700823 new uirenderer::skiapipeline::AnimatedRoundRect(left, top, right, bottom, rx, ry,
824 paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400825 mCanvas->drawDrawable(drawable.get());
826}
827
John Reck1bcacfd2017-11-03 10:12:19 -0700828void SkiaCanvas::drawCircle(uirenderer::CanvasPropertyPrimitive* x,
829 uirenderer::CanvasPropertyPrimitive* y,
830 uirenderer::CanvasPropertyPrimitive* radius,
831 uirenderer::CanvasPropertyPaint* paint) {
832 sk_sp<uirenderer::skiapipeline::AnimatedCircle> drawable(
833 new uirenderer::skiapipeline::AnimatedCircle(x, y, radius, paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400834 mCanvas->drawDrawable(drawable.get());
835}
836
Lucas Dupin00af5272021-04-29 20:30:01 -0700837void SkiaCanvas::drawRipple(const uirenderer::skiapipeline::RippleDrawableParams& params) {
838 uirenderer::skiapipeline::AnimatedRippleDrawable::draw(mCanvas, params);
Derek Sollenbergerdf301aa2020-12-17 11:06:03 -0500839}
840
John Reck894e85a2019-07-15 16:16:44 -0700841void SkiaCanvas::drawPicture(const SkPicture& picture) {
842 // TODO: Change to mCanvas->drawPicture()? SkCanvas::drawPicture seems to be
843 // where the logic is for playback vs. ref picture. Using picture.playback here
844 // to stay behavior-identical for now, but should revisit this at some point.
845 picture.playback(mCanvas);
846}
847
Derek Sollenberger6f485562015-07-30 10:00:39 -0400848// ----------------------------------------------------------------------------
849// Canvas draw operations: View System
850// ----------------------------------------------------------------------------
851
Stan Ilievf50806a2016-10-24 10:40:39 -0400852void SkiaCanvas::drawLayer(uirenderer::DeferredLayerUpdater* layerUpdater) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400853 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw Layers");
854}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400855
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400856void SkiaCanvas::drawRenderNode(uirenderer::RenderNode* renderNode) {
857 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw RenderNodes");
858}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400859
John Reck1bcacfd2017-11-03 10:12:19 -0700860} // namespace android