blob: b2d1fecdaa4cd6fa57615e0658de951380decfc4 [file] [log] [blame]
Derek Sollenberger8872b382014-06-23 14:13:53 -04001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Derek Sollenberger8872b382014-06-23 14:13:53 -040017#include "Canvas.h"
Derek Sollenberger8872b382014-06-23 14:13:53 -040018
John Reck849911a2015-01-20 07:51:14 -080019#include <SkCanvas.h>
20#include <SkClipStack.h>
21#include <SkDevice.h>
22#include <SkDeque.h>
23#include <SkDrawFilter.h>
24#include <SkGraphics.h>
John Reck849911a2015-01-20 07:51:14 -080025#include <SkShader.h>
26#include <SkTArray.h>
27#include <SkTemplates.h>
Derek Sollenberger8872b382014-06-23 14:13:53 -040028
Ben Wagner60126ef2015-08-07 12:13:48 -040029#include <memory>
30
Derek Sollenberger8872b382014-06-23 14:13:53 -040031namespace android {
32
33// Holds an SkCanvas reference plus additional native data.
34class SkiaCanvas : public Canvas {
35public:
John Reckc1b33d62015-04-22 09:04:45 -070036 explicit SkiaCanvas(const SkBitmap& bitmap);
Derek Sollenberger8872b382014-06-23 14:13:53 -040037
Leon Scroggins III18981292014-12-17 11:30:31 -050038 /**
39 * Create a new SkiaCanvas.
40 *
41 * @param canvas SkCanvas to handle calls made to this SkiaCanvas. Must
42 * not be NULL. This constructor will ref() the SkCanvas, and unref()
43 * it in its destructor.
44 */
45 explicit SkiaCanvas(SkCanvas* canvas) : mCanvas(canvas) {
Derek Sollenberger8872b382014-06-23 14:13:53 -040046 SkASSERT(canvas);
Leon Scroggins III18981292014-12-17 11:30:31 -050047 canvas->ref();
Derek Sollenberger8872b382014-06-23 14:13:53 -040048 }
49
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050050 virtual SkCanvas* asSkCanvas() override {
Derek Sollenberger8872b382014-06-23 14:13:53 -040051 return mCanvas.get();
52 }
53
John Reckc1b33d62015-04-22 09:04:45 -070054 virtual void setBitmap(const SkBitmap& bitmap) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040055
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050056 virtual bool isOpaque() override;
57 virtual int width() override;
58 virtual int height() override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040059
Derek Sollenberger6578a982015-07-13 13:24:29 -040060 virtual void setHighContrastText(bool highContrastText) override {
61 mHighContrastText = highContrastText;
62 }
63 virtual bool isHighContrastText() override { return mHighContrastText; }
64
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050065 virtual int getSaveCount() const override;
66 virtual int save(SkCanvas::SaveFlags flags) override;
67 virtual void restore() override;
68 virtual void restoreToCount(int saveCount) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040069
70 virtual int saveLayer(float left, float top, float right, float bottom,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050071 const SkPaint* paint, SkCanvas::SaveFlags flags) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040072 virtual int saveLayerAlpha(float left, float top, float right, float bottom,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050073 int alpha, SkCanvas::SaveFlags flags) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040074
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050075 virtual void getMatrix(SkMatrix* outMatrix) const override;
76 virtual void setMatrix(const SkMatrix& matrix) override;
Tom Hudsonac7b6d32015-06-30 11:26:13 -040077 virtual void setLocalMatrix(const SkMatrix& matrix) override { this->setMatrix(matrix); }
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050078 virtual void concat(const SkMatrix& matrix) override;
79 virtual void rotate(float degrees) override;
80 virtual void scale(float sx, float sy) override;
81 virtual void skew(float sx, float sy) override;
82 virtual void translate(float dx, float dy) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040083
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050084 virtual bool getClipBounds(SkRect* outRect) const override;
85 virtual bool quickRejectRect(float left, float top, float right, float bottom) const override;
86 virtual bool quickRejectPath(const SkPath& path) const override;
87 virtual bool clipRect(float left, float top, float right, float bottom,
88 SkRegion::Op op) override;
89 virtual bool clipPath(const SkPath* path, SkRegion::Op op) override;
90 virtual bool clipRegion(const SkRegion* region, SkRegion::Op op) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040091
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050092 virtual SkDrawFilter* getDrawFilter() override;
93 virtual void setDrawFilter(SkDrawFilter* drawFilter) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040094
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050095 virtual void drawColor(int color, SkXfermode::Mode mode) override;
96 virtual void drawPaint(const SkPaint& paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040097
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050098 virtual void drawPoint(float x, float y, const SkPaint& paint) override;
99 virtual void drawPoints(const float* points, int count, const SkPaint& paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400100 virtual void drawLine(float startX, float startY, float stopX, float stopY,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500101 const SkPaint& paint) override;
102 virtual void drawLines(const float* points, int count, const SkPaint& paint) override;
103 virtual void drawRect(float left, float top, float right, float bottom,
104 const SkPaint& paint) override;
Derek Sollenberger94394b32015-07-10 09:58:41 -0400105 virtual void drawRegion(const SkRegion& region, const SkPaint& paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400106 virtual void drawRoundRect(float left, float top, float right, float bottom,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500107 float rx, float ry, const SkPaint& paint) override;
108 virtual void drawCircle(float x, float y, float radius, const SkPaint& paint) override;
109 virtual void drawOval(float left, float top, float right, float bottom,
110 const SkPaint& paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400111 virtual void drawArc(float left, float top, float right, float bottom,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500112 float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) override;
113 virtual void drawPath(const SkPath& path, const SkPaint& paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400114 virtual void drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
115 const float* verts, const float* tex, const int* colors,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500116 const uint16_t* indices, int indexCount, const SkPaint& paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400117
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500118 virtual void drawBitmap(const SkBitmap& bitmap, float left, float top,
119 const SkPaint* paint) override;
120 virtual void drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix,
121 const SkPaint* paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400122 virtual void drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop,
123 float srcRight, float srcBottom, float dstLeft, float dstTop,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500124 float dstRight, float dstBottom, const SkPaint* paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400125 virtual void drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500126 const float* vertices, const int* colors, const SkPaint* paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400127
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400128 virtual void drawText(const uint16_t* text, const float* positions, int count,
129 const SkPaint& paint, float x, float y,
Tom Hudson8dfaa492014-12-09 15:03:44 -0500130 float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500131 float totalAdvance) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400132 virtual void drawPosText(const uint16_t* text, const float* positions, int count,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500133 int posCount, const SkPaint& paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400134 virtual void drawTextOnPath(const uint16_t* glyphs, int count, const SkPath& path,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500135 float hOffset, float vOffset, const SkPaint& paint) override;
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400136
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500137 virtual bool drawTextAbsolutePos() const override { return true; }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400138
139private:
140 struct SaveRec {
141 int saveCount;
142 SkCanvas::SaveFlags saveFlags;
143 };
144
Derek Sollenberger6578a982015-07-13 13:24:29 -0400145 bool mHighContrastText = false;
146
Derek Sollenberger8872b382014-06-23 14:13:53 -0400147 void recordPartialSave(SkCanvas::SaveFlags flags);
148 void saveClipsForFrame(SkTArray<SkClipStack::Element>& clips, int frameSaveCount);
149 void applyClips(const SkTArray<SkClipStack::Element>& clips);
150
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400151 void drawPoints(const float* points, int count, const SkPaint& paint,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400152 SkCanvas::PointMode mode);
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400153 void drawTextDecorations(float x, float y, float length, const SkPaint& paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400154
155 SkAutoTUnref<SkCanvas> mCanvas;
Ben Wagner60126ef2015-08-07 12:13:48 -0400156 std::unique_ptr<SkDeque> mSaveStack; // lazily allocated, tracks partial saves.
Derek Sollenberger8872b382014-06-23 14:13:53 -0400157};
158
John Reckc1b33d62015-04-22 09:04:45 -0700159Canvas* Canvas::create_canvas(const SkBitmap& bitmap) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400160 return new SkiaCanvas(bitmap);
161}
162
163Canvas* Canvas::create_canvas(SkCanvas* skiaCanvas) {
164 return new SkiaCanvas(skiaCanvas);
165}
166
John Reckc1b33d62015-04-22 09:04:45 -0700167SkiaCanvas::SkiaCanvas(const SkBitmap& bitmap) {
168 mCanvas.reset(new SkCanvas(bitmap));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400169}
170
171// ----------------------------------------------------------------------------
172// Canvas state operations: Replace Bitmap
173// ----------------------------------------------------------------------------
174
175class ClipCopier : public SkCanvas::ClipVisitor {
176public:
177 ClipCopier(SkCanvas* dstCanvas) : m_dstCanvas(dstCanvas) {}
178
179 virtual void clipRect(const SkRect& rect, SkRegion::Op op, bool antialias) {
180 m_dstCanvas->clipRect(rect, op, antialias);
181 }
182 virtual void clipRRect(const SkRRect& rrect, SkRegion::Op op, bool antialias) {
183 m_dstCanvas->clipRRect(rrect, op, antialias);
184 }
185 virtual void clipPath(const SkPath& path, SkRegion::Op op, bool antialias) {
186 m_dstCanvas->clipPath(path, op, antialias);
187 }
188
189private:
190 SkCanvas* m_dstCanvas;
191};
192
John Reckc1b33d62015-04-22 09:04:45 -0700193void SkiaCanvas::setBitmap(const SkBitmap& bitmap) {
194 SkCanvas* newCanvas = new SkCanvas(bitmap);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400195 SkASSERT(newCanvas);
196
John Reckc1b33d62015-04-22 09:04:45 -0700197 if (!bitmap.isNull()) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400198 // Copy the canvas matrix & clip state.
199 newCanvas->setMatrix(mCanvas->getTotalMatrix());
200 if (NULL != mCanvas->getDevice() && NULL != newCanvas->getDevice()) {
201 ClipCopier copier(newCanvas);
202 mCanvas->replayClips(&copier);
203 }
204 }
205
206 // unrefs the existing canvas
207 mCanvas.reset(newCanvas);
208
209 // clean up the old save stack
210 mSaveStack.reset(NULL);
211}
212
213// ----------------------------------------------------------------------------
214// Canvas state operations
215// ----------------------------------------------------------------------------
216
217bool SkiaCanvas::isOpaque() {
218 return mCanvas->getDevice()->accessBitmap(false).isOpaque();
219}
220
221int SkiaCanvas::width() {
222 return mCanvas->getBaseLayerSize().width();
223}
224
225int SkiaCanvas::height() {
226 return mCanvas->getBaseLayerSize().height();
227}
228
229// ----------------------------------------------------------------------------
230// Canvas state operations: Save (layer)
231// ----------------------------------------------------------------------------
232
233int SkiaCanvas::getSaveCount() const {
234 return mCanvas->getSaveCount();
235}
236
237int SkiaCanvas::save(SkCanvas::SaveFlags flags) {
238 int count = mCanvas->save();
239 recordPartialSave(flags);
240 return count;
241}
242
243void SkiaCanvas::restore() {
244 const SaveRec* rec = (NULL == mSaveStack.get())
245 ? NULL
246 : static_cast<SaveRec*>(mSaveStack->back());
247 int currentSaveCount = mCanvas->getSaveCount() - 1;
248 SkASSERT(NULL == rec || currentSaveCount >= rec->saveCount);
249
250 if (NULL == rec || rec->saveCount != currentSaveCount) {
251 // Fast path - no record for this frame.
252 mCanvas->restore();
253 return;
254 }
255
256 bool preserveMatrix = !(rec->saveFlags & SkCanvas::kMatrix_SaveFlag);
257 bool preserveClip = !(rec->saveFlags & SkCanvas::kClip_SaveFlag);
258
259 SkMatrix savedMatrix;
260 if (preserveMatrix) {
261 savedMatrix = mCanvas->getTotalMatrix();
262 }
263
264 SkTArray<SkClipStack::Element> savedClips;
265 if (preserveClip) {
266 saveClipsForFrame(savedClips, currentSaveCount);
267 }
268
269 mCanvas->restore();
270
271 if (preserveMatrix) {
272 mCanvas->setMatrix(savedMatrix);
273 }
274
275 if (preserveClip && !savedClips.empty()) {
276 applyClips(savedClips);
277 }
278
279 mSaveStack->pop_back();
280}
281
282void SkiaCanvas::restoreToCount(int restoreCount) {
283 while (mCanvas->getSaveCount() > restoreCount) {
284 this->restore();
285 }
286}
287
288int SkiaCanvas::saveLayer(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400289 const SkPaint* paint, SkCanvas::SaveFlags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400290 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
291 int count = mCanvas->saveLayer(&bounds, paint, flags | SkCanvas::kMatrixClip_SaveFlag);
292 recordPartialSave(flags);
293 return count;
294}
295
296int SkiaCanvas::saveLayerAlpha(float left, float top, float right, float bottom,
297 int alpha, SkCanvas::SaveFlags flags) {
298 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
299 int count = mCanvas->saveLayerAlpha(&bounds, alpha, flags | SkCanvas::kMatrixClip_SaveFlag);
300 recordPartialSave(flags);
301 return count;
302}
303
304// ----------------------------------------------------------------------------
305// functions to emulate legacy SaveFlags (i.e. independent matrix/clip flags)
306// ----------------------------------------------------------------------------
307
308void SkiaCanvas::recordPartialSave(SkCanvas::SaveFlags flags) {
309 // A partial save is a save operation which doesn't capture the full canvas state.
310 // (either kMatrix_SaveFlags or kClip_SaveFlag is missing).
311
312 // Mask-out non canvas state bits.
313 flags = static_cast<SkCanvas::SaveFlags>(flags & SkCanvas::kMatrixClip_SaveFlag);
314
315 if (SkCanvas::kMatrixClip_SaveFlag == flags) {
316 // not a partial save.
317 return;
318 }
319
320 if (NULL == mSaveStack.get()) {
321 mSaveStack.reset(SkNEW_ARGS(SkDeque, (sizeof(struct SaveRec), 8)));
322 }
323
324 SaveRec* rec = static_cast<SaveRec*>(mSaveStack->push_back());
325 // Store the save counter in the SkClipStack domain.
326 // (0-based, equal to the number of save ops on the stack).
327 rec->saveCount = mCanvas->getSaveCount() - 1;
328 rec->saveFlags = flags;
329}
330
331void SkiaCanvas::saveClipsForFrame(SkTArray<SkClipStack::Element>& clips, int frameSaveCount) {
332 SkClipStack::Iter clipIterator(*mCanvas->getClipStack(),
333 SkClipStack::Iter::kTop_IterStart);
334 while (const SkClipStack::Element* elem = clipIterator.next()) {
335 if (elem->getSaveCount() < frameSaveCount) {
336 // done with the current frame.
337 break;
338 }
339 SkASSERT(elem->getSaveCount() == frameSaveCount);
340 clips.push_back(*elem);
341 }
342}
343
344void SkiaCanvas::applyClips(const SkTArray<SkClipStack::Element>& clips) {
345 ClipCopier clipCopier(mCanvas);
346
347 // The clip stack stores clips in device space.
348 SkMatrix origMatrix = mCanvas->getTotalMatrix();
349 mCanvas->resetMatrix();
350
351 // We pushed the clips in reverse order.
352 for (int i = clips.count() - 1; i >= 0; --i) {
353 clips[i].replay(&clipCopier);
354 }
355
356 mCanvas->setMatrix(origMatrix);
357}
358
359// ----------------------------------------------------------------------------
360// Canvas state operations: Matrix
361// ----------------------------------------------------------------------------
362
363void SkiaCanvas::getMatrix(SkMatrix* outMatrix) const {
364 *outMatrix = mCanvas->getTotalMatrix();
365}
366
367void SkiaCanvas::setMatrix(const SkMatrix& matrix) {
368 mCanvas->setMatrix(matrix);
369}
370
371void SkiaCanvas::concat(const SkMatrix& matrix) {
372 mCanvas->concat(matrix);
373}
374
375void SkiaCanvas::rotate(float degrees) {
376 mCanvas->rotate(degrees);
377}
378
379void SkiaCanvas::scale(float sx, float sy) {
380 mCanvas->scale(sx, sy);
381}
382
383void SkiaCanvas::skew(float sx, float sy) {
384 mCanvas->skew(sx, sy);
385}
386
387void SkiaCanvas::translate(float dx, float dy) {
388 mCanvas->translate(dx, dy);
389}
390
391// ----------------------------------------------------------------------------
392// Canvas state operations: Clips
393// ----------------------------------------------------------------------------
394
395// This function is a mirror of SkCanvas::getClipBounds except that it does
396// not outset the edge of the clip to account for anti-aliasing. There is
397// a skia bug to investigate pushing this logic into back into skia.
398// (see https://code.google.com/p/skia/issues/detail?id=1303)
399bool SkiaCanvas::getClipBounds(SkRect* outRect) const {
400 SkIRect ibounds;
401 if (!mCanvas->getClipDeviceBounds(&ibounds)) {
402 return false;
403 }
404
405 SkMatrix inverse;
406 // if we can't invert the CTM, we can't return local clip bounds
407 if (!mCanvas->getTotalMatrix().invert(&inverse)) {
408 if (outRect) {
409 outRect->setEmpty();
410 }
411 return false;
412 }
413
414 if (NULL != outRect) {
415 SkRect r = SkRect::Make(ibounds);
416 inverse.mapRect(outRect, r);
417 }
418 return true;
419}
420
421bool SkiaCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
422 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
423 return mCanvas->quickReject(bounds);
424}
425
426bool SkiaCanvas::quickRejectPath(const SkPath& path) const {
427 return mCanvas->quickReject(path);
428}
429
430bool SkiaCanvas::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
431 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
432 mCanvas->clipRect(rect, op);
Chris Craik5ec6a282015-06-23 15:42:12 -0700433 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400434}
435
436bool SkiaCanvas::clipPath(const SkPath* path, SkRegion::Op op) {
437 mCanvas->clipPath(*path, op);
Chris Craik5ec6a282015-06-23 15:42:12 -0700438 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400439}
440
441bool SkiaCanvas::clipRegion(const SkRegion* region, SkRegion::Op op) {
442 SkPath rgnPath;
443 if (region->getBoundaryPath(&rgnPath)) {
444 // The region is specified in device space.
445 SkMatrix savedMatrix = mCanvas->getTotalMatrix();
446 mCanvas->resetMatrix();
447 mCanvas->clipPath(rgnPath, op);
448 mCanvas->setMatrix(savedMatrix);
449 } else {
450 mCanvas->clipRect(SkRect::MakeEmpty(), op);
451 }
Chris Craik5ec6a282015-06-23 15:42:12 -0700452 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400453}
454
455// ----------------------------------------------------------------------------
456// Canvas state operations: Filters
457// ----------------------------------------------------------------------------
458
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400459SkDrawFilter* SkiaCanvas::getDrawFilter() {
460 return mCanvas->getDrawFilter();
461}
462
Derek Sollenberger8872b382014-06-23 14:13:53 -0400463void SkiaCanvas::setDrawFilter(SkDrawFilter* drawFilter) {
464 mCanvas->setDrawFilter(drawFilter);
465}
466
467// ----------------------------------------------------------------------------
468// Canvas draw operations
469// ----------------------------------------------------------------------------
470
471void SkiaCanvas::drawColor(int color, SkXfermode::Mode mode) {
472 mCanvas->drawColor(color, mode);
473}
474
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400475void SkiaCanvas::drawPaint(const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400476 mCanvas->drawPaint(paint);
477}
478
479// ----------------------------------------------------------------------------
480// Canvas draw operations: Geometry
481// ----------------------------------------------------------------------------
482
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400483void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400484 SkCanvas::PointMode mode) {
485 // convert the floats into SkPoints
486 count >>= 1; // now it is the number of points
487 SkAutoSTMalloc<32, SkPoint> storage(count);
488 SkPoint* pts = storage.get();
489 for (int i = 0; i < count; i++) {
490 pts[i].set(points[0], points[1]);
491 points += 2;
492 }
493 mCanvas->drawPoints(mode, count, pts, paint);
494}
495
496
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400497void SkiaCanvas::drawPoint(float x, float y, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400498 mCanvas->drawPoint(x, y, paint);
499}
500
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400501void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400502 this->drawPoints(points, count, paint, SkCanvas::kPoints_PointMode);
503}
504
505void SkiaCanvas::drawLine(float startX, float startY, float stopX, float stopY,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400506 const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400507 mCanvas->drawLine(startX, startY, stopX, stopY, paint);
508}
509
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400510void SkiaCanvas::drawLines(const float* points, int count, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400511 this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode);
512}
513
514void SkiaCanvas::drawRect(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400515 const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400516 mCanvas->drawRectCoords(left, top, right, bottom, paint);
517
518}
519
Derek Sollenberger94394b32015-07-10 09:58:41 -0400520void SkiaCanvas::drawRegion(const SkRegion& region, const SkPaint& paint) {
521 SkRegion::Iterator it(region);
522 while (!it.done()) {
523 mCanvas->drawRect(SkRect::Make(it.rect()), paint);
524 it.next();
525 }
526}
527
Derek Sollenberger8872b382014-06-23 14:13:53 -0400528void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400529 float rx, float ry, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400530 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
531 mCanvas->drawRoundRect(rect, rx, ry, paint);
532}
533
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400534void SkiaCanvas::drawCircle(float x, float y, float radius, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400535 mCanvas->drawCircle(x, y, radius, paint);
536}
537
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400538void SkiaCanvas::drawOval(float left, float top, float right, float bottom, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400539 SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
540 mCanvas->drawOval(oval, paint);
541}
542
543void SkiaCanvas::drawArc(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400544 float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400545 SkRect arc = SkRect::MakeLTRB(left, top, right, bottom);
546 mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, paint);
547}
548
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400549void SkiaCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400550 mCanvas->drawPath(path, paint);
551}
552
553void SkiaCanvas::drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
554 const float* verts, const float* texs, const int* colors,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400555 const uint16_t* indices, int indexCount, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400556#ifndef SK_SCALAR_IS_FLOAT
557 SkDEBUGFAIL("SkScalar must be a float for these conversions to be valid");
558#endif
559 const int ptCount = vertexCount >> 1;
560 mCanvas->drawVertices(vertexMode, ptCount, (SkPoint*)verts, (SkPoint*)texs,
561 (SkColor*)colors, NULL, indices, indexCount, paint);
562}
563
564// ----------------------------------------------------------------------------
565// Canvas draw operations: Bitmaps
566// ----------------------------------------------------------------------------
567
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400568void SkiaCanvas::drawBitmap(const SkBitmap& bitmap, float left, float top, const SkPaint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400569 mCanvas->drawBitmap(bitmap, left, top, paint);
570}
571
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400572void SkiaCanvas::drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix, const SkPaint* paint) {
Mike Reed70ffbf92014-12-08 17:03:30 -0500573 SkAutoCanvasRestore acr(mCanvas, true);
574 mCanvas->concat(matrix);
575 mCanvas->drawBitmap(bitmap, 0, 0, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400576}
577
578void SkiaCanvas::drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop,
579 float srcRight, float srcBottom, float dstLeft, float dstTop,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400580 float dstRight, float dstBottom, const SkPaint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400581 SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
582 SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
583 mCanvas->drawBitmapRectToRect(bitmap, &srcRect, dstRect, paint);
584}
585
586void SkiaCanvas::drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400587 const float* vertices, const int* colors, const SkPaint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400588
589 const int ptCount = (meshWidth + 1) * (meshHeight + 1);
590 const int indexCount = meshWidth * meshHeight * 6;
591
592 /* Our temp storage holds 2 or 3 arrays.
593 texture points [ptCount * sizeof(SkPoint)]
594 optionally vertex points [ptCount * sizeof(SkPoint)] if we need a
595 copy to convert from float to fixed
596 indices [ptCount * sizeof(uint16_t)]
597 */
598 ssize_t storageSize = ptCount * sizeof(SkPoint); // texs[]
599 storageSize += indexCount * sizeof(uint16_t); // indices[]
600
601
602#ifndef SK_SCALAR_IS_FLOAT
603 SkDEBUGFAIL("SkScalar must be a float for these conversions to be valid");
604#endif
605 SkAutoMalloc storage(storageSize);
606 SkPoint* texs = (SkPoint*)storage.get();
607 uint16_t* indices = (uint16_t*)(texs + ptCount);
608
609 // cons up texture coordinates and indices
610 {
611 const SkScalar w = SkIntToScalar(bitmap.width());
612 const SkScalar h = SkIntToScalar(bitmap.height());
613 const SkScalar dx = w / meshWidth;
614 const SkScalar dy = h / meshHeight;
615
616 SkPoint* texsPtr = texs;
617 SkScalar y = 0;
618 for (int i = 0; i <= meshHeight; i++) {
619 if (i == meshHeight) {
620 y = h; // to ensure numerically we hit h exactly
621 }
622 SkScalar x = 0;
623 for (int j = 0; j < meshWidth; j++) {
624 texsPtr->set(x, y);
625 texsPtr += 1;
626 x += dx;
627 }
628 texsPtr->set(w, y);
629 texsPtr += 1;
630 y += dy;
631 }
632 SkASSERT(texsPtr - texs == ptCount);
633 }
634
635 // cons up indices
636 {
637 uint16_t* indexPtr = indices;
638 int index = 0;
639 for (int i = 0; i < meshHeight; i++) {
640 for (int j = 0; j < meshWidth; j++) {
641 // lower-left triangle
642 *indexPtr++ = index;
643 *indexPtr++ = index + meshWidth + 1;
644 *indexPtr++ = index + meshWidth + 2;
645 // upper-right triangle
646 *indexPtr++ = index;
647 *indexPtr++ = index + meshWidth + 2;
648 *indexPtr++ = index + 1;
649 // bump to the next cell
650 index += 1;
651 }
652 // bump to the next row
653 index += 1;
654 }
655 SkASSERT(indexPtr - indices == indexCount);
656 SkASSERT((char*)indexPtr - (char*)storage.get() == storageSize);
657 }
658
659 // double-check that we have legal indices
660#ifdef SK_DEBUG
661 {
662 for (int i = 0; i < indexCount; i++) {
663 SkASSERT((unsigned)indices[i] < (unsigned)ptCount);
664 }
665 }
666#endif
667
668 // cons-up a shader for the bitmap
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400669 SkPaint tmpPaint;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400670 if (paint) {
671 tmpPaint = *paint;
672 }
673 SkShader* shader = SkShader::CreateBitmapShader(bitmap,
674 SkShader::kClamp_TileMode,
675 SkShader::kClamp_TileMode);
676 SkSafeUnref(tmpPaint.setShader(shader));
677
678 mCanvas->drawVertices(SkCanvas::kTriangles_VertexMode, ptCount, (SkPoint*)vertices,
679 texs, (const SkColor*)colors, NULL, indices,
680 indexCount, tmpPaint);
681}
682
683// ----------------------------------------------------------------------------
684// Canvas draw operations: Text
685// ----------------------------------------------------------------------------
686
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400687void SkiaCanvas::drawText(const uint16_t* text, const float* positions, int count,
688 const SkPaint& paint, float x, float y,
Tom Hudson8dfaa492014-12-09 15:03:44 -0500689 float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
690 float totalAdvance) {
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400691 // Set align to left for drawing, as we don't want individual
692 // glyphs centered or right-aligned; the offset above takes
693 // care of all alignment.
694 SkPaint paintCopy(paint);
695 paintCopy.setTextAlign(SkPaint::kLeft_Align);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400696
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400697 SK_COMPILE_ASSERT(sizeof(SkPoint) == sizeof(float)*2, SkPoint_is_no_longer_2_floats);
698 mCanvas->drawPosText(text, count << 1, reinterpret_cast<const SkPoint*>(positions), paintCopy);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400699}
700
701void SkiaCanvas::drawPosText(const uint16_t* text, const float* positions, int count, int posCount,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400702 const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400703 SkPoint* posPtr = posCount > 0 ? new SkPoint[posCount] : NULL;
704 int indx;
705 for (indx = 0; indx < posCount; indx++) {
706 posPtr[indx].fX = positions[indx << 1];
707 posPtr[indx].fY = positions[(indx << 1) + 1];
708 }
709
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400710 SkPaint paintCopy(paint);
711 paintCopy.setTextEncoding(SkPaint::kUTF16_TextEncoding);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400712 mCanvas->drawPosText(text, count, posPtr, paintCopy);
713
714 delete[] posPtr;
715}
716
717void SkiaCanvas::drawTextOnPath(const uint16_t* glyphs, int count, const SkPath& path,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400718 float hOffset, float vOffset, const SkPaint& paint) {
Tom Hudson34e79c12015-04-14 11:34:39 -0400719 mCanvas->drawTextOnPathHV(glyphs, count << 1, path, hOffset, vOffset, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400720}
721
722} // namespace android