blob: c902a7333f2fd417e5575d9bbb99cacd1aa9fbf1 [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) {
Romain Guy82426562017-04-04 19:38:50 -070061 sk_sp<SkColorSpace> cs = bitmap.refColorSpace();
Mike Reed6acfe162016-11-18 17:21:09 -050062 mCanvasOwned = std::unique_ptr<SkCanvas>(new SkCanvas(bitmap));
Romain Guy82426562017-04-04 19:38:50 -070063 mCanvasWrapper = SkCreateColorSpaceXformCanvas(mCanvasOwned.get(),
64 cs == nullptr ? SkColorSpace::MakeSRGB() : std::move(cs));
Matt Sarettea70d222017-03-29 16:25:10 -040065 mCanvas = mCanvasWrapper.get();
Derek Sollenberger8872b382014-06-23 14:13:53 -040066}
67
Stan Iliev021693b2016-10-17 16:26:15 -040068SkiaCanvas::~SkiaCanvas() {}
69
Derek Sollenbergerc1908132016-07-15 10:28:16 -040070void SkiaCanvas::reset(SkCanvas* skiaCanvas) {
Mike Reed6acfe162016-11-18 17:21:09 -050071 if (mCanvas != skiaCanvas) {
72 mCanvas = skiaCanvas;
73 mCanvasOwned.reset();
74 }
Derek Sollenbergerc1908132016-07-15 10:28:16 -040075 mSaveStack.reset(nullptr);
76 mHighContrastText = false;
77}
78
Derek Sollenberger8872b382014-06-23 14:13:53 -040079// ----------------------------------------------------------------------------
80// Canvas state operations: Replace Bitmap
81// ----------------------------------------------------------------------------
82
John Reckc1b33d62015-04-22 09:04:45 -070083void SkiaCanvas::setBitmap(const SkBitmap& bitmap) {
Romain Guy82426562017-04-04 19:38:50 -070084 sk_sp<SkColorSpace> cs = bitmap.refColorSpace();
Matt Sarettea70d222017-03-29 16:25:10 -040085 std::unique_ptr<SkCanvas> newCanvas = std::unique_ptr<SkCanvas>(new SkCanvas(bitmap));
Romain Guy82426562017-04-04 19:38:50 -070086 std::unique_ptr<SkCanvas> newCanvasWrapper = SkCreateColorSpaceXformCanvas(newCanvas.get(),
87 cs == nullptr ? SkColorSpace::MakeSRGB() : std::move(cs));
Tony Mantler4f641d12017-03-14 22:36:14 +000088
Tony Mantler4f641d12017-03-14 22:36:14 +000089 // deletes the previously owned canvas (if any)
Matt Sarettea70d222017-03-29 16:25:10 -040090 mCanvasOwned = std::move(newCanvas);
91 mCanvasWrapper = std::move(newCanvasWrapper);
92 mCanvas = mCanvasWrapper.get();
Tony Mantler4f641d12017-03-14 22:36:14 +000093
Derek Sollenberger8872b382014-06-23 14:13:53 -040094 // clean up the old save stack
Stan Ilievf50806a2016-10-24 10:40:39 -040095 mSaveStack.reset(nullptr);
Derek Sollenberger8872b382014-06-23 14:13:53 -040096}
97
98// ----------------------------------------------------------------------------
99// Canvas state operations
100// ----------------------------------------------------------------------------
101
102bool SkiaCanvas::isOpaque() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400103 return mCanvas->imageInfo().isOpaque();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400104}
105
106int SkiaCanvas::width() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400107 return mCanvas->imageInfo().width();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400108}
109
110int SkiaCanvas::height() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400111 return mCanvas->imageInfo().height();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400112}
113
114// ----------------------------------------------------------------------------
115// Canvas state operations: Save (layer)
116// ----------------------------------------------------------------------------
117
118int SkiaCanvas::getSaveCount() const {
119 return mCanvas->getSaveCount();
120}
121
Florin Malitaeecff562015-12-21 10:43:01 -0500122int SkiaCanvas::save(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400123 int count = mCanvas->save();
124 recordPartialSave(flags);
125 return count;
126}
127
Florin Malita5e271402015-11-04 14:36:02 -0500128// The SkiaCanvas::restore operation layers on the capability to preserve
129// either (or both) the matrix and/or clip state after a SkCanvas::restore
130// operation. It does this by explicitly saving off the clip & matrix state
131// when requested and playing it back after the SkCanvas::restore.
Derek Sollenberger8872b382014-06-23 14:13:53 -0400132void SkiaCanvas::restore() {
Stan Ilievf50806a2016-10-24 10:40:39 -0400133 const auto* rec = this->currentSaveRec();
134 if (!rec) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400135 // Fast path - no record for this frame.
136 mCanvas->restore();
137 return;
138 }
139
Florin Malitaeecff562015-12-21 10:43:01 -0500140 bool preserveMatrix = !(rec->saveFlags & SaveFlags::Matrix);
141 bool preserveClip = !(rec->saveFlags & SaveFlags::Clip);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400142
143 SkMatrix savedMatrix;
144 if (preserveMatrix) {
145 savedMatrix = mCanvas->getTotalMatrix();
146 }
147
Stan Ilievf50806a2016-10-24 10:40:39 -0400148 const size_t clipIndex = rec->clipIndex;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400149
150 mCanvas->restore();
Stan Ilievf50806a2016-10-24 10:40:39 -0400151 mSaveStack->pop_back();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400152
153 if (preserveMatrix) {
154 mCanvas->setMatrix(savedMatrix);
155 }
156
Stan Ilievf50806a2016-10-24 10:40:39 -0400157 if (preserveClip) {
158 this->applyPersistentClips(clipIndex);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400159 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400160}
161
162void SkiaCanvas::restoreToCount(int restoreCount) {
163 while (mCanvas->getSaveCount() > restoreCount) {
164 this->restore();
165 }
166}
167
Florin Malitaeecff562015-12-21 10:43:01 -0500168static inline SkCanvas::SaveLayerFlags layerFlags(SaveFlags::Flags flags) {
169 SkCanvas::SaveLayerFlags layerFlags = 0;
170
Yuqian Li83427ff2016-09-14 11:14:06 -0400171 // We intentionally ignore the SaveFlags::HasAlphaLayer and
172 // SkCanvas::kIsOpaque_SaveLayerFlag flags because HWUI ignores it
173 // and our Android client may use it incorrectly.
174 // In Skia, this flag is purely for performance optimization.
Florin Malitaeecff562015-12-21 10:43:01 -0500175
176 if (!(flags & SaveFlags::ClipToLayer)) {
177 layerFlags |= SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag;
178 }
179
180 return layerFlags;
181}
182
Derek Sollenberger8872b382014-06-23 14:13:53 -0400183int SkiaCanvas::saveLayer(float left, float top, float right, float bottom,
Florin Malitaeecff562015-12-21 10:43:01 -0500184 const SkPaint* paint, SaveFlags::Flags flags) {
185 const SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
Derek Sollenbergerb8201192017-01-09 16:11:59 -0500186 const SkCanvas::SaveLayerRec rec(&bounds, paint, layerFlags(flags));
Florin Malitaeecff562015-12-21 10:43:01 -0500187
Stan Iliev68885e32016-12-14 11:18:34 -0500188 return mCanvas->saveLayer(rec);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400189}
190
191int SkiaCanvas::saveLayerAlpha(float left, float top, float right, float bottom,
Florin Malitaeecff562015-12-21 10:43:01 -0500192 int alpha, SaveFlags::Flags flags) {
Florin Malitaeecff562015-12-21 10:43:01 -0500193 if (static_cast<unsigned>(alpha) < 0xFF) {
Yuqian Lifd92ee42016-04-27 17:03:38 -0400194 SkPaint alphaPaint;
195 alphaPaint.setAlpha(alpha);
196 return this->saveLayer(left, top, right, bottom, &alphaPaint, flags);
Florin Malitaeecff562015-12-21 10:43:01 -0500197 }
Yuqian Lifd92ee42016-04-27 17:03:38 -0400198 return this->saveLayer(left, top, right, bottom, nullptr, flags);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400199}
200
Stan Ilievf50806a2016-10-24 10:40:39 -0400201class SkiaCanvas::Clip {
202public:
Mike Reed6e49c9f2016-12-02 15:36:59 -0500203 Clip(const SkRect& rect, SkClipOp op, const SkMatrix& m)
Stan Ilievf50806a2016-10-24 10:40:39 -0400204 : mType(Type::Rect), mOp(op), mMatrix(m), mRRect(SkRRect::MakeRect(rect)) {}
Mike Reed6e49c9f2016-12-02 15:36:59 -0500205 Clip(const SkRRect& rrect, SkClipOp op, const SkMatrix& m)
Stan Ilievf50806a2016-10-24 10:40:39 -0400206 : mType(Type::RRect), mOp(op), mMatrix(m), mRRect(rrect) {}
Mike Reed6e49c9f2016-12-02 15:36:59 -0500207 Clip(const SkPath& path, SkClipOp op, const SkMatrix& m)
Stan Ilievf50806a2016-10-24 10:40:39 -0400208 : mType(Type::Path), mOp(op), mMatrix(m), mPath(&path) {}
209
210 void apply(SkCanvas* canvas) const {
211 canvas->setMatrix(mMatrix);
212 switch (mType) {
213 case Type::Rect:
214 canvas->clipRect(mRRect.rect(), mOp);
215 break;
216 case Type::RRect:
217 canvas->clipRRect(mRRect, mOp);
218 break;
219 case Type::Path:
220 canvas->clipPath(*mPath.get(), mOp);
221 break;
222 }
223 }
224
225private:
226 enum class Type {
227 Rect,
228 RRect,
229 Path,
230 };
231
Mike Reed6e49c9f2016-12-02 15:36:59 -0500232 Type mType;
233 SkClipOp mOp;
234 SkMatrix mMatrix;
Stan Ilievf50806a2016-10-24 10:40:39 -0400235
236 // These are logically a union (tracked separately due to non-POD path).
237 SkTLazy<SkPath> mPath;
238 SkRRect mRRect;
239};
240
241const SkiaCanvas::SaveRec* SkiaCanvas::currentSaveRec() const {
242 const SaveRec* rec = mSaveStack
243 ? static_cast<const SaveRec*>(mSaveStack->back())
244 : nullptr;
245 int currentSaveCount = mCanvas->getSaveCount();
246 SkASSERT(!rec || currentSaveCount >= rec->saveCount);
247
248 return (rec && rec->saveCount == currentSaveCount) ? rec : nullptr;
249}
250
Derek Sollenberger8872b382014-06-23 14:13:53 -0400251// ----------------------------------------------------------------------------
252// functions to emulate legacy SaveFlags (i.e. independent matrix/clip flags)
253// ----------------------------------------------------------------------------
254
Florin Malitaeecff562015-12-21 10:43:01 -0500255void SkiaCanvas::recordPartialSave(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400256 // A partial save is a save operation which doesn't capture the full canvas state.
Florin Malitaeecff562015-12-21 10:43:01 -0500257 // (either SaveFlags::Matrix or SaveFlags::Clip is missing).
Derek Sollenberger8872b382014-06-23 14:13:53 -0400258
259 // Mask-out non canvas state bits.
Florin Malitaeecff562015-12-21 10:43:01 -0500260 flags &= SaveFlags::MatrixClip;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400261
Florin Malitaeecff562015-12-21 10:43:01 -0500262 if (flags == SaveFlags::MatrixClip) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400263 // not a partial save.
264 return;
265 }
266
Stan Ilievf50806a2016-10-24 10:40:39 -0400267 if (!mSaveStack) {
Ben Wagnerd1cbc162015-08-19 12:45:09 -0400268 mSaveStack.reset(new SkDeque(sizeof(struct SaveRec), 8));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400269 }
270
271 SaveRec* rec = static_cast<SaveRec*>(mSaveStack->push_back());
Florin Malita5e271402015-11-04 14:36:02 -0500272 rec->saveCount = mCanvas->getSaveCount();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400273 rec->saveFlags = flags;
Stan Ilievf50806a2016-10-24 10:40:39 -0400274 rec->clipIndex = mClipStack.size();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400275}
276
Stan Ilievf50806a2016-10-24 10:40:39 -0400277template <typename T>
Mike Reed6e49c9f2016-12-02 15:36:59 -0500278void SkiaCanvas::recordClip(const T& clip, SkClipOp op) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400279 // Only need tracking when in a partial save frame which
280 // doesn't restore the clip.
281 const SaveRec* rec = this->currentSaveRec();
282 if (rec && !(rec->saveFlags & SaveFlags::Clip)) {
283 mClipStack.emplace_back(clip, op, mCanvas->getTotalMatrix());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400284 }
285}
286
Stan Ilievf50806a2016-10-24 10:40:39 -0400287// Applies and optionally removes all clips >= index.
288void SkiaCanvas::applyPersistentClips(size_t clipStartIndex) {
289 SkASSERT(clipStartIndex <= mClipStack.size());
290 const auto begin = mClipStack.cbegin() + clipStartIndex;
291 const auto end = mClipStack.cend();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400292
Stan Ilievf50806a2016-10-24 10:40:39 -0400293 // Clip application mutates the CTM.
294 const SkMatrix saveMatrix = mCanvas->getTotalMatrix();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400295
Stan Ilievf50806a2016-10-24 10:40:39 -0400296 for (auto clip = begin; clip != end; ++clip) {
Mike Reed6acfe162016-11-18 17:21:09 -0500297 clip->apply(mCanvas);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400298 }
299
Stan Ilievf50806a2016-10-24 10:40:39 -0400300 mCanvas->setMatrix(saveMatrix);
301
302 // If the current/post-restore save rec is also persisting clips, we
303 // leave them on the stack to be reapplied part of the next restore().
304 // Otherwise we're done and just pop them.
305 const auto* rec = this->currentSaveRec();
306 if (!rec || (rec->saveFlags & SaveFlags::Clip)) {
307 mClipStack.erase(begin, end);
308 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400309}
310
311// ----------------------------------------------------------------------------
312// Canvas state operations: Matrix
313// ----------------------------------------------------------------------------
314
315void SkiaCanvas::getMatrix(SkMatrix* outMatrix) const {
316 *outMatrix = mCanvas->getTotalMatrix();
317}
318
319void SkiaCanvas::setMatrix(const SkMatrix& matrix) {
320 mCanvas->setMatrix(matrix);
321}
322
323void SkiaCanvas::concat(const SkMatrix& matrix) {
324 mCanvas->concat(matrix);
325}
326
327void SkiaCanvas::rotate(float degrees) {
328 mCanvas->rotate(degrees);
329}
330
331void SkiaCanvas::scale(float sx, float sy) {
332 mCanvas->scale(sx, sy);
333}
334
335void SkiaCanvas::skew(float sx, float sy) {
336 mCanvas->skew(sx, sy);
337}
338
339void SkiaCanvas::translate(float dx, float dy) {
340 mCanvas->translate(dx, dy);
341}
342
343// ----------------------------------------------------------------------------
344// Canvas state operations: Clips
345// ----------------------------------------------------------------------------
346
347// This function is a mirror of SkCanvas::getClipBounds except that it does
348// not outset the edge of the clip to account for anti-aliasing. There is
349// a skia bug to investigate pushing this logic into back into skia.
350// (see https://code.google.com/p/skia/issues/detail?id=1303)
351bool SkiaCanvas::getClipBounds(SkRect* outRect) const {
352 SkIRect ibounds;
Mike Reed5e438982017-01-25 08:23:25 -0500353 if (!mCanvas->getDeviceClipBounds(&ibounds)) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400354 return false;
355 }
356
357 SkMatrix inverse;
358 // if we can't invert the CTM, we can't return local clip bounds
359 if (!mCanvas->getTotalMatrix().invert(&inverse)) {
360 if (outRect) {
361 outRect->setEmpty();
362 }
363 return false;
364 }
365
366 if (NULL != outRect) {
367 SkRect r = SkRect::Make(ibounds);
368 inverse.mapRect(outRect, r);
369 }
370 return true;
371}
372
373bool SkiaCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
374 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
375 return mCanvas->quickReject(bounds);
376}
377
378bool SkiaCanvas::quickRejectPath(const SkPath& path) const {
379 return mCanvas->quickReject(path);
380}
381
Mike Reed6e49c9f2016-12-02 15:36:59 -0500382bool SkiaCanvas::clipRect(float left, float top, float right, float bottom, SkClipOp op) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400383 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
Stan Ilievf50806a2016-10-24 10:40:39 -0400384 this->recordClip(rect, op);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400385 mCanvas->clipRect(rect, op);
Chris Craik5ec6a282015-06-23 15:42:12 -0700386 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400387}
388
Mike Reed6e49c9f2016-12-02 15:36:59 -0500389bool SkiaCanvas::clipPath(const SkPath* path, SkClipOp op) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400390 SkRRect roundRect;
391 if (path->isRRect(&roundRect)) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400392 this->recordClip(roundRect, op);
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400393 mCanvas->clipRRect(roundRect, op);
394 } else {
Stan Ilievf50806a2016-10-24 10:40:39 -0400395 this->recordClip(*path, op);
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400396 mCanvas->clipPath(*path, op);
397 }
Chris Craik5ec6a282015-06-23 15:42:12 -0700398 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400399}
400
Derek Sollenberger8872b382014-06-23 14:13:53 -0400401// ----------------------------------------------------------------------------
402// Canvas state operations: Filters
403// ----------------------------------------------------------------------------
404
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400405SkDrawFilter* SkiaCanvas::getDrawFilter() {
406 return mCanvas->getDrawFilter();
407}
408
Derek Sollenberger8872b382014-06-23 14:13:53 -0400409void SkiaCanvas::setDrawFilter(SkDrawFilter* drawFilter) {
410 mCanvas->setDrawFilter(drawFilter);
411}
412
413// ----------------------------------------------------------------------------
414// Canvas draw operations
415// ----------------------------------------------------------------------------
416
Mike Reed260ab722016-10-07 15:59:20 -0400417void SkiaCanvas::drawColor(int color, SkBlendMode mode) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400418 mCanvas->drawColor(color, mode);
419}
420
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400421void SkiaCanvas::drawPaint(const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400422 mCanvas->drawPaint(paint);
423}
424
425// ----------------------------------------------------------------------------
426// Canvas draw operations: Geometry
427// ----------------------------------------------------------------------------
428
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400429void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400430 SkCanvas::PointMode mode) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500431 if (CC_UNLIKELY(count < 2 || paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400432 // convert the floats into SkPoints
433 count >>= 1; // now it is the number of points
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400434 std::unique_ptr<SkPoint[]> pts(new SkPoint[count]);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400435 for (int i = 0; i < count; i++) {
436 pts[i].set(points[0], points[1]);
437 points += 2;
438 }
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400439 mCanvas->drawPoints(mode, count, pts.get(), paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400440}
441
442
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400443void SkiaCanvas::drawPoint(float x, float y, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400444 mCanvas->drawPoint(x, y, paint);
445}
446
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400447void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400448 this->drawPoints(points, count, paint, SkCanvas::kPoints_PointMode);
449}
450
451void SkiaCanvas::drawLine(float startX, float startY, float stopX, float stopY,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400452 const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400453 mCanvas->drawLine(startX, startY, stopX, stopY, paint);
454}
455
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400456void SkiaCanvas::drawLines(const float* points, int count, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500457 if (CC_UNLIKELY(count < 4 || paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400458 this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode);
459}
460
461void SkiaCanvas::drawRect(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400462 const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500463 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Mike Reed6c9bb242017-04-04 09:15:37 -0400464 mCanvas->drawRect({left, top, right, bottom}, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400465
466}
467
Derek Sollenberger94394b32015-07-10 09:58:41 -0400468void SkiaCanvas::drawRegion(const SkRegion& region, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500469 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Stan Ilievf50806a2016-10-24 10:40:39 -0400470 mCanvas->drawRegion(region, paint);
Derek Sollenberger94394b32015-07-10 09:58:41 -0400471}
472
Derek Sollenberger8872b382014-06-23 14:13:53 -0400473void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400474 float rx, float ry, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500475 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400476 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
477 mCanvas->drawRoundRect(rect, rx, ry, paint);
478}
479
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400480void SkiaCanvas::drawCircle(float x, float y, float radius, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500481 if (CC_UNLIKELY(radius <= 0 || paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400482 mCanvas->drawCircle(x, y, radius, paint);
483}
484
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400485void SkiaCanvas::drawOval(float left, float top, float right, float bottom, 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 SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
488 mCanvas->drawOval(oval, paint);
489}
490
491void SkiaCanvas::drawArc(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400492 float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500493 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400494 SkRect arc = SkRect::MakeLTRB(left, top, right, bottom);
495 mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, paint);
496}
497
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400498void SkiaCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
Derek Sollenberger792fb322017-03-03 14:02:09 -0500499 if (CC_UNLIKELY(paint.nothingToDraw())) return;
Derek Sollenbergerd7f13f82017-03-09 13:08:27 -0500500 mCanvas->drawPath(path, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400501}
502
Mike Reed826deef2017-04-04 15:32:04 -0400503void SkiaCanvas::drawVertices(const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint) {
504 mCanvas->drawVertices(vertices, mode, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400505}
506
507// ----------------------------------------------------------------------------
508// Canvas draw operations: Bitmaps
509// ----------------------------------------------------------------------------
510
sergeyvaed7f582016-10-14 16:30:21 -0700511void SkiaCanvas::drawBitmap(Bitmap& bitmap, float left, float top, const SkPaint* paint) {
512 SkBitmap skBitmap;
513 bitmap.getSkBitmap(&skBitmap);
514 mCanvas->drawBitmap(skBitmap, left, top, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400515}
516
sergeyvfc9999502016-10-17 13:07:38 -0700517void SkiaCanvas::drawBitmap(Bitmap& hwuiBitmap, const SkMatrix& matrix, const SkPaint* paint) {
518 SkBitmap bitmap;
519 hwuiBitmap.getSkBitmap(&bitmap);
Mike Reed6acfe162016-11-18 17:21:09 -0500520 SkAutoCanvasRestore acr(mCanvas, true);
Mike Reed70ffbf92014-12-08 17:03:30 -0500521 mCanvas->concat(matrix);
522 mCanvas->drawBitmap(bitmap, 0, 0, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400523}
524
sergeyvfc9999502016-10-17 13:07:38 -0700525void SkiaCanvas::drawBitmap(Bitmap& hwuiBitmap, float srcLeft, float srcTop,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400526 float srcRight, float srcBottom, float dstLeft, float dstTop,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400527 float dstRight, float dstBottom, const SkPaint* paint) {
sergeyvfc9999502016-10-17 13:07:38 -0700528 SkBitmap bitmap;
529 hwuiBitmap.getSkBitmap(&bitmap);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400530 SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
531 SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400532 mCanvas->drawBitmapRect(bitmap, srcRect, dstRect, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400533}
534
sergeyv5fd2a1c2016-10-20 15:04:28 -0700535void SkiaCanvas::drawBitmapMesh(Bitmap& hwuiBitmap, int meshWidth, int meshHeight,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400536 const float* vertices, const int* colors, const SkPaint* paint) {
sergeyv5fd2a1c2016-10-20 15:04:28 -0700537 SkBitmap bitmap;
538 hwuiBitmap.getSkBitmap(&bitmap);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400539 const int ptCount = (meshWidth + 1) * (meshHeight + 1);
540 const int indexCount = meshWidth * meshHeight * 6;
Mike Reed871cd2d2017-03-17 10:15:52 -0400541 uint32_t flags = SkVertices::kHasTexCoords_BuilderFlag;
542 if (colors) {
543 flags |= SkVertices::kHasColors_BuilderFlag;
544 }
Mike Reed826deef2017-04-04 15:32:04 -0400545 SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, ptCount, indexCount, flags);
Mike Reed871cd2d2017-03-17 10:15:52 -0400546 memcpy(builder.positions(), vertices, ptCount * sizeof(SkPoint));
547 if (colors) {
548 memcpy(builder.colors(), colors, ptCount * sizeof(SkColor));
549 }
550 SkPoint* texs = builder.texCoords();
551 uint16_t* indices = builder.indices();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400552
553 // cons up texture coordinates and indices
554 {
555 const SkScalar w = SkIntToScalar(bitmap.width());
556 const SkScalar h = SkIntToScalar(bitmap.height());
557 const SkScalar dx = w / meshWidth;
558 const SkScalar dy = h / meshHeight;
559
560 SkPoint* texsPtr = texs;
561 SkScalar y = 0;
562 for (int i = 0; i <= meshHeight; i++) {
563 if (i == meshHeight) {
564 y = h; // to ensure numerically we hit h exactly
565 }
566 SkScalar x = 0;
567 for (int j = 0; j < meshWidth; j++) {
568 texsPtr->set(x, y);
569 texsPtr += 1;
570 x += dx;
571 }
572 texsPtr->set(w, y);
573 texsPtr += 1;
574 y += dy;
575 }
576 SkASSERT(texsPtr - texs == ptCount);
577 }
578
579 // cons up indices
580 {
581 uint16_t* indexPtr = indices;
582 int index = 0;
583 for (int i = 0; i < meshHeight; i++) {
584 for (int j = 0; j < meshWidth; j++) {
585 // lower-left triangle
586 *indexPtr++ = index;
587 *indexPtr++ = index + meshWidth + 1;
588 *indexPtr++ = index + meshWidth + 2;
589 // upper-right triangle
590 *indexPtr++ = index;
591 *indexPtr++ = index + meshWidth + 2;
592 *indexPtr++ = index + 1;
593 // bump to the next cell
594 index += 1;
595 }
596 // bump to the next row
597 index += 1;
598 }
599 SkASSERT(indexPtr - indices == indexCount);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400600 }
601
602 // double-check that we have legal indices
603#ifdef SK_DEBUG
604 {
605 for (int i = 0; i < indexCount; i++) {
606 SkASSERT((unsigned)indices[i] < (unsigned)ptCount);
607 }
608 }
609#endif
610
611 // cons-up a shader for the bitmap
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400612 SkPaint tmpPaint;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400613 if (paint) {
614 tmpPaint = *paint;
615 }
Stan Ilievf50806a2016-10-24 10:40:39 -0400616
617 sk_sp<SkImage> image = SkMakeImageFromRasterBitmap(bitmap, kNever_SkCopyPixelsMode);
618 tmpPaint.setShader(image->makeShader(SkShader::kClamp_TileMode, SkShader::kClamp_TileMode));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400619
Mike Reed871cd2d2017-03-17 10:15:52 -0400620 mCanvas->drawVertices(builder.detach(), SkBlendMode::kModulate, tmpPaint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400621}
622
sergeyv5fd2a1c2016-10-20 15:04:28 -0700623void SkiaCanvas::drawNinePatch(Bitmap& hwuiBitmap, const Res_png_9patch& chunk,
Derek Sollenbergeredca3202015-07-10 13:56:39 -0400624 float dstLeft, float dstTop, float dstRight, float dstBottom, const SkPaint* paint) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400625
sergeyv5fd2a1c2016-10-20 15:04:28 -0700626 SkBitmap bitmap;
627 hwuiBitmap.getSkBitmap(&bitmap);
Stan Ilievf50806a2016-10-24 10:40:39 -0400628
629 SkCanvas::Lattice lattice;
Matt Sarett7de73852016-10-25 18:36:39 -0400630 NinePatchUtils::SetLatticeDivs(&lattice, chunk, bitmap.width(), bitmap.height());
Stan Ilievf50806a2016-10-24 10:40:39 -0400631
632 lattice.fFlags = nullptr;
633 int numFlags = 0;
Stan Iliev021693b2016-10-17 16:26:15 -0400634 if (chunk.numColors > 0 && chunk.numColors == NinePatchUtils::NumDistinctRects(lattice)) {
Stan Ilievf50806a2016-10-24 10:40:39 -0400635 // We can expect the framework to give us a color for every distinct rect.
636 // Skia requires a flag for every rect.
637 numFlags = (lattice.fXCount + 1) * (lattice.fYCount + 1);
638 }
639
640 SkAutoSTMalloc<25, SkCanvas::Lattice::Flags> flags(numFlags);
641 if (numFlags > 0) {
Stan Iliev021693b2016-10-17 16:26:15 -0400642 NinePatchUtils::SetLatticeFlags(&lattice, flags.get(), numFlags, chunk);
Stan Ilievf50806a2016-10-24 10:40:39 -0400643 }
644
645 lattice.fBounds = nullptr;
646 SkRect dst = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
647 mCanvas->drawBitmapLattice(bitmap, lattice, dst, paint);
Derek Sollenbergeredca3202015-07-10 13:56:39 -0400648}
649
Doris Liu766431a2016-02-04 22:17:11 +0000650void SkiaCanvas::drawVectorDrawable(VectorDrawableRoot* vectorDrawable) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800651 vectorDrawable->drawStaging(this);
Doris Liu766431a2016-02-04 22:17:11 +0000652}
653
Derek Sollenberger8872b382014-06-23 14:13:53 -0400654// ----------------------------------------------------------------------------
655// Canvas draw operations: Text
656// ----------------------------------------------------------------------------
657
Stan Iliev0b58d992017-03-30 18:22:27 -0400658void SkiaCanvas::drawGlyphs(ReadGlyphFunc glyphFunc, int count, const SkPaint& paint, float x,
659 float y, float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
Tom Hudson8dfaa492014-12-09 15:03:44 -0500660 float totalAdvance) {
Stan Iliev0b58d992017-03-30 18:22:27 -0400661 if (count <= 0 || paint.nothingToDraw()) return;
Stan Ilievf50806a2016-10-24 10:40:39 -0400662 // Set align to left for drawing, as we don't want individual
663 // glyphs centered or right-aligned; the offset above takes
664 // care of all alignment.
665 SkPaint paintCopy(paint);
666 paintCopy.setTextAlign(SkPaint::kLeft_Align);
667
668 SkRect bounds = SkRect::MakeLTRB(boundsLeft + x, boundsTop + y,
669 boundsRight + x, boundsBottom + y);
670
671 SkTextBlobBuilder builder;
672 const SkTextBlobBuilder::RunBuffer& buffer = builder.allocRunPos(paintCopy, count, &bounds);
Stan Iliev0b58d992017-03-30 18:22:27 -0400673 glyphFunc(buffer.glyphs, buffer.pos);
Stan Ilievf50806a2016-10-24 10:40:39 -0400674
675 sk_sp<SkTextBlob> textBlob(builder.make());
676 mCanvas->drawTextBlob(textBlob, 0, 0, paintCopy);
677 drawTextDecorations(x, y, totalAdvance, paintCopy);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400678}
679
Yuqian Liafc221492016-07-18 13:07:42 -0400680void SkiaCanvas::drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
681 const SkPaint& paint, const SkPath& path, size_t start, size_t end) {
682 const int N = end - start;
Derek Sollenbergere547dd02016-11-09 11:55:59 -0500683 SkAutoSTMalloc<1024, uint8_t> storage(N * (sizeof(uint16_t) + sizeof(SkRSXform)));
Yuqian Liafc221492016-07-18 13:07:42 -0400684 SkRSXform* xform = (SkRSXform*)storage.get();
685 uint16_t* glyphs = (uint16_t*)(xform + N);
686 SkPathMeasure meas(path, false);
687
688 for (size_t i = start; i < end; i++) {
689 glyphs[i - start] = layout.getGlyphId(i);
690 float x = hOffset + layout.getX(i);
691 float y = vOffset + layout.getY(i);
692
693 SkPoint pos;
694 SkVector tan;
695 if (!meas.getPosTan(x, &pos, &tan)) {
696 pos.set(x, y);
697 tan.set(1, 0);
698 }
699 xform[i - start].fSCos = tan.x();
700 xform[i - start].fSSin = tan.y();
701 xform[i - start].fTx = pos.x() - tan.y() * y;
702 xform[i - start].fTy = pos.y() + tan.x() * y;
703 }
704
705 this->asSkCanvas()->drawTextRSXform(glyphs, sizeof(uint16_t) * N, xform, nullptr, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400706}
707
Derek Sollenberger6f485562015-07-30 10:00:39 -0400708// ----------------------------------------------------------------------------
709// Canvas draw operations: Animations
710// ----------------------------------------------------------------------------
711
Derek Sollenberger6f485562015-07-30 10:00:39 -0400712void SkiaCanvas::drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
713 uirenderer::CanvasPropertyPrimitive* top, uirenderer::CanvasPropertyPrimitive* right,
714 uirenderer::CanvasPropertyPrimitive* bottom, uirenderer::CanvasPropertyPrimitive* rx,
715 uirenderer::CanvasPropertyPrimitive* ry, uirenderer::CanvasPropertyPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -0400716 sk_sp<uirenderer::skiapipeline::AnimatedRoundRect> drawable(
717 new uirenderer::skiapipeline::AnimatedRoundRect(left, top, right, bottom, rx, ry, paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400718 mCanvas->drawDrawable(drawable.get());
719}
720
721void SkiaCanvas::drawCircle(uirenderer::CanvasPropertyPrimitive* x, uirenderer::CanvasPropertyPrimitive* y,
722 uirenderer::CanvasPropertyPrimitive* radius, uirenderer::CanvasPropertyPaint* paint) {
Stan Iliev021693b2016-10-17 16:26:15 -0400723 sk_sp<uirenderer::skiapipeline::AnimatedCircle> drawable(new uirenderer::skiapipeline::AnimatedCircle(x, y, radius, paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400724 mCanvas->drawDrawable(drawable.get());
725}
726
727// ----------------------------------------------------------------------------
728// Canvas draw operations: View System
729// ----------------------------------------------------------------------------
730
Stan Ilievf50806a2016-10-24 10:40:39 -0400731void SkiaCanvas::drawLayer(uirenderer::DeferredLayerUpdater* layerUpdater) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400732 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw Layers");
733}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400734
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400735void SkiaCanvas::drawRenderNode(uirenderer::RenderNode* renderNode) {
736 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw RenderNodes");
737}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400738
John Reckcd1c3eb2016-04-14 10:38:54 -0700739void SkiaCanvas::callDrawGLFunction(Functor* functor,
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400740 uirenderer::GlFunctorLifecycleListener* listener) {
741 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw GL Content");
742}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400743
Derek Sollenberger8872b382014-06-23 14:13:53 -0400744} // namespace android