OpenGLRenderer.h revision 5b3b35296e8b2c8d3f07d32bb645d5414db41a1d
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_HWUI_OPENGL_RENDERER_H 18#define ANDROID_HWUI_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/Vector.h> 32 33#include "Extensions.h" 34#include "Matrix.h" 35#include "Program.h" 36#include "Rect.h" 37#include "Snapshot.h" 38#include "Vertex.h" 39#include "SkiaShader.h" 40#include "SkiaColorFilter.h" 41#include "Caches.h" 42 43namespace android { 44namespace uirenderer { 45 46/////////////////////////////////////////////////////////////////////////////// 47// Defines 48/////////////////////////////////////////////////////////////////////////////// 49 50// Debug 51#define DEBUG_OPENGL 1 52 53// If turned on, layers drawn inside FBOs are optimized with regions 54#define RENDER_LAYERS_AS_REGIONS 0 55#define DEBUG_LAYERS_AS_REGIONS 0 56 57/////////////////////////////////////////////////////////////////////////////// 58// Renderer 59/////////////////////////////////////////////////////////////////////////////// 60 61class DisplayListRenderer; 62 63/** 64 * OpenGL renderer used to draw accelerated 2D graphics. The API is a 65 * simplified version of Skia's Canvas API. 66 */ 67class OpenGLRenderer { 68public: 69 OpenGLRenderer(); 70 virtual ~OpenGLRenderer(); 71 72 virtual void setViewport(int width, int height); 73 74 virtual void prepare(bool opaque); 75 virtual void finish(); 76 77 virtual void acquireContext(); 78 virtual void releaseContext(); 79 80 int getSaveCount() const; 81 virtual int save(int flags); 82 virtual void restore(); 83 virtual void restoreToCount(int saveCount); 84 85 virtual int saveLayer(float left, float top, float right, float bottom, 86 SkPaint* p, int flags); 87 virtual int saveLayerAlpha(float left, float top, float right, float bottom, 88 int alpha, int flags); 89 90 virtual void translate(float dx, float dy); 91 virtual void rotate(float degrees); 92 virtual void scale(float sx, float sy); 93 94 const float* getMatrix() const; 95 void getMatrix(SkMatrix* matrix); 96 virtual void setMatrix(SkMatrix* matrix); 97 virtual void concatMatrix(SkMatrix* matrix); 98 99 const Rect& getClipBounds(); 100 bool quickReject(float left, float top, float right, float bottom); 101 virtual bool clipRect(float left, float top, float right, float bottom, SkRegion::Op op); 102 103 virtual void drawBitmap(SkBitmap* bitmap, float left, float top, SkPaint* paint); 104 virtual void drawBitmap(SkBitmap* bitmap, SkMatrix* matrix, SkPaint* paint); 105 virtual void drawBitmap(SkBitmap* bitmap, float srcLeft, float srcTop, 106 float srcRight, float srcBottom, float dstLeft, float dstTop, 107 float dstRight, float dstBottom, SkPaint* paint); 108 virtual void drawPatch(SkBitmap* bitmap, const int32_t* xDivs, const int32_t* yDivs, 109 const uint32_t* colors, uint32_t width, uint32_t height, int8_t numColors, 110 float left, float top, float right, float bottom, SkPaint* paint); 111 virtual void drawColor(int color, SkXfermode::Mode mode); 112 virtual void drawRect(float left, float top, float right, float bottom, SkPaint* paint); 113 virtual void drawPath(SkPath* path, SkPaint* paint); 114 virtual void drawLines(float* points, int count, SkPaint* paint); 115 virtual void drawText(const char* text, int bytesCount, int count, float x, float y, 116 SkPaint* paint); 117 118 virtual void resetShader(); 119 virtual void setupShader(SkiaShader* shader); 120 121 virtual void resetColorFilter(); 122 virtual void setupColorFilter(SkiaColorFilter* filter); 123 124 virtual void resetShadow(); 125 virtual void setupShadow(float radius, float dx, float dy, int color); 126 127protected: 128 /** 129 * Compose the layer defined in the current snapshot with the layer 130 * defined by the previous snapshot. 131 * 132 * The current snapshot *must* be a layer (flag kFlagIsLayer set.) 133 * 134 * @param curent The current snapshot containing the layer to compose 135 * @param previous The previous snapshot to compose the current layer with 136 */ 137 virtual void composeLayer(sp<Snapshot> current, sp<Snapshot> previous); 138 139private: 140 /** 141 * Saves the current state of the renderer as a new snapshot. 142 * The new snapshot is saved in mSnapshot and the previous snapshot 143 * is linked from mSnapshot->previous. 144 * 145 * @param flags The save flags; see SkCanvas for more information 146 * 147 * @return The new save count. This value can be passed to #restoreToCount() 148 */ 149 int saveSnapshot(int flags); 150 151 /** 152 * Restores the current snapshot; mSnapshot becomes mSnapshot->previous. 153 * 154 * @return True if the clip was modified. 155 */ 156 bool restoreSnapshot(); 157 158 /** 159 * Sets the clipping rectangle using glScissor. The clip is defined by 160 * the current snapshot's clipRect member. 161 */ 162 void setScissorFromClip(); 163 164 /** 165 * Creates a new layer stored in the specified snapshot. 166 * 167 * @param snapshot The snapshot associated with the new layer 168 * @param left The left coordinate of the layer 169 * @param top The top coordinate of the layer 170 * @param right The right coordinate of the layer 171 * @param bottom The bottom coordinate of the layer 172 * @param alpha The translucency of the layer 173 * @param mode The blending mode of the layer 174 * @param flags The layer save flags 175 * @param previousFbo The name of the current framebuffer 176 * 177 * @return True if the layer was successfully created, false otherwise 178 */ 179 bool createLayer(sp<Snapshot> snapshot, float left, float top, float right, float bottom, 180 int alpha, SkXfermode::Mode mode, int flags, GLuint previousFbo); 181 182 /** 183 * Creates a new layer stored in the specified snapshot as an FBO. 184 * 185 * @param layer The layer to store as an FBO 186 * @param snapshot The snapshot associated with the new layer 187 * @param bounds The bounds of the layer 188 * @param previousFbo The name of the current framebuffer 189 */ 190 bool createFboLayer(Layer* layer, Rect& bounds, sp<Snapshot> snapshot, 191 GLuint previousFbo); 192 193 /** 194 * Compose the specified layer as a region. 195 * 196 * @param layer The layer to compose 197 * @param rect The layer's bounds 198 */ 199 void composeLayerRegion(Layer* layer, const Rect& rect); 200 201 /** 202 * Compose the specified layer as a simple rectangle. 203 * 204 * @param layer The layer to compose 205 * @param rect The layer's bounds 206 * @param swap If true, the source and destination are swapped 207 */ 208 void composeLayerRect(Layer* layer, const Rect& rect, bool swap = false); 209 210 /** 211 * Clears all the regions corresponding to the current list of layers. 212 * This method MUST be invoked before any drawing operation. 213 */ 214 void clearLayerRegions(); 215 216 /** 217 * Draws a colored rectangle with the specified color. The specified coordinates 218 * are transformed by the current snapshot's transform matrix. 219 * 220 * @param left The left coordinate of the rectangle 221 * @param top The top coordinate of the rectangle 222 * @param right The right coordinate of the rectangle 223 * @param bottom The bottom coordinate of the rectangle 224 * @param color The rectangle's ARGB color, defined as a packed 32 bits word 225 * @param mode The Skia xfermode to use 226 * @param ignoreTransform True if the current transform should be ignored 227 * @param ignoreBlending True if the blending is set by the caller 228 */ 229 void drawColorRect(float left, float top, float right, float bottom, 230 int color, SkXfermode::Mode mode, bool ignoreTransform = false); 231 232 /** 233 * Setups shaders to draw a colored rect. 234 */ 235 void setupColorRect(float left, float top, float right, float bottom, 236 float r, float g, float b, float a, SkXfermode::Mode mode, bool ignoreTransform); 237 238 /** 239 * Draws a textured rectangle with the specified texture. The specified coordinates 240 * are transformed by the current snapshot's transform matrix. 241 * 242 * @param left The left coordinate of the rectangle 243 * @param top The top coordinate of the rectangle 244 * @param right The right coordinate of the rectangle 245 * @param bottom The bottom coordinate of the rectangle 246 * @param texture The texture name to map onto the rectangle 247 * @param alpha An additional translucency parameter, between 0.0f and 1.0f 248 * @param mode The blending mode 249 * @param blend True if the texture contains an alpha channel 250 */ 251 void drawTextureRect(float left, float top, float right, float bottom, GLuint texture, 252 float alpha, SkXfermode::Mode mode, bool blend); 253 254 /** 255 * Draws a textured rectangle with the specified texture. The specified coordinates 256 * are transformed by the current snapshot's transform matrix. 257 * 258 * @param left The left coordinate of the rectangle 259 * @param top The top coordinate of the rectangle 260 * @param right The right coordinate of the rectangle 261 * @param bottom The bottom coordinate of the rectangle 262 * @param texture The texture to use 263 * @param paint The paint containing the alpha, blending mode, etc. 264 */ 265 void drawTextureRect(float left, float top, float right, float bottom, 266 Texture* texture, SkPaint* paint); 267 268 /** 269 * Draws a textured mesh with the specified texture. If the indices are omitted, 270 * the mesh is drawn as a simple quad. The mesh pointers become offsets when a 271 * VBO is bound. 272 * 273 * @param left The left coordinate of the rectangle 274 * @param top The top coordinate of the rectangle 275 * @param right The right coordinate of the rectangle 276 * @param bottom The bottom coordinate of the rectangle 277 * @param texture The texture name to map onto the rectangle 278 * @param alpha An additional translucency parameter, between 0.0f and 1.0f 279 * @param mode The blending mode 280 * @param blend True if the texture contains an alpha channel 281 * @param vertices The vertices that define the mesh 282 * @param texCoords The texture coordinates of each vertex 283 * @param elementsCount The number of elements in the mesh, required by indices 284 * @param swapSrcDst Whether or not the src and dst blending operations should be swapped 285 * @param ignoreTransform True if the current transform should be ignored 286 * @param vbo The VBO used to draw the mesh 287 * @param ignoreScale True if the model view matrix should not be scaled 288 * @param dirty True if calling this method should dirty the current layer 289 */ 290 void drawTextureMesh(float left, float top, float right, float bottom, GLuint texture, 291 float alpha, SkXfermode::Mode mode, bool blend, 292 GLvoid* vertices, GLvoid* texCoords, GLenum drawMode, GLsizei elementsCount, 293 bool swapSrcDst = false, bool ignoreTransform = false, GLuint vbo = 0, 294 bool ignoreScale = false, bool dirty = true); 295 296 /** 297 * Prepares the renderer to draw the specified shadow. The active texture 298 * unit must be 0 and the other units must be unbound. 299 * 300 * @param texture The shadow texture 301 * @param x The x coordinate of the shadow 302 * @param y The y coordinate of the shadow 303 * @param mode The blending mode 304 * @param alpha The alpha value 305 */ 306 void setupShadow(const ShadowTexture* texture, float x, float y, SkXfermode::Mode mode, 307 float alpha); 308 309 /** 310 * Prepares the renderer to draw the specified Alpha8 texture as a rectangle. 311 * 312 * @param texture The texture to render with 313 * @param textureUnit The texture unit to use, may be modified 314 * @param x The x coordinate of the rectangle to draw 315 * @param y The y coordinate of the rectangle to draw 316 * @param r The red component of the color 317 * @param g The green component of the color 318 * @param b The blue component of the color 319 * @param a The alpha component of the color 320 * @param mode The blending mode 321 * @param transforms True if the matrix passed to the shader should be multiplied 322 * by the model-view matrix 323 * @param applyFilters Whether or not to take color filters and 324 * shaders into account 325 */ 326 void setupTextureAlpha8(const Texture* texture, GLuint& textureUnit, float x, float y, 327 float r, float g, float b, float a, SkXfermode::Mode mode, bool transforms, 328 bool applyFilters); 329 330 /** 331 * Prepares the renderer to draw the specified Alpha8 texture as a rectangle. 332 * 333 * @param texture The texture to render with 334 * @param width The width of the texture 335 * @param height The height of the texture 336 * @param textureUnit The texture unit to use, may be modified 337 * @param x The x coordinate of the rectangle to draw 338 * @param y The y coordinate of the rectangle to draw 339 * @param r The red component of the color 340 * @param g The green component of the color 341 * @param b The blue component of the color 342 * @param a The alpha component of the color 343 * @param mode The blending mode 344 * @param transforms True if the matrix passed to the shader should be multiplied 345 * by the model-view matrix 346 * @param applyFilters Whether or not to take color filters and 347 * shaders into account 348 */ 349 void setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height, 350 GLuint& textureUnit, float x, float y, float r, float g, float b, float a, 351 SkXfermode::Mode mode, bool transforms, bool applyFilters); 352 353 /** 354 * Same as above setupTextureAlpha8() but specifies the mesh's vertices 355 * and texCoords pointers. The pointers become offsets when a VBO is bound. 356 */ 357 void setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height, 358 GLuint& textureUnit, float x, float y, float r, float g, float b, float a, 359 SkXfermode::Mode mode, bool transforms, bool applyFilters, 360 GLvoid* vertices, GLvoid* texCoords, GLuint vbo = 0); 361 362 /** 363 * Draws text underline and strike-through if needed. 364 * 365 * @param text The text to decor 366 * @param bytesCount The number of bytes in the text 367 * @param length The length in pixels of the text, can be <= 0.0f to force a measurement 368 * @param x The x coordinate where the text will be drawn 369 * @param y The y coordinate where the text will be drawn 370 * @param paint The paint to draw the text with 371 */ 372 void drawTextDecorations(const char* text, int bytesCount, float length, 373 float x, float y, SkPaint* paint); 374 375 /** 376 * Resets the texture coordinates stored in mMeshVertices. Setting the values 377 * back to default is achieved by calling: 378 * 379 * resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); 380 * 381 * @param u1 The left coordinate of the texture 382 * @param v1 The bottom coordinate of the texture 383 * @param u2 The right coordinate of the texture 384 * @param v2 The top coordinate of the texture 385 */ 386 void resetDrawTextureTexCoords(float u1, float v1, float u2, float v2); 387 388 /** 389 * Gets the alpha and xfermode out of a paint object. If the paint is null 390 * alpha will be 255 and the xfermode will be SRC_OVER. 391 * 392 * @param paint The paint to extract values from 393 * @param alpha Where to store the resulting alpha 394 * @param mode Where to store the resulting xfermode 395 */ 396 inline void getAlphaAndMode(SkPaint* paint, int* alpha, SkXfermode::Mode* mode); 397 398 /** 399 * Binds the specified texture. The texture unit must have been selected 400 * prior to calling this method. 401 */ 402 inline void bindTexture(GLuint texture) { 403 glBindTexture(GL_TEXTURE_2D, texture); 404 } 405 406 /** 407 * Sets the wrap modes for the specified texture. The wrap modes are modified 408 * only when needed. 409 */ 410 inline void setTextureWrapModes(Texture* texture, GLenum wrapS, GLenum wrapT); 411 412 /** 413 * Enable or disable blending as necessary. This function sets the appropriate 414 * blend function based on the specified xfermode. 415 */ 416 inline void chooseBlending(bool blend, SkXfermode::Mode mode, ProgramDescription& description, 417 bool swapSrcDst = false); 418 419 /** 420 * Safely retrieves the mode from the specified xfermode. If the specified 421 * xfermode is null, the mode is assumed to be SkXfermode::kSrcOver_Mode. 422 */ 423 inline SkXfermode::Mode getXfermode(SkXfermode* mode); 424 425 /** 426 * Use the specified program with the current GL context. If the program is already 427 * in use, it will not be bound again. If it is not in use, the current program is 428 * marked unused and the specified program becomes used and becomes the new 429 * current program. 430 * 431 * @param program The program to use 432 * 433 * @return true If the specified program was already in use, false otherwise. 434 */ 435 inline bool useProgram(Program* program); 436 437 /** 438 * Invoked before any drawing operation. This sets required state. 439 */ 440 void setupDraw(); 441 442 /** 443 * Should be invoked every time the glScissor is modified. 444 */ 445 inline void dirtyClip() { 446 mDirtyClip = true; 447 } 448 449 /** 450 * Mark the layer as dirty at the specified coordinates. The coordinates 451 * are transformed with the supplied matrix. 452 */ 453 void dirtyLayer(const float left, const float top, const float right, const float bottom, 454 const mat4 transform); 455 456 /** 457 * Mark the layer as dirty at the specified coordinates. 458 */ 459 void dirtyLayer(const float left, const float top, const float right, const float bottom); 460 461 // Dimensions of the drawing surface 462 int mWidth, mHeight; 463 464 // Matrix used for ortho projection in shaders 465 mat4 mOrthoMatrix; 466 467 // Model-view matrix used to position/size objects 468 mat4 mModelView; 469 470 // Number of saved states 471 int mSaveCount; 472 // Base state 473 sp<Snapshot> mFirstSnapshot; 474 // Current state 475 sp<Snapshot> mSnapshot; 476 477 // Shaders 478 SkiaShader* mShader; 479 480 // Color filters 481 SkiaColorFilter* mColorFilter; 482 483 // Used to draw textured quads 484 TextureVertex mMeshVertices[4]; 485 486 // Drop shadow 487 bool mHasShadow; 488 float mShadowRadius; 489 float mShadowDx; 490 float mShadowDy; 491 int mShadowColor; 492 493 // Various caches 494 Caches& mCaches; 495 496 // List of rectangles to clear due to calls to saveLayer() 497 Vector<Rect*> mLayers; 498 499 // Indentity matrix 500 const mat4 mIdentity; 501 502 // Indicates whether the clip must be restored 503 bool mDirtyClip; 504 505 friend class DisplayListRenderer; 506 507}; // class OpenGLRenderer 508 509}; // namespace uirenderer 510}; // namespace android 511 512#endif // ANDROID_HWUI_OPENGL_RENDERER_H 513