GLES20Canvas.java revision f61970fc79e9c5cf340fa942597628242361864a
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    @Override
252    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    void onPostDraw() {
266        nFinish(mRenderer);
267    }
268
269    private static native void nFinish(int renderer);
270
271    @Override
272    public boolean callDrawGLFunction(int drawGLFunction) {
273        return nCallDrawGLFunction(mRenderer, drawGLFunction);
274    }
275
276    private static native boolean nCallDrawGLFunction(int renderer, int drawGLFunction);
277
278    ///////////////////////////////////////////////////////////////////////////
279    // Display list
280    ///////////////////////////////////////////////////////////////////////////
281
282    int getDisplayList() {
283        return nGetDisplayList(mRenderer);
284    }
285
286    private static native int nGetDisplayList(int renderer);
287
288    static void destroyDisplayList(int displayList) {
289        nDestroyDisplayList(displayList);
290    }
291
292    private static native void nDestroyDisplayList(int displayList);
293
294    @Override
295    public boolean drawDisplayList(DisplayList displayList, int width, int height, Rect dirty) {
296        return nDrawDisplayList(mRenderer,
297                ((GLES20DisplayList) displayList).mNativeDisplayList, width, height, dirty);
298    }
299
300    private static native boolean nDrawDisplayList(int renderer, int displayList,
301            int width, int height, Rect dirty);
302
303    @Override
304    void outputDisplayList(DisplayList displayList) {
305        nOutputDisplayList(mRenderer, ((GLES20DisplayList) displayList).mNativeDisplayList);
306    }
307
308    private static native void nOutputDisplayList(int renderer, int displayList);
309
310    ///////////////////////////////////////////////////////////////////////////
311    // Hardware layer
312    ///////////////////////////////////////////////////////////////////////////
313
314    void drawHardwareLayer(HardwareLayer layer, float x, float y, Paint paint) {
315        final GLES20Layer glLayer = (GLES20Layer) layer;
316        int modifier = paint != null ? setupColorFilter(paint) : MODIFIER_NONE;
317        final int nativePaint = paint == null ? 0 : paint.mNativePaint;
318        nDrawLayer(mRenderer, glLayer.getLayer(), x, y, nativePaint);
319        if (modifier != MODIFIER_NONE) nResetModifiers(mRenderer, modifier);
320    }
321
322    private static native void nDrawLayer(int renderer, int layer, float x, float y, int paint);
323
324    void interrupt() {
325        nInterrupt(mRenderer);
326    }
327
328    void resume() {
329        nResume(mRenderer);
330    }
331
332    private static native void nInterrupt(int renderer);
333    private static native void nResume(int renderer);
334
335    ///////////////////////////////////////////////////////////////////////////
336    // Clipping
337    ///////////////////////////////////////////////////////////////////////////
338
339    @Override
340    public boolean clipPath(Path path) {
341        throw new UnsupportedOperationException();
342    }
343
344    @Override
345    public boolean clipPath(Path path, Region.Op op) {
346        throw new UnsupportedOperationException();
347    }
348
349    @Override
350    public boolean clipRect(float left, float top, float right, float bottom) {
351        return nClipRect(mRenderer, left, top, right, bottom, Region.Op.INTERSECT.nativeInt);
352    }
353
354    private static native boolean nClipRect(int renderer, float left, float top,
355            float right, float bottom, int op);
356
357    @Override
358    public boolean clipRect(float left, float top, float right, float bottom, Region.Op op) {
359        return nClipRect(mRenderer, left, top, right, bottom, op.nativeInt);
360    }
361
362    @Override
363    public boolean clipRect(int left, int top, int right, int bottom) {
364        return nClipRect(mRenderer, left, top, right, bottom, Region.Op.INTERSECT.nativeInt);
365    }
366
367    private static native boolean nClipRect(int renderer, int left, int top, int right, int bottom,
368            int op);
369
370    @Override
371    public boolean clipRect(Rect rect) {
372        return nClipRect(mRenderer, rect.left, rect.top, rect.right, rect.bottom,
373                Region.Op.INTERSECT.nativeInt);
374    }
375
376    @Override
377    public boolean clipRect(Rect rect, Region.Op op) {
378        return nClipRect(mRenderer, rect.left, rect.top, rect.right, rect.bottom, op.nativeInt);
379    }
380
381    @Override
382    public boolean clipRect(RectF 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(RectF 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 clipRegion(Region region) {
394        throw new UnsupportedOperationException();
395    }
396
397    @Override
398    public boolean clipRegion(Region region, Region.Op op) {
399        throw new UnsupportedOperationException();
400    }
401
402    @Override
403    public boolean getClipBounds(Rect bounds) {
404        return nGetClipBounds(mRenderer, bounds);
405    }
406
407    private static native boolean nGetClipBounds(int renderer, Rect bounds);
408
409    @Override
410    public boolean quickReject(float left, float top, float right, float bottom, EdgeType type) {
411        return nQuickReject(mRenderer, left, top, right, bottom, type.nativeInt);
412    }
413
414    private static native boolean nQuickReject(int renderer, float left, float top,
415            float right, float bottom, int edge);
416
417    @Override
418    public boolean quickReject(Path path, EdgeType type) {
419        throw new UnsupportedOperationException();
420    }
421
422    @Override
423    public boolean quickReject(RectF rect, EdgeType type) {
424        return quickReject(rect.left, rect.top, rect.right, rect.bottom, type);
425    }
426
427    ///////////////////////////////////////////////////////////////////////////
428    // Transformations
429    ///////////////////////////////////////////////////////////////////////////
430
431    @Override
432    public void translate(float dx, float dy) {
433        if (dx != 0.0f || dy != 0.0f) nTranslate(mRenderer, dx, dy);
434    }
435
436    private static native void nTranslate(int renderer, float dx, float dy);
437
438    @Override
439    public void skew(float sx, float sy) {
440        nSkew(mRenderer, sx, sy);
441    }
442
443    private static native void nSkew(int renderer, float sx, float sy);
444
445    @Override
446    public void rotate(float degrees) {
447        nRotate(mRenderer, degrees);
448    }
449
450    private static native void nRotate(int renderer, float degrees);
451
452    @Override
453    public void scale(float sx, float sy) {
454        nScale(mRenderer, sx, sy);
455    }
456
457    private static native void nScale(int renderer, float sx, float sy);
458
459    @Override
460    public void setMatrix(Matrix matrix) {
461        nSetMatrix(mRenderer, matrix.native_instance);
462    }
463
464    private static native void nSetMatrix(int renderer, int matrix);
465
466    @Override
467    public int getNativeMatrix() {
468        return nGetMatrix(mRenderer);
469    }
470
471    private static native int nGetMatrix(int renderer);
472
473    @Override
474    public void getMatrix(Matrix matrix) {
475        nGetMatrix(mRenderer, matrix.native_instance);
476    }
477
478    private static native void nGetMatrix(int renderer, int matrix);
479
480    @Override
481    public void concat(Matrix matrix) {
482        nConcatMatrix(mRenderer, matrix.native_instance);
483    }
484
485    private static native void nConcatMatrix(int renderer, int matrix);
486
487    ///////////////////////////////////////////////////////////////////////////
488    // State management
489    ///////////////////////////////////////////////////////////////////////////
490
491    @Override
492    public int save() {
493        return nSave(mRenderer, Canvas.CLIP_SAVE_FLAG | Canvas.MATRIX_SAVE_FLAG);
494    }
495
496    @Override
497    public int save(int saveFlags) {
498        return nSave(mRenderer, saveFlags);
499    }
500
501    private static native int nSave(int renderer, int flags);
502
503    @Override
504    public int saveLayer(RectF bounds, Paint paint, int saveFlags) {
505        return saveLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, paint, saveFlags);
506    }
507
508    @Override
509    public int saveLayer(float left, float top, float right, float bottom, Paint paint,
510            int saveFlags) {
511        if (left < right && top < bottom) {
512            int modifier = paint != null ? setupColorFilter(paint) : MODIFIER_NONE;
513            final int nativePaint = paint == null ? 0 : paint.mNativePaint;
514            int count = nSaveLayer(mRenderer, left, top, right, bottom, nativePaint, saveFlags);
515            if (modifier != MODIFIER_NONE) nResetModifiers(mRenderer, modifier);
516            return count;
517        }
518        return save(saveFlags);
519    }
520
521    private static native int nSaveLayer(int renderer, float left, float top,
522            float right, float bottom, int paint, int saveFlags);
523
524    @Override
525    public int saveLayerAlpha(RectF bounds, int alpha, int saveFlags) {
526        return saveLayerAlpha(bounds.left, bounds.top, bounds.right, bounds.bottom,
527                alpha, saveFlags);
528    }
529
530    @Override
531    public int saveLayerAlpha(float left, float top, float right, float bottom, int alpha,
532            int saveFlags) {
533        if (left < right && top < bottom) {
534            return nSaveLayerAlpha(mRenderer, left, top, right, bottom, alpha, saveFlags);
535        }
536        return save(saveFlags);
537    }
538
539    private static native int nSaveLayerAlpha(int renderer, float left, float top, float right,
540            float bottom, int alpha, int saveFlags);
541
542    @Override
543    public void restore() {
544        nRestore(mRenderer);
545    }
546
547    private static native void nRestore(int renderer);
548
549    @Override
550    public void restoreToCount(int saveCount) {
551        nRestoreToCount(mRenderer, saveCount);
552    }
553
554    private static native void nRestoreToCount(int renderer, int saveCount);
555
556    @Override
557    public int getSaveCount() {
558        return nGetSaveCount(mRenderer);
559    }
560
561    private static native int nGetSaveCount(int renderer);
562
563    ///////////////////////////////////////////////////////////////////////////
564    // Filtering
565    ///////////////////////////////////////////////////////////////////////////
566
567    @Override
568    public void setDrawFilter(DrawFilter filter) {
569        mFilter = filter;
570    }
571
572    @Override
573    public DrawFilter getDrawFilter() {
574        return mFilter;
575    }
576
577    ///////////////////////////////////////////////////////////////////////////
578    // Drawing
579    ///////////////////////////////////////////////////////////////////////////
580
581    @Override
582    public void drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter,
583            Paint paint) {
584        int modifiers = setupModifiers(paint);
585        nDrawArc(mRenderer, oval.left, oval.top, oval.right, oval.bottom, startAngle, sweepAngle,
586                useCenter, paint.mNativePaint);
587        if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
588    }
589
590    private static native void nDrawArc(int renderer, float left, float top,
591            float right, float bottom, float startAngle, float sweepAngle,
592            boolean useCenter, int paint);
593
594    @Override
595    public void drawARGB(int a, int r, int g, int b) {
596        drawColor((a & 0xFF) << 24 | (r & 0xFF) << 16 | (g & 0xFF) << 8 | (b & 0xFF));
597    }
598
599    @Override
600    public void drawPatch(Bitmap bitmap, byte[] chunks, RectF dst, Paint paint) {
601        // Shaders are ignored when drawing patches
602        int modifier = paint != null ? setupColorFilter(paint) : MODIFIER_NONE;
603        final int nativePaint = paint == null ? 0 : paint.mNativePaint;
604        nDrawPatch(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, chunks,
605                dst.left, dst.top, dst.right, dst.bottom, nativePaint);
606        if (modifier != MODIFIER_NONE) nResetModifiers(mRenderer, modifier);
607    }
608
609    private static native void nDrawPatch(int renderer, int bitmap, byte[] buffer, byte[] chunks,
610            float left, float top, float right, float bottom, int paint);
611
612    @Override
613    public void drawBitmap(Bitmap bitmap, float left, float top, Paint paint) {
614        // Shaders are ignored when drawing bitmaps
615        int modifiers = paint != null ? setupModifiers(bitmap, paint) : MODIFIER_NONE;
616        final int nativePaint = paint == null ? 0 : paint.mNativePaint;
617        nDrawBitmap(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, left, top, nativePaint);
618        if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
619    }
620
621    private static native void nDrawBitmap(
622            int renderer, int bitmap, byte[] buffer, float left, float top, int paint);
623
624    @Override
625    public void drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint) {
626        // Shaders are ignored when drawing bitmaps
627        int modifiers = paint != null ? setupModifiers(bitmap, paint) : MODIFIER_NONE;
628        final int nativePaint = paint == null ? 0 : paint.mNativePaint;
629        nDrawBitmap(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer,
630                matrix.native_instance, nativePaint);
631        if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
632    }
633
634    private static native void nDrawBitmap(int renderer, int bitmap, byte[] buff,
635            int matrix, int paint);
636
637    @Override
638    public void drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint) {
639        // Shaders are ignored when drawing bitmaps
640        int modifiers = paint != null ? setupModifiers(bitmap, paint) : MODIFIER_NONE;
641        final int nativePaint = paint == null ? 0 : paint.mNativePaint;
642
643        int left, top, right, bottom;
644        if (src == null) {
645            left = top = 0;
646            right = bitmap.getWidth();
647            bottom = bitmap.getHeight();
648        } else {
649            left = src.left;
650            right = src.right;
651            top = src.top;
652            bottom = src.bottom;
653        }
654
655        nDrawBitmap(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, left, top, right, bottom,
656                dst.left, dst.top, dst.right, dst.bottom, nativePaint);
657        if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
658    }
659
660    @Override
661    public void drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint) {
662        // Shaders are ignored when drawing bitmaps
663        int modifiers = paint != null ? setupModifiers(bitmap, paint) : MODIFIER_NONE;
664        final int nativePaint = paint == null ? 0 : paint.mNativePaint;
665        nDrawBitmap(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, src.left, src.top, src.right,
666                src.bottom, dst.left, dst.top, dst.right, dst.bottom, nativePaint);
667        if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
668    }
669
670    private static native void nDrawBitmap(int renderer, int bitmap, byte[] buffer,
671            float srcLeft, float srcTop, float srcRight, float srcBottom,
672            float left, float top, float right, float bottom, int paint);
673
674    @Override
675    public void drawBitmap(int[] colors, int offset, int stride, float x, float y,
676            int width, int height, boolean hasAlpha, Paint paint) {
677        // Shaders are ignored when drawing bitmaps
678        int modifier = paint != null ? setupColorFilter(paint) : MODIFIER_NONE;
679        final Bitmap.Config config = hasAlpha ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
680        final Bitmap b = Bitmap.createBitmap(colors, offset, stride, width, height, config);
681        final int nativePaint = paint == null ? 0 : paint.mNativePaint;
682        nDrawBitmap(mRenderer, b.mNativeBitmap, b.mBuffer, x, y, nativePaint);
683        b.recycle();
684        if (modifier != MODIFIER_NONE) nResetModifiers(mRenderer, modifier);
685    }
686
687    @Override
688    public void drawBitmap(int[] colors, int offset, int stride, int x, int y,
689            int width, int height, boolean hasAlpha, Paint paint) {
690        // Shaders are ignored when drawing bitmaps
691        drawBitmap(colors, offset, stride, (float) x, (float) y, width, height, hasAlpha, paint);
692    }
693
694    @Override
695    public void drawBitmapMesh(Bitmap bitmap, int meshWidth, int meshHeight, float[] verts,
696            int vertOffset, int[] colors, int colorOffset, Paint paint) {
697        if (meshWidth < 0 || meshHeight < 0 || vertOffset < 0 || colorOffset < 0) {
698            throw new ArrayIndexOutOfBoundsException();
699        }
700
701        if (meshWidth == 0 || meshHeight == 0) {
702            return;
703        }
704
705        final int count = (meshWidth + 1) * (meshHeight + 1);
706        checkRange(verts.length, vertOffset, count * 2);
707
708        // TODO: Colors are ignored for now
709        colors = null;
710        colorOffset = 0;
711
712        int modifiers = paint != null ? setupModifiers(bitmap, paint) : MODIFIER_NONE;
713        final int nativePaint = paint == null ? 0 : paint.mNativePaint;
714        nDrawBitmapMesh(mRenderer, bitmap.mNativeBitmap, bitmap.mBuffer, meshWidth, meshHeight,
715                verts, vertOffset, colors, colorOffset, nativePaint);
716        if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
717    }
718
719    private static native void nDrawBitmapMesh(int renderer, int bitmap, byte[] buffer,
720            int meshWidth, int meshHeight, float[] verts, int vertOffset,
721            int[] colors, int colorOffset, int paint);
722
723    @Override
724    public void drawCircle(float cx, float cy, float radius, Paint paint) {
725        int modifiers = setupModifiers(paint);
726        nDrawCircle(mRenderer, cx, cy, radius, paint.mNativePaint);
727        if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
728    }
729
730    private static native void nDrawCircle(int renderer, float cx, float cy,
731            float radius, int paint);
732
733    @Override
734    public void drawColor(int color) {
735        drawColor(color, PorterDuff.Mode.SRC_OVER);
736    }
737
738    @Override
739    public void drawColor(int color, PorterDuff.Mode mode) {
740        nDrawColor(mRenderer, color, mode.nativeInt);
741    }
742
743    private static native void nDrawColor(int renderer, int color, int mode);
744
745    @Override
746    public void drawLine(float startX, float startY, float stopX, float stopY, Paint paint) {
747        mLine[0] = startX;
748        mLine[1] = startY;
749        mLine[2] = stopX;
750        mLine[3] = stopY;
751        drawLines(mLine, 0, 4, paint);
752    }
753
754    @Override
755    public void drawLines(float[] pts, int offset, int count, Paint paint) {
756        if ((offset | count) < 0 || offset + count > pts.length) {
757            throw new IllegalArgumentException("The lines array must contain 4 elements per line.");
758        }
759        int modifiers = setupModifiers(paint);
760        nDrawLines(mRenderer, pts, offset, count, paint.mNativePaint);
761        if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
762    }
763
764    private static native void nDrawLines(int renderer, float[] points,
765            int offset, int count, int paint);
766
767    @Override
768    public void drawLines(float[] pts, Paint paint) {
769        drawLines(pts, 0, pts.length, paint);
770    }
771
772    @Override
773    public void drawOval(RectF oval, Paint paint) {
774        int modifiers = setupModifiers(paint);
775        nDrawOval(mRenderer, oval.left, oval.top, oval.right, oval.bottom, paint.mNativePaint);
776        if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
777    }
778
779    private static native void nDrawOval(int renderer, float left, float top,
780            float right, float bottom, int paint);
781
782    @Override
783    public void drawPaint(Paint paint) {
784        final Rect r = mClipBounds;
785        nGetClipBounds(mRenderer, r);
786        drawRect(r.left, r.top, r.right, r.bottom, paint);
787    }
788
789    @Override
790    public void drawPath(Path path, Paint paint) {
791        int modifiers = setupModifiers(paint);
792        if (path.isSimplePath) {
793            if (path.rects != null) {
794                nDrawRects(mRenderer, path.rects.mNativeRegion, paint.mNativePaint);
795            }
796        } else {
797            nDrawPath(mRenderer, path.mNativePath, paint.mNativePaint);
798        }
799        if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
800    }
801
802    private static native void nDrawPath(int renderer, int path, int paint);
803    private static native void nDrawRects(int renderer, int region, int paint);
804
805    @Override
806    public void drawPicture(Picture picture) {
807        throw new UnsupportedOperationException();
808    }
809
810    @Override
811    public void drawPicture(Picture picture, Rect dst) {
812        throw new UnsupportedOperationException();
813    }
814
815    @Override
816    public void drawPicture(Picture picture, RectF dst) {
817        throw new UnsupportedOperationException();
818    }
819
820    @Override
821    public void drawPoint(float x, float y, Paint paint) {
822        mPoint[0] = x;
823        mPoint[1] = y;
824        drawPoints(mPoint, 0, 2, paint);
825    }
826
827    @Override
828    public void drawPoints(float[] pts, Paint paint) {
829        drawPoints(pts, 0, pts.length, paint);
830    }
831
832    @Override
833    public void drawPoints(float[] pts, int offset, int count, Paint paint) {
834        int modifiers = setupModifiers(paint);
835        nDrawPoints(mRenderer, pts, offset, count, paint.mNativePaint);
836        if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
837    }
838
839    private static native void nDrawPoints(int renderer, float[] points,
840            int offset, int count, int paint);
841
842    @Override
843    public void drawPosText(char[] text, int index, int count, float[] pos, Paint paint) {
844        // TODO: Implement
845    }
846
847    @Override
848    public void drawPosText(String text, float[] pos, Paint paint) {
849        // TODO: Implement
850    }
851
852    @Override
853    public void drawRect(float left, float top, float right, float bottom, Paint paint) {
854        int modifiers = setupModifiers(paint);
855        nDrawRect(mRenderer, left, top, right, bottom, paint.mNativePaint);
856        if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
857    }
858
859    private static native void nDrawRect(int renderer, float left, float top,
860            float right, float bottom, int paint);
861
862    @Override
863    public void drawRect(Rect r, Paint paint) {
864        drawRect(r.left, r.top, r.right, r.bottom, paint);
865    }
866
867    @Override
868    public void drawRect(RectF r, Paint paint) {
869        drawRect(r.left, r.top, r.right, r.bottom, paint);
870    }
871
872    @Override
873    public void drawRGB(int r, int g, int b) {
874        drawColor(0xFF000000 | (r & 0xFF) << 16 | (g & 0xFF) << 8 | (b & 0xFF));
875    }
876
877    @Override
878    public void drawRoundRect(RectF rect, float rx, float ry, Paint paint) {
879        int modifiers = setupModifiers(paint);
880        nDrawRoundRect(mRenderer, rect.left, rect.top, rect.right, rect.bottom,
881                rx, ry, paint.mNativePaint);
882        if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
883    }
884
885    private static native void nDrawRoundRect(int renderer, float left, float top,
886            float right, float bottom, float rx, float y, int paint);
887
888    @Override
889    public void drawText(char[] text, int index, int count, float x, float y, Paint paint) {
890        if ((index | count | (index + count) | (text.length - index - count)) < 0) {
891            throw new IndexOutOfBoundsException();
892        }
893
894        int modifiers = setupModifiers(paint);
895        try {
896            nDrawText(mRenderer, text, index, count, x, y, paint.mBidiFlags, paint.mNativePaint);
897        } finally {
898            if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
899        }
900    }
901
902    private static native void nDrawText(int renderer, char[] text, int index, int count,
903            float x, float y, int bidiFlags, int paint);
904
905    @Override
906    public void drawText(CharSequence text, int start, int end, float x, float y, Paint paint) {
907        int modifiers = setupModifiers(paint);
908        try {
909            if (text instanceof String || text instanceof SpannedString ||
910                    text instanceof SpannableString) {
911                nDrawText(mRenderer, text.toString(), start, end, x, y, paint.mBidiFlags,
912                        paint.mNativePaint);
913            } else if (text instanceof GraphicsOperations) {
914                ((GraphicsOperations) text).drawText(this, start, end, x, y,
915                                                         paint);
916            } else {
917                char[] buf = TemporaryBuffer.obtain(end - start);
918                TextUtils.getChars(text, start, end, buf, 0);
919                nDrawText(mRenderer, buf, 0, end - start, x, y,
920                        paint.mBidiFlags, paint.mNativePaint);
921                TemporaryBuffer.recycle(buf);
922            }
923        } finally {
924            if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
925        }
926    }
927
928    @Override
929    public void drawText(String text, int start, int end, float x, float y, Paint paint) {
930        if ((start | end | (end - start) | (text.length() - end)) < 0) {
931            throw new IndexOutOfBoundsException();
932        }
933
934        int modifiers = setupModifiers(paint);
935        try {
936            nDrawText(mRenderer, text, start, end, x, y, paint.mBidiFlags, paint.mNativePaint);
937        } finally {
938            if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
939        }
940    }
941
942    private static native void nDrawText(int renderer, String text, int start, int end,
943            float x, float y, int bidiFlags, int paint);
944
945    @Override
946    public void drawText(String text, float x, float y, Paint paint) {
947        int modifiers = setupModifiers(paint);
948        try {
949            nDrawText(mRenderer, text, 0, text.length(), x, y, paint.mBidiFlags,
950                    paint.mNativePaint);
951        } finally {
952            if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
953        }
954    }
955
956    @Override
957    public void drawTextOnPath(char[] text, int index, int count, Path path, float hOffset,
958            float vOffset, Paint paint) {
959        // TODO: Implement
960    }
961
962    @Override
963    public void drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint) {
964        // TODO: Implement
965    }
966
967    @Override
968    public void drawTextRun(char[] text, int index, int count, int contextIndex, int contextCount,
969            float x, float y, int dir, Paint paint) {
970        if ((index | count | text.length - index - count) < 0) {
971            throw new IndexOutOfBoundsException();
972        }
973        if (dir != DIRECTION_LTR && dir != DIRECTION_RTL) {
974            throw new IllegalArgumentException("Unknown direction: " + dir);
975        }
976
977        int modifiers = setupModifiers(paint);
978        try {
979            nDrawTextRun(mRenderer, text, index, count, contextIndex, contextCount, x, y, dir,
980                    paint.mNativePaint);
981        } finally {
982            if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
983        }
984    }
985
986    private static native void nDrawTextRun(int renderer, char[] text, int index, int count,
987            int contextIndex, int contextCount, float x, float y, int dir, int nativePaint);
988
989    @Override
990    public void drawTextRun(CharSequence text, int start, int end, int contextStart, int contextEnd,
991            float x, float y, int dir, Paint paint) {
992        if ((start | end | end - start | text.length() - end) < 0) {
993            throw new IndexOutOfBoundsException();
994        }
995
996        int modifiers = setupModifiers(paint);
997        try {
998            int flags = dir == 0 ? 0 : 1;
999            if (text instanceof String || text instanceof SpannedString ||
1000                    text instanceof SpannableString) {
1001                nDrawTextRun(mRenderer, text.toString(), start, end, contextStart,
1002                        contextEnd, x, y, flags, paint.mNativePaint);
1003            } else if (text instanceof GraphicsOperations) {
1004                ((GraphicsOperations) text).drawTextRun(this, start, end,
1005                        contextStart, contextEnd, x, y, flags, paint);
1006            } else {
1007                int contextLen = contextEnd - contextStart;
1008                int len = end - start;
1009                char[] buf = TemporaryBuffer.obtain(contextLen);
1010                TextUtils.getChars(text, contextStart, contextEnd, buf, 0);
1011                nDrawTextRun(mRenderer, buf, start - contextStart, len, 0, contextLen,
1012                        x, y, flags, paint.mNativePaint);
1013                TemporaryBuffer.recycle(buf);
1014            }
1015        } finally {
1016            if (modifiers != MODIFIER_NONE) nResetModifiers(mRenderer, modifiers);
1017        }
1018    }
1019
1020    private static native void nDrawTextRun(int renderer, String text, int start, int end,
1021            int contextStart, int contextEnd, float x, float y, int flags, int nativePaint);
1022
1023    @Override
1024    public void drawVertices(VertexMode mode, int vertexCount, float[] verts, int vertOffset,
1025            float[] texs, int texOffset, int[] colors, int colorOffset, short[] indices,
1026            int indexOffset, int indexCount, Paint paint) {
1027        // TODO: Implement
1028    }
1029
1030    private int setupModifiers(Bitmap b, Paint paint) {
1031        if (b.getConfig() == Bitmap.Config.ALPHA_8) {
1032            return setupModifiers(paint);
1033        }
1034
1035        final ColorFilter filter = paint.getColorFilter();
1036        if (filter != null) {
1037            nSetupColorFilter(mRenderer, filter.nativeColorFilter);
1038            return MODIFIER_COLOR_FILTER;
1039        }
1040
1041        return MODIFIER_NONE;
1042    }
1043
1044    private int setupModifiers(Paint paint) {
1045        int modifiers = MODIFIER_NONE;
1046
1047        if (paint.hasShadow) {
1048            nSetupShadow(mRenderer, paint.shadowRadius, paint.shadowDx, paint.shadowDy,
1049                    paint.shadowColor);
1050            modifiers |= MODIFIER_SHADOW;
1051        }
1052
1053        final Shader shader = paint.getShader();
1054        if (shader != null) {
1055            nSetupShader(mRenderer, shader.native_shader);
1056            modifiers |= MODIFIER_SHADER;
1057        }
1058
1059        final ColorFilter filter = paint.getColorFilter();
1060        if (filter != null) {
1061            nSetupColorFilter(mRenderer, filter.nativeColorFilter);
1062            modifiers |= MODIFIER_COLOR_FILTER;
1063        }
1064
1065        return modifiers;
1066    }
1067
1068    private int setupColorFilter(Paint paint) {
1069        final ColorFilter filter = paint.getColorFilter();
1070        if (filter != null) {
1071            nSetupColorFilter(mRenderer, filter.nativeColorFilter);
1072            return MODIFIER_COLOR_FILTER;
1073        }
1074        return MODIFIER_NONE;
1075    }
1076
1077    private static native void nSetupShader(int renderer, int shader);
1078    private static native void nSetupColorFilter(int renderer, int colorFilter);
1079    private static native void nSetupShadow(int renderer, float radius,
1080            float dx, float dy, int color);
1081
1082    private static native void nResetModifiers(int renderer, int modifiers);
1083}
1084