blob: 50c9a57eaca74ac6b5c027046464c75f813f955e [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"
20#include "VectorDrawable.h"
sergeyvaed7f582016-10-14 16:30:21 -070021#include "hwui/Bitmap.h"
Yuqian Liafc221492016-07-18 13:07:42 -040022#include "hwui/MinikinUtils.h"
Derek Sollenbergerc1908132016-07-15 10:28:16 -040023
Derek Sollenberger6f485562015-07-30 10:00:39 -040024#include <SkDrawable.h>
John Reck849911a2015-01-20 07:51:14 -080025#include <SkDevice.h>
26#include <SkDeque.h>
27#include <SkDrawFilter.h>
28#include <SkGraphics.h>
Derek Sollenberger6f485562015-07-30 10:00:39 -040029#include <SkImage.h>
Matt Sarett62feb3a2016-09-20 10:34:11 -040030#include <SkImagePriv.h>
Yuqian Liafc221492016-07-18 13:07:42 -040031#include <SkRSXform.h>
John Reck849911a2015-01-20 07:51:14 -080032#include <SkShader.h>
John Reck849911a2015-01-20 07:51:14 -080033#include <SkTemplates.h>
Derek Sollenberger8872b382014-06-23 14:13:53 -040034
Ben Wagner60126ef2015-08-07 12:13:48 -040035#include <memory>
36
Derek Sollenberger8872b382014-06-23 14:13:53 -040037namespace android {
38
John Reckc1b33d62015-04-22 09:04:45 -070039Canvas* Canvas::create_canvas(const SkBitmap& bitmap) {
Derek Sollenberger8872b382014-06-23 14:13:53 -040040 return new SkiaCanvas(bitmap);
41}
42
43Canvas* Canvas::create_canvas(SkCanvas* skiaCanvas) {
44 return new SkiaCanvas(skiaCanvas);
45}
46
John Reckc1b33d62015-04-22 09:04:45 -070047SkiaCanvas::SkiaCanvas(const SkBitmap& bitmap) {
48 mCanvas.reset(new SkCanvas(bitmap));
Derek Sollenberger8872b382014-06-23 14:13:53 -040049}
50
Derek Sollenbergerc1908132016-07-15 10:28:16 -040051void SkiaCanvas::reset(SkCanvas* skiaCanvas) {
Derek Sollenberger86cbf882016-07-20 11:53:05 -040052 mCanvas.reset(SkRef(skiaCanvas));
Derek Sollenbergerc1908132016-07-15 10:28:16 -040053 mSaveStack.reset(nullptr);
54 mHighContrastText = false;
55}
56
Derek Sollenberger8872b382014-06-23 14:13:53 -040057// ----------------------------------------------------------------------------
58// Canvas state operations: Replace Bitmap
59// ----------------------------------------------------------------------------
60
61class ClipCopier : public SkCanvas::ClipVisitor {
62public:
Chih-Hung Hsiehc6baf562016-04-27 11:29:23 -070063 explicit ClipCopier(SkCanvas* dstCanvas) : m_dstCanvas(dstCanvas) {}
Derek Sollenberger8872b382014-06-23 14:13:53 -040064
65 virtual void clipRect(const SkRect& rect, SkRegion::Op op, bool antialias) {
66 m_dstCanvas->clipRect(rect, op, antialias);
67 }
68 virtual void clipRRect(const SkRRect& rrect, SkRegion::Op op, bool antialias) {
69 m_dstCanvas->clipRRect(rrect, op, antialias);
70 }
71 virtual void clipPath(const SkPath& path, SkRegion::Op op, bool antialias) {
72 m_dstCanvas->clipPath(path, op, antialias);
73 }
74
75private:
76 SkCanvas* m_dstCanvas;
77};
78
John Reckc1b33d62015-04-22 09:04:45 -070079void SkiaCanvas::setBitmap(const SkBitmap& bitmap) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -040080 sk_sp<SkCanvas> newCanvas(new SkCanvas(bitmap));
Derek Sollenberger8872b382014-06-23 14:13:53 -040081
John Reckc1b33d62015-04-22 09:04:45 -070082 if (!bitmap.isNull()) {
Derek Sollenberger8872b382014-06-23 14:13:53 -040083 // Copy the canvas matrix & clip state.
84 newCanvas->setMatrix(mCanvas->getTotalMatrix());
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -040085
Derek Sollenbergerc1908132016-07-15 10:28:16 -040086 ClipCopier copier(newCanvas.get());
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -040087 mCanvas->replayClips(&copier);
Derek Sollenberger8872b382014-06-23 14:13:53 -040088 }
89
90 // unrefs the existing canvas
Derek Sollenbergerc1908132016-07-15 10:28:16 -040091 mCanvas = std::move(newCanvas);
Derek Sollenberger8872b382014-06-23 14:13:53 -040092
93 // clean up the old save stack
94 mSaveStack.reset(NULL);
95}
96
97// ----------------------------------------------------------------------------
98// Canvas state operations
99// ----------------------------------------------------------------------------
100
101bool SkiaCanvas::isOpaque() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400102 return mCanvas->imageInfo().isOpaque();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400103}
104
105int SkiaCanvas::width() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400106 return mCanvas->imageInfo().width();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400107}
108
109int SkiaCanvas::height() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400110 return mCanvas->imageInfo().height();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400111}
112
113// ----------------------------------------------------------------------------
114// Canvas state operations: Save (layer)
115// ----------------------------------------------------------------------------
116
117int SkiaCanvas::getSaveCount() const {
118 return mCanvas->getSaveCount();
119}
120
Florin Malitaeecff562015-12-21 10:43:01 -0500121int SkiaCanvas::save(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400122 int count = mCanvas->save();
123 recordPartialSave(flags);
124 return count;
125}
126
Florin Malita5e271402015-11-04 14:36:02 -0500127// The SkiaCanvas::restore operation layers on the capability to preserve
128// either (or both) the matrix and/or clip state after a SkCanvas::restore
129// operation. It does this by explicitly saving off the clip & matrix state
130// when requested and playing it back after the SkCanvas::restore.
Derek Sollenberger8872b382014-06-23 14:13:53 -0400131void SkiaCanvas::restore() {
132 const SaveRec* rec = (NULL == mSaveStack.get())
133 ? NULL
134 : static_cast<SaveRec*>(mSaveStack->back());
Florin Malita5e271402015-11-04 14:36:02 -0500135 int currentSaveCount = mCanvas->getSaveCount();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400136 SkASSERT(NULL == rec || currentSaveCount >= rec->saveCount);
137
138 if (NULL == rec || rec->saveCount != currentSaveCount) {
139 // Fast path - no record for this frame.
140 mCanvas->restore();
141 return;
142 }
143
Florin Malitaeecff562015-12-21 10:43:01 -0500144 bool preserveMatrix = !(rec->saveFlags & SaveFlags::Matrix);
145 bool preserveClip = !(rec->saveFlags & SaveFlags::Clip);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400146
147 SkMatrix savedMatrix;
148 if (preserveMatrix) {
149 savedMatrix = mCanvas->getTotalMatrix();
150 }
151
152 SkTArray<SkClipStack::Element> savedClips;
Florin Malita5e271402015-11-04 14:36:02 -0500153 int topClipStackFrame = mCanvas->getClipStack()->getSaveCount();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400154 if (preserveClip) {
Florin Malita5e271402015-11-04 14:36:02 -0500155 saveClipsForFrame(savedClips, topClipStackFrame);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400156 }
157
158 mCanvas->restore();
159
160 if (preserveMatrix) {
161 mCanvas->setMatrix(savedMatrix);
162 }
163
Florin Malita5e271402015-11-04 14:36:02 -0500164 if (preserveClip && !savedClips.empty() &&
165 topClipStackFrame != mCanvas->getClipStack()->getSaveCount()) {
166 // Only reapply the saved clips if the top clip stack frame was actually
167 // popped by restore(). If it wasn't, it means it doesn't belong to the
168 // restored canvas frame (SkCanvas lazy save/restore kicked in).
Derek Sollenberger8872b382014-06-23 14:13:53 -0400169 applyClips(savedClips);
170 }
171
172 mSaveStack->pop_back();
173}
174
175void SkiaCanvas::restoreToCount(int restoreCount) {
176 while (mCanvas->getSaveCount() > restoreCount) {
177 this->restore();
178 }
179}
180
Florin Malitaeecff562015-12-21 10:43:01 -0500181static inline SkCanvas::SaveLayerFlags layerFlags(SaveFlags::Flags flags) {
182 SkCanvas::SaveLayerFlags layerFlags = 0;
183
Yuqian Li83427ff2016-09-14 11:14:06 -0400184 // We intentionally ignore the SaveFlags::HasAlphaLayer and
185 // SkCanvas::kIsOpaque_SaveLayerFlag flags because HWUI ignores it
186 // and our Android client may use it incorrectly.
187 // In Skia, this flag is purely for performance optimization.
Florin Malitaeecff562015-12-21 10:43:01 -0500188
189 if (!(flags & SaveFlags::ClipToLayer)) {
190 layerFlags |= SkCanvas::kDontClipToLayer_Legacy_SaveLayerFlag;
191 }
192
193 return layerFlags;
194}
195
Derek Sollenberger8872b382014-06-23 14:13:53 -0400196int SkiaCanvas::saveLayer(float left, float top, float right, float bottom,
Florin Malitaeecff562015-12-21 10:43:01 -0500197 const SkPaint* paint, SaveFlags::Flags flags) {
198 const SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
199 const SkCanvas::SaveLayerRec rec(&bounds, paint, layerFlags(flags));
200
201 int count = mCanvas->saveLayer(rec);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400202 recordPartialSave(flags);
203 return count;
204}
205
206int SkiaCanvas::saveLayerAlpha(float left, float top, float right, float bottom,
Florin Malitaeecff562015-12-21 10:43:01 -0500207 int alpha, SaveFlags::Flags flags) {
Florin Malitaeecff562015-12-21 10:43:01 -0500208 if (static_cast<unsigned>(alpha) < 0xFF) {
Yuqian Lifd92ee42016-04-27 17:03:38 -0400209 SkPaint alphaPaint;
210 alphaPaint.setAlpha(alpha);
211 return this->saveLayer(left, top, right, bottom, &alphaPaint, flags);
Florin Malitaeecff562015-12-21 10:43:01 -0500212 }
Yuqian Lifd92ee42016-04-27 17:03:38 -0400213 return this->saveLayer(left, top, right, bottom, nullptr, flags);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400214}
215
216// ----------------------------------------------------------------------------
217// functions to emulate legacy SaveFlags (i.e. independent matrix/clip flags)
218// ----------------------------------------------------------------------------
219
Florin Malitaeecff562015-12-21 10:43:01 -0500220void SkiaCanvas::recordPartialSave(SaveFlags::Flags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400221 // A partial save is a save operation which doesn't capture the full canvas state.
Florin Malitaeecff562015-12-21 10:43:01 -0500222 // (either SaveFlags::Matrix or SaveFlags::Clip is missing).
Derek Sollenberger8872b382014-06-23 14:13:53 -0400223
224 // Mask-out non canvas state bits.
Florin Malitaeecff562015-12-21 10:43:01 -0500225 flags &= SaveFlags::MatrixClip;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400226
Florin Malitaeecff562015-12-21 10:43:01 -0500227 if (flags == SaveFlags::MatrixClip) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400228 // not a partial save.
229 return;
230 }
231
232 if (NULL == mSaveStack.get()) {
Ben Wagnerd1cbc162015-08-19 12:45:09 -0400233 mSaveStack.reset(new SkDeque(sizeof(struct SaveRec), 8));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400234 }
235
236 SaveRec* rec = static_cast<SaveRec*>(mSaveStack->push_back());
Florin Malita5e271402015-11-04 14:36:02 -0500237 rec->saveCount = mCanvas->getSaveCount();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400238 rec->saveFlags = flags;
239}
240
Florin Malita5e271402015-11-04 14:36:02 -0500241void SkiaCanvas::saveClipsForFrame(SkTArray<SkClipStack::Element>& clips,
242 int saveCountToBackup) {
243 // Each SkClipStack::Element stores the index of the canvas save
244 // with which it is associated. Backup only those Elements that
245 // are associated with 'saveCountToBackup'
Derek Sollenberger8872b382014-06-23 14:13:53 -0400246 SkClipStack::Iter clipIterator(*mCanvas->getClipStack(),
247 SkClipStack::Iter::kTop_IterStart);
Florin Malita5e271402015-11-04 14:36:02 -0500248 while (const SkClipStack::Element* elem = clipIterator.prev()) {
249 if (elem->getSaveCount() < saveCountToBackup) {
250 // done with the target save count.
Derek Sollenberger8872b382014-06-23 14:13:53 -0400251 break;
252 }
Florin Malita5e271402015-11-04 14:36:02 -0500253 SkASSERT(elem->getSaveCount() == saveCountToBackup);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400254 clips.push_back(*elem);
255 }
256}
257
258void SkiaCanvas::applyClips(const SkTArray<SkClipStack::Element>& clips) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400259 ClipCopier clipCopier(mCanvas.get());
Derek Sollenberger8872b382014-06-23 14:13:53 -0400260
261 // The clip stack stores clips in device space.
262 SkMatrix origMatrix = mCanvas->getTotalMatrix();
263 mCanvas->resetMatrix();
264
265 // We pushed the clips in reverse order.
266 for (int i = clips.count() - 1; i >= 0; --i) {
267 clips[i].replay(&clipCopier);
268 }
269
270 mCanvas->setMatrix(origMatrix);
271}
272
273// ----------------------------------------------------------------------------
274// Canvas state operations: Matrix
275// ----------------------------------------------------------------------------
276
277void SkiaCanvas::getMatrix(SkMatrix* outMatrix) const {
278 *outMatrix = mCanvas->getTotalMatrix();
279}
280
281void SkiaCanvas::setMatrix(const SkMatrix& matrix) {
282 mCanvas->setMatrix(matrix);
283}
284
285void SkiaCanvas::concat(const SkMatrix& matrix) {
286 mCanvas->concat(matrix);
287}
288
289void SkiaCanvas::rotate(float degrees) {
290 mCanvas->rotate(degrees);
291}
292
293void SkiaCanvas::scale(float sx, float sy) {
294 mCanvas->scale(sx, sy);
295}
296
297void SkiaCanvas::skew(float sx, float sy) {
298 mCanvas->skew(sx, sy);
299}
300
301void SkiaCanvas::translate(float dx, float dy) {
302 mCanvas->translate(dx, dy);
303}
304
305// ----------------------------------------------------------------------------
306// Canvas state operations: Clips
307// ----------------------------------------------------------------------------
308
309// This function is a mirror of SkCanvas::getClipBounds except that it does
310// not outset the edge of the clip to account for anti-aliasing. There is
311// a skia bug to investigate pushing this logic into back into skia.
312// (see https://code.google.com/p/skia/issues/detail?id=1303)
313bool SkiaCanvas::getClipBounds(SkRect* outRect) const {
314 SkIRect ibounds;
315 if (!mCanvas->getClipDeviceBounds(&ibounds)) {
316 return false;
317 }
318
319 SkMatrix inverse;
320 // if we can't invert the CTM, we can't return local clip bounds
321 if (!mCanvas->getTotalMatrix().invert(&inverse)) {
322 if (outRect) {
323 outRect->setEmpty();
324 }
325 return false;
326 }
327
328 if (NULL != outRect) {
329 SkRect r = SkRect::Make(ibounds);
330 inverse.mapRect(outRect, r);
331 }
332 return true;
333}
334
335bool SkiaCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
336 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
337 return mCanvas->quickReject(bounds);
338}
339
340bool SkiaCanvas::quickRejectPath(const SkPath& path) const {
341 return mCanvas->quickReject(path);
342}
343
344bool SkiaCanvas::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
345 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
346 mCanvas->clipRect(rect, op);
Chris Craik5ec6a282015-06-23 15:42:12 -0700347 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400348}
349
350bool SkiaCanvas::clipPath(const SkPath* path, SkRegion::Op op) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400351 SkRRect roundRect;
352 if (path->isRRect(&roundRect)) {
353 mCanvas->clipRRect(roundRect, op);
354 } else {
355 mCanvas->clipPath(*path, op);
356 }
Chris Craik5ec6a282015-06-23 15:42:12 -0700357 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400358}
359
360bool SkiaCanvas::clipRegion(const SkRegion* region, SkRegion::Op op) {
361 SkPath rgnPath;
362 if (region->getBoundaryPath(&rgnPath)) {
363 // The region is specified in device space.
364 SkMatrix savedMatrix = mCanvas->getTotalMatrix();
365 mCanvas->resetMatrix();
366 mCanvas->clipPath(rgnPath, op);
367 mCanvas->setMatrix(savedMatrix);
368 } else {
369 mCanvas->clipRect(SkRect::MakeEmpty(), op);
370 }
Chris Craik5ec6a282015-06-23 15:42:12 -0700371 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400372}
373
374// ----------------------------------------------------------------------------
375// Canvas state operations: Filters
376// ----------------------------------------------------------------------------
377
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400378SkDrawFilter* SkiaCanvas::getDrawFilter() {
379 return mCanvas->getDrawFilter();
380}
381
Derek Sollenberger8872b382014-06-23 14:13:53 -0400382void SkiaCanvas::setDrawFilter(SkDrawFilter* drawFilter) {
383 mCanvas->setDrawFilter(drawFilter);
384}
385
386// ----------------------------------------------------------------------------
387// Canvas draw operations
388// ----------------------------------------------------------------------------
389
Mike Reed260ab722016-10-07 15:59:20 -0400390void SkiaCanvas::drawColor(int color, SkBlendMode mode) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400391 mCanvas->drawColor(color, mode);
392}
393
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400394void SkiaCanvas::drawPaint(const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400395 mCanvas->drawPaint(paint);
396}
397
398// ----------------------------------------------------------------------------
399// Canvas draw operations: Geometry
400// ----------------------------------------------------------------------------
401
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400402void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400403 SkCanvas::PointMode mode) {
404 // convert the floats into SkPoints
405 count >>= 1; // now it is the number of points
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400406 std::unique_ptr<SkPoint[]> pts(new SkPoint[count]);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400407 for (int i = 0; i < count; i++) {
408 pts[i].set(points[0], points[1]);
409 points += 2;
410 }
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400411 mCanvas->drawPoints(mode, count, pts.get(), paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400412}
413
414
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400415void SkiaCanvas::drawPoint(float x, float y, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400416 mCanvas->drawPoint(x, y, paint);
417}
418
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400419void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400420 this->drawPoints(points, count, paint, SkCanvas::kPoints_PointMode);
421}
422
423void SkiaCanvas::drawLine(float startX, float startY, float stopX, float stopY,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400424 const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400425 mCanvas->drawLine(startX, startY, stopX, stopY, paint);
426}
427
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400428void SkiaCanvas::drawLines(const float* points, int count, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400429 this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode);
430}
431
432void SkiaCanvas::drawRect(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400433 const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400434 mCanvas->drawRectCoords(left, top, right, bottom, paint);
435
436}
437
Derek Sollenberger94394b32015-07-10 09:58:41 -0400438void SkiaCanvas::drawRegion(const SkRegion& region, const SkPaint& paint) {
439 SkRegion::Iterator it(region);
440 while (!it.done()) {
441 mCanvas->drawRect(SkRect::Make(it.rect()), paint);
442 it.next();
443 }
444}
445
Derek Sollenberger8872b382014-06-23 14:13:53 -0400446void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400447 float rx, float ry, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400448 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
449 mCanvas->drawRoundRect(rect, rx, ry, paint);
450}
451
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400452void SkiaCanvas::drawCircle(float x, float y, float radius, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400453 mCanvas->drawCircle(x, y, radius, paint);
454}
455
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400456void SkiaCanvas::drawOval(float left, float top, float right, float bottom, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400457 SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
458 mCanvas->drawOval(oval, paint);
459}
460
461void SkiaCanvas::drawArc(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400462 float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400463 SkRect arc = SkRect::MakeLTRB(left, top, right, bottom);
464 mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, paint);
465}
466
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400467void SkiaCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400468 SkRect rect;
469 SkRRect roundRect;
470 if (path.isOval(&rect)) {
471 mCanvas->drawOval(rect, paint);
472 } else if (path.isRRect(&roundRect)) {
473 mCanvas->drawRRect(roundRect, paint);
474 } else {
475 mCanvas->drawPath(path, paint);
476 }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400477}
478
479void SkiaCanvas::drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
480 const float* verts, const float* texs, const int* colors,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400481 const uint16_t* indices, int indexCount, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400482#ifndef SK_SCALAR_IS_FLOAT
483 SkDEBUGFAIL("SkScalar must be a float for these conversions to be valid");
484#endif
485 const int ptCount = vertexCount >> 1;
486 mCanvas->drawVertices(vertexMode, ptCount, (SkPoint*)verts, (SkPoint*)texs,
487 (SkColor*)colors, NULL, indices, indexCount, paint);
488}
489
490// ----------------------------------------------------------------------------
491// Canvas draw operations: Bitmaps
492// ----------------------------------------------------------------------------
493
sergeyvaed7f582016-10-14 16:30:21 -0700494void SkiaCanvas::drawBitmap(Bitmap& bitmap, float left, float top, const SkPaint* paint) {
495 SkBitmap skBitmap;
496 bitmap.getSkBitmap(&skBitmap);
497 mCanvas->drawBitmap(skBitmap, left, top, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400498}
499
sergeyvfc9999502016-10-17 13:07:38 -0700500void SkiaCanvas::drawBitmap(Bitmap& hwuiBitmap, const SkMatrix& matrix, const SkPaint* paint) {
501 SkBitmap bitmap;
502 hwuiBitmap.getSkBitmap(&bitmap);
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400503 SkAutoCanvasRestore acr(mCanvas.get(), true);
Mike Reed70ffbf92014-12-08 17:03:30 -0500504 mCanvas->concat(matrix);
505 mCanvas->drawBitmap(bitmap, 0, 0, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400506}
507
sergeyvfc9999502016-10-17 13:07:38 -0700508void SkiaCanvas::drawBitmap(Bitmap& hwuiBitmap, float srcLeft, float srcTop,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400509 float srcRight, float srcBottom, float dstLeft, float dstTop,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400510 float dstRight, float dstBottom, const SkPaint* paint) {
sergeyvfc9999502016-10-17 13:07:38 -0700511 SkBitmap bitmap;
512 hwuiBitmap.getSkBitmap(&bitmap);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400513 SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
514 SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400515 mCanvas->drawBitmapRect(bitmap, srcRect, dstRect, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400516}
517
sergeyv5fd2a1c2016-10-20 15:04:28 -0700518void SkiaCanvas::drawBitmapMesh(Bitmap& hwuiBitmap, int meshWidth, int meshHeight,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400519 const float* vertices, const int* colors, const SkPaint* paint) {
sergeyv5fd2a1c2016-10-20 15:04:28 -0700520 SkBitmap bitmap;
521 hwuiBitmap.getSkBitmap(&bitmap);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400522 const int ptCount = (meshWidth + 1) * (meshHeight + 1);
523 const int indexCount = meshWidth * meshHeight * 6;
524
525 /* Our temp storage holds 2 or 3 arrays.
526 texture points [ptCount * sizeof(SkPoint)]
527 optionally vertex points [ptCount * sizeof(SkPoint)] if we need a
528 copy to convert from float to fixed
529 indices [ptCount * sizeof(uint16_t)]
530 */
531 ssize_t storageSize = ptCount * sizeof(SkPoint); // texs[]
532 storageSize += indexCount * sizeof(uint16_t); // indices[]
533
534
535#ifndef SK_SCALAR_IS_FLOAT
536 SkDEBUGFAIL("SkScalar must be a float for these conversions to be valid");
537#endif
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400538 std::unique_ptr<char[]> storage(new char[storageSize]);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400539 SkPoint* texs = (SkPoint*)storage.get();
540 uint16_t* indices = (uint16_t*)(texs + ptCount);
541
542 // cons up texture coordinates and indices
543 {
544 const SkScalar w = SkIntToScalar(bitmap.width());
545 const SkScalar h = SkIntToScalar(bitmap.height());
546 const SkScalar dx = w / meshWidth;
547 const SkScalar dy = h / meshHeight;
548
549 SkPoint* texsPtr = texs;
550 SkScalar y = 0;
551 for (int i = 0; i <= meshHeight; i++) {
552 if (i == meshHeight) {
553 y = h; // to ensure numerically we hit h exactly
554 }
555 SkScalar x = 0;
556 for (int j = 0; j < meshWidth; j++) {
557 texsPtr->set(x, y);
558 texsPtr += 1;
559 x += dx;
560 }
561 texsPtr->set(w, y);
562 texsPtr += 1;
563 y += dy;
564 }
565 SkASSERT(texsPtr - texs == ptCount);
566 }
567
568 // cons up indices
569 {
570 uint16_t* indexPtr = indices;
571 int index = 0;
572 for (int i = 0; i < meshHeight; i++) {
573 for (int j = 0; j < meshWidth; j++) {
574 // lower-left triangle
575 *indexPtr++ = index;
576 *indexPtr++ = index + meshWidth + 1;
577 *indexPtr++ = index + meshWidth + 2;
578 // upper-right triangle
579 *indexPtr++ = index;
580 *indexPtr++ = index + meshWidth + 2;
581 *indexPtr++ = index + 1;
582 // bump to the next cell
583 index += 1;
584 }
585 // bump to the next row
586 index += 1;
587 }
588 SkASSERT(indexPtr - indices == indexCount);
589 SkASSERT((char*)indexPtr - (char*)storage.get() == storageSize);
590 }
591
592 // double-check that we have legal indices
593#ifdef SK_DEBUG
594 {
595 for (int i = 0; i < indexCount; i++) {
596 SkASSERT((unsigned)indices[i] < (unsigned)ptCount);
597 }
598 }
599#endif
600
601 // cons-up a shader for the bitmap
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400602 SkPaint tmpPaint;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400603 if (paint) {
604 tmpPaint = *paint;
605 }
Matt Sarett62feb3a2016-09-20 10:34:11 -0400606 sk_sp<SkShader> shader = SkMakeBitmapShader(bitmap,
607 SkShader::kClamp_TileMode,
608 SkShader::kClamp_TileMode,
609 nullptr,
610 kNever_SkCopyPixelsMode,
611 nullptr);
612 tmpPaint.setShader(std::move(shader));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400613
614 mCanvas->drawVertices(SkCanvas::kTriangles_VertexMode, ptCount, (SkPoint*)vertices,
615 texs, (const SkColor*)colors, NULL, indices,
616 indexCount, tmpPaint);
617}
618
sergeyv5fd2a1c2016-10-20 15:04:28 -0700619void SkiaCanvas::drawNinePatch(Bitmap& hwuiBitmap, const Res_png_9patch& chunk,
Derek Sollenbergeredca3202015-07-10 13:56:39 -0400620 float dstLeft, float dstTop, float dstRight, float dstBottom, const SkPaint* paint) {
sergeyv5fd2a1c2016-10-20 15:04:28 -0700621 SkBitmap bitmap;
622 hwuiBitmap.getSkBitmap(&bitmap);
Derek Sollenbergeredca3202015-07-10 13:56:39 -0400623 SkRect bounds = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400624 NinePatch::Draw(mCanvas.get(), bounds, bitmap, chunk, paint, nullptr);
Derek Sollenbergeredca3202015-07-10 13:56:39 -0400625}
626
Doris Liu766431a2016-02-04 22:17:11 +0000627void SkiaCanvas::drawVectorDrawable(VectorDrawableRoot* vectorDrawable) {
Doris Liu1d8e1942016-03-02 15:16:28 -0800628 vectorDrawable->drawStaging(this);
Doris Liu766431a2016-02-04 22:17:11 +0000629}
630
Derek Sollenberger8872b382014-06-23 14:13:53 -0400631// ----------------------------------------------------------------------------
632// Canvas draw operations: Text
633// ----------------------------------------------------------------------------
634
sergeyvdccca442016-03-21 15:38:21 -0700635void SkiaCanvas::drawGlyphs(const uint16_t* text, const float* positions, int count,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400636 const SkPaint& paint, float x, float y,
Tom Hudson8dfaa492014-12-09 15:03:44 -0500637 float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
638 float totalAdvance) {
Ben Wagnere3a40ea2015-08-19 15:49:37 -0400639 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats");
Derek Sollenberger79abbf22016-03-24 11:07:19 -0400640 mCanvas->drawPosText(text, count << 1, reinterpret_cast<const SkPoint*>(positions), paint);
Chris Craika1717272015-11-19 13:02:43 -0800641 drawTextDecorations(x, y, totalAdvance, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400642}
643
Yuqian Liafc221492016-07-18 13:07:42 -0400644void SkiaCanvas::drawLayoutOnPath(const minikin::Layout& layout, float hOffset, float vOffset,
645 const SkPaint& paint, const SkPath& path, size_t start, size_t end) {
646 const int N = end - start;
647 SkAutoSMalloc<1024> storage(N * (sizeof(uint16_t) + sizeof(SkRSXform)));
648 SkRSXform* xform = (SkRSXform*)storage.get();
649 uint16_t* glyphs = (uint16_t*)(xform + N);
650 SkPathMeasure meas(path, false);
651
652 for (size_t i = start; i < end; i++) {
653 glyphs[i - start] = layout.getGlyphId(i);
654 float x = hOffset + layout.getX(i);
655 float y = vOffset + layout.getY(i);
656
657 SkPoint pos;
658 SkVector tan;
659 if (!meas.getPosTan(x, &pos, &tan)) {
660 pos.set(x, y);
661 tan.set(1, 0);
662 }
663 xform[i - start].fSCos = tan.x();
664 xform[i - start].fSSin = tan.y();
665 xform[i - start].fTx = pos.x() - tan.y() * y;
666 xform[i - start].fTy = pos.y() + tan.x() * y;
667 }
668
669 this->asSkCanvas()->drawTextRSXform(glyphs, sizeof(uint16_t) * N, xform, nullptr, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400670}
671
Derek Sollenberger6f485562015-07-30 10:00:39 -0400672// ----------------------------------------------------------------------------
673// Canvas draw operations: Animations
674// ----------------------------------------------------------------------------
675
676class AnimatedRoundRect : public SkDrawable {
677 public:
678 AnimatedRoundRect(uirenderer::CanvasPropertyPrimitive* left,
679 uirenderer::CanvasPropertyPrimitive* top, uirenderer::CanvasPropertyPrimitive* right,
680 uirenderer::CanvasPropertyPrimitive* bottom, uirenderer::CanvasPropertyPrimitive* rx,
681 uirenderer::CanvasPropertyPrimitive* ry, uirenderer::CanvasPropertyPaint* p) :
682 mLeft(left), mTop(top), mRight(right), mBottom(bottom), mRx(rx), mRy(ry), mPaint(p) {}
683
684 protected:
685 virtual SkRect onGetBounds() override {
686 return SkRect::MakeLTRB(mLeft->value, mTop->value, mRight->value, mBottom->value);
687 }
688 virtual void onDraw(SkCanvas* canvas) override {
689 SkRect rect = SkRect::MakeLTRB(mLeft->value, mTop->value, mRight->value, mBottom->value);
690 canvas->drawRoundRect(rect, mRx->value, mRy->value, mPaint->value);
691 }
692
693 private:
694 sp<uirenderer::CanvasPropertyPrimitive> mLeft;
695 sp<uirenderer::CanvasPropertyPrimitive> mTop;
696 sp<uirenderer::CanvasPropertyPrimitive> mRight;
697 sp<uirenderer::CanvasPropertyPrimitive> mBottom;
698 sp<uirenderer::CanvasPropertyPrimitive> mRx;
699 sp<uirenderer::CanvasPropertyPrimitive> mRy;
700 sp<uirenderer::CanvasPropertyPaint> mPaint;
701};
702
703class AnimatedCircle : public SkDrawable {
704 public:
705 AnimatedCircle(uirenderer::CanvasPropertyPrimitive* x, uirenderer::CanvasPropertyPrimitive* y,
706 uirenderer::CanvasPropertyPrimitive* radius, uirenderer::CanvasPropertyPaint* paint) :
707 mX(x), mY(y), mRadius(radius), mPaint(paint) {}
708
709 protected:
710 virtual SkRect onGetBounds() override {
711 const float x = mX->value;
712 const float y = mY->value;
713 const float radius = mRadius->value;
714 return SkRect::MakeLTRB(x - radius, y - radius, x + radius, y + radius);
715 }
716 virtual void onDraw(SkCanvas* canvas) override {
717 canvas->drawCircle(mX->value, mY->value, mRadius->value, mPaint->value);
718 }
719
720 private:
721 sp<uirenderer::CanvasPropertyPrimitive> mX;
722 sp<uirenderer::CanvasPropertyPrimitive> mY;
723 sp<uirenderer::CanvasPropertyPrimitive> mRadius;
724 sp<uirenderer::CanvasPropertyPaint> mPaint;
725};
726
727void SkiaCanvas::drawRoundRect(uirenderer::CanvasPropertyPrimitive* left,
728 uirenderer::CanvasPropertyPrimitive* top, uirenderer::CanvasPropertyPrimitive* right,
729 uirenderer::CanvasPropertyPrimitive* bottom, uirenderer::CanvasPropertyPrimitive* rx,
730 uirenderer::CanvasPropertyPrimitive* ry, uirenderer::CanvasPropertyPaint* paint) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400731 sk_sp<AnimatedRoundRect> drawable(
Derek Sollenberger6f485562015-07-30 10:00:39 -0400732 new AnimatedRoundRect(left, top, right, bottom, rx, ry, paint));
733 mCanvas->drawDrawable(drawable.get());
734}
735
736void SkiaCanvas::drawCircle(uirenderer::CanvasPropertyPrimitive* x, uirenderer::CanvasPropertyPrimitive* y,
737 uirenderer::CanvasPropertyPrimitive* radius, uirenderer::CanvasPropertyPaint* paint) {
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400738 sk_sp<AnimatedCircle> drawable(new AnimatedCircle(x, y, radius, paint));
Derek Sollenberger6f485562015-07-30 10:00:39 -0400739 mCanvas->drawDrawable(drawable.get());
740}
741
742// ----------------------------------------------------------------------------
743// Canvas draw operations: View System
744// ----------------------------------------------------------------------------
745
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400746void SkiaCanvas::drawLayer(uirenderer::DeferredLayerUpdater* layer) {
747 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw Layers");
748}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400749
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400750void SkiaCanvas::drawRenderNode(uirenderer::RenderNode* renderNode) {
751 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw RenderNodes");
752}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400753
John Reckcd1c3eb2016-04-14 10:38:54 -0700754void SkiaCanvas::callDrawGLFunction(Functor* functor,
Derek Sollenbergerc1908132016-07-15 10:28:16 -0400755 uirenderer::GlFunctorLifecycleListener* listener) {
756 LOG_ALWAYS_FATAL("SkiaCanvas can't directly draw GL Content");
757}
Derek Sollenberger6f485562015-07-30 10:00:39 -0400758
Derek Sollenberger8872b382014-06-23 14:13:53 -0400759} // namespace android