Caches.h revision 96a5c4c7bab6718524de7253da8309143ab48bef
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
25#include "AssetAtlas.h"
26#include "Dither.h"
27#include "Extensions.h"
28#include "FboCache.h"
29#include "GradientCache.h"
30#include "LayerCache.h"
31#include "PatchCache.h"
32#include "ProgramCache.h"
33#include "PathCache.h"
34#include "RenderBufferCache.h"
35#include "renderstate/PixelBufferState.h"
36#include "ResourceCache.h"
37#include "TessellationCache.h"
38#include "TextDropShadowCache.h"
39#include "TextureCache.h"
40#include "thread/TaskProcessor.h"
41#include "thread/TaskManager.h"
42
43#include <vector>
44#include <memory>
45
46#include <GLES3/gl3.h>
47
48#include <utils/KeyedVector.h>
49#include <utils/Singleton.h>
50#include <utils/Vector.h>
51
52#include <cutils/compiler.h>
53
54#include <SkPath.h>
55
56namespace android {
57namespace uirenderer {
58
59class GammaFontRenderer;
60
61///////////////////////////////////////////////////////////////////////////////
62// Globals
63///////////////////////////////////////////////////////////////////////////////
64
65// GL ES 2.0 defines that at least 16 texture units must be supported
66#define REQUIRED_TEXTURE_UNITS_COUNT 3
67
68// Must define as many texture units as specified by REQUIRED_TEXTURE_UNITS_COUNT
69static const GLenum gTextureUnits[] = {
70    GL_TEXTURE0,
71    GL_TEXTURE1,
72    GL_TEXTURE2
73};
74
75///////////////////////////////////////////////////////////////////////////////
76// Caches
77///////////////////////////////////////////////////////////////////////////////
78
79class RenderNode;
80class RenderState;
81
82class ANDROID_API Caches {
83public:
84    static Caches& createInstance(RenderState& renderState) {
85        LOG_ALWAYS_FATAL_IF(sInstance, "double create of Caches attempted");
86        sInstance = new Caches(renderState);
87        return *sInstance;
88    }
89
90    static Caches& getInstance() {
91        LOG_ALWAYS_FATAL_IF(!sInstance, "instance not yet created");
92        return *sInstance;
93    }
94
95    static bool hasInstance() {
96        return sInstance != 0;
97    }
98private:
99    Caches(RenderState& renderState);
100    static Caches* sInstance;
101
102public:
103    enum FlushMode {
104        kFlushMode_Layers = 0,
105        kFlushMode_Moderate,
106        kFlushMode_Full
107    };
108
109    /**
110     * Initialize caches.
111     */
112    bool init();
113
114    /**
115     * Initialize global system properties.
116     */
117    bool initProperties();
118
119    /**
120     * Flush the cache.
121     *
122     * @param mode Indicates how much of the cache should be flushed
123     */
124    void flush(FlushMode mode);
125
126    /**
127     * Destroys all resources associated with this cache. This should
128     * be called after a flush(kFlushMode_Full).
129     */
130    void terminate();
131
132    /**
133     * Indicates whether the renderer is in debug mode.
134     * This debug mode provides limited information to app developers.
135     */
136    DebugLevel getDebugLevel() const {
137        return mDebugLevel;
138    }
139
140    /**
141     * Returns a non-premultiplied ARGB color for the specified
142     * amount of overdraw (1 for 1x, 2 for 2x, etc.)
143     */
144    uint32_t getOverdrawColor(uint32_t amount) const;
145
146    /**
147     * Call this on each frame to ensure that garbage is deleted from
148     * GPU memory.
149     */
150    void clearGarbage();
151
152    /**
153     * Can be used to delete a layer from a non EGL thread.
154     */
155    void deleteLayerDeferred(Layer* layer);
156
157
158    /**
159     * Activate the specified texture unit. The texture unit must
160     * be specified using an integer number (0 for GL_TEXTURE0 etc.)
161     */
162    void activeTexture(GLuint textureUnit);
163
164    /**
165     * Invalidate the cached value of the active texture unit.
166     */
167    void resetActiveTexture();
168
169    /**
170     * Binds the specified texture as a GL_TEXTURE_2D texture.
171     * All texture bindings must be performed with this method or
172     * bindTexture(GLenum, GLuint).
173     */
174    void bindTexture(GLuint texture);
175
176    /**
177     * Binds the specified texture with the specified render target.
178     * All texture bindings must be performed with this method or
179     * bindTexture(GLuint).
180     */
181    void bindTexture(GLenum target, GLuint texture);
182
183    /**
184     * Deletes the specified texture and clears it from the cache
185     * of bound textures.
186     * All textures must be deleted using this method.
187     */
188    void deleteTexture(GLuint texture);
189
190    /**
191     * Signals that the cache of bound textures should be cleared.
192     * Other users of the context may have altered which textures are bound.
193     */
194    void resetBoundTextures();
195
196    /**
197     * Clear the cache of bound textures.
198     */
199    void unbindTexture(GLuint texture);
200
201    void startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard);
202    void endTiling();
203
204    /**
205     * Returns the mesh used to draw regions. Calling this method will
206     * bind a VBO of type GL_ELEMENT_ARRAY_BUFFER that contains the
207     * indices for the region mesh.
208     */
209    TextureVertex* getRegionMesh();
210
211    /**
212     * Displays the memory usage of each cache and the total sum.
213     */
214    void dumpMemoryUsage();
215    void dumpMemoryUsage(String8& log);
216
217    bool hasRegisteredFunctors();
218    void registerFunctors(uint32_t functorCount);
219    void unregisterFunctors(uint32_t functorCount);
220
221    bool blend;
222    GLenum lastSrcMode;
223    GLenum lastDstMode;
224    Program* currentProgram;
225
226    bool drawDeferDisabled;
227    bool drawReorderDisabled;
228
229    // Misc
230    GLint maxTextureSize;
231
232    // Debugging
233    bool debugLayersUpdates;
234    bool debugOverdraw;
235
236    enum StencilClipDebug {
237        kStencilHide,
238        kStencilShowHighlight,
239        kStencilShowRegion
240    };
241    StencilClipDebug debugStencilClip;
242
243    TextureCache textureCache;
244    LayerCache layerCache;
245    RenderBufferCache renderBufferCache;
246    GradientCache gradientCache;
247    ProgramCache programCache;
248    PathCache pathCache;
249    PatchCache patchCache;
250    TessellationCache tessellationCache;
251    TextDropShadowCache dropShadowCache;
252    FboCache fboCache;
253
254    GammaFontRenderer* fontRenderer;
255
256    TaskManager tasks;
257
258    Dither dither;
259
260    bool gpuPixelBuffersEnabled;
261
262    // Debug methods
263    PFNGLINSERTEVENTMARKEREXTPROC eventMark;
264    PFNGLPUSHGROUPMARKEREXTPROC startMark;
265    PFNGLPOPGROUPMARKEREXTPROC endMark;
266
267    PFNGLLABELOBJECTEXTPROC setLabel;
268    PFNGLGETOBJECTLABELEXTPROC getLabel;
269
270    // TEMPORARY properties
271    void initTempProperties();
272    void setTempProperty(const char* name, const char* value);
273
274    float propertyLightDiameter;
275    float propertyLightPosY;
276    float propertyLightPosZ;
277    float propertyAmbientRatio;
278    int propertyAmbientShadowStrength;
279    int propertySpotShadowStrength;
280
281    PixelBufferState& pixelBuffer() { return *mPixelBufferState; }
282
283private:
284    enum OverdrawColorSet {
285        kColorSet_Default = 0,
286        kColorSet_Deuteranomaly
287    };
288
289    void initFont();
290    void initExtensions();
291    void initConstraints();
292    void initStaticProperties();
293
294    static void eventMarkNull(GLsizei length, const GLchar* marker) { }
295    static void startMarkNull(GLsizei length, const GLchar* marker) { }
296    static void endMarkNull() { }
297
298    static void setLabelNull(GLenum type, uint object, GLsizei length,
299            const char* label) { }
300    static void getLabelNull(GLenum type, uint object, GLsizei bufferSize,
301            GLsizei* length, char* label) {
302        if (length) *length = 0;
303        if (label) *label = '\0';
304    }
305
306    RenderState* mRenderState;
307
308    std::unique_ptr<PixelBufferState> mPixelBufferState; // TODO: move to RenderState
309
310    GLuint mTextureUnit;
311
312    Extensions& mExtensions;
313
314    // Used to render layers
315    std::unique_ptr<TextureVertex[]> mRegionMesh;
316
317    mutable Mutex mGarbageLock;
318    Vector<Layer*> mLayerGarbage;
319
320    DebugLevel mDebugLevel;
321    bool mInitialized;
322
323    uint32_t mFunctorsCount;
324
325    // Caches texture bindings for the GL_TEXTURE_2D target
326    GLuint mBoundTextures[REQUIRED_TEXTURE_UNITS_COUNT];
327
328    OverdrawColorSet mOverdrawDebugColorSet;
329}; // class Caches
330
331}; // namespace uirenderer
332}; // namespace android
333
334#endif // ANDROID_HWUI_CACHES_H
335