Home | History | Annotate | Download | only in view
      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.annotation.NonNull;
     20 import android.annotation.Nullable;
     21 import android.util.Pools.SynchronizedPool;
     22 
     23 /**
     24  * An implementation of a GL canvas that records drawing operations.
     25  * This is intended for use with a DisplayList. This class keeps a list of all the Paint and
     26  * Bitmap objects that it draws, preventing the backing memory of Bitmaps from being freed while
     27  * the DisplayList is still holding a native reference to the memory.
     28  */
     29 class GLES20RecordingCanvas extends GLES20Canvas {
     30     // The recording canvas pool should be large enough to handle a deeply nested
     31     // view hierarchy because display lists are generated recursively.
     32     private static final int POOL_LIMIT = 25;
     33 
     34     private static final SynchronizedPool<GLES20RecordingCanvas> sPool =
     35             new SynchronizedPool<GLES20RecordingCanvas>(POOL_LIMIT);
     36 
     37     RenderNode mNode;
     38 
     39     private GLES20RecordingCanvas() {
     40         super();
     41     }
     42 
     43     static GLES20RecordingCanvas obtain(@NonNull RenderNode node) {
     44         if (node == null) throw new IllegalArgumentException("node cannot be null");
     45         GLES20RecordingCanvas canvas = sPool.acquire();
     46         if (canvas == null) {
     47             canvas = new GLES20RecordingCanvas();
     48         }
     49         canvas.mNode = node;
     50         return canvas;
     51     }
     52 
     53     void recycle() {
     54         mNode = null;
     55         sPool.release(this);
     56     }
     57 
     58     long finishRecording() {
     59         return nFinishRecording(mRenderer);
     60     }
     61 
     62     @Override
     63     public boolean isRecordingFor(Object o) {
     64         return o == mNode;
     65     }
     66 }
     67