blob: 023d6bf0b67364cbea364bd56510b772af185533 [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"
Derek Sollenbergerc1908132016-07-15 10:28:16 -040021#include "VectorDrawable.h"
sergeyvaed7f582016-10-14 16:30:21 -070022#include "hwui/Bitmap.h"
Yuqian Liafc221492016-07-18 13:07:42 -040023#include "hwui/MinikinUtils.h"
Ben Wagner0ed10be2018-06-28 17:08:16 -040024#include "hwui/PaintFilter.h"
Stan Iliev021693b2016-10-17 16:26:15 -040025#include "pipeline/skia/AnimatedDrawables.h"
Nader Jawad2dc632a2021-03-29 18:51:29 -070026#include "pipeline/skia/HolePunch.h"
Derek Sollenbergerc1908132016-07-15 10:28:16 -040027
Derek Sollenberger24fc9012018-12-07 14:12:12 -050028#include <SkAndroidFrameworkUtils.h>
Leon Scroggins III671cce22018-01-14 16:52:17 -050029#include <SkAnimatedImage.h>
Kevin Lubick856848e2022-02-24 16:24:09 +000030#include <SkBitmap.h>
Derek Sollenbergerac33a482019-04-22 16:28:09 -040031#include <SkCanvasPriv.h>
Matt Sarettd0814db2017-04-13 09:33:18 -040032#include <SkCanvasStateUtils.h>
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -040033#include <SkColorFilter.h>
John Reck849911a2015-01-20 07:51:14 -080034#include <SkDeque.h>
John Reck1bcacfd2017-11-03 10:12:19 -070035#include <SkDrawable.h>
Mike Reed1c79eab2018-11-21 11:01:57 -050036#include <SkFont.h>
John Reck849911a2015-01-20 07:51:14 -080037#include <SkGraphics.h>
Derek Sollenberger6f485562015-07-30 10:00:39 -040038#include <SkImage.h>
Matt Sarett62feb3a2016-09-20 10:34:11 -040039#include <SkImagePriv.h>
Kevin Lubick856848e2022-02-24 16:24:09 +000040#include <SkMatrix.h>
41#include <SkPaint.h>
Leon Scroggins III671cce22018-01-14 16:52:17 -050042#include <SkPicture.h>
Yuqian Liafc221492016-07-18 13:07:42 -040043#include <SkRSXform.h>
Kevin Lubick856848e2022-02-24 16:24:09 +000044#include <SkRRect.h>
45#include <SkRect.h>
46#include <SkRefCnt.h>
John Reck849911a2015-01-20 07:51:14 -080047#include <SkShader.h>
John Reck849911a2015-01-20 07:51:14 -080048#include <SkTemplates.h>
Stan Ilievf50806a2016-10-24 10:40:39 -040049#include <SkTextBlob.h>
Brian Osmane3b9a122020-04-01 12:24:19 -040050#include <SkVertices.h>
Derek Sollenberger8872b382014-06-23 14:13:53 -040051
Ben Wagner60126ef2015-08-07 12:13:48 -040052#include <memory>
Ben Wagner0ed10be2018-06-28 17:08:16 -040053#include <optional>
54#include <utility>
Ben Wagner60126ef2015-08-07 12:13:48 -040055
Derek Sollenberger8872b382014-06-23 14:13:53 -040056namespace android {
57
Stan Ilievf50806a2016-10-24 10:40:39 -040058using uirenderer::PaintUtils;
59
John Reckc1b33d62015-04-22 09:04:45 -070060Canvas* Canvas::create_canvas(const SkBitmap& bitmap) {
Derek Sollenberger8872b382014-06-23 14:13:53 -040061 return new SkiaCanvas(bitmap);
62}
63
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040064Canvas* Canvas::create_canvas(SkCanvas* skiaCanvas) {
65 return new SkiaCanvas(skiaCanvas);
Derek Sollenberger8872b382014-06-23 14:13:53 -040066}
67
Stan Ilievf50806a2016-10-24 10:40:39 -040068SkiaCanvas::SkiaCanvas() {}
69
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040070SkiaCanvas::SkiaCanvas(SkCanvas* canvas) : mCanvas(canvas) {}
Stan Ilievf50806a2016-10-24 10:40:39 -040071
John Reckc1b33d62015-04-22 09:04:45 -070072SkiaCanvas::SkiaCanvas(const SkBitmap& bitmap) {
Derek Sollenbergerd01b5912018-10-19 15:55:33 -040073 mCanvasOwned = std::unique_ptr<SkCanvas>(new SkCanvas(bitmap));
74 mCanvas = mCanvasOwned.get();
Derek Sollenberger8872b382014-06-23 14:13:53 -040075}
76
Stan Iliev021693b2016-10-17 16:26:15 -040077SkiaCanvas::~SkiaCanvas() {}
78
Derek Sollenbergerc1908132016-07-15 10:28:16 -040079void SkiaCanvas::reset(SkCanvas* skiaCanvas) {
Mike Reed6acfe162016-11-18 17:21:09 -050080 if (mCanvas != skiaCanvas) {
81 mCanvas = skiaCanvas;
82 mCanvasOwned.reset();
83 }
Derek Sollenbergerc1908132016-07-15 10:28:16 -040084 mSaveStack.reset(nullptr);
Derek Sollenbergerc1908132016-07-15 10:28:16 -040085}
86
Derek Sollenberger8872b382014-06-23 14:13:53 -040087// ----------------------------------------------------------------------------
88// Canvas state operations: Replace Bitmap
89// ----------------------------------------------------------------------------
90
John Reckc1b33d62015-04-22 09:04:45 -070091void SkiaCanvas::setBitmap(const SkBitmap& bitmap) {
Tony Mantler4f641d12017-03-14 22:36:14 +000092 // deletes the previously owned canvas (if any)
Derek Sollenbergerd01b5912018-10-19 15:55:33 -040093 mCanvasOwned.reset(new SkCanvas(bitmap));
94 mCanvas = mCanvasOwned.get();
Tony Mantler4f641d12017-03-14 22:36:14 +000095
Derek Sollenberger8872b382014-06-23 14:13:53 -040096 // clean up the old save stack
Stan Ilievf50806a2016-10-24 10:40:39 -040097 mSaveStack.reset(nullptr);
Derek Sollenberger8872b382014-06-23 14:13:53 -040098}
99
100// ----------------------------------------------------------------------------
101// Canvas state operations
102// ----------------------------------------------------------------------------
103
104bool SkiaCanvas::isOpaque() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400105 return mCanvas->imageInfo().isOpaque();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400106}
107
108int SkiaCanvas::width() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400109 return mCanvas->imageInfo().width();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400110}
111
112int SkiaCanvas::height() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400113 return mCanvas->imageInfo().height();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400114}
115
116// ----------------------------------------------------------------------------
117// Canvas state operations: Save (layer)
118// ----------------------------------------------------------------------------
119
120int SkiaCanvas::getSaveCount() const {
121 return mCanvas->getSaveCount();
122}
123
Florin Malitaeecff562015-12-21 10:43:01 -0500124int SkiaCanvas::save(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400125 int count = mCanvas->save();
126 recordPartialSave(flags);
127 return count;
128}
129
Florin Malita5e271402015-11-04 14:36:02 -0500130// The SkiaCanvas::restore operation layers on the capability to preserve
131// either (or both) the matrix and/or clip state after a SkCanvas::restore
132// operation. It does this by explicitly saving off the clip & matrix state
133// when requested and playing it back after the SkCanvas::restore.
Derek Sollenberger8872b382014-06-23 14:13:53 -0400134void SkiaCanvas::restore() {
Stan Ilievf50806a2016-10-24 10:40:39 -0400135 const auto* rec = this->currentSaveRec();
136 if (!rec) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400137 // Fast path - no record for this frame.
138 mCanvas->restore();
139 return;
140 }
141
Florin Malitaeecff562015-12-21 10:43:01 -0500142 bool preserveMatrix = !(rec->saveFlags & SaveFlags::Matrix);
John Reck1bcacfd2017-11-03 10:12:19 -0700143 bool preserveClip = !(rec->saveFlags & SaveFlags::Clip);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400144
145 SkMatrix savedMatrix;
146 if (preserveMatrix) {
147 savedMatrix = mCanvas->getTotalMatrix();
148 }
149
Stan Ilievf50806a2016-10-24 10:40:39 -0400150 const size_t clipIndex = rec->clipIndex;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400151
152 mCanvas->restore();
Stan Ilievf50806a2016-10-24 10:40:39 -0400153 mSaveStack->pop_back();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400154
155 if (preserveMatrix) {
156 mCanvas->setMatrix(savedMatrix);
157 }
158
Stan Ilievf50806a2016-10-24 10:40:39 -0400159 if (preserveClip) {
160 this->applyPersistentClips(clipIndex);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400161 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400162}
163
164void SkiaCanvas::restoreToCount(int restoreCount) {
165 while (mCanvas->getSaveCount() > restoreCount) {
166 this->restore();
167 }
168}
169
John Recka00eef212020-11-16 12:45:55 -0500170int SkiaCanvas::saveLayer(float left, float top, float right, float bottom, const SkPaint* paint) {
Florin Malitaeecff562015-12-21 10:43:01 -0500171 const SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
John Recka00eef212020-11-16 12:45:55 -0500172 const SkCanvas::SaveLayerRec rec(&bounds, paint);
Florin Malitaeecff562015-12-21 10:43:01 -0500173
Stan Iliev68885e32016-12-14 11:18:34 -0500174 return mCanvas->saveLayer(rec);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400175}
176
John Recka00eef212020-11-16 12:45:55 -0500177int SkiaCanvas::saveLayerAlpha(float left, float top, float right, float bottom, int alpha) {
Florin Malitaeecff562015-12-21 10:43:01 -0500178 if (static_cast<unsigned>(alpha) < 0xFF) {
Yuqian Lifd92ee42016-04-27 17:03:38 -0400179 SkPaint alphaPaint;
180 alphaPaint.setAlpha(alpha);
John Recka00eef212020-11-16 12:45:55 -0500181 return this->saveLayer(left, top, right, bottom, &alphaPaint);
Florin Malitaeecff562015-12-21 10:43:01 -0500182 }
John Recka00eef212020-11-16 12:45:55 -0500183 return this->saveLayer(left, top, right, bottom, nullptr);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400184}
185
Derek Sollenberger24fc9012018-12-07 14:12:12 -0500186int SkiaCanvas::saveUnclippedLayer(int left, int top, int right, int bottom) {
187 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
188 return SkAndroidFrameworkUtils::SaveBehind(mCanvas, &bounds);
189}
190
Mike Reeda6cb58a2021-07-10 13:31:34 -0400191void SkiaCanvas::restoreUnclippedLayer(int restoreCount, const Paint& paint) {
Derek Sollenbergerac33a482019-04-22 16:28:09 -0400192
193 while (mCanvas->getSaveCount() > restoreCount + 1) {
194 this->restore();
195 }
196
197 if (mCanvas->getSaveCount() == restoreCount + 1) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500198 SkCanvasPriv::DrawBehind(mCanvas, filterPaint(paint));
Derek Sollenbergerac33a482019-04-22 16:28:09 -0400199 this->restore();
200 }
201}
202
Stan Ilievf50806a2016-10-24 10:40:39 -0400203class SkiaCanvas::Clip {
204public:
Mike Reed6e49c9f2016-12-02 15:36:59 -0500205 Clip(const SkRect& rect, SkClipOp op, const SkMatrix& m)
John Reck1bcacfd2017-11-03 10:12:19 -0700206 : mType(Type::Rect), mOp(op), mMatrix(m), mRRect(SkRRect::MakeRect(rect)) {}
Mike Reed6e49c9f2016-12-02 15:36:59 -0500207 Clip(const SkRRect& rrect, SkClipOp op, const SkMatrix& m)
John Reck1bcacfd2017-11-03 10:12:19 -0700208 : mType(Type::RRect), mOp(op), mMatrix(m), mRRect(rrect) {}
Mike Reed6e49c9f2016-12-02 15:36:59 -0500209 Clip(const SkPath& path, SkClipOp op, const SkMatrix& m)
Ben Wagner0ed10be2018-06-28 17:08:16 -0400210 : mType(Type::Path), mOp(op), mMatrix(m), mPath(std::in_place, path) {}
Stan Ilievf50806a2016-10-24 10:40:39 -0400211
212 void apply(SkCanvas* canvas) const {
213 canvas->setMatrix(mMatrix);
214 switch (mType) {
John Reck1bcacfd2017-11-03 10:12:19 -0700215 case Type::Rect:
Nader Jawade431e312019-12-04 14:13:18 -0800216 // Don't anti-alias rectangular clips
217 canvas->clipRect(mRRect.rect(), mOp, false);
John Reck1bcacfd2017-11-03 10:12:19 -0700218 break;
219 case Type::RRect:
Nader Jawade431e312019-12-04 14:13:18 -0800220 // Ensure rounded rectangular clips are anti-aliased
221 canvas->clipRRect(mRRect, mOp, true);
John Reck1bcacfd2017-11-03 10:12:19 -0700222 break;
223 case Type::Path:
Nader Jawade431e312019-12-04 14:13:18 -0800224 // Ensure path clips are anti-aliased
225 canvas->clipPath(mPath.value(), mOp, true);
John Reck1bcacfd2017-11-03 10:12:19 -0700226 break;
Stan Ilievf50806a2016-10-24 10:40:39 -0400227 }
228 }
229
230private:
231 enum class Type {
232 Rect,
233 RRect,
234 Path,
235 };
236
John Reck1bcacfd2017-11-03 10:12:19 -0700237 Type mType;
238 SkClipOp mOp;
239 SkMatrix mMatrix;
Stan Ilievf50806a2016-10-24 10:40:39 -0400240
241 // These are logically a union (tracked separately due to non-POD path).
Ben Wagner0ed10be2018-06-28 17:08:16 -0400242 std::optional<SkPath> mPath;
John Reck1bcacfd2017-11-03 10:12:19 -0700243 SkRRect mRRect;
Stan Ilievf50806a2016-10-24 10:40:39 -0400244};
245
246const SkiaCanvas::SaveRec* SkiaCanvas::currentSaveRec() const {
John Reck1bcacfd2017-11-03 10:12:19 -0700247 const SaveRec* rec = mSaveStack ? static_cast<const SaveRec*>(mSaveStack->back()) : nullptr;
Stan Ilievf50806a2016-10-24 10:40:39 -0400248 int currentSaveCount = mCanvas->getSaveCount();
249 SkASSERT(!rec || currentSaveCount >= rec->saveCount);
250
251 return (rec && rec->saveCount == currentSaveCount) ? rec : nullptr;
252}
253
Nader Jawad2dc632a2021-03-29 18:51:29 -0700254void SkiaCanvas::punchHole(const SkRRect& rect) {
255 SkPaint paint = SkPaint();
256 paint.setColor(0);
257 paint.setBlendMode(SkBlendMode::kClear);
258 mCanvas->drawRRect(rect, paint);
259}
260
Derek Sollenberger8872b382014-06-23 14:13:53 -0400261// ----------------------------------------------------------------------------
262// functions to emulate legacy SaveFlags (i.e. independent matrix/clip flags)
263// ----------------------------------------------------------------------------
264
Florin Malitaeecff562015-12-21 10:43:01 -0500265void SkiaCanvas::recordPartialSave(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400266 // A partial save is a save operation which doesn't capture the full canvas state.
Florin Malitaeecff562015-12-21 10:43:01 -0500267 // (either SaveFlags::Matrix or SaveFlags::Clip is missing).
Derek Sollenberger8872b382014-06-23 14:13:53 -0400268
269 // Mask-out non canvas state bits.
Florin Malitaeecff562015-12-21 10:43:01 -0500270 flags &= SaveFlags::MatrixClip;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400271
Florin Malitaeecff562015-12-21 10:43:01 -0500272 if (flags == SaveFlags::MatrixClip) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400273 // not a partial save.
274 return;
275 }
276
Stan Ilievf50806a2016-10-24 10:40:39 -0400277 if (!mSaveStack) {
Ben Wagnerd1cbc162015-08-19 12:45:09 -0400278 mSaveStack.reset(new SkDeque(sizeof(struct SaveRec), 8));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400279 }
280
281 SaveRec* rec = static_cast<SaveRec*>(mSaveStack->push_back());
Florin Malita5e271402015-11-04 14:36:02 -0500282 rec->saveCount = mCanvas->getSaveCount();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400283 rec->saveFlags = flags;
Stan Ilievf50806a2016-10-24 10:40:39 -0400284 rec->clipIndex = mClipStack.size();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400285}
286
Stan Ilievf50806a2016-10-24 10:40:39 -0400287template <typename T>
Mike Reed6e49c9f2016-12-02 15:36:59 -0500288void SkiaCanvas::recordClip(const T& clip, SkClipOp op) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400289 // Only need tracking when in a partial save frame which
290 // doesn't restore the clip.
291 const SaveRec* rec = this->currentSaveRec();
292 if (rec && !(rec->saveFlags & SaveFlags::Clip)) {
293 mClipStack.emplace_back(clip, op, mCanvas->getTotalMatrix());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400294 }
295}
296
Stan Ilievf50806a2016-10-24 10:40:39 -0400297// Applies and optionally removes all clips >= index.
298void SkiaCanvas::applyPersistentClips(size_t clipStartIndex) {
299 SkASSERT(clipStartIndex <= mClipStack.size());
300 const auto begin = mClipStack.cbegin() + clipStartIndex;
301 const auto end = mClipStack.cend();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400302
Stan Ilievf50806a2016-10-24 10:40:39 -0400303 // Clip application mutates the CTM.
304 const SkMatrix saveMatrix = mCanvas->getTotalMatrix();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400305
Stan Ilievf50806a2016-10-24 10:40:39 -0400306 for (auto clip = begin; clip != end; ++clip) {
Mike Reed6acfe162016-11-18 17:21:09 -0500307 clip->apply(mCanvas);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400308 }
309
Stan Ilievf50806a2016-10-24 10:40:39 -0400310 mCanvas->setMatrix(saveMatrix);
311
312 // If the current/post-restore save rec is also persisting clips, we
313 // leave them on the stack to be reapplied part of the next restore().
314 // Otherwise we're done and just pop them.
315 const auto* rec = this->currentSaveRec();
316 if (!rec || (rec->saveFlags & SaveFlags::Clip)) {
317 mClipStack.erase(begin, end);
318 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400319}
320
321// ----------------------------------------------------------------------------
322// Canvas state operations: Matrix
323// ----------------------------------------------------------------------------
324
325void SkiaCanvas::getMatrix(SkMatrix* outMatrix) const {
326 *outMatrix = mCanvas->getTotalMatrix();
327}
328
329void SkiaCanvas::setMatrix(const SkMatrix& matrix) {
330 mCanvas->setMatrix(matrix);
331}
332
333void SkiaCanvas::concat(const SkMatrix& matrix) {
334 mCanvas->concat(matrix);
335}
336
337void SkiaCanvas::rotate(float degrees) {
338 mCanvas->rotate(degrees);
339}
340
341void SkiaCanvas::scale(float sx, float sy) {
342 mCanvas->scale(sx, sy);
343}
344
345void SkiaCanvas::skew(float sx, float sy) {
346 mCanvas->skew(sx, sy);
347}
348
349void SkiaCanvas::translate(float dx, float dy) {
350 mCanvas->translate(dx, dy);
351}
352
353// ----------------------------------------------------------------------------
354// Canvas state operations: Clips
355// ----------------------------------------------------------------------------
356
357// This function is a mirror of SkCanvas::getClipBounds except that it does
358// not outset the edge of the clip to account for anti-aliasing. There is
359// a skia bug to investigate pushing this logic into back into skia.
360// (see https://code.google.com/p/skia/issues/detail?id=1303)
361bool SkiaCanvas::getClipBounds(SkRect* outRect) const {
362 SkIRect ibounds;
Mike Reed5e438982017-01-25 08:23:25 -0500363 if (!mCanvas->getDeviceClipBounds(&ibounds)) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400364 return false;
365 }
366
367 SkMatrix inverse;
368 // if we can't invert the CTM, we can't return local clip bounds
369 if (!mCanvas->getTotalMatrix().invert(&inverse)) {
370 if (outRect) {
371 outRect->setEmpty();
372 }
373 return false;
374 }
375
376 if (NULL != outRect) {
377 SkRect r = SkRect::Make(ibounds);
378 inverse.mapRect(outRect, r);
379 }
380 return true;
381}
382
383bool SkiaCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
384 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
385 return mCanvas->quickReject(bounds);
386}
387
388bool SkiaCanvas::quickRejectPath(const SkPath& path) const {
389 return mCanvas->quickReject(path);
390}
391
Mike Reed6e49c9f2016-12-02 15:36:59 -0500392bool SkiaCanvas::clipRect(float left, float top, float right, float bottom, SkClipOp op) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400393 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Stan Ilievf50806a2016-10-24 10:40:39 -0400394 this->recordClip(rect, op);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400395 mCanvas->clipRect(rect, op);
Chris Craik5ec6a282015-06-23 15:42:12 -0700396 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400397}
398
Mike Reed6e49c9f2016-12-02 15:36:59 -0500399bool SkiaCanvas::clipPath(const SkPath* path, SkClipOp op) {
Derek Sollenbergerf7d98f42017-04-17 11:27:36 -0400400 this->recordClip(*path, op);
Nader Jawade431e312019-12-04 14:13:18 -0800401 mCanvas->clipPath(*path, op, true);
Chris Craik5ec6a282015-06-23 15:42:12 -0700402 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400403}
404
Michael Ludwig70cf50c22021-07-21 17:02:39 +0000405bool SkiaCanvas::replaceClipRect_deprecated(float left, float top, float right, float bottom) {
406 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
407
408 // Emulated clip rects are not recorded for partial saves, since
409 // partial saves have been removed from the public API.
410 SkAndroidFrameworkUtils::ResetClip(mCanvas);
411 mCanvas->clipRect(rect, SkClipOp::kIntersect);
412 return !mCanvas->isClipEmpty();
413}
414
415bool SkiaCanvas::replaceClipPath_deprecated(const SkPath* path) {
416 SkAndroidFrameworkUtils::ResetClip(mCanvas);
417 mCanvas->clipPath(*path, SkClipOp::kIntersect, true);
418 return !mCanvas->isClipEmpty();
419}
420
Derek Sollenberger8872b382014-06-23 14:13:53 -0400421// ----------------------------------------------------------------------------
422// Canvas state operations: Filters
423// ----------------------------------------------------------------------------
424
Ben Wagner0ed10be2018-06-28 17:08:16 -0400425PaintFilter* SkiaCanvas::getPaintFilter() {
426 return mPaintFilter.get();
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400427}
428
Ben Wagner0ed10be2018-06-28 17:08:16 -0400429void SkiaCanvas::setPaintFilter(sk_sp<PaintFilter> paintFilter) {
430 mPaintFilter = std::move(paintFilter);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400431}
432
433// ----------------------------------------------------------------------------
Matt Sarettd0814db2017-04-13 09:33:18 -0400434// Canvas state operations: Capture
435// ----------------------------------------------------------------------------
436
437SkCanvasState* SkiaCanvas::captureCanvasState() const {
438 SkCanvas* canvas = mCanvas;
439 if (mCanvasOwned) {
440 // Important to use the underlying SkCanvas, not the wrapper.
441 canvas = mCanvasOwned.get();
442 }
443
444 // Workarounds for http://crbug.com/271096: SW draw only supports
445 // translate & scale transforms, and a simple rectangular clip.
446 // (This also avoids significant wasted time in calling
447 // SkCanvasStateUtils::CaptureCanvasState when the clip is complex).
John Reck1bcacfd2017-11-03 10:12:19 -0700448 if (!canvas->isClipRect() || (canvas->getTotalMatrix().getType() &
449 ~(SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask))) {
450 return nullptr;
Matt Sarettd0814db2017-04-13 09:33:18 -0400451 }
452
453 return SkCanvasStateUtils::CaptureCanvasState(canvas);
454}
455
456// ----------------------------------------------------------------------------
Derek Sollenberger8872b382014-06-23 14:13:53 -0400457// Canvas draw operations
458// ----------------------------------------------------------------------------
459
Mike Reed260ab722016-10-07 15:59:20 -0400460void SkiaCanvas::drawColor(int color, SkBlendMode mode) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400461 mCanvas->drawColor(color, mode);
462}
463
Mike Reeda6cb58a2021-07-10 13:31:34 -0400464void SkiaCanvas::onFilterPaint(Paint& paint) {
Ben Wagner0ed10be2018-06-28 17:08:16 -0400465 if (mPaintFilter) {
Mike Reeda6cb58a2021-07-10 13:31:34 -0400466 mPaintFilter->filterFullPaint(&paint);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400467 }
Ben Wagner0ed10be2018-06-28 17:08:16 -0400468}
469
Mike Reeda6cb58a2021-07-10 13:31:34 -0400470void SkiaCanvas::drawPaint(const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500471 mCanvas->drawPaint(filterPaint(paint));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400472}
473
474// ----------------------------------------------------------------------------
475// Canvas draw operations: Geometry
476// ----------------------------------------------------------------------------
477
Mike Reedc2dbc032019-07-25 12:28:29 -0400478void SkiaCanvas::drawPoints(const float* points, int count, const Paint& paint,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400479 SkCanvas::PointMode mode) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500480 if (CC_UNLIKELY(count < 2 || paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400481 // convert the floats into SkPoints
John Reck1bcacfd2017-11-03 10:12:19 -0700482 count >>= 1; // now it is the number of points
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400483 std::unique_ptr<SkPoint[]> pts(new SkPoint[count]);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400484 for (int i = 0; i < count; i++) {
485 pts[i].set(points[0], points[1]);
486 points += 2;
487 }
Mike Reedc2dbc032019-07-25 12:28:29 -0400488
Mike Reedbb4cb482021-02-22 14:21:20 -0500489 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawPoints(mode, count, pts.get(), p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400490}
491
Mike Reedc2dbc032019-07-25 12:28:29 -0400492void SkiaCanvas::drawPoint(float x, float y, const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500493 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawPoint(x, y, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400494}
495
Mike Reedc2dbc032019-07-25 12:28:29 -0400496void SkiaCanvas::drawPoints(const float* points, int count, const Paint& paint) {
497 this->drawPoints(points, count, paint, SkCanvas::kPoints_PointMode);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400498}
499
500void SkiaCanvas::drawLine(float startX, float startY, float stopX, float stopY,
Mike Reedc2dbc032019-07-25 12:28:29 -0400501 const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500502 applyLooper(&paint,
503 [&](const SkPaint& p) { mCanvas->drawLine(startX, startY, stopX, stopY, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400504}
505
Mike Reedc2dbc032019-07-25 12:28:29 -0400506void SkiaCanvas::drawLines(const float* points, int count, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500507 if (CC_UNLIKELY(count < 4 || paint.nothingToDraw())) return;
Mike Reedc2dbc032019-07-25 12:28:29 -0400508 this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400509}
510
Mike Reedc2dbc032019-07-25 12:28:29 -0400511void SkiaCanvas::drawRect(float left, float top, float right, float bottom, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500512 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Mike Reedbb4cb482021-02-22 14:21:20 -0500513 applyLooper(&paint, [&](const SkPaint& p) {
Mike Reedc2dbc032019-07-25 12:28:29 -0400514 mCanvas->drawRect({left, top, right, bottom}, p);
515 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400516}
517
Mike Reedc2dbc032019-07-25 12:28:29 -0400518void SkiaCanvas::drawRegion(const SkRegion& region, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500519 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Mike Reedbb4cb482021-02-22 14:21:20 -0500520 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawRegion(region, p); });
Derek Sollenberger94394b32015-07-10 09:58:41 -0400521}
522
John Reck1bcacfd2017-11-03 10:12:19 -0700523void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom, float rx, float ry,
Mike Reedc2dbc032019-07-25 12:28:29 -0400524 const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500525 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400526 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Mike Reedbb4cb482021-02-22 14:21:20 -0500527 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawRoundRect(rect, rx, ry, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400528}
529
Nader Jawadadfe1d92018-09-27 12:27:36 -0700530void SkiaCanvas::drawDoubleRoundRect(const SkRRect& outer, const SkRRect& inner,
Mike Reedc2dbc032019-07-25 12:28:29 -0400531 const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500532 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawDRRect(outer, inner, p); });
Nader Jawadadfe1d92018-09-27 12:27:36 -0700533}
534
Mike Reedc2dbc032019-07-25 12:28:29 -0400535void SkiaCanvas::drawCircle(float x, float y, float radius, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500536 if (CC_UNLIKELY(radius <= 0 || paint.nothingToDraw())) return;
Mike Reedbb4cb482021-02-22 14:21:20 -0500537 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawCircle(x, y, radius, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400538}
539
Mike Reedc2dbc032019-07-25 12:28:29 -0400540void SkiaCanvas::drawOval(float left, float top, float right, float bottom, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500541 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400542 SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
Mike Reedbb4cb482021-02-22 14:21:20 -0500543 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawOval(oval, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400544}
545
John Reck1bcacfd2017-11-03 10:12:19 -0700546void SkiaCanvas::drawArc(float left, float top, float right, float bottom, float startAngle,
Mike Reedc2dbc032019-07-25 12:28:29 -0400547 float sweepAngle, bool useCenter, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500548 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400549 SkRect arc = SkRect::MakeLTRB(left, top, right, bottom);
Mike Reedbb4cb482021-02-22 14:21:20 -0500550 applyLooper(&paint, [&](const SkPaint& p) {
Mike Reedc2dbc032019-07-25 12:28:29 -0400551 if (fabs(sweepAngle) >= 360.0f) {
552 mCanvas->drawOval(arc, p);
553 } else {
554 mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, p);
555 }
556 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400557}
558
Mike Reedc2dbc032019-07-25 12:28:29 -0400559void SkiaCanvas::drawPath(const SkPath& path, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500560 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Stan Iliev6dcfdec2017-08-15 16:42:05 -0400561 if (CC_UNLIKELY(path.isEmpty() && (!path.isInverseFillType()))) {
562 return;
563 }
Mike Reedbb4cb482021-02-22 14:21:20 -0500564 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawPath(path, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400565}
566
Mike Reedc2dbc032019-07-25 12:28:29 -0400567void SkiaCanvas::drawVertices(const SkVertices* vertices, SkBlendMode mode, const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500568 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawVertices(vertices, mode, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400569}
570
571// ----------------------------------------------------------------------------
572// Canvas draw operations: Bitmaps
573// ----------------------------------------------------------------------------
574
Mike Reedc2dbc032019-07-25 12:28:29 -0400575void SkiaCanvas::drawBitmap(Bitmap& bitmap, float left, float top, const Paint* paint) {
576 auto image = bitmap.makeImage();
Mike Reeda6cb58a2021-07-10 13:31:34 -0400577 applyLooper(paint, [&](const Paint& p) {
578 mCanvas->drawImage(image, left, top, p.sampling(), &p);
Mike Reedc2dbc032019-07-25 12:28:29 -0400579 });
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400580}
581
Mike Reedc2dbc032019-07-25 12:28:29 -0400582void SkiaCanvas::drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const Paint* paint) {
583 auto image = bitmap.makeImage();
Mike Reed6acfe162016-11-18 17:21:09 -0500584 SkAutoCanvasRestore acr(mCanvas, true);
Mike Reed70ffbf92014-12-08 17:03:30 -0500585 mCanvas->concat(matrix);
Mike Reeda6cb58a2021-07-10 13:31:34 -0400586 applyLooper(paint, [&](const Paint& p) {
587 mCanvas->drawImage(image, 0, 0, p.sampling(), &p);
Mike Reedc2dbc032019-07-25 12:28:29 -0400588 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400589}
590
John Reck1bcacfd2017-11-03 10:12:19 -0700591void SkiaCanvas::drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight,
592 float srcBottom, float dstLeft, float dstTop, float dstRight,
Mike Reedc2dbc032019-07-25 12:28:29 -0400593 float dstBottom, const Paint* paint) {
594 auto image = bitmap.makeImage();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400595 SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
596 SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400597
Mike Reeda6cb58a2021-07-10 13:31:34 -0400598 applyLooper(paint, [&](const Paint& p) {
599 mCanvas->drawImageRect(image, srcRect, dstRect, p.sampling(), &p,
Mike Reed7994a312021-01-28 18:06:26 -0500600 SkCanvas::kFast_SrcRectConstraint);
Mike Reedc2dbc032019-07-25 12:28:29 -0400601 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400602}
603
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400604void SkiaCanvas::drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight,
Mike Reedc2dbc032019-07-25 12:28:29 -0400605 const float* vertices, const int* colors, const Paint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400606 const int ptCount = (meshWidth + 1) * (meshHeight + 1);
607 const int indexCount = meshWidth * meshHeight * 6;
Mike Reed871cd2d2017-03-17 10:15:52 -0400608 uint32_t flags = SkVertices::kHasTexCoords_BuilderFlag;
609 if (colors) {
610 flags |= SkVertices::kHasColors_BuilderFlag;
611 }
Mike Reed826deef2017-04-04 15:32:04 -0400612 SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, ptCount, indexCount, flags);
Mike Reed871cd2d2017-03-17 10:15:52 -0400613 memcpy(builder.positions(), vertices, ptCount * sizeof(SkPoint));
614 if (colors) {
615 memcpy(builder.colors(), colors, ptCount * sizeof(SkColor));
616 }
617 SkPoint* texs = builder.texCoords();
618 uint16_t* indices = builder.indices();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400619
620 // cons up texture coordinates and indices
621 {
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400622 const SkScalar w = SkIntToScalar(bitmap.width());
623 const SkScalar h = SkIntToScalar(bitmap.height());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400624 const SkScalar dx = w / meshWidth;
625 const SkScalar dy = h / meshHeight;
626
627 SkPoint* texsPtr = texs;
628 SkScalar y = 0;
629 for (int i = 0; i <= meshHeight; i++) {
630 if (i == meshHeight) {
631 y = h; // to ensure numerically we hit h exactly
632 }
633 SkScalar x = 0;
634 for (int j = 0; j < meshWidth; j++) {
635 texsPtr->set(x, y);
636 texsPtr += 1;
637 x += dx;
638 }
639 texsPtr->set(w, y);
640 texsPtr += 1;
641 y += dy;
642 }
643 SkASSERT(texsPtr - texs == ptCount);
644 }
645
646 // cons up indices
647 {
648 uint16_t* indexPtr = indices;
649 int index = 0;
650 for (int i = 0; i < meshHeight; i++) {
651 for (int j = 0; j < meshWidth; j++) {
652 // lower-left triangle
653 *indexPtr++ = index;
654 *indexPtr++ = index + meshWidth + 1;
655 *indexPtr++ = index + meshWidth + 2;
656 // upper-right triangle
657 *indexPtr++ = index;
658 *indexPtr++ = index + meshWidth + 2;
659 *indexPtr++ = index + 1;
660 // bump to the next cell
661 index += 1;
662 }
663 // bump to the next row
664 index += 1;
665 }
666 SkASSERT(indexPtr - indices == indexCount);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400667 }
668
John Reck1bcacfd2017-11-03 10:12:19 -0700669// double-check that we have legal indices
Derek Sollenberger8872b382014-06-23 14:13:53 -0400670#ifdef SK_DEBUG
671 {
672 for (int i = 0; i < indexCount; i++) {
673 SkASSERT((unsigned)indices[i] < (unsigned)ptCount);
674 }
675 }
676#endif
677
Mike Reed7994a312021-01-28 18:06:26 -0500678 auto image = bitmap.makeImage();
679
Derek Sollenberger8872b382014-06-23 14:13:53 -0400680 // cons-up a shader for the bitmap
Mike Reedc2dbc032019-07-25 12:28:29 -0400681 Paint pnt;
682 if (paint) {
683 pnt = *paint;
684 }
Mike Reeda6cb58a2021-07-10 13:31:34 -0400685 SkSamplingOptions sampling = pnt.sampling();
Mike Reed7994a312021-01-28 18:06:26 -0500686 pnt.setShader(image->makeShader(sampling));
687
Mike Reedc2dbc032019-07-25 12:28:29 -0400688 auto v = builder.detach();
Mike Reeda6cb58a2021-07-10 13:31:34 -0400689 applyLooper(&pnt, [&](const Paint& p) {
Mike Reed7994a312021-01-28 18:06:26 -0500690 SkPaint copy(p);
Mike Reeda6cb58a2021-07-10 13:31:34 -0400691 auto s = p.sampling();
Mike Reed7994a312021-01-28 18:06:26 -0500692 if (s != sampling) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500693 // applyLooper changed the quality?
Mike Reed7994a312021-01-28 18:06:26 -0500694 copy.setShader(image->makeShader(s));
695 }
696 mCanvas->drawVertices(v, SkBlendMode::kModulate, copy);
Mike Reedc2dbc032019-07-25 12:28:29 -0400697 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400698}
699
John Reck1bcacfd2017-11-03 10:12:19 -0700700void SkiaCanvas::drawNinePatch(Bitmap& bitmap, const Res_png_9patch& chunk, float dstLeft,
701 float dstTop, float dstRight, float dstBottom,
Mike Reedc2dbc032019-07-25 12:28:29 -0400702 const Paint* paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400703 SkCanvas::Lattice lattice;
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400704 NinePatchUtils::SetLatticeDivs(&lattice, chunk, bitmap.width(), bitmap.height());
Stan Ilievf50806a2016-10-24 10:40:39 -0400705
Stan Ilieve12d7312017-12-04 14:48:27 -0500706 lattice.fRectTypes = nullptr;
707 lattice.fColors = nullptr;
Stan Ilievf50806a2016-10-24 10:40:39 -0400708 int numFlags = 0;
Stan Iliev021693b2016-10-17 16:26:15 -0400709 if (chunk.numColors > 0 && chunk.numColors == NinePatchUtils::NumDistinctRects(lattice)) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400710 // We can expect the framework to give us a color for every distinct rect.
711 // Skia requires a flag for every rect.
712 numFlags = (lattice.fXCount + 1) * (lattice.fYCount + 1);
713 }
714
Stan Ilieve12d7312017-12-04 14:48:27 -0500715 SkAutoSTMalloc<25, SkCanvas::Lattice::RectType> flags(numFlags);
716 SkAutoSTMalloc<25, SkColor> colors(numFlags);
Stan Ilievf50806a2016-10-24 10:40:39 -0400717 if (numFlags > 0) {
Stan Ilieve12d7312017-12-04 14:48:27 -0500718 NinePatchUtils::SetLatticeFlags(&lattice, flags.get(), numFlags, chunk, colors.get());
Stan Ilievf50806a2016-10-24 10:40:39 -0400719 }
720
721 lattice.fBounds = nullptr;
722 SkRect dst = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Mike Reedc2dbc032019-07-25 12:28:29 -0400723 auto image = bitmap.makeImage();
Mike Reeda6cb58a2021-07-10 13:31:34 -0400724 applyLooper(paint, [&](const Paint& p) {
725 mCanvas->drawImageLattice(image.get(), lattice, dst, p.filterMode(), &p);
Mike Reedc2dbc032019-07-25 12:28:29 -0400726 });
Derek Sollenbergeredca3202015-07-10 13:56:39 -0400727}
728
Derek Sollenberger2d142132018-01-22 10:25:26 -0500729double SkiaCanvas::drawAnimatedImage(AnimatedImageDrawable* imgDrawable) {
730 return imgDrawable->drawStaging(mCanvas);
Leon Scroggins III671cce22018-01-14 16:52:17 -0500731}
732
Doris Liu766431a2016-02-04 22:17:11 +0000733void SkiaCanvas::drawVectorDrawable(VectorDrawableRoot* vectorDrawable) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800734 vectorDrawable->drawStaging(this);
Doris Liu766431a2016-02-04 22:17:11 +0000735}
736
Derek Sollenberger8872b382014-06-23 14:13:53 -0400737// ----------------------------------------------------------------------------
738// Canvas draw operations: Text
739// ----------------------------------------------------------------------------
740
Mike Reed2dfd55d2019-01-08 16:19:03 -0500741void SkiaCanvas::drawGlyphs(ReadGlyphFunc glyphFunc, int count, const Paint& paint, float x,
Seigo Nonaka63af7ba2020-09-21 23:07:43 -0700742 float y, float totalAdvance) {
Stan Iliev0b58d992017-03-30 18:22:27 -0400743 if (count <= 0 || paint.nothingToDraw()) return;
Mike Reedf6d86ac2019-01-18 14:13:23 -0500744 Paint paintCopy(paint);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400745 if (mPaintFilter) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500746 mPaintFilter->filterFullPaint(&paintCopy);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400747 }
Mike Reedf6d86ac2019-01-18 14:13:23 -0500748 const SkFont& font = paintCopy.getSkFont();
Stan Iliev7717e222018-02-05 18:04:11 -0500749 // Stroke with a hairline is drawn on HW with a fill style for compatibility with Android O and
750 // older.
John Recke170fb62018-05-07 08:12:07 -0700751 if (!mCanvasOwned && sApiLevel <= 27 && paintCopy.getStrokeWidth() <= 0 &&
752 paintCopy.getStyle() == SkPaint::kStroke_Style) {
Stan Iliev7717e222018-02-05 18:04:11 -0500753 paintCopy.setStyle(SkPaint::kFill_Style);
754 }
Stan Ilievf50806a2016-10-24 10:40:39 -0400755
Stan Ilievf50806a2016-10-24 10:40:39 -0400756 SkTextBlobBuilder builder;
Mike Reed2e204fc2019-01-28 13:31:36 -0500757 const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(font, count);
Stan Iliev0b58d992017-03-30 18:22:27 -0400758 glyphFunc(buffer.glyphs, buffer.pos);
Stan Ilievf50806a2016-10-24 10:40:39 -0400759
760 sk_sp<SkTextBlob> textBlob(builder.make());
Nathaniel Nifong52d37772020-01-10 16:12:41 -0500761
Mike Reedbb4cb482021-02-22 14:21:20 -0500762 applyLooper(&paintCopy, [&](const SkPaint& p) { mCanvas->drawTextBlob(textBlob, 0, 0, p); });
Stan Ilievf50806a2016-10-24 10:40:39 -0400763 drawTextDecorations(x, y, totalAdvance, paintCopy);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400764}
765
Yuqian Liafc221492016-07-18 13:07:42 -0400766void SkiaCanvas::drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
Mike Reed2dfd55d2019-01-08 16:19:03 -0500767 const Paint& paint, const SkPath& path, size_t start,
John Reck1bcacfd2017-11-03 10:12:19 -0700768 size_t end) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500769 Paint paintCopy(paint);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400770 if (mPaintFilter) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500771 mPaintFilter->filterFullPaint(&paintCopy);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400772 }
Mike Reedf6d86ac2019-01-18 14:13:23 -0500773 const SkFont& font = paintCopy.getSkFont();
Derek Sollenberger415a74b2018-03-14 15:03:13 -0400774
Yuqian Liafc221492016-07-18 13:07:42 -0400775 const int N = end - start;
Mike Reed4a4b1be2019-01-01 15:43:06 -0500776 SkTextBlobBuilder builder;
777 auto rec = builder.allocRunRSXform(font, N);
778 SkRSXform* xform = (SkRSXform*)rec.pos;
779 uint16_t* glyphs = rec.glyphs;
Yuqian Liafc221492016-07-18 13:07:42 -0400780 SkPathMeasure meas(path, false);
781
782 for (size_t i = start; i < end; i++) {
783 glyphs[i - start] = layout.getGlyphId(i);
Stan Ilievc6c96dd2018-01-10 16:06:04 -0500784 float halfWidth = layout.getCharAdvance(i) * 0.5f;
785 float x = hOffset + layout.getX(i) + halfWidth;
Yuqian Liafc221492016-07-18 13:07:42 -0400786 float y = vOffset + layout.getY(i);
787
788 SkPoint pos;
789 SkVector tan;
790 if (!meas.getPosTan(x, &pos, &tan)) {
791 pos.set(x, y);
792 tan.set(1, 0);
793 }
794 xform[i - start].fSCos = tan.x();
795 xform[i - start].fSSin = tan.y();
Stan Ilievc6c96dd2018-01-10 16:06:04 -0500796 xform[i - start].fTx = pos.x() - tan.y() * y - halfWidth * tan.x();
797 xform[i - start].fTy = pos.y() + tan.x() * y - halfWidth * tan.y();
Yuqian Liafc221492016-07-18 13:07:42 -0400798 }
Leon Scroggins III950f2aa2020-04-29 13:46:00 -0400799
800 sk_sp<SkTextBlob> textBlob(builder.make());
801
Mike Reedbb4cb482021-02-22 14:21:20 -0500802 applyLooper(&paintCopy, [&](const SkPaint& p) { mCanvas->drawTextBlob(textBlob, 0, 0, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400803}
804
Derek Sollenberger6f485562015-07-30 10:00:39 -0400805// ----------------------------------------------------------------------------
806// Canvas draw operations: Animations
807// ----------------------------------------------------------------------------
808
Derek Sollenberger6f485562015-07-30 10:00:39 -0400809void SkiaCanvas::drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
John Reck1bcacfd2017-11-03 10:12:19 -0700810 uirenderer::CanvasPropertyPrimitive* top,
811 uirenderer::CanvasPropertyPrimitive* right,
812 uirenderer::CanvasPropertyPrimitive* bottom,
813 uirenderer::CanvasPropertyPrimitive* rx,
814 uirenderer::CanvasPropertyPrimitive* ry,
815 uirenderer::CanvasPropertyPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -0400816 sk_sp<uirenderer::skiapipeline::AnimatedRoundRect> drawable(
John Reck1bcacfd2017-11-03 10:12:19 -0700817 new uirenderer::skiapipeline::AnimatedRoundRect(left, top, right, bottom, rx, ry,
818 paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400819 mCanvas->drawDrawable(drawable.get());
820}
821
John Reck1bcacfd2017-11-03 10:12:19 -0700822void SkiaCanvas::drawCircle(uirenderer::CanvasPropertyPrimitive* x,
823 uirenderer::CanvasPropertyPrimitive* y,
824 uirenderer::CanvasPropertyPrimitive* radius,
825 uirenderer::CanvasPropertyPaint* paint) {
826 sk_sp<uirenderer::skiapipeline::AnimatedCircle> drawable(
827 new uirenderer::skiapipeline::AnimatedCircle(x, y, radius, paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400828 mCanvas->drawDrawable(drawable.get());
829}
830
Lucas Dupin00af5272021-04-29 20:30:01 -0700831void SkiaCanvas::drawRipple(const uirenderer::skiapipeline::RippleDrawableParams& params) {
832 uirenderer::skiapipeline::AnimatedRippleDrawable::draw(mCanvas, params);
Derek Sollenbergerdf301aa2020-12-17 11:06:03 -0500833}
834
John Reck894e85a2019-07-15 16:16:44 -0700835void SkiaCanvas::drawPicture(const SkPicture& picture) {
836 // TODO: Change to mCanvas->drawPicture()? SkCanvas::drawPicture seems to be
837 // where the logic is for playback vs. ref picture. Using picture.playback here
838 // to stay behavior-identical for now, but should revisit this at some point.
839 picture.playback(mCanvas);
840}
841
Derek Sollenberger6f485562015-07-30 10:00:39 -0400842// ----------------------------------------------------------------------------
843// Canvas draw operations: View System
844// ----------------------------------------------------------------------------
845
Stan Ilievf50806a2016-10-24 10:40:39 -0400846void SkiaCanvas::drawLayer(uirenderer::DeferredLayerUpdater* layerUpdater) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400847 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw Layers");
848}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400849
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400850void SkiaCanvas::drawRenderNode(uirenderer::RenderNode* renderNode) {
851 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw RenderNodes");
852}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400853
John Reck1bcacfd2017-11-03 10:12:19 -0700854} // namespace android