blob: 29eddde1e42b14243b2deae304e6cdf43b360f7e [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
Chris Craik5e00c7c2016-07-06 16:10:09 -070017#pragma once
Romain Guyfb8b7632010-08-23 21:05:08 -070018
John Reck8dc02f92017-07-17 09:55:02 -070019#include "DeviceInfo.h"
Chris Craik96a5c4c2015-01-27 15:46:35 -080020#include "Extensions.h"
21#include "FboCache.h"
Chris Craikc08820f2015-09-22 14:22:29 -070022#include "GammaFontRenderer.h"
Chris Craik96a5c4c2015-01-27 15:46:35 -080023#include "GradientCache.h"
Chris Craik96a5c4c2015-01-27 15:46:35 -080024#include "PatchCache.h"
25#include "ProgramCache.h"
26#include "PathCache.h"
27#include "RenderBufferCache.h"
28#include "renderstate/PixelBufferState.h"
Chris Craik44eb2c02015-01-29 09:45:09 -080029#include "renderstate/TextureState.h"
Chris Craik96a5c4c2015-01-27 15:46:35 -080030#include "ResourceCache.h"
31#include "TessellationCache.h"
32#include "TextDropShadowCache.h"
33#include "TextureCache.h"
34#include "thread/TaskProcessor.h"
35#include "thread/TaskManager.h"
36
Chris Craik59744b72014-07-01 17:56:52 -070037#include <vector>
Chris Craik96a5c4c2015-01-27 15:46:35 -080038#include <memory>
Chris Craik59744b72014-07-01 17:56:52 -070039
Romain Guy3b748a42013-04-17 18:54:38 -070040#include <GLES3/gl3.h>
41
42#include <utils/KeyedVector.h>
Romain Guyfb8b7632010-08-23 21:05:08 -070043
Romain Guy79537452011-10-12 13:48:51 -070044#include <cutils/compiler.h>
45
Tom Hudson2dc236b2014-10-15 15:46:42 -040046#include <SkPath.h>
47
John Reck272a6852015-07-29 16:48:58 -070048#include <vector>
49
Romain Guyfb8b7632010-08-23 21:05:08 -070050namespace android {
51namespace uirenderer {
52
Romain Guy03750a02010-10-18 14:06:08 -070053///////////////////////////////////////////////////////////////////////////////
Romain Guy03750a02010-10-18 14:06:08 -070054// Caches
55///////////////////////////////////////////////////////////////////////////////
56
John Recke18264b2014-03-12 13:56:30 -070057class RenderNode;
John Reck17035b02014-09-03 07:39:53 -070058class RenderState;
Romain Guybb0acdf2012-03-05 13:44:35 -080059
Chris Craik96a5c4c2015-01-27 15:46:35 -080060class ANDROID_API Caches {
61public:
62 static Caches& createInstance(RenderState& renderState) {
63 LOG_ALWAYS_FATAL_IF(sInstance, "double create of Caches attempted");
64 sInstance = new Caches(renderState);
65 return *sInstance;
66 }
Romain Guyfb8b7632010-08-23 21:05:08 -070067
Chris Craik96a5c4c2015-01-27 15:46:35 -080068 static Caches& getInstance() {
69 LOG_ALWAYS_FATAL_IF(!sInstance, "instance not yet created");
70 return *sInstance;
71 }
Romain Guyfb8b7632010-08-23 21:05:08 -070072
Chris Craik96a5c4c2015-01-27 15:46:35 -080073 static bool hasInstance() {
Chris Craik2507c342015-05-04 14:36:49 -070074 return sInstance != nullptr;
Chris Craik96a5c4c2015-01-27 15:46:35 -080075 }
76private:
Chih-Hung Hsiehfaecb782016-07-21 11:23:06 -070077 explicit Caches(RenderState& renderState);
Chris Craik96a5c4c2015-01-27 15:46:35 -080078 static Caches* sInstance;
Romain Guy9bca4792010-10-25 18:42:25 -070079
Romain Guyfb8b7632010-08-23 21:05:08 -070080public:
Chris Craikb9ce116d2015-08-20 15:14:06 -070081 enum class FlushMode {
82 Layers = 0,
83 Moderate,
84 Full
Romain Guybdf76092011-07-18 15:00:43 -070085 };
86
87 /**
Romain Guydfa10462012-05-12 16:18:58 -070088 * Initialize caches.
Romain Guy8ff6b9e2011-11-09 20:10:18 -080089 */
Romain Guy3b748a42013-04-17 18:54:38 -070090 bool init();
Romain Guy8ff6b9e2011-11-09 20:10:18 -080091
John Reckdb009172016-03-17 11:02:07 -070092 bool isInitialized() { return mInitialized; }
93
Romain Guy8ff6b9e2011-11-09 20:10:18 -080094 /**
Romain Guybdf76092011-07-18 15:00:43 -070095 * Flush the cache.
96 *
97 * @param mode Indicates how much of the cache should be flushed
98 */
99 void flush(FlushMode mode);
100
Romain Guy5b3b3522010-10-27 18:57:51 -0700101 /**
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800102 * Destroys all resources associated with this cache. This should
Chris Craikb9ce116d2015-08-20 15:14:06 -0700103 * be called after a flush(FlushMode::Full).
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800104 */
105 void terminate();
106
107 /**
Romain Guy627c6fd2013-08-21 11:53:18 -0700108 * Returns a non-premultiplied ARGB color for the specified
109 * amount of overdraw (1 for 1x, 2 for 2x, etc.)
110 */
111 uint32_t getOverdrawColor(uint32_t amount) const;
112
113 /**
Romain Guyfe48f652010-11-11 15:36:56 -0800114 * Call this on each frame to ensure that garbage is deleted from
115 * GPU memory.
116 */
117 void clearGarbage();
118
119 /**
Romain Guyada830f2011-01-13 12:13:20 -0800120 * Can be used to delete a layer from a non EGL thread.
Romain Guy57066eb2011-01-12 12:53:32 -0800121 */
Romain Guyada830f2011-01-13 12:13:20 -0800122 void deleteLayerDeferred(Layer* layer);
Romain Guy57066eb2011-01-12 12:53:32 -0800123
Romain Guy82bc7a72012-01-03 14:13:39 -0800124 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700125 * Returns the mesh used to draw regions. Calling this method will
126 * bind a VBO of type GL_ELEMENT_ARRAY_BUFFER that contains the
127 * indices for the region mesh.
128 */
129 TextureVertex* getRegionMesh();
130
Romain Guyc15008e2010-11-10 11:59:15 -0800131 /**
Romain Guy253f2c22016-09-28 17:34:42 -0700132 * Returns the GL RGBA internal format to use for the current device
133 * If the device supports linear blending and needSRGB is true,
134 * this function returns GL_SRGB8_ALPHA8, otherwise it returns GL_RGBA
135 */
136 constexpr GLint rgbaInternalFormat(bool needSRGB = true) const {
Romain Guyefb4b062017-02-27 11:00:04 -0800137 return extensions().hasLinearBlending() && needSRGB ? GL_SRGB8_ALPHA8 : GL_RGBA;
Romain Guy253f2c22016-09-28 17:34:42 -0700138 }
139
140 /**
Romain Guyc15008e2010-11-10 11:59:15 -0800141 * Displays the memory usage of each cache and the total sum.
142 */
143 void dumpMemoryUsage();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700144 void dumpMemoryUsage(String8& log);
Romain Guyc15008e2010-11-10 11:59:15 -0800145
Romain Guy746b7402010-10-26 16:27:31 -0700146 // Misc
147 GLint maxTextureSize;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800148
Chris Craik117bdbc2015-02-05 10:12:38 -0800149public:
Romain Guyfb8b7632010-08-23 21:05:08 -0700150 TextureCache textureCache;
Romain Guy8d4aeb72013-02-12 16:08:55 -0800151 RenderBufferCache renderBufferCache;
Romain Guyfb8b7632010-08-23 21:05:08 -0700152 GradientCache gradientCache;
Romain Guyfb8b7632010-08-23 21:05:08 -0700153 PatchCache patchCache;
Chris Craik117bdbc2015-02-05 10:12:38 -0800154 PathCache pathCache;
155 ProgramCache programCache;
Chris Craik05f3d6e2014-06-02 16:27:04 -0700156 TessellationCache tessellationCache;
Romain Guyfb8b7632010-08-23 21:05:08 -0700157 TextDropShadowCache dropShadowCache;
Romain Guye2d345e2010-09-24 18:39:22 -0700158 FboCache fboCache;
Romain Guy29d89972010-09-22 16:10:57 -0700159
Chris Craikc08820f2015-09-22 14:22:29 -0700160 GammaFontRenderer fontRenderer;
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700161
Romain Guy5dc7fa72013-03-11 20:48:31 -0700162 TaskManager tasks;
163
Romain Guyf9f00162013-05-09 11:50:12 -0700164 bool gpuPixelBuffersEnabled;
165
Romain Guydfa10462012-05-12 16:18:58 -0700166 // Debug methods
Romain Guy13631f32012-01-30 17:41:55 -0800167 PFNGLINSERTEVENTMARKEREXTPROC eventMark;
168 PFNGLPUSHGROUPMARKEREXTPROC startMark;
169 PFNGLPOPGROUPMARKEREXTPROC endMark;
170
Chris Craik6c15ffa2015-02-02 13:50:55 -0800171 void setProgram(const ProgramDescription& description);
172 void setProgram(Program* program);
173
John Reck8dc02f92017-07-17 09:55:02 -0700174 const Extensions& extensions() const { return DeviceInfo::get()->extensions(); }
Chris Craik6c15ffa2015-02-02 13:50:55 -0800175 Program& program() { return *mProgram; }
Chris Craik44eb2c02015-01-29 09:45:09 -0800176 PixelBufferState& pixelBufferState() { return *mPixelBufferState; }
177 TextureState& textureState() { return *mTextureState; }
Chris Craik96a5c4c2015-01-27 15:46:35 -0800178
Romain Guye190aa62010-11-10 19:01:29 -0800179private:
Romain Guydfa10462012-05-12 16:18:58 -0700180 void initExtensions();
181 void initConstraints();
Romain Guyf9f00162013-05-09 11:50:12 -0700182 void initStaticProperties();
Romain Guydfa10462012-05-12 16:18:58 -0700183
Andreas Gampe64bb4132014-11-22 00:35:09 +0000184 static void eventMarkNull(GLsizei length, const GLchar* marker) { }
185 static void startMarkNull(GLsizei length, const GLchar* marker) { }
Romain Guy13631f32012-01-30 17:41:55 -0800186 static void endMarkNull() { }
187
Chris Craik96a5c4c2015-01-27 15:46:35 -0800188 RenderState* mRenderState;
Romain Guy3bbacf22013-02-06 16:51:04 -0800189
Romain Guyf3a910b42011-12-12 20:35:21 -0800190 // Used to render layers
Chris Craik51d6a3d2014-12-22 17:16:56 -0800191 std::unique_ptr<TextureVertex[]> mRegionMesh;
Romain Guy3b748a42013-04-17 18:54:38 -0700192
Romain Guyf3a910b42011-12-12 20:35:21 -0800193 mutable Mutex mGarbageLock;
John Reck272a6852015-07-29 16:48:58 -0700194 std::vector<Layer*> mLayerGarbage;
Romain Guyf3a910b42011-12-12 20:35:21 -0800195
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800196 bool mInitialized;
Romain Guy54c1a642012-09-27 17:55:46 -0700197
Chris Craik6c15ffa2015-02-02 13:50:55 -0800198 // TODO: move below to RenderState
199 PixelBufferState* mPixelBufferState = nullptr;
200 TextureState* mTextureState = nullptr;
201 Program* mProgram = nullptr; // note: object owned by ProgramCache
202
Romain Guyfb8b7632010-08-23 21:05:08 -0700203}; // class Caches
204
205}; // namespace uirenderer
Romain Guyfb8b7632010-08-23 21:05:08 -0700206}; // namespace android