blob: efbb7094c96aa24e4cfb1c689523809e0bc8a75d [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>
25#include <SkPorterDuff.h>
26#include <SkShader.h>
27#include <SkTArray.h>
28#include <SkTemplates.h>
Derek Sollenberger8872b382014-06-23 14:13:53 -040029
30namespace android {
31
32// Holds an SkCanvas reference plus additional native data.
33class SkiaCanvas : public Canvas {
34public:
Leon Scroggins III18981292014-12-17 11:30:31 -050035 explicit SkiaCanvas(SkBitmap* bitmap);
Derek Sollenberger8872b382014-06-23 14:13:53 -040036
Leon Scroggins III18981292014-12-17 11:30:31 -050037 /**
38 * Create a new SkiaCanvas.
39 *
40 * @param canvas SkCanvas to handle calls made to this SkiaCanvas. Must
41 * not be NULL. This constructor will ref() the SkCanvas, and unref()
42 * it in its destructor.
43 */
44 explicit SkiaCanvas(SkCanvas* canvas) : mCanvas(canvas) {
Derek Sollenberger8872b382014-06-23 14:13:53 -040045 SkASSERT(canvas);
Leon Scroggins III18981292014-12-17 11:30:31 -050046 canvas->ref();
Derek Sollenberger8872b382014-06-23 14:13:53 -040047 }
48
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050049 virtual SkCanvas* asSkCanvas() override {
Derek Sollenberger8872b382014-06-23 14:13:53 -040050 return mCanvas.get();
51 }
52
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050053 virtual void setBitmap(SkBitmap* bitmap, bool copyState) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040054
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050055 virtual bool isOpaque() override;
56 virtual int width() override;
57 virtual int height() override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040058
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050059 virtual int getSaveCount() const override;
60 virtual int save(SkCanvas::SaveFlags flags) override;
61 virtual void restore() override;
62 virtual void restoreToCount(int saveCount) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040063
64 virtual int saveLayer(float left, float top, float right, float bottom,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050065 const SkPaint* paint, SkCanvas::SaveFlags flags) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040066 virtual int saveLayerAlpha(float left, float top, float right, float bottom,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050067 int alpha, SkCanvas::SaveFlags flags) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040068
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050069 virtual void getMatrix(SkMatrix* outMatrix) const override;
70 virtual void setMatrix(const SkMatrix& matrix) override;
71 virtual void concat(const SkMatrix& matrix) override;
72 virtual void rotate(float degrees) override;
73 virtual void scale(float sx, float sy) override;
74 virtual void skew(float sx, float sy) override;
75 virtual void translate(float dx, float dy) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040076
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050077 virtual bool getClipBounds(SkRect* outRect) const override;
78 virtual bool quickRejectRect(float left, float top, float right, float bottom) const override;
79 virtual bool quickRejectPath(const SkPath& path) const override;
80 virtual bool clipRect(float left, float top, float right, float bottom,
81 SkRegion::Op op) override;
82 virtual bool clipPath(const SkPath* path, SkRegion::Op op) override;
83 virtual bool clipRegion(const SkRegion* region, SkRegion::Op op) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040084
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050085 virtual SkDrawFilter* getDrawFilter() override;
86 virtual void setDrawFilter(SkDrawFilter* drawFilter) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040087
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050088 virtual void drawColor(int color, SkXfermode::Mode mode) override;
89 virtual void drawPaint(const SkPaint& paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040090
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050091 virtual void drawPoint(float x, float y, const SkPaint& paint) override;
92 virtual void drawPoints(const float* points, int count, const SkPaint& paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040093 virtual void drawLine(float startX, float startY, float stopX, float stopY,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050094 const SkPaint& paint) override;
95 virtual void drawLines(const float* points, int count, const SkPaint& paint) override;
96 virtual void drawRect(float left, float top, float right, float bottom,
97 const SkPaint& paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040098 virtual void drawRoundRect(float left, float top, float right, float bottom,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050099 float rx, float ry, const SkPaint& paint) override;
100 virtual void drawCircle(float x, float y, float radius, const SkPaint& paint) override;
101 virtual void drawOval(float left, float top, float right, float bottom,
102 const SkPaint& paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400103 virtual void drawArc(float left, float top, float right, float bottom,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500104 float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) override;
105 virtual void drawPath(const SkPath& path, const SkPaint& paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400106 virtual void drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
107 const float* verts, const float* tex, const int* colors,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500108 const uint16_t* indices, int indexCount, const SkPaint& paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400109
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500110 virtual void drawBitmap(const SkBitmap& bitmap, float left, float top,
111 const SkPaint* paint) override;
112 virtual void drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix,
113 const SkPaint* paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400114 virtual void drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop,
115 float srcRight, float srcBottom, float dstLeft, float dstTop,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500116 float dstRight, float dstBottom, const SkPaint* paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400117 virtual void drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500118 const float* vertices, const int* colors, const SkPaint* paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400119
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400120 virtual void drawText(const uint16_t* text, const float* positions, int count,
121 const SkPaint& paint, float x, float y,
Tom Hudson8dfaa492014-12-09 15:03:44 -0500122 float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500123 float totalAdvance) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400124 virtual void drawPosText(const uint16_t* text, const float* positions, int count,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500125 int posCount, const SkPaint& paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400126 virtual void drawTextOnPath(const uint16_t* glyphs, int count, const SkPath& path,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500127 float hOffset, float vOffset, const SkPaint& paint) override;
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400128
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500129 virtual bool drawTextAbsolutePos() const override { return true; }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400130
131private:
132 struct SaveRec {
133 int saveCount;
134 SkCanvas::SaveFlags saveFlags;
135 };
136
137 void recordPartialSave(SkCanvas::SaveFlags flags);
138 void saveClipsForFrame(SkTArray<SkClipStack::Element>& clips, int frameSaveCount);
139 void applyClips(const SkTArray<SkClipStack::Element>& clips);
140
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400141 void drawPoints(const float* points, int count, const SkPaint& paint,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400142 SkCanvas::PointMode mode);
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400143 void drawTextDecorations(float x, float y, float length, const SkPaint& paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400144
145 SkAutoTUnref<SkCanvas> mCanvas;
146 SkAutoTDelete<SkDeque> mSaveStack; // lazily allocated, tracks partial saves.
147};
148
149// Construct an SkCanvas from the bitmap.
150static SkCanvas* createCanvas(SkBitmap* bitmap) {
151 if (bitmap) {
152 return SkNEW_ARGS(SkCanvas, (*bitmap));
153 }
154
155 // Create an empty bitmap device to prevent callers from crashing
156 // if they attempt to draw into this canvas.
157 SkBitmap emptyBitmap;
158 return new SkCanvas(emptyBitmap);
159}
160
161Canvas* Canvas::create_canvas(SkBitmap* bitmap) {
162 return new SkiaCanvas(bitmap);
163}
164
165Canvas* Canvas::create_canvas(SkCanvas* skiaCanvas) {
166 return new SkiaCanvas(skiaCanvas);
167}
168
169SkiaCanvas::SkiaCanvas(SkBitmap* bitmap) {
170 mCanvas.reset(createCanvas(bitmap));
171}
172
173// ----------------------------------------------------------------------------
174// Canvas state operations: Replace Bitmap
175// ----------------------------------------------------------------------------
176
177class ClipCopier : public SkCanvas::ClipVisitor {
178public:
179 ClipCopier(SkCanvas* dstCanvas) : m_dstCanvas(dstCanvas) {}
180
181 virtual void clipRect(const SkRect& rect, SkRegion::Op op, bool antialias) {
182 m_dstCanvas->clipRect(rect, op, antialias);
183 }
184 virtual void clipRRect(const SkRRect& rrect, SkRegion::Op op, bool antialias) {
185 m_dstCanvas->clipRRect(rrect, op, antialias);
186 }
187 virtual void clipPath(const SkPath& path, SkRegion::Op op, bool antialias) {
188 m_dstCanvas->clipPath(path, op, antialias);
189 }
190
191private:
192 SkCanvas* m_dstCanvas;
193};
194
195void SkiaCanvas::setBitmap(SkBitmap* bitmap, bool copyState) {
196 SkCanvas* newCanvas = createCanvas(bitmap);
197 SkASSERT(newCanvas);
198
199 if (copyState) {
200 // Copy the canvas matrix & clip state.
201 newCanvas->setMatrix(mCanvas->getTotalMatrix());
202 if (NULL != mCanvas->getDevice() && NULL != newCanvas->getDevice()) {
203 ClipCopier copier(newCanvas);
204 mCanvas->replayClips(&copier);
205 }
206 }
207
208 // unrefs the existing canvas
209 mCanvas.reset(newCanvas);
210
211 // clean up the old save stack
212 mSaveStack.reset(NULL);
213}
214
215// ----------------------------------------------------------------------------
216// Canvas state operations
217// ----------------------------------------------------------------------------
218
219bool SkiaCanvas::isOpaque() {
220 return mCanvas->getDevice()->accessBitmap(false).isOpaque();
221}
222
223int SkiaCanvas::width() {
224 return mCanvas->getBaseLayerSize().width();
225}
226
227int SkiaCanvas::height() {
228 return mCanvas->getBaseLayerSize().height();
229}
230
231// ----------------------------------------------------------------------------
232// Canvas state operations: Save (layer)
233// ----------------------------------------------------------------------------
234
235int SkiaCanvas::getSaveCount() const {
236 return mCanvas->getSaveCount();
237}
238
239int SkiaCanvas::save(SkCanvas::SaveFlags flags) {
240 int count = mCanvas->save();
241 recordPartialSave(flags);
242 return count;
243}
244
245void SkiaCanvas::restore() {
246 const SaveRec* rec = (NULL == mSaveStack.get())
247 ? NULL
248 : static_cast<SaveRec*>(mSaveStack->back());
249 int currentSaveCount = mCanvas->getSaveCount() - 1;
250 SkASSERT(NULL == rec || currentSaveCount >= rec->saveCount);
251
252 if (NULL == rec || rec->saveCount != currentSaveCount) {
253 // Fast path - no record for this frame.
254 mCanvas->restore();
255 return;
256 }
257
258 bool preserveMatrix = !(rec->saveFlags & SkCanvas::kMatrix_SaveFlag);
259 bool preserveClip = !(rec->saveFlags & SkCanvas::kClip_SaveFlag);
260
261 SkMatrix savedMatrix;
262 if (preserveMatrix) {
263 savedMatrix = mCanvas->getTotalMatrix();
264 }
265
266 SkTArray<SkClipStack::Element> savedClips;
267 if (preserveClip) {
268 saveClipsForFrame(savedClips, currentSaveCount);
269 }
270
271 mCanvas->restore();
272
273 if (preserveMatrix) {
274 mCanvas->setMatrix(savedMatrix);
275 }
276
277 if (preserveClip && !savedClips.empty()) {
278 applyClips(savedClips);
279 }
280
281 mSaveStack->pop_back();
282}
283
284void SkiaCanvas::restoreToCount(int restoreCount) {
285 while (mCanvas->getSaveCount() > restoreCount) {
286 this->restore();
287 }
288}
289
290int SkiaCanvas::saveLayer(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400291 const SkPaint* paint, SkCanvas::SaveFlags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400292 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
293 int count = mCanvas->saveLayer(&bounds, paint, flags | SkCanvas::kMatrixClip_SaveFlag);
294 recordPartialSave(flags);
295 return count;
296}
297
298int SkiaCanvas::saveLayerAlpha(float left, float top, float right, float bottom,
299 int alpha, SkCanvas::SaveFlags flags) {
300 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
301 int count = mCanvas->saveLayerAlpha(&bounds, alpha, flags | SkCanvas::kMatrixClip_SaveFlag);
302 recordPartialSave(flags);
303 return count;
304}
305
306// ----------------------------------------------------------------------------
307// functions to emulate legacy SaveFlags (i.e. independent matrix/clip flags)
308// ----------------------------------------------------------------------------
309
310void SkiaCanvas::recordPartialSave(SkCanvas::SaveFlags flags) {
311 // A partial save is a save operation which doesn't capture the full canvas state.
312 // (either kMatrix_SaveFlags or kClip_SaveFlag is missing).
313
314 // Mask-out non canvas state bits.
315 flags = static_cast<SkCanvas::SaveFlags>(flags & SkCanvas::kMatrixClip_SaveFlag);
316
317 if (SkCanvas::kMatrixClip_SaveFlag == flags) {
318 // not a partial save.
319 return;
320 }
321
322 if (NULL == mSaveStack.get()) {
323 mSaveStack.reset(SkNEW_ARGS(SkDeque, (sizeof(struct SaveRec), 8)));
324 }
325
326 SaveRec* rec = static_cast<SaveRec*>(mSaveStack->push_back());
327 // Store the save counter in the SkClipStack domain.
328 // (0-based, equal to the number of save ops on the stack).
329 rec->saveCount = mCanvas->getSaveCount() - 1;
330 rec->saveFlags = flags;
331}
332
333void SkiaCanvas::saveClipsForFrame(SkTArray<SkClipStack::Element>& clips, int frameSaveCount) {
334 SkClipStack::Iter clipIterator(*mCanvas->getClipStack(),
335 SkClipStack::Iter::kTop_IterStart);
336 while (const SkClipStack::Element* elem = clipIterator.next()) {
337 if (elem->getSaveCount() < frameSaveCount) {
338 // done with the current frame.
339 break;
340 }
341 SkASSERT(elem->getSaveCount() == frameSaveCount);
342 clips.push_back(*elem);
343 }
344}
345
346void SkiaCanvas::applyClips(const SkTArray<SkClipStack::Element>& clips) {
347 ClipCopier clipCopier(mCanvas);
348
349 // The clip stack stores clips in device space.
350 SkMatrix origMatrix = mCanvas->getTotalMatrix();
351 mCanvas->resetMatrix();
352
353 // We pushed the clips in reverse order.
354 for (int i = clips.count() - 1; i >= 0; --i) {
355 clips[i].replay(&clipCopier);
356 }
357
358 mCanvas->setMatrix(origMatrix);
359}
360
361// ----------------------------------------------------------------------------
362// Canvas state operations: Matrix
363// ----------------------------------------------------------------------------
364
365void SkiaCanvas::getMatrix(SkMatrix* outMatrix) const {
366 *outMatrix = mCanvas->getTotalMatrix();
367}
368
369void SkiaCanvas::setMatrix(const SkMatrix& matrix) {
370 mCanvas->setMatrix(matrix);
371}
372
373void SkiaCanvas::concat(const SkMatrix& matrix) {
374 mCanvas->concat(matrix);
375}
376
377void SkiaCanvas::rotate(float degrees) {
378 mCanvas->rotate(degrees);
379}
380
381void SkiaCanvas::scale(float sx, float sy) {
382 mCanvas->scale(sx, sy);
383}
384
385void SkiaCanvas::skew(float sx, float sy) {
386 mCanvas->skew(sx, sy);
387}
388
389void SkiaCanvas::translate(float dx, float dy) {
390 mCanvas->translate(dx, dy);
391}
392
393// ----------------------------------------------------------------------------
394// Canvas state operations: Clips
395// ----------------------------------------------------------------------------
396
397// This function is a mirror of SkCanvas::getClipBounds except that it does
398// not outset the edge of the clip to account for anti-aliasing. There is
399// a skia bug to investigate pushing this logic into back into skia.
400// (see https://code.google.com/p/skia/issues/detail?id=1303)
401bool SkiaCanvas::getClipBounds(SkRect* outRect) const {
402 SkIRect ibounds;
403 if (!mCanvas->getClipDeviceBounds(&ibounds)) {
404 return false;
405 }
406
407 SkMatrix inverse;
408 // if we can't invert the CTM, we can't return local clip bounds
409 if (!mCanvas->getTotalMatrix().invert(&inverse)) {
410 if (outRect) {
411 outRect->setEmpty();
412 }
413 return false;
414 }
415
416 if (NULL != outRect) {
417 SkRect r = SkRect::Make(ibounds);
418 inverse.mapRect(outRect, r);
419 }
420 return true;
421}
422
423bool SkiaCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
424 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
425 return mCanvas->quickReject(bounds);
426}
427
428bool SkiaCanvas::quickRejectPath(const SkPath& path) const {
429 return mCanvas->quickReject(path);
430}
431
432bool SkiaCanvas::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
433 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
434 mCanvas->clipRect(rect, op);
435 return mCanvas->isClipEmpty();
436}
437
438bool SkiaCanvas::clipPath(const SkPath* path, SkRegion::Op op) {
439 mCanvas->clipPath(*path, op);
440 return mCanvas->isClipEmpty();
441}
442
443bool SkiaCanvas::clipRegion(const SkRegion* region, SkRegion::Op op) {
444 SkPath rgnPath;
445 if (region->getBoundaryPath(&rgnPath)) {
446 // The region is specified in device space.
447 SkMatrix savedMatrix = mCanvas->getTotalMatrix();
448 mCanvas->resetMatrix();
449 mCanvas->clipPath(rgnPath, op);
450 mCanvas->setMatrix(savedMatrix);
451 } else {
452 mCanvas->clipRect(SkRect::MakeEmpty(), op);
453 }
454 return mCanvas->isClipEmpty();
455}
456
457// ----------------------------------------------------------------------------
458// Canvas state operations: Filters
459// ----------------------------------------------------------------------------
460
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400461SkDrawFilter* SkiaCanvas::getDrawFilter() {
462 return mCanvas->getDrawFilter();
463}
464
Derek Sollenberger8872b382014-06-23 14:13:53 -0400465void SkiaCanvas::setDrawFilter(SkDrawFilter* drawFilter) {
466 mCanvas->setDrawFilter(drawFilter);
467}
468
469// ----------------------------------------------------------------------------
470// Canvas draw operations
471// ----------------------------------------------------------------------------
472
473void SkiaCanvas::drawColor(int color, SkXfermode::Mode mode) {
474 mCanvas->drawColor(color, mode);
475}
476
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400477void SkiaCanvas::drawPaint(const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400478 mCanvas->drawPaint(paint);
479}
480
481// ----------------------------------------------------------------------------
482// Canvas draw operations: Geometry
483// ----------------------------------------------------------------------------
484
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400485void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400486 SkCanvas::PointMode mode) {
487 // convert the floats into SkPoints
488 count >>= 1; // now it is the number of points
489 SkAutoSTMalloc<32, SkPoint> storage(count);
490 SkPoint* pts = storage.get();
491 for (int i = 0; i < count; i++) {
492 pts[i].set(points[0], points[1]);
493 points += 2;
494 }
495 mCanvas->drawPoints(mode, count, pts, paint);
496}
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
522void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400523 float rx, float ry, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400524 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
525 mCanvas->drawRoundRect(rect, rx, ry, paint);
526}
527
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400528void SkiaCanvas::drawCircle(float x, float y, float radius, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400529 mCanvas->drawCircle(x, y, radius, paint);
530}
531
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400532void SkiaCanvas::drawOval(float left, float top, float right, float bottom, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400533 SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
534 mCanvas->drawOval(oval, paint);
535}
536
537void SkiaCanvas::drawArc(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400538 float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400539 SkRect arc = SkRect::MakeLTRB(left, top, right, bottom);
540 mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, paint);
541}
542
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400543void SkiaCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400544 mCanvas->drawPath(path, paint);
545}
546
547void SkiaCanvas::drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
548 const float* verts, const float* texs, const int* colors,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400549 const uint16_t* indices, int indexCount, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400550#ifndef SK_SCALAR_IS_FLOAT
551 SkDEBUGFAIL("SkScalar must be a float for these conversions to be valid");
552#endif
553 const int ptCount = vertexCount >> 1;
554 mCanvas->drawVertices(vertexMode, ptCount, (SkPoint*)verts, (SkPoint*)texs,
555 (SkColor*)colors, NULL, indices, indexCount, paint);
556}
557
558// ----------------------------------------------------------------------------
559// Canvas draw operations: Bitmaps
560// ----------------------------------------------------------------------------
561
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400562void SkiaCanvas::drawBitmap(const SkBitmap& bitmap, float left, float top, const SkPaint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400563 mCanvas->drawBitmap(bitmap, left, top, paint);
564}
565
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400566void SkiaCanvas::drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix, const SkPaint* paint) {
Mike Reed70ffbf92014-12-08 17:03:30 -0500567 SkAutoCanvasRestore acr(mCanvas, true);
568 mCanvas->concat(matrix);
569 mCanvas->drawBitmap(bitmap, 0, 0, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400570}
571
572void SkiaCanvas::drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop,
573 float srcRight, float srcBottom, float dstLeft, float dstTop,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400574 float dstRight, float dstBottom, const SkPaint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400575 SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
576 SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
577 mCanvas->drawBitmapRectToRect(bitmap, &srcRect, dstRect, paint);
578}
579
580void SkiaCanvas::drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400581 const float* vertices, const int* colors, const SkPaint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400582
583 const int ptCount = (meshWidth + 1) * (meshHeight + 1);
584 const int indexCount = meshWidth * meshHeight * 6;
585
586 /* Our temp storage holds 2 or 3 arrays.
587 texture points [ptCount * sizeof(SkPoint)]
588 optionally vertex points [ptCount * sizeof(SkPoint)] if we need a
589 copy to convert from float to fixed
590 indices [ptCount * sizeof(uint16_t)]
591 */
592 ssize_t storageSize = ptCount * sizeof(SkPoint); // texs[]
593 storageSize += indexCount * sizeof(uint16_t); // indices[]
594
595
596#ifndef SK_SCALAR_IS_FLOAT
597 SkDEBUGFAIL("SkScalar must be a float for these conversions to be valid");
598#endif
599 SkAutoMalloc storage(storageSize);
600 SkPoint* texs = (SkPoint*)storage.get();
601 uint16_t* indices = (uint16_t*)(texs + ptCount);
602
603 // cons up texture coordinates and indices
604 {
605 const SkScalar w = SkIntToScalar(bitmap.width());
606 const SkScalar h = SkIntToScalar(bitmap.height());
607 const SkScalar dx = w / meshWidth;
608 const SkScalar dy = h / meshHeight;
609
610 SkPoint* texsPtr = texs;
611 SkScalar y = 0;
612 for (int i = 0; i <= meshHeight; i++) {
613 if (i == meshHeight) {
614 y = h; // to ensure numerically we hit h exactly
615 }
616 SkScalar x = 0;
617 for (int j = 0; j < meshWidth; j++) {
618 texsPtr->set(x, y);
619 texsPtr += 1;
620 x += dx;
621 }
622 texsPtr->set(w, y);
623 texsPtr += 1;
624 y += dy;
625 }
626 SkASSERT(texsPtr - texs == ptCount);
627 }
628
629 // cons up indices
630 {
631 uint16_t* indexPtr = indices;
632 int index = 0;
633 for (int i = 0; i < meshHeight; i++) {
634 for (int j = 0; j < meshWidth; j++) {
635 // lower-left triangle
636 *indexPtr++ = index;
637 *indexPtr++ = index + meshWidth + 1;
638 *indexPtr++ = index + meshWidth + 2;
639 // upper-right triangle
640 *indexPtr++ = index;
641 *indexPtr++ = index + meshWidth + 2;
642 *indexPtr++ = index + 1;
643 // bump to the next cell
644 index += 1;
645 }
646 // bump to the next row
647 index += 1;
648 }
649 SkASSERT(indexPtr - indices == indexCount);
650 SkASSERT((char*)indexPtr - (char*)storage.get() == storageSize);
651 }
652
653 // double-check that we have legal indices
654#ifdef SK_DEBUG
655 {
656 for (int i = 0; i < indexCount; i++) {
657 SkASSERT((unsigned)indices[i] < (unsigned)ptCount);
658 }
659 }
660#endif
661
662 // cons-up a shader for the bitmap
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400663 SkPaint tmpPaint;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400664 if (paint) {
665 tmpPaint = *paint;
666 }
667 SkShader* shader = SkShader::CreateBitmapShader(bitmap,
668 SkShader::kClamp_TileMode,
669 SkShader::kClamp_TileMode);
670 SkSafeUnref(tmpPaint.setShader(shader));
671
672 mCanvas->drawVertices(SkCanvas::kTriangles_VertexMode, ptCount, (SkPoint*)vertices,
673 texs, (const SkColor*)colors, NULL, indices,
674 indexCount, tmpPaint);
675}
676
677// ----------------------------------------------------------------------------
678// Canvas draw operations: Text
679// ----------------------------------------------------------------------------
680
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400681void SkiaCanvas::drawText(const uint16_t* text, const float* positions, int count,
682 const SkPaint& paint, float x, float y,
Tom Hudson8dfaa492014-12-09 15:03:44 -0500683 float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
684 float totalAdvance) {
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400685 // Set align to left for drawing, as we don't want individual
686 // glyphs centered or right-aligned; the offset above takes
687 // care of all alignment.
688 SkPaint paintCopy(paint);
689 paintCopy.setTextAlign(SkPaint::kLeft_Align);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400690
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400691 SK_COMPILE_ASSERT(sizeof(SkPoint) == sizeof(float)*2, SkPoint_is_no_longer_2_floats);
692 mCanvas->drawPosText(text, count << 1, reinterpret_cast<const SkPoint*>(positions), paintCopy);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400693}
694
695void SkiaCanvas::drawPosText(const uint16_t* text, const float* positions, int count, int posCount,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400696 const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400697 SkPoint* posPtr = posCount > 0 ? new SkPoint[posCount] : NULL;
698 int indx;
699 for (indx = 0; indx < posCount; indx++) {
700 posPtr[indx].fX = positions[indx << 1];
701 posPtr[indx].fY = positions[(indx << 1) + 1];
702 }
703
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400704 SkPaint paintCopy(paint);
705 paintCopy.setTextEncoding(SkPaint::kUTF16_TextEncoding);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400706 mCanvas->drawPosText(text, count, posPtr, paintCopy);
707
708 delete[] posPtr;
709}
710
711void SkiaCanvas::drawTextOnPath(const uint16_t* glyphs, int count, const SkPath& path,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400712 float hOffset, float vOffset, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400713 mCanvas->drawTextOnPathHV(glyphs, count, path, hOffset, vOffset, paint);
714}
715
716} // namespace android