Caches.h revision 57066eb64c9a190d1afc87bb060bbb2d31e5b86c
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_CACHES_H
18#define ANDROID_HWUI_CACHES_H
19
20#ifndef LOG_TAG
21    #define LOG_TAG "OpenGLRenderer"
22#endif
23
24#include <utils/Singleton.h>
25
26#include "Extensions.h"
27#include "FontRenderer.h"
28#include "GammaFontRenderer.h"
29#include "TextureCache.h"
30#include "LayerCache.h"
31#include "GradientCache.h"
32#include "PatchCache.h"
33#include "ProgramCache.h"
34#include "PathCache.h"
35#include "TextDropShadowCache.h"
36#include "FboCache.h"
37#include "ResourceCache.h"
38
39namespace android {
40namespace uirenderer {
41
42///////////////////////////////////////////////////////////////////////////////
43// Globals
44///////////////////////////////////////////////////////////////////////////////
45
46#define REQUIRED_TEXTURE_UNITS_COUNT 3
47
48#define REGION_MESH_QUAD_COUNT 512
49
50// Generates simple and textured vertices
51#define FV(x, y, u, v) { { x, y }, { u, v } }
52
53// This array is never used directly but used as a memcpy source in the
54// OpenGLRenderer constructor
55static const TextureVertex gMeshVertices[] = {
56        FV(0.0f, 0.0f, 0.0f, 0.0f),
57        FV(1.0f, 0.0f, 1.0f, 0.0f),
58        FV(0.0f, 1.0f, 0.0f, 1.0f),
59        FV(1.0f, 1.0f, 1.0f, 1.0f)
60};
61static const GLsizei gMeshStride = sizeof(TextureVertex);
62static const GLsizei gMeshTextureOffset = 2 * sizeof(float);
63static const GLsizei gMeshCount = 4;
64
65///////////////////////////////////////////////////////////////////////////////
66// Debug
67///////////////////////////////////////////////////////////////////////////////
68
69struct CacheLogger {
70    CacheLogger() {
71        LOGD("Creating caches");
72    }
73}; // struct CacheLogger
74
75///////////////////////////////////////////////////////////////////////////////
76// Caches
77///////////////////////////////////////////////////////////////////////////////
78
79class Caches: public Singleton<Caches> {
80    Caches();
81    ~Caches();
82
83    friend class Singleton<Caches>;
84
85    CacheLogger mLogger;
86
87    GLuint mCurrentBuffer;
88
89    // Used to render layers
90    TextureVertex* mRegionMesh;
91    GLuint mRegionMeshIndices;
92
93    mutable Mutex mGarbageLock;
94    Vector<GLuint> mFboGarbage;
95    Vector<GLuint> mTextureGarbage;
96
97public:
98    /**
99     * Indicates whether the renderer is in debug mode.
100     * This debug mode provides limited information to app developers.
101     */
102    DebugLevel getDebugLevel() const {
103        return mDebugLevel;
104    }
105
106    /**
107     * Call this on each frame to ensure that garbage is deleted from
108     * GPU memory.
109     */
110    void clearGarbage();
111
112    /**
113     * Can be used to delete an FBO from a non EGL thread.
114     */
115    void deleteFboDeferred(GLuint fbo);
116
117    /**
118     * Can be used to delete a texture from a non EGL thread.
119     */
120    void deleteTextureDeferred(GLuint texture);
121
122    /**
123     * Binds the VBO used to render simple textured quads.
124     */
125    void bindMeshBuffer();
126
127    /**
128     * Binds the specified VBO if needed.
129     */
130    void bindMeshBuffer(const GLuint buffer);
131
132    /**
133     * Unbinds the VBO used to render simple textured quads.
134     */
135    void unbindMeshBuffer();
136
137    /**
138     * Returns the mesh used to draw regions. Calling this method will
139     * bind a VBO of type GL_ELEMENT_ARRAY_BUFFER that contains the
140     * indices for the region mesh.
141     */
142    TextureVertex* getRegionMesh();
143
144    /**
145     * Displays the memory usage of each cache and the total sum.
146     */
147    void dumpMemoryUsage();
148
149    bool blend;
150    GLenum lastSrcMode;
151    GLenum lastDstMode;
152    Program* currentProgram;
153
154    // VBO to draw with
155    GLuint meshBuffer;
156
157    // GL extensions
158    Extensions extensions;
159
160    // Misc
161    GLint maxTextureSize;
162
163    TextureCache textureCache;
164    LayerCache layerCache;
165    GradientCache gradientCache;
166    ProgramCache programCache;
167    PathCache pathCache;
168    PatchCache patchCache;
169    TextDropShadowCache dropShadowCache;
170    FboCache fboCache;
171    GammaFontRenderer fontRenderer;
172    ResourceCache resourceCache;
173
174private:
175    DebugLevel mDebugLevel;
176}; // class Caches
177
178}; // namespace uirenderer
179}; // namespace android
180
181#endif // ANDROID_HWUI_CACHES_H
182