blob: 644a4f305a2eff97d16ea7301b3a7d6367fbc891 [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 Reckc1b33d62015-04-22 09:04:45 -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 Reckc1b33d62015-04-22 09:04:45 -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;
Tom Hudsonac7b6d32015-06-30 11:26:13 -040070 virtual void setLocalMatrix(const SkMatrix& matrix) override { this->setMatrix(matrix); }
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050071 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
John Reckc1b33d62015-04-22 09:04:45 -0700149Canvas* Canvas::create_canvas(const SkBitmap& bitmap) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400150 return new SkiaCanvas(bitmap);
151}
152
153Canvas* Canvas::create_canvas(SkCanvas* skiaCanvas) {
154 return new SkiaCanvas(skiaCanvas);
155}
156
John Reckc1b33d62015-04-22 09:04:45 -0700157SkiaCanvas::SkiaCanvas(const SkBitmap& bitmap) {
158 mCanvas.reset(new SkCanvas(bitmap));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400159}
160
161// ----------------------------------------------------------------------------
162// Canvas state operations: Replace Bitmap
163// ----------------------------------------------------------------------------
164
165class ClipCopier : public SkCanvas::ClipVisitor {
166public:
167 ClipCopier(SkCanvas* dstCanvas) : m_dstCanvas(dstCanvas) {}
168
169 virtual void clipRect(const SkRect& rect, SkRegion::Op op, bool antialias) {
170 m_dstCanvas->clipRect(rect, op, antialias);
171 }
172 virtual void clipRRect(const SkRRect& rrect, SkRegion::Op op, bool antialias) {
173 m_dstCanvas->clipRRect(rrect, op, antialias);
174 }
175 virtual void clipPath(const SkPath& path, SkRegion::Op op, bool antialias) {
176 m_dstCanvas->clipPath(path, op, antialias);
177 }
178
179private:
180 SkCanvas* m_dstCanvas;
181};
182
John Reckc1b33d62015-04-22 09:04:45 -0700183void SkiaCanvas::setBitmap(const SkBitmap& bitmap) {
184 SkCanvas* newCanvas = new SkCanvas(bitmap);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400185 SkASSERT(newCanvas);
186
John Reckc1b33d62015-04-22 09:04:45 -0700187 if (!bitmap.isNull()) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400188 // Copy the canvas matrix & clip state.
189 newCanvas->setMatrix(mCanvas->getTotalMatrix());
190 if (NULL != mCanvas->getDevice() && NULL != newCanvas->getDevice()) {
191 ClipCopier copier(newCanvas);
192 mCanvas->replayClips(&copier);
193 }
194 }
195
196 // unrefs the existing canvas
197 mCanvas.reset(newCanvas);
198
199 // clean up the old save stack
200 mSaveStack.reset(NULL);
201}
202
203// ----------------------------------------------------------------------------
204// Canvas state operations
205// ----------------------------------------------------------------------------
206
207bool SkiaCanvas::isOpaque() {
208 return mCanvas->getDevice()->accessBitmap(false).isOpaque();
209}
210
211int SkiaCanvas::width() {
212 return mCanvas->getBaseLayerSize().width();
213}
214
215int SkiaCanvas::height() {
216 return mCanvas->getBaseLayerSize().height();
217}
218
219// ----------------------------------------------------------------------------
220// Canvas state operations: Save (layer)
221// ----------------------------------------------------------------------------
222
223int SkiaCanvas::getSaveCount() const {
224 return mCanvas->getSaveCount();
225}
226
227int SkiaCanvas::save(SkCanvas::SaveFlags flags) {
228 int count = mCanvas->save();
229 recordPartialSave(flags);
230 return count;
231}
232
233void SkiaCanvas::restore() {
234 const SaveRec* rec = (NULL == mSaveStack.get())
235 ? NULL
236 : static_cast<SaveRec*>(mSaveStack->back());
237 int currentSaveCount = mCanvas->getSaveCount() - 1;
238 SkASSERT(NULL == rec || currentSaveCount >= rec->saveCount);
239
240 if (NULL == rec || rec->saveCount != currentSaveCount) {
241 // Fast path - no record for this frame.
242 mCanvas->restore();
243 return;
244 }
245
246 bool preserveMatrix = !(rec->saveFlags & SkCanvas::kMatrix_SaveFlag);
247 bool preserveClip = !(rec->saveFlags & SkCanvas::kClip_SaveFlag);
248
249 SkMatrix savedMatrix;
250 if (preserveMatrix) {
251 savedMatrix = mCanvas->getTotalMatrix();
252 }
253
254 SkTArray<SkClipStack::Element> savedClips;
255 if (preserveClip) {
256 saveClipsForFrame(savedClips, currentSaveCount);
257 }
258
259 mCanvas->restore();
260
261 if (preserveMatrix) {
262 mCanvas->setMatrix(savedMatrix);
263 }
264
265 if (preserveClip && !savedClips.empty()) {
266 applyClips(savedClips);
267 }
268
269 mSaveStack->pop_back();
270}
271
272void SkiaCanvas::restoreToCount(int restoreCount) {
273 while (mCanvas->getSaveCount() > restoreCount) {
274 this->restore();
275 }
276}
277
278int SkiaCanvas::saveLayer(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400279 const SkPaint* paint, SkCanvas::SaveFlags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400280 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
281 int count = mCanvas->saveLayer(&bounds, paint, flags | SkCanvas::kMatrixClip_SaveFlag);
282 recordPartialSave(flags);
283 return count;
284}
285
286int SkiaCanvas::saveLayerAlpha(float left, float top, float right, float bottom,
287 int alpha, SkCanvas::SaveFlags flags) {
288 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
289 int count = mCanvas->saveLayerAlpha(&bounds, alpha, flags | SkCanvas::kMatrixClip_SaveFlag);
290 recordPartialSave(flags);
291 return count;
292}
293
294// ----------------------------------------------------------------------------
295// functions to emulate legacy SaveFlags (i.e. independent matrix/clip flags)
296// ----------------------------------------------------------------------------
297
298void SkiaCanvas::recordPartialSave(SkCanvas::SaveFlags flags) {
299 // A partial save is a save operation which doesn't capture the full canvas state.
300 // (either kMatrix_SaveFlags or kClip_SaveFlag is missing).
301
302 // Mask-out non canvas state bits.
303 flags = static_cast<SkCanvas::SaveFlags>(flags & SkCanvas::kMatrixClip_SaveFlag);
304
305 if (SkCanvas::kMatrixClip_SaveFlag == flags) {
306 // not a partial save.
307 return;
308 }
309
310 if (NULL == mSaveStack.get()) {
311 mSaveStack.reset(SkNEW_ARGS(SkDeque, (sizeof(struct SaveRec), 8)));
312 }
313
314 SaveRec* rec = static_cast<SaveRec*>(mSaveStack->push_back());
315 // Store the save counter in the SkClipStack domain.
316 // (0-based, equal to the number of save ops on the stack).
317 rec->saveCount = mCanvas->getSaveCount() - 1;
318 rec->saveFlags = flags;
319}
320
321void SkiaCanvas::saveClipsForFrame(SkTArray<SkClipStack::Element>& clips, int frameSaveCount) {
322 SkClipStack::Iter clipIterator(*mCanvas->getClipStack(),
323 SkClipStack::Iter::kTop_IterStart);
324 while (const SkClipStack::Element* elem = clipIterator.next()) {
325 if (elem->getSaveCount() < frameSaveCount) {
326 // done with the current frame.
327 break;
328 }
329 SkASSERT(elem->getSaveCount() == frameSaveCount);
330 clips.push_back(*elem);
331 }
332}
333
334void SkiaCanvas::applyClips(const SkTArray<SkClipStack::Element>& clips) {
335 ClipCopier clipCopier(mCanvas);
336
337 // The clip stack stores clips in device space.
338 SkMatrix origMatrix = mCanvas->getTotalMatrix();
339 mCanvas->resetMatrix();
340
341 // We pushed the clips in reverse order.
342 for (int i = clips.count() - 1; i >= 0; --i) {
343 clips[i].replay(&clipCopier);
344 }
345
346 mCanvas->setMatrix(origMatrix);
347}
348
349// ----------------------------------------------------------------------------
350// Canvas state operations: Matrix
351// ----------------------------------------------------------------------------
352
353void SkiaCanvas::getMatrix(SkMatrix* outMatrix) const {
354 *outMatrix = mCanvas->getTotalMatrix();
355}
356
357void SkiaCanvas::setMatrix(const SkMatrix& matrix) {
358 mCanvas->setMatrix(matrix);
359}
360
361void SkiaCanvas::concat(const SkMatrix& matrix) {
362 mCanvas->concat(matrix);
363}
364
365void SkiaCanvas::rotate(float degrees) {
366 mCanvas->rotate(degrees);
367}
368
369void SkiaCanvas::scale(float sx, float sy) {
370 mCanvas->scale(sx, sy);
371}
372
373void SkiaCanvas::skew(float sx, float sy) {
374 mCanvas->skew(sx, sy);
375}
376
377void SkiaCanvas::translate(float dx, float dy) {
378 mCanvas->translate(dx, dy);
379}
380
381// ----------------------------------------------------------------------------
382// Canvas state operations: Clips
383// ----------------------------------------------------------------------------
384
385// This function is a mirror of SkCanvas::getClipBounds except that it does
386// not outset the edge of the clip to account for anti-aliasing. There is
387// a skia bug to investigate pushing this logic into back into skia.
388// (see https://code.google.com/p/skia/issues/detail?id=1303)
389bool SkiaCanvas::getClipBounds(SkRect* outRect) const {
390 SkIRect ibounds;
391 if (!mCanvas->getClipDeviceBounds(&ibounds)) {
392 return false;
393 }
394
395 SkMatrix inverse;
396 // if we can't invert the CTM, we can't return local clip bounds
397 if (!mCanvas->getTotalMatrix().invert(&inverse)) {
398 if (outRect) {
399 outRect->setEmpty();
400 }
401 return false;
402 }
403
404 if (NULL != outRect) {
405 SkRect r = SkRect::Make(ibounds);
406 inverse.mapRect(outRect, r);
407 }
408 return true;
409}
410
411bool SkiaCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
412 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
413 return mCanvas->quickReject(bounds);
414}
415
416bool SkiaCanvas::quickRejectPath(const SkPath& path) const {
417 return mCanvas->quickReject(path);
418}
419
420bool SkiaCanvas::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
421 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
422 mCanvas->clipRect(rect, op);
Chris Craik5ec6a282015-06-23 15:42:12 -0700423 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400424}
425
426bool SkiaCanvas::clipPath(const SkPath* path, SkRegion::Op op) {
427 mCanvas->clipPath(*path, op);
Chris Craik5ec6a282015-06-23 15:42:12 -0700428 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400429}
430
431bool SkiaCanvas::clipRegion(const SkRegion* region, SkRegion::Op op) {
432 SkPath rgnPath;
433 if (region->getBoundaryPath(&rgnPath)) {
434 // The region is specified in device space.
435 SkMatrix savedMatrix = mCanvas->getTotalMatrix();
436 mCanvas->resetMatrix();
437 mCanvas->clipPath(rgnPath, op);
438 mCanvas->setMatrix(savedMatrix);
439 } else {
440 mCanvas->clipRect(SkRect::MakeEmpty(), op);
441 }
Chris Craik5ec6a282015-06-23 15:42:12 -0700442 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400443}
444
445// ----------------------------------------------------------------------------
446// Canvas state operations: Filters
447// ----------------------------------------------------------------------------
448
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400449SkDrawFilter* SkiaCanvas::getDrawFilter() {
450 return mCanvas->getDrawFilter();
451}
452
Derek Sollenberger8872b382014-06-23 14:13:53 -0400453void SkiaCanvas::setDrawFilter(SkDrawFilter* drawFilter) {
454 mCanvas->setDrawFilter(drawFilter);
455}
456
457// ----------------------------------------------------------------------------
458// Canvas draw operations
459// ----------------------------------------------------------------------------
460
461void SkiaCanvas::drawColor(int color, SkXfermode::Mode mode) {
462 mCanvas->drawColor(color, mode);
463}
464
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400465void SkiaCanvas::drawPaint(const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400466 mCanvas->drawPaint(paint);
467}
468
469// ----------------------------------------------------------------------------
470// Canvas draw operations: Geometry
471// ----------------------------------------------------------------------------
472
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400473void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400474 SkCanvas::PointMode mode) {
475 // convert the floats into SkPoints
476 count >>= 1; // now it is the number of points
477 SkAutoSTMalloc<32, SkPoint> storage(count);
478 SkPoint* pts = storage.get();
479 for (int i = 0; i < count; i++) {
480 pts[i].set(points[0], points[1]);
481 points += 2;
482 }
483 mCanvas->drawPoints(mode, count, pts, paint);
484}
485
486
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400487void SkiaCanvas::drawPoint(float x, float y, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400488 mCanvas->drawPoint(x, y, paint);
489}
490
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400491void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400492 this->drawPoints(points, count, paint, SkCanvas::kPoints_PointMode);
493}
494
495void SkiaCanvas::drawLine(float startX, float startY, float stopX, float stopY,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400496 const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400497 mCanvas->drawLine(startX, startY, stopX, stopY, paint);
498}
499
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400500void SkiaCanvas::drawLines(const float* points, int count, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400501 this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode);
502}
503
504void SkiaCanvas::drawRect(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400505 const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400506 mCanvas->drawRectCoords(left, top, right, bottom, paint);
507
508}
509
510void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400511 float rx, float ry, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400512 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
513 mCanvas->drawRoundRect(rect, rx, ry, paint);
514}
515
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400516void SkiaCanvas::drawCircle(float x, float y, float radius, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400517 mCanvas->drawCircle(x, y, radius, paint);
518}
519
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400520void SkiaCanvas::drawOval(float left, float top, float right, float bottom, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400521 SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
522 mCanvas->drawOval(oval, paint);
523}
524
525void SkiaCanvas::drawArc(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400526 float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400527 SkRect arc = SkRect::MakeLTRB(left, top, right, bottom);
528 mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, paint);
529}
530
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400531void SkiaCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400532 mCanvas->drawPath(path, paint);
533}
534
535void SkiaCanvas::drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
536 const float* verts, const float* texs, const int* colors,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400537 const uint16_t* indices, int indexCount, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400538#ifndef SK_SCALAR_IS_FLOAT
539 SkDEBUGFAIL("SkScalar must be a float for these conversions to be valid");
540#endif
541 const int ptCount = vertexCount >> 1;
542 mCanvas->drawVertices(vertexMode, ptCount, (SkPoint*)verts, (SkPoint*)texs,
543 (SkColor*)colors, NULL, indices, indexCount, paint);
544}
545
546// ----------------------------------------------------------------------------
547// Canvas draw operations: Bitmaps
548// ----------------------------------------------------------------------------
549
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400550void SkiaCanvas::drawBitmap(const SkBitmap& bitmap, float left, float top, const SkPaint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400551 mCanvas->drawBitmap(bitmap, left, top, paint);
552}
553
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400554void SkiaCanvas::drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix, const SkPaint* paint) {
Mike Reed70ffbf92014-12-08 17:03:30 -0500555 SkAutoCanvasRestore acr(mCanvas, true);
556 mCanvas->concat(matrix);
557 mCanvas->drawBitmap(bitmap, 0, 0, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400558}
559
560void SkiaCanvas::drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop,
561 float srcRight, float srcBottom, float dstLeft, float dstTop,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400562 float dstRight, float dstBottom, const SkPaint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400563 SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
564 SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
565 mCanvas->drawBitmapRectToRect(bitmap, &srcRect, dstRect, paint);
566}
567
568void SkiaCanvas::drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400569 const float* vertices, const int* colors, const SkPaint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400570
571 const int ptCount = (meshWidth + 1) * (meshHeight + 1);
572 const int indexCount = meshWidth * meshHeight * 6;
573
574 /* Our temp storage holds 2 or 3 arrays.
575 texture points [ptCount * sizeof(SkPoint)]
576 optionally vertex points [ptCount * sizeof(SkPoint)] if we need a
577 copy to convert from float to fixed
578 indices [ptCount * sizeof(uint16_t)]
579 */
580 ssize_t storageSize = ptCount * sizeof(SkPoint); // texs[]
581 storageSize += indexCount * sizeof(uint16_t); // indices[]
582
583
584#ifndef SK_SCALAR_IS_FLOAT
585 SkDEBUGFAIL("SkScalar must be a float for these conversions to be valid");
586#endif
587 SkAutoMalloc storage(storageSize);
588 SkPoint* texs = (SkPoint*)storage.get();
589 uint16_t* indices = (uint16_t*)(texs + ptCount);
590
591 // cons up texture coordinates and indices
592 {
593 const SkScalar w = SkIntToScalar(bitmap.width());
594 const SkScalar h = SkIntToScalar(bitmap.height());
595 const SkScalar dx = w / meshWidth;
596 const SkScalar dy = h / meshHeight;
597
598 SkPoint* texsPtr = texs;
599 SkScalar y = 0;
600 for (int i = 0; i <= meshHeight; i++) {
601 if (i == meshHeight) {
602 y = h; // to ensure numerically we hit h exactly
603 }
604 SkScalar x = 0;
605 for (int j = 0; j < meshWidth; j++) {
606 texsPtr->set(x, y);
607 texsPtr += 1;
608 x += dx;
609 }
610 texsPtr->set(w, y);
611 texsPtr += 1;
612 y += dy;
613 }
614 SkASSERT(texsPtr - texs == ptCount);
615 }
616
617 // cons up indices
618 {
619 uint16_t* indexPtr = indices;
620 int index = 0;
621 for (int i = 0; i < meshHeight; i++) {
622 for (int j = 0; j < meshWidth; j++) {
623 // lower-left triangle
624 *indexPtr++ = index;
625 *indexPtr++ = index + meshWidth + 1;
626 *indexPtr++ = index + meshWidth + 2;
627 // upper-right triangle
628 *indexPtr++ = index;
629 *indexPtr++ = index + meshWidth + 2;
630 *indexPtr++ = index + 1;
631 // bump to the next cell
632 index += 1;
633 }
634 // bump to the next row
635 index += 1;
636 }
637 SkASSERT(indexPtr - indices == indexCount);
638 SkASSERT((char*)indexPtr - (char*)storage.get() == storageSize);
639 }
640
641 // double-check that we have legal indices
642#ifdef SK_DEBUG
643 {
644 for (int i = 0; i < indexCount; i++) {
645 SkASSERT((unsigned)indices[i] < (unsigned)ptCount);
646 }
647 }
648#endif
649
650 // cons-up a shader for the bitmap
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400651 SkPaint tmpPaint;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400652 if (paint) {
653 tmpPaint = *paint;
654 }
655 SkShader* shader = SkShader::CreateBitmapShader(bitmap,
656 SkShader::kClamp_TileMode,
657 SkShader::kClamp_TileMode);
658 SkSafeUnref(tmpPaint.setShader(shader));
659
660 mCanvas->drawVertices(SkCanvas::kTriangles_VertexMode, ptCount, (SkPoint*)vertices,
661 texs, (const SkColor*)colors, NULL, indices,
662 indexCount, tmpPaint);
663}
664
665// ----------------------------------------------------------------------------
666// Canvas draw operations: Text
667// ----------------------------------------------------------------------------
668
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400669void SkiaCanvas::drawText(const uint16_t* text, const float* positions, int count,
670 const SkPaint& paint, float x, float y,
Tom Hudson8dfaa492014-12-09 15:03:44 -0500671 float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
672 float totalAdvance) {
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400673 // Set align to left for drawing, as we don't want individual
674 // glyphs centered or right-aligned; the offset above takes
675 // care of all alignment.
676 SkPaint paintCopy(paint);
677 paintCopy.setTextAlign(SkPaint::kLeft_Align);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400678
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400679 SK_COMPILE_ASSERT(sizeof(SkPoint) == sizeof(float)*2, SkPoint_is_no_longer_2_floats);
680 mCanvas->drawPosText(text, count << 1, reinterpret_cast<const SkPoint*>(positions), paintCopy);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400681}
682
683void SkiaCanvas::drawPosText(const uint16_t* text, const float* positions, int count, int posCount,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400684 const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400685 SkPoint* posPtr = posCount > 0 ? new SkPoint[posCount] : NULL;
686 int indx;
687 for (indx = 0; indx < posCount; indx++) {
688 posPtr[indx].fX = positions[indx << 1];
689 posPtr[indx].fY = positions[(indx << 1) + 1];
690 }
691
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400692 SkPaint paintCopy(paint);
693 paintCopy.setTextEncoding(SkPaint::kUTF16_TextEncoding);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400694 mCanvas->drawPosText(text, count, posPtr, paintCopy);
695
696 delete[] posPtr;
697}
698
699void SkiaCanvas::drawTextOnPath(const uint16_t* glyphs, int count, const SkPath& path,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400700 float hOffset, float vOffset, const SkPaint& paint) {
Tom Hudson34e79c12015-04-14 11:34:39 -0400701 mCanvas->drawTextOnPathHV(glyphs, count << 1, path, hOffset, vOffset, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400702}
703
704} // namespace android