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