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