blob: e9bd0c43976dea97edf95ce9271fa2ca1982a2f4 [file] [log] [blame]
Romain Guyb051e892010-09-28 19:09:36 -07001/*
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
Jeff Brown162a0212011-07-21 17:02:54 -070019import android.graphics.Bitmap;
Chet Haase9420abd2012-03-29 16:28:32 -070020import android.graphics.Matrix;
Jeff Brown162a0212011-07-21 17:02:54 -070021
22import java.util.ArrayList;
Chet Haasedaf98e92011-01-10 14:10:36 -080023
Romain Guyb051e892010-09-28 19:09:36 -070024/**
25 * An implementation of display list for OpenGL ES 2.0.
26 */
27class GLES20DisplayList extends DisplayList {
Chet Haaseca479d42012-08-30 17:20:08 -070028 // These lists ensure that any Bitmaps and DisplayLists recorded by a DisplayList are kept
29 // alive as long as the DisplayList is alive. The Bitmap and DisplayList lists
30 // are populated by the GLES20RecordingCanvas during appropriate drawing calls and are
31 // cleared at the start of a new drawing frame or when the view is detached from the window.
Jeff Brown162a0212011-07-21 17:02:54 -070032 final ArrayList<Bitmap> mBitmaps = new ArrayList<Bitmap>(5);
Chet Haaseca479d42012-08-30 17:20:08 -070033 final ArrayList<DisplayList> mChildDisplayLists = new ArrayList<DisplayList>();
Romain Guyb051e892010-09-28 19:09:36 -070034
Jeff Brown162a0212011-07-21 17:02:54 -070035 private GLES20RecordingCanvas mCanvas;
36 private boolean mValid;
Romain Guyb051e892010-09-28 19:09:36 -070037
Romain Guy13631f32012-01-30 17:41:55 -080038 // Used for debugging
39 private final String mName;
40
Patrick Dubroyf890fab2010-12-19 16:47:17 -080041 // The native display list will be destroyed when this object dies.
42 // DO NOT overwrite this reference once it is set.
Patrick Dubroyf890fab2010-12-19 16:47:17 -080043 private DisplayListFinalizer mFinalizer;
44
Romain Guy13631f32012-01-30 17:41:55 -080045 GLES20DisplayList(String name) {
46 mName = name;
47 }
48
Romain Guy0d6f4c02012-06-20 23:59:06 -070049 boolean hasNativeDisplayList() {
50 return mValid && mFinalizer != null;
51 }
52
Jeff Brown162a0212011-07-21 17:02:54 -070053 int getNativeDisplayList() {
54 if (!mValid || mFinalizer == null) {
55 throw new IllegalStateException("The display list is not valid.");
56 }
57 return mFinalizer.mNativeDisplayList;
Chet Haasedaf98e92011-01-10 14:10:36 -080058 }
59
Romain Guyb051e892010-09-28 19:09:36 -070060 @Override
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080061 public HardwareCanvas start() {
Jeff Brown162a0212011-07-21 17:02:54 -070062 if (mCanvas != null) {
Romain Guyb051e892010-09-28 19:09:36 -070063 throw new IllegalStateException("Recording has already started");
64 }
65
Jeff Brown162a0212011-07-21 17:02:54 -070066 mValid = false;
67 mCanvas = GLES20RecordingCanvas.obtain(this);
68 mCanvas.start();
Romain Guyb051e892010-09-28 19:09:36 -070069 return mCanvas;
70 }
71
Romain Guyb051e892010-09-28 19:09:36 -070072 @Override
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080073 public void invalidate() {
Jeff Brown162a0212011-07-21 17:02:54 -070074 if (mCanvas != null) {
75 mCanvas.recycle();
76 mCanvas = null;
77 }
Chet Haase9e90a992011-01-04 16:23:21 -080078 mValid = false;
79 }
80
81 @Override
Romain Guy38c2ece2012-05-24 14:20:56 -070082 public void clear() {
83 if (!mValid) {
84 mBitmaps.clear();
Chet Haaseca479d42012-08-30 17:20:08 -070085 mChildDisplayLists.clear();
Romain Guy38c2ece2012-05-24 14:20:56 -070086 }
87 }
88
89 @Override
Chet Haase6a2d17f2012-09-30 12:14:13 -070090 public void reset() {
91 if (hasNativeDisplayList()) {
92 nReset(mFinalizer.mNativeDisplayList);
93 }
94 }
95
96 @Override
Gilles Debunneb35ab7b2011-12-05 15:54:00 -080097 public boolean isValid() {
Chet Haase9e90a992011-01-04 16:23:21 -080098 return mValid;
99 }
100
101 @Override
Gilles Debunneb35ab7b2011-12-05 15:54:00 -0800102 public void end() {
Romain Guyb051e892010-09-28 19:09:36 -0700103 if (mCanvas != null) {
Jeff Brown162a0212011-07-21 17:02:54 -0700104 if (mFinalizer != null) {
105 mCanvas.end(mFinalizer.mNativeDisplayList);
106 } else {
107 mFinalizer = new DisplayListFinalizer(mCanvas.end(0));
Romain Guy13631f32012-01-30 17:41:55 -0800108 GLES20Canvas.setDisplayListName(mFinalizer.mNativeDisplayList, mName);
Jeff Brown162a0212011-07-21 17:02:54 -0700109 }
110 mCanvas.recycle();
111 mCanvas = null;
112 mValid = true;
Romain Guyb051e892010-09-28 19:09:36 -0700113 }
114 }
115
Romain Guy65b345f2011-07-27 18:51:50 -0700116 @Override
Gilles Debunneb35ab7b2011-12-05 15:54:00 -0800117 public int getSize() {
Romain Guy65b345f2011-07-27 18:51:50 -0700118 if (mFinalizer == null) return 0;
119 return GLES20Canvas.getDisplayListSize(mFinalizer.mNativeDisplayList);
120 }
121
Chet Haasea1cff502012-02-21 13:43:44 -0800122 ///////////////////////////////////////////////////////////////////////////
123 // Native View Properties
124 ///////////////////////////////////////////////////////////////////////////
125
126 @Override
127 public void setCaching(boolean caching) {
Romain Guy0d6f4c02012-06-20 23:59:06 -0700128 if (hasNativeDisplayList()) {
129 nSetCaching(mFinalizer.mNativeDisplayList, caching);
Chet Haasea1cff502012-02-21 13:43:44 -0800130 }
131 }
132
133 @Override
134 public void setClipChildren(boolean clipChildren) {
Romain Guy0d6f4c02012-06-20 23:59:06 -0700135 if (hasNativeDisplayList()) {
136 nSetClipChildren(mFinalizer.mNativeDisplayList, clipChildren);
Chet Haasea1cff502012-02-21 13:43:44 -0800137 }
138 }
139
140 @Override
Chet Haase9420abd2012-03-29 16:28:32 -0700141 public void setStaticMatrix(Matrix matrix) {
Romain Guy0d6f4c02012-06-20 23:59:06 -0700142 if (hasNativeDisplayList()) {
143 nSetStaticMatrix(mFinalizer.mNativeDisplayList, matrix.native_instance);
Chet Haase9420abd2012-03-29 16:28:32 -0700144 }
145 }
146
147 @Override
148 public void setAnimationMatrix(Matrix matrix) {
Romain Guy0d6f4c02012-06-20 23:59:06 -0700149 if (hasNativeDisplayList()) {
150 nSetAnimationMatrix(mFinalizer.mNativeDisplayList,
Chet Haaseafd5c3e2012-05-10 13:21:10 -0700151 (matrix != null) ? matrix.native_instance : 0);
Chet Haasea1cff502012-02-21 13:43:44 -0800152 }
153 }
154
155 @Override
156 public void setAlpha(float alpha) {
Romain Guy0d6f4c02012-06-20 23:59:06 -0700157 if (hasNativeDisplayList()) {
158 nSetAlpha(mFinalizer.mNativeDisplayList, alpha);
Chet Haasea1cff502012-02-21 13:43:44 -0800159 }
160 }
161
162 @Override
Chet Haasedb8c9a62012-03-21 18:54:18 -0700163 public void setHasOverlappingRendering(boolean hasOverlappingRendering) {
Romain Guy0d6f4c02012-06-20 23:59:06 -0700164 if (hasNativeDisplayList()) {
165 nSetHasOverlappingRendering(mFinalizer.mNativeDisplayList, hasOverlappingRendering);
Chet Haasedb8c9a62012-03-21 18:54:18 -0700166 }
167 }
168
169 @Override
Chet Haasea1cff502012-02-21 13:43:44 -0800170 public void setTranslationX(float translationX) {
Romain Guy0d6f4c02012-06-20 23:59:06 -0700171 if (hasNativeDisplayList()) {
172 nSetTranslationX(mFinalizer.mNativeDisplayList, translationX);
Chet Haasea1cff502012-02-21 13:43:44 -0800173 }
174 }
175
176 @Override
177 public void setTranslationY(float translationY) {
Romain Guy0d6f4c02012-06-20 23:59:06 -0700178 if (hasNativeDisplayList()) {
179 nSetTranslationY(mFinalizer.mNativeDisplayList, translationY);
Chet Haasea1cff502012-02-21 13:43:44 -0800180 }
181 }
182
183 @Override
184 public void setRotation(float rotation) {
Romain Guy0d6f4c02012-06-20 23:59:06 -0700185 if (hasNativeDisplayList()) {
186 nSetRotation(mFinalizer.mNativeDisplayList, rotation);
Chet Haasea1cff502012-02-21 13:43:44 -0800187 }
188 }
189
190 @Override
191 public void setRotationX(float rotationX) {
Romain Guy0d6f4c02012-06-20 23:59:06 -0700192 if (hasNativeDisplayList()) {
193 nSetRotationX(mFinalizer.mNativeDisplayList, rotationX);
Chet Haasea1cff502012-02-21 13:43:44 -0800194 }
195 }
196
197 @Override
198 public void setRotationY(float rotationY) {
Romain Guy0d6f4c02012-06-20 23:59:06 -0700199 if (hasNativeDisplayList()) {
200 nSetRotationY(mFinalizer.mNativeDisplayList, rotationY);
Chet Haasea1cff502012-02-21 13:43:44 -0800201 }
202 }
203
204 @Override
205 public void setScaleX(float scaleX) {
Romain Guy0d6f4c02012-06-20 23:59:06 -0700206 if (hasNativeDisplayList()) {
207 nSetScaleX(mFinalizer.mNativeDisplayList, scaleX);
Chet Haasea1cff502012-02-21 13:43:44 -0800208 }
209 }
210
211 @Override
212 public void setScaleY(float scaleY) {
Romain Guy0d6f4c02012-06-20 23:59:06 -0700213 if (hasNativeDisplayList()) {
214 nSetScaleY(mFinalizer.mNativeDisplayList, scaleY);
Chet Haasea1cff502012-02-21 13:43:44 -0800215 }
216 }
217
218 @Override
219 public void setTransformationInfo(float alpha, float translationX, float translationY,
220 float rotation, float rotationX, float rotationY, float scaleX, float scaleY) {
Romain Guy0d6f4c02012-06-20 23:59:06 -0700221 if (hasNativeDisplayList()) {
222 nSetTransformationInfo(mFinalizer.mNativeDisplayList, alpha, translationX, translationY,
Chet Haasea1cff502012-02-21 13:43:44 -0800223 rotation, rotationX, rotationY, scaleX, scaleY);
Chet Haasea1cff502012-02-21 13:43:44 -0800224 }
225 }
226
227 @Override
228 public void setPivotX(float pivotX) {
Romain Guy0d6f4c02012-06-20 23:59:06 -0700229 if (hasNativeDisplayList()) {
230 nSetPivotX(mFinalizer.mNativeDisplayList, pivotX);
Chet Haasea1cff502012-02-21 13:43:44 -0800231 }
232 }
233
234 @Override
235 public void setPivotY(float pivotY) {
Romain Guy0d6f4c02012-06-20 23:59:06 -0700236 if (hasNativeDisplayList()) {
237 nSetPivotY(mFinalizer.mNativeDisplayList, pivotY);
Chet Haasea1cff502012-02-21 13:43:44 -0800238 }
239 }
240
241 @Override
242 public void setCameraDistance(float distance) {
Romain Guy0d6f4c02012-06-20 23:59:06 -0700243 if (hasNativeDisplayList()) {
244 nSetCameraDistance(mFinalizer.mNativeDisplayList, distance);
Chet Haasea1cff502012-02-21 13:43:44 -0800245 }
246 }
247
248 @Override
249 public void setLeft(int left) {
Romain Guy0d6f4c02012-06-20 23:59:06 -0700250 if (hasNativeDisplayList()) {
251 nSetLeft(mFinalizer.mNativeDisplayList, left);
Chet Haasea1cff502012-02-21 13:43:44 -0800252 }
253 }
254
255 @Override
256 public void setTop(int top) {
Romain Guy0d6f4c02012-06-20 23:59:06 -0700257 if (hasNativeDisplayList()) {
258 nSetTop(mFinalizer.mNativeDisplayList, top);
Chet Haasea1cff502012-02-21 13:43:44 -0800259 }
260 }
261
262 @Override
263 public void setRight(int right) {
Romain Guy0d6f4c02012-06-20 23:59:06 -0700264 if (hasNativeDisplayList()) {
265 nSetRight(mFinalizer.mNativeDisplayList, right);
Chet Haasea1cff502012-02-21 13:43:44 -0800266 }
267 }
268
269 @Override
270 public void setBottom(int bottom) {
Romain Guy0d6f4c02012-06-20 23:59:06 -0700271 if (hasNativeDisplayList()) {
272 nSetBottom(mFinalizer.mNativeDisplayList, bottom);
Chet Haasea1cff502012-02-21 13:43:44 -0800273 }
274 }
275
276 @Override
277 public void setLeftTop(int left, int top) {
Romain Guy0d6f4c02012-06-20 23:59:06 -0700278 if (hasNativeDisplayList()) {
279 nSetLeftTop(mFinalizer.mNativeDisplayList, left, top);
Chet Haasea1cff502012-02-21 13:43:44 -0800280 }
281 }
282
283 @Override
284 public void setLeftTopRightBottom(int left, int top, int right, int bottom) {
Romain Guy0d6f4c02012-06-20 23:59:06 -0700285 if (hasNativeDisplayList()) {
286 nSetLeftTopRightBottom(mFinalizer.mNativeDisplayList, left, top, right, bottom);
Chet Haasea1cff502012-02-21 13:43:44 -0800287 }
288 }
289
290 @Override
291 public void offsetLeftRight(int offset) {
Romain Guy0d6f4c02012-06-20 23:59:06 -0700292 if (hasNativeDisplayList()) {
293 nOffsetLeftRight(mFinalizer.mNativeDisplayList, offset);
Chet Haasea1cff502012-02-21 13:43:44 -0800294 }
295 }
296
297 @Override
298 public void offsetTopBottom(int offset) {
Romain Guy0d6f4c02012-06-20 23:59:06 -0700299 if (hasNativeDisplayList()) {
300 nOffsetTopBottom(mFinalizer.mNativeDisplayList, offset);
Chet Haasea1cff502012-02-21 13:43:44 -0800301 }
302 }
303
Chet Haase6a2d17f2012-09-30 12:14:13 -0700304 private static native void nReset(int displayList);
Chet Haasea1cff502012-02-21 13:43:44 -0800305 private static native void nOffsetTopBottom(int displayList, int offset);
306 private static native void nOffsetLeftRight(int displayList, int offset);
307 private static native void nSetLeftTopRightBottom(int displayList, int left, int top,
308 int right, int bottom);
309 private static native void nSetLeftTop(int displayList, int left, int top);
310 private static native void nSetBottom(int displayList, int bottom);
311 private static native void nSetRight(int displayList, int right);
312 private static native void nSetTop(int displayList, int top);
313 private static native void nSetLeft(int displayList, int left);
314 private static native void nSetCameraDistance(int displayList, float distance);
315 private static native void nSetPivotY(int displayList, float pivotY);
316 private static native void nSetPivotX(int displayList, float pivotX);
317 private static native void nSetCaching(int displayList, boolean caching);
318 private static native void nSetClipChildren(int displayList, boolean clipChildren);
Chet Haasea1cff502012-02-21 13:43:44 -0800319 private static native void nSetAlpha(int displayList, float alpha);
Chet Haasedb8c9a62012-03-21 18:54:18 -0700320 private static native void nSetHasOverlappingRendering(int displayList,
321 boolean hasOverlappingRendering);
Chet Haasea1cff502012-02-21 13:43:44 -0800322 private static native void nSetTranslationX(int displayList, float translationX);
323 private static native void nSetTranslationY(int displayList, float translationY);
324 private static native void nSetRotation(int displayList, float rotation);
325 private static native void nSetRotationX(int displayList, float rotationX);
326 private static native void nSetRotationY(int displayList, float rotationY);
327 private static native void nSetScaleX(int displayList, float scaleX);
328 private static native void nSetScaleY(int displayList, float scaleY);
329 private static native void nSetTransformationInfo(int displayList, float alpha,
330 float translationX, float translationY, float rotation, float rotationX,
331 float rotationY, float scaleX, float scaleY);
Chet Haase9420abd2012-03-29 16:28:32 -0700332 private static native void nSetStaticMatrix(int displayList, int nativeMatrix);
333 private static native void nSetAnimationMatrix(int displayList, int animationMatrix);
Chet Haasea1cff502012-02-21 13:43:44 -0800334
335
336 ///////////////////////////////////////////////////////////////////////////
337 // Finalization
338 ///////////////////////////////////////////////////////////////////////////
339
Patrick Dubroyf890fab2010-12-19 16:47:17 -0800340 private static class DisplayListFinalizer {
Jeff Brown162a0212011-07-21 17:02:54 -0700341 final int mNativeDisplayList;
Chet Haase5c13d892010-10-08 08:37:55 -0700342
Jeff Brown162a0212011-07-21 17:02:54 -0700343 public DisplayListFinalizer(int nativeDisplayList) {
Patrick Dubroyf890fab2010-12-19 16:47:17 -0800344 mNativeDisplayList = nativeDisplayList;
Chet Haase5c13d892010-10-08 08:37:55 -0700345 }
346
347 @Override
Patrick Dubroyf890fab2010-12-19 16:47:17 -0800348 protected void finalize() throws Throwable {
Romain Guy6c319ca2011-01-11 14:29:25 -0800349 try {
Jeff Brown162a0212011-07-21 17:02:54 -0700350 GLES20Canvas.destroyDisplayList(mNativeDisplayList);
Romain Guy6c319ca2011-01-11 14:29:25 -0800351 } finally {
352 super.finalize();
353 }
Chet Haase5c13d892010-10-08 08:37:55 -0700354 }
355 }
Romain Guyb051e892010-09-28 19:09:36 -0700356}