blob: 13d7e09b6e79bb8983161ce26b668226d649d091 [file] [log] [blame]
Derek Sollenberger8872b382014-06-23 14:13:53 -04001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Derek Sollenbergerc1908132016-07-15 10:28:16 -040017#include "SkiaCanvas.h"
Derek Sollenberger8872b382014-06-23 14:13:53 -040018
Derek Sollenbergerc1908132016-07-15 10:28:16 -040019#include "CanvasProperty.h"
Matt Sarett7de73852016-10-25 18:36:39 -040020#include "NinePatchUtils.h"
Derek Sollenbergerc1908132016-07-15 10:28:16 -040021#include "VectorDrawable.h"
sergeyvaed7f582016-10-14 16:30:21 -070022#include "hwui/Bitmap.h"
Yuqian Liafc221492016-07-18 13:07:42 -040023#include "hwui/MinikinUtils.h"
Stan Iliev021693b2016-10-17 16:26:15 -040024#include "pipeline/skia/AnimatedDrawables.h"
Derek Sollenbergerc1908132016-07-15 10:28:16 -040025
Matt Sarettea70d222017-03-29 16:25:10 -040026#include <SkColorSpaceXformCanvas.h>
Derek Sollenberger6f485562015-07-30 10:00:39 -040027#include <SkDrawable.h>
John Reck849911a2015-01-20 07:51:14 -080028#include <SkDeque.h>
29#include <SkDrawFilter.h>
30#include <SkGraphics.h>
Derek Sollenberger6f485562015-07-30 10:00:39 -040031#include <SkImage.h>
Matt Sarett62feb3a2016-09-20 10:34:11 -040032#include <SkImagePriv.h>
Yuqian Liafc221492016-07-18 13:07:42 -040033#include <SkRSXform.h>
John Reck849911a2015-01-20 07:51:14 -080034#include <SkShader.h>
John Reck849911a2015-01-20 07:51:14 -080035#include <SkTemplates.h>
Stan Ilievf50806a2016-10-24 10:40:39 -040036#include <SkTextBlob.h>
Derek Sollenberger8872b382014-06-23 14:13:53 -040037
Ben Wagner60126ef2015-08-07 12:13:48 -040038#include <memory>
39
Derek Sollenberger8872b382014-06-23 14:13:53 -040040namespace android {
41
Stan Ilievf50806a2016-10-24 10:40:39 -040042using uirenderer::PaintUtils;
43
John Reckc1b33d62015-04-22 09:04:45 -070044Canvas* Canvas::create_canvas(const SkBitmap& bitmap) {
Derek Sollenberger8872b382014-06-23 14:13:53 -040045 return new SkiaCanvas(bitmap);
46}
47
Matt Sarettea70d222017-03-29 16:25:10 -040048Canvas* Canvas::create_canvas(SkCanvas* skiaCanvas, XformToSRGB xformToSRGB) {
49 return new SkiaCanvas(skiaCanvas, xformToSRGB);
Derek Sollenberger8872b382014-06-23 14:13:53 -040050}
51
Stan Ilievf50806a2016-10-24 10:40:39 -040052SkiaCanvas::SkiaCanvas() {}
53
Matt Sarettea70d222017-03-29 16:25:10 -040054SkiaCanvas::SkiaCanvas(SkCanvas* canvas, XformToSRGB xformToSRGB)
55 : mCanvas(canvas)
56{
57 LOG_ALWAYS_FATAL_IF(XformToSRGB::kImmediate == xformToSRGB);
58}
Stan Ilievf50806a2016-10-24 10:40:39 -040059
John Reckc1b33d62015-04-22 09:04:45 -070060SkiaCanvas::SkiaCanvas(const SkBitmap& bitmap) {
Mike Reed6acfe162016-11-18 17:21:09 -050061 mCanvasOwned = std::unique_ptr<SkCanvas>(new SkCanvas(bitmap));
Matt Sarettea70d222017-03-29 16:25:10 -040062 mCanvasWrapper = SkCreateColorSpaceXformCanvas(mCanvasOwned.get(), SkColorSpace::MakeSRGB());
63 mCanvas = mCanvasWrapper.get();
Derek Sollenberger8872b382014-06-23 14:13:53 -040064}
65
Stan Iliev021693b2016-10-17 16:26:15 -040066SkiaCanvas::~SkiaCanvas() {}
67
Derek Sollenbergerc1908132016-07-15 10:28:16 -040068void SkiaCanvas::reset(SkCanvas* skiaCanvas) {
Mike Reed6acfe162016-11-18 17:21:09 -050069 if (mCanvas != skiaCanvas) {
70 mCanvas = skiaCanvas;
71 mCanvasOwned.reset();
72 }
Derek Sollenbergerc1908132016-07-15 10:28:16 -040073 mSaveStack.reset(nullptr);
74 mHighContrastText = false;
75}
76
Derek Sollenberger8872b382014-06-23 14:13:53 -040077// ----------------------------------------------------------------------------
78// Canvas state operations: Replace Bitmap
79// ----------------------------------------------------------------------------
80
Tony Mantler4f641d12017-03-14 22:36:14 +000081class ClipCopier : public SkCanvas::ClipVisitor {
82public:
83 explicit ClipCopier(SkCanvas* dstCanvas) : m_dstCanvas(dstCanvas) {}
84
85 virtual void clipRect(const SkRect& rect, SkClipOp op, bool antialias) {
86 m_dstCanvas->clipRect(rect, op, antialias);
87 }
88 virtual void clipRRect(const SkRRect& rrect, SkClipOp op, bool antialias) {
89 m_dstCanvas->clipRRect(rrect, op, antialias);
90 }
91 virtual void clipPath(const SkPath& path, SkClipOp op, bool antialias) {
92 m_dstCanvas->clipPath(path, op, antialias);
93 }
94
95private:
96 SkCanvas* m_dstCanvas;
97};
98
John Reckc1b33d62015-04-22 09:04:45 -070099void SkiaCanvas::setBitmap(const SkBitmap& bitmap) {
Matt Sarettea70d222017-03-29 16:25:10 -0400100 std::unique_ptr<SkCanvas> newCanvas = std::unique_ptr<SkCanvas>(new SkCanvas(bitmap));
101 std::unique_ptr<SkCanvas> newCanvasWrapper =
102 SkCreateColorSpaceXformCanvas(newCanvas.get(), SkColorSpace::MakeSRGB());
Tony Mantler4f641d12017-03-14 22:36:14 +0000103
104 if (!bitmap.isNull()) {
105 // Copy the canvas matrix & clip state.
Matt Sarettea70d222017-03-29 16:25:10 -0400106 newCanvasWrapper->setMatrix(mCanvas->getTotalMatrix());
Tony Mantler4f641d12017-03-14 22:36:14 +0000107
Matt Sarettea70d222017-03-29 16:25:10 -0400108 ClipCopier copier(newCanvasWrapper.get());
Tony Mantler4f641d12017-03-14 22:36:14 +0000109 mCanvas->replayClips(&copier);
110 }
111
112 // deletes the previously owned canvas (if any)
Matt Sarettea70d222017-03-29 16:25:10 -0400113 mCanvasOwned = std::move(newCanvas);
114 mCanvasWrapper = std::move(newCanvasWrapper);
115 mCanvas = mCanvasWrapper.get();
Tony Mantler4f641d12017-03-14 22:36:14 +0000116
Derek Sollenberger8872b382014-06-23 14:13:53 -0400117 // clean up the old save stack
Stan Ilievf50806a2016-10-24 10:40:39 -0400118 mSaveStack.reset(nullptr);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400119}
120
121// ----------------------------------------------------------------------------
122// Canvas state operations
123// ----------------------------------------------------------------------------
124
125bool SkiaCanvas::isOpaque() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400126 return mCanvas->imageInfo().isOpaque();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400127}
128
129int SkiaCanvas::width() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400130 return mCanvas->imageInfo().width();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400131}
132
133int SkiaCanvas::height() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400134 return mCanvas->imageInfo().height();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400135}
136
137// ----------------------------------------------------------------------------
138// Canvas state operations: Save (layer)
139// ----------------------------------------------------------------------------
140
141int SkiaCanvas::getSaveCount() const {
142 return mCanvas->getSaveCount();
143}
144
Florin Malitaeecff562015-12-21 10:43:01 -0500145int SkiaCanvas::save(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400146 int count = mCanvas->save();
147 recordPartialSave(flags);
148 return count;
149}
150
Florin Malita5e271402015-11-04 14:36:02 -0500151// The SkiaCanvas::restore operation layers on the capability to preserve
152// either (or both) the matrix and/or clip state after a SkCanvas::restore
153// operation. It does this by explicitly saving off the clip & matrix state
154// when requested and playing it back after the SkCanvas::restore.
Derek Sollenberger8872b382014-06-23 14:13:53 -0400155void SkiaCanvas::restore() {
Stan Ilievf50806a2016-10-24 10:40:39 -0400156 const auto* rec = this->currentSaveRec();
157 if (!rec) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400158 // Fast path - no record for this frame.
159 mCanvas->restore();
160 return;
161 }
162
Florin Malitaeecff562015-12-21 10:43:01 -0500163 bool preserveMatrix = !(rec->saveFlags & SaveFlags::Matrix);
164 bool preserveClip = !(rec->saveFlags & SaveFlags::Clip);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400165
166 SkMatrix savedMatrix;
167 if (preserveMatrix) {
168 savedMatrix = mCanvas->getTotalMatrix();
169 }
170
Stan Ilievf50806a2016-10-24 10:40:39 -0400171 const size_t clipIndex = rec->clipIndex;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400172
173 mCanvas->restore();
Stan Ilievf50806a2016-10-24 10:40:39 -0400174 mSaveStack->pop_back();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400175
176 if (preserveMatrix) {
177 mCanvas->setMatrix(savedMatrix);
178 }
179
Stan Ilievf50806a2016-10-24 10:40:39 -0400180 if (preserveClip) {
181 this->applyPersistentClips(clipIndex);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400182 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400183}
184
185void SkiaCanvas::restoreToCount(int restoreCount) {
186 while (mCanvas->getSaveCount() > restoreCount) {
187 this->restore();
188 }
189}
190
Florin Malitaeecff562015-12-21 10:43:01 -0500191static inline SkCanvas::SaveLayerFlags layerFlags(SaveFlags::Flags flags) {
192 SkCanvas::SaveLayerFlags layerFlags = 0;
193
Yuqian Li83427ff2016-09-14 11:14:06 -0400194 // We intentionally ignore the SaveFlags::HasAlphaLayer and
195 // SkCanvas::kIsOpaque_SaveLayerFlag flags because HWUI ignores it
196 // and our Android client may use it incorrectly.
197 // In Skia, this flag is purely for performance optimization.
Florin Malitaeecff562015-12-21 10:43:01 -0500198
199 if (!(flags & SaveFlags::ClipToLayer)) {
200 layerFlags |= SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag;
201 }
202
203 return layerFlags;
204}
205
Derek Sollenberger8872b382014-06-23 14:13:53 -0400206int SkiaCanvas::saveLayer(float left, float top, float right, float bottom,
Florin Malitaeecff562015-12-21 10:43:01 -0500207 const SkPaint* paint, SaveFlags::Flags flags) {
208 const SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
Derek Sollenbergerb8201192017-01-09 16:11:59 -0500209 const SkCanvas::SaveLayerRec rec(&bounds, paint, layerFlags(flags));
Florin Malitaeecff562015-12-21 10:43:01 -0500210
Stan Iliev68885e32016-12-14 11:18:34 -0500211 return mCanvas->saveLayer(rec);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400212}
213
214int SkiaCanvas::saveLayerAlpha(float left, float top, float right, float bottom,
Florin Malitaeecff562015-12-21 10:43:01 -0500215 int alpha, SaveFlags::Flags flags) {
Florin Malitaeecff562015-12-21 10:43:01 -0500216 if (static_cast<unsigned>(alpha) < 0xFF) {
Yuqian Lifd92ee42016-04-27 17:03:38 -0400217 SkPaint alphaPaint;
218 alphaPaint.setAlpha(alpha);
219 return this->saveLayer(left, top, right, bottom, &alphaPaint, flags);
Florin Malitaeecff562015-12-21 10:43:01 -0500220 }
Yuqian Lifd92ee42016-04-27 17:03:38 -0400221 return this->saveLayer(left, top, right, bottom, nullptr, flags);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400222}
223
Stan Ilievf50806a2016-10-24 10:40:39 -0400224class SkiaCanvas::Clip {
225public:
Mike Reed6e49c9f2016-12-02 15:36:59 -0500226 Clip(const SkRect& rect, SkClipOp op, const SkMatrix& m)
Stan Ilievf50806a2016-10-24 10:40:39 -0400227 : mType(Type::Rect), mOp(op), mMatrix(m), mRRect(SkRRect::MakeRect(rect)) {}
Mike Reed6e49c9f2016-12-02 15:36:59 -0500228 Clip(const SkRRect& rrect, SkClipOp op, const SkMatrix& m)
Stan Ilievf50806a2016-10-24 10:40:39 -0400229 : mType(Type::RRect), mOp(op), mMatrix(m), mRRect(rrect) {}
Mike Reed6e49c9f2016-12-02 15:36:59 -0500230 Clip(const SkPath& path, SkClipOp op, const SkMatrix& m)
Stan Ilievf50806a2016-10-24 10:40:39 -0400231 : mType(Type::Path), mOp(op), mMatrix(m), mPath(&path) {}
232
233 void apply(SkCanvas* canvas) const {
234 canvas->setMatrix(mMatrix);
235 switch (mType) {
236 case Type::Rect:
237 canvas->clipRect(mRRect.rect(), mOp);
238 break;
239 case Type::RRect:
240 canvas->clipRRect(mRRect, mOp);
241 break;
242 case Type::Path:
243 canvas->clipPath(*mPath.get(), mOp);
244 break;
245 }
246 }
247
248private:
249 enum class Type {
250 Rect,
251 RRect,
252 Path,
253 };
254
Mike Reed6e49c9f2016-12-02 15:36:59 -0500255 Type mType;
256 SkClipOp mOp;
257 SkMatrix mMatrix;
Stan Ilievf50806a2016-10-24 10:40:39 -0400258
259 // These are logically a union (tracked separately due to non-POD path).
260 SkTLazy<SkPath> mPath;
261 SkRRect mRRect;
262};
263
264const SkiaCanvas::SaveRec* SkiaCanvas::currentSaveRec() const {
265 const SaveRec* rec = mSaveStack
266 ? static_cast<const SaveRec*>(mSaveStack->back())
267 : nullptr;
268 int currentSaveCount = mCanvas->getSaveCount();
269 SkASSERT(!rec || currentSaveCount >= rec->saveCount);
270
271 return (rec && rec->saveCount == currentSaveCount) ? rec : nullptr;
272}
273
Derek Sollenberger8872b382014-06-23 14:13:53 -0400274// ----------------------------------------------------------------------------
275// functions to emulate legacy SaveFlags (i.e. independent matrix/clip flags)
276// ----------------------------------------------------------------------------
277
Florin Malitaeecff562015-12-21 10:43:01 -0500278void SkiaCanvas::recordPartialSave(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400279 // A partial save is a save operation which doesn't capture the full canvas state.
Florin Malitaeecff562015-12-21 10:43:01 -0500280 // (either SaveFlags::Matrix or SaveFlags::Clip is missing).
Derek Sollenberger8872b382014-06-23 14:13:53 -0400281
282 // Mask-out non canvas state bits.
Florin Malitaeecff562015-12-21 10:43:01 -0500283 flags &= SaveFlags::MatrixClip;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400284
Florin Malitaeecff562015-12-21 10:43:01 -0500285 if (flags == SaveFlags::MatrixClip) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400286 // not a partial save.
287 return;
288 }
289
Stan Ilievf50806a2016-10-24 10:40:39 -0400290 if (!mSaveStack) {
Ben Wagnerd1cbc162015-08-19 12:45:09 -0400291 mSaveStack.reset(new SkDeque(sizeof(struct SaveRec), 8));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400292 }
293
294 SaveRec* rec = static_cast<SaveRec*>(mSaveStack->push_back());
Florin Malita5e271402015-11-04 14:36:02 -0500295 rec->saveCount = mCanvas->getSaveCount();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400296 rec->saveFlags = flags;
Stan Ilievf50806a2016-10-24 10:40:39 -0400297 rec->clipIndex = mClipStack.size();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400298}
299
Stan Ilievf50806a2016-10-24 10:40:39 -0400300template <typename T>
Mike Reed6e49c9f2016-12-02 15:36:59 -0500301void SkiaCanvas::recordClip(const T& clip, SkClipOp op) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400302 // Only need tracking when in a partial save frame which
303 // doesn't restore the clip.
304 const SaveRec* rec = this->currentSaveRec();
305 if (rec && !(rec->saveFlags & SaveFlags::Clip)) {
306 mClipStack.emplace_back(clip, op, mCanvas->getTotalMatrix());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400307 }
308}
309
Stan Ilievf50806a2016-10-24 10:40:39 -0400310// Applies and optionally removes all clips >= index.
311void SkiaCanvas::applyPersistentClips(size_t clipStartIndex) {
312 SkASSERT(clipStartIndex <= mClipStack.size());
313 const auto begin = mClipStack.cbegin() + clipStartIndex;
314 const auto end = mClipStack.cend();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400315
Stan Ilievf50806a2016-10-24 10:40:39 -0400316 // Clip application mutates the CTM.
317 const SkMatrix saveMatrix = mCanvas->getTotalMatrix();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400318
Stan Ilievf50806a2016-10-24 10:40:39 -0400319 for (auto clip = begin; clip != end; ++clip) {
Mike Reed6acfe162016-11-18 17:21:09 -0500320 clip->apply(mCanvas);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400321 }
322
Stan Ilievf50806a2016-10-24 10:40:39 -0400323 mCanvas->setMatrix(saveMatrix);
324
325 // If the current/post-restore save rec is also persisting clips, we
326 // leave them on the stack to be reapplied part of the next restore().
327 // Otherwise we're done and just pop them.
328 const auto* rec = this->currentSaveRec();
329 if (!rec || (rec->saveFlags & SaveFlags::Clip)) {
330 mClipStack.erase(begin, end);
331 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400332}
333
334// ----------------------------------------------------------------------------
335// Canvas state operations: Matrix
336// ----------------------------------------------------------------------------
337
338void SkiaCanvas::getMatrix(SkMatrix* outMatrix) const {
339 *outMatrix = mCanvas->getTotalMatrix();
340}
341
342void SkiaCanvas::setMatrix(const SkMatrix& matrix) {
343 mCanvas->setMatrix(matrix);
344}
345
346void SkiaCanvas::concat(const SkMatrix& matrix) {
347 mCanvas->concat(matrix);
348}
349
350void SkiaCanvas::rotate(float degrees) {
351 mCanvas->rotate(degrees);
352}
353
354void SkiaCanvas::scale(float sx, float sy) {
355 mCanvas->scale(sx, sy);
356}
357
358void SkiaCanvas::skew(float sx, float sy) {
359 mCanvas->skew(sx, sy);
360}
361
362void SkiaCanvas::translate(float dx, float dy) {
363 mCanvas->translate(dx, dy);
364}
365
366// ----------------------------------------------------------------------------
367// Canvas state operations: Clips
368// ----------------------------------------------------------------------------
369
370// This function is a mirror of SkCanvas::getClipBounds except that it does
371// not outset the edge of the clip to account for anti-aliasing. There is
372// a skia bug to investigate pushing this logic into back into skia.
373// (see https://code.google.com/p/skia/issues/detail?id=1303)
374bool SkiaCanvas::getClipBounds(SkRect* outRect) const {
375 SkIRect ibounds;
Mike Reed5e438982017-01-25 08:23:25 -0500376 if (!mCanvas->getDeviceClipBounds(&ibounds)) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400377 return false;
378 }
379
380 SkMatrix inverse;
381 // if we can't invert the CTM, we can't return local clip bounds
382 if (!mCanvas->getTotalMatrix().invert(&inverse)) {
383 if (outRect) {
384 outRect->setEmpty();
385 }
386 return false;
387 }
388
389 if (NULL != outRect) {
390 SkRect r = SkRect::Make(ibounds);
391 inverse.mapRect(outRect, r);
392 }
393 return true;
394}
395
396bool SkiaCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
397 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
398 return mCanvas->quickReject(bounds);
399}
400
401bool SkiaCanvas::quickRejectPath(const SkPath& path) const {
402 return mCanvas->quickReject(path);
403}
404
Mike Reed6e49c9f2016-12-02 15:36:59 -0500405bool SkiaCanvas::clipRect(float left, float top, float right, float bottom, SkClipOp op) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400406 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Stan Ilievf50806a2016-10-24 10:40:39 -0400407 this->recordClip(rect, op);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400408 mCanvas->clipRect(rect, op);
Chris Craik5ec6a282015-06-23 15:42:12 -0700409 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400410}
411
Mike Reed6e49c9f2016-12-02 15:36:59 -0500412bool SkiaCanvas::clipPath(const SkPath* path, SkClipOp op) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400413 SkRRect roundRect;
414 if (path->isRRect(&roundRect)) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400415 this->recordClip(roundRect, op);
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400416 mCanvas->clipRRect(roundRect, op);
417 } else {
Stan Ilievf50806a2016-10-24 10:40:39 -0400418 this->recordClip(*path, op);
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400419 mCanvas->clipPath(*path, op);
420 }
Chris Craik5ec6a282015-06-23 15:42:12 -0700421 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400422}
423
Derek Sollenberger8872b382014-06-23 14:13:53 -0400424// ----------------------------------------------------------------------------
425// Canvas state operations: Filters
426// ----------------------------------------------------------------------------
427
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400428SkDrawFilter* SkiaCanvas::getDrawFilter() {
429 return mCanvas->getDrawFilter();
430}
431
Derek Sollenberger8872b382014-06-23 14:13:53 -0400432void SkiaCanvas::setDrawFilter(SkDrawFilter* drawFilter) {
433 mCanvas->setDrawFilter(drawFilter);
434}
435
436// ----------------------------------------------------------------------------
437// Canvas draw operations
438// ----------------------------------------------------------------------------
439
Mike Reed260ab722016-10-07 15:59:20 -0400440void SkiaCanvas::drawColor(int color, SkBlendMode mode) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400441 mCanvas->drawColor(color, mode);
442}
443
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400444void SkiaCanvas::drawPaint(const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400445 mCanvas->drawPaint(paint);
446}
447
448// ----------------------------------------------------------------------------
449// Canvas draw operations: Geometry
450// ----------------------------------------------------------------------------
451
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400452void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400453 SkCanvas::PointMode mode) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500454 if (CC_UNLIKELY(count < 2 || paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400455 // convert the floats into SkPoints
456 count >>= 1; // now it is the number of points
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400457 std::unique_ptr<SkPoint[]> pts(new SkPoint[count]);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400458 for (int i = 0; i < count; i++) {
459 pts[i].set(points[0], points[1]);
460 points += 2;
461 }
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400462 mCanvas->drawPoints(mode, count, pts.get(), paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400463}
464
465
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400466void SkiaCanvas::drawPoint(float x, float y, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400467 mCanvas->drawPoint(x, y, paint);
468}
469
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400470void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400471 this->drawPoints(points, count, paint, SkCanvas::kPoints_PointMode);
472}
473
474void SkiaCanvas::drawLine(float startX, float startY, float stopX, float stopY,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400475 const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400476 mCanvas->drawLine(startX, startY, stopX, stopY, paint);
477}
478
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400479void SkiaCanvas::drawLines(const float* points, int count, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500480 if (CC_UNLIKELY(count < 4 || paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400481 this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode);
482}
483
484void SkiaCanvas::drawRect(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400485 const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500486 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400487 mCanvas->drawRectCoords(left, top, right, bottom, paint);
488
489}
490
Derek Sollenberger94394b32015-07-10 09:58:41 -0400491void SkiaCanvas::drawRegion(const SkRegion& region, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500492 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Stan Ilievf50806a2016-10-24 10:40:39 -0400493 mCanvas->drawRegion(region, paint);
Derek Sollenberger94394b32015-07-10 09:58:41 -0400494}
495
Derek Sollenberger8872b382014-06-23 14:13:53 -0400496void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400497 float rx, float ry, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500498 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400499 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
500 mCanvas->drawRoundRect(rect, rx, ry, paint);
501}
502
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400503void SkiaCanvas::drawCircle(float x, float y, float radius, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500504 if (CC_UNLIKELY(radius <= 0 || paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400505 mCanvas->drawCircle(x, y, radius, paint);
506}
507
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400508void SkiaCanvas::drawOval(float left, float top, float right, float bottom, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500509 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400510 SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
511 mCanvas->drawOval(oval, paint);
512}
513
514void SkiaCanvas::drawArc(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400515 float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500516 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400517 SkRect arc = SkRect::MakeLTRB(left, top, right, bottom);
518 mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, paint);
519}
520
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400521void SkiaCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500522 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenbergerd7f13f82017-03-09 13:08:27 -0500523 mCanvas->drawPath(path, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400524}
525
526void SkiaCanvas::drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
527 const float* verts, const float* texs, const int* colors,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400528 const uint16_t* indices, int indexCount, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400529#ifndef SK_SCALAR_IS_FLOAT
530 SkDEBUGFAIL("SkScalar must be a float for these conversions to be valid");
531#endif
532 const int ptCount = vertexCount >> 1;
Mike Reed871cd2d2017-03-17 10:15:52 -0400533 mCanvas->drawVertices(SkVertices::MakeCopy(vertexMode, ptCount, (SkPoint*)verts,
534 (SkPoint*)texs, (SkColor*)colors,
535 indexCount, indices),
536 SkBlendMode::kModulate, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400537}
538
539// ----------------------------------------------------------------------------
540// Canvas draw operations: Bitmaps
541// ----------------------------------------------------------------------------
542
sergeyvaed7f582016-10-14 16:30:21 -0700543void SkiaCanvas::drawBitmap(Bitmap& bitmap, float left, float top, const SkPaint* paint) {
544 SkBitmap skBitmap;
545 bitmap.getSkBitmap(&skBitmap);
546 mCanvas->drawBitmap(skBitmap, left, top, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400547}
548
sergeyvfc9999502016-10-17 13:07:38 -0700549void SkiaCanvas::drawBitmap(Bitmap& hwuiBitmap, const SkMatrix& matrix, const SkPaint* paint) {
550 SkBitmap bitmap;
551 hwuiBitmap.getSkBitmap(&bitmap);
Mike Reed6acfe162016-11-18 17:21:09 -0500552 SkAutoCanvasRestore acr(mCanvas, true);
Mike Reed70ffbf92014-12-08 17:03:30 -0500553 mCanvas->concat(matrix);
554 mCanvas->drawBitmap(bitmap, 0, 0, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400555}
556
sergeyvfc9999502016-10-17 13:07:38 -0700557void SkiaCanvas::drawBitmap(Bitmap& hwuiBitmap, float srcLeft, float srcTop,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400558 float srcRight, float srcBottom, float dstLeft, float dstTop,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400559 float dstRight, float dstBottom, const SkPaint* paint) {
sergeyvfc9999502016-10-17 13:07:38 -0700560 SkBitmap bitmap;
561 hwuiBitmap.getSkBitmap(&bitmap);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400562 SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
563 SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400564 mCanvas->drawBitmapRect(bitmap, srcRect, dstRect, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400565}
566
sergeyv5fd2a1c2016-10-20 15:04:28 -0700567void SkiaCanvas::drawBitmapMesh(Bitmap& hwuiBitmap, int meshWidth, int meshHeight,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400568 const float* vertices, const int* colors, const SkPaint* paint) {
sergeyv5fd2a1c2016-10-20 15:04:28 -0700569 SkBitmap bitmap;
570 hwuiBitmap.getSkBitmap(&bitmap);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400571 const int ptCount = (meshWidth + 1) * (meshHeight + 1);
572 const int indexCount = meshWidth * meshHeight * 6;
Mike Reed871cd2d2017-03-17 10:15:52 -0400573 uint32_t flags = SkVertices::kHasTexCoords_BuilderFlag;
574 if (colors) {
575 flags |= SkVertices::kHasColors_BuilderFlag;
576 }
577 SkVertices::Builder builder(SkCanvas::kTriangles_VertexMode, ptCount, indexCount, flags);
578 memcpy(builder.positions(), vertices, ptCount * sizeof(SkPoint));
579 if (colors) {
580 memcpy(builder.colors(), colors, ptCount * sizeof(SkColor));
581 }
582 SkPoint* texs = builder.texCoords();
583 uint16_t* indices = builder.indices();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400584
585 // cons up texture coordinates and indices
586 {
587 const SkScalar w = SkIntToScalar(bitmap.width());
588 const SkScalar h = SkIntToScalar(bitmap.height());
589 const SkScalar dx = w / meshWidth;
590 const SkScalar dy = h / meshHeight;
591
592 SkPoint* texsPtr = texs;
593 SkScalar y = 0;
594 for (int i = 0; i <= meshHeight; i++) {
595 if (i == meshHeight) {
596 y = h; // to ensure numerically we hit h exactly
597 }
598 SkScalar x = 0;
599 for (int j = 0; j < meshWidth; j++) {
600 texsPtr->set(x, y);
601 texsPtr += 1;
602 x += dx;
603 }
604 texsPtr->set(w, y);
605 texsPtr += 1;
606 y += dy;
607 }
608 SkASSERT(texsPtr - texs == ptCount);
609 }
610
611 // cons up indices
612 {
613 uint16_t* indexPtr = indices;
614 int index = 0;
615 for (int i = 0; i < meshHeight; i++) {
616 for (int j = 0; j < meshWidth; j++) {
617 // lower-left triangle
618 *indexPtr++ = index;
619 *indexPtr++ = index + meshWidth + 1;
620 *indexPtr++ = index + meshWidth + 2;
621 // upper-right triangle
622 *indexPtr++ = index;
623 *indexPtr++ = index + meshWidth + 2;
624 *indexPtr++ = index + 1;
625 // bump to the next cell
626 index += 1;
627 }
628 // bump to the next row
629 index += 1;
630 }
631 SkASSERT(indexPtr - indices == indexCount);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400632 }
633
634 // double-check that we have legal indices
635#ifdef SK_DEBUG
636 {
637 for (int i = 0; i < indexCount; i++) {
638 SkASSERT((unsigned)indices[i] < (unsigned)ptCount);
639 }
640 }
641#endif
642
643 // cons-up a shader for the bitmap
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400644 SkPaint tmpPaint;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400645 if (paint) {
646 tmpPaint = *paint;
647 }
Stan Ilievf50806a2016-10-24 10:40:39 -0400648
649 sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(bitmap, kNever_SkCopyPixelsMode);
650 tmpPaint.setShader(image->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400651
Mike Reed871cd2d2017-03-17 10:15:52 -0400652 mCanvas->drawVertices(builder.detach(), SkBlendMode::kModulate, tmpPaint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400653}
654
sergeyv5fd2a1c2016-10-20 15:04:28 -0700655void SkiaCanvas::drawNinePatch(Bitmap& hwuiBitmap, const Res_png_9patch& chunk,
Derek Sollenbergeredca3202015-07-10 13:56:39 -0400656 float dstLeft, float dstTop, float dstRight, float dstBottom, const SkPaint* paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400657
sergeyv5fd2a1c2016-10-20 15:04:28 -0700658 SkBitmap bitmap;
659 hwuiBitmap.getSkBitmap(&bitmap);
Stan Ilievf50806a2016-10-24 10:40:39 -0400660
661 SkCanvas::Lattice lattice;
Matt Sarett7de73852016-10-25 18:36:39 -0400662 NinePatchUtils::SetLatticeDivs(&lattice, chunk, bitmap.width(), bitmap.height());
Stan Ilievf50806a2016-10-24 10:40:39 -0400663
664 lattice.fFlags = nullptr;
665 int numFlags = 0;
Stan Iliev021693b2016-10-17 16:26:15 -0400666 if (chunk.numColors > 0 && chunk.numColors == NinePatchUtils::NumDistinctRects(lattice)) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400667 // We can expect the framework to give us a color for every distinct rect.
668 // Skia requires a flag for every rect.
669 numFlags = (lattice.fXCount + 1) * (lattice.fYCount + 1);
670 }
671
672 SkAutoSTMalloc<25, SkCanvas::Lattice::Flags> flags(numFlags);
673 if (numFlags > 0) {
Stan Iliev021693b2016-10-17 16:26:15 -0400674 NinePatchUtils::SetLatticeFlags(&lattice, flags.get(), numFlags, chunk);
Stan Ilievf50806a2016-10-24 10:40:39 -0400675 }
676
677 lattice.fBounds = nullptr;
678 SkRect dst = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
679 mCanvas->drawBitmapLattice(bitmap, lattice, dst, paint);
Derek Sollenbergeredca3202015-07-10 13:56:39 -0400680}
681
Doris Liu766431a2016-02-04 22:17:11 +0000682void SkiaCanvas::drawVectorDrawable(VectorDrawableRoot* vectorDrawable) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800683 vectorDrawable->drawStaging(this);
Doris Liu766431a2016-02-04 22:17:11 +0000684}
685
Derek Sollenberger8872b382014-06-23 14:13:53 -0400686// ----------------------------------------------------------------------------
687// Canvas draw operations: Text
688// ----------------------------------------------------------------------------
689
sergeyvdccca442016-03-21 15:38:21 -0700690void SkiaCanvas::drawGlyphs(const uint16_t* text, const float* positions, int count,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400691 const SkPaint& paint, float x, float y,
Tom Hudson8dfaa492014-12-09 15:03:44 -0500692 float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
693 float totalAdvance) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500694 if (!text || !positions || count <= 0 || paint.nothingToDraw()) return;
Stan Ilievf50806a2016-10-24 10:40:39 -0400695 // Set align to left for drawing, as we don't want individual
696 // glyphs centered or right-aligned; the offset above takes
697 // care of all alignment.
698 SkPaint paintCopy(paint);
699 paintCopy.setTextAlign(SkPaint::kLeft_Align);
700
701 SkRect bounds = SkRect::MakeLTRB(boundsLeft + x, boundsTop + y,
702 boundsRight + x, boundsBottom + y);
703
704 SkTextBlobBuilder builder;
705 const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(paintCopy, count, &bounds);
706 // TODO: we could reduce the number of memcpy's if the this were exposed further up
707 // in the architecture.
708 memcpy(buffer.glyphs, text, count * sizeof(uint16_t));
709 memcpy(buffer.pos, positions, (count << 1) * sizeof(float));
710
711 sk_sp<SkTextBlob> textBlob(builder.make());
712 mCanvas->drawTextBlob(textBlob, 0, 0, paintCopy);
713 drawTextDecorations(x, y, totalAdvance, paintCopy);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400714}
715
Yuqian Liafc221492016-07-18 13:07:42 -0400716void SkiaCanvas::drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
717 const SkPaint& paint, const SkPath& path, size_t start, size_t end) {
718 const int N = end - start;
Derek Sollenbergere547dd02016-11-09 11:55:59 -0500719 SkAutoSTMalloc<1024, uint8_t> storage(N * (sizeof(uint16_t) + sizeof(SkRSXform)));
Yuqian Liafc221492016-07-18 13:07:42 -0400720 SkRSXform* xform = (SkRSXform*)storage.get();
721 uint16_t* glyphs = (uint16_t*)(xform + N);
722 SkPathMeasure meas(path, false);
723
724 for (size_t i = start; i < end; i++) {
725 glyphs[i - start] = layout.getGlyphId(i);
726 float x = hOffset + layout.getX(i);
727 float y = vOffset + layout.getY(i);
728
729 SkPoint pos;
730 SkVector tan;
731 if (!meas.getPosTan(x, &pos, &tan)) {
732 pos.set(x, y);
733 tan.set(1, 0);
734 }
735 xform[i - start].fSCos = tan.x();
736 xform[i - start].fSSin = tan.y();
737 xform[i - start].fTx = pos.x() - tan.y() * y;
738 xform[i - start].fTy = pos.y() + tan.x() * y;
739 }
740
741 this->asSkCanvas()->drawTextRSXform(glyphs, sizeof(uint16_t) * N, xform, nullptr, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400742}
743
Derek Sollenberger6f485562015-07-30 10:00:39 -0400744// ----------------------------------------------------------------------------
745// Canvas draw operations: Animations
746// ----------------------------------------------------------------------------
747
Derek Sollenberger6f485562015-07-30 10:00:39 -0400748void SkiaCanvas::drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
749 uirenderer::CanvasPropertyPrimitive* top, uirenderer::CanvasPropertyPrimitive* right,
750 uirenderer::CanvasPropertyPrimitive* bottom, uirenderer::CanvasPropertyPrimitive* rx,
751 uirenderer::CanvasPropertyPrimitive* ry, uirenderer::CanvasPropertyPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -0400752 sk_sp<uirenderer::skiapipeline::AnimatedRoundRect> drawable(
753 new uirenderer::skiapipeline::AnimatedRoundRect(left, top, right, bottom, rx, ry, paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400754 mCanvas->drawDrawable(drawable.get());
755}
756
757void SkiaCanvas::drawCircle(uirenderer::CanvasPropertyPrimitive* x, uirenderer::CanvasPropertyPrimitive* y,
758 uirenderer::CanvasPropertyPrimitive* radius, uirenderer::CanvasPropertyPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -0400759 sk_sp<uirenderer::skiapipeline::AnimatedCircle> drawable(new uirenderer::skiapipeline::AnimatedCircle(x, y, radius, paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400760 mCanvas->drawDrawable(drawable.get());
761}
762
763// ----------------------------------------------------------------------------
764// Canvas draw operations: View System
765// ----------------------------------------------------------------------------
766
Stan Ilievf50806a2016-10-24 10:40:39 -0400767void SkiaCanvas::drawLayer(uirenderer::DeferredLayerUpdater* layerUpdater) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400768 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw Layers");
769}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400770
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400771void SkiaCanvas::drawRenderNode(uirenderer::RenderNode* renderNode) {
772 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw RenderNodes");
773}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400774
John Reckcd1c3eb2016-04-14 10:38:54 -0700775void SkiaCanvas::callDrawGLFunction(Functor* functor,
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400776 uirenderer::GlFunctorLifecycleListener* listener) {
777 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw GL Content");
778}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400779
Derek Sollenberger8872b382014-06-23 14:13:53 -0400780} // namespace android