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