GLES20RecordingCanvas.java revision 9e90a9953b65ae575ec8db3989857e0c145724b1
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.BitmapShader;
21import android.graphics.Matrix;
22import android.graphics.Paint;
23import android.graphics.Path;
24import android.graphics.Rect;
25import android.graphics.RectF;
26import android.graphics.Shader;
27
28import java.util.HashSet;
29
30/**
31 * An implementation of a GL canvas that records drawing operations.
32 * This is intended for use with a DisplayList. This class keeps a list of all the Paint and
33 * Bitmap objects that it draws, preventing the backing memory of Bitmaps from being freed while
34 * the DisplayList is still holding a native reference to the memory.
35 */
36class GLES20RecordingCanvas extends GLES20Canvas {
37    // These lists ensure that any Bitmaps recorded by a DisplayList are kept alive as long
38    // as the DisplayList is alive.
39    private HashSet<Bitmap> mBitmaps = new HashSet<Bitmap>();
40
41    GLES20RecordingCanvas(boolean translucent) {
42        super(true, translucent);
43    }
44
45    private void recordShaderBitmap(Paint paint) {
46        if (paint != null) {
47            final Shader shader = paint.getShader();
48            if (shader instanceof BitmapShader) {
49                mBitmaps.add(((BitmapShader) shader).mBitmap);
50            }
51        }
52    }
53
54    void reset() {
55        mBitmaps.clear();
56        setupRenderer(true);
57    }
58
59    @Override
60    public void drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter,
61            Paint paint) {
62        super.drawArc(oval, startAngle, sweepAngle, useCenter, paint);
63    }
64
65    @Override
66    public void drawPatch(Bitmap bitmap, byte[] chunks, RectF dst, Paint paint) {
67        super.drawPatch(bitmap, chunks, dst, paint);
68        mBitmaps.add(bitmap);
69        // Shaders in the Paint are ignored when drawing a Bitmap
70    }
71
72    @Override
73    public void drawBitmap(Bitmap bitmap, float left, float top, Paint paint) {
74        super.drawBitmap(bitmap, left, top, paint);
75        mBitmaps.add(bitmap);
76        // Shaders in the Paint are ignored when drawing a Bitmap
77    }
78
79    @Override
80    public void drawBitmap(Bitmap bitmap, Matrix matrix, Paint paint) {
81        super.drawBitmap(bitmap, matrix, paint);
82        mBitmaps.add(bitmap);
83        // Shaders in the Paint are ignored when drawing a Bitmap
84    }
85
86    @Override
87    public void drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint) {
88        super.drawBitmap(bitmap, src, dst, paint);
89        mBitmaps.add(bitmap);
90        // Shaders in the Paint are ignored when drawing a Bitmap
91    }
92
93    @Override
94    public void drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint) {
95        super.drawBitmap(bitmap, src, dst, paint);
96        mBitmaps.add(bitmap);
97        // Shaders in the Paint are ignored when drawing a Bitmap
98    }
99
100    @Override
101    public void drawBitmap(int[] colors, int offset, int stride, float x, float y, int width,
102            int height, boolean hasAlpha, Paint paint) {
103        super.drawBitmap(colors, offset, stride, x, y, width, height, hasAlpha, paint);
104        // Shaders in the Paint are ignored when drawing a Bitmap
105    }
106
107    @Override
108    public void drawBitmap(int[] colors, int offset, int stride, int x, int y, int width,
109            int height, boolean hasAlpha, Paint paint) {
110        super.drawBitmap(colors, offset, stride, x, y, width, height, hasAlpha, paint);
111        // Shaders in the Paint are ignored when drawing a Bitmap
112    }
113
114    @Override
115    public void drawBitmapMesh(Bitmap bitmap, int meshWidth, int meshHeight, float[] verts,
116            int vertOffset, int[] colors, int colorOffset, Paint paint) {
117        super.drawBitmapMesh(bitmap, meshWidth, meshHeight, verts, vertOffset, colors, colorOffset,
118                paint);
119        mBitmaps.add(bitmap);
120        // Shaders in the Paint are ignored when drawing a Bitmap
121    }
122
123    @Override
124    public void drawCircle(float cx, float cy, float radius, Paint paint) {
125        super.drawCircle(cx, cy, radius, paint);
126        recordShaderBitmap(paint);
127    }
128
129    @Override
130    public void drawLine(float startX, float startY, float stopX, float stopY, Paint paint) {
131        super.drawLine(startX, startY, stopX, stopY, paint);
132        recordShaderBitmap(paint);
133    }
134
135    @Override
136    public void drawLines(float[] pts, int offset, int count, Paint paint) {
137        super.drawLines(pts, offset, count, paint);
138        recordShaderBitmap(paint);
139    }
140
141    @Override
142    public void drawLines(float[] pts, Paint paint) {
143        super.drawLines(pts, paint);
144        recordShaderBitmap(paint);
145    }
146
147    @Override
148    public void drawOval(RectF oval, Paint paint) {
149        super.drawOval(oval, paint);
150        recordShaderBitmap(paint);
151    }
152
153    @Override
154    public void drawPaint(Paint paint) {
155        super.drawPaint(paint);
156        recordShaderBitmap(paint);
157    }
158
159    @Override
160    public void drawPath(Path path, Paint paint) {
161        super.drawPath(path, paint);
162        recordShaderBitmap(paint);
163    }
164
165    @Override
166    public void drawPoint(float x, float y, Paint paint) {
167        super.drawPoint(x, y, paint);
168        recordShaderBitmap(paint);
169    }
170
171    @Override
172    public void drawPoints(float[] pts, int offset, int count, Paint paint) {
173        super.drawPoints(pts, offset, count, paint);
174        recordShaderBitmap(paint);
175    }
176
177    @Override
178    public void drawPoints(float[] pts, Paint paint) {
179        super.drawPoints(pts, paint);
180        recordShaderBitmap(paint);
181    }
182
183    @Override
184    public void drawPosText(char[] text, int index, int count, float[] pos, Paint paint) {
185        super.drawPosText(text, index, count, pos, paint);
186        recordShaderBitmap(paint);
187    }
188
189    @Override
190    public void drawPosText(String text, float[] pos, Paint paint) {
191        super.drawPosText(text, pos, paint);
192        recordShaderBitmap(paint);
193    }
194
195    @Override
196    public void drawRect(float left, float top, float right, float bottom, Paint paint) {
197        super.drawRect(left, top, right, bottom, paint);
198        recordShaderBitmap(paint);
199    }
200
201    @Override
202    public void drawRect(Rect r, Paint paint) {
203        super.drawRect(r, paint);
204        recordShaderBitmap(paint);
205    }
206
207    @Override
208    public void drawRect(RectF r, Paint paint) {
209        super.drawRect(r, paint);
210        recordShaderBitmap(paint);
211    }
212
213    @Override
214    public void drawRoundRect(RectF rect, float rx, float ry, Paint paint) {
215        super.drawRoundRect(rect, rx, ry, paint);
216        recordShaderBitmap(paint);
217    }
218
219    @Override
220    public void drawText(char[] text, int index, int count, float x, float y, Paint paint) {
221        super.drawText(text, index, count, x, y, paint);
222        recordShaderBitmap(paint);
223    }
224
225    @Override
226    public void drawText(CharSequence text, int start, int end, float x, float y, Paint paint) {
227        super.drawText(text, start, end, x, y, paint);
228        recordShaderBitmap(paint);
229    }
230
231    @Override
232    public void drawText(String text, int start, int end, float x, float y, Paint paint) {
233        super.drawText(text, start, end, x, y, paint);
234        recordShaderBitmap(paint);
235    }
236
237    @Override
238    public void drawText(String text, float x, float y, Paint paint) {
239        super.drawText(text, x, y, paint);
240        recordShaderBitmap(paint);
241    }
242
243    @Override
244    public void drawTextOnPath(char[] text, int index, int count, Path path, float hOffset,
245            float vOffset, Paint paint) {
246        super.drawTextOnPath(text, index, count, path, hOffset, vOffset, paint);
247        recordShaderBitmap(paint);
248    }
249
250    @Override
251    public void drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint) {
252        super.drawTextOnPath(text, path, hOffset, vOffset, paint);
253        recordShaderBitmap(paint);
254    }
255
256    @Override
257    public void drawTextRun(char[] text, int index, int count, int contextIndex, int contextCount,
258            float x, float y, int dir, Paint paint) {
259        super.drawTextRun(text, index, count, contextIndex, contextCount, x, y, dir, paint);
260        recordShaderBitmap(paint);
261    }
262
263    @Override
264    public void drawTextRun(CharSequence text, int start, int end, int contextStart,
265            int contextEnd, float x, float y, int dir, Paint paint) {
266        super.drawTextRun(text, start, end, contextStart, contextEnd, x, y, dir, paint);
267        recordShaderBitmap(paint);
268    }
269
270    @Override
271    public void drawVertices(VertexMode mode, int vertexCount, float[] verts, int vertOffset,
272            float[] texs, int texOffset, int[] colors, int colorOffset, short[] indices,
273            int indexOffset, int indexCount, Paint paint) {
274        super.drawVertices(mode, vertexCount, verts, vertOffset, texs, texOffset, colors,
275                colorOffset, indices, indexOffset, indexCount, paint);
276        recordShaderBitmap(paint);
277    }
278}
279