blob: 812e4d885b39142a31ca727fd54e56a17396180f [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
Derek Sollenberger6f485562015-07-30 10:00:39 -040026#include <SkDrawable.h>
John Reck849911a2015-01-20 07:51:14 -080027#include <SkDeque.h>
28#include <SkDrawFilter.h>
29#include <SkGraphics.h>
Derek Sollenberger6f485562015-07-30 10:00:39 -040030#include <SkImage.h>
Matt Sarett62feb3a2016-09-20 10:34:11 -040031#include <SkImagePriv.h>
Yuqian Liafc221492016-07-18 13:07:42 -040032#include <SkRSXform.h>
John Reck849911a2015-01-20 07:51:14 -080033#include <SkShader.h>
John Reck849911a2015-01-20 07:51:14 -080034#include <SkTemplates.h>
Stan Ilievf50806a2016-10-24 10:40:39 -040035#include <SkTextBlob.h>
Derek Sollenberger8872b382014-06-23 14:13:53 -040036
Ben Wagner60126ef2015-08-07 12:13:48 -040037#include <memory>
38
Derek Sollenberger8872b382014-06-23 14:13:53 -040039namespace android {
40
Stan Ilievf50806a2016-10-24 10:40:39 -040041using uirenderer::PaintUtils;
42
John Reckc1b33d62015-04-22 09:04:45 -070043Canvas* Canvas::create_canvas(const SkBitmap& bitmap) {
Derek Sollenberger8872b382014-06-23 14:13:53 -040044 return new SkiaCanvas(bitmap);
45}
46
47Canvas* Canvas::create_canvas(SkCanvas* skiaCanvas) {
48 return new SkiaCanvas(skiaCanvas);
49}
50
Stan Ilievf50806a2016-10-24 10:40:39 -040051SkiaCanvas::SkiaCanvas() {}
52
53SkiaCanvas::SkiaCanvas(SkCanvas* canvas)
Mike Reed6acfe162016-11-18 17:21:09 -050054 : mCanvas(canvas) {}
Stan Ilievf50806a2016-10-24 10:40:39 -040055
John Reckc1b33d62015-04-22 09:04:45 -070056SkiaCanvas::SkiaCanvas(const SkBitmap& bitmap) {
Mike Reed6acfe162016-11-18 17:21:09 -050057 mCanvasOwned = std::unique_ptr<SkCanvas>(new SkCanvas(bitmap));
58 mCanvas = mCanvasOwned.get();
Derek Sollenberger8872b382014-06-23 14:13:53 -040059}
60
Stan Iliev021693b2016-10-17 16:26:15 -040061SkiaCanvas::~SkiaCanvas() {}
62
Derek Sollenbergerc1908132016-07-15 10:28:16 -040063void SkiaCanvas::reset(SkCanvas* skiaCanvas) {
Mike Reed6acfe162016-11-18 17:21:09 -050064 if (mCanvas != skiaCanvas) {
65 mCanvas = skiaCanvas;
66 mCanvasOwned.reset();
67 }
Derek Sollenbergerc1908132016-07-15 10:28:16 -040068 mSaveStack.reset(nullptr);
69 mHighContrastText = false;
70}
71
Derek Sollenberger8872b382014-06-23 14:13:53 -040072// ----------------------------------------------------------------------------
73// Canvas state operations: Replace Bitmap
74// ----------------------------------------------------------------------------
75
Tony Mantler4f641d12017-03-14 22:36:14 +000076class ClipCopier : public SkCanvas::ClipVisitor {
77public:
78 explicit ClipCopier(SkCanvas* dstCanvas) : m_dstCanvas(dstCanvas) {}
79
80 virtual void clipRect(const SkRect& rect, SkClipOp op, bool antialias) {
81 m_dstCanvas->clipRect(rect, op, antialias);
82 }
83 virtual void clipRRect(const SkRRect& rrect, SkClipOp op, bool antialias) {
84 m_dstCanvas->clipRRect(rrect, op, antialias);
85 }
86 virtual void clipPath(const SkPath& path, SkClipOp op, bool antialias) {
87 m_dstCanvas->clipPath(path, op, antialias);
88 }
89
90private:
91 SkCanvas* m_dstCanvas;
92};
93
John Reckc1b33d62015-04-22 09:04:45 -070094void SkiaCanvas::setBitmap(const SkBitmap& bitmap) {
Tony Mantler4f641d12017-03-14 22:36:14 +000095 SkCanvas* newCanvas = new SkCanvas(bitmap);
96
97 if (!bitmap.isNull()) {
98 // Copy the canvas matrix & clip state.
99 newCanvas->setMatrix(mCanvas->getTotalMatrix());
100
101 ClipCopier copier(newCanvas);
102 mCanvas->replayClips(&copier);
103 }
104
105 // deletes the previously owned canvas (if any)
106 mCanvasOwned = std::unique_ptr<SkCanvas>(newCanvas);
107 mCanvas = newCanvas;
108
Derek Sollenberger8872b382014-06-23 14:13:53 -0400109 // clean up the old save stack
Stan Ilievf50806a2016-10-24 10:40:39 -0400110 mSaveStack.reset(nullptr);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400111}
112
113// ----------------------------------------------------------------------------
114// Canvas state operations
115// ----------------------------------------------------------------------------
116
117bool SkiaCanvas::isOpaque() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400118 return mCanvas->imageInfo().isOpaque();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400119}
120
121int SkiaCanvas::width() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400122 return mCanvas->imageInfo().width();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400123}
124
125int SkiaCanvas::height() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400126 return mCanvas->imageInfo().height();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400127}
128
129// ----------------------------------------------------------------------------
130// Canvas state operations: Save (layer)
131// ----------------------------------------------------------------------------
132
133int SkiaCanvas::getSaveCount() const {
134 return mCanvas->getSaveCount();
135}
136
Florin Malitaeecff562015-12-21 10:43:01 -0500137int SkiaCanvas::save(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400138 int count = mCanvas->save();
139 recordPartialSave(flags);
140 return count;
141}
142
Florin Malita5e271402015-11-04 14:36:02 -0500143// The SkiaCanvas::restore operation layers on the capability to preserve
144// either (or both) the matrix and/or clip state after a SkCanvas::restore
145// operation. It does this by explicitly saving off the clip & matrix state
146// when requested and playing it back after the SkCanvas::restore.
Derek Sollenberger8872b382014-06-23 14:13:53 -0400147void SkiaCanvas::restore() {
Stan Ilievf50806a2016-10-24 10:40:39 -0400148 const auto* rec = this->currentSaveRec();
149 if (!rec) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400150 // Fast path - no record for this frame.
151 mCanvas->restore();
152 return;
153 }
154
Florin Malitaeecff562015-12-21 10:43:01 -0500155 bool preserveMatrix = !(rec->saveFlags & SaveFlags::Matrix);
156 bool preserveClip = !(rec->saveFlags & SaveFlags::Clip);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400157
158 SkMatrix savedMatrix;
159 if (preserveMatrix) {
160 savedMatrix = mCanvas->getTotalMatrix();
161 }
162
Stan Ilievf50806a2016-10-24 10:40:39 -0400163 const size_t clipIndex = rec->clipIndex;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400164
165 mCanvas->restore();
Stan Ilievf50806a2016-10-24 10:40:39 -0400166 mSaveStack->pop_back();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400167
168 if (preserveMatrix) {
169 mCanvas->setMatrix(savedMatrix);
170 }
171
Stan Ilievf50806a2016-10-24 10:40:39 -0400172 if (preserveClip) {
173 this->applyPersistentClips(clipIndex);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400174 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400175}
176
177void SkiaCanvas::restoreToCount(int restoreCount) {
178 while (mCanvas->getSaveCount() > restoreCount) {
179 this->restore();
180 }
181}
182
Florin Malitaeecff562015-12-21 10:43:01 -0500183static inline SkCanvas::SaveLayerFlags layerFlags(SaveFlags::Flags flags) {
184 SkCanvas::SaveLayerFlags layerFlags = 0;
185
Yuqian Li83427ff2016-09-14 11:14:06 -0400186 // We intentionally ignore the SaveFlags::HasAlphaLayer and
187 // SkCanvas::kIsOpaque_SaveLayerFlag flags because HWUI ignores it
188 // and our Android client may use it incorrectly.
189 // In Skia, this flag is purely for performance optimization.
Florin Malitaeecff562015-12-21 10:43:01 -0500190
191 if (!(flags & SaveFlags::ClipToLayer)) {
192 layerFlags |= SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag;
193 }
194
195 return layerFlags;
196}
197
Derek Sollenberger8872b382014-06-23 14:13:53 -0400198int SkiaCanvas::saveLayer(float left, float top, float right, float bottom,
Florin Malitaeecff562015-12-21 10:43:01 -0500199 const SkPaint* paint, SaveFlags::Flags flags) {
200 const SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
Derek Sollenbergerb8201192017-01-09 16:11:59 -0500201 const SkCanvas::SaveLayerRec rec(&bounds, paint, layerFlags(flags));
Florin Malitaeecff562015-12-21 10:43:01 -0500202
Stan Iliev68885e32016-12-14 11:18:34 -0500203 return mCanvas->saveLayer(rec);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400204}
205
206int SkiaCanvas::saveLayerAlpha(float left, float top, float right, float bottom,
Florin Malitaeecff562015-12-21 10:43:01 -0500207 int alpha, SaveFlags::Flags flags) {
Florin Malitaeecff562015-12-21 10:43:01 -0500208 if (static_cast<unsigned>(alpha) < 0xFF) {
Yuqian Lifd92ee42016-04-27 17:03:38 -0400209 SkPaint alphaPaint;
210 alphaPaint.setAlpha(alpha);
211 return this->saveLayer(left, top, right, bottom, &alphaPaint, flags);
Florin Malitaeecff562015-12-21 10:43:01 -0500212 }
Yuqian Lifd92ee42016-04-27 17:03:38 -0400213 return this->saveLayer(left, top, right, bottom, nullptr, flags);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400214}
215
Stan Ilievf50806a2016-10-24 10:40:39 -0400216class SkiaCanvas::Clip {
217public:
Mike Reed6e49c9f2016-12-02 15:36:59 -0500218 Clip(const SkRect& rect, SkClipOp op, const SkMatrix& m)
Stan Ilievf50806a2016-10-24 10:40:39 -0400219 : mType(Type::Rect), mOp(op), mMatrix(m), mRRect(SkRRect::MakeRect(rect)) {}
Mike Reed6e49c9f2016-12-02 15:36:59 -0500220 Clip(const SkRRect& rrect, SkClipOp op, const SkMatrix& m)
Stan Ilievf50806a2016-10-24 10:40:39 -0400221 : mType(Type::RRect), mOp(op), mMatrix(m), mRRect(rrect) {}
Mike Reed6e49c9f2016-12-02 15:36:59 -0500222 Clip(const SkPath& path, SkClipOp op, const SkMatrix& m)
Stan Ilievf50806a2016-10-24 10:40:39 -0400223 : mType(Type::Path), mOp(op), mMatrix(m), mPath(&path) {}
224
225 void apply(SkCanvas* canvas) const {
226 canvas->setMatrix(mMatrix);
227 switch (mType) {
228 case Type::Rect:
229 canvas->clipRect(mRRect.rect(), mOp);
230 break;
231 case Type::RRect:
232 canvas->clipRRect(mRRect, mOp);
233 break;
234 case Type::Path:
235 canvas->clipPath(*mPath.get(), mOp);
236 break;
237 }
238 }
239
240private:
241 enum class Type {
242 Rect,
243 RRect,
244 Path,
245 };
246
Mike Reed6e49c9f2016-12-02 15:36:59 -0500247 Type mType;
248 SkClipOp mOp;
249 SkMatrix mMatrix;
Stan Ilievf50806a2016-10-24 10:40:39 -0400250
251 // These are logically a union (tracked separately due to non-POD path).
252 SkTLazy<SkPath> mPath;
253 SkRRect mRRect;
254};
255
256const SkiaCanvas::SaveRec* SkiaCanvas::currentSaveRec() const {
257 const SaveRec* rec = mSaveStack
258 ? static_cast<const SaveRec*>(mSaveStack->back())
259 : nullptr;
260 int currentSaveCount = mCanvas->getSaveCount();
261 SkASSERT(!rec || currentSaveCount >= rec->saveCount);
262
263 return (rec && rec->saveCount == currentSaveCount) ? rec : nullptr;
264}
265
Derek Sollenberger8872b382014-06-23 14:13:53 -0400266// ----------------------------------------------------------------------------
267// functions to emulate legacy SaveFlags (i.e. independent matrix/clip flags)
268// ----------------------------------------------------------------------------
269
Florin Malitaeecff562015-12-21 10:43:01 -0500270void SkiaCanvas::recordPartialSave(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400271 // A partial save is a save operation which doesn't capture the full canvas state.
Florin Malitaeecff562015-12-21 10:43:01 -0500272 // (either SaveFlags::Matrix or SaveFlags::Clip is missing).
Derek Sollenberger8872b382014-06-23 14:13:53 -0400273
274 // Mask-out non canvas state bits.
Florin Malitaeecff562015-12-21 10:43:01 -0500275 flags &= SaveFlags::MatrixClip;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400276
Florin Malitaeecff562015-12-21 10:43:01 -0500277 if (flags == SaveFlags::MatrixClip) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400278 // not a partial save.
279 return;
280 }
281
Stan Ilievf50806a2016-10-24 10:40:39 -0400282 if (!mSaveStack) {
Ben Wagnerd1cbc162015-08-19 12:45:09 -0400283 mSaveStack.reset(new SkDeque(sizeof(struct SaveRec), 8));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400284 }
285
286 SaveRec* rec = static_cast<SaveRec*>(mSaveStack->push_back());
Florin Malita5e271402015-11-04 14:36:02 -0500287 rec->saveCount = mCanvas->getSaveCount();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400288 rec->saveFlags = flags;
Stan Ilievf50806a2016-10-24 10:40:39 -0400289 rec->clipIndex = mClipStack.size();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400290}
291
Stan Ilievf50806a2016-10-24 10:40:39 -0400292template <typename T>
Mike Reed6e49c9f2016-12-02 15:36:59 -0500293void SkiaCanvas::recordClip(const T& clip, SkClipOp op) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400294 // Only need tracking when in a partial save frame which
295 // doesn't restore the clip.
296 const SaveRec* rec = this->currentSaveRec();
297 if (rec && !(rec->saveFlags & SaveFlags::Clip)) {
298 mClipStack.emplace_back(clip, op, mCanvas->getTotalMatrix());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400299 }
300}
301
Stan Ilievf50806a2016-10-24 10:40:39 -0400302// Applies and optionally removes all clips >= index.
303void SkiaCanvas::applyPersistentClips(size_t clipStartIndex) {
304 SkASSERT(clipStartIndex <= mClipStack.size());
305 const auto begin = mClipStack.cbegin() + clipStartIndex;
306 const auto end = mClipStack.cend();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400307
Stan Ilievf50806a2016-10-24 10:40:39 -0400308 // Clip application mutates the CTM.
309 const SkMatrix saveMatrix = mCanvas->getTotalMatrix();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400310
Stan Ilievf50806a2016-10-24 10:40:39 -0400311 for (auto clip = begin; clip != end; ++clip) {
Mike Reed6acfe162016-11-18 17:21:09 -0500312 clip->apply(mCanvas);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400313 }
314
Stan Ilievf50806a2016-10-24 10:40:39 -0400315 mCanvas->setMatrix(saveMatrix);
316
317 // If the current/post-restore save rec is also persisting clips, we
318 // leave them on the stack to be reapplied part of the next restore().
319 // Otherwise we're done and just pop them.
320 const auto* rec = this->currentSaveRec();
321 if (!rec || (rec->saveFlags & SaveFlags::Clip)) {
322 mClipStack.erase(begin, end);
323 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400324}
325
326// ----------------------------------------------------------------------------
327// Canvas state operations: Matrix
328// ----------------------------------------------------------------------------
329
330void SkiaCanvas::getMatrix(SkMatrix* outMatrix) const {
331 *outMatrix = mCanvas->getTotalMatrix();
332}
333
334void SkiaCanvas::setMatrix(const SkMatrix& matrix) {
335 mCanvas->setMatrix(matrix);
336}
337
338void SkiaCanvas::concat(const SkMatrix& matrix) {
339 mCanvas->concat(matrix);
340}
341
342void SkiaCanvas::rotate(float degrees) {
343 mCanvas->rotate(degrees);
344}
345
346void SkiaCanvas::scale(float sx, float sy) {
347 mCanvas->scale(sx, sy);
348}
349
350void SkiaCanvas::skew(float sx, float sy) {
351 mCanvas->skew(sx, sy);
352}
353
354void SkiaCanvas::translate(float dx, float dy) {
355 mCanvas->translate(dx, dy);
356}
357
358// ----------------------------------------------------------------------------
359// Canvas state operations: Clips
360// ----------------------------------------------------------------------------
361
362// This function is a mirror of SkCanvas::getClipBounds except that it does
363// not outset the edge of the clip to account for anti-aliasing. There is
364// a skia bug to investigate pushing this logic into back into skia.
365// (see https://code.google.com/p/skia/issues/detail?id=1303)
366bool SkiaCanvas::getClipBounds(SkRect* outRect) const {
367 SkIRect ibounds;
Mike Reed5e438982017-01-25 08:23:25 -0500368 if (!mCanvas->getDeviceClipBounds(&ibounds)) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400369 return false;
370 }
371
372 SkMatrix inverse;
373 // if we can't invert the CTM, we can't return local clip bounds
374 if (!mCanvas->getTotalMatrix().invert(&inverse)) {
375 if (outRect) {
376 outRect->setEmpty();
377 }
378 return false;
379 }
380
381 if (NULL != outRect) {
382 SkRect r = SkRect::Make(ibounds);
383 inverse.mapRect(outRect, r);
384 }
385 return true;
386}
387
388bool SkiaCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
389 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
390 return mCanvas->quickReject(bounds);
391}
392
393bool SkiaCanvas::quickRejectPath(const SkPath& path) const {
394 return mCanvas->quickReject(path);
395}
396
Mike Reed6e49c9f2016-12-02 15:36:59 -0500397bool SkiaCanvas::clipRect(float left, float top, float right, float bottom, SkClipOp op) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400398 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Stan Ilievf50806a2016-10-24 10:40:39 -0400399 this->recordClip(rect, op);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400400 mCanvas->clipRect(rect, op);
Chris Craik5ec6a282015-06-23 15:42:12 -0700401 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400402}
403
Mike Reed6e49c9f2016-12-02 15:36:59 -0500404bool SkiaCanvas::clipPath(const SkPath* path, SkClipOp op) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400405 SkRRect roundRect;
406 if (path->isRRect(&roundRect)) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400407 this->recordClip(roundRect, op);
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400408 mCanvas->clipRRect(roundRect, op);
409 } else {
Stan Ilievf50806a2016-10-24 10:40:39 -0400410 this->recordClip(*path, op);
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400411 mCanvas->clipPath(*path, op);
412 }
Chris Craik5ec6a282015-06-23 15:42:12 -0700413 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400414}
415
Derek Sollenberger8872b382014-06-23 14:13:53 -0400416// ----------------------------------------------------------------------------
417// Canvas state operations: Filters
418// ----------------------------------------------------------------------------
419
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400420SkDrawFilter* SkiaCanvas::getDrawFilter() {
421 return mCanvas->getDrawFilter();
422}
423
Derek Sollenberger8872b382014-06-23 14:13:53 -0400424void SkiaCanvas::setDrawFilter(SkDrawFilter* drawFilter) {
425 mCanvas->setDrawFilter(drawFilter);
426}
427
428// ----------------------------------------------------------------------------
429// Canvas draw operations
430// ----------------------------------------------------------------------------
431
Mike Reed260ab722016-10-07 15:59:20 -0400432void SkiaCanvas::drawColor(int color, SkBlendMode mode) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400433 mCanvas->drawColor(color, mode);
434}
435
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400436void SkiaCanvas::drawPaint(const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400437 mCanvas->drawPaint(paint);
438}
439
440// ----------------------------------------------------------------------------
441// Canvas draw operations: Geometry
442// ----------------------------------------------------------------------------
443
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400444void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400445 SkCanvas::PointMode mode) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500446 if (CC_UNLIKELY(count < 2 || paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400447 // convert the floats into SkPoints
448 count >>= 1; // now it is the number of points
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400449 std::unique_ptr<SkPoint[]> pts(new SkPoint[count]);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400450 for (int i = 0; i < count; i++) {
451 pts[i].set(points[0], points[1]);
452 points += 2;
453 }
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400454 mCanvas->drawPoints(mode, count, pts.get(), paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400455}
456
457
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400458void SkiaCanvas::drawPoint(float x, float y, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400459 mCanvas->drawPoint(x, y, paint);
460}
461
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400462void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400463 this->drawPoints(points, count, paint, SkCanvas::kPoints_PointMode);
464}
465
466void SkiaCanvas::drawLine(float startX, float startY, float stopX, float stopY,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400467 const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400468 mCanvas->drawLine(startX, startY, stopX, stopY, paint);
469}
470
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400471void SkiaCanvas::drawLines(const float* points, int count, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500472 if (CC_UNLIKELY(count < 4 || paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400473 this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode);
474}
475
476void SkiaCanvas::drawRect(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400477 const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500478 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400479 mCanvas->drawRectCoords(left, top, right, bottom, paint);
480
481}
482
Derek Sollenberger94394b32015-07-10 09:58:41 -0400483void SkiaCanvas::drawRegion(const SkRegion& region, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500484 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Stan Ilievf50806a2016-10-24 10:40:39 -0400485 mCanvas->drawRegion(region, paint);
Derek Sollenberger94394b32015-07-10 09:58:41 -0400486}
487
Derek Sollenberger8872b382014-06-23 14:13:53 -0400488void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400489 float rx, float ry, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500490 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400491 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
492 mCanvas->drawRoundRect(rect, rx, ry, paint);
493}
494
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400495void SkiaCanvas::drawCircle(float x, float y, float radius, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500496 if (CC_UNLIKELY(radius <= 0 || paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400497 mCanvas->drawCircle(x, y, radius, paint);
498}
499
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400500void SkiaCanvas::drawOval(float left, float top, float right, float bottom, 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 oval = SkRect::MakeLTRB(left, top, right, bottom);
503 mCanvas->drawOval(oval, paint);
504}
505
506void SkiaCanvas::drawArc(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400507 float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500508 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400509 SkRect arc = SkRect::MakeLTRB(left, top, right, bottom);
510 mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, paint);
511}
512
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400513void SkiaCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500514 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenbergerd7f13f82017-03-09 13:08:27 -0500515 mCanvas->drawPath(path, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400516}
517
518void SkiaCanvas::drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
519 const float* verts, const float* texs, const int* colors,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400520 const uint16_t* indices, int indexCount, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400521#ifndef SK_SCALAR_IS_FLOAT
522 SkDEBUGFAIL("SkScalar must be a float for these conversions to be valid");
523#endif
524 const int ptCount = vertexCount >> 1;
525 mCanvas->drawVertices(vertexMode, ptCount, (SkPoint*)verts, (SkPoint*)texs,
Mike Reedc2f31df2016-10-28 17:21:45 -0400526 (SkColor*)colors, indices, indexCount, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400527}
528
529// ----------------------------------------------------------------------------
530// Canvas draw operations: Bitmaps
531// ----------------------------------------------------------------------------
532
sergeyvaed7f582016-10-14 16:30:21 -0700533void SkiaCanvas::drawBitmap(Bitmap& bitmap, float left, float top, const SkPaint* paint) {
534 SkBitmap skBitmap;
535 bitmap.getSkBitmap(&skBitmap);
536 mCanvas->drawBitmap(skBitmap, left, top, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400537}
538
sergeyvfc9999502016-10-17 13:07:38 -0700539void SkiaCanvas::drawBitmap(Bitmap& hwuiBitmap, const SkMatrix& matrix, const SkPaint* paint) {
540 SkBitmap bitmap;
541 hwuiBitmap.getSkBitmap(&bitmap);
Mike Reed6acfe162016-11-18 17:21:09 -0500542 SkAutoCanvasRestore acr(mCanvas, true);
Mike Reed70ffbf92014-12-08 17:03:30 -0500543 mCanvas->concat(matrix);
544 mCanvas->drawBitmap(bitmap, 0, 0, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400545}
546
sergeyvfc9999502016-10-17 13:07:38 -0700547void SkiaCanvas::drawBitmap(Bitmap& hwuiBitmap, float srcLeft, float srcTop,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400548 float srcRight, float srcBottom, float dstLeft, float dstTop,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400549 float dstRight, float dstBottom, const SkPaint* paint) {
sergeyvfc9999502016-10-17 13:07:38 -0700550 SkBitmap bitmap;
551 hwuiBitmap.getSkBitmap(&bitmap);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400552 SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
553 SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400554 mCanvas->drawBitmapRect(bitmap, srcRect, dstRect, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400555}
556
sergeyv5fd2a1c2016-10-20 15:04:28 -0700557void SkiaCanvas::drawBitmapMesh(Bitmap& hwuiBitmap, int meshWidth, int meshHeight,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400558 const float* vertices, const int* colors, const SkPaint* paint) {
sergeyv5fd2a1c2016-10-20 15:04:28 -0700559 SkBitmap bitmap;
560 hwuiBitmap.getSkBitmap(&bitmap);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400561 const int ptCount = (meshWidth + 1) * (meshHeight + 1);
562 const int indexCount = meshWidth * meshHeight * 6;
563
564 /* Our temp storage holds 2 or 3 arrays.
565 texture points [ptCount * sizeof(SkPoint)]
566 optionally vertex points [ptCount * sizeof(SkPoint)] if we need a
567 copy to convert from float to fixed
568 indices [ptCount * sizeof(uint16_t)]
569 */
570 ssize_t storageSize = ptCount * sizeof(SkPoint); // texs[]
571 storageSize += indexCount * sizeof(uint16_t); // indices[]
572
573
574#ifndef SK_SCALAR_IS_FLOAT
575 SkDEBUGFAIL("SkScalar must be a float for these conversions to be valid");
576#endif
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400577 std::unique_ptr<char[]> storage(new char[storageSize]);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400578 SkPoint* texs = (SkPoint*)storage.get();
579 uint16_t* indices = (uint16_t*)(texs + ptCount);
580
581 // cons up texture coordinates and indices
582 {
583 const SkScalar w = SkIntToScalar(bitmap.width());
584 const SkScalar h = SkIntToScalar(bitmap.height());
585 const SkScalar dx = w / meshWidth;
586 const SkScalar dy = h / meshHeight;
587
588 SkPoint* texsPtr = texs;
589 SkScalar y = 0;
590 for (int i = 0; i <= meshHeight; i++) {
591 if (i == meshHeight) {
592 y = h; // to ensure numerically we hit h exactly
593 }
594 SkScalar x = 0;
595 for (int j = 0; j < meshWidth; j++) {
596 texsPtr->set(x, y);
597 texsPtr += 1;
598 x += dx;
599 }
600 texsPtr->set(w, y);
601 texsPtr += 1;
602 y += dy;
603 }
604 SkASSERT(texsPtr - texs == ptCount);
605 }
606
607 // cons up indices
608 {
609 uint16_t* indexPtr = indices;
610 int index = 0;
611 for (int i = 0; i < meshHeight; i++) {
612 for (int j = 0; j < meshWidth; j++) {
613 // lower-left triangle
614 *indexPtr++ = index;
615 *indexPtr++ = index + meshWidth + 1;
616 *indexPtr++ = index + meshWidth + 2;
617 // upper-right triangle
618 *indexPtr++ = index;
619 *indexPtr++ = index + meshWidth + 2;
620 *indexPtr++ = index + 1;
621 // bump to the next cell
622 index += 1;
623 }
624 // bump to the next row
625 index += 1;
626 }
627 SkASSERT(indexPtr - indices == indexCount);
628 SkASSERT((char*)indexPtr - (char*)storage.get() == storageSize);
629 }
630
631 // double-check that we have legal indices
632#ifdef SK_DEBUG
633 {
634 for (int i = 0; i < indexCount; i++) {
635 SkASSERT((unsigned)indices[i] < (unsigned)ptCount);
636 }
637 }
638#endif
639
640 // cons-up a shader for the bitmap
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400641 SkPaint tmpPaint;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400642 if (paint) {
643 tmpPaint = *paint;
644 }
Stan Ilievf50806a2016-10-24 10:40:39 -0400645
646 sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(bitmap, kNever_SkCopyPixelsMode);
647 tmpPaint.setShader(image->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400648
649 mCanvas->drawVertices(SkCanvas::kTriangles_VertexMode, ptCount, (SkPoint*)vertices,
Mike Reedc2f31df2016-10-28 17:21:45 -0400650 texs, (const SkColor*)colors, indices,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400651 indexCount, tmpPaint);
652}
653
sergeyv5fd2a1c2016-10-20 15:04:28 -0700654void SkiaCanvas::drawNinePatch(Bitmap& hwuiBitmap, const Res_png_9patch& chunk,
Derek Sollenbergeredca3202015-07-10 13:56:39 -0400655 float dstLeft, float dstTop, float dstRight, float dstBottom, const SkPaint* paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400656
sergeyv5fd2a1c2016-10-20 15:04:28 -0700657 SkBitmap bitmap;
658 hwuiBitmap.getSkBitmap(&bitmap);
Stan Ilievf50806a2016-10-24 10:40:39 -0400659
660 SkCanvas::Lattice lattice;
Matt Sarett7de73852016-10-25 18:36:39 -0400661 NinePatchUtils::SetLatticeDivs(&lattice, chunk, bitmap.width(), bitmap.height());
Stan Ilievf50806a2016-10-24 10:40:39 -0400662
663 lattice.fFlags = nullptr;
664 int numFlags = 0;
Stan Iliev021693b2016-10-17 16:26:15 -0400665 if (chunk.numColors > 0 && chunk.numColors == NinePatchUtils::NumDistinctRects(lattice)) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400666 // We can expect the framework to give us a color for every distinct rect.
667 // Skia requires a flag for every rect.
668 numFlags = (lattice.fXCount + 1) * (lattice.fYCount + 1);
669 }
670
671 SkAutoSTMalloc<25, SkCanvas::Lattice::Flags> flags(numFlags);
672 if (numFlags > 0) {
Stan Iliev021693b2016-10-17 16:26:15 -0400673 NinePatchUtils::SetLatticeFlags(&lattice, flags.get(), numFlags, chunk);
Stan Ilievf50806a2016-10-24 10:40:39 -0400674 }
675
676 lattice.fBounds = nullptr;
677 SkRect dst = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
678 mCanvas->drawBitmapLattice(bitmap, lattice, dst, paint);
Derek Sollenbergeredca3202015-07-10 13:56:39 -0400679}
680
Doris Liu766431a2016-02-04 22:17:11 +0000681void SkiaCanvas::drawVectorDrawable(VectorDrawableRoot* vectorDrawable) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800682 vectorDrawable->drawStaging(this);
Doris Liu766431a2016-02-04 22:17:11 +0000683}
684
Derek Sollenberger8872b382014-06-23 14:13:53 -0400685// ----------------------------------------------------------------------------
686// Canvas draw operations: Text
687// ----------------------------------------------------------------------------
688
sergeyvdccca442016-03-21 15:38:21 -0700689void SkiaCanvas::drawGlyphs(const uint16_t* text, const float* positions, int count,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400690 const SkPaint& paint, float x, float y,
Tom Hudson8dfaa492014-12-09 15:03:44 -0500691 float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
692 float totalAdvance) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500693 if (!text || !positions || count <= 0 || paint.nothingToDraw()) return;
Stan Ilievf50806a2016-10-24 10:40:39 -0400694 // Set align to left for drawing, as we don't want individual
695 // glyphs centered or right-aligned; the offset above takes
696 // care of all alignment.
697 SkPaint paintCopy(paint);
698 paintCopy.setTextAlign(SkPaint::kLeft_Align);
699
700 SkRect bounds = SkRect::MakeLTRB(boundsLeft + x, boundsTop + y,
701 boundsRight + x, boundsBottom + y);
702
703 SkTextBlobBuilder builder;
704 const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(paintCopy, count, &bounds);
705 // TODO: we could reduce the number of memcpy's if the this were exposed further up
706 // in the architecture.
707 memcpy(buffer.glyphs, text, count * sizeof(uint16_t));
708 memcpy(buffer.pos, positions, (count << 1) * sizeof(float));
709
710 sk_sp<SkTextBlob> textBlob(builder.make());
711 mCanvas->drawTextBlob(textBlob, 0, 0, paintCopy);
712 drawTextDecorations(x, y, totalAdvance, paintCopy);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400713}
714
Yuqian Liafc221492016-07-18 13:07:42 -0400715void SkiaCanvas::drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
716 const SkPaint& paint, const SkPath& path, size_t start, size_t end) {
717 const int N = end - start;
Derek Sollenbergere547dd02016-11-09 11:55:59 -0500718 SkAutoSTMalloc<1024, uint8_t> storage(N * (sizeof(uint16_t) + sizeof(SkRSXform)));
Yuqian Liafc221492016-07-18 13:07:42 -0400719 SkRSXform* xform = (SkRSXform*)storage.get();
720 uint16_t* glyphs = (uint16_t*)(xform + N);
721 SkPathMeasure meas(path, false);
722
723 for (size_t i = start; i < end; i++) {
724 glyphs[i - start] = layout.getGlyphId(i);
725 float x = hOffset + layout.getX(i);
726 float y = vOffset + layout.getY(i);
727
728 SkPoint pos;
729 SkVector tan;
730 if (!meas.getPosTan(x, &pos, &tan)) {
731 pos.set(x, y);
732 tan.set(1, 0);
733 }
734 xform[i - start].fSCos = tan.x();
735 xform[i - start].fSSin = tan.y();
736 xform[i - start].fTx = pos.x() - tan.y() * y;
737 xform[i - start].fTy = pos.y() + tan.x() * y;
738 }
739
740 this->asSkCanvas()->drawTextRSXform(glyphs, sizeof(uint16_t) * N, xform, nullptr, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400741}
742
Derek Sollenberger6f485562015-07-30 10:00:39 -0400743// ----------------------------------------------------------------------------
744// Canvas draw operations: Animations
745// ----------------------------------------------------------------------------
746
Derek Sollenberger6f485562015-07-30 10:00:39 -0400747void SkiaCanvas::drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
748 uirenderer::CanvasPropertyPrimitive* top, uirenderer::CanvasPropertyPrimitive* right,
749 uirenderer::CanvasPropertyPrimitive* bottom, uirenderer::CanvasPropertyPrimitive* rx,
750 uirenderer::CanvasPropertyPrimitive* ry, uirenderer::CanvasPropertyPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -0400751 sk_sp<uirenderer::skiapipeline::AnimatedRoundRect> drawable(
752 new uirenderer::skiapipeline::AnimatedRoundRect(left, top, right, bottom, rx, ry, paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400753 mCanvas->drawDrawable(drawable.get());
754}
755
756void SkiaCanvas::drawCircle(uirenderer::CanvasPropertyPrimitive* x, uirenderer::CanvasPropertyPrimitive* y,
757 uirenderer::CanvasPropertyPrimitive* radius, uirenderer::CanvasPropertyPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -0400758 sk_sp<uirenderer::skiapipeline::AnimatedCircle> drawable(new uirenderer::skiapipeline::AnimatedCircle(x, y, radius, paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400759 mCanvas->drawDrawable(drawable.get());
760}
761
762// ----------------------------------------------------------------------------
763// Canvas draw operations: View System
764// ----------------------------------------------------------------------------
765
Stan Ilievf50806a2016-10-24 10:40:39 -0400766void SkiaCanvas::drawLayer(uirenderer::DeferredLayerUpdater* layerUpdater) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400767 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw Layers");
768}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400769
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400770void SkiaCanvas::drawRenderNode(uirenderer::RenderNode* renderNode) {
771 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw RenderNodes");
772}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400773
John Reckcd1c3eb2016-04-14 10:38:54 -0700774void SkiaCanvas::callDrawGLFunction(Functor* functor,
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400775 uirenderer::GlFunctorLifecycleListener* listener) {
776 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw GL Content");
777}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400778
Derek Sollenberger8872b382014-06-23 14:13:53 -0400779} // namespace android