blob: b6fc38d892f5eeb1acb1b2c7bd5f9bd81e0ac204 [file] [log] [blame]
Patrick Dubroyf890fab2010-12-19 16:47:17 -08001/*
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
Patrick Dubroyf890fab2010-12-19 16:47:17 -080019import android.graphics.Rect;
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080020import android.util.Pools.SynchronizedPool;
Patrick Dubroyf890fab2010-12-19 16:47:17 -080021
22/**
23 * An implementation of a GL canvas that records drawing operations.
24 * This is intended for use with a DisplayList. This class keeps a list of all the Paint and
25 * Bitmap objects that it draws, preventing the backing memory of Bitmaps from being freed while
26 * the DisplayList is still holding a native reference to the memory.
27 */
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080028class GLES20RecordingCanvas extends GLES20Canvas {
Jeff Brown162a0212011-07-21 17:02:54 -070029 // The recording canvas pool should be large enough to handle a deeply nested
30 // view hierarchy because display lists are generated recursively.
Romain Guy6d7475d2011-07-27 16:28:21 -070031 private static final int POOL_LIMIT = 25;
Patrick Dubroyf890fab2010-12-19 16:47:17 -080032
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080033 private static final SynchronizedPool<GLES20RecordingCanvas> sPool =
34 new SynchronizedPool<GLES20RecordingCanvas>(POOL_LIMIT);
Jeff Brown162a0212011-07-21 17:02:54 -070035
36 private GLES20DisplayList mDisplayList;
37
38 private GLES20RecordingCanvas() {
Romain Guyef359272013-01-31 19:07:29 -080039 super(true, true);
Jeff Brown162a0212011-07-21 17:02:54 -070040 }
41
42 static GLES20RecordingCanvas obtain(GLES20DisplayList displayList) {
43 GLES20RecordingCanvas canvas = sPool.acquire();
Svetoslav Ganovabae2a12012-11-27 16:59:37 -080044 if (canvas == null) {
45 canvas = new GLES20RecordingCanvas();
46 }
Jeff Brown162a0212011-07-21 17:02:54 -070047 canvas.mDisplayList = displayList;
48 return canvas;
49 }
50
51 void recycle() {
52 mDisplayList = null;
53 resetDisplayListRenderer();
54 sPool.release(this);
55 }
56
57 void start() {
Romain Guy3b748a42013-04-17 18:54:38 -070058 mDisplayList.clearReferences();
Jeff Brown162a0212011-07-21 17:02:54 -070059 }
60
61 int end(int nativeDisplayList) {
62 return getDisplayList(nativeDisplayList);
Patrick Dubroyf890fab2010-12-19 16:47:17 -080063 }
64
Patrick Dubroyf890fab2010-12-19 16:47:17 -080065 @Override
Chet Haaseca479d42012-08-30 17:20:08 -070066 public int drawDisplayList(DisplayList displayList, Rect dirty, int flags) {
67 int status = super.drawDisplayList(displayList, dirty, flags);
Romain Guy16c03182013-06-17 11:21:58 -070068 mDisplayList.getChildDisplayLists().add(displayList);
Chet Haaseca479d42012-08-30 17:20:08 -070069 return status;
70 }
Patrick Dubroyf890fab2010-12-19 16:47:17 -080071}