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 | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 23 | #include <SkMatrix.h> |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 24 | #include <SkXfermode.h> |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 25 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 26 | #include <utils/RefBase.h> |
| 27 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 28 | #include "Matrix.h" |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame^] | 29 | #include "Program.h" |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 30 | #include "Rect.h" |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame^] | 31 | #include "Snapshot.h" |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 32 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 33 | namespace android { |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 34 | namespace uirenderer { |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 35 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 36 | /////////////////////////////////////////////////////////////////////////////// |
| 37 | // Support |
| 38 | /////////////////////////////////////////////////////////////////////////////// |
| 39 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame^] | 40 | /** |
| 41 | * Simple structure to describe a vertex with a position. |
| 42 | * This is used to draw filled rectangles without a texture. |
| 43 | */ |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 44 | struct SimpleVertex { |
Romain Guy | 7ae7ac4 | 2010-06-25 13:46:18 -0700 | [diff] [blame] | 45 | float position[2]; |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 46 | }; // struct SimpleVertex |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 47 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame^] | 48 | /** |
| 49 | * Simple structure to describe a vertex with a position and a texture. |
| 50 | */ |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 51 | struct TextureVertex { |
| 52 | float position[2]; |
| 53 | float texture[2]; |
| 54 | }; // struct TextureVertex |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 55 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 56 | /////////////////////////////////////////////////////////////////////////////// |
| 57 | // Renderer |
| 58 | /////////////////////////////////////////////////////////////////////////////// |
| 59 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame^] | 60 | /** |
| 61 | * OpenGL renderer used to draw accelerated 2D graphics. The API is a |
| 62 | * simplified version of Skia's Canvas API. |
| 63 | */ |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 64 | class OpenGLRenderer { |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 65 | public: |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 66 | OpenGLRenderer(); |
| 67 | ~OpenGLRenderer(); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 68 | |
| 69 | void setViewport(int width, int height); |
| 70 | void prepare(); |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 71 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 72 | int getSaveCount() const; |
| 73 | int save(int flags); |
| 74 | void restore(); |
| 75 | void restoreToCount(int saveCount); |
| 76 | |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 77 | int saveLayer(float left, float top, float right, float bottom, const SkPaint* p, int flags); |
| 78 | int saveLayerAlpha(float left, float top, float right, float bottom, int alpha, int flags); |
| 79 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 80 | void translate(float dx, float dy); |
| 81 | void rotate(float degrees); |
| 82 | void scale(float sx, float sy); |
| 83 | |
| 84 | void setMatrix(SkMatrix* matrix); |
| 85 | void getMatrix(SkMatrix* matrix); |
| 86 | void concatMatrix(SkMatrix* matrix); |
| 87 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 88 | const Rect& getClipBounds(); |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 89 | bool quickReject(float left, float top, float right, float bottom); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 90 | bool clipRect(float left, float top, float right, float bottom); |
| 91 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 92 | void drawColor(int color, SkXfermode::Mode mode); |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 93 | 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] | 94 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 95 | private: |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame^] | 96 | /** |
| 97 | * Saves the current state of the renderer as a new snapshot. |
| 98 | * The new snapshot is saved in mSnapshot and the previous snapshot |
| 99 | * is linked from mSnapshot->previous. |
| 100 | * |
| 101 | * @return The new save count. This value can be passed to #restoreToCount() |
| 102 | */ |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 103 | int saveSnapshot(); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame^] | 104 | |
| 105 | /** |
| 106 | * Restores the current snapshot; mSnapshot becomes mSnapshot->previous. |
| 107 | * |
| 108 | * @return True if the clip should be also reapplied by calling |
| 109 | * #setScissorFromClip(). |
| 110 | */ |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 111 | bool restoreSnapshot(); |
| 112 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame^] | 113 | /** |
| 114 | * Sets the clipping rectangle using glScissor. The clip is defined by |
| 115 | * the current snapshot's clipRect member. |
| 116 | */ |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 117 | void setScissorFromClip(); |
| 118 | |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame^] | 119 | /** |
| 120 | * Draws a colored rectangle with the specified color. The specified coordinates |
| 121 | * are transformed by the current snapshot's transform matrix. |
| 122 | * |
| 123 | * @param left The left coordinate of the rectangle |
| 124 | * @param top The top coordinate of the rectangle |
| 125 | * @param right The right coordinate of the rectangle |
| 126 | * @param bottom The bottom coordinate of the rectangle |
| 127 | * @param color The rectangle's ARGB color, defined as a packed 32 bits word |
| 128 | */ |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 129 | void drawColorRect(float left, float top, float right, float bottom, int color); |
Romain Guy | 5cbbce5 | 2010-06-27 22:59:20 -0700 | [diff] [blame^] | 130 | |
| 131 | /** |
| 132 | * Draws a textured rectangle with the specified texture. The specified coordinates |
| 133 | * are transformed by the current snapshot's transform matrix. |
| 134 | * |
| 135 | * @param left The left coordinate of the rectangle |
| 136 | * @param top The top coordinate of the rectangle |
| 137 | * @param right The right coordinate of the rectangle |
| 138 | * @param bottom The bottom coordinate of the rectangle |
| 139 | * @param texture The texture name to map onto the rectangle |
| 140 | * @param alpha An additional translucency parameter, between 0.0f and 1.0f |
| 141 | */ |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 142 | void drawTextureRect(float left, float top, float right, float bottom, GLuint texture, |
| 143 | float alpha); |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 144 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 145 | // Dimensions of the drawing surface |
| 146 | int mWidth, mHeight; |
| 147 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 148 | // Matrix used for ortho projection in shaders |
| 149 | float mOrthoMatrix[16]; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 150 | |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame] | 151 | // Model-view matrix used to position/size objects |
| 152 | mat4 mModelView; |
| 153 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 154 | // Number of saved states |
| 155 | int mSaveCount; |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 156 | // Base state |
| 157 | Snapshot mFirstSnapshot; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 158 | // Current state |
| 159 | sp<Snapshot> mSnapshot; |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 160 | |
| 161 | // Shaders |
| 162 | sp<DrawColorProgram> mDrawColorShader; |
Romain Guy | bd6b79b | 2010-06-26 00:13:53 -0700 | [diff] [blame] | 163 | sp<DrawTextureProgram> mDrawTextureShader; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 164 | }; // class OpenGLRenderer |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 165 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 166 | }; // namespace uirenderer |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 167 | }; // namespace android |
| 168 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 169 | #endif // ANDROID_UI_OPENGL_RENDERER_H |