Caches.h revision 5dc7fa709646799a5207a5d217f70aa02bf4a3aa
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 <cutils/compiler.h>
27
28#include "thread/TaskProcessor.h"
29#include "thread/TaskManager.h"
30
31#include "FontRenderer.h"
32#include "GammaFontRenderer.h"
33#include "TextureCache.h"
34#include "LayerCache.h"
35#include "RenderBufferCache.h"
36#include "GradientCache.h"
37#include "PatchCache.h"
38#include "ProgramCache.h"
39#include "ShapeCache.h"
40#include "PathCache.h"
41#include "TextDropShadowCache.h"
42#include "FboCache.h"
43#include "ResourceCache.h"
44#include "Stencil.h"
45#include "Dither.h"
46
47namespace android {
48namespace uirenderer {
49
50///////////////////////////////////////////////////////////////////////////////
51// Globals
52///////////////////////////////////////////////////////////////////////////////
53
54#define REQUIRED_TEXTURE_UNITS_COUNT 3
55
56#define REGION_MESH_QUAD_COUNT 512
57
58// Generates simple and textured vertices
59#define FV(x, y, u, v) { { x, y }, { u, v } }
60
61// This array is never used directly but used as a memcpy source in the
62// OpenGLRenderer constructor
63static const TextureVertex gMeshVertices[] = {
64        FV(0.0f, 0.0f, 0.0f, 0.0f),
65        FV(1.0f, 0.0f, 1.0f, 0.0f),
66        FV(0.0f, 1.0f, 0.0f, 1.0f),
67        FV(1.0f, 1.0f, 1.0f, 1.0f)
68};
69static const GLsizei gMeshStride = sizeof(TextureVertex);
70static const GLsizei gVertexStride = sizeof(Vertex);
71static const GLsizei gAlphaVertexStride = sizeof(AlphaVertex);
72static const GLsizei gMeshTextureOffset = 2 * sizeof(float);
73static const GLsizei gVertexAlphaOffset = 2 * sizeof(float);
74static const GLsizei gVertexAAWidthOffset = 2 * sizeof(float);
75static const GLsizei gVertexAALengthOffset = 3 * sizeof(float);
76static const GLsizei gMeshCount = 4;
77
78static const GLenum gTextureUnits[] = {
79    GL_TEXTURE0,
80    GL_TEXTURE1,
81    GL_TEXTURE2
82};
83
84///////////////////////////////////////////////////////////////////////////////
85// Debug
86///////////////////////////////////////////////////////////////////////////////
87
88struct CacheLogger {
89    CacheLogger() {
90        INIT_LOGD("Creating OpenGL renderer caches");
91    }
92}; // struct CacheLogger
93
94///////////////////////////////////////////////////////////////////////////////
95// Caches
96///////////////////////////////////////////////////////////////////////////////
97
98class DisplayList;
99
100class ANDROID_API Caches: public Singleton<Caches> {
101    Caches();
102
103    friend class Singleton<Caches>;
104
105    CacheLogger mLogger;
106
107public:
108    enum FlushMode {
109        kFlushMode_Layers = 0,
110        kFlushMode_Moderate,
111        kFlushMode_Full
112    };
113
114    /**
115     * Initialize caches.
116     */
117    void init();
118
119    /**
120     * Initialize global system properties.
121     */
122    bool initProperties();
123
124    /**
125     * Flush the cache.
126     *
127     * @param mode Indicates how much of the cache should be flushed
128     */
129    void flush(FlushMode mode);
130
131    /**
132     * Destroys all resources associated with this cache. This should
133     * be called after a flush(kFlushMode_Full).
134     */
135    void terminate();
136
137    /**
138     * Indicates whether the renderer is in debug mode.
139     * This debug mode provides limited information to app developers.
140     */
141    DebugLevel getDebugLevel() const {
142        return mDebugLevel;
143    }
144
145    /**
146     * Call this on each frame to ensure that garbage is deleted from
147     * GPU memory.
148     */
149    void clearGarbage();
150
151    /**
152     * Can be used to delete a layer from a non EGL thread.
153     */
154    void deleteLayerDeferred(Layer* layer);
155
156    /*
157     * Can be used to delete a display list from a non EGL thread.
158     */
159    void deleteDisplayListDeferred(DisplayList* layer);
160
161    /**
162     * Binds the VBO used to render simple textured quads.
163     */
164    bool bindMeshBuffer();
165
166    /**
167     * Binds the specified VBO if needed.
168     */
169    bool bindMeshBuffer(const GLuint buffer);
170
171    /**
172     * Unbinds the VBO used to render simple textured quads.
173     */
174    bool unbindMeshBuffer();
175
176    bool bindIndicesBuffer(const GLuint buffer);
177    bool unbindIndicesBuffer();
178
179    /**
180     * Binds an attrib to the specified float vertex pointer.
181     * Assumes a stride of gMeshStride and a size of 2.
182     */
183    void bindPositionVertexPointer(bool force, GLvoid* vertices, GLsizei stride = gMeshStride);
184
185    /**
186     * Binds an attrib to the specified float vertex pointer.
187     * Assumes a stride of gMeshStride and a size of 2.
188     */
189    void bindTexCoordsVertexPointer(bool force, GLvoid* vertices, GLsizei stride = gMeshStride);
190
191    /**
192     * Resets the vertex pointers.
193     */
194    void resetVertexPointers();
195    void resetTexCoordsVertexPointer();
196
197    void enableTexCoordsVertexArray();
198    void disableTexCoordsVertexArray();
199
200    /**
201     * Activate the specified texture unit. The texture unit must
202     * be specified using an integer number (0 for GL_TEXTURE0 etc.)
203     */
204    void activeTexture(GLuint textureUnit);
205
206    /**
207     * Sets the scissor for the current surface.
208     */
209    bool setScissor(GLint x, GLint y, GLint width, GLint height);
210
211    /**
212     * Resets the scissor state.
213     */
214    void resetScissor();
215
216    bool enableScissor();
217    bool disableScissor();
218    void setScissorEnabled(bool enabled);
219
220    void startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard);
221    void endTiling();
222
223    /**
224     * Returns the mesh used to draw regions. Calling this method will
225     * bind a VBO of type GL_ELEMENT_ARRAY_BUFFER that contains the
226     * indices for the region mesh.
227     */
228    TextureVertex* getRegionMesh();
229
230    /**
231     * Displays the memory usage of each cache and the total sum.
232     */
233    void dumpMemoryUsage();
234    void dumpMemoryUsage(String8& log);
235
236    bool hasRegisteredFunctors();
237    void registerFunctors(uint32_t functorCount);
238    void unregisterFunctors(uint32_t functorCount);
239
240    bool blend;
241    GLenum lastSrcMode;
242    GLenum lastDstMode;
243    Program* currentProgram;
244    bool scissorEnabled;
245
246    bool drawDeferDisabled;
247    bool drawReorderDisabled;
248
249    // VBO to draw with
250    GLuint meshBuffer;
251
252    // Misc
253    GLint maxTextureSize;
254
255    // Debugging
256    bool debugLayersUpdates;
257    bool debugOverdraw;
258
259    enum StencilClipDebug {
260        kStencilHide,
261        kStencilShowHighlight,
262        kStencilShowRegion
263    };
264    StencilClipDebug debugStencilClip;
265
266    TextureCache textureCache;
267    LayerCache layerCache;
268    RenderBufferCache renderBufferCache;
269    GradientCache gradientCache;
270    ProgramCache programCache;
271    PathCache pathCache;
272    RoundRectShapeCache roundRectShapeCache;
273    CircleShapeCache circleShapeCache;
274    OvalShapeCache ovalShapeCache;
275    RectShapeCache rectShapeCache;
276    ArcShapeCache arcShapeCache;
277    PatchCache patchCache;
278    TextDropShadowCache dropShadowCache;
279    FboCache fboCache;
280    ResourceCache resourceCache;
281
282    GammaFontRenderer* fontRenderer;
283
284    TaskManager tasks;
285
286    Dither dither;
287    Stencil stencil;
288
289    // Debug methods
290    PFNGLINSERTEVENTMARKEREXTPROC eventMark;
291    PFNGLPUSHGROUPMARKEREXTPROC startMark;
292    PFNGLPOPGROUPMARKEREXTPROC endMark;
293
294    PFNGLLABELOBJECTEXTPROC setLabel;
295    PFNGLGETOBJECTLABELEXTPROC getLabel;
296
297private:
298    void initFont();
299    void initExtensions();
300    void initConstraints();
301
302    static void eventMarkNull(GLsizei length, const GLchar* marker) { }
303    static void startMarkNull(GLsizei length, const GLchar* marker) { }
304    static void endMarkNull() { }
305
306    static void setLabelNull(GLenum type, uint object, GLsizei length,
307            const char* label) { }
308    static void getLabelNull(GLenum type, uint object, GLsizei bufferSize,
309            GLsizei* length, char* label) {
310        if (length) *length = 0;
311        if (label) *label = '\0';
312    }
313
314    GLuint mCurrentBuffer;
315    GLuint mCurrentIndicesBuffer;
316    void* mCurrentPositionPointer;
317    GLsizei mCurrentPositionStride;
318    void* mCurrentTexCoordsPointer;
319    GLsizei mCurrentTexCoordsStride;
320
321    bool mTexCoordsArrayEnabled;
322
323    GLuint mTextureUnit;
324
325    GLint mScissorX;
326    GLint mScissorY;
327    GLint mScissorWidth;
328    GLint mScissorHeight;
329
330    Extensions& mExtensions;
331
332    // Used to render layers
333    TextureVertex* mRegionMesh;
334    GLuint mRegionMeshIndices;
335
336    mutable Mutex mGarbageLock;
337    Vector<Layer*> mLayerGarbage;
338    Vector<DisplayList*> mDisplayListGarbage;
339
340    DebugLevel mDebugLevel;
341    bool mInitialized;
342
343    uint32_t mFunctorsCount;
344}; // class Caches
345
346}; // namespace uirenderer
347}; // namespace android
348
349#endif // ANDROID_HWUI_CACHES_H
350