blob: 5e21dfc6db8aff0a6fabdb1c978c7ed101fc8c28 [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 <SkDevice.h>
28#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
48Canvas* Canvas::create_canvas(SkCanvas* skiaCanvas) {
49 return new SkiaCanvas(skiaCanvas);
50}
51
Stan Ilievf50806a2016-10-24 10:40:39 -040052SkiaCanvas::SkiaCanvas() {}
53
54SkiaCanvas::SkiaCanvas(SkCanvas* canvas)
55 : mCanvas(SkRef(canvas)) {}
56
John Reckc1b33d62015-04-22 09:04:45 -070057SkiaCanvas::SkiaCanvas(const SkBitmap& bitmap) {
58 mCanvas.reset(new SkCanvas(bitmap));
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) {
Derek Sollenberger86cbf882016-07-20 11:53:05 -040064 mCanvas.reset(SkRef(skiaCanvas));
Derek Sollenbergerc1908132016-07-15 10:28:16 -040065 mSaveStack.reset(nullptr);
66 mHighContrastText = false;
67}
68
Derek Sollenberger8872b382014-06-23 14:13:53 -040069// ----------------------------------------------------------------------------
70// Canvas state operations: Replace Bitmap
71// ----------------------------------------------------------------------------
72
73class ClipCopier : public SkCanvas::ClipVisitor {
74public:
Chih-Hung Hsiehc6baf562016-04-27 11:29:23 -070075 explicit ClipCopier(SkCanvas* dstCanvas) : m_dstCanvas(dstCanvas) {}
Derek Sollenberger8872b382014-06-23 14:13:53 -040076
77 virtual void clipRect(const SkRect& rect, SkRegion::Op op, bool antialias) {
78 m_dstCanvas->clipRect(rect, op, antialias);
79 }
80 virtual void clipRRect(const SkRRect& rrect, SkRegion::Op op, bool antialias) {
81 m_dstCanvas->clipRRect(rrect, op, antialias);
82 }
83 virtual void clipPath(const SkPath& path, SkRegion::Op op, bool antialias) {
84 m_dstCanvas->clipPath(path, op, antialias);
85 }
86
87private:
88 SkCanvas* m_dstCanvas;
89};
90
John Reckc1b33d62015-04-22 09:04:45 -070091void SkiaCanvas::setBitmap(const SkBitmap& bitmap) {
Stan Ilievf50806a2016-10-24 10:40:39 -040092 SkCanvas* newCanvas = new SkCanvas(bitmap);
Derek Sollenberger8872b382014-06-23 14:13:53 -040093
John Reckc1b33d62015-04-22 09:04:45 -070094 if (!bitmap.isNull()) {
Derek Sollenberger8872b382014-06-23 14:13:53 -040095 // Copy the canvas matrix & clip state.
96 newCanvas->setMatrix(mCanvas->getTotalMatrix());
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -040097
Stan Ilievf50806a2016-10-24 10:40:39 -040098 ClipCopier copier(newCanvas);
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -040099 mCanvas->replayClips(&copier);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400100 }
101
102 // unrefs the existing canvas
Stan Ilievf50806a2016-10-24 10:40:39 -0400103 mCanvas.reset(newCanvas);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400104
105 // clean up the old save stack
Stan Ilievf50806a2016-10-24 10:40:39 -0400106 mSaveStack.reset(nullptr);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400107}
108
109// ----------------------------------------------------------------------------
110// Canvas state operations
111// ----------------------------------------------------------------------------
112
113bool SkiaCanvas::isOpaque() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400114 return mCanvas->imageInfo().isOpaque();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400115}
116
117int SkiaCanvas::width() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400118 return mCanvas->imageInfo().width();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400119}
120
121int SkiaCanvas::height() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400122 return mCanvas->imageInfo().height();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400123}
124
125// ----------------------------------------------------------------------------
126// Canvas state operations: Save (layer)
127// ----------------------------------------------------------------------------
128
129int SkiaCanvas::getSaveCount() const {
130 return mCanvas->getSaveCount();
131}
132
Florin Malitaeecff562015-12-21 10:43:01 -0500133int SkiaCanvas::save(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400134 int count = mCanvas->save();
135 recordPartialSave(flags);
136 return count;
137}
138
Florin Malita5e271402015-11-04 14:36:02 -0500139// The SkiaCanvas::restore operation layers on the capability to preserve
140// either (or both) the matrix and/or clip state after a SkCanvas::restore
141// operation. It does this by explicitly saving off the clip & matrix state
142// when requested and playing it back after the SkCanvas::restore.
Derek Sollenberger8872b382014-06-23 14:13:53 -0400143void SkiaCanvas::restore() {
Stan Ilievf50806a2016-10-24 10:40:39 -0400144 const auto* rec = this->currentSaveRec();
145 if (!rec) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400146 // Fast path - no record for this frame.
147 mCanvas->restore();
148 return;
149 }
150
Florin Malitaeecff562015-12-21 10:43:01 -0500151 bool preserveMatrix = !(rec->saveFlags & SaveFlags::Matrix);
152 bool preserveClip = !(rec->saveFlags & SaveFlags::Clip);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400153
154 SkMatrix savedMatrix;
155 if (preserveMatrix) {
156 savedMatrix = mCanvas->getTotalMatrix();
157 }
158
Stan Ilievf50806a2016-10-24 10:40:39 -0400159 const size_t clipIndex = rec->clipIndex;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400160
161 mCanvas->restore();
Stan Ilievf50806a2016-10-24 10:40:39 -0400162 mSaveStack->pop_back();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400163
164 if (preserveMatrix) {
165 mCanvas->setMatrix(savedMatrix);
166 }
167
Stan Ilievf50806a2016-10-24 10:40:39 -0400168 if (preserveClip) {
169 this->applyPersistentClips(clipIndex);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400170 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400171}
172
173void SkiaCanvas::restoreToCount(int restoreCount) {
174 while (mCanvas->getSaveCount() > restoreCount) {
175 this->restore();
176 }
177}
178
Florin Malitaeecff562015-12-21 10:43:01 -0500179static inline SkCanvas::SaveLayerFlags layerFlags(SaveFlags::Flags flags) {
180 SkCanvas::SaveLayerFlags layerFlags = 0;
181
Yuqian Li83427ff2016-09-14 11:14:06 -0400182 // We intentionally ignore the SaveFlags::HasAlphaLayer and
183 // SkCanvas::kIsOpaque_SaveLayerFlag flags because HWUI ignores it
184 // and our Android client may use it incorrectly.
185 // In Skia, this flag is purely for performance optimization.
Florin Malitaeecff562015-12-21 10:43:01 -0500186
187 if (!(flags & SaveFlags::ClipToLayer)) {
188 layerFlags |= SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag;
189 }
190
191 return layerFlags;
192}
193
Derek Sollenberger8872b382014-06-23 14:13:53 -0400194int SkiaCanvas::saveLayer(float left, float top, float right, float bottom,
Florin Malitaeecff562015-12-21 10:43:01 -0500195 const SkPaint* paint, SaveFlags::Flags flags) {
196 const SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
197 const SkCanvas::SaveLayerRec rec(&bounds, paint, layerFlags(flags));
198
199 int count = mCanvas->saveLayer(rec);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400200 recordPartialSave(flags);
201 return count;
202}
203
204int SkiaCanvas::saveLayerAlpha(float left, float top, float right, float bottom,
Florin Malitaeecff562015-12-21 10:43:01 -0500205 int alpha, SaveFlags::Flags flags) {
Florin Malitaeecff562015-12-21 10:43:01 -0500206 if (static_cast<unsigned>(alpha) < 0xFF) {
Yuqian Lifd92ee42016-04-27 17:03:38 -0400207 SkPaint alphaPaint;
208 alphaPaint.setAlpha(alpha);
209 return this->saveLayer(left, top, right, bottom, &alphaPaint, flags);
Florin Malitaeecff562015-12-21 10:43:01 -0500210 }
Yuqian Lifd92ee42016-04-27 17:03:38 -0400211 return this->saveLayer(left, top, right, bottom, nullptr, flags);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400212}
213
Stan Ilievf50806a2016-10-24 10:40:39 -0400214class SkiaCanvas::Clip {
215public:
216 Clip(const SkRect& rect, SkRegion::Op op, const SkMatrix& m)
217 : mType(Type::Rect), mOp(op), mMatrix(m), mRRect(SkRRect::MakeRect(rect)) {}
218 Clip(const SkRRect& rrect, SkRegion::Op op, const SkMatrix& m)
219 : mType(Type::RRect), mOp(op), mMatrix(m), mRRect(rrect) {}
220 Clip(const SkPath& path, SkRegion::Op op, const SkMatrix& m)
221 : mType(Type::Path), mOp(op), mMatrix(m), mPath(&path) {}
222
223 void apply(SkCanvas* canvas) const {
224 canvas->setMatrix(mMatrix);
225 switch (mType) {
226 case Type::Rect:
227 canvas->clipRect(mRRect.rect(), mOp);
228 break;
229 case Type::RRect:
230 canvas->clipRRect(mRRect, mOp);
231 break;
232 case Type::Path:
233 canvas->clipPath(*mPath.get(), mOp);
234 break;
235 }
236 }
237
238private:
239 enum class Type {
240 Rect,
241 RRect,
242 Path,
243 };
244
245 Type mType;
246 SkRegion::Op mOp;
247 SkMatrix mMatrix;
248
249 // These are logically a union (tracked separately due to non-POD path).
250 SkTLazy<SkPath> mPath;
251 SkRRect mRRect;
252};
253
254const SkiaCanvas::SaveRec* SkiaCanvas::currentSaveRec() const {
255 const SaveRec* rec = mSaveStack
256 ? static_cast<const SaveRec*>(mSaveStack->back())
257 : nullptr;
258 int currentSaveCount = mCanvas->getSaveCount();
259 SkASSERT(!rec || currentSaveCount >= rec->saveCount);
260
261 return (rec && rec->saveCount == currentSaveCount) ? rec : nullptr;
262}
263
Derek Sollenberger8872b382014-06-23 14:13:53 -0400264// ----------------------------------------------------------------------------
265// functions to emulate legacy SaveFlags (i.e. independent matrix/clip flags)
266// ----------------------------------------------------------------------------
267
Florin Malitaeecff562015-12-21 10:43:01 -0500268void SkiaCanvas::recordPartialSave(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400269 // A partial save is a save operation which doesn't capture the full canvas state.
Florin Malitaeecff562015-12-21 10:43:01 -0500270 // (either SaveFlags::Matrix or SaveFlags::Clip is missing).
Derek Sollenberger8872b382014-06-23 14:13:53 -0400271
272 // Mask-out non canvas state bits.
Florin Malitaeecff562015-12-21 10:43:01 -0500273 flags &= SaveFlags::MatrixClip;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400274
Florin Malitaeecff562015-12-21 10:43:01 -0500275 if (flags == SaveFlags::MatrixClip) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400276 // not a partial save.
277 return;
278 }
279
Stan Ilievf50806a2016-10-24 10:40:39 -0400280 if (!mSaveStack) {
Ben Wagnerd1cbc162015-08-19 12:45:09 -0400281 mSaveStack.reset(new SkDeque(sizeof(struct SaveRec), 8));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400282 }
283
284 SaveRec* rec = static_cast<SaveRec*>(mSaveStack->push_back());
Florin Malita5e271402015-11-04 14:36:02 -0500285 rec->saveCount = mCanvas->getSaveCount();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400286 rec->saveFlags = flags;
Stan Ilievf50806a2016-10-24 10:40:39 -0400287 rec->clipIndex = mClipStack.size();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400288}
289
Stan Ilievf50806a2016-10-24 10:40:39 -0400290template <typename T>
291void SkiaCanvas::recordClip(const T& clip, SkRegion::Op op) {
292 // Only need tracking when in a partial save frame which
293 // doesn't restore the clip.
294 const SaveRec* rec = this->currentSaveRec();
295 if (rec && !(rec->saveFlags & SaveFlags::Clip)) {
296 mClipStack.emplace_back(clip, op, mCanvas->getTotalMatrix());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400297 }
298}
299
Stan Ilievf50806a2016-10-24 10:40:39 -0400300// Applies and optionally removes all clips >= index.
301void SkiaCanvas::applyPersistentClips(size_t clipStartIndex) {
302 SkASSERT(clipStartIndex <= mClipStack.size());
303 const auto begin = mClipStack.cbegin() + clipStartIndex;
304 const auto end = mClipStack.cend();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400305
Stan Ilievf50806a2016-10-24 10:40:39 -0400306 // Clip application mutates the CTM.
307 const SkMatrix saveMatrix = mCanvas->getTotalMatrix();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400308
Stan Ilievf50806a2016-10-24 10:40:39 -0400309 for (auto clip = begin; clip != end; ++clip) {
310 clip->apply(mCanvas.get());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400311 }
312
Stan Ilievf50806a2016-10-24 10:40:39 -0400313 mCanvas->setMatrix(saveMatrix);
314
315 // If the current/post-restore save rec is also persisting clips, we
316 // leave them on the stack to be reapplied part of the next restore().
317 // Otherwise we're done and just pop them.
318 const auto* rec = this->currentSaveRec();
319 if (!rec || (rec->saveFlags & SaveFlags::Clip)) {
320 mClipStack.erase(begin, end);
321 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400322}
323
324// ----------------------------------------------------------------------------
325// Canvas state operations: Matrix
326// ----------------------------------------------------------------------------
327
328void SkiaCanvas::getMatrix(SkMatrix* outMatrix) const {
329 *outMatrix = mCanvas->getTotalMatrix();
330}
331
332void SkiaCanvas::setMatrix(const SkMatrix& matrix) {
333 mCanvas->setMatrix(matrix);
334}
335
336void SkiaCanvas::concat(const SkMatrix& matrix) {
337 mCanvas->concat(matrix);
338}
339
340void SkiaCanvas::rotate(float degrees) {
341 mCanvas->rotate(degrees);
342}
343
344void SkiaCanvas::scale(float sx, float sy) {
345 mCanvas->scale(sx, sy);
346}
347
348void SkiaCanvas::skew(float sx, float sy) {
349 mCanvas->skew(sx, sy);
350}
351
352void SkiaCanvas::translate(float dx, float dy) {
353 mCanvas->translate(dx, dy);
354}
355
356// ----------------------------------------------------------------------------
357// Canvas state operations: Clips
358// ----------------------------------------------------------------------------
359
360// This function is a mirror of SkCanvas::getClipBounds except that it does
361// not outset the edge of the clip to account for anti-aliasing. There is
362// a skia bug to investigate pushing this logic into back into skia.
363// (see https://code.google.com/p/skia/issues/detail?id=1303)
364bool SkiaCanvas::getClipBounds(SkRect* outRect) const {
365 SkIRect ibounds;
366 if (!mCanvas->getClipDeviceBounds(&ibounds)) {
367 return false;
368 }
369
370 SkMatrix inverse;
371 // if we can't invert the CTM, we can't return local clip bounds
372 if (!mCanvas->getTotalMatrix().invert(&inverse)) {
373 if (outRect) {
374 outRect->setEmpty();
375 }
376 return false;
377 }
378
379 if (NULL != outRect) {
380 SkRect r = SkRect::Make(ibounds);
381 inverse.mapRect(outRect, r);
382 }
383 return true;
384}
385
386bool SkiaCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
387 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
388 return mCanvas->quickReject(bounds);
389}
390
391bool SkiaCanvas::quickRejectPath(const SkPath& path) const {
392 return mCanvas->quickReject(path);
393}
394
395bool SkiaCanvas::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
396 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Stan Ilievf50806a2016-10-24 10:40:39 -0400397 this->recordClip(rect, op);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400398 mCanvas->clipRect(rect, op);
Chris Craik5ec6a282015-06-23 15:42:12 -0700399 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400400}
401
402bool SkiaCanvas::clipPath(const SkPath* path, SkRegion::Op op) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400403 SkRRect roundRect;
404 if (path->isRRect(&roundRect)) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400405 this->recordClip(roundRect, op);
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400406 mCanvas->clipRRect(roundRect, op);
407 } else {
Stan Ilievf50806a2016-10-24 10:40:39 -0400408 this->recordClip(*path, op);
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400409 mCanvas->clipPath(*path, op);
410 }
Chris Craik5ec6a282015-06-23 15:42:12 -0700411 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400412}
413
414bool SkiaCanvas::clipRegion(const SkRegion* region, SkRegion::Op op) {
415 SkPath rgnPath;
416 if (region->getBoundaryPath(&rgnPath)) {
417 // The region is specified in device space.
418 SkMatrix savedMatrix = mCanvas->getTotalMatrix();
419 mCanvas->resetMatrix();
Stan Ilievf50806a2016-10-24 10:40:39 -0400420 this->recordClip(rgnPath, op);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400421 mCanvas->clipPath(rgnPath, op);
422 mCanvas->setMatrix(savedMatrix);
423 } else {
Stan Ilievf50806a2016-10-24 10:40:39 -0400424 const auto emptyClip = SkRect::MakeEmpty();
425 this->recordClip(emptyClip, op);
426 mCanvas->clipRect(emptyClip, op);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400427 }
Chris Craik5ec6a282015-06-23 15:42:12 -0700428 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400429}
430
431// ----------------------------------------------------------------------------
432// Canvas state operations: Filters
433// ----------------------------------------------------------------------------
434
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400435SkDrawFilter* SkiaCanvas::getDrawFilter() {
436 return mCanvas->getDrawFilter();
437}
438
Derek Sollenberger8872b382014-06-23 14:13:53 -0400439void SkiaCanvas::setDrawFilter(SkDrawFilter* drawFilter) {
440 mCanvas->setDrawFilter(drawFilter);
441}
442
443// ----------------------------------------------------------------------------
444// Canvas draw operations
445// ----------------------------------------------------------------------------
446
Mike Reed260ab722016-10-07 15:59:20 -0400447void SkiaCanvas::drawColor(int color, SkBlendMode mode) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400448 mCanvas->drawColor(color, mode);
449}
450
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400451void SkiaCanvas::drawPaint(const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400452 mCanvas->drawPaint(paint);
453}
454
455// ----------------------------------------------------------------------------
456// Canvas draw operations: Geometry
457// ----------------------------------------------------------------------------
458
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400459void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400460 SkCanvas::PointMode mode) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400461 if (CC_UNLIKELY(count < 2 || PaintUtils::paintWillNotDraw(paint))) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400462 // convert the floats into SkPoints
463 count >>= 1; // now it is the number of points
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400464 std::unique_ptr<SkPoint[]> pts(new SkPoint[count]);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400465 for (int i = 0; i < count; i++) {
466 pts[i].set(points[0], points[1]);
467 points += 2;
468 }
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400469 mCanvas->drawPoints(mode, count, pts.get(), paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400470}
471
472
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400473void SkiaCanvas::drawPoint(float x, float y, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400474 mCanvas->drawPoint(x, y, paint);
475}
476
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400477void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400478 this->drawPoints(points, count, paint, SkCanvas::kPoints_PointMode);
479}
480
481void SkiaCanvas::drawLine(float startX, float startY, float stopX, float stopY,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400482 const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400483 mCanvas->drawLine(startX, startY, stopX, stopY, paint);
484}
485
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400486void SkiaCanvas::drawLines(const float* points, int count, const SkPaint& paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400487 if (CC_UNLIKELY(count < 4 || PaintUtils::paintWillNotDraw(paint))) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400488 this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode);
489}
490
491void SkiaCanvas::drawRect(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400492 const SkPaint& paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400493 if (CC_UNLIKELY(PaintUtils::paintWillNotDraw(paint))) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400494 mCanvas->drawRectCoords(left, top, right, bottom, paint);
495
496}
497
Derek Sollenberger94394b32015-07-10 09:58:41 -0400498void SkiaCanvas::drawRegion(const SkRegion& region, const SkPaint& paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400499 if (CC_UNLIKELY(PaintUtils::paintWillNotDraw(paint))) return;
500 mCanvas->drawRegion(region, paint);
Derek Sollenberger94394b32015-07-10 09:58:41 -0400501}
502
Derek Sollenberger8872b382014-06-23 14:13:53 -0400503void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400504 float rx, float ry, const SkPaint& paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400505 if (CC_UNLIKELY(PaintUtils::paintWillNotDraw(paint))) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400506 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
507 mCanvas->drawRoundRect(rect, rx, ry, paint);
508}
509
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400510void SkiaCanvas::drawCircle(float x, float y, float radius, const SkPaint& paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400511 if (CC_UNLIKELY(radius <= 0 || PaintUtils::paintWillNotDraw(paint))) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400512 mCanvas->drawCircle(x, y, radius, paint);
513}
514
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400515void SkiaCanvas::drawOval(float left, float top, float right, float bottom, const SkPaint& paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400516 if (CC_UNLIKELY(PaintUtils::paintWillNotDraw(paint))) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400517 SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
518 mCanvas->drawOval(oval, paint);
519}
520
521void SkiaCanvas::drawArc(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400522 float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400523 if (CC_UNLIKELY(PaintUtils::paintWillNotDraw(paint))) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400524 SkRect arc = SkRect::MakeLTRB(left, top, right, bottom);
525 mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, paint);
526}
527
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400528void SkiaCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400529 if (CC_UNLIKELY(PaintUtils::paintWillNotDraw(paint))) return;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400530 SkRect rect;
531 SkRRect roundRect;
532 if (path.isOval(&rect)) {
533 mCanvas->drawOval(rect, paint);
534 } else if (path.isRRect(&roundRect)) {
535 mCanvas->drawRRect(roundRect, paint);
536 } else {
537 mCanvas->drawPath(path, paint);
538 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400539}
540
541void SkiaCanvas::drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
542 const float* verts, const float* texs, const int* colors,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400543 const uint16_t* indices, int indexCount, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400544#ifndef SK_SCALAR_IS_FLOAT
545 SkDEBUGFAIL("SkScalar must be a float for these conversions to be valid");
546#endif
547 const int ptCount = vertexCount >> 1;
548 mCanvas->drawVertices(vertexMode, ptCount, (SkPoint*)verts, (SkPoint*)texs,
Mike Reedc2f31df2016-10-28 17:21:45 -0400549 (SkColor*)colors, indices, indexCount, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400550}
551
552// ----------------------------------------------------------------------------
553// Canvas draw operations: Bitmaps
554// ----------------------------------------------------------------------------
555
sergeyvaed7f582016-10-14 16:30:21 -0700556void SkiaCanvas::drawBitmap(Bitmap& bitmap, float left, float top, const SkPaint* paint) {
557 SkBitmap skBitmap;
558 bitmap.getSkBitmap(&skBitmap);
559 mCanvas->drawBitmap(skBitmap, left, top, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400560}
561
sergeyvfc9999502016-10-17 13:07:38 -0700562void SkiaCanvas::drawBitmap(Bitmap& hwuiBitmap, const SkMatrix& matrix, const SkPaint* paint) {
563 SkBitmap bitmap;
564 hwuiBitmap.getSkBitmap(&bitmap);
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400565 SkAutoCanvasRestore acr(mCanvas.get(), true);
Mike Reed70ffbf92014-12-08 17:03:30 -0500566 mCanvas->concat(matrix);
567 mCanvas->drawBitmap(bitmap, 0, 0, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400568}
569
sergeyvfc9999502016-10-17 13:07:38 -0700570void SkiaCanvas::drawBitmap(Bitmap& hwuiBitmap, float srcLeft, float srcTop,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400571 float srcRight, float srcBottom, float dstLeft, float dstTop,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400572 float dstRight, float dstBottom, const SkPaint* paint) {
sergeyvfc9999502016-10-17 13:07:38 -0700573 SkBitmap bitmap;
574 hwuiBitmap.getSkBitmap(&bitmap);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400575 SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
576 SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400577 mCanvas->drawBitmapRect(bitmap, srcRect, dstRect, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400578}
579
sergeyv5fd2a1c2016-10-20 15:04:28 -0700580void SkiaCanvas::drawBitmapMesh(Bitmap& hwuiBitmap, int meshWidth, int meshHeight,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400581 const float* vertices, const int* colors, const SkPaint* paint) {
sergeyv5fd2a1c2016-10-20 15:04:28 -0700582 SkBitmap bitmap;
583 hwuiBitmap.getSkBitmap(&bitmap);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400584 const int ptCount = (meshWidth + 1) * (meshHeight + 1);
585 const int indexCount = meshWidth * meshHeight * 6;
586
587 /* Our temp storage holds 2 or 3 arrays.
588 texture points [ptCount * sizeof(SkPoint)]
589 optionally vertex points [ptCount * sizeof(SkPoint)] if we need a
590 copy to convert from float to fixed
591 indices [ptCount * sizeof(uint16_t)]
592 */
593 ssize_t storageSize = ptCount * sizeof(SkPoint); // texs[]
594 storageSize += indexCount * sizeof(uint16_t); // indices[]
595
596
597#ifndef SK_SCALAR_IS_FLOAT
598 SkDEBUGFAIL("SkScalar must be a float for these conversions to be valid");
599#endif
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400600 std::unique_ptr<char[]> storage(new char[storageSize]);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400601 SkPoint* texs = (SkPoint*)storage.get();
602 uint16_t* indices = (uint16_t*)(texs + ptCount);
603
604 // cons up texture coordinates and indices
605 {
606 const SkScalar w = SkIntToScalar(bitmap.width());
607 const SkScalar h = SkIntToScalar(bitmap.height());
608 const SkScalar dx = w / meshWidth;
609 const SkScalar dy = h / meshHeight;
610
611 SkPoint* texsPtr = texs;
612 SkScalar y = 0;
613 for (int i = 0; i <= meshHeight; i++) {
614 if (i == meshHeight) {
615 y = h; // to ensure numerically we hit h exactly
616 }
617 SkScalar x = 0;
618 for (int j = 0; j < meshWidth; j++) {
619 texsPtr->set(x, y);
620 texsPtr += 1;
621 x += dx;
622 }
623 texsPtr->set(w, y);
624 texsPtr += 1;
625 y += dy;
626 }
627 SkASSERT(texsPtr - texs == ptCount);
628 }
629
630 // cons up indices
631 {
632 uint16_t* indexPtr = indices;
633 int index = 0;
634 for (int i = 0; i < meshHeight; i++) {
635 for (int j = 0; j < meshWidth; j++) {
636 // lower-left triangle
637 *indexPtr++ = index;
638 *indexPtr++ = index + meshWidth + 1;
639 *indexPtr++ = index + meshWidth + 2;
640 // upper-right triangle
641 *indexPtr++ = index;
642 *indexPtr++ = index + meshWidth + 2;
643 *indexPtr++ = index + 1;
644 // bump to the next cell
645 index += 1;
646 }
647 // bump to the next row
648 index += 1;
649 }
650 SkASSERT(indexPtr - indices == indexCount);
651 SkASSERT((char*)indexPtr - (char*)storage.get() == storageSize);
652 }
653
654 // double-check that we have legal indices
655#ifdef SK_DEBUG
656 {
657 for (int i = 0; i < indexCount; i++) {
658 SkASSERT((unsigned)indices[i] < (unsigned)ptCount);
659 }
660 }
661#endif
662
663 // cons-up a shader for the bitmap
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400664 SkPaint tmpPaint;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400665 if (paint) {
666 tmpPaint = *paint;
667 }
Stan Ilievf50806a2016-10-24 10:40:39 -0400668
669 sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(bitmap, kNever_SkCopyPixelsMode);
670 tmpPaint.setShader(image->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400671
672 mCanvas->drawVertices(SkCanvas::kTriangles_VertexMode, ptCount, (SkPoint*)vertices,
Mike Reedc2f31df2016-10-28 17:21:45 -0400673 texs, (const SkColor*)colors, indices,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400674 indexCount, tmpPaint);
675}
676
sergeyv5fd2a1c2016-10-20 15:04:28 -0700677void SkiaCanvas::drawNinePatch(Bitmap& hwuiBitmap, const Res_png_9patch& chunk,
Derek Sollenbergeredca3202015-07-10 13:56:39 -0400678 float dstLeft, float dstTop, float dstRight, float dstBottom, const SkPaint* paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400679
sergeyv5fd2a1c2016-10-20 15:04:28 -0700680 SkBitmap bitmap;
681 hwuiBitmap.getSkBitmap(&bitmap);
Stan Ilievf50806a2016-10-24 10:40:39 -0400682
683 SkCanvas::Lattice lattice;
Matt Sarett7de73852016-10-25 18:36:39 -0400684 NinePatchUtils::SetLatticeDivs(&lattice, chunk, bitmap.width(), bitmap.height());
Stan Ilievf50806a2016-10-24 10:40:39 -0400685
686 lattice.fFlags = nullptr;
687 int numFlags = 0;
Stan Iliev021693b2016-10-17 16:26:15 -0400688 if (chunk.numColors > 0 && chunk.numColors == NinePatchUtils::NumDistinctRects(lattice)) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400689 // We can expect the framework to give us a color for every distinct rect.
690 // Skia requires a flag for every rect.
691 numFlags = (lattice.fXCount + 1) * (lattice.fYCount + 1);
692 }
693
694 SkAutoSTMalloc<25, SkCanvas::Lattice::Flags> flags(numFlags);
695 if (numFlags > 0) {
Stan Iliev021693b2016-10-17 16:26:15 -0400696 NinePatchUtils::SetLatticeFlags(&lattice, flags.get(), numFlags, chunk);
Stan Ilievf50806a2016-10-24 10:40:39 -0400697 }
698
699 lattice.fBounds = nullptr;
700 SkRect dst = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
701 mCanvas->drawBitmapLattice(bitmap, lattice, dst, paint);
Derek Sollenbergeredca3202015-07-10 13:56:39 -0400702}
703
Doris Liu766431a2016-02-04 22:17:11 +0000704void SkiaCanvas::drawVectorDrawable(VectorDrawableRoot* vectorDrawable) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800705 vectorDrawable->drawStaging(this);
Doris Liu766431a2016-02-04 22:17:11 +0000706}
707
Derek Sollenberger8872b382014-06-23 14:13:53 -0400708// ----------------------------------------------------------------------------
709// Canvas draw operations: Text
710// ----------------------------------------------------------------------------
711
sergeyvdccca442016-03-21 15:38:21 -0700712void SkiaCanvas::drawGlyphs(const uint16_t* text, const float* positions, int count,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400713 const SkPaint& paint, float x, float y,
Tom Hudson8dfaa492014-12-09 15:03:44 -0500714 float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
715 float totalAdvance) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400716 if (!text || !positions || count <= 0 || PaintUtils::paintWillNotDrawText(paint)) return;
717 // Set align to left for drawing, as we don't want individual
718 // glyphs centered or right-aligned; the offset above takes
719 // care of all alignment.
720 SkPaint paintCopy(paint);
721 paintCopy.setTextAlign(SkPaint::kLeft_Align);
722
723 SkRect bounds = SkRect::MakeLTRB(boundsLeft + x, boundsTop + y,
724 boundsRight + x, boundsBottom + y);
725
726 SkTextBlobBuilder builder;
727 const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(paintCopy, count, &bounds);
728 // TODO: we could reduce the number of memcpy's if the this were exposed further up
729 // in the architecture.
730 memcpy(buffer.glyphs, text, count * sizeof(uint16_t));
731 memcpy(buffer.pos, positions, (count << 1) * sizeof(float));
732
733 sk_sp<SkTextBlob> textBlob(builder.make());
734 mCanvas->drawTextBlob(textBlob, 0, 0, paintCopy);
735 drawTextDecorations(x, y, totalAdvance, paintCopy);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400736}
737
Yuqian Liafc221492016-07-18 13:07:42 -0400738void SkiaCanvas::drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
739 const SkPaint& paint, const SkPath& path, size_t start, size_t end) {
740 const int N = end - start;
Derek Sollenbergere547dd02016-11-09 11:55:59 -0500741 SkAutoSTMalloc<1024, uint8_t> storage(N * (sizeof(uint16_t) + sizeof(SkRSXform)));
Yuqian Liafc221492016-07-18 13:07:42 -0400742 SkRSXform* xform = (SkRSXform*)storage.get();
743 uint16_t* glyphs = (uint16_t*)(xform + N);
744 SkPathMeasure meas(path, false);
745
746 for (size_t i = start; i < end; i++) {
747 glyphs[i - start] = layout.getGlyphId(i);
748 float x = hOffset + layout.getX(i);
749 float y = vOffset + layout.getY(i);
750
751 SkPoint pos;
752 SkVector tan;
753 if (!meas.getPosTan(x, &pos, &tan)) {
754 pos.set(x, y);
755 tan.set(1, 0);
756 }
757 xform[i - start].fSCos = tan.x();
758 xform[i - start].fSSin = tan.y();
759 xform[i - start].fTx = pos.x() - tan.y() * y;
760 xform[i - start].fTy = pos.y() + tan.x() * y;
761 }
762
763 this->asSkCanvas()->drawTextRSXform(glyphs, sizeof(uint16_t) * N, xform, nullptr, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400764}
765
Derek Sollenberger6f485562015-07-30 10:00:39 -0400766// ----------------------------------------------------------------------------
767// Canvas draw operations: Animations
768// ----------------------------------------------------------------------------
769
Derek Sollenberger6f485562015-07-30 10:00:39 -0400770void SkiaCanvas::drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
771 uirenderer::CanvasPropertyPrimitive* top, uirenderer::CanvasPropertyPrimitive* right,
772 uirenderer::CanvasPropertyPrimitive* bottom, uirenderer::CanvasPropertyPrimitive* rx,
773 uirenderer::CanvasPropertyPrimitive* ry, uirenderer::CanvasPropertyPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -0400774 sk_sp<uirenderer::skiapipeline::AnimatedRoundRect> drawable(
775 new uirenderer::skiapipeline::AnimatedRoundRect(left, top, right, bottom, rx, ry, paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400776 mCanvas->drawDrawable(drawable.get());
777}
778
779void SkiaCanvas::drawCircle(uirenderer::CanvasPropertyPrimitive* x, uirenderer::CanvasPropertyPrimitive* y,
780 uirenderer::CanvasPropertyPrimitive* radius, uirenderer::CanvasPropertyPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -0400781 sk_sp<uirenderer::skiapipeline::AnimatedCircle> drawable(new uirenderer::skiapipeline::AnimatedCircle(x, y, radius, paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400782 mCanvas->drawDrawable(drawable.get());
783}
784
785// ----------------------------------------------------------------------------
786// Canvas draw operations: View System
787// ----------------------------------------------------------------------------
788
Stan Ilievf50806a2016-10-24 10:40:39 -0400789void SkiaCanvas::drawLayer(uirenderer::DeferredLayerUpdater* layerUpdater) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400790 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw Layers");
791}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400792
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400793void SkiaCanvas::drawRenderNode(uirenderer::RenderNode* renderNode) {
794 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw RenderNodes");
795}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400796
John Reckcd1c3eb2016-04-14 10:38:54 -0700797void SkiaCanvas::callDrawGLFunction(Functor* functor,
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400798 uirenderer::GlFunctorLifecycleListener* listener) {
799 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw GL Content");
800}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400801
Derek Sollenberger8872b382014-06-23 14:13:53 -0400802} // namespace android