• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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.Canvas;
20 import android.graphics.Matrix;
21 import android.graphics.Rect;
22 
23 /**
24  * An OpenGL ES 2.0 implementation of {@link HardwareLayer}. This
25  * implementation can be used a rendering target. It generates a
26  * {@link Canvas} that can be used to render into an FBO using OpenGL.
27  */
28 class GLES20RenderLayer extends GLES20Layer {
29     private int mLayerWidth;
30     private int mLayerHeight;
31 
32     private final GLES20Canvas mCanvas;
33 
GLES20RenderLayer(int width, int height, boolean isOpaque)34     GLES20RenderLayer(int width, int height, boolean isOpaque) {
35         super(width, height, isOpaque);
36 
37         int[] layerInfo = new int[2];
38         mLayer = GLES20Canvas.nCreateLayer(width, height, isOpaque, layerInfo);
39         if (mLayer != 0) {
40             mLayerWidth = layerInfo[0];
41             mLayerHeight = layerInfo[1];
42 
43             mCanvas = new GLES20Canvas(mLayer, !isOpaque);
44             mFinalizer = new Finalizer(mLayer);
45         } else {
46             mCanvas = null;
47             mFinalizer = null;
48         }
49     }
50 
51     @Override
isValid()52     boolean isValid() {
53         return mLayer != 0 && mLayerWidth > 0 && mLayerHeight > 0;
54     }
55 
56     @Override
resize(int width, int height)57     boolean resize(int width, int height) {
58         if (!isValid() || width <= 0 || height <= 0) return false;
59 
60         mWidth = width;
61         mHeight = height;
62 
63         if (width != mLayerWidth || height != mLayerHeight) {
64             int[] layerInfo = new int[2];
65 
66             if (GLES20Canvas.nResizeLayer(mLayer, width, height, layerInfo)) {
67                 mLayerWidth = layerInfo[0];
68                 mLayerHeight = layerInfo[1];
69             } else {
70                 // Failure: not enough GPU resources for requested size
71                 mLayer = 0;
72                 mLayerWidth = 0;
73                 mLayerHeight = 0;
74             }
75         }
76         return isValid();
77     }
78 
79     @Override
setOpaque(boolean isOpaque)80     void setOpaque(boolean isOpaque) {
81         mOpaque = isOpaque;
82         GLES20Canvas.nSetOpaqueLayer(mLayer, isOpaque);
83     }
84 
85     @Override
getCanvas()86     HardwareCanvas getCanvas() {
87         return mCanvas;
88     }
89 
90     @Override
end(Canvas currentCanvas)91     void end(Canvas currentCanvas) {
92         if (currentCanvas instanceof GLES20Canvas) {
93             ((GLES20Canvas) currentCanvas).resume();
94         }
95     }
96 
97     @Override
start(Canvas currentCanvas)98     HardwareCanvas start(Canvas currentCanvas) {
99         if (currentCanvas instanceof GLES20Canvas) {
100             ((GLES20Canvas) currentCanvas).interrupt();
101         }
102         return getCanvas();
103     }
104 
105     /**
106      * Ignored
107      */
108     @Override
setTransform(Matrix matrix)109     void setTransform(Matrix matrix) {
110     }
111 
112     @Override
redrawLater(DisplayList displayList, Rect dirtyRect)113     void redrawLater(DisplayList displayList, Rect dirtyRect) {
114         GLES20Canvas.nUpdateRenderLayer(mLayer, mCanvas.getRenderer(),
115                 ((GLES20DisplayList) displayList).getNativeDisplayList(),
116                 dirtyRect.left, dirtyRect.top, dirtyRect.right, dirtyRect.bottom);
117     }
118 }
119