Canvas.java revision 95d49149cc7fff87a18449fa661454c1b5ddbd30
1/*
2 * Copyright (C) 2006 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
17package android.graphics;
18
19import android.text.GraphicsOperations;
20import android.text.SpannableString;
21import android.text.SpannedString;
22import android.text.TextUtils;
23
24import javax.microedition.khronos.opengles.GL;
25
26/**
27 * The Canvas class holds the "draw" calls. To draw something, you need
28 * 4 basic components: A Bitmap to hold the pixels, a Canvas to host
29 * the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect,
30 * Path, text, Bitmap), and a paint (to describe the colors and styles for the
31 * drawing).
32 *
33 * <div class="special reference">
34 * <h3>Developer Guides</h3>
35 * <p>For more information about how to use Canvas, read the
36 * <a href="{@docRoot}guide/topics/graphics/2d-graphics.html">
37 * Canvas and Drawables</a> developer guide.</p></div>
38 */
39public class Canvas {
40
41    // assigned in constructors or setBitmap, freed in finalizer
42    private long mNativeCanvasWrapper;
43
44    /** @hide */
45    public long getNativeCanvasWrapper() {
46        return mNativeCanvasWrapper;
47    }
48
49    // may be null
50    private Bitmap mBitmap;
51
52    // optional field set by the caller
53    private DrawFilter mDrawFilter;
54
55    /**
56     * @hide
57     */
58    protected int mDensity = Bitmap.DENSITY_NONE;
59
60    /**
61     * Used to determine when compatibility scaling is in effect.
62     *
63     * @hide
64     */
65    protected int mScreenDensity = Bitmap.DENSITY_NONE;
66
67    // Used by native code
68    @SuppressWarnings("UnusedDeclaration")
69    private int mSurfaceFormat;
70
71    /**
72     * Flag for drawTextRun indicating left-to-right run direction.
73     * @hide
74     */
75    public static final int DIRECTION_LTR = 0;
76
77    /**
78     * Flag for drawTextRun indicating right-to-left run direction.
79     * @hide
80     */
81    public static final int DIRECTION_RTL = 1;
82
83    // Maximum bitmap size as defined in Skia's native code
84    // (see SkCanvas.cpp, SkDraw.cpp)
85    private static final int MAXMIMUM_BITMAP_SIZE = 32766;
86
87    // This field is used to finalize the native Canvas properly
88    private final CanvasFinalizer mFinalizer;
89
90    private static final class CanvasFinalizer {
91        private long mNativeCanvasWrapper;
92
93        public CanvasFinalizer(long nativeCanvas) {
94            mNativeCanvasWrapper = nativeCanvas;
95        }
96
97        @Override
98        protected void finalize() throws Throwable {
99            try {
100                dispose();
101            } finally {
102                super.finalize();
103            }
104        }
105
106        public void dispose() {
107            if (mNativeCanvasWrapper != 0) {
108                finalizer(mNativeCanvasWrapper);
109                mNativeCanvasWrapper = 0;
110            }
111        }
112    }
113
114    /**
115     * Construct an empty raster canvas. Use setBitmap() to specify a bitmap to
116     * draw into.  The initial target density is {@link Bitmap#DENSITY_NONE};
117     * this will typically be replaced when a target bitmap is set for the
118     * canvas.
119     */
120    public Canvas() {
121        if (!isHardwareAccelerated()) {
122            // 0 means no native bitmap
123            mNativeCanvasWrapper = initRaster(0);
124            mFinalizer = new CanvasFinalizer(mNativeCanvasWrapper);
125        } else {
126            mFinalizer = null;
127        }
128    }
129
130    /**
131     * Construct a canvas with the specified bitmap to draw into. The bitmap
132     * must be mutable.
133     *
134     * <p>The initial target density of the canvas is the same as the given
135     * bitmap's density.
136     *
137     * @param bitmap Specifies a mutable bitmap for the canvas to draw into.
138     */
139    public Canvas(Bitmap bitmap) {
140        if (!bitmap.isMutable()) {
141            throw new IllegalStateException("Immutable bitmap passed to Canvas constructor");
142        }
143        throwIfCannotDraw(bitmap);
144        mNativeCanvasWrapper = initRaster(bitmap.ni());
145        mFinalizer = new CanvasFinalizer(mNativeCanvasWrapper);
146        mBitmap = bitmap;
147        mDensity = bitmap.mDensity;
148    }
149
150    /** @hide */
151    public Canvas(long nativeCanvas) {
152        if (nativeCanvas == 0) {
153            throw new IllegalStateException();
154        }
155        mNativeCanvasWrapper = initCanvas(nativeCanvas);
156        mFinalizer = new CanvasFinalizer(mNativeCanvasWrapper);
157        mDensity = Bitmap.getDefaultDensity();
158    }
159
160    /**
161     * Returns null.
162     *
163     * @deprecated This method is not supported and should not be invoked.
164     *
165     * @hide
166     */
167    @Deprecated
168    protected GL getGL() {
169        return null;
170    }
171
172    /**
173     * Indicates whether this Canvas uses hardware acceleration.
174     *
175     * Note that this method does not define what type of hardware acceleration
176     * may or may not be used.
177     *
178     * @return True if drawing operations are hardware accelerated,
179     *         false otherwise.
180     */
181    public boolean isHardwareAccelerated() {
182        return false;
183    }
184
185    /**
186     * Specify a bitmap for the canvas to draw into. All canvas state such as
187     * layers, filters, and the save/restore stack are reset with the exception
188     * of the current matrix and clip stack. Additionally, as a side-effect
189     * the canvas' target density is updated to match that of the bitmap.
190     *
191     * @param bitmap Specifies a mutable bitmap for the canvas to draw into.
192     * @see #setDensity(int)
193     * @see #getDensity()
194     */
195    public void setBitmap(Bitmap bitmap) {
196        if (isHardwareAccelerated()) {
197            throw new RuntimeException("Can't set a bitmap device on a GL canvas");
198        }
199
200        if (bitmap == null) {
201            native_setBitmap(mNativeCanvasWrapper, 0, false);
202            mDensity = Bitmap.DENSITY_NONE;
203        } else {
204            if (!bitmap.isMutable()) {
205                throw new IllegalStateException();
206            }
207            throwIfCannotDraw(bitmap);
208
209            native_setBitmap(mNativeCanvasWrapper, bitmap.ni(), true);
210            mDensity = bitmap.mDensity;
211        }
212
213        mBitmap = bitmap;
214    }
215
216    /**
217     * setBitmap() variant for native callers with a raw bitmap handle.
218     */
219    private void setNativeBitmap(long bitmapHandle) {
220        native_setBitmap(mNativeCanvasWrapper, bitmapHandle, false);
221    }
222
223    /**
224     * Set the viewport dimensions if this canvas is GL based. If it is not,
225     * this method is ignored and no exception is thrown.
226     *
227     * @param width The width of the viewport
228     * @param height The height of the viewport
229     *
230     * @hide
231     */
232    public void setViewport(int width, int height) {}
233
234    /**
235     * @hide
236     */
237    public void initializeLight(float lightX, float lightY, float lightZ, float lightRadius) {}
238
239    /**
240     * Return true if the device that the current layer draws into is opaque
241     * (i.e. does not support per-pixel alpha).
242     *
243     * @return true if the device that the current layer draws into is opaque
244     */
245    public boolean isOpaque() {
246        return native_isOpaque(mNativeCanvasWrapper);
247    }
248
249    /**
250     * Returns the width of the current drawing layer
251     *
252     * @return the width of the current drawing layer
253     */
254    public int getWidth() {
255        return native_getWidth(mNativeCanvasWrapper);
256    }
257
258    /**
259     * Returns the height of the current drawing layer
260     *
261     * @return the height of the current drawing layer
262     */
263    public int getHeight() {
264        return native_getHeight(mNativeCanvasWrapper);
265    }
266
267    /**
268     * <p>Returns the target density of the canvas.  The default density is
269     * derived from the density of its backing bitmap, or
270     * {@link Bitmap#DENSITY_NONE} if there is not one.</p>
271     *
272     * @return Returns the current target density of the canvas, which is used
273     * to determine the scaling factor when drawing a bitmap into it.
274     *
275     * @see #setDensity(int)
276     * @see Bitmap#getDensity()
277     */
278    public int getDensity() {
279        return mDensity;
280    }
281
282    /**
283     * <p>Specifies the density for this Canvas' backing bitmap.  This modifies
284     * the target density of the canvas itself, as well as the density of its
285     * backing bitmap via {@link Bitmap#setDensity(int) Bitmap.setDensity(int)}.
286     *
287     * @param density The new target density of the canvas, which is used
288     * to determine the scaling factor when drawing a bitmap into it.  Use
289     * {@link Bitmap#DENSITY_NONE} to disable bitmap scaling.
290     *
291     * @see #getDensity()
292     * @see Bitmap#setDensity(int)
293     */
294    public void setDensity(int density) {
295        if (mBitmap != null) {
296            mBitmap.setDensity(density);
297        }
298        mDensity = density;
299    }
300
301    /** @hide */
302    public void setScreenDensity(int density) {
303        mScreenDensity = density;
304    }
305
306    /**
307     * Returns the maximum allowed width for bitmaps drawn with this canvas.
308     * Attempting to draw with a bitmap wider than this value will result
309     * in an error.
310     *
311     * @see #getMaximumBitmapHeight()
312     */
313    public int getMaximumBitmapWidth() {
314        return MAXMIMUM_BITMAP_SIZE;
315    }
316
317    /**
318     * Returns the maximum allowed height for bitmaps drawn with this canvas.
319     * Attempting to draw with a bitmap taller than this value will result
320     * in an error.
321     *
322     * @see #getMaximumBitmapWidth()
323     */
324    public int getMaximumBitmapHeight() {
325        return MAXMIMUM_BITMAP_SIZE;
326    }
327
328    // the SAVE_FLAG constants must match their native equivalents
329
330    /** restore the current matrix when restore() is called */
331    public static final int MATRIX_SAVE_FLAG = 0x01;
332    /** restore the current clip when restore() is called */
333    public static final int CLIP_SAVE_FLAG = 0x02;
334    /** the layer needs to per-pixel alpha */
335    public static final int HAS_ALPHA_LAYER_SAVE_FLAG = 0x04;
336    /** the layer needs to 8-bits per color component */
337    public static final int FULL_COLOR_LAYER_SAVE_FLAG = 0x08;
338    /** clip against the layer's bounds */
339    public static final int CLIP_TO_LAYER_SAVE_FLAG = 0x10;
340    /** restore everything when restore() is called */
341    public static final int ALL_SAVE_FLAG = 0x1F;
342
343    /**
344     * Saves the current matrix and clip onto a private stack. Subsequent
345     * calls to translate,scale,rotate,skew,concat or clipRect,clipPath
346     * will all operate as usual, but when the balancing call to restore()
347     * is made, those calls will be forgotten, and the settings that existed
348     * before the save() will be reinstated.
349     *
350     * @return The value to pass to restoreToCount() to balance this save()
351     */
352    public int save() {
353        return native_save(mNativeCanvasWrapper, MATRIX_SAVE_FLAG | CLIP_SAVE_FLAG);
354    }
355
356    /**
357     * Based on saveFlags, can save the current matrix and clip onto a private
358     * stack. Subsequent calls to translate,scale,rotate,skew,concat or
359     * clipRect,clipPath will all operate as usual, but when the balancing
360     * call to restore() is made, those calls will be forgotten, and the
361     * settings that existed before the save() will be reinstated.
362     *
363     * @param saveFlags flag bits that specify which parts of the Canvas state
364     *                  to save/restore
365     * @return The value to pass to restoreToCount() to balance this save()
366     */
367    public int save(int saveFlags) {
368        return native_save(mNativeCanvasWrapper, saveFlags);
369    }
370
371    /**
372     * This behaves the same as save(), but in addition it allocates an
373     * offscreen bitmap. All drawing calls are directed there, and only when
374     * the balancing call to restore() is made is that offscreen transfered to
375     * the canvas (or the previous layer). Subsequent calls to translate,
376     * scale, rotate, skew, concat or clipRect, clipPath all operate on this
377     * copy. When the balancing call to restore() is made, this copy is
378     * deleted and the previous matrix/clip state is restored.
379     *
380     * @param bounds May be null. The maximum size the offscreen bitmap
381     *               needs to be (in local coordinates)
382     * @param paint  This is copied, and is applied to the offscreen when
383     *               restore() is called.
384     * @param saveFlags  see _SAVE_FLAG constants
385     * @return       value to pass to restoreToCount() to balance this save()
386     */
387    public int saveLayer(RectF bounds, Paint paint, int saveFlags) {
388        return native_saveLayer(mNativeCanvasWrapper,
389                bounds.left, bounds.top, bounds.right, bounds.bottom,
390                paint != null ? paint.mNativePaint : 0,
391                saveFlags);
392    }
393
394    /**
395     * Convenience for saveLayer(bounds, paint, {@link #ALL_SAVE_FLAG})
396     */
397    public int saveLayer(RectF bounds, Paint paint) {
398        return saveLayer(bounds, paint, ALL_SAVE_FLAG);
399    }
400
401    /**
402     * Helper version of saveLayer() that takes 4 values rather than a RectF.
403     */
404    public int saveLayer(float left, float top, float right, float bottom, Paint paint,
405            int saveFlags) {
406        return native_saveLayer(mNativeCanvasWrapper, left, top, right, bottom,
407                paint != null ? paint.mNativePaint : 0,
408                saveFlags);
409    }
410
411    /**
412     * Convenience for saveLayer(left, top, right, bottom, paint, {@link #ALL_SAVE_FLAG})
413     */
414    public int saveLayer(float left, float top, float right, float bottom, Paint paint) {
415        return saveLayer(left, top, right, bottom, paint, ALL_SAVE_FLAG);
416    }
417
418    /**
419     * This behaves the same as save(), but in addition it allocates an
420     * offscreen bitmap. All drawing calls are directed there, and only when
421     * the balancing call to restore() is made is that offscreen transfered to
422     * the canvas (or the previous layer). Subsequent calls to translate,
423     * scale, rotate, skew, concat or clipRect, clipPath all operate on this
424     * copy. When the balancing call to restore() is made, this copy is
425     * deleted and the previous matrix/clip state is restored.
426     *
427     * @param bounds    The maximum size the offscreen bitmap needs to be
428     *                  (in local coordinates)
429     * @param alpha     The alpha to apply to the offscreen when when it is
430                        drawn during restore()
431     * @param saveFlags see _SAVE_FLAG constants
432     * @return          value to pass to restoreToCount() to balance this call
433     */
434    public int saveLayerAlpha(RectF bounds, int alpha, int saveFlags) {
435        alpha = Math.min(255, Math.max(0, alpha));
436        return native_saveLayerAlpha(mNativeCanvasWrapper,
437                bounds.left, bounds.top, bounds.right, bounds.bottom,
438                alpha, saveFlags);
439    }
440
441    /**
442     * Convenience for saveLayerAlpha(bounds, alpha, {@link #ALL_SAVE_FLAG})
443     */
444    public int saveLayerAlpha(RectF bounds, int alpha) {
445        return saveLayerAlpha(bounds, alpha, ALL_SAVE_FLAG);
446    }
447
448    /**
449     * Helper for saveLayerAlpha() that takes 4 values instead of a RectF.
450     */
451    public int saveLayerAlpha(float left, float top, float right, float bottom, int alpha,
452            int saveFlags) {
453        return native_saveLayerAlpha(mNativeCanvasWrapper, left, top, right, bottom,
454                                     alpha, saveFlags);
455    }
456
457    /**
458     * Helper for saveLayerAlpha(left, top, right, bottom, alpha, {@link #ALL_SAVE_FLAG})
459     */
460    public int saveLayerAlpha(float left, float top, float right, float bottom, int alpha) {
461        return saveLayerAlpha(left, top, right, bottom, alpha, ALL_SAVE_FLAG);
462    }
463
464    /**
465     * This call balances a previous call to save(), and is used to remove all
466     * modifications to the matrix/clip state since the last save call. It is
467     * an error to call restore() more times than save() was called.
468     */
469    public void restore() {
470        native_restore(mNativeCanvasWrapper);
471    }
472
473    /**
474     * Returns the number of matrix/clip states on the Canvas' private stack.
475     * This will equal # save() calls - # restore() calls.
476     */
477    public int getSaveCount() {
478        return native_getSaveCount(mNativeCanvasWrapper);
479    }
480
481    /**
482     * Efficient way to pop any calls to save() that happened after the save
483     * count reached saveCount. It is an error for saveCount to be less than 1.
484     *
485     * Example:
486     *    int count = canvas.save();
487     *    ... // more calls potentially to save()
488     *    canvas.restoreToCount(count);
489     *    // now the canvas is back in the same state it was before the initial
490     *    // call to save().
491     *
492     * @param saveCount The save level to restore to.
493     */
494    public void restoreToCount(int saveCount) {
495        native_restoreToCount(mNativeCanvasWrapper, saveCount);
496    }
497
498    /**
499     * Preconcat the current matrix with the specified translation
500     *
501     * @param dx The distance to translate in X
502     * @param dy The distance to translate in Y
503    */
504    public void translate(float dx, float dy) {
505        native_translate(mNativeCanvasWrapper, dx, dy);
506    }
507
508    /**
509     * Preconcat the current matrix with the specified scale.
510     *
511     * @param sx The amount to scale in X
512     * @param sy The amount to scale in Y
513     */
514    public void scale(float sx, float sy) {
515        native_scale(mNativeCanvasWrapper, sx, sy);
516    }
517
518    /**
519     * Preconcat the current matrix with the specified scale.
520     *
521     * @param sx The amount to scale in X
522     * @param sy The amount to scale in Y
523     * @param px The x-coord for the pivot point (unchanged by the scale)
524     * @param py The y-coord for the pivot point (unchanged by the scale)
525     */
526    public final void scale(float sx, float sy, float px, float py) {
527        translate(px, py);
528        scale(sx, sy);
529        translate(-px, -py);
530    }
531
532    /**
533     * Preconcat the current matrix with the specified rotation.
534     *
535     * @param degrees The amount to rotate, in degrees
536     */
537    public void rotate(float degrees) {
538        native_rotate(mNativeCanvasWrapper, degrees);
539    }
540
541    /**
542     * Preconcat the current matrix with the specified rotation.
543     *
544     * @param degrees The amount to rotate, in degrees
545     * @param px The x-coord for the pivot point (unchanged by the rotation)
546     * @param py The y-coord for the pivot point (unchanged by the rotation)
547     */
548    public final void rotate(float degrees, float px, float py) {
549        translate(px, py);
550        rotate(degrees);
551        translate(-px, -py);
552    }
553
554    /**
555     * Preconcat the current matrix with the specified skew.
556     *
557     * @param sx The amount to skew in X
558     * @param sy The amount to skew in Y
559     */
560    public void skew(float sx, float sy) {
561        native_skew(mNativeCanvasWrapper, sx, sy);
562    }
563
564    /**
565     * Preconcat the current matrix with the specified matrix. If the specified
566     * matrix is null, this method does nothing.
567     *
568     * @param matrix The matrix to preconcatenate with the current matrix
569     */
570    public void concat(Matrix matrix) {
571        if (matrix != null) native_concat(mNativeCanvasWrapper, matrix.native_instance);
572    }
573
574    /**
575     * Completely replace the current matrix with the specified matrix. If the
576     * matrix parameter is null, then the current matrix is reset to identity.
577     *
578     * <strong>Note:</strong> it is recommended to use {@link #concat(Matrix)},
579     * {@link #scale(float, float)}, {@link #translate(float, float)} and
580     * {@link #rotate(float)} instead of this method.
581     *
582     * @param matrix The matrix to replace the current matrix with. If it is
583     *               null, set the current matrix to identity.
584     *
585     * @see #concat(Matrix)
586     */
587    public void setMatrix(Matrix matrix) {
588        native_setMatrix(mNativeCanvasWrapper,
589                         matrix == null ? 0 : matrix.native_instance);
590    }
591
592    /**
593     * Return, in ctm, the current transformation matrix. This does not alter
594     * the matrix in the canvas, but just returns a copy of it.
595     */
596    @Deprecated
597    public void getMatrix(Matrix ctm) {
598        native_getCTM(mNativeCanvasWrapper, ctm.native_instance);
599    }
600
601    /**
602     * Return a new matrix with a copy of the canvas' current transformation
603     * matrix.
604     */
605    @Deprecated
606    public final Matrix getMatrix() {
607        Matrix m = new Matrix();
608        //noinspection deprecation
609        getMatrix(m);
610        return m;
611    }
612
613    /**
614     * Modify the current clip with the specified rectangle.
615     *
616     * @param rect The rect to intersect with the current clip
617     * @param op How the clip is modified
618     * @return true if the resulting clip is non-empty
619     */
620    public boolean clipRect(RectF rect, Region.Op op) {
621        return native_clipRect(mNativeCanvasWrapper, rect.left, rect.top, rect.right, rect.bottom,
622                op.nativeInt);
623    }
624
625    /**
626     * Modify the current clip with the specified rectangle, which is
627     * expressed in local coordinates.
628     *
629     * @param rect The rectangle to intersect with the current clip.
630     * @param op How the clip is modified
631     * @return true if the resulting clip is non-empty
632     */
633    public boolean clipRect(Rect rect, Region.Op op) {
634        return native_clipRect(mNativeCanvasWrapper, rect.left, rect.top, rect.right, rect.bottom,
635                op.nativeInt);
636    }
637
638    /**
639     * Intersect the current clip with the specified rectangle, which is
640     * expressed in local coordinates.
641     *
642     * @param rect The rectangle to intersect with the current clip.
643     * @return true if the resulting clip is non-empty
644     */
645    public boolean clipRect(RectF rect) {
646        return native_clipRect(mNativeCanvasWrapper, rect.left, rect.top, rect.right, rect.bottom,
647                Region.Op.INTERSECT.nativeInt);
648    }
649
650    /**
651     * Intersect the current clip with the specified rectangle, which is
652     * expressed in local coordinates.
653     *
654     * @param rect The rectangle to intersect with the current clip.
655     * @return true if the resulting clip is non-empty
656     */
657    public boolean clipRect(Rect rect) {
658        return native_clipRect(mNativeCanvasWrapper, rect.left, rect.top, rect.right, rect.bottom,
659                Region.Op.INTERSECT.nativeInt);
660    }
661
662    /**
663     * Modify the current clip with the specified rectangle, which is
664     * expressed in local coordinates.
665     *
666     * @param left   The left side of the rectangle to intersect with the
667     *               current clip
668     * @param top    The top of the rectangle to intersect with the current
669     *               clip
670     * @param right  The right side of the rectangle to intersect with the
671     *               current clip
672     * @param bottom The bottom of the rectangle to intersect with the current
673     *               clip
674     * @param op     How the clip is modified
675     * @return       true if the resulting clip is non-empty
676     */
677    public boolean clipRect(float left, float top, float right, float bottom, Region.Op op) {
678        return native_clipRect(mNativeCanvasWrapper, left, top, right, bottom, op.nativeInt);
679    }
680
681    /**
682     * Intersect the current clip with the specified rectangle, which is
683     * expressed in local coordinates.
684     *
685     * @param left   The left side of the rectangle to intersect with the
686     *               current clip
687     * @param top    The top of the rectangle to intersect with the current clip
688     * @param right  The right side of the rectangle to intersect with the
689     *               current clip
690     * @param bottom The bottom of the rectangle to intersect with the current
691     *               clip
692     * @return       true if the resulting clip is non-empty
693     */
694    public boolean clipRect(float left, float top, float right, float bottom) {
695        return native_clipRect(mNativeCanvasWrapper, left, top, right, bottom,
696                Region.Op.INTERSECT.nativeInt);
697    }
698
699    /**
700     * Intersect the current clip with the specified rectangle, which is
701     * expressed in local coordinates.
702     *
703     * @param left   The left side of the rectangle to intersect with the
704     *               current clip
705     * @param top    The top of the rectangle to intersect with the current clip
706     * @param right  The right side of the rectangle to intersect with the
707     *               current clip
708     * @param bottom The bottom of the rectangle to intersect with the current
709     *               clip
710     * @return       true if the resulting clip is non-empty
711     */
712    public boolean clipRect(int left, int top, int right, int bottom) {
713        return native_clipRect(mNativeCanvasWrapper, left, top, right, bottom,
714                Region.Op.INTERSECT.nativeInt);
715    }
716
717    /**
718        * Modify the current clip with the specified path.
719     *
720     * @param path The path to operate on the current clip
721     * @param op   How the clip is modified
722     * @return     true if the resulting is non-empty
723     */
724    public boolean clipPath(Path path, Region.Op op) {
725        return native_clipPath(mNativeCanvasWrapper, path.ni(), op.nativeInt);
726    }
727
728    /**
729     * Intersect the current clip with the specified path.
730     *
731     * @param path The path to intersect with the current clip
732     * @return     true if the resulting is non-empty
733     */
734    public boolean clipPath(Path path) {
735        return clipPath(path, Region.Op.INTERSECT);
736    }
737
738    /**
739     * Modify the current clip with the specified region. Note that unlike
740     * clipRect() and clipPath() which transform their arguments by the
741     * current matrix, clipRegion() assumes its argument is already in the
742     * coordinate system of the current layer's bitmap, and so not
743     * transformation is performed.
744     *
745     * @param region The region to operate on the current clip, based on op
746     * @param op How the clip is modified
747     * @return true if the resulting is non-empty
748     *
749     * @deprecated Unlike all other clip calls this API does not respect the
750     *             current matrix. Use {@link #clipRect(Rect)} as an alternative.
751     */
752    public boolean clipRegion(Region region, Region.Op op) {
753        return native_clipRegion(mNativeCanvasWrapper, region.ni(), op.nativeInt);
754    }
755
756    /**
757     * Intersect the current clip with the specified region. Note that unlike
758     * clipRect() and clipPath() which transform their arguments by the
759     * current matrix, clipRegion() assumes its argument is already in the
760     * coordinate system of the current layer's bitmap, and so not
761     * transformation is performed.
762     *
763     * @param region The region to operate on the current clip, based on op
764     * @return true if the resulting is non-empty
765     *
766     * @deprecated Unlike all other clip calls this API does not respect the
767     *             current matrix. Use {@link #clipRect(Rect)} as an alternative.
768     */
769    public boolean clipRegion(Region region) {
770        return clipRegion(region, Region.Op.INTERSECT);
771    }
772
773    public DrawFilter getDrawFilter() {
774        return mDrawFilter;
775    }
776
777    public void setDrawFilter(DrawFilter filter) {
778        long nativeFilter = 0;
779        if (filter != null) {
780            nativeFilter = filter.mNativeInt;
781        }
782        mDrawFilter = filter;
783        nativeSetDrawFilter(mNativeCanvasWrapper, nativeFilter);
784    }
785
786    public enum EdgeType {
787
788        /**
789         * Black-and-White: Treat edges by just rounding to nearest pixel boundary
790         */
791        BW(0),  //!< treat edges by just rounding to nearest pixel boundary
792
793        /**
794         * Antialiased: Treat edges by rounding-out, since they may be antialiased
795         */
796        AA(1);
797
798        EdgeType(int nativeInt) {
799            this.nativeInt = nativeInt;
800        }
801
802        /**
803         * @hide
804         */
805        public final int nativeInt;
806    }
807
808    /**
809     * Return true if the specified rectangle, after being transformed by the
810     * current matrix, would lie completely outside of the current clip. Call
811     * this to check if an area you intend to draw into is clipped out (and
812     * therefore you can skip making the draw calls).
813     *
814     * @param rect  the rect to compare with the current clip
815     * @param type  {@link Canvas.EdgeType#AA} if the path should be considered antialiased,
816     *              since that means it may affect a larger area (more pixels) than
817     *              non-antialiased ({@link Canvas.EdgeType#BW}).
818     * @return      true if the rect (transformed by the canvas' matrix)
819     *              does not intersect with the canvas' clip
820     */
821    public boolean quickReject(RectF rect, EdgeType type) {
822        return native_quickReject(mNativeCanvasWrapper,
823                rect.left, rect.top, rect.right, rect.bottom);
824    }
825
826    /**
827     * Return true if the specified path, after being transformed by the
828     * current matrix, would lie completely outside of the current clip. Call
829     * this to check if an area you intend to draw into is clipped out (and
830     * therefore you can skip making the draw calls). Note: for speed it may
831     * return false even if the path itself might not intersect the clip
832     * (i.e. the bounds of the path intersects, but the path does not).
833     *
834     * @param path        The path to compare with the current clip
835     * @param type        {@link Canvas.EdgeType#AA} if the path should be considered antialiased,
836     *                    since that means it may affect a larger area (more pixels) than
837     *                    non-antialiased ({@link Canvas.EdgeType#BW}).
838     * @return            true if the path (transformed by the canvas' matrix)
839     *                    does not intersect with the canvas' clip
840     */
841    public boolean quickReject(Path path, EdgeType type) {
842        return native_quickReject(mNativeCanvasWrapper, path.ni());
843    }
844
845    /**
846     * Return true if the specified rectangle, after being transformed by the
847     * current matrix, would lie completely outside of the current clip. Call
848     * this to check if an area you intend to draw into is clipped out (and
849     * therefore you can skip making the draw calls).
850     *
851     * @param left        The left side of the rectangle to compare with the
852     *                    current clip
853     * @param top         The top of the rectangle to compare with the current
854     *                    clip
855     * @param right       The right side of the rectangle to compare with the
856     *                    current clip
857     * @param bottom      The bottom of the rectangle to compare with the
858     *                    current clip
859     * @param type        {@link Canvas.EdgeType#AA} if the path should be considered antialiased,
860     *                    since that means it may affect a larger area (more pixels) than
861     *                    non-antialiased ({@link Canvas.EdgeType#BW}).
862     * @return            true if the rect (transformed by the canvas' matrix)
863     *                    does not intersect with the canvas' clip
864     */
865    public boolean quickReject(float left, float top, float right, float bottom,
866                               EdgeType type) {
867        return native_quickReject(mNativeCanvasWrapper, left, top, right, bottom);
868    }
869
870    /**
871     * Return the bounds of the current clip (in local coordinates) in the
872     * bounds parameter, and return true if it is non-empty. This can be useful
873     * in a way similar to quickReject, in that it tells you that drawing
874     * outside of these bounds will be clipped out.
875     *
876     * @param bounds Return the clip bounds here. If it is null, ignore it but
877     *               still return true if the current clip is non-empty.
878     * @return true if the current clip is non-empty.
879     */
880    public boolean getClipBounds(Rect bounds) {
881        return native_getClipBounds(mNativeCanvasWrapper, bounds);
882    }
883
884    /**
885     * Retrieve the bounds of the current clip (in local coordinates).
886     *
887     * @return the clip bounds, or [0, 0, 0, 0] if the clip is empty.
888     */
889    public final Rect getClipBounds() {
890        Rect r = new Rect();
891        getClipBounds(r);
892        return r;
893    }
894
895    /**
896     * Fill the entire canvas' bitmap (restricted to the current clip) with the
897     * specified RGB color, using srcover porterduff mode.
898     *
899     * @param r red component (0..255) of the color to draw onto the canvas
900     * @param g green component (0..255) of the color to draw onto the canvas
901     * @param b blue component (0..255) of the color to draw onto the canvas
902     */
903    public void drawRGB(int r, int g, int b) {
904        native_drawRGB(mNativeCanvasWrapper, r, g, b);
905    }
906
907    /**
908     * Fill the entire canvas' bitmap (restricted to the current clip) with the
909     * specified ARGB color, using srcover porterduff mode.
910     *
911     * @param a alpha component (0..255) of the color to draw onto the canvas
912     * @param r red component (0..255) of the color to draw onto the canvas
913     * @param g green component (0..255) of the color to draw onto the canvas
914     * @param b blue component (0..255) of the color to draw onto the canvas
915     */
916    public void drawARGB(int a, int r, int g, int b) {
917        native_drawARGB(mNativeCanvasWrapper, a, r, g, b);
918    }
919
920    /**
921     * Fill the entire canvas' bitmap (restricted to the current clip) with the
922     * specified color, using srcover porterduff mode.
923     *
924     * @param color the color to draw onto the canvas
925     */
926    public void drawColor(int color) {
927        native_drawColor(mNativeCanvasWrapper, color);
928    }
929
930    /**
931     * Fill the entire canvas' bitmap (restricted to the current clip) with the
932     * specified color and porter-duff xfermode.
933     *
934     * @param color the color to draw with
935     * @param mode  the porter-duff mode to apply to the color
936     */
937    public void drawColor(int color, PorterDuff.Mode mode) {
938        native_drawColor(mNativeCanvasWrapper, color, mode.nativeInt);
939    }
940
941    /**
942     * Fill the entire canvas' bitmap (restricted to the current clip) with
943     * the specified paint. This is equivalent (but faster) to drawing an
944     * infinitely large rectangle with the specified paint.
945     *
946     * @param paint The paint used to draw onto the canvas
947     */
948    public void drawPaint(Paint paint) {
949        native_drawPaint(mNativeCanvasWrapper, paint.mNativePaint);
950    }
951
952    /**
953     * Draw a series of points. Each point is centered at the coordinate
954     * specified by pts[], and its diameter is specified by the paint's stroke
955     * width (as transformed by the canvas' CTM), with special treatment for
956     * a stroke width of 0, which always draws exactly 1 pixel (or at most 4
957     * if antialiasing is enabled). The shape of the point is controlled by
958     * the paint's Cap type. The shape is a square, unless the cap type is
959     * Round, in which case the shape is a circle.
960     *
961     * @param pts      Array of points to draw [x0 y0 x1 y1 x2 y2 ...]
962     * @param offset   Number of values to skip before starting to draw.
963     * @param count    The number of values to process, after skipping offset
964     *                 of them. Since one point uses two values, the number of
965     *                 "points" that are drawn is really (count >> 1).
966     * @param paint    The paint used to draw the points
967     */
968    public void drawPoints(float[] pts, int offset, int count, Paint paint) {
969        native_drawPoints(mNativeCanvasWrapper, pts, offset, count, paint.mNativePaint);
970    }
971
972    /**
973     * Helper for drawPoints() that assumes you want to draw the entire array
974     */
975    public void drawPoints(float[] pts, Paint paint) {
976        drawPoints(pts, 0, pts.length, paint);
977    }
978
979    /**
980     * Helper for drawPoints() for drawing a single point.
981     */
982    public void drawPoint(float x, float y, Paint paint) {
983        native_drawPoint(mNativeCanvasWrapper, x, y, paint.mNativePaint);
984    }
985
986    /**
987     * Draw a line segment with the specified start and stop x,y coordinates,
988     * using the specified paint.
989     *
990     * <p>Note that since a line is always "framed", the Style is ignored in the paint.</p>
991     *
992     * <p>Degenerate lines (length is 0) will not be drawn.</p>
993     *
994     * @param startX The x-coordinate of the start point of the line
995     * @param startY The y-coordinate of the start point of the line
996     * @param paint  The paint used to draw the line
997     */
998    public void drawLine(float startX, float startY, float stopX, float stopY, Paint paint) {
999        native_drawLine(mNativeCanvasWrapper, startX, startY, stopX, stopY, paint.mNativePaint);
1000    }
1001
1002    /**
1003     * Draw a series of lines. Each line is taken from 4 consecutive values
1004     * in the pts array. Thus to draw 1 line, the array must contain at least 4
1005     * values. This is logically the same as drawing the array as follows:
1006     * drawLine(pts[0], pts[1], pts[2], pts[3]) followed by
1007     * drawLine(pts[4], pts[5], pts[6], pts[7]) and so on.
1008     *
1009     * @param pts      Array of points to draw [x0 y0 x1 y1 x2 y2 ...]
1010     * @param offset   Number of values in the array to skip before drawing.
1011     * @param count    The number of values in the array to process, after
1012     *                 skipping "offset" of them. Since each line uses 4 values,
1013     *                 the number of "lines" that are drawn is really
1014     *                 (count >> 2).
1015     * @param paint    The paint used to draw the points
1016     */
1017    public void drawLines(float[] pts, int offset, int count, Paint paint) {
1018        native_drawLines(mNativeCanvasWrapper, pts, offset, count, paint.mNativePaint);
1019    }
1020
1021    public void drawLines(float[] pts, Paint paint) {
1022        drawLines(pts, 0, pts.length, paint);
1023    }
1024
1025    /**
1026     * Draw the specified Rect using the specified paint. The rectangle will
1027     * be filled or framed based on the Style in the paint.
1028     *
1029     * @param rect  The rect to be drawn
1030     * @param paint The paint used to draw the rect
1031     */
1032    public void drawRect(RectF rect, Paint paint) {
1033        native_drawRect(mNativeCanvasWrapper,
1034                rect.left, rect.top, rect.right, rect.bottom, paint.mNativePaint);
1035    }
1036
1037    /**
1038     * Draw the specified Rect using the specified Paint. The rectangle
1039     * will be filled or framed based on the Style in the paint.
1040     *
1041     * @param r        The rectangle to be drawn.
1042     * @param paint    The paint used to draw the rectangle
1043     */
1044    public void drawRect(Rect r, Paint paint) {
1045        drawRect(r.left, r.top, r.right, r.bottom, paint);
1046    }
1047
1048
1049    /**
1050     * Draw the specified Rect using the specified paint. The rectangle will
1051     * be filled or framed based on the Style in the paint.
1052     *
1053     * @param left   The left side of the rectangle to be drawn
1054     * @param top    The top side of the rectangle to be drawn
1055     * @param right  The right side of the rectangle to be drawn
1056     * @param bottom The bottom side of the rectangle to be drawn
1057     * @param paint  The paint used to draw the rect
1058     */
1059    public void drawRect(float left, float top, float right, float bottom, Paint paint) {
1060        native_drawRect(mNativeCanvasWrapper, left, top, right, bottom, paint.mNativePaint);
1061    }
1062
1063    /**
1064     * Draw the specified oval using the specified paint. The oval will be
1065     * filled or framed based on the Style in the paint.
1066     *
1067     * @param oval The rectangle bounds of the oval to be drawn
1068     */
1069    public void drawOval(RectF oval, Paint paint) {
1070        if (oval == null) {
1071            throw new NullPointerException();
1072        }
1073        native_drawOval(mNativeCanvasWrapper, oval, paint.mNativePaint);
1074    }
1075
1076    /**
1077     * Draw the specified circle using the specified paint. If radius is <= 0,
1078     * then nothing will be drawn. The circle will be filled or framed based
1079     * on the Style in the paint.
1080     *
1081     * @param cx     The x-coordinate of the center of the cirle to be drawn
1082     * @param cy     The y-coordinate of the center of the cirle to be drawn
1083     * @param radius The radius of the cirle to be drawn
1084     * @param paint  The paint used to draw the circle
1085     */
1086    public void drawCircle(float cx, float cy, float radius, Paint paint) {
1087        native_drawCircle(mNativeCanvasWrapper, cx, cy, radius, paint.mNativePaint);
1088    }
1089
1090    /**
1091     * <p>Draw the specified arc, which will be scaled to fit inside the
1092     * specified oval.</p>
1093     *
1094     * <p>If the start angle is negative or >= 360, the start angle is treated
1095     * as start angle modulo 360.</p>
1096     *
1097     * <p>If the sweep angle is >= 360, then the oval is drawn
1098     * completely. Note that this differs slightly from SkPath::arcTo, which
1099     * treats the sweep angle modulo 360. If the sweep angle is negative,
1100     * the sweep angle is treated as sweep angle modulo 360</p>
1101     *
1102     * <p>The arc is drawn clockwise. An angle of 0 degrees correspond to the
1103     * geometric angle of 0 degrees (3 o'clock on a watch.)</p>
1104     *
1105     * @param oval       The bounds of oval used to define the shape and size
1106     *                   of the arc
1107     * @param startAngle Starting angle (in degrees) where the arc begins
1108     * @param sweepAngle Sweep angle (in degrees) measured clockwise
1109     * @param useCenter If true, include the center of the oval in the arc, and
1110                        close it if it is being stroked. This will draw a wedge
1111     * @param paint      The paint used to draw the arc
1112     */
1113    public void drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter,
1114            Paint paint) {
1115        if (oval == null) {
1116            throw new NullPointerException();
1117        }
1118        native_drawArc(mNativeCanvasWrapper, oval, startAngle, sweepAngle,
1119                useCenter, paint.mNativePaint);
1120    }
1121
1122    /**
1123     * Draw the specified round-rect using the specified paint. The roundrect
1124     * will be filled or framed based on the Style in the paint.
1125     *
1126     * @param rect  The rectangular bounds of the roundRect to be drawn
1127     * @param rx    The x-radius of the oval used to round the corners
1128     * @param ry    The y-radius of the oval used to round the corners
1129     * @param paint The paint used to draw the roundRect
1130     */
1131    public void drawRoundRect(RectF rect, float rx, float ry, Paint paint) {
1132        drawRoundRect(rect.left, rect.top, rect.right, rect.bottom, rx, ry, paint);
1133    }
1134
1135    /**
1136     * Draw the specified round-rect using the specified paint. The roundrect
1137     * will be filled or framed based on the Style in the paint.
1138     *
1139     * @param rx    The x-radius of the oval used to round the corners
1140     * @param ry    The y-radius of the oval used to round the corners
1141     * @param paint The paint used to draw the roundRect
1142     */
1143    public void drawRoundRect(float left, float top, float right, float bottom, float rx, float ry,
1144            Paint paint) {
1145        native_drawRoundRect(mNativeCanvasWrapper, left, top, right, bottom, rx, ry, paint.mNativePaint);
1146    }
1147
1148    /**
1149     * Draw the specified path using the specified paint. The path will be
1150     * filled or framed based on the Style in the paint.
1151     *
1152     * @param path  The path to be drawn
1153     * @param paint The paint used to draw the path
1154     */
1155    public void drawPath(Path path, Paint paint) {
1156        native_drawPath(mNativeCanvasWrapper, path.ni(), paint.mNativePaint);
1157    }
1158
1159    /**
1160     * @hide
1161     */
1162    protected static void throwIfCannotDraw(Bitmap bitmap) {
1163        if (bitmap.isRecycled()) {
1164            throw new RuntimeException("Canvas: trying to use a recycled bitmap " + bitmap);
1165        }
1166        if (!bitmap.isPremultiplied() && bitmap.getConfig() == Bitmap.Config.ARGB_8888 &&
1167                bitmap.hasAlpha()) {
1168            throw new RuntimeException("Canvas: trying to use a non-premultiplied bitmap "
1169                    + bitmap);
1170        }
1171    }
1172
1173    /**
1174     * Draws the specified bitmap as an N-patch (most often, a 9-patches.)
1175     *
1176     * @param patch The ninepatch object to render
1177     * @param dst The destination rectangle.
1178     * @param paint The paint to draw the bitmap with. may be null
1179     *
1180     * @hide
1181     */
1182    public void drawPatch(NinePatch patch, Rect dst, Paint paint) {
1183        patch.drawSoftware(this, dst, paint);
1184    }
1185
1186    /**
1187     * Draws the specified bitmap as an N-patch (most often, a 9-patches.)
1188     *
1189     * @param patch The ninepatch object to render
1190     * @param dst The destination rectangle.
1191     * @param paint The paint to draw the bitmap with. may be null
1192     *
1193     * @hide
1194     */
1195    public void drawPatch(NinePatch patch, RectF dst, Paint paint) {
1196        patch.drawSoftware(this, dst, paint);
1197    }
1198
1199    /**
1200     * Draw the specified bitmap, with its top/left corner at (x,y), using
1201     * the specified paint, transformed by the current matrix.
1202     *
1203     * <p>Note: if the paint contains a maskfilter that generates a mask which
1204     * extends beyond the bitmap's original width/height (e.g. BlurMaskFilter),
1205     * then the bitmap will be drawn as if it were in a Shader with CLAMP mode.
1206     * Thus the color outside of the original width/height will be the edge
1207     * color replicated.
1208     *
1209     * <p>If the bitmap and canvas have different densities, this function
1210     * will take care of automatically scaling the bitmap to draw at the
1211     * same density as the canvas.
1212     *
1213     * @param bitmap The bitmap to be drawn
1214     * @param left   The position of the left side of the bitmap being drawn
1215     * @param top    The position of the top side of the bitmap being drawn
1216     * @param paint  The paint used to draw the bitmap (may be null)
1217     */
1218    public void drawBitmap(Bitmap bitmap, float left, float top, Paint paint) {
1219        throwIfCannotDraw(bitmap);
1220        native_drawBitmap(mNativeCanvasWrapper, bitmap.ni(), left, top,
1221                paint != null ? paint.mNativePaint : 0, mDensity, mScreenDensity, bitmap.mDensity);
1222    }
1223
1224    /**
1225     * Draw the specified bitmap, scaling/translating automatically to fill
1226     * the destination rectangle. If the source rectangle is not null, it
1227     * specifies the subset of the bitmap to draw.
1228     *
1229     * <p>Note: if the paint contains a maskfilter that generates a mask which
1230     * extends beyond the bitmap's original width/height (e.g. BlurMaskFilter),
1231     * then the bitmap will be drawn as if it were in a Shader with CLAMP mode.
1232     * Thus the color outside of the original width/height will be the edge
1233     * color replicated.
1234     *
1235     * <p>This function <em>ignores the density associated with the bitmap</em>.
1236     * This is because the source and destination rectangle coordinate
1237     * spaces are in their respective densities, so must already have the
1238     * appropriate scaling factor applied.
1239     *
1240     * @param bitmap The bitmap to be drawn
1241     * @param src    May be null. The subset of the bitmap to be drawn
1242     * @param dst    The rectangle that the bitmap will be scaled/translated
1243     *               to fit into
1244     * @param paint  May be null. The paint used to draw the bitmap
1245     */
1246    public void drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint) {
1247        if (dst == null) {
1248            throw new NullPointerException();
1249        }
1250        throwIfCannotDraw(bitmap);
1251        native_drawBitmap(mNativeCanvasWrapper, bitmap.ni(), src, dst,
1252                          paint != null ? paint.mNativePaint : 0, mScreenDensity, bitmap.mDensity);
1253    }
1254
1255    /**
1256     * Draw the specified bitmap, scaling/translating automatically to fill
1257     * the destination rectangle. If the source rectangle is not null, it
1258     * specifies the subset of the bitmap to draw.
1259     *
1260     * <p>Note: if the paint contains a maskfilter that generates a mask which
1261     * extends beyond the bitmap's original width/height (e.g. BlurMaskFilter),
1262     * then the bitmap will be drawn as if it were in a Shader with CLAMP mode.
1263     * Thus the color outside of the original width/height will be the edge
1264     * color replicated.
1265     *
1266     * <p>This function <em>ignores the density associated with the bitmap</em>.
1267     * This is because the source and destination rectangle coordinate
1268     * spaces are in their respective densities, so must already have the
1269     * appropriate scaling factor applied.
1270     *
1271     * @param bitmap The bitmap to be drawn
1272     * @param src    May be null. The subset of the bitmap to be drawn
1273     * @param dst    The rectangle that the bitmap will be scaled/translated
1274     *               to fit into
1275     * @param paint  May be null. The paint used to draw the bitmap
1276     */
1277    public void drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint) {
1278        if (dst == null) {
1279            throw new NullPointerException();
1280        }
1281        throwIfCannotDraw(bitmap);
1282        native_drawBitmap(mNativeCanvasWrapper, bitmap.ni(), src, dst,
1283                paint != null ? paint.mNativePaint : 0, mScreenDensity, bitmap.mDensity);
1284    }
1285
1286    /**
1287     * Treat the specified array of colors as a bitmap, and draw it. This gives
1288     * the same result as first creating a bitmap from the array, and then
1289     * drawing it, but this method avoids explicitly creating a bitmap object
1290     * which can be more efficient if the colors are changing often.
1291     *
1292     * @param colors Array of colors representing the pixels of the bitmap
1293     * @param offset Offset into the array of colors for the first pixel
1294     * @param stride The number of colors in the array between rows (must be
1295     *               >= width or <= -width).
1296     * @param x The X coordinate for where to draw the bitmap
1297     * @param y The Y coordinate for where to draw the bitmap
1298     * @param width The width of the bitmap
1299     * @param height The height of the bitmap
1300     * @param hasAlpha True if the alpha channel of the colors contains valid
1301     *                 values. If false, the alpha byte is ignored (assumed to
1302     *                 be 0xFF for every pixel).
1303     * @param paint  May be null. The paint used to draw the bitmap
1304     *
1305     * @deprecated Usage with a {@link #isHardwareAccelerated() hardware accelerated} canvas
1306     * requires an internal copy of color buffer contents every time this method is called. Using a
1307     * Bitmap avoids this copy, and allows the application to more explicitly control the lifetime
1308     * and copies of pixel data.
1309     */
1310    @Deprecated
1311    public void drawBitmap(int[] colors, int offset, int stride, float x, float y,
1312            int width, int height, boolean hasAlpha, Paint paint) {
1313        // check for valid input
1314        if (width < 0) {
1315            throw new IllegalArgumentException("width must be >= 0");
1316        }
1317        if (height < 0) {
1318            throw new IllegalArgumentException("height must be >= 0");
1319        }
1320        if (Math.abs(stride) < width) {
1321            throw new IllegalArgumentException("abs(stride) must be >= width");
1322        }
1323        int lastScanline = offset + (height - 1) * stride;
1324        int length = colors.length;
1325        if (offset < 0 || (offset + width > length) || lastScanline < 0
1326                || (lastScanline + width > length)) {
1327            throw new ArrayIndexOutOfBoundsException();
1328        }
1329        // quick escape if there's nothing to draw
1330        if (width == 0 || height == 0) {
1331            return;
1332        }
1333        // punch down to native for the actual draw
1334        native_drawBitmap(mNativeCanvasWrapper, colors, offset, stride, x, y, width, height, hasAlpha,
1335                paint != null ? paint.mNativePaint : 0);
1336    }
1337
1338    /**
1339     * Legacy version of drawBitmap(int[] colors, ...) that took ints for x,y
1340     *
1341     * @deprecated Usage with a {@link #isHardwareAccelerated() hardware accelerated} canvas
1342     * requires an internal copy of color buffer contents every time this method is called. Using a
1343     * Bitmap avoids this copy, and allows the application to more explicitly control the lifetime
1344     * and copies of pixel data.
1345     */
1346    @Deprecated
1347    public void drawBitmap(int[] colors, int offset, int stride, int x, int y,
1348            int width, int height, boolean hasAlpha, Paint paint) {
1349        // call through to the common float version
1350        drawBitmap(colors, offset, stride, (float)x, (float)y, width, height,
1351                   hasAlpha, paint);
1352    }
1353
1354    /**
1355     * Draw the bitmap using the specified matrix.
1356     *
1357     * @param bitmap The bitmap to draw
1358     * @param matrix The matrix used to transform the bitmap when it is drawn
1359     * @param paint  May be null. The paint used to draw the bitmap
1360     */
1361    public void drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint) {
1362        nativeDrawBitmapMatrix(mNativeCanvasWrapper, bitmap.ni(), matrix.ni(),
1363                paint != null ? paint.mNativePaint : 0);
1364    }
1365
1366    /**
1367     * @hide
1368     */
1369    protected static void checkRange(int length, int offset, int count) {
1370        if ((offset | count) < 0 || offset + count > length) {
1371            throw new ArrayIndexOutOfBoundsException();
1372        }
1373    }
1374
1375    /**
1376     * Draw the bitmap through the mesh, where mesh vertices are evenly
1377     * distributed across the bitmap. There are meshWidth+1 vertices across, and
1378     * meshHeight+1 vertices down. The verts array is accessed in row-major
1379     * order, so that the first meshWidth+1 vertices are distributed across the
1380     * top of the bitmap from left to right. A more general version of this
1381     * method is drawVertices().
1382     *
1383     * @param bitmap The bitmap to draw using the mesh
1384     * @param meshWidth The number of columns in the mesh. Nothing is drawn if
1385     *                  this is 0
1386     * @param meshHeight The number of rows in the mesh. Nothing is drawn if
1387     *                   this is 0
1388     * @param verts Array of x,y pairs, specifying where the mesh should be
1389     *              drawn. There must be at least
1390     *              (meshWidth+1) * (meshHeight+1) * 2 + vertOffset values
1391     *              in the array
1392     * @param vertOffset Number of verts elements to skip before drawing
1393     * @param colors May be null. Specifies a color at each vertex, which is
1394     *               interpolated across the cell, and whose values are
1395     *               multiplied by the corresponding bitmap colors. If not null,
1396     *               there must be at least (meshWidth+1) * (meshHeight+1) +
1397     *               colorOffset values in the array.
1398     * @param colorOffset Number of color elements to skip before drawing
1399     * @param paint  May be null. The paint used to draw the bitmap
1400     */
1401    public void drawBitmapMesh(Bitmap bitmap, int meshWidth, int meshHeight,
1402            float[] verts, int vertOffset, int[] colors, int colorOffset, Paint paint) {
1403        if ((meshWidth | meshHeight | vertOffset | colorOffset) < 0) {
1404            throw new ArrayIndexOutOfBoundsException();
1405        }
1406        if (meshWidth == 0 || meshHeight == 0) {
1407            return;
1408        }
1409        int count = (meshWidth + 1) * (meshHeight + 1);
1410        // we mul by 2 since we need two floats per vertex
1411        checkRange(verts.length, vertOffset, count * 2);
1412        if (colors != null) {
1413            // no mul by 2, since we need only 1 color per vertex
1414            checkRange(colors.length, colorOffset, count);
1415        }
1416        nativeDrawBitmapMesh(mNativeCanvasWrapper, bitmap.ni(), meshWidth, meshHeight,
1417                verts, vertOffset, colors, colorOffset,
1418                paint != null ? paint.mNativePaint : 0);
1419    }
1420
1421    public enum VertexMode {
1422        TRIANGLES(0),
1423        TRIANGLE_STRIP(1),
1424        TRIANGLE_FAN(2);
1425
1426        VertexMode(int nativeInt) {
1427            this.nativeInt = nativeInt;
1428        }
1429
1430        /**
1431         * @hide
1432         */
1433        public final int nativeInt;
1434    }
1435
1436    /**
1437     * Draw the array of vertices, interpreted as triangles (based on mode). The
1438     * verts array is required, and specifies the x,y pairs for each vertex. If
1439     * texs is non-null, then it is used to specify the coordinate in shader
1440     * coordinates to use at each vertex (the paint must have a shader in this
1441     * case). If there is no texs array, but there is a color array, then each
1442     * color is interpolated across its corresponding triangle in a gradient. If
1443     * both texs and colors arrays are present, then they behave as before, but
1444     * the resulting color at each pixels is the result of multiplying the
1445     * colors from the shader and the color-gradient together. The indices array
1446     * is optional, but if it is present, then it is used to specify the index
1447     * of each triangle, rather than just walking through the arrays in order.
1448     *
1449     * @param mode How to interpret the array of vertices
1450     * @param vertexCount The number of values in the vertices array (and
1451     *      corresponding texs and colors arrays if non-null). Each logical
1452     *      vertex is two values (x, y), vertexCount must be a multiple of 2.
1453     * @param verts Array of vertices for the mesh
1454     * @param vertOffset Number of values in the verts to skip before drawing.
1455     * @param texs May be null. If not null, specifies the coordinates to sample
1456     *      into the current shader (e.g. bitmap tile or gradient)
1457     * @param texOffset Number of values in texs to skip before drawing.
1458     * @param colors May be null. If not null, specifies a color for each
1459     *      vertex, to be interpolated across the triangle.
1460     * @param colorOffset Number of values in colors to skip before drawing.
1461     * @param indices If not null, array of indices to reference into the
1462     *      vertex (texs, colors) array.
1463     * @param indexCount number of entries in the indices array (if not null).
1464     * @param paint Specifies the shader to use if the texs array is non-null.
1465     */
1466    public void drawVertices(VertexMode mode, int vertexCount, float[] verts, int vertOffset,
1467            float[] texs, int texOffset, int[] colors, int colorOffset,
1468            short[] indices, int indexOffset, int indexCount, Paint paint) {
1469        checkRange(verts.length, vertOffset, vertexCount);
1470        if (texs != null) {
1471            checkRange(texs.length, texOffset, vertexCount);
1472        }
1473        if (colors != null) {
1474            checkRange(colors.length, colorOffset, vertexCount / 2);
1475        }
1476        if (indices != null) {
1477            checkRange(indices.length, indexOffset, indexCount);
1478        }
1479        nativeDrawVertices(mNativeCanvasWrapper, mode.nativeInt, vertexCount, verts,
1480                vertOffset, texs, texOffset, colors, colorOffset,
1481                indices, indexOffset, indexCount, paint.mNativePaint);
1482    }
1483
1484    /**
1485     * Draw the text, with origin at (x,y), using the specified paint. The
1486     * origin is interpreted based on the Align setting in the paint.
1487     *
1488     * @param text  The text to be drawn
1489     * @param x     The x-coordinate of the origin of the text being drawn
1490     * @param y     The y-coordinate of the origin of the text being drawn
1491     * @param paint The paint used for the text (e.g. color, size, style)
1492     */
1493    public void drawText(char[] text, int index, int count, float x, float y, Paint paint) {
1494        if ((index | count | (index + count) |
1495            (text.length - index - count)) < 0) {
1496            throw new IndexOutOfBoundsException();
1497        }
1498        native_drawText(mNativeCanvasWrapper, text, index, count, x, y, paint.mBidiFlags,
1499                paint.mNativePaint, paint.mNativeTypeface);
1500    }
1501
1502    /**
1503     * Draw the text, with origin at (x,y), using the specified paint. The
1504     * origin is interpreted based on the Align setting in the paint.
1505     *
1506     * @param text  The text to be drawn
1507     * @param x     The x-coordinate of the origin of the text being drawn
1508     * @param y     The y-coordinate of the origin of the text being drawn
1509     * @param paint The paint used for the text (e.g. color, size, style)
1510     */
1511    public void drawText(String text, float x, float y, Paint paint) {
1512        native_drawText(mNativeCanvasWrapper, text, 0, text.length(), x, y, paint.mBidiFlags,
1513                paint.mNativePaint, paint.mNativeTypeface);
1514    }
1515
1516    /**
1517     * Draw the text, with origin at (x,y), using the specified paint.
1518     * The origin is interpreted based on the Align setting in the paint.
1519     *
1520     * @param text  The text to be drawn
1521     * @param start The index of the first character in text to draw
1522     * @param end   (end - 1) is the index of the last character in text to draw
1523     * @param x     The x-coordinate of the origin of the text being drawn
1524     * @param y     The y-coordinate of the origin of the text being drawn
1525     * @param paint The paint used for the text (e.g. color, size, style)
1526     */
1527    public void drawText(String text, int start, int end, float x, float y, Paint paint) {
1528        if ((start | end | (end - start) | (text.length() - end)) < 0) {
1529            throw new IndexOutOfBoundsException();
1530        }
1531        native_drawText(mNativeCanvasWrapper, text, start, end, x, y, paint.mBidiFlags,
1532                paint.mNativePaint, paint.mNativeTypeface);
1533    }
1534
1535    /**
1536     * Draw the specified range of text, specified by start/end, with its
1537     * origin at (x,y), in the specified Paint. The origin is interpreted
1538     * based on the Align setting in the Paint.
1539     *
1540     * @param text     The text to be drawn
1541     * @param start    The index of the first character in text to draw
1542     * @param end      (end - 1) is the index of the last character in text
1543     *                 to draw
1544     * @param x        The x-coordinate of origin for where to draw the text
1545     * @param y        The y-coordinate of origin for where to draw the text
1546     * @param paint The paint used for the text (e.g. color, size, style)
1547     */
1548    public void drawText(CharSequence text, int start, int end, float x, float y, Paint paint) {
1549        if (text instanceof String || text instanceof SpannedString ||
1550            text instanceof SpannableString) {
1551            native_drawText(mNativeCanvasWrapper, text.toString(), start, end, x, y,
1552                    paint.mBidiFlags, paint.mNativePaint, paint.mNativeTypeface);
1553        } else if (text instanceof GraphicsOperations) {
1554            ((GraphicsOperations) text).drawText(this, start, end, x, y,
1555                    paint);
1556        } else {
1557            char[] buf = TemporaryBuffer.obtain(end - start);
1558            TextUtils.getChars(text, start, end, buf, 0);
1559            native_drawText(mNativeCanvasWrapper, buf, 0, end - start, x, y,
1560                    paint.mBidiFlags, paint.mNativePaint, paint.mNativeTypeface);
1561            TemporaryBuffer.recycle(buf);
1562        }
1563    }
1564
1565    /**
1566     * Render a run of all LTR or all RTL text, with shaping. This does not run
1567     * bidi on the provided text, but renders it as a uniform right-to-left or
1568     * left-to-right run, as indicated by dir. Alignment of the text is as
1569     * determined by the Paint's TextAlign value.
1570     *
1571     * @param text the text to render
1572     * @param index the start of the text to render
1573     * @param count the count of chars to render
1574     * @param contextIndex the start of the context for shaping.  Must be
1575     *         no greater than index.
1576     * @param contextCount the number of characters in the context for shaping.
1577     *         ContexIndex + contextCount must be no less than index
1578     *         + count.
1579     * @param x the x position at which to draw the text
1580     * @param y the y position at which to draw the text
1581     * @param dir the run direction, either {@link #DIRECTION_LTR} or
1582     *         {@link #DIRECTION_RTL}.
1583     * @param paint the paint
1584     * @hide
1585     */
1586    public void drawTextRun(char[] text, int index, int count, int contextIndex, int contextCount,
1587            float x, float y, int dir, Paint paint) {
1588
1589        if (text == null) {
1590            throw new NullPointerException("text is null");
1591        }
1592        if (paint == null) {
1593            throw new NullPointerException("paint is null");
1594        }
1595        if ((index | count | text.length - index - count) < 0) {
1596            throw new IndexOutOfBoundsException();
1597        }
1598        if (dir != DIRECTION_LTR && dir != DIRECTION_RTL) {
1599            throw new IllegalArgumentException("unknown dir: " + dir);
1600        }
1601
1602        native_drawTextRun(mNativeCanvasWrapper, text, index, count,
1603                contextIndex, contextCount, x, y, dir, paint.mNativePaint, paint.mNativeTypeface);
1604    }
1605
1606    /**
1607     * Render a run of all LTR or all RTL text, with shaping. This does not run
1608     * bidi on the provided text, but renders it as a uniform right-to-left or
1609     * left-to-right run, as indicated by dir. Alignment of the text is as
1610     * determined by the Paint's TextAlign value.
1611     *
1612     * @param text the text to render
1613     * @param start the start of the text to render. Data before this position
1614     *            can be used for shaping context.
1615     * @param end the end of the text to render. Data at or after this
1616     *            position can be used for shaping context.
1617     * @param x the x position at which to draw the text
1618     * @param y the y position at which to draw the text
1619     * @param dir the run direction, either 0 for LTR or 1 for RTL.
1620     * @param paint the paint
1621     * @hide
1622     */
1623    public void drawTextRun(CharSequence text, int start, int end, int contextStart, int contextEnd,
1624            float x, float y, int dir, Paint paint) {
1625
1626        if (text == null) {
1627            throw new NullPointerException("text is null");
1628        }
1629        if (paint == null) {
1630            throw new NullPointerException("paint is null");
1631        }
1632        if ((start | end | end - start | text.length() - end) < 0) {
1633            throw new IndexOutOfBoundsException();
1634        }
1635
1636        int flags = dir == 0 ? 0 : 1;
1637
1638        if (text instanceof String || text instanceof SpannedString ||
1639                text instanceof SpannableString) {
1640            native_drawTextRun(mNativeCanvasWrapper, text.toString(), start, end,
1641                    contextStart, contextEnd, x, y, flags, paint.mNativePaint, paint.mNativeTypeface);
1642        } else if (text instanceof GraphicsOperations) {
1643            ((GraphicsOperations) text).drawTextRun(this, start, end,
1644                    contextStart, contextEnd, x, y, flags, paint);
1645        } else {
1646            int contextLen = contextEnd - contextStart;
1647            int len = end - start;
1648            char[] buf = TemporaryBuffer.obtain(contextLen);
1649            TextUtils.getChars(text, contextStart, contextEnd, buf, 0);
1650            native_drawTextRun(mNativeCanvasWrapper, buf, start - contextStart, len,
1651                    0, contextLen, x, y, flags, paint.mNativePaint, paint.mNativeTypeface);
1652            TemporaryBuffer.recycle(buf);
1653        }
1654    }
1655
1656    /**
1657     * Draw the text in the array, with each character's origin specified by
1658     * the pos array.
1659     *
1660     * This method does not support glyph composition and decomposition and
1661     * should therefore not be used to render complex scripts.
1662     *
1663     * @param text     The text to be drawn
1664     * @param index    The index of the first character to draw
1665     * @param count    The number of characters to draw, starting from index.
1666     * @param pos      Array of [x,y] positions, used to position each
1667     *                 character
1668     * @param paint    The paint used for the text (e.g. color, size, style)
1669     */
1670    @Deprecated
1671    public void drawPosText(char[] text, int index, int count, float[] pos, Paint paint) {
1672        if (index < 0 || index + count > text.length || count*2 > pos.length) {
1673            throw new IndexOutOfBoundsException();
1674        }
1675        native_drawPosText(mNativeCanvasWrapper, text, index, count, pos,
1676                paint.mNativePaint);
1677    }
1678
1679    /**
1680     * Draw the text in the array, with each character's origin specified by
1681     * the pos array.
1682     *
1683     * This method does not support glyph composition and decomposition and
1684     * should therefore not be used to render complex scripts.
1685     *
1686     * @param text  The text to be drawn
1687     * @param pos   Array of [x,y] positions, used to position each character
1688     * @param paint The paint used for the text (e.g. color, size, style)
1689     */
1690    @Deprecated
1691    public void drawPosText(String text, float[] pos, Paint paint) {
1692        if (text.length()*2 > pos.length) {
1693            throw new ArrayIndexOutOfBoundsException();
1694        }
1695        native_drawPosText(mNativeCanvasWrapper, text, pos, paint.mNativePaint);
1696    }
1697
1698    /**
1699     * Draw the text, with origin at (x,y), using the specified paint, along
1700     * the specified path. The paint's Align setting determins where along the
1701     * path to start the text.
1702     *
1703     * @param text     The text to be drawn
1704     * @param path     The path the text should follow for its baseline
1705     * @param hOffset  The distance along the path to add to the text's
1706     *                 starting position
1707     * @param vOffset  The distance above(-) or below(+) the path to position
1708     *                 the text
1709     * @param paint    The paint used for the text (e.g. color, size, style)
1710     */
1711    public void drawTextOnPath(char[] text, int index, int count, Path path,
1712            float hOffset, float vOffset, Paint paint) {
1713        if (index < 0 || index + count > text.length) {
1714            throw new ArrayIndexOutOfBoundsException();
1715        }
1716        native_drawTextOnPath(mNativeCanvasWrapper, text, index, count,
1717                path.ni(), hOffset, vOffset,
1718                paint.mBidiFlags, paint.mNativePaint);
1719    }
1720
1721    /**
1722     * Draw the text, with origin at (x,y), using the specified paint, along
1723     * the specified path. The paint's Align setting determins where along the
1724     * path to start the text.
1725     *
1726     * @param text     The text to be drawn
1727     * @param path     The path the text should follow for its baseline
1728     * @param hOffset  The distance along the path to add to the text's
1729     *                 starting position
1730     * @param vOffset  The distance above(-) or below(+) the path to position
1731     *                 the text
1732     * @param paint    The paint used for the text (e.g. color, size, style)
1733     */
1734    public void drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint) {
1735        if (text.length() > 0) {
1736            native_drawTextOnPath(mNativeCanvasWrapper, text, path.ni(), hOffset, vOffset,
1737                    paint.mBidiFlags, paint.mNativePaint);
1738        }
1739    }
1740
1741    /**
1742     * Save the canvas state, draw the picture, and restore the canvas state.
1743     * This differs from picture.draw(canvas), which does not perform any
1744     * save/restore.
1745     *
1746     * <p>
1747     * <strong>Note:</strong> This forces the picture to internally call
1748     * {@link Picture#endRecording} in order to prepare for playback.
1749     *
1750     * @param picture  The picture to be drawn
1751     */
1752    public void drawPicture(Picture picture) {
1753        picture.endRecording();
1754        int restoreCount = save();
1755        picture.draw(this);
1756        restoreToCount(restoreCount);
1757    }
1758
1759    /**
1760     * Draw the picture, stretched to fit into the dst rectangle.
1761     */
1762    public void drawPicture(Picture picture, RectF dst) {
1763        save();
1764        translate(dst.left, dst.top);
1765        if (picture.getWidth() > 0 && picture.getHeight() > 0) {
1766            scale(dst.width() / picture.getWidth(), dst.height() / picture.getHeight());
1767        }
1768        drawPicture(picture);
1769        restore();
1770    }
1771
1772    /**
1773     * Draw the picture, stretched to fit into the dst rectangle.
1774     */
1775    public void drawPicture(Picture picture, Rect dst) {
1776        save();
1777        translate(dst.left, dst.top);
1778        if (picture.getWidth() > 0 && picture.getHeight() > 0) {
1779            scale((float) dst.width() / picture.getWidth(),
1780                    (float) dst.height() / picture.getHeight());
1781        }
1782        drawPicture(picture);
1783        restore();
1784    }
1785
1786    /**
1787     * Releases the resources associated with this canvas.
1788     *
1789     * @hide
1790     */
1791    public void release() {
1792        mFinalizer.dispose();
1793    }
1794
1795    /**
1796     * Free up as much memory as possible from private caches (e.g. fonts, images)
1797     *
1798     * @hide
1799     */
1800    public static native void freeCaches();
1801
1802    /**
1803     * Free up text layout caches
1804     *
1805     * @hide
1806     */
1807    public static native void freeTextLayoutCaches();
1808
1809    private static native long initRaster(long nativeBitmapOrZero);
1810    private static native long initCanvas(long canvasHandle);
1811    private static native void native_setBitmap(long canvasHandle,
1812                                                long bitmapHandle,
1813                                                boolean copyState);
1814    private static native boolean native_isOpaque(long canvasHandle);
1815    private static native int native_getWidth(long canvasHandle);
1816    private static native int native_getHeight(long canvasHandle);
1817
1818    private static native int native_save(long canvasHandle, int saveFlags);
1819    private static native int native_saveLayer(long nativeCanvas, float l,
1820                                               float t, float r, float b,
1821                                               long nativePaint,
1822                                               int layerFlags);
1823    private static native int native_saveLayerAlpha(long nativeCanvas, float l,
1824                                                    float t, float r, float b,
1825                                                    int alpha, int layerFlags);
1826    private static native void native_restore(long canvasHandle);
1827    private static native void native_restoreToCount(long canvasHandle,
1828                                                     int saveCount);
1829    private static native int native_getSaveCount(long canvasHandle);
1830
1831    private static native void native_translate(long canvasHandle,
1832                                                float dx, float dy);
1833    private static native void native_scale(long canvasHandle,
1834                                            float sx, float sy);
1835    private static native void native_rotate(long canvasHandle, float degrees);
1836    private static native void native_skew(long canvasHandle,
1837                                           float sx, float sy);
1838    private static native void native_concat(long nativeCanvas,
1839                                             long nativeMatrix);
1840    private static native void native_setMatrix(long nativeCanvas,
1841                                                long nativeMatrix);
1842    private static native boolean native_clipRect(long nativeCanvas,
1843                                                  float left, float top,
1844                                                  float right, float bottom,
1845                                                  int regionOp);
1846    private static native boolean native_clipPath(long nativeCanvas,
1847                                                  long nativePath,
1848                                                  int regionOp);
1849    private static native boolean native_clipRegion(long nativeCanvas,
1850                                                    long nativeRegion,
1851                                                    int regionOp);
1852    private static native void nativeSetDrawFilter(long nativeCanvas,
1853                                                   long nativeFilter);
1854    private static native boolean native_getClipBounds(long nativeCanvas,
1855                                                       Rect bounds);
1856    private static native void native_getCTM(long nativeCanvas,
1857                                             long nativeMatrix);
1858    private static native boolean native_quickReject(long nativeCanvas,
1859                                                     long nativePath);
1860    private static native boolean native_quickReject(long nativeCanvas,
1861                                                     float left, float top,
1862                                                     float right, float bottom);
1863    private static native void native_drawRGB(long nativeCanvas, int r, int g,
1864                                              int b);
1865    private static native void native_drawARGB(long nativeCanvas, int a, int r,
1866                                               int g, int b);
1867    private static native void native_drawColor(long nativeCanvas, int color);
1868    private static native void native_drawColor(long nativeCanvas, int color,
1869                                                int mode);
1870    private static native void native_drawPaint(long nativeCanvas,
1871                                                long nativePaint);
1872    private static native void native_drawPoint(long canvasHandle, float x, float y,
1873                                                long paintHandle);
1874    private static native void native_drawPoints(long canvasHandle, float[] pts,
1875                                                 int offset, int count,
1876                                                 long paintHandle);
1877    private static native void native_drawLine(long nativeCanvas, float startX,
1878                                               float startY, float stopX,
1879                                               float stopY, long nativePaint);
1880    private static native void native_drawLines(long canvasHandle, float[] pts,
1881                                                int offset, int count,
1882                                                long paintHandle);
1883    private static native void native_drawRect(long nativeCanvas, float left,
1884                                               float top, float right,
1885                                               float bottom,
1886                                               long nativePaint);
1887    private static native void native_drawOval(long nativeCanvas, RectF oval,
1888                                               long nativePaint);
1889    private static native void native_drawCircle(long nativeCanvas, float cx,
1890                                                 float cy, float radius,
1891                                                 long nativePaint);
1892    private static native void native_drawArc(long nativeCanvas, RectF oval,
1893                                              float startAngle, float sweep,
1894                                              boolean useCenter,
1895                                              long nativePaint);
1896    private static native void native_drawRoundRect(long nativeCanvas,
1897            float left, float top, float right, float bottom,
1898            float rx, float ry, long nativePaint);
1899    private static native void native_drawPath(long nativeCanvas,
1900                                               long nativePath,
1901                                               long nativePaint);
1902    private native void native_drawBitmap(long nativeCanvas, long nativeBitmap,
1903                                                 float left, float top,
1904                                                 long nativePaintOrZero,
1905                                                 int canvasDensity,
1906                                                 int screenDensity,
1907                                                 int bitmapDensity);
1908    private native void native_drawBitmap(long nativeCanvas, long nativeBitmap,
1909                                                 Rect src, RectF dst,
1910                                                 long nativePaintOrZero,
1911                                                 int screenDensity,
1912                                                 int bitmapDensity);
1913    private static native void native_drawBitmap(long nativeCanvas,
1914                                                 long nativeBitmap,
1915                                                 Rect src, Rect dst,
1916                                                 long nativePaintOrZero,
1917                                                 int screenDensity,
1918                                                 int bitmapDensity);
1919    private static native void native_drawBitmap(long nativeCanvas, int[] colors,
1920                                                int offset, int stride, float x,
1921                                                 float y, int width, int height,
1922                                                 boolean hasAlpha,
1923                                                 long nativePaintOrZero);
1924    private static native void nativeDrawBitmapMatrix(long nativeCanvas,
1925                                                      long nativeBitmap,
1926                                                      long nativeMatrix,
1927                                                      long nativePaint);
1928    private static native void nativeDrawBitmapMesh(long nativeCanvas,
1929                                                    long nativeBitmap,
1930                                                    int meshWidth, int meshHeight,
1931                                                    float[] verts, int vertOffset,
1932                                                    int[] colors, int colorOffset,
1933                                                    long nativePaint);
1934    private static native void nativeDrawVertices(long nativeCanvas, int mode, int n,
1935                   float[] verts, int vertOffset, float[] texs, int texOffset,
1936                   int[] colors, int colorOffset, short[] indices,
1937                   int indexOffset, int indexCount, long nativePaint);
1938
1939    private static native void native_drawText(long nativeCanvas, char[] text,
1940                                               int index, int count, float x,
1941                                               float y, int flags, long nativePaint,
1942                                               long nativeTypeface);
1943    private static native void native_drawText(long nativeCanvas, String text,
1944                                               int start, int end, float x,
1945                                               float y, int flags, long nativePaint,
1946                                               long nativeTypeface);
1947
1948    private static native void native_drawTextRun(long nativeCanvas, String text,
1949            int start, int end, int contextStart, int contextEnd,
1950            float x, float y, int flags, long nativePaint, long nativeTypeface);
1951
1952    private static native void native_drawTextRun(long nativeCanvas, char[] text,
1953            int start, int count, int contextStart, int contextCount,
1954            float x, float y, int flags, long nativePaint, long nativeTypeface);
1955
1956    private static native void native_drawPosText(long nativeCanvas,
1957                                                  char[] text, int index,
1958                                                  int count, float[] pos,
1959                                                  long nativePaint);
1960    private static native void native_drawPosText(long nativeCanvas,
1961                                                  String text, float[] pos,
1962                                                  long nativePaint);
1963    private static native void native_drawTextOnPath(long nativeCanvas,
1964                                                     char[] text, int index,
1965                                                     int count, long nativePath,
1966                                                     float hOffset,
1967                                                     float vOffset, int bidiFlags,
1968                                                     long nativePaint);
1969    private static native void native_drawTextOnPath(long nativeCanvas,
1970                                                     String text, long nativePath,
1971                                                     float hOffset,
1972                                                     float vOffset,
1973                                                     int flags, long nativePaint);
1974    private static native void finalizer(long nativeCanvas);
1975}
1976