blob: 53c6db0cdf3a24e4deac9698d1f3236942a82a62 [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>
Derek Sollenbergerac33a482019-04-22 16:28:09 -040030#include <SkCanvasPriv.h>
Matt Sarettd0814db2017-04-13 09:33:18 -040031#include <SkCanvasStateUtils.h>
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -040032#include <SkColorFilter.h>
John Reck849911a2015-01-20 07:51:14 -080033#include <SkDeque.h>
John Reck1bcacfd2017-11-03 10:12:19 -070034#include <SkDrawable.h>
Mike Reed1c79eab2018-11-21 11:01:57 -050035#include <SkFont.h>
John Reck849911a2015-01-20 07:51:14 -080036#include <SkGraphics.h>
Derek Sollenberger6f485562015-07-30 10:00:39 -040037#include <SkImage.h>
Matt Sarett62feb3a2016-09-20 10:34:11 -040038#include <SkImagePriv.h>
Leon Scroggins III671cce22018-01-14 16:52:17 -050039#include <SkPicture.h>
Yuqian Liafc221492016-07-18 13:07:42 -040040#include <SkRSXform.h>
John Reck849911a2015-01-20 07:51:14 -080041#include <SkShader.h>
John Reck849911a2015-01-20 07:51:14 -080042#include <SkTemplates.h>
Stan Ilievf50806a2016-10-24 10:40:39 -040043#include <SkTextBlob.h>
Brian Osmane3b9a122020-04-01 12:24:19 -040044#include <SkVertices.h>
Derek Sollenberger8872b382014-06-23 14:13:53 -040045
Ben Wagner60126ef2015-08-07 12:13:48 -040046#include <memory>
Ben Wagner0ed10be2018-06-28 17:08:16 -040047#include <optional>
48#include <utility>
Ben Wagner60126ef2015-08-07 12:13:48 -040049
Derek Sollenberger8872b382014-06-23 14:13:53 -040050namespace android {
51
Stan Ilievf50806a2016-10-24 10:40:39 -040052using uirenderer::PaintUtils;
53
John Reckc1b33d62015-04-22 09:04:45 -070054Canvas* Canvas::create_canvas(const SkBitmap& bitmap) {
Derek Sollenberger8872b382014-06-23 14:13:53 -040055 return new SkiaCanvas(bitmap);
56}
57
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040058Canvas* Canvas::create_canvas(SkCanvas* skiaCanvas) {
59 return new SkiaCanvas(skiaCanvas);
Derek Sollenberger8872b382014-06-23 14:13:53 -040060}
61
Stan Ilievf50806a2016-10-24 10:40:39 -040062SkiaCanvas::SkiaCanvas() {}
63
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040064SkiaCanvas::SkiaCanvas(SkCanvas* canvas) : mCanvas(canvas) {}
Stan Ilievf50806a2016-10-24 10:40:39 -040065
John Reckc1b33d62015-04-22 09:04:45 -070066SkiaCanvas::SkiaCanvas(const SkBitmap& bitmap) {
Derek Sollenbergerd01b5912018-10-19 15:55:33 -040067 mCanvasOwned = std::unique_ptr<SkCanvas>(new SkCanvas(bitmap));
68 mCanvas = mCanvasOwned.get();
Derek Sollenberger8872b382014-06-23 14:13:53 -040069}
70
Stan Iliev021693b2016-10-17 16:26:15 -040071SkiaCanvas::~SkiaCanvas() {}
72
Derek Sollenbergerc1908132016-07-15 10:28:16 -040073void SkiaCanvas::reset(SkCanvas* skiaCanvas) {
Mike Reed6acfe162016-11-18 17:21:09 -050074 if (mCanvas != skiaCanvas) {
75 mCanvas = skiaCanvas;
76 mCanvasOwned.reset();
77 }
Derek Sollenbergerc1908132016-07-15 10:28:16 -040078 mSaveStack.reset(nullptr);
Derek Sollenbergerc1908132016-07-15 10:28:16 -040079}
80
Derek Sollenberger8872b382014-06-23 14:13:53 -040081// ----------------------------------------------------------------------------
82// Canvas state operations: Replace Bitmap
83// ----------------------------------------------------------------------------
84
John Reckc1b33d62015-04-22 09:04:45 -070085void SkiaCanvas::setBitmap(const SkBitmap& bitmap) {
Tony Mantler4f641d12017-03-14 22:36:14 +000086 // deletes the previously owned canvas (if any)
Derek Sollenbergerd01b5912018-10-19 15:55:33 -040087 mCanvasOwned.reset(new SkCanvas(bitmap));
88 mCanvas = mCanvasOwned.get();
Tony Mantler4f641d12017-03-14 22:36:14 +000089
Derek Sollenberger8872b382014-06-23 14:13:53 -040090 // clean up the old save stack
Stan Ilievf50806a2016-10-24 10:40:39 -040091 mSaveStack.reset(nullptr);
Derek Sollenberger8872b382014-06-23 14:13:53 -040092}
93
94// ----------------------------------------------------------------------------
95// Canvas state operations
96// ----------------------------------------------------------------------------
97
98bool SkiaCanvas::isOpaque() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -040099 return mCanvas->imageInfo().isOpaque();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400100}
101
102int SkiaCanvas::width() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400103 return mCanvas->imageInfo().width();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400104}
105
106int SkiaCanvas::height() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400107 return mCanvas->imageInfo().height();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400108}
109
110// ----------------------------------------------------------------------------
111// Canvas state operations: Save (layer)
112// ----------------------------------------------------------------------------
113
114int SkiaCanvas::getSaveCount() const {
115 return mCanvas->getSaveCount();
116}
117
Florin Malitaeecff562015-12-21 10:43:01 -0500118int SkiaCanvas::save(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400119 int count = mCanvas->save();
120 recordPartialSave(flags);
121 return count;
122}
123
Florin Malita5e271402015-11-04 14:36:02 -0500124// The SkiaCanvas::restore operation layers on the capability to preserve
125// either (or both) the matrix and/or clip state after a SkCanvas::restore
126// operation. It does this by explicitly saving off the clip & matrix state
127// when requested and playing it back after the SkCanvas::restore.
Derek Sollenberger8872b382014-06-23 14:13:53 -0400128void SkiaCanvas::restore() {
Stan Ilievf50806a2016-10-24 10:40:39 -0400129 const auto* rec = this->currentSaveRec();
130 if (!rec) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400131 // Fast path - no record for this frame.
132 mCanvas->restore();
133 return;
134 }
135
Florin Malitaeecff562015-12-21 10:43:01 -0500136 bool preserveMatrix = !(rec->saveFlags & SaveFlags::Matrix);
John Reck1bcacfd2017-11-03 10:12:19 -0700137 bool preserveClip = !(rec->saveFlags & SaveFlags::Clip);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400138
139 SkMatrix savedMatrix;
140 if (preserveMatrix) {
141 savedMatrix = mCanvas->getTotalMatrix();
142 }
143
Stan Ilievf50806a2016-10-24 10:40:39 -0400144 const size_t clipIndex = rec->clipIndex;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400145
146 mCanvas->restore();
Stan Ilievf50806a2016-10-24 10:40:39 -0400147 mSaveStack->pop_back();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400148
149 if (preserveMatrix) {
150 mCanvas->setMatrix(savedMatrix);
151 }
152
Stan Ilievf50806a2016-10-24 10:40:39 -0400153 if (preserveClip) {
154 this->applyPersistentClips(clipIndex);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400155 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400156}
157
158void SkiaCanvas::restoreToCount(int restoreCount) {
159 while (mCanvas->getSaveCount() > restoreCount) {
160 this->restore();
161 }
162}
163
John Recka00eef212020-11-16 12:45:55 -0500164int SkiaCanvas::saveLayer(float left, float top, float right, float bottom, const SkPaint* paint) {
Florin Malitaeecff562015-12-21 10:43:01 -0500165 const SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
John Recka00eef212020-11-16 12:45:55 -0500166 const SkCanvas::SaveLayerRec rec(&bounds, paint);
Florin Malitaeecff562015-12-21 10:43:01 -0500167
Stan Iliev68885e32016-12-14 11:18:34 -0500168 return mCanvas->saveLayer(rec);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400169}
170
John Recka00eef212020-11-16 12:45:55 -0500171int SkiaCanvas::saveLayerAlpha(float left, float top, float right, float bottom, int alpha) {
Florin Malitaeecff562015-12-21 10:43:01 -0500172 if (static_cast<unsigned>(alpha) < 0xFF) {
Yuqian Lifd92ee42016-04-27 17:03:38 -0400173 SkPaint alphaPaint;
174 alphaPaint.setAlpha(alpha);
John Recka00eef212020-11-16 12:45:55 -0500175 return this->saveLayer(left, top, right, bottom, &alphaPaint);
Florin Malitaeecff562015-12-21 10:43:01 -0500176 }
John Recka00eef212020-11-16 12:45:55 -0500177 return this->saveLayer(left, top, right, bottom, nullptr);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400178}
179
Derek Sollenberger24fc9012018-12-07 14:12:12 -0500180int SkiaCanvas::saveUnclippedLayer(int left, int top, int right, int bottom) {
181 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
182 return SkAndroidFrameworkUtils::SaveBehind(mCanvas, &bounds);
183}
184
Mike Reeda6cb58a2021-07-10 13:31:34 -0400185void SkiaCanvas::restoreUnclippedLayer(int restoreCount, const Paint& paint) {
Derek Sollenbergerac33a482019-04-22 16:28:09 -0400186
187 while (mCanvas->getSaveCount() > restoreCount + 1) {
188 this->restore();
189 }
190
191 if (mCanvas->getSaveCount() == restoreCount + 1) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500192 SkCanvasPriv::DrawBehind(mCanvas, filterPaint(paint));
Derek Sollenbergerac33a482019-04-22 16:28:09 -0400193 this->restore();
194 }
195}
196
Stan Ilievf50806a2016-10-24 10:40:39 -0400197class SkiaCanvas::Clip {
198public:
Mike Reed6e49c9f2016-12-02 15:36:59 -0500199 Clip(const SkRect& rect, SkClipOp op, const SkMatrix& m)
John Reck1bcacfd2017-11-03 10:12:19 -0700200 : mType(Type::Rect), mOp(op), mMatrix(m), mRRect(SkRRect::MakeRect(rect)) {}
Mike Reed6e49c9f2016-12-02 15:36:59 -0500201 Clip(const SkRRect& rrect, SkClipOp op, const SkMatrix& m)
John Reck1bcacfd2017-11-03 10:12:19 -0700202 : mType(Type::RRect), mOp(op), mMatrix(m), mRRect(rrect) {}
Mike Reed6e49c9f2016-12-02 15:36:59 -0500203 Clip(const SkPath& path, SkClipOp op, const SkMatrix& m)
Ben Wagner0ed10be2018-06-28 17:08:16 -0400204 : mType(Type::Path), mOp(op), mMatrix(m), mPath(std::in_place, path) {}
Stan Ilievf50806a2016-10-24 10:40:39 -0400205
206 void apply(SkCanvas* canvas) const {
207 canvas->setMatrix(mMatrix);
208 switch (mType) {
John Reck1bcacfd2017-11-03 10:12:19 -0700209 case Type::Rect:
Nader Jawade431e312019-12-04 14:13:18 -0800210 // Don't anti-alias rectangular clips
211 canvas->clipRect(mRRect.rect(), mOp, false);
John Reck1bcacfd2017-11-03 10:12:19 -0700212 break;
213 case Type::RRect:
Nader Jawade431e312019-12-04 14:13:18 -0800214 // Ensure rounded rectangular clips are anti-aliased
215 canvas->clipRRect(mRRect, mOp, true);
John Reck1bcacfd2017-11-03 10:12:19 -0700216 break;
217 case Type::Path:
Nader Jawade431e312019-12-04 14:13:18 -0800218 // Ensure path clips are anti-aliased
219 canvas->clipPath(mPath.value(), mOp, true);
John Reck1bcacfd2017-11-03 10:12:19 -0700220 break;
Stan Ilievf50806a2016-10-24 10:40:39 -0400221 }
222 }
223
224private:
225 enum class Type {
226 Rect,
227 RRect,
228 Path,
229 };
230
John Reck1bcacfd2017-11-03 10:12:19 -0700231 Type mType;
232 SkClipOp mOp;
233 SkMatrix mMatrix;
Stan Ilievf50806a2016-10-24 10:40:39 -0400234
235 // These are logically a union (tracked separately due to non-POD path).
Ben Wagner0ed10be2018-06-28 17:08:16 -0400236 std::optional<SkPath> mPath;
John Reck1bcacfd2017-11-03 10:12:19 -0700237 SkRRect mRRect;
Stan Ilievf50806a2016-10-24 10:40:39 -0400238};
239
240const SkiaCanvas::SaveRec* SkiaCanvas::currentSaveRec() const {
John Reck1bcacfd2017-11-03 10:12:19 -0700241 const SaveRec* rec = mSaveStack ? static_cast<const SaveRec*>(mSaveStack->back()) : nullptr;
Stan Ilievf50806a2016-10-24 10:40:39 -0400242 int currentSaveCount = mCanvas->getSaveCount();
243 SkASSERT(!rec || currentSaveCount >= rec->saveCount);
244
245 return (rec && rec->saveCount == currentSaveCount) ? rec : nullptr;
246}
247
Nader Jawad2dc632a2021-03-29 18:51:29 -0700248void SkiaCanvas::punchHole(const SkRRect& rect) {
249 SkPaint paint = SkPaint();
250 paint.setColor(0);
251 paint.setBlendMode(SkBlendMode::kClear);
252 mCanvas->drawRRect(rect, paint);
253}
254
Derek Sollenberger8872b382014-06-23 14:13:53 -0400255// ----------------------------------------------------------------------------
256// functions to emulate legacy SaveFlags (i.e. independent matrix/clip flags)
257// ----------------------------------------------------------------------------
258
Florin Malitaeecff562015-12-21 10:43:01 -0500259void SkiaCanvas::recordPartialSave(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400260 // A partial save is a save operation which doesn't capture the full canvas state.
Florin Malitaeecff562015-12-21 10:43:01 -0500261 // (either SaveFlags::Matrix or SaveFlags::Clip is missing).
Derek Sollenberger8872b382014-06-23 14:13:53 -0400262
263 // Mask-out non canvas state bits.
Florin Malitaeecff562015-12-21 10:43:01 -0500264 flags &= SaveFlags::MatrixClip;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400265
Florin Malitaeecff562015-12-21 10:43:01 -0500266 if (flags == SaveFlags::MatrixClip) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400267 // not a partial save.
268 return;
269 }
270
Stan Ilievf50806a2016-10-24 10:40:39 -0400271 if (!mSaveStack) {
Ben Wagnerd1cbc162015-08-19 12:45:09 -0400272 mSaveStack.reset(new SkDeque(sizeof(struct SaveRec), 8));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400273 }
274
275 SaveRec* rec = static_cast<SaveRec*>(mSaveStack->push_back());
Florin Malita5e271402015-11-04 14:36:02 -0500276 rec->saveCount = mCanvas->getSaveCount();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400277 rec->saveFlags = flags;
Stan Ilievf50806a2016-10-24 10:40:39 -0400278 rec->clipIndex = mClipStack.size();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400279}
280
Stan Ilievf50806a2016-10-24 10:40:39 -0400281template <typename T>
Mike Reed6e49c9f2016-12-02 15:36:59 -0500282void SkiaCanvas::recordClip(const T& clip, SkClipOp op) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400283 // Only need tracking when in a partial save frame which
284 // doesn't restore the clip.
285 const SaveRec* rec = this->currentSaveRec();
286 if (rec && !(rec->saveFlags & SaveFlags::Clip)) {
287 mClipStack.emplace_back(clip, op, mCanvas->getTotalMatrix());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400288 }
289}
290
Stan Ilievf50806a2016-10-24 10:40:39 -0400291// Applies and optionally removes all clips >= index.
292void SkiaCanvas::applyPersistentClips(size_t clipStartIndex) {
293 SkASSERT(clipStartIndex <= mClipStack.size());
294 const auto begin = mClipStack.cbegin() + clipStartIndex;
295 const auto end = mClipStack.cend();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400296
Stan Ilievf50806a2016-10-24 10:40:39 -0400297 // Clip application mutates the CTM.
298 const SkMatrix saveMatrix = mCanvas->getTotalMatrix();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400299
Stan Ilievf50806a2016-10-24 10:40:39 -0400300 for (auto clip = begin; clip != end; ++clip) {
Mike Reed6acfe162016-11-18 17:21:09 -0500301 clip->apply(mCanvas);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400302 }
303
Stan Ilievf50806a2016-10-24 10:40:39 -0400304 mCanvas->setMatrix(saveMatrix);
305
306 // If the current/post-restore save rec is also persisting clips, we
307 // leave them on the stack to be reapplied part of the next restore().
308 // Otherwise we're done and just pop them.
309 const auto* rec = this->currentSaveRec();
310 if (!rec || (rec->saveFlags & SaveFlags::Clip)) {
311 mClipStack.erase(begin, end);
312 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400313}
314
315// ----------------------------------------------------------------------------
316// Canvas state operations: Matrix
317// ----------------------------------------------------------------------------
318
319void SkiaCanvas::getMatrix(SkMatrix* outMatrix) const {
320 *outMatrix = mCanvas->getTotalMatrix();
321}
322
323void SkiaCanvas::setMatrix(const SkMatrix& matrix) {
324 mCanvas->setMatrix(matrix);
325}
326
327void SkiaCanvas::concat(const SkMatrix& matrix) {
328 mCanvas->concat(matrix);
329}
330
331void SkiaCanvas::rotate(float degrees) {
332 mCanvas->rotate(degrees);
333}
334
335void SkiaCanvas::scale(float sx, float sy) {
336 mCanvas->scale(sx, sy);
337}
338
339void SkiaCanvas::skew(float sx, float sy) {
340 mCanvas->skew(sx, sy);
341}
342
343void SkiaCanvas::translate(float dx, float dy) {
344 mCanvas->translate(dx, dy);
345}
346
347// ----------------------------------------------------------------------------
348// Canvas state operations: Clips
349// ----------------------------------------------------------------------------
350
351// This function is a mirror of SkCanvas::getClipBounds except that it does
352// not outset the edge of the clip to account for anti-aliasing. There is
353// a skia bug to investigate pushing this logic into back into skia.
354// (see https://code.google.com/p/skia/issues/detail?id=1303)
355bool SkiaCanvas::getClipBounds(SkRect* outRect) const {
356 SkIRect ibounds;
Mike Reed5e438982017-01-25 08:23:25 -0500357 if (!mCanvas->getDeviceClipBounds(&ibounds)) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400358 return false;
359 }
360
361 SkMatrix inverse;
362 // if we can't invert the CTM, we can't return local clip bounds
363 if (!mCanvas->getTotalMatrix().invert(&inverse)) {
364 if (outRect) {
365 outRect->setEmpty();
366 }
367 return false;
368 }
369
370 if (NULL != outRect) {
371 SkRect r = SkRect::Make(ibounds);
372 inverse.mapRect(outRect, r);
373 }
374 return true;
375}
376
377bool SkiaCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
378 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
379 return mCanvas->quickReject(bounds);
380}
381
382bool SkiaCanvas::quickRejectPath(const SkPath& path) const {
383 return mCanvas->quickReject(path);
384}
385
Mike Reed6e49c9f2016-12-02 15:36:59 -0500386bool SkiaCanvas::clipRect(float left, float top, float right, float bottom, SkClipOp op) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400387 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Stan Ilievf50806a2016-10-24 10:40:39 -0400388 this->recordClip(rect, op);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400389 mCanvas->clipRect(rect, op);
Chris Craik5ec6a282015-06-23 15:42:12 -0700390 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400391}
392
Mike Reed6e49c9f2016-12-02 15:36:59 -0500393bool SkiaCanvas::clipPath(const SkPath* path, SkClipOp op) {
Derek Sollenbergerf7d98f42017-04-17 11:27:36 -0400394 this->recordClip(*path, op);
Nader Jawade431e312019-12-04 14:13:18 -0800395 mCanvas->clipPath(*path, op, true);
Chris Craik5ec6a282015-06-23 15:42:12 -0700396 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400397}
398
Michael Ludwig70cf50c22021-07-21 17:02:39 +0000399bool SkiaCanvas::replaceClipRect_deprecated(float left, float top, float right, float bottom) {
400 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
401
402 // Emulated clip rects are not recorded for partial saves, since
403 // partial saves have been removed from the public API.
404 SkAndroidFrameworkUtils::ResetClip(mCanvas);
405 mCanvas->clipRect(rect, SkClipOp::kIntersect);
406 return !mCanvas->isClipEmpty();
407}
408
409bool SkiaCanvas::replaceClipPath_deprecated(const SkPath* path) {
410 SkAndroidFrameworkUtils::ResetClip(mCanvas);
411 mCanvas->clipPath(*path, SkClipOp::kIntersect, true);
412 return !mCanvas->isClipEmpty();
413}
414
Derek Sollenberger8872b382014-06-23 14:13:53 -0400415// ----------------------------------------------------------------------------
416// Canvas state operations: Filters
417// ----------------------------------------------------------------------------
418
Ben Wagner0ed10be2018-06-28 17:08:16 -0400419PaintFilter* SkiaCanvas::getPaintFilter() {
420 return mPaintFilter.get();
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400421}
422
Ben Wagner0ed10be2018-06-28 17:08:16 -0400423void SkiaCanvas::setPaintFilter(sk_sp<PaintFilter> paintFilter) {
424 mPaintFilter = std::move(paintFilter);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400425}
426
427// ----------------------------------------------------------------------------
Matt Sarettd0814db2017-04-13 09:33:18 -0400428// Canvas state operations: Capture
429// ----------------------------------------------------------------------------
430
431SkCanvasState* SkiaCanvas::captureCanvasState() const {
432 SkCanvas* canvas = mCanvas;
433 if (mCanvasOwned) {
434 // Important to use the underlying SkCanvas, not the wrapper.
435 canvas = mCanvasOwned.get();
436 }
437
438 // Workarounds for http://crbug.com/271096: SW draw only supports
439 // translate & scale transforms, and a simple rectangular clip.
440 // (This also avoids significant wasted time in calling
441 // SkCanvasStateUtils::CaptureCanvasState when the clip is complex).
John Reck1bcacfd2017-11-03 10:12:19 -0700442 if (!canvas->isClipRect() || (canvas->getTotalMatrix().getType() &
443 ~(SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask))) {
444 return nullptr;
Matt Sarettd0814db2017-04-13 09:33:18 -0400445 }
446
447 return SkCanvasStateUtils::CaptureCanvasState(canvas);
448}
449
450// ----------------------------------------------------------------------------
Derek Sollenberger8872b382014-06-23 14:13:53 -0400451// Canvas draw operations
452// ----------------------------------------------------------------------------
453
Mike Reed260ab722016-10-07 15:59:20 -0400454void SkiaCanvas::drawColor(int color, SkBlendMode mode) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400455 mCanvas->drawColor(color, mode);
456}
457
Mike Reeda6cb58a2021-07-10 13:31:34 -0400458void SkiaCanvas::onFilterPaint(Paint& paint) {
Ben Wagner0ed10be2018-06-28 17:08:16 -0400459 if (mPaintFilter) {
Mike Reeda6cb58a2021-07-10 13:31:34 -0400460 mPaintFilter->filterFullPaint(&paint);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400461 }
Ben Wagner0ed10be2018-06-28 17:08:16 -0400462}
463
Mike Reeda6cb58a2021-07-10 13:31:34 -0400464void SkiaCanvas::drawPaint(const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500465 mCanvas->drawPaint(filterPaint(paint));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400466}
467
468// ----------------------------------------------------------------------------
469// Canvas draw operations: Geometry
470// ----------------------------------------------------------------------------
471
Mike Reedc2dbc032019-07-25 12:28:29 -0400472void SkiaCanvas::drawPoints(const float* points, int count, const Paint& paint,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400473 SkCanvas::PointMode mode) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500474 if (CC_UNLIKELY(count < 2 || paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400475 // convert the floats into SkPoints
John Reck1bcacfd2017-11-03 10:12:19 -0700476 count >>= 1; // now it is the number of points
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400477 std::unique_ptr<SkPoint[]> pts(new SkPoint[count]);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400478 for (int i = 0; i < count; i++) {
479 pts[i].set(points[0], points[1]);
480 points += 2;
481 }
Mike Reedc2dbc032019-07-25 12:28:29 -0400482
Mike Reedbb4cb482021-02-22 14:21:20 -0500483 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawPoints(mode, count, pts.get(), p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400484}
485
Mike Reedc2dbc032019-07-25 12:28:29 -0400486void SkiaCanvas::drawPoint(float x, float y, const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500487 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawPoint(x, y, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400488}
489
Mike Reedc2dbc032019-07-25 12:28:29 -0400490void SkiaCanvas::drawPoints(const float* points, int count, const Paint& paint) {
491 this->drawPoints(points, count, paint, SkCanvas::kPoints_PointMode);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400492}
493
494void SkiaCanvas::drawLine(float startX, float startY, float stopX, float stopY,
Mike Reedc2dbc032019-07-25 12:28:29 -0400495 const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500496 applyLooper(&paint,
497 [&](const SkPaint& p) { mCanvas->drawLine(startX, startY, stopX, stopY, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400498}
499
Mike Reedc2dbc032019-07-25 12:28:29 -0400500void SkiaCanvas::drawLines(const float* points, int count, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500501 if (CC_UNLIKELY(count < 4 || paint.nothingToDraw())) return;
Mike Reedc2dbc032019-07-25 12:28:29 -0400502 this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400503}
504
Mike Reedc2dbc032019-07-25 12:28:29 -0400505void SkiaCanvas::drawRect(float left, float top, float right, float bottom, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500506 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Mike Reedbb4cb482021-02-22 14:21:20 -0500507 applyLooper(&paint, [&](const SkPaint& p) {
Mike Reedc2dbc032019-07-25 12:28:29 -0400508 mCanvas->drawRect({left, top, right, bottom}, p);
509 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400510}
511
Mike Reedc2dbc032019-07-25 12:28:29 -0400512void SkiaCanvas::drawRegion(const SkRegion& region, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500513 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Mike Reedbb4cb482021-02-22 14:21:20 -0500514 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawRegion(region, p); });
Derek Sollenberger94394b32015-07-10 09:58:41 -0400515}
516
John Reck1bcacfd2017-11-03 10:12:19 -0700517void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom, float rx, float ry,
Mike Reedc2dbc032019-07-25 12:28:29 -0400518 const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500519 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400520 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Mike Reedbb4cb482021-02-22 14:21:20 -0500521 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawRoundRect(rect, rx, ry, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400522}
523
Nader Jawadadfe1d92018-09-27 12:27:36 -0700524void SkiaCanvas::drawDoubleRoundRect(const SkRRect& outer, const SkRRect& inner,
Mike Reedc2dbc032019-07-25 12:28:29 -0400525 const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500526 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawDRRect(outer, inner, p); });
Nader Jawadadfe1d92018-09-27 12:27:36 -0700527}
528
Mike Reedc2dbc032019-07-25 12:28:29 -0400529void SkiaCanvas::drawCircle(float x, float y, float radius, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500530 if (CC_UNLIKELY(radius <= 0 || paint.nothingToDraw())) return;
Mike Reedbb4cb482021-02-22 14:21:20 -0500531 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawCircle(x, y, radius, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400532}
533
Mike Reedc2dbc032019-07-25 12:28:29 -0400534void SkiaCanvas::drawOval(float left, float top, float right, float bottom, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500535 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400536 SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
Mike Reedbb4cb482021-02-22 14:21:20 -0500537 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawOval(oval, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400538}
539
John Reck1bcacfd2017-11-03 10:12:19 -0700540void SkiaCanvas::drawArc(float left, float top, float right, float bottom, float startAngle,
Mike Reedc2dbc032019-07-25 12:28:29 -0400541 float sweepAngle, bool useCenter, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500542 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400543 SkRect arc = SkRect::MakeLTRB(left, top, right, bottom);
Mike Reedbb4cb482021-02-22 14:21:20 -0500544 applyLooper(&paint, [&](const SkPaint& p) {
Mike Reedc2dbc032019-07-25 12:28:29 -0400545 if (fabs(sweepAngle) >= 360.0f) {
546 mCanvas->drawOval(arc, p);
547 } else {
548 mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, p);
549 }
550 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400551}
552
Mike Reedc2dbc032019-07-25 12:28:29 -0400553void SkiaCanvas::drawPath(const SkPath& path, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500554 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Stan Iliev6dcfdec2017-08-15 16:42:05 -0400555 if (CC_UNLIKELY(path.isEmpty() && (!path.isInverseFillType()))) {
556 return;
557 }
Mike Reedbb4cb482021-02-22 14:21:20 -0500558 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawPath(path, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400559}
560
Mike Reedc2dbc032019-07-25 12:28:29 -0400561void SkiaCanvas::drawVertices(const SkVertices* vertices, SkBlendMode mode, const Paint& paint) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500562 applyLooper(&paint, [&](const SkPaint& p) { mCanvas->drawVertices(vertices, mode, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400563}
564
565// ----------------------------------------------------------------------------
566// Canvas draw operations: Bitmaps
567// ----------------------------------------------------------------------------
568
Mike Reedc2dbc032019-07-25 12:28:29 -0400569void SkiaCanvas::drawBitmap(Bitmap& bitmap, float left, float top, const Paint* paint) {
570 auto image = bitmap.makeImage();
Mike Reeda6cb58a2021-07-10 13:31:34 -0400571 applyLooper(paint, [&](const Paint& p) {
572 mCanvas->drawImage(image, left, top, p.sampling(), &p);
Mike Reedc2dbc032019-07-25 12:28:29 -0400573 });
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400574}
575
Mike Reedc2dbc032019-07-25 12:28:29 -0400576void SkiaCanvas::drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const Paint* paint) {
577 auto image = bitmap.makeImage();
Mike Reed6acfe162016-11-18 17:21:09 -0500578 SkAutoCanvasRestore acr(mCanvas, true);
Mike Reed70ffbf92014-12-08 17:03:30 -0500579 mCanvas->concat(matrix);
Mike Reeda6cb58a2021-07-10 13:31:34 -0400580 applyLooper(paint, [&](const Paint& p) {
581 mCanvas->drawImage(image, 0, 0, p.sampling(), &p);
Mike Reedc2dbc032019-07-25 12:28:29 -0400582 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400583}
584
John Reck1bcacfd2017-11-03 10:12:19 -0700585void SkiaCanvas::drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight,
586 float srcBottom, float dstLeft, float dstTop, float dstRight,
Mike Reedc2dbc032019-07-25 12:28:29 -0400587 float dstBottom, const Paint* paint) {
588 auto image = bitmap.makeImage();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400589 SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
590 SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400591
Mike Reeda6cb58a2021-07-10 13:31:34 -0400592 applyLooper(paint, [&](const Paint& p) {
593 mCanvas->drawImageRect(image, srcRect, dstRect, p.sampling(), &p,
Mike Reed7994a312021-01-28 18:06:26 -0500594 SkCanvas::kFast_SrcRectConstraint);
Mike Reedc2dbc032019-07-25 12:28:29 -0400595 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400596}
597
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400598void SkiaCanvas::drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight,
Mike Reedc2dbc032019-07-25 12:28:29 -0400599 const float* vertices, const int* colors, const Paint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400600 const int ptCount = (meshWidth + 1) * (meshHeight + 1);
601 const int indexCount = meshWidth * meshHeight * 6;
Mike Reed871cd2d2017-03-17 10:15:52 -0400602 uint32_t flags = SkVertices::kHasTexCoords_BuilderFlag;
603 if (colors) {
604 flags |= SkVertices::kHasColors_BuilderFlag;
605 }
Mike Reed826deef2017-04-04 15:32:04 -0400606 SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, ptCount, indexCount, flags);
Mike Reed871cd2d2017-03-17 10:15:52 -0400607 memcpy(builder.positions(), vertices, ptCount * sizeof(SkPoint));
608 if (colors) {
609 memcpy(builder.colors(), colors, ptCount * sizeof(SkColor));
610 }
611 SkPoint* texs = builder.texCoords();
612 uint16_t* indices = builder.indices();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400613
614 // cons up texture coordinates and indices
615 {
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400616 const SkScalar w = SkIntToScalar(bitmap.width());
617 const SkScalar h = SkIntToScalar(bitmap.height());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400618 const SkScalar dx = w / meshWidth;
619 const SkScalar dy = h / meshHeight;
620
621 SkPoint* texsPtr = texs;
622 SkScalar y = 0;
623 for (int i = 0; i <= meshHeight; i++) {
624 if (i == meshHeight) {
625 y = h; // to ensure numerically we hit h exactly
626 }
627 SkScalar x = 0;
628 for (int j = 0; j < meshWidth; j++) {
629 texsPtr->set(x, y);
630 texsPtr += 1;
631 x += dx;
632 }
633 texsPtr->set(w, y);
634 texsPtr += 1;
635 y += dy;
636 }
637 SkASSERT(texsPtr - texs == ptCount);
638 }
639
640 // cons up indices
641 {
642 uint16_t* indexPtr = indices;
643 int index = 0;
644 for (int i = 0; i < meshHeight; i++) {
645 for (int j = 0; j < meshWidth; j++) {
646 // lower-left triangle
647 *indexPtr++ = index;
648 *indexPtr++ = index + meshWidth + 1;
649 *indexPtr++ = index + meshWidth + 2;
650 // upper-right triangle
651 *indexPtr++ = index;
652 *indexPtr++ = index + meshWidth + 2;
653 *indexPtr++ = index + 1;
654 // bump to the next cell
655 index += 1;
656 }
657 // bump to the next row
658 index += 1;
659 }
660 SkASSERT(indexPtr - indices == indexCount);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400661 }
662
John Reck1bcacfd2017-11-03 10:12:19 -0700663// double-check that we have legal indices
Derek Sollenberger8872b382014-06-23 14:13:53 -0400664#ifdef SK_DEBUG
665 {
666 for (int i = 0; i < indexCount; i++) {
667 SkASSERT((unsigned)indices[i] < (unsigned)ptCount);
668 }
669 }
670#endif
671
Mike Reed7994a312021-01-28 18:06:26 -0500672 auto image = bitmap.makeImage();
673
Derek Sollenberger8872b382014-06-23 14:13:53 -0400674 // cons-up a shader for the bitmap
Mike Reedc2dbc032019-07-25 12:28:29 -0400675 Paint pnt;
676 if (paint) {
677 pnt = *paint;
678 }
Mike Reeda6cb58a2021-07-10 13:31:34 -0400679 SkSamplingOptions sampling = pnt.sampling();
Mike Reed7994a312021-01-28 18:06:26 -0500680 pnt.setShader(image->makeShader(sampling));
681
Mike Reedc2dbc032019-07-25 12:28:29 -0400682 auto v = builder.detach();
Mike Reeda6cb58a2021-07-10 13:31:34 -0400683 applyLooper(&pnt, [&](const Paint& p) {
Mike Reed7994a312021-01-28 18:06:26 -0500684 SkPaint copy(p);
Mike Reeda6cb58a2021-07-10 13:31:34 -0400685 auto s = p.sampling();
Mike Reed7994a312021-01-28 18:06:26 -0500686 if (s != sampling) {
Mike Reedbb4cb482021-02-22 14:21:20 -0500687 // applyLooper changed the quality?
Mike Reed7994a312021-01-28 18:06:26 -0500688 copy.setShader(image->makeShader(s));
689 }
690 mCanvas->drawVertices(v, SkBlendMode::kModulate, copy);
Mike Reedc2dbc032019-07-25 12:28:29 -0400691 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400692}
693
John Reck1bcacfd2017-11-03 10:12:19 -0700694void SkiaCanvas::drawNinePatch(Bitmap& bitmap, const Res_png_9patch& chunk, float dstLeft,
695 float dstTop, float dstRight, float dstBottom,
Mike Reedc2dbc032019-07-25 12:28:29 -0400696 const Paint* paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400697 SkCanvas::Lattice lattice;
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400698 NinePatchUtils::SetLatticeDivs(&lattice, chunk, bitmap.width(), bitmap.height());
Stan Ilievf50806a2016-10-24 10:40:39 -0400699
Stan Ilieve12d7312017-12-04 14:48:27 -0500700 lattice.fRectTypes = nullptr;
701 lattice.fColors = nullptr;
Stan Ilievf50806a2016-10-24 10:40:39 -0400702 int numFlags = 0;
Stan Iliev021693b2016-10-17 16:26:15 -0400703 if (chunk.numColors > 0 && chunk.numColors == NinePatchUtils::NumDistinctRects(lattice)) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400704 // We can expect the framework to give us a color for every distinct rect.
705 // Skia requires a flag for every rect.
706 numFlags = (lattice.fXCount + 1) * (lattice.fYCount + 1);
707 }
708
Stan Ilieve12d7312017-12-04 14:48:27 -0500709 SkAutoSTMalloc<25, SkCanvas::Lattice::RectType> flags(numFlags);
710 SkAutoSTMalloc<25, SkColor> colors(numFlags);
Stan Ilievf50806a2016-10-24 10:40:39 -0400711 if (numFlags > 0) {
Stan Ilieve12d7312017-12-04 14:48:27 -0500712 NinePatchUtils::SetLatticeFlags(&lattice, flags.get(), numFlags, chunk, colors.get());
Stan Ilievf50806a2016-10-24 10:40:39 -0400713 }
714
715 lattice.fBounds = nullptr;
716 SkRect dst = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Mike Reedc2dbc032019-07-25 12:28:29 -0400717 auto image = bitmap.makeImage();
Mike Reeda6cb58a2021-07-10 13:31:34 -0400718 applyLooper(paint, [&](const Paint& p) {
719 mCanvas->drawImageLattice(image.get(), lattice, dst, p.filterMode(), &p);
Mike Reedc2dbc032019-07-25 12:28:29 -0400720 });
Derek Sollenbergeredca3202015-07-10 13:56:39 -0400721}
722
Derek Sollenberger2d142132018-01-22 10:25:26 -0500723double SkiaCanvas::drawAnimatedImage(AnimatedImageDrawable* imgDrawable) {
724 return imgDrawable->drawStaging(mCanvas);
Leon Scroggins III671cce22018-01-14 16:52:17 -0500725}
726
Doris Liu766431a2016-02-04 22:17:11 +0000727void SkiaCanvas::drawVectorDrawable(VectorDrawableRoot* vectorDrawable) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800728 vectorDrawable->drawStaging(this);
Doris Liu766431a2016-02-04 22:17:11 +0000729}
730
Derek Sollenberger8872b382014-06-23 14:13:53 -0400731// ----------------------------------------------------------------------------
732// Canvas draw operations: Text
733// ----------------------------------------------------------------------------
734
Mike Reed2dfd55d2019-01-08 16:19:03 -0500735void SkiaCanvas::drawGlyphs(ReadGlyphFunc glyphFunc, int count, const Paint& paint, float x,
Seigo Nonaka63af7ba2020-09-21 23:07:43 -0700736 float y, float totalAdvance) {
Stan Iliev0b58d992017-03-30 18:22:27 -0400737 if (count <= 0 || paint.nothingToDraw()) return;
Mike Reedf6d86ac2019-01-18 14:13:23 -0500738 Paint paintCopy(paint);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400739 if (mPaintFilter) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500740 mPaintFilter->filterFullPaint(&paintCopy);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400741 }
Mike Reedf6d86ac2019-01-18 14:13:23 -0500742 const SkFont& font = paintCopy.getSkFont();
Stan Iliev7717e222018-02-05 18:04:11 -0500743 // Stroke with a hairline is drawn on HW with a fill style for compatibility with Android O and
744 // older.
John Recke170fb62018-05-07 08:12:07 -0700745 if (!mCanvasOwned && sApiLevel <= 27 && paintCopy.getStrokeWidth() <= 0 &&
746 paintCopy.getStyle() == SkPaint::kStroke_Style) {
Stan Iliev7717e222018-02-05 18:04:11 -0500747 paintCopy.setStyle(SkPaint::kFill_Style);
748 }
Stan Ilievf50806a2016-10-24 10:40:39 -0400749
Stan Ilievf50806a2016-10-24 10:40:39 -0400750 SkTextBlobBuilder builder;
Mike Reed2e204fc2019-01-28 13:31:36 -0500751 const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(font, count);
Stan Iliev0b58d992017-03-30 18:22:27 -0400752 glyphFunc(buffer.glyphs, buffer.pos);
Stan Ilievf50806a2016-10-24 10:40:39 -0400753
754 sk_sp<SkTextBlob> textBlob(builder.make());
Nathaniel Nifong52d37772020-01-10 16:12:41 -0500755
Mike Reedbb4cb482021-02-22 14:21:20 -0500756 applyLooper(&paintCopy, [&](const SkPaint& p) { mCanvas->drawTextBlob(textBlob, 0, 0, p); });
Stan Ilievf50806a2016-10-24 10:40:39 -0400757 drawTextDecorations(x, y, totalAdvance, paintCopy);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400758}
759
Yuqian Liafc221492016-07-18 13:07:42 -0400760void SkiaCanvas::drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
Mike Reed2dfd55d2019-01-08 16:19:03 -0500761 const Paint& paint, const SkPath& path, size_t start,
John Reck1bcacfd2017-11-03 10:12:19 -0700762 size_t end) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500763 Paint paintCopy(paint);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400764 if (mPaintFilter) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500765 mPaintFilter->filterFullPaint(&paintCopy);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400766 }
Mike Reedf6d86ac2019-01-18 14:13:23 -0500767 const SkFont& font = paintCopy.getSkFont();
Derek Sollenberger415a74b2018-03-14 15:03:13 -0400768
Yuqian Liafc221492016-07-18 13:07:42 -0400769 const int N = end - start;
Mike Reed4a4b1be2019-01-01 15:43:06 -0500770 SkTextBlobBuilder builder;
771 auto rec = builder.allocRunRSXform(font, N);
772 SkRSXform* xform = (SkRSXform*)rec.pos;
773 uint16_t* glyphs = rec.glyphs;
Yuqian Liafc221492016-07-18 13:07:42 -0400774 SkPathMeasure meas(path, false);
775
776 for (size_t i = start; i < end; i++) {
777 glyphs[i - start] = layout.getGlyphId(i);
Stan Ilievc6c96dd2018-01-10 16:06:04 -0500778 float halfWidth = layout.getCharAdvance(i) * 0.5f;
779 float x = hOffset + layout.getX(i) + halfWidth;
Yuqian Liafc221492016-07-18 13:07:42 -0400780 float y = vOffset + layout.getY(i);
781
782 SkPoint pos;
783 SkVector tan;
784 if (!meas.getPosTan(x, &pos, &tan)) {
785 pos.set(x, y);
786 tan.set(1, 0);
787 }
788 xform[i - start].fSCos = tan.x();
789 xform[i - start].fSSin = tan.y();
Stan Ilievc6c96dd2018-01-10 16:06:04 -0500790 xform[i - start].fTx = pos.x() - tan.y() * y - halfWidth * tan.x();
791 xform[i - start].fTy = pos.y() + tan.x() * y - halfWidth * tan.y();
Yuqian Liafc221492016-07-18 13:07:42 -0400792 }
Leon Scroggins III950f2aa2020-04-29 13:46:00 -0400793
794 sk_sp<SkTextBlob> textBlob(builder.make());
795
Mike Reedbb4cb482021-02-22 14:21:20 -0500796 applyLooper(&paintCopy, [&](const SkPaint& p) { mCanvas->drawTextBlob(textBlob, 0, 0, p); });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400797}
798
Derek Sollenberger6f485562015-07-30 10:00:39 -0400799// ----------------------------------------------------------------------------
800// Canvas draw operations: Animations
801// ----------------------------------------------------------------------------
802
Derek Sollenberger6f485562015-07-30 10:00:39 -0400803void SkiaCanvas::drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
John Reck1bcacfd2017-11-03 10:12:19 -0700804 uirenderer::CanvasPropertyPrimitive* top,
805 uirenderer::CanvasPropertyPrimitive* right,
806 uirenderer::CanvasPropertyPrimitive* bottom,
807 uirenderer::CanvasPropertyPrimitive* rx,
808 uirenderer::CanvasPropertyPrimitive* ry,
809 uirenderer::CanvasPropertyPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -0400810 sk_sp<uirenderer::skiapipeline::AnimatedRoundRect> drawable(
John Reck1bcacfd2017-11-03 10:12:19 -0700811 new uirenderer::skiapipeline::AnimatedRoundRect(left, top, right, bottom, rx, ry,
812 paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400813 mCanvas->drawDrawable(drawable.get());
814}
815
John Reck1bcacfd2017-11-03 10:12:19 -0700816void SkiaCanvas::drawCircle(uirenderer::CanvasPropertyPrimitive* x,
817 uirenderer::CanvasPropertyPrimitive* y,
818 uirenderer::CanvasPropertyPrimitive* radius,
819 uirenderer::CanvasPropertyPaint* paint) {
820 sk_sp<uirenderer::skiapipeline::AnimatedCircle> drawable(
821 new uirenderer::skiapipeline::AnimatedCircle(x, y, radius, paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400822 mCanvas->drawDrawable(drawable.get());
823}
824
Lucas Dupin00af5272021-04-29 20:30:01 -0700825void SkiaCanvas::drawRipple(const uirenderer::skiapipeline::RippleDrawableParams& params) {
826 uirenderer::skiapipeline::AnimatedRippleDrawable::draw(mCanvas, params);
Derek Sollenbergerdf301aa2020-12-17 11:06:03 -0500827}
828
John Reck894e85a2019-07-15 16:16:44 -0700829void SkiaCanvas::drawPicture(const SkPicture& picture) {
830 // TODO: Change to mCanvas->drawPicture()? SkCanvas::drawPicture seems to be
831 // where the logic is for playback vs. ref picture. Using picture.playback here
832 // to stay behavior-identical for now, but should revisit this at some point.
833 picture.playback(mCanvas);
834}
835
Derek Sollenberger6f485562015-07-30 10:00:39 -0400836// ----------------------------------------------------------------------------
837// Canvas draw operations: View System
838// ----------------------------------------------------------------------------
839
Stan Ilievf50806a2016-10-24 10:40:39 -0400840void SkiaCanvas::drawLayer(uirenderer::DeferredLayerUpdater* layerUpdater) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400841 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw Layers");
842}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400843
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400844void SkiaCanvas::drawRenderNode(uirenderer::RenderNode* renderNode) {
845 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw RenderNodes");
846}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400847
John Reck1bcacfd2017-11-03 10:12:19 -0700848} // namespace android