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 | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 26 | #include <utils/KeyedVector.h> |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 27 | #include <utils/RefBase.h> |
| 28 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 29 | #include "Matrix.h" |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 30 | #include "Rect.h" |
| 31 | |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 32 | namespace android { |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 33 | namespace uirenderer { |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 34 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 35 | /////////////////////////////////////////////////////////////////////////////// |
| 36 | // Support |
| 37 | /////////////////////////////////////////////////////////////////////////////// |
| 38 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 39 | class Snapshot: public LightRefBase<Snapshot> { |
| 40 | public: |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 41 | Snapshot() { |
| 42 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 43 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 44 | Snapshot(const sp<Snapshot> s): |
| 45 | transform(s->transform), |
| 46 | clipRect(s->clipRect), |
| 47 | flags(kFlagDirtyTransform), |
| 48 | previous(s) { |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 49 | } |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 50 | |
| 51 | enum Flags { |
| 52 | kFlagClipSet = 0x1, |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 53 | kFlagDirtyTransform = 0x2, |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 54 | }; |
| 55 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 56 | const Rect& getMappedClip(); |
| 57 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 58 | // Local transformations |
| 59 | mat4 transform; |
| 60 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 61 | // Clipping rectangle at the time of this snapshot |
| 62 | Rect clipRect; |
| 63 | |
| 64 | // Dirty flags |
| 65 | int flags; |
| 66 | |
| 67 | // Previous snapshot in the frames stack |
| 68 | sp<Snapshot> previous; |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 69 | |
| 70 | private: |
| 71 | // Clipping rectangle mapped with the transform |
| 72 | Rect mappedClip; |
| 73 | }; // class Snapshot |
| 74 | |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame^] | 75 | struct SimpleVertex { |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 76 | float position[2]; |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame^] | 77 | }; // struct SimpleVertex |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 78 | |
| 79 | typedef char* shader; |
| 80 | |
| 81 | class Program: public LightRefBase<Program> { |
| 82 | public: |
| 83 | Program(const char* vertex, const char* fragment); |
| 84 | ~Program(); |
| 85 | |
| 86 | void use(); |
| 87 | |
| 88 | protected: |
| 89 | int addAttrib(const char* name); |
| 90 | int getAttrib(const char* name); |
| 91 | |
| 92 | int addUniform(const char* name); |
| 93 | int getUniform(const char* name); |
| 94 | |
| 95 | private: |
| 96 | GLuint buildShader(const char* source, GLenum type); |
| 97 | |
| 98 | // Handle of the OpenGL program |
| 99 | GLuint id; |
| 100 | |
| 101 | // Handles of the shaders |
| 102 | GLuint vertexShader; |
| 103 | GLuint fragmentShader; |
| 104 | |
| 105 | // Keeps track of attributes and uniforms slots |
| 106 | KeyedVector<const char*, int> attributes; |
| 107 | KeyedVector<const char*, int> uniforms; |
| 108 | }; // class Program |
| 109 | |
| 110 | class DrawColorProgram: public Program { |
| 111 | public: |
| 112 | DrawColorProgram(); |
| 113 | |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame^] | 114 | void use(const GLfloat* projectionMatrix, const GLfloat* modelViewMatrix, |
| 115 | const GLfloat* transformMatrix); |
| 116 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 117 | int position; |
| 118 | int color; |
| 119 | |
| 120 | int projection; |
| 121 | int modelView; |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame^] | 122 | int transform; |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 123 | }; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 124 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 125 | /////////////////////////////////////////////////////////////////////////////// |
| 126 | // Renderer |
| 127 | /////////////////////////////////////////////////////////////////////////////// |
| 128 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 129 | class OpenGLRenderer { |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 130 | public: |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 131 | OpenGLRenderer(); |
| 132 | ~OpenGLRenderer(); |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 133 | |
| 134 | void setViewport(int width, int height); |
| 135 | void prepare(); |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 136 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 137 | int getSaveCount() const; |
| 138 | int save(int flags); |
| 139 | void restore(); |
| 140 | void restoreToCount(int saveCount); |
| 141 | |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 142 | void translate(float dx, float dy); |
| 143 | void rotate(float degrees); |
| 144 | void scale(float sx, float sy); |
| 145 | |
| 146 | void setMatrix(SkMatrix* matrix); |
| 147 | void getMatrix(SkMatrix* matrix); |
| 148 | void concatMatrix(SkMatrix* matrix); |
| 149 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 150 | const Rect& getClipBounds(); |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame^] | 151 | bool quickReject(float left, float top, float right, float bottom); |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 152 | bool clipRect(float left, float top, float right, float bottom); |
| 153 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 154 | void drawColor(int color, SkXfermode::Mode mode); |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame^] | 155 | void drawRect(float left, float top, float right, float bottom, SkPaint* paint); |
Romain Guy | 08ae317 | 2010-06-21 19:35:50 -0700 | [diff] [blame] | 156 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 157 | private: |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 158 | int saveSnapshot(); |
| 159 | bool restoreSnapshot(); |
| 160 | |
| 161 | void setScissorFromClip(); |
| 162 | |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame^] | 163 | void drawColorRect(float left, float top, float right, float bottom, int color); |
| 164 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 165 | // Dimensions of the drawing surface |
| 166 | int mWidth, mHeight; |
| 167 | |
Romain Guy | 85bf02f | 2010-06-22 13:11:24 -0700 | [diff] [blame] | 168 | // Matrix used for ortho projection in shaders |
| 169 | float mOrthoMatrix[16]; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 170 | |
Romain Guy | c7d5349 | 2010-06-25 13:41:57 -0700 | [diff] [blame^] | 171 | // Model-view matrix used to position/size objects |
| 172 | mat4 mModelView; |
| 173 | |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 174 | // Number of saved states |
| 175 | int mSaveCount; |
Romain Guy | f6a11b8 | 2010-06-23 17:47:49 -0700 | [diff] [blame] | 176 | // Base state |
| 177 | Snapshot mFirstSnapshot; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 178 | // Current state |
| 179 | sp<Snapshot> mSnapshot; |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 180 | |
| 181 | // Shaders |
| 182 | sp<DrawColorProgram> mDrawColorShader; |
Romain Guy | bb9524b | 2010-06-22 18:56:38 -0700 | [diff] [blame] | 183 | }; // class OpenGLRenderer |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 184 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 185 | }; // namespace uirenderer |
Romain Guy | e4d0112 | 2010-06-16 18:44:05 -0700 | [diff] [blame] | 186 | }; // namespace android |
| 187 | |
Romain Guy | 9d5316e | 2010-06-24 19:30:36 -0700 | [diff] [blame] | 188 | #endif // ANDROID_UI_OPENGL_RENDERER_H |