Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 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; |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 20 | import android.graphics.Matrix; |
Romain Guy | 2bf68f0 | 2012-03-02 13:37:47 -0800 | [diff] [blame] | 21 | import android.graphics.Rect; |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 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 { |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 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 |
Chet Haase | 603f6de | 2012-09-14 15:31:25 -0700 | [diff] [blame] | 57 | boolean resize(int width, int height) { |
| 58 | if (!isValid() || width <= 0 || height <= 0) return false; |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 59 | |
| 60 | mWidth = width; |
| 61 | mHeight = height; |
| 62 | |
| 63 | if (width != mLayerWidth || height != mLayerHeight) { |
| 64 | int[] layerInfo = new int[2]; |
| 65 | |
Chet Haase | 603f6de | 2012-09-14 15:31:25 -0700 | [diff] [blame] | 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 | } |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 75 | } |
Chet Haase | 603f6de | 2012-09-14 15:31:25 -0700 | [diff] [blame] | 76 | return isValid(); |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Override |
Romain Guy | 846a533 | 2012-07-11 17:44:57 -0700 | [diff] [blame] | 80 | void setOpaque(boolean isOpaque) { |
| 81 | mOpaque = isOpaque; |
| 82 | GLES20Canvas.nSetOpaqueLayer(mLayer, isOpaque); |
| 83 | } |
| 84 | |
| 85 | @Override |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 86 | HardwareCanvas getCanvas() { |
| 87 | return mCanvas; |
| 88 | } |
| 89 | |
| 90 | @Override |
Romain Guy | c89b14b | 2012-08-08 14:53:48 -0700 | [diff] [blame] | 91 | void end(Canvas currentCanvas) { |
Romain Guy | 52036b1 | 2013-02-14 18:03:37 -0800 | [diff] [blame] | 92 | HardwareCanvas canvas = getCanvas(); |
| 93 | if (canvas != null) { |
| 94 | canvas.onPostDraw(); |
| 95 | } |
Romain Guy | e78b800 | 2013-03-04 19:08:02 -0800 | [diff] [blame] | 96 | if (currentCanvas instanceof GLES20Canvas) { |
| 97 | ((GLES20Canvas) currentCanvas).resume(); |
| 98 | } |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Override |
Romain Guy | c89b14b | 2012-08-08 14:53:48 -0700 | [diff] [blame] | 102 | HardwareCanvas start(Canvas currentCanvas) { |
Romain Guy | 78dd96d | 2013-05-03 14:24:16 -0700 | [diff] [blame] | 103 | return start(currentCanvas, null); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | HardwareCanvas start(Canvas currentCanvas, Rect dirty) { |
Romain Guy | c89b14b | 2012-08-08 14:53:48 -0700 | [diff] [blame] | 108 | if (currentCanvas instanceof GLES20Canvas) { |
| 109 | ((GLES20Canvas) currentCanvas).interrupt(); |
| 110 | } |
Romain Guy | 52036b1 | 2013-02-14 18:03:37 -0800 | [diff] [blame] | 111 | HardwareCanvas canvas = getCanvas(); |
| 112 | canvas.setViewport(mWidth, mHeight); |
Romain Guy | 78dd96d | 2013-05-03 14:24:16 -0700 | [diff] [blame] | 113 | canvas.onPreDraw(dirty); |
Romain Guy | 52036b1 | 2013-02-14 18:03:37 -0800 | [diff] [blame] | 114 | return canvas; |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 115 | } |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 116 | |
| 117 | /** |
| 118 | * Ignored |
| 119 | */ |
| 120 | @Override |
| 121 | void setTransform(Matrix matrix) { |
| 122 | } |
Romain Guy | 2bf68f0 | 2012-03-02 13:37:47 -0800 | [diff] [blame] | 123 | |
| 124 | @Override |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 125 | void redrawLater(DisplayList displayList, Rect dirtyRect) { |
Romain Guy | 2bf68f0 | 2012-03-02 13:37:47 -0800 | [diff] [blame] | 126 | GLES20Canvas.nUpdateRenderLayer(mLayer, mCanvas.getRenderer(), |
Chris Craik | 5438979 | 2013-12-20 13:28:11 -0800 | [diff] [blame] | 127 | displayList.getNativeDisplayList(), |
Romain Guy | 2bf68f0 | 2012-03-02 13:37:47 -0800 | [diff] [blame] | 128 | dirtyRect.left, dirtyRect.top, dirtyRect.right, dirtyRect.bottom); |
| 129 | } |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 130 | } |