blob: a3230655469933ec0f36ae3c9f6553663f28315f [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
29namespace android {
30
31// Holds an SkCanvas reference plus additional native data.
32class SkiaCanvas : public Canvas {
33public:
John Reckc294d122015-04-13 15:20:29 -070034 explicit SkiaCanvas(const SkBitmap& bitmap);
Derek Sollenberger8872b382014-06-23 14:13:53 -040035
Leon Scroggins III18981292014-12-17 11:30:31 -050036 /**
37 * Create a new SkiaCanvas.
38 *
39 * @param canvas SkCanvas to handle calls made to this SkiaCanvas. Must
40 * not be NULL. This constructor will ref() the SkCanvas, and unref()
41 * it in its destructor.
42 */
43 explicit SkiaCanvas(SkCanvas* canvas) : mCanvas(canvas) {
Derek Sollenberger8872b382014-06-23 14:13:53 -040044 SkASSERT(canvas);
Leon Scroggins III18981292014-12-17 11:30:31 -050045 canvas->ref();
Derek Sollenberger8872b382014-06-23 14:13:53 -040046 }
47
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050048 virtual SkCanvas* asSkCanvas() override {
Derek Sollenberger8872b382014-06-23 14:13:53 -040049 return mCanvas.get();
50 }
51
John Reckc294d122015-04-13 15:20:29 -070052 virtual void setBitmap(const SkBitmap& bitmap) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040053
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050054 virtual bool isOpaque() override;
55 virtual int width() override;
56 virtual int height() override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040057
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050058 virtual int getSaveCount() const override;
59 virtual int save(SkCanvas::SaveFlags flags) override;
60 virtual void restore() override;
61 virtual void restoreToCount(int saveCount) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040062
63 virtual int saveLayer(float left, float top, float right, float bottom,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050064 const SkPaint* paint, SkCanvas::SaveFlags flags) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040065 virtual int saveLayerAlpha(float left, float top, float right, float bottom,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050066 int alpha, SkCanvas::SaveFlags flags) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040067
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050068 virtual void getMatrix(SkMatrix* outMatrix) const override;
69 virtual void setMatrix(const SkMatrix& matrix) override;
70 virtual void concat(const SkMatrix& matrix) override;
71 virtual void rotate(float degrees) override;
72 virtual void scale(float sx, float sy) override;
73 virtual void skew(float sx, float sy) override;
74 virtual void translate(float dx, float dy) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040075
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050076 virtual bool getClipBounds(SkRect* outRect) const override;
77 virtual bool quickRejectRect(float left, float top, float right, float bottom) const override;
78 virtual bool quickRejectPath(const SkPath& path) const override;
79 virtual bool clipRect(float left, float top, float right, float bottom,
80 SkRegion::Op op) override;
81 virtual bool clipPath(const SkPath* path, SkRegion::Op op) override;
82 virtual bool clipRegion(const SkRegion* region, SkRegion::Op op) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040083
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050084 virtual SkDrawFilter* getDrawFilter() override;
85 virtual void setDrawFilter(SkDrawFilter* drawFilter) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040086
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050087 virtual void drawColor(int color, SkXfermode::Mode mode) override;
88 virtual void drawPaint(const SkPaint& paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040089
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050090 virtual void drawPoint(float x, float y, const SkPaint& paint) override;
91 virtual void drawPoints(const float* points, int count, const SkPaint& paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040092 virtual void drawLine(float startX, float startY, float stopX, float stopY,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050093 const SkPaint& paint) override;
94 virtual void drawLines(const float* points, int count, const SkPaint& paint) override;
95 virtual void drawRect(float left, float top, float right, float bottom,
96 const SkPaint& paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040097 virtual void drawRoundRect(float left, float top, float right, float bottom,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050098 float rx, float ry, const SkPaint& paint) override;
99 virtual void drawCircle(float x, float y, float radius, const SkPaint& paint) override;
100 virtual void drawOval(float left, float top, float right, float bottom,
101 const SkPaint& paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400102 virtual void drawArc(float left, float top, float right, float bottom,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500103 float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) override;
104 virtual void drawPath(const SkPath& path, const SkPaint& paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400105 virtual void drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
106 const float* verts, const float* tex, const int* colors,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500107 const uint16_t* indices, int indexCount, const SkPaint& paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400108
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500109 virtual void drawBitmap(const SkBitmap& bitmap, float left, float top,
110 const SkPaint* paint) override;
111 virtual void drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix,
112 const SkPaint* paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400113 virtual void drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop,
114 float srcRight, float srcBottom, float dstLeft, float dstTop,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500115 float dstRight, float dstBottom, const SkPaint* paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400116 virtual void drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500117 const float* vertices, const int* colors, const SkPaint* paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400118
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400119 virtual void drawText(const uint16_t* text, const float* positions, int count,
120 const SkPaint& paint, float x, float y,
Tom Hudson8dfaa492014-12-09 15:03:44 -0500121 float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500122 float totalAdvance) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400123 virtual void drawPosText(const uint16_t* text, const float* positions, int count,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500124 int posCount, const SkPaint& paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400125 virtual void drawTextOnPath(const uint16_t* glyphs, int count, const SkPath& path,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500126 float hOffset, float vOffset, const SkPaint& paint) override;
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400127
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500128 virtual bool drawTextAbsolutePos() const override { return true; }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400129
130private:
131 struct SaveRec {
132 int saveCount;
133 SkCanvas::SaveFlags saveFlags;
134 };
135
136 void recordPartialSave(SkCanvas::SaveFlags flags);
137 void saveClipsForFrame(SkTArray<SkClipStack::Element>& clips, int frameSaveCount);
138 void applyClips(const SkTArray<SkClipStack::Element>& clips);
139
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400140 void drawPoints(const float* points, int count, const SkPaint& paint,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400141 SkCanvas::PointMode mode);
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400142 void drawTextDecorations(float x, float y, float length, const SkPaint& paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400143
144 SkAutoTUnref<SkCanvas> mCanvas;
145 SkAutoTDelete<SkDeque> mSaveStack; // lazily allocated, tracks partial saves.
146};
147
John Reckc294d122015-04-13 15:20:29 -0700148Canvas* Canvas::create_canvas(const SkBitmap& bitmap) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400149 return new SkiaCanvas(bitmap);
150}
151
152Canvas* Canvas::create_canvas(SkCanvas* skiaCanvas) {
153 return new SkiaCanvas(skiaCanvas);
154}
155
John Reckc294d122015-04-13 15:20:29 -0700156SkiaCanvas::SkiaCanvas(const SkBitmap& bitmap) {
157 mCanvas.reset(new SkCanvas(bitmap));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400158}
159
160// ----------------------------------------------------------------------------
161// Canvas state operations: Replace Bitmap
162// ----------------------------------------------------------------------------
163
164class ClipCopier : public SkCanvas::ClipVisitor {
165public:
166 ClipCopier(SkCanvas* dstCanvas) : m_dstCanvas(dstCanvas) {}
167
168 virtual void clipRect(const SkRect& rect, SkRegion::Op op, bool antialias) {
169 m_dstCanvas->clipRect(rect, op, antialias);
170 }
171 virtual void clipRRect(const SkRRect& rrect, SkRegion::Op op, bool antialias) {
172 m_dstCanvas->clipRRect(rrect, op, antialias);
173 }
174 virtual void clipPath(const SkPath& path, SkRegion::Op op, bool antialias) {
175 m_dstCanvas->clipPath(path, op, antialias);
176 }
177
178private:
179 SkCanvas* m_dstCanvas;
180};
181
John Reckc294d122015-04-13 15:20:29 -0700182void SkiaCanvas::setBitmap(const SkBitmap& bitmap) {
183 SkCanvas* newCanvas = new SkCanvas(bitmap);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400184 SkASSERT(newCanvas);
185
John Reckc294d122015-04-13 15:20:29 -0700186 if (!bitmap.isNull()) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400187 // Copy the canvas matrix & clip state.
188 newCanvas->setMatrix(mCanvas->getTotalMatrix());
189 if (NULL != mCanvas->getDevice() && NULL != newCanvas->getDevice()) {
190 ClipCopier copier(newCanvas);
191 mCanvas->replayClips(&copier);
192 }
193 }
194
195 // unrefs the existing canvas
196 mCanvas.reset(newCanvas);
197
198 // clean up the old save stack
199 mSaveStack.reset(NULL);
200}
201
202// ----------------------------------------------------------------------------
203// Canvas state operations
204// ----------------------------------------------------------------------------
205
206bool SkiaCanvas::isOpaque() {
207 return mCanvas->getDevice()->accessBitmap(false).isOpaque();
208}
209
210int SkiaCanvas::width() {
211 return mCanvas->getBaseLayerSize().width();
212}
213
214int SkiaCanvas::height() {
215 return mCanvas->getBaseLayerSize().height();
216}
217
218// ----------------------------------------------------------------------------
219// Canvas state operations: Save (layer)
220// ----------------------------------------------------------------------------
221
222int SkiaCanvas::getSaveCount() const {
223 return mCanvas->getSaveCount();
224}
225
226int SkiaCanvas::save(SkCanvas::SaveFlags flags) {
227 int count = mCanvas->save();
228 recordPartialSave(flags);
229 return count;
230}
231
232void SkiaCanvas::restore() {
233 const SaveRec* rec = (NULL == mSaveStack.get())
234 ? NULL
235 : static_cast<SaveRec*>(mSaveStack->back());
236 int currentSaveCount = mCanvas->getSaveCount() - 1;
237 SkASSERT(NULL == rec || currentSaveCount >= rec->saveCount);
238
239 if (NULL == rec || rec->saveCount != currentSaveCount) {
240 // Fast path - no record for this frame.
241 mCanvas->restore();
242 return;
243 }
244
245 bool preserveMatrix = !(rec->saveFlags & SkCanvas::kMatrix_SaveFlag);
246 bool preserveClip = !(rec->saveFlags & SkCanvas::kClip_SaveFlag);
247
248 SkMatrix savedMatrix;
249 if (preserveMatrix) {
250 savedMatrix = mCanvas->getTotalMatrix();
251 }
252
253 SkTArray<SkClipStack::Element> savedClips;
254 if (preserveClip) {
255 saveClipsForFrame(savedClips, currentSaveCount);
256 }
257
258 mCanvas->restore();
259
260 if (preserveMatrix) {
261 mCanvas->setMatrix(savedMatrix);
262 }
263
264 if (preserveClip && !savedClips.empty()) {
265 applyClips(savedClips);
266 }
267
268 mSaveStack->pop_back();
269}
270
271void SkiaCanvas::restoreToCount(int restoreCount) {
272 while (mCanvas->getSaveCount() > restoreCount) {
273 this->restore();
274 }
275}
276
277int SkiaCanvas::saveLayer(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400278 const SkPaint* paint, SkCanvas::SaveFlags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400279 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
280 int count = mCanvas->saveLayer(&bounds, paint, flags | SkCanvas::kMatrixClip_SaveFlag);
281 recordPartialSave(flags);
282 return count;
283}
284
285int SkiaCanvas::saveLayerAlpha(float left, float top, float right, float bottom,
286 int alpha, SkCanvas::SaveFlags flags) {
287 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
288 int count = mCanvas->saveLayerAlpha(&bounds, alpha, flags | SkCanvas::kMatrixClip_SaveFlag);
289 recordPartialSave(flags);
290 return count;
291}
292
293// ----------------------------------------------------------------------------
294// functions to emulate legacy SaveFlags (i.e. independent matrix/clip flags)
295// ----------------------------------------------------------------------------
296
297void SkiaCanvas::recordPartialSave(SkCanvas::SaveFlags flags) {
298 // A partial save is a save operation which doesn't capture the full canvas state.
299 // (either kMatrix_SaveFlags or kClip_SaveFlag is missing).
300
301 // Mask-out non canvas state bits.
302 flags = static_cast<SkCanvas::SaveFlags>(flags & SkCanvas::kMatrixClip_SaveFlag);
303
304 if (SkCanvas::kMatrixClip_SaveFlag == flags) {
305 // not a partial save.
306 return;
307 }
308
309 if (NULL == mSaveStack.get()) {
310 mSaveStack.reset(SkNEW_ARGS(SkDeque, (sizeof(struct SaveRec), 8)));
311 }
312
313 SaveRec* rec = static_cast<SaveRec*>(mSaveStack->push_back());
314 // Store the save counter in the SkClipStack domain.
315 // (0-based, equal to the number of save ops on the stack).
316 rec->saveCount = mCanvas->getSaveCount() - 1;
317 rec->saveFlags = flags;
318}
319
320void SkiaCanvas::saveClipsForFrame(SkTArray<SkClipStack::Element>& clips, int frameSaveCount) {
321 SkClipStack::Iter clipIterator(*mCanvas->getClipStack(),
322 SkClipStack::Iter::kTop_IterStart);
323 while (const SkClipStack::Element* elem = clipIterator.next()) {
324 if (elem->getSaveCount() < frameSaveCount) {
325 // done with the current frame.
326 break;
327 }
328 SkASSERT(elem->getSaveCount() == frameSaveCount);
329 clips.push_back(*elem);
330 }
331}
332
333void SkiaCanvas::applyClips(const SkTArray<SkClipStack::Element>& clips) {
334 ClipCopier clipCopier(mCanvas);
335
336 // The clip stack stores clips in device space.
337 SkMatrix origMatrix = mCanvas->getTotalMatrix();
338 mCanvas->resetMatrix();
339
340 // We pushed the clips in reverse order.
341 for (int i = clips.count() - 1; i >= 0; --i) {
342 clips[i].replay(&clipCopier);
343 }
344
345 mCanvas->setMatrix(origMatrix);
346}
347
348// ----------------------------------------------------------------------------
349// Canvas state operations: Matrix
350// ----------------------------------------------------------------------------
351
352void SkiaCanvas::getMatrix(SkMatrix* outMatrix) const {
353 *outMatrix = mCanvas->getTotalMatrix();
354}
355
356void SkiaCanvas::setMatrix(const SkMatrix& matrix) {
357 mCanvas->setMatrix(matrix);
358}
359
360void SkiaCanvas::concat(const SkMatrix& matrix) {
361 mCanvas->concat(matrix);
362}
363
364void SkiaCanvas::rotate(float degrees) {
365 mCanvas->rotate(degrees);
366}
367
368void SkiaCanvas::scale(float sx, float sy) {
369 mCanvas->scale(sx, sy);
370}
371
372void SkiaCanvas::skew(float sx, float sy) {
373 mCanvas->skew(sx, sy);
374}
375
376void SkiaCanvas::translate(float dx, float dy) {
377 mCanvas->translate(dx, dy);
378}
379
380// ----------------------------------------------------------------------------
381// Canvas state operations: Clips
382// ----------------------------------------------------------------------------
383
384// This function is a mirror of SkCanvas::getClipBounds except that it does
385// not outset the edge of the clip to account for anti-aliasing. There is
386// a skia bug to investigate pushing this logic into back into skia.
387// (see https://code.google.com/p/skia/issues/detail?id=1303)
388bool SkiaCanvas::getClipBounds(SkRect* outRect) const {
389 SkIRect ibounds;
390 if (!mCanvas->getClipDeviceBounds(&ibounds)) {
391 return false;
392 }
393
394 SkMatrix inverse;
395 // if we can't invert the CTM, we can't return local clip bounds
396 if (!mCanvas->getTotalMatrix().invert(&inverse)) {
397 if (outRect) {
398 outRect->setEmpty();
399 }
400 return false;
401 }
402
403 if (NULL != outRect) {
404 SkRect r = SkRect::Make(ibounds);
405 inverse.mapRect(outRect, r);
406 }
407 return true;
408}
409
410bool SkiaCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
411 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
412 return mCanvas->quickReject(bounds);
413}
414
415bool SkiaCanvas::quickRejectPath(const SkPath& path) const {
416 return mCanvas->quickReject(path);
417}
418
419bool SkiaCanvas::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
420 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
421 mCanvas->clipRect(rect, op);
422 return mCanvas->isClipEmpty();
423}
424
425bool SkiaCanvas::clipPath(const SkPath* path, SkRegion::Op op) {
426 mCanvas->clipPath(*path, op);
427 return mCanvas->isClipEmpty();
428}
429
430bool SkiaCanvas::clipRegion(const SkRegion* region, SkRegion::Op op) {
431 SkPath rgnPath;
432 if (region->getBoundaryPath(&rgnPath)) {
433 // The region is specified in device space.
434 SkMatrix savedMatrix = mCanvas->getTotalMatrix();
435 mCanvas->resetMatrix();
436 mCanvas->clipPath(rgnPath, op);
437 mCanvas->setMatrix(savedMatrix);
438 } else {
439 mCanvas->clipRect(SkRect::MakeEmpty(), op);
440 }
441 return mCanvas->isClipEmpty();
442}
443
444// ----------------------------------------------------------------------------
445// Canvas state operations: Filters
446// ----------------------------------------------------------------------------
447
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400448SkDrawFilter* SkiaCanvas::getDrawFilter() {
449 return mCanvas->getDrawFilter();
450}
451
Derek Sollenberger8872b382014-06-23 14:13:53 -0400452void SkiaCanvas::setDrawFilter(SkDrawFilter* drawFilter) {
453 mCanvas->setDrawFilter(drawFilter);
454}
455
456// ----------------------------------------------------------------------------
457// Canvas draw operations
458// ----------------------------------------------------------------------------
459
460void SkiaCanvas::drawColor(int color, SkXfermode::Mode mode) {
461 mCanvas->drawColor(color, mode);
462}
463
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400464void SkiaCanvas::drawPaint(const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400465 mCanvas->drawPaint(paint);
466}
467
468// ----------------------------------------------------------------------------
469// Canvas draw operations: Geometry
470// ----------------------------------------------------------------------------
471
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400472void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400473 SkCanvas::PointMode mode) {
474 // convert the floats into SkPoints
475 count >>= 1; // now it is the number of points
476 SkAutoSTMalloc<32, SkPoint> storage(count);
477 SkPoint* pts = storage.get();
478 for (int i = 0; i < count; i++) {
479 pts[i].set(points[0], points[1]);
480 points += 2;
481 }
482 mCanvas->drawPoints(mode, count, pts, paint);
483}
484
485
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400486void SkiaCanvas::drawPoint(float x, float y, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400487 mCanvas->drawPoint(x, y, paint);
488}
489
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400490void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400491 this->drawPoints(points, count, paint, SkCanvas::kPoints_PointMode);
492}
493
494void SkiaCanvas::drawLine(float startX, float startY, float stopX, float stopY,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400495 const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400496 mCanvas->drawLine(startX, startY, stopX, stopY, paint);
497}
498
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400499void SkiaCanvas::drawLines(const float* points, int count, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400500 this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode);
501}
502
503void SkiaCanvas::drawRect(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400504 const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400505 mCanvas->drawRectCoords(left, top, right, bottom, paint);
506
507}
508
509void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400510 float rx, float ry, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400511 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
512 mCanvas->drawRoundRect(rect, rx, ry, paint);
513}
514
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400515void SkiaCanvas::drawCircle(float x, float y, float radius, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400516 mCanvas->drawCircle(x, y, radius, paint);
517}
518
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400519void SkiaCanvas::drawOval(float left, float top, float right, float bottom, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400520 SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
521 mCanvas->drawOval(oval, paint);
522}
523
524void SkiaCanvas::drawArc(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400525 float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400526 SkRect arc = SkRect::MakeLTRB(left, top, right, bottom);
527 mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, paint);
528}
529
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400530void SkiaCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400531 mCanvas->drawPath(path, paint);
532}
533
534void SkiaCanvas::drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
535 const float* verts, const float* texs, const int* colors,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400536 const uint16_t* indices, int indexCount, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400537#ifndef SK_SCALAR_IS_FLOAT
538 SkDEBUGFAIL("SkScalar must be a float for these conversions to be valid");
539#endif
540 const int ptCount = vertexCount >> 1;
541 mCanvas->drawVertices(vertexMode, ptCount, (SkPoint*)verts, (SkPoint*)texs,
542 (SkColor*)colors, NULL, indices, indexCount, paint);
543}
544
545// ----------------------------------------------------------------------------
546// Canvas draw operations: Bitmaps
547// ----------------------------------------------------------------------------
548
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400549void SkiaCanvas::drawBitmap(const SkBitmap& bitmap, float left, float top, const SkPaint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400550 mCanvas->drawBitmap(bitmap, left, top, paint);
551}
552
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400553void SkiaCanvas::drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix, const SkPaint* paint) {
Mike Reed70ffbf92014-12-08 17:03:30 -0500554 SkAutoCanvasRestore acr(mCanvas, true);
555 mCanvas->concat(matrix);
556 mCanvas->drawBitmap(bitmap, 0, 0, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400557}
558
559void SkiaCanvas::drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop,
560 float srcRight, float srcBottom, float dstLeft, float dstTop,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400561 float dstRight, float dstBottom, const SkPaint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400562 SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
563 SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
564 mCanvas->drawBitmapRectToRect(bitmap, &srcRect, dstRect, paint);
565}
566
567void SkiaCanvas::drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400568 const float* vertices, const int* colors, const SkPaint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400569
570 const int ptCount = (meshWidth + 1) * (meshHeight + 1);
571 const int indexCount = meshWidth * meshHeight * 6;
572
573 /* Our temp storage holds 2 or 3 arrays.
574 texture points [ptCount * sizeof(SkPoint)]
575 optionally vertex points [ptCount * sizeof(SkPoint)] if we need a
576 copy to convert from float to fixed
577 indices [ptCount * sizeof(uint16_t)]
578 */
579 ssize_t storageSize = ptCount * sizeof(SkPoint); // texs[]
580 storageSize += indexCount * sizeof(uint16_t); // indices[]
581
582
583#ifndef SK_SCALAR_IS_FLOAT
584 SkDEBUGFAIL("SkScalar must be a float for these conversions to be valid");
585#endif
586 SkAutoMalloc storage(storageSize);
587 SkPoint* texs = (SkPoint*)storage.get();
588 uint16_t* indices = (uint16_t*)(texs + ptCount);
589
590 // cons up texture coordinates and indices
591 {
592 const SkScalar w = SkIntToScalar(bitmap.width());
593 const SkScalar h = SkIntToScalar(bitmap.height());
594 const SkScalar dx = w / meshWidth;
595 const SkScalar dy = h / meshHeight;
596
597 SkPoint* texsPtr = texs;
598 SkScalar y = 0;
599 for (int i = 0; i <= meshHeight; i++) {
600 if (i == meshHeight) {
601 y = h; // to ensure numerically we hit h exactly
602 }
603 SkScalar x = 0;
604 for (int j = 0; j < meshWidth; j++) {
605 texsPtr->set(x, y);
606 texsPtr += 1;
607 x += dx;
608 }
609 texsPtr->set(w, y);
610 texsPtr += 1;
611 y += dy;
612 }
613 SkASSERT(texsPtr - texs == ptCount);
614 }
615
616 // cons up indices
617 {
618 uint16_t* indexPtr = indices;
619 int index = 0;
620 for (int i = 0; i < meshHeight; i++) {
621 for (int j = 0; j < meshWidth; j++) {
622 // lower-left triangle
623 *indexPtr++ = index;
624 *indexPtr++ = index + meshWidth + 1;
625 *indexPtr++ = index + meshWidth + 2;
626 // upper-right triangle
627 *indexPtr++ = index;
628 *indexPtr++ = index + meshWidth + 2;
629 *indexPtr++ = index + 1;
630 // bump to the next cell
631 index += 1;
632 }
633 // bump to the next row
634 index += 1;
635 }
636 SkASSERT(indexPtr - indices == indexCount);
637 SkASSERT((char*)indexPtr - (char*)storage.get() == storageSize);
638 }
639
640 // double-check that we have legal indices
641#ifdef SK_DEBUG
642 {
643 for (int i = 0; i < indexCount; i++) {
644 SkASSERT((unsigned)indices[i] < (unsigned)ptCount);
645 }
646 }
647#endif
648
649 // cons-up a shader for the bitmap
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400650 SkPaint tmpPaint;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400651 if (paint) {
652 tmpPaint = *paint;
653 }
654 SkShader* shader = SkShader::CreateBitmapShader(bitmap,
655 SkShader::kClamp_TileMode,
656 SkShader::kClamp_TileMode);
657 SkSafeUnref(tmpPaint.setShader(shader));
658
659 mCanvas->drawVertices(SkCanvas::kTriangles_VertexMode, ptCount, (SkPoint*)vertices,
660 texs, (const SkColor*)colors, NULL, indices,
661 indexCount, tmpPaint);
662}
663
664// ----------------------------------------------------------------------------
665// Canvas draw operations: Text
666// ----------------------------------------------------------------------------
667
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400668void SkiaCanvas::drawText(const uint16_t* text, const float* positions, int count,
669 const SkPaint& paint, float x, float y,
Tom Hudson8dfaa492014-12-09 15:03:44 -0500670 float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
671 float totalAdvance) {
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400672 // Set align to left for drawing, as we don't want individual
673 // glyphs centered or right-aligned; the offset above takes
674 // care of all alignment.
675 SkPaint paintCopy(paint);
676 paintCopy.setTextAlign(SkPaint::kLeft_Align);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400677
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400678 SK_COMPILE_ASSERT(sizeof(SkPoint) == sizeof(float)*2, SkPoint_is_no_longer_2_floats);
679 mCanvas->drawPosText(text, count << 1, reinterpret_cast<const SkPoint*>(positions), paintCopy);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400680}
681
682void SkiaCanvas::drawPosText(const uint16_t* text, const float* positions, int count, int posCount,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400683 const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400684 SkPoint* posPtr = posCount > 0 ? new SkPoint[posCount] : NULL;
685 int indx;
686 for (indx = 0; indx < posCount; indx++) {
687 posPtr[indx].fX = positions[indx << 1];
688 posPtr[indx].fY = positions[(indx << 1) + 1];
689 }
690
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400691 SkPaint paintCopy(paint);
692 paintCopy.setTextEncoding(SkPaint::kUTF16_TextEncoding);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400693 mCanvas->drawPosText(text, count, posPtr, paintCopy);
694
695 delete[] posPtr;
696}
697
698void SkiaCanvas::drawTextOnPath(const uint16_t* glyphs, int count, const SkPath& path,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400699 float hOffset, float vOffset, const SkPaint& paint) {
Tom Hudson34e79c12015-04-14 11:34:39 -0400700 mCanvas->drawTextOnPathHV(glyphs, count << 1, path, hOffset, vOffset, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400701}
702
703} // namespace android