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 | c89b14b | 2012-08-08 14:53:48 -0700 | [diff] [blame] | 20 | import android.graphics.Canvas; |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 21 | import android.graphics.Matrix; |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 22 | import android.graphics.Paint; |
Romain Guy | 2bf68f0 | 2012-03-02 13:37:47 -0800 | [diff] [blame] | 23 | import android.graphics.Rect; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * A hardware layer can be used to render graphics operations into a hardware |
Romain Guy | 52036b1 | 2013-02-14 18:03:37 -0800 | [diff] [blame] | 27 | * friendly buffer. For instance, with an OpenGL backend a hardware layer |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 28 | * would use a Frame Buffer Object (FBO.) The hardware layer can be used as |
| 29 | * a drawing cache when a complex set of graphics operations needs to be |
| 30 | * drawn several times. |
| 31 | */ |
| 32 | abstract class HardwareLayer { |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 33 | /** |
| 34 | * Indicates an unknown dimension (width or height.) |
| 35 | */ |
| 36 | static final int DIMENSION_UNDEFINED = -1; |
| 37 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 38 | int mWidth; |
| 39 | int mHeight; |
Chet Haase | a1cff50 | 2012-02-21 13:43:44 -0800 | [diff] [blame] | 40 | DisplayList mDisplayList; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 41 | |
Romain Guy | 02ccac6 | 2011-06-24 13:20:23 -0700 | [diff] [blame] | 42 | boolean mOpaque; |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 43 | |
| 44 | /** |
Romain Guy | aa6c24c | 2011-04-28 18:40:04 -0700 | [diff] [blame] | 45 | * Creates a new hardware layer with undefined dimensions. |
| 46 | */ |
| 47 | HardwareLayer() { |
| 48 | this(DIMENSION_UNDEFINED, DIMENSION_UNDEFINED, false); |
| 49 | } |
| 50 | |
| 51 | /** |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 52 | * Creates a new hardware layer at least as large as the supplied |
| 53 | * dimensions. |
| 54 | * |
| 55 | * @param width The minimum width of the layer |
| 56 | * @param height The minimum height of the layer |
| 57 | * @param isOpaque Whether the layer should be opaque or not |
| 58 | */ |
| 59 | HardwareLayer(int width, int height, boolean isOpaque) { |
| 60 | mWidth = width; |
| 61 | mHeight = height; |
| 62 | mOpaque = isOpaque; |
| 63 | } |
| 64 | |
| 65 | /** |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 66 | * Update the paint used when drawing this layer. |
| 67 | * |
| 68 | * @param paint The paint used when the layer is drawn into the destination canvas. |
| 69 | * @see View#setLayerPaint(android.graphics.Paint) |
| 70 | */ |
Romain Guy | 52036b1 | 2013-02-14 18:03:37 -0800 | [diff] [blame] | 71 | void setLayerPaint(Paint paint) { } |
Chet Haase | d15ebf2 | 2012-09-05 11:40:29 -0700 | [diff] [blame] | 72 | |
| 73 | /** |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 74 | * Returns the minimum width of the layer. |
| 75 | * |
| 76 | * @return The minimum desired width of the hardware layer |
| 77 | */ |
| 78 | int getWidth() { |
| 79 | return mWidth; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns the minimum height of the layer. |
| 84 | * |
| 85 | * @return The minimum desired height of the hardware layer |
| 86 | */ |
| 87 | int getHeight() { |
| 88 | return mHeight; |
| 89 | } |
| 90 | |
| 91 | /** |
Chet Haase | a1cff50 | 2012-02-21 13:43:44 -0800 | [diff] [blame] | 92 | * Returns the DisplayList for the layer. |
| 93 | * |
| 94 | * @return The DisplayList of the hardware layer |
| 95 | */ |
| 96 | DisplayList getDisplayList() { |
| 97 | return mDisplayList; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Sets the DisplayList for the layer. |
| 102 | * |
| 103 | * @param displayList The new DisplayList for this layer |
| 104 | */ |
| 105 | void setDisplayList(DisplayList displayList) { |
| 106 | mDisplayList = displayList; |
| 107 | } |
| 108 | |
| 109 | /** |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 110 | * Returns whether or not this layer is opaque. |
| 111 | * |
| 112 | * @return True if the layer is opaque, false otherwise |
| 113 | */ |
| 114 | boolean isOpaque() { |
| 115 | return mOpaque; |
| 116 | } |
| 117 | |
| 118 | /** |
Romain Guy | 846a533 | 2012-07-11 17:44:57 -0700 | [diff] [blame] | 119 | * Sets whether or not this layer should be considered opaque. |
| 120 | * |
| 121 | * @param isOpaque True if the layer is opaque, false otherwise |
| 122 | */ |
| 123 | abstract void setOpaque(boolean isOpaque); |
| 124 | |
| 125 | /** |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 126 | * Indicates whether this layer can be rendered. |
| 127 | * |
| 128 | * @return True if the layer can be rendered into, false otherwise |
| 129 | */ |
| 130 | abstract boolean isValid(); |
| 131 | |
| 132 | /** |
Romain Guy | 02ccac6 | 2011-06-24 13:20:23 -0700 | [diff] [blame] | 133 | * Resize the layer, if necessary, to be at least as large |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 134 | * as the supplied dimensions. |
| 135 | * |
| 136 | * @param width The new desired minimum width for this layer |
| 137 | * @param height The new desired minimum height for this layer |
Chet Haase | 603f6de | 2012-09-14 15:31:25 -0700 | [diff] [blame] | 138 | * @return True if the resulting layer is valid, false otherwise |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 139 | */ |
Chet Haase | 603f6de | 2012-09-14 15:31:25 -0700 | [diff] [blame] | 140 | abstract boolean resize(int width, int height); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * Returns a hardware canvas that can be used to render onto |
| 144 | * this layer. |
| 145 | * |
| 146 | * @return A hardware canvas, or null if a canvas cannot be created |
Romain Guy | 52036b1 | 2013-02-14 18:03:37 -0800 | [diff] [blame] | 147 | * |
| 148 | * @see #start(android.graphics.Canvas) |
| 149 | * @see #end(android.graphics.Canvas) |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 150 | */ |
| 151 | abstract HardwareCanvas getCanvas(); |
| 152 | |
| 153 | /** |
| 154 | * Destroys resources without waiting for a GC. |
| 155 | */ |
| 156 | abstract void destroy(); |
| 157 | |
| 158 | /** |
| 159 | * This must be invoked before drawing onto this layer. |
Romain Guy | 52036b1 | 2013-02-14 18:03:37 -0800 | [diff] [blame] | 160 | * |
Romain Guy | 78dd96d | 2013-05-03 14:24:16 -0700 | [diff] [blame] | 161 | * @param currentCanvas The canvas whose rendering needs to be interrupted |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 162 | */ |
Romain Guy | c89b14b | 2012-08-08 14:53:48 -0700 | [diff] [blame] | 163 | abstract HardwareCanvas start(Canvas currentCanvas); |
Romain Guy | 52036b1 | 2013-02-14 18:03:37 -0800 | [diff] [blame] | 164 | |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 165 | /** |
Romain Guy | 78dd96d | 2013-05-03 14:24:16 -0700 | [diff] [blame] | 166 | * This must be invoked before drawing onto this layer. |
| 167 | * |
| 168 | * @param dirty The dirty area to repaint |
| 169 | * @param currentCanvas The canvas whose rendering needs to be interrupted |
| 170 | */ |
| 171 | abstract HardwareCanvas start(Canvas currentCanvas, Rect dirty); |
| 172 | |
| 173 | /** |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 174 | * This must be invoked after drawing onto this layer. |
Romain Guy | 52036b1 | 2013-02-14 18:03:37 -0800 | [diff] [blame] | 175 | * |
Romain Guy | 78dd96d | 2013-05-03 14:24:16 -0700 | [diff] [blame] | 176 | * @param currentCanvas The canvas whose rendering needs to be resumed |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 177 | */ |
Romain Guy | c89b14b | 2012-08-08 14:53:48 -0700 | [diff] [blame] | 178 | abstract void end(Canvas currentCanvas); |
Romain Guy | 02ccac6 | 2011-06-24 13:20:23 -0700 | [diff] [blame] | 179 | |
| 180 | /** |
| 181 | * Copies this layer into the specified bitmap. |
| 182 | * |
| 183 | * @param bitmap The bitmap to copy they layer into |
| 184 | * |
| 185 | * @return True if the copy was successful, false otherwise |
| 186 | */ |
| 187 | abstract boolean copyInto(Bitmap bitmap); |
| 188 | |
| 189 | /** |
| 190 | * Update the layer's properties. This method should be used |
| 191 | * when the underlying storage is modified by an external entity. |
| 192 | * To change the underlying storage, use the {@link #resize(int, int)} |
| 193 | * method instead. |
| 194 | * |
| 195 | * @param width The new width of this layer |
| 196 | * @param height The new height of this layer |
| 197 | * @param isOpaque Whether this layer is opaque |
| 198 | */ |
| 199 | void update(int width, int height, boolean isOpaque) { |
| 200 | mWidth = width; |
| 201 | mHeight = height; |
| 202 | mOpaque = isOpaque; |
| 203 | } |
Romain Guy | 302a9df | 2011-08-16 13:55:02 -0700 | [diff] [blame] | 204 | |
| 205 | /** |
| 206 | * Sets an optional transform on this layer. |
| 207 | * |
| 208 | * @param matrix The transform to apply to the layer. |
| 209 | */ |
| 210 | abstract void setTransform(Matrix matrix); |
Romain Guy | 2bf68f0 | 2012-03-02 13:37:47 -0800 | [diff] [blame] | 211 | |
| 212 | /** |
| 213 | * Specifies the display list to use to refresh the layer. |
Michael Jurka | 7e52caf | 2012-03-06 15:57:06 -0800 | [diff] [blame] | 214 | * |
Romain Guy | 2bf68f0 | 2012-03-02 13:37:47 -0800 | [diff] [blame] | 215 | * @param displayList The display list containing the drawing commands to |
| 216 | * execute in this layer |
| 217 | * @param dirtyRect The dirty region of the layer that needs to be redrawn |
| 218 | */ |
Romain Guy | 11cb642 | 2012-09-21 00:39:43 -0700 | [diff] [blame] | 219 | abstract void redrawLater(DisplayList displayList, Rect dirtyRect); |
Romain Guy | ef09a21 | 2012-09-25 12:17:14 -0700 | [diff] [blame] | 220 | |
| 221 | /** |
| 222 | * Indicates that this layer has lost its underlying storage. |
| 223 | */ |
| 224 | abstract void clearStorage(); |
Romain Guy | 6c319ca | 2011-01-11 14:29:25 -0800 | [diff] [blame] | 225 | } |