SkiaCanvas.cpp revision c1b33d665c8caf5760f68c45c6ca0baa649b832a
1/*
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
17#include "Canvas.h"
18
19#include <SkCanvas.h>
20#include <SkClipStack.h>
21#include <SkDevice.h>
22#include <SkDeque.h>
23#include <SkDrawFilter.h>
24#include <SkGraphics.h>
25#include <SkShader.h>
26#include <SkTArray.h>
27#include <SkTemplates.h>
28
29namespace android {
30
31// Holds an SkCanvas reference plus additional native data.
32class SkiaCanvas : public Canvas {
33public:
34    explicit SkiaCanvas(const SkBitmap& bitmap);
35
36    /**
37     *  Create a new SkiaCanvas.
38     *
39     *  @param canvas SkCanvas to handle calls made to this SkiaCanvas. Must
40     *      not be NULL. This constructor will ref() the SkCanvas, and unref()
41     *      it in its destructor.
42     */
43    explicit SkiaCanvas(SkCanvas* canvas) : mCanvas(canvas) {
44        SkASSERT(canvas);
45        canvas->ref();
46    }
47
48    virtual SkCanvas* asSkCanvas() override {
49        return mCanvas.get();
50    }
51
52    virtual void setBitmap(const SkBitmap& bitmap) override;
53
54    virtual bool isOpaque() override;
55    virtual int width() override;
56    virtual int height() override;
57
58    virtual int getSaveCount() const override;
59    virtual int save(SkCanvas::SaveFlags flags) override;
60    virtual void restore() override;
61    virtual void restoreToCount(int saveCount) override;
62
63    virtual int saveLayer(float left, float top, float right, float bottom,
64                const SkPaint* paint, SkCanvas::SaveFlags flags) override;
65    virtual int saveLayerAlpha(float left, float top, float right, float bottom,
66            int alpha, SkCanvas::SaveFlags flags) override;
67
68    virtual void getMatrix(SkMatrix* outMatrix) const override;
69    virtual void setMatrix(const SkMatrix& matrix) override;
70    virtual void concat(const SkMatrix& matrix) override;
71    virtual void rotate(float degrees) override;
72    virtual void scale(float sx, float sy) override;
73    virtual void skew(float sx, float sy) override;
74    virtual void translate(float dx, float dy) override;
75
76    virtual bool getClipBounds(SkRect* outRect) const override;
77    virtual bool quickRejectRect(float left, float top, float right, float bottom) const override;
78    virtual bool quickRejectPath(const SkPath& path) const override;
79    virtual bool clipRect(float left, float top, float right, float bottom,
80            SkRegion::Op op) override;
81    virtual bool clipPath(const SkPath* path, SkRegion::Op op) override;
82    virtual bool clipRegion(const SkRegion* region, SkRegion::Op op) override;
83
84    virtual SkDrawFilter* getDrawFilter() override;
85    virtual void setDrawFilter(SkDrawFilter* drawFilter) override;
86
87    virtual void drawColor(int color, SkXfermode::Mode mode) override;
88    virtual void drawPaint(const SkPaint& paint) override;
89
90    virtual void drawPoint(float x, float y, const SkPaint& paint) override;
91    virtual void drawPoints(const float* points, int count, const SkPaint& paint) override;
92    virtual void drawLine(float startX, float startY, float stopX, float stopY,
93            const SkPaint& paint) override;
94    virtual void drawLines(const float* points, int count, const SkPaint& paint) override;
95    virtual void drawRect(float left, float top, float right, float bottom,
96            const SkPaint& paint) override;
97    virtual void drawRoundRect(float left, float top, float right, float bottom,
98            float rx, float ry, const SkPaint& paint) override;
99    virtual void drawCircle(float x, float y, float radius, const SkPaint& paint) override;
100    virtual void drawOval(float left, float top, float right, float bottom,
101            const SkPaint& paint) override;
102    virtual void drawArc(float left, float top, float right, float bottom,
103            float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) override;
104    virtual void drawPath(const SkPath& path, const SkPaint& paint) override;
105    virtual void drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
106            const float* verts, const float* tex, const int* colors,
107            const uint16_t* indices, int indexCount, const SkPaint& paint) override;
108
109    virtual void drawBitmap(const SkBitmap& bitmap, float left, float top,
110            const SkPaint* paint) override;
111    virtual void drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix,
112            const SkPaint* paint) override;
113    virtual void drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop,
114            float srcRight, float srcBottom, float dstLeft, float dstTop,
115            float dstRight, float dstBottom, const SkPaint* paint) override;
116    virtual void drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
117            const float* vertices, const int* colors, const SkPaint* paint) override;
118
119    virtual void drawText(const uint16_t* text, const float* positions, int count,
120            const SkPaint& paint, float x, float y,
121            float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
122            float totalAdvance) override;
123    virtual void drawPosText(const uint16_t* text, const float* positions, int count,
124            int posCount, const SkPaint& paint) override;
125    virtual void drawTextOnPath(const uint16_t* glyphs, int count, const SkPath& path,
126            float hOffset, float vOffset, const SkPaint& paint) override;
127
128    virtual bool drawTextAbsolutePos() const  override { return true; }
129
130private:
131    struct SaveRec {
132        int                 saveCount;
133        SkCanvas::SaveFlags saveFlags;
134    };
135
136    void recordPartialSave(SkCanvas::SaveFlags flags);
137    void saveClipsForFrame(SkTArray<SkClipStack::Element>& clips, int frameSaveCount);
138    void applyClips(const SkTArray<SkClipStack::Element>& clips);
139
140    void drawPoints(const float* points, int count, const SkPaint& paint,
141                    SkCanvas::PointMode mode);
142    void drawTextDecorations(float x, float y, float length, const SkPaint& paint);
143
144    SkAutoTUnref<SkCanvas> mCanvas;
145    SkAutoTDelete<SkDeque> mSaveStack; // lazily allocated, tracks partial saves.
146};
147
148Canvas* Canvas::create_canvas(const SkBitmap& bitmap) {
149    return new SkiaCanvas(bitmap);
150}
151
152Canvas* Canvas::create_canvas(SkCanvas* skiaCanvas) {
153    return new SkiaCanvas(skiaCanvas);
154}
155
156SkiaCanvas::SkiaCanvas(const SkBitmap& bitmap) {
157    mCanvas.reset(new SkCanvas(bitmap));
158}
159
160// ----------------------------------------------------------------------------
161// Canvas state operations: Replace Bitmap
162// ----------------------------------------------------------------------------
163
164class ClipCopier : public SkCanvas::ClipVisitor {
165public:
166    ClipCopier(SkCanvas* dstCanvas) : m_dstCanvas(dstCanvas) {}
167
168    virtual void clipRect(const SkRect& rect, SkRegion::Op op, bool antialias) {
169        m_dstCanvas->clipRect(rect, op, antialias);
170    }
171    virtual void clipRRect(const SkRRect& rrect, SkRegion::Op op, bool antialias) {
172        m_dstCanvas->clipRRect(rrect, op, antialias);
173    }
174    virtual void clipPath(const SkPath& path, SkRegion::Op op, bool antialias) {
175        m_dstCanvas->clipPath(path, op, antialias);
176    }
177
178private:
179    SkCanvas* m_dstCanvas;
180};
181
182void SkiaCanvas::setBitmap(const SkBitmap& bitmap) {
183    SkCanvas* newCanvas = new SkCanvas(bitmap);
184    SkASSERT(newCanvas);
185
186    if (!bitmap.isNull()) {
187        // Copy the canvas matrix & clip state.
188        newCanvas->setMatrix(mCanvas->getTotalMatrix());
189        if (NULL != mCanvas->getDevice() && NULL != newCanvas->getDevice()) {
190            ClipCopier copier(newCanvas);
191            mCanvas->replayClips(&copier);
192        }
193    }
194
195    // unrefs the existing canvas
196    mCanvas.reset(newCanvas);
197
198    // clean up the old save stack
199    mSaveStack.reset(NULL);
200}
201
202// ----------------------------------------------------------------------------
203// Canvas state operations
204// ----------------------------------------------------------------------------
205
206bool SkiaCanvas::isOpaque() {
207    return mCanvas->getDevice()->accessBitmap(false).isOpaque();
208}
209
210int SkiaCanvas::width() {
211    return mCanvas->getBaseLayerSize().width();
212}
213
214int SkiaCanvas::height() {
215    return mCanvas->getBaseLayerSize().height();
216}
217
218// ----------------------------------------------------------------------------
219// Canvas state operations: Save (layer)
220// ----------------------------------------------------------------------------
221
222int SkiaCanvas::getSaveCount() const {
223    return mCanvas->getSaveCount();
224}
225
226int SkiaCanvas::save(SkCanvas::SaveFlags flags) {
227    int count = mCanvas->save();
228    recordPartialSave(flags);
229    return count;
230}
231
232void SkiaCanvas::restore() {
233    const SaveRec* rec = (NULL == mSaveStack.get())
234            ? NULL
235            : static_cast<SaveRec*>(mSaveStack->back());
236    int currentSaveCount = mCanvas->getSaveCount() - 1;
237    SkASSERT(NULL == rec || currentSaveCount >= rec->saveCount);
238
239    if (NULL == rec || rec->saveCount != currentSaveCount) {
240        // Fast path - no record for this frame.
241        mCanvas->restore();
242        return;
243    }
244
245    bool preserveMatrix = !(rec->saveFlags & SkCanvas::kMatrix_SaveFlag);
246    bool preserveClip   = !(rec->saveFlags & SkCanvas::kClip_SaveFlag);
247
248    SkMatrix savedMatrix;
249    if (preserveMatrix) {
250        savedMatrix = mCanvas->getTotalMatrix();
251    }
252
253    SkTArray<SkClipStack::Element> savedClips;
254    if (preserveClip) {
255        saveClipsForFrame(savedClips, currentSaveCount);
256    }
257
258    mCanvas->restore();
259
260    if (preserveMatrix) {
261        mCanvas->setMatrix(savedMatrix);
262    }
263
264    if (preserveClip && !savedClips.empty()) {
265        applyClips(savedClips);
266    }
267
268    mSaveStack->pop_back();
269}
270
271void SkiaCanvas::restoreToCount(int restoreCount) {
272    while (mCanvas->getSaveCount() > restoreCount) {
273        this->restore();
274    }
275}
276
277int SkiaCanvas::saveLayer(float left, float top, float right, float bottom,
278            const SkPaint* paint, SkCanvas::SaveFlags flags) {
279    SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
280    int count = mCanvas->saveLayer(&bounds, paint, flags | SkCanvas::kMatrixClip_SaveFlag);
281    recordPartialSave(flags);
282    return count;
283}
284
285int SkiaCanvas::saveLayerAlpha(float left, float top, float right, float bottom,
286        int alpha, SkCanvas::SaveFlags flags) {
287    SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
288    int count = mCanvas->saveLayerAlpha(&bounds, alpha, flags | SkCanvas::kMatrixClip_SaveFlag);
289    recordPartialSave(flags);
290    return count;
291}
292
293// ----------------------------------------------------------------------------
294// functions to emulate legacy SaveFlags (i.e. independent matrix/clip flags)
295// ----------------------------------------------------------------------------
296
297void SkiaCanvas::recordPartialSave(SkCanvas::SaveFlags flags) {
298    // A partial save is a save operation which doesn't capture the full canvas state.
299    // (either kMatrix_SaveFlags or kClip_SaveFlag is missing).
300
301    // Mask-out non canvas state bits.
302    flags = static_cast<SkCanvas::SaveFlags>(flags & SkCanvas::kMatrixClip_SaveFlag);
303
304    if (SkCanvas::kMatrixClip_SaveFlag == flags) {
305        // not a partial save.
306        return;
307    }
308
309    if (NULL == mSaveStack.get()) {
310        mSaveStack.reset(SkNEW_ARGS(SkDeque, (sizeof(struct SaveRec), 8)));
311    }
312
313    SaveRec* rec = static_cast<SaveRec*>(mSaveStack->push_back());
314    // Store the save counter in the SkClipStack domain.
315    // (0-based, equal to the number of save ops on the stack).
316    rec->saveCount = mCanvas->getSaveCount() - 1;
317    rec->saveFlags = flags;
318}
319
320void SkiaCanvas::saveClipsForFrame(SkTArray<SkClipStack::Element>& clips, int frameSaveCount) {
321    SkClipStack::Iter clipIterator(*mCanvas->getClipStack(),
322                                   SkClipStack::Iter::kTop_IterStart);
323    while (const SkClipStack::Element* elem = clipIterator.next()) {
324        if (elem->getSaveCount() < frameSaveCount) {
325            // done with the current frame.
326            break;
327        }
328        SkASSERT(elem->getSaveCount() == frameSaveCount);
329        clips.push_back(*elem);
330    }
331}
332
333void SkiaCanvas::applyClips(const SkTArray<SkClipStack::Element>& clips) {
334    ClipCopier clipCopier(mCanvas);
335
336    // The clip stack stores clips in device space.
337    SkMatrix origMatrix = mCanvas->getTotalMatrix();
338    mCanvas->resetMatrix();
339
340    // We pushed the clips in reverse order.
341    for (int i = clips.count() - 1; i >= 0; --i) {
342        clips[i].replay(&clipCopier);
343    }
344
345    mCanvas->setMatrix(origMatrix);
346}
347
348// ----------------------------------------------------------------------------
349// Canvas state operations: Matrix
350// ----------------------------------------------------------------------------
351
352void SkiaCanvas::getMatrix(SkMatrix* outMatrix) const {
353    *outMatrix = mCanvas->getTotalMatrix();
354}
355
356void SkiaCanvas::setMatrix(const SkMatrix& matrix) {
357    mCanvas->setMatrix(matrix);
358}
359
360void SkiaCanvas::concat(const SkMatrix& matrix) {
361    mCanvas->concat(matrix);
362}
363
364void SkiaCanvas::rotate(float degrees) {
365    mCanvas->rotate(degrees);
366}
367
368void SkiaCanvas::scale(float sx, float sy) {
369    mCanvas->scale(sx, sy);
370}
371
372void SkiaCanvas::skew(float sx, float sy) {
373    mCanvas->skew(sx, sy);
374}
375
376void SkiaCanvas::translate(float dx, float dy) {
377    mCanvas->translate(dx, dy);
378}
379
380// ----------------------------------------------------------------------------
381// Canvas state operations: Clips
382// ----------------------------------------------------------------------------
383
384// This function is a mirror of SkCanvas::getClipBounds except that it does
385// not outset the edge of the clip to account for anti-aliasing. There is
386// a skia bug to investigate pushing this logic into back into skia.
387// (see https://code.google.com/p/skia/issues/detail?id=1303)
388bool SkiaCanvas::getClipBounds(SkRect* outRect) const {
389    SkIRect ibounds;
390    if (!mCanvas->getClipDeviceBounds(&ibounds)) {
391        return false;
392    }
393
394    SkMatrix inverse;
395    // if we can't invert the CTM, we can't return local clip bounds
396    if (!mCanvas->getTotalMatrix().invert(&inverse)) {
397        if (outRect) {
398            outRect->setEmpty();
399        }
400        return false;
401    }
402
403    if (NULL != outRect) {
404        SkRect r = SkRect::Make(ibounds);
405        inverse.mapRect(outRect, r);
406    }
407    return true;
408}
409
410bool SkiaCanvas::quickRejectRect(float left, float top, float right, float bottom) const {
411    SkRect bounds = SkRect::MakeLTRB(left, top, right, bottom);
412    return mCanvas->quickReject(bounds);
413}
414
415bool SkiaCanvas::quickRejectPath(const SkPath& path) const {
416    return mCanvas->quickReject(path);
417}
418
419bool SkiaCanvas::clipRect(float left, float top, float right, float bottom, SkRegion::Op op) {
420    SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
421    mCanvas->clipRect(rect, op);
422    return mCanvas->isClipEmpty();
423}
424
425bool SkiaCanvas::clipPath(const SkPath* path, SkRegion::Op op) {
426    mCanvas->clipPath(*path, op);
427    return mCanvas->isClipEmpty();
428}
429
430bool SkiaCanvas::clipRegion(const SkRegion* region, SkRegion::Op op) {
431    SkPath rgnPath;
432    if (region->getBoundaryPath(&rgnPath)) {
433        // The region is specified in device space.
434        SkMatrix savedMatrix = mCanvas->getTotalMatrix();
435        mCanvas->resetMatrix();
436        mCanvas->clipPath(rgnPath, op);
437        mCanvas->setMatrix(savedMatrix);
438    } else {
439        mCanvas->clipRect(SkRect::MakeEmpty(), op);
440    }
441    return mCanvas->isClipEmpty();
442}
443
444// ----------------------------------------------------------------------------
445// Canvas state operations: Filters
446// ----------------------------------------------------------------------------
447
448SkDrawFilter* SkiaCanvas::getDrawFilter() {
449    return mCanvas->getDrawFilter();
450}
451
452void SkiaCanvas::setDrawFilter(SkDrawFilter* drawFilter) {
453    mCanvas->setDrawFilter(drawFilter);
454}
455
456// ----------------------------------------------------------------------------
457// Canvas draw operations
458// ----------------------------------------------------------------------------
459
460void SkiaCanvas::drawColor(int color, SkXfermode::Mode mode) {
461    mCanvas->drawColor(color, mode);
462}
463
464void SkiaCanvas::drawPaint(const SkPaint& paint) {
465    mCanvas->drawPaint(paint);
466}
467
468// ----------------------------------------------------------------------------
469// Canvas draw operations: Geometry
470// ----------------------------------------------------------------------------
471
472void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint,
473                            SkCanvas::PointMode mode) {
474    // convert the floats into SkPoints
475    count >>= 1;    // now it is the number of points
476    SkAutoSTMalloc<32, SkPoint> storage(count);
477    SkPoint* pts = storage.get();
478    for (int i = 0; i < count; i++) {
479        pts[i].set(points[0], points[1]);
480        points += 2;
481    }
482    mCanvas->drawPoints(mode, count, pts, paint);
483}
484
485
486void SkiaCanvas::drawPoint(float x, float y, const SkPaint& paint) {
487    mCanvas->drawPoint(x, y, paint);
488}
489
490void SkiaCanvas::drawPoints(const float* points, int count, const SkPaint& paint) {
491    this->drawPoints(points, count, paint, SkCanvas::kPoints_PointMode);
492}
493
494void SkiaCanvas::drawLine(float startX, float startY, float stopX, float stopY,
495                          const SkPaint& paint) {
496    mCanvas->drawLine(startX, startY, stopX, stopY, paint);
497}
498
499void SkiaCanvas::drawLines(const float* points, int count, const SkPaint& paint) {
500    this->drawPoints(points, count, paint, SkCanvas::kLines_PointMode);
501}
502
503void SkiaCanvas::drawRect(float left, float top, float right, float bottom,
504        const SkPaint& paint) {
505    mCanvas->drawRectCoords(left, top, right, bottom, paint);
506
507}
508
509void SkiaCanvas::drawRoundRect(float left, float top, float right, float bottom,
510        float rx, float ry, const SkPaint& paint) {
511    SkRect rect = SkRect::MakeLTRB(left, top, right, bottom);
512    mCanvas->drawRoundRect(rect, rx, ry, paint);
513}
514
515void SkiaCanvas::drawCircle(float x, float y, float radius, const SkPaint& paint) {
516    mCanvas->drawCircle(x, y, radius, paint);
517}
518
519void SkiaCanvas::drawOval(float left, float top, float right, float bottom, const SkPaint& paint) {
520    SkRect oval = SkRect::MakeLTRB(left, top, right, bottom);
521    mCanvas->drawOval(oval, paint);
522}
523
524void SkiaCanvas::drawArc(float left, float top, float right, float bottom,
525        float startAngle, float sweepAngle, bool useCenter, const SkPaint& paint) {
526    SkRect arc = SkRect::MakeLTRB(left, top, right, bottom);
527    mCanvas->drawArc(arc, startAngle, sweepAngle, useCenter, paint);
528}
529
530void SkiaCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
531    mCanvas->drawPath(path, paint);
532}
533
534void SkiaCanvas::drawVertices(SkCanvas::VertexMode vertexMode, int vertexCount,
535                              const float* verts, const float* texs, const int* colors,
536                              const uint16_t* indices, int indexCount, const SkPaint& paint) {
537#ifndef SK_SCALAR_IS_FLOAT
538    SkDEBUGFAIL("SkScalar must be a float for these conversions to be valid");
539#endif
540    const int ptCount = vertexCount >> 1;
541    mCanvas->drawVertices(vertexMode, ptCount, (SkPoint*)verts, (SkPoint*)texs,
542                          (SkColor*)colors, NULL, indices, indexCount, paint);
543}
544
545// ----------------------------------------------------------------------------
546// Canvas draw operations: Bitmaps
547// ----------------------------------------------------------------------------
548
549void SkiaCanvas::drawBitmap(const SkBitmap& bitmap, float left, float top, const SkPaint* paint) {
550    mCanvas->drawBitmap(bitmap, left, top, paint);
551}
552
553void SkiaCanvas::drawBitmap(const SkBitmap& bitmap, const SkMatrix& matrix, const SkPaint* paint) {
554    SkAutoCanvasRestore acr(mCanvas, true);
555    mCanvas->concat(matrix);
556    mCanvas->drawBitmap(bitmap, 0, 0, paint);
557}
558
559void SkiaCanvas::drawBitmap(const SkBitmap& bitmap, float srcLeft, float srcTop,
560                            float srcRight, float srcBottom, float dstLeft, float dstTop,
561                            float dstRight, float dstBottom, const SkPaint* paint) {
562    SkRect srcRect = SkRect::MakeLTRB(srcLeft, srcTop, srcRight, srcBottom);
563    SkRect dstRect = SkRect::MakeLTRB(dstLeft, dstTop, dstRight, dstBottom);
564    mCanvas->drawBitmapRectToRect(bitmap, &srcRect, dstRect, paint);
565}
566
567void SkiaCanvas::drawBitmapMesh(const SkBitmap& bitmap, int meshWidth, int meshHeight,
568        const float* vertices, const int* colors, const SkPaint* paint) {
569
570    const int ptCount = (meshWidth + 1) * (meshHeight + 1);
571    const int indexCount = meshWidth * meshHeight * 6;
572
573    /*  Our temp storage holds 2 or 3 arrays.
574        texture points [ptCount * sizeof(SkPoint)]
575        optionally vertex points [ptCount * sizeof(SkPoint)] if we need a
576            copy to convert from float to fixed
577        indices [ptCount * sizeof(uint16_t)]
578    */
579    ssize_t storageSize = ptCount * sizeof(SkPoint); // texs[]
580    storageSize += indexCount * sizeof(uint16_t);  // indices[]
581
582
583#ifndef SK_SCALAR_IS_FLOAT
584    SkDEBUGFAIL("SkScalar must be a float for these conversions to be valid");
585#endif
586    SkAutoMalloc storage(storageSize);
587    SkPoint* texs = (SkPoint*)storage.get();
588    uint16_t* indices = (uint16_t*)(texs + ptCount);
589
590    // cons up texture coordinates and indices
591    {
592        const SkScalar w = SkIntToScalar(bitmap.width());
593        const SkScalar h = SkIntToScalar(bitmap.height());
594        const SkScalar dx = w / meshWidth;
595        const SkScalar dy = h / meshHeight;
596
597        SkPoint* texsPtr = texs;
598        SkScalar y = 0;
599        for (int i = 0; i <= meshHeight; i++) {
600            if (i == meshHeight) {
601                y = h;  // to ensure numerically we hit h exactly
602            }
603            SkScalar x = 0;
604            for (int j = 0; j < meshWidth; j++) {
605                texsPtr->set(x, y);
606                texsPtr += 1;
607                x += dx;
608            }
609            texsPtr->set(w, y);
610            texsPtr += 1;
611            y += dy;
612        }
613        SkASSERT(texsPtr - texs == ptCount);
614    }
615
616    // cons up indices
617    {
618        uint16_t* indexPtr = indices;
619        int index = 0;
620        for (int i = 0; i < meshHeight; i++) {
621            for (int j = 0; j < meshWidth; j++) {
622                // lower-left triangle
623                *indexPtr++ = index;
624                *indexPtr++ = index + meshWidth + 1;
625                *indexPtr++ = index + meshWidth + 2;
626                // upper-right triangle
627                *indexPtr++ = index;
628                *indexPtr++ = index + meshWidth + 2;
629                *indexPtr++ = index + 1;
630                // bump to the next cell
631                index += 1;
632            }
633            // bump to the next row
634            index += 1;
635        }
636        SkASSERT(indexPtr - indices == indexCount);
637        SkASSERT((char*)indexPtr - (char*)storage.get() == storageSize);
638    }
639
640    // double-check that we have legal indices
641#ifdef SK_DEBUG
642    {
643        for (int i = 0; i < indexCount; i++) {
644            SkASSERT((unsigned)indices[i] < (unsigned)ptCount);
645        }
646    }
647#endif
648
649    // cons-up a shader for the bitmap
650    SkPaint tmpPaint;
651    if (paint) {
652        tmpPaint = *paint;
653    }
654    SkShader* shader = SkShader::CreateBitmapShader(bitmap,
655                                                    SkShader::kClamp_TileMode,
656                                                    SkShader::kClamp_TileMode);
657    SkSafeUnref(tmpPaint.setShader(shader));
658
659    mCanvas->drawVertices(SkCanvas::kTriangles_VertexMode, ptCount, (SkPoint*)vertices,
660                         texs, (const SkColor*)colors, NULL, indices,
661                         indexCount, tmpPaint);
662}
663
664// ----------------------------------------------------------------------------
665// Canvas draw operations: Text
666// ----------------------------------------------------------------------------
667
668void SkiaCanvas::drawText(const uint16_t* text, const float* positions, int count,
669        const SkPaint& paint, float x, float y,
670        float boundsLeft, float boundsTop, float boundsRight, float boundsBottom,
671        float totalAdvance) {
672    // Set align to left for drawing, as we don't want individual
673    // glyphs centered or right-aligned; the offset above takes
674    // care of all alignment.
675    SkPaint paintCopy(paint);
676    paintCopy.setTextAlign(SkPaint::kLeft_Align);
677
678    SK_COMPILE_ASSERT(sizeof(SkPoint) == sizeof(float)*2, SkPoint_is_no_longer_2_floats);
679    mCanvas->drawPosText(text, count << 1, reinterpret_cast<const SkPoint*>(positions), paintCopy);
680}
681
682void SkiaCanvas::drawPosText(const uint16_t* text, const float* positions, int count, int posCount,
683        const SkPaint& paint) {
684    SkPoint* posPtr = posCount > 0 ? new SkPoint[posCount] : NULL;
685    int indx;
686    for (indx = 0; indx < posCount; indx++) {
687        posPtr[indx].fX = positions[indx << 1];
688        posPtr[indx].fY = positions[(indx << 1) + 1];
689    }
690
691    SkPaint paintCopy(paint);
692    paintCopy.setTextEncoding(SkPaint::kUTF16_TextEncoding);
693    mCanvas->drawPosText(text, count, posPtr, paintCopy);
694
695    delete[] posPtr;
696}
697
698void SkiaCanvas::drawTextOnPath(const uint16_t* glyphs, int count, const SkPath& path,
699        float hOffset, float vOffset, const SkPaint& paint) {
700    mCanvas->drawTextOnPathHV(glyphs, count << 1, path, hOffset, vOffset, paint);
701}
702
703} // namespace android
704