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 |
| 57 | void resize(int width, int height) { |
| 58 | if (!isValid() || width <= 0 || height <= 0) return; |
| 59 | |
| 60 | mWidth = width; |
| 61 | mHeight = height; |
| 62 | |
| 63 | if (width != mLayerWidth || height != mLayerHeight) { |
| 64 | int[] layerInfo = new int[2]; |
| 65 | |
| 66 | GLES20Canvas.nResizeLayer(mLayer, width, height, layerInfo); |
| 67 | |
| 68 | mLayerWidth = layerInfo[0]; |
| 69 | mLayerHeight = layerInfo[1]; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | HardwareCanvas getCanvas() { |
| 75 | return mCanvas; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | void end(Canvas currentCanvas) { |
| 80 | if (currentCanvas instanceof GLES20Canvas) { |
| 81 | ((GLES20Canvas) currentCanvas).resume(); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | HardwareCanvas start(Canvas currentCanvas) { |
| 87 | if (currentCanvas instanceof GLES20Canvas) { |
| 88 | ((GLES20Canvas) currentCanvas).interrupt(); |
| 89 | } |
| 90 | return getCanvas(); |
| 91 | } |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * Ignored |
| 95 | */ |
| 96 | @Override |
| 97 | void setTransform(Matrix matrix) { |
| 98 | } |
Romain Guy | 2bf68f0 | 2012-03-02 13:37:47 -0800 | [diff] [blame^] | 99 | |
| 100 | @Override |
| 101 | void redraw(DisplayList displayList, Rect dirtyRect) { |
| 102 | GLES20Canvas.nUpdateRenderLayer(mLayer, mCanvas.getRenderer(), |
| 103 | ((GLES20DisplayList) displayList).getNativeDisplayList(), |
| 104 | dirtyRect.left, dirtyRect.top, dirtyRect.right, dirtyRect.bottom); |
| 105 | } |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 106 | } |