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