blob: 40b811d813fdc420f9fb4522337e6bf278383341 [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 Reck1bcacfd2017-11-03 10:12:19 -070065 if (!uirenderer::Properties::isSkiaEnabled()) {
66 mCanvasWrapper =
67 SkCreateColorSpaceXformCanvas(mCanvasOwned.get(), SkColorSpace::MakeSRGB());
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040068 mCanvas = mCanvasWrapper.get();
69 } else {
70 mCanvas = mCanvasOwned.get();
71 }
72 } else {
73 /** The wrapper is needed if we are drawing into a non-sRGB destination, since
74 * we need to transform all colors (not just bitmaps via filters) into the
75 * destination's colorspace.
76 */
77 mCanvasWrapper = SkCreateColorSpaceXformCanvas(mCanvasOwned.get(), std::move(cs));
78 mCanvas = mCanvasWrapper.get();
79 }
Derek Sollenberger8872b382014-06-23 14:13:53 -040080}
81
Stan Iliev021693b2016-10-17 16:26:15 -040082SkiaCanvas::~SkiaCanvas() {}
83
Derek Sollenbergerc1908132016-07-15 10:28:16 -040084void SkiaCanvas::reset(SkCanvas* skiaCanvas) {
Mike Reed6acfe162016-11-18 17:21:09 -050085 if (mCanvas != skiaCanvas) {
86 mCanvas = skiaCanvas;
87 mCanvasOwned.reset();
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -040088 mCanvasWrapper.reset();
Mike Reed6acfe162016-11-18 17:21:09 -050089 }
Derek Sollenbergerc1908132016-07-15 10:28:16 -040090 mSaveStack.reset(nullptr);
Derek Sollenbergerc1908132016-07-15 10:28:16 -040091}
92
Derek Sollenberger8872b382014-06-23 14:13:53 -040093// ----------------------------------------------------------------------------
94// Canvas state operations: Replace Bitmap
95// ----------------------------------------------------------------------------
96
John Reckc1b33d62015-04-22 09:04:45 -070097void SkiaCanvas::setBitmap(const SkBitmap& bitmap) {
Romain Guy82426562017-04-04 19:38:50 -070098 sk_sp<SkColorSpace> cs = bitmap.refColorSpace();
Matt Sarettca9b7032017-04-13 12:18:47 -040099 std::unique_ptr<SkCanvas> newCanvas =
100 std::unique_ptr<SkCanvas>(new SkCanvas(bitmap, SkCanvas::ColorBehavior::kLegacy));
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400101 std::unique_ptr<SkCanvas> newCanvasWrapper;
102 if (cs.get() != nullptr && !cs->isSRGB()) {
103 newCanvasWrapper = SkCreateColorSpaceXformCanvas(newCanvas.get(), std::move(cs));
John Reck1bcacfd2017-11-03 10:12:19 -0700104 } else if (!uirenderer::Properties::isSkiaEnabled()) {
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400105 newCanvasWrapper = SkCreateColorSpaceXformCanvas(newCanvas.get(), SkColorSpace::MakeSRGB());
106 }
Tony Mantler4f641d12017-03-14 22:36:14 +0000107
Tony Mantler4f641d12017-03-14 22:36:14 +0000108 // deletes the previously owned canvas (if any)
Matt Sarettea70d222017-03-29 16:25:10 -0400109 mCanvasOwned = std::move(newCanvas);
110 mCanvasWrapper = std::move(newCanvasWrapper);
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400111 mCanvas = mCanvasWrapper ? mCanvasWrapper.get() : mCanvasOwned.get();
Tony Mantler4f641d12017-03-14 22:36:14 +0000112
Derek Sollenberger8872b382014-06-23 14:13:53 -0400113 // clean up the old save stack
Stan Ilievf50806a2016-10-24 10:40:39 -0400114 mSaveStack.reset(nullptr);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400115}
116
117// ----------------------------------------------------------------------------
118// Canvas state operations
119// ----------------------------------------------------------------------------
120
121bool SkiaCanvas::isOpaque() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400122 return mCanvas->imageInfo().isOpaque();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400123}
124
125int SkiaCanvas::width() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400126 return mCanvas->imageInfo().width();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400127}
128
129int SkiaCanvas::height() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400130 return mCanvas->imageInfo().height();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400131}
132
133// ----------------------------------------------------------------------------
134// Canvas state operations: Save (layer)
135// ----------------------------------------------------------------------------
136
137int SkiaCanvas::getSaveCount() const {
138 return mCanvas->getSaveCount();
139}
140
Florin Malitaeecff562015-12-21 10:43:01 -0500141int SkiaCanvas::save(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400142 int count = mCanvas->save();
143 recordPartialSave(flags);
144 return count;
145}
146
Florin Malita5e271402015-11-04 14:36:02 -0500147// The SkiaCanvas::restore operation layers on the capability to preserve
148// either (or both) the matrix and/or clip state after a SkCanvas::restore
149// operation. It does this by explicitly saving off the clip & matrix state
150// when requested and playing it back after the SkCanvas::restore.
Derek Sollenberger8872b382014-06-23 14:13:53 -0400151void SkiaCanvas::restore() {
Stan Ilievf50806a2016-10-24 10:40:39 -0400152 const auto* rec = this->currentSaveRec();
153 if (!rec) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400154 // Fast path - no record for this frame.
155 mCanvas->restore();
156 return;
157 }
158
Florin Malitaeecff562015-12-21 10:43:01 -0500159 bool preserveMatrix = !(rec->saveFlags & SaveFlags::Matrix);
John Reck1bcacfd2017-11-03 10:12:19 -0700160 bool preserveClip = !(rec->saveFlags & SaveFlags::Clip);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400161
162 SkMatrix savedMatrix;
163 if (preserveMatrix) {
164 savedMatrix = mCanvas->getTotalMatrix();
165 }
166
Stan Ilievf50806a2016-10-24 10:40:39 -0400167 const size_t clipIndex = rec->clipIndex;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400168
169 mCanvas->restore();
Stan Ilievf50806a2016-10-24 10:40:39 -0400170 mSaveStack->pop_back();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400171
172 if (preserveMatrix) {
173 mCanvas->setMatrix(savedMatrix);
174 }
175
Stan Ilievf50806a2016-10-24 10:40:39 -0400176 if (preserveClip) {
177 this->applyPersistentClips(clipIndex);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400178 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400179}
180
181void SkiaCanvas::restoreToCount(int restoreCount) {
182 while (mCanvas->getSaveCount() > restoreCount) {
183 this->restore();
184 }
185}
186
Florin Malitaeecff562015-12-21 10:43:01 -0500187static inline SkCanvas::SaveLayerFlags layerFlags(SaveFlags::Flags flags) {
188 SkCanvas::SaveLayerFlags layerFlags = 0;
189
Florin Malitaeecff562015-12-21 10:43:01 -0500190 if (!(flags & SaveFlags::ClipToLayer)) {
191 layerFlags |= SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag;
192 }
193
194 return layerFlags;
195}
196
John Reck1bcacfd2017-11-03 10:12:19 -0700197int SkiaCanvas::saveLayer(float left, float top, float right, float bottom, const SkPaint* paint,
198 SaveFlags::Flags flags) {
Florin Malitaeecff562015-12-21 10:43:01 -0500199 const SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
Derek Sollenbergerb8201192017-01-09 16:11:59 -0500200 const SkCanvas::SaveLayerRec rec(&bounds, paint, layerFlags(flags));
Florin Malitaeecff562015-12-21 10:43:01 -0500201
Stan Iliev68885e32016-12-14 11:18:34 -0500202 return mCanvas->saveLayer(rec);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400203}
204
John Reck1bcacfd2017-11-03 10:12:19 -0700205int SkiaCanvas::saveLayerAlpha(float left, float top, float right, float bottom, int alpha,
206 SaveFlags::Flags flags) {
Florin Malitaeecff562015-12-21 10:43:01 -0500207 if (static_cast<unsigned>(alpha) < 0xFF) {
Yuqian Lifd92ee42016-04-27 17:03:38 -0400208 SkPaint alphaPaint;
209 alphaPaint.setAlpha(alpha);
210 return this->saveLayer(left, top, right, bottom, &alphaPaint, flags);
Florin Malitaeecff562015-12-21 10:43:01 -0500211 }
Yuqian Lifd92ee42016-04-27 17:03:38 -0400212 return this->saveLayer(left, top, right, bottom, nullptr, flags);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400213}
214
Stan Ilievf50806a2016-10-24 10:40:39 -0400215class SkiaCanvas::Clip {
216public:
Mike Reed6e49c9f2016-12-02 15:36:59 -0500217 Clip(const SkRect& rect, SkClipOp op, const SkMatrix& m)
John Reck1bcacfd2017-11-03 10:12:19 -0700218 : mType(Type::Rect), mOp(op), mMatrix(m), mRRect(SkRRect::MakeRect(rect)) {}
Mike Reed6e49c9f2016-12-02 15:36:59 -0500219 Clip(const SkRRect& rrect, SkClipOp op, const SkMatrix& m)
John Reck1bcacfd2017-11-03 10:12:19 -0700220 : mType(Type::RRect), mOp(op), mMatrix(m), mRRect(rrect) {}
Mike Reed6e49c9f2016-12-02 15:36:59 -0500221 Clip(const SkPath& path, SkClipOp op, const SkMatrix& m)
John Reck1bcacfd2017-11-03 10:12:19 -0700222 : mType(Type::Path), mOp(op), mMatrix(m), mPath(&path) {}
Stan Ilievf50806a2016-10-24 10:40:39 -0400223
224 void apply(SkCanvas* canvas) const {
225 canvas->setMatrix(mMatrix);
226 switch (mType) {
John Reck1bcacfd2017-11-03 10:12:19 -0700227 case Type::Rect:
228 canvas->clipRect(mRRect.rect(), mOp);
229 break;
230 case Type::RRect:
231 canvas->clipRRect(mRRect, mOp);
232 break;
233 case Type::Path:
234 canvas->clipPath(*mPath.get(), mOp);
235 break;
Stan Ilievf50806a2016-10-24 10:40:39 -0400236 }
237 }
238
239private:
240 enum class Type {
241 Rect,
242 RRect,
243 Path,
244 };
245
John Reck1bcacfd2017-11-03 10:12:19 -0700246 Type mType;
247 SkClipOp mOp;
248 SkMatrix mMatrix;
Stan Ilievf50806a2016-10-24 10:40:39 -0400249
250 // These are logically a union (tracked separately due to non-POD path).
251 SkTLazy<SkPath> mPath;
John Reck1bcacfd2017-11-03 10:12:19 -0700252 SkRRect mRRect;
Stan Ilievf50806a2016-10-24 10:40:39 -0400253};
254
255const SkiaCanvas::SaveRec* SkiaCanvas::currentSaveRec() const {
John Reck1bcacfd2017-11-03 10:12:19 -0700256 const SaveRec* rec = mSaveStack ? static_cast<const SaveRec*>(mSaveStack->back()) : nullptr;
Stan Ilievf50806a2016-10-24 10:40:39 -0400257 int currentSaveCount = mCanvas->getSaveCount();
258 SkASSERT(!rec || currentSaveCount >= rec->saveCount);
259
260 return (rec && rec->saveCount == currentSaveCount) ? rec : nullptr;
261}
262
Derek Sollenberger8872b382014-06-23 14:13:53 -0400263// ----------------------------------------------------------------------------
264// functions to emulate legacy SaveFlags (i.e. independent matrix/clip flags)
265// ----------------------------------------------------------------------------
266
Florin Malitaeecff562015-12-21 10:43:01 -0500267void SkiaCanvas::recordPartialSave(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400268 // A partial save is a save operation which doesn't capture the full canvas state.
Florin Malitaeecff562015-12-21 10:43:01 -0500269 // (either SaveFlags::Matrix or SaveFlags::Clip is missing).
Derek Sollenberger8872b382014-06-23 14:13:53 -0400270
271 // Mask-out non canvas state bits.
Florin Malitaeecff562015-12-21 10:43:01 -0500272 flags &= SaveFlags::MatrixClip;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400273
Florin Malitaeecff562015-12-21 10:43:01 -0500274 if (flags == SaveFlags::MatrixClip) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400275 // not a partial save.
276 return;
277 }
278
Stan Ilievf50806a2016-10-24 10:40:39 -0400279 if (!mSaveStack) {
Ben Wagnerd1cbc162015-08-19 12:45:09 -0400280 mSaveStack.reset(new SkDeque(sizeof(struct SaveRec), 8));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400281 }
282
283 SaveRec* rec = static_cast<SaveRec*>(mSaveStack->push_back());
Florin Malita5e271402015-11-04 14:36:02 -0500284 rec->saveCount = mCanvas->getSaveCount();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400285 rec->saveFlags = flags;
Stan Ilievf50806a2016-10-24 10:40:39 -0400286 rec->clipIndex = mClipStack.size();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400287}
288
Stan Ilievf50806a2016-10-24 10:40:39 -0400289template <typename T>
Mike Reed6e49c9f2016-12-02 15:36:59 -0500290void SkiaCanvas::recordClip(const T& clip, SkClipOp op) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400291 // Only need tracking when in a partial save frame which
292 // doesn't restore the clip.
293 const SaveRec* rec = this->currentSaveRec();
294 if (rec && !(rec->saveFlags & SaveFlags::Clip)) {
295 mClipStack.emplace_back(clip, op, mCanvas->getTotalMatrix());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400296 }
297}
298
Stan Ilievf50806a2016-10-24 10:40:39 -0400299// Applies and optionally removes all clips >= index.
300void SkiaCanvas::applyPersistentClips(size_t clipStartIndex) {
301 SkASSERT(clipStartIndex <= mClipStack.size());
302 const auto begin = mClipStack.cbegin() + clipStartIndex;
303 const auto end = mClipStack.cend();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400304
Stan Ilievf50806a2016-10-24 10:40:39 -0400305 // Clip application mutates the CTM.
306 const SkMatrix saveMatrix = mCanvas->getTotalMatrix();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400307
Stan Ilievf50806a2016-10-24 10:40:39 -0400308 for (auto clip = begin; clip != end; ++clip) {
Mike Reed6acfe162016-11-18 17:21:09 -0500309 clip->apply(mCanvas);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400310 }
311
Stan Ilievf50806a2016-10-24 10:40:39 -0400312 mCanvas->setMatrix(saveMatrix);
313
314 // If the current/post-restore save rec is also persisting clips, we
315 // leave them on the stack to be reapplied part of the next restore().
316 // Otherwise we're done and just pop them.
317 const auto* rec = this->currentSaveRec();
318 if (!rec || (rec->saveFlags & SaveFlags::Clip)) {
319 mClipStack.erase(begin, end);
320 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400321}
322
323// ----------------------------------------------------------------------------
324// Canvas state operations: Matrix
325// ----------------------------------------------------------------------------
326
327void SkiaCanvas::getMatrix(SkMatrix* outMatrix) const {
328 *outMatrix = mCanvas->getTotalMatrix();
329}
330
331void SkiaCanvas::setMatrix(const SkMatrix& matrix) {
332 mCanvas->setMatrix(matrix);
333}
334
335void SkiaCanvas::concat(const SkMatrix& matrix) {
336 mCanvas->concat(matrix);
337}
338
339void SkiaCanvas::rotate(float degrees) {
340 mCanvas->rotate(degrees);
341}
342
343void SkiaCanvas::scale(float sx, float sy) {
344 mCanvas->scale(sx, sy);
345}
346
347void SkiaCanvas::skew(float sx, float sy) {
348 mCanvas->skew(sx, sy);
349}
350
351void SkiaCanvas::translate(float dx, float dy) {
352 mCanvas->translate(dx, dy);
353}
354
355// ----------------------------------------------------------------------------
356// Canvas state operations: Clips
357// ----------------------------------------------------------------------------
358
359// This function is a mirror of SkCanvas::getClipBounds except that it does
360// not outset the edge of the clip to account for anti-aliasing. There is
361// a skia bug to investigate pushing this logic into back into skia.
362// (see https://code.google.com/p/skia/issues/detail?id=1303)
363bool SkiaCanvas::getClipBounds(SkRect* outRect) const {
364 SkIRect ibounds;
Mike Reed5e438982017-01-25 08:23:25 -0500365 if (!mCanvas->getDeviceClipBounds(&ibounds)) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400366 return false;
367 }
368
369 SkMatrix inverse;
370 // if we can't invert the CTM, we can't return local clip bounds
371 if (!mCanvas->getTotalMatrix().invert(&inverse)) {
372 if (outRect) {
373 outRect->setEmpty();
374 }
375 return false;
376 }
377
378 if (NULL != outRect) {
379 SkRect r = SkRect::Make(ibounds);
380 inverse.mapRect(outRect, r);
381 }
382 return true;
383}
384
385bool SkiaCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
386 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
387 return mCanvas->quickReject(bounds);
388}
389
390bool SkiaCanvas::quickRejectPath(const SkPath& path) const {
391 return mCanvas->quickReject(path);
392}
393
Mike Reed6e49c9f2016-12-02 15:36:59 -0500394bool SkiaCanvas::clipRect(float left, float top, float right, float bottom, SkClipOp op) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400395 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Stan Ilievf50806a2016-10-24 10:40:39 -0400396 this->recordClip(rect, op);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400397 mCanvas->clipRect(rect, op);
Chris Craik5ec6a282015-06-23 15:42:12 -0700398 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400399}
400
Mike Reed6e49c9f2016-12-02 15:36:59 -0500401bool SkiaCanvas::clipPath(const SkPath* path, SkClipOp op) {
Derek Sollenbergerf7d98f42017-04-17 11:27:36 -0400402 this->recordClip(*path, op);
403 mCanvas->clipPath(*path, op);
Chris Craik5ec6a282015-06-23 15:42:12 -0700404 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400405}
406
Derek Sollenberger8872b382014-06-23 14:13:53 -0400407// ----------------------------------------------------------------------------
408// Canvas state operations: Filters
409// ----------------------------------------------------------------------------
410
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400411SkDrawFilter* SkiaCanvas::getDrawFilter() {
412 return mCanvas->getDrawFilter();
413}
414
Derek Sollenberger8872b382014-06-23 14:13:53 -0400415void SkiaCanvas::setDrawFilter(SkDrawFilter* drawFilter) {
416 mCanvas->setDrawFilter(drawFilter);
417}
418
419// ----------------------------------------------------------------------------
Matt Sarettd0814db2017-04-13 09:33:18 -0400420// Canvas state operations: Capture
421// ----------------------------------------------------------------------------
422
423SkCanvasState* SkiaCanvas::captureCanvasState() const {
424 SkCanvas* canvas = mCanvas;
425 if (mCanvasOwned) {
426 // Important to use the underlying SkCanvas, not the wrapper.
427 canvas = mCanvasOwned.get();
428 }
429
430 // Workarounds for http://crbug.com/271096: SW draw only supports
431 // translate & scale transforms, and a simple rectangular clip.
432 // (This also avoids significant wasted time in calling
433 // SkCanvasStateUtils::CaptureCanvasState when the clip is complex).
John Reck1bcacfd2017-11-03 10:12:19 -0700434 if (!canvas->isClipRect() || (canvas->getTotalMatrix().getType() &
435 ~(SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask))) {
436 return nullptr;
Matt Sarettd0814db2017-04-13 09:33:18 -0400437 }
438
439 return SkCanvasStateUtils::CaptureCanvasState(canvas);
440}
441
442// ----------------------------------------------------------------------------
Derek Sollenberger8872b382014-06-23 14:13:53 -0400443// Canvas draw operations
444// ----------------------------------------------------------------------------
445
Mike Reed260ab722016-10-07 15:59:20 -0400446void SkiaCanvas::drawColor(int color, SkBlendMode mode) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400447 mCanvas->drawColor(color, mode);
448}
449
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400450void SkiaCanvas::drawPaint(const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400451 mCanvas->drawPaint(paint);
452}
453
454// ----------------------------------------------------------------------------
455// Canvas draw operations: Geometry
456// ----------------------------------------------------------------------------
457
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400458void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400459 SkCanvas::PointMode mode) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500460 if (CC_UNLIKELY(count < 2 || paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400461 // convert the floats into SkPoints
John Reck1bcacfd2017-11-03 10:12:19 -0700462 count >>= 1; // now it is the number of points
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400463 std::unique_ptr<SkPoint[]> pts(new SkPoint[count]);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400464 for (int i = 0; i < count; i++) {
465 pts[i].set(points[0], points[1]);
466 points += 2;
467 }
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400468 mCanvas->drawPoints(mode, count, pts.get(), paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400469}
470
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400471void SkiaCanvas::drawPoint(float x, float y, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400472 mCanvas->drawPoint(x, y, paint);
473}
474
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400475void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400476 this->drawPoints(points, count, paint, SkCanvas::kPoints_PointMode);
477}
478
479void SkiaCanvas::drawLine(float startX, float startY, float stopX, float stopY,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400480 const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400481 mCanvas->drawLine(startX, startY, stopX, stopY, paint);
482}
483
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400484void SkiaCanvas::drawLines(const float* points, int count, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500485 if (CC_UNLIKELY(count < 4 || paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400486 this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode);
487}
488
John Reck1bcacfd2017-11-03 10:12:19 -0700489void SkiaCanvas::drawRect(float left, float top, float right, float bottom, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500490 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Mike Reed6c9bb242017-04-04 09:15:37 -0400491 mCanvas->drawRect({left, top, right, bottom}, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400492}
493
Derek Sollenberger94394b32015-07-10 09:58:41 -0400494void SkiaCanvas::drawRegion(const SkRegion& region, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500495 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Stan Ilievf50806a2016-10-24 10:40:39 -0400496 mCanvas->drawRegion(region, paint);
Derek Sollenberger94394b32015-07-10 09:58:41 -0400497}
498
John Reck1bcacfd2017-11-03 10:12:19 -0700499void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom, float rx, float ry,
500 const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500501 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400502 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
503 mCanvas->drawRoundRect(rect, rx, ry, paint);
504}
505
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400506void SkiaCanvas::drawCircle(float x, float y, float radius, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500507 if (CC_UNLIKELY(radius <= 0 || paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400508 mCanvas->drawCircle(x, y, radius, paint);
509}
510
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400511void SkiaCanvas::drawOval(float left, float top, float right, float bottom, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500512 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400513 SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
514 mCanvas->drawOval(oval, paint);
515}
516
John Reck1bcacfd2017-11-03 10:12:19 -0700517void SkiaCanvas::drawArc(float left, float top, float right, float bottom, float startAngle,
518 float sweepAngle, bool useCenter, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500519 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400520 SkRect arc = SkRect::MakeLTRB(left, top, right, bottom);
Derek Sollenbergeref3b2182017-11-06 13:55:59 -0500521 if (fabs(sweepAngle) >= 360.0f) {
522 mCanvas->drawOval(arc, paint);
523 } else {
524 mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, paint);
525 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400526}
527
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400528void SkiaCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500529 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Stan Iliev6dcfdec2017-08-15 16:42:05 -0400530 if (CC_UNLIKELY(path.isEmpty() && (!path.isInverseFillType()))) {
531 return;
532 }
Derek Sollenbergerd7f13f82017-03-09 13:08:27 -0500533 mCanvas->drawPath(path, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400534}
535
Mike Reed826deef2017-04-04 15:32:04 -0400536void SkiaCanvas::drawVertices(const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint) {
537 mCanvas->drawVertices(vertices, mode, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400538}
539
540// ----------------------------------------------------------------------------
541// Canvas draw operations: Bitmaps
542// ----------------------------------------------------------------------------
543
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400544const SkPaint* SkiaCanvas::addFilter(const SkPaint* origPaint, SkPaint* tmpPaint,
John Reck1bcacfd2017-11-03 10:12:19 -0700545 sk_sp<SkColorFilter> colorSpaceFilter) {
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400546 /* We don't apply the colorSpace filter if this canvas is already wrapped with
547 * a SkColorSpaceXformCanvas since it already takes care of converting the
548 * contents of the bitmap into the appropriate colorspace. The mCanvasWrapper
549 * should only be used if this canvas is backed by a surface/bitmap that is known
550 * to have a non-sRGB colorspace.
551 */
552 if (!mCanvasWrapper && colorSpaceFilter) {
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400553 if (origPaint) {
554 *tmpPaint = *origPaint;
555 }
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400556
557 if (tmpPaint->getColorFilter()) {
John Reck1bcacfd2017-11-03 10:12:19 -0700558 tmpPaint->setColorFilter(
559 SkColorFilter::MakeComposeFilter(tmpPaint->refColorFilter(), colorSpaceFilter));
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400560 LOG_ALWAYS_FATAL_IF(!tmpPaint->getColorFilter());
561 } else {
562 tmpPaint->setColorFilter(colorSpaceFilter);
563 }
564
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400565 return tmpPaint;
566 } else {
567 return origPaint;
568 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400569}
570
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400571void SkiaCanvas::drawBitmap(Bitmap& bitmap, float left, float top, const SkPaint* paint) {
572 SkPaint tmpPaint;
573 sk_sp<SkColorFilter> colorFilter;
574 sk_sp<SkImage> image = bitmap.makeImage(&colorFilter);
575 mCanvas->drawImage(image, left, top, addFilter(paint, &tmpPaint, colorFilter));
576}
577
578void SkiaCanvas::drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const SkPaint* paint) {
Mike Reed6acfe162016-11-18 17:21:09 -0500579 SkAutoCanvasRestore acr(mCanvas, true);
Mike Reed70ffbf92014-12-08 17:03:30 -0500580 mCanvas->concat(matrix);
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400581
582 SkPaint tmpPaint;
583 sk_sp<SkColorFilter> colorFilter;
584 sk_sp<SkImage> image = bitmap.makeImage(&colorFilter);
585 mCanvas->drawImage(image, 0, 0, addFilter(paint, &tmpPaint, colorFilter));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400586}
587
John Reck1bcacfd2017-11-03 10:12:19 -0700588void SkiaCanvas::drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight,
589 float srcBottom, float dstLeft, float dstTop, float dstRight,
590 float dstBottom, const SkPaint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400591 SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
592 SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400593
594 SkPaint tmpPaint;
595 sk_sp<SkColorFilter> colorFilter;
596 sk_sp<SkImage> image = bitmap.makeImage(&colorFilter);
Derek Sollenberger6c2a9e22017-08-15 16:23:01 -0400597 mCanvas->drawImageRect(image, srcRect, dstRect, addFilter(paint, &tmpPaint, colorFilter),
John Reck1bcacfd2017-11-03 10:12:19 -0700598 SkCanvas::kFast_SrcRectConstraint);
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,
John Reck1bcacfd2017-11-03 10:12:19 -0700602 const float* vertices, const int* colors, const SkPaint* 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
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400676 SkPaint tmpPaint;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400677 if (paint) {
678 tmpPaint = *paint;
679 }
Stan Ilievf50806a2016-10-24 10:40:39 -0400680
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400681 sk_sp<SkColorFilter> colorFilter;
682 sk_sp<SkImage> image = bitmap.makeImage(&colorFilter);
John Reck1bcacfd2017-11-03 10:12:19 -0700683 sk_sp<SkShader> shader =
684 image->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
685 if (colorFilter) {
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400686 shader = shader->makeWithColorFilter(colorFilter);
687 }
688 tmpPaint.setShader(shader);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400689
Mike Reed871cd2d2017-03-17 10:15:52 -0400690 mCanvas->drawVertices(builder.detach(), SkBlendMode::kModulate, tmpPaint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400691}
692
John Reck1bcacfd2017-11-03 10:12:19 -0700693void SkiaCanvas::drawNinePatch(Bitmap& bitmap, const Res_png_9patch& chunk, float dstLeft,
694 float dstTop, float dstRight, float dstBottom,
695 const SkPaint* paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400696 SkCanvas::Lattice lattice;
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400697 NinePatchUtils::SetLatticeDivs(&lattice, chunk, bitmap.width(), bitmap.height());
Stan Ilievf50806a2016-10-24 10:40:39 -0400698
Stan Ilieve12d7312017-12-04 14:48:27 -0500699 lattice.fRectTypes = nullptr;
700 lattice.fColors = nullptr;
Stan Ilievf50806a2016-10-24 10:40:39 -0400701 int numFlags = 0;
Stan Iliev021693b2016-10-17 16:26:15 -0400702 if (chunk.numColors > 0 && chunk.numColors == NinePatchUtils::NumDistinctRects(lattice)) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400703 // We can expect the framework to give us a color for every distinct rect.
704 // Skia requires a flag for every rect.
705 numFlags = (lattice.fXCount + 1) * (lattice.fYCount + 1);
706 }
707
Stan Ilieve12d7312017-12-04 14:48:27 -0500708 SkAutoSTMalloc<25, SkCanvas::Lattice::RectType> flags(numFlags);
709 SkAutoSTMalloc<25, SkColor> colors(numFlags);
Stan Ilievf50806a2016-10-24 10:40:39 -0400710 if (numFlags > 0) {
Stan Ilieve12d7312017-12-04 14:48:27 -0500711 NinePatchUtils::SetLatticeFlags(&lattice, flags.get(), numFlags, chunk, colors.get());
Stan Ilievf50806a2016-10-24 10:40:39 -0400712 }
713
714 lattice.fBounds = nullptr;
715 SkRect dst = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400716
717 SkPaint tmpPaint;
718 sk_sp<SkColorFilter> colorFilter;
719 sk_sp<SkImage> image = bitmap.makeImage(&colorFilter);
720 mCanvas->drawImageLattice(image.get(), lattice, dst, addFilter(paint, &tmpPaint, colorFilter));
Derek Sollenbergeredca3202015-07-10 13:56:39 -0400721}
722
Derek Sollenberger2d142132018-01-22 10:25:26 -0500723double SkiaCanvas::drawAnimatedImage(AnimatedImageDrawable* imgDrawable) {
724 return imgDrawable->drawStaging(mCanvas);
Leon Scroggins III671cce22018-01-14 16:52:17 -0500725}
726
Doris Liu766431a2016-02-04 22:17:11 +0000727void SkiaCanvas::drawVectorDrawable(VectorDrawableRoot* vectorDrawable) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800728 vectorDrawable->drawStaging(this);
Doris Liu766431a2016-02-04 22:17:11 +0000729}
730
Derek Sollenberger8872b382014-06-23 14:13:53 -0400731// ----------------------------------------------------------------------------
732// Canvas draw operations: Text
733// ----------------------------------------------------------------------------
734
Stan Iliev0b58d992017-03-30 18:22:27 -0400735void SkiaCanvas::drawGlyphs(ReadGlyphFunc glyphFunc, int count, const SkPaint& paint, float x,
John Reck1bcacfd2017-11-03 10:12:19 -0700736 float y, float boundsLeft, float boundsTop, float boundsRight,
737 float boundsBottom, float totalAdvance) {
Stan Iliev0b58d992017-03-30 18:22:27 -0400738 if (count <= 0 || paint.nothingToDraw()) return;
Stan Ilievf50806a2016-10-24 10:40:39 -0400739 // Set align to left for drawing, as we don't want individual
740 // glyphs centered or right-aligned; the offset above takes
741 // care of all alignment.
742 SkPaint paintCopy(paint);
743 paintCopy.setTextAlign(SkPaint::kLeft_Align);
Stan Ilieva39b7742017-11-29 13:15:45 -0500744 SkASSERT(paintCopy.getTextEncoding() == SkPaint::kGlyphID_TextEncoding);
Stan Iliev7717e222018-02-05 18:04:11 -0500745 // Stroke with a hairline is drawn on HW with a fill style for compatibility with Android O and
746 // older.
747 if (!mCanvasOwned && sApiLevel <= 27 && paintCopy.getStrokeWidth() <= 0
748 && paintCopy.getStyle() == SkPaint::kStroke_Style) {
749 paintCopy.setStyle(SkPaint::kFill_Style);
750 }
Stan Ilievf50806a2016-10-24 10:40:39 -0400751
John Reck1bcacfd2017-11-03 10:12:19 -0700752 SkRect bounds =
753 SkRect::MakeLTRB(boundsLeft + x, boundsTop + y, boundsRight + x, boundsBottom + y);
Stan Ilievf50806a2016-10-24 10:40:39 -0400754
755 SkTextBlobBuilder builder;
756 const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(paintCopy, count, &bounds);
Stan Iliev0b58d992017-03-30 18:22:27 -0400757 glyphFunc(buffer.glyphs, buffer.pos);
Stan Ilievf50806a2016-10-24 10:40:39 -0400758
759 sk_sp<SkTextBlob> textBlob(builder.make());
760 mCanvas->drawTextBlob(textBlob, 0, 0, paintCopy);
761 drawTextDecorations(x, y, totalAdvance, paintCopy);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400762}
763
Yuqian Liafc221492016-07-18 13:07:42 -0400764void SkiaCanvas::drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
John Reck1bcacfd2017-11-03 10:12:19 -0700765 const SkPaint& paint, const SkPath& path, size_t start,
766 size_t end) {
Derek Sollenberger415a74b2018-03-14 15:03:13 -0400767 // Set align to left for drawing, as we don't want individual
768 // glyphs centered or right-aligned; the offsets take care of
769 // that portion of the alignment.
770 SkPaint paintCopy(paint);
771 paintCopy.setTextAlign(SkPaint::kLeft_Align);
772 SkASSERT(paintCopy.getTextEncoding() == SkPaint::kGlyphID_TextEncoding);
773
Yuqian Liafc221492016-07-18 13:07:42 -0400774 const int N = end - start;
Derek Sollenbergere547dd02016-11-09 11:55:59 -0500775 SkAutoSTMalloc<1024, uint8_t> storage(N * (sizeof(uint16_t) + sizeof(SkRSXform)));
Yuqian Liafc221492016-07-18 13:07:42 -0400776 SkRSXform* xform = (SkRSXform*)storage.get();
777 uint16_t* glyphs = (uint16_t*)(xform + N);
778 SkPathMeasure meas(path, false);
779
780 for (size_t i = start; i < end; i++) {
781 glyphs[i - start] = layout.getGlyphId(i);
Stan Ilievc6c96dd2018-01-10 16:06:04 -0500782 float halfWidth = layout.getCharAdvance(i) * 0.5f;
783 float x = hOffset + layout.getX(i) + halfWidth;
Yuqian Liafc221492016-07-18 13:07:42 -0400784 float y = vOffset + layout.getY(i);
785
786 SkPoint pos;
787 SkVector tan;
788 if (!meas.getPosTan(x, &pos, &tan)) {
789 pos.set(x, y);
790 tan.set(1, 0);
791 }
792 xform[i - start].fSCos = tan.x();
793 xform[i - start].fSSin = tan.y();
Stan Ilievc6c96dd2018-01-10 16:06:04 -0500794 xform[i - start].fTx = pos.x() - tan.y() * y - halfWidth * tan.x();
795 xform[i - start].fTy = pos.y() + tan.x() * y - halfWidth * tan.y();
Yuqian Liafc221492016-07-18 13:07:42 -0400796 }
797
Derek Sollenberger415a74b2018-03-14 15:03:13 -0400798 this->asSkCanvas()->drawTextRSXform(glyphs, sizeof(uint16_t) * N, xform, nullptr, paintCopy);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400799}
800
Derek Sollenberger6f485562015-07-30 10:00:39 -0400801// ----------------------------------------------------------------------------
802// Canvas draw operations: Animations
803// ----------------------------------------------------------------------------
804
Derek Sollenberger6f485562015-07-30 10:00:39 -0400805void SkiaCanvas::drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
John Reck1bcacfd2017-11-03 10:12:19 -0700806 uirenderer::CanvasPropertyPrimitive* top,
807 uirenderer::CanvasPropertyPrimitive* right,
808 uirenderer::CanvasPropertyPrimitive* bottom,
809 uirenderer::CanvasPropertyPrimitive* rx,
810 uirenderer::CanvasPropertyPrimitive* ry,
811 uirenderer::CanvasPropertyPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -0400812 sk_sp<uirenderer::skiapipeline::AnimatedRoundRect> drawable(
John Reck1bcacfd2017-11-03 10:12:19 -0700813 new uirenderer::skiapipeline::AnimatedRoundRect(left, top, right, bottom, rx, ry,
814 paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400815 mCanvas->drawDrawable(drawable.get());
816}
817
John Reck1bcacfd2017-11-03 10:12:19 -0700818void SkiaCanvas::drawCircle(uirenderer::CanvasPropertyPrimitive* x,
819 uirenderer::CanvasPropertyPrimitive* y,
820 uirenderer::CanvasPropertyPrimitive* radius,
821 uirenderer::CanvasPropertyPaint* paint) {
822 sk_sp<uirenderer::skiapipeline::AnimatedCircle> drawable(
823 new uirenderer::skiapipeline::AnimatedCircle(x, y, radius, paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400824 mCanvas->drawDrawable(drawable.get());
825}
826
827// ----------------------------------------------------------------------------
828// Canvas draw operations: View System
829// ----------------------------------------------------------------------------
830
Stan Ilievf50806a2016-10-24 10:40:39 -0400831void SkiaCanvas::drawLayer(uirenderer::DeferredLayerUpdater* layerUpdater) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400832 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw Layers");
833}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400834
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400835void SkiaCanvas::drawRenderNode(uirenderer::RenderNode* renderNode) {
836 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw RenderNodes");
837}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400838
John Reckcd1c3eb2016-04-14 10:38:54 -0700839void SkiaCanvas::callDrawGLFunction(Functor* functor,
John Reck1bcacfd2017-11-03 10:12:19 -0700840 uirenderer::GlFunctorLifecycleListener* listener) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400841 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw GL Content");
842}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400843
John Reck1bcacfd2017-11-03 10:12:19 -0700844} // namespace android