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