blob: 6d3dfac4ba9c07b5d91fb7b66e11e67463821faa [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;
77 virtual void concat(const SkMatrix& matrix) override;
78 virtual void rotate(float degrees) override;
79 virtual void scale(float sx, float sy) override;
80 virtual void skew(float sx, float sy) override;
81 virtual void translate(float dx, float dy) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040082
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050083 virtual bool getClipBounds(SkRect* outRect) const override;
84 virtual bool quickRejectRect(float left, float top, float right, float bottom) const override;
85 virtual bool quickRejectPath(const SkPath& path) const override;
86 virtual bool clipRect(float left, float top, float right, float bottom,
87 SkRegion::Op op) override;
88 virtual bool clipPath(const SkPath* path, SkRegion::Op op) override;
89 virtual bool clipRegion(const SkRegion* region, SkRegion::Op op) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040090
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050091 virtual SkDrawFilter* getDrawFilter() override;
92 virtual void setDrawFilter(SkDrawFilter* drawFilter) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040093
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050094 virtual void drawColor(int color, SkXfermode::Mode mode) override;
95 virtual void drawPaint(const SkPaint& paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040096
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -050097 virtual void drawPoint(float x, float y, const SkPaint& paint) override;
98 virtual void drawPoints(const float* points, int count, const SkPaint& paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -040099 virtual void drawLine(float startX, float startY, float stopX, float stopY,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500100 const SkPaint& paint) override;
101 virtual void drawLines(const float* points, int count, const SkPaint& paint) override;
102 virtual void drawRect(float left, float top, float right, float bottom,
103 const SkPaint& paint) override;
Derek Sollenberger94394b32015-07-10 09:58:41 -0400104 virtual void drawRegion(const SkRegion& region, const SkPaint& paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400105 virtual void drawRoundRect(float left, float top, float right, float bottom,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500106 float rx, float ry, const SkPaint& paint) override;
107 virtual void drawCircle(float x, float y, float radius, const SkPaint& paint) override;
108 virtual void drawOval(float left, float top, float right, float bottom,
109 const SkPaint& paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400110 virtual void drawArc(float left, float top, float right, float bottom,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500111 float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) override;
112 virtual void drawPath(const SkPath& path, const SkPaint& paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400113 virtual void drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
114 const float* verts, const float* tex, const int* colors,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500115 const uint16_t* indices, int indexCount, const SkPaint& paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400116
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500117 virtual void drawBitmap(const SkBitmap& bitmap, float left, float top,
118 const SkPaint* paint) override;
119 virtual void drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix,
120 const SkPaint* paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400121 virtual void drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop,
122 float srcRight, float srcBottom, float dstLeft, float dstTop,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500123 float dstRight, float dstBottom, const SkPaint* paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400124 virtual void drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500125 const float* vertices, const int* colors, const SkPaint* paint) override;
Derek Sollenbergeredca3202015-07-10 13:56:39 -0400126 virtual void drawNinePatch(const SkBitmap& bitmap, const android::Res_png_9patch& chunk,
127 float dstLeft, float dstTop, float dstRight, float dstBottom,
128 const SkPaint* paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400129
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400130 virtual void drawText(const uint16_t* text, const float* positions, int count,
131 const SkPaint& paint, float x, float y,
Tom Hudson8dfaa492014-12-09 15:03:44 -0500132 float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500133 float totalAdvance) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400134 virtual void drawPosText(const uint16_t* text, const float* positions, int count,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500135 int posCount, const SkPaint& paint) override;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400136 virtual void drawTextOnPath(const uint16_t* glyphs, int count, const SkPath& path,
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500137 float hOffset, float vOffset, const SkPaint& paint) override;
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400138
Derek Sollenbergerb3d50e02015-01-29 11:19:31 -0500139 virtual bool drawTextAbsolutePos() const override { return true; }
Derek Sollenberger8872b382014-06-23 14:13:53 -0400140
141private:
142 struct SaveRec {
143 int saveCount;
144 SkCanvas::SaveFlags saveFlags;
145 };
146
Derek Sollenberger6578a982015-07-13 13:24:29 -0400147 bool mHighContrastText = false;
148
Derek Sollenberger8872b382014-06-23 14:13:53 -0400149 void recordPartialSave(SkCanvas::SaveFlags flags);
150 void saveClipsForFrame(SkTArray<SkClipStack::Element>& clips, int frameSaveCount);
151 void applyClips(const SkTArray<SkClipStack::Element>& clips);
152
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400153 void drawPoints(const float* points, int count, const SkPaint& paint,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400154 SkCanvas::PointMode mode);
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400155 void drawTextDecorations(float x, float y, float length, const SkPaint& paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400156
157 SkAutoTUnref<SkCanvas> mCanvas;
Ben Wagner60126ef2015-08-07 12:13:48 -0400158 std::unique_ptr<SkDeque> mSaveStack; // lazily allocated, tracks partial saves.
Derek Sollenberger8872b382014-06-23 14:13:53 -0400159};
160
John Reckc1b33d62015-04-22 09:04:45 -0700161Canvas* Canvas::create_canvas(const SkBitmap& bitmap) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400162 return new SkiaCanvas(bitmap);
163}
164
165Canvas* Canvas::create_canvas(SkCanvas* skiaCanvas) {
166 return new SkiaCanvas(skiaCanvas);
167}
168
John Reckc1b33d62015-04-22 09:04:45 -0700169SkiaCanvas::SkiaCanvas(const SkBitmap& bitmap) {
170 mCanvas.reset(new SkCanvas(bitmap));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400171}
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
John Reckc1b33d62015-04-22 09:04:45 -0700195void SkiaCanvas::setBitmap(const SkBitmap& bitmap) {
196 SkCanvas* newCanvas = new SkCanvas(bitmap);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400197
John Reckc1b33d62015-04-22 09:04:45 -0700198 if (!bitmap.isNull()) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400199 // Copy the canvas matrix & clip state.
200 newCanvas->setMatrix(mCanvas->getTotalMatrix());
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400201
202 ClipCopier copier(newCanvas);
203 mCanvas->replayClips(&copier);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400204 }
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() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400218 return mCanvas->imageInfo().isOpaque();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400219}
220
221int SkiaCanvas::width() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400222 return mCanvas->imageInfo().width();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400223}
224
225int SkiaCanvas::height() {
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400226 return mCanvas->imageInfo().height();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400227}
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
Florin Malita5e271402015-11-04 14:36:02 -0500243// The SkiaCanvas::restore operation layers on the capability to preserve
244// either (or both) the matrix and/or clip state after a SkCanvas::restore
245// operation. It does this by explicitly saving off the clip & matrix state
246// when requested and playing it back after the SkCanvas::restore.
Derek Sollenberger8872b382014-06-23 14:13:53 -0400247void SkiaCanvas::restore() {
248 const SaveRec* rec = (NULL == mSaveStack.get())
249 ? NULL
250 : static_cast<SaveRec*>(mSaveStack->back());
Florin Malita5e271402015-11-04 14:36:02 -0500251 int currentSaveCount = mCanvas->getSaveCount();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400252 SkASSERT(NULL == rec || currentSaveCount >= rec->saveCount);
253
254 if (NULL == rec || rec->saveCount != currentSaveCount) {
255 // Fast path - no record for this frame.
256 mCanvas->restore();
257 return;
258 }
259
260 bool preserveMatrix = !(rec->saveFlags & SkCanvas::kMatrix_SaveFlag);
261 bool preserveClip = !(rec->saveFlags & SkCanvas::kClip_SaveFlag);
262
263 SkMatrix savedMatrix;
264 if (preserveMatrix) {
265 savedMatrix = mCanvas->getTotalMatrix();
266 }
267
268 SkTArray<SkClipStack::Element> savedClips;
Florin Malita5e271402015-11-04 14:36:02 -0500269 int topClipStackFrame = mCanvas->getClipStack()->getSaveCount();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400270 if (preserveClip) {
Florin Malita5e271402015-11-04 14:36:02 -0500271 saveClipsForFrame(savedClips, topClipStackFrame);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400272 }
273
274 mCanvas->restore();
275
276 if (preserveMatrix) {
277 mCanvas->setMatrix(savedMatrix);
278 }
279
Florin Malita5e271402015-11-04 14:36:02 -0500280 if (preserveClip && !savedClips.empty() &&
281 topClipStackFrame != mCanvas->getClipStack()->getSaveCount()) {
282 // Only reapply the saved clips if the top clip stack frame was actually
283 // popped by restore(). If it wasn't, it means it doesn't belong to the
284 // restored canvas frame (SkCanvas lazy save/restore kicked in).
Derek Sollenberger8872b382014-06-23 14:13:53 -0400285 applyClips(savedClips);
286 }
287
288 mSaveStack->pop_back();
289}
290
291void SkiaCanvas::restoreToCount(int restoreCount) {
292 while (mCanvas->getSaveCount() > restoreCount) {
293 this->restore();
294 }
295}
296
297int SkiaCanvas::saveLayer(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400298 const SkPaint* paint, SkCanvas::SaveFlags flags) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400299 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
300 int count = mCanvas->saveLayer(&bounds, paint, flags | SkCanvas::kMatrixClip_SaveFlag);
301 recordPartialSave(flags);
302 return count;
303}
304
305int SkiaCanvas::saveLayerAlpha(float left, float top, float right, float bottom,
306 int alpha, SkCanvas::SaveFlags flags) {
307 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
308 int count = mCanvas->saveLayerAlpha(&bounds, alpha, flags | SkCanvas::kMatrixClip_SaveFlag);
309 recordPartialSave(flags);
310 return count;
311}
312
313// ----------------------------------------------------------------------------
314// functions to emulate legacy SaveFlags (i.e. independent matrix/clip flags)
315// ----------------------------------------------------------------------------
316
317void SkiaCanvas::recordPartialSave(SkCanvas::SaveFlags flags) {
318 // A partial save is a save operation which doesn't capture the full canvas state.
319 // (either kMatrix_SaveFlags or kClip_SaveFlag is missing).
320
321 // Mask-out non canvas state bits.
322 flags = static_cast<SkCanvas::SaveFlags>(flags & SkCanvas::kMatrixClip_SaveFlag);
323
324 if (SkCanvas::kMatrixClip_SaveFlag == flags) {
325 // not a partial save.
326 return;
327 }
328
329 if (NULL == mSaveStack.get()) {
Ben Wagnerd1cbc162015-08-19 12:45:09 -0400330 mSaveStack.reset(new SkDeque(sizeof(struct SaveRec), 8));
Derek Sollenberger8872b382014-06-23 14:13:53 -0400331 }
332
333 SaveRec* rec = static_cast<SaveRec*>(mSaveStack->push_back());
Florin Malita5e271402015-11-04 14:36:02 -0500334 rec->saveCount = mCanvas->getSaveCount();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400335 rec->saveFlags = flags;
336}
337
Florin Malita5e271402015-11-04 14:36:02 -0500338void SkiaCanvas::saveClipsForFrame(SkTArray<SkClipStack::Element>& clips,
339 int saveCountToBackup) {
340 // Each SkClipStack::Element stores the index of the canvas save
341 // with which it is associated. Backup only those Elements that
342 // are associated with 'saveCountToBackup'
Derek Sollenberger8872b382014-06-23 14:13:53 -0400343 SkClipStack::Iter clipIterator(*mCanvas->getClipStack(),
344 SkClipStack::Iter::kTop_IterStart);
Florin Malita5e271402015-11-04 14:36:02 -0500345 while (const SkClipStack::Element* elem = clipIterator.prev()) {
346 if (elem->getSaveCount() < saveCountToBackup) {
347 // done with the target save count.
Derek Sollenberger8872b382014-06-23 14:13:53 -0400348 break;
349 }
Florin Malita5e271402015-11-04 14:36:02 -0500350 SkASSERT(elem->getSaveCount() == saveCountToBackup);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400351 clips.push_back(*elem);
352 }
353}
354
355void SkiaCanvas::applyClips(const SkTArray<SkClipStack::Element>& clips) {
356 ClipCopier clipCopier(mCanvas);
357
358 // The clip stack stores clips in device space.
359 SkMatrix origMatrix = mCanvas->getTotalMatrix();
360 mCanvas->resetMatrix();
361
362 // We pushed the clips in reverse order.
363 for (int i = clips.count() - 1; i >= 0; --i) {
364 clips[i].replay(&clipCopier);
365 }
366
367 mCanvas->setMatrix(origMatrix);
368}
369
370// ----------------------------------------------------------------------------
371// Canvas state operations: Matrix
372// ----------------------------------------------------------------------------
373
374void SkiaCanvas::getMatrix(SkMatrix* outMatrix) const {
375 *outMatrix = mCanvas->getTotalMatrix();
376}
377
378void SkiaCanvas::setMatrix(const SkMatrix& matrix) {
379 mCanvas->setMatrix(matrix);
380}
381
382void SkiaCanvas::concat(const SkMatrix& matrix) {
383 mCanvas->concat(matrix);
384}
385
386void SkiaCanvas::rotate(float degrees) {
387 mCanvas->rotate(degrees);
388}
389
390void SkiaCanvas::scale(float sx, float sy) {
391 mCanvas->scale(sx, sy);
392}
393
394void SkiaCanvas::skew(float sx, float sy) {
395 mCanvas->skew(sx, sy);
396}
397
398void SkiaCanvas::translate(float dx, float dy) {
399 mCanvas->translate(dx, dy);
400}
401
402// ----------------------------------------------------------------------------
403// Canvas state operations: Clips
404// ----------------------------------------------------------------------------
405
406// This function is a mirror of SkCanvas::getClipBounds except that it does
407// not outset the edge of the clip to account for anti-aliasing. There is
408// a skia bug to investigate pushing this logic into back into skia.
409// (see https://code.google.com/p/skia/issues/detail?id=1303)
410bool SkiaCanvas::getClipBounds(SkRect* outRect) const {
411 SkIRect ibounds;
412 if (!mCanvas->getClipDeviceBounds(&ibounds)) {
413 return false;
414 }
415
416 SkMatrix inverse;
417 // if we can't invert the CTM, we can't return local clip bounds
418 if (!mCanvas->getTotalMatrix().invert(&inverse)) {
419 if (outRect) {
420 outRect->setEmpty();
421 }
422 return false;
423 }
424
425 if (NULL != outRect) {
426 SkRect r = SkRect::Make(ibounds);
427 inverse.mapRect(outRect, r);
428 }
429 return true;
430}
431
432bool SkiaCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
433 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
434 return mCanvas->quickReject(bounds);
435}
436
437bool SkiaCanvas::quickRejectPath(const SkPath& path) const {
438 return mCanvas->quickReject(path);
439}
440
441bool SkiaCanvas::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
442 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
443 mCanvas->clipRect(rect, op);
Chris Craik5ec6a282015-06-23 15:42:12 -0700444 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400445}
446
447bool SkiaCanvas::clipPath(const SkPath* path, SkRegion::Op op) {
448 mCanvas->clipPath(*path, op);
Chris Craik5ec6a282015-06-23 15:42:12 -0700449 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400450}
451
452bool SkiaCanvas::clipRegion(const SkRegion* region, SkRegion::Op op) {
453 SkPath rgnPath;
454 if (region->getBoundaryPath(&rgnPath)) {
455 // The region is specified in device space.
456 SkMatrix savedMatrix = mCanvas->getTotalMatrix();
457 mCanvas->resetMatrix();
458 mCanvas->clipPath(rgnPath, op);
459 mCanvas->setMatrix(savedMatrix);
460 } else {
461 mCanvas->clipRect(SkRect::MakeEmpty(), op);
462 }
Chris Craik5ec6a282015-06-23 15:42:12 -0700463 return !mCanvas->isClipEmpty();
Derek Sollenberger8872b382014-06-23 14:13:53 -0400464}
465
466// ----------------------------------------------------------------------------
467// Canvas state operations: Filters
468// ----------------------------------------------------------------------------
469
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400470SkDrawFilter* SkiaCanvas::getDrawFilter() {
471 return mCanvas->getDrawFilter();
472}
473
Derek Sollenberger8872b382014-06-23 14:13:53 -0400474void SkiaCanvas::setDrawFilter(SkDrawFilter* drawFilter) {
475 mCanvas->setDrawFilter(drawFilter);
476}
477
478// ----------------------------------------------------------------------------
479// Canvas draw operations
480// ----------------------------------------------------------------------------
481
482void SkiaCanvas::drawColor(int color, SkXfermode::Mode mode) {
483 mCanvas->drawColor(color, mode);
484}
485
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400486void SkiaCanvas::drawPaint(const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400487 mCanvas->drawPaint(paint);
488}
489
490// ----------------------------------------------------------------------------
491// Canvas draw operations: Geometry
492// ----------------------------------------------------------------------------
493
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400494void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint,
Derek Sollenberger8872b382014-06-23 14:13:53 -0400495 SkCanvas::PointMode mode) {
496 // convert the floats into SkPoints
497 count >>= 1; // now it is the number of points
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400498 std::unique_ptr<SkPoint[]> pts(new SkPoint[count]);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400499 for (int i = 0; i < count; i++) {
500 pts[i].set(points[0], points[1]);
501 points += 2;
502 }
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400503 mCanvas->drawPoints(mode, count, pts.get(), paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400504}
505
506
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400507void SkiaCanvas::drawPoint(float x, float y, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400508 mCanvas->drawPoint(x, y, paint);
509}
510
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400511void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400512 this->drawPoints(points, count, paint, SkCanvas::kPoints_PointMode);
513}
514
515void SkiaCanvas::drawLine(float startX, float startY, float stopX, float stopY,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400516 const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400517 mCanvas->drawLine(startX, startY, stopX, stopY, paint);
518}
519
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400520void SkiaCanvas::drawLines(const float* points, int count, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400521 this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode);
522}
523
524void SkiaCanvas::drawRect(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400525 const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400526 mCanvas->drawRectCoords(left, top, right, bottom, paint);
527
528}
529
Derek Sollenberger94394b32015-07-10 09:58:41 -0400530void SkiaCanvas::drawRegion(const SkRegion& region, const SkPaint& paint) {
531 SkRegion::Iterator it(region);
532 while (!it.done()) {
533 mCanvas->drawRect(SkRect::Make(it.rect()), paint);
534 it.next();
535 }
536}
537
Derek Sollenberger8872b382014-06-23 14:13:53 -0400538void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400539 float rx, float ry, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400540 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
541 mCanvas->drawRoundRect(rect, rx, ry, paint);
542}
543
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400544void SkiaCanvas::drawCircle(float x, float y, float radius, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400545 mCanvas->drawCircle(x, y, radius, paint);
546}
547
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400548void SkiaCanvas::drawOval(float left, float top, float right, float bottom, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400549 SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
550 mCanvas->drawOval(oval, paint);
551}
552
553void SkiaCanvas::drawArc(float left, float top, float right, float bottom,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400554 float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400555 SkRect arc = SkRect::MakeLTRB(left, top, right, bottom);
556 mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, paint);
557}
558
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400559void SkiaCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400560 mCanvas->drawPath(path, paint);
561}
562
563void SkiaCanvas::drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
564 const float* verts, const float* texs, const int* colors,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400565 const uint16_t* indices, int indexCount, const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400566#ifndef SK_SCALAR_IS_FLOAT
567 SkDEBUGFAIL("SkScalar must be a float for these conversions to be valid");
568#endif
569 const int ptCount = vertexCount >> 1;
570 mCanvas->drawVertices(vertexMode, ptCount, (SkPoint*)verts, (SkPoint*)texs,
571 (SkColor*)colors, NULL, indices, indexCount, paint);
572}
573
574// ----------------------------------------------------------------------------
575// Canvas draw operations: Bitmaps
576// ----------------------------------------------------------------------------
577
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400578void SkiaCanvas::drawBitmap(const SkBitmap& bitmap, float left, float top, const SkPaint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400579 mCanvas->drawBitmap(bitmap, left, top, paint);
580}
581
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400582void SkiaCanvas::drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix, const SkPaint* paint) {
Mike Reed70ffbf92014-12-08 17:03:30 -0500583 SkAutoCanvasRestore acr(mCanvas, true);
584 mCanvas->concat(matrix);
585 mCanvas->drawBitmap(bitmap, 0, 0, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400586}
587
588void SkiaCanvas::drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop,
589 float srcRight, float srcBottom, float dstLeft, float dstTop,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400590 float dstRight, float dstBottom, const SkPaint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400591 SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
592 SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
Leon Scroggins IIIf35b9892015-07-31 10:38:40 -0400593 mCanvas->drawBitmapRect(bitmap, srcRect, dstRect, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400594}
595
596void SkiaCanvas::drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400597 const float* vertices, const int* colors, const SkPaint* paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400598
599 const int ptCount = (meshWidth + 1) * (meshHeight + 1);
600 const int indexCount = meshWidth * meshHeight * 6;
601
602 /* Our temp storage holds 2 or 3 arrays.
603 texture points [ptCount * sizeof(SkPoint)]
604 optionally vertex points [ptCount * sizeof(SkPoint)] if we need a
605 copy to convert from float to fixed
606 indices [ptCount * sizeof(uint16_t)]
607 */
608 ssize_t storageSize = ptCount * sizeof(SkPoint); // texs[]
609 storageSize += indexCount * sizeof(uint16_t); // indices[]
610
611
612#ifndef SK_SCALAR_IS_FLOAT
613 SkDEBUGFAIL("SkScalar must be a float for these conversions to be valid");
614#endif
Ben Wagner6bbf68d2015-08-19 11:26:06 -0400615 std::unique_ptr<char[]> storage(new char[storageSize]);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400616 SkPoint* texs = (SkPoint*)storage.get();
617 uint16_t* indices = (uint16_t*)(texs + ptCount);
618
619 // cons up texture coordinates and indices
620 {
621 const SkScalar w = SkIntToScalar(bitmap.width());
622 const SkScalar h = SkIntToScalar(bitmap.height());
623 const SkScalar dx = w / meshWidth;
624 const SkScalar dy = h / meshHeight;
625
626 SkPoint* texsPtr = texs;
627 SkScalar y = 0;
628 for (int i = 0; i <= meshHeight; i++) {
629 if (i == meshHeight) {
630 y = h; // to ensure numerically we hit h exactly
631 }
632 SkScalar x = 0;
633 for (int j = 0; j < meshWidth; j++) {
634 texsPtr->set(x, y);
635 texsPtr += 1;
636 x += dx;
637 }
638 texsPtr->set(w, y);
639 texsPtr += 1;
640 y += dy;
641 }
642 SkASSERT(texsPtr - texs == ptCount);
643 }
644
645 // cons up indices
646 {
647 uint16_t* indexPtr = indices;
648 int index = 0;
649 for (int i = 0; i < meshHeight; i++) {
650 for (int j = 0; j < meshWidth; j++) {
651 // lower-left triangle
652 *indexPtr++ = index;
653 *indexPtr++ = index + meshWidth + 1;
654 *indexPtr++ = index + meshWidth + 2;
655 // upper-right triangle
656 *indexPtr++ = index;
657 *indexPtr++ = index + meshWidth + 2;
658 *indexPtr++ = index + 1;
659 // bump to the next cell
660 index += 1;
661 }
662 // bump to the next row
663 index += 1;
664 }
665 SkASSERT(indexPtr - indices == indexCount);
666 SkASSERT((char*)indexPtr - (char*)storage.get() == storageSize);
667 }
668
669 // double-check that we have legal indices
670#ifdef SK_DEBUG
671 {
672 for (int i = 0; i < indexCount; i++) {
673 SkASSERT((unsigned)indices[i] < (unsigned)ptCount);
674 }
675 }
676#endif
677
678 // cons-up a shader for the bitmap
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400679 SkPaint tmpPaint;
Derek Sollenberger8872b382014-06-23 14:13:53 -0400680 if (paint) {
681 tmpPaint = *paint;
682 }
683 SkShader* shader = SkShader::CreateBitmapShader(bitmap,
684 SkShader::kClamp_TileMode,
685 SkShader::kClamp_TileMode);
686 SkSafeUnref(tmpPaint.setShader(shader));
687
688 mCanvas->drawVertices(SkCanvas::kTriangles_VertexMode, ptCount, (SkPoint*)vertices,
689 texs, (const SkColor*)colors, NULL, indices,
690 indexCount, tmpPaint);
691}
692
Derek Sollenbergeredca3202015-07-10 13:56:39 -0400693void SkiaCanvas::drawNinePatch(const SkBitmap& bitmap, const Res_png_9patch& chunk,
694 float dstLeft, float dstTop, float dstRight, float dstBottom, const SkPaint* paint) {
695 SkRect bounds = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
696 NinePatch::Draw(mCanvas, bounds, bitmap, chunk, paint, nullptr);
697}
698
Derek Sollenberger8872b382014-06-23 14:13:53 -0400699// ----------------------------------------------------------------------------
700// Canvas draw operations: Text
701// ----------------------------------------------------------------------------
702
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400703void SkiaCanvas::drawText(const uint16_t* text, const float* positions, int count,
704 const SkPaint& paint, float x, float y,
Tom Hudson8dfaa492014-12-09 15:03:44 -0500705 float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
706 float totalAdvance) {
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400707 // Set align to left for drawing, as we don't want individual
708 // glyphs centered or right-aligned; the offset above takes
709 // care of all alignment.
710 SkPaint paintCopy(paint);
711 paintCopy.setTextAlign(SkPaint::kLeft_Align);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400712
Ben Wagnere3a40ea2015-08-19 15:49:37 -0400713 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats");
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400714 mCanvas->drawPosText(text, count << 1, reinterpret_cast<const SkPoint*>(positions), paintCopy);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400715}
716
717void SkiaCanvas::drawPosText(const uint16_t* text, const float* positions, int count, int posCount,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400718 const SkPaint& paint) {
Derek Sollenberger8872b382014-06-23 14:13:53 -0400719 SkPoint* posPtr = posCount > 0 ? new SkPoint[posCount] : NULL;
720 int indx;
721 for (indx = 0; indx < posCount; indx++) {
722 posPtr[indx].fX = positions[indx << 1];
723 posPtr[indx].fY = positions[(indx << 1) + 1];
724 }
725
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400726 SkPaint paintCopy(paint);
727 paintCopy.setTextEncoding(SkPaint::kUTF16_TextEncoding);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400728 mCanvas->drawPosText(text, count, posPtr, paintCopy);
729
730 delete[] posPtr;
731}
732
733void SkiaCanvas::drawTextOnPath(const uint16_t* glyphs, int count, const SkPath& path,
Derek Sollenbergeracb40992014-07-21 15:22:10 -0400734 float hOffset, float vOffset, const SkPaint& paint) {
Tom Hudson34e79c12015-04-14 11:34:39 -0400735 mCanvas->drawTextOnPathHV(glyphs, count << 1, path, hOffset, vOffset, paint);
Derek Sollenberger8872b382014-06-23 14:13:53 -0400736}
737
738} // namespace android