blob: 7b41f89605d24299a6d508651f84a350fbdcd0fa [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
Leon Scroggins III671cce22018-01-14 16:52:17 -050026#include <SkAnimatedImage.h>
Matt Sarettd0814db2017-04-13 09:33:18 -040027#include <SkCanvasStateUtils.h>
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -040028#include <SkColorFilter.h>
Matt Sarettea70d222017-03-29 16:25:10 -040029#include <SkColorSpaceXformCanvas.h>
John Reck849911a2015-01-20 07:51:14 -080030#include <SkDeque.h>
31#include <SkDrawFilter.h>
John Reck1bcacfd2017-11-03 10:12:19 -070032#include <SkDrawable.h>
John Reck849911a2015-01-20 07:51:14 -080033#include <SkGraphics.h>
Derek Sollenberger6f485562015-07-30 10:00:39 -040034#include <SkImage.h>
Matt Sarett62feb3a2016-09-20 10:34:11 -040035#include <SkImagePriv.h>
Leon Scroggins III671cce22018-01-14 16:52:17 -050036#include <SkPicture.h>
Yuqian Liafc221492016-07-18 13:07:42 -040037#include <SkRSXform.h>
John Reck849911a2015-01-20 07:51:14 -080038#include <SkShader.h>
John Reck849911a2015-01-20 07:51:14 -080039#include <SkTemplates.h>
Stan Ilievf50806a2016-10-24 10:40:39 -040040#include <SkTextBlob.h>
Derek Sollenberger8872b382014-06-23 14:13:53 -040041
Ben Wagner60126ef2015-08-07 12:13:48 -040042#include <memory>
43
Derek Sollenberger8872b382014-06-23 14:13:53 -040044namespace android {
45
Stan Ilievf50806a2016-10-24 10:40:39 -040046using uirenderer::PaintUtils;
47
John Reckc1b33d62015-04-22 09:04:45 -070048Canvas* Canvas::create_canvas(const SkBitmap& bitmap) {
Derek Sollenberger8872b382014-06-23 14:13:53 -040049 return new SkiaCanvas(bitmap);
50}
51
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040052Canvas* Canvas::create_canvas(SkCanvas* skiaCanvas) {
53 return new SkiaCanvas(skiaCanvas);
Derek Sollenberger8872b382014-06-23 14:13:53 -040054}
55
Stan Ilievf50806a2016-10-24 10:40:39 -040056SkiaCanvas::SkiaCanvas() {}
57
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040058SkiaCanvas::SkiaCanvas(SkCanvas* canvas) : mCanvas(canvas) {}
Stan Ilievf50806a2016-10-24 10:40:39 -040059
John Reckc1b33d62015-04-22 09:04:45 -070060SkiaCanvas::SkiaCanvas(const SkBitmap& bitmap) {
Romain Guy82426562017-04-04 19:38:50 -070061 sk_sp<SkColorSpace> cs = bitmap.refColorSpace();
Matt Sarettca9b7032017-04-13 12:18:47 -040062 mCanvasOwned =
63 std::unique_ptr<SkCanvas>(new SkCanvas(bitmap, SkCanvas::ColorBehavior::kLegacy));
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040064 if (cs.get() == nullptr || cs->isSRGB()) {
John Reck1072fff2018-04-12 15:20:09 -070065 mCanvas = mCanvasOwned.get();
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040066 } else {
67 /** The wrapper is needed if we are drawing into a non-sRGB destination, since
68 * we need to transform all colors (not just bitmaps via filters) into the
69 * destination's colorspace.
70 */
71 mCanvasWrapper = SkCreateColorSpaceXformCanvas(mCanvasOwned.get(), std::move(cs));
72 mCanvas = mCanvasWrapper.get();
73 }
Derek Sollenberger8872b382014-06-23 14:13:53 -040074}
75
Stan Iliev021693b2016-10-17 16:26:15 -040076SkiaCanvas::~SkiaCanvas() {}
77
Derek Sollenbergerc1908132016-07-15 10:28:16 -040078void SkiaCanvas::reset(SkCanvas* skiaCanvas) {
Mike Reed6acfe162016-11-18 17:21:09 -050079 if (mCanvas != skiaCanvas) {
80 mCanvas = skiaCanvas;
81 mCanvasOwned.reset();
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040082 mCanvasWrapper.reset();
Mike Reed6acfe162016-11-18 17:21:09 -050083 }
Derek Sollenbergerc1908132016-07-15 10:28:16 -040084 mSaveStack.reset(nullptr);
Derek Sollenbergerc1908132016-07-15 10:28:16 -040085}
86
Derek Sollenberger8872b382014-06-23 14:13:53 -040087// ----------------------------------------------------------------------------
88// Canvas state operations: Replace Bitmap
89// ----------------------------------------------------------------------------
90
John Reckc1b33d62015-04-22 09:04:45 -070091void SkiaCanvas::setBitmap(const SkBitmap& bitmap) {
Romain Guy82426562017-04-04 19:38:50 -070092 sk_sp<SkColorSpace> cs = bitmap.refColorSpace();
Matt Sarettca9b7032017-04-13 12:18:47 -040093 std::unique_ptr<SkCanvas> newCanvas =
94 std::unique_ptr<SkCanvas>(new SkCanvas(bitmap, SkCanvas::ColorBehavior::kLegacy));
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040095 std::unique_ptr<SkCanvas> newCanvasWrapper;
96 if (cs.get() != nullptr && !cs->isSRGB()) {
97 newCanvasWrapper = SkCreateColorSpaceXformCanvas(newCanvas.get(), std::move(cs));
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040098 }
Tony Mantler4f641d12017-03-14 22:36:14 +000099
Tony Mantler4f641d12017-03-14 22:36:14 +0000100 // deletes the previously owned canvas (if any)
Matt Sarettea70d222017-03-29 16:25:10 -0400101 mCanvasOwned = std::move(newCanvas);
102 mCanvasWrapper = std::move(newCanvasWrapper);
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400103 mCanvas = mCanvasWrapper ? mCanvasWrapper.get() : mCanvasOwned.get();
Tony Mantler4f641d12017-03-14 22:36:14 +0000104
Derek Sollenberger8872b382014-06-23 14:13:53 -0400105 // clean up the old save stack
Stan Ilievf50806a2016-10-24 10:40:39 -0400106 mSaveStack.reset(nullptr);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400107}
108
109// ----------------------------------------------------------------------------
110// Canvas state operations
111// ----------------------------------------------------------------------------
112
113bool SkiaCanvas::isOpaque() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400114 return mCanvas->imageInfo().isOpaque();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400115}
116
117int SkiaCanvas::width() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400118 return mCanvas->imageInfo().width();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400119}
120
121int SkiaCanvas::height() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400122 return mCanvas->imageInfo().height();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400123}
124
125// ----------------------------------------------------------------------------
126// Canvas state operations: Save (layer)
127// ----------------------------------------------------------------------------
128
129int SkiaCanvas::getSaveCount() const {
130 return mCanvas->getSaveCount();
131}
132
Florin Malitaeecff562015-12-21 10:43:01 -0500133int SkiaCanvas::save(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400134 int count = mCanvas->save();
135 recordPartialSave(flags);
136 return count;
137}
138
Florin Malita5e271402015-11-04 14:36:02 -0500139// The SkiaCanvas::restore operation layers on the capability to preserve
140// either (or both) the matrix and/or clip state after a SkCanvas::restore
141// operation. It does this by explicitly saving off the clip & matrix state
142// when requested and playing it back after the SkCanvas::restore.
Derek Sollenberger8872b382014-06-23 14:13:53 -0400143void SkiaCanvas::restore() {
Stan Ilievf50806a2016-10-24 10:40:39 -0400144 const auto* rec = this->currentSaveRec();
145 if (!rec) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400146 // Fast path - no record for this frame.
147 mCanvas->restore();
148 return;
149 }
150
Florin Malitaeecff562015-12-21 10:43:01 -0500151 bool preserveMatrix = !(rec->saveFlags & SaveFlags::Matrix);
John Reck1bcacfd2017-11-03 10:12:19 -0700152 bool preserveClip = !(rec->saveFlags & SaveFlags::Clip);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400153
154 SkMatrix savedMatrix;
155 if (preserveMatrix) {
156 savedMatrix = mCanvas->getTotalMatrix();
157 }
158
Stan Ilievf50806a2016-10-24 10:40:39 -0400159 const size_t clipIndex = rec->clipIndex;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400160
161 mCanvas->restore();
Stan Ilievf50806a2016-10-24 10:40:39 -0400162 mSaveStack->pop_back();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400163
164 if (preserveMatrix) {
165 mCanvas->setMatrix(savedMatrix);
166 }
167
Stan Ilievf50806a2016-10-24 10:40:39 -0400168 if (preserveClip) {
169 this->applyPersistentClips(clipIndex);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400170 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400171}
172
173void SkiaCanvas::restoreToCount(int restoreCount) {
174 while (mCanvas->getSaveCount() > restoreCount) {
175 this->restore();
176 }
177}
178
Florin Malitaeecff562015-12-21 10:43:01 -0500179static inline SkCanvas::SaveLayerFlags layerFlags(SaveFlags::Flags flags) {
180 SkCanvas::SaveLayerFlags layerFlags = 0;
181
Florin Malitaeecff562015-12-21 10:43:01 -0500182 if (!(flags & SaveFlags::ClipToLayer)) {
183 layerFlags |= SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag;
184 }
185
186 return layerFlags;
187}
188
John Reck1bcacfd2017-11-03 10:12:19 -0700189int SkiaCanvas::saveLayer(float left, float top, float right, float bottom, const SkPaint* paint,
190 SaveFlags::Flags flags) {
Florin Malitaeecff562015-12-21 10:43:01 -0500191 const SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
Derek Sollenbergerb8201192017-01-09 16:11:59 -0500192 const SkCanvas::SaveLayerRec rec(&bounds, paint, layerFlags(flags));
Florin Malitaeecff562015-12-21 10:43:01 -0500193
Stan Iliev68885e32016-12-14 11:18:34 -0500194 return mCanvas->saveLayer(rec);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400195}
196
John Reck1bcacfd2017-11-03 10:12:19 -0700197int SkiaCanvas::saveLayerAlpha(float left, float top, float right, float bottom, int alpha,
198 SaveFlags::Flags flags) {
Florin Malitaeecff562015-12-21 10:43:01 -0500199 if (static_cast<unsigned>(alpha) < 0xFF) {
Yuqian Lifd92ee42016-04-27 17:03:38 -0400200 SkPaint alphaPaint;
201 alphaPaint.setAlpha(alpha);
202 return this->saveLayer(left, top, right, bottom, &alphaPaint, flags);
Florin Malitaeecff562015-12-21 10:43:01 -0500203 }
Yuqian Lifd92ee42016-04-27 17:03:38 -0400204 return this->saveLayer(left, top, right, bottom, nullptr, flags);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400205}
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)
John Reck1bcacfd2017-11-03 10:12:19 -0700214 : mType(Type::Path), mOp(op), mMatrix(m), mPath(&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:
226 canvas->clipPath(*mPath.get(), mOp);
227 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).
243 SkTLazy<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
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400403SkDrawFilter* SkiaCanvas::getDrawFilter() {
404 return mCanvas->getDrawFilter();
405}
406
Derek Sollenberger8872b382014-06-23 14:13:53 -0400407void SkiaCanvas::setDrawFilter(SkDrawFilter* drawFilter) {
408 mCanvas->setDrawFilter(drawFilter);
409}
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
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400442void SkiaCanvas::drawPaint(const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400443 mCanvas->drawPaint(paint);
444}
445
446// ----------------------------------------------------------------------------
447// Canvas draw operations: Geometry
448// ----------------------------------------------------------------------------
449
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400450void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400451 SkCanvas::PointMode mode) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500452 if (CC_UNLIKELY(count < 2 || paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400453 // convert the floats into SkPoints
John Reck1bcacfd2017-11-03 10:12:19 -0700454 count >>= 1; // now it is the number of points
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400455 std::unique_ptr<SkPoint[]> pts(new SkPoint[count]);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400456 for (int i = 0; i < count; i++) {
457 pts[i].set(points[0], points[1]);
458 points += 2;
459 }
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400460 mCanvas->drawPoints(mode, count, pts.get(), paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400461}
462
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400463void SkiaCanvas::drawPoint(float x, float y, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400464 mCanvas->drawPoint(x, y, paint);
465}
466
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400467void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400468 this->drawPoints(points, count, paint, SkCanvas::kPoints_PointMode);
469}
470
471void SkiaCanvas::drawLine(float startX, float startY, float stopX, float stopY,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400472 const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400473 mCanvas->drawLine(startX, startY, stopX, stopY, paint);
474}
475
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400476void SkiaCanvas::drawLines(const float* points, int count, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500477 if (CC_UNLIKELY(count < 4 || paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400478 this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode);
479}
480
John Reck1bcacfd2017-11-03 10:12:19 -0700481void SkiaCanvas::drawRect(float left, float top, float right, float bottom, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500482 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Mike Reed6c9bb242017-04-04 09:15:37 -0400483 mCanvas->drawRect({left, top, right, bottom}, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400484}
485
Derek Sollenberger94394b32015-07-10 09:58:41 -0400486void SkiaCanvas::drawRegion(const SkRegion& region, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500487 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Stan Ilievf50806a2016-10-24 10:40:39 -0400488 mCanvas->drawRegion(region, paint);
Derek Sollenberger94394b32015-07-10 09:58:41 -0400489}
490
John Reck1bcacfd2017-11-03 10:12:19 -0700491void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom, float rx, float ry,
492 const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500493 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400494 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
495 mCanvas->drawRoundRect(rect, rx, ry, paint);
496}
497
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400498void SkiaCanvas::drawCircle(float x, float y, float radius, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500499 if (CC_UNLIKELY(radius <= 0 || paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400500 mCanvas->drawCircle(x, y, radius, paint);
501}
502
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400503void SkiaCanvas::drawOval(float left, float top, float right, float bottom, 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 oval = SkRect::MakeLTRB(left, top, right, bottom);
506 mCanvas->drawOval(oval, paint);
507}
508
John Reck1bcacfd2017-11-03 10:12:19 -0700509void SkiaCanvas::drawArc(float left, float top, float right, float bottom, float startAngle,
510 float sweepAngle, bool useCenter, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500511 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400512 SkRect arc = SkRect::MakeLTRB(left, top, right, bottom);
Derek Sollenbergeref3b2182017-11-06 13:55:59 -0500513 if (fabs(sweepAngle) >= 360.0f) {
514 mCanvas->drawOval(arc, paint);
515 } else {
516 mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, paint);
517 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400518}
519
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400520void SkiaCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500521 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Stan Iliev6dcfdec2017-08-15 16:42:05 -0400522 if (CC_UNLIKELY(path.isEmpty() && (!path.isInverseFillType()))) {
523 return;
524 }
Derek Sollenbergerd7f13f82017-03-09 13:08:27 -0500525 mCanvas->drawPath(path, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400526}
527
Mike Reed826deef2017-04-04 15:32:04 -0400528void SkiaCanvas::drawVertices(const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint) {
529 mCanvas->drawVertices(vertices, mode, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400530}
531
532// ----------------------------------------------------------------------------
533// Canvas draw operations: Bitmaps
534// ----------------------------------------------------------------------------
535
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400536const SkPaint* SkiaCanvas::addFilter(const SkPaint* origPaint, SkPaint* tmpPaint,
John Reck1bcacfd2017-11-03 10:12:19 -0700537 sk_sp<SkColorFilter> colorSpaceFilter) {
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400538 /* We don't apply the colorSpace filter if this canvas is already wrapped with
539 * a SkColorSpaceXformCanvas since it already takes care of converting the
540 * contents of the bitmap into the appropriate colorspace. The mCanvasWrapper
541 * should only be used if this canvas is backed by a surface/bitmap that is known
542 * to have a non-sRGB colorspace.
543 */
544 if (!mCanvasWrapper && colorSpaceFilter) {
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400545 if (origPaint) {
546 *tmpPaint = *origPaint;
547 }
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400548
549 if (tmpPaint->getColorFilter()) {
John Reck1bcacfd2017-11-03 10:12:19 -0700550 tmpPaint->setColorFilter(
551 SkColorFilter::MakeComposeFilter(tmpPaint->refColorFilter(), colorSpaceFilter));
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400552 LOG_ALWAYS_FATAL_IF(!tmpPaint->getColorFilter());
553 } else {
554 tmpPaint->setColorFilter(colorSpaceFilter);
555 }
556
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400557 return tmpPaint;
558 } else {
559 return origPaint;
560 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400561}
562
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400563void SkiaCanvas::drawBitmap(Bitmap& bitmap, float left, float top, const SkPaint* paint) {
564 SkPaint tmpPaint;
565 sk_sp<SkColorFilter> colorFilter;
566 sk_sp<SkImage> image = bitmap.makeImage(&colorFilter);
567 mCanvas->drawImage(image, left, top, addFilter(paint, &tmpPaint, colorFilter));
568}
569
570void SkiaCanvas::drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const SkPaint* paint) {
Mike Reed6acfe162016-11-18 17:21:09 -0500571 SkAutoCanvasRestore acr(mCanvas, true);
Mike Reed70ffbf92014-12-08 17:03:30 -0500572 mCanvas->concat(matrix);
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400573
574 SkPaint tmpPaint;
575 sk_sp<SkColorFilter> colorFilter;
576 sk_sp<SkImage> image = bitmap.makeImage(&colorFilter);
577 mCanvas->drawImage(image, 0, 0, addFilter(paint, &tmpPaint, colorFilter));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400578}
579
John Reck1bcacfd2017-11-03 10:12:19 -0700580void SkiaCanvas::drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight,
581 float srcBottom, float dstLeft, float dstTop, float dstRight,
582 float dstBottom, const SkPaint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400583 SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
584 SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400585
586 SkPaint tmpPaint;
587 sk_sp<SkColorFilter> colorFilter;
588 sk_sp<SkImage> image = bitmap.makeImage(&colorFilter);
Derek Sollenberger6c2a9e22017-08-15 16:23:01 -0400589 mCanvas->drawImageRect(image, srcRect, dstRect, addFilter(paint, &tmpPaint, colorFilter),
John Reck1bcacfd2017-11-03 10:12:19 -0700590 SkCanvas::kFast_SrcRectConstraint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400591}
592
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400593void SkiaCanvas::drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight,
John Reck1bcacfd2017-11-03 10:12:19 -0700594 const float* vertices, const int* colors, const SkPaint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400595 const int ptCount = (meshWidth + 1) * (meshHeight + 1);
596 const int indexCount = meshWidth * meshHeight * 6;
Mike Reed871cd2d2017-03-17 10:15:52 -0400597 uint32_t flags = SkVertices::kHasTexCoords_BuilderFlag;
598 if (colors) {
599 flags |= SkVertices::kHasColors_BuilderFlag;
600 }
Mike Reed826deef2017-04-04 15:32:04 -0400601 SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, ptCount, indexCount, flags);
Mike Reed871cd2d2017-03-17 10:15:52 -0400602 memcpy(builder.positions(), vertices, ptCount * sizeof(SkPoint));
603 if (colors) {
604 memcpy(builder.colors(), colors, ptCount * sizeof(SkColor));
605 }
606 SkPoint* texs = builder.texCoords();
607 uint16_t* indices = builder.indices();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400608
609 // cons up texture coordinates and indices
610 {
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400611 const SkScalar w = SkIntToScalar(bitmap.width());
612 const SkScalar h = SkIntToScalar(bitmap.height());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400613 const SkScalar dx = w / meshWidth;
614 const SkScalar dy = h / meshHeight;
615
616 SkPoint* texsPtr = texs;
617 SkScalar y = 0;
618 for (int i = 0; i <= meshHeight; i++) {
619 if (i == meshHeight) {
620 y = h; // to ensure numerically we hit h exactly
621 }
622 SkScalar x = 0;
623 for (int j = 0; j < meshWidth; j++) {
624 texsPtr->set(x, y);
625 texsPtr += 1;
626 x += dx;
627 }
628 texsPtr->set(w, y);
629 texsPtr += 1;
630 y += dy;
631 }
632 SkASSERT(texsPtr - texs == ptCount);
633 }
634
635 // cons up indices
636 {
637 uint16_t* indexPtr = indices;
638 int index = 0;
639 for (int i = 0; i < meshHeight; i++) {
640 for (int j = 0; j < meshWidth; j++) {
641 // lower-left triangle
642 *indexPtr++ = index;
643 *indexPtr++ = index + meshWidth + 1;
644 *indexPtr++ = index + meshWidth + 2;
645 // upper-right triangle
646 *indexPtr++ = index;
647 *indexPtr++ = index + meshWidth + 2;
648 *indexPtr++ = index + 1;
649 // bump to the next cell
650 index += 1;
651 }
652 // bump to the next row
653 index += 1;
654 }
655 SkASSERT(indexPtr - indices == indexCount);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400656 }
657
John Reck1bcacfd2017-11-03 10:12:19 -0700658// double-check that we have legal indices
Derek Sollenberger8872b382014-06-23 14:13:53 -0400659#ifdef SK_DEBUG
660 {
661 for (int i = 0; i < indexCount; i++) {
662 SkASSERT((unsigned)indices[i] < (unsigned)ptCount);
663 }
664 }
665#endif
666
667 // cons-up a shader for the bitmap
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400668 SkPaint tmpPaint;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400669 if (paint) {
670 tmpPaint = *paint;
671 }
Stan Ilievf50806a2016-10-24 10:40:39 -0400672
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400673 sk_sp<SkColorFilter> colorFilter;
674 sk_sp<SkImage> image = bitmap.makeImage(&colorFilter);
John Reck1bcacfd2017-11-03 10:12:19 -0700675 sk_sp<SkShader> shader =
676 image->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
677 if (colorFilter) {
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400678 shader = shader->makeWithColorFilter(colorFilter);
679 }
680 tmpPaint.setShader(shader);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400681
Mike Reed871cd2d2017-03-17 10:15:52 -0400682 mCanvas->drawVertices(builder.detach(), SkBlendMode::kModulate, tmpPaint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400683}
684
John Reck1bcacfd2017-11-03 10:12:19 -0700685void SkiaCanvas::drawNinePatch(Bitmap& bitmap, const Res_png_9patch& chunk, float dstLeft,
686 float dstTop, float dstRight, float dstBottom,
687 const SkPaint* paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400688 SkCanvas::Lattice lattice;
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400689 NinePatchUtils::SetLatticeDivs(&lattice, chunk, bitmap.width(), bitmap.height());
Stan Ilievf50806a2016-10-24 10:40:39 -0400690
Stan Ilieve12d7312017-12-04 14:48:27 -0500691 lattice.fRectTypes = nullptr;
692 lattice.fColors = nullptr;
Stan Ilievf50806a2016-10-24 10:40:39 -0400693 int numFlags = 0;
Stan Iliev021693b2016-10-17 16:26:15 -0400694 if (chunk.numColors > 0 && chunk.numColors == NinePatchUtils::NumDistinctRects(lattice)) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400695 // We can expect the framework to give us a color for every distinct rect.
696 // Skia requires a flag for every rect.
697 numFlags = (lattice.fXCount + 1) * (lattice.fYCount + 1);
698 }
699
Stan Ilieve12d7312017-12-04 14:48:27 -0500700 SkAutoSTMalloc<25, SkCanvas::Lattice::RectType> flags(numFlags);
701 SkAutoSTMalloc<25, SkColor> colors(numFlags);
Stan Ilievf50806a2016-10-24 10:40:39 -0400702 if (numFlags > 0) {
Stan Ilieve12d7312017-12-04 14:48:27 -0500703 NinePatchUtils::SetLatticeFlags(&lattice, flags.get(), numFlags, chunk, colors.get());
Stan Ilievf50806a2016-10-24 10:40:39 -0400704 }
705
706 lattice.fBounds = nullptr;
707 SkRect dst = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400708
709 SkPaint tmpPaint;
710 sk_sp<SkColorFilter> colorFilter;
711 sk_sp<SkImage> image = bitmap.makeImage(&colorFilter);
712 mCanvas->drawImageLattice(image.get(), lattice, dst, addFilter(paint, &tmpPaint, colorFilter));
Derek Sollenbergeredca3202015-07-10 13:56:39 -0400713}
714
Derek Sollenberger2d142132018-01-22 10:25:26 -0500715double SkiaCanvas::drawAnimatedImage(AnimatedImageDrawable* imgDrawable) {
716 return imgDrawable->drawStaging(mCanvas);
Leon Scroggins III671cce22018-01-14 16:52:17 -0500717}
718
Doris Liu766431a2016-02-04 22:17:11 +0000719void SkiaCanvas::drawVectorDrawable(VectorDrawableRoot* vectorDrawable) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800720 vectorDrawable->drawStaging(this);
Doris Liu766431a2016-02-04 22:17:11 +0000721}
722
Derek Sollenberger8872b382014-06-23 14:13:53 -0400723// ----------------------------------------------------------------------------
724// Canvas draw operations: Text
725// ----------------------------------------------------------------------------
726
Stan Iliev0b58d992017-03-30 18:22:27 -0400727void SkiaCanvas::drawGlyphs(ReadGlyphFunc glyphFunc, int count, const SkPaint& paint, float x,
John Reck1bcacfd2017-11-03 10:12:19 -0700728 float y, float boundsLeft, float boundsTop, float boundsRight,
729 float boundsBottom, float totalAdvance) {
Stan Iliev0b58d992017-03-30 18:22:27 -0400730 if (count <= 0 || paint.nothingToDraw()) return;
Stan Ilievf50806a2016-10-24 10:40:39 -0400731 // Set align to left for drawing, as we don't want individual
732 // glyphs centered or right-aligned; the offset above takes
733 // care of all alignment.
734 SkPaint paintCopy(paint);
735 paintCopy.setTextAlign(SkPaint::kLeft_Align);
Stan Ilieva39b7742017-11-29 13:15:45 -0500736 SkASSERT(paintCopy.getTextEncoding() == SkPaint::kGlyphID_TextEncoding);
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
John Reck1bcacfd2017-11-03 10:12:19 -0700744 SkRect bounds =
745 SkRect::MakeLTRB(boundsLeft + x, boundsTop + y, boundsRight + x, boundsBottom + y);
Stan Ilievf50806a2016-10-24 10:40:39 -0400746
747 SkTextBlobBuilder builder;
748 const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(paintCopy, count, &bounds);
Stan Iliev0b58d992017-03-30 18:22:27 -0400749 glyphFunc(buffer.glyphs, buffer.pos);
Stan Ilievf50806a2016-10-24 10:40:39 -0400750
751 sk_sp<SkTextBlob> textBlob(builder.make());
752 mCanvas->drawTextBlob(textBlob, 0, 0, paintCopy);
753 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,
John Reck1bcacfd2017-11-03 10:12:19 -0700757 const SkPaint& paint, const SkPath& path, size_t start,
758 size_t end) {
Derek Sollenberger415a74b2018-03-14 15:03:13 -0400759 // Set align to left for drawing, as we don't want individual
760 // glyphs centered or right-aligned; the offsets take care of
761 // that portion of the alignment.
762 SkPaint paintCopy(paint);
763 paintCopy.setTextAlign(SkPaint::kLeft_Align);
764 SkASSERT(paintCopy.getTextEncoding() == SkPaint::kGlyphID_TextEncoding);
765
Yuqian Liafc221492016-07-18 13:07:42 -0400766 const int N = end - start;
Derek Sollenbergere547dd02016-11-09 11:55:59 -0500767 SkAutoSTMalloc<1024, uint8_t> storage(N * (sizeof(uint16_t) + sizeof(SkRSXform)));
Yuqian Liafc221492016-07-18 13:07:42 -0400768 SkRSXform* xform = (SkRSXform*)storage.get();
769 uint16_t* glyphs = (uint16_t*)(xform + N);
770 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 }
789
Derek Sollenberger415a74b2018-03-14 15:03:13 -0400790 this->asSkCanvas()->drawTextRSXform(glyphs, sizeof(uint16_t) * N, xform, nullptr, paintCopy);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400791}
792
Derek Sollenberger6f485562015-07-30 10:00:39 -0400793// ----------------------------------------------------------------------------
794// Canvas draw operations: Animations
795// ----------------------------------------------------------------------------
796
Derek Sollenberger6f485562015-07-30 10:00:39 -0400797void SkiaCanvas::drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
John Reck1bcacfd2017-11-03 10:12:19 -0700798 uirenderer::CanvasPropertyPrimitive* top,
799 uirenderer::CanvasPropertyPrimitive* right,
800 uirenderer::CanvasPropertyPrimitive* bottom,
801 uirenderer::CanvasPropertyPrimitive* rx,
802 uirenderer::CanvasPropertyPrimitive* ry,
803 uirenderer::CanvasPropertyPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -0400804 sk_sp<uirenderer::skiapipeline::AnimatedRoundRect> drawable(
John Reck1bcacfd2017-11-03 10:12:19 -0700805 new uirenderer::skiapipeline::AnimatedRoundRect(left, top, right, bottom, rx, ry,
806 paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400807 mCanvas->drawDrawable(drawable.get());
808}
809
John Reck1bcacfd2017-11-03 10:12:19 -0700810void SkiaCanvas::drawCircle(uirenderer::CanvasPropertyPrimitive* x,
811 uirenderer::CanvasPropertyPrimitive* y,
812 uirenderer::CanvasPropertyPrimitive* radius,
813 uirenderer::CanvasPropertyPaint* paint) {
814 sk_sp<uirenderer::skiapipeline::AnimatedCircle> drawable(
815 new uirenderer::skiapipeline::AnimatedCircle(x, y, radius, paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400816 mCanvas->drawDrawable(drawable.get());
817}
818
819// ----------------------------------------------------------------------------
820// Canvas draw operations: View System
821// ----------------------------------------------------------------------------
822
Stan Ilievf50806a2016-10-24 10:40:39 -0400823void SkiaCanvas::drawLayer(uirenderer::DeferredLayerUpdater* layerUpdater) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400824 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw Layers");
825}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400826
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400827void SkiaCanvas::drawRenderNode(uirenderer::RenderNode* renderNode) {
828 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw RenderNodes");
829}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400830
John Reckcd1c3eb2016-04-14 10:38:54 -0700831void SkiaCanvas::callDrawGLFunction(Functor* functor,
John Reck1bcacfd2017-11-03 10:12:19 -0700832 uirenderer::GlFunctorLifecycleListener* listener) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400833 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw GL Content");
834}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400835
John Reck1bcacfd2017-11-03 10:12:19 -0700836} // namespace android