OpenGLRenderer.h revision 759ea80dca64ad652110a129e0d8bf93fea79f61
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#include <utils/Vector.h> 33 34#include "Extensions.h" 35#include "Matrix.h" 36#include "Program.h" 37#include "Rect.h" 38#include "Snapshot.h" 39#include "Vertex.h" 40#include "SkiaShader.h" 41#include "SkiaColorFilter.h" 42#include "Caches.h" 43#include "Line.h" 44 45namespace android { 46namespace uirenderer { 47 48/////////////////////////////////////////////////////////////////////////////// 49// Defines 50/////////////////////////////////////////////////////////////////////////////// 51 52// Debug 53#define DEBUG_OPENGL 1 54 55/////////////////////////////////////////////////////////////////////////////// 56// Renderer 57/////////////////////////////////////////////////////////////////////////////// 58 59/** 60 * OpenGL renderer used to draw accelerated 2D graphics. The API is a 61 * simplified version of Skia's Canvas API. 62 */ 63class OpenGLRenderer { 64public: 65 OpenGLRenderer(); 66 ~OpenGLRenderer(); 67 68 void setViewport(int width, int height); 69 void prepare(); 70 void finish(); 71 void acquireContext(); 72 void releaseContext(); 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 void drawPath(SkPath* path, SkPaint* paint); 103 void drawLines(float* points, int count, const SkPaint* paint); 104 105 void resetShader(); 106 void setupShader(SkiaShader* shader); 107 108 void resetColorFilter(); 109 void setupColorFilter(SkiaColorFilter* filter); 110 111 void resetShadow(); 112 void setupShadow(float radius, float dx, float dy, int color); 113 114 void drawText(const char* text, int bytesCount, int count, float x, float y, SkPaint* paint); 115 116private: 117 /** 118 * Saves the current state of the renderer as a new snapshot. 119 * The new snapshot is saved in mSnapshot and the previous snapshot 120 * is linked from mSnapshot->previous. 121 * 122 * @param flags The save flags; see SkCanvas for more information 123 * 124 * @return The new save count. This value can be passed to #restoreToCount() 125 */ 126 int saveSnapshot(int flags); 127 128 /** 129 * Restores the current snapshot; mSnapshot becomes mSnapshot->previous. 130 * 131 * @return True if the clip was modified. 132 */ 133 bool restoreSnapshot(); 134 135 /** 136 * Sets the clipping rectangle using glScissor. The clip is defined by 137 * the current snapshot's clipRect member. 138 */ 139 void setScissorFromClip(); 140 141 /** 142 * Compose the layer defined in the current snapshot with the layer 143 * defined by the previous snapshot. 144 * 145 * The current snapshot *must* be a layer (flag kFlagIsLayer set.) 146 * 147 * @param curent The current snapshot containing the layer to compose 148 * @param previous The previous snapshot to compose the current layer with 149 */ 150 void composeLayer(sp<Snapshot> current, sp<Snapshot> previous); 151 152 /** 153 * Creates a new layer stored in the specified snapshot. 154 * 155 * @param snapshot The snapshot associated with the new layer 156 * @param left The left coordinate of the layer 157 * @param top The top coordinate of the layer 158 * @param right The right coordinate of the layer 159 * @param bottom The bottom coordinate of the layer 160 * @param alpha The translucency of the layer 161 * @param mode The blending mode of the layer 162 * @param flags The layer save flags 163 * 164 * @return True if the layer was successfully created, false otherwise 165 */ 166 bool createLayer(sp<Snapshot> snapshot, float left, float top, float right, float bottom, 167 int alpha, SkXfermode::Mode mode, int flags); 168 169 /** 170 * Clears all the regions corresponding to the current list of layers. 171 * This method MUST be invoked before any drawing operation. 172 */ 173 void clearLayerRegions(); 174 175 /** 176 * Draws a colored rectangle with the specified color. The specified coordinates 177 * are transformed by the current snapshot's transform matrix. 178 * 179 * @param left The left coordinate of the rectangle 180 * @param top The top coordinate of the rectangle 181 * @param right The right coordinate of the rectangle 182 * @param bottom The bottom coordinate of the rectangle 183 * @param color The rectangle's ARGB color, defined as a packed 32 bits word 184 * @param mode The Skia xfermode to use 185 * @param ignoreTransform True if the current transform should be ignored 186 * @paran ignoreBlending True if the blending is set by the caller 187 */ 188 void drawColorRect(float left, float top, float right, float bottom, 189 int color, SkXfermode::Mode mode, bool ignoreTransform = false); 190 191 /** 192 * Draws a textured rectangle with the specified texture. The specified coordinates 193 * are transformed by the current snapshot's transform matrix. 194 * 195 * @param left The left coordinate of the rectangle 196 * @param top The top coordinate of the rectangle 197 * @param right The right coordinate of the rectangle 198 * @param bottom The bottom coordinate of the rectangle 199 * @param texture The texture name to map onto the rectangle 200 * @param alpha An additional translucency parameter, between 0.0f and 1.0f 201 * @param mode The blending mode 202 * @param blend True if the texture contains an alpha channel 203 */ 204 void drawTextureRect(float left, float top, float right, float bottom, GLuint texture, 205 float alpha, SkXfermode::Mode mode, bool blend); 206 207 /** 208 * Draws a textured rectangle with the specified texture. The specified coordinates 209 * are transformed by the current snapshot's transform matrix. 210 * 211 * @param left The left coordinate of the rectangle 212 * @param top The top coordinate of the rectangle 213 * @param right The right coordinate of the rectangle 214 * @param bottom The bottom coordinate of the rectangle 215 * @param texture The texture to use 216 * @param paint The paint containing the alpha, blending mode, etc. 217 */ 218 void drawTextureRect(float left, float top, float right, float bottom, 219 const Texture* texture, const SkPaint* paint); 220 221 /** 222 * Draws a textured mesh with the specified texture. If the indices are omitted, the 223 * mesh is drawn as a simple quad. 224 * 225 * @param left The left coordinate of the rectangle 226 * @param top The top coordinate of the rectangle 227 * @param right The right coordinate of the rectangle 228 * @param bottom The bottom coordinate of the rectangle 229 * @param texture The texture name to map onto the rectangle 230 * @param alpha An additional translucency parameter, between 0.0f and 1.0f 231 * @param mode The blending mode 232 * @param blend True if the texture contains an alpha channel 233 * @param vertices The vertices that define the mesh 234 * @param texCoords The texture coordinates of each vertex 235 * @param indices The indices of the vertices, can be NULL 236 * @param elementsCount The number of elements in the mesh, required by indices 237 * @param swapSrcDst Whether or not the src and dst blending operations should be swapped 238 * @param ignoreTransform True if the current transform should be ignored 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, GLenum drawMode, GLsizei elementsCount, 243 bool swapSrcDst = false, bool ignoreTransform = false); 244 245 /** 246 * Prepares the renderer to draw the specified shadow. 247 * 248 * @param texture The shadow texture 249 * @param x The x coordinate of the shadow 250 * @param y The y coordinate of the shadow 251 * @param mode The blending mode 252 * @param alpha The alpha value 253 */ 254 void setupShadow(const ShadowTexture* texture, float x, float y, SkXfermode::Mode mode, 255 float alpha); 256 257 /** 258 * Prepares the renderer to draw the specified Alpha8 texture as a rectangle. 259 * 260 * @param texture The texture to render with 261 * @param textureUnit The texture unit to use, may be modified 262 * @param x The x coordinate of the rectangle to draw 263 * @param y The y coordinate of the rectangle to draw 264 * @param r The red component of the color 265 * @param g The green component of the color 266 * @param b The blue component of the color 267 * @param a The alpha component of the color 268 * @param mode The blending mode 269 * @param transforms True if the matrix passed to the shader should be multiplied 270 * by the model-view matrix 271 * @param applyFilters Whether or not to take color filters and 272 * shaders into account 273 */ 274 void setupTextureAlpha8(const Texture* texture, GLuint& textureUnit, float x, float y, 275 float r, float g, float b, float a, SkXfermode::Mode mode, bool transforms, 276 bool applyFilters); 277 278 /** 279 * Prepares the renderer to draw the specified Alpha8 texture as a rectangle. 280 * 281 * @param texture The texture to render with 282 * @param width The width of the texture 283 * @param height The height of the texture 284 * @param textureUnit The texture unit to use, may be modified 285 * @param x The x coordinate of the rectangle to draw 286 * @param y The y coordinate of the rectangle to draw 287 * @param r The red component of the color 288 * @param g The green component of the color 289 * @param b The blue component of the color 290 * @param a The alpha component of the color 291 * @param mode The blending mode 292 * @param transforms True if the matrix passed to the shader should be multiplied 293 * by the model-view matrix 294 * @param applyFilters Whether or not to take color filters and 295 * shaders into account 296 */ 297 void setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height, 298 GLuint& textureUnit, float x, float y, float r, float g, float b, float a, 299 SkXfermode::Mode mode, bool transforms, bool applyFilters); 300 301 /** 302 * Same as above setupTextureAlpha8() but specifies the mesh's vertices 303 * and texCoords pointers. 304 */ 305 void setupTextureAlpha8(GLuint texture, uint32_t width, uint32_t height, 306 GLuint& textureUnit, float x, float y, float r, float g, float b, float a, 307 SkXfermode::Mode mode, bool transforms, bool applyFilters, 308 GLvoid* vertices, GLvoid* texCoords); 309 310 /** 311 * Draws text underline and strike-through if needed. 312 * 313 * @param text The text to decor 314 * @param bytesCount The number of bytes in the text 315 * @param length The length in pixels of the text, can be <= 0.0f to force a measurement 316 * @param x The x coordinate where the text will be drawn 317 * @param y The y coordinate where the text will be drawn 318 * @param paint The paint to draw the text with 319 */ 320 void drawTextDecorations(const char* text, int bytesCount, float length, 321 float x, float y, SkPaint* paint); 322 323 /** 324 * Resets the texture coordinates stored in mMeshVertices. Setting the values 325 * back to default is achieved by calling: 326 * 327 * resetDrawTextureTexCoords(0.0f, 0.0f, 1.0f, 1.0f); 328 * 329 * @param u1 The left coordinate of the texture 330 * @param v1 The bottom coordinate of the texture 331 * @param u2 The right coordinate of the texture 332 * @param v2 The top coordinate of the texture 333 */ 334 void resetDrawTextureTexCoords(float u1, float v1, float u2, float v2); 335 336 /** 337 * Gets the alpha and xfermode out of a paint object. If the paint is null 338 * alpha will be 255 and the xfermode will be SRC_OVER. 339 * 340 * @param paint The paint to extract values from 341 * @param alpha Where to store the resulting alpha 342 * @param mode Where to store the resulting xfermode 343 */ 344 inline void getAlphaAndMode(const SkPaint* paint, int* alpha, SkXfermode::Mode* mode); 345 346 /** 347 * Binds the specified texture with the specified wrap modes. 348 */ 349 inline void bindTexture(GLuint texture, GLenum wrapS, GLenum wrapT, GLuint textureUnit = 0); 350 351 /** 352 * Enable or disable blending as necessary. This function sets the appropriate 353 * blend function based on the specified xfermode. 354 */ 355 inline void chooseBlending(bool blend, SkXfermode::Mode mode, ProgramDescription& description, 356 bool swapSrcDst = false); 357 358 /** 359 * Safely retrieves the mode from the specified xfermode. If the specified 360 * xfermode is null, the mode is assumed to be SkXfermode::kSrcOver_Mode. 361 */ 362 inline SkXfermode::Mode getXfermode(SkXfermode* mode); 363 364 /** 365 * Use the specified program with the current GL context. If the program is already 366 * in use, it will not be bound again. If it is not in use, the current program is 367 * marked unused and the specified program becomes used and becomes the new 368 * current program. 369 * 370 * @param program The program to use 371 * 372 * @return true If the specified program was already in use, false otherwise. 373 */ 374 inline bool useProgram(Program* program); 375 376 // Dimensions of the drawing surface 377 int mWidth, mHeight; 378 379 // Matrix used for ortho projection in shaders 380 mat4 mOrthoMatrix; 381 382 // Model-view matrix used to position/size objects 383 mat4 mModelView; 384 385 // Number of saved states 386 int mSaveCount; 387 // Base state 388 sp<Snapshot> mFirstSnapshot; 389 // Current state 390 sp<Snapshot> mSnapshot; 391 392 // Shaders 393 SkiaShader* mShader; 394 395 // Color filters 396 SkiaColorFilter* mColorFilter; 397 398 // Used to draw textured quads 399 TextureVertex mMeshVertices[4]; 400 401 // GL extensions 402 Extensions mExtensions; 403 404 // Drop shadow 405 bool mHasShadow; 406 float mShadowRadius; 407 float mShadowDx; 408 float mShadowDy; 409 int mShadowColor; 410 411 // Various caches 412 Caches& mCaches; 413 414 // List of rectangles to clear due to calls to saveLayer() 415 Vector<Rect*> mLayers; 416 417 // Single object used to draw lines 418 Line mLine; 419 420 // Misc 421 GLint mMaxTextureSize; 422 423}; // class OpenGLRenderer 424 425}; // namespace uirenderer 426}; // namespace android 427 428#endif // ANDROID_UI_OPENGL_RENDERER_H 429