OpenGLRenderer.h revision 9d5316e3f56d138504565ff311145ac01621dff4
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 } 50 51 enum Flags { 52 kFlagClipSet = 0x1, 53 kFlagDirtyTransform = 0x2, 54 }; 55 56 const Rect& getMappedClip(); 57 58 // Local transformations 59 mat4 transform; 60 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; 69 70private: 71 // Clipping rectangle mapped with the transform 72 Rect mappedClip; 73}; // class Snapshot 74 75struct Vertex { 76 float position[2]; 77 float color[4]; 78}; // struct Vertex 79 80typedef char* shader; 81 82class Program: public LightRefBase<Program> { 83public: 84 Program(const char* vertex, const char* fragment); 85 ~Program(); 86 87 void use(); 88 89protected: 90 int addAttrib(const char* name); 91 int getAttrib(const char* name); 92 93 int addUniform(const char* name); 94 int getUniform(const char* name); 95 96private: 97 GLuint buildShader(const char* source, GLenum type); 98 99 // Handle of the OpenGL program 100 GLuint id; 101 102 // Handles of the shaders 103 GLuint vertexShader; 104 GLuint fragmentShader; 105 106 // Keeps track of attributes and uniforms slots 107 KeyedVector<const char*, int> attributes; 108 KeyedVector<const char*, int> uniforms; 109}; // class Program 110 111class DrawColorProgram: public Program { 112public: 113 DrawColorProgram(); 114 115 int position; 116 int color; 117 118 int projection; 119 int modelView; 120}; 121 122/////////////////////////////////////////////////////////////////////////////// 123// Renderer 124/////////////////////////////////////////////////////////////////////////////// 125 126class OpenGLRenderer { 127public: 128 OpenGLRenderer(); 129 ~OpenGLRenderer(); 130 131 void setViewport(int width, int height); 132 void prepare(); 133 134 int getSaveCount() const; 135 int save(int flags); 136 void restore(); 137 void restoreToCount(int saveCount); 138 139 void translate(float dx, float dy); 140 void rotate(float degrees); 141 void scale(float sx, float sy); 142 143 void setMatrix(SkMatrix* matrix); 144 void getMatrix(SkMatrix* matrix); 145 void concatMatrix(SkMatrix* matrix); 146 147 const Rect& getClipBounds(); 148 bool clipRect(float left, float top, float right, float bottom); 149 150 void drawColor(int color, SkXfermode::Mode mode); 151 152private: 153 int saveSnapshot(); 154 bool restoreSnapshot(); 155 156 void setScissorFromClip(); 157 158 // Dimensions of the drawing surface 159 int mWidth, mHeight; 160 161 // Matrix used for ortho projection in shaders 162 float mOrthoMatrix[16]; 163 164 // Number of saved states 165 int mSaveCount; 166 // Base state 167 Snapshot mFirstSnapshot; 168 // Current state 169 sp<Snapshot> mSnapshot; 170 171 // Shaders 172 sp<DrawColorProgram> mDrawColorShader; 173}; // class OpenGLRenderer 174 175}; // namespace uirenderer 176}; // namespace android 177 178#endif // ANDROID_UI_OPENGL_RENDERER_H 179