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