blob: 7cab11a4cf81682335f91c06abdcd5cb25adc1ea [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
John Reckc1b33d62015-04-22 09:04:45 -070076void SkiaCanvas::setBitmap(const SkBitmap& bitmap) {
Stan Ilievf50806a2016-10-24 10:40:39 -040077 SkCanvas* newCanvas = new SkCanvas(bitmap);
Derek Sollenberger8872b382014-06-23 14:13:53 -040078
John Reckc1b33d62015-04-22 09:04:45 -070079 if (!bitmap.isNull()) {
Derek Sollenberger8872b382014-06-23 14:13:53 -040080 // Copy the canvas matrix & clip state.
81 newCanvas->setMatrix(mCanvas->getTotalMatrix());
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -040082
Mike Reed82d3b172017-02-24 11:03:09 -050083 SkRegion rgn;
84 mCanvas->temporary_internal_getRgnClip(&rgn);
85 newCanvas->clipRegion(rgn, SkClipOp::kIntersect);
Derek Sollenberger8872b382014-06-23 14:13:53 -040086 }
87
Mike Reed6acfe162016-11-18 17:21:09 -050088 // deletes the previously owned canvas (if any)
89 mCanvasOwned = std::unique_ptr<SkCanvas>(newCanvas);
90 mCanvas = newCanvas;
Derek Sollenberger8872b382014-06-23 14:13:53 -040091
92 // clean up the old save stack
Stan Ilievf50806a2016-10-24 10:40:39 -040093 mSaveStack.reset(nullptr);
Derek Sollenberger8872b382014-06-23 14:13:53 -040094}
95
96// ----------------------------------------------------------------------------
97// Canvas state operations
98// ----------------------------------------------------------------------------
99
100bool SkiaCanvas::isOpaque() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400101 return mCanvas->imageInfo().isOpaque();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400102}
103
104int SkiaCanvas::width() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400105 return mCanvas->imageInfo().width();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400106}
107
108int SkiaCanvas::height() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400109 return mCanvas->imageInfo().height();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400110}
111
112// ----------------------------------------------------------------------------
113// Canvas state operations: Save (layer)
114// ----------------------------------------------------------------------------
115
116int SkiaCanvas::getSaveCount() const {
117 return mCanvas->getSaveCount();
118}
119
Florin Malitaeecff562015-12-21 10:43:01 -0500120int SkiaCanvas::save(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400121 int count = mCanvas->save();
122 recordPartialSave(flags);
123 return count;
124}
125
Florin Malita5e271402015-11-04 14:36:02 -0500126// The SkiaCanvas::restore operation layers on the capability to preserve
127// either (or both) the matrix and/or clip state after a SkCanvas::restore
128// operation. It does this by explicitly saving off the clip & matrix state
129// when requested and playing it back after the SkCanvas::restore.
Derek Sollenberger8872b382014-06-23 14:13:53 -0400130void SkiaCanvas::restore() {
Stan Ilievf50806a2016-10-24 10:40:39 -0400131 const auto* rec = this->currentSaveRec();
132 if (!rec) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400133 // Fast path - no record for this frame.
134 mCanvas->restore();
135 return;
136 }
137
Florin Malitaeecff562015-12-21 10:43:01 -0500138 bool preserveMatrix = !(rec->saveFlags & SaveFlags::Matrix);
139 bool preserveClip = !(rec->saveFlags & SaveFlags::Clip);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400140
141 SkMatrix savedMatrix;
142 if (preserveMatrix) {
143 savedMatrix = mCanvas->getTotalMatrix();
144 }
145
Stan Ilievf50806a2016-10-24 10:40:39 -0400146 const size_t clipIndex = rec->clipIndex;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400147
148 mCanvas->restore();
Stan Ilievf50806a2016-10-24 10:40:39 -0400149 mSaveStack->pop_back();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400150
151 if (preserveMatrix) {
152 mCanvas->setMatrix(savedMatrix);
153 }
154
Stan Ilievf50806a2016-10-24 10:40:39 -0400155 if (preserveClip) {
156 this->applyPersistentClips(clipIndex);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400157 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400158}
159
160void SkiaCanvas::restoreToCount(int restoreCount) {
161 while (mCanvas->getSaveCount() > restoreCount) {
162 this->restore();
163 }
164}
165
Florin Malitaeecff562015-12-21 10:43:01 -0500166static inline SkCanvas::SaveLayerFlags layerFlags(SaveFlags::Flags flags) {
167 SkCanvas::SaveLayerFlags layerFlags = 0;
168
Yuqian Li83427ff2016-09-14 11:14:06 -0400169 // We intentionally ignore the SaveFlags::HasAlphaLayer and
170 // SkCanvas::kIsOpaque_SaveLayerFlag flags because HWUI ignores it
171 // and our Android client may use it incorrectly.
172 // In Skia, this flag is purely for performance optimization.
Florin Malitaeecff562015-12-21 10:43:01 -0500173
174 if (!(flags & SaveFlags::ClipToLayer)) {
175 layerFlags |= SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag;
176 }
177
178 return layerFlags;
179}
180
Derek Sollenberger8872b382014-06-23 14:13:53 -0400181int SkiaCanvas::saveLayer(float left, float top, float right, float bottom,
Florin Malitaeecff562015-12-21 10:43:01 -0500182 const SkPaint* paint, SaveFlags::Flags flags) {
183 const SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
Derek Sollenbergerb8201192017-01-09 16:11:59 -0500184 const SkCanvas::SaveLayerRec rec(&bounds, paint, layerFlags(flags));
Florin Malitaeecff562015-12-21 10:43:01 -0500185
Stan Iliev68885e32016-12-14 11:18:34 -0500186 return mCanvas->saveLayer(rec);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400187}
188
189int SkiaCanvas::saveLayerAlpha(float left, float top, float right, float bottom,
Florin Malitaeecff562015-12-21 10:43:01 -0500190 int alpha, SaveFlags::Flags flags) {
Florin Malitaeecff562015-12-21 10:43:01 -0500191 if (static_cast<unsigned>(alpha) < 0xFF) {
Yuqian Lifd92ee42016-04-27 17:03:38 -0400192 SkPaint alphaPaint;
193 alphaPaint.setAlpha(alpha);
194 return this->saveLayer(left, top, right, bottom, &alphaPaint, flags);
Florin Malitaeecff562015-12-21 10:43:01 -0500195 }
Yuqian Lifd92ee42016-04-27 17:03:38 -0400196 return this->saveLayer(left, top, right, bottom, nullptr, flags);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400197}
198
Stan Ilievf50806a2016-10-24 10:40:39 -0400199class SkiaCanvas::Clip {
200public:
Mike Reed6e49c9f2016-12-02 15:36:59 -0500201 Clip(const SkRect& rect, SkClipOp op, const SkMatrix& m)
Stan Ilievf50806a2016-10-24 10:40:39 -0400202 : mType(Type::Rect), mOp(op), mMatrix(m), mRRect(SkRRect::MakeRect(rect)) {}
Mike Reed6e49c9f2016-12-02 15:36:59 -0500203 Clip(const SkRRect& rrect, SkClipOp op, const SkMatrix& m)
Stan Ilievf50806a2016-10-24 10:40:39 -0400204 : mType(Type::RRect), mOp(op), mMatrix(m), mRRect(rrect) {}
Mike Reed6e49c9f2016-12-02 15:36:59 -0500205 Clip(const SkPath& path, SkClipOp op, const SkMatrix& m)
Stan Ilievf50806a2016-10-24 10:40:39 -0400206 : mType(Type::Path), mOp(op), mMatrix(m), mPath(&path) {}
207
208 void apply(SkCanvas* canvas) const {
209 canvas->setMatrix(mMatrix);
210 switch (mType) {
211 case Type::Rect:
212 canvas->clipRect(mRRect.rect(), mOp);
213 break;
214 case Type::RRect:
215 canvas->clipRRect(mRRect, mOp);
216 break;
217 case Type::Path:
218 canvas->clipPath(*mPath.get(), mOp);
219 break;
220 }
221 }
222
223private:
224 enum class Type {
225 Rect,
226 RRect,
227 Path,
228 };
229
Mike Reed6e49c9f2016-12-02 15:36:59 -0500230 Type mType;
231 SkClipOp mOp;
232 SkMatrix mMatrix;
Stan Ilievf50806a2016-10-24 10:40:39 -0400233
234 // These are logically a union (tracked separately due to non-POD path).
235 SkTLazy<SkPath> mPath;
236 SkRRect mRRect;
237};
238
239const SkiaCanvas::SaveRec* SkiaCanvas::currentSaveRec() const {
240 const SaveRec* rec = mSaveStack
241 ? static_cast<const SaveRec*>(mSaveStack->back())
242 : nullptr;
243 int currentSaveCount = mCanvas->getSaveCount();
244 SkASSERT(!rec || currentSaveCount >= rec->saveCount);
245
246 return (rec && rec->saveCount == currentSaveCount) ? rec : nullptr;
247}
248
Derek Sollenberger8872b382014-06-23 14:13:53 -0400249// ----------------------------------------------------------------------------
250// functions to emulate legacy SaveFlags (i.e. independent matrix/clip flags)
251// ----------------------------------------------------------------------------
252
Florin Malitaeecff562015-12-21 10:43:01 -0500253void SkiaCanvas::recordPartialSave(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400254 // A partial save is a save operation which doesn't capture the full canvas state.
Florin Malitaeecff562015-12-21 10:43:01 -0500255 // (either SaveFlags::Matrix or SaveFlags::Clip is missing).
Derek Sollenberger8872b382014-06-23 14:13:53 -0400256
257 // Mask-out non canvas state bits.
Florin Malitaeecff562015-12-21 10:43:01 -0500258 flags &= SaveFlags::MatrixClip;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400259
Florin Malitaeecff562015-12-21 10:43:01 -0500260 if (flags == SaveFlags::MatrixClip) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400261 // not a partial save.
262 return;
263 }
264
Stan Ilievf50806a2016-10-24 10:40:39 -0400265 if (!mSaveStack) {
Ben Wagnerd1cbc162015-08-19 12:45:09 -0400266 mSaveStack.reset(new SkDeque(sizeof(struct SaveRec), 8));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400267 }
268
269 SaveRec* rec = static_cast<SaveRec*>(mSaveStack->push_back());
Florin Malita5e271402015-11-04 14:36:02 -0500270 rec->saveCount = mCanvas->getSaveCount();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400271 rec->saveFlags = flags;
Stan Ilievf50806a2016-10-24 10:40:39 -0400272 rec->clipIndex = mClipStack.size();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400273}
274
Stan Ilievf50806a2016-10-24 10:40:39 -0400275template <typename T>
Mike Reed6e49c9f2016-12-02 15:36:59 -0500276void SkiaCanvas::recordClip(const T& clip, SkClipOp op) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400277 // Only need tracking when in a partial save frame which
278 // doesn't restore the clip.
279 const SaveRec* rec = this->currentSaveRec();
280 if (rec && !(rec->saveFlags & SaveFlags::Clip)) {
281 mClipStack.emplace_back(clip, op, mCanvas->getTotalMatrix());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400282 }
283}
284
Stan Ilievf50806a2016-10-24 10:40:39 -0400285// Applies and optionally removes all clips >= index.
286void SkiaCanvas::applyPersistentClips(size_t clipStartIndex) {
287 SkASSERT(clipStartIndex <= mClipStack.size());
288 const auto begin = mClipStack.cbegin() + clipStartIndex;
289 const auto end = mClipStack.cend();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400290
Stan Ilievf50806a2016-10-24 10:40:39 -0400291 // Clip application mutates the CTM.
292 const SkMatrix saveMatrix = mCanvas->getTotalMatrix();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400293
Stan Ilievf50806a2016-10-24 10:40:39 -0400294 for (auto clip = begin; clip != end; ++clip) {
Mike Reed6acfe162016-11-18 17:21:09 -0500295 clip->apply(mCanvas);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400296 }
297
Stan Ilievf50806a2016-10-24 10:40:39 -0400298 mCanvas->setMatrix(saveMatrix);
299
300 // If the current/post-restore save rec is also persisting clips, we
301 // leave them on the stack to be reapplied part of the next restore().
302 // Otherwise we're done and just pop them.
303 const auto* rec = this->currentSaveRec();
304 if (!rec || (rec->saveFlags & SaveFlags::Clip)) {
305 mClipStack.erase(begin, end);
306 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400307}
308
309// ----------------------------------------------------------------------------
310// Canvas state operations: Matrix
311// ----------------------------------------------------------------------------
312
313void SkiaCanvas::getMatrix(SkMatrix* outMatrix) const {
314 *outMatrix = mCanvas->getTotalMatrix();
315}
316
317void SkiaCanvas::setMatrix(const SkMatrix& matrix) {
318 mCanvas->setMatrix(matrix);
319}
320
321void SkiaCanvas::concat(const SkMatrix& matrix) {
322 mCanvas->concat(matrix);
323}
324
325void SkiaCanvas::rotate(float degrees) {
326 mCanvas->rotate(degrees);
327}
328
329void SkiaCanvas::scale(float sx, float sy) {
330 mCanvas->scale(sx, sy);
331}
332
333void SkiaCanvas::skew(float sx, float sy) {
334 mCanvas->skew(sx, sy);
335}
336
337void SkiaCanvas::translate(float dx, float dy) {
338 mCanvas->translate(dx, dy);
339}
340
341// ----------------------------------------------------------------------------
342// Canvas state operations: Clips
343// ----------------------------------------------------------------------------
344
345// This function is a mirror of SkCanvas::getClipBounds except that it does
346// not outset the edge of the clip to account for anti-aliasing. There is
347// a skia bug to investigate pushing this logic into back into skia.
348// (see https://code.google.com/p/skia/issues/detail?id=1303)
349bool SkiaCanvas::getClipBounds(SkRect* outRect) const {
350 SkIRect ibounds;
Mike Reed5e438982017-01-25 08:23:25 -0500351 if (!mCanvas->getDeviceClipBounds(&ibounds)) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400352 return false;
353 }
354
355 SkMatrix inverse;
356 // if we can't invert the CTM, we can't return local clip bounds
357 if (!mCanvas->getTotalMatrix().invert(&inverse)) {
358 if (outRect) {
359 outRect->setEmpty();
360 }
361 return false;
362 }
363
364 if (NULL != outRect) {
365 SkRect r = SkRect::Make(ibounds);
366 inverse.mapRect(outRect, r);
367 }
368 return true;
369}
370
371bool SkiaCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
372 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
373 return mCanvas->quickReject(bounds);
374}
375
376bool SkiaCanvas::quickRejectPath(const SkPath& path) const {
377 return mCanvas->quickReject(path);
378}
379
Mike Reed6e49c9f2016-12-02 15:36:59 -0500380bool SkiaCanvas::clipRect(float left, float top, float right, float bottom, SkClipOp op) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400381 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Stan Ilievf50806a2016-10-24 10:40:39 -0400382 this->recordClip(rect, op);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400383 mCanvas->clipRect(rect, op);
Chris Craik5ec6a282015-06-23 15:42:12 -0700384 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400385}
386
Mike Reed6e49c9f2016-12-02 15:36:59 -0500387bool SkiaCanvas::clipPath(const SkPath* path, SkClipOp op) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400388 SkRRect roundRect;
389 if (path->isRRect(&roundRect)) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400390 this->recordClip(roundRect, op);
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400391 mCanvas->clipRRect(roundRect, op);
392 } else {
Stan Ilievf50806a2016-10-24 10:40:39 -0400393 this->recordClip(*path, op);
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400394 mCanvas->clipPath(*path, op);
395 }
Chris Craik5ec6a282015-06-23 15:42:12 -0700396 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400397}
398
Derek Sollenberger8872b382014-06-23 14:13:53 -0400399// ----------------------------------------------------------------------------
400// Canvas state operations: Filters
401// ----------------------------------------------------------------------------
402
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400403SkDrawFilter* SkiaCanvas::getDrawFilter() {
404 return mCanvas->getDrawFilter();
405}
406
Derek Sollenberger8872b382014-06-23 14:13:53 -0400407void SkiaCanvas::setDrawFilter(SkDrawFilter* drawFilter) {
408 mCanvas->setDrawFilter(drawFilter);
409}
410
411// ----------------------------------------------------------------------------
412// Canvas draw operations
413// ----------------------------------------------------------------------------
414
Mike Reed260ab722016-10-07 15:59:20 -0400415void SkiaCanvas::drawColor(int color, SkBlendMode mode) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400416 mCanvas->drawColor(color, mode);
417}
418
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400419void SkiaCanvas::drawPaint(const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400420 mCanvas->drawPaint(paint);
421}
422
423// ----------------------------------------------------------------------------
424// Canvas draw operations: Geometry
425// ----------------------------------------------------------------------------
426
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400427void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400428 SkCanvas::PointMode mode) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400429 if (CC_UNLIKELY(count < 2 || PaintUtils::paintWillNotDraw(paint))) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400430 // convert the floats into SkPoints
431 count >>= 1; // now it is the number of points
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400432 std::unique_ptr<SkPoint[]> pts(new SkPoint[count]);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400433 for (int i = 0; i < count; i++) {
434 pts[i].set(points[0], points[1]);
435 points += 2;
436 }
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400437 mCanvas->drawPoints(mode, count, pts.get(), paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400438}
439
440
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400441void SkiaCanvas::drawPoint(float x, float y, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400442 mCanvas->drawPoint(x, y, paint);
443}
444
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400445void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400446 this->drawPoints(points, count, paint, SkCanvas::kPoints_PointMode);
447}
448
449void SkiaCanvas::drawLine(float startX, float startY, float stopX, float stopY,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400450 const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400451 mCanvas->drawLine(startX, startY, stopX, stopY, paint);
452}
453
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400454void SkiaCanvas::drawLines(const float* points, int count, const SkPaint& paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400455 if (CC_UNLIKELY(count < 4 || PaintUtils::paintWillNotDraw(paint))) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400456 this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode);
457}
458
459void SkiaCanvas::drawRect(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400460 const SkPaint& paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400461 if (CC_UNLIKELY(PaintUtils::paintWillNotDraw(paint))) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400462 mCanvas->drawRectCoords(left, top, right, bottom, paint);
463
464}
465
Derek Sollenberger94394b32015-07-10 09:58:41 -0400466void SkiaCanvas::drawRegion(const SkRegion& region, const SkPaint& paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400467 if (CC_UNLIKELY(PaintUtils::paintWillNotDraw(paint))) return;
468 mCanvas->drawRegion(region, paint);
Derek Sollenberger94394b32015-07-10 09:58:41 -0400469}
470
Derek Sollenberger8872b382014-06-23 14:13:53 -0400471void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400472 float rx, float ry, const SkPaint& paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400473 if (CC_UNLIKELY(PaintUtils::paintWillNotDraw(paint))) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400474 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
475 mCanvas->drawRoundRect(rect, rx, ry, paint);
476}
477
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400478void SkiaCanvas::drawCircle(float x, float y, float radius, const SkPaint& paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400479 if (CC_UNLIKELY(radius <= 0 || PaintUtils::paintWillNotDraw(paint))) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400480 mCanvas->drawCircle(x, y, radius, paint);
481}
482
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400483void SkiaCanvas::drawOval(float left, float top, float right, float bottom, const SkPaint& paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400484 if (CC_UNLIKELY(PaintUtils::paintWillNotDraw(paint))) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400485 SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
486 mCanvas->drawOval(oval, paint);
487}
488
489void SkiaCanvas::drawArc(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400490 float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400491 if (CC_UNLIKELY(PaintUtils::paintWillNotDraw(paint))) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400492 SkRect arc = SkRect::MakeLTRB(left, top, right, bottom);
493 mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, paint);
494}
495
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400496void SkiaCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400497 if (CC_UNLIKELY(PaintUtils::paintWillNotDraw(paint))) return;
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400498 SkRect rect;
499 SkRRect roundRect;
500 if (path.isOval(&rect)) {
501 mCanvas->drawOval(rect, paint);
502 } else if (path.isRRect(&roundRect)) {
503 mCanvas->drawRRect(roundRect, paint);
504 } else {
505 mCanvas->drawPath(path, paint);
506 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400507}
508
509void SkiaCanvas::drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
510 const float* verts, const float* texs, const int* colors,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400511 const uint16_t* indices, int indexCount, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400512#ifndef SK_SCALAR_IS_FLOAT
513 SkDEBUGFAIL("SkScalar must be a float for these conversions to be valid");
514#endif
515 const int ptCount = vertexCount >> 1;
516 mCanvas->drawVertices(vertexMode, ptCount, (SkPoint*)verts, (SkPoint*)texs,
Mike Reedc2f31df2016-10-28 17:21:45 -0400517 (SkColor*)colors, indices, indexCount, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400518}
519
520// ----------------------------------------------------------------------------
521// Canvas draw operations: Bitmaps
522// ----------------------------------------------------------------------------
523
sergeyvaed7f582016-10-14 16:30:21 -0700524void SkiaCanvas::drawBitmap(Bitmap& bitmap, float left, float top, const SkPaint* paint) {
525 SkBitmap skBitmap;
526 bitmap.getSkBitmap(&skBitmap);
527 mCanvas->drawBitmap(skBitmap, left, top, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400528}
529
sergeyvfc9999502016-10-17 13:07:38 -0700530void SkiaCanvas::drawBitmap(Bitmap& hwuiBitmap, const SkMatrix& matrix, const SkPaint* paint) {
531 SkBitmap bitmap;
532 hwuiBitmap.getSkBitmap(&bitmap);
Mike Reed6acfe162016-11-18 17:21:09 -0500533 SkAutoCanvasRestore acr(mCanvas, true);
Mike Reed70ffbf92014-12-08 17:03:30 -0500534 mCanvas->concat(matrix);
535 mCanvas->drawBitmap(bitmap, 0, 0, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400536}
537
sergeyvfc9999502016-10-17 13:07:38 -0700538void SkiaCanvas::drawBitmap(Bitmap& hwuiBitmap, float srcLeft, float srcTop,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400539 float srcRight, float srcBottom, float dstLeft, float dstTop,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400540 float dstRight, float dstBottom, const SkPaint* paint) {
sergeyvfc9999502016-10-17 13:07:38 -0700541 SkBitmap bitmap;
542 hwuiBitmap.getSkBitmap(&bitmap);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400543 SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
544 SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400545 mCanvas->drawBitmapRect(bitmap, srcRect, dstRect, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400546}
547
sergeyv5fd2a1c2016-10-20 15:04:28 -0700548void SkiaCanvas::drawBitmapMesh(Bitmap& hwuiBitmap, int meshWidth, int meshHeight,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400549 const float* vertices, const int* colors, const SkPaint* paint) {
sergeyv5fd2a1c2016-10-20 15:04:28 -0700550 SkBitmap bitmap;
551 hwuiBitmap.getSkBitmap(&bitmap);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400552 const int ptCount = (meshWidth + 1) * (meshHeight + 1);
553 const int indexCount = meshWidth * meshHeight * 6;
554
555 /* Our temp storage holds 2 or 3 arrays.
556 texture points [ptCount * sizeof(SkPoint)]
557 optionally vertex points [ptCount * sizeof(SkPoint)] if we need a
558 copy to convert from float to fixed
559 indices [ptCount * sizeof(uint16_t)]
560 */
561 ssize_t storageSize = ptCount * sizeof(SkPoint); // texs[]
562 storageSize += indexCount * sizeof(uint16_t); // indices[]
563
564
565#ifndef SK_SCALAR_IS_FLOAT
566 SkDEBUGFAIL("SkScalar must be a float for these conversions to be valid");
567#endif
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400568 std::unique_ptr<char[]> storage(new char[storageSize]);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400569 SkPoint* texs = (SkPoint*)storage.get();
570 uint16_t* indices = (uint16_t*)(texs + ptCount);
571
572 // cons up texture coordinates and indices
573 {
574 const SkScalar w = SkIntToScalar(bitmap.width());
575 const SkScalar h = SkIntToScalar(bitmap.height());
576 const SkScalar dx = w / meshWidth;
577 const SkScalar dy = h / meshHeight;
578
579 SkPoint* texsPtr = texs;
580 SkScalar y = 0;
581 for (int i = 0; i <= meshHeight; i++) {
582 if (i == meshHeight) {
583 y = h; // to ensure numerically we hit h exactly
584 }
585 SkScalar x = 0;
586 for (int j = 0; j < meshWidth; j++) {
587 texsPtr->set(x, y);
588 texsPtr += 1;
589 x += dx;
590 }
591 texsPtr->set(w, y);
592 texsPtr += 1;
593 y += dy;
594 }
595 SkASSERT(texsPtr - texs == ptCount);
596 }
597
598 // cons up indices
599 {
600 uint16_t* indexPtr = indices;
601 int index = 0;
602 for (int i = 0; i < meshHeight; i++) {
603 for (int j = 0; j < meshWidth; j++) {
604 // lower-left triangle
605 *indexPtr++ = index;
606 *indexPtr++ = index + meshWidth + 1;
607 *indexPtr++ = index + meshWidth + 2;
608 // upper-right triangle
609 *indexPtr++ = index;
610 *indexPtr++ = index + meshWidth + 2;
611 *indexPtr++ = index + 1;
612 // bump to the next cell
613 index += 1;
614 }
615 // bump to the next row
616 index += 1;
617 }
618 SkASSERT(indexPtr - indices == indexCount);
619 SkASSERT((char*)indexPtr - (char*)storage.get() == storageSize);
620 }
621
622 // double-check that we have legal indices
623#ifdef SK_DEBUG
624 {
625 for (int i = 0; i < indexCount; i++) {
626 SkASSERT((unsigned)indices[i] < (unsigned)ptCount);
627 }
628 }
629#endif
630
631 // cons-up a shader for the bitmap
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400632 SkPaint tmpPaint;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400633 if (paint) {
634 tmpPaint = *paint;
635 }
Stan Ilievf50806a2016-10-24 10:40:39 -0400636
637 sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(bitmap, kNever_SkCopyPixelsMode);
638 tmpPaint.setShader(image->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400639
640 mCanvas->drawVertices(SkCanvas::kTriangles_VertexMode, ptCount, (SkPoint*)vertices,
Mike Reedc2f31df2016-10-28 17:21:45 -0400641 texs, (const SkColor*)colors, indices,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400642 indexCount, tmpPaint);
643}
644
sergeyv5fd2a1c2016-10-20 15:04:28 -0700645void SkiaCanvas::drawNinePatch(Bitmap& hwuiBitmap, const Res_png_9patch& chunk,
Derek Sollenbergeredca3202015-07-10 13:56:39 -0400646 float dstLeft, float dstTop, float dstRight, float dstBottom, const SkPaint* paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400647
sergeyv5fd2a1c2016-10-20 15:04:28 -0700648 SkBitmap bitmap;
649 hwuiBitmap.getSkBitmap(&bitmap);
Stan Ilievf50806a2016-10-24 10:40:39 -0400650
651 SkCanvas::Lattice lattice;
Matt Sarett7de73852016-10-25 18:36:39 -0400652 NinePatchUtils::SetLatticeDivs(&lattice, chunk, bitmap.width(), bitmap.height());
Stan Ilievf50806a2016-10-24 10:40:39 -0400653
654 lattice.fFlags = nullptr;
655 int numFlags = 0;
Stan Iliev021693b2016-10-17 16:26:15 -0400656 if (chunk.numColors > 0 && chunk.numColors == NinePatchUtils::NumDistinctRects(lattice)) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400657 // We can expect the framework to give us a color for every distinct rect.
658 // Skia requires a flag for every rect.
659 numFlags = (lattice.fXCount + 1) * (lattice.fYCount + 1);
660 }
661
662 SkAutoSTMalloc<25, SkCanvas::Lattice::Flags> flags(numFlags);
663 if (numFlags > 0) {
Stan Iliev021693b2016-10-17 16:26:15 -0400664 NinePatchUtils::SetLatticeFlags(&lattice, flags.get(), numFlags, chunk);
Stan Ilievf50806a2016-10-24 10:40:39 -0400665 }
666
667 lattice.fBounds = nullptr;
668 SkRect dst = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
669 mCanvas->drawBitmapLattice(bitmap, lattice, dst, paint);
Derek Sollenbergeredca3202015-07-10 13:56:39 -0400670}
671
Doris Liu766431a2016-02-04 22:17:11 +0000672void SkiaCanvas::drawVectorDrawable(VectorDrawableRoot* vectorDrawable) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800673 vectorDrawable->drawStaging(this);
Doris Liu766431a2016-02-04 22:17:11 +0000674}
675
Derek Sollenberger8872b382014-06-23 14:13:53 -0400676// ----------------------------------------------------------------------------
677// Canvas draw operations: Text
678// ----------------------------------------------------------------------------
679
sergeyvdccca442016-03-21 15:38:21 -0700680void SkiaCanvas::drawGlyphs(const uint16_t* text, const float* positions, int count,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400681 const SkPaint& paint, float x, float y,
Tom Hudson8dfaa492014-12-09 15:03:44 -0500682 float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
683 float totalAdvance) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400684 if (!text || !positions || count <= 0 || PaintUtils::paintWillNotDrawText(paint)) return;
685 // Set align to left for drawing, as we don't want individual
686 // glyphs centered or right-aligned; the offset above takes
687 // care of all alignment.
688 SkPaint paintCopy(paint);
689 paintCopy.setTextAlign(SkPaint::kLeft_Align);
690
691 SkRect bounds = SkRect::MakeLTRB(boundsLeft + x, boundsTop + y,
692 boundsRight + x, boundsBottom + y);
693
694 SkTextBlobBuilder builder;
695 const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(paintCopy, count, &bounds);
696 // TODO: we could reduce the number of memcpy's if the this were exposed further up
697 // in the architecture.
698 memcpy(buffer.glyphs, text, count * sizeof(uint16_t));
699 memcpy(buffer.pos, positions, (count << 1) * sizeof(float));
700
701 sk_sp<SkTextBlob> textBlob(builder.make());
702 mCanvas->drawTextBlob(textBlob, 0, 0, paintCopy);
703 drawTextDecorations(x, y, totalAdvance, paintCopy);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400704}
705
Yuqian Liafc221492016-07-18 13:07:42 -0400706void SkiaCanvas::drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
707 const SkPaint& paint, const SkPath& path, size_t start, size_t end) {
708 const int N = end - start;
Derek Sollenbergere547dd02016-11-09 11:55:59 -0500709 SkAutoSTMalloc<1024, uint8_t> storage(N * (sizeof(uint16_t) + sizeof(SkRSXform)));
Yuqian Liafc221492016-07-18 13:07:42 -0400710 SkRSXform* xform = (SkRSXform*)storage.get();
711 uint16_t* glyphs = (uint16_t*)(xform + N);
712 SkPathMeasure meas(path, false);
713
714 for (size_t i = start; i < end; i++) {
715 glyphs[i - start] = layout.getGlyphId(i);
716 float x = hOffset + layout.getX(i);
717 float y = vOffset + layout.getY(i);
718
719 SkPoint pos;
720 SkVector tan;
721 if (!meas.getPosTan(x, &pos, &tan)) {
722 pos.set(x, y);
723 tan.set(1, 0);
724 }
725 xform[i - start].fSCos = tan.x();
726 xform[i - start].fSSin = tan.y();
727 xform[i - start].fTx = pos.x() - tan.y() * y;
728 xform[i - start].fTy = pos.y() + tan.x() * y;
729 }
730
731 this->asSkCanvas()->drawTextRSXform(glyphs, sizeof(uint16_t) * N, xform, nullptr, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400732}
733
Derek Sollenberger6f485562015-07-30 10:00:39 -0400734// ----------------------------------------------------------------------------
735// Canvas draw operations: Animations
736// ----------------------------------------------------------------------------
737
Derek Sollenberger6f485562015-07-30 10:00:39 -0400738void SkiaCanvas::drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
739 uirenderer::CanvasPropertyPrimitive* top, uirenderer::CanvasPropertyPrimitive* right,
740 uirenderer::CanvasPropertyPrimitive* bottom, uirenderer::CanvasPropertyPrimitive* rx,
741 uirenderer::CanvasPropertyPrimitive* ry, uirenderer::CanvasPropertyPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -0400742 sk_sp<uirenderer::skiapipeline::AnimatedRoundRect> drawable(
743 new uirenderer::skiapipeline::AnimatedRoundRect(left, top, right, bottom, rx, ry, paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400744 mCanvas->drawDrawable(drawable.get());
745}
746
747void SkiaCanvas::drawCircle(uirenderer::CanvasPropertyPrimitive* x, uirenderer::CanvasPropertyPrimitive* y,
748 uirenderer::CanvasPropertyPrimitive* radius, uirenderer::CanvasPropertyPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -0400749 sk_sp<uirenderer::skiapipeline::AnimatedCircle> drawable(new uirenderer::skiapipeline::AnimatedCircle(x, y, radius, paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400750 mCanvas->drawDrawable(drawable.get());
751}
752
753// ----------------------------------------------------------------------------
754// Canvas draw operations: View System
755// ----------------------------------------------------------------------------
756
Stan Ilievf50806a2016-10-24 10:40:39 -0400757void SkiaCanvas::drawLayer(uirenderer::DeferredLayerUpdater* layerUpdater) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400758 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw Layers");
759}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400760
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400761void SkiaCanvas::drawRenderNode(uirenderer::RenderNode* renderNode) {
762 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw RenderNodes");
763}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400764
John Reckcd1c3eb2016-04-14 10:38:54 -0700765void SkiaCanvas::callDrawGLFunction(Functor* functor,
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400766 uirenderer::GlFunctorLifecycleListener* listener) {
767 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw GL Content");
768}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400769
Derek Sollenberger8872b382014-06-23 14:13:53 -0400770} // namespace android