Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_UI_OPENGL_RENDERER_H |
| 18 | #define ANDROID_UI_OPENGL_RENDERER_H |
| 19 | |
| 20 | #include <GLES2/gl2.h> |
| 21 | #include <GLES2/gl2ext.h> |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 22 | |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 23 | #include <SkBitmap.h> |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 24 | #include <SkMatrix.h> |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 25 | #include <SkPaint.h> |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 26 | #include <SkXfermode.h> |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 27 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 28 | #include <utils/RefBase.h> |
| 29 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 30 | #include "Matrix.h" |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 31 | #include "Program.h" |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 32 | #include "Rect.h" |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 33 | #include "Snapshot.h" |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 34 | #include "Texture.h" |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame^] | 35 | #include "Layer.h" |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 36 | #include "TextureCache.h" |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame^] | 37 | #include "LayerCache.h" |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 38 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 39 | namespace android { |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 40 | namespace uirenderer { |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 41 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 42 | /////////////////////////////////////////////////////////////////////////////// |
| 43 | // Support |
| 44 | /////////////////////////////////////////////////////////////////////////////// |
| 45 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 46 | /** |
| 47 | * Simple structure to describe a vertex with a position. |
| 48 | * This is used to draw filled rectangles without a texture. |
| 49 | */ |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 50 | struct SimpleVertex { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 51 | float position[2]; |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 52 | }; // struct SimpleVertex |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 53 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 54 | /** |
| 55 | * Simple structure to describe a vertex with a position and a texture. |
| 56 | */ |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 57 | struct TextureVertex { |
| 58 | float position[2]; |
| 59 | float texture[2]; |
| 60 | }; // struct TextureVertex |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 61 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 62 | /** |
| 63 | * Structure mapping Skia xfermodes to OpenGL blending factors. |
| 64 | */ |
| 65 | struct Blender { |
| 66 | SkXfermode::Mode mode; |
| 67 | GLenum src; |
| 68 | GLenum dst; |
| 69 | }; // struct Blender |
| 70 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 71 | /////////////////////////////////////////////////////////////////////////////// |
| 72 | // Renderer |
| 73 | /////////////////////////////////////////////////////////////////////////////// |
| 74 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 75 | /** |
| 76 | * OpenGL renderer used to draw accelerated 2D graphics. The API is a |
| 77 | * simplified version of Skia's Canvas API. |
| 78 | */ |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 79 | class OpenGLRenderer { |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 80 | public: |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 81 | OpenGLRenderer(); |
| 82 | ~OpenGLRenderer(); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 83 | |
| 84 | void setViewport(int width, int height); |
| 85 | void prepare(); |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 86 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 87 | int getSaveCount() const; |
| 88 | int save(int flags); |
| 89 | void restore(); |
| 90 | void restoreToCount(int saveCount); |
| 91 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 92 | int saveLayer(float left, float top, float right, float bottom, const SkPaint* p, int flags); |
| 93 | int saveLayerAlpha(float left, float top, float right, float bottom, int alpha, int flags); |
| 94 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 95 | void translate(float dx, float dy); |
| 96 | void rotate(float degrees); |
| 97 | void scale(float sx, float sy); |
| 98 | |
| 99 | void setMatrix(SkMatrix* matrix); |
| 100 | void getMatrix(SkMatrix* matrix); |
| 101 | void concatMatrix(SkMatrix* matrix); |
| 102 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 103 | const Rect& getClipBounds(); |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 104 | bool quickReject(float left, float top, float right, float bottom); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 105 | bool clipRect(float left, float top, float right, float bottom); |
| 106 | |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 107 | void drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint); |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 108 | void drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint); |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 109 | void drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop, float srcRight, float srcBottom, |
Romain Guy | f86ef57 | 2010-07-01 11:05:42 -0700 | [diff] [blame] | 110 | float dstLeft, float dstTop, float dstRight, float dstBottom, const SkPaint* paint); |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 111 | void drawColor(int color, SkXfermode::Mode mode); |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 112 | void drawRect(float left, float top, float right, float bottom, const SkPaint* paint); |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 113 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 114 | private: |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 115 | /** |
| 116 | * Saves the current state of the renderer as a new snapshot. |
| 117 | * The new snapshot is saved in mSnapshot and the previous snapshot |
| 118 | * is linked from mSnapshot->previous. |
| 119 | * |
| 120 | * @return The new save count. This value can be passed to #restoreToCount() |
| 121 | */ |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 122 | int saveSnapshot(); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 123 | |
| 124 | /** |
| 125 | * Restores the current snapshot; mSnapshot becomes mSnapshot->previous. |
| 126 | * |
| 127 | * @return True if the clip should be also reapplied by calling |
| 128 | * #setScissorFromClip(). |
| 129 | */ |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 130 | bool restoreSnapshot(); |
| 131 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 132 | /** |
| 133 | * Sets the clipping rectangle using glScissor. The clip is defined by |
| 134 | * the current snapshot's clipRect member. |
| 135 | */ |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 136 | void setScissorFromClip(); |
| 137 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 138 | /** |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 139 | * Compose the layer defined in the current snapshot with the layer |
| 140 | * defined by the previous snapshot. |
| 141 | * |
| 142 | * The current snapshot *must* be a layer (flag kFlagIsLayer set.) |
| 143 | * |
| 144 | * @param curent The current snapshot containing the layer to compose |
| 145 | * @param previous The previous snapshot to compose the current layer with |
| 146 | */ |
| 147 | void composeLayer(sp<Snapshot> current, sp<Snapshot> previous); |
| 148 | |
| 149 | /** |
| 150 | * Creates a new layer stored in the specified snapshot. |
| 151 | * |
| 152 | * @param snapshot The snapshot associated with the new layer |
| 153 | * @param left The left coordinate of the layer |
| 154 | * @param top The top coordinate of the layer |
| 155 | * @param right The right coordinate of the layer |
| 156 | * @param bottom The bottom coordinate of the layer |
| 157 | * @param alpha The translucency of the layer |
| 158 | * @param mode The blending mode of the layer |
| 159 | * @param flags The layer save flags |
| 160 | * |
| 161 | * @return True if the layer was successfully created, false otherwise |
| 162 | */ |
| 163 | bool createLayer(sp<Snapshot> snapshot, float left, float top, float right, float bottom, |
| 164 | int alpha, SkXfermode::Mode mode, int flags); |
| 165 | |
| 166 | /** |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 167 | * Draws a colored rectangle with the specified color. The specified coordinates |
| 168 | * are transformed by the current snapshot's transform matrix. |
| 169 | * |
| 170 | * @param left The left coordinate of the rectangle |
| 171 | * @param top The top coordinate of the rectangle |
| 172 | * @param right The right coordinate of the rectangle |
| 173 | * @param bottom The bottom coordinate of the rectangle |
| 174 | * @param color The rectangle's ARGB color, defined as a packed 32 bits word |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 175 | * @param mode The Skia xfermode to use |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 176 | */ |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 177 | void drawColorRect(float left, float top, float right, float bottom, |
| 178 | int color, SkXfermode::Mode mode); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 179 | |
| 180 | /** |
| 181 | * Draws a textured rectangle with the specified texture. The specified coordinates |
| 182 | * are transformed by the current snapshot's transform matrix. |
| 183 | * |
| 184 | * @param left The left coordinate of the rectangle |
| 185 | * @param top The top coordinate of the rectangle |
| 186 | * @param right The right coordinate of the rectangle |
| 187 | * @param bottom The bottom coordinate of the rectangle |
| 188 | * @param texture The texture name to map onto the rectangle |
| 189 | * @param alpha An additional translucency parameter, between 0.0f and 1.0f |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 190 | * @param mode The blending mode |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 191 | * @param blend True if the texture contains an alpha channel |
Romain Guy | d55a861 | 2010-06-28 17:42:46 -0700 | [diff] [blame] | 192 | * @param isPremultiplied Indicates whether the texture has premultiplied alpha |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame] | 193 | */ |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 194 | void drawTextureRect(float left, float top, float right, float bottom, GLuint texture, |
Romain Guy | c1396e9 | 2010-06-30 17:56:19 -0700 | [diff] [blame] | 195 | float alpha, SkXfermode::Mode mode, bool blend, bool isPremultiplied = false); |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 196 | |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 197 | /** |
| 198 | * Resets the texture coordinates stored in mDrawTextureVertices. Setting the values |
| 199 | * back to default is achieved by calling: |
| 200 | * |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 201 | * resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 202 | * |
| 203 | * @param u1 The left coordinate of the texture |
| 204 | * @param v1 The bottom coordinate of the texture |
| 205 | * @param u2 The right coordinate of the texture |
| 206 | * @param v2 The top coordinate of the texture |
| 207 | */ |
| 208 | void resetDrawTextureTexCoords(float u1, float v1, float u2, float v2); |
| 209 | |
Romain Guy | 8ba548f | 2010-06-30 19:21:21 -0700 | [diff] [blame] | 210 | /** |
| 211 | * Gets the alpha and xfermode out of a paint object. If the paint is null |
| 212 | * alpha will be 255 and the xfermode will be SRC_OVER. |
| 213 | * |
| 214 | * @param paint The paint to extract values from |
| 215 | * @param alpha Where to store the resulting alpha |
| 216 | * @param mode Where to store the resulting xfermode |
| 217 | */ |
| 218 | inline void getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode); |
| 219 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 220 | // Dimensions of the drawing surface |
| 221 | int mWidth, mHeight; |
| 222 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 223 | // Matrix used for ortho projection in shaders |
| 224 | float mOrthoMatrix[16]; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 225 | |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 226 | // Model-view matrix used to position/size objects |
| 227 | mat4 mModelView; |
| 228 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 229 | // Number of saved states |
| 230 | int mSaveCount; |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 231 | // Base state |
| 232 | Snapshot mFirstSnapshot; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 233 | // Current state |
| 234 | sp<Snapshot> mSnapshot; |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 235 | |
| 236 | // Shaders |
| 237 | sp<DrawColorProgram> mDrawColorShader; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 238 | sp<DrawTextureProgram> mDrawTextureShader; |
Romain Guy | 026c5e16 | 2010-06-28 17:12:22 -0700 | [diff] [blame] | 239 | |
| 240 | // Used to draw textured quads |
| 241 | TextureVertex mDrawTextureVertices[4]; |
Romain Guy | ce0537b | 2010-06-29 21:05:21 -0700 | [diff] [blame] | 242 | |
| 243 | // Used to cache all drawBitmap textures |
| 244 | TextureCache mTextureCache; |
Romain Guy | dda57020 | 2010-07-06 11:39:32 -0700 | [diff] [blame^] | 245 | LayerCache mLayerCache; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 246 | }; // class OpenGLRenderer |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 247 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 248 | }; // namespace uirenderer |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 249 | }; // namespace android |
| 250 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 251 | #endif // ANDROID_UI_OPENGL_RENDERER_H |