GLES20Canvas.java revision 9ace8f5e79e76893fe4ca9e4d10f6c4056330485
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.Path;
26import android.graphics.Picture;
27import android.graphics.PorterDuff;
28import android.graphics.Rect;
29import android.graphics.RectF;
30import android.graphics.Region;
31import android.graphics.Shader;
32import android.graphics.SurfaceTexture;
33import android.graphics.TemporaryBuffer;
34import android.text.GraphicsOperations;
35import android.text.SpannableString;
36import android.text.SpannedString;
37import android.text.TextUtils;
38
39/**
40 * An implementation of Canvas on top of OpenGL ES 2.0.
41 */
42class GLES20Canvas extends HardwareCanvas {
43    // Must match modifiers used in the JNI layer
44    private static final int MODIFIER_NONE = 0;
45    private static final int MODIFIER_SHADOW = 1;
46    private static final int MODIFIER_SHADER = 2;
47    private static final int MODIFIER_COLOR_FILTER = 4;
48
49    private final boolean mOpaque;
50    private int mRenderer;
51
52    // The native renderer will be destroyed when this object dies.
53    // DO NOT overwrite this reference once it is set.
54    private CanvasFinalizer mFinalizer;
55
56    private int mWidth;
57    private int mHeight;
58
59    private final float[] mPoint = new float[2];
60    private final float[] mLine = new float[4];
61
62    private final Rect mClipBounds = new Rect();
63
64    private DrawFilter mFilter;
65
66    ///////////////////////////////////////////////////////////////////////////
67    // JNI
68    ///////////////////////////////////////////////////////////////////////////
69
70    private static native boolean nIsAvailable();
71    private static boolean sIsAvailable = nIsAvailable();
72
73    static boolean isAvailable() {
74        return sIsAvailable;
75    }
76
77    ///////////////////////////////////////////////////////////////////////////
78    // Constructors
79    ///////////////////////////////////////////////////////////////////////////
80
81    /**
82     * Creates a canvas to render directly on screen.
83     */
84    GLES20Canvas(boolean translucent) {
85        this(false, translucent);
86    }
87
88    /**
89     * Creates a canvas to render into an FBO.
90     */
91    GLES20Canvas(int layer, boolean translucent) {
92        mOpaque = !translucent;
93        mRenderer = nCreateLayerRenderer(layer);
94        setupFinalizer();
95    }
96
97    protected GLES20Canvas(boolean record, boolean translucent) {
98        mOpaque = !translucent;
99
100        setupRenderer(record);
101    }
102
103    protected void setupRenderer(boolean record) {
104        if (record) {
105            mRenderer = nGetDisplayListRenderer(mRenderer);
106        } else {
107            mRenderer = nCreateRenderer();
108        }
109
110        setupFinalizer();
111    }
112
113    private void setupFinalizer() {
114        if (mRenderer == 0) {
115            throw new IllegalStateException("Could not create GLES20Canvas renderer");
116        } else {
117            mFinalizer = CanvasFinalizer.getFinalizer(mFinalizer, mRenderer);
118        }
119    }
120
121    private static native int nCreateRenderer();
122    private static native int nCreateLayerRenderer(int layer);
123    private static native int nGetDisplayListRenderer(int renderer);
124    private static native void nDestroyRenderer(int renderer);
125
126    private static class CanvasFinalizer {
127        int mRenderer;
128
129        // Factory method returns new instance if old one is null, or old instance
130        // otherwise, destroying native renderer along the way as necessary
131        static CanvasFinalizer getFinalizer(CanvasFinalizer oldFinalizer, int renderer) {
132            if (oldFinalizer == null) {
133                return new CanvasFinalizer(renderer);
134            }
135            oldFinalizer.replaceNativeObject(renderer);
136            return oldFinalizer;
137        }
138
139        private CanvasFinalizer(int renderer) {
140            mRenderer = renderer;
141        }
142
143        private void replaceNativeObject(int newRenderer) {
144            if (mRenderer != 0 && newRenderer != mRenderer) {
145                nDestroyRenderer(mRenderer);
146            }
147            mRenderer = newRenderer;
148        }
149
150        @Override
151        protected void finalize() throws Throwable {
152            try {
153                replaceNativeObject(0);
154            } finally {
155                super.finalize();
156            }
157        }
158    }
159
160    ///////////////////////////////////////////////////////////////////////////
161    // Hardware layers
162    ///////////////////////////////////////////////////////////////////////////
163
164    static native int nCreateTextureLayer(boolean opaque, int[] layerInfo);
165    static native int nCreateLayer(int width, int height, boolean isOpaque, int[] layerInfo);
166    static native void nResizeLayer(int layerId, int width, int height, int[] layerInfo);
167    static native void nUpdateTextureLayer(int layerId, int width, int height, boolean opaque,
168            SurfaceTexture surface);
169    static native void nDestroyLayer(int layerId);
170    static native void nDestroyLayerDeferred(int layerId);
171    static native boolean nCopyLayer(int layerId, int bitmap);
172
173    ///////////////////////////////////////////////////////////////////////////
174    // Canvas management
175    ///////////////////////////////////////////////////////////////////////////
176
177    @Override
178    public boolean isOpaque() {
179        return mOpaque;
180    }
181
182    @Override
183    public int getWidth() {
184        return mWidth;
185    }
186
187    @Override
188    public int getHeight() {
189        return mHeight;
190    }
191
192    @Override
193    public int getMaximumBitmapWidth() {
194        return nGetMaximumTextureWidth();
195    }
196
197    @Override
198    public int getMaximumBitmapHeight() {
199        return nGetMaximumTextureHeight();
200    }
201
202    private static native int nGetMaximumTextureWidth();
203    private static native int nGetMaximumTextureHeight();
204
205    ///////////////////////////////////////////////////////////////////////////
206    // Setup
207    ///////////////////////////////////////////////////////////////////////////
208
209    @Override
210    public void setViewport(int width, int height) {
211        mWidth = width;
212        mHeight = height;
213
214        nSetViewport(mRenderer, width, height);
215    }
216
217    private static native void nSetViewport(int renderer, int width, int height);
218
219    /**
220     * Preserves the back buffer of the current surface after a buffer swap.
221     * Calling this method sets the EGL_SWAP_BEHAVIOR attribute of the current
222     * surface to EGL_BUFFER_PRESERVED. Calling this method requires an EGL
223     * config that supports EGL_SWAP_BEHAVIOR_PRESERVED_BIT.
224     *
225     * @return True if the swap behavior was successfully changed,
226     *         false otherwise.
227     *
228     * @hide
229     */
230    public static boolean preserveBackBuffer() {
231        return nPreserveBackBuffer();
232    }
233
234    private static native boolean nPreserveBackBuffer();
235
236    /**
237     * Indicates whether the current surface preserves its back buffer
238     * after a buffer swap.
239     *
240     * @return True, if the surface's EGL_SWAP_BEHAVIOR is EGL_BUFFER_PRESERVED,
241     *         false otherwise
242     *
243     * @hide
244     */
245    public static boolean isBackBufferPreserved() {
246        return nIsBackBufferPreserved();
247    }
248
249    private static native boolean nIsBackBufferPreserved();
250
251    /**
252     * Disables v-sync. For performance testing only.
253     *
254     * @hide
255     */
256    public static void disableVsync() {
257        nDisableVsync();
258    }
259
260    private static native void nDisableVsync();
261
262    @Override
263    void onPreDraw(Rect dirty) {
264        if (dirty != null) {
265            nPrepareDirty(mRenderer, dirty.left, dirty.top, dirty.right, dirty.bottom, mOpaque);
266        } else {
267            nPrepare(mRenderer, mOpaque);
268        }
269    }
270
271    private static native void nPrepare(int renderer, boolean opaque);
272    private static native void nPrepareDirty(int renderer, int left, int top, int right, int bottom,
273            boolean opaque);
274
275    @Override
276    void onPostDraw() {
277        nFinish(mRenderer);
278    }
279
280    private static native void nFinish(int renderer);
281
282    @Override
283    public boolean callDrawGLFunction(int drawGLFunction) {
284        return nCallDrawGLFunction(mRenderer, drawGLFunction);
285    }
286
287    private static native boolean nCallDrawGLFunction(int renderer, int drawGLFunction);
288
289    ///////////////////////////////////////////////////////////////////////////
290    // Display list
291    ///////////////////////////////////////////////////////////////////////////
292
293    int getDisplayList() {
294        return nGetDisplayList(mRenderer);
295    }
296
297    private static native int nGetDisplayList(int renderer);
298
299    static void destroyDisplayList(int displayList) {
300        nDestroyDisplayList(displayList);
301    }
302
303    private static native void nDestroyDisplayList(int displayList);
304
305    @Override
306    public boolean drawDisplayList(DisplayList displayList, int width, int height, Rect dirty) {
307        return nDrawDisplayList(mRenderer,
308                ((GLES20DisplayList) displayList).mNativeDisplayList, width, height, dirty);
309    }
310
311    private static native boolean nDrawDisplayList(int renderer, int displayList,
312            int width, int height, Rect dirty);
313
314    @Override
315    void outputDisplayList(DisplayList displayList) {
316        nOutputDisplayList(mRenderer, ((GLES20DisplayList) displayList).mNativeDisplayList);
317    }
318
319    private static native void nOutputDisplayList(int renderer, int displayList);
320
321    ///////////////////////////////////////////////////////////////////////////
322    // Hardware layer
323    ///////////////////////////////////////////////////////////////////////////
324
325    void drawHardwareLayer(HardwareLayer layer, float x, float y, Paint paint) {
326        final GLES20Layer glLayer = (GLES20Layer) layer;
327        int modifier = paint != null ? setupColorFilter(paint) : MODIFIER_NONE;
328        final int nativePaint = paint == null ? 0 : paint.mNativePaint;
329        nDrawLayer(mRenderer, glLayer.getLayer(), x, y, nativePaint);
330        if (modifier != MODIFIER_NONE) nResetModifiers(mRenderer, modifier);
331    }
332
333    private static native void nDrawLayer(int renderer, int layer, float x, float y, int paint);
334
335    void interrupt() {
336        nInterrupt(mRenderer);
337    }
338
339    void resume() {
340        nResume(mRenderer);
341    }
342
343    private static native void nInterrupt(int renderer);
344    private static native void nResume(int renderer);
345
346    ///////////////////////////////////////////////////////////////////////////
347    // Clipping
348    ///////////////////////////////////////////////////////////////////////////
349
350    @Override
351    public boolean clipPath(Path path) {
352        throw new UnsupportedOperationException();
353    }
354
355    @Override
356    public boolean clipPath(Path path, Region.Op op) {
357        throw new UnsupportedOperationException();
358    }
359
360    @Override
361    public boolean clipRect(float left, float top, float right, float bottom) {
362        return nClipRect(mRenderer, left, top, right, bottom, Region.Op.INTERSECT.nativeInt);
363    }
364
365    private static native boolean nClipRect(int renderer, float left, float top,
366            float right, float bottom, int op);
367
368    @Override
369    public boolean clipRect(float left, float top, float right, float bottom, Region.Op op) {
370        return nClipRect(mRenderer, left, top, right, bottom, op.nativeInt);
371    }
372
373    @Override
374    public boolean clipRect(int left, int top, int right, int bottom) {
375        return nClipRect(mRenderer, left, top, right, bottom, Region.Op.INTERSECT.nativeInt);
376    }
377
378    private static native boolean nClipRect(int renderer, int left, int top, int right, int bottom,
379            int op);
380
381    @Override
382    public boolean clipRect(Rect rect) {
383        return nClipRect(mRenderer, rect.left, rect.top, rect.right, rect.bottom,
384                Region.Op.INTERSECT.nativeInt);
385    }
386
387    @Override
388    public boolean clipRect(Rect rect, Region.Op op) {
389        return nClipRect(mRenderer, rect.left, rect.top, rect.right, rect.bottom, op.nativeInt);
390    }
391
392    @Override
393    public boolean clipRect(RectF rect) {
394        return nClipRect(mRenderer, rect.left, rect.top, rect.right, rect.bottom,
395                Region.Op.INTERSECT.nativeInt);
396    }
397
398    @Override
399    public boolean clipRect(RectF rect, Region.Op op) {
400        return nClipRect(mRenderer, rect.left, rect.top, rect.right, rect.bottom, op.nativeInt);
401    }
402
403    @Override
404    public boolean clipRegion(Region region) {
405        throw new UnsupportedOperationException();
406    }
407
408    @Override
409    public boolean clipRegion(Region region, Region.Op op) {
410        throw new UnsupportedOperationException();
411    }
412
413    @Override
414    public boolean getClipBounds(Rect bounds) {
415        return nGetClipBounds(mRenderer, bounds);
416    }
417
418    private static native boolean nGetClipBounds(int renderer, Rect bounds);
419
420    @Override
421    public boolean quickReject(float left, float top, float right, float bottom, EdgeType type) {
422        return nQuickReject(mRenderer, left, top, right, bottom, type.nativeInt);
423    }
424
425    private static native boolean nQuickReject(int renderer, float left, float top,
426            float right, float bottom, int edge);
427
428    @Override
429    public boolean quickReject(Path path, EdgeType type) {
430        throw new UnsupportedOperationException();
431    }
432
433    @Override
434    public boolean quickReject(RectF rect, EdgeType type) {
435        return quickReject(rect.left, rect.top, rect.right, rect.bottom, type);
436    }
437
438    ///////////////////////////////////////////////////////////////////////////
439    // Transformations
440    ///////////////////////////////////////////////////////////////////////////
441
442    @Override
443    public void translate(float dx, float dy) {
444        if (dx != 0.0f || dy != 0.0f) nTranslate(mRenderer, dx, dy);
445    }
446
447    private static native void nTranslate(int renderer, float dx, float dy);
448
449    @Override
450    public void skew(float sx, float sy) {
451        nSkew(mRenderer, sx, sy);
452    }
453
454    private static native void nSkew(int renderer, float sx, float sy);
455
456    @Override
457    public void rotate(float degrees) {
458        nRotate(mRenderer, degrees);
459    }
460
461    private static native void nRotate(int renderer, float degrees);
462
463    @Override
464    public void scale(float sx, float sy) {
465        nScale(mRenderer, sx, sy);
466    }
467
468    private static native void nScale(int renderer, float sx, float sy);
469
470    @Override
471    public void setMatrix(Matrix matrix) {
472        nSetMatrix(mRenderer, matrix.native_instance);
473    }
474
475    private static native void nSetMatrix(int renderer, int matrix);
476
477    @Override
478    public int getNativeMatrix() {
479        return nGetMatrix(mRenderer);
480    }
481
482    private static native int nGetMatrix(int renderer);
483
484    @Override
485    public void getMatrix(Matrix matrix) {
486        nGetMatrix(mRenderer, matrix.native_instance);
487    }
488
489    private static native void nGetMatrix(int renderer, int matrix);
490
491    @Override
492    public void concat(Matrix matrix) {
493        nConcatMatrix(mRenderer, matrix.native_instance);
494    }
495
496    private static native void nConcatMatrix(int renderer, int matrix);
497
498    ///////////////////////////////////////////////////////////////////////////
499    // State management
500    ///////////////////////////////////////////////////////////////////////////
501
502    @Override
503    public int save() {
504        return nSave(mRenderer, Canvas.CLIP_SAVE_FLAG | Canvas.MATRIX_SAVE_FLAG);
505    }
506
507    @Override
508    public int save(int saveFlags) {
509        return nSave(mRenderer, saveFlags);
510    }
511
512    private static native int nSave(int renderer, int flags);
513
514    @Override
515    public int saveLayer(RectF bounds, Paint paint, int saveFlags) {
516        return saveLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, paint, saveFlags);
517    }
518
519    @Override
520    public int saveLayer(float left, float top, float right, float bottom, Paint paint,
521            int saveFlags) {
522        if (left < right && top < bottom) {
523            int modifier = paint != null ? setupColorFilter(paint) : MODIFIER_NONE;
524            final int nativePaint = paint == null ? 0 : paint.mNativePaint;
525            int count = nSaveLayer(mRenderer, left, top, right, bottom, nativePaint, saveFlags);
526            if (modifier != MODIFIER_NONE) nResetModifiers(mRenderer, modifier);
527            return count;
528        }
529        return save(saveFlags);
530    }
531
532    private static native int nSaveLayer(int renderer, float left, float top,
533            float right, float bottom, int paint, int saveFlags);
534
535    @Override
536    public int saveLayerAlpha(RectF bounds, int alpha, int saveFlags) {
537        return saveLayerAlpha(bounds.left, bounds.top, bounds.right, bounds.bottom,
538                alpha, saveFlags);
539    }
540
541    @Override
542    public int saveLayerAlpha(float left, float top, float right, float bottom, int alpha,
543            int saveFlags) {
544        if (left < right && top < bottom) {
545            return nSaveLayerAlpha(mRenderer, left, top, right, bottom, alpha, saveFlags);
546        }
547        return save(saveFlags);
548    }
549
550    private static native int nSaveLayerAlpha(int renderer, float left, float top, float right,
551            float bottom, int alpha, int saveFlags);
552
553    @Override
554    public void restore() {
555        nRestore(mRenderer);
556    }
557
558    private static native void nRestore(int renderer);
559
560    @Override
561    public void restoreToCount(int saveCount) {
562        nRestoreToCount(mRenderer, saveCount);
563    }
564
565    private static native void nRestoreToCount(int renderer, int saveCount);
566
567    @Override
568    public int getSaveCount() {
569        return nGetSaveCount(mRenderer);
570    }
571
572    private static native int nGetSaveCount(int renderer);
573
574    ///////////////////////////////////////////////////////////////////////////
575    // Filtering
576    ///////////////////////////////////////////////////////////////////////////
577
578    @Override
579    public void setDrawFilter(DrawFilter filter) {
580        mFilter = filter;
581    }
582
583    @Override
584    public DrawFilter getDrawFilter() {
585        return mFilter;
586    }
587
588    ///////////////////////////////////////////////////////////////////////////
589    // Drawing
590    ///////////////////////////////////////////////////////////////////////////
591
592    @Override
593    public void drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter,
594            Paint paint) {
595        int modifiers = setupModifiers(paint);
596        nDrawArc(mRenderer, oval.left, oval.top, oval.right, oval.bottom, startAngle, sweepAngle,
597                useCenter, paint.mNativePaint);
598        if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
599    }
600
601    private static native void nDrawArc(int renderer, float left, float top,
602            float right, float bottom, float startAngle, float sweepAngle,
603            boolean useCenter, int paint);
604
605    @Override
606    public void drawARGB(int a, int r, int g, int b) {
607        drawColor((a & 0xFF) << 24 | (r & 0xFF) << 16 | (g & 0xFF) << 8 | (b & 0xFF));
608    }
609
610    @Override
611    public void drawPatch(Bitmap bitmap, byte[] chunks, RectF dst, Paint paint) {
612        // Shaders are ignored when drawing patches
613        int modifier = paint != null ? setupColorFilter(paint) : MODIFIER_NONE;
614        final int nativePaint = paint == null ? 0 : paint.mNativePaint;
615        nDrawPatch(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, chunks,
616                dst.left, dst.top, dst.right, dst.bottom, nativePaint);
617        if (modifier != MODIFIER_NONE) nResetModifiers(mRenderer, modifier);
618    }
619
620    private static native void nDrawPatch(int renderer, int bitmap, byte[] buffer, byte[] chunks,
621            float left, float top, float right, float bottom, int paint);
622
623    @Override
624    public void drawBitmap(Bitmap bitmap, float left, float top, Paint paint) {
625        // Shaders are ignored when drawing bitmaps
626        int modifiers = paint != null ? setupModifiers(bitmap, paint) : MODIFIER_NONE;
627        final int nativePaint = paint == null ? 0 : paint.mNativePaint;
628        nDrawBitmap(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, left, top, nativePaint);
629        if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
630    }
631
632    private static native void nDrawBitmap(
633            int renderer, int bitmap, byte[] buffer, float left, float top, int paint);
634
635    @Override
636    public void drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint) {
637        // Shaders are ignored when drawing bitmaps
638        int modifiers = paint != null ? setupModifiers(bitmap, paint) : MODIFIER_NONE;
639        final int nativePaint = paint == null ? 0 : paint.mNativePaint;
640        nDrawBitmap(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer,
641                matrix.native_instance, nativePaint);
642        if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
643    }
644
645    private static native void nDrawBitmap(int renderer, int bitmap, byte[] buff,
646            int matrix, int paint);
647
648    @Override
649    public void drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint) {
650        // Shaders are ignored when drawing bitmaps
651        int modifiers = paint != null ? setupModifiers(bitmap, paint) : MODIFIER_NONE;
652        final int nativePaint = paint == null ? 0 : paint.mNativePaint;
653
654        int left, top, right, bottom;
655        if (src == null) {
656            left = top = 0;
657            right = bitmap.getWidth();
658            bottom = bitmap.getHeight();
659        } else {
660            left = src.left;
661            right = src.right;
662            top = src.top;
663            bottom = src.bottom;
664        }
665
666        nDrawBitmap(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, left, top, right, bottom,
667                dst.left, dst.top, dst.right, dst.bottom, nativePaint);
668        if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
669    }
670
671    @Override
672    public void drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint) {
673        // Shaders are ignored when drawing bitmaps
674        int modifiers = paint != null ? setupModifiers(bitmap, paint) : MODIFIER_NONE;
675        final int nativePaint = paint == null ? 0 : paint.mNativePaint;
676        nDrawBitmap(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, src.left, src.top, src.right,
677                src.bottom, dst.left, dst.top, dst.right, dst.bottom, nativePaint);
678        if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
679    }
680
681    private static native void nDrawBitmap(int renderer, int bitmap, byte[] buffer,
682            float srcLeft, float srcTop, float srcRight, float srcBottom,
683            float left, float top, float right, float bottom, int paint);
684
685    @Override
686    public void drawBitmap(int[] colors, int offset, int stride, float x, float y,
687            int width, int height, boolean hasAlpha, Paint paint) {
688        // Shaders are ignored when drawing bitmaps
689        int modifier = paint != null ? setupColorFilter(paint) : MODIFIER_NONE;
690        final Bitmap.Config config = hasAlpha ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
691        final Bitmap b = Bitmap.createBitmap(colors, offset, stride, width, height, config);
692        final int nativePaint = paint == null ? 0 : paint.mNativePaint;
693        nDrawBitmap(mRenderer, b.mNativeBitmap, b.mBuffer, x, y, nativePaint);
694        b.recycle();
695        if (modifier != MODIFIER_NONE) nResetModifiers(mRenderer, modifier);
696    }
697
698    @Override
699    public void drawBitmap(int[] colors, int offset, int stride, int x, int y,
700            int width, int height, boolean hasAlpha, Paint paint) {
701        // Shaders are ignored when drawing bitmaps
702        drawBitmap(colors, offset, stride, (float) x, (float) y, width, height, hasAlpha, paint);
703    }
704
705    @Override
706    public void drawBitmapMesh(Bitmap bitmap, int meshWidth, int meshHeight, float[] verts,
707            int vertOffset, int[] colors, int colorOffset, Paint paint) {
708        if (meshWidth < 0 || meshHeight < 0 || vertOffset < 0 || colorOffset < 0) {
709            throw new ArrayIndexOutOfBoundsException();
710        }
711
712        if (meshWidth == 0 || meshHeight == 0) {
713            return;
714        }
715
716        final int count = (meshWidth + 1) * (meshHeight + 1);
717        checkRange(verts.length, vertOffset, count * 2);
718
719        // TODO: Colors are ignored for now
720        colors = null;
721        colorOffset = 0;
722
723        int modifiers = paint != null ? setupModifiers(bitmap, paint) : MODIFIER_NONE;
724        final int nativePaint = paint == null ? 0 : paint.mNativePaint;
725        nDrawBitmapMesh(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, meshWidth, meshHeight,
726                verts, vertOffset, colors, colorOffset, nativePaint);
727        if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
728    }
729
730    private static native void nDrawBitmapMesh(int renderer, int bitmap, byte[] buffer,
731            int meshWidth, int meshHeight, float[] verts, int vertOffset,
732            int[] colors, int colorOffset, int paint);
733
734    @Override
735    public void drawCircle(float cx, float cy, float radius, Paint paint) {
736        int modifiers = setupModifiers(paint);
737        nDrawCircle(mRenderer, cx, cy, radius, paint.mNativePaint);
738        if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
739    }
740
741    private static native void nDrawCircle(int renderer, float cx, float cy,
742            float radius, int paint);
743
744    @Override
745    public void drawColor(int color) {
746        drawColor(color, PorterDuff.Mode.SRC_OVER);
747    }
748
749    @Override
750    public void drawColor(int color, PorterDuff.Mode mode) {
751        nDrawColor(mRenderer, color, mode.nativeInt);
752    }
753
754    private static native void nDrawColor(int renderer, int color, int mode);
755
756    @Override
757    public void drawLine(float startX, float startY, float stopX, float stopY, Paint paint) {
758        mLine[0] = startX;
759        mLine[1] = startY;
760        mLine[2] = stopX;
761        mLine[3] = stopY;
762        drawLines(mLine, 0, 4, paint);
763    }
764
765    @Override
766    public void drawLines(float[] pts, int offset, int count, Paint paint) {
767        if ((offset | count) < 0 || offset + count > pts.length) {
768            throw new IllegalArgumentException("The lines array must contain 4 elements per line.");
769        }
770        int modifiers = setupModifiers(paint);
771        nDrawLines(mRenderer, pts, offset, count, paint.mNativePaint);
772        if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
773    }
774
775    private static native void nDrawLines(int renderer, float[] points,
776            int offset, int count, int paint);
777
778    @Override
779    public void drawLines(float[] pts, Paint paint) {
780        drawLines(pts, 0, pts.length, paint);
781    }
782
783    @Override
784    public void drawOval(RectF oval, Paint paint) {
785        int modifiers = setupModifiers(paint);
786        nDrawOval(mRenderer, oval.left, oval.top, oval.right, oval.bottom, paint.mNativePaint);
787        if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
788    }
789
790    private static native void nDrawOval(int renderer, float left, float top,
791            float right, float bottom, int paint);
792
793    @Override
794    public void drawPaint(Paint paint) {
795        final Rect r = mClipBounds;
796        nGetClipBounds(mRenderer, r);
797        drawRect(r.left, r.top, r.right, r.bottom, paint);
798    }
799
800    @Override
801    public void drawPath(Path path, Paint paint) {
802        int modifiers = setupModifiers(paint);
803        if (path.isSimplePath) {
804            if (path.rects != null) {
805                nDrawRects(mRenderer, path.rects.mNativeRegion, paint.mNativePaint);
806            }
807        } else {
808            nDrawPath(mRenderer, path.mNativePath, paint.mNativePaint);
809        }
810        if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
811    }
812
813    private static native void nDrawPath(int renderer, int path, int paint);
814    private static native void nDrawRects(int renderer, int region, int paint);
815
816    @Override
817    public void drawPicture(Picture picture) {
818        throw new UnsupportedOperationException();
819    }
820
821    @Override
822    public void drawPicture(Picture picture, Rect dst) {
823        throw new UnsupportedOperationException();
824    }
825
826    @Override
827    public void drawPicture(Picture picture, RectF dst) {
828        throw new UnsupportedOperationException();
829    }
830
831    @Override
832    public void drawPoint(float x, float y, Paint paint) {
833        mPoint[0] = x;
834        mPoint[1] = y;
835        drawPoints(mPoint, 0, 2, paint);
836    }
837
838    @Override
839    public void drawPoints(float[] pts, Paint paint) {
840        drawPoints(pts, 0, pts.length, paint);
841    }
842
843    @Override
844    public void drawPoints(float[] pts, int offset, int count, Paint paint) {
845        int modifiers = setupModifiers(paint);
846        nDrawPoints(mRenderer, pts, offset, count, paint.mNativePaint);
847        if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
848    }
849
850    private static native void nDrawPoints(int renderer, float[] points,
851            int offset, int count, int paint);
852
853    @Override
854    public void drawPosText(char[] text, int index, int count, float[] pos, Paint paint) {
855        // TODO: Implement
856    }
857
858    @Override
859    public void drawPosText(String text, float[] pos, Paint paint) {
860        // TODO: Implement
861    }
862
863    @Override
864    public void drawRect(float left, float top, float right, float bottom, Paint paint) {
865        int modifiers = setupModifiers(paint);
866        nDrawRect(mRenderer, left, top, right, bottom, paint.mNativePaint);
867        if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
868    }
869
870    private static native void nDrawRect(int renderer, float left, float top,
871            float right, float bottom, int paint);
872
873    @Override
874    public void drawRect(Rect r, Paint paint) {
875        drawRect(r.left, r.top, r.right, r.bottom, paint);
876    }
877
878    @Override
879    public void drawRect(RectF r, Paint paint) {
880        drawRect(r.left, r.top, r.right, r.bottom, paint);
881    }
882
883    @Override
884    public void drawRGB(int r, int g, int b) {
885        drawColor(0xFF000000 | (r & 0xFF) << 16 | (g & 0xFF) << 8 | (b & 0xFF));
886    }
887
888    @Override
889    public void drawRoundRect(RectF rect, float rx, float ry, Paint paint) {
890        int modifiers = setupModifiers(paint);
891        nDrawRoundRect(mRenderer, rect.left, rect.top, rect.right, rect.bottom,
892                rx, ry, paint.mNativePaint);
893        if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
894    }
895
896    private static native void nDrawRoundRect(int renderer, float left, float top,
897            float right, float bottom, float rx, float y, int paint);
898
899    @Override
900    public void drawText(char[] text, int index, int count, float x, float y, Paint paint) {
901        if ((index | count | (index + count) | (text.length - index - count)) < 0) {
902            throw new IndexOutOfBoundsException();
903        }
904
905        int modifiers = setupModifiers(paint);
906        try {
907            nDrawText(mRenderer, text, index, count, x, y, paint.mBidiFlags, paint.mNativePaint);
908        } finally {
909            if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
910        }
911    }
912
913    private static native void nDrawText(int renderer, char[] text, int index, int count,
914            float x, float y, int bidiFlags, int paint);
915
916    @Override
917    public void drawText(CharSequence text, int start, int end, float x, float y, Paint paint) {
918        int modifiers = setupModifiers(paint);
919        try {
920            if (text instanceof String || text instanceof SpannedString ||
921                    text instanceof SpannableString) {
922                nDrawText(mRenderer, text.toString(), start, end, x, y, paint.mBidiFlags,
923                        paint.mNativePaint);
924            } else if (text instanceof GraphicsOperations) {
925                ((GraphicsOperations) text).drawText(this, start, end, x, y,
926                                                         paint);
927            } else {
928                char[] buf = TemporaryBuffer.obtain(end - start);
929                TextUtils.getChars(text, start, end, buf, 0);
930                nDrawText(mRenderer, buf, 0, end - start, x, y,
931                        paint.mBidiFlags, paint.mNativePaint);
932                TemporaryBuffer.recycle(buf);
933            }
934        } finally {
935            if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
936        }
937    }
938
939    @Override
940    public void drawText(String text, int start, int end, float x, float y, Paint paint) {
941        if ((start | end | (end - start) | (text.length() - end)) < 0) {
942            throw new IndexOutOfBoundsException();
943        }
944
945        int modifiers = setupModifiers(paint);
946        try {
947            nDrawText(mRenderer, text, start, end, x, y, paint.mBidiFlags, paint.mNativePaint);
948        } finally {
949            if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
950        }
951    }
952
953    private static native void nDrawText(int renderer, String text, int start, int end,
954            float x, float y, int bidiFlags, int paint);
955
956    @Override
957    public void drawText(String text, float x, float y, Paint paint) {
958        int modifiers = setupModifiers(paint);
959        try {
960            nDrawText(mRenderer, text, 0, text.length(), x, y, paint.mBidiFlags,
961                    paint.mNativePaint);
962        } finally {
963            if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
964        }
965    }
966
967    @Override
968    public void drawTextOnPath(char[] text, int index, int count, Path path, float hOffset,
969            float vOffset, Paint paint) {
970        // TODO: Implement
971    }
972
973    @Override
974    public void drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint) {
975        // TODO: Implement
976    }
977
978    @Override
979    public void drawTextRun(char[] text, int index, int count, int contextIndex, int contextCount,
980            float x, float y, int dir, Paint paint) {
981        if ((index | count | text.length - index - count) < 0) {
982            throw new IndexOutOfBoundsException();
983        }
984        if (dir != DIRECTION_LTR && dir != DIRECTION_RTL) {
985            throw new IllegalArgumentException("Unknown direction: " + dir);
986        }
987
988        int modifiers = setupModifiers(paint);
989        try {
990            nDrawTextRun(mRenderer, text, index, count, contextIndex, contextCount, x, y, dir,
991                    paint.mNativePaint);
992        } finally {
993            if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
994        }
995    }
996
997    private static native void nDrawTextRun(int renderer, char[] text, int index, int count,
998            int contextIndex, int contextCount, float x, float y, int dir, int nativePaint);
999
1000    @Override
1001    public void drawTextRun(CharSequence text, int start, int end, int contextStart, int contextEnd,
1002            float x, float y, int dir, Paint paint) {
1003        if ((start | end | end - start | text.length() - end) < 0) {
1004            throw new IndexOutOfBoundsException();
1005        }
1006
1007        int modifiers = setupModifiers(paint);
1008        try {
1009            int flags = dir == 0 ? 0 : 1;
1010            if (text instanceof String || text instanceof SpannedString ||
1011                    text instanceof SpannableString) {
1012                nDrawTextRun(mRenderer, text.toString(), start, end, contextStart,
1013                        contextEnd, x, y, flags, paint.mNativePaint);
1014            } else if (text instanceof GraphicsOperations) {
1015                ((GraphicsOperations) text).drawTextRun(this, start, end,
1016                        contextStart, contextEnd, x, y, flags, paint);
1017            } else {
1018                int contextLen = contextEnd - contextStart;
1019                int len = end - start;
1020                char[] buf = TemporaryBuffer.obtain(contextLen);
1021                TextUtils.getChars(text, contextStart, contextEnd, buf, 0);
1022                nDrawTextRun(mRenderer, buf, start - contextStart, len, 0, contextLen,
1023                        x, y, flags, paint.mNativePaint);
1024                TemporaryBuffer.recycle(buf);
1025            }
1026        } finally {
1027            if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
1028        }
1029    }
1030
1031    private static native void nDrawTextRun(int renderer, String text, int start, int end,
1032            int contextStart, int contextEnd, float x, float y, int flags, int nativePaint);
1033
1034    @Override
1035    public void drawVertices(VertexMode mode, int vertexCount, float[] verts, int vertOffset,
1036            float[] texs, int texOffset, int[] colors, int colorOffset, short[] indices,
1037            int indexOffset, int indexCount, Paint paint) {
1038        // TODO: Implement
1039    }
1040
1041    private int setupModifiers(Bitmap b, Paint paint) {
1042        if (b.getConfig() == Bitmap.Config.ALPHA_8) {
1043            return setupModifiers(paint);
1044        }
1045
1046        final ColorFilter filter = paint.getColorFilter();
1047        if (filter != null) {
1048            nSetupColorFilter(mRenderer, filter.nativeColorFilter);
1049            return MODIFIER_COLOR_FILTER;
1050        }
1051
1052        return MODIFIER_NONE;
1053    }
1054
1055    private int setupModifiers(Paint paint) {
1056        int modifiers = MODIFIER_NONE;
1057
1058        if (paint.hasShadow) {
1059            nSetupShadow(mRenderer, paint.shadowRadius, paint.shadowDx, paint.shadowDy,
1060                    paint.shadowColor);
1061            modifiers |= MODIFIER_SHADOW;
1062        }
1063
1064        final Shader shader = paint.getShader();
1065        if (shader != null) {
1066            nSetupShader(mRenderer, shader.native_shader);
1067            modifiers |= MODIFIER_SHADER;
1068        }
1069
1070        final ColorFilter filter = paint.getColorFilter();
1071        if (filter != null) {
1072            nSetupColorFilter(mRenderer, filter.nativeColorFilter);
1073            modifiers |= MODIFIER_COLOR_FILTER;
1074        }
1075
1076        return modifiers;
1077    }
1078
1079    private int setupColorFilter(Paint paint) {
1080        final ColorFilter filter = paint.getColorFilter();
1081        if (filter != null) {
1082            nSetupColorFilter(mRenderer, filter.nativeColorFilter);
1083            return MODIFIER_COLOR_FILTER;
1084        }
1085        return MODIFIER_NONE;
1086    }
1087
1088    private static native void nSetupShader(int renderer, int shader);
1089    private static native void nSetupColorFilter(int renderer, int colorFilter);
1090    private static native void nSetupShadow(int renderer, float radius,
1091            float dx, float dy, int color);
1092
1093    private static native void nResetModifiers(int renderer, int modifiers);
1094}
1095