GLES20Canvas.java revision 6926c72e25b8dec3dd4b84af0819fa1937ae7296
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.DrawFilter;
22import android.graphics.Matrix;
23import android.graphics.Paint;
24import android.graphics.Path;
25import android.graphics.Picture;
26import android.graphics.PorterDuff;
27import android.graphics.Rect;
28import android.graphics.RectF;
29import android.graphics.Region;
30
31import javax.microedition.khronos.opengles.GL;
32
33/**
34 * An implementation of Canvas on top of OpenGL ES 2.0.
35 */
36@SuppressWarnings({"deprecation"})
37class GLES20Canvas extends Canvas {
38    @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"})
39    private final GL mGl;
40    private final boolean mOpaque;
41    private final int mRenderer;
42
43    private int mWidth;
44    private int mHeight;
45
46    private final float[] mPoint = new float[2];
47    private final float[] mLine = new float[4];
48
49    private final Rect mClipBounds = new Rect();
50
51    private DrawFilter mFilter;
52
53    ///////////////////////////////////////////////////////////////////////////
54    // Constructors
55    ///////////////////////////////////////////////////////////////////////////
56
57    GLES20Canvas(GL gl, boolean translucent) {
58        mGl = gl;
59        mOpaque = !translucent;
60
61        mRenderer = nCreateRenderer();
62    }
63
64    private native int nCreateRenderer();
65
66    @Override
67    protected void finalize() throws Throwable {
68        try {
69            super.finalize();
70        } finally {
71            nDestroyRenderer(mRenderer);
72        }
73    }
74
75    private native void nDestroyRenderer(int renderer);
76
77    ///////////////////////////////////////////////////////////////////////////
78    // Canvas management
79    ///////////////////////////////////////////////////////////////////////////
80
81    @Override
82    public boolean isHardwareAccelerated() {
83        return true;
84    }
85
86    @Override
87    public GL getGL() {
88        throw new UnsupportedOperationException();
89    }
90
91    @Override
92    public void setBitmap(Bitmap bitmap) {
93        throw new UnsupportedOperationException();
94    }
95
96    @Override
97    public boolean isOpaque() {
98        return mOpaque;
99    }
100
101    @Override
102    public int getWidth() {
103        return mWidth;
104    }
105
106    @Override
107    public int getHeight() {
108        return mHeight;
109    }
110
111    ///////////////////////////////////////////////////////////////////////////
112    // Setup
113    ///////////////////////////////////////////////////////////////////////////
114
115    @Override
116    public void setViewport(int width, int height) {
117        mWidth = width;
118        mHeight = height;
119
120        nSetViewport(mRenderer, width, height);
121    }
122
123    private native void nSetViewport(int renderer, int width, int height);
124
125    void onPreDraw() {
126        nPrepare(mRenderer);
127    }
128
129    private native void nPrepare(int renderer);
130
131    ///////////////////////////////////////////////////////////////////////////
132    // Clipping
133    ///////////////////////////////////////////////////////////////////////////
134
135    @Override
136    public boolean clipPath(Path path) {
137        throw new UnsupportedOperationException();
138    }
139
140    @Override
141    public boolean clipPath(Path path, Region.Op op) {
142        throw new UnsupportedOperationException();
143    }
144
145    @Override
146    public boolean clipRect(float left, float top, float right, float bottom) {
147        return nClipRect(mRenderer, left, top, right, bottom);
148    }
149
150    private native boolean nClipRect(int renderer, float left, float top, float right, float bottom);
151
152    @Override
153    public boolean clipRect(float left, float top, float right, float bottom, Region.Op op) {
154        throw new UnsupportedOperationException();
155    }
156
157    @Override
158    public boolean clipRect(int left, int top, int right, int bottom) {
159        return nClipRect(mRenderer, left, top, right, bottom);
160    }
161
162    private native boolean nClipRect(int renderer, int left, int top, int right, int bottom);
163
164    @Override
165    public boolean clipRect(Rect rect) {
166        return clipRect(rect.left, rect.top, rect.right, rect.bottom);
167    }
168
169    @Override
170    public boolean clipRect(Rect rect, Region.Op op) {
171        // TODO: Implement
172        throw new UnsupportedOperationException();
173    }
174
175    @Override
176    public boolean clipRect(RectF rect) {
177        return clipRect(rect.left, rect.top, rect.right, rect.bottom);
178    }
179
180    @Override
181    public boolean clipRect(RectF rect, Region.Op op) {
182        // TODO: Implement
183        throw new UnsupportedOperationException();
184    }
185
186    @Override
187    public boolean clipRegion(Region region) {
188        throw new UnsupportedOperationException();
189    }
190
191    @Override
192    public boolean clipRegion(Region region, Region.Op op) {
193        throw new UnsupportedOperationException();
194    }
195
196    @Override
197    public boolean getClipBounds(Rect bounds) {
198        return nGetClipBounds(mRenderer, bounds);
199    }
200
201    private native boolean nGetClipBounds(int renderer, Rect bounds);
202
203    @Override
204    public boolean quickReject(float left, float top, float right, float bottom, EdgeType type) {
205        return nQuickReject(mRenderer, left, top, right, bottom, type.nativeInt);
206    }
207
208    private native boolean nQuickReject(int renderer, float left, float top,
209            float right, float bottom, int edge);
210
211    @Override
212    public boolean quickReject(Path path, EdgeType type) {
213        throw new UnsupportedOperationException();
214    }
215
216    @Override
217    public boolean quickReject(RectF rect, EdgeType type) {
218        return quickReject(rect.left, rect.top, rect.right, rect.bottom, type);
219    }
220
221    ///////////////////////////////////////////////////////////////////////////
222    // Transformations
223    ///////////////////////////////////////////////////////////////////////////
224
225    @Override
226    public void translate(float dx, float dy) {
227        nTranslate(mRenderer, dx, dy);
228    }
229
230    private native void nTranslate(int renderer, float dx, float dy);
231
232    @Override
233    public void skew(float sx, float sy) {
234        throw new UnsupportedOperationException();
235    }
236
237    @Override
238    public void rotate(float degrees) {
239        nRotate(mRenderer, degrees);
240    }
241
242    private native void nRotate(int renderer, float degrees);
243
244    @Override
245    public void scale(float sx, float sy) {
246        nScale(mRenderer, sx, sy);
247    }
248
249    private native void nScale(int renderer, float sx, float sy);
250
251    @Override
252    public void setMatrix(Matrix matrix) {
253        nSetMatrix(mRenderer, matrix.native_instance);
254    }
255
256    private native void nSetMatrix(int renderer, int matrix);
257
258    @Override
259    public void getMatrix(Matrix matrix) {
260        nGetMatrix(mRenderer, matrix.native_instance);
261    }
262
263    private native void nGetMatrix(int renderer, int matrix);
264
265    @Override
266    public void concat(Matrix matrix) {
267        nConcatMatrix(mRenderer, matrix.native_instance);
268    }
269
270    private native void nConcatMatrix(int renderer, int matrix);
271
272    ///////////////////////////////////////////////////////////////////////////
273    // State management
274    ///////////////////////////////////////////////////////////////////////////
275
276    @Override
277    public int save() {
278        return nSave(mRenderer, 0);
279    }
280
281    @Override
282    public int save(int saveFlags) {
283        return nSave(mRenderer, saveFlags);
284    }
285
286    private native int nSave(int renderer, int flags);
287
288    @Override
289    public int saveLayer(RectF bounds, Paint paint, int saveFlags) {
290        return saveLayer(bounds.left, bounds.top, bounds.right, bounds.bottom, paint, saveFlags);
291    }
292
293    @Override
294    public int saveLayer(float left, float top, float right, float bottom, Paint paint,
295            int saveFlags) {
296        int nativePaint = paint == null ? 0 : paint.mNativePaint;
297        return nSaveLayer(mRenderer, left, top, right, bottom, nativePaint, saveFlags);
298    }
299
300    private native int nSaveLayer(int renderer, float left, float top, float right, float bottom,
301            int paint, int saveFlags);
302
303    @Override
304    public int saveLayerAlpha(RectF bounds, int alpha, int saveFlags) {
305        return saveLayerAlpha(bounds.left, bounds.top, bounds.right, bounds.bottom,
306                alpha, saveFlags);
307    }
308
309    @Override
310    public int saveLayerAlpha(float left, float top, float right, float bottom, int alpha,
311            int saveFlags) {
312        return nSaveLayerAlpha(mRenderer, left, top, right, bottom, alpha, saveFlags);
313    }
314
315    private native int nSaveLayerAlpha(int renderer, float left, float top, float right,
316            float bottom, int alpha, int saveFlags);
317
318    @Override
319    public void restore() {
320        nRestore(mRenderer);
321    }
322
323    private native void nRestore(int renderer);
324
325    @Override
326    public void restoreToCount(int saveCount) {
327        nRestoreToCount(mRenderer, saveCount);
328    }
329
330    private native void nRestoreToCount(int renderer, int saveCount);
331
332    @Override
333    public int getSaveCount() {
334        return nGetSaveCount(mRenderer);
335    }
336
337    private native int nGetSaveCount(int renderer);
338
339    ///////////////////////////////////////////////////////////////////////////
340    // Filtering
341    ///////////////////////////////////////////////////////////////////////////
342
343    @Override
344    public void setDrawFilter(DrawFilter filter) {
345        // Don't crash, but ignore the draw filter
346        // TODO: Implement PaintDrawFilter
347        mFilter = filter;
348    }
349
350    @Override
351    public DrawFilter getDrawFilter() {
352        return mFilter;
353    }
354
355    ///////////////////////////////////////////////////////////////////////////
356    // Drawing
357    ///////////////////////////////////////////////////////////////////////////
358
359    @Override
360    public void drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter,
361            Paint paint) {
362        throw new UnsupportedOperationException();
363    }
364
365    @Override
366    public void drawARGB(int a, int r, int g, int b) {
367        drawColor((a & 0xFF) << 24 | (r & 0xFF) << 16 | (g & 0xFF) << 8 | (b & 0xFF));
368    }
369
370    @Override
371    public void drawPatch(Bitmap bitmap, byte[] chunks, RectF dst, Paint paint) {
372        final int nativePaint = paint == null ? 0 : paint.mNativePaint;
373        nDrawPatch(mRenderer, bitmap.mNativeBitmap, chunks, dst.left, dst.top,
374                dst.right, dst.bottom, nativePaint);
375    }
376
377    private native void nDrawPatch(int renderer, int bitmap, byte[] chunks, float left, float top,
378            float right, float bottom, int paint);
379
380    @Override
381    public void drawBitmap(Bitmap bitmap, float left, float top, Paint paint) {
382        final int nativePaint = paint == null ? 0 : paint.mNativePaint;
383        nDrawBitmap(mRenderer, bitmap.mNativeBitmap, left, top, nativePaint);
384    }
385
386    private native void nDrawBitmap(int renderer, int bitmap, float left, float top, int paint);
387
388    @Override
389    public void drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint) {
390        final int nativePaint = paint == null ? 0 : paint.mNativePaint;
391        nDrawBitmap(mRenderer, bitmap.mNativeBitmap, matrix.native_instance, nativePaint);
392    }
393
394    private native void nDrawBitmap(int renderer, int bitmap, int matrix, int paint);
395
396    @Override
397    public void drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint) {
398        final int nativePaint = paint == null ? 0 : paint.mNativePaint;
399        nDrawBitmap(mRenderer, bitmap.mNativeBitmap, src.left, src.top, src.right, src.bottom,
400                dst.left, dst.top, dst.right, dst.bottom, nativePaint
401        );
402    }
403
404    @Override
405    public void drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint) {
406        final int nativePaint = paint == null ? 0 : paint.mNativePaint;
407        nDrawBitmap(mRenderer, bitmap.mNativeBitmap, src.left, src.top, src.right, src.bottom,
408                dst.left, dst.top, dst.right, dst.bottom, nativePaint
409        );
410    }
411
412    private native void nDrawBitmap(int renderer, int bitmap,
413            float srcLeft, float srcTop, float srcRight, float srcBottom,
414            float left, float top, float right, float bottom, int paint);
415
416    @Override
417    public void drawBitmap(int[] colors, int offset, int stride, float x, float y,
418            int width, int height, boolean hasAlpha, Paint paint) {
419        final Bitmap.Config config = hasAlpha ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
420        final Bitmap b = Bitmap.createBitmap(colors, offset, stride, width, height, config);
421        final int nativePaint = paint == null ? 0 : paint.mNativePaint;
422        nDrawBitmap(mRenderer, b.mNativeBitmap, x, y, nativePaint);
423        b.recycle();
424    }
425
426    @Override
427    public void drawBitmap(int[] colors, int offset, int stride, int x, int y,
428            int width, int height, boolean hasAlpha, Paint paint) {
429        drawBitmap(colors, offset, stride, (float) x, (float) y, width, height, hasAlpha, paint);
430    }
431
432    @Override
433    public void drawBitmapMesh(Bitmap bitmap, int meshWidth, int meshHeight, float[] verts,
434            int vertOffset, int[] colors, int colorOffset, Paint paint) {
435        // TODO: Implement
436    }
437
438    @Override
439    public void drawCircle(float cx, float cy, float radius, Paint paint) {
440        throw new UnsupportedOperationException();
441    }
442
443    @Override
444    public void drawColor(int color) {
445        drawColor(color, PorterDuff.Mode.SRC_OVER);
446    }
447
448    @Override
449    public void drawColor(int color, PorterDuff.Mode mode) {
450        nDrawColor(mRenderer, color, mode.nativeInt);
451    }
452
453    private native void nDrawColor(int renderer, int color, int mode);
454
455    @Override
456    public void drawLine(float startX, float startY, float stopX, float stopY, Paint paint) {
457        mLine[0] = startX;
458        mLine[1] = startY;
459        mLine[2] = stopX;
460        mLine[3] = stopY;
461        drawLines(mLine, 0, 1, paint);
462    }
463
464    @Override
465    public void drawLines(float[] pts, int offset, int count, Paint paint) {
466        // TODO: Implement
467    }
468
469    @Override
470    public void drawLines(float[] pts, Paint paint) {
471        drawLines(pts, 0, pts.length / 4, paint);
472    }
473
474    @Override
475    public void drawOval(RectF oval, Paint paint) {
476        throw new UnsupportedOperationException();
477    }
478
479    @Override
480    public void drawPaint(Paint paint) {
481        final Rect r = mClipBounds;
482        nGetClipBounds(mRenderer, r);
483        drawRect(r.left, r.top, r.right, r.bottom, paint);
484    }
485
486    @Override
487    public void drawPath(Path path, Paint paint) {
488        throw new UnsupportedOperationException();
489    }
490
491    @Override
492    public void drawPicture(Picture picture) {
493        throw new UnsupportedOperationException();
494    }
495
496    @Override
497    public void drawPicture(Picture picture, Rect dst) {
498        throw new UnsupportedOperationException();
499    }
500
501    @Override
502    public void drawPicture(Picture picture, RectF dst) {
503        throw new UnsupportedOperationException();
504    }
505
506    @Override
507    public void drawPoint(float x, float y, Paint paint) {
508        mPoint[0] = x;
509        mPoint[1] = y;
510        drawPoints(mPoint, 0, 1, paint);
511    }
512
513    @Override
514    public void drawPoints(float[] pts, int offset, int count, Paint paint) {
515        // TODO: Implement
516    }
517
518    @Override
519    public void drawPoints(float[] pts, Paint paint) {
520        drawPoints(pts, 0, pts.length / 2, paint);
521    }
522
523    @Override
524    public void drawPosText(char[] text, int index, int count, float[] pos, Paint paint) {
525        throw new UnsupportedOperationException();
526    }
527
528    @Override
529    public void drawPosText(String text, float[] pos, Paint paint) {
530        throw new UnsupportedOperationException();
531    }
532
533    @Override
534    public void drawRect(float left, float top, float right, float bottom, Paint paint) {
535        nDrawRect(mRenderer, left, top, right, bottom, paint.mNativePaint);
536    }
537
538    private native void nDrawRect(int renderer, float left, float top, float right, float bottom,
539            int paint);
540
541    @Override
542    public void drawRect(Rect r, Paint paint) {
543        drawRect(r.left, r.top, r.right, r.bottom, paint);
544    }
545
546    @Override
547    public void drawRect(RectF r, Paint paint) {
548        drawRect(r.left, r.top, r.right, r.bottom, paint);
549    }
550
551    @Override
552    public void drawRGB(int r, int g, int b) {
553        drawColor(0xFF000000 | (r & 0xFF) << 16 | (g & 0xFF) << 8 | (b & 0xFF));
554    }
555
556    @Override
557    public void drawRoundRect(RectF rect, float rx, float ry, Paint paint) {
558        throw new UnsupportedOperationException();
559    }
560
561    @Override
562    public void drawText(char[] text, int index, int count, float x, float y, Paint paint) {
563        // TODO: Implement
564    }
565
566    @Override
567    public void drawText(CharSequence text, int start, int end, float x, float y, Paint paint) {
568        // TODO: Implement
569    }
570
571    @Override
572    public void drawText(String text, int start, int end, float x, float y, Paint paint) {
573        // TODO: Implement
574    }
575
576    @Override
577    public void drawText(String text, float x, float y, Paint paint) {
578        drawText(text, 0, text.length(), x, y, paint);
579    }
580
581    @Override
582    public void drawTextOnPath(char[] text, int index, int count, Path path, float hOffset,
583            float vOffset, Paint paint) {
584        throw new UnsupportedOperationException();
585    }
586
587    @Override
588    public void drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint) {
589        throw new UnsupportedOperationException();
590    }
591
592    @Override
593    public void drawTextRun(char[] text, int index, int count, int contextIndex, int contextCount,
594            float x, float y, int dir, Paint paint) {
595        throw new UnsupportedOperationException();
596    }
597
598    @Override
599    public void drawTextRun(CharSequence text, int start, int end, int contextStart, int contextEnd,
600            float x, float y, int dir, Paint paint) {
601        throw new UnsupportedOperationException();
602    }
603
604    @Override
605    public void drawVertices(VertexMode mode, int vertexCount, float[] verts, int vertOffset,
606            float[] texs, int texOffset, int[] colors, int colorOffset, short[] indices,
607            int indexOffset, int indexCount, Paint paint) {
608        // TODO: Implement
609    }
610}
611