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