blob: a690840e91a926a3c2ac615f326dcbc3790a219c [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"
Derek Sollenbergerc1908132016-07-15 10:28:16 -040026
Derek Sollenberger24fc9012018-12-07 14:12:12 -050027#include <SkAndroidFrameworkUtils.h>
Leon Scroggins III671cce22018-01-14 16:52:17 -050028#include <SkAnimatedImage.h>
Derek Sollenbergerac33a482019-04-22 16:28:09 -040029#include <SkCanvasPriv.h>
Matt Sarettd0814db2017-04-13 09:33:18 -040030#include <SkCanvasStateUtils.h>
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -040031#include <SkColorFilter.h>
John Reck849911a2015-01-20 07:51:14 -080032#include <SkDeque.h>
John Reck1bcacfd2017-11-03 10:12:19 -070033#include <SkDrawable.h>
Mike Reed1c79eab2018-11-21 11:01:57 -050034#include <SkFont.h>
John Reck849911a2015-01-20 07:51:14 -080035#include <SkGraphics.h>
Derek Sollenberger6f485562015-07-30 10:00:39 -040036#include <SkImage.h>
Matt Sarett62feb3a2016-09-20 10:34:11 -040037#include <SkImagePriv.h>
Leon Scroggins III671cce22018-01-14 16:52:17 -050038#include <SkPicture.h>
Yuqian Liafc221492016-07-18 13:07:42 -040039#include <SkRSXform.h>
John Reck849911a2015-01-20 07:51:14 -080040#include <SkShader.h>
John Reck849911a2015-01-20 07:51:14 -080041#include <SkTemplates.h>
Stan Ilievf50806a2016-10-24 10:40:39 -040042#include <SkTextBlob.h>
Brian Osmane3b9a122020-04-01 12:24:19 -040043#include <SkVertices.h>
Derek Sollenberger8872b382014-06-23 14:13:53 -040044
Nader Jawadfc42a992020-07-29 22:48:59 -070045#include <shader/BitmapShader.h>
46
Ben Wagner60126ef2015-08-07 12:13:48 -040047#include <memory>
Ben Wagner0ed10be2018-06-28 17:08:16 -040048#include <optional>
49#include <utility>
Ben Wagner60126ef2015-08-07 12:13:48 -040050
Derek Sollenberger8872b382014-06-23 14:13:53 -040051namespace android {
52
Stan Ilievf50806a2016-10-24 10:40:39 -040053using uirenderer::PaintUtils;
Nader Jawadfc42a992020-07-29 22:48:59 -070054using uirenderer::BitmapShader;
Stan Ilievf50806a2016-10-24 10:40:39 -040055
John Reckc1b33d62015-04-22 09:04:45 -070056Canvas* Canvas::create_canvas(const SkBitmap& bitmap) {
Derek Sollenberger8872b382014-06-23 14:13:53 -040057 return new SkiaCanvas(bitmap);
58}
59
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040060Canvas* Canvas::create_canvas(SkCanvas* skiaCanvas) {
61 return new SkiaCanvas(skiaCanvas);
Derek Sollenberger8872b382014-06-23 14:13:53 -040062}
63
Stan Ilievf50806a2016-10-24 10:40:39 -040064SkiaCanvas::SkiaCanvas() {}
65
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040066SkiaCanvas::SkiaCanvas(SkCanvas* canvas) : mCanvas(canvas) {}
Stan Ilievf50806a2016-10-24 10:40:39 -040067
John Reckc1b33d62015-04-22 09:04:45 -070068SkiaCanvas::SkiaCanvas(const SkBitmap& bitmap) {
Derek Sollenbergerd01b5912018-10-19 15:55:33 -040069 mCanvasOwned = std::unique_ptr<SkCanvas>(new SkCanvas(bitmap));
70 mCanvas = mCanvasOwned.get();
Derek Sollenberger8872b382014-06-23 14:13:53 -040071}
72
Stan Iliev021693b2016-10-17 16:26:15 -040073SkiaCanvas::~SkiaCanvas() {}
74
Derek Sollenbergerc1908132016-07-15 10:28:16 -040075void SkiaCanvas::reset(SkCanvas* skiaCanvas) {
Mike Reed6acfe162016-11-18 17:21:09 -050076 if (mCanvas != skiaCanvas) {
77 mCanvas = skiaCanvas;
78 mCanvasOwned.reset();
79 }
Derek Sollenbergerc1908132016-07-15 10:28:16 -040080 mSaveStack.reset(nullptr);
Derek Sollenbergerc1908132016-07-15 10:28:16 -040081}
82
Derek Sollenberger8872b382014-06-23 14:13:53 -040083// ----------------------------------------------------------------------------
84// Canvas state operations: Replace Bitmap
85// ----------------------------------------------------------------------------
86
John Reckc1b33d62015-04-22 09:04:45 -070087void SkiaCanvas::setBitmap(const SkBitmap& bitmap) {
Tony Mantler4f641d12017-03-14 22:36:14 +000088 // deletes the previously owned canvas (if any)
Derek Sollenbergerd01b5912018-10-19 15:55:33 -040089 mCanvasOwned.reset(new SkCanvas(bitmap));
90 mCanvas = mCanvasOwned.get();
Tony Mantler4f641d12017-03-14 22:36:14 +000091
Derek Sollenberger8872b382014-06-23 14:13:53 -040092 // clean up the old save stack
Stan Ilievf50806a2016-10-24 10:40:39 -040093 mSaveStack.reset(nullptr);
Derek Sollenberger8872b382014-06-23 14:13:53 -040094}
95
96// ----------------------------------------------------------------------------
97// Canvas state operations
98// ----------------------------------------------------------------------------
99
100bool SkiaCanvas::isOpaque() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400101 return mCanvas->imageInfo().isOpaque();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400102}
103
104int SkiaCanvas::width() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400105 return mCanvas->imageInfo().width();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400106}
107
108int SkiaCanvas::height() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400109 return mCanvas->imageInfo().height();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400110}
111
112// ----------------------------------------------------------------------------
113// Canvas state operations: Save (layer)
114// ----------------------------------------------------------------------------
115
116int SkiaCanvas::getSaveCount() const {
117 return mCanvas->getSaveCount();
118}
119
Florin Malitaeecff562015-12-21 10:43:01 -0500120int SkiaCanvas::save(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400121 int count = mCanvas->save();
122 recordPartialSave(flags);
123 return count;
124}
125
Florin Malita5e271402015-11-04 14:36:02 -0500126// The SkiaCanvas::restore operation layers on the capability to preserve
127// either (or both) the matrix and/or clip state after a SkCanvas::restore
128// operation. It does this by explicitly saving off the clip & matrix state
129// when requested and playing it back after the SkCanvas::restore.
Derek Sollenberger8872b382014-06-23 14:13:53 -0400130void SkiaCanvas::restore() {
Stan Ilievf50806a2016-10-24 10:40:39 -0400131 const auto* rec = this->currentSaveRec();
132 if (!rec) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400133 // Fast path - no record for this frame.
134 mCanvas->restore();
135 return;
136 }
137
Florin Malitaeecff562015-12-21 10:43:01 -0500138 bool preserveMatrix = !(rec->saveFlags & SaveFlags::Matrix);
John Reck1bcacfd2017-11-03 10:12:19 -0700139 bool preserveClip = !(rec->saveFlags & SaveFlags::Clip);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400140
141 SkMatrix savedMatrix;
142 if (preserveMatrix) {
143 savedMatrix = mCanvas->getTotalMatrix();
144 }
145
Stan Ilievf50806a2016-10-24 10:40:39 -0400146 const size_t clipIndex = rec->clipIndex;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400147
148 mCanvas->restore();
Stan Ilievf50806a2016-10-24 10:40:39 -0400149 mSaveStack->pop_back();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400150
151 if (preserveMatrix) {
152 mCanvas->setMatrix(savedMatrix);
153 }
154
Stan Ilievf50806a2016-10-24 10:40:39 -0400155 if (preserveClip) {
156 this->applyPersistentClips(clipIndex);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400157 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400158}
159
160void SkiaCanvas::restoreToCount(int restoreCount) {
161 while (mCanvas->getSaveCount() > restoreCount) {
162 this->restore();
163 }
164}
165
Florin Malitaeecff562015-12-21 10:43:01 -0500166static inline SkCanvas::SaveLayerFlags layerFlags(SaveFlags::Flags flags) {
167 SkCanvas::SaveLayerFlags layerFlags = 0;
168
Florin Malitaeecff562015-12-21 10:43:01 -0500169 if (!(flags & SaveFlags::ClipToLayer)) {
Mike Reed93b71202019-09-03 16:45:12 -0400170 layerFlags |= SkCanvasPriv::kDontClipToLayer_SaveLayerFlag;
Florin Malitaeecff562015-12-21 10:43:01 -0500171 }
172
173 return layerFlags;
174}
175
John Reck1bcacfd2017-11-03 10:12:19 -0700176int SkiaCanvas::saveLayer(float left, float top, float right, float bottom, const SkPaint* paint,
177 SaveFlags::Flags flags) {
Florin Malitaeecff562015-12-21 10:43:01 -0500178 const SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
Derek Sollenbergerb8201192017-01-09 16:11:59 -0500179 const SkCanvas::SaveLayerRec rec(&bounds, paint, layerFlags(flags));
Florin Malitaeecff562015-12-21 10:43:01 -0500180
Stan Iliev68885e32016-12-14 11:18:34 -0500181 return mCanvas->saveLayer(rec);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400182}
183
John Reck1bcacfd2017-11-03 10:12:19 -0700184int SkiaCanvas::saveLayerAlpha(float left, float top, float right, float bottom, int alpha,
185 SaveFlags::Flags flags) {
Florin Malitaeecff562015-12-21 10:43:01 -0500186 if (static_cast<unsigned>(alpha) < 0xFF) {
Yuqian Lifd92ee42016-04-27 17:03:38 -0400187 SkPaint alphaPaint;
188 alphaPaint.setAlpha(alpha);
189 return this->saveLayer(left, top, right, bottom, &alphaPaint, flags);
Florin Malitaeecff562015-12-21 10:43:01 -0500190 }
Yuqian Lifd92ee42016-04-27 17:03:38 -0400191 return this->saveLayer(left, top, right, bottom, nullptr, flags);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400192}
193
Derek Sollenberger24fc9012018-12-07 14:12:12 -0500194int SkiaCanvas::saveUnclippedLayer(int left, int top, int right, int bottom) {
195 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
196 return SkAndroidFrameworkUtils::SaveBehind(mCanvas, &bounds);
197}
198
Derek Sollenbergerac33a482019-04-22 16:28:09 -0400199void SkiaCanvas::restoreUnclippedLayer(int restoreCount, const SkPaint& paint) {
200
201 while (mCanvas->getSaveCount() > restoreCount + 1) {
202 this->restore();
203 }
204
205 if (mCanvas->getSaveCount() == restoreCount + 1) {
206 SkCanvasPriv::DrawBehind(mCanvas, *filterPaint(paint));
207 this->restore();
208 }
209}
210
Stan Ilievf50806a2016-10-24 10:40:39 -0400211class SkiaCanvas::Clip {
212public:
Mike Reed6e49c9f2016-12-02 15:36:59 -0500213 Clip(const SkRect& rect, SkClipOp op, const SkMatrix& m)
John Reck1bcacfd2017-11-03 10:12:19 -0700214 : mType(Type::Rect), mOp(op), mMatrix(m), mRRect(SkRRect::MakeRect(rect)) {}
Mike Reed6e49c9f2016-12-02 15:36:59 -0500215 Clip(const SkRRect& rrect, SkClipOp op, const SkMatrix& m)
John Reck1bcacfd2017-11-03 10:12:19 -0700216 : mType(Type::RRect), mOp(op), mMatrix(m), mRRect(rrect) {}
Mike Reed6e49c9f2016-12-02 15:36:59 -0500217 Clip(const SkPath& path, SkClipOp op, const SkMatrix& m)
Ben Wagner0ed10be2018-06-28 17:08:16 -0400218 : mType(Type::Path), mOp(op), mMatrix(m), mPath(std::in_place, path) {}
Stan Ilievf50806a2016-10-24 10:40:39 -0400219
220 void apply(SkCanvas* canvas) const {
221 canvas->setMatrix(mMatrix);
222 switch (mType) {
John Reck1bcacfd2017-11-03 10:12:19 -0700223 case Type::Rect:
Nader Jawade431e312019-12-04 14:13:18 -0800224 // Don't anti-alias rectangular clips
225 canvas->clipRect(mRRect.rect(), mOp, false);
John Reck1bcacfd2017-11-03 10:12:19 -0700226 break;
227 case Type::RRect:
Nader Jawade431e312019-12-04 14:13:18 -0800228 // Ensure rounded rectangular clips are anti-aliased
229 canvas->clipRRect(mRRect, mOp, true);
John Reck1bcacfd2017-11-03 10:12:19 -0700230 break;
231 case Type::Path:
Nader Jawade431e312019-12-04 14:13:18 -0800232 // Ensure path clips are anti-aliased
233 canvas->clipPath(mPath.value(), mOp, true);
John Reck1bcacfd2017-11-03 10:12:19 -0700234 break;
Stan Ilievf50806a2016-10-24 10:40:39 -0400235 }
236 }
237
238private:
239 enum class Type {
240 Rect,
241 RRect,
242 Path,
243 };
244
John Reck1bcacfd2017-11-03 10:12:19 -0700245 Type mType;
246 SkClipOp mOp;
247 SkMatrix mMatrix;
Stan Ilievf50806a2016-10-24 10:40:39 -0400248
249 // These are logically a union (tracked separately due to non-POD path).
Ben Wagner0ed10be2018-06-28 17:08:16 -0400250 std::optional<SkPath> mPath;
John Reck1bcacfd2017-11-03 10:12:19 -0700251 SkRRect mRRect;
Stan Ilievf50806a2016-10-24 10:40:39 -0400252};
253
254const SkiaCanvas::SaveRec* SkiaCanvas::currentSaveRec() const {
John Reck1bcacfd2017-11-03 10:12:19 -0700255 const SaveRec* rec = mSaveStack ? static_cast<const SaveRec*>(mSaveStack->back()) : nullptr;
Stan Ilievf50806a2016-10-24 10:40:39 -0400256 int currentSaveCount = mCanvas->getSaveCount();
257 SkASSERT(!rec || currentSaveCount >= rec->saveCount);
258
259 return (rec && rec->saveCount == currentSaveCount) ? rec : nullptr;
260}
261
Derek Sollenberger8872b382014-06-23 14:13:53 -0400262// ----------------------------------------------------------------------------
263// functions to emulate legacy SaveFlags (i.e. independent matrix/clip flags)
264// ----------------------------------------------------------------------------
265
Florin Malitaeecff562015-12-21 10:43:01 -0500266void SkiaCanvas::recordPartialSave(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400267 // A partial save is a save operation which doesn't capture the full canvas state.
Florin Malitaeecff562015-12-21 10:43:01 -0500268 // (either SaveFlags::Matrix or SaveFlags::Clip is missing).
Derek Sollenberger8872b382014-06-23 14:13:53 -0400269
270 // Mask-out non canvas state bits.
Florin Malitaeecff562015-12-21 10:43:01 -0500271 flags &= SaveFlags::MatrixClip;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400272
Florin Malitaeecff562015-12-21 10:43:01 -0500273 if (flags == SaveFlags::MatrixClip) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400274 // not a partial save.
275 return;
276 }
277
Stan Ilievf50806a2016-10-24 10:40:39 -0400278 if (!mSaveStack) {
Ben Wagnerd1cbc162015-08-19 12:45:09 -0400279 mSaveStack.reset(new SkDeque(sizeof(struct SaveRec), 8));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400280 }
281
282 SaveRec* rec = static_cast<SaveRec*>(mSaveStack->push_back());
Florin Malita5e271402015-11-04 14:36:02 -0500283 rec->saveCount = mCanvas->getSaveCount();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400284 rec->saveFlags = flags;
Stan Ilievf50806a2016-10-24 10:40:39 -0400285 rec->clipIndex = mClipStack.size();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400286}
287
Stan Ilievf50806a2016-10-24 10:40:39 -0400288template <typename T>
Mike Reed6e49c9f2016-12-02 15:36:59 -0500289void SkiaCanvas::recordClip(const T& clip, SkClipOp op) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400290 // Only need tracking when in a partial save frame which
291 // doesn't restore the clip.
292 const SaveRec* rec = this->currentSaveRec();
293 if (rec && !(rec->saveFlags & SaveFlags::Clip)) {
294 mClipStack.emplace_back(clip, op, mCanvas->getTotalMatrix());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400295 }
296}
297
Stan Ilievf50806a2016-10-24 10:40:39 -0400298// Applies and optionally removes all clips >= index.
299void SkiaCanvas::applyPersistentClips(size_t clipStartIndex) {
300 SkASSERT(clipStartIndex <= mClipStack.size());
301 const auto begin = mClipStack.cbegin() + clipStartIndex;
302 const auto end = mClipStack.cend();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400303
Stan Ilievf50806a2016-10-24 10:40:39 -0400304 // Clip application mutates the CTM.
305 const SkMatrix saveMatrix = mCanvas->getTotalMatrix();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400306
Stan Ilievf50806a2016-10-24 10:40:39 -0400307 for (auto clip = begin; clip != end; ++clip) {
Mike Reed6acfe162016-11-18 17:21:09 -0500308 clip->apply(mCanvas);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400309 }
310
Stan Ilievf50806a2016-10-24 10:40:39 -0400311 mCanvas->setMatrix(saveMatrix);
312
313 // If the current/post-restore save rec is also persisting clips, we
314 // leave them on the stack to be reapplied part of the next restore().
315 // Otherwise we're done and just pop them.
316 const auto* rec = this->currentSaveRec();
317 if (!rec || (rec->saveFlags & SaveFlags::Clip)) {
318 mClipStack.erase(begin, end);
319 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400320}
321
322// ----------------------------------------------------------------------------
323// Canvas state operations: Matrix
324// ----------------------------------------------------------------------------
325
326void SkiaCanvas::getMatrix(SkMatrix* outMatrix) const {
327 *outMatrix = mCanvas->getTotalMatrix();
328}
329
330void SkiaCanvas::setMatrix(const SkMatrix& matrix) {
331 mCanvas->setMatrix(matrix);
332}
333
334void SkiaCanvas::concat(const SkMatrix& matrix) {
335 mCanvas->concat(matrix);
336}
337
338void SkiaCanvas::rotate(float degrees) {
339 mCanvas->rotate(degrees);
340}
341
342void SkiaCanvas::scale(float sx, float sy) {
343 mCanvas->scale(sx, sy);
344}
345
346void SkiaCanvas::skew(float sx, float sy) {
347 mCanvas->skew(sx, sy);
348}
349
350void SkiaCanvas::translate(float dx, float dy) {
351 mCanvas->translate(dx, dy);
352}
353
354// ----------------------------------------------------------------------------
355// Canvas state operations: Clips
356// ----------------------------------------------------------------------------
357
358// This function is a mirror of SkCanvas::getClipBounds except that it does
359// not outset the edge of the clip to account for anti-aliasing. There is
360// a skia bug to investigate pushing this logic into back into skia.
361// (see https://code.google.com/p/skia/issues/detail?id=1303)
362bool SkiaCanvas::getClipBounds(SkRect* outRect) const {
363 SkIRect ibounds;
Mike Reed5e438982017-01-25 08:23:25 -0500364 if (!mCanvas->getDeviceClipBounds(&ibounds)) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400365 return false;
366 }
367
368 SkMatrix inverse;
369 // if we can't invert the CTM, we can't return local clip bounds
370 if (!mCanvas->getTotalMatrix().invert(&inverse)) {
371 if (outRect) {
372 outRect->setEmpty();
373 }
374 return false;
375 }
376
377 if (NULL != outRect) {
378 SkRect r = SkRect::Make(ibounds);
379 inverse.mapRect(outRect, r);
380 }
381 return true;
382}
383
384bool SkiaCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
385 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
386 return mCanvas->quickReject(bounds);
387}
388
389bool SkiaCanvas::quickRejectPath(const SkPath& path) const {
390 return mCanvas->quickReject(path);
391}
392
Mike Reed6e49c9f2016-12-02 15:36:59 -0500393bool SkiaCanvas::clipRect(float left, float top, float right, float bottom, SkClipOp op) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400394 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Stan Ilievf50806a2016-10-24 10:40:39 -0400395 this->recordClip(rect, op);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400396 mCanvas->clipRect(rect, op);
Chris Craik5ec6a282015-06-23 15:42:12 -0700397 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400398}
399
Mike Reed6e49c9f2016-12-02 15:36:59 -0500400bool SkiaCanvas::clipPath(const SkPath* path, SkClipOp op) {
Derek Sollenbergerf7d98f42017-04-17 11:27:36 -0400401 this->recordClip(*path, op);
Nader Jawade431e312019-12-04 14:13:18 -0800402 mCanvas->clipPath(*path, op, true);
Chris Craik5ec6a282015-06-23 15:42:12 -0700403 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400404}
405
Derek Sollenberger8872b382014-06-23 14:13:53 -0400406// ----------------------------------------------------------------------------
407// Canvas state operations: Filters
408// ----------------------------------------------------------------------------
409
Ben Wagner0ed10be2018-06-28 17:08:16 -0400410PaintFilter* SkiaCanvas::getPaintFilter() {
411 return mPaintFilter.get();
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400412}
413
Ben Wagner0ed10be2018-06-28 17:08:16 -0400414void SkiaCanvas::setPaintFilter(sk_sp<PaintFilter> paintFilter) {
415 mPaintFilter = std::move(paintFilter);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400416}
417
418// ----------------------------------------------------------------------------
Matt Sarettd0814db2017-04-13 09:33:18 -0400419// Canvas state operations: Capture
420// ----------------------------------------------------------------------------
421
422SkCanvasState* SkiaCanvas::captureCanvasState() const {
423 SkCanvas* canvas = mCanvas;
424 if (mCanvasOwned) {
425 // Important to use the underlying SkCanvas, not the wrapper.
426 canvas = mCanvasOwned.get();
427 }
428
429 // Workarounds for http://crbug.com/271096: SW draw only supports
430 // translate & scale transforms, and a simple rectangular clip.
431 // (This also avoids significant wasted time in calling
432 // SkCanvasStateUtils::CaptureCanvasState when the clip is complex).
John Reck1bcacfd2017-11-03 10:12:19 -0700433 if (!canvas->isClipRect() || (canvas->getTotalMatrix().getType() &
434 ~(SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask))) {
435 return nullptr;
Matt Sarettd0814db2017-04-13 09:33:18 -0400436 }
437
438 return SkCanvasStateUtils::CaptureCanvasState(canvas);
439}
440
441// ----------------------------------------------------------------------------
Derek Sollenberger8872b382014-06-23 14:13:53 -0400442// Canvas draw operations
443// ----------------------------------------------------------------------------
444
Mike Reed260ab722016-10-07 15:59:20 -0400445void SkiaCanvas::drawColor(int color, SkBlendMode mode) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400446 mCanvas->drawColor(color, mode);
447}
448
Ben Wagner0ed10be2018-06-28 17:08:16 -0400449SkiaCanvas::PaintCoW&& SkiaCanvas::filterPaint(PaintCoW&& paint) const {
450 if (mPaintFilter) {
451 mPaintFilter->filter(&paint.writeable());
452 }
453 return std::move(paint);
454}
455
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400456void SkiaCanvas::drawPaint(const SkPaint& paint) {
Ben Wagner0ed10be2018-06-28 17:08:16 -0400457 mCanvas->drawPaint(*filterPaint(paint));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400458}
459
460// ----------------------------------------------------------------------------
461// Canvas draw operations: Geometry
462// ----------------------------------------------------------------------------
463
Mike Reedc2dbc032019-07-25 12:28:29 -0400464void SkiaCanvas::drawPoints(const float* points, int count, const Paint& paint,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400465 SkCanvas::PointMode mode) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500466 if (CC_UNLIKELY(count < 2 || paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400467 // convert the floats into SkPoints
John Reck1bcacfd2017-11-03 10:12:19 -0700468 count >>= 1; // now it is the number of points
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400469 std::unique_ptr<SkPoint[]> pts(new SkPoint[count]);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400470 for (int i = 0; i < count; i++) {
471 pts[i].set(points[0], points[1]);
472 points += 2;
473 }
Mike Reedc2dbc032019-07-25 12:28:29 -0400474
475 apply_looper(&paint, [&](const SkPaint& p) {
476 mCanvas->drawPoints(mode, count, pts.get(), p);
477 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400478}
479
Mike Reedc2dbc032019-07-25 12:28:29 -0400480void SkiaCanvas::drawPoint(float x, float y, const Paint& paint) {
481 apply_looper(&paint, [&](const SkPaint& p) {
482 mCanvas->drawPoint(x, y, p);
483 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400484}
485
Mike Reedc2dbc032019-07-25 12:28:29 -0400486void SkiaCanvas::drawPoints(const float* points, int count, const Paint& paint) {
487 this->drawPoints(points, count, paint, SkCanvas::kPoints_PointMode);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400488}
489
490void SkiaCanvas::drawLine(float startX, float startY, float stopX, float stopY,
Mike Reedc2dbc032019-07-25 12:28:29 -0400491 const Paint& paint) {
492 apply_looper(&paint, [&](const SkPaint& p) {
493 mCanvas->drawLine(startX, startY, stopX, stopY, p);
494 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400495}
496
Mike Reedc2dbc032019-07-25 12:28:29 -0400497void SkiaCanvas::drawLines(const float* points, int count, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500498 if (CC_UNLIKELY(count < 4 || paint.nothingToDraw())) return;
Mike Reedc2dbc032019-07-25 12:28:29 -0400499 this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400500}
501
Mike Reedc2dbc032019-07-25 12:28:29 -0400502void SkiaCanvas::drawRect(float left, float top, float right, float bottom, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500503 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Mike Reedc2dbc032019-07-25 12:28:29 -0400504 apply_looper(&paint, [&](const SkPaint& p) {
505 mCanvas->drawRect({left, top, right, bottom}, p);
506 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400507}
508
Mike Reedc2dbc032019-07-25 12:28:29 -0400509void SkiaCanvas::drawRegion(const SkRegion& region, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500510 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Mike Reedc2dbc032019-07-25 12:28:29 -0400511 apply_looper(&paint, [&](const SkPaint& p) {
512 mCanvas->drawRegion(region, p);
513 });
Derek Sollenberger94394b32015-07-10 09:58:41 -0400514}
515
John Reck1bcacfd2017-11-03 10:12:19 -0700516void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom, float rx, float ry,
Mike Reedc2dbc032019-07-25 12:28:29 -0400517 const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500518 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400519 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Mike Reedc2dbc032019-07-25 12:28:29 -0400520 apply_looper(&paint, [&](const SkPaint& p) {
521 mCanvas->drawRoundRect(rect, rx, ry, p);
522 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400523}
524
Nader Jawadadfe1d92018-09-27 12:27:36 -0700525void SkiaCanvas::drawDoubleRoundRect(const SkRRect& outer, const SkRRect& inner,
Mike Reedc2dbc032019-07-25 12:28:29 -0400526 const Paint& paint) {
527 apply_looper(&paint, [&](const SkPaint& p) {
528 mCanvas->drawDRRect(outer, inner, p);
529 });
Nader Jawadadfe1d92018-09-27 12:27:36 -0700530}
531
Mike Reedc2dbc032019-07-25 12:28:29 -0400532void SkiaCanvas::drawCircle(float x, float y, float radius, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500533 if (CC_UNLIKELY(radius <= 0 || paint.nothingToDraw())) return;
Mike Reedc2dbc032019-07-25 12:28:29 -0400534 apply_looper(&paint, [&](const SkPaint& p) {
535 mCanvas->drawCircle(x, y, radius, p);
536 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400537}
538
Mike Reedc2dbc032019-07-25 12:28:29 -0400539void SkiaCanvas::drawOval(float left, float top, float right, float bottom, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500540 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400541 SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
Mike Reedc2dbc032019-07-25 12:28:29 -0400542 apply_looper(&paint, [&](const SkPaint& p) {
543 mCanvas->drawOval(oval, p);
544 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400545}
546
John Reck1bcacfd2017-11-03 10:12:19 -0700547void SkiaCanvas::drawArc(float left, float top, float right, float bottom, float startAngle,
Mike Reedc2dbc032019-07-25 12:28:29 -0400548 float sweepAngle, bool useCenter, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500549 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400550 SkRect arc = SkRect::MakeLTRB(left, top, right, bottom);
Mike Reedc2dbc032019-07-25 12:28:29 -0400551 apply_looper(&paint, [&](const SkPaint& p) {
552 if (fabs(sweepAngle) >= 360.0f) {
553 mCanvas->drawOval(arc, p);
554 } else {
555 mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, p);
556 }
557 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400558}
559
Mike Reedc2dbc032019-07-25 12:28:29 -0400560void SkiaCanvas::drawPath(const SkPath& path, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500561 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Stan Iliev6dcfdec2017-08-15 16:42:05 -0400562 if (CC_UNLIKELY(path.isEmpty() && (!path.isInverseFillType()))) {
563 return;
564 }
Mike Reedc2dbc032019-07-25 12:28:29 -0400565 apply_looper(&paint, [&](const SkPaint& p) {
566 mCanvas->drawPath(path, p);
567 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400568}
569
Mike Reedc2dbc032019-07-25 12:28:29 -0400570void SkiaCanvas::drawVertices(const SkVertices* vertices, SkBlendMode mode, const Paint& paint) {
571 apply_looper(&paint, [&](const SkPaint& p) {
572 mCanvas->drawVertices(vertices, mode, p);
573 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400574}
575
576// ----------------------------------------------------------------------------
577// Canvas draw operations: Bitmaps
578// ----------------------------------------------------------------------------
579
Mike Reedc2dbc032019-07-25 12:28:29 -0400580void SkiaCanvas::drawBitmap(Bitmap& bitmap, float left, float top, const Paint* paint) {
581 auto image = bitmap.makeImage();
582 apply_looper(paint, [&](const SkPaint& p) {
583 mCanvas->drawImage(image, left, top, &p);
584 });
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400585}
586
Mike Reedc2dbc032019-07-25 12:28:29 -0400587void SkiaCanvas::drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const Paint* paint) {
588 auto image = bitmap.makeImage();
Mike Reed6acfe162016-11-18 17:21:09 -0500589 SkAutoCanvasRestore acr(mCanvas, true);
Mike Reed70ffbf92014-12-08 17:03:30 -0500590 mCanvas->concat(matrix);
Mike Reedc2dbc032019-07-25 12:28:29 -0400591 apply_looper(paint, [&](const SkPaint& p) {
592 mCanvas->drawImage(image, 0, 0, &p);
593 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400594}
595
John Reck1bcacfd2017-11-03 10:12:19 -0700596void SkiaCanvas::drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight,
597 float srcBottom, float dstLeft, float dstTop, float dstRight,
Mike Reedc2dbc032019-07-25 12:28:29 -0400598 float dstBottom, const Paint* paint) {
599 auto image = bitmap.makeImage();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400600 SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
601 SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400602
Mike Reedc2dbc032019-07-25 12:28:29 -0400603 apply_looper(paint, [&](const SkPaint& p) {
604 mCanvas->drawImageRect(image, srcRect, dstRect, &p, SkCanvas::kFast_SrcRectConstraint);
605 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400606}
607
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400608void SkiaCanvas::drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight,
Mike Reedc2dbc032019-07-25 12:28:29 -0400609 const float* vertices, const int* colors, const Paint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400610 const int ptCount = (meshWidth + 1) * (meshHeight + 1);
611 const int indexCount = meshWidth * meshHeight * 6;
Mike Reed871cd2d2017-03-17 10:15:52 -0400612 uint32_t flags = SkVertices::kHasTexCoords_BuilderFlag;
613 if (colors) {
614 flags |= SkVertices::kHasColors_BuilderFlag;
615 }
Mike Reed826deef2017-04-04 15:32:04 -0400616 SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, ptCount, indexCount, flags);
Mike Reed871cd2d2017-03-17 10:15:52 -0400617 memcpy(builder.positions(), vertices, ptCount * sizeof(SkPoint));
618 if (colors) {
619 memcpy(builder.colors(), colors, ptCount * sizeof(SkColor));
620 }
621 SkPoint* texs = builder.texCoords();
622 uint16_t* indices = builder.indices();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400623
624 // cons up texture coordinates and indices
625 {
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400626 const SkScalar w = SkIntToScalar(bitmap.width());
627 const SkScalar h = SkIntToScalar(bitmap.height());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400628 const SkScalar dx = w / meshWidth;
629 const SkScalar dy = h / meshHeight;
630
631 SkPoint* texsPtr = texs;
632 SkScalar y = 0;
633 for (int i = 0; i <= meshHeight; i++) {
634 if (i == meshHeight) {
635 y = h; // to ensure numerically we hit h exactly
636 }
637 SkScalar x = 0;
638 for (int j = 0; j < meshWidth; j++) {
639 texsPtr->set(x, y);
640 texsPtr += 1;
641 x += dx;
642 }
643 texsPtr->set(w, y);
644 texsPtr += 1;
645 y += dy;
646 }
647 SkASSERT(texsPtr - texs == ptCount);
648 }
649
650 // cons up indices
651 {
652 uint16_t* indexPtr = indices;
653 int index = 0;
654 for (int i = 0; i < meshHeight; i++) {
655 for (int j = 0; j < meshWidth; j++) {
656 // lower-left triangle
657 *indexPtr++ = index;
658 *indexPtr++ = index + meshWidth + 1;
659 *indexPtr++ = index + meshWidth + 2;
660 // upper-right triangle
661 *indexPtr++ = index;
662 *indexPtr++ = index + meshWidth + 2;
663 *indexPtr++ = index + 1;
664 // bump to the next cell
665 index += 1;
666 }
667 // bump to the next row
668 index += 1;
669 }
670 SkASSERT(indexPtr - indices == indexCount);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400671 }
672
John Reck1bcacfd2017-11-03 10:12:19 -0700673// double-check that we have legal indices
Derek Sollenberger8872b382014-06-23 14:13:53 -0400674#ifdef SK_DEBUG
675 {
676 for (int i = 0; i < indexCount; i++) {
677 SkASSERT((unsigned)indices[i] < (unsigned)ptCount);
678 }
679 }
680#endif
681
682 // cons-up a shader for the bitmap
Mike Reedc2dbc032019-07-25 12:28:29 -0400683 Paint pnt;
684 if (paint) {
685 pnt = *paint;
686 }
Nader Jawadfc42a992020-07-29 22:48:59 -0700687
688 pnt.setShader(sk_ref_sp(new BitmapShader(
689 bitmap.makeImage(), SkTileMode::kClamp, SkTileMode::kClamp, nullptr)));
Mike Reedc2dbc032019-07-25 12:28:29 -0400690 auto v = builder.detach();
691 apply_looper(&pnt, [&](const SkPaint& p) {
692 mCanvas->drawVertices(v, SkBlendMode::kModulate, p);
693 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400694}
695
John Reck1bcacfd2017-11-03 10:12:19 -0700696void SkiaCanvas::drawNinePatch(Bitmap& bitmap, const Res_png_9patch& chunk, float dstLeft,
697 float dstTop, float dstRight, float dstBottom,
Mike Reedc2dbc032019-07-25 12:28:29 -0400698 const Paint* paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400699 SkCanvas::Lattice lattice;
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400700 NinePatchUtils::SetLatticeDivs(&lattice, chunk, bitmap.width(), bitmap.height());
Stan Ilievf50806a2016-10-24 10:40:39 -0400701
Stan Ilieve12d7312017-12-04 14:48:27 -0500702 lattice.fRectTypes = nullptr;
703 lattice.fColors = nullptr;
Stan Ilievf50806a2016-10-24 10:40:39 -0400704 int numFlags = 0;
Stan Iliev021693b2016-10-17 16:26:15 -0400705 if (chunk.numColors > 0 && chunk.numColors == NinePatchUtils::NumDistinctRects(lattice)) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400706 // We can expect the framework to give us a color for every distinct rect.
707 // Skia requires a flag for every rect.
708 numFlags = (lattice.fXCount + 1) * (lattice.fYCount + 1);
709 }
710
Stan Ilieve12d7312017-12-04 14:48:27 -0500711 SkAutoSTMalloc<25, SkCanvas::Lattice::RectType> flags(numFlags);
712 SkAutoSTMalloc<25, SkColor> colors(numFlags);
Stan Ilievf50806a2016-10-24 10:40:39 -0400713 if (numFlags > 0) {
Stan Ilieve12d7312017-12-04 14:48:27 -0500714 NinePatchUtils::SetLatticeFlags(&lattice, flags.get(), numFlags, chunk, colors.get());
Stan Ilievf50806a2016-10-24 10:40:39 -0400715 }
716
717 lattice.fBounds = nullptr;
718 SkRect dst = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Mike Reedc2dbc032019-07-25 12:28:29 -0400719 auto image = bitmap.makeImage();
720 apply_looper(paint, [&](const SkPaint& p) {
721 mCanvas->drawImageLattice(image.get(), lattice, dst, &p);
722 });
Derek Sollenbergeredca3202015-07-10 13:56:39 -0400723}
724
Derek Sollenberger2d142132018-01-22 10:25:26 -0500725double SkiaCanvas::drawAnimatedImage(AnimatedImageDrawable* imgDrawable) {
726 return imgDrawable->drawStaging(mCanvas);
Leon Scroggins III671cce22018-01-14 16:52:17 -0500727}
728
Doris Liu766431a2016-02-04 22:17:11 +0000729void SkiaCanvas::drawVectorDrawable(VectorDrawableRoot* vectorDrawable) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800730 vectorDrawable->drawStaging(this);
Doris Liu766431a2016-02-04 22:17:11 +0000731}
732
Derek Sollenberger8872b382014-06-23 14:13:53 -0400733// ----------------------------------------------------------------------------
734// Canvas draw operations: Text
735// ----------------------------------------------------------------------------
736
Mike Reed2dfd55d2019-01-08 16:19:03 -0500737void SkiaCanvas::drawGlyphs(ReadGlyphFunc glyphFunc, int count, const Paint& paint, float x,
Seigo Nonaka63af7ba2020-09-21 23:07:43 -0700738 float y, float totalAdvance) {
Stan Iliev0b58d992017-03-30 18:22:27 -0400739 if (count <= 0 || paint.nothingToDraw()) return;
Mike Reedf6d86ac2019-01-18 14:13:23 -0500740 Paint paintCopy(paint);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400741 if (mPaintFilter) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500742 mPaintFilter->filterFullPaint(&paintCopy);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400743 }
Mike Reedf6d86ac2019-01-18 14:13:23 -0500744 const SkFont& font = paintCopy.getSkFont();
Stan Iliev7717e222018-02-05 18:04:11 -0500745 // Stroke with a hairline is drawn on HW with a fill style for compatibility with Android O and
746 // older.
John Recke170fb62018-05-07 08:12:07 -0700747 if (!mCanvasOwned && sApiLevel <= 27 && paintCopy.getStrokeWidth() <= 0 &&
748 paintCopy.getStyle() == SkPaint::kStroke_Style) {
Stan Iliev7717e222018-02-05 18:04:11 -0500749 paintCopy.setStyle(SkPaint::kFill_Style);
750 }
Stan Ilievf50806a2016-10-24 10:40:39 -0400751
Stan Ilievf50806a2016-10-24 10:40:39 -0400752 SkTextBlobBuilder builder;
Mike Reed2e204fc2019-01-28 13:31:36 -0500753 const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(font, count);
Stan Iliev0b58d992017-03-30 18:22:27 -0400754 glyphFunc(buffer.glyphs, buffer.pos);
Stan Ilievf50806a2016-10-24 10:40:39 -0400755
756 sk_sp<SkTextBlob> textBlob(builder.make());
Nathaniel Nifong52d37772020-01-10 16:12:41 -0500757
758 apply_looper(&paintCopy, [&](const SkPaint& p) {
759 mCanvas->drawTextBlob(textBlob, 0, 0, p);
760 });
Stan Ilievf50806a2016-10-24 10:40:39 -0400761 drawTextDecorations(x, y, totalAdvance, paintCopy);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400762}
763
Yuqian Liafc221492016-07-18 13:07:42 -0400764void SkiaCanvas::drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
Mike Reed2dfd55d2019-01-08 16:19:03 -0500765 const Paint& paint, const SkPath& path, size_t start,
John Reck1bcacfd2017-11-03 10:12:19 -0700766 size_t end) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500767 Paint paintCopy(paint);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400768 if (mPaintFilter) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500769 mPaintFilter->filterFullPaint(&paintCopy);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400770 }
Mike Reedf6d86ac2019-01-18 14:13:23 -0500771 const SkFont& font = paintCopy.getSkFont();
Derek Sollenberger415a74b2018-03-14 15:03:13 -0400772
Yuqian Liafc221492016-07-18 13:07:42 -0400773 const int N = end - start;
Mike Reed4a4b1be2019-01-01 15:43:06 -0500774 SkTextBlobBuilder builder;
775 auto rec = builder.allocRunRSXform(font, N);
776 SkRSXform* xform = (SkRSXform*)rec.pos;
777 uint16_t* glyphs = rec.glyphs;
Yuqian Liafc221492016-07-18 13:07:42 -0400778 SkPathMeasure meas(path, false);
779
780 for (size_t i = start; i < end; i++) {
781 glyphs[i - start] = layout.getGlyphId(i);
Stan Ilievc6c96dd2018-01-10 16:06:04 -0500782 float halfWidth = layout.getCharAdvance(i) * 0.5f;
783 float x = hOffset + layout.getX(i) + halfWidth;
Yuqian Liafc221492016-07-18 13:07:42 -0400784 float y = vOffset + layout.getY(i);
785
786 SkPoint pos;
787 SkVector tan;
788 if (!meas.getPosTan(x, &pos, &tan)) {
789 pos.set(x, y);
790 tan.set(1, 0);
791 }
792 xform[i - start].fSCos = tan.x();
793 xform[i - start].fSSin = tan.y();
Stan Ilievc6c96dd2018-01-10 16:06:04 -0500794 xform[i - start].fTx = pos.x() - tan.y() * y - halfWidth * tan.x();
795 xform[i - start].fTy = pos.y() + tan.x() * y - halfWidth * tan.y();
Yuqian Liafc221492016-07-18 13:07:42 -0400796 }
Leon Scroggins III950f2aa2020-04-29 13:46:00 -0400797
798 sk_sp<SkTextBlob> textBlob(builder.make());
799
Nathaniel Nifong52d37772020-01-10 16:12:41 -0500800 apply_looper(&paintCopy, [&](const SkPaint& p) {
Leon Scroggins III950f2aa2020-04-29 13:46:00 -0400801 mCanvas->drawTextBlob(textBlob, 0, 0, p);
Nathaniel Nifong52d37772020-01-10 16:12:41 -0500802 });
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
John Reck894e85a2019-07-15 16:16:44 -0700831void SkiaCanvas::drawPicture(const SkPicture& picture) {
832 // TODO: Change to mCanvas->drawPicture()? SkCanvas::drawPicture seems to be
833 // where the logic is for playback vs. ref picture. Using picture.playback here
834 // to stay behavior-identical for now, but should revisit this at some point.
835 picture.playback(mCanvas);
836}
837
Derek Sollenberger6f485562015-07-30 10:00:39 -0400838// ----------------------------------------------------------------------------
839// Canvas draw operations: View System
840// ----------------------------------------------------------------------------
841
Stan Ilievf50806a2016-10-24 10:40:39 -0400842void SkiaCanvas::drawLayer(uirenderer::DeferredLayerUpdater* layerUpdater) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400843 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw Layers");
844}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400845
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400846void SkiaCanvas::drawRenderNode(uirenderer::RenderNode* renderNode) {
847 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw RenderNodes");
848}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400849
John Reck1bcacfd2017-11-03 10:12:19 -0700850} // namespace android