blob: dc274cf50a52f29499927096cfcf33455711fd68 [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
Yuqian Li83427ff2016-09-14 11:14:06 -0400190 // We intentionally ignore the SaveFlags::HasAlphaLayer and
191 // SkCanvas::kIsOpaque_SaveLayerFlag flags because HWUI ignores it
192 // and our Android client may use it incorrectly.
193 // In Skia, this flag is purely for performance optimization.
Florin Malitaeecff562015-12-21 10:43:01 -0500194
195 if (!(flags & SaveFlags::ClipToLayer)) {
196 layerFlags |= SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag;
197 }
198
199 return layerFlags;
200}
201
John Reck1bcacfd2017-11-03 10:12:19 -0700202int SkiaCanvas::saveLayer(float left, float top, float right, float bottom, const SkPaint* paint,
203 SaveFlags::Flags flags) {
Florin Malitaeecff562015-12-21 10:43:01 -0500204 const SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
Derek Sollenbergerb8201192017-01-09 16:11:59 -0500205 const SkCanvas::SaveLayerRec rec(&bounds, paint, layerFlags(flags));
Florin Malitaeecff562015-12-21 10:43:01 -0500206
Stan Iliev68885e32016-12-14 11:18:34 -0500207 return mCanvas->saveLayer(rec);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400208}
209
John Reck1bcacfd2017-11-03 10:12:19 -0700210int SkiaCanvas::saveLayerAlpha(float left, float top, float right, float bottom, int alpha,
211 SaveFlags::Flags flags) {
Florin Malitaeecff562015-12-21 10:43:01 -0500212 if (static_cast<unsigned>(alpha) < 0xFF) {
Yuqian Lifd92ee42016-04-27 17:03:38 -0400213 SkPaint alphaPaint;
214 alphaPaint.setAlpha(alpha);
215 return this->saveLayer(left, top, right, bottom, &alphaPaint, flags);
Florin Malitaeecff562015-12-21 10:43:01 -0500216 }
Yuqian Lifd92ee42016-04-27 17:03:38 -0400217 return this->saveLayer(left, top, right, bottom, nullptr, flags);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400218}
219
Stan Ilievf50806a2016-10-24 10:40:39 -0400220class SkiaCanvas::Clip {
221public:
Mike Reed6e49c9f2016-12-02 15:36:59 -0500222 Clip(const SkRect& rect, SkClipOp op, const SkMatrix& m)
John Reck1bcacfd2017-11-03 10:12:19 -0700223 : mType(Type::Rect), mOp(op), mMatrix(m), mRRect(SkRRect::MakeRect(rect)) {}
Mike Reed6e49c9f2016-12-02 15:36:59 -0500224 Clip(const SkRRect& rrect, SkClipOp op, const SkMatrix& m)
John Reck1bcacfd2017-11-03 10:12:19 -0700225 : mType(Type::RRect), mOp(op), mMatrix(m), mRRect(rrect) {}
Mike Reed6e49c9f2016-12-02 15:36:59 -0500226 Clip(const SkPath& path, SkClipOp op, const SkMatrix& m)
John Reck1bcacfd2017-11-03 10:12:19 -0700227 : mType(Type::Path), mOp(op), mMatrix(m), mPath(&path) {}
Stan Ilievf50806a2016-10-24 10:40:39 -0400228
229 void apply(SkCanvas* canvas) const {
230 canvas->setMatrix(mMatrix);
231 switch (mType) {
John Reck1bcacfd2017-11-03 10:12:19 -0700232 case Type::Rect:
233 canvas->clipRect(mRRect.rect(), mOp);
234 break;
235 case Type::RRect:
236 canvas->clipRRect(mRRect, mOp);
237 break;
238 case Type::Path:
239 canvas->clipPath(*mPath.get(), mOp);
240 break;
Stan Ilievf50806a2016-10-24 10:40:39 -0400241 }
242 }
243
244private:
245 enum class Type {
246 Rect,
247 RRect,
248 Path,
249 };
250
John Reck1bcacfd2017-11-03 10:12:19 -0700251 Type mType;
252 SkClipOp mOp;
253 SkMatrix mMatrix;
Stan Ilievf50806a2016-10-24 10:40:39 -0400254
255 // These are logically a union (tracked separately due to non-POD path).
256 SkTLazy<SkPath> mPath;
John Reck1bcacfd2017-11-03 10:12:19 -0700257 SkRRect mRRect;
Stan Ilievf50806a2016-10-24 10:40:39 -0400258};
259
260const SkiaCanvas::SaveRec* SkiaCanvas::currentSaveRec() const {
John Reck1bcacfd2017-11-03 10:12:19 -0700261 const SaveRec* rec = mSaveStack ? static_cast<const SaveRec*>(mSaveStack->back()) : nullptr;
Stan Ilievf50806a2016-10-24 10:40:39 -0400262 int currentSaveCount = mCanvas->getSaveCount();
263 SkASSERT(!rec || currentSaveCount >= rec->saveCount);
264
265 return (rec && rec->saveCount == currentSaveCount) ? rec : nullptr;
266}
267
Derek Sollenberger8872b382014-06-23 14:13:53 -0400268// ----------------------------------------------------------------------------
269// functions to emulate legacy SaveFlags (i.e. independent matrix/clip flags)
270// ----------------------------------------------------------------------------
271
Florin Malitaeecff562015-12-21 10:43:01 -0500272void SkiaCanvas::recordPartialSave(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400273 // A partial save is a save operation which doesn't capture the full canvas state.
Florin Malitaeecff562015-12-21 10:43:01 -0500274 // (either SaveFlags::Matrix or SaveFlags::Clip is missing).
Derek Sollenberger8872b382014-06-23 14:13:53 -0400275
276 // Mask-out non canvas state bits.
Florin Malitaeecff562015-12-21 10:43:01 -0500277 flags &= SaveFlags::MatrixClip;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400278
Florin Malitaeecff562015-12-21 10:43:01 -0500279 if (flags == SaveFlags::MatrixClip) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400280 // not a partial save.
281 return;
282 }
283
Stan Ilievf50806a2016-10-24 10:40:39 -0400284 if (!mSaveStack) {
Ben Wagnerd1cbc162015-08-19 12:45:09 -0400285 mSaveStack.reset(new SkDeque(sizeof(struct SaveRec), 8));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400286 }
287
288 SaveRec* rec = static_cast<SaveRec*>(mSaveStack->push_back());
Florin Malita5e271402015-11-04 14:36:02 -0500289 rec->saveCount = mCanvas->getSaveCount();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400290 rec->saveFlags = flags;
Stan Ilievf50806a2016-10-24 10:40:39 -0400291 rec->clipIndex = mClipStack.size();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400292}
293
Stan Ilievf50806a2016-10-24 10:40:39 -0400294template <typename T>
Mike Reed6e49c9f2016-12-02 15:36:59 -0500295void SkiaCanvas::recordClip(const T& clip, SkClipOp op) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400296 // Only need tracking when in a partial save frame which
297 // doesn't restore the clip.
298 const SaveRec* rec = this->currentSaveRec();
299 if (rec && !(rec->saveFlags & SaveFlags::Clip)) {
300 mClipStack.emplace_back(clip, op, mCanvas->getTotalMatrix());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400301 }
302}
303
Stan Ilievf50806a2016-10-24 10:40:39 -0400304// Applies and optionally removes all clips >= index.
305void SkiaCanvas::applyPersistentClips(size_t clipStartIndex) {
306 SkASSERT(clipStartIndex <= mClipStack.size());
307 const auto begin = mClipStack.cbegin() + clipStartIndex;
308 const auto end = mClipStack.cend();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400309
Stan Ilievf50806a2016-10-24 10:40:39 -0400310 // Clip application mutates the CTM.
311 const SkMatrix saveMatrix = mCanvas->getTotalMatrix();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400312
Stan Ilievf50806a2016-10-24 10:40:39 -0400313 for (auto clip = begin; clip != end; ++clip) {
Mike Reed6acfe162016-11-18 17:21:09 -0500314 clip->apply(mCanvas);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400315 }
316
Stan Ilievf50806a2016-10-24 10:40:39 -0400317 mCanvas->setMatrix(saveMatrix);
318
319 // If the current/post-restore save rec is also persisting clips, we
320 // leave them on the stack to be reapplied part of the next restore().
321 // Otherwise we're done and just pop them.
322 const auto* rec = this->currentSaveRec();
323 if (!rec || (rec->saveFlags & SaveFlags::Clip)) {
324 mClipStack.erase(begin, end);
325 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400326}
327
328// ----------------------------------------------------------------------------
329// Canvas state operations: Matrix
330// ----------------------------------------------------------------------------
331
332void SkiaCanvas::getMatrix(SkMatrix* outMatrix) const {
333 *outMatrix = mCanvas->getTotalMatrix();
334}
335
336void SkiaCanvas::setMatrix(const SkMatrix& matrix) {
337 mCanvas->setMatrix(matrix);
338}
339
340void SkiaCanvas::concat(const SkMatrix& matrix) {
341 mCanvas->concat(matrix);
342}
343
344void SkiaCanvas::rotate(float degrees) {
345 mCanvas->rotate(degrees);
346}
347
348void SkiaCanvas::scale(float sx, float sy) {
349 mCanvas->scale(sx, sy);
350}
351
352void SkiaCanvas::skew(float sx, float sy) {
353 mCanvas->skew(sx, sy);
354}
355
356void SkiaCanvas::translate(float dx, float dy) {
357 mCanvas->translate(dx, dy);
358}
359
360// ----------------------------------------------------------------------------
361// Canvas state operations: Clips
362// ----------------------------------------------------------------------------
363
364// This function is a mirror of SkCanvas::getClipBounds except that it does
365// not outset the edge of the clip to account for anti-aliasing. There is
366// a skia bug to investigate pushing this logic into back into skia.
367// (see https://code.google.com/p/skia/issues/detail?id=1303)
368bool SkiaCanvas::getClipBounds(SkRect* outRect) const {
369 SkIRect ibounds;
Mike Reed5e438982017-01-25 08:23:25 -0500370 if (!mCanvas->getDeviceClipBounds(&ibounds)) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400371 return false;
372 }
373
374 SkMatrix inverse;
375 // if we can't invert the CTM, we can't return local clip bounds
376 if (!mCanvas->getTotalMatrix().invert(&inverse)) {
377 if (outRect) {
378 outRect->setEmpty();
379 }
380 return false;
381 }
382
383 if (NULL != outRect) {
384 SkRect r = SkRect::Make(ibounds);
385 inverse.mapRect(outRect, r);
386 }
387 return true;
388}
389
390bool SkiaCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
391 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
392 return mCanvas->quickReject(bounds);
393}
394
395bool SkiaCanvas::quickRejectPath(const SkPath& path) const {
396 return mCanvas->quickReject(path);
397}
398
Mike Reed6e49c9f2016-12-02 15:36:59 -0500399bool SkiaCanvas::clipRect(float left, float top, float right, float bottom, SkClipOp op) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400400 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Stan Ilievf50806a2016-10-24 10:40:39 -0400401 this->recordClip(rect, op);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400402 mCanvas->clipRect(rect, op);
Chris Craik5ec6a282015-06-23 15:42:12 -0700403 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400404}
405
Mike Reed6e49c9f2016-12-02 15:36:59 -0500406bool SkiaCanvas::clipPath(const SkPath* path, SkClipOp op) {
Derek Sollenbergerf7d98f42017-04-17 11:27:36 -0400407 this->recordClip(*path, op);
408 mCanvas->clipPath(*path, op);
Chris Craik5ec6a282015-06-23 15:42:12 -0700409 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400410}
411
Derek Sollenberger8872b382014-06-23 14:13:53 -0400412// ----------------------------------------------------------------------------
413// Canvas state operations: Filters
414// ----------------------------------------------------------------------------
415
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400416SkDrawFilter* SkiaCanvas::getDrawFilter() {
417 return mCanvas->getDrawFilter();
418}
419
Derek Sollenberger8872b382014-06-23 14:13:53 -0400420void SkiaCanvas::setDrawFilter(SkDrawFilter* drawFilter) {
421 mCanvas->setDrawFilter(drawFilter);
422}
423
424// ----------------------------------------------------------------------------
Matt Sarettd0814db2017-04-13 09:33:18 -0400425// Canvas state operations: Capture
426// ----------------------------------------------------------------------------
427
428SkCanvasState* SkiaCanvas::captureCanvasState() const {
429 SkCanvas* canvas = mCanvas;
430 if (mCanvasOwned) {
431 // Important to use the underlying SkCanvas, not the wrapper.
432 canvas = mCanvasOwned.get();
433 }
434
435 // Workarounds for http://crbug.com/271096: SW draw only supports
436 // translate & scale transforms, and a simple rectangular clip.
437 // (This also avoids significant wasted time in calling
438 // SkCanvasStateUtils::CaptureCanvasState when the clip is complex).
John Reck1bcacfd2017-11-03 10:12:19 -0700439 if (!canvas->isClipRect() || (canvas->getTotalMatrix().getType() &
440 ~(SkMatrix::kTranslate_Mask | SkMatrix::kScale_Mask))) {
441 return nullptr;
Matt Sarettd0814db2017-04-13 09:33:18 -0400442 }
443
444 return SkCanvasStateUtils::CaptureCanvasState(canvas);
445}
446
447// ----------------------------------------------------------------------------
Derek Sollenberger8872b382014-06-23 14:13:53 -0400448// Canvas draw operations
449// ----------------------------------------------------------------------------
450
Mike Reed260ab722016-10-07 15:59:20 -0400451void SkiaCanvas::drawColor(int color, SkBlendMode mode) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400452 mCanvas->drawColor(color, mode);
453}
454
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400455void SkiaCanvas::drawPaint(const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400456 mCanvas->drawPaint(paint);
457}
458
459// ----------------------------------------------------------------------------
460// Canvas draw operations: Geometry
461// ----------------------------------------------------------------------------
462
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400463void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400464 SkCanvas::PointMode mode) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500465 if (CC_UNLIKELY(count < 2 || paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400466 // convert the floats into SkPoints
John Reck1bcacfd2017-11-03 10:12:19 -0700467 count >>= 1; // now it is the number of points
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400468 std::unique_ptr<SkPoint[]> pts(new SkPoint[count]);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400469 for (int i = 0; i < count; i++) {
470 pts[i].set(points[0], points[1]);
471 points += 2;
472 }
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400473 mCanvas->drawPoints(mode, count, pts.get(), paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400474}
475
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400476void SkiaCanvas::drawPoint(float x, float y, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400477 mCanvas->drawPoint(x, y, paint);
478}
479
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400480void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400481 this->drawPoints(points, count, paint, SkCanvas::kPoints_PointMode);
482}
483
484void SkiaCanvas::drawLine(float startX, float startY, float stopX, float stopY,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400485 const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400486 mCanvas->drawLine(startX, startY, stopX, stopY, paint);
487}
488
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400489void SkiaCanvas::drawLines(const float* points, int count, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500490 if (CC_UNLIKELY(count < 4 || paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400491 this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode);
492}
493
John Reck1bcacfd2017-11-03 10:12:19 -0700494void SkiaCanvas::drawRect(float left, float top, float right, float bottom, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500495 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Mike Reed6c9bb242017-04-04 09:15:37 -0400496 mCanvas->drawRect({left, top, right, bottom}, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400497}
498
Derek Sollenberger94394b32015-07-10 09:58:41 -0400499void SkiaCanvas::drawRegion(const SkRegion& region, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500500 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Stan Ilievf50806a2016-10-24 10:40:39 -0400501 mCanvas->drawRegion(region, paint);
Derek Sollenberger94394b32015-07-10 09:58:41 -0400502}
503
John Reck1bcacfd2017-11-03 10:12:19 -0700504void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom, float rx, float ry,
505 const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500506 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400507 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
508 mCanvas->drawRoundRect(rect, rx, ry, paint);
509}
510
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400511void SkiaCanvas::drawCircle(float x, float y, float radius, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500512 if (CC_UNLIKELY(radius <= 0 || paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400513 mCanvas->drawCircle(x, y, radius, paint);
514}
515
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400516void SkiaCanvas::drawOval(float left, float top, float right, float bottom, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500517 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400518 SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
519 mCanvas->drawOval(oval, paint);
520}
521
John Reck1bcacfd2017-11-03 10:12:19 -0700522void SkiaCanvas::drawArc(float left, float top, float right, float bottom, float startAngle,
523 float sweepAngle, bool useCenter, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500524 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400525 SkRect arc = SkRect::MakeLTRB(left, top, right, bottom);
Derek Sollenbergeref3b2182017-11-06 13:55:59 -0500526 if (fabs(sweepAngle) >= 360.0f) {
527 mCanvas->drawOval(arc, paint);
528 } else {
529 mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, paint);
530 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400531}
532
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400533void SkiaCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500534 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Stan Iliev6dcfdec2017-08-15 16:42:05 -0400535 if (CC_UNLIKELY(path.isEmpty() && (!path.isInverseFillType()))) {
536 return;
537 }
Derek Sollenbergerd7f13f82017-03-09 13:08:27 -0500538 mCanvas->drawPath(path, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400539}
540
Mike Reed826deef2017-04-04 15:32:04 -0400541void SkiaCanvas::drawVertices(const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint) {
542 mCanvas->drawVertices(vertices, mode, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400543}
544
545// ----------------------------------------------------------------------------
546// Canvas draw operations: Bitmaps
547// ----------------------------------------------------------------------------
548
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400549const SkPaint* SkiaCanvas::addFilter(const SkPaint* origPaint, SkPaint* tmpPaint,
John Reck1bcacfd2017-11-03 10:12:19 -0700550 sk_sp<SkColorFilter> colorSpaceFilter) {
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400551 /* We don't apply the colorSpace filter if this canvas is already wrapped with
552 * a SkColorSpaceXformCanvas since it already takes care of converting the
553 * contents of the bitmap into the appropriate colorspace. The mCanvasWrapper
554 * should only be used if this canvas is backed by a surface/bitmap that is known
555 * to have a non-sRGB colorspace.
556 */
557 if (!mCanvasWrapper && colorSpaceFilter) {
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400558 if (origPaint) {
559 *tmpPaint = *origPaint;
560 }
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400561
562 if (tmpPaint->getColorFilter()) {
John Reck1bcacfd2017-11-03 10:12:19 -0700563 tmpPaint->setColorFilter(
564 SkColorFilter::MakeComposeFilter(tmpPaint->refColorFilter(), colorSpaceFilter));
Derek Sollenbergerfa3e3402017-08-04 08:35:10 -0400565 LOG_ALWAYS_FATAL_IF(!tmpPaint->getColorFilter());
566 } else {
567 tmpPaint->setColorFilter(colorSpaceFilter);
568 }
569
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400570 return tmpPaint;
571 } else {
572 return origPaint;
573 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400574}
575
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400576void SkiaCanvas::drawBitmap(Bitmap& bitmap, float left, float top, const SkPaint* paint) {
577 SkPaint tmpPaint;
578 sk_sp<SkColorFilter> colorFilter;
579 sk_sp<SkImage> image = bitmap.makeImage(&colorFilter);
580 mCanvas->drawImage(image, left, top, addFilter(paint, &tmpPaint, colorFilter));
581}
582
583void SkiaCanvas::drawBitmap(Bitmap& bitmap, const SkMatrix& matrix, const SkPaint* paint) {
Mike Reed6acfe162016-11-18 17:21:09 -0500584 SkAutoCanvasRestore acr(mCanvas, true);
Mike Reed70ffbf92014-12-08 17:03:30 -0500585 mCanvas->concat(matrix);
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400586
587 SkPaint tmpPaint;
588 sk_sp<SkColorFilter> colorFilter;
589 sk_sp<SkImage> image = bitmap.makeImage(&colorFilter);
590 mCanvas->drawImage(image, 0, 0, addFilter(paint, &tmpPaint, colorFilter));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400591}
592
John Reck1bcacfd2017-11-03 10:12:19 -0700593void SkiaCanvas::drawBitmap(Bitmap& bitmap, float srcLeft, float srcTop, float srcRight,
594 float srcBottom, float dstLeft, float dstTop, float dstRight,
595 float dstBottom, const SkPaint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400596 SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
597 SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400598
599 SkPaint tmpPaint;
600 sk_sp<SkColorFilter> colorFilter;
601 sk_sp<SkImage> image = bitmap.makeImage(&colorFilter);
Derek Sollenberger6c2a9e22017-08-15 16:23:01 -0400602 mCanvas->drawImageRect(image, srcRect, dstRect, addFilter(paint, &tmpPaint, colorFilter),
John Reck1bcacfd2017-11-03 10:12:19 -0700603 SkCanvas::kFast_SrcRectConstraint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400604}
605
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400606void SkiaCanvas::drawBitmapMesh(Bitmap& bitmap, int meshWidth, int meshHeight,
John Reck1bcacfd2017-11-03 10:12:19 -0700607 const float* vertices, const int* colors, const SkPaint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400608 const int ptCount = (meshWidth + 1) * (meshHeight + 1);
609 const int indexCount = meshWidth * meshHeight * 6;
Mike Reed871cd2d2017-03-17 10:15:52 -0400610 uint32_t flags = SkVertices::kHasTexCoords_BuilderFlag;
611 if (colors) {
612 flags |= SkVertices::kHasColors_BuilderFlag;
613 }
Mike Reed826deef2017-04-04 15:32:04 -0400614 SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, ptCount, indexCount, flags);
Mike Reed871cd2d2017-03-17 10:15:52 -0400615 memcpy(builder.positions(), vertices, ptCount * sizeof(SkPoint));
616 if (colors) {
617 memcpy(builder.colors(), colors, ptCount * sizeof(SkColor));
618 }
619 SkPoint* texs = builder.texCoords();
620 uint16_t* indices = builder.indices();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400621
622 // cons up texture coordinates and indices
623 {
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400624 const SkScalar w = SkIntToScalar(bitmap.width());
625 const SkScalar h = SkIntToScalar(bitmap.height());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400626 const SkScalar dx = w / meshWidth;
627 const SkScalar dy = h / meshHeight;
628
629 SkPoint* texsPtr = texs;
630 SkScalar y = 0;
631 for (int i = 0; i <= meshHeight; i++) {
632 if (i == meshHeight) {
633 y = h; // to ensure numerically we hit h exactly
634 }
635 SkScalar x = 0;
636 for (int j = 0; j < meshWidth; j++) {
637 texsPtr->set(x, y);
638 texsPtr += 1;
639 x += dx;
640 }
641 texsPtr->set(w, y);
642 texsPtr += 1;
643 y += dy;
644 }
645 SkASSERT(texsPtr - texs == ptCount);
646 }
647
648 // cons up indices
649 {
650 uint16_t* indexPtr = indices;
651 int index = 0;
652 for (int i = 0; i < meshHeight; i++) {
653 for (int j = 0; j < meshWidth; j++) {
654 // lower-left triangle
655 *indexPtr++ = index;
656 *indexPtr++ = index + meshWidth + 1;
657 *indexPtr++ = index + meshWidth + 2;
658 // upper-right triangle
659 *indexPtr++ = index;
660 *indexPtr++ = index + meshWidth + 2;
661 *indexPtr++ = index + 1;
662 // bump to the next cell
663 index += 1;
664 }
665 // bump to the next row
666 index += 1;
667 }
668 SkASSERT(indexPtr - indices == indexCount);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400669 }
670
John Reck1bcacfd2017-11-03 10:12:19 -0700671// double-check that we have legal indices
Derek Sollenberger8872b382014-06-23 14:13:53 -0400672#ifdef SK_DEBUG
673 {
674 for (int i = 0; i < indexCount; i++) {
675 SkASSERT((unsigned)indices[i] < (unsigned)ptCount);
676 }
677 }
678#endif
679
680 // cons-up a shader for the bitmap
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400681 SkPaint tmpPaint;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400682 if (paint) {
683 tmpPaint = *paint;
684 }
Stan Ilievf50806a2016-10-24 10:40:39 -0400685
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400686 sk_sp<SkColorFilter> colorFilter;
687 sk_sp<SkImage> image = bitmap.makeImage(&colorFilter);
John Reck1bcacfd2017-11-03 10:12:19 -0700688 sk_sp<SkShader> shader =
689 image->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode);
690 if (colorFilter) {
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400691 shader = shader->makeWithColorFilter(colorFilter);
692 }
693 tmpPaint.setShader(shader);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400694
Mike Reed871cd2d2017-03-17 10:15:52 -0400695 mCanvas->drawVertices(builder.detach(), SkBlendMode::kModulate, tmpPaint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400696}
697
John Reck1bcacfd2017-11-03 10:12:19 -0700698void SkiaCanvas::drawNinePatch(Bitmap& bitmap, const Res_png_9patch& chunk, float dstLeft,
699 float dstTop, float dstRight, float dstBottom,
700 const SkPaint* paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400701 SkCanvas::Lattice lattice;
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400702 NinePatchUtils::SetLatticeDivs(&lattice, chunk, bitmap.width(), bitmap.height());
Stan Ilievf50806a2016-10-24 10:40:39 -0400703
Stan Ilieve12d7312017-12-04 14:48:27 -0500704 lattice.fRectTypes = nullptr;
705 lattice.fColors = nullptr;
Stan Ilievf50806a2016-10-24 10:40:39 -0400706 int numFlags = 0;
Stan Iliev021693b2016-10-17 16:26:15 -0400707 if (chunk.numColors > 0 && chunk.numColors == NinePatchUtils::NumDistinctRects(lattice)) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400708 // We can expect the framework to give us a color for every distinct rect.
709 // Skia requires a flag for every rect.
710 numFlags = (lattice.fXCount + 1) * (lattice.fYCount + 1);
711 }
712
Stan Ilieve12d7312017-12-04 14:48:27 -0500713 SkAutoSTMalloc<25, SkCanvas::Lattice::RectType> flags(numFlags);
714 SkAutoSTMalloc<25, SkColor> colors(numFlags);
Stan Ilievf50806a2016-10-24 10:40:39 -0400715 if (numFlags > 0) {
Stan Ilieve12d7312017-12-04 14:48:27 -0500716 NinePatchUtils::SetLatticeFlags(&lattice, flags.get(), numFlags, chunk, colors.get());
Stan Ilievf50806a2016-10-24 10:40:39 -0400717 }
718
719 lattice.fBounds = nullptr;
720 SkRect dst = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Derek Sollenbergerfb0c8fc2017-07-26 13:53:27 -0400721
722 SkPaint tmpPaint;
723 sk_sp<SkColorFilter> colorFilter;
724 sk_sp<SkImage> image = bitmap.makeImage(&colorFilter);
725 mCanvas->drawImageLattice(image.get(), lattice, dst, addFilter(paint, &tmpPaint, colorFilter));
Derek Sollenbergeredca3202015-07-10 13:56:39 -0400726}
727
Leon Scroggins III671cce22018-01-14 16:52:17 -0500728void SkiaCanvas::drawAnimatedImage(SkAnimatedImage* image, float left, float top,
729 const SkPaint* paint) {
730 sk_sp<SkPicture> pic(image->newPictureSnapshot());
731 SkMatrix matrixStorage;
732 SkMatrix* matrix;
733 if (left == 0.0f && top == 0.0f) {
734 matrix = nullptr;
735 } else {
736 matrixStorage = SkMatrix::MakeTrans(left, top);
737 matrix = &matrixStorage;
738 }
739 mCanvas->drawPicture(pic.get(), matrix, paint);
740}
741
Doris Liu766431a2016-02-04 22:17:11 +0000742void SkiaCanvas::drawVectorDrawable(VectorDrawableRoot* vectorDrawable) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800743 vectorDrawable->drawStaging(this);
Doris Liu766431a2016-02-04 22:17:11 +0000744}
745
Derek Sollenberger8872b382014-06-23 14:13:53 -0400746// ----------------------------------------------------------------------------
747// Canvas draw operations: Text
748// ----------------------------------------------------------------------------
749
Stan Iliev0b58d992017-03-30 18:22:27 -0400750void SkiaCanvas::drawGlyphs(ReadGlyphFunc glyphFunc, int count, const SkPaint& paint, float x,
John Reck1bcacfd2017-11-03 10:12:19 -0700751 float y, float boundsLeft, float boundsTop, float boundsRight,
752 float boundsBottom, float totalAdvance) {
Stan Iliev0b58d992017-03-30 18:22:27 -0400753 if (count <= 0 || paint.nothingToDraw()) return;
Stan Ilievf50806a2016-10-24 10:40:39 -0400754 // Set align to left for drawing, as we don't want individual
755 // glyphs centered or right-aligned; the offset above takes
756 // care of all alignment.
757 SkPaint paintCopy(paint);
758 paintCopy.setTextAlign(SkPaint::kLeft_Align);
Stan Ilieva39b7742017-11-29 13:15:45 -0500759 SkASSERT(paintCopy.getTextEncoding() == SkPaint::kGlyphID_TextEncoding);
Stan Ilievf50806a2016-10-24 10:40:39 -0400760
John Reck1bcacfd2017-11-03 10:12:19 -0700761 SkRect bounds =
762 SkRect::MakeLTRB(boundsLeft + x, boundsTop + y, boundsRight + x, boundsBottom + y);
Stan Ilievf50806a2016-10-24 10:40:39 -0400763
764 SkTextBlobBuilder builder;
765 const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(paintCopy, count, &bounds);
Stan Iliev0b58d992017-03-30 18:22:27 -0400766 glyphFunc(buffer.glyphs, buffer.pos);
Stan Ilievf50806a2016-10-24 10:40:39 -0400767
768 sk_sp<SkTextBlob> textBlob(builder.make());
769 mCanvas->drawTextBlob(textBlob, 0, 0, paintCopy);
770 drawTextDecorations(x, y, totalAdvance, paintCopy);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400771}
772
Yuqian Liafc221492016-07-18 13:07:42 -0400773void SkiaCanvas::drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
John Reck1bcacfd2017-11-03 10:12:19 -0700774 const SkPaint& paint, const SkPath& path, size_t start,
775 size_t end) {
Yuqian Liafc221492016-07-18 13:07:42 -0400776 const int N = end - start;
Derek Sollenbergere547dd02016-11-09 11:55:59 -0500777 SkAutoSTMalloc<1024, uint8_t> storage(N * (sizeof(uint16_t) + sizeof(SkRSXform)));
Yuqian Liafc221492016-07-18 13:07:42 -0400778 SkRSXform* xform = (SkRSXform*)storage.get();
779 uint16_t* glyphs = (uint16_t*)(xform + N);
780 SkPathMeasure meas(path, false);
781
782 for (size_t i = start; i < end; i++) {
783 glyphs[i - start] = layout.getGlyphId(i);
Stan Ilievc6c96dd2018-01-10 16:06:04 -0500784 float halfWidth = layout.getCharAdvance(i) * 0.5f;
785 float x = hOffset + layout.getX(i) + halfWidth;
Yuqian Liafc221492016-07-18 13:07:42 -0400786 float y = vOffset + layout.getY(i);
787
788 SkPoint pos;
789 SkVector tan;
790 if (!meas.getPosTan(x, &pos, &tan)) {
791 pos.set(x, y);
792 tan.set(1, 0);
793 }
794 xform[i - start].fSCos = tan.x();
795 xform[i - start].fSSin = tan.y();
Stan Ilievc6c96dd2018-01-10 16:06:04 -0500796 xform[i - start].fTx = pos.x() - tan.y() * y - halfWidth * tan.x();
797 xform[i - start].fTy = pos.y() + tan.x() * y - halfWidth * tan.y();
Yuqian Liafc221492016-07-18 13:07:42 -0400798 }
799
800 this->asSkCanvas()->drawTextRSXform(glyphs, sizeof(uint16_t) * N, xform, nullptr, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400801}
802
Derek Sollenberger6f485562015-07-30 10:00:39 -0400803// ----------------------------------------------------------------------------
804// Canvas draw operations: Animations
805// ----------------------------------------------------------------------------
806
Derek Sollenberger6f485562015-07-30 10:00:39 -0400807void SkiaCanvas::drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
John Reck1bcacfd2017-11-03 10:12:19 -0700808 uirenderer::CanvasPropertyPrimitive* top,
809 uirenderer::CanvasPropertyPrimitive* right,
810 uirenderer::CanvasPropertyPrimitive* bottom,
811 uirenderer::CanvasPropertyPrimitive* rx,
812 uirenderer::CanvasPropertyPrimitive* ry,
813 uirenderer::CanvasPropertyPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -0400814 sk_sp<uirenderer::skiapipeline::AnimatedRoundRect> drawable(
John Reck1bcacfd2017-11-03 10:12:19 -0700815 new uirenderer::skiapipeline::AnimatedRoundRect(left, top, right, bottom, rx, ry,
816 paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400817 mCanvas->drawDrawable(drawable.get());
818}
819
John Reck1bcacfd2017-11-03 10:12:19 -0700820void SkiaCanvas::drawCircle(uirenderer::CanvasPropertyPrimitive* x,
821 uirenderer::CanvasPropertyPrimitive* y,
822 uirenderer::CanvasPropertyPrimitive* radius,
823 uirenderer::CanvasPropertyPaint* paint) {
824 sk_sp<uirenderer::skiapipeline::AnimatedCircle> drawable(
825 new uirenderer::skiapipeline::AnimatedCircle(x, y, radius, paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400826 mCanvas->drawDrawable(drawable.get());
827}
828
829// ----------------------------------------------------------------------------
830// Canvas draw operations: View System
831// ----------------------------------------------------------------------------
832
Stan Ilievf50806a2016-10-24 10:40:39 -0400833void SkiaCanvas::drawLayer(uirenderer::DeferredLayerUpdater* layerUpdater) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400834 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw Layers");
835}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400836
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400837void SkiaCanvas::drawRenderNode(uirenderer::RenderNode* renderNode) {
838 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw RenderNodes");
839}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400840
John Reckcd1c3eb2016-04-14 10:38:54 -0700841void SkiaCanvas::callDrawGLFunction(Functor* functor,
John Reck1bcacfd2017-11-03 10:12:19 -0700842 uirenderer::GlFunctorLifecycleListener* listener) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400843 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw GL Content");
844}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400845
John Reck1bcacfd2017-11-03 10:12:19 -0700846} // namespace android