blob: ed2d0b350d1b28b84477b0d062262abc3cd21392 [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"
22#include "GradientCache.h"
Chris Craik96a5c4c2015-01-27 15:46:35 -080023#include "PatchCache.h"
Chris Craik96a5c4c2015-01-27 15:46:35 -080024#include "PathCache.h"
John Reck1bcacfd2017-11-03 10:12:19 -070025#include "ProgramCache.h"
Chris Craik96a5c4c2015-01-27 15:46:35 -080026#include "RenderBufferCache.h"
Chris Craik96a5c4c2015-01-27 15:46:35 -080027#include "ResourceCache.h"
28#include "TessellationCache.h"
Chris Craik96a5c4c2015-01-27 15:46:35 -080029#include "TextureCache.h"
John Reck1bcacfd2017-11-03 10:12:19 -070030#include "renderstate/PixelBufferState.h"
31#include "renderstate/TextureState.h"
Chris Craik96a5c4c2015-01-27 15:46:35 -080032#include "thread/TaskManager.h"
John Reck1bcacfd2017-11-03 10:12:19 -070033#include "thread/TaskProcessor.h"
Chris Craik96a5c4c2015-01-27 15:46:35 -080034
Chris Craik96a5c4c2015-01-27 15:46:35 -080035#include <memory>
John Reck1bcacfd2017-11-03 10:12:19 -070036#include <vector>
Chris Craik59744b72014-07-01 17:56:52 -070037
Romain Guy3b748a42013-04-17 18:54:38 -070038#include <GLES3/gl3.h>
39
40#include <utils/KeyedVector.h>
Romain Guyfb8b7632010-08-23 21:05:08 -070041
Romain Guy79537452011-10-12 13:48:51 -070042#include <cutils/compiler.h>
43
Tom Hudson2dc236b2014-10-15 15:46:42 -040044#include <SkPath.h>
45
John Reck272a6852015-07-29 16:48:58 -070046#include <vector>
47
Romain Guyfb8b7632010-08-23 21:05:08 -070048namespace android {
49namespace uirenderer {
50
Romain Guy03750a02010-10-18 14:06:08 -070051///////////////////////////////////////////////////////////////////////////////
Romain Guy03750a02010-10-18 14:06:08 -070052// Caches
53///////////////////////////////////////////////////////////////////////////////
54
John Recke18264b2014-03-12 13:56:30 -070055class RenderNode;
John Reck17035b02014-09-03 07:39:53 -070056class RenderState;
Romain Guybb0acdf2012-03-05 13:44:35 -080057
Chris Craik96a5c4c2015-01-27 15:46:35 -080058class ANDROID_API Caches {
59public:
60 static Caches& createInstance(RenderState& renderState) {
61 LOG_ALWAYS_FATAL_IF(sInstance, "double create of Caches attempted");
62 sInstance = new Caches(renderState);
63 return *sInstance;
64 }
Romain Guyfb8b7632010-08-23 21:05:08 -070065
Chris Craik96a5c4c2015-01-27 15:46:35 -080066 static Caches& getInstance() {
67 LOG_ALWAYS_FATAL_IF(!sInstance, "instance not yet created");
68 return *sInstance;
69 }
Romain Guyfb8b7632010-08-23 21:05:08 -070070
John Reck1bcacfd2017-11-03 10:12:19 -070071 static bool hasInstance() { return sInstance != nullptr; }
72
Chris Craik96a5c4c2015-01-27 15:46:35 -080073private:
Chih-Hung Hsiehfaecb782016-07-21 11:23:06 -070074 explicit Caches(RenderState& renderState);
Chris Craik96a5c4c2015-01-27 15:46:35 -080075 static Caches* sInstance;
Romain Guy9bca4792010-10-25 18:42:25 -070076
Romain Guyfb8b7632010-08-23 21:05:08 -070077public:
John Reck1bcacfd2017-11-03 10:12:19 -070078 enum class FlushMode { Layers = 0, Moderate, Full };
Romain Guybdf76092011-07-18 15:00:43 -070079
80 /**
Romain Guydfa10462012-05-12 16:18:58 -070081 * Initialize caches.
Romain Guy8ff6b9e2011-11-09 20:10:18 -080082 */
Romain Guy3b748a42013-04-17 18:54:38 -070083 bool init();
Romain Guy8ff6b9e2011-11-09 20:10:18 -080084
John Reckdb009172016-03-17 11:02:07 -070085 bool isInitialized() { return mInitialized; }
86
Romain Guy8ff6b9e2011-11-09 20:10:18 -080087 /**
Romain Guybdf76092011-07-18 15:00:43 -070088 * Flush the cache.
89 *
90 * @param mode Indicates how much of the cache should be flushed
91 */
92 void flush(FlushMode mode);
93
Romain Guy5b3b3522010-10-27 18:57:51 -070094 /**
Romain Guy8ff6b9e2011-11-09 20:10:18 -080095 * Destroys all resources associated with this cache. This should
Chris Craikb9ce116d2015-08-20 15:14:06 -070096 * be called after a flush(FlushMode::Full).
Romain Guy8ff6b9e2011-11-09 20:10:18 -080097 */
98 void terminate();
99
100 /**
Romain Guy627c6fd2013-08-21 11:53:18 -0700101 * Returns a non-premultiplied ARGB color for the specified
102 * amount of overdraw (1 for 1x, 2 for 2x, etc.)
103 */
104 uint32_t getOverdrawColor(uint32_t amount) const;
105
106 /**
Romain Guyfe48f652010-11-11 15:36:56 -0800107 * Call this on each frame to ensure that garbage is deleted from
108 * GPU memory.
109 */
110 void clearGarbage();
111
112 /**
Romain Guyada830f2011-01-13 12:13:20 -0800113 * Can be used to delete a layer from a non EGL thread.
Romain Guy57066eb2011-01-12 12:53:32 -0800114 */
Romain Guyada830f2011-01-13 12:13:20 -0800115 void deleteLayerDeferred(Layer* layer);
Romain Guy57066eb2011-01-12 12:53:32 -0800116
Romain Guy82bc7a72012-01-03 14:13:39 -0800117 /**
Romain Guy5b3b3522010-10-27 18:57:51 -0700118 * Returns the mesh used to draw regions. Calling this method will
119 * bind a VBO of type GL_ELEMENT_ARRAY_BUFFER that contains the
120 * indices for the region mesh.
121 */
122 TextureVertex* getRegionMesh();
123
Romain Guyc15008e2010-11-10 11:59:15 -0800124 /**
Romain Guy253f2c22016-09-28 17:34:42 -0700125 * Returns the GL RGBA internal format to use for the current device
126 * If the device supports linear blending and needSRGB is true,
127 * this function returns GL_SRGB8_ALPHA8, otherwise it returns GL_RGBA
128 */
129 constexpr GLint rgbaInternalFormat(bool needSRGB = true) const {
Romain Guyefb4b062017-02-27 11:00:04 -0800130 return extensions().hasLinearBlending() && needSRGB ? GL_SRGB8_ALPHA8 : GL_RGBA;
Romain Guy253f2c22016-09-28 17:34:42 -0700131 }
132
133 /**
Romain Guyc15008e2010-11-10 11:59:15 -0800134 * Displays the memory usage of each cache and the total sum.
135 */
136 void dumpMemoryUsage();
Chet Haase9c1e23b2011-03-24 10:51:31 -0700137 void dumpMemoryUsage(String8& log);
Romain Guyc15008e2010-11-10 11:59:15 -0800138
Romain Guy746b7402010-10-26 16:27:31 -0700139 // Misc
140 GLint maxTextureSize;
Romain Guy3ff0bfd2013-02-25 14:15:37 -0800141
Chris Craik117bdbc2015-02-05 10:12:38 -0800142public:
Romain Guyfb8b7632010-08-23 21:05:08 -0700143 TextureCache textureCache;
Romain Guy8d4aeb72013-02-12 16:08:55 -0800144 RenderBufferCache renderBufferCache;
Romain Guyfb8b7632010-08-23 21:05:08 -0700145 GradientCache gradientCache;
Romain Guyfb8b7632010-08-23 21:05:08 -0700146 PatchCache patchCache;
Chris Craik117bdbc2015-02-05 10:12:38 -0800147 PathCache pathCache;
148 ProgramCache programCache;
Chris Craik05f3d6e2014-06-02 16:27:04 -0700149 TessellationCache tessellationCache;
Romain Guye2d345e2010-09-24 18:39:22 -0700150 FboCache fboCache;
Romain Guy29d89972010-09-22 16:10:57 -0700151
Romain Guy5dc7fa72013-03-11 20:48:31 -0700152 TaskManager tasks;
153
Romain Guyf9f00162013-05-09 11:50:12 -0700154 bool gpuPixelBuffersEnabled;
155
Romain Guydfa10462012-05-12 16:18:58 -0700156 // Debug methods
Romain Guy13631f32012-01-30 17:41:55 -0800157 PFNGLINSERTEVENTMARKEREXTPROC eventMark;
158 PFNGLPUSHGROUPMARKEREXTPROC startMark;
159 PFNGLPOPGROUPMARKEREXTPROC endMark;
160
Chris Craik6c15ffa2015-02-02 13:50:55 -0800161 void setProgram(const ProgramDescription& description);
162 void setProgram(Program* program);
163
John Reck8dc02f92017-07-17 09:55:02 -0700164 const Extensions& extensions() const { return DeviceInfo::get()->extensions(); }
Chris Craik6c15ffa2015-02-02 13:50:55 -0800165 Program& program() { return *mProgram; }
Chris Craik44eb2c02015-01-29 09:45:09 -0800166 PixelBufferState& pixelBufferState() { return *mPixelBufferState; }
167 TextureState& textureState() { return *mTextureState; }
Chris Craik96a5c4c2015-01-27 15:46:35 -0800168
Romain Guye190aa62010-11-10 19:01:29 -0800169private:
Romain Guydfa10462012-05-12 16:18:58 -0700170 void initExtensions();
171 void initConstraints();
Romain Guyf9f00162013-05-09 11:50:12 -0700172 void initStaticProperties();
Romain Guydfa10462012-05-12 16:18:58 -0700173
John Reck1bcacfd2017-11-03 10:12:19 -0700174 static void eventMarkNull(GLsizei length, const GLchar* marker) {}
175 static void startMarkNull(GLsizei length, const GLchar* marker) {}
176 static void endMarkNull() {}
Romain Guy13631f32012-01-30 17:41:55 -0800177
Chris Craik96a5c4c2015-01-27 15:46:35 -0800178 RenderState* mRenderState;
Romain Guy3bbacf22013-02-06 16:51:04 -0800179
Romain Guyf3a910b42011-12-12 20:35:21 -0800180 // Used to render layers
Chris Craik51d6a3d2014-12-22 17:16:56 -0800181 std::unique_ptr<TextureVertex[]> mRegionMesh;
Romain Guy3b748a42013-04-17 18:54:38 -0700182
Romain Guyf3a910b42011-12-12 20:35:21 -0800183 mutable Mutex mGarbageLock;
John Reck272a6852015-07-29 16:48:58 -0700184 std::vector<Layer*> mLayerGarbage;
Romain Guyf3a910b42011-12-12 20:35:21 -0800185
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800186 bool mInitialized;
Romain Guy54c1a642012-09-27 17:55:46 -0700187
Chris Craik6c15ffa2015-02-02 13:50:55 -0800188 // TODO: move below to RenderState
189 PixelBufferState* mPixelBufferState = nullptr;
190 TextureState* mTextureState = nullptr;
John Reck1bcacfd2017-11-03 10:12:19 -0700191 Program* mProgram = nullptr; // note: object owned by ProgramCache
Chris Craik6c15ffa2015-02-02 13:50:55 -0800192
John Reck1bcacfd2017-11-03 10:12:19 -0700193}; // class Caches
Romain Guyfb8b7632010-08-23 21:05:08 -0700194
John Reck1bcacfd2017-11-03 10:12:19 -0700195}; // namespace uirenderer
196}; // namespace android