blob: 8d2383309b6545a31da89ccba5030ef31cc3fa68 [file] [log] [blame]
Romain Guyfb8b7632010-08-23 21:05:08 -07001/*
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
Romain Guy5b3b3522010-10-27 18:57:51 -070017#ifndef ANDROID_HWUI_CACHES_H
18#define ANDROID_HWUI_CACHES_H
Romain Guyfb8b7632010-08-23 21:05:08 -070019
Romain Guya2341a92010-09-08 18:04:33 -070020#ifndef LOG_TAG
21 #define LOG_TAG "OpenGLRenderer"
22#endif
Romain Guyfb8b7632010-08-23 21:05:08 -070023
Chris Craik96a5c4c2015-01-27 15:46:35 -080024
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
Chris Craik59744b72014-07-01 17:56:52 -070043#include <vector>
Chris Craik96a5c4c2015-01-27 15:46:35 -080044#include <memory>
Chris Craik59744b72014-07-01 17:56:52 -070045
Romain Guy3b748a42013-04-17 18:54:38 -070046#include <GLES3/gl3.h>
47
48#include <utils/KeyedVector.h>
Romain Guyfb8b7632010-08-23 21:05:08 -070049#include <utils/Singleton.h>
Romain Guy3b748a42013-04-17 18:54:38 -070050#include <utils/Vector.h>
Romain Guyfb8b7632010-08-23 21:05:08 -070051
Romain Guy79537452011-10-12 13:48:51 -070052#include <cutils/compiler.h>
53
Tom Hudson2dc236b2014-10-15 15:46:42 -040054#include <SkPath.h>
55
Romain Guyfb8b7632010-08-23 21:05:08 -070056namespace android {
57namespace uirenderer {
58
Tom Hudson2dc236b2014-10-15 15:46:42 -040059class GammaFontRenderer;
60
Romain Guy03750a02010-10-18 14:06:08 -070061///////////////////////////////////////////////////////////////////////////////
62// Globals
63///////////////////////////////////////////////////////////////////////////////
64
Romain Guy8aa195d2013-06-04 18:00:09 -070065// GL ES 2.0 defines that at least 16 texture units must be supported
Romain Guy03750a02010-10-18 14:06:08 -070066#define REQUIRED_TEXTURE_UNITS_COUNT 3
67
Romain Guy8aa195d2013-06-04 18:00:09 -070068// Must define as many texture units as specified by REQUIRED_TEXTURE_UNITS_COUNT
Romain Guya1d3c912011-12-13 14:55:06 -080069static const GLenum gTextureUnits[] = {
70 GL_TEXTURE0,
71 GL_TEXTURE1,
72 GL_TEXTURE2
73};
74
Romain Guy03750a02010-10-18 14:06:08 -070075///////////////////////////////////////////////////////////////////////////////
Romain Guy03750a02010-10-18 14:06:08 -070076// Caches
77///////////////////////////////////////////////////////////////////////////////
78
John Recke18264b2014-03-12 13:56:30 -070079class RenderNode;
John Reck17035b02014-09-03 07:39:53 -070080class RenderState;
Romain Guybb0acdf2012-03-05 13:44:35 -080081
Chris Craik96a5c4c2015-01-27 15:46:35 -080082class 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 }
Romain Guyfb8b7632010-08-23 21:05:08 -070089
Chris Craik96a5c4c2015-01-27 15:46:35 -080090 static Caches& getInstance() {
91 LOG_ALWAYS_FATAL_IF(!sInstance, "instance not yet created");
92 return *sInstance;
93 }
Romain Guyfb8b7632010-08-23 21:05:08 -070094
Chris Craik96a5c4c2015-01-27 15:46:35 -080095 static bool hasInstance() {
96 return sInstance != 0;
97 }
98private:
99 Caches(RenderState& renderState);
100 static Caches* sInstance;
Romain Guy9bca4792010-10-25 18:42:25 -0700101
Romain Guyfb8b7632010-08-23 21:05:08 -0700102public:
Romain Guybdf76092011-07-18 15:00:43 -0700103 enum FlushMode {
Romain Guy6d7475d2011-07-27 16:28:21 -0700104 kFlushMode_Layers = 0,
105 kFlushMode_Moderate,
Romain Guybdf76092011-07-18 15:00:43 -0700106 kFlushMode_Full
107 };
108
109 /**
Romain Guydfa10462012-05-12 16:18:58 -0700110 * Initialize caches.
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800111 */
Romain Guy3b748a42013-04-17 18:54:38 -0700112 bool init();
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800113
114 /**
Romain Guy5bb3c732012-11-29 17:52:58 -0800115 * Initialize global system properties.
116 */
117 bool initProperties();
118
119 /**
Romain Guybdf76092011-07-18 15:00:43 -0700120 * Flush the cache.
121 *
122 * @param mode Indicates how much of the cache should be flushed
123 */
124 void flush(FlushMode mode);
125
Romain Guy5b3b3522010-10-27 18:57:51 -0700126 /**
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800127 * Destroys all resources associated with this cache. This should
128 * be called after a flush(kFlushMode_Full).
129 */
130 void terminate();
131
132 /**
Romain Guye190aa62010-11-10 19:01:29 -0800133 * 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 /**
Romain Guy627c6fd2013-08-21 11:53:18 -0700141 * 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 /**
Romain Guyfe48f652010-11-11 15:36:56 -0800147 * Call this on each frame to ensure that garbage is deleted from
148 * GPU memory.
149 */
150 void clearGarbage();
151
152 /**
Romain Guyada830f2011-01-13 12:13:20 -0800153 * Can be used to delete a layer from a non EGL thread.
Romain Guy57066eb2011-01-12 12:53:32 -0800154 */
Romain Guyada830f2011-01-13 12:13:20 -0800155 void deleteLayerDeferred(Layer* layer);
Romain Guy57066eb2011-01-12 12:53:32 -0800156
Romain Guy15bc6432011-12-13 13:11:32 -0800157
Romain Guy5b3b3522010-10-27 18:57:51 -0700158 /**
Romain Guya1d3c912011-12-13 14:55:06 -0800159 * 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 /**
Chris Craik9ab2d182013-07-22 16:16:06 -0700165 * Invalidate the cached value of the active texture unit.
166 */
167 void resetActiveTexture();
168
169 /**
Romain Guy8aa195d2013-06-04 18:00:09 -0700170 * Binds the specified texture as a GL_TEXTURE_2D texture.
Romain Guybe1b1272013-06-06 14:02:54 -0700171 * All texture bindings must be performed with this method or
172 * bindTexture(GLenum, GLuint).
Romain Guy8aa195d2013-06-04 18:00:09 -0700173 */
174 void bindTexture(GLuint texture);
175
176 /**
Romain Guybe1b1272013-06-06 14:02:54 -0700177 * Binds the specified texture with the specified render target.
178 * All texture bindings must be performed with this method or
179 * bindTexture(GLuint).
Romain Guy8aa195d2013-06-04 18:00:09 -0700180 */
181 void bindTexture(GLenum target, GLuint texture);
182
183 /**
Romain Guybe1b1272013-06-06 14:02:54 -0700184 * 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 /**
Romain Guy8aa195d2013-06-04 18:00:09 -0700191 * 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 /**
jiayuanr4a473c7d2014-06-10 17:41:49 +0800197 * Clear the cache of bound textures.
198 */
199 void unbindTexture(GLuint texture);
200
Romain Guyef359272013-01-31 19:07:29 -0800201 void startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard);
Romain Guy85ef80d2012-09-13 20:26:50 -0700202 void endTiling();
203
Romain Guy82bc7a72012-01-03 14:13:39 -0800204 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700205 * 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
Romain Guyc15008e2010-11-10 11:59:15 -0800211 /**
212 * Displays the memory usage of each cache and the total sum.
213 */
214 void dumpMemoryUsage();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700215 void dumpMemoryUsage(String8& log);
Romain Guyc15008e2010-11-10 11:59:15 -0800216
Romain Guy54c1a642012-09-27 17:55:46 -0700217 bool hasRegisteredFunctors();
218 void registerFunctors(uint32_t functorCount);
219 void unregisterFunctors(uint32_t functorCount);
220
Romain Guyfb8b7632010-08-23 21:05:08 -0700221 bool blend;
222 GLenum lastSrcMode;
223 GLenum lastDstMode;
224 Program* currentProgram;
225
Romain Guy0f667532013-03-01 14:31:04 -0800226 bool drawDeferDisabled;
227 bool drawReorderDisabled;
228
Romain Guy746b7402010-10-26 16:27:31 -0700229 // Misc
230 GLint maxTextureSize;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800231
232 // Debugging
Romain Guy4ff0cf42012-08-06 14:51:10 -0700233 bool debugLayersUpdates;
Romain Guy7c450aa2012-09-21 19:15:00 -0700234 bool debugOverdraw;
Romain Guy746b7402010-10-26 16:27:31 -0700235
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800236 enum StencilClipDebug {
237 kStencilHide,
238 kStencilShowHighlight,
239 kStencilShowRegion
240 };
241 StencilClipDebug debugStencilClip;
242
Romain Guyfb8b7632010-08-23 21:05:08 -0700243 TextureCache textureCache;
244 LayerCache layerCache;
Romain Guy8d4aeb72013-02-12 16:08:55 -0800245 RenderBufferCache renderBufferCache;
Romain Guyfb8b7632010-08-23 21:05:08 -0700246 GradientCache gradientCache;
247 ProgramCache programCache;
248 PathCache pathCache;
249 PatchCache patchCache;
Chris Craik05f3d6e2014-06-02 16:27:04 -0700250 TessellationCache tessellationCache;
Romain Guyfb8b7632010-08-23 21:05:08 -0700251 TextDropShadowCache dropShadowCache;
Romain Guye2d345e2010-09-24 18:39:22 -0700252 FboCache fboCache;
Romain Guy29d89972010-09-22 16:10:57 -0700253
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700254 GammaFontRenderer* fontRenderer;
255
Romain Guy5dc7fa72013-03-11 20:48:31 -0700256 TaskManager tasks;
257
Romain Guy0baaac52012-08-31 20:31:01 -0700258 Dither dither;
Romain Guy0baaac52012-08-31 20:31:01 -0700259
Romain Guyf9f00162013-05-09 11:50:12 -0700260 bool gpuPixelBuffersEnabled;
261
Romain Guydfa10462012-05-12 16:18:58 -0700262 // Debug methods
Romain Guy13631f32012-01-30 17:41:55 -0800263 PFNGLINSERTEVENTMARKEREXTPROC eventMark;
264 PFNGLPUSHGROUPMARKEREXTPROC startMark;
265 PFNGLPOPGROUPMARKEREXTPROC endMark;
266
Romain Guydfa10462012-05-12 16:18:58 -0700267 PFNGLLABELOBJECTEXTPROC setLabel;
268 PFNGLGETOBJECTLABELEXTPROC getLabel;
269
Chris Craikba9b6132013-12-15 17:10:19 -0800270 // TEMPORARY properties
271 void initTempProperties();
272 void setTempProperty(const char* name, const char* value);
ztenghuicc3c2562014-01-17 10:34:10 -0800273
Chris Craikf5be3ca2014-04-30 18:20:03 -0700274 float propertyLightDiameter;
275 float propertyLightPosY;
276 float propertyLightPosZ;
277 float propertyAmbientRatio;
ztenghui14a4e352014-08-13 10:44:39 -0700278 int propertyAmbientShadowStrength;
279 int propertySpotShadowStrength;
280
Chris Craik96a5c4c2015-01-27 15:46:35 -0800281 PixelBufferState& pixelBuffer() { return *mPixelBufferState; }
282
Romain Guye190aa62010-11-10 19:01:29 -0800283private:
Romain Guy627c6fd2013-08-21 11:53:18 -0700284 enum OverdrawColorSet {
285 kColorSet_Default = 0,
286 kColorSet_Deuteranomaly
287 };
288
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700289 void initFont();
Romain Guydfa10462012-05-12 16:18:58 -0700290 void initExtensions();
291 void initConstraints();
Romain Guyf9f00162013-05-09 11:50:12 -0700292 void initStaticProperties();
Romain Guydfa10462012-05-12 16:18:58 -0700293
Andreas Gampe64bb4132014-11-22 00:35:09 +0000294 static void eventMarkNull(GLsizei length, const GLchar* marker) { }
295 static void startMarkNull(GLsizei length, const GLchar* marker) { }
Romain Guy13631f32012-01-30 17:41:55 -0800296 static void endMarkNull() { }
297
Andreas Gampe64bb4132014-11-22 00:35:09 +0000298 static void setLabelNull(GLenum type, uint object, GLsizei length,
299 const char* label) { }
300 static void getLabelNull(GLenum type, uint object, GLsizei bufferSize,
Romain Guydfa10462012-05-12 16:18:58 -0700301 GLsizei* length, char* label) {
302 if (length) *length = 0;
303 if (label) *label = '\0';
304 }
305
Chris Craik96a5c4c2015-01-27 15:46:35 -0800306 RenderState* mRenderState;
Romain Guyf3a910b42011-12-12 20:35:21 -0800307
Chris Craik96a5c4c2015-01-27 15:46:35 -0800308 std::unique_ptr<PixelBufferState> mPixelBufferState; // TODO: move to RenderState
Romain Guy15bc6432011-12-13 13:11:32 -0800309
Romain Guya1d3c912011-12-13 14:55:06 -0800310 GLuint mTextureUnit;
311
Romain Guy3bbacf22013-02-06 16:51:04 -0800312 Extensions& mExtensions;
313
Romain Guyf3a910b42011-12-12 20:35:21 -0800314 // Used to render layers
Chris Craik51d6a3d2014-12-22 17:16:56 -0800315 std::unique_ptr<TextureVertex[]> mRegionMesh;
Romain Guy3b748a42013-04-17 18:54:38 -0700316
Romain Guyf3a910b42011-12-12 20:35:21 -0800317 mutable Mutex mGarbageLock;
318 Vector<Layer*> mLayerGarbage;
319
Romain Guye190aa62010-11-10 19:01:29 -0800320 DebugLevel mDebugLevel;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800321 bool mInitialized;
Romain Guy54c1a642012-09-27 17:55:46 -0700322
323 uint32_t mFunctorsCount;
Romain Guy8aa195d2013-06-04 18:00:09 -0700324
Fred Fettinger70735bd2014-08-29 14:02:31 -0500325 // Caches texture bindings for the GL_TEXTURE_2D target
Romain Guy8aa195d2013-06-04 18:00:09 -0700326 GLuint mBoundTextures[REQUIRED_TEXTURE_UNITS_COUNT];
Romain Guy627c6fd2013-08-21 11:53:18 -0700327
328 OverdrawColorSet mOverdrawDebugColorSet;
Romain Guyfb8b7632010-08-23 21:05:08 -0700329}; // class Caches
330
331}; // namespace uirenderer
Romain Guyfb8b7632010-08-23 21:05:08 -0700332}; // namespace android
333
Romain Guy5b3b3522010-10-27 18:57:51 -0700334#endif // ANDROID_HWUI_CACHES_H