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 17 package android.view; 18 19 import android.graphics.Bitmap; 20 21 import java.util.ArrayList; 22 23 /** 24 * An implementation of display list for OpenGL ES 2.0. 25 */ 26 class GLES20DisplayList extends DisplayList { 27 // These lists ensure that any Bitmaps recorded by a DisplayList are kept alive as long 28 // as the DisplayList is alive. The Bitmaps are populated by the GLES20RecordingCanvas. 29 final ArrayList<Bitmap> mBitmaps = new ArrayList<Bitmap>(5); 30 31 private GLES20RecordingCanvas mCanvas; 32 private boolean mValid; 33 34 // The native display list will be destroyed when this object dies. 35 // DO NOT overwrite this reference once it is set. 36 private DisplayListFinalizer mFinalizer; 37 getNativeDisplayList()38 int getNativeDisplayList() { 39 if (!mValid || mFinalizer == null) { 40 throw new IllegalStateException("The display list is not valid."); 41 } 42 return mFinalizer.mNativeDisplayList; 43 } 44 45 @Override start()46 HardwareCanvas start() { 47 if (mCanvas != null) { 48 throw new IllegalStateException("Recording has already started"); 49 } 50 51 mValid = false; 52 mCanvas = GLES20RecordingCanvas.obtain(this); 53 mCanvas.start(); 54 return mCanvas; 55 } 56 57 @Override invalidate()58 void invalidate() { 59 if (mCanvas != null) { 60 mCanvas.recycle(); 61 mCanvas = null; 62 } 63 mValid = false; 64 } 65 66 @Override isValid()67 boolean isValid() { 68 return mValid; 69 } 70 71 @Override end()72 void end() { 73 if (mCanvas != null) { 74 if (mFinalizer != null) { 75 mCanvas.end(mFinalizer.mNativeDisplayList); 76 } else { 77 mFinalizer = new DisplayListFinalizer(mCanvas.end(0)); 78 } 79 mCanvas.recycle(); 80 mCanvas = null; 81 mValid = true; 82 } 83 } 84 85 @Override getSize()86 int getSize() { 87 if (mFinalizer == null) return 0; 88 return GLES20Canvas.getDisplayListSize(mFinalizer.mNativeDisplayList); 89 } 90 91 private static class DisplayListFinalizer { 92 final int mNativeDisplayList; 93 DisplayListFinalizer(int nativeDisplayList)94 public DisplayListFinalizer(int nativeDisplayList) { 95 mNativeDisplayList = nativeDisplayList; 96 } 97 98 @Override finalize()99 protected void finalize() throws Throwable { 100 try { 101 GLES20Canvas.destroyDisplayList(mNativeDisplayList); 102 } finally { 103 super.finalize(); 104 } 105 } 106 } 107 } 108