GLES20Canvas.java revision e93482f5eac3df581d57e64c2a771a96aa868585
1/*
2 * Copyright (C) 2010 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.view;
18
19import android.graphics.Bitmap;
20import android.graphics.Canvas;
21import android.graphics.ColorFilter;
22import android.graphics.DrawFilter;
23import android.graphics.Matrix;
24import android.graphics.NinePatch;
25import android.graphics.Paint;
26import android.graphics.PaintFlagsDrawFilter;
27import android.graphics.Path;
28import android.graphics.Picture;
29import android.graphics.PorterDuff;
30import android.graphics.Rect;
31import android.graphics.RectF;
32import android.graphics.Region;
33import android.graphics.Shader;
34import android.graphics.SurfaceTexture;
35import android.graphics.TemporaryBuffer;
36import android.text.GraphicsOperations;
37import android.text.SpannableString;
38import android.text.SpannedString;
39import android.text.TextUtils;
40
41/**
42 * An implementation of Canvas on top of OpenGL ES 2.0.
43 */
44class GLES20Canvas extends HardwareCanvas {
45    // Must match modifiers used in the JNI layer
46    private static final int MODIFIER_NONE = 0;
47    private static final int MODIFIER_SHADOW = 1;
48    private static final int MODIFIER_SHADER = 2;
49    private static final int MODIFIER_COLOR_FILTER = 4;
50
51    private final boolean mOpaque;
52    private int mRenderer;
53
54    // The native renderer will be destroyed when this object dies.
55    // DO NOT overwrite this reference once it is set.
56    @SuppressWarnings({"unused", "FieldCanBeLocal"})
57    private CanvasFinalizer mFinalizer;
58
59    private int mWidth;
60    private int mHeight;
61
62    private float[] mPoint;
63    private float[] mLine;
64
65    private Rect mClipBounds;
66    private RectF mPathBounds;
67
68    private DrawFilter mFilter;
69
70    ///////////////////////////////////////////////////////////////////////////
71    // JNI
72    ///////////////////////////////////////////////////////////////////////////
73
74    private static native boolean nIsAvailable();
75    private static boolean sIsAvailable = nIsAvailable();
76
77    static boolean isAvailable() {
78        return sIsAvailable;
79    }
80
81    ///////////////////////////////////////////////////////////////////////////
82    // Constructors
83    ///////////////////////////////////////////////////////////////////////////
84
85    /**
86     * Creates a canvas to render directly on screen.
87     */
88    GLES20Canvas(boolean translucent) {
89        this(false, translucent);
90    }
91
92    /**
93     * Creates a canvas to render into an FBO.
94     */
95    GLES20Canvas(int layer, boolean translucent) {
96        mOpaque = !translucent;
97        mRenderer = nCreateLayerRenderer(layer);
98        setupFinalizer();
99    }
100
101    protected GLES20Canvas(boolean record, boolean translucent) {
102        mOpaque = !translucent;
103
104        if (record) {
105            mRenderer = nCreateDisplayListRenderer();
106        } else {
107            mRenderer = nCreateRenderer();
108        }
109
110        setupFinalizer();
111    }
112
113    private void setupFinalizer() {
114        if (mRenderer == 0) {
115            throw new IllegalStateException("Could not create GLES20Canvas renderer");
116        } else {
117            mFinalizer = new CanvasFinalizer(mRenderer);
118        }
119    }
120
121    protected void resetDisplayListRenderer() {
122        nResetDisplayListRenderer(mRenderer);
123    }
124
125    private static native int nCreateRenderer();
126    private static native int nCreateLayerRenderer(int layer);
127    private static native int nCreateDisplayListRenderer();
128    private static native void nResetDisplayListRenderer(int renderer);
129    private static native void nDestroyRenderer(int renderer);
130
131    private static final class CanvasFinalizer {
132        private final int mRenderer;
133
134        public CanvasFinalizer(int renderer) {
135            mRenderer = renderer;
136        }
137
138        @Override
139        protected void finalize() throws Throwable {
140            try {
141                nDestroyRenderer(mRenderer);
142            } finally {
143                super.finalize();
144            }
145        }
146    }
147
148    @Override
149    public void setName(String name) {
150        super.setName(name);
151        nSetName(mRenderer, name);
152    }
153
154    private static native void nSetName(int renderer, String name);
155
156    ///////////////////////////////////////////////////////////////////////////
157    // Hardware layers
158    ///////////////////////////////////////////////////////////////////////////
159
160    @Override
161    void pushLayerUpdate(HardwareLayer layer) {
162        nPushLayerUpdate(mRenderer, ((GLES20RenderLayer) layer).mLayer);
163    }
164
165    @Override
166    void cancelLayerUpdate(HardwareLayer layer) {
167        nCancelLayerUpdate(mRenderer, ((GLES20RenderLayer) layer).mLayer);
168    }
169
170    @Override
171    void flushLayerUpdates() {
172        nFlushLayerUpdates(mRenderer);
173    }
174
175    @Override
176    void clearLayerUpdates() {
177        nClearLayerUpdates(mRenderer);
178    }
179
180    static native int nCreateTextureLayer(boolean opaque, int[] layerInfo);
181    static native int nCreateLayer(int width, int height, boolean isOpaque, int[] layerInfo);
182    static native boolean nResizeLayer(int layerId, int width, int height, int[] layerInfo);
183    static native void nSetOpaqueLayer(int layerId, boolean isOpaque);
184    static native void nSetLayerPaint(int layerId, int nativePaint);
185    static native void nSetLayerColorFilter(int layerId, int nativeColorFilter);
186    static native void nUpdateTextureLayer(int layerId, int width, int height, boolean opaque,
187            SurfaceTexture surface);
188    static native void nClearLayerTexture(int layerId);
189    static native void nSetTextureLayerTransform(int layerId, int matrix);
190    static native void nDestroyLayer(int layerId);
191    static native void nDestroyLayerDeferred(int layerId);
192    static native void nUpdateRenderLayer(int layerId, int renderer, int displayList,
193            int left, int top, int right, int bottom);
194    static native boolean nCopyLayer(int layerId, int bitmap);
195
196    private static native void nClearLayerUpdates(int renderer);
197    private static native void nFlushLayerUpdates(int renderer);
198    private static native void nPushLayerUpdate(int renderer, int layer);
199    private static native void nCancelLayerUpdate(int renderer, int layer);
200
201    ///////////////////////////////////////////////////////////////////////////
202    // Canvas management
203    ///////////////////////////////////////////////////////////////////////////
204
205    @Override
206    public boolean isOpaque() {
207        return mOpaque;
208    }
209
210    @Override
211    public int getWidth() {
212        return mWidth;
213    }
214
215    @Override
216    public int getHeight() {
217        return mHeight;
218    }
219
220    @Override
221    public int getMaximumBitmapWidth() {
222        return nGetMaximumTextureWidth();
223    }
224
225    @Override
226    public int getMaximumBitmapHeight() {
227        return nGetMaximumTextureHeight();
228    }
229
230    private static native int nGetMaximumTextureWidth();
231    private static native int nGetMaximumTextureHeight();
232
233    /**
234     * Returns the native OpenGLRenderer object.
235     */
236    int getRenderer() {
237        return mRenderer;
238    }
239
240    ///////////////////////////////////////////////////////////////////////////
241    // Setup
242    ///////////////////////////////////////////////////////////////////////////
243
244    @Override
245    public void setViewport(int width, int height) {
246        mWidth = width;
247        mHeight = height;
248
249        nSetViewport(mRenderer, width, height);
250    }
251
252    private static native void nSetViewport(int renderer, int width, int height);
253
254    @Override
255    public int onPreDraw(Rect dirty) {
256        if (dirty != null) {
257            return nPrepareDirty(mRenderer, dirty.left, dirty.top, dirty.right, dirty.bottom,
258                    mOpaque);
259        } else {
260            return nPrepare(mRenderer, mOpaque);
261        }
262    }
263
264    private static native int nPrepare(int renderer, boolean opaque);
265    private static native int nPrepareDirty(int renderer, int left, int top, int right, int bottom,
266            boolean opaque);
267
268    @Override
269    public void onPostDraw() {
270        nFinish(mRenderer);
271    }
272
273    private static native void nFinish(int renderer);
274
275    /**
276     * Returns the size of the stencil buffer required by the underlying
277     * implementation.
278     *
279     * @return The minimum number of bits the stencil buffer must. Always >= 0.
280     *
281     * @hide
282     */
283    public static int getStencilSize() {
284        return nGetStencilSize();
285    }
286
287    private static native int nGetStencilSize();
288
289    void setCountOverdrawEnabled(boolean enabled) {
290        nSetCountOverdrawEnabled(mRenderer, enabled);
291    }
292
293    static native void nSetCountOverdrawEnabled(int renderer, boolean enabled);
294
295    float getOverdraw() {
296        return nGetOverdraw(mRenderer);
297    }
298
299    static native float nGetOverdraw(int renderer);
300
301    ///////////////////////////////////////////////////////////////////////////
302    // Functor
303    ///////////////////////////////////////////////////////////////////////////
304
305    @Override
306    public int callDrawGLFunction(int drawGLFunction) {
307        return nCallDrawGLFunction(mRenderer, drawGLFunction);
308    }
309
310    private static native int nCallDrawGLFunction(int renderer, int drawGLFunction);
311
312    @Override
313    public int invokeFunctors(Rect dirty) {
314        return nInvokeFunctors(mRenderer, dirty);
315    }
316
317    private static native int nInvokeFunctors(int renderer, Rect dirty);
318
319    @Override
320    public void detachFunctor(int functor) {
321        nDetachFunctor(mRenderer, functor);
322    }
323
324    private static native void nDetachFunctor(int renderer, int functor);
325
326    @Override
327    public void attachFunctor(int functor) {
328        nAttachFunctor(mRenderer, functor);
329    }
330
331    private static native void nAttachFunctor(int renderer, int functor);
332
333    ///////////////////////////////////////////////////////////////////////////
334    // Memory
335    ///////////////////////////////////////////////////////////////////////////
336
337    /**
338     * Must match Caches::FlushMode values
339     *
340     * @see #flushCaches(int)
341     */
342    static final int FLUSH_CACHES_LAYERS = 0;
343
344    /**
345     * Must match Caches::FlushMode values
346     *
347     * @see #flushCaches(int)
348     */
349    static final int FLUSH_CACHES_MODERATE = 1;
350
351    /**
352     * Must match Caches::FlushMode values
353     *
354     * @see #flushCaches(int)
355     */
356    static final int FLUSH_CACHES_FULL = 2;
357
358    /**
359     * Flush caches to reclaim as much memory as possible. The amount of memory
360     * to reclaim is indicate by the level parameter.
361     *
362     * The level can be one of {@link #FLUSH_CACHES_MODERATE} or
363     * {@link #FLUSH_CACHES_FULL}.
364     *
365     * @param level Hint about the amount of memory to reclaim
366     */
367    static void flushCaches(int level) {
368        nFlushCaches(level);
369    }
370
371    private static native void nFlushCaches(int level);
372
373    /**
374     * Release all resources associated with the underlying caches. This should
375     * only be called after a full flushCaches().
376     *
377     * @hide
378     */
379    static void terminateCaches() {
380        nTerminateCaches();
381    }
382
383    private static native void nTerminateCaches();
384
385    static boolean initCaches() {
386        return nInitCaches();
387    }
388
389    private static native boolean nInitCaches();
390
391    ///////////////////////////////////////////////////////////////////////////
392    // Atlas
393    ///////////////////////////////////////////////////////////////////////////
394
395    static void initAtlas(GraphicBuffer buffer, int[] map) {
396        nInitAtlas(buffer, map, map.length);
397    }
398
399    private static native void nInitAtlas(GraphicBuffer buffer, int[] map, int count);
400
401    ///////////////////////////////////////////////////////////////////////////
402    // Display list
403    ///////////////////////////////////////////////////////////////////////////
404
405    int getDisplayList(int displayList) {
406        return nGetDisplayList(mRenderer, displayList);
407    }
408
409    private static native int nGetDisplayList(int renderer, int displayList);
410
411    @Override
412    void outputDisplayList(DisplayList displayList) {
413        nOutputDisplayList(mRenderer, ((GLES20DisplayList) displayList).getNativeDisplayList());
414    }
415
416    private static native void nOutputDisplayList(int renderer, int displayList);
417
418    @Override
419    public int drawDisplayList(DisplayList displayList, Rect dirty, int flags) {
420        return nDrawDisplayList(mRenderer, ((GLES20DisplayList) displayList).getNativeDisplayList(),
421                dirty, flags);
422    }
423
424    private static native int nDrawDisplayList(int renderer, int displayList,
425            Rect dirty, int flags);
426
427    ///////////////////////////////////////////////////////////////////////////
428    // Hardware layer
429    ///////////////////////////////////////////////////////////////////////////
430
431    void drawHardwareLayer(HardwareLayer layer, float x, float y, Paint paint) {
432        layer.setLayerPaint(paint);
433
434        final GLES20Layer glLayer = (GLES20Layer) layer;
435        nDrawLayer(mRenderer, glLayer.getLayer(), x, y);
436    }
437
438    private static native void nDrawLayer(int renderer, int layer, float x, float y);
439
440    void interrupt() {
441        nInterrupt(mRenderer);
442    }
443
444    void resume() {
445        nResume(mRenderer);
446    }
447
448    private static native void nInterrupt(int renderer);
449    private static native void nResume(int renderer);
450
451    ///////////////////////////////////////////////////////////////////////////
452    // Support
453    ///////////////////////////////////////////////////////////////////////////
454
455    private Rect getInternalClipBounds() {
456        if (mClipBounds == null) mClipBounds = new Rect();
457        return mClipBounds;
458    }
459
460
461    private RectF getPathBounds() {
462        if (mPathBounds == null) mPathBounds = new RectF();
463        return mPathBounds;
464    }
465
466    private float[] getPointStorage() {
467        if (mPoint == null) mPoint = new float[2];
468        return mPoint;
469    }
470
471    private float[] getLineStorage() {
472        if (mLine == null) mLine = new float[4];
473        return mLine;
474    }
475
476    ///////////////////////////////////////////////////////////////////////////
477    // Clipping
478    ///////////////////////////////////////////////////////////////////////////
479
480    @Override
481    public boolean clipPath(Path path) {
482        return nClipPath(mRenderer, path.mNativePath, Region.Op.INTERSECT.nativeInt);
483    }
484
485    @Override
486    public boolean clipPath(Path path, Region.Op op) {
487        return nClipPath(mRenderer, path.mNativePath, op.nativeInt);
488    }
489
490    private static native boolean nClipPath(int renderer, int path, int op);
491
492    @Override
493    public boolean clipRect(float left, float top, float right, float bottom) {
494        return nClipRect(mRenderer, left, top, right, bottom, Region.Op.INTERSECT.nativeInt);
495    }
496
497    private static native boolean nClipRect(int renderer, float left, float top,
498            float right, float bottom, int op);
499
500    @Override
501    public boolean clipRect(float left, float top, float right, float bottom, Region.Op op) {
502        return nClipRect(mRenderer, left, top, right, bottom, op.nativeInt);
503    }
504
505    @Override
506    public boolean clipRect(int left, int top, int right, int bottom) {
507        return nClipRect(mRenderer, left, top, right, bottom, Region.Op.INTERSECT.nativeInt);
508    }
509
510    private static native boolean nClipRect(int renderer, int left, int top,
511            int right, int bottom, int op);
512
513    @Override
514    public boolean clipRect(Rect rect) {
515        return nClipRect(mRenderer, rect.left, rect.top, rect.right, rect.bottom,
516                Region.Op.INTERSECT.nativeInt);
517    }
518
519    @Override
520    public boolean clipRect(Rect rect, Region.Op op) {
521        return nClipRect(mRenderer, rect.left, rect.top, rect.right, rect.bottom, op.nativeInt);
522    }
523
524    @Override
525    public boolean clipRect(RectF rect) {
526        return nClipRect(mRenderer, rect.left, rect.top, rect.right, rect.bottom,
527                Region.Op.INTERSECT.nativeInt);
528    }
529
530    @Override
531    public boolean clipRect(RectF rect, Region.Op op) {
532        return nClipRect(mRenderer, rect.left, rect.top, rect.right, rect.bottom, op.nativeInt);
533    }
534
535    @Override
536    public boolean clipRegion(Region region) {
537        return nClipRegion(mRenderer, region.mNativeRegion, Region.Op.INTERSECT.nativeInt);
538    }
539
540    @Override
541    public boolean clipRegion(Region region, Region.Op op) {
542        return nClipRegion(mRenderer, region.mNativeRegion, op.nativeInt);
543    }
544
545    private static native boolean nClipRegion(int renderer, int region, int op);
546
547    @Override
548    public boolean getClipBounds(Rect bounds) {
549        return nGetClipBounds(mRenderer, bounds);
550    }
551
552    private static native boolean nGetClipBounds(int renderer, Rect bounds);
553
554    @Override
555    public boolean quickReject(float left, float top, float right, float bottom, EdgeType type) {
556        return nQuickReject(mRenderer, left, top, right, bottom);
557    }
558
559    private static native boolean nQuickReject(int renderer, float left, float top,
560            float right, float bottom);
561
562    @Override
563    public boolean quickReject(Path path, EdgeType type) {
564        RectF pathBounds = getPathBounds();
565        path.computeBounds(pathBounds, true);
566        return nQuickReject(mRenderer, pathBounds.left, pathBounds.top,
567                pathBounds.right, pathBounds.bottom);
568    }
569
570    @Override
571    public boolean quickReject(RectF rect, EdgeType type) {
572        return nQuickReject(mRenderer, rect.left, rect.top, rect.right, rect.bottom);
573    }
574
575    ///////////////////////////////////////////////////////////////////////////
576    // Transformations
577    ///////////////////////////////////////////////////////////////////////////
578
579    @Override
580    public void translate(float dx, float dy) {
581        if (dx != 0.0f || dy != 0.0f) nTranslate(mRenderer, dx, dy);
582    }
583
584    private static native void nTranslate(int renderer, float dx, float dy);
585
586    @Override
587    public void skew(float sx, float sy) {
588        nSkew(mRenderer, sx, sy);
589    }
590
591    private static native void nSkew(int renderer, float sx, float sy);
592
593    @Override
594    public void rotate(float degrees) {
595        nRotate(mRenderer, degrees);
596    }
597
598    private static native void nRotate(int renderer, float degrees);
599
600    @Override
601    public void scale(float sx, float sy) {
602        nScale(mRenderer, sx, sy);
603    }
604
605    private static native void nScale(int renderer, float sx, float sy);
606
607    @Override
608    public void setMatrix(Matrix matrix) {
609        nSetMatrix(mRenderer, matrix == null ? 0 : matrix.native_instance);
610    }
611
612    private static native void nSetMatrix(int renderer, int matrix);
613
614    @SuppressWarnings("deprecation")
615    @Override
616    public void getMatrix(Matrix matrix) {
617        nGetMatrix(mRenderer, matrix.native_instance);
618    }
619
620    private static native void nGetMatrix(int renderer, int matrix);
621
622    @Override
623    public void concat(Matrix matrix) {
624        nConcatMatrix(mRenderer, matrix.native_instance);
625    }
626
627    private static native void nConcatMatrix(int renderer, int matrix);
628
629    ///////////////////////////////////////////////////////////////////////////
630    // State management
631    ///////////////////////////////////////////////////////////////////////////
632
633    @Override
634    public int save() {
635        return nSave(mRenderer, Canvas.CLIP_SAVE_FLAG | Canvas.MATRIX_SAVE_FLAG);
636    }
637
638    @Override
639    public int save(int saveFlags) {
640        return nSave(mRenderer, saveFlags);
641    }
642
643    private static native int nSave(int renderer, int flags);
644
645    @Override
646    public int saveLayer(RectF bounds, Paint paint, int saveFlags) {
647        if (bounds != null) {
648            return saveLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, paint, saveFlags);
649        }
650
651        int count;
652        int modifier = paint != null ? setupColorFilter(paint) : MODIFIER_NONE;
653        try {
654            final int nativePaint = paint == null ? 0 : paint.mNativePaint;
655            count = nSaveLayer(mRenderer, nativePaint, saveFlags);
656        } finally {
657            if (modifier != MODIFIER_NONE) nResetModifiers(mRenderer, modifier);
658        }
659        return count;
660    }
661
662    private static native int nSaveLayer(int renderer, int paint, int saveFlags);
663
664    @Override
665    public int saveLayer(float left, float top, float right, float bottom, Paint paint,
666            int saveFlags) {
667        if (left < right && top < bottom) {
668            int count;
669            int modifier = paint != null ? setupColorFilter(paint) : MODIFIER_NONE;
670            try {
671                final int nativePaint = paint == null ? 0 : paint.mNativePaint;
672                count = nSaveLayer(mRenderer, left, top, right, bottom, nativePaint, saveFlags);
673            } finally {
674                if (modifier != MODIFIER_NONE) nResetModifiers(mRenderer, modifier);
675            }
676            return count;
677        }
678        return save(saveFlags);
679    }
680
681    private static native int nSaveLayer(int renderer, float left, float top,
682            float right, float bottom, int paint, int saveFlags);
683
684    @Override
685    public int saveLayerAlpha(RectF bounds, int alpha, int saveFlags) {
686        if (bounds != null) {
687            return saveLayerAlpha(bounds.left, bounds.top, bounds.right, bounds.bottom,
688                    alpha, saveFlags);
689        }
690        return nSaveLayerAlpha(mRenderer, alpha, saveFlags);
691    }
692
693    private static native int nSaveLayerAlpha(int renderer, int alpha, int saveFlags);
694
695    @Override
696    public int saveLayerAlpha(float left, float top, float right, float bottom, int alpha,
697            int saveFlags) {
698        if (left < right && top < bottom) {
699            return nSaveLayerAlpha(mRenderer, left, top, right, bottom, alpha, saveFlags);
700        }
701        return save(saveFlags);
702    }
703
704    private static native int nSaveLayerAlpha(int renderer, float left, float top, float right,
705            float bottom, int alpha, int saveFlags);
706
707    @Override
708    public void restore() {
709        nRestore(mRenderer);
710    }
711
712    private static native void nRestore(int renderer);
713
714    @Override
715    public void restoreToCount(int saveCount) {
716        nRestoreToCount(mRenderer, saveCount);
717    }
718
719    private static native void nRestoreToCount(int renderer, int saveCount);
720
721    @Override
722    public int getSaveCount() {
723        return nGetSaveCount(mRenderer);
724    }
725
726    private static native int nGetSaveCount(int renderer);
727
728    ///////////////////////////////////////////////////////////////////////////
729    // Filtering
730    ///////////////////////////////////////////////////////////////////////////
731
732    @Override
733    public void setDrawFilter(DrawFilter filter) {
734        mFilter = filter;
735        if (filter == null) {
736            nResetPaintFilter(mRenderer);
737        } else if (filter instanceof PaintFlagsDrawFilter) {
738            PaintFlagsDrawFilter flagsFilter = (PaintFlagsDrawFilter) filter;
739            nSetupPaintFilter(mRenderer, flagsFilter.clearBits, flagsFilter.setBits);
740        }
741    }
742
743    private static native void nResetPaintFilter(int renderer);
744    private static native void nSetupPaintFilter(int renderer, int clearBits, int setBits);
745
746    @Override
747    public DrawFilter getDrawFilter() {
748        return mFilter;
749    }
750
751    ///////////////////////////////////////////////////////////////////////////
752    // Drawing
753    ///////////////////////////////////////////////////////////////////////////
754
755    @Override
756    public void drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter,
757            Paint paint) {
758        int modifiers = setupModifiers(paint, MODIFIER_COLOR_FILTER | MODIFIER_SHADER);
759        try {
760            nDrawArc(mRenderer, oval.left, oval.top, oval.right, oval.bottom,
761                    startAngle, sweepAngle, useCenter, paint.mNativePaint);
762        } finally {
763            if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
764        }
765    }
766
767    private static native void nDrawArc(int renderer, float left, float top,
768            float right, float bottom, float startAngle, float sweepAngle,
769            boolean useCenter, int paint);
770
771    @Override
772    public void drawARGB(int a, int r, int g, int b) {
773        drawColor((a & 0xFF) << 24 | (r & 0xFF) << 16 | (g & 0xFF) << 8 | (b & 0xFF));
774    }
775
776    @Override
777    public void drawPatch(NinePatch patch, Rect dst, Paint paint) {
778        Bitmap bitmap = patch.getBitmap();
779        if (bitmap.isRecycled()) throw new IllegalArgumentException("Cannot draw recycled bitmaps");
780        // Shaders are ignored when drawing patches
781        int modifier = paint != null ? setupColorFilter(paint) : MODIFIER_NONE;
782        try {
783            final int nativePaint = paint == null ? 0 : paint.mNativePaint;
784            nDrawPatch(mRenderer, bitmap.mNativeBitmap, patch.mChunk,
785                    dst.left, dst.top, dst.right, dst.bottom, nativePaint);
786        } finally {
787            if (modifier != MODIFIER_NONE) nResetModifiers(mRenderer, modifier);
788        }
789    }
790
791    @Override
792    public void drawPatch(NinePatch patch, RectF dst, Paint paint) {
793        Bitmap bitmap = patch.getBitmap();
794        if (bitmap.isRecycled()) throw new IllegalArgumentException("Cannot draw recycled bitmaps");
795        // Shaders are ignored when drawing patches
796        int modifier = paint != null ? setupColorFilter(paint) : MODIFIER_NONE;
797        try {
798            final int nativePaint = paint == null ? 0 : paint.mNativePaint;
799            nDrawPatch(mRenderer, bitmap.mNativeBitmap, patch.mChunk,
800                    dst.left, dst.top, dst.right, dst.bottom, nativePaint);
801        } finally {
802            if (modifier != MODIFIER_NONE) nResetModifiers(mRenderer, modifier);
803        }
804    }
805
806    private static native void nDrawPatch(int renderer, int bitmap, byte[] chunks,
807            float left, float top, float right, float bottom, int paint);
808
809    @Override
810    public void drawBitmap(Bitmap bitmap, float left, float top, Paint paint) {
811        if (bitmap.isRecycled()) throw new IllegalArgumentException("Cannot draw recycled bitmaps");
812        // Shaders are ignored when drawing bitmaps
813        int modifiers = paint != null ? setupModifiers(bitmap, paint) : MODIFIER_NONE;
814        try {
815            final int nativePaint = paint == null ? 0 : paint.mNativePaint;
816            nDrawBitmap(mRenderer, bitmap.mNativeBitmap, left, top, nativePaint);
817        } finally {
818            if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
819        }
820    }
821
822    private static native void nDrawBitmap(int renderer, int bitmap,
823            float left, float top, int paint);
824
825    @Override
826    public void drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint) {
827        if (bitmap.isRecycled()) throw new IllegalArgumentException("Cannot draw recycled bitmaps");
828        // Shaders are ignored when drawing bitmaps
829        int modifiers = paint != null ? setupModifiers(bitmap, paint) : MODIFIER_NONE;
830        try {
831            final int nativePaint = paint == null ? 0 : paint.mNativePaint;
832            nDrawBitmap(mRenderer, bitmap.mNativeBitmap,  matrix.native_instance, nativePaint);
833        } finally {
834            if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
835        }
836    }
837
838    private static native void nDrawBitmap(int renderer, int bitmap, int matrix, int paint);
839
840    @Override
841    public void drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint) {
842        if (bitmap.isRecycled()) throw new IllegalArgumentException("Cannot draw recycled bitmaps");
843        // Shaders are ignored when drawing bitmaps
844        int modifiers = paint != null ? setupModifiers(bitmap, paint) : MODIFIER_NONE;
845        try {
846            final int nativePaint = paint == null ? 0 : paint.mNativePaint;
847
848            int left, top, right, bottom;
849            if (src == null) {
850                left = top = 0;
851                right = bitmap.getWidth();
852                bottom = bitmap.getHeight();
853            } else {
854                left = src.left;
855                right = src.right;
856                top = src.top;
857                bottom = src.bottom;
858            }
859
860            nDrawBitmap(mRenderer, bitmap.mNativeBitmap, left, top, right, bottom,
861                    dst.left, dst.top, dst.right, dst.bottom, nativePaint);
862        } finally {
863            if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
864        }
865    }
866
867    @Override
868    public void drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint) {
869        if (bitmap.isRecycled()) throw new IllegalArgumentException("Cannot draw recycled bitmaps");
870        // Shaders are ignored when drawing bitmaps
871        int modifiers = paint != null ? setupModifiers(bitmap, paint) : MODIFIER_NONE;
872        try {
873            final int nativePaint = paint == null ? 0 : paint.mNativePaint;
874
875            float left, top, right, bottom;
876            if (src == null) {
877                left = top = 0;
878                right = bitmap.getWidth();
879                bottom = bitmap.getHeight();
880            } else {
881                left = src.left;
882                right = src.right;
883                top = src.top;
884                bottom = src.bottom;
885            }
886
887            nDrawBitmap(mRenderer, bitmap.mNativeBitmap, left, top, right, bottom,
888                    dst.left, dst.top, dst.right, dst.bottom, nativePaint);
889        } finally {
890            if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
891        }
892    }
893
894    private static native void nDrawBitmap(int renderer, int bitmap,
895            float srcLeft, float srcTop, float srcRight, float srcBottom,
896            float left, float top, float right, float bottom, int paint);
897
898    @Override
899    public void drawBitmap(int[] colors, int offset, int stride, float x, float y,
900            int width, int height, boolean hasAlpha, Paint paint) {
901        if (width < 0) {
902            throw new IllegalArgumentException("width must be >= 0");
903        }
904
905        if (height < 0) {
906            throw new IllegalArgumentException("height must be >= 0");
907        }
908
909        if (Math.abs(stride) < width) {
910            throw new IllegalArgumentException("abs(stride) must be >= width");
911        }
912
913        int lastScanline = offset + (height - 1) * stride;
914        int length = colors.length;
915
916        if (offset < 0 || (offset + width > length) || lastScanline < 0 ||
917                (lastScanline + width > length)) {
918            throw new ArrayIndexOutOfBoundsException();
919        }
920
921        // Shaders are ignored when drawing bitmaps
922        int modifier = paint != null ? setupColorFilter(paint) : MODIFIER_NONE;
923        try {
924            final int nativePaint = paint == null ? 0 : paint.mNativePaint;
925            nDrawBitmap(mRenderer, colors, offset, stride, x, y,
926                    width, height, hasAlpha, nativePaint);
927        } finally {
928            if (modifier != MODIFIER_NONE) nResetModifiers(mRenderer, modifier);
929        }
930    }
931
932    private static native void nDrawBitmap(int renderer, int[] colors, int offset, int stride,
933            float x, float y, int width, int height, boolean hasAlpha, int nativePaint);
934
935    @Override
936    public void drawBitmap(int[] colors, int offset, int stride, int x, int y,
937            int width, int height, boolean hasAlpha, Paint paint) {
938        // Shaders are ignored when drawing bitmaps
939        drawBitmap(colors, offset, stride, (float) x, (float) y, width, height, hasAlpha, paint);
940    }
941
942    @Override
943    public void drawBitmapMesh(Bitmap bitmap, int meshWidth, int meshHeight, float[] verts,
944            int vertOffset, int[] colors, int colorOffset, Paint paint) {
945        if (bitmap.isRecycled()) throw new IllegalArgumentException("Cannot draw recycled bitmaps");
946        if (meshWidth < 0 || meshHeight < 0 || vertOffset < 0 || colorOffset < 0) {
947            throw new ArrayIndexOutOfBoundsException();
948        }
949
950        if (meshWidth == 0 || meshHeight == 0) {
951            return;
952        }
953
954        final int count = (meshWidth + 1) * (meshHeight + 1);
955        checkRange(verts.length, vertOffset, count * 2);
956
957        if (colors != null) {
958            checkRange(colors.length, colorOffset, count);
959        }
960
961        int modifiers = paint != null ? setupModifiers(bitmap, paint) : MODIFIER_NONE;
962        try {
963            final int nativePaint = paint == null ? 0 : paint.mNativePaint;
964            nDrawBitmapMesh(mRenderer, bitmap.mNativeBitmap, meshWidth, meshHeight,
965                    verts, vertOffset, colors, colorOffset, nativePaint);
966        } finally {
967            if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
968        }
969    }
970
971    private static native void nDrawBitmapMesh(int renderer, int bitmap,
972            int meshWidth, int meshHeight, float[] verts, int vertOffset,
973            int[] colors, int colorOffset, int paint);
974
975    @Override
976    public void drawCircle(float cx, float cy, float radius, Paint paint) {
977        int modifiers = setupModifiers(paint, MODIFIER_COLOR_FILTER | MODIFIER_SHADER);
978        try {
979            nDrawCircle(mRenderer, cx, cy, radius, paint.mNativePaint);
980        } finally {
981            if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
982        }
983    }
984
985    private static native void nDrawCircle(int renderer, float cx, float cy,
986            float radius, int paint);
987
988    @Override
989    public void drawColor(int color) {
990        drawColor(color, PorterDuff.Mode.SRC_OVER);
991    }
992
993    @Override
994    public void drawColor(int color, PorterDuff.Mode mode) {
995        nDrawColor(mRenderer, color, mode.nativeInt);
996    }
997
998    private static native void nDrawColor(int renderer, int color, int mode);
999
1000    @Override
1001    public void drawLine(float startX, float startY, float stopX, float stopY, Paint paint) {
1002        float[] line = getLineStorage();
1003        line[0] = startX;
1004        line[1] = startY;
1005        line[2] = stopX;
1006        line[3] = stopY;
1007        drawLines(line, 0, 4, paint);
1008    }
1009
1010    @Override
1011    public void drawLines(float[] pts, int offset, int count, Paint paint) {
1012        if (count < 4) return;
1013
1014        if ((offset | count) < 0 || offset + count > pts.length) {
1015            throw new IllegalArgumentException("The lines array must contain 4 elements per line.");
1016        }
1017        int modifiers = setupModifiers(paint, MODIFIER_COLOR_FILTER | MODIFIER_SHADER);
1018        try {
1019            nDrawLines(mRenderer, pts, offset, count, paint.mNativePaint);
1020        } finally {
1021            if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
1022        }
1023    }
1024
1025    private static native void nDrawLines(int renderer, float[] points,
1026            int offset, int count, int paint);
1027
1028    @Override
1029    public void drawLines(float[] pts, Paint paint) {
1030        drawLines(pts, 0, pts.length, paint);
1031    }
1032
1033    @Override
1034    public void drawOval(RectF oval, Paint paint) {
1035        int modifiers = setupModifiers(paint, MODIFIER_COLOR_FILTER | MODIFIER_SHADER);
1036        try {
1037            nDrawOval(mRenderer, oval.left, oval.top, oval.right, oval.bottom, paint.mNativePaint);
1038        } finally {
1039            if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
1040        }
1041    }
1042
1043    private static native void nDrawOval(int renderer, float left, float top,
1044            float right, float bottom, int paint);
1045
1046    @Override
1047    public void drawPaint(Paint paint) {
1048        final Rect r = getInternalClipBounds();
1049        nGetClipBounds(mRenderer, r);
1050        drawRect(r.left, r.top, r.right, r.bottom, paint);
1051    }
1052
1053    @Override
1054    public void drawPath(Path path, Paint paint) {
1055        int modifiers = setupModifiers(paint, MODIFIER_COLOR_FILTER | MODIFIER_SHADER);
1056        try {
1057            if (path.isSimplePath) {
1058                if (path.rects != null) {
1059                    nDrawRects(mRenderer, path.rects.mNativeRegion, paint.mNativePaint);
1060                }
1061            } else {
1062                nDrawPath(mRenderer, path.mNativePath, paint.mNativePaint);
1063            }
1064        } finally {
1065            if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
1066        }
1067    }
1068
1069    private static native void nDrawPath(int renderer, int path, int paint);
1070    private static native void nDrawRects(int renderer, int region, int paint);
1071
1072    void drawRects(float[] rects, int count, Paint paint) {
1073        int modifiers = setupModifiers(paint, MODIFIER_COLOR_FILTER | MODIFIER_SHADER);
1074        try {
1075            nDrawRects(mRenderer, rects, count, paint.mNativePaint);
1076        } finally {
1077            if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
1078        }
1079    }
1080
1081    private static native void nDrawRects(int renderer, float[] rects, int count, int paint);
1082
1083    @Override
1084    public void drawPicture(Picture picture) {
1085        if (picture.createdFromStream) {
1086            return;
1087        }
1088
1089        picture.endRecording();
1090        // TODO: Implement rendering
1091    }
1092
1093    @Override
1094    public void drawPicture(Picture picture, Rect dst) {
1095        if (picture.createdFromStream) {
1096            return;
1097        }
1098
1099        save();
1100        translate(dst.left, dst.top);
1101        if (picture.getWidth() > 0 && picture.getHeight() > 0) {
1102            scale(dst.width() / picture.getWidth(), dst.height() / picture.getHeight());
1103        }
1104        drawPicture(picture);
1105        restore();
1106    }
1107
1108    @Override
1109    public void drawPicture(Picture picture, RectF dst) {
1110        if (picture.createdFromStream) {
1111            return;
1112        }
1113
1114        save();
1115        translate(dst.left, dst.top);
1116        if (picture.getWidth() > 0 && picture.getHeight() > 0) {
1117            scale(dst.width() / picture.getWidth(), dst.height() / picture.getHeight());
1118        }
1119        drawPicture(picture);
1120        restore();
1121    }
1122
1123    @Override
1124    public void drawPoint(float x, float y, Paint paint) {
1125        float[] point = getPointStorage();
1126        point[0] = x;
1127        point[1] = y;
1128        drawPoints(point, 0, 2, paint);
1129    }
1130
1131    @Override
1132    public void drawPoints(float[] pts, Paint paint) {
1133        drawPoints(pts, 0, pts.length, paint);
1134    }
1135
1136    @Override
1137    public void drawPoints(float[] pts, int offset, int count, Paint paint) {
1138        if (count < 2) return;
1139
1140        int modifiers = setupModifiers(paint, MODIFIER_COLOR_FILTER | MODIFIER_SHADER);
1141        try {
1142            nDrawPoints(mRenderer, pts, offset, count, paint.mNativePaint);
1143        } finally {
1144            if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
1145        }
1146    }
1147
1148    private static native void nDrawPoints(int renderer, float[] points,
1149            int offset, int count, int paint);
1150
1151    @SuppressWarnings("deprecation")
1152    @Override
1153    public void drawPosText(char[] text, int index, int count, float[] pos, Paint paint) {
1154        if (index < 0 || index + count > text.length || count * 2 > pos.length) {
1155            throw new IndexOutOfBoundsException();
1156        }
1157
1158        int modifiers = setupModifiers(paint);
1159        try {
1160            nDrawPosText(mRenderer, text, index, count, pos, paint.mNativePaint);
1161        } finally {
1162            if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
1163        }
1164    }
1165
1166    private static native void nDrawPosText(int renderer, char[] text, int index, int count,
1167            float[] pos, int paint);
1168
1169    @SuppressWarnings("deprecation")
1170    @Override
1171    public void drawPosText(String text, float[] pos, Paint paint) {
1172        if (text.length() * 2 > pos.length) {
1173            throw new ArrayIndexOutOfBoundsException();
1174        }
1175
1176        int modifiers = setupModifiers(paint);
1177        try {
1178            nDrawPosText(mRenderer, text, 0, text.length(), pos, paint.mNativePaint);
1179        } finally {
1180            if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
1181        }
1182    }
1183
1184    private static native void nDrawPosText(int renderer, String text, int start, int end,
1185            float[] pos, int paint);
1186
1187    @Override
1188    public void drawRect(float left, float top, float right, float bottom, Paint paint) {
1189        if (left == right || top == bottom) return;
1190        int modifiers = setupModifiers(paint, MODIFIER_COLOR_FILTER | MODIFIER_SHADER);
1191        try {
1192            nDrawRect(mRenderer, left, top, right, bottom, paint.mNativePaint);
1193        } finally {
1194            if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
1195        }
1196    }
1197
1198    private static native void nDrawRect(int renderer, float left, float top,
1199            float right, float bottom, int paint);
1200
1201    @Override
1202    public void drawRect(Rect r, Paint paint) {
1203        drawRect(r.left, r.top, r.right, r.bottom, paint);
1204    }
1205
1206    @Override
1207    public void drawRect(RectF r, Paint paint) {
1208        drawRect(r.left, r.top, r.right, r.bottom, paint);
1209    }
1210
1211    @Override
1212    public void drawRGB(int r, int g, int b) {
1213        drawColor(0xFF000000 | (r & 0xFF) << 16 | (g & 0xFF) << 8 | (b & 0xFF));
1214    }
1215
1216    @Override
1217    public void drawRoundRect(RectF rect, float rx, float ry, Paint paint) {
1218        int modifiers = setupModifiers(paint, MODIFIER_COLOR_FILTER | MODIFIER_SHADER);
1219        try {
1220            nDrawRoundRect(mRenderer, rect.left, rect.top, rect.right, rect.bottom,
1221                    rx, ry, paint.mNativePaint);
1222        } finally {
1223            if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
1224        }
1225    }
1226
1227    private static native void nDrawRoundRect(int renderer, float left, float top,
1228            float right, float bottom, float rx, float y, int paint);
1229
1230    @Override
1231    public void drawText(char[] text, int index, int count, float x, float y, Paint paint) {
1232        if ((index | count | (index + count) | (text.length - index - count)) < 0) {
1233            throw new IndexOutOfBoundsException();
1234        }
1235
1236        int modifiers = setupModifiers(paint);
1237        try {
1238            nDrawText(mRenderer, text, index, count, x, y, paint.mBidiFlags, paint.mNativePaint);
1239        } finally {
1240            if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
1241        }
1242    }
1243
1244    private static native void nDrawText(int renderer, char[] text, int index, int count,
1245            float x, float y, int bidiFlags, int paint);
1246
1247    @Override
1248    public void drawText(CharSequence text, int start, int end, float x, float y, Paint paint) {
1249        int modifiers = setupModifiers(paint);
1250        try {
1251            if (text instanceof String || text instanceof SpannedString ||
1252                    text instanceof SpannableString) {
1253                nDrawText(mRenderer, text.toString(), start, end, x, y, paint.mBidiFlags,
1254                        paint.mNativePaint);
1255            } else if (text instanceof GraphicsOperations) {
1256                ((GraphicsOperations) text).drawText(this, start, end, x, y,
1257                                                         paint);
1258            } else {
1259                char[] buf = TemporaryBuffer.obtain(end - start);
1260                TextUtils.getChars(text, start, end, buf, 0);
1261                nDrawText(mRenderer, buf, 0, end - start, x, y,
1262                        paint.mBidiFlags, paint.mNativePaint);
1263                TemporaryBuffer.recycle(buf);
1264            }
1265        } finally {
1266            if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
1267        }
1268    }
1269
1270    @Override
1271    public void drawText(String text, int start, int end, float x, float y, Paint paint) {
1272        if ((start | end | (end - start) | (text.length() - end)) < 0) {
1273            throw new IndexOutOfBoundsException();
1274        }
1275
1276        int modifiers = setupModifiers(paint);
1277        try {
1278            nDrawText(mRenderer, text, start, end, x, y, paint.mBidiFlags, paint.mNativePaint);
1279        } finally {
1280            if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
1281        }
1282    }
1283
1284    private static native void nDrawText(int renderer, String text, int start, int end,
1285            float x, float y, int bidiFlags, int paint);
1286
1287    @Override
1288    public void drawText(String text, float x, float y, Paint paint) {
1289        int modifiers = setupModifiers(paint);
1290        try {
1291            nDrawText(mRenderer, text, 0, text.length(), x, y, paint.mBidiFlags,
1292                    paint.mNativePaint);
1293        } finally {
1294            if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
1295        }
1296    }
1297
1298    @Override
1299    public void drawTextOnPath(char[] text, int index, int count, Path path, float hOffset,
1300            float vOffset, Paint paint) {
1301        if (index < 0 || index + count > text.length) {
1302            throw new ArrayIndexOutOfBoundsException();
1303        }
1304
1305        int modifiers = setupModifiers(paint);
1306        try {
1307            nDrawTextOnPath(mRenderer, text, index, count, path.mNativePath, hOffset, vOffset,
1308                    paint.mBidiFlags, paint.mNativePaint);
1309        } finally {
1310            if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
1311        }
1312    }
1313
1314    private static native void nDrawTextOnPath(int renderer, char[] text, int index, int count,
1315            int path, float hOffset, float vOffset, int bidiFlags, int nativePaint);
1316
1317    @Override
1318    public void drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint) {
1319        if (text.length() == 0) return;
1320
1321        int modifiers = setupModifiers(paint);
1322        try {
1323            nDrawTextOnPath(mRenderer, text, 0, text.length(), path.mNativePath, hOffset, vOffset,
1324                    paint.mBidiFlags, paint.mNativePaint);
1325        } finally {
1326            if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
1327        }
1328    }
1329
1330    private static native void nDrawTextOnPath(int renderer, String text, int start, int end,
1331            int path, float hOffset, float vOffset, int bidiFlags, int nativePaint);
1332
1333    @Override
1334    public void drawTextRun(char[] text, int index, int count, int contextIndex, int contextCount,
1335            float x, float y, int dir, Paint paint) {
1336        if ((index | count | text.length - index - count) < 0) {
1337            throw new IndexOutOfBoundsException();
1338        }
1339        if (dir != DIRECTION_LTR && dir != DIRECTION_RTL) {
1340            throw new IllegalArgumentException("Unknown direction: " + dir);
1341        }
1342
1343        int modifiers = setupModifiers(paint);
1344        try {
1345            nDrawTextRun(mRenderer, text, index, count, contextIndex, contextCount, x, y, dir,
1346                    paint.mNativePaint);
1347        } finally {
1348            if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
1349        }
1350    }
1351
1352    private static native void nDrawTextRun(int renderer, char[] text, int index, int count,
1353            int contextIndex, int contextCount, float x, float y, int dir, int nativePaint);
1354
1355    @Override
1356    public void drawTextRun(CharSequence text, int start, int end, int contextStart, int contextEnd,
1357            float x, float y, int dir, Paint paint) {
1358        if ((start | end | end - start | text.length() - end) < 0) {
1359            throw new IndexOutOfBoundsException();
1360        }
1361
1362        int modifiers = setupModifiers(paint);
1363        try {
1364            int flags = dir == 0 ? 0 : 1;
1365            if (text instanceof String || text instanceof SpannedString ||
1366                    text instanceof SpannableString) {
1367                nDrawTextRun(mRenderer, text.toString(), start, end, contextStart,
1368                        contextEnd, x, y, flags, paint.mNativePaint);
1369            } else if (text instanceof GraphicsOperations) {
1370                ((GraphicsOperations) text).drawTextRun(this, start, end,
1371                        contextStart, contextEnd, x, y, flags, paint);
1372            } else {
1373                int contextLen = contextEnd - contextStart;
1374                int len = end - start;
1375                char[] buf = TemporaryBuffer.obtain(contextLen);
1376                TextUtils.getChars(text, contextStart, contextEnd, buf, 0);
1377                nDrawTextRun(mRenderer, buf, start - contextStart, len, 0, contextLen,
1378                        x, y, flags, paint.mNativePaint);
1379                TemporaryBuffer.recycle(buf);
1380            }
1381        } finally {
1382            if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
1383        }
1384    }
1385
1386    private static native void nDrawTextRun(int renderer, String text, int start, int end,
1387            int contextStart, int contextEnd, float x, float y, int flags, int nativePaint);
1388
1389    @Override
1390    public void drawVertices(VertexMode mode, int vertexCount, float[] verts, int vertOffset,
1391            float[] texs, int texOffset, int[] colors, int colorOffset, short[] indices,
1392            int indexOffset, int indexCount, Paint paint) {
1393        // TODO: Implement
1394    }
1395
1396    private int setupModifiers(Bitmap b, Paint paint) {
1397        if (b.getConfig() != Bitmap.Config.ALPHA_8) {
1398            final ColorFilter filter = paint.getColorFilter();
1399            if (filter != null) {
1400                nSetupColorFilter(mRenderer, filter.nativeColorFilter);
1401                return MODIFIER_COLOR_FILTER;
1402            }
1403
1404            return MODIFIER_NONE;
1405        } else {
1406            return setupModifiers(paint);
1407        }
1408    }
1409
1410    private int setupModifiers(Paint paint) {
1411        int modifiers = MODIFIER_NONE;
1412
1413        if (paint.hasShadow) {
1414            nSetupShadow(mRenderer, paint.shadowRadius, paint.shadowDx, paint.shadowDy,
1415                    paint.shadowColor);
1416            modifiers |= MODIFIER_SHADOW;
1417        }
1418
1419        final Shader shader = paint.getShader();
1420        if (shader != null) {
1421            nSetupShader(mRenderer, shader.native_shader);
1422            modifiers |= MODIFIER_SHADER;
1423        }
1424
1425        final ColorFilter filter = paint.getColorFilter();
1426        if (filter != null) {
1427            nSetupColorFilter(mRenderer, filter.nativeColorFilter);
1428            modifiers |= MODIFIER_COLOR_FILTER;
1429        }
1430
1431        return modifiers;
1432    }
1433
1434    private int setupModifiers(Paint paint, int flags) {
1435        int modifiers = MODIFIER_NONE;
1436
1437        if (paint.hasShadow && (flags & MODIFIER_SHADOW) != 0) {
1438            nSetupShadow(mRenderer, paint.shadowRadius, paint.shadowDx, paint.shadowDy,
1439                    paint.shadowColor);
1440            modifiers |= MODIFIER_SHADOW;
1441        }
1442
1443        final Shader shader = paint.getShader();
1444        if (shader != null && (flags & MODIFIER_SHADER) != 0) {
1445            nSetupShader(mRenderer, shader.native_shader);
1446            modifiers |= MODIFIER_SHADER;
1447        }
1448
1449        final ColorFilter filter = paint.getColorFilter();
1450        if (filter != null && (flags & MODIFIER_COLOR_FILTER) != 0) {
1451            nSetupColorFilter(mRenderer, filter.nativeColorFilter);
1452            modifiers |= MODIFIER_COLOR_FILTER;
1453        }
1454
1455        return modifiers;
1456    }
1457
1458    private int setupColorFilter(Paint paint) {
1459        final ColorFilter filter = paint.getColorFilter();
1460        if (filter != null) {
1461            nSetupColorFilter(mRenderer, filter.nativeColorFilter);
1462            return MODIFIER_COLOR_FILTER;
1463        }
1464        return MODIFIER_NONE;
1465    }
1466
1467    private static native void nSetupShader(int renderer, int shader);
1468    private static native void nSetupColorFilter(int renderer, int colorFilter);
1469    private static native void nSetupShadow(int renderer, float radius,
1470            float dx, float dy, int color);
1471
1472    private static native void nResetModifiers(int renderer, int modifiers);
1473}
1474