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