2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <SkClipStack.h>
23 #include <SkDrawFilter.h>
24 #include <SkGraphics.h>
27 #include <SkTemplates.h>
33 // Holds an SkCanvas reference plus additional native data.
34 class SkiaCanvas : public Canvas {
36 explicit SkiaCanvas(const SkBitmap& bitmap);
39 * Create a new SkiaCanvas.
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.
45 explicit SkiaCanvas(SkCanvas* canvas) : mCanvas(canvas) {
50 virtual SkCanvas* asSkCanvas() override {
54 virtual void setBitmap(const SkBitmap& bitmap) override;
56 virtual bool isOpaque() override;
57 virtual int width() override;
58 virtual int height() override;
60 virtual void setHighContrastText(bool highContrastText) override {
61 mHighContrastText = highContrastText;
63 virtual bool isHighContrastText() override { return mHighContrastText; }
65 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;
70 virtual int saveLayer(float left, float top, float right, float bottom,
71 const SkPaint* paint, SkCanvas::SaveFlags flags) override;
72 virtual int saveLayerAlpha(float left, float top, float right, float bottom,
73 int alpha, SkCanvas::SaveFlags flags) override;
75 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;
83 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;
91 virtual SkDrawFilter* getDrawFilter() override;
92 virtual void setDrawFilter(SkDrawFilter* drawFilter) override;
94 virtual void drawColor(int color, SkXfermode::Mode mode) override;
95 virtual void drawPaint(const SkPaint& paint) override;
97 virtual void drawPoint(float x, float y, const SkPaint& paint) override;
98 virtual void drawPoints(const float* points, int count, const SkPaint& paint) override;
99 virtual void drawLine(float startX, float startY, float stopX, float stopY,
100 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;
104 virtual void drawRegion(const SkRegion& region, const SkPaint& paint) override;
105 virtual void drawRoundRect(float left, float top, float right, float bottom,
106 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;
110 virtual void drawArc(float left, float top, float right, float bottom,
111 float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) override;
112 virtual void drawPath(const SkPath& path, const SkPaint& paint) override;
113 virtual void drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
114 const float* verts, const float* tex, const int* colors,
115 const uint16_t* indices, int indexCount, const SkPaint& paint) override;
117 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;
121 virtual void drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop,
122 float srcRight, float srcBottom, float dstLeft, float dstTop,
123 float dstRight, float dstBottom, const SkPaint* paint) override;
124 virtual void drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
125 const float* vertices, const int* colors, const SkPaint* paint) override;
126 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;
130 virtual void drawText(const uint16_t* text, const float* positions, int count,
131 const SkPaint& paint, float x, float y,
132 float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
133 float totalAdvance) override;
134 virtual void drawPosText(const uint16_t* text, const float* positions, int count,
135 int posCount, const SkPaint& paint) override;
136 virtual void drawTextOnPath(const uint16_t* glyphs, int count, const SkPath& path,
137 float hOffset, float vOffset, const SkPaint& paint) override;
139 virtual bool drawTextAbsolutePos() const override { return true; }
144 SkCanvas::SaveFlags saveFlags;
147 bool mHighContrastText = false;
149 void recordPartialSave(SkCanvas::SaveFlags flags);
150 void saveClipsForFrame(SkTArray<SkClipStack::Element>& clips, int frameSaveCount);
151 void applyClips(const SkTArray<SkClipStack::Element>& clips);
153 void drawPoints(const float* points, int count, const SkPaint& paint,
154 SkCanvas::PointMode mode);
155 void drawTextDecorations(float x, float y, float length, const SkPaint& paint);
157 SkAutoTUnref<SkCanvas> mCanvas;
158 std::unique_ptr<SkDeque> mSaveStack; // lazily allocated, tracks partial saves.
161 Canvas* Canvas::create_canvas(const SkBitmap& bitmap) {
162 return new SkiaCanvas(bitmap);
165 Canvas* Canvas::create_canvas(SkCanvas* skiaCanvas) {
166 return new SkiaCanvas(skiaCanvas);
169 SkiaCanvas::SkiaCanvas(const SkBitmap& bitmap) {
170 mCanvas.reset(new SkCanvas(bitmap));
173 // ----------------------------------------------------------------------------
174 // Canvas state operations: Replace Bitmap
175 // ----------------------------------------------------------------------------
177 class ClipCopier : public SkCanvas::ClipVisitor {
179 ClipCopier(SkCanvas* dstCanvas) : m_dstCanvas(dstCanvas) {}
181 virtual void clipRect(const SkRect& rect, SkRegion::Op op, bool antialias) {
182 m_dstCanvas->clipRect(rect, op, antialias);
184 virtual void clipRRect(const SkRRect& rrect, SkRegion::Op op, bool antialias) {
185 m_dstCanvas->clipRRect(rrect, op, antialias);
187 virtual void clipPath(const SkPath& path, SkRegion::Op op, bool antialias) {
188 m_dstCanvas->clipPath(path, op, antialias);
192 SkCanvas* m_dstCanvas;
195 void SkiaCanvas::setBitmap(const SkBitmap& bitmap) {
196 SkCanvas* newCanvas = new SkCanvas(bitmap);
198 if (!bitmap.isNull()) {
199 // Copy the canvas matrix & clip state.
200 newCanvas->setMatrix(mCanvas->getTotalMatrix());
202 ClipCopier copier(newCanvas);
203 mCanvas->replayClips(&copier);
206 // unrefs the existing canvas
207 mCanvas.reset(newCanvas);
209 // clean up the old save stack
210 mSaveStack.reset(NULL);
213 // ----------------------------------------------------------------------------
214 // Canvas state operations
215 // ----------------------------------------------------------------------------
217 bool SkiaCanvas::isOpaque() {
218 return mCanvas->imageInfo().isOpaque();
221 int SkiaCanvas::width() {
222 return mCanvas->imageInfo().width();
225 int SkiaCanvas::height() {
226 return mCanvas->imageInfo().height();
229 // ----------------------------------------------------------------------------
230 // Canvas state operations: Save (layer)
231 // ----------------------------------------------------------------------------
233 int SkiaCanvas::getSaveCount() const {
234 return mCanvas->getSaveCount();
237 int SkiaCanvas::save(SkCanvas::SaveFlags flags) {
238 int count = mCanvas->save();
239 recordPartialSave(flags);
243 void SkiaCanvas::restore() {
244 const SaveRec* rec = (NULL == mSaveStack.get())
246 : static_cast<SaveRec*>(mSaveStack->back());
247 int currentSaveCount = mCanvas->getSaveCount() - 1;
248 SkASSERT(NULL == rec || currentSaveCount >= rec->saveCount);
250 if (NULL == rec || rec->saveCount != currentSaveCount) {
251 // Fast path - no record for this frame.
256 bool preserveMatrix = !(rec->saveFlags & SkCanvas::kMatrix_SaveFlag);
257 bool preserveClip = !(rec->saveFlags & SkCanvas::kClip_SaveFlag);
259 SkMatrix savedMatrix;
260 if (preserveMatrix) {
261 savedMatrix = mCanvas->getTotalMatrix();
264 SkTArray<SkClipStack::Element> savedClips;
266 saveClipsForFrame(savedClips, currentSaveCount);
271 if (preserveMatrix) {
272 mCanvas->setMatrix(savedMatrix);
275 if (preserveClip && !savedClips.empty()) {
276 applyClips(savedClips);
279 mSaveStack->pop_back();
282 void SkiaCanvas::restoreToCount(int restoreCount) {
283 while (mCanvas->getSaveCount() > restoreCount) {
288 int SkiaCanvas::saveLayer(float left, float top, float right, float bottom,
289 const SkPaint* paint, SkCanvas::SaveFlags flags) {
290 SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
291 int count = mCanvas->saveLayer(&bounds, paint, flags | SkCanvas::kMatrixClip_SaveFlag);
292 recordPartialSave(flags);
296 int 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);
304 // ----------------------------------------------------------------------------
305 // functions to emulate legacy SaveFlags (i.e. independent matrix/clip flags)
306 // ----------------------------------------------------------------------------
308 void 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).
312 // Mask-out non canvas state bits.
313 flags = static_cast<SkCanvas::SaveFlags>(flags & SkCanvas::kMatrixClip_SaveFlag);
315 if (SkCanvas::kMatrixClip_SaveFlag == flags) {
316 // not a partial save.
320 if (NULL == mSaveStack.get()) {
321 mSaveStack.reset(new SkDeque(sizeof(struct SaveRec), 8));
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;
331 void 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.
339 SkASSERT(elem->getSaveCount() == frameSaveCount);
340 clips.push_back(*elem);
344 void SkiaCanvas::applyClips(const SkTArray<SkClipStack::Element>& clips) {
345 ClipCopier clipCopier(mCanvas);
347 // The clip stack stores clips in device space.
348 SkMatrix origMatrix = mCanvas->getTotalMatrix();
349 mCanvas->resetMatrix();
351 // We pushed the clips in reverse order.
352 for (int i = clips.count() - 1; i >= 0; --i) {
353 clips[i].replay(&clipCopier);
356 mCanvas->setMatrix(origMatrix);
359 // ----------------------------------------------------------------------------
360 // Canvas state operations: Matrix
361 // ----------------------------------------------------------------------------
363 void SkiaCanvas::getMatrix(SkMatrix* outMatrix) const {
364 *outMatrix = mCanvas->getTotalMatrix();
367 void SkiaCanvas::setMatrix(const SkMatrix& matrix) {
368 mCanvas->setMatrix(matrix);
371 void SkiaCanvas::concat(const SkMatrix& matrix) {
372 mCanvas->concat(matrix);
375 void SkiaCanvas::rotate(float degrees) {
376 mCanvas->rotate(degrees);
379 void SkiaCanvas::scale(float sx, float sy) {
380 mCanvas->scale(sx, sy);
383 void SkiaCanvas::skew(float sx, float sy) {
384 mCanvas->skew(sx, sy);
387 void SkiaCanvas::translate(float dx, float dy) {
388 mCanvas->translate(dx, dy);
391 // ----------------------------------------------------------------------------
392 // Canvas state operations: Clips
393 // ----------------------------------------------------------------------------
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)
399 bool SkiaCanvas::getClipBounds(SkRect* outRect) const {
401 if (!mCanvas->getClipDeviceBounds(&ibounds)) {
406 // if we can't invert the CTM, we can't return local clip bounds
407 if (!mCanvas->getTotalMatrix().invert(&inverse)) {
414 if (NULL != outRect) {
415 SkRect r = SkRect::Make(ibounds);
416 inverse.mapRect(outRect, r);
421 bool 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);
426 bool SkiaCanvas::quickRejectPath(const SkPath& path) const {
427 return mCanvas->quickReject(path);
430 bool 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);
433 return !mCanvas->isClipEmpty();
436 bool SkiaCanvas::clipPath(const SkPath* path, SkRegion::Op op) {
437 mCanvas->clipPath(*path, op);
438 return !mCanvas->isClipEmpty();
441 bool SkiaCanvas::clipRegion(const SkRegion* region, SkRegion::Op op) {
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);
450 mCanvas->clipRect(SkRect::MakeEmpty(), op);
452 return !mCanvas->isClipEmpty();
455 // ----------------------------------------------------------------------------
456 // Canvas state operations: Filters
457 // ----------------------------------------------------------------------------
459 SkDrawFilter* SkiaCanvas::getDrawFilter() {
460 return mCanvas->getDrawFilter();
463 void SkiaCanvas::setDrawFilter(SkDrawFilter* drawFilter) {
464 mCanvas->setDrawFilter(drawFilter);
467 // ----------------------------------------------------------------------------
468 // Canvas draw operations
469 // ----------------------------------------------------------------------------
471 void SkiaCanvas::drawColor(int color, SkXfermode::Mode mode) {
472 mCanvas->drawColor(color, mode);
475 void SkiaCanvas::drawPaint(const SkPaint& paint) {
476 mCanvas->drawPaint(paint);
479 // ----------------------------------------------------------------------------
480 // Canvas draw operations: Geometry
481 // ----------------------------------------------------------------------------
483 void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint,
484 SkCanvas::PointMode mode) {
485 // convert the floats into SkPoints
486 count >>= 1; // now it is the number of points
487 std::unique_ptr<SkPoint[]> pts(new SkPoint[count]);
488 for (int i = 0; i < count; i++) {
489 pts[i].set(points[0], points[1]);
492 mCanvas->drawPoints(mode, count, pts.get(), paint);
496 void SkiaCanvas::drawPoint(float x, float y, const SkPaint& paint) {
497 mCanvas->drawPoint(x, y, paint);
500 void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint) {
501 this->drawPoints(points, count, paint, SkCanvas::kPoints_PointMode);
504 void SkiaCanvas::drawLine(float startX, float startY, float stopX, float stopY,
505 const SkPaint& paint) {
506 mCanvas->drawLine(startX, startY, stopX, stopY, paint);
509 void SkiaCanvas::drawLines(const float* points, int count, const SkPaint& paint) {
510 this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode);
513 void SkiaCanvas::drawRect(float left, float top, float right, float bottom,
514 const SkPaint& paint) {
515 mCanvas->drawRectCoords(left, top, right, bottom, paint);
519 void SkiaCanvas::drawRegion(const SkRegion& region, const SkPaint& paint) {
520 SkRegion::Iterator it(region);
522 mCanvas->drawRect(SkRect::Make(it.rect()), paint);
527 void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom,
528 float rx, float ry, const SkPaint& paint) {
529 SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
530 mCanvas->drawRoundRect(rect, rx, ry, paint);
533 void SkiaCanvas::drawCircle(float x, float y, float radius, const SkPaint& paint) {
534 mCanvas->drawCircle(x, y, radius, paint);
537 void SkiaCanvas::drawOval(float left, float top, float right, float bottom, const SkPaint& paint) {
538 SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
539 mCanvas->drawOval(oval, paint);
542 void SkiaCanvas::drawArc(float left, float top, float right, float bottom,
543 float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) {
544 SkRect arc = SkRect::MakeLTRB(left, top, right, bottom);
545 mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, paint);
548 void SkiaCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
549 mCanvas->drawPath(path, paint);
552 void SkiaCanvas::drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
553 const float* verts, const float* texs, const int* colors,
554 const uint16_t* indices, int indexCount, const SkPaint& paint) {
555 #ifndef SK_SCALAR_IS_FLOAT
556 SkDEBUGFAIL("SkScalar must be a float for these conversions to be valid");
558 const int ptCount = vertexCount >> 1;
559 mCanvas->drawVertices(vertexMode, ptCount, (SkPoint*)verts, (SkPoint*)texs,
560 (SkColor*)colors, NULL, indices, indexCount, paint);
563 // ----------------------------------------------------------------------------
564 // Canvas draw operations: Bitmaps
565 // ----------------------------------------------------------------------------
567 void SkiaCanvas::drawBitmap(const SkBitmap& bitmap, float left, float top, const SkPaint* paint) {
568 mCanvas->drawBitmap(bitmap, left, top, paint);
571 void SkiaCanvas::drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix, const SkPaint* paint) {
572 SkAutoCanvasRestore acr(mCanvas, true);
573 mCanvas->concat(matrix);
574 mCanvas->drawBitmap(bitmap, 0, 0, paint);
577 void SkiaCanvas::drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop,
578 float srcRight, float srcBottom, float dstLeft, float dstTop,
579 float dstRight, float dstBottom, const SkPaint* paint) {
580 SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
581 SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
582 mCanvas->drawBitmapRect(bitmap, srcRect, dstRect, paint);
585 void SkiaCanvas::drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
586 const float* vertices, const int* colors, const SkPaint* paint) {
588 const int ptCount = (meshWidth + 1) * (meshHeight + 1);
589 const int indexCount = meshWidth * meshHeight * 6;
591 /* Our temp storage holds 2 or 3 arrays.
592 texture points [ptCount * sizeof(SkPoint)]
593 optionally vertex points [ptCount * sizeof(SkPoint)] if we need a
594 copy to convert from float to fixed
595 indices [ptCount * sizeof(uint16_t)]
597 ssize_t storageSize = ptCount * sizeof(SkPoint); // texs[]
598 storageSize += indexCount * sizeof(uint16_t); // indices[]
601 #ifndef SK_SCALAR_IS_FLOAT
602 SkDEBUGFAIL("SkScalar must be a float for these conversions to be valid");
604 std::unique_ptr<char[]> storage(new char[storageSize]);
605 SkPoint* texs = (SkPoint*)storage.get();
606 uint16_t* indices = (uint16_t*)(texs + ptCount);
608 // cons up texture coordinates and indices
610 const SkScalar w = SkIntToScalar(bitmap.width());
611 const SkScalar h = SkIntToScalar(bitmap.height());
612 const SkScalar dx = w / meshWidth;
613 const SkScalar dy = h / meshHeight;
615 SkPoint* texsPtr = texs;
617 for (int i = 0; i <= meshHeight; i++) {
618 if (i == meshHeight) {
619 y = h; // to ensure numerically we hit h exactly
622 for (int j = 0; j < meshWidth; j++) {
631 SkASSERT(texsPtr - texs == ptCount);
636 uint16_t* indexPtr = indices;
638 for (int i = 0; i < meshHeight; i++) {
639 for (int j = 0; j < meshWidth; j++) {
640 // lower-left triangle
642 *indexPtr++ = index + meshWidth + 1;
643 *indexPtr++ = index + meshWidth + 2;
644 // upper-right triangle
646 *indexPtr++ = index + meshWidth + 2;
647 *indexPtr++ = index + 1;
648 // bump to the next cell
651 // bump to the next row
654 SkASSERT(indexPtr - indices == indexCount);
655 SkASSERT((char*)indexPtr - (char*)storage.get() == storageSize);
658 // double-check that we have legal indices
661 for (int i = 0; i < indexCount; i++) {
662 SkASSERT((unsigned)indices[i] < (unsigned)ptCount);
667 // cons-up a shader for the bitmap
672 SkShader* shader = SkShader::CreateBitmapShader(bitmap,
673 SkShader::kClamp_TileMode,
674 SkShader::kClamp_TileMode);
675 SkSafeUnref(tmpPaint.setShader(shader));
677 mCanvas->drawVertices(SkCanvas::kTriangles_VertexMode, ptCount, (SkPoint*)vertices,
678 texs, (const SkColor*)colors, NULL, indices,
679 indexCount, tmpPaint);
682 void SkiaCanvas::drawNinePatch(const SkBitmap& bitmap, const Res_png_9patch& chunk,
683 float dstLeft, float dstTop, float dstRight, float dstBottom, const SkPaint* paint) {
684 SkRect bounds = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
685 NinePatch::Draw(mCanvas, bounds, bitmap, chunk, paint, nullptr);
688 // ----------------------------------------------------------------------------
689 // Canvas draw operations: Text
690 // ----------------------------------------------------------------------------
692 void SkiaCanvas::drawText(const uint16_t* text, const float* positions, int count,
693 const SkPaint& paint, float x, float y,
694 float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
695 float totalAdvance) {
696 // Set align to left for drawing, as we don't want individual
697 // glyphs centered or right-aligned; the offset above takes
698 // care of all alignment.
699 SkPaint paintCopy(paint);
700 paintCopy.setTextAlign(SkPaint::kLeft_Align);
702 static_assert(sizeof(SkPoint) == sizeof(float)*2, "SkPoint is no longer two floats");
703 mCanvas->drawPosText(text, count << 1, reinterpret_cast<const SkPoint*>(positions), paintCopy);
706 void SkiaCanvas::drawPosText(const uint16_t* text, const float* positions, int count, int posCount,
707 const SkPaint& paint) {
708 SkPoint* posPtr = posCount > 0 ? new SkPoint[posCount] : NULL;
710 for (indx = 0; indx < posCount; indx++) {
711 posPtr[indx].fX = positions[indx << 1];
712 posPtr[indx].fY = positions[(indx << 1) + 1];
715 SkPaint paintCopy(paint);
716 paintCopy.setTextEncoding(SkPaint::kUTF16_TextEncoding);
717 mCanvas->drawPosText(text, count, posPtr, paintCopy);
722 void SkiaCanvas::drawTextOnPath(const uint16_t* glyphs, int count, const SkPath& path,
723 float hOffset, float vOffset, const SkPaint& paint) {
724 mCanvas->drawTextOnPathHV(glyphs, count << 1, path, hOffset, vOffset, paint);
727 } // namespace android