blob: 646401cb131a1f2f0efa2baccc4e690d2ee41165 [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 Sollenberger8872b382014-06-23 14:13:53 -040017#include "Canvas.h"
Derek Sollenberger8872b382014-06-23 14:13:53 -040018
John Reck849911a2015-01-20 07:51:14 -080019#include <SkCanvas.h>
20#include <SkClipStack.h>
21#include <SkDevice.h>
22#include <SkDeque.h>
23#include <SkDrawFilter.h>
24#include <SkGraphics.h>
John Reck849911a2015-01-20 07:51:14 -080025#include <SkShader.h>
26#include <SkTArray.h>
27#include <SkTemplates.h>
Derek Sollenberger8872b382014-06-23 14:13:53 -040028
Ben Wagner60126ef2015-08-07 12:13:48 -040029#include <memory>
30
Derek Sollenberger8872b382014-06-23 14:13:53 -040031namespace android {
32
33// Holds an SkCanvas reference plus additional native data.
34class SkiaCanvas : public Canvas {
35public:
John Reckc1b33d62015-04-22 09:04:45 -070036 explicit SkiaCanvas(const SkBitmap& bitmap);
Derek Sollenberger8872b382014-06-23 14:13:53 -040037
Leon Scroggins III18981292014-12-17 11:30:31 -050038 /**
39 * Create a new SkiaCanvas.
40 *
41 * @param canvas SkCanvas to handle calls made to this SkiaCanvas. Must
42 * not be NULL. This constructor will ref() the SkCanvas, and unref()
43 * it in its destructor.
44 */
45 explicit SkiaCanvas(SkCanvas* canvas) : mCanvas(canvas) {
Derek Sollenberger8872b382014-06-23 14:13:53 -040046 SkASSERT(canvas);
Leon Scroggins III18981292014-12-17 11:30:31 -050047 canvas->ref();
Derek Sollenberger8872b382014-06-23 14:13:53 -040048 }
49
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050050 virtual SkCanvas* asSkCanvas() override {
Derek Sollenberger8872b382014-06-23 14:13:53 -040051 return mCanvas.get();
52 }
53
John Reckc1b33d62015-04-22 09:04:45 -070054 virtual void setBitmap(const SkBitmap& bitmap) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040055
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050056 virtual bool isOpaque() override;
57 virtual int width() override;
58 virtual int height() override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040059
Derek Sollenberger6578a982015-07-13 13:24:29 -040060 virtual void setHighContrastText(bool highContrastText) override {
61 mHighContrastText = highContrastText;
62 }
63 virtual bool isHighContrastText() override { return mHighContrastText; }
64
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050065 virtual int getSaveCount() const override;
66 virtual int save(SkCanvas::SaveFlags flags) override;
67 virtual void restore() override;
68 virtual void restoreToCount(int saveCount) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040069
70 virtual int saveLayer(float left, float top, float right, float bottom,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050071 const SkPaint* paint, SkCanvas::SaveFlags flags) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040072 virtual int saveLayerAlpha(float left, float top, float right, float bottom,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050073 int alpha, SkCanvas::SaveFlags flags) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040074
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050075 virtual void getMatrix(SkMatrix* outMatrix) const override;
76 virtual void setMatrix(const SkMatrix& matrix) override;
Tom Hudsonac7b6d32015-06-30 11:26:13 -040077 virtual void setLocalMatrix(const SkMatrix& matrix) override { this->setMatrix(matrix); }
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050078 virtual void concat(const SkMatrix& matrix) override;
79 virtual void rotate(float degrees) override;
80 virtual void scale(float sx, float sy) override;
81 virtual void skew(float sx, float sy) override;
82 virtual void translate(float dx, float dy) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040083
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050084 virtual bool getClipBounds(SkRect* outRect) const override;
85 virtual bool quickRejectRect(float left, float top, float right, float bottom) const override;
86 virtual bool quickRejectPath(const SkPath& path) const override;
87 virtual bool clipRect(float left, float top, float right, float bottom,
88 SkRegion::Op op) override;
89 virtual bool clipPath(const SkPath* path, SkRegion::Op op) override;
90 virtual bool clipRegion(const SkRegion* region, SkRegion::Op op) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040091
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050092 virtual SkDrawFilter* getDrawFilter() override;
93 virtual void setDrawFilter(SkDrawFilter* drawFilter) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040094
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050095 virtual void drawColor(int color, SkXfermode::Mode mode) override;
96 virtual void drawPaint(const SkPaint& paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040097
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050098 virtual void drawPoint(float x, float y, const SkPaint& paint) override;
99 virtual void drawPoints(const float* points, int count, const SkPaint& paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400100 virtual void drawLine(float startX, float startY, float stopX, float stopY,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500101 const SkPaint& paint) override;
102 virtual void drawLines(const float* points, int count, const SkPaint& paint) override;
103 virtual void drawRect(float left, float top, float right, float bottom,
104 const SkPaint& paint) override;
Derek Sollenberger94394b32015-07-10 09:58:41 -0400105 virtual void drawRegion(const SkRegion& region, const SkPaint& paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400106 virtual void drawRoundRect(float left, float top, float right, float bottom,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500107 float rx, float ry, const SkPaint& paint) override;
108 virtual void drawCircle(float x, float y, float radius, const SkPaint& paint) override;
109 virtual void drawOval(float left, float top, float right, float bottom,
110 const SkPaint& paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400111 virtual void drawArc(float left, float top, float right, float bottom,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500112 float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) override;
113 virtual void drawPath(const SkPath& path, const SkPaint& paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400114 virtual void drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
115 const float* verts, const float* tex, const int* colors,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500116 const uint16_t* indices, int indexCount, const SkPaint& paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400117
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500118 virtual void drawBitmap(const SkBitmap& bitmap, float left, float top,
119 const SkPaint* paint) override;
120 virtual void drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix,
121 const SkPaint* paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400122 virtual void drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop,
123 float srcRight, float srcBottom, float dstLeft, float dstTop,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500124 float dstRight, float dstBottom, const SkPaint* paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400125 virtual void drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500126 const float* vertices, const int* colors, const SkPaint* paint) override;
Derek Sollenbergeredca3202015-07-10 13:56:39 -0400127 virtual void drawNinePatch(const SkBitmap& bitmap, const android::Res_png_9patch& chunk,
128 float dstLeft, float dstTop, float dstRight, float dstBottom,
129 const SkPaint* paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400130
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400131 virtual void drawText(const uint16_t* text, const float* positions, int count,
132 const SkPaint& paint, float x, float y,
Tom Hudson8dfaa492014-12-09 15:03:44 -0500133 float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500134 float totalAdvance) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400135 virtual void drawPosText(const uint16_t* text, const float* positions, int count,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500136 int posCount, const SkPaint& paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400137 virtual void drawTextOnPath(const uint16_t* glyphs, int count, const SkPath& path,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500138 float hOffset, float vOffset, const SkPaint& paint) override;
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400139
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500140 virtual bool drawTextAbsolutePos() const override { return true; }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400141
142private:
143 struct SaveRec {
144 int saveCount;
145 SkCanvas::SaveFlags saveFlags;
146 };
147
Derek Sollenberger6578a982015-07-13 13:24:29 -0400148 bool mHighContrastText = false;
149
Derek Sollenberger8872b382014-06-23 14:13:53 -0400150 void recordPartialSave(SkCanvas::SaveFlags flags);
151 void saveClipsForFrame(SkTArray<SkClipStack::Element>& clips, int frameSaveCount);
152 void applyClips(const SkTArray<SkClipStack::Element>& clips);
153
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400154 void drawPoints(const float* points, int count, const SkPaint& paint,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400155 SkCanvas::PointMode mode);
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400156 void drawTextDecorations(float x, float y, float length, const SkPaint& paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400157
158 SkAutoTUnref<SkCanvas> mCanvas;
Ben Wagner60126ef2015-08-07 12:13:48 -0400159 std::unique_ptr<SkDeque> mSaveStack; // lazily allocated, tracks partial saves.
Derek Sollenberger8872b382014-06-23 14:13:53 -0400160};
161
John Reckc1b33d62015-04-22 09:04:45 -0700162Canvas* Canvas::create_canvas(const SkBitmap& bitmap) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400163 return new SkiaCanvas(bitmap);
164}
165
166Canvas* Canvas::create_canvas(SkCanvas* skiaCanvas) {
167 return new SkiaCanvas(skiaCanvas);
168}
169
John Reckc1b33d62015-04-22 09:04:45 -0700170SkiaCanvas::SkiaCanvas(const SkBitmap& bitmap) {
171 mCanvas.reset(new SkCanvas(bitmap));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400172}
173
174// ----------------------------------------------------------------------------
175// Canvas state operations: Replace Bitmap
176// ----------------------------------------------------------------------------
177
178class ClipCopier : public SkCanvas::ClipVisitor {
179public:
180 ClipCopier(SkCanvas* dstCanvas) : m_dstCanvas(dstCanvas) {}
181
182 virtual void clipRect(const SkRect& rect, SkRegion::Op op, bool antialias) {
183 m_dstCanvas->clipRect(rect, op, antialias);
184 }
185 virtual void clipRRect(const SkRRect& rrect, SkRegion::Op op, bool antialias) {
186 m_dstCanvas->clipRRect(rrect, op, antialias);
187 }
188 virtual void clipPath(const SkPath& path, SkRegion::Op op, bool antialias) {
189 m_dstCanvas->clipPath(path, op, antialias);
190 }
191
192private:
193 SkCanvas* m_dstCanvas;
194};
195
John Reckc1b33d62015-04-22 09:04:45 -0700196void SkiaCanvas::setBitmap(const SkBitmap& bitmap) {
197 SkCanvas* newCanvas = new SkCanvas(bitmap);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400198 SkASSERT(newCanvas);
199
John Reckc1b33d62015-04-22 09:04:45 -0700200 if (!bitmap.isNull()) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400201 // Copy the canvas matrix & clip state.
202 newCanvas->setMatrix(mCanvas->getTotalMatrix());
203 if (NULL != mCanvas->getDevice() && NULL != newCanvas->getDevice()) {
204 ClipCopier copier(newCanvas);
205 mCanvas->replayClips(&copier);
206 }
207 }
208
209 // unrefs the existing canvas
210 mCanvas.reset(newCanvas);
211
212 // clean up the old save stack
213 mSaveStack.reset(NULL);
214}
215
216// ----------------------------------------------------------------------------
217// Canvas state operations
218// ----------------------------------------------------------------------------
219
220bool SkiaCanvas::isOpaque() {
221 return mCanvas->getDevice()->accessBitmap(false).isOpaque();
222}
223
224int SkiaCanvas::width() {
225 return mCanvas->getBaseLayerSize().width();
226}
227
228int SkiaCanvas::height() {
229 return mCanvas->getBaseLayerSize().height();
230}
231
232// ----------------------------------------------------------------------------
233// Canvas state operations: Save (layer)
234// ----------------------------------------------------------------------------
235
236int SkiaCanvas::getSaveCount() const {
237 return mCanvas->getSaveCount();
238}
239
240int SkiaCanvas::save(SkCanvas::SaveFlags flags) {
241 int count = mCanvas->save();
242 recordPartialSave(flags);
243 return count;
244}
245
246void SkiaCanvas::restore() {
247 const SaveRec* rec = (NULL == mSaveStack.get())
248 ? NULL
249 : static_cast<SaveRec*>(mSaveStack->back());
250 int currentSaveCount = mCanvas->getSaveCount() - 1;
251 SkASSERT(NULL == rec || currentSaveCount >= rec->saveCount);
252
253 if (NULL == rec || rec->saveCount != currentSaveCount) {
254 // Fast path - no record for this frame.
255 mCanvas->restore();
256 return;
257 }
258
259 bool preserveMatrix = !(rec->saveFlags & SkCanvas::kMatrix_SaveFlag);
260 bool preserveClip = !(rec->saveFlags & SkCanvas::kClip_SaveFlag);
261
262 SkMatrix savedMatrix;
263 if (preserveMatrix) {
264 savedMatrix = mCanvas->getTotalMatrix();
265 }
266
267 SkTArray<SkClipStack::Element> savedClips;
268 if (preserveClip) {
269 saveClipsForFrame(savedClips, currentSaveCount);
270 }
271
272 mCanvas->restore();
273
274 if (preserveMatrix) {
275 mCanvas->setMatrix(savedMatrix);
276 }
277
278 if (preserveClip && !savedClips.empty()) {
279 applyClips(savedClips);
280 }
281
282 mSaveStack->pop_back();
283}
284
285void SkiaCanvas::restoreToCount(int restoreCount) {
286 while (mCanvas->getSaveCount() > restoreCount) {
287 this->restore();
288 }
289}
290
291int SkiaCanvas::saveLayer(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400292 const SkPaint* paint, SkCanvas::SaveFlags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400293 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
294 int count = mCanvas->saveLayer(&bounds, paint, flags | SkCanvas::kMatrixClip_SaveFlag);
295 recordPartialSave(flags);
296 return count;
297}
298
299int SkiaCanvas::saveLayerAlpha(float left, float top, float right, float bottom,
300 int alpha, SkCanvas::SaveFlags flags) {
301 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
302 int count = mCanvas->saveLayerAlpha(&bounds, alpha, flags | SkCanvas::kMatrixClip_SaveFlag);
303 recordPartialSave(flags);
304 return count;
305}
306
307// ----------------------------------------------------------------------------
308// functions to emulate legacy SaveFlags (i.e. independent matrix/clip flags)
309// ----------------------------------------------------------------------------
310
311void SkiaCanvas::recordPartialSave(SkCanvas::SaveFlags flags) {
312 // A partial save is a save operation which doesn't capture the full canvas state.
313 // (either kMatrix_SaveFlags or kClip_SaveFlag is missing).
314
315 // Mask-out non canvas state bits.
316 flags = static_cast<SkCanvas::SaveFlags>(flags & SkCanvas::kMatrixClip_SaveFlag);
317
318 if (SkCanvas::kMatrixClip_SaveFlag == flags) {
319 // not a partial save.
320 return;
321 }
322
323 if (NULL == mSaveStack.get()) {
Ben Wagnerd1cbc162015-08-19 12:45:09 -0400324 mSaveStack.reset(new SkDeque(sizeof(struct SaveRec), 8));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400325 }
326
327 SaveRec* rec = static_cast<SaveRec*>(mSaveStack->push_back());
328 // Store the save counter in the SkClipStack domain.
329 // (0-based, equal to the number of save ops on the stack).
330 rec->saveCount = mCanvas->getSaveCount() - 1;
331 rec->saveFlags = flags;
332}
333
334void SkiaCanvas::saveClipsForFrame(SkTArray<SkClipStack::Element>& clips, int frameSaveCount) {
335 SkClipStack::Iter clipIterator(*mCanvas->getClipStack(),
336 SkClipStack::Iter::kTop_IterStart);
337 while (const SkClipStack::Element* elem = clipIterator.next()) {
338 if (elem->getSaveCount() < frameSaveCount) {
339 // done with the current frame.
340 break;
341 }
342 SkASSERT(elem->getSaveCount() == frameSaveCount);
343 clips.push_back(*elem);
344 }
345}
346
347void SkiaCanvas::applyClips(const SkTArray<SkClipStack::Element>& clips) {
348 ClipCopier clipCopier(mCanvas);
349
350 // The clip stack stores clips in device space.
351 SkMatrix origMatrix = mCanvas->getTotalMatrix();
352 mCanvas->resetMatrix();
353
354 // We pushed the clips in reverse order.
355 for (int i = clips.count() - 1; i >= 0; --i) {
356 clips[i].replay(&clipCopier);
357 }
358
359 mCanvas->setMatrix(origMatrix);
360}
361
362// ----------------------------------------------------------------------------
363// Canvas state operations: Matrix
364// ----------------------------------------------------------------------------
365
366void SkiaCanvas::getMatrix(SkMatrix* outMatrix) const {
367 *outMatrix = mCanvas->getTotalMatrix();
368}
369
370void SkiaCanvas::setMatrix(const SkMatrix& matrix) {
371 mCanvas->setMatrix(matrix);
372}
373
374void SkiaCanvas::concat(const SkMatrix& matrix) {
375 mCanvas->concat(matrix);
376}
377
378void SkiaCanvas::rotate(float degrees) {
379 mCanvas->rotate(degrees);
380}
381
382void SkiaCanvas::scale(float sx, float sy) {
383 mCanvas->scale(sx, sy);
384}
385
386void SkiaCanvas::skew(float sx, float sy) {
387 mCanvas->skew(sx, sy);
388}
389
390void SkiaCanvas::translate(float dx, float dy) {
391 mCanvas->translate(dx, dy);
392}
393
394// ----------------------------------------------------------------------------
395// Canvas state operations: Clips
396// ----------------------------------------------------------------------------
397
398// This function is a mirror of SkCanvas::getClipBounds except that it does
399// not outset the edge of the clip to account for anti-aliasing. There is
400// a skia bug to investigate pushing this logic into back into skia.
401// (see https://code.google.com/p/skia/issues/detail?id=1303)
402bool SkiaCanvas::getClipBounds(SkRect* outRect) const {
403 SkIRect ibounds;
404 if (!mCanvas->getClipDeviceBounds(&ibounds)) {
405 return false;
406 }
407
408 SkMatrix inverse;
409 // if we can't invert the CTM, we can't return local clip bounds
410 if (!mCanvas->getTotalMatrix().invert(&inverse)) {
411 if (outRect) {
412 outRect->setEmpty();
413 }
414 return false;
415 }
416
417 if (NULL != outRect) {
418 SkRect r = SkRect::Make(ibounds);
419 inverse.mapRect(outRect, r);
420 }
421 return true;
422}
423
424bool SkiaCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
425 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
426 return mCanvas->quickReject(bounds);
427}
428
429bool SkiaCanvas::quickRejectPath(const SkPath& path) const {
430 return mCanvas->quickReject(path);
431}
432
433bool SkiaCanvas::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
434 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
435 mCanvas->clipRect(rect, op);
Chris Craik5ec6a282015-06-23 15:42:12 -0700436 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400437}
438
439bool SkiaCanvas::clipPath(const SkPath* path, SkRegion::Op op) {
440 mCanvas->clipPath(*path, op);
Chris Craik5ec6a282015-06-23 15:42:12 -0700441 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400442}
443
444bool SkiaCanvas::clipRegion(const SkRegion* region, SkRegion::Op op) {
445 SkPath rgnPath;
446 if (region->getBoundaryPath(&rgnPath)) {
447 // The region is specified in device space.
448 SkMatrix savedMatrix = mCanvas->getTotalMatrix();
449 mCanvas->resetMatrix();
450 mCanvas->clipPath(rgnPath, op);
451 mCanvas->setMatrix(savedMatrix);
452 } else {
453 mCanvas->clipRect(SkRect::MakeEmpty(), op);
454 }
Chris Craik5ec6a282015-06-23 15:42:12 -0700455 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400456}
457
458// ----------------------------------------------------------------------------
459// Canvas state operations: Filters
460// ----------------------------------------------------------------------------
461
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400462SkDrawFilter* SkiaCanvas::getDrawFilter() {
463 return mCanvas->getDrawFilter();
464}
465
Derek Sollenberger8872b382014-06-23 14:13:53 -0400466void SkiaCanvas::setDrawFilter(SkDrawFilter* drawFilter) {
467 mCanvas->setDrawFilter(drawFilter);
468}
469
470// ----------------------------------------------------------------------------
471// Canvas draw operations
472// ----------------------------------------------------------------------------
473
474void SkiaCanvas::drawColor(int color, SkXfermode::Mode mode) {
475 mCanvas->drawColor(color, mode);
476}
477
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400478void SkiaCanvas::drawPaint(const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400479 mCanvas->drawPaint(paint);
480}
481
482// ----------------------------------------------------------------------------
483// Canvas draw operations: Geometry
484// ----------------------------------------------------------------------------
485
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400486void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400487 SkCanvas::PointMode mode) {
488 // convert the floats into SkPoints
489 count >>= 1; // now it is the number of points
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400490 std::unique_ptr<SkPoint[]> pts(new SkPoint[count]);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400491 for (int i = 0; i < count; i++) {
492 pts[i].set(points[0], points[1]);
493 points += 2;
494 }
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400495 mCanvas->drawPoints(mode, count, pts.get(), paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400496}
497
498
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400499void SkiaCanvas::drawPoint(float x, float y, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400500 mCanvas->drawPoint(x, y, paint);
501}
502
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400503void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400504 this->drawPoints(points, count, paint, SkCanvas::kPoints_PointMode);
505}
506
507void SkiaCanvas::drawLine(float startX, float startY, float stopX, float stopY,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400508 const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400509 mCanvas->drawLine(startX, startY, stopX, stopY, paint);
510}
511
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400512void SkiaCanvas::drawLines(const float* points, int count, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400513 this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode);
514}
515
516void SkiaCanvas::drawRect(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400517 const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400518 mCanvas->drawRectCoords(left, top, right, bottom, paint);
519
520}
521
Derek Sollenberger94394b32015-07-10 09:58:41 -0400522void SkiaCanvas::drawRegion(const SkRegion& region, const SkPaint& paint) {
523 SkRegion::Iterator it(region);
524 while (!it.done()) {
525 mCanvas->drawRect(SkRect::Make(it.rect()), paint);
526 it.next();
527 }
528}
529
Derek Sollenberger8872b382014-06-23 14:13:53 -0400530void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400531 float rx, float ry, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400532 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
533 mCanvas->drawRoundRect(rect, rx, ry, paint);
534}
535
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400536void SkiaCanvas::drawCircle(float x, float y, float radius, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400537 mCanvas->drawCircle(x, y, radius, paint);
538}
539
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400540void SkiaCanvas::drawOval(float left, float top, float right, float bottom, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400541 SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
542 mCanvas->drawOval(oval, paint);
543}
544
545void SkiaCanvas::drawArc(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400546 float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400547 SkRect arc = SkRect::MakeLTRB(left, top, right, bottom);
548 mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, paint);
549}
550
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400551void SkiaCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400552 mCanvas->drawPath(path, paint);
553}
554
555void SkiaCanvas::drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
556 const float* verts, const float* texs, const int* colors,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400557 const uint16_t* indices, int indexCount, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400558#ifndef SK_SCALAR_IS_FLOAT
559 SkDEBUGFAIL("SkScalar must be a float for these conversions to be valid");
560#endif
561 const int ptCount = vertexCount >> 1;
562 mCanvas->drawVertices(vertexMode, ptCount, (SkPoint*)verts, (SkPoint*)texs,
563 (SkColor*)colors, NULL, indices, indexCount, paint);
564}
565
566// ----------------------------------------------------------------------------
567// Canvas draw operations: Bitmaps
568// ----------------------------------------------------------------------------
569
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400570void SkiaCanvas::drawBitmap(const SkBitmap& bitmap, float left, float top, const SkPaint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400571 mCanvas->drawBitmap(bitmap, left, top, paint);
572}
573
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400574void SkiaCanvas::drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix, const SkPaint* paint) {
Mike Reed70ffbf92014-12-08 17:03:30 -0500575 SkAutoCanvasRestore acr(mCanvas, true);
576 mCanvas->concat(matrix);
577 mCanvas->drawBitmap(bitmap, 0, 0, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400578}
579
580void SkiaCanvas::drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop,
581 float srcRight, float srcBottom, float dstLeft, float dstTop,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400582 float dstRight, float dstBottom, const SkPaint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400583 SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
584 SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
585 mCanvas->drawBitmapRectToRect(bitmap, &srcRect, dstRect, paint);
586}
587
588void SkiaCanvas::drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400589 const float* vertices, const int* colors, const SkPaint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400590
591 const int ptCount = (meshWidth + 1) * (meshHeight + 1);
592 const int indexCount = meshWidth * meshHeight * 6;
593
594 /* Our temp storage holds 2 or 3 arrays.
595 texture points [ptCount * sizeof(SkPoint)]
596 optionally vertex points [ptCount * sizeof(SkPoint)] if we need a
597 copy to convert from float to fixed
598 indices [ptCount * sizeof(uint16_t)]
599 */
600 ssize_t storageSize = ptCount * sizeof(SkPoint); // texs[]
601 storageSize += indexCount * sizeof(uint16_t); // indices[]
602
603
604#ifndef SK_SCALAR_IS_FLOAT
605 SkDEBUGFAIL("SkScalar must be a float for these conversions to be valid");
606#endif
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400607 std::unique_ptr<char[]> storage(new char[storageSize]);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400608 SkPoint* texs = (SkPoint*)storage.get();
609 uint16_t* indices = (uint16_t*)(texs + ptCount);
610
611 // cons up texture coordinates and indices
612 {
613 const SkScalar w = SkIntToScalar(bitmap.width());
614 const SkScalar h = SkIntToScalar(bitmap.height());
615 const SkScalar dx = w / meshWidth;
616 const SkScalar dy = h / meshHeight;
617
618 SkPoint* texsPtr = texs;
619 SkScalar y = 0;
620 for (int i = 0; i <= meshHeight; i++) {
621 if (i == meshHeight) {
622 y = h; // to ensure numerically we hit h exactly
623 }
624 SkScalar x = 0;
625 for (int j = 0; j < meshWidth; j++) {
626 texsPtr->set(x, y);
627 texsPtr += 1;
628 x += dx;
629 }
630 texsPtr->set(w, y);
631 texsPtr += 1;
632 y += dy;
633 }
634 SkASSERT(texsPtr - texs == ptCount);
635 }
636
637 // cons up indices
638 {
639 uint16_t* indexPtr = indices;
640 int index = 0;
641 for (int i = 0; i < meshHeight; i++) {
642 for (int j = 0; j < meshWidth; j++) {
643 // lower-left triangle
644 *indexPtr++ = index;
645 *indexPtr++ = index + meshWidth + 1;
646 *indexPtr++ = index + meshWidth + 2;
647 // upper-right triangle
648 *indexPtr++ = index;
649 *indexPtr++ = index + meshWidth + 2;
650 *indexPtr++ = index + 1;
651 // bump to the next cell
652 index += 1;
653 }
654 // bump to the next row
655 index += 1;
656 }
657 SkASSERT(indexPtr - indices == indexCount);
658 SkASSERT((char*)indexPtr - (char*)storage.get() == storageSize);
659 }
660
661 // double-check that we have legal indices
662#ifdef SK_DEBUG
663 {
664 for (int i = 0; i < indexCount; i++) {
665 SkASSERT((unsigned)indices[i] < (unsigned)ptCount);
666 }
667 }
668#endif
669
670 // cons-up a shader for the bitmap
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400671 SkPaint tmpPaint;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400672 if (paint) {
673 tmpPaint = *paint;
674 }
675 SkShader* shader = SkShader::CreateBitmapShader(bitmap,
676 SkShader::kClamp_TileMode,
677 SkShader::kClamp_TileMode);
678 SkSafeUnref(tmpPaint.setShader(shader));
679
680 mCanvas->drawVertices(SkCanvas::kTriangles_VertexMode, ptCount, (SkPoint*)vertices,
681 texs, (const SkColor*)colors, NULL, indices,
682 indexCount, tmpPaint);
683}
684
Derek Sollenbergeredca3202015-07-10 13:56:39 -0400685void SkiaCanvas::drawNinePatch(const SkBitmap& bitmap, const Res_png_9patch& chunk,
686 float dstLeft, float dstTop, float dstRight, float dstBottom, const SkPaint* paint) {
687 SkRect bounds = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
688 NinePatch::Draw(mCanvas, bounds, bitmap, chunk, paint, nullptr);
689}
690
Derek Sollenberger8872b382014-06-23 14:13:53 -0400691// ----------------------------------------------------------------------------
692// Canvas draw operations: Text
693// ----------------------------------------------------------------------------
694
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400695void SkiaCanvas::drawText(const uint16_t* text, const float* positions, int count,
696 const SkPaint& paint, float x, float y,
Tom Hudson8dfaa492014-12-09 15:03:44 -0500697 float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
698 float totalAdvance) {
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400699 // Set align to left for drawing, as we don't want individual
700 // glyphs centered or right-aligned; the offset above takes
701 // care of all alignment.
702 SkPaint paintCopy(paint);
703 paintCopy.setTextAlign(SkPaint::kLeft_Align);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400704
Ben Wagnere3a40ea2015-08-19 15:49:37 -0400705 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats");
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400706 mCanvas->drawPosText(text, count << 1, reinterpret_cast<const SkPoint*>(positions), paintCopy);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400707}
708
709void SkiaCanvas::drawPosText(const uint16_t* text, const float* positions, int count, int posCount,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400710 const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400711 SkPoint* posPtr = posCount > 0 ? new SkPoint[posCount] : NULL;
712 int indx;
713 for (indx = 0; indx < posCount; indx++) {
714 posPtr[indx].fX = positions[indx << 1];
715 posPtr[indx].fY = positions[(indx << 1) + 1];
716 }
717
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400718 SkPaint paintCopy(paint);
719 paintCopy.setTextEncoding(SkPaint::kUTF16_TextEncoding);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400720 mCanvas->drawPosText(text, count, posPtr, paintCopy);
721
722 delete[] posPtr;
723}
724
725void SkiaCanvas::drawTextOnPath(const uint16_t* glyphs, int count, const SkPath& path,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400726 float hOffset, float vOffset, const SkPaint& paint) {
Tom Hudson34e79c12015-04-14 11:34:39 -0400727 mCanvas->drawTextOnPathHV(glyphs, count << 1, path, hOffset, vOffset, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400728}
729
730} // namespace android