blob: eb0d161d71d17982cb05b61f172406e1f2df633f [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"
Stan Iliev021693b2016-10-17 16:26:15 -040024#include "pipeline/skia/AnimatedDrawables.h"
Derek Sollenbergerc1908132016-07-15 10:28:16 -040025
Matt Sarettd0814db2017-04-13 09:33:18 -040026#include <SkCanvasStateUtils.h>
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -040027#include <SkColorFilter.h>
Matt Sarettea70d222017-03-29 16:25:10 -040028#include <SkColorSpaceXformCanvas.h>
John Reck849911a2015-01-20 07:51:14 -080029#include <SkDeque.h>
30#include <SkDrawFilter.h>
John Reck1bcacfd2017-11-03 10:12:19 -070031#include <SkDrawable.h>
John Reck849911a2015-01-20 07:51:14 -080032#include <SkGraphics.h>
Derek Sollenberger6f485562015-07-30 10:00:39 -040033#include <SkImage.h>
Matt Sarett62feb3a2016-09-20 10:34:11 -040034#include <SkImagePriv.h>
Yuqian Liafc221492016-07-18 13:07:42 -040035#include <SkRSXform.h>
John Reck849911a2015-01-20 07:51:14 -080036#include <SkShader.h>
John Reck849911a2015-01-20 07:51:14 -080037#include <SkTemplates.h>
Stan Ilievf50806a2016-10-24 10:40:39 -040038#include <SkTextBlob.h>
Derek Sollenberger8872b382014-06-23 14:13:53 -040039
Ben Wagner60126ef2015-08-07 12:13:48 -040040#include <memory>
41
Derek Sollenberger8872b382014-06-23 14:13:53 -040042namespace android {
43
Stan Ilievf50806a2016-10-24 10:40:39 -040044using uirenderer::PaintUtils;
45
John Reckc1b33d62015-04-22 09:04:45 -070046Canvas* Canvas::create_canvas(const SkBitmap& bitmap) {
Derek Sollenberger8872b382014-06-23 14:13:53 -040047 return new SkiaCanvas(bitmap);
48}
49
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040050Canvas* Canvas::create_canvas(SkCanvas* skiaCanvas) {
51 return new SkiaCanvas(skiaCanvas);
Derek Sollenberger8872b382014-06-23 14:13:53 -040052}
53
Stan Ilievf50806a2016-10-24 10:40:39 -040054SkiaCanvas::SkiaCanvas() {}
55
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040056SkiaCanvas::SkiaCanvas(SkCanvas* canvas) : mCanvas(canvas) {}
Stan Ilievf50806a2016-10-24 10:40:39 -040057
John Reckc1b33d62015-04-22 09:04:45 -070058SkiaCanvas::SkiaCanvas(const SkBitmap& bitmap) {
Romain Guy82426562017-04-04 19:38:50 -070059 sk_sp<SkColorSpace> cs = bitmap.refColorSpace();
Matt Sarettca9b7032017-04-13 12:18:47 -040060 mCanvasOwned =
61 std::unique_ptr<SkCanvas>(new SkCanvas(bitmap, SkCanvas::ColorBehavior::kLegacy));
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040062 if (cs.get() == nullptr || cs->isSRGB()) {
John Reck1bcacfd2017-11-03 10:12:19 -070063 if (!uirenderer::Properties::isSkiaEnabled()) {
64 mCanvasWrapper =
65 SkCreateColorSpaceXformCanvas(mCanvasOwned.get(), SkColorSpace::MakeSRGB());
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040066 mCanvas = mCanvasWrapper.get();
67 } else {
68 mCanvas = mCanvasOwned.get();
69 }
70 } else {
71 /** The wrapper is needed if we are drawing into a non-sRGB destination, since
72 * we need to transform all colors (not just bitmaps via filters) into the
73 * destination's colorspace.
74 */
75 mCanvasWrapper = SkCreateColorSpaceXformCanvas(mCanvasOwned.get(), std::move(cs));
76 mCanvas = mCanvasWrapper.get();
77 }
Derek Sollenberger8872b382014-06-23 14:13:53 -040078}
79
Stan Iliev021693b2016-10-17 16:26:15 -040080SkiaCanvas::~SkiaCanvas() {}
81
Derek Sollenbergerc1908132016-07-15 10:28:16 -040082void SkiaCanvas::reset(SkCanvas* skiaCanvas) {
Mike Reed6acfe162016-11-18 17:21:09 -050083 if (mCanvas != skiaCanvas) {
84 mCanvas = skiaCanvas;
85 mCanvasOwned.reset();
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040086 mCanvasWrapper.reset();
Mike Reed6acfe162016-11-18 17:21:09 -050087 }
Derek Sollenbergerc1908132016-07-15 10:28:16 -040088 mSaveStack.reset(nullptr);
Derek Sollenbergerc1908132016-07-15 10:28:16 -040089}
90
Derek Sollenberger8872b382014-06-23 14:13:53 -040091// ----------------------------------------------------------------------------
92// Canvas state operations: Replace Bitmap
93// ----------------------------------------------------------------------------
94
John Reckc1b33d62015-04-22 09:04:45 -070095void SkiaCanvas::setBitmap(const SkBitmap& bitmap) {
Romain Guy82426562017-04-04 19:38:50 -070096 sk_sp<SkColorSpace> cs = bitmap.refColorSpace();
Matt Sarettca9b7032017-04-13 12:18:47 -040097 std::unique_ptr<SkCanvas> newCanvas =
98 std::unique_ptr<SkCanvas>(new SkCanvas(bitmap, SkCanvas::ColorBehavior::kLegacy));
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040099 std::unique_ptr<SkCanvas> newCanvasWrapper;
100 if (cs.get() != nullptr && !cs->isSRGB()) {
101 newCanvasWrapper = SkCreateColorSpaceXformCanvas(newCanvas.get(), std::move(cs));
John Reck1bcacfd2017-11-03 10:12:19 -0700102 } else if (!uirenderer::Properties::isSkiaEnabled()) {
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400103 newCanvasWrapper = SkCreateColorSpaceXformCanvas(newCanvas.get(), SkColorSpace::MakeSRGB());
104 }
Tony Mantler4f641d12017-03-14 22:36:14 +0000105
Tony Mantler4f641d12017-03-14 22:36:14 +0000106 // deletes the previously owned canvas (if any)
Matt Sarettea70d222017-03-29 16:25:10 -0400107 mCanvasOwned = std::move(newCanvas);
108 mCanvasWrapper = std::move(newCanvasWrapper);
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400109 mCanvas = mCanvasWrapper ? mCanvasWrapper.get() : mCanvasOwned.get();
Tony Mantler4f641d12017-03-14 22:36:14 +0000110
Derek Sollenberger8872b382014-06-23 14:13:53 -0400111 // clean up the old save stack
Stan Ilievf50806a2016-10-24 10:40:39 -0400112 mSaveStack.reset(nullptr);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400113}
114
115// ----------------------------------------------------------------------------
116// Canvas state operations
117// ----------------------------------------------------------------------------
118
119bool SkiaCanvas::isOpaque() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400120 return mCanvas->imageInfo().isOpaque();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400121}
122
123int SkiaCanvas::width() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400124 return mCanvas->imageInfo().width();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400125}
126
127int SkiaCanvas::height() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400128 return mCanvas->imageInfo().height();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400129}
130
131// ----------------------------------------------------------------------------
132// Canvas state operations: Save (layer)
133// ----------------------------------------------------------------------------
134
135int SkiaCanvas::getSaveCount() const {
136 return mCanvas->getSaveCount();
137}
138
Florin Malitaeecff562015-12-21 10:43:01 -0500139int SkiaCanvas::save(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400140 int count = mCanvas->save();
141 recordPartialSave(flags);
142 return count;
143}
144
Florin Malita5e271402015-11-04 14:36:02 -0500145// The SkiaCanvas::restore operation layers on the capability to preserve
146// either (or both) the matrix and/or clip state after a SkCanvas::restore
147// operation. It does this by explicitly saving off the clip & matrix state
148// when requested and playing it back after the SkCanvas::restore.
Derek Sollenberger8872b382014-06-23 14:13:53 -0400149void SkiaCanvas::restore() {
Stan Ilievf50806a2016-10-24 10:40:39 -0400150 const auto* rec = this->currentSaveRec();
151 if (!rec) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400152 // Fast path - no record for this frame.
153 mCanvas->restore();
154 return;
155 }
156
Florin Malitaeecff562015-12-21 10:43:01 -0500157 bool preserveMatrix = !(rec->saveFlags & SaveFlags::Matrix);
John Reck1bcacfd2017-11-03 10:12:19 -0700158 bool preserveClip = !(rec->saveFlags & SaveFlags::Clip);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400159
160 SkMatrix savedMatrix;
161 if (preserveMatrix) {
162 savedMatrix = mCanvas->getTotalMatrix();
163 }
164
Stan Ilievf50806a2016-10-24 10:40:39 -0400165 const size_t clipIndex = rec->clipIndex;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400166
167 mCanvas->restore();
Stan Ilievf50806a2016-10-24 10:40:39 -0400168 mSaveStack->pop_back();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400169
170 if (preserveMatrix) {
171 mCanvas->setMatrix(savedMatrix);
172 }
173
Stan Ilievf50806a2016-10-24 10:40:39 -0400174 if (preserveClip) {
175 this->applyPersistentClips(clipIndex);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400176 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400177}
178
179void SkiaCanvas::restoreToCount(int restoreCount) {
180 while (mCanvas->getSaveCount() > restoreCount) {
181 this->restore();
182 }
183}
184
Florin Malitaeecff562015-12-21 10:43:01 -0500185static inline SkCanvas::SaveLayerFlags layerFlags(SaveFlags::Flags flags) {
186 SkCanvas::SaveLayerFlags layerFlags = 0;
187
Yuqian Li83427ff2016-09-14 11:14:06 -0400188 // We intentionally ignore the SaveFlags::HasAlphaLayer and
189 // SkCanvas::kIsOpaque_SaveLayerFlag flags because HWUI ignores it
190 // and our Android client may use it incorrectly.
191 // In Skia, this flag is purely for performance optimization.
Florin Malitaeecff562015-12-21 10:43:01 -0500192
193 if (!(flags & SaveFlags::ClipToLayer)) {
194 layerFlags |= SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag;
195 }
196
197 return layerFlags;
198}
199
John Reck1bcacfd2017-11-03 10:12:19 -0700200int SkiaCanvas::saveLayer(float left, float top, float right, float bottom, const SkPaint* paint,
201 SaveFlags::Flags flags) {
Florin Malitaeecff562015-12-21 10:43:01 -0500202 const SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
Derek Sollenbergerb8201192017-01-09 16:11:59 -0500203 const SkCanvas::SaveLayerRec rec(&bounds, paint, layerFlags(flags));
Florin Malitaeecff562015-12-21 10:43:01 -0500204
Stan Iliev68885e32016-12-14 11:18:34 -0500205 return mCanvas->saveLayer(rec);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400206}
207
John Reck1bcacfd2017-11-03 10:12:19 -0700208int SkiaCanvas::saveLayerAlpha(float left, float top, float right, float bottom, int alpha,
209 SaveFlags::Flags flags) {
Florin Malitaeecff562015-12-21 10:43:01 -0500210 if (static_cast<unsigned>(alpha) < 0xFF) {
Yuqian Lifd92ee42016-04-27 17:03:38 -0400211 SkPaint alphaPaint;
212 alphaPaint.setAlpha(alpha);
213 return this->saveLayer(left, top, right, bottom, &alphaPaint, flags);
Florin Malitaeecff562015-12-21 10:43:01 -0500214 }
Yuqian Lifd92ee42016-04-27 17:03:38 -0400215 return this->saveLayer(left, top, right, bottom, nullptr, flags);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400216}
217
Stan Ilievf50806a2016-10-24 10:40:39 -0400218class SkiaCanvas::Clip {
219public:
Mike Reed6e49c9f2016-12-02 15:36:59 -0500220 Clip(const SkRect& rect, SkClipOp op, const SkMatrix& m)
John Reck1bcacfd2017-11-03 10:12:19 -0700221 : mType(Type::Rect), mOp(op), mMatrix(m), mRRect(SkRRect::MakeRect(rect)) {}
Mike Reed6e49c9f2016-12-02 15:36:59 -0500222 Clip(const SkRRect& rrect, SkClipOp op, const SkMatrix& m)
John Reck1bcacfd2017-11-03 10:12:19 -0700223 : mType(Type::RRect), mOp(op), mMatrix(m), mRRect(rrect) {}
Mike Reed6e49c9f2016-12-02 15:36:59 -0500224 Clip(const SkPath& path, SkClipOp op, const SkMatrix& m)
John Reck1bcacfd2017-11-03 10:12:19 -0700225 : mType(Type::Path), mOp(op), mMatrix(m), mPath(&path) {}
Stan Ilievf50806a2016-10-24 10:40:39 -0400226
227 void apply(SkCanvas* canvas) const {
228 canvas->setMatrix(mMatrix);
229 switch (mType) {
John Reck1bcacfd2017-11-03 10:12:19 -0700230 case Type::Rect:
231 canvas->clipRect(mRRect.rect(), mOp);
232 break;
233 case Type::RRect:
234 canvas->clipRRect(mRRect, mOp);
235 break;
236 case Type::Path:
237 canvas->clipPath(*mPath.get(), mOp);
238 break;
Stan Ilievf50806a2016-10-24 10:40:39 -0400239 }
240 }
241
242private:
243 enum class Type {
244 Rect,
245 RRect,
246 Path,
247 };
248
John Reck1bcacfd2017-11-03 10:12:19 -0700249 Type mType;
250 SkClipOp mOp;
251 SkMatrix mMatrix;
Stan Ilievf50806a2016-10-24 10:40:39 -0400252
253 // These are logically a union (tracked separately due to non-POD path).
254 SkTLazy<SkPath> mPath;
John Reck1bcacfd2017-11-03 10:12:19 -0700255 SkRRect mRRect;
Stan Ilievf50806a2016-10-24 10:40:39 -0400256};
257
258const SkiaCanvas::SaveRec* SkiaCanvas::currentSaveRec() const {
John Reck1bcacfd2017-11-03 10:12:19 -0700259 const SaveRec* rec = mSaveStack ? static_cast<const SaveRec*>(mSaveStack->back()) : nullptr;
Stan Ilievf50806a2016-10-24 10:40:39 -0400260 int currentSaveCount = mCanvas->getSaveCount();
261 SkASSERT(!rec || currentSaveCount >= rec->saveCount);
262
263 return (rec && rec->saveCount == currentSaveCount) ? rec : nullptr;
264}
265
Derek Sollenberger8872b382014-06-23 14:13:53 -0400266// ----------------------------------------------------------------------------
267// functions to emulate legacy SaveFlags (i.e. independent matrix/clip flags)
268// ----------------------------------------------------------------------------
269
Florin Malitaeecff562015-12-21 10:43:01 -0500270void SkiaCanvas::recordPartialSave(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400271 // A partial save is a save operation which doesn't capture the full canvas state.
Florin Malitaeecff562015-12-21 10:43:01 -0500272 // (either SaveFlags::Matrix or SaveFlags::Clip is missing).
Derek Sollenberger8872b382014-06-23 14:13:53 -0400273
274 // Mask-out non canvas state bits.
Florin Malitaeecff562015-12-21 10:43:01 -0500275 flags &= SaveFlags::MatrixClip;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400276
Florin Malitaeecff562015-12-21 10:43:01 -0500277 if (flags == SaveFlags::MatrixClip) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400278 // not a partial save.
279 return;
280 }
281
Stan Ilievf50806a2016-10-24 10:40:39 -0400282 if (!mSaveStack) {
Ben Wagnerd1cbc162015-08-19 12:45:09 -0400283 mSaveStack.reset(new SkDeque(sizeof(struct SaveRec), 8));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400284 }
285
286 SaveRec* rec = static_cast<SaveRec*>(mSaveStack->push_back());
Florin Malita5e271402015-11-04 14:36:02 -0500287 rec->saveCount = mCanvas->getSaveCount();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400288 rec->saveFlags = flags;
Stan Ilievf50806a2016-10-24 10:40:39 -0400289 rec->clipIndex = mClipStack.size();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400290}
291
Stan Ilievf50806a2016-10-24 10:40:39 -0400292template <typename T>
Mike Reed6e49c9f2016-12-02 15:36:59 -0500293void SkiaCanvas::recordClip(const T& clip, SkClipOp op) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400294 // Only need tracking when in a partial save frame which
295 // doesn't restore the clip.
296 const SaveRec* rec = this->currentSaveRec();
297 if (rec && !(rec->saveFlags & SaveFlags::Clip)) {
298 mClipStack.emplace_back(clip, op, mCanvas->getTotalMatrix());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400299 }
300}
301
Stan Ilievf50806a2016-10-24 10:40:39 -0400302// Applies and optionally removes all clips >= index.
303void SkiaCanvas::applyPersistentClips(size_t clipStartIndex) {
304 SkASSERT(clipStartIndex <= mClipStack.size());
305 const auto begin = mClipStack.cbegin() + clipStartIndex;
306 const auto end = mClipStack.cend();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400307
Stan Ilievf50806a2016-10-24 10:40:39 -0400308 // Clip application mutates the CTM.
309 const SkMatrix saveMatrix = mCanvas->getTotalMatrix();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400310
Stan Ilievf50806a2016-10-24 10:40:39 -0400311 for (auto clip = begin; clip != end; ++clip) {
Mike Reed6acfe162016-11-18 17:21:09 -0500312 clip->apply(mCanvas);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400313 }
314
Stan Ilievf50806a2016-10-24 10:40:39 -0400315 mCanvas->setMatrix(saveMatrix);
316
317 // If the current/post-restore save rec is also persisting clips, we
318 // leave them on the stack to be reapplied part of the next restore().
319 // Otherwise we're done and just pop them.
320 const auto* rec = this->currentSaveRec();
321 if (!rec || (rec->saveFlags & SaveFlags::Clip)) {
322 mClipStack.erase(begin, end);
323 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400324}
325
326// ----------------------------------------------------------------------------
327// Canvas state operations: Matrix
328// ----------------------------------------------------------------------------
329
330void SkiaCanvas::getMatrix(SkMatrix* outMatrix) const {
331 *outMatrix = mCanvas->getTotalMatrix();
332}
333
334void SkiaCanvas::setMatrix(const SkMatrix& matrix) {
335 mCanvas->setMatrix(matrix);
336}
337
338void SkiaCanvas::concat(const SkMatrix& matrix) {
339 mCanvas->concat(matrix);
340}
341
342void SkiaCanvas::rotate(float degrees) {
343 mCanvas->rotate(degrees);
344}
345
346void SkiaCanvas::scale(float sx, float sy) {
347 mCanvas->scale(sx, sy);
348}
349
350void SkiaCanvas::skew(float sx, float sy) {
351 mCanvas->skew(sx, sy);
352}
353
354void SkiaCanvas::translate(float dx, float dy) {
355 mCanvas->translate(dx, dy);
356}
357
358// ----------------------------------------------------------------------------
359// Canvas state operations: Clips
360// ----------------------------------------------------------------------------
361
362// This function is a mirror of SkCanvas::getClipBounds except that it does
363// not outset the edge of the clip to account for anti-aliasing. There is
364// a skia bug to investigate pushing this logic into back into skia.
365// (see https://code.google.com/p/skia/issues/detail?id=1303)
366bool SkiaCanvas::getClipBounds(SkRect* outRect) const {
367 SkIRect ibounds;
Mike Reed5e438982017-01-25 08:23:25 -0500368 if (!mCanvas->getDeviceClipBounds(&ibounds)) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400369 return false;
370 }
371
372 SkMatrix inverse;
373 // if we can't invert the CTM, we can't return local clip bounds
374 if (!mCanvas->getTotalMatrix().invert(&inverse)) {
375 if (outRect) {
376 outRect->setEmpty();
377 }
378 return false;
379 }
380
381 if (NULL != outRect) {
382 SkRect r = SkRect::Make(ibounds);
383 inverse.mapRect(outRect, r);
384 }
385 return true;
386}
387
388bool SkiaCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
389 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
390 return mCanvas->quickReject(bounds);
391}
392
393bool SkiaCanvas::quickRejectPath(const SkPath& path) const {
394 return mCanvas->quickReject(path);
395}
396
Mike Reed6e49c9f2016-12-02 15:36:59 -0500397bool SkiaCanvas::clipRect(float left, float top, float right, float bottom, SkClipOp op) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400398 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Stan Ilievf50806a2016-10-24 10:40:39 -0400399 this->recordClip(rect, op);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400400 mCanvas->clipRect(rect, op);
Chris Craik5ec6a282015-06-23 15:42:12 -0700401 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400402}
403
Mike Reed6e49c9f2016-12-02 15:36:59 -0500404bool SkiaCanvas::clipPath(const SkPath* path, SkClipOp op) {
Derek Sollenbergerf7d98f42017-04-17 11:27:36 -0400405 this->recordClip(*path, op);
406 mCanvas->clipPath(*path, op);
Chris Craik5ec6a282015-06-23 15:42:12 -0700407 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400408}
409
Derek Sollenberger8872b382014-06-23 14:13:53 -0400410// ----------------------------------------------------------------------------
411// Canvas state operations: Filters
412// ----------------------------------------------------------------------------
413
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400414SkDrawFilter* SkiaCanvas::getDrawFilter() {
415 return mCanvas->getDrawFilter();
416}
417
Derek Sollenberger8872b382014-06-23 14:13:53 -0400418void SkiaCanvas::setDrawFilter(SkDrawFilter* drawFilter) {
419 mCanvas->setDrawFilter(drawFilter);
420}
421
422// ----------------------------------------------------------------------------
Matt Sarettd0814db2017-04-13 09:33:18 -0400423// Canvas state operations: Capture
424// ----------------------------------------------------------------------------
425
426SkCanvasState* SkiaCanvas::captureCanvasState() const {
427 SkCanvas* canvas = mCanvas;
428 if (mCanvasOwned) {
429 // Important to use the underlying SkCanvas, not the wrapper.
430 canvas = mCanvasOwned.get();
431 }
432
433 // Workarounds for http://crbug.com/271096: SW draw only supports
434 // translate & scale transforms, and a simple rectangular clip.
435 // (This also avoids significant wasted time in calling
436 // SkCanvasStateUtils::CaptureCanvasState when the clip is complex).
John Reck1bcacfd2017-11-03 10:12:19 -0700437 if (!canvas->isClipRect() || (canvas->getTotalMatrix().getType() &
438 ~(SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask))) {
439 return nullptr;
Matt Sarettd0814db2017-04-13 09:33:18 -0400440 }
441
442 return SkCanvasStateUtils::CaptureCanvasState(canvas);
443}
444
445// ----------------------------------------------------------------------------
Derek Sollenberger8872b382014-06-23 14:13:53 -0400446// Canvas draw operations
447// ----------------------------------------------------------------------------
448
Mike Reed260ab722016-10-07 15:59:20 -0400449void SkiaCanvas::drawColor(int color, SkBlendMode mode) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400450 mCanvas->drawColor(color, mode);
451}
452
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400453void SkiaCanvas::drawPaint(const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400454 mCanvas->drawPaint(paint);
455}
456
457// ----------------------------------------------------------------------------
458// Canvas draw operations: Geometry
459// ----------------------------------------------------------------------------
460
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400461void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400462 SkCanvas::PointMode mode) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500463 if (CC_UNLIKELY(count < 2 || paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400464 // convert the floats into SkPoints
John Reck1bcacfd2017-11-03 10:12:19 -0700465 count >>= 1; // now it is the number of points
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400466 std::unique_ptr<SkPoint[]> pts(new SkPoint[count]);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400467 for (int i = 0; i < count; i++) {
468 pts[i].set(points[0], points[1]);
469 points += 2;
470 }
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400471 mCanvas->drawPoints(mode, count, pts.get(), paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400472}
473
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400474void SkiaCanvas::drawPoint(float x, float y, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400475 mCanvas->drawPoint(x, y, paint);
476}
477
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400478void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400479 this->drawPoints(points, count, paint, SkCanvas::kPoints_PointMode);
480}
481
482void SkiaCanvas::drawLine(float startX, float startY, float stopX, float stopY,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400483 const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400484 mCanvas->drawLine(startX, startY, stopX, stopY, paint);
485}
486
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400487void SkiaCanvas::drawLines(const float* points, int count, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500488 if (CC_UNLIKELY(count < 4 || paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400489 this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode);
490}
491
John Reck1bcacfd2017-11-03 10:12:19 -0700492void SkiaCanvas::drawRect(float left, float top, float right, float bottom, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500493 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Mike Reed6c9bb242017-04-04 09:15:37 -0400494 mCanvas->drawRect({left, top, right, bottom}, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400495}
496
Derek Sollenberger94394b32015-07-10 09:58:41 -0400497void SkiaCanvas::drawRegion(const SkRegion& region, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500498 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Stan Ilievf50806a2016-10-24 10:40:39 -0400499 mCanvas->drawRegion(region, paint);
Derek Sollenberger94394b32015-07-10 09:58:41 -0400500}
501
John Reck1bcacfd2017-11-03 10:12:19 -0700502void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom, float rx, float ry,
503 const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500504 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400505 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
506 mCanvas->drawRoundRect(rect, rx, ry, paint);
507}
508
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400509void SkiaCanvas::drawCircle(float x, float y, float radius, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500510 if (CC_UNLIKELY(radius <= 0 || paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400511 mCanvas->drawCircle(x, y, radius, paint);
512}
513
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400514void SkiaCanvas::drawOval(float left, float top, float right, float bottom, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500515 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400516 SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
517 mCanvas->drawOval(oval, paint);
518}
519
John Reck1bcacfd2017-11-03 10:12:19 -0700520void SkiaCanvas::drawArc(float left, float top, float right, float bottom, float startAngle,
521 float sweepAngle, bool useCenter, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500522 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400523 SkRect arc = SkRect::MakeLTRB(left, top, right, bottom);
Derek Sollenbergeref3b2182017-11-06 13:55:59 -0500524 if (fabs(sweepAngle) >= 360.0f) {
525 mCanvas->drawOval(arc, paint);
526 } else {
527 mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, paint);
528 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400529}
530
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400531void SkiaCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500532 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Stan Iliev6dcfdec2017-08-15 16:42:05 -0400533 if (CC_UNLIKELY(path.isEmpty() && (!path.isInverseFillType()))) {
534 return;
535 }
Derek Sollenbergerd7f13f82017-03-09 13:08:27 -0500536 mCanvas->drawPath(path, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400537}
538
Mike Reed826deef2017-04-04 15:32:04 -0400539void SkiaCanvas::drawVertices(const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint) {
540 mCanvas->drawVertices(vertices, mode, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400541}
542
543// ----------------------------------------------------------------------------
544// Canvas draw operations: Bitmaps
545// ----------------------------------------------------------------------------
546
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400547const SkPaint* SkiaCanvas::addFilter(const SkPaint* origPaint, SkPaint* tmpPaint,
John Reck1bcacfd2017-11-03 10:12:19 -0700548 sk_sp<SkColorFilter> colorSpaceFilter) {
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400549 /* We don't apply the colorSpace filter if this canvas is already wrapped with
550 * a SkColorSpaceXformCanvas since it already takes care of converting the
551 * contents of the bitmap into the appropriate colorspace. The mCanvasWrapper
552 * should only be used if this canvas is backed by a surface/bitmap that is known
553 * to have a non-sRGB colorspace.
554 */
555 if (!mCanvasWrapper && colorSpaceFilter) {
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400556 if (origPaint) {
557 *tmpPaint = *origPaint;
558 }
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400559
560 if (tmpPaint->getColorFilter()) {
John Reck1bcacfd2017-11-03 10:12:19 -0700561 tmpPaint->setColorFilter(
562 SkColorFilter::MakeComposeFilter(tmpPaint->refColorFilter(), colorSpaceFilter));
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400563 LOG_ALWAYS_FATAL_IF(!tmpPaint->getColorFilter());
564 } else {
565 tmpPaint->setColorFilter(colorSpaceFilter);
566 }
567
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400568 return tmpPaint;
569 } else {
570 return origPaint;
571 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400572}
573
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400574void SkiaCanvas::drawBitmap(Bitmap& bitmap, float left, float top, const SkPaint* paint) {
575 SkPaint tmpPaint;
576 sk_sp<SkColorFilter> colorFilter;
577 sk_sp<SkImage> image = bitmap.makeImage(&colorFilter);
578 mCanvas->drawImage(image, left, top, addFilter(paint, &tmpPaint, colorFilter));
579}
580
581void SkiaCanvas::drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const SkPaint* paint) {
Mike Reed6acfe162016-11-18 17:21:09 -0500582 SkAutoCanvasRestore acr(mCanvas, true);
Mike Reed70ffbf92014-12-08 17:03:30 -0500583 mCanvas->concat(matrix);
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400584
585 SkPaint tmpPaint;
586 sk_sp<SkColorFilter> colorFilter;
587 sk_sp<SkImage> image = bitmap.makeImage(&colorFilter);
588 mCanvas->drawImage(image, 0, 0, addFilter(paint, &tmpPaint, colorFilter));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400589}
590
John Reck1bcacfd2017-11-03 10:12:19 -0700591void SkiaCanvas::drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight,
592 float srcBottom, float dstLeft, float dstTop, float dstRight,
593 float dstBottom, const SkPaint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400594 SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
595 SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400596
597 SkPaint tmpPaint;
598 sk_sp<SkColorFilter> colorFilter;
599 sk_sp<SkImage> image = bitmap.makeImage(&colorFilter);
Derek Sollenberger6c2a9e22017-08-15 16:23:01 -0400600 mCanvas->drawImageRect(image, srcRect, dstRect, addFilter(paint, &tmpPaint, colorFilter),
John Reck1bcacfd2017-11-03 10:12:19 -0700601 SkCanvas::kFast_SrcRectConstraint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400602}
603
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400604void SkiaCanvas::drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight,
John Reck1bcacfd2017-11-03 10:12:19 -0700605 const float* vertices, const int* colors, const SkPaint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400606 const int ptCount = (meshWidth + 1) * (meshHeight + 1);
607 const int indexCount = meshWidth * meshHeight * 6;
Mike Reed871cd2d2017-03-17 10:15:52 -0400608 uint32_t flags = SkVertices::kHasTexCoords_BuilderFlag;
609 if (colors) {
610 flags |= SkVertices::kHasColors_BuilderFlag;
611 }
Mike Reed826deef2017-04-04 15:32:04 -0400612 SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, ptCount, indexCount, flags);
Mike Reed871cd2d2017-03-17 10:15:52 -0400613 memcpy(builder.positions(), vertices, ptCount * sizeof(SkPoint));
614 if (colors) {
615 memcpy(builder.colors(), colors, ptCount * sizeof(SkColor));
616 }
617 SkPoint* texs = builder.texCoords();
618 uint16_t* indices = builder.indices();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400619
620 // cons up texture coordinates and indices
621 {
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400622 const SkScalar w = SkIntToScalar(bitmap.width());
623 const SkScalar h = SkIntToScalar(bitmap.height());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400624 const SkScalar dx = w / meshWidth;
625 const SkScalar dy = h / meshHeight;
626
627 SkPoint* texsPtr = texs;
628 SkScalar y = 0;
629 for (int i = 0; i <= meshHeight; i++) {
630 if (i == meshHeight) {
631 y = h; // to ensure numerically we hit h exactly
632 }
633 SkScalar x = 0;
634 for (int j = 0; j < meshWidth; j++) {
635 texsPtr->set(x, y);
636 texsPtr += 1;
637 x += dx;
638 }
639 texsPtr->set(w, y);
640 texsPtr += 1;
641 y += dy;
642 }
643 SkASSERT(texsPtr - texs == ptCount);
644 }
645
646 // cons up indices
647 {
648 uint16_t* indexPtr = indices;
649 int index = 0;
650 for (int i = 0; i < meshHeight; i++) {
651 for (int j = 0; j < meshWidth; j++) {
652 // lower-left triangle
653 *indexPtr++ = index;
654 *indexPtr++ = index + meshWidth + 1;
655 *indexPtr++ = index + meshWidth + 2;
656 // upper-right triangle
657 *indexPtr++ = index;
658 *indexPtr++ = index + meshWidth + 2;
659 *indexPtr++ = index + 1;
660 // bump to the next cell
661 index += 1;
662 }
663 // bump to the next row
664 index += 1;
665 }
666 SkASSERT(indexPtr - indices == indexCount);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400667 }
668
John Reck1bcacfd2017-11-03 10:12:19 -0700669// double-check that we have legal indices
Derek Sollenberger8872b382014-06-23 14:13:53 -0400670#ifdef SK_DEBUG
671 {
672 for (int i = 0; i < indexCount; i++) {
673 SkASSERT((unsigned)indices[i] < (unsigned)ptCount);
674 }
675 }
676#endif
677
678 // cons-up a shader for the bitmap
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400679 SkPaint tmpPaint;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400680 if (paint) {
681 tmpPaint = *paint;
682 }
Stan Ilievf50806a2016-10-24 10:40:39 -0400683
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400684 sk_sp<SkColorFilter> colorFilter;
685 sk_sp<SkImage> image = bitmap.makeImage(&colorFilter);
John Reck1bcacfd2017-11-03 10:12:19 -0700686 sk_sp<SkShader> shader =
687 image->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
688 if (colorFilter) {
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400689 shader = shader->makeWithColorFilter(colorFilter);
690 }
691 tmpPaint.setShader(shader);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400692
Mike Reed871cd2d2017-03-17 10:15:52 -0400693 mCanvas->drawVertices(builder.detach(), SkBlendMode::kModulate, tmpPaint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400694}
695
John Reck1bcacfd2017-11-03 10:12:19 -0700696void SkiaCanvas::drawNinePatch(Bitmap& bitmap, const Res_png_9patch& chunk, float dstLeft,
697 float dstTop, float dstRight, float dstBottom,
698 const SkPaint* paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400699 SkCanvas::Lattice lattice;
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400700 NinePatchUtils::SetLatticeDivs(&lattice, chunk, bitmap.width(), bitmap.height());
Stan Ilievf50806a2016-10-24 10:40:39 -0400701
702 lattice.fFlags = nullptr;
703 int numFlags = 0;
Stan Iliev021693b2016-10-17 16:26:15 -0400704 if (chunk.numColors > 0 && chunk.numColors == NinePatchUtils::NumDistinctRects(lattice)) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400705 // We can expect the framework to give us a color for every distinct rect.
706 // Skia requires a flag for every rect.
707 numFlags = (lattice.fXCount + 1) * (lattice.fYCount + 1);
708 }
709
710 SkAutoSTMalloc<25, SkCanvas::Lattice::Flags> flags(numFlags);
711 if (numFlags > 0) {
Stan Iliev021693b2016-10-17 16:26:15 -0400712 NinePatchUtils::SetLatticeFlags(&lattice, flags.get(), numFlags, chunk);
Stan Ilievf50806a2016-10-24 10:40:39 -0400713 }
714
715 lattice.fBounds = nullptr;
716 SkRect dst = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400717
718 SkPaint tmpPaint;
719 sk_sp<SkColorFilter> colorFilter;
720 sk_sp<SkImage> image = bitmap.makeImage(&colorFilter);
721 mCanvas->drawImageLattice(image.get(), lattice, dst, addFilter(paint, &tmpPaint, colorFilter));
Derek Sollenbergeredca3202015-07-10 13:56:39 -0400722}
723
Doris Liu766431a2016-02-04 22:17:11 +0000724void SkiaCanvas::drawVectorDrawable(VectorDrawableRoot* vectorDrawable) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800725 vectorDrawable->drawStaging(this);
Doris Liu766431a2016-02-04 22:17:11 +0000726}
727
Derek Sollenberger8872b382014-06-23 14:13:53 -0400728// ----------------------------------------------------------------------------
729// Canvas draw operations: Text
730// ----------------------------------------------------------------------------
731
Stan Iliev0b58d992017-03-30 18:22:27 -0400732void SkiaCanvas::drawGlyphs(ReadGlyphFunc glyphFunc, int count, const SkPaint& paint, float x,
John Reck1bcacfd2017-11-03 10:12:19 -0700733 float y, float boundsLeft, float boundsTop, float boundsRight,
734 float boundsBottom, float totalAdvance) {
Stan Iliev0b58d992017-03-30 18:22:27 -0400735 if (count <= 0 || paint.nothingToDraw()) return;
Stan Ilievf50806a2016-10-24 10:40:39 -0400736 // Set align to left for drawing, as we don't want individual
737 // glyphs centered or right-aligned; the offset above takes
738 // care of all alignment.
739 SkPaint paintCopy(paint);
740 paintCopy.setTextAlign(SkPaint::kLeft_Align);
Stan Ilieva39b7742017-11-29 13:15:45 -0500741 SkASSERT(paintCopy.getTextEncoding() == SkPaint::kGlyphID_TextEncoding);
Stan Ilievf50806a2016-10-24 10:40:39 -0400742
John Reck1bcacfd2017-11-03 10:12:19 -0700743 SkRect bounds =
744 SkRect::MakeLTRB(boundsLeft + x, boundsTop + y, boundsRight + x, boundsBottom + y);
Stan Ilievf50806a2016-10-24 10:40:39 -0400745
746 SkTextBlobBuilder builder;
747 const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(paintCopy, count, &bounds);
Stan Iliev0b58d992017-03-30 18:22:27 -0400748 glyphFunc(buffer.glyphs, buffer.pos);
Stan Ilievf50806a2016-10-24 10:40:39 -0400749
750 sk_sp<SkTextBlob> textBlob(builder.make());
751 mCanvas->drawTextBlob(textBlob, 0, 0, paintCopy);
752 drawTextDecorations(x, y, totalAdvance, paintCopy);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400753}
754
Yuqian Liafc221492016-07-18 13:07:42 -0400755void SkiaCanvas::drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
John Reck1bcacfd2017-11-03 10:12:19 -0700756 const SkPaint& paint, const SkPath& path, size_t start,
757 size_t end) {
Yuqian Liafc221492016-07-18 13:07:42 -0400758 const int N = end - start;
Derek Sollenbergere547dd02016-11-09 11:55:59 -0500759 SkAutoSTMalloc<1024, uint8_t> storage(N * (sizeof(uint16_t) + sizeof(SkRSXform)));
Yuqian Liafc221492016-07-18 13:07:42 -0400760 SkRSXform* xform = (SkRSXform*)storage.get();
761 uint16_t* glyphs = (uint16_t*)(xform + N);
762 SkPathMeasure meas(path, false);
763
764 for (size_t i = start; i < end; i++) {
765 glyphs[i - start] = layout.getGlyphId(i);
766 float x = hOffset + layout.getX(i);
767 float y = vOffset + layout.getY(i);
768
769 SkPoint pos;
770 SkVector tan;
771 if (!meas.getPosTan(x, &pos, &tan)) {
772 pos.set(x, y);
773 tan.set(1, 0);
774 }
775 xform[i - start].fSCos = tan.x();
776 xform[i - start].fSSin = tan.y();
John Reck1bcacfd2017-11-03 10:12:19 -0700777 xform[i - start].fTx = pos.x() - tan.y() * y;
778 xform[i - start].fTy = pos.y() + tan.x() * y;
Yuqian Liafc221492016-07-18 13:07:42 -0400779 }
780
781 this->asSkCanvas()->drawTextRSXform(glyphs, sizeof(uint16_t) * N, xform, nullptr, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400782}
783
Derek Sollenberger6f485562015-07-30 10:00:39 -0400784// ----------------------------------------------------------------------------
785// Canvas draw operations: Animations
786// ----------------------------------------------------------------------------
787
Derek Sollenberger6f485562015-07-30 10:00:39 -0400788void SkiaCanvas::drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
John Reck1bcacfd2017-11-03 10:12:19 -0700789 uirenderer::CanvasPropertyPrimitive* top,
790 uirenderer::CanvasPropertyPrimitive* right,
791 uirenderer::CanvasPropertyPrimitive* bottom,
792 uirenderer::CanvasPropertyPrimitive* rx,
793 uirenderer::CanvasPropertyPrimitive* ry,
794 uirenderer::CanvasPropertyPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -0400795 sk_sp<uirenderer::skiapipeline::AnimatedRoundRect> drawable(
John Reck1bcacfd2017-11-03 10:12:19 -0700796 new uirenderer::skiapipeline::AnimatedRoundRect(left, top, right, bottom, rx, ry,
797 paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400798 mCanvas->drawDrawable(drawable.get());
799}
800
John Reck1bcacfd2017-11-03 10:12:19 -0700801void SkiaCanvas::drawCircle(uirenderer::CanvasPropertyPrimitive* x,
802 uirenderer::CanvasPropertyPrimitive* y,
803 uirenderer::CanvasPropertyPrimitive* radius,
804 uirenderer::CanvasPropertyPaint* paint) {
805 sk_sp<uirenderer::skiapipeline::AnimatedCircle> drawable(
806 new uirenderer::skiapipeline::AnimatedCircle(x, y, radius, paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400807 mCanvas->drawDrawable(drawable.get());
808}
809
810// ----------------------------------------------------------------------------
811// Canvas draw operations: View System
812// ----------------------------------------------------------------------------
813
Stan Ilievf50806a2016-10-24 10:40:39 -0400814void SkiaCanvas::drawLayer(uirenderer::DeferredLayerUpdater* layerUpdater) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400815 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw Layers");
816}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400817
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400818void SkiaCanvas::drawRenderNode(uirenderer::RenderNode* renderNode) {
819 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw RenderNodes");
820}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400821
John Reckcd1c3eb2016-04-14 10:38:54 -0700822void SkiaCanvas::callDrawGLFunction(Functor* functor,
John Reck1bcacfd2017-11-03 10:12:19 -0700823 uirenderer::GlFunctorLifecycleListener* listener) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400824 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw GL Content");
825}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400826
John Reck1bcacfd2017-11-03 10:12:19 -0700827} // namespace android