Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [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 | |
Romain Guy | 02ccac6 | 2011-06-24 13:20:23 -0700 | [diff] [blame] | 19 | import android.graphics.Bitmap; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 20 | import android.graphics.Canvas; |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 21 | import android.graphics.Matrix; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * A hardware layer can be used to render graphics operations into a hardware |
| 25 | * friendly buffer. For instance, with an OpenGL backend, a hardware layer |
| 26 | * would use a Frame Buffer Object (FBO.) The hardware layer can be used as |
| 27 | * a drawing cache when a complex set of graphics operations needs to be |
| 28 | * drawn several times. |
| 29 | */ |
| 30 | abstract class HardwareLayer { |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 31 | /** |
| 32 | * Indicates an unknown dimension (width or height.) |
| 33 | */ |
| 34 | static final int DIMENSION_UNDEFINED = -1; |
| 35 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 36 | int mWidth; |
| 37 | int mHeight; |
| 38 | |
Romain Guy | 02ccac6 | 2011-06-24 13:20:23 -0700 | [diff] [blame] | 39 | boolean mOpaque; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 40 | |
| 41 | /** |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 42 | * Creates a new hardware layer with undefined dimensions. |
| 43 | */ |
| 44 | HardwareLayer() { |
| 45 | this(DIMENSION_UNDEFINED, DIMENSION_UNDEFINED, false); |
| 46 | } |
| 47 | |
| 48 | /** |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 49 | * Creates a new hardware layer at least as large as the supplied |
| 50 | * dimensions. |
| 51 | * |
| 52 | * @param width The minimum width of the layer |
| 53 | * @param height The minimum height of the layer |
| 54 | * @param isOpaque Whether the layer should be opaque or not |
| 55 | */ |
| 56 | HardwareLayer(int width, int height, boolean isOpaque) { |
| 57 | mWidth = width; |
| 58 | mHeight = height; |
| 59 | mOpaque = isOpaque; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Returns the minimum width of the layer. |
| 64 | * |
| 65 | * @return The minimum desired width of the hardware layer |
| 66 | */ |
| 67 | int getWidth() { |
| 68 | return mWidth; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Returns the minimum height of the layer. |
| 73 | * |
| 74 | * @return The minimum desired height of the hardware layer |
| 75 | */ |
| 76 | int getHeight() { |
| 77 | return mHeight; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Returns whether or not this layer is opaque. |
| 82 | * |
| 83 | * @return True if the layer is opaque, false otherwise |
| 84 | */ |
| 85 | boolean isOpaque() { |
| 86 | return mOpaque; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Indicates whether this layer can be rendered. |
| 91 | * |
| 92 | * @return True if the layer can be rendered into, false otherwise |
| 93 | */ |
| 94 | abstract boolean isValid(); |
| 95 | |
| 96 | /** |
Romain Guy | 02ccac6 | 2011-06-24 13:20:23 -0700 | [diff] [blame] | 97 | * Resize the layer, if necessary, to be at least as large |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 98 | * as the supplied dimensions. |
| 99 | * |
| 100 | * @param width The new desired minimum width for this layer |
| 101 | * @param height The new desired minimum height for this layer |
| 102 | */ |
| 103 | abstract void resize(int width, int height); |
| 104 | |
| 105 | /** |
| 106 | * Returns a hardware canvas that can be used to render onto |
| 107 | * this layer. |
| 108 | * |
| 109 | * @return A hardware canvas, or null if a canvas cannot be created |
| 110 | */ |
| 111 | abstract HardwareCanvas getCanvas(); |
| 112 | |
| 113 | /** |
| 114 | * Destroys resources without waiting for a GC. |
| 115 | */ |
| 116 | abstract void destroy(); |
| 117 | |
| 118 | /** |
| 119 | * This must be invoked before drawing onto this layer. |
| 120 | * @param currentCanvas |
| 121 | */ |
| 122 | abstract HardwareCanvas start(Canvas currentCanvas); |
| 123 | |
| 124 | /** |
| 125 | * This must be invoked after drawing onto this layer. |
| 126 | * @param currentCanvas |
| 127 | */ |
| 128 | abstract void end(Canvas currentCanvas); |
Romain Guy | 02ccac6 | 2011-06-24 13:20:23 -0700 | [diff] [blame] | 129 | |
| 130 | /** |
| 131 | * Copies this layer into the specified bitmap. |
| 132 | * |
| 133 | * @param bitmap The bitmap to copy they layer into |
| 134 | * |
| 135 | * @return True if the copy was successful, false otherwise |
| 136 | */ |
| 137 | abstract boolean copyInto(Bitmap bitmap); |
| 138 | |
| 139 | /** |
| 140 | * Update the layer's properties. This method should be used |
| 141 | * when the underlying storage is modified by an external entity. |
| 142 | * To change the underlying storage, use the {@link #resize(int, int)} |
| 143 | * method instead. |
| 144 | * |
| 145 | * @param width The new width of this layer |
| 146 | * @param height The new height of this layer |
| 147 | * @param isOpaque Whether this layer is opaque |
| 148 | */ |
| 149 | void update(int width, int height, boolean isOpaque) { |
| 150 | mWidth = width; |
| 151 | mHeight = height; |
| 152 | mOpaque = isOpaque; |
| 153 | } |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 154 | |
| 155 | /** |
| 156 | * Sets an optional transform on this layer. |
| 157 | * |
| 158 | * @param matrix The transform to apply to the layer. |
| 159 | */ |
| 160 | abstract void setTransform(Matrix matrix); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 161 | } |