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