blob: 8fddf713f1fa1a3ad10919a9f79cdd46c0bd931b [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
Ben Wagner60126ef2015-08-07 12:13:48 -040045#include <memory>
Ben Wagner0ed10be2018-06-28 17:08:16 -040046#include <optional>
47#include <utility>
Ben Wagner60126ef2015-08-07 12:13:48 -040048
Derek Sollenberger8872b382014-06-23 14:13:53 -040049namespace android {
50
Stan Ilievf50806a2016-10-24 10:40:39 -040051using uirenderer::PaintUtils;
52
John Reckc1b33d62015-04-22 09:04:45 -070053Canvas* Canvas::create_canvas(const SkBitmap& bitmap) {
Derek Sollenberger8872b382014-06-23 14:13:53 -040054 return new SkiaCanvas(bitmap);
55}
56
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040057Canvas* Canvas::create_canvas(SkCanvas* skiaCanvas) {
58 return new SkiaCanvas(skiaCanvas);
Derek Sollenberger8872b382014-06-23 14:13:53 -040059}
60
Stan Ilievf50806a2016-10-24 10:40:39 -040061SkiaCanvas::SkiaCanvas() {}
62
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040063SkiaCanvas::SkiaCanvas(SkCanvas* canvas) : mCanvas(canvas) {}
Stan Ilievf50806a2016-10-24 10:40:39 -040064
John Reckc1b33d62015-04-22 09:04:45 -070065SkiaCanvas::SkiaCanvas(const SkBitmap& bitmap) {
Derek Sollenbergerd01b5912018-10-19 15:55:33 -040066 mCanvasOwned = std::unique_ptr<SkCanvas>(new SkCanvas(bitmap));
67 mCanvas = mCanvasOwned.get();
Derek Sollenberger8872b382014-06-23 14:13:53 -040068}
69
Stan Iliev021693b2016-10-17 16:26:15 -040070SkiaCanvas::~SkiaCanvas() {}
71
Derek Sollenbergerc1908132016-07-15 10:28:16 -040072void SkiaCanvas::reset(SkCanvas* skiaCanvas) {
Mike Reed6acfe162016-11-18 17:21:09 -050073 if (mCanvas != skiaCanvas) {
74 mCanvas = skiaCanvas;
75 mCanvasOwned.reset();
76 }
Derek Sollenbergerc1908132016-07-15 10:28:16 -040077 mSaveStack.reset(nullptr);
Derek Sollenbergerc1908132016-07-15 10:28:16 -040078}
79
Derek Sollenberger8872b382014-06-23 14:13:53 -040080// ----------------------------------------------------------------------------
81// Canvas state operations: Replace Bitmap
82// ----------------------------------------------------------------------------
83
John Reckc1b33d62015-04-22 09:04:45 -070084void SkiaCanvas::setBitmap(const SkBitmap& bitmap) {
Tony Mantler4f641d12017-03-14 22:36:14 +000085 // deletes the previously owned canvas (if any)
Derek Sollenbergerd01b5912018-10-19 15:55:33 -040086 mCanvasOwned.reset(new SkCanvas(bitmap));
87 mCanvas = mCanvasOwned.get();
Tony Mantler4f641d12017-03-14 22:36:14 +000088
Derek Sollenberger8872b382014-06-23 14:13:53 -040089 // clean up the old save stack
Stan Ilievf50806a2016-10-24 10:40:39 -040090 mSaveStack.reset(nullptr);
Derek Sollenberger8872b382014-06-23 14:13:53 -040091}
92
93// ----------------------------------------------------------------------------
94// Canvas state operations
95// ----------------------------------------------------------------------------
96
97bool SkiaCanvas::isOpaque() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -040098 return mCanvas->imageInfo().isOpaque();
Derek Sollenberger8872b382014-06-23 14:13:53 -040099}
100
101int SkiaCanvas::width() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400102 return mCanvas->imageInfo().width();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400103}
104
105int SkiaCanvas::height() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400106 return mCanvas->imageInfo().height();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400107}
108
109// ----------------------------------------------------------------------------
110// Canvas state operations: Save (layer)
111// ----------------------------------------------------------------------------
112
113int SkiaCanvas::getSaveCount() const {
114 return mCanvas->getSaveCount();
115}
116
Florin Malitaeecff562015-12-21 10:43:01 -0500117int SkiaCanvas::save(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400118 int count = mCanvas->save();
119 recordPartialSave(flags);
120 return count;
121}
122
Florin Malita5e271402015-11-04 14:36:02 -0500123// The SkiaCanvas::restore operation layers on the capability to preserve
124// either (or both) the matrix and/or clip state after a SkCanvas::restore
125// operation. It does this by explicitly saving off the clip & matrix state
126// when requested and playing it back after the SkCanvas::restore.
Derek Sollenberger8872b382014-06-23 14:13:53 -0400127void SkiaCanvas::restore() {
Stan Ilievf50806a2016-10-24 10:40:39 -0400128 const auto* rec = this->currentSaveRec();
129 if (!rec) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400130 // Fast path - no record for this frame.
131 mCanvas->restore();
132 return;
133 }
134
Florin Malitaeecff562015-12-21 10:43:01 -0500135 bool preserveMatrix = !(rec->saveFlags & SaveFlags::Matrix);
John Reck1bcacfd2017-11-03 10:12:19 -0700136 bool preserveClip = !(rec->saveFlags & SaveFlags::Clip);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400137
138 SkMatrix savedMatrix;
139 if (preserveMatrix) {
140 savedMatrix = mCanvas->getTotalMatrix();
141 }
142
Stan Ilievf50806a2016-10-24 10:40:39 -0400143 const size_t clipIndex = rec->clipIndex;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400144
145 mCanvas->restore();
Stan Ilievf50806a2016-10-24 10:40:39 -0400146 mSaveStack->pop_back();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400147
148 if (preserveMatrix) {
149 mCanvas->setMatrix(savedMatrix);
150 }
151
Stan Ilievf50806a2016-10-24 10:40:39 -0400152 if (preserveClip) {
153 this->applyPersistentClips(clipIndex);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400154 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400155}
156
157void SkiaCanvas::restoreToCount(int restoreCount) {
158 while (mCanvas->getSaveCount() > restoreCount) {
159 this->restore();
160 }
161}
162
John Recka00eef212020-11-16 12:45:55 -0500163int SkiaCanvas::saveLayer(float left, float top, float right, float bottom, const SkPaint* paint) {
Florin Malitaeecff562015-12-21 10:43:01 -0500164 const SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
John Recka00eef212020-11-16 12:45:55 -0500165 const SkCanvas::SaveLayerRec rec(&bounds, paint);
Florin Malitaeecff562015-12-21 10:43:01 -0500166
Stan Iliev68885e32016-12-14 11:18:34 -0500167 return mCanvas->saveLayer(rec);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400168}
169
John Recka00eef212020-11-16 12:45:55 -0500170int SkiaCanvas::saveLayerAlpha(float left, float top, float right, float bottom, int alpha) {
Florin Malitaeecff562015-12-21 10:43:01 -0500171 if (static_cast<unsigned>(alpha) < 0xFF) {
Yuqian Lifd92ee42016-04-27 17:03:38 -0400172 SkPaint alphaPaint;
173 alphaPaint.setAlpha(alpha);
John Recka00eef212020-11-16 12:45:55 -0500174 return this->saveLayer(left, top, right, bottom, &alphaPaint);
Florin Malitaeecff562015-12-21 10:43:01 -0500175 }
John Recka00eef212020-11-16 12:45:55 -0500176 return this->saveLayer(left, top, right, bottom, nullptr);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400177}
178
Derek Sollenberger24fc9012018-12-07 14:12:12 -0500179int SkiaCanvas::saveUnclippedLayer(int left, int top, int right, int bottom) {
180 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
181 return SkAndroidFrameworkUtils::SaveBehind(mCanvas, &bounds);
182}
183
Derek Sollenbergerac33a482019-04-22 16:28:09 -0400184void SkiaCanvas::restoreUnclippedLayer(int restoreCount, const SkPaint& paint) {
185
186 while (mCanvas->getSaveCount() > restoreCount + 1) {
187 this->restore();
188 }
189
190 if (mCanvas->getSaveCount() == restoreCount + 1) {
191 SkCanvasPriv::DrawBehind(mCanvas, *filterPaint(paint));
192 this->restore();
193 }
194}
195
Stan Ilievf50806a2016-10-24 10:40:39 -0400196class SkiaCanvas::Clip {
197public:
Mike Reed6e49c9f2016-12-02 15:36:59 -0500198 Clip(const SkRect& rect, SkClipOp op, const SkMatrix& m)
John Reck1bcacfd2017-11-03 10:12:19 -0700199 : mType(Type::Rect), mOp(op), mMatrix(m), mRRect(SkRRect::MakeRect(rect)) {}
Mike Reed6e49c9f2016-12-02 15:36:59 -0500200 Clip(const SkRRect& rrect, SkClipOp op, const SkMatrix& m)
John Reck1bcacfd2017-11-03 10:12:19 -0700201 : mType(Type::RRect), mOp(op), mMatrix(m), mRRect(rrect) {}
Mike Reed6e49c9f2016-12-02 15:36:59 -0500202 Clip(const SkPath& path, SkClipOp op, const SkMatrix& m)
Ben Wagner0ed10be2018-06-28 17:08:16 -0400203 : mType(Type::Path), mOp(op), mMatrix(m), mPath(std::in_place, path) {}
Stan Ilievf50806a2016-10-24 10:40:39 -0400204
205 void apply(SkCanvas* canvas) const {
206 canvas->setMatrix(mMatrix);
207 switch (mType) {
John Reck1bcacfd2017-11-03 10:12:19 -0700208 case Type::Rect:
Nader Jawade431e312019-12-04 14:13:18 -0800209 // Don't anti-alias rectangular clips
210 canvas->clipRect(mRRect.rect(), mOp, false);
John Reck1bcacfd2017-11-03 10:12:19 -0700211 break;
212 case Type::RRect:
Nader Jawade431e312019-12-04 14:13:18 -0800213 // Ensure rounded rectangular clips are anti-aliased
214 canvas->clipRRect(mRRect, mOp, true);
John Reck1bcacfd2017-11-03 10:12:19 -0700215 break;
216 case Type::Path:
Nader Jawade431e312019-12-04 14:13:18 -0800217 // Ensure path clips are anti-aliased
218 canvas->clipPath(mPath.value(), mOp, true);
John Reck1bcacfd2017-11-03 10:12:19 -0700219 break;
Stan Ilievf50806a2016-10-24 10:40:39 -0400220 }
221 }
222
223private:
224 enum class Type {
225 Rect,
226 RRect,
227 Path,
228 };
229
John Reck1bcacfd2017-11-03 10:12:19 -0700230 Type mType;
231 SkClipOp mOp;
232 SkMatrix mMatrix;
Stan Ilievf50806a2016-10-24 10:40:39 -0400233
234 // These are logically a union (tracked separately due to non-POD path).
Ben Wagner0ed10be2018-06-28 17:08:16 -0400235 std::optional<SkPath> mPath;
John Reck1bcacfd2017-11-03 10:12:19 -0700236 SkRRect mRRect;
Stan Ilievf50806a2016-10-24 10:40:39 -0400237};
238
239const SkiaCanvas::SaveRec* SkiaCanvas::currentSaveRec() const {
John Reck1bcacfd2017-11-03 10:12:19 -0700240 const SaveRec* rec = mSaveStack ? static_cast<const SaveRec*>(mSaveStack->back()) : nullptr;
Stan Ilievf50806a2016-10-24 10:40:39 -0400241 int currentSaveCount = mCanvas->getSaveCount();
242 SkASSERT(!rec || currentSaveCount >= rec->saveCount);
243
244 return (rec && rec->saveCount == currentSaveCount) ? rec : nullptr;
245}
246
Derek Sollenberger8872b382014-06-23 14:13:53 -0400247// ----------------------------------------------------------------------------
248// functions to emulate legacy SaveFlags (i.e. independent matrix/clip flags)
249// ----------------------------------------------------------------------------
250
Florin Malitaeecff562015-12-21 10:43:01 -0500251void SkiaCanvas::recordPartialSave(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400252 // A partial save is a save operation which doesn't capture the full canvas state.
Florin Malitaeecff562015-12-21 10:43:01 -0500253 // (either SaveFlags::Matrix or SaveFlags::Clip is missing).
Derek Sollenberger8872b382014-06-23 14:13:53 -0400254
255 // Mask-out non canvas state bits.
Florin Malitaeecff562015-12-21 10:43:01 -0500256 flags &= SaveFlags::MatrixClip;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400257
Florin Malitaeecff562015-12-21 10:43:01 -0500258 if (flags == SaveFlags::MatrixClip) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400259 // not a partial save.
260 return;
261 }
262
Stan Ilievf50806a2016-10-24 10:40:39 -0400263 if (!mSaveStack) {
Ben Wagnerd1cbc162015-08-19 12:45:09 -0400264 mSaveStack.reset(new SkDeque(sizeof(struct SaveRec), 8));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400265 }
266
267 SaveRec* rec = static_cast<SaveRec*>(mSaveStack->push_back());
Florin Malita5e271402015-11-04 14:36:02 -0500268 rec->saveCount = mCanvas->getSaveCount();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400269 rec->saveFlags = flags;
Stan Ilievf50806a2016-10-24 10:40:39 -0400270 rec->clipIndex = mClipStack.size();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400271}
272
Stan Ilievf50806a2016-10-24 10:40:39 -0400273template <typename T>
Mike Reed6e49c9f2016-12-02 15:36:59 -0500274void SkiaCanvas::recordClip(const T& clip, SkClipOp op) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400275 // Only need tracking when in a partial save frame which
276 // doesn't restore the clip.
277 const SaveRec* rec = this->currentSaveRec();
278 if (rec && !(rec->saveFlags & SaveFlags::Clip)) {
279 mClipStack.emplace_back(clip, op, mCanvas->getTotalMatrix());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400280 }
281}
282
Stan Ilievf50806a2016-10-24 10:40:39 -0400283// Applies and optionally removes all clips >= index.
284void SkiaCanvas::applyPersistentClips(size_t clipStartIndex) {
285 SkASSERT(clipStartIndex <= mClipStack.size());
286 const auto begin = mClipStack.cbegin() + clipStartIndex;
287 const auto end = mClipStack.cend();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400288
Stan Ilievf50806a2016-10-24 10:40:39 -0400289 // Clip application mutates the CTM.
290 const SkMatrix saveMatrix = mCanvas->getTotalMatrix();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400291
Stan Ilievf50806a2016-10-24 10:40:39 -0400292 for (auto clip = begin; clip != end; ++clip) {
Mike Reed6acfe162016-11-18 17:21:09 -0500293 clip->apply(mCanvas);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400294 }
295
Stan Ilievf50806a2016-10-24 10:40:39 -0400296 mCanvas->setMatrix(saveMatrix);
297
298 // If the current/post-restore save rec is also persisting clips, we
299 // leave them on the stack to be reapplied part of the next restore().
300 // Otherwise we're done and just pop them.
301 const auto* rec = this->currentSaveRec();
302 if (!rec || (rec->saveFlags & SaveFlags::Clip)) {
303 mClipStack.erase(begin, end);
304 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400305}
306
307// ----------------------------------------------------------------------------
308// Canvas state operations: Matrix
309// ----------------------------------------------------------------------------
310
311void SkiaCanvas::getMatrix(SkMatrix* outMatrix) const {
312 *outMatrix = mCanvas->getTotalMatrix();
313}
314
315void SkiaCanvas::setMatrix(const SkMatrix& matrix) {
316 mCanvas->setMatrix(matrix);
317}
318
319void SkiaCanvas::concat(const SkMatrix& matrix) {
320 mCanvas->concat(matrix);
321}
322
323void SkiaCanvas::rotate(float degrees) {
324 mCanvas->rotate(degrees);
325}
326
327void SkiaCanvas::scale(float sx, float sy) {
328 mCanvas->scale(sx, sy);
329}
330
331void SkiaCanvas::skew(float sx, float sy) {
332 mCanvas->skew(sx, sy);
333}
334
335void SkiaCanvas::translate(float dx, float dy) {
336 mCanvas->translate(dx, dy);
337}
338
339// ----------------------------------------------------------------------------
340// Canvas state operations: Clips
341// ----------------------------------------------------------------------------
342
343// This function is a mirror of SkCanvas::getClipBounds except that it does
344// not outset the edge of the clip to account for anti-aliasing. There is
345// a skia bug to investigate pushing this logic into back into skia.
346// (see https://code.google.com/p/skia/issues/detail?id=1303)
347bool SkiaCanvas::getClipBounds(SkRect* outRect) const {
348 SkIRect ibounds;
Mike Reed5e438982017-01-25 08:23:25 -0500349 if (!mCanvas->getDeviceClipBounds(&ibounds)) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400350 return false;
351 }
352
353 SkMatrix inverse;
354 // if we can't invert the CTM, we can't return local clip bounds
355 if (!mCanvas->getTotalMatrix().invert(&inverse)) {
356 if (outRect) {
357 outRect->setEmpty();
358 }
359 return false;
360 }
361
362 if (NULL != outRect) {
363 SkRect r = SkRect::Make(ibounds);
364 inverse.mapRect(outRect, r);
365 }
366 return true;
367}
368
369bool SkiaCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
370 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
371 return mCanvas->quickReject(bounds);
372}
373
374bool SkiaCanvas::quickRejectPath(const SkPath& path) const {
375 return mCanvas->quickReject(path);
376}
377
Mike Reed6e49c9f2016-12-02 15:36:59 -0500378bool SkiaCanvas::clipRect(float left, float top, float right, float bottom, SkClipOp op) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400379 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Stan Ilievf50806a2016-10-24 10:40:39 -0400380 this->recordClip(rect, op);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400381 mCanvas->clipRect(rect, op);
Chris Craik5ec6a282015-06-23 15:42:12 -0700382 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400383}
384
Mike Reed6e49c9f2016-12-02 15:36:59 -0500385bool SkiaCanvas::clipPath(const SkPath* path, SkClipOp op) {
Derek Sollenbergerf7d98f42017-04-17 11:27:36 -0400386 this->recordClip(*path, op);
Nader Jawade431e312019-12-04 14:13:18 -0800387 mCanvas->clipPath(*path, op, true);
Chris Craik5ec6a282015-06-23 15:42:12 -0700388 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400389}
390
Derek Sollenberger8872b382014-06-23 14:13:53 -0400391// ----------------------------------------------------------------------------
392// Canvas state operations: Filters
393// ----------------------------------------------------------------------------
394
Ben Wagner0ed10be2018-06-28 17:08:16 -0400395PaintFilter* SkiaCanvas::getPaintFilter() {
396 return mPaintFilter.get();
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400397}
398
Ben Wagner0ed10be2018-06-28 17:08:16 -0400399void SkiaCanvas::setPaintFilter(sk_sp<PaintFilter> paintFilter) {
400 mPaintFilter = std::move(paintFilter);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400401}
402
403// ----------------------------------------------------------------------------
Matt Sarettd0814db2017-04-13 09:33:18 -0400404// Canvas state operations: Capture
405// ----------------------------------------------------------------------------
406
407SkCanvasState* SkiaCanvas::captureCanvasState() const {
408 SkCanvas* canvas = mCanvas;
409 if (mCanvasOwned) {
410 // Important to use the underlying SkCanvas, not the wrapper.
411 canvas = mCanvasOwned.get();
412 }
413
414 // Workarounds for http://crbug.com/271096: SW draw only supports
415 // translate & scale transforms, and a simple rectangular clip.
416 // (This also avoids significant wasted time in calling
417 // SkCanvasStateUtils::CaptureCanvasState when the clip is complex).
John Reck1bcacfd2017-11-03 10:12:19 -0700418 if (!canvas->isClipRect() || (canvas->getTotalMatrix().getType() &
419 ~(SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask))) {
420 return nullptr;
Matt Sarettd0814db2017-04-13 09:33:18 -0400421 }
422
423 return SkCanvasStateUtils::CaptureCanvasState(canvas);
424}
425
426// ----------------------------------------------------------------------------
Derek Sollenberger8872b382014-06-23 14:13:53 -0400427// Canvas draw operations
428// ----------------------------------------------------------------------------
429
Mike Reed260ab722016-10-07 15:59:20 -0400430void SkiaCanvas::drawColor(int color, SkBlendMode mode) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400431 mCanvas->drawColor(color, mode);
432}
433
Ben Wagner0ed10be2018-06-28 17:08:16 -0400434SkiaCanvas::PaintCoW&& SkiaCanvas::filterPaint(PaintCoW&& paint) const {
435 if (mPaintFilter) {
436 mPaintFilter->filter(&paint.writeable());
437 }
438 return std::move(paint);
439}
440
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400441void SkiaCanvas::drawPaint(const SkPaint& paint) {
Ben Wagner0ed10be2018-06-28 17:08:16 -0400442 mCanvas->drawPaint(*filterPaint(paint));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400443}
444
445// ----------------------------------------------------------------------------
446// Canvas draw operations: Geometry
447// ----------------------------------------------------------------------------
448
Mike Reedc2dbc032019-07-25 12:28:29 -0400449void SkiaCanvas::drawPoints(const float* points, int count, const Paint& paint,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400450 SkCanvas::PointMode mode) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500451 if (CC_UNLIKELY(count < 2 || paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400452 // convert the floats into SkPoints
John Reck1bcacfd2017-11-03 10:12:19 -0700453 count >>= 1; // now it is the number of points
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400454 std::unique_ptr<SkPoint[]> pts(new SkPoint[count]);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400455 for (int i = 0; i < count; i++) {
456 pts[i].set(points[0], points[1]);
457 points += 2;
458 }
Mike Reedc2dbc032019-07-25 12:28:29 -0400459
460 apply_looper(&paint, [&](const SkPaint& p) {
461 mCanvas->drawPoints(mode, count, pts.get(), p);
462 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400463}
464
Mike Reedc2dbc032019-07-25 12:28:29 -0400465void SkiaCanvas::drawPoint(float x, float y, const Paint& paint) {
466 apply_looper(&paint, [&](const SkPaint& p) {
467 mCanvas->drawPoint(x, y, p);
468 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400469}
470
Mike Reedc2dbc032019-07-25 12:28:29 -0400471void SkiaCanvas::drawPoints(const float* points, int count, const Paint& paint) {
472 this->drawPoints(points, count, paint, SkCanvas::kPoints_PointMode);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400473}
474
475void SkiaCanvas::drawLine(float startX, float startY, float stopX, float stopY,
Mike Reedc2dbc032019-07-25 12:28:29 -0400476 const Paint& paint) {
477 apply_looper(&paint, [&](const SkPaint& p) {
478 mCanvas->drawLine(startX, startY, stopX, stopY, p);
479 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400480}
481
Mike Reedc2dbc032019-07-25 12:28:29 -0400482void SkiaCanvas::drawLines(const float* points, int count, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500483 if (CC_UNLIKELY(count < 4 || paint.nothingToDraw())) return;
Mike Reedc2dbc032019-07-25 12:28:29 -0400484 this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400485}
486
Mike Reedc2dbc032019-07-25 12:28:29 -0400487void SkiaCanvas::drawRect(float left, float top, float right, float bottom, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500488 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Mike Reedc2dbc032019-07-25 12:28:29 -0400489 apply_looper(&paint, [&](const SkPaint& p) {
490 mCanvas->drawRect({left, top, right, bottom}, p);
491 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400492}
493
Mike Reedc2dbc032019-07-25 12:28:29 -0400494void SkiaCanvas::drawRegion(const SkRegion& region, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500495 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Mike Reedc2dbc032019-07-25 12:28:29 -0400496 apply_looper(&paint, [&](const SkPaint& p) {
497 mCanvas->drawRegion(region, p);
498 });
Derek Sollenberger94394b32015-07-10 09:58:41 -0400499}
500
John Reck1bcacfd2017-11-03 10:12:19 -0700501void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom, float rx, float ry,
Mike Reedc2dbc032019-07-25 12:28:29 -0400502 const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500503 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400504 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Mike Reedc2dbc032019-07-25 12:28:29 -0400505 apply_looper(&paint, [&](const SkPaint& p) {
506 mCanvas->drawRoundRect(rect, rx, ry, p);
507 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400508}
509
Nader Jawadadfe1d92018-09-27 12:27:36 -0700510void SkiaCanvas::drawDoubleRoundRect(const SkRRect& outer, const SkRRect& inner,
Mike Reedc2dbc032019-07-25 12:28:29 -0400511 const Paint& paint) {
512 apply_looper(&paint, [&](const SkPaint& p) {
513 mCanvas->drawDRRect(outer, inner, p);
514 });
Nader Jawadadfe1d92018-09-27 12:27:36 -0700515}
516
Mike Reedc2dbc032019-07-25 12:28:29 -0400517void SkiaCanvas::drawCircle(float x, float y, float radius, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500518 if (CC_UNLIKELY(radius <= 0 || paint.nothingToDraw())) return;
Mike Reedc2dbc032019-07-25 12:28:29 -0400519 apply_looper(&paint, [&](const SkPaint& p) {
520 mCanvas->drawCircle(x, y, radius, p);
521 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400522}
523
Mike Reedc2dbc032019-07-25 12:28:29 -0400524void SkiaCanvas::drawOval(float left, float top, float right, float bottom, 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 oval = SkRect::MakeLTRB(left, top, right, bottom);
Mike Reedc2dbc032019-07-25 12:28:29 -0400527 apply_looper(&paint, [&](const SkPaint& p) {
528 mCanvas->drawOval(oval, p);
529 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400530}
531
John Reck1bcacfd2017-11-03 10:12:19 -0700532void SkiaCanvas::drawArc(float left, float top, float right, float bottom, float startAngle,
Mike Reedc2dbc032019-07-25 12:28:29 -0400533 float sweepAngle, bool useCenter, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500534 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400535 SkRect arc = SkRect::MakeLTRB(left, top, right, bottom);
Mike Reedc2dbc032019-07-25 12:28:29 -0400536 apply_looper(&paint, [&](const SkPaint& p) {
537 if (fabs(sweepAngle) >= 360.0f) {
538 mCanvas->drawOval(arc, p);
539 } else {
540 mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, p);
541 }
542 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400543}
544
Mike Reedc2dbc032019-07-25 12:28:29 -0400545void SkiaCanvas::drawPath(const SkPath& path, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500546 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Stan Iliev6dcfdec2017-08-15 16:42:05 -0400547 if (CC_UNLIKELY(path.isEmpty() && (!path.isInverseFillType()))) {
548 return;
549 }
Mike Reedc2dbc032019-07-25 12:28:29 -0400550 apply_looper(&paint, [&](const SkPaint& p) {
551 mCanvas->drawPath(path, p);
552 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400553}
554
Mike Reedc2dbc032019-07-25 12:28:29 -0400555void SkiaCanvas::drawVertices(const SkVertices* vertices, SkBlendMode mode, const Paint& paint) {
556 apply_looper(&paint, [&](const SkPaint& p) {
557 mCanvas->drawVertices(vertices, mode, p);
558 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400559}
560
561// ----------------------------------------------------------------------------
562// Canvas draw operations: Bitmaps
563// ----------------------------------------------------------------------------
564
Mike Reedc2dbc032019-07-25 12:28:29 -0400565void SkiaCanvas::drawBitmap(Bitmap& bitmap, float left, float top, const Paint* paint) {
566 auto image = bitmap.makeImage();
567 apply_looper(paint, [&](const SkPaint& p) {
Mike Reed7994a312021-01-28 18:06:26 -0500568 auto sampling = SkSamplingOptions(p.getFilterQuality());
569 mCanvas->drawImage(image, left, top, sampling, &p);
Mike Reedc2dbc032019-07-25 12:28:29 -0400570 });
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400571}
572
Mike Reedc2dbc032019-07-25 12:28:29 -0400573void SkiaCanvas::drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const Paint* paint) {
574 auto image = bitmap.makeImage();
Mike Reed6acfe162016-11-18 17:21:09 -0500575 SkAutoCanvasRestore acr(mCanvas, true);
Mike Reed70ffbf92014-12-08 17:03:30 -0500576 mCanvas->concat(matrix);
Mike Reedc2dbc032019-07-25 12:28:29 -0400577 apply_looper(paint, [&](const SkPaint& p) {
Mike Reed7994a312021-01-28 18:06:26 -0500578 auto sampling = SkSamplingOptions(p.getFilterQuality());
579 mCanvas->drawImage(image, 0, 0, sampling, &p);
Mike Reedc2dbc032019-07-25 12:28:29 -0400580 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400581}
582
John Reck1bcacfd2017-11-03 10:12:19 -0700583void SkiaCanvas::drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight,
584 float srcBottom, float dstLeft, float dstTop, float dstRight,
Mike Reedc2dbc032019-07-25 12:28:29 -0400585 float dstBottom, const Paint* paint) {
586 auto image = bitmap.makeImage();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400587 SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
588 SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400589
Mike Reedc2dbc032019-07-25 12:28:29 -0400590 apply_looper(paint, [&](const SkPaint& p) {
Mike Reed7994a312021-01-28 18:06:26 -0500591 auto sampling = SkSamplingOptions(p.getFilterQuality());
592 mCanvas->drawImageRect(image, srcRect, dstRect, sampling, &p,
593 SkCanvas::kFast_SrcRectConstraint);
Mike Reedc2dbc032019-07-25 12:28:29 -0400594 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400595}
596
Mike Reed7994a312021-01-28 18:06:26 -0500597static SkFilterMode paintToFilter(const Paint* paint) {
598 return paint && paint->isFilterBitmap() ? SkFilterMode::kLinear
599 : SkFilterMode::kNearest;
600}
601
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400602void SkiaCanvas::drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight,
Mike Reedc2dbc032019-07-25 12:28:29 -0400603 const float* vertices, const int* colors, const Paint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400604 const int ptCount = (meshWidth + 1) * (meshHeight + 1);
605 const int indexCount = meshWidth * meshHeight * 6;
Mike Reed871cd2d2017-03-17 10:15:52 -0400606 uint32_t flags = SkVertices::kHasTexCoords_BuilderFlag;
607 if (colors) {
608 flags |= SkVertices::kHasColors_BuilderFlag;
609 }
Mike Reed826deef2017-04-04 15:32:04 -0400610 SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, ptCount, indexCount, flags);
Mike Reed871cd2d2017-03-17 10:15:52 -0400611 memcpy(builder.positions(), vertices, ptCount * sizeof(SkPoint));
612 if (colors) {
613 memcpy(builder.colors(), colors, ptCount * sizeof(SkColor));
614 }
615 SkPoint* texs = builder.texCoords();
616 uint16_t* indices = builder.indices();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400617
618 // cons up texture coordinates and indices
619 {
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400620 const SkScalar w = SkIntToScalar(bitmap.width());
621 const SkScalar h = SkIntToScalar(bitmap.height());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400622 const SkScalar dx = w / meshWidth;
623 const SkScalar dy = h / meshHeight;
624
625 SkPoint* texsPtr = texs;
626 SkScalar y = 0;
627 for (int i = 0; i <= meshHeight; i++) {
628 if (i == meshHeight) {
629 y = h; // to ensure numerically we hit h exactly
630 }
631 SkScalar x = 0;
632 for (int j = 0; j < meshWidth; j++) {
633 texsPtr->set(x, y);
634 texsPtr += 1;
635 x += dx;
636 }
637 texsPtr->set(w, y);
638 texsPtr += 1;
639 y += dy;
640 }
641 SkASSERT(texsPtr - texs == ptCount);
642 }
643
644 // cons up indices
645 {
646 uint16_t* indexPtr = indices;
647 int index = 0;
648 for (int i = 0; i < meshHeight; i++) {
649 for (int j = 0; j < meshWidth; j++) {
650 // lower-left triangle
651 *indexPtr++ = index;
652 *indexPtr++ = index + meshWidth + 1;
653 *indexPtr++ = index + meshWidth + 2;
654 // upper-right triangle
655 *indexPtr++ = index;
656 *indexPtr++ = index + meshWidth + 2;
657 *indexPtr++ = index + 1;
658 // bump to the next cell
659 index += 1;
660 }
661 // bump to the next row
662 index += 1;
663 }
664 SkASSERT(indexPtr - indices == indexCount);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400665 }
666
John Reck1bcacfd2017-11-03 10:12:19 -0700667// double-check that we have legal indices
Derek Sollenberger8872b382014-06-23 14:13:53 -0400668#ifdef SK_DEBUG
669 {
670 for (int i = 0; i < indexCount; i++) {
671 SkASSERT((unsigned)indices[i] < (unsigned)ptCount);
672 }
673 }
674#endif
675
Mike Reed7994a312021-01-28 18:06:26 -0500676 auto image = bitmap.makeImage();
677
Derek Sollenberger8872b382014-06-23 14:13:53 -0400678 // cons-up a shader for the bitmap
Mike Reedc2dbc032019-07-25 12:28:29 -0400679 Paint pnt;
680 if (paint) {
681 pnt = *paint;
682 }
Mike Reed7994a312021-01-28 18:06:26 -0500683 SkSamplingOptions sampling(paintToFilter(&pnt));
684 pnt.setShader(image->makeShader(sampling));
685
Mike Reedc2dbc032019-07-25 12:28:29 -0400686 auto v = builder.detach();
687 apply_looper(&pnt, [&](const SkPaint& p) {
Mike Reed7994a312021-01-28 18:06:26 -0500688 SkPaint copy(p);
689 auto s = SkSamplingOptions(p.getFilterQuality());
690 if (s != sampling) {
691 // apply_looper changed the quality?
692 copy.setShader(image->makeShader(s));
693 }
694 mCanvas->drawVertices(v, SkBlendMode::kModulate, copy);
Mike Reedc2dbc032019-07-25 12:28:29 -0400695 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400696}
697
John Reck1bcacfd2017-11-03 10:12:19 -0700698void SkiaCanvas::drawNinePatch(Bitmap& bitmap, const Res_png_9patch& chunk, float dstLeft,
699 float dstTop, float dstRight, float dstBottom,
Mike Reedc2dbc032019-07-25 12:28:29 -0400700 const Paint* paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400701 SkCanvas::Lattice lattice;
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400702 NinePatchUtils::SetLatticeDivs(&lattice, chunk, bitmap.width(), bitmap.height());
Stan Ilievf50806a2016-10-24 10:40:39 -0400703
Stan Ilieve12d7312017-12-04 14:48:27 -0500704 lattice.fRectTypes = nullptr;
705 lattice.fColors = nullptr;
Stan Ilievf50806a2016-10-24 10:40:39 -0400706 int numFlags = 0;
Stan Iliev021693b2016-10-17 16:26:15 -0400707 if (chunk.numColors > 0 && chunk.numColors == NinePatchUtils::NumDistinctRects(lattice)) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400708 // We can expect the framework to give us a color for every distinct rect.
709 // Skia requires a flag for every rect.
710 numFlags = (lattice.fXCount + 1) * (lattice.fYCount + 1);
711 }
712
Stan Ilieve12d7312017-12-04 14:48:27 -0500713 SkAutoSTMalloc<25, SkCanvas::Lattice::RectType> flags(numFlags);
714 SkAutoSTMalloc<25, SkColor> colors(numFlags);
Stan Ilievf50806a2016-10-24 10:40:39 -0400715 if (numFlags > 0) {
Stan Ilieve12d7312017-12-04 14:48:27 -0500716 NinePatchUtils::SetLatticeFlags(&lattice, flags.get(), numFlags, chunk, colors.get());
Stan Ilievf50806a2016-10-24 10:40:39 -0400717 }
718
719 lattice.fBounds = nullptr;
720 SkRect dst = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Mike Reedc2dbc032019-07-25 12:28:29 -0400721 auto image = bitmap.makeImage();
722 apply_looper(paint, [&](const SkPaint& p) {
Mike Reed7994a312021-01-28 18:06:26 -0500723 auto filter = SkSamplingOptions(p.getFilterQuality()).filter;
Mike Reed1b3dcfc2021-01-26 20:31:20 -0500724 mCanvas->drawImageLattice(image.get(), lattice, dst, filter, &p);
Mike Reedc2dbc032019-07-25 12:28:29 -0400725 });
Derek Sollenbergeredca3202015-07-10 13:56:39 -0400726}
727
Derek Sollenberger2d142132018-01-22 10:25:26 -0500728double SkiaCanvas::drawAnimatedImage(AnimatedImageDrawable* imgDrawable) {
729 return imgDrawable->drawStaging(mCanvas);
Leon Scroggins III671cce22018-01-14 16:52:17 -0500730}
731
Doris Liu766431a2016-02-04 22:17:11 +0000732void SkiaCanvas::drawVectorDrawable(VectorDrawableRoot* vectorDrawable) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800733 vectorDrawable->drawStaging(this);
Doris Liu766431a2016-02-04 22:17:11 +0000734}
735
Derek Sollenberger8872b382014-06-23 14:13:53 -0400736// ----------------------------------------------------------------------------
737// Canvas draw operations: Text
738// ----------------------------------------------------------------------------
739
Mike Reed2dfd55d2019-01-08 16:19:03 -0500740void SkiaCanvas::drawGlyphs(ReadGlyphFunc glyphFunc, int count, const Paint& paint, float x,
Seigo Nonaka63af7ba2020-09-21 23:07:43 -0700741 float y, float totalAdvance) {
Stan Iliev0b58d992017-03-30 18:22:27 -0400742 if (count <= 0 || paint.nothingToDraw()) return;
Mike Reedf6d86ac2019-01-18 14:13:23 -0500743 Paint paintCopy(paint);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400744 if (mPaintFilter) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500745 mPaintFilter->filterFullPaint(&paintCopy);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400746 }
Mike Reedf6d86ac2019-01-18 14:13:23 -0500747 const SkFont& font = paintCopy.getSkFont();
Stan Iliev7717e222018-02-05 18:04:11 -0500748 // Stroke with a hairline is drawn on HW with a fill style for compatibility with Android O and
749 // older.
John Recke170fb62018-05-07 08:12:07 -0700750 if (!mCanvasOwned && sApiLevel <= 27 && paintCopy.getStrokeWidth() <= 0 &&
751 paintCopy.getStyle() == SkPaint::kStroke_Style) {
Stan Iliev7717e222018-02-05 18:04:11 -0500752 paintCopy.setStyle(SkPaint::kFill_Style);
753 }
Stan Ilievf50806a2016-10-24 10:40:39 -0400754
Stan Ilievf50806a2016-10-24 10:40:39 -0400755 SkTextBlobBuilder builder;
Mike Reed2e204fc2019-01-28 13:31:36 -0500756 const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(font, count);
Stan Iliev0b58d992017-03-30 18:22:27 -0400757 glyphFunc(buffer.glyphs, buffer.pos);
Stan Ilievf50806a2016-10-24 10:40:39 -0400758
759 sk_sp<SkTextBlob> textBlob(builder.make());
Nathaniel Nifong52d37772020-01-10 16:12:41 -0500760
761 apply_looper(&paintCopy, [&](const SkPaint& p) {
762 mCanvas->drawTextBlob(textBlob, 0, 0, p);
763 });
Stan Ilievf50806a2016-10-24 10:40:39 -0400764 drawTextDecorations(x, y, totalAdvance, paintCopy);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400765}
766
Yuqian Liafc221492016-07-18 13:07:42 -0400767void SkiaCanvas::drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
Mike Reed2dfd55d2019-01-08 16:19:03 -0500768 const Paint& paint, const SkPath& path, size_t start,
John Reck1bcacfd2017-11-03 10:12:19 -0700769 size_t end) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500770 Paint paintCopy(paint);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400771 if (mPaintFilter) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500772 mPaintFilter->filterFullPaint(&paintCopy);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400773 }
Mike Reedf6d86ac2019-01-18 14:13:23 -0500774 const SkFont& font = paintCopy.getSkFont();
Derek Sollenberger415a74b2018-03-14 15:03:13 -0400775
Yuqian Liafc221492016-07-18 13:07:42 -0400776 const int N = end - start;
Mike Reed4a4b1be2019-01-01 15:43:06 -0500777 SkTextBlobBuilder builder;
778 auto rec = builder.allocRunRSXform(font, N);
779 SkRSXform* xform = (SkRSXform*)rec.pos;
780 uint16_t* glyphs = rec.glyphs;
Yuqian Liafc221492016-07-18 13:07:42 -0400781 SkPathMeasure meas(path, false);
782
783 for (size_t i = start; i < end; i++) {
784 glyphs[i - start] = layout.getGlyphId(i);
Stan Ilievc6c96dd2018-01-10 16:06:04 -0500785 float halfWidth = layout.getCharAdvance(i) * 0.5f;
786 float x = hOffset + layout.getX(i) + halfWidth;
Yuqian Liafc221492016-07-18 13:07:42 -0400787 float y = vOffset + layout.getY(i);
788
789 SkPoint pos;
790 SkVector tan;
791 if (!meas.getPosTan(x, &pos, &tan)) {
792 pos.set(x, y);
793 tan.set(1, 0);
794 }
795 xform[i - start].fSCos = tan.x();
796 xform[i - start].fSSin = tan.y();
Stan Ilievc6c96dd2018-01-10 16:06:04 -0500797 xform[i - start].fTx = pos.x() - tan.y() * y - halfWidth * tan.x();
798 xform[i - start].fTy = pos.y() + tan.x() * y - halfWidth * tan.y();
Yuqian Liafc221492016-07-18 13:07:42 -0400799 }
Leon Scroggins III950f2aa2020-04-29 13:46:00 -0400800
801 sk_sp<SkTextBlob> textBlob(builder.make());
802
Nathaniel Nifong52d37772020-01-10 16:12:41 -0500803 apply_looper(&paintCopy, [&](const SkPaint& p) {
Leon Scroggins III950f2aa2020-04-29 13:46:00 -0400804 mCanvas->drawTextBlob(textBlob, 0, 0, p);
Nathaniel Nifong52d37772020-01-10 16:12:41 -0500805 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400806}
807
Derek Sollenberger6f485562015-07-30 10:00:39 -0400808// ----------------------------------------------------------------------------
809// Canvas draw operations: Animations
810// ----------------------------------------------------------------------------
811
Derek Sollenberger6f485562015-07-30 10:00:39 -0400812void SkiaCanvas::drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
John Reck1bcacfd2017-11-03 10:12:19 -0700813 uirenderer::CanvasPropertyPrimitive* top,
814 uirenderer::CanvasPropertyPrimitive* right,
815 uirenderer::CanvasPropertyPrimitive* bottom,
816 uirenderer::CanvasPropertyPrimitive* rx,
817 uirenderer::CanvasPropertyPrimitive* ry,
818 uirenderer::CanvasPropertyPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -0400819 sk_sp<uirenderer::skiapipeline::AnimatedRoundRect> drawable(
John Reck1bcacfd2017-11-03 10:12:19 -0700820 new uirenderer::skiapipeline::AnimatedRoundRect(left, top, right, bottom, rx, ry,
821 paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400822 mCanvas->drawDrawable(drawable.get());
823}
824
John Reck1bcacfd2017-11-03 10:12:19 -0700825void SkiaCanvas::drawCircle(uirenderer::CanvasPropertyPrimitive* x,
826 uirenderer::CanvasPropertyPrimitive* y,
827 uirenderer::CanvasPropertyPrimitive* radius,
828 uirenderer::CanvasPropertyPaint* paint) {
829 sk_sp<uirenderer::skiapipeline::AnimatedCircle> drawable(
830 new uirenderer::skiapipeline::AnimatedCircle(x, y, radius, paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400831 mCanvas->drawDrawable(drawable.get());
832}
833
Derek Sollenbergerdf301aa2020-12-17 11:06:03 -0500834void SkiaCanvas::drawRipple(uirenderer::CanvasPropertyPrimitive* x,
835 uirenderer::CanvasPropertyPrimitive* y,
836 uirenderer::CanvasPropertyPrimitive* radius,
837 uirenderer::CanvasPropertyPaint* paint,
838 uirenderer::CanvasPropertyPrimitive* progress,
Derek Sollenberger783e5ae2021-01-11 15:44:46 -0500839 const SkRuntimeShaderBuilder& effectBuilder) {
Derek Sollenbergerdf301aa2020-12-17 11:06:03 -0500840 sk_sp<uirenderer::skiapipeline::AnimatedRipple> drawable(
841 new uirenderer::skiapipeline::AnimatedRipple(x, y, radius, paint, progress,
Derek Sollenberger783e5ae2021-01-11 15:44:46 -0500842 effectBuilder));
Derek Sollenbergerdf301aa2020-12-17 11:06:03 -0500843 mCanvas->drawDrawable(drawable.get());
844}
845
John Reck894e85a2019-07-15 16:16:44 -0700846void SkiaCanvas::drawPicture(const SkPicture& picture) {
847 // TODO: Change to mCanvas->drawPicture()? SkCanvas::drawPicture seems to be
848 // where the logic is for playback vs. ref picture. Using picture.playback here
849 // to stay behavior-identical for now, but should revisit this at some point.
850 picture.playback(mCanvas);
851}
852
Derek Sollenberger6f485562015-07-30 10:00:39 -0400853// ----------------------------------------------------------------------------
854// Canvas draw operations: View System
855// ----------------------------------------------------------------------------
856
Stan Ilievf50806a2016-10-24 10:40:39 -0400857void SkiaCanvas::drawLayer(uirenderer::DeferredLayerUpdater* layerUpdater) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400858 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw Layers");
859}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400860
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400861void SkiaCanvas::drawRenderNode(uirenderer::RenderNode* renderNode) {
862 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw RenderNodes");
863}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400864
John Reck1bcacfd2017-11-03 10:12:19 -0700865} // namespace android