blob: 12681ae221d5186645d271106624f5935844e0c2 [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>
Derek Sollenberger8872b382014-06-23 14:13:53 -040043
Ben Wagner60126ef2015-08-07 12:13:48 -040044#include <memory>
Ben Wagner0ed10be2018-06-28 17:08:16 -040045#include <optional>
46#include <utility>
Ben Wagner60126ef2015-08-07 12:13:48 -040047
Derek Sollenberger8872b382014-06-23 14:13:53 -040048namespace android {
49
Stan Ilievf50806a2016-10-24 10:40:39 -040050using uirenderer::PaintUtils;
51
John Reckc1b33d62015-04-22 09:04:45 -070052Canvas* Canvas::create_canvas(const SkBitmap& bitmap) {
Derek Sollenberger8872b382014-06-23 14:13:53 -040053 return new SkiaCanvas(bitmap);
54}
55
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040056Canvas* Canvas::create_canvas(SkCanvas* skiaCanvas) {
57 return new SkiaCanvas(skiaCanvas);
Derek Sollenberger8872b382014-06-23 14:13:53 -040058}
59
Stan Ilievf50806a2016-10-24 10:40:39 -040060SkiaCanvas::SkiaCanvas() {}
61
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040062SkiaCanvas::SkiaCanvas(SkCanvas* canvas) : mCanvas(canvas) {}
Stan Ilievf50806a2016-10-24 10:40:39 -040063
John Reckc1b33d62015-04-22 09:04:45 -070064SkiaCanvas::SkiaCanvas(const SkBitmap& bitmap) {
Derek Sollenbergerd01b5912018-10-19 15:55:33 -040065 mCanvasOwned = std::unique_ptr<SkCanvas>(new SkCanvas(bitmap));
66 mCanvas = mCanvasOwned.get();
Derek Sollenberger8872b382014-06-23 14:13:53 -040067}
68
Stan Iliev021693b2016-10-17 16:26:15 -040069SkiaCanvas::~SkiaCanvas() {}
70
Derek Sollenbergerc1908132016-07-15 10:28:16 -040071void SkiaCanvas::reset(SkCanvas* skiaCanvas) {
Mike Reed6acfe162016-11-18 17:21:09 -050072 if (mCanvas != skiaCanvas) {
73 mCanvas = skiaCanvas;
74 mCanvasOwned.reset();
75 }
Derek Sollenbergerc1908132016-07-15 10:28:16 -040076 mSaveStack.reset(nullptr);
Derek Sollenbergerc1908132016-07-15 10:28:16 -040077}
78
Derek Sollenberger8872b382014-06-23 14:13:53 -040079// ----------------------------------------------------------------------------
80// Canvas state operations: Replace Bitmap
81// ----------------------------------------------------------------------------
82
John Reckc1b33d62015-04-22 09:04:45 -070083void SkiaCanvas::setBitmap(const SkBitmap& bitmap) {
Tony Mantler4f641d12017-03-14 22:36:14 +000084 // deletes the previously owned canvas (if any)
Derek Sollenbergerd01b5912018-10-19 15:55:33 -040085 mCanvasOwned.reset(new SkCanvas(bitmap));
86 mCanvas = mCanvasOwned.get();
Tony Mantler4f641d12017-03-14 22:36:14 +000087
Derek Sollenberger8872b382014-06-23 14:13:53 -040088 // clean up the old save stack
Stan Ilievf50806a2016-10-24 10:40:39 -040089 mSaveStack.reset(nullptr);
Derek Sollenberger8872b382014-06-23 14:13:53 -040090}
91
92// ----------------------------------------------------------------------------
93// Canvas state operations
94// ----------------------------------------------------------------------------
95
96bool SkiaCanvas::isOpaque() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -040097 return mCanvas->imageInfo().isOpaque();
Derek Sollenberger8872b382014-06-23 14:13:53 -040098}
99
100int SkiaCanvas::width() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400101 return mCanvas->imageInfo().width();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400102}
103
104int SkiaCanvas::height() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400105 return mCanvas->imageInfo().height();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400106}
107
108// ----------------------------------------------------------------------------
109// Canvas state operations: Save (layer)
110// ----------------------------------------------------------------------------
111
112int SkiaCanvas::getSaveCount() const {
113 return mCanvas->getSaveCount();
114}
115
Florin Malitaeecff562015-12-21 10:43:01 -0500116int SkiaCanvas::save(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400117 int count = mCanvas->save();
118 recordPartialSave(flags);
119 return count;
120}
121
Florin Malita5e271402015-11-04 14:36:02 -0500122// The SkiaCanvas::restore operation layers on the capability to preserve
123// either (or both) the matrix and/or clip state after a SkCanvas::restore
124// operation. It does this by explicitly saving off the clip & matrix state
125// when requested and playing it back after the SkCanvas::restore.
Derek Sollenberger8872b382014-06-23 14:13:53 -0400126void SkiaCanvas::restore() {
Stan Ilievf50806a2016-10-24 10:40:39 -0400127 const auto* rec = this->currentSaveRec();
128 if (!rec) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400129 // Fast path - no record for this frame.
130 mCanvas->restore();
131 return;
132 }
133
Florin Malitaeecff562015-12-21 10:43:01 -0500134 bool preserveMatrix = !(rec->saveFlags & SaveFlags::Matrix);
John Reck1bcacfd2017-11-03 10:12:19 -0700135 bool preserveClip = !(rec->saveFlags & SaveFlags::Clip);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400136
137 SkMatrix savedMatrix;
138 if (preserveMatrix) {
139 savedMatrix = mCanvas->getTotalMatrix();
140 }
141
Stan Ilievf50806a2016-10-24 10:40:39 -0400142 const size_t clipIndex = rec->clipIndex;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400143
144 mCanvas->restore();
Stan Ilievf50806a2016-10-24 10:40:39 -0400145 mSaveStack->pop_back();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400146
147 if (preserveMatrix) {
148 mCanvas->setMatrix(savedMatrix);
149 }
150
Stan Ilievf50806a2016-10-24 10:40:39 -0400151 if (preserveClip) {
152 this->applyPersistentClips(clipIndex);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400153 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400154}
155
156void SkiaCanvas::restoreToCount(int restoreCount) {
157 while (mCanvas->getSaveCount() > restoreCount) {
158 this->restore();
159 }
160}
161
Florin Malitaeecff562015-12-21 10:43:01 -0500162static inline SkCanvas::SaveLayerFlags layerFlags(SaveFlags::Flags flags) {
163 SkCanvas::SaveLayerFlags layerFlags = 0;
164
Florin Malitaeecff562015-12-21 10:43:01 -0500165 if (!(flags & SaveFlags::ClipToLayer)) {
Mike Reed93b71202019-09-03 16:45:12 -0400166 layerFlags |= SkCanvasPriv::kDontClipToLayer_SaveLayerFlag;
Florin Malitaeecff562015-12-21 10:43:01 -0500167 }
168
169 return layerFlags;
170}
171
John Reck1bcacfd2017-11-03 10:12:19 -0700172int SkiaCanvas::saveLayer(float left, float top, float right, float bottom, const SkPaint* paint,
173 SaveFlags::Flags flags) {
Florin Malitaeecff562015-12-21 10:43:01 -0500174 const SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
Derek Sollenbergerb8201192017-01-09 16:11:59 -0500175 const SkCanvas::SaveLayerRec rec(&bounds, paint, layerFlags(flags));
Florin Malitaeecff562015-12-21 10:43:01 -0500176
Stan Iliev68885e32016-12-14 11:18:34 -0500177 return mCanvas->saveLayer(rec);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400178}
179
John Reck1bcacfd2017-11-03 10:12:19 -0700180int SkiaCanvas::saveLayerAlpha(float left, float top, float right, float bottom, int alpha,
181 SaveFlags::Flags flags) {
Florin Malitaeecff562015-12-21 10:43:01 -0500182 if (static_cast<unsigned>(alpha) < 0xFF) {
Yuqian Lifd92ee42016-04-27 17:03:38 -0400183 SkPaint alphaPaint;
184 alphaPaint.setAlpha(alpha);
185 return this->saveLayer(left, top, right, bottom, &alphaPaint, flags);
Florin Malitaeecff562015-12-21 10:43:01 -0500186 }
Yuqian Lifd92ee42016-04-27 17:03:38 -0400187 return this->saveLayer(left, top, right, bottom, nullptr, flags);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400188}
189
Derek Sollenberger24fc9012018-12-07 14:12:12 -0500190int SkiaCanvas::saveUnclippedLayer(int left, int top, int right, int bottom) {
191 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
192 return SkAndroidFrameworkUtils::SaveBehind(mCanvas, &bounds);
193}
194
Derek Sollenbergerac33a482019-04-22 16:28:09 -0400195void SkiaCanvas::restoreUnclippedLayer(int restoreCount, const SkPaint& paint) {
196
197 while (mCanvas->getSaveCount() > restoreCount + 1) {
198 this->restore();
199 }
200
201 if (mCanvas->getSaveCount() == restoreCount + 1) {
202 SkCanvasPriv::DrawBehind(mCanvas, *filterPaint(paint));
203 this->restore();
204 }
205}
206
Stan Ilievf50806a2016-10-24 10:40:39 -0400207class SkiaCanvas::Clip {
208public:
Mike Reed6e49c9f2016-12-02 15:36:59 -0500209 Clip(const SkRect& rect, SkClipOp op, const SkMatrix& m)
John Reck1bcacfd2017-11-03 10:12:19 -0700210 : mType(Type::Rect), mOp(op), mMatrix(m), mRRect(SkRRect::MakeRect(rect)) {}
Mike Reed6e49c9f2016-12-02 15:36:59 -0500211 Clip(const SkRRect& rrect, SkClipOp op, const SkMatrix& m)
John Reck1bcacfd2017-11-03 10:12:19 -0700212 : mType(Type::RRect), mOp(op), mMatrix(m), mRRect(rrect) {}
Mike Reed6e49c9f2016-12-02 15:36:59 -0500213 Clip(const SkPath& path, SkClipOp op, const SkMatrix& m)
Ben Wagner0ed10be2018-06-28 17:08:16 -0400214 : mType(Type::Path), mOp(op), mMatrix(m), mPath(std::in_place, path) {}
Stan Ilievf50806a2016-10-24 10:40:39 -0400215
216 void apply(SkCanvas* canvas) const {
217 canvas->setMatrix(mMatrix);
218 switch (mType) {
John Reck1bcacfd2017-11-03 10:12:19 -0700219 case Type::Rect:
220 canvas->clipRect(mRRect.rect(), mOp);
221 break;
222 case Type::RRect:
223 canvas->clipRRect(mRRect, mOp);
224 break;
225 case Type::Path:
Ben Wagner0ed10be2018-06-28 17:08:16 -0400226 canvas->clipPath(mPath.value(), mOp);
John Reck1bcacfd2017-11-03 10:12:19 -0700227 break;
Stan Ilievf50806a2016-10-24 10:40:39 -0400228 }
229 }
230
231private:
232 enum class Type {
233 Rect,
234 RRect,
235 Path,
236 };
237
John Reck1bcacfd2017-11-03 10:12:19 -0700238 Type mType;
239 SkClipOp mOp;
240 SkMatrix mMatrix;
Stan Ilievf50806a2016-10-24 10:40:39 -0400241
242 // These are logically a union (tracked separately due to non-POD path).
Ben Wagner0ed10be2018-06-28 17:08:16 -0400243 std::optional<SkPath> mPath;
John Reck1bcacfd2017-11-03 10:12:19 -0700244 SkRRect mRRect;
Stan Ilievf50806a2016-10-24 10:40:39 -0400245};
246
247const SkiaCanvas::SaveRec* SkiaCanvas::currentSaveRec() const {
John Reck1bcacfd2017-11-03 10:12:19 -0700248 const SaveRec* rec = mSaveStack ? static_cast<const SaveRec*>(mSaveStack->back()) : nullptr;
Stan Ilievf50806a2016-10-24 10:40:39 -0400249 int currentSaveCount = mCanvas->getSaveCount();
250 SkASSERT(!rec || currentSaveCount >= rec->saveCount);
251
252 return (rec && rec->saveCount == currentSaveCount) ? rec : nullptr;
253}
254
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);
395 mCanvas->clipPath(*path, op);
Chris Craik5ec6a282015-06-23 15:42:12 -0700396 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400397}
398
Derek Sollenberger8872b382014-06-23 14:13:53 -0400399// ----------------------------------------------------------------------------
400// Canvas state operations: Filters
401// ----------------------------------------------------------------------------
402
Ben Wagner0ed10be2018-06-28 17:08:16 -0400403PaintFilter* SkiaCanvas::getPaintFilter() {
404 return mPaintFilter.get();
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400405}
406
Ben Wagner0ed10be2018-06-28 17:08:16 -0400407void SkiaCanvas::setPaintFilter(sk_sp<PaintFilter> paintFilter) {
408 mPaintFilter = std::move(paintFilter);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400409}
410
411// ----------------------------------------------------------------------------
Matt Sarettd0814db2017-04-13 09:33:18 -0400412// Canvas state operations: Capture
413// ----------------------------------------------------------------------------
414
415SkCanvasState* SkiaCanvas::captureCanvasState() const {
416 SkCanvas* canvas = mCanvas;
417 if (mCanvasOwned) {
418 // Important to use the underlying SkCanvas, not the wrapper.
419 canvas = mCanvasOwned.get();
420 }
421
422 // Workarounds for http://crbug.com/271096: SW draw only supports
423 // translate & scale transforms, and a simple rectangular clip.
424 // (This also avoids significant wasted time in calling
425 // SkCanvasStateUtils::CaptureCanvasState when the clip is complex).
John Reck1bcacfd2017-11-03 10:12:19 -0700426 if (!canvas->isClipRect() || (canvas->getTotalMatrix().getType() &
427 ~(SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask))) {
428 return nullptr;
Matt Sarettd0814db2017-04-13 09:33:18 -0400429 }
430
431 return SkCanvasStateUtils::CaptureCanvasState(canvas);
432}
433
434// ----------------------------------------------------------------------------
Derek Sollenberger8872b382014-06-23 14:13:53 -0400435// Canvas draw operations
436// ----------------------------------------------------------------------------
437
Mike Reed260ab722016-10-07 15:59:20 -0400438void SkiaCanvas::drawColor(int color, SkBlendMode mode) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400439 mCanvas->drawColor(color, mode);
440}
441
Ben Wagner0ed10be2018-06-28 17:08:16 -0400442SkiaCanvas::PaintCoW&& SkiaCanvas::filterPaint(PaintCoW&& paint) const {
443 if (mPaintFilter) {
444 mPaintFilter->filter(&paint.writeable());
445 }
446 return std::move(paint);
447}
448
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400449void SkiaCanvas::drawPaint(const SkPaint& paint) {
Ben Wagner0ed10be2018-06-28 17:08:16 -0400450 mCanvas->drawPaint(*filterPaint(paint));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400451}
452
453// ----------------------------------------------------------------------------
454// Canvas draw operations: Geometry
455// ----------------------------------------------------------------------------
456
Mike Reedc2dbc032019-07-25 12:28:29 -0400457void SkiaCanvas::drawPoints(const float* points, int count, const Paint& paint,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400458 SkCanvas::PointMode mode) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500459 if (CC_UNLIKELY(count < 2 || paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400460 // convert the floats into SkPoints
John Reck1bcacfd2017-11-03 10:12:19 -0700461 count >>= 1; // now it is the number of points
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400462 std::unique_ptr<SkPoint[]> pts(new SkPoint[count]);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400463 for (int i = 0; i < count; i++) {
464 pts[i].set(points[0], points[1]);
465 points += 2;
466 }
Mike Reedc2dbc032019-07-25 12:28:29 -0400467
468 apply_looper(&paint, [&](const SkPaint& p) {
469 mCanvas->drawPoints(mode, count, pts.get(), p);
470 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400471}
472
Mike Reedc2dbc032019-07-25 12:28:29 -0400473void SkiaCanvas::drawPoint(float x, float y, const Paint& paint) {
474 apply_looper(&paint, [&](const SkPaint& p) {
475 mCanvas->drawPoint(x, y, p);
476 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400477}
478
Mike Reedc2dbc032019-07-25 12:28:29 -0400479void SkiaCanvas::drawPoints(const float* points, int count, const Paint& paint) {
480 this->drawPoints(points, count, paint, SkCanvas::kPoints_PointMode);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400481}
482
483void SkiaCanvas::drawLine(float startX, float startY, float stopX, float stopY,
Mike Reedc2dbc032019-07-25 12:28:29 -0400484 const Paint& paint) {
485 apply_looper(&paint, [&](const SkPaint& p) {
486 mCanvas->drawLine(startX, startY, stopX, stopY, p);
487 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400488}
489
Mike Reedc2dbc032019-07-25 12:28:29 -0400490void SkiaCanvas::drawLines(const float* points, int count, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500491 if (CC_UNLIKELY(count < 4 || paint.nothingToDraw())) return;
Mike Reedc2dbc032019-07-25 12:28:29 -0400492 this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400493}
494
Mike Reedc2dbc032019-07-25 12:28:29 -0400495void SkiaCanvas::drawRect(float left, float top, float right, float bottom, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500496 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Mike Reedc2dbc032019-07-25 12:28:29 -0400497 apply_looper(&paint, [&](const SkPaint& p) {
498 mCanvas->drawRect({left, top, right, bottom}, p);
499 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400500}
501
Mike Reedc2dbc032019-07-25 12:28:29 -0400502void SkiaCanvas::drawRegion(const SkRegion& region, 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->drawRegion(region, p);
506 });
Derek Sollenberger94394b32015-07-10 09:58:41 -0400507}
508
John Reck1bcacfd2017-11-03 10:12:19 -0700509void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom, float rx, float ry,
Mike Reedc2dbc032019-07-25 12:28:29 -0400510 const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500511 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400512 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Mike Reedc2dbc032019-07-25 12:28:29 -0400513 apply_looper(&paint, [&](const SkPaint& p) {
514 mCanvas->drawRoundRect(rect, rx, ry, p);
515 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400516}
517
Nader Jawadadfe1d92018-09-27 12:27:36 -0700518void SkiaCanvas::drawDoubleRoundRect(const SkRRect& outer, const SkRRect& inner,
Mike Reedc2dbc032019-07-25 12:28:29 -0400519 const Paint& paint) {
520 apply_looper(&paint, [&](const SkPaint& p) {
521 mCanvas->drawDRRect(outer, inner, p);
522 });
Nader Jawadadfe1d92018-09-27 12:27:36 -0700523}
524
Mike Reedc2dbc032019-07-25 12:28:29 -0400525void SkiaCanvas::drawCircle(float x, float y, float radius, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500526 if (CC_UNLIKELY(radius <= 0 || paint.nothingToDraw())) return;
Mike Reedc2dbc032019-07-25 12:28:29 -0400527 apply_looper(&paint, [&](const SkPaint& p) {
528 mCanvas->drawCircle(x, y, radius, p);
529 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400530}
531
Mike Reedc2dbc032019-07-25 12:28:29 -0400532void SkiaCanvas::drawOval(float left, float top, float right, float bottom, const Paint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500533 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400534 SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
Mike Reedc2dbc032019-07-25 12:28:29 -0400535 apply_looper(&paint, [&](const SkPaint& p) {
536 mCanvas->drawOval(oval, p);
537 });
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 Reedc2dbc032019-07-25 12:28:29 -0400544 apply_looper(&paint, [&](const SkPaint& p) {
545 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 Reedc2dbc032019-07-25 12:28:29 -0400558 apply_looper(&paint, [&](const SkPaint& p) {
559 mCanvas->drawPath(path, p);
560 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400561}
562
Mike Reedc2dbc032019-07-25 12:28:29 -0400563void SkiaCanvas::drawVertices(const SkVertices* vertices, SkBlendMode mode, const Paint& paint) {
564 apply_looper(&paint, [&](const SkPaint& p) {
565 mCanvas->drawVertices(vertices, mode, p);
566 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400567}
568
569// ----------------------------------------------------------------------------
570// Canvas draw operations: Bitmaps
571// ----------------------------------------------------------------------------
572
Mike Reedc2dbc032019-07-25 12:28:29 -0400573void SkiaCanvas::drawBitmap(Bitmap& bitmap, float left, float top, const Paint* paint) {
574 auto image = bitmap.makeImage();
575 apply_looper(paint, [&](const SkPaint& p) {
576 mCanvas->drawImage(image, left, top, &p);
577 });
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400578}
579
Mike Reedc2dbc032019-07-25 12:28:29 -0400580void SkiaCanvas::drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const Paint* paint) {
581 auto image = bitmap.makeImage();
Mike Reed6acfe162016-11-18 17:21:09 -0500582 SkAutoCanvasRestore acr(mCanvas, true);
Mike Reed70ffbf92014-12-08 17:03:30 -0500583 mCanvas->concat(matrix);
Mike Reedc2dbc032019-07-25 12:28:29 -0400584 apply_looper(paint, [&](const SkPaint& p) {
585 mCanvas->drawImage(image, 0, 0, &p);
586 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400587}
588
John Reck1bcacfd2017-11-03 10:12:19 -0700589void SkiaCanvas::drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight,
590 float srcBottom, float dstLeft, float dstTop, float dstRight,
Mike Reedc2dbc032019-07-25 12:28:29 -0400591 float dstBottom, const Paint* paint) {
592 auto image = bitmap.makeImage();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400593 SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
594 SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400595
Mike Reedc2dbc032019-07-25 12:28:29 -0400596 apply_looper(paint, [&](const SkPaint& p) {
597 mCanvas->drawImageRect(image, srcRect, dstRect, &p, SkCanvas::kFast_SrcRectConstraint);
598 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400599}
600
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400601void SkiaCanvas::drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight,
Mike Reedc2dbc032019-07-25 12:28:29 -0400602 const float* vertices, const int* colors, const Paint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400603 const int ptCount = (meshWidth + 1) * (meshHeight + 1);
604 const int indexCount = meshWidth * meshHeight * 6;
Mike Reed871cd2d2017-03-17 10:15:52 -0400605 uint32_t flags = SkVertices::kHasTexCoords_BuilderFlag;
606 if (colors) {
607 flags |= SkVertices::kHasColors_BuilderFlag;
608 }
Mike Reed826deef2017-04-04 15:32:04 -0400609 SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, ptCount, indexCount, flags);
Mike Reed871cd2d2017-03-17 10:15:52 -0400610 memcpy(builder.positions(), vertices, ptCount * sizeof(SkPoint));
611 if (colors) {
612 memcpy(builder.colors(), colors, ptCount * sizeof(SkColor));
613 }
614 SkPoint* texs = builder.texCoords();
615 uint16_t* indices = builder.indices();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400616
617 // cons up texture coordinates and indices
618 {
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400619 const SkScalar w = SkIntToScalar(bitmap.width());
620 const SkScalar h = SkIntToScalar(bitmap.height());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400621 const SkScalar dx = w / meshWidth;
622 const SkScalar dy = h / meshHeight;
623
624 SkPoint* texsPtr = texs;
625 SkScalar y = 0;
626 for (int i = 0; i <= meshHeight; i++) {
627 if (i == meshHeight) {
628 y = h; // to ensure numerically we hit h exactly
629 }
630 SkScalar x = 0;
631 for (int j = 0; j < meshWidth; j++) {
632 texsPtr->set(x, y);
633 texsPtr += 1;
634 x += dx;
635 }
636 texsPtr->set(w, y);
637 texsPtr += 1;
638 y += dy;
639 }
640 SkASSERT(texsPtr - texs == ptCount);
641 }
642
643 // cons up indices
644 {
645 uint16_t* indexPtr = indices;
646 int index = 0;
647 for (int i = 0; i < meshHeight; i++) {
648 for (int j = 0; j < meshWidth; j++) {
649 // lower-left triangle
650 *indexPtr++ = index;
651 *indexPtr++ = index + meshWidth + 1;
652 *indexPtr++ = index + meshWidth + 2;
653 // upper-right triangle
654 *indexPtr++ = index;
655 *indexPtr++ = index + meshWidth + 2;
656 *indexPtr++ = index + 1;
657 // bump to the next cell
658 index += 1;
659 }
660 // bump to the next row
661 index += 1;
662 }
663 SkASSERT(indexPtr - indices == indexCount);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400664 }
665
John Reck1bcacfd2017-11-03 10:12:19 -0700666// double-check that we have legal indices
Derek Sollenberger8872b382014-06-23 14:13:53 -0400667#ifdef SK_DEBUG
668 {
669 for (int i = 0; i < indexCount; i++) {
670 SkASSERT((unsigned)indices[i] < (unsigned)ptCount);
671 }
672 }
673#endif
674
675 // cons-up a shader for the bitmap
Mike Reedc2dbc032019-07-25 12:28:29 -0400676 Paint pnt;
677 if (paint) {
678 pnt = *paint;
679 }
680 pnt.setShader(bitmap.makeImage()->makeShader());
681 auto v = builder.detach();
682 apply_looper(&pnt, [&](const SkPaint& p) {
683 mCanvas->drawVertices(v, SkBlendMode::kModulate, p);
684 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400685}
686
John Reck1bcacfd2017-11-03 10:12:19 -0700687void SkiaCanvas::drawNinePatch(Bitmap& bitmap, const Res_png_9patch& chunk, float dstLeft,
688 float dstTop, float dstRight, float dstBottom,
Mike Reedc2dbc032019-07-25 12:28:29 -0400689 const Paint* paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400690 SkCanvas::Lattice lattice;
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400691 NinePatchUtils::SetLatticeDivs(&lattice, chunk, bitmap.width(), bitmap.height());
Stan Ilievf50806a2016-10-24 10:40:39 -0400692
Stan Ilieve12d7312017-12-04 14:48:27 -0500693 lattice.fRectTypes = nullptr;
694 lattice.fColors = nullptr;
Stan Ilievf50806a2016-10-24 10:40:39 -0400695 int numFlags = 0;
Stan Iliev021693b2016-10-17 16:26:15 -0400696 if (chunk.numColors > 0 && chunk.numColors == NinePatchUtils::NumDistinctRects(lattice)) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400697 // We can expect the framework to give us a color for every distinct rect.
698 // Skia requires a flag for every rect.
699 numFlags = (lattice.fXCount + 1) * (lattice.fYCount + 1);
700 }
701
Stan Ilieve12d7312017-12-04 14:48:27 -0500702 SkAutoSTMalloc<25, SkCanvas::Lattice::RectType> flags(numFlags);
703 SkAutoSTMalloc<25, SkColor> colors(numFlags);
Stan Ilievf50806a2016-10-24 10:40:39 -0400704 if (numFlags > 0) {
Stan Ilieve12d7312017-12-04 14:48:27 -0500705 NinePatchUtils::SetLatticeFlags(&lattice, flags.get(), numFlags, chunk, colors.get());
Stan Ilievf50806a2016-10-24 10:40:39 -0400706 }
707
708 lattice.fBounds = nullptr;
709 SkRect dst = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Mike Reedc2dbc032019-07-25 12:28:29 -0400710 auto image = bitmap.makeImage();
711 apply_looper(paint, [&](const SkPaint& p) {
712 mCanvas->drawImageLattice(image.get(), lattice, dst, &p);
713 });
Derek Sollenbergeredca3202015-07-10 13:56:39 -0400714}
715
Derek Sollenberger2d142132018-01-22 10:25:26 -0500716double SkiaCanvas::drawAnimatedImage(AnimatedImageDrawable* imgDrawable) {
717 return imgDrawable->drawStaging(mCanvas);
Leon Scroggins III671cce22018-01-14 16:52:17 -0500718}
719
Doris Liu766431a2016-02-04 22:17:11 +0000720void SkiaCanvas::drawVectorDrawable(VectorDrawableRoot* vectorDrawable) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800721 vectorDrawable->drawStaging(this);
Doris Liu766431a2016-02-04 22:17:11 +0000722}
723
Derek Sollenberger8872b382014-06-23 14:13:53 -0400724// ----------------------------------------------------------------------------
725// Canvas draw operations: Text
726// ----------------------------------------------------------------------------
727
Mike Reed2dfd55d2019-01-08 16:19:03 -0500728void SkiaCanvas::drawGlyphs(ReadGlyphFunc glyphFunc, int count, const Paint& paint, float x,
John Reck1bcacfd2017-11-03 10:12:19 -0700729 float y, float boundsLeft, float boundsTop, float boundsRight,
730 float boundsBottom, float totalAdvance) {
Stan Iliev0b58d992017-03-30 18:22:27 -0400731 if (count <= 0 || paint.nothingToDraw()) return;
Mike Reedf6d86ac2019-01-18 14:13:23 -0500732 Paint paintCopy(paint);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400733 if (mPaintFilter) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500734 mPaintFilter->filterFullPaint(&paintCopy);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400735 }
Mike Reedf6d86ac2019-01-18 14:13:23 -0500736 const SkFont& font = paintCopy.getSkFont();
Stan Iliev7717e222018-02-05 18:04:11 -0500737 // Stroke with a hairline is drawn on HW with a fill style for compatibility with Android O and
738 // older.
John Recke170fb62018-05-07 08:12:07 -0700739 if (!mCanvasOwned && sApiLevel <= 27 && paintCopy.getStrokeWidth() <= 0 &&
740 paintCopy.getStyle() == SkPaint::kStroke_Style) {
Stan Iliev7717e222018-02-05 18:04:11 -0500741 paintCopy.setStyle(SkPaint::kFill_Style);
742 }
Stan Ilievf50806a2016-10-24 10:40:39 -0400743
Stan Ilievf50806a2016-10-24 10:40:39 -0400744 SkTextBlobBuilder builder;
Mike Reed2e204fc2019-01-28 13:31:36 -0500745 const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(font, count);
Stan Iliev0b58d992017-03-30 18:22:27 -0400746 glyphFunc(buffer.glyphs, buffer.pos);
Stan Ilievf50806a2016-10-24 10:40:39 -0400747
748 sk_sp<SkTextBlob> textBlob(builder.make());
Nathaniel Nifong52d37772020-01-10 16:12:41 -0500749
750 apply_looper(&paintCopy, [&](const SkPaint& p) {
751 mCanvas->drawTextBlob(textBlob, 0, 0, p);
752 });
Stan Ilievf50806a2016-10-24 10:40:39 -0400753 drawTextDecorations(x, y, totalAdvance, paintCopy);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400754}
755
Yuqian Liafc221492016-07-18 13:07:42 -0400756void SkiaCanvas::drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
Mike Reed2dfd55d2019-01-08 16:19:03 -0500757 const Paint& paint, const SkPath& path, size_t start,
John Reck1bcacfd2017-11-03 10:12:19 -0700758 size_t end) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500759 Paint paintCopy(paint);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400760 if (mPaintFilter) {
Mike Reedf6d86ac2019-01-18 14:13:23 -0500761 mPaintFilter->filterFullPaint(&paintCopy);
Ben Wagner0ed10be2018-06-28 17:08:16 -0400762 }
Mike Reedf6d86ac2019-01-18 14:13:23 -0500763 const SkFont& font = paintCopy.getSkFont();
Derek Sollenberger415a74b2018-03-14 15:03:13 -0400764
Yuqian Liafc221492016-07-18 13:07:42 -0400765 const int N = end - start;
Mike Reed4a4b1be2019-01-01 15:43:06 -0500766 SkTextBlobBuilder builder;
767 auto rec = builder.allocRunRSXform(font, N);
768 SkRSXform* xform = (SkRSXform*)rec.pos;
769 uint16_t* glyphs = rec.glyphs;
Yuqian Liafc221492016-07-18 13:07:42 -0400770 SkPathMeasure meas(path, false);
771
772 for (size_t i = start; i < end; i++) {
773 glyphs[i - start] = layout.getGlyphId(i);
Stan Ilievc6c96dd2018-01-10 16:06:04 -0500774 float halfWidth = layout.getCharAdvance(i) * 0.5f;
775 float x = hOffset + layout.getX(i) + halfWidth;
Yuqian Liafc221492016-07-18 13:07:42 -0400776 float y = vOffset + layout.getY(i);
777
778 SkPoint pos;
779 SkVector tan;
780 if (!meas.getPosTan(x, &pos, &tan)) {
781 pos.set(x, y);
782 tan.set(1, 0);
783 }
784 xform[i - start].fSCos = tan.x();
785 xform[i - start].fSSin = tan.y();
Stan Ilievc6c96dd2018-01-10 16:06:04 -0500786 xform[i - start].fTx = pos.x() - tan.y() * y - halfWidth * tan.x();
787 xform[i - start].fTy = pos.y() + tan.x() * y - halfWidth * tan.y();
Yuqian Liafc221492016-07-18 13:07:42 -0400788 }
Nathaniel Nifong52d37772020-01-10 16:12:41 -0500789 auto* finalCanvas = this->asSkCanvas();
790 apply_looper(&paintCopy, [&](const SkPaint& p) {
791 finalCanvas->drawTextBlob(builder.make(), 0, 0, paintCopy);
792 });
Derek Sollenberger8872b382014-06-23 14:13:53 -0400793}
794
Derek Sollenberger6f485562015-07-30 10:00:39 -0400795// ----------------------------------------------------------------------------
796// Canvas draw operations: Animations
797// ----------------------------------------------------------------------------
798
Derek Sollenberger6f485562015-07-30 10:00:39 -0400799void SkiaCanvas::drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
John Reck1bcacfd2017-11-03 10:12:19 -0700800 uirenderer::CanvasPropertyPrimitive* top,
801 uirenderer::CanvasPropertyPrimitive* right,
802 uirenderer::CanvasPropertyPrimitive* bottom,
803 uirenderer::CanvasPropertyPrimitive* rx,
804 uirenderer::CanvasPropertyPrimitive* ry,
805 uirenderer::CanvasPropertyPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -0400806 sk_sp<uirenderer::skiapipeline::AnimatedRoundRect> drawable(
John Reck1bcacfd2017-11-03 10:12:19 -0700807 new uirenderer::skiapipeline::AnimatedRoundRect(left, top, right, bottom, rx, ry,
808 paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400809 mCanvas->drawDrawable(drawable.get());
810}
811
John Reck1bcacfd2017-11-03 10:12:19 -0700812void SkiaCanvas::drawCircle(uirenderer::CanvasPropertyPrimitive* x,
813 uirenderer::CanvasPropertyPrimitive* y,
814 uirenderer::CanvasPropertyPrimitive* radius,
815 uirenderer::CanvasPropertyPaint* paint) {
816 sk_sp<uirenderer::skiapipeline::AnimatedCircle> drawable(
817 new uirenderer::skiapipeline::AnimatedCircle(x, y, radius, paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400818 mCanvas->drawDrawable(drawable.get());
819}
820
John Reck894e85a2019-07-15 16:16:44 -0700821void SkiaCanvas::drawPicture(const SkPicture& picture) {
822 // TODO: Change to mCanvas->drawPicture()? SkCanvas::drawPicture seems to be
823 // where the logic is for playback vs. ref picture. Using picture.playback here
824 // to stay behavior-identical for now, but should revisit this at some point.
825 picture.playback(mCanvas);
826}
827
Derek Sollenberger6f485562015-07-30 10:00:39 -0400828// ----------------------------------------------------------------------------
829// Canvas draw operations: View System
830// ----------------------------------------------------------------------------
831
Stan Ilievf50806a2016-10-24 10:40:39 -0400832void SkiaCanvas::drawLayer(uirenderer::DeferredLayerUpdater* layerUpdater) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400833 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw Layers");
834}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400835
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400836void SkiaCanvas::drawRenderNode(uirenderer::RenderNode* renderNode) {
837 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw RenderNodes");
838}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400839
John Reckcd1c3eb2016-04-14 10:38:54 -0700840void SkiaCanvas::callDrawGLFunction(Functor* functor,
John Reck1bcacfd2017-11-03 10:12:19 -0700841 uirenderer::GlFunctorLifecycleListener* listener) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400842 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw GL Content");
843}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400844
John Reck1bcacfd2017-11-03 10:12:19 -0700845} // namespace android