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 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 52 boolean isValid() { 53 return mLayer != 0 && mLayerWidth > 0 && mLayerHeight > 0; 54 } 55 56 @Override 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 80 void setOpaque(boolean isOpaque) { 81 mOpaque = isOpaque; 82 GLES20Canvas.nSetOpaqueLayer(mLayer, isOpaque); 83 } 84 85 @Override 86 HardwareCanvas getCanvas() { 87 return mCanvas; 88 } 89 90 @Override 91 void end(Canvas currentCanvas) { 92 HardwareCanvas canvas = getCanvas(); 93 if (canvas != null) { 94 canvas.onPostDraw(); 95 } 96 if (currentCanvas instanceof GLES20Canvas) { 97 ((GLES20Canvas) currentCanvas).resume(); 98 } 99 } 100 101 @Override 102 HardwareCanvas start(Canvas currentCanvas) { 103 return start(currentCanvas, null); 104 } 105 106 @Override 107 HardwareCanvas start(Canvas currentCanvas, Rect dirty) { 108 if (currentCanvas instanceof GLES20Canvas) { 109 ((GLES20Canvas) currentCanvas).interrupt(); 110 } 111 HardwareCanvas canvas = getCanvas(); 112 canvas.setViewport(mWidth, mHeight); 113 canvas.onPreDraw(dirty); 114 return canvas; 115 } 116 117 /** 118 * Ignored 119 */ 120 @Override 121 void setTransform(Matrix matrix) { 122 } 123 124 @Override 125 void redrawLater(DisplayList displayList, Rect dirtyRect) { 126 GLES20Canvas.nUpdateRenderLayer(mLayer, mCanvas.getRenderer(), 127 ((GLES20DisplayList) displayList).getNativeDisplayList(), 128 dirtyRect.left, dirtyRect.top, dirtyRect.right, dirtyRect.bottom); 129 } 130 } 131