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