blob: f75d6a082ee41d5bf899fa135acf44d4b73d5155 [file] [log] [blame]
Chet Haasedd78cca2010-10-22 18:59:26 -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
17#define LOG_TAG "OpenGLRenderer"
18
19#include "Caches.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080020
Tom Hudson2dc236b2014-10-15 15:46:42 -040021#include "GammaFontRenderer.h"
Romain Guy09b7c912011-02-02 20:28:09 -080022#include "LayerRenderer.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080023#include "Properties.h"
24#include "renderstate/RenderState.h"
ztenghui63d41ab2014-02-14 13:13:41 -080025#include "ShadowTessellator.h"
Chris Craik65fe5ee2015-01-26 18:06:29 -080026
27#include <utils/Log.h>
28#include <utils/String8.h>
Chet Haasedd78cca2010-10-22 18:59:26 -070029
30namespace android {
Chet Haasedd78cca2010-10-22 18:59:26 -070031namespace uirenderer {
32
Chris Craik96a5c4c2015-01-27 15:46:35 -080033Caches* Caches::sInstance = nullptr;
34
Chet Haasedd78cca2010-10-22 18:59:26 -070035///////////////////////////////////////////////////////////////////////////////
Romain Guybdf76092011-07-18 15:00:43 -070036// Macros
37///////////////////////////////////////////////////////////////////////////////
38
39#if DEBUG_CACHE_FLUSH
Steve Block5baa3a62011-12-20 16:23:08 +000040 #define FLUSH_LOGD(...) ALOGD(__VA_ARGS__)
Romain Guybdf76092011-07-18 15:00:43 -070041#else
42 #define FLUSH_LOGD(...)
43#endif
44
45///////////////////////////////////////////////////////////////////////////////
Chet Haasedd78cca2010-10-22 18:59:26 -070046// Constructors/destructor
47///////////////////////////////////////////////////////////////////////////////
48
Chris Craik96a5c4c2015-01-27 15:46:35 -080049Caches::Caches(RenderState& renderState)
Chris Craik117bdbc2015-02-05 10:12:38 -080050 : gradientCache(mExtensions)
51 , patchCache(renderState)
52 , programCache(mExtensions)
Chris Craik44eb2c02015-01-29 09:45:09 -080053 , dither(*this)
Chris Craik96a5c4c2015-01-27 15:46:35 -080054 , mRenderState(&renderState)
Chris Craik96a5c4c2015-01-27 15:46:35 -080055 , mInitialized(false) {
56 INIT_LOGD("Creating OpenGL renderer caches");
Romain Guy8ff6b9e2011-11-09 20:10:18 -080057 init();
Romain Guyb1d0a4e2012-07-13 18:25:35 -070058 initFont();
Romain Guydfa10462012-05-12 16:18:58 -070059 initConstraints();
Romain Guyf9f00162013-05-09 11:50:12 -070060 initStaticProperties();
Romain Guy0f667532013-03-01 14:31:04 -080061 initExtensions();
Chet Haasedd78cca2010-10-22 18:59:26 -070062}
63
Romain Guy3b748a42013-04-17 18:54:38 -070064bool Caches::init() {
65 if (mInitialized) return false;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080066
John Reckfbc8df02014-11-14 16:18:41 -080067 ATRACE_NAME("Caches::init");
68
Chris Craikd41c4d82015-01-05 15:51:13 -080069 mRegionMesh = nullptr;
Chris Craik6c15ffa2015-02-02 13:50:55 -080070 mProgram = nullptr;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080071
Romain Guy54c1a642012-09-27 17:55:46 -070072 mFunctorsCount = 0;
73
Chris Craik96a5c4c2015-01-27 15:46:35 -080074 patchCache.init();
Romain Guy3b748a42013-04-17 18:54:38 -070075
Romain Guy8ff6b9e2011-11-09 20:10:18 -080076 mInitialized = true;
Romain Guy3b748a42013-04-17 18:54:38 -070077
Chris Craik44eb2c02015-01-29 09:45:09 -080078 mPixelBufferState = new PixelBufferState();
79 mTextureState = new TextureState();
Romain Guy8aa195d2013-06-04 18:00:09 -070080
Romain Guy3b748a42013-04-17 18:54:38 -070081 return true;
Romain Guy8ff6b9e2011-11-09 20:10:18 -080082}
83
Romain Guyb1d0a4e2012-07-13 18:25:35 -070084void Caches::initFont() {
85 fontRenderer = GammaFontRenderer::createRenderer();
86}
87
Romain Guydfa10462012-05-12 16:18:58 -070088void Caches::initExtensions() {
Romain Guy3bbacf22013-02-06 16:51:04 -080089 if (mExtensions.hasDebugMarker()) {
Romain Guydfa10462012-05-12 16:18:58 -070090 eventMark = glInsertEventMarkerEXT;
Romain Guy0f667532013-03-01 14:31:04 -080091
Chris Craikff785832013-03-08 13:12:16 -080092 startMark = glPushGroupMarkerEXT;
93 endMark = glPopGroupMarkerEXT;
Romain Guydfa10462012-05-12 16:18:58 -070094 } else {
95 eventMark = eventMarkNull;
96 startMark = startMarkNull;
97 endMark = endMarkNull;
98 }
Romain Guydfa10462012-05-12 16:18:58 -070099}
100
101void Caches::initConstraints() {
Romain Guydfa10462012-05-12 16:18:58 -0700102 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
103}
104
Romain Guyf9f00162013-05-09 11:50:12 -0700105void Caches::initStaticProperties() {
106 gpuPixelBuffersEnabled = false;
107
108 // OpenGL ES 3.0+ specific features
Romain Guy7f430762013-06-13 14:29:40 -0700109 if (mExtensions.hasPixelBufferObjects()) {
Romain Guyf9f00162013-05-09 11:50:12 -0700110 char property[PROPERTY_VALUE_MAX];
111 if (property_get(PROPERTY_ENABLE_GPU_PIXEL_BUFFERS, property, "true") > 0) {
112 gpuPixelBuffersEnabled = !strcmp(property, "true");
113 }
114 }
115}
116
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800117void Caches::terminate() {
118 if (!mInitialized) return;
Chris Craik51d6a3d2014-12-22 17:16:56 -0800119 mRegionMesh.release();
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800120
121 fboCache.clear();
122
123 programCache.clear();
Chris Craik34725682015-02-05 09:06:30 -0800124 mProgram = nullptr;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800125
Romain Guy3b748a42013-04-17 18:54:38 -0700126 patchCache.clear();
127
Romain Guy46bfc482013-08-16 18:38:29 -0700128 clearGarbage();
129
Chris Craik44eb2c02015-01-29 09:45:09 -0800130 delete mPixelBufferState;
131 mPixelBufferState = nullptr;
132 delete mTextureState;
133 mTextureState = nullptr;
Romain Guy8ff6b9e2011-11-09 20:10:18 -0800134 mInitialized = false;
Romain Guy5b3b3522010-10-27 18:57:51 -0700135}
136
Chris Craik6c15ffa2015-02-02 13:50:55 -0800137void Caches::setProgram(const ProgramDescription& description) {
138 setProgram(programCache.get(description));
139}
140
141void Caches::setProgram(Program* program) {
142 if (!program || !program->isInUse()) {
143 if (mProgram) {
144 mProgram->remove();
145 }
146 if (program) {
147 program->use();
148 }
149 mProgram = program;
150 }
151}
152
Romain Guy5b3b3522010-10-27 18:57:51 -0700153///////////////////////////////////////////////////////////////////////////////
Romain Guyc15008e2010-11-10 11:59:15 -0800154// Debug
155///////////////////////////////////////////////////////////////////////////////
156
Romain Guy627c6fd2013-08-21 11:53:18 -0700157uint32_t Caches::getOverdrawColor(uint32_t amount) const {
158 static uint32_t sOverdrawColors[2][4] = {
159 { 0x2f0000ff, 0x2f00ff00, 0x3fff0000, 0x7fff0000 },
160 { 0x2f0000ff, 0x4fffff00, 0x5fff8ad8, 0x7fff0000 }
161 };
162 if (amount < 1) amount = 1;
163 if (amount > 4) amount = 4;
Chris Craik2507c342015-05-04 14:36:49 -0700164
165 int overdrawColorIndex = static_cast<int>(Properties::overdrawColorSet);
166 return sOverdrawColors[overdrawColorIndex][amount - 1];
Romain Guy627c6fd2013-08-21 11:53:18 -0700167}
168
Romain Guyc15008e2010-11-10 11:59:15 -0800169void Caches::dumpMemoryUsage() {
Chet Haase9c1e23b2011-03-24 10:51:31 -0700170 String8 stringLog;
171 dumpMemoryUsage(stringLog);
Steve Block5baa3a62011-12-20 16:23:08 +0000172 ALOGD("%s", stringLog.string());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700173}
174
175void Caches::dumpMemoryUsage(String8 &log) {
John Reck0e89e2b2014-10-31 14:49:06 -0700176 uint32_t total = 0;
Chet Haase9c1e23b2011-03-24 10:51:31 -0700177 log.appendFormat("Current memory usage / total memory usage (bytes):\n");
178 log.appendFormat(" TextureCache %8d / %8d\n",
179 textureCache.getSize(), textureCache.getMaxSize());
John Reck17035b02014-09-03 07:39:53 -0700180 log.appendFormat(" LayerCache %8d / %8d (numLayers = %zu)\n",
181 layerCache.getSize(), layerCache.getMaxSize(), layerCache.getCount());
John Reck0e89e2b2014-10-31 14:49:06 -0700182 if (mRenderState) {
183 int memused = 0;
John Reck57998012015-01-29 10:17:57 -0800184 for (std::set<Layer*>::iterator it = mRenderState->mActiveLayers.begin();
John Reck0e89e2b2014-10-31 14:49:06 -0700185 it != mRenderState->mActiveLayers.end(); it++) {
186 const Layer* layer = *it;
187 log.appendFormat(" Layer size %dx%d; isTextureLayer()=%d; texid=%u fbo=%u; refs=%d\n",
188 layer->getWidth(), layer->getHeight(),
Chris Craikf27133d2015-02-19 09:51:53 -0800189 layer->isTextureLayer(), layer->getTextureId(),
John Reck0e89e2b2014-10-31 14:49:06 -0700190 layer->getFbo(), layer->getStrongCount());
John Reck88f5fc72014-11-03 10:32:24 -0800191 memused += layer->getWidth() * layer->getHeight() * 4;
John Reck0e89e2b2014-10-31 14:49:06 -0700192 }
193 log.appendFormat(" Layers total %8d (numLayers = %zu)\n",
194 memused, mRenderState->mActiveLayers.size());
195 total += memused;
196 }
Romain Guy8d4aeb72013-02-12 16:08:55 -0800197 log.appendFormat(" RenderBufferCache %8d / %8d\n",
198 renderBufferCache.getSize(), renderBufferCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700199 log.appendFormat(" GradientCache %8d / %8d\n",
200 gradientCache.getSize(), gradientCache.getMaxSize());
201 log.appendFormat(" PathCache %8d / %8d\n",
202 pathCache.getSize(), pathCache.getMaxSize());
Chris Craik05f3d6e2014-06-02 16:27:04 -0700203 log.appendFormat(" TessellationCache %8d / %8d\n",
204 tessellationCache.getSize(), tessellationCache.getMaxSize());
Chet Haase9c1e23b2011-03-24 10:51:31 -0700205 log.appendFormat(" TextDropShadowCache %8d / %8d\n", dropShadowCache.getSize(),
Romain Guyc15008e2010-11-10 11:59:15 -0800206 dropShadowCache.getMaxSize());
Romain Guy3b748a42013-04-17 18:54:38 -0700207 log.appendFormat(" PatchCache %8d / %8d\n",
208 patchCache.getSize(), patchCache.getMaxSize());
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700209 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
Victoria Lease1e546812013-06-25 14:25:17 -0700210 const uint32_t sizeA8 = fontRenderer->getFontRendererSize(i, GL_ALPHA);
211 const uint32_t sizeRGBA = fontRenderer->getFontRendererSize(i, GL_RGBA);
212 log.appendFormat(" FontRenderer %d A8 %8d / %8d\n", i, sizeA8, sizeA8);
213 log.appendFormat(" FontRenderer %d RGBA %8d / %8d\n", i, sizeRGBA, sizeRGBA);
214 log.appendFormat(" FontRenderer %d total %8d / %8d\n", i, sizeA8 + sizeRGBA,
215 sizeA8 + sizeRGBA);
Romain Guyc15008e2010-11-10 11:59:15 -0800216 }
Romain Guyd2ba50a2011-05-27 10:21:07 -0700217 log.appendFormat("Other:\n");
Chet Haase9c1e23b2011-03-24 10:51:31 -0700218 log.appendFormat(" FboCache %8d / %8d\n",
219 fboCache.getSize(), fboCache.getMaxSize());
Romain Guyc15008e2010-11-10 11:59:15 -0800220
Romain Guyc15008e2010-11-10 11:59:15 -0800221 total += textureCache.getSize();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800222 total += renderBufferCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800223 total += gradientCache.getSize();
224 total += pathCache.getSize();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700225 total += tessellationCache.getSize();
Romain Guyc15008e2010-11-10 11:59:15 -0800226 total += dropShadowCache.getSize();
Romain Guy3b748a42013-04-17 18:54:38 -0700227 total += patchCache.getSize();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700228 for (uint32_t i = 0; i < fontRenderer->getFontRendererCount(); i++) {
Victoria Lease1e546812013-06-25 14:25:17 -0700229 total += fontRenderer->getFontRendererSize(i, GL_ALPHA);
230 total += fontRenderer->getFontRendererSize(i, GL_RGBA);
Romain Guyc15008e2010-11-10 11:59:15 -0800231 }
232
Chet Haase9c1e23b2011-03-24 10:51:31 -0700233 log.appendFormat("Total memory usage:\n");
234 log.appendFormat(" %d bytes, %.2f MB\n", total, total / 1024.0f / 1024.0f);
Romain Guyc15008e2010-11-10 11:59:15 -0800235}
236
237///////////////////////////////////////////////////////////////////////////////
Romain Guyfe48f652010-11-11 15:36:56 -0800238// Memory management
239///////////////////////////////////////////////////////////////////////////////
240
241void Caches::clearGarbage() {
242 textureCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800243 pathCache.clearGarbage();
Romain Guye3b0a012013-06-26 15:45:41 -0700244 patchCache.clearGarbage();
Romain Guyfe48f652010-11-11 15:36:56 -0800245}
246
Romain Guybdf76092011-07-18 15:00:43 -0700247void Caches::flush(FlushMode mode) {
248 FLUSH_LOGD("Flushing caches (mode %d)", mode);
249
Romain Guyb0a41ed2013-08-16 14:44:38 -0700250 // We must stop tasks before clearing caches
251 if (mode > kFlushMode_Layers) {
252 tasks.stop();
253 }
254
Romain Guybdf76092011-07-18 15:00:43 -0700255 switch (mode) {
256 case kFlushMode_Full:
257 textureCache.clear();
258 patchCache.clear();
259 dropShadowCache.clear();
260 gradientCache.clear();
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700261 fontRenderer->clear();
Romain Guyb7463712013-08-16 13:55:29 -0700262 fboCache.clear();
Romain Guy211efea2012-07-31 21:16:07 -0700263 dither.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700264 // fall through
265 case kFlushMode_Moderate:
Romain Guyb1d0a4e2012-07-13 18:25:35 -0700266 fontRenderer->flush();
Romain Guyeca0ca22011-11-04 15:12:29 -0700267 textureCache.flush();
Romain Guybdf76092011-07-18 15:00:43 -0700268 pathCache.clear();
Chris Craik05f3d6e2014-06-02 16:27:04 -0700269 tessellationCache.clear();
Romain Guy6d7475d2011-07-27 16:28:21 -0700270 // fall through
271 case kFlushMode_Layers:
272 layerCache.clear();
Romain Guy8d4aeb72013-02-12 16:08:55 -0800273 renderBufferCache.clear();
Romain Guybdf76092011-07-18 15:00:43 -0700274 break;
275 }
Chet Haase6a2d17f2012-09-30 12:14:13 -0700276
277 clearGarbage();
John Reck4ced2622014-09-19 10:10:19 -0700278 glFinish();
Romain Guybdf76092011-07-18 15:00:43 -0700279}
280
Romain Guyfe48f652010-11-11 15:36:56 -0800281///////////////////////////////////////////////////////////////////////////////
Romain Guy85ef80d2012-09-13 20:26:50 -0700282// Tiling
283///////////////////////////////////////////////////////////////////////////////
284
Romain Guyf735c8e2013-01-31 17:45:55 -0800285void Caches::startTiling(GLuint x, GLuint y, GLuint width, GLuint height, bool discard) {
Chris Craik2507c342015-05-04 14:36:49 -0700286 if (mExtensions.hasTiledRendering() && !Properties::debugOverdraw) {
Romain Guyf735c8e2013-01-31 17:45:55 -0800287 glStartTilingQCOM(x, y, width, height, (discard ? GL_NONE : GL_COLOR_BUFFER_BIT0_QCOM));
Romain Guy85ef80d2012-09-13 20:26:50 -0700288 }
289}
290
291void Caches::endTiling() {
Chris Craik2507c342015-05-04 14:36:49 -0700292 if (mExtensions.hasTiledRendering() && !Properties::debugOverdraw) {
Romain Guy2b7028e2012-09-19 17:25:38 -0700293 glEndTilingQCOM(GL_COLOR_BUFFER_BIT0_QCOM);
Romain Guy85ef80d2012-09-13 20:26:50 -0700294 }
295}
296
Romain Guy54c1a642012-09-27 17:55:46 -0700297bool Caches::hasRegisteredFunctors() {
298 return mFunctorsCount > 0;
299}
300
301void Caches::registerFunctors(uint32_t functorCount) {
302 mFunctorsCount += functorCount;
303}
304
305void Caches::unregisterFunctors(uint32_t functorCount) {
306 if (functorCount > mFunctorsCount) {
307 mFunctorsCount = 0;
308 } else {
309 mFunctorsCount -= functorCount;
310 }
311}
312
Romain Guy85ef80d2012-09-13 20:26:50 -0700313///////////////////////////////////////////////////////////////////////////////
314// Regions
315///////////////////////////////////////////////////////////////////////////////
316
Romain Guy5b3b3522010-10-27 18:57:51 -0700317TextureVertex* Caches::getRegionMesh() {
318 // Create the mesh, 2 triangles and 4 vertices per rectangle in the region
319 if (!mRegionMesh) {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800320 mRegionMesh.reset(new TextureVertex[kMaxNumberOfQuads * 4]);
Romain Guy5b3b3522010-10-27 18:57:51 -0700321 }
322
Chris Craik51d6a3d2014-12-22 17:16:56 -0800323 return mRegionMesh.get();
Romain Guy5b3b3522010-10-27 18:57:51 -0700324}
325
Chris Craikba9b6132013-12-15 17:10:19 -0800326///////////////////////////////////////////////////////////////////////////////
327// Temporary Properties
328///////////////////////////////////////////////////////////////////////////////
329
Chet Haasedd78cca2010-10-22 18:59:26 -0700330}; // namespace uirenderer
331}; // namespace android