OpenGLRenderer.h revision 1e79386ba34f0db38c1b35b22cdf122632534354
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 <SkBitmap.h> 24#include <SkMatrix.h> 25#include <SkPaint.h> 26#include <SkRegion.h> 27#include <SkShader.h> 28#include <SkXfermode.h> 29 30#include <utils/RefBase.h> 31#include <utils/ResourceTypes.h> 32 33#include "Matrix.h" 34#include "Program.h" 35#include "Rect.h" 36#include "Snapshot.h" 37#include "TextureCache.h" 38#include "LayerCache.h" 39#include "PatchCache.h" 40#include "Vertex.h" 41 42namespace android { 43namespace uirenderer { 44 45/////////////////////////////////////////////////////////////////////////////// 46// Support 47/////////////////////////////////////////////////////////////////////////////// 48 49/** 50 * Structure mapping Skia xfermodes to OpenGL blending factors. 51 */ 52struct Blender { 53 SkXfermode::Mode mode; 54 GLenum src; 55 GLenum dst; 56}; // struct Blender 57 58/////////////////////////////////////////////////////////////////////////////// 59// Renderer 60/////////////////////////////////////////////////////////////////////////////// 61 62/** 63 * OpenGL renderer used to draw accelerated 2D graphics. The API is a 64 * simplified version of Skia's Canvas API. 65 */ 66class OpenGLRenderer { 67public: 68 OpenGLRenderer(); 69 ~OpenGLRenderer(); 70 71 void setViewport(int width, int height); 72 void prepare(); 73 74 int getSaveCount() const; 75 int save(int flags); 76 void restore(); 77 void restoreToCount(int saveCount); 78 79 int saveLayer(float left, float top, float right, float bottom, const SkPaint* p, int flags); 80 int saveLayerAlpha(float left, float top, float right, float bottom, int alpha, int flags); 81 82 void translate(float dx, float dy); 83 void rotate(float degrees); 84 void scale(float sx, float sy); 85 86 void setMatrix(SkMatrix* matrix); 87 void getMatrix(SkMatrix* matrix); 88 void concatMatrix(SkMatrix* matrix); 89 90 const Rect& getClipBounds(); 91 bool quickReject(float left, float top, float right, float bottom); 92 bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op); 93 94 void drawBitmap(SkBitmap* bitmap, float left, float top, const SkPaint* paint); 95 void drawBitmap(SkBitmap* bitmap, const SkMatrix* matrix, const SkPaint* paint); 96 void drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop, float srcRight, float srcBottom, 97 float dstLeft, float dstTop, float dstRight, float dstBottom, const SkPaint* paint); 98 void drawPatch(SkBitmap* bitmap, Res_png_9patch* patch, float left, float top, 99 float right, float bottom, const SkPaint* paint); 100 void drawColor(int color, SkXfermode::Mode mode); 101 void drawRect(float left, float top, float right, float bottom, const SkPaint* paint); 102 103 void resetShader(); 104 void setupBitmapShader(SkBitmap* bitmap, SkShader::TileMode tileX, SkShader::TileMode tileY, 105 SkMatrix* matrix, bool hasAlpha); 106 107private: 108 /** 109 * Type of Skia shader in use. 110 */ 111 enum ShaderType { 112 kShaderNone, 113 kShaderBitmap, 114 kShaderLinearGradient, 115 kShaderCircularGradient, 116 kShaderSweepGradient 117 }; 118 119 /** 120 * Saves the current state of the renderer as a new snapshot. 121 * The new snapshot is saved in mSnapshot and the previous snapshot 122 * is linked from mSnapshot->previous. 123 * 124 * @return The new save count. This value can be passed to #restoreToCount() 125 */ 126 int saveSnapshot(); 127 128 /** 129 * Restores the current snapshot; mSnapshot becomes mSnapshot->previous. 130 * 131 * @return True if the clip should be also reapplied by calling 132 * #setScissorFromClip(). 133 */ 134 bool restoreSnapshot(); 135 136 /** 137 * Sets the clipping rectangle using glScissor. The clip is defined by 138 * the current snapshot's clipRect member. 139 */ 140 void setScissorFromClip(); 141 142 /** 143 * Compose the layer defined in the current snapshot with the layer 144 * defined by the previous snapshot. 145 * 146 * The current snapshot *must* be a layer (flag kFlagIsLayer set.) 147 * 148 * @param curent The current snapshot containing the layer to compose 149 * @param previous The previous snapshot to compose the current layer with 150 */ 151 void composeLayer(sp<Snapshot> current, sp<Snapshot> previous); 152 153 /** 154 * Creates a new layer stored in the specified snapshot. 155 * 156 * @param snapshot The snapshot associated with the new layer 157 * @param left The left coordinate of the layer 158 * @param top The top coordinate of the layer 159 * @param right The right coordinate of the layer 160 * @param bottom The bottom coordinate of the layer 161 * @param alpha The translucency of the layer 162 * @param mode The blending mode of the layer 163 * @param flags The layer save flags 164 * 165 * @return True if the layer was successfully created, false otherwise 166 */ 167 bool createLayer(sp<Snapshot> snapshot, float left, float top, float right, float bottom, 168 int alpha, SkXfermode::Mode mode, int flags); 169 170 /** 171 * Draws a colored rectangle with the specified color. The specified coordinates 172 * are transformed by the current snapshot's transform matrix. 173 * 174 * @param left The left coordinate of the rectangle 175 * @param top The top coordinate of the rectangle 176 * @param right The right coordinate of the rectangle 177 * @param bottom The bottom coordinate of the rectangle 178 * @param color The rectangle's ARGB color, defined as a packed 32 bits word 179 * @param mode The Skia xfermode to use 180 * @param ignoreTransform True if the current transform should be ignored 181 */ 182 void drawColorRect(float left, float top, float right, float bottom, 183 int color, SkXfermode::Mode mode, bool ignoreTransform = false); 184 185 /** 186 * Draws a textured rectangle with the specified texture. The specified coordinates 187 * are transformed by the current snapshot's transform matrix. 188 * 189 * @param left The left coordinate of the rectangle 190 * @param top The top coordinate of the rectangle 191 * @param right The right coordinate of the rectangle 192 * @param bottom The bottom coordinate of the rectangle 193 * @param texture The texture name to map onto the rectangle 194 * @param alpha An additional translucency parameter, between 0.0f and 1.0f 195 * @param mode The blending mode 196 * @param blend True if the texture contains an alpha channel 197 */ 198 void drawTextureRect(float left, float top, float right, float bottom, GLuint texture, 199 float alpha, SkXfermode::Mode mode, bool blend); 200 201 /** 202 * Draws a textured rectangle with the specified texture. The specified coordinates 203 * are transformed by the current snapshot's transform matrix. 204 * 205 * @param left The left coordinate of the rectangle 206 * @param top The top coordinate of the rectangle 207 * @param right The right coordinate of the rectangle 208 * @param bottom The bottom coordinate of the rectangle 209 * @param texture The texture to use 210 * @param paint The paint containing the alpha, blending mode, etc. 211 */ 212 void drawTextureRect(float left, float top, float right, float bottom, 213 const Texture* texture, const SkPaint* paint); 214 215 /** 216 * Draws a textured mesh with the specified texture. If the indices are omitted, the 217 * mesh is drawn as a simple quad. 218 * 219 * @param left The left coordinate of the rectangle 220 * @param top The top coordinate of the rectangle 221 * @param right The right coordinate of the rectangle 222 * @param bottom The bottom coordinate of the rectangle 223 * @param texture The texture name to map onto the rectangle 224 * @param alpha An additional translucency parameter, between 0.0f and 1.0f 225 * @param mode The blending mode 226 * @param blend True if the texture contains an alpha channel 227 * @param vertices The vertices that define the mesh 228 * @param texCoords The texture coordinates of each vertex 229 * @param indices The indices of the vertices, can be NULL 230 * @param elementsCount The number of elements in the mesh, required by indices 231 */ 232 void drawTextureMesh(float left, float top, float right, float bottom, GLuint texture, 233 float alpha, SkXfermode::Mode mode, bool blend, 234 GLvoid* vertices, GLvoid* texCoords, GLvoid* indices, GLsizei elementsCount = 0); 235 236 /** 237 * Fills the specified rectangle with the currently set bitmap shader. 238 * 239 * @param left The left coordinate of the rectangle 240 * @param top The top coordinate of the rectangle 241 * @param right The right coordinate of the rectangle 242 * @param bottom The bottom coordinate of the rectangle 243 * @param alpha An additional translucency parameter, between 0.0f and 1.0f 244 * @param mode The blending mode 245 */ 246 void drawBitmapShader(float left, float top, float right, float bottom, float alpha, 247 SkXfermode::Mode mode); 248 249 /** 250 * Resets the texture coordinates stored in mDrawTextureVertices. Setting the values 251 * back to default is achieved by calling: 252 * 253 * resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); 254 * 255 * @param u1 The left coordinate of the texture 256 * @param v1 The bottom coordinate of the texture 257 * @param u2 The right coordinate of the texture 258 * @param v2 The top coordinate of the texture 259 */ 260 void resetDrawTextureTexCoords(float u1, float v1, float u2, float v2); 261 262 /** 263 * Gets the alpha and xfermode out of a paint object. If the paint is null 264 * alpha will be 255 and the xfermode will be SRC_OVER. 265 * 266 * @param paint The paint to extract values from 267 * @param alpha Where to store the resulting alpha 268 * @param mode Where to store the resulting xfermode 269 */ 270 inline void getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode); 271 272 /** 273 * Enable or disable blending as necessary. This function sets the appropriate 274 * blend function based on the specified xfermode. 275 */ 276 inline void chooseBlending(bool blend, SkXfermode::Mode mode, bool isPremultiplied = true); 277 278 /** 279 * Use the specified program with the current GL context. If the program is already 280 * in use, it will not be bound again. If it is not in use, the current program is 281 * marked unused and the specified program becomes used and becomes the new 282 * current program. 283 * 284 * @param program The program to use 285 * 286 * @return true If the specified program was already in use, false otherwise. 287 */ 288 inline bool useProgram(const sp<Program>& program); 289 290 // Dimensions of the drawing surface 291 int mWidth, mHeight; 292 293 // Matrix used for ortho projection in shaders 294 mat4 mOrthoMatrix; 295 296 // Model-view matrix used to position/size objects 297 mat4 mModelView; 298 299 // Number of saved states 300 int mSaveCount; 301 // Base state 302 Snapshot mFirstSnapshot; 303 // Current state 304 sp<Snapshot> mSnapshot; 305 306 // Shaders 307 sp<Program> mCurrentProgram; 308 sp<DrawColorProgram> mDrawColorProgram; 309 sp<DrawTextureProgram> mDrawTextureProgram; 310 311 // Used to draw textured quads 312 TextureVertex mDrawTextureVertices[4]; 313 314 // Current texture state 315 GLuint mLastTexture; 316 317 // Last known blend state 318 bool mBlend; 319 GLenum mLastSrcMode; 320 GLenum mLastDstMode; 321 322 // Skia shader 323 ShaderType mShader; 324 bool mShaderBlend; 325 SkBitmap* mShaderBitmap; 326 SkShader::TileMode mShaderTileX; 327 SkShader::TileMode mShaderTileY; 328 SkMatrix* mShaderMatrix; 329 330 // Various caches 331 TextureCache mTextureCache; 332 LayerCache mLayerCache; 333 PatchCache mPatchCache; 334}; // class OpenGLRenderer 335 336}; // namespace uirenderer 337}; // namespace android 338 339#endif // ANDROID_UI_OPENGL_RENDERER_H 340